blob: d4d5f270586a6c993d15c9a756bb1e3ce31524bd [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 Stewart0af98bf2011-05-10 17:38:08 -070050using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070051using std::vector;
Paul Stewart0af98bf2011-05-10 17:38:08 -070052
53namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070054
55// static
Jason Glasgowabc54032012-04-20 16:08:32 -040056const char DeviceInfo::kModemPseudoDeviceNamePrefix[] = "pseudomodem";
Paul Stewartca876ee2012-04-21 08:55:58 -070057const char DeviceInfo::kDeviceInfoRoot[] = "/sys/class/net";
Ben Chan4e64d2d2012-05-16 00:02:25 -070058const char DeviceInfo::kDriverCdcEther[] = "cdc_ether";
59const char DeviceInfo::kDriverGdmWiMax[] = "gdm_wimax";
mukesh agrawal93a29ed2012-04-17 16:13:01 -070060const char DeviceInfo::kDriverVirtioNet[] = "virtio_net";
Paul Stewartca876ee2012-04-21 08:55:58 -070061const char DeviceInfo::kInterfaceUevent[] = "uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080062const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Paul Stewartca876ee2012-04-21 08:55:58 -070063const char DeviceInfo::kInterfaceDevice[] = "device";
64const char DeviceInfo::kInterfaceDriver[] = "device/driver";
65const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags";
66const char DeviceInfo::kInterfaceType[] = "type";
Paul Stewartb50f0b92011-05-16 16:31:42 -070067const char *DeviceInfo::kModemDrivers[] = {
68 "gobi",
69 "QCUSBNet2k",
Thieu Le8f1c8352012-04-16 11:02:12 -070070 "GobiNet"
Paul Stewartb50f0b92011-05-16 16:31:42 -070071};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080072const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewartb50f0b92011-05-16 16:31:42 -070073
74DeviceInfo::DeviceInfo(ControlInterface *control_interface,
75 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080076 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070077 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070078 : control_interface_(control_interface),
79 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080080 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070081 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050082 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
83 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070084 link_listener_(NULL),
85 address_listener_(NULL),
Paul Stewartca876ee2012-04-21 08:55:58 -070086 device_info_root_(kDeviceInfoRoot),
Paul Stewart8c116a92012-05-02 18:30:03 -070087 routing_table_(RoutingTable::GetInstance()),
Paul Stewart9a908082011-08-31 12:18:48 -070088 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070089}
90
Paul Stewarta3c56f92011-05-26 07:08:52 -070091DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070092
mukesh agrawal8f317b62011-07-15 11:53:23 -070093void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
94 black_list_.insert(device_name);
95}
96
Eric Shienbrood5e628a52012-03-21 16:56:59 -040097bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
98 return ContainsKey(black_list_, device_name);
99}
100
Paul Stewarta3c56f92011-05-26 07:08:52 -0700101void DeviceInfo::Start() {
102 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500103 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700104 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500105 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700106 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
107 RTNLHandler::kRequestAddr);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700108}
109
Paul Stewarta3c56f92011-05-26 07:08:52 -0700110void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700111 link_listener_.reset();
112 address_listener_.reset();
Paul Stewart8c116a92012-05-02 18:30:03 -0700113 infos_.clear();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700114}
115
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700116void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700117 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
118 << device->interface_index() << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700119 CHECK(!GetDevice(device->interface_index()).get());
120 infos_[device->interface_index()].device = device;
Paul Stewartfdd16072011-09-16 12:41:35 -0700121 if (device->TechnologyIs(Technology::kCellular) ||
122 device->TechnologyIs(Technology::kEthernet) ||
Darin Petkove4b27022012-05-16 13:28:50 +0200123 device->TechnologyIs(Technology::kWifi) ||
124 device->TechnologyIs(Technology::kWiMax)) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700125 manager_->RegisterDevice(device);
126 }
127}
128
Jason Glasgowe9089492012-02-23 17:57:37 -0500129void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
130 int interface_index = device->interface_index();
131
Ben Chanfad4a0b2012-04-18 15:49:59 -0700132 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
133 << interface_index << ")";
Darin Petkove4b27022012-05-16 13:28:50 +0200134 CHECK(device->TechnologyIs(Technology::kCellular) ||
135 device->TechnologyIs(Technology::kWiMax));
Jason Glasgowe9089492012-02-23 17:57:37 -0500136
137 // Release reference to the device
138 map<int, Info>::iterator iter = infos_.find(interface_index);
139 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700140 SLOG(Device, 2) << "Removing device from info for index: "
141 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500142 manager_->DeregisterDevice(device);
143 // Release the reference to the device, but maintain the mapping
144 // for the index. That will be cleaned up by an RTNL message.
145 iter->second.device = NULL;
146 }
147}
148
Paul Stewartca876ee2012-04-21 08:55:58 -0700149FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name,
150 const string &path_name) {
151 return device_info_root_.Append(iface_name).Append(path_name);
152}
153
154bool DeviceInfo::GetDeviceInfoContents(const string &iface_name,
155 const string &path_name,
156 string *contents_out) {
157 return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
158 contents_out);
159}
160bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700161 const string &path_name,
162 FilePath *path_out) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700163 return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
164 path_out);
165}
166
Paul Stewartfdd16072011-09-16 12:41:35 -0700167Technology::Identifier DeviceInfo::GetDeviceTechnology(
168 const string &iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700169 string type_string;
170 int arp_type = ARPHRD_VOID;;
171 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
172 TrimString(type_string, "\n", &type_string) &&
173 !base::StringToInt(type_string, &arp_type)) {
174 arp_type = ARPHRD_VOID;
175 }
176
Paul Stewart9364c4c2011-12-06 17:12:42 -0800177 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700178 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700179 SLOG(Device, 2) << StringPrintf("%s: device %s has no uevent file",
180 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700181 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800182 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700183
Thieu Le8f1c8352012-04-16 11:02:12 -0700184 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
185 // start of the file or after a newline, we can safely assume this
186 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800187 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700188 SLOG(Device, 2)
189 << StringPrintf("%s: device %s has wifi signature in uevent file",
190 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700191 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700192 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
193 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800194 return Technology::kWiFiMonitor;
195 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700196 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700197 }
198
Jason Glasgow7904d142012-05-18 13:36:20 -0400199 // Special case for pseudo modems which are used for testing
200 if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) {
201 SLOG(Device, 2) << StringPrintf(
202 "%s: device %s is a pseudo modem for testing",
203 __func__, iface_name.c_str());
204 return Technology::kCellular;
205 }
206
Paul Stewart9364c4c2011-12-06 17:12:42 -0800207 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700208 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700209 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
210 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700211 if (arp_type == ARPHRD_LOOPBACK) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700212 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
213 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700214 return Technology::kLoopback;
215 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700216 if (arp_type == ARPHRD_PPP) {
217 SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device",
218 __func__, iface_name.c_str());
219 return Technology::kPPP;
220 }
221 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800222 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700223 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
224 TrimString(tun_flags_str, "\n", &tun_flags_str) &&
225 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800226 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700227 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
228 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800229 return Technology::kTunnel;
230 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700231 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800232 }
233
234 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700235 // See if driver for this interface is in a list of known modem driver names.
236 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
237 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800238 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700239 SLOG(Device, 2)
240 << StringPrintf("%s: device %s is matched with modem driver %s",
241 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800242 return Technology::kCellular;
243 }
244 }
245
Ben Chan4e64d2d2012-05-16 00:02:25 -0700246 if (driver_name == kDriverGdmWiMax) {
247 SLOG(Device, 2) << StringPrintf("%s: device %s is a WiMAX device",
248 __func__, iface_name.c_str());
249 return Technology::kWiMax;
250 }
251
Thieu Le8f1c8352012-04-16 11:02:12 -0700252 // For cdc_ether devices, make sure it's a modem because this driver
253 // can be used for other ethernet devices.
254 if (driver_name == kDriverCdcEther &&
255 IsCdcEtherModemDevice(iface_name)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700256 SLOG(Device, 2) << StringPrintf("%s: device %s is a modem cdc_ether device",
257 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700258 return Technology::kCellular;
259 }
260
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700261 // Special case for the virtio driver, used when run under KVM. See also
262 // the comment in VirtioEthernet::Start.
263 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700264 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
265 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700266 return Technology::kVirtioEthernet;
267 }
268
Ben Chanfad4a0b2012-04-18 15:49:59 -0700269 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
270 "is defaulted to type ethernet",
271 __func__, iface_name.c_str(),
272 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700273 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700274}
275
Thieu Le8f1c8352012-04-16 11:02:12 -0700276bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
277 // A cdc_ether device is a modem device if it also exposes tty interfaces.
278 // To determine this, we look for the existence of the tty interface in the
279 // USB device sysfs tree.
280 //
281 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
282 // follows:
283 //
284 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
285 // 1-2:1.0
286 // tty
287 // ttyACM0
288 // 1-2:1.1
289 // net
290 // usb0
291 // 1-2:1.2
292 // tty
293 // ttyACM1
294 // ...
295 //
296 // /sys/class/net/usb0/device symlinks to
297 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700298 //
299 // Note that some modem devices have the tty directory one level deeper
300 // (eg. E362), so the device tree for the tty interface is:
301 // /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 -0700302
Paul Stewartca876ee2012-04-21 08:55:58 -0700303 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700304 FilePath device_path;
305 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700306 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
307 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700308 return false;
309 }
310 if (!device_path.IsAbsolute()) {
311 device_path = device_file.DirName().Append(device_path);
312 file_util::AbsolutePath(&device_path);
313 }
314
315 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700316 // USB device and see if there's a subdirectory "tty" inside. In other
317 // words, using the example dir hierarchy above, find
318 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700319 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700320 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700321}
322
323// static
Thieu Leb27beee2012-04-20 09:19:06 -0700324bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
325 file_util::FileEnumerator::FileType type =
326 static_cast<file_util::FileEnumerator::FileType>(
327 file_util::FileEnumerator::DIRECTORIES |
328 file_util::FileEnumerator::SHOW_SYM_LINKS);
329 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700330 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
331 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700332 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700333 return true;
334 }
335 return false;
336}
337
Paul Stewart8c116a92012-05-02 18:30:03 -0700338DeviceRefPtr DeviceInfo::CreateDevice(const string &link_name,
339 const string &address,
340 int interface_index,
341 Technology::Identifier technology) {
342 DeviceRefPtr device;
343
344 switch (technology) {
345 case Technology::kCellular:
346 // Cellular devices are managed by ModemInfo.
347 SLOG(Device, 2) << "Cellular link " << link_name
348 << " at index " << interface_index
349 << " -- notifying ModemInfo.";
350 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
351 break;
352 case Technology::kEthernet:
353 device = new Ethernet(control_interface_, dispatcher_, metrics_,
354 manager_, link_name, address, interface_index);
355 device->EnableIPv6Privacy();
356 break;
357 case Technology::kVirtioEthernet:
358 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
359 manager_, link_name, address,
360 interface_index);
361 device->EnableIPv6Privacy();
362 break;
363 case Technology::kWifi:
364 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
365 link_name, address, interface_index);
366 device->EnableIPv6Privacy();
367 break;
Ben Chan4e64d2d2012-05-16 00:02:25 -0700368 case Technology::kWiMax:
Darin Petkove4b27022012-05-16 13:28:50 +0200369 // WiMax devices are managed by WiMaxProvider.
370 SLOG(Device, 2) << "WiMax link " << link_name
371 << " at index " << interface_index
372 << " -- notifying WiMaxProvider.";
373 manager_->wimax_provider()->OnDeviceInfoAvailable(link_name);
Ben Chan4e64d2d2012-05-16 00:02:25 -0700374 break;
Paul Stewart8c116a92012-05-02 18:30:03 -0700375 case Technology::kPPP:
376 case Technology::kTunnel:
377 // Tunnel and PPP devices are managed by the VPN code (PPP for
378 // l2tpipsec). Notify the VPN Provider of the interface's presence.
379 // Since CreateDevice is only called once in the lifetime of an
380 // interface index, this notification will only occur the first
381 // time the device is seen.
382 SLOG(Device, 2) << "Tunnel / PPP link " << link_name
383 << " at index " << interface_index
384 << " -- notifying VPNProvider.";
385 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
386 interface_index) &&
387 technology == Technology::kTunnel) {
388 // If VPN does not know anything about this tunnel, it is probably
389 // left over from a previous instance and should not exist.
390 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
391 DeleteInterface(interface_index);
392 }
393 break;
394 case Technology::kLoopback:
395 // Loopback devices are largely ignored, but we should make sure the
396 // link is enabled.
397 SLOG(Device, 2) << "Bringing up loopback device " << link_name
398 << " at index " << interface_index;
399 rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP);
400 return NULL;
401 default:
402 // We will not manage this device in shill. Do not create a device
403 // object or do anything to change its state. We create a stub object
404 // which is useful for testing.
405 return new DeviceStub(control_interface_, dispatcher_, metrics_,
406 manager_, link_name, address, interface_index,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700407 technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700408 }
409
410 // Reset the routing table and addresses.
411 routing_table_->FlushRoutes(interface_index);
412 FlushAddresses(interface_index);
413
414 return device;
415}
416
Chris Masone2aa97072011-08-09 17:35:08 -0700417void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700418 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
419 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700420 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700421 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700422
Darin Petkove6193c02011-08-11 12:42:40 -0700423 unsigned int flags = msg.link_status().flags;
424 unsigned int change = msg.link_status().change;
Paul Stewart8c116a92012-05-02 18:30:03 -0700425 bool new_device =
426 !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700427 SLOG(Device, 2) << __func__ << "(index=" << dev_index
428 << std::showbase << std::hex
429 << ", flags=" << flags << ", change=" << change << ")"
430 << std::dec << std::noshowbase
431 << ", new_device=" << new_device;
Paul Stewart8c116a92012-05-02 18:30:03 -0700432 infos_[dev_index].has_addresses_only = false;
Darin Petkove6193c02011-08-11 12:42:40 -0700433 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700434
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700435 DeviceRefPtr device = GetDevice(dev_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700436 if (new_device) {
437 CHECK(!device);
Chris Masone2aa97072011-08-09 17:35:08 -0700438 if (!msg.HasAttribute(IFLA_IFNAME)) {
439 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
440 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700441 }
Chris Masone2aa97072011-08-09 17:35:08 -0700442 ByteString b(msg.GetAttribute(IFLA_IFNAME));
443 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700444 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200445 infos_[dev_index].name = link_name;
446 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700447
Chris Masone2aa97072011-08-09 17:35:08 -0700448 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700449 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700450 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700451 } else {
452 technology = GetDeviceTechnology(link_name);
453 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700454 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800455 string address;
456 if (msg.HasAttribute(IFLA_ADDRESS)) {
457 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
458 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700459 SLOG(Device, 2) << "link index " << dev_index << " address "
460 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700461 } else if (technology != Technology::kTunnel &&
462 technology != Technology::kPPP) {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800463 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
464 return;
465 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700466 device = CreateDevice(link_name, address, dev_index, technology);
467 if (device) {
468 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700469 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700470 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700471 if (device) {
472 device->LinkEvent(flags, change);
473 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700474}
475
Chris Masone2aa97072011-08-09 17:35:08 -0700476void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700477 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700478
Paul Stewart9a908082011-08-31 12:18:48 -0700479 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
480 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700481 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
482 << std::showbase << std::hex
483 << ", flags=" << msg.link_status().flags
484 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700485 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700486}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700487
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700488DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
489 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700490 return info ? info->device : NULL;
491}
492
Darin Petkovf8046b82012-04-24 16:29:23 +0200493int DeviceInfo::GetIndex(const string &interface_name) const {
494 map<string, int>::const_iterator it = indices_.find(interface_name);
495 return it == indices_.end() ? -1 : it->second;
496}
497
Paul Stewart32852962011-08-30 14:06:53 -0700498bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700499 const Info *info = GetInfo(interface_index);
500 if (!info) {
501 return false;
502 }
Paul Stewart32852962011-08-30 14:06:53 -0700503 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700504 return true;
505}
506
Paul Stewart9a908082011-08-31 12:18:48 -0700507bool DeviceInfo::GetAddresses(int interface_index,
508 vector<AddressData> *addresses) const {
509 const Info *info = GetInfo(interface_index);
510 if (!info) {
511 return false;
512 }
513 *addresses = info->ip_addresses;
514 return true;
515}
516
517void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700518 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700519 const Info *info = GetInfo(interface_index);
520 if (!info) {
521 return;
522 }
523 const vector<AddressData> &addresses = info->ip_addresses;
524 vector<AddressData>::const_iterator iter;
525 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700526 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700527 (iter->scope == RT_SCOPE_UNIVERSE &&
528 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700529 SLOG(Device, 2) << __func__ << ": removing ip address "
530 << iter->address.ToString()
531 << " from interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700532 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
533 }
534 }
535}
536
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700537bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
538 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700539 if (!info) {
540 return false;
541 }
542 *flags = info->flags;
543 return true;
544}
545
Paul Stewartca6abd42012-03-01 15:45:29 -0800546bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800547 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
548 if (fd < 0) {
549 PLOG(ERROR) << "failed to open " << kTunDeviceName;
550 return false;
551 }
552 file_util::ScopedFD scoped_fd(&fd);
553
554 struct ifreq ifr;
555 memset(&ifr, 0, sizeof(ifr));
556 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
557 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
558 PLOG(ERROR) << "failed to create tunnel interface";
559 return false;
560 }
561
562 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
563 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
564 return false;
565 }
566
567 *interface_name = string(ifr.ifr_name);
568
569 return true;
570}
571
Paul Stewartca6abd42012-03-01 15:45:29 -0800572bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800573 return rtnl_handler_->RemoveInterface(interface_index);
574}
575
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700576const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
577 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700578 if (iter == infos_.end()) {
579 return NULL;
580 }
581 return &iter->second;
582}
583
584void DeviceInfo::RemoveInfo(int interface_index) {
585 map<int, Info>::iterator iter = infos_.find(interface_index);
586 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700587 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700588 if (iter->second.device.get()) {
589 manager_->DeregisterDevice(iter->second.device);
590 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200591 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700592 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700593 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700594 SLOG(Device, 2) << __func__ << ": Unknown device index: "
595 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700596 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700597}
598
Chris Masone2aa97072011-08-09 17:35:08 -0700599void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700600 DCHECK(msg.type() == RTNLMessage::kTypeLink);
601 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700602 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700603 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700604 DelLinkMsgHandler(msg);
605 } else {
606 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700607 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700608}
609
Paul Stewart9a908082011-08-31 12:18:48 -0700610void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700611 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700612 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
613 int interface_index = msg.interface_index();
614 if (!ContainsKey(infos_, interface_index)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700615 SLOG(Device, 2) << "Got advance address information for unknown index "
616 << interface_index;
617 infos_[interface_index].has_addresses_only = true;
Paul Stewart9a908082011-08-31 12:18:48 -0700618 }
619 const RTNLMessage::AddressStatus &status = msg.address_status();
620 IPAddress address(msg.family(),
621 msg.GetAttribute(IFA_ADDRESS),
622 status.prefix_len);
623
624 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
625 vector<AddressData>::iterator iter;
626 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
627 if (address.Equals(iter->address)) {
628 break;
629 }
630 }
631 if (iter != address_list.end()) {
632 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700633 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700634 address_list.erase(iter);
635 } else {
636 iter->flags = status.flags;
637 iter->scope = status.scope;
638 }
639 } else if (msg.mode() == RTNLMessage::kModeAdd) {
640 address_list.push_back(AddressData(address, status.flags, status.scope));
Paul Stewart8c116a92012-05-02 18:30:03 -0700641 SLOG(Device, 2) << "Add address " << address.ToString()
642 << " for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700643 }
644}
645
Paul Stewart0af98bf2011-05-10 17:38:08 -0700646} // namespace shill