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