Ip6Route.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /** @file
  2. EFI IP6 route table and route cache table definitions.
  3. Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #ifndef __EFI_IP6_ROUTE_H__
  7. #define __EFI_IP6_ROUTE_H__
  8. #define IP6_DIRECT_ROUTE 0x00000001
  9. #define IP6_PACKET_TOO_BIG 0x00000010
  10. #define IP6_ROUTE_CACHE_HASH_SIZE 31
  11. ///
  12. /// Max NO. of cache entry per hash bucket
  13. ///
  14. #define IP6_ROUTE_CACHE_MAX 32
  15. #define IP6_ROUTE_CACHE_HASH(Ip1, Ip2) Ip6RouteCacheHash ((Ip1), (Ip2))
  16. typedef struct {
  17. LIST_ENTRY Link;
  18. INTN RefCnt;
  19. UINT32 Flag;
  20. UINT8 PrefixLength;
  21. EFI_IPv6_ADDRESS Destination;
  22. EFI_IPv6_ADDRESS NextHop;
  23. } IP6_ROUTE_ENTRY;
  24. typedef struct {
  25. LIST_ENTRY Link;
  26. INTN RefCnt;
  27. UINTN Tag;
  28. EFI_IPv6_ADDRESS Destination;
  29. EFI_IPv6_ADDRESS Source;
  30. EFI_IPv6_ADDRESS NextHop;
  31. } IP6_ROUTE_CACHE_ENTRY;
  32. typedef struct {
  33. LIST_ENTRY CacheBucket[IP6_ROUTE_CACHE_HASH_SIZE];
  34. UINT8 CacheNum[IP6_ROUTE_CACHE_HASH_SIZE];
  35. } IP6_ROUTE_CACHE;
  36. //
  37. // Each IP6 instance has its own route table. Each ServiceBinding
  38. // instance has a default route table and default address.
  39. //
  40. // All the route table entries with the same prefix length are linked
  41. // together in one route area. For example, RouteArea[0] contains
  42. // the default routes. A route table also contains a route cache.
  43. //
  44. typedef struct _IP6_ROUTE_TABLE {
  45. INTN RefCnt;
  46. UINT32 TotalNum;
  47. LIST_ENTRY RouteArea[IP6_PREFIX_NUM];
  48. IP6_ROUTE_CACHE Cache;
  49. } IP6_ROUTE_TABLE;
  50. /**
  51. This is the worker function for IP6_ROUTE_CACHE_HASH(). It calculates the value
  52. as the index of the route cache bucket according to the prefix of two IPv6 addresses.
  53. @param[in] Ip1 The IPv6 address.
  54. @param[in] Ip2 The IPv6 address.
  55. @return The hash value of the prefix of two IPv6 addresses.
  56. **/
  57. UINT32
  58. Ip6RouteCacheHash (
  59. IN EFI_IPv6_ADDRESS *Ip1,
  60. IN EFI_IPv6_ADDRESS *Ip2
  61. );
  62. /**
  63. Allocate and initialize an IP6 route cache entry.
  64. @param[in] Dst The destination address.
  65. @param[in] Src The source address.
  66. @param[in] GateWay The next hop address.
  67. @param[in] Tag The tag from the caller. This marks all the cache entries
  68. spawned from one route table entry.
  69. @return NULL if it failed to allocate memory for the cache. Otherwise, point
  70. to the created route cache entry.
  71. **/
  72. IP6_ROUTE_CACHE_ENTRY *
  73. Ip6CreateRouteCacheEntry (
  74. IN EFI_IPv6_ADDRESS *Dst,
  75. IN EFI_IPv6_ADDRESS *Src,
  76. IN EFI_IPv6_ADDRESS *GateWay,
  77. IN UINTN Tag
  78. );
  79. /**
  80. Free the route cache entry. It is reference counted.
  81. @param[in, out] RtCacheEntry The route cache entry to free.
  82. **/
  83. VOID
  84. Ip6FreeRouteCacheEntry (
  85. IN OUT IP6_ROUTE_CACHE_ENTRY *RtCacheEntry
  86. );
  87. /**
  88. Find a route cache with the destination and source address. This is
  89. used by the ICMPv6 redirect message process.
  90. @param[in] RtTable The route table to search the cache for.
  91. @param[in] Dest The destination address.
  92. @param[in] Src The source address.
  93. @return NULL if no route entry to the (Dest, Src). Otherwise, point
  94. to the correct route cache entry.
  95. **/
  96. IP6_ROUTE_CACHE_ENTRY *
  97. Ip6FindRouteCache (
  98. IN IP6_ROUTE_TABLE *RtTable,
  99. IN EFI_IPv6_ADDRESS *Dest,
  100. IN EFI_IPv6_ADDRESS *Src
  101. );
  102. /**
  103. Build a array of EFI_IP6_ROUTE_TABLE to be returned to the caller. The number
  104. of EFI_IP6_ROUTE_TABLE is also returned.
  105. @param[in] RouteTable The pointer of IP6_ROUTE_TABLE internal used.
  106. @param[out] EfiRouteCount The number of returned route entries.
  107. @param[out] EfiRouteTable The pointer to the array of EFI_IP6_ROUTE_TABLE.
  108. If NULL, only the route entry count is returned.
  109. @retval EFI_SUCCESS The EFI_IP6_ROUTE_TABLE successfully built.
  110. @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the route table.
  111. **/
  112. EFI_STATUS
  113. Ip6BuildEfiRouteTable (
  114. IN IP6_ROUTE_TABLE *RouteTable,
  115. OUT UINT32 *EfiRouteCount,
  116. OUT EFI_IP6_ROUTE_TABLE **EfiRouteTable OPTIONAL
  117. );
  118. /**
  119. Create an empty route table, includes its internal route cache.
  120. @return NULL if failed to allocate memory for the route table. Otherwise,
  121. the point to newly created route table.
  122. **/
  123. IP6_ROUTE_TABLE *
  124. Ip6CreateRouteTable (
  125. VOID
  126. );
  127. /**
  128. Free the route table and its associated route cache. Route
  129. table is reference counted.
  130. @param[in, out] RtTable The route table to free.
  131. **/
  132. VOID
  133. Ip6CleanRouteTable (
  134. IN OUT IP6_ROUTE_TABLE *RtTable
  135. );
  136. /**
  137. Allocate a route entry then initialize it with the Destination/PrefixLength
  138. and Gateway.
  139. @param[in] Destination The IPv6 destination address. This is an optional
  140. parameter that may be NULL.
  141. @param[in] PrefixLength The destination network's prefix length.
  142. @param[in] GatewayAddress The next hop address. This is optional parameter
  143. that may be NULL.
  144. @return NULL if it failed to allocate memory. Otherwise, the newly created route entry.
  145. **/
  146. IP6_ROUTE_ENTRY *
  147. Ip6CreateRouteEntry (
  148. IN EFI_IPv6_ADDRESS *Destination OPTIONAL,
  149. IN UINT8 PrefixLength,
  150. IN EFI_IPv6_ADDRESS *GatewayAddress OPTIONAL
  151. );
  152. /**
  153. Search the route table for a most specific match to the Dst. It searches
  154. from the longest route area (prefix length == 128) to the shortest route area
  155. (default routes). In each route area, it will first search the instance's
  156. route table, then the default route table. This is required per the following
  157. requirements:
  158. 1. IP search the route table for a most specific match.
  159. 2. The local route entries have precedence over the default route entry.
  160. @param[in] RtTable The route table to search from.
  161. @param[in] Destination The destination address to search. If NULL, search
  162. the route table by NextHop.
  163. @param[in] NextHop The next hop address. If NULL, search the route table
  164. by Destination.
  165. @return NULL if no route matches the Dst. Otherwise the point to the
  166. most specific route to the Dst.
  167. **/
  168. IP6_ROUTE_ENTRY *
  169. Ip6FindRouteEntry (
  170. IN IP6_ROUTE_TABLE *RtTable,
  171. IN EFI_IPv6_ADDRESS *Destination OPTIONAL,
  172. IN EFI_IPv6_ADDRESS *NextHop OPTIONAL
  173. );
  174. /**
  175. Free the route table entry. It is reference counted.
  176. @param[in, out] RtEntry The route entry to free.
  177. **/
  178. VOID
  179. Ip6FreeRouteEntry (
  180. IN OUT IP6_ROUTE_ENTRY *RtEntry
  181. );
  182. /**
  183. Add a route entry to the route table. It is the help function for EfiIp6Routes.
  184. @param[in, out] RtTable Route table to add route to.
  185. @param[in] Destination The destination of the network.
  186. @param[in] PrefixLength The PrefixLength of the destination.
  187. @param[in] GatewayAddress The next hop address.
  188. @retval EFI_ACCESS_DENIED The same route already exists.
  189. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the entry.
  190. @retval EFI_SUCCESS The route was added successfully.
  191. **/
  192. EFI_STATUS
  193. Ip6AddRoute (
  194. IN OUT IP6_ROUTE_TABLE *RtTable,
  195. IN EFI_IPv6_ADDRESS *Destination,
  196. IN UINT8 PrefixLength,
  197. IN EFI_IPv6_ADDRESS *GatewayAddress
  198. );
  199. /**
  200. Remove a route entry and all the route caches spawn from it.
  201. It is the help function for EfiIp6Routes.
  202. @param[in, out] RtTable The route table to remove the route from.
  203. @param[in] Destination The destination network.
  204. @param[in] PrefixLength The PrefixLength of the Destination.
  205. @param[in] GatewayAddress The next hop address.
  206. @retval EFI_SUCCESS Successfully removed the route entry.
  207. @retval EFI_NOT_FOUND There is no route entry in the table with that
  208. property.
  209. **/
  210. EFI_STATUS
  211. Ip6DelRoute (
  212. IN OUT IP6_ROUTE_TABLE *RtTable,
  213. IN EFI_IPv6_ADDRESS *Destination,
  214. IN UINT8 PrefixLength,
  215. IN EFI_IPv6_ADDRESS *GatewayAddress
  216. );
  217. /**
  218. Search the route table to route the packet. Return/create a route
  219. cache if there is a route to the destination.
  220. @param[in] IpSb The IP6 service data.
  221. @param[in] Dest The destination address to search for.
  222. @param[in] Src The source address to search for.
  223. @return NULL if failed to route packet. Otherwise, a route cache
  224. entry that can be used to route packet.
  225. **/
  226. IP6_ROUTE_CACHE_ENTRY *
  227. Ip6Route (
  228. IN IP6_SERVICE *IpSb,
  229. IN EFI_IPv6_ADDRESS *Dest,
  230. IN EFI_IPv6_ADDRESS *Src
  231. );
  232. #endif