ip4route.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 779 行 · 第 1/2 页

C
779
字号
  Netmask - The netmask of the destination
  Gateway - The next hop address

Returns:

  EFI_ACCESS_DENIED    - The same route already exists
  EFI_OUT_OF_RESOURCES - Failed to allocate memory for the entry
  EFI_SUCCESS          - The route is added successfully.

--*/
{
  NET_LIST_ENTRY            *Head;
  NET_LIST_ENTRY            *Entry;
  IP4_ROUTE_ENTRY           *RtEntry;

  //
  // All the route entries with the same netmask length are 
  // linke to the same route area
  //
  Head = &(RtTable->RouteArea[NetGetMaskLength (Netmask)]);

  //
  // First check whether the route exists
  //
  NET_LIST_FOR_EACH (Entry, Head) {
    RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);

    if (IP4_NET_EQUAL (RtEntry->Dest, Dest, Netmask) && (RtEntry->NextHop == Gateway)) {
      return EFI_ACCESS_DENIED;
    }
  }
  
  //
  // Create a route entry and insert it to the route area.
  //
  RtEntry = Ip4CreateRouteEntry (Dest, Netmask, Gateway);

  if (RtEntry == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  if (Gateway == IP4_ALLZERO_ADDRESS) {
    RtEntry->Flag = IP4_DIRECT_ROUTE;
  }

  NetListInsertHead (Head, &RtEntry->Link);
  RtTable->TotalNum++;

  return EFI_SUCCESS;
}

EFI_STATUS
Ip4DelRoute (
  IN IP4_ROUTE_TABLE      *RtTable,
  IN IP4_ADDR             Dest,
  IN IP4_ADDR             Netmask,
  IN IP4_ADDR             Gateway
  )
/*++

Routine Description:

  Remove a route entry and all the route caches spawn from it.

Arguments:

  RtTable - The route table to remove the route from
  Dest    - The destination network
  Netmask - The netmask of the Dest
  Gateway - The next hop address

Returns:

  EFI_SUCCESS   - The route entry is successfully removed
  EFI_NOT_FOUND - There is no route entry in the table with that properity.

--*/
{
  NET_LIST_ENTRY            *Head;
  NET_LIST_ENTRY            *Entry;
  NET_LIST_ENTRY            *Next;
  IP4_ROUTE_ENTRY           *RtEntry;

  Head = &(RtTable->RouteArea[NetGetMaskLength (Netmask)]);

  NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {
    RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);

    if (IP4_NET_EQUAL (RtEntry->Dest, Dest, Netmask) && (RtEntry->NextHop == Gateway)) {
      Ip4PurgeRouteCache (&RtTable->Cache, (UINTN) RtEntry);
      NetListRemoveEntry (Entry);
      Ip4FreeRouteEntry  (RtEntry);

      RtTable->TotalNum--;
      return EFI_SUCCESS;
    }
  }

  return EFI_NOT_FOUND;
}

IP4_ROUTE_CACHE_ENTRY *
Ip4FindRouteCache (
  IN IP4_ROUTE_TABLE        *RtTable,
  IN IP4_ADDR               Dest,
  IN IP4_ADDR               Src
  )
/*++

Routine Description:

  Find a route cache with the dst and src. This is used by ICMP 
  redirect messasge process. All kinds of redirect is treated as 
  host redirect according to RFC1122. So, only route cache entries
  are modified according to the ICMP redirect message.

Arguments:

  RtTable - The route table to search the cache for
  Dest    - The destination address
  Src     - The source address

Returns:

  NULL if no route entry to the (Dest, Src). Otherwise the point
  to the correct route cache entry.

--*/
{
  NET_LIST_ENTRY            *Entry;
  IP4_ROUTE_CACHE_ENTRY     *RtCacheEntry;
  UINT32                    Index;

  Index = IP4_ROUTE_CACHE_HASH (Dest, Src);

  NET_LIST_FOR_EACH (Entry, &RtTable->Cache.CacheBucket[Index]) {
    RtCacheEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_CACHE_ENTRY, Link);

    if ((RtCacheEntry->Dest == Dest) && (RtCacheEntry->Src == Src)) {
      NET_GET_REF (RtCacheEntry);
      return RtCacheEntry;
    }
  }

  return NULL;
}

STATIC
IP4_ROUTE_ENTRY *
Ip4FindRouteEntry (
  IN IP4_ROUTE_TABLE        *RtTable,
  IN IP4_ADDR               Dst
  )
/*++

Routine Description:

  Search the route table for a most specific match to the Dst. It searches 
  from the longest route area (mask length == 32) to the shortest route area (
  default routes). In each route area, it will first search the instance's 
  route table, then the default route table. This is required by the following
  requirements:
    1. IP search the route table for a most specific match
    2. The local route entries have precedence over the default route entry.

Arguments:

  RtTable - The route table to search from
  Dst     - The destionation address to search 

Returns:

  NULL if no route matches the Dst, otherwise the point to the 
  most specific route to the Dst.

--*/
{
  NET_LIST_ENTRY            *Entry;
  IP4_ROUTE_ENTRY           *RtEntry;
  IP4_ROUTE_TABLE           *Table;
  INTN                      Index;

  RtEntry = NULL;
    
  for (Index = IP4_MASK_NUM - 1; Index >= 0; Index--) {
    for (Table = RtTable; Table != NULL; Table = Table->Next) {
      NET_LIST_FOR_EACH (Entry, &Table->RouteArea[Index]) {
        RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);
        
        if (IP4_NET_EQUAL (RtEntry->Dest, Dst, RtEntry->Netmask)) {
          NET_GET_REF (RtEntry);
          return RtEntry;
        }      
      }
    }
  }


  return NULL;
}

IP4_ROUTE_CACHE_ENTRY *
Ip4Route (
  IN IP4_ROUTE_TABLE        *RtTable,
  IN IP4_ADDR               Dest,
  IN IP4_ADDR               Src
  )
/*++

Routine Description:

  Search the route table to route the packet. Return/creat a route
  cache if there is a route to the destination.

Arguments:

  RtTable  - The route table to search from
  Dest     - The destination address to search for
  Src      - The source address to search for

Returns:

  NULL if failed to route packet, otherwise a route cache 
  entry that can be used to route packet.

--*/
{
  NET_LIST_ENTRY            *Head;
  NET_LIST_ENTRY            *Entry;
  NET_LIST_ENTRY            *Next;
  IP4_ROUTE_CACHE_ENTRY     *RtCacheEntry;
  IP4_ROUTE_CACHE_ENTRY     *Cache;
  IP4_ROUTE_ENTRY           *RtEntry;
  IP4_ADDR                  NextHop;
  UINT32                    Count;

  ASSERT (RtTable != NULL);

  Head          = &RtTable->Cache.CacheBucket[IP4_ROUTE_CACHE_HASH (Dest, Src)];
  RtCacheEntry  = Ip4FindRouteCache (RtTable, Dest, Src);

  //
  // If found, promote the cache entry to the head of the hash bucket. LRU
  //
  if (RtCacheEntry != NULL) {
    NetListRemoveEntry (&RtCacheEntry->Link);
    NetListInsertHead (Head, &RtCacheEntry->Link);
    return RtCacheEntry;
  }
  
  //
  // Search the route table for the most specific route
  //
  RtEntry = Ip4FindRouteEntry (RtTable, Dest);
  
  if (RtEntry == NULL) {
    return NULL;
  }
  
  //
  // Found a route to the Dest, if it is a direct route, the packet
  // will be send directly to the destination, such as for connected
  // network. Otherwise, it is an indirect route, the packet will be
  // send the next hop router.
  //
  if (RtEntry->Flag & IP4_DIRECT_ROUTE) {
    NextHop = Dest;
  } else {
    NextHop = RtEntry->NextHop;
  }

  Ip4FreeRouteEntry (RtEntry);

  //
  // Create a route cache entry, and tag it as spawned from this route entry
  //
  RtCacheEntry = Ip4CreateRouteCacheEntry (Dest, Src, NextHop, (UINTN) RtEntry);

  if (RtCacheEntry == NULL) {
    return NULL;
  }

  NetListInsertHead (Head, &RtCacheEntry->Link);
  NET_GET_REF (RtCacheEntry);

  //
  // Each bucket of route cache can contain at most 64 entries.
  // Remove the entries at the tail of the bucket. These entries
  // are likely to be used least.
  //
  Count = 0;
  NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {
    if (++Count < IP4_ROUTE_CACHE_MAX) {
      continue;
    }

    Cache = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_CACHE_ENTRY, Link);

    NetListRemoveEntry (Entry);
    Ip4FreeRouteCacheEntry (Cache);
  }

  return RtCacheEntry;
}

EFI_STATUS
Ip4BuildEfiRouteTable (
  IN IP4_PROTOCOL           *IpInstance
  )
/*++

Routine Description:

  Build a EFI_IP4_ROUTE_TABLE to be returned to the caller of 
  GetModeData. The EFI_IP4_ROUTE_TABLE is clumsy to use in the 
  internal operation of the IP4 driver.

Arguments:

  IpInstance  - The IP4 child that requests the route table.

Returns:

  EFI_SUCCESS          - The route table is successfully build
  EFI_OUT_OF_RESOURCES - Failed to allocate the memory for the rotue table.

--*/
{
  IP4_SERVICE               *IpSb;
  NET_LIST_ENTRY            *Entry;
  IP4_ROUTE_TABLE           *RtTable;
  IP4_ROUTE_ENTRY           *RtEntry;
  EFI_IP4_ROUTE_TABLE       *Table;
  UINT32                    Count;
  INT32                     Index;

  IpSb    = IpInstance->Service;
  RtTable = IpInstance->RouteTable;

  if (IpInstance->EfiRouteTable != NULL) {
    NetFreePool (IpInstance->EfiRouteTable);
    
    IpInstance->EfiRouteTable = NULL;
    IpInstance->EfiRouteCount = 0;
  }

  Count = RtTable->TotalNum;

  if (RtTable->Next != NULL) {
    Count += RtTable->Next->TotalNum;
  }

  if (Count == 0) {
    return EFI_SUCCESS;
  }

  Table = NetAllocatePool (sizeof (EFI_IP4_ROUTE_TABLE) * Count);

  if (Table == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
  
  //
  // Copy the route entry to EFI route table. Keep the order of
  // route entry copied from most specific to default route. That
  // is, interlevel the route entry from the instance's route area
  // and those from the default route table's route area.
  //
  Count = 0;
  
  for (Index = IP4_MASK_NUM - 1; Index >= 0; Index--) {
    for (RtTable = IpInstance->RouteTable; RtTable != NULL; RtTable = RtTable->Next) {
      NET_LIST_FOR_EACH (Entry, &(RtTable->RouteArea[Index])) {
        RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);

        EFI_IP4 (Table[Count].SubnetAddress)  = HTONL (RtEntry->Dest & RtEntry->Netmask);
        EFI_IP4 (Table[Count].SubnetMask)     = HTONL (RtEntry->Netmask);
        EFI_IP4 (Table[Count].GatewayAddress) = HTONL (RtEntry->NextHop);

        Count++;
      }
    }
  }

  IpInstance->EfiRouteTable = Table;
  IpInstance->EfiRouteCount = Count;
  return EFI_SUCCESS;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?