]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Route.c
MdeModulePkg: Remove trailing white space
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Route.c
CommitLineData
772db4bb 1/** @file\r
2\r
364f4efa 3Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 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
364f4efa 207 for (Index = 0; Index <= IP4_MASK_MAX; 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
364f4efa 244 for (Index = 0; Index <= IP4_MASK_MAX; Index++) {\r
772db4bb 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
364f4efa 472 for (Index = IP4_MASK_MAX; Index >= 0; Index--) {\r
772db4bb 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
12ae56cf
FS
497 @param[in] SubnetMask The subnet mask of the Src address, this field is\r
498 used to check if the station is using /32 subnet.\r
499 @param[in] AlwaysTryDestAddr Always try to use the dest address as next hop even\r
500 though we can't find a matching route entry. This\r
501 field is only valid when using /32 subnet.\r
772db4bb 502\r
503 @return NULL if failed to route packet, otherwise a route cache\r
96e1079f 504 entry that can be used to route packet.\r
772db4bb 505\r
506**/\r
507IP4_ROUTE_CACHE_ENTRY *\r
508Ip4Route (\r
509 IN IP4_ROUTE_TABLE *RtTable,\r
510 IN IP4_ADDR Dest,\r
12ae56cf
FS
511 IN IP4_ADDR Src,\r
512 IN IP4_ADDR SubnetMask,\r
513 IN BOOLEAN AlwaysTryDestAddr\r
772db4bb 514 )\r
515{\r
e48e37fc 516 LIST_ENTRY *Head;\r
517 LIST_ENTRY *Entry;\r
518 LIST_ENTRY *Next;\r
772db4bb 519 IP4_ROUTE_CACHE_ENTRY *RtCacheEntry;\r
520 IP4_ROUTE_CACHE_ENTRY *Cache;\r
521 IP4_ROUTE_ENTRY *RtEntry;\r
522 IP4_ADDR NextHop;\r
523 UINT32 Count;\r
524\r
525 ASSERT (RtTable != NULL);\r
526\r
527 Head = &RtTable->Cache.CacheBucket[IP4_ROUTE_CACHE_HASH (Dest, Src)];\r
528 RtCacheEntry = Ip4FindRouteCache (RtTable, Dest, Src);\r
529\r
530 //\r
531 // If found, promote the cache entry to the head of the hash bucket. LRU\r
532 //\r
533 if (RtCacheEntry != NULL) {\r
e48e37fc 534 RemoveEntryList (&RtCacheEntry->Link);\r
535 InsertHeadList (Head, &RtCacheEntry->Link);\r
772db4bb 536 return RtCacheEntry;\r
537 }\r
538\r
539 //\r
540 // Search the route table for the most specific route\r
541 //\r
542 RtEntry = Ip4FindRouteEntry (RtTable, Dest);\r
543\r
544 if (RtEntry == NULL) {\r
12ae56cf
FS
545 if (SubnetMask != IP4_ALLONE_ADDRESS) {\r
546 return NULL;\r
547 } else if (!AlwaysTryDestAddr) {\r
548 return NULL;\r
549 }\r
772db4bb 550 }\r
551\r
552 //\r
553 // Found a route to the Dest, if it is a direct route, the packet\r
96e1079f 554 // will be sent directly to the destination, such as for connected\r
772db4bb 555 // network. Otherwise, it is an indirect route, the packet will be\r
96e1079f 556 // sent to the next hop router.\r
772db4bb 557 //\r
12ae56cf
FS
558 // When using /32 subnet mask, the packet will always be sent to the direct\r
559 // destination first, if we can't find a matching route cache.\r
560 //\r
561 if (SubnetMask == IP4_ALLONE_ADDRESS || ((RtEntry->Flag & IP4_DIRECT_ROUTE) != 0)) {\r
772db4bb 562 NextHop = Dest;\r
563 } else {\r
564 NextHop = RtEntry->NextHop;\r
565 }\r
566\r
12ae56cf
FS
567 if (RtEntry != NULL) {\r
568 Ip4FreeRouteEntry (RtEntry);\r
569 }\r
772db4bb 570\r
571 //\r
572 // Create a route cache entry, and tag it as spawned from this route entry\r
12ae56cf
FS
573 // For /32 subnet mask, the default route in RtEntry will be used if failed\r
574 // to send the packet to driect destination address.\r
772db4bb 575 //\r
576 RtCacheEntry = Ip4CreateRouteCacheEntry (Dest, Src, NextHop, (UINTN) RtEntry);\r
577\r
578 if (RtCacheEntry == NULL) {\r
579 return NULL;\r
580 }\r
581\r
e48e37fc 582 InsertHeadList (Head, &RtCacheEntry->Link);\r
772db4bb 583 NET_GET_REF (RtCacheEntry);\r
584\r
585 //\r
586 // Each bucket of route cache can contain at most 64 entries.\r
587 // Remove the entries at the tail of the bucket. These entries\r
588 // are likely to be used least.\r
589 //\r
590 Count = 0;\r
591 NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {\r
592 if (++Count < IP4_ROUTE_CACHE_MAX) {\r
593 continue;\r
594 }\r
595\r
596 Cache = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_CACHE_ENTRY, Link);\r
597\r
e48e37fc 598 RemoveEntryList (Entry);\r
772db4bb 599 Ip4FreeRouteCacheEntry (Cache);\r
600 }\r
601\r
602 return RtCacheEntry;\r
603}\r
604\r
605\r
606/**\r
607 Build a EFI_IP4_ROUTE_TABLE to be returned to the caller of\r
608 GetModeData. The EFI_IP4_ROUTE_TABLE is clumsy to use in the\r
609 internal operation of the IP4 driver.\r
610\r
3e8c18da 611 @param[in] IpInstance The IP4 child that requests the route table.\r
772db4bb 612\r
613 @retval EFI_SUCCESS The route table is successfully build\r
614 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the rotue table.\r
615\r
616**/\r
617EFI_STATUS\r
618Ip4BuildEfiRouteTable (\r
619 IN IP4_PROTOCOL *IpInstance\r
620 )\r
621{\r
e48e37fc 622 LIST_ENTRY *Entry;\r
772db4bb 623 IP4_ROUTE_TABLE *RtTable;\r
624 IP4_ROUTE_ENTRY *RtEntry;\r
625 EFI_IP4_ROUTE_TABLE *Table;\r
626 UINT32 Count;\r
627 INT32 Index;\r
628\r
772db4bb 629 RtTable = IpInstance->RouteTable;\r
630\r
631 if (IpInstance->EfiRouteTable != NULL) {\r
766c7483 632 FreePool (IpInstance->EfiRouteTable);\r
772db4bb 633\r
634 IpInstance->EfiRouteTable = NULL;\r
635 IpInstance->EfiRouteCount = 0;\r
636 }\r
637\r
638 Count = RtTable->TotalNum;\r
639\r
640 if (RtTable->Next != NULL) {\r
641 Count += RtTable->Next->TotalNum;\r
642 }\r
643\r
644 if (Count == 0) {\r
645 return EFI_SUCCESS;\r
646 }\r
647\r
e48e37fc 648 Table = AllocatePool (sizeof (EFI_IP4_ROUTE_TABLE) * Count);\r
772db4bb 649\r
650 if (Table == NULL) {\r
651 return EFI_OUT_OF_RESOURCES;\r
652 }\r
653\r
654 //\r
655 // Copy the route entry to EFI route table. Keep the order of\r
656 // route entry copied from most specific to default route. That\r
657 // is, interlevel the route entry from the instance's route area\r
658 // and those from the default route table's route area.\r
659 //\r
660 Count = 0;\r
661\r
364f4efa 662 for (Index = IP4_MASK_MAX; Index >= 0; Index--) {\r
772db4bb 663 for (RtTable = IpInstance->RouteTable; RtTable != NULL; RtTable = RtTable->Next) {\r
664 NET_LIST_FOR_EACH (Entry, &(RtTable->RouteArea[Index])) {\r
665 RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);\r
666\r
667 EFI_IP4 (Table[Count].SubnetAddress) = HTONL (RtEntry->Dest & RtEntry->Netmask);\r
668 EFI_IP4 (Table[Count].SubnetMask) = HTONL (RtEntry->Netmask);\r
669 EFI_IP4 (Table[Count].GatewayAddress) = HTONL (RtEntry->NextHop);\r
670\r
671 Count++;\r
672 }\r
673 }\r
674 }\r
675\r
676 IpInstance->EfiRouteTable = Table;\r
677 IpInstance->EfiRouteCount = Count;\r
678 return EFI_SUCCESS;\r
679}\r