blob: 21e1d6b09c36033f9a7e25de864b08003d2fcbb1 [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 Stewarta3c56f92011-05-26 07:08:52 -070037#include "shill/rtnl_handler.h"
38#include "shill/rtnl_listener.h"
Chris Masone2aa97072011-08-09 17:35:08 -070039#include "shill/rtnl_message.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070040#include "shill/scope_logger.h"
Chris Masone487b8bf2011-05-13 16:27:57 -070041#include "shill/service.h"
mukesh agrawal93a29ed2012-04-17 16:13:01 -070042#include "shill/virtio_ethernet.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070043#include "shill/wifi.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070044
Eric Shienbrood3e20a232012-02-16 11:35:56 -050045using base::Bind;
Thieu Le8f1c8352012-04-16 11:02:12 -070046using base::StringPrintf;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050047using base::Unretained;
Darin Petkove6193c02011-08-11 12:42:40 -070048using std::map;
Paul Stewart0af98bf2011-05-10 17:38:08 -070049using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070050using std::vector;
Paul Stewart0af98bf2011-05-10 17:38:08 -070051
52namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070053
54// static
Paul Stewartca876ee2012-04-21 08:55:58 -070055const char DeviceInfo::kDeviceInfoRoot[] = "/sys/class/net";
mukesh agrawal93a29ed2012-04-17 16:13:01 -070056const char DeviceInfo::kDriverVirtioNet[] = "virtio_net";
Paul Stewartca876ee2012-04-21 08:55:58 -070057const char DeviceInfo::kInterfaceUevent[] = "uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080058const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Paul Stewartca876ee2012-04-21 08:55:58 -070059const char DeviceInfo::kInterfaceDevice[] = "device";
60const char DeviceInfo::kInterfaceDriver[] = "device/driver";
61const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags";
62const char DeviceInfo::kInterfaceType[] = "type";
Thieu Le8f1c8352012-04-16 11:02:12 -070063const char DeviceInfo::kDriverCdcEther[] = "cdc_ether";
Paul Stewartb50f0b92011-05-16 16:31:42 -070064const char *DeviceInfo::kModemDrivers[] = {
65 "gobi",
66 "QCUSBNet2k",
Thieu Le8f1c8352012-04-16 11:02:12 -070067 "GobiNet"
Paul Stewartb50f0b92011-05-16 16:31:42 -070068};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080069const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewartb50f0b92011-05-16 16:31:42 -070070
71DeviceInfo::DeviceInfo(ControlInterface *control_interface,
72 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080073 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070074 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070075 : control_interface_(control_interface),
76 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080077 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070078 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050079 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
80 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070081 link_listener_(NULL),
82 address_listener_(NULL),
Paul Stewartca876ee2012-04-21 08:55:58 -070083 device_info_root_(kDeviceInfoRoot),
Paul Stewart9a908082011-08-31 12:18:48 -070084 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070085}
86
Paul Stewarta3c56f92011-05-26 07:08:52 -070087DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070088
mukesh agrawal8f317b62011-07-15 11:53:23 -070089void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
90 black_list_.insert(device_name);
91}
92
Eric Shienbrood5e628a52012-03-21 16:56:59 -040093bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
94 return ContainsKey(black_list_, device_name);
95}
96
Paul Stewarta3c56f92011-05-26 07:08:52 -070097void DeviceInfo::Start() {
98 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -050099 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700100 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500101 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700102 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
103 RTNLHandler::kRequestAddr);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700104}
105
Paul Stewarta3c56f92011-05-26 07:08:52 -0700106void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700107 link_listener_.reset();
108 address_listener_.reset();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700109}
110
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700111void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700112 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
113 << device->interface_index() << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700114 CHECK(!GetDevice(device->interface_index()).get());
115 infos_[device->interface_index()].device = device;
Paul Stewartfdd16072011-09-16 12:41:35 -0700116 if (device->TechnologyIs(Technology::kCellular) ||
117 device->TechnologyIs(Technology::kEthernet) ||
118 device->TechnologyIs(Technology::kWifi)) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700119 manager_->RegisterDevice(device);
120 }
121}
122
Jason Glasgowe9089492012-02-23 17:57:37 -0500123void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
124 int interface_index = device->interface_index();
125
Ben Chanfad4a0b2012-04-18 15:49:59 -0700126 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
127 << interface_index << ")";
Jason Glasgowe9089492012-02-23 17:57:37 -0500128 CHECK(device->TechnologyIs(Technology::kCellular));
129
130 // Release reference to the device
131 map<int, Info>::iterator iter = infos_.find(interface_index);
132 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700133 SLOG(Device, 2) << "Removing device from info for index: "
134 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500135 manager_->DeregisterDevice(device);
136 // Release the reference to the device, but maintain the mapping
137 // for the index. That will be cleaned up by an RTNL message.
138 iter->second.device = NULL;
139 }
140}
141
Paul Stewartca876ee2012-04-21 08:55:58 -0700142FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name,
143 const string &path_name) {
144 return device_info_root_.Append(iface_name).Append(path_name);
145}
146
147bool DeviceInfo::GetDeviceInfoContents(const string &iface_name,
148 const string &path_name,
149 string *contents_out) {
150 return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
151 contents_out);
152}
153bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name,
154 const string &path_name,
155 FilePath *path_out) {
156 return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
157 path_out);
158}
159
Paul Stewartfdd16072011-09-16 12:41:35 -0700160Technology::Identifier DeviceInfo::GetDeviceTechnology(
161 const string &iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700162 string type_string;
163 int arp_type = ARPHRD_VOID;;
164 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
165 TrimString(type_string, "\n", &type_string) &&
166 !base::StringToInt(type_string, &arp_type)) {
167 arp_type = ARPHRD_VOID;
168 }
169
Paul Stewart9364c4c2011-12-06 17:12:42 -0800170 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700171 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700172 SLOG(Device, 2) << StringPrintf("%s: device %s has no uevent file",
173 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700174 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800175 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700176
Thieu Le8f1c8352012-04-16 11:02:12 -0700177 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
178 // start of the file or after a newline, we can safely assume this
179 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800180 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700181 SLOG(Device, 2)
182 << StringPrintf("%s: device %s has wifi signature in uevent file",
183 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700184 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700185 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
186 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800187 return Technology::kWiFiMonitor;
188 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700189 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700190 }
191
Paul Stewart9364c4c2011-12-06 17:12:42 -0800192 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700193 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700194 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
195 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700196 if (arp_type == ARPHRD_LOOPBACK) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700197 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
198 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700199 return Technology::kLoopback;
200 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700201 if (arp_type == ARPHRD_PPP) {
202 SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device",
203 __func__, iface_name.c_str());
204 return Technology::kPPP;
205 }
206 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800207 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700208 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
209 TrimString(tun_flags_str, "\n", &tun_flags_str) &&
210 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800211 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700212 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
213 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800214 return Technology::kTunnel;
215 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700216 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800217 }
218
219 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700220 // See if driver for this interface is in a list of known modem driver names.
221 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
222 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800223 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700224 SLOG(Device, 2)
225 << StringPrintf("%s: device %s is matched with modem driver %s",
226 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800227 return Technology::kCellular;
228 }
229 }
230
Thieu Le8f1c8352012-04-16 11:02:12 -0700231 // For cdc_ether devices, make sure it's a modem because this driver
232 // can be used for other ethernet devices.
233 if (driver_name == kDriverCdcEther &&
234 IsCdcEtherModemDevice(iface_name)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700235 SLOG(Device, 2) << StringPrintf("%s: device %s is a modem cdc_ether device",
236 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700237 return Technology::kCellular;
238 }
239
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700240 // Special case for the virtio driver, used when run under KVM. See also
241 // the comment in VirtioEthernet::Start.
242 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700243 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
244 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700245 return Technology::kVirtioEthernet;
246 }
247
Ben Chanfad4a0b2012-04-18 15:49:59 -0700248 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
249 "is defaulted to type ethernet",
250 __func__, iface_name.c_str(),
251 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700252 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700253}
254
Thieu Le8f1c8352012-04-16 11:02:12 -0700255bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
256 // A cdc_ether device is a modem device if it also exposes tty interfaces.
257 // To determine this, we look for the existence of the tty interface in the
258 // USB device sysfs tree.
259 //
260 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
261 // follows:
262 //
263 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
264 // 1-2:1.0
265 // tty
266 // ttyACM0
267 // 1-2:1.1
268 // net
269 // usb0
270 // 1-2:1.2
271 // tty
272 // ttyACM1
273 // ...
274 //
275 // /sys/class/net/usb0/device symlinks to
276 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700277 //
278 // Note that some modem devices have the tty directory one level deeper
279 // (eg. E362), so the device tree for the tty interface is:
280 // /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 -0700281
Paul Stewartca876ee2012-04-21 08:55:58 -0700282 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700283 FilePath device_path;
284 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700285 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
286 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700287 return false;
288 }
289 if (!device_path.IsAbsolute()) {
290 device_path = device_file.DirName().Append(device_path);
291 file_util::AbsolutePath(&device_path);
292 }
293
294 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700295 // USB device and see if there's a subdirectory "tty" inside. In other
296 // words, using the example dir hierarchy above, find
297 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700298 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700299 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700300}
301
302// static
Thieu Leb27beee2012-04-20 09:19:06 -0700303bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
304 file_util::FileEnumerator::FileType type =
305 static_cast<file_util::FileEnumerator::FileType>(
306 file_util::FileEnumerator::DIRECTORIES |
307 file_util::FileEnumerator::SHOW_SYM_LINKS);
308 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700309 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
310 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700311 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700312 return true;
313 }
314 return false;
315}
316
Chris Masone2aa97072011-08-09 17:35:08 -0700317void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700318 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
319 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700320 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700321 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700322
Darin Petkove6193c02011-08-11 12:42:40 -0700323 unsigned int flags = msg.link_status().flags;
324 unsigned int change = msg.link_status().change;
Paul Stewart536820d2012-03-19 16:05:59 -0700325 bool new_device = !ContainsKey(infos_, dev_index);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700326 SLOG(Device, 2) << __func__ << "(index=" << dev_index
327 << std::showbase << std::hex
328 << ", flags=" << flags << ", change=" << change << ")"
329 << std::dec << std::noshowbase
330 << ", new_device=" << new_device;
Darin Petkove6193c02011-08-11 12:42:40 -0700331 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700332
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700333 DeviceRefPtr device = GetDevice(dev_index);
334 if (!device.get()) {
Chris Masone2aa97072011-08-09 17:35:08 -0700335 if (!msg.HasAttribute(IFLA_IFNAME)) {
336 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
337 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700338 }
Chris Masone2aa97072011-08-09 17:35:08 -0700339 ByteString b(msg.GetAttribute(IFLA_IFNAME));
340 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700341 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200342 infos_[dev_index].name = link_name;
343 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700344
Chris Masone2aa97072011-08-09 17:35:08 -0700345 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700346 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700347 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700348 } else {
349 technology = GetDeviceTechnology(link_name);
350 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700351 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800352 string address;
353 if (msg.HasAttribute(IFLA_ADDRESS)) {
354 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
355 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700356 SLOG(Device, 2) << "link index " << dev_index << " address "
357 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700358 } else if (technology != Technology::kTunnel &&
359 technology != Technology::kPPP) {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800360 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
361 return;
362 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700363 switch (technology) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700364 case Technology::kCellular:
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700365 // Cellular devices are managed by ModemInfo.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700366 SLOG(Device, 2) << "Cellular link " << link_name
367 << " at index " << dev_index
368 << " -- notifying ModemInfo.";
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100369 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700370 return;
Paul Stewartfdd16072011-09-16 12:41:35 -0700371 case Technology::kEthernet:
Thieu Le3426c8f2012-01-11 17:35:11 -0800372 device = new Ethernet(control_interface_, dispatcher_, metrics_,
373 manager_, link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800374 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700375 break;
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700376 case Technology::kVirtioEthernet:
377 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
378 manager_, link_name, address, dev_index);
379 device->EnableIPv6Privacy();
380 break;
Paul Stewartfdd16072011-09-16 12:41:35 -0700381 case Technology::kWifi:
Thieu Le3426c8f2012-01-11 17:35:11 -0800382 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700383 link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800384 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700385 break;
Paul Stewartca876ee2012-04-21 08:55:58 -0700386 case Technology::kPPP:
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800387 case Technology::kTunnel:
Paul Stewartca876ee2012-04-21 08:55:58 -0700388 // Tunnel and PPP devices are managed by the VPN code (PPP for
389 // l2tpipsec). Notify the VPN Provider only if this is the first
390 // time we have seen this device index.
Paul Stewart536820d2012-03-19 16:05:59 -0700391 if (new_device) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700392 SLOG(Device, 2) << "Tunnel / PPP link " << link_name
Ben Chanfad4a0b2012-04-18 15:49:59 -0700393 << " at index " << dev_index
394 << " -- notifying VPNProvider.";
Paul Stewart536820d2012-03-19 16:05:59 -0700395 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
Paul Stewartca876ee2012-04-21 08:55:58 -0700396 dev_index) &&
397 technology == Technology::kTunnel) {
Paul Stewart536820d2012-03-19 16:05:59 -0700398 // If VPN does not know anything about this tunnel, it is probably
399 // left over from a previous instance and should not exist.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700400 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
Paul Stewart536820d2012-03-19 16:05:59 -0700401 DeleteInterface(dev_index);
402 }
Paul Stewartca6abd42012-03-01 15:45:29 -0800403 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800404 return;
Paul Stewarte81eb702012-04-11 15:04:53 -0700405 case Technology::kLoopback:
406 // Loopback devices are largely ignored, but we should make sure the
407 // link is enabled.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700408 SLOG(Device, 2) << "Bringing up loopback device " << link_name
409 << " at index " << dev_index;
Paul Stewarte81eb702012-04-11 15:04:53 -0700410 rtnl_handler_->SetInterfaceFlags(dev_index, IFF_UP, IFF_UP);
411 return;
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700412 default:
Thieu Le3426c8f2012-01-11 17:35:11 -0800413 device = new DeviceStub(control_interface_, dispatcher_, metrics_,
414 manager_, link_name, address, dev_index,
415 technology);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700416 break;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700417 }
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700418 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700419 }
Darin Petkove6193c02011-08-11 12:42:40 -0700420 device->LinkEvent(flags, change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700421}
422
Chris Masone2aa97072011-08-09 17:35:08 -0700423void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700424 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700425
Paul Stewart9a908082011-08-31 12:18:48 -0700426 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
427 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700428 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
429 << std::showbase << std::hex
430 << ", flags=" << msg.link_status().flags
431 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700432 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700433}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700434
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700435DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
436 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700437 return info ? info->device : NULL;
438}
439
Darin Petkovf8046b82012-04-24 16:29:23 +0200440int DeviceInfo::GetIndex(const string &interface_name) const {
441 map<string, int>::const_iterator it = indices_.find(interface_name);
442 return it == indices_.end() ? -1 : it->second;
443}
444
Paul Stewart32852962011-08-30 14:06:53 -0700445bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700446 const Info *info = GetInfo(interface_index);
447 if (!info) {
448 return false;
449 }
Paul Stewart32852962011-08-30 14:06:53 -0700450 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700451 return true;
452}
453
Paul Stewart9a908082011-08-31 12:18:48 -0700454bool DeviceInfo::GetAddresses(int interface_index,
455 vector<AddressData> *addresses) const {
456 const Info *info = GetInfo(interface_index);
457 if (!info) {
458 return false;
459 }
460 *addresses = info->ip_addresses;
461 return true;
462}
463
464void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700465 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700466 const Info *info = GetInfo(interface_index);
467 if (!info) {
468 return;
469 }
470 const vector<AddressData> &addresses = info->ip_addresses;
471 vector<AddressData>::const_iterator iter;
472 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700473 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700474 (iter->scope == RT_SCOPE_UNIVERSE &&
475 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700476 SLOG(Device, 2) << __func__ << ": removing ip address from interface "
477 << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700478 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
479 }
480 }
481}
482
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700483bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
484 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700485 if (!info) {
486 return false;
487 }
488 *flags = info->flags;
489 return true;
490}
491
Paul Stewartca6abd42012-03-01 15:45:29 -0800492bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800493 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
494 if (fd < 0) {
495 PLOG(ERROR) << "failed to open " << kTunDeviceName;
496 return false;
497 }
498 file_util::ScopedFD scoped_fd(&fd);
499
500 struct ifreq ifr;
501 memset(&ifr, 0, sizeof(ifr));
502 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
503 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
504 PLOG(ERROR) << "failed to create tunnel interface";
505 return false;
506 }
507
508 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
509 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
510 return false;
511 }
512
513 *interface_name = string(ifr.ifr_name);
514
515 return true;
516}
517
Paul Stewartca6abd42012-03-01 15:45:29 -0800518bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800519 return rtnl_handler_->RemoveInterface(interface_index);
520}
521
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700522const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
523 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700524 if (iter == infos_.end()) {
525 return NULL;
526 }
527 return &iter->second;
528}
529
530void DeviceInfo::RemoveInfo(int interface_index) {
531 map<int, Info>::iterator iter = infos_.find(interface_index);
532 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700533 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700534 if (iter->second.device.get()) {
535 manager_->DeregisterDevice(iter->second.device);
536 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200537 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700538 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700539 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700540 SLOG(Device, 2) << __func__ << ": Unknown device index: "
541 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700542 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700543}
544
Chris Masone2aa97072011-08-09 17:35:08 -0700545void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700546 DCHECK(msg.type() == RTNLMessage::kTypeLink);
547 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700548 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700549 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700550 DelLinkMsgHandler(msg);
551 } else {
552 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700553 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700554}
555
Paul Stewart9a908082011-08-31 12:18:48 -0700556void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700557 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700558 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
559 int interface_index = msg.interface_index();
560 if (!ContainsKey(infos_, interface_index)) {
561 LOG(ERROR) << "Got address type message for unknown index "
562 << interface_index;
563 return;
564 }
565 const RTNLMessage::AddressStatus &status = msg.address_status();
566 IPAddress address(msg.family(),
567 msg.GetAttribute(IFA_ADDRESS),
568 status.prefix_len);
569
570 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
571 vector<AddressData>::iterator iter;
572 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
573 if (address.Equals(iter->address)) {
574 break;
575 }
576 }
577 if (iter != address_list.end()) {
578 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700579 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700580 address_list.erase(iter);
581 } else {
582 iter->flags = status.flags;
583 iter->scope = status.scope;
584 }
585 } else if (msg.mode() == RTNLMessage::kModeAdd) {
586 address_list.push_back(AddressData(address, status.flags, status.scope));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700587 SLOG(Device, 2) << "Add address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700588 }
589}
590
Paul Stewart0af98bf2011-05-10 17:38:08 -0700591} // namespace shill