blob: e8d44ee0f743d1b4147205c5caadaebe1000341a [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/logging.h>
26#include <base/memory/scoped_ptr.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050027#include <base/stl_util.h>
Paul Stewart2001a422011-12-15 10:20:09 -080028#include <base/string_number_conversions.h>
Chris Masone877ff982011-09-21 16:18:24 -070029#include <base/string_util.h>
Paul Stewartb50f0b92011-05-16 16:31:42 -070030#include <base/stringprintf.h>
Paul Stewart0af98bf2011-05-10 17:38:08 -070031
32#include "shill/control_interface.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070033#include "shill/device.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070034#include "shill/device_stub.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070035#include "shill/ethernet.h"
36#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"
Ben Chanfad4a0b2012-04-18 15:49:59 -070041#include "shill/scope_logger.h"
Chris Masone487b8bf2011-05-13 16:27:57 -070042#include "shill/service.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";
Paul Stewartca876ee2012-04-21 08:55:58 -070058const char DeviceInfo::kDeviceInfoRoot[] = "/sys/class/net";
Ben Chan4e64d2d2012-05-16 00:02:25 -070059const char DeviceInfo::kDriverCdcEther[] = "cdc_ether";
60const char DeviceInfo::kDriverGdmWiMax[] = "gdm_wimax";
mukesh agrawal93a29ed2012-04-17 16:13:01 -070061const char DeviceInfo::kDriverVirtioNet[] = "virtio_net";
Paul Stewartca876ee2012-04-21 08:55:58 -070062const char DeviceInfo::kInterfaceUevent[] = "uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080063const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Paul Stewartca876ee2012-04-21 08:55:58 -070064const char DeviceInfo::kInterfaceDevice[] = "device";
65const char DeviceInfo::kInterfaceDriver[] = "device/driver";
66const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags";
67const char DeviceInfo::kInterfaceType[] = "type";
Paul Stewartb50f0b92011-05-16 16:31:42 -070068const char *DeviceInfo::kModemDrivers[] = {
69 "gobi",
70 "QCUSBNet2k",
Thieu Le8f1c8352012-04-16 11:02:12 -070071 "GobiNet"
Paul Stewartb50f0b92011-05-16 16:31:42 -070072};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080073const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewart050cfc02012-07-06 20:38:54 -070074const int DeviceInfo::kDelayedDeviceCreationSeconds = 5;
Paul Stewart1ac4e842012-07-10 12:58:12 -070075const int DeviceInfo::kRequestLinkStatisticsIntervalSeconds = 60;
Paul Stewartb50f0b92011-05-16 16:31:42 -070076
77DeviceInfo::DeviceInfo(ControlInterface *control_interface,
78 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080079 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070080 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070081 : control_interface_(control_interface),
82 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080083 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070084 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050085 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
86 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070087 link_listener_(NULL),
88 address_listener_(NULL),
Paul Stewartca876ee2012-04-21 08:55:58 -070089 device_info_root_(kDeviceInfoRoot),
Paul Stewart8c116a92012-05-02 18:30:03 -070090 routing_table_(RoutingTable::GetInstance()),
Paul Stewart9a908082011-08-31 12:18:48 -070091 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070092}
93
Paul Stewarta3c56f92011-05-26 07:08:52 -070094DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070095
mukesh agrawal8f317b62011-07-15 11:53:23 -070096void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
97 black_list_.insert(device_name);
98}
99
Eric Shienbrood5e628a52012-03-21 16:56:59 -0400100bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
101 return ContainsKey(black_list_, device_name);
102}
103
Paul Stewarta3c56f92011-05-26 07:08:52 -0700104void DeviceInfo::Start() {
105 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500106 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700107 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500108 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700109 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
110 RTNLHandler::kRequestAddr);
Paul Stewart1ac4e842012-07-10 12:58:12 -0700111 request_link_statistics_callback_.Reset(
112 Bind(&DeviceInfo::RequestLinkStatistics, AsWeakPtr()));
113 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
114 kRequestLinkStatisticsIntervalSeconds * 1000);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700115}
116
Paul Stewarta3c56f92011-05-26 07:08:52 -0700117void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700118 link_listener_.reset();
119 address_listener_.reset();
Paul Stewart8c116a92012-05-02 18:30:03 -0700120 infos_.clear();
Paul Stewart1ac4e842012-07-10 12:58:12 -0700121 request_link_statistics_callback_.Cancel();
Paul Stewart050cfc02012-07-06 20:38:54 -0700122 delayed_devices_callback_.Cancel();
123 delayed_devices_.clear();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700124}
125
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700126void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700127 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
128 << device->interface_index() << ")";
Paul Stewart050cfc02012-07-06 20:38:54 -0700129 delayed_devices_.erase(device->interface_index());
Darin Petkove6193c02011-08-11 12:42:40 -0700130 CHECK(!GetDevice(device->interface_index()).get());
131 infos_[device->interface_index()].device = device;
Joshua Krollda798622012-06-05 12:30:48 -0700132 if ((device->technology() == Technology::kCellular) ||
133 (device->technology() == Technology::kEthernet) ||
134 (device->technology() == Technology::kWifi) ||
135 (device->technology() == Technology::kWiMax)) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700136 manager_->RegisterDevice(device);
137 }
138}
139
Jason Glasgowe9089492012-02-23 17:57:37 -0500140void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
141 int interface_index = device->interface_index();
142
Ben Chanfad4a0b2012-04-18 15:49:59 -0700143 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
144 << interface_index << ")";
Joshua Krollda798622012-06-05 12:30:48 -0700145 CHECK((device->technology() == Technology::kCellular) ||
146 (device->technology() == Technology::kWiMax));
Jason Glasgowe9089492012-02-23 17:57:37 -0500147
148 // Release reference to the device
149 map<int, Info>::iterator iter = infos_.find(interface_index);
150 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700151 SLOG(Device, 2) << "Removing device from info for index: "
152 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500153 manager_->DeregisterDevice(device);
154 // Release the reference to the device, but maintain the mapping
155 // for the index. That will be cleaned up by an RTNL message.
156 iter->second.device = NULL;
157 }
158}
159
Paul Stewartca876ee2012-04-21 08:55:58 -0700160FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name,
161 const string &path_name) {
162 return device_info_root_.Append(iface_name).Append(path_name);
163}
164
165bool DeviceInfo::GetDeviceInfoContents(const string &iface_name,
166 const string &path_name,
167 string *contents_out) {
168 return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
169 contents_out);
170}
171bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700172 const string &path_name,
173 FilePath *path_out) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700174 return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
175 path_out);
176}
177
Paul Stewartfdd16072011-09-16 12:41:35 -0700178Technology::Identifier DeviceInfo::GetDeviceTechnology(
179 const string &iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700180 string type_string;
181 int arp_type = ARPHRD_VOID;;
182 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
183 TrimString(type_string, "\n", &type_string) &&
184 !base::StringToInt(type_string, &arp_type)) {
185 arp_type = ARPHRD_VOID;
186 }
187
Paul Stewart9364c4c2011-12-06 17:12:42 -0800188 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700189 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Paul Stewart050cfc02012-07-06 20:38:54 -0700190 LOG(INFO) << StringPrintf("%s: device %s has no uevent file",
191 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700192 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800193 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700194
Thieu Le8f1c8352012-04-16 11:02:12 -0700195 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
196 // start of the file or after a newline, we can safely assume this
197 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800198 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700199 SLOG(Device, 2)
200 << StringPrintf("%s: device %s has wifi signature in uevent file",
201 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700202 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700203 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
204 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800205 return Technology::kWiFiMonitor;
206 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700207 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700208 }
209
Jason Glasgow7904d142012-05-18 13:36:20 -0400210 // Special case for pseudo modems which are used for testing
211 if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) {
212 SLOG(Device, 2) << StringPrintf(
213 "%s: device %s is a pseudo modem for testing",
214 __func__, iface_name.c_str());
215 return Technology::kCellular;
216 }
217
Paul Stewart9364c4c2011-12-06 17:12:42 -0800218 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700219 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700220 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
221 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700222 if (arp_type == ARPHRD_LOOPBACK) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700223 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
224 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700225 return Technology::kLoopback;
226 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700227 if (arp_type == ARPHRD_PPP) {
228 SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device",
229 __func__, iface_name.c_str());
230 return Technology::kPPP;
231 }
232 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800233 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700234 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
235 TrimString(tun_flags_str, "\n", &tun_flags_str) &&
236 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800237 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700238 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
239 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800240 return Technology::kTunnel;
241 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700242 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800243 }
244
245 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700246 // See if driver for this interface is in a list of known modem driver names.
247 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
248 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800249 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700250 SLOG(Device, 2)
251 << StringPrintf("%s: device %s is matched with modem driver %s",
252 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800253 return Technology::kCellular;
254 }
255 }
256
Ben Chan4e64d2d2012-05-16 00:02:25 -0700257 if (driver_name == kDriverGdmWiMax) {
258 SLOG(Device, 2) << StringPrintf("%s: device %s is a WiMAX device",
259 __func__, iface_name.c_str());
260 return Technology::kWiMax;
261 }
262
Thieu Le8f1c8352012-04-16 11:02:12 -0700263 // For cdc_ether devices, make sure it's a modem because this driver
264 // can be used for other ethernet devices.
Paul Stewart050cfc02012-07-06 20:38:54 -0700265 if (driver_name == kDriverCdcEther) {
266 if (IsCdcEtherModemDevice(iface_name)) {
267 LOG(INFO) << StringPrintf("%s: device %s is a modem cdc_ether "
268 "device", __func__, iface_name.c_str());
269 return Technology::kCellular;
270 }
271 SLOG(Device, 2) << StringPrintf("%s: device %s is a cdc_ether "
272 "device", __func__, iface_name.c_str());
273 return Technology::kCDCEthernet;
Thieu Le8f1c8352012-04-16 11:02:12 -0700274 }
275
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700276 // Special case for the virtio driver, used when run under KVM. See also
277 // the comment in VirtioEthernet::Start.
278 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700279 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
280 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700281 return Technology::kVirtioEthernet;
282 }
283
Ben Chanfad4a0b2012-04-18 15:49:59 -0700284 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
285 "is defaulted to type ethernet",
286 __func__, iface_name.c_str(),
287 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700288 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700289}
290
Thieu Le8f1c8352012-04-16 11:02:12 -0700291bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
292 // A cdc_ether device is a modem device if it also exposes tty interfaces.
293 // To determine this, we look for the existence of the tty interface in the
294 // USB device sysfs tree.
295 //
296 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
297 // follows:
298 //
299 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
300 // 1-2:1.0
301 // tty
302 // ttyACM0
303 // 1-2:1.1
304 // net
305 // usb0
306 // 1-2:1.2
307 // tty
308 // ttyACM1
309 // ...
310 //
311 // /sys/class/net/usb0/device symlinks to
312 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700313 //
314 // Note that some modem devices have the tty directory one level deeper
315 // (eg. E362), so the device tree for the tty interface is:
316 // /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 -0700317
Paul Stewartca876ee2012-04-21 08:55:58 -0700318 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700319 FilePath device_path;
320 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700321 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
322 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700323 return false;
324 }
325 if (!device_path.IsAbsolute()) {
326 device_path = device_file.DirName().Append(device_path);
327 file_util::AbsolutePath(&device_path);
328 }
329
330 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700331 // USB device and see if there's a subdirectory "tty" inside. In other
332 // words, using the example dir hierarchy above, find
333 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700334 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700335 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700336}
337
338// static
Thieu Leb27beee2012-04-20 09:19:06 -0700339bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
340 file_util::FileEnumerator::FileType type =
341 static_cast<file_util::FileEnumerator::FileType>(
342 file_util::FileEnumerator::DIRECTORIES |
343 file_util::FileEnumerator::SHOW_SYM_LINKS);
344 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700345 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
346 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700347 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700348 return true;
349 }
350 return false;
351}
352
Paul Stewart8c116a92012-05-02 18:30:03 -0700353DeviceRefPtr DeviceInfo::CreateDevice(const string &link_name,
354 const string &address,
355 int interface_index,
356 Technology::Identifier technology) {
357 DeviceRefPtr device;
Paul Stewart050cfc02012-07-06 20:38:54 -0700358 delayed_devices_.erase(interface_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700359
360 switch (technology) {
361 case Technology::kCellular:
362 // Cellular devices are managed by ModemInfo.
363 SLOG(Device, 2) << "Cellular link " << link_name
364 << " at index " << interface_index
365 << " -- notifying ModemInfo.";
366 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
367 break;
368 case Technology::kEthernet:
369 device = new Ethernet(control_interface_, dispatcher_, metrics_,
370 manager_, link_name, address, interface_index);
371 device->EnableIPv6Privacy();
372 break;
373 case Technology::kVirtioEthernet:
374 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
375 manager_, link_name, address,
376 interface_index);
377 device->EnableIPv6Privacy();
378 break;
379 case Technology::kWifi:
380 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
381 link_name, address, interface_index);
382 device->EnableIPv6Privacy();
383 break;
Ben Chan4e64d2d2012-05-16 00:02:25 -0700384 case Technology::kWiMax:
Darin Petkove4b27022012-05-16 13:28:50 +0200385 // WiMax devices are managed by WiMaxProvider.
386 SLOG(Device, 2) << "WiMax link " << link_name
387 << " at index " << interface_index
388 << " -- notifying WiMaxProvider.";
389 manager_->wimax_provider()->OnDeviceInfoAvailable(link_name);
Ben Chan4e64d2d2012-05-16 00:02:25 -0700390 break;
Paul Stewart8c116a92012-05-02 18:30:03 -0700391 case Technology::kPPP:
392 case Technology::kTunnel:
393 // Tunnel and PPP devices are managed by the VPN code (PPP for
394 // l2tpipsec). Notify the VPN Provider of the interface's presence.
395 // Since CreateDevice is only called once in the lifetime of an
396 // interface index, this notification will only occur the first
397 // time the device is seen.
398 SLOG(Device, 2) << "Tunnel / PPP link " << link_name
399 << " at index " << interface_index
400 << " -- notifying VPNProvider.";
401 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
402 interface_index) &&
403 technology == Technology::kTunnel) {
404 // If VPN does not know anything about this tunnel, it is probably
405 // left over from a previous instance and should not exist.
406 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
407 DeleteInterface(interface_index);
408 }
409 break;
410 case Technology::kLoopback:
411 // Loopback devices are largely ignored, but we should make sure the
412 // link is enabled.
413 SLOG(Device, 2) << "Bringing up loopback device " << link_name
414 << " at index " << interface_index;
415 rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP);
416 return NULL;
Paul Stewart050cfc02012-07-06 20:38:54 -0700417 case Technology::kCDCEthernet:
418 // CDCEnternet devices are of indeterminate type when they are
419 // initially created. Some time later, tty devices may or may
420 // not appear under the same USB device root, which will identify
421 // it as a modem. Alternatively, ModemManager may discover the
422 // device and create and register a Cellular device. In either
423 // case, we should delay creating a Device until we can make a
424 // better determination of what type this Device should be.
425 LOG(INFO) << "Delaying creation of device for " << link_name
426 << " at index " << interface_index;
427 DelayDeviceCreation(interface_index);
428 return NULL;
Paul Stewart8c116a92012-05-02 18:30:03 -0700429 default:
430 // We will not manage this device in shill. Do not create a device
431 // object or do anything to change its state. We create a stub object
432 // which is useful for testing.
433 return new DeviceStub(control_interface_, dispatcher_, metrics_,
434 manager_, link_name, address, interface_index,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700435 technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700436 }
437
438 // Reset the routing table and addresses.
439 routing_table_->FlushRoutes(interface_index);
440 FlushAddresses(interface_index);
441
442 return device;
443}
444
Chris Masone2aa97072011-08-09 17:35:08 -0700445void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700446 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
447 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700448 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700449 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700450
Darin Petkove6193c02011-08-11 12:42:40 -0700451 unsigned int flags = msg.link_status().flags;
452 unsigned int change = msg.link_status().change;
Paul Stewart8c116a92012-05-02 18:30:03 -0700453 bool new_device =
454 !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700455 SLOG(Device, 2) << __func__ << "(index=" << dev_index
456 << std::showbase << std::hex
457 << ", flags=" << flags << ", change=" << change << ")"
458 << std::dec << std::noshowbase
459 << ", new_device=" << new_device;
Paul Stewart8c116a92012-05-02 18:30:03 -0700460 infos_[dev_index].has_addresses_only = false;
Darin Petkove6193c02011-08-11 12:42:40 -0700461 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700462
Paul Stewart1ac4e842012-07-10 12:58:12 -0700463 RetrieveLinkStatistics(dev_index, msg);
464
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700465 DeviceRefPtr device = GetDevice(dev_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700466 if (new_device) {
467 CHECK(!device);
Chris Masone2aa97072011-08-09 17:35:08 -0700468 if (!msg.HasAttribute(IFLA_IFNAME)) {
469 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
470 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700471 }
Chris Masone2aa97072011-08-09 17:35:08 -0700472 ByteString b(msg.GetAttribute(IFLA_IFNAME));
473 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700474 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200475 infos_[dev_index].name = link_name;
476 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700477
Chris Masone2aa97072011-08-09 17:35:08 -0700478 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700479 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700480 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700481 } else {
482 technology = GetDeviceTechnology(link_name);
483 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700484 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800485 string address;
486 if (msg.HasAttribute(IFLA_ADDRESS)) {
487 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
488 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700489 SLOG(Device, 2) << "link index " << dev_index << " address "
490 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700491 } else if (technology != Technology::kTunnel &&
492 technology != Technology::kPPP) {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800493 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
494 return;
495 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700496 device = CreateDevice(link_name, address, dev_index, technology);
497 if (device) {
498 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700499 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700500 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700501 if (device) {
502 device->LinkEvent(flags, change);
503 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700504}
505
Chris Masone2aa97072011-08-09 17:35:08 -0700506void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700507 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700508
Paul Stewart9a908082011-08-31 12:18:48 -0700509 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
510 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700511 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
512 << std::showbase << std::hex
513 << ", flags=" << msg.link_status().flags
514 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700515 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700516}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700517
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700518DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
519 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700520 return info ? info->device : NULL;
521}
522
Darin Petkovf8046b82012-04-24 16:29:23 +0200523int DeviceInfo::GetIndex(const string &interface_name) const {
524 map<string, int>::const_iterator it = indices_.find(interface_name);
525 return it == indices_.end() ? -1 : it->second;
526}
527
Paul Stewart32852962011-08-30 14:06:53 -0700528bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700529 const Info *info = GetInfo(interface_index);
530 if (!info) {
531 return false;
532 }
Paul Stewart32852962011-08-30 14:06:53 -0700533 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700534 return true;
535}
536
Paul Stewart9a908082011-08-31 12:18:48 -0700537bool DeviceInfo::GetAddresses(int interface_index,
538 vector<AddressData> *addresses) const {
539 const Info *info = GetInfo(interface_index);
540 if (!info) {
541 return false;
542 }
543 *addresses = info->ip_addresses;
544 return true;
545}
546
547void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700548 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700549 const Info *info = GetInfo(interface_index);
550 if (!info) {
551 return;
552 }
553 const vector<AddressData> &addresses = info->ip_addresses;
554 vector<AddressData>::const_iterator iter;
555 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700556 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700557 (iter->scope == RT_SCOPE_UNIVERSE &&
558 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700559 SLOG(Device, 2) << __func__ << ": removing ip address "
560 << iter->address.ToString()
561 << " from interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700562 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
563 }
564 }
565}
566
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700567bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
568 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700569 if (!info) {
570 return false;
571 }
572 *flags = info->flags;
573 return true;
574}
575
Paul Stewart1ac4e842012-07-10 12:58:12 -0700576bool DeviceInfo::GetByteCounts(int interface_index,
577 uint64 *rx_bytes,
578 uint64 *tx_bytes) const {
579 const Info *info = GetInfo(interface_index);
580 if (!info) {
581 return false;
582 }
583 *rx_bytes = info->rx_bytes;
584 *tx_bytes = info->tx_bytes;
585 return true;
586}
587
Paul Stewartca6abd42012-03-01 15:45:29 -0800588bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800589 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
590 if (fd < 0) {
591 PLOG(ERROR) << "failed to open " << kTunDeviceName;
592 return false;
593 }
594 file_util::ScopedFD scoped_fd(&fd);
595
596 struct ifreq ifr;
597 memset(&ifr, 0, sizeof(ifr));
598 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
599 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
600 PLOG(ERROR) << "failed to create tunnel interface";
601 return false;
602 }
603
604 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
605 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
606 return false;
607 }
608
609 *interface_name = string(ifr.ifr_name);
610
611 return true;
612}
613
Paul Stewartca6abd42012-03-01 15:45:29 -0800614bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800615 return rtnl_handler_->RemoveInterface(interface_index);
616}
617
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700618const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
619 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700620 if (iter == infos_.end()) {
621 return NULL;
622 }
623 return &iter->second;
624}
625
626void DeviceInfo::RemoveInfo(int interface_index) {
627 map<int, Info>::iterator iter = infos_.find(interface_index);
628 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700629 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700630 if (iter->second.device.get()) {
631 manager_->DeregisterDevice(iter->second.device);
632 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200633 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700634 infos_.erase(iter);
Paul Stewart050cfc02012-07-06 20:38:54 -0700635 delayed_devices_.erase(interface_index);
mukesh agrawal47009f82011-08-25 14:07:35 -0700636 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700637 SLOG(Device, 2) << __func__ << ": Unknown device index: "
638 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700639 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700640}
641
Chris Masone2aa97072011-08-09 17:35:08 -0700642void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700643 DCHECK(msg.type() == RTNLMessage::kTypeLink);
644 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700645 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700646 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700647 DelLinkMsgHandler(msg);
648 } else {
649 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700650 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700651}
652
Paul Stewart9a908082011-08-31 12:18:48 -0700653void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700654 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700655 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
656 int interface_index = msg.interface_index();
657 if (!ContainsKey(infos_, interface_index)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700658 SLOG(Device, 2) << "Got advance address information for unknown index "
659 << interface_index;
660 infos_[interface_index].has_addresses_only = true;
Paul Stewart9a908082011-08-31 12:18:48 -0700661 }
662 const RTNLMessage::AddressStatus &status = msg.address_status();
663 IPAddress address(msg.family(),
Ben Chan682c5d42012-06-18 14:11:04 -0700664 msg.HasAttribute(IFA_LOCAL) ?
665 msg.GetAttribute(IFA_LOCAL) : msg.GetAttribute(IFA_ADDRESS),
Paul Stewart9a908082011-08-31 12:18:48 -0700666 status.prefix_len);
667
Ben Chan682c5d42012-06-18 14:11:04 -0700668 SLOG_IF(Device, 2, msg.HasAttribute(IFA_LOCAL))
669 << "Found local address attribute for interface " << interface_index;
670
Paul Stewart9a908082011-08-31 12:18:48 -0700671 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
672 vector<AddressData>::iterator iter;
673 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
674 if (address.Equals(iter->address)) {
675 break;
676 }
677 }
678 if (iter != address_list.end()) {
679 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700680 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700681 address_list.erase(iter);
682 } else {
683 iter->flags = status.flags;
684 iter->scope = status.scope;
685 }
686 } else if (msg.mode() == RTNLMessage::kModeAdd) {
687 address_list.push_back(AddressData(address, status.flags, status.scope));
Paul Stewart8c116a92012-05-02 18:30:03 -0700688 SLOG(Device, 2) << "Add address " << address.ToString()
689 << " for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700690 }
691}
692
Paul Stewart050cfc02012-07-06 20:38:54 -0700693void DeviceInfo::DelayDeviceCreation(int interface_index) {
694 delayed_devices_.insert(interface_index);
695 delayed_devices_callback_.Reset(
696 Bind(&DeviceInfo::DelayedDeviceCreationTask, AsWeakPtr()));
697 dispatcher_->PostDelayedTask(delayed_devices_callback_.callback(),
698 kDelayedDeviceCreationSeconds * 1000);
699}
700
701// Re-evaluate the technology type for each delayed device.
702void DeviceInfo::DelayedDeviceCreationTask() {
703 while (!delayed_devices_.empty()) {
704 set<int>::iterator it = delayed_devices_.begin();
705 int dev_index = *it;
706 delayed_devices_.erase(it);
707
708 DCHECK(ContainsKey(infos_, dev_index));
709 DCHECK(!GetDevice(dev_index));
710
711 const string &link_name = infos_[dev_index].name;
712 Technology::Identifier technology = GetDeviceTechnology(link_name);
713
714 if (technology == Technology::kCDCEthernet) {
715 LOG(INFO) << "In " << __func__ << ": device " << link_name
716 << " is now assumed to be regular Ethernet.";
717 technology = Technology::kEthernet;
718 } else if (technology != Technology::kCellular) {
719 LOG(WARNING) << "In " << __func__ << ": device " << link_name
720 << " is unexpected technology "
721 << Technology::NameFromIdentifier(technology);
722 }
723 string address =
724 StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
725 DCHECK(!address.empty());
726
727 DeviceRefPtr device = CreateDevice(link_name, address, dev_index,
728 technology);
729 if (device) {
730 RegisterDevice(device);
731 }
732 }
733}
734
Paul Stewart1ac4e842012-07-10 12:58:12 -0700735void DeviceInfo::RetrieveLinkStatistics(int interface_index,
736 const RTNLMessage &msg) {
737 if (!msg.HasAttribute(IFLA_STATS64)) {
738 return;
739 }
740 ByteString stats_bytes(msg.GetAttribute(IFLA_STATS64));
741 struct rtnl_link_stats64 stats;
742 if (stats_bytes.GetLength() < sizeof(stats)) {
743 LOG(WARNING) << "Link statistics size is too small: "
744 << stats_bytes.GetLength() << " < " << sizeof(stats);
745 return;
746 }
747
748 memcpy(&stats, stats_bytes.GetConstData(), sizeof(stats));
749 SLOG(Device, 2) << "Link statistics for "
750 << " interface index " << interface_index << ": "
751 << "receive: " << stats.rx_bytes << "; "
752 << "transmit: " << stats.tx_bytes << ".";
753 infos_[interface_index].rx_bytes = stats.rx_bytes;
754 infos_[interface_index].tx_bytes = stats.tx_bytes;
755}
756
757void DeviceInfo::RequestLinkStatistics() {
758 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink);
759 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
760 kRequestLinkStatisticsIntervalSeconds * 1000);
761}
762
Paul Stewart0af98bf2011-05-10 17:38:08 -0700763} // namespace shill