blob: f5d84af49a402a65f9837b89d778ae4806d5f08d [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"
35
Eric Shienbrood3e20a232012-02-16 11:35:56 -050036using base::Bind;
37using base::Unretained;
Paul Stewart75e89d22011-08-01 10:00:02 -070038using std::string;
39using std::vector;
40
41namespace shill {
42
Eric Shienbrood3e20a232012-02-16 11:35:56 -050043// TODO(ers): not using LAZY_INSTANCE_INITIALIZER
44// because of http://crbug.com/114828
45static base::LazyInstance<RoutingTable> g_routing_table = {0, {{0}}};
Paul Stewart0d2ada32011-08-09 17:01:57 -070046
Paul Stewart75e89d22011-08-01 10:00:02 -070047// static
48const char RoutingTable::kRouteFlushPath4[] = "/proc/sys/net/ipv4/route/flush";
Paul Stewart0d2ada32011-08-09 17:01:57 -070049// static
Paul Stewart75e89d22011-08-01 10:00:02 -070050const char RoutingTable::kRouteFlushPath6[] = "/proc/sys/net/ipv6/route/flush";
51
52RoutingTable::RoutingTable()
Eric Shienbrood3e20a232012-02-16 11:35:56 -050053 : route_callback_(Bind(&RoutingTable::RouteMsgHandler, Unretained(this))),
Paul Stewartf748a362012-03-07 12:01:20 -080054 route_listener_(NULL),
55 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart75e89d22011-08-01 10:00:02 -070056 VLOG(2) << __func__;
57}
58
59RoutingTable::~RoutingTable() {}
60
61RoutingTable* RoutingTable::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070062 return g_routing_table.Pointer();
Paul Stewart75e89d22011-08-01 10:00:02 -070063}
64
65void RoutingTable::Start() {
66 VLOG(2) << __func__;
67
68 route_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -050069 new RTNLListener(RTNLHandler::kRequestRoute, route_callback_));
Paul Stewartf748a362012-03-07 12:01:20 -080070 rtnl_handler_->RequestDump(RTNLHandler::kRequestRoute);
Paul Stewart75e89d22011-08-01 10:00:02 -070071}
72
73void RoutingTable::Stop() {
74 VLOG(2) << __func__;
75
76 route_listener_.reset();
77}
78
79bool RoutingTable::AddRoute(int interface_index,
80 const RoutingTableEntry &entry) {
Paul Stewartf748a362012-03-07 12:01:20 -080081 VLOG(2) << __func__ << ": "
82 << "destination " << entry.dst.ToString()
83 << " index " << interface_index
84 << " gateway " << entry.gateway.ToString()
85 << " metric " << entry.metric;
Paul Stewart75e89d22011-08-01 10:00:02 -070086
87 CHECK(!entry.from_rtnl);
88 if (!ApplyRoute(interface_index,
89 entry,
Paul Stewart9a908082011-08-31 12:18:48 -070090 RTNLMessage::kModeAdd,
Paul Stewart75e89d22011-08-01 10:00:02 -070091 NLM_F_CREATE | NLM_F_EXCL)) {
92 return false;
93 }
94 tables_[interface_index].push_back(entry);
95 return true;
96}
97
98bool RoutingTable::GetDefaultRoute(int interface_index,
99 IPAddress::Family family,
100 RoutingTableEntry *entry) {
mukesh agrawald4ef6772012-02-21 16:28:04 -0800101 RoutingTableEntry *found_entry;
102 bool ret = GetDefaultRouteInternal(interface_index, family, &found_entry);
103 if (ret) {
104 *entry = *found_entry;
105 }
106 return ret;
107}
108
109bool RoutingTable::GetDefaultRouteInternal(int interface_index,
110 IPAddress::Family family,
111 RoutingTableEntry **entry) {
Paul Stewartf748a362012-03-07 12:01:20 -0800112 VLOG(2) << __func__ << " index " << interface_index
113 << " family " << IPAddress::GetAddressFamilyName(family);
Paul Stewart75e89d22011-08-01 10:00:02 -0700114
115 base::hash_map<int, vector<RoutingTableEntry> >::iterator table =
116 tables_.find(interface_index);
117
118 if (table == tables_.end()) {
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800119 VLOG(2) << __func__ << " no table";
Paul Stewart75e89d22011-08-01 10:00:02 -0700120 return false;
121 }
122
123 vector<RoutingTableEntry>::iterator nent;
124
125 for (nent = table->second.begin(); nent != table->second.end(); ++nent) {
126 if (nent->dst.IsDefault() && nent->dst.family() == family) {
mukesh agrawald4ef6772012-02-21 16:28:04 -0800127 *entry = &(*nent);
Paul Stewartf748a362012-03-07 12:01:20 -0800128 VLOG(2) << __func__ << ": found"
129 << " gateway " << nent->gateway.ToString()
130 << " metric " << nent->metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700131 return true;
132 }
133 }
134
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800135 VLOG(2) << __func__ << " no route";
Paul Stewart75e89d22011-08-01 10:00:02 -0700136 return false;
137}
138
139bool RoutingTable::SetDefaultRoute(int interface_index,
140 const IPConfigRefPtr &ipconfig,
141 uint32 metric) {
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800142 VLOG(2) << __func__ << " index " << interface_index << " metric " << metric;
143
Paul Stewart75e89d22011-08-01 10:00:02 -0700144 const IPConfig::Properties &ipconfig_props = ipconfig->properties();
mukesh agrawald4ef6772012-02-21 16:28:04 -0800145 RoutingTableEntry *old_entry;
Paul Stewart75e89d22011-08-01 10:00:02 -0700146 IPAddress gateway_address(ipconfig_props.address_family);
147 if (!gateway_address.SetAddressFromString(ipconfig_props.gateway)) {
148 return false;
149 }
150
mukesh agrawald4ef6772012-02-21 16:28:04 -0800151 if (GetDefaultRouteInternal(interface_index,
152 ipconfig_props.address_family,
153 &old_entry)) {
154 if (old_entry->gateway.Equals(gateway_address)) {
155 if (old_entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800156 ReplaceMetric(interface_index, old_entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700157 }
158 return true;
159 } else {
mukesh agrawald4ef6772012-02-21 16:28:04 -0800160 // TODO(quiche): Update internal state as well?
Paul Stewart75e89d22011-08-01 10:00:02 -0700161 ApplyRoute(interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -0800162 *old_entry,
Paul Stewart9a908082011-08-31 12:18:48 -0700163 RTNLMessage::kModeDelete,
Paul Stewart75e89d22011-08-01 10:00:02 -0700164 0);
165 }
166 }
167
168 IPAddress default_address(ipconfig_props.address_family);
169 default_address.SetAddressToDefault();
170
171 return AddRoute(interface_index,
172 RoutingTableEntry(default_address,
Paul Stewart75e89d22011-08-01 10:00:02 -0700173 default_address,
Paul Stewart75e89d22011-08-01 10:00:02 -0700174 gateway_address,
175 metric,
176 RT_SCOPE_UNIVERSE,
177 false));
178}
179
Paul Stewart3f68bb12012-03-15 13:33:10 -0700180bool RoutingTable::ConfigureRoutes(int interface_index,
181 const IPConfigRefPtr &ipconfig,
182 uint32 metric) {
183 bool ret = true;
184
185 IPAddress::Family address_family = ipconfig->properties().address_family;
186 const vector<IPConfig::Route> &routes = ipconfig->properties().routes;
187
188 for (vector<IPConfig::Route>::const_iterator it = routes.begin();
189 it != routes.end();
190 ++it) {
191 VLOG(3) << "Installing route:"
192 << " Destination: " << it->host
193 << " Netmask: " << it->netmask
194 << " Gateway: " << it->gateway;
195 IPAddress destination_address(address_family);
196 IPAddress source_address(address_family); // Left as default.
197 IPAddress gateway_address(address_family);
198 if (!destination_address.SetAddressFromString(it->host)) {
199 LOG(ERROR) << "Failed to parse host "
200 << it->host;
201 ret = false;
202 continue;
203 }
204 if (!gateway_address.SetAddressFromString(it->gateway)) {
205 LOG(ERROR) << "Failed to parse gateway "
206 << it->gateway;
207 ret = false;
208 continue;
209 }
210 destination_address.set_prefix(
211 IPAddress::GetPrefixLengthFromMask(address_family, it->netmask));
212 if (!AddRoute(interface_index,
213 RoutingTableEntry(destination_address,
214 source_address,
215 gateway_address,
216 metric,
217 RT_SCOPE_UNIVERSE,
218 false))) {
219 ret = false;
220 }
221 }
222 return ret;
223}
224
Thieu Lefb46caf2012-03-08 11:57:15 -0800225void RoutingTable::FlushRoutes(int interface_index) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700226 VLOG(2) << __func__;
227
228 base::hash_map<int, vector<RoutingTableEntry> >::iterator table =
229 tables_.find(interface_index);
230
231 if (table == tables_.end()) {
232 return;
233 }
234
235 vector<RoutingTableEntry>::iterator nent;
236
237 for (nent = table->second.begin(); nent != table->second.end(); ++nent) {
Thieu Lecaef8932012-02-28 16:06:59 -0800238 ApplyRoute(interface_index, *nent, RTNLMessage::kModeDelete, 0);
Paul Stewart75e89d22011-08-01 10:00:02 -0700239 }
Thieu Lefb46caf2012-03-08 11:57:15 -0800240 table->second.clear();
Paul Stewart75e89d22011-08-01 10:00:02 -0700241}
242
243void RoutingTable::ResetTable(int interface_index) {
244 tables_.erase(interface_index);
245}
246
247void RoutingTable::SetDefaultMetric(int interface_index, uint32 metric) {
Paul Stewartf748a362012-03-07 12:01:20 -0800248 VLOG(2) << __func__ << " index " << interface_index << " metric " << metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700249
mukesh agrawald4ef6772012-02-21 16:28:04 -0800250 RoutingTableEntry *entry;
251 if (GetDefaultRouteInternal(
252 interface_index, IPAddress::kFamilyIPv4, &entry) &&
253 entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800254 ReplaceMetric(interface_index, entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700255 }
256
mukesh agrawald4ef6772012-02-21 16:28:04 -0800257 if (GetDefaultRouteInternal(
258 interface_index, IPAddress::kFamilyIPv6, &entry) &&
259 entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800260 ReplaceMetric(interface_index, entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700261 }
262}
263
Paul Stewartf748a362012-03-07 12:01:20 -0800264// static
265bool RoutingTable::ParseRoutingTableMessage(const RTNLMessage &message,
266 int *interface_index,
267 RoutingTableEntry *entry) {
268 if (message.type() != RTNLMessage::kTypeRoute ||
269 message.family() == IPAddress::kFamilyUnknown ||
270 !message.HasAttribute(RTA_OIF)) {
271 return false;
Paul Stewart75e89d22011-08-01 10:00:02 -0700272 }
273
Paul Stewartf748a362012-03-07 12:01:20 -0800274 const RTNLMessage::RouteStatus &route_status = message.route_status();
Paul Stewart75e89d22011-08-01 10:00:02 -0700275
276 if (route_status.type != RTN_UNICAST ||
Paul Stewart75e89d22011-08-01 10:00:02 -0700277 route_status.table != RT_TABLE_MAIN) {
Paul Stewartf748a362012-03-07 12:01:20 -0800278 return false;
Paul Stewart75e89d22011-08-01 10:00:02 -0700279 }
280
Paul Stewartf748a362012-03-07 12:01:20 -0800281 uint32 interface_index_u32 = 0;
282 if (!message.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&interface_index_u32)) {
283 return false;
Paul Stewart75e89d22011-08-01 10:00:02 -0700284 }
Paul Stewartf748a362012-03-07 12:01:20 -0800285 *interface_index = interface_index_u32;
Paul Stewart75e89d22011-08-01 10:00:02 -0700286
287 uint32 metric = 0;
Paul Stewartf748a362012-03-07 12:01:20 -0800288 if (message.HasAttribute(RTA_PRIORITY)) {
289 message.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700290 }
291
Paul Stewartf748a362012-03-07 12:01:20 -0800292 IPAddress default_addr(message.family());
Paul Stewart75e89d22011-08-01 10:00:02 -0700293 default_addr.SetAddressToDefault();
294
295 ByteString dst_bytes(default_addr.address());
Paul Stewartf748a362012-03-07 12:01:20 -0800296 if (message.HasAttribute(RTA_DST)) {
297 dst_bytes = message.GetAttribute(RTA_DST);
Paul Stewart75e89d22011-08-01 10:00:02 -0700298 }
299 ByteString src_bytes(default_addr.address());
Paul Stewartf748a362012-03-07 12:01:20 -0800300 if (message.HasAttribute(RTA_SRC)) {
301 src_bytes = message.GetAttribute(RTA_SRC);
Paul Stewart75e89d22011-08-01 10:00:02 -0700302 }
303 ByteString gateway_bytes(default_addr.address());
Paul Stewartf748a362012-03-07 12:01:20 -0800304 if (message.HasAttribute(RTA_GATEWAY)) {
305 gateway_bytes = message.GetAttribute(RTA_GATEWAY);
Paul Stewart75e89d22011-08-01 10:00:02 -0700306 }
307
Paul Stewartf748a362012-03-07 12:01:20 -0800308 entry->dst = IPAddress(message.family(), dst_bytes, route_status.dst_prefix);
309 entry->src = IPAddress(message.family(), src_bytes, route_status.src_prefix);
310 entry->gateway = IPAddress(message.family(), gateway_bytes);
311 entry->metric = metric;
312 entry->scope = route_status.scope;
313 entry->from_rtnl = true;
314
315 return true;
316}
317
318void RoutingTable::RouteMsgHandler(const RTNLMessage &message) {
319 int interface_index;
320 RoutingTableEntry entry;
321
322 if (!ParseRoutingTableMessage(message, &interface_index, &entry)) {
323 return;
324 }
325
326 if (!route_query_sequences_.empty() &&
327 message.route_status().protocol == RTPROT_UNSPEC) {
328 VLOG(3) << __func__ << ": Message seq: " << message.seq()
329 << " mode " << message.mode()
330 << ", next query seq: " << route_query_sequences_.front();
331
332 // Purge queries that have expired (sequence number of this message is
333 // greater than that of the head of the route query sequence). Do the
334 // math in a way that's roll-over independent.
335 while (route_query_sequences_.front() - message.seq() > kuint32max / 2) {
336 LOG(ERROR) << __func__ << ": Purging un-replied route request sequence "
337 << route_query_sequences_.front()
338 << " (< " << message.seq() << ")";
339 route_query_sequences_.pop();
340 if (route_query_sequences_.empty())
341 return;
342 }
343
344 if (route_query_sequences_.front() == message.seq()) {
345 VLOG(2) << __func__ << ": Adding host route to " << entry.dst.ToString();
346 route_query_sequences_.pop();
347 RoutingTableEntry add_entry(entry);
348 add_entry.from_rtnl = false;
349 AddRoute(interface_index, add_entry);
350 }
351 return;
352 } else if (message.route_status().protocol != RTPROT_BOOT) {
353 // Responses to route queries come back with a protocol of
354 // RTPROT_UNSPEC. Otherwise, normal route updates that we are
355 // interested in come with a protocol of RTPROT_BOOT.
356 return;
357 }
Paul Stewart75e89d22011-08-01 10:00:02 -0700358
359 vector<RoutingTableEntry> &table = tables_[interface_index];
360 vector<RoutingTableEntry>::iterator nent;
361 for (nent = table.begin(); nent != table.end(); ++nent) {
362 if (nent->dst.Equals(entry.dst) &&
Paul Stewart75e89d22011-08-01 10:00:02 -0700363 nent->src.Equals(entry.src) &&
Paul Stewart75e89d22011-08-01 10:00:02 -0700364 nent->gateway.Equals(entry.gateway) &&
365 nent->scope == entry.scope) {
Paul Stewartf748a362012-03-07 12:01:20 -0800366 if (message.mode() == RTNLMessage::kModeDelete &&
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800367 nent->metric == entry.metric) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700368 table.erase(nent);
Paul Stewartf748a362012-03-07 12:01:20 -0800369 } else if (message.mode() == RTNLMessage::kModeAdd) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700370 nent->from_rtnl = true;
371 nent->metric = entry.metric;
372 }
373 return;
374 }
375 }
376
Paul Stewartf748a362012-03-07 12:01:20 -0800377 if (message.mode() == RTNLMessage::kModeAdd) {
378 VLOG(2) << __func__ << " adding"
379 << " destination " << entry.dst.ToString()
380 << " index " << interface_index
381 << " gateway " << entry.gateway.ToString()
382 << " metric " << entry.metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700383 table.push_back(entry);
384 }
385}
386
387bool RoutingTable::ApplyRoute(uint32 interface_index,
388 const RoutingTableEntry &entry,
Paul Stewart9a908082011-08-31 12:18:48 -0700389 RTNLMessage::Mode mode,
Paul Stewart75e89d22011-08-01 10:00:02 -0700390 unsigned int flags) {
Paul Stewart3f68bb12012-03-15 13:33:10 -0700391 VLOG(2) << base::StringPrintf("%s: dst %s/%d src %s/%d index %d mode %d "
392 "flags 0x%x",
mukesh agrawalbf14e942012-03-02 14:36:34 -0800393 __func__, entry.dst.ToString().c_str(),
Paul Stewart3f68bb12012-03-15 13:33:10 -0700394 entry.dst.prefix(),
395 entry.src.ToString().c_str(),
396 entry.src.prefix(), interface_index, mode,
397 flags);
Paul Stewart75e89d22011-08-01 10:00:02 -0700398
Paul Stewartf748a362012-03-07 12:01:20 -0800399 RTNLMessage message(
Paul Stewart9a908082011-08-31 12:18:48 -0700400 RTNLMessage::kTypeRoute,
Paul Stewart75e89d22011-08-01 10:00:02 -0700401 mode,
Paul Stewarte6132022011-08-16 09:11:02 -0700402 NLM_F_REQUEST | flags,
Paul Stewart75e89d22011-08-01 10:00:02 -0700403 0,
404 0,
405 0,
406 entry.dst.family());
407
Paul Stewartf748a362012-03-07 12:01:20 -0800408 message.set_route_status(RTNLMessage::RouteStatus(
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700409 entry.dst.prefix(),
410 entry.src.prefix(),
Paul Stewart75e89d22011-08-01 10:00:02 -0700411 RT_TABLE_MAIN,
412 RTPROT_BOOT,
413 entry.scope,
414 RTN_UNICAST,
415 0));
416
Paul Stewartf748a362012-03-07 12:01:20 -0800417 message.SetAttribute(RTA_DST, entry.dst.address());
Paul Stewart75e89d22011-08-01 10:00:02 -0700418 if (!entry.src.IsDefault()) {
Paul Stewartf748a362012-03-07 12:01:20 -0800419 message.SetAttribute(RTA_SRC, entry.src.address());
Paul Stewart75e89d22011-08-01 10:00:02 -0700420 }
421 if (!entry.gateway.IsDefault()) {
Paul Stewartf748a362012-03-07 12:01:20 -0800422 message.SetAttribute(RTA_GATEWAY, entry.gateway.address());
Paul Stewart75e89d22011-08-01 10:00:02 -0700423 }
Paul Stewartf748a362012-03-07 12:01:20 -0800424 message.SetAttribute(RTA_PRIORITY,
425 ByteString::CreateFromCPUUInt32(entry.metric));
426 message.SetAttribute(RTA_OIF,
427 ByteString::CreateFromCPUUInt32(interface_index));
Paul Stewart75e89d22011-08-01 10:00:02 -0700428
Paul Stewartf748a362012-03-07 12:01:20 -0800429 return rtnl_handler_->SendMessage(&message);
Paul Stewart75e89d22011-08-01 10:00:02 -0700430}
431
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800432// Somewhat surprisingly, the kernel allows you to create multiple routes
433// to the same destination through the same interface with different metrics.
434// Therefore, to change the metric on a route, we can't just use the
435// NLM_F_REPLACE flag by itself. We have to explicitly remove the old route.
436// We do so after creating the route at a new metric so there is no traffic
437// disruption to existing network streams.
438void RoutingTable::ReplaceMetric(uint32 interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -0800439 RoutingTableEntry *entry,
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800440 uint32 metric) {
Paul Stewartf748a362012-03-07 12:01:20 -0800441 VLOG(2) << __func__ << " index " << interface_index << " metric " << metric;
mukesh agrawald4ef6772012-02-21 16:28:04 -0800442 RoutingTableEntry new_entry = *entry;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800443 new_entry.metric = metric;
444 // First create the route at the new metric.
445 ApplyRoute(interface_index, new_entry, RTNLMessage::kModeAdd,
446 NLM_F_CREATE | NLM_F_REPLACE);
447 // Then delete the route at the old metric.
mukesh agrawald4ef6772012-02-21 16:28:04 -0800448 ApplyRoute(interface_index, *entry, RTNLMessage::kModeDelete, 0);
449 // Now, update our routing table (via |*entry|) from |new_entry|.
450 *entry = new_entry;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800451}
452
Paul Stewart75e89d22011-08-01 10:00:02 -0700453bool RoutingTable::FlushCache() {
454 static const char *kPaths[2] = { kRouteFlushPath4, kRouteFlushPath6 };
455 bool ret = true;
456
457 VLOG(2) << __func__;
458
459 for (size_t i = 0; i < arraysize(kPaths); ++i) {
460 if (file_util::WriteFile(FilePath(kPaths[i]), "-1", 2) != 2) {
461 LOG(ERROR) << base::StringPrintf("Cannot write to route flush file %s",
462 kPaths[i]);
463 ret = false;
464 }
465 }
466
467 return ret;
468}
469
Paul Stewartf748a362012-03-07 12:01:20 -0800470bool RoutingTable::RequestRouteToHost(const IPAddress &address,
471 int interface_index) {
472 RTNLMessage message(
473 RTNLMessage::kTypeRoute,
474 RTNLMessage::kModeQuery,
475 NLM_F_REQUEST,
476 0,
477 0,
478 interface_index,
479 address.family());
480
481 RTNLMessage::RouteStatus status;
482 status.dst_prefix = address.prefix();
483 message.set_route_status(status);
484 message.SetAttribute(RTA_DST, address.address());
Paul Stewart536820d2012-03-19 16:05:59 -0700485
486 if (interface_index != -1) {
487 message.SetAttribute(RTA_OIF,
488 ByteString::CreateFromCPUUInt32(interface_index));
489 }
Paul Stewartf748a362012-03-07 12:01:20 -0800490
491 if (!rtnl_handler_->SendMessage(&message)) {
492 return false;
493 }
494
495 // Save the sequence number of the request so we can create a route for
496 // this host when we get a reply.
497 route_query_sequences_.push(message.seq());
498
499 return true;
500}
501
Paul Stewart75e89d22011-08-01 10:00:02 -0700502} // namespace shill