blob: c724f52c80e2c9f76607721123556ad0da186144 [file] [log] [blame]
Darin Petkov41c0e0a2012-01-09 16:38:53 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart0af98bf2011-05-10 17:38:08 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Paul Stewartcba0f7f2012-02-29 16:33:05 -08005#include "shill/device_info.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -07006
Paul Stewart0af98bf2011-05-10 17:38:08 -07007#include <arpa/inet.h>
Paul Stewartcba0f7f2012-02-29 16:33:05 -08008#include <fcntl.h>
9#include <linux/if_tun.h>
Paul Stewart0af98bf2011-05-10 17:38:08 -070010#include <linux/netlink.h>
11#include <linux/rtnetlink.h>
Paul Stewartcba0f7f2012-02-29 16:33:05 -080012#include <net/if.h>
13#include <net/if_arp.h>
14#include <netinet/ether.h>
15#include <string.h>
16#include <sys/ioctl.h>
17#include <sys/socket.h>
18#include <time.h>
19#include <unistd.h>
20
Paul Stewart0af98bf2011-05-10 17:38:08 -070021#include <string>
22
Eric Shienbrood3e20a232012-02-16 11:35:56 -050023#include <base/bind.h>
Paul Stewartbf1861b2011-08-23 15:45:35 -070024#include <base/file_util.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070025#include <base/logging.h>
26#include <base/memory/scoped_ptr.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050027#include <base/stl_util.h>
Paul Stewart2001a422011-12-15 10:20:09 -080028#include <base/string_number_conversions.h>
Chris Masone877ff982011-09-21 16:18:24 -070029#include <base/string_util.h>
Paul Stewartb50f0b92011-05-16 16:31:42 -070030#include <base/stringprintf.h>
Paul Stewart0af98bf2011-05-10 17:38:08 -070031
32#include "shill/control_interface.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070033#include "shill/device.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070034#include "shill/device_stub.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070035#include "shill/ethernet.h"
36#include "shill/manager.h"
Paul Stewart8c116a92012-05-02 18:30:03 -070037#include "shill/routing_table.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070038#include "shill/rtnl_handler.h"
39#include "shill/rtnl_listener.h"
Chris Masone2aa97072011-08-09 17:35:08 -070040#include "shill/rtnl_message.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070041#include "shill/scope_logger.h"
Chris Masone487b8bf2011-05-13 16:27:57 -070042#include "shill/service.h"
mukesh agrawal93a29ed2012-04-17 16:13:01 -070043#include "shill/virtio_ethernet.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070044#include "shill/wifi.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070045
Eric Shienbrood3e20a232012-02-16 11:35:56 -050046using base::Bind;
Thieu Le8f1c8352012-04-16 11:02:12 -070047using base::StringPrintf;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050048using base::Unretained;
Darin Petkove6193c02011-08-11 12:42:40 -070049using std::map;
Paul Stewart0af98bf2011-05-10 17:38:08 -070050using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070051using std::vector;
Paul Stewart0af98bf2011-05-10 17:38:08 -070052
53namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070054
55// static
Jason Glasgowabc54032012-04-20 16:08:32 -040056const char DeviceInfo::kModemPseudoDeviceNamePrefix[] = "pseudomodem";
Paul Stewartca876ee2012-04-21 08:55:58 -070057const char DeviceInfo::kDeviceInfoRoot[] = "/sys/class/net";
mukesh agrawal93a29ed2012-04-17 16:13:01 -070058const char DeviceInfo::kDriverVirtioNet[] = "virtio_net";
Paul Stewartca876ee2012-04-21 08:55:58 -070059const char DeviceInfo::kInterfaceUevent[] = "uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080060const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Paul Stewartca876ee2012-04-21 08:55:58 -070061const char DeviceInfo::kInterfaceDevice[] = "device";
62const char DeviceInfo::kInterfaceDriver[] = "device/driver";
63const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags";
64const char DeviceInfo::kInterfaceType[] = "type";
Thieu Le8f1c8352012-04-16 11:02:12 -070065const char DeviceInfo::kDriverCdcEther[] = "cdc_ether";
Paul Stewartb50f0b92011-05-16 16:31:42 -070066const char *DeviceInfo::kModemDrivers[] = {
67 "gobi",
68 "QCUSBNet2k",
Thieu Le8f1c8352012-04-16 11:02:12 -070069 "GobiNet"
Paul Stewartb50f0b92011-05-16 16:31:42 -070070};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080071const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewartb50f0b92011-05-16 16:31:42 -070072
73DeviceInfo::DeviceInfo(ControlInterface *control_interface,
74 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080075 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070076 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070077 : control_interface_(control_interface),
78 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080079 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070080 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050081 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
82 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070083 link_listener_(NULL),
84 address_listener_(NULL),
Paul Stewartca876ee2012-04-21 08:55:58 -070085 device_info_root_(kDeviceInfoRoot),
Paul Stewart8c116a92012-05-02 18:30:03 -070086 routing_table_(RoutingTable::GetInstance()),
Paul Stewart9a908082011-08-31 12:18:48 -070087 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070088}
89
Paul Stewarta3c56f92011-05-26 07:08:52 -070090DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070091
mukesh agrawal8f317b62011-07-15 11:53:23 -070092void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
93 black_list_.insert(device_name);
94}
95
Eric Shienbrood5e628a52012-03-21 16:56:59 -040096bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
97 return ContainsKey(black_list_, device_name);
98}
99
Paul Stewarta3c56f92011-05-26 07:08:52 -0700100void DeviceInfo::Start() {
101 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500102 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700103 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500104 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700105 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
106 RTNLHandler::kRequestAddr);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700107}
108
Paul Stewarta3c56f92011-05-26 07:08:52 -0700109void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700110 link_listener_.reset();
111 address_listener_.reset();
Paul Stewart8c116a92012-05-02 18:30:03 -0700112 infos_.clear();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700113}
114
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700115void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700116 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
117 << device->interface_index() << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700118 CHECK(!GetDevice(device->interface_index()).get());
119 infos_[device->interface_index()].device = device;
Paul Stewartfdd16072011-09-16 12:41:35 -0700120 if (device->TechnologyIs(Technology::kCellular) ||
121 device->TechnologyIs(Technology::kEthernet) ||
122 device->TechnologyIs(Technology::kWifi)) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700123 manager_->RegisterDevice(device);
124 }
125}
126
Jason Glasgowe9089492012-02-23 17:57:37 -0500127void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
128 int interface_index = device->interface_index();
129
Ben Chanfad4a0b2012-04-18 15:49:59 -0700130 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
131 << interface_index << ")";
Jason Glasgowe9089492012-02-23 17:57:37 -0500132 CHECK(device->TechnologyIs(Technology::kCellular));
133
134 // Release reference to the device
135 map<int, Info>::iterator iter = infos_.find(interface_index);
136 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700137 SLOG(Device, 2) << "Removing device from info for index: "
138 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500139 manager_->DeregisterDevice(device);
140 // Release the reference to the device, but maintain the mapping
141 // for the index. That will be cleaned up by an RTNL message.
142 iter->second.device = NULL;
143 }
144}
145
Paul Stewartca876ee2012-04-21 08:55:58 -0700146FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name,
147 const string &path_name) {
148 return device_info_root_.Append(iface_name).Append(path_name);
149}
150
151bool DeviceInfo::GetDeviceInfoContents(const string &iface_name,
152 const string &path_name,
153 string *contents_out) {
154 return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
155 contents_out);
156}
157bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name,
158 const string &path_name,
159 FilePath *path_out) {
160 return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
161 path_out);
162}
163
Paul Stewartfdd16072011-09-16 12:41:35 -0700164Technology::Identifier DeviceInfo::GetDeviceTechnology(
165 const string &iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700166 string type_string;
167 int arp_type = ARPHRD_VOID;;
168 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
169 TrimString(type_string, "\n", &type_string) &&
170 !base::StringToInt(type_string, &arp_type)) {
171 arp_type = ARPHRD_VOID;
172 }
173
Paul Stewart9364c4c2011-12-06 17:12:42 -0800174 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700175 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700176 SLOG(Device, 2) << StringPrintf("%s: device %s has no uevent file",
177 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700178 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800179 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700180
Thieu Le8f1c8352012-04-16 11:02:12 -0700181 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
182 // start of the file or after a newline, we can safely assume this
183 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800184 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700185 SLOG(Device, 2)
186 << StringPrintf("%s: device %s has wifi signature in uevent file",
187 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700188 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700189 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
190 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800191 return Technology::kWiFiMonitor;
192 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700193 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700194 }
195
Paul Stewart9364c4c2011-12-06 17:12:42 -0800196 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700197 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700198 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
199 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700200 if (arp_type == ARPHRD_LOOPBACK) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700201 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
202 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700203 return Technology::kLoopback;
204 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700205 if (arp_type == ARPHRD_PPP) {
206 SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device",
207 __func__, iface_name.c_str());
208 return Technology::kPPP;
209 }
210 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800211 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700212 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
213 TrimString(tun_flags_str, "\n", &tun_flags_str) &&
214 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800215 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700216 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
217 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800218 return Technology::kTunnel;
219 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700220 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800221 }
222
223 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700224 // See if driver for this interface is in a list of known modem driver names.
225 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
226 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800227 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700228 SLOG(Device, 2)
229 << StringPrintf("%s: device %s is matched with modem driver %s",
230 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800231 return Technology::kCellular;
232 }
233 }
234
Thieu Le8f1c8352012-04-16 11:02:12 -0700235 // For cdc_ether devices, make sure it's a modem because this driver
236 // can be used for other ethernet devices.
237 if (driver_name == kDriverCdcEther &&
238 IsCdcEtherModemDevice(iface_name)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700239 SLOG(Device, 2) << StringPrintf("%s: device %s is a modem cdc_ether device",
240 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700241 return Technology::kCellular;
242 }
243
Jason Glasgowabc54032012-04-20 16:08:32 -0400244 // Special case for pseudo modems which are used for testing
245 if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) {
246 SLOG(Device, 2) << StringPrintf(
247 "%s: device %s is a pseudo modem for testing",
248 __func__, iface_name.c_str());
249 return Technology::kCellular;
250 }
251
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700252 // Special case for the virtio driver, used when run under KVM. See also
253 // the comment in VirtioEthernet::Start.
254 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700255 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
256 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700257 return Technology::kVirtioEthernet;
258 }
259
Ben Chanfad4a0b2012-04-18 15:49:59 -0700260 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
261 "is defaulted to type ethernet",
262 __func__, iface_name.c_str(),
263 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700264 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700265}
266
Thieu Le8f1c8352012-04-16 11:02:12 -0700267bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
268 // A cdc_ether device is a modem device if it also exposes tty interfaces.
269 // To determine this, we look for the existence of the tty interface in the
270 // USB device sysfs tree.
271 //
272 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
273 // follows:
274 //
275 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
276 // 1-2:1.0
277 // tty
278 // ttyACM0
279 // 1-2:1.1
280 // net
281 // usb0
282 // 1-2:1.2
283 // tty
284 // ttyACM1
285 // ...
286 //
287 // /sys/class/net/usb0/device symlinks to
288 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700289 //
290 // Note that some modem devices have the tty directory one level deeper
291 // (eg. E362), so the device tree for the tty interface is:
292 // /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 -0700293
Paul Stewartca876ee2012-04-21 08:55:58 -0700294 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700295 FilePath device_path;
296 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700297 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
298 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700299 return false;
300 }
301 if (!device_path.IsAbsolute()) {
302 device_path = device_file.DirName().Append(device_path);
303 file_util::AbsolutePath(&device_path);
304 }
305
306 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700307 // USB device and see if there's a subdirectory "tty" inside. In other
308 // words, using the example dir hierarchy above, find
309 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700310 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700311 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700312}
313
314// static
Thieu Leb27beee2012-04-20 09:19:06 -0700315bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
316 file_util::FileEnumerator::FileType type =
317 static_cast<file_util::FileEnumerator::FileType>(
318 file_util::FileEnumerator::DIRECTORIES |
319 file_util::FileEnumerator::SHOW_SYM_LINKS);
320 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700321 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
322 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700323 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700324 return true;
325 }
326 return false;
327}
328
Paul Stewart8c116a92012-05-02 18:30:03 -0700329DeviceRefPtr DeviceInfo::CreateDevice(const string &link_name,
330 const string &address,
331 int interface_index,
332 Technology::Identifier technology) {
333 DeviceRefPtr device;
334
335 switch (technology) {
336 case Technology::kCellular:
337 // Cellular devices are managed by ModemInfo.
338 SLOG(Device, 2) << "Cellular link " << link_name
339 << " at index " << interface_index
340 << " -- notifying ModemInfo.";
341 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
342 break;
343 case Technology::kEthernet:
344 device = new Ethernet(control_interface_, dispatcher_, metrics_,
345 manager_, link_name, address, interface_index);
346 device->EnableIPv6Privacy();
347 break;
348 case Technology::kVirtioEthernet:
349 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
350 manager_, link_name, address,
351 interface_index);
352 device->EnableIPv6Privacy();
353 break;
354 case Technology::kWifi:
355 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
356 link_name, address, interface_index);
357 device->EnableIPv6Privacy();
358 break;
359 case Technology::kPPP:
360 case Technology::kTunnel:
361 // Tunnel and PPP devices are managed by the VPN code (PPP for
362 // l2tpipsec). Notify the VPN Provider of the interface's presence.
363 // Since CreateDevice is only called once in the lifetime of an
364 // interface index, this notification will only occur the first
365 // time the device is seen.
366 SLOG(Device, 2) << "Tunnel / PPP link " << link_name
367 << " at index " << interface_index
368 << " -- notifying VPNProvider.";
369 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
370 interface_index) &&
371 technology == Technology::kTunnel) {
372 // If VPN does not know anything about this tunnel, it is probably
373 // left over from a previous instance and should not exist.
374 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
375 DeleteInterface(interface_index);
376 }
377 break;
378 case Technology::kLoopback:
379 // Loopback devices are largely ignored, but we should make sure the
380 // link is enabled.
381 SLOG(Device, 2) << "Bringing up loopback device " << link_name
382 << " at index " << interface_index;
383 rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP);
384 return NULL;
385 default:
386 // We will not manage this device in shill. Do not create a device
387 // object or do anything to change its state. We create a stub object
388 // which is useful for testing.
389 return new DeviceStub(control_interface_, dispatcher_, metrics_,
390 manager_, link_name, address, interface_index,
391 technology);;
392 }
393
394 // Reset the routing table and addresses.
395 routing_table_->FlushRoutes(interface_index);
396 FlushAddresses(interface_index);
397
398 return device;
399}
400
Chris Masone2aa97072011-08-09 17:35:08 -0700401void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700402 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
403 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700404 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700405 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700406
Darin Petkove6193c02011-08-11 12:42:40 -0700407 unsigned int flags = msg.link_status().flags;
408 unsigned int change = msg.link_status().change;
Paul Stewart8c116a92012-05-02 18:30:03 -0700409 bool new_device =
410 !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700411 SLOG(Device, 2) << __func__ << "(index=" << dev_index
412 << std::showbase << std::hex
413 << ", flags=" << flags << ", change=" << change << ")"
414 << std::dec << std::noshowbase
415 << ", new_device=" << new_device;
Paul Stewart8c116a92012-05-02 18:30:03 -0700416 infos_[dev_index].has_addresses_only = false;
Darin Petkove6193c02011-08-11 12:42:40 -0700417 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700418
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700419 DeviceRefPtr device = GetDevice(dev_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700420 if (new_device) {
421 CHECK(!device);
Chris Masone2aa97072011-08-09 17:35:08 -0700422 if (!msg.HasAttribute(IFLA_IFNAME)) {
423 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
424 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700425 }
Chris Masone2aa97072011-08-09 17:35:08 -0700426 ByteString b(msg.GetAttribute(IFLA_IFNAME));
427 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700428 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200429 infos_[dev_index].name = link_name;
430 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700431
Chris Masone2aa97072011-08-09 17:35:08 -0700432 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700433 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700434 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700435 } else {
436 technology = GetDeviceTechnology(link_name);
437 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700438 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800439 string address;
440 if (msg.HasAttribute(IFLA_ADDRESS)) {
441 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
442 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700443 SLOG(Device, 2) << "link index " << dev_index << " address "
444 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700445 } else if (technology != Technology::kTunnel &&
446 technology != Technology::kPPP) {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800447 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
448 return;
449 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700450 device = CreateDevice(link_name, address, dev_index, technology);
451 if (device) {
452 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700453 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700454 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700455 if (device) {
456 device->LinkEvent(flags, change);
457 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700458}
459
Chris Masone2aa97072011-08-09 17:35:08 -0700460void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700461 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700462
Paul Stewart9a908082011-08-31 12:18:48 -0700463 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
464 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700465 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
466 << std::showbase << std::hex
467 << ", flags=" << msg.link_status().flags
468 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700469 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700470}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700471
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700472DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
473 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700474 return info ? info->device : NULL;
475}
476
Darin Petkovf8046b82012-04-24 16:29:23 +0200477int DeviceInfo::GetIndex(const string &interface_name) const {
478 map<string, int>::const_iterator it = indices_.find(interface_name);
479 return it == indices_.end() ? -1 : it->second;
480}
481
Paul Stewart32852962011-08-30 14:06:53 -0700482bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700483 const Info *info = GetInfo(interface_index);
484 if (!info) {
485 return false;
486 }
Paul Stewart32852962011-08-30 14:06:53 -0700487 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700488 return true;
489}
490
Paul Stewart9a908082011-08-31 12:18:48 -0700491bool DeviceInfo::GetAddresses(int interface_index,
492 vector<AddressData> *addresses) const {
493 const Info *info = GetInfo(interface_index);
494 if (!info) {
495 return false;
496 }
497 *addresses = info->ip_addresses;
498 return true;
499}
500
501void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700502 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700503 const Info *info = GetInfo(interface_index);
504 if (!info) {
505 return;
506 }
507 const vector<AddressData> &addresses = info->ip_addresses;
508 vector<AddressData>::const_iterator iter;
509 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700510 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700511 (iter->scope == RT_SCOPE_UNIVERSE &&
512 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700513 SLOG(Device, 2) << __func__ << ": removing ip address "
514 << iter->address.ToString()
515 << " from interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700516 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
517 }
518 }
519}
520
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700521bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
522 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700523 if (!info) {
524 return false;
525 }
526 *flags = info->flags;
527 return true;
528}
529
Paul Stewartca6abd42012-03-01 15:45:29 -0800530bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800531 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
532 if (fd < 0) {
533 PLOG(ERROR) << "failed to open " << kTunDeviceName;
534 return false;
535 }
536 file_util::ScopedFD scoped_fd(&fd);
537
538 struct ifreq ifr;
539 memset(&ifr, 0, sizeof(ifr));
540 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
541 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
542 PLOG(ERROR) << "failed to create tunnel interface";
543 return false;
544 }
545
546 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
547 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
548 return false;
549 }
550
551 *interface_name = string(ifr.ifr_name);
552
553 return true;
554}
555
Paul Stewartca6abd42012-03-01 15:45:29 -0800556bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800557 return rtnl_handler_->RemoveInterface(interface_index);
558}
559
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700560const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
561 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700562 if (iter == infos_.end()) {
563 return NULL;
564 }
565 return &iter->second;
566}
567
568void DeviceInfo::RemoveInfo(int interface_index) {
569 map<int, Info>::iterator iter = infos_.find(interface_index);
570 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700571 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700572 if (iter->second.device.get()) {
573 manager_->DeregisterDevice(iter->second.device);
574 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200575 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700576 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700577 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700578 SLOG(Device, 2) << __func__ << ": Unknown device index: "
579 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700580 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700581}
582
Chris Masone2aa97072011-08-09 17:35:08 -0700583void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700584 DCHECK(msg.type() == RTNLMessage::kTypeLink);
585 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700586 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700587 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700588 DelLinkMsgHandler(msg);
589 } else {
590 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700591 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700592}
593
Paul Stewart9a908082011-08-31 12:18:48 -0700594void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700595 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700596 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
597 int interface_index = msg.interface_index();
598 if (!ContainsKey(infos_, interface_index)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700599 SLOG(Device, 2) << "Got advance address information for unknown index "
600 << interface_index;
601 infos_[interface_index].has_addresses_only = true;
Paul Stewart9a908082011-08-31 12:18:48 -0700602 }
603 const RTNLMessage::AddressStatus &status = msg.address_status();
604 IPAddress address(msg.family(),
605 msg.GetAttribute(IFA_ADDRESS),
606 status.prefix_len);
607
608 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
609 vector<AddressData>::iterator iter;
610 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
611 if (address.Equals(iter->address)) {
612 break;
613 }
614 }
615 if (iter != address_list.end()) {
616 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700617 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700618 address_list.erase(iter);
619 } else {
620 iter->flags = status.flags;
621 iter->scope = status.scope;
622 }
623 } else if (msg.mode() == RTNLMessage::kModeAdd) {
624 address_list.push_back(AddressData(address, status.flags, status.scope));
Paul Stewart8c116a92012-05-02 18:30:03 -0700625 SLOG(Device, 2) << "Add address " << address.ToString()
626 << " for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700627 }
628}
629
Paul Stewart0af98bf2011-05-10 17:38:08 -0700630} // namespace shill