DnsImpl.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266
  1. /** @file
  2. DnsDxe support functions implementation.
  3. Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "DnsImpl.h"
  7. /**
  8. Remove TokenEntry from TokenMap.
  9. @param[in] TokenMap All DNSv4 Token entrys.
  10. @param[in] TokenEntry TokenEntry need to be removed.
  11. @retval EFI_SUCCESS Remove TokenEntry from TokenMap successfully.
  12. @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap.
  13. **/
  14. EFI_STATUS
  15. Dns4RemoveTokenEntry (
  16. IN NET_MAP *TokenMap,
  17. IN DNS4_TOKEN_ENTRY *TokenEntry
  18. )
  19. {
  20. NET_MAP_ITEM *Item;
  21. //
  22. // Find the TokenEntry first.
  23. //
  24. Item = NetMapFindKey (TokenMap, (VOID *)TokenEntry);
  25. if (Item != NULL) {
  26. //
  27. // Remove the TokenEntry if it's found in the map.
  28. //
  29. NetMapRemoveItem (TokenMap, Item, NULL);
  30. return EFI_SUCCESS;
  31. }
  32. return EFI_NOT_FOUND;
  33. }
  34. /**
  35. Remove TokenEntry from TokenMap.
  36. @param[in] TokenMap All DNSv6 Token entrys.
  37. @param[in] TokenEntry TokenEntry need to be removed.
  38. @retval EFI_SUCCESS Remove TokenEntry from TokenMap successfully.
  39. @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap.
  40. **/
  41. EFI_STATUS
  42. Dns6RemoveTokenEntry (
  43. IN NET_MAP *TokenMap,
  44. IN DNS6_TOKEN_ENTRY *TokenEntry
  45. )
  46. {
  47. NET_MAP_ITEM *Item;
  48. //
  49. // Find the TokenEntry first.
  50. //
  51. Item = NetMapFindKey (TokenMap, (VOID *)TokenEntry);
  52. if (Item != NULL) {
  53. //
  54. // Remove the TokenEntry if it's found in the map.
  55. //
  56. NetMapRemoveItem (TokenMap, Item, NULL);
  57. return EFI_SUCCESS;
  58. }
  59. return EFI_NOT_FOUND;
  60. }
  61. /**
  62. This function cancel the token specified by Arg in the Map.
  63. @param[in] Map Pointer to the NET_MAP.
  64. @param[in] Item Pointer to the NET_MAP_ITEM.
  65. @param[in] Arg Pointer to the token to be cancelled. If NULL, all
  66. the tokens in this Map will be cancelled.
  67. This parameter is optional and may be NULL.
  68. @retval EFI_SUCCESS The token is cancelled if Arg is NULL, or the token
  69. is not the same as that in the Item, if Arg is not
  70. NULL.
  71. @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
  72. cancelled.
  73. **/
  74. EFI_STATUS
  75. EFIAPI
  76. Dns4CancelTokens (
  77. IN NET_MAP *Map,
  78. IN NET_MAP_ITEM *Item,
  79. IN VOID *Arg OPTIONAL
  80. )
  81. {
  82. DNS4_TOKEN_ENTRY *TokenEntry;
  83. NET_BUF *Packet;
  84. UDP_IO *UdpIo;
  85. if ((Arg != NULL) && (Item->Key != Arg)) {
  86. return EFI_SUCCESS;
  87. }
  88. if (Item->Value != NULL) {
  89. //
  90. // If the TokenEntry is a transmit TokenEntry, the corresponding Packet is recorded in
  91. // Item->Value.
  92. //
  93. Packet = (NET_BUF *)(Item->Value);
  94. UdpIo = (UDP_IO *)(*((UINTN *)&Packet->ProtoData[0]));
  95. UdpIoCancelSentDatagram (UdpIo, Packet);
  96. }
  97. //
  98. // Remove TokenEntry from Dns4TxTokens.
  99. //
  100. TokenEntry = (DNS4_TOKEN_ENTRY *)Item->Key;
  101. if (Dns4RemoveTokenEntry (Map, TokenEntry) == EFI_SUCCESS) {
  102. TokenEntry->Token->Status = EFI_ABORTED;
  103. gBS->SignalEvent (TokenEntry->Token->Event);
  104. DispatchDpc ();
  105. }
  106. if (Arg != NULL) {
  107. return EFI_ABORTED;
  108. }
  109. return EFI_SUCCESS;
  110. }
  111. /**
  112. This function cancel the token specified by Arg in the Map.
  113. @param[in] Map Pointer to the NET_MAP.
  114. @param[in] Item Pointer to the NET_MAP_ITEM.
  115. @param[in] Arg Pointer to the token to be cancelled. If NULL, all
  116. the tokens in this Map will be cancelled.
  117. This parameter is optional and may be NULL.
  118. @retval EFI_SUCCESS The token is cancelled if Arg is NULL, or the token
  119. is not the same as that in the Item, if Arg is not
  120. NULL.
  121. @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
  122. cancelled.
  123. **/
  124. EFI_STATUS
  125. EFIAPI
  126. Dns6CancelTokens (
  127. IN NET_MAP *Map,
  128. IN NET_MAP_ITEM *Item,
  129. IN VOID *Arg OPTIONAL
  130. )
  131. {
  132. DNS6_TOKEN_ENTRY *TokenEntry;
  133. NET_BUF *Packet;
  134. UDP_IO *UdpIo;
  135. if ((Arg != NULL) && (Item->Key != Arg)) {
  136. return EFI_SUCCESS;
  137. }
  138. if (Item->Value != NULL) {
  139. //
  140. // If the TokenEntry is a transmit TokenEntry, the corresponding Packet is recorded in
  141. // Item->Value.
  142. //
  143. Packet = (NET_BUF *)(Item->Value);
  144. UdpIo = (UDP_IO *)(*((UINTN *)&Packet->ProtoData[0]));
  145. UdpIoCancelSentDatagram (UdpIo, Packet);
  146. }
  147. //
  148. // Remove TokenEntry from Dns6TxTokens.
  149. //
  150. TokenEntry = (DNS6_TOKEN_ENTRY *)Item->Key;
  151. if (Dns6RemoveTokenEntry (Map, TokenEntry) == EFI_SUCCESS) {
  152. TokenEntry->Token->Status = EFI_ABORTED;
  153. gBS->SignalEvent (TokenEntry->Token->Event);
  154. DispatchDpc ();
  155. }
  156. if (Arg != NULL) {
  157. return EFI_ABORTED;
  158. }
  159. return EFI_SUCCESS;
  160. }
  161. /**
  162. Get the TokenEntry from the TokensMap.
  163. @param[in] TokensMap All DNSv4 Token entrys
  164. @param[in] Token Pointer to the token to be get.
  165. @param[out] TokenEntry Pointer to TokenEntry corresponding Token.
  166. @retval EFI_SUCCESS Get the TokenEntry from the TokensMap successfully.
  167. @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap.
  168. **/
  169. EFI_STATUS
  170. EFIAPI
  171. GetDns4TokenEntry (
  172. IN NET_MAP *TokensMap,
  173. IN EFI_DNS4_COMPLETION_TOKEN *Token,
  174. OUT DNS4_TOKEN_ENTRY **TokenEntry
  175. )
  176. {
  177. LIST_ENTRY *Entry;
  178. NET_MAP_ITEM *Item;
  179. NET_LIST_FOR_EACH (Entry, &TokensMap->Used) {
  180. Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
  181. *TokenEntry = (DNS4_TOKEN_ENTRY *)(Item->Key);
  182. if ((*TokenEntry)->Token == Token) {
  183. return EFI_SUCCESS;
  184. }
  185. }
  186. *TokenEntry = NULL;
  187. return EFI_NOT_FOUND;
  188. }
  189. /**
  190. Get the TokenEntry from the TokensMap.
  191. @param[in] TokensMap All DNSv6 Token entrys
  192. @param[in] Token Pointer to the token to be get.
  193. @param[out] TokenEntry Pointer to TokenEntry corresponding Token.
  194. @retval EFI_SUCCESS Get the TokenEntry from the TokensMap successfully.
  195. @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap.
  196. **/
  197. EFI_STATUS
  198. EFIAPI
  199. GetDns6TokenEntry (
  200. IN NET_MAP *TokensMap,
  201. IN EFI_DNS6_COMPLETION_TOKEN *Token,
  202. OUT DNS6_TOKEN_ENTRY **TokenEntry
  203. )
  204. {
  205. LIST_ENTRY *Entry;
  206. NET_MAP_ITEM *Item;
  207. NET_LIST_FOR_EACH (Entry, &TokensMap->Used) {
  208. Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
  209. *TokenEntry = (DNS6_TOKEN_ENTRY *)(Item->Key);
  210. if ((*TokenEntry)->Token == Token) {
  211. return EFI_SUCCESS;
  212. }
  213. }
  214. *TokenEntry = NULL;
  215. return EFI_NOT_FOUND;
  216. }
  217. /**
  218. Cancel DNS4 tokens from the DNS4 instance.
  219. @param[in] Instance Pointer to the DNS instance context data.
  220. @param[in] Token Pointer to the token to be canceled. If NULL, all
  221. tokens in this instance will be cancelled.
  222. This parameter is optional and may be NULL.
  223. @retval EFI_SUCCESS The Token is cancelled.
  224. @retval EFI_NOT_FOUND The Token is not found.
  225. **/
  226. EFI_STATUS
  227. Dns4InstanceCancelToken (
  228. IN DNS_INSTANCE *Instance,
  229. IN EFI_DNS4_COMPLETION_TOKEN *Token
  230. )
  231. {
  232. EFI_STATUS Status;
  233. DNS4_TOKEN_ENTRY *TokenEntry;
  234. TokenEntry = NULL;
  235. if (Token != NULL ) {
  236. Status = GetDns4TokenEntry (&Instance->Dns4TxTokens, Token, &TokenEntry);
  237. if (EFI_ERROR (Status)) {
  238. return Status;
  239. }
  240. } else {
  241. TokenEntry = NULL;
  242. }
  243. //
  244. // Cancel this TokenEntry from the Dns4TxTokens map.
  245. //
  246. Status = NetMapIterate (&Instance->Dns4TxTokens, Dns4CancelTokens, TokenEntry);
  247. if ((TokenEntry != NULL) && (Status == EFI_ABORTED)) {
  248. //
  249. // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
  250. // the Dns4TxTokens and returns success.
  251. //
  252. if (NetMapIsEmpty (&Instance->Dns4TxTokens)) {
  253. Instance->UdpIo->Protocol.Udp4->Cancel (Instance->UdpIo->Protocol.Udp4, &Instance->UdpIo->RecvRequest->Token.Udp4);
  254. }
  255. return EFI_SUCCESS;
  256. }
  257. ASSERT ((TokenEntry != NULL) || (0 == NetMapGetCount (&Instance->Dns4TxTokens)));
  258. if (NetMapIsEmpty (&Instance->Dns4TxTokens)) {
  259. Instance->UdpIo->Protocol.Udp4->Cancel (Instance->UdpIo->Protocol.Udp4, &Instance->UdpIo->RecvRequest->Token.Udp4);
  260. }
  261. return EFI_SUCCESS;
  262. }
  263. /**
  264. Cancel DNS6 tokens from the DNS6 instance.
  265. @param[in] Instance Pointer to the DNS instance context data.
  266. @param[in] Token Pointer to the token to be canceled. If NULL, all
  267. tokens in this instance will be cancelled.
  268. This parameter is optional and may be NULL.
  269. @retval EFI_SUCCESS The Token is cancelled.
  270. @retval EFI_NOT_FOUND The Token is not found.
  271. **/
  272. EFI_STATUS
  273. Dns6InstanceCancelToken (
  274. IN DNS_INSTANCE *Instance,
  275. IN EFI_DNS6_COMPLETION_TOKEN *Token
  276. )
  277. {
  278. EFI_STATUS Status;
  279. DNS6_TOKEN_ENTRY *TokenEntry;
  280. TokenEntry = NULL;
  281. if (Token != NULL ) {
  282. Status = GetDns6TokenEntry (&Instance->Dns6TxTokens, Token, &TokenEntry);
  283. if (EFI_ERROR (Status)) {
  284. return Status;
  285. }
  286. } else {
  287. TokenEntry = NULL;
  288. }
  289. //
  290. // Cancel this TokenEntry from the Dns6TxTokens map.
  291. //
  292. Status = NetMapIterate (&Instance->Dns6TxTokens, Dns6CancelTokens, TokenEntry);
  293. if ((TokenEntry != NULL) && (Status == EFI_ABORTED)) {
  294. //
  295. // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
  296. // the Dns6TxTokens and returns success.
  297. //
  298. if (NetMapIsEmpty (&Instance->Dns6TxTokens)) {
  299. Instance->UdpIo->Protocol.Udp6->Cancel (Instance->UdpIo->Protocol.Udp6, &Instance->UdpIo->RecvRequest->Token.Udp6);
  300. }
  301. return EFI_SUCCESS;
  302. }
  303. ASSERT ((TokenEntry != NULL) || (0 == NetMapGetCount (&Instance->Dns6TxTokens)));
  304. if (NetMapIsEmpty (&Instance->Dns6TxTokens)) {
  305. Instance->UdpIo->Protocol.Udp6->Cancel (Instance->UdpIo->Protocol.Udp6, &Instance->UdpIo->RecvRequest->Token.Udp6);
  306. }
  307. return EFI_SUCCESS;
  308. }
  309. /**
  310. Free the resource related to the configure parameters.
  311. @param Config The DNS configure data
  312. **/
  313. VOID
  314. Dns4CleanConfigure (
  315. IN OUT EFI_DNS4_CONFIG_DATA *Config
  316. )
  317. {
  318. if (Config->DnsServerList != NULL) {
  319. FreePool (Config->DnsServerList);
  320. }
  321. ZeroMem (Config, sizeof (EFI_DNS4_CONFIG_DATA));
  322. }
  323. /**
  324. Free the resource related to the configure parameters.
  325. @param Config The DNS configure data
  326. **/
  327. VOID
  328. Dns6CleanConfigure (
  329. IN OUT EFI_DNS6_CONFIG_DATA *Config
  330. )
  331. {
  332. if (Config->DnsServerList != NULL) {
  333. FreePool (Config->DnsServerList);
  334. }
  335. ZeroMem (Config, sizeof (EFI_DNS6_CONFIG_DATA));
  336. }
  337. /**
  338. Allocate memory for configure parameter such as timeout value for Dst,
  339. then copy the configure parameter from Src to Dst.
  340. @param[out] Dst The destination DHCP configure data.
  341. @param[in] Src The source DHCP configure data.
  342. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
  343. @retval EFI_SUCCESS The configure is copied.
  344. **/
  345. EFI_STATUS
  346. Dns4CopyConfigure (
  347. OUT EFI_DNS4_CONFIG_DATA *Dst,
  348. IN EFI_DNS4_CONFIG_DATA *Src
  349. )
  350. {
  351. UINTN Len;
  352. UINT32 Index;
  353. CopyMem (Dst, Src, sizeof (*Dst));
  354. Dst->DnsServerList = NULL;
  355. //
  356. // Allocate a memory then copy DnsServerList to it
  357. //
  358. if (Src->DnsServerList != NULL) {
  359. Len = Src->DnsServerListCount * sizeof (EFI_IPv4_ADDRESS);
  360. Dst->DnsServerList = AllocatePool (Len);
  361. if (Dst->DnsServerList == NULL) {
  362. Dns4CleanConfigure (Dst);
  363. return EFI_OUT_OF_RESOURCES;
  364. }
  365. for (Index = 0; Index < Src->DnsServerListCount; Index++) {
  366. CopyMem (&Dst->DnsServerList[Index], &Src->DnsServerList[Index], sizeof (EFI_IPv4_ADDRESS));
  367. }
  368. }
  369. return EFI_SUCCESS;
  370. }
  371. /**
  372. Allocate memory for configure parameter such as timeout value for Dst,
  373. then copy the configure parameter from Src to Dst.
  374. @param[out] Dst The destination DHCP configure data.
  375. @param[in] Src The source DHCP configure data.
  376. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
  377. @retval EFI_SUCCESS The configure is copied.
  378. **/
  379. EFI_STATUS
  380. Dns6CopyConfigure (
  381. OUT EFI_DNS6_CONFIG_DATA *Dst,
  382. IN EFI_DNS6_CONFIG_DATA *Src
  383. )
  384. {
  385. UINTN Len;
  386. UINT32 Index;
  387. CopyMem (Dst, Src, sizeof (*Dst));
  388. Dst->DnsServerList = NULL;
  389. //
  390. // Allocate a memory then copy DnsServerList to it
  391. //
  392. if (Src->DnsServerList != NULL) {
  393. Len = Src->DnsServerCount * sizeof (EFI_IPv6_ADDRESS);
  394. Dst->DnsServerList = AllocatePool (Len);
  395. if (Dst->DnsServerList == NULL) {
  396. Dns6CleanConfigure (Dst);
  397. return EFI_OUT_OF_RESOURCES;
  398. }
  399. for (Index = 0; Index < Src->DnsServerCount; Index++) {
  400. CopyMem (&Dst->DnsServerList[Index], &Src->DnsServerList[Index], sizeof (EFI_IPv6_ADDRESS));
  401. }
  402. }
  403. return EFI_SUCCESS;
  404. }
  405. /**
  406. Callback of Dns packet. Does nothing.
  407. @param Arg The context.
  408. **/
  409. VOID
  410. EFIAPI
  411. DnsDummyExtFree (
  412. IN VOID *Arg
  413. )
  414. {
  415. }
  416. /**
  417. Poll the UDP to get the IP4 default address, which may be retrieved
  418. by DHCP.
  419. The default time out value is 5 seconds. If IP has retrieved the default address,
  420. the UDP is reconfigured.
  421. @param Instance The DNS instance
  422. @param UdpIo The UDP_IO to poll
  423. @param UdpCfgData The UDP configure data to reconfigure the UDP_IO
  424. @retval TRUE The default address is retrieved and UDP is reconfigured.
  425. @retval FALSE Some error occurred.
  426. **/
  427. BOOLEAN
  428. Dns4GetMapping (
  429. IN DNS_INSTANCE *Instance,
  430. IN UDP_IO *UdpIo,
  431. IN EFI_UDP4_CONFIG_DATA *UdpCfgData
  432. )
  433. {
  434. DNS_SERVICE *Service;
  435. EFI_IP4_MODE_DATA Ip4Mode;
  436. EFI_UDP4_PROTOCOL *Udp;
  437. EFI_STATUS Status;
  438. ASSERT (Instance->Dns4CfgData.UseDefaultSetting);
  439. Service = Instance->Service;
  440. Udp = UdpIo->Protocol.Udp4;
  441. Status = gBS->SetTimer (
  442. Service->TimerToGetMap,
  443. TimerRelative,
  444. DNS_TIME_TO_GETMAP * TICKS_PER_SECOND
  445. );
  446. if (EFI_ERROR (Status)) {
  447. return FALSE;
  448. }
  449. while (EFI_ERROR (gBS->CheckEvent (Service->TimerToGetMap))) {
  450. Udp->Poll (Udp);
  451. if (!EFI_ERROR (Udp->GetModeData (Udp, NULL, &Ip4Mode, NULL, NULL)) &&
  452. Ip4Mode.IsConfigured)
  453. {
  454. Udp->Configure (Udp, NULL);
  455. return (BOOLEAN)(Udp->Configure (Udp, UdpCfgData) == EFI_SUCCESS);
  456. }
  457. }
  458. return FALSE;
  459. }
  460. /**
  461. Configure the opened Udp6 instance until the corresponding Ip6 instance
  462. has been configured.
  463. @param Instance The DNS instance
  464. @param UdpIo The UDP_IO to poll
  465. @param UdpCfgData The UDP configure data to reconfigure the UDP_IO
  466. @retval TRUE Configure the Udp6 instance successfully.
  467. @retval FALSE Some error occurred.
  468. **/
  469. BOOLEAN
  470. Dns6GetMapping (
  471. IN DNS_INSTANCE *Instance,
  472. IN UDP_IO *UdpIo,
  473. IN EFI_UDP6_CONFIG_DATA *UdpCfgData
  474. )
  475. {
  476. DNS_SERVICE *Service;
  477. EFI_IP6_MODE_DATA Ip6Mode;
  478. EFI_UDP6_PROTOCOL *Udp;
  479. EFI_STATUS Status;
  480. Service = Instance->Service;
  481. Udp = UdpIo->Protocol.Udp6;
  482. Status = gBS->SetTimer (
  483. Service->TimerToGetMap,
  484. TimerRelative,
  485. DNS_TIME_TO_GETMAP * TICKS_PER_SECOND
  486. );
  487. if (EFI_ERROR (Status)) {
  488. return FALSE;
  489. }
  490. while (EFI_ERROR (gBS->CheckEvent (Service->TimerToGetMap))) {
  491. Udp->Poll (Udp);
  492. if (!EFI_ERROR (Udp->GetModeData (Udp, NULL, &Ip6Mode, NULL, NULL))) {
  493. if (Ip6Mode.AddressList != NULL) {
  494. FreePool (Ip6Mode.AddressList);
  495. }
  496. if (Ip6Mode.GroupTable != NULL) {
  497. FreePool (Ip6Mode.GroupTable);
  498. }
  499. if (Ip6Mode.RouteTable != NULL) {
  500. FreePool (Ip6Mode.RouteTable);
  501. }
  502. if (Ip6Mode.NeighborCache != NULL) {
  503. FreePool (Ip6Mode.NeighborCache);
  504. }
  505. if (Ip6Mode.PrefixTable != NULL) {
  506. FreePool (Ip6Mode.PrefixTable);
  507. }
  508. if (Ip6Mode.IcmpTypeList != NULL) {
  509. FreePool (Ip6Mode.IcmpTypeList);
  510. }
  511. if (!Ip6Mode.IsStarted || Ip6Mode.IsConfigured) {
  512. Udp->Configure (Udp, NULL);
  513. if (Udp->Configure (Udp, UdpCfgData) == EFI_SUCCESS) {
  514. return TRUE;
  515. }
  516. }
  517. }
  518. }
  519. return FALSE;
  520. }
  521. /**
  522. Configure the UDP.
  523. @param Instance The DNS session
  524. @param UdpIo The UDP_IO instance
  525. @retval EFI_SUCCESS The UDP is successfully configured for the
  526. session.
  527. **/
  528. EFI_STATUS
  529. Dns4ConfigUdp (
  530. IN DNS_INSTANCE *Instance,
  531. IN UDP_IO *UdpIo
  532. )
  533. {
  534. EFI_DNS4_CONFIG_DATA *Config;
  535. EFI_UDP4_CONFIG_DATA UdpConfig;
  536. EFI_STATUS Status;
  537. Config = &Instance->Dns4CfgData;
  538. UdpConfig.AcceptBroadcast = FALSE;
  539. UdpConfig.AcceptPromiscuous = FALSE;
  540. UdpConfig.AcceptAnyPort = FALSE;
  541. UdpConfig.AllowDuplicatePort = FALSE;
  542. UdpConfig.TypeOfService = 0;
  543. UdpConfig.TimeToLive = 128;
  544. UdpConfig.DoNotFragment = FALSE;
  545. UdpConfig.ReceiveTimeout = 0;
  546. UdpConfig.TransmitTimeout = 0;
  547. UdpConfig.UseDefaultAddress = Config->UseDefaultSetting;
  548. UdpConfig.SubnetMask = Config->SubnetMask;
  549. UdpConfig.StationPort = Config->LocalPort;
  550. UdpConfig.RemotePort = DNS_SERVER_PORT;
  551. CopyMem (&UdpConfig.StationAddress, &Config->StationIp, sizeof (EFI_IPv4_ADDRESS));
  552. CopyMem (&UdpConfig.RemoteAddress, &Instance->SessionDnsServer.v4, sizeof (EFI_IPv4_ADDRESS));
  553. Status = UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, &UdpConfig);
  554. if ((Status == EFI_NO_MAPPING) && Dns4GetMapping (Instance, UdpIo, &UdpConfig)) {
  555. return EFI_SUCCESS;
  556. }
  557. return Status;
  558. }
  559. /**
  560. Configure the UDP.
  561. @param Instance The DNS session
  562. @param UdpIo The UDP_IO instance
  563. @retval EFI_SUCCESS The UDP is successfully configured for the
  564. session.
  565. **/
  566. EFI_STATUS
  567. Dns6ConfigUdp (
  568. IN DNS_INSTANCE *Instance,
  569. IN UDP_IO *UdpIo
  570. )
  571. {
  572. EFI_DNS6_CONFIG_DATA *Config;
  573. EFI_UDP6_CONFIG_DATA UdpConfig;
  574. EFI_STATUS Status;
  575. Config = &Instance->Dns6CfgData;
  576. UdpConfig.AcceptPromiscuous = FALSE;
  577. UdpConfig.AcceptAnyPort = FALSE;
  578. UdpConfig.AllowDuplicatePort = FALSE;
  579. UdpConfig.TrafficClass = 0;
  580. UdpConfig.HopLimit = 128;
  581. UdpConfig.ReceiveTimeout = 0;
  582. UdpConfig.TransmitTimeout = 0;
  583. UdpConfig.StationPort = Config->LocalPort;
  584. UdpConfig.RemotePort = DNS_SERVER_PORT;
  585. CopyMem (&UdpConfig.StationAddress, &Config->StationIp, sizeof (EFI_IPv6_ADDRESS));
  586. CopyMem (&UdpConfig.RemoteAddress, &Instance->SessionDnsServer.v6, sizeof (EFI_IPv6_ADDRESS));
  587. Status = UdpIo->Protocol.Udp6->Configure (UdpIo->Protocol.Udp6, &UdpConfig);
  588. if ((Status == EFI_NO_MAPPING) && Dns6GetMapping (Instance, UdpIo, &UdpConfig)) {
  589. return EFI_SUCCESS;
  590. }
  591. return Status;
  592. }
  593. /**
  594. Update Dns4 cache to shared list of caches of all DNSv4 instances.
  595. @param Dns4CacheList All Dns4 cache list.
  596. @param DeleteFlag If FALSE, this function is to add one entry to the DNS Cache.
  597. If TRUE, this function will delete matching DNS Cache entry.
  598. @param Override If TRUE, the matching DNS cache entry will be overwritten with the supplied parameter.
  599. If FALSE, EFI_ACCESS_DENIED will be returned if the entry to be added is already exists.
  600. @param DnsCacheEntry Entry Pointer to DNS Cache entry.
  601. @retval EFI_SUCCESS Update Dns4 cache successfully.
  602. @retval Others Failed to update Dns4 cache.
  603. **/
  604. EFI_STATUS
  605. EFIAPI
  606. UpdateDns4Cache (
  607. IN LIST_ENTRY *Dns4CacheList,
  608. IN BOOLEAN DeleteFlag,
  609. IN BOOLEAN Override,
  610. IN EFI_DNS4_CACHE_ENTRY DnsCacheEntry
  611. )
  612. {
  613. DNS4_CACHE *NewDnsCache;
  614. DNS4_CACHE *Item;
  615. LIST_ENTRY *Entry;
  616. LIST_ENTRY *Next;
  617. NewDnsCache = NULL;
  618. Item = NULL;
  619. //
  620. // Search the database for the matching EFI_DNS_CACHE_ENTRY
  621. //
  622. NET_LIST_FOR_EACH_SAFE (Entry, Next, Dns4CacheList) {
  623. Item = NET_LIST_USER_STRUCT (Entry, DNS4_CACHE, AllCacheLink);
  624. if ((StrCmp (DnsCacheEntry.HostName, Item->DnsCache.HostName) == 0) && \
  625. (CompareMem (DnsCacheEntry.IpAddress, Item->DnsCache.IpAddress, sizeof (EFI_IPv4_ADDRESS)) == 0))
  626. {
  627. //
  628. // This is the Dns cache entry
  629. //
  630. if (DeleteFlag) {
  631. //
  632. // Delete matching DNS Cache entry
  633. //
  634. RemoveEntryList (&Item->AllCacheLink);
  635. FreePool (Item->DnsCache.HostName);
  636. FreePool (Item->DnsCache.IpAddress);
  637. FreePool (Item);
  638. return EFI_SUCCESS;
  639. } else if (Override) {
  640. //
  641. // Update this one
  642. //
  643. Item->DnsCache.Timeout = DnsCacheEntry.Timeout;
  644. return EFI_SUCCESS;
  645. } else {
  646. return EFI_ACCESS_DENIED;
  647. }
  648. }
  649. }
  650. //
  651. // Add new one
  652. //
  653. NewDnsCache = AllocatePool (sizeof (DNS4_CACHE));
  654. if (NewDnsCache == NULL) {
  655. return EFI_OUT_OF_RESOURCES;
  656. }
  657. InitializeListHead (&NewDnsCache->AllCacheLink);
  658. NewDnsCache->DnsCache.HostName = AllocatePool (StrSize (DnsCacheEntry.HostName));
  659. if (NewDnsCache->DnsCache.HostName == NULL) {
  660. FreePool (NewDnsCache);
  661. return EFI_OUT_OF_RESOURCES;
  662. }
  663. CopyMem (NewDnsCache->DnsCache.HostName, DnsCacheEntry.HostName, StrSize (DnsCacheEntry.HostName));
  664. NewDnsCache->DnsCache.IpAddress = AllocatePool (sizeof (EFI_IPv4_ADDRESS));
  665. if (NewDnsCache->DnsCache.IpAddress == NULL) {
  666. FreePool (NewDnsCache->DnsCache.HostName);
  667. FreePool (NewDnsCache);
  668. return EFI_OUT_OF_RESOURCES;
  669. }
  670. CopyMem (NewDnsCache->DnsCache.IpAddress, DnsCacheEntry.IpAddress, sizeof (EFI_IPv4_ADDRESS));
  671. NewDnsCache->DnsCache.Timeout = DnsCacheEntry.Timeout;
  672. InsertTailList (Dns4CacheList, &NewDnsCache->AllCacheLink);
  673. return EFI_SUCCESS;
  674. }
  675. /**
  676. Update Dns6 cache to shared list of caches of all DNSv6 instances.
  677. @param Dns6CacheList All Dns6 cache list.
  678. @param DeleteFlag If FALSE, this function is to add one entry to the DNS Cache.
  679. If TRUE, this function will delete matching DNS Cache entry.
  680. @param Override If TRUE, the matching DNS cache entry will be overwritten with the supplied parameter.
  681. If FALSE, EFI_ACCESS_DENIED will be returned if the entry to be added is already exists.
  682. @param DnsCacheEntry Entry Pointer to DNS Cache entry.
  683. @retval EFI_SUCCESS Update Dns6 cache successfully.
  684. @retval Others Failed to update Dns6 cache.
  685. **/
  686. EFI_STATUS
  687. EFIAPI
  688. UpdateDns6Cache (
  689. IN LIST_ENTRY *Dns6CacheList,
  690. IN BOOLEAN DeleteFlag,
  691. IN BOOLEAN Override,
  692. IN EFI_DNS6_CACHE_ENTRY DnsCacheEntry
  693. )
  694. {
  695. DNS6_CACHE *NewDnsCache;
  696. DNS6_CACHE *Item;
  697. LIST_ENTRY *Entry;
  698. LIST_ENTRY *Next;
  699. NewDnsCache = NULL;
  700. Item = NULL;
  701. //
  702. // Search the database for the matching EFI_DNS_CACHE_ENTRY
  703. //
  704. NET_LIST_FOR_EACH_SAFE (Entry, Next, Dns6CacheList) {
  705. Item = NET_LIST_USER_STRUCT (Entry, DNS6_CACHE, AllCacheLink);
  706. if ((StrCmp (DnsCacheEntry.HostName, Item->DnsCache.HostName) == 0) && \
  707. (CompareMem (DnsCacheEntry.IpAddress, Item->DnsCache.IpAddress, sizeof (EFI_IPv6_ADDRESS)) == 0))
  708. {
  709. //
  710. // This is the Dns cache entry
  711. //
  712. if (DeleteFlag) {
  713. //
  714. // Delete matching DNS Cache entry
  715. //
  716. RemoveEntryList (&Item->AllCacheLink);
  717. FreePool (Item->DnsCache.HostName);
  718. FreePool (Item->DnsCache.IpAddress);
  719. FreePool (Item);
  720. return EFI_SUCCESS;
  721. } else if (Override) {
  722. //
  723. // Update this one
  724. //
  725. Item->DnsCache.Timeout = DnsCacheEntry.Timeout;
  726. return EFI_SUCCESS;
  727. } else {
  728. return EFI_ACCESS_DENIED;
  729. }
  730. }
  731. }
  732. //
  733. // Add new one
  734. //
  735. NewDnsCache = AllocatePool (sizeof (DNS6_CACHE));
  736. if (NewDnsCache == NULL) {
  737. return EFI_OUT_OF_RESOURCES;
  738. }
  739. InitializeListHead (&NewDnsCache->AllCacheLink);
  740. NewDnsCache->DnsCache.HostName = AllocatePool (StrSize (DnsCacheEntry.HostName));
  741. if (NewDnsCache->DnsCache.HostName == NULL) {
  742. FreePool (NewDnsCache);
  743. return EFI_OUT_OF_RESOURCES;
  744. }
  745. CopyMem (NewDnsCache->DnsCache.HostName, DnsCacheEntry.HostName, StrSize (DnsCacheEntry.HostName));
  746. NewDnsCache->DnsCache.IpAddress = AllocatePool (sizeof (EFI_IPv6_ADDRESS));
  747. if (NewDnsCache->DnsCache.IpAddress == NULL) {
  748. FreePool (NewDnsCache->DnsCache.HostName);
  749. FreePool (NewDnsCache);
  750. return EFI_OUT_OF_RESOURCES;
  751. }
  752. CopyMem (NewDnsCache->DnsCache.IpAddress, DnsCacheEntry.IpAddress, sizeof (EFI_IPv6_ADDRESS));
  753. NewDnsCache->DnsCache.Timeout = DnsCacheEntry.Timeout;
  754. InsertTailList (Dns6CacheList, &NewDnsCache->AllCacheLink);
  755. return EFI_SUCCESS;
  756. }
  757. /**
  758. Add Dns4 ServerIp to common list of addresses of all configured DNSv4 server.
  759. @param Dns4ServerList Common list of addresses of all configured DNSv4 server.
  760. @param ServerIp DNS server Ip.
  761. @retval EFI_SUCCESS Add Dns4 ServerIp to common list successfully.
  762. @retval Others Failed to add Dns4 ServerIp to common list.
  763. **/
  764. EFI_STATUS
  765. EFIAPI
  766. AddDns4ServerIp (
  767. IN LIST_ENTRY *Dns4ServerList,
  768. IN EFI_IPv4_ADDRESS ServerIp
  769. )
  770. {
  771. DNS4_SERVER_IP *NewServerIp;
  772. DNS4_SERVER_IP *Item;
  773. LIST_ENTRY *Entry;
  774. LIST_ENTRY *Next;
  775. NewServerIp = NULL;
  776. Item = NULL;
  777. //
  778. // Search the database for the matching ServerIp
  779. //
  780. NET_LIST_FOR_EACH_SAFE (Entry, Next, Dns4ServerList) {
  781. Item = NET_LIST_USER_STRUCT (Entry, DNS4_SERVER_IP, AllServerLink);
  782. if (CompareMem (&Item->Dns4ServerIp, &ServerIp, sizeof (EFI_IPv4_ADDRESS)) == 0) {
  783. //
  784. // Already done.
  785. //
  786. return EFI_SUCCESS;
  787. }
  788. }
  789. //
  790. // Add new one
  791. //
  792. NewServerIp = AllocatePool (sizeof (DNS4_SERVER_IP));
  793. if (NewServerIp == NULL) {
  794. return EFI_OUT_OF_RESOURCES;
  795. }
  796. InitializeListHead (&NewServerIp->AllServerLink);
  797. CopyMem (&NewServerIp->Dns4ServerIp, &ServerIp, sizeof (EFI_IPv4_ADDRESS));
  798. InsertTailList (Dns4ServerList, &NewServerIp->AllServerLink);
  799. return EFI_SUCCESS;
  800. }
  801. /**
  802. Add Dns6 ServerIp to common list of addresses of all configured DNSv6 server.
  803. @param Dns6ServerList Common list of addresses of all configured DNSv6 server.
  804. @param ServerIp DNS server Ip.
  805. @retval EFI_SUCCESS Add Dns6 ServerIp to common list successfully.
  806. @retval Others Failed to add Dns6 ServerIp to common list.
  807. **/
  808. EFI_STATUS
  809. EFIAPI
  810. AddDns6ServerIp (
  811. IN LIST_ENTRY *Dns6ServerList,
  812. IN EFI_IPv6_ADDRESS ServerIp
  813. )
  814. {
  815. DNS6_SERVER_IP *NewServerIp;
  816. DNS6_SERVER_IP *Item;
  817. LIST_ENTRY *Entry;
  818. LIST_ENTRY *Next;
  819. NewServerIp = NULL;
  820. Item = NULL;
  821. //
  822. // Search the database for the matching ServerIp
  823. //
  824. NET_LIST_FOR_EACH_SAFE (Entry, Next, Dns6ServerList) {
  825. Item = NET_LIST_USER_STRUCT (Entry, DNS6_SERVER_IP, AllServerLink);
  826. if (CompareMem (&Item->Dns6ServerIp, &ServerIp, sizeof (EFI_IPv6_ADDRESS)) == 0) {
  827. //
  828. // Already done.
  829. //
  830. return EFI_SUCCESS;
  831. }
  832. }
  833. //
  834. // Add new one
  835. //
  836. NewServerIp = AllocatePool (sizeof (DNS6_SERVER_IP));
  837. if (NewServerIp == NULL) {
  838. return EFI_OUT_OF_RESOURCES;
  839. }
  840. InitializeListHead (&NewServerIp->AllServerLink);
  841. CopyMem (&NewServerIp->Dns6ServerIp, &ServerIp, sizeof (EFI_IPv6_ADDRESS));
  842. InsertTailList (Dns6ServerList, &NewServerIp->AllServerLink);
  843. return EFI_SUCCESS;
  844. }
  845. /**
  846. Find out whether the response is valid or invalid.
  847. @param TokensMap All DNS transmittal Tokens entry.
  848. @param Identification Identification for queried packet.
  849. @param Type Type for queried packet.
  850. @param Class Class for queried packet.
  851. @param Item Return corresponding Token entry.
  852. @retval TRUE The response is valid.
  853. @retval FALSE The response is invalid.
  854. **/
  855. BOOLEAN
  856. IsValidDnsResponse (
  857. IN NET_MAP *TokensMap,
  858. IN UINT16 Identification,
  859. IN UINT16 Type,
  860. IN UINT16 Class,
  861. OUT NET_MAP_ITEM **Item
  862. )
  863. {
  864. LIST_ENTRY *Entry;
  865. NET_BUF *Packet;
  866. UINT8 *TxString;
  867. DNS_HEADER *DnsHeader;
  868. CHAR8 *QueryName;
  869. DNS_QUERY_SECTION *QuerySection;
  870. NET_LIST_FOR_EACH (Entry, &TokensMap->Used) {
  871. *Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
  872. Packet = (NET_BUF *)((*Item)->Value);
  873. if (Packet == NULL) {
  874. continue;
  875. } else {
  876. TxString = NetbufGetByte (Packet, 0, NULL);
  877. ASSERT (TxString != NULL);
  878. DnsHeader = (DNS_HEADER *)TxString;
  879. QueryName = (CHAR8 *)(TxString + sizeof (*DnsHeader));
  880. QuerySection = (DNS_QUERY_SECTION *)(QueryName + AsciiStrLen (QueryName) + 1);
  881. if ((NTOHS (DnsHeader->Identification) == Identification) &&
  882. (NTOHS (QuerySection->Type) == Type) &&
  883. (NTOHS (QuerySection->Class) == Class))
  884. {
  885. return TRUE;
  886. }
  887. }
  888. }
  889. *Item = NULL;
  890. return FALSE;
  891. }
  892. /**
  893. Parse Dns Response.
  894. @param Instance The DNS instance
  895. @param RxString Received buffer.
  896. @param Length Received buffer length.
  897. @param Completed Flag to indicate that Dns response is valid.
  898. @retval EFI_SUCCESS Parse Dns Response successfully.
  899. @retval Others Failed to parse Dns Response.
  900. **/
  901. EFI_STATUS
  902. ParseDnsResponse (
  903. IN OUT DNS_INSTANCE *Instance,
  904. IN UINT8 *RxString,
  905. IN UINT32 Length,
  906. OUT BOOLEAN *Completed
  907. )
  908. {
  909. DNS_HEADER *DnsHeader;
  910. CHAR8 *QueryName;
  911. UINT32 QueryNameLen;
  912. DNS_QUERY_SECTION *QuerySection;
  913. CHAR8 *AnswerName;
  914. DNS_ANSWER_SECTION *AnswerSection;
  915. UINT8 *AnswerData;
  916. NET_MAP_ITEM *Item;
  917. DNS4_TOKEN_ENTRY *Dns4TokenEntry;
  918. DNS6_TOKEN_ENTRY *Dns6TokenEntry;
  919. UINT32 IpCount;
  920. UINT32 RRCount;
  921. UINT32 AnswerSectionNum;
  922. UINT32 CNameTtl;
  923. EFI_IPv4_ADDRESS *HostAddr4;
  924. EFI_IPv6_ADDRESS *HostAddr6;
  925. EFI_DNS4_CACHE_ENTRY *Dns4CacheEntry;
  926. EFI_DNS6_CACHE_ENTRY *Dns6CacheEntry;
  927. DNS_RESOURCE_RECORD *Dns4RR;
  928. DNS6_RESOURCE_RECORD *Dns6RR;
  929. EFI_STATUS Status;
  930. UINT32 RemainingLength;
  931. EFI_TPL OldTpl;
  932. Item = NULL;
  933. Dns4TokenEntry = NULL;
  934. Dns6TokenEntry = NULL;
  935. IpCount = 0;
  936. RRCount = 0;
  937. AnswerSectionNum = 0;
  938. CNameTtl = 0;
  939. HostAddr4 = NULL;
  940. HostAddr6 = NULL;
  941. Dns4CacheEntry = NULL;
  942. Dns6CacheEntry = NULL;
  943. Dns4RR = NULL;
  944. Dns6RR = NULL;
  945. *Completed = TRUE;
  946. Status = EFI_SUCCESS;
  947. RemainingLength = Length;
  948. //
  949. // Check whether the remaining packet length is available or not.
  950. //
  951. if (RemainingLength <= sizeof (DNS_HEADER)) {
  952. *Completed = FALSE;
  953. return EFI_ABORTED;
  954. } else {
  955. RemainingLength -= sizeof (DNS_HEADER);
  956. }
  957. //
  958. // Get header
  959. //
  960. DnsHeader = (DNS_HEADER *)RxString;
  961. DnsHeader->Identification = NTOHS (DnsHeader->Identification);
  962. DnsHeader->Flags.Uint16 = NTOHS (DnsHeader->Flags.Uint16);
  963. DnsHeader->QuestionsNum = NTOHS (DnsHeader->QuestionsNum);
  964. DnsHeader->AnswersNum = NTOHS (DnsHeader->AnswersNum);
  965. DnsHeader->AuthorityNum = NTOHS (DnsHeader->AuthorityNum);
  966. DnsHeader->AditionalNum = NTOHS (DnsHeader->AditionalNum);
  967. //
  968. // There is always one QuestionsNum in DNS message. The capability to handle more
  969. // than one requires to redesign the message format. Currently, it's not supported.
  970. //
  971. if (DnsHeader->QuestionsNum > 1) {
  972. *Completed = FALSE;
  973. return EFI_UNSUPPORTED;
  974. }
  975. //
  976. // Get Query name
  977. //
  978. QueryName = (CHAR8 *)(RxString + sizeof (*DnsHeader));
  979. QueryNameLen = (UINT32)AsciiStrLen (QueryName) + 1;
  980. //
  981. // Check whether the remaining packet length is available or not.
  982. //
  983. if (RemainingLength <= QueryNameLen + sizeof (DNS_QUERY_SECTION)) {
  984. *Completed = FALSE;
  985. return EFI_ABORTED;
  986. } else {
  987. RemainingLength -= (QueryNameLen + sizeof (DNS_QUERY_SECTION));
  988. }
  989. //
  990. // Get query section
  991. //
  992. QuerySection = (DNS_QUERY_SECTION *)(QueryName + QueryNameLen);
  993. QuerySection->Type = NTOHS (QuerySection->Type);
  994. QuerySection->Class = NTOHS (QuerySection->Class);
  995. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  996. //
  997. // Check DnsResponse Validity, if so, also get a valid NET_MAP_ITEM.
  998. //
  999. if (Instance->Service->IpVersion == IP_VERSION_4) {
  1000. if (!IsValidDnsResponse (
  1001. &Instance->Dns4TxTokens,
  1002. DnsHeader->Identification,
  1003. QuerySection->Type,
  1004. QuerySection->Class,
  1005. &Item
  1006. ))
  1007. {
  1008. *Completed = FALSE;
  1009. Status = EFI_ABORTED;
  1010. goto ON_EXIT;
  1011. }
  1012. ASSERT (Item != NULL);
  1013. Dns4TokenEntry = (DNS4_TOKEN_ENTRY *)(Item->Key);
  1014. } else {
  1015. if (!IsValidDnsResponse (
  1016. &Instance->Dns6TxTokens,
  1017. DnsHeader->Identification,
  1018. QuerySection->Type,
  1019. QuerySection->Class,
  1020. &Item
  1021. ))
  1022. {
  1023. *Completed = FALSE;
  1024. Status = EFI_ABORTED;
  1025. goto ON_EXIT;
  1026. }
  1027. ASSERT (Item != NULL);
  1028. Dns6TokenEntry = (DNS6_TOKEN_ENTRY *)(Item->Key);
  1029. }
  1030. //
  1031. // Continue Check Some Errors.
  1032. //
  1033. if ((DnsHeader->Flags.Bits.RCode != DNS_FLAGS_RCODE_NO_ERROR) || (DnsHeader->AnswersNum < 1) || \
  1034. (DnsHeader->Flags.Bits.QR != DNS_FLAGS_QR_RESPONSE))
  1035. {
  1036. //
  1037. // The domain name referenced in the query does not exist.
  1038. //
  1039. if (DnsHeader->Flags.Bits.RCode == DNS_FLAGS_RCODE_NAME_ERROR) {
  1040. Status = EFI_NOT_FOUND;
  1041. } else {
  1042. Status = EFI_DEVICE_ERROR;
  1043. }
  1044. goto ON_COMPLETE;
  1045. }
  1046. //
  1047. // Do some buffer allocations.
  1048. //
  1049. if (Instance->Service->IpVersion == IP_VERSION_4) {
  1050. ASSERT (Dns4TokenEntry != NULL);
  1051. if (Dns4TokenEntry->GeneralLookUp) {
  1052. //
  1053. // It's the GeneralLookUp querying.
  1054. //
  1055. Dns4TokenEntry->Token->RspData.GLookupData = AllocateZeroPool (sizeof (DNS_RESOURCE_RECORD));
  1056. if (Dns4TokenEntry->Token->RspData.GLookupData == NULL) {
  1057. Status = EFI_OUT_OF_RESOURCES;
  1058. goto ON_EXIT;
  1059. }
  1060. Dns4TokenEntry->Token->RspData.GLookupData->RRList = AllocateZeroPool (DnsHeader->AnswersNum * sizeof (DNS_RESOURCE_RECORD));
  1061. if (Dns4TokenEntry->Token->RspData.GLookupData->RRList == NULL) {
  1062. Status = EFI_OUT_OF_RESOURCES;
  1063. goto ON_EXIT;
  1064. }
  1065. } else {
  1066. //
  1067. // It's not the GeneralLookUp querying. Check the Query type.
  1068. //
  1069. if (QuerySection->Type == DNS_TYPE_A) {
  1070. Dns4TokenEntry->Token->RspData.H2AData = AllocateZeroPool (sizeof (DNS_HOST_TO_ADDR_DATA));
  1071. if (Dns4TokenEntry->Token->RspData.H2AData == NULL) {
  1072. Status = EFI_OUT_OF_RESOURCES;
  1073. goto ON_EXIT;
  1074. }
  1075. Dns4TokenEntry->Token->RspData.H2AData->IpList = AllocateZeroPool (DnsHeader->AnswersNum * sizeof (EFI_IPv4_ADDRESS));
  1076. if (Dns4TokenEntry->Token->RspData.H2AData->IpList == NULL) {
  1077. Status = EFI_OUT_OF_RESOURCES;
  1078. goto ON_EXIT;
  1079. }
  1080. } else {
  1081. Status = EFI_UNSUPPORTED;
  1082. goto ON_EXIT;
  1083. }
  1084. }
  1085. } else {
  1086. ASSERT (Dns6TokenEntry != NULL);
  1087. if (Dns6TokenEntry->GeneralLookUp) {
  1088. //
  1089. // It's the GeneralLookUp querying.
  1090. //
  1091. Dns6TokenEntry->Token->RspData.GLookupData = AllocateZeroPool (sizeof (DNS_RESOURCE_RECORD));
  1092. if (Dns6TokenEntry->Token->RspData.GLookupData == NULL) {
  1093. Status = EFI_OUT_OF_RESOURCES;
  1094. goto ON_EXIT;
  1095. }
  1096. Dns6TokenEntry->Token->RspData.GLookupData->RRList = AllocateZeroPool (DnsHeader->AnswersNum * sizeof (DNS_RESOURCE_RECORD));
  1097. if (Dns6TokenEntry->Token->RspData.GLookupData->RRList == NULL) {
  1098. Status = EFI_OUT_OF_RESOURCES;
  1099. goto ON_EXIT;
  1100. }
  1101. } else {
  1102. //
  1103. // It's not the GeneralLookUp querying. Check the Query type.
  1104. //
  1105. if (QuerySection->Type == DNS_TYPE_AAAA) {
  1106. Dns6TokenEntry->Token->RspData.H2AData = AllocateZeroPool (sizeof (DNS6_HOST_TO_ADDR_DATA));
  1107. if (Dns6TokenEntry->Token->RspData.H2AData == NULL) {
  1108. Status = EFI_OUT_OF_RESOURCES;
  1109. goto ON_EXIT;
  1110. }
  1111. Dns6TokenEntry->Token->RspData.H2AData->IpList = AllocateZeroPool (DnsHeader->AnswersNum * sizeof (EFI_IPv6_ADDRESS));
  1112. if (Dns6TokenEntry->Token->RspData.H2AData->IpList == NULL) {
  1113. Status = EFI_OUT_OF_RESOURCES;
  1114. goto ON_EXIT;
  1115. }
  1116. } else {
  1117. Status = EFI_UNSUPPORTED;
  1118. goto ON_EXIT;
  1119. }
  1120. }
  1121. }
  1122. Status = EFI_NOT_FOUND;
  1123. //
  1124. // Get Answer name
  1125. //
  1126. AnswerName = (CHAR8 *)QuerySection + sizeof (*QuerySection);
  1127. //
  1128. // Processing AnswerSection.
  1129. //
  1130. while (AnswerSectionNum < DnsHeader->AnswersNum) {
  1131. //
  1132. // Check whether the remaining packet length is available or not.
  1133. //
  1134. if (RemainingLength <= sizeof (UINT16) + sizeof (DNS_ANSWER_SECTION)) {
  1135. *Completed = FALSE;
  1136. Status = EFI_ABORTED;
  1137. goto ON_EXIT;
  1138. } else {
  1139. RemainingLength -= (sizeof (UINT16) + sizeof (DNS_ANSWER_SECTION));
  1140. }
  1141. //
  1142. // Answer name should be PTR, else EFI_UNSUPPORTED returned.
  1143. //
  1144. if ((*(UINT8 *)AnswerName & 0xC0) != 0xC0) {
  1145. Status = EFI_UNSUPPORTED;
  1146. goto ON_EXIT;
  1147. }
  1148. //
  1149. // Get Answer section.
  1150. //
  1151. AnswerSection = (DNS_ANSWER_SECTION *)(AnswerName + sizeof (UINT16));
  1152. AnswerSection->Type = NTOHS (AnswerSection->Type);
  1153. AnswerSection->Class = NTOHS (AnswerSection->Class);
  1154. AnswerSection->Ttl = NTOHL (AnswerSection->Ttl);
  1155. AnswerSection->DataLength = NTOHS (AnswerSection->DataLength);
  1156. //
  1157. // Check whether the remaining packet length is available or not.
  1158. //
  1159. if (RemainingLength < AnswerSection->DataLength) {
  1160. *Completed = FALSE;
  1161. Status = EFI_ABORTED;
  1162. goto ON_EXIT;
  1163. } else {
  1164. RemainingLength -= AnswerSection->DataLength;
  1165. }
  1166. //
  1167. // Check whether it's the GeneralLookUp querying.
  1168. //
  1169. if ((Instance->Service->IpVersion == IP_VERSION_4) && Dns4TokenEntry->GeneralLookUp) {
  1170. Dns4RR = Dns4TokenEntry->Token->RspData.GLookupData->RRList;
  1171. AnswerData = (UINT8 *)AnswerSection + sizeof (*AnswerSection);
  1172. //
  1173. // Fill the ResourceRecord.
  1174. //
  1175. Dns4RR[RRCount].QName = AllocateZeroPool (AsciiStrLen (QueryName) + 1);
  1176. if (Dns4RR[RRCount].QName == NULL) {
  1177. Status = EFI_OUT_OF_RESOURCES;
  1178. goto ON_EXIT;
  1179. }
  1180. CopyMem (Dns4RR[RRCount].QName, QueryName, AsciiStrLen (QueryName));
  1181. Dns4RR[RRCount].QType = AnswerSection->Type;
  1182. Dns4RR[RRCount].QClass = AnswerSection->Class;
  1183. Dns4RR[RRCount].TTL = AnswerSection->Ttl;
  1184. Dns4RR[RRCount].DataLength = AnswerSection->DataLength;
  1185. Dns4RR[RRCount].RData = AllocateZeroPool (Dns4RR[RRCount].DataLength);
  1186. if (Dns4RR[RRCount].RData == NULL) {
  1187. Status = EFI_OUT_OF_RESOURCES;
  1188. goto ON_EXIT;
  1189. }
  1190. CopyMem (Dns4RR[RRCount].RData, AnswerData, Dns4RR[RRCount].DataLength);
  1191. RRCount++;
  1192. Status = EFI_SUCCESS;
  1193. } else if ((Instance->Service->IpVersion == IP_VERSION_6) && Dns6TokenEntry->GeneralLookUp) {
  1194. Dns6RR = Dns6TokenEntry->Token->RspData.GLookupData->RRList;
  1195. AnswerData = (UINT8 *)AnswerSection + sizeof (*AnswerSection);
  1196. //
  1197. // Fill the ResourceRecord.
  1198. //
  1199. Dns6RR[RRCount].QName = AllocateZeroPool (AsciiStrLen (QueryName) + 1);
  1200. if (Dns6RR[RRCount].QName == NULL) {
  1201. Status = EFI_OUT_OF_RESOURCES;
  1202. goto ON_EXIT;
  1203. }
  1204. CopyMem (Dns6RR[RRCount].QName, QueryName, AsciiStrLen (QueryName));
  1205. Dns6RR[RRCount].QType = AnswerSection->Type;
  1206. Dns6RR[RRCount].QClass = AnswerSection->Class;
  1207. Dns6RR[RRCount].TTL = AnswerSection->Ttl;
  1208. Dns6RR[RRCount].DataLength = AnswerSection->DataLength;
  1209. Dns6RR[RRCount].RData = AllocateZeroPool (Dns6RR[RRCount].DataLength);
  1210. if (Dns6RR[RRCount].RData == NULL) {
  1211. Status = EFI_OUT_OF_RESOURCES;
  1212. goto ON_EXIT;
  1213. }
  1214. CopyMem (Dns6RR[RRCount].RData, AnswerData, Dns6RR[RRCount].DataLength);
  1215. RRCount++;
  1216. Status = EFI_SUCCESS;
  1217. } else {
  1218. //
  1219. // It's not the GeneralLookUp querying.
  1220. // Check the Query type, parse the response packet.
  1221. //
  1222. switch (AnswerSection->Type) {
  1223. case DNS_TYPE_A:
  1224. //
  1225. // This is address entry, get Data.
  1226. //
  1227. ASSERT (Dns4TokenEntry != NULL);
  1228. if (AnswerSection->DataLength != 4) {
  1229. Status = EFI_ABORTED;
  1230. goto ON_EXIT;
  1231. }
  1232. HostAddr4 = Dns4TokenEntry->Token->RspData.H2AData->IpList;
  1233. AnswerData = (UINT8 *)AnswerSection + sizeof (*AnswerSection);
  1234. CopyMem (&HostAddr4[IpCount], AnswerData, sizeof (EFI_IPv4_ADDRESS));
  1235. //
  1236. // Allocate new CacheEntry pool to update DNS cache dynamically.
  1237. //
  1238. Dns4CacheEntry = AllocateZeroPool (sizeof (EFI_DNS4_CACHE_ENTRY));
  1239. if (Dns4CacheEntry == NULL) {
  1240. Status = EFI_OUT_OF_RESOURCES;
  1241. goto ON_EXIT;
  1242. }
  1243. Dns4CacheEntry->HostName = AllocateZeroPool (2 * (StrLen (Dns4TokenEntry->QueryHostName) + 1));
  1244. if (Dns4CacheEntry->HostName == NULL) {
  1245. Status = EFI_OUT_OF_RESOURCES;
  1246. goto ON_EXIT;
  1247. }
  1248. CopyMem (Dns4CacheEntry->HostName, Dns4TokenEntry->QueryHostName, 2 * (StrLen (Dns4TokenEntry->QueryHostName) + 1));
  1249. Dns4CacheEntry->IpAddress = AllocateZeroPool (sizeof (EFI_IPv4_ADDRESS));
  1250. if (Dns4CacheEntry->IpAddress == NULL) {
  1251. Status = EFI_OUT_OF_RESOURCES;
  1252. goto ON_EXIT;
  1253. }
  1254. CopyMem (Dns4CacheEntry->IpAddress, AnswerData, sizeof (EFI_IPv4_ADDRESS));
  1255. if ((CNameTtl != 0) && (AnswerSection->Ttl != 0)) {
  1256. Dns4CacheEntry->Timeout = MIN (CNameTtl, AnswerSection->Ttl);
  1257. } else {
  1258. Dns4CacheEntry->Timeout = MAX (CNameTtl, AnswerSection->Ttl);
  1259. }
  1260. UpdateDns4Cache (&mDriverData->Dns4CacheList, FALSE, TRUE, *Dns4CacheEntry);
  1261. //
  1262. // Free allocated CacheEntry pool.
  1263. //
  1264. FreePool (Dns4CacheEntry->HostName);
  1265. Dns4CacheEntry->HostName = NULL;
  1266. FreePool (Dns4CacheEntry->IpAddress);
  1267. Dns4CacheEntry->IpAddress = NULL;
  1268. FreePool (Dns4CacheEntry);
  1269. Dns4CacheEntry = NULL;
  1270. IpCount++;
  1271. Status = EFI_SUCCESS;
  1272. break;
  1273. case DNS_TYPE_AAAA:
  1274. //
  1275. // This is address entry, get Data.
  1276. //
  1277. ASSERT (Dns6TokenEntry != NULL);
  1278. if (AnswerSection->DataLength != 16) {
  1279. Status = EFI_ABORTED;
  1280. goto ON_EXIT;
  1281. }
  1282. HostAddr6 = Dns6TokenEntry->Token->RspData.H2AData->IpList;
  1283. AnswerData = (UINT8 *)AnswerSection + sizeof (*AnswerSection);
  1284. CopyMem (&HostAddr6[IpCount], AnswerData, sizeof (EFI_IPv6_ADDRESS));
  1285. //
  1286. // Allocate new CacheEntry pool to update DNS cache dynamically.
  1287. //
  1288. Dns6CacheEntry = AllocateZeroPool (sizeof (EFI_DNS6_CACHE_ENTRY));
  1289. if (Dns6CacheEntry == NULL) {
  1290. Status = EFI_OUT_OF_RESOURCES;
  1291. goto ON_EXIT;
  1292. }
  1293. Dns6CacheEntry->HostName = AllocateZeroPool (2 * (StrLen (Dns6TokenEntry->QueryHostName) + 1));
  1294. if (Dns6CacheEntry->HostName == NULL) {
  1295. Status = EFI_OUT_OF_RESOURCES;
  1296. goto ON_EXIT;
  1297. }
  1298. CopyMem (Dns6CacheEntry->HostName, Dns6TokenEntry->QueryHostName, 2 * (StrLen (Dns6TokenEntry->QueryHostName) + 1));
  1299. Dns6CacheEntry->IpAddress = AllocateZeroPool (sizeof (EFI_IPv6_ADDRESS));
  1300. if (Dns6CacheEntry->IpAddress == NULL) {
  1301. Status = EFI_OUT_OF_RESOURCES;
  1302. goto ON_EXIT;
  1303. }
  1304. CopyMem (Dns6CacheEntry->IpAddress, AnswerData, sizeof (EFI_IPv6_ADDRESS));
  1305. if ((CNameTtl != 0) && (AnswerSection->Ttl != 0)) {
  1306. Dns6CacheEntry->Timeout = MIN (CNameTtl, AnswerSection->Ttl);
  1307. } else {
  1308. Dns6CacheEntry->Timeout = MAX (CNameTtl, AnswerSection->Ttl);
  1309. }
  1310. UpdateDns6Cache (&mDriverData->Dns6CacheList, FALSE, TRUE, *Dns6CacheEntry);
  1311. //
  1312. // Free allocated CacheEntry pool.
  1313. //
  1314. FreePool (Dns6CacheEntry->HostName);
  1315. Dns6CacheEntry->HostName = NULL;
  1316. FreePool (Dns6CacheEntry->IpAddress);
  1317. Dns6CacheEntry->IpAddress = NULL;
  1318. FreePool (Dns6CacheEntry);
  1319. Dns6CacheEntry = NULL;
  1320. IpCount++;
  1321. Status = EFI_SUCCESS;
  1322. break;
  1323. case DNS_TYPE_CNAME:
  1324. //
  1325. // According RFC 1034 - 3.6.2, if the query name is an alias, the name server will include the CNAME
  1326. // record in the response and restart the query at the domain name specified in the data field of the
  1327. // CNAME record. So, just record the TTL value of the CNAME, then skip to parse the next record.
  1328. //
  1329. CNameTtl = AnswerSection->Ttl;
  1330. break;
  1331. default:
  1332. Status = EFI_UNSUPPORTED;
  1333. goto ON_EXIT;
  1334. }
  1335. }
  1336. //
  1337. // Find next one
  1338. //
  1339. AnswerName = (CHAR8 *)AnswerSection + sizeof (*AnswerSection) + AnswerSection->DataLength;
  1340. AnswerSectionNum++;
  1341. }
  1342. if (Instance->Service->IpVersion == IP_VERSION_4) {
  1343. ASSERT (Dns4TokenEntry != NULL);
  1344. if (Dns4TokenEntry->GeneralLookUp) {
  1345. Dns4TokenEntry->Token->RspData.GLookupData->RRCount = RRCount;
  1346. } else {
  1347. if (QuerySection->Type == DNS_TYPE_A) {
  1348. Dns4TokenEntry->Token->RspData.H2AData->IpCount = IpCount;
  1349. } else {
  1350. Status = EFI_UNSUPPORTED;
  1351. goto ON_EXIT;
  1352. }
  1353. }
  1354. } else {
  1355. ASSERT (Dns6TokenEntry != NULL);
  1356. if (Dns6TokenEntry->GeneralLookUp) {
  1357. Dns6TokenEntry->Token->RspData.GLookupData->RRCount = RRCount;
  1358. } else {
  1359. if (QuerySection->Type == DNS_TYPE_AAAA) {
  1360. Dns6TokenEntry->Token->RspData.H2AData->IpCount = IpCount;
  1361. } else {
  1362. Status = EFI_UNSUPPORTED;
  1363. goto ON_EXIT;
  1364. }
  1365. }
  1366. }
  1367. ON_COMPLETE:
  1368. //
  1369. // Parsing is complete, free the sending packet and signal Event here.
  1370. //
  1371. if ((Item != NULL) && (Item->Value != NULL)) {
  1372. NetbufFree ((NET_BUF *)(Item->Value));
  1373. }
  1374. if (Instance->Service->IpVersion == IP_VERSION_4) {
  1375. ASSERT (Dns4TokenEntry != NULL);
  1376. Dns4RemoveTokenEntry (&Instance->Dns4TxTokens, Dns4TokenEntry);
  1377. Dns4TokenEntry->Token->Status = Status;
  1378. if (Dns4TokenEntry->Token->Event != NULL) {
  1379. gBS->SignalEvent (Dns4TokenEntry->Token->Event);
  1380. DispatchDpc ();
  1381. }
  1382. } else {
  1383. ASSERT (Dns6TokenEntry != NULL);
  1384. Dns6RemoveTokenEntry (&Instance->Dns6TxTokens, Dns6TokenEntry);
  1385. Dns6TokenEntry->Token->Status = Status;
  1386. if (Dns6TokenEntry->Token->Event != NULL) {
  1387. gBS->SignalEvent (Dns6TokenEntry->Token->Event);
  1388. DispatchDpc ();
  1389. }
  1390. }
  1391. ON_EXIT:
  1392. //
  1393. // Free the allocated buffer if error happen.
  1394. //
  1395. if (EFI_ERROR (Status)) {
  1396. if (Dns4TokenEntry != NULL) {
  1397. if (Dns4TokenEntry->GeneralLookUp) {
  1398. if (Dns4TokenEntry->Token->RspData.GLookupData != NULL) {
  1399. if (Dns4TokenEntry->Token->RspData.GLookupData->RRList != NULL) {
  1400. while (RRCount != 0) {
  1401. RRCount--;
  1402. if (Dns4TokenEntry->Token->RspData.GLookupData->RRList[RRCount].QName != NULL) {
  1403. FreePool (Dns4TokenEntry->Token->RspData.GLookupData->RRList[RRCount].QName);
  1404. }
  1405. if (Dns4TokenEntry->Token->RspData.GLookupData->RRList[RRCount].RData != NULL) {
  1406. FreePool (Dns4TokenEntry->Token->RspData.GLookupData->RRList[RRCount].RData);
  1407. }
  1408. }
  1409. FreePool (Dns4TokenEntry->Token->RspData.GLookupData->RRList);
  1410. }
  1411. FreePool (Dns4TokenEntry->Token->RspData.GLookupData);
  1412. }
  1413. } else {
  1414. if ((QuerySection->Type == DNS_TYPE_A) && (Dns4TokenEntry->Token->RspData.H2AData != NULL)) {
  1415. if (Dns4TokenEntry->Token->RspData.H2AData->IpList != NULL) {
  1416. FreePool (Dns4TokenEntry->Token->RspData.H2AData->IpList);
  1417. }
  1418. FreePool (Dns4TokenEntry->Token->RspData.H2AData);
  1419. Dns4TokenEntry->Token->RspData.H2AData = NULL;
  1420. }
  1421. }
  1422. }
  1423. if (Dns6TokenEntry != NULL) {
  1424. if (Dns6TokenEntry->GeneralLookUp) {
  1425. if (Dns6TokenEntry->Token->RspData.GLookupData != NULL) {
  1426. if (Dns6TokenEntry->Token->RspData.GLookupData->RRList != NULL) {
  1427. while (RRCount != 0) {
  1428. RRCount--;
  1429. if (Dns6TokenEntry->Token->RspData.GLookupData->RRList[RRCount].QName != NULL) {
  1430. FreePool (Dns6TokenEntry->Token->RspData.GLookupData->RRList[RRCount].QName);
  1431. }
  1432. if (Dns6TokenEntry->Token->RspData.GLookupData->RRList[RRCount].RData != NULL) {
  1433. FreePool (Dns6TokenEntry->Token->RspData.GLookupData->RRList[RRCount].RData);
  1434. }
  1435. }
  1436. FreePool (Dns6TokenEntry->Token->RspData.GLookupData->RRList);
  1437. }
  1438. FreePool (Dns6TokenEntry->Token->RspData.GLookupData);
  1439. }
  1440. } else {
  1441. if ((QuerySection->Type == DNS_TYPE_AAAA) && (Dns6TokenEntry->Token->RspData.H2AData != NULL)) {
  1442. if (Dns6TokenEntry->Token->RspData.H2AData->IpList != NULL) {
  1443. FreePool (Dns6TokenEntry->Token->RspData.H2AData->IpList);
  1444. }
  1445. FreePool (Dns6TokenEntry->Token->RspData.H2AData);
  1446. Dns6TokenEntry->Token->RspData.H2AData = NULL;
  1447. }
  1448. }
  1449. }
  1450. if (Dns4CacheEntry != NULL) {
  1451. if (Dns4CacheEntry->HostName != NULL) {
  1452. FreePool (Dns4CacheEntry->HostName);
  1453. }
  1454. if (Dns4CacheEntry->IpAddress != NULL) {
  1455. FreePool (Dns4CacheEntry->IpAddress);
  1456. }
  1457. FreePool (Dns4CacheEntry);
  1458. }
  1459. if (Dns6CacheEntry != NULL) {
  1460. if (Dns6CacheEntry->HostName != NULL) {
  1461. FreePool (Dns6CacheEntry->HostName);
  1462. }
  1463. if (Dns6CacheEntry->IpAddress != NULL) {
  1464. FreePool (Dns6CacheEntry->IpAddress);
  1465. }
  1466. FreePool (Dns6CacheEntry);
  1467. }
  1468. }
  1469. gBS->RestoreTPL (OldTpl);
  1470. return Status;
  1471. }
  1472. /**
  1473. Parse response packet.
  1474. @param Packet The packets received.
  1475. @param EndPoint The local/remote UDP access point
  1476. @param IoStatus The status of the UDP receive
  1477. @param Context The opaque parameter to the function.
  1478. **/
  1479. VOID
  1480. EFIAPI
  1481. DnsOnPacketReceived (
  1482. NET_BUF *Packet,
  1483. UDP_END_POINT *EndPoint,
  1484. EFI_STATUS IoStatus,
  1485. VOID *Context
  1486. )
  1487. {
  1488. DNS_INSTANCE *Instance;
  1489. UINT8 *RcvString;
  1490. UINT32 Len;
  1491. BOOLEAN Completed;
  1492. Instance = (DNS_INSTANCE *)Context;
  1493. NET_CHECK_SIGNATURE (Instance, DNS_INSTANCE_SIGNATURE);
  1494. RcvString = NULL;
  1495. Completed = FALSE;
  1496. if (EFI_ERROR (IoStatus)) {
  1497. goto ON_EXIT;
  1498. }
  1499. ASSERT (Packet != NULL);
  1500. Len = Packet->TotalSize;
  1501. RcvString = NetbufGetByte (Packet, 0, NULL);
  1502. ASSERT (RcvString != NULL);
  1503. //
  1504. // Parse Dns Response
  1505. //
  1506. ParseDnsResponse (Instance, RcvString, Len, &Completed);
  1507. ON_EXIT:
  1508. if (Packet != NULL) {
  1509. NetbufFree (Packet);
  1510. }
  1511. if (!Completed) {
  1512. UdpIoRecvDatagram (Instance->UdpIo, DnsOnPacketReceived, Instance, 0);
  1513. }
  1514. }
  1515. /**
  1516. Release the net buffer when packet is sent.
  1517. @param Packet The packets received.
  1518. @param EndPoint The local/remote UDP access point
  1519. @param IoStatus The status of the UDP receive
  1520. @param Context The opaque parameter to the function.
  1521. **/
  1522. VOID
  1523. EFIAPI
  1524. DnsOnPacketSent (
  1525. NET_BUF *Packet,
  1526. UDP_END_POINT *EndPoint,
  1527. EFI_STATUS IoStatus,
  1528. VOID *Context
  1529. )
  1530. {
  1531. DNS_INSTANCE *Instance;
  1532. LIST_ENTRY *Entry;
  1533. NET_MAP_ITEM *Item;
  1534. DNS4_TOKEN_ENTRY *Dns4TokenEntry;
  1535. DNS6_TOKEN_ENTRY *Dns6TokenEntry;
  1536. Dns4TokenEntry = NULL;
  1537. Dns6TokenEntry = NULL;
  1538. Instance = (DNS_INSTANCE *)Context;
  1539. NET_CHECK_SIGNATURE (Instance, DNS_INSTANCE_SIGNATURE);
  1540. if (Instance->Service->IpVersion == IP_VERSION_4) {
  1541. NET_LIST_FOR_EACH (Entry, &Instance->Dns4TxTokens.Used) {
  1542. Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
  1543. if (Packet == (NET_BUF *)(Item->Value)) {
  1544. Dns4TokenEntry = ((DNS4_TOKEN_ENTRY *)Item->Key);
  1545. Dns4TokenEntry->PacketToLive = Dns4TokenEntry->Token->RetryInterval;
  1546. break;
  1547. }
  1548. }
  1549. } else {
  1550. NET_LIST_FOR_EACH (Entry, &Instance->Dns6TxTokens.Used) {
  1551. Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
  1552. if (Packet == (NET_BUF *)(Item->Value)) {
  1553. Dns6TokenEntry = ((DNS6_TOKEN_ENTRY *)Item->Key);
  1554. Dns6TokenEntry->PacketToLive = Dns6TokenEntry->Token->RetryInterval;
  1555. break;
  1556. }
  1557. }
  1558. }
  1559. NetbufFree (Packet);
  1560. }
  1561. /**
  1562. Query request information.
  1563. @param Instance The DNS instance
  1564. @param Packet The packet for querying request information.
  1565. @retval EFI_SUCCESS Query request information successfully.
  1566. @retval Others Failed to query request information.
  1567. **/
  1568. EFI_STATUS
  1569. DoDnsQuery (
  1570. IN DNS_INSTANCE *Instance,
  1571. IN NET_BUF *Packet
  1572. )
  1573. {
  1574. EFI_STATUS Status;
  1575. //
  1576. // Ready to receive the DNS response.
  1577. //
  1578. if (Instance->UdpIo->RecvRequest == NULL) {
  1579. Status = UdpIoRecvDatagram (Instance->UdpIo, DnsOnPacketReceived, Instance, 0);
  1580. if (EFI_ERROR (Status)) {
  1581. return Status;
  1582. }
  1583. }
  1584. //
  1585. // Transmit the DNS packet.
  1586. //
  1587. NET_GET_REF (Packet);
  1588. Status = UdpIoSendDatagram (Instance->UdpIo, Packet, NULL, NULL, DnsOnPacketSent, Instance);
  1589. return Status;
  1590. }
  1591. /**
  1592. Construct the Packet according query section.
  1593. @param Instance The DNS instance
  1594. @param QueryName Queried Name
  1595. @param Type Queried Type
  1596. @param Class Queried Class
  1597. @param Packet The packet for query
  1598. @retval EFI_SUCCESS The packet is constructed.
  1599. @retval Others Failed to construct the Packet.
  1600. **/
  1601. EFI_STATUS
  1602. ConstructDNSQuery (
  1603. IN DNS_INSTANCE *Instance,
  1604. IN CHAR8 *QueryName,
  1605. IN UINT16 Type,
  1606. IN UINT16 Class,
  1607. OUT NET_BUF **Packet
  1608. )
  1609. {
  1610. NET_FRAGMENT Frag;
  1611. DNS_HEADER *DnsHeader;
  1612. DNS_QUERY_SECTION *DnsQuery;
  1613. //
  1614. // Messages carried by UDP are restricted to 512 bytes (not counting the IP
  1615. // or UDP headers).
  1616. //
  1617. Frag.Bulk = AllocatePool (DNS_MAX_MESSAGE_SIZE * sizeof (UINT8));
  1618. if (Frag.Bulk == NULL) {
  1619. return EFI_OUT_OF_RESOURCES;
  1620. }
  1621. //
  1622. // Fill header
  1623. //
  1624. DnsHeader = (DNS_HEADER *)Frag.Bulk;
  1625. DnsHeader->Identification = (UINT16)NET_RANDOM (NetRandomInitSeed ());
  1626. DnsHeader->Flags.Uint16 = 0x0000;
  1627. DnsHeader->Flags.Bits.RD = 1;
  1628. DnsHeader->Flags.Bits.OpCode = DNS_FLAGS_OPCODE_STANDARD;
  1629. DnsHeader->Flags.Bits.QR = DNS_FLAGS_QR_QUERY;
  1630. DnsHeader->QuestionsNum = 1;
  1631. DnsHeader->AnswersNum = 0;
  1632. DnsHeader->AuthorityNum = 0;
  1633. DnsHeader->AditionalNum = 0;
  1634. DnsHeader->Identification = HTONS (DnsHeader->Identification);
  1635. DnsHeader->Flags.Uint16 = HTONS (DnsHeader->Flags.Uint16);
  1636. DnsHeader->QuestionsNum = HTONS (DnsHeader->QuestionsNum);
  1637. DnsHeader->AnswersNum = HTONS (DnsHeader->AnswersNum);
  1638. DnsHeader->AuthorityNum = HTONS (DnsHeader->AuthorityNum);
  1639. DnsHeader->AditionalNum = HTONS (DnsHeader->AditionalNum);
  1640. Frag.Len = sizeof (*DnsHeader);
  1641. //
  1642. // Fill Query name
  1643. //
  1644. CopyMem (Frag.Bulk + Frag.Len, QueryName, AsciiStrLen (QueryName));
  1645. Frag.Len = (UINT32)(Frag.Len + AsciiStrLen (QueryName));
  1646. *(Frag.Bulk + Frag.Len) = 0;
  1647. Frag.Len++;
  1648. //
  1649. // Rest query section
  1650. //
  1651. DnsQuery = (DNS_QUERY_SECTION *)(Frag.Bulk + Frag.Len);
  1652. DnsQuery->Type = HTONS (Type);
  1653. DnsQuery->Class = HTONS (Class);
  1654. Frag.Len += sizeof (*DnsQuery);
  1655. //
  1656. // Wrap the Frag in a net buffer.
  1657. //
  1658. *Packet = NetbufFromExt (&Frag, 1, 0, 0, DnsDummyExtFree, NULL);
  1659. if (*Packet == NULL) {
  1660. FreePool (Frag.Bulk);
  1661. return EFI_OUT_OF_RESOURCES;
  1662. }
  1663. //
  1664. // Store the UdpIo in ProtoData.
  1665. //
  1666. *((UINTN *)&((*Packet)->ProtoData[0])) = (UINTN)(Instance->UdpIo);
  1667. return EFI_SUCCESS;
  1668. }
  1669. /**
  1670. Retransmit the packet.
  1671. @param Instance The DNS instance
  1672. @param Packet Retransmit the packet
  1673. @retval EFI_SUCCESS The packet is retransmitted.
  1674. @retval Others Failed to retransmit.
  1675. **/
  1676. EFI_STATUS
  1677. DnsRetransmit (
  1678. IN DNS_INSTANCE *Instance,
  1679. IN NET_BUF *Packet
  1680. )
  1681. {
  1682. EFI_STATUS Status;
  1683. UINT8 *Buffer;
  1684. ASSERT (Packet != NULL);
  1685. //
  1686. // Set the requests to the listening port, other packets to the connected port
  1687. //
  1688. Buffer = NetbufGetByte (Packet, 0, NULL);
  1689. ASSERT (Buffer != NULL);
  1690. NET_GET_REF (Packet);
  1691. Status = UdpIoSendDatagram (
  1692. Instance->UdpIo,
  1693. Packet,
  1694. NULL,
  1695. NULL,
  1696. DnsOnPacketSent,
  1697. Instance
  1698. );
  1699. if (EFI_ERROR (Status)) {
  1700. NET_PUT_REF (Packet);
  1701. }
  1702. return Status;
  1703. }
  1704. /**
  1705. The timer ticking function for the DNS services.
  1706. @param Event The ticking event
  1707. @param Context The DNS service instance
  1708. **/
  1709. VOID
  1710. EFIAPI
  1711. DnsOnTimerRetransmit (
  1712. IN EFI_EVENT Event,
  1713. IN VOID *Context
  1714. )
  1715. {
  1716. DNS_SERVICE *Service;
  1717. LIST_ENTRY *Entry;
  1718. LIST_ENTRY *Next;
  1719. DNS_INSTANCE *Instance;
  1720. LIST_ENTRY *EntryNetMap;
  1721. NET_MAP_ITEM *ItemNetMap;
  1722. DNS4_TOKEN_ENTRY *Dns4TokenEntry;
  1723. DNS6_TOKEN_ENTRY *Dns6TokenEntry;
  1724. Dns4TokenEntry = NULL;
  1725. Dns6TokenEntry = NULL;
  1726. Service = (DNS_SERVICE *)Context;
  1727. if (Service->IpVersion == IP_VERSION_4) {
  1728. //
  1729. // Iterate through all the children of the DNS service instance. Time
  1730. // out the packet. If maximum retries reached, clean the Token up.
  1731. //
  1732. NET_LIST_FOR_EACH_SAFE (Entry, Next, &Service->Dns4ChildrenList) {
  1733. Instance = NET_LIST_USER_STRUCT (Entry, DNS_INSTANCE, Link);
  1734. EntryNetMap = Instance->Dns4TxTokens.Used.ForwardLink;
  1735. while (EntryNetMap != &Instance->Dns4TxTokens.Used) {
  1736. ItemNetMap = NET_LIST_USER_STRUCT (EntryNetMap, NET_MAP_ITEM, Link);
  1737. Dns4TokenEntry = (DNS4_TOKEN_ENTRY *)(ItemNetMap->Key);
  1738. if ((Dns4TokenEntry->PacketToLive == 0) || (--Dns4TokenEntry->PacketToLive > 0)) {
  1739. EntryNetMap = EntryNetMap->ForwardLink;
  1740. continue;
  1741. }
  1742. //
  1743. // Retransmit the packet if haven't reach the maximum retry count,
  1744. // otherwise exit the transfer.
  1745. //
  1746. if (++Dns4TokenEntry->RetryCounting <= Dns4TokenEntry->Token->RetryCount) {
  1747. DnsRetransmit (Instance, (NET_BUF *)ItemNetMap->Value);
  1748. EntryNetMap = EntryNetMap->ForwardLink;
  1749. } else {
  1750. //
  1751. // Maximum retries reached, clean the Token up.
  1752. //
  1753. Dns4RemoveTokenEntry (&Instance->Dns4TxTokens, Dns4TokenEntry);
  1754. Dns4TokenEntry->Token->Status = EFI_TIMEOUT;
  1755. gBS->SignalEvent (Dns4TokenEntry->Token->Event);
  1756. DispatchDpc ();
  1757. //
  1758. // Free the sending packet.
  1759. //
  1760. if (ItemNetMap->Value != NULL) {
  1761. NetbufFree ((NET_BUF *)(ItemNetMap->Value));
  1762. }
  1763. EntryNetMap = Instance->Dns4TxTokens.Used.ForwardLink;
  1764. }
  1765. }
  1766. }
  1767. } else {
  1768. //
  1769. // Iterate through all the children of the DNS service instance. Time
  1770. // out the packet. If maximum retries reached, clean the Token up.
  1771. //
  1772. NET_LIST_FOR_EACH_SAFE (Entry, Next, &Service->Dns6ChildrenList) {
  1773. Instance = NET_LIST_USER_STRUCT (Entry, DNS_INSTANCE, Link);
  1774. EntryNetMap = Instance->Dns6TxTokens.Used.ForwardLink;
  1775. while (EntryNetMap != &Instance->Dns6TxTokens.Used) {
  1776. ItemNetMap = NET_LIST_USER_STRUCT (EntryNetMap, NET_MAP_ITEM, Link);
  1777. Dns6TokenEntry = (DNS6_TOKEN_ENTRY *)(ItemNetMap->Key);
  1778. if ((Dns6TokenEntry->PacketToLive == 0) || (--Dns6TokenEntry->PacketToLive > 0)) {
  1779. EntryNetMap = EntryNetMap->ForwardLink;
  1780. continue;
  1781. }
  1782. //
  1783. // Retransmit the packet if haven't reach the maximum retry count,
  1784. // otherwise exit the transfer.
  1785. //
  1786. if (++Dns6TokenEntry->RetryCounting <= Dns6TokenEntry->Token->RetryCount) {
  1787. DnsRetransmit (Instance, (NET_BUF *)ItemNetMap->Value);
  1788. EntryNetMap = EntryNetMap->ForwardLink;
  1789. } else {
  1790. //
  1791. // Maximum retries reached, clean the Token up.
  1792. //
  1793. Dns6RemoveTokenEntry (&Instance->Dns6TxTokens, Dns6TokenEntry);
  1794. Dns6TokenEntry->Token->Status = EFI_TIMEOUT;
  1795. gBS->SignalEvent (Dns6TokenEntry->Token->Event);
  1796. DispatchDpc ();
  1797. //
  1798. // Free the sending packet.
  1799. //
  1800. if (ItemNetMap->Value != NULL) {
  1801. NetbufFree ((NET_BUF *)(ItemNetMap->Value));
  1802. }
  1803. EntryNetMap = Instance->Dns6TxTokens.Used.ForwardLink;
  1804. }
  1805. }
  1806. }
  1807. }
  1808. }
  1809. /**
  1810. The timer ticking function for the DNS driver.
  1811. @param Event The ticking event
  1812. @param Context NULL
  1813. **/
  1814. VOID
  1815. EFIAPI
  1816. DnsOnTimerUpdate (
  1817. IN EFI_EVENT Event,
  1818. IN VOID *Context
  1819. )
  1820. {
  1821. LIST_ENTRY *Entry;
  1822. LIST_ENTRY *Next;
  1823. DNS4_CACHE *Item4;
  1824. DNS6_CACHE *Item6;
  1825. Item4 = NULL;
  1826. Item6 = NULL;
  1827. //
  1828. // Iterate through all the DNS4 cache list.
  1829. //
  1830. NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns4CacheList) {
  1831. Item4 = NET_LIST_USER_STRUCT (Entry, DNS4_CACHE, AllCacheLink);
  1832. Item4->DnsCache.Timeout--;
  1833. }
  1834. Entry = mDriverData->Dns4CacheList.ForwardLink;
  1835. while (Entry != &mDriverData->Dns4CacheList) {
  1836. Item4 = NET_LIST_USER_STRUCT (Entry, DNS4_CACHE, AllCacheLink);
  1837. if (Item4->DnsCache.Timeout == 0) {
  1838. RemoveEntryList (&Item4->AllCacheLink);
  1839. FreePool (Item4->DnsCache.HostName);
  1840. FreePool (Item4->DnsCache.IpAddress);
  1841. FreePool (Item4);
  1842. Entry = mDriverData->Dns4CacheList.ForwardLink;
  1843. } else {
  1844. Entry = Entry->ForwardLink;
  1845. }
  1846. }
  1847. //
  1848. // Iterate through all the DNS6 cache list.
  1849. //
  1850. NET_LIST_FOR_EACH_SAFE (Entry, Next, &mDriverData->Dns6CacheList) {
  1851. Item6 = NET_LIST_USER_STRUCT (Entry, DNS6_CACHE, AllCacheLink);
  1852. Item6->DnsCache.Timeout--;
  1853. }
  1854. Entry = mDriverData->Dns6CacheList.ForwardLink;
  1855. while (Entry != &mDriverData->Dns6CacheList) {
  1856. Item6 = NET_LIST_USER_STRUCT (Entry, DNS6_CACHE, AllCacheLink);
  1857. if (Item6->DnsCache.Timeout == 0) {
  1858. RemoveEntryList (&Item6->AllCacheLink);
  1859. FreePool (Item6->DnsCache.HostName);
  1860. FreePool (Item6->DnsCache.IpAddress);
  1861. FreePool (Item6);
  1862. Entry = mDriverData->Dns6CacheList.ForwardLink;
  1863. } else {
  1864. Entry = Entry->ForwardLink;
  1865. }
  1866. }
  1867. }