Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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/callback_old.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-inl.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 | |
| 36 | using std::string; |
| 37 | using std::vector; |
| 38 | |
| 39 | namespace shill { |
| 40 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 41 | static base::LazyInstance<RoutingTable> g_routing_table( |
| 42 | base::LINKER_INITIALIZED); |
| 43 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 44 | // static |
| 45 | const char RoutingTable::kRouteFlushPath4[] = "/proc/sys/net/ipv4/route/flush"; |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 46 | // static |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 47 | const char RoutingTable::kRouteFlushPath6[] = "/proc/sys/net/ipv6/route/flush"; |
| 48 | |
| 49 | RoutingTable::RoutingTable() |
| 50 | : route_callback_(NewCallback(this, &RoutingTable::RouteMsgHandler)), |
| 51 | route_listener_(NULL) { |
| 52 | VLOG(2) << __func__; |
| 53 | } |
| 54 | |
| 55 | RoutingTable::~RoutingTable() {} |
| 56 | |
| 57 | RoutingTable* RoutingTable::GetInstance() { |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 58 | return g_routing_table.Pointer(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void RoutingTable::Start() { |
| 62 | VLOG(2) << __func__; |
| 63 | |
| 64 | route_listener_.reset( |
| 65 | new RTNLListener(RTNLHandler::kRequestRoute, route_callback_.get())); |
| 66 | RTNLHandler::GetInstance()->RequestDump( |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 67 | RTNLHandler::kRequestRoute); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void RoutingTable::Stop() { |
| 71 | VLOG(2) << __func__; |
| 72 | |
| 73 | route_listener_.reset(); |
| 74 | } |
| 75 | |
| 76 | bool RoutingTable::AddRoute(int interface_index, |
| 77 | const RoutingTableEntry &entry) { |
| 78 | VLOG(2) << __func__; |
| 79 | |
| 80 | CHECK(!entry.from_rtnl); |
| 81 | if (!ApplyRoute(interface_index, |
| 82 | entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 83 | RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 84 | NLM_F_CREATE | NLM_F_EXCL)) { |
| 85 | return false; |
| 86 | } |
| 87 | tables_[interface_index].push_back(entry); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | bool RoutingTable::GetDefaultRoute(int interface_index, |
| 92 | IPAddress::Family family, |
| 93 | RoutingTableEntry *entry) { |
| 94 | VLOG(2) << __func__; |
| 95 | |
| 96 | base::hash_map<int, vector<RoutingTableEntry> >::iterator table = |
| 97 | tables_.find(interface_index); |
| 98 | |
| 99 | if (table == tables_.end()) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | vector<RoutingTableEntry>::iterator nent; |
| 104 | |
| 105 | for (nent = table->second.begin(); nent != table->second.end(); ++nent) { |
| 106 | if (nent->dst.IsDefault() && nent->dst.family() == family) { |
| 107 | *entry = *nent; |
| 108 | return true; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | bool RoutingTable::SetDefaultRoute(int interface_index, |
| 116 | const IPConfigRefPtr &ipconfig, |
| 117 | uint32 metric) { |
| 118 | const IPConfig::Properties &ipconfig_props = ipconfig->properties(); |
| 119 | RoutingTableEntry old_entry; |
| 120 | |
| 121 | VLOG(2) << __func__; |
| 122 | |
| 123 | IPAddress gateway_address(ipconfig_props.address_family); |
| 124 | if (!gateway_address.SetAddressFromString(ipconfig_props.gateway)) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | if (GetDefaultRoute(interface_index, |
| 129 | ipconfig_props.address_family, |
| 130 | &old_entry)) { |
| 131 | if (old_entry.gateway.Equals(gateway_address)) { |
| 132 | if (old_entry.metric != metric) { |
| 133 | old_entry.metric = metric; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 134 | ApplyRoute(interface_index, old_entry, RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 135 | NLM_F_CREATE | NLM_F_REPLACE); |
| 136 | } |
| 137 | return true; |
| 138 | } else { |
| 139 | ApplyRoute(interface_index, |
| 140 | old_entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 141 | RTNLMessage::kModeDelete, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 142 | 0); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | IPAddress default_address(ipconfig_props.address_family); |
| 147 | default_address.SetAddressToDefault(); |
| 148 | |
| 149 | return AddRoute(interface_index, |
| 150 | RoutingTableEntry(default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 151 | default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 152 | gateway_address, |
| 153 | metric, |
| 154 | RT_SCOPE_UNIVERSE, |
| 155 | false)); |
| 156 | } |
| 157 | |
| 158 | void RoutingTable::FlushRoutes(int interface_index) { |
| 159 | VLOG(2) << __func__; |
| 160 | |
| 161 | base::hash_map<int, vector<RoutingTableEntry> >::iterator table = |
| 162 | tables_.find(interface_index); |
| 163 | |
| 164 | if (table == tables_.end()) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | vector<RoutingTableEntry>::iterator nent; |
| 169 | |
| 170 | for (nent = table->second.begin(); nent != table->second.end(); ++nent) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 171 | ApplyRoute(interface_index, *nent, RTNLMessage::kModeDelete, 0); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | void RoutingTable::ResetTable(int interface_index) { |
| 176 | tables_.erase(interface_index); |
| 177 | } |
| 178 | |
| 179 | void RoutingTable::SetDefaultMetric(int interface_index, uint32 metric) { |
| 180 | RoutingTableEntry entry; |
| 181 | |
| 182 | VLOG(2) << __func__; |
| 183 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 184 | if (GetDefaultRoute(interface_index, IPAddress::kFamilyIPv4, &entry) && |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 185 | entry.metric != metric) { |
| 186 | entry.metric = metric; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 187 | ApplyRoute(interface_index, entry, RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 188 | NLM_F_CREATE | NLM_F_REPLACE); |
| 189 | } |
| 190 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 191 | if (GetDefaultRoute(interface_index, IPAddress::kFamilyIPv6, &entry) && |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 192 | entry.metric != metric) { |
| 193 | entry.metric = metric; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 194 | ApplyRoute(interface_index, entry, RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 195 | NLM_F_CREATE | NLM_F_REPLACE); |
| 196 | } |
| 197 | } |
| 198 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 199 | void RoutingTable::RouteMsgHandler(const RTNLMessage &msg) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 200 | VLOG(2) << __func__; |
| 201 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 202 | if (msg.type() != RTNLMessage::kTypeRoute || |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 203 | msg.family() == IPAddress::kFamilyUnknown || |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 204 | !msg.HasAttribute(RTA_OIF)) { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | const RTNLMessage::RouteStatus &route_status = msg.route_status(); |
| 209 | |
| 210 | if (route_status.type != RTN_UNICAST || |
| 211 | route_status.protocol != RTPROT_BOOT || |
| 212 | route_status.table != RT_TABLE_MAIN) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | uint32 interface_index = 0; |
| 217 | if (!msg.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&interface_index)) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | uint32 metric = 0; |
| 222 | if (msg.HasAttribute(RTA_PRIORITY)) { |
| 223 | msg.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric); |
| 224 | } |
| 225 | |
| 226 | IPAddress default_addr(msg.family()); |
| 227 | default_addr.SetAddressToDefault(); |
| 228 | |
| 229 | ByteString dst_bytes(default_addr.address()); |
| 230 | if (msg.HasAttribute(RTA_DST)) { |
| 231 | dst_bytes = msg.GetAttribute(RTA_DST); |
| 232 | } |
| 233 | ByteString src_bytes(default_addr.address()); |
| 234 | if (msg.HasAttribute(RTA_SRC)) { |
| 235 | src_bytes = msg.GetAttribute(RTA_SRC); |
| 236 | } |
| 237 | ByteString gateway_bytes(default_addr.address()); |
| 238 | if (msg.HasAttribute(RTA_GATEWAY)) { |
| 239 | gateway_bytes = msg.GetAttribute(RTA_GATEWAY); |
| 240 | } |
| 241 | |
| 242 | RoutingTableEntry entry( |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 243 | IPAddress(msg.family(), dst_bytes, route_status.dst_prefix), |
| 244 | IPAddress(msg.family(), src_bytes, route_status.src_prefix), |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 245 | IPAddress(msg.family(), gateway_bytes), |
| 246 | metric, |
| 247 | route_status.scope, |
| 248 | true); |
| 249 | |
| 250 | vector<RoutingTableEntry> &table = tables_[interface_index]; |
| 251 | vector<RoutingTableEntry>::iterator nent; |
| 252 | for (nent = table.begin(); nent != table.end(); ++nent) { |
| 253 | if (nent->dst.Equals(entry.dst) && |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 254 | nent->src.Equals(entry.src) && |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 255 | nent->gateway.Equals(entry.gateway) && |
| 256 | nent->scope == entry.scope) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 257 | if (msg.mode() == RTNLMessage::kModeDelete) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 258 | table.erase(nent); |
| 259 | } else { |
| 260 | nent->from_rtnl = true; |
| 261 | nent->metric = entry.metric; |
| 262 | } |
| 263 | return; |
| 264 | } |
| 265 | } |
| 266 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 267 | if (msg.mode() == RTNLMessage::kModeAdd) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 268 | table.push_back(entry); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | bool RoutingTable::ApplyRoute(uint32 interface_index, |
| 273 | const RoutingTableEntry &entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 274 | RTNLMessage::Mode mode, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 275 | unsigned int flags) { |
| 276 | VLOG(2) << base::StringPrintf("%s: index %d mode %d flags 0x%x", |
| 277 | __func__, interface_index, mode, flags); |
| 278 | |
| 279 | RTNLMessage msg( |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 280 | RTNLMessage::kTypeRoute, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 281 | mode, |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 282 | NLM_F_REQUEST | flags, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 283 | 0, |
| 284 | 0, |
| 285 | 0, |
| 286 | entry.dst.family()); |
| 287 | |
| 288 | msg.set_route_status(RTNLMessage::RouteStatus( |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 289 | entry.dst.prefix(), |
| 290 | entry.src.prefix(), |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 291 | RT_TABLE_MAIN, |
| 292 | RTPROT_BOOT, |
| 293 | entry.scope, |
| 294 | RTN_UNICAST, |
| 295 | 0)); |
| 296 | |
| 297 | msg.SetAttribute(RTA_DST, entry.dst.address()); |
| 298 | if (!entry.src.IsDefault()) { |
| 299 | msg.SetAttribute(RTA_SRC, entry.src.address()); |
| 300 | } |
| 301 | if (!entry.gateway.IsDefault()) { |
| 302 | msg.SetAttribute(RTA_GATEWAY, entry.gateway.address()); |
| 303 | } |
| 304 | msg.SetAttribute(RTA_PRIORITY, ByteString::CreateFromCPUUInt32(entry.metric)); |
| 305 | msg.SetAttribute(RTA_OIF, ByteString::CreateFromCPUUInt32(interface_index)); |
| 306 | |
| 307 | return RTNLHandler::GetInstance()->SendMessage(&msg); |
| 308 | } |
| 309 | |
| 310 | bool RoutingTable::FlushCache() { |
| 311 | static const char *kPaths[2] = { kRouteFlushPath4, kRouteFlushPath6 }; |
| 312 | bool ret = true; |
| 313 | |
| 314 | VLOG(2) << __func__; |
| 315 | |
| 316 | for (size_t i = 0; i < arraysize(kPaths); ++i) { |
| 317 | if (file_util::WriteFile(FilePath(kPaths[i]), "-1", 2) != 2) { |
| 318 | LOG(ERROR) << base::StringPrintf("Cannot write to route flush file %s", |
| 319 | kPaths[i]); |
| 320 | ret = false; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | } // namespace shill |