Ip4Route.c revision 4fd606d1f5abe38e1f42c38de1d2e895166bd0f4
09249711700a6acbc40a2e337688b434d7aafa28Christian MaederCopyright (c) 2005 - 2009, Intel Corporation. All rights reserved.<BR>
76647324ed70f33b95a881b536d883daccf9568dChristian MaederThis program and the accompanying materials
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian Maederare licensed and made available under the terms and conditions of the BSD License
c00adad2e9459b422dee09e3a2bddba66b433bb7Christian Maederwhich accompanies this distribution. The full text of the license may be found at
3f69b6948966979163bdfe8331c38833d5d90ecdChristian Maederhttp://opensource.org/licenses/bsd-license.php
76647324ed70f33b95a881b536d883daccf9568dChristian MaederTHE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
f3a94a197960e548ecd6520bb768cb0d547457bbChristian MaederWITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
950e053ba55ac9c7d9c26a1ab48bd00202b29511Christian Maeder Allocate a route entry then initialize it with the Dest/Netmaks
0a39036fa485579a7b7c81cdd44a412392571927Christian Maeder @param[in] Dest The destination network
d48085f765fca838c1d972d2123601997174583dChristian Maeder @param[in] Netmask The destination network mask
5d7e4bf173534e7eb3fc84dce7bb0151079d3f8aChristian Maeder @param[in] GateWay The nexthop address
0a39036fa485579a7b7c81cdd44a412392571927Christian Maeder @return NULL if failed to allocate memeory, otherwise the newly created
d48085f765fca838c1d972d2123601997174583dChristian Maeder RtEntry = AllocatePool (sizeof (IP4_ROUTE_ENTRY));
f2c2b420e386a90d940c758c631d16f12952d2b7Christian Maeder Free the route table entry. It is reference counted.
f2c2b420e386a90d940c758c631d16f12952d2b7Christian Maeder @param RtEntry The route entry to free.
793945d4ac7c0f22760589c87af8e71427c76118Christian Maeder Allocate and initialize an IP4 route cache entry.
793945d4ac7c0f22760589c87af8e71427c76118Christian Maeder @param[in] Dst The destination address
793945d4ac7c0f22760589c87af8e71427c76118Christian Maeder @param[in] Src The source address
a716971174535184da7713ed308423e355a4aa66Christian Maeder @param[in] GateWay The next hop address
a716971174535184da7713ed308423e355a4aa66Christian Maeder @param[in] Tag The tag from the caller. This marks all the cache
a716971174535184da7713ed308423e355a4aa66Christian Maeder entries spawned from one route table entry.
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian Maeder @return NULL if failed to allocate memory for the cache, other point
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian Maeder to the created route cache entry.
d48085f765fca838c1d972d2123601997174583dChristian Maeder RtCacheEntry = AllocatePool (sizeof (IP4_ROUTE_CACHE_ENTRY));
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian Maeder Free the route cache entry. It is reference counted.
35cd0c10843c2cdbbe29f00a2a5d7e5e4f2d0064Christian Maeder @param RtCacheEntry The route cache entry to free.
3eb7ebab2dd79ac5277f087b18e8f05b9e9f0f9bChristian Maeder Initialize an empty route cache table.
3eb7ebab2dd79ac5277f087b18e8f05b9e9f0f9bChristian Maeder @param[in, out] RtCache The rotue cache table to initialize.
9a44a07ffc79da9852b6319bd6d9df81efe99809Christian Maeder for (Index = 0; Index < IP4_ROUTE_CACHE_HASH_VALUE; Index++) {
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder InitializeListHead (&(RtCache->CacheBucket[Index]));
a39a820684c1974350f46593025e0bb279f41bc6Christian Maeder Clean up a route cache, that is free all the route cache
a39a820684c1974350f46593025e0bb279f41bc6Christian Maeder entries enqueued in the cache.
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian Maeder @param[in] RtCache The route cache table to clean up
d48085f765fca838c1d972d2123601997174583dChristian Maeder for (Index = 0; Index < IP4_ROUTE_CACHE_HASH_VALUE; Index++) {
d48085f765fca838c1d972d2123601997174583dChristian Maeder NET_LIST_FOR_EACH_SAFE (Entry, Next, &(RtCache->CacheBucket[Index])) {
d48085f765fca838c1d972d2123601997174583dChristian Maeder RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_CACHE_ENTRY, Link);
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder Create an empty route table, includes its internal route cache
35cd0c10843c2cdbbe29f00a2a5d7e5e4f2d0064Christian Maeder @return NULL if failed to allocate memory for the route table, otherwise
35cd0c10843c2cdbbe29f00a2a5d7e5e4f2d0064Christian Maeder the point to newly created route table.
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder RtTable = AllocatePool (sizeof (IP4_ROUTE_TABLE));
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder for (Index = 0; Index < IP4_MASK_NUM; Index++) {
a39a820684c1974350f46593025e0bb279f41bc6Christian Maeder InitializeListHead (&(RtTable->RouteArea[Index]));
98c47b3c137bdb20c53b6c1d346c0fb48b48d673Christian Maeder Free the route table and its associated route cache. Route
98c47b3c137bdb20c53b6c1d346c0fb48b48d673Christian Maeder table is reference counted.
98c47b3c137bdb20c53b6c1d346c0fb48b48d673Christian Maeder @param[in] RtTable The route table to free.
35cd0c10843c2cdbbe29f00a2a5d7e5e4f2d0064Christian Maeder // Free all the route table entry and its route cache.
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian Maeder for (Index = 0; Index < IP4_MASK_NUM; Index++) {
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder NET_LIST_FOR_EACH_SAFE (Entry, Next, &(RtTable->RouteArea[Index])) {
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian Maeder Remove all the cache entries bearing the Tag. When a route cache
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder entry is created, it is tagged with the address of route entry
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder from which it is spawned. When a route entry is deleted, the cache
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder entries spawned from it are also deleted.
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder @param RtCache Route cache to remove the entries from
19f104861f1832b452c9f98e59880d05e865d9bdChristian Maeder @param Tag The Tag of the entries to remove
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder for (Index = 0; Index < IP4_ROUTE_CACHE_HASH_VALUE; Index++) {
62ecb1e7f8fd9573eea8369657de12c7bf9f4f25Christian Maeder NET_LIST_FOR_EACH_SAFE (Entry, Next, &RtCache->CacheBucket[Index]) {
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_CACHE_ENTRY, Link);
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder Add a route entry to the route table. All the IP4_ADDRs are in
fc8c6570c7b4ee13f375eb607bed2290438573bfChristian Maeder host byte order.
fc8c6570c7b4ee13f375eb607bed2290438573bfChristian Maeder @param[in, out] RtTable Route table to add route to
fc8c6570c7b4ee13f375eb607bed2290438573bfChristian Maeder @param[in] Dest The destination of the network
09249711700a6acbc40a2e337688b434d7aafa28Christian Maeder @param[in] Netmask The netmask of the destination
fc8c6570c7b4ee13f375eb607bed2290438573bfChristian Maeder @param[in] Gateway The next hop address
fc8c6570c7b4ee13f375eb607bed2290438573bfChristian Maeder @retval EFI_ACCESS_DENIED The same route already exists
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maeder @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the entry
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maeder @retval EFI_SUCCESS The route is added successfully.
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian Maeder // All the route entries with the same netmask length are
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian Maeder // linke to the same route area
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian Maeder Head = &(RtTable->RouteArea[NetGetMaskLength (Netmask)]);
9cb4aa4ea6685489a38f9b609f5dbe5d37f25bc7Christian Maeder // First check whether the route exists
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maeder if (IP4_NET_EQUAL (RtEntry->Dest, Dest, Netmask) && (RtEntry->NextHop == Gateway)) {
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // Create a route entry and insert it to the route area.
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder RtEntry = Ip4CreateRouteEntry (Dest, Netmask, Gateway);
d48085f765fca838c1d972d2123601997174583dChristian Maeder Remove a route entry and all the route caches spawn from it.
13f6b64b022fac1179149bfacf9a2ad908f7038dChristian Maeder @param RtTable The route table to remove the route from
13f6b64b022fac1179149bfacf9a2ad908f7038dChristian Maeder @param Dest The destination network
94b34b35075c9115a22b512fd4ec3fb290f13d59Christian Maeder @param Netmask The netmask of the Dest
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder @param Gateway The next hop address
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder @retval EFI_SUCCESS The route entry is successfully removed
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder @retval EFI_NOT_FOUND There is no route entry in the table with that
0a39036fa485579a7b7c81cdd44a412392571927Christian Maeder Head = &(RtTable->RouteArea[NetGetMaskLength (Netmask)]);
0a39036fa485579a7b7c81cdd44a412392571927Christian Maeder NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {
0a39036fa485579a7b7c81cdd44a412392571927Christian Maeder RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder if (IP4_NET_EQUAL (RtEntry->Dest, Dest, Netmask) && (RtEntry->NextHop == Gateway)) {
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder Ip4PurgeRouteCache (&RtTable->Cache, (UINTN) RtEntry);
return RtCacheEntry;
return NULL;
return RtEntry;
return NULL;
Ip4Route (
return RtCacheEntry;
return NULL;
return NULL;
Count = 0;
return RtCacheEntry;
if (Count == 0) {
return EFI_SUCCESS;
return EFI_OUT_OF_RESOURCES;
Count = 0;
Count++;
return EFI_SUCCESS;