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