Dhcp4Impl.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  1. /** @file
  2. This file implement the EFI_DHCP4_PROTOCOL interface.
  3. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Dhcp4Impl.h"
  7. /**
  8. Returns the current operating mode and cached data packet for the EFI DHCPv4 Protocol driver.
  9. The GetModeData() function returns the current operating mode and cached data
  10. packet for the EFI DHCPv4 Protocol driver.
  11. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  12. @param[out] Dhcp4ModeData Pointer to storage for the EFI_DHCP4_MODE_DATA structure.
  13. @retval EFI_SUCCESS The mode data was returned.
  14. @retval EFI_INVALID_PARAMETER This is NULL.
  15. **/
  16. EFI_STATUS
  17. EFIAPI
  18. EfiDhcp4GetModeData (
  19. IN EFI_DHCP4_PROTOCOL *This,
  20. OUT EFI_DHCP4_MODE_DATA *Dhcp4ModeData
  21. );
  22. /**
  23. Initializes, changes, or resets the operational settings for the EFI DHCPv4 Protocol driver.
  24. The Configure() function is used to initialize, change, or reset the operational
  25. settings of the EFI DHCPv4 Protocol driver for the communication device on which
  26. the EFI DHCPv4 Service Binding Protocol is installed. This function can be
  27. successfully called only if both of the following are true:
  28. * This instance of the EFI DHCPv4 Protocol driver is in the Dhcp4Stopped, Dhcp4Init,
  29. Dhcp4InitReboot, or Dhcp4Bound states.
  30. * No other EFI DHCPv4 Protocol driver instance that is controlled by this EFI
  31. DHCPv4 Service Binding Protocol driver instance has configured this EFI DHCPv4
  32. Protocol driver.
  33. When this driver is in the Dhcp4Stopped state, it can transfer into one of the
  34. following two possible initial states:
  35. * Dhcp4Init
  36. * Dhcp4InitReboot
  37. The driver can transfer into these states by calling Configure() with a non-NULL
  38. Dhcp4CfgData. The driver will transfer into the appropriate state based on the
  39. supplied client network address in the ClientAddress parameter and DHCP options
  40. in the OptionList parameter as described in RFC 2131.
  41. When Configure() is called successfully while Dhcp4CfgData is set to NULL, the
  42. default configuring data will be reset in the EFI DHCPv4 Protocol driver and
  43. the state of the EFI DHCPv4 Protocol driver will not be changed. If one instance
  44. wants to make it possible for another instance to configure the EFI DHCPv4 Protocol
  45. driver, it must call this function with Dhcp4CfgData set to NULL.
  46. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  47. @param[in] Dhcp4CfgData Pointer to the EFI_DHCP4_CONFIG_DATA.
  48. @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init or
  49. Dhcp4InitReboot state, if the original state of this driver
  50. was Dhcp4Stopped and the value of Dhcp4CfgData was
  51. not NULL. Otherwise, the state was left unchanged.
  52. @retval EFI_ACCESS_DENIED This instance of the EFI DHCPv4 Protocol driver was not in the
  53. Dhcp4Stopped, Dhcp4Init, Dhcp4InitReboot, or Dhcp4Bound state;
  54. Or another instance of this EFI DHCPv4 Protocol driver is already
  55. in a valid configured state.
  56. @retval EFI_INVALID_PARAMETER Some parameter is NULL.
  57. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  58. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  59. **/
  60. EFI_STATUS
  61. EFIAPI
  62. EfiDhcp4Configure (
  63. IN EFI_DHCP4_PROTOCOL *This,
  64. IN EFI_DHCP4_CONFIG_DATA *Dhcp4CfgData OPTIONAL
  65. );
  66. /**
  67. Starts the DHCP configuration process.
  68. The Start() function starts the DHCP configuration process. This function can
  69. be called only when the EFI DHCPv4 Protocol driver is in the Dhcp4Init or
  70. Dhcp4InitReboot state.
  71. If the DHCP process completes successfully, the state of the EFI DHCPv4 Protocol
  72. driver will be transferred through Dhcp4Selecting and Dhcp4Requesting to the
  73. Dhcp4Bound state. The CompletionEvent will then be signaled if it is not NULL.
  74. If the process aborts, either by the user or by some unexpected network error,
  75. the state is restored to the Dhcp4Init state. The Start() function can be called
  76. again to restart the process.
  77. Refer to RFC 2131 for precise state transitions during this process. At the
  78. time when each event occurs in this process, the callback function that was set
  79. by EFI_DHCP4_PROTOCOL.Configure() will be called and the user can take this
  80. opportunity to control the process.
  81. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  82. @param[in] CompletionEvent If not NULL, indicates the event that will be signaled when the
  83. EFI DHCPv4 Protocol driver is transferred into the
  84. Dhcp4Bound state or when the DHCP process is aborted.
  85. EFI_DHCP4_PROTOCOL.GetModeData() can be called to
  86. check the completion status. If NULL,
  87. EFI_DHCP4_PROTOCOL.Start() will wait until the driver
  88. is transferred into the Dhcp4Bound state or the process fails.
  89. @retval EFI_SUCCESS The DHCP configuration process has started, or it has completed
  90. when CompletionEvent is NULL.
  91. @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped
  92. state. EFI_DHCP4_PROTOCOL. Configure() needs to be called.
  93. @retval EFI_INVALID_PARAMETER This is NULL.
  94. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  95. @retval EFI_TIMEOUT The DHCP configuration process failed because no response was
  96. received from the server within the specified timeout value.
  97. @retval EFI_ABORTED The user aborted the DHCP process.
  98. @retval EFI_ALREADY_STARTED Some other EFI DHCPv4 Protocol instance already started the
  99. DHCP process.
  100. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  101. **/
  102. EFI_STATUS
  103. EFIAPI
  104. EfiDhcp4Start (
  105. IN EFI_DHCP4_PROTOCOL *This,
  106. IN EFI_EVENT CompletionEvent OPTIONAL
  107. );
  108. /**
  109. Extends the lease time by sending a request packet.
  110. The RenewRebind() function is used to manually extend the lease time when the
  111. EFI DHCPv4 Protocol driver is in the Dhcp4Bound state and the lease time has
  112. not expired yet. This function will send a request packet to the previously
  113. found server (or to any server when RebindRequest is TRUE) and transfer the
  114. state into the Dhcp4Renewing state (or Dhcp4Rebinding when RebindingRequest is
  115. TRUE). When a response is received, the state is returned to Dhcp4Bound.
  116. If no response is received before the try count is exceeded (the RequestTryCount
  117. field that is specified in EFI_DHCP4_CONFIG_DATA) but before the lease time that
  118. was issued by the previous server expires, the driver will return to the Dhcp4Bound
  119. state and the previous configuration is restored. The outgoing and incoming packets
  120. can be captured by the EFI_DHCP4_CALLBACK function.
  121. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  122. @param[in] RebindRequest If TRUE, this function broadcasts the request packets and enters
  123. the Dhcp4Rebinding state. Otherwise, it sends a unicast
  124. request packet and enters the Dhcp4Renewing state.
  125. @param[in] CompletionEvent If not NULL, this event is signaled when the renew/rebind phase
  126. completes or some error occurs.
  127. EFI_DHCP4_PROTOCOL.GetModeData() can be called to
  128. check the completion status. If NULL,
  129. EFI_DHCP4_PROTOCOL.RenewRebind() will busy-wait
  130. until the DHCP process finishes.
  131. @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the
  132. Dhcp4Renewing state or is back to the Dhcp4Bound state.
  133. @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped
  134. state. EFI_DHCP4_PROTOCOL.Configure() needs to
  135. be called.
  136. @retval EFI_INVALID_PARAMETER This is NULL.
  137. @retval EFI_TIMEOUT There was no response from the server when the try count was
  138. exceeded.
  139. @retval EFI_ACCESS_DENIED The driver is not in the Dhcp4Bound state.
  140. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  141. **/
  142. EFI_STATUS
  143. EFIAPI
  144. EfiDhcp4RenewRebind (
  145. IN EFI_DHCP4_PROTOCOL *This,
  146. IN BOOLEAN RebindRequest,
  147. IN EFI_EVENT CompletionEvent OPTIONAL
  148. );
  149. /**
  150. Releases the current address configuration.
  151. The Release() function releases the current configured IP address by doing either
  152. of the following:
  153. * Sending a DHCPRELEASE packet when the EFI DHCPv4 Protocol driver is in the
  154. Dhcp4Bound state
  155. * Setting the previously assigned IP address that was provided with the
  156. EFI_DHCP4_PROTOCOL.Configure() function to 0.0.0.0 when the driver is in
  157. Dhcp4InitReboot state
  158. After a successful call to this function, the EFI DHCPv4 Protocol driver returns
  159. to the Dhcp4Init state and any subsequent incoming packets will be discarded silently.
  160. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  161. @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init phase.
  162. @retval EFI_INVALID_PARAMETER This is NULL.
  163. @retval EFI_ACCESS_DENIED The EFI DHCPv4 Protocol driver is not Dhcp4InitReboot state.
  164. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  165. **/
  166. EFI_STATUS
  167. EFIAPI
  168. EfiDhcp4Release (
  169. IN EFI_DHCP4_PROTOCOL *This
  170. );
  171. /**
  172. Stops the current address configuration.
  173. The Stop() function is used to stop the DHCP configuration process. After this
  174. function is called successfully, the EFI DHCPv4 Protocol driver is transferred
  175. into the Dhcp4Stopped state. EFI_DHCP4_PROTOCOL.Configure() needs to be called
  176. before DHCP configuration process can be started again. This function can be
  177. called when the EFI DHCPv4 Protocol driver is in any state.
  178. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  179. @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Stopped phase.
  180. @retval EFI_INVALID_PARAMETER This is NULL.
  181. **/
  182. EFI_STATUS
  183. EFIAPI
  184. EfiDhcp4Stop (
  185. IN EFI_DHCP4_PROTOCOL *This
  186. );
  187. /**
  188. Builds a DHCP packet, given the options to be appended or deleted or replaced.
  189. The Build() function is used to assemble a new packet from the original packet
  190. by replacing or deleting existing options or appending new options. This function
  191. does not change any state of the EFI DHCPv4 Protocol driver and can be used at
  192. any time.
  193. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  194. @param[in] SeedPacket Initial packet to be used as a base for building new packet.
  195. @param[in] DeleteCount Number of opcodes in the DeleteList.
  196. @param[in] DeleteList List of opcodes to be deleted from the seed packet.
  197. Ignored if DeleteCount is zero.
  198. @param[in] AppendCount Number of entries in the OptionList.
  199. @param[in] AppendList Pointer to a DHCP option list to be appended to SeedPacket.
  200. If SeedPacket also contains options in this list, they are
  201. replaced by new options (except pad option). Ignored if
  202. AppendCount is zero. Type EFI_DHCP4_PACKET_OPTION
  203. @param[out] NewPacket Pointer to storage for the pointer to the new allocated packet.
  204. Use the EFI Boot Service FreePool() on the resulting pointer
  205. when done with the packet.
  206. @retval EFI_SUCCESS The new packet was built.
  207. @retval EFI_OUT_OF_RESOURCES Storage for the new packet could not be allocated.
  208. @retval EFI_INVALID_PARAMETER Some parameter is NULL.
  209. **/
  210. EFI_STATUS
  211. EFIAPI
  212. EfiDhcp4Build (
  213. IN EFI_DHCP4_PROTOCOL *This,
  214. IN EFI_DHCP4_PACKET *SeedPacket,
  215. IN UINT32 DeleteCount,
  216. IN UINT8 *DeleteList OPTIONAL,
  217. IN UINT32 AppendCount,
  218. IN EFI_DHCP4_PACKET_OPTION *AppendList[] OPTIONAL,
  219. OUT EFI_DHCP4_PACKET **NewPacket
  220. );
  221. /**
  222. Transmits a DHCP formatted packet and optionally waits for responses.
  223. The TransmitReceive() function is used to transmit a DHCP packet and optionally
  224. wait for the response from servers. This function does not change the state of
  225. the EFI DHCPv4 Protocol driver and thus can be used at any time.
  226. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  227. @param[in] Token Pointer to the EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN structure.
  228. @retval EFI_SUCCESS The packet was successfully queued for transmission.
  229. @retval EFI_INVALID_PARAMETER Some parameter is NULL.
  230. @retval EFI_NOT_READY The previous call to this function has not finished yet. Try to call
  231. this function after collection process completes.
  232. @retval EFI_NO_MAPPING The default station address is not available yet.
  233. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  234. @retval Others Some other unexpected error occurred.
  235. **/
  236. EFI_STATUS
  237. EFIAPI
  238. EfiDhcp4TransmitReceive (
  239. IN EFI_DHCP4_PROTOCOL *This,
  240. IN EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN *Token
  241. );
  242. /**
  243. Parses the packed DHCP option data.
  244. The Parse() function is used to retrieve the option list from a DHCP packet.
  245. If *OptionCount isn't zero, and there is enough space for all the DHCP options
  246. in the Packet, each element of PacketOptionList is set to point to somewhere in
  247. the Packet->Dhcp4.Option where a new DHCP option begins. If RFC3396 is supported,
  248. the caller should reassemble the parsed DHCP options to get the finial result.
  249. If *OptionCount is zero or there isn't enough space for all of them, the number
  250. of DHCP options in the Packet is returned in OptionCount.
  251. @param This Pointer to the EFI_DHCP4_PROTOCOL instance.
  252. @param Packet Pointer to packet to be parsed.
  253. @param OptionCount On input, the number of entries in the PacketOptionList.
  254. On output, the number of entries that were written into the
  255. PacketOptionList.
  256. @param PacketOptionList List of packet option entries to be filled in. End option or pad
  257. options are not included.
  258. @retval EFI_SUCCESS The packet was successfully parsed.
  259. @retval EFI_INVALID_PARAMETER Some parameter is NULL.
  260. @retval EFI_BUFFER_TOO_SMALL One or more of the following conditions is TRUE:
  261. 1) *OptionCount is smaller than the number of options that
  262. were found in the Packet.
  263. 2) PacketOptionList is NULL.
  264. **/
  265. EFI_STATUS
  266. EFIAPI
  267. EfiDhcp4Parse (
  268. IN EFI_DHCP4_PROTOCOL *This,
  269. IN EFI_DHCP4_PACKET *Packet,
  270. IN OUT UINT32 *OptionCount,
  271. OUT EFI_DHCP4_PACKET_OPTION *PacketOptionList[] OPTIONAL
  272. );
  273. EFI_DHCP4_PROTOCOL mDhcp4ProtocolTemplate = {
  274. EfiDhcp4GetModeData,
  275. EfiDhcp4Configure,
  276. EfiDhcp4Start,
  277. EfiDhcp4RenewRebind,
  278. EfiDhcp4Release,
  279. EfiDhcp4Stop,
  280. EfiDhcp4Build,
  281. EfiDhcp4TransmitReceive,
  282. EfiDhcp4Parse
  283. };
  284. /**
  285. Returns the current operating mode and cached data packet for the EFI DHCPv4 Protocol driver.
  286. The GetModeData() function returns the current operating mode and cached data
  287. packet for the EFI DHCPv4 Protocol driver.
  288. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  289. @param[out] Dhcp4ModeData Pointer to storage for the EFI_DHCP4_MODE_DATA structure.
  290. @retval EFI_SUCCESS The mode data was returned.
  291. @retval EFI_INVALID_PARAMETER This is NULL.
  292. **/
  293. EFI_STATUS
  294. EFIAPI
  295. EfiDhcp4GetModeData (
  296. IN EFI_DHCP4_PROTOCOL *This,
  297. OUT EFI_DHCP4_MODE_DATA *Dhcp4ModeData
  298. )
  299. {
  300. DHCP_PROTOCOL *Instance;
  301. DHCP_SERVICE *DhcpSb;
  302. DHCP_PARAMETER *Para;
  303. EFI_TPL OldTpl;
  304. IP4_ADDR Ip;
  305. //
  306. // First validate the parameters.
  307. //
  308. if ((This == NULL) || (Dhcp4ModeData == NULL)) {
  309. return EFI_INVALID_PARAMETER;
  310. }
  311. Instance = DHCP_INSTANCE_FROM_THIS (This);
  312. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  313. DhcpSb = Instance->Service;
  314. //
  315. // Caller can use GetModeData to retrieve current DHCP states
  316. // no matter whether it is the active child or not.
  317. //
  318. Dhcp4ModeData->State = (EFI_DHCP4_STATE)DhcpSb->DhcpState;
  319. CopyMem (&Dhcp4ModeData->ConfigData, &DhcpSb->ActiveConfig, sizeof (Dhcp4ModeData->ConfigData));
  320. CopyMem (&Dhcp4ModeData->ClientMacAddress, &DhcpSb->Mac, sizeof (Dhcp4ModeData->ClientMacAddress));
  321. Ip = HTONL (DhcpSb->ClientAddr);
  322. CopyMem (&Dhcp4ModeData->ClientAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));
  323. Ip = HTONL (DhcpSb->Netmask);
  324. CopyMem (&Dhcp4ModeData->SubnetMask, &Ip, sizeof (EFI_IPv4_ADDRESS));
  325. Ip = HTONL (DhcpSb->ServerAddr);
  326. CopyMem (&Dhcp4ModeData->ServerAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));
  327. Para = DhcpSb->Para;
  328. if (Para != NULL) {
  329. Ip = HTONL (Para->Router);
  330. CopyMem (&Dhcp4ModeData->RouterAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));
  331. Dhcp4ModeData->LeaseTime = Para->Lease;
  332. } else {
  333. ZeroMem (&Dhcp4ModeData->RouterAddress, sizeof (EFI_IPv4_ADDRESS));
  334. Dhcp4ModeData->LeaseTime = 0xffffffff;
  335. }
  336. Dhcp4ModeData->ReplyPacket = DhcpSb->Selected;
  337. gBS->RestoreTPL (OldTpl);
  338. return EFI_SUCCESS;
  339. }
  340. /**
  341. Free the resource related to the configure parameters.
  342. DHCP driver will make a copy of the user's configure
  343. such as the time out value.
  344. @param Config The DHCP configure data
  345. **/
  346. VOID
  347. DhcpCleanConfigure (
  348. IN OUT EFI_DHCP4_CONFIG_DATA *Config
  349. )
  350. {
  351. UINT32 Index;
  352. if (Config->DiscoverTimeout != NULL) {
  353. FreePool (Config->DiscoverTimeout);
  354. }
  355. if (Config->RequestTimeout != NULL) {
  356. FreePool (Config->RequestTimeout);
  357. }
  358. if (Config->OptionList != NULL) {
  359. for (Index = 0; Index < Config->OptionCount; Index++) {
  360. if (Config->OptionList[Index] != NULL) {
  361. FreePool (Config->OptionList[Index]);
  362. }
  363. }
  364. FreePool (Config->OptionList);
  365. }
  366. ZeroMem (Config, sizeof (EFI_DHCP4_CONFIG_DATA));
  367. }
  368. /**
  369. Allocate memory for configure parameter such as timeout value for Dst,
  370. then copy the configure parameter from Src to Dst.
  371. @param[out] Dst The destination DHCP configure data.
  372. @param[in] Src The source DHCP configure data.
  373. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
  374. @retval EFI_SUCCESS The configure is copied.
  375. **/
  376. EFI_STATUS
  377. DhcpCopyConfigure (
  378. OUT EFI_DHCP4_CONFIG_DATA *Dst,
  379. IN EFI_DHCP4_CONFIG_DATA *Src
  380. )
  381. {
  382. EFI_DHCP4_PACKET_OPTION **DstOptions;
  383. EFI_DHCP4_PACKET_OPTION **SrcOptions;
  384. UINTN Len;
  385. UINT32 Index;
  386. CopyMem (Dst, Src, sizeof (*Dst));
  387. Dst->DiscoverTimeout = NULL;
  388. Dst->RequestTimeout = NULL;
  389. Dst->OptionList = NULL;
  390. //
  391. // Allocate a memory then copy DiscoverTimeout to it
  392. //
  393. if (Src->DiscoverTimeout != NULL) {
  394. Len = Src->DiscoverTryCount * sizeof (UINT32);
  395. Dst->DiscoverTimeout = AllocatePool (Len);
  396. if (Dst->DiscoverTimeout == NULL) {
  397. return EFI_OUT_OF_RESOURCES;
  398. }
  399. for (Index = 0; Index < Src->DiscoverTryCount; Index++) {
  400. Dst->DiscoverTimeout[Index] = MAX (Src->DiscoverTimeout[Index], 1);
  401. }
  402. }
  403. //
  404. // Allocate a memory then copy RequestTimeout to it
  405. //
  406. if (Src->RequestTimeout != NULL) {
  407. Len = Src->RequestTryCount * sizeof (UINT32);
  408. Dst->RequestTimeout = AllocatePool (Len);
  409. if (Dst->RequestTimeout == NULL) {
  410. goto ON_ERROR;
  411. }
  412. for (Index = 0; Index < Src->RequestTryCount; Index++) {
  413. Dst->RequestTimeout[Index] = MAX (Src->RequestTimeout[Index], 1);
  414. }
  415. }
  416. //
  417. // Allocate an array of dhcp option point, then allocate memory
  418. // for each option and copy the source option to it
  419. //
  420. if (Src->OptionList != NULL) {
  421. Len = Src->OptionCount * sizeof (EFI_DHCP4_PACKET_OPTION *);
  422. Dst->OptionList = AllocateZeroPool (Len);
  423. if (Dst->OptionList == NULL) {
  424. goto ON_ERROR;
  425. }
  426. DstOptions = Dst->OptionList;
  427. SrcOptions = Src->OptionList;
  428. for (Index = 0; Index < Src->OptionCount; Index++) {
  429. Len = sizeof (EFI_DHCP4_PACKET_OPTION) + MAX (SrcOptions[Index]->Length - 1, 0);
  430. DstOptions[Index] = AllocatePool (Len);
  431. if (DstOptions[Index] == NULL) {
  432. goto ON_ERROR;
  433. }
  434. CopyMem (DstOptions[Index], SrcOptions[Index], Len);
  435. }
  436. }
  437. return EFI_SUCCESS;
  438. ON_ERROR:
  439. DhcpCleanConfigure (Dst);
  440. return EFI_OUT_OF_RESOURCES;
  441. }
  442. /**
  443. Give up the control of the DHCP service to let other child
  444. resume. Don't change the service's DHCP state and the Client
  445. address and option list configure as required by RFC2131.
  446. @param DhcpSb The DHCP service instance.
  447. **/
  448. VOID
  449. DhcpYieldControl (
  450. IN DHCP_SERVICE *DhcpSb
  451. )
  452. {
  453. EFI_DHCP4_CONFIG_DATA *Config;
  454. Config = &DhcpSb->ActiveConfig;
  455. DhcpSb->ServiceState = DHCP_UNCONFIGED;
  456. DhcpSb->ActiveChild = NULL;
  457. if (Config->DiscoverTimeout != NULL) {
  458. FreePool (Config->DiscoverTimeout);
  459. Config->DiscoverTryCount = 0;
  460. Config->DiscoverTimeout = NULL;
  461. }
  462. if (Config->RequestTimeout != NULL) {
  463. FreePool (Config->RequestTimeout);
  464. Config->RequestTryCount = 0;
  465. Config->RequestTimeout = NULL;
  466. }
  467. Config->Dhcp4Callback = NULL;
  468. Config->CallbackContext = NULL;
  469. }
  470. /**
  471. Initializes, changes, or resets the operational settings for the EFI DHCPv4 Protocol driver.
  472. The Configure() function is used to initialize, change, or reset the operational
  473. settings of the EFI DHCPv4 Protocol driver for the communication device on which
  474. the EFI DHCPv4 Service Binding Protocol is installed. This function can be
  475. successfully called only if both of the following are true:
  476. * This instance of the EFI DHCPv4 Protocol driver is in the Dhcp4Stopped, Dhcp4Init,
  477. Dhcp4InitReboot, or Dhcp4Bound states.
  478. * No other EFI DHCPv4 Protocol driver instance that is controlled by this EFI
  479. DHCPv4 Service Binding Protocol driver instance has configured this EFI DHCPv4
  480. Protocol driver.
  481. When this driver is in the Dhcp4Stopped state, it can transfer into one of the
  482. following two possible initial states:
  483. * Dhcp4Init
  484. * Dhcp4InitReboot
  485. The driver can transfer into these states by calling Configure() with a non-NULL
  486. Dhcp4CfgData. The driver will transfer into the appropriate state based on the
  487. supplied client network address in the ClientAddress parameter and DHCP options
  488. in the OptionList parameter as described in RFC 2131.
  489. When Configure() is called successfully while Dhcp4CfgData is set to NULL, the
  490. default configuring data will be reset in the EFI DHCPv4 Protocol driver and
  491. the state of the EFI DHCPv4 Protocol driver will not be changed. If one instance
  492. wants to make it possible for another instance to configure the EFI DHCPv4 Protocol
  493. driver, it must call this function with Dhcp4CfgData set to NULL.
  494. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  495. @param[in] Dhcp4CfgData Pointer to the EFI_DHCP4_CONFIG_DATA.
  496. @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init or
  497. Dhcp4InitReboot state, if the original state of this driver
  498. was Dhcp4Stopped and the value of Dhcp4CfgData was
  499. not NULL. Otherwise, the state was left unchanged.
  500. @retval EFI_ACCESS_DENIED This instance of the EFI DHCPv4 Protocol driver was not in the
  501. Dhcp4Stopped, Dhcp4Init, Dhcp4InitReboot, or Dhcp4Bound state;
  502. Or another instance of this EFI DHCPv4 Protocol driver is already
  503. in a valid configured state.
  504. @retval EFI_INVALID_PARAMETER Some parameter is NULL.
  505. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  506. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  507. **/
  508. EFI_STATUS
  509. EFIAPI
  510. EfiDhcp4Configure (
  511. IN EFI_DHCP4_PROTOCOL *This,
  512. IN EFI_DHCP4_CONFIG_DATA *Dhcp4CfgData OPTIONAL
  513. )
  514. {
  515. EFI_DHCP4_CONFIG_DATA *Config;
  516. DHCP_PROTOCOL *Instance;
  517. DHCP_SERVICE *DhcpSb;
  518. EFI_STATUS Status;
  519. EFI_TPL OldTpl;
  520. UINT32 Index;
  521. IP4_ADDR Ip;
  522. //
  523. // First validate the parameters
  524. //
  525. if (This == NULL) {
  526. return EFI_INVALID_PARAMETER;
  527. }
  528. if (Dhcp4CfgData != NULL) {
  529. if ((Dhcp4CfgData->DiscoverTryCount != 0) && (Dhcp4CfgData->DiscoverTimeout == NULL)) {
  530. return EFI_INVALID_PARAMETER;
  531. }
  532. if ((Dhcp4CfgData->RequestTryCount != 0) && (Dhcp4CfgData->RequestTimeout == NULL)) {
  533. return EFI_INVALID_PARAMETER;
  534. }
  535. if ((Dhcp4CfgData->OptionCount != 0) && (Dhcp4CfgData->OptionList == NULL)) {
  536. return EFI_INVALID_PARAMETER;
  537. }
  538. CopyMem (&Ip, &Dhcp4CfgData->ClientAddress, sizeof (IP4_ADDR));
  539. if (IP4_IS_LOCAL_BROADCAST (NTOHL (Ip))) {
  540. return EFI_INVALID_PARAMETER;
  541. }
  542. }
  543. Instance = DHCP_INSTANCE_FROM_THIS (This);
  544. if (Instance->Signature != DHCP_PROTOCOL_SIGNATURE) {
  545. return EFI_INVALID_PARAMETER;
  546. }
  547. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  548. DhcpSb = Instance->Service;
  549. Config = &DhcpSb->ActiveConfig;
  550. Status = EFI_ACCESS_DENIED;
  551. if ((DhcpSb->DhcpState != Dhcp4Stopped) &&
  552. (DhcpSb->DhcpState != Dhcp4Init) &&
  553. (DhcpSb->DhcpState != Dhcp4InitReboot) &&
  554. (DhcpSb->DhcpState != Dhcp4Bound))
  555. {
  556. goto ON_EXIT;
  557. }
  558. if ((DhcpSb->ActiveChild != NULL) && (DhcpSb->ActiveChild != Instance)) {
  559. goto ON_EXIT;
  560. }
  561. if (Dhcp4CfgData != NULL) {
  562. Status = EFI_OUT_OF_RESOURCES;
  563. DhcpCleanConfigure (Config);
  564. if (EFI_ERROR (DhcpCopyConfigure (Config, Dhcp4CfgData))) {
  565. goto ON_EXIT;
  566. }
  567. DhcpSb->UserOptionLen = 0;
  568. for (Index = 0; Index < Dhcp4CfgData->OptionCount; Index++) {
  569. DhcpSb->UserOptionLen += Dhcp4CfgData->OptionList[Index]->Length + 2;
  570. }
  571. DhcpSb->ActiveChild = Instance;
  572. if (DhcpSb->DhcpState == Dhcp4Stopped) {
  573. DhcpSb->ClientAddr = EFI_NTOHL (Dhcp4CfgData->ClientAddress);
  574. if (DhcpSb->ClientAddr != 0) {
  575. DhcpSb->DhcpState = Dhcp4InitReboot;
  576. } else {
  577. DhcpSb->DhcpState = Dhcp4Init;
  578. }
  579. }
  580. DhcpSb->ServiceState = DHCP_CONFIGED;
  581. Status = EFI_SUCCESS;
  582. } else if (DhcpSb->ActiveChild == Instance) {
  583. Status = EFI_SUCCESS;
  584. DhcpYieldControl (DhcpSb);
  585. }
  586. ON_EXIT:
  587. gBS->RestoreTPL (OldTpl);
  588. return Status;
  589. }
  590. /**
  591. Starts the DHCP configuration process.
  592. The Start() function starts the DHCP configuration process. This function can
  593. be called only when the EFI DHCPv4 Protocol driver is in the Dhcp4Init or
  594. Dhcp4InitReboot state.
  595. If the DHCP process completes successfully, the state of the EFI DHCPv4 Protocol
  596. driver will be transferred through Dhcp4Selecting and Dhcp4Requesting to the
  597. Dhcp4Bound state. The CompletionEvent will then be signaled if it is not NULL.
  598. If the process aborts, either by the user or by some unexpected network error,
  599. the state is restored to the Dhcp4Init state. The Start() function can be called
  600. again to restart the process.
  601. Refer to RFC 2131 for precise state transitions during this process. At the
  602. time when each event occurs in this process, the callback function that was set
  603. by EFI_DHCP4_PROTOCOL.Configure() will be called and the user can take this
  604. opportunity to control the process.
  605. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  606. @param[in] CompletionEvent If not NULL, indicates the event that will be signaled when the
  607. EFI DHCPv4 Protocol driver is transferred into the
  608. Dhcp4Bound state or when the DHCP process is aborted.
  609. EFI_DHCP4_PROTOCOL.GetModeData() can be called to
  610. check the completion status. If NULL,
  611. EFI_DHCP4_PROTOCOL.Start() will wait until the driver
  612. is transferred into the Dhcp4Bound state or the process fails.
  613. @retval EFI_SUCCESS The DHCP configuration process has started, or it has completed
  614. when CompletionEvent is NULL.
  615. @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped
  616. state. EFI_DHCP4_PROTOCOL. Configure() needs to be called.
  617. @retval EFI_INVALID_PARAMETER This is NULL.
  618. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  619. @retval EFI_TIMEOUT The DHCP configuration process failed because no response was
  620. received from the server within the specified timeout value.
  621. @retval EFI_ABORTED The user aborted the DHCP process.
  622. @retval EFI_ALREADY_STARTED Some other EFI DHCPv4 Protocol instance already started the
  623. DHCP process.
  624. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  625. @retval EFI_NO_MEDIA There was a media error.
  626. **/
  627. EFI_STATUS
  628. EFIAPI
  629. EfiDhcp4Start (
  630. IN EFI_DHCP4_PROTOCOL *This,
  631. IN EFI_EVENT CompletionEvent OPTIONAL
  632. )
  633. {
  634. DHCP_PROTOCOL *Instance;
  635. DHCP_SERVICE *DhcpSb;
  636. EFI_STATUS Status;
  637. EFI_TPL OldTpl;
  638. EFI_STATUS MediaStatus;
  639. //
  640. // First validate the parameters
  641. //
  642. if (This == NULL) {
  643. return EFI_INVALID_PARAMETER;
  644. }
  645. Instance = DHCP_INSTANCE_FROM_THIS (This);
  646. if (Instance->Signature != DHCP_PROTOCOL_SIGNATURE) {
  647. return EFI_INVALID_PARAMETER;
  648. }
  649. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  650. DhcpSb = Instance->Service;
  651. if (DhcpSb->DhcpState == Dhcp4Stopped) {
  652. Status = EFI_NOT_STARTED;
  653. goto ON_ERROR;
  654. }
  655. if ((DhcpSb->DhcpState != Dhcp4Init) && (DhcpSb->DhcpState != Dhcp4InitReboot)) {
  656. Status = EFI_ALREADY_STARTED;
  657. goto ON_ERROR;
  658. }
  659. //
  660. // Check Media Status.
  661. //
  662. MediaStatus = EFI_SUCCESS;
  663. NetLibDetectMediaWaitTimeout (DhcpSb->Controller, DHCP_CHECK_MEDIA_WAITING_TIME, &MediaStatus);
  664. if (MediaStatus != EFI_SUCCESS) {
  665. Status = EFI_NO_MEDIA;
  666. goto ON_ERROR;
  667. }
  668. DhcpSb->IoStatus = EFI_ALREADY_STARTED;
  669. if (EFI_ERROR (Status = DhcpInitRequest (DhcpSb))) {
  670. goto ON_ERROR;
  671. }
  672. Instance->CompletionEvent = CompletionEvent;
  673. //
  674. // Restore the TPL now, don't call poll function at TPL_CALLBACK.
  675. //
  676. gBS->RestoreTPL (OldTpl);
  677. if (CompletionEvent == NULL) {
  678. while (DhcpSb->IoStatus == EFI_ALREADY_STARTED) {
  679. DhcpSb->UdpIo->Protocol.Udp4->Poll (DhcpSb->UdpIo->Protocol.Udp4);
  680. }
  681. return DhcpSb->IoStatus;
  682. }
  683. return EFI_SUCCESS;
  684. ON_ERROR:
  685. gBS->RestoreTPL (OldTpl);
  686. return Status;
  687. }
  688. /**
  689. Extends the lease time by sending a request packet.
  690. The RenewRebind() function is used to manually extend the lease time when the
  691. EFI DHCPv4 Protocol driver is in the Dhcp4Bound state and the lease time has
  692. not expired yet. This function will send a request packet to the previously
  693. found server (or to any server when RebindRequest is TRUE) and transfer the
  694. state into the Dhcp4Renewing state (or Dhcp4Rebinding when RebindingRequest is
  695. TRUE). When a response is received, the state is returned to Dhcp4Bound.
  696. If no response is received before the try count is exceeded (the RequestTryCount
  697. field that is specified in EFI_DHCP4_CONFIG_DATA) but before the lease time that
  698. was issued by the previous server expires, the driver will return to the Dhcp4Bound
  699. state and the previous configuration is restored. The outgoing and incoming packets
  700. can be captured by the EFI_DHCP4_CALLBACK function.
  701. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  702. @param[in] RebindRequest If TRUE, this function broadcasts the request packets and enters
  703. the Dhcp4Rebinding state. Otherwise, it sends a unicast
  704. request packet and enters the Dhcp4Renewing state.
  705. @param[in] CompletionEvent If not NULL, this event is signaled when the renew/rebind phase
  706. completes or some error occurs.
  707. EFI_DHCP4_PROTOCOL.GetModeData() can be called to
  708. check the completion status. If NULL,
  709. EFI_DHCP4_PROTOCOL.RenewRebind() will busy-wait
  710. until the DHCP process finishes.
  711. @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the
  712. Dhcp4Renewing state or is back to the Dhcp4Bound state.
  713. @retval EFI_NOT_STARTED The EFI DHCPv4 Protocol driver is in the Dhcp4Stopped
  714. state. EFI_DHCP4_PROTOCOL.Configure() needs to
  715. be called.
  716. @retval EFI_INVALID_PARAMETER This is NULL.
  717. @retval EFI_TIMEOUT There was no response from the server when the try count was
  718. exceeded.
  719. @retval EFI_ACCESS_DENIED The driver is not in the Dhcp4Bound state.
  720. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  721. **/
  722. EFI_STATUS
  723. EFIAPI
  724. EfiDhcp4RenewRebind (
  725. IN EFI_DHCP4_PROTOCOL *This,
  726. IN BOOLEAN RebindRequest,
  727. IN EFI_EVENT CompletionEvent OPTIONAL
  728. )
  729. {
  730. DHCP_PROTOCOL *Instance;
  731. DHCP_SERVICE *DhcpSb;
  732. EFI_STATUS Status;
  733. EFI_TPL OldTpl;
  734. //
  735. // First validate the parameters
  736. //
  737. if (This == NULL) {
  738. return EFI_INVALID_PARAMETER;
  739. }
  740. Instance = DHCP_INSTANCE_FROM_THIS (This);
  741. if (Instance->Signature != DHCP_PROTOCOL_SIGNATURE) {
  742. return EFI_INVALID_PARAMETER;
  743. }
  744. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  745. DhcpSb = Instance->Service;
  746. if (DhcpSb->DhcpState == Dhcp4Stopped) {
  747. Status = EFI_NOT_STARTED;
  748. goto ON_EXIT;
  749. }
  750. if (DhcpSb->DhcpState != Dhcp4Bound) {
  751. Status = EFI_ACCESS_DENIED;
  752. goto ON_EXIT;
  753. }
  754. if (DHCP_IS_BOOTP (DhcpSb->Para)) {
  755. Status = EFI_SUCCESS;
  756. goto ON_EXIT;
  757. }
  758. //
  759. // Transit the states then send a extra DHCP request
  760. //
  761. if (!RebindRequest) {
  762. DhcpSetState (DhcpSb, Dhcp4Renewing, FALSE);
  763. } else {
  764. DhcpSetState (DhcpSb, Dhcp4Rebinding, FALSE);
  765. }
  766. //
  767. // Clear initial time to make sure that elapsed-time
  768. // is set to 0 for first REQUEST in renewal process.
  769. //
  770. Instance->ElaspedTime = 0;
  771. Status = DhcpSendMessage (
  772. DhcpSb,
  773. DhcpSb->Selected,
  774. DhcpSb->Para,
  775. DHCP_MSG_REQUEST,
  776. (UINT8 *)"Extra renew/rebind by the application"
  777. );
  778. if (EFI_ERROR (Status)) {
  779. DhcpSetState (DhcpSb, Dhcp4Bound, FALSE);
  780. goto ON_EXIT;
  781. }
  782. DhcpSb->ExtraRefresh = TRUE;
  783. DhcpSb->IoStatus = EFI_ALREADY_STARTED;
  784. Instance->RenewRebindEvent = CompletionEvent;
  785. gBS->RestoreTPL (OldTpl);
  786. if (CompletionEvent == NULL) {
  787. while (DhcpSb->IoStatus == EFI_ALREADY_STARTED) {
  788. DhcpSb->UdpIo->Protocol.Udp4->Poll (DhcpSb->UdpIo->Protocol.Udp4);
  789. }
  790. return DhcpSb->IoStatus;
  791. }
  792. return EFI_SUCCESS;
  793. ON_EXIT:
  794. gBS->RestoreTPL (OldTpl);
  795. return Status;
  796. }
  797. /**
  798. Releases the current address configuration.
  799. The Release() function releases the current configured IP address by doing either
  800. of the following:
  801. * Sending a DHCPRELEASE packet when the EFI DHCPv4 Protocol driver is in the
  802. Dhcp4Bound state
  803. * Setting the previously assigned IP address that was provided with the
  804. EFI_DHCP4_PROTOCOL.Configure() function to 0.0.0.0 when the driver is in
  805. Dhcp4InitReboot state
  806. After a successful call to this function, the EFI DHCPv4 Protocol driver returns
  807. to the Dhcp4Init state and any subsequent incoming packets will be discarded silently.
  808. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  809. @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Init phase.
  810. @retval EFI_INVALID_PARAMETER This is NULL.
  811. @retval EFI_ACCESS_DENIED The EFI DHCPv4 Protocol driver is not Dhcp4InitReboot state.
  812. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  813. **/
  814. EFI_STATUS
  815. EFIAPI
  816. EfiDhcp4Release (
  817. IN EFI_DHCP4_PROTOCOL *This
  818. )
  819. {
  820. DHCP_PROTOCOL *Instance;
  821. DHCP_SERVICE *DhcpSb;
  822. EFI_STATUS Status;
  823. EFI_TPL OldTpl;
  824. //
  825. // First validate the parameters
  826. //
  827. if (This == NULL) {
  828. return EFI_INVALID_PARAMETER;
  829. }
  830. Instance = DHCP_INSTANCE_FROM_THIS (This);
  831. if (Instance->Signature != DHCP_PROTOCOL_SIGNATURE) {
  832. return EFI_INVALID_PARAMETER;
  833. }
  834. Status = EFI_SUCCESS;
  835. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  836. DhcpSb = Instance->Service;
  837. if ((DhcpSb->DhcpState != Dhcp4InitReboot) && (DhcpSb->DhcpState != Dhcp4Bound)) {
  838. Status = EFI_ACCESS_DENIED;
  839. goto ON_EXIT;
  840. }
  841. if (!DHCP_IS_BOOTP (DhcpSb->Para) && (DhcpSb->DhcpState == Dhcp4Bound)) {
  842. Status = DhcpSendMessage (
  843. DhcpSb,
  844. DhcpSb->Selected,
  845. DhcpSb->Para,
  846. DHCP_MSG_RELEASE,
  847. NULL
  848. );
  849. if (EFI_ERROR (Status)) {
  850. Status = EFI_DEVICE_ERROR;
  851. goto ON_EXIT;
  852. }
  853. }
  854. DhcpCleanLease (DhcpSb);
  855. ON_EXIT:
  856. gBS->RestoreTPL (OldTpl);
  857. return Status;
  858. }
  859. /**
  860. Stops the current address configuration.
  861. The Stop() function is used to stop the DHCP configuration process. After this
  862. function is called successfully, the EFI DHCPv4 Protocol driver is transferred
  863. into the Dhcp4Stopped state. EFI_DHCP4_PROTOCOL.Configure() needs to be called
  864. before DHCP configuration process can be started again. This function can be
  865. called when the EFI DHCPv4 Protocol driver is in any state.
  866. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  867. @retval EFI_SUCCESS The EFI DHCPv4 Protocol driver is now in the Dhcp4Stopped phase.
  868. @retval EFI_INVALID_PARAMETER This is NULL.
  869. **/
  870. EFI_STATUS
  871. EFIAPI
  872. EfiDhcp4Stop (
  873. IN EFI_DHCP4_PROTOCOL *This
  874. )
  875. {
  876. DHCP_PROTOCOL *Instance;
  877. DHCP_SERVICE *DhcpSb;
  878. EFI_TPL OldTpl;
  879. //
  880. // First validate the parameters
  881. //
  882. if (This == NULL) {
  883. return EFI_INVALID_PARAMETER;
  884. }
  885. Instance = DHCP_INSTANCE_FROM_THIS (This);
  886. if (Instance->Signature != DHCP_PROTOCOL_SIGNATURE) {
  887. return EFI_INVALID_PARAMETER;
  888. }
  889. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  890. DhcpSb = Instance->Service;
  891. DhcpCleanLease (DhcpSb);
  892. DhcpSb->DhcpState = Dhcp4Stopped;
  893. DhcpSb->ServiceState = DHCP_UNCONFIGED;
  894. gBS->RestoreTPL (OldTpl);
  895. return EFI_SUCCESS;
  896. }
  897. /**
  898. Builds a DHCP packet, given the options to be appended or deleted or replaced.
  899. The Build() function is used to assemble a new packet from the original packet
  900. by replacing or deleting existing options or appending new options. This function
  901. does not change any state of the EFI DHCPv4 Protocol driver and can be used at
  902. any time.
  903. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  904. @param[in] SeedPacket Initial packet to be used as a base for building new packet.
  905. @param[in] DeleteCount Number of opcodes in the DeleteList.
  906. @param[in] DeleteList List of opcodes to be deleted from the seed packet.
  907. Ignored if DeleteCount is zero.
  908. @param[in] AppendCount Number of entries in the OptionList.
  909. @param[in] AppendList Pointer to a DHCP option list to be appended to SeedPacket.
  910. If SeedPacket also contains options in this list, they are
  911. replaced by new options (except pad option). Ignored if
  912. AppendCount is zero. Type EFI_DHCP4_PACKET_OPTION
  913. @param[out] NewPacket Pointer to storage for the pointer to the new allocated packet.
  914. Use the EFI Boot Service FreePool() on the resulting pointer
  915. when done with the packet.
  916. @retval EFI_SUCCESS The new packet was built.
  917. @retval EFI_OUT_OF_RESOURCES Storage for the new packet could not be allocated.
  918. @retval EFI_INVALID_PARAMETER Some parameter is NULL.
  919. **/
  920. EFI_STATUS
  921. EFIAPI
  922. EfiDhcp4Build (
  923. IN EFI_DHCP4_PROTOCOL *This,
  924. IN EFI_DHCP4_PACKET *SeedPacket,
  925. IN UINT32 DeleteCount,
  926. IN UINT8 *DeleteList OPTIONAL,
  927. IN UINT32 AppendCount,
  928. IN EFI_DHCP4_PACKET_OPTION *AppendList[] OPTIONAL,
  929. OUT EFI_DHCP4_PACKET **NewPacket
  930. )
  931. {
  932. //
  933. // First validate the parameters
  934. //
  935. if ((This == NULL) || (NewPacket == NULL)) {
  936. return EFI_INVALID_PARAMETER;
  937. }
  938. if ((SeedPacket == NULL) || (SeedPacket->Dhcp4.Magik != DHCP_OPTION_MAGIC) ||
  939. EFI_ERROR (DhcpValidateOptions (SeedPacket, NULL)))
  940. {
  941. return EFI_INVALID_PARAMETER;
  942. }
  943. if (((DeleteCount == 0) && (AppendCount == 0)) ||
  944. ((DeleteCount != 0) && (DeleteList == NULL)) ||
  945. ((AppendCount != 0) && (AppendList == NULL)))
  946. {
  947. return EFI_INVALID_PARAMETER;
  948. }
  949. return DhcpBuild (
  950. SeedPacket,
  951. DeleteCount,
  952. DeleteList,
  953. AppendCount,
  954. AppendList,
  955. NewPacket
  956. );
  957. }
  958. /**
  959. Callback by UdpIoCreatePort() when creating UdpIo for this Dhcp4 instance.
  960. @param[in] UdpIo The UdpIo being created.
  961. @param[in] Context Dhcp4 instance.
  962. @retval EFI_SUCCESS UdpIo is configured successfully.
  963. @retval EFI_INVALID_PARAMETER Class E IP address is not supported or other parameters
  964. are not valid.
  965. @retval other Other error occurs.
  966. **/
  967. EFI_STATUS
  968. EFIAPI
  969. Dhcp4InstanceConfigUdpIo (
  970. IN UDP_IO *UdpIo,
  971. IN VOID *Context
  972. )
  973. {
  974. DHCP_PROTOCOL *Instance;
  975. DHCP_SERVICE *DhcpSb;
  976. EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN *Token;
  977. EFI_UDP4_CONFIG_DATA UdpConfigData;
  978. IP4_ADDR ClientAddr;
  979. IP4_ADDR Ip;
  980. INTN Class;
  981. IP4_ADDR SubnetMask;
  982. Instance = (DHCP_PROTOCOL *)Context;
  983. DhcpSb = Instance->Service;
  984. Token = Instance->Token;
  985. ZeroMem (&UdpConfigData, sizeof (EFI_UDP4_CONFIG_DATA));
  986. UdpConfigData.AcceptBroadcast = TRUE;
  987. UdpConfigData.AllowDuplicatePort = TRUE;
  988. UdpConfigData.TimeToLive = 64;
  989. UdpConfigData.DoNotFragment = TRUE;
  990. ClientAddr = EFI_NTOHL (Token->Packet->Dhcp4.Header.ClientAddr);
  991. Ip = HTONL (ClientAddr);
  992. CopyMem (&UdpConfigData.StationAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));
  993. if (DhcpSb->Netmask == 0) {
  994. //
  995. // The Dhcp4.TransmitReceive() API should be able to used at any time according to
  996. // UEFI spec, while in classless addressing network, the netmask must be explicitly
  997. // provided together with the station address.
  998. // If the DHCP instance haven't be configured with a valid netmask, we could only
  999. // compute it according to the classful addressing rule.
  1000. //
  1001. Class = NetGetIpClass (ClientAddr);
  1002. //
  1003. // Class E IP address is not supported here!
  1004. //
  1005. ASSERT (Class < IP4_ADDR_CLASSE);
  1006. if (Class >= IP4_ADDR_CLASSE) {
  1007. return EFI_INVALID_PARAMETER;
  1008. }
  1009. SubnetMask = gIp4AllMasks[Class << 3];
  1010. } else {
  1011. SubnetMask = DhcpSb->Netmask;
  1012. }
  1013. Ip = HTONL (SubnetMask);
  1014. CopyMem (&UdpConfigData.SubnetMask, &Ip, sizeof (EFI_IPv4_ADDRESS));
  1015. if ((Token->ListenPointCount == 0) || (Token->ListenPoints[0].ListenPort == 0)) {
  1016. UdpConfigData.StationPort = DHCP_CLIENT_PORT;
  1017. } else {
  1018. UdpConfigData.StationPort = Token->ListenPoints[0].ListenPort;
  1019. }
  1020. return UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, &UdpConfigData);
  1021. }
  1022. /**
  1023. Create UdpIo for this Dhcp4 instance.
  1024. @param Instance The Dhcp4 instance.
  1025. @retval EFI_SUCCESS UdpIo is created successfully.
  1026. @retval EFI_OUT_OF_RESOURCES Fails to create UdpIo because of limited
  1027. resources or configuration failure.
  1028. **/
  1029. EFI_STATUS
  1030. Dhcp4InstanceCreateUdpIo (
  1031. IN OUT DHCP_PROTOCOL *Instance
  1032. )
  1033. {
  1034. DHCP_SERVICE *DhcpSb;
  1035. EFI_STATUS Status;
  1036. VOID *Udp4;
  1037. ASSERT (Instance->Token != NULL);
  1038. DhcpSb = Instance->Service;
  1039. Instance->UdpIo = UdpIoCreateIo (
  1040. DhcpSb->Controller,
  1041. DhcpSb->Image,
  1042. Dhcp4InstanceConfigUdpIo,
  1043. UDP_IO_UDP4_VERSION,
  1044. Instance
  1045. );
  1046. if (Instance->UdpIo == NULL) {
  1047. return EFI_OUT_OF_RESOURCES;
  1048. } else {
  1049. Status = gBS->OpenProtocol (
  1050. Instance->UdpIo->UdpHandle,
  1051. &gEfiUdp4ProtocolGuid,
  1052. (VOID **)&Udp4,
  1053. Instance->Service->Image,
  1054. Instance->Handle,
  1055. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  1056. );
  1057. if (EFI_ERROR (Status)) {
  1058. UdpIoFreeIo (Instance->UdpIo);
  1059. Instance->UdpIo = NULL;
  1060. }
  1061. return Status;
  1062. }
  1063. }
  1064. /**
  1065. Callback of Dhcp packet. Does nothing.
  1066. @param Arg The context.
  1067. **/
  1068. VOID
  1069. EFIAPI
  1070. DhcpDummyExtFree (
  1071. IN VOID *Arg
  1072. )
  1073. {
  1074. }
  1075. /**
  1076. Callback of UdpIoRecvDatagram() that handles a Dhcp4 packet.
  1077. Only BOOTP responses will be handled that correspond to the Xid of the request
  1078. sent out. The packet will be queued to the response queue.
  1079. @param UdpPacket The Dhcp4 packet.
  1080. @param EndPoint Udp4 address pair.
  1081. @param IoStatus Status of the input.
  1082. @param Context Extra info for the input.
  1083. **/
  1084. VOID
  1085. EFIAPI
  1086. PxeDhcpInput (
  1087. NET_BUF *UdpPacket,
  1088. UDP_END_POINT *EndPoint,
  1089. EFI_STATUS IoStatus,
  1090. VOID *Context
  1091. )
  1092. {
  1093. DHCP_PROTOCOL *Instance;
  1094. EFI_DHCP4_HEADER *Head;
  1095. NET_BUF *Wrap;
  1096. EFI_DHCP4_PACKET *Packet;
  1097. EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN *Token;
  1098. UINT32 Len;
  1099. EFI_STATUS Status;
  1100. Wrap = NULL;
  1101. Instance = (DHCP_PROTOCOL *)Context;
  1102. Token = Instance->Token;
  1103. //
  1104. // Don't restart receive if error occurs or DHCP is destroyed.
  1105. //
  1106. if (EFI_ERROR (IoStatus)) {
  1107. return;
  1108. }
  1109. ASSERT (UdpPacket != NULL);
  1110. //
  1111. // Validate the packet received
  1112. //
  1113. if (UdpPacket->TotalSize < sizeof (EFI_DHCP4_HEADER)) {
  1114. goto RESTART;
  1115. }
  1116. //
  1117. // Copy the DHCP message to a continuous memory block, make the buffer size
  1118. // of the EFI_DHCP4_PACKET a multiple of 4-byte.
  1119. //
  1120. Len = NET_ROUNDUP (sizeof (EFI_DHCP4_PACKET) + UdpPacket->TotalSize - sizeof (EFI_DHCP4_HEADER), 4);
  1121. Wrap = NetbufAlloc (Len);
  1122. if (Wrap == NULL) {
  1123. goto RESTART;
  1124. }
  1125. Packet = (EFI_DHCP4_PACKET *)NetbufAllocSpace (Wrap, Len, NET_BUF_TAIL);
  1126. ASSERT (Packet != NULL);
  1127. Packet->Size = Len;
  1128. Head = &Packet->Dhcp4.Header;
  1129. Packet->Length = NetbufCopy (UdpPacket, 0, UdpPacket->TotalSize, (UINT8 *)Head);
  1130. if (Packet->Length != UdpPacket->TotalSize) {
  1131. goto RESTART;
  1132. }
  1133. //
  1134. // Is this packet the answer to our packet?
  1135. //
  1136. if ((Head->OpCode != BOOTP_REPLY) ||
  1137. (Head->Xid != Token->Packet->Dhcp4.Header.Xid) ||
  1138. (CompareMem (&Token->Packet->Dhcp4.Header.ClientHwAddr[0], Head->ClientHwAddr, Head->HwAddrLen) != 0))
  1139. {
  1140. goto RESTART;
  1141. }
  1142. //
  1143. // Validate the options and retrieve the interested options
  1144. //
  1145. if ((Packet->Length > sizeof (EFI_DHCP4_HEADER) + sizeof (UINT32)) &&
  1146. (Packet->Dhcp4.Magik == DHCP_OPTION_MAGIC) &&
  1147. EFI_ERROR (DhcpValidateOptions (Packet, NULL)))
  1148. {
  1149. goto RESTART;
  1150. }
  1151. //
  1152. // Keep this packet in the ResponseQueue.
  1153. //
  1154. NET_GET_REF (Wrap);
  1155. NetbufQueAppend (&Instance->ResponseQueue, Wrap);
  1156. RESTART:
  1157. NetbufFree (UdpPacket);
  1158. if (Wrap != NULL) {
  1159. NetbufFree (Wrap);
  1160. }
  1161. Status = UdpIoRecvDatagram (Instance->UdpIo, PxeDhcpInput, Instance, 0);
  1162. if (EFI_ERROR (Status)) {
  1163. PxeDhcpDone (Instance);
  1164. }
  1165. }
  1166. /**
  1167. Complete a Dhcp4 transaction and signal the upper layer.
  1168. @param Instance Dhcp4 instance.
  1169. **/
  1170. VOID
  1171. PxeDhcpDone (
  1172. IN DHCP_PROTOCOL *Instance
  1173. )
  1174. {
  1175. EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN *Token;
  1176. Token = Instance->Token;
  1177. Token->ResponseCount = Instance->ResponseQueue.BufNum;
  1178. if (Token->ResponseCount != 0) {
  1179. Token->ResponseList = (EFI_DHCP4_PACKET *)AllocatePool (Instance->ResponseQueue.BufSize);
  1180. if (Token->ResponseList == NULL) {
  1181. Token->Status = EFI_OUT_OF_RESOURCES;
  1182. goto SIGNAL_USER;
  1183. }
  1184. //
  1185. // Copy the received DHCP responses.
  1186. //
  1187. NetbufQueCopy (&Instance->ResponseQueue, 0, Instance->ResponseQueue.BufSize, (UINT8 *)Token->ResponseList);
  1188. Token->Status = EFI_SUCCESS;
  1189. } else {
  1190. Token->ResponseList = NULL;
  1191. Token->Status = EFI_TIMEOUT;
  1192. }
  1193. SIGNAL_USER:
  1194. //
  1195. // Clean up the resources dedicated for this transmit receive transaction.
  1196. //
  1197. NetbufQueFlush (&Instance->ResponseQueue);
  1198. UdpIoCleanIo (Instance->UdpIo);
  1199. gBS->CloseProtocol (
  1200. Instance->UdpIo->UdpHandle,
  1201. &gEfiUdp4ProtocolGuid,
  1202. Instance->Service->Image,
  1203. Instance->Handle
  1204. );
  1205. UdpIoFreeIo (Instance->UdpIo);
  1206. Instance->UdpIo = NULL;
  1207. Instance->Token = NULL;
  1208. if (Token->CompletionEvent != NULL) {
  1209. gBS->SignalEvent (Token->CompletionEvent);
  1210. }
  1211. }
  1212. /**
  1213. Transmits a DHCP formatted packet and optionally waits for responses.
  1214. The TransmitReceive() function is used to transmit a DHCP packet and optionally
  1215. wait for the response from servers. This function does not change the state of
  1216. the EFI DHCPv4 Protocol driver and thus can be used at any time.
  1217. @param[in] This Pointer to the EFI_DHCP4_PROTOCOL instance.
  1218. @param[in] Token Pointer to the EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN structure.
  1219. @retval EFI_SUCCESS The packet was successfully queued for transmission.
  1220. @retval EFI_INVALID_PARAMETER Some parameter is NULL.
  1221. @retval EFI_NOT_READY The previous call to this function has not finished yet. Try to call
  1222. this function after collection process completes.
  1223. @retval EFI_NO_MAPPING The default station address is not available yet.
  1224. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  1225. @retval Others Some other unexpected error occurred.
  1226. **/
  1227. EFI_STATUS
  1228. EFIAPI
  1229. EfiDhcp4TransmitReceive (
  1230. IN EFI_DHCP4_PROTOCOL *This,
  1231. IN EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN *Token
  1232. )
  1233. {
  1234. DHCP_PROTOCOL *Instance;
  1235. EFI_TPL OldTpl;
  1236. EFI_STATUS Status;
  1237. NET_FRAGMENT Frag;
  1238. NET_BUF *Wrap;
  1239. UDP_END_POINT EndPoint;
  1240. IP4_ADDR Ip;
  1241. DHCP_SERVICE *DhcpSb;
  1242. EFI_IP_ADDRESS Gateway;
  1243. IP4_ADDR ClientAddr;
  1244. if ((This == NULL) || (Token == NULL) || (Token->Packet == NULL)) {
  1245. return EFI_INVALID_PARAMETER;
  1246. }
  1247. Instance = DHCP_INSTANCE_FROM_THIS (This);
  1248. DhcpSb = Instance->Service;
  1249. if (Instance->Token != NULL) {
  1250. //
  1251. // The previous call to TransmitReceive is not finished.
  1252. //
  1253. return EFI_NOT_READY;
  1254. }
  1255. if ((Token->Packet->Dhcp4.Magik != DHCP_OPTION_MAGIC) ||
  1256. (NTOHL (Token->Packet->Dhcp4.Header.Xid) == Instance->Service->Xid) ||
  1257. (Token->TimeoutValue == 0) ||
  1258. ((Token->ListenPointCount != 0) && (Token->ListenPoints == NULL)) ||
  1259. EFI_ERROR (DhcpValidateOptions (Token->Packet, NULL)) ||
  1260. EFI_IP4_EQUAL (&Token->RemoteAddress, &mZeroIp4Addr)
  1261. )
  1262. {
  1263. //
  1264. // The DHCP packet isn't well-formed, the Transaction ID is already used,
  1265. // the timeout value is zero, the ListenPoint is invalid, or the
  1266. // RemoteAddress is zero.
  1267. //
  1268. return EFI_INVALID_PARAMETER;
  1269. }
  1270. ClientAddr = EFI_NTOHL (Token->Packet->Dhcp4.Header.ClientAddr);
  1271. if (ClientAddr == 0) {
  1272. return EFI_NO_MAPPING;
  1273. }
  1274. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1275. //
  1276. // Save the token and the timeout value.
  1277. //
  1278. Instance->Token = Token;
  1279. Instance->Timeout = Token->TimeoutValue;
  1280. //
  1281. // Create a UDP IO for this transmit receive transaction.
  1282. //
  1283. Status = Dhcp4InstanceCreateUdpIo (Instance);
  1284. if (EFI_ERROR (Status)) {
  1285. goto ON_ERROR;
  1286. }
  1287. //
  1288. // Save the Client Address is sent out
  1289. //
  1290. CopyMem (
  1291. &DhcpSb->ClientAddressSendOut[0],
  1292. &Token->Packet->Dhcp4.Header.ClientHwAddr[0],
  1293. Token->Packet->Dhcp4.Header.HwAddrLen
  1294. );
  1295. //
  1296. // Wrap the DHCP packet into a net buffer.
  1297. //
  1298. Frag.Bulk = (UINT8 *)&Token->Packet->Dhcp4;
  1299. Frag.Len = Token->Packet->Length;
  1300. Wrap = NetbufFromExt (&Frag, 1, 0, 0, DhcpDummyExtFree, NULL);
  1301. if (Wrap == NULL) {
  1302. Status = EFI_OUT_OF_RESOURCES;
  1303. goto ON_ERROR;
  1304. }
  1305. //
  1306. // Set the local address and local port to ZERO.
  1307. //
  1308. ZeroMem (&EndPoint, sizeof (UDP_END_POINT));
  1309. //
  1310. // Set the destination address and destination port.
  1311. //
  1312. CopyMem (&Ip, &Token->RemoteAddress, sizeof (EFI_IPv4_ADDRESS));
  1313. EndPoint.RemoteAddr.Addr[0] = NTOHL (Ip);
  1314. if (Token->RemotePort == 0) {
  1315. EndPoint.RemotePort = DHCP_SERVER_PORT;
  1316. } else {
  1317. EndPoint.RemotePort = Token->RemotePort;
  1318. }
  1319. //
  1320. // Get the gateway.
  1321. //
  1322. ZeroMem (&Gateway, sizeof (Gateway));
  1323. if (!IP4_NET_EQUAL (ClientAddr, EndPoint.RemoteAddr.Addr[0], DhcpSb->Netmask)) {
  1324. CopyMem (&Gateway.v4, &Token->GatewayAddress, sizeof (EFI_IPv4_ADDRESS));
  1325. Gateway.Addr[0] = NTOHL (Gateway.Addr[0]);
  1326. }
  1327. //
  1328. // Transmit the DHCP packet.
  1329. //
  1330. Status = UdpIoSendDatagram (Instance->UdpIo, Wrap, &EndPoint, &Gateway, DhcpOnPacketSent, NULL);
  1331. if (EFI_ERROR (Status)) {
  1332. NetbufFree (Wrap);
  1333. goto ON_ERROR;
  1334. }
  1335. //
  1336. // Start to receive the DHCP response.
  1337. //
  1338. Status = UdpIoRecvDatagram (Instance->UdpIo, PxeDhcpInput, Instance, 0);
  1339. if (EFI_ERROR (Status)) {
  1340. goto ON_ERROR;
  1341. }
  1342. ON_ERROR:
  1343. if (EFI_ERROR (Status) && (Instance->UdpIo != NULL)) {
  1344. UdpIoCleanIo (Instance->UdpIo);
  1345. gBS->CloseProtocol (
  1346. Instance->UdpIo->UdpHandle,
  1347. &gEfiUdp4ProtocolGuid,
  1348. Instance->Service->Image,
  1349. Instance->Handle
  1350. );
  1351. UdpIoFreeIo (Instance->UdpIo);
  1352. Instance->UdpIo = NULL;
  1353. Instance->Token = NULL;
  1354. }
  1355. gBS->RestoreTPL (OldTpl);
  1356. if (!EFI_ERROR (Status) && (Token->CompletionEvent == NULL)) {
  1357. //
  1358. // Keep polling until timeout if no error happens and the CompletionEvent
  1359. // is NULL.
  1360. //
  1361. while (TRUE) {
  1362. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1363. //
  1364. // Raise TPL to protect the UDPIO in instance, in case that DhcpOnTimerTick
  1365. // free it when timeout.
  1366. //
  1367. if (Instance->Timeout > 0) {
  1368. Instance->UdpIo->Protocol.Udp4->Poll (Instance->UdpIo->Protocol.Udp4);
  1369. gBS->RestoreTPL (OldTpl);
  1370. } else {
  1371. gBS->RestoreTPL (OldTpl);
  1372. break;
  1373. }
  1374. }
  1375. }
  1376. return Status;
  1377. }
  1378. /**
  1379. Callback function for DhcpIterateOptions. This callback sets the
  1380. EFI_DHCP4_PACKET_OPTION array in the DHCP_PARSE_CONTEXT to point
  1381. the individual DHCP option in the packet.
  1382. @param[in] Tag The DHCP option type
  1383. @param[in] Len Length of the DHCP option data
  1384. @param[in] Data The DHCP option data
  1385. @param[in] Context The context, to pass several parameters in.
  1386. @retval EFI_SUCCESS It always returns EFI_SUCCESS
  1387. **/
  1388. EFI_STATUS
  1389. Dhcp4ParseCheckOption (
  1390. IN UINT8 Tag,
  1391. IN UINT8 Len,
  1392. IN UINT8 *Data,
  1393. IN VOID *Context
  1394. )
  1395. {
  1396. DHCP_PARSE_CONTEXT *Parse;
  1397. Parse = (DHCP_PARSE_CONTEXT *)Context;
  1398. Parse->Index++;
  1399. if (Parse->Index <= Parse->OptionCount) {
  1400. //
  1401. // Use BASE_CR to get the memory position of EFI_DHCP4_PACKET_OPTION for
  1402. // the EFI_DHCP4_PACKET_OPTION->Data because DhcpIterateOptions only
  1403. // pass in the point to option data.
  1404. //
  1405. Parse->Option[Parse->Index - 1] = BASE_CR (Data, EFI_DHCP4_PACKET_OPTION, Data);
  1406. }
  1407. return EFI_SUCCESS;
  1408. }
  1409. /**
  1410. Parses the packed DHCP option data.
  1411. The Parse() function is used to retrieve the option list from a DHCP packet.
  1412. If *OptionCount isn't zero, and there is enough space for all the DHCP options
  1413. in the Packet, each element of PacketOptionList is set to point to somewhere in
  1414. the Packet->Dhcp4.Option where a new DHCP option begins. If RFC3396 is supported,
  1415. the caller should reassemble the parsed DHCP options to get the finial result.
  1416. If *OptionCount is zero or there isn't enough space for all of them, the number
  1417. of DHCP options in the Packet is returned in OptionCount.
  1418. @param This Pointer to the EFI_DHCP4_PROTOCOL instance.
  1419. @param Packet Pointer to packet to be parsed.
  1420. @param OptionCount On input, the number of entries in the PacketOptionList.
  1421. On output, the number of entries that were written into the
  1422. PacketOptionList.
  1423. @param PacketOptionList List of packet option entries to be filled in. End option or pad
  1424. options are not included.
  1425. @retval EFI_SUCCESS The packet was successfully parsed.
  1426. @retval EFI_INVALID_PARAMETER Some parameter is NULL.
  1427. @retval EFI_BUFFER_TOO_SMALL One or more of the following conditions is TRUE:
  1428. 1) *OptionCount is smaller than the number of options that
  1429. were found in the Packet.
  1430. 2) PacketOptionList is NULL.
  1431. **/
  1432. EFI_STATUS
  1433. EFIAPI
  1434. EfiDhcp4Parse (
  1435. IN EFI_DHCP4_PROTOCOL *This,
  1436. IN EFI_DHCP4_PACKET *Packet,
  1437. IN OUT UINT32 *OptionCount,
  1438. OUT EFI_DHCP4_PACKET_OPTION *PacketOptionList[] OPTIONAL
  1439. )
  1440. {
  1441. DHCP_PARSE_CONTEXT Context;
  1442. EFI_STATUS Status;
  1443. //
  1444. // First validate the parameters
  1445. //
  1446. if ((This == NULL) || (Packet == NULL) || (OptionCount == NULL)) {
  1447. return EFI_INVALID_PARAMETER;
  1448. }
  1449. if ((Packet->Size < Packet->Length + 2 * sizeof (UINT32)) ||
  1450. (Packet->Dhcp4.Magik != DHCP_OPTION_MAGIC) ||
  1451. EFI_ERROR (DhcpValidateOptions (Packet, NULL)))
  1452. {
  1453. return EFI_INVALID_PARAMETER;
  1454. }
  1455. if ((*OptionCount != 0) && (PacketOptionList == NULL)) {
  1456. return EFI_BUFFER_TOO_SMALL;
  1457. }
  1458. ZeroMem (PacketOptionList, *OptionCount * sizeof (EFI_DHCP4_PACKET_OPTION *));
  1459. Context.Option = PacketOptionList;
  1460. Context.OptionCount = *OptionCount;
  1461. Context.Index = 0;
  1462. Status = DhcpIterateOptions (Packet, Dhcp4ParseCheckOption, &Context);
  1463. if (EFI_ERROR (Status)) {
  1464. return Status;
  1465. }
  1466. *OptionCount = Context.Index;
  1467. if (Context.Index > Context.OptionCount) {
  1468. return EFI_BUFFER_TOO_SMALL;
  1469. }
  1470. return EFI_SUCCESS;
  1471. }
  1472. /**
  1473. Set the elapsed time based on the given instance and the pointer to the
  1474. elapsed time option.
  1475. @param[in] Elapsed The pointer to the position to append.
  1476. @param[in] Instance The pointer to the Dhcp4 instance.
  1477. **/
  1478. VOID
  1479. SetElapsedTime (
  1480. IN UINT16 *Elapsed,
  1481. IN DHCP_PROTOCOL *Instance
  1482. )
  1483. {
  1484. WriteUnaligned16 (Elapsed, HTONS (Instance->ElaspedTime));
  1485. }