blob: a294f79d042138bdb1339cf1fbf12e65c05cee89 [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
Jason Glasgowabc54032012-04-20 16:08:32 -040055const char DeviceInfo::kModemPseudoDeviceNamePrefix[] = "pseudomodem";
Paul Stewartca876ee2012-04-21 08:55:58 -070056const char DeviceInfo::kDeviceInfoRoot[] = "/sys/class/net";
mukesh agrawal93a29ed2012-04-17 16:13:01 -070057const char DeviceInfo::kDriverVirtioNet[] = "virtio_net";
Paul Stewartca876ee2012-04-21 08:55:58 -070058const char DeviceInfo::kInterfaceUevent[] = "uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080059const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Paul Stewartca876ee2012-04-21 08:55:58 -070060const char DeviceInfo::kInterfaceDevice[] = "device";
61const char DeviceInfo::kInterfaceDriver[] = "device/driver";
62const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags";
63const char DeviceInfo::kInterfaceType[] = "type";
Thieu Le8f1c8352012-04-16 11:02:12 -070064const char DeviceInfo::kDriverCdcEther[] = "cdc_ether";
Paul Stewartb50f0b92011-05-16 16:31:42 -070065const char *DeviceInfo::kModemDrivers[] = {
66 "gobi",
67 "QCUSBNet2k",
Thieu Le8f1c8352012-04-16 11:02:12 -070068 "GobiNet"
Paul Stewartb50f0b92011-05-16 16:31:42 -070069};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080070const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewartb50f0b92011-05-16 16:31:42 -070071
72DeviceInfo::DeviceInfo(ControlInterface *control_interface,
73 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080074 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070075 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070076 : control_interface_(control_interface),
77 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080078 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070079 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050080 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
81 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070082 link_listener_(NULL),
83 address_listener_(NULL),
Paul Stewartca876ee2012-04-21 08:55:58 -070084 device_info_root_(kDeviceInfoRoot),
Paul Stewart9a908082011-08-31 12:18:48 -070085 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070086}
87
Paul Stewarta3c56f92011-05-26 07:08:52 -070088DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070089
mukesh agrawal8f317b62011-07-15 11:53:23 -070090void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
91 black_list_.insert(device_name);
92}
93
Eric Shienbrood5e628a52012-03-21 16:56:59 -040094bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
95 return ContainsKey(black_list_, device_name);
96}
97
Paul Stewarta3c56f92011-05-26 07:08:52 -070098void DeviceInfo::Start() {
99 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500100 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700101 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500102 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700103 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
104 RTNLHandler::kRequestAddr);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700105}
106
Paul Stewarta3c56f92011-05-26 07:08:52 -0700107void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700108 link_listener_.reset();
109 address_listener_.reset();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700110}
111
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700112void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700113 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
114 << device->interface_index() << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700115 CHECK(!GetDevice(device->interface_index()).get());
116 infos_[device->interface_index()].device = device;
Paul Stewartfdd16072011-09-16 12:41:35 -0700117 if (device->TechnologyIs(Technology::kCellular) ||
118 device->TechnologyIs(Technology::kEthernet) ||
119 device->TechnologyIs(Technology::kWifi)) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700120 manager_->RegisterDevice(device);
121 }
122}
123
Jason Glasgowe9089492012-02-23 17:57:37 -0500124void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
125 int interface_index = device->interface_index();
126
Ben Chanfad4a0b2012-04-18 15:49:59 -0700127 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
128 << interface_index << ")";
Jason Glasgowe9089492012-02-23 17:57:37 -0500129 CHECK(device->TechnologyIs(Technology::kCellular));
130
131 // Release reference to the device
132 map<int, Info>::iterator iter = infos_.find(interface_index);
133 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700134 SLOG(Device, 2) << "Removing device from info for index: "
135 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500136 manager_->DeregisterDevice(device);
137 // Release the reference to the device, but maintain the mapping
138 // for the index. That will be cleaned up by an RTNL message.
139 iter->second.device = NULL;
140 }
141}
142
Paul Stewartca876ee2012-04-21 08:55:58 -0700143FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name,
144 const string &path_name) {
145 return device_info_root_.Append(iface_name).Append(path_name);
146}
147
148bool DeviceInfo::GetDeviceInfoContents(const string &iface_name,
149 const string &path_name,
150 string *contents_out) {
151 return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
152 contents_out);
153}
154bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name,
155 const string &path_name,
156 FilePath *path_out) {
157 return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
158 path_out);
159}
160
Paul Stewartfdd16072011-09-16 12:41:35 -0700161Technology::Identifier DeviceInfo::GetDeviceTechnology(
162 const string &iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700163 string type_string;
164 int arp_type = ARPHRD_VOID;;
165 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
166 TrimString(type_string, "\n", &type_string) &&
167 !base::StringToInt(type_string, &arp_type)) {
168 arp_type = ARPHRD_VOID;
169 }
170
Paul Stewart9364c4c2011-12-06 17:12:42 -0800171 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700172 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700173 SLOG(Device, 2) << StringPrintf("%s: device %s has no uevent file",
174 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700175 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800176 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700177
Thieu Le8f1c8352012-04-16 11:02:12 -0700178 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
179 // start of the file or after a newline, we can safely assume this
180 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800181 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700182 SLOG(Device, 2)
183 << StringPrintf("%s: device %s has wifi signature in uevent file",
184 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700185 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700186 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
187 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800188 return Technology::kWiFiMonitor;
189 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700190 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700191 }
192
Paul Stewart9364c4c2011-12-06 17:12:42 -0800193 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700194 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700195 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
196 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700197 if (arp_type == ARPHRD_LOOPBACK) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700198 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
199 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700200 return Technology::kLoopback;
201 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700202 if (arp_type == ARPHRD_PPP) {
203 SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device",
204 __func__, iface_name.c_str());
205 return Technology::kPPP;
206 }
207 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800208 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700209 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
210 TrimString(tun_flags_str, "\n", &tun_flags_str) &&
211 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800212 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700213 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
214 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800215 return Technology::kTunnel;
216 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700217 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800218 }
219
220 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700221 // See if driver for this interface is in a list of known modem driver names.
222 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
223 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800224 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700225 SLOG(Device, 2)
226 << StringPrintf("%s: device %s is matched with modem driver %s",
227 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800228 return Technology::kCellular;
229 }
230 }
231
Thieu Le8f1c8352012-04-16 11:02:12 -0700232 // For cdc_ether devices, make sure it's a modem because this driver
233 // can be used for other ethernet devices.
234 if (driver_name == kDriverCdcEther &&
235 IsCdcEtherModemDevice(iface_name)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700236 SLOG(Device, 2) << StringPrintf("%s: device %s is a modem cdc_ether device",
237 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700238 return Technology::kCellular;
239 }
240
Jason Glasgowabc54032012-04-20 16:08:32 -0400241 // Special case for pseudo modems which are used for testing
242 if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) {
243 SLOG(Device, 2) << StringPrintf(
244 "%s: device %s is a pseudo modem for testing",
245 __func__, iface_name.c_str());
246 return Technology::kCellular;
247 }
248
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700249 // Special case for the virtio driver, used when run under KVM. See also
250 // the comment in VirtioEthernet::Start.
251 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700252 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
253 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700254 return Technology::kVirtioEthernet;
255 }
256
Ben Chanfad4a0b2012-04-18 15:49:59 -0700257 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
258 "is defaulted to type ethernet",
259 __func__, iface_name.c_str(),
260 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700261 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700262}
263
Thieu Le8f1c8352012-04-16 11:02:12 -0700264bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
265 // A cdc_ether device is a modem device if it also exposes tty interfaces.
266 // To determine this, we look for the existence of the tty interface in the
267 // USB device sysfs tree.
268 //
269 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
270 // follows:
271 //
272 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
273 // 1-2:1.0
274 // tty
275 // ttyACM0
276 // 1-2:1.1
277 // net
278 // usb0
279 // 1-2:1.2
280 // tty
281 // ttyACM1
282 // ...
283 //
284 // /sys/class/net/usb0/device symlinks to
285 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700286 //
287 // Note that some modem devices have the tty directory one level deeper
288 // (eg. E362), so the device tree for the tty interface is:
289 // /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 -0700290
Paul Stewartca876ee2012-04-21 08:55:58 -0700291 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700292 FilePath device_path;
293 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700294 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
295 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700296 return false;
297 }
298 if (!device_path.IsAbsolute()) {
299 device_path = device_file.DirName().Append(device_path);
300 file_util::AbsolutePath(&device_path);
301 }
302
303 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700304 // USB device and see if there's a subdirectory "tty" inside. In other
305 // words, using the example dir hierarchy above, find
306 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700307 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700308 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700309}
310
311// static
Thieu Leb27beee2012-04-20 09:19:06 -0700312bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
313 file_util::FileEnumerator::FileType type =
314 static_cast<file_util::FileEnumerator::FileType>(
315 file_util::FileEnumerator::DIRECTORIES |
316 file_util::FileEnumerator::SHOW_SYM_LINKS);
317 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700318 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
319 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700320 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700321 return true;
322 }
323 return false;
324}
325
Chris Masone2aa97072011-08-09 17:35:08 -0700326void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700327 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
328 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700329 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700330 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700331
Darin Petkove6193c02011-08-11 12:42:40 -0700332 unsigned int flags = msg.link_status().flags;
333 unsigned int change = msg.link_status().change;
Paul Stewart536820d2012-03-19 16:05:59 -0700334 bool new_device = !ContainsKey(infos_, dev_index);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700335 SLOG(Device, 2) << __func__ << "(index=" << dev_index
336 << std::showbase << std::hex
337 << ", flags=" << flags << ", change=" << change << ")"
338 << std::dec << std::noshowbase
339 << ", new_device=" << new_device;
Darin Petkove6193c02011-08-11 12:42:40 -0700340 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700341
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700342 DeviceRefPtr device = GetDevice(dev_index);
343 if (!device.get()) {
Chris Masone2aa97072011-08-09 17:35:08 -0700344 if (!msg.HasAttribute(IFLA_IFNAME)) {
345 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
346 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700347 }
Chris Masone2aa97072011-08-09 17:35:08 -0700348 ByteString b(msg.GetAttribute(IFLA_IFNAME));
349 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700350 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200351 infos_[dev_index].name = link_name;
352 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700353
Chris Masone2aa97072011-08-09 17:35:08 -0700354 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700355 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700356 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700357 } else {
358 technology = GetDeviceTechnology(link_name);
359 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700360 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800361 string address;
362 if (msg.HasAttribute(IFLA_ADDRESS)) {
363 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
364 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700365 SLOG(Device, 2) << "link index " << dev_index << " address "
366 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700367 } else if (technology != Technology::kTunnel &&
368 technology != Technology::kPPP) {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800369 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
370 return;
371 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700372 switch (technology) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700373 case Technology::kCellular:
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700374 // Cellular devices are managed by ModemInfo.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700375 SLOG(Device, 2) << "Cellular link " << link_name
376 << " at index " << dev_index
377 << " -- notifying ModemInfo.";
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100378 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700379 return;
Paul Stewartfdd16072011-09-16 12:41:35 -0700380 case Technology::kEthernet:
Thieu Le3426c8f2012-01-11 17:35:11 -0800381 device = new Ethernet(control_interface_, dispatcher_, metrics_,
382 manager_, link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800383 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700384 break;
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700385 case Technology::kVirtioEthernet:
386 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
387 manager_, link_name, address, dev_index);
388 device->EnableIPv6Privacy();
389 break;
Paul Stewartfdd16072011-09-16 12:41:35 -0700390 case Technology::kWifi:
Thieu Le3426c8f2012-01-11 17:35:11 -0800391 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700392 link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800393 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700394 break;
Paul Stewartca876ee2012-04-21 08:55:58 -0700395 case Technology::kPPP:
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800396 case Technology::kTunnel:
Paul Stewartca876ee2012-04-21 08:55:58 -0700397 // Tunnel and PPP devices are managed by the VPN code (PPP for
398 // l2tpipsec). Notify the VPN Provider only if this is the first
399 // time we have seen this device index.
Paul Stewart536820d2012-03-19 16:05:59 -0700400 if (new_device) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700401 SLOG(Device, 2) << "Tunnel / PPP link " << link_name
Ben Chanfad4a0b2012-04-18 15:49:59 -0700402 << " at index " << dev_index
403 << " -- notifying VPNProvider.";
Paul Stewart536820d2012-03-19 16:05:59 -0700404 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
Paul Stewartca876ee2012-04-21 08:55:58 -0700405 dev_index) &&
406 technology == Technology::kTunnel) {
Paul Stewart536820d2012-03-19 16:05:59 -0700407 // If VPN does not know anything about this tunnel, it is probably
408 // left over from a previous instance and should not exist.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700409 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
Paul Stewart536820d2012-03-19 16:05:59 -0700410 DeleteInterface(dev_index);
411 }
Paul Stewartca6abd42012-03-01 15:45:29 -0800412 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800413 return;
Paul Stewarte81eb702012-04-11 15:04:53 -0700414 case Technology::kLoopback:
415 // Loopback devices are largely ignored, but we should make sure the
416 // link is enabled.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700417 SLOG(Device, 2) << "Bringing up loopback device " << link_name
418 << " at index " << dev_index;
Paul Stewarte81eb702012-04-11 15:04:53 -0700419 rtnl_handler_->SetInterfaceFlags(dev_index, IFF_UP, IFF_UP);
420 return;
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700421 default:
Thieu Le3426c8f2012-01-11 17:35:11 -0800422 device = new DeviceStub(control_interface_, dispatcher_, metrics_,
423 manager_, link_name, address, dev_index,
424 technology);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700425 break;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700426 }
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700427 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700428 }
Darin Petkove6193c02011-08-11 12:42:40 -0700429 device->LinkEvent(flags, change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700430}
431
Chris Masone2aa97072011-08-09 17:35:08 -0700432void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700433 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700434
Paul Stewart9a908082011-08-31 12:18:48 -0700435 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
436 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700437 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
438 << std::showbase << std::hex
439 << ", flags=" << msg.link_status().flags
440 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700441 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700442}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700443
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700444DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
445 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700446 return info ? info->device : NULL;
447}
448
Darin Petkovf8046b82012-04-24 16:29:23 +0200449int DeviceInfo::GetIndex(const string &interface_name) const {
450 map<string, int>::const_iterator it = indices_.find(interface_name);
451 return it == indices_.end() ? -1 : it->second;
452}
453
Paul Stewart32852962011-08-30 14:06:53 -0700454bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700455 const Info *info = GetInfo(interface_index);
456 if (!info) {
457 return false;
458 }
Paul Stewart32852962011-08-30 14:06:53 -0700459 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700460 return true;
461}
462
Paul Stewart9a908082011-08-31 12:18:48 -0700463bool DeviceInfo::GetAddresses(int interface_index,
464 vector<AddressData> *addresses) const {
465 const Info *info = GetInfo(interface_index);
466 if (!info) {
467 return false;
468 }
469 *addresses = info->ip_addresses;
470 return true;
471}
472
473void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700474 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700475 const Info *info = GetInfo(interface_index);
476 if (!info) {
477 return;
478 }
479 const vector<AddressData> &addresses = info->ip_addresses;
480 vector<AddressData>::const_iterator iter;
481 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700482 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700483 (iter->scope == RT_SCOPE_UNIVERSE &&
484 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700485 SLOG(Device, 2) << __func__ << ": removing ip address from interface "
486 << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700487 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
488 }
489 }
490}
491
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700492bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
493 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700494 if (!info) {
495 return false;
496 }
497 *flags = info->flags;
498 return true;
499}
500
Paul Stewartca6abd42012-03-01 15:45:29 -0800501bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800502 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
503 if (fd < 0) {
504 PLOG(ERROR) << "failed to open " << kTunDeviceName;
505 return false;
506 }
507 file_util::ScopedFD scoped_fd(&fd);
508
509 struct ifreq ifr;
510 memset(&ifr, 0, sizeof(ifr));
511 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
512 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
513 PLOG(ERROR) << "failed to create tunnel interface";
514 return false;
515 }
516
517 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
518 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
519 return false;
520 }
521
522 *interface_name = string(ifr.ifr_name);
523
524 return true;
525}
526
Paul Stewartca6abd42012-03-01 15:45:29 -0800527bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800528 return rtnl_handler_->RemoveInterface(interface_index);
529}
530
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700531const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
532 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700533 if (iter == infos_.end()) {
534 return NULL;
535 }
536 return &iter->second;
537}
538
539void DeviceInfo::RemoveInfo(int interface_index) {
540 map<int, Info>::iterator iter = infos_.find(interface_index);
541 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700542 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700543 if (iter->second.device.get()) {
544 manager_->DeregisterDevice(iter->second.device);
545 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200546 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700547 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700548 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700549 SLOG(Device, 2) << __func__ << ": Unknown device index: "
550 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700551 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700552}
553
Chris Masone2aa97072011-08-09 17:35:08 -0700554void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700555 DCHECK(msg.type() == RTNLMessage::kTypeLink);
556 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700557 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700558 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700559 DelLinkMsgHandler(msg);
560 } else {
561 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700562 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700563}
564
Paul Stewart9a908082011-08-31 12:18:48 -0700565void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700566 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700567 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
568 int interface_index = msg.interface_index();
569 if (!ContainsKey(infos_, interface_index)) {
570 LOG(ERROR) << "Got address type message for unknown index "
571 << interface_index;
572 return;
573 }
574 const RTNLMessage::AddressStatus &status = msg.address_status();
575 IPAddress address(msg.family(),
576 msg.GetAttribute(IFA_ADDRESS),
577 status.prefix_len);
578
579 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
580 vector<AddressData>::iterator iter;
581 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
582 if (address.Equals(iter->address)) {
583 break;
584 }
585 }
586 if (iter != address_list.end()) {
587 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700588 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700589 address_list.erase(iter);
590 } else {
591 iter->flags = status.flags;
592 iter->scope = status.scope;
593 }
594 } else if (msg.mode() == RTNLMessage::kModeAdd) {
595 address_list.push_back(AddressData(address, status.flags, status.scope));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700596 SLOG(Device, 2) << "Add address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700597 }
598}
599
Paul Stewart0af98bf2011-05-10 17:38:08 -0700600} // namespace shill