]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Dhcp4Dxe/Dhcp4Driver.c
1. remove duplicated NetLibDispatchDpc() calling in Pool function.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Dhcp4Dxe / Dhcp4Driver.c
1 /** @file
2
3 Copyright (c) 2006 - 2008, Intel Corporation.<BR>
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 **/
13
14 #include "Dhcp4Impl.h"
15 #include "Dhcp4Driver.h"
16
17 EFI_DRIVER_BINDING_PROTOCOL gDhcp4DriverBinding = {
18 Dhcp4DriverBindingSupported,
19 Dhcp4DriverBindingStart,
20 Dhcp4DriverBindingStop,
21 0xa,
22 NULL,
23 NULL
24 };
25
26 EFI_SERVICE_BINDING_PROTOCOL mDhcp4ServiceBindingTemplate = {
27 Dhcp4ServiceBindingCreateChild,
28 Dhcp4ServiceBindingDestroyChild
29 };
30
31 /**
32 This is the declaration of an EFI image entry point. This entry point is
33 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
34 both device drivers and bus drivers.
35
36 Entry point of the DHCP driver to install various protocols.
37
38 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
39 @param[in] SystemTable A pointer to the EFI System Table.
40
41 @retval EFI_SUCCESS The operation completed successfully.
42 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
43
44 **/
45 EFI_STATUS
46 EFIAPI
47 Dhcp4DriverEntryPoint (
48 IN EFI_HANDLE ImageHandle,
49 IN EFI_SYSTEM_TABLE *SystemTable
50 )
51 {
52 return EfiLibInstallDriverBindingComponentName2 (
53 ImageHandle,
54 SystemTable,
55 &gDhcp4DriverBinding,
56 ImageHandle,
57 &gDhcp4ComponentName,
58 &gDhcp4ComponentName2
59 );
60 }
61
62
63 /**
64 Test to see if this driver supports ControllerHandle. This service
65 is called by the EFI boot service ConnectController(). In
66 order to make drivers as small as possible, there are a few calling
67 restrictions for this service. ConnectController() must
68 follow these calling restrictions. If any other agent wishes to call
69 Supported() it must also follow these calling restrictions.
70
71 @param[in] This Protocol instance pointer.
72 @param[in] ControllerHandle Handle of device to test
73 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
74 device to start.
75
76 @retval EFI_SUCCESS This driver supports this device
77 @retval EFI_ALREADY_STARTED This driver is already running on this device
78 @retval other This driver does not support this device
79
80 **/
81 EFI_STATUS
82 EFIAPI
83 Dhcp4DriverBindingSupported (
84 IN EFI_DRIVER_BINDING_PROTOCOL *This,
85 IN EFI_HANDLE ControllerHandle,
86 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
87 )
88 {
89 EFI_STATUS Status;
90
91 Status = gBS->OpenProtocol (
92 ControllerHandle,
93 &gEfiUdp4ServiceBindingProtocolGuid,
94 NULL,
95 This->DriverBindingHandle,
96 ControllerHandle,
97 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
98 );
99
100 return Status;
101 }
102
103
104
105 /**
106 Configure the default UDP child to receive all the DHCP traffics
107 on this network interface.
108
109 @param[in] UdpIo The UDP IO port to configure
110 @param[in] Context The context to the function
111
112 @retval EFI_SUCCESS The UDP IO port is successfully configured.
113 @retval Others Failed to configure the UDP child.
114
115 **/
116 EFI_STATUS
117 DhcpConfigUdpIo (
118 IN UDP_IO_PORT *UdpIo,
119 IN VOID *Context
120 )
121 {
122 EFI_UDP4_CONFIG_DATA UdpConfigData;
123
124 UdpConfigData.AcceptBroadcast = TRUE;
125 UdpConfigData.AcceptPromiscuous = FALSE;
126 UdpConfigData.AcceptAnyPort = FALSE;
127 UdpConfigData.AllowDuplicatePort = TRUE;
128 UdpConfigData.TypeOfService = 0;
129 UdpConfigData.TimeToLive = 64;
130 UdpConfigData.DoNotFragment = FALSE;
131 UdpConfigData.ReceiveTimeout = 0;
132 UdpConfigData.TransmitTimeout = 0;
133
134 UdpConfigData.UseDefaultAddress = FALSE;
135 UdpConfigData.StationPort = DHCP_CLIENT_PORT;
136 UdpConfigData.RemotePort = DHCP_SERVER_PORT;
137
138 ZeroMem (&UdpConfigData.StationAddress, sizeof (EFI_IPv4_ADDRESS));
139 ZeroMem (&UdpConfigData.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
140 ZeroMem (&UdpConfigData.RemoteAddress, sizeof (EFI_IPv4_ADDRESS));
141
142 return UdpIo->Udp->Configure (UdpIo->Udp, &UdpConfigData);;
143 }
144
145
146
147 /**
148 Destory the DHCP service. The Dhcp4 service may be partly initialized,
149 or partly destroyed. If a resource is destroyed, it is marked as so in
150 case the destroy failed and being called again later.
151
152 @param[in] DhcpSb The DHCP service instance to destory.
153
154 @retval EFI_SUCCESS Always return success.
155
156 **/
157 EFI_STATUS
158 Dhcp4CloseService (
159 IN DHCP_SERVICE *DhcpSb
160 )
161 {
162 DhcpCleanLease (DhcpSb);
163
164 if (DhcpSb->UdpIo != NULL) {
165 UdpIoFreePort (DhcpSb->UdpIo);
166 DhcpSb->UdpIo = NULL;
167 }
168
169 if (DhcpSb->Timer != NULL) {
170 gBS->SetTimer (DhcpSb->Timer, TimerCancel, 0);
171 gBS->CloseEvent (DhcpSb->Timer);
172
173 DhcpSb->Timer = NULL;
174 }
175
176 return EFI_SUCCESS;
177 }
178
179
180
181 /**
182 Create a new DHCP service binding instance for the controller.
183
184 @param[in] Controller The controller to install DHCP service binding
185 protocol onto
186 @param[in] ImageHandle The driver's image handle
187 @param[out] Service The variable to receive the created DHCP service
188 instance.
189
190 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource .
191 @retval EFI_SUCCESS The DHCP service instance is created.
192 @retval other Other error occurs.
193
194 **/
195 EFI_STATUS
196 Dhcp4CreateService (
197 IN EFI_HANDLE Controller,
198 IN EFI_HANDLE ImageHandle,
199 OUT DHCP_SERVICE **Service
200 )
201 {
202 DHCP_SERVICE *DhcpSb;
203 EFI_STATUS Status;
204
205 *Service = NULL;
206 DhcpSb = AllocateZeroPool (sizeof (DHCP_SERVICE));
207
208 if (DhcpSb == NULL) {
209 return EFI_OUT_OF_RESOURCES;
210 }
211
212 DhcpSb->Signature = DHCP_SERVICE_SIGNATURE;
213 DhcpSb->ServiceState = DHCP_UNCONFIGED;
214 DhcpSb->InDestory = FALSE;
215 DhcpSb->Controller = Controller;
216 DhcpSb->Image = ImageHandle;
217 InitializeListHead (&DhcpSb->Children);
218 DhcpSb->DhcpState = Dhcp4Stopped;
219 DhcpSb->Xid = NET_RANDOM (NetRandomInitSeed ());
220 CopyMem (
221 &DhcpSb->ServiceBinding,
222 &mDhcp4ServiceBindingTemplate,
223 sizeof (EFI_SERVICE_BINDING_PROTOCOL)
224 );
225 //
226 // Create various resources, UdpIo, Timer, and get Mac address
227 //
228 Status = gBS->CreateEvent (
229 EVT_NOTIFY_SIGNAL | EVT_TIMER,
230 TPL_CALLBACK,
231 DhcpOnTimerTick,
232 DhcpSb,
233 &DhcpSb->Timer
234 );
235
236 if (EFI_ERROR (Status)) {
237 goto ON_ERROR;
238 }
239
240 DhcpSb->UdpIo = UdpIoCreatePort (Controller, ImageHandle, DhcpConfigUdpIo, NULL);
241
242 if (DhcpSb->UdpIo == NULL) {
243 Status = EFI_OUT_OF_RESOURCES;
244 goto ON_ERROR;
245 }
246
247 DhcpSb->HwLen = (UINT8) DhcpSb->UdpIo->SnpMode.HwAddressSize;
248 DhcpSb->HwType = DhcpSb->UdpIo->SnpMode.IfType;
249 CopyMem (&DhcpSb->Mac, &DhcpSb->UdpIo->SnpMode.CurrentAddress, sizeof (DhcpSb->Mac));
250
251 *Service = DhcpSb;
252 return EFI_SUCCESS;
253
254 ON_ERROR:
255 Dhcp4CloseService (DhcpSb);
256 FreePool (DhcpSb);
257
258 return Status;
259 }
260
261
262 /**
263 Start this driver on ControllerHandle. This service is called by the
264 EFI boot service ConnectController(). In order to make
265 drivers as small as possible, there are a few calling restrictions for
266 this service. ConnectController() must follow these
267 calling restrictions. If any other agent wishes to call Start() it
268 must also follow these calling restrictions.
269
270 @param[in] This Protocol instance pointer.
271 @param[in] ControllerHandle Handle of device to bind driver to
272 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
273 device to start.
274
275 @retval EFI_SUCCESS This driver is added to ControllerHandle
276 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
277 @retval other This driver does not support this device
278
279 **/
280 EFI_STATUS
281 EFIAPI
282 Dhcp4DriverBindingStart (
283 IN EFI_DRIVER_BINDING_PROTOCOL *This,
284 IN EFI_HANDLE ControllerHandle,
285 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
286 )
287 {
288 DHCP_SERVICE *DhcpSb;
289 EFI_STATUS Status;
290
291 //
292 // First: test for the DHCP4 Protocol
293 //
294 Status = gBS->OpenProtocol (
295 ControllerHandle,
296 &gEfiDhcp4ServiceBindingProtocolGuid,
297 NULL,
298 This->DriverBindingHandle,
299 ControllerHandle,
300 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
301 );
302
303 if (Status == EFI_SUCCESS) {
304 return EFI_ALREADY_STARTED;
305 }
306
307 Status = Dhcp4CreateService (ControllerHandle, This->DriverBindingHandle, &DhcpSb);
308
309 if (EFI_ERROR (Status)) {
310 return Status;
311 }
312
313 Status = gBS->SetTimer (DhcpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);
314
315 if (EFI_ERROR (Status)) {
316 goto ON_ERROR;
317 }
318
319 //
320 // Install the Dhcp4ServiceBinding Protocol onto ControlerHandle
321 //
322 Status = gBS->InstallMultipleProtocolInterfaces (
323 &ControllerHandle,
324 &gEfiDhcp4ServiceBindingProtocolGuid,
325 &DhcpSb->ServiceBinding,
326 NULL
327 );
328
329 if (EFI_ERROR (Status)) {
330 goto ON_ERROR;
331 }
332
333 return Status;
334
335 ON_ERROR:
336 Dhcp4CloseService (DhcpSb);
337 FreePool (DhcpSb);
338 return Status;
339 }
340
341
342 /**
343 Stop this driver on ControllerHandle. This service is called by the
344 EFI boot service DisconnectController(). In order to
345 make drivers as small as possible, there are a few calling
346 restrictions for this service. DisconnectController()
347 must follow these calling restrictions. If any other agent wishes
348 to call Stop() it must also follow these calling restrictions.
349
350 @param[in] This Protocol instance pointer.
351 @param[in] ControllerHandle Handle of device to stop driver on
352 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
353 children is zero stop the entire bus driver.
354 @param[in] ChildHandleBuffer List of Child Handles to Stop.
355
356 @retval EFI_SUCCESS This driver is removed ControllerHandle
357 @retval other This driver was not removed from this device
358
359 **/
360 EFI_STATUS
361 EFIAPI
362 Dhcp4DriverBindingStop (
363 IN EFI_DRIVER_BINDING_PROTOCOL *This,
364 IN EFI_HANDLE ControllerHandle,
365 IN UINTN NumberOfChildren,
366 IN EFI_HANDLE *ChildHandleBuffer
367 )
368 {
369 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
370 DHCP_SERVICE *DhcpSb;
371 DHCP_PROTOCOL *Instance;
372 EFI_HANDLE NicHandle;
373 EFI_STATUS Status;
374 EFI_TPL OldTpl;
375
376 //
377 // DHCP driver opens UDP child, So, the ControllerHandle is the
378 // UDP child handle. locate the Nic handle first.
379 //
380 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp4ProtocolGuid);
381
382 if (NicHandle == NULL) {
383 return EFI_DEVICE_ERROR;
384 }
385
386 Status = gBS->OpenProtocol (
387 NicHandle,
388 &gEfiDhcp4ServiceBindingProtocolGuid,
389 (VOID **) &ServiceBinding,
390 This->DriverBindingHandle,
391 NicHandle,
392 EFI_OPEN_PROTOCOL_GET_PROTOCOL
393 );
394
395 if (EFI_ERROR (Status)) {
396 return EFI_DEVICE_ERROR;
397 }
398
399 DhcpSb = DHCP_SERVICE_FROM_THIS (ServiceBinding);
400
401 if (DhcpSb->InDestory) {
402 return EFI_SUCCESS;
403 }
404
405 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
406
407 if (NumberOfChildren == 0) {
408
409 DhcpSb->InDestory = TRUE;
410 DhcpSb->ServiceState = DHCP_DESTORY;
411
412 gBS->UninstallProtocolInterface (
413 NicHandle,
414 &gEfiDhcp4ServiceBindingProtocolGuid,
415 ServiceBinding
416 );
417
418 Dhcp4CloseService (DhcpSb);
419
420 FreePool (DhcpSb);
421 } else {
422 //
423 // Don't use NET_LIST_FOR_EACH_SAFE here, Dhcp4ServiceBindingDestoryChild
424 // may cause other child to be deleted.
425 //
426 while (!IsListEmpty (&DhcpSb->Children)) {
427 Instance = NET_LIST_HEAD (&DhcpSb->Children, DHCP_PROTOCOL, Link);
428 ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);
429 }
430
431 if (DhcpSb->NumChildren != 0) {
432 Status = EFI_DEVICE_ERROR;
433 }
434 }
435
436 gBS->RestoreTPL (OldTpl);
437
438 return Status;
439 }
440
441
442 /**
443 Initialize a new DHCP instance.
444
445 @param DhcpSb The dhcp service instance
446 @param Instance The dhcp instance to initialize
447
448 **/
449 VOID
450 DhcpInitProtocol (
451 IN DHCP_SERVICE *DhcpSb,
452 IN OUT DHCP_PROTOCOL *Instance
453 )
454 {
455 Instance->Signature = DHCP_PROTOCOL_SIGNATURE;
456 CopyMem (&Instance->Dhcp4Protocol, &mDhcp4ProtocolTemplate, sizeof (Instance->Dhcp4Protocol));
457 InitializeListHead (&Instance->Link);
458 Instance->Handle = NULL;
459 Instance->Service = DhcpSb;
460 Instance->InDestory = FALSE;
461 Instance->CompletionEvent = NULL;
462 Instance->RenewRebindEvent = NULL;
463 Instance->Token = NULL;
464 Instance->UdpIo = NULL;
465 NetbufQueInit (&Instance->ResponseQueue);
466 }
467
468
469 /**
470 Creates a child handle and installs a protocol.
471
472 The CreateChild() function installs a protocol on ChildHandle.
473 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
474 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
475
476 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
477 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
478 then a new handle is created. If it is a pointer to an existing UEFI handle,
479 then the protocol is added to the existing UEFI handle.
480
481 @retval EFI_SUCCES The protocol was added to ChildHandle.
482 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
483 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
484 the child
485 @retval other The child handle was not created
486
487 **/
488 EFI_STATUS
489 EFIAPI
490 Dhcp4ServiceBindingCreateChild (
491 IN EFI_SERVICE_BINDING_PROTOCOL *This,
492 IN EFI_HANDLE *ChildHandle
493 )
494 {
495 DHCP_SERVICE *DhcpSb;
496 DHCP_PROTOCOL *Instance;
497 EFI_STATUS Status;
498 EFI_TPL OldTpl;
499 VOID *Udp4;
500
501 if ((This == NULL) || (ChildHandle == NULL)) {
502 return EFI_INVALID_PARAMETER;
503 }
504
505 Instance = AllocatePool (sizeof (*Instance));
506
507 if (Instance == NULL) {
508 return EFI_OUT_OF_RESOURCES;
509 }
510
511 DhcpSb = DHCP_SERVICE_FROM_THIS (This);
512 DhcpInitProtocol (DhcpSb, Instance);
513
514 //
515 // Install DHCP4 onto ChildHandle
516 //
517 Status = gBS->InstallMultipleProtocolInterfaces (
518 ChildHandle,
519 &gEfiDhcp4ProtocolGuid,
520 &Instance->Dhcp4Protocol,
521 NULL
522 );
523
524 if (EFI_ERROR (Status)) {
525 FreePool (Instance);
526 return Status;
527 }
528
529 Instance->Handle = *ChildHandle;
530
531 //
532 // Open the Udp4 protocol BY_CHILD.
533 //
534 Status = gBS->OpenProtocol (
535 DhcpSb->UdpIo->UdpHandle,
536 &gEfiUdp4ProtocolGuid,
537 (VOID **) &Udp4,
538 gDhcp4DriverBinding.DriverBindingHandle,
539 Instance->Handle,
540 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
541 );
542 if (EFI_ERROR (Status)) {
543 gBS->UninstallMultipleProtocolInterfaces (
544 Instance->Handle,
545 &gEfiDhcp4ProtocolGuid,
546 &Instance->Dhcp4Protocol,
547 NULL
548 );
549
550 FreePool (Instance);
551 return Status;
552 }
553
554 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
555
556 InsertTailList (&DhcpSb->Children, &Instance->Link);
557 DhcpSb->NumChildren++;
558
559 gBS->RestoreTPL (OldTpl);
560
561 return EFI_SUCCESS;
562 }
563
564
565 /**
566 Destroys a child handle with a protocol installed on it.
567
568 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
569 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
570 last protocol on ChildHandle, then ChildHandle is destroyed.
571
572 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
573 @param ChildHandle Handle of the child to destroy
574
575 @retval EFI_SUCCES The protocol was removed from ChildHandle.
576 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
577 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
578 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
579 because its services are being used.
580 @retval other The child handle was not destroyed
581
582 **/
583 EFI_STATUS
584 EFIAPI
585 Dhcp4ServiceBindingDestroyChild (
586 IN EFI_SERVICE_BINDING_PROTOCOL *This,
587 IN EFI_HANDLE ChildHandle
588 )
589 {
590 DHCP_SERVICE *DhcpSb;
591 DHCP_PROTOCOL *Instance;
592 EFI_DHCP4_PROTOCOL *Dhcp;
593 EFI_TPL OldTpl;
594 EFI_STATUS Status;
595
596 if ((This == NULL) || (ChildHandle == NULL)) {
597 return EFI_INVALID_PARAMETER;
598 }
599
600 //
601 // Retrieve the private context data structures
602 //
603 Status = gBS->OpenProtocol (
604 ChildHandle,
605 &gEfiDhcp4ProtocolGuid,
606 (VOID **) &Dhcp,
607 gDhcp4DriverBinding.DriverBindingHandle,
608 ChildHandle,
609 EFI_OPEN_PROTOCOL_GET_PROTOCOL
610 );
611
612 if (EFI_ERROR (Status)) {
613 return EFI_UNSUPPORTED;
614 }
615
616 Instance = DHCP_INSTANCE_FROM_THIS (Dhcp);
617 DhcpSb = DHCP_SERVICE_FROM_THIS (This);
618
619 if (Instance->Service != DhcpSb) {
620 return EFI_INVALID_PARAMETER;
621 }
622
623 //
624 // A child can be destroyed more than once. For example,
625 // Dhcp4DriverBindingStop will destroy all of its children.
626 // when caller driver is being stopped, it will destory the
627 // dhcp child it opens.
628 //
629 if (Instance->InDestory) {
630 return EFI_SUCCESS;
631 }
632
633 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
634 Instance->InDestory = TRUE;
635
636 //
637 // Close the Udp4 protocol.
638 //
639 gBS->CloseProtocol (
640 DhcpSb->UdpIo->UdpHandle,
641 &gEfiUdp4ProtocolGuid,
642 gDhcp4DriverBinding.DriverBindingHandle,
643 ChildHandle
644 );
645
646 //
647 // Uninstall the DHCP4 protocol first to enable a top down destruction.
648 //
649 Status = gBS->UninstallProtocolInterface (
650 ChildHandle,
651 &gEfiDhcp4ProtocolGuid,
652 Dhcp
653 );
654
655 if (EFI_ERROR (Status)) {
656 Instance->InDestory = FALSE;
657
658 gBS->RestoreTPL (OldTpl);
659 return Status;
660 }
661
662 if (DhcpSb->ActiveChild == Instance) {
663 DhcpYieldControl (DhcpSb);
664 }
665
666 RemoveEntryList (&Instance->Link);
667 DhcpSb->NumChildren--;
668
669 gBS->RestoreTPL (OldTpl);
670
671 FreePool (Instance);
672 return EFI_SUCCESS;
673 }