blob: 6b2fc15903b4f040c3897cf45a287c0cab3423ff [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;
Arman Ugurayb00c13d2013-07-29 18:20:52 -0700139 set<Technology::Identifier> initialized_technologies;
Ben Chan5086b972013-01-15 21:51:38 -0800140 for (map<int, Info>::const_iterator it = infos_.begin(); it != infos_.end();
141 ++it) {
Ben Chan5086b972013-01-15 21:51:38 -0800142 Technology::Identifier technology = it->second.technology;
Arman Ugurayb00c13d2013-07-29 18:20:52 -0700143 if (it->second.device) {
144 // If there is more than one device for a technology and at least
145 // one of them has been initialized, make sure that it doesn't get
146 // listed as uninitialized.
147 initialized_technologies.insert(technology);
148 unique_technologies.erase(Technology::NameFromIdentifier(technology));
149 continue;
150 }
151 if (Technology::IsPrimaryConnectivityTechnology(technology) &&
152 !ContainsKey(initialized_technologies, technology))
Ben Chan5086b972013-01-15 21:51:38 -0800153 unique_technologies.insert(Technology::NameFromIdentifier(technology));
154 }
155 return vector<string>(unique_technologies.begin(), unique_technologies.end());
156}
157
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700158void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700159 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
160 << device->interface_index() << ")";
Paul Stewart050cfc02012-07-06 20:38:54 -0700161 delayed_devices_.erase(device->interface_index());
Darin Petkove6193c02011-08-11 12:42:40 -0700162 CHECK(!GetDevice(device->interface_index()).get());
163 infos_[device->interface_index()].device = device;
Thieu Le9abd6742013-01-23 23:35:37 -0800164 if (metrics_->IsDeviceRegistered(device->interface_index(),
165 device->technology())) {
166 metrics_->NotifyDeviceInitialized(device->interface_index());
167 } else {
168 metrics_->RegisterDevice(device->interface_index(), device->technology());
169 }
Ben Chan5086b972013-01-15 21:51:38 -0800170 if (Technology::IsPrimaryConnectivityTechnology(device->technology())) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700171 manager_->RegisterDevice(device);
172 }
173}
174
Jason Glasgowe9089492012-02-23 17:57:37 -0500175void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
176 int interface_index = device->interface_index();
177
Ben Chanfad4a0b2012-04-18 15:49:59 -0700178 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
179 << interface_index << ")";
Joshua Krollda798622012-06-05 12:30:48 -0700180 CHECK((device->technology() == Technology::kCellular) ||
181 (device->technology() == Technology::kWiMax));
Jason Glasgowe9089492012-02-23 17:57:37 -0500182
183 // Release reference to the device
184 map<int, Info>::iterator iter = infos_.find(interface_index);
185 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700186 SLOG(Device, 2) << "Removing device from info for index: "
187 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500188 manager_->DeregisterDevice(device);
189 // Release the reference to the device, but maintain the mapping
190 // for the index. That will be cleaned up by an RTNL message.
191 iter->second.device = NULL;
192 }
Thieu Lec8078a62013-01-22 18:01:12 -0800193 metrics_->DeregisterDevice(device->interface_index());
Jason Glasgowe9089492012-02-23 17:57:37 -0500194}
195
Paul Stewartca876ee2012-04-21 08:55:58 -0700196FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name,
197 const string &path_name) {
198 return device_info_root_.Append(iface_name).Append(path_name);
199}
200
201bool DeviceInfo::GetDeviceInfoContents(const string &iface_name,
202 const string &path_name,
203 string *contents_out) {
204 return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
205 contents_out);
206}
207bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700208 const string &path_name,
209 FilePath *path_out) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700210 return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
211 path_out);
212}
213
Paul Stewartfdd16072011-09-16 12:41:35 -0700214Technology::Identifier DeviceInfo::GetDeviceTechnology(
215 const string &iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700216 string type_string;
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700217 int arp_type = ARPHRD_VOID;
Paul Stewartca876ee2012-04-21 08:55:58 -0700218 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
219 TrimString(type_string, "\n", &type_string) &&
220 !base::StringToInt(type_string, &arp_type)) {
221 arp_type = ARPHRD_VOID;
222 }
223
Paul Stewart9364c4c2011-12-06 17:12:42 -0800224 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700225 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Paul Stewart050cfc02012-07-06 20:38:54 -0700226 LOG(INFO) << StringPrintf("%s: device %s has no uevent file",
227 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700228 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800229 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700230
Thieu Le8f1c8352012-04-16 11:02:12 -0700231 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
232 // start of the file or after a newline, we can safely assume this
233 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800234 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700235 SLOG(Device, 2)
236 << StringPrintf("%s: device %s has wifi signature in uevent file",
237 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700238 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700239 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
240 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800241 return Technology::kWiFiMonitor;
242 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700243 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700244 }
245
Jason Glasgow7904d142012-05-18 13:36:20 -0400246 // Special case for pseudo modems which are used for testing
247 if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) {
248 SLOG(Device, 2) << StringPrintf(
249 "%s: device %s is a pseudo modem for testing",
250 __func__, iface_name.c_str());
251 return Technology::kCellular;
252 }
253
Christopher Wileye049cc52012-09-10 14:36:57 -0700254 // Special case for pseudo ethernet devices which are used for testing.
255 if (iface_name.find(kEthernetPseudoDeviceNamePrefix) == 0) {
256 SLOG(Device, 2) << StringPrintf(
257 "%s: device %s is a virtual ethernet device for testing",
258 __func__, iface_name.c_str());
259 return Technology::kEthernet;
260 }
261
Paul Stewart9364c4c2011-12-06 17:12:42 -0800262 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700263 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700264 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
265 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700266 if (arp_type == ARPHRD_LOOPBACK) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700267 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
268 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700269 return Technology::kLoopback;
270 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700271 if (arp_type == ARPHRD_PPP) {
272 SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device",
273 __func__, iface_name.c_str());
274 return Technology::kPPP;
275 }
276 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800277 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700278 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
279 TrimString(tun_flags_str, "\n", &tun_flags_str) &&
280 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800281 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700282 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
283 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800284 return Technology::kTunnel;
285 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700286 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800287 }
288
289 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700290 // See if driver for this interface is in a list of known modem driver names.
291 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
292 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800293 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700294 SLOG(Device, 2)
295 << StringPrintf("%s: device %s is matched with modem driver %s",
296 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800297 return Technology::kCellular;
298 }
299 }
300
Ben Chan4e64d2d2012-05-16 00:02:25 -0700301 if (driver_name == kDriverGdmWiMax) {
302 SLOG(Device, 2) << StringPrintf("%s: device %s is a WiMAX device",
303 __func__, iface_name.c_str());
304 return Technology::kWiMax;
305 }
306
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700307 // For cdc_ether / cdc_ncm devices, make sure it's a modem because this driver
Thieu Le8f1c8352012-04-16 11:02:12 -0700308 // can be used for other ethernet devices.
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700309 if (driver_name == kDriverCdcEther || driver_name == kDriverCdcNcm) {
310 if (IsCdcEthernetModemDevice(iface_name)) {
311 LOG(INFO) << StringPrintf("%s: device %s is a %s modem device", __func__,
mukesh agrawal35ec8402013-07-19 14:49:08 -0700312 iface_name.c_str(), driver_name.c_str());
Paul Stewart050cfc02012-07-06 20:38:54 -0700313 return Technology::kCellular;
314 }
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700315 SLOG(Device, 2) << StringPrintf("%s: device %s is a %s device", __func__,
mukesh agrawal35ec8402013-07-19 14:49:08 -0700316 iface_name.c_str(), driver_name.c_str());
Paul Stewart050cfc02012-07-06 20:38:54 -0700317 return Technology::kCDCEthernet;
Thieu Le8f1c8352012-04-16 11:02:12 -0700318 }
319
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700320 // Special case for the virtio driver, used when run under KVM. See also
321 // the comment in VirtioEthernet::Start.
322 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700323 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
324 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700325 return Technology::kVirtioEthernet;
326 }
327
Ben Chanfad4a0b2012-04-18 15:49:59 -0700328 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
329 "is defaulted to type ethernet",
330 __func__, iface_name.c_str(),
331 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700332 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700333}
334
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700335bool DeviceInfo::IsCdcEthernetModemDevice(const std::string &iface_name) {
336 // A cdc_ether / cdc_ncm device is a modem device if it also exposes tty
337 // interfaces. To determine this, we look for the existence of the tty
338 // interface in the USB device sysfs tree.
Thieu Le8f1c8352012-04-16 11:02:12 -0700339 //
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700340 // A typical sysfs dir hierarchy for a cdc_ether / cdc_ncm modem USB device is
341 // as follows:
Thieu Le8f1c8352012-04-16 11:02:12 -0700342 //
343 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
344 // 1-2:1.0
345 // tty
346 // ttyACM0
347 // 1-2:1.1
348 // net
349 // usb0
350 // 1-2:1.2
351 // tty
352 // ttyACM1
353 // ...
354 //
355 // /sys/class/net/usb0/device symlinks to
356 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700357 //
358 // Note that some modem devices have the tty directory one level deeper
359 // (eg. E362), so the device tree for the tty interface is:
360 // /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 -0700361
Paul Stewartca876ee2012-04-21 08:55:58 -0700362 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700363 FilePath device_path;
364 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700365 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
366 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700367 return false;
368 }
369 if (!device_path.IsAbsolute()) {
370 device_path = device_file.DirName().Append(device_path);
371 file_util::AbsolutePath(&device_path);
372 }
373
374 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700375 // USB device and see if there's a subdirectory "tty" inside. In other
376 // words, using the example dir hierarchy above, find
377 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700378 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700379 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700380}
381
382// static
Thieu Leb27beee2012-04-20 09:19:06 -0700383bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
384 file_util::FileEnumerator::FileType type =
385 static_cast<file_util::FileEnumerator::FileType>(
386 file_util::FileEnumerator::DIRECTORIES |
387 file_util::FileEnumerator::SHOW_SYM_LINKS);
388 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700389 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
390 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700391 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700392 return true;
393 }
394 return false;
395}
396
Paul Stewart8c116a92012-05-02 18:30:03 -0700397DeviceRefPtr DeviceInfo::CreateDevice(const string &link_name,
398 const string &address,
399 int interface_index,
400 Technology::Identifier technology) {
401 DeviceRefPtr device;
Paul Stewart050cfc02012-07-06 20:38:54 -0700402 delayed_devices_.erase(interface_index);
Ben Chan5086b972013-01-15 21:51:38 -0800403 infos_[interface_index].technology = technology;
Paul Stewart8c116a92012-05-02 18:30:03 -0700404
405 switch (technology) {
406 case Technology::kCellular:
407 // Cellular devices are managed by ModemInfo.
408 SLOG(Device, 2) << "Cellular link " << link_name
409 << " at index " << interface_index
410 << " -- notifying ModemInfo.";
Gary Morainbf74a672012-07-30 16:27:19 -0700411
412 // The MAC address provided by RTNL is not reliable for Gobi 2K modems.
Ben Chan4b285862012-10-10 22:52:16 -0700413 // Clear it here, and it will be fetched from the kernel in
414 // GetMACAddress().
Gary Morainbf74a672012-07-30 16:27:19 -0700415 infos_[interface_index].mac_address.Clear();
Paul Stewart8c116a92012-05-02 18:30:03 -0700416 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
417 break;
418 case Technology::kEthernet:
419 device = new Ethernet(control_interface_, dispatcher_, metrics_,
420 manager_, link_name, address, interface_index);
421 device->EnableIPv6Privacy();
422 break;
423 case Technology::kVirtioEthernet:
424 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
425 manager_, link_name, address,
426 interface_index);
427 device->EnableIPv6Privacy();
428 break;
429 case Technology::kWifi:
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700430 // Defer creating this device until we get information about the
431 // type of WiFi interface.
432 GetWiFiInterfaceInfo(interface_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700433 break;
Ben Chan4e64d2d2012-05-16 00:02:25 -0700434 case Technology::kWiMax:
Darin Petkove4b27022012-05-16 13:28:50 +0200435 // WiMax devices are managed by WiMaxProvider.
436 SLOG(Device, 2) << "WiMax link " << link_name
437 << " at index " << interface_index
438 << " -- notifying WiMaxProvider.";
Ben Chan4b285862012-10-10 22:52:16 -0700439 // The MAC address provided by RTNL may not be the final value as the
440 // WiMAX device may change the address after initialization. Clear it
441 // here, and it will be fetched from the kernel when
442 // WiMaxProvider::CreateDevice() is called after the WiMAX device DBus
443 // object is created by the WiMAX manager daemon.
444 infos_[interface_index].mac_address.Clear();
Darin Petkove4b27022012-05-16 13:28:50 +0200445 manager_->wimax_provider()->OnDeviceInfoAvailable(link_name);
Ben Chan4e64d2d2012-05-16 00:02:25 -0700446 break;
Paul Stewart8c116a92012-05-02 18:30:03 -0700447 case Technology::kPPP:
448 case Technology::kTunnel:
449 // Tunnel and PPP devices are managed by the VPN code (PPP for
450 // l2tpipsec). Notify the VPN Provider of the interface's presence.
451 // Since CreateDevice is only called once in the lifetime of an
452 // interface index, this notification will only occur the first
453 // time the device is seen.
454 SLOG(Device, 2) << "Tunnel / PPP link " << link_name
455 << " at index " << interface_index
456 << " -- notifying VPNProvider.";
457 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
458 interface_index) &&
459 technology == Technology::kTunnel) {
460 // If VPN does not know anything about this tunnel, it is probably
461 // left over from a previous instance and should not exist.
462 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
463 DeleteInterface(interface_index);
464 }
465 break;
466 case Technology::kLoopback:
467 // Loopback devices are largely ignored, but we should make sure the
468 // link is enabled.
469 SLOG(Device, 2) << "Bringing up loopback device " << link_name
470 << " at index " << interface_index;
471 rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP);
472 return NULL;
Paul Stewart050cfc02012-07-06 20:38:54 -0700473 case Technology::kCDCEthernet:
Ben Chan4eb4ddf2013-06-20 22:16:56 -0700474 // CDCEthernet devices are of indeterminate type when they are
Paul Stewart050cfc02012-07-06 20:38:54 -0700475 // initially created. Some time later, tty devices may or may
476 // not appear under the same USB device root, which will identify
477 // it as a modem. Alternatively, ModemManager may discover the
478 // device and create and register a Cellular device. In either
479 // case, we should delay creating a Device until we can make a
480 // better determination of what type this Device should be.
481 LOG(INFO) << "Delaying creation of device for " << link_name
482 << " at index " << interface_index;
483 DelayDeviceCreation(interface_index);
484 return NULL;
Paul Stewart8c116a92012-05-02 18:30:03 -0700485 default:
486 // We will not manage this device in shill. Do not create a device
487 // object or do anything to change its state. We create a stub object
488 // which is useful for testing.
489 return new DeviceStub(control_interface_, dispatcher_, metrics_,
490 manager_, link_name, address, interface_index,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700491 technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700492 }
493
494 // Reset the routing table and addresses.
495 routing_table_->FlushRoutes(interface_index);
496 FlushAddresses(interface_index);
497
Ben Chan5086b972013-01-15 21:51:38 -0800498 manager_->UpdateUninitializedTechnologies();
499
Paul Stewart8c116a92012-05-02 18:30:03 -0700500 return device;
501}
502
Chris Masone2aa97072011-08-09 17:35:08 -0700503void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700504 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
505 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700506 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700507 Technology::Identifier technology = Technology::kUnknown;
Darin Petkove6193c02011-08-11 12:42:40 -0700508 unsigned int flags = msg.link_status().flags;
509 unsigned int change = msg.link_status().change;
Paul Stewart8c116a92012-05-02 18:30:03 -0700510 bool new_device =
511 !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700512 SLOG(Device, 2) << __func__ << "(index=" << dev_index
513 << std::showbase << std::hex
514 << ", flags=" << flags << ", change=" << change << ")"
515 << std::dec << std::noshowbase
516 << ", new_device=" << new_device;
Paul Stewart8c116a92012-05-02 18:30:03 -0700517 infos_[dev_index].has_addresses_only = false;
Darin Petkove6193c02011-08-11 12:42:40 -0700518 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700519
Paul Stewart1ac4e842012-07-10 12:58:12 -0700520 RetrieveLinkStatistics(dev_index, msg);
521
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700522 DeviceRefPtr device = GetDevice(dev_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700523 if (new_device) {
524 CHECK(!device);
Chris Masone2aa97072011-08-09 17:35:08 -0700525 if (!msg.HasAttribute(IFLA_IFNAME)) {
526 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
527 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700528 }
Chris Masone2aa97072011-08-09 17:35:08 -0700529 ByteString b(msg.GetAttribute(IFLA_IFNAME));
530 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700531 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200532 infos_[dev_index].name = link_name;
533 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700534
Chris Masone2aa97072011-08-09 17:35:08 -0700535 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700536 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700537 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700538 } else {
539 technology = GetDeviceTechnology(link_name);
540 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700541 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800542 string address;
543 if (msg.HasAttribute(IFLA_ADDRESS)) {
544 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
545 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700546 SLOG(Device, 2) << "link index " << dev_index << " address "
547 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700548 } else if (technology != Technology::kTunnel &&
549 technology != Technology::kPPP) {
mukesh agrawal6813e762013-07-10 19:05:08 -0700550 LOG(ERROR) << "Add Link message for link '" << link_name
551 << "' does not have IFLA_ADDRESS!";
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800552 return;
553 }
Thieu Lec8078a62013-01-22 18:01:12 -0800554 metrics_->RegisterDevice(dev_index, technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700555 device = CreateDevice(link_name, address, dev_index, technology);
556 if (device) {
557 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700558 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700559 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700560 if (device) {
561 device->LinkEvent(flags, change);
562 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700563}
564
Chris Masone2aa97072011-08-09 17:35:08 -0700565void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700566 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700567
Paul Stewart9a908082011-08-31 12:18:48 -0700568 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
569 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700570 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
571 << std::showbase << std::hex
572 << ", flags=" << msg.link_status().flags
573 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700574 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700575}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700576
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700577DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
578 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700579 return info ? info->device : NULL;
580}
581
Darin Petkovf8046b82012-04-24 16:29:23 +0200582int DeviceInfo::GetIndex(const string &interface_name) const {
583 map<string, int>::const_iterator it = indices_.find(interface_name);
584 return it == indices_.end() ? -1 : it->second;
585}
586
Paul Stewart32852962011-08-30 14:06:53 -0700587bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700588 const Info *info = GetInfo(interface_index);
589 if (!info) {
590 return false;
591 }
Gary Morainbf74a672012-07-30 16:27:19 -0700592 // |mac_address| from RTNL is not used for some devices, in which case it will
593 // be empty here.
594 if (!info->mac_address.IsEmpty()) {
595 *address = info->mac_address;
596 return true;
597 }
598
599 // Ask the kernel for the MAC address.
600 *address = GetMACAddressFromKernel(interface_index);
601 return !address->IsEmpty();
602}
603
604ByteString DeviceInfo::GetMACAddressFromKernel(int interface_index) const {
Gary Morain41780232012-07-31 15:08:31 -0700605 const Info *info = GetInfo(interface_index);
606 if (!info) {
607 return ByteString();
608 }
609
610 const int fd = sockets_->Socket(PF_INET, SOCK_DGRAM, 0);
Gary Morainbf74a672012-07-30 16:27:19 -0700611 if (fd < 0) {
612 LOG(ERROR) << __func__ << ": Unable to open socket: " << fd;
Gary Morain41780232012-07-31 15:08:31 -0700613 return ByteString();
Gary Morainbf74a672012-07-30 16:27:19 -0700614 }
Gary Morain41780232012-07-31 15:08:31 -0700615
616 ScopedSocketCloser socket_closer(sockets_.get(), fd);
Gary Morainbf74a672012-07-30 16:27:19 -0700617 struct ifreq ifr;
618 memset(&ifr, 0, sizeof(ifr));
619 ifr.ifr_ifindex = interface_index;
Gary Morain41780232012-07-31 15:08:31 -0700620 strcpy(ifr.ifr_ifrn.ifrn_name, info->name.c_str());
621 int err = sockets_->Ioctl(fd, SIOCGIFHWADDR, &ifr);
Gary Morainbf74a672012-07-30 16:27:19 -0700622 if (err < 0) {
623 LOG(ERROR) << __func__ << ": Unable to read MAC address: " << errno;
Gary Morain41780232012-07-31 15:08:31 -0700624 return ByteString();
Gary Morainbf74a672012-07-30 16:27:19 -0700625 }
Gary Morain41780232012-07-31 15:08:31 -0700626
627 return ByteString(ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700628}
629
Paul Stewart9a908082011-08-31 12:18:48 -0700630bool DeviceInfo::GetAddresses(int interface_index,
631 vector<AddressData> *addresses) const {
632 const Info *info = GetInfo(interface_index);
633 if (!info) {
634 return false;
635 }
636 *addresses = info->ip_addresses;
637 return true;
638}
639
640void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700641 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700642 const Info *info = GetInfo(interface_index);
643 if (!info) {
644 return;
645 }
646 const vector<AddressData> &addresses = info->ip_addresses;
647 vector<AddressData>::const_iterator iter;
648 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700649 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700650 (iter->scope == RT_SCOPE_UNIVERSE &&
651 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700652 SLOG(Device, 2) << __func__ << ": removing ip address "
653 << iter->address.ToString()
654 << " from interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700655 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
656 }
657 }
658}
659
Paul Stewart05a42c22012-08-02 16:47:21 -0700660bool DeviceInfo::HasOtherAddress(
661 int interface_index, const IPAddress &this_address) const {
662 SLOG(Device, 3) << __func__ << "(" << interface_index << ")";
663 const Info *info = GetInfo(interface_index);
664 if (!info) {
665 return false;
666 }
667 const vector<AddressData> &addresses = info->ip_addresses;
668 vector<AddressData>::const_iterator iter;
669 bool has_other_address = false;
670 bool has_this_address = false;
671 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
672 if (iter->address.family() != this_address.family()) {
673 continue;
674 }
675 if (iter->address.address().Equals(this_address.address())) {
676 has_this_address = true;
677 } else if (this_address.family() == IPAddress::kFamilyIPv4) {
678 has_other_address = true;
679 } else if ((iter->scope == RT_SCOPE_UNIVERSE &&
680 (iter->flags & IFA_F_TEMPORARY) == 0)) {
681 has_other_address = true;
682 }
683 }
684 return has_other_address && !has_this_address;
685}
686
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700687bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
688 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700689 if (!info) {
690 return false;
691 }
692 *flags = info->flags;
693 return true;
694}
695
Paul Stewart1ac4e842012-07-10 12:58:12 -0700696bool DeviceInfo::GetByteCounts(int interface_index,
697 uint64 *rx_bytes,
698 uint64 *tx_bytes) const {
699 const Info *info = GetInfo(interface_index);
700 if (!info) {
701 return false;
702 }
703 *rx_bytes = info->rx_bytes;
704 *tx_bytes = info->tx_bytes;
705 return true;
706}
707
Paul Stewartca6abd42012-03-01 15:45:29 -0800708bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800709 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
710 if (fd < 0) {
711 PLOG(ERROR) << "failed to open " << kTunDeviceName;
712 return false;
713 }
714 file_util::ScopedFD scoped_fd(&fd);
715
716 struct ifreq ifr;
717 memset(&ifr, 0, sizeof(ifr));
718 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
719 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
720 PLOG(ERROR) << "failed to create tunnel interface";
721 return false;
722 }
723
724 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
725 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
726 return false;
727 }
728
729 *interface_name = string(ifr.ifr_name);
730
731 return true;
732}
733
Paul Stewartca6abd42012-03-01 15:45:29 -0800734bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800735 return rtnl_handler_->RemoveInterface(interface_index);
736}
737
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700738const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
739 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700740 if (iter == infos_.end()) {
741 return NULL;
742 }
743 return &iter->second;
744}
745
746void DeviceInfo::RemoveInfo(int interface_index) {
747 map<int, Info>::iterator iter = infos_.find(interface_index);
748 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700749 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700750 if (iter->second.device.get()) {
751 manager_->DeregisterDevice(iter->second.device);
752 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200753 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700754 infos_.erase(iter);
Paul Stewart050cfc02012-07-06 20:38:54 -0700755 delayed_devices_.erase(interface_index);
mukesh agrawal47009f82011-08-25 14:07:35 -0700756 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700757 SLOG(Device, 2) << __func__ << ": Unknown device index: "
758 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700759 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700760}
761
Chris Masone2aa97072011-08-09 17:35:08 -0700762void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700763 DCHECK(msg.type() == RTNLMessage::kTypeLink);
764 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700765 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700766 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700767 DelLinkMsgHandler(msg);
768 } else {
769 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700770 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700771}
772
Paul Stewart9a908082011-08-31 12:18:48 -0700773void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700774 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700775 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
776 int interface_index = msg.interface_index();
777 if (!ContainsKey(infos_, interface_index)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700778 SLOG(Device, 2) << "Got advance address information for unknown index "
779 << interface_index;
780 infos_[interface_index].has_addresses_only = true;
Paul Stewart9a908082011-08-31 12:18:48 -0700781 }
782 const RTNLMessage::AddressStatus &status = msg.address_status();
783 IPAddress address(msg.family(),
Ben Chan682c5d42012-06-18 14:11:04 -0700784 msg.HasAttribute(IFA_LOCAL) ?
785 msg.GetAttribute(IFA_LOCAL) : msg.GetAttribute(IFA_ADDRESS),
Paul Stewart9a908082011-08-31 12:18:48 -0700786 status.prefix_len);
787
Ben Chan682c5d42012-06-18 14:11:04 -0700788 SLOG_IF(Device, 2, msg.HasAttribute(IFA_LOCAL))
789 << "Found local address attribute for interface " << interface_index;
790
Paul Stewart9a908082011-08-31 12:18:48 -0700791 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
792 vector<AddressData>::iterator iter;
793 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
794 if (address.Equals(iter->address)) {
795 break;
796 }
797 }
798 if (iter != address_list.end()) {
799 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700800 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700801 address_list.erase(iter);
802 } else {
803 iter->flags = status.flags;
804 iter->scope = status.scope;
805 }
806 } else if (msg.mode() == RTNLMessage::kModeAdd) {
807 address_list.push_back(AddressData(address, status.flags, status.scope));
Paul Stewart8c116a92012-05-02 18:30:03 -0700808 SLOG(Device, 2) << "Add address " << address.ToString()
809 << " for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700810 }
811}
812
Paul Stewart050cfc02012-07-06 20:38:54 -0700813void DeviceInfo::DelayDeviceCreation(int interface_index) {
814 delayed_devices_.insert(interface_index);
815 delayed_devices_callback_.Reset(
816 Bind(&DeviceInfo::DelayedDeviceCreationTask, AsWeakPtr()));
817 dispatcher_->PostDelayedTask(delayed_devices_callback_.callback(),
818 kDelayedDeviceCreationSeconds * 1000);
819}
820
821// Re-evaluate the technology type for each delayed device.
822void DeviceInfo::DelayedDeviceCreationTask() {
823 while (!delayed_devices_.empty()) {
824 set<int>::iterator it = delayed_devices_.begin();
825 int dev_index = *it;
826 delayed_devices_.erase(it);
827
828 DCHECK(ContainsKey(infos_, dev_index));
829 DCHECK(!GetDevice(dev_index));
830
831 const string &link_name = infos_[dev_index].name;
832 Technology::Identifier technology = GetDeviceTechnology(link_name);
833
834 if (technology == Technology::kCDCEthernet) {
835 LOG(INFO) << "In " << __func__ << ": device " << link_name
836 << " is now assumed to be regular Ethernet.";
837 technology = Technology::kEthernet;
838 } else if (technology != Technology::kCellular) {
839 LOG(WARNING) << "In " << __func__ << ": device " << link_name
840 << " is unexpected technology "
841 << Technology::NameFromIdentifier(technology);
842 }
843 string address =
844 StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
845 DCHECK(!address.empty());
846
847 DeviceRefPtr device = CreateDevice(link_name, address, dev_index,
848 technology);
849 if (device) {
850 RegisterDevice(device);
851 }
852 }
853}
854
Paul Stewart1ac4e842012-07-10 12:58:12 -0700855void DeviceInfo::RetrieveLinkStatistics(int interface_index,
856 const RTNLMessage &msg) {
857 if (!msg.HasAttribute(IFLA_STATS64)) {
858 return;
859 }
860 ByteString stats_bytes(msg.GetAttribute(IFLA_STATS64));
861 struct rtnl_link_stats64 stats;
862 if (stats_bytes.GetLength() < sizeof(stats)) {
863 LOG(WARNING) << "Link statistics size is too small: "
864 << stats_bytes.GetLength() << " < " << sizeof(stats);
865 return;
866 }
867
868 memcpy(&stats, stats_bytes.GetConstData(), sizeof(stats));
869 SLOG(Device, 2) << "Link statistics for "
870 << " interface index " << interface_index << ": "
871 << "receive: " << stats.rx_bytes << "; "
872 << "transmit: " << stats.tx_bytes << ".";
873 infos_[interface_index].rx_bytes = stats.rx_bytes;
874 infos_[interface_index].tx_bytes = stats.tx_bytes;
875}
876
877void DeviceInfo::RequestLinkStatistics() {
878 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink);
879 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
Ben Chanb061f892013-02-27 17:46:55 -0800880 kRequestLinkStatisticsIntervalMilliseconds);
Paul Stewart1ac4e842012-07-10 12:58:12 -0700881}
882
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700883void DeviceInfo::GetWiFiInterfaceInfo(int interface_index) {
884 GetInterfaceMessage msg;
885 if (!msg.attributes()->SetU32AttributeValue(NL80211_ATTR_IFINDEX,
886 interface_index)) {
887 LOG(ERROR) << "Unable to set interface index attribute for "
888 "GetInterface message. Interface type cannot be "
889 "determined!";
890 return;
891 }
Wade Guthrie7347bf22013-04-30 11:21:51 -0700892 netlink_manager_->SendNl80211Message(
893 &msg,
894 Bind(&DeviceInfo::OnWiFiInterfaceInfoReceived, AsWeakPtr()),
895 Bind(&NetlinkManager::OnNetlinkMessageError));
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700896}
897
Wade Guthrie7347bf22013-04-30 11:21:51 -0700898void DeviceInfo::OnWiFiInterfaceInfoReceived(const Nl80211Message &msg) {
899 if (msg.command() != NL80211_CMD_NEW_INTERFACE) {
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700900 LOG(ERROR) << "Message is not a new interface response";
901 return;
902 }
903
904 uint32_t interface_index;
Wade Guthrie7347bf22013-04-30 11:21:51 -0700905 if (!msg.const_attributes()->GetU32AttributeValue(NL80211_ATTR_IFINDEX,
906 &interface_index)) {
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700907 LOG(ERROR) << "Message contains no interface index";
908 return;
909 }
910 uint32_t interface_type;
Wade Guthrie7347bf22013-04-30 11:21:51 -0700911 if (!msg.const_attributes()->GetU32AttributeValue(NL80211_ATTR_IFTYPE,
912 &interface_type)) {
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700913 LOG(ERROR) << "Message contains no interface type";
914 return;
915 }
916 const Info *info = GetInfo(interface_index);
917 if (!info) {
918 LOG(ERROR) << "Could not find device info for interface index "
919 << interface_index;
920 return;
921 }
922 if (info->device) {
923 LOG(ERROR) << "Device already created for interface index "
924 << interface_index;
925 return;
926 }
927 if (interface_type != NL80211_IFTYPE_STATION) {
928 LOG(INFO) << "Ignoring WiFi device "
929 << info->name
930 << " at interface index "
931 << interface_index
932 << " since it is not in station mode.";
933 return;
934 }
935 LOG(INFO) << "Creating WiFi device for station mode interface "
936 << info->name
937 << " at interface index "
938 << interface_index;
939 string address = StringToLowerASCII(info->mac_address.HexEncode());
940 DeviceRefPtr device =
941 new WiFi(control_interface_, dispatcher_, metrics_, manager_,
942 info->name, address, interface_index);
943 device->EnableIPv6Privacy();
944 RegisterDevice(device);
945}
946
Paul Stewart0af98bf2011-05-10 17:38:08 -0700947} // namespace shill