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