routing_table.cc revision 08a4ffb4ecf5893eb55c523d528bf3e52c66facf
1// Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 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 21#include <base/bind.h> 22#include <base/file_path.h> 23#include <base/file_util.h> 24#include <base/hash_tables.h> 25#include <base/logging.h> 26#include <base/memory/scoped_ptr.h> 27#include <base/stl_util.h> 28#include <base/stringprintf.h> 29 30#include "shill/byte_string.h" 31#include "shill/routing_table_entry.h" 32#include "shill/rtnl_handler.h" 33#include "shill/rtnl_listener.h" 34#include "shill/rtnl_message.h" 35 36using base::Bind; 37using base::Unretained; 38using std::string; 39using std::vector; 40 41namespace shill { 42 43// TODO(ers): not using LAZY_INSTANCE_INITIALIZER 44// because of http://crbug.com/114828 45static base::LazyInstance<RoutingTable> g_routing_table = {0, {{0}}}; 46 47// static 48const char RoutingTable::kRouteFlushPath4[] = "/proc/sys/net/ipv4/route/flush"; 49// static 50const char RoutingTable::kRouteFlushPath6[] = "/proc/sys/net/ipv6/route/flush"; 51 52RoutingTable::RoutingTable() 53 : route_callback_(Bind(&RoutingTable::RouteMsgHandler, Unretained(this))), 54 route_listener_(NULL) { 55 VLOG(2) << __func__; 56} 57 58RoutingTable::~RoutingTable() {} 59 60RoutingTable* RoutingTable::GetInstance() { 61 return g_routing_table.Pointer(); 62} 63 64void RoutingTable::Start() { 65 VLOG(2) << __func__; 66 67 route_listener_.reset( 68 new RTNLListener(RTNLHandler::kRequestRoute, route_callback_)); 69 RTNLHandler::GetInstance()->RequestDump( 70 RTNLHandler::kRequestRoute); 71} 72 73void RoutingTable::Stop() { 74 VLOG(2) << __func__; 75 76 route_listener_.reset(); 77} 78 79bool RoutingTable::AddRoute(int interface_index, 80 const RoutingTableEntry &entry) { 81 VLOG(2) << __func__ << " " 82 << "index " << interface_index << " " 83 << "gateway " << entry.gateway.ToString() << " " 84 << "metric " << entry.metric; 85 86 CHECK(!entry.from_rtnl); 87 if (!ApplyRoute(interface_index, 88 entry, 89 RTNLMessage::kModeAdd, 90 NLM_F_CREATE | NLM_F_EXCL)) { 91 return false; 92 } 93 tables_[interface_index].push_back(entry); 94 return true; 95} 96 97bool RoutingTable::GetDefaultRoute(int interface_index, 98 IPAddress::Family family, 99 RoutingTableEntry *entry) { 100 RoutingTableEntry *found_entry; 101 bool ret = GetDefaultRouteInternal(interface_index, family, &found_entry); 102 if (ret) { 103 *entry = *found_entry; 104 } 105 return ret; 106} 107 108bool RoutingTable::GetDefaultRouteInternal(int interface_index, 109 IPAddress::Family family, 110 RoutingTableEntry **entry) { 111 VLOG(2) << __func__ << " index " << interface_index << " family " << family; 112 113 base::hash_map<int, vector<RoutingTableEntry> >::iterator table = 114 tables_.find(interface_index); 115 116 if (table == tables_.end()) { 117 VLOG(2) << __func__ << " no table"; 118 return false; 119 } 120 121 vector<RoutingTableEntry>::iterator nent; 122 123 for (nent = table->second.begin(); nent != table->second.end(); ++nent) { 124 if (nent->dst.IsDefault() && nent->dst.family() == family) { 125 *entry = &(*nent); 126 VLOG(2) << __func__ << " found " 127 << "gateway " << nent->gateway.ToString() << " " 128 << "metric " << nent->metric; 129 return true; 130 } 131 } 132 133 VLOG(2) << __func__ << " no route"; 134 return false; 135} 136 137bool RoutingTable::SetDefaultRoute(int interface_index, 138 const IPConfigRefPtr &ipconfig, 139 uint32 metric) { 140 VLOG(2) << __func__ << " index " << interface_index << " metric " << metric; 141 142 const IPConfig::Properties &ipconfig_props = ipconfig->properties(); 143 RoutingTableEntry *old_entry; 144 IPAddress gateway_address(ipconfig_props.address_family); 145 if (!gateway_address.SetAddressFromString(ipconfig_props.gateway)) { 146 return false; 147 } 148 149 if (GetDefaultRouteInternal(interface_index, 150 ipconfig_props.address_family, 151 &old_entry)) { 152 if (old_entry->gateway.Equals(gateway_address)) { 153 if (old_entry->metric != metric) { 154 ReplaceMetric(interface_index, old_entry, metric); 155 } 156 return true; 157 } else { 158 // TODO(quiche): Update internal state as well? 159 ApplyRoute(interface_index, 160 *old_entry, 161 RTNLMessage::kModeDelete, 162 0); 163 } 164 } 165 166 IPAddress default_address(ipconfig_props.address_family); 167 default_address.SetAddressToDefault(); 168 169 return AddRoute(interface_index, 170 RoutingTableEntry(default_address, 171 default_address, 172 gateway_address, 173 metric, 174 RT_SCOPE_UNIVERSE, 175 false)); 176} 177 178void RoutingTable::FlushRoutes(int interface_index) { 179 VLOG(2) << __func__; 180 181 base::hash_map<int, vector<RoutingTableEntry> >::iterator table = 182 tables_.find(interface_index); 183 184 if (table == tables_.end()) { 185 return; 186 } 187 188 vector<RoutingTableEntry>::iterator nent; 189 190 for (nent = table->second.begin(); nent != table->second.end(); ++nent) { 191 ApplyRoute(interface_index, *nent, RTNLMessage::kModeDelete, 0); 192 } 193 table->second.clear(); 194} 195 196void RoutingTable::ResetTable(int interface_index) { 197 tables_.erase(interface_index); 198} 199 200void RoutingTable::SetDefaultMetric(int interface_index, uint32 metric) { 201 VLOG(2) << __func__ << " " 202 << "index " << interface_index << " metric " << metric; 203 204 RoutingTableEntry *entry; 205 if (GetDefaultRouteInternal( 206 interface_index, IPAddress::kFamilyIPv4, &entry) && 207 entry->metric != metric) { 208 ReplaceMetric(interface_index, entry, metric); 209 } 210 211 if (GetDefaultRouteInternal( 212 interface_index, IPAddress::kFamilyIPv6, &entry) && 213 entry->metric != metric) { 214 ReplaceMetric(interface_index, entry, metric); 215 } 216} 217 218void RoutingTable::RouteMsgHandler(const RTNLMessage &msg) { 219 if (msg.type() != RTNLMessage::kTypeRoute || 220 msg.family() == IPAddress::kFamilyUnknown || 221 !msg.HasAttribute(RTA_OIF)) { 222 return; 223 } 224 225 const RTNLMessage::RouteStatus &route_status = msg.route_status(); 226 227 if (route_status.type != RTN_UNICAST || 228 route_status.protocol != RTPROT_BOOT || 229 route_status.table != RT_TABLE_MAIN) { 230 return; 231 } 232 233 uint32 interface_index = 0; 234 if (!msg.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&interface_index)) { 235 return; 236 } 237 238 uint32 metric = 0; 239 if (msg.HasAttribute(RTA_PRIORITY)) { 240 msg.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric); 241 } 242 243 IPAddress default_addr(msg.family()); 244 default_addr.SetAddressToDefault(); 245 246 ByteString dst_bytes(default_addr.address()); 247 if (msg.HasAttribute(RTA_DST)) { 248 dst_bytes = msg.GetAttribute(RTA_DST); 249 } 250 ByteString src_bytes(default_addr.address()); 251 if (msg.HasAttribute(RTA_SRC)) { 252 src_bytes = msg.GetAttribute(RTA_SRC); 253 } 254 ByteString gateway_bytes(default_addr.address()); 255 if (msg.HasAttribute(RTA_GATEWAY)) { 256 gateway_bytes = msg.GetAttribute(RTA_GATEWAY); 257 } 258 259 RoutingTableEntry entry( 260 IPAddress(msg.family(), dst_bytes, route_status.dst_prefix), 261 IPAddress(msg.family(), src_bytes, route_status.src_prefix), 262 IPAddress(msg.family(), gateway_bytes), 263 metric, 264 route_status.scope, 265 true); 266 267 vector<RoutingTableEntry> &table = tables_[interface_index]; 268 vector<RoutingTableEntry>::iterator nent; 269 for (nent = table.begin(); nent != table.end(); ++nent) { 270 if (nent->dst.Equals(entry.dst) && 271 nent->src.Equals(entry.src) && 272 nent->gateway.Equals(entry.gateway) && 273 nent->scope == entry.scope) { 274 if (msg.mode() == RTNLMessage::kModeDelete && 275 nent->metric == entry.metric) { 276 table.erase(nent); 277 } else if (msg.mode() == RTNLMessage::kModeAdd) { 278 nent->from_rtnl = true; 279 nent->metric = entry.metric; 280 } 281 return; 282 } 283 } 284 285 if (msg.mode() == RTNLMessage::kModeAdd) { 286 VLOG(2) << __func__ << " adding " 287 << "index " << interface_index 288 << "gateway " << entry.gateway.ToString() << " " 289 << "metric " << entry.metric; 290 table.push_back(entry); 291 } 292} 293 294bool RoutingTable::ApplyRoute(uint32 interface_index, 295 const RoutingTableEntry &entry, 296 RTNLMessage::Mode mode, 297 unsigned int flags) { 298 VLOG(2) << base::StringPrintf("%s: dst %s index %d mode %d flags 0x%x", 299 __func__, entry.dst.ToString().c_str(), 300 interface_index, mode, flags); 301 302 RTNLMessage msg( 303 RTNLMessage::kTypeRoute, 304 mode, 305 NLM_F_REQUEST | flags, 306 0, 307 0, 308 0, 309 entry.dst.family()); 310 311 msg.set_route_status(RTNLMessage::RouteStatus( 312 entry.dst.prefix(), 313 entry.src.prefix(), 314 RT_TABLE_MAIN, 315 RTPROT_BOOT, 316 entry.scope, 317 RTN_UNICAST, 318 0)); 319 320 msg.SetAttribute(RTA_DST, entry.dst.address()); 321 if (!entry.src.IsDefault()) { 322 msg.SetAttribute(RTA_SRC, entry.src.address()); 323 } 324 if (!entry.gateway.IsDefault()) { 325 msg.SetAttribute(RTA_GATEWAY, entry.gateway.address()); 326 } 327 msg.SetAttribute(RTA_PRIORITY, ByteString::CreateFromCPUUInt32(entry.metric)); 328 msg.SetAttribute(RTA_OIF, ByteString::CreateFromCPUUInt32(interface_index)); 329 330 return RTNLHandler::GetInstance()->SendMessage(&msg); 331} 332 333// Somewhat surprisingly, the kernel allows you to create multiple routes 334// to the same destination through the same interface with different metrics. 335// Therefore, to change the metric on a route, we can't just use the 336// NLM_F_REPLACE flag by itself. We have to explicitly remove the old route. 337// We do so after creating the route at a new metric so there is no traffic 338// disruption to existing network streams. 339void RoutingTable::ReplaceMetric(uint32 interface_index, 340 RoutingTableEntry *entry, 341 uint32 metric) { 342 VLOG(2) << __func__ << " " 343 << "index " << interface_index << " metric " << metric; 344 RoutingTableEntry new_entry = *entry; 345 new_entry.metric = metric; 346 // First create the route at the new metric. 347 ApplyRoute(interface_index, new_entry, RTNLMessage::kModeAdd, 348 NLM_F_CREATE | NLM_F_REPLACE); 349 // Then delete the route at the old metric. 350 ApplyRoute(interface_index, *entry, RTNLMessage::kModeDelete, 0); 351 // Now, update our routing table (via |*entry|) from |new_entry|. 352 *entry = new_entry; 353} 354 355bool RoutingTable::FlushCache() { 356 static const char *kPaths[2] = { kRouteFlushPath4, kRouteFlushPath6 }; 357 bool ret = true; 358 359 VLOG(2) << __func__; 360 361 for (size_t i = 0; i < arraysize(kPaths); ++i) { 362 if (file_util::WriteFile(FilePath(kPaths[i]), "-1", 2) != 2) { 363 LOG(ERROR) << base::StringPrintf("Cannot write to route flush file %s", 364 kPaths[i]); 365 ret = false; 366 } 367 } 368 369 return ret; 370} 371 372} // namespace shill 373