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