blob: bf9e906f5a3143a4ecbee10e6b4c6e4fcbbc3ab9 [file] [log] [blame]
mukesh agrawal2c15d2c2012-02-21 16:09:21 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75e89d22011-08-01 10:00:02 -07002// 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 Vakulenkoa41ab512014-07-23 14:24:23 -070012#include <net/if.h> // NOLINT - must be included after netinet/ether.h
Paul Stewart75e89d22011-08-01 10:00:02 -070013#include <net/if_arp.h>
14#include <string.h>
15#include <sys/socket.h>
16#include <time.h>
17#include <unistd.h>
18
Ben Chancd477322014-10-17 14:19:30 -070019#include <memory>
Paul Stewart75e89d22011-08-01 10:00:02 -070020#include <string>
21
Eric Shienbrood3e20a232012-02-16 11:35:56 -050022#include <base/bind.h>
Ben Chana0ddf462014-02-06 11:32:42 -080023#include <base/files/file_path.h>
Ben Chan11c213f2014-09-05 08:21:06 -070024#include <base/files/file_util.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050025#include <base/stl_util.h>
Ben Chana0ddf462014-02-06 11:32:42 -080026#include <base/strings/stringprintf.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070027
Peter Qiu8d6b5972014-10-28 15:33:34 -070028#include "shill/ipconfig.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070029#include "shill/logging.h"
Peter Qiu8d6b5972014-10-28 15:33:34 -070030#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 Stewart75e89d22011-08-01 10:00:02 -070034#include "shill/routing_table_entry.h"
Paul Stewart75e89d22011-08-01 10:00:02 -070035
Eric Shienbrood3e20a232012-02-16 11:35:56 -050036using base::Bind;
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080037using base::FilePath;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050038using base::Unretained;
Darin Petkovabf6d282012-05-08 15:49:05 +020039using std::deque;
Paul Stewart75e89d22011-08-01 10:00:02 -070040using std::string;
41using std::vector;
42
43namespace shill {
44
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070045namespace Logging {
46static auto kModuleLogScope = ScopeLogger::kRoute;
47static string ObjectID(RoutingTable *r) { return "(routing_table)"; }
48}
49
Ben Chanbbdef5f2012-04-23 13:58:15 -070050namespace {
51base::LazyInstance<RoutingTable> g_routing_table = LAZY_INSTANCE_INITIALIZER;
52} // namespace
Paul Stewart0d2ada32011-08-09 17:01:57 -070053
Paul Stewart75e89d22011-08-01 10:00:02 -070054// static
55const char RoutingTable::kRouteFlushPath4[] = "/proc/sys/net/ipv4/route/flush";
Paul Stewart0d2ada32011-08-09 17:01:57 -070056// static
Paul Stewart75e89d22011-08-01 10:00:02 -070057const char RoutingTable::kRouteFlushPath6[] = "/proc/sys/net/ipv6/route/flush";
58
59RoutingTable::RoutingTable()
Eric Shienbrood3e20a232012-02-16 11:35:56 -050060 : route_callback_(Bind(&RoutingTable::RouteMsgHandler, Unretained(this))),
Paul Stewartf748a362012-03-07 12:01:20 -080061 rtnl_handler_(RTNLHandler::GetInstance()) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070062 SLOG(this, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -070063}
64
65RoutingTable::~RoutingTable() {}
66
67RoutingTable* RoutingTable::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070068 return g_routing_table.Pointer();
Paul Stewart75e89d22011-08-01 10:00:02 -070069}
70
71void RoutingTable::Start() {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070072 SLOG(this, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -070073
74 route_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -050075 new RTNLListener(RTNLHandler::kRequestRoute, route_callback_));
Paul Stewartf748a362012-03-07 12:01:20 -080076 rtnl_handler_->RequestDump(RTNLHandler::kRequestRoute);
Paul Stewart75e89d22011-08-01 10:00:02 -070077}
78
79void RoutingTable::Stop() {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070080 SLOG(this, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -070081
82 route_listener_.reset();
83}
84
85bool RoutingTable::AddRoute(int interface_index,
86 const RoutingTableEntry &entry) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070087 SLOG(this, 2) << __func__ << ": "
88 << "destination " << entry.dst.ToString()
89 << " index " << interface_index
90 << " gateway " << entry.gateway.ToString()
91 << " metric " << entry.metric;
Paul Stewart75e89d22011-08-01 10:00:02 -070092
93 CHECK(!entry.from_rtnl);
94 if (!ApplyRoute(interface_index,
95 entry,
Paul Stewart9a908082011-08-31 12:18:48 -070096 RTNLMessage::kModeAdd,
Paul Stewart75e89d22011-08-01 10:00:02 -070097 NLM_F_CREATE | NLM_F_EXCL)) {
98 return false;
99 }
100 tables_[interface_index].push_back(entry);
101 return true;
102}
103
104bool RoutingTable::GetDefaultRoute(int interface_index,
105 IPAddress::Family family,
106 RoutingTableEntry *entry) {
mukesh agrawald4ef6772012-02-21 16:28:04 -0800107 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
115bool RoutingTable::GetDefaultRouteInternal(int interface_index,
116 IPAddress::Family family,
117 RoutingTableEntry **entry) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700118 SLOG(this, 2) << __func__ << " index " << interface_index
119 << " family " << IPAddress::GetAddressFamilyName(family);
Paul Stewart75e89d22011-08-01 10:00:02 -0700120
Paul Stewart6db7b242014-05-02 15:34:21 -0700121 Tables::iterator table = tables_.find(interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -0700122 if (table == tables_.end()) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700123 SLOG(this, 2) << __func__ << " no table";
Paul Stewart75e89d22011-08-01 10:00:02 -0700124 return false;
125 }
126
Paul Stewart6db7b242014-05-02 15:34:21 -0700127 for (auto &nent : table->second) {
128 if (nent.dst.IsDefault() && nent.dst.family() == family) {
129 *entry = &nent;
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700130 SLOG(this, 2) << __func__ << ": found"
131 << " gateway " << nent.gateway.ToString()
132 << " metric " << nent.metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700133 return true;
134 }
135 }
136
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700137 SLOG(this, 2) << __func__ << " no route";
Paul Stewart75e89d22011-08-01 10:00:02 -0700138 return false;
139}
140
141bool RoutingTable::SetDefaultRoute(int interface_index,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700142 const IPAddress &gateway_address,
Ben Chan7fab8972014-08-10 17:14:46 -0700143 uint32_t metric) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700144 SLOG(this, 2) << __func__ << " index " << interface_index
145 << " metric " << metric;
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800146
mukesh agrawald4ef6772012-02-21 16:28:04 -0800147 RoutingTableEntry *old_entry;
Paul Stewart75e89d22011-08-01 10:00:02 -0700148
mukesh agrawald4ef6772012-02-21 16:28:04 -0800149 if (GetDefaultRouteInternal(interface_index,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700150 gateway_address.family(),
mukesh agrawald4ef6772012-02-21 16:28:04 -0800151 &old_entry)) {
152 if (old_entry->gateway.Equals(gateway_address)) {
153 if (old_entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800154 ReplaceMetric(interface_index, old_entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700155 }
156 return true;
157 } else {
mukesh agrawald4ef6772012-02-21 16:28:04 -0800158 // TODO(quiche): Update internal state as well?
Paul Stewart75e89d22011-08-01 10:00:02 -0700159 ApplyRoute(interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -0800160 *old_entry,
Paul Stewart9a908082011-08-31 12:18:48 -0700161 RTNLMessage::kModeDelete,
Paul Stewart75e89d22011-08-01 10:00:02 -0700162 0);
163 }
164 }
165
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700166 IPAddress default_address(gateway_address.family());
Paul Stewart75e89d22011-08-01 10:00:02 -0700167 default_address.SetAddressToDefault();
168
169 return AddRoute(interface_index,
170 RoutingTableEntry(default_address,
Paul Stewart75e89d22011-08-01 10:00:02 -0700171 default_address,
Paul Stewart75e89d22011-08-01 10:00:02 -0700172 gateway_address,
173 metric,
174 RT_SCOPE_UNIVERSE,
175 false));
176}
177
Paul Stewart3f68bb12012-03-15 13:33:10 -0700178bool RoutingTable::ConfigureRoutes(int interface_index,
179 const IPConfigRefPtr &ipconfig,
Ben Chan7fab8972014-08-10 17:14:46 -0700180 uint32_t metric) {
Paul Stewart3f68bb12012-03-15 13:33:10 -0700181 bool ret = true;
182
183 IPAddress::Family address_family = ipconfig->properties().address_family;
184 const vector<IPConfig::Route> &routes = ipconfig->properties().routes;
185
Paul Stewart6db7b242014-05-02 15:34:21 -0700186 for (const auto &route : routes) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700187 SLOG(this, 3) << "Installing route:"
188 << " Destination: " << route.host
189 << " Netmask: " << route.netmask
190 << " Gateway: " << route.gateway;
Paul Stewart3f68bb12012-03-15 13:33:10 -0700191 IPAddress destination_address(address_family);
192 IPAddress source_address(address_family); // Left as default.
193 IPAddress gateway_address(address_family);
Paul Stewart6db7b242014-05-02 15:34:21 -0700194 if (!destination_address.SetAddressFromString(route.host)) {
Paul Stewart3f68bb12012-03-15 13:33:10 -0700195 LOG(ERROR) << "Failed to parse host "
Paul Stewart6db7b242014-05-02 15:34:21 -0700196 << route.host;
Paul Stewart3f68bb12012-03-15 13:33:10 -0700197 ret = false;
198 continue;
199 }
Paul Stewart6db7b242014-05-02 15:34:21 -0700200 if (!gateway_address.SetAddressFromString(route.gateway)) {
Paul Stewart3f68bb12012-03-15 13:33:10 -0700201 LOG(ERROR) << "Failed to parse gateway "
Paul Stewart6db7b242014-05-02 15:34:21 -0700202 << route.gateway;
Paul Stewart3f68bb12012-03-15 13:33:10 -0700203 ret = false;
204 continue;
205 }
206 destination_address.set_prefix(
Paul Stewart6db7b242014-05-02 15:34:21 -0700207 IPAddress::GetPrefixLengthFromMask(address_family, route.netmask));
Paul Stewart3f68bb12012-03-15 13:33:10 -0700208 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 Lefb46caf2012-03-08 11:57:15 -0800221void RoutingTable::FlushRoutes(int interface_index) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700222 SLOG(this, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -0700223
Paul Stewart6db7b242014-05-02 15:34:21 -0700224 auto table = tables_.find(interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -0700225 if (table == tables_.end()) {
226 return;
227 }
228
Paul Stewart6db7b242014-05-02 15:34:21 -0700229 for (const auto &nent : table->second) {
230 ApplyRoute(interface_index, nent, RTNLMessage::kModeDelete, 0);
Paul Stewart75e89d22011-08-01 10:00:02 -0700231 }
Thieu Lefb46caf2012-03-08 11:57:15 -0800232 table->second.clear();
Paul Stewart75e89d22011-08-01 10:00:02 -0700233}
234
Paul Stewarte93b0382012-04-24 13:11:28 -0700235void RoutingTable::FlushRoutesWithTag(int tag) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700236 SLOG(this, 2) << __func__;
Paul Stewarte93b0382012-04-24 13:11:28 -0700237
Paul Stewart6db7b242014-05-02 15:34:21 -0700238 for (auto &table : tables_) {
239 for (auto nent = table.second.begin(); nent != table.second.end();) {
Paul Stewarte93b0382012-04-24 13:11:28 -0700240 if (nent->tag == tag) {
Paul Stewart6db7b242014-05-02 15:34:21 -0700241 ApplyRoute(table.first, *nent, RTNLMessage::kModeDelete, 0);
242 nent = table.second.erase(nent);
Paul Stewarte93b0382012-04-24 13:11:28 -0700243 } else {
244 ++nent;
245 }
246 }
247 }
248}
249
Paul Stewart75e89d22011-08-01 10:00:02 -0700250void RoutingTable::ResetTable(int interface_index) {
251 tables_.erase(interface_index);
252}
253
Ben Chan7fab8972014-08-10 17:14:46 -0700254void RoutingTable::SetDefaultMetric(int interface_index, uint32_t metric) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700255 SLOG(this, 2) << __func__ << " index " << interface_index
256 << " metric " << metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700257
mukesh agrawald4ef6772012-02-21 16:28:04 -0800258 RoutingTableEntry *entry;
259 if (GetDefaultRouteInternal(
260 interface_index, IPAddress::kFamilyIPv4, &entry) &&
261 entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800262 ReplaceMetric(interface_index, entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700263 }
264
mukesh agrawald4ef6772012-02-21 16:28:04 -0800265 if (GetDefaultRouteInternal(
266 interface_index, IPAddress::kFamilyIPv6, &entry) &&
267 entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800268 ReplaceMetric(interface_index, entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700269 }
270}
271
Paul Stewartf748a362012-03-07 12:01:20 -0800272// static
273bool 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 Stewart75e89d22011-08-01 10:00:02 -0700280 }
281
Paul Stewartf748a362012-03-07 12:01:20 -0800282 const RTNLMessage::RouteStatus &route_status = message.route_status();
Paul Stewart75e89d22011-08-01 10:00:02 -0700283
284 if (route_status.type != RTN_UNICAST ||
Paul Stewart75e89d22011-08-01 10:00:02 -0700285 route_status.table != RT_TABLE_MAIN) {
Paul Stewartf748a362012-03-07 12:01:20 -0800286 return false;
Paul Stewart75e89d22011-08-01 10:00:02 -0700287 }
288
Ben Chan7fab8972014-08-10 17:14:46 -0700289 uint32_t interface_index_u32 = 0;
Paul Stewartf748a362012-03-07 12:01:20 -0800290 if (!message.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&interface_index_u32)) {
291 return false;
Paul Stewart75e89d22011-08-01 10:00:02 -0700292 }
Paul Stewartf748a362012-03-07 12:01:20 -0800293 *interface_index = interface_index_u32;
Paul Stewart75e89d22011-08-01 10:00:02 -0700294
Ben Chan7fab8972014-08-10 17:14:46 -0700295 uint32_t metric = 0;
Paul Stewartf748a362012-03-07 12:01:20 -0800296 if (message.HasAttribute(RTA_PRIORITY)) {
297 message.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700298 }
299
Paul Stewartf748a362012-03-07 12:01:20 -0800300 IPAddress default_addr(message.family());
Paul Stewart75e89d22011-08-01 10:00:02 -0700301 default_addr.SetAddressToDefault();
302
303 ByteString dst_bytes(default_addr.address());
Paul Stewartf748a362012-03-07 12:01:20 -0800304 if (message.HasAttribute(RTA_DST)) {
305 dst_bytes = message.GetAttribute(RTA_DST);
Paul Stewart75e89d22011-08-01 10:00:02 -0700306 }
307 ByteString src_bytes(default_addr.address());
Paul Stewartf748a362012-03-07 12:01:20 -0800308 if (message.HasAttribute(RTA_SRC)) {
309 src_bytes = message.GetAttribute(RTA_SRC);
Paul Stewart75e89d22011-08-01 10:00:02 -0700310 }
311 ByteString gateway_bytes(default_addr.address());
Paul Stewartf748a362012-03-07 12:01:20 -0800312 if (message.HasAttribute(RTA_GATEWAY)) {
313 gateway_bytes = message.GetAttribute(RTA_GATEWAY);
Paul Stewart75e89d22011-08-01 10:00:02 -0700314 }
315
Paul Stewartf748a362012-03-07 12:01:20 -0800316 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
326void 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 Stewarte93b0382012-04-24 13:11:28 -0700334 if (!route_queries_.empty() &&
Paul Stewartf748a362012-03-07 12:01:20 -0800335 message.route_status().protocol == RTPROT_UNSPEC) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700336 SLOG(this, 3) << __func__ << ": Message seq: " << message.seq()
337 << " mode " << message.mode()
338 << ", next query seq: " << route_queries_.front().sequence;
Paul Stewartf748a362012-03-07 12:01:20 -0800339
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 Stewarte93b0382012-04-24 13:11:28 -0700343 while (route_queries_.front().sequence - message.seq() > kuint32max / 2) {
Paul Stewartf748a362012-03-07 12:01:20 -0800344 LOG(ERROR) << __func__ << ": Purging un-replied route request sequence "
Paul Stewarte93b0382012-04-24 13:11:28 -0700345 << route_queries_.front().sequence
Paul Stewartf748a362012-03-07 12:01:20 -0800346 << " (< " << message.seq() << ")";
Darin Petkovabf6d282012-05-08 15:49:05 +0200347 route_queries_.pop_front();
Paul Stewarte93b0382012-04-24 13:11:28 -0700348 if (route_queries_.empty())
Paul Stewartf748a362012-03-07 12:01:20 -0800349 return;
350 }
351
Darin Petkovabf6d282012-05-08 15:49:05 +0200352 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 Silbersteinc9c31d82014-10-21 15:01:00 -0700359 SLOG(this, 2) << __func__ << ": Ignoring route result with no gateway "
360 << "since we don't need to plumb these.";
Paul Stewartbbed76d2012-04-27 20:02:13 -0700361 } else {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700362 SLOG(this, 2) << __func__ << ": Adding host route to "
363 << add_entry.dst.ToString();
Darin Petkovabf6d282012-05-08 15:49:05 +0200364 added = AddRoute(interface_index, add_entry);
Paul Stewartbbed76d2012-04-27 20:02:13 -0700365 }
Darin Petkovabf6d282012-05-08 15:49:05 +0200366 if (added && !query.callback.is_null()) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700367 SLOG(this, 2) << "Running query callback.";
Darin Petkovabf6d282012-05-08 15:49:05 +0200368 query.callback.Run(interface_index, add_entry);
369 }
370 route_queries_.pop_front();
Paul Stewartf748a362012-03-07 12:01:20 -0800371 }
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 Stewart75e89d22011-08-01 10:00:02 -0700379
Paul Stewart6db7b242014-05-02 15:34:21 -0700380 TableEntryVector &table = tables_[interface_index];
381 for (auto nent = table.begin(); nent != table.end(); ++nent) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700382 if (nent->dst.Equals(entry.dst) &&
Paul Stewart75e89d22011-08-01 10:00:02 -0700383 nent->src.Equals(entry.src) &&
Paul Stewart75e89d22011-08-01 10:00:02 -0700384 nent->gateway.Equals(entry.gateway) &&
385 nent->scope == entry.scope) {
Paul Stewartf748a362012-03-07 12:01:20 -0800386 if (message.mode() == RTNLMessage::kModeDelete &&
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800387 nent->metric == entry.metric) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700388 table.erase(nent);
Paul Stewartf748a362012-03-07 12:01:20 -0800389 } else if (message.mode() == RTNLMessage::kModeAdd) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700390 nent->from_rtnl = true;
391 nent->metric = entry.metric;
392 }
393 return;
394 }
395 }
396
Paul Stewartf748a362012-03-07 12:01:20 -0800397 if (message.mode() == RTNLMessage::kModeAdd) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700398 SLOG(this, 2) << __func__ << " adding"
399 << " destination " << entry.dst.ToString()
400 << " index " << interface_index
401 << " gateway " << entry.gateway.ToString()
402 << " metric " << entry.metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700403 table.push_back(entry);
404 }
405}
406
Ben Chan7fab8972014-08-10 17:14:46 -0700407bool RoutingTable::ApplyRoute(uint32_t interface_index,
Paul Stewart75e89d22011-08-01 10:00:02 -0700408 const RoutingTableEntry &entry,
Paul Stewart9a908082011-08-31 12:18:48 -0700409 RTNLMessage::Mode mode,
Paul Stewart75e89d22011-08-01 10:00:02 -0700410 unsigned int flags) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700411 SLOG(this, 2) << base::StringPrintf(
Ben Chanfad4a0b2012-04-18 15:49:59 -0700412 "%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 Stewart75e89d22011-08-01 10:00:02 -0700416
Paul Stewartf748a362012-03-07 12:01:20 -0800417 RTNLMessage message(
Paul Stewart9a908082011-08-31 12:18:48 -0700418 RTNLMessage::kTypeRoute,
Paul Stewart75e89d22011-08-01 10:00:02 -0700419 mode,
Paul Stewarte6132022011-08-16 09:11:02 -0700420 NLM_F_REQUEST | flags,
Paul Stewart75e89d22011-08-01 10:00:02 -0700421 0,
422 0,
423 0,
424 entry.dst.family());
425
Paul Stewartf748a362012-03-07 12:01:20 -0800426 message.set_route_status(RTNLMessage::RouteStatus(
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700427 entry.dst.prefix(),
428 entry.src.prefix(),
Paul Stewart75e89d22011-08-01 10:00:02 -0700429 RT_TABLE_MAIN,
430 RTPROT_BOOT,
431 entry.scope,
432 RTN_UNICAST,
433 0));
434
Paul Stewartf748a362012-03-07 12:01:20 -0800435 message.SetAttribute(RTA_DST, entry.dst.address());
Paul Stewart75e89d22011-08-01 10:00:02 -0700436 if (!entry.src.IsDefault()) {
Paul Stewartf748a362012-03-07 12:01:20 -0800437 message.SetAttribute(RTA_SRC, entry.src.address());
Paul Stewart75e89d22011-08-01 10:00:02 -0700438 }
439 if (!entry.gateway.IsDefault()) {
Paul Stewartf748a362012-03-07 12:01:20 -0800440 message.SetAttribute(RTA_GATEWAY, entry.gateway.address());
Paul Stewart75e89d22011-08-01 10:00:02 -0700441 }
Paul Stewartf748a362012-03-07 12:01:20 -0800442 message.SetAttribute(RTA_PRIORITY,
443 ByteString::CreateFromCPUUInt32(entry.metric));
444 message.SetAttribute(RTA_OIF,
445 ByteString::CreateFromCPUUInt32(interface_index));
Paul Stewart75e89d22011-08-01 10:00:02 -0700446
Paul Stewartf748a362012-03-07 12:01:20 -0800447 return rtnl_handler_->SendMessage(&message);
Paul Stewart75e89d22011-08-01 10:00:02 -0700448}
449
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800450// 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 Chan7fab8972014-08-10 17:14:46 -0700456void RoutingTable::ReplaceMetric(uint32_t interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -0800457 RoutingTableEntry *entry,
Ben Chan7fab8972014-08-10 17:14:46 -0700458 uint32_t metric) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700459 SLOG(this, 2) << __func__ << " index " << interface_index
460 << " metric " << metric;
mukesh agrawald4ef6772012-02-21 16:28:04 -0800461 RoutingTableEntry new_entry = *entry;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800462 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 agrawald4ef6772012-02-21 16:28:04 -0800467 ApplyRoute(interface_index, *entry, RTNLMessage::kModeDelete, 0);
468 // Now, update our routing table (via |*entry|) from |new_entry|.
469 *entry = new_entry;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800470}
471
Paul Stewart75e89d22011-08-01 10:00:02 -0700472bool RoutingTable::FlushCache() {
473 static const char *kPaths[2] = { kRouteFlushPath4, kRouteFlushPath6 };
474 bool ret = true;
475
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700476 SLOG(this, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -0700477
478 for (size_t i = 0; i < arraysize(kPaths); ++i) {
Ben Chan6fbf64f2014-05-21 18:07:01 -0700479 if (base::WriteFile(FilePath(kPaths[i]), "-1", 2) != 2) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700480 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 Stewartf748a362012-03-07 12:01:20 -0800489bool RoutingTable::RequestRouteToHost(const IPAddress &address,
Paul Stewarte93b0382012-04-24 13:11:28 -0700490 int interface_index,
Darin Petkovabf6d282012-05-08 15:49:05 +0200491 int tag,
492 const Query::Callback &callback) {
Paul Stewarte78ec542012-06-08 18:28:50 -0700493 // Make sure we don't get a cached response that is no longer valid.
494 FlushCache();
495
Paul Stewartf748a362012-03-07 12:01:20 -0800496 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 Stewart536820d2012-03-19 16:05:59 -0700509
510 if (interface_index != -1) {
511 message.SetAttribute(RTA_OIF,
512 ByteString::CreateFromCPUUInt32(interface_index));
513 }
Paul Stewartf748a362012-03-07 12:01:20 -0800514
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 Petkovabf6d282012-05-08 15:49:05 +0200521 route_queries_.push_back(Query(message.seq(), tag, callback));
Paul Stewartf748a362012-03-07 12:01:20 -0800522
523 return true;
524}
525
Ben Chana0163122012-09-25 15:10:52 -0700526bool RoutingTable::CreateBlackholeRoute(int interface_index,
Ben Chana6bfe872012-09-26 09:48:34 -0700527 IPAddress::Family family,
Ben Chan7fab8972014-08-10 17:14:46 -0700528 uint32_t metric) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700529 SLOG(this, 2) << base::StringPrintf(
Ben Chana0163122012-09-25 15:10:52 -0700530 "%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 Stewart4a6748d2012-07-17 14:31:36 -0700560bool 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 Silbersteinc9c31d82014-10-21 15:01:00 -0700575 SLOG(this, 2) << "Creating link route to " << destination_address.ToString()
576 << " from " << local_address.ToString()
577 << " on interface index " << interface_index;
Paul Stewart4a6748d2012-07-17 14:31:36 -0700578 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 Stewart75e89d22011-08-01 10:00:02 -0700587} // namespace shill