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