blob: b2ad399786586a1e1f04041a4bc1875708509b95 [file] [log] [blame]
Darin Petkov41c0e0a2012-01-09 16:38:53 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart0af98bf2011-05-10 17:38:08 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Paul Stewartcba0f7f2012-02-29 16:33:05 -08005#include "shill/device_info.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -07006
Paul Stewart0af98bf2011-05-10 17:38:08 -07007#include <arpa/inet.h>
Paul Stewartcba0f7f2012-02-29 16:33:05 -08008#include <fcntl.h>
9#include <linux/if_tun.h>
Paul Stewart0af98bf2011-05-10 17:38:08 -070010#include <linux/netlink.h>
11#include <linux/rtnetlink.h>
Paul Stewartcba0f7f2012-02-29 16:33:05 -080012#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 Stewart0af98bf2011-05-10 17:38:08 -070021#include <string>
22
Eric Shienbrood3e20a232012-02-16 11:35:56 -050023#include <base/bind.h>
Ben Chana0ddf462014-02-06 11:32:42 -080024#include <base/files/file_enumerator.h>
Ben Chan11c213f2014-09-05 08:21:06 -070025#include <base/files/file_util.h>
Ben Chan6fbf64f2014-05-21 18:07:01 -070026#include <base/files/scoped_file.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050027#include <base/stl_util.h>
Ben Chana0ddf462014-02-06 11:32:42 -080028#include <base/strings/string_number_conversions.h>
29#include <base/strings/string_util.h>
30#include <base/strings/stringprintf.h>
Paul Stewart0af98bf2011-05-10 17:38:08 -070031
32#include "shill/control_interface.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070033#include "shill/device.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070034#include "shill/device_stub.h"
Ben Chan87602512014-11-07 20:50:05 -080035#include "shill/ethernet/ethernet.h"
36#include "shill/ethernet/virtio_ethernet.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070037#include "shill/logging.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070038#include "shill/manager.h"
Peter Qiu8d6b5972014-10-28 15:33:34 -070039#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 Stewart8c116a92012-05-02 18:30:03 -070045#include "shill/routing_table.h"
Chris Masone487b8bf2011-05-13 16:27:57 -070046#include "shill/service.h"
Ben Chan2240e8c2014-11-06 13:37:18 -080047#include "shill/vpn/vpn_provider.h"
Peter Qiu1a72f542015-04-14 16:31:36 -070048
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 Chand6a8b512014-11-18 10:45:15 -080053#include "shill/wifi/wifi.h"
Peter Qiu1a72f542015-04-14 16:31:36 -070054#endif // DISABLE_WIFI
Paul Stewart0af98bf2011-05-10 17:38:08 -070055
Eric Shienbrood3e20a232012-02-16 11:35:56 -050056using base::Bind;
Ben Chana0ddf462014-02-06 11:32:42 -080057using base::FileEnumerator;
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080058using base::FilePath;
Thieu Le8f1c8352012-04-16 11:02:12 -070059using base::StringPrintf;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050060using base::Unretained;
Darin Petkove6193c02011-08-11 12:42:40 -070061using std::map;
Paul Stewart050cfc02012-07-06 20:38:54 -070062using std::set;
Paul Stewart0af98bf2011-05-10 17:38:08 -070063using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070064using std::vector;
Paul Stewart0af98bf2011-05-10 17:38:08 -070065
66namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070067
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070068namespace Logging {
69static auto kModuleLogScope = ScopeLogger::kDevice;
Paul Stewarta794cd62015-06-16 13:13:10 -070070static string ObjectID(const DeviceInfo* d) { return "(device_info)"; }
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070071}
72
Paul Stewartb50f0b92011-05-16 16:31:42 -070073// static
Jason Glasgowabc54032012-04-20 16:08:32 -040074const char DeviceInfo::kModemPseudoDeviceNamePrefix[] = "pseudomodem";
Christopher Wileye049cc52012-09-10 14:36:57 -070075const char DeviceInfo::kEthernetPseudoDeviceNamePrefix[] = "pseudoethernet";
Paul Stewart83d62562015-02-09 19:57:47 -080076const char DeviceInfo::kIgnoredDeviceNamePrefix[] = "veth";
Paul Stewartca876ee2012-04-21 08:55:58 -070077const char DeviceInfo::kDeviceInfoRoot[] = "/sys/class/net";
Ben Chan4e64d2d2012-05-16 00:02:25 -070078const char DeviceInfo::kDriverCdcEther[] = "cdc_ether";
Ben Chan4eb4ddf2013-06-20 22:16:56 -070079const char DeviceInfo::kDriverCdcNcm[] = "cdc_ncm";
Ben Chan4e64d2d2012-05-16 00:02:25 -070080const char DeviceInfo::kDriverGdmWiMax[] = "gdm_wimax";
mukesh agrawal93a29ed2012-04-17 16:13:01 -070081const char DeviceInfo::kDriverVirtioNet[] = "virtio_net";
Paul Stewartca876ee2012-04-21 08:55:58 -070082const char DeviceInfo::kInterfaceUevent[] = "uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080083const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Paul Stewartca876ee2012-04-21 08:55:58 -070084const char DeviceInfo::kInterfaceDevice[] = "device";
85const char DeviceInfo::kInterfaceDriver[] = "device/driver";
86const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags";
87const char DeviceInfo::kInterfaceType[] = "type";
Paul Stewarta794cd62015-06-16 13:13:10 -070088const char* DeviceInfo::kModemDrivers[] = {
Paul Stewartb50f0b92011-05-16 16:31:42 -070089 "gobi",
90 "QCUSBNet2k",
Ben Chan226d46a2012-10-11 00:22:17 -070091 "GobiNet",
Ben Chan0f90e0b2013-06-26 23:37:16 -070092 "cdc_mbim",
Ben Chan226d46a2012-10-11 00:22:17 -070093 "qmi_wwan"
Paul Stewartb50f0b92011-05-16 16:31:42 -070094};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080095const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewart050cfc02012-07-06 20:38:54 -070096const int DeviceInfo::kDelayedDeviceCreationSeconds = 5;
Ben Chanb061f892013-02-27 17:46:55 -080097const int DeviceInfo::kRequestLinkStatisticsIntervalMilliseconds = 20000;
Paul Stewartb50f0b92011-05-16 16:31:42 -070098
Paul Stewarta794cd62015-06-16 13:13:10 -070099DeviceInfo::DeviceInfo(ControlInterface* control_interface,
100 EventDispatcher* dispatcher,
101 Metrics* metrics,
102 Manager* manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -0700103 : control_interface_(control_interface),
104 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -0800105 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -0700106 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500107 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
108 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Peter Qiu98551702014-07-28 13:28:53 -0700109 rdnss_callback_(Bind(&DeviceInfo::RdnssMsgHandler, Unretained(this))),
Paul Stewartca876ee2012-04-21 08:55:58 -0700110 device_info_root_(kDeviceInfoRoot),
Paul Stewart8c116a92012-05-02 18:30:03 -0700111 routing_table_(RoutingTable::GetInstance()),
Gary Morain41780232012-07-31 15:08:31 -0700112 rtnl_handler_(RTNLHandler::GetInstance()),
Peter Qiu1a72f542015-04-14 16:31:36 -0700113#if !defined(DISABLE_WIFI)
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700114 netlink_manager_(NetlinkManager::GetInstance()),
Peter Qiu1a72f542015-04-14 16:31:36 -0700115#endif // DISABLE_WIFI
Peter Qiu98551702014-07-28 13:28:53 -0700116 sockets_(new Sockets()),
117 time_(Time::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -0700118}
119
Paul Stewarta3c56f92011-05-26 07:08:52 -0700120DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -0700121
Paul Stewarta794cd62015-06-16 13:13:10 -0700122void DeviceInfo::AddDeviceToBlackList(const string& device_name) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700123 black_list_.insert(device_name);
Peter Qiu7e8b8ee2014-11-25 13:55:57 -0800124 // 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 Qiuebd709e2015-03-16 14:03:37 -0700127 if (manager_->running()) {
128 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink);
129 }
Peter Qiu7e8b8ee2014-11-25 13:55:57 -0800130}
131
Paul Stewarta794cd62015-06-16 13:13:10 -0700132void DeviceInfo::RemoveDeviceFromBlackList(const string& device_name) {
Peter Qiu7e8b8ee2014-11-25 13:55:57 -0800133 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 Qiuebd709e2015-03-16 14:03:37 -0700137 if (manager_->running()) {
138 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink);
139 }
mukesh agrawal8f317b62011-07-15 11:53:23 -0700140}
141
Paul Stewarta794cd62015-06-16 13:13:10 -0700142bool DeviceInfo::IsDeviceBlackListed(const string& device_name) {
Eric Shienbrood5e628a52012-03-21 16:56:59 -0400143 return ContainsKey(black_list_, device_name);
144}
145
Paul Stewarta3c56f92011-05-26 07:08:52 -0700146void DeviceInfo::Start() {
147 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500148 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700149 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500150 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Peter Qiu98551702014-07-28 13:28:53 -0700151 rdnss_listener_.reset(
152 new RTNLListener(RTNLHandler::kRequestRdnss, rdnss_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700153 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
154 RTNLHandler::kRequestAddr);
Paul Stewart1ac4e842012-07-10 12:58:12 -0700155 request_link_statistics_callback_.Reset(
156 Bind(&DeviceInfo::RequestLinkStatistics, AsWeakPtr()));
157 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
Ben Chanb061f892013-02-27 17:46:55 -0800158 kRequestLinkStatisticsIntervalMilliseconds);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700159}
160
Paul Stewarta3c56f92011-05-26 07:08:52 -0700161void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700162 link_listener_.reset();
163 address_listener_.reset();
Paul Stewart8c116a92012-05-02 18:30:03 -0700164 infos_.clear();
Paul Stewart1ac4e842012-07-10 12:58:12 -0700165 request_link_statistics_callback_.Cancel();
Paul Stewart050cfc02012-07-06 20:38:54 -0700166 delayed_devices_callback_.Cancel();
167 delayed_devices_.clear();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700168}
169
Ben Chan5086b972013-01-15 21:51:38 -0800170vector<string> DeviceInfo::GetUninitializedTechnologies() const {
171 set<string> unique_technologies;
Arman Ugurayb00c13d2013-07-29 18:20:52 -0700172 set<Technology::Identifier> initialized_technologies;
Paul Stewarta794cd62015-06-16 13:13:10 -0700173 for (const auto& info : infos_) {
Paul Stewart6db7b242014-05-02 15:34:21 -0700174 Technology::Identifier technology = info.second.technology;
175 if (info.second.device) {
Arman Ugurayb00c13d2013-07-29 18:20:52 -0700176 // 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 Chan5086b972013-01-15 21:51:38 -0800185 unique_technologies.insert(Technology::NameFromIdentifier(technology));
186 }
187 return vector<string>(unique_technologies.begin(), unique_technologies.end());
188}
189
Paul Stewarta794cd62015-06-16 13:13:10 -0700190void DeviceInfo::RegisterDevice(const DeviceRefPtr& device) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700191 SLOG(this, 2) << __func__ << "(" << device->link_name() << ", "
192 << device->interface_index() << ")";
Paul Stewart050cfc02012-07-06 20:38:54 -0700193 delayed_devices_.erase(device->interface_index());
Darin Petkove6193c02011-08-11 12:42:40 -0700194 CHECK(!GetDevice(device->interface_index()).get());
195 infos_[device->interface_index()].device = device;
Thieu Le9abd6742013-01-23 23:35:37 -0800196 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 Chan5086b972013-01-15 21:51:38 -0800202 if (Technology::IsPrimaryConnectivityTechnology(device->technology())) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700203 manager_->RegisterDevice(device);
204 }
205}
206
Paul Stewarta794cd62015-06-16 13:13:10 -0700207void DeviceInfo::DeregisterDevice(const DeviceRefPtr& device) {
Jason Glasgowe9089492012-02-23 17:57:37 -0500208 int interface_index = device->interface_index();
209
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700210 SLOG(this, 2) << __func__ << "(" << device->link_name() << ", "
211 << interface_index << ")";
Joshua Krollda798622012-06-05 12:30:48 -0700212 CHECK((device->technology() == Technology::kCellular) ||
213 (device->technology() == Technology::kWiMax));
Jason Glasgowe9089492012-02-23 17:57:37 -0500214
215 // Release reference to the device
216 map<int, Info>::iterator iter = infos_.find(interface_index);
217 if (iter != infos_.end()) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700218 SLOG(this, 2) << "Removing device from info for index: "
219 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500220 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 Chancc225ef2014-09-30 13:26:51 -0700223 iter->second.device = nullptr;
Jason Glasgowe9089492012-02-23 17:57:37 -0500224 }
Thieu Lec8078a62013-01-22 18:01:12 -0800225 metrics_->DeregisterDevice(device->interface_index());
Jason Glasgowe9089492012-02-23 17:57:37 -0500226}
227
Paul Stewarta794cd62015-06-16 13:13:10 -0700228FilePath DeviceInfo::GetDeviceInfoPath(const string& iface_name,
229 const string& path_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700230 return device_info_root_.Append(iface_name).Append(path_name);
231}
232
Paul Stewarta794cd62015-06-16 13:13:10 -0700233bool DeviceInfo::GetDeviceInfoContents(const string& iface_name,
234 const string& path_name,
235 string* contents_out) {
Ben Chana0ddf462014-02-06 11:32:42 -0800236 return base::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
237 contents_out);
Paul Stewartca876ee2012-04-21 08:55:58 -0700238}
Paul Stewarta794cd62015-06-16 13:13:10 -0700239bool DeviceInfo::GetDeviceInfoSymbolicLink(const string& iface_name,
240 const string& path_name,
241 FilePath* path_out) {
Ben Chana0ddf462014-02-06 11:32:42 -0800242 return base::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
243 path_out);
Paul Stewartca876ee2012-04-21 08:55:58 -0700244}
245
Paul Stewartfdd16072011-09-16 12:41:35 -0700246Technology::Identifier DeviceInfo::GetDeviceTechnology(
Paul Stewarta794cd62015-06-16 13:13:10 -0700247 const string& iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700248 string type_string;
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700249 int arp_type = ARPHRD_VOID;
Paul Stewartca876ee2012-04-21 08:55:58 -0700250 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
Ben Chana0ddf462014-02-06 11:32:42 -0800251 base::TrimString(type_string, "\n", &type_string) &&
Paul Stewartca876ee2012-04-21 08:55:58 -0700252 !base::StringToInt(type_string, &arp_type)) {
253 arp_type = ARPHRD_VOID;
254 }
255
Paul Stewart9364c4c2011-12-06 17:12:42 -0800256 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700257 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Paul Stewart050cfc02012-07-06 20:38:54 -0700258 LOG(INFO) << StringPrintf("%s: device %s has no uevent file",
259 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700260 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800261 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700262
Thieu Le8f1c8352012-04-16 11:02:12 -0700263 // 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 Stewart9364c4c2011-12-06 17:12:42 -0800266 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700267 SLOG(this, 2)
Ben Chanfad4a0b2012-04-18 15:49:59 -0700268 << StringPrintf("%s: device %s has wifi signature in uevent file",
269 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700270 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700271 SLOG(this, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
272 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800273 return Technology::kWiFiMonitor;
274 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700275 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700276 }
277
Jason Glasgow7904d142012-05-18 13:36:20 -0400278 // Special case for pseudo modems which are used for testing
279 if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700280 SLOG(this, 2) << StringPrintf(
Jason Glasgow7904d142012-05-18 13:36:20 -0400281 "%s: device %s is a pseudo modem for testing",
282 __func__, iface_name.c_str());
283 return Technology::kCellular;
284 }
285
Christopher Wileye049cc52012-09-10 14:36:57 -0700286 // Special case for pseudo ethernet devices which are used for testing.
287 if (iface_name.find(kEthernetPseudoDeviceNamePrefix) == 0) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700288 SLOG(this, 2) << StringPrintf(
Christopher Wileye049cc52012-09-10 14:36:57 -0700289 "%s: device %s is a virtual ethernet device for testing",
290 __func__, iface_name.c_str());
291 return Technology::kEthernet;
292 }
293
Paul Stewart83d62562015-02-09 19:57:47 -0800294 // 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 Stewart9364c4c2011-12-06 17:12:42 -0800301 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700302 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700303 SLOG(this, 2) << StringPrintf("%s: device %s has no device symlink",
304 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700305 if (arp_type == ARPHRD_LOOPBACK) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700306 SLOG(this, 2) << StringPrintf("%s: device %s is a loopback device",
307 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700308 return Technology::kLoopback;
309 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700310 if (arp_type == ARPHRD_PPP) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700311 SLOG(this, 2) << StringPrintf("%s: device %s is a ppp device",
312 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700313 return Technology::kPPP;
314 }
315 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800316 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700317 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
Ben Chana0ddf462014-02-06 11:32:42 -0800318 base::TrimString(tun_flags_str, "\n", &tun_flags_str) &&
Paul Stewartca876ee2012-04-21 08:55:58 -0700319 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800320 (tun_flags & IFF_TUN)) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700321 SLOG(this, 2) << StringPrintf("%s: device %s is tun device",
322 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800323 return Technology::kTunnel;
324 }
Paul Stewartda8cbee2014-10-20 11:44:03 -0700325
Paul Stewart83d62562015-02-09 19:57:47 -0800326 // 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 Stewart9364c4c2011-12-06 17:12:42 -0800340 }
341
342 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700343 // 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 Stewart9364c4c2011-12-06 17:12:42 -0800346 if (driver_name == kModemDrivers[modem_idx]) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700347 SLOG(this, 2)
Ben Chanfad4a0b2012-04-18 15:49:59 -0700348 << StringPrintf("%s: device %s is matched with modem driver %s",
349 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800350 return Technology::kCellular;
351 }
352 }
353
Ben Chan4e64d2d2012-05-16 00:02:25 -0700354 if (driver_name == kDriverGdmWiMax) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700355 SLOG(this, 2) << StringPrintf("%s: device %s is a WiMAX device",
356 __func__, iface_name.c_str());
Ben Chan4e64d2d2012-05-16 00:02:25 -0700357 return Technology::kWiMax;
358 }
359
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700360 // For cdc_ether / cdc_ncm devices, make sure it's a modem because this driver
Thieu Le8f1c8352012-04-16 11:02:12 -0700361 // can be used for other ethernet devices.
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700362 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 agrawal35ec8402013-07-19 14:49:08 -0700365 iface_name.c_str(), driver_name.c_str());
Paul Stewart050cfc02012-07-06 20:38:54 -0700366 return Technology::kCellular;
367 }
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700368 SLOG(this, 2) << StringPrintf("%s: device %s is a %s device", __func__,
369 iface_name.c_str(), driver_name.c_str());
Paul Stewart050cfc02012-07-06 20:38:54 -0700370 return Technology::kCDCEthernet;
Thieu Le8f1c8352012-04-16 11:02:12 -0700371 }
372
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700373 // 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 Silbersteinc9c31d82014-10-21 15:01:00 -0700376 SLOG(this, 2) << StringPrintf("%s: device %s is virtio ethernet",
377 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700378 return Technology::kVirtioEthernet;
379 }
380
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700381 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 Stewartfdd16072011-09-16 12:41:35 -0700385 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700386}
387
Paul Stewarta794cd62015-06-16 13:13:10 -0700388bool DeviceInfo::IsCdcEthernetModemDevice(const std::string& iface_name) {
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700389 // 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 Le8f1c8352012-04-16 11:02:12 -0700392 //
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700393 // A typical sysfs dir hierarchy for a cdc_ether / cdc_ncm modem USB device is
394 // as follows:
Thieu Le8f1c8352012-04-16 11:02:12 -0700395 //
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 Leb27beee2012-04-20 09:19:06 -0700410 //
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 Le8f1c8352012-04-16 11:02:12 -0700414
Paul Stewartca876ee2012-04-21 08:55:58 -0700415 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700416 FilePath device_path;
Ben Chana0ddf462014-02-06 11:32:42 -0800417 if (!base::ReadSymbolicLink(device_file, &device_path)) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700418 SLOG(this, 2) << StringPrintf("%s: device %s has no device symlink",
419 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700420 return false;
421 }
422 if (!device_path.IsAbsolute()) {
Ben Chana0ddf462014-02-06 11:32:42 -0800423 device_path =
424 base::MakeAbsoluteFilePath(device_file.DirName().Append(device_path));
Thieu Le8f1c8352012-04-16 11:02:12 -0700425 }
426
427 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700428 // 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 Le8f1c8352012-04-16 11:02:12 -0700431 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700432 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700433}
434
435// static
Paul Stewarta794cd62015-06-16 13:13:10 -0700436bool DeviceInfo::HasSubdir(const FilePath& base_dir, const FilePath& subdir) {
Ben Chana0ddf462014-02-06 11:32:42 -0800437 FileEnumerator::FileType type = static_cast<FileEnumerator::FileType>(
438 FileEnumerator::DIRECTORIES | FileEnumerator::SHOW_SYM_LINKS);
439 FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700440 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
441 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700442 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700443 return true;
444 }
445 return false;
446}
447
Paul Stewarta794cd62015-06-16 13:13:10 -0700448DeviceRefPtr DeviceInfo::CreateDevice(const string& link_name,
449 const string& address,
Paul Stewart8c116a92012-05-02 18:30:03 -0700450 int interface_index,
451 Technology::Identifier technology) {
452 DeviceRefPtr device;
Paul Stewart050cfc02012-07-06 20:38:54 -0700453 delayed_devices_.erase(interface_index);
Ben Chan5086b972013-01-15 21:51:38 -0800454 infos_[interface_index].technology = technology;
Paul Stewart8c116a92012-05-02 18:30:03 -0700455
456 switch (technology) {
457 case Technology::kCellular:
Ben Chan5742b242013-12-18 16:25:43 -0800458#if defined(DISABLE_CELLULAR)
459 LOG(WARNING) << "Cellular support is not implemented. "
460 << "Ignore cellular device " << link_name << " at index "
461 << interface_index << ".";
Ben Chancc225ef2014-09-30 13:26:51 -0700462 return nullptr;
Ben Chan5742b242013-12-18 16:25:43 -0800463#else
Paul Stewart8c116a92012-05-02 18:30:03 -0700464 // Cellular devices are managed by ModemInfo.
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700465 SLOG(this, 2) << "Cellular link " << link_name
466 << " at index " << interface_index
467 << " -- notifying ModemInfo.";
Gary Morainbf74a672012-07-30 16:27:19 -0700468
469 // The MAC address provided by RTNL is not reliable for Gobi 2K modems.
Ben Chan4b285862012-10-10 22:52:16 -0700470 // Clear it here, and it will be fetched from the kernel in
471 // GetMACAddress().
Gary Morainbf74a672012-07-30 16:27:19 -0700472 infos_[interface_index].mac_address.Clear();
Paul Stewart8c116a92012-05-02 18:30:03 -0700473 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
474 break;
Ben Chan5742b242013-12-18 16:25:43 -0800475#endif // DISABLE_CELLULAR
Paul Stewart8c116a92012-05-02 18:30:03 -0700476 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 Qiu1a72f542015-04-14 16:31:36 -0700488#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 Stewart2ddf2c62013-04-16 09:47:34 -0700493 // Defer creating this device until we get information about the
494 // type of WiFi interface.
495 GetWiFiInterfaceInfo(interface_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700496 break;
Peter Qiu1a72f542015-04-14 16:31:36 -0700497#endif // DISABLE_WIFI
Ben Chan4e64d2d2012-05-16 00:02:25 -0700498 case Technology::kWiMax:
Ben Chan520eb172013-10-30 20:51:04 -0700499#if defined(DISABLE_WIMAX)
500 LOG(WARNING) << "WiMax support is not implemented. Ignore WiMax link "
501 << link_name << " at index " << interface_index << ".";
Ben Chancc225ef2014-09-30 13:26:51 -0700502 return nullptr;
Ben Chan520eb172013-10-30 20:51:04 -0700503#else
Darin Petkove4b27022012-05-16 13:28:50 +0200504 // WiMax devices are managed by WiMaxProvider.
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700505 SLOG(this, 2) << "WiMax link " << link_name
506 << " at index " << interface_index
507 << " -- notifying WiMaxProvider.";
Ben Chan4b285862012-10-10 22:52:16 -0700508 // 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 Petkove4b27022012-05-16 13:28:50 +0200514 manager_->wimax_provider()->OnDeviceInfoAvailable(link_name);
Ben Chan4e64d2d2012-05-16 00:02:25 -0700515 break;
Ben Chan520eb172013-10-30 20:51:04 -0700516#endif // DISABLE_WIMAX
Paul Stewart8c116a92012-05-02 18:30:03 -0700517 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 Silbersteinc9c31d82014-10-21 15:01:00 -0700524 SLOG(this, 2) << "Tunnel / PPP link " << link_name
525 << " at index " << interface_index
526 << " -- notifying VPNProvider.";
Paul Stewart8c116a92012-05-02 18:30:03 -0700527 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 Silbersteinc9c31d82014-10-21 15:01:00 -0700532 SLOG(this, 2) << "Tunnel link is unused. Deleting.";
Paul Stewart8c116a92012-05-02 18:30:03 -0700533 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 Silbersteinc9c31d82014-10-21 15:01:00 -0700539 SLOG(this, 2) << "Bringing up loopback device " << link_name
540 << " at index " << interface_index;
Paul Stewart8c116a92012-05-02 18:30:03 -0700541 rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP);
Ben Chancc225ef2014-09-30 13:26:51 -0700542 return nullptr;
Paul Stewart050cfc02012-07-06 20:38:54 -0700543 case Technology::kCDCEthernet:
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700544 // CDCEthernet devices are of indeterminate type when they are
Paul Stewart050cfc02012-07-06 20:38:54 -0700545 // 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 Chancc225ef2014-09-30 13:26:51 -0700554 return nullptr;
Paul Stewart8c116a92012-05-02 18:30:03 -0700555 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 Chan4e64d2d2012-05-16 00:02:25 -0700561 technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700562 }
563
564 // Reset the routing table and addresses.
565 routing_table_->FlushRoutes(interface_index);
566 FlushAddresses(interface_index);
567
Ben Chan5086b972013-01-15 21:51:38 -0800568 manager_->UpdateUninitializedTechnologies();
569
Paul Stewart8c116a92012-05-02 18:30:03 -0700570 return device;
571}
572
Paul Stewarta794cd62015-06-16 13:13:10 -0700573void DeviceInfo::AddLinkMsgHandler(const RTNLMessage& msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700574 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
575 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700576 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700577 Technology::Identifier technology = Technology::kUnknown;
Darin Petkove6193c02011-08-11 12:42:40 -0700578 unsigned int flags = msg.link_status().flags;
579 unsigned int change = msg.link_status().change;
Paul Stewart8c116a92012-05-02 18:30:03 -0700580 bool new_device =
581 !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only;
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700582 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 Stewart8c116a92012-05-02 18:30:03 -0700587 infos_[dev_index].has_addresses_only = false;
Darin Petkove6193c02011-08-11 12:42:40 -0700588 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700589
Paul Stewart1ac4e842012-07-10 12:58:12 -0700590 RetrieveLinkStatistics(dev_index, msg);
591
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700592 DeviceRefPtr device = GetDevice(dev_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700593 if (new_device) {
594 CHECK(!device);
Chris Masone2aa97072011-08-09 17:35:08 -0700595 if (!msg.HasAttribute(IFLA_IFNAME)) {
596 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
597 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700598 }
Chris Masone2aa97072011-08-09 17:35:08 -0700599 ByteString b(msg.GetAttribute(IFLA_IFNAME));
600 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700601 SLOG(this, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200602 infos_[dev_index].name = link_name;
603 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700604
Chris Masone2aa97072011-08-09 17:35:08 -0700605 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700606 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700607 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700608 } else {
609 technology = GetDeviceTechnology(link_name);
610 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700611 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800612 string address;
613 if (msg.HasAttribute(IFLA_ADDRESS)) {
614 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
Ben Chan0353c222014-09-05 08:20:59 -0700615 address =
616 base::StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700617 SLOG(this, 2) << "link index " << dev_index << " address "
618 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700619 } else if (technology != Technology::kTunnel &&
620 technology != Technology::kPPP) {
mukesh agrawal6813e762013-07-10 19:05:08 -0700621 LOG(ERROR) << "Add Link message for link '" << link_name
622 << "' does not have IFLA_ADDRESS!";
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800623 return;
624 }
Thieu Lec8078a62013-01-22 18:01:12 -0800625 metrics_->RegisterDevice(dev_index, technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700626 device = CreateDevice(link_name, address, dev_index, technology);
627 if (device) {
628 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700629 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700630 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700631 if (device) {
632 device->LinkEvent(flags, change);
633 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700634}
635
Paul Stewarta794cd62015-06-16 13:13:10 -0700636void DeviceInfo::DelLinkMsgHandler(const RTNLMessage& msg) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700637 SLOG(this, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700638
Paul Stewart9a908082011-08-31 12:18:48 -0700639 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
640 msg.mode() == RTNLMessage::kModeDelete);
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700641 SLOG(this, 2) << __func__ << "(index=" << msg.interface_index()
642 << std::showbase << std::hex
Ben Chanfad4a0b2012-04-18 15:49:59 -0700643 << ", flags=" << msg.link_status().flags
644 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700645 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700646}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700647
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700648DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
Paul Stewarta794cd62015-06-16 13:13:10 -0700649 const Info* info = GetInfo(interface_index);
Ben Chancc225ef2014-09-30 13:26:51 -0700650 return info ? info->device : nullptr;
Darin Petkove6193c02011-08-11 12:42:40 -0700651}
652
Paul Stewarta794cd62015-06-16 13:13:10 -0700653int DeviceInfo::GetIndex(const string& interface_name) const {
Darin Petkovf8046b82012-04-24 16:29:23 +0200654 map<string, int>::const_iterator it = indices_.find(interface_name);
655 return it == indices_.end() ? -1 : it->second;
656}
657
Paul Stewarta794cd62015-06-16 13:13:10 -0700658bool DeviceInfo::GetMACAddress(int interface_index, ByteString* address) const {
659 const Info* info = GetInfo(interface_index);
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700660 if (!info) {
661 return false;
662 }
Gary Morainbf74a672012-07-30 16:27:19 -0700663 // |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
675ByteString DeviceInfo::GetMACAddressFromKernel(int interface_index) const {
Paul Stewarta794cd62015-06-16 13:13:10 -0700676 const Info* info = GetInfo(interface_index);
Gary Morain41780232012-07-31 15:08:31 -0700677 if (!info) {
678 return ByteString();
679 }
680
681 const int fd = sockets_->Socket(PF_INET, SOCK_DGRAM, 0);
Gary Morainbf74a672012-07-30 16:27:19 -0700682 if (fd < 0) {
683 LOG(ERROR) << __func__ << ": Unable to open socket: " << fd;
Gary Morain41780232012-07-31 15:08:31 -0700684 return ByteString();
Gary Morainbf74a672012-07-30 16:27:19 -0700685 }
Gary Morain41780232012-07-31 15:08:31 -0700686
687 ScopedSocketCloser socket_closer(sockets_.get(), fd);
Gary Morainbf74a672012-07-30 16:27:19 -0700688 struct ifreq ifr;
689 memset(&ifr, 0, sizeof(ifr));
690 ifr.ifr_ifindex = interface_index;
Alex Vakulenko8a532292014-06-16 17:18:44 -0700691 strcpy(ifr.ifr_ifrn.ifrn_name, info->name.c_str()); // NOLINT(runtime/printf)
Gary Morain41780232012-07-31 15:08:31 -0700692 int err = sockets_->Ioctl(fd, SIOCGIFHWADDR, &ifr);
Gary Morainbf74a672012-07-30 16:27:19 -0700693 if (err < 0) {
694 LOG(ERROR) << __func__ << ": Unable to read MAC address: " << errno;
Gary Morain41780232012-07-31 15:08:31 -0700695 return ByteString();
Gary Morainbf74a672012-07-30 16:27:19 -0700696 }
Gary Morain41780232012-07-31 15:08:31 -0700697
698 return ByteString(ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700699}
700
Paul Stewart6950ba52013-12-05 08:28:14 -0800701bool DeviceInfo::GetMACAddressOfPeer(int interface_index,
Paul Stewarta794cd62015-06-16 13:13:10 -0700702 const IPAddress& peer,
703 ByteString* mac_address) const {
704 const Info* info = GetInfo(interface_index);
Paul Stewart6950ba52013-12-05 08:28:14 -0800705 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) {
716 LOG(ERROR) << __func__ << ": Unable to open socket: " << fd;
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 Stewarta794cd62015-06-16 13:13:10 -0700727 struct sockaddr_in* protocol_address =
728 reinterpret_cast<struct sockaddr_in*>(&areq.arp_pa);
Paul Stewart6950ba52013-12-05 08:28:14 -0800729 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 Stewarta794cd62015-06-16 13:13:10 -0700734 struct sockaddr_in* hardware_address =
735 reinterpret_cast<struct sockaddr_in*>(&areq.arp_ha);
Paul Stewart6950ba52013-12-05 08:28:14 -0800736 hardware_address->sin_family = ARPHRD_ETHER;
737
738 int err = sockets_->Ioctl(fd, SIOCGARP, &areq);
739 if (err < 0) {
740 LOG(ERROR) << __func__ << ": Unable to perform ARP lookup: " << errno;
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 Stewart9a908082011-08-31 12:18:48 -0700756bool DeviceInfo::GetAddresses(int interface_index,
Paul Stewarta794cd62015-06-16 13:13:10 -0700757 vector<AddressData>* addresses) const {
758 const Info* info = GetInfo(interface_index);
Paul Stewart9a908082011-08-31 12:18:48 -0700759 if (!info) {
760 return false;
761 }
762 *addresses = info->ip_addresses;
763 return true;
764}
765
766void DeviceInfo::FlushAddresses(int interface_index) const {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700767 SLOG(this, 2) << __func__ << "(" << interface_index << ")";
Paul Stewarta794cd62015-06-16 13:13:10 -0700768 const Info* info = GetInfo(interface_index);
Paul Stewart9a908082011-08-31 12:18:48 -0700769 if (!info) {
770 return;
771 }
Paul Stewarta794cd62015-06-16 13:13:10 -0700772 for (const auto& address_info : info->ip_addresses) {
Paul Stewart6db7b242014-05-02 15:34:21 -0700773 if (address_info.address.family() == IPAddress::kFamilyIPv4 ||
774 (address_info.scope == RT_SCOPE_UNIVERSE &&
775 (address_info.flags & ~IFA_F_TEMPORARY) == 0)) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700776 SLOG(this, 2) << __func__ << ": removing ip address "
777 << address_info.address.ToString()
778 << " from interface " << interface_index;
Paul Stewart6db7b242014-05-02 15:34:21 -0700779 rtnl_handler_->RemoveInterfaceAddress(interface_index,
780 address_info.address);
Paul Stewart9a908082011-08-31 12:18:48 -0700781 }
782 }
783}
784
Paul Stewart05a42c22012-08-02 16:47:21 -0700785bool DeviceInfo::HasOtherAddress(
Paul Stewarta794cd62015-06-16 13:13:10 -0700786 int interface_index, const IPAddress& this_address) const {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700787 SLOG(this, 3) << __func__ << "(" << interface_index << ")";
Paul Stewarta794cd62015-06-16 13:13:10 -0700788 const Info* info = GetInfo(interface_index);
Paul Stewart05a42c22012-08-02 16:47:21 -0700789 if (!info) {
790 return false;
791 }
Paul Stewart05a42c22012-08-02 16:47:21 -0700792 bool has_other_address = false;
793 bool has_this_address = false;
Paul Stewarta794cd62015-06-16 13:13:10 -0700794 for (const auto& local_address : info->ip_addresses) {
Paul Stewarta0db0ff2013-12-09 09:48:47 -0800795 if (local_address.address.family() != this_address.family()) {
Paul Stewart05a42c22012-08-02 16:47:21 -0700796 continue;
797 }
Paul Stewarta0db0ff2013-12-09 09:48:47 -0800798 if (local_address.address.address().Equals(this_address.address())) {
Paul Stewart05a42c22012-08-02 16:47:21 -0700799 has_this_address = true;
800 } else if (this_address.family() == IPAddress::kFamilyIPv4) {
801 has_other_address = true;
Paul Stewarta0db0ff2013-12-09 09:48:47 -0800802 } else if ((local_address.scope == RT_SCOPE_UNIVERSE &&
803 (local_address.flags & IFA_F_TEMPORARY) == 0)) {
Paul Stewart05a42c22012-08-02 16:47:21 -0700804 has_other_address = true;
805 }
806 }
807 return has_other_address && !has_this_address;
808}
809
Paul Stewartd55f6ae2014-04-24 16:34:21 -0700810bool DeviceInfo::GetPrimaryIPv6Address(int interface_index,
Paul Stewarta794cd62015-06-16 13:13:10 -0700811 IPAddress* address) {
812 const Info* info = GetInfo(interface_index);
Paul Stewartd55f6ae2014-04-24 16:34:21 -0700813 if (!info) {
814 return false;
815 }
816 bool has_temporary_address = false;
817 bool has_address = false;
Paul Stewarta794cd62015-06-16 13:13:10 -0700818 for (const auto& local_address : info->ip_addresses) {
Paul Stewartd55f6ae2014-04-24 16:34:21 -0700819 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 Qiu98551702014-07-28 13:28:53 -0700839bool DeviceInfo::GetIPv6DnsServerAddresses(int interface_index,
Paul Stewarta794cd62015-06-16 13:13:10 -0700840 std::vector<IPAddress>* address_list,
841 uint32_t* life_time) {
842 const Info* info = GetInfo(interface_index);
Peter Qiu98551702014-07-28 13:28:53 -0700843 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 Chan7fab8972014-08-10 17:14:46 -0700855 uint32_t time_elapsed = static_cast<uint32_t>(
Peter Qiu98551702014-07-28 13:28:53 -0700856 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 Stewarta0db0ff2013-12-09 09:48:47 -0800867bool DeviceInfo::HasDirectConnectivityTo(
Paul Stewarta794cd62015-06-16 13:13:10 -0700868 int interface_index, const IPAddress& address) const {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700869 SLOG(this, 3) << __func__ << "(" << interface_index << ")";
Paul Stewarta794cd62015-06-16 13:13:10 -0700870 const Info* info = GetInfo(interface_index);
Paul Stewarta0db0ff2013-12-09 09:48:47 -0800871 if (!info) {
872 return false;
873 }
874
Paul Stewarta794cd62015-06-16 13:13:10 -0700875 for (const auto& local_address : info->ip_addresses) {
Paul Stewarta0db0ff2013-12-09 09:48:47 -0800876 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 Stewarta794cd62015-06-16 13:13:10 -0700885bool DeviceInfo::GetFlags(int interface_index, unsigned int* flags) const {
886 const Info* info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700887 if (!info) {
888 return false;
889 }
890 *flags = info->flags;
891 return true;
892}
893
Paul Stewart1ac4e842012-07-10 12:58:12 -0700894bool DeviceInfo::GetByteCounts(int interface_index,
Paul Stewarta794cd62015-06-16 13:13:10 -0700895 uint64_t* rx_bytes,
896 uint64_t* tx_bytes) const {
897 const Info* info = GetInfo(interface_index);
Paul Stewart1ac4e842012-07-10 12:58:12 -0700898 if (!info) {
899 return false;
900 }
901 *rx_bytes = info->rx_bytes;
902 *tx_bytes = info->tx_bytes;
903 return true;
904}
905
Paul Stewarta794cd62015-06-16 13:13:10 -0700906bool DeviceInfo::CreateTunnelInterface(string* interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800907 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
908 if (fd < 0) {
909 PLOG(ERROR) << "failed to open " << kTunDeviceName;
910 return false;
911 }
Ben Chan6fbf64f2014-05-21 18:07:01 -0700912 base::ScopedFD scoped_fd(fd);
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800913
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 Stewarta794cd62015-06-16 13:13:10 -0700932int DeviceInfo::OpenTunnelInterface(const std::string& interface_name) const {
Prabhu Kaliamoorthi127b3412014-10-16 13:00:25 +0200933 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 Stewartca6abd42012-03-01 15:45:29 -0800951bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800952 return rtnl_handler_->RemoveInterface(interface_index);
953}
954
Paul Stewarta794cd62015-06-16 13:13:10 -0700955const DeviceInfo::Info* DeviceInfo::GetInfo(int interface_index) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700956 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700957 if (iter == infos_.end()) {
Ben Chancc225ef2014-09-30 13:26:51 -0700958 return nullptr;
Darin Petkove6193c02011-08-11 12:42:40 -0700959 }
960 return &iter->second;
961}
962
963void DeviceInfo::RemoveInfo(int interface_index) {
964 map<int, Info>::iterator iter = infos_.find(interface_index);
965 if (iter != infos_.end()) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700966 SLOG(this, 2) << "Removing info for device index: " << interface_index;
Peter Qiu9f5159e2014-09-12 16:50:14 -0700967 // Deregister the device if not deregistered yet. Cellular and WiMax devices
968 // are deregistered through a call to DeviceInfo::DeregisterDevice.
Darin Petkove6193c02011-08-11 12:42:40 -0700969 if (iter->second.device.get()) {
970 manager_->DeregisterDevice(iter->second.device);
Peter Qiu9f5159e2014-09-12 16:50:14 -0700971 metrics_->DeregisterDevice(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700972 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200973 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700974 infos_.erase(iter);
Paul Stewart050cfc02012-07-06 20:38:54 -0700975 delayed_devices_.erase(interface_index);
mukesh agrawal47009f82011-08-25 14:07:35 -0700976 } else {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700977 SLOG(this, 2) << __func__ << ": Unknown device index: "
978 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700979 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700980}
981
Paul Stewarta794cd62015-06-16 13:13:10 -0700982void DeviceInfo::LinkMsgHandler(const RTNLMessage& msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700983 DCHECK(msg.type() == RTNLMessage::kTypeLink);
984 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700985 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700986 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700987 DelLinkMsgHandler(msg);
988 } else {
989 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700990 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700991}
992
Paul Stewarta794cd62015-06-16 13:13:10 -0700993void DeviceInfo::AddressMsgHandler(const RTNLMessage& msg) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700994 SLOG(this, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700995 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
996 int interface_index = msg.interface_index();
997 if (!ContainsKey(infos_, interface_index)) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700998 SLOG(this, 2) << "Got advance address information for unknown index "
999 << interface_index;
Paul Stewart8c116a92012-05-02 18:30:03 -07001000 infos_[interface_index].has_addresses_only = true;
Paul Stewart9a908082011-08-31 12:18:48 -07001001 }
Paul Stewarta794cd62015-06-16 13:13:10 -07001002 const RTNLMessage::AddressStatus& status = msg.address_status();
Paul Stewart9a908082011-08-31 12:18:48 -07001003 IPAddress address(msg.family(),
Ben Chan682c5d42012-06-18 14:11:04 -07001004 msg.HasAttribute(IFA_LOCAL) ?
1005 msg.GetAttribute(IFA_LOCAL) : msg.GetAttribute(IFA_ADDRESS),
Paul Stewart9a908082011-08-31 12:18:48 -07001006 status.prefix_len);
1007
Ben Chan682c5d42012-06-18 14:11:04 -07001008 SLOG_IF(Device, 2, msg.HasAttribute(IFA_LOCAL))
1009 << "Found local address attribute for interface " << interface_index;
1010
Paul Stewarta794cd62015-06-16 13:13:10 -07001011 vector<AddressData>& address_list = infos_[interface_index].ip_addresses;
Paul Stewart9a908082011-08-31 12:18:48 -07001012 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 Silbersteinc9c31d82014-10-21 15:01:00 -07001020 SLOG(this, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -07001021 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 Silbersteinc9c31d82014-10-21 15:01:00 -07001028 SLOG(this, 2) << "Add address " << address.ToString()
1029 << " for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -07001030 }
Paul Stewartd55f6ae2014-04-24 16:34:21 -07001031
1032 DeviceRefPtr device = GetDevice(interface_index);
1033 if (device && address.family() == IPAddress::kFamilyIPv6 &&
1034 status.scope == RT_SCOPE_UNIVERSE) {
1035 device->OnIPv6AddressChanged();
1036 }
Paul Stewart9a908082011-08-31 12:18:48 -07001037}
1038
Paul Stewarta794cd62015-06-16 13:13:10 -07001039void DeviceInfo::RdnssMsgHandler(const RTNLMessage& msg) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -07001040 SLOG(this, 2) << __func__;
Peter Qiu98551702014-07-28 13:28:53 -07001041 DCHECK(msg.type() == RTNLMessage::kTypeRdnss);
1042 int interface_index = msg.interface_index();
1043 if (!ContainsKey(infos_, interface_index)) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -07001044 SLOG(this, 2) << "Got RDNSS option for unknown index "
1045 << interface_index;
Peter Qiu98551702014-07-28 13:28:53 -07001046 }
1047
Paul Stewarta794cd62015-06-16 13:13:10 -07001048 const RTNLMessage::RdnssOption& rdnss_option = msg.rdnss_option();
Peter Qiu98551702014-07-28 13:28:53 -07001049 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 Stewart050cfc02012-07-06 20:38:54 -07001064void 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.
1073void 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 Stewarta794cd62015-06-16 13:13:10 -07001082 const string& link_name = infos_[dev_index].name;
Paul Stewart050cfc02012-07-06 20:38:54 -07001083 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 Chan0353c222014-09-05 08:20:59 -07001095 base::StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Paul Stewart050cfc02012-07-06 20:38:54 -07001096 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 Stewart1ac4e842012-07-10 12:58:12 -07001106void DeviceInfo::RetrieveLinkStatistics(int interface_index,
Paul Stewarta794cd62015-06-16 13:13:10 -07001107 const RTNLMessage& msg) {
Paul Stewart1ac4e842012-07-10 12:58:12 -07001108 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 Silbersteinc9c31d82014-10-21 15:01:00 -07001120 SLOG(this, 2) << "Link statistics for "
1121 << " interface index " << interface_index << ": "
1122 << "receive: " << stats.rx_bytes << "; "
1123 << "transmit: " << stats.tx_bytes << ".";
Paul Stewart1ac4e842012-07-10 12:58:12 -07001124 infos_[interface_index].rx_bytes = stats.rx_bytes;
1125 infos_[interface_index].tx_bytes = stats.tx_bytes;
1126}
1127
1128void DeviceInfo::RequestLinkStatistics() {
1129 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink);
1130 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
Ben Chanb061f892013-02-27 17:46:55 -08001131 kRequestLinkStatisticsIntervalMilliseconds);
Paul Stewart1ac4e842012-07-10 12:58:12 -07001132}
1133
Peter Qiu1a72f542015-04-14 16:31:36 -07001134#if !defined(DISABLE_WIFI)
Paul Stewart2ddf2c62013-04-16 09:47:34 -07001135void 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 Guthrie7347bf22013-04-30 11:21:51 -07001144 netlink_manager_->SendNl80211Message(
1145 &msg,
1146 Bind(&DeviceInfo::OnWiFiInterfaceInfoReceived, AsWeakPtr()),
Samuel Tan5412de02014-08-15 17:56:26 -07001147 Bind(&NetlinkManager::OnAckDoNothing),
Wade Guthrie7347bf22013-04-30 11:21:51 -07001148 Bind(&NetlinkManager::OnNetlinkMessageError));
Paul Stewart2ddf2c62013-04-16 09:47:34 -07001149}
1150
Paul Stewarta794cd62015-06-16 13:13:10 -07001151void DeviceInfo::OnWiFiInterfaceInfoReceived(const Nl80211Message& msg) {
Wade Guthrie7347bf22013-04-30 11:21:51 -07001152 if (msg.command() != NL80211_CMD_NEW_INTERFACE) {
Paul Stewart2ddf2c62013-04-16 09:47:34 -07001153 LOG(ERROR) << "Message is not a new interface response";
1154 return;
1155 }
1156
1157 uint32_t interface_index;
Wade Guthrie7347bf22013-04-30 11:21:51 -07001158 if (!msg.const_attributes()->GetU32AttributeValue(NL80211_ATTR_IFINDEX,
1159 &interface_index)) {
Paul Stewart2ddf2c62013-04-16 09:47:34 -07001160 LOG(ERROR) << "Message contains no interface index";
1161 return;
1162 }
1163 uint32_t interface_type;
Wade Guthrie7347bf22013-04-30 11:21:51 -07001164 if (!msg.const_attributes()->GetU32AttributeValue(NL80211_ATTR_IFTYPE,
1165 &interface_type)) {
Paul Stewart2ddf2c62013-04-16 09:47:34 -07001166 LOG(ERROR) << "Message contains no interface type";
1167 return;
1168 }
Paul Stewarta794cd62015-06-16 13:13:10 -07001169 const Info* info = GetInfo(interface_index);
Paul Stewart2ddf2c62013-04-16 09:47:34 -07001170 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 Chan0353c222014-09-05 08:20:59 -07001192 string address = base::StringToLowerASCII(info->mac_address.HexEncode());
Paul Stewart2ddf2c62013-04-16 09:47:34 -07001193 DeviceRefPtr device =
1194 new WiFi(control_interface_, dispatcher_, metrics_, manager_,
1195 info->name, address, interface_index);
1196 device->EnableIPv6Privacy();
1197 RegisterDevice(device);
1198}
Peter Qiu1a72f542015-04-14 16:31:36 -07001199#endif // DISABLE_WIFI
Paul Stewart2ddf2c62013-04-16 09:47:34 -07001200
Paul Stewarta794cd62015-06-16 13:13:10 -07001201bool DeviceInfo::SetHostname(const std::string& hostname) const {
Paul Stewart208a97e2015-05-13 09:11:12 -07001202 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 Stewart0af98bf2011-05-10 17:38:08 -07001210} // namespace shill