]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Dhcp4Dxe/Dhcp4Driver.c
1. Fix a bug in Dhcp4 to prevent unexpected broadcast DHCP packets be queued up in...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Dhcp4Dxe / Dhcp4Driver.c
1 /** @file
2
3 Copyright (c) 2006 - 2009, 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 //
314 // Start the receiving
315 //
316 Status = UdpIoRecvDatagram (DhcpSb->UdpIo, DhcpInput, DhcpSb, 0);
317
318 if (EFI_ERROR (Status)) {
319 goto ON_ERROR;
320 }
321 Status = gBS->SetTimer (DhcpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);
322
323 if (EFI_ERROR (Status)) {
324 goto ON_ERROR;
325 }
326
327 //
328 // Install the Dhcp4ServiceBinding Protocol onto ControlerHandle
329 //
330 Status = gBS->InstallMultipleProtocolInterfaces (
331 &ControllerHandle,
332 &gEfiDhcp4ServiceBindingProtocolGuid,
333 &DhcpSb->ServiceBinding,
334 NULL
335 );
336
337 if (EFI_ERROR (Status)) {
338 goto ON_ERROR;
339 }
340
341 return Status;
342
343 ON_ERROR:
344 Dhcp4CloseService (DhcpSb);
345 FreePool (DhcpSb);
346 return Status;
347 }
348
349
350 /**
351 Stop this driver on ControllerHandle. This service is called by the
352 EFI boot service DisconnectController(). In order to
353 make drivers as small as possible, there are a few calling
354 restrictions for this service. DisconnectController()
355 must follow these calling restrictions. If any other agent wishes
356 to call Stop() it must also follow these calling restrictions.
357
358 @param[in] This Protocol instance pointer.
359 @param[in] ControllerHandle Handle of device to stop driver on
360 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
361 children is zero stop the entire bus driver.
362 @param[in] ChildHandleBuffer List of Child Handles to Stop.
363
364 @retval EFI_SUCCESS This driver is removed ControllerHandle
365 @retval other This driver was not removed from this device
366
367 **/
368 EFI_STATUS
369 EFIAPI
370 Dhcp4DriverBindingStop (
371 IN EFI_DRIVER_BINDING_PROTOCOL *This,
372 IN EFI_HANDLE ControllerHandle,
373 IN UINTN NumberOfChildren,
374 IN EFI_HANDLE *ChildHandleBuffer
375 )
376 {
377 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
378 DHCP_SERVICE *DhcpSb;
379 DHCP_PROTOCOL *Instance;
380 EFI_HANDLE NicHandle;
381 EFI_STATUS Status;
382 EFI_TPL OldTpl;
383
384 //
385 // DHCP driver opens UDP child, So, the ControllerHandle is the
386 // UDP child handle. locate the Nic handle first.
387 //
388 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp4ProtocolGuid);
389
390 if (NicHandle == NULL) {
391 return EFI_DEVICE_ERROR;
392 }
393
394 Status = gBS->OpenProtocol (
395 NicHandle,
396 &gEfiDhcp4ServiceBindingProtocolGuid,
397 (VOID **) &ServiceBinding,
398 This->DriverBindingHandle,
399 NicHandle,
400 EFI_OPEN_PROTOCOL_GET_PROTOCOL
401 );
402
403 if (EFI_ERROR (Status)) {
404 return EFI_DEVICE_ERROR;
405 }
406
407 DhcpSb = DHCP_SERVICE_FROM_THIS (ServiceBinding);
408
409 if (DhcpSb->InDestory) {
410 return EFI_SUCCESS;
411 }
412
413 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
414
415 if (NumberOfChildren == 0) {
416
417 DhcpSb->InDestory = TRUE;
418 DhcpSb->ServiceState = DHCP_DESTORY;
419
420 gBS->UninstallProtocolInterface (
421 NicHandle,
422 &gEfiDhcp4ServiceBindingProtocolGuid,
423 ServiceBinding
424 );
425
426 Dhcp4CloseService (DhcpSb);
427
428 FreePool (DhcpSb);
429 } else {
430 //
431 // Don't use NET_LIST_FOR_EACH_SAFE here, Dhcp4ServiceBindingDestoryChild
432 // may cause other child to be deleted.
433 //
434 while (!IsListEmpty (&DhcpSb->Children)) {
435 Instance = NET_LIST_HEAD (&DhcpSb->Children, DHCP_PROTOCOL, Link);
436 ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);
437 }
438
439 if (DhcpSb->NumChildren != 0) {
440 Status = EFI_DEVICE_ERROR;
441 }
442 }
443
444 gBS->RestoreTPL (OldTpl);
445
446 return Status;
447 }
448
449
450 /**
451 Initialize a new DHCP instance.
452
453 @param DhcpSb The dhcp service instance
454 @param Instance The dhcp instance to initialize
455
456 **/
457 VOID
458 DhcpInitProtocol (
459 IN DHCP_SERVICE *DhcpSb,
460 IN OUT DHCP_PROTOCOL *Instance
461 )
462 {
463 Instance->Signature = DHCP_PROTOCOL_SIGNATURE;
464 CopyMem (&Instance->Dhcp4Protocol, &mDhcp4ProtocolTemplate, sizeof (Instance->Dhcp4Protocol));
465 InitializeListHead (&Instance->Link);
466 Instance->Handle = NULL;
467 Instance->Service = DhcpSb;
468 Instance->InDestory = FALSE;
469 Instance->CompletionEvent = NULL;
470 Instance->RenewRebindEvent = NULL;
471 Instance->Token = NULL;
472 Instance->UdpIo = NULL;
473 NetbufQueInit (&Instance->ResponseQueue);
474 }
475
476
477 /**
478 Creates a child handle and installs a protocol.
479
480 The CreateChild() function installs a protocol on ChildHandle.
481 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
482 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
483
484 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
485 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
486 then a new handle is created. If it is a pointer to an existing UEFI handle,
487 then the protocol is added to the existing UEFI handle.
488
489 @retval EFI_SUCCES The protocol was added to ChildHandle.
490 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
491 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
492 the child
493 @retval other The child handle was not created
494
495 **/
496 EFI_STATUS
497 EFIAPI
498 Dhcp4ServiceBindingCreateChild (
499 IN EFI_SERVICE_BINDING_PROTOCOL *This,
500 IN EFI_HANDLE *ChildHandle
501 )
502 {
503 DHCP_SERVICE *DhcpSb;
504 DHCP_PROTOCOL *Instance;
505 EFI_STATUS Status;
506 EFI_TPL OldTpl;
507 VOID *Udp4;
508
509 if ((This == NULL) || (ChildHandle == NULL)) {
510 return EFI_INVALID_PARAMETER;
511 }
512
513 Instance = AllocatePool (sizeof (*Instance));
514
515 if (Instance == NULL) {
516 return EFI_OUT_OF_RESOURCES;
517 }
518
519 DhcpSb = DHCP_SERVICE_FROM_THIS (This);
520 DhcpInitProtocol (DhcpSb, Instance);
521
522 //
523 // Install DHCP4 onto ChildHandle
524 //
525 Status = gBS->InstallMultipleProtocolInterfaces (
526 ChildHandle,
527 &gEfiDhcp4ProtocolGuid,
528 &Instance->Dhcp4Protocol,
529 NULL
530 );
531
532 if (EFI_ERROR (Status)) {
533 FreePool (Instance);
534 return Status;
535 }
536
537 Instance->Handle = *ChildHandle;
538
539 //
540 // Open the Udp4 protocol BY_CHILD.
541 //
542 Status = gBS->OpenProtocol (
543 DhcpSb->UdpIo->UdpHandle,
544 &gEfiUdp4ProtocolGuid,
545 (VOID **) &Udp4,
546 gDhcp4DriverBinding.DriverBindingHandle,
547 Instance->Handle,
548 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
549 );
550 if (EFI_ERROR (Status)) {
551 gBS->UninstallMultipleProtocolInterfaces (
552 Instance->Handle,
553 &gEfiDhcp4ProtocolGuid,
554 &Instance->Dhcp4Protocol,
555 NULL
556 );
557
558 FreePool (Instance);
559 return Status;
560 }
561
562 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
563
564 InsertTailList (&DhcpSb->Children, &Instance->Link);
565 DhcpSb->NumChildren++;
566
567 gBS->RestoreTPL (OldTpl);
568
569 return EFI_SUCCESS;
570 }
571
572
573 /**
574 Destroys a child handle with a protocol installed on it.
575
576 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
577 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
578 last protocol on ChildHandle, then ChildHandle is destroyed.
579
580 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
581 @param ChildHandle Handle of the child to destroy
582
583 @retval EFI_SUCCES The protocol was removed from ChildHandle.
584 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
585 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
586 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
587 because its services are being used.
588 @retval other The child handle was not destroyed
589
590 **/
591 EFI_STATUS
592 EFIAPI
593 Dhcp4ServiceBindingDestroyChild (
594 IN EFI_SERVICE_BINDING_PROTOCOL *This,
595 IN EFI_HANDLE ChildHandle
596 )
597 {
598 DHCP_SERVICE *DhcpSb;
599 DHCP_PROTOCOL *Instance;
600 EFI_DHCP4_PROTOCOL *Dhcp;
601 EFI_TPL OldTpl;
602 EFI_STATUS Status;
603
604 if ((This == NULL) || (ChildHandle == NULL)) {
605 return EFI_INVALID_PARAMETER;
606 }
607
608 //
609 // Retrieve the private context data structures
610 //
611 Status = gBS->OpenProtocol (
612 ChildHandle,
613 &gEfiDhcp4ProtocolGuid,
614 (VOID **) &Dhcp,
615 gDhcp4DriverBinding.DriverBindingHandle,
616 ChildHandle,
617 EFI_OPEN_PROTOCOL_GET_PROTOCOL
618 );
619
620 if (EFI_ERROR (Status)) {
621 return EFI_UNSUPPORTED;
622 }
623
624 Instance = DHCP_INSTANCE_FROM_THIS (Dhcp);
625 DhcpSb = DHCP_SERVICE_FROM_THIS (This);
626
627 if (Instance->Service != DhcpSb) {
628 return EFI_INVALID_PARAMETER;
629 }
630
631 //
632 // A child can be destroyed more than once. For example,
633 // Dhcp4DriverBindingStop will destroy all of its children.
634 // when caller driver is being stopped, it will destory the
635 // dhcp child it opens.
636 //
637 if (Instance->InDestory) {
638 return EFI_SUCCESS;
639 }
640
641 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
642 Instance->InDestory = TRUE;
643
644 //
645 // Close the Udp4 protocol.
646 //
647 gBS->CloseProtocol (
648 DhcpSb->UdpIo->UdpHandle,
649 &gEfiUdp4ProtocolGuid,
650 gDhcp4DriverBinding.DriverBindingHandle,
651 ChildHandle
652 );
653
654 //
655 // Uninstall the DHCP4 protocol first to enable a top down destruction.
656 //
657 Status = gBS->UninstallProtocolInterface (
658 ChildHandle,
659 &gEfiDhcp4ProtocolGuid,
660 Dhcp
661 );
662
663 if (EFI_ERROR (Status)) {
664 Instance->InDestory = FALSE;
665
666 gBS->RestoreTPL (OldTpl);
667 return Status;
668 }
669
670 if (DhcpSb->ActiveChild == Instance) {
671 DhcpYieldControl (DhcpSb);
672 }
673
674 RemoveEntryList (&Instance->Link);
675 DhcpSb->NumChildren--;
676
677 gBS->RestoreTPL (OldTpl);
678
679 FreePool (Instance);
680 return EFI_SUCCESS;
681 }