blob: f3c9263c12f20a49b278f6b481838c935b001a00 [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"
Ben Chan4e64d2d2012-05-16 00:02:25 -070045#include "shill/wimax.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070046
Eric Shienbrood3e20a232012-02-16 11:35:56 -050047using base::Bind;
Thieu Le8f1c8352012-04-16 11:02:12 -070048using base::StringPrintf;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050049using base::Unretained;
Darin Petkove6193c02011-08-11 12:42:40 -070050using std::map;
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 Stewartb50f0b92011-05-16 16:31:42 -070074
75DeviceInfo::DeviceInfo(ControlInterface *control_interface,
76 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080077 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070078 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070079 : control_interface_(control_interface),
80 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080081 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070082 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050083 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
84 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070085 link_listener_(NULL),
86 address_listener_(NULL),
Paul Stewartca876ee2012-04-21 08:55:58 -070087 device_info_root_(kDeviceInfoRoot),
Paul Stewart8c116a92012-05-02 18:30:03 -070088 routing_table_(RoutingTable::GetInstance()),
Paul Stewart9a908082011-08-31 12:18:48 -070089 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070090}
91
Paul Stewarta3c56f92011-05-26 07:08:52 -070092DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070093
mukesh agrawal8f317b62011-07-15 11:53:23 -070094void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
95 black_list_.insert(device_name);
96}
97
Eric Shienbrood5e628a52012-03-21 16:56:59 -040098bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
99 return ContainsKey(black_list_, device_name);
100}
101
Paul Stewarta3c56f92011-05-26 07:08:52 -0700102void DeviceInfo::Start() {
103 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500104 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700105 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500106 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700107 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
108 RTNLHandler::kRequestAddr);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700109}
110
Paul Stewarta3c56f92011-05-26 07:08:52 -0700111void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700112 link_listener_.reset();
113 address_listener_.reset();
Paul Stewart8c116a92012-05-02 18:30:03 -0700114 infos_.clear();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700115}
116
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700117void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700118 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
119 << device->interface_index() << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700120 CHECK(!GetDevice(device->interface_index()).get());
121 infos_[device->interface_index()].device = device;
Paul Stewartfdd16072011-09-16 12:41:35 -0700122 if (device->TechnologyIs(Technology::kCellular) ||
123 device->TechnologyIs(Technology::kEthernet) ||
124 device->TechnologyIs(Technology::kWifi)) {
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 << ")";
Jason Glasgowe9089492012-02-23 17:57:37 -0500134 CHECK(device->TechnologyIs(Technology::kCellular));
135
136 // Release reference to the device
137 map<int, Info>::iterator iter = infos_.find(interface_index);
138 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700139 SLOG(Device, 2) << "Removing device from info for index: "
140 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500141 manager_->DeregisterDevice(device);
142 // Release the reference to the device, but maintain the mapping
143 // for the index. That will be cleaned up by an RTNL message.
144 iter->second.device = NULL;
145 }
146}
147
Paul Stewartca876ee2012-04-21 08:55:58 -0700148FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name,
149 const string &path_name) {
150 return device_info_root_.Append(iface_name).Append(path_name);
151}
152
153bool DeviceInfo::GetDeviceInfoContents(const string &iface_name,
154 const string &path_name,
155 string *contents_out) {
156 return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
157 contents_out);
158}
159bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700160 const string &path_name,
161 FilePath *path_out) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700162 return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
163 path_out);
164}
165
Paul Stewartfdd16072011-09-16 12:41:35 -0700166Technology::Identifier DeviceInfo::GetDeviceTechnology(
167 const string &iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700168 string type_string;
169 int arp_type = ARPHRD_VOID;;
170 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
171 TrimString(type_string, "\n", &type_string) &&
172 !base::StringToInt(type_string, &arp_type)) {
173 arp_type = ARPHRD_VOID;
174 }
175
Paul Stewart9364c4c2011-12-06 17:12:42 -0800176 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700177 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700178 SLOG(Device, 2) << StringPrintf("%s: device %s has no uevent file",
179 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700180 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800181 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700182
Thieu Le8f1c8352012-04-16 11:02:12 -0700183 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
184 // start of the file or after a newline, we can safely assume this
185 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800186 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700187 SLOG(Device, 2)
188 << StringPrintf("%s: device %s has wifi signature in uevent file",
189 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700190 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700191 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
192 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800193 return Technology::kWiFiMonitor;
194 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700195 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700196 }
197
Paul Stewart9364c4c2011-12-06 17:12:42 -0800198 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700199 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700200 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
201 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700202 if (arp_type == ARPHRD_LOOPBACK) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700203 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
204 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700205 return Technology::kLoopback;
206 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700207 if (arp_type == ARPHRD_PPP) {
208 SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device",
209 __func__, iface_name.c_str());
210 return Technology::kPPP;
211 }
212 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800213 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700214 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
215 TrimString(tun_flags_str, "\n", &tun_flags_str) &&
216 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800217 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700218 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
219 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800220 return Technology::kTunnel;
221 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700222 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800223 }
224
225 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700226 // See if driver for this interface is in a list of known modem driver names.
227 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
228 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800229 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700230 SLOG(Device, 2)
231 << StringPrintf("%s: device %s is matched with modem driver %s",
232 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800233 return Technology::kCellular;
234 }
235 }
236
Ben Chan4e64d2d2012-05-16 00:02:25 -0700237 if (driver_name == kDriverGdmWiMax) {
238 SLOG(Device, 2) << StringPrintf("%s: device %s is a WiMAX device",
239 __func__, iface_name.c_str());
240 return Technology::kWiMax;
241 }
242
Thieu Le8f1c8352012-04-16 11:02:12 -0700243 // For cdc_ether devices, make sure it's a modem because this driver
244 // can be used for other ethernet devices.
245 if (driver_name == kDriverCdcEther &&
246 IsCdcEtherModemDevice(iface_name)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700247 SLOG(Device, 2) << StringPrintf("%s: device %s is a modem cdc_ether device",
248 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700249 return Technology::kCellular;
250 }
251
Jason Glasgowabc54032012-04-20 16:08:32 -0400252 // Special case for pseudo modems which are used for testing
253 if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) {
254 SLOG(Device, 2) << StringPrintf(
255 "%s: device %s is a pseudo modem for testing",
256 __func__, iface_name.c_str());
257 return Technology::kCellular;
258 }
259
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700260 // Special case for the virtio driver, used when run under KVM. See also
261 // the comment in VirtioEthernet::Start.
262 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700263 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
264 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700265 return Technology::kVirtioEthernet;
266 }
267
Ben Chanfad4a0b2012-04-18 15:49:59 -0700268 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
269 "is defaulted to type ethernet",
270 __func__, iface_name.c_str(),
271 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700272 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700273}
274
Thieu Le8f1c8352012-04-16 11:02:12 -0700275bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
276 // A cdc_ether device is a modem device if it also exposes tty interfaces.
277 // To determine this, we look for the existence of the tty interface in the
278 // USB device sysfs tree.
279 //
280 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
281 // follows:
282 //
283 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
284 // 1-2:1.0
285 // tty
286 // ttyACM0
287 // 1-2:1.1
288 // net
289 // usb0
290 // 1-2:1.2
291 // tty
292 // ttyACM1
293 // ...
294 //
295 // /sys/class/net/usb0/device symlinks to
296 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700297 //
298 // Note that some modem devices have the tty directory one level deeper
299 // (eg. E362), so the device tree for the tty interface is:
300 // /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 -0700301
Paul Stewartca876ee2012-04-21 08:55:58 -0700302 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700303 FilePath device_path;
304 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700305 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
306 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700307 return false;
308 }
309 if (!device_path.IsAbsolute()) {
310 device_path = device_file.DirName().Append(device_path);
311 file_util::AbsolutePath(&device_path);
312 }
313
314 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700315 // USB device and see if there's a subdirectory "tty" inside. In other
316 // words, using the example dir hierarchy above, find
317 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700318 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700319 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700320}
321
322// static
Thieu Leb27beee2012-04-20 09:19:06 -0700323bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
324 file_util::FileEnumerator::FileType type =
325 static_cast<file_util::FileEnumerator::FileType>(
326 file_util::FileEnumerator::DIRECTORIES |
327 file_util::FileEnumerator::SHOW_SYM_LINKS);
328 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700329 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
330 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700331 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700332 return true;
333 }
334 return false;
335}
336
Paul Stewart8c116a92012-05-02 18:30:03 -0700337DeviceRefPtr DeviceInfo::CreateDevice(const string &link_name,
338 const string &address,
339 int interface_index,
340 Technology::Identifier technology) {
341 DeviceRefPtr device;
342
343 switch (technology) {
344 case Technology::kCellular:
345 // Cellular devices are managed by ModemInfo.
346 SLOG(Device, 2) << "Cellular link " << link_name
347 << " at index " << interface_index
348 << " -- notifying ModemInfo.";
349 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
350 break;
351 case Technology::kEthernet:
352 device = new Ethernet(control_interface_, dispatcher_, metrics_,
353 manager_, link_name, address, interface_index);
354 device->EnableIPv6Privacy();
355 break;
356 case Technology::kVirtioEthernet:
357 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
358 manager_, link_name, address,
359 interface_index);
360 device->EnableIPv6Privacy();
361 break;
362 case Technology::kWifi:
363 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
364 link_name, address, interface_index);
365 device->EnableIPv6Privacy();
366 break;
Ben Chan4e64d2d2012-05-16 00:02:25 -0700367 case Technology::kWiMax:
368 // TODO(benchan): Identify the object path.
369 // device = new WiMax(control_interface_, dispatcher_, metrics_, manager_,
370 // link_name, address, interface_index,
371 // /* object_path */);
372 break;
Paul Stewart8c116a92012-05-02 18:30:03 -0700373 case Technology::kPPP:
374 case Technology::kTunnel:
375 // Tunnel and PPP devices are managed by the VPN code (PPP for
376 // l2tpipsec). Notify the VPN Provider of the interface's presence.
377 // Since CreateDevice is only called once in the lifetime of an
378 // interface index, this notification will only occur the first
379 // time the device is seen.
380 SLOG(Device, 2) << "Tunnel / PPP link " << link_name
381 << " at index " << interface_index
382 << " -- notifying VPNProvider.";
383 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
384 interface_index) &&
385 technology == Technology::kTunnel) {
386 // If VPN does not know anything about this tunnel, it is probably
387 // left over from a previous instance and should not exist.
388 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
389 DeleteInterface(interface_index);
390 }
391 break;
392 case Technology::kLoopback:
393 // Loopback devices are largely ignored, but we should make sure the
394 // link is enabled.
395 SLOG(Device, 2) << "Bringing up loopback device " << link_name
396 << " at index " << interface_index;
397 rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP);
398 return NULL;
399 default:
400 // We will not manage this device in shill. Do not create a device
401 // object or do anything to change its state. We create a stub object
402 // which is useful for testing.
403 return new DeviceStub(control_interface_, dispatcher_, metrics_,
404 manager_, link_name, address, interface_index,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700405 technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700406 }
407
408 // Reset the routing table and addresses.
409 routing_table_->FlushRoutes(interface_index);
410 FlushAddresses(interface_index);
411
412 return device;
413}
414
Chris Masone2aa97072011-08-09 17:35:08 -0700415void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700416 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
417 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700418 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700419 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700420
Darin Petkove6193c02011-08-11 12:42:40 -0700421 unsigned int flags = msg.link_status().flags;
422 unsigned int change = msg.link_status().change;
Paul Stewart8c116a92012-05-02 18:30:03 -0700423 bool new_device =
424 !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700425 SLOG(Device, 2) << __func__ << "(index=" << dev_index
426 << std::showbase << std::hex
427 << ", flags=" << flags << ", change=" << change << ")"
428 << std::dec << std::noshowbase
429 << ", new_device=" << new_device;
Paul Stewart8c116a92012-05-02 18:30:03 -0700430 infos_[dev_index].has_addresses_only = false;
Darin Petkove6193c02011-08-11 12:42:40 -0700431 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700432
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700433 DeviceRefPtr device = GetDevice(dev_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700434 if (new_device) {
435 CHECK(!device);
Chris Masone2aa97072011-08-09 17:35:08 -0700436 if (!msg.HasAttribute(IFLA_IFNAME)) {
437 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
438 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700439 }
Chris Masone2aa97072011-08-09 17:35:08 -0700440 ByteString b(msg.GetAttribute(IFLA_IFNAME));
441 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700442 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200443 infos_[dev_index].name = link_name;
444 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700445
Chris Masone2aa97072011-08-09 17:35:08 -0700446 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700447 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700448 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700449 } else {
450 technology = GetDeviceTechnology(link_name);
451 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700452 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800453 string address;
454 if (msg.HasAttribute(IFLA_ADDRESS)) {
455 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
456 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700457 SLOG(Device, 2) << "link index " << dev_index << " address "
458 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700459 } else if (technology != Technology::kTunnel &&
460 technology != Technology::kPPP) {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800461 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
462 return;
463 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700464 device = CreateDevice(link_name, address, dev_index, technology);
465 if (device) {
466 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700467 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700468 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700469 if (device) {
470 device->LinkEvent(flags, change);
471 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700472}
473
Chris Masone2aa97072011-08-09 17:35:08 -0700474void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700475 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700476
Paul Stewart9a908082011-08-31 12:18:48 -0700477 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
478 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700479 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
480 << std::showbase << std::hex
481 << ", flags=" << msg.link_status().flags
482 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700483 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700484}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700485
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700486DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
487 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700488 return info ? info->device : NULL;
489}
490
Darin Petkovf8046b82012-04-24 16:29:23 +0200491int DeviceInfo::GetIndex(const string &interface_name) const {
492 map<string, int>::const_iterator it = indices_.find(interface_name);
493 return it == indices_.end() ? -1 : it->second;
494}
495
Paul Stewart32852962011-08-30 14:06:53 -0700496bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700497 const Info *info = GetInfo(interface_index);
498 if (!info) {
499 return false;
500 }
Paul Stewart32852962011-08-30 14:06:53 -0700501 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700502 return true;
503}
504
Paul Stewart9a908082011-08-31 12:18:48 -0700505bool DeviceInfo::GetAddresses(int interface_index,
506 vector<AddressData> *addresses) const {
507 const Info *info = GetInfo(interface_index);
508 if (!info) {
509 return false;
510 }
511 *addresses = info->ip_addresses;
512 return true;
513}
514
515void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700516 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700517 const Info *info = GetInfo(interface_index);
518 if (!info) {
519 return;
520 }
521 const vector<AddressData> &addresses = info->ip_addresses;
522 vector<AddressData>::const_iterator iter;
523 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700524 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700525 (iter->scope == RT_SCOPE_UNIVERSE &&
526 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700527 SLOG(Device, 2) << __func__ << ": removing ip address "
528 << iter->address.ToString()
529 << " from interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700530 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
531 }
532 }
533}
534
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700535bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
536 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700537 if (!info) {
538 return false;
539 }
540 *flags = info->flags;
541 return true;
542}
543
Paul Stewartca6abd42012-03-01 15:45:29 -0800544bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800545 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
546 if (fd < 0) {
547 PLOG(ERROR) << "failed to open " << kTunDeviceName;
548 return false;
549 }
550 file_util::ScopedFD scoped_fd(&fd);
551
552 struct ifreq ifr;
553 memset(&ifr, 0, sizeof(ifr));
554 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
555 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
556 PLOG(ERROR) << "failed to create tunnel interface";
557 return false;
558 }
559
560 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
561 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
562 return false;
563 }
564
565 *interface_name = string(ifr.ifr_name);
566
567 return true;
568}
569
Paul Stewartca6abd42012-03-01 15:45:29 -0800570bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800571 return rtnl_handler_->RemoveInterface(interface_index);
572}
573
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700574const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
575 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700576 if (iter == infos_.end()) {
577 return NULL;
578 }
579 return &iter->second;
580}
581
582void DeviceInfo::RemoveInfo(int interface_index) {
583 map<int, Info>::iterator iter = infos_.find(interface_index);
584 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700585 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700586 if (iter->second.device.get()) {
587 manager_->DeregisterDevice(iter->second.device);
588 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200589 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700590 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700591 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700592 SLOG(Device, 2) << __func__ << ": Unknown device index: "
593 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700594 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700595}
596
Chris Masone2aa97072011-08-09 17:35:08 -0700597void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700598 DCHECK(msg.type() == RTNLMessage::kTypeLink);
599 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700600 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700601 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700602 DelLinkMsgHandler(msg);
603 } else {
604 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700605 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700606}
607
Paul Stewart9a908082011-08-31 12:18:48 -0700608void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700609 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700610 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
611 int interface_index = msg.interface_index();
612 if (!ContainsKey(infos_, interface_index)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700613 SLOG(Device, 2) << "Got advance address information for unknown index "
614 << interface_index;
615 infos_[interface_index].has_addresses_only = true;
Paul Stewart9a908082011-08-31 12:18:48 -0700616 }
617 const RTNLMessage::AddressStatus &status = msg.address_status();
618 IPAddress address(msg.family(),
619 msg.GetAttribute(IFA_ADDRESS),
620 status.prefix_len);
621
622 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
623 vector<AddressData>::iterator iter;
624 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
625 if (address.Equals(iter->address)) {
626 break;
627 }
628 }
629 if (iter != address_list.end()) {
630 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700631 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700632 address_list.erase(iter);
633 } else {
634 iter->flags = status.flags;
635 iter->scope = status.scope;
636 }
637 } else if (msg.mode() == RTNLMessage::kModeAdd) {
638 address_list.push_back(AddressData(address, status.flags, status.scope));
Paul Stewart8c116a92012-05-02 18:30:03 -0700639 SLOG(Device, 2) << "Add address " << address.ToString()
640 << " for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700641 }
642}
643
Paul Stewart0af98bf2011-05-10 17:38:08 -0700644} // namespace shill