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