blob: bfc761f6eb9522341b6e78b35b4fa30b5a5c95b7 [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
21#include <base/callback_old.h>
22#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>
27#include <base/stl_util-inl.h>
28#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
36using std::string;
37using std::vector;
38
39namespace shill {
40
Paul Stewart0d2ada32011-08-09 17:01:57 -070041static base::LazyInstance<RoutingTable> g_routing_table(
42 base::LINKER_INITIALIZED);
43
Paul Stewart75e89d22011-08-01 10:00:02 -070044// static
45const char RoutingTable::kRouteFlushPath4[] = "/proc/sys/net/ipv4/route/flush";
Paul Stewart0d2ada32011-08-09 17:01:57 -070046// static
Paul Stewart75e89d22011-08-01 10:00:02 -070047const char RoutingTable::kRouteFlushPath6[] = "/proc/sys/net/ipv6/route/flush";
48
49RoutingTable::RoutingTable()
50 : route_callback_(NewCallback(this, &RoutingTable::RouteMsgHandler)),
51 route_listener_(NULL) {
52 VLOG(2) << __func__;
53}
54
55RoutingTable::~RoutingTable() {}
56
57RoutingTable* RoutingTable::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070058 return g_routing_table.Pointer();
Paul Stewart75e89d22011-08-01 10:00:02 -070059}
60
61void RoutingTable::Start() {
62 VLOG(2) << __func__;
63
64 route_listener_.reset(
65 new RTNLListener(RTNLHandler::kRequestRoute, route_callback_.get()));
66 RTNLHandler::GetInstance()->RequestDump(
Paul Stewart9a908082011-08-31 12:18:48 -070067 RTNLHandler::kRequestRoute);
Paul Stewart75e89d22011-08-01 10:00:02 -070068}
69
70void RoutingTable::Stop() {
71 VLOG(2) << __func__;
72
73 route_listener_.reset();
74}
75
76bool RoutingTable::AddRoute(int interface_index,
77 const RoutingTableEntry &entry) {
mukesh agrawal2c15d2c2012-02-21 16:09:21 -080078 VLOG(2) << __func__ << " "
79 << "index " << interface_index
80 << "gateway " << entry.gateway.ToString() << " "
81 << "metric " << entry.metric;
Paul Stewart75e89d22011-08-01 10:00:02 -070082
83 CHECK(!entry.from_rtnl);
84 if (!ApplyRoute(interface_index,
85 entry,
Paul Stewart9a908082011-08-31 12:18:48 -070086 RTNLMessage::kModeAdd,
Paul Stewart75e89d22011-08-01 10:00:02 -070087 NLM_F_CREATE | NLM_F_EXCL)) {
88 return false;
89 }
90 tables_[interface_index].push_back(entry);
91 return true;
92}
93
94bool RoutingTable::GetDefaultRoute(int interface_index,
95 IPAddress::Family family,
96 RoutingTableEntry *entry) {
mukesh agrawald4ef6772012-02-21 16:28:04 -080097 RoutingTableEntry *found_entry;
98 bool ret = GetDefaultRouteInternal(interface_index, family, &found_entry);
99 if (ret) {
100 *entry = *found_entry;
101 }
102 return ret;
103}
104
105bool RoutingTable::GetDefaultRouteInternal(int interface_index,
106 IPAddress::Family family,
107 RoutingTableEntry **entry) {
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800108 VLOG(2) << __func__ << " index " << interface_index << " family " << family;
Paul Stewart75e89d22011-08-01 10:00:02 -0700109
110 base::hash_map<int, vector<RoutingTableEntry> >::iterator table =
111 tables_.find(interface_index);
112
113 if (table == tables_.end()) {
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800114 VLOG(2) << __func__ << " no table";
Paul Stewart75e89d22011-08-01 10:00:02 -0700115 return false;
116 }
117
118 vector<RoutingTableEntry>::iterator nent;
119
120 for (nent = table->second.begin(); nent != table->second.end(); ++nent) {
121 if (nent->dst.IsDefault() && nent->dst.family() == family) {
mukesh agrawald4ef6772012-02-21 16:28:04 -0800122 *entry = &(*nent);
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800123 VLOG(2) << __func__ << " found "
124 << "gateway " << nent->gateway.ToString() << " "
125 << "metric " << nent->metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700126 return true;
127 }
128 }
129
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800130 VLOG(2) << __func__ << " no route";
Paul Stewart75e89d22011-08-01 10:00:02 -0700131 return false;
132}
133
134bool RoutingTable::SetDefaultRoute(int interface_index,
135 const IPConfigRefPtr &ipconfig,
136 uint32 metric) {
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800137 VLOG(2) << __func__ << " index " << interface_index << " metric " << metric;
138
Paul Stewart75e89d22011-08-01 10:00:02 -0700139 const IPConfig::Properties &ipconfig_props = ipconfig->properties();
mukesh agrawald4ef6772012-02-21 16:28:04 -0800140 RoutingTableEntry *old_entry;
Paul Stewart75e89d22011-08-01 10:00:02 -0700141 IPAddress gateway_address(ipconfig_props.address_family);
142 if (!gateway_address.SetAddressFromString(ipconfig_props.gateway)) {
143 return false;
144 }
145
mukesh agrawald4ef6772012-02-21 16:28:04 -0800146 if (GetDefaultRouteInternal(interface_index,
147 ipconfig_props.address_family,
148 &old_entry)) {
149 if (old_entry->gateway.Equals(gateway_address)) {
150 if (old_entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800151 ReplaceMetric(interface_index, old_entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700152 }
153 return true;
154 } else {
mukesh agrawald4ef6772012-02-21 16:28:04 -0800155 // TODO(quiche): Update internal state as well?
Paul Stewart75e89d22011-08-01 10:00:02 -0700156 ApplyRoute(interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -0800157 *old_entry,
Paul Stewart9a908082011-08-31 12:18:48 -0700158 RTNLMessage::kModeDelete,
Paul Stewart75e89d22011-08-01 10:00:02 -0700159 0);
160 }
161 }
162
163 IPAddress default_address(ipconfig_props.address_family);
164 default_address.SetAddressToDefault();
165
166 return AddRoute(interface_index,
167 RoutingTableEntry(default_address,
Paul Stewart75e89d22011-08-01 10:00:02 -0700168 default_address,
Paul Stewart75e89d22011-08-01 10:00:02 -0700169 gateway_address,
170 metric,
171 RT_SCOPE_UNIVERSE,
172 false));
173}
174
Thieu Lefb46caf2012-03-08 11:57:15 -0800175void RoutingTable::FlushRoutes(int interface_index) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700176 VLOG(2) << __func__;
177
178 base::hash_map<int, vector<RoutingTableEntry> >::iterator table =
179 tables_.find(interface_index);
180
181 if (table == tables_.end()) {
182 return;
183 }
184
185 vector<RoutingTableEntry>::iterator nent;
186
187 for (nent = table->second.begin(); nent != table->second.end(); ++nent) {
Thieu Lecaef8932012-02-28 16:06:59 -0800188 ApplyRoute(interface_index, *nent, RTNLMessage::kModeDelete, 0);
Paul Stewart75e89d22011-08-01 10:00:02 -0700189 }
Thieu Lefb46caf2012-03-08 11:57:15 -0800190 table->second.clear();
Paul Stewart75e89d22011-08-01 10:00:02 -0700191}
192
193void RoutingTable::ResetTable(int interface_index) {
194 tables_.erase(interface_index);
195}
196
197void RoutingTable::SetDefaultMetric(int interface_index, uint32 metric) {
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800198 VLOG(2) << __func__ << " "
199 << "index " << interface_index << " metric " << metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700200
mukesh agrawald4ef6772012-02-21 16:28:04 -0800201 RoutingTableEntry *entry;
202 if (GetDefaultRouteInternal(
203 interface_index, IPAddress::kFamilyIPv4, &entry) &&
204 entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800205 ReplaceMetric(interface_index, entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700206 }
207
mukesh agrawald4ef6772012-02-21 16:28:04 -0800208 if (GetDefaultRouteInternal(
209 interface_index, IPAddress::kFamilyIPv6, &entry) &&
210 entry->metric != metric) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800211 ReplaceMetric(interface_index, entry, metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700212 }
213}
214
Chris Masone2aa97072011-08-09 17:35:08 -0700215void RoutingTable::RouteMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700216 if (msg.type() != RTNLMessage::kTypeRoute ||
Paul Stewart7355ce12011-09-02 10:47:01 -0700217 msg.family() == IPAddress::kFamilyUnknown ||
Paul Stewart75e89d22011-08-01 10:00:02 -0700218 !msg.HasAttribute(RTA_OIF)) {
219 return;
220 }
221
222 const RTNLMessage::RouteStatus &route_status = msg.route_status();
223
224 if (route_status.type != RTN_UNICAST ||
225 route_status.protocol != RTPROT_BOOT ||
226 route_status.table != RT_TABLE_MAIN) {
227 return;
228 }
229
230 uint32 interface_index = 0;
231 if (!msg.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&interface_index)) {
232 return;
233 }
234
235 uint32 metric = 0;
236 if (msg.HasAttribute(RTA_PRIORITY)) {
237 msg.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric);
238 }
239
240 IPAddress default_addr(msg.family());
241 default_addr.SetAddressToDefault();
242
243 ByteString dst_bytes(default_addr.address());
244 if (msg.HasAttribute(RTA_DST)) {
245 dst_bytes = msg.GetAttribute(RTA_DST);
246 }
247 ByteString src_bytes(default_addr.address());
248 if (msg.HasAttribute(RTA_SRC)) {
249 src_bytes = msg.GetAttribute(RTA_SRC);
250 }
251 ByteString gateway_bytes(default_addr.address());
252 if (msg.HasAttribute(RTA_GATEWAY)) {
253 gateway_bytes = msg.GetAttribute(RTA_GATEWAY);
254 }
255
256 RoutingTableEntry entry(
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700257 IPAddress(msg.family(), dst_bytes, route_status.dst_prefix),
258 IPAddress(msg.family(), src_bytes, route_status.src_prefix),
Paul Stewart75e89d22011-08-01 10:00:02 -0700259 IPAddress(msg.family(), gateway_bytes),
260 metric,
261 route_status.scope,
262 true);
263
264 vector<RoutingTableEntry> &table = tables_[interface_index];
265 vector<RoutingTableEntry>::iterator nent;
266 for (nent = table.begin(); nent != table.end(); ++nent) {
267 if (nent->dst.Equals(entry.dst) &&
Paul Stewart75e89d22011-08-01 10:00:02 -0700268 nent->src.Equals(entry.src) &&
Paul Stewart75e89d22011-08-01 10:00:02 -0700269 nent->gateway.Equals(entry.gateway) &&
270 nent->scope == entry.scope) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800271 if (msg.mode() == RTNLMessage::kModeDelete &&
272 nent->metric == entry.metric) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700273 table.erase(nent);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800274 } else if (msg.mode() == RTNLMessage::kModeAdd) {
Paul Stewart75e89d22011-08-01 10:00:02 -0700275 nent->from_rtnl = true;
276 nent->metric = entry.metric;
277 }
278 return;
279 }
280 }
281
Paul Stewart9a908082011-08-31 12:18:48 -0700282 if (msg.mode() == RTNLMessage::kModeAdd) {
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800283 VLOG(2) << __func__ << " adding "
284 << "index " << interface_index
285 << "gateway " << entry.gateway.ToString() << " "
286 << "metric " << entry.metric;
Paul Stewart75e89d22011-08-01 10:00:02 -0700287 table.push_back(entry);
288 }
289}
290
291bool RoutingTable::ApplyRoute(uint32 interface_index,
292 const RoutingTableEntry &entry,
Paul Stewart9a908082011-08-31 12:18:48 -0700293 RTNLMessage::Mode mode,
Paul Stewart75e89d22011-08-01 10:00:02 -0700294 unsigned int flags) {
295 VLOG(2) << base::StringPrintf("%s: index %d mode %d flags 0x%x",
296 __func__, interface_index, mode, flags);
297
298 RTNLMessage msg(
Paul Stewart9a908082011-08-31 12:18:48 -0700299 RTNLMessage::kTypeRoute,
Paul Stewart75e89d22011-08-01 10:00:02 -0700300 mode,
Paul Stewarte6132022011-08-16 09:11:02 -0700301 NLM_F_REQUEST | flags,
Paul Stewart75e89d22011-08-01 10:00:02 -0700302 0,
303 0,
304 0,
305 entry.dst.family());
306
307 msg.set_route_status(RTNLMessage::RouteStatus(
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700308 entry.dst.prefix(),
309 entry.src.prefix(),
Paul Stewart75e89d22011-08-01 10:00:02 -0700310 RT_TABLE_MAIN,
311 RTPROT_BOOT,
312 entry.scope,
313 RTN_UNICAST,
314 0));
315
316 msg.SetAttribute(RTA_DST, entry.dst.address());
317 if (!entry.src.IsDefault()) {
318 msg.SetAttribute(RTA_SRC, entry.src.address());
319 }
320 if (!entry.gateway.IsDefault()) {
321 msg.SetAttribute(RTA_GATEWAY, entry.gateway.address());
322 }
323 msg.SetAttribute(RTA_PRIORITY, ByteString::CreateFromCPUUInt32(entry.metric));
324 msg.SetAttribute(RTA_OIF, ByteString::CreateFromCPUUInt32(interface_index));
325
326 return RTNLHandler::GetInstance()->SendMessage(&msg);
327}
328
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800329// Somewhat surprisingly, the kernel allows you to create multiple routes
330// to the same destination through the same interface with different metrics.
331// Therefore, to change the metric on a route, we can't just use the
332// NLM_F_REPLACE flag by itself. We have to explicitly remove the old route.
333// We do so after creating the route at a new metric so there is no traffic
334// disruption to existing network streams.
335void RoutingTable::ReplaceMetric(uint32 interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -0800336 RoutingTableEntry *entry,
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800337 uint32 metric) {
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800338 VLOG(2) << __func__ << " "
339 << "index " << interface_index << " metric " << metric;
mukesh agrawald4ef6772012-02-21 16:28:04 -0800340 RoutingTableEntry new_entry = *entry;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800341 new_entry.metric = metric;
342 // First create the route at the new metric.
343 ApplyRoute(interface_index, new_entry, RTNLMessage::kModeAdd,
344 NLM_F_CREATE | NLM_F_REPLACE);
345 // Then delete the route at the old metric.
mukesh agrawald4ef6772012-02-21 16:28:04 -0800346 ApplyRoute(interface_index, *entry, RTNLMessage::kModeDelete, 0);
347 // Now, update our routing table (via |*entry|) from |new_entry|.
348 *entry = new_entry;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800349}
350
Paul Stewart75e89d22011-08-01 10:00:02 -0700351bool RoutingTable::FlushCache() {
352 static const char *kPaths[2] = { kRouteFlushPath4, kRouteFlushPath6 };
353 bool ret = true;
354
355 VLOG(2) << __func__;
356
357 for (size_t i = 0; i < arraysize(kPaths); ++i) {
358 if (file_util::WriteFile(FilePath(kPaths[i]), "-1", 2) != 2) {
359 LOG(ERROR) << base::StringPrintf("Cannot write to route flush file %s",
360 kPaths[i]);
361 ret = false;
362 }
363 }
364
365 return ret;
366}
367
368} // namespace shill