Udp4Impl.c 53 KB

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