]>
Commit | Line | Data |
---|---|---|
772db4bb | 1 | /** @file\r |
2 | \r | |
3 | Copyright (c) 2005 - 2006, Intel Corporation\r | |
4 | All rights reserved. This program and the accompanying materials\r | |
5 | are licensed and made available under the terms and conditions of the BSD License\r | |
6 | which accompanies this distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | \r | |
13 | Module Name:\r | |
14 | \r | |
15 | Ip4Route.c\r | |
16 | \r | |
17 | Abstract:\r | |
18 | \r | |
19 | \r | |
20 | **/\r | |
21 | \r | |
22 | #include "Ip4Impl.h"\r | |
23 | \r | |
24 | \r | |
25 | /**\r | |
26 | Allocate a route entry then initialize it with the Dest/Netmaks\r | |
27 | and Gateway.\r | |
28 | \r | |
29 | @param Dest The destination network\r | |
30 | @param Netmask The destination network mask\r | |
31 | @param GateWay The nexthop address\r | |
32 | \r | |
33 | @return NULL if failed to allocate memeory, otherwise the newly created\r | |
34 | @return route entry.\r | |
35 | \r | |
36 | **/\r | |
37 | STATIC\r | |
38 | IP4_ROUTE_ENTRY *\r | |
39 | Ip4CreateRouteEntry (\r | |
40 | IN IP4_ADDR Dest,\r | |
41 | IN IP4_ADDR Netmask,\r | |
42 | IN IP4_ADDR GateWay\r | |
43 | )\r | |
44 | {\r | |
45 | IP4_ROUTE_ENTRY *RtEntry;\r | |
46 | \r | |
e48e37fc | 47 | RtEntry = AllocatePool (sizeof (IP4_ROUTE_ENTRY));\r |
772db4bb | 48 | \r |
49 | if (RtEntry == NULL) {\r | |
50 | return NULL;\r | |
51 | }\r | |
52 | \r | |
e48e37fc | 53 | InitializeListHead (&RtEntry->Link);\r |
772db4bb | 54 | \r |
55 | RtEntry->RefCnt = 1;\r | |
56 | RtEntry->Dest = Dest;\r | |
57 | RtEntry->Netmask = Netmask;\r | |
58 | RtEntry->NextHop = GateWay;\r | |
59 | RtEntry->Flag = 0;\r | |
60 | \r | |
61 | return RtEntry;\r | |
62 | }\r | |
63 | \r | |
64 | \r | |
65 | /**\r | |
66 | Free the route table entry. It is reference counted.\r | |
67 | \r | |
68 | @param RtEntry The route entry to free.\r | |
69 | \r | |
70 | @return NONE\r | |
71 | \r | |
72 | **/\r | |
73 | STATIC\r | |
74 | VOID\r | |
75 | Ip4FreeRouteEntry (\r | |
76 | IN IP4_ROUTE_ENTRY *RtEntry\r | |
77 | )\r | |
78 | {\r | |
79 | ASSERT (RtEntry->RefCnt > 0);\r | |
80 | \r | |
81 | if (--RtEntry->RefCnt == 0) {\r | |
e48e37fc | 82 | gBS->FreePool (RtEntry);\r |
772db4bb | 83 | }\r |
84 | }\r | |
85 | \r | |
86 | \r | |
87 | /**\r | |
88 | Allocate and initialize a IP4 route cache entry.\r | |
89 | \r | |
90 | @param Dst The destination address\r | |
91 | @param Src The source address\r | |
92 | @param GateWay The next hop address\r | |
93 | @param Tag The tag from the caller. This marks all the cache\r | |
94 | entries spawned from one route table entry.\r | |
95 | \r | |
96 | @return NULL if failed to allocate memory for the cache, other point\r | |
97 | @return to the created route cache entry.\r | |
98 | \r | |
99 | **/\r | |
100 | STATIC\r | |
101 | IP4_ROUTE_CACHE_ENTRY *\r | |
102 | Ip4CreateRouteCacheEntry (\r | |
103 | IN IP4_ADDR Dst,\r | |
104 | IN IP4_ADDR Src,\r | |
105 | IN IP4_ADDR GateWay,\r | |
106 | IN UINTN Tag\r | |
107 | )\r | |
108 | {\r | |
109 | IP4_ROUTE_CACHE_ENTRY *RtCacheEntry;\r | |
110 | \r | |
e48e37fc | 111 | RtCacheEntry = AllocatePool (sizeof (IP4_ROUTE_CACHE_ENTRY));\r |
772db4bb | 112 | \r |
113 | if (RtCacheEntry == NULL) {\r | |
114 | return NULL;\r | |
115 | }\r | |
116 | \r | |
e48e37fc | 117 | InitializeListHead (&RtCacheEntry->Link);\r |
772db4bb | 118 | \r |
119 | RtCacheEntry->RefCnt = 1;\r | |
120 | RtCacheEntry->Dest = Dst;\r | |
121 | RtCacheEntry->Src = Src;\r | |
122 | RtCacheEntry->NextHop = GateWay;\r | |
123 | RtCacheEntry->Tag = Tag;\r | |
124 | \r | |
125 | return RtCacheEntry;\r | |
126 | }\r | |
127 | \r | |
128 | \r | |
129 | /**\r | |
130 | Free the route cache entry. It is reference counted.\r | |
131 | \r | |
132 | @param RtCacheEntry The route cache entry to free.\r | |
133 | \r | |
134 | @return None\r | |
135 | \r | |
136 | **/\r | |
137 | VOID\r | |
138 | Ip4FreeRouteCacheEntry (\r | |
139 | IN IP4_ROUTE_CACHE_ENTRY *RtCacheEntry\r | |
140 | )\r | |
141 | {\r | |
142 | ASSERT (RtCacheEntry->RefCnt > 0);\r | |
143 | \r | |
144 | if (--RtCacheEntry->RefCnt == 0) {\r | |
e48e37fc | 145 | gBS->FreePool (RtCacheEntry);\r |
772db4bb | 146 | }\r |
147 | }\r | |
148 | \r | |
149 | \r | |
150 | /**\r | |
151 | Initialize an empty route cache table.\r | |
152 | \r | |
153 | @param RtCache The rotue cache table to initialize.\r | |
154 | \r | |
155 | @return NONE\r | |
156 | \r | |
157 | **/\r | |
158 | VOID\r | |
159 | Ip4InitRouteCache (\r | |
160 | IN IP4_ROUTE_CACHE *RtCache\r | |
161 | )\r | |
162 | {\r | |
163 | UINT32 Index;\r | |
164 | \r | |
165 | for (Index = 0; Index < IP4_ROUTE_CACHE_HASH; Index++) {\r | |
e48e37fc | 166 | InitializeListHead (&(RtCache->CacheBucket[Index]));\r |
772db4bb | 167 | }\r |
168 | }\r | |
169 | \r | |
170 | \r | |
171 | /**\r | |
172 | Clean up a route cache, that is free all the route cache\r | |
173 | entries enqueued in the cache.\r | |
174 | \r | |
175 | @param RtCache The route cache table to clean up\r | |
176 | \r | |
177 | @return None\r | |
178 | \r | |
179 | **/\r | |
180 | VOID\r | |
181 | Ip4CleanRouteCache (\r | |
182 | IN IP4_ROUTE_CACHE *RtCache\r | |
183 | )\r | |
184 | {\r | |
e48e37fc | 185 | LIST_ENTRY *Entry;\r |
186 | LIST_ENTRY *Next;\r | |
772db4bb | 187 | IP4_ROUTE_CACHE_ENTRY *RtCacheEntry;\r |
188 | UINT32 Index;\r | |
189 | \r | |
190 | for (Index = 0; Index < IP4_ROUTE_CACHE_HASH; Index++) {\r | |
191 | NET_LIST_FOR_EACH_SAFE (Entry, Next, &(RtCache->CacheBucket[Index])) {\r | |
192 | RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_CACHE_ENTRY, Link);\r | |
193 | \r | |
e48e37fc | 194 | RemoveEntryList (Entry);\r |
772db4bb | 195 | Ip4FreeRouteCacheEntry (RtCacheEntry);\r |
196 | }\r | |
197 | }\r | |
198 | }\r | |
199 | \r | |
200 | \r | |
201 | \r | |
202 | /**\r | |
203 | Create an empty route table, includes its internal route cache\r | |
204 | \r | |
205 | None\r | |
206 | \r | |
207 | @return NULL if failed to allocate memory for the route table, otherwise\r | |
208 | @return the point to newly created route table.\r | |
209 | \r | |
210 | **/\r | |
211 | IP4_ROUTE_TABLE *\r | |
212 | Ip4CreateRouteTable (\r | |
213 | VOID\r | |
214 | )\r | |
215 | {\r | |
216 | IP4_ROUTE_TABLE *RtTable;\r | |
217 | UINT32 Index;\r | |
218 | \r | |
e48e37fc | 219 | RtTable = AllocatePool (sizeof (IP4_ROUTE_TABLE));\r |
772db4bb | 220 | \r |
221 | if (RtTable == NULL) {\r | |
222 | return NULL;\r | |
223 | }\r | |
224 | \r | |
225 | RtTable->RefCnt = 1;\r | |
226 | RtTable->TotalNum = 0;\r | |
227 | \r | |
228 | for (Index = 0; Index < IP4_MASK_NUM; Index++) {\r | |
e48e37fc | 229 | InitializeListHead (&(RtTable->RouteArea[Index]));\r |
772db4bb | 230 | }\r |
231 | \r | |
232 | RtTable->Next = NULL;\r | |
233 | \r | |
234 | Ip4InitRouteCache (&RtTable->Cache);\r | |
235 | return RtTable;\r | |
236 | }\r | |
237 | \r | |
238 | \r | |
239 | /**\r | |
240 | Free the route table and its associated route cache. Route\r | |
241 | table is reference counted.\r | |
242 | \r | |
243 | @param RtTable The route table to free.\r | |
244 | \r | |
245 | @return None\r | |
246 | \r | |
247 | **/\r | |
248 | VOID\r | |
249 | Ip4FreeRouteTable (\r | |
250 | IN IP4_ROUTE_TABLE *RtTable\r | |
251 | )\r | |
252 | {\r | |
e48e37fc | 253 | LIST_ENTRY *Entry;\r |
254 | LIST_ENTRY *Next;\r | |
772db4bb | 255 | IP4_ROUTE_ENTRY *RtEntry;\r |
256 | UINT32 Index;\r | |
257 | \r | |
258 | ASSERT (RtTable->RefCnt > 0);\r | |
259 | \r | |
260 | if (--RtTable->RefCnt > 0) {\r | |
261 | return ;\r | |
262 | }\r | |
263 | \r | |
264 | //\r | |
265 | // Free all the route table entry and its route cache.\r | |
266 | //\r | |
267 | for (Index = 0; Index < IP4_MASK_NUM; Index++) {\r | |
268 | NET_LIST_FOR_EACH_SAFE (Entry, Next, &(RtTable->RouteArea[Index])) {\r | |
269 | RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);\r | |
270 | \r | |
e48e37fc | 271 | RemoveEntryList (Entry);\r |
772db4bb | 272 | Ip4FreeRouteEntry (RtEntry);\r |
273 | }\r | |
274 | }\r | |
275 | \r | |
276 | Ip4CleanRouteCache (&RtTable->Cache);\r | |
277 | \r | |
e48e37fc | 278 | gBS->FreePool (RtTable);\r |
772db4bb | 279 | }\r |
280 | \r | |
281 | \r | |
282 | \r | |
283 | /**\r | |
284 | Remove all the cache entries bearing the Tag. When a route cache\r | |
285 | entry is created, it is tagged with the address of route entry\r | |
286 | from which it is spawned. When a route entry is deleted, the cache\r | |
287 | entries spawned from it are also deleted.\r | |
288 | \r | |
289 | @param RtCache Route cache to remove the entries from\r | |
290 | @param Tag The Tag of the entries to remove\r | |
291 | \r | |
292 | @return None\r | |
293 | \r | |
294 | **/\r | |
295 | STATIC\r | |
296 | VOID\r | |
297 | Ip4PurgeRouteCache (\r | |
298 | IN IP4_ROUTE_CACHE *RtCache,\r | |
299 | IN UINTN Tag\r | |
300 | )\r | |
301 | {\r | |
e48e37fc | 302 | LIST_ENTRY *Entry;\r |
303 | LIST_ENTRY *Next;\r | |
772db4bb | 304 | IP4_ROUTE_CACHE_ENTRY *RtCacheEntry;\r |
305 | UINT32 Index;\r | |
306 | \r | |
307 | for (Index = 0; Index < IP4_ROUTE_CACHE_HASH; Index++) {\r | |
308 | NET_LIST_FOR_EACH_SAFE (Entry, Next, &RtCache->CacheBucket[Index]) {\r | |
309 | \r | |
310 | RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_CACHE_ENTRY, Link);\r | |
311 | \r | |
312 | if (RtCacheEntry->Tag == Tag) {\r | |
e48e37fc | 313 | RemoveEntryList (Entry);\r |
772db4bb | 314 | Ip4FreeRouteCacheEntry (RtCacheEntry);\r |
315 | }\r | |
316 | }\r | |
317 | }\r | |
318 | }\r | |
319 | \r | |
320 | \r | |
321 | /**\r | |
322 | Add a route entry to the route table. All the IP4_ADDRs are in\r | |
323 | host byte order.\r | |
324 | \r | |
325 | @param RtTable Route table to add route to\r | |
326 | @param Dest The destination of the network\r | |
327 | @param Netmask The netmask of the destination\r | |
328 | @param Gateway The next hop address\r | |
329 | \r | |
330 | @retval EFI_ACCESS_DENIED The same route already exists\r | |
331 | @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the entry\r | |
332 | @retval EFI_SUCCESS The route is added successfully.\r | |
333 | \r | |
334 | **/\r | |
335 | EFI_STATUS\r | |
336 | Ip4AddRoute (\r | |
337 | IN IP4_ROUTE_TABLE *RtTable,\r | |
338 | IN IP4_ADDR Dest,\r | |
339 | IN IP4_ADDR Netmask,\r | |
340 | IN IP4_ADDR Gateway\r | |
341 | )\r | |
342 | {\r | |
e48e37fc | 343 | LIST_ENTRY *Head;\r |
344 | LIST_ENTRY *Entry;\r | |
772db4bb | 345 | IP4_ROUTE_ENTRY *RtEntry;\r |
346 | \r | |
347 | //\r | |
348 | // All the route entries with the same netmask length are\r | |
349 | // linke to the same route area\r | |
350 | //\r | |
351 | Head = &(RtTable->RouteArea[NetGetMaskLength (Netmask)]);\r | |
352 | \r | |
353 | //\r | |
354 | // First check whether the route exists\r | |
355 | //\r | |
356 | NET_LIST_FOR_EACH (Entry, Head) {\r | |
357 | RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);\r | |
358 | \r | |
359 | if (IP4_NET_EQUAL (RtEntry->Dest, Dest, Netmask) && (RtEntry->NextHop == Gateway)) {\r | |
360 | return EFI_ACCESS_DENIED;\r | |
361 | }\r | |
362 | }\r | |
363 | \r | |
364 | //\r | |
365 | // Create a route entry and insert it to the route area.\r | |
366 | //\r | |
367 | RtEntry = Ip4CreateRouteEntry (Dest, Netmask, Gateway);\r | |
368 | \r | |
369 | if (RtEntry == NULL) {\r | |
370 | return EFI_OUT_OF_RESOURCES;\r | |
371 | }\r | |
372 | \r | |
373 | if (Gateway == IP4_ALLZERO_ADDRESS) {\r | |
374 | RtEntry->Flag = IP4_DIRECT_ROUTE;\r | |
375 | }\r | |
376 | \r | |
e48e37fc | 377 | InsertHeadList (Head, &RtEntry->Link);\r |
772db4bb | 378 | RtTable->TotalNum++;\r |
379 | \r | |
380 | return EFI_SUCCESS;\r | |
381 | }\r | |
382 | \r | |
383 | \r | |
384 | /**\r | |
385 | Remove a route entry and all the route caches spawn from it.\r | |
386 | \r | |
387 | @param RtTable The route table to remove the route from\r | |
388 | @param Dest The destination network\r | |
389 | @param Netmask The netmask of the Dest\r | |
390 | @param Gateway The next hop address\r | |
391 | \r | |
392 | @retval EFI_SUCCESS The route entry is successfully removed\r | |
393 | @retval EFI_NOT_FOUND There is no route entry in the table with that\r | |
394 | properity.\r | |
395 | \r | |
396 | **/\r | |
397 | EFI_STATUS\r | |
398 | Ip4DelRoute (\r | |
399 | IN IP4_ROUTE_TABLE *RtTable,\r | |
400 | IN IP4_ADDR Dest,\r | |
401 | IN IP4_ADDR Netmask,\r | |
402 | IN IP4_ADDR Gateway\r | |
403 | )\r | |
404 | {\r | |
e48e37fc | 405 | LIST_ENTRY *Head;\r |
406 | LIST_ENTRY *Entry;\r | |
407 | LIST_ENTRY *Next;\r | |
772db4bb | 408 | IP4_ROUTE_ENTRY *RtEntry;\r |
409 | \r | |
410 | Head = &(RtTable->RouteArea[NetGetMaskLength (Netmask)]);\r | |
411 | \r | |
412 | NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {\r | |
413 | RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);\r | |
414 | \r | |
415 | if (IP4_NET_EQUAL (RtEntry->Dest, Dest, Netmask) && (RtEntry->NextHop == Gateway)) {\r | |
416 | Ip4PurgeRouteCache (&RtTable->Cache, (UINTN) RtEntry);\r | |
e48e37fc | 417 | RemoveEntryList (Entry);\r |
772db4bb | 418 | Ip4FreeRouteEntry (RtEntry);\r |
419 | \r | |
420 | RtTable->TotalNum--;\r | |
421 | return EFI_SUCCESS;\r | |
422 | }\r | |
423 | }\r | |
424 | \r | |
425 | return EFI_NOT_FOUND;\r | |
426 | }\r | |
427 | \r | |
428 | \r | |
429 | /**\r | |
430 | Find a route cache with the dst and src. This is used by ICMP\r | |
431 | redirect messasge process. All kinds of redirect is treated as\r | |
432 | host redirect according to RFC1122. So, only route cache entries\r | |
433 | are modified according to the ICMP redirect message.\r | |
434 | \r | |
435 | @param RtTable The route table to search the cache for\r | |
436 | @param Dest The destination address\r | |
437 | @param Src The source address\r | |
438 | \r | |
439 | @return NULL if no route entry to the (Dest, Src). Otherwise the point\r | |
440 | @return to the correct route cache entry.\r | |
441 | \r | |
442 | **/\r | |
443 | IP4_ROUTE_CACHE_ENTRY *\r | |
444 | Ip4FindRouteCache (\r | |
445 | IN IP4_ROUTE_TABLE *RtTable,\r | |
446 | IN IP4_ADDR Dest,\r | |
447 | IN IP4_ADDR Src\r | |
448 | )\r | |
449 | {\r | |
e48e37fc | 450 | LIST_ENTRY *Entry;\r |
772db4bb | 451 | IP4_ROUTE_CACHE_ENTRY *RtCacheEntry;\r |
452 | UINT32 Index;\r | |
453 | \r | |
454 | Index = IP4_ROUTE_CACHE_HASH (Dest, Src);\r | |
455 | \r | |
456 | NET_LIST_FOR_EACH (Entry, &RtTable->Cache.CacheBucket[Index]) {\r | |
457 | RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_CACHE_ENTRY, Link);\r | |
458 | \r | |
459 | if ((RtCacheEntry->Dest == Dest) && (RtCacheEntry->Src == Src)) {\r | |
460 | NET_GET_REF (RtCacheEntry);\r | |
461 | return RtCacheEntry;\r | |
462 | }\r | |
463 | }\r | |
464 | \r | |
465 | return NULL;\r | |
466 | }\r | |
467 | \r | |
468 | \r | |
469 | /**\r | |
470 | Search the route table for a most specific match to the Dst. It searches\r | |
471 | from the longest route area (mask length == 32) to the shortest route area (\r | |
472 | default routes). In each route area, it will first search the instance's\r | |
473 | route table, then the default route table. This is required by the following\r | |
474 | requirements:\r | |
475 | 1. IP search the route table for a most specific match\r | |
476 | 2. The local route entries have precedence over the default route entry.\r | |
477 | \r | |
478 | @param RtTable The route table to search from\r | |
479 | @param Dst The destionation address to search\r | |
480 | \r | |
481 | @return NULL if no route matches the Dst, otherwise the point to the\r | |
482 | @return most specific route to the Dst.\r | |
483 | \r | |
484 | **/\r | |
485 | STATIC\r | |
486 | IP4_ROUTE_ENTRY *\r | |
487 | Ip4FindRouteEntry (\r | |
488 | IN IP4_ROUTE_TABLE *RtTable,\r | |
489 | IN IP4_ADDR Dst\r | |
490 | )\r | |
491 | {\r | |
e48e37fc | 492 | LIST_ENTRY *Entry;\r |
772db4bb | 493 | IP4_ROUTE_ENTRY *RtEntry;\r |
494 | IP4_ROUTE_TABLE *Table;\r | |
495 | INTN Index;\r | |
496 | \r | |
497 | RtEntry = NULL;\r | |
498 | \r | |
499 | for (Index = IP4_MASK_NUM - 1; Index >= 0; Index--) {\r | |
500 | for (Table = RtTable; Table != NULL; Table = Table->Next) {\r | |
501 | NET_LIST_FOR_EACH (Entry, &Table->RouteArea[Index]) {\r | |
502 | RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);\r | |
503 | \r | |
504 | if (IP4_NET_EQUAL (RtEntry->Dest, Dst, RtEntry->Netmask)) {\r | |
505 | NET_GET_REF (RtEntry);\r | |
506 | return RtEntry;\r | |
507 | }\r | |
508 | }\r | |
509 | }\r | |
510 | }\r | |
511 | \r | |
512 | \r | |
513 | return NULL;\r | |
514 | }\r | |
515 | \r | |
516 | \r | |
517 | /**\r | |
518 | Search the route table to route the packet. Return/creat a route\r | |
519 | cache if there is a route to the destination.\r | |
520 | \r | |
521 | @param RtTable The route table to search from\r | |
522 | @param Dest The destination address to search for\r | |
523 | @param Src The source address to search for\r | |
524 | \r | |
525 | @return NULL if failed to route packet, otherwise a route cache\r | |
526 | @return entry that can be used to route packet.\r | |
527 | \r | |
528 | **/\r | |
529 | IP4_ROUTE_CACHE_ENTRY *\r | |
530 | Ip4Route (\r | |
531 | IN IP4_ROUTE_TABLE *RtTable,\r | |
532 | IN IP4_ADDR Dest,\r | |
533 | IN IP4_ADDR Src\r | |
534 | )\r | |
535 | {\r | |
e48e37fc | 536 | LIST_ENTRY *Head;\r |
537 | LIST_ENTRY *Entry;\r | |
538 | LIST_ENTRY *Next;\r | |
772db4bb | 539 | IP4_ROUTE_CACHE_ENTRY *RtCacheEntry;\r |
540 | IP4_ROUTE_CACHE_ENTRY *Cache;\r | |
541 | IP4_ROUTE_ENTRY *RtEntry;\r | |
542 | IP4_ADDR NextHop;\r | |
543 | UINT32 Count;\r | |
544 | \r | |
545 | ASSERT (RtTable != NULL);\r | |
546 | \r | |
547 | Head = &RtTable->Cache.CacheBucket[IP4_ROUTE_CACHE_HASH (Dest, Src)];\r | |
548 | RtCacheEntry = Ip4FindRouteCache (RtTable, Dest, Src);\r | |
549 | \r | |
550 | //\r | |
551 | // If found, promote the cache entry to the head of the hash bucket. LRU\r | |
552 | //\r | |
553 | if (RtCacheEntry != NULL) {\r | |
e48e37fc | 554 | RemoveEntryList (&RtCacheEntry->Link);\r |
555 | InsertHeadList (Head, &RtCacheEntry->Link);\r | |
772db4bb | 556 | return RtCacheEntry;\r |
557 | }\r | |
558 | \r | |
559 | //\r | |
560 | // Search the route table for the most specific route\r | |
561 | //\r | |
562 | RtEntry = Ip4FindRouteEntry (RtTable, Dest);\r | |
563 | \r | |
564 | if (RtEntry == NULL) {\r | |
565 | return NULL;\r | |
566 | }\r | |
567 | \r | |
568 | //\r | |
569 | // Found a route to the Dest, if it is a direct route, the packet\r | |
570 | // will be send directly to the destination, such as for connected\r | |
571 | // network. Otherwise, it is an indirect route, the packet will be\r | |
572 | // send the next hop router.\r | |
573 | //\r | |
574 | if (RtEntry->Flag & IP4_DIRECT_ROUTE) {\r | |
575 | NextHop = Dest;\r | |
576 | } else {\r | |
577 | NextHop = RtEntry->NextHop;\r | |
578 | }\r | |
579 | \r | |
580 | Ip4FreeRouteEntry (RtEntry);\r | |
581 | \r | |
582 | //\r | |
583 | // Create a route cache entry, and tag it as spawned from this route entry\r | |
584 | //\r | |
585 | RtCacheEntry = Ip4CreateRouteCacheEntry (Dest, Src, NextHop, (UINTN) RtEntry);\r | |
586 | \r | |
587 | if (RtCacheEntry == NULL) {\r | |
588 | return NULL;\r | |
589 | }\r | |
590 | \r | |
e48e37fc | 591 | InsertHeadList (Head, &RtCacheEntry->Link);\r |
772db4bb | 592 | NET_GET_REF (RtCacheEntry);\r |
593 | \r | |
594 | //\r | |
595 | // Each bucket of route cache can contain at most 64 entries.\r | |
596 | // Remove the entries at the tail of the bucket. These entries\r | |
597 | // are likely to be used least.\r | |
598 | //\r | |
599 | Count = 0;\r | |
600 | NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {\r | |
601 | if (++Count < IP4_ROUTE_CACHE_MAX) {\r | |
602 | continue;\r | |
603 | }\r | |
604 | \r | |
605 | Cache = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_CACHE_ENTRY, Link);\r | |
606 | \r | |
e48e37fc | 607 | RemoveEntryList (Entry);\r |
772db4bb | 608 | Ip4FreeRouteCacheEntry (Cache);\r |
609 | }\r | |
610 | \r | |
611 | return RtCacheEntry;\r | |
612 | }\r | |
613 | \r | |
614 | \r | |
615 | /**\r | |
616 | Build a EFI_IP4_ROUTE_TABLE to be returned to the caller of\r | |
617 | GetModeData. The EFI_IP4_ROUTE_TABLE is clumsy to use in the\r | |
618 | internal operation of the IP4 driver.\r | |
619 | \r | |
620 | @param IpInstance The IP4 child that requests the route table.\r | |
621 | \r | |
622 | @retval EFI_SUCCESS The route table is successfully build\r | |
623 | @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the rotue table.\r | |
624 | \r | |
625 | **/\r | |
626 | EFI_STATUS\r | |
627 | Ip4BuildEfiRouteTable (\r | |
628 | IN IP4_PROTOCOL *IpInstance\r | |
629 | )\r | |
630 | {\r | |
e48e37fc | 631 | LIST_ENTRY *Entry;\r |
772db4bb | 632 | IP4_ROUTE_TABLE *RtTable;\r |
633 | IP4_ROUTE_ENTRY *RtEntry;\r | |
634 | EFI_IP4_ROUTE_TABLE *Table;\r | |
635 | UINT32 Count;\r | |
636 | INT32 Index;\r | |
637 | \r | |
772db4bb | 638 | RtTable = IpInstance->RouteTable;\r |
639 | \r | |
640 | if (IpInstance->EfiRouteTable != NULL) {\r | |
e48e37fc | 641 | gBS->FreePool (IpInstance->EfiRouteTable);\r |
772db4bb | 642 | \r |
643 | IpInstance->EfiRouteTable = NULL;\r | |
644 | IpInstance->EfiRouteCount = 0;\r | |
645 | }\r | |
646 | \r | |
647 | Count = RtTable->TotalNum;\r | |
648 | \r | |
649 | if (RtTable->Next != NULL) {\r | |
650 | Count += RtTable->Next->TotalNum;\r | |
651 | }\r | |
652 | \r | |
653 | if (Count == 0) {\r | |
654 | return EFI_SUCCESS;\r | |
655 | }\r | |
656 | \r | |
e48e37fc | 657 | Table = AllocatePool (sizeof (EFI_IP4_ROUTE_TABLE) * Count);\r |
772db4bb | 658 | \r |
659 | if (Table == NULL) {\r | |
660 | return EFI_OUT_OF_RESOURCES;\r | |
661 | }\r | |
662 | \r | |
663 | //\r | |
664 | // Copy the route entry to EFI route table. Keep the order of\r | |
665 | // route entry copied from most specific to default route. That\r | |
666 | // is, interlevel the route entry from the instance's route area\r | |
667 | // and those from the default route table's route area.\r | |
668 | //\r | |
669 | Count = 0;\r | |
670 | \r | |
671 | for (Index = IP4_MASK_NUM - 1; Index >= 0; Index--) {\r | |
672 | for (RtTable = IpInstance->RouteTable; RtTable != NULL; RtTable = RtTable->Next) {\r | |
673 | NET_LIST_FOR_EACH (Entry, &(RtTable->RouteArea[Index])) {\r | |
674 | RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);\r | |
675 | \r | |
676 | EFI_IP4 (Table[Count].SubnetAddress) = HTONL (RtEntry->Dest & RtEntry->Netmask);\r | |
677 | EFI_IP4 (Table[Count].SubnetMask) = HTONL (RtEntry->Netmask);\r | |
678 | EFI_IP4 (Table[Count].GatewayAddress) = HTONL (RtEntry->NextHop);\r | |
679 | \r | |
680 | Count++;\r | |
681 | }\r | |
682 | }\r | |
683 | }\r | |
684 | \r | |
685 | IpInstance->EfiRouteTable = Table;\r | |
686 | IpInstance->EfiRouteCount = Count;\r | |
687 | return EFI_SUCCESS;\r | |
688 | }\r |