-
Notifications
You must be signed in to change notification settings - Fork 110
/
Dhcp4Io.c
1656 lines (1364 loc) · 45 KB
/
Dhcp4Io.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
EFI DHCP protocol implementation.
Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "Dhcp4Impl.h"
UINT32 mDhcp4DefaultTimeout[4] = { 4, 8, 16, 32 };
/**
Send an initial DISCOVER or REQUEST message according to the
DHCP service's current state.
@param[in] DhcpSb The DHCP service instance
@retval EFI_SUCCESS The request has been sent
@retval other Some error occurs when sending the request.
**/
EFI_STATUS
DhcpInitRequest (
IN DHCP_SERVICE *DhcpSb
)
{
EFI_STATUS Status;
ASSERT ((DhcpSb->DhcpState == Dhcp4Init) || (DhcpSb->DhcpState == Dhcp4InitReboot));
//
// Clear initial time to make sure that elapsed-time is set to 0 for first Discover or REQUEST message.
//
DhcpSb->ActiveChild->ElaspedTime = 0;
if (DhcpSb->DhcpState == Dhcp4Init) {
DhcpSetState (DhcpSb, Dhcp4Selecting, FALSE);
Status = DhcpSendMessage (DhcpSb, NULL, NULL, DHCP_MSG_DISCOVER, NULL);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_NET | DEBUG_ERROR, "%a: SendError, new state is Dhcp4Init. Code=%r\n", __FUNCTION__, Status));
DhcpSb->DhcpState = Dhcp4Init;
return Status;
}
} else {
DhcpSetState (DhcpSb, Dhcp4Rebooting, FALSE);
Status = DhcpSendMessage (DhcpSb, NULL, NULL, DHCP_MSG_REQUEST, NULL);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_NET | DEBUG_ERROR, "%a: SendError, new state is Dhcp4InitReboot. Code-%r\n", __FUNCTION__, Status));
DhcpSb->DhcpState = Dhcp4InitReboot;
return Status;
}
}
return EFI_SUCCESS;
}
/**
Call user provided callback function, and return the value the
function returns. If the user doesn't provide a callback, a
proper return value is selected to let the caller continue the
normal process.
@param[in] DhcpSb The DHCP service instance
@param[in] Event The event as defined in the spec
@param[in] Packet The current packet trigger the event
@param[out] NewPacket The user's return new packet
@retval EFI_NOT_READY Direct the caller to continue collecting the offer.
@retval EFI_SUCCESS The user function returns success.
@retval EFI_ABORTED The user function ask it to abort.
**/
EFI_STATUS
DhcpCallUser (
IN DHCP_SERVICE *DhcpSb,
IN EFI_DHCP4_EVENT Event,
IN EFI_DHCP4_PACKET *Packet OPTIONAL,
OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL
)
{
EFI_DHCP4_CONFIG_DATA *Config;
EFI_STATUS Status;
if (NewPacket != NULL) {
*NewPacket = NULL;
}
DEBUG ((DEBUG_NET, "%a: Calling user code with DhcpState=%d.\n", __FUNCTION__, Event));
//
// If user doesn't provide the call back function, return the value
// that directs the client to continue the normal process.
// In Dhcp4Selecting EFI_SUCCESS tells the client to stop collecting
// the offers and select a offer, EFI_NOT_READY tells the client to
// collect more offers.
//
Config = &DhcpSb->ActiveConfig;
if (Config->Dhcp4Callback == NULL) {
if (Event == Dhcp4RcvdOffer) {
return EFI_NOT_READY;
}
return EFI_SUCCESS;
}
Status = Config->Dhcp4Callback (
&DhcpSb->ActiveChild->Dhcp4Protocol,
Config->CallbackContext,
(EFI_DHCP4_STATE)DhcpSb->DhcpState,
Event,
Packet,
NewPacket
);
//
// User's callback should only return EFI_SUCCESS, EFI_NOT_READY,
// and EFI_ABORTED. If it returns values other than those, assume
// it to be EFI_ABORTED.
//
if ((Status == EFI_SUCCESS) || (Status == EFI_NOT_READY)) {
return Status;
}
return EFI_ABORTED;
}
/**
Notify the user about the operation result.
@param DhcpSb DHCP service instance
@param Which Which notify function to signal
**/
VOID
DhcpNotifyUser (
IN DHCP_SERVICE *DhcpSb,
IN INTN Which
)
{
DHCP_PROTOCOL *Child;
if ((Child = DhcpSb->ActiveChild) == NULL) {
return;
}
if ((Child->CompletionEvent != NULL) &&
((Which == DHCP_NOTIFY_COMPLETION) || (Which == DHCP_NOTIFY_ALL))
)
{
gBS->SignalEvent (Child->CompletionEvent);
Child->CompletionEvent = NULL;
}
if ((Child->RenewRebindEvent != NULL) &&
((Which == DHCP_NOTIFY_RENEWREBIND) || (Which == DHCP_NOTIFY_ALL))
)
{
gBS->SignalEvent (Child->RenewRebindEvent);
Child->RenewRebindEvent = NULL;
}
}
/**
Set the DHCP state. If CallUser is true, it will try to notify
the user before change the state by DhcpNotifyUser. It returns
EFI_ABORTED if the user return EFI_ABORTED, otherwise, it returns
EFI_SUCCESS. If CallUser is FALSE, it isn't necessary to test
the return value of this function.
@param DhcpSb The DHCP service instance
@param State The new DHCP state to change to
@param CallUser Whether we need to call user
@retval EFI_SUCCESS The state is changed
@retval EFI_ABORTED The user asks to abort the DHCP process.
**/
EFI_STATUS
DhcpSetState (
IN OUT DHCP_SERVICE *DhcpSb,
IN INTN State,
IN BOOLEAN CallUser
)
{
EFI_STATUS Status;
DEBUG ((DEBUG_NET, "%a: Setting state to %d, CallUser=%d.\n", __FUNCTION__, State, CallUser));
if (CallUser) {
Status = EFI_SUCCESS;
if (State == Dhcp4Renewing) {
Status = DhcpCallUser (DhcpSb, Dhcp4EnterRenewing, NULL, NULL);
} else if (State == Dhcp4Rebinding) {
Status = DhcpCallUser (DhcpSb, Dhcp4EnterRebinding, NULL, NULL);
} else if (State == Dhcp4Bound) {
Status = DhcpCallUser (DhcpSb, Dhcp4BoundCompleted, NULL, NULL);
}
if (EFI_ERROR (Status)) {
return Status;
}
}
//
// Update the retransmission timer during the state transition.
// This will clear the retry count. This is also why the rule
// first transit the state, then send packets.
//
if (State == Dhcp4Selecting) {
DhcpSb->MaxRetries = DhcpSb->ActiveConfig.DiscoverTryCount;
} else {
DhcpSb->MaxRetries = DhcpSb->ActiveConfig.RequestTryCount;
}
if (DhcpSb->MaxRetries == 0) {
DhcpSb->MaxRetries = 4;
}
DhcpSb->CurRetry = 0;
DhcpSb->PacketToLive = 0;
DhcpSb->LastTimeout = 0;
DhcpSb->DhcpState = State;
return EFI_SUCCESS;
}
/**
Set the retransmit timer for the packet. It will select from either
the discover timeouts/request timeouts or the default timeout values.
@param DhcpSb The DHCP service instance.
**/
VOID
DhcpSetTransmitTimer (
IN OUT DHCP_SERVICE *DhcpSb
)
{
UINT32 *Times;
ASSERT (DhcpSb->MaxRetries > DhcpSb->CurRetry);
if (DhcpSb->DhcpState == Dhcp4Selecting) {
Times = DhcpSb->ActiveConfig.DiscoverTimeout;
} else {
Times = DhcpSb->ActiveConfig.RequestTimeout;
}
if (Times == NULL) {
Times = mDhcp4DefaultTimeout;
}
DhcpSb->PacketToLive = Times[DhcpSb->CurRetry];
DhcpSb->LastTimeout = DhcpSb->PacketToLive;
return;
}
/**
Compute the lease. If the server grants a permanent lease, just
process it as a normal timeout value since the lease will last
more than 100 years.
@param DhcpSb The DHCP service instance
@param Para The DHCP parameter extracted from the server's
response.
**/
VOID
DhcpComputeLease (
IN OUT DHCP_SERVICE *DhcpSb,
IN DHCP_PARAMETER *Para
)
{
ASSERT (Para != NULL);
DhcpSb->Lease = Para->Lease;
DhcpSb->T2 = Para->T2;
DhcpSb->T1 = Para->T1;
if (DhcpSb->Lease == 0) {
DhcpSb->Lease = DHCP_DEFAULT_LEASE;
}
if ((DhcpSb->T2 == 0) || (DhcpSb->T2 >= Para->Lease)) {
DhcpSb->T2 = Para->Lease - (Para->Lease >> 3);
}
if ((DhcpSb->T1 == 0) || (DhcpSb->T1 >= Para->T2)) {
DhcpSb->T1 = DhcpSb->Lease >> 1;
}
}
/**
Configure a UDP IO port to use the acquired lease address.
DHCP driver needs this port to unicast packet to the server
such as DHCP release.
@param[in] UdpIo The UDP IO to configure
@param[in] Context Dhcp service instance.
@retval EFI_SUCCESS The UDP IO port is successfully configured.
@retval Others It failed to configure the port.
**/
EFI_STATUS
EFIAPI
DhcpConfigLeaseIoPort (
IN UDP_IO *UdpIo,
IN VOID *Context
)
{
EFI_UDP4_CONFIG_DATA UdpConfigData;
EFI_IPv4_ADDRESS Subnet;
EFI_IPv4_ADDRESS Gateway;
DHCP_SERVICE *DhcpSb;
EFI_STATUS Status;
IP4_ADDR Ip;
DhcpSb = (DHCP_SERVICE *)Context;
UdpConfigData.AcceptBroadcast = FALSE;
UdpConfigData.AcceptPromiscuous = FALSE;
UdpConfigData.AcceptAnyPort = FALSE;
UdpConfigData.AllowDuplicatePort = TRUE;
UdpConfigData.TypeOfService = 0;
UdpConfigData.TimeToLive = 64;
UdpConfigData.DoNotFragment = FALSE;
UdpConfigData.ReceiveTimeout = 1;
UdpConfigData.TransmitTimeout = 0;
UdpConfigData.UseDefaultAddress = FALSE;
UdpConfigData.StationPort = DHCP_CLIENT_PORT;
UdpConfigData.RemotePort = DHCP_SERVER_PORT;
Ip = HTONL (DhcpSb->ClientAddr);
CopyMem (&UdpConfigData.StationAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));
Ip = HTONL (DhcpSb->Netmask);
CopyMem (&UdpConfigData.SubnetMask, &Ip, sizeof (EFI_IPv4_ADDRESS));
ZeroMem (&UdpConfigData.RemoteAddress, sizeof (EFI_IPv4_ADDRESS));
Status = UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, &UdpConfigData);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Add a default route if received from the server.
//
if ((DhcpSb->Para != NULL) && (DhcpSb->Para->Router != 0)) {
ZeroMem (&Subnet, sizeof (EFI_IPv4_ADDRESS));
Ip = HTONL (DhcpSb->Para->Router);
CopyMem (&Gateway, &Ip, sizeof (EFI_IPv4_ADDRESS));
UdpIo->Protocol.Udp4->Routes (UdpIo->Protocol.Udp4, FALSE, &Subnet, &Subnet, &Gateway);
}
return EFI_SUCCESS;
}
/**
Update the lease states when a new lease is acquired. It will not only
save the acquired the address and lease time, it will also create a UDP
child to provide address resolution for the address.
@param DhcpSb The DHCP service instance
@retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
@retval EFI_SUCCESS The lease is recorded.
**/
EFI_STATUS
DhcpLeaseAcquired (
IN OUT DHCP_SERVICE *DhcpSb
)
{
DhcpSb->ClientAddr = EFI_NTOHL (DhcpSb->Selected->Dhcp4.Header.YourAddr);
if (DhcpSb->Para != NULL) {
DhcpSb->Netmask = DhcpSb->Para->NetMask;
DhcpSb->ServerAddr = DhcpSb->Para->ServerId;
}
if (DhcpSb->Netmask == 0) {
return EFI_ABORTED;
}
if (DhcpSb->LeaseIoPort != NULL) {
UdpIoFreeIo (DhcpSb->LeaseIoPort);
}
//
// Create a UDP/IP child to provide ARP service for the Leased IP,
// and transmit unicast packet with it as source address. Don't
// start receive on this port, the queued packet will be timeout.
//
DhcpSb->LeaseIoPort = UdpIoCreateIo (
DhcpSb->Controller,
DhcpSb->Image,
DhcpConfigLeaseIoPort,
UDP_IO_UDP4_VERSION,
DhcpSb
);
if (DhcpSb->LeaseIoPort == NULL) {
return EFI_OUT_OF_RESOURCES;
}
if (!DHCP_IS_BOOTP (DhcpSb->Para)) {
DhcpComputeLease (DhcpSb, DhcpSb->Para);
}
return DhcpSetState (DhcpSb, Dhcp4Bound, TRUE);
}
/**
Clean up the DHCP related states, IoStatus isn't reset.
@param DhcpSb The DHCP instance service.
**/
VOID
DhcpCleanLease (
IN DHCP_SERVICE *DhcpSb
)
{
DEBUG ((DEBUG_NET, "%a: Setting state to Init.\n", __FUNCTION__));
DhcpSb->DhcpState = Dhcp4Init;
DhcpSb->Xid = DhcpSb->Xid + 1;
DhcpSb->ClientAddr = 0;
DhcpSb->Netmask = 0;
DhcpSb->ServerAddr = 0;
if (DhcpSb->LastOffer != NULL) {
FreePool (DhcpSb->LastOffer);
DhcpSb->LastOffer = NULL;
}
if (DhcpSb->Selected != NULL) {
FreePool (DhcpSb->Selected);
DhcpSb->Selected = NULL;
}
if (DhcpSb->Para != NULL) {
FreePool (DhcpSb->Para);
DhcpSb->Para = NULL;
}
DhcpSb->Lease = 0;
DhcpSb->T1 = 0;
DhcpSb->T2 = 0;
DhcpSb->ExtraRefresh = FALSE;
if (DhcpSb->LeaseIoPort != NULL) {
UdpIoFreeIo (DhcpSb->LeaseIoPort);
DhcpSb->LeaseIoPort = NULL;
}
if (DhcpSb->LastPacket != NULL) {
FreePool (DhcpSb->LastPacket);
DhcpSb->LastPacket = NULL;
}
DhcpSb->PacketToLive = 0;
DhcpSb->LastTimeout = 0;
DhcpSb->CurRetry = 0;
DhcpSb->MaxRetries = 0;
DhcpSb->LeaseLife = 0;
//
// Clean active config data.
//
DhcpCleanConfigure (&DhcpSb->ActiveConfig);
}
/**
Select a offer among all the offers collected. If the offer selected is
of BOOTP, the lease is recorded and user notified. If the offer is of
DHCP, it will request the offer from the server.
@param[in] DhcpSb The DHCP service instance.
@retval EFI_SUCCESS One of the offer is selected.
**/
EFI_STATUS
DhcpChooseOffer (
IN DHCP_SERVICE *DhcpSb
)
{
EFI_DHCP4_PACKET *Selected;
EFI_DHCP4_PACKET *NewPacket;
EFI_DHCP4_PACKET *TempPacket;
EFI_STATUS Status;
ASSERT (DhcpSb->LastOffer != NULL);
//
// User will cache previous offers if he wants to select
// from multiple offers. If user provides an invalid packet,
// use the last offer, otherwise use the provided packet.
//
NewPacket = NULL;
Status = DhcpCallUser (DhcpSb, Dhcp4SelectOffer, DhcpSb->LastOffer, &NewPacket);
if (EFI_ERROR (Status)) {
return Status;
}
Selected = DhcpSb->LastOffer;
if ((NewPacket != NULL) && !EFI_ERROR (DhcpValidateOptions (NewPacket, NULL))) {
TempPacket = (EFI_DHCP4_PACKET *)AllocatePool (NewPacket->Size);
if (TempPacket != NULL) {
CopyMem (TempPacket, NewPacket, NewPacket->Size);
FreePool (Selected);
Selected = TempPacket;
}
}
DhcpSb->Selected = Selected;
DhcpSb->LastOffer = NULL;
DhcpSb->Para = NULL;
DhcpValidateOptions (Selected, &DhcpSb->Para);
//
// A bootp offer has been selected, save the lease status,
// enter bound state then notify the user.
//
if (DHCP_IS_BOOTP (DhcpSb->Para)) {
Status = DhcpLeaseAcquired (DhcpSb);
if (EFI_ERROR (Status)) {
return Status;
}
DhcpSb->IoStatus = EFI_SUCCESS;
DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_ALL);
return EFI_SUCCESS;
}
//
// Send a DHCP requests
//
Status = DhcpSetState (DhcpSb, Dhcp4Requesting, TRUE);
if (EFI_ERROR (Status)) {
return Status;
}
return DhcpSendMessage (DhcpSb, Selected, DhcpSb->Para, DHCP_MSG_REQUEST, NULL);
}
/**
Terminate the current address acquire. All the allocated resources
are released. Be careful when calling this function. A rule related
to this is: only call DhcpEndSession at the highest level, such as
DhcpInput, DhcpOnTimerTick...At the other level, just return error.
@param[in] DhcpSb The DHCP service instance
@param[in] Status The result of the DHCP process.
**/
VOID
DhcpEndSession (
IN DHCP_SERVICE *DhcpSb,
IN EFI_STATUS Status
)
{
if (DHCP_CONNECTED (DhcpSb->DhcpState)) {
DEBUG ((DEBUG_NET, "%a: AddressLost.\n", __FUNCTION__));
DhcpCallUser (DhcpSb, Dhcp4AddressLost, NULL, NULL);
} else {
DEBUG ((DEBUG_NET, "%a: Fail.\n", __FUNCTION__));
DhcpCallUser (DhcpSb, Dhcp4Fail, NULL, NULL);
}
DhcpCleanLease (DhcpSb);
DhcpSb->IoStatus = Status;
DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_ALL);
}
/**
Handle packets in DHCP select state.
@param[in] DhcpSb The DHCP service instance
@param[in] Packet The DHCP packet received
@param[in] Para The DHCP parameter extracted from the packet. That
is, all the option value that we care.
@retval EFI_SUCCESS The packet is successfully processed.
@retval Others Some error occurred.
**/
EFI_STATUS
DhcpHandleSelect (
IN DHCP_SERVICE *DhcpSb,
IN EFI_DHCP4_PACKET *Packet,
IN DHCP_PARAMETER *Para
)
{
EFI_STATUS Status;
Status = EFI_SUCCESS;
//
// First validate the message:
// 1. the offer is a unicast
// 2. if it is a DHCP message, it must contains a server ID.
// Don't return a error for these two case otherwise the session is ended.
//
if (!DHCP_IS_BOOTP (Para) &&
((Para->DhcpType != DHCP_MSG_OFFER) || (Para->ServerId == 0))
)
{
goto ON_EXIT;
}
//
// Call the user's callback. The action according to the return is as:
// 1. EFI_SUCCESS: stop waiting for more offers, select the offer now
// 2. EFI_NOT_READY: wait for more offers
// 3. EFI_ABORTED: abort the address acquiring.
//
Status = DhcpCallUser (DhcpSb, Dhcp4RcvdOffer, Packet, NULL);
if (Status == EFI_SUCCESS) {
if (DhcpSb->LastOffer != NULL) {
FreePool (DhcpSb->LastOffer);
}
DhcpSb->LastOffer = Packet;
return DhcpChooseOffer (DhcpSb);
} else if (Status == EFI_NOT_READY) {
if (DhcpSb->LastOffer != NULL) {
FreePool (DhcpSb->LastOffer);
}
DhcpSb->LastOffer = Packet;
} else if (Status == EFI_ABORTED) {
//
// DhcpInput will end the session upon error return. Remember
// only to call DhcpEndSession at the top level call.
//
goto ON_EXIT;
}
return EFI_SUCCESS;
ON_EXIT:
FreePool (Packet);
return Status;
}
/**
Handle packets in DHCP request state.
@param[in] DhcpSb The DHCP service instance
@param[in] Packet The DHCP packet received
@param[in] Para The DHCP parameter extracted from the packet. That
is, all the option value that we care.
@retval EFI_SUCCESS The packet is successfully processed.
@retval Others Some error occurred.
**/
EFI_STATUS
DhcpHandleRequest (
IN DHCP_SERVICE *DhcpSb,
IN EFI_DHCP4_PACKET *Packet,
IN DHCP_PARAMETER *Para
)
{
EFI_DHCP4_HEADER *Head;
EFI_DHCP4_HEADER *Selected;
EFI_STATUS Status;
UINT8 *Message;
ASSERT (!DHCP_IS_BOOTP (DhcpSb->Para));
Head = &Packet->Dhcp4.Header;
Selected = &DhcpSb->Selected->Dhcp4.Header;
//
// Ignore the BOOTP message and DHCP messages other than DHCP ACK/NACK.
//
if (DHCP_IS_BOOTP (Para) ||
(Para->ServerId != DhcpSb->Para->ServerId) ||
((Para->DhcpType != DHCP_MSG_ACK) && (Para->DhcpType != DHCP_MSG_NAK))
)
{
Status = EFI_SUCCESS;
goto ON_EXIT;
}
//
// Received a NAK, end the session no matter what the user returns
//
Status = EFI_DEVICE_ERROR;
if (Para->DhcpType == DHCP_MSG_NAK) {
DhcpCallUser (DhcpSb, Dhcp4RcvdNak, Packet, NULL);
goto ON_EXIT;
}
//
// Check whether the ACK matches the selected offer
//
Message = NULL;
if (!EFI_IP4_EQUAL (&Head->YourAddr, &Selected->YourAddr)) {
Message = (UINT8 *)"Lease confirmed isn't the same as that in the offer";
goto REJECT;
}
Status = DhcpCallUser (DhcpSb, Dhcp4RcvdAck, Packet, NULL);
if (EFI_ERROR (Status)) {
Message = (UINT8 *)"Lease is denied upon received ACK";
goto REJECT;
}
//
// Record the lease, transit to BOUND state, then notify the user
//
Status = DhcpLeaseAcquired (DhcpSb);
if (EFI_ERROR (Status)) {
Message = (UINT8 *)"Lease is denied upon entering bound";
goto REJECT;
}
DhcpSb->IoStatus = EFI_SUCCESS;
DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_COMPLETION);
FreePool (Packet);
return EFI_SUCCESS;
REJECT:
DhcpSendMessage (DhcpSb, DhcpSb->Selected, DhcpSb->Para, DHCP_MSG_DECLINE, Message);
ON_EXIT:
FreePool (Packet);
return Status;
}
/**
Handle packets in DHCP renew/rebound state.
@param[in] DhcpSb The DHCP service instance
@param[in] Packet The DHCP packet received
@param[in] Para The DHCP parameter extracted from the packet. That
is, all the option value that we care.
@retval EFI_SUCCESS The packet is successfully processed.
@retval Others Some error occurred.
**/
EFI_STATUS
DhcpHandleRenewRebind (
IN DHCP_SERVICE *DhcpSb,
IN EFI_DHCP4_PACKET *Packet,
IN DHCP_PARAMETER *Para
)
{
EFI_DHCP4_HEADER *Head;
EFI_DHCP4_HEADER *Selected;
EFI_STATUS Status;
ASSERT (!DHCP_IS_BOOTP (DhcpSb->Para));
Head = &Packet->Dhcp4.Header;
Selected = &DhcpSb->Selected->Dhcp4.Header;
//
// Ignore the BOOTP message and DHCP messages other than DHCP ACK/NACK
//
if (DHCP_IS_BOOTP (Para) ||
(Para->ServerId != DhcpSb->Para->ServerId) ||
((Para->DhcpType != DHCP_MSG_ACK) && (Para->DhcpType != DHCP_MSG_NAK))
)
{
Status = EFI_SUCCESS;
goto ON_EXIT;
}
//
// Received a NAK, ignore the user's return then terminate the process
//
Status = EFI_DEVICE_ERROR;
if (Para->DhcpType == DHCP_MSG_NAK) {
DhcpCallUser (DhcpSb, Dhcp4RcvdNak, Packet, NULL);
goto ON_EXIT;
}
//
// The lease is different from the selected. Don't send a DECLINE
// since it isn't existed in the client's FSM.
//
if (!EFI_IP4_EQUAL (&Head->YourAddr, &Selected->YourAddr)) {
goto ON_EXIT;
}
Status = DhcpCallUser (DhcpSb, Dhcp4RcvdAck, Packet, NULL);
if (EFI_ERROR (Status)) {
goto ON_EXIT;
}
//
// Record the lease, start timer for T1 and T2,
//
DhcpComputeLease (DhcpSb, Para);
DhcpSb->LeaseLife = 0;
DhcpSetState (DhcpSb, Dhcp4Bound, TRUE);
if (DhcpSb->ExtraRefresh != 0) {
DhcpSb->ExtraRefresh = FALSE;
DhcpSb->IoStatus = EFI_SUCCESS;
DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_RENEWREBIND);
}
ON_EXIT:
FreePool (Packet);
return Status;
}
/**
Handle packets in DHCP reboot state.
@param[in] DhcpSb The DHCP service instance
@param[in] Packet The DHCP packet received
@param[in] Para The DHCP parameter extracted from the packet. That
is, all the option value that we care.
@retval EFI_SUCCESS The packet is successfully processed.
@retval Others Some error occurred.
**/
EFI_STATUS
DhcpHandleReboot (
IN DHCP_SERVICE *DhcpSb,
IN EFI_DHCP4_PACKET *Packet,
IN DHCP_PARAMETER *Para
)
{
EFI_DHCP4_HEADER *Head;
EFI_STATUS Status;
Head = &Packet->Dhcp4.Header;
//
// Ignore the BOOTP message and DHCP messages other than DHCP ACK/NACK
//
if (DHCP_IS_BOOTP (Para) ||
((Para->DhcpType != DHCP_MSG_ACK) && (Para->DhcpType != DHCP_MSG_NAK))
)
{
Status = EFI_SUCCESS;
goto ON_EXIT;
}
//
// If a NAK is received, transit to INIT and try again.
//
if (Para->DhcpType == DHCP_MSG_NAK) {
DhcpCallUser (DhcpSb, Dhcp4RcvdNak, Packet, NULL);
DhcpSb->ClientAddr = 0;
DEBUG ((DEBUG_NET, "%a: NAK received, going to Init\n", __FUNCTION__));
DhcpSb->DhcpState = Dhcp4Init;
Status = DhcpInitRequest (DhcpSb);
goto ON_EXIT;
}
DEBUG ((DEBUG_NET, "%a: ACK Received.\n", __FUNCTION__));
//
// Check whether the ACK matches the selected offer
//
if (EFI_NTOHL (Head->YourAddr) != DhcpSb->ClientAddr) {
DEBUG ((DEBUG_NET, "%a: ACK didn't match offer.\n", __FUNCTION__));
Status = EFI_DEVICE_ERROR;
goto ON_EXIT;
}
Status = DhcpCallUser (DhcpSb, Dhcp4RcvdAck, Packet, NULL);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_NET, "%a: Error from CallUser - %r.\n", __FUNCTION__, Status));
goto ON_EXIT;
}
//
// OK, get the parameter from server, record the lease
//
DhcpSb->Para = AllocateCopyPool (sizeof (DHCP_PARAMETER), Para);
if (DhcpSb->Para == NULL) {
DEBUG ((DEBUG_NET, "%a: Out of resources.\n", __FUNCTION__));
Status = EFI_OUT_OF_RESOURCES;
goto ON_EXIT;
}
DhcpSb->Selected = Packet;
Status = DhcpLeaseAcquired (DhcpSb);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_NET, "%a: Error from LeaseAcquired - %r.\n", __FUNCTION__, Status));
return Status;
}
DhcpSb->IoStatus = EFI_SUCCESS;
DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_COMPLETION);
DEBUG ((DEBUG_NET, "%a: DHCP Complete.\n", __FUNCTION__));
return EFI_SUCCESS;
ON_EXIT:
FreePool (Packet);
return Status;
}
/**
Handle the received DHCP packets. This function drives the DHCP
state machine.
@param UdpPacket The UDP packets received.
@param EndPoint The local/remote UDP access point
@param IoStatus The status of the UDP receive
@param Context The opaque parameter to the function.
**/
VOID
EFIAPI
DhcpInput (
NET_BUF *UdpPacket,
UDP_END_POINT *EndPoint,
EFI_STATUS IoStatus,
VOID *Context
)
{
DHCP_SERVICE *DhcpSb;
EFI_DHCP4_HEADER *Head;
EFI_DHCP4_PACKET *Packet;
DHCP_PARAMETER *Para;
EFI_STATUS Status;
UINT32 Len;
Packet = NULL;
DhcpSb = (DHCP_SERVICE *)Context;
//
// Don't restart receive if error occurs or DHCP is destroyed.
//
if (EFI_ERROR (IoStatus)) {
return;
} else if (DhcpSb->ServiceState == DHCP_DESTROY) {
NetbufFree (UdpPacket);
return;
}
ASSERT (UdpPacket != NULL);
if (DhcpSb->DhcpState == Dhcp4Stopped) {
DEBUG ((DEBUG_NET, "%a: Restarting due to state == Stopped.\n", __FUNCTION__));
goto RESTART;
}
//
// Validate the packet received
//
if (UdpPacket->TotalSize < sizeof (EFI_DHCP4_HEADER)) {
DEBUG ((DEBUG_NET, "%a: Restarting due to invalid header size.\n", __FUNCTION__));
goto RESTART;
}
//
// Copy the DHCP message to a continuous memory block
//
Len = sizeof (EFI_DHCP4_PACKET) + UdpPacket->TotalSize - sizeof (EFI_DHCP4_HEADER);
Packet = (EFI_DHCP4_PACKET *)AllocatePool (Len);
if (Packet == NULL) {
DEBUG ((DEBUG_NET, "%a: Restarting due to out of resources.\n", __FUNCTION__));
goto RESTART;
}
Packet->Size = Len;
Head = &Packet->Dhcp4.Header;