Ip4Input.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. /** @file
  2. IP4 input process.
  3. Copyright (c) 2005 - 2020, Intel Corporation. All rights reserved.<BR>
  4. (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "Ip4Impl.h"
  8. /**
  9. Create an empty assemble entry for the packet identified by
  10. (Dst, Src, Id, Protocol). The default life for the packet is
  11. 120 seconds.
  12. @param[in] Dst The destination address
  13. @param[in] Src The source address
  14. @param[in] Id The ID field in IP header
  15. @param[in] Protocol The protocol field in IP header
  16. @return NULL if failed to allocate memory for the entry, otherwise
  17. the point to just created reassemble entry.
  18. **/
  19. IP4_ASSEMBLE_ENTRY *
  20. Ip4CreateAssembleEntry (
  21. IN IP4_ADDR Dst,
  22. IN IP4_ADDR Src,
  23. IN UINT16 Id,
  24. IN UINT8 Protocol
  25. )
  26. {
  27. IP4_ASSEMBLE_ENTRY *Assemble;
  28. Assemble = AllocatePool (sizeof (IP4_ASSEMBLE_ENTRY));
  29. if (Assemble == NULL) {
  30. return NULL;
  31. }
  32. InitializeListHead (&Assemble->Link);
  33. InitializeListHead (&Assemble->Fragments);
  34. Assemble->Dst = Dst;
  35. Assemble->Src = Src;
  36. Assemble->Id = Id;
  37. Assemble->Protocol = Protocol;
  38. Assemble->TotalLen = 0;
  39. Assemble->CurLen = 0;
  40. Assemble->Head = NULL;
  41. Assemble->Info = NULL;
  42. Assemble->Life = IP4_FRAGMENT_LIFE;
  43. return Assemble;
  44. }
  45. /**
  46. Release all the fragments of a packet, then free the assemble entry.
  47. @param[in] Assemble The assemble entry to free
  48. **/
  49. VOID
  50. Ip4FreeAssembleEntry (
  51. IN IP4_ASSEMBLE_ENTRY *Assemble
  52. )
  53. {
  54. LIST_ENTRY *Entry;
  55. LIST_ENTRY *Next;
  56. NET_BUF *Fragment;
  57. NET_LIST_FOR_EACH_SAFE (Entry, Next, &Assemble->Fragments) {
  58. Fragment = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
  59. RemoveEntryList (Entry);
  60. NetbufFree (Fragment);
  61. }
  62. FreePool (Assemble);
  63. }
  64. /**
  65. Initialize an already allocated assemble table. This is generally
  66. the assemble table embedded in the IP4 service instance.
  67. @param[in, out] Table The assemble table to initialize.
  68. **/
  69. VOID
  70. Ip4InitAssembleTable (
  71. IN OUT IP4_ASSEMBLE_TABLE *Table
  72. )
  73. {
  74. UINT32 Index;
  75. for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {
  76. InitializeListHead (&Table->Bucket[Index]);
  77. }
  78. }
  79. /**
  80. Clean up the assemble table: remove all the fragments
  81. and assemble entries.
  82. @param[in] Table The assemble table to clean up
  83. **/
  84. VOID
  85. Ip4CleanAssembleTable (
  86. IN IP4_ASSEMBLE_TABLE *Table
  87. )
  88. {
  89. LIST_ENTRY *Entry;
  90. LIST_ENTRY *Next;
  91. IP4_ASSEMBLE_ENTRY *Assemble;
  92. UINT32 Index;
  93. for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {
  94. NET_LIST_FOR_EACH_SAFE (Entry, Next, &Table->Bucket[Index]) {
  95. Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);
  96. RemoveEntryList (Entry);
  97. Ip4FreeAssembleEntry (Assemble);
  98. }
  99. }
  100. }
  101. /**
  102. Trim the packet to fit in [Start, End), and update the per
  103. packet information.
  104. @param Packet Packet to trim
  105. @param Start The sequence of the first byte to fit in
  106. @param End One beyond the sequence of last byte to fit in.
  107. **/
  108. VOID
  109. Ip4TrimPacket (
  110. IN OUT NET_BUF *Packet,
  111. IN INTN Start,
  112. IN INTN End
  113. )
  114. {
  115. IP4_CLIP_INFO *Info;
  116. INTN Len;
  117. Info = IP4_GET_CLIP_INFO (Packet);
  118. ASSERT (Info->Start + Info->Length == Info->End);
  119. ASSERT ((Info->Start < End) && (Start < Info->End));
  120. if (Info->Start < Start) {
  121. Len = Start - Info->Start;
  122. NetbufTrim (Packet, (UINT32)Len, NET_BUF_HEAD);
  123. Info->Start = Start;
  124. Info->Length -= Len;
  125. }
  126. if (End < Info->End) {
  127. Len = End - Info->End;
  128. NetbufTrim (Packet, (UINT32)Len, NET_BUF_TAIL);
  129. Info->End = End;
  130. Info->Length -= Len;
  131. }
  132. }
  133. /**
  134. Release all the fragments of the packet. This is the callback for
  135. the assembled packet's OnFree. It will free the assemble entry,
  136. which in turn will free all the fragments of the packet.
  137. @param[in] Arg The assemble entry to free
  138. **/
  139. VOID
  140. EFIAPI
  141. Ip4OnFreeFragments (
  142. IN VOID *Arg
  143. )
  144. {
  145. Ip4FreeAssembleEntry ((IP4_ASSEMBLE_ENTRY *)Arg);
  146. }
  147. /**
  148. Reassemble the IP fragments. If all the fragments of the packet
  149. have been received, it will wrap the packet in a net buffer then
  150. return it to caller. If the packet can't be assembled, NULL is
  151. return.
  152. @param Table The assemble table used. New assemble entry will be created
  153. if the Packet is from a new chain of fragments.
  154. @param Packet The fragment to assemble. It might be freed if the fragment
  155. can't be re-assembled.
  156. @return NULL if the packet can't be reassemble. The point to just assembled
  157. packet if all the fragments of the packet have arrived.
  158. **/
  159. NET_BUF *
  160. Ip4Reassemble (
  161. IN OUT IP4_ASSEMBLE_TABLE *Table,
  162. IN OUT NET_BUF *Packet
  163. )
  164. {
  165. IP4_HEAD *IpHead;
  166. IP4_CLIP_INFO *This;
  167. IP4_CLIP_INFO *Node;
  168. IP4_ASSEMBLE_ENTRY *Assemble;
  169. LIST_ENTRY *Head;
  170. LIST_ENTRY *Prev;
  171. LIST_ENTRY *Cur;
  172. NET_BUF *Fragment;
  173. NET_BUF *NewPacket;
  174. INTN Index;
  175. IpHead = Packet->Ip.Ip4;
  176. This = IP4_GET_CLIP_INFO (Packet);
  177. ASSERT (IpHead != NULL);
  178. //
  179. // First: find the related assemble entry
  180. //
  181. Assemble = NULL;
  182. Index = IP4_ASSEMBLE_HASH (IpHead->Dst, IpHead->Src, IpHead->Id, IpHead->Protocol);
  183. NET_LIST_FOR_EACH (Cur, &Table->Bucket[Index]) {
  184. Assemble = NET_LIST_USER_STRUCT (Cur, IP4_ASSEMBLE_ENTRY, Link);
  185. if ((Assemble->Dst == IpHead->Dst) && (Assemble->Src == IpHead->Src) &&
  186. (Assemble->Id == IpHead->Id) && (Assemble->Protocol == IpHead->Protocol))
  187. {
  188. break;
  189. }
  190. }
  191. //
  192. // Create a new assemble entry if no assemble entry is related to this packet
  193. //
  194. if (Cur == &Table->Bucket[Index]) {
  195. Assemble = Ip4CreateAssembleEntry (
  196. IpHead->Dst,
  197. IpHead->Src,
  198. IpHead->Id,
  199. IpHead->Protocol
  200. );
  201. if (Assemble == NULL) {
  202. goto DROP;
  203. }
  204. InsertHeadList (&Table->Bucket[Index], &Assemble->Link);
  205. }
  206. //
  207. // Assemble shouldn't be NULL here
  208. //
  209. ASSERT (Assemble != NULL);
  210. //
  211. // Find the point to insert the packet: before the first
  212. // fragment with THIS.Start < CUR.Start. the previous one
  213. // has PREV.Start <= THIS.Start < CUR.Start.
  214. //
  215. Head = &Assemble->Fragments;
  216. NET_LIST_FOR_EACH (Cur, Head) {
  217. Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
  218. if (This->Start < IP4_GET_CLIP_INFO (Fragment)->Start) {
  219. break;
  220. }
  221. }
  222. //
  223. // Check whether the current fragment overlaps with the previous one.
  224. // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to
  225. // check whether THIS.Start < PREV.End for overlap. If two fragments
  226. // overlaps, trim the overlapped part off THIS fragment.
  227. //
  228. if ((Prev = Cur->BackLink) != Head) {
  229. Fragment = NET_LIST_USER_STRUCT (Prev, NET_BUF, List);
  230. Node = IP4_GET_CLIP_INFO (Fragment);
  231. if (This->Start < Node->End) {
  232. if (This->End <= Node->End) {
  233. NetbufFree (Packet);
  234. return NULL;
  235. }
  236. Ip4TrimPacket (Packet, Node->End, This->End);
  237. }
  238. }
  239. //
  240. // Insert the fragment into the packet. The fragment may be removed
  241. // from the list by the following checks.
  242. //
  243. NetListInsertBefore (Cur, &Packet->List);
  244. //
  245. // Check the packets after the insert point. It holds that:
  246. // THIS.Start <= NODE.Start < NODE.End. The equality holds
  247. // if PREV and NEXT are continuous. THIS fragment may fill
  248. // several holes. Remove the completely overlapped fragments
  249. //
  250. while (Cur != Head) {
  251. Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
  252. Node = IP4_GET_CLIP_INFO (Fragment);
  253. //
  254. // Remove fragments completely overlapped by this fragment
  255. //
  256. if (Node->End <= This->End) {
  257. Cur = Cur->ForwardLink;
  258. RemoveEntryList (&Fragment->List);
  259. Assemble->CurLen -= Node->Length;
  260. NetbufFree (Fragment);
  261. continue;
  262. }
  263. //
  264. // The conditions are: THIS.Start <= NODE.Start, and THIS.End <
  265. // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.
  266. // If two fragments start at the same offset, remove THIS fragment
  267. // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).
  268. //
  269. if (Node->Start < This->End) {
  270. if (This->Start == Node->Start) {
  271. RemoveEntryList (&Packet->List);
  272. goto DROP;
  273. }
  274. Ip4TrimPacket (Packet, This->Start, Node->Start);
  275. }
  276. break;
  277. }
  278. //
  279. // Update the assemble info: increase the current length. If it is
  280. // the frist fragment, update the packet's IP head and per packet
  281. // info. If it is the last fragment, update the total length.
  282. //
  283. Assemble->CurLen += This->Length;
  284. if (This->Start == 0) {
  285. //
  286. // Once the first fragment is enqueued, it can't be removed
  287. // from the fragment list. So, Assemble->Head always point
  288. // to valid memory area.
  289. //
  290. ASSERT (Assemble->Head == NULL);
  291. Assemble->Head = IpHead;
  292. Assemble->Info = IP4_GET_CLIP_INFO (Packet);
  293. }
  294. //
  295. // Don't update the length more than once.
  296. //
  297. if (IP4_LAST_FRAGMENT (IpHead->Fragment) && (Assemble->TotalLen == 0)) {
  298. Assemble->TotalLen = This->End;
  299. }
  300. //
  301. // Deliver the whole packet if all the fragments received.
  302. // All fragments received if:
  303. // 1. received the last one, so, the total length is know
  304. // 2. received all the data. If the last fragment on the
  305. // queue ends at the total length, all data is received.
  306. //
  307. if ((Assemble->TotalLen != 0) && (Assemble->CurLen >= Assemble->TotalLen)) {
  308. RemoveEntryList (&Assemble->Link);
  309. //
  310. // If the packet is properly formatted, the last fragment's End
  311. // equals to the packet's total length. Otherwise, the packet
  312. // is a fake, drop it now.
  313. //
  314. Fragment = NET_LIST_USER_STRUCT (Head->BackLink, NET_BUF, List);
  315. if (IP4_GET_CLIP_INFO (Fragment)->End != Assemble->TotalLen) {
  316. Ip4FreeAssembleEntry (Assemble);
  317. return NULL;
  318. }
  319. //
  320. // Wrap the packet in a net buffer then deliver it up
  321. //
  322. NewPacket = NetbufFromBufList (
  323. &Assemble->Fragments,
  324. 0,
  325. 0,
  326. Ip4OnFreeFragments,
  327. Assemble
  328. );
  329. if (NewPacket == NULL) {
  330. Ip4FreeAssembleEntry (Assemble);
  331. return NULL;
  332. }
  333. NewPacket->Ip.Ip4 = Assemble->Head;
  334. ASSERT (Assemble->Info != NULL);
  335. CopyMem (
  336. IP4_GET_CLIP_INFO (NewPacket),
  337. Assemble->Info,
  338. sizeof (*IP4_GET_CLIP_INFO (NewPacket))
  339. );
  340. return NewPacket;
  341. }
  342. return NULL;
  343. DROP:
  344. NetbufFree (Packet);
  345. return NULL;
  346. }
  347. /**
  348. The callback function for the net buffer which wraps the packet processed by
  349. IPsec. It releases the wrap packet and also signals IPsec to free the resources.
  350. @param[in] Arg The wrap context
  351. **/
  352. VOID
  353. EFIAPI
  354. Ip4IpSecFree (
  355. IN VOID *Arg
  356. )
  357. {
  358. IP4_IPSEC_WRAP *Wrap;
  359. Wrap = (IP4_IPSEC_WRAP *)Arg;
  360. if (Wrap->IpSecRecycleSignal != NULL) {
  361. gBS->SignalEvent (Wrap->IpSecRecycleSignal);
  362. }
  363. NetbufFree (Wrap->Packet);
  364. FreePool (Wrap);
  365. return;
  366. }
  367. /**
  368. The work function to locate IPsec protocol to process the inbound or
  369. outbound IP packets. The process routine handls the packet with following
  370. actions: bypass the packet, discard the packet, or protect the packet.
  371. @param[in] IpSb The IP4 service instance.
  372. @param[in, out] Head The caller supplied IP4 header.
  373. @param[in, out] Netbuf The IP4 packet to be processed by IPsec.
  374. @param[in, out] Options The caller supplied options.
  375. @param[in, out] OptionsLen The length of the option.
  376. @param[in] Direction The directionality in an SPD entry,
  377. EfiIPsecInBound or EfiIPsecOutBound.
  378. @param[in] Context The token's wrap.
  379. @retval EFI_SUCCESS The IPsec protocol is not available or disabled.
  380. @retval EFI_SUCCESS The packet was bypassed and all buffers remain the same.
  381. @retval EFI_SUCCESS The packet was protected.
  382. @retval EFI_ACCESS_DENIED The packet was discarded.
  383. @retval EFI_OUT_OF_RESOURCES There is no sufficient resource to complete the operation.
  384. @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than the
  385. number of input data blocks when build a fragment table.
  386. **/
  387. EFI_STATUS
  388. Ip4IpSecProcessPacket (
  389. IN IP4_SERVICE *IpSb,
  390. IN OUT IP4_HEAD **Head,
  391. IN OUT NET_BUF **Netbuf,
  392. IN OUT UINT8 **Options,
  393. IN OUT UINT32 *OptionsLen,
  394. IN EFI_IPSEC_TRAFFIC_DIR Direction,
  395. IN VOID *Context
  396. )
  397. {
  398. NET_FRAGMENT *FragmentTable;
  399. NET_FRAGMENT *OriginalFragmentTable;
  400. UINT32 FragmentCount;
  401. UINT32 OriginalFragmentCount;
  402. EFI_EVENT RecycleEvent;
  403. NET_BUF *Packet;
  404. IP4_TXTOKEN_WRAP *TxWrap;
  405. IP4_IPSEC_WRAP *IpSecWrap;
  406. EFI_STATUS Status;
  407. IP4_HEAD ZeroHead;
  408. Status = EFI_SUCCESS;
  409. if (!mIpSec2Installed) {
  410. goto ON_EXIT;
  411. }
  412. ASSERT (mIpSec != NULL);
  413. Packet = *Netbuf;
  414. RecycleEvent = NULL;
  415. IpSecWrap = NULL;
  416. FragmentTable = NULL;
  417. TxWrap = (IP4_TXTOKEN_WRAP *)Context;
  418. FragmentCount = Packet->BlockOpNum;
  419. ZeroMem (&ZeroHead, sizeof (IP4_HEAD));
  420. //
  421. // Check whether the IPsec enable variable is set.
  422. //
  423. if (mIpSec->DisabledFlag) {
  424. //
  425. // If IPsec is disabled, restore the original MTU
  426. //
  427. IpSb->MaxPacketSize = IpSb->OldMaxPacketSize;
  428. goto ON_EXIT;
  429. } else {
  430. //
  431. // If IPsec is enabled, use the MTU which reduce the IPsec header length.
  432. //
  433. IpSb->MaxPacketSize = IpSb->OldMaxPacketSize - IP4_MAX_IPSEC_HEADLEN;
  434. }
  435. //
  436. // Rebuild fragment table from netbuf to ease IPsec process.
  437. //
  438. FragmentTable = AllocateZeroPool (FragmentCount * sizeof (NET_FRAGMENT));
  439. if (FragmentTable == NULL) {
  440. Status = EFI_OUT_OF_RESOURCES;
  441. goto ON_EXIT;
  442. }
  443. Status = NetbufBuildExt (Packet, FragmentTable, &FragmentCount);
  444. //
  445. // Record the original FragmentTable and count.
  446. //
  447. OriginalFragmentTable = FragmentTable;
  448. OriginalFragmentCount = FragmentCount;
  449. if (EFI_ERROR (Status)) {
  450. FreePool (FragmentTable);
  451. goto ON_EXIT;
  452. }
  453. //
  454. // Convert host byte order to network byte order
  455. //
  456. Ip4NtohHead (*Head);
  457. Status = mIpSec->ProcessExt (
  458. mIpSec,
  459. IpSb->Controller,
  460. IP_VERSION_4,
  461. (VOID *)(*Head),
  462. &(*Head)->Protocol,
  463. (VOID **)Options,
  464. OptionsLen,
  465. (EFI_IPSEC_FRAGMENT_DATA **)(&FragmentTable),
  466. &FragmentCount,
  467. Direction,
  468. &RecycleEvent
  469. );
  470. //
  471. // Convert back to host byte order
  472. //
  473. Ip4NtohHead (*Head);
  474. if (EFI_ERROR (Status)) {
  475. FreePool (OriginalFragmentTable);
  476. goto ON_EXIT;
  477. }
  478. if ((OriginalFragmentTable == FragmentTable) && (OriginalFragmentCount == FragmentCount)) {
  479. //
  480. // For ByPass Packet
  481. //
  482. FreePool (FragmentTable);
  483. goto ON_EXIT;
  484. } else {
  485. //
  486. // Free the FragmentTable which allocated before calling the IPsec.
  487. //
  488. FreePool (OriginalFragmentTable);
  489. }
  490. if ((Direction == EfiIPsecOutBound) && (TxWrap != NULL)) {
  491. TxWrap->IpSecRecycleSignal = RecycleEvent;
  492. TxWrap->Packet = NetbufFromExt (
  493. FragmentTable,
  494. FragmentCount,
  495. IP4_MAX_HEADLEN,
  496. 0,
  497. Ip4FreeTxToken,
  498. TxWrap
  499. );
  500. if (TxWrap->Packet == NULL) {
  501. //
  502. // Recover the TxWrap->Packet, if meet a error, and the caller will free
  503. // the TxWrap.
  504. //
  505. TxWrap->Packet = *Netbuf;
  506. Status = EFI_OUT_OF_RESOURCES;
  507. goto ON_EXIT;
  508. }
  509. //
  510. // Free original Netbuf.
  511. //
  512. NetIpSecNetbufFree (*Netbuf);
  513. *Netbuf = TxWrap->Packet;
  514. } else {
  515. IpSecWrap = AllocateZeroPool (sizeof (IP4_IPSEC_WRAP));
  516. if (IpSecWrap == NULL) {
  517. Status = EFI_OUT_OF_RESOURCES;
  518. gBS->SignalEvent (RecycleEvent);
  519. goto ON_EXIT;
  520. }
  521. IpSecWrap->IpSecRecycleSignal = RecycleEvent;
  522. IpSecWrap->Packet = Packet;
  523. Packet = NetbufFromExt (
  524. FragmentTable,
  525. FragmentCount,
  526. IP4_MAX_HEADLEN,
  527. 0,
  528. Ip4IpSecFree,
  529. IpSecWrap
  530. );
  531. if (Packet == NULL) {
  532. Packet = IpSecWrap->Packet;
  533. gBS->SignalEvent (RecycleEvent);
  534. FreePool (IpSecWrap);
  535. Status = EFI_OUT_OF_RESOURCES;
  536. goto ON_EXIT;
  537. }
  538. if ((Direction == EfiIPsecInBound) && (0 != CompareMem (*Head, &ZeroHead, sizeof (IP4_HEAD)))) {
  539. Ip4PrependHead (Packet, *Head, *Options, *OptionsLen);
  540. Ip4NtohHead (Packet->Ip.Ip4);
  541. NetbufTrim (Packet, ((*Head)->HeadLen << 2), TRUE);
  542. CopyMem (
  543. IP4_GET_CLIP_INFO (Packet),
  544. IP4_GET_CLIP_INFO (IpSecWrap->Packet),
  545. sizeof (IP4_CLIP_INFO)
  546. );
  547. }
  548. *Netbuf = Packet;
  549. }
  550. ON_EXIT:
  551. return Status;
  552. }
  553. /**
  554. Pre-process the IPv4 packet. First validates the IPv4 packet, and
  555. then reassembles packet if it is necessary.
  556. @param[in] IpSb Pointer to IP4_SERVICE.
  557. @param[in, out] Packet Pointer to the Packet to be processed.
  558. @param[in] Head Pointer to the IP4_HEAD.
  559. @param[in] Option Pointer to a buffer which contains the IPv4 option.
  560. @param[in] OptionLen The length of Option in bytes.
  561. @param[in] Flag The link layer flag for the packet received, such
  562. as multicast.
  563. @retval EFI_SUCCESS The received packet is in well form.
  564. @retval EFI_INVALID_PARAMETER The received packet is malformed.
  565. **/
  566. EFI_STATUS
  567. Ip4PreProcessPacket (
  568. IN IP4_SERVICE *IpSb,
  569. IN OUT NET_BUF **Packet,
  570. IN IP4_HEAD *Head,
  571. IN UINT8 *Option,
  572. IN UINT32 OptionLen,
  573. IN UINT32 Flag
  574. )
  575. {
  576. IP4_CLIP_INFO *Info;
  577. UINT32 HeadLen;
  578. UINT32 TotalLen;
  579. UINT16 Checksum;
  580. //
  581. // Check if the IP4 header is correctly formatted.
  582. //
  583. HeadLen = (Head->HeadLen << 2);
  584. TotalLen = NTOHS (Head->TotalLen);
  585. //
  586. // Mnp may deliver frame trailer sequence up, trim it off.
  587. //
  588. if (TotalLen < (*Packet)->TotalSize) {
  589. NetbufTrim (*Packet, (*Packet)->TotalSize - TotalLen, FALSE);
  590. }
  591. if ((Head->Ver != 4) || (HeadLen < IP4_MIN_HEADLEN) ||
  592. (TotalLen < HeadLen) || (TotalLen != (*Packet)->TotalSize))
  593. {
  594. return EFI_INVALID_PARAMETER;
  595. }
  596. //
  597. // Some OS may send IP packets without checksum.
  598. //
  599. Checksum = (UINT16)(~NetblockChecksum ((UINT8 *)Head, HeadLen));
  600. if ((Head->Checksum != 0) && (Checksum != 0)) {
  601. return EFI_INVALID_PARAMETER;
  602. }
  603. //
  604. // Convert the IP header to host byte order, then get the per packet info.
  605. //
  606. (*Packet)->Ip.Ip4 = Ip4NtohHead (Head);
  607. Info = IP4_GET_CLIP_INFO (*Packet);
  608. Info->LinkFlag = Flag;
  609. Info->CastType = Ip4GetHostCast (IpSb, Head->Dst, Head->Src);
  610. Info->Start = (Head->Fragment & IP4_HEAD_OFFSET_MASK) << 3;
  611. Info->Length = Head->TotalLen - HeadLen;
  612. Info->End = Info->Start + Info->Length;
  613. Info->Status = EFI_SUCCESS;
  614. //
  615. // The packet is destinated to us if the CastType is non-zero.
  616. //
  617. if ((Info->CastType == 0) || (Info->End > IP4_MAX_PACKET_SIZE)) {
  618. return EFI_INVALID_PARAMETER;
  619. }
  620. //
  621. // Validate the options. Don't call the Ip4OptionIsValid if
  622. // there is no option to save some CPU process.
  623. //
  624. if ((OptionLen > 0) && !Ip4OptionIsValid (Option, OptionLen, TRUE)) {
  625. return EFI_INVALID_PARAMETER;
  626. }
  627. //
  628. // Trim the head off, after this point, the packet is headless,
  629. // and Packet->TotalLen == Info->Length.
  630. //
  631. NetbufTrim (*Packet, HeadLen, TRUE);
  632. //
  633. // Reassemble the packet if this is a fragment. The packet is a
  634. // fragment if its head has MF (more fragment) set, or it starts
  635. // at non-zero byte.
  636. //
  637. if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) || (Info->Start != 0)) {
  638. //
  639. // Drop the fragment if DF is set but it is fragmented. Gateway
  640. // need to send a type 4 destination unreache ICMP message here.
  641. //
  642. if ((Head->Fragment & IP4_HEAD_DF_MASK) != 0) {
  643. return EFI_INVALID_PARAMETER;
  644. }
  645. //
  646. // The length of all but the last fragments is in the unit of 8 bytes.
  647. //
  648. if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) && (Info->Length % 8 != 0)) {
  649. return EFI_INVALID_PARAMETER;
  650. }
  651. *Packet = Ip4Reassemble (&IpSb->Assemble, *Packet);
  652. //
  653. // Packet assembly isn't complete, start receive more packet.
  654. //
  655. if (*Packet == NULL) {
  656. return EFI_INVALID_PARAMETER;
  657. }
  658. }
  659. return EFI_SUCCESS;
  660. }
  661. /**
  662. This function checks the IPv4 packet length.
  663. @param[in] Packet Pointer to the IPv4 Packet to be checked.
  664. @retval TRUE The input IPv4 packet length is valid.
  665. @retval FALSE The input IPv4 packet length is invalid.
  666. **/
  667. BOOLEAN
  668. Ip4IsValidPacketLength (
  669. IN NET_BUF *Packet
  670. )
  671. {
  672. //
  673. // Check the IP4 packet length.
  674. //
  675. if (Packet->TotalSize < IP4_MIN_HEADLEN) {
  676. return FALSE;
  677. }
  678. return TRUE;
  679. }
  680. /**
  681. The IP4 input routine. It is called by the IP4_INTERFACE when a
  682. IP4 fragment is received from MNP.
  683. @param[in] Ip4Instance The IP4 child that request the receive, most like
  684. it is NULL.
  685. @param[in] Packet The IP4 packet received.
  686. @param[in] IoStatus The return status of receive request.
  687. @param[in] Flag The link layer flag for the packet received, such
  688. as multicast.
  689. @param[in] Context The IP4 service instance that own the MNP.
  690. **/
  691. VOID
  692. Ip4AccpetFrame (
  693. IN IP4_PROTOCOL *Ip4Instance,
  694. IN NET_BUF *Packet,
  695. IN EFI_STATUS IoStatus,
  696. IN UINT32 Flag,
  697. IN VOID *Context
  698. )
  699. {
  700. IP4_SERVICE *IpSb;
  701. IP4_HEAD *Head;
  702. EFI_STATUS Status;
  703. IP4_HEAD ZeroHead;
  704. UINT8 *Option;
  705. UINT32 OptionLen;
  706. IpSb = (IP4_SERVICE *)Context;
  707. Option = NULL;
  708. if (EFI_ERROR (IoStatus) || (IpSb->State == IP4_SERVICE_DESTROY)) {
  709. goto DROP;
  710. }
  711. if (!Ip4IsValidPacketLength (Packet)) {
  712. goto RESTART;
  713. }
  714. Head = (IP4_HEAD *)NetbufGetByte (Packet, 0, NULL);
  715. ASSERT (Head != NULL);
  716. OptionLen = (Head->HeadLen << 2) - IP4_MIN_HEADLEN;
  717. if (OptionLen > 0) {
  718. Option = (UINT8 *)(Head + 1);
  719. }
  720. //
  721. // Validate packet format and reassemble packet if it is necessary.
  722. //
  723. Status = Ip4PreProcessPacket (
  724. IpSb,
  725. &Packet,
  726. Head,
  727. Option,
  728. OptionLen,
  729. Flag
  730. );
  731. if (EFI_ERROR (Status)) {
  732. goto RESTART;
  733. }
  734. //
  735. // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,
  736. // and no need consider any other ahead ext headers.
  737. //
  738. Status = Ip4IpSecProcessPacket (
  739. IpSb,
  740. &Head,
  741. &Packet,
  742. &Option,
  743. &OptionLen,
  744. EfiIPsecInBound,
  745. NULL
  746. );
  747. if (EFI_ERROR (Status)) {
  748. goto RESTART;
  749. }
  750. //
  751. // If the packet is protected by tunnel mode, parse the inner Ip Packet.
  752. //
  753. ZeroMem (&ZeroHead, sizeof (IP4_HEAD));
  754. if (0 == CompareMem (Head, &ZeroHead, sizeof (IP4_HEAD))) {
  755. // Packet may have been changed. Head, HeadLen, TotalLen, and
  756. // info must be reloaded before use. The ownership of the packet
  757. // is transferred to the packet process logic.
  758. //
  759. if (!Ip4IsValidPacketLength (Packet)) {
  760. goto RESTART;
  761. }
  762. Head = (IP4_HEAD *)NetbufGetByte (Packet, 0, NULL);
  763. ASSERT (Head != NULL);
  764. Status = Ip4PreProcessPacket (
  765. IpSb,
  766. &Packet,
  767. Head,
  768. Option,
  769. OptionLen,
  770. Flag
  771. );
  772. if (EFI_ERROR (Status)) {
  773. goto RESTART;
  774. }
  775. }
  776. ASSERT (Packet != NULL);
  777. Head = Packet->Ip.Ip4;
  778. IP4_GET_CLIP_INFO (Packet)->Status = EFI_SUCCESS;
  779. switch (Head->Protocol) {
  780. case EFI_IP_PROTO_ICMP:
  781. Ip4IcmpHandle (IpSb, Head, Packet);
  782. break;
  783. case IP4_PROTO_IGMP:
  784. Ip4IgmpHandle (IpSb, Head, Packet);
  785. break;
  786. default:
  787. Ip4Demultiplex (IpSb, Head, Packet, Option, OptionLen);
  788. }
  789. Packet = NULL;
  790. //
  791. // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
  792. // which are signaled with received data.
  793. //
  794. DispatchDpc ();
  795. RESTART:
  796. Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);
  797. DROP:
  798. if (Packet != NULL) {
  799. NetbufFree (Packet);
  800. }
  801. return;
  802. }
  803. /**
  804. Check whether this IP child accepts the packet.
  805. @param[in] IpInstance The IP child to check
  806. @param[in] Head The IP header of the packet
  807. @param[in] Packet The data of the packet
  808. @retval TRUE If the child wants to receive the packet.
  809. @retval FALSE Otherwise.
  810. **/
  811. BOOLEAN
  812. Ip4InstanceFrameAcceptable (
  813. IN IP4_PROTOCOL *IpInstance,
  814. IN IP4_HEAD *Head,
  815. IN NET_BUF *Packet
  816. )
  817. {
  818. IP4_ICMP_ERROR_HEAD Icmp;
  819. EFI_IP4_CONFIG_DATA *Config;
  820. IP4_CLIP_INFO *Info;
  821. UINT16 Proto;
  822. UINT32 Index;
  823. Config = &IpInstance->ConfigData;
  824. //
  825. // Dirty trick for the Tiano UEFI network stack implementation. If
  826. // ReceiveTimeout == -1, the receive of the packet for this instance
  827. // is disabled. The UEFI spec don't have such capability. We add
  828. // this to improve the performance because IP will make a copy of
  829. // the received packet for each accepting instance. Some IP instances
  830. // used by UDP/TCP only send packets, they don't wants to receive.
  831. //
  832. if (Config->ReceiveTimeout == (UINT32)(-1)) {
  833. return FALSE;
  834. }
  835. if (Config->AcceptPromiscuous) {
  836. return TRUE;
  837. }
  838. //
  839. // Use protocol from the IP header embedded in the ICMP error
  840. // message to filter, instead of ICMP itself. ICMP handle will
  841. // call Ip4Demultiplex to deliver ICMP errors.
  842. //
  843. Proto = Head->Protocol;
  844. if ((Proto == EFI_IP_PROTO_ICMP) && (!Config->AcceptAnyProtocol) && (Proto != Config->DefaultProtocol)) {
  845. NetbufCopy (Packet, 0, sizeof (Icmp.Head), (UINT8 *)&Icmp.Head);
  846. if (mIcmpClass[Icmp.Head.Type].IcmpClass == ICMP_ERROR_MESSAGE) {
  847. if (!Config->AcceptIcmpErrors) {
  848. return FALSE;
  849. }
  850. NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *)&Icmp);
  851. Proto = Icmp.IpHead.Protocol;
  852. }
  853. }
  854. //
  855. // Match the protocol
  856. //
  857. if (!Config->AcceptAnyProtocol && (Proto != Config->DefaultProtocol)) {
  858. return FALSE;
  859. }
  860. //
  861. // Check for broadcast, the caller has computed the packet's
  862. // cast type for this child's interface.
  863. //
  864. Info = IP4_GET_CLIP_INFO (Packet);
  865. if (IP4_IS_BROADCAST (Info->CastType)) {
  866. return Config->AcceptBroadcast;
  867. }
  868. //
  869. // If it is a multicast packet, check whether we are in the group.
  870. //
  871. if (Info->CastType == IP4_MULTICAST) {
  872. //
  873. // Receive the multicast if the instance wants to receive all packets.
  874. //
  875. if (!IpInstance->ConfigData.UseDefaultAddress && (IpInstance->Interface->Ip == 0)) {
  876. return TRUE;
  877. }
  878. for (Index = 0; Index < IpInstance->GroupCount; Index++) {
  879. if (IpInstance->Groups[Index] == HTONL (Head->Dst)) {
  880. break;
  881. }
  882. }
  883. return (BOOLEAN)(Index < IpInstance->GroupCount);
  884. }
  885. return TRUE;
  886. }
  887. /**
  888. Enqueue a shared copy of the packet to the IP4 child if the
  889. packet is acceptable to it. Here the data of the packet is
  890. shared, but the net buffer isn't.
  891. @param[in] IpInstance The IP4 child to enqueue the packet to
  892. @param[in] Head The IP header of the received packet
  893. @param[in] Packet The data of the received packet
  894. @retval EFI_NOT_STARTED The IP child hasn't been configured.
  895. @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
  896. @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
  897. @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
  898. **/
  899. EFI_STATUS
  900. Ip4InstanceEnquePacket (
  901. IN IP4_PROTOCOL *IpInstance,
  902. IN IP4_HEAD *Head,
  903. IN NET_BUF *Packet
  904. )
  905. {
  906. IP4_CLIP_INFO *Info;
  907. NET_BUF *Clone;
  908. //
  909. // Check whether the packet is acceptable to this instance.
  910. //
  911. if (IpInstance->State != IP4_STATE_CONFIGED) {
  912. return EFI_NOT_STARTED;
  913. }
  914. if (!Ip4InstanceFrameAcceptable (IpInstance, Head, Packet)) {
  915. return EFI_INVALID_PARAMETER;
  916. }
  917. //
  918. // Enqueue a shared copy of the packet.
  919. //
  920. Clone = NetbufClone (Packet);
  921. if (Clone == NULL) {
  922. return EFI_OUT_OF_RESOURCES;
  923. }
  924. //
  925. // Set the receive time out for the assembled packet. If it expires,
  926. // packet will be removed from the queue.
  927. //
  928. Info = IP4_GET_CLIP_INFO (Clone);
  929. Info->Life = IP4_US_TO_SEC (IpInstance->ConfigData.ReceiveTimeout);
  930. InsertTailList (&IpInstance->Received, &Clone->List);
  931. return EFI_SUCCESS;
  932. }
  933. /**
  934. The signal handle of IP4's recycle event. It is called back
  935. when the upper layer release the packet.
  936. @param Event The IP4's recycle event.
  937. @param Context The context of the handle, which is a
  938. IP4_RXDATA_WRAP
  939. **/
  940. VOID
  941. EFIAPI
  942. Ip4OnRecyclePacket (
  943. IN EFI_EVENT Event,
  944. IN VOID *Context
  945. )
  946. {
  947. IP4_RXDATA_WRAP *Wrap;
  948. Wrap = (IP4_RXDATA_WRAP *)Context;
  949. EfiAcquireLockOrFail (&Wrap->IpInstance->RecycleLock);
  950. RemoveEntryList (&Wrap->Link);
  951. EfiReleaseLock (&Wrap->IpInstance->RecycleLock);
  952. ASSERT (!NET_BUF_SHARED (Wrap->Packet));
  953. NetbufFree (Wrap->Packet);
  954. gBS->CloseEvent (Wrap->RxData.RecycleSignal);
  955. FreePool (Wrap);
  956. }
  957. /**
  958. Wrap the received packet to a IP4_RXDATA_WRAP, which will be
  959. delivered to the upper layer. Each IP4 child that accepts the
  960. packet will get a not-shared copy of the packet which is wrapped
  961. in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
  962. to the upper layer. Upper layer will signal the recycle event in
  963. it when it is done with the packet.
  964. @param[in] IpInstance The IP4 child to receive the packet.
  965. @param[in] Packet The packet to deliver up.
  966. @retval Wrap if warp the packet succeed.
  967. @retval NULL failed to wrap the packet .
  968. **/
  969. IP4_RXDATA_WRAP *
  970. Ip4WrapRxData (
  971. IN IP4_PROTOCOL *IpInstance,
  972. IN NET_BUF *Packet
  973. )
  974. {
  975. IP4_RXDATA_WRAP *Wrap;
  976. EFI_IP4_RECEIVE_DATA *RxData;
  977. EFI_STATUS Status;
  978. BOOLEAN RawData;
  979. Wrap = AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet->BlockOpNum));
  980. if (Wrap == NULL) {
  981. return NULL;
  982. }
  983. InitializeListHead (&Wrap->Link);
  984. Wrap->IpInstance = IpInstance;
  985. Wrap->Packet = Packet;
  986. RxData = &Wrap->RxData;
  987. ZeroMem (RxData, sizeof (EFI_IP4_RECEIVE_DATA));
  988. Status = gBS->CreateEvent (
  989. EVT_NOTIFY_SIGNAL,
  990. TPL_NOTIFY,
  991. Ip4OnRecyclePacket,
  992. Wrap,
  993. &RxData->RecycleSignal
  994. );
  995. if (EFI_ERROR (Status)) {
  996. FreePool (Wrap);
  997. return NULL;
  998. }
  999. ASSERT (Packet->Ip.Ip4 != NULL);
  1000. ASSERT (IpInstance != NULL);
  1001. RawData = IpInstance->ConfigData.RawData;
  1002. //
  1003. // The application expects a network byte order header.
  1004. //
  1005. if (!RawData) {
  1006. RxData->HeaderLength = (Packet->Ip.Ip4->HeadLen << 2);
  1007. RxData->Header = (EFI_IP4_HEADER *)Ip4NtohHead (Packet->Ip.Ip4);
  1008. RxData->OptionsLength = RxData->HeaderLength - IP4_MIN_HEADLEN;
  1009. RxData->Options = NULL;
  1010. if (RxData->OptionsLength != 0) {
  1011. RxData->Options = (VOID *)(RxData->Header + 1);
  1012. }
  1013. }
  1014. RxData->DataLength = Packet->TotalSize;
  1015. //
  1016. // Build the fragment table to be delivered up.
  1017. //
  1018. RxData->FragmentCount = Packet->BlockOpNum;
  1019. NetbufBuildExt (Packet, (NET_FRAGMENT *)RxData->FragmentTable, &RxData->FragmentCount);
  1020. return Wrap;
  1021. }
  1022. /**
  1023. Deliver the received packets to upper layer if there are both received
  1024. requests and enqueued packets. If the enqueued packet is shared, it will
  1025. duplicate it to a non-shared packet, release the shared packet, then
  1026. deliver the non-shared packet up.
  1027. @param[in] IpInstance The IP child to deliver the packet up.
  1028. @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
  1029. packets.
  1030. @retval EFI_SUCCESS All the enqueued packets that can be delivered
  1031. are delivered up.
  1032. **/
  1033. EFI_STATUS
  1034. Ip4InstanceDeliverPacket (
  1035. IN IP4_PROTOCOL *IpInstance
  1036. )
  1037. {
  1038. EFI_IP4_COMPLETION_TOKEN *Token;
  1039. IP4_RXDATA_WRAP *Wrap;
  1040. NET_BUF *Packet;
  1041. NET_BUF *Dup;
  1042. UINT8 *Head;
  1043. UINT32 HeadLen;
  1044. //
  1045. // Deliver a packet if there are both a packet and a receive token.
  1046. //
  1047. while (!IsListEmpty (&IpInstance->Received) &&
  1048. !NetMapIsEmpty (&IpInstance->RxTokens))
  1049. {
  1050. Packet = NET_LIST_HEAD (&IpInstance->Received, NET_BUF, List);
  1051. if (!NET_BUF_SHARED (Packet)) {
  1052. //
  1053. // If this is the only instance that wants the packet, wrap it up.
  1054. //
  1055. Wrap = Ip4WrapRxData (IpInstance, Packet);
  1056. if (Wrap == NULL) {
  1057. return EFI_OUT_OF_RESOURCES;
  1058. }
  1059. RemoveEntryList (&Packet->List);
  1060. } else {
  1061. //
  1062. // Create a duplicated packet if this packet is shared
  1063. //
  1064. if (IpInstance->ConfigData.RawData) {
  1065. HeadLen = 0;
  1066. } else {
  1067. HeadLen = IP4_MAX_HEADLEN;
  1068. }
  1069. Dup = NetbufDuplicate (Packet, NULL, HeadLen);
  1070. if (Dup == NULL) {
  1071. return EFI_OUT_OF_RESOURCES;
  1072. }
  1073. if (!IpInstance->ConfigData.RawData) {
  1074. //
  1075. // Copy the IP head over. The packet to deliver up is
  1076. // headless. Trim the head off after copy. The IP head
  1077. // may be not continuous before the data.
  1078. //
  1079. Head = NetbufAllocSpace (Dup, IP4_MAX_HEADLEN, NET_BUF_HEAD);
  1080. ASSERT (Head != NULL);
  1081. Dup->Ip.Ip4 = (IP4_HEAD *)Head;
  1082. CopyMem (Head, Packet->Ip.Ip4, Packet->Ip.Ip4->HeadLen << 2);
  1083. NetbufTrim (Dup, IP4_MAX_HEADLEN, TRUE);
  1084. }
  1085. Wrap = Ip4WrapRxData (IpInstance, Dup);
  1086. if (Wrap == NULL) {
  1087. NetbufFree (Dup);
  1088. return EFI_OUT_OF_RESOURCES;
  1089. }
  1090. RemoveEntryList (&Packet->List);
  1091. NetbufFree (Packet);
  1092. Packet = Dup;
  1093. }
  1094. //
  1095. // Insert it into the delivered packet, then get a user's
  1096. // receive token, pass the wrapped packet up.
  1097. //
  1098. EfiAcquireLockOrFail (&IpInstance->RecycleLock);
  1099. InsertHeadList (&IpInstance->Delivered, &Wrap->Link);
  1100. EfiReleaseLock (&IpInstance->RecycleLock);
  1101. Token = NetMapRemoveHead (&IpInstance->RxTokens, NULL);
  1102. Token->Status = IP4_GET_CLIP_INFO (Packet)->Status;
  1103. Token->Packet.RxData = &Wrap->RxData;
  1104. gBS->SignalEvent (Token->Event);
  1105. }
  1106. return EFI_SUCCESS;
  1107. }
  1108. /**
  1109. Enqueue a received packet to all the IP children that share
  1110. the same interface.
  1111. @param[in] IpSb The IP4 service instance that receive the packet.
  1112. @param[in] Head The header of the received packet.
  1113. @param[in] Packet The data of the received packet.
  1114. @param[in] Option Point to the IP4 packet header options.
  1115. @param[in] OptionLen Length of the IP4 packet header options.
  1116. @param[in] IpIf The interface to enqueue the packet to.
  1117. @return The number of the IP4 children that accepts the packet
  1118. **/
  1119. INTN
  1120. Ip4InterfaceEnquePacket (
  1121. IN IP4_SERVICE *IpSb,
  1122. IN IP4_HEAD *Head,
  1123. IN NET_BUF *Packet,
  1124. IN UINT8 *Option,
  1125. IN UINT32 OptionLen,
  1126. IN IP4_INTERFACE *IpIf
  1127. )
  1128. {
  1129. IP4_PROTOCOL *IpInstance;
  1130. IP4_CLIP_INFO *Info;
  1131. LIST_ENTRY *Entry;
  1132. INTN Enqueued;
  1133. INTN LocalType;
  1134. INTN SavedType;
  1135. //
  1136. // First, check that the packet is acceptable to this interface
  1137. // and find the local cast type for the interface. A packet sent
  1138. // to say 192.168.1.1 should NOT be deliver to 10.0.0.1 unless
  1139. // promiscuous receiving.
  1140. //
  1141. LocalType = 0;
  1142. Info = IP4_GET_CLIP_INFO (Packet);
  1143. if ((Info->CastType == IP4_MULTICAST) || (Info->CastType == IP4_LOCAL_BROADCAST)) {
  1144. //
  1145. // If the CastType is multicast, don't need to filter against
  1146. // the group address here, Ip4InstanceFrameAcceptable will do
  1147. // that later.
  1148. //
  1149. LocalType = Info->CastType;
  1150. } else {
  1151. //
  1152. // Check the destination against local IP. If the station
  1153. // address is 0.0.0.0, it means receiving all the IP destined
  1154. // to local non-zero IP. Otherwise, it is necessary to compare
  1155. // the destination to the interface's IP address.
  1156. //
  1157. if (IpIf->Ip == IP4_ALLZERO_ADDRESS) {
  1158. LocalType = IP4_LOCAL_HOST;
  1159. } else {
  1160. LocalType = Ip4GetNetCast (Head->Dst, IpIf);
  1161. if ((LocalType == 0) && IpIf->PromiscRecv) {
  1162. LocalType = IP4_PROMISCUOUS;
  1163. }
  1164. }
  1165. }
  1166. if (LocalType == 0) {
  1167. return 0;
  1168. }
  1169. //
  1170. // Iterate through the ip instances on the interface, enqueue
  1171. // the packet if filter passed. Save the original cast type,
  1172. // and pass the local cast type to the IP children on the
  1173. // interface. The global cast type will be restored later.
  1174. //
  1175. SavedType = Info->CastType;
  1176. Info->CastType = LocalType;
  1177. Enqueued = 0;
  1178. NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {
  1179. IpInstance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);
  1180. NET_CHECK_SIGNATURE (IpInstance, IP4_PROTOCOL_SIGNATURE);
  1181. //
  1182. // In RawData mode, add IPv4 headers and options back to packet.
  1183. //
  1184. if ((IpInstance->ConfigData.RawData) && (Option != NULL) && (OptionLen != 0)) {
  1185. Ip4PrependHead (Packet, Head, Option, OptionLen);
  1186. }
  1187. if (Ip4InstanceEnquePacket (IpInstance, Head, Packet) == EFI_SUCCESS) {
  1188. Enqueued++;
  1189. }
  1190. }
  1191. Info->CastType = SavedType;
  1192. return Enqueued;
  1193. }
  1194. /**
  1195. Deliver the packet for each IP4 child on the interface.
  1196. @param[in] IpSb The IP4 service instance that received the packet
  1197. @param[in] IpIf The IP4 interface to deliver the packet.
  1198. @retval EFI_SUCCESS It always returns EFI_SUCCESS now
  1199. **/
  1200. EFI_STATUS
  1201. Ip4InterfaceDeliverPacket (
  1202. IN IP4_SERVICE *IpSb,
  1203. IN IP4_INTERFACE *IpIf
  1204. )
  1205. {
  1206. IP4_PROTOCOL *Ip4Instance;
  1207. LIST_ENTRY *Entry;
  1208. NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {
  1209. Ip4Instance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);
  1210. Ip4InstanceDeliverPacket (Ip4Instance);
  1211. }
  1212. return EFI_SUCCESS;
  1213. }
  1214. /**
  1215. Demultiple the packet. the packet delivery is processed in two
  1216. passes. The first pass will enqueue a shared copy of the packet
  1217. to each IP4 child that accepts the packet. The second pass will
  1218. deliver a non-shared copy of the packet to each IP4 child that
  1219. has pending receive requests. Data is copied if more than one
  1220. child wants to consume the packet because each IP child needs
  1221. its own copy of the packet to make changes.
  1222. @param[in] IpSb The IP4 service instance that received the packet.
  1223. @param[in] Head The header of the received packet.
  1224. @param[in] Packet The data of the received packet.
  1225. @param[in] Option Point to the IP4 packet header options.
  1226. @param[in] OptionLen Length of the IP4 packet header options.
  1227. @retval EFI_NOT_FOUND No IP child accepts the packet.
  1228. @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
  1229. children.
  1230. **/
  1231. EFI_STATUS
  1232. Ip4Demultiplex (
  1233. IN IP4_SERVICE *IpSb,
  1234. IN IP4_HEAD *Head,
  1235. IN NET_BUF *Packet,
  1236. IN UINT8 *Option,
  1237. IN UINT32 OptionLen
  1238. )
  1239. {
  1240. LIST_ENTRY *Entry;
  1241. IP4_INTERFACE *IpIf;
  1242. INTN Enqueued;
  1243. //
  1244. // Two pass delivery: first, enqueue a shared copy of the packet
  1245. // to each instance that accept the packet.
  1246. //
  1247. Enqueued = 0;
  1248. NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
  1249. IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
  1250. if (IpIf->Configured) {
  1251. Enqueued += Ip4InterfaceEnquePacket (
  1252. IpSb,
  1253. Head,
  1254. Packet,
  1255. Option,
  1256. OptionLen,
  1257. IpIf
  1258. );
  1259. }
  1260. }
  1261. //
  1262. // Second: deliver a duplicate of the packet to each instance.
  1263. // Release the local reference first, so that the last instance
  1264. // getting the packet will not copy the data.
  1265. //
  1266. NetbufFree (Packet);
  1267. if (Enqueued == 0) {
  1268. return EFI_NOT_FOUND;
  1269. }
  1270. NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
  1271. IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
  1272. if (IpIf->Configured) {
  1273. Ip4InterfaceDeliverPacket (IpSb, IpIf);
  1274. }
  1275. }
  1276. return EFI_SUCCESS;
  1277. }
  1278. /**
  1279. Timeout the fragment and enqueued packets.
  1280. @param[in] IpSb The IP4 service instance to timeout
  1281. **/
  1282. VOID
  1283. Ip4PacketTimerTicking (
  1284. IN IP4_SERVICE *IpSb
  1285. )
  1286. {
  1287. LIST_ENTRY *InstanceEntry;
  1288. LIST_ENTRY *Entry;
  1289. LIST_ENTRY *Next;
  1290. IP4_PROTOCOL *IpInstance;
  1291. IP4_ASSEMBLE_ENTRY *Assemble;
  1292. NET_BUF *Packet;
  1293. IP4_CLIP_INFO *Info;
  1294. UINT32 Index;
  1295. //
  1296. // First, time out the fragments. The packet's life is counting down
  1297. // once the first-arrived fragment was received.
  1298. //
  1299. for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {
  1300. NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->Assemble.Bucket[Index]) {
  1301. Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);
  1302. if ((Assemble->Life > 0) && (--Assemble->Life == 0)) {
  1303. RemoveEntryList (Entry);
  1304. Ip4FreeAssembleEntry (Assemble);
  1305. }
  1306. }
  1307. }
  1308. NET_LIST_FOR_EACH (InstanceEntry, &IpSb->Children) {
  1309. IpInstance = NET_LIST_USER_STRUCT (InstanceEntry, IP4_PROTOCOL, Link);
  1310. //
  1311. // Second, time out the assembled packets enqueued on each IP child.
  1312. //
  1313. NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpInstance->Received) {
  1314. Packet = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
  1315. Info = IP4_GET_CLIP_INFO (Packet);
  1316. if ((Info->Life > 0) && (--Info->Life == 0)) {
  1317. RemoveEntryList (Entry);
  1318. NetbufFree (Packet);
  1319. }
  1320. }
  1321. //
  1322. // Third: time out the transmitted packets.
  1323. //
  1324. NetMapIterate (&IpInstance->TxTokens, Ip4SentPacketTicking, NULL);
  1325. }
  1326. }