mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 2 | // 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 Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 21 | #include <base/bind.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 22 | #include <base/file_path.h> |
| 23 | #include <base/file_util.h> |
| 24 | #include <base/hash_tables.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 25 | #include <base/memory/scoped_ptr.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 26 | #include <base/stl_util.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 27 | #include <base/stringprintf.h> |
| 28 | |
| 29 | #include "shill/byte_string.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 30 | #include "shill/logging.h" |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 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 Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 36 | using base::Bind; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 37 | using base::FilePath; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 38 | using base::Unretained; |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 39 | using std::deque; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 40 | using std::string; |
| 41 | using std::vector; |
| 42 | |
| 43 | namespace shill { |
| 44 | |
Ben Chan | bbdef5f | 2012-04-23 13:58:15 -0700 | [diff] [blame] | 45 | namespace { |
| 46 | base::LazyInstance<RoutingTable> g_routing_table = LAZY_INSTANCE_INITIALIZER; |
| 47 | } // namespace |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 48 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 49 | // static |
| 50 | const char RoutingTable::kRouteFlushPath4[] = "/proc/sys/net/ipv4/route/flush"; |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 51 | // static |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 52 | const char RoutingTable::kRouteFlushPath6[] = "/proc/sys/net/ipv6/route/flush"; |
| 53 | |
| 54 | RoutingTable::RoutingTable() |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 55 | : route_callback_(Bind(&RoutingTable::RouteMsgHandler, Unretained(this))), |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 56 | route_listener_(NULL), |
| 57 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 58 | SLOG(Route, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | RoutingTable::~RoutingTable() {} |
| 62 | |
| 63 | RoutingTable* RoutingTable::GetInstance() { |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 64 | return g_routing_table.Pointer(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void RoutingTable::Start() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 68 | SLOG(Route, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 69 | |
| 70 | route_listener_.reset( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 71 | new RTNLListener(RTNLHandler::kRequestRoute, route_callback_)); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 72 | rtnl_handler_->RequestDump(RTNLHandler::kRequestRoute); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void RoutingTable::Stop() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 76 | SLOG(Route, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 77 | |
| 78 | route_listener_.reset(); |
| 79 | } |
| 80 | |
| 81 | bool RoutingTable::AddRoute(int interface_index, |
| 82 | const RoutingTableEntry &entry) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 83 | SLOG(Route, 2) << __func__ << ": " |
| 84 | << "destination " << entry.dst.ToString() |
| 85 | << " index " << interface_index |
| 86 | << " gateway " << entry.gateway.ToString() |
| 87 | << " metric " << entry.metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 88 | |
| 89 | CHECK(!entry.from_rtnl); |
| 90 | if (!ApplyRoute(interface_index, |
| 91 | entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 92 | RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 93 | NLM_F_CREATE | NLM_F_EXCL)) { |
| 94 | return false; |
| 95 | } |
| 96 | tables_[interface_index].push_back(entry); |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | bool RoutingTable::GetDefaultRoute(int interface_index, |
| 101 | IPAddress::Family family, |
| 102 | RoutingTableEntry *entry) { |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 103 | 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 | |
| 111 | bool RoutingTable::GetDefaultRouteInternal(int interface_index, |
| 112 | IPAddress::Family family, |
| 113 | RoutingTableEntry **entry) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 114 | SLOG(Route, 2) << __func__ << " index " << interface_index |
| 115 | << " family " << IPAddress::GetAddressFamilyName(family); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 116 | |
| 117 | base::hash_map<int, vector<RoutingTableEntry> >::iterator table = |
| 118 | tables_.find(interface_index); |
| 119 | |
| 120 | if (table == tables_.end()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 121 | SLOG(Route, 2) << __func__ << " no table"; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 122 | 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 agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 129 | *entry = &(*nent); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 130 | SLOG(Route, 2) << __func__ << ": found" |
| 131 | << " gateway " << nent->gateway.ToString() |
| 132 | << " metric " << nent->metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | } |
| 136 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 137 | SLOG(Route, 2) << __func__ << " no route"; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 138 | return false; |
| 139 | } |
| 140 | |
| 141 | bool RoutingTable::SetDefaultRoute(int interface_index, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 142 | const IPAddress &gateway_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 143 | uint32 metric) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 144 | SLOG(Route, 2) << __func__ << " index " << interface_index |
| 145 | << " metric " << metric; |
mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 146 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 147 | RoutingTableEntry *old_entry; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 148 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 149 | if (GetDefaultRouteInternal(interface_index, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 150 | gateway_address.family(), |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 151 | &old_entry)) { |
| 152 | if (old_entry->gateway.Equals(gateway_address)) { |
| 153 | if (old_entry->metric != metric) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 154 | ReplaceMetric(interface_index, old_entry, metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 155 | } |
| 156 | return true; |
| 157 | } else { |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 158 | // TODO(quiche): Update internal state as well? |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 159 | ApplyRoute(interface_index, |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 160 | *old_entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 161 | RTNLMessage::kModeDelete, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 162 | 0); |
| 163 | } |
| 164 | } |
| 165 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 166 | IPAddress default_address(gateway_address.family()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 167 | default_address.SetAddressToDefault(); |
| 168 | |
| 169 | return AddRoute(interface_index, |
| 170 | RoutingTableEntry(default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 171 | default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 172 | gateway_address, |
| 173 | metric, |
| 174 | RT_SCOPE_UNIVERSE, |
| 175 | false)); |
| 176 | } |
| 177 | |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 178 | bool 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 Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 189 | SLOG(Route, 3) << "Installing route:" |
| 190 | << " Destination: " << it->host |
| 191 | << " Netmask: " << it->netmask |
| 192 | << " Gateway: " << it->gateway; |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 193 | 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 Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 223 | void RoutingTable::FlushRoutes(int interface_index) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 224 | SLOG(Route, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 225 | |
| 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 Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 236 | ApplyRoute(interface_index, *nent, RTNLMessage::kModeDelete, 0); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 237 | } |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 238 | table->second.clear(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 241 | void 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 Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 259 | void RoutingTable::ResetTable(int interface_index) { |
| 260 | tables_.erase(interface_index); |
| 261 | } |
| 262 | |
| 263 | void RoutingTable::SetDefaultMetric(int interface_index, uint32 metric) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 264 | SLOG(Route, 2) << __func__ << " index " << interface_index |
| 265 | << " metric " << metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 266 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 267 | RoutingTableEntry *entry; |
| 268 | if (GetDefaultRouteInternal( |
| 269 | interface_index, IPAddress::kFamilyIPv4, &entry) && |
| 270 | entry->metric != metric) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 271 | ReplaceMetric(interface_index, entry, metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 272 | } |
| 273 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 274 | if (GetDefaultRouteInternal( |
| 275 | interface_index, IPAddress::kFamilyIPv6, &entry) && |
| 276 | entry->metric != metric) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 277 | ReplaceMetric(interface_index, entry, metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 281 | // static |
| 282 | bool 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 Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 291 | const RTNLMessage::RouteStatus &route_status = message.route_status(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 292 | |
| 293 | if (route_status.type != RTN_UNICAST || |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 294 | route_status.table != RT_TABLE_MAIN) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 295 | return false; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 298 | uint32 interface_index_u32 = 0; |
| 299 | if (!message.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&interface_index_u32)) { |
| 300 | return false; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 301 | } |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 302 | *interface_index = interface_index_u32; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 303 | |
| 304 | uint32 metric = 0; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 305 | if (message.HasAttribute(RTA_PRIORITY)) { |
| 306 | message.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 309 | IPAddress default_addr(message.family()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 310 | default_addr.SetAddressToDefault(); |
| 311 | |
| 312 | ByteString dst_bytes(default_addr.address()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 313 | if (message.HasAttribute(RTA_DST)) { |
| 314 | dst_bytes = message.GetAttribute(RTA_DST); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 315 | } |
| 316 | ByteString src_bytes(default_addr.address()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 317 | if (message.HasAttribute(RTA_SRC)) { |
| 318 | src_bytes = message.GetAttribute(RTA_SRC); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 319 | } |
| 320 | ByteString gateway_bytes(default_addr.address()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 321 | if (message.HasAttribute(RTA_GATEWAY)) { |
| 322 | gateway_bytes = message.GetAttribute(RTA_GATEWAY); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 325 | 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 | |
| 335 | void 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 Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 343 | if (!route_queries_.empty() && |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 344 | message.route_status().protocol == RTPROT_UNSPEC) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 345 | SLOG(Route, 3) << __func__ << ": Message seq: " << message.seq() |
| 346 | << " mode " << message.mode() |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 347 | << ", next query seq: " << route_queries_.front().sequence; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 348 | |
| 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 Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 352 | while (route_queries_.front().sequence - message.seq() > kuint32max / 2) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 353 | LOG(ERROR) << __func__ << ": Purging un-replied route request sequence " |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 354 | << route_queries_.front().sequence |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 355 | << " (< " << message.seq() << ")"; |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 356 | route_queries_.pop_front(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 357 | if (route_queries_.empty()) |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 361 | 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 Stewart | bbed76d | 2012-04-27 20:02:13 -0700 | [diff] [blame] | 368 | 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 Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 372 | << add_entry.dst.ToString(); |
| 373 | added = AddRoute(interface_index, add_entry); |
Paul Stewart | bbed76d | 2012-04-27 20:02:13 -0700 | [diff] [blame] | 374 | } |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 375 | 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 Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 380 | } |
| 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 Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 388 | |
| 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 Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 393 | nent->src.Equals(entry.src) && |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 394 | nent->gateway.Equals(entry.gateway) && |
| 395 | nent->scope == entry.scope) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 396 | if (message.mode() == RTNLMessage::kModeDelete && |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 397 | nent->metric == entry.metric) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 398 | table.erase(nent); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 399 | } else if (message.mode() == RTNLMessage::kModeAdd) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 400 | nent->from_rtnl = true; |
| 401 | nent->metric = entry.metric; |
| 402 | } |
| 403 | return; |
| 404 | } |
| 405 | } |
| 406 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 407 | if (message.mode() == RTNLMessage::kModeAdd) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 408 | SLOG(Route, 2) << __func__ << " adding" |
| 409 | << " destination " << entry.dst.ToString() |
| 410 | << " index " << interface_index |
| 411 | << " gateway " << entry.gateway.ToString() |
| 412 | << " metric " << entry.metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 413 | table.push_back(entry); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | bool RoutingTable::ApplyRoute(uint32 interface_index, |
| 418 | const RoutingTableEntry &entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 419 | RTNLMessage::Mode mode, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 420 | unsigned int flags) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 421 | 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 Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 426 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 427 | RTNLMessage message( |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 428 | RTNLMessage::kTypeRoute, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 429 | mode, |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 430 | NLM_F_REQUEST | flags, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 431 | 0, |
| 432 | 0, |
| 433 | 0, |
| 434 | entry.dst.family()); |
| 435 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 436 | message.set_route_status(RTNLMessage::RouteStatus( |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 437 | entry.dst.prefix(), |
| 438 | entry.src.prefix(), |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 439 | RT_TABLE_MAIN, |
| 440 | RTPROT_BOOT, |
| 441 | entry.scope, |
| 442 | RTN_UNICAST, |
| 443 | 0)); |
| 444 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 445 | message.SetAttribute(RTA_DST, entry.dst.address()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 446 | if (!entry.src.IsDefault()) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 447 | message.SetAttribute(RTA_SRC, entry.src.address()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 448 | } |
| 449 | if (!entry.gateway.IsDefault()) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 450 | message.SetAttribute(RTA_GATEWAY, entry.gateway.address()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 451 | } |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 452 | message.SetAttribute(RTA_PRIORITY, |
| 453 | ByteString::CreateFromCPUUInt32(entry.metric)); |
| 454 | message.SetAttribute(RTA_OIF, |
| 455 | ByteString::CreateFromCPUUInt32(interface_index)); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 456 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 457 | return rtnl_handler_->SendMessage(&message); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 460 | // 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. |
| 466 | void RoutingTable::ReplaceMetric(uint32 interface_index, |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 467 | RoutingTableEntry *entry, |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 468 | uint32 metric) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 469 | SLOG(Route, 2) << __func__ << " index " << interface_index |
| 470 | << " metric " << metric; |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 471 | RoutingTableEntry new_entry = *entry; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 472 | 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 agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 477 | ApplyRoute(interface_index, *entry, RTNLMessage::kModeDelete, 0); |
| 478 | // Now, update our routing table (via |*entry|) from |new_entry|. |
| 479 | *entry = new_entry; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 482 | bool RoutingTable::FlushCache() { |
| 483 | static const char *kPaths[2] = { kRouteFlushPath4, kRouteFlushPath6 }; |
| 484 | bool ret = true; |
| 485 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 486 | SLOG(Route, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 487 | |
| 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 Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 499 | bool RoutingTable::RequestRouteToHost(const IPAddress &address, |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 500 | int interface_index, |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 501 | int tag, |
| 502 | const Query::Callback &callback) { |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 503 | // Make sure we don't get a cached response that is no longer valid. |
| 504 | FlushCache(); |
| 505 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 506 | 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 Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 519 | |
| 520 | if (interface_index != -1) { |
| 521 | message.SetAttribute(RTA_OIF, |
| 522 | ByteString::CreateFromCPUUInt32(interface_index)); |
| 523 | } |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 524 | |
| 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 Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 531 | route_queries_.push_back(Query(message.seq(), tag, callback)); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 532 | |
| 533 | return true; |
| 534 | } |
| 535 | |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 536 | bool RoutingTable::CreateBlackholeRoute(int interface_index, |
Ben Chan | a6bfe87 | 2012-09-26 09:48:34 -0700 | [diff] [blame] | 537 | IPAddress::Family family, |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 538 | uint32 metric) { |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 539 | 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 Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 570 | bool 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 Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 597 | } // namespace shill |