Ip4Driver.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /** @file
  2. The driver binding and service binding protocol for IP4 driver.
  3. Copyright (c) 2005 - 2019, Intel Corporation. All rights reserved.<BR>
  4. (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "Ip4Impl.h"
  8. EFI_DRIVER_BINDING_PROTOCOL gIp4DriverBinding = {
  9. Ip4DriverBindingSupported,
  10. Ip4DriverBindingStart,
  11. Ip4DriverBindingStop,
  12. 0xa,
  13. NULL,
  14. NULL
  15. };
  16. BOOLEAN mIpSec2Installed = FALSE;
  17. /**
  18. Callback function for IpSec2 Protocol install.
  19. @param[in] Event Event whose notification function is being invoked
  20. @param[in] Context Pointer to the notification function's context
  21. **/
  22. VOID
  23. EFIAPI
  24. IpSec2InstalledCallback (
  25. IN EFI_EVENT Event,
  26. IN VOID *Context
  27. )
  28. {
  29. EFI_STATUS Status;
  30. //
  31. // Test if protocol was even found.
  32. // Notification function will be called at least once.
  33. //
  34. Status = gBS->LocateProtocol (&gEfiIpSec2ProtocolGuid, NULL, (VOID **)&mIpSec);
  35. if ((Status == EFI_SUCCESS) && (mIpSec != NULL)) {
  36. //
  37. // Close the event so it does not get called again.
  38. //
  39. gBS->CloseEvent (Event);
  40. mIpSec2Installed = TRUE;
  41. }
  42. }
  43. /**
  44. This is the declaration of an EFI image entry point. This entry point is
  45. the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
  46. both device drivers and bus drivers.
  47. The entry point for IP4 driver which install the driver
  48. binding and component name protocol on its image.
  49. @param[in] ImageHandle The firmware allocated handle for the UEFI image.
  50. @param[in] SystemTable A pointer to the EFI System Table.
  51. @retval EFI_SUCCESS The operation completed successfully.
  52. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
  53. **/
  54. EFI_STATUS
  55. EFIAPI
  56. Ip4DriverEntryPoint (
  57. IN EFI_HANDLE ImageHandle,
  58. IN EFI_SYSTEM_TABLE *SystemTable
  59. )
  60. {
  61. VOID *Registration;
  62. EfiCreateProtocolNotifyEvent (
  63. &gEfiIpSec2ProtocolGuid,
  64. TPL_CALLBACK,
  65. IpSec2InstalledCallback,
  66. NULL,
  67. &Registration
  68. );
  69. return EfiLibInstallDriverBindingComponentName2 (
  70. ImageHandle,
  71. SystemTable,
  72. &gIp4DriverBinding,
  73. ImageHandle,
  74. &gIp4ComponentName,
  75. &gIp4ComponentName2
  76. );
  77. }
  78. /**
  79. Test to see if this driver supports ControllerHandle. This service
  80. is called by the EFI boot service ConnectController(). In
  81. order to make drivers as small as possible, there are a few calling
  82. restrictions for this service. ConnectController() must
  83. follow these calling restrictions. If any other agent wishes to call
  84. Supported() it must also follow these calling restrictions.
  85. @param[in] This Protocol instance pointer.
  86. @param[in] ControllerHandle Handle of device to test
  87. @param[in] RemainingDevicePath Optional parameter use to pick a specific child
  88. device to start.
  89. @retval EFI_SUCCESS This driver supports this device
  90. @retval EFI_ALREADY_STARTED This driver is already running on this device
  91. @retval other This driver does not support this device
  92. **/
  93. EFI_STATUS
  94. EFIAPI
  95. Ip4DriverBindingSupported (
  96. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  97. IN EFI_HANDLE ControllerHandle,
  98. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  99. )
  100. {
  101. EFI_STATUS Status;
  102. //
  103. // Test for the MNP service binding Protocol
  104. //
  105. Status = gBS->OpenProtocol (
  106. ControllerHandle,
  107. &gEfiManagedNetworkServiceBindingProtocolGuid,
  108. NULL,
  109. This->DriverBindingHandle,
  110. ControllerHandle,
  111. EFI_OPEN_PROTOCOL_TEST_PROTOCOL
  112. );
  113. if (EFI_ERROR (Status)) {
  114. return Status;
  115. }
  116. //
  117. // Test for the Arp service binding Protocol
  118. //
  119. Status = gBS->OpenProtocol (
  120. ControllerHandle,
  121. &gEfiArpServiceBindingProtocolGuid,
  122. NULL,
  123. This->DriverBindingHandle,
  124. ControllerHandle,
  125. EFI_OPEN_PROTOCOL_TEST_PROTOCOL
  126. );
  127. return Status;
  128. }
  129. /**
  130. Clean up a IP4 service binding instance. It will release all
  131. the resource allocated by the instance. The instance may be
  132. partly initialized, or partly destroyed. If a resource is
  133. destroyed, it is marked as that in case the destroy failed and
  134. being called again later.
  135. @param[in] IpSb The IP4 service binding instance to clean up
  136. @retval EFI_SUCCESS The resource used by the instance are cleaned up
  137. @retval other Failed to clean up some of the resources.
  138. **/
  139. EFI_STATUS
  140. Ip4CleanService (
  141. IN IP4_SERVICE *IpSb
  142. );
  143. /**
  144. Create a new IP4 driver service binding private instance.
  145. @param Controller The controller that has MNP service binding
  146. installed
  147. @param ImageHandle The IP4 driver's image handle
  148. @param Service The variable to receive the newly created IP4
  149. service.
  150. @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
  151. @retval EFI_SUCCESS A new IP4 service binding private is created.
  152. @retval other Other error occurs.
  153. **/
  154. EFI_STATUS
  155. Ip4CreateService (
  156. IN EFI_HANDLE Controller,
  157. IN EFI_HANDLE ImageHandle,
  158. OUT IP4_SERVICE **Service
  159. )
  160. {
  161. IP4_SERVICE *IpSb;
  162. EFI_STATUS Status;
  163. ASSERT (Service != NULL);
  164. *Service = NULL;
  165. //
  166. // allocate a service private data then initialize all the filed to
  167. // empty resources, so if any thing goes wrong when allocating
  168. // resources, Ip4CleanService can be called to clean it up.
  169. //
  170. IpSb = AllocateZeroPool (sizeof (IP4_SERVICE));
  171. if (IpSb == NULL) {
  172. return EFI_OUT_OF_RESOURCES;
  173. }
  174. IpSb->Signature = IP4_SERVICE_SIGNATURE;
  175. IpSb->ServiceBinding.CreateChild = Ip4ServiceBindingCreateChild;
  176. IpSb->ServiceBinding.DestroyChild = Ip4ServiceBindingDestroyChild;
  177. IpSb->State = IP4_SERVICE_UNSTARTED;
  178. IpSb->NumChildren = 0;
  179. InitializeListHead (&IpSb->Children);
  180. InitializeListHead (&IpSb->Interfaces);
  181. IpSb->DefaultInterface = NULL;
  182. IpSb->DefaultRouteTable = NULL;
  183. Ip4InitAssembleTable (&IpSb->Assemble);
  184. IpSb->IgmpCtrl.Igmpv1QuerySeen = 0;
  185. InitializeListHead (&IpSb->IgmpCtrl.Groups);
  186. IpSb->Image = ImageHandle;
  187. IpSb->Controller = Controller;
  188. IpSb->MnpChildHandle = NULL;
  189. IpSb->Mnp = NULL;
  190. IpSb->MnpConfigData.ReceivedQueueTimeoutValue = 0;
  191. IpSb->MnpConfigData.TransmitQueueTimeoutValue = 0;
  192. IpSb->MnpConfigData.ProtocolTypeFilter = IP4_ETHER_PROTO;
  193. IpSb->MnpConfigData.EnableUnicastReceive = TRUE;
  194. IpSb->MnpConfigData.EnableMulticastReceive = TRUE;
  195. IpSb->MnpConfigData.EnableBroadcastReceive = TRUE;
  196. IpSb->MnpConfigData.EnablePromiscuousReceive = FALSE;
  197. IpSb->MnpConfigData.FlushQueuesOnReset = TRUE;
  198. IpSb->MnpConfigData.EnableReceiveTimestamps = FALSE;
  199. IpSb->MnpConfigData.DisableBackgroundPolling = FALSE;
  200. ZeroMem (&IpSb->SnpMode, sizeof (EFI_SIMPLE_NETWORK_MODE));
  201. IpSb->Timer = NULL;
  202. IpSb->ReconfigCheckTimer = NULL;
  203. IpSb->ReconfigEvent = NULL;
  204. IpSb->Reconfig = FALSE;
  205. IpSb->MediaPresent = TRUE;
  206. //
  207. // Create various resources. First create the route table, timer
  208. // event, ReconfigEvent and MNP child. IGMP, interface's initialization depend
  209. // on the MNP child.
  210. //
  211. IpSb->DefaultRouteTable = Ip4CreateRouteTable ();
  212. if (IpSb->DefaultRouteTable == NULL) {
  213. Status = EFI_OUT_OF_RESOURCES;
  214. goto ON_ERROR;
  215. }
  216. Status = gBS->CreateEvent (
  217. EVT_NOTIFY_SIGNAL | EVT_TIMER,
  218. TPL_CALLBACK,
  219. Ip4TimerTicking,
  220. IpSb,
  221. &IpSb->Timer
  222. );
  223. if (EFI_ERROR (Status)) {
  224. goto ON_ERROR;
  225. }
  226. Status = gBS->CreateEvent (
  227. EVT_NOTIFY_SIGNAL | EVT_TIMER,
  228. TPL_CALLBACK,
  229. Ip4TimerReconfigChecking,
  230. IpSb,
  231. &IpSb->ReconfigCheckTimer
  232. );
  233. if (EFI_ERROR (Status)) {
  234. goto ON_ERROR;
  235. }
  236. Status = gBS->CreateEvent (
  237. EVT_NOTIFY_SIGNAL,
  238. TPL_NOTIFY,
  239. Ip4AutoReconfigCallBack,
  240. IpSb,
  241. &IpSb->ReconfigEvent
  242. );
  243. if (EFI_ERROR (Status)) {
  244. goto ON_ERROR;
  245. }
  246. Status = NetLibCreateServiceChild (
  247. Controller,
  248. ImageHandle,
  249. &gEfiManagedNetworkServiceBindingProtocolGuid,
  250. &IpSb->MnpChildHandle
  251. );
  252. if (EFI_ERROR (Status)) {
  253. goto ON_ERROR;
  254. }
  255. Status = gBS->OpenProtocol (
  256. IpSb->MnpChildHandle,
  257. &gEfiManagedNetworkProtocolGuid,
  258. (VOID **)&IpSb->Mnp,
  259. ImageHandle,
  260. Controller,
  261. EFI_OPEN_PROTOCOL_BY_DRIVER
  262. );
  263. if (EFI_ERROR (Status)) {
  264. goto ON_ERROR;
  265. }
  266. Status = Ip4ServiceConfigMnp (IpSb, TRUE);
  267. if (EFI_ERROR (Status)) {
  268. goto ON_ERROR;
  269. }
  270. Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &IpSb->SnpMode);
  271. if (EFI_ERROR (Status)) {
  272. goto ON_ERROR;
  273. }
  274. Status = Ip4InitIgmp (IpSb);
  275. if (EFI_ERROR (Status)) {
  276. goto ON_ERROR;
  277. }
  278. IpSb->MacString = NULL;
  279. Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &IpSb->MacString);
  280. if (EFI_ERROR (Status)) {
  281. goto ON_ERROR;
  282. }
  283. IpSb->DefaultInterface = Ip4CreateInterface (IpSb->Mnp, Controller, ImageHandle);
  284. if (IpSb->DefaultInterface == NULL) {
  285. Status = EFI_OUT_OF_RESOURCES;
  286. goto ON_ERROR;
  287. }
  288. InsertHeadList (&IpSb->Interfaces, &IpSb->DefaultInterface->Link);
  289. ZeroMem (&IpSb->Ip4Config2Instance, sizeof (IP4_CONFIG2_INSTANCE));
  290. Status = Ip4Config2InitInstance (&IpSb->Ip4Config2Instance);
  291. if (EFI_ERROR (Status)) {
  292. goto ON_ERROR;
  293. }
  294. IpSb->MaxPacketSize = IpSb->SnpMode.MaxPacketSize - sizeof (IP4_HEAD);
  295. if (NetLibGetVlanId (IpSb->Controller) != 0) {
  296. //
  297. // This is a VLAN device, reduce MTU by VLAN tag length
  298. //
  299. IpSb->MaxPacketSize -= NET_VLAN_TAG_LEN;
  300. }
  301. IpSb->OldMaxPacketSize = IpSb->MaxPacketSize;
  302. *Service = IpSb;
  303. return EFI_SUCCESS;
  304. ON_ERROR:
  305. Ip4CleanService (IpSb);
  306. FreePool (IpSb);
  307. return Status;
  308. }
  309. /**
  310. Clean up a IP4 service binding instance. It will release all
  311. the resource allocated by the instance. The instance may be
  312. partly initialized, or partly destroyed. If a resource is
  313. destroyed, it is marked as that in case the destroy failed and
  314. being called again later.
  315. @param[in] IpSb The IP4 service binding instance to clean up
  316. @retval EFI_SUCCESS The resource used by the instance are cleaned up
  317. @retval other Failed to clean up some of the resources.
  318. **/
  319. EFI_STATUS
  320. Ip4CleanService (
  321. IN IP4_SERVICE *IpSb
  322. )
  323. {
  324. EFI_STATUS Status;
  325. IpSb->State = IP4_SERVICE_DESTROY;
  326. if (IpSb->Timer != NULL) {
  327. gBS->SetTimer (IpSb->Timer, TimerCancel, 0);
  328. gBS->CloseEvent (IpSb->Timer);
  329. IpSb->Timer = NULL;
  330. }
  331. if (IpSb->ReconfigCheckTimer != NULL) {
  332. gBS->SetTimer (IpSb->ReconfigCheckTimer, TimerCancel, 0);
  333. gBS->CloseEvent (IpSb->ReconfigCheckTimer);
  334. IpSb->ReconfigCheckTimer = NULL;
  335. }
  336. if (IpSb->DefaultInterface != NULL) {
  337. Status = Ip4FreeInterface (IpSb->DefaultInterface, NULL);
  338. if (EFI_ERROR (Status)) {
  339. return Status;
  340. }
  341. IpSb->DefaultInterface = NULL;
  342. }
  343. if (IpSb->DefaultRouteTable != NULL) {
  344. Ip4FreeRouteTable (IpSb->DefaultRouteTable);
  345. IpSb->DefaultRouteTable = NULL;
  346. }
  347. Ip4CleanAssembleTable (&IpSb->Assemble);
  348. if (IpSb->MnpChildHandle != NULL) {
  349. if (IpSb->Mnp != NULL) {
  350. gBS->CloseProtocol (
  351. IpSb->MnpChildHandle,
  352. &gEfiManagedNetworkProtocolGuid,
  353. IpSb->Image,
  354. IpSb->Controller
  355. );
  356. IpSb->Mnp = NULL;
  357. }
  358. NetLibDestroyServiceChild (
  359. IpSb->Controller,
  360. IpSb->Image,
  361. &gEfiManagedNetworkServiceBindingProtocolGuid,
  362. IpSb->MnpChildHandle
  363. );
  364. IpSb->MnpChildHandle = NULL;
  365. }
  366. if (IpSb->ReconfigEvent != NULL) {
  367. gBS->CloseEvent (IpSb->ReconfigEvent);
  368. IpSb->ReconfigEvent = NULL;
  369. }
  370. IpSb->Reconfig = FALSE;
  371. if (IpSb->MacString != NULL) {
  372. FreePool (IpSb->MacString);
  373. }
  374. Ip4Config2CleanInstance (&IpSb->Ip4Config2Instance);
  375. return EFI_SUCCESS;
  376. }
  377. /**
  378. Callback function which provided by user to remove one node in NetDestroyLinkList process.
  379. @param[in] Entry The entry to be removed.
  380. @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
  381. @retval EFI_SUCCESS The entry has been removed successfully.
  382. @retval Others Fail to remove the entry.
  383. **/
  384. EFI_STATUS
  385. EFIAPI
  386. Ip4DestroyChildEntryInHandleBuffer (
  387. IN LIST_ENTRY *Entry,
  388. IN VOID *Context
  389. )
  390. {
  391. IP4_PROTOCOL *IpInstance;
  392. EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
  393. UINTN NumberOfChildren;
  394. EFI_HANDLE *ChildHandleBuffer;
  395. if ((Entry == NULL) || (Context == NULL)) {
  396. return EFI_INVALID_PARAMETER;
  397. }
  398. IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, Link, IP4_PROTOCOL_SIGNATURE);
  399. ServiceBinding = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->ServiceBinding;
  400. NumberOfChildren = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->NumberOfChildren;
  401. ChildHandleBuffer = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->ChildHandleBuffer;
  402. if (!NetIsInHandleBuffer (IpInstance->Handle, NumberOfChildren, ChildHandleBuffer)) {
  403. return EFI_SUCCESS;
  404. }
  405. return ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);
  406. }
  407. /**
  408. Start this driver on ControllerHandle. This service is called by the
  409. EFI boot service ConnectController(). In order to make
  410. drivers as small as possible, there are a few calling restrictions for
  411. this service. ConnectController() must follow these
  412. calling restrictions. If any other agent wishes to call Start() it
  413. must also follow these calling restrictions.
  414. @param[in] This Protocol instance pointer.
  415. @param[in] ControllerHandle Handle of device to bind driver to
  416. @param[in] RemainingDevicePath Optional parameter use to pick a specific child
  417. device to start.
  418. @retval EFI_SUCCESS This driver is added to ControllerHandle
  419. @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
  420. @retval other This driver does not support this device
  421. **/
  422. EFI_STATUS
  423. EFIAPI
  424. Ip4DriverBindingStart (
  425. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  426. IN EFI_HANDLE ControllerHandle,
  427. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  428. )
  429. {
  430. EFI_STATUS Status;
  431. IP4_SERVICE *IpSb;
  432. EFI_IP4_CONFIG2_PROTOCOL *Ip4Cfg2;
  433. UINTN Index;
  434. IP4_CONFIG2_DATA_ITEM *DataItem;
  435. IpSb = NULL;
  436. Ip4Cfg2 = NULL;
  437. DataItem = NULL;
  438. //
  439. // Test for the Ip4 service binding protocol
  440. //
  441. Status = gBS->OpenProtocol (
  442. ControllerHandle,
  443. &gEfiIp4ServiceBindingProtocolGuid,
  444. NULL,
  445. This->DriverBindingHandle,
  446. ControllerHandle,
  447. EFI_OPEN_PROTOCOL_TEST_PROTOCOL
  448. );
  449. if (Status == EFI_SUCCESS) {
  450. return EFI_ALREADY_STARTED;
  451. }
  452. Status = Ip4CreateService (ControllerHandle, This->DriverBindingHandle, &IpSb);
  453. if (EFI_ERROR (Status)) {
  454. return Status;
  455. }
  456. ASSERT (IpSb != NULL);
  457. Ip4Cfg2 = &IpSb->Ip4Config2Instance.Ip4Config2;
  458. //
  459. // Install the Ip4ServiceBinding Protocol onto ControllerHandle
  460. //
  461. Status = gBS->InstallMultipleProtocolInterfaces (
  462. &ControllerHandle,
  463. &gEfiIp4ServiceBindingProtocolGuid,
  464. &IpSb->ServiceBinding,
  465. &gEfiIp4Config2ProtocolGuid,
  466. Ip4Cfg2,
  467. NULL
  468. );
  469. if (EFI_ERROR (Status)) {
  470. goto FREE_SERVICE;
  471. }
  472. //
  473. // Read the config data from NV variable again.
  474. // The default data can be changed by other drivers.
  475. //
  476. Status = Ip4Config2ReadConfigData (IpSb->MacString, &IpSb->Ip4Config2Instance);
  477. if (EFI_ERROR (Status)) {
  478. goto UNINSTALL_PROTOCOL;
  479. }
  480. //
  481. // Consume the installed EFI_IP4_CONFIG2_PROTOCOL to set the default data items.
  482. //
  483. for (Index = Ip4Config2DataTypePolicy; Index < Ip4Config2DataTypeMaximum; Index++) {
  484. DataItem = &IpSb->Ip4Config2Instance.DataItem[Index];
  485. if (DataItem->Data.Ptr != NULL) {
  486. Status = Ip4Cfg2->SetData (
  487. Ip4Cfg2,
  488. Index,
  489. DataItem->DataSize,
  490. DataItem->Data.Ptr
  491. );
  492. if (EFI_ERROR (Status)) {
  493. goto UNINSTALL_PROTOCOL;
  494. }
  495. if ((Index == Ip4Config2DataTypePolicy) && (*(DataItem->Data.Policy) == Ip4Config2PolicyDhcp)) {
  496. break;
  497. }
  498. }
  499. }
  500. //
  501. // Ready to go: start the receiving and timer.
  502. // Ip4Config2SetPolicy maybe call Ip4ReceiveFrame() to set the default interface's RecvRequest first after
  503. // Ip4Config2 instance is initialized. So, EFI_ALREADY_STARTED is the allowed return status.
  504. //
  505. Status = Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);
  506. if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
  507. goto UNINSTALL_PROTOCOL;
  508. }
  509. Status = gBS->SetTimer (IpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);
  510. if (EFI_ERROR (Status)) {
  511. goto UNINSTALL_PROTOCOL;
  512. }
  513. Status = gBS->SetTimer (IpSb->ReconfigCheckTimer, TimerPeriodic, 500 * TICKS_PER_MS);
  514. if (EFI_ERROR (Status)) {
  515. goto UNINSTALL_PROTOCOL;
  516. }
  517. //
  518. // Initialize the IP4 ID
  519. //
  520. mIp4Id = (UINT16)NET_RANDOM (NetRandomInitSeed ());
  521. return Status;
  522. UNINSTALL_PROTOCOL:
  523. gBS->UninstallMultipleProtocolInterfaces (
  524. ControllerHandle,
  525. &gEfiIp4ServiceBindingProtocolGuid,
  526. &IpSb->ServiceBinding,
  527. &gEfiIp4Config2ProtocolGuid,
  528. Ip4Cfg2,
  529. NULL
  530. );
  531. FREE_SERVICE:
  532. Ip4CleanService (IpSb);
  533. FreePool (IpSb);
  534. return Status;
  535. }
  536. /**
  537. Stop this driver on ControllerHandle. This service is called by the
  538. EFI boot service DisconnectController(). In order to
  539. make drivers as small as possible, there are a few calling
  540. restrictions for this service. DisconnectController()
  541. must follow these calling restrictions. If any other agent wishes
  542. to call Stop() it must also follow these calling restrictions.
  543. @param[in] This Protocol instance pointer.
  544. @param[in] ControllerHandle Handle of device to stop driver on
  545. @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
  546. of children is zero stop the entire bus driver.
  547. @param[in] ChildHandleBuffer List of Child Handles to Stop.
  548. @retval EFI_SUCCESS This driver is removed ControllerHandle
  549. @retval other This driver was not removed from this device
  550. **/
  551. EFI_STATUS
  552. EFIAPI
  553. Ip4DriverBindingStop (
  554. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  555. IN EFI_HANDLE ControllerHandle,
  556. IN UINTN NumberOfChildren,
  557. IN EFI_HANDLE *ChildHandleBuffer
  558. )
  559. {
  560. EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
  561. IP4_SERVICE *IpSb;
  562. EFI_HANDLE NicHandle;
  563. EFI_STATUS Status;
  564. INTN State;
  565. LIST_ENTRY *List;
  566. IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context;
  567. IP4_INTERFACE *IpIf;
  568. IP4_ROUTE_TABLE *RouteTable;
  569. BOOLEAN IsDhcp4;
  570. IsDhcp4 = FALSE;
  571. NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);
  572. if (NicHandle == NULL) {
  573. NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiArpProtocolGuid);
  574. if (NicHandle == NULL) {
  575. NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp4ProtocolGuid);
  576. if (NicHandle != NULL) {
  577. IsDhcp4 = TRUE;
  578. } else {
  579. return EFI_SUCCESS;
  580. }
  581. }
  582. }
  583. Status = gBS->OpenProtocol (
  584. NicHandle,
  585. &gEfiIp4ServiceBindingProtocolGuid,
  586. (VOID **)&ServiceBinding,
  587. This->DriverBindingHandle,
  588. NicHandle,
  589. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  590. );
  591. if (EFI_ERROR (Status)) {
  592. return EFI_DEVICE_ERROR;
  593. }
  594. IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);
  595. if (IsDhcp4) {
  596. Status = Ip4Config2DestroyDhcp4 (&IpSb->Ip4Config2Instance);
  597. gBS->CloseEvent (IpSb->Ip4Config2Instance.Dhcp4Event);
  598. IpSb->Ip4Config2Instance.Dhcp4Event = NULL;
  599. } else if (NumberOfChildren != 0) {
  600. List = &IpSb->Children;
  601. Context.ServiceBinding = ServiceBinding;
  602. Context.NumberOfChildren = NumberOfChildren;
  603. Context.ChildHandleBuffer = ChildHandleBuffer;
  604. Status = NetDestroyLinkList (
  605. List,
  606. Ip4DestroyChildEntryInHandleBuffer,
  607. &Context,
  608. NULL
  609. );
  610. } else if (IpSb->DefaultInterface->ArpHandle == ControllerHandle) {
  611. //
  612. // The ARP protocol for the default interface is being uninstalled and all
  613. // its IP child handles should have been destroyed before. So, release the
  614. // default interface and route table, create a new one and mark it as not started.
  615. //
  616. Ip4CancelReceive (IpSb->DefaultInterface);
  617. Ip4FreeInterface (IpSb->DefaultInterface, NULL);
  618. Ip4FreeRouteTable (IpSb->DefaultRouteTable);
  619. IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);
  620. if (IpIf == NULL) {
  621. goto ON_ERROR;
  622. }
  623. RouteTable = Ip4CreateRouteTable ();
  624. if (RouteTable == NULL) {
  625. Ip4FreeInterface (IpIf, NULL);
  626. goto ON_ERROR;
  627. }
  628. IpSb->DefaultInterface = IpIf;
  629. InsertHeadList (&IpSb->Interfaces, &IpIf->Link);
  630. IpSb->DefaultRouteTable = RouteTable;
  631. Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);
  632. IpSb->State = IP4_SERVICE_UNSTARTED;
  633. } else if (IsListEmpty (&IpSb->Children)) {
  634. State = IpSb->State;
  635. //
  636. // OK, clean other resources then uninstall the service binding protocol.
  637. //
  638. Status = Ip4CleanService (IpSb);
  639. if (EFI_ERROR (Status)) {
  640. IpSb->State = State;
  641. goto ON_ERROR;
  642. }
  643. gBS->UninstallMultipleProtocolInterfaces (
  644. NicHandle,
  645. &gEfiIp4ServiceBindingProtocolGuid,
  646. ServiceBinding,
  647. &gEfiIp4Config2ProtocolGuid,
  648. &IpSb->Ip4Config2Instance.Ip4Config2,
  649. NULL
  650. );
  651. if (gIp4ControllerNameTable != NULL) {
  652. FreeUnicodeStringTable (gIp4ControllerNameTable);
  653. gIp4ControllerNameTable = NULL;
  654. }
  655. FreePool (IpSb);
  656. }
  657. ON_ERROR:
  658. return Status;
  659. }
  660. /**
  661. Creates a child handle and installs a protocol.
  662. The CreateChild() function installs a protocol on ChildHandle.
  663. If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
  664. If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
  665. @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
  666. @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
  667. then a new handle is created. If it is a pointer to an existing UEFI handle,
  668. then the protocol is added to the existing UEFI handle.
  669. @retval EFI_SUCCESS The protocol was added to ChildHandle.
  670. @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
  671. @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
  672. the child
  673. @retval other The child handle was not created
  674. **/
  675. EFI_STATUS
  676. EFIAPI
  677. Ip4ServiceBindingCreateChild (
  678. IN EFI_SERVICE_BINDING_PROTOCOL *This,
  679. IN OUT EFI_HANDLE *ChildHandle
  680. )
  681. {
  682. IP4_SERVICE *IpSb;
  683. IP4_PROTOCOL *IpInstance;
  684. EFI_TPL OldTpl;
  685. EFI_STATUS Status;
  686. VOID *Mnp;
  687. if ((This == NULL) || (ChildHandle == NULL)) {
  688. return EFI_INVALID_PARAMETER;
  689. }
  690. IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
  691. IpInstance = AllocatePool (sizeof (IP4_PROTOCOL));
  692. if (IpInstance == NULL) {
  693. return EFI_OUT_OF_RESOURCES;
  694. }
  695. Ip4InitProtocol (IpSb, IpInstance);
  696. //
  697. // Install Ip4 onto ChildHandle
  698. //
  699. Status = gBS->InstallMultipleProtocolInterfaces (
  700. ChildHandle,
  701. &gEfiIp4ProtocolGuid,
  702. &IpInstance->Ip4Proto,
  703. NULL
  704. );
  705. if (EFI_ERROR (Status)) {
  706. goto ON_ERROR;
  707. }
  708. IpInstance->Handle = *ChildHandle;
  709. //
  710. // Open the Managed Network protocol BY_CHILD.
  711. //
  712. Status = gBS->OpenProtocol (
  713. IpSb->MnpChildHandle,
  714. &gEfiManagedNetworkProtocolGuid,
  715. (VOID **)&Mnp,
  716. gIp4DriverBinding.DriverBindingHandle,
  717. IpInstance->Handle,
  718. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  719. );
  720. if (EFI_ERROR (Status)) {
  721. gBS->UninstallMultipleProtocolInterfaces (
  722. *ChildHandle,
  723. &gEfiIp4ProtocolGuid,
  724. &IpInstance->Ip4Proto,
  725. NULL
  726. );
  727. goto ON_ERROR;
  728. }
  729. //
  730. // Insert it into the service binding instance.
  731. //
  732. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  733. InsertTailList (&IpSb->Children, &IpInstance->Link);
  734. IpSb->NumChildren++;
  735. gBS->RestoreTPL (OldTpl);
  736. ON_ERROR:
  737. if (EFI_ERROR (Status)) {
  738. Ip4CleanProtocol (IpInstance);
  739. FreePool (IpInstance);
  740. }
  741. return Status;
  742. }
  743. /**
  744. Destroys a child handle with a protocol installed on it.
  745. The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
  746. that was installed by CreateChild() from ChildHandle. If the removed protocol is the
  747. last protocol on ChildHandle, then ChildHandle is destroyed.
  748. @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
  749. @param ChildHandle Handle of the child to destroy
  750. @retval EFI_SUCCESS The protocol was removed from ChildHandle.
  751. @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
  752. @retval EFI_INVALID_PARAMETER Child handle is NULL.
  753. @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
  754. because its services are being used.
  755. @retval other The child handle was not destroyed
  756. **/
  757. EFI_STATUS
  758. EFIAPI
  759. Ip4ServiceBindingDestroyChild (
  760. IN EFI_SERVICE_BINDING_PROTOCOL *This,
  761. IN EFI_HANDLE ChildHandle
  762. )
  763. {
  764. EFI_STATUS Status;
  765. IP4_SERVICE *IpSb;
  766. IP4_PROTOCOL *IpInstance;
  767. EFI_IP4_PROTOCOL *Ip4;
  768. EFI_TPL OldTpl;
  769. if ((This == NULL) || (ChildHandle == NULL)) {
  770. return EFI_INVALID_PARAMETER;
  771. }
  772. //
  773. // Retrieve the private context data structures
  774. //
  775. IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
  776. Status = gBS->OpenProtocol (
  777. ChildHandle,
  778. &gEfiIp4ProtocolGuid,
  779. (VOID **)&Ip4,
  780. gIp4DriverBinding.DriverBindingHandle,
  781. ChildHandle,
  782. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  783. );
  784. if (EFI_ERROR (Status)) {
  785. return EFI_UNSUPPORTED;
  786. }
  787. IpInstance = IP4_INSTANCE_FROM_PROTOCOL (Ip4);
  788. if (IpInstance->Service != IpSb) {
  789. return EFI_INVALID_PARAMETER;
  790. }
  791. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  792. //
  793. // A child can be destroyed more than once. For example,
  794. // Ip4DriverBindingStop will destroy all of its children.
  795. // when UDP driver is being stopped, it will destroy all
  796. // the IP child it opens.
  797. //
  798. if (IpInstance->InDestroy) {
  799. gBS->RestoreTPL (OldTpl);
  800. return EFI_SUCCESS;
  801. }
  802. IpInstance->InDestroy = TRUE;
  803. //
  804. // Close the Managed Network protocol.
  805. //
  806. gBS->CloseProtocol (
  807. IpSb->MnpChildHandle,
  808. &gEfiManagedNetworkProtocolGuid,
  809. gIp4DriverBinding.DriverBindingHandle,
  810. ChildHandle
  811. );
  812. if ((IpInstance->Interface != NULL) && (IpInstance->Interface->Arp != NULL)) {
  813. gBS->CloseProtocol (
  814. IpInstance->Interface->ArpHandle,
  815. &gEfiArpProtocolGuid,
  816. gIp4DriverBinding.DriverBindingHandle,
  817. ChildHandle
  818. );
  819. }
  820. //
  821. // Uninstall the IP4 protocol first. Many thing happens during
  822. // this:
  823. // 1. The consumer of the IP4 protocol will be stopped if it
  824. // opens the protocol BY_DRIVER. For example, if MNP driver is
  825. // stopped, IP driver's stop function will be called, and uninstall
  826. // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This
  827. // makes it possible to create the network stack bottom up, and
  828. // stop it top down.
  829. // 2. the upper layer will recycle the received packet. The recycle
  830. // event's TPL is higher than this function. The recycle events
  831. // will be called back before preceding. If any packets not recycled,
  832. // that means there is a resource leak.
  833. //
  834. gBS->RestoreTPL (OldTpl);
  835. Status = gBS->UninstallProtocolInterface (
  836. ChildHandle,
  837. &gEfiIp4ProtocolGuid,
  838. &IpInstance->Ip4Proto
  839. );
  840. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  841. if (EFI_ERROR (Status)) {
  842. IpInstance->InDestroy = FALSE;
  843. goto ON_ERROR;
  844. }
  845. Status = Ip4CleanProtocol (IpInstance);
  846. if (EFI_ERROR (Status)) {
  847. gBS->InstallMultipleProtocolInterfaces (
  848. &ChildHandle,
  849. &gEfiIp4ProtocolGuid,
  850. Ip4,
  851. NULL
  852. );
  853. goto ON_ERROR;
  854. }
  855. RemoveEntryList (&IpInstance->Link);
  856. IpSb->NumChildren--;
  857. gBS->RestoreTPL (OldTpl);
  858. FreePool (IpInstance);
  859. return EFI_SUCCESS;
  860. ON_ERROR:
  861. gBS->RestoreTPL (OldTpl);
  862. return Status;
  863. }