Dhcp6Io.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168
  1. /** @file
  2. Dhcp6 internal functions implementation.
  3. (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
  4. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "Dhcp6Impl.h"
  8. /**
  9. Enqueue the packet into the retry list in case of timeout.
  10. @param[in] Instance The pointer to the Dhcp6 instance.
  11. @param[in] Packet The pointer to the Dhcp6 packet to retry.
  12. @param[in] Elapsed The pointer to the elapsed time value in the packet.
  13. @param[in] RetryCtl The pointer to the transmission control of the packet.
  14. This parameter is optional and may be NULL.
  15. @retval EFI_SUCCESS Successfully enqueued the packet into the retry list according
  16. to its message type.
  17. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  18. @retval EFI_DEVICE_ERROR An unexpected message type.
  19. **/
  20. EFI_STATUS
  21. Dhcp6EnqueueRetry (
  22. IN DHCP6_INSTANCE *Instance,
  23. IN EFI_DHCP6_PACKET *Packet,
  24. IN UINT16 *Elapsed,
  25. IN EFI_DHCP6_RETRANSMISSION *RetryCtl OPTIONAL
  26. )
  27. {
  28. DHCP6_TX_CB *TxCb;
  29. DHCP6_IA_CB *IaCb;
  30. ASSERT (Packet != NULL);
  31. IaCb = &Instance->IaCb;
  32. TxCb = AllocateZeroPool (sizeof (DHCP6_TX_CB));
  33. if (TxCb == NULL) {
  34. return EFI_OUT_OF_RESOURCES;
  35. }
  36. //
  37. // Save tx packet pointer, and it will be destroyed when reply received.
  38. //
  39. TxCb->TxPacket = Packet;
  40. TxCb->Xid = Packet->Dhcp6.Header.TransactionId;
  41. //
  42. // Save pointer to elapsed-time value so we can update it on retransmits.
  43. //
  44. TxCb->Elapsed = Elapsed;
  45. //
  46. // Calculate the retransmission according to the the message type.
  47. //
  48. switch (Packet->Dhcp6.Header.MessageType) {
  49. case Dhcp6MsgSolicit:
  50. //
  51. // Calculate the retransmission threshold value for solicit packet.
  52. // Use the default value by rfc-3315 if user doesn't configure.
  53. //
  54. if (RetryCtl == NULL) {
  55. TxCb->RetryCtl.Irt = DHCP6_SOL_IRT;
  56. TxCb->RetryCtl.Mrc = DHCP6_SOL_MRC;
  57. TxCb->RetryCtl.Mrt = DHCP6_SOL_MRT;
  58. TxCb->RetryCtl.Mrd = DHCP6_SOL_MRD;
  59. } else {
  60. TxCb->RetryCtl.Irt = (RetryCtl->Irt != 0) ? RetryCtl->Irt : DHCP6_SOL_IRT;
  61. TxCb->RetryCtl.Mrc = (RetryCtl->Mrc != 0) ? RetryCtl->Mrc : DHCP6_SOL_MRC;
  62. TxCb->RetryCtl.Mrt = (RetryCtl->Mrt != 0) ? RetryCtl->Mrt : DHCP6_SOL_MRT;
  63. TxCb->RetryCtl.Mrd = (RetryCtl->Mrd != 0) ? RetryCtl->Mrd : DHCP6_SOL_MRD;
  64. }
  65. TxCb->RetryExp = Dhcp6CalculateExpireTime (
  66. TxCb->RetryCtl.Irt,
  67. TRUE,
  68. FALSE
  69. );
  70. break;
  71. case Dhcp6MsgRequest:
  72. //
  73. // Calculate the retransmission threshold value for request packet.
  74. //
  75. TxCb->RetryCtl.Irt = DHCP6_REQ_IRT;
  76. TxCb->RetryCtl.Mrc = DHCP6_REQ_MRC;
  77. TxCb->RetryCtl.Mrt = DHCP6_REQ_MRT;
  78. TxCb->RetryCtl.Mrd = DHCP6_REQ_MRD;
  79. TxCb->RetryExp = Dhcp6CalculateExpireTime (
  80. TxCb->RetryCtl.Irt,
  81. TRUE,
  82. TRUE
  83. );
  84. break;
  85. case Dhcp6MsgConfirm:
  86. //
  87. // Calculate the retransmission threshold value for confirm packet.
  88. //
  89. TxCb->RetryCtl.Irt = DHCP6_CNF_IRT;
  90. TxCb->RetryCtl.Mrc = DHCP6_CNF_MRC;
  91. TxCb->RetryCtl.Mrt = DHCP6_CNF_MRT;
  92. TxCb->RetryCtl.Mrd = DHCP6_CNF_MRD;
  93. TxCb->RetryExp = Dhcp6CalculateExpireTime (
  94. TxCb->RetryCtl.Irt,
  95. TRUE,
  96. TRUE
  97. );
  98. break;
  99. case Dhcp6MsgRenew:
  100. //
  101. // Calculate the retransmission threshold value for renew packet.
  102. //
  103. TxCb->RetryCtl.Irt = DHCP6_REB_IRT;
  104. TxCb->RetryCtl.Mrc = DHCP6_REB_MRC;
  105. TxCb->RetryCtl.Mrt = DHCP6_REB_MRT;
  106. TxCb->RetryCtl.Mrd = IaCb->T2 - IaCb->T1;
  107. TxCb->RetryExp = Dhcp6CalculateExpireTime (
  108. TxCb->RetryCtl.Irt,
  109. TRUE,
  110. TRUE
  111. );
  112. break;
  113. case Dhcp6MsgRebind:
  114. //
  115. // Calculate the retransmission threshold value for rebind packet.
  116. //
  117. TxCb->RetryCtl.Irt = DHCP6_REN_IRT;
  118. TxCb->RetryCtl.Mrc = DHCP6_REN_MRC;
  119. TxCb->RetryCtl.Mrt = DHCP6_REN_MRT;
  120. TxCb->RetryCtl.Mrd = IaCb->AllExpireTime - IaCb->T2;
  121. TxCb->RetryExp = Dhcp6CalculateExpireTime (
  122. TxCb->RetryCtl.Irt,
  123. TRUE,
  124. TRUE
  125. );
  126. break;
  127. case Dhcp6MsgDecline:
  128. //
  129. // Calculate the retransmission threshold value for decline packet.
  130. //
  131. TxCb->RetryCtl.Irt = DHCP6_DEC_IRT;
  132. TxCb->RetryCtl.Mrc = DHCP6_DEC_MRC;
  133. TxCb->RetryCtl.Mrt = DHCP6_DEC_MRT;
  134. TxCb->RetryCtl.Mrd = DHCP6_DEC_MRD;
  135. TxCb->RetryExp = Dhcp6CalculateExpireTime (
  136. TxCb->RetryCtl.Irt,
  137. TRUE,
  138. TRUE
  139. );
  140. break;
  141. case Dhcp6MsgRelease:
  142. //
  143. // Calculate the retransmission threshold value for release packet.
  144. //
  145. TxCb->RetryCtl.Irt = DHCP6_REL_IRT;
  146. TxCb->RetryCtl.Mrc = DHCP6_REL_MRC;
  147. TxCb->RetryCtl.Mrt = DHCP6_REL_MRT;
  148. TxCb->RetryCtl.Mrd = DHCP6_REL_MRD;
  149. TxCb->RetryExp = Dhcp6CalculateExpireTime (
  150. TxCb->RetryCtl.Irt,
  151. TRUE,
  152. TRUE
  153. );
  154. break;
  155. case Dhcp6MsgInfoRequest:
  156. //
  157. // Calculate the retransmission threshold value for info-request packet.
  158. // Use the default value by rfc-3315 if user doesn't configure.
  159. //
  160. if (RetryCtl == NULL) {
  161. TxCb->RetryCtl.Irt = DHCP6_INF_IRT;
  162. TxCb->RetryCtl.Mrc = DHCP6_INF_MRC;
  163. TxCb->RetryCtl.Mrt = DHCP6_INF_MRT;
  164. TxCb->RetryCtl.Mrd = DHCP6_INF_MRD;
  165. } else {
  166. TxCb->RetryCtl.Irt = (RetryCtl->Irt != 0) ? RetryCtl->Irt : DHCP6_INF_IRT;
  167. TxCb->RetryCtl.Mrc = (RetryCtl->Mrc != 0) ? RetryCtl->Mrc : DHCP6_INF_MRC;
  168. TxCb->RetryCtl.Mrt = (RetryCtl->Mrt != 0) ? RetryCtl->Mrt : DHCP6_INF_MRT;
  169. TxCb->RetryCtl.Mrd = (RetryCtl->Mrd != 0) ? RetryCtl->Mrd : DHCP6_INF_MRD;
  170. }
  171. TxCb->RetryExp = Dhcp6CalculateExpireTime (
  172. TxCb->RetryCtl.Irt,
  173. TRUE,
  174. TRUE
  175. );
  176. break;
  177. default:
  178. //
  179. // Unexpected message type.
  180. //
  181. return EFI_DEVICE_ERROR;
  182. }
  183. //
  184. // Insert into the retransmit list of the instance.
  185. //
  186. InsertTailList (&Instance->TxList, &TxCb->Link);
  187. return EFI_SUCCESS;
  188. }
  189. /**
  190. Dequeue the packet from retry list if reply received or timeout at last.
  191. @param[in] Instance The pointer to the Dhcp6 instance.
  192. @param[in] PacketXid The packet transaction id to match.
  193. @param[in] NeedSignal If TRUE, then an timeout event need be signaled when it is existed.
  194. Otherwise, this parameter is ignored.
  195. @retval EFI_SUCCESS Successfully dequeued the packet into retry list .
  196. @retval EFI_NOT_FOUND There is no xid matched in retry list.
  197. **/
  198. EFI_STATUS
  199. Dhcp6DequeueRetry (
  200. IN DHCP6_INSTANCE *Instance,
  201. IN UINT32 PacketXid,
  202. IN BOOLEAN NeedSignal
  203. )
  204. {
  205. LIST_ENTRY *Entry;
  206. LIST_ENTRY *NextEntry;
  207. DHCP6_TX_CB *TxCb;
  208. DHCP6_INF_CB *InfCb;
  209. //
  210. // Seek the retransmit node in the retransmit list by packet xid.
  211. //
  212. NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->TxList) {
  213. TxCb = NET_LIST_USER_STRUCT (Entry, DHCP6_TX_CB, Link);
  214. ASSERT(TxCb->TxPacket);
  215. if (TxCb->Xid == PacketXid) {
  216. if (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgInfoRequest) {
  217. //
  218. // Seek the info-request node in the info-request list by packet xid.
  219. //
  220. NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->InfList) {
  221. InfCb = NET_LIST_USER_STRUCT (Entry, DHCP6_INF_CB, Link);
  222. if (InfCb->Xid == PacketXid) {
  223. //
  224. // Remove the info-request node, and signal the event if timeout.
  225. //
  226. if (InfCb->TimeoutEvent != NULL && NeedSignal) {
  227. gBS->SignalEvent (InfCb->TimeoutEvent);
  228. }
  229. RemoveEntryList (&InfCb->Link);
  230. FreePool (InfCb);
  231. }
  232. }
  233. }
  234. //
  235. // Remove the retransmit node.
  236. //
  237. RemoveEntryList (&TxCb->Link);
  238. ASSERT(TxCb->TxPacket);
  239. FreePool (TxCb->TxPacket);
  240. FreePool (TxCb);
  241. return EFI_SUCCESS;
  242. }
  243. }
  244. return EFI_NOT_FOUND;
  245. }
  246. /**
  247. Clean up the specific nodes in the retry list.
  248. @param[in] Instance The pointer to the Dhcp6 instance.
  249. @param[in] Scope The scope of cleanup nodes.
  250. **/
  251. VOID
  252. Dhcp6CleanupRetry (
  253. IN DHCP6_INSTANCE *Instance,
  254. IN UINT32 Scope
  255. )
  256. {
  257. LIST_ENTRY *Entry;
  258. LIST_ENTRY *NextEntry;
  259. DHCP6_TX_CB *TxCb;
  260. DHCP6_INF_CB *InfCb;
  261. //
  262. // Clean up all the stateful messages from the retransmit list.
  263. //
  264. if (Scope == DHCP6_PACKET_STATEFUL || Scope == DHCP6_PACKET_ALL) {
  265. NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->TxList) {
  266. TxCb = NET_LIST_USER_STRUCT (Entry, DHCP6_TX_CB, Link);
  267. ASSERT(TxCb->TxPacket);
  268. if (TxCb->TxPacket->Dhcp6.Header.MessageType != Dhcp6MsgInfoRequest) {
  269. RemoveEntryList (&TxCb->Link);
  270. FreePool (TxCb->TxPacket);
  271. FreePool (TxCb);
  272. }
  273. }
  274. }
  275. //
  276. // Clean up all the stateless messages from the retransmit list.
  277. //
  278. if (Scope == DHCP6_PACKET_STATELESS || Scope == DHCP6_PACKET_ALL) {
  279. //
  280. // Clean up all the retransmit list for stateless messages.
  281. //
  282. NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->TxList) {
  283. TxCb = NET_LIST_USER_STRUCT (Entry, DHCP6_TX_CB, Link);
  284. ASSERT(TxCb->TxPacket);
  285. if (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgInfoRequest) {
  286. RemoveEntryList (&TxCb->Link);
  287. FreePool (TxCb->TxPacket);
  288. FreePool (TxCb);
  289. }
  290. }
  291. //
  292. // Clean up all the info-request messages list.
  293. //
  294. NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->InfList) {
  295. InfCb = NET_LIST_USER_STRUCT (Entry, DHCP6_INF_CB, Link);
  296. if (InfCb->TimeoutEvent != NULL) {
  297. gBS->SignalEvent (InfCb->TimeoutEvent);
  298. }
  299. RemoveEntryList (&InfCb->Link);
  300. FreePool (InfCb);
  301. }
  302. }
  303. }
  304. /**
  305. Check whether the TxCb is still a valid control block in the instance's retry list.
  306. @param[in] Instance The pointer to DHCP6_INSTANCE.
  307. @param[in] TxCb The control block for a transmitted message.
  308. @retval TRUE The control block is in Instance's retry list.
  309. @retval FALSE The control block is NOT in Instance's retry list.
  310. **/
  311. BOOLEAN
  312. Dhcp6IsValidTxCb (
  313. IN DHCP6_INSTANCE *Instance,
  314. IN DHCP6_TX_CB *TxCb
  315. )
  316. {
  317. LIST_ENTRY *Entry;
  318. NET_LIST_FOR_EACH (Entry, &Instance->TxList) {
  319. if (TxCb == NET_LIST_USER_STRUCT (Entry, DHCP6_TX_CB, Link)) {
  320. return TRUE;
  321. }
  322. }
  323. return FALSE;
  324. }
  325. /**
  326. Clean up the session of the instance stateful exchange.
  327. @param[in, out] Instance The pointer to the Dhcp6 instance.
  328. @param[in] Status The return status from udp.
  329. **/
  330. VOID
  331. Dhcp6CleanupSession (
  332. IN OUT DHCP6_INSTANCE *Instance,
  333. IN EFI_STATUS Status
  334. )
  335. {
  336. UINTN Index;
  337. EFI_DHCP6_IA *Ia;
  338. ASSERT(Instance->Config);
  339. ASSERT(Instance->IaCb.Ia);
  340. //
  341. // Clean up the retransmit list for stateful messages.
  342. //
  343. Dhcp6CleanupRetry (Instance, DHCP6_PACKET_STATEFUL);
  344. if (Instance->Unicast != NULL) {
  345. FreePool (Instance->Unicast);
  346. }
  347. if (Instance->AdSelect != NULL) {
  348. FreePool (Instance->AdSelect);
  349. }
  350. if (Instance->IaCb.Ia->ReplyPacket != NULL) {
  351. FreePool (Instance->IaCb.Ia->ReplyPacket);
  352. }
  353. //
  354. // Reinitialize the Ia fields of the instance.
  355. //
  356. Instance->UdpSts = Status;
  357. Instance->AdSelect = NULL;
  358. Instance->AdPref = 0;
  359. Instance->Unicast = NULL;
  360. Instance->IaCb.T1 = 0;
  361. Instance->IaCb.T2 = 0;
  362. Instance->IaCb.AllExpireTime = 0;
  363. Instance->IaCb.LeaseTime = 0;
  364. //
  365. // Clear start time
  366. //
  367. Instance->StartTime = 0;
  368. Ia = Instance->IaCb.Ia;
  369. Ia->State = Dhcp6Init;
  370. Ia->ReplyPacket = NULL;
  371. //
  372. // Set the addresses as zero lifetime, and then the notify
  373. // function in Ip6Config will remove these timeout address.
  374. //
  375. for (Index = 0; Index < Ia->IaAddressCount; Index++) {
  376. Ia->IaAddress[Index].PreferredLifetime = 0;
  377. Ia->IaAddress[Index].ValidLifetime = 0;
  378. }
  379. //
  380. //
  381. // Signal the Ia information updated event to informal user.
  382. //
  383. if (Instance->Config->IaInfoEvent != NULL) {
  384. gBS->SignalEvent (Instance->Config->IaInfoEvent);
  385. }
  386. }
  387. /**
  388. Callback to user when Dhcp6 transmit/receive occurs.
  389. @param[in] Instance The pointer to the Dhcp6 instance.
  390. @param[in] Event The current Dhcp6 event.
  391. @param[in, out] Packet The pointer to the packet sending or received.
  392. @retval EFI_SUCCESS The user function returns success.
  393. @retval EFI_NOT_READY Direct the caller to continue collecting the offer.
  394. @retval EFI_ABORTED The user function ask it to abort.
  395. **/
  396. EFI_STATUS
  397. EFIAPI
  398. Dhcp6CallbackUser (
  399. IN DHCP6_INSTANCE *Instance,
  400. IN EFI_DHCP6_EVENT Event,
  401. IN OUT EFI_DHCP6_PACKET **Packet
  402. )
  403. {
  404. EFI_STATUS Status;
  405. EFI_DHCP6_PACKET *NewPacket;
  406. EFI_DHCP6_CALLBACK Callback;
  407. VOID *Context;
  408. ASSERT (Packet != NULL);
  409. ASSERT (Instance->Config != NULL);
  410. ASSERT (Instance->IaCb.Ia != NULL);
  411. NewPacket = NULL;
  412. Status = EFI_SUCCESS;
  413. Callback = Instance->Config->Dhcp6Callback;
  414. Context = Instance->Config->CallbackContext;
  415. //
  416. // Callback to user with the new message if has.
  417. //
  418. if (Callback != NULL) {
  419. Status = Callback (
  420. &Instance->Dhcp6,
  421. Context,
  422. Instance->IaCb.Ia->State,
  423. Event,
  424. *Packet,
  425. &NewPacket
  426. );
  427. //
  428. // Updated the new packet from user to replace the original one.
  429. //
  430. if (NewPacket != NULL) {
  431. ASSERT (*Packet != NULL);
  432. FreePool (*Packet);
  433. *Packet = NewPacket;
  434. }
  435. }
  436. return Status;
  437. }
  438. /**
  439. Update Ia according to the new reply message.
  440. @param[in, out] Instance The pointer to the Dhcp6 instance.
  441. @param[in] Packet The pointer to reply messages.
  442. @retval EFI_SUCCESS Updated the Ia information successfully.
  443. @retval EFI_DEVICE_ERROR An unexpected error.
  444. **/
  445. EFI_STATUS
  446. Dhcp6UpdateIaInfo (
  447. IN OUT DHCP6_INSTANCE *Instance,
  448. IN EFI_DHCP6_PACKET *Packet
  449. )
  450. {
  451. EFI_STATUS Status;
  452. UINT8 *Option;
  453. UINT8 *IaInnerOpt;
  454. UINT16 IaInnerLen;
  455. UINT16 StsCode;
  456. UINT32 T1;
  457. UINT32 T2;
  458. ASSERT (Instance->Config != NULL);
  459. //
  460. // If the reply was received in reponse to a solicit with rapid commit option,
  461. // request, renew or rebind message, the client updates the information it has
  462. // recorded about IAs from the IA options contained in the reply message:
  463. // 1. record the T1 and T2 times
  464. // 2. add any new addresses in the IA
  465. // 3. discard any addresses from the IA, that have a valid lifetime of 0
  466. // 4. update lifetimes for any addresses that alread recorded
  467. // 5. leave unchanged any information about addresses
  468. //
  469. // See details in the section-18.1.8 of rfc-3315.
  470. //
  471. Option = Dhcp6SeekIaOption (
  472. Packet->Dhcp6.Option,
  473. Packet->Length - sizeof (EFI_DHCP6_HEADER),
  474. &Instance->Config->IaDescriptor
  475. );
  476. if (Option == NULL) {
  477. return EFI_DEVICE_ERROR;
  478. }
  479. //
  480. // The format of the IA_NA option is:
  481. //
  482. // 0 1 2 3
  483. // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  484. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  485. // | OPTION_IA_NA | option-len |
  486. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  487. // | IAID (4 octets) |
  488. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  489. // | T1 |
  490. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  491. // | T2 |
  492. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  493. // | |
  494. // . IA_NA-options .
  495. // . .
  496. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  497. //
  498. // The format of the IA_TA option is:
  499. //
  500. // 0 1 2 3
  501. // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  502. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  503. // | OPTION_IA_TA | option-len |
  504. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  505. // | IAID (4 octets) |
  506. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  507. // | |
  508. // . IA_TA-options .
  509. // . .
  510. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  511. //
  512. //
  513. // sizeof (option-code + option-len + IaId) = 8
  514. // sizeof (option-code + option-len + IaId + T1) = 12
  515. // sizeof (option-code + option-len + IaId + T1 + T2) = 16
  516. //
  517. // The inner options still start with 2 bytes option-code and 2 bytes option-len.
  518. //
  519. if (Instance->Config->IaDescriptor.Type == Dhcp6OptIana) {
  520. T1 = NTOHL (ReadUnaligned32 ((UINT32 *) (Option + 8)));
  521. T2 = NTOHL (ReadUnaligned32 ((UINT32 *) (Option + 12)));
  522. //
  523. // Refer to RFC3155 Chapter 22.4. If a client receives an IA_NA with T1 greater than T2,
  524. // and both T1 and T2 are greater than 0, the client discards the IA_NA option and processes
  525. // the remainder of the message as though the server had not included the invalid IA_NA option.
  526. //
  527. if (T1 > T2 && T2 > 0) {
  528. return EFI_DEVICE_ERROR;
  529. }
  530. IaInnerOpt = Option + 16;
  531. IaInnerLen = (UINT16) (NTOHS (ReadUnaligned16 ((UINT16 *) (Option + 2))) - 12);
  532. } else {
  533. T1 = 0;
  534. T2 = 0;
  535. IaInnerOpt = Option + 8;
  536. IaInnerLen = (UINT16) (NTOHS (ReadUnaligned16 ((UINT16 *) (Option + 2))) - 4);
  537. }
  538. //
  539. // The format of the Status Code option is:
  540. //
  541. // 0 1 2 3
  542. // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  543. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  544. // | OPTION_STATUS_CODE | option-len |
  545. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  546. // | status-code | |
  547. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
  548. // . .
  549. // . status-message .
  550. // . .
  551. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  552. //
  553. //
  554. // sizeof (option-code + option-len) = 4
  555. //
  556. StsCode = Dhcp6StsSuccess;
  557. Option = Dhcp6SeekOption (IaInnerOpt, IaInnerLen, Dhcp6OptStatusCode);
  558. if (Option != NULL) {
  559. StsCode = NTOHS (ReadUnaligned16 ((UINT16 *) (Option + 4)));
  560. if (StsCode != Dhcp6StsSuccess) {
  561. return EFI_DEVICE_ERROR;
  562. }
  563. }
  564. //
  565. // Generate control block for the Ia.
  566. //
  567. Status = Dhcp6GenerateIaCb (
  568. Instance,
  569. IaInnerOpt,
  570. IaInnerLen,
  571. T1,
  572. T2
  573. );
  574. return Status;
  575. }
  576. /**
  577. Seek StatusCode Option in package. A Status Code option may appear in the
  578. options field of a DHCP message and/or in the options field of another option.
  579. See details in section 22.13, RFC3315.
  580. @param[in] Instance The pointer to the Dhcp6 instance.
  581. @param[in] Packet The pointer to reply messages.
  582. @param[out] Option The pointer to status code option.
  583. @retval EFI_SUCCESS Seek status code option successfully.
  584. @retval EFI_DEVICE_ERROR An unexpected error.
  585. **/
  586. EFI_STATUS
  587. Dhcp6SeekStsOption (
  588. IN DHCP6_INSTANCE *Instance,
  589. IN EFI_DHCP6_PACKET *Packet,
  590. OUT UINT8 **Option
  591. )
  592. {
  593. UINT8 *IaInnerOpt;
  594. UINT16 IaInnerLen;
  595. UINT16 StsCode;
  596. //
  597. // Seek StatusCode option directly in DHCP message body. That is, search in
  598. // non-encapsulated option fields.
  599. //
  600. *Option = Dhcp6SeekOption (
  601. Packet->Dhcp6.Option,
  602. Packet->Length - 4,
  603. Dhcp6OptStatusCode
  604. );
  605. if (*Option != NULL) {
  606. StsCode = NTOHS (ReadUnaligned16 ((UINT16 *) (*Option + 4)));
  607. if (StsCode != Dhcp6StsSuccess) {
  608. return EFI_DEVICE_ERROR;
  609. }
  610. }
  611. //
  612. // Seek in encapsulated options, IA_NA and IA_TA.
  613. //
  614. *Option = Dhcp6SeekIaOption (
  615. Packet->Dhcp6.Option,
  616. Packet->Length - sizeof (EFI_DHCP6_HEADER),
  617. &Instance->Config->IaDescriptor
  618. );
  619. if (*Option == NULL) {
  620. return EFI_SUCCESS;
  621. }
  622. //
  623. // The format of the IA_NA option is:
  624. //
  625. // 0 1 2 3
  626. // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  627. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  628. // | OPTION_IA_NA | option-len |
  629. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  630. // | IAID (4 octets) |
  631. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  632. // | T1 |
  633. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  634. // | T2 |
  635. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  636. // | |
  637. // . IA_NA-options .
  638. // . .
  639. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  640. //
  641. // The format of the IA_TA option is:
  642. //
  643. // 0 1 2 3
  644. // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  645. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  646. // | OPTION_IA_TA | option-len |
  647. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  648. // | IAID (4 octets) |
  649. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  650. // | |
  651. // . IA_TA-options .
  652. // . .
  653. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  654. //
  655. //
  656. // sizeof (option-code + option-len + IaId) = 8
  657. // sizeof (option-code + option-len + IaId + T1) = 12
  658. // sizeof (option-code + option-len + IaId + T1 + T2) = 16
  659. //
  660. // The inner options still start with 2 bytes option-code and 2 bytes option-len.
  661. //
  662. if (Instance->Config->IaDescriptor.Type == Dhcp6OptIana) {
  663. IaInnerOpt = *Option + 16;
  664. IaInnerLen = (UINT16) (NTOHS (ReadUnaligned16 ((UINT16 *) (*Option + 2))) - 12);
  665. } else {
  666. IaInnerOpt = *Option + 8;
  667. IaInnerLen = (UINT16) (NTOHS (ReadUnaligned16 ((UINT16 *) (*Option + 2))) - 4);
  668. }
  669. //
  670. // The format of the Status Code option is:
  671. //
  672. // 0 1 2 3
  673. // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  674. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  675. // | OPTION_STATUS_CODE | option-len |
  676. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  677. // | status-code | |
  678. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
  679. // . .
  680. // . status-message .
  681. // . .
  682. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  683. //
  684. //
  685. // sizeof (option-code + option-len) = 4
  686. //
  687. *Option = Dhcp6SeekOption (IaInnerOpt, IaInnerLen, Dhcp6OptStatusCode);
  688. if (*Option != NULL) {
  689. StsCode = NTOHS (ReadUnaligned16 ((UINT16 *) (*Option + 4)));
  690. if (StsCode != Dhcp6StsSuccess) {
  691. return EFI_DEVICE_ERROR;
  692. }
  693. }
  694. return EFI_SUCCESS;
  695. }
  696. /**
  697. Transmit Dhcp6 message by udpio.
  698. @param[in] Instance The pointer to the Dhcp6 instance.
  699. @param[in] Packet The pointer to transmit message.
  700. @param[in] Elapsed The pointer to the elapsed time value to fill in.
  701. @retval EFI_SUCCESS Successfully transmitted the packet.
  702. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  703. @retval Others Failed to transmit the packet.
  704. **/
  705. EFI_STATUS
  706. Dhcp6TransmitPacket (
  707. IN DHCP6_INSTANCE *Instance,
  708. IN EFI_DHCP6_PACKET *Packet,
  709. IN UINT16 *Elapsed
  710. )
  711. {
  712. EFI_STATUS Status;
  713. NET_BUF *Wrap;
  714. NET_FRAGMENT Frag;
  715. UDP_END_POINT EndPt;
  716. DHCP6_SERVICE *Service;
  717. Service = Instance->Service;
  718. //
  719. // Wrap it into a netbuf then send it.
  720. //
  721. Frag.Bulk = (UINT8 *) &Packet->Dhcp6.Header;
  722. Frag.Len = Packet->Length;
  723. //
  724. // Do not register free packet here, which will be handled in retry list.
  725. //
  726. Wrap = NetbufFromExt (&Frag, 1, 0, 0, Dhcp6DummyExtFree, NULL);
  727. if (Wrap == NULL) {
  728. return EFI_OUT_OF_RESOURCES;
  729. }
  730. //
  731. // Multicast the Dhcp6 message, unless get the unicast server address by option.
  732. //
  733. ZeroMem (&EndPt, sizeof (UDP_END_POINT));
  734. if (Instance->Unicast != NULL) {
  735. CopyMem (
  736. &EndPt.RemoteAddr,
  737. Instance->Unicast,
  738. sizeof (EFI_IPv6_ADDRESS)
  739. );
  740. } else {
  741. CopyMem (
  742. &EndPt.RemoteAddr,
  743. &mAllDhcpRelayAndServersAddress,
  744. sizeof (EFI_IPv6_ADDRESS)
  745. );
  746. }
  747. EndPt.RemotePort = DHCP6_PORT_SERVER;
  748. EndPt.LocalPort = DHCP6_PORT_CLIENT;
  749. //
  750. // Update the elapsed time value.
  751. //
  752. if (Elapsed != NULL) {
  753. SetElapsedTime (Elapsed, Instance);
  754. }
  755. //
  756. // Send out the message by the configured Udp6Io.
  757. //
  758. Status = UdpIoSendDatagram (
  759. Service->UdpIo,
  760. Wrap,
  761. &EndPt,
  762. NULL,
  763. Dhcp6OnTransmitted,
  764. NULL
  765. );
  766. if (EFI_ERROR (Status)) {
  767. NetbufFree (Wrap);
  768. return Status;
  769. }
  770. return EFI_SUCCESS;
  771. }
  772. /**
  773. Create the solicit message and send it.
  774. @param[in] Instance The pointer to the Dhcp6 instance.
  775. @retval EFI_SUCCESS Created and sent the solicit message successfully.
  776. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  777. @retval Others Failed to send the solicit message.
  778. **/
  779. EFI_STATUS
  780. Dhcp6SendSolicitMsg (
  781. IN DHCP6_INSTANCE *Instance
  782. )
  783. {
  784. EFI_STATUS Status;
  785. EFI_DHCP6_PACKET *Packet;
  786. EFI_DHCP6_PACKET_OPTION *UserOpt;
  787. EFI_DHCP6_DUID *ClientId;
  788. DHCP6_SERVICE *Service;
  789. UINT8 *Cursor;
  790. UINT16 *Elapsed;
  791. UINT32 UserLen;
  792. UINTN Index;
  793. UINT16 Length;
  794. Service = Instance->Service;
  795. ClientId = Service->ClientId;
  796. UserLen = 0;
  797. ASSERT (Service->ClientId != NULL);
  798. ASSERT (Instance->Config != NULL);
  799. ASSERT (Instance->IaCb.Ia != NULL);
  800. //
  801. // Calculate the added length of customized option list.
  802. //
  803. for (Index = 0; Index < Instance->Config->OptionCount; Index++) {
  804. UserLen += (NTOHS (Instance->Config->OptionList[Index]->OpLen) + 4);
  805. }
  806. //
  807. // Create the Dhcp6 packet and initialize commone fields.
  808. //
  809. Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen);
  810. if (Packet == NULL) {
  811. return EFI_OUT_OF_RESOURCES;
  812. }
  813. Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen;
  814. Packet->Length = sizeof (EFI_DHCP6_HEADER);
  815. Packet->Dhcp6.Header.MessageType = Dhcp6MsgSolicit;
  816. Packet->Dhcp6.Header.TransactionId = Service->Xid++;
  817. //
  818. // Assembly Dhcp6 options for solicit message.
  819. //
  820. Cursor = Packet->Dhcp6.Option;
  821. Length = HTONS (ClientId->Length);
  822. Cursor = Dhcp6AppendOption (
  823. Cursor,
  824. HTONS (Dhcp6OptClientId),
  825. Length,
  826. ClientId->Duid
  827. );
  828. Cursor = Dhcp6AppendETOption (
  829. Cursor,
  830. Instance,
  831. &Elapsed
  832. );
  833. Cursor = Dhcp6AppendIaOption (
  834. Cursor,
  835. Instance->IaCb.Ia,
  836. Instance->IaCb.T1,
  837. Instance->IaCb.T2,
  838. Packet->Dhcp6.Header.MessageType
  839. );
  840. //
  841. // Append user-defined when configurate Dhcp6 service.
  842. //
  843. for (Index = 0; Index < Instance->Config->OptionCount; Index++) {
  844. UserOpt = Instance->Config->OptionList[Index];
  845. Cursor = Dhcp6AppendOption(
  846. Cursor,
  847. UserOpt->OpCode,
  848. UserOpt->OpLen,
  849. UserOpt->Data
  850. );
  851. }
  852. //
  853. // Determine the size/length of packet.
  854. //
  855. Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option);
  856. ASSERT (Packet->Size > Packet->Length + 8);
  857. //
  858. // Callback to user with the packet to be sent and check the user's feedback.
  859. //
  860. Status = Dhcp6CallbackUser (Instance, Dhcp6SendSolicit, &Packet);
  861. if (EFI_ERROR (Status)) {
  862. FreePool (Packet);
  863. return Status;
  864. }
  865. //
  866. // Send solicit packet with the state transition from Dhcp6init to
  867. // Dhcp6selecting.
  868. //
  869. Instance->IaCb.Ia->State = Dhcp6Selecting;
  870. //
  871. // Clear initial time for current transaction.
  872. //
  873. Instance->StartTime = 0;
  874. Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
  875. if (EFI_ERROR (Status)) {
  876. FreePool (Packet);
  877. return Status;
  878. }
  879. //
  880. // Enqueue the sent packet for the retransmission in case reply timeout.
  881. //
  882. return Dhcp6EnqueueRetry (
  883. Instance,
  884. Packet,
  885. Elapsed,
  886. Instance->Config->SolicitRetransmission
  887. );
  888. }
  889. /**
  890. Configure some parameter to initiate SolicitMsg.
  891. @param[in] Instance The pointer to the Dhcp6 instance.
  892. @retval EFI_SUCCESS Created and sent the solicit message successfully.
  893. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  894. @retval Others Failed to send the solicit message.
  895. **/
  896. EFI_STATUS
  897. Dhcp6InitSolicitMsg (
  898. IN DHCP6_INSTANCE *Instance
  899. )
  900. {
  901. Instance->IaCb.T1 = 0;
  902. Instance->IaCb.T2 = 0;
  903. Instance->IaCb.Ia->IaAddressCount = 0;
  904. return Dhcp6SendSolicitMsg (Instance);
  905. }
  906. /**
  907. Create the request message and send it.
  908. @param[in] Instance The pointer to the Dhcp6 instance.
  909. @retval EFI_SUCCESS Created and sent the request message successfully.
  910. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  911. @retval EFI_DEVICE_ERROR An unexpected error.
  912. @retval Others Failed to send the request message.
  913. **/
  914. EFI_STATUS
  915. Dhcp6SendRequestMsg (
  916. IN DHCP6_INSTANCE *Instance
  917. )
  918. {
  919. EFI_STATUS Status;
  920. EFI_DHCP6_PACKET *Packet;
  921. EFI_DHCP6_PACKET_OPTION *UserOpt;
  922. EFI_DHCP6_DUID *ClientId;
  923. EFI_DHCP6_DUID *ServerId;
  924. DHCP6_SERVICE *Service;
  925. UINT8 *Option;
  926. UINT8 *Cursor;
  927. UINT16 *Elapsed;
  928. UINT32 UserLen;
  929. UINTN Index;
  930. UINT16 Length;
  931. ASSERT(Instance->AdSelect != NULL);
  932. ASSERT(Instance->Config != NULL);
  933. ASSERT(Instance->IaCb.Ia != NULL);
  934. ASSERT(Instance->Service != NULL);
  935. Service = Instance->Service;
  936. ClientId = Service->ClientId;
  937. ASSERT(ClientId != NULL);
  938. //
  939. // Get the server Id from the selected advertisement message.
  940. //
  941. Option = Dhcp6SeekOption (
  942. Instance->AdSelect->Dhcp6.Option,
  943. Instance->AdSelect->Length - 4,
  944. Dhcp6OptServerId
  945. );
  946. if (Option == NULL) {
  947. return EFI_DEVICE_ERROR;
  948. }
  949. ServerId = (EFI_DHCP6_DUID *) (Option + 2);
  950. //
  951. // Calculate the added length of customized option list.
  952. //
  953. UserLen = 0;
  954. for (Index = 0; Index < Instance->Config->OptionCount; Index++) {
  955. UserLen += (NTOHS (Instance->Config->OptionList[Index]->OpLen) + 4);
  956. }
  957. //
  958. // Create the Dhcp6 packet and initialize commone fields.
  959. //
  960. Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen);
  961. if (Packet == NULL) {
  962. return EFI_OUT_OF_RESOURCES;
  963. }
  964. Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen;
  965. Packet->Length = sizeof (EFI_DHCP6_HEADER);
  966. Packet->Dhcp6.Header.MessageType = Dhcp6MsgRequest;
  967. Packet->Dhcp6.Header.TransactionId = Service->Xid++;
  968. //
  969. // Assembly Dhcp6 options for request message.
  970. //
  971. Cursor = Packet->Dhcp6.Option;
  972. Length = HTONS (ClientId->Length);
  973. Cursor = Dhcp6AppendOption (
  974. Cursor,
  975. HTONS (Dhcp6OptClientId),
  976. Length,
  977. ClientId->Duid
  978. );
  979. Cursor = Dhcp6AppendETOption (
  980. Cursor,
  981. Instance,
  982. &Elapsed
  983. );
  984. Cursor = Dhcp6AppendOption (
  985. Cursor,
  986. HTONS (Dhcp6OptServerId),
  987. ServerId->Length,
  988. ServerId->Duid
  989. );
  990. Cursor = Dhcp6AppendIaOption (
  991. Cursor,
  992. Instance->IaCb.Ia,
  993. Instance->IaCb.T1,
  994. Instance->IaCb.T2,
  995. Packet->Dhcp6.Header.MessageType
  996. );
  997. //
  998. // Append user-defined when configurate Dhcp6 service.
  999. //
  1000. for (Index = 0; Index < Instance->Config->OptionCount; Index++) {
  1001. UserOpt = Instance->Config->OptionList[Index];
  1002. Cursor = Dhcp6AppendOption(
  1003. Cursor,
  1004. UserOpt->OpCode,
  1005. UserOpt->OpLen,
  1006. UserOpt->Data
  1007. );
  1008. }
  1009. //
  1010. // Determine the size/length of packet.
  1011. //
  1012. Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option);
  1013. ASSERT (Packet->Size > Packet->Length + 8);
  1014. //
  1015. // Callback to user with the packet to be sent and check the user's feedback.
  1016. //
  1017. Status = Dhcp6CallbackUser (Instance, Dhcp6SendRequest, &Packet);
  1018. if (EFI_ERROR (Status)) {
  1019. FreePool (Packet);
  1020. return Status;
  1021. }
  1022. //
  1023. // Send request packet with the state transition from Dhcp6selecting to
  1024. // Dhcp6requesting.
  1025. //
  1026. Instance->IaCb.Ia->State = Dhcp6Requesting;
  1027. //
  1028. // Clear initial time for current transaction.
  1029. //
  1030. Instance->StartTime = 0;
  1031. Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
  1032. if (EFI_ERROR (Status)) {
  1033. FreePool (Packet);
  1034. return Status;
  1035. }
  1036. //
  1037. // Enqueue the sent packet for the retransmission in case reply timeout.
  1038. //
  1039. return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL);
  1040. }
  1041. /**
  1042. Create the decline message and send it.
  1043. @param[in] Instance The pointer to the Dhcp6 instance.
  1044. @param[in] DecIa The pointer to the decline Ia.
  1045. @retval EFI_SUCCESS Created and sent the decline message successfully.
  1046. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  1047. @retval EFI_DEVICE_ERROR An unexpected error.
  1048. @retval Others Failed to send the decline message.
  1049. **/
  1050. EFI_STATUS
  1051. Dhcp6SendDeclineMsg (
  1052. IN DHCP6_INSTANCE *Instance,
  1053. IN EFI_DHCP6_IA *DecIa
  1054. )
  1055. {
  1056. EFI_STATUS Status;
  1057. EFI_DHCP6_PACKET *Packet;
  1058. EFI_DHCP6_PACKET *LastReply;
  1059. EFI_DHCP6_DUID *ClientId;
  1060. EFI_DHCP6_DUID *ServerId;
  1061. DHCP6_SERVICE *Service;
  1062. UINT8 *Option;
  1063. UINT8 *Cursor;
  1064. UINT16 *Elapsed;
  1065. UINT16 Length;
  1066. ASSERT (Instance->Config != NULL);
  1067. ASSERT (Instance->IaCb.Ia != NULL);
  1068. ASSERT (Instance->Service != NULL);
  1069. Service = Instance->Service;
  1070. ClientId = Service->ClientId;
  1071. LastReply = Instance->IaCb.Ia->ReplyPacket;
  1072. ASSERT (ClientId != NULL);
  1073. ASSERT (LastReply != NULL);
  1074. //
  1075. // Get the server Id from the last reply message.
  1076. //
  1077. Option = Dhcp6SeekOption (
  1078. LastReply->Dhcp6.Option,
  1079. LastReply->Length - 4,
  1080. Dhcp6OptServerId
  1081. );
  1082. if (Option == NULL) {
  1083. return EFI_DEVICE_ERROR;
  1084. }
  1085. //
  1086. // EFI_DHCP6_DUID contains a length field of 2 bytes.
  1087. //
  1088. ServerId = (EFI_DHCP6_DUID *) (Option + 2);
  1089. //
  1090. // Create the Dhcp6 packet and initialize commone fields.
  1091. //
  1092. Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE);
  1093. if (Packet == NULL) {
  1094. return EFI_OUT_OF_RESOURCES;
  1095. }
  1096. Packet->Size = DHCP6_BASE_PACKET_SIZE;
  1097. Packet->Length = sizeof (EFI_DHCP6_HEADER);
  1098. Packet->Dhcp6.Header.MessageType = Dhcp6MsgDecline;
  1099. Packet->Dhcp6.Header.TransactionId = Service->Xid++;
  1100. //
  1101. // Assembly Dhcp6 options for rebind/renew message.
  1102. //
  1103. Cursor = Packet->Dhcp6.Option;
  1104. Length = HTONS (ClientId->Length);
  1105. Cursor = Dhcp6AppendOption (
  1106. Cursor,
  1107. HTONS (Dhcp6OptClientId),
  1108. Length,
  1109. ClientId->Duid
  1110. );
  1111. Cursor = Dhcp6AppendETOption (
  1112. Cursor,
  1113. Instance,
  1114. &Elapsed
  1115. );
  1116. Cursor = Dhcp6AppendOption (
  1117. Cursor,
  1118. HTONS (Dhcp6OptServerId),
  1119. ServerId->Length,
  1120. ServerId->Duid
  1121. );
  1122. Cursor = Dhcp6AppendIaOption (Cursor, DecIa, 0, 0, Packet->Dhcp6.Header.MessageType);
  1123. //
  1124. // Determine the size/length of packet.
  1125. //
  1126. Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option);
  1127. ASSERT (Packet->Size > Packet->Length + 8);
  1128. //
  1129. // Callback to user with the packet to be sent and check the user's feedback.
  1130. //
  1131. Status = Dhcp6CallbackUser (Instance, Dhcp6SendDecline, &Packet);
  1132. if (EFI_ERROR (Status)) {
  1133. FreePool (Packet);
  1134. return Status;
  1135. }
  1136. //
  1137. // Send decline packet with the state transition from Dhcp6bound to
  1138. // Dhcp6declining.
  1139. //
  1140. Instance->IaCb.Ia->State = Dhcp6Declining;
  1141. //
  1142. // Clear initial time for current transaction.
  1143. //
  1144. Instance->StartTime = 0;
  1145. Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
  1146. if (EFI_ERROR (Status)) {
  1147. FreePool (Packet);
  1148. return Status;
  1149. }
  1150. //
  1151. // Enqueue the sent packet for the retransmission in case reply timeout.
  1152. //
  1153. return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL);
  1154. }
  1155. /**
  1156. Create the release message and send it.
  1157. @param[in] Instance The pointer to the Dhcp6 instance.
  1158. @param[in] RelIa The pointer to the release Ia.
  1159. @retval EFI_SUCCESS Created and sent the release message successfully.
  1160. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  1161. @retval EFI_DEVICE_ERROR An unexpected error.
  1162. @retval Others Failed to send the release message.
  1163. **/
  1164. EFI_STATUS
  1165. Dhcp6SendReleaseMsg (
  1166. IN DHCP6_INSTANCE *Instance,
  1167. IN EFI_DHCP6_IA *RelIa
  1168. )
  1169. {
  1170. EFI_STATUS Status;
  1171. EFI_DHCP6_PACKET *Packet;
  1172. EFI_DHCP6_PACKET *LastReply;
  1173. EFI_DHCP6_DUID *ClientId;
  1174. EFI_DHCP6_DUID *ServerId;
  1175. DHCP6_SERVICE *Service;
  1176. UINT8 *Option;
  1177. UINT8 *Cursor;
  1178. UINT16 *Elapsed;
  1179. UINT16 Length;
  1180. ASSERT(Instance->Config);
  1181. ASSERT(Instance->IaCb.Ia);
  1182. Service = Instance->Service;
  1183. ClientId = Service->ClientId;
  1184. LastReply = Instance->IaCb.Ia->ReplyPacket;
  1185. ASSERT(ClientId);
  1186. ASSERT(LastReply);
  1187. //
  1188. // Get the server Id from the last reply message.
  1189. //
  1190. Option = Dhcp6SeekOption (
  1191. LastReply->Dhcp6.Option,
  1192. LastReply->Length - 4,
  1193. Dhcp6OptServerId
  1194. );
  1195. if (Option == NULL) {
  1196. return EFI_DEVICE_ERROR;
  1197. }
  1198. ServerId = (EFI_DHCP6_DUID *) (Option + 2);
  1199. //
  1200. // Create the Dhcp6 packet and initialize commone fields.
  1201. //
  1202. Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE);
  1203. if (Packet == NULL) {
  1204. return EFI_OUT_OF_RESOURCES;
  1205. }
  1206. Packet->Size = DHCP6_BASE_PACKET_SIZE;
  1207. Packet->Length = sizeof (EFI_DHCP6_HEADER);
  1208. Packet->Dhcp6.Header.MessageType = Dhcp6MsgRelease;
  1209. Packet->Dhcp6.Header.TransactionId = Service->Xid++;
  1210. //
  1211. // Assembly Dhcp6 options for rebind/renew message
  1212. //
  1213. Cursor = Packet->Dhcp6.Option;
  1214. Length = HTONS (ClientId->Length);
  1215. Cursor = Dhcp6AppendOption (
  1216. Cursor,
  1217. HTONS (Dhcp6OptClientId),
  1218. Length,
  1219. ClientId->Duid
  1220. );
  1221. //
  1222. // ServerId is extracted from packet, it's network order.
  1223. //
  1224. Cursor = Dhcp6AppendOption (
  1225. Cursor,
  1226. HTONS (Dhcp6OptServerId),
  1227. ServerId->Length,
  1228. ServerId->Duid
  1229. );
  1230. Cursor = Dhcp6AppendETOption (
  1231. Cursor,
  1232. Instance,
  1233. &Elapsed
  1234. );
  1235. Cursor = Dhcp6AppendIaOption (Cursor, RelIa, 0, 0, Packet->Dhcp6.Header.MessageType);
  1236. //
  1237. // Determine the size/length of packet
  1238. //
  1239. Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option);
  1240. ASSERT (Packet->Size > Packet->Length + 8);
  1241. //
  1242. // Callback to user with the packet to be sent and check the user's feedback.
  1243. //
  1244. Status = Dhcp6CallbackUser (Instance, Dhcp6SendRelease, &Packet);
  1245. if (EFI_ERROR (Status)) {
  1246. FreePool (Packet);
  1247. return Status;
  1248. }
  1249. //
  1250. // Send release packet with the state transition from Dhcp6bound to
  1251. // Dhcp6releasing.
  1252. //
  1253. Instance->IaCb.Ia->State = Dhcp6Releasing;
  1254. Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
  1255. if (EFI_ERROR (Status)) {
  1256. FreePool (Packet);
  1257. return Status;
  1258. }
  1259. //
  1260. // Enqueue the sent packet for the retransmission in case reply timeout.
  1261. //
  1262. return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL);
  1263. }
  1264. /**
  1265. Create the renew/rebind message and send it.
  1266. @param[in] Instance The pointer to the Dhcp6 instance.
  1267. @param[in] RebindRequest If TRUE, it is a Rebind type message.
  1268. Otherwise, it is a Renew type message.
  1269. @retval EFI_SUCCESS Created and sent the renew/rebind message successfully.
  1270. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  1271. @retval EFI_DEVICE_ERROR An unexpected error.
  1272. @retval Others Failed to send the renew/rebind message.
  1273. **/
  1274. EFI_STATUS
  1275. Dhcp6SendRenewRebindMsg (
  1276. IN DHCP6_INSTANCE *Instance,
  1277. IN BOOLEAN RebindRequest
  1278. )
  1279. {
  1280. EFI_STATUS Status;
  1281. EFI_DHCP6_PACKET *Packet;
  1282. EFI_DHCP6_PACKET *LastReply;
  1283. EFI_DHCP6_PACKET_OPTION *UserOpt;
  1284. EFI_DHCP6_DUID *ClientId;
  1285. EFI_DHCP6_DUID *ServerId;
  1286. EFI_DHCP6_STATE State;
  1287. EFI_DHCP6_EVENT Event;
  1288. DHCP6_SERVICE *Service;
  1289. UINT8 *Option;
  1290. UINT8 *Cursor;
  1291. UINT16 *Elapsed;
  1292. UINT32 UserLen;
  1293. UINTN Index;
  1294. UINT16 Length;
  1295. ASSERT(Instance->Config);
  1296. ASSERT(Instance->IaCb.Ia);
  1297. Service = Instance->Service;
  1298. ClientId = Service->ClientId;
  1299. ASSERT(ClientId);
  1300. //
  1301. // Calculate the added length of customized option list.
  1302. //
  1303. UserLen = 0;
  1304. for (Index = 0; Index < Instance->Config->OptionCount; Index++) {
  1305. UserLen += (NTOHS (Instance->Config->OptionList[Index]->OpLen) + 4);
  1306. }
  1307. //
  1308. // Create the Dhcp6 packet and initialize commone fields.
  1309. //
  1310. Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen);
  1311. if (Packet == NULL) {
  1312. return EFI_OUT_OF_RESOURCES;
  1313. }
  1314. Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen;
  1315. Packet->Length = sizeof (EFI_DHCP6_HEADER);
  1316. Packet->Dhcp6.Header.MessageType = RebindRequest ? Dhcp6MsgRebind : Dhcp6MsgRenew;
  1317. Packet->Dhcp6.Header.TransactionId = Service->Xid++;
  1318. //
  1319. // Assembly Dhcp6 options for rebind/renew message.
  1320. //
  1321. Cursor = Packet->Dhcp6.Option;
  1322. Length = HTONS (ClientId->Length);
  1323. Cursor = Dhcp6AppendOption (
  1324. Cursor,
  1325. HTONS (Dhcp6OptClientId),
  1326. Length,
  1327. ClientId->Duid
  1328. );
  1329. Cursor = Dhcp6AppendETOption (
  1330. Cursor,
  1331. Instance,
  1332. &Elapsed
  1333. );
  1334. Cursor = Dhcp6AppendIaOption (
  1335. Cursor,
  1336. Instance->IaCb.Ia,
  1337. Instance->IaCb.T1,
  1338. Instance->IaCb.T2,
  1339. Packet->Dhcp6.Header.MessageType
  1340. );
  1341. if (!RebindRequest) {
  1342. //
  1343. // Get the server Id from the last reply message and
  1344. // insert it for rebind request.
  1345. //
  1346. LastReply = Instance->IaCb.Ia->ReplyPacket;
  1347. ASSERT (LastReply);
  1348. Option = Dhcp6SeekOption (
  1349. LastReply->Dhcp6.Option,
  1350. LastReply->Length - 4,
  1351. Dhcp6OptServerId
  1352. );
  1353. if (Option == NULL) {
  1354. FreePool (Packet);
  1355. return EFI_DEVICE_ERROR;
  1356. }
  1357. ServerId = (EFI_DHCP6_DUID *) (Option + 2);
  1358. Cursor = Dhcp6AppendOption (
  1359. Cursor,
  1360. HTONS (Dhcp6OptServerId),
  1361. ServerId->Length,
  1362. ServerId->Duid
  1363. );
  1364. }
  1365. //
  1366. // Append user-defined when configurate Dhcp6 service.
  1367. //
  1368. for (Index = 0; Index < Instance->Config->OptionCount; Index++) {
  1369. UserOpt = Instance->Config->OptionList[Index];
  1370. Cursor = Dhcp6AppendOption(
  1371. Cursor,
  1372. UserOpt->OpCode,
  1373. UserOpt->OpLen,
  1374. UserOpt->Data
  1375. );
  1376. }
  1377. //
  1378. // Determine the size/length of packet.
  1379. //
  1380. Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option);
  1381. ASSERT (Packet->Size > Packet->Length + 8);
  1382. //
  1383. // Callback to user with the packet to be sent and check the user's feedback.
  1384. //
  1385. State = (RebindRequest) ? Dhcp6Rebinding : Dhcp6Renewing;
  1386. Event = (RebindRequest) ? Dhcp6EnterRebinding : Dhcp6EnterRenewing;
  1387. Status = Dhcp6CallbackUser (Instance, Event, &Packet);
  1388. if (EFI_ERROR (Status)) {
  1389. FreePool (Packet);
  1390. return Status;
  1391. }
  1392. //
  1393. // Send renew/rebind packet with the state transition from Dhcp6bound to
  1394. // Dhcp6renew/rebind.
  1395. // And sync the lease time when send renew/rebind, in case that user send
  1396. // renew/rebind actively.
  1397. //
  1398. Instance->IaCb.Ia->State = State;
  1399. Instance->IaCb.LeaseTime = (RebindRequest) ? Instance->IaCb.T2 : Instance->IaCb.T1;
  1400. //
  1401. // Clear initial time for current transaction.
  1402. //
  1403. Instance->StartTime = 0;
  1404. Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
  1405. if (EFI_ERROR (Status)) {
  1406. FreePool (Packet);
  1407. return Status;
  1408. }
  1409. //
  1410. // Enqueue the sent packet for the retransmission in case reply timeout.
  1411. //
  1412. return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL);
  1413. }
  1414. /**
  1415. Start the information request process.
  1416. @param[in] Instance The pointer to the Dhcp6 instance.
  1417. @param[in] SendClientId If TRUE, the client identifier option will be included in
  1418. information request message. Otherwise, the client identifier
  1419. option will not be included.
  1420. @param[in] OptionRequest The pointer to the option request option.
  1421. @param[in] OptionCount The number options in the OptionList.
  1422. @param[in] OptionList The array pointers to the appended options.
  1423. @param[in] Retransmission The pointer to the retransmission control.
  1424. @param[in] TimeoutEvent The event of timeout.
  1425. @param[in] ReplyCallback The callback function when the reply was received.
  1426. @param[in] CallbackContext The pointer to the parameter passed to the callback.
  1427. @retval EFI_SUCCESS Start the info-request process successfully.
  1428. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  1429. @retval EFI_NO_MAPPING No source address is available for use.
  1430. @retval Others Failed to start the info-request process.
  1431. **/
  1432. EFI_STATUS
  1433. Dhcp6StartInfoRequest (
  1434. IN DHCP6_INSTANCE *Instance,
  1435. IN BOOLEAN SendClientId,
  1436. IN EFI_DHCP6_PACKET_OPTION *OptionRequest,
  1437. IN UINT32 OptionCount,
  1438. IN EFI_DHCP6_PACKET_OPTION *OptionList[] OPTIONAL,
  1439. IN EFI_DHCP6_RETRANSMISSION *Retransmission,
  1440. IN EFI_EVENT TimeoutEvent OPTIONAL,
  1441. IN EFI_DHCP6_INFO_CALLBACK ReplyCallback,
  1442. IN VOID *CallbackContext OPTIONAL
  1443. )
  1444. {
  1445. EFI_STATUS Status;
  1446. DHCP6_INF_CB *InfCb;
  1447. DHCP6_SERVICE *Service;
  1448. EFI_TPL OldTpl;
  1449. Service = Instance->Service;
  1450. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1451. Instance->UdpSts = EFI_ALREADY_STARTED;
  1452. //
  1453. // Create and initialize the control block for the info-request.
  1454. //
  1455. InfCb = AllocateZeroPool (sizeof(DHCP6_INF_CB));
  1456. if (InfCb == NULL) {
  1457. gBS->RestoreTPL (OldTpl);
  1458. return EFI_OUT_OF_RESOURCES;
  1459. }
  1460. InfCb->ReplyCallback = ReplyCallback;
  1461. InfCb->CallbackContext = CallbackContext;
  1462. InfCb->TimeoutEvent = TimeoutEvent;
  1463. InsertTailList (&Instance->InfList, &InfCb->Link);
  1464. //
  1465. // Send the info-request message to start exchange process.
  1466. //
  1467. Status = Dhcp6SendInfoRequestMsg (
  1468. Instance,
  1469. InfCb,
  1470. SendClientId,
  1471. OptionRequest,
  1472. OptionCount,
  1473. OptionList,
  1474. Retransmission
  1475. );
  1476. if (EFI_ERROR (Status)) {
  1477. goto ON_ERROR;
  1478. }
  1479. //
  1480. // Register receive callback for the stateless exchange process.
  1481. //
  1482. Status = UdpIoRecvDatagram(
  1483. Service->UdpIo,
  1484. Dhcp6ReceivePacket,
  1485. Service,
  1486. 0
  1487. );
  1488. if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
  1489. goto ON_ERROR;
  1490. }
  1491. gBS->RestoreTPL (OldTpl);
  1492. return EFI_SUCCESS;
  1493. ON_ERROR:
  1494. gBS->RestoreTPL (OldTpl);
  1495. RemoveEntryList (&InfCb->Link);
  1496. FreePool (InfCb);
  1497. return Status;
  1498. }
  1499. /**
  1500. Create the information request message and send it.
  1501. @param[in] Instance The pointer to the Dhcp6 instance.
  1502. @param[in] InfCb The pointer to the information request control block.
  1503. @param[in] SendClientId If TRUE, the client identifier option will be included in
  1504. information request message. Otherwise, the client identifier
  1505. option will not be included.
  1506. @param[in] OptionRequest The pointer to the option request option.
  1507. @param[in] OptionCount The number options in the OptionList.
  1508. @param[in] OptionList The array pointers to the appended options.
  1509. @param[in] Retransmission The pointer to the retransmission control.
  1510. @retval EFI_SUCCESS Created and sent the info-request message successfully.
  1511. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  1512. @retval Others Failed to send the info-request message.
  1513. **/
  1514. EFI_STATUS
  1515. Dhcp6SendInfoRequestMsg (
  1516. IN DHCP6_INSTANCE *Instance,
  1517. IN DHCP6_INF_CB *InfCb,
  1518. IN BOOLEAN SendClientId,
  1519. IN EFI_DHCP6_PACKET_OPTION *OptionRequest,
  1520. IN UINT32 OptionCount,
  1521. IN EFI_DHCP6_PACKET_OPTION *OptionList[],
  1522. IN EFI_DHCP6_RETRANSMISSION *Retransmission
  1523. )
  1524. {
  1525. EFI_STATUS Status;
  1526. EFI_DHCP6_PACKET *Packet;
  1527. EFI_DHCP6_PACKET_OPTION *UserOpt;
  1528. EFI_DHCP6_DUID *ClientId;
  1529. DHCP6_SERVICE *Service;
  1530. UINT8 *Cursor;
  1531. UINT16 *Elapsed;
  1532. UINT32 UserLen;
  1533. UINTN Index;
  1534. UINT16 Length;
  1535. ASSERT(OptionRequest);
  1536. Service = Instance->Service;
  1537. ClientId = Service->ClientId;
  1538. UserLen = NTOHS (OptionRequest->OpLen) + 4;
  1539. ASSERT(ClientId);
  1540. //
  1541. // Calculate the added length of customized option list.
  1542. //
  1543. for (Index = 0; Index < OptionCount; Index++) {
  1544. UserLen += (NTOHS (OptionList[Index]->OpLen) + 4);
  1545. }
  1546. //
  1547. // Create the Dhcp6 packet and initialize commone fields.
  1548. //
  1549. Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen);
  1550. if (Packet == NULL) {
  1551. return EFI_OUT_OF_RESOURCES;
  1552. }
  1553. Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen;
  1554. Packet->Length = sizeof (EFI_DHCP6_HEADER);
  1555. Packet->Dhcp6.Header.MessageType = Dhcp6MsgInfoRequest;
  1556. Packet->Dhcp6.Header.TransactionId = Service->Xid++;
  1557. InfCb->Xid = Packet->Dhcp6.Header.TransactionId;
  1558. //
  1559. // Assembly Dhcp6 options for info-request message.
  1560. //
  1561. Cursor = Packet->Dhcp6.Option;
  1562. if (SendClientId) {
  1563. Length = HTONS (ClientId->Length);
  1564. Cursor = Dhcp6AppendOption (
  1565. Cursor,
  1566. HTONS (Dhcp6OptClientId),
  1567. Length,
  1568. ClientId->Duid
  1569. );
  1570. }
  1571. Cursor = Dhcp6AppendETOption (
  1572. Cursor,
  1573. Instance,
  1574. &Elapsed
  1575. );
  1576. Cursor = Dhcp6AppendOption (
  1577. Cursor,
  1578. OptionRequest->OpCode,
  1579. OptionRequest->OpLen,
  1580. OptionRequest->Data
  1581. );
  1582. //
  1583. // Append user-defined when configurate Dhcp6 service.
  1584. //
  1585. for (Index = 0; Index < OptionCount; Index++) {
  1586. UserOpt = OptionList[Index];
  1587. Cursor = Dhcp6AppendOption(
  1588. Cursor,
  1589. UserOpt->OpCode,
  1590. UserOpt->OpLen,
  1591. UserOpt->Data
  1592. );
  1593. }
  1594. //
  1595. // Determine the size/length of packet.
  1596. //
  1597. Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option);
  1598. ASSERT (Packet->Size > Packet->Length + 8);
  1599. //
  1600. // Clear initial time for current transaction.
  1601. //
  1602. Instance->StartTime = 0;
  1603. //
  1604. // Send info-request packet with no state.
  1605. //
  1606. Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
  1607. if (EFI_ERROR (Status)) {
  1608. FreePool (Packet);
  1609. return Status;
  1610. }
  1611. //
  1612. // Enqueue the sent packet for the retransmission in case reply timeout.
  1613. //
  1614. return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, Retransmission);
  1615. }
  1616. /**
  1617. Create the Confirm message and send it.
  1618. @param[in] Instance The pointer to the Dhcp6 instance.
  1619. @retval EFI_SUCCESS Created and sent the confirm message successfully.
  1620. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  1621. @retval EFI_DEVICE_ERROR An unexpected error.
  1622. @retval Others Failed to send the confirm message.
  1623. **/
  1624. EFI_STATUS
  1625. Dhcp6SendConfirmMsg (
  1626. IN DHCP6_INSTANCE *Instance
  1627. )
  1628. {
  1629. UINT8 *Cursor;
  1630. UINTN Index;
  1631. UINT16 Length;
  1632. UINT32 UserLen;
  1633. EFI_STATUS Status;
  1634. DHCP6_SERVICE *Service;
  1635. EFI_DHCP6_DUID *ClientId;
  1636. EFI_DHCP6_PACKET *Packet;
  1637. EFI_DHCP6_PACKET_OPTION *UserOpt;
  1638. UINT16 *Elapsed;
  1639. ASSERT (Instance->Config != NULL);
  1640. ASSERT (Instance->IaCb.Ia != NULL);
  1641. ASSERT (Instance->Service != NULL);
  1642. Service = Instance->Service;
  1643. ClientId = Service->ClientId;
  1644. ASSERT (ClientId != NULL);
  1645. //
  1646. // Calculate the added length of customized option list.
  1647. //
  1648. UserLen = 0;
  1649. for (Index = 0; Index < Instance->Config->OptionCount; Index++) {
  1650. UserLen += (NTOHS (Instance->Config->OptionList[Index]->OpLen) + 4);
  1651. }
  1652. //
  1653. // Create the Dhcp6 packet and initialize common fields.
  1654. //
  1655. Packet = AllocateZeroPool (DHCP6_BASE_PACKET_SIZE + UserLen);
  1656. if (Packet == NULL) {
  1657. return EFI_OUT_OF_RESOURCES;
  1658. }
  1659. Packet->Size = DHCP6_BASE_PACKET_SIZE + UserLen;
  1660. Packet->Length = sizeof (EFI_DHCP6_HEADER);
  1661. Packet->Dhcp6.Header.MessageType = Dhcp6MsgConfirm;
  1662. Packet->Dhcp6.Header.TransactionId = Service->Xid++;
  1663. //
  1664. // Assembly Dhcp6 options for solicit message.
  1665. //
  1666. Cursor = Packet->Dhcp6.Option;
  1667. Length = HTONS (ClientId->Length);
  1668. Cursor = Dhcp6AppendOption (
  1669. Cursor,
  1670. HTONS (Dhcp6OptClientId),
  1671. Length,
  1672. ClientId->Duid
  1673. );
  1674. Cursor = Dhcp6AppendETOption (
  1675. Cursor,
  1676. Instance,
  1677. &Elapsed
  1678. );
  1679. Cursor = Dhcp6AppendIaOption (
  1680. Cursor,
  1681. Instance->IaCb.Ia,
  1682. Instance->IaCb.T1,
  1683. Instance->IaCb.T2,
  1684. Packet->Dhcp6.Header.MessageType
  1685. );
  1686. //
  1687. // Append user-defined when configurate Dhcp6 service.
  1688. //
  1689. for (Index = 0; Index < Instance->Config->OptionCount; Index++) {
  1690. UserOpt = Instance->Config->OptionList[Index];
  1691. Cursor = Dhcp6AppendOption (
  1692. Cursor,
  1693. UserOpt->OpCode,
  1694. UserOpt->OpLen,
  1695. UserOpt->Data
  1696. );
  1697. }
  1698. //
  1699. // Determine the size/length of packet.
  1700. //
  1701. Packet->Length += (UINT32) (Cursor - Packet->Dhcp6.Option);
  1702. ASSERT (Packet->Size > Packet->Length + 8);
  1703. //
  1704. // Callback to user with the packet to be sent and check the user's feedback.
  1705. //
  1706. Status = Dhcp6CallbackUser (Instance, Dhcp6SendConfirm, &Packet);
  1707. if (EFI_ERROR (Status)) {
  1708. FreePool (Packet);
  1709. return Status;
  1710. }
  1711. //
  1712. // Send confirm packet with the state transition from Dhcp6Bound to
  1713. // Dhcp6Confirming.
  1714. //
  1715. Instance->IaCb.Ia->State = Dhcp6Confirming;
  1716. //
  1717. // Clear initial time for current transaction.
  1718. //
  1719. Instance->StartTime = 0;
  1720. Status = Dhcp6TransmitPacket (Instance, Packet, Elapsed);
  1721. if (EFI_ERROR (Status)) {
  1722. FreePool (Packet);
  1723. return Status;
  1724. }
  1725. //
  1726. // Enqueue the sent packet for the retransmission in case reply timeout.
  1727. //
  1728. return Dhcp6EnqueueRetry (Instance, Packet, Elapsed, NULL);
  1729. }
  1730. /**
  1731. Handle with the Dhcp6 reply message.
  1732. @param[in] Instance The pointer to Dhcp6 instance.
  1733. @param[in] Packet The pointer to the Dhcp6 reply message.
  1734. @retval EFI_SUCCESS Processed the reply message successfully.
  1735. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  1736. @retval EFI_DEVICE_ERROR An unexpected error.
  1737. @retval Others Failed to process the reply message.
  1738. **/
  1739. EFI_STATUS
  1740. Dhcp6HandleReplyMsg (
  1741. IN DHCP6_INSTANCE *Instance,
  1742. IN EFI_DHCP6_PACKET *Packet
  1743. )
  1744. {
  1745. EFI_STATUS Status;
  1746. UINT8 *Option;
  1747. UINT16 StsCode;
  1748. ASSERT (Instance->Config != NULL);
  1749. ASSERT (Instance->IaCb.Ia != NULL);
  1750. ASSERT (Packet != NULL);
  1751. Status = EFI_SUCCESS;
  1752. if (Packet->Dhcp6.Header.MessageType != Dhcp6MsgReply) {
  1753. return EFI_DEVICE_ERROR;
  1754. }
  1755. //
  1756. // If the client subsequently receives a valid reply message that includes a
  1757. // rapid commit option since send a solicit with rapid commit option before,
  1758. // preocess the reply message and discard any reply messages received in
  1759. // response to the request message.
  1760. // See details in the section-17.1.4 of rfc-3315.
  1761. //
  1762. Option = Dhcp6SeekOption (
  1763. Packet->Dhcp6.Option,
  1764. Packet->Length - 4,
  1765. Dhcp6OptRapidCommit
  1766. );
  1767. if ((Option != NULL && !Instance->Config->RapidCommit) || (Option == NULL && Instance->Config->RapidCommit)) {
  1768. return EFI_DEVICE_ERROR;
  1769. }
  1770. //
  1771. // As to a valid reply packet in response to a request/renew/rebind packet,
  1772. // ignore the packet if not contains the Ia option
  1773. //
  1774. if (Instance->IaCb.Ia->State == Dhcp6Requesting ||
  1775. Instance->IaCb.Ia->State == Dhcp6Renewing ||
  1776. Instance->IaCb.Ia->State == Dhcp6Rebinding
  1777. ) {
  1778. Option = Dhcp6SeekIaOption (
  1779. Packet->Dhcp6.Option,
  1780. Packet->Length,
  1781. &Instance->Config->IaDescriptor
  1782. );
  1783. if (Option == NULL) {
  1784. return EFI_SUCCESS;
  1785. }
  1786. }
  1787. //
  1788. // Callback to user with the received packet and check the user's feedback.
  1789. //
  1790. Status = Dhcp6CallbackUser (Instance, Dhcp6RcvdReply, &Packet);
  1791. if (EFI_ERROR (Status)) {
  1792. return Status;
  1793. }
  1794. //
  1795. // When receive a valid reply packet in response to a decline/release packet,
  1796. // the client considers the decline/release event completed regardless of the
  1797. // status code.
  1798. //
  1799. if (Instance->IaCb.Ia->State == Dhcp6Declining || Instance->IaCb.Ia->State == Dhcp6Releasing) {
  1800. if (Instance->IaCb.Ia->IaAddressCount != 0) {
  1801. Instance->IaCb.Ia->State = Dhcp6Bound;
  1802. } else {
  1803. ASSERT (Instance->IaCb.Ia->ReplyPacket);
  1804. FreePool (Instance->IaCb.Ia->ReplyPacket);
  1805. Instance->IaCb.Ia->ReplyPacket = NULL;
  1806. Instance->IaCb.Ia->State = Dhcp6Init;
  1807. }
  1808. //
  1809. // For sync, set the success flag out of polling in decline/release.
  1810. //
  1811. Instance->UdpSts = EFI_SUCCESS;
  1812. //
  1813. // For async, signal the Ia event to inform Ia infomation update.
  1814. //
  1815. if (Instance->Config->IaInfoEvent != NULL) {
  1816. gBS->SignalEvent (Instance->Config->IaInfoEvent);
  1817. }
  1818. //
  1819. // Reset start time for next exchange.
  1820. //
  1821. Instance->StartTime = 0;
  1822. Status = EFI_SUCCESS;
  1823. goto ON_EXIT;
  1824. }
  1825. //
  1826. // Upon the receipt of a valid reply packet in response to a solicit, request,
  1827. // confirm, renew and rebind, the behavior depends on the status code option.
  1828. // See the details in the section-18.1.8 of rfc-3315.
  1829. //
  1830. Option = NULL;
  1831. Status = Dhcp6SeekStsOption (
  1832. Instance,
  1833. Packet,
  1834. &Option
  1835. );
  1836. if (!EFI_ERROR (Status)) {
  1837. //
  1838. // No status code or no error status code means succeed to reply.
  1839. //
  1840. Status = Dhcp6UpdateIaInfo (Instance, Packet);
  1841. if (!EFI_ERROR (Status)) {
  1842. //
  1843. // Reset start time for next exchange.
  1844. //
  1845. Instance->StartTime = 0;
  1846. //
  1847. // Set bound state and store the reply packet.
  1848. //
  1849. if (Instance->IaCb.Ia->ReplyPacket != NULL) {
  1850. FreePool (Instance->IaCb.Ia->ReplyPacket);
  1851. }
  1852. Instance->IaCb.Ia->ReplyPacket = AllocateZeroPool (Packet->Size);
  1853. if (Instance->IaCb.Ia->ReplyPacket == NULL) {
  1854. Status = EFI_OUT_OF_RESOURCES;
  1855. goto ON_EXIT;
  1856. }
  1857. CopyMem (Instance->IaCb.Ia->ReplyPacket, Packet, Packet->Size);
  1858. Instance->IaCb.Ia->State = Dhcp6Bound;
  1859. //
  1860. // For sync, set the success flag out of polling in start/renewrebind.
  1861. //
  1862. Instance->UdpSts = EFI_SUCCESS;
  1863. //
  1864. // Maybe this is a new round DHCP process due to some reason, such as NotOnLink
  1865. // ReplyMsg for ConfirmMsg should triger new round to acquire new address. In that
  1866. // case, clear old address.ValidLifetime and append to new address. Therefore, DHCP
  1867. // consumers can be notified to flush old address.
  1868. //
  1869. Dhcp6AppendCacheIa (Instance);
  1870. //
  1871. // For async, signal the Ia event to inform Ia infomation update.
  1872. //
  1873. if (Instance->Config->IaInfoEvent != NULL) {
  1874. gBS->SignalEvent (Instance->Config->IaInfoEvent);
  1875. }
  1876. } else if (Status == EFI_NOT_FOUND) {
  1877. //
  1878. // Refer to RFC3315 Chapter 18.1.8, for each IA in the original Renew or Rebind message,
  1879. // the client sends a Renew or Rebind if the IA is not in the Reply message.
  1880. // Return EFI_SUCCESS so we can continue to restart the Renew/Rebind process.
  1881. //
  1882. return EFI_SUCCESS;
  1883. }
  1884. goto ON_EXIT;
  1885. } else if (Option != NULL) {
  1886. //
  1887. // Any error status code option is found.
  1888. //
  1889. StsCode = NTOHS (ReadUnaligned16 ((UINT16 *) (Option + 4)));
  1890. switch (StsCode) {
  1891. case Dhcp6StsUnspecFail:
  1892. //
  1893. // It indicates the server is unable to process the message due to an
  1894. // unspecified failure condition, so just retry if possible.
  1895. //
  1896. break;
  1897. case Dhcp6StsUseMulticast:
  1898. //
  1899. // It indicates the server receives a message via unicast from a client
  1900. // to which the server has not sent a unicast option, so retry it by
  1901. // multi-cast address.
  1902. //
  1903. if (Instance->Unicast != NULL) {
  1904. FreePool (Instance->Unicast);
  1905. Instance->Unicast = NULL;
  1906. }
  1907. break;
  1908. case Dhcp6StsNotOnLink:
  1909. if (Instance->IaCb.Ia->State == Dhcp6Confirming) {
  1910. //
  1911. // Before initiate new round DHCP, cache the current IA.
  1912. //
  1913. Status = Dhcp6CacheIa (Instance);
  1914. if (EFI_ERROR (Status)) {
  1915. return Status;
  1916. }
  1917. //
  1918. // Restart S.A.R.R process to acquire new address.
  1919. //
  1920. Status = Dhcp6InitSolicitMsg (Instance);
  1921. if (EFI_ERROR (Status)) {
  1922. return Status;
  1923. }
  1924. }
  1925. break;
  1926. case Dhcp6StsNoBinding:
  1927. if (Instance->IaCb.Ia->State == Dhcp6Renewing || Instance->IaCb.Ia->State == Dhcp6Rebinding) {
  1928. //
  1929. // Refer to RFC3315 Chapter 18.1.8, for each IA in the original Renew or Rebind message, the client
  1930. // sends a Request message if the IA contained a Status Code option with the NoBinding status.
  1931. //
  1932. Status = Dhcp6SendRequestMsg(Instance);
  1933. if (EFI_ERROR (Status)) {
  1934. return Status;
  1935. }
  1936. }
  1937. break;
  1938. default:
  1939. //
  1940. // The other status code, just restart solicitation.
  1941. //
  1942. break;
  1943. }
  1944. }
  1945. return EFI_SUCCESS;
  1946. ON_EXIT:
  1947. if (!EFI_ERROR(Status)) {
  1948. Status = Dhcp6DequeueRetry (
  1949. Instance,
  1950. Packet->Dhcp6.Header.TransactionId,
  1951. FALSE
  1952. );
  1953. }
  1954. return Status;
  1955. }
  1956. /**
  1957. Select the appointed Dhcp6 advertisement message.
  1958. @param[in] Instance The pointer to the Dhcp6 instance.
  1959. @param[in] AdSelect The pointer to the selected Dhcp6 advertisement message.
  1960. @retval EFI_SUCCESS Selected the right advertisement message successfully.
  1961. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  1962. @retval Others Failed to select the advertise message.
  1963. **/
  1964. EFI_STATUS
  1965. Dhcp6SelectAdvertiseMsg (
  1966. IN DHCP6_INSTANCE *Instance,
  1967. IN EFI_DHCP6_PACKET *AdSelect
  1968. )
  1969. {
  1970. EFI_STATUS Status;
  1971. UINT8 *Option;
  1972. ASSERT (AdSelect != NULL);
  1973. //
  1974. // Callback to user with the selected advertisement packet, and the user
  1975. // might overwrite it.
  1976. //
  1977. Status = Dhcp6CallbackUser (Instance, Dhcp6SelectAdvertise, &AdSelect);
  1978. if (EFI_ERROR (Status)) {
  1979. return Status;
  1980. }
  1981. Instance->AdSelect = AdSelect;
  1982. //
  1983. // Dequeue the sent packet for the retransmission since advertisement selected.
  1984. //
  1985. Status = Dhcp6DequeueRetry (
  1986. Instance,
  1987. AdSelect->Dhcp6.Header.TransactionId,
  1988. FALSE
  1989. );
  1990. if (EFI_ERROR(Status)) {
  1991. return Status;
  1992. }
  1993. //
  1994. // Check whether there is server unicast option in the selected advertise
  1995. // packet, and update it.
  1996. //
  1997. Option = Dhcp6SeekOption(
  1998. AdSelect->Dhcp6.Option,
  1999. AdSelect->Length - 4,
  2000. Dhcp6OptServerUnicast
  2001. );
  2002. if (Option != NULL) {
  2003. Instance->Unicast = AllocateZeroPool (sizeof(EFI_IPv6_ADDRESS));
  2004. if (Instance->Unicast == NULL) {
  2005. return EFI_OUT_OF_RESOURCES;
  2006. }
  2007. CopyMem (Instance->Unicast, Option + 4, sizeof(EFI_IPv6_ADDRESS));
  2008. }
  2009. //
  2010. // Update the information of the Ia by the selected advertisement message.
  2011. //
  2012. Status = Dhcp6UpdateIaInfo (Instance, AdSelect);
  2013. if (EFI_ERROR (Status)) {
  2014. return Status;
  2015. }
  2016. //
  2017. // Send the request message to continue the S.A.R.R. process.
  2018. //
  2019. return Dhcp6SendRequestMsg (Instance);
  2020. }
  2021. /**
  2022. Handle with the Dhcp6 advertisement message.
  2023. @param[in] Instance The pointer to the Dhcp6 instance.
  2024. @param[in] Packet The pointer to the Dhcp6 advertisement message.
  2025. @retval EFI_SUCCESS Processed the advertisement message successfully.
  2026. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  2027. @retval EFI_DEVICE_ERROR An unexpected error.
  2028. @retval Others Failed to process the advertise message.
  2029. **/
  2030. EFI_STATUS
  2031. Dhcp6HandleAdvertiseMsg (
  2032. IN DHCP6_INSTANCE *Instance,
  2033. IN EFI_DHCP6_PACKET *Packet
  2034. )
  2035. {
  2036. EFI_STATUS Status;
  2037. UINT8 *Option;
  2038. BOOLEAN Timeout;
  2039. ASSERT(Instance->Config);
  2040. ASSERT(Instance->IaCb.Ia);
  2041. Timeout = FALSE;
  2042. //
  2043. // If the client does receives a valid reply message that includes a rapid
  2044. // commit option since a solicit with rapid commit optioin sent before, select
  2045. // this reply message. Or else, process the advertise messages as normal.
  2046. // See details in the section-17.1.4 of rfc-3315.
  2047. //
  2048. Option = Dhcp6SeekOption(
  2049. Packet->Dhcp6.Option,
  2050. Packet->Length - 4,
  2051. Dhcp6OptRapidCommit
  2052. );
  2053. if (Option != NULL && Instance->Config->RapidCommit && Packet->Dhcp6.Header.MessageType == Dhcp6MsgReply) {
  2054. return Dhcp6HandleReplyMsg (Instance, Packet);
  2055. }
  2056. if (Packet->Dhcp6.Header.MessageType != Dhcp6MsgAdvertise) {
  2057. return EFI_DEVICE_ERROR;
  2058. }
  2059. //
  2060. // Client must ignore any advertise message that includes a status code option
  2061. // containing the value noaddrsavail, with the exception that the client may
  2062. // display the associated status message to the user.
  2063. // See the details in the section-17.1.3 of rfc-3315.
  2064. //
  2065. Status = Dhcp6SeekStsOption (
  2066. Instance,
  2067. Packet,
  2068. &Option
  2069. );
  2070. if (EFI_ERROR (Status)) {
  2071. return EFI_DEVICE_ERROR;
  2072. }
  2073. //
  2074. // Callback to user with the received packet and check the user's feedback.
  2075. //
  2076. Status = Dhcp6CallbackUser (Instance, Dhcp6RcvdAdvertise, &Packet);
  2077. if (!EFI_ERROR (Status)) {
  2078. //
  2079. // Success means user choose the current advertisement packet.
  2080. //
  2081. if (Instance->AdSelect != NULL) {
  2082. FreePool (Instance->AdSelect);
  2083. }
  2084. //
  2085. // Store the selected advertisement packet and set a flag.
  2086. //
  2087. Instance->AdSelect = AllocateZeroPool (Packet->Size);
  2088. if (Instance->AdSelect == NULL) {
  2089. return EFI_OUT_OF_RESOURCES;
  2090. }
  2091. CopyMem (Instance->AdSelect, Packet, Packet->Size);
  2092. Instance->AdPref = 0xff;
  2093. } else if (Status == EFI_NOT_READY) {
  2094. //
  2095. // Not_ready means user wants to continue to receive more advertise packets.
  2096. //
  2097. if (Instance->AdPref == 0xff && Instance->AdSelect == NULL) {
  2098. //
  2099. // It's a tricky point. The timer routine set adpref as 0xff if the first
  2100. // rt timeout and no advertisement received, which means any advertisement
  2101. // received will be selected after the first rt.
  2102. //
  2103. Timeout = TRUE;
  2104. }
  2105. //
  2106. // Check whether the current packet has a 255 preference option or not.
  2107. // Take non-preference option as 0 value.
  2108. //
  2109. Option = Dhcp6SeekOption(
  2110. Packet->Dhcp6.Option,
  2111. Packet->Length - 4,
  2112. Dhcp6OptPreference
  2113. );
  2114. if (Instance->AdSelect == NULL || (Option != NULL && *(Option + 4) > Instance->AdPref)) {
  2115. //
  2116. // No advertisements received before or preference is more than other
  2117. // advertisements received before. Then store the new packet and the
  2118. // preference value.
  2119. //
  2120. if (Instance->AdSelect != NULL) {
  2121. FreePool (Instance->AdSelect);
  2122. }
  2123. Instance->AdSelect = AllocateZeroPool (Packet->Size);
  2124. if (Instance->AdSelect == NULL) {
  2125. return EFI_OUT_OF_RESOURCES;
  2126. }
  2127. CopyMem (Instance->AdSelect, Packet, Packet->Size);
  2128. if (Option != NULL) {
  2129. Instance->AdPref = *(Option + 4);
  2130. }
  2131. } else {
  2132. //
  2133. // Non-preference and other advertisements received before or current
  2134. // preference is less than other advertisements received before.
  2135. // Leave the packet alone.
  2136. }
  2137. } else {
  2138. //
  2139. // Other error status means termination.
  2140. //
  2141. return Status;
  2142. }
  2143. //
  2144. // Client must collect advertise messages as more as possible until the first
  2145. // RT has elapsed, or get a highest preference 255 advertise.
  2146. // See details in the section-17.1.2 of rfc-3315.
  2147. //
  2148. if (Instance->AdPref == 0xff || Timeout) {
  2149. Status = Dhcp6SelectAdvertiseMsg (Instance, Instance->AdSelect);
  2150. }
  2151. return Status;
  2152. }
  2153. /**
  2154. The Dhcp6 stateful exchange process routine.
  2155. @param[in] Instance The pointer to the Dhcp6 instance.
  2156. @param[in] Packet The pointer to the received Dhcp6 message.
  2157. **/
  2158. VOID
  2159. Dhcp6HandleStateful (
  2160. IN DHCP6_INSTANCE *Instance,
  2161. IN EFI_DHCP6_PACKET *Packet
  2162. )
  2163. {
  2164. EFI_STATUS Status;
  2165. EFI_DHCP6_DUID *ClientId;
  2166. DHCP6_SERVICE *Service;
  2167. UINT8 *Option;
  2168. Service = Instance->Service;
  2169. ClientId = Service->ClientId;
  2170. Status = EFI_SUCCESS;
  2171. if (Instance->Config == NULL) {
  2172. goto ON_CONTINUE;
  2173. }
  2174. ASSERT (ClientId);
  2175. ASSERT (Instance->Config);
  2176. ASSERT (Instance->IaCb.Ia);
  2177. //
  2178. // Discard the packet if not advertisement or reply packet.
  2179. //
  2180. if (Packet->Dhcp6.Header.MessageType != Dhcp6MsgAdvertise && Packet->Dhcp6.Header.MessageType != Dhcp6MsgReply) {
  2181. goto ON_CONTINUE;
  2182. }
  2183. //
  2184. // Check whether include client Id or not.
  2185. //
  2186. Option = Dhcp6SeekOption(
  2187. Packet->Dhcp6.Option,
  2188. Packet->Length - 4,
  2189. Dhcp6OptClientId
  2190. );
  2191. if (Option == NULL || CompareMem (Option + 4, ClientId->Duid, ClientId->Length) != 0) {
  2192. goto ON_CONTINUE;
  2193. }
  2194. //
  2195. // Check whether include server Id or not.
  2196. //
  2197. Option = Dhcp6SeekOption(
  2198. Packet->Dhcp6.Option,
  2199. Packet->Length - 4,
  2200. Dhcp6OptServerId
  2201. );
  2202. if (Option == NULL) {
  2203. goto ON_CONTINUE;
  2204. }
  2205. switch (Instance->IaCb.Ia->State) {
  2206. case Dhcp6Selecting:
  2207. //
  2208. // Handle the advertisement message when in the Dhcp6Selecting state.
  2209. // Do not need check return status, if failed, just continue to the next.
  2210. //
  2211. Dhcp6HandleAdvertiseMsg (Instance, Packet);
  2212. break;
  2213. case Dhcp6Requesting:
  2214. case Dhcp6Confirming:
  2215. case Dhcp6Renewing:
  2216. case Dhcp6Rebinding:
  2217. case Dhcp6Releasing:
  2218. case Dhcp6Declining:
  2219. //
  2220. // Handle the reply message when in the Dhcp6Requesting, Dhcp6Renewing
  2221. // Dhcp6Rebinding, Dhcp6Releasing and Dhcp6Declining state.
  2222. // If failed here, it should reset the current session.
  2223. //
  2224. Status = Dhcp6HandleReplyMsg (Instance, Packet);
  2225. if (EFI_ERROR (Status)) {
  2226. goto ON_EXIT;
  2227. }
  2228. break;
  2229. default:
  2230. //
  2231. // Other state has not supported yet.
  2232. //
  2233. break;
  2234. }
  2235. ON_CONTINUE:
  2236. //
  2237. // Continue to receive the following Dhcp6 message.
  2238. //
  2239. Status = UdpIoRecvDatagram (
  2240. Service->UdpIo,
  2241. Dhcp6ReceivePacket,
  2242. Service,
  2243. 0
  2244. );
  2245. ON_EXIT:
  2246. if (EFI_ERROR (Status)) {
  2247. Dhcp6CleanupSession (Instance, Status);
  2248. }
  2249. }
  2250. /**
  2251. The Dhcp6 stateless exchange process routine.
  2252. @param[in] Instance The pointer to the Dhcp6 instance.
  2253. @param[in] Packet The pointer to the received Dhcp6 message.
  2254. **/
  2255. VOID
  2256. Dhcp6HandleStateless (
  2257. IN DHCP6_INSTANCE *Instance,
  2258. IN EFI_DHCP6_PACKET *Packet
  2259. )
  2260. {
  2261. EFI_STATUS Status;
  2262. DHCP6_SERVICE *Service;
  2263. DHCP6_INF_CB *InfCb;
  2264. UINT8 *Option;
  2265. BOOLEAN IsMatched;
  2266. Service = Instance->Service;
  2267. Status = EFI_SUCCESS;
  2268. IsMatched = FALSE;
  2269. InfCb = NULL;
  2270. if (Packet->Dhcp6.Header.MessageType != Dhcp6MsgReply) {
  2271. goto ON_EXIT;
  2272. }
  2273. //
  2274. // Check whether it's a desired Info-request message by Xid.
  2275. //
  2276. while (!IsListEmpty (&Instance->InfList)) {
  2277. InfCb = NET_LIST_HEAD (&Instance->InfList, DHCP6_INF_CB, Link);
  2278. if (InfCb->Xid == Packet->Dhcp6.Header.TransactionId) {
  2279. IsMatched = TRUE;
  2280. break;
  2281. }
  2282. }
  2283. if (!IsMatched) {
  2284. goto ON_EXIT;
  2285. }
  2286. //
  2287. // Check whether include server Id or not.
  2288. //
  2289. Option = Dhcp6SeekOption (
  2290. Packet->Dhcp6.Option,
  2291. Packet->Length - 4,
  2292. Dhcp6OptServerId
  2293. );
  2294. if (Option == NULL) {
  2295. goto ON_EXIT;
  2296. }
  2297. //
  2298. // Callback to user with the received packet and check the user's feedback.
  2299. //
  2300. Status = InfCb->ReplyCallback (
  2301. &Instance->Dhcp6,
  2302. InfCb->CallbackContext,
  2303. Packet
  2304. );
  2305. if (Status == EFI_NOT_READY) {
  2306. //
  2307. // Success or aborted will both stop this info-request exchange process,
  2308. // but not ready means user wants to continue to receive reply.
  2309. //
  2310. goto ON_EXIT;
  2311. }
  2312. //
  2313. // Dequeue the sent packet from the txlist if the xid matched, and ignore
  2314. // if no xid matched.
  2315. //
  2316. Dhcp6DequeueRetry (
  2317. Instance,
  2318. Packet->Dhcp6.Header.TransactionId,
  2319. FALSE
  2320. );
  2321. //
  2322. // For sync, set the status out of polling for info-request.
  2323. //
  2324. Instance->UdpSts = Status;
  2325. ON_EXIT:
  2326. Status = UdpIoRecvDatagram (
  2327. Service->UdpIo,
  2328. Dhcp6ReceivePacket,
  2329. Service,
  2330. 0
  2331. );
  2332. if (EFI_ERROR (Status)) {
  2333. Dhcp6CleanupRetry (Instance, DHCP6_PACKET_STATELESS);
  2334. }
  2335. }
  2336. /**
  2337. The receive callback function for Dhcp6 exchange process.
  2338. @param[in] Udp6Wrap The pointer to the received net buffer.
  2339. @param[in] EndPoint The pointer to the udp end point.
  2340. @param[in] IoStatus The return status from udp io.
  2341. @param[in] Context The opaque parameter to the function.
  2342. **/
  2343. VOID
  2344. EFIAPI
  2345. Dhcp6ReceivePacket (
  2346. IN NET_BUF *Udp6Wrap,
  2347. IN UDP_END_POINT *EndPoint,
  2348. IN EFI_STATUS IoStatus,
  2349. IN VOID *Context
  2350. )
  2351. {
  2352. EFI_DHCP6_HEADER *Head;
  2353. EFI_DHCP6_PACKET *Packet;
  2354. DHCP6_SERVICE *Service;
  2355. DHCP6_INSTANCE *Instance;
  2356. DHCP6_TX_CB *TxCb;
  2357. UINT32 Size;
  2358. BOOLEAN IsDispatched;
  2359. BOOLEAN IsStateless;
  2360. LIST_ENTRY *Entry1;
  2361. LIST_ENTRY *Next1;
  2362. LIST_ENTRY *Entry2;
  2363. LIST_ENTRY *Next2;
  2364. EFI_STATUS Status;
  2365. ASSERT (Udp6Wrap != NULL);
  2366. ASSERT (Context != NULL);
  2367. Service = (DHCP6_SERVICE *) Context;
  2368. Instance = NULL;
  2369. Packet = NULL;
  2370. IsDispatched = FALSE;
  2371. IsStateless = FALSE;
  2372. if (EFI_ERROR (IoStatus)) {
  2373. return ;
  2374. }
  2375. if (Udp6Wrap->TotalSize < sizeof (EFI_DHCP6_HEADER)) {
  2376. goto ON_CONTINUE;
  2377. }
  2378. //
  2379. // Copy the net buffer received from upd6 to a Dhcp6 packet.
  2380. //
  2381. Size = sizeof (EFI_DHCP6_PACKET) + Udp6Wrap->TotalSize;
  2382. Packet = (EFI_DHCP6_PACKET *) AllocateZeroPool (Size);
  2383. if (Packet == NULL) {
  2384. goto ON_CONTINUE;
  2385. }
  2386. Packet->Size = Size;
  2387. Head = &Packet->Dhcp6.Header;
  2388. Packet->Length = NetbufCopy (Udp6Wrap, 0, Udp6Wrap->TotalSize, (UINT8 *) Head);
  2389. if (Packet->Length == 0) {
  2390. goto ON_CONTINUE;
  2391. }
  2392. //
  2393. // Dispatch packet to right instance by transaction id.
  2394. //
  2395. NET_LIST_FOR_EACH_SAFE (Entry1, Next1, &Service->Child) {
  2396. Instance = NET_LIST_USER_STRUCT (Entry1, DHCP6_INSTANCE, Link);
  2397. NET_LIST_FOR_EACH_SAFE (Entry2, Next2, &Instance->TxList) {
  2398. TxCb = NET_LIST_USER_STRUCT (Entry2, DHCP6_TX_CB, Link);
  2399. if (Packet->Dhcp6.Header.TransactionId == TxCb->Xid) {
  2400. //
  2401. // Find the corresponding packet in tx list, and check it whether belongs
  2402. // to stateful exchange process.
  2403. //
  2404. if (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgInfoRequest) {
  2405. IsStateless = TRUE;
  2406. }
  2407. IsDispatched = TRUE;
  2408. break;
  2409. }
  2410. }
  2411. if (IsDispatched) {
  2412. break;
  2413. }
  2414. }
  2415. //
  2416. // Skip this packet if not dispatched to any instance.
  2417. //
  2418. if (!IsDispatched) {
  2419. goto ON_CONTINUE;
  2420. }
  2421. //
  2422. // Dispatch the received packet ot the right instance.
  2423. //
  2424. if (IsStateless) {
  2425. Dhcp6HandleStateless (Instance, Packet);
  2426. } else {
  2427. Dhcp6HandleStateful (Instance, Packet);
  2428. }
  2429. ON_CONTINUE:
  2430. if (!IsDispatched) {
  2431. Status = UdpIoRecvDatagram (
  2432. Service->UdpIo,
  2433. Dhcp6ReceivePacket,
  2434. Service,
  2435. 0
  2436. );
  2437. if (EFI_ERROR (Status)) {
  2438. NET_LIST_FOR_EACH_SAFE (Entry1, Next1, &Service->Child) {
  2439. Instance = NET_LIST_USER_STRUCT (Entry1, DHCP6_INSTANCE, Link);
  2440. Dhcp6CleanupRetry (Instance, DHCP6_PACKET_ALL);
  2441. }
  2442. }
  2443. }
  2444. NetbufFree (Udp6Wrap);
  2445. if (Packet != NULL) {
  2446. FreePool (Packet);
  2447. }
  2448. }
  2449. /**
  2450. Detect Link movement for specified network device.
  2451. This routine will try to invoke Snp->GetStatus() to get the media status.
  2452. If media present status switches from unpresent to present, a link movement
  2453. is detected. Note that the underlying UNDI driver may not support reporting
  2454. media status from GET_STATUS command. If that, fail to detect link movement.
  2455. @param[in] Instance The pointer to DHCP6_INSTANCE.
  2456. @retval TRUE A link movement is detected.
  2457. @retval FALSE A link movement is not detected.
  2458. **/
  2459. BOOLEAN
  2460. Dhcp6LinkMovDetect (
  2461. IN DHCP6_INSTANCE *Instance
  2462. )
  2463. {
  2464. UINT32 InterruptStatus;
  2465. BOOLEAN MediaPresent;
  2466. EFI_STATUS Status;
  2467. EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
  2468. ASSERT (Instance != NULL);
  2469. Snp = Instance->Service->Snp;
  2470. MediaPresent = Instance->MediaPresent;
  2471. //
  2472. // Check whether SNP support media detection
  2473. //
  2474. if (!Snp->Mode->MediaPresentSupported) {
  2475. return FALSE;
  2476. }
  2477. //
  2478. // Invoke Snp->GetStatus() to refresh MediaPresent field in SNP mode data
  2479. //
  2480. Status = Snp->GetStatus (Snp, &InterruptStatus, NULL);
  2481. if (EFI_ERROR (Status)) {
  2482. return FALSE;
  2483. }
  2484. Instance->MediaPresent = Snp->Mode->MediaPresent;
  2485. //
  2486. // Media transimit Unpresent to Present means new link movement is detected.
  2487. //
  2488. if (!MediaPresent && Instance->MediaPresent) {
  2489. return TRUE;
  2490. }
  2491. return FALSE;
  2492. }
  2493. /**
  2494. The timer routine of the Dhcp6 instance for each second.
  2495. @param[in] Event The timer event.
  2496. @param[in] Context The opaque parameter to the function.
  2497. **/
  2498. VOID
  2499. EFIAPI
  2500. Dhcp6OnTimerTick (
  2501. IN EFI_EVENT Event,
  2502. IN VOID *Context
  2503. )
  2504. {
  2505. LIST_ENTRY *Entry;
  2506. LIST_ENTRY *NextEntry;
  2507. DHCP6_INSTANCE *Instance;
  2508. DHCP6_TX_CB *TxCb;
  2509. DHCP6_IA_CB *IaCb;
  2510. UINT32 LossTime;
  2511. EFI_STATUS Status;
  2512. ASSERT (Context != NULL);
  2513. Instance = (DHCP6_INSTANCE *) Context;
  2514. //
  2515. // 1. Loop the tx list, count live time of every tx packet to check whether
  2516. // need re-transmit or not.
  2517. //
  2518. NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->TxList) {
  2519. TxCb = NET_LIST_USER_STRUCT (Entry, DHCP6_TX_CB, Link);
  2520. TxCb->TickTime++;
  2521. if (TxCb->TickTime > TxCb->RetryExp) {
  2522. //
  2523. // Handle the first rt in the transmission of solicit specially.
  2524. //
  2525. if ((TxCb->RetryCnt == 0 || TxCb->SolicitRetry) && TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgSolicit) {
  2526. if (Instance->AdSelect == NULL) {
  2527. //
  2528. // Set adpref as 0xff here to indicate select any advertisement
  2529. // afterwards.
  2530. //
  2531. Instance->AdPref = 0xff;
  2532. } else {
  2533. //
  2534. // Select the advertisement received before.
  2535. //
  2536. Status = Dhcp6SelectAdvertiseMsg (Instance, Instance->AdSelect);
  2537. if (Status == EFI_ABORTED) {
  2538. goto ON_CLOSE;
  2539. } else if (EFI_ERROR (Status)) {
  2540. TxCb->RetryCnt++;
  2541. }
  2542. return;
  2543. }
  2544. }
  2545. //
  2546. // Increase the retry count for the packet and add up the total loss time.
  2547. //
  2548. TxCb->RetryCnt++;
  2549. TxCb->RetryLos += TxCb->RetryExp;
  2550. //
  2551. // Check whether overflow the max retry count limit for this packet
  2552. //
  2553. if (TxCb->RetryCtl.Mrc != 0 && TxCb->RetryCtl.Mrc < TxCb->RetryCnt) {
  2554. Status = EFI_NO_RESPONSE;
  2555. goto ON_CLOSE;
  2556. }
  2557. //
  2558. // Check whether overflow the max retry duration for this packet
  2559. //
  2560. if (TxCb->RetryCtl.Mrd != 0 && TxCb->RetryCtl.Mrd <= TxCb->RetryLos) {
  2561. Status = EFI_NO_RESPONSE;
  2562. goto ON_CLOSE;
  2563. }
  2564. //
  2565. // Re-calculate retry expire timeout for the next time.
  2566. //
  2567. // Firstly, Check the new calculated time whether overflow the max retry
  2568. // expire time.
  2569. //
  2570. TxCb->RetryExp = Dhcp6CalculateExpireTime (
  2571. TxCb->RetryExp,
  2572. FALSE,
  2573. TRUE
  2574. );
  2575. if (TxCb->RetryCtl.Mrt != 0 && TxCb->RetryCtl.Mrt < TxCb->RetryExp) {
  2576. TxCb->RetryExp = Dhcp6CalculateExpireTime (
  2577. TxCb->RetryCtl.Mrt,
  2578. TRUE,
  2579. TRUE
  2580. );
  2581. }
  2582. //
  2583. // Secondly, Check the new calculated time whether overflow the max retry
  2584. // duration time.
  2585. //
  2586. LossTime = TxCb->RetryLos + TxCb->RetryExp;
  2587. if (TxCb->RetryCtl.Mrd != 0 && TxCb->RetryCtl.Mrd < LossTime) {
  2588. TxCb->RetryExp = TxCb->RetryCtl.Mrd - TxCb->RetryLos;
  2589. }
  2590. //
  2591. // Reset the tick time for the next retransmission
  2592. //
  2593. TxCb->TickTime = 0;
  2594. //
  2595. // Retransmit the last sent packet again.
  2596. //
  2597. Dhcp6TransmitPacket (Instance, TxCb->TxPacket, TxCb->Elapsed);
  2598. TxCb->SolicitRetry = FALSE;
  2599. if (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgSolicit) {
  2600. TxCb->SolicitRetry = TRUE;
  2601. }
  2602. }
  2603. }
  2604. //
  2605. // 2. Check the configured Ia, count lease time of every valid Ia to check
  2606. // whether need to renew or rebind this Ia.
  2607. //
  2608. IaCb = &Instance->IaCb;
  2609. if (Instance->Config == NULL || IaCb->Ia == NULL) {
  2610. return;
  2611. }
  2612. if (IaCb->Ia->State == Dhcp6Bound || IaCb->Ia->State == Dhcp6Renewing || IaCb->Ia->State == Dhcp6Rebinding) {
  2613. IaCb->LeaseTime++;
  2614. if (IaCb->LeaseTime > IaCb->T2 && IaCb->Ia->State == Dhcp6Bound) {
  2615. //
  2616. // Exceed t2, send rebind packet to extend the Ia lease.
  2617. //
  2618. Dhcp6SendRenewRebindMsg (Instance, TRUE);
  2619. } else if (IaCb->LeaseTime > IaCb->T1 && IaCb->Ia->State == Dhcp6Bound) {
  2620. //
  2621. // Exceed t1, send renew packet to extend the Ia lease.
  2622. //
  2623. Dhcp6SendRenewRebindMsg (Instance, FALSE);
  2624. }
  2625. }
  2626. //
  2627. // 3. In any situation when a client may have moved to a new link, the
  2628. // client MUST initiate a Confirm/Reply message exchange.
  2629. //
  2630. if (Dhcp6LinkMovDetect (Instance) && (IaCb->Ia->State == Dhcp6Bound)) {
  2631. Dhcp6SendConfirmMsg (Instance);
  2632. }
  2633. return;
  2634. ON_CLOSE:
  2635. if (Dhcp6IsValidTxCb (Instance, TxCb) &&
  2636. TxCb->TxPacket != NULL &&
  2637. (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgInfoRequest ||
  2638. TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgRenew ||
  2639. TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgConfirm)
  2640. ) {
  2641. //
  2642. // The failure of renew/Confirm will still switch to the bound state.
  2643. //
  2644. if ((TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgRenew) ||
  2645. (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgConfirm)) {
  2646. ASSERT (Instance->IaCb.Ia);
  2647. Instance->IaCb.Ia->State = Dhcp6Bound;
  2648. }
  2649. //
  2650. // The failure of info-request will return no response.
  2651. //
  2652. if (TxCb->TxPacket->Dhcp6.Header.MessageType == Dhcp6MsgInfoRequest) {
  2653. Instance->UdpSts = EFI_NO_RESPONSE;
  2654. }
  2655. Dhcp6DequeueRetry (
  2656. Instance,
  2657. TxCb->Xid,
  2658. TRUE
  2659. );
  2660. } else {
  2661. //
  2662. // The failure of the others will terminate current state machine if timeout.
  2663. //
  2664. Dhcp6CleanupSession (Instance, Status);
  2665. }
  2666. }