blob: f298ddc57d9ab4f091573733de3604bca7d04140 [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";
66const char DeviceInfo::kDriverGdmWiMax[] = "gdm_wimax";
mukesh agrawal93a29ed2012-04-17 16:13:01 -070067const char DeviceInfo::kDriverVirtioNet[] = "virtio_net";
Paul Stewartca876ee2012-04-21 08:55:58 -070068const char DeviceInfo::kInterfaceUevent[] = "uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080069const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Paul Stewartca876ee2012-04-21 08:55:58 -070070const char DeviceInfo::kInterfaceDevice[] = "device";
71const char DeviceInfo::kInterfaceDriver[] = "device/driver";
72const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags";
73const char DeviceInfo::kInterfaceType[] = "type";
Paul Stewartb50f0b92011-05-16 16:31:42 -070074const char *DeviceInfo::kModemDrivers[] = {
75 "gobi",
76 "QCUSBNet2k",
Ben Chan226d46a2012-10-11 00:22:17 -070077 "GobiNet",
78 "qmi_wwan"
Paul Stewartb50f0b92011-05-16 16:31:42 -070079};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080080const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewart050cfc02012-07-06 20:38:54 -070081const int DeviceInfo::kDelayedDeviceCreationSeconds = 5;
Ben Chanb061f892013-02-27 17:46:55 -080082const int DeviceInfo::kRequestLinkStatisticsIntervalMilliseconds = 20000;
Paul Stewartb50f0b92011-05-16 16:31:42 -070083
84DeviceInfo::DeviceInfo(ControlInterface *control_interface,
85 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080086 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070087 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070088 : control_interface_(control_interface),
89 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080090 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070091 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050092 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
93 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070094 link_listener_(NULL),
95 address_listener_(NULL),
Paul Stewartca876ee2012-04-21 08:55:58 -070096 device_info_root_(kDeviceInfoRoot),
Paul Stewart8c116a92012-05-02 18:30:03 -070097 routing_table_(RoutingTable::GetInstance()),
Gary Morain41780232012-07-31 15:08:31 -070098 rtnl_handler_(RTNLHandler::GetInstance()),
Paul Stewart2ddf2c62013-04-16 09:47:34 -070099 netlink_manager_(NetlinkManager::GetInstance()),
Gary Morain41780232012-07-31 15:08:31 -0700100 sockets_(new Sockets()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -0700101}
102
Paul Stewarta3c56f92011-05-26 07:08:52 -0700103DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -0700104
mukesh agrawal8f317b62011-07-15 11:53:23 -0700105void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
106 black_list_.insert(device_name);
107}
108
Eric Shienbrood5e628a52012-03-21 16:56:59 -0400109bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
110 return ContainsKey(black_list_, device_name);
111}
112
Paul Stewarta3c56f92011-05-26 07:08:52 -0700113void DeviceInfo::Start() {
114 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500115 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700116 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500117 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700118 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
119 RTNLHandler::kRequestAddr);
Paul Stewart1ac4e842012-07-10 12:58:12 -0700120 request_link_statistics_callback_.Reset(
121 Bind(&DeviceInfo::RequestLinkStatistics, AsWeakPtr()));
122 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
Ben Chanb061f892013-02-27 17:46:55 -0800123 kRequestLinkStatisticsIntervalMilliseconds);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700124}
125
Paul Stewarta3c56f92011-05-26 07:08:52 -0700126void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700127 link_listener_.reset();
128 address_listener_.reset();
Paul Stewart8c116a92012-05-02 18:30:03 -0700129 infos_.clear();
Paul Stewart1ac4e842012-07-10 12:58:12 -0700130 request_link_statistics_callback_.Cancel();
Paul Stewart050cfc02012-07-06 20:38:54 -0700131 delayed_devices_callback_.Cancel();
132 delayed_devices_.clear();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700133}
134
Ben Chan5086b972013-01-15 21:51:38 -0800135vector<string> DeviceInfo::GetUninitializedTechnologies() const {
136 set<string> unique_technologies;
137 for (map<int, Info>::const_iterator it = infos_.begin(); it != infos_.end();
138 ++it) {
139 if (it->second.device)
140 continue;
141
142 Technology::Identifier technology = it->second.technology;
143 if (Technology::IsPrimaryConnectivityTechnology(technology))
144 unique_technologies.insert(Technology::NameFromIdentifier(technology));
145 }
146 return vector<string>(unique_technologies.begin(), unique_technologies.end());
147}
148
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700149void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700150 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
151 << device->interface_index() << ")";
Paul Stewart050cfc02012-07-06 20:38:54 -0700152 delayed_devices_.erase(device->interface_index());
Darin Petkove6193c02011-08-11 12:42:40 -0700153 CHECK(!GetDevice(device->interface_index()).get());
154 infos_[device->interface_index()].device = device;
Thieu Le9abd6742013-01-23 23:35:37 -0800155 if (metrics_->IsDeviceRegistered(device->interface_index(),
156 device->technology())) {
157 metrics_->NotifyDeviceInitialized(device->interface_index());
158 } else {
159 metrics_->RegisterDevice(device->interface_index(), device->technology());
160 }
Ben Chan5086b972013-01-15 21:51:38 -0800161 if (Technology::IsPrimaryConnectivityTechnology(device->technology())) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700162 manager_->RegisterDevice(device);
163 }
164}
165
Jason Glasgowe9089492012-02-23 17:57:37 -0500166void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
167 int interface_index = device->interface_index();
168
Ben Chanfad4a0b2012-04-18 15:49:59 -0700169 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
170 << interface_index << ")";
Joshua Krollda798622012-06-05 12:30:48 -0700171 CHECK((device->technology() == Technology::kCellular) ||
172 (device->technology() == Technology::kWiMax));
Jason Glasgowe9089492012-02-23 17:57:37 -0500173
174 // Release reference to the device
175 map<int, Info>::iterator iter = infos_.find(interface_index);
176 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700177 SLOG(Device, 2) << "Removing device from info for index: "
178 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500179 manager_->DeregisterDevice(device);
180 // Release the reference to the device, but maintain the mapping
181 // for the index. That will be cleaned up by an RTNL message.
182 iter->second.device = NULL;
183 }
Thieu Lec8078a62013-01-22 18:01:12 -0800184 metrics_->DeregisterDevice(device->interface_index());
Jason Glasgowe9089492012-02-23 17:57:37 -0500185}
186
Paul Stewartca876ee2012-04-21 08:55:58 -0700187FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name,
188 const string &path_name) {
189 return device_info_root_.Append(iface_name).Append(path_name);
190}
191
192bool DeviceInfo::GetDeviceInfoContents(const string &iface_name,
193 const string &path_name,
194 string *contents_out) {
195 return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
196 contents_out);
197}
198bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700199 const string &path_name,
200 FilePath *path_out) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700201 return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
202 path_out);
203}
204
Paul Stewartfdd16072011-09-16 12:41:35 -0700205Technology::Identifier DeviceInfo::GetDeviceTechnology(
206 const string &iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700207 string type_string;
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700208 int arp_type = ARPHRD_VOID;
Paul Stewartca876ee2012-04-21 08:55:58 -0700209 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
210 TrimString(type_string, "\n", &type_string) &&
211 !base::StringToInt(type_string, &arp_type)) {
212 arp_type = ARPHRD_VOID;
213 }
214
Paul Stewart9364c4c2011-12-06 17:12:42 -0800215 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700216 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Paul Stewart050cfc02012-07-06 20:38:54 -0700217 LOG(INFO) << StringPrintf("%s: device %s has no uevent file",
218 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700219 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800220 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700221
Thieu Le8f1c8352012-04-16 11:02:12 -0700222 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
223 // start of the file or after a newline, we can safely assume this
224 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800225 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700226 SLOG(Device, 2)
227 << StringPrintf("%s: device %s has wifi signature in uevent file",
228 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700229 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700230 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
231 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800232 return Technology::kWiFiMonitor;
233 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700234 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700235 }
236
Jason Glasgow7904d142012-05-18 13:36:20 -0400237 // Special case for pseudo modems which are used for testing
238 if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) {
239 SLOG(Device, 2) << StringPrintf(
240 "%s: device %s is a pseudo modem for testing",
241 __func__, iface_name.c_str());
242 return Technology::kCellular;
243 }
244
Christopher Wileye049cc52012-09-10 14:36:57 -0700245 // Special case for pseudo ethernet devices which are used for testing.
246 if (iface_name.find(kEthernetPseudoDeviceNamePrefix) == 0) {
247 SLOG(Device, 2) << StringPrintf(
248 "%s: device %s is a virtual ethernet device for testing",
249 __func__, iface_name.c_str());
250 return Technology::kEthernet;
251 }
252
Paul Stewart9364c4c2011-12-06 17:12:42 -0800253 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700254 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700255 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
256 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700257 if (arp_type == ARPHRD_LOOPBACK) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700258 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
259 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700260 return Technology::kLoopback;
261 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700262 if (arp_type == ARPHRD_PPP) {
263 SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device",
264 __func__, iface_name.c_str());
265 return Technology::kPPP;
266 }
267 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800268 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700269 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
270 TrimString(tun_flags_str, "\n", &tun_flags_str) &&
271 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800272 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700273 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
274 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800275 return Technology::kTunnel;
276 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700277 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800278 }
279
280 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700281 // See if driver for this interface is in a list of known modem driver names.
282 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
283 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800284 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700285 SLOG(Device, 2)
286 << StringPrintf("%s: device %s is matched with modem driver %s",
287 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800288 return Technology::kCellular;
289 }
290 }
291
Ben Chan4e64d2d2012-05-16 00:02:25 -0700292 if (driver_name == kDriverGdmWiMax) {
293 SLOG(Device, 2) << StringPrintf("%s: device %s is a WiMAX device",
294 __func__, iface_name.c_str());
295 return Technology::kWiMax;
296 }
297
Thieu Le8f1c8352012-04-16 11:02:12 -0700298 // For cdc_ether devices, make sure it's a modem because this driver
299 // can be used for other ethernet devices.
Paul Stewart050cfc02012-07-06 20:38:54 -0700300 if (driver_name == kDriverCdcEther) {
301 if (IsCdcEtherModemDevice(iface_name)) {
302 LOG(INFO) << StringPrintf("%s: device %s is a modem cdc_ether "
303 "device", __func__, iface_name.c_str());
304 return Technology::kCellular;
305 }
306 SLOG(Device, 2) << StringPrintf("%s: device %s is a cdc_ether "
307 "device", __func__, iface_name.c_str());
308 return Technology::kCDCEthernet;
Thieu Le8f1c8352012-04-16 11:02:12 -0700309 }
310
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700311 // Special case for the virtio driver, used when run under KVM. See also
312 // the comment in VirtioEthernet::Start.
313 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700314 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
315 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700316 return Technology::kVirtioEthernet;
317 }
318
Ben Chanfad4a0b2012-04-18 15:49:59 -0700319 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
320 "is defaulted to type ethernet",
321 __func__, iface_name.c_str(),
322 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700323 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700324}
325
Thieu Le8f1c8352012-04-16 11:02:12 -0700326bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
327 // A cdc_ether device is a modem device if it also exposes tty interfaces.
328 // To determine this, we look for the existence of the tty interface in the
329 // USB device sysfs tree.
330 //
331 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
332 // follows:
333 //
334 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
335 // 1-2:1.0
336 // tty
337 // ttyACM0
338 // 1-2:1.1
339 // net
340 // usb0
341 // 1-2:1.2
342 // tty
343 // ttyACM1
344 // ...
345 //
346 // /sys/class/net/usb0/device symlinks to
347 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700348 //
349 // Note that some modem devices have the tty directory one level deeper
350 // (eg. E362), so the device tree for the tty interface is:
351 // /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 -0700352
Paul Stewartca876ee2012-04-21 08:55:58 -0700353 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700354 FilePath device_path;
355 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700356 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
357 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700358 return false;
359 }
360 if (!device_path.IsAbsolute()) {
361 device_path = device_file.DirName().Append(device_path);
362 file_util::AbsolutePath(&device_path);
363 }
364
365 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700366 // USB device and see if there's a subdirectory "tty" inside. In other
367 // words, using the example dir hierarchy above, find
368 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700369 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700370 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700371}
372
373// static
Thieu Leb27beee2012-04-20 09:19:06 -0700374bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
375 file_util::FileEnumerator::FileType type =
376 static_cast<file_util::FileEnumerator::FileType>(
377 file_util::FileEnumerator::DIRECTORIES |
378 file_util::FileEnumerator::SHOW_SYM_LINKS);
379 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700380 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
381 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700382 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700383 return true;
384 }
385 return false;
386}
387
Paul Stewart8c116a92012-05-02 18:30:03 -0700388DeviceRefPtr DeviceInfo::CreateDevice(const string &link_name,
389 const string &address,
390 int interface_index,
391 Technology::Identifier technology) {
392 DeviceRefPtr device;
Paul Stewart050cfc02012-07-06 20:38:54 -0700393 delayed_devices_.erase(interface_index);
Ben Chan5086b972013-01-15 21:51:38 -0800394 infos_[interface_index].technology = technology;
Paul Stewart8c116a92012-05-02 18:30:03 -0700395
396 switch (technology) {
397 case Technology::kCellular:
398 // Cellular devices are managed by ModemInfo.
399 SLOG(Device, 2) << "Cellular link " << link_name
400 << " at index " << interface_index
401 << " -- notifying ModemInfo.";
Gary Morainbf74a672012-07-30 16:27:19 -0700402
403 // The MAC address provided by RTNL is not reliable for Gobi 2K modems.
Ben Chan4b285862012-10-10 22:52:16 -0700404 // Clear it here, and it will be fetched from the kernel in
405 // GetMACAddress().
Gary Morainbf74a672012-07-30 16:27:19 -0700406 infos_[interface_index].mac_address.Clear();
Paul Stewart8c116a92012-05-02 18:30:03 -0700407 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
408 break;
409 case Technology::kEthernet:
410 device = new Ethernet(control_interface_, dispatcher_, metrics_,
411 manager_, link_name, address, interface_index);
412 device->EnableIPv6Privacy();
413 break;
414 case Technology::kVirtioEthernet:
415 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
416 manager_, link_name, address,
417 interface_index);
418 device->EnableIPv6Privacy();
419 break;
420 case Technology::kWifi:
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700421 // Defer creating this device until we get information about the
422 // type of WiFi interface.
423 GetWiFiInterfaceInfo(interface_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700424 break;
Ben Chan4e64d2d2012-05-16 00:02:25 -0700425 case Technology::kWiMax:
Darin Petkove4b27022012-05-16 13:28:50 +0200426 // WiMax devices are managed by WiMaxProvider.
427 SLOG(Device, 2) << "WiMax link " << link_name
428 << " at index " << interface_index
429 << " -- notifying WiMaxProvider.";
Ben Chan4b285862012-10-10 22:52:16 -0700430 // The MAC address provided by RTNL may not be the final value as the
431 // WiMAX device may change the address after initialization. Clear it
432 // here, and it will be fetched from the kernel when
433 // WiMaxProvider::CreateDevice() is called after the WiMAX device DBus
434 // object is created by the WiMAX manager daemon.
435 infos_[interface_index].mac_address.Clear();
Darin Petkove4b27022012-05-16 13:28:50 +0200436 manager_->wimax_provider()->OnDeviceInfoAvailable(link_name);
Ben Chan4e64d2d2012-05-16 00:02:25 -0700437 break;
Paul Stewart8c116a92012-05-02 18:30:03 -0700438 case Technology::kPPP:
439 case Technology::kTunnel:
440 // Tunnel and PPP devices are managed by the VPN code (PPP for
441 // l2tpipsec). Notify the VPN Provider of the interface's presence.
442 // Since CreateDevice is only called once in the lifetime of an
443 // interface index, this notification will only occur the first
444 // time the device is seen.
445 SLOG(Device, 2) << "Tunnel / PPP link " << link_name
446 << " at index " << interface_index
447 << " -- notifying VPNProvider.";
448 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
449 interface_index) &&
450 technology == Technology::kTunnel) {
451 // If VPN does not know anything about this tunnel, it is probably
452 // left over from a previous instance and should not exist.
453 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
454 DeleteInterface(interface_index);
455 }
456 break;
457 case Technology::kLoopback:
458 // Loopback devices are largely ignored, but we should make sure the
459 // link is enabled.
460 SLOG(Device, 2) << "Bringing up loopback device " << link_name
461 << " at index " << interface_index;
462 rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP);
463 return NULL;
Paul Stewart050cfc02012-07-06 20:38:54 -0700464 case Technology::kCDCEthernet:
465 // CDCEnternet devices are of indeterminate type when they are
466 // initially created. Some time later, tty devices may or may
467 // not appear under the same USB device root, which will identify
468 // it as a modem. Alternatively, ModemManager may discover the
469 // device and create and register a Cellular device. In either
470 // case, we should delay creating a Device until we can make a
471 // better determination of what type this Device should be.
472 LOG(INFO) << "Delaying creation of device for " << link_name
473 << " at index " << interface_index;
474 DelayDeviceCreation(interface_index);
475 return NULL;
Paul Stewart8c116a92012-05-02 18:30:03 -0700476 default:
477 // We will not manage this device in shill. Do not create a device
478 // object or do anything to change its state. We create a stub object
479 // which is useful for testing.
480 return new DeviceStub(control_interface_, dispatcher_, metrics_,
481 manager_, link_name, address, interface_index,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700482 technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700483 }
484
485 // Reset the routing table and addresses.
486 routing_table_->FlushRoutes(interface_index);
487 FlushAddresses(interface_index);
488
Ben Chan5086b972013-01-15 21:51:38 -0800489 manager_->UpdateUninitializedTechnologies();
490
Paul Stewart8c116a92012-05-02 18:30:03 -0700491 return device;
492}
493
Chris Masone2aa97072011-08-09 17:35:08 -0700494void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700495 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
496 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700497 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700498 Technology::Identifier technology = Technology::kUnknown;
Darin Petkove6193c02011-08-11 12:42:40 -0700499 unsigned int flags = msg.link_status().flags;
500 unsigned int change = msg.link_status().change;
Paul Stewart8c116a92012-05-02 18:30:03 -0700501 bool new_device =
502 !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700503 SLOG(Device, 2) << __func__ << "(index=" << dev_index
504 << std::showbase << std::hex
505 << ", flags=" << flags << ", change=" << change << ")"
506 << std::dec << std::noshowbase
507 << ", new_device=" << new_device;
Paul Stewart8c116a92012-05-02 18:30:03 -0700508 infos_[dev_index].has_addresses_only = false;
Darin Petkove6193c02011-08-11 12:42:40 -0700509 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700510
Paul Stewart1ac4e842012-07-10 12:58:12 -0700511 RetrieveLinkStatistics(dev_index, msg);
512
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700513 DeviceRefPtr device = GetDevice(dev_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700514 if (new_device) {
515 CHECK(!device);
Chris Masone2aa97072011-08-09 17:35:08 -0700516 if (!msg.HasAttribute(IFLA_IFNAME)) {
517 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
518 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700519 }
Chris Masone2aa97072011-08-09 17:35:08 -0700520 ByteString b(msg.GetAttribute(IFLA_IFNAME));
521 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700522 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200523 infos_[dev_index].name = link_name;
524 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700525
Chris Masone2aa97072011-08-09 17:35:08 -0700526 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700527 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700528 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700529 } else {
530 technology = GetDeviceTechnology(link_name);
531 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700532 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800533 string address;
534 if (msg.HasAttribute(IFLA_ADDRESS)) {
535 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
536 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700537 SLOG(Device, 2) << "link index " << dev_index << " address "
538 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700539 } else if (technology != Technology::kTunnel &&
540 technology != Technology::kPPP) {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800541 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
542 return;
543 }
Thieu Lec8078a62013-01-22 18:01:12 -0800544 metrics_->RegisterDevice(dev_index, technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700545 device = CreateDevice(link_name, address, dev_index, technology);
546 if (device) {
547 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700548 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700549 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700550 if (device) {
551 device->LinkEvent(flags, change);
552 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700553}
554
Chris Masone2aa97072011-08-09 17:35:08 -0700555void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700556 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700557
Paul Stewart9a908082011-08-31 12:18:48 -0700558 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
559 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700560 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
561 << std::showbase << std::hex
562 << ", flags=" << msg.link_status().flags
563 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700564 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700565}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700566
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700567DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
568 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700569 return info ? info->device : NULL;
570}
571
Darin Petkovf8046b82012-04-24 16:29:23 +0200572int DeviceInfo::GetIndex(const string &interface_name) const {
573 map<string, int>::const_iterator it = indices_.find(interface_name);
574 return it == indices_.end() ? -1 : it->second;
575}
576
Paul Stewart32852962011-08-30 14:06:53 -0700577bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700578 const Info *info = GetInfo(interface_index);
579 if (!info) {
580 return false;
581 }
Gary Morainbf74a672012-07-30 16:27:19 -0700582 // |mac_address| from RTNL is not used for some devices, in which case it will
583 // be empty here.
584 if (!info->mac_address.IsEmpty()) {
585 *address = info->mac_address;
586 return true;
587 }
588
589 // Ask the kernel for the MAC address.
590 *address = GetMACAddressFromKernel(interface_index);
591 return !address->IsEmpty();
592}
593
594ByteString DeviceInfo::GetMACAddressFromKernel(int interface_index) const {
Gary Morain41780232012-07-31 15:08:31 -0700595 const Info *info = GetInfo(interface_index);
596 if (!info) {
597 return ByteString();
598 }
599
600 const int fd = sockets_->Socket(PF_INET, SOCK_DGRAM, 0);
Gary Morainbf74a672012-07-30 16:27:19 -0700601 if (fd < 0) {
602 LOG(ERROR) << __func__ << ": Unable to open socket: " << fd;
Gary Morain41780232012-07-31 15:08:31 -0700603 return ByteString();
Gary Morainbf74a672012-07-30 16:27:19 -0700604 }
Gary Morain41780232012-07-31 15:08:31 -0700605
606 ScopedSocketCloser socket_closer(sockets_.get(), fd);
Gary Morainbf74a672012-07-30 16:27:19 -0700607 struct ifreq ifr;
608 memset(&ifr, 0, sizeof(ifr));
609 ifr.ifr_ifindex = interface_index;
Gary Morain41780232012-07-31 15:08:31 -0700610 strcpy(ifr.ifr_ifrn.ifrn_name, info->name.c_str());
611 int err = sockets_->Ioctl(fd, SIOCGIFHWADDR, &ifr);
Gary Morainbf74a672012-07-30 16:27:19 -0700612 if (err < 0) {
613 LOG(ERROR) << __func__ << ": Unable to read MAC address: " << errno;
Gary Morain41780232012-07-31 15:08:31 -0700614 return ByteString();
Gary Morainbf74a672012-07-30 16:27:19 -0700615 }
Gary Morain41780232012-07-31 15:08:31 -0700616
617 return ByteString(ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700618}
619
Paul Stewart9a908082011-08-31 12:18:48 -0700620bool DeviceInfo::GetAddresses(int interface_index,
621 vector<AddressData> *addresses) const {
622 const Info *info = GetInfo(interface_index);
623 if (!info) {
624 return false;
625 }
626 *addresses = info->ip_addresses;
627 return true;
628}
629
630void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700631 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700632 const Info *info = GetInfo(interface_index);
633 if (!info) {
634 return;
635 }
636 const vector<AddressData> &addresses = info->ip_addresses;
637 vector<AddressData>::const_iterator iter;
638 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700639 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700640 (iter->scope == RT_SCOPE_UNIVERSE &&
641 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700642 SLOG(Device, 2) << __func__ << ": removing ip address "
643 << iter->address.ToString()
644 << " from interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700645 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
646 }
647 }
648}
649
Paul Stewart05a42c22012-08-02 16:47:21 -0700650bool DeviceInfo::HasOtherAddress(
651 int interface_index, const IPAddress &this_address) const {
652 SLOG(Device, 3) << __func__ << "(" << interface_index << ")";
653 const Info *info = GetInfo(interface_index);
654 if (!info) {
655 return false;
656 }
657 const vector<AddressData> &addresses = info->ip_addresses;
658 vector<AddressData>::const_iterator iter;
659 bool has_other_address = false;
660 bool has_this_address = false;
661 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
662 if (iter->address.family() != this_address.family()) {
663 continue;
664 }
665 if (iter->address.address().Equals(this_address.address())) {
666 has_this_address = true;
667 } else if (this_address.family() == IPAddress::kFamilyIPv4) {
668 has_other_address = true;
669 } else if ((iter->scope == RT_SCOPE_UNIVERSE &&
670 (iter->flags & IFA_F_TEMPORARY) == 0)) {
671 has_other_address = true;
672 }
673 }
674 return has_other_address && !has_this_address;
675}
676
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700677bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
678 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700679 if (!info) {
680 return false;
681 }
682 *flags = info->flags;
683 return true;
684}
685
Paul Stewart1ac4e842012-07-10 12:58:12 -0700686bool DeviceInfo::GetByteCounts(int interface_index,
687 uint64 *rx_bytes,
688 uint64 *tx_bytes) const {
689 const Info *info = GetInfo(interface_index);
690 if (!info) {
691 return false;
692 }
693 *rx_bytes = info->rx_bytes;
694 *tx_bytes = info->tx_bytes;
695 return true;
696}
697
Paul Stewartca6abd42012-03-01 15:45:29 -0800698bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800699 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
700 if (fd < 0) {
701 PLOG(ERROR) << "failed to open " << kTunDeviceName;
702 return false;
703 }
704 file_util::ScopedFD scoped_fd(&fd);
705
706 struct ifreq ifr;
707 memset(&ifr, 0, sizeof(ifr));
708 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
709 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
710 PLOG(ERROR) << "failed to create tunnel interface";
711 return false;
712 }
713
714 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
715 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
716 return false;
717 }
718
719 *interface_name = string(ifr.ifr_name);
720
721 return true;
722}
723
Paul Stewartca6abd42012-03-01 15:45:29 -0800724bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800725 return rtnl_handler_->RemoveInterface(interface_index);
726}
727
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700728const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
729 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700730 if (iter == infos_.end()) {
731 return NULL;
732 }
733 return &iter->second;
734}
735
736void DeviceInfo::RemoveInfo(int interface_index) {
737 map<int, Info>::iterator iter = infos_.find(interface_index);
738 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700739 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700740 if (iter->second.device.get()) {
741 manager_->DeregisterDevice(iter->second.device);
742 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200743 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700744 infos_.erase(iter);
Paul Stewart050cfc02012-07-06 20:38:54 -0700745 delayed_devices_.erase(interface_index);
mukesh agrawal47009f82011-08-25 14:07:35 -0700746 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700747 SLOG(Device, 2) << __func__ << ": Unknown device index: "
748 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700749 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700750}
751
Chris Masone2aa97072011-08-09 17:35:08 -0700752void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700753 DCHECK(msg.type() == RTNLMessage::kTypeLink);
754 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700755 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700756 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700757 DelLinkMsgHandler(msg);
758 } else {
759 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700760 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700761}
762
Paul Stewart9a908082011-08-31 12:18:48 -0700763void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700764 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700765 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
766 int interface_index = msg.interface_index();
767 if (!ContainsKey(infos_, interface_index)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700768 SLOG(Device, 2) << "Got advance address information for unknown index "
769 << interface_index;
770 infos_[interface_index].has_addresses_only = true;
Paul Stewart9a908082011-08-31 12:18:48 -0700771 }
772 const RTNLMessage::AddressStatus &status = msg.address_status();
773 IPAddress address(msg.family(),
Ben Chan682c5d42012-06-18 14:11:04 -0700774 msg.HasAttribute(IFA_LOCAL) ?
775 msg.GetAttribute(IFA_LOCAL) : msg.GetAttribute(IFA_ADDRESS),
Paul Stewart9a908082011-08-31 12:18:48 -0700776 status.prefix_len);
777
Ben Chan682c5d42012-06-18 14:11:04 -0700778 SLOG_IF(Device, 2, msg.HasAttribute(IFA_LOCAL))
779 << "Found local address attribute for interface " << interface_index;
780
Paul Stewart9a908082011-08-31 12:18:48 -0700781 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
782 vector<AddressData>::iterator iter;
783 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
784 if (address.Equals(iter->address)) {
785 break;
786 }
787 }
788 if (iter != address_list.end()) {
789 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700790 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700791 address_list.erase(iter);
792 } else {
793 iter->flags = status.flags;
794 iter->scope = status.scope;
795 }
796 } else if (msg.mode() == RTNLMessage::kModeAdd) {
797 address_list.push_back(AddressData(address, status.flags, status.scope));
Paul Stewart8c116a92012-05-02 18:30:03 -0700798 SLOG(Device, 2) << "Add address " << address.ToString()
799 << " for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700800 }
801}
802
Paul Stewart050cfc02012-07-06 20:38:54 -0700803void DeviceInfo::DelayDeviceCreation(int interface_index) {
804 delayed_devices_.insert(interface_index);
805 delayed_devices_callback_.Reset(
806 Bind(&DeviceInfo::DelayedDeviceCreationTask, AsWeakPtr()));
807 dispatcher_->PostDelayedTask(delayed_devices_callback_.callback(),
808 kDelayedDeviceCreationSeconds * 1000);
809}
810
811// Re-evaluate the technology type for each delayed device.
812void DeviceInfo::DelayedDeviceCreationTask() {
813 while (!delayed_devices_.empty()) {
814 set<int>::iterator it = delayed_devices_.begin();
815 int dev_index = *it;
816 delayed_devices_.erase(it);
817
818 DCHECK(ContainsKey(infos_, dev_index));
819 DCHECK(!GetDevice(dev_index));
820
821 const string &link_name = infos_[dev_index].name;
822 Technology::Identifier technology = GetDeviceTechnology(link_name);
823
824 if (technology == Technology::kCDCEthernet) {
825 LOG(INFO) << "In " << __func__ << ": device " << link_name
826 << " is now assumed to be regular Ethernet.";
827 technology = Technology::kEthernet;
828 } else if (technology != Technology::kCellular) {
829 LOG(WARNING) << "In " << __func__ << ": device " << link_name
830 << " is unexpected technology "
831 << Technology::NameFromIdentifier(technology);
832 }
833 string address =
834 StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
835 DCHECK(!address.empty());
836
837 DeviceRefPtr device = CreateDevice(link_name, address, dev_index,
838 technology);
839 if (device) {
840 RegisterDevice(device);
841 }
842 }
843}
844
Paul Stewart1ac4e842012-07-10 12:58:12 -0700845void DeviceInfo::RetrieveLinkStatistics(int interface_index,
846 const RTNLMessage &msg) {
847 if (!msg.HasAttribute(IFLA_STATS64)) {
848 return;
849 }
850 ByteString stats_bytes(msg.GetAttribute(IFLA_STATS64));
851 struct rtnl_link_stats64 stats;
852 if (stats_bytes.GetLength() < sizeof(stats)) {
853 LOG(WARNING) << "Link statistics size is too small: "
854 << stats_bytes.GetLength() << " < " << sizeof(stats);
855 return;
856 }
857
858 memcpy(&stats, stats_bytes.GetConstData(), sizeof(stats));
859 SLOG(Device, 2) << "Link statistics for "
860 << " interface index " << interface_index << ": "
861 << "receive: " << stats.rx_bytes << "; "
862 << "transmit: " << stats.tx_bytes << ".";
863 infos_[interface_index].rx_bytes = stats.rx_bytes;
864 infos_[interface_index].tx_bytes = stats.tx_bytes;
865}
866
867void DeviceInfo::RequestLinkStatistics() {
868 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink);
869 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
Ben Chanb061f892013-02-27 17:46:55 -0800870 kRequestLinkStatisticsIntervalMilliseconds);
Paul Stewart1ac4e842012-07-10 12:58:12 -0700871}
872
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700873void DeviceInfo::GetWiFiInterfaceInfo(int interface_index) {
874 GetInterfaceMessage msg;
875 if (!msg.attributes()->SetU32AttributeValue(NL80211_ATTR_IFINDEX,
876 interface_index)) {
877 LOG(ERROR) << "Unable to set interface index attribute for "
878 "GetInterface message. Interface type cannot be "
879 "determined!";
880 return;
881 }
882 netlink_manager_->SendMessage(&msg,
883 Bind(&DeviceInfo::OnWiFiInterfaceInfoReceived,
884 AsWeakPtr()));
885}
886
887void DeviceInfo::OnWiFiInterfaceInfoReceived(
888 const NetlinkMessage &raw_message) {
889 if (raw_message.message_type() != Nl80211Message::GetMessageType()) {
890 LOG(ERROR) << "Unknown message type received: "
891 << raw_message.message_type();
892 return;
893 }
894 // Due to the test above, this is guaranteed to be an NL80211Message type.
895 const Nl80211Message *msg =
896 reinterpret_cast<const Nl80211Message *>(&raw_message);
897
898 if (msg->command() != NL80211_CMD_NEW_INTERFACE) {
899 LOG(ERROR) << "Message is not a new interface response";
900 return;
901 }
902
903 uint32_t interface_index;
904 if (!msg->const_attributes()->GetU32AttributeValue(NL80211_ATTR_IFINDEX,
905 &interface_index)) {
906 LOG(ERROR) << "Message contains no interface index";
907 return;
908 }
909 uint32_t interface_type;
910 if (!msg->const_attributes()->GetU32AttributeValue(NL80211_ATTR_IFTYPE,
911 &interface_type)) {
912 LOG(ERROR) << "Message contains no interface type";
913 return;
914 }
915 const Info *info = GetInfo(interface_index);
916 if (!info) {
917 LOG(ERROR) << "Could not find device info for interface index "
918 << interface_index;
919 return;
920 }
921 if (info->device) {
922 LOG(ERROR) << "Device already created for interface index "
923 << interface_index;
924 return;
925 }
926 if (interface_type != NL80211_IFTYPE_STATION) {
927 LOG(INFO) << "Ignoring WiFi device "
928 << info->name
929 << " at interface index "
930 << interface_index
931 << " since it is not in station mode.";
932 return;
933 }
934 LOG(INFO) << "Creating WiFi device for station mode interface "
935 << info->name
936 << " at interface index "
937 << interface_index;
938 string address = StringToLowerASCII(info->mac_address.HexEncode());
939 DeviceRefPtr device =
940 new WiFi(control_interface_, dispatcher_, metrics_, manager_,
941 info->name, address, interface_index);
942 device->EnableIPv6Privacy();
943 RegisterDevice(device);
944}
945
Paul Stewart0af98bf2011-05-10 17:38:08 -0700946} // namespace shill