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