blob: 05afa5428d69639ec6d462f7e568ab70cb0be0c8 [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
257
258 FilePath device_file(StringPrintf(kInterfaceDevice, iface_name.c_str()));
259 FilePath device_path;
260 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700261 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
262 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700263 return false;
264 }
265 if (!device_path.IsAbsolute()) {
266 device_path = device_file.DirName().Append(device_path);
267 file_util::AbsolutePath(&device_path);
268 }
269
270 // Look for tty interface by enumerating all directories under the parent
271 // USB device and seeing if there's a subdirectory "tty" inside of the
272 // enumerated directory. In other words, using the example dir hierarchy
273 // above, find /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2*/tty.
274 // If this exists, then this is a modem device.
275 FilePath usb_device_path(device_path.DirName());
276 string search_pattern(usb_device_path.BaseName().MaybeAsASCII() + "*");
277 return IsGrandchildSubdir(usb_device_path, search_pattern, "tty");
278}
279
280// static
281bool DeviceInfo::IsGrandchildSubdir(const FilePath &base_dir,
282 const string &children_filter,
283 const string &grandchild) {
284 file_util::FileEnumerator dir_enum(base_dir, false,
285 file_util::FileEnumerator::DIRECTORIES,
286 children_filter);
287 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
288 curr_dir = dir_enum.Next()) {
289 if (file_util::DirectoryExists(curr_dir.Append(grandchild)))
290 return true;
291 }
292 return false;
293}
294
Chris Masone2aa97072011-08-09 17:35:08 -0700295void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700296 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
297 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700298 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700299 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700300
Darin Petkove6193c02011-08-11 12:42:40 -0700301 unsigned int flags = msg.link_status().flags;
302 unsigned int change = msg.link_status().change;
Paul Stewart536820d2012-03-19 16:05:59 -0700303 bool new_device = !ContainsKey(infos_, dev_index);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700304 SLOG(Device, 2) << __func__ << "(index=" << dev_index
305 << std::showbase << std::hex
306 << ", flags=" << flags << ", change=" << change << ")"
307 << std::dec << std::noshowbase
308 << ", new_device=" << new_device;
Darin Petkove6193c02011-08-11 12:42:40 -0700309 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700310
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700311 DeviceRefPtr device = GetDevice(dev_index);
312 if (!device.get()) {
Chris Masone2aa97072011-08-09 17:35:08 -0700313 if (!msg.HasAttribute(IFLA_IFNAME)) {
314 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
315 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700316 }
Chris Masone2aa97072011-08-09 17:35:08 -0700317 ByteString b(msg.GetAttribute(IFLA_IFNAME));
318 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700319 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700320
Chris Masone2aa97072011-08-09 17:35:08 -0700321 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700322 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700323 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700324 } else {
325 technology = GetDeviceTechnology(link_name);
326 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700327 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800328 string address;
329 if (msg.HasAttribute(IFLA_ADDRESS)) {
330 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
331 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700332 SLOG(Device, 2) << "link index " << dev_index << " address "
333 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800334 } else if (technology != Technology::kTunnel) {
335 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
336 return;
337 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700338 switch (technology) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700339 case Technology::kCellular:
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700340 // Cellular devices are managed by ModemInfo.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700341 SLOG(Device, 2) << "Cellular link " << link_name
342 << " at index " << dev_index
343 << " -- notifying ModemInfo.";
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100344 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700345 return;
Paul Stewartfdd16072011-09-16 12:41:35 -0700346 case Technology::kEthernet:
Thieu Le3426c8f2012-01-11 17:35:11 -0800347 device = new Ethernet(control_interface_, dispatcher_, metrics_,
348 manager_, link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800349 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700350 break;
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700351 case Technology::kVirtioEthernet:
352 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
353 manager_, link_name, address, dev_index);
354 device->EnableIPv6Privacy();
355 break;
Paul Stewartfdd16072011-09-16 12:41:35 -0700356 case Technology::kWifi:
Thieu Le3426c8f2012-01-11 17:35:11 -0800357 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700358 link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800359 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700360 break;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800361 case Technology::kTunnel:
Paul Stewart536820d2012-03-19 16:05:59 -0700362 // Tunnel devices are managed by the VPN code. Notify the VPN Provider
363 // only if this is the first time we have seen this device index.
364 if (new_device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700365 SLOG(Device, 2) << "Tunnel link " << link_name
366 << " at index " << dev_index
367 << " -- notifying VPNProvider.";
Paul Stewart536820d2012-03-19 16:05:59 -0700368 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
369 dev_index)) {
370 // If VPN does not know anything about this tunnel, it is probably
371 // left over from a previous instance and should not exist.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700372 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
Paul Stewart536820d2012-03-19 16:05:59 -0700373 DeleteInterface(dev_index);
374 }
Paul Stewartca6abd42012-03-01 15:45:29 -0800375 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800376 return;
Paul Stewarte81eb702012-04-11 15:04:53 -0700377 case Technology::kLoopback:
378 // Loopback devices are largely ignored, but we should make sure the
379 // link is enabled.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700380 SLOG(Device, 2) << "Bringing up loopback device " << link_name
381 << " at index " << dev_index;
Paul Stewarte81eb702012-04-11 15:04:53 -0700382 rtnl_handler_->SetInterfaceFlags(dev_index, IFF_UP, IFF_UP);
383 return;
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700384 default:
Thieu Le3426c8f2012-01-11 17:35:11 -0800385 device = new DeviceStub(control_interface_, dispatcher_, metrics_,
386 manager_, link_name, address, dev_index,
387 technology);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700388 break;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700389 }
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700390 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700391 }
Darin Petkove6193c02011-08-11 12:42:40 -0700392 device->LinkEvent(flags, change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700393}
394
Chris Masone2aa97072011-08-09 17:35:08 -0700395void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700396 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700397
Paul Stewart9a908082011-08-31 12:18:48 -0700398 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
399 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700400 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
401 << std::showbase << std::hex
402 << ", flags=" << msg.link_status().flags
403 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700404 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700405}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700406
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700407DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
408 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700409 return info ? info->device : NULL;
410}
411
Paul Stewart32852962011-08-30 14:06:53 -0700412bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700413 const Info *info = GetInfo(interface_index);
414 if (!info) {
415 return false;
416 }
Paul Stewart32852962011-08-30 14:06:53 -0700417 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700418 return true;
419}
420
Paul Stewart9a908082011-08-31 12:18:48 -0700421bool DeviceInfo::GetAddresses(int interface_index,
422 vector<AddressData> *addresses) const {
423 const Info *info = GetInfo(interface_index);
424 if (!info) {
425 return false;
426 }
427 *addresses = info->ip_addresses;
428 return true;
429}
430
431void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700432 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700433 const Info *info = GetInfo(interface_index);
434 if (!info) {
435 return;
436 }
437 const vector<AddressData> &addresses = info->ip_addresses;
438 vector<AddressData>::const_iterator iter;
439 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700440 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700441 (iter->scope == RT_SCOPE_UNIVERSE &&
442 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700443 SLOG(Device, 2) << __func__ << ": removing ip address from interface "
444 << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700445 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
446 }
447 }
448}
449
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700450bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
451 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700452 if (!info) {
453 return false;
454 }
455 *flags = info->flags;
456 return true;
457}
458
Paul Stewartca6abd42012-03-01 15:45:29 -0800459bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800460 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
461 if (fd < 0) {
462 PLOG(ERROR) << "failed to open " << kTunDeviceName;
463 return false;
464 }
465 file_util::ScopedFD scoped_fd(&fd);
466
467 struct ifreq ifr;
468 memset(&ifr, 0, sizeof(ifr));
469 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
470 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
471 PLOG(ERROR) << "failed to create tunnel interface";
472 return false;
473 }
474
475 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
476 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
477 return false;
478 }
479
480 *interface_name = string(ifr.ifr_name);
481
482 return true;
483}
484
Paul Stewartca6abd42012-03-01 15:45:29 -0800485bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800486 return rtnl_handler_->RemoveInterface(interface_index);
487}
488
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700489const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
490 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700491 if (iter == infos_.end()) {
492 return NULL;
493 }
494 return &iter->second;
495}
496
497void DeviceInfo::RemoveInfo(int interface_index) {
498 map<int, Info>::iterator iter = infos_.find(interface_index);
499 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700500 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700501 if (iter->second.device.get()) {
502 manager_->DeregisterDevice(iter->second.device);
503 }
504 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700505 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700506 SLOG(Device, 2) << __func__ << ": Unknown device index: "
507 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700508 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700509}
510
Chris Masone2aa97072011-08-09 17:35:08 -0700511void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700512 DCHECK(msg.type() == RTNLMessage::kTypeLink);
513 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700514 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700515 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700516 DelLinkMsgHandler(msg);
517 } else {
518 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700519 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700520}
521
Paul Stewart9a908082011-08-31 12:18:48 -0700522void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700523 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700524 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
525 int interface_index = msg.interface_index();
526 if (!ContainsKey(infos_, interface_index)) {
527 LOG(ERROR) << "Got address type message for unknown index "
528 << interface_index;
529 return;
530 }
531 const RTNLMessage::AddressStatus &status = msg.address_status();
532 IPAddress address(msg.family(),
533 msg.GetAttribute(IFA_ADDRESS),
534 status.prefix_len);
535
536 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
537 vector<AddressData>::iterator iter;
538 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
539 if (address.Equals(iter->address)) {
540 break;
541 }
542 }
543 if (iter != address_list.end()) {
544 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700545 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700546 address_list.erase(iter);
547 } else {
548 iter->flags = status.flags;
549 iter->scope = status.scope;
550 }
551 } else if (msg.mode() == RTNLMessage::kModeAdd) {
552 address_list.push_back(AddressData(address, status.flags, status.scope));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700553 SLOG(Device, 2) << "Add address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700554 }
555}
556
Paul Stewart0af98bf2011-05-10 17:38:08 -0700557} // namespace shill