blob: 9cd1d6e29e853ca0a4d80b71e58a147286d3d2a6 [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 Stewartb50f0b92011-05-16 16:31:42 -070075
76DeviceInfo::DeviceInfo(ControlInterface *control_interface,
77 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080078 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070079 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070080 : control_interface_(control_interface),
81 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080082 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070083 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050084 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
85 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070086 link_listener_(NULL),
87 address_listener_(NULL),
Paul Stewartca876ee2012-04-21 08:55:58 -070088 device_info_root_(kDeviceInfoRoot),
Paul Stewart8c116a92012-05-02 18:30:03 -070089 routing_table_(RoutingTable::GetInstance()),
Paul Stewart9a908082011-08-31 12:18:48 -070090 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070091}
92
Paul Stewarta3c56f92011-05-26 07:08:52 -070093DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070094
mukesh agrawal8f317b62011-07-15 11:53:23 -070095void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
96 black_list_.insert(device_name);
97}
98
Eric Shienbrood5e628a52012-03-21 16:56:59 -040099bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
100 return ContainsKey(black_list_, device_name);
101}
102
Paul Stewarta3c56f92011-05-26 07:08:52 -0700103void DeviceInfo::Start() {
104 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500105 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700106 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500107 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700108 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
109 RTNLHandler::kRequestAddr);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700110}
111
Paul Stewarta3c56f92011-05-26 07:08:52 -0700112void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700113 link_listener_.reset();
114 address_listener_.reset();
Paul Stewart8c116a92012-05-02 18:30:03 -0700115 infos_.clear();
Paul Stewart050cfc02012-07-06 20:38:54 -0700116 delayed_devices_callback_.Cancel();
117 delayed_devices_.clear();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700118}
119
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700120void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700121 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
122 << device->interface_index() << ")";
Paul Stewart050cfc02012-07-06 20:38:54 -0700123 delayed_devices_.erase(device->interface_index());
Darin Petkove6193c02011-08-11 12:42:40 -0700124 CHECK(!GetDevice(device->interface_index()).get());
125 infos_[device->interface_index()].device = device;
Joshua Krollda798622012-06-05 12:30:48 -0700126 if ((device->technology() == Technology::kCellular) ||
127 (device->technology() == Technology::kEthernet) ||
128 (device->technology() == Technology::kWifi) ||
129 (device->technology() == Technology::kWiMax)) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700130 manager_->RegisterDevice(device);
131 }
132}
133
Jason Glasgowe9089492012-02-23 17:57:37 -0500134void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
135 int interface_index = device->interface_index();
136
Ben Chanfad4a0b2012-04-18 15:49:59 -0700137 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
138 << interface_index << ")";
Joshua Krollda798622012-06-05 12:30:48 -0700139 CHECK((device->technology() == Technology::kCellular) ||
140 (device->technology() == Technology::kWiMax));
Jason Glasgowe9089492012-02-23 17:57:37 -0500141
142 // Release reference to the device
143 map<int, Info>::iterator iter = infos_.find(interface_index);
144 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700145 SLOG(Device, 2) << "Removing device from info for index: "
146 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500147 manager_->DeregisterDevice(device);
148 // Release the reference to the device, but maintain the mapping
149 // for the index. That will be cleaned up by an RTNL message.
150 iter->second.device = NULL;
151 }
152}
153
Paul Stewartca876ee2012-04-21 08:55:58 -0700154FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name,
155 const string &path_name) {
156 return device_info_root_.Append(iface_name).Append(path_name);
157}
158
159bool DeviceInfo::GetDeviceInfoContents(const string &iface_name,
160 const string &path_name,
161 string *contents_out) {
162 return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
163 contents_out);
164}
165bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700166 const string &path_name,
167 FilePath *path_out) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700168 return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
169 path_out);
170}
171
Paul Stewartfdd16072011-09-16 12:41:35 -0700172Technology::Identifier DeviceInfo::GetDeviceTechnology(
173 const string &iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700174 string type_string;
175 int arp_type = ARPHRD_VOID;;
176 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
177 TrimString(type_string, "\n", &type_string) &&
178 !base::StringToInt(type_string, &arp_type)) {
179 arp_type = ARPHRD_VOID;
180 }
181
Paul Stewart9364c4c2011-12-06 17:12:42 -0800182 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700183 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Paul Stewart050cfc02012-07-06 20:38:54 -0700184 LOG(INFO) << StringPrintf("%s: device %s has no uevent file",
185 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700186 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800187 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700188
Thieu Le8f1c8352012-04-16 11:02:12 -0700189 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
190 // start of the file or after a newline, we can safely assume this
191 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800192 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700193 SLOG(Device, 2)
194 << StringPrintf("%s: device %s has wifi signature in uevent file",
195 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700196 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700197 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
198 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800199 return Technology::kWiFiMonitor;
200 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700201 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700202 }
203
Jason Glasgow7904d142012-05-18 13:36:20 -0400204 // Special case for pseudo modems which are used for testing
205 if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) {
206 SLOG(Device, 2) << StringPrintf(
207 "%s: device %s is a pseudo modem for testing",
208 __func__, iface_name.c_str());
209 return Technology::kCellular;
210 }
211
Paul Stewart9364c4c2011-12-06 17:12:42 -0800212 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700213 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700214 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
215 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700216 if (arp_type == ARPHRD_LOOPBACK) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700217 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
218 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700219 return Technology::kLoopback;
220 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700221 if (arp_type == ARPHRD_PPP) {
222 SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device",
223 __func__, iface_name.c_str());
224 return Technology::kPPP;
225 }
226 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800227 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700228 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
229 TrimString(tun_flags_str, "\n", &tun_flags_str) &&
230 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800231 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700232 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
233 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800234 return Technology::kTunnel;
235 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700236 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800237 }
238
239 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700240 // See if driver for this interface is in a list of known modem driver names.
241 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
242 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800243 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700244 SLOG(Device, 2)
245 << StringPrintf("%s: device %s is matched with modem driver %s",
246 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800247 return Technology::kCellular;
248 }
249 }
250
Ben Chan4e64d2d2012-05-16 00:02:25 -0700251 if (driver_name == kDriverGdmWiMax) {
252 SLOG(Device, 2) << StringPrintf("%s: device %s is a WiMAX device",
253 __func__, iface_name.c_str());
254 return Technology::kWiMax;
255 }
256
Thieu Le8f1c8352012-04-16 11:02:12 -0700257 // For cdc_ether devices, make sure it's a modem because this driver
258 // can be used for other ethernet devices.
Paul Stewart050cfc02012-07-06 20:38:54 -0700259 if (driver_name == kDriverCdcEther) {
260 if (IsCdcEtherModemDevice(iface_name)) {
261 LOG(INFO) << StringPrintf("%s: device %s is a modem cdc_ether "
262 "device", __func__, iface_name.c_str());
263 return Technology::kCellular;
264 }
265 SLOG(Device, 2) << StringPrintf("%s: device %s is a cdc_ether "
266 "device", __func__, iface_name.c_str());
267 return Technology::kCDCEthernet;
Thieu Le8f1c8352012-04-16 11:02:12 -0700268 }
269
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700270 // Special case for the virtio driver, used when run under KVM. See also
271 // the comment in VirtioEthernet::Start.
272 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700273 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
274 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700275 return Technology::kVirtioEthernet;
276 }
277
Ben Chanfad4a0b2012-04-18 15:49:59 -0700278 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
279 "is defaulted to type ethernet",
280 __func__, iface_name.c_str(),
281 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700282 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700283}
284
Thieu Le8f1c8352012-04-16 11:02:12 -0700285bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
286 // A cdc_ether device is a modem device if it also exposes tty interfaces.
287 // To determine this, we look for the existence of the tty interface in the
288 // USB device sysfs tree.
289 //
290 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
291 // follows:
292 //
293 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
294 // 1-2:1.0
295 // tty
296 // ttyACM0
297 // 1-2:1.1
298 // net
299 // usb0
300 // 1-2:1.2
301 // tty
302 // ttyACM1
303 // ...
304 //
305 // /sys/class/net/usb0/device symlinks to
306 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700307 //
308 // Note that some modem devices have the tty directory one level deeper
309 // (eg. E362), so the device tree for the tty interface is:
310 // /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 -0700311
Paul Stewartca876ee2012-04-21 08:55:58 -0700312 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700313 FilePath device_path;
314 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700315 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
316 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700317 return false;
318 }
319 if (!device_path.IsAbsolute()) {
320 device_path = device_file.DirName().Append(device_path);
321 file_util::AbsolutePath(&device_path);
322 }
323
324 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700325 // USB device and see if there's a subdirectory "tty" inside. In other
326 // words, using the example dir hierarchy above, find
327 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700328 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700329 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700330}
331
332// static
Thieu Leb27beee2012-04-20 09:19:06 -0700333bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
334 file_util::FileEnumerator::FileType type =
335 static_cast<file_util::FileEnumerator::FileType>(
336 file_util::FileEnumerator::DIRECTORIES |
337 file_util::FileEnumerator::SHOW_SYM_LINKS);
338 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700339 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
340 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700341 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700342 return true;
343 }
344 return false;
345}
346
Paul Stewart8c116a92012-05-02 18:30:03 -0700347DeviceRefPtr DeviceInfo::CreateDevice(const string &link_name,
348 const string &address,
349 int interface_index,
350 Technology::Identifier technology) {
351 DeviceRefPtr device;
Paul Stewart050cfc02012-07-06 20:38:54 -0700352 delayed_devices_.erase(interface_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700353
354 switch (technology) {
355 case Technology::kCellular:
356 // Cellular devices are managed by ModemInfo.
357 SLOG(Device, 2) << "Cellular link " << link_name
358 << " at index " << interface_index
359 << " -- notifying ModemInfo.";
360 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
361 break;
362 case Technology::kEthernet:
363 device = new Ethernet(control_interface_, dispatcher_, metrics_,
364 manager_, link_name, address, interface_index);
365 device->EnableIPv6Privacy();
366 break;
367 case Technology::kVirtioEthernet:
368 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
369 manager_, link_name, address,
370 interface_index);
371 device->EnableIPv6Privacy();
372 break;
373 case Technology::kWifi:
374 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
375 link_name, address, interface_index);
376 device->EnableIPv6Privacy();
377 break;
Ben Chan4e64d2d2012-05-16 00:02:25 -0700378 case Technology::kWiMax:
Darin Petkove4b27022012-05-16 13:28:50 +0200379 // WiMax devices are managed by WiMaxProvider.
380 SLOG(Device, 2) << "WiMax link " << link_name
381 << " at index " << interface_index
382 << " -- notifying WiMaxProvider.";
383 manager_->wimax_provider()->OnDeviceInfoAvailable(link_name);
Ben Chan4e64d2d2012-05-16 00:02:25 -0700384 break;
Paul Stewart8c116a92012-05-02 18:30:03 -0700385 case Technology::kPPP:
386 case Technology::kTunnel:
387 // Tunnel and PPP devices are managed by the VPN code (PPP for
388 // l2tpipsec). Notify the VPN Provider of the interface's presence.
389 // Since CreateDevice is only called once in the lifetime of an
390 // interface index, this notification will only occur the first
391 // time the device is seen.
392 SLOG(Device, 2) << "Tunnel / PPP link " << link_name
393 << " at index " << interface_index
394 << " -- notifying VPNProvider.";
395 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
396 interface_index) &&
397 technology == Technology::kTunnel) {
398 // If VPN does not know anything about this tunnel, it is probably
399 // left over from a previous instance and should not exist.
400 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
401 DeleteInterface(interface_index);
402 }
403 break;
404 case Technology::kLoopback:
405 // Loopback devices are largely ignored, but we should make sure the
406 // link is enabled.
407 SLOG(Device, 2) << "Bringing up loopback device " << link_name
408 << " at index " << interface_index;
409 rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP);
410 return NULL;
Paul Stewart050cfc02012-07-06 20:38:54 -0700411 case Technology::kCDCEthernet:
412 // CDCEnternet devices are of indeterminate type when they are
413 // initially created. Some time later, tty devices may or may
414 // not appear under the same USB device root, which will identify
415 // it as a modem. Alternatively, ModemManager may discover the
416 // device and create and register a Cellular device. In either
417 // case, we should delay creating a Device until we can make a
418 // better determination of what type this Device should be.
419 LOG(INFO) << "Delaying creation of device for " << link_name
420 << " at index " << interface_index;
421 DelayDeviceCreation(interface_index);
422 return NULL;
Paul Stewart8c116a92012-05-02 18:30:03 -0700423 default:
424 // We will not manage this device in shill. Do not create a device
425 // object or do anything to change its state. We create a stub object
426 // which is useful for testing.
427 return new DeviceStub(control_interface_, dispatcher_, metrics_,
428 manager_, link_name, address, interface_index,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700429 technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700430 }
431
432 // Reset the routing table and addresses.
433 routing_table_->FlushRoutes(interface_index);
434 FlushAddresses(interface_index);
435
436 return device;
437}
438
Chris Masone2aa97072011-08-09 17:35:08 -0700439void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700440 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
441 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700442 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700443 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700444
Darin Petkove6193c02011-08-11 12:42:40 -0700445 unsigned int flags = msg.link_status().flags;
446 unsigned int change = msg.link_status().change;
Paul Stewart8c116a92012-05-02 18:30:03 -0700447 bool new_device =
448 !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700449 SLOG(Device, 2) << __func__ << "(index=" << dev_index
450 << std::showbase << std::hex
451 << ", flags=" << flags << ", change=" << change << ")"
452 << std::dec << std::noshowbase
453 << ", new_device=" << new_device;
Paul Stewart8c116a92012-05-02 18:30:03 -0700454 infos_[dev_index].has_addresses_only = false;
Darin Petkove6193c02011-08-11 12:42:40 -0700455 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700456
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700457 DeviceRefPtr device = GetDevice(dev_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700458 if (new_device) {
459 CHECK(!device);
Chris Masone2aa97072011-08-09 17:35:08 -0700460 if (!msg.HasAttribute(IFLA_IFNAME)) {
461 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
462 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700463 }
Chris Masone2aa97072011-08-09 17:35:08 -0700464 ByteString b(msg.GetAttribute(IFLA_IFNAME));
465 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700466 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200467 infos_[dev_index].name = link_name;
468 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700469
Chris Masone2aa97072011-08-09 17:35:08 -0700470 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700471 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700472 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700473 } else {
474 technology = GetDeviceTechnology(link_name);
475 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700476 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800477 string address;
478 if (msg.HasAttribute(IFLA_ADDRESS)) {
479 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
480 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700481 SLOG(Device, 2) << "link index " << dev_index << " address "
482 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700483 } else if (technology != Technology::kTunnel &&
484 technology != Technology::kPPP) {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800485 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
486 return;
487 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700488 device = CreateDevice(link_name, address, dev_index, technology);
489 if (device) {
490 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700491 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700492 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700493 if (device) {
494 device->LinkEvent(flags, change);
495 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700496}
497
Chris Masone2aa97072011-08-09 17:35:08 -0700498void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700499 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700500
Paul Stewart9a908082011-08-31 12:18:48 -0700501 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
502 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700503 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
504 << std::showbase << std::hex
505 << ", flags=" << msg.link_status().flags
506 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700507 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700508}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700509
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700510DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
511 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700512 return info ? info->device : NULL;
513}
514
Darin Petkovf8046b82012-04-24 16:29:23 +0200515int DeviceInfo::GetIndex(const string &interface_name) const {
516 map<string, int>::const_iterator it = indices_.find(interface_name);
517 return it == indices_.end() ? -1 : it->second;
518}
519
Paul Stewart32852962011-08-30 14:06:53 -0700520bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700521 const Info *info = GetInfo(interface_index);
522 if (!info) {
523 return false;
524 }
Paul Stewart32852962011-08-30 14:06:53 -0700525 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700526 return true;
527}
528
Paul Stewart9a908082011-08-31 12:18:48 -0700529bool DeviceInfo::GetAddresses(int interface_index,
530 vector<AddressData> *addresses) const {
531 const Info *info = GetInfo(interface_index);
532 if (!info) {
533 return false;
534 }
535 *addresses = info->ip_addresses;
536 return true;
537}
538
539void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700540 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700541 const Info *info = GetInfo(interface_index);
542 if (!info) {
543 return;
544 }
545 const vector<AddressData> &addresses = info->ip_addresses;
546 vector<AddressData>::const_iterator iter;
547 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700548 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700549 (iter->scope == RT_SCOPE_UNIVERSE &&
550 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700551 SLOG(Device, 2) << __func__ << ": removing ip address "
552 << iter->address.ToString()
553 << " from interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700554 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
555 }
556 }
557}
558
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700559bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
560 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700561 if (!info) {
562 return false;
563 }
564 *flags = info->flags;
565 return true;
566}
567
Paul Stewartca6abd42012-03-01 15:45:29 -0800568bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800569 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
570 if (fd < 0) {
571 PLOG(ERROR) << "failed to open " << kTunDeviceName;
572 return false;
573 }
574 file_util::ScopedFD scoped_fd(&fd);
575
576 struct ifreq ifr;
577 memset(&ifr, 0, sizeof(ifr));
578 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
579 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
580 PLOG(ERROR) << "failed to create tunnel interface";
581 return false;
582 }
583
584 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
585 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
586 return false;
587 }
588
589 *interface_name = string(ifr.ifr_name);
590
591 return true;
592}
593
Paul Stewartca6abd42012-03-01 15:45:29 -0800594bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800595 return rtnl_handler_->RemoveInterface(interface_index);
596}
597
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700598const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
599 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700600 if (iter == infos_.end()) {
601 return NULL;
602 }
603 return &iter->second;
604}
605
606void DeviceInfo::RemoveInfo(int interface_index) {
607 map<int, Info>::iterator iter = infos_.find(interface_index);
608 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700609 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700610 if (iter->second.device.get()) {
611 manager_->DeregisterDevice(iter->second.device);
612 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200613 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700614 infos_.erase(iter);
Paul Stewart050cfc02012-07-06 20:38:54 -0700615 delayed_devices_.erase(interface_index);
mukesh agrawal47009f82011-08-25 14:07:35 -0700616 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700617 SLOG(Device, 2) << __func__ << ": Unknown device index: "
618 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700619 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700620}
621
Chris Masone2aa97072011-08-09 17:35:08 -0700622void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700623 DCHECK(msg.type() == RTNLMessage::kTypeLink);
624 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700625 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700626 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700627 DelLinkMsgHandler(msg);
628 } else {
629 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700630 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700631}
632
Paul Stewart9a908082011-08-31 12:18:48 -0700633void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700634 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700635 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
636 int interface_index = msg.interface_index();
637 if (!ContainsKey(infos_, interface_index)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700638 SLOG(Device, 2) << "Got advance address information for unknown index "
639 << interface_index;
640 infos_[interface_index].has_addresses_only = true;
Paul Stewart9a908082011-08-31 12:18:48 -0700641 }
642 const RTNLMessage::AddressStatus &status = msg.address_status();
643 IPAddress address(msg.family(),
Ben Chan682c5d42012-06-18 14:11:04 -0700644 msg.HasAttribute(IFA_LOCAL) ?
645 msg.GetAttribute(IFA_LOCAL) : msg.GetAttribute(IFA_ADDRESS),
Paul Stewart9a908082011-08-31 12:18:48 -0700646 status.prefix_len);
647
Ben Chan682c5d42012-06-18 14:11:04 -0700648 SLOG_IF(Device, 2, msg.HasAttribute(IFA_LOCAL))
649 << "Found local address attribute for interface " << interface_index;
650
Paul Stewart9a908082011-08-31 12:18:48 -0700651 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
652 vector<AddressData>::iterator iter;
653 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
654 if (address.Equals(iter->address)) {
655 break;
656 }
657 }
658 if (iter != address_list.end()) {
659 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700660 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700661 address_list.erase(iter);
662 } else {
663 iter->flags = status.flags;
664 iter->scope = status.scope;
665 }
666 } else if (msg.mode() == RTNLMessage::kModeAdd) {
667 address_list.push_back(AddressData(address, status.flags, status.scope));
Paul Stewart8c116a92012-05-02 18:30:03 -0700668 SLOG(Device, 2) << "Add address " << address.ToString()
669 << " for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700670 }
671}
672
Paul Stewart050cfc02012-07-06 20:38:54 -0700673void DeviceInfo::DelayDeviceCreation(int interface_index) {
674 delayed_devices_.insert(interface_index);
675 delayed_devices_callback_.Reset(
676 Bind(&DeviceInfo::DelayedDeviceCreationTask, AsWeakPtr()));
677 dispatcher_->PostDelayedTask(delayed_devices_callback_.callback(),
678 kDelayedDeviceCreationSeconds * 1000);
679}
680
681// Re-evaluate the technology type for each delayed device.
682void DeviceInfo::DelayedDeviceCreationTask() {
683 while (!delayed_devices_.empty()) {
684 set<int>::iterator it = delayed_devices_.begin();
685 int dev_index = *it;
686 delayed_devices_.erase(it);
687
688 DCHECK(ContainsKey(infos_, dev_index));
689 DCHECK(!GetDevice(dev_index));
690
691 const string &link_name = infos_[dev_index].name;
692 Technology::Identifier technology = GetDeviceTechnology(link_name);
693
694 if (technology == Technology::kCDCEthernet) {
695 LOG(INFO) << "In " << __func__ << ": device " << link_name
696 << " is now assumed to be regular Ethernet.";
697 technology = Technology::kEthernet;
698 } else if (technology != Technology::kCellular) {
699 LOG(WARNING) << "In " << __func__ << ": device " << link_name
700 << " is unexpected technology "
701 << Technology::NameFromIdentifier(technology);
702 }
703 string address =
704 StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
705 DCHECK(!address.empty());
706
707 DeviceRefPtr device = CreateDevice(link_name, address, dev_index,
708 technology);
709 if (device) {
710 RegisterDevice(device);
711 }
712 }
713}
714
Paul Stewart0af98bf2011-05-10 17:38:08 -0700715} // namespace shill