blob: 7cf21045835b6377d7c4e2169cfcb59a7f34f20e [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
mukesh agrawal93a29ed2012-04-17 16:13:01 -070055const char DeviceInfo::kDriverVirtioNet[] = "virtio_net";
Paul Stewartbf1861b2011-08-23 15:45:35 -070056const char DeviceInfo::kInterfaceUevent[] = "/sys/class/net/%s/uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080057const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Thieu Le8f1c8352012-04-16 11:02:12 -070058const char DeviceInfo::kInterfaceDevice[] = "/sys/class/net/%s/device";
Paul Stewartbf1861b2011-08-23 15:45:35 -070059const char DeviceInfo::kInterfaceDriver[] = "/sys/class/net/%s/device/driver";
Paul Stewartcba0f7f2012-02-29 16:33:05 -080060const char DeviceInfo::kInterfaceTunFlags[] = "/sys/class/net/%s/tun_flags";
Paul Stewart2001a422011-12-15 10:20:09 -080061const char DeviceInfo::kInterfaceType[] = "/sys/class/net/%s/type";
Paul Stewarte81eb702012-04-11 15:04:53 -070062const char DeviceInfo::kLoopbackDeviceName[] = "lo";
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),
83 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070084}
85
Paul Stewarta3c56f92011-05-26 07:08:52 -070086DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070087
mukesh agrawal8f317b62011-07-15 11:53:23 -070088void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
89 black_list_.insert(device_name);
90}
91
Eric Shienbrood5e628a52012-03-21 16:56:59 -040092bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
93 return ContainsKey(black_list_, device_name);
94}
95
Paul Stewarta3c56f92011-05-26 07:08:52 -070096void DeviceInfo::Start() {
97 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -050098 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -070099 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500100 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700101 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
102 RTNLHandler::kRequestAddr);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700103}
104
Paul Stewarta3c56f92011-05-26 07:08:52 -0700105void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700106 link_listener_.reset();
107 address_listener_.reset();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700108}
109
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700110void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700111 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
112 << device->interface_index() << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700113 CHECK(!GetDevice(device->interface_index()).get());
114 infos_[device->interface_index()].device = device;
Paul Stewartfdd16072011-09-16 12:41:35 -0700115 if (device->TechnologyIs(Technology::kCellular) ||
116 device->TechnologyIs(Technology::kEthernet) ||
117 device->TechnologyIs(Technology::kWifi)) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700118 manager_->RegisterDevice(device);
119 }
120}
121
Jason Glasgowe9089492012-02-23 17:57:37 -0500122void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
123 int interface_index = device->interface_index();
124
Ben Chanfad4a0b2012-04-18 15:49:59 -0700125 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
126 << interface_index << ")";
Jason Glasgowe9089492012-02-23 17:57:37 -0500127 CHECK(device->TechnologyIs(Technology::kCellular));
128
129 // Release reference to the device
130 map<int, Info>::iterator iter = infos_.find(interface_index);
131 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700132 SLOG(Device, 2) << "Removing device from info for index: "
133 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500134 manager_->DeregisterDevice(device);
135 // Release the reference to the device, but maintain the mapping
136 // for the index. That will be cleaned up by an RTNL message.
137 iter->second.device = NULL;
138 }
139}
140
Thieu Le8f1c8352012-04-16 11:02:12 -0700141// static
Paul Stewartfdd16072011-09-16 12:41:35 -0700142Technology::Identifier DeviceInfo::GetDeviceTechnology(
143 const string &iface_name) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800144 FilePath uevent_file(StringPrintf(kInterfaceUevent, iface_name.c_str()));
145 string contents;
146 if (!file_util::ReadFileToString(uevent_file, &contents)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700147 SLOG(Device, 2) << StringPrintf("%s: device %s has no uevent file",
148 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700149 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800150 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700151
Thieu Le8f1c8352012-04-16 11:02:12 -0700152 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
153 // start of the file or after a newline, we can safely assume this
154 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800155 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700156 SLOG(Device, 2)
157 << StringPrintf("%s: device %s has wifi signature in uevent file",
158 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800159 FilePath type_file(StringPrintf(kInterfaceType, iface_name.c_str()));
160 string type_string;
161 int type_val = 0;
162 if (file_util::ReadFileToString(type_file, &type_string) &&
163 TrimString(type_string, "\n", &type_string) &&
164 base::StringToInt(type_string, &type_val) &&
165 type_val == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700166 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
167 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800168 return Technology::kWiFiMonitor;
169 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700170 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700171 }
172
Paul Stewart9364c4c2011-12-06 17:12:42 -0800173 FilePath driver_file(StringPrintf(kInterfaceDriver, iface_name.c_str()));
174 FilePath driver_path;
175 if (!file_util::ReadSymbolicLink(driver_file, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700176 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
177 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700178 if (iface_name == kLoopbackDeviceName) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700179 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
180 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700181 return Technology::kLoopback;
182 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800183 FilePath tun_flags_file(StringPrintf(kInterfaceTunFlags,
184 iface_name.c_str()));
185 string tun_flags_string;
186 int tun_flags = 0;
187 if (file_util::ReadFileToString(tun_flags_file, &tun_flags_string) &&
188 TrimString(tun_flags_string, "\n", &tun_flags_string) &&
189 base::HexStringToInt(tun_flags_string, &tun_flags) &&
190 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700191 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
192 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800193 return Technology::kTunnel;
194 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700195 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800196 }
197
198 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700199 // See if driver for this interface is in a list of known modem driver names.
200 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
201 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800202 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700203 SLOG(Device, 2)
204 << StringPrintf("%s: device %s is matched with modem driver %s",
205 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800206 return Technology::kCellular;
207 }
208 }
209
Thieu Le8f1c8352012-04-16 11:02:12 -0700210 // For cdc_ether devices, make sure it's a modem because this driver
211 // can be used for other ethernet devices.
212 if (driver_name == kDriverCdcEther &&
213 IsCdcEtherModemDevice(iface_name)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700214 SLOG(Device, 2) << StringPrintf("%s: device %s is a modem cdc_ether device",
215 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700216 return Technology::kCellular;
217 }
218
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700219 // Special case for the virtio driver, used when run under KVM. See also
220 // the comment in VirtioEthernet::Start.
221 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700222 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
223 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700224 return Technology::kVirtioEthernet;
225 }
226
Ben Chanfad4a0b2012-04-18 15:49:59 -0700227 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
228 "is defaulted to type ethernet",
229 __func__, iface_name.c_str(),
230 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700231 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700232}
233
Thieu Le8f1c8352012-04-16 11:02:12 -0700234// static
235bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
236 // A cdc_ether device is a modem device if it also exposes tty interfaces.
237 // To determine this, we look for the existence of the tty interface in the
238 // USB device sysfs tree.
239 //
240 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
241 // follows:
242 //
243 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
244 // 1-2:1.0
245 // tty
246 // ttyACM0
247 // 1-2:1.1
248 // net
249 // usb0
250 // 1-2:1.2
251 // tty
252 // ttyACM1
253 // ...
254 //
255 // /sys/class/net/usb0/device symlinks to
256 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700257 //
258 // Note that some modem devices have the tty directory one level deeper
259 // (eg. E362), so the device tree for the tty interface is:
260 // /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 -0700261
262 FilePath device_file(StringPrintf(kInterfaceDevice, iface_name.c_str()));
263 FilePath device_path;
264 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700265 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
266 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700267 return false;
268 }
269 if (!device_path.IsAbsolute()) {
270 device_path = device_file.DirName().Append(device_path);
271 file_util::AbsolutePath(&device_path);
272 }
273
274 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700275 // USB device and see if there's a subdirectory "tty" inside. In other
276 // words, using the example dir hierarchy above, find
277 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700278 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700279 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700280}
281
282// static
Thieu Leb27beee2012-04-20 09:19:06 -0700283bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
284 file_util::FileEnumerator::FileType type =
285 static_cast<file_util::FileEnumerator::FileType>(
286 file_util::FileEnumerator::DIRECTORIES |
287 file_util::FileEnumerator::SHOW_SYM_LINKS);
288 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700289 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
290 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700291 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700292 return true;
293 }
294 return false;
295}
296
Chris Masone2aa97072011-08-09 17:35:08 -0700297void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700298 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
299 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700300 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700301 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700302
Darin Petkove6193c02011-08-11 12:42:40 -0700303 unsigned int flags = msg.link_status().flags;
304 unsigned int change = msg.link_status().change;
Paul Stewart536820d2012-03-19 16:05:59 -0700305 bool new_device = !ContainsKey(infos_, dev_index);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700306 SLOG(Device, 2) << __func__ << "(index=" << dev_index
307 << std::showbase << std::hex
308 << ", flags=" << flags << ", change=" << change << ")"
309 << std::dec << std::noshowbase
310 << ", new_device=" << new_device;
Darin Petkove6193c02011-08-11 12:42:40 -0700311 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700312
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700313 DeviceRefPtr device = GetDevice(dev_index);
314 if (!device.get()) {
Chris Masone2aa97072011-08-09 17:35:08 -0700315 if (!msg.HasAttribute(IFLA_IFNAME)) {
316 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
317 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700318 }
Chris Masone2aa97072011-08-09 17:35:08 -0700319 ByteString b(msg.GetAttribute(IFLA_IFNAME));
320 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700321 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700322
Chris Masone2aa97072011-08-09 17:35:08 -0700323 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700324 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700325 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700326 } else {
327 technology = GetDeviceTechnology(link_name);
328 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700329 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800330 string address;
331 if (msg.HasAttribute(IFLA_ADDRESS)) {
332 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
333 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700334 SLOG(Device, 2) << "link index " << dev_index << " address "
335 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800336 } else if (technology != Technology::kTunnel) {
337 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
338 return;
339 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700340 switch (technology) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700341 case Technology::kCellular:
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700342 // Cellular devices are managed by ModemInfo.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700343 SLOG(Device, 2) << "Cellular link " << link_name
344 << " at index " << dev_index
345 << " -- notifying ModemInfo.";
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100346 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700347 return;
Paul Stewartfdd16072011-09-16 12:41:35 -0700348 case Technology::kEthernet:
Thieu Le3426c8f2012-01-11 17:35:11 -0800349 device = new Ethernet(control_interface_, dispatcher_, metrics_,
350 manager_, link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800351 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700352 break;
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700353 case Technology::kVirtioEthernet:
354 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
355 manager_, link_name, address, dev_index);
356 device->EnableIPv6Privacy();
357 break;
Paul Stewartfdd16072011-09-16 12:41:35 -0700358 case Technology::kWifi:
Thieu Le3426c8f2012-01-11 17:35:11 -0800359 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700360 link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800361 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700362 break;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800363 case Technology::kTunnel:
Paul Stewart536820d2012-03-19 16:05:59 -0700364 // Tunnel devices are managed by the VPN code. Notify the VPN Provider
365 // only if this is the first time we have seen this device index.
366 if (new_device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700367 SLOG(Device, 2) << "Tunnel link " << link_name
368 << " at index " << dev_index
369 << " -- notifying VPNProvider.";
Paul Stewart536820d2012-03-19 16:05:59 -0700370 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
371 dev_index)) {
372 // If VPN does not know anything about this tunnel, it is probably
373 // left over from a previous instance and should not exist.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700374 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
Paul Stewart536820d2012-03-19 16:05:59 -0700375 DeleteInterface(dev_index);
376 }
Paul Stewartca6abd42012-03-01 15:45:29 -0800377 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800378 return;
Paul Stewarte81eb702012-04-11 15:04:53 -0700379 case Technology::kLoopback:
380 // Loopback devices are largely ignored, but we should make sure the
381 // link is enabled.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700382 SLOG(Device, 2) << "Bringing up loopback device " << link_name
383 << " at index " << dev_index;
Paul Stewarte81eb702012-04-11 15:04:53 -0700384 rtnl_handler_->SetInterfaceFlags(dev_index, IFF_UP, IFF_UP);
385 return;
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700386 default:
Thieu Le3426c8f2012-01-11 17:35:11 -0800387 device = new DeviceStub(control_interface_, dispatcher_, metrics_,
388 manager_, link_name, address, dev_index,
389 technology);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700390 break;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700391 }
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700392 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700393 }
Darin Petkove6193c02011-08-11 12:42:40 -0700394 device->LinkEvent(flags, change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700395}
396
Chris Masone2aa97072011-08-09 17:35:08 -0700397void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700398 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700399
Paul Stewart9a908082011-08-31 12:18:48 -0700400 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
401 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700402 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
403 << std::showbase << std::hex
404 << ", flags=" << msg.link_status().flags
405 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700406 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700407}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700408
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700409DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
410 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700411 return info ? info->device : NULL;
412}
413
Paul Stewart32852962011-08-30 14:06:53 -0700414bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700415 const Info *info = GetInfo(interface_index);
416 if (!info) {
417 return false;
418 }
Paul Stewart32852962011-08-30 14:06:53 -0700419 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700420 return true;
421}
422
Paul Stewart9a908082011-08-31 12:18:48 -0700423bool DeviceInfo::GetAddresses(int interface_index,
424 vector<AddressData> *addresses) const {
425 const Info *info = GetInfo(interface_index);
426 if (!info) {
427 return false;
428 }
429 *addresses = info->ip_addresses;
430 return true;
431}
432
433void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700434 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700435 const Info *info = GetInfo(interface_index);
436 if (!info) {
437 return;
438 }
439 const vector<AddressData> &addresses = info->ip_addresses;
440 vector<AddressData>::const_iterator iter;
441 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700442 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700443 (iter->scope == RT_SCOPE_UNIVERSE &&
444 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700445 SLOG(Device, 2) << __func__ << ": removing ip address from interface "
446 << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700447 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
448 }
449 }
450}
451
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700452bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
453 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700454 if (!info) {
455 return false;
456 }
457 *flags = info->flags;
458 return true;
459}
460
Paul Stewartca6abd42012-03-01 15:45:29 -0800461bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800462 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
463 if (fd < 0) {
464 PLOG(ERROR) << "failed to open " << kTunDeviceName;
465 return false;
466 }
467 file_util::ScopedFD scoped_fd(&fd);
468
469 struct ifreq ifr;
470 memset(&ifr, 0, sizeof(ifr));
471 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
472 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
473 PLOG(ERROR) << "failed to create tunnel interface";
474 return false;
475 }
476
477 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
478 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
479 return false;
480 }
481
482 *interface_name = string(ifr.ifr_name);
483
484 return true;
485}
486
Paul Stewartca6abd42012-03-01 15:45:29 -0800487bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800488 return rtnl_handler_->RemoveInterface(interface_index);
489}
490
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700491const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
492 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700493 if (iter == infos_.end()) {
494 return NULL;
495 }
496 return &iter->second;
497}
498
499void DeviceInfo::RemoveInfo(int interface_index) {
500 map<int, Info>::iterator iter = infos_.find(interface_index);
501 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700502 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700503 if (iter->second.device.get()) {
504 manager_->DeregisterDevice(iter->second.device);
505 }
506 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700507 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700508 SLOG(Device, 2) << __func__ << ": Unknown device index: "
509 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700510 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700511}
512
Chris Masone2aa97072011-08-09 17:35:08 -0700513void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700514 DCHECK(msg.type() == RTNLMessage::kTypeLink);
515 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700516 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700517 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700518 DelLinkMsgHandler(msg);
519 } else {
520 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700521 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700522}
523
Paul Stewart9a908082011-08-31 12:18:48 -0700524void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700525 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700526 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
527 int interface_index = msg.interface_index();
528 if (!ContainsKey(infos_, interface_index)) {
529 LOG(ERROR) << "Got address type message for unknown index "
530 << interface_index;
531 return;
532 }
533 const RTNLMessage::AddressStatus &status = msg.address_status();
534 IPAddress address(msg.family(),
535 msg.GetAttribute(IFA_ADDRESS),
536 status.prefix_len);
537
538 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
539 vector<AddressData>::iterator iter;
540 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
541 if (address.Equals(iter->address)) {
542 break;
543 }
544 }
545 if (iter != address_list.end()) {
546 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700547 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700548 address_list.erase(iter);
549 } else {
550 iter->flags = status.flags;
551 iter->scope = status.scope;
552 }
553 } else if (msg.mode() == RTNLMessage::kModeAdd) {
554 address_list.push_back(AddressData(address, status.flags, status.scope));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700555 SLOG(Device, 2) << "Add address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700556 }
557}
558
Paul Stewart0af98bf2011-05-10 17:38:08 -0700559} // namespace shill