Ip4Route.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /** @file
  2. EFI IP4 route table and route cache table definitions.
  3. Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #ifndef __EFI_IP4_ROUTE_H__
  7. #define __EFI_IP4_ROUTE_H__
  8. #include "Ip4Common.h"
  9. #define IP4_DIRECT_ROUTE 0x00000001
  10. #define IP4_ROUTE_CACHE_HASH_VALUE 31
  11. #define IP4_ROUTE_CACHE_MAX 64 // Max NO. of cache entry per hash bucket
  12. #define IP4_ROUTE_CACHE_HASH(Dst, Src) (((Dst) ^ (Src)) % IP4_ROUTE_CACHE_HASH_VALUE)
  13. ///
  14. /// The route entry in the route table. Dest/Netmask is the destion
  15. /// network. The nexthop is the gateway to send the packet to in
  16. /// order to reach the Dest/Netmask. If the Flag has IP4_DIRECT_ROUTE
  17. /// on, the gateway is the destination of the IP packet itself. Route
  18. /// enties of the connected network have the flag on.
  19. ///
  20. typedef struct {
  21. LIST_ENTRY Link;
  22. INTN RefCnt;
  23. IP4_ADDR Dest;
  24. IP4_ADDR Netmask;
  25. IP4_ADDR NextHop;
  26. UINT32 Flag;
  27. } IP4_ROUTE_ENTRY;
  28. ///
  29. /// The route cache entry. The route cache entry is optional.
  30. /// But it is necessary to support the ICMP redirect message.
  31. /// Check Ip4ProcessIcmpRedirect for information.
  32. ///
  33. /// The cache entry field Tag is used to tag all the route
  34. /// cache entry spawned from a route table entry. This makes
  35. /// it simple to delete all the route cache entries from a
  36. /// to-be-deleted route entry.
  37. ///
  38. typedef struct {
  39. LIST_ENTRY Link;
  40. INTN RefCnt;
  41. IP4_ADDR Dest;
  42. IP4_ADDR Src;
  43. IP4_ADDR NextHop;
  44. UINTN Tag;
  45. } IP4_ROUTE_CACHE_ENTRY;
  46. ///
  47. /// The route cache table is organized as a hash table. Each
  48. /// IP4 route table has a embedded route cache. For now the
  49. /// route cache and route table are binded together. But keep
  50. /// the route cache a separated structure in case we want to
  51. /// detach them later.
  52. ///
  53. typedef struct {
  54. LIST_ENTRY CacheBucket[IP4_ROUTE_CACHE_HASH_VALUE];
  55. } IP4_ROUTE_CACHE;
  56. ///
  57. /// Each IP4 instance has its own route table. Each ServiceBinding
  58. /// instance has a default route table and default address.
  59. ///
  60. /// All the route table entries with the same mask are linked
  61. /// together in one route area. For example, RouteArea[0] contains
  62. /// the default routes. A route table also contains a route cache.
  63. ///
  64. typedef struct _IP4_ROUTE_TABLE IP4_ROUTE_TABLE;
  65. struct _IP4_ROUTE_TABLE {
  66. INTN RefCnt;
  67. UINT32 TotalNum;
  68. LIST_ENTRY RouteArea[IP4_MASK_NUM];
  69. IP4_ROUTE_TABLE *Next;
  70. IP4_ROUTE_CACHE Cache;
  71. };
  72. /**
  73. Create an empty route table, includes its internal route cache
  74. @return NULL if failed to allocate memory for the route table, otherwise
  75. the point to newly created route table.
  76. **/
  77. IP4_ROUTE_TABLE *
  78. Ip4CreateRouteTable (
  79. VOID
  80. );
  81. /**
  82. Free the route table and its associated route cache. Route
  83. table is reference counted.
  84. @param[in] RtTable The route table to free.
  85. **/
  86. VOID
  87. Ip4FreeRouteTable (
  88. IN IP4_ROUTE_TABLE *RtTable
  89. );
  90. /**
  91. Add a route entry to the route table. All the IP4_ADDRs are in
  92. host byte order.
  93. @param[in, out] RtTable Route table to add route to
  94. @param[in] Dest The destination of the network
  95. @param[in] Netmask The netmask of the destination
  96. @param[in] Gateway The next hop address
  97. @retval EFI_ACCESS_DENIED The same route already exists
  98. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the entry
  99. @retval EFI_SUCCESS The route is added successfully.
  100. **/
  101. EFI_STATUS
  102. Ip4AddRoute (
  103. IN OUT IP4_ROUTE_TABLE *RtTable,
  104. IN IP4_ADDR Dest,
  105. IN IP4_ADDR Netmask,
  106. IN IP4_ADDR Gateway
  107. );
  108. /**
  109. Remove a route entry and all the route caches spawn from it.
  110. @param RtTable The route table to remove the route from
  111. @param Dest The destination network
  112. @param Netmask The netmask of the Dest
  113. @param Gateway The next hop address
  114. @retval EFI_SUCCESS The route entry is successfully removed
  115. @retval EFI_NOT_FOUND There is no route entry in the table with that
  116. property.
  117. **/
  118. EFI_STATUS
  119. Ip4DelRoute (
  120. IN OUT IP4_ROUTE_TABLE *RtTable,
  121. IN IP4_ADDR Dest,
  122. IN IP4_ADDR Netmask,
  123. IN IP4_ADDR Gateway
  124. );
  125. /**
  126. Find a route cache with the dst and src. This is used by ICMP
  127. redirect message process. All kinds of redirect is treated as
  128. host redirect according to RFC1122. So, only route cache entries
  129. are modified according to the ICMP redirect message.
  130. @param[in] RtTable The route table to search the cache for
  131. @param[in] Dest The destination address
  132. @param[in] Src The source address
  133. @return NULL if no route entry to the (Dest, Src). Otherwise the point
  134. to the correct route cache entry.
  135. **/
  136. IP4_ROUTE_CACHE_ENTRY *
  137. Ip4FindRouteCache (
  138. IN IP4_ROUTE_TABLE *RtTable,
  139. IN IP4_ADDR Dest,
  140. IN IP4_ADDR Src
  141. );
  142. /**
  143. Free the route cache entry. It is reference counted.
  144. @param RtCacheEntry The route cache entry to free.
  145. **/
  146. VOID
  147. Ip4FreeRouteCacheEntry (
  148. IN IP4_ROUTE_CACHE_ENTRY *RtCacheEntry
  149. );
  150. /**
  151. Search the route table to route the packet. Return/create a route
  152. cache if there is a route to the destination.
  153. @param[in] RtTable The route table to search from
  154. @param[in] Dest The destination address to search for
  155. @param[in] Src The source address to search for
  156. @param[in] SubnetMask The subnet mask of the Src address, this field is
  157. used to check if the station is using /32 subnet.
  158. @param[in] AlwaysTryDestAddr Always try to use the dest address as next hop even
  159. though we can't find a matching route entry. This
  160. field is only valid when using /32 subnet.
  161. @return NULL if failed to route packet, otherwise a route cache
  162. entry that can be used to route packet.
  163. **/
  164. IP4_ROUTE_CACHE_ENTRY *
  165. Ip4Route (
  166. IN IP4_ROUTE_TABLE *RtTable,
  167. IN IP4_ADDR Dest,
  168. IN IP4_ADDR Src,
  169. IN IP4_ADDR SubnetMask,
  170. IN BOOLEAN AlwaysTryDestAddr
  171. );
  172. /**
  173. Build a EFI_IP4_ROUTE_TABLE to be returned to the caller of
  174. GetModeData. The EFI_IP4_ROUTE_TABLE is clumsy to use in the
  175. internal operation of the IP4 driver.
  176. @param[in] IpInstance The IP4 child that requests the route table.
  177. @retval EFI_SUCCESS The route table is successfully build
  178. @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the route table.
  179. **/
  180. EFI_STATUS
  181. Ip4BuildEfiRouteTable (
  182. IN IP4_PROTOCOL *IpInstance
  183. );
  184. #endif