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, |
| 140 | const IPConfigRefPtr &ipconfig, |
| 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 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 144 | const IPConfig::Properties &ipconfig_props = ipconfig->properties(); |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 145 | RoutingTableEntry *old_entry; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 146 | IPAddress gateway_address(ipconfig_props.address_family); |
| 147 | if (!gateway_address.SetAddressFromString(ipconfig_props.gateway)) { |
| 148 | return false; |
| 149 | } |
| 150 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 151 | if (GetDefaultRouteInternal(interface_index, |
| 152 | ipconfig_props.address_family, |
| 153 | &old_entry)) { |
| 154 | if (old_entry->gateway.Equals(gateway_address)) { |
| 155 | if (old_entry->metric != metric) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 156 | ReplaceMetric(interface_index, old_entry, metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 157 | } |
| 158 | return true; |
| 159 | } else { |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 160 | // TODO(quiche): Update internal state as well? |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 161 | ApplyRoute(interface_index, |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 162 | *old_entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 163 | RTNLMessage::kModeDelete, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 164 | 0); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | IPAddress default_address(ipconfig_props.address_family); |
| 169 | default_address.SetAddressToDefault(); |
| 170 | |
| 171 | return AddRoute(interface_index, |
| 172 | RoutingTableEntry(default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 173 | default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 174 | gateway_address, |
| 175 | metric, |
| 176 | RT_SCOPE_UNIVERSE, |
| 177 | false)); |
| 178 | } |
| 179 | |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 180 | void RoutingTable::FlushRoutes(int interface_index) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 181 | VLOG(2) << __func__; |
| 182 | |
| 183 | base::hash_map<int, vector<RoutingTableEntry> >::iterator table = |
| 184 | tables_.find(interface_index); |
| 185 | |
| 186 | if (table == tables_.end()) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | vector<RoutingTableEntry>::iterator nent; |
| 191 | |
| 192 | for (nent = table->second.begin(); nent != table->second.end(); ++nent) { |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame] | 193 | ApplyRoute(interface_index, *nent, RTNLMessage::kModeDelete, 0); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 194 | } |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 195 | table->second.clear(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void RoutingTable::ResetTable(int interface_index) { |
| 199 | tables_.erase(interface_index); |
| 200 | } |
| 201 | |
| 202 | void RoutingTable::SetDefaultMetric(int interface_index, uint32 metric) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 203 | VLOG(2) << __func__ << " index " << interface_index << " metric " << metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 204 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 205 | RoutingTableEntry *entry; |
| 206 | if (GetDefaultRouteInternal( |
| 207 | interface_index, IPAddress::kFamilyIPv4, &entry) && |
| 208 | entry->metric != metric) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 209 | ReplaceMetric(interface_index, entry, metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 210 | } |
| 211 | |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 212 | if (GetDefaultRouteInternal( |
| 213 | interface_index, IPAddress::kFamilyIPv6, &entry) && |
| 214 | entry->metric != metric) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 215 | ReplaceMetric(interface_index, entry, metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 219 | // static |
| 220 | bool RoutingTable::ParseRoutingTableMessage(const RTNLMessage &message, |
| 221 | int *interface_index, |
| 222 | RoutingTableEntry *entry) { |
| 223 | if (message.type() != RTNLMessage::kTypeRoute || |
| 224 | message.family() == IPAddress::kFamilyUnknown || |
| 225 | !message.HasAttribute(RTA_OIF)) { |
| 226 | return false; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 229 | const RTNLMessage::RouteStatus &route_status = message.route_status(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 230 | |
| 231 | if (route_status.type != RTN_UNICAST || |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 232 | route_status.table != RT_TABLE_MAIN) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 233 | return false; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 236 | uint32 interface_index_u32 = 0; |
| 237 | if (!message.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&interface_index_u32)) { |
| 238 | return false; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 239 | } |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 240 | *interface_index = interface_index_u32; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 241 | |
| 242 | uint32 metric = 0; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 243 | if (message.HasAttribute(RTA_PRIORITY)) { |
| 244 | message.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 247 | IPAddress default_addr(message.family()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 248 | default_addr.SetAddressToDefault(); |
| 249 | |
| 250 | ByteString dst_bytes(default_addr.address()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 251 | if (message.HasAttribute(RTA_DST)) { |
| 252 | dst_bytes = message.GetAttribute(RTA_DST); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 253 | } |
| 254 | ByteString src_bytes(default_addr.address()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 255 | if (message.HasAttribute(RTA_SRC)) { |
| 256 | src_bytes = message.GetAttribute(RTA_SRC); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 257 | } |
| 258 | ByteString gateway_bytes(default_addr.address()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 259 | if (message.HasAttribute(RTA_GATEWAY)) { |
| 260 | gateway_bytes = message.GetAttribute(RTA_GATEWAY); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 263 | entry->dst = IPAddress(message.family(), dst_bytes, route_status.dst_prefix); |
| 264 | entry->src = IPAddress(message.family(), src_bytes, route_status.src_prefix); |
| 265 | entry->gateway = IPAddress(message.family(), gateway_bytes); |
| 266 | entry->metric = metric; |
| 267 | entry->scope = route_status.scope; |
| 268 | entry->from_rtnl = true; |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | void RoutingTable::RouteMsgHandler(const RTNLMessage &message) { |
| 274 | int interface_index; |
| 275 | RoutingTableEntry entry; |
| 276 | |
| 277 | if (!ParseRoutingTableMessage(message, &interface_index, &entry)) { |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | if (!route_query_sequences_.empty() && |
| 282 | message.route_status().protocol == RTPROT_UNSPEC) { |
| 283 | VLOG(3) << __func__ << ": Message seq: " << message.seq() |
| 284 | << " mode " << message.mode() |
| 285 | << ", next query seq: " << route_query_sequences_.front(); |
| 286 | |
| 287 | // Purge queries that have expired (sequence number of this message is |
| 288 | // greater than that of the head of the route query sequence). Do the |
| 289 | // math in a way that's roll-over independent. |
| 290 | while (route_query_sequences_.front() - message.seq() > kuint32max / 2) { |
| 291 | LOG(ERROR) << __func__ << ": Purging un-replied route request sequence " |
| 292 | << route_query_sequences_.front() |
| 293 | << " (< " << message.seq() << ")"; |
| 294 | route_query_sequences_.pop(); |
| 295 | if (route_query_sequences_.empty()) |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | if (route_query_sequences_.front() == message.seq()) { |
| 300 | VLOG(2) << __func__ << ": Adding host route to " << entry.dst.ToString(); |
| 301 | route_query_sequences_.pop(); |
| 302 | RoutingTableEntry add_entry(entry); |
| 303 | add_entry.from_rtnl = false; |
| 304 | AddRoute(interface_index, add_entry); |
| 305 | } |
| 306 | return; |
| 307 | } else if (message.route_status().protocol != RTPROT_BOOT) { |
| 308 | // Responses to route queries come back with a protocol of |
| 309 | // RTPROT_UNSPEC. Otherwise, normal route updates that we are |
| 310 | // interested in come with a protocol of RTPROT_BOOT. |
| 311 | return; |
| 312 | } |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 313 | |
| 314 | vector<RoutingTableEntry> &table = tables_[interface_index]; |
| 315 | vector<RoutingTableEntry>::iterator nent; |
| 316 | for (nent = table.begin(); nent != table.end(); ++nent) { |
| 317 | if (nent->dst.Equals(entry.dst) && |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 318 | nent->src.Equals(entry.src) && |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 319 | nent->gateway.Equals(entry.gateway) && |
| 320 | nent->scope == entry.scope) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 321 | if (message.mode() == RTNLMessage::kModeDelete && |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 322 | nent->metric == entry.metric) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 323 | table.erase(nent); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 324 | } else if (message.mode() == RTNLMessage::kModeAdd) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 325 | nent->from_rtnl = true; |
| 326 | nent->metric = entry.metric; |
| 327 | } |
| 328 | return; |
| 329 | } |
| 330 | } |
| 331 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 332 | if (message.mode() == RTNLMessage::kModeAdd) { |
| 333 | VLOG(2) << __func__ << " adding" |
| 334 | << " destination " << entry.dst.ToString() |
| 335 | << " index " << interface_index |
| 336 | << " gateway " << entry.gateway.ToString() |
| 337 | << " metric " << entry.metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 338 | table.push_back(entry); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | bool RoutingTable::ApplyRoute(uint32 interface_index, |
| 343 | const RoutingTableEntry &entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 344 | RTNLMessage::Mode mode, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 345 | unsigned int flags) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 346 | VLOG(2) << base::StringPrintf("%s: dst %s index %d mode %d flags 0x%x", |
| 347 | __func__, entry.dst.ToString().c_str(), |
| 348 | interface_index, mode, flags); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 349 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 350 | RTNLMessage message( |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 351 | RTNLMessage::kTypeRoute, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 352 | mode, |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 353 | NLM_F_REQUEST | flags, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 354 | 0, |
| 355 | 0, |
| 356 | 0, |
| 357 | entry.dst.family()); |
| 358 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 359 | message.set_route_status(RTNLMessage::RouteStatus( |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 360 | entry.dst.prefix(), |
| 361 | entry.src.prefix(), |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 362 | RT_TABLE_MAIN, |
| 363 | RTPROT_BOOT, |
| 364 | entry.scope, |
| 365 | RTN_UNICAST, |
| 366 | 0)); |
| 367 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 368 | message.SetAttribute(RTA_DST, entry.dst.address()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 369 | if (!entry.src.IsDefault()) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 370 | message.SetAttribute(RTA_SRC, entry.src.address()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 371 | } |
| 372 | if (!entry.gateway.IsDefault()) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 373 | message.SetAttribute(RTA_GATEWAY, entry.gateway.address()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 374 | } |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 375 | message.SetAttribute(RTA_PRIORITY, |
| 376 | ByteString::CreateFromCPUUInt32(entry.metric)); |
| 377 | message.SetAttribute(RTA_OIF, |
| 378 | ByteString::CreateFromCPUUInt32(interface_index)); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 379 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 380 | return rtnl_handler_->SendMessage(&message); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 383 | // Somewhat surprisingly, the kernel allows you to create multiple routes |
| 384 | // to the same destination through the same interface with different metrics. |
| 385 | // Therefore, to change the metric on a route, we can't just use the |
| 386 | // NLM_F_REPLACE flag by itself. We have to explicitly remove the old route. |
| 387 | // We do so after creating the route at a new metric so there is no traffic |
| 388 | // disruption to existing network streams. |
| 389 | void RoutingTable::ReplaceMetric(uint32 interface_index, |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 390 | RoutingTableEntry *entry, |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 391 | uint32 metric) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 392 | VLOG(2) << __func__ << " index " << interface_index << " metric " << metric; |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 393 | RoutingTableEntry new_entry = *entry; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 394 | new_entry.metric = metric; |
| 395 | // First create the route at the new metric. |
| 396 | ApplyRoute(interface_index, new_entry, RTNLMessage::kModeAdd, |
| 397 | NLM_F_CREATE | NLM_F_REPLACE); |
| 398 | // Then delete the route at the old metric. |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 399 | ApplyRoute(interface_index, *entry, RTNLMessage::kModeDelete, 0); |
| 400 | // Now, update our routing table (via |*entry|) from |new_entry|. |
| 401 | *entry = new_entry; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 404 | bool RoutingTable::FlushCache() { |
| 405 | static const char *kPaths[2] = { kRouteFlushPath4, kRouteFlushPath6 }; |
| 406 | bool ret = true; |
| 407 | |
| 408 | VLOG(2) << __func__; |
| 409 | |
| 410 | for (size_t i = 0; i < arraysize(kPaths); ++i) { |
| 411 | if (file_util::WriteFile(FilePath(kPaths[i]), "-1", 2) != 2) { |
| 412 | LOG(ERROR) << base::StringPrintf("Cannot write to route flush file %s", |
| 413 | kPaths[i]); |
| 414 | ret = false; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 421 | bool RoutingTable::RequestRouteToHost(const IPAddress &address, |
| 422 | int interface_index) { |
| 423 | RTNLMessage message( |
| 424 | RTNLMessage::kTypeRoute, |
| 425 | RTNLMessage::kModeQuery, |
| 426 | NLM_F_REQUEST, |
| 427 | 0, |
| 428 | 0, |
| 429 | interface_index, |
| 430 | address.family()); |
| 431 | |
| 432 | RTNLMessage::RouteStatus status; |
| 433 | status.dst_prefix = address.prefix(); |
| 434 | message.set_route_status(status); |
| 435 | message.SetAttribute(RTA_DST, address.address()); |
| 436 | message.SetAttribute(RTA_OIF, |
| 437 | ByteString::CreateFromCPUUInt32(interface_index)); |
| 438 | |
| 439 | if (!rtnl_handler_->SendMessage(&message)) { |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | // Save the sequence number of the request so we can create a route for |
| 444 | // this host when we get a reply. |
| 445 | route_query_sequences_.push(message.seq()); |
| 446 | |
| 447 | return true; |
| 448 | } |
| 449 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 450 | } // namespace shill |