Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -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 | |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 5 | #include "shill/device_info.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 6 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 7 | #include <arpa/inet.h> |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 8 | #include <fcntl.h> |
| 9 | #include <linux/if_tun.h> |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 10 | #include <linux/netlink.h> |
| 11 | #include <linux/rtnetlink.h> |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 12 | #include <net/if.h> |
| 13 | #include <net/if_arp.h> |
| 14 | #include <netinet/ether.h> |
| 15 | #include <string.h> |
| 16 | #include <sys/ioctl.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <time.h> |
| 19 | #include <unistd.h> |
| 20 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 23 | #include <base/bind.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 24 | #include <base/files/file_enumerator.h> |
Ben Chan | 11c213f | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 25 | #include <base/files/file_util.h> |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 26 | #include <base/files/scoped_file.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 27 | #include <base/stl_util.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 28 | #include <base/strings/string_number_conversions.h> |
| 29 | #include <base/strings/string_util.h> |
| 30 | #include <base/strings/stringprintf.h> |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 31 | |
| 32 | #include "shill/control_interface.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 33 | #include "shill/device.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 34 | #include "shill/device_stub.h" |
Ben Chan | 8760251 | 2014-11-07 20:50:05 -0800 | [diff] [blame] | 35 | #include "shill/ethernet/ethernet.h" |
| 36 | #include "shill/ethernet/virtio_ethernet.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 37 | #include "shill/logging.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 38 | #include "shill/manager.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 39 | #include "shill/net/ndisc.h" |
| 40 | #include "shill/net/rtnl_handler.h" |
| 41 | #include "shill/net/rtnl_listener.h" |
| 42 | #include "shill/net/rtnl_message.h" |
| 43 | #include "shill/net/shill_time.h" |
| 44 | #include "shill/net/sockets.h" |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 45 | #include "shill/routing_table.h" |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 46 | #include "shill/service.h" |
Ben Chan | 2240e8c | 2014-11-06 13:37:18 -0800 | [diff] [blame] | 47 | #include "shill/vpn/vpn_provider.h" |
Peter Qiu | 1a72f54 | 2015-04-14 16:31:36 -0700 | [diff] [blame] | 48 | |
| 49 | #if !defined(DISABLE_WIFI) |
| 50 | #include "shill/net/netlink_attribute.h" |
| 51 | #include "shill/net/netlink_manager.h" |
| 52 | #include "shill/net/nl80211_message.h" |
Ben Chan | d6a8b51 | 2014-11-18 10:45:15 -0800 | [diff] [blame] | 53 | #include "shill/wifi/wifi.h" |
Peter Qiu | 1a72f54 | 2015-04-14 16:31:36 -0700 | [diff] [blame] | 54 | #endif // DISABLE_WIFI |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 55 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 56 | using base::Bind; |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 57 | using base::FileEnumerator; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 58 | using base::FilePath; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 59 | using base::StringPrintf; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 60 | using base::Unretained; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 61 | using std::map; |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 62 | using std::set; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 63 | using std::string; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 64 | using std::vector; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 65 | |
| 66 | namespace shill { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 67 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 68 | namespace Logging { |
| 69 | static auto kModuleLogScope = ScopeLogger::kDevice; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 70 | static string ObjectID(const DeviceInfo* d) { return "(device_info)"; } |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 73 | // static |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 74 | const char DeviceInfo::kModemPseudoDeviceNamePrefix[] = "pseudomodem"; |
Christopher Wiley | e049cc5 | 2012-09-10 14:36:57 -0700 | [diff] [blame] | 75 | const char DeviceInfo::kEthernetPseudoDeviceNamePrefix[] = "pseudoethernet"; |
Paul Stewart | 83d6256 | 2015-02-09 19:57:47 -0800 | [diff] [blame] | 76 | const char DeviceInfo::kIgnoredDeviceNamePrefix[] = "veth"; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 77 | const char DeviceInfo::kDeviceInfoRoot[] = "/sys/class/net"; |
Ben Chan | 4e64d2d | 2012-05-16 00:02:25 -0700 | [diff] [blame] | 78 | const char DeviceInfo::kDriverCdcEther[] = "cdc_ether"; |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 79 | const char DeviceInfo::kDriverCdcNcm[] = "cdc_ncm"; |
Ben Chan | 4e64d2d | 2012-05-16 00:02:25 -0700 | [diff] [blame] | 80 | const char DeviceInfo::kDriverGdmWiMax[] = "gdm_wimax"; |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 81 | const char DeviceInfo::kDriverVirtioNet[] = "virtio_net"; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 82 | const char DeviceInfo::kInterfaceUevent[] = "uevent"; |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 83 | const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n"; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 84 | const char DeviceInfo::kInterfaceDevice[] = "device"; |
| 85 | const char DeviceInfo::kInterfaceDriver[] = "device/driver"; |
| 86 | const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags"; |
| 87 | const char DeviceInfo::kInterfaceType[] = "type"; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 88 | const char* DeviceInfo::kModemDrivers[] = { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 89 | "gobi", |
| 90 | "QCUSBNet2k", |
Ben Chan | 226d46a | 2012-10-11 00:22:17 -0700 | [diff] [blame] | 91 | "GobiNet", |
Ben Chan | 0f90e0b | 2013-06-26 23:37:16 -0700 | [diff] [blame] | 92 | "cdc_mbim", |
Ben Chan | 226d46a | 2012-10-11 00:22:17 -0700 | [diff] [blame] | 93 | "qmi_wwan" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 94 | }; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 95 | const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun"; |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 96 | const int DeviceInfo::kDelayedDeviceCreationSeconds = 5; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 97 | const int DeviceInfo::kRequestLinkStatisticsIntervalMilliseconds = 20000; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 98 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 99 | DeviceInfo::DeviceInfo(ControlInterface* control_interface, |
| 100 | EventDispatcher* dispatcher, |
| 101 | Metrics* metrics, |
| 102 | Manager* manager) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 103 | : control_interface_(control_interface), |
| 104 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 105 | metrics_(metrics), |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 106 | manager_(manager), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 107 | link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))), |
| 108 | address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))), |
Peter Qiu | 9855170 | 2014-07-28 13:28:53 -0700 | [diff] [blame] | 109 | rdnss_callback_(Bind(&DeviceInfo::RdnssMsgHandler, Unretained(this))), |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 110 | device_info_root_(kDeviceInfoRoot), |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 111 | routing_table_(RoutingTable::GetInstance()), |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 112 | rtnl_handler_(RTNLHandler::GetInstance()), |
Peter Qiu | 1a72f54 | 2015-04-14 16:31:36 -0700 | [diff] [blame] | 113 | #if !defined(DISABLE_WIFI) |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 114 | netlink_manager_(NetlinkManager::GetInstance()), |
Peter Qiu | 1a72f54 | 2015-04-14 16:31:36 -0700 | [diff] [blame] | 115 | #endif // DISABLE_WIFI |
Peter Qiu | 9855170 | 2014-07-28 13:28:53 -0700 | [diff] [blame] | 116 | sockets_(new Sockets()), |
| 117 | time_(Time::GetInstance()) { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 120 | DeviceInfo::~DeviceInfo() {} |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 121 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 122 | void DeviceInfo::AddDeviceToBlackList(const string& device_name) { |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 123 | black_list_.insert(device_name); |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 124 | // Remove the current device info if it exist, since it will be out-dated. |
| 125 | RemoveInfo(GetIndex(device_name)); |
| 126 | // Request link info update to allow device info to be recreated. |
Peter Qiu | ebd709e | 2015-03-16 14:03:37 -0700 | [diff] [blame] | 127 | if (manager_->running()) { |
| 128 | rtnl_handler_->RequestDump(RTNLHandler::kRequestLink); |
| 129 | } |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 132 | void DeviceInfo::RemoveDeviceFromBlackList(const string& device_name) { |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 133 | black_list_.erase(device_name); |
| 134 | // Remove the current device info if it exist, since it will be out-dated. |
| 135 | RemoveInfo(GetIndex(device_name)); |
| 136 | // Request link info update to allow device info to be recreated. |
Peter Qiu | ebd709e | 2015-03-16 14:03:37 -0700 | [diff] [blame] | 137 | if (manager_->running()) { |
| 138 | rtnl_handler_->RequestDump(RTNLHandler::kRequestLink); |
| 139 | } |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 142 | bool DeviceInfo::IsDeviceBlackListed(const string& device_name) { |
Eric Shienbrood | 5e628a5 | 2012-03-21 16:56:59 -0400 | [diff] [blame] | 143 | return ContainsKey(black_list_, device_name); |
| 144 | } |
| 145 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 146 | void DeviceInfo::Start() { |
| 147 | link_listener_.reset( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 148 | new RTNLListener(RTNLHandler::kRequestLink, link_callback_)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 149 | address_listener_.reset( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 150 | new RTNLListener(RTNLHandler::kRequestAddr, address_callback_)); |
Peter Qiu | 9855170 | 2014-07-28 13:28:53 -0700 | [diff] [blame] | 151 | rdnss_listener_.reset( |
| 152 | new RTNLListener(RTNLHandler::kRequestRdnss, rdnss_callback_)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 153 | rtnl_handler_->RequestDump(RTNLHandler::kRequestLink | |
| 154 | RTNLHandler::kRequestAddr); |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 155 | request_link_statistics_callback_.Reset( |
| 156 | Bind(&DeviceInfo::RequestLinkStatistics, AsWeakPtr())); |
| 157 | dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(), |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 158 | kRequestLinkStatisticsIntervalMilliseconds); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 161 | void DeviceInfo::Stop() { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 162 | link_listener_.reset(); |
| 163 | address_listener_.reset(); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 164 | infos_.clear(); |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 165 | request_link_statistics_callback_.Cancel(); |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 166 | delayed_devices_callback_.Cancel(); |
| 167 | delayed_devices_.clear(); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 170 | vector<string> DeviceInfo::GetUninitializedTechnologies() const { |
| 171 | set<string> unique_technologies; |
Arman Uguray | b00c13d | 2013-07-29 18:20:52 -0700 | [diff] [blame] | 172 | set<Technology::Identifier> initialized_technologies; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 173 | for (const auto& info : infos_) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 174 | Technology::Identifier technology = info.second.technology; |
| 175 | if (info.second.device) { |
Arman Uguray | b00c13d | 2013-07-29 18:20:52 -0700 | [diff] [blame] | 176 | // If there is more than one device for a technology and at least |
| 177 | // one of them has been initialized, make sure that it doesn't get |
| 178 | // listed as uninitialized. |
| 179 | initialized_technologies.insert(technology); |
| 180 | unique_technologies.erase(Technology::NameFromIdentifier(technology)); |
| 181 | continue; |
| 182 | } |
| 183 | if (Technology::IsPrimaryConnectivityTechnology(technology) && |
| 184 | !ContainsKey(initialized_technologies, technology)) |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 185 | unique_technologies.insert(Technology::NameFromIdentifier(technology)); |
| 186 | } |
| 187 | return vector<string>(unique_technologies.begin(), unique_technologies.end()); |
| 188 | } |
| 189 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 190 | void DeviceInfo::RegisterDevice(const DeviceRefPtr& device) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 191 | SLOG(this, 2) << __func__ << "(" << device->link_name() << ", " |
| 192 | << device->interface_index() << ")"; |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 193 | delayed_devices_.erase(device->interface_index()); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 194 | CHECK(!GetDevice(device->interface_index()).get()); |
| 195 | infos_[device->interface_index()].device = device; |
Thieu Le | 9abd674 | 2013-01-23 23:35:37 -0800 | [diff] [blame] | 196 | if (metrics_->IsDeviceRegistered(device->interface_index(), |
| 197 | device->technology())) { |
| 198 | metrics_->NotifyDeviceInitialized(device->interface_index()); |
| 199 | } else { |
| 200 | metrics_->RegisterDevice(device->interface_index(), device->technology()); |
| 201 | } |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 202 | if (Technology::IsPrimaryConnectivityTechnology(device->technology())) { |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 203 | manager_->RegisterDevice(device); |
| 204 | } |
| 205 | } |
| 206 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 207 | void DeviceInfo::DeregisterDevice(const DeviceRefPtr& device) { |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 208 | int interface_index = device->interface_index(); |
| 209 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 210 | SLOG(this, 2) << __func__ << "(" << device->link_name() << ", " |
| 211 | << interface_index << ")"; |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 212 | CHECK((device->technology() == Technology::kCellular) || |
| 213 | (device->technology() == Technology::kWiMax)); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 214 | |
| 215 | // Release reference to the device |
| 216 | map<int, Info>::iterator iter = infos_.find(interface_index); |
| 217 | if (iter != infos_.end()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 218 | SLOG(this, 2) << "Removing device from info for index: " |
| 219 | << interface_index; |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 220 | manager_->DeregisterDevice(device); |
| 221 | // Release the reference to the device, but maintain the mapping |
| 222 | // for the index. That will be cleaned up by an RTNL message. |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 223 | iter->second.device = nullptr; |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 224 | } |
Thieu Le | c8078a6 | 2013-01-22 18:01:12 -0800 | [diff] [blame] | 225 | metrics_->DeregisterDevice(device->interface_index()); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 226 | } |
| 227 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 228 | FilePath DeviceInfo::GetDeviceInfoPath(const string& iface_name, |
| 229 | const string& path_name) { |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 230 | return device_info_root_.Append(iface_name).Append(path_name); |
| 231 | } |
| 232 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 233 | bool DeviceInfo::GetDeviceInfoContents(const string& iface_name, |
| 234 | const string& path_name, |
| 235 | string* contents_out) { |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 236 | return base::ReadFileToString(GetDeviceInfoPath(iface_name, path_name), |
| 237 | contents_out); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 238 | } |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 239 | bool DeviceInfo::GetDeviceInfoSymbolicLink(const string& iface_name, |
| 240 | const string& path_name, |
| 241 | FilePath* path_out) { |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 242 | return base::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name), |
| 243 | path_out); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 246 | Technology::Identifier DeviceInfo::GetDeviceTechnology( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 247 | const string& iface_name) { |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 248 | string type_string; |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 249 | int arp_type = ARPHRD_VOID; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 250 | if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) && |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 251 | base::TrimString(type_string, "\n", &type_string) && |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 252 | !base::StringToInt(type_string, &arp_type)) { |
| 253 | arp_type = ARPHRD_VOID; |
| 254 | } |
| 255 | |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 256 | string contents; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 257 | if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) { |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 258 | LOG(INFO) << StringPrintf("%s: device %s has no uevent file", |
| 259 | __func__, iface_name.c_str()); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 260 | return Technology::kUnknown; |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 261 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 262 | |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 263 | // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the |
| 264 | // start of the file or after a newline, we can safely assume this |
| 265 | // is a wifi device. |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 266 | if (contents.find(kInterfaceUeventWifiSignature) != string::npos) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 267 | SLOG(this, 2) |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 268 | << StringPrintf("%s: device %s has wifi signature in uevent file", |
| 269 | __func__, iface_name.c_str()); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 270 | if (arp_type == ARPHRD_IEEE80211_RADIOTAP) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 271 | SLOG(this, 2) << StringPrintf("%s: wifi device %s is in monitor mode", |
| 272 | __func__, iface_name.c_str()); |
Paul Stewart | 2001a42 | 2011-12-15 10:20:09 -0800 | [diff] [blame] | 273 | return Technology::kWiFiMonitor; |
| 274 | } |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 275 | return Technology::kWifi; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Jason Glasgow | 7904d14 | 2012-05-18 13:36:20 -0400 | [diff] [blame] | 278 | // Special case for pseudo modems which are used for testing |
| 279 | if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 280 | SLOG(this, 2) << StringPrintf( |
Jason Glasgow | 7904d14 | 2012-05-18 13:36:20 -0400 | [diff] [blame] | 281 | "%s: device %s is a pseudo modem for testing", |
| 282 | __func__, iface_name.c_str()); |
| 283 | return Technology::kCellular; |
| 284 | } |
| 285 | |
Christopher Wiley | e049cc5 | 2012-09-10 14:36:57 -0700 | [diff] [blame] | 286 | // Special case for pseudo ethernet devices which are used for testing. |
| 287 | if (iface_name.find(kEthernetPseudoDeviceNamePrefix) == 0) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 288 | SLOG(this, 2) << StringPrintf( |
Christopher Wiley | e049cc5 | 2012-09-10 14:36:57 -0700 | [diff] [blame] | 289 | "%s: device %s is a virtual ethernet device for testing", |
| 290 | __func__, iface_name.c_str()); |
| 291 | return Technology::kEthernet; |
| 292 | } |
| 293 | |
Paul Stewart | 83d6256 | 2015-02-09 19:57:47 -0800 | [diff] [blame] | 294 | // Special case for devices which should be ignored. |
| 295 | if (iface_name.find(kIgnoredDeviceNamePrefix) == 0) { |
| 296 | SLOG(this, 2) << StringPrintf( |
| 297 | "%s: device %s should be ignored", __func__, iface_name.c_str()); |
| 298 | return Technology::kUnknown; |
| 299 | } |
| 300 | |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 301 | FilePath driver_path; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 302 | if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 303 | SLOG(this, 2) << StringPrintf("%s: device %s has no device symlink", |
| 304 | __func__, iface_name.c_str()); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 305 | if (arp_type == ARPHRD_LOOPBACK) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 306 | SLOG(this, 2) << StringPrintf("%s: device %s is a loopback device", |
| 307 | __func__, iface_name.c_str()); |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 308 | return Technology::kLoopback; |
| 309 | } |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 310 | if (arp_type == ARPHRD_PPP) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 311 | SLOG(this, 2) << StringPrintf("%s: device %s is a ppp device", |
| 312 | __func__, iface_name.c_str()); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 313 | return Technology::kPPP; |
| 314 | } |
| 315 | string tun_flags_str; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 316 | int tun_flags = 0; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 317 | if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) && |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 318 | base::TrimString(tun_flags_str, "\n", &tun_flags_str) && |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 319 | base::HexStringToInt(tun_flags_str, &tun_flags) && |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 320 | (tun_flags & IFF_TUN)) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 321 | SLOG(this, 2) << StringPrintf("%s: device %s is tun device", |
| 322 | __func__, iface_name.c_str()); |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 323 | return Technology::kTunnel; |
| 324 | } |
Paul Stewart | da8cbee | 2014-10-20 11:44:03 -0700 | [diff] [blame] | 325 | |
Paul Stewart | 83d6256 | 2015-02-09 19:57:47 -0800 | [diff] [blame] | 326 | // We don't know what sort of device it is. |
| 327 | if (manager_->ignore_unknown_ethernet()) { |
| 328 | SLOG(this, 2) << StringPrintf("%s: device %s, without driver name " |
| 329 | "will be ignored", |
| 330 | __func__, iface_name.c_str()); |
| 331 | return Technology::kUnknown; |
| 332 | } else { |
| 333 | // Act the same as if there was a driver symlink, but we did not |
| 334 | // recognize the driver name. |
| 335 | SLOG(this, 2) << StringPrintf("%s: device %s, without driver name " |
| 336 | "is defaulted to type ethernet", |
| 337 | __func__, iface_name.c_str()); |
| 338 | return Technology::kEthernet; |
| 339 | } |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | string driver_name(driver_path.BaseName().value()); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 343 | // See if driver for this interface is in a list of known modem driver names. |
| 344 | for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers); |
| 345 | ++modem_idx) { |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 346 | if (driver_name == kModemDrivers[modem_idx]) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 347 | SLOG(this, 2) |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 348 | << StringPrintf("%s: device %s is matched with modem driver %s", |
| 349 | __func__, iface_name.c_str(), driver_name.c_str()); |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 350 | return Technology::kCellular; |
| 351 | } |
| 352 | } |
| 353 | |
Ben Chan | 4e64d2d | 2012-05-16 00:02:25 -0700 | [diff] [blame] | 354 | if (driver_name == kDriverGdmWiMax) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 355 | SLOG(this, 2) << StringPrintf("%s: device %s is a WiMAX device", |
| 356 | __func__, iface_name.c_str()); |
Ben Chan | 4e64d2d | 2012-05-16 00:02:25 -0700 | [diff] [blame] | 357 | return Technology::kWiMax; |
| 358 | } |
| 359 | |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 360 | // For cdc_ether / cdc_ncm devices, make sure it's a modem because this driver |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 361 | // can be used for other ethernet devices. |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 362 | if (driver_name == kDriverCdcEther || driver_name == kDriverCdcNcm) { |
| 363 | if (IsCdcEthernetModemDevice(iface_name)) { |
| 364 | LOG(INFO) << StringPrintf("%s: device %s is a %s modem device", __func__, |
mukesh agrawal | 35ec840 | 2013-07-19 14:49:08 -0700 | [diff] [blame] | 365 | iface_name.c_str(), driver_name.c_str()); |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 366 | return Technology::kCellular; |
| 367 | } |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 368 | SLOG(this, 2) << StringPrintf("%s: device %s is a %s device", __func__, |
| 369 | iface_name.c_str(), driver_name.c_str()); |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 370 | return Technology::kCDCEthernet; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 371 | } |
| 372 | |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 373 | // Special case for the virtio driver, used when run under KVM. See also |
| 374 | // the comment in VirtioEthernet::Start. |
| 375 | if (driver_name == kDriverVirtioNet) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 376 | SLOG(this, 2) << StringPrintf("%s: device %s is virtio ethernet", |
| 377 | __func__, iface_name.c_str()); |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 378 | return Technology::kVirtioEthernet; |
| 379 | } |
| 380 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 381 | SLOG(this, 2) << StringPrintf("%s: device %s, with driver %s, " |
| 382 | "is defaulted to type ethernet", |
| 383 | __func__, iface_name.c_str(), |
| 384 | driver_name.c_str()); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 385 | return Technology::kEthernet; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 388 | bool DeviceInfo::IsCdcEthernetModemDevice(const std::string& iface_name) { |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 389 | // A cdc_ether / cdc_ncm device is a modem device if it also exposes tty |
| 390 | // interfaces. To determine this, we look for the existence of the tty |
| 391 | // interface in the USB device sysfs tree. |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 392 | // |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 393 | // A typical sysfs dir hierarchy for a cdc_ether / cdc_ncm modem USB device is |
| 394 | // as follows: |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 395 | // |
| 396 | // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2 |
| 397 | // 1-2:1.0 |
| 398 | // tty |
| 399 | // ttyACM0 |
| 400 | // 1-2:1.1 |
| 401 | // net |
| 402 | // usb0 |
| 403 | // 1-2:1.2 |
| 404 | // tty |
| 405 | // ttyACM1 |
| 406 | // ... |
| 407 | // |
| 408 | // /sys/class/net/usb0/device symlinks to |
| 409 | // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1 |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 410 | // |
| 411 | // Note that some modem devices have the tty directory one level deeper |
| 412 | // (eg. E362), so the device tree for the tty interface is: |
| 413 | // /sys/devices/pci0000:00/0000:00:1d.7/usb/1-2/1-2:1.0/ttyUSB0/tty/ttyUSB0 |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 414 | |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 415 | FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 416 | FilePath device_path; |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 417 | if (!base::ReadSymbolicLink(device_file, &device_path)) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 418 | SLOG(this, 2) << StringPrintf("%s: device %s has no device symlink", |
| 419 | __func__, iface_name.c_str()); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 420 | return false; |
| 421 | } |
| 422 | if (!device_path.IsAbsolute()) { |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 423 | device_path = |
| 424 | base::MakeAbsoluteFilePath(device_file.DirName().Append(device_path)); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | // Look for tty interface by enumerating all directories under the parent |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 428 | // USB device and see if there's a subdirectory "tty" inside. In other |
| 429 | // words, using the example dir hierarchy above, find |
| 430 | // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty. |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 431 | // If this exists, then this is a modem device. |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 432 | return HasSubdir(device_path.DirName(), FilePath("tty")); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | // static |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 436 | bool DeviceInfo::HasSubdir(const FilePath& base_dir, const FilePath& subdir) { |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 437 | FileEnumerator::FileType type = static_cast<FileEnumerator::FileType>( |
| 438 | FileEnumerator::DIRECTORIES | FileEnumerator::SHOW_SYM_LINKS); |
| 439 | FileEnumerator dir_enum(base_dir, true, type); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 440 | for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty(); |
| 441 | curr_dir = dir_enum.Next()) { |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 442 | if (curr_dir.BaseName() == subdir) |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 443 | return true; |
| 444 | } |
| 445 | return false; |
| 446 | } |
| 447 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 448 | DeviceRefPtr DeviceInfo::CreateDevice(const string& link_name, |
| 449 | const string& address, |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 450 | int interface_index, |
| 451 | Technology::Identifier technology) { |
| 452 | DeviceRefPtr device; |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 453 | delayed_devices_.erase(interface_index); |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 454 | infos_[interface_index].technology = technology; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 455 | |
| 456 | switch (technology) { |
| 457 | case Technology::kCellular: |
Ben Chan | 5742b24 | 2013-12-18 16:25:43 -0800 | [diff] [blame] | 458 | #if defined(DISABLE_CELLULAR) |
| 459 | LOG(WARNING) << "Cellular support is not implemented. " |
| 460 | << "Ignore cellular device " << link_name << " at index " |
| 461 | << interface_index << "."; |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 462 | return nullptr; |
Ben Chan | 5742b24 | 2013-12-18 16:25:43 -0800 | [diff] [blame] | 463 | #else |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 464 | // Cellular devices are managed by ModemInfo. |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 465 | SLOG(this, 2) << "Cellular link " << link_name |
| 466 | << " at index " << interface_index |
| 467 | << " -- notifying ModemInfo."; |
Gary Morain | bf74a67 | 2012-07-30 16:27:19 -0700 | [diff] [blame] | 468 | |
| 469 | // The MAC address provided by RTNL is not reliable for Gobi 2K modems. |
Ben Chan | 4b28586 | 2012-10-10 22:52:16 -0700 | [diff] [blame] | 470 | // Clear it here, and it will be fetched from the kernel in |
| 471 | // GetMACAddress(). |
Gary Morain | bf74a67 | 2012-07-30 16:27:19 -0700 | [diff] [blame] | 472 | infos_[interface_index].mac_address.Clear(); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 473 | manager_->modem_info()->OnDeviceInfoAvailable(link_name); |
| 474 | break; |
Ben Chan | 5742b24 | 2013-12-18 16:25:43 -0800 | [diff] [blame] | 475 | #endif // DISABLE_CELLULAR |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 476 | case Technology::kEthernet: |
| 477 | device = new Ethernet(control_interface_, dispatcher_, metrics_, |
| 478 | manager_, link_name, address, interface_index); |
| 479 | device->EnableIPv6Privacy(); |
| 480 | break; |
| 481 | case Technology::kVirtioEthernet: |
| 482 | device = new VirtioEthernet(control_interface_, dispatcher_, metrics_, |
| 483 | manager_, link_name, address, |
| 484 | interface_index); |
| 485 | device->EnableIPv6Privacy(); |
| 486 | break; |
| 487 | case Technology::kWifi: |
Peter Qiu | 1a72f54 | 2015-04-14 16:31:36 -0700 | [diff] [blame] | 488 | #if defined(DISABLE_WIFI) |
| 489 | LOG(WARNING) << "WiFi support is not implemented. Ignore WiFi link " |
| 490 | << link_name << " at index " << interface_index << "."; |
| 491 | return nullptr; |
| 492 | #else |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 493 | // Defer creating this device until we get information about the |
| 494 | // type of WiFi interface. |
| 495 | GetWiFiInterfaceInfo(interface_index); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 496 | break; |
Peter Qiu | 1a72f54 | 2015-04-14 16:31:36 -0700 | [diff] [blame] | 497 | #endif // DISABLE_WIFI |
Ben Chan | 4e64d2d | 2012-05-16 00:02:25 -0700 | [diff] [blame] | 498 | case Technology::kWiMax: |
Ben Chan | 520eb17 | 2013-10-30 20:51:04 -0700 | [diff] [blame] | 499 | #if defined(DISABLE_WIMAX) |
| 500 | LOG(WARNING) << "WiMax support is not implemented. Ignore WiMax link " |
| 501 | << link_name << " at index " << interface_index << "."; |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 502 | return nullptr; |
Ben Chan | 520eb17 | 2013-10-30 20:51:04 -0700 | [diff] [blame] | 503 | #else |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 504 | // WiMax devices are managed by WiMaxProvider. |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 505 | SLOG(this, 2) << "WiMax link " << link_name |
| 506 | << " at index " << interface_index |
| 507 | << " -- notifying WiMaxProvider."; |
Ben Chan | 4b28586 | 2012-10-10 22:52:16 -0700 | [diff] [blame] | 508 | // The MAC address provided by RTNL may not be the final value as the |
| 509 | // WiMAX device may change the address after initialization. Clear it |
| 510 | // here, and it will be fetched from the kernel when |
| 511 | // WiMaxProvider::CreateDevice() is called after the WiMAX device DBus |
| 512 | // object is created by the WiMAX manager daemon. |
| 513 | infos_[interface_index].mac_address.Clear(); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 514 | manager_->wimax_provider()->OnDeviceInfoAvailable(link_name); |
Ben Chan | 4e64d2d | 2012-05-16 00:02:25 -0700 | [diff] [blame] | 515 | break; |
Ben Chan | 520eb17 | 2013-10-30 20:51:04 -0700 | [diff] [blame] | 516 | #endif // DISABLE_WIMAX |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 517 | case Technology::kPPP: |
| 518 | case Technology::kTunnel: |
| 519 | // Tunnel and PPP devices are managed by the VPN code (PPP for |
| 520 | // l2tpipsec). Notify the VPN Provider of the interface's presence. |
| 521 | // Since CreateDevice is only called once in the lifetime of an |
| 522 | // interface index, this notification will only occur the first |
| 523 | // time the device is seen. |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 524 | SLOG(this, 2) << "Tunnel / PPP link " << link_name |
| 525 | << " at index " << interface_index |
| 526 | << " -- notifying VPNProvider."; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 527 | if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name, |
| 528 | interface_index) && |
| 529 | technology == Technology::kTunnel) { |
| 530 | // If VPN does not know anything about this tunnel, it is probably |
| 531 | // left over from a previous instance and should not exist. |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 532 | SLOG(this, 2) << "Tunnel link is unused. Deleting."; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 533 | DeleteInterface(interface_index); |
| 534 | } |
| 535 | break; |
| 536 | case Technology::kLoopback: |
| 537 | // Loopback devices are largely ignored, but we should make sure the |
| 538 | // link is enabled. |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 539 | SLOG(this, 2) << "Bringing up loopback device " << link_name |
| 540 | << " at index " << interface_index; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 541 | rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 542 | return nullptr; |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 543 | case Technology::kCDCEthernet: |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 544 | // CDCEthernet devices are of indeterminate type when they are |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 545 | // initially created. Some time later, tty devices may or may |
| 546 | // not appear under the same USB device root, which will identify |
| 547 | // it as a modem. Alternatively, ModemManager may discover the |
| 548 | // device and create and register a Cellular device. In either |
| 549 | // case, we should delay creating a Device until we can make a |
| 550 | // better determination of what type this Device should be. |
| 551 | LOG(INFO) << "Delaying creation of device for " << link_name |
| 552 | << " at index " << interface_index; |
| 553 | DelayDeviceCreation(interface_index); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 554 | return nullptr; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 555 | default: |
| 556 | // We will not manage this device in shill. Do not create a device |
| 557 | // object or do anything to change its state. We create a stub object |
| 558 | // which is useful for testing. |
| 559 | return new DeviceStub(control_interface_, dispatcher_, metrics_, |
| 560 | manager_, link_name, address, interface_index, |
Ben Chan | 4e64d2d | 2012-05-16 00:02:25 -0700 | [diff] [blame] | 561 | technology); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | // Reset the routing table and addresses. |
| 565 | routing_table_->FlushRoutes(interface_index); |
| 566 | FlushAddresses(interface_index); |
| 567 | |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 568 | manager_->UpdateUninitializedTechnologies(); |
| 569 | |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 570 | return device; |
| 571 | } |
| 572 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 573 | void DeviceInfo::AddLinkMsgHandler(const RTNLMessage& msg) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 574 | DCHECK(msg.type() == RTNLMessage::kTypeLink && |
| 575 | msg.mode() == RTNLMessage::kModeAdd); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 576 | int dev_index = msg.interface_index(); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 577 | Technology::Identifier technology = Technology::kUnknown; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 578 | unsigned int flags = msg.link_status().flags; |
| 579 | unsigned int change = msg.link_status().change; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 580 | bool new_device = |
| 581 | !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only; |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 582 | SLOG(this, 2) << __func__ << "(index=" << dev_index |
| 583 | << std::showbase << std::hex |
| 584 | << ", flags=" << flags << ", change=" << change << ")" |
| 585 | << std::dec << std::noshowbase |
| 586 | << ", new_device=" << new_device; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 587 | infos_[dev_index].has_addresses_only = false; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 588 | infos_[dev_index].flags = flags; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 589 | |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 590 | RetrieveLinkStatistics(dev_index, msg); |
| 591 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 592 | DeviceRefPtr device = GetDevice(dev_index); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 593 | if (new_device) { |
| 594 | CHECK(!device); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 595 | if (!msg.HasAttribute(IFLA_IFNAME)) { |
| 596 | LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!"; |
| 597 | return; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 598 | } |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 599 | ByteString b(msg.GetAttribute(IFLA_IFNAME)); |
| 600 | string link_name(reinterpret_cast<const char*>(b.GetConstData())); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 601 | SLOG(this, 2) << "add link index " << dev_index << " name " << link_name; |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 602 | infos_[dev_index].name = link_name; |
| 603 | indices_[link_name] = dev_index; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 604 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 605 | if (!link_name.empty()) { |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 606 | if (ContainsKey(black_list_, link_name)) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 607 | technology = Technology::kBlacklisted; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 608 | } else { |
| 609 | technology = GetDeviceTechnology(link_name); |
| 610 | } |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 611 | } |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 612 | string address; |
| 613 | if (msg.HasAttribute(IFLA_ADDRESS)) { |
| 614 | infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS); |
Ben Chan | 0353c22 | 2014-09-05 08:20:59 -0700 | [diff] [blame] | 615 | address = |
| 616 | base::StringToLowerASCII(infos_[dev_index].mac_address.HexEncode()); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 617 | SLOG(this, 2) << "link index " << dev_index << " address " |
| 618 | << infos_[dev_index].mac_address.HexEncode(); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 619 | } else if (technology != Technology::kTunnel && |
| 620 | technology != Technology::kPPP) { |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 621 | LOG(ERROR) << "Add Link message for link '" << link_name |
| 622 | << "' does not have IFLA_ADDRESS!"; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 623 | return; |
| 624 | } |
Thieu Le | c8078a6 | 2013-01-22 18:01:12 -0800 | [diff] [blame] | 625 | metrics_->RegisterDevice(dev_index, technology); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 626 | device = CreateDevice(link_name, address, dev_index, technology); |
| 627 | if (device) { |
| 628 | RegisterDevice(device); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 629 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 630 | } |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 631 | if (device) { |
| 632 | device->LinkEvent(flags, change); |
| 633 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 636 | void DeviceInfo::DelLinkMsgHandler(const RTNLMessage& msg) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 637 | SLOG(this, 2) << __func__ << "(index=" << msg.interface_index() << ")"; |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 638 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 639 | DCHECK(msg.type() == RTNLMessage::kTypeLink && |
| 640 | msg.mode() == RTNLMessage::kModeDelete); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 641 | SLOG(this, 2) << __func__ << "(index=" << msg.interface_index() |
| 642 | << std::showbase << std::hex |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 643 | << ", flags=" << msg.link_status().flags |
| 644 | << ", change=" << msg.link_status().change << ")"; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 645 | RemoveInfo(msg.interface_index()); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 646 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 647 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 648 | DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const { |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 649 | const Info* info = GetInfo(interface_index); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 650 | return info ? info->device : nullptr; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 653 | int DeviceInfo::GetIndex(const string& interface_name) const { |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 654 | map<string, int>::const_iterator it = indices_.find(interface_name); |
| 655 | return it == indices_.end() ? -1 : it->second; |
| 656 | } |
| 657 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 658 | bool DeviceInfo::GetMACAddress(int interface_index, ByteString* address) const { |
| 659 | const Info* info = GetInfo(interface_index); |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 660 | if (!info) { |
| 661 | return false; |
| 662 | } |
Gary Morain | bf74a67 | 2012-07-30 16:27:19 -0700 | [diff] [blame] | 663 | // |mac_address| from RTNL is not used for some devices, in which case it will |
| 664 | // be empty here. |
| 665 | if (!info->mac_address.IsEmpty()) { |
| 666 | *address = info->mac_address; |
| 667 | return true; |
| 668 | } |
| 669 | |
| 670 | // Ask the kernel for the MAC address. |
| 671 | *address = GetMACAddressFromKernel(interface_index); |
| 672 | return !address->IsEmpty(); |
| 673 | } |
| 674 | |
| 675 | ByteString DeviceInfo::GetMACAddressFromKernel(int interface_index) const { |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 676 | const Info* info = GetInfo(interface_index); |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 677 | if (!info) { |
| 678 | return ByteString(); |
| 679 | } |
| 680 | |
| 681 | const int fd = sockets_->Socket(PF_INET, SOCK_DGRAM, 0); |
Gary Morain | bf74a67 | 2012-07-30 16:27:19 -0700 | [diff] [blame] | 682 | if (fd < 0) { |
Ningyuan Wang | fc93327 | 2015-08-03 14:47:14 -0700 | [diff] [blame] | 683 | PLOG(ERROR) << __func__ << ": Unable to open socket"; |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 684 | return ByteString(); |
Gary Morain | bf74a67 | 2012-07-30 16:27:19 -0700 | [diff] [blame] | 685 | } |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 686 | |
| 687 | ScopedSocketCloser socket_closer(sockets_.get(), fd); |
Gary Morain | bf74a67 | 2012-07-30 16:27:19 -0700 | [diff] [blame] | 688 | struct ifreq ifr; |
| 689 | memset(&ifr, 0, sizeof(ifr)); |
| 690 | ifr.ifr_ifindex = interface_index; |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 691 | strcpy(ifr.ifr_ifrn.ifrn_name, info->name.c_str()); // NOLINT(runtime/printf) |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 692 | int err = sockets_->Ioctl(fd, SIOCGIFHWADDR, &ifr); |
Gary Morain | bf74a67 | 2012-07-30 16:27:19 -0700 | [diff] [blame] | 693 | if (err < 0) { |
Ningyuan Wang | fc93327 | 2015-08-03 14:47:14 -0700 | [diff] [blame] | 694 | PLOG(ERROR) << __func__ << ": Unable to read MAC address"; |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 695 | return ByteString(); |
Gary Morain | bf74a67 | 2012-07-30 16:27:19 -0700 | [diff] [blame] | 696 | } |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 697 | |
| 698 | return ByteString(ifr.ifr_hwaddr.sa_data, IFHWADDRLEN); |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Paul Stewart | 6950ba5 | 2013-12-05 08:28:14 -0800 | [diff] [blame] | 701 | bool DeviceInfo::GetMACAddressOfPeer(int interface_index, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 702 | const IPAddress& peer, |
| 703 | ByteString* mac_address) const { |
| 704 | const Info* info = GetInfo(interface_index); |
Paul Stewart | 6950ba5 | 2013-12-05 08:28:14 -0800 | [diff] [blame] | 705 | if (!info || !peer.IsValid()) { |
| 706 | return false; |
| 707 | } |
| 708 | |
| 709 | if (peer.family() != IPAddress::kFamilyIPv4) { |
| 710 | NOTIMPLEMENTED() << ": only implemented for IPv4"; |
| 711 | return false; |
| 712 | } |
| 713 | |
| 714 | const int fd = sockets_->Socket(PF_INET, SOCK_DGRAM, 0); |
| 715 | if (fd < 0) { |
Ningyuan Wang | fc93327 | 2015-08-03 14:47:14 -0700 | [diff] [blame] | 716 | PLOG(ERROR) << __func__ << ": Unable to open socket"; |
Paul Stewart | 6950ba5 | 2013-12-05 08:28:14 -0800 | [diff] [blame] | 717 | return false; |
| 718 | } |
| 719 | |
| 720 | ScopedSocketCloser socket_closer(sockets_.get(), fd); |
| 721 | struct arpreq areq; |
| 722 | memset(&areq, 0, sizeof(areq)); |
| 723 | |
| 724 | strncpy(areq.arp_dev, info->name.c_str(), sizeof(areq.arp_dev) - 1); |
| 725 | areq.arp_dev[sizeof(areq.arp_dev) - 1] = '\0'; |
| 726 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 727 | struct sockaddr_in* protocol_address = |
| 728 | reinterpret_cast<struct sockaddr_in*>(&areq.arp_pa); |
Paul Stewart | 6950ba5 | 2013-12-05 08:28:14 -0800 | [diff] [blame] | 729 | protocol_address->sin_family = AF_INET; |
| 730 | CHECK_EQ(sizeof(protocol_address->sin_addr.s_addr), peer.GetLength()); |
| 731 | memcpy(&protocol_address->sin_addr.s_addr, peer.address().GetConstData(), |
| 732 | sizeof(protocol_address->sin_addr.s_addr)); |
| 733 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 734 | struct sockaddr_in* hardware_address = |
| 735 | reinterpret_cast<struct sockaddr_in*>(&areq.arp_ha); |
Paul Stewart | 6950ba5 | 2013-12-05 08:28:14 -0800 | [diff] [blame] | 736 | hardware_address->sin_family = ARPHRD_ETHER; |
| 737 | |
| 738 | int err = sockets_->Ioctl(fd, SIOCGARP, &areq); |
| 739 | if (err < 0) { |
Ningyuan Wang | fc93327 | 2015-08-03 14:47:14 -0700 | [diff] [blame] | 740 | PLOG(ERROR) << __func__ << ": Unable to perform ARP lookup"; |
Paul Stewart | 6950ba5 | 2013-12-05 08:28:14 -0800 | [diff] [blame] | 741 | return false; |
| 742 | } |
| 743 | |
| 744 | ByteString peer_address(areq.arp_ha.sa_data, IFHWADDRLEN); |
| 745 | |
| 746 | if (peer_address.IsZero()) { |
| 747 | LOG(INFO) << __func__ << ": ARP lookup is still in progress"; |
| 748 | return false; |
| 749 | } |
| 750 | |
| 751 | CHECK(mac_address); |
| 752 | *mac_address = peer_address; |
| 753 | return true; |
| 754 | } |
| 755 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 756 | bool DeviceInfo::GetAddresses(int interface_index, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 757 | vector<AddressData>* addresses) const { |
| 758 | const Info* info = GetInfo(interface_index); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 759 | if (!info) { |
| 760 | return false; |
| 761 | } |
| 762 | *addresses = info->ip_addresses; |
| 763 | return true; |
| 764 | } |
| 765 | |
| 766 | void DeviceInfo::FlushAddresses(int interface_index) const { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 767 | SLOG(this, 2) << __func__ << "(" << interface_index << ")"; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 768 | const Info* info = GetInfo(interface_index); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 769 | if (!info) { |
| 770 | return; |
| 771 | } |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 772 | for (const auto& address_info : info->ip_addresses) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 773 | if (address_info.address.family() == IPAddress::kFamilyIPv4 || |
| 774 | (address_info.scope == RT_SCOPE_UNIVERSE && |
| 775 | (address_info.flags & ~IFA_F_TEMPORARY) == 0)) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 776 | SLOG(this, 2) << __func__ << ": removing ip address " |
| 777 | << address_info.address.ToString() |
| 778 | << " from interface " << interface_index; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 779 | rtnl_handler_->RemoveInterfaceAddress(interface_index, |
| 780 | address_info.address); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 785 | bool DeviceInfo::HasOtherAddress( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 786 | int interface_index, const IPAddress& this_address) const { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 787 | SLOG(this, 3) << __func__ << "(" << interface_index << ")"; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 788 | const Info* info = GetInfo(interface_index); |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 789 | if (!info) { |
| 790 | return false; |
| 791 | } |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 792 | bool has_other_address = false; |
| 793 | bool has_this_address = false; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 794 | for (const auto& local_address : info->ip_addresses) { |
Paul Stewart | a0db0ff | 2013-12-09 09:48:47 -0800 | [diff] [blame] | 795 | if (local_address.address.family() != this_address.family()) { |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 796 | continue; |
| 797 | } |
Paul Stewart | a0db0ff | 2013-12-09 09:48:47 -0800 | [diff] [blame] | 798 | if (local_address.address.address().Equals(this_address.address())) { |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 799 | has_this_address = true; |
| 800 | } else if (this_address.family() == IPAddress::kFamilyIPv4) { |
| 801 | has_other_address = true; |
Paul Stewart | a0db0ff | 2013-12-09 09:48:47 -0800 | [diff] [blame] | 802 | } else if ((local_address.scope == RT_SCOPE_UNIVERSE && |
| 803 | (local_address.flags & IFA_F_TEMPORARY) == 0)) { |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 804 | has_other_address = true; |
| 805 | } |
| 806 | } |
| 807 | return has_other_address && !has_this_address; |
| 808 | } |
| 809 | |
Paul Stewart | d55f6ae | 2014-04-24 16:34:21 -0700 | [diff] [blame] | 810 | bool DeviceInfo::GetPrimaryIPv6Address(int interface_index, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 811 | IPAddress* address) { |
| 812 | const Info* info = GetInfo(interface_index); |
Paul Stewart | d55f6ae | 2014-04-24 16:34:21 -0700 | [diff] [blame] | 813 | if (!info) { |
| 814 | return false; |
| 815 | } |
| 816 | bool has_temporary_address = false; |
| 817 | bool has_address = false; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 818 | for (const auto& local_address : info->ip_addresses) { |
Paul Stewart | d55f6ae | 2014-04-24 16:34:21 -0700 | [diff] [blame] | 819 | if (local_address.address.family() != IPAddress::kFamilyIPv6 || |
| 820 | local_address.scope != RT_SCOPE_UNIVERSE) { |
| 821 | continue; |
| 822 | } |
| 823 | |
| 824 | // Prefer temporary addresses to non-temporary addresses to match the |
| 825 | // kernel's preference. |
| 826 | bool is_temporary_address = ((local_address.flags & IFA_F_TEMPORARY) != 0); |
| 827 | if (has_temporary_address && !is_temporary_address) { |
| 828 | continue; |
| 829 | } |
| 830 | |
| 831 | *address = local_address.address; |
| 832 | has_temporary_address = is_temporary_address; |
| 833 | has_address = true; |
| 834 | } |
| 835 | |
| 836 | return has_address; |
| 837 | } |
| 838 | |
Peter Qiu | 9855170 | 2014-07-28 13:28:53 -0700 | [diff] [blame] | 839 | bool DeviceInfo::GetIPv6DnsServerAddresses(int interface_index, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 840 | std::vector<IPAddress>* address_list, |
| 841 | uint32_t* life_time) { |
| 842 | const Info* info = GetInfo(interface_index); |
Peter Qiu | 9855170 | 2014-07-28 13:28:53 -0700 | [diff] [blame] | 843 | if (!info || info->ipv6_dns_server_addresses.empty()) { |
| 844 | return false; |
| 845 | } |
| 846 | |
| 847 | // Determine the remaining DNS server life time. |
| 848 | if (info->ipv6_dns_server_lifetime_seconds == ND_OPT_LIFETIME_INFINITY) { |
| 849 | *life_time = ND_OPT_LIFETIME_INFINITY; |
| 850 | } else { |
| 851 | time_t cur_time; |
| 852 | if (!time_->GetSecondsBoottime(&cur_time)) { |
| 853 | NOTREACHED(); |
| 854 | } |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 855 | uint32_t time_elapsed = static_cast<uint32_t>( |
Peter Qiu | 9855170 | 2014-07-28 13:28:53 -0700 | [diff] [blame] | 856 | cur_time - info->ipv6_dns_server_received_time_seconds); |
| 857 | if (time_elapsed >= info->ipv6_dns_server_lifetime_seconds) { |
| 858 | *life_time = 0; |
| 859 | } else { |
| 860 | *life_time = info->ipv6_dns_server_lifetime_seconds - time_elapsed; |
| 861 | } |
| 862 | } |
| 863 | *address_list = info->ipv6_dns_server_addresses; |
| 864 | return true; |
| 865 | } |
| 866 | |
Paul Stewart | a0db0ff | 2013-12-09 09:48:47 -0800 | [diff] [blame] | 867 | bool DeviceInfo::HasDirectConnectivityTo( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 868 | int interface_index, const IPAddress& address) const { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 869 | SLOG(this, 3) << __func__ << "(" << interface_index << ")"; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 870 | const Info* info = GetInfo(interface_index); |
Paul Stewart | a0db0ff | 2013-12-09 09:48:47 -0800 | [diff] [blame] | 871 | if (!info) { |
| 872 | return false; |
| 873 | } |
| 874 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 875 | for (const auto& local_address : info->ip_addresses) { |
Paul Stewart | a0db0ff | 2013-12-09 09:48:47 -0800 | [diff] [blame] | 876 | if (local_address.address.family() == address.family() && |
| 877 | local_address.address.CanReachAddress(address)) { |
| 878 | return true; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | return false; |
| 883 | } |
| 884 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 885 | bool DeviceInfo::GetFlags(int interface_index, unsigned int* flags) const { |
| 886 | const Info* info = GetInfo(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 887 | if (!info) { |
| 888 | return false; |
| 889 | } |
| 890 | *flags = info->flags; |
| 891 | return true; |
| 892 | } |
| 893 | |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 894 | bool DeviceInfo::GetByteCounts(int interface_index, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 895 | uint64_t* rx_bytes, |
| 896 | uint64_t* tx_bytes) const { |
| 897 | const Info* info = GetInfo(interface_index); |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 898 | if (!info) { |
| 899 | return false; |
| 900 | } |
| 901 | *rx_bytes = info->rx_bytes; |
| 902 | *tx_bytes = info->tx_bytes; |
| 903 | return true; |
| 904 | } |
| 905 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 906 | bool DeviceInfo::CreateTunnelInterface(string* interface_name) const { |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 907 | int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR)); |
| 908 | if (fd < 0) { |
| 909 | PLOG(ERROR) << "failed to open " << kTunDeviceName; |
| 910 | return false; |
| 911 | } |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 912 | base::ScopedFD scoped_fd(fd); |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 913 | |
| 914 | struct ifreq ifr; |
| 915 | memset(&ifr, 0, sizeof(ifr)); |
| 916 | ifr.ifr_flags = IFF_TUN | IFF_NO_PI; |
| 917 | if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) { |
| 918 | PLOG(ERROR) << "failed to create tunnel interface"; |
| 919 | return false; |
| 920 | } |
| 921 | |
| 922 | if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) { |
| 923 | PLOG(ERROR) << "failed to set tunnel interface to be persistent"; |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | *interface_name = string(ifr.ifr_name); |
| 928 | |
| 929 | return true; |
| 930 | } |
| 931 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 932 | int DeviceInfo::OpenTunnelInterface(const std::string& interface_name) const { |
Prabhu Kaliamoorthi | 127b341 | 2014-10-16 13:00:25 +0200 | [diff] [blame] | 933 | int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR)); |
| 934 | if (fd < 0) { |
| 935 | PLOG(ERROR) << "failed to open " << kTunDeviceName; |
| 936 | return -1; |
| 937 | } |
| 938 | |
| 939 | struct ifreq ifr; |
| 940 | memset(&ifr, 0, sizeof(ifr)); |
| 941 | strncpy(ifr.ifr_name, interface_name.c_str(), sizeof(ifr.ifr_name)); |
| 942 | ifr.ifr_flags = IFF_TUN | IFF_NO_PI; |
| 943 | if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) { |
| 944 | PLOG(ERROR) << "failed to set tunnel interface name"; |
| 945 | return -1; |
| 946 | } |
| 947 | |
| 948 | return fd; |
| 949 | } |
| 950 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 951 | bool DeviceInfo::DeleteInterface(int interface_index) const { |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 952 | return rtnl_handler_->RemoveInterface(interface_index); |
| 953 | } |
| 954 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 955 | const DeviceInfo::Info* DeviceInfo::GetInfo(int interface_index) const { |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 956 | map<int, Info>::const_iterator iter = infos_.find(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 957 | if (iter == infos_.end()) { |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 958 | return nullptr; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 959 | } |
| 960 | return &iter->second; |
| 961 | } |
| 962 | |
| 963 | void DeviceInfo::RemoveInfo(int interface_index) { |
| 964 | map<int, Info>::iterator iter = infos_.find(interface_index); |
| 965 | if (iter != infos_.end()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 966 | SLOG(this, 2) << "Removing info for device index: " << interface_index; |
Peter Qiu | 9f5159e | 2014-09-12 16:50:14 -0700 | [diff] [blame] | 967 | // Deregister the device if not deregistered yet. Cellular and WiMax devices |
| 968 | // are deregistered through a call to DeviceInfo::DeregisterDevice. |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 969 | if (iter->second.device.get()) { |
| 970 | manager_->DeregisterDevice(iter->second.device); |
Peter Qiu | 9f5159e | 2014-09-12 16:50:14 -0700 | [diff] [blame] | 971 | metrics_->DeregisterDevice(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 972 | } |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 973 | indices_.erase(iter->second.name); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 974 | infos_.erase(iter); |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 975 | delayed_devices_.erase(interface_index); |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 976 | } else { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 977 | SLOG(this, 2) << __func__ << ": Unknown device index: " |
| 978 | << interface_index; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 979 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 982 | void DeviceInfo::LinkMsgHandler(const RTNLMessage& msg) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 983 | DCHECK(msg.type() == RTNLMessage::kTypeLink); |
| 984 | if (msg.mode() == RTNLMessage::kModeAdd) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 985 | AddLinkMsgHandler(msg); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 986 | } else if (msg.mode() == RTNLMessage::kModeDelete) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 987 | DelLinkMsgHandler(msg); |
| 988 | } else { |
| 989 | NOTREACHED(); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 990 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 993 | void DeviceInfo::AddressMsgHandler(const RTNLMessage& msg) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 994 | SLOG(this, 2) << __func__; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 995 | DCHECK(msg.type() == RTNLMessage::kTypeAddress); |
| 996 | int interface_index = msg.interface_index(); |
| 997 | if (!ContainsKey(infos_, interface_index)) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 998 | SLOG(this, 2) << "Got advance address information for unknown index " |
| 999 | << interface_index; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 1000 | infos_[interface_index].has_addresses_only = true; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 1001 | } |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1002 | const RTNLMessage::AddressStatus& status = msg.address_status(); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 1003 | IPAddress address(msg.family(), |
Ben Chan | 682c5d4 | 2012-06-18 14:11:04 -0700 | [diff] [blame] | 1004 | msg.HasAttribute(IFA_LOCAL) ? |
| 1005 | msg.GetAttribute(IFA_LOCAL) : msg.GetAttribute(IFA_ADDRESS), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 1006 | status.prefix_len); |
| 1007 | |
Ben Chan | 682c5d4 | 2012-06-18 14:11:04 -0700 | [diff] [blame] | 1008 | SLOG_IF(Device, 2, msg.HasAttribute(IFA_LOCAL)) |
| 1009 | << "Found local address attribute for interface " << interface_index; |
| 1010 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1011 | vector<AddressData>& address_list = infos_[interface_index].ip_addresses; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 1012 | vector<AddressData>::iterator iter; |
| 1013 | for (iter = address_list.begin(); iter != address_list.end(); ++iter) { |
| 1014 | if (address.Equals(iter->address)) { |
| 1015 | break; |
| 1016 | } |
| 1017 | } |
| 1018 | if (iter != address_list.end()) { |
| 1019 | if (msg.mode() == RTNLMessage::kModeDelete) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1020 | SLOG(this, 2) << "Delete address for interface " << interface_index; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 1021 | address_list.erase(iter); |
| 1022 | } else { |
| 1023 | iter->flags = status.flags; |
| 1024 | iter->scope = status.scope; |
| 1025 | } |
| 1026 | } else if (msg.mode() == RTNLMessage::kModeAdd) { |
| 1027 | address_list.push_back(AddressData(address, status.flags, status.scope)); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1028 | SLOG(this, 2) << "Add address " << address.ToString() |
| 1029 | << " for interface " << interface_index; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 1030 | } |
Paul Stewart | d55f6ae | 2014-04-24 16:34:21 -0700 | [diff] [blame] | 1031 | |
| 1032 | DeviceRefPtr device = GetDevice(interface_index); |
| 1033 | if (device && address.family() == IPAddress::kFamilyIPv6 && |
| 1034 | status.scope == RT_SCOPE_UNIVERSE) { |
| 1035 | device->OnIPv6AddressChanged(); |
| 1036 | } |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1039 | void DeviceInfo::RdnssMsgHandler(const RTNLMessage& msg) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1040 | SLOG(this, 2) << __func__; |
Peter Qiu | 9855170 | 2014-07-28 13:28:53 -0700 | [diff] [blame] | 1041 | DCHECK(msg.type() == RTNLMessage::kTypeRdnss); |
| 1042 | int interface_index = msg.interface_index(); |
| 1043 | if (!ContainsKey(infos_, interface_index)) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1044 | SLOG(this, 2) << "Got RDNSS option for unknown index " |
| 1045 | << interface_index; |
Peter Qiu | 9855170 | 2014-07-28 13:28:53 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1048 | const RTNLMessage::RdnssOption& rdnss_option = msg.rdnss_option(); |
Peter Qiu | 9855170 | 2014-07-28 13:28:53 -0700 | [diff] [blame] | 1049 | infos_[interface_index].ipv6_dns_server_lifetime_seconds = |
| 1050 | rdnss_option.lifetime; |
| 1051 | infos_[interface_index].ipv6_dns_server_addresses = rdnss_option.addresses; |
| 1052 | if (!time_->GetSecondsBoottime( |
| 1053 | &infos_[interface_index].ipv6_dns_server_received_time_seconds)) { |
| 1054 | NOTREACHED(); |
| 1055 | } |
| 1056 | |
| 1057 | // Notify device of the IPv6 DNS server addresses update. |
| 1058 | DeviceRefPtr device = GetDevice(interface_index); |
| 1059 | if (device) { |
| 1060 | device->OnIPv6DnsServerAddressesChanged(); |
| 1061 | } |
| 1062 | } |
| 1063 | |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 1064 | void DeviceInfo::DelayDeviceCreation(int interface_index) { |
| 1065 | delayed_devices_.insert(interface_index); |
| 1066 | delayed_devices_callback_.Reset( |
| 1067 | Bind(&DeviceInfo::DelayedDeviceCreationTask, AsWeakPtr())); |
| 1068 | dispatcher_->PostDelayedTask(delayed_devices_callback_.callback(), |
| 1069 | kDelayedDeviceCreationSeconds * 1000); |
| 1070 | } |
| 1071 | |
| 1072 | // Re-evaluate the technology type for each delayed device. |
| 1073 | void DeviceInfo::DelayedDeviceCreationTask() { |
| 1074 | while (!delayed_devices_.empty()) { |
| 1075 | set<int>::iterator it = delayed_devices_.begin(); |
| 1076 | int dev_index = *it; |
| 1077 | delayed_devices_.erase(it); |
| 1078 | |
| 1079 | DCHECK(ContainsKey(infos_, dev_index)); |
| 1080 | DCHECK(!GetDevice(dev_index)); |
| 1081 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1082 | const string& link_name = infos_[dev_index].name; |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 1083 | Technology::Identifier technology = GetDeviceTechnology(link_name); |
| 1084 | |
| 1085 | if (technology == Technology::kCDCEthernet) { |
| 1086 | LOG(INFO) << "In " << __func__ << ": device " << link_name |
| 1087 | << " is now assumed to be regular Ethernet."; |
| 1088 | technology = Technology::kEthernet; |
| 1089 | } else if (technology != Technology::kCellular) { |
| 1090 | LOG(WARNING) << "In " << __func__ << ": device " << link_name |
| 1091 | << " is unexpected technology " |
| 1092 | << Technology::NameFromIdentifier(technology); |
| 1093 | } |
| 1094 | string address = |
Ben Chan | 0353c22 | 2014-09-05 08:20:59 -0700 | [diff] [blame] | 1095 | base::StringToLowerASCII(infos_[dev_index].mac_address.HexEncode()); |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 1096 | DCHECK(!address.empty()); |
| 1097 | |
| 1098 | DeviceRefPtr device = CreateDevice(link_name, address, dev_index, |
| 1099 | technology); |
| 1100 | if (device) { |
| 1101 | RegisterDevice(device); |
| 1102 | } |
| 1103 | } |
| 1104 | } |
| 1105 | |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 1106 | void DeviceInfo::RetrieveLinkStatistics(int interface_index, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1107 | const RTNLMessage& msg) { |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 1108 | if (!msg.HasAttribute(IFLA_STATS64)) { |
| 1109 | return; |
| 1110 | } |
| 1111 | ByteString stats_bytes(msg.GetAttribute(IFLA_STATS64)); |
| 1112 | struct rtnl_link_stats64 stats; |
| 1113 | if (stats_bytes.GetLength() < sizeof(stats)) { |
| 1114 | LOG(WARNING) << "Link statistics size is too small: " |
| 1115 | << stats_bytes.GetLength() << " < " << sizeof(stats); |
| 1116 | return; |
| 1117 | } |
| 1118 | |
| 1119 | memcpy(&stats, stats_bytes.GetConstData(), sizeof(stats)); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1120 | SLOG(this, 2) << "Link statistics for " |
| 1121 | << " interface index " << interface_index << ": " |
| 1122 | << "receive: " << stats.rx_bytes << "; " |
| 1123 | << "transmit: " << stats.tx_bytes << "."; |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 1124 | infos_[interface_index].rx_bytes = stats.rx_bytes; |
| 1125 | infos_[interface_index].tx_bytes = stats.tx_bytes; |
| 1126 | } |
| 1127 | |
| 1128 | void DeviceInfo::RequestLinkStatistics() { |
| 1129 | rtnl_handler_->RequestDump(RTNLHandler::kRequestLink); |
| 1130 | dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(), |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 1131 | kRequestLinkStatisticsIntervalMilliseconds); |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Peter Qiu | 1a72f54 | 2015-04-14 16:31:36 -0700 | [diff] [blame] | 1134 | #if !defined(DISABLE_WIFI) |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 1135 | void DeviceInfo::GetWiFiInterfaceInfo(int interface_index) { |
| 1136 | GetInterfaceMessage msg; |
| 1137 | if (!msg.attributes()->SetU32AttributeValue(NL80211_ATTR_IFINDEX, |
| 1138 | interface_index)) { |
| 1139 | LOG(ERROR) << "Unable to set interface index attribute for " |
| 1140 | "GetInterface message. Interface type cannot be " |
| 1141 | "determined!"; |
| 1142 | return; |
| 1143 | } |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 1144 | netlink_manager_->SendNl80211Message( |
| 1145 | &msg, |
| 1146 | Bind(&DeviceInfo::OnWiFiInterfaceInfoReceived, AsWeakPtr()), |
Samuel Tan | 5412de0 | 2014-08-15 17:56:26 -0700 | [diff] [blame] | 1147 | Bind(&NetlinkManager::OnAckDoNothing), |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 1148 | Bind(&NetlinkManager::OnNetlinkMessageError)); |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1151 | void DeviceInfo::OnWiFiInterfaceInfoReceived(const Nl80211Message& msg) { |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 1152 | if (msg.command() != NL80211_CMD_NEW_INTERFACE) { |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 1153 | LOG(ERROR) << "Message is not a new interface response"; |
| 1154 | return; |
| 1155 | } |
| 1156 | |
| 1157 | uint32_t interface_index; |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 1158 | if (!msg.const_attributes()->GetU32AttributeValue(NL80211_ATTR_IFINDEX, |
| 1159 | &interface_index)) { |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 1160 | LOG(ERROR) << "Message contains no interface index"; |
| 1161 | return; |
| 1162 | } |
| 1163 | uint32_t interface_type; |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 1164 | if (!msg.const_attributes()->GetU32AttributeValue(NL80211_ATTR_IFTYPE, |
| 1165 | &interface_type)) { |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 1166 | LOG(ERROR) << "Message contains no interface type"; |
| 1167 | return; |
| 1168 | } |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1169 | const Info* info = GetInfo(interface_index); |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 1170 | if (!info) { |
| 1171 | LOG(ERROR) << "Could not find device info for interface index " |
| 1172 | << interface_index; |
| 1173 | return; |
| 1174 | } |
| 1175 | if (info->device) { |
| 1176 | LOG(ERROR) << "Device already created for interface index " |
| 1177 | << interface_index; |
| 1178 | return; |
| 1179 | } |
| 1180 | if (interface_type != NL80211_IFTYPE_STATION) { |
| 1181 | LOG(INFO) << "Ignoring WiFi device " |
| 1182 | << info->name |
| 1183 | << " at interface index " |
| 1184 | << interface_index |
| 1185 | << " since it is not in station mode."; |
| 1186 | return; |
| 1187 | } |
| 1188 | LOG(INFO) << "Creating WiFi device for station mode interface " |
| 1189 | << info->name |
| 1190 | << " at interface index " |
| 1191 | << interface_index; |
Ben Chan | 0353c22 | 2014-09-05 08:20:59 -0700 | [diff] [blame] | 1192 | string address = base::StringToLowerASCII(info->mac_address.HexEncode()); |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 1193 | DeviceRefPtr device = |
| 1194 | new WiFi(control_interface_, dispatcher_, metrics_, manager_, |
| 1195 | info->name, address, interface_index); |
| 1196 | device->EnableIPv6Privacy(); |
| 1197 | RegisterDevice(device); |
| 1198 | } |
Peter Qiu | 1a72f54 | 2015-04-14 16:31:36 -0700 | [diff] [blame] | 1199 | #endif // DISABLE_WIFI |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 1200 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1201 | bool DeviceInfo::SetHostname(const std::string& hostname) const { |
Paul Stewart | 208a97e | 2015-05-13 09:11:12 -0700 | [diff] [blame] | 1202 | if (sethostname(hostname.c_str(), hostname.length())) { |
| 1203 | PLOG(ERROR) << "Failed to set hostname to: " << hostname; |
| 1204 | return false; |
| 1205 | } |
| 1206 | |
| 1207 | return true; |
| 1208 | } |
| 1209 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 1210 | } // namespace shill |