blob: 17fc9e978d60295828f2edd8294888944c1f3b33 [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>
12#include <net/if.h>
13#include <net/if_arp.h>
14#include <string.h>
15#include <sys/socket.h>
16#include <time.h>
17#include <unistd.h>
18
19#include <string>
20
Eric Shienbrood3e20a232012-02-16 11:35:56 -050021#include <base/bind.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070022#include <base/file_path.h>
23#include <base/file_util.h>
24#include <base/hash_tables.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070025#include <base/memory/scoped_ptr.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050026#include <base/stl_util.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070027#include <base/stringprintf.h>
28
29#include "shill/byte_string.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070030#include "shill/logging.h"
Paul Stewart75e89d22011-08-01 10:00:02 -070031#include "shill/routing_table_entry.h"
32#include "shill/rtnl_handler.h"
33#include "shill/rtnl_listener.h"
34#include "shill/rtnl_message.h"
35
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
Ben Chanbbdef5f2012-04-23 13:58:15 -070045namespace {
46base::LazyInstance<RoutingTable> g_routing_table = LAZY_INSTANCE_INITIALIZER;
47} // namespace
Paul Stewart0d2ada32011-08-09 17:01:57 -070048
Paul Stewart75e89d22011-08-01 10:00:02 -070049// static
50const char RoutingTable::kRouteFlushPath4[] = "/proc/sys/net/ipv4/route/flush";
Paul Stewart0d2ada32011-08-09 17:01:57 -070051// static
Paul Stewart75e89d22011-08-01 10:00:02 -070052const char RoutingTable::kRouteFlushPath6[] = "/proc/sys/net/ipv6/route/flush";
53
54RoutingTable::RoutingTable()
Eric Shienbrood3e20a232012-02-16 11:35:56 -050055 : route_callback_(Bind(&RoutingTable::RouteMsgHandler, Unretained(this))),
Paul Stewartf748a362012-03-07 12:01:20 -080056 route_listener_(NULL),
57 rtnl_handler_(RTNLHandler::GetInstance()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070058 SLOG(Route, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -070059}
60
61RoutingTable::~RoutingTable() {}
62
63RoutingTable* RoutingTable::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070064 return g_routing_table.Pointer();
Paul Stewart75e89d22011-08-01 10:00:02 -070065}
66
67void RoutingTable::Start() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070068 SLOG(Route, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -070069
70 route_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -050071 new RTNLListener(RTNLHandler::kRequestRoute, route_callback_));
Paul Stewartf748a362012-03-07 12:01:20 -080072 rtnl_handler_->RequestDump(RTNLHandler::kRequestRoute);
Paul Stewart75e89d22011-08-01 10:00:02 -070073}
74
75void RoutingTable::Stop() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070076 SLOG(Route, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -070077
78 route_listener_.reset();
79}
80
81bool RoutingTable::AddRoute(int interface_index,
82 const RoutingTableEntry &entry) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070083 SLOG(Route, 2) << __func__ << ": "
84 << "destination " << entry.dst.ToString()
85 << " index " << interface_index
86 << " gateway " << entry.gateway.ToString()
87 << " metric " << entry.metric;
Paul Stewart75e89d22011-08-01 10:00:02 -070088
89 CHECK(!entry.from_rtnl);
90 if (!ApplyRoute(interface_index,
91 entry,
Paul Stewart9a908082011-08-31 12:18:48 -070092 RTNLMessage::kModeAdd,
Paul Stewart75e89d22011-08-01 10:00:02 -070093 NLM_F_CREATE | NLM_F_EXCL)) {
94 return false;
95 }
96 tables_[interface_index].push_back(entry);
97 return true;
98}
99
100bool RoutingTable::GetDefaultRoute(int interface_index,
101 IPAddress::Family family,
102 RoutingTableEntry *entry) {
mukesh agrawald4ef6772012-02-21 16:28:04 -0800103 RoutingTableEntry *found_entry;
104 bool ret = GetDefaultRouteInternal(interface_index, family, &found_entry);
105 if (ret) {
106 *entry = *found_entry;
107 }
108 return ret;
109}
110
111bool RoutingTable::GetDefaultRouteInternal(int interface_index,
112 IPAddress::Family family,
113 RoutingTableEntry **entry) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700114 SLOG(Route, 2) << __func__ << " index " << interface_index
115 << " family " << IPAddress::GetAddressFamilyName(family);
Paul Stewart75e89d22011-08-01 10:00:02 -0700116
117 base::hash_map<int, vector<RoutingTableEntry> >::iterator table =
118 tables_.find(interface_index);
119
120 if (table == tables_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700121 SLOG(Route, 2) << __func__ << " no table";
Paul Stewart75e89d22011-08-01 10:00:02 -0700122 return false;
123 }
124
125 vector<RoutingTableEntry>::iterator nent;
126
127 for (nent = table->second.begin(); nent != table->second.end(); ++nent) {
128 if (nent->dst.IsDefault() && nent->dst.family() == family) {
mukesh agrawald4ef6772012-02-21 16:28:04 -0800129 *entry = &(*nent);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700130 SLOG(Route, 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
Ben Chanfad4a0b2012-04-18 15:49:59 -0700137 SLOG(Route, 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,
Paul Stewart75e89d22011-08-01 10:00:02 -0700143 uint32 metric) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700144 SLOG(Route, 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,
180 uint32 metric) {
181 bool ret = true;
182
183 IPAddress::Family address_family = ipconfig->properties().address_family;
184 const vector<IPConfig::Route> &routes = ipconfig->properties().routes;
185
186 for (vector<IPConfig::Route>::const_iterator it = routes.begin();
187 it != routes.end();
188 ++it) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700189 SLOG(Route, 3) << "Installing route:"
190 << " Destination: " << it->host
191 << " Netmask: " << it->netmask
192 << " Gateway: " << it->gateway;
Paul Stewart3f68bb12012-03-15 13:33:10 -0700193 IPAddress destination_address(address_family);
194 IPAddress source_address(address_family); // Left as default.
195 IPAddress gateway_address(address_family);
196 if (!destination_address.SetAddressFromString(it->host)) {
197 LOG(ERROR) << "Failed to parse host "
198 << it->host;
199 ret = false;
200 continue;
201 }
202 if (!gateway_address.SetAddressFromString(it->gateway)) {
203 LOG(ERROR) << "Failed to parse gateway "
204 << it->gateway;
205 ret = false;
206 continue;
207 }
208 destination_address.set_prefix(
209 IPAddress::GetPrefixLengthFromMask(address_family, it->netmask));
210 if (!AddRoute(interface_index,
211 RoutingTableEntry(destination_address,
212 source_address,
213 gateway_address,
214 metric,
215 RT_SCOPE_UNIVERSE,
216 false))) {
217 ret = false;
218 }
219 }
220 return ret;
221}
222
Thieu Lefb46caf2012-03-08 11:57:15 -0800223void RoutingTable::FlushRoutes(int interface_index) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700224 SLOG(Route, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -0700225
226 base::hash_map<int, vector<RoutingTableEntry> >::iterator table =
227 tables_.find(interface_index);
228
229 if (table == tables_.end()) {
230 return;
231 }
232
233 vector<RoutingTableEntry>::iterator nent;
234
235 for (nent = table->second.begin(); nent != table->second.end(); ++nent) {
Darin Petkovabf6d282012-05-08 15:49:05 +0200236 ApplyRoute(interface_index, *nent, RTNLMessage::kModeDelete, 0);
Paul Stewart75e89d22011-08-01 10:00:02 -0700237 }
Thieu Lefb46caf2012-03-08 11:57:15 -0800238 table->second.clear();
Paul Stewart75e89d22011-08-01 10:00:02 -0700239}
240
Paul Stewarte93b0382012-04-24 13:11:28 -0700241void RoutingTable::FlushRoutesWithTag(int tag) {
242 SLOG(Route, 2) << __func__;
243
244 base::hash_map<int, vector<RoutingTableEntry> >::iterator table;
245 for (table = tables_.begin(); table != tables_.end(); ++table) {
246 vector<RoutingTableEntry>::iterator nent;
247
248 for (nent = table->second.begin(); nent != table->second.end();) {
249 if (nent->tag == tag) {
250 ApplyRoute(table->first, *nent, RTNLMessage::kModeDelete, 0);
251 nent = table->second.erase(nent);
252 } else {
253 ++nent;
254 }
255 }
256 }
257}
258
Paul Stewart75e89d22011-08-01 10:00:02 -0700259void RoutingTable::ResetTable(int interface_index) {
260 tables_.erase(interface_index);
261}
262
263void RoutingTable::SetDefaultMetric(int interface_index, uint32 metric) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700264 SLOG(Route, 2) << __func__ << " index " << interface_index
265 << " metric " << metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700266
mukesh agrawald4ef6772012-02-21 16:28:04 -0800267 RoutingTableEntry *entry;
268 if (GetDefaultRouteInternal(
269 interface_index, IPAddress::kFamilyIPv4, &entry) &&
270 entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800271 ReplaceMetric(interface_index, entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700272 }
273
mukesh agrawald4ef6772012-02-21 16:28:04 -0800274 if (GetDefaultRouteInternal(
275 interface_index, IPAddress::kFamilyIPv6, &entry) &&
276 entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800277 ReplaceMetric(interface_index, entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700278 }
279}
280
Paul Stewartf748a362012-03-07 12:01:20 -0800281// static
282bool RoutingTable::ParseRoutingTableMessage(const RTNLMessage &message,
283 int *interface_index,
284 RoutingTableEntry *entry) {
285 if (message.type() != RTNLMessage::kTypeRoute ||
286 message.family() == IPAddress::kFamilyUnknown ||
287 !message.HasAttribute(RTA_OIF)) {
288 return false;
Paul Stewart75e89d22011-08-01 10:00:02 -0700289 }
290
Paul Stewartf748a362012-03-07 12:01:20 -0800291 const RTNLMessage::RouteStatus &route_status = message.route_status();
Paul Stewart75e89d22011-08-01 10:00:02 -0700292
293 if (route_status.type != RTN_UNICAST ||
Paul Stewart75e89d22011-08-01 10:00:02 -0700294 route_status.table != RT_TABLE_MAIN) {
Paul Stewartf748a362012-03-07 12:01:20 -0800295 return false;
Paul Stewart75e89d22011-08-01 10:00:02 -0700296 }
297
Paul Stewartf748a362012-03-07 12:01:20 -0800298 uint32 interface_index_u32 = 0;
299 if (!message.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&interface_index_u32)) {
300 return false;
Paul Stewart75e89d22011-08-01 10:00:02 -0700301 }
Paul Stewartf748a362012-03-07 12:01:20 -0800302 *interface_index = interface_index_u32;
Paul Stewart75e89d22011-08-01 10:00:02 -0700303
304 uint32 metric = 0;
Paul Stewartf748a362012-03-07 12:01:20 -0800305 if (message.HasAttribute(RTA_PRIORITY)) {
306 message.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700307 }
308
Paul Stewartf748a362012-03-07 12:01:20 -0800309 IPAddress default_addr(message.family());
Paul Stewart75e89d22011-08-01 10:00:02 -0700310 default_addr.SetAddressToDefault();
311
312 ByteString dst_bytes(default_addr.address());
Paul Stewartf748a362012-03-07 12:01:20 -0800313 if (message.HasAttribute(RTA_DST)) {
314 dst_bytes = message.GetAttribute(RTA_DST);
Paul Stewart75e89d22011-08-01 10:00:02 -0700315 }
316 ByteString src_bytes(default_addr.address());
Paul Stewartf748a362012-03-07 12:01:20 -0800317 if (message.HasAttribute(RTA_SRC)) {
318 src_bytes = message.GetAttribute(RTA_SRC);
Paul Stewart75e89d22011-08-01 10:00:02 -0700319 }
320 ByteString gateway_bytes(default_addr.address());
Paul Stewartf748a362012-03-07 12:01:20 -0800321 if (message.HasAttribute(RTA_GATEWAY)) {
322 gateway_bytes = message.GetAttribute(RTA_GATEWAY);
Paul Stewart75e89d22011-08-01 10:00:02 -0700323 }
324
Paul Stewartf748a362012-03-07 12:01:20 -0800325 entry->dst = IPAddress(message.family(), dst_bytes, route_status.dst_prefix);
326 entry->src = IPAddress(message.family(), src_bytes, route_status.src_prefix);
327 entry->gateway = IPAddress(message.family(), gateway_bytes);
328 entry->metric = metric;
329 entry->scope = route_status.scope;
330 entry->from_rtnl = true;
331
332 return true;
333}
334
335void RoutingTable::RouteMsgHandler(const RTNLMessage &message) {
336 int interface_index;
337 RoutingTableEntry entry;
338
339 if (!ParseRoutingTableMessage(message, &interface_index, &entry)) {
340 return;
341 }
342
Paul Stewarte93b0382012-04-24 13:11:28 -0700343 if (!route_queries_.empty() &&
Paul Stewartf748a362012-03-07 12:01:20 -0800344 message.route_status().protocol == RTPROT_UNSPEC) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700345 SLOG(Route, 3) << __func__ << ": Message seq: " << message.seq()
346 << " mode " << message.mode()
Paul Stewarte93b0382012-04-24 13:11:28 -0700347 << ", next query seq: " << route_queries_.front().sequence;
Paul Stewartf748a362012-03-07 12:01:20 -0800348
349 // Purge queries that have expired (sequence number of this message is
350 // greater than that of the head of the route query sequence). Do the
351 // math in a way that's roll-over independent.
Paul Stewarte93b0382012-04-24 13:11:28 -0700352 while (route_queries_.front().sequence - message.seq() > kuint32max / 2) {
Paul Stewartf748a362012-03-07 12:01:20 -0800353 LOG(ERROR) << __func__ << ": Purging un-replied route request sequence "
Paul Stewarte93b0382012-04-24 13:11:28 -0700354 << route_queries_.front().sequence
Paul Stewartf748a362012-03-07 12:01:20 -0800355 << " (< " << message.seq() << ")";
Darin Petkovabf6d282012-05-08 15:49:05 +0200356 route_queries_.pop_front();
Paul Stewarte93b0382012-04-24 13:11:28 -0700357 if (route_queries_.empty())
Paul Stewartf748a362012-03-07 12:01:20 -0800358 return;
359 }
360
Darin Petkovabf6d282012-05-08 15:49:05 +0200361 const Query &query = route_queries_.front();
362 if (query.sequence == message.seq()) {
363 RoutingTableEntry add_entry(entry);
364 add_entry.from_rtnl = false;
365 add_entry.tag = query.tag;
366 bool added = true;
367 if (add_entry.gateway.IsDefault()) {
Paul Stewartbbed76d2012-04-27 20:02:13 -0700368 SLOG(Route, 2) << __func__ << ": Ignoring route result with no gateway "
369 << "since we don't need to plumb these.";
370 } else {
371 SLOG(Route, 2) << __func__ << ": Adding host route to "
Darin Petkovabf6d282012-05-08 15:49:05 +0200372 << add_entry.dst.ToString();
373 added = AddRoute(interface_index, add_entry);
Paul Stewartbbed76d2012-04-27 20:02:13 -0700374 }
Darin Petkovabf6d282012-05-08 15:49:05 +0200375 if (added && !query.callback.is_null()) {
376 SLOG(Route, 2) << "Running query callback.";
377 query.callback.Run(interface_index, add_entry);
378 }
379 route_queries_.pop_front();
Paul Stewartf748a362012-03-07 12:01:20 -0800380 }
381 return;
382 } else if (message.route_status().protocol != RTPROT_BOOT) {
383 // Responses to route queries come back with a protocol of
384 // RTPROT_UNSPEC. Otherwise, normal route updates that we are
385 // interested in come with a protocol of RTPROT_BOOT.
386 return;
387 }
Paul Stewart75e89d22011-08-01 10:00:02 -0700388
389 vector<RoutingTableEntry> &table = tables_[interface_index];
390 vector<RoutingTableEntry>::iterator nent;
391 for (nent = table.begin(); nent != table.end(); ++nent) {
392 if (nent->dst.Equals(entry.dst) &&
Paul Stewart75e89d22011-08-01 10:00:02 -0700393 nent->src.Equals(entry.src) &&
Paul Stewart75e89d22011-08-01 10:00:02 -0700394 nent->gateway.Equals(entry.gateway) &&
395 nent->scope == entry.scope) {
Paul Stewartf748a362012-03-07 12:01:20 -0800396 if (message.mode() == RTNLMessage::kModeDelete &&
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800397 nent->metric == entry.metric) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700398 table.erase(nent);
Paul Stewartf748a362012-03-07 12:01:20 -0800399 } else if (message.mode() == RTNLMessage::kModeAdd) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700400 nent->from_rtnl = true;
401 nent->metric = entry.metric;
402 }
403 return;
404 }
405 }
406
Paul Stewartf748a362012-03-07 12:01:20 -0800407 if (message.mode() == RTNLMessage::kModeAdd) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700408 SLOG(Route, 2) << __func__ << " adding"
409 << " destination " << entry.dst.ToString()
410 << " index " << interface_index
411 << " gateway " << entry.gateway.ToString()
412 << " metric " << entry.metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700413 table.push_back(entry);
414 }
415}
416
417bool RoutingTable::ApplyRoute(uint32 interface_index,
418 const RoutingTableEntry &entry,
Paul Stewart9a908082011-08-31 12:18:48 -0700419 RTNLMessage::Mode mode,
Paul Stewart75e89d22011-08-01 10:00:02 -0700420 unsigned int flags) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700421 SLOG(Route, 2) << base::StringPrintf(
422 "%s: dst %s/%d src %s/%d index %d mode %d flags 0x%x",
423 __func__, entry.dst.ToString().c_str(), entry.dst.prefix(),
424 entry.src.ToString().c_str(), entry.src.prefix(),
425 interface_index, mode, flags);
Paul Stewart75e89d22011-08-01 10:00:02 -0700426
Paul Stewartf748a362012-03-07 12:01:20 -0800427 RTNLMessage message(
Paul Stewart9a908082011-08-31 12:18:48 -0700428 RTNLMessage::kTypeRoute,
Paul Stewart75e89d22011-08-01 10:00:02 -0700429 mode,
Paul Stewarte6132022011-08-16 09:11:02 -0700430 NLM_F_REQUEST | flags,
Paul Stewart75e89d22011-08-01 10:00:02 -0700431 0,
432 0,
433 0,
434 entry.dst.family());
435
Paul Stewartf748a362012-03-07 12:01:20 -0800436 message.set_route_status(RTNLMessage::RouteStatus(
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700437 entry.dst.prefix(),
438 entry.src.prefix(),
Paul Stewart75e89d22011-08-01 10:00:02 -0700439 RT_TABLE_MAIN,
440 RTPROT_BOOT,
441 entry.scope,
442 RTN_UNICAST,
443 0));
444
Paul Stewartf748a362012-03-07 12:01:20 -0800445 message.SetAttribute(RTA_DST, entry.dst.address());
Paul Stewart75e89d22011-08-01 10:00:02 -0700446 if (!entry.src.IsDefault()) {
Paul Stewartf748a362012-03-07 12:01:20 -0800447 message.SetAttribute(RTA_SRC, entry.src.address());
Paul Stewart75e89d22011-08-01 10:00:02 -0700448 }
449 if (!entry.gateway.IsDefault()) {
Paul Stewartf748a362012-03-07 12:01:20 -0800450 message.SetAttribute(RTA_GATEWAY, entry.gateway.address());
Paul Stewart75e89d22011-08-01 10:00:02 -0700451 }
Paul Stewartf748a362012-03-07 12:01:20 -0800452 message.SetAttribute(RTA_PRIORITY,
453 ByteString::CreateFromCPUUInt32(entry.metric));
454 message.SetAttribute(RTA_OIF,
455 ByteString::CreateFromCPUUInt32(interface_index));
Paul Stewart75e89d22011-08-01 10:00:02 -0700456
Paul Stewartf748a362012-03-07 12:01:20 -0800457 return rtnl_handler_->SendMessage(&message);
Paul Stewart75e89d22011-08-01 10:00:02 -0700458}
459
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800460// Somewhat surprisingly, the kernel allows you to create multiple routes
461// to the same destination through the same interface with different metrics.
462// Therefore, to change the metric on a route, we can't just use the
463// NLM_F_REPLACE flag by itself. We have to explicitly remove the old route.
464// We do so after creating the route at a new metric so there is no traffic
465// disruption to existing network streams.
466void RoutingTable::ReplaceMetric(uint32 interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -0800467 RoutingTableEntry *entry,
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800468 uint32 metric) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700469 SLOG(Route, 2) << __func__ << " index " << interface_index
470 << " metric " << metric;
mukesh agrawald4ef6772012-02-21 16:28:04 -0800471 RoutingTableEntry new_entry = *entry;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800472 new_entry.metric = metric;
473 // First create the route at the new metric.
474 ApplyRoute(interface_index, new_entry, RTNLMessage::kModeAdd,
475 NLM_F_CREATE | NLM_F_REPLACE);
476 // Then delete the route at the old metric.
mukesh agrawald4ef6772012-02-21 16:28:04 -0800477 ApplyRoute(interface_index, *entry, RTNLMessage::kModeDelete, 0);
478 // Now, update our routing table (via |*entry|) from |new_entry|.
479 *entry = new_entry;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800480}
481
Paul Stewart75e89d22011-08-01 10:00:02 -0700482bool RoutingTable::FlushCache() {
483 static const char *kPaths[2] = { kRouteFlushPath4, kRouteFlushPath6 };
484 bool ret = true;
485
Ben Chanfad4a0b2012-04-18 15:49:59 -0700486 SLOG(Route, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -0700487
488 for (size_t i = 0; i < arraysize(kPaths); ++i) {
489 if (file_util::WriteFile(FilePath(kPaths[i]), "-1", 2) != 2) {
490 LOG(ERROR) << base::StringPrintf("Cannot write to route flush file %s",
491 kPaths[i]);
492 ret = false;
493 }
494 }
495
496 return ret;
497}
498
Paul Stewartf748a362012-03-07 12:01:20 -0800499bool RoutingTable::RequestRouteToHost(const IPAddress &address,
Paul Stewarte93b0382012-04-24 13:11:28 -0700500 int interface_index,
Darin Petkovabf6d282012-05-08 15:49:05 +0200501 int tag,
502 const Query::Callback &callback) {
Paul Stewarte78ec542012-06-08 18:28:50 -0700503 // Make sure we don't get a cached response that is no longer valid.
504 FlushCache();
505
Paul Stewartf748a362012-03-07 12:01:20 -0800506 RTNLMessage message(
507 RTNLMessage::kTypeRoute,
508 RTNLMessage::kModeQuery,
509 NLM_F_REQUEST,
510 0,
511 0,
512 interface_index,
513 address.family());
514
515 RTNLMessage::RouteStatus status;
516 status.dst_prefix = address.prefix();
517 message.set_route_status(status);
518 message.SetAttribute(RTA_DST, address.address());
Paul Stewart536820d2012-03-19 16:05:59 -0700519
520 if (interface_index != -1) {
521 message.SetAttribute(RTA_OIF,
522 ByteString::CreateFromCPUUInt32(interface_index));
523 }
Paul Stewartf748a362012-03-07 12:01:20 -0800524
525 if (!rtnl_handler_->SendMessage(&message)) {
526 return false;
527 }
528
529 // Save the sequence number of the request so we can create a route for
530 // this host when we get a reply.
Darin Petkovabf6d282012-05-08 15:49:05 +0200531 route_queries_.push_back(Query(message.seq(), tag, callback));
Paul Stewartf748a362012-03-07 12:01:20 -0800532
533 return true;
534}
535
Ben Chana0163122012-09-25 15:10:52 -0700536bool RoutingTable::CreateBlackholeRoute(int interface_index,
Ben Chana6bfe872012-09-26 09:48:34 -0700537 IPAddress::Family family,
Ben Chana0163122012-09-25 15:10:52 -0700538 uint32 metric) {
Ben Chana0163122012-09-25 15:10:52 -0700539 SLOG(Route, 2) << base::StringPrintf(
540 "%s: index %d family %s metric %d",
541 __func__, interface_index,
542 IPAddress::GetAddressFamilyName(family).c_str(), metric);
543
544 RTNLMessage message(
545 RTNLMessage::kTypeRoute,
546 RTNLMessage::kModeAdd,
547 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL,
548 0,
549 0,
550 0,
551 family);
552
553 message.set_route_status(RTNLMessage::RouteStatus(
554 0,
555 0,
556 RT_TABLE_MAIN,
557 RTPROT_BOOT,
558 RT_SCOPE_UNIVERSE,
559 RTN_BLACKHOLE,
560 0));
561
562 message.SetAttribute(RTA_PRIORITY,
563 ByteString::CreateFromCPUUInt32(metric));
564 message.SetAttribute(RTA_OIF,
565 ByteString::CreateFromCPUUInt32(interface_index));
566
567 return rtnl_handler_->SendMessage(&message);
568}
569
Paul Stewart4a6748d2012-07-17 14:31:36 -0700570bool RoutingTable::CreateLinkRoute(int interface_index,
571 const IPAddress &local_address,
572 const IPAddress &remote_address) {
573 if (!local_address.CanReachAddress(remote_address)) {
574 LOG(ERROR) << __func__ << " failed: "
575 << remote_address.ToString() << " is not reachable from "
576 << local_address.ToString();
577 return false;
578 }
579
580 IPAddress default_address(local_address.family());
581 default_address.SetAddressToDefault();
582 IPAddress destination_address(remote_address);
583 destination_address.set_prefix(
584 IPAddress::GetMaxPrefixLength(remote_address.family()));
585 SLOG(Route, 2) << "Creating link route to " << destination_address.ToString()
586 << " from " << local_address.ToString()
587 << " on interface index " << interface_index;
588 return AddRoute(interface_index,
589 RoutingTableEntry(destination_address,
590 local_address,
591 default_address,
592 0,
593 RT_SCOPE_LINK,
594 false));
595}
596
Paul Stewart75e89d22011-08-01 10:00:02 -0700597} // namespace shill