Ip6Route.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /** @file
  2. The functions and routines to handle the route caches and route table.
  3. Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Ip6Impl.h"
  7. /**
  8. This is the worker function for IP6_ROUTE_CACHE_HASH(). It calculates the value
  9. as the index of the route cache bucket according to the prefix of two IPv6 addresses.
  10. @param[in] Ip1 The IPv6 address.
  11. @param[in] Ip2 The IPv6 address.
  12. @return The hash value of the prefix of two IPv6 addresses.
  13. **/
  14. UINT32
  15. Ip6RouteCacheHash (
  16. IN EFI_IPv6_ADDRESS *Ip1,
  17. IN EFI_IPv6_ADDRESS *Ip2
  18. )
  19. {
  20. UINT32 Prefix1;
  21. UINT32 Prefix2;
  22. Prefix1 = *((UINT32 *)((UINTN *)(Ip1)));
  23. Prefix2 = *((UINT32 *)((UINTN *)(Ip2)));
  24. return ((UINT32)(Prefix1 ^ Prefix2) % IP6_ROUTE_CACHE_HASH_SIZE);
  25. }
  26. /**
  27. Allocate a route entry then initialize it with the Destination/PrefixLength
  28. and Gateway.
  29. @param[in] Destination The IPv6 destination address. This is an optional
  30. parameter that may be NULL.
  31. @param[in] PrefixLength The destination network's prefix length.
  32. @param[in] GatewayAddress The next hop address. This is an optional parameter
  33. that may be NULL.
  34. @return NULL if failed to allocate memory; otherwise, the newly created route entry.
  35. **/
  36. IP6_ROUTE_ENTRY *
  37. Ip6CreateRouteEntry (
  38. IN EFI_IPv6_ADDRESS *Destination OPTIONAL,
  39. IN UINT8 PrefixLength,
  40. IN EFI_IPv6_ADDRESS *GatewayAddress OPTIONAL
  41. )
  42. {
  43. IP6_ROUTE_ENTRY *RtEntry;
  44. RtEntry = AllocateZeroPool (sizeof (IP6_ROUTE_ENTRY));
  45. if (RtEntry == NULL) {
  46. return NULL;
  47. }
  48. RtEntry->RefCnt = 1;
  49. RtEntry->Flag = 0;
  50. RtEntry->PrefixLength = PrefixLength;
  51. if (Destination != NULL) {
  52. IP6_COPY_ADDRESS (&RtEntry->Destination, Destination);
  53. }
  54. if (GatewayAddress != NULL) {
  55. IP6_COPY_ADDRESS (&RtEntry->NextHop, GatewayAddress);
  56. }
  57. return RtEntry;
  58. }
  59. /**
  60. Free the route table entry. It is reference counted.
  61. @param[in, out] RtEntry The route entry to free.
  62. **/
  63. VOID
  64. Ip6FreeRouteEntry (
  65. IN OUT IP6_ROUTE_ENTRY *RtEntry
  66. )
  67. {
  68. ASSERT ((RtEntry != NULL) && (RtEntry->RefCnt > 0));
  69. if (--RtEntry->RefCnt == 0) {
  70. FreePool (RtEntry);
  71. }
  72. }
  73. /**
  74. Search the route table for a most specific match to the Dst. It searches
  75. from the longest route area (prefix length == 128) to the shortest route area
  76. (default routes). In each route area, it will first search the instance's
  77. route table, then the default route table. This is required per the following
  78. requirements:
  79. 1. IP search the route table for a most specific match.
  80. 2. The local route entries have precedence over the default route entry.
  81. @param[in] RtTable The route table to search from.
  82. @param[in] Destination The destination address to search. If NULL, search
  83. the route table by NextHop.
  84. @param[in] NextHop The next hop address. If NULL, search the route table
  85. by Destination.
  86. @return NULL if no route matches the Dst. Otherwise, the point to the
  87. @return most specific route to the Dst.
  88. **/
  89. IP6_ROUTE_ENTRY *
  90. Ip6FindRouteEntry (
  91. IN IP6_ROUTE_TABLE *RtTable,
  92. IN EFI_IPv6_ADDRESS *Destination OPTIONAL,
  93. IN EFI_IPv6_ADDRESS *NextHop OPTIONAL
  94. )
  95. {
  96. LIST_ENTRY *Entry;
  97. IP6_ROUTE_ENTRY *RtEntry;
  98. INTN Index;
  99. ASSERT (Destination != NULL || NextHop != NULL);
  100. RtEntry = NULL;
  101. for (Index = IP6_PREFIX_MAX; Index >= 0; Index--) {
  102. NET_LIST_FOR_EACH (Entry, &RtTable->RouteArea[Index]) {
  103. RtEntry = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_ENTRY, Link);
  104. if (Destination != NULL) {
  105. if (NetIp6IsNetEqual (Destination, &RtEntry->Destination, RtEntry->PrefixLength)) {
  106. NET_GET_REF (RtEntry);
  107. return RtEntry;
  108. }
  109. } else if (NextHop != NULL) {
  110. if (NetIp6IsNetEqual (NextHop, &RtEntry->NextHop, RtEntry->PrefixLength)) {
  111. NET_GET_REF (RtEntry);
  112. return RtEntry;
  113. }
  114. }
  115. }
  116. }
  117. return NULL;
  118. }
  119. /**
  120. Allocate and initialize a IP6 route cache entry.
  121. @param[in] Dst The destination address.
  122. @param[in] Src The source address.
  123. @param[in] GateWay The next hop address.
  124. @param[in] Tag The tag from the caller. This marks all the cache entries
  125. spawned from one route table entry.
  126. @return NULL if failed to allocate memory for the cache. Otherwise, point
  127. to the created route cache entry.
  128. **/
  129. IP6_ROUTE_CACHE_ENTRY *
  130. Ip6CreateRouteCacheEntry (
  131. IN EFI_IPv6_ADDRESS *Dst,
  132. IN EFI_IPv6_ADDRESS *Src,
  133. IN EFI_IPv6_ADDRESS *GateWay,
  134. IN UINTN Tag
  135. )
  136. {
  137. IP6_ROUTE_CACHE_ENTRY *RtCacheEntry;
  138. RtCacheEntry = AllocatePool (sizeof (IP6_ROUTE_CACHE_ENTRY));
  139. if (RtCacheEntry == NULL) {
  140. return NULL;
  141. }
  142. RtCacheEntry->RefCnt = 1;
  143. RtCacheEntry->Tag = Tag;
  144. IP6_COPY_ADDRESS (&RtCacheEntry->Destination, Dst);
  145. IP6_COPY_ADDRESS (&RtCacheEntry->Source, Src);
  146. IP6_COPY_ADDRESS (&RtCacheEntry->NextHop, GateWay);
  147. return RtCacheEntry;
  148. }
  149. /**
  150. Free the route cache entry. It is reference counted.
  151. @param[in, out] RtCacheEntry The route cache entry to free.
  152. **/
  153. VOID
  154. Ip6FreeRouteCacheEntry (
  155. IN OUT IP6_ROUTE_CACHE_ENTRY *RtCacheEntry
  156. )
  157. {
  158. ASSERT (RtCacheEntry->RefCnt > 0);
  159. if (--RtCacheEntry->RefCnt == 0) {
  160. FreePool (RtCacheEntry);
  161. }
  162. }
  163. /**
  164. Find a route cache with the destination and source address. This is
  165. used by the ICMPv6 redirect message process.
  166. @param[in] RtTable The route table to search the cache for.
  167. @param[in] Dest The destination address.
  168. @param[in] Src The source address.
  169. @return NULL if no route entry to the (Dest, Src). Otherwise, the pointer
  170. to the correct route cache entry.
  171. **/
  172. IP6_ROUTE_CACHE_ENTRY *
  173. Ip6FindRouteCache (
  174. IN IP6_ROUTE_TABLE *RtTable,
  175. IN EFI_IPv6_ADDRESS *Dest,
  176. IN EFI_IPv6_ADDRESS *Src
  177. )
  178. {
  179. LIST_ENTRY *Entry;
  180. IP6_ROUTE_CACHE_ENTRY *RtCacheEntry;
  181. UINT32 Index;
  182. Index = IP6_ROUTE_CACHE_HASH (Dest, Src);
  183. NET_LIST_FOR_EACH (Entry, &RtTable->Cache.CacheBucket[Index]) {
  184. RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_CACHE_ENTRY, Link);
  185. if (EFI_IP6_EQUAL (Dest, &RtCacheEntry->Destination) && EFI_IP6_EQUAL (Src, &RtCacheEntry->Source)) {
  186. NET_GET_REF (RtCacheEntry);
  187. return RtCacheEntry;
  188. }
  189. }
  190. return NULL;
  191. }
  192. /**
  193. Build an array of EFI_IP6_ROUTE_TABLE to be returned to the caller. The number
  194. of EFI_IP6_ROUTE_TABLE is also returned.
  195. @param[in] RouteTable The pointer of IP6_ROUTE_TABLE internal used.
  196. @param[out] EfiRouteCount The number of returned route entries.
  197. @param[out] EfiRouteTable The pointer to the array of EFI_IP6_ROUTE_TABLE.
  198. If NULL, only the route entry count is returned.
  199. @retval EFI_SUCCESS The EFI_IP6_ROUTE_TABLE successfully built.
  200. @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the route table.
  201. **/
  202. EFI_STATUS
  203. Ip6BuildEfiRouteTable (
  204. IN IP6_ROUTE_TABLE *RouteTable,
  205. OUT UINT32 *EfiRouteCount,
  206. OUT EFI_IP6_ROUTE_TABLE **EfiRouteTable OPTIONAL
  207. )
  208. {
  209. LIST_ENTRY *Entry;
  210. IP6_ROUTE_ENTRY *RtEntry;
  211. EFI_IP6_ROUTE_TABLE *EfiTable;
  212. UINT32 Count;
  213. INT32 Index;
  214. ASSERT (EfiRouteCount != NULL);
  215. Count = RouteTable->TotalNum;
  216. *EfiRouteCount = Count;
  217. if ((EfiRouteTable == NULL) || (Count == 0)) {
  218. return EFI_SUCCESS;
  219. }
  220. if (*EfiRouteTable == NULL) {
  221. *EfiRouteTable = AllocatePool (sizeof (EFI_IP6_ROUTE_TABLE) * Count);
  222. if (*EfiRouteTable == NULL) {
  223. return EFI_OUT_OF_RESOURCES;
  224. }
  225. }
  226. EfiTable = *EfiRouteTable;
  227. //
  228. // Copy the route entry to EFI route table.
  229. //
  230. Count = 0;
  231. for (Index = IP6_PREFIX_MAX; Index >= 0; Index--) {
  232. NET_LIST_FOR_EACH (Entry, &(RouteTable->RouteArea[Index])) {
  233. RtEntry = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_ENTRY, Link);
  234. Ip6CopyAddressByPrefix (
  235. &EfiTable[Count].Destination,
  236. &RtEntry->Destination,
  237. RtEntry->PrefixLength
  238. );
  239. IP6_COPY_ADDRESS (&EfiTable[Count].Gateway, &RtEntry->NextHop);
  240. EfiTable[Count].PrefixLength = RtEntry->PrefixLength;
  241. Count++;
  242. }
  243. }
  244. ASSERT (Count == RouteTable->TotalNum);
  245. return EFI_SUCCESS;
  246. }
  247. /**
  248. Create an empty route table. This includes its internal route cache.
  249. @return NULL if failed to allocate memory for the route table. Otherwise,
  250. the point to newly created route table.
  251. **/
  252. IP6_ROUTE_TABLE *
  253. Ip6CreateRouteTable (
  254. VOID
  255. )
  256. {
  257. IP6_ROUTE_TABLE *RtTable;
  258. UINT32 Index;
  259. RtTable = AllocatePool (sizeof (IP6_ROUTE_TABLE));
  260. if (RtTable == NULL) {
  261. return NULL;
  262. }
  263. RtTable->RefCnt = 1;
  264. RtTable->TotalNum = 0;
  265. for (Index = 0; Index <= IP6_PREFIX_MAX; Index++) {
  266. InitializeListHead (&RtTable->RouteArea[Index]);
  267. }
  268. for (Index = 0; Index < IP6_ROUTE_CACHE_HASH_SIZE; Index++) {
  269. InitializeListHead (&RtTable->Cache.CacheBucket[Index]);
  270. RtTable->Cache.CacheNum[Index] = 0;
  271. }
  272. return RtTable;
  273. }
  274. /**
  275. Free the route table and its associated route cache. Route
  276. table is reference counted.
  277. @param[in, out] RtTable The route table to free.
  278. **/
  279. VOID
  280. Ip6CleanRouteTable (
  281. IN OUT IP6_ROUTE_TABLE *RtTable
  282. )
  283. {
  284. LIST_ENTRY *Entry;
  285. LIST_ENTRY *Next;
  286. IP6_ROUTE_ENTRY *RtEntry;
  287. IP6_ROUTE_CACHE_ENTRY *RtCacheEntry;
  288. UINT32 Index;
  289. ASSERT (RtTable->RefCnt > 0);
  290. if (--RtTable->RefCnt > 0) {
  291. return;
  292. }
  293. //
  294. // Free all the route table entry and its route cache.
  295. //
  296. for (Index = 0; Index <= IP6_PREFIX_MAX; Index++) {
  297. NET_LIST_FOR_EACH_SAFE (Entry, Next, &RtTable->RouteArea[Index]) {
  298. RtEntry = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_ENTRY, Link);
  299. RemoveEntryList (Entry);
  300. Ip6FreeRouteEntry (RtEntry);
  301. }
  302. }
  303. for (Index = 0; Index < IP6_ROUTE_CACHE_HASH_SIZE; Index++) {
  304. NET_LIST_FOR_EACH_SAFE (Entry, Next, &RtTable->Cache.CacheBucket[Index]) {
  305. RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_CACHE_ENTRY, Link);
  306. RemoveEntryList (Entry);
  307. Ip6FreeRouteCacheEntry (RtCacheEntry);
  308. }
  309. }
  310. FreePool (RtTable);
  311. }
  312. /**
  313. Remove all the cache entries bearing the Tag. When a route cache
  314. entry is created, it is tagged with the address of route entry
  315. from which it is spawned. When a route entry is deleted, the cache
  316. entries spawned from it are also deleted.
  317. @param[in] RtCache Route cache to remove the entries from.
  318. @param[in] Tag The Tag of the entries to remove.
  319. **/
  320. VOID
  321. Ip6PurgeRouteCache (
  322. IN IP6_ROUTE_CACHE *RtCache,
  323. IN UINTN Tag
  324. )
  325. {
  326. LIST_ENTRY *Entry;
  327. LIST_ENTRY *Next;
  328. IP6_ROUTE_CACHE_ENTRY *RtCacheEntry;
  329. UINT32 Index;
  330. for (Index = 0; Index < IP6_ROUTE_CACHE_HASH_SIZE; Index++) {
  331. NET_LIST_FOR_EACH_SAFE (Entry, Next, &RtCache->CacheBucket[Index]) {
  332. RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_CACHE_ENTRY, Link);
  333. if (RtCacheEntry->Tag == Tag) {
  334. RemoveEntryList (Entry);
  335. Ip6FreeRouteCacheEntry (RtCacheEntry);
  336. }
  337. }
  338. }
  339. }
  340. /**
  341. Add a route entry to the route table. It is the help function for EfiIp6Routes.
  342. @param[in, out] RtTable Route table to add route to.
  343. @param[in] Destination The destination of the network.
  344. @param[in] PrefixLength The PrefixLength of the destination.
  345. @param[in] GatewayAddress The next hop address.
  346. @retval EFI_ACCESS_DENIED The same route already exists.
  347. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the entry.
  348. @retval EFI_SUCCESS The route was added successfully.
  349. **/
  350. EFI_STATUS
  351. Ip6AddRoute (
  352. IN OUT IP6_ROUTE_TABLE *RtTable,
  353. IN EFI_IPv6_ADDRESS *Destination,
  354. IN UINT8 PrefixLength,
  355. IN EFI_IPv6_ADDRESS *GatewayAddress
  356. )
  357. {
  358. LIST_ENTRY *ListHead;
  359. LIST_ENTRY *Entry;
  360. IP6_ROUTE_ENTRY *Route;
  361. ListHead = &RtTable->RouteArea[PrefixLength];
  362. //
  363. // First check whether the route exists
  364. //
  365. NET_LIST_FOR_EACH (Entry, ListHead) {
  366. Route = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_ENTRY, Link);
  367. if (NetIp6IsNetEqual (Destination, &Route->Destination, PrefixLength) &&
  368. EFI_IP6_EQUAL (GatewayAddress, &Route->NextHop))
  369. {
  370. return EFI_ACCESS_DENIED;
  371. }
  372. }
  373. //
  374. // Create a route entry and insert it to the route area.
  375. //
  376. Route = Ip6CreateRouteEntry (Destination, PrefixLength, GatewayAddress);
  377. if (Route == NULL) {
  378. return EFI_OUT_OF_RESOURCES;
  379. }
  380. if (NetIp6IsUnspecifiedAddr (GatewayAddress)) {
  381. Route->Flag = IP6_DIRECT_ROUTE;
  382. }
  383. InsertHeadList (ListHead, &Route->Link);
  384. RtTable->TotalNum++;
  385. return EFI_SUCCESS;
  386. }
  387. /**
  388. Remove a route entry and all the route caches spawn from it.
  389. It is the help function for EfiIp6Routes.
  390. @param[in, out] RtTable The route table to remove the route from.
  391. @param[in] Destination The destination network.
  392. @param[in] PrefixLength The PrefixLength of the Destination.
  393. @param[in] GatewayAddress The next hop address.
  394. @retval EFI_SUCCESS The route entry was successfully removed.
  395. @retval EFI_NOT_FOUND There is no route entry in the table with that
  396. property.
  397. **/
  398. EFI_STATUS
  399. Ip6DelRoute (
  400. IN OUT IP6_ROUTE_TABLE *RtTable,
  401. IN EFI_IPv6_ADDRESS *Destination,
  402. IN UINT8 PrefixLength,
  403. IN EFI_IPv6_ADDRESS *GatewayAddress
  404. )
  405. {
  406. LIST_ENTRY *ListHead;
  407. LIST_ENTRY *Entry;
  408. LIST_ENTRY *Next;
  409. IP6_ROUTE_ENTRY *Route;
  410. UINT32 TotalNum;
  411. ListHead = &RtTable->RouteArea[PrefixLength];
  412. TotalNum = RtTable->TotalNum;
  413. NET_LIST_FOR_EACH_SAFE (Entry, Next, ListHead) {
  414. Route = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_ENTRY, Link);
  415. if ((Destination != NULL) && !NetIp6IsNetEqual (Destination, &Route->Destination, PrefixLength)) {
  416. continue;
  417. }
  418. if ((GatewayAddress != NULL) && !EFI_IP6_EQUAL (GatewayAddress, &Route->NextHop)) {
  419. continue;
  420. }
  421. Ip6PurgeRouteCache (&RtTable->Cache, (UINTN)Route);
  422. RemoveEntryList (Entry);
  423. Ip6FreeRouteEntry (Route);
  424. ASSERT (RtTable->TotalNum > 0);
  425. RtTable->TotalNum--;
  426. }
  427. return TotalNum == RtTable->TotalNum ? EFI_NOT_FOUND : EFI_SUCCESS;
  428. }
  429. /**
  430. Search the route table to route the packet. Return/create a route
  431. cache if there is a route to the destination.
  432. @param[in] IpSb The IP6 service data.
  433. @param[in] Dest The destination address to search for.
  434. @param[in] Src The source address to search for.
  435. @return NULL if it failed to route the packet. Otherwise, a route cache
  436. entry that can be used to route packets.
  437. **/
  438. IP6_ROUTE_CACHE_ENTRY *
  439. Ip6Route (
  440. IN IP6_SERVICE *IpSb,
  441. IN EFI_IPv6_ADDRESS *Dest,
  442. IN EFI_IPv6_ADDRESS *Src
  443. )
  444. {
  445. IP6_ROUTE_TABLE *RtTable;
  446. LIST_ENTRY *ListHead;
  447. IP6_ROUTE_CACHE_ENTRY *RtCacheEntry;
  448. IP6_ROUTE_ENTRY *RtEntry;
  449. EFI_IPv6_ADDRESS NextHop;
  450. UINT32 Index;
  451. RtTable = IpSb->RouteTable;
  452. ASSERT (RtTable != NULL);
  453. //
  454. // Search the destination cache in IP6_ROUTE_TABLE.
  455. //
  456. Index = IP6_ROUTE_CACHE_HASH (Dest, Src);
  457. ListHead = &RtTable->Cache.CacheBucket[Index];
  458. RtCacheEntry = Ip6FindRouteCache (RtTable, Dest, Src);
  459. //
  460. // If found, promote the cache entry to the head of the hash bucket.
  461. //
  462. if (RtCacheEntry != NULL) {
  463. RemoveEntryList (&RtCacheEntry->Link);
  464. InsertHeadList (ListHead, &RtCacheEntry->Link);
  465. return RtCacheEntry;
  466. }
  467. //
  468. // Search the route table for the most specific route
  469. //
  470. RtEntry = Ip6FindRouteEntry (RtTable, Dest, NULL);
  471. if (RtEntry == NULL) {
  472. return NULL;
  473. }
  474. //
  475. // Found a route to the Dest, if it is a direct route, the packet
  476. // will be send directly to the destination, such as for connected
  477. // network. Otherwise, it is an indirect route, the packet will be
  478. // send the next hop router.
  479. //
  480. if ((RtEntry->Flag & IP6_DIRECT_ROUTE) == IP6_DIRECT_ROUTE) {
  481. IP6_COPY_ADDRESS (&NextHop, Dest);
  482. } else {
  483. IP6_COPY_ADDRESS (&NextHop, &RtEntry->NextHop);
  484. }
  485. Ip6FreeRouteEntry (RtEntry);
  486. //
  487. // Create a route cache entry, and tag it as spawned from this route entry
  488. //
  489. RtCacheEntry = Ip6CreateRouteCacheEntry (Dest, Src, &NextHop, (UINTN)RtEntry);
  490. if (RtCacheEntry == NULL) {
  491. return NULL;
  492. }
  493. InsertHeadList (ListHead, &RtCacheEntry->Link);
  494. NET_GET_REF (RtCacheEntry);
  495. RtTable->Cache.CacheNum[Index]++;
  496. return RtCacheEntry;
  497. }