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> |
| 25 | #include <base/logging.h> |
| 26 | #include <base/memory/scoped_ptr.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 27 | #include <base/stl_util.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 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" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 35 | #include "shill/scope_logger.h" |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 36 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 37 | using base::Bind; |
| 38 | using base::Unretained; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 39 | using std::string; |
| 40 | using std::vector; |
| 41 | |
| 42 | namespace shill { |
| 43 | |
Ben Chan | bbdef5f | 2012-04-23 13:58:15 -0700 | [diff] [blame] | 44 | namespace { |
| 45 | base::LazyInstance<RoutingTable> g_routing_table = LAZY_INSTANCE_INITIALIZER; |
| 46 | } // namespace |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 47 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 48 | // static |
| 49 | const char RoutingTable::kRouteFlushPath4[] = "/proc/sys/net/ipv4/route/flush"; |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 50 | // static |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 51 | const char RoutingTable::kRouteFlushPath6[] = "/proc/sys/net/ipv6/route/flush"; |
| 52 | |
| 53 | RoutingTable::RoutingTable() |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 54 | : route_callback_(Bind(&RoutingTable::RouteMsgHandler, Unretained(this))), |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 55 | route_listener_(NULL), |
| 56 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 57 | SLOG(Route, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | RoutingTable::~RoutingTable() {} |
| 61 | |
| 62 | RoutingTable* RoutingTable::GetInstance() { |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 63 | return g_routing_table.Pointer(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void RoutingTable::Start() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 67 | SLOG(Route, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 68 | |
| 69 | route_listener_.reset( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 70 | new RTNLListener(RTNLHandler::kRequestRoute, route_callback_)); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 71 | rtnl_handler_->RequestDump(RTNLHandler::kRequestRoute); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void RoutingTable::Stop() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 75 | SLOG(Route, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 76 | |
| 77 | route_listener_.reset(); |
| 78 | } |
| 79 | |
| 80 | bool RoutingTable::AddRoute(int interface_index, |
| 81 | const RoutingTableEntry &entry) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 82 | SLOG(Route, 2) << __func__ << ": " |
| 83 | << "destination " << entry.dst.ToString() |
| 84 | << " index " << interface_index |
| 85 | << " gateway " << entry.gateway.ToString() |
| 86 | << " metric " << entry.metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 87 | |
| 88 | CHECK(!entry.from_rtnl); |
| 89 | if (!ApplyRoute(interface_index, |
| 90 | entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 91 | RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 92 | NLM_F_CREATE | NLM_F_EXCL)) { |
| 93 | return false; |
| 94 | } |
| 95 | tables_[interface_index].push_back(entry); |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | bool RoutingTable::GetDefaultRoute(int interface_index, |
| 100 | IPAddress::Family family, |
| 101 | RoutingTableEntry *entry) { |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 102 | 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 | |
| 110 | bool RoutingTable::GetDefaultRouteInternal(int interface_index, |
| 111 | IPAddress::Family family, |
| 112 | RoutingTableEntry **entry) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 113 | SLOG(Route, 2) << __func__ << " index " << interface_index |
| 114 | << " family " << IPAddress::GetAddressFamilyName(family); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 115 | |
| 116 | base::hash_map<int, vector<RoutingTableEntry> >::iterator table = |
| 117 | tables_.find(interface_index); |
| 118 | |
| 119 | if (table == tables_.end()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 120 | SLOG(Route, 2) << __func__ << " no table"; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 121 | 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 agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 128 | *entry = &(*nent); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 129 | SLOG(Route, 2) << __func__ << ": found" |
| 130 | << " gateway " << nent->gateway.ToString() |
| 131 | << " metric " << nent->metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | } |
| 135 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 136 | SLOG(Route, 2) << __func__ << " no route"; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
| 140 | bool RoutingTable::SetDefaultRoute(int interface_index, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 141 | const IPAddress &gateway_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 142 | uint32 metric) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 143 | SLOG(Route, 2) << __func__ << " index " << interface_index |
| 144 | << " metric " << metric; |
mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 145 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 146 | RoutingTableEntry *old_entry; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 147 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 148 | if (GetDefaultRouteInternal(interface_index, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 149 | gateway_address.family(), |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 150 | &old_entry)) { |
| 151 | if (old_entry->gateway.Equals(gateway_address)) { |
| 152 | if (old_entry->metric != metric) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 153 | ReplaceMetric(interface_index, old_entry, metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 154 | } |
| 155 | return true; |
| 156 | } else { |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 157 | // TODO(quiche): Update internal state as well? |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 158 | ApplyRoute(interface_index, |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 159 | *old_entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 160 | RTNLMessage::kModeDelete, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 161 | 0); |
| 162 | } |
| 163 | } |
| 164 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 165 | IPAddress default_address(gateway_address.family()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 166 | default_address.SetAddressToDefault(); |
| 167 | |
| 168 | return AddRoute(interface_index, |
| 169 | RoutingTableEntry(default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 170 | default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 171 | gateway_address, |
| 172 | metric, |
| 173 | RT_SCOPE_UNIVERSE, |
| 174 | false)); |
| 175 | } |
| 176 | |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 177 | bool 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 Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 188 | SLOG(Route, 3) << "Installing route:" |
| 189 | << " Destination: " << it->host |
| 190 | << " Netmask: " << it->netmask |
| 191 | << " Gateway: " << it->gateway; |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 192 | 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 Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 222 | void RoutingTable::FlushRoutes(int interface_index) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 223 | SLOG(Route, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 224 | |
| 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 Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame] | 235 | ApplyRoute(interface_index, *nent, RTNLMessage::kModeDelete, 0); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 236 | } |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 237 | table->second.clear(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 240 | void RoutingTable::FlushRoutesWithTag(int tag) { |
| 241 | SLOG(Route, 2) << __func__; |
| 242 | |
| 243 | base::hash_map<int, vector<RoutingTableEntry> >::iterator table; |
| 244 | for (table = tables_.begin(); table != tables_.end(); ++table) { |
| 245 | vector<RoutingTableEntry>::iterator nent; |
| 246 | |
| 247 | for (nent = table->second.begin(); nent != table->second.end();) { |
| 248 | if (nent->tag == tag) { |
| 249 | ApplyRoute(table->first, *nent, RTNLMessage::kModeDelete, 0); |
| 250 | nent = table->second.erase(nent); |
| 251 | } else { |
| 252 | ++nent; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 258 | void RoutingTable::ResetTable(int interface_index) { |
| 259 | tables_.erase(interface_index); |
| 260 | } |
| 261 | |
| 262 | void RoutingTable::SetDefaultMetric(int interface_index, uint32 metric) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 263 | SLOG(Route, 2) << __func__ << " index " << interface_index |
| 264 | << " metric " << metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 265 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 266 | RoutingTableEntry *entry; |
| 267 | if (GetDefaultRouteInternal( |
| 268 | interface_index, IPAddress::kFamilyIPv4, &entry) && |
| 269 | entry->metric != metric) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 270 | ReplaceMetric(interface_index, entry, metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 271 | } |
| 272 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 273 | if (GetDefaultRouteInternal( |
| 274 | interface_index, IPAddress::kFamilyIPv6, &entry) && |
| 275 | entry->metric != metric) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 276 | ReplaceMetric(interface_index, entry, metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 280 | // static |
| 281 | bool RoutingTable::ParseRoutingTableMessage(const RTNLMessage &message, |
| 282 | int *interface_index, |
| 283 | RoutingTableEntry *entry) { |
| 284 | if (message.type() != RTNLMessage::kTypeRoute || |
| 285 | message.family() == IPAddress::kFamilyUnknown || |
| 286 | !message.HasAttribute(RTA_OIF)) { |
| 287 | return false; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 290 | const RTNLMessage::RouteStatus &route_status = message.route_status(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 291 | |
| 292 | if (route_status.type != RTN_UNICAST || |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 293 | route_status.table != RT_TABLE_MAIN) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 294 | return false; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 297 | uint32 interface_index_u32 = 0; |
| 298 | if (!message.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&interface_index_u32)) { |
| 299 | return false; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 300 | } |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 301 | *interface_index = interface_index_u32; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 302 | |
| 303 | uint32 metric = 0; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 304 | if (message.HasAttribute(RTA_PRIORITY)) { |
| 305 | message.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 308 | IPAddress default_addr(message.family()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 309 | default_addr.SetAddressToDefault(); |
| 310 | |
| 311 | ByteString dst_bytes(default_addr.address()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 312 | if (message.HasAttribute(RTA_DST)) { |
| 313 | dst_bytes = message.GetAttribute(RTA_DST); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 314 | } |
| 315 | ByteString src_bytes(default_addr.address()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 316 | if (message.HasAttribute(RTA_SRC)) { |
| 317 | src_bytes = message.GetAttribute(RTA_SRC); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 318 | } |
| 319 | ByteString gateway_bytes(default_addr.address()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 320 | if (message.HasAttribute(RTA_GATEWAY)) { |
| 321 | gateway_bytes = message.GetAttribute(RTA_GATEWAY); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 324 | entry->dst = IPAddress(message.family(), dst_bytes, route_status.dst_prefix); |
| 325 | entry->src = IPAddress(message.family(), src_bytes, route_status.src_prefix); |
| 326 | entry->gateway = IPAddress(message.family(), gateway_bytes); |
| 327 | entry->metric = metric; |
| 328 | entry->scope = route_status.scope; |
| 329 | entry->from_rtnl = true; |
| 330 | |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | void RoutingTable::RouteMsgHandler(const RTNLMessage &message) { |
| 335 | int interface_index; |
| 336 | RoutingTableEntry entry; |
| 337 | |
| 338 | if (!ParseRoutingTableMessage(message, &interface_index, &entry)) { |
| 339 | return; |
| 340 | } |
| 341 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 342 | if (!route_queries_.empty() && |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 343 | message.route_status().protocol == RTPROT_UNSPEC) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 344 | SLOG(Route, 3) << __func__ << ": Message seq: " << message.seq() |
| 345 | << " mode " << message.mode() |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 346 | << ", next query seq: " << route_queries_.front().sequence; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 347 | |
| 348 | // Purge queries that have expired (sequence number of this message is |
| 349 | // greater than that of the head of the route query sequence). Do the |
| 350 | // math in a way that's roll-over independent. |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 351 | while (route_queries_.front().sequence - message.seq() > kuint32max / 2) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 352 | LOG(ERROR) << __func__ << ": Purging un-replied route request sequence " |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 353 | << route_queries_.front().sequence |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 354 | << " (< " << message.seq() << ")"; |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 355 | route_queries_.pop(); |
| 356 | if (route_queries_.empty()) |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 357 | return; |
| 358 | } |
| 359 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 360 | if (route_queries_.front().sequence == message.seq()) { |
Paul Stewart | bbed76d | 2012-04-27 20:02:13 -0700 | [diff] [blame^] | 361 | if (entry.gateway.IsDefault()) { |
| 362 | SLOG(Route, 2) << __func__ << ": Ignoring route result with no gateway " |
| 363 | << "since we don't need to plumb these."; |
| 364 | } else { |
| 365 | SLOG(Route, 2) << __func__ << ": Adding host route to " |
| 366 | << entry.dst.ToString(); |
| 367 | RoutingTableEntry add_entry(entry); |
| 368 | add_entry.from_rtnl = false; |
| 369 | add_entry.tag = route_queries_.front().tag; |
| 370 | route_queries_.pop(); |
| 371 | AddRoute(interface_index, add_entry); |
| 372 | } |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 373 | } |
| 374 | return; |
| 375 | } else if (message.route_status().protocol != RTPROT_BOOT) { |
| 376 | // Responses to route queries come back with a protocol of |
| 377 | // RTPROT_UNSPEC. Otherwise, normal route updates that we are |
| 378 | // interested in come with a protocol of RTPROT_BOOT. |
| 379 | return; |
| 380 | } |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 381 | |
| 382 | vector<RoutingTableEntry> &table = tables_[interface_index]; |
| 383 | vector<RoutingTableEntry>::iterator nent; |
| 384 | for (nent = table.begin(); nent != table.end(); ++nent) { |
| 385 | if (nent->dst.Equals(entry.dst) && |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 386 | nent->src.Equals(entry.src) && |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 387 | nent->gateway.Equals(entry.gateway) && |
| 388 | nent->scope == entry.scope) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 389 | if (message.mode() == RTNLMessage::kModeDelete && |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 390 | nent->metric == entry.metric) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 391 | table.erase(nent); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 392 | } else if (message.mode() == RTNLMessage::kModeAdd) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 393 | nent->from_rtnl = true; |
| 394 | nent->metric = entry.metric; |
| 395 | } |
| 396 | return; |
| 397 | } |
| 398 | } |
| 399 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 400 | if (message.mode() == RTNLMessage::kModeAdd) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 401 | SLOG(Route, 2) << __func__ << " adding" |
| 402 | << " destination " << entry.dst.ToString() |
| 403 | << " index " << interface_index |
| 404 | << " gateway " << entry.gateway.ToString() |
| 405 | << " metric " << entry.metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 406 | table.push_back(entry); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | bool RoutingTable::ApplyRoute(uint32 interface_index, |
| 411 | const RoutingTableEntry &entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 412 | RTNLMessage::Mode mode, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 413 | unsigned int flags) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 414 | SLOG(Route, 2) << base::StringPrintf( |
| 415 | "%s: dst %s/%d src %s/%d index %d mode %d flags 0x%x", |
| 416 | __func__, entry.dst.ToString().c_str(), entry.dst.prefix(), |
| 417 | entry.src.ToString().c_str(), entry.src.prefix(), |
| 418 | interface_index, mode, flags); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 419 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 420 | RTNLMessage message( |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 421 | RTNLMessage::kTypeRoute, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 422 | mode, |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 423 | NLM_F_REQUEST | flags, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 424 | 0, |
| 425 | 0, |
| 426 | 0, |
| 427 | entry.dst.family()); |
| 428 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 429 | message.set_route_status(RTNLMessage::RouteStatus( |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 430 | entry.dst.prefix(), |
| 431 | entry.src.prefix(), |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 432 | RT_TABLE_MAIN, |
| 433 | RTPROT_BOOT, |
| 434 | entry.scope, |
| 435 | RTN_UNICAST, |
| 436 | 0)); |
| 437 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 438 | message.SetAttribute(RTA_DST, entry.dst.address()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 439 | if (!entry.src.IsDefault()) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 440 | message.SetAttribute(RTA_SRC, entry.src.address()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 441 | } |
| 442 | if (!entry.gateway.IsDefault()) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 443 | message.SetAttribute(RTA_GATEWAY, entry.gateway.address()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 444 | } |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 445 | message.SetAttribute(RTA_PRIORITY, |
| 446 | ByteString::CreateFromCPUUInt32(entry.metric)); |
| 447 | message.SetAttribute(RTA_OIF, |
| 448 | ByteString::CreateFromCPUUInt32(interface_index)); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 449 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 450 | return rtnl_handler_->SendMessage(&message); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 453 | // Somewhat surprisingly, the kernel allows you to create multiple routes |
| 454 | // to the same destination through the same interface with different metrics. |
| 455 | // Therefore, to change the metric on a route, we can't just use the |
| 456 | // NLM_F_REPLACE flag by itself. We have to explicitly remove the old route. |
| 457 | // We do so after creating the route at a new metric so there is no traffic |
| 458 | // disruption to existing network streams. |
| 459 | void RoutingTable::ReplaceMetric(uint32 interface_index, |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 460 | RoutingTableEntry *entry, |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 461 | uint32 metric) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 462 | SLOG(Route, 2) << __func__ << " index " << interface_index |
| 463 | << " metric " << metric; |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 464 | RoutingTableEntry new_entry = *entry; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 465 | new_entry.metric = metric; |
| 466 | // First create the route at the new metric. |
| 467 | ApplyRoute(interface_index, new_entry, RTNLMessage::kModeAdd, |
| 468 | NLM_F_CREATE | NLM_F_REPLACE); |
| 469 | // Then delete the route at the old metric. |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 470 | ApplyRoute(interface_index, *entry, RTNLMessage::kModeDelete, 0); |
| 471 | // Now, update our routing table (via |*entry|) from |new_entry|. |
| 472 | *entry = new_entry; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 475 | bool RoutingTable::FlushCache() { |
| 476 | static const char *kPaths[2] = { kRouteFlushPath4, kRouteFlushPath6 }; |
| 477 | bool ret = true; |
| 478 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 479 | SLOG(Route, 2) << __func__; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 480 | |
| 481 | for (size_t i = 0; i < arraysize(kPaths); ++i) { |
| 482 | if (file_util::WriteFile(FilePath(kPaths[i]), "-1", 2) != 2) { |
| 483 | LOG(ERROR) << base::StringPrintf("Cannot write to route flush file %s", |
| 484 | kPaths[i]); |
| 485 | ret = false; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return ret; |
| 490 | } |
| 491 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 492 | bool RoutingTable::RequestRouteToHost(const IPAddress &address, |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 493 | int interface_index, |
| 494 | int tag) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 495 | RTNLMessage message( |
| 496 | RTNLMessage::kTypeRoute, |
| 497 | RTNLMessage::kModeQuery, |
| 498 | NLM_F_REQUEST, |
| 499 | 0, |
| 500 | 0, |
| 501 | interface_index, |
| 502 | address.family()); |
| 503 | |
| 504 | RTNLMessage::RouteStatus status; |
| 505 | status.dst_prefix = address.prefix(); |
| 506 | message.set_route_status(status); |
| 507 | message.SetAttribute(RTA_DST, address.address()); |
Paul Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 508 | |
| 509 | if (interface_index != -1) { |
| 510 | message.SetAttribute(RTA_OIF, |
| 511 | ByteString::CreateFromCPUUInt32(interface_index)); |
| 512 | } |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 513 | |
| 514 | if (!rtnl_handler_->SendMessage(&message)) { |
| 515 | return false; |
| 516 | } |
| 517 | |
| 518 | // Save the sequence number of the request so we can create a route for |
| 519 | // this host when we get a reply. |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 520 | route_queries_.push(Query(message.seq(), tag)); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 521 | |
| 522 | return true; |
| 523 | } |
| 524 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 525 | } // namespace shill |