]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Dhcp6Dxe/Dhcp6Impl.c
NetworkPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / NetworkPkg / Dhcp6Dxe / Dhcp6Impl.c
1 /** @file
2 This EFI_DHCP6_PROTOCOL interface implementation.
3
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "Dhcp6Impl.h"
11
12 //
13 // Well-known multi-cast address defined in section-24.1 of rfc-3315
14 //
15 // ALL_DHCP_Relay_Agents_and_Servers address: FF02::1:2
16 //
17 EFI_IPv6_ADDRESS mAllDhcpRelayAndServersAddress = {{0xFF, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2}};
18
19 EFI_DHCP6_PROTOCOL gDhcp6ProtocolTemplate = {
20 EfiDhcp6GetModeData,
21 EfiDhcp6Configure,
22 EfiDhcp6Start,
23 EfiDhcp6InfoRequest,
24 EfiDhcp6RenewRebind,
25 EfiDhcp6Decline,
26 EfiDhcp6Release,
27 EfiDhcp6Stop,
28 EfiDhcp6Parse
29 };
30
31 /**
32 Starts the DHCPv6 standard S.A.R.R. process.
33
34 The Start() function starts the DHCPv6 standard process. This function can
35 be called only when the state of Dhcp6 instance is in the Dhcp6Init state.
36 If the DHCP process completes successfully, the state of the Dhcp6 instance
37 will be transferred through Dhcp6Selecting and Dhcp6Requesting to the
38 Dhcp6Bound state.
39 Refer to rfc-3315 for precise state transitions during this process. At the
40 time when each event occurs in this process, the callback function that was set
41 by EFI_DHCP6_PROTOCOL.Configure() will be called, and the user can take this
42 opportunity to control the process.
43
44 @param[in] This The pointer to Dhcp6 protocol.
45
46 @retval EFI_SUCCESS The DHCPv6 standard process has started, or it has
47 completed when CompletionEvent is NULL.
48 @retval EFI_ACCESS_DENIED The EFI DHCPv6 Child instance hasn't been configured.
49 @retval EFI_INVALID_PARAMETER This is NULL.
50 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
51 @retval EFI_TIMEOUT The DHCPv6 configuration process failed because no
52 response was received from the server within the
53 specified timeout value.
54 @retval EFI_ABORTED The user aborted the DHCPv6 process.
55 @retval EFI_ALREADY_STARTED Some other Dhcp6 instance already started the DHCPv6
56 standard process.
57 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
58 @retval EFI_NO_MEDIA There was a media error.
59
60 **/
61 EFI_STATUS
62 EFIAPI
63 EfiDhcp6Start (
64 IN EFI_DHCP6_PROTOCOL *This
65 )
66 {
67 EFI_STATUS Status;
68 EFI_TPL OldTpl;
69 DHCP6_INSTANCE *Instance;
70 DHCP6_SERVICE *Service;
71 EFI_STATUS MediaStatus;
72
73 if (This == NULL) {
74 return EFI_INVALID_PARAMETER;
75 }
76
77 Instance = DHCP6_INSTANCE_FROM_THIS (This);
78 Service = Instance->Service;
79
80 //
81 // The instance hasn't been configured.
82 //
83 if (Instance->Config == NULL) {
84 return EFI_ACCESS_DENIED;
85 }
86
87 ASSERT (Instance->IaCb.Ia != NULL);
88
89 //
90 // The instance has already been started.
91 //
92 if (Instance->IaCb.Ia->State != Dhcp6Init) {
93 return EFI_ALREADY_STARTED;
94 }
95
96 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
97
98 //
99 // Check Media Satus.
100 //
101 MediaStatus = EFI_SUCCESS;
102 NetLibDetectMediaWaitTimeout (Service->Controller, DHCP_CHECK_MEDIA_WAITING_TIME, &MediaStatus);
103 if (MediaStatus != EFI_SUCCESS) {
104 Status = EFI_NO_MEDIA;
105 goto ON_ERROR;
106 }
107
108 Instance->UdpSts = EFI_ALREADY_STARTED;
109
110 //
111 // Send the solicit message to start S.A.R.R process.
112 //
113 Status = Dhcp6SendSolicitMsg (Instance);
114
115 if (EFI_ERROR (Status)) {
116 goto ON_ERROR;
117 }
118
119 //
120 // Register receive callback for the stateful exchange process.
121 //
122 Status = UdpIoRecvDatagram(
123 Service->UdpIo,
124 Dhcp6ReceivePacket,
125 Service,
126 0
127 );
128
129 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
130 goto ON_ERROR;
131 }
132
133 gBS->RestoreTPL (OldTpl);
134
135 //
136 // Poll udp out of the net tpl if synchronous call.
137 //
138 if (Instance->Config->IaInfoEvent == NULL) {
139
140 while (Instance->UdpSts == EFI_ALREADY_STARTED) {
141 Service->UdpIo->Protocol.Udp6->Poll (Service->UdpIo->Protocol.Udp6);
142 }
143 return Instance->UdpSts;
144 }
145
146 return EFI_SUCCESS;
147
148 ON_ERROR:
149
150 gBS->RestoreTPL (OldTpl);
151 return Status;
152 }
153
154
155 /**
156 Stops the DHCPv6 standard S.A.R.R. process.
157
158 The Stop() function is used to stop the DHCPv6 standard process. After this
159 function is called successfully, the state of Dhcp6 instance is transferred
160 into Dhcp6Init. EFI_DHCP6_PROTOCOL.Configure() needs to be called
161 before DHCPv6 standard process can be started again. This function can be
162 called when the Dhcp6 instance is in any state.
163
164 @param[in] This The pointer to the Dhcp6 protocol.
165
166 @retval EFI_SUCCESS The Dhcp6 instance is now in the Dhcp6Init state.
167 @retval EFI_INVALID_PARAMETER This is NULL.
168
169 **/
170 EFI_STATUS
171 EFIAPI
172 EfiDhcp6Stop (
173 IN EFI_DHCP6_PROTOCOL *This
174 )
175 {
176 EFI_TPL OldTpl;
177 EFI_STATUS Status;
178 EFI_UDP6_PROTOCOL *Udp6;
179 DHCP6_INSTANCE *Instance;
180 DHCP6_SERVICE *Service;
181
182 if (This == NULL) {
183 return EFI_INVALID_PARAMETER;
184 }
185
186 Instance = DHCP6_INSTANCE_FROM_THIS (This);
187 Service = Instance->Service;
188 Udp6 = Service->UdpIo->Protocol.Udp6;
189 Status = EFI_SUCCESS;
190
191 //
192 // The instance hasn't been configured.
193 //
194 if (Instance->Config == NULL) {
195 return Status;
196 }
197
198 ASSERT (Instance->IaCb.Ia != NULL);
199
200 //
201 // No valid REPLY message received yet, cleanup this instance directly.
202 //
203 if (Instance->IaCb.Ia->State == Dhcp6Init ||
204 Instance->IaCb.Ia->State == Dhcp6Selecting ||
205 Instance->IaCb.Ia->State == Dhcp6Requesting
206 ) {
207 goto ON_EXIT;
208 }
209
210 //
211 // Release the current ready Ia.
212 //
213 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
214
215 Instance->UdpSts = EFI_ALREADY_STARTED;
216 Status = Dhcp6SendReleaseMsg (Instance, Instance->IaCb.Ia);
217 gBS->RestoreTPL (OldTpl);
218 if (EFI_ERROR (Status)) {
219 goto ON_EXIT;
220 }
221
222 //
223 // Poll udp out of the net tpl if synchoronus call.
224 //
225 if (Instance->Config->IaInfoEvent == NULL) {
226 ASSERT (Udp6 != NULL);
227 while (Instance->UdpSts == EFI_ALREADY_STARTED) {
228 Udp6->Poll (Udp6);
229 }
230 Status = Instance->UdpSts;
231 }
232
233 ON_EXIT:
234 //
235 // Clean up the session data for the released Ia.
236 //
237 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
238 Dhcp6CleanupSession (Instance, EFI_SUCCESS);
239 gBS->RestoreTPL (OldTpl);
240
241 return Status;
242 }
243
244
245 /**
246 Returns the current operating mode data for the Dhcp6 instance.
247
248 The GetModeData() function returns the current operating mode and
249 cached data packet for the Dhcp6 instance.
250
251 @param[in] This The pointer to the Dhcp6 protocol.
252 @param[out] Dhcp6ModeData The pointer to the Dhcp6 mode data.
253 @param[out] Dhcp6ConfigData The pointer to the Dhcp6 configure data.
254
255 @retval EFI_SUCCESS The mode data was returned.
256 @retval EFI_INVALID_PARAMETER This is NULL.
257 @retval EFI_ACCESS_DENIED The EFI DHCPv6 Protocol instance was not
258 configured.
259 **/
260 EFI_STATUS
261 EFIAPI
262 EfiDhcp6GetModeData (
263 IN EFI_DHCP6_PROTOCOL *This,
264 OUT EFI_DHCP6_MODE_DATA *Dhcp6ModeData OPTIONAL,
265 OUT EFI_DHCP6_CONFIG_DATA *Dhcp6ConfigData OPTIONAL
266 )
267 {
268 EFI_TPL OldTpl;
269 EFI_DHCP6_IA *Ia;
270 DHCP6_INSTANCE *Instance;
271 DHCP6_SERVICE *Service;
272 UINT32 IaSize;
273 UINT32 IdSize;
274
275 if (This == NULL || (Dhcp6ModeData == NULL && Dhcp6ConfigData == NULL)) {
276 return EFI_INVALID_PARAMETER;
277 }
278
279 Instance = DHCP6_INSTANCE_FROM_THIS (This);
280 Service = Instance->Service;
281
282 if (Instance->Config == NULL && Dhcp6ConfigData != NULL) {
283 return EFI_ACCESS_DENIED;
284 }
285
286 ASSERT (Service->ClientId != NULL);
287
288 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
289
290 //
291 // User needs a copy of instance config data.
292 //
293 if (Dhcp6ConfigData != NULL) {
294 ZeroMem (Dhcp6ConfigData, sizeof(EFI_DHCP6_CONFIG_DATA));
295 //
296 // Duplicate config data, including all reference buffers.
297 //
298 if (EFI_ERROR (Dhcp6CopyConfigData (Dhcp6ConfigData, Instance->Config))) {
299 goto ON_ERROR;
300 }
301 }
302
303 //
304 // User need a copy of instance mode data.
305 //
306 if (Dhcp6ModeData != NULL) {
307 ZeroMem (Dhcp6ModeData, sizeof (EFI_DHCP6_MODE_DATA));
308 //
309 // Duplicate a copy of EFI_DHCP6_DUID for client Id.
310 //
311 IdSize = Service->ClientId->Length + sizeof (Service->ClientId->Length);
312
313 Dhcp6ModeData->ClientId = AllocateZeroPool (IdSize);
314 if (Dhcp6ModeData->ClientId == NULL) {
315 goto ON_ERROR;
316 }
317
318 CopyMem (
319 Dhcp6ModeData->ClientId,
320 Service->ClientId,
321 IdSize
322 );
323
324 Ia = Instance->IaCb.Ia;
325 if (Ia != NULL) {
326 //
327 // Duplicate a copy of EFI_DHCP6_IA for configured Ia.
328 //
329 IaSize = sizeof (EFI_DHCP6_IA) + (Ia->IaAddressCount -1) * sizeof (EFI_DHCP6_IA_ADDRESS);
330
331 Dhcp6ModeData->Ia = AllocateZeroPool (IaSize);
332 if (Dhcp6ModeData->Ia == NULL) {
333 goto ON_ERROR;
334 }
335
336 CopyMem (
337 Dhcp6ModeData->Ia,
338 Ia,
339 IaSize
340 );
341
342 //
343 // Duplicate a copy of reply packet if has.
344 //
345 if (Ia->ReplyPacket != NULL) {
346 Dhcp6ModeData->Ia->ReplyPacket = AllocateZeroPool (Ia->ReplyPacket->Size);
347 if (Dhcp6ModeData->Ia->ReplyPacket == NULL) {
348 goto ON_ERROR;
349 }
350 CopyMem (
351 Dhcp6ModeData->Ia->ReplyPacket,
352 Ia->ReplyPacket,
353 Ia->ReplyPacket->Size
354 );
355 }
356 }
357 }
358
359 gBS->RestoreTPL (OldTpl);
360
361 return EFI_SUCCESS;
362
363 ON_ERROR:
364
365 if (Dhcp6ConfigData != NULL) {
366 Dhcp6CleanupConfigData (Dhcp6ConfigData);
367 }
368 if (Dhcp6ModeData != NULL) {
369 Dhcp6CleanupModeData (Dhcp6ModeData);
370 }
371 gBS->RestoreTPL (OldTpl);
372
373 return EFI_OUT_OF_RESOURCES;
374 }
375
376
377 /**
378 Initializes, changes, or resets the operational settings for the Dhcp6 instance.
379
380 The Configure() function is used to initialize or clean up the configuration
381 data of the Dhcp6 instance:
382 - When Dhcp6CfgData is not NULL and Configure() is called successfully, the
383 configuration data will be initialized in the Dhcp6 instance, and the state
384 of the configured IA will be transferred into Dhcp6Init.
385 - When Dhcp6CfgData is NULL and Configure() is called successfully, the
386 configuration data will be cleaned up and no IA will be associated with
387 the Dhcp6 instance.
388 To update the configuration data for an Dhcp6 instance, the original data
389 must be cleaned up before setting the new configuration data.
390
391 @param[in] This The pointer to the Dhcp6 protocol
392 @param[in] Dhcp6CfgData The pointer to the EFI_DHCP6_CONFIG_DATA.
393
394 @retval EFI_SUCCESS The Dhcp6 is configured successfully with the
395 Dhcp6Init state, or cleaned up the original
396 configuration setting.
397 @retval EFI_ACCESS_DENIED The Dhcp6 instance was already configured.
398 The Dhcp6 instance has already started the
399 DHCPv6 S.A.R.R when Dhcp6CfgData is NULL.
400 @retval EFI_INVALID_PARAMETER Some of the parameter is invalid.
401 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
402 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
403
404 **/
405 EFI_STATUS
406 EFIAPI
407 EfiDhcp6Configure (
408 IN EFI_DHCP6_PROTOCOL *This,
409 IN EFI_DHCP6_CONFIG_DATA *Dhcp6CfgData OPTIONAL
410 )
411 {
412 EFI_TPL OldTpl;
413 EFI_STATUS Status;
414 LIST_ENTRY *Entry;
415 DHCP6_INSTANCE *Other;
416 DHCP6_INSTANCE *Instance;
417 DHCP6_SERVICE *Service;
418 UINTN Index;
419
420 if (This == NULL) {
421 return EFI_INVALID_PARAMETER;
422 }
423
424 Instance = DHCP6_INSTANCE_FROM_THIS (This);
425 Service = Instance->Service;
426
427 //
428 // Check the parameter of configure data.
429 //
430 if (Dhcp6CfgData != NULL) {
431 if (Dhcp6CfgData->OptionCount > 0 && Dhcp6CfgData->OptionList == NULL) {
432 return EFI_INVALID_PARAMETER;
433 }
434 if (Dhcp6CfgData->OptionList != NULL) {
435 for (Index = 0; Index < Dhcp6CfgData->OptionCount; Index++) {
436 if (Dhcp6CfgData->OptionList[Index]->OpCode == Dhcp6OptClientId ||
437 Dhcp6CfgData->OptionList[Index]->OpCode == Dhcp6OptRapidCommit ||
438 Dhcp6CfgData->OptionList[Index]->OpCode == Dhcp6OptReconfigureAccept ||
439 Dhcp6CfgData->OptionList[Index]->OpCode == Dhcp6OptIana ||
440 Dhcp6CfgData->OptionList[Index]->OpCode == Dhcp6OptIata
441 ) {
442 return EFI_INVALID_PARAMETER;
443 }
444 }
445 }
446
447 if (Dhcp6CfgData->IaDescriptor.Type != EFI_DHCP6_IA_TYPE_NA &&
448 Dhcp6CfgData->IaDescriptor.Type != EFI_DHCP6_IA_TYPE_TA
449 ) {
450 return EFI_INVALID_PARAMETER;
451 }
452
453 if (Dhcp6CfgData->IaInfoEvent == NULL && Dhcp6CfgData->SolicitRetransmission == NULL) {
454 return EFI_INVALID_PARAMETER;
455 }
456
457 if (Dhcp6CfgData->SolicitRetransmission != NULL &&
458 Dhcp6CfgData->SolicitRetransmission->Mrc == 0 &&
459 Dhcp6CfgData->SolicitRetransmission->Mrd == 0
460 ) {
461 return EFI_INVALID_PARAMETER;
462 }
463
464 //
465 // Make sure the (IaId, IaType) is unique over all the instances.
466 //
467 NET_LIST_FOR_EACH (Entry, &Service->Child) {
468 Other = NET_LIST_USER_STRUCT (Entry, DHCP6_INSTANCE, Link);
469 if (Other->IaCb.Ia != NULL &&
470 Other->IaCb.Ia->Descriptor.Type == Dhcp6CfgData->IaDescriptor.Type &&
471 Other->IaCb.Ia->Descriptor.IaId == Dhcp6CfgData->IaDescriptor.IaId
472 ) {
473 return EFI_INVALID_PARAMETER;
474 }
475 }
476 }
477
478 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
479
480 if (Dhcp6CfgData != NULL) {
481 //
482 // It's not allowed to configure one instance twice without configure null.
483 //
484 if (Instance->Config != NULL) {
485 gBS->RestoreTPL (OldTpl);
486 return EFI_ACCESS_DENIED;
487 }
488
489 //
490 // Duplicate config data including all reference buffers.
491 //
492 Instance->Config = AllocateZeroPool (sizeof (EFI_DHCP6_CONFIG_DATA));
493 if (Instance->Config == NULL) {
494 gBS->RestoreTPL (OldTpl);
495 return EFI_OUT_OF_RESOURCES;
496 }
497
498 Status = Dhcp6CopyConfigData (Instance->Config, Dhcp6CfgData);
499 if (EFI_ERROR(Status)) {
500 FreePool (Instance->Config);
501 gBS->RestoreTPL (OldTpl);
502 return EFI_OUT_OF_RESOURCES;
503 }
504
505 //
506 // Initialize the Ia descriptor from the config data, and leave the other
507 // fields of the Ia as default value 0.
508 //
509 Instance->IaCb.Ia = AllocateZeroPool (sizeof(EFI_DHCP6_IA));
510 if (Instance->IaCb.Ia == NULL) {
511 Dhcp6CleanupConfigData (Instance->Config);
512 FreePool (Instance->Config);
513 gBS->RestoreTPL (OldTpl);
514 return EFI_OUT_OF_RESOURCES;
515 }
516 CopyMem (
517 &Instance->IaCb.Ia->Descriptor,
518 &Dhcp6CfgData->IaDescriptor,
519 sizeof(EFI_DHCP6_IA_DESCRIPTOR)
520 );
521
522 } else {
523
524 if (Instance->Config == NULL) {
525 ASSERT (Instance->IaCb.Ia == NULL);
526 gBS->RestoreTPL (OldTpl);
527 return EFI_SUCCESS;
528 }
529
530 //
531 // It's not allowed to configure a started instance as null.
532 //
533 if (Instance->IaCb.Ia->State != Dhcp6Init) {
534 gBS->RestoreTPL (OldTpl);
535 return EFI_ACCESS_DENIED;
536 }
537
538 Dhcp6CleanupConfigData (Instance->Config);
539 FreePool (Instance->Config);
540 Instance->Config = NULL;
541
542 FreePool (Instance->IaCb.Ia);
543 Instance->IaCb.Ia = NULL;
544 }
545
546 gBS->RestoreTPL (OldTpl);
547
548 return EFI_SUCCESS;
549 }
550
551
552 /**
553 Request configuration information without the assignment of any
554 Ia addresses of the client.
555
556 The InfoRequest() function is used to request configuration information
557 without the assignment of any IPv6 address of the client. The client sends
558 out an Information Request packet to obtain the required configuration
559 information, and DHCPv6 server responds with a Reply packet containing
560 the information for the client. The received Reply packet will be passed
561 to the user by ReplyCallback function. If the user returns EFI_NOT_READY from
562 ReplyCallback, the Dhcp6 instance will continue to receive other Reply
563 packets unless timeout according to the Retransmission parameter.
564 Otherwise, the Information Request exchange process will be finished
565 successfully if user returns EFI_SUCCESS from ReplyCallback.
566
567 @param[in] This The pointer to the Dhcp6 protocol.
568 @param[in] SendClientId If TRUE, the DHCPv6 protocol instance will build Client
569 Identifier option and include it into Information Request
570 packet. Otherwise, Client Identifier option will not be included.
571 @param[in] OptionRequest The pointer to the buffer of option request options.
572 @param[in] OptionCount The option number in the OptionList.
573 @param[in] OptionList The list of appended options.
574 @param[in] Retransmission The pointer to the retransmission of the message.
575 @param[in] TimeoutEvent The event of timeout.
576 @param[in] ReplyCallback The callback function when the reply was received.
577 @param[in] CallbackContext The pointer to the parameter passed to the callback.
578
579 @retval EFI_SUCCESS The DHCPv6 information request exchange process
580 completed when TimeoutEvent is NULL. Information
581 Request packet has been sent to DHCPv6 server when
582 TimeoutEvent is not NULL.
583 @retval EFI_NO_RESPONSE The DHCPv6 information request exchange process failed
584 because of no response, or not all requested-options
585 are responded by DHCPv6 servers when Timeout happened.
586 @retval EFI_ABORTED The DHCPv6 information request exchange process was aborted
587 by user.
588 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
589 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
590 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
591
592 **/
593 EFI_STATUS
594 EFIAPI
595 EfiDhcp6InfoRequest (
596 IN EFI_DHCP6_PROTOCOL *This,
597 IN BOOLEAN SendClientId,
598 IN EFI_DHCP6_PACKET_OPTION *OptionRequest,
599 IN UINT32 OptionCount,
600 IN EFI_DHCP6_PACKET_OPTION *OptionList[] OPTIONAL,
601 IN EFI_DHCP6_RETRANSMISSION *Retransmission,
602 IN EFI_EVENT TimeoutEvent OPTIONAL,
603 IN EFI_DHCP6_INFO_CALLBACK ReplyCallback,
604 IN VOID *CallbackContext OPTIONAL
605 )
606 {
607 EFI_STATUS Status;
608 DHCP6_INSTANCE *Instance;
609 DHCP6_SERVICE *Service;
610 UINTN Index;
611 EFI_EVENT Timer;
612 EFI_STATUS TimerStatus;
613 UINTN GetMappingTimeOut;
614
615 if (This == NULL || OptionRequest == NULL || Retransmission == NULL || ReplyCallback == NULL) {
616 return EFI_INVALID_PARAMETER;
617 }
618
619 if (Retransmission != NULL && Retransmission->Mrc == 0 && Retransmission->Mrd == 0) {
620 return EFI_INVALID_PARAMETER;
621 }
622
623 if (OptionCount > 0 && OptionList == NULL) {
624 return EFI_INVALID_PARAMETER;
625 }
626
627 if (OptionList != NULL) {
628 for (Index = 0; Index < OptionCount; Index++) {
629 if (OptionList[Index]->OpCode == Dhcp6OptClientId || OptionList[Index]->OpCode == Dhcp6OptRequestOption) {
630 return EFI_INVALID_PARAMETER;
631 }
632 }
633 }
634
635 Instance = DHCP6_INSTANCE_FROM_THIS (This);
636 Service = Instance->Service;
637
638 Status = Dhcp6StartInfoRequest (
639 Instance,
640 SendClientId,
641 OptionRequest,
642 OptionCount,
643 OptionList,
644 Retransmission,
645 TimeoutEvent,
646 ReplyCallback,
647 CallbackContext
648 );
649 if (Status == EFI_NO_MAPPING) {
650 //
651 // The link local address is not ready, wait for some time and restart
652 // the DHCP6 information request process.
653 //
654 Status = Dhcp6GetMappingTimeOut(Service->Ip6Cfg, &GetMappingTimeOut);
655 if (EFI_ERROR(Status)) {
656 return Status;
657 }
658
659 Status = gBS->CreateEvent (EVT_TIMER, TPL_CALLBACK, NULL, NULL, &Timer);
660 if (EFI_ERROR (Status)) {
661 return Status;
662 }
663
664 //
665 // Start the timer, wait for link local address DAD to finish.
666 //
667 Status = gBS->SetTimer (Timer, TimerRelative, GetMappingTimeOut);
668 if (EFI_ERROR (Status)) {
669 gBS->CloseEvent (Timer);
670 return Status;
671 }
672
673 do {
674 TimerStatus = gBS->CheckEvent (Timer);
675 if (!EFI_ERROR (TimerStatus)) {
676 Status = Dhcp6StartInfoRequest (
677 Instance,
678 SendClientId,
679 OptionRequest,
680 OptionCount,
681 OptionList,
682 Retransmission,
683 TimeoutEvent,
684 ReplyCallback,
685 CallbackContext
686 );
687 }
688 } while (TimerStatus == EFI_NOT_READY);
689
690 gBS->CloseEvent (Timer);
691 }
692 if (EFI_ERROR (Status)) {
693 return Status;
694 }
695
696 //
697 // Poll udp out of the net tpl if synchoronus call.
698 //
699 if (TimeoutEvent == NULL) {
700
701 while (Instance->UdpSts == EFI_ALREADY_STARTED) {
702 Service->UdpIo->Protocol.Udp6->Poll (Service->UdpIo->Protocol.Udp6);
703 }
704 return Instance->UdpSts;
705 }
706
707 return EFI_SUCCESS;
708 }
709
710
711 /**
712 Manually extend the valid and preferred lifetimes for the IPv6 addresses
713 of the configured IA and update other configuration parameters by sending a
714 Renew or Rebind packet.
715
716 The RenewRebind() function is used to manually extend the valid and preferred
717 lifetimes for the IPv6 addresses of the configured IA, and update other
718 configuration parameters by sending Renew or Rebind packet.
719 - When RebindRequest is FALSE and the state of the configured IA is Dhcp6Bound,
720 it sends Renew packet to the previously DHCPv6 server and transfer the
721 state of the configured IA to Dhcp6Renewing. If valid Reply packet received,
722 the state transfers to Dhcp6Bound and the valid and preferred timer restarts.
723 If fails, the state transfers to Dhcp6Bound, but the timer continues.
724 - When RebindRequest is TRUE and the state of the configured IA is Dhcp6Bound,
725 it will send a Rebind packet. If valid Reply packet is received, the state transfers
726 to Dhcp6Bound and the valid and preferred timer restarts. If it fails, the state
727 transfers to Dhcp6Init, and the IA can't be used.
728
729 @param[in] This The pointer to the Dhcp6 protocol.
730 @param[in] RebindRequest If TRUE, Rebind packet will be sent and enter Dhcp6Rebinding state.
731 Otherwise, Renew packet will be sent and enter Dhcp6Renewing state.
732
733 @retval EFI_SUCCESS The DHCPv6 renew/rebind exchange process has
734 completed and at least one IPv6 address of the
735 configured IA has been bound again when
736 EFI_DHCP6_CONFIG_DATA.IaInfoEvent is NULL.
737 The EFI DHCPv6 Protocol instance has sent Renew
738 or Rebind packet when
739 EFI_DHCP6_CONFIG_DATA.IaInfoEvent is not NULL.
740 @retval EFI_ACCESS_DENIED The Dhcp6 instance hasn't been configured, or the
741 state of the configured IA is not in Dhcp6Bound.
742 @retval EFI_ALREADY_STARTED The state of the configured IA has already entered
743 Dhcp6Renewing when RebindRequest is FALSE.
744 The state of the configured IA has already entered
745 Dhcp6Rebinding when RebindRequest is TRUE.
746 @retval EFI_ABORTED The DHCPv6 renew/rebind exchange process aborted
747 by the user.
748 @retval EFI_NO_RESPONSE The DHCPv6 renew/rebind exchange process failed
749 because of no response.
750 @retval EFI_NO_MAPPING No IPv6 address has been bound to the configured
751 IA after the DHCPv6 renew/rebind exchange process.
752 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
753 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
754
755 **/
756 EFI_STATUS
757 EFIAPI
758 EfiDhcp6RenewRebind (
759 IN EFI_DHCP6_PROTOCOL *This,
760 IN BOOLEAN RebindRequest
761 )
762 {
763 EFI_STATUS Status;
764 EFI_TPL OldTpl;
765 DHCP6_INSTANCE *Instance;
766 DHCP6_SERVICE *Service;
767
768 if (This == NULL) {
769 return EFI_INVALID_PARAMETER;
770 }
771
772 Instance = DHCP6_INSTANCE_FROM_THIS (This);
773 Service = Instance->Service;
774
775 //
776 // The instance hasn't been configured.
777 //
778 if (Instance->Config == NULL) {
779 return EFI_ACCESS_DENIED;
780 }
781
782 ASSERT (Instance->IaCb.Ia != NULL);
783
784 //
785 // The instance has already entered renewing or rebinding state.
786 //
787 if ((Instance->IaCb.Ia->State == Dhcp6Rebinding && RebindRequest) ||
788 (Instance->IaCb.Ia->State == Dhcp6Renewing && !RebindRequest)
789 ) {
790 return EFI_ALREADY_STARTED;
791 }
792
793 if (Instance->IaCb.Ia->State != Dhcp6Bound) {
794 return EFI_ACCESS_DENIED;
795 }
796
797 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
798 Instance->UdpSts = EFI_ALREADY_STARTED;
799
800 //
801 // Send renew/rebind message to start exchange process.
802 //
803 Status = Dhcp6SendRenewRebindMsg (Instance, RebindRequest);
804
805 if (EFI_ERROR (Status)) {
806 goto ON_ERROR;
807 }
808
809 //
810 // Register receive callback for the stateful exchange process.
811 //
812 Status = UdpIoRecvDatagram(
813 Service->UdpIo,
814 Dhcp6ReceivePacket,
815 Service,
816 0
817 );
818
819 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
820 goto ON_ERROR;
821 }
822
823 gBS->RestoreTPL (OldTpl);
824
825 //
826 // Poll udp out of the net tpl if synchoronus call.
827 //
828 if (Instance->Config->IaInfoEvent == NULL) {
829
830 while (Instance->UdpSts == EFI_ALREADY_STARTED) {
831 Service->UdpIo->Protocol.Udp6->Poll (Service->UdpIo->Protocol.Udp6);
832 }
833 return Instance->UdpSts;
834 }
835
836 return EFI_SUCCESS;
837
838 ON_ERROR:
839
840 gBS->RestoreTPL (OldTpl);
841 return Status;
842 }
843
844
845 /**
846 Inform that one or more addresses assigned by a server are already
847 in use by another node.
848
849 The Decline() function is used to manually decline the assignment of
850 IPv6 addresses, which have been already used by another node. If all
851 IPv6 addresses of the configured IA are declined through this function,
852 the state of the IA will switch through Dhcp6Declining to Dhcp6Init.
853 Otherwise, the state of the IA will restore to Dhcp6Bound after the
854 declining process. The Decline() can only be called when the IA is in
855 Dhcp6Bound state. If the EFI_DHCP6_CONFIG_DATA.IaInfoEvent is NULL,
856 this function is a blocking operation. It will return after the
857 declining process finishes, or aborted by user.
858
859 @param[in] This The pointer to EFI_DHCP6_PROTOCOL.
860 @param[in] AddressCount The number of declining addresses.
861 @param[in] Addresses The pointer to the buffer stored the declining
862 addresses.
863
864 @retval EFI_SUCCESS The DHCPv6 decline exchange process completed
865 when EFI_DHCP6_CONFIG_DATA.IaInfoEvent was NULL.
866 The Dhcp6 instance sent Decline packet when
867 EFI_DHCP6_CONFIG_DATA.IaInfoEvent was not NULL.
868 @retval EFI_ACCESS_DENIED The Dhcp6 instance hasn't been configured, or the
869 state of the configured IA is not in Dhcp6Bound.
870 @retval EFI_ABORTED The DHCPv6 decline exchange process aborted by user.
871 @retval EFI_NOT_FOUND Any specified IPv6 address is not correlated with
872 the configured IA for this instance.
873 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
874 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
875
876 **/
877 EFI_STATUS
878 EFIAPI
879 EfiDhcp6Decline (
880 IN EFI_DHCP6_PROTOCOL *This,
881 IN UINT32 AddressCount,
882 IN EFI_IPv6_ADDRESS *Addresses
883 )
884 {
885 EFI_STATUS Status;
886 EFI_TPL OldTpl;
887 EFI_DHCP6_IA *DecIa;
888 DHCP6_INSTANCE *Instance;
889 DHCP6_SERVICE *Service;
890
891 if (This == NULL || AddressCount == 0 || Addresses == NULL) {
892 return EFI_INVALID_PARAMETER;
893 }
894
895 Instance = DHCP6_INSTANCE_FROM_THIS (This);
896 Service = Instance->Service;
897
898 //
899 // The instance hasn't been configured.
900 //
901 if (Instance->Config == NULL) {
902 return EFI_ACCESS_DENIED;
903 }
904
905 ASSERT (Instance->IaCb.Ia != NULL);
906
907 if (Instance->IaCb.Ia->State != Dhcp6Bound) {
908 return EFI_ACCESS_DENIED;
909 }
910
911 //
912 // Check whether all the declined addresses belongs to the configured Ia.
913 //
914 Status = Dhcp6CheckAddress (Instance->IaCb.Ia, AddressCount, Addresses);
915
916 if (EFI_ERROR(Status)) {
917 return Status;
918 }
919
920 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
921 Instance->UdpSts = EFI_ALREADY_STARTED;
922
923 //
924 // Deprive of all the declined addresses from the configured Ia, and create a
925 // DeclineIa used to create decline message.
926 //
927 DecIa = Dhcp6DepriveAddress (Instance->IaCb.Ia, AddressCount, Addresses);
928
929 if (DecIa == NULL) {
930 Status = EFI_OUT_OF_RESOURCES;
931 goto ON_ERROR;
932 }
933
934 //
935 // Send the decline message to start exchange process.
936 //
937 Status = Dhcp6SendDeclineMsg (Instance, DecIa);
938
939 if (EFI_ERROR (Status)) {
940 goto ON_ERROR;
941 }
942
943 //
944 // Register receive callback for the stateful exchange process.
945 //
946 Status = UdpIoRecvDatagram(
947 Service->UdpIo,
948 Dhcp6ReceivePacket,
949 Service,
950 0
951 );
952
953 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
954 goto ON_ERROR;
955 }
956
957 FreePool (DecIa);
958 gBS->RestoreTPL (OldTpl);
959
960 //
961 // Poll udp out of the net tpl if synchoronus call.
962 //
963 if (Instance->Config->IaInfoEvent == NULL) {
964
965 while (Instance->UdpSts == EFI_ALREADY_STARTED) {
966 Service->UdpIo->Protocol.Udp6->Poll (Service->UdpIo->Protocol.Udp6);
967 }
968 return Instance->UdpSts;
969 }
970
971 return EFI_SUCCESS;
972
973 ON_ERROR:
974
975 if (DecIa != NULL) {
976 FreePool (DecIa);
977 }
978 gBS->RestoreTPL (OldTpl);
979
980 return Status;
981 }
982
983
984 /**
985 Release one or more addresses associated with the configured Ia
986 for current instance.
987
988 The Release() function is used to manually release one or more
989 IPv6 addresses. If AddressCount is zero, it will release all IPv6
990 addresses of the configured IA. If all IPv6 addresses of the IA are
991 released through this function, the state of the IA will switch
992 through Dhcp6Releasing to Dhcp6Init, otherwise, the state of the
993 IA will restore to Dhcp6Bound after the releasing process.
994 The Release() can only be called when the IA is in Dhcp6Bound state.
995 If the EFI_DHCP6_CONFIG_DATA.IaInfoEvent is NULL, the function is
996 a blocking operation. It will return after the releasing process
997 finishes, or is aborted by user.
998
999 @param[in] This The pointer to the Dhcp6 protocol.
1000 @param[in] AddressCount The number of releasing addresses.
1001 @param[in] Addresses The pointer to the buffer stored the releasing
1002 addresses.
1003
1004 @retval EFI_SUCCESS The DHCPv6 release exchange process
1005 completed when EFI_DHCP6_CONFIG_DATA.IaInfoEvent
1006 was NULL. The Dhcp6 instance was sent Release
1007 packet when EFI_DHCP6_CONFIG_DATA.IaInfoEvent
1008 was not NULL.
1009 @retval EFI_ACCESS_DENIED The Dhcp6 instance hasn't been configured, or the
1010 state of the configured IA is not in Dhcp6Bound.
1011 @retval EFI_ABORTED The DHCPv6 release exchange process aborted by user.
1012 @retval EFI_NOT_FOUND Any specified IPv6 address is not correlated with
1013 the configured IA for this instance.
1014 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
1015 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1016
1017 **/
1018 EFI_STATUS
1019 EFIAPI
1020 EfiDhcp6Release (
1021 IN EFI_DHCP6_PROTOCOL *This,
1022 IN UINT32 AddressCount,
1023 IN EFI_IPv6_ADDRESS *Addresses
1024 )
1025 {
1026 EFI_STATUS Status;
1027 EFI_TPL OldTpl;
1028 EFI_DHCP6_IA *RelIa;
1029 DHCP6_INSTANCE *Instance;
1030 DHCP6_SERVICE *Service;
1031
1032 if (This == NULL || (AddressCount != 0 && Addresses == NULL)) {
1033 return EFI_INVALID_PARAMETER;
1034 }
1035
1036 Instance = DHCP6_INSTANCE_FROM_THIS (This);
1037 Service = Instance->Service;
1038
1039 //
1040 // The instance hasn't been configured.
1041 //
1042 if (Instance->Config == NULL) {
1043 return EFI_ACCESS_DENIED;
1044 }
1045
1046 ASSERT (Instance->IaCb.Ia != NULL);
1047
1048 if (Instance->IaCb.Ia->State != Dhcp6Bound) {
1049 return EFI_ACCESS_DENIED;
1050 }
1051
1052 //
1053 // Check whether all the released addresses belongs to the configured Ia.
1054 //
1055 Status = Dhcp6CheckAddress (Instance->IaCb.Ia, AddressCount, Addresses);
1056
1057 if (EFI_ERROR(Status)) {
1058 return Status;
1059 }
1060
1061 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1062 Instance->UdpSts = EFI_ALREADY_STARTED;
1063
1064 //
1065 // Deprive of all the released addresses from the configured Ia, and create a
1066 // ReleaseIa used to create release message.
1067 //
1068 RelIa = Dhcp6DepriveAddress (Instance->IaCb.Ia, AddressCount, Addresses);
1069
1070 if (RelIa == NULL) {
1071 Status = EFI_OUT_OF_RESOURCES;
1072 goto ON_ERROR;
1073 }
1074
1075 //
1076 // Send the release message to start exchange process.
1077 //
1078 Status = Dhcp6SendReleaseMsg (Instance, RelIa);
1079
1080 if (EFI_ERROR (Status)) {
1081 goto ON_ERROR;
1082 }
1083
1084 //
1085 // Register receive callback for the stateful exchange process.
1086 //
1087 Status = UdpIoRecvDatagram(
1088 Service->UdpIo,
1089 Dhcp6ReceivePacket,
1090 Service,
1091 0
1092 );
1093
1094 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
1095 goto ON_ERROR;
1096 }
1097
1098 FreePool (RelIa);
1099 gBS->RestoreTPL (OldTpl);
1100
1101 //
1102 // Poll udp out of the net tpl if synchoronus call.
1103 //
1104 if (Instance->Config->IaInfoEvent == NULL) {
1105 while (Instance->UdpSts == EFI_ALREADY_STARTED) {
1106 Service->UdpIo->Protocol.Udp6->Poll (Service->UdpIo->Protocol.Udp6);
1107 }
1108 return Instance->UdpSts;
1109 }
1110
1111 return EFI_SUCCESS;
1112
1113 ON_ERROR:
1114
1115 if (RelIa != NULL) {
1116 FreePool (RelIa);
1117 }
1118 gBS->RestoreTPL (OldTpl);
1119
1120 return Status;
1121 }
1122
1123
1124 /**
1125 Parse the option data in the Dhcp6 packet.
1126
1127 The Parse() function is used to retrieve the option list in the DHCPv6 packet.
1128
1129 @param[in] This The pointer to the Dhcp6 protocol.
1130 @param[in] Packet The pointer to the Dhcp6 packet.
1131 @param[in, out] OptionCount The number of option in the packet.
1132 @param[out] PacketOptionList The array of pointers to each option in the packet.
1133
1134 @retval EFI_SUCCESS The packet was successfully parsed.
1135 @retval EFI_INVALID_PARAMETER Some parameter is NULL.
1136 @retval EFI_BUFFER_TOO_SMALL *OptionCount is smaller than the number of options
1137 that were found in the Packet.
1138
1139 **/
1140 EFI_STATUS
1141 EFIAPI
1142 EfiDhcp6Parse (
1143 IN EFI_DHCP6_PROTOCOL *This,
1144 IN EFI_DHCP6_PACKET *Packet,
1145 IN OUT UINT32 *OptionCount,
1146 OUT EFI_DHCP6_PACKET_OPTION *PacketOptionList[] OPTIONAL
1147 )
1148 {
1149 UINT32 OptCnt;
1150 UINT32 OptLen;
1151 UINT16 DataLen;
1152 UINT8 *Start;
1153 UINT8 *End;
1154
1155 if (This == NULL || Packet == NULL || OptionCount == NULL) {
1156 return EFI_INVALID_PARAMETER;
1157 }
1158
1159 if (*OptionCount != 0 && PacketOptionList == NULL) {
1160 return EFI_INVALID_PARAMETER;
1161 }
1162
1163 if (Packet->Length > Packet->Size || Packet->Length < sizeof (EFI_DHCP6_HEADER)) {
1164 return EFI_INVALID_PARAMETER;
1165 }
1166
1167 //
1168 // The format of Dhcp6 option:
1169 //
1170 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1171 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1172 // | option-code | option-len (option data) |
1173 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1174 // | option-data |
1175 // | (option-len octets) |
1176 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1177 //
1178
1179 OptCnt = 0;
1180 OptLen = Packet->Length - sizeof (EFI_DHCP6_HEADER);
1181 Start = Packet->Dhcp6.Option;
1182 End = Start + OptLen;
1183
1184 //
1185 // Calculate the number of option in the packet.
1186 //
1187 while (Start < End) {
1188 DataLen = ((EFI_DHCP6_PACKET_OPTION *) Start)->OpLen;
1189 Start += (NTOHS (DataLen) + 4);
1190 OptCnt++;
1191 }
1192
1193 //
1194 // It will return buffer too small if pass-in option count is smaller than the
1195 // actual count of options in the packet.
1196 //
1197 if (OptCnt > *OptionCount) {
1198 *OptionCount = OptCnt;
1199 return EFI_BUFFER_TOO_SMALL;
1200 }
1201
1202 ZeroMem (
1203 PacketOptionList,
1204 (*OptionCount * sizeof (EFI_DHCP6_PACKET_OPTION *))
1205 );
1206
1207 OptCnt = 0;
1208 Start = Packet->Dhcp6.Option;
1209
1210 while (Start < End) {
1211
1212 PacketOptionList[OptCnt] = (EFI_DHCP6_PACKET_OPTION *) Start;
1213 DataLen = ((EFI_DHCP6_PACKET_OPTION *) Start)->OpLen;
1214 Start += (NTOHS (DataLen) + 4);
1215 OptCnt++;
1216 }
1217
1218 return EFI_SUCCESS;
1219 }
1220