blob: e7240c50a0a2af842437ab00c849181d8e8df5c8 [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>
Paul Stewartbf1861b2011-08-23 15:45:35 -070024#include <base/file_util.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070025#include <base/memory/scoped_ptr.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050026#include <base/stl_util.h>
Paul Stewart2001a422011-12-15 10:20:09 -080027#include <base/string_number_conversions.h>
Chris Masone877ff982011-09-21 16:18:24 -070028#include <base/string_util.h>
Paul Stewartb50f0b92011-05-16 16:31:42 -070029#include <base/stringprintf.h>
Paul Stewart0af98bf2011-05-10 17:38:08 -070030
31#include "shill/control_interface.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070032#include "shill/device.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070033#include "shill/device_stub.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070034#include "shill/ethernet.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070035#include "shill/logging.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070036#include "shill/manager.h"
Paul Stewart2ddf2c62013-04-16 09:47:34 -070037#include "shill/netlink_attribute.h"
38#include "shill/netlink_manager.h"
39#include "shill/nl80211_message.h"
Paul Stewart8c116a92012-05-02 18:30:03 -070040#include "shill/routing_table.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070041#include "shill/rtnl_handler.h"
42#include "shill/rtnl_listener.h"
Chris Masone2aa97072011-08-09 17:35:08 -070043#include "shill/rtnl_message.h"
Chris Masone487b8bf2011-05-13 16:27:57 -070044#include "shill/service.h"
Gary Morain41780232012-07-31 15:08:31 -070045#include "shill/sockets.h"
mukesh agrawal93a29ed2012-04-17 16:13:01 -070046#include "shill/virtio_ethernet.h"
Darin Petkovc3505a52013-03-18 15:13:29 +010047#include "shill/vpn_provider.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070048#include "shill/wifi.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070049
Eric Shienbrood3e20a232012-02-16 11:35:56 -050050using base::Bind;
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080051using base::FilePath;
Thieu Le8f1c8352012-04-16 11:02:12 -070052using base::StringPrintf;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050053using base::Unretained;
Darin Petkove6193c02011-08-11 12:42:40 -070054using std::map;
Paul Stewart050cfc02012-07-06 20:38:54 -070055using std::set;
Paul Stewart0af98bf2011-05-10 17:38:08 -070056using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070057using std::vector;
Paul Stewart0af98bf2011-05-10 17:38:08 -070058
59namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070060
61// static
Jason Glasgowabc54032012-04-20 16:08:32 -040062const char DeviceInfo::kModemPseudoDeviceNamePrefix[] = "pseudomodem";
Christopher Wileye049cc52012-09-10 14:36:57 -070063const char DeviceInfo::kEthernetPseudoDeviceNamePrefix[] = "pseudoethernet";
Paul Stewartca876ee2012-04-21 08:55:58 -070064const char DeviceInfo::kDeviceInfoRoot[] = "/sys/class/net";
Ben Chan4e64d2d2012-05-16 00:02:25 -070065const char DeviceInfo::kDriverCdcEther[] = "cdc_ether";
Ben Chan4eb4ddf2013-06-20 22:16:56 -070066const char DeviceInfo::kDriverCdcNcm[] = "cdc_ncm";
Ben Chan4e64d2d2012-05-16 00:02:25 -070067const char DeviceInfo::kDriverGdmWiMax[] = "gdm_wimax";
mukesh agrawal93a29ed2012-04-17 16:13:01 -070068const char DeviceInfo::kDriverVirtioNet[] = "virtio_net";
Paul Stewartca876ee2012-04-21 08:55:58 -070069const char DeviceInfo::kInterfaceUevent[] = "uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080070const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Paul Stewartca876ee2012-04-21 08:55:58 -070071const char DeviceInfo::kInterfaceDevice[] = "device";
72const char DeviceInfo::kInterfaceDriver[] = "device/driver";
73const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags";
74const char DeviceInfo::kInterfaceType[] = "type";
Paul Stewartb50f0b92011-05-16 16:31:42 -070075const char *DeviceInfo::kModemDrivers[] = {
76 "gobi",
77 "QCUSBNet2k",
Ben Chan226d46a2012-10-11 00:22:17 -070078 "GobiNet",
Ben Chan0f90e0b2013-06-26 23:37:16 -070079 "cdc_mbim",
Ben Chan226d46a2012-10-11 00:22:17 -070080 "qmi_wwan"
Paul Stewartb50f0b92011-05-16 16:31:42 -070081};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080082const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewart050cfc02012-07-06 20:38:54 -070083const int DeviceInfo::kDelayedDeviceCreationSeconds = 5;
Ben Chanb061f892013-02-27 17:46:55 -080084const int DeviceInfo::kRequestLinkStatisticsIntervalMilliseconds = 20000;
Paul Stewartb50f0b92011-05-16 16:31:42 -070085
86DeviceInfo::DeviceInfo(ControlInterface *control_interface,
87 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080088 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070089 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070090 : control_interface_(control_interface),
91 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080092 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070093 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050094 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
95 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070096 link_listener_(NULL),
97 address_listener_(NULL),
Paul Stewartca876ee2012-04-21 08:55:58 -070098 device_info_root_(kDeviceInfoRoot),
Paul Stewart8c116a92012-05-02 18:30:03 -070099 routing_table_(RoutingTable::GetInstance()),
Gary Morain41780232012-07-31 15:08:31 -0700100 rtnl_handler_(RTNLHandler::GetInstance()),
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700101 netlink_manager_(NetlinkManager::GetInstance()),
Gary Morain41780232012-07-31 15:08:31 -0700102 sockets_(new Sockets()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -0700103}
104
Paul Stewarta3c56f92011-05-26 07:08:52 -0700105DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -0700106
mukesh agrawal8f317b62011-07-15 11:53:23 -0700107void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
108 black_list_.insert(device_name);
109}
110
Eric Shienbrood5e628a52012-03-21 16:56:59 -0400111bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
112 return ContainsKey(black_list_, device_name);
113}
114
Paul Stewarta3c56f92011-05-26 07:08:52 -0700115void DeviceInfo::Start() {
116 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500117 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700118 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500119 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700120 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
121 RTNLHandler::kRequestAddr);
Paul Stewart1ac4e842012-07-10 12:58:12 -0700122 request_link_statistics_callback_.Reset(
123 Bind(&DeviceInfo::RequestLinkStatistics, AsWeakPtr()));
124 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
Ben Chanb061f892013-02-27 17:46:55 -0800125 kRequestLinkStatisticsIntervalMilliseconds);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700126}
127
Paul Stewarta3c56f92011-05-26 07:08:52 -0700128void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700129 link_listener_.reset();
130 address_listener_.reset();
Paul Stewart8c116a92012-05-02 18:30:03 -0700131 infos_.clear();
Paul Stewart1ac4e842012-07-10 12:58:12 -0700132 request_link_statistics_callback_.Cancel();
Paul Stewart050cfc02012-07-06 20:38:54 -0700133 delayed_devices_callback_.Cancel();
134 delayed_devices_.clear();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700135}
136
Ben Chan5086b972013-01-15 21:51:38 -0800137vector<string> DeviceInfo::GetUninitializedTechnologies() const {
138 set<string> unique_technologies;
139 for (map<int, Info>::const_iterator it = infos_.begin(); it != infos_.end();
140 ++it) {
141 if (it->second.device)
142 continue;
143
144 Technology::Identifier technology = it->second.technology;
145 if (Technology::IsPrimaryConnectivityTechnology(technology))
146 unique_technologies.insert(Technology::NameFromIdentifier(technology));
147 }
148 return vector<string>(unique_technologies.begin(), unique_technologies.end());
149}
150
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700151void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700152 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
153 << device->interface_index() << ")";
Paul Stewart050cfc02012-07-06 20:38:54 -0700154 delayed_devices_.erase(device->interface_index());
Darin Petkove6193c02011-08-11 12:42:40 -0700155 CHECK(!GetDevice(device->interface_index()).get());
156 infos_[device->interface_index()].device = device;
Thieu Le9abd6742013-01-23 23:35:37 -0800157 if (metrics_->IsDeviceRegistered(device->interface_index(),
158 device->technology())) {
159 metrics_->NotifyDeviceInitialized(device->interface_index());
160 } else {
161 metrics_->RegisterDevice(device->interface_index(), device->technology());
162 }
Ben Chan5086b972013-01-15 21:51:38 -0800163 if (Technology::IsPrimaryConnectivityTechnology(device->technology())) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700164 manager_->RegisterDevice(device);
165 }
166}
167
Jason Glasgowe9089492012-02-23 17:57:37 -0500168void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
169 int interface_index = device->interface_index();
170
Ben Chanfad4a0b2012-04-18 15:49:59 -0700171 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
172 << interface_index << ")";
Joshua Krollda798622012-06-05 12:30:48 -0700173 CHECK((device->technology() == Technology::kCellular) ||
174 (device->technology() == Technology::kWiMax));
Jason Glasgowe9089492012-02-23 17:57:37 -0500175
176 // Release reference to the device
177 map<int, Info>::iterator iter = infos_.find(interface_index);
178 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700179 SLOG(Device, 2) << "Removing device from info for index: "
180 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500181 manager_->DeregisterDevice(device);
182 // Release the reference to the device, but maintain the mapping
183 // for the index. That will be cleaned up by an RTNL message.
184 iter->second.device = NULL;
185 }
Thieu Lec8078a62013-01-22 18:01:12 -0800186 metrics_->DeregisterDevice(device->interface_index());
Jason Glasgowe9089492012-02-23 17:57:37 -0500187}
188
Paul Stewartca876ee2012-04-21 08:55:58 -0700189FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name,
190 const string &path_name) {
191 return device_info_root_.Append(iface_name).Append(path_name);
192}
193
194bool DeviceInfo::GetDeviceInfoContents(const string &iface_name,
195 const string &path_name,
196 string *contents_out) {
197 return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
198 contents_out);
199}
200bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700201 const string &path_name,
202 FilePath *path_out) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700203 return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
204 path_out);
205}
206
Paul Stewartfdd16072011-09-16 12:41:35 -0700207Technology::Identifier DeviceInfo::GetDeviceTechnology(
208 const string &iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700209 string type_string;
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700210 int arp_type = ARPHRD_VOID;
Paul Stewartca876ee2012-04-21 08:55:58 -0700211 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
212 TrimString(type_string, "\n", &type_string) &&
213 !base::StringToInt(type_string, &arp_type)) {
214 arp_type = ARPHRD_VOID;
215 }
216
Paul Stewart9364c4c2011-12-06 17:12:42 -0800217 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700218 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Paul Stewart050cfc02012-07-06 20:38:54 -0700219 LOG(INFO) << StringPrintf("%s: device %s has no uevent file",
220 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700221 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800222 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700223
Thieu Le8f1c8352012-04-16 11:02:12 -0700224 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
225 // start of the file or after a newline, we can safely assume this
226 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800227 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700228 SLOG(Device, 2)
229 << StringPrintf("%s: device %s has wifi signature in uevent file",
230 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700231 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700232 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
233 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800234 return Technology::kWiFiMonitor;
235 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700236 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700237 }
238
Jason Glasgow7904d142012-05-18 13:36:20 -0400239 // Special case for pseudo modems which are used for testing
240 if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) {
241 SLOG(Device, 2) << StringPrintf(
242 "%s: device %s is a pseudo modem for testing",
243 __func__, iface_name.c_str());
244 return Technology::kCellular;
245 }
246
Christopher Wileye049cc52012-09-10 14:36:57 -0700247 // Special case for pseudo ethernet devices which are used for testing.
248 if (iface_name.find(kEthernetPseudoDeviceNamePrefix) == 0) {
249 SLOG(Device, 2) << StringPrintf(
250 "%s: device %s is a virtual ethernet device for testing",
251 __func__, iface_name.c_str());
252 return Technology::kEthernet;
253 }
254
Paul Stewart9364c4c2011-12-06 17:12:42 -0800255 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700256 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700257 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
258 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700259 if (arp_type == ARPHRD_LOOPBACK) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700260 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
261 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700262 return Technology::kLoopback;
263 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700264 if (arp_type == ARPHRD_PPP) {
265 SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device",
266 __func__, iface_name.c_str());
267 return Technology::kPPP;
268 }
269 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800270 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700271 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
272 TrimString(tun_flags_str, "\n", &tun_flags_str) &&
273 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800274 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700275 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
276 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800277 return Technology::kTunnel;
278 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700279 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800280 }
281
282 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700283 // See if driver for this interface is in a list of known modem driver names.
284 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
285 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800286 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700287 SLOG(Device, 2)
288 << StringPrintf("%s: device %s is matched with modem driver %s",
289 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800290 return Technology::kCellular;
291 }
292 }
293
Ben Chan4e64d2d2012-05-16 00:02:25 -0700294 if (driver_name == kDriverGdmWiMax) {
295 SLOG(Device, 2) << StringPrintf("%s: device %s is a WiMAX device",
296 __func__, iface_name.c_str());
297 return Technology::kWiMax;
298 }
299
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700300 // For cdc_ether / cdc_ncm devices, make sure it's a modem because this driver
Thieu Le8f1c8352012-04-16 11:02:12 -0700301 // can be used for other ethernet devices.
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700302 if (driver_name == kDriverCdcEther || driver_name == kDriverCdcNcm) {
303 if (IsCdcEthernetModemDevice(iface_name)) {
304 LOG(INFO) << StringPrintf("%s: device %s is a %s modem device", __func__,
mukesh agrawal35ec8402013-07-19 14:49:08 -0700305 iface_name.c_str(), driver_name.c_str());
Paul Stewart050cfc02012-07-06 20:38:54 -0700306 return Technology::kCellular;
307 }
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700308 SLOG(Device, 2) << StringPrintf("%s: device %s is a %s device", __func__,
mukesh agrawal35ec8402013-07-19 14:49:08 -0700309 iface_name.c_str(), driver_name.c_str());
Paul Stewart050cfc02012-07-06 20:38:54 -0700310 return Technology::kCDCEthernet;
Thieu Le8f1c8352012-04-16 11:02:12 -0700311 }
312
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700313 // Special case for the virtio driver, used when run under KVM. See also
314 // the comment in VirtioEthernet::Start.
315 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700316 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
317 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700318 return Technology::kVirtioEthernet;
319 }
320
Ben Chanfad4a0b2012-04-18 15:49:59 -0700321 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
322 "is defaulted to type ethernet",
323 __func__, iface_name.c_str(),
324 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700325 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700326}
327
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700328bool DeviceInfo::IsCdcEthernetModemDevice(const std::string &iface_name) {
329 // A cdc_ether / cdc_ncm device is a modem device if it also exposes tty
330 // interfaces. To determine this, we look for the existence of the tty
331 // interface in the USB device sysfs tree.
Thieu Le8f1c8352012-04-16 11:02:12 -0700332 //
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700333 // A typical sysfs dir hierarchy for a cdc_ether / cdc_ncm modem USB device is
334 // as follows:
Thieu Le8f1c8352012-04-16 11:02:12 -0700335 //
336 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
337 // 1-2:1.0
338 // tty
339 // ttyACM0
340 // 1-2:1.1
341 // net
342 // usb0
343 // 1-2:1.2
344 // tty
345 // ttyACM1
346 // ...
347 //
348 // /sys/class/net/usb0/device symlinks to
349 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700350 //
351 // Note that some modem devices have the tty directory one level deeper
352 // (eg. E362), so the device tree for the tty interface is:
353 // /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 -0700354
Paul Stewartca876ee2012-04-21 08:55:58 -0700355 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700356 FilePath device_path;
357 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700358 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
359 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700360 return false;
361 }
362 if (!device_path.IsAbsolute()) {
363 device_path = device_file.DirName().Append(device_path);
364 file_util::AbsolutePath(&device_path);
365 }
366
367 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700368 // USB device and see if there's a subdirectory "tty" inside. In other
369 // words, using the example dir hierarchy above, find
370 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700371 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700372 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700373}
374
375// static
Thieu Leb27beee2012-04-20 09:19:06 -0700376bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
377 file_util::FileEnumerator::FileType type =
378 static_cast<file_util::FileEnumerator::FileType>(
379 file_util::FileEnumerator::DIRECTORIES |
380 file_util::FileEnumerator::SHOW_SYM_LINKS);
381 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700382 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
383 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700384 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700385 return true;
386 }
387 return false;
388}
389
Paul Stewart8c116a92012-05-02 18:30:03 -0700390DeviceRefPtr DeviceInfo::CreateDevice(const string &link_name,
391 const string &address,
392 int interface_index,
393 Technology::Identifier technology) {
394 DeviceRefPtr device;
Paul Stewart050cfc02012-07-06 20:38:54 -0700395 delayed_devices_.erase(interface_index);
Ben Chan5086b972013-01-15 21:51:38 -0800396 infos_[interface_index].technology = technology;
Paul Stewart8c116a92012-05-02 18:30:03 -0700397
398 switch (technology) {
399 case Technology::kCellular:
400 // Cellular devices are managed by ModemInfo.
401 SLOG(Device, 2) << "Cellular link " << link_name
402 << " at index " << interface_index
403 << " -- notifying ModemInfo.";
Gary Morainbf74a672012-07-30 16:27:19 -0700404
405 // The MAC address provided by RTNL is not reliable for Gobi 2K modems.
Ben Chan4b285862012-10-10 22:52:16 -0700406 // Clear it here, and it will be fetched from the kernel in
407 // GetMACAddress().
Gary Morainbf74a672012-07-30 16:27:19 -0700408 infos_[interface_index].mac_address.Clear();
Paul Stewart8c116a92012-05-02 18:30:03 -0700409 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
410 break;
411 case Technology::kEthernet:
412 device = new Ethernet(control_interface_, dispatcher_, metrics_,
413 manager_, link_name, address, interface_index);
414 device->EnableIPv6Privacy();
415 break;
416 case Technology::kVirtioEthernet:
417 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
418 manager_, link_name, address,
419 interface_index);
420 device->EnableIPv6Privacy();
421 break;
422 case Technology::kWifi:
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700423 // Defer creating this device until we get information about the
424 // type of WiFi interface.
425 GetWiFiInterfaceInfo(interface_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700426 break;
Ben Chan4e64d2d2012-05-16 00:02:25 -0700427 case Technology::kWiMax:
Darin Petkove4b27022012-05-16 13:28:50 +0200428 // WiMax devices are managed by WiMaxProvider.
429 SLOG(Device, 2) << "WiMax link " << link_name
430 << " at index " << interface_index
431 << " -- notifying WiMaxProvider.";
Ben Chan4b285862012-10-10 22:52:16 -0700432 // The MAC address provided by RTNL may not be the final value as the
433 // WiMAX device may change the address after initialization. Clear it
434 // here, and it will be fetched from the kernel when
435 // WiMaxProvider::CreateDevice() is called after the WiMAX device DBus
436 // object is created by the WiMAX manager daemon.
437 infos_[interface_index].mac_address.Clear();
Darin Petkove4b27022012-05-16 13:28:50 +0200438 manager_->wimax_provider()->OnDeviceInfoAvailable(link_name);
Ben Chan4e64d2d2012-05-16 00:02:25 -0700439 break;
Paul Stewart8c116a92012-05-02 18:30:03 -0700440 case Technology::kPPP:
441 case Technology::kTunnel:
442 // Tunnel and PPP devices are managed by the VPN code (PPP for
443 // l2tpipsec). Notify the VPN Provider of the interface's presence.
444 // Since CreateDevice is only called once in the lifetime of an
445 // interface index, this notification will only occur the first
446 // time the device is seen.
447 SLOG(Device, 2) << "Tunnel / PPP link " << link_name
448 << " at index " << interface_index
449 << " -- notifying VPNProvider.";
450 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
451 interface_index) &&
452 technology == Technology::kTunnel) {
453 // If VPN does not know anything about this tunnel, it is probably
454 // left over from a previous instance and should not exist.
455 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
456 DeleteInterface(interface_index);
457 }
458 break;
459 case Technology::kLoopback:
460 // Loopback devices are largely ignored, but we should make sure the
461 // link is enabled.
462 SLOG(Device, 2) << "Bringing up loopback device " << link_name
463 << " at index " << interface_index;
464 rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP);
465 return NULL;
Paul Stewart050cfc02012-07-06 20:38:54 -0700466 case Technology::kCDCEthernet:
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700467 // CDCEthernet devices are of indeterminate type when they are
Paul Stewart050cfc02012-07-06 20:38:54 -0700468 // initially created. Some time later, tty devices may or may
469 // not appear under the same USB device root, which will identify
470 // it as a modem. Alternatively, ModemManager may discover the
471 // device and create and register a Cellular device. In either
472 // case, we should delay creating a Device until we can make a
473 // better determination of what type this Device should be.
474 LOG(INFO) << "Delaying creation of device for " << link_name
475 << " at index " << interface_index;
476 DelayDeviceCreation(interface_index);
477 return NULL;
Paul Stewart8c116a92012-05-02 18:30:03 -0700478 default:
479 // We will not manage this device in shill. Do not create a device
480 // object or do anything to change its state. We create a stub object
481 // which is useful for testing.
482 return new DeviceStub(control_interface_, dispatcher_, metrics_,
483 manager_, link_name, address, interface_index,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700484 technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700485 }
486
487 // Reset the routing table and addresses.
488 routing_table_->FlushRoutes(interface_index);
489 FlushAddresses(interface_index);
490
Ben Chan5086b972013-01-15 21:51:38 -0800491 manager_->UpdateUninitializedTechnologies();
492
Paul Stewart8c116a92012-05-02 18:30:03 -0700493 return device;
494}
495
Chris Masone2aa97072011-08-09 17:35:08 -0700496void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700497 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
498 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700499 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700500 Technology::Identifier technology = Technology::kUnknown;
Darin Petkove6193c02011-08-11 12:42:40 -0700501 unsigned int flags = msg.link_status().flags;
502 unsigned int change = msg.link_status().change;
Paul Stewart8c116a92012-05-02 18:30:03 -0700503 bool new_device =
504 !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700505 SLOG(Device, 2) << __func__ << "(index=" << dev_index
506 << std::showbase << std::hex
507 << ", flags=" << flags << ", change=" << change << ")"
508 << std::dec << std::noshowbase
509 << ", new_device=" << new_device;
Paul Stewart8c116a92012-05-02 18:30:03 -0700510 infos_[dev_index].has_addresses_only = false;
Darin Petkove6193c02011-08-11 12:42:40 -0700511 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700512
Paul Stewart1ac4e842012-07-10 12:58:12 -0700513 RetrieveLinkStatistics(dev_index, msg);
514
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700515 DeviceRefPtr device = GetDevice(dev_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700516 if (new_device) {
517 CHECK(!device);
Chris Masone2aa97072011-08-09 17:35:08 -0700518 if (!msg.HasAttribute(IFLA_IFNAME)) {
519 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
520 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700521 }
Chris Masone2aa97072011-08-09 17:35:08 -0700522 ByteString b(msg.GetAttribute(IFLA_IFNAME));
523 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700524 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200525 infos_[dev_index].name = link_name;
526 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700527
Chris Masone2aa97072011-08-09 17:35:08 -0700528 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700529 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700530 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700531 } else {
532 technology = GetDeviceTechnology(link_name);
533 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700534 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800535 string address;
536 if (msg.HasAttribute(IFLA_ADDRESS)) {
537 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
538 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700539 SLOG(Device, 2) << "link index " << dev_index << " address "
540 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700541 } else if (technology != Technology::kTunnel &&
542 technology != Technology::kPPP) {
mukesh agrawal6813e762013-07-10 19:05:08 -0700543 LOG(ERROR) << "Add Link message for link '" << link_name
544 << "' does not have IFLA_ADDRESS!";
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800545 return;
546 }
Thieu Lec8078a62013-01-22 18:01:12 -0800547 metrics_->RegisterDevice(dev_index, technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700548 device = CreateDevice(link_name, address, dev_index, technology);
549 if (device) {
550 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700551 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700552 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700553 if (device) {
554 device->LinkEvent(flags, change);
555 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700556}
557
Chris Masone2aa97072011-08-09 17:35:08 -0700558void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700559 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700560
Paul Stewart9a908082011-08-31 12:18:48 -0700561 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
562 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700563 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
564 << std::showbase << std::hex
565 << ", flags=" << msg.link_status().flags
566 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700567 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700568}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700569
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700570DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
571 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700572 return info ? info->device : NULL;
573}
574
Darin Petkovf8046b82012-04-24 16:29:23 +0200575int DeviceInfo::GetIndex(const string &interface_name) const {
576 map<string, int>::const_iterator it = indices_.find(interface_name);
577 return it == indices_.end() ? -1 : it->second;
578}
579
Paul Stewart32852962011-08-30 14:06:53 -0700580bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700581 const Info *info = GetInfo(interface_index);
582 if (!info) {
583 return false;
584 }
Gary Morainbf74a672012-07-30 16:27:19 -0700585 // |mac_address| from RTNL is not used for some devices, in which case it will
586 // be empty here.
587 if (!info->mac_address.IsEmpty()) {
588 *address = info->mac_address;
589 return true;
590 }
591
592 // Ask the kernel for the MAC address.
593 *address = GetMACAddressFromKernel(interface_index);
594 return !address->IsEmpty();
595}
596
597ByteString DeviceInfo::GetMACAddressFromKernel(int interface_index) const {
Gary Morain41780232012-07-31 15:08:31 -0700598 const Info *info = GetInfo(interface_index);
599 if (!info) {
600 return ByteString();
601 }
602
603 const int fd = sockets_->Socket(PF_INET, SOCK_DGRAM, 0);
Gary Morainbf74a672012-07-30 16:27:19 -0700604 if (fd < 0) {
605 LOG(ERROR) << __func__ << ": Unable to open socket: " << fd;
Gary Morain41780232012-07-31 15:08:31 -0700606 return ByteString();
Gary Morainbf74a672012-07-30 16:27:19 -0700607 }
Gary Morain41780232012-07-31 15:08:31 -0700608
609 ScopedSocketCloser socket_closer(sockets_.get(), fd);
Gary Morainbf74a672012-07-30 16:27:19 -0700610 struct ifreq ifr;
611 memset(&ifr, 0, sizeof(ifr));
612 ifr.ifr_ifindex = interface_index;
Gary Morain41780232012-07-31 15:08:31 -0700613 strcpy(ifr.ifr_ifrn.ifrn_name, info->name.c_str());
614 int err = sockets_->Ioctl(fd, SIOCGIFHWADDR, &ifr);
Gary Morainbf74a672012-07-30 16:27:19 -0700615 if (err < 0) {
616 LOG(ERROR) << __func__ << ": Unable to read MAC address: " << errno;
Gary Morain41780232012-07-31 15:08:31 -0700617 return ByteString();
Gary Morainbf74a672012-07-30 16:27:19 -0700618 }
Gary Morain41780232012-07-31 15:08:31 -0700619
620 return ByteString(ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700621}
622
Paul Stewart9a908082011-08-31 12:18:48 -0700623bool DeviceInfo::GetAddresses(int interface_index,
624 vector<AddressData> *addresses) const {
625 const Info *info = GetInfo(interface_index);
626 if (!info) {
627 return false;
628 }
629 *addresses = info->ip_addresses;
630 return true;
631}
632
633void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700634 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700635 const Info *info = GetInfo(interface_index);
636 if (!info) {
637 return;
638 }
639 const vector<AddressData> &addresses = info->ip_addresses;
640 vector<AddressData>::const_iterator iter;
641 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700642 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700643 (iter->scope == RT_SCOPE_UNIVERSE &&
644 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700645 SLOG(Device, 2) << __func__ << ": removing ip address "
646 << iter->address.ToString()
647 << " from interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700648 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
649 }
650 }
651}
652
Paul Stewart05a42c22012-08-02 16:47:21 -0700653bool DeviceInfo::HasOtherAddress(
654 int interface_index, const IPAddress &this_address) const {
655 SLOG(Device, 3) << __func__ << "(" << interface_index << ")";
656 const Info *info = GetInfo(interface_index);
657 if (!info) {
658 return false;
659 }
660 const vector<AddressData> &addresses = info->ip_addresses;
661 vector<AddressData>::const_iterator iter;
662 bool has_other_address = false;
663 bool has_this_address = false;
664 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
665 if (iter->address.family() != this_address.family()) {
666 continue;
667 }
668 if (iter->address.address().Equals(this_address.address())) {
669 has_this_address = true;
670 } else if (this_address.family() == IPAddress::kFamilyIPv4) {
671 has_other_address = true;
672 } else if ((iter->scope == RT_SCOPE_UNIVERSE &&
673 (iter->flags & IFA_F_TEMPORARY) == 0)) {
674 has_other_address = true;
675 }
676 }
677 return has_other_address && !has_this_address;
678}
679
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700680bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
681 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700682 if (!info) {
683 return false;
684 }
685 *flags = info->flags;
686 return true;
687}
688
Paul Stewart1ac4e842012-07-10 12:58:12 -0700689bool DeviceInfo::GetByteCounts(int interface_index,
690 uint64 *rx_bytes,
691 uint64 *tx_bytes) const {
692 const Info *info = GetInfo(interface_index);
693 if (!info) {
694 return false;
695 }
696 *rx_bytes = info->rx_bytes;
697 *tx_bytes = info->tx_bytes;
698 return true;
699}
700
Paul Stewartca6abd42012-03-01 15:45:29 -0800701bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800702 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
703 if (fd < 0) {
704 PLOG(ERROR) << "failed to open " << kTunDeviceName;
705 return false;
706 }
707 file_util::ScopedFD scoped_fd(&fd);
708
709 struct ifreq ifr;
710 memset(&ifr, 0, sizeof(ifr));
711 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
712 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
713 PLOG(ERROR) << "failed to create tunnel interface";
714 return false;
715 }
716
717 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
718 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
719 return false;
720 }
721
722 *interface_name = string(ifr.ifr_name);
723
724 return true;
725}
726
Paul Stewartca6abd42012-03-01 15:45:29 -0800727bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800728 return rtnl_handler_->RemoveInterface(interface_index);
729}
730
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700731const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
732 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700733 if (iter == infos_.end()) {
734 return NULL;
735 }
736 return &iter->second;
737}
738
739void DeviceInfo::RemoveInfo(int interface_index) {
740 map<int, Info>::iterator iter = infos_.find(interface_index);
741 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700742 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700743 if (iter->second.device.get()) {
744 manager_->DeregisterDevice(iter->second.device);
745 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200746 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700747 infos_.erase(iter);
Paul Stewart050cfc02012-07-06 20:38:54 -0700748 delayed_devices_.erase(interface_index);
mukesh agrawal47009f82011-08-25 14:07:35 -0700749 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700750 SLOG(Device, 2) << __func__ << ": Unknown device index: "
751 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700752 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700753}
754
Chris Masone2aa97072011-08-09 17:35:08 -0700755void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700756 DCHECK(msg.type() == RTNLMessage::kTypeLink);
757 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700758 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700759 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700760 DelLinkMsgHandler(msg);
761 } else {
762 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700763 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700764}
765
Paul Stewart9a908082011-08-31 12:18:48 -0700766void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700767 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700768 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
769 int interface_index = msg.interface_index();
770 if (!ContainsKey(infos_, interface_index)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700771 SLOG(Device, 2) << "Got advance address information for unknown index "
772 << interface_index;
773 infos_[interface_index].has_addresses_only = true;
Paul Stewart9a908082011-08-31 12:18:48 -0700774 }
775 const RTNLMessage::AddressStatus &status = msg.address_status();
776 IPAddress address(msg.family(),
Ben Chan682c5d42012-06-18 14:11:04 -0700777 msg.HasAttribute(IFA_LOCAL) ?
778 msg.GetAttribute(IFA_LOCAL) : msg.GetAttribute(IFA_ADDRESS),
Paul Stewart9a908082011-08-31 12:18:48 -0700779 status.prefix_len);
780
Ben Chan682c5d42012-06-18 14:11:04 -0700781 SLOG_IF(Device, 2, msg.HasAttribute(IFA_LOCAL))
782 << "Found local address attribute for interface " << interface_index;
783
Paul Stewart9a908082011-08-31 12:18:48 -0700784 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
785 vector<AddressData>::iterator iter;
786 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
787 if (address.Equals(iter->address)) {
788 break;
789 }
790 }
791 if (iter != address_list.end()) {
792 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700793 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700794 address_list.erase(iter);
795 } else {
796 iter->flags = status.flags;
797 iter->scope = status.scope;
798 }
799 } else if (msg.mode() == RTNLMessage::kModeAdd) {
800 address_list.push_back(AddressData(address, status.flags, status.scope));
Paul Stewart8c116a92012-05-02 18:30:03 -0700801 SLOG(Device, 2) << "Add address " << address.ToString()
802 << " for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700803 }
804}
805
Paul Stewart050cfc02012-07-06 20:38:54 -0700806void DeviceInfo::DelayDeviceCreation(int interface_index) {
807 delayed_devices_.insert(interface_index);
808 delayed_devices_callback_.Reset(
809 Bind(&DeviceInfo::DelayedDeviceCreationTask, AsWeakPtr()));
810 dispatcher_->PostDelayedTask(delayed_devices_callback_.callback(),
811 kDelayedDeviceCreationSeconds * 1000);
812}
813
814// Re-evaluate the technology type for each delayed device.
815void DeviceInfo::DelayedDeviceCreationTask() {
816 while (!delayed_devices_.empty()) {
817 set<int>::iterator it = delayed_devices_.begin();
818 int dev_index = *it;
819 delayed_devices_.erase(it);
820
821 DCHECK(ContainsKey(infos_, dev_index));
822 DCHECK(!GetDevice(dev_index));
823
824 const string &link_name = infos_[dev_index].name;
825 Technology::Identifier technology = GetDeviceTechnology(link_name);
826
827 if (technology == Technology::kCDCEthernet) {
828 LOG(INFO) << "In " << __func__ << ": device " << link_name
829 << " is now assumed to be regular Ethernet.";
830 technology = Technology::kEthernet;
831 } else if (technology != Technology::kCellular) {
832 LOG(WARNING) << "In " << __func__ << ": device " << link_name
833 << " is unexpected technology "
834 << Technology::NameFromIdentifier(technology);
835 }
836 string address =
837 StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
838 DCHECK(!address.empty());
839
840 DeviceRefPtr device = CreateDevice(link_name, address, dev_index,
841 technology);
842 if (device) {
843 RegisterDevice(device);
844 }
845 }
846}
847
Paul Stewart1ac4e842012-07-10 12:58:12 -0700848void DeviceInfo::RetrieveLinkStatistics(int interface_index,
849 const RTNLMessage &msg) {
850 if (!msg.HasAttribute(IFLA_STATS64)) {
851 return;
852 }
853 ByteString stats_bytes(msg.GetAttribute(IFLA_STATS64));
854 struct rtnl_link_stats64 stats;
855 if (stats_bytes.GetLength() < sizeof(stats)) {
856 LOG(WARNING) << "Link statistics size is too small: "
857 << stats_bytes.GetLength() << " < " << sizeof(stats);
858 return;
859 }
860
861 memcpy(&stats, stats_bytes.GetConstData(), sizeof(stats));
862 SLOG(Device, 2) << "Link statistics for "
863 << " interface index " << interface_index << ": "
864 << "receive: " << stats.rx_bytes << "; "
865 << "transmit: " << stats.tx_bytes << ".";
866 infos_[interface_index].rx_bytes = stats.rx_bytes;
867 infos_[interface_index].tx_bytes = stats.tx_bytes;
868}
869
870void DeviceInfo::RequestLinkStatistics() {
871 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink);
872 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
Ben Chanb061f892013-02-27 17:46:55 -0800873 kRequestLinkStatisticsIntervalMilliseconds);
Paul Stewart1ac4e842012-07-10 12:58:12 -0700874}
875
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700876void DeviceInfo::GetWiFiInterfaceInfo(int interface_index) {
877 GetInterfaceMessage msg;
878 if (!msg.attributes()->SetU32AttributeValue(NL80211_ATTR_IFINDEX,
879 interface_index)) {
880 LOG(ERROR) << "Unable to set interface index attribute for "
881 "GetInterface message. Interface type cannot be "
882 "determined!";
883 return;
884 }
Wade Guthrie7347bf22013-04-30 11:21:51 -0700885 netlink_manager_->SendNl80211Message(
886 &msg,
887 Bind(&DeviceInfo::OnWiFiInterfaceInfoReceived, AsWeakPtr()),
888 Bind(&NetlinkManager::OnNetlinkMessageError));
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700889}
890
Wade Guthrie7347bf22013-04-30 11:21:51 -0700891void DeviceInfo::OnWiFiInterfaceInfoReceived(const Nl80211Message &msg) {
892 if (msg.command() != NL80211_CMD_NEW_INTERFACE) {
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700893 LOG(ERROR) << "Message is not a new interface response";
894 return;
895 }
896
897 uint32_t interface_index;
Wade Guthrie7347bf22013-04-30 11:21:51 -0700898 if (!msg.const_attributes()->GetU32AttributeValue(NL80211_ATTR_IFINDEX,
899 &interface_index)) {
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700900 LOG(ERROR) << "Message contains no interface index";
901 return;
902 }
903 uint32_t interface_type;
Wade Guthrie7347bf22013-04-30 11:21:51 -0700904 if (!msg.const_attributes()->GetU32AttributeValue(NL80211_ATTR_IFTYPE,
905 &interface_type)) {
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700906 LOG(ERROR) << "Message contains no interface type";
907 return;
908 }
909 const Info *info = GetInfo(interface_index);
910 if (!info) {
911 LOG(ERROR) << "Could not find device info for interface index "
912 << interface_index;
913 return;
914 }
915 if (info->device) {
916 LOG(ERROR) << "Device already created for interface index "
917 << interface_index;
918 return;
919 }
920 if (interface_type != NL80211_IFTYPE_STATION) {
921 LOG(INFO) << "Ignoring WiFi device "
922 << info->name
923 << " at interface index "
924 << interface_index
925 << " since it is not in station mode.";
926 return;
927 }
928 LOG(INFO) << "Creating WiFi device for station mode interface "
929 << info->name
930 << " at interface index "
931 << interface_index;
932 string address = StringToLowerASCII(info->mac_address.HexEncode());
933 DeviceRefPtr device =
934 new WiFi(control_interface_, dispatcher_, metrics_, manager_,
935 info->name, address, interface_index);
936 device->EnableIPv6Privacy();
937 RegisterDevice(device);
938}
939
Paul Stewart0af98bf2011-05-10 17:38:08 -0700940} // namespace shill