blob: e227b075e629af7743ab34084724984ca3b5d9b9 [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>
25#include <base/logging.h>
26#include <base/memory/scoped_ptr.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050027#include <base/stl_util.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070028#include <base/stringprintf.h>
29
30#include "shill/byte_string.h"
31#include "shill/routing_table_entry.h"
32#include "shill/rtnl_handler.h"
33#include "shill/rtnl_listener.h"
34#include "shill/rtnl_message.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070035#include "shill/scope_logger.h"
Paul Stewart75e89d22011-08-01 10:00:02 -070036
Eric Shienbrood3e20a232012-02-16 11:35:56 -050037using base::Bind;
38using base::Unretained;
Paul Stewart75e89d22011-08-01 10:00:02 -070039using std::string;
40using std::vector;
41
42namespace shill {
43
Ben Chanbbdef5f2012-04-23 13:58:15 -070044namespace {
45base::LazyInstance<RoutingTable> g_routing_table = LAZY_INSTANCE_INITIALIZER;
46} // namespace
Paul Stewart0d2ada32011-08-09 17:01:57 -070047
Paul Stewart75e89d22011-08-01 10:00:02 -070048// static
49const char RoutingTable::kRouteFlushPath4[] = "/proc/sys/net/ipv4/route/flush";
Paul Stewart0d2ada32011-08-09 17:01:57 -070050// static
Paul Stewart75e89d22011-08-01 10:00:02 -070051const char RoutingTable::kRouteFlushPath6[] = "/proc/sys/net/ipv6/route/flush";
52
53RoutingTable::RoutingTable()
Eric Shienbrood3e20a232012-02-16 11:35:56 -050054 : route_callback_(Bind(&RoutingTable::RouteMsgHandler, Unretained(this))),
Paul Stewartf748a362012-03-07 12:01:20 -080055 route_listener_(NULL),
56 rtnl_handler_(RTNLHandler::GetInstance()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070057 SLOG(Route, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -070058}
59
60RoutingTable::~RoutingTable() {}
61
62RoutingTable* RoutingTable::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070063 return g_routing_table.Pointer();
Paul Stewart75e89d22011-08-01 10:00:02 -070064}
65
66void RoutingTable::Start() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070067 SLOG(Route, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -070068
69 route_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -050070 new RTNLListener(RTNLHandler::kRequestRoute, route_callback_));
Paul Stewartf748a362012-03-07 12:01:20 -080071 rtnl_handler_->RequestDump(RTNLHandler::kRequestRoute);
Paul Stewart75e89d22011-08-01 10:00:02 -070072}
73
74void RoutingTable::Stop() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070075 SLOG(Route, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -070076
77 route_listener_.reset();
78}
79
80bool RoutingTable::AddRoute(int interface_index,
81 const RoutingTableEntry &entry) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070082 SLOG(Route, 2) << __func__ << ": "
83 << "destination " << entry.dst.ToString()
84 << " index " << interface_index
85 << " gateway " << entry.gateway.ToString()
86 << " metric " << entry.metric;
Paul Stewart75e89d22011-08-01 10:00:02 -070087
88 CHECK(!entry.from_rtnl);
89 if (!ApplyRoute(interface_index,
90 entry,
Paul Stewart9a908082011-08-31 12:18:48 -070091 RTNLMessage::kModeAdd,
Paul Stewart75e89d22011-08-01 10:00:02 -070092 NLM_F_CREATE | NLM_F_EXCL)) {
93 return false;
94 }
95 tables_[interface_index].push_back(entry);
96 return true;
97}
98
99bool RoutingTable::GetDefaultRoute(int interface_index,
100 IPAddress::Family family,
101 RoutingTableEntry *entry) {
mukesh agrawald4ef6772012-02-21 16:28:04 -0800102 RoutingTableEntry *found_entry;
103 bool ret = GetDefaultRouteInternal(interface_index, family, &found_entry);
104 if (ret) {
105 *entry = *found_entry;
106 }
107 return ret;
108}
109
110bool RoutingTable::GetDefaultRouteInternal(int interface_index,
111 IPAddress::Family family,
112 RoutingTableEntry **entry) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700113 SLOG(Route, 2) << __func__ << " index " << interface_index
114 << " family " << IPAddress::GetAddressFamilyName(family);
Paul Stewart75e89d22011-08-01 10:00:02 -0700115
116 base::hash_map<int, vector<RoutingTableEntry> >::iterator table =
117 tables_.find(interface_index);
118
119 if (table == tables_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700120 SLOG(Route, 2) << __func__ << " no table";
Paul Stewart75e89d22011-08-01 10:00:02 -0700121 return false;
122 }
123
124 vector<RoutingTableEntry>::iterator nent;
125
126 for (nent = table->second.begin(); nent != table->second.end(); ++nent) {
127 if (nent->dst.IsDefault() && nent->dst.family() == family) {
mukesh agrawald4ef6772012-02-21 16:28:04 -0800128 *entry = &(*nent);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700129 SLOG(Route, 2) << __func__ << ": found"
130 << " gateway " << nent->gateway.ToString()
131 << " metric " << nent->metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700132 return true;
133 }
134 }
135
Ben Chanfad4a0b2012-04-18 15:49:59 -0700136 SLOG(Route, 2) << __func__ << " no route";
Paul Stewart75e89d22011-08-01 10:00:02 -0700137 return false;
138}
139
140bool RoutingTable::SetDefaultRoute(int interface_index,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700141 const IPAddress &gateway_address,
Paul Stewart75e89d22011-08-01 10:00:02 -0700142 uint32 metric) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700143 SLOG(Route, 2) << __func__ << " index " << interface_index
144 << " metric " << metric;
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800145
mukesh agrawald4ef6772012-02-21 16:28:04 -0800146 RoutingTableEntry *old_entry;
Paul Stewart75e89d22011-08-01 10:00:02 -0700147
mukesh agrawald4ef6772012-02-21 16:28:04 -0800148 if (GetDefaultRouteInternal(interface_index,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700149 gateway_address.family(),
mukesh agrawald4ef6772012-02-21 16:28:04 -0800150 &old_entry)) {
151 if (old_entry->gateway.Equals(gateway_address)) {
152 if (old_entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800153 ReplaceMetric(interface_index, old_entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700154 }
155 return true;
156 } else {
mukesh agrawald4ef6772012-02-21 16:28:04 -0800157 // TODO(quiche): Update internal state as well?
Paul Stewart75e89d22011-08-01 10:00:02 -0700158 ApplyRoute(interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -0800159 *old_entry,
Paul Stewart9a908082011-08-31 12:18:48 -0700160 RTNLMessage::kModeDelete,
Paul Stewart75e89d22011-08-01 10:00:02 -0700161 0);
162 }
163 }
164
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700165 IPAddress default_address(gateway_address.family());
Paul Stewart75e89d22011-08-01 10:00:02 -0700166 default_address.SetAddressToDefault();
167
168 return AddRoute(interface_index,
169 RoutingTableEntry(default_address,
Paul Stewart75e89d22011-08-01 10:00:02 -0700170 default_address,
Paul Stewart75e89d22011-08-01 10:00:02 -0700171 gateway_address,
172 metric,
173 RT_SCOPE_UNIVERSE,
174 false));
175}
176
Paul Stewart3f68bb12012-03-15 13:33:10 -0700177bool RoutingTable::ConfigureRoutes(int interface_index,
178 const IPConfigRefPtr &ipconfig,
179 uint32 metric) {
180 bool ret = true;
181
182 IPAddress::Family address_family = ipconfig->properties().address_family;
183 const vector<IPConfig::Route> &routes = ipconfig->properties().routes;
184
185 for (vector<IPConfig::Route>::const_iterator it = routes.begin();
186 it != routes.end();
187 ++it) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700188 SLOG(Route, 3) << "Installing route:"
189 << " Destination: " << it->host
190 << " Netmask: " << it->netmask
191 << " Gateway: " << it->gateway;
Paul Stewart3f68bb12012-03-15 13:33:10 -0700192 IPAddress destination_address(address_family);
193 IPAddress source_address(address_family); // Left as default.
194 IPAddress gateway_address(address_family);
195 if (!destination_address.SetAddressFromString(it->host)) {
196 LOG(ERROR) << "Failed to parse host "
197 << it->host;
198 ret = false;
199 continue;
200 }
201 if (!gateway_address.SetAddressFromString(it->gateway)) {
202 LOG(ERROR) << "Failed to parse gateway "
203 << it->gateway;
204 ret = false;
205 continue;
206 }
207 destination_address.set_prefix(
208 IPAddress::GetPrefixLengthFromMask(address_family, it->netmask));
209 if (!AddRoute(interface_index,
210 RoutingTableEntry(destination_address,
211 source_address,
212 gateway_address,
213 metric,
214 RT_SCOPE_UNIVERSE,
215 false))) {
216 ret = false;
217 }
218 }
219 return ret;
220}
221
Thieu Lefb46caf2012-03-08 11:57:15 -0800222void RoutingTable::FlushRoutes(int interface_index) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700223 SLOG(Route, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -0700224
225 base::hash_map<int, vector<RoutingTableEntry> >::iterator table =
226 tables_.find(interface_index);
227
228 if (table == tables_.end()) {
229 return;
230 }
231
232 vector<RoutingTableEntry>::iterator nent;
233
234 for (nent = table->second.begin(); nent != table->second.end(); ++nent) {
Thieu Lecaef8932012-02-28 16:06:59 -0800235 ApplyRoute(interface_index, *nent, RTNLMessage::kModeDelete, 0);
Paul Stewart75e89d22011-08-01 10:00:02 -0700236 }
Thieu Lefb46caf2012-03-08 11:57:15 -0800237 table->second.clear();
Paul Stewart75e89d22011-08-01 10:00:02 -0700238}
239
240void RoutingTable::ResetTable(int interface_index) {
241 tables_.erase(interface_index);
242}
243
244void RoutingTable::SetDefaultMetric(int interface_index, uint32 metric) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700245 SLOG(Route, 2) << __func__ << " index " << interface_index
246 << " metric " << metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700247
mukesh agrawald4ef6772012-02-21 16:28:04 -0800248 RoutingTableEntry *entry;
249 if (GetDefaultRouteInternal(
250 interface_index, IPAddress::kFamilyIPv4, &entry) &&
251 entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800252 ReplaceMetric(interface_index, entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700253 }
254
mukesh agrawald4ef6772012-02-21 16:28:04 -0800255 if (GetDefaultRouteInternal(
256 interface_index, IPAddress::kFamilyIPv6, &entry) &&
257 entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800258 ReplaceMetric(interface_index, entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700259 }
260}
261
Paul Stewartf748a362012-03-07 12:01:20 -0800262// static
263bool RoutingTable::ParseRoutingTableMessage(const RTNLMessage &message,
264 int *interface_index,
265 RoutingTableEntry *entry) {
266 if (message.type() != RTNLMessage::kTypeRoute ||
267 message.family() == IPAddress::kFamilyUnknown ||
268 !message.HasAttribute(RTA_OIF)) {
269 return false;
Paul Stewart75e89d22011-08-01 10:00:02 -0700270 }
271
Paul Stewartf748a362012-03-07 12:01:20 -0800272 const RTNLMessage::RouteStatus &route_status = message.route_status();
Paul Stewart75e89d22011-08-01 10:00:02 -0700273
274 if (route_status.type != RTN_UNICAST ||
Paul Stewart75e89d22011-08-01 10:00:02 -0700275 route_status.table != RT_TABLE_MAIN) {
Paul Stewartf748a362012-03-07 12:01:20 -0800276 return false;
Paul Stewart75e89d22011-08-01 10:00:02 -0700277 }
278
Paul Stewartf748a362012-03-07 12:01:20 -0800279 uint32 interface_index_u32 = 0;
280 if (!message.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&interface_index_u32)) {
281 return false;
Paul Stewart75e89d22011-08-01 10:00:02 -0700282 }
Paul Stewartf748a362012-03-07 12:01:20 -0800283 *interface_index = interface_index_u32;
Paul Stewart75e89d22011-08-01 10:00:02 -0700284
285 uint32 metric = 0;
Paul Stewartf748a362012-03-07 12:01:20 -0800286 if (message.HasAttribute(RTA_PRIORITY)) {
287 message.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700288 }
289
Paul Stewartf748a362012-03-07 12:01:20 -0800290 IPAddress default_addr(message.family());
Paul Stewart75e89d22011-08-01 10:00:02 -0700291 default_addr.SetAddressToDefault();
292
293 ByteString dst_bytes(default_addr.address());
Paul Stewartf748a362012-03-07 12:01:20 -0800294 if (message.HasAttribute(RTA_DST)) {
295 dst_bytes = message.GetAttribute(RTA_DST);
Paul Stewart75e89d22011-08-01 10:00:02 -0700296 }
297 ByteString src_bytes(default_addr.address());
Paul Stewartf748a362012-03-07 12:01:20 -0800298 if (message.HasAttribute(RTA_SRC)) {
299 src_bytes = message.GetAttribute(RTA_SRC);
Paul Stewart75e89d22011-08-01 10:00:02 -0700300 }
301 ByteString gateway_bytes(default_addr.address());
Paul Stewartf748a362012-03-07 12:01:20 -0800302 if (message.HasAttribute(RTA_GATEWAY)) {
303 gateway_bytes = message.GetAttribute(RTA_GATEWAY);
Paul Stewart75e89d22011-08-01 10:00:02 -0700304 }
305
Paul Stewartf748a362012-03-07 12:01:20 -0800306 entry->dst = IPAddress(message.family(), dst_bytes, route_status.dst_prefix);
307 entry->src = IPAddress(message.family(), src_bytes, route_status.src_prefix);
308 entry->gateway = IPAddress(message.family(), gateway_bytes);
309 entry->metric = metric;
310 entry->scope = route_status.scope;
311 entry->from_rtnl = true;
312
313 return true;
314}
315
316void RoutingTable::RouteMsgHandler(const RTNLMessage &message) {
317 int interface_index;
318 RoutingTableEntry entry;
319
320 if (!ParseRoutingTableMessage(message, &interface_index, &entry)) {
321 return;
322 }
323
324 if (!route_query_sequences_.empty() &&
325 message.route_status().protocol == RTPROT_UNSPEC) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700326 SLOG(Route, 3) << __func__ << ": Message seq: " << message.seq()
327 << " mode " << message.mode()
328 << ", next query seq: " << route_query_sequences_.front();
Paul Stewartf748a362012-03-07 12:01:20 -0800329
330 // Purge queries that have expired (sequence number of this message is
331 // greater than that of the head of the route query sequence). Do the
332 // math in a way that's roll-over independent.
333 while (route_query_sequences_.front() - message.seq() > kuint32max / 2) {
334 LOG(ERROR) << __func__ << ": Purging un-replied route request sequence "
335 << route_query_sequences_.front()
336 << " (< " << message.seq() << ")";
337 route_query_sequences_.pop();
338 if (route_query_sequences_.empty())
339 return;
340 }
341
342 if (route_query_sequences_.front() == message.seq()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700343 SLOG(Route, 2) << __func__ << ": Adding host route to "
344 << entry.dst.ToString();
Paul Stewartf748a362012-03-07 12:01:20 -0800345 route_query_sequences_.pop();
346 RoutingTableEntry add_entry(entry);
347 add_entry.from_rtnl = false;
348 AddRoute(interface_index, add_entry);
349 }
350 return;
351 } else if (message.route_status().protocol != RTPROT_BOOT) {
352 // Responses to route queries come back with a protocol of
353 // RTPROT_UNSPEC. Otherwise, normal route updates that we are
354 // interested in come with a protocol of RTPROT_BOOT.
355 return;
356 }
Paul Stewart75e89d22011-08-01 10:00:02 -0700357
358 vector<RoutingTableEntry> &table = tables_[interface_index];
359 vector<RoutingTableEntry>::iterator nent;
360 for (nent = table.begin(); nent != table.end(); ++nent) {
361 if (nent->dst.Equals(entry.dst) &&
Paul Stewart75e89d22011-08-01 10:00:02 -0700362 nent->src.Equals(entry.src) &&
Paul Stewart75e89d22011-08-01 10:00:02 -0700363 nent->gateway.Equals(entry.gateway) &&
364 nent->scope == entry.scope) {
Paul Stewartf748a362012-03-07 12:01:20 -0800365 if (message.mode() == RTNLMessage::kModeDelete &&
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800366 nent->metric == entry.metric) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700367 table.erase(nent);
Paul Stewartf748a362012-03-07 12:01:20 -0800368 } else if (message.mode() == RTNLMessage::kModeAdd) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700369 nent->from_rtnl = true;
370 nent->metric = entry.metric;
371 }
372 return;
373 }
374 }
375
Paul Stewartf748a362012-03-07 12:01:20 -0800376 if (message.mode() == RTNLMessage::kModeAdd) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700377 SLOG(Route, 2) << __func__ << " adding"
378 << " destination " << entry.dst.ToString()
379 << " index " << interface_index
380 << " gateway " << entry.gateway.ToString()
381 << " metric " << entry.metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700382 table.push_back(entry);
383 }
384}
385
386bool RoutingTable::ApplyRoute(uint32 interface_index,
387 const RoutingTableEntry &entry,
Paul Stewart9a908082011-08-31 12:18:48 -0700388 RTNLMessage::Mode mode,
Paul Stewart75e89d22011-08-01 10:00:02 -0700389 unsigned int flags) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700390 SLOG(Route, 2) << base::StringPrintf(
391 "%s: dst %s/%d src %s/%d index %d mode %d flags 0x%x",
392 __func__, entry.dst.ToString().c_str(), entry.dst.prefix(),
393 entry.src.ToString().c_str(), entry.src.prefix(),
394 interface_index, mode, flags);
Paul Stewart75e89d22011-08-01 10:00:02 -0700395
Paul Stewartf748a362012-03-07 12:01:20 -0800396 RTNLMessage message(
Paul Stewart9a908082011-08-31 12:18:48 -0700397 RTNLMessage::kTypeRoute,
Paul Stewart75e89d22011-08-01 10:00:02 -0700398 mode,
Paul Stewarte6132022011-08-16 09:11:02 -0700399 NLM_F_REQUEST | flags,
Paul Stewart75e89d22011-08-01 10:00:02 -0700400 0,
401 0,
402 0,
403 entry.dst.family());
404
Paul Stewartf748a362012-03-07 12:01:20 -0800405 message.set_route_status(RTNLMessage::RouteStatus(
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700406 entry.dst.prefix(),
407 entry.src.prefix(),
Paul Stewart75e89d22011-08-01 10:00:02 -0700408 RT_TABLE_MAIN,
409 RTPROT_BOOT,
410 entry.scope,
411 RTN_UNICAST,
412 0));
413
Paul Stewartf748a362012-03-07 12:01:20 -0800414 message.SetAttribute(RTA_DST, entry.dst.address());
Paul Stewart75e89d22011-08-01 10:00:02 -0700415 if (!entry.src.IsDefault()) {
Paul Stewartf748a362012-03-07 12:01:20 -0800416 message.SetAttribute(RTA_SRC, entry.src.address());
Paul Stewart75e89d22011-08-01 10:00:02 -0700417 }
418 if (!entry.gateway.IsDefault()) {
Paul Stewartf748a362012-03-07 12:01:20 -0800419 message.SetAttribute(RTA_GATEWAY, entry.gateway.address());
Paul Stewart75e89d22011-08-01 10:00:02 -0700420 }
Paul Stewartf748a362012-03-07 12:01:20 -0800421 message.SetAttribute(RTA_PRIORITY,
422 ByteString::CreateFromCPUUInt32(entry.metric));
423 message.SetAttribute(RTA_OIF,
424 ByteString::CreateFromCPUUInt32(interface_index));
Paul Stewart75e89d22011-08-01 10:00:02 -0700425
Paul Stewartf748a362012-03-07 12:01:20 -0800426 return rtnl_handler_->SendMessage(&message);
Paul Stewart75e89d22011-08-01 10:00:02 -0700427}
428
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800429// Somewhat surprisingly, the kernel allows you to create multiple routes
430// to the same destination through the same interface with different metrics.
431// Therefore, to change the metric on a route, we can't just use the
432// NLM_F_REPLACE flag by itself. We have to explicitly remove the old route.
433// We do so after creating the route at a new metric so there is no traffic
434// disruption to existing network streams.
435void RoutingTable::ReplaceMetric(uint32 interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -0800436 RoutingTableEntry *entry,
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800437 uint32 metric) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700438 SLOG(Route, 2) << __func__ << " index " << interface_index
439 << " metric " << metric;
mukesh agrawald4ef6772012-02-21 16:28:04 -0800440 RoutingTableEntry new_entry = *entry;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800441 new_entry.metric = metric;
442 // First create the route at the new metric.
443 ApplyRoute(interface_index, new_entry, RTNLMessage::kModeAdd,
444 NLM_F_CREATE | NLM_F_REPLACE);
445 // Then delete the route at the old metric.
mukesh agrawald4ef6772012-02-21 16:28:04 -0800446 ApplyRoute(interface_index, *entry, RTNLMessage::kModeDelete, 0);
447 // Now, update our routing table (via |*entry|) from |new_entry|.
448 *entry = new_entry;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800449}
450
Paul Stewart75e89d22011-08-01 10:00:02 -0700451bool RoutingTable::FlushCache() {
452 static const char *kPaths[2] = { kRouteFlushPath4, kRouteFlushPath6 };
453 bool ret = true;
454
Ben Chanfad4a0b2012-04-18 15:49:59 -0700455 SLOG(Route, 2) << __func__;
Paul Stewart75e89d22011-08-01 10:00:02 -0700456
457 for (size_t i = 0; i < arraysize(kPaths); ++i) {
458 if (file_util::WriteFile(FilePath(kPaths[i]), "-1", 2) != 2) {
459 LOG(ERROR) << base::StringPrintf("Cannot write to route flush file %s",
460 kPaths[i]);
461 ret = false;
462 }
463 }
464
465 return ret;
466}
467
Paul Stewartf748a362012-03-07 12:01:20 -0800468bool RoutingTable::RequestRouteToHost(const IPAddress &address,
469 int interface_index) {
470 RTNLMessage message(
471 RTNLMessage::kTypeRoute,
472 RTNLMessage::kModeQuery,
473 NLM_F_REQUEST,
474 0,
475 0,
476 interface_index,
477 address.family());
478
479 RTNLMessage::RouteStatus status;
480 status.dst_prefix = address.prefix();
481 message.set_route_status(status);
482 message.SetAttribute(RTA_DST, address.address());
Paul Stewart536820d2012-03-19 16:05:59 -0700483
484 if (interface_index != -1) {
485 message.SetAttribute(RTA_OIF,
486 ByteString::CreateFromCPUUInt32(interface_index));
487 }
Paul Stewartf748a362012-03-07 12:01:20 -0800488
489 if (!rtnl_handler_->SendMessage(&message)) {
490 return false;
491 }
492
493 // Save the sequence number of the request so we can create a route for
494 // this host when we get a reply.
495 route_query_sequences_.push(message.seq());
496
497 return true;
498}
499
Paul Stewart75e89d22011-08-01 10:00:02 -0700500} // namespace shill