]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/IpSecDxe/IpSecDriver.c
Add NetworkPkg (P.UDK2010.UP3.Network.P1)
[mirror_edk2.git] / NetworkPkg / IpSecDxe / IpSecDriver.c
1 /** @file
2 Driver Binding Protocol for IPsec Driver.
3
4 Copyright (c) 2009 - 2010, 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 <Library/UdpIoLib.h>
17 #include "IpSecConfigImpl.h"
18 #include "IpSecDebug.h"
19
20 /**
21 Test to see if this driver supports ControllerHandle.
22
23 @param[in] This Protocol instance pointer.
24 @param[in] ControllerHandle Handle of device to test.
25 @param[in] RemainingDevicePath Optional parameter used to pick a specific child
26 device to start.
27
28 @retval EFI_SUCCES This driver supports this device.
29 @retval EFI_ALREADY_STARTED This driver is already running on this device.
30 @retval other This driver does not support this device.
31
32 **/
33 EFI_STATUS
34 EFIAPI
35 IpSecDriverBindingSupported (
36 IN EFI_DRIVER_BINDING_PROTOCOL *This,
37 IN EFI_HANDLE ControllerHandle,
38 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
39 )
40 {
41 //
42 //TODO: Add Udp4Protocol and Udp6Protocol testing.
43 //
44 return EFI_UNSUPPORTED;
45 }
46
47 /**
48 Start this driver on ControllerHandle.
49
50 @param[in] This Protocol instance pointer.
51 @param[in] ControllerHandle Handle of device to bind driver to.
52 @param[in] RemainingDevicePath Optional parameter used to pick a specific child
53 device to start.
54
55 @retval EFI_SUCCES This driver is added to ControllerHandle
56 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
57 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.
58 Currently not implemented.
59 @retval other This driver does not support this device
60
61 **/
62 EFI_STATUS
63 EFIAPI
64 IpSecDriverBindingStart (
65 IN EFI_DRIVER_BINDING_PROTOCOL *This,
66 IN EFI_HANDLE ControllerHandle,
67 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
68 )
69 {
70 //
71 //TODO: Add Udp4Io and Udp6Io creation for the IKE.
72 //
73 return EFI_SUCCESS;
74 }
75
76 /**
77 Stop this driver on ControllerHandle.
78
79 @param[in] This Protocol instance pointer.
80 @param[in] ControllerHandle Handle of a device to stop the driver on.
81 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If the number of
82 children is zero, stop the entire bus driver.
83 @param[in] ChildHandleBuffer List of Child Handles to Stop.
84
85 @retval EFI_SUCCES This driver removed ControllerHandle.
86 @retval other This driver was not removed from this device.
87
88 **/
89 EFI_STATUS
90 EFIAPI
91 IpSecDriverBindingStop (
92 IN EFI_DRIVER_BINDING_PROTOCOL *This,
93 IN EFI_HANDLE ControllerHandle,
94 IN UINTN NumberOfChildren,
95 IN EFI_HANDLE *ChildHandleBuffer
96 )
97 {
98 //
99 //TODO: Add UdpIo4 and UdpIo6 destruction when the Udp driver unload or stop.
100 //
101 return EFI_UNSUPPORTED;
102 }
103
104 EFI_DRIVER_BINDING_PROTOCOL gIpSecDriverBinding = {
105 IpSecDriverBindingSupported,
106 IpSecDriverBindingStart,
107 IpSecDriverBindingStop,
108 0xa,
109 NULL,
110 NULL
111 };
112
113 /**
114 This is a callback function when the mIpSecInstance.DisabledEvent is signaled.
115
116 @param[in] Event Event whose notification function is being invoked.
117 @param[in] Context Pointer to the notification function's context.
118
119 **/
120 VOID
121 EFIAPI
122 IpSecCleanupAllSa (
123 IN EFI_EVENT Event,
124 IN VOID *Context
125 )
126 {
127 IPSEC_PRIVATE_DATA *Private;
128 UINT8 Value;
129 EFI_STATUS Status;
130
131 Private = (IPSEC_PRIVATE_DATA *) Context;
132
133 //
134 // Set the Status Variable
135 //
136 Value = IPSEC_STATUS_DISABLED;
137 Status = gRT->SetVariable (
138 IPSECCONFIG_STATUS_NAME,
139 &gEfiIpSecConfigProtocolGuid,
140 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
141 sizeof (Value),
142 &Value
143 );
144 if (!EFI_ERROR (Status)) {
145 Private->IpSec.DisabledFlag = TRUE;
146 }
147
148 }
149
150 /**
151 This is the declaration of an EFI image entry point. This entry point is
152 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers, including
153 both device drivers and bus drivers.
154
155 The entry point for IPsec driver which installs the driver binding,
156 component name protocol, IPsec Config protcolon, and IPsec protocol in
157 its ImageHandle.
158
159 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
160 @param[in] SystemTable A pointer to the EFI System Table.
161
162 @retval EFI_SUCCESS The operation completed successfully.
163 @retval EFI_ALREADY_STARTED The IPsec driver has been already loaded.
164 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
165 @retval Others The operation is failed.
166
167 **/
168 EFI_STATUS
169 EFIAPI
170 IpSecDriverEntryPoint (
171 IN EFI_HANDLE ImageHandle,
172 IN EFI_SYSTEM_TABLE *SystemTable
173 )
174 {
175 EFI_STATUS Status;
176 IPSEC_PRIVATE_DATA *Private;
177 EFI_IPSEC_PROTOCOL *IpSec;
178
179 //
180 // Check whether ipsec protocol has already been installed.
181 //
182 Status = gBS->LocateProtocol (&gEfiIpSecProtocolGuid, NULL, (VOID **) &IpSec);
183
184 if (!EFI_ERROR (Status)) {
185 DEBUG ((DEBUG_WARN, "_ModuleEntryPoint: IpSec has been already loaded\n"));
186 Status = EFI_ALREADY_STARTED;
187 goto ON_EXIT;
188 }
189
190 Status = gBS->LocateProtocol (&gEfiDpcProtocolGuid, NULL, (VOID **) &mDpc);
191
192 if (EFI_ERROR (Status)) {
193 DEBUG ((DEBUG_ERROR, "_ModuleEntryPoint: Failed to locate EfiDpcProtocol\n"));
194 goto ON_EXIT;
195 }
196
197 Private = AllocateZeroPool (sizeof (IPSEC_PRIVATE_DATA));
198
199 if (Private == NULL) {
200 DEBUG ((DEBUG_ERROR, "_ModuleEntryPoint: Failed to allocate private data\n"));
201 Status = EFI_OUT_OF_RESOURCES;
202 goto ON_EXIT;
203 }
204 //
205 // Create disable event to cleanup all sa when ipsec disabled by user.
206 //
207 Status = gBS->CreateEvent (
208 EVT_NOTIFY_SIGNAL,
209 TPL_CALLBACK,
210 IpSecCleanupAllSa,
211 Private,
212 &mIpSecInstance.DisabledEvent
213 );
214 if (EFI_ERROR (Status)) {
215 DEBUG ((DEBUG_ERROR, "_ModuleEntryPoint: Failed to create disable event\n"));
216 goto ON_FREE_PRIVATE;
217 }
218
219 Private->Signature = IPSEC_PRIVATE_DATA_SIGNATURE;
220 Private->ImageHandle = ImageHandle;
221 CopyMem (&Private->IpSec, &mIpSecInstance, sizeof (EFI_IPSEC_PROTOCOL));
222
223 //
224 // Initilize Private's members. Thess members is used for IKE.
225 //
226 InitializeListHead (&Private->Udp4List);
227 InitializeListHead (&Private->Udp6List);
228 InitializeListHead (&Private->Ikev1SessionList);
229 InitializeListHead (&Private->Ikev1EstablishedList);
230 InitializeListHead (&Private->Ikev2SessionList);
231 InitializeListHead (&Private->Ikev2EstablishedList);
232
233 //
234 // Initialize the ipsec config data and restore it from variable.
235 //
236 Status = IpSecConfigInitialize (Private);
237 if (EFI_ERROR (Status)) {
238 DEBUG ((DEBUG_ERROR, "_ModuleEntryPoint: Failed to initialize IpSecConfig\n"));
239 goto ON_CLOSE_EVENT;
240 }
241 //
242 // Install ipsec protocol which is used by ip driver to process ipsec header.
243 //
244 Status = gBS->InstallMultipleProtocolInterfaces (
245 &Private->Handle,
246 &gEfiIpSecProtocolGuid,
247 &Private->IpSec,
248 NULL
249 );
250 if (EFI_ERROR (Status)) {
251 goto ON_UNINSTALL_CONFIG;
252 }
253
254 Status = EfiLibInstallDriverBindingComponentName2 (
255 ImageHandle,
256 SystemTable,
257 &gIpSecDriverBinding,
258 ImageHandle,
259 &gIpSecComponentName,
260 &gIpSecComponentName2
261 );
262 if (EFI_ERROR (Status)) {
263 goto ON_UNINSTALL_CONFIG;
264 }
265
266 return Status;
267
268 ON_UNINSTALL_CONFIG:
269 gBS->UninstallProtocolInterface (
270 Private->Handle,
271 &gEfiIpSecConfigProtocolGuid,
272 &Private->IpSecConfig
273 );
274 ON_CLOSE_EVENT:
275 gBS->CloseEvent (mIpSecInstance.DisabledEvent);
276 mIpSecInstance.DisabledEvent = NULL;
277 ON_FREE_PRIVATE:
278 FreePool (Private);
279 ON_EXIT:
280 return Status;
281 }
282