]>
Commit | Line | Data |
---|---|---|
a3bcde70 HT |
1 | /** @file\r |
2 | Driver Binding functions and Service Binding functions\r | |
f97117ba | 3 | implementation for Dhcp6 Driver.\r |
a3bcde70 | 4 | \r |
f75a7f56 | 5 | Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r |
a3bcde70 | 6 | \r |
ecf98fbc | 7 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
a3bcde70 HT |
8 | \r |
9 | **/\r | |
10 | \r | |
11 | #include "Dhcp6Impl.h"\r | |
12 | \r | |
13 | \r | |
14 | EFI_DRIVER_BINDING_PROTOCOL gDhcp6DriverBinding = {\r | |
15 | Dhcp6DriverBindingSupported,\r | |
16 | Dhcp6DriverBindingStart,\r | |
17 | Dhcp6DriverBindingStop,\r | |
18 | 0xa,\r | |
19 | NULL,\r | |
20 | NULL\r | |
21 | };\r | |
22 | \r | |
23 | EFI_SERVICE_BINDING_PROTOCOL gDhcp6ServiceBindingTemplate = {\r | |
24 | Dhcp6ServiceBindingCreateChild,\r | |
25 | Dhcp6ServiceBindingDestroyChild\r | |
26 | };\r | |
27 | \r | |
a3bcde70 HT |
28 | /**\r |
29 | Configure the default Udp6Io to receive all the DHCP6 traffic\r | |
30 | on this network interface.\r | |
31 | \r | |
32 | @param[in] UdpIo The pointer to Udp6Io to be configured.\r | |
33 | @param[in] Context The pointer to the context.\r | |
34 | \r | |
35 | @retval EFI_SUCCESS The Udp6Io is successfully configured.\r | |
36 | @retval Others Failed to configure the Udp6Io.\r | |
37 | \r | |
38 | **/\r | |
39 | EFI_STATUS\r | |
40 | EFIAPI\r | |
41 | Dhcp6ConfigureUdpIo (\r | |
42 | IN UDP_IO *UdpIo,\r | |
43 | IN VOID *Context\r | |
44 | )\r | |
45 | {\r | |
46 | EFI_UDP6_PROTOCOL *Udp6;\r | |
47 | EFI_UDP6_CONFIG_DATA *Config;\r | |
48 | \r | |
49 | Udp6 = UdpIo->Protocol.Udp6;\r | |
50 | Config = &(UdpIo->Config.Udp6);\r | |
51 | \r | |
52 | ZeroMem (Config, sizeof (EFI_UDP6_CONFIG_DATA));\r | |
53 | \r | |
54 | //\r | |
55 | // Set Udp6 configure data for the Dhcp6 instance.\r | |
56 | //\r | |
57 | Config->AcceptPromiscuous = FALSE;\r | |
58 | Config->AcceptAnyPort = FALSE;\r | |
59 | Config->AllowDuplicatePort = FALSE;\r | |
60 | Config->TrafficClass = 0;\r | |
61 | Config->HopLimit = 128;\r | |
62 | Config->ReceiveTimeout = 0;\r | |
63 | Config->TransmitTimeout = 0;\r | |
64 | \r | |
65 | //\r | |
66 | // Configure an endpoint of client(0, 546), server(0, 0), the addresses\r | |
67 | // will be overridden later. Note that we MUST not limit RemotePort.\r | |
68 | // More details, refer to RFC 3315 section 5.2.\r | |
69 | //\r | |
70 | Config->StationPort = DHCP6_PORT_CLIENT;\r | |
71 | Config->RemotePort = 0;\r | |
72 | \r | |
73 | return Udp6->Configure (Udp6, Config);;\r | |
74 | }\r | |
75 | \r | |
76 | \r | |
77 | /**\r | |
75dce340 | 78 | Destroy the Dhcp6 service. The Dhcp6 service may be partly initialized,\r |
a3bcde70 HT |
79 | or partly destroyed. If a resource is destroyed, it is marked as such in\r |
80 | case the destroy failed and being called again later.\r | |
81 | \r | |
82 | @param[in, out] Service The pointer to Dhcp6 service to be destroyed.\r | |
83 | \r | |
84 | **/\r | |
85 | VOID\r | |
86 | Dhcp6DestroyService (\r | |
87 | IN OUT DHCP6_SERVICE *Service\r | |
88 | )\r | |
89 | {\r | |
90 | //\r | |
75dce340 | 91 | // All children instances should have been already destroyed here.\r |
a3bcde70 HT |
92 | //\r |
93 | ASSERT (Service->NumOfChild == 0);\r | |
94 | \r | |
95 | if (Service->ClientId != NULL) {\r | |
96 | FreePool (Service->ClientId);\r | |
97 | }\r | |
98 | \r | |
99 | if (Service->UdpIo != NULL) {\r | |
100 | UdpIoFreeIo (Service->UdpIo);\r | |
101 | }\r | |
102 | \r | |
103 | FreePool (Service);\r | |
104 | }\r | |
105 | \r | |
106 | \r | |
107 | /**\r | |
108 | Create a new Dhcp6 service for the Nic controller.\r | |
109 | \r | |
110 | @param[in] Controller The controller to be installed DHCP6 service\r | |
111 | binding protocol.\r | |
112 | @param[in] ImageHandle The image handle of the Dhcp6 driver.\r | |
113 | @param[out] Service The return pointer of the new Dhcp6 service.\r | |
114 | \r | |
115 | @retval EFI_SUCCESS The Dhcp6 service is created successfully.\r | |
116 | @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r | |
117 | @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.\r | |
118 | \r | |
119 | **/\r | |
120 | EFI_STATUS\r | |
121 | Dhcp6CreateService (\r | |
122 | IN EFI_HANDLE Controller,\r | |
123 | IN EFI_HANDLE ImageHandle,\r | |
124 | OUT DHCP6_SERVICE **Service\r | |
125 | )\r | |
126 | {\r | |
127 | DHCP6_SERVICE *Dhcp6Srv;\r | |
cc658224 | 128 | EFI_STATUS Status;\r |
a3bcde70 HT |
129 | \r |
130 | *Service = NULL;\r | |
131 | Dhcp6Srv = AllocateZeroPool (sizeof (DHCP6_SERVICE));\r | |
132 | \r | |
133 | if (Dhcp6Srv == NULL) {\r | |
134 | return EFI_OUT_OF_RESOURCES;\r | |
135 | }\r | |
136 | \r | |
137 | //\r | |
138 | // Open the SNP protocol to get mode data later.\r | |
139 | //\r | |
140 | Dhcp6Srv->Snp = NULL;\r | |
141 | NetLibGetSnpHandle (Controller, &Dhcp6Srv->Snp);\r | |
142 | if (Dhcp6Srv->Snp == NULL) {\r | |
143 | FreePool (Dhcp6Srv);\r | |
144 | return EFI_DEVICE_ERROR;\r | |
145 | }\r | |
146 | \r | |
147 | //\r | |
148 | // Initialize the fields of the new Dhcp6 service.\r | |
149 | //\r | |
150 | Dhcp6Srv->Signature = DHCP6_SERVICE_SIGNATURE;\r | |
a3bcde70 HT |
151 | Dhcp6Srv->Controller = Controller;\r |
152 | Dhcp6Srv->Image = ImageHandle;\r | |
153 | Dhcp6Srv->Xid = (0xffffff & NET_RANDOM (NetRandomInitSeed ()));\r | |
154 | \r | |
155 | CopyMem (\r | |
156 | &Dhcp6Srv->ServiceBinding,\r | |
157 | &gDhcp6ServiceBindingTemplate,\r | |
158 | sizeof (EFI_SERVICE_BINDING_PROTOCOL)\r | |
159 | );\r | |
160 | \r | |
cc658224 | 161 | //\r |
162 | // Locate Ip6->Ip6Config and store it for get IP6 Duplicate Address Detection transmits.\r | |
163 | //\r | |
164 | Status = gBS->HandleProtocol (\r | |
165 | Controller,\r | |
166 | &gEfiIp6ConfigProtocolGuid,\r | |
167 | (VOID **) &Dhcp6Srv->Ip6Cfg\r | |
168 | );\r | |
169 | if (EFI_ERROR (Status)) {\r | |
170 | FreePool (Dhcp6Srv);\r | |
171 | return Status;\r | |
172 | }\r | |
173 | \r | |
a3bcde70 | 174 | //\r |
33c09637 | 175 | // Generate client Duid: If SMBIOS system UUID is located, generate DUID in DUID-UUID format.\r |
176 | // Otherwise, in DUID-LLT format.\r | |
a3bcde70 HT |
177 | //\r |
178 | Dhcp6Srv->ClientId = Dhcp6GenerateClientId (Dhcp6Srv->Snp->Mode);\r | |
179 | \r | |
180 | if (Dhcp6Srv->ClientId == NULL) {\r | |
181 | FreePool (Dhcp6Srv);\r | |
182 | return EFI_DEVICE_ERROR;\r | |
183 | }\r | |
184 | \r | |
185 | //\r | |
186 | // Create an Udp6Io for stateful transmit/receive of each Dhcp6 instance.\r | |
187 | //\r | |
188 | Dhcp6Srv->UdpIo = UdpIoCreateIo (\r | |
189 | Controller,\r | |
190 | ImageHandle,\r | |
191 | Dhcp6ConfigureUdpIo,\r | |
192 | UDP_IO_UDP6_VERSION,\r | |
193 | NULL\r | |
194 | );\r | |
195 | \r | |
196 | if (Dhcp6Srv->UdpIo == NULL) {\r | |
197 | FreePool (Dhcp6Srv->ClientId);\r | |
198 | FreePool (Dhcp6Srv);\r | |
199 | return EFI_DEVICE_ERROR;\r | |
200 | }\r | |
201 | \r | |
202 | InitializeListHead (&Dhcp6Srv->Child);\r | |
203 | \r | |
204 | *Service = Dhcp6Srv;\r | |
205 | \r | |
206 | return EFI_SUCCESS;\r | |
207 | }\r | |
208 | \r | |
209 | \r | |
210 | /**\r | |
211 | Destroy the Dhcp6 instance and recycle the resources.\r | |
212 | \r | |
213 | @param[in, out] Instance The pointer to the Dhcp6 instance.\r | |
214 | \r | |
215 | **/\r | |
216 | VOID\r | |
217 | Dhcp6DestroyInstance (\r | |
218 | IN OUT DHCP6_INSTANCE *Instance\r | |
219 | )\r | |
220 | {\r | |
221 | //\r | |
222 | // Clean up the retry list first.\r | |
223 | //\r | |
224 | Dhcp6CleanupRetry (Instance, DHCP6_PACKET_ALL);\r | |
225 | gBS->CloseEvent (Instance->Timer);\r | |
226 | \r | |
227 | //\r | |
228 | // Clean up the current configure data.\r | |
229 | //\r | |
230 | if (Instance->Config != NULL) {\r | |
231 | Dhcp6CleanupConfigData (Instance->Config);\r | |
232 | FreePool (Instance->Config);\r | |
233 | }\r | |
234 | \r | |
235 | //\r | |
236 | // Clean up the current Ia.\r | |
237 | //\r | |
238 | if (Instance->IaCb.Ia != NULL) {\r | |
239 | if (Instance->IaCb.Ia->ReplyPacket != NULL) {\r | |
240 | FreePool (Instance->IaCb.Ia->ReplyPacket);\r | |
241 | }\r | |
242 | FreePool (Instance->IaCb.Ia);\r | |
243 | }\r | |
244 | \r | |
245 | if (Instance->Unicast != NULL) {\r | |
246 | FreePool (Instance->Unicast);\r | |
247 | }\r | |
248 | \r | |
249 | if (Instance->AdSelect != NULL) {\r | |
250 | FreePool (Instance->AdSelect);\r | |
251 | }\r | |
252 | \r | |
253 | FreePool (Instance);\r | |
254 | }\r | |
255 | \r | |
256 | \r | |
257 | /**\r | |
258 | Create the Dhcp6 instance and initialize it.\r | |
259 | \r | |
260 | @param[in] Service The pointer to the Dhcp6 service.\r | |
261 | @param[out] Instance The pointer to the Dhcp6 instance.\r | |
262 | \r | |
263 | @retval EFI_SUCCESS The Dhcp6 instance is created.\r | |
264 | @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.\r | |
265 | \r | |
266 | **/\r | |
267 | EFI_STATUS\r | |
268 | Dhcp6CreateInstance (\r | |
269 | IN DHCP6_SERVICE *Service,\r | |
270 | OUT DHCP6_INSTANCE **Instance\r | |
271 | )\r | |
272 | {\r | |
273 | EFI_STATUS Status;\r | |
274 | DHCP6_INSTANCE *Dhcp6Ins;\r | |
275 | \r | |
276 | *Instance = NULL;\r | |
277 | Dhcp6Ins = AllocateZeroPool (sizeof (DHCP6_INSTANCE));\r | |
278 | \r | |
279 | if (Dhcp6Ins == NULL) {\r | |
280 | return EFI_OUT_OF_RESOURCES;\r | |
281 | }\r | |
282 | \r | |
283 | //\r | |
284 | // Initialize the fields of the new Dhcp6 instance.\r | |
285 | //\r | |
286 | Dhcp6Ins->Signature = DHCP6_INSTANCE_SIGNATURE;\r | |
287 | Dhcp6Ins->UdpSts = EFI_ALREADY_STARTED;\r | |
288 | Dhcp6Ins->Service = Service;\r | |
75dce340 | 289 | Dhcp6Ins->InDestroy = FALSE;\r |
a3bcde70 HT |
290 | Dhcp6Ins->MediaPresent = TRUE;\r |
291 | \r | |
292 | CopyMem (\r | |
293 | &Dhcp6Ins->Dhcp6,\r | |
294 | &gDhcp6ProtocolTemplate,\r | |
295 | sizeof (EFI_DHCP6_PROTOCOL)\r | |
296 | );\r | |
297 | \r | |
298 | InitializeListHead (&Dhcp6Ins->TxList);\r | |
299 | InitializeListHead (&Dhcp6Ins->InfList);\r | |
300 | \r | |
301 | //\r | |
302 | // There is a timer for each Dhcp6 instance, which is used to track the\r | |
f97117ba | 303 | // lease time of Ia and the retransmission time of all sent packets.\r |
a3bcde70 HT |
304 | //\r |
305 | Status = gBS->CreateEvent (\r | |
306 | EVT_NOTIFY_SIGNAL | EVT_TIMER,\r | |
307 | TPL_CALLBACK,\r | |
308 | Dhcp6OnTimerTick,\r | |
309 | Dhcp6Ins,\r | |
310 | &Dhcp6Ins->Timer\r | |
311 | );\r | |
312 | \r | |
313 | if (EFI_ERROR (Status)) {\r | |
314 | FreePool (Dhcp6Ins);\r | |
315 | return Status;\r | |
316 | }\r | |
317 | \r | |
318 | *Instance = Dhcp6Ins;\r | |
319 | \r | |
320 | return EFI_SUCCESS;\r | |
321 | }\r | |
322 | \r | |
216f7970 | 323 | /**\r |
324 | Callback function which provided by user to remove one node in NetDestroyLinkList process.\r | |
f75a7f56 | 325 | \r |
216f7970 | 326 | @param[in] Entry The entry to be removed.\r |
327 | @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.\r | |
328 | \r | |
329 | @retval EFI_SUCCESS The entry has been removed successfully.\r | |
330 | @retval Others Fail to remove the entry.\r | |
331 | \r | |
332 | **/\r | |
333 | EFI_STATUS\r | |
1f7eb561 | 334 | EFIAPI\r |
216f7970 | 335 | Dhcp6DestroyChildEntry (\r |
336 | IN LIST_ENTRY *Entry,\r | |
337 | IN VOID *Context\r | |
1f7eb561 | 338 | )\r |
216f7970 | 339 | {\r |
340 | DHCP6_INSTANCE *Instance;\r | |
341 | EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;\r | |
342 | \r | |
343 | if (Entry == NULL || Context == NULL) {\r | |
344 | return EFI_INVALID_PARAMETER;\r | |
345 | }\r | |
346 | \r | |
347 | Instance = NET_LIST_USER_STRUCT_S (Entry, DHCP6_INSTANCE, Link, DHCP6_INSTANCE_SIGNATURE);\r | |
348 | ServiceBinding = (EFI_SERVICE_BINDING_PROTOCOL *) Context;\r | |
f75a7f56 | 349 | \r |
216f7970 | 350 | return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);\r |
351 | }\r | |
352 | \r | |
a3bcde70 HT |
353 | \r |
354 | /**\r | |
355 | Entry point of the DHCP6 driver to install various protocols.\r | |
356 | \r | |
357 | @param[in] ImageHandle The handle of the UEFI image file.\r | |
358 | @param[in] SystemTable The pointer to the EFI System Table.\r | |
359 | \r | |
360 | @retval EFI_SUCCESS The operation completed successfully.\r | |
361 | @retval Others Unexpected error occurs.\r | |
362 | \r | |
363 | **/\r | |
364 | EFI_STATUS\r | |
365 | EFIAPI\r | |
366 | Dhcp6DriverEntryPoint (\r | |
367 | IN EFI_HANDLE ImageHandle,\r | |
368 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
369 | )\r | |
370 | {\r | |
371 | return EfiLibInstallDriverBindingComponentName2 (\r | |
372 | ImageHandle,\r | |
373 | SystemTable,\r | |
374 | &gDhcp6DriverBinding,\r | |
375 | ImageHandle,\r | |
376 | &gDhcp6ComponentName,\r | |
377 | &gDhcp6ComponentName2\r | |
378 | );\r | |
379 | }\r | |
380 | \r | |
381 | \r | |
382 | /**\r | |
383 | Test to see if this driver supports ControllerHandle. This service\r | |
384 | is called by the EFI boot service ConnectController(). In\r | |
385 | order to make drivers as small as possible, there are a few calling\r | |
386 | restrictions for this service. ConnectController() must\r | |
387 | follow these calling restrictions. If any other agent wishes to call\r | |
388 | Supported() it must also follow these calling restrictions.\r | |
389 | \r | |
390 | @param[in] This The pointer to the driver binding protocol.\r | |
391 | @param[in] ControllerHandle The handle of device to be tested.\r | |
392 | @param[in] RemainingDevicePath Optional parameter use to pick a specific child\r | |
393 | device to be started.\r | |
394 | \r | |
395 | @retval EFI_SUCCESS This driver supports this device.\r | |
396 | @retval Others This driver does not support this device.\r | |
397 | \r | |
398 | **/\r | |
399 | EFI_STATUS\r | |
400 | EFIAPI\r | |
401 | Dhcp6DriverBindingSupported (\r | |
402 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
403 | IN EFI_HANDLE ControllerHandle,\r | |
404 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r | |
405 | )\r | |
406 | {\r | |
407 | return gBS->OpenProtocol (\r | |
408 | ControllerHandle,\r | |
409 | &gEfiUdp6ServiceBindingProtocolGuid,\r | |
410 | NULL,\r | |
411 | This->DriverBindingHandle,\r | |
412 | ControllerHandle,\r | |
413 | EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r | |
414 | );\r | |
415 | }\r | |
416 | \r | |
417 | \r | |
418 | /**\r | |
419 | Start this driver on ControllerHandle. This service is called by the\r | |
420 | EFI boot service ConnectController(). In order to make\r | |
421 | drivers as small as possible, there are a few calling restrictions for\r | |
422 | this service. ConnectController() must follow these\r | |
423 | calling restrictions. If any other agent wishes to call Start() it\r | |
424 | must also follow these calling restrictions.\r | |
425 | \r | |
426 | @param[in] This The pointer to the driver binding protocol.\r | |
427 | @param[in] ControllerHandle The handle of device to be started.\r | |
428 | @param[in] RemainingDevicePath Optional parameter use to pick a specific child\r | |
429 | device to be started.\r | |
430 | \r | |
431 | @retval EFI_SUCCESS This driver is installed to ControllerHandle.\r | |
432 | @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.\r | |
433 | @retval other This driver does not support this device.\r | |
434 | \r | |
435 | **/\r | |
436 | EFI_STATUS\r | |
437 | EFIAPI\r | |
438 | Dhcp6DriverBindingStart (\r | |
439 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
440 | IN EFI_HANDLE ControllerHandle,\r | |
441 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r | |
442 | )\r | |
443 | {\r | |
444 | EFI_STATUS Status;\r | |
445 | DHCP6_SERVICE *Service;\r | |
446 | \r | |
447 | //\r | |
f97117ba | 448 | // Check the Dhcp6 service whether already started.\r |
a3bcde70 HT |
449 | //\r |
450 | Status = gBS->OpenProtocol (\r | |
451 | ControllerHandle,\r | |
452 | &gEfiDhcp6ServiceBindingProtocolGuid,\r | |
453 | NULL,\r | |
454 | This->DriverBindingHandle,\r | |
455 | ControllerHandle,\r | |
456 | EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r | |
457 | );\r | |
458 | \r | |
459 | if (!EFI_ERROR (Status)) {\r | |
460 | return EFI_ALREADY_STARTED;\r | |
461 | }\r | |
462 | \r | |
463 | //\r | |
464 | // Create and initialize the Dhcp6 service.\r | |
465 | //\r | |
466 | Status = Dhcp6CreateService (\r | |
467 | ControllerHandle,\r | |
468 | This->DriverBindingHandle,\r | |
469 | &Service\r | |
470 | );\r | |
471 | \r | |
472 | if (EFI_ERROR (Status)) {\r | |
473 | return Status;\r | |
474 | }\r | |
475 | \r | |
476 | ASSERT (Service != NULL);\r | |
477 | \r | |
478 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
479 | &ControllerHandle,\r | |
480 | &gEfiDhcp6ServiceBindingProtocolGuid,\r | |
481 | &Service->ServiceBinding,\r | |
482 | NULL\r | |
483 | );\r | |
484 | \r | |
485 | if (EFI_ERROR (Status)) {\r | |
486 | Dhcp6DestroyService (Service);\r | |
487 | return Status;\r | |
488 | }\r | |
489 | \r | |
490 | return EFI_SUCCESS;\r | |
491 | }\r | |
492 | \r | |
493 | \r | |
494 | /**\r | |
495 | Stop this driver on ControllerHandle. This service is called by the\r | |
496 | EFI boot service DisconnectController(). In order to\r | |
497 | make drivers as small as possible, there are a few calling\r | |
498 | restrictions for this service. DisconnectController()\r | |
499 | must follow these calling restrictions. If any other agent wishes\r | |
500 | to call Stop() it must also follow these calling restrictions.\r | |
501 | \r | |
502 | @param[in] This Protocol instance pointer.\r | |
503 | @param[in] ControllerHandle Handle of device to stop driver on\r | |
504 | @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r | |
505 | children is zero stop the entire bus driver.\r | |
506 | @param[in] ChildHandleBuffer List of Child Handles to Stop.\r | |
507 | \r | |
508 | @retval EFI_SUCCESS This driver is removed ControllerHandle\r | |
509 | @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r | |
510 | @retval other This driver was not removed from this device\r | |
511 | \r | |
512 | **/\r | |
513 | EFI_STATUS\r | |
514 | EFIAPI\r | |
515 | Dhcp6DriverBindingStop (\r | |
516 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
517 | IN EFI_HANDLE ControllerHandle,\r | |
518 | IN UINTN NumberOfChildren,\r | |
519 | IN EFI_HANDLE *ChildHandleBuffer OPTIONAL\r | |
520 | )\r | |
521 | {\r | |
522 | EFI_STATUS Status;\r | |
a3bcde70 HT |
523 | EFI_HANDLE NicHandle;\r |
524 | EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;\r | |
525 | DHCP6_SERVICE *Service;\r | |
216f7970 | 526 | LIST_ENTRY *List;\r |
527 | UINTN ListLength;\r | |
a3bcde70 HT |
528 | \r |
529 | //\r | |
530 | // Find and check the Nic handle by the controller handle.\r | |
531 | //\r | |
532 | NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp6ProtocolGuid);\r | |
533 | \r | |
534 | if (NicHandle == NULL) {\r | |
216f7970 | 535 | return EFI_SUCCESS;\r |
a3bcde70 HT |
536 | }\r |
537 | \r | |
538 | Status = gBS->OpenProtocol (\r | |
539 | NicHandle,\r | |
540 | &gEfiDhcp6ServiceBindingProtocolGuid,\r | |
541 | (VOID **) &ServiceBinding,\r | |
542 | This->DriverBindingHandle,\r | |
543 | NicHandle,\r | |
544 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
545 | );\r | |
546 | \r | |
547 | if (EFI_ERROR (Status)) {\r | |
548 | return Status;\r | |
549 | }\r | |
550 | \r | |
551 | Service = DHCP6_SERVICE_FROM_THIS (ServiceBinding);\r | |
216f7970 | 552 | if (!IsListEmpty (&Service->Child)) {\r |
553 | //\r | |
f97117ba | 554 | // Destroy all the children instances before destroy the service.\r |
f75a7f56 | 555 | //\r |
216f7970 | 556 | List = &Service->Child;\r |
557 | Status = NetDestroyLinkList (\r | |
558 | List,\r | |
559 | Dhcp6DestroyChildEntry,\r | |
560 | ServiceBinding,\r | |
561 | &ListLength\r | |
562 | );\r | |
563 | if (EFI_ERROR (Status) || ListLength != 0) {\r | |
564 | Status = EFI_DEVICE_ERROR;\r | |
565 | }\r | |
a3bcde70 HT |
566 | }\r |
567 | \r | |
216f7970 | 568 | if (NumberOfChildren == 0 && !IsListEmpty (&Service->Child)) {\r |
569 | Status = EFI_DEVICE_ERROR;\r | |
570 | }\r | |
a3bcde70 | 571 | \r |
216f7970 | 572 | if (NumberOfChildren == 0 && IsListEmpty (&Service->Child)) {\r |
a3bcde70 | 573 | //\r |
75dce340 | 574 | // Destroy the service itself if no child instance left.\r |
a3bcde70 | 575 | //\r |
a3bcde70 HT |
576 | Status = gBS->UninstallProtocolInterface (\r |
577 | NicHandle,\r | |
578 | &gEfiDhcp6ServiceBindingProtocolGuid,\r | |
579 | ServiceBinding\r | |
580 | );\r | |
a3bcde70 | 581 | if (EFI_ERROR (Status)) {\r |
a3bcde70 HT |
582 | goto ON_EXIT;\r |
583 | }\r | |
584 | \r | |
585 | Dhcp6DestroyService (Service);\r | |
216f7970 | 586 | Status = EFI_SUCCESS;\r |
a3bcde70 | 587 | }\r |
f75a7f56 | 588 | \r |
a3bcde70 | 589 | ON_EXIT:\r |
a3bcde70 HT |
590 | return Status;\r |
591 | }\r | |
592 | \r | |
593 | \r | |
594 | /**\r | |
595 | Creates a child handle and installs a protocol.\r | |
596 | \r | |
597 | The CreateChild() function installs a protocol on ChildHandle.\r | |
598 | If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.\r | |
599 | If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.\r | |
600 | \r | |
601 | @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r | |
602 | @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL,\r | |
603 | then a new handle is created. If it is a pointer to an existing\r | |
604 | UEFI handle, then the protocol is added to the existing UEFI handle.\r | |
605 | \r | |
f97117ba | 606 | @retval EFI_SUCCESS The protocol was added to ChildHandle.\r |
a3bcde70 HT |
607 | @retval EFI_INVALID_PARAMETER ChildHandle is NULL.\r |
608 | @retval other The child handle was not created.\r | |
609 | \r | |
610 | **/\r | |
611 | EFI_STATUS\r | |
612 | EFIAPI\r | |
613 | Dhcp6ServiceBindingCreateChild (\r | |
614 | IN EFI_SERVICE_BINDING_PROTOCOL *This,\r | |
615 | IN OUT EFI_HANDLE *ChildHandle\r | |
616 | )\r | |
617 | {\r | |
618 | EFI_STATUS Status;\r | |
619 | EFI_TPL OldTpl;\r | |
620 | DHCP6_SERVICE *Service;\r | |
621 | DHCP6_INSTANCE *Instance;\r | |
622 | VOID *Udp6;\r | |
623 | \r | |
624 | if (This == NULL || ChildHandle == NULL) {\r | |
625 | return EFI_INVALID_PARAMETER;\r | |
626 | }\r | |
627 | \r | |
628 | Service = DHCP6_SERVICE_FROM_THIS (This);\r | |
629 | \r | |
630 | Status = Dhcp6CreateInstance (Service, &Instance);\r | |
631 | \r | |
632 | if (EFI_ERROR (Status)) {\r | |
633 | return Status;\r | |
634 | }\r | |
635 | \r | |
636 | ASSERT (Instance != NULL);\r | |
637 | \r | |
638 | //\r | |
639 | // Start the timer when the instance is ready to use.\r | |
640 | //\r | |
641 | Status = gBS->SetTimer (\r | |
642 | Instance->Timer,\r | |
643 | TimerPeriodic,\r | |
644 | TICKS_PER_SECOND\r | |
645 | );\r | |
646 | \r | |
647 | if (EFI_ERROR (Status)) {\r | |
648 | goto ON_ERROR;\r | |
649 | }\r | |
650 | \r | |
651 | //\r | |
652 | // Install the DHCP6 protocol onto ChildHandle.\r | |
653 | //\r | |
654 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
655 | ChildHandle,\r | |
656 | &gEfiDhcp6ProtocolGuid,\r | |
657 | &Instance->Dhcp6,\r | |
658 | NULL\r | |
659 | );\r | |
660 | \r | |
661 | if (EFI_ERROR (Status)) {\r | |
662 | goto ON_ERROR;\r | |
663 | }\r | |
664 | \r | |
665 | Instance->Handle = *ChildHandle;\r | |
666 | \r | |
667 | //\r | |
668 | // Open the UDP6 protocol BY_CHILD.\r | |
669 | //\r | |
670 | Status = gBS->OpenProtocol (\r | |
671 | Service->UdpIo->UdpHandle,\r | |
672 | &gEfiUdp6ProtocolGuid,\r | |
673 | (VOID **) &Udp6,\r | |
674 | gDhcp6DriverBinding.DriverBindingHandle,\r | |
675 | Instance->Handle,\r | |
676 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r | |
677 | );\r | |
678 | \r | |
679 | if (EFI_ERROR (Status)) {\r | |
680 | \r | |
681 | gBS->UninstallMultipleProtocolInterfaces (\r | |
682 | Instance->Handle,\r | |
683 | &gEfiDhcp6ProtocolGuid,\r | |
684 | &Instance->Dhcp6,\r | |
685 | NULL\r | |
686 | );\r | |
687 | goto ON_ERROR;\r | |
688 | }\r | |
689 | \r | |
690 | //\r | |
691 | // Add into the children list of its parent service.\r | |
692 | //\r | |
693 | OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r | |
694 | \r | |
695 | InsertTailList (&Service->Child, &Instance->Link);\r | |
696 | Service->NumOfChild++;\r | |
697 | \r | |
698 | gBS->RestoreTPL (OldTpl);\r | |
699 | return EFI_SUCCESS;\r | |
700 | \r | |
701 | ON_ERROR:\r | |
702 | \r | |
703 | Dhcp6DestroyInstance (Instance);\r | |
704 | return Status;\r | |
705 | }\r | |
706 | \r | |
707 | \r | |
708 | /**\r | |
709 | Destroys a child handle with a protocol installed on it.\r | |
710 | \r | |
711 | The DestroyChild() function does the opposite of CreateChild(). It removes a protocol\r | |
712 | that was installed by CreateChild() from ChildHandle. If the removed protocol is the\r | |
713 | last protocol on ChildHandle, then ChildHandle is destroyed.\r | |
714 | \r | |
715 | @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r | |
716 | @param[in] ChildHandle Handle of the child to destroy\r | |
717 | \r | |
f97117ba | 718 | @retval EFI_SUCCESS The protocol was removed from ChildHandle.\r |
a3bcde70 | 719 | @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.\r |
15ee13fc | 720 | @retval EFI_INVALID_PARAMETER Child handle is NULL.\r |
a3bcde70 HT |
721 | @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle\r |
722 | because its services are being used.\r | |
723 | @retval other The child handle was not destroyed\r | |
724 | \r | |
725 | **/\r | |
726 | EFI_STATUS\r | |
727 | EFIAPI\r | |
728 | Dhcp6ServiceBindingDestroyChild (\r | |
729 | IN EFI_SERVICE_BINDING_PROTOCOL *This,\r | |
730 | IN EFI_HANDLE ChildHandle\r | |
731 | )\r | |
732 | {\r | |
733 | EFI_STATUS Status;\r | |
734 | EFI_TPL OldTpl;\r | |
735 | EFI_DHCP6_PROTOCOL *Dhcp6;\r | |
736 | DHCP6_SERVICE *Service;\r | |
737 | DHCP6_INSTANCE *Instance;\r | |
738 | \r | |
739 | if (This == NULL || ChildHandle == NULL) {\r | |
740 | return EFI_INVALID_PARAMETER;\r | |
741 | }\r | |
742 | \r | |
743 | //\r | |
744 | // Retrieve the private context data structures\r | |
745 | //\r | |
746 | Status = gBS->OpenProtocol (\r | |
747 | ChildHandle,\r | |
748 | &gEfiDhcp6ProtocolGuid,\r | |
749 | (VOID **) &Dhcp6,\r | |
750 | gDhcp6DriverBinding.DriverBindingHandle,\r | |
751 | ChildHandle,\r | |
752 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
753 | );\r | |
754 | \r | |
755 | if (EFI_ERROR (Status)) {\r | |
756 | return EFI_UNSUPPORTED;\r | |
757 | }\r | |
758 | \r | |
759 | Instance = DHCP6_INSTANCE_FROM_THIS (Dhcp6);\r | |
760 | Service = DHCP6_SERVICE_FROM_THIS (This);\r | |
761 | \r | |
762 | if (Instance->Service != Service) {\r | |
763 | return EFI_INVALID_PARAMETER;\r | |
764 | }\r | |
765 | \r | |
75dce340 | 766 | if (Instance->InDestroy) {\r |
a3bcde70 HT |
767 | return EFI_SUCCESS;\r |
768 | }\r | |
769 | \r | |
770 | OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r | |
771 | \r | |
75dce340 | 772 | Instance->InDestroy = TRUE;\r |
a3bcde70 HT |
773 | \r |
774 | Status = gBS->CloseProtocol (\r | |
775 | Service->UdpIo->UdpHandle,\r | |
776 | &gEfiUdp6ProtocolGuid,\r | |
777 | gDhcp6DriverBinding.DriverBindingHandle,\r | |
778 | ChildHandle\r | |
779 | );\r | |
780 | \r | |
781 | if (EFI_ERROR (Status)) {\r | |
75dce340 | 782 | Instance->InDestroy = FALSE;\r |
a3bcde70 HT |
783 | gBS->RestoreTPL (OldTpl);\r |
784 | return Status;\r | |
785 | }\r | |
786 | \r | |
787 | //\r | |
788 | // Uninstall the MTFTP6 protocol first to enable a top down destruction.\r | |
789 | //\r | |
216f7970 | 790 | gBS->RestoreTPL (OldTpl);\r |
a3bcde70 HT |
791 | Status = gBS->UninstallProtocolInterface (\r |
792 | ChildHandle,\r | |
793 | &gEfiDhcp6ProtocolGuid,\r | |
794 | Dhcp6\r | |
795 | );\r | |
216f7970 | 796 | OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r |
a3bcde70 | 797 | if (EFI_ERROR (Status)) {\r |
75dce340 | 798 | Instance->InDestroy = FALSE;\r |
a3bcde70 HT |
799 | gBS->RestoreTPL (OldTpl);\r |
800 | return Status;\r | |
801 | }\r | |
802 | \r | |
803 | //\r | |
804 | // Remove it from the children list of its parent service.\r | |
805 | //\r | |
806 | RemoveEntryList (&Instance->Link);\r | |
807 | Service->NumOfChild--;\r | |
808 | \r | |
a3bcde70 HT |
809 | gBS->RestoreTPL (OldTpl);\r |
810 | \r | |
216f7970 | 811 | Dhcp6DestroyInstance (Instance);\r |
a3bcde70 HT |
812 | return EFI_SUCCESS;\r |
813 | }\r |