mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/routing_table.h" |
| 6 | |
| 7 | #include <arpa/inet.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <linux/netlink.h> |
| 10 | #include <linux/rtnetlink.h> |
| 11 | #include <netinet/ether.h> |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 12 | #include <net/if.h> // NOLINT - must be included after netinet/ether.h |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 13 | #include <net/if_arp.h> |
| 14 | #include <string.h> |
| 15 | #include <sys/socket.h> |
| 16 | #include <time.h> |
| 17 | #include <unistd.h> |
| 18 | |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 19 | #include <memory> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 22 | #include <base/bind.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 23 | #include <base/files/file_path.h> |
Ben Chan | 11c213f | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 24 | #include <base/files/file_util.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 25 | #include <base/stl_util.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 26 | #include <base/strings/stringprintf.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 27 | |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 28 | #include "shill/ipconfig.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 29 | #include "shill/logging.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 30 | #include "shill/net/byte_string.h" |
| 31 | #include "shill/net/rtnl_handler.h" |
| 32 | #include "shill/net/rtnl_listener.h" |
| 33 | #include "shill/net/rtnl_message.h" |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 34 | #include "shill/routing_table_entry.h" |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 35 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 36 | using base::Bind; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 37 | using base::FilePath; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 38 | using base::Unretained; |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 39 | using std::deque; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 40 | using std::string; |
| 41 | using std::vector; |
| 42 | |
| 43 | namespace shill { |
| 44 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 45 | namespace Logging { |
| 46 | static auto kModuleLogScope = ScopeLogger::kRoute; |
| 47 | static string ObjectID(RoutingTable *r) { return "(routing_table)"; } |
| 48 | } |
| 49 | |
Ben Chan | bbdef5f | 2012-04-23 13:58:15 -0700 | [diff] [blame] | 50 | namespace { |
| 51 | base::LazyInstance<RoutingTable> g_routing_table = LAZY_INSTANCE_INITIALIZER; |
| 52 | } // namespace |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 53 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 54 | // static |
| 55 | const char RoutingTable::kRouteFlushPath4[] = "/proc/sys/net/ipv4/route/flush"; |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 56 | // static |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 57 | const char RoutingTable::kRouteFlushPath6[] = "/proc/sys/net/ipv6/route/flush"; |
| 58 | |
| 59 | RoutingTable::RoutingTable() |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 60 | : route_callback_(Bind(&RoutingTable::RouteMsgHandler, Unretained(this))), |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 61 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 62 | SLOG(this, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | RoutingTable::~RoutingTable() {} |
| 66 | |
| 67 | RoutingTable* RoutingTable::GetInstance() { |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 68 | return g_routing_table.Pointer(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void RoutingTable::Start() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 72 | SLOG(this, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 73 | |
| 74 | route_listener_.reset( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 75 | new RTNLListener(RTNLHandler::kRequestRoute, route_callback_)); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 76 | rtnl_handler_->RequestDump(RTNLHandler::kRequestRoute); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void RoutingTable::Stop() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 80 | SLOG(this, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 81 | |
| 82 | route_listener_.reset(); |
| 83 | } |
| 84 | |
| 85 | bool RoutingTable::AddRoute(int interface_index, |
| 86 | const RoutingTableEntry &entry) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 87 | SLOG(this, 2) << __func__ << ": " |
| 88 | << "destination " << entry.dst.ToString() |
| 89 | << " index " << interface_index |
| 90 | << " gateway " << entry.gateway.ToString() |
| 91 | << " metric " << entry.metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 92 | |
| 93 | CHECK(!entry.from_rtnl); |
| 94 | if (!ApplyRoute(interface_index, |
| 95 | entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 96 | RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 97 | NLM_F_CREATE | NLM_F_EXCL)) { |
| 98 | return false; |
| 99 | } |
| 100 | tables_[interface_index].push_back(entry); |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | bool RoutingTable::GetDefaultRoute(int interface_index, |
| 105 | IPAddress::Family family, |
| 106 | RoutingTableEntry *entry) { |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 107 | RoutingTableEntry *found_entry; |
| 108 | bool ret = GetDefaultRouteInternal(interface_index, family, &found_entry); |
| 109 | if (ret) { |
| 110 | *entry = *found_entry; |
| 111 | } |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | bool RoutingTable::GetDefaultRouteInternal(int interface_index, |
| 116 | IPAddress::Family family, |
| 117 | RoutingTableEntry **entry) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 118 | SLOG(this, 2) << __func__ << " index " << interface_index |
| 119 | << " family " << IPAddress::GetAddressFamilyName(family); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 120 | |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 121 | Tables::iterator table = tables_.find(interface_index); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 122 | if (table == tables_.end()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 123 | SLOG(this, 2) << __func__ << " no table"; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 127 | for (auto &nent : table->second) { |
| 128 | if (nent.dst.IsDefault() && nent.dst.family() == family) { |
| 129 | *entry = &nent; |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 130 | SLOG(this, 2) << __func__ << ": found" |
| 131 | << " gateway " << nent.gateway.ToString() |
| 132 | << " metric " << nent.metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | } |
| 136 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 137 | SLOG(this, 2) << __func__ << " no route"; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 138 | return false; |
| 139 | } |
| 140 | |
| 141 | bool RoutingTable::SetDefaultRoute(int interface_index, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 142 | const IPAddress &gateway_address, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 143 | uint32_t metric) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 144 | SLOG(this, 2) << __func__ << " index " << interface_index |
| 145 | << " metric " << metric; |
mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 146 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 147 | RoutingTableEntry *old_entry; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 148 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 149 | if (GetDefaultRouteInternal(interface_index, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 150 | gateway_address.family(), |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 151 | &old_entry)) { |
| 152 | if (old_entry->gateway.Equals(gateway_address)) { |
| 153 | if (old_entry->metric != metric) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 154 | ReplaceMetric(interface_index, old_entry, metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 155 | } |
| 156 | return true; |
| 157 | } else { |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 158 | // TODO(quiche): Update internal state as well? |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 159 | ApplyRoute(interface_index, |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 160 | *old_entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 161 | RTNLMessage::kModeDelete, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 162 | 0); |
| 163 | } |
| 164 | } |
| 165 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 166 | IPAddress default_address(gateway_address.family()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 167 | default_address.SetAddressToDefault(); |
| 168 | |
| 169 | return AddRoute(interface_index, |
| 170 | RoutingTableEntry(default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 171 | default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 172 | gateway_address, |
| 173 | metric, |
| 174 | RT_SCOPE_UNIVERSE, |
| 175 | false)); |
| 176 | } |
| 177 | |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 178 | bool RoutingTable::ConfigureRoutes(int interface_index, |
| 179 | const IPConfigRefPtr &ipconfig, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 180 | uint32_t metric) { |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 181 | bool ret = true; |
| 182 | |
| 183 | IPAddress::Family address_family = ipconfig->properties().address_family; |
| 184 | const vector<IPConfig::Route> &routes = ipconfig->properties().routes; |
| 185 | |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 186 | for (const auto &route : routes) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 187 | SLOG(this, 3) << "Installing route:" |
| 188 | << " Destination: " << route.host |
| 189 | << " Netmask: " << route.netmask |
| 190 | << " Gateway: " << route.gateway; |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 191 | IPAddress destination_address(address_family); |
| 192 | IPAddress source_address(address_family); // Left as default. |
| 193 | IPAddress gateway_address(address_family); |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 194 | if (!destination_address.SetAddressFromString(route.host)) { |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 195 | LOG(ERROR) << "Failed to parse host " |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 196 | << route.host; |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 197 | ret = false; |
| 198 | continue; |
| 199 | } |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 200 | if (!gateway_address.SetAddressFromString(route.gateway)) { |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 201 | LOG(ERROR) << "Failed to parse gateway " |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 202 | << route.gateway; |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 203 | ret = false; |
| 204 | continue; |
| 205 | } |
| 206 | destination_address.set_prefix( |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 207 | IPAddress::GetPrefixLengthFromMask(address_family, route.netmask)); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 208 | if (!AddRoute(interface_index, |
| 209 | RoutingTableEntry(destination_address, |
| 210 | source_address, |
| 211 | gateway_address, |
| 212 | metric, |
| 213 | RT_SCOPE_UNIVERSE, |
| 214 | false))) { |
| 215 | ret = false; |
| 216 | } |
| 217 | } |
| 218 | return ret; |
| 219 | } |
| 220 | |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 221 | void RoutingTable::FlushRoutes(int interface_index) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 222 | SLOG(this, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 223 | |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 224 | auto table = tables_.find(interface_index); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 225 | if (table == tables_.end()) { |
| 226 | return; |
| 227 | } |
| 228 | |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 229 | for (const auto &nent : table->second) { |
| 230 | ApplyRoute(interface_index, nent, RTNLMessage::kModeDelete, 0); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 231 | } |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 232 | table->second.clear(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 235 | void RoutingTable::FlushRoutesWithTag(int tag) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 236 | SLOG(this, 2) << __func__; |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 237 | |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 238 | for (auto &table : tables_) { |
| 239 | for (auto nent = table.second.begin(); nent != table.second.end();) { |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 240 | if (nent->tag == tag) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 241 | ApplyRoute(table.first, *nent, RTNLMessage::kModeDelete, 0); |
| 242 | nent = table.second.erase(nent); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 243 | } else { |
| 244 | ++nent; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 250 | void RoutingTable::ResetTable(int interface_index) { |
| 251 | tables_.erase(interface_index); |
| 252 | } |
| 253 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 254 | void RoutingTable::SetDefaultMetric(int interface_index, uint32_t metric) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 255 | SLOG(this, 2) << __func__ << " index " << interface_index |
| 256 | << " metric " << metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 257 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 258 | RoutingTableEntry *entry; |
| 259 | if (GetDefaultRouteInternal( |
| 260 | interface_index, IPAddress::kFamilyIPv4, &entry) && |
| 261 | entry->metric != metric) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 262 | ReplaceMetric(interface_index, entry, metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 263 | } |
| 264 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 265 | if (GetDefaultRouteInternal( |
| 266 | interface_index, IPAddress::kFamilyIPv6, &entry) && |
| 267 | entry->metric != metric) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 268 | ReplaceMetric(interface_index, entry, metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 272 | // static |
| 273 | bool RoutingTable::ParseRoutingTableMessage(const RTNLMessage &message, |
| 274 | int *interface_index, |
| 275 | RoutingTableEntry *entry) { |
| 276 | if (message.type() != RTNLMessage::kTypeRoute || |
| 277 | message.family() == IPAddress::kFamilyUnknown || |
| 278 | !message.HasAttribute(RTA_OIF)) { |
| 279 | return false; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 282 | const RTNLMessage::RouteStatus &route_status = message.route_status(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 283 | |
| 284 | if (route_status.type != RTN_UNICAST || |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 285 | route_status.table != RT_TABLE_MAIN) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 286 | return false; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 289 | uint32_t interface_index_u32 = 0; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 290 | if (!message.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&interface_index_u32)) { |
| 291 | return false; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 292 | } |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 293 | *interface_index = interface_index_u32; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 294 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 295 | uint32_t metric = 0; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 296 | if (message.HasAttribute(RTA_PRIORITY)) { |
| 297 | message.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 300 | IPAddress default_addr(message.family()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 301 | default_addr.SetAddressToDefault(); |
| 302 | |
| 303 | ByteString dst_bytes(default_addr.address()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 304 | if (message.HasAttribute(RTA_DST)) { |
| 305 | dst_bytes = message.GetAttribute(RTA_DST); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 306 | } |
| 307 | ByteString src_bytes(default_addr.address()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 308 | if (message.HasAttribute(RTA_SRC)) { |
| 309 | src_bytes = message.GetAttribute(RTA_SRC); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 310 | } |
| 311 | ByteString gateway_bytes(default_addr.address()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 312 | if (message.HasAttribute(RTA_GATEWAY)) { |
| 313 | gateway_bytes = message.GetAttribute(RTA_GATEWAY); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 316 | entry->dst = IPAddress(message.family(), dst_bytes, route_status.dst_prefix); |
| 317 | entry->src = IPAddress(message.family(), src_bytes, route_status.src_prefix); |
| 318 | entry->gateway = IPAddress(message.family(), gateway_bytes); |
| 319 | entry->metric = metric; |
| 320 | entry->scope = route_status.scope; |
| 321 | entry->from_rtnl = true; |
| 322 | |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | void RoutingTable::RouteMsgHandler(const RTNLMessage &message) { |
| 327 | int interface_index; |
| 328 | RoutingTableEntry entry; |
| 329 | |
| 330 | if (!ParseRoutingTableMessage(message, &interface_index, &entry)) { |
| 331 | return; |
| 332 | } |
| 333 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 334 | if (!route_queries_.empty() && |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 335 | message.route_status().protocol == RTPROT_UNSPEC) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 336 | SLOG(this, 3) << __func__ << ": Message seq: " << message.seq() |
| 337 | << " mode " << message.mode() |
| 338 | << ", next query seq: " << route_queries_.front().sequence; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 339 | |
| 340 | // Purge queries that have expired (sequence number of this message is |
| 341 | // greater than that of the head of the route query sequence). Do the |
| 342 | // math in a way that's roll-over independent. |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 343 | while (route_queries_.front().sequence - message.seq() > kuint32max / 2) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 344 | LOG(ERROR) << __func__ << ": Purging un-replied route request sequence " |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 345 | << route_queries_.front().sequence |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 346 | << " (< " << message.seq() << ")"; |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 347 | route_queries_.pop_front(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 348 | if (route_queries_.empty()) |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 349 | return; |
| 350 | } |
| 351 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 352 | const Query &query = route_queries_.front(); |
| 353 | if (query.sequence == message.seq()) { |
| 354 | RoutingTableEntry add_entry(entry); |
| 355 | add_entry.from_rtnl = false; |
| 356 | add_entry.tag = query.tag; |
| 357 | bool added = true; |
| 358 | if (add_entry.gateway.IsDefault()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 359 | SLOG(this, 2) << __func__ << ": Ignoring route result with no gateway " |
| 360 | << "since we don't need to plumb these."; |
Paul Stewart | bbed76d | 2012-04-27 20:02:13 -0700 | [diff] [blame] | 361 | } else { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 362 | SLOG(this, 2) << __func__ << ": Adding host route to " |
| 363 | << add_entry.dst.ToString(); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 364 | added = AddRoute(interface_index, add_entry); |
Paul Stewart | bbed76d | 2012-04-27 20:02:13 -0700 | [diff] [blame] | 365 | } |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 366 | if (added && !query.callback.is_null()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 367 | SLOG(this, 2) << "Running query callback."; |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 368 | query.callback.Run(interface_index, add_entry); |
| 369 | } |
| 370 | route_queries_.pop_front(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 371 | } |
| 372 | return; |
| 373 | } else if (message.route_status().protocol != RTPROT_BOOT) { |
| 374 | // Responses to route queries come back with a protocol of |
| 375 | // RTPROT_UNSPEC. Otherwise, normal route updates that we are |
| 376 | // interested in come with a protocol of RTPROT_BOOT. |
| 377 | return; |
| 378 | } |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 379 | |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 380 | TableEntryVector &table = tables_[interface_index]; |
| 381 | for (auto nent = table.begin(); nent != table.end(); ++nent) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 382 | if (nent->dst.Equals(entry.dst) && |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 383 | nent->src.Equals(entry.src) && |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 384 | nent->gateway.Equals(entry.gateway) && |
| 385 | nent->scope == entry.scope) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 386 | if (message.mode() == RTNLMessage::kModeDelete && |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 387 | nent->metric == entry.metric) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 388 | table.erase(nent); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 389 | } else if (message.mode() == RTNLMessage::kModeAdd) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 390 | nent->from_rtnl = true; |
| 391 | nent->metric = entry.metric; |
| 392 | } |
| 393 | return; |
| 394 | } |
| 395 | } |
| 396 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 397 | if (message.mode() == RTNLMessage::kModeAdd) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 398 | SLOG(this, 2) << __func__ << " adding" |
| 399 | << " destination " << entry.dst.ToString() |
| 400 | << " index " << interface_index |
| 401 | << " gateway " << entry.gateway.ToString() |
| 402 | << " metric " << entry.metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 403 | table.push_back(entry); |
| 404 | } |
| 405 | } |
| 406 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 407 | bool RoutingTable::ApplyRoute(uint32_t interface_index, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 408 | const RoutingTableEntry &entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 409 | RTNLMessage::Mode mode, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 410 | unsigned int flags) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 411 | SLOG(this, 2) << base::StringPrintf( |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 412 | "%s: dst %s/%d src %s/%d index %d mode %d flags 0x%x", |
| 413 | __func__, entry.dst.ToString().c_str(), entry.dst.prefix(), |
| 414 | entry.src.ToString().c_str(), entry.src.prefix(), |
| 415 | interface_index, mode, flags); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 416 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 417 | RTNLMessage message( |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 418 | RTNLMessage::kTypeRoute, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 419 | mode, |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 420 | NLM_F_REQUEST | flags, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 421 | 0, |
| 422 | 0, |
| 423 | 0, |
| 424 | entry.dst.family()); |
| 425 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 426 | message.set_route_status(RTNLMessage::RouteStatus( |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 427 | entry.dst.prefix(), |
| 428 | entry.src.prefix(), |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 429 | RT_TABLE_MAIN, |
| 430 | RTPROT_BOOT, |
| 431 | entry.scope, |
| 432 | RTN_UNICAST, |
| 433 | 0)); |
| 434 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 435 | message.SetAttribute(RTA_DST, entry.dst.address()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 436 | if (!entry.src.IsDefault()) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 437 | message.SetAttribute(RTA_SRC, entry.src.address()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 438 | } |
| 439 | if (!entry.gateway.IsDefault()) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 440 | message.SetAttribute(RTA_GATEWAY, entry.gateway.address()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 441 | } |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 442 | message.SetAttribute(RTA_PRIORITY, |
| 443 | ByteString::CreateFromCPUUInt32(entry.metric)); |
| 444 | message.SetAttribute(RTA_OIF, |
| 445 | ByteString::CreateFromCPUUInt32(interface_index)); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 446 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 447 | return rtnl_handler_->SendMessage(&message); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 450 | // Somewhat surprisingly, the kernel allows you to create multiple routes |
| 451 | // to the same destination through the same interface with different metrics. |
| 452 | // Therefore, to change the metric on a route, we can't just use the |
| 453 | // NLM_F_REPLACE flag by itself. We have to explicitly remove the old route. |
| 454 | // We do so after creating the route at a new metric so there is no traffic |
| 455 | // disruption to existing network streams. |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 456 | void RoutingTable::ReplaceMetric(uint32_t interface_index, |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 457 | RoutingTableEntry *entry, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 458 | uint32_t metric) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 459 | SLOG(this, 2) << __func__ << " index " << interface_index |
| 460 | << " metric " << metric; |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 461 | RoutingTableEntry new_entry = *entry; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 462 | new_entry.metric = metric; |
| 463 | // First create the route at the new metric. |
| 464 | ApplyRoute(interface_index, new_entry, RTNLMessage::kModeAdd, |
| 465 | NLM_F_CREATE | NLM_F_REPLACE); |
| 466 | // Then delete the route at the old metric. |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 467 | ApplyRoute(interface_index, *entry, RTNLMessage::kModeDelete, 0); |
| 468 | // Now, update our routing table (via |*entry|) from |new_entry|. |
| 469 | *entry = new_entry; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 472 | bool RoutingTable::FlushCache() { |
| 473 | static const char *kPaths[2] = { kRouteFlushPath4, kRouteFlushPath6 }; |
| 474 | bool ret = true; |
| 475 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 476 | SLOG(this, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 477 | |
| 478 | for (size_t i = 0; i < arraysize(kPaths); ++i) { |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 479 | if (base::WriteFile(FilePath(kPaths[i]), "-1", 2) != 2) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 480 | LOG(ERROR) << base::StringPrintf("Cannot write to route flush file %s", |
| 481 | kPaths[i]); |
| 482 | ret = false; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | return ret; |
| 487 | } |
| 488 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 489 | bool RoutingTable::RequestRouteToHost(const IPAddress &address, |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 490 | int interface_index, |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 491 | int tag, |
| 492 | const Query::Callback &callback) { |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 493 | // Make sure we don't get a cached response that is no longer valid. |
| 494 | FlushCache(); |
| 495 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 496 | RTNLMessage message( |
| 497 | RTNLMessage::kTypeRoute, |
| 498 | RTNLMessage::kModeQuery, |
| 499 | NLM_F_REQUEST, |
| 500 | 0, |
| 501 | 0, |
| 502 | interface_index, |
| 503 | address.family()); |
| 504 | |
| 505 | RTNLMessage::RouteStatus status; |
| 506 | status.dst_prefix = address.prefix(); |
| 507 | message.set_route_status(status); |
| 508 | message.SetAttribute(RTA_DST, address.address()); |
Paul Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 509 | |
| 510 | if (interface_index != -1) { |
| 511 | message.SetAttribute(RTA_OIF, |
| 512 | ByteString::CreateFromCPUUInt32(interface_index)); |
| 513 | } |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 514 | |
| 515 | if (!rtnl_handler_->SendMessage(&message)) { |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | // Save the sequence number of the request so we can create a route for |
| 520 | // this host when we get a reply. |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 521 | route_queries_.push_back(Query(message.seq(), tag, callback)); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 522 | |
| 523 | return true; |
| 524 | } |
| 525 | |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 526 | bool RoutingTable::CreateBlackholeRoute(int interface_index, |
Ben Chan | a6bfe87 | 2012-09-26 09:48:34 -0700 | [diff] [blame] | 527 | IPAddress::Family family, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 528 | uint32_t metric) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 529 | SLOG(this, 2) << base::StringPrintf( |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 530 | "%s: index %d family %s metric %d", |
| 531 | __func__, interface_index, |
| 532 | IPAddress::GetAddressFamilyName(family).c_str(), metric); |
| 533 | |
| 534 | RTNLMessage message( |
| 535 | RTNLMessage::kTypeRoute, |
| 536 | RTNLMessage::kModeAdd, |
| 537 | NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL, |
| 538 | 0, |
| 539 | 0, |
| 540 | 0, |
| 541 | family); |
| 542 | |
| 543 | message.set_route_status(RTNLMessage::RouteStatus( |
| 544 | 0, |
| 545 | 0, |
| 546 | RT_TABLE_MAIN, |
| 547 | RTPROT_BOOT, |
| 548 | RT_SCOPE_UNIVERSE, |
| 549 | RTN_BLACKHOLE, |
| 550 | 0)); |
| 551 | |
| 552 | message.SetAttribute(RTA_PRIORITY, |
| 553 | ByteString::CreateFromCPUUInt32(metric)); |
| 554 | message.SetAttribute(RTA_OIF, |
| 555 | ByteString::CreateFromCPUUInt32(interface_index)); |
| 556 | |
| 557 | return rtnl_handler_->SendMessage(&message); |
| 558 | } |
| 559 | |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 560 | bool RoutingTable::CreateLinkRoute(int interface_index, |
| 561 | const IPAddress &local_address, |
| 562 | const IPAddress &remote_address) { |
| 563 | if (!local_address.CanReachAddress(remote_address)) { |
| 564 | LOG(ERROR) << __func__ << " failed: " |
| 565 | << remote_address.ToString() << " is not reachable from " |
| 566 | << local_address.ToString(); |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | IPAddress default_address(local_address.family()); |
| 571 | default_address.SetAddressToDefault(); |
| 572 | IPAddress destination_address(remote_address); |
| 573 | destination_address.set_prefix( |
| 574 | IPAddress::GetMaxPrefixLength(remote_address.family())); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 575 | SLOG(this, 2) << "Creating link route to " << destination_address.ToString() |
| 576 | << " from " << local_address.ToString() |
| 577 | << " on interface index " << interface_index; |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 578 | return AddRoute(interface_index, |
| 579 | RoutingTableEntry(destination_address, |
| 580 | local_address, |
| 581 | default_address, |
| 582 | 0, |
| 583 | RT_SCOPE_LINK, |
| 584 | false)); |
| 585 | } |
| 586 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 587 | } // namespace shill |