blob: 2e8df1ba46993fc0ed6d53cbe4e72e65ee0f998d [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 Stewart8c116a92012-05-02 18:30:03 -070037#include "shill/routing_table.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070038#include "shill/rtnl_handler.h"
39#include "shill/rtnl_listener.h"
Chris Masone2aa97072011-08-09 17:35:08 -070040#include "shill/rtnl_message.h"
Chris Masone487b8bf2011-05-13 16:27:57 -070041#include "shill/service.h"
Gary Morain41780232012-07-31 15:08:31 -070042#include "shill/sockets.h"
mukesh agrawal93a29ed2012-04-17 16:13:01 -070043#include "shill/virtio_ethernet.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070044#include "shill/wifi.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070045
Eric Shienbrood3e20a232012-02-16 11:35:56 -050046using base::Bind;
Thieu Le8f1c8352012-04-16 11:02:12 -070047using base::StringPrintf;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050048using base::Unretained;
Darin Petkove6193c02011-08-11 12:42:40 -070049using std::map;
Paul Stewart050cfc02012-07-06 20:38:54 -070050using std::set;
Paul Stewart0af98bf2011-05-10 17:38:08 -070051using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070052using std::vector;
Paul Stewart0af98bf2011-05-10 17:38:08 -070053
54namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070055
56// static
Jason Glasgowabc54032012-04-20 16:08:32 -040057const char DeviceInfo::kModemPseudoDeviceNamePrefix[] = "pseudomodem";
Christopher Wileye049cc52012-09-10 14:36:57 -070058const char DeviceInfo::kEthernetPseudoDeviceNamePrefix[] = "pseudoethernet";
Paul Stewartca876ee2012-04-21 08:55:58 -070059const char DeviceInfo::kDeviceInfoRoot[] = "/sys/class/net";
Ben Chan4e64d2d2012-05-16 00:02:25 -070060const char DeviceInfo::kDriverCdcEther[] = "cdc_ether";
61const char DeviceInfo::kDriverGdmWiMax[] = "gdm_wimax";
mukesh agrawal93a29ed2012-04-17 16:13:01 -070062const char DeviceInfo::kDriverVirtioNet[] = "virtio_net";
Paul Stewartca876ee2012-04-21 08:55:58 -070063const char DeviceInfo::kInterfaceUevent[] = "uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080064const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Paul Stewartca876ee2012-04-21 08:55:58 -070065const char DeviceInfo::kInterfaceDevice[] = "device";
66const char DeviceInfo::kInterfaceDriver[] = "device/driver";
67const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags";
68const char DeviceInfo::kInterfaceType[] = "type";
Paul Stewartb50f0b92011-05-16 16:31:42 -070069const char *DeviceInfo::kModemDrivers[] = {
70 "gobi",
71 "QCUSBNet2k",
Ben Chan226d46a2012-10-11 00:22:17 -070072 "GobiNet",
73 "qmi_wwan"
Paul Stewartb50f0b92011-05-16 16:31:42 -070074};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080075const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewart050cfc02012-07-06 20:38:54 -070076const int DeviceInfo::kDelayedDeviceCreationSeconds = 5;
Paul Stewart1ac4e842012-07-10 12:58:12 -070077const int DeviceInfo::kRequestLinkStatisticsIntervalSeconds = 60;
Paul Stewartb50f0b92011-05-16 16:31:42 -070078
79DeviceInfo::DeviceInfo(ControlInterface *control_interface,
80 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080081 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070082 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070083 : control_interface_(control_interface),
84 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080085 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070086 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050087 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
88 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070089 link_listener_(NULL),
90 address_listener_(NULL),
Paul Stewartca876ee2012-04-21 08:55:58 -070091 device_info_root_(kDeviceInfoRoot),
Paul Stewart8c116a92012-05-02 18:30:03 -070092 routing_table_(RoutingTable::GetInstance()),
Gary Morain41780232012-07-31 15:08:31 -070093 rtnl_handler_(RTNLHandler::GetInstance()),
94 sockets_(new Sockets()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070095}
96
Paul Stewarta3c56f92011-05-26 07:08:52 -070097DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070098
mukesh agrawal8f317b62011-07-15 11:53:23 -070099void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
100 black_list_.insert(device_name);
101}
102
Eric Shienbrood5e628a52012-03-21 16:56:59 -0400103bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
104 return ContainsKey(black_list_, device_name);
105}
106
Paul Stewarta3c56f92011-05-26 07:08:52 -0700107void DeviceInfo::Start() {
108 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500109 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700110 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500111 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700112 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
113 RTNLHandler::kRequestAddr);
Paul Stewart1ac4e842012-07-10 12:58:12 -0700114 request_link_statistics_callback_.Reset(
115 Bind(&DeviceInfo::RequestLinkStatistics, AsWeakPtr()));
116 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
117 kRequestLinkStatisticsIntervalSeconds * 1000);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700118}
119
Paul Stewarta3c56f92011-05-26 07:08:52 -0700120void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700121 link_listener_.reset();
122 address_listener_.reset();
Paul Stewart8c116a92012-05-02 18:30:03 -0700123 infos_.clear();
Paul Stewart1ac4e842012-07-10 12:58:12 -0700124 request_link_statistics_callback_.Cancel();
Paul Stewart050cfc02012-07-06 20:38:54 -0700125 delayed_devices_callback_.Cancel();
126 delayed_devices_.clear();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700127}
128
Ben Chan5086b972013-01-15 21:51:38 -0800129vector<string> DeviceInfo::GetUninitializedTechnologies() const {
130 set<string> unique_technologies;
131 for (map<int, Info>::const_iterator it = infos_.begin(); it != infos_.end();
132 ++it) {
133 if (it->second.device)
134 continue;
135
136 Technology::Identifier technology = it->second.technology;
137 if (Technology::IsPrimaryConnectivityTechnology(technology))
138 unique_technologies.insert(Technology::NameFromIdentifier(technology));
139 }
140 return vector<string>(unique_technologies.begin(), unique_technologies.end());
141}
142
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700143void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700144 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
145 << device->interface_index() << ")";
Paul Stewart050cfc02012-07-06 20:38:54 -0700146 delayed_devices_.erase(device->interface_index());
Darin Petkove6193c02011-08-11 12:42:40 -0700147 CHECK(!GetDevice(device->interface_index()).get());
148 infos_[device->interface_index()].device = device;
Ben Chan5086b972013-01-15 21:51:38 -0800149 if (Technology::IsPrimaryConnectivityTechnology(device->technology())) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700150 manager_->RegisterDevice(device);
151 }
152}
153
Jason Glasgowe9089492012-02-23 17:57:37 -0500154void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
155 int interface_index = device->interface_index();
156
Ben Chanfad4a0b2012-04-18 15:49:59 -0700157 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
158 << interface_index << ")";
Joshua Krollda798622012-06-05 12:30:48 -0700159 CHECK((device->technology() == Technology::kCellular) ||
160 (device->technology() == Technology::kWiMax));
Jason Glasgowe9089492012-02-23 17:57:37 -0500161
162 // Release reference to the device
163 map<int, Info>::iterator iter = infos_.find(interface_index);
164 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700165 SLOG(Device, 2) << "Removing device from info for index: "
166 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500167 manager_->DeregisterDevice(device);
168 // Release the reference to the device, but maintain the mapping
169 // for the index. That will be cleaned up by an RTNL message.
170 iter->second.device = NULL;
171 }
172}
173
Paul Stewartca876ee2012-04-21 08:55:58 -0700174FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name,
175 const string &path_name) {
176 return device_info_root_.Append(iface_name).Append(path_name);
177}
178
179bool DeviceInfo::GetDeviceInfoContents(const string &iface_name,
180 const string &path_name,
181 string *contents_out) {
182 return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
183 contents_out);
184}
185bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700186 const string &path_name,
187 FilePath *path_out) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700188 return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
189 path_out);
190}
191
Paul Stewartfdd16072011-09-16 12:41:35 -0700192Technology::Identifier DeviceInfo::GetDeviceTechnology(
193 const string &iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700194 string type_string;
195 int arp_type = ARPHRD_VOID;;
196 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
197 TrimString(type_string, "\n", &type_string) &&
198 !base::StringToInt(type_string, &arp_type)) {
199 arp_type = ARPHRD_VOID;
200 }
201
Paul Stewart9364c4c2011-12-06 17:12:42 -0800202 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700203 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Paul Stewart050cfc02012-07-06 20:38:54 -0700204 LOG(INFO) << StringPrintf("%s: device %s has no uevent file",
205 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700206 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800207 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700208
Thieu Le8f1c8352012-04-16 11:02:12 -0700209 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
210 // start of the file or after a newline, we can safely assume this
211 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800212 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700213 SLOG(Device, 2)
214 << StringPrintf("%s: device %s has wifi signature in uevent file",
215 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700216 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700217 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
218 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800219 return Technology::kWiFiMonitor;
220 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700221 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700222 }
223
Jason Glasgow7904d142012-05-18 13:36:20 -0400224 // Special case for pseudo modems which are used for testing
225 if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) {
226 SLOG(Device, 2) << StringPrintf(
227 "%s: device %s is a pseudo modem for testing",
228 __func__, iface_name.c_str());
229 return Technology::kCellular;
230 }
231
Christopher Wileye049cc52012-09-10 14:36:57 -0700232 // Special case for pseudo ethernet devices which are used for testing.
233 if (iface_name.find(kEthernetPseudoDeviceNamePrefix) == 0) {
234 SLOG(Device, 2) << StringPrintf(
235 "%s: device %s is a virtual ethernet device for testing",
236 __func__, iface_name.c_str());
237 return Technology::kEthernet;
238 }
239
Paul Stewart9364c4c2011-12-06 17:12:42 -0800240 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700241 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700242 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
243 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700244 if (arp_type == ARPHRD_LOOPBACK) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700245 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
246 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700247 return Technology::kLoopback;
248 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700249 if (arp_type == ARPHRD_PPP) {
250 SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device",
251 __func__, iface_name.c_str());
252 return Technology::kPPP;
253 }
254 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800255 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700256 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
257 TrimString(tun_flags_str, "\n", &tun_flags_str) &&
258 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800259 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700260 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
261 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800262 return Technology::kTunnel;
263 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700264 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800265 }
266
267 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700268 // See if driver for this interface is in a list of known modem driver names.
269 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
270 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800271 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700272 SLOG(Device, 2)
273 << StringPrintf("%s: device %s is matched with modem driver %s",
274 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800275 return Technology::kCellular;
276 }
277 }
278
Ben Chan4e64d2d2012-05-16 00:02:25 -0700279 if (driver_name == kDriverGdmWiMax) {
280 SLOG(Device, 2) << StringPrintf("%s: device %s is a WiMAX device",
281 __func__, iface_name.c_str());
282 return Technology::kWiMax;
283 }
284
Thieu Le8f1c8352012-04-16 11:02:12 -0700285 // For cdc_ether devices, make sure it's a modem because this driver
286 // can be used for other ethernet devices.
Paul Stewart050cfc02012-07-06 20:38:54 -0700287 if (driver_name == kDriverCdcEther) {
288 if (IsCdcEtherModemDevice(iface_name)) {
289 LOG(INFO) << StringPrintf("%s: device %s is a modem cdc_ether "
290 "device", __func__, iface_name.c_str());
291 return Technology::kCellular;
292 }
293 SLOG(Device, 2) << StringPrintf("%s: device %s is a cdc_ether "
294 "device", __func__, iface_name.c_str());
295 return Technology::kCDCEthernet;
Thieu Le8f1c8352012-04-16 11:02:12 -0700296 }
297
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700298 // Special case for the virtio driver, used when run under KVM. See also
299 // the comment in VirtioEthernet::Start.
300 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700301 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
302 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700303 return Technology::kVirtioEthernet;
304 }
305
Ben Chanfad4a0b2012-04-18 15:49:59 -0700306 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
307 "is defaulted to type ethernet",
308 __func__, iface_name.c_str(),
309 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700310 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700311}
312
Thieu Le8f1c8352012-04-16 11:02:12 -0700313bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
314 // A cdc_ether device is a modem device if it also exposes tty interfaces.
315 // To determine this, we look for the existence of the tty interface in the
316 // USB device sysfs tree.
317 //
318 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
319 // follows:
320 //
321 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
322 // 1-2:1.0
323 // tty
324 // ttyACM0
325 // 1-2:1.1
326 // net
327 // usb0
328 // 1-2:1.2
329 // tty
330 // ttyACM1
331 // ...
332 //
333 // /sys/class/net/usb0/device symlinks to
334 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700335 //
336 // Note that some modem devices have the tty directory one level deeper
337 // (eg. E362), so the device tree for the tty interface is:
338 // /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 -0700339
Paul Stewartca876ee2012-04-21 08:55:58 -0700340 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700341 FilePath device_path;
342 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700343 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
344 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700345 return false;
346 }
347 if (!device_path.IsAbsolute()) {
348 device_path = device_file.DirName().Append(device_path);
349 file_util::AbsolutePath(&device_path);
350 }
351
352 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700353 // USB device and see if there's a subdirectory "tty" inside. In other
354 // words, using the example dir hierarchy above, find
355 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700356 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700357 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700358}
359
360// static
Thieu Leb27beee2012-04-20 09:19:06 -0700361bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
362 file_util::FileEnumerator::FileType type =
363 static_cast<file_util::FileEnumerator::FileType>(
364 file_util::FileEnumerator::DIRECTORIES |
365 file_util::FileEnumerator::SHOW_SYM_LINKS);
366 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700367 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
368 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700369 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700370 return true;
371 }
372 return false;
373}
374
Paul Stewart8c116a92012-05-02 18:30:03 -0700375DeviceRefPtr DeviceInfo::CreateDevice(const string &link_name,
376 const string &address,
377 int interface_index,
378 Technology::Identifier technology) {
379 DeviceRefPtr device;
Paul Stewart050cfc02012-07-06 20:38:54 -0700380 delayed_devices_.erase(interface_index);
Ben Chan5086b972013-01-15 21:51:38 -0800381 infos_[interface_index].technology = technology;
Paul Stewart8c116a92012-05-02 18:30:03 -0700382
383 switch (technology) {
384 case Technology::kCellular:
385 // Cellular devices are managed by ModemInfo.
386 SLOG(Device, 2) << "Cellular link " << link_name
387 << " at index " << interface_index
388 << " -- notifying ModemInfo.";
Gary Morainbf74a672012-07-30 16:27:19 -0700389
390 // The MAC address provided by RTNL is not reliable for Gobi 2K modems.
Ben Chan4b285862012-10-10 22:52:16 -0700391 // Clear it here, and it will be fetched from the kernel in
392 // GetMACAddress().
Gary Morainbf74a672012-07-30 16:27:19 -0700393 infos_[interface_index].mac_address.Clear();
Paul Stewart8c116a92012-05-02 18:30:03 -0700394 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
395 break;
396 case Technology::kEthernet:
397 device = new Ethernet(control_interface_, dispatcher_, metrics_,
398 manager_, link_name, address, interface_index);
399 device->EnableIPv6Privacy();
400 break;
401 case Technology::kVirtioEthernet:
402 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
403 manager_, link_name, address,
404 interface_index);
405 device->EnableIPv6Privacy();
406 break;
407 case Technology::kWifi:
408 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
409 link_name, address, interface_index);
410 device->EnableIPv6Privacy();
411 break;
Ben Chan4e64d2d2012-05-16 00:02:25 -0700412 case Technology::kWiMax:
Darin Petkove4b27022012-05-16 13:28:50 +0200413 // WiMax devices are managed by WiMaxProvider.
414 SLOG(Device, 2) << "WiMax link " << link_name
415 << " at index " << interface_index
416 << " -- notifying WiMaxProvider.";
Ben Chan4b285862012-10-10 22:52:16 -0700417 // The MAC address provided by RTNL may not be the final value as the
418 // WiMAX device may change the address after initialization. Clear it
419 // here, and it will be fetched from the kernel when
420 // WiMaxProvider::CreateDevice() is called after the WiMAX device DBus
421 // object is created by the WiMAX manager daemon.
422 infos_[interface_index].mac_address.Clear();
Darin Petkove4b27022012-05-16 13:28:50 +0200423 manager_->wimax_provider()->OnDeviceInfoAvailable(link_name);
Ben Chan4e64d2d2012-05-16 00:02:25 -0700424 break;
Paul Stewart8c116a92012-05-02 18:30:03 -0700425 case Technology::kPPP:
426 case Technology::kTunnel:
427 // Tunnel and PPP devices are managed by the VPN code (PPP for
428 // l2tpipsec). Notify the VPN Provider of the interface's presence.
429 // Since CreateDevice is only called once in the lifetime of an
430 // interface index, this notification will only occur the first
431 // time the device is seen.
432 SLOG(Device, 2) << "Tunnel / PPP link " << link_name
433 << " at index " << interface_index
434 << " -- notifying VPNProvider.";
435 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
436 interface_index) &&
437 technology == Technology::kTunnel) {
438 // If VPN does not know anything about this tunnel, it is probably
439 // left over from a previous instance and should not exist.
440 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
441 DeleteInterface(interface_index);
442 }
443 break;
444 case Technology::kLoopback:
445 // Loopback devices are largely ignored, but we should make sure the
446 // link is enabled.
447 SLOG(Device, 2) << "Bringing up loopback device " << link_name
448 << " at index " << interface_index;
449 rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP);
450 return NULL;
Paul Stewart050cfc02012-07-06 20:38:54 -0700451 case Technology::kCDCEthernet:
452 // CDCEnternet devices are of indeterminate type when they are
453 // initially created. Some time later, tty devices may or may
454 // not appear under the same USB device root, which will identify
455 // it as a modem. Alternatively, ModemManager may discover the
456 // device and create and register a Cellular device. In either
457 // case, we should delay creating a Device until we can make a
458 // better determination of what type this Device should be.
459 LOG(INFO) << "Delaying creation of device for " << link_name
460 << " at index " << interface_index;
461 DelayDeviceCreation(interface_index);
462 return NULL;
Paul Stewart8c116a92012-05-02 18:30:03 -0700463 default:
464 // We will not manage this device in shill. Do not create a device
465 // object or do anything to change its state. We create a stub object
466 // which is useful for testing.
467 return new DeviceStub(control_interface_, dispatcher_, metrics_,
468 manager_, link_name, address, interface_index,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700469 technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700470 }
471
472 // Reset the routing table and addresses.
473 routing_table_->FlushRoutes(interface_index);
474 FlushAddresses(interface_index);
475
Ben Chan5086b972013-01-15 21:51:38 -0800476 manager_->UpdateUninitializedTechnologies();
477
Paul Stewart8c116a92012-05-02 18:30:03 -0700478 return device;
479}
480
Chris Masone2aa97072011-08-09 17:35:08 -0700481void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700482 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
483 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700484 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700485 Technology::Identifier technology = Technology::kUnknown;
Darin Petkove6193c02011-08-11 12:42:40 -0700486 unsigned int flags = msg.link_status().flags;
487 unsigned int change = msg.link_status().change;
Paul Stewart8c116a92012-05-02 18:30:03 -0700488 bool new_device =
489 !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700490 SLOG(Device, 2) << __func__ << "(index=" << dev_index
491 << std::showbase << std::hex
492 << ", flags=" << flags << ", change=" << change << ")"
493 << std::dec << std::noshowbase
494 << ", new_device=" << new_device;
Paul Stewart8c116a92012-05-02 18:30:03 -0700495 infos_[dev_index].has_addresses_only = false;
Darin Petkove6193c02011-08-11 12:42:40 -0700496 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700497
Paul Stewart1ac4e842012-07-10 12:58:12 -0700498 RetrieveLinkStatistics(dev_index, msg);
499
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700500 DeviceRefPtr device = GetDevice(dev_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700501 if (new_device) {
502 CHECK(!device);
Chris Masone2aa97072011-08-09 17:35:08 -0700503 if (!msg.HasAttribute(IFLA_IFNAME)) {
504 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
505 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700506 }
Chris Masone2aa97072011-08-09 17:35:08 -0700507 ByteString b(msg.GetAttribute(IFLA_IFNAME));
508 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700509 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200510 infos_[dev_index].name = link_name;
511 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700512
Chris Masone2aa97072011-08-09 17:35:08 -0700513 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700514 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700515 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700516 } else {
517 technology = GetDeviceTechnology(link_name);
518 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700519 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800520 string address;
521 if (msg.HasAttribute(IFLA_ADDRESS)) {
522 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
523 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700524 SLOG(Device, 2) << "link index " << dev_index << " address "
525 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700526 } else if (technology != Technology::kTunnel &&
527 technology != Technology::kPPP) {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800528 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
529 return;
530 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700531 device = CreateDevice(link_name, address, dev_index, technology);
532 if (device) {
533 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700534 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700535 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700536 if (device) {
537 device->LinkEvent(flags, change);
538 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700539}
540
Chris Masone2aa97072011-08-09 17:35:08 -0700541void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700542 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700543
Paul Stewart9a908082011-08-31 12:18:48 -0700544 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
545 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700546 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
547 << std::showbase << std::hex
548 << ", flags=" << msg.link_status().flags
549 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700550 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700551}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700552
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700553DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
554 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700555 return info ? info->device : NULL;
556}
557
Darin Petkovf8046b82012-04-24 16:29:23 +0200558int DeviceInfo::GetIndex(const string &interface_name) const {
559 map<string, int>::const_iterator it = indices_.find(interface_name);
560 return it == indices_.end() ? -1 : it->second;
561}
562
Paul Stewart32852962011-08-30 14:06:53 -0700563bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700564 const Info *info = GetInfo(interface_index);
565 if (!info) {
566 return false;
567 }
Gary Morainbf74a672012-07-30 16:27:19 -0700568 // |mac_address| from RTNL is not used for some devices, in which case it will
569 // be empty here.
570 if (!info->mac_address.IsEmpty()) {
571 *address = info->mac_address;
572 return true;
573 }
574
575 // Ask the kernel for the MAC address.
576 *address = GetMACAddressFromKernel(interface_index);
577 return !address->IsEmpty();
578}
579
580ByteString DeviceInfo::GetMACAddressFromKernel(int interface_index) const {
Gary Morain41780232012-07-31 15:08:31 -0700581 const Info *info = GetInfo(interface_index);
582 if (!info) {
583 return ByteString();
584 }
585
586 const int fd = sockets_->Socket(PF_INET, SOCK_DGRAM, 0);
Gary Morainbf74a672012-07-30 16:27:19 -0700587 if (fd < 0) {
588 LOG(ERROR) << __func__ << ": Unable to open socket: " << fd;
Gary Morain41780232012-07-31 15:08:31 -0700589 return ByteString();
Gary Morainbf74a672012-07-30 16:27:19 -0700590 }
Gary Morain41780232012-07-31 15:08:31 -0700591
592 ScopedSocketCloser socket_closer(sockets_.get(), fd);
Gary Morainbf74a672012-07-30 16:27:19 -0700593 struct ifreq ifr;
594 memset(&ifr, 0, sizeof(ifr));
595 ifr.ifr_ifindex = interface_index;
Gary Morain41780232012-07-31 15:08:31 -0700596 strcpy(ifr.ifr_ifrn.ifrn_name, info->name.c_str());
597 int err = sockets_->Ioctl(fd, SIOCGIFHWADDR, &ifr);
Gary Morainbf74a672012-07-30 16:27:19 -0700598 if (err < 0) {
599 LOG(ERROR) << __func__ << ": Unable to read MAC address: " << errno;
Gary Morain41780232012-07-31 15:08:31 -0700600 return ByteString();
Gary Morainbf74a672012-07-30 16:27:19 -0700601 }
Gary Morain41780232012-07-31 15:08:31 -0700602
603 return ByteString(ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700604}
605
Paul Stewart9a908082011-08-31 12:18:48 -0700606bool DeviceInfo::GetAddresses(int interface_index,
607 vector<AddressData> *addresses) const {
608 const Info *info = GetInfo(interface_index);
609 if (!info) {
610 return false;
611 }
612 *addresses = info->ip_addresses;
613 return true;
614}
615
616void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700617 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700618 const Info *info = GetInfo(interface_index);
619 if (!info) {
620 return;
621 }
622 const vector<AddressData> &addresses = info->ip_addresses;
623 vector<AddressData>::const_iterator iter;
624 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700625 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700626 (iter->scope == RT_SCOPE_UNIVERSE &&
627 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700628 SLOG(Device, 2) << __func__ << ": removing ip address "
629 << iter->address.ToString()
630 << " from interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700631 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
632 }
633 }
634}
635
Paul Stewart05a42c22012-08-02 16:47:21 -0700636bool DeviceInfo::HasOtherAddress(
637 int interface_index, const IPAddress &this_address) const {
638 SLOG(Device, 3) << __func__ << "(" << interface_index << ")";
639 const Info *info = GetInfo(interface_index);
640 if (!info) {
641 return false;
642 }
643 const vector<AddressData> &addresses = info->ip_addresses;
644 vector<AddressData>::const_iterator iter;
645 bool has_other_address = false;
646 bool has_this_address = false;
647 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
648 if (iter->address.family() != this_address.family()) {
649 continue;
650 }
651 if (iter->address.address().Equals(this_address.address())) {
652 has_this_address = true;
653 } else if (this_address.family() == IPAddress::kFamilyIPv4) {
654 has_other_address = true;
655 } else if ((iter->scope == RT_SCOPE_UNIVERSE &&
656 (iter->flags & IFA_F_TEMPORARY) == 0)) {
657 has_other_address = true;
658 }
659 }
660 return has_other_address && !has_this_address;
661}
662
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700663bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
664 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700665 if (!info) {
666 return false;
667 }
668 *flags = info->flags;
669 return true;
670}
671
Paul Stewart1ac4e842012-07-10 12:58:12 -0700672bool DeviceInfo::GetByteCounts(int interface_index,
673 uint64 *rx_bytes,
674 uint64 *tx_bytes) const {
675 const Info *info = GetInfo(interface_index);
676 if (!info) {
677 return false;
678 }
679 *rx_bytes = info->rx_bytes;
680 *tx_bytes = info->tx_bytes;
681 return true;
682}
683
Paul Stewartca6abd42012-03-01 15:45:29 -0800684bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800685 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
686 if (fd < 0) {
687 PLOG(ERROR) << "failed to open " << kTunDeviceName;
688 return false;
689 }
690 file_util::ScopedFD scoped_fd(&fd);
691
692 struct ifreq ifr;
693 memset(&ifr, 0, sizeof(ifr));
694 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
695 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
696 PLOG(ERROR) << "failed to create tunnel interface";
697 return false;
698 }
699
700 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
701 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
702 return false;
703 }
704
705 *interface_name = string(ifr.ifr_name);
706
707 return true;
708}
709
Paul Stewartca6abd42012-03-01 15:45:29 -0800710bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800711 return rtnl_handler_->RemoveInterface(interface_index);
712}
713
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700714const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
715 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700716 if (iter == infos_.end()) {
717 return NULL;
718 }
719 return &iter->second;
720}
721
722void DeviceInfo::RemoveInfo(int interface_index) {
723 map<int, Info>::iterator iter = infos_.find(interface_index);
724 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700725 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700726 if (iter->second.device.get()) {
727 manager_->DeregisterDevice(iter->second.device);
728 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200729 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700730 infos_.erase(iter);
Paul Stewart050cfc02012-07-06 20:38:54 -0700731 delayed_devices_.erase(interface_index);
mukesh agrawal47009f82011-08-25 14:07:35 -0700732 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700733 SLOG(Device, 2) << __func__ << ": Unknown device index: "
734 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700735 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700736}
737
Chris Masone2aa97072011-08-09 17:35:08 -0700738void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700739 DCHECK(msg.type() == RTNLMessage::kTypeLink);
740 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700741 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700742 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700743 DelLinkMsgHandler(msg);
744 } else {
745 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700746 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700747}
748
Paul Stewart9a908082011-08-31 12:18:48 -0700749void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700750 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700751 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
752 int interface_index = msg.interface_index();
753 if (!ContainsKey(infos_, interface_index)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700754 SLOG(Device, 2) << "Got advance address information for unknown index "
755 << interface_index;
756 infos_[interface_index].has_addresses_only = true;
Paul Stewart9a908082011-08-31 12:18:48 -0700757 }
758 const RTNLMessage::AddressStatus &status = msg.address_status();
759 IPAddress address(msg.family(),
Ben Chan682c5d42012-06-18 14:11:04 -0700760 msg.HasAttribute(IFA_LOCAL) ?
761 msg.GetAttribute(IFA_LOCAL) : msg.GetAttribute(IFA_ADDRESS),
Paul Stewart9a908082011-08-31 12:18:48 -0700762 status.prefix_len);
763
Ben Chan682c5d42012-06-18 14:11:04 -0700764 SLOG_IF(Device, 2, msg.HasAttribute(IFA_LOCAL))
765 << "Found local address attribute for interface " << interface_index;
766
Paul Stewart9a908082011-08-31 12:18:48 -0700767 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
768 vector<AddressData>::iterator iter;
769 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
770 if (address.Equals(iter->address)) {
771 break;
772 }
773 }
774 if (iter != address_list.end()) {
775 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700776 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700777 address_list.erase(iter);
778 } else {
779 iter->flags = status.flags;
780 iter->scope = status.scope;
781 }
782 } else if (msg.mode() == RTNLMessage::kModeAdd) {
783 address_list.push_back(AddressData(address, status.flags, status.scope));
Paul Stewart8c116a92012-05-02 18:30:03 -0700784 SLOG(Device, 2) << "Add address " << address.ToString()
785 << " for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700786 }
787}
788
Paul Stewart050cfc02012-07-06 20:38:54 -0700789void DeviceInfo::DelayDeviceCreation(int interface_index) {
790 delayed_devices_.insert(interface_index);
791 delayed_devices_callback_.Reset(
792 Bind(&DeviceInfo::DelayedDeviceCreationTask, AsWeakPtr()));
793 dispatcher_->PostDelayedTask(delayed_devices_callback_.callback(),
794 kDelayedDeviceCreationSeconds * 1000);
795}
796
797// Re-evaluate the technology type for each delayed device.
798void DeviceInfo::DelayedDeviceCreationTask() {
799 while (!delayed_devices_.empty()) {
800 set<int>::iterator it = delayed_devices_.begin();
801 int dev_index = *it;
802 delayed_devices_.erase(it);
803
804 DCHECK(ContainsKey(infos_, dev_index));
805 DCHECK(!GetDevice(dev_index));
806
807 const string &link_name = infos_[dev_index].name;
808 Technology::Identifier technology = GetDeviceTechnology(link_name);
809
810 if (technology == Technology::kCDCEthernet) {
811 LOG(INFO) << "In " << __func__ << ": device " << link_name
812 << " is now assumed to be regular Ethernet.";
813 technology = Technology::kEthernet;
814 } else if (technology != Technology::kCellular) {
815 LOG(WARNING) << "In " << __func__ << ": device " << link_name
816 << " is unexpected technology "
817 << Technology::NameFromIdentifier(technology);
818 }
819 string address =
820 StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
821 DCHECK(!address.empty());
822
823 DeviceRefPtr device = CreateDevice(link_name, address, dev_index,
824 technology);
825 if (device) {
826 RegisterDevice(device);
827 }
828 }
829}
830
Paul Stewart1ac4e842012-07-10 12:58:12 -0700831void DeviceInfo::RetrieveLinkStatistics(int interface_index,
832 const RTNLMessage &msg) {
833 if (!msg.HasAttribute(IFLA_STATS64)) {
834 return;
835 }
836 ByteString stats_bytes(msg.GetAttribute(IFLA_STATS64));
837 struct rtnl_link_stats64 stats;
838 if (stats_bytes.GetLength() < sizeof(stats)) {
839 LOG(WARNING) << "Link statistics size is too small: "
840 << stats_bytes.GetLength() << " < " << sizeof(stats);
841 return;
842 }
843
844 memcpy(&stats, stats_bytes.GetConstData(), sizeof(stats));
845 SLOG(Device, 2) << "Link statistics for "
846 << " interface index " << interface_index << ": "
847 << "receive: " << stats.rx_bytes << "; "
848 << "transmit: " << stats.tx_bytes << ".";
849 infos_[interface_index].rx_bytes = stats.rx_bytes;
850 infos_[interface_index].tx_bytes = stats.tx_bytes;
851}
852
853void DeviceInfo::RequestLinkStatistics() {
854 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink);
855 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
856 kRequestLinkStatisticsIntervalSeconds * 1000);
857}
858
Paul Stewart0af98bf2011-05-10 17:38:08 -0700859} // namespace shill