routing_table.cc revision 7fab89734d88724a288e96a9996b15548c5294c7
12c15d2c7ae814d95673ea48ec8f4eb582ef3652emukesh agrawal// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart// Use of this source code is governed by a BSD-style license that can be
375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart// found in the LICENSE file.
475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include "shill/routing_table.h"
675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include <arpa/inet.h>
875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include <fcntl.h>
975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include <linux/netlink.h>
1075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include <linux/rtnetlink.h>
1175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include <netinet/ether.h>
12a41ab517725d036b63420f8445550246f8f50b99Alex Vakulenko#include <net/if.h>  // NOLINT - must be included after netinet/ether.h
1375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include <net/if_arp.h>
1475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include <string.h>
1575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include <sys/socket.h>
1675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include <time.h>
1775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include <unistd.h>
1875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
1975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include <string>
2075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
213e20a2341d0aeb7681e4ee0f89eae6817ade2b3bEric Shienbrood#include <base/bind.h>
22a0ddf46e466bd4ba3d20952f0a6988c680c1af14Ben Chan#include <base/files/file_path.h>
2375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include <base/file_util.h>
2475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include <base/memory/scoped_ptr.h>
253e20a2341d0aeb7681e4ee0f89eae6817ade2b3bEric Shienbrood#include <base/stl_util.h>
26a0ddf46e466bd4ba3d20952f0a6988c680c1af14Ben Chan#include <base/strings/stringprintf.h>
2775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
2875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include "shill/byte_string.h"
29b691efd71561246065eae3cdd73a96ca1b8a528dChristopher Wiley#include "shill/logging.h"
3075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include "shill/routing_table_entry.h"
3175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include "shill/rtnl_handler.h"
3275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include "shill/rtnl_listener.h"
3375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart#include "shill/rtnl_message.h"
3475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
353e20a2341d0aeb7681e4ee0f89eae6817ade2b3bEric Shienbroodusing base::Bind;
360e1cdeae24dd678a5fe27c840802582c0ca45ec0Albert Chaulkusing base::FilePath;
373e20a2341d0aeb7681e4ee0f89eae6817ade2b3bEric Shienbroodusing base::Unretained;
38abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkovusing std::deque;
3975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewartusing std::string;
4075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewartusing std::vector;
4175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
4275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewartnamespace shill {
4375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
44bbdef5fc35b48d9b43ba0dc005384abd36b72213Ben Channamespace {
45bbdef5fc35b48d9b43ba0dc005384abd36b72213Ben Chanbase::LazyInstance<RoutingTable> g_routing_table = LAZY_INSTANCE_INITIALIZER;
46bbdef5fc35b48d9b43ba0dc005384abd36b72213Ben Chan}  // namespace
470d2ada3971075e78fb9900d0753c9ad94c15add4Paul Stewart
4875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart// static
4975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewartconst char RoutingTable::kRouteFlushPath4[] = "/proc/sys/net/ipv4/route/flush";
500d2ada3971075e78fb9900d0753c9ad94c15add4Paul Stewart// static
5175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewartconst char RoutingTable::kRouteFlushPath6[] = "/proc/sys/net/ipv6/route/flush";
5275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
5375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul StewartRoutingTable::RoutingTable()
543e20a2341d0aeb7681e4ee0f89eae6817ade2b3bEric Shienbrood    : route_callback_(Bind(&RoutingTable::RouteMsgHandler, Unretained(this))),
55f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      rtnl_handler_(RTNLHandler::GetInstance()) {
56fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan  SLOG(Route, 2) << __func__;
5775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}
5875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
5975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul StewartRoutingTable::~RoutingTable() {}
6075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
6175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul StewartRoutingTable* RoutingTable::GetInstance() {
620d2ada3971075e78fb9900d0753c9ad94c15add4Paul Stewart  return g_routing_table.Pointer();
6375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}
6475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
6575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewartvoid RoutingTable::Start() {
66fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan  SLOG(Route, 2) << __func__;
6775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
6875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  route_listener_.reset(
693e20a2341d0aeb7681e4ee0f89eae6817ade2b3bEric Shienbrood      new RTNLListener(RTNLHandler::kRequestRoute, route_callback_));
70f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  rtnl_handler_->RequestDump(RTNLHandler::kRequestRoute);
7175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}
7275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
7375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewartvoid RoutingTable::Stop() {
74fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan  SLOG(Route, 2) << __func__;
7575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
7675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  route_listener_.reset();
7775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}
7875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
7975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewartbool RoutingTable::AddRoute(int interface_index,
8075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                            const RoutingTableEntry &entry) {
81fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan  SLOG(Route, 2) << __func__ << ": "
82fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan                 << "destination " << entry.dst.ToString()
83fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan                 << " index " << interface_index
84fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan                 << " gateway " << entry.gateway.ToString()
85fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan                 << " metric " << entry.metric;
8675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
8775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  CHECK(!entry.from_rtnl);
8875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  if (!ApplyRoute(interface_index,
8975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                  entry,
909a908080fc2a72dbf06f995b878fc8a3693b725aPaul Stewart                  RTNLMessage::kModeAdd,
9175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                  NLM_F_CREATE | NLM_F_EXCL)) {
9275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart    return false;
9375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
9475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  tables_[interface_index].push_back(entry);
9575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  return true;
9675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}
9775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
9875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewartbool RoutingTable::GetDefaultRoute(int interface_index,
9975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                                   IPAddress::Family family,
10075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                                   RoutingTableEntry *entry) {
101d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  RoutingTableEntry *found_entry;
102d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  bool ret = GetDefaultRouteInternal(interface_index, family, &found_entry);
103d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  if (ret) {
104d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal    *entry = *found_entry;
105d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  }
106d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  return ret;
107d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal}
108d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal
109d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawalbool RoutingTable::GetDefaultRouteInternal(int interface_index,
110d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal                                           IPAddress::Family family,
111d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal                                           RoutingTableEntry **entry) {
112fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan  SLOG(Route, 2) << __func__ << " index " << interface_index
113fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan                 << " family " << IPAddress::GetAddressFamilyName(family);
11475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
1156db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart  Tables::iterator table = tables_.find(interface_index);
11675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  if (table == tables_.end()) {
117fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan    SLOG(Route, 2) << __func__ << " no table";
11875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart    return false;
11975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
12075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
1216db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart  for (auto &nent : table->second) {
1226db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart    if (nent.dst.IsDefault() && nent.dst.family() == family) {
1236db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart      *entry = &nent;
124fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan      SLOG(Route, 2) << __func__ << ": found"
1256db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart                     << " gateway " << nent.gateway.ToString()
1266db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart                     << " metric " << nent.metric;
12775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      return true;
12875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart    }
12975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
13075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
131fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan  SLOG(Route, 2) << __func__ << " no route";
13275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  return false;
13375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}
13475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
13575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewartbool RoutingTable::SetDefaultRoute(int interface_index,
1365b7ba8c2e9e5ab5e67c68d0cde963141beb501d8Paul Stewart                                   const IPAddress &gateway_address,
1377fab89734d88724a288e96a9996b15548c5294c7Ben Chan                                   uint32_t metric) {
138fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan  SLOG(Route, 2) << __func__ << " index " << interface_index
139fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan                 << " metric " << metric;
1402c15d2c7ae814d95673ea48ec8f4eb582ef3652emukesh agrawal
141d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  RoutingTableEntry *old_entry;
14275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
143d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  if (GetDefaultRouteInternal(interface_index,
1445b7ba8c2e9e5ab5e67c68d0cde963141beb501d8Paul Stewart                              gateway_address.family(),
145d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal                              &old_entry)) {
146d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal    if (old_entry->gateway.Equals(gateway_address)) {
147d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal      if (old_entry->metric != metric) {
148c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart        ReplaceMetric(interface_index, old_entry, metric);
14975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      }
15075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      return true;
15175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart    } else {
152d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal      // TODO(quiche): Update internal state as well?
15375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      ApplyRoute(interface_index,
154d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal                 *old_entry,
1559a908080fc2a72dbf06f995b878fc8a3693b725aPaul Stewart                 RTNLMessage::kModeDelete,
15675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                 0);
15775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart    }
15875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
15975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
1605b7ba8c2e9e5ab5e67c68d0cde963141beb501d8Paul Stewart  IPAddress default_address(gateway_address.family());
16175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  default_address.SetAddressToDefault();
16275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
16375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  return AddRoute(interface_index,
16475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                  RoutingTableEntry(default_address,
16575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                                    default_address,
16675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                                    gateway_address,
16775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                                    metric,
16875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                                    RT_SCOPE_UNIVERSE,
16975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                                    false));
17075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}
17175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
1723f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewartbool RoutingTable::ConfigureRoutes(int interface_index,
1733f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart                                   const IPConfigRefPtr &ipconfig,
1747fab89734d88724a288e96a9996b15548c5294c7Ben Chan                                   uint32_t metric) {
1753f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart  bool ret = true;
1763f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart
1773f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart  IPAddress::Family address_family = ipconfig->properties().address_family;
1783f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart  const vector<IPConfig::Route> &routes = ipconfig->properties().routes;
1793f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart
1806db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart  for (const auto &route : routes) {
181fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan    SLOG(Route, 3) << "Installing route:"
1826db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart                   << " Destination: " << route.host
1836db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart                   << " Netmask: " << route.netmask
1846db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart                   << " Gateway: " << route.gateway;
1853f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart    IPAddress destination_address(address_family);
1863f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart    IPAddress source_address(address_family);  // Left as default.
1873f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart    IPAddress gateway_address(address_family);
1886db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart    if (!destination_address.SetAddressFromString(route.host)) {
1893f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart      LOG(ERROR) << "Failed to parse host "
1906db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart                 << route.host;
1913f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart      ret = false;
1923f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart      continue;
1933f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart    }
1946db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart    if (!gateway_address.SetAddressFromString(route.gateway)) {
1953f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart      LOG(ERROR) << "Failed to parse gateway "
1966db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart                 << route.gateway;
1973f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart      ret = false;
1983f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart      continue;
1993f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart    }
2003f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart    destination_address.set_prefix(
2016db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart        IPAddress::GetPrefixLengthFromMask(address_family, route.netmask));
2023f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart    if (!AddRoute(interface_index,
2033f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart                  RoutingTableEntry(destination_address,
2043f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart                                    source_address,
2053f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart                                    gateway_address,
2063f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart                                    metric,
2073f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart                                    RT_SCOPE_UNIVERSE,
2083f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart                                    false))) {
2093f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart      ret = false;
2103f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart    }
2113f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart  }
2123f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart  return ret;
2133f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart}
2143f68bb1e3768969a1db686325ae9a84c8fd06005Paul Stewart
215fb46caf4f23a41e73dfd87547121b5fb0230295aThieu Levoid RoutingTable::FlushRoutes(int interface_index) {
216fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan  SLOG(Route, 2) << __func__;
21775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
2186db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart  auto table = tables_.find(interface_index);
21975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  if (table == tables_.end()) {
22075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart    return;
22175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
22275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
2236db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart  for (const auto &nent : table->second) {
2246db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart    ApplyRoute(interface_index, nent, RTNLMessage::kModeDelete, 0);
22575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
226fb46caf4f23a41e73dfd87547121b5fb0230295aThieu Le  table->second.clear();
22775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}
22875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
229e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewartvoid RoutingTable::FlushRoutesWithTag(int tag) {
230e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart  SLOG(Route, 2) << __func__;
231e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart
2326db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart  for (auto &table : tables_) {
2336db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart    for (auto nent = table.second.begin(); nent != table.second.end();) {
234e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart      if (nent->tag == tag) {
2356db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart        ApplyRoute(table.first, *nent, RTNLMessage::kModeDelete, 0);
2366db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart        nent = table.second.erase(nent);
237e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart      } else {
238e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart        ++nent;
239e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart      }
240e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart    }
241e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart  }
242e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart}
243e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart
24475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewartvoid RoutingTable::ResetTable(int interface_index) {
24575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  tables_.erase(interface_index);
24675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}
24775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
2487fab89734d88724a288e96a9996b15548c5294c7Ben Chanvoid RoutingTable::SetDefaultMetric(int interface_index, uint32_t metric) {
249fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan  SLOG(Route, 2) << __func__ << " index " << interface_index
250fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan                 << " metric " << metric;
25175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
252d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  RoutingTableEntry *entry;
253d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  if (GetDefaultRouteInternal(
254d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal          interface_index, IPAddress::kFamilyIPv4, &entry) &&
255d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal      entry->metric != metric) {
256c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart    ReplaceMetric(interface_index, entry, metric);
25775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
25875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
259d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  if (GetDefaultRouteInternal(
260d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal          interface_index, IPAddress::kFamilyIPv6, &entry) &&
261d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal      entry->metric != metric) {
262c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart    ReplaceMetric(interface_index, entry, metric);
26375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
26475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}
26575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
266f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart// static
267f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewartbool RoutingTable::ParseRoutingTableMessage(const RTNLMessage &message,
268f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart                                            int *interface_index,
269f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart                                            RoutingTableEntry *entry) {
270f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  if (message.type() != RTNLMessage::kTypeRoute ||
271f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      message.family() == IPAddress::kFamilyUnknown ||
272f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      !message.HasAttribute(RTA_OIF)) {
273f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    return false;
27475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
27575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
276f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  const RTNLMessage::RouteStatus &route_status = message.route_status();
27775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
27875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  if (route_status.type != RTN_UNICAST ||
27975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      route_status.table != RT_TABLE_MAIN) {
280f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    return false;
28175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
28275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
2837fab89734d88724a288e96a9996b15548c5294c7Ben Chan  uint32_t interface_index_u32 = 0;
284f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  if (!message.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&interface_index_u32)) {
285f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    return false;
28675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
287f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  *interface_index = interface_index_u32;
28875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
2897fab89734d88724a288e96a9996b15548c5294c7Ben Chan  uint32_t metric = 0;
290f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  if (message.HasAttribute(RTA_PRIORITY)) {
291f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    message.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric);
29275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
29375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
294f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  IPAddress default_addr(message.family());
29575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  default_addr.SetAddressToDefault();
29675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
29775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  ByteString dst_bytes(default_addr.address());
298f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  if (message.HasAttribute(RTA_DST)) {
299f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    dst_bytes = message.GetAttribute(RTA_DST);
30075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
30175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  ByteString src_bytes(default_addr.address());
302f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  if (message.HasAttribute(RTA_SRC)) {
303f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    src_bytes = message.GetAttribute(RTA_SRC);
30475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
30575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  ByteString gateway_bytes(default_addr.address());
306f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  if (message.HasAttribute(RTA_GATEWAY)) {
307f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    gateway_bytes = message.GetAttribute(RTA_GATEWAY);
308f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  }
309f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart
310f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  entry->dst = IPAddress(message.family(), dst_bytes, route_status.dst_prefix);
311f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  entry->src = IPAddress(message.family(), src_bytes, route_status.src_prefix);
312f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  entry->gateway = IPAddress(message.family(), gateway_bytes);
313f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  entry->metric = metric;
314f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  entry->scope = route_status.scope;
315f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  entry->from_rtnl = true;
316f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart
317f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  return true;
318f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart}
319f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart
320f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewartvoid RoutingTable::RouteMsgHandler(const RTNLMessage &message) {
321f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  int interface_index;
322f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  RoutingTableEntry entry;
323f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart
324f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  if (!ParseRoutingTableMessage(message, &interface_index, &entry)) {
325f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    return;
32675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
32775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
328e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart  if (!route_queries_.empty() &&
329f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      message.route_status().protocol == RTPROT_UNSPEC) {
330fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan    SLOG(Route, 3) << __func__ << ": Message seq: " << message.seq()
331fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan                   << " mode " << message.mode()
332e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart                   << ", next query seq: " << route_queries_.front().sequence;
333f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart
334f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    // Purge queries that have expired (sequence number of this message is
335f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    // greater than that of the head of the route query sequence).  Do the
336f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    // math in a way that's roll-over independent.
337e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart    while (route_queries_.front().sequence - message.seq() > kuint32max / 2) {
338f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      LOG(ERROR) << __func__ << ": Purging un-replied route request sequence "
339e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart                 << route_queries_.front().sequence
340f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart                 << " (< " << message.seq() << ")";
341abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov      route_queries_.pop_front();
342e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart      if (route_queries_.empty())
343f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart        return;
344f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    }
345f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart
346abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov    const Query &query = route_queries_.front();
347abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov    if (query.sequence == message.seq()) {
348abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov      RoutingTableEntry add_entry(entry);
349abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov      add_entry.from_rtnl = false;
350abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov      add_entry.tag = query.tag;
351abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov      bool added = true;
352abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov      if (add_entry.gateway.IsDefault()) {
353bbed76d275058c8f9a42c5c71efdcc1223617bdcPaul Stewart        SLOG(Route, 2) << __func__ << ": Ignoring route result with no gateway "
354bbed76d275058c8f9a42c5c71efdcc1223617bdcPaul Stewart                       << "since we don't need to plumb these.";
355bbed76d275058c8f9a42c5c71efdcc1223617bdcPaul Stewart      } else {
356bbed76d275058c8f9a42c5c71efdcc1223617bdcPaul Stewart        SLOG(Route, 2) << __func__ << ": Adding host route to "
357abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov                       << add_entry.dst.ToString();
358abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov        added = AddRoute(interface_index, add_entry);
359bbed76d275058c8f9a42c5c71efdcc1223617bdcPaul Stewart      }
360abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov      if (added && !query.callback.is_null()) {
361abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov        SLOG(Route, 2) << "Running query callback.";
362abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov        query.callback.Run(interface_index, add_entry);
363abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov      }
364abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov      route_queries_.pop_front();
365f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    }
366f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    return;
367f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  } else if (message.route_status().protocol != RTPROT_BOOT) {
368f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    // Responses to route queries come back with a protocol of
369f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    // RTPROT_UNSPEC.  Otherwise, normal route updates that we are
370f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    // interested in come with a protocol of RTPROT_BOOT.
371f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    return;
372f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  }
37375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
3746db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart  TableEntryVector &table = tables_[interface_index];
3756db7b24348e69639e19cd6c408388b10d6ee54fePaul Stewart  for (auto nent = table.begin(); nent != table.end(); ++nent)  {
37675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart    if (nent->dst.Equals(entry.dst) &&
37775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart        nent->src.Equals(entry.src) &&
37875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart        nent->gateway.Equals(entry.gateway) &&
37975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart        nent->scope == entry.scope) {
380f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      if (message.mode() == RTNLMessage::kModeDelete &&
381c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart          nent->metric == entry.metric) {
38275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart        table.erase(nent);
383f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      } else if (message.mode() == RTNLMessage::kModeAdd) {
38475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart        nent->from_rtnl = true;
38575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart        nent->metric = entry.metric;
38675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      }
38775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      return;
38875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart    }
38975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
39075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
391f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  if (message.mode() == RTNLMessage::kModeAdd) {
392fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan    SLOG(Route, 2) << __func__ << " adding"
393fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan                   << " destination " << entry.dst.ToString()
394fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan                   << " index " << interface_index
395fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan                   << " gateway " << entry.gateway.ToString()
396fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan                   << " metric " << entry.metric;
39775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart    table.push_back(entry);
39875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
39975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}
40075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
4017fab89734d88724a288e96a9996b15548c5294c7Ben Chanbool RoutingTable::ApplyRoute(uint32_t interface_index,
40275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                              const RoutingTableEntry &entry,
4039a908080fc2a72dbf06f995b878fc8a3693b725aPaul Stewart                              RTNLMessage::Mode mode,
40475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                              unsigned int flags) {
405fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan  SLOG(Route, 2) << base::StringPrintf(
406fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan      "%s: dst %s/%d src %s/%d index %d mode %d flags 0x%x",
407fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan      __func__, entry.dst.ToString().c_str(), entry.dst.prefix(),
408fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan      entry.src.ToString().c_str(), entry.src.prefix(),
409fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan      interface_index, mode, flags);
41075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
411f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  RTNLMessage message(
4129a908080fc2a72dbf06f995b878fc8a3693b725aPaul Stewart      RTNLMessage::kTypeRoute,
41375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      mode,
414e613202d36e3bfb06a40eea1888694413210ef7ePaul Stewart      NLM_F_REQUEST | flags,
41575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      0,
41675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      0,
41775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      0,
41875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      entry.dst.family());
41975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
420f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  message.set_route_status(RTNLMessage::RouteStatus(
4219e3fcd7141240bc5efd0bf1513b9e78aa79d90ebPaul Stewart      entry.dst.prefix(),
4229e3fcd7141240bc5efd0bf1513b9e78aa79d90ebPaul Stewart      entry.src.prefix(),
42375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      RT_TABLE_MAIN,
42475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      RTPROT_BOOT,
42575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      entry.scope,
42675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      RTN_UNICAST,
42775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      0));
42875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
429f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  message.SetAttribute(RTA_DST, entry.dst.address());
43075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  if (!entry.src.IsDefault()) {
431f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    message.SetAttribute(RTA_SRC, entry.src.address());
43275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
43375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  if (!entry.gateway.IsDefault()) {
434f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    message.SetAttribute(RTA_GATEWAY, entry.gateway.address());
43575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
436f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  message.SetAttribute(RTA_PRIORITY,
437f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart                       ByteString::CreateFromCPUUInt32(entry.metric));
438f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  message.SetAttribute(RTA_OIF,
439f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart                       ByteString::CreateFromCPUUInt32(interface_index));
44075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
441f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  return rtnl_handler_->SendMessage(&message);
44275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}
44375e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
444c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart// Somewhat surprisingly, the kernel allows you to create multiple routes
445c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart// to the same destination through the same interface with different metrics.
446c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart// Therefore, to change the metric on a route, we can't just use the
447c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart// NLM_F_REPLACE flag by itself.  We have to explicitly remove the old route.
448c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart// We do so after creating the route at a new metric so there is no traffic
449c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart// disruption to existing network streams.
4507fab89734d88724a288e96a9996b15548c5294c7Ben Chanvoid RoutingTable::ReplaceMetric(uint32_t interface_index,
451d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal                                 RoutingTableEntry *entry,
4527fab89734d88724a288e96a9996b15548c5294c7Ben Chan                                 uint32_t metric) {
453fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan  SLOG(Route, 2) << __func__ << " index " << interface_index
454fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan                 << " metric " << metric;
455d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  RoutingTableEntry new_entry = *entry;
456c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart  new_entry.metric = metric;
457c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart  // First create the route at the new metric.
458c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart  ApplyRoute(interface_index, new_entry, RTNLMessage::kModeAdd,
459c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart             NLM_F_CREATE | NLM_F_REPLACE);
460c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart  // Then delete the route at the old metric.
461d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  ApplyRoute(interface_index, *entry, RTNLMessage::kModeDelete, 0);
462d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  // Now, update our routing table (via |*entry|) from |new_entry|.
463d4ef677ad36ef34f59529af557adbe77b2c29c80mukesh agrawal  *entry = new_entry;
464c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart}
465c1dec4d5cad7c6ee2cd8dbc4f47e4d30403dcca1Paul Stewart
46675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewartbool RoutingTable::FlushCache() {
46775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  static const char *kPaths[2] = { kRouteFlushPath4, kRouteFlushPath6 };
46875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  bool ret = true;
46975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
470fad4a0b7e55dd82d3815ee96862b6e546727eb6eBen Chan  SLOG(Route, 2) << __func__;
47175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
47275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  for (size_t i = 0; i < arraysize(kPaths); ++i) {
4736fbf64f493a9aae7d743888039c61a57386203dbBen Chan    if (base::WriteFile(FilePath(kPaths[i]), "-1", 2) != 2) {
47475e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      LOG(ERROR) << base::StringPrintf("Cannot write to route flush file %s",
47575e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart                                       kPaths[i]);
47675e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart      ret = false;
47775e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart    }
47875e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  }
47975e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
48075e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart  return ret;
48175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}
48275e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart
483f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewartbool RoutingTable::RequestRouteToHost(const IPAddress &address,
484e93b038972d43fd703b3c68603fb4d02bec6504ePaul Stewart                                      int interface_index,
485abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov                                      int tag,
486abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov                                      const Query::Callback &callback) {
487e78ec548060d969fecdb488d6fed711d843bba91Paul Stewart  // Make sure we don't get a cached response that is no longer valid.
488e78ec548060d969fecdb488d6fed711d843bba91Paul Stewart  FlushCache();
489e78ec548060d969fecdb488d6fed711d843bba91Paul Stewart
490f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  RTNLMessage message(
491f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      RTNLMessage::kTypeRoute,
492f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      RTNLMessage::kModeQuery,
493f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      NLM_F_REQUEST,
494f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      0,
495f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      0,
496f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      interface_index,
497f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart      address.family());
498f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart
499f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  RTNLMessage::RouteStatus status;
500f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  status.dst_prefix = address.prefix();
501f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  message.set_route_status(status);
502f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  message.SetAttribute(RTA_DST, address.address());
503536820ddd33f63e1d95e6e04e1757bd0248bbe38Paul Stewart
504536820ddd33f63e1d95e6e04e1757bd0248bbe38Paul Stewart  if (interface_index != -1) {
505536820ddd33f63e1d95e6e04e1757bd0248bbe38Paul Stewart    message.SetAttribute(RTA_OIF,
506536820ddd33f63e1d95e6e04e1757bd0248bbe38Paul Stewart                         ByteString::CreateFromCPUUInt32(interface_index));
507536820ddd33f63e1d95e6e04e1757bd0248bbe38Paul Stewart  }
508f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart
509f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  if (!rtnl_handler_->SendMessage(&message)) {
510f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart    return false;
511f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  }
512f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart
513f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  // Save the sequence number of the request so we can create a route for
514f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  // this host when we get a reply.
515abf6d289b2d29487f0a51b6138a127707a38507aDarin Petkov  route_queries_.push_back(Query(message.seq(), tag, callback));
516f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart
517f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart  return true;
518f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart}
519f748a36f2cde2b1ac7fc543cc710c81cb431cc2fPaul Stewart
520a016312b7470e124774aec197e3b43bc795637c3Ben Chanbool RoutingTable::CreateBlackholeRoute(int interface_index,
521a6bfe87a2c0bcb68d789473ca10988243229667bBen Chan                                        IPAddress::Family family,
5227fab89734d88724a288e96a9996b15548c5294c7Ben Chan                                        uint32_t metric) {
523a016312b7470e124774aec197e3b43bc795637c3Ben Chan  SLOG(Route, 2) << base::StringPrintf(
524a016312b7470e124774aec197e3b43bc795637c3Ben Chan      "%s: index %d family %s metric %d",
525a016312b7470e124774aec197e3b43bc795637c3Ben Chan      __func__, interface_index,
526a016312b7470e124774aec197e3b43bc795637c3Ben Chan      IPAddress::GetAddressFamilyName(family).c_str(), metric);
527a016312b7470e124774aec197e3b43bc795637c3Ben Chan
528a016312b7470e124774aec197e3b43bc795637c3Ben Chan  RTNLMessage message(
529a016312b7470e124774aec197e3b43bc795637c3Ben Chan      RTNLMessage::kTypeRoute,
530a016312b7470e124774aec197e3b43bc795637c3Ben Chan      RTNLMessage::kModeAdd,
531a016312b7470e124774aec197e3b43bc795637c3Ben Chan      NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL,
532a016312b7470e124774aec197e3b43bc795637c3Ben Chan      0,
533a016312b7470e124774aec197e3b43bc795637c3Ben Chan      0,
534a016312b7470e124774aec197e3b43bc795637c3Ben Chan      0,
535a016312b7470e124774aec197e3b43bc795637c3Ben Chan      family);
536a016312b7470e124774aec197e3b43bc795637c3Ben Chan
537a016312b7470e124774aec197e3b43bc795637c3Ben Chan  message.set_route_status(RTNLMessage::RouteStatus(
538a016312b7470e124774aec197e3b43bc795637c3Ben Chan      0,
539a016312b7470e124774aec197e3b43bc795637c3Ben Chan      0,
540a016312b7470e124774aec197e3b43bc795637c3Ben Chan      RT_TABLE_MAIN,
541a016312b7470e124774aec197e3b43bc795637c3Ben Chan      RTPROT_BOOT,
542a016312b7470e124774aec197e3b43bc795637c3Ben Chan      RT_SCOPE_UNIVERSE,
543a016312b7470e124774aec197e3b43bc795637c3Ben Chan      RTN_BLACKHOLE,
544a016312b7470e124774aec197e3b43bc795637c3Ben Chan      0));
545a016312b7470e124774aec197e3b43bc795637c3Ben Chan
546a016312b7470e124774aec197e3b43bc795637c3Ben Chan  message.SetAttribute(RTA_PRIORITY,
547a016312b7470e124774aec197e3b43bc795637c3Ben Chan                       ByteString::CreateFromCPUUInt32(metric));
548a016312b7470e124774aec197e3b43bc795637c3Ben Chan  message.SetAttribute(RTA_OIF,
549a016312b7470e124774aec197e3b43bc795637c3Ben Chan                       ByteString::CreateFromCPUUInt32(interface_index));
550a016312b7470e124774aec197e3b43bc795637c3Ben Chan
551a016312b7470e124774aec197e3b43bc795637c3Ben Chan  return rtnl_handler_->SendMessage(&message);
552a016312b7470e124774aec197e3b43bc795637c3Ben Chan}
553a016312b7470e124774aec197e3b43bc795637c3Ben Chan
5544a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewartbool RoutingTable::CreateLinkRoute(int interface_index,
5554a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart                                   const IPAddress &local_address,
5564a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart                                   const IPAddress &remote_address) {
5574a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart  if (!local_address.CanReachAddress(remote_address)) {
5584a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart    LOG(ERROR) << __func__ << " failed: "
5594a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart               << remote_address.ToString() << " is not reachable from "
5604a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart               << local_address.ToString();
5614a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart    return false;
5624a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart  }
5634a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart
5644a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart  IPAddress default_address(local_address.family());
5654a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart  default_address.SetAddressToDefault();
5664a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart  IPAddress destination_address(remote_address);
5674a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart  destination_address.set_prefix(
5684a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart      IPAddress::GetMaxPrefixLength(remote_address.family()));
5694a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart  SLOG(Route, 2) << "Creating link route to " << destination_address.ToString()
5704a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart                 << " from " << local_address.ToString()
5714a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart                 << " on interface index " << interface_index;
5724a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart  return AddRoute(interface_index,
5734a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart                  RoutingTableEntry(destination_address,
5744a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart                                    local_address,
5754a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart                                    default_address,
5764a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart                                    0,
5774a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart                                    RT_SCOPE_LINK,
5784a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart                                    false));
5794a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart}
5804a6748d492214afcb7c484668fa8cfd3ad963f10Paul Stewart
58175e89d2d5ecb3c42a869f485d4483fd2176381c0Paul Stewart}  // namespace shill
582