Udp6Impl.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  1. /** @file
  2. Udp6 driver's whole implementation.
  3. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Udp6Impl.h"
  7. UINT16 mUdp6RandomPort;
  8. /**
  9. This function checks and timeouts the I/O datagrams holding by the corresponding
  10. service context.
  11. @param[in] Event The event this function is registered to.
  12. @param[in] Context The context data registered during the creation of
  13. the Event.
  14. **/
  15. VOID
  16. EFIAPI
  17. Udp6CheckTimeout (
  18. IN EFI_EVENT Event,
  19. IN VOID *Context
  20. );
  21. /**
  22. This function finds the udp instance by the specified <Address, Port> pair.
  23. @param[in] InstanceList Pointer to the head of the list linking the udp
  24. instances.
  25. @param[in] Address Pointer to the specified IPv6 address.
  26. @param[in] Port The udp port number.
  27. @retval TRUE The specified <Address, Port> pair is found.
  28. @retval FALSE Otherwise.
  29. **/
  30. BOOLEAN
  31. Udp6FindInstanceByPort (
  32. IN LIST_ENTRY *InstanceList,
  33. IN EFI_IPv6_ADDRESS *Address,
  34. IN UINT16 Port
  35. );
  36. /**
  37. This function is the packet transmitting notify function registered to the IpIo
  38. interface. It's called to signal the udp TxToken when the IpIo layer completes
  39. transmitting of the udp datagram.
  40. If Context is NULL, then ASSERT().
  41. If NotifyData is NULL, then ASSERT().
  42. @param[in] Status The completion status of the output udp datagram.
  43. @param[in] Context Pointer to the context data.
  44. @param[in] Sender Specify a EFI_IP6_PROTOCOL for sending.
  45. @param[in] NotifyData Pointer to the notify data.
  46. **/
  47. VOID
  48. EFIAPI
  49. Udp6DgramSent (
  50. IN EFI_STATUS Status,
  51. IN VOID *Context,
  52. IN IP_IO_IP_PROTOCOL Sender,
  53. IN VOID *NotifyData
  54. );
  55. /**
  56. This function processes the received datagram passed up by the IpIo layer.
  57. If NetSession is NULL, then ASSERT().
  58. If Packet is NULL, then ASSERT().
  59. If Context is NULL, then ASSERT().
  60. @param[in] Status The status of this udp datagram.
  61. @param[in] IcmpError The IcmpError code, only available when Status is
  62. EFI_ICMP_ERROR.
  63. @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA.
  64. @param[in] Packet Pointer to the NET_BUF containing the received udp
  65. datagram.
  66. @param[in] Context Pointer to the context data.
  67. **/
  68. VOID
  69. EFIAPI
  70. Udp6DgramRcvd (
  71. IN EFI_STATUS Status,
  72. IN UINT8 IcmpError,
  73. IN EFI_NET_SESSION_DATA *NetSession,
  74. IN NET_BUF *Packet,
  75. IN VOID *Context
  76. );
  77. /**
  78. This function cancel the token specified by Arg in the Map.
  79. @param[in] Map Pointer to the NET_MAP.
  80. @param[in] Item Pointer to the NET_MAP_ITEM.
  81. @param[in] Arg Pointer to the token to be cancelled, if NULL, all
  82. the tokens in this Map will be cancelled.
  83. This parameter is optional and may be NULL.
  84. @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
  85. is not the same as that in the Item if Arg is not
  86. NULL.
  87. @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
  88. cancelled.
  89. **/
  90. EFI_STATUS
  91. EFIAPI
  92. Udp6CancelTokens (
  93. IN NET_MAP *Map,
  94. IN NET_MAP_ITEM *Item,
  95. IN VOID *Arg OPTIONAL
  96. );
  97. /**
  98. This function check if the received udp datagram matches with the Instance.
  99. @param[in] Instance Pointer to the udp instance context data.
  100. @param[in] Udp6Session Pointer to the EFI_UDP6_SESSION_DATA abstracted
  101. from the received udp datagram.
  102. @retval TRUE The udp datagram matches the receiving requirements of the Instance.
  103. @retval FALSE The udp datagram doe not match the receiving requirements of the Instance.
  104. **/
  105. BOOLEAN
  106. Udp6MatchDgram (
  107. IN UDP6_INSTANCE_DATA *Instance,
  108. IN EFI_UDP6_SESSION_DATA *Udp6Session
  109. );
  110. /**
  111. This function removes the Wrap specified by Context and releases relevant resources.
  112. @param[in] Event The Event this notify function is registered to.
  113. @param[in] Context Pointer to the context data.
  114. **/
  115. VOID
  116. EFIAPI
  117. Udp6RecycleRxDataWrap (
  118. IN EFI_EVENT Event,
  119. IN VOID *Context
  120. );
  121. /**
  122. This function wraps the Packet into RxData.
  123. @param[in] Instance Pointer to the instance context data.
  124. @param[in] Packet Pointer to the buffer containing the received
  125. datagram.
  126. @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this
  127. datagram.
  128. @return Pointer to the structure wrapping the RxData and the Packet. NULL will
  129. be returned if any error occurs.
  130. **/
  131. UDP6_RXDATA_WRAP *
  132. Udp6WrapRxData (
  133. IN UDP6_INSTANCE_DATA *Instance,
  134. IN NET_BUF *Packet,
  135. IN EFI_UDP6_RECEIVE_DATA *RxData
  136. );
  137. /**
  138. This function enqueues the received datagram into the instances' receiving queues.
  139. @param[in] Udp6Service Pointer to the udp service context data.
  140. @param[in] Packet Pointer to the buffer containing the received
  141. datagram.
  142. @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this
  143. datagram.
  144. @return The times this datagram is enqueued.
  145. **/
  146. UINTN
  147. Udp6EnqueueDgram (
  148. IN UDP6_SERVICE_DATA *Udp6Service,
  149. IN NET_BUF *Packet,
  150. IN EFI_UDP6_RECEIVE_DATA *RxData
  151. );
  152. /**
  153. This function delivers the datagrams enqueued in the instances.
  154. @param[in] Udp6Service Pointer to the udp service context data.
  155. **/
  156. VOID
  157. Udp6DeliverDgram (
  158. IN UDP6_SERVICE_DATA *Udp6Service
  159. );
  160. /**
  161. This function demultiplexes the received udp datagram to the appropriate instances.
  162. @param[in] Udp6Service Pointer to the udp service context data.
  163. @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from
  164. the received datagram.
  165. @param[in] Packet Pointer to the buffer containing the received udp
  166. datagram.
  167. **/
  168. VOID
  169. Udp6Demultiplex (
  170. IN UDP6_SERVICE_DATA *Udp6Service,
  171. IN EFI_NET_SESSION_DATA *NetSession,
  172. IN NET_BUF *Packet
  173. );
  174. /**
  175. This function handles the received Icmp Error message and demultiplexes it to the
  176. instance.
  177. @param[in] Udp6Service Pointer to the udp service context data.
  178. @param[in] IcmpError The icmp error code.
  179. @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
  180. from the received Icmp Error packet.
  181. @param[in, out] Packet Pointer to the Icmp Error packet.
  182. **/
  183. VOID
  184. Udp6IcmpHandler (
  185. IN UDP6_SERVICE_DATA *Udp6Service,
  186. IN UINT8 IcmpError,
  187. IN EFI_NET_SESSION_DATA *NetSession,
  188. IN OUT NET_BUF *Packet
  189. );
  190. /**
  191. This function builds and sends out a icmp port unreachable message.
  192. @param[in] IpIo Pointer to the IP_IO instance.
  193. @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
  194. causes this icmp error message.
  195. @param[in] Udp6Header Pointer to the udp header of the datagram causes
  196. this icmp error message.
  197. **/
  198. VOID
  199. Udp6SendPortUnreach (
  200. IN IP_IO *IpIo,
  201. IN EFI_NET_SESSION_DATA *NetSession,
  202. IN VOID *Udp6Header
  203. );
  204. /**
  205. Find the key in the netmap
  206. @param[in] Map The netmap to search within.
  207. @param[in] Key The key to search.
  208. @return The point to the item contains the Key, or NULL if Key isn't in the map.
  209. **/
  210. NET_MAP_ITEM *
  211. Udp6MapMultiCastAddr (
  212. IN NET_MAP *Map,
  213. IN VOID *Key
  214. );
  215. /**
  216. Create the Udp service context data.
  217. @param[in] Udp6Service Pointer to the UDP6_SERVICE_DATA.
  218. @param[in] ImageHandle The image handle of this udp6 driver.
  219. @param[in] ControllerHandle The controller handle this udp6 driver binds on.
  220. @retval EFI_SUCCESS The udp6 service context data was created and
  221. initialized.
  222. @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
  223. @retval Others An error condition occurred.
  224. **/
  225. EFI_STATUS
  226. Udp6CreateService (
  227. IN UDP6_SERVICE_DATA *Udp6Service,
  228. IN EFI_HANDLE ImageHandle,
  229. IN EFI_HANDLE ControllerHandle
  230. )
  231. {
  232. EFI_STATUS Status;
  233. IP_IO_OPEN_DATA OpenData;
  234. ZeroMem (Udp6Service, sizeof (UDP6_SERVICE_DATA));
  235. Udp6Service->Signature = UDP6_SERVICE_DATA_SIGNATURE;
  236. Udp6Service->ServiceBinding = mUdp6ServiceBinding;
  237. Udp6Service->ImageHandle = ImageHandle;
  238. Udp6Service->ControllerHandle = ControllerHandle;
  239. Udp6Service->ChildrenNumber = 0;
  240. InitializeListHead (&Udp6Service->ChildrenList);
  241. //
  242. // Create the IpIo for this service context.
  243. //
  244. Udp6Service->IpIo = IpIoCreate (ImageHandle, ControllerHandle, IP_VERSION_6);
  245. if (Udp6Service->IpIo == NULL) {
  246. return EFI_OUT_OF_RESOURCES;
  247. }
  248. //
  249. // Set the OpenData used to open the IpIo.
  250. //
  251. CopyMem (
  252. &OpenData.IpConfigData.Ip6CfgData,
  253. &mIp6IoDefaultIpConfigData,
  254. sizeof (EFI_IP6_CONFIG_DATA)
  255. );
  256. OpenData.RcvdContext = (VOID *)Udp6Service;
  257. OpenData.SndContext = NULL;
  258. OpenData.PktRcvdNotify = Udp6DgramRcvd;
  259. OpenData.PktSentNotify = Udp6DgramSent;
  260. //
  261. // Configure and start the IpIo.
  262. //
  263. Status = IpIoOpen (Udp6Service->IpIo, &OpenData);
  264. if (EFI_ERROR (Status)) {
  265. goto ON_ERROR;
  266. }
  267. //
  268. // Create the event for Udp timeout checking.
  269. //
  270. Status = gBS->CreateEvent (
  271. EVT_TIMER | EVT_NOTIFY_SIGNAL,
  272. TPL_CALLBACK,
  273. Udp6CheckTimeout,
  274. Udp6Service,
  275. &Udp6Service->TimeoutEvent
  276. );
  277. if (EFI_ERROR (Status)) {
  278. goto ON_ERROR;
  279. }
  280. //
  281. // Start the timeout timer event.
  282. //
  283. Status = gBS->SetTimer (
  284. Udp6Service->TimeoutEvent,
  285. TimerPeriodic,
  286. UDP6_TIMEOUT_INTERVAL
  287. );
  288. if (EFI_ERROR (Status)) {
  289. goto ON_ERROR;
  290. }
  291. return EFI_SUCCESS;
  292. ON_ERROR:
  293. if (Udp6Service->TimeoutEvent != NULL) {
  294. gBS->CloseEvent (Udp6Service->TimeoutEvent);
  295. }
  296. IpIoDestroy (Udp6Service->IpIo);
  297. Udp6Service->IpIo = NULL;
  298. return Status;
  299. }
  300. /**
  301. Clean the Udp service context data.
  302. @param[in, out] Udp6Service Pointer to the UDP6_SERVICE_DATA.
  303. **/
  304. VOID
  305. Udp6CleanService (
  306. IN OUT UDP6_SERVICE_DATA *Udp6Service
  307. )
  308. {
  309. //
  310. // Close the TimeoutEvent timer.
  311. //
  312. gBS->CloseEvent (Udp6Service->TimeoutEvent);
  313. //
  314. // Destroy the IpIo.
  315. //
  316. IpIoDestroy (Udp6Service->IpIo);
  317. Udp6Service->IpIo = NULL;
  318. ZeroMem (Udp6Service, sizeof (UDP6_SERVICE_DATA));
  319. }
  320. /**
  321. This function checks and times out the I/O datagrams listed in the
  322. UDP6_SERVICE_DATA which is specified by the input parameter Context.
  323. @param[in] Event The event this function registered to.
  324. @param[in] Context The context data registered during the creation of
  325. the Event.
  326. **/
  327. VOID
  328. EFIAPI
  329. Udp6CheckTimeout (
  330. IN EFI_EVENT Event,
  331. IN VOID *Context
  332. )
  333. {
  334. UDP6_SERVICE_DATA *Udp6Service;
  335. LIST_ENTRY *Entry;
  336. UDP6_INSTANCE_DATA *Instance;
  337. LIST_ENTRY *WrapEntry;
  338. LIST_ENTRY *NextEntry;
  339. UDP6_RXDATA_WRAP *Wrap;
  340. Udp6Service = (UDP6_SERVICE_DATA *)Context;
  341. NET_CHECK_SIGNATURE (Udp6Service, UDP6_SERVICE_DATA_SIGNATURE);
  342. NET_LIST_FOR_EACH (Entry, &Udp6Service->ChildrenList) {
  343. //
  344. // Iterate all the instances belonging to this service context.
  345. //
  346. Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link);
  347. NET_CHECK_SIGNATURE (Instance, UDP6_INSTANCE_DATA_SIGNATURE);
  348. if (!Instance->Configured || (Instance->ConfigData.ReceiveTimeout == 0)) {
  349. //
  350. // Skip this instance if it's not configured or no receive timeout.
  351. //
  352. continue;
  353. }
  354. NET_LIST_FOR_EACH_SAFE (WrapEntry, NextEntry, &Instance->RcvdDgramQue) {
  355. //
  356. // Iterate all the rxdatas belonging to this udp instance.
  357. //
  358. Wrap = NET_LIST_USER_STRUCT (WrapEntry, UDP6_RXDATA_WRAP, Link);
  359. if (Wrap->TimeoutTick < UDP6_TIMEOUT_INTERVAL / 10) {
  360. //
  361. // Remove this RxData if it timeouts.
  362. //
  363. Udp6RecycleRxDataWrap (NULL, (VOID *)Wrap);
  364. } else {
  365. Wrap->TimeoutTick -= UDP6_TIMEOUT_INTERVAL / 10;
  366. }
  367. }
  368. }
  369. }
  370. /**
  371. This function initializes the new created udp instance.
  372. @param[in] Udp6Service Pointer to the UDP6_SERVICE_DATA.
  373. @param[in, out] Instance Pointer to the un-initialized UDP6_INSTANCE_DATA.
  374. **/
  375. VOID
  376. Udp6InitInstance (
  377. IN UDP6_SERVICE_DATA *Udp6Service,
  378. IN OUT UDP6_INSTANCE_DATA *Instance
  379. )
  380. {
  381. //
  382. // Set the signature.
  383. //
  384. Instance->Signature = UDP6_INSTANCE_DATA_SIGNATURE;
  385. //
  386. // Init the lists.
  387. //
  388. InitializeListHead (&Instance->Link);
  389. InitializeListHead (&Instance->RcvdDgramQue);
  390. InitializeListHead (&Instance->DeliveredDgramQue);
  391. //
  392. // Init the NET_MAPs.
  393. //
  394. NetMapInit (&Instance->TxTokens);
  395. NetMapInit (&Instance->RxTokens);
  396. NetMapInit (&Instance->McastIps);
  397. //
  398. // Save the pointer to the UDP6_SERVICE_DATA, and initialize other members.
  399. //
  400. Instance->Udp6Service = Udp6Service;
  401. CopyMem (&Instance->Udp6Proto, &mUdp6Protocol, sizeof (EFI_UDP6_PROTOCOL));
  402. Instance->IcmpError = EFI_SUCCESS;
  403. Instance->Configured = FALSE;
  404. Instance->IsNoMapping = FALSE;
  405. Instance->InDestroy = FALSE;
  406. }
  407. /**
  408. This function cleans the udp instance.
  409. @param[in, out] Instance Pointer to the UDP6_INSTANCE_DATA to clean.
  410. **/
  411. VOID
  412. Udp6CleanInstance (
  413. IN OUT UDP6_INSTANCE_DATA *Instance
  414. )
  415. {
  416. NetMapClean (&Instance->McastIps);
  417. NetMapClean (&Instance->RxTokens);
  418. NetMapClean (&Instance->TxTokens);
  419. }
  420. /**
  421. This function finds the udp instance by the specified <Address, Port> pair.
  422. @param[in] InstanceList Pointer to the head of the list linking the udp
  423. instances.
  424. @param[in] Address Pointer to the specified IPv6 address.
  425. @param[in] Port The udp port number.
  426. @retval TRUE The specified <Address, Port> pair is found.
  427. @retval FALSE Otherwise.
  428. **/
  429. BOOLEAN
  430. Udp6FindInstanceByPort (
  431. IN LIST_ENTRY *InstanceList,
  432. IN EFI_IPv6_ADDRESS *Address,
  433. IN UINT16 Port
  434. )
  435. {
  436. LIST_ENTRY *Entry;
  437. UDP6_INSTANCE_DATA *Instance;
  438. EFI_UDP6_CONFIG_DATA *ConfigData;
  439. NET_LIST_FOR_EACH (Entry, InstanceList) {
  440. //
  441. // Iterate all the udp instances.
  442. //
  443. Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link);
  444. ConfigData = &Instance->ConfigData;
  445. if (!Instance->Configured || ConfigData->AcceptAnyPort) {
  446. //
  447. // If the instance is not configured, or the configdata of the instance indicates
  448. // this instance accepts any port, skip it.
  449. //
  450. continue;
  451. }
  452. if (EFI_IP6_EQUAL (&ConfigData->StationAddress, Address) &&
  453. (ConfigData->StationPort == Port)
  454. )
  455. {
  456. //
  457. // If both the address and the port are the same, return TRUE.
  458. //
  459. return TRUE;
  460. }
  461. }
  462. //
  463. // Return FALSE when matching fails.
  464. //
  465. return FALSE;
  466. }
  467. /**
  468. This function tries to bind the udp instance according to the configured port
  469. allocation strategy.
  470. @param[in] InstanceList Pointer to the head of the list linking the udp
  471. instances.
  472. @param[in] ConfigData Pointer to the ConfigData of the instance to be
  473. bound.
  474. @retval EFI_SUCCESS The bound operation completed successfully.
  475. @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is
  476. already used by other instance.
  477. @retval EFI_OUT_OF_RESOURCES No available port resources.
  478. **/
  479. EFI_STATUS
  480. Udp6Bind (
  481. IN LIST_ENTRY *InstanceList,
  482. IN EFI_UDP6_CONFIG_DATA *ConfigData
  483. )
  484. {
  485. EFI_IPv6_ADDRESS *StationAddress;
  486. UINT16 StartPort;
  487. if (ConfigData->AcceptAnyPort) {
  488. return EFI_SUCCESS;
  489. }
  490. StationAddress = &ConfigData->StationAddress;
  491. if (ConfigData->StationPort != 0) {
  492. if (!ConfigData->AllowDuplicatePort &&
  493. Udp6FindInstanceByPort (InstanceList, StationAddress, ConfigData->StationPort)
  494. )
  495. {
  496. //
  497. // Do not allow duplicate ports and the port is already used by other instance.
  498. //
  499. return EFI_ACCESS_DENIED;
  500. }
  501. } else {
  502. //
  503. // Select a random port for this instance.
  504. //
  505. if (ConfigData->AllowDuplicatePort) {
  506. //
  507. // Just pick up the random port if the instance allows duplicate port.
  508. //
  509. ConfigData->StationPort = mUdp6RandomPort;
  510. } else {
  511. StartPort = mUdp6RandomPort;
  512. while (Udp6FindInstanceByPort (InstanceList, StationAddress, mUdp6RandomPort)) {
  513. mUdp6RandomPort++;
  514. if (mUdp6RandomPort == 0) {
  515. mUdp6RandomPort = UDP6_PORT_KNOWN;
  516. }
  517. if (mUdp6RandomPort == StartPort) {
  518. //
  519. // No available port.
  520. //
  521. return EFI_OUT_OF_RESOURCES;
  522. }
  523. }
  524. ConfigData->StationPort = mUdp6RandomPort;
  525. }
  526. mUdp6RandomPort++;
  527. if (mUdp6RandomPort == 0) {
  528. mUdp6RandomPort = UDP6_PORT_KNOWN;
  529. }
  530. }
  531. return EFI_SUCCESS;
  532. }
  533. /**
  534. This function is used to check whether the NewConfigData has any un-reconfigurable
  535. parameters changed compared to the OldConfigData.
  536. @param[in] OldConfigData Pointer to the current ConfigData the udp instance
  537. uses.
  538. @param[in] NewConfigData Pointer to the new ConfigData.
  539. @retval TRUE The instance is reconfigurable according to the NewConfigData.
  540. @retval FALSE Otherwise.
  541. **/
  542. BOOLEAN
  543. Udp6IsReconfigurable (
  544. IN EFI_UDP6_CONFIG_DATA *OldConfigData,
  545. IN EFI_UDP6_CONFIG_DATA *NewConfigData
  546. )
  547. {
  548. if ((NewConfigData->AcceptAnyPort != OldConfigData->AcceptAnyPort) ||
  549. (NewConfigData->AcceptPromiscuous != OldConfigData->AcceptPromiscuous) ||
  550. (NewConfigData->AllowDuplicatePort != OldConfigData->AllowDuplicatePort)
  551. )
  552. {
  553. //
  554. // The receiving filter parameters cannot be changed.
  555. //
  556. return FALSE;
  557. }
  558. if ((!NewConfigData->AcceptAnyPort) &&
  559. (NewConfigData->StationPort != OldConfigData->StationPort)
  560. )
  561. {
  562. //
  563. // The port is not changeable.
  564. //
  565. return FALSE;
  566. }
  567. if (!EFI_IP6_EQUAL (&NewConfigData->StationAddress, &OldConfigData->StationAddress)) {
  568. //
  569. // The StationAddress is not the same.
  570. //
  571. return FALSE;
  572. }
  573. if (!EFI_IP6_EQUAL (&NewConfigData->RemoteAddress, &OldConfigData->RemoteAddress)) {
  574. //
  575. // The remoteaddress is not the same.
  576. //
  577. return FALSE;
  578. }
  579. if (!NetIp6IsUnspecifiedAddr (&NewConfigData->RemoteAddress) &&
  580. (NewConfigData->RemotePort != OldConfigData->RemotePort)
  581. )
  582. {
  583. //
  584. // The RemotePort differs if it's designated in the configdata.
  585. //
  586. return FALSE;
  587. }
  588. //
  589. // All checks pass, return TRUE.
  590. //
  591. return TRUE;
  592. }
  593. /**
  594. This function builds the Ip6 configdata from the Udp6ConfigData.
  595. @param[in] Udp6ConfigData Pointer to the EFI_UDP6_CONFIG_DATA.
  596. @param[in, out] Ip6ConfigData Pointer to the EFI_IP6_CONFIG_DATA.
  597. **/
  598. VOID
  599. Udp6BuildIp6ConfigData (
  600. IN EFI_UDP6_CONFIG_DATA *Udp6ConfigData,
  601. IN OUT EFI_IP6_CONFIG_DATA *Ip6ConfigData
  602. )
  603. {
  604. CopyMem (
  605. Ip6ConfigData,
  606. &mIp6IoDefaultIpConfigData,
  607. sizeof (EFI_IP6_CONFIG_DATA)
  608. );
  609. Ip6ConfigData->DefaultProtocol = EFI_IP_PROTO_UDP;
  610. Ip6ConfigData->AcceptPromiscuous = Udp6ConfigData->AcceptPromiscuous;
  611. IP6_COPY_ADDRESS (&Ip6ConfigData->StationAddress, &Udp6ConfigData->StationAddress);
  612. IP6_COPY_ADDRESS (&Ip6ConfigData->DestinationAddress, &Udp6ConfigData->RemoteAddress);
  613. //
  614. // Use the -1 magic number to disable the receiving process of the ip instance.
  615. //
  616. Ip6ConfigData->ReceiveTimeout = (UINT32)(-1);
  617. }
  618. /**
  619. This function validates the TxToken. It returns the error code according to the spec.
  620. @param[in] Instance Pointer to the udp instance context data.
  621. @param[in] TxToken Pointer to the token to be checked.
  622. @retval EFI_SUCCESS The TxToken is valid.
  623. @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
  624. Token.Event is NULL;
  625. Token.Packet.TxData is NULL;
  626. Token.Packet.TxData.FragmentCount is zero;
  627. Token.Packet.TxData.DataLength is not equal to the
  628. sum of fragment lengths;
  629. One or more of the
  630. Token.Packet.TxData.FragmentTable[].FragmentLength
  631. fields is zero;
  632. One or more of the
  633. Token.Packet.TxData.FragmentTable[].FragmentBuffer
  634. fields is NULL;
  635. UdpSessionData.DestinationAddress are not valid
  636. unicast IPv6 addresses if the UdpSessionData is
  637. not NULL;
  638. UdpSessionData.DestinationPort and
  639. ConfigData.RemotePort are all zero if the
  640. UdpSessionData is not NULL.
  641. @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
  642. packet size.
  643. **/
  644. EFI_STATUS
  645. Udp6ValidateTxToken (
  646. IN UDP6_INSTANCE_DATA *Instance,
  647. IN EFI_UDP6_COMPLETION_TOKEN *TxToken
  648. )
  649. {
  650. EFI_UDP6_TRANSMIT_DATA *TxData;
  651. UINT32 Index;
  652. UINT32 TotalLen;
  653. EFI_UDP6_CONFIG_DATA *ConfigData;
  654. EFI_UDP6_SESSION_DATA *UdpSessionData;
  655. if (TxToken->Event == NULL) {
  656. return EFI_INVALID_PARAMETER;
  657. }
  658. TxData = TxToken->Packet.TxData;
  659. if ((TxData == NULL) || (TxData->FragmentCount == 0)) {
  660. return EFI_INVALID_PARAMETER;
  661. }
  662. TotalLen = 0;
  663. for (Index = 0; Index < TxData->FragmentCount; Index++) {
  664. if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) ||
  665. (TxData->FragmentTable[Index].FragmentLength == 0)
  666. )
  667. {
  668. //
  669. // If the FragmentBuffer is NULL, or the FragmentLeng is zero.
  670. //
  671. return EFI_INVALID_PARAMETER;
  672. }
  673. TotalLen += TxData->FragmentTable[Index].FragmentLength;
  674. }
  675. if (TotalLen != TxData->DataLength) {
  676. //
  677. // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the
  678. // DataLength.
  679. //
  680. return EFI_INVALID_PARAMETER;
  681. }
  682. ConfigData = &Instance->ConfigData;
  683. UdpSessionData = TxData->UdpSessionData;
  684. if (UdpSessionData != NULL) {
  685. if ((UdpSessionData->DestinationPort == 0) && (ConfigData->RemotePort == 0)) {
  686. //
  687. // Ambiguous; no available DestinationPort for this token.
  688. //
  689. return EFI_INVALID_PARAMETER;
  690. }
  691. if (NetIp6IsUnspecifiedAddr (&UdpSessionData->DestinationAddress) &&
  692. NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress)
  693. )
  694. {
  695. //
  696. // The DestinationAddress is not specified.
  697. //
  698. return EFI_INVALID_PARAMETER;
  699. }
  700. if (!NetIp6IsUnspecifiedAddr (&UdpSessionData->DestinationAddress) &&
  701. !NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress)
  702. )
  703. {
  704. //
  705. // The ConfigData.RemoteAddress is not zero and the UdpSessionData.DestinationAddress
  706. // is not zero too.
  707. //
  708. return EFI_INVALID_PARAMETER;
  709. }
  710. } else if (NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress)) {
  711. //
  712. // The configured RemoteAddress is all zero, and the user doesn't override the
  713. // destination address.
  714. //
  715. return EFI_INVALID_PARAMETER;
  716. }
  717. if (TxData->DataLength > UDP6_MAX_DATA_SIZE) {
  718. return EFI_BAD_BUFFER_SIZE;
  719. }
  720. return EFI_SUCCESS;
  721. }
  722. /**
  723. This function checks whether the specified Token duplicates the one in the Map.
  724. @param[in] Map Pointer to the NET_MAP.
  725. @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to
  726. the Token.
  727. @param[in] Context Pointer to the Token to be checked.
  728. @retval EFI_SUCCESS The Token specified by Context differs from the
  729. one in the Item.
  730. @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
  731. **/
  732. EFI_STATUS
  733. EFIAPI
  734. Udp6TokenExist (
  735. IN NET_MAP *Map,
  736. IN NET_MAP_ITEM *Item,
  737. IN VOID *Context
  738. )
  739. {
  740. EFI_UDP6_COMPLETION_TOKEN *Token;
  741. EFI_UDP6_COMPLETION_TOKEN *TokenInItem;
  742. Token = (EFI_UDP6_COMPLETION_TOKEN *)Context;
  743. TokenInItem = (EFI_UDP6_COMPLETION_TOKEN *)Item->Key;
  744. if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {
  745. //
  746. // The Token duplicates with the TokenInItem in case either the two pointers are the
  747. // same, or the Events of these two tokens are the same.
  748. //
  749. return EFI_ACCESS_DENIED;
  750. }
  751. return EFI_SUCCESS;
  752. }
  753. /**
  754. This function calculates the checksum for the Packet, utilizing the pre-calculated
  755. pseudo HeadSum to reduce some overhead.
  756. @param[in] Packet Pointer to the NET_BUF contains the udp datagram.
  757. @param[in] HeadSum Checksum of the pseudo header, except the length
  758. field.
  759. @return The 16-bit checksum of this udp datagram.
  760. **/
  761. UINT16
  762. Udp6Checksum (
  763. IN NET_BUF *Packet,
  764. IN UINT16 HeadSum
  765. )
  766. {
  767. UINT16 Checksum;
  768. Checksum = NetbufChecksum (Packet);
  769. Checksum = NetAddChecksum (Checksum, HeadSum);
  770. Checksum = NetAddChecksum (Checksum, HTONS ((UINT16)Packet->TotalSize));
  771. Checksum = (UINT16)(~Checksum);
  772. return Checksum;
  773. }
  774. /**
  775. This function removes the specified Token from the TokenMap.
  776. @param[in] TokenMap Pointer to the NET_MAP containing the tokens.
  777. @param[in] Token Pointer to the Token to be removed.
  778. @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
  779. @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
  780. **/
  781. EFI_STATUS
  782. Udp6RemoveToken (
  783. IN NET_MAP *TokenMap,
  784. IN EFI_UDP6_COMPLETION_TOKEN *Token
  785. )
  786. {
  787. NET_MAP_ITEM *Item;
  788. //
  789. // Find the Token first.
  790. //
  791. Item = NetMapFindKey (TokenMap, (VOID *)Token);
  792. if (Item != NULL) {
  793. //
  794. // Remove the token if it's found in the map.
  795. //
  796. NetMapRemoveItem (TokenMap, Item, NULL);
  797. return EFI_SUCCESS;
  798. }
  799. return EFI_NOT_FOUND;
  800. }
  801. /**
  802. This function is the packet transmitting notify function registered to the IpIo
  803. interface. It's called to signal the udp TxToken when IpIo layer completes the
  804. transmitting of the udp datagram.
  805. If Context is NULL, then ASSERT().
  806. If NotifyData is NULL, then ASSERT().
  807. @param[in] Status The completion status of the output udp datagram.
  808. @param[in] Context Pointer to the context data.
  809. @param[in] Sender Specify a EFI_IP6_PROTOCOL for sending.
  810. @param[in] NotifyData Pointer to the notify data.
  811. **/
  812. VOID
  813. EFIAPI
  814. Udp6DgramSent (
  815. IN EFI_STATUS Status,
  816. IN VOID *Context,
  817. IN IP_IO_IP_PROTOCOL Sender,
  818. IN VOID *NotifyData
  819. )
  820. {
  821. UDP6_INSTANCE_DATA *Instance;
  822. EFI_UDP6_COMPLETION_TOKEN *Token;
  823. ASSERT (Context != NULL && NotifyData != NULL);
  824. Instance = (UDP6_INSTANCE_DATA *)Context;
  825. Token = (EFI_UDP6_COMPLETION_TOKEN *)NotifyData;
  826. if (Udp6RemoveToken (&Instance->TxTokens, Token) == EFI_SUCCESS) {
  827. //
  828. // The token may be cancelled. Only signal it if the remove operation succeeds.
  829. //
  830. Token->Status = Status;
  831. gBS->SignalEvent (Token->Event);
  832. DispatchDpc ();
  833. }
  834. }
  835. /**
  836. This function processes the received datagram passed up by the IpIo layer.
  837. If NetSession is NULL, then ASSERT().
  838. If Packet is NULL, then ASSERT().
  839. If Context is NULL, then ASSERT().
  840. @param[in] Status The status of this udp datagram.
  841. @param[in] IcmpError The IcmpError code, only available when Status is
  842. EFI_ICMP_ERROR.
  843. @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA.
  844. @param[in] Packet Pointer to the NET_BUF containing the received udp
  845. datagram.
  846. @param[in] Context Pointer to the context data.
  847. **/
  848. VOID
  849. EFIAPI
  850. Udp6DgramRcvd (
  851. IN EFI_STATUS Status,
  852. IN UINT8 IcmpError,
  853. IN EFI_NET_SESSION_DATA *NetSession,
  854. IN NET_BUF *Packet,
  855. IN VOID *Context
  856. )
  857. {
  858. ASSERT (NetSession != NULL && Packet != NULL && Context != NULL);
  859. NET_CHECK_SIGNATURE (Packet, NET_BUF_SIGNATURE);
  860. //
  861. // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.
  862. //
  863. if (Status == EFI_SUCCESS) {
  864. //
  865. // Demultiplex the received datagram.
  866. //
  867. Udp6Demultiplex ((UDP6_SERVICE_DATA *)Context, NetSession, Packet);
  868. } else {
  869. //
  870. // Handle the ICMP6 Error packet.
  871. //
  872. Udp6IcmpHandler ((UDP6_SERVICE_DATA *)Context, IcmpError, NetSession, Packet);
  873. }
  874. //
  875. // Dispatch the DPC queued by the NotifyFunction of the rx token's events
  876. // that are signaled with received data.
  877. //
  878. DispatchDpc ();
  879. }
  880. /**
  881. This function removes the multicast group specified by Arg from the Map.
  882. @param[in] Map Pointer to the NET_MAP.
  883. @param[in] Item Pointer to the NET_MAP_ITEM.
  884. @param[in] Arg Pointer to the Arg, it's the pointer to a
  885. multicast IPv6 Address. This parameter is
  886. optional and may be NULL.
  887. @retval EFI_SUCCESS The multicast address is removed.
  888. @retval EFI_ABORTED The specified multicast address is removed, and the
  889. Arg is not NULL.
  890. **/
  891. EFI_STATUS
  892. EFIAPI
  893. Udp6LeaveGroup (
  894. IN NET_MAP *Map,
  895. IN NET_MAP_ITEM *Item,
  896. IN VOID *Arg OPTIONAL
  897. )
  898. {
  899. EFI_IPv6_ADDRESS *McastIp;
  900. McastIp = Arg;
  901. if ((McastIp != NULL) &&
  902. !EFI_IP6_EQUAL (McastIp, ((EFI_IPv6_ADDRESS *)Item->Key))
  903. )
  904. {
  905. //
  906. // McastIp is not NULL and the multicast address contained in the Item
  907. // is not the same as McastIp.
  908. //
  909. return EFI_SUCCESS;
  910. }
  911. FreePool (Item->Key);
  912. //
  913. // Remove this Item.
  914. //
  915. NetMapRemoveItem (Map, Item, NULL);
  916. if (McastIp != NULL) {
  917. //
  918. // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration.
  919. //
  920. return EFI_ABORTED;
  921. }
  922. return EFI_SUCCESS;
  923. }
  924. /**
  925. This function cancel the token specified by Arg in the Map.
  926. @param[in] Map Pointer to the NET_MAP.
  927. @param[in] Item Pointer to the NET_MAP_ITEM.
  928. @param[in] Arg Pointer to the token to be cancelled. If NULL, all
  929. the tokens in this Map will be cancelled.
  930. This parameter is optional and may be NULL.
  931. @retval EFI_SUCCESS The token is cancelled if Arg is NULL, or the token
  932. is not the same as that in the Item, if Arg is not
  933. NULL.
  934. @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
  935. cancelled.
  936. **/
  937. EFI_STATUS
  938. EFIAPI
  939. Udp6CancelTokens (
  940. IN NET_MAP *Map,
  941. IN NET_MAP_ITEM *Item,
  942. IN VOID *Arg OPTIONAL
  943. )
  944. {
  945. EFI_UDP6_COMPLETION_TOKEN *TokenToCancel;
  946. NET_BUF *Packet;
  947. IP_IO *IpIo;
  948. if ((Arg != NULL) && (Item->Key != Arg)) {
  949. return EFI_SUCCESS;
  950. }
  951. if (Item->Value != NULL) {
  952. //
  953. // If the token is a transmit token, the corresponding Packet is recorded in
  954. // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken
  955. // will invoke Udp6DgramSent, the token will be signaled and this Item will
  956. // be removed from the Map there.
  957. //
  958. Packet = (NET_BUF *)(Item->Value);
  959. IpIo = (IP_IO *)(*((UINTN *)&Packet->ProtoData[0]));
  960. IpIoCancelTxToken (IpIo, Packet);
  961. } else {
  962. //
  963. // The token is a receive token. Abort it and remove it from the Map.
  964. //
  965. TokenToCancel = (EFI_UDP6_COMPLETION_TOKEN *)Item->Key;
  966. NetMapRemoveItem (Map, Item, NULL);
  967. TokenToCancel->Status = EFI_ABORTED;
  968. gBS->SignalEvent (TokenToCancel->Event);
  969. }
  970. if (Arg != NULL) {
  971. return EFI_ABORTED;
  972. }
  973. return EFI_SUCCESS;
  974. }
  975. /**
  976. This function removes all the Wrap datas in the RcvdDgramQue.
  977. @param[in] Instance Pointer to the Udp6 Instance.
  978. **/
  979. VOID
  980. Udp6FlushRcvdDgram (
  981. IN UDP6_INSTANCE_DATA *Instance
  982. )
  983. {
  984. UDP6_RXDATA_WRAP *Wrap;
  985. while (!IsListEmpty (&Instance->RcvdDgramQue)) {
  986. //
  987. // Iterate all the Wraps in the RcvdDgramQue.
  988. //
  989. Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP6_RXDATA_WRAP, Link);
  990. //
  991. // The Wrap will be removed from the RcvdDgramQue by this function call.
  992. //
  993. Udp6RecycleRxDataWrap (NULL, (VOID *)Wrap);
  994. }
  995. }
  996. /**
  997. Cancel Udp6 tokens from the Udp6 instance.
  998. @param[in] Instance Pointer to the udp instance context data.
  999. @param[in] Token Pointer to the token to be canceled. If NULL, all
  1000. tokens in this instance will be cancelled.
  1001. This parameter is optional and may be NULL.
  1002. @retval EFI_SUCCESS The Token is cancelled.
  1003. @retval EFI_NOT_FOUND The Token is not found.
  1004. **/
  1005. EFI_STATUS
  1006. Udp6InstanceCancelToken (
  1007. IN UDP6_INSTANCE_DATA *Instance,
  1008. IN EFI_UDP6_COMPLETION_TOKEN *Token OPTIONAL
  1009. )
  1010. {
  1011. EFI_STATUS Status;
  1012. //
  1013. // Cancel this token from the TxTokens map.
  1014. //
  1015. Status = NetMapIterate (&Instance->TxTokens, Udp6CancelTokens, Token);
  1016. if ((Token != NULL) && (Status == EFI_ABORTED)) {
  1017. //
  1018. // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
  1019. // the TxTokens and returns success.
  1020. //
  1021. return EFI_SUCCESS;
  1022. }
  1023. //
  1024. // Try to cancel this token from the RxTokens map in condition either the Token
  1025. // is NULL or the specified Token is not in TxTokens.
  1026. //
  1027. Status = NetMapIterate (&Instance->RxTokens, Udp6CancelTokens, Token);
  1028. if ((Token != NULL) && (Status == EFI_SUCCESS)) {
  1029. //
  1030. // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the
  1031. // TxTokens nor the RxTokens, or say, it's not found.
  1032. //
  1033. return EFI_NOT_FOUND;
  1034. }
  1035. ASSERT (
  1036. (Token != NULL) ||
  1037. ((0 == NetMapGetCount (&Instance->TxTokens)) &&
  1038. (0 == NetMapGetCount (&Instance->RxTokens)))
  1039. );
  1040. return EFI_SUCCESS;
  1041. }
  1042. /**
  1043. This function checks if the received udp datagram matches with the Instance.
  1044. @param[in] Instance Pointer to the udp instance context data.
  1045. @param[in] Udp6Session Pointer to the EFI_UDP6_SESSION_DATA abstracted
  1046. from the received udp datagram.
  1047. @retval TRUE The udp datagram matches the receiving requirements of the Instance.
  1048. @retval FALSE The udp datagram does not match the receiving requirements of the Instance.
  1049. **/
  1050. BOOLEAN
  1051. Udp6MatchDgram (
  1052. IN UDP6_INSTANCE_DATA *Instance,
  1053. IN EFI_UDP6_SESSION_DATA *Udp6Session
  1054. )
  1055. {
  1056. EFI_UDP6_CONFIG_DATA *ConfigData;
  1057. EFI_IPv6_ADDRESS Destination;
  1058. ConfigData = &Instance->ConfigData;
  1059. if (ConfigData->AcceptPromiscuous) {
  1060. //
  1061. // Always matches if this instance is in the promiscuous state.
  1062. //
  1063. return TRUE;
  1064. }
  1065. if ((!ConfigData->AcceptAnyPort && (Udp6Session->DestinationPort != ConfigData->StationPort)) ||
  1066. ((ConfigData->RemotePort != 0) && (Udp6Session->SourcePort != ConfigData->RemotePort))
  1067. )
  1068. {
  1069. //
  1070. // The local port or the remote port doesn't match.
  1071. //
  1072. return FALSE;
  1073. }
  1074. if (!NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress) &&
  1075. !EFI_IP6_EQUAL (&ConfigData->RemoteAddress, &Udp6Session->SourceAddress)
  1076. )
  1077. {
  1078. //
  1079. // This datagram doesn't come from the instance's specified sender.
  1080. //
  1081. return FALSE;
  1082. }
  1083. if (NetIp6IsUnspecifiedAddr (&ConfigData->StationAddress) ||
  1084. EFI_IP6_EQUAL (&Udp6Session->DestinationAddress, &ConfigData->StationAddress)
  1085. )
  1086. {
  1087. //
  1088. // The instance is configured to receive datagrams destinated to any station IP or
  1089. // the destination address of this datagram matches the configured station IP.
  1090. //
  1091. return TRUE;
  1092. }
  1093. IP6_COPY_ADDRESS (&Destination, &Udp6Session->DestinationAddress);
  1094. if (IP6_IS_MULTICAST (&Destination) &&
  1095. (NULL != Udp6MapMultiCastAddr (&Instance->McastIps, &Destination))
  1096. )
  1097. {
  1098. //
  1099. // It's a multicast packet and the multicast address is accepted by this instance.
  1100. //
  1101. return TRUE;
  1102. }
  1103. return FALSE;
  1104. }
  1105. /**
  1106. This function removes the Wrap specified by Context and release relevant resources.
  1107. @param[in] Event The Event this notify function registered to.
  1108. @param[in] Context Pointer to the context data.
  1109. **/
  1110. VOID
  1111. EFIAPI
  1112. Udp6RecycleRxDataWrap (
  1113. IN EFI_EVENT Event,
  1114. IN VOID *Context
  1115. )
  1116. {
  1117. UDP6_RXDATA_WRAP *Wrap;
  1118. Wrap = (UDP6_RXDATA_WRAP *)Context;
  1119. //
  1120. // Remove the Wrap from the list it belongs to.
  1121. //
  1122. RemoveEntryList (&Wrap->Link);
  1123. //
  1124. // Free the Packet associated with this Wrap.
  1125. //
  1126. NetbufFree (Wrap->Packet);
  1127. //
  1128. // Close the event.
  1129. //
  1130. gBS->CloseEvent (Wrap->RxData.RecycleSignal);
  1131. FreePool (Wrap);
  1132. }
  1133. /**
  1134. This function wraps the Packet into RxData.
  1135. @param[in] Instance Pointer to the instance context data.
  1136. @param[in] Packet Pointer to the buffer containing the received
  1137. datagram.
  1138. @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this
  1139. datagram.
  1140. @return Pointer to the structure wrapping the RxData and the Packet. NULL will
  1141. be returned if any error occurs.
  1142. **/
  1143. UDP6_RXDATA_WRAP *
  1144. Udp6WrapRxData (
  1145. IN UDP6_INSTANCE_DATA *Instance,
  1146. IN NET_BUF *Packet,
  1147. IN EFI_UDP6_RECEIVE_DATA *RxData
  1148. )
  1149. {
  1150. EFI_STATUS Status;
  1151. UDP6_RXDATA_WRAP *Wrap;
  1152. //
  1153. // Allocate buffer for the Wrap.
  1154. //
  1155. Wrap = AllocateZeroPool (
  1156. sizeof (UDP6_RXDATA_WRAP) +
  1157. (Packet->BlockOpNum - 1) * sizeof (EFI_UDP6_FRAGMENT_DATA)
  1158. );
  1159. if (Wrap == NULL) {
  1160. return NULL;
  1161. }
  1162. InitializeListHead (&Wrap->Link);
  1163. CopyMem (&Wrap->RxData, RxData, sizeof (EFI_UDP6_RECEIVE_DATA));
  1164. //
  1165. // Create the Recycle event.
  1166. //
  1167. Status = gBS->CreateEvent (
  1168. EVT_NOTIFY_SIGNAL,
  1169. TPL_NOTIFY,
  1170. Udp6RecycleRxDataWrap,
  1171. Wrap,
  1172. &Wrap->RxData.RecycleSignal
  1173. );
  1174. if (EFI_ERROR (Status)) {
  1175. FreePool (Wrap);
  1176. return NULL;
  1177. }
  1178. Wrap->Packet = Packet;
  1179. Wrap->TimeoutTick = Instance->ConfigData.ReceiveTimeout;
  1180. return Wrap;
  1181. }
  1182. /**
  1183. This function enqueues the received datagram into the instances' receiving queues.
  1184. @param[in] Udp6Service Pointer to the udp service context data.
  1185. @param[in] Packet Pointer to the buffer containing the received
  1186. datagram.
  1187. @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this
  1188. datagram.
  1189. @return The times this datagram is enqueued.
  1190. **/
  1191. UINTN
  1192. Udp6EnqueueDgram (
  1193. IN UDP6_SERVICE_DATA *Udp6Service,
  1194. IN NET_BUF *Packet,
  1195. IN EFI_UDP6_RECEIVE_DATA *RxData
  1196. )
  1197. {
  1198. LIST_ENTRY *Entry;
  1199. UDP6_INSTANCE_DATA *Instance;
  1200. UDP6_RXDATA_WRAP *Wrap;
  1201. UINTN Enqueued;
  1202. Enqueued = 0;
  1203. NET_LIST_FOR_EACH (Entry, &Udp6Service->ChildrenList) {
  1204. //
  1205. // Iterate the instances.
  1206. //
  1207. Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link);
  1208. if (!Instance->Configured) {
  1209. continue;
  1210. }
  1211. if (Udp6MatchDgram (Instance, &RxData->UdpSession)) {
  1212. //
  1213. // Wrap the RxData and put this Wrap into the instances RcvdDgramQue.
  1214. //
  1215. Wrap = Udp6WrapRxData (Instance, Packet, RxData);
  1216. if (Wrap == NULL) {
  1217. continue;
  1218. }
  1219. NET_GET_REF (Packet);
  1220. InsertTailList (&Instance->RcvdDgramQue, &Wrap->Link);
  1221. Enqueued++;
  1222. }
  1223. }
  1224. return Enqueued;
  1225. }
  1226. /**
  1227. This function delivers the received datagrams to the specified instance.
  1228. @param[in] Instance Pointer to the instance context data.
  1229. **/
  1230. VOID
  1231. Udp6InstanceDeliverDgram (
  1232. IN UDP6_INSTANCE_DATA *Instance
  1233. )
  1234. {
  1235. UDP6_RXDATA_WRAP *Wrap;
  1236. EFI_UDP6_COMPLETION_TOKEN *Token;
  1237. NET_BUF *Dup;
  1238. EFI_UDP6_RECEIVE_DATA *RxData;
  1239. EFI_TPL OldTpl;
  1240. if (!IsListEmpty (&Instance->RcvdDgramQue) &&
  1241. !NetMapIsEmpty (&Instance->RxTokens)
  1242. )
  1243. {
  1244. Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP6_RXDATA_WRAP, Link);
  1245. if (NET_BUF_SHARED (Wrap->Packet)) {
  1246. //
  1247. // Duplicate the Packet if it is shared between instances.
  1248. //
  1249. Dup = NetbufDuplicate (Wrap->Packet, NULL, 0);
  1250. if (Dup == NULL) {
  1251. return;
  1252. }
  1253. NetbufFree (Wrap->Packet);
  1254. Wrap->Packet = Dup;
  1255. }
  1256. NetListRemoveHead (&Instance->RcvdDgramQue);
  1257. Token = (EFI_UDP6_COMPLETION_TOKEN *)NetMapRemoveHead (&Instance->RxTokens, NULL);
  1258. //
  1259. // Build the FragmentTable and set the FragmentCount in RxData.
  1260. //
  1261. RxData = &Wrap->RxData;
  1262. RxData->FragmentCount = Wrap->Packet->BlockOpNum;
  1263. NetbufBuildExt (
  1264. Wrap->Packet,
  1265. (NET_FRAGMENT *)RxData->FragmentTable,
  1266. &RxData->FragmentCount
  1267. );
  1268. Token->Status = EFI_SUCCESS;
  1269. Token->Packet.RxData = &Wrap->RxData;
  1270. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  1271. InsertTailList (&Instance->DeliveredDgramQue, &Wrap->Link);
  1272. gBS->RestoreTPL (OldTpl);
  1273. gBS->SignalEvent (Token->Event);
  1274. }
  1275. }
  1276. /**
  1277. This function delivers the datagrams enqueued in the instances.
  1278. @param[in] Udp6Service Pointer to the udp service context data.
  1279. **/
  1280. VOID
  1281. Udp6DeliverDgram (
  1282. IN UDP6_SERVICE_DATA *Udp6Service
  1283. )
  1284. {
  1285. LIST_ENTRY *Entry;
  1286. UDP6_INSTANCE_DATA *Instance;
  1287. NET_LIST_FOR_EACH (Entry, &Udp6Service->ChildrenList) {
  1288. //
  1289. // Iterate the instances.
  1290. //
  1291. Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link);
  1292. if (!Instance->Configured) {
  1293. continue;
  1294. }
  1295. //
  1296. // Deliver the datagrams of this instance.
  1297. //
  1298. Udp6InstanceDeliverDgram (Instance);
  1299. }
  1300. }
  1301. /**
  1302. This function demultiplexes the received udp datagram to the appropriate instances.
  1303. @param[in] Udp6Service Pointer to the udp service context data.
  1304. @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from
  1305. the received datagram.
  1306. @param[in] Packet Pointer to the buffer containing the received udp
  1307. datagram.
  1308. **/
  1309. VOID
  1310. Udp6Demultiplex (
  1311. IN UDP6_SERVICE_DATA *Udp6Service,
  1312. IN EFI_NET_SESSION_DATA *NetSession,
  1313. IN NET_BUF *Packet
  1314. )
  1315. {
  1316. EFI_UDP_HEADER *Udp6Header;
  1317. UINT16 HeadSum;
  1318. EFI_UDP6_RECEIVE_DATA RxData;
  1319. EFI_UDP6_SESSION_DATA *Udp6Session;
  1320. UINTN Enqueued;
  1321. if (Packet->TotalSize < UDP6_HEADER_SIZE) {
  1322. NetbufFree (Packet);
  1323. return;
  1324. }
  1325. //
  1326. // Get the datagram header from the packet buffer.
  1327. //
  1328. Udp6Header = (EFI_UDP_HEADER *)NetbufGetByte (Packet, 0, NULL);
  1329. ASSERT (Udp6Header != NULL);
  1330. if (Udp6Header == NULL) {
  1331. NetbufFree (Packet);
  1332. return;
  1333. }
  1334. if (Udp6Header->Checksum != 0) {
  1335. //
  1336. // check the checksum.
  1337. //
  1338. HeadSum = NetIp6PseudoHeadChecksum (
  1339. &NetSession->Source.v6,
  1340. &NetSession->Dest.v6,
  1341. EFI_IP_PROTO_UDP,
  1342. 0
  1343. );
  1344. if (Udp6Checksum (Packet, HeadSum) != 0) {
  1345. //
  1346. // Wrong checksum.
  1347. //
  1348. NetbufFree (Packet);
  1349. return;
  1350. }
  1351. }
  1352. Udp6Session = &RxData.UdpSession;
  1353. Udp6Session->SourcePort = NTOHS (Udp6Header->SrcPort);
  1354. Udp6Session->DestinationPort = NTOHS (Udp6Header->DstPort);
  1355. IP6_COPY_ADDRESS (&Udp6Session->SourceAddress, &NetSession->Source);
  1356. IP6_COPY_ADDRESS (&Udp6Session->DestinationAddress, &NetSession->Dest);
  1357. //
  1358. // Trim the UDP header.
  1359. //
  1360. NetbufTrim (Packet, UDP6_HEADER_SIZE, TRUE);
  1361. RxData.DataLength = (UINT32)Packet->TotalSize;
  1362. //
  1363. // Try to enqueue this datagram into the instances.
  1364. //
  1365. Enqueued = Udp6EnqueueDgram (Udp6Service, Packet, &RxData);
  1366. if (Enqueued == 0) {
  1367. //
  1368. // Send the port unreachable ICMP packet before we free this NET_BUF
  1369. //
  1370. Udp6SendPortUnreach (Udp6Service->IpIo, NetSession, Udp6Header);
  1371. }
  1372. //
  1373. // Try to free the packet before deliver it.
  1374. //
  1375. NetbufFree (Packet);
  1376. if (Enqueued > 0) {
  1377. //
  1378. // Deliver the datagram.
  1379. //
  1380. Udp6DeliverDgram (Udp6Service);
  1381. }
  1382. }
  1383. /**
  1384. This function builds and sends out a icmp port unreachable message.
  1385. @param[in] IpIo Pointer to the IP_IO instance.
  1386. @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
  1387. causes this icmp error message.
  1388. @param[in] Udp6Header Pointer to the udp header of the datagram causes
  1389. this icmp error message.
  1390. **/
  1391. VOID
  1392. Udp6SendPortUnreach (
  1393. IN IP_IO *IpIo,
  1394. IN EFI_NET_SESSION_DATA *NetSession,
  1395. IN VOID *Udp6Header
  1396. )
  1397. {
  1398. NET_BUF *Packet;
  1399. UINT32 Len;
  1400. IP6_ICMP_ERROR_HEAD *IcmpErrHdr;
  1401. UINT8 *Ptr;
  1402. IP_IO_OVERRIDE Override;
  1403. IP_IO_IP_INFO *IpSender;
  1404. EFI_IP6_MODE_DATA *Ip6ModeData;
  1405. EFI_STATUS Status;
  1406. EFI_IP6_PROTOCOL *Ip6Protocol;
  1407. Ip6ModeData = NULL;
  1408. //
  1409. // An ICMPv6 error message MUST NOT be originated as A packet destined to
  1410. // 1) an IPv6 multicast address 2) The IPv6 Unspecified Address
  1411. //
  1412. if (NetSession->IpVersion == IP_VERSION_6) {
  1413. if (NetIp6IsUnspecifiedAddr (&NetSession->Dest.v6) ||
  1414. IP6_IS_MULTICAST (&NetSession->Dest.v6)
  1415. )
  1416. {
  1417. goto EXIT;
  1418. }
  1419. }
  1420. IpSender = IpIoFindSender (&IpIo, NetSession->IpVersion, &NetSession->Dest);
  1421. //
  1422. // Get the Ipv6 Mode Data.
  1423. //
  1424. Ip6ModeData = AllocateZeroPool (sizeof (EFI_IP6_MODE_DATA));
  1425. ASSERT (Ip6ModeData != NULL);
  1426. if (Ip6ModeData == NULL) {
  1427. goto EXIT;
  1428. }
  1429. //
  1430. // If not finding the related IpSender use the default IpIo to send out
  1431. // the port unreachable ICMP message.
  1432. //
  1433. if (IpSender == NULL) {
  1434. Ip6Protocol = IpIo->Ip.Ip6;
  1435. } else {
  1436. Ip6Protocol = IpSender->Ip.Ip6;
  1437. }
  1438. Status = Ip6Protocol->GetModeData (
  1439. Ip6Protocol,
  1440. Ip6ModeData,
  1441. NULL,
  1442. NULL
  1443. );
  1444. if (EFI_ERROR (Status)) {
  1445. goto EXIT;
  1446. }
  1447. //
  1448. // The ICMP6 packet length, includes whole invoking packet and ICMP6 error header.
  1449. //
  1450. Len = NetSession->IpHdrLen +
  1451. NTOHS (((EFI_UDP_HEADER *)Udp6Header)->Length) +
  1452. sizeof (IP6_ICMP_ERROR_HEAD);
  1453. //
  1454. // If the ICMP6 packet length larger than IP MTU, adjust its length to MTU.
  1455. //
  1456. if (Ip6ModeData->MaxPacketSize < Len) {
  1457. Len = Ip6ModeData->MaxPacketSize;
  1458. }
  1459. //
  1460. // Allocate buffer for the icmp error message.
  1461. //
  1462. Packet = NetbufAlloc (Len);
  1463. if (Packet == NULL) {
  1464. goto EXIT;
  1465. }
  1466. //
  1467. // Allocate space for the IP6_ICMP_ERROR_HEAD.
  1468. //
  1469. IcmpErrHdr = (IP6_ICMP_ERROR_HEAD *)NetbufAllocSpace (Packet, Len, FALSE);
  1470. ASSERT (IcmpErrHdr != NULL);
  1471. if (IcmpErrHdr == NULL) {
  1472. goto EXIT;
  1473. }
  1474. //
  1475. // Set the required fields for the icmp port unreachable message.
  1476. //
  1477. IcmpErrHdr->Head.Type = ICMP_V6_DEST_UNREACHABLE;
  1478. IcmpErrHdr->Head.Code = ICMP_V6_PORT_UNREACHABLE;
  1479. IcmpErrHdr->Head.Checksum = 0;
  1480. IcmpErrHdr->Fourth = 0;
  1481. //
  1482. // Copy as much of invoking Packet as possible without the ICMPv6 packet
  1483. // exceeding the minimum Ipv6 MTU. The length of IP6_ICMP_ERROR_HEAD contains
  1484. // the length of EFI_IP6_HEADER, so when using the length of IP6_ICMP_ERROR_HEAD
  1485. // for pointer movement that fact should be considered.
  1486. //
  1487. Ptr = (VOID *)&IcmpErrHdr->Head;
  1488. Ptr = (UINT8 *)(UINTN)((UINTN)Ptr + sizeof (IP6_ICMP_ERROR_HEAD) - sizeof (EFI_IP6_HEADER));
  1489. CopyMem (Ptr, NetSession->IpHdr.Ip6Hdr, NetSession->IpHdrLen);
  1490. CopyMem (
  1491. Ptr + NetSession->IpHdrLen,
  1492. Udp6Header,
  1493. Len - NetSession->IpHdrLen - sizeof (IP6_ICMP_ERROR_HEAD) + sizeof (EFI_IP6_HEADER)
  1494. );
  1495. //
  1496. // Set the checksum as zero, and IP6 driver will calculate it with pseudo header.
  1497. //
  1498. IcmpErrHdr->Head.Checksum = 0;
  1499. //
  1500. // Fill the override data.
  1501. //
  1502. Override.Ip6OverrideData.FlowLabel = 0;
  1503. Override.Ip6OverrideData.HopLimit = 255;
  1504. Override.Ip6OverrideData.Protocol = IP6_ICMP;
  1505. //
  1506. // Send out this icmp packet.
  1507. //
  1508. IpIoSend (IpIo, Packet, IpSender, NULL, NULL, &NetSession->Source, &Override);
  1509. NetbufFree (Packet);
  1510. EXIT:
  1511. if (Ip6ModeData != NULL) {
  1512. FreePool (Ip6ModeData);
  1513. }
  1514. }
  1515. /**
  1516. This function handles the received Icmp Error message and de-multiplexes it to the
  1517. instance.
  1518. @param[in] Udp6Service Pointer to the udp service context data.
  1519. @param[in] IcmpError The icmp error code.
  1520. @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
  1521. from the received Icmp Error packet.
  1522. @param[in, out] Packet Pointer to the Icmp Error packet.
  1523. **/
  1524. VOID
  1525. Udp6IcmpHandler (
  1526. IN UDP6_SERVICE_DATA *Udp6Service,
  1527. IN UINT8 IcmpError,
  1528. IN EFI_NET_SESSION_DATA *NetSession,
  1529. IN OUT NET_BUF *Packet
  1530. )
  1531. {
  1532. EFI_UDP_HEADER *Udp6Header;
  1533. EFI_UDP6_SESSION_DATA Udp6Session;
  1534. LIST_ENTRY *Entry;
  1535. UDP6_INSTANCE_DATA *Instance;
  1536. if (Packet->TotalSize < UDP6_HEADER_SIZE) {
  1537. NetbufFree (Packet);
  1538. return;
  1539. }
  1540. Udp6Header = (EFI_UDP_HEADER *)NetbufGetByte (Packet, 0, NULL);
  1541. ASSERT (Udp6Header != NULL);
  1542. if (Udp6Header == NULL) {
  1543. NetbufFree (Packet);
  1544. return;
  1545. }
  1546. IP6_COPY_ADDRESS (&Udp6Session.SourceAddress, &NetSession->Source);
  1547. IP6_COPY_ADDRESS (&Udp6Session.DestinationAddress, &NetSession->Dest);
  1548. Udp6Session.SourcePort = NTOHS (Udp6Header->DstPort);
  1549. Udp6Session.DestinationPort = NTOHS (Udp6Header->SrcPort);
  1550. NET_LIST_FOR_EACH (Entry, &Udp6Service->ChildrenList) {
  1551. //
  1552. // Iterate all the instances.
  1553. //
  1554. Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link);
  1555. if (!Instance->Configured) {
  1556. continue;
  1557. }
  1558. if (Udp6MatchDgram (Instance, &Udp6Session)) {
  1559. //
  1560. // Translate the Icmp Error code according to the udp spec.
  1561. //
  1562. Instance->IcmpError = IpIoGetIcmpErrStatus (IcmpError, IP_VERSION_6, NULL, NULL);
  1563. if (IcmpError > ICMP_ERR_UNREACH_PORT) {
  1564. Instance->IcmpError = EFI_ICMP_ERROR;
  1565. }
  1566. //
  1567. // Notify the instance with the received Icmp Error.
  1568. //
  1569. Udp6ReportIcmpError (Instance);
  1570. break;
  1571. }
  1572. }
  1573. NetbufFree (Packet);
  1574. }
  1575. /**
  1576. This function reports the received ICMP error.
  1577. @param[in] Instance Pointer to the udp instance context data.
  1578. **/
  1579. VOID
  1580. Udp6ReportIcmpError (
  1581. IN UDP6_INSTANCE_DATA *Instance
  1582. )
  1583. {
  1584. EFI_UDP6_COMPLETION_TOKEN *Token;
  1585. if (NetMapIsEmpty (&Instance->RxTokens)) {
  1586. //
  1587. // There are no receive tokens to deliver the ICMP error.
  1588. //
  1589. return;
  1590. }
  1591. if (EFI_ERROR (Instance->IcmpError)) {
  1592. //
  1593. // Try to get a RxToken from the RxTokens map.
  1594. //
  1595. Token = (EFI_UDP6_COMPLETION_TOKEN *)NetMapRemoveHead (&Instance->RxTokens, NULL);
  1596. if (Token != NULL) {
  1597. //
  1598. // Report the error through the Token.
  1599. //
  1600. Token->Status = Instance->IcmpError;
  1601. gBS->SignalEvent (Token->Event);
  1602. //
  1603. // Clear the IcmpError.
  1604. //
  1605. Instance->IcmpError = EFI_SUCCESS;
  1606. }
  1607. }
  1608. }
  1609. /**
  1610. This function is a dummy ext-free function for the NET_BUF created for the output
  1611. udp datagram.
  1612. @param[in] Context Pointer to the context data.
  1613. **/
  1614. VOID
  1615. EFIAPI
  1616. Udp6NetVectorExtFree (
  1617. IN VOID *Context
  1618. )
  1619. {
  1620. }
  1621. /**
  1622. Find the key in the netmap.
  1623. @param[in] Map The netmap to search within.
  1624. @param[in] Key The key to search.
  1625. @return The point to the item contains the Key, or NULL, if Key isn't in the map.
  1626. **/
  1627. NET_MAP_ITEM *
  1628. Udp6MapMultiCastAddr (
  1629. IN NET_MAP *Map,
  1630. IN VOID *Key
  1631. )
  1632. {
  1633. LIST_ENTRY *Entry;
  1634. NET_MAP_ITEM *Item;
  1635. EFI_IPv6_ADDRESS *Addr;
  1636. ASSERT (Map != NULL);
  1637. NET_LIST_FOR_EACH (Entry, &Map->Used) {
  1638. Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
  1639. Addr = (EFI_IPv6_ADDRESS *)Item->Key;
  1640. if (EFI_IP6_EQUAL (Addr, Key)) {
  1641. return Item;
  1642. }
  1643. }
  1644. return NULL;
  1645. }