blob: 34680bc77eb8ea04804e08cca6cb384d4cedb2af [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"
Chris Masone487b8bf2011-05-13 16:27:57 -070040#include "shill/service.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070041#include "shill/wifi.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070042
Eric Shienbrood3e20a232012-02-16 11:35:56 -050043using base::Bind;
Thieu Le8f1c8352012-04-16 11:02:12 -070044using base::StringPrintf;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050045using base::Unretained;
Darin Petkove6193c02011-08-11 12:42:40 -070046using std::map;
Paul Stewart0af98bf2011-05-10 17:38:08 -070047using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070048using std::vector;
Paul Stewart0af98bf2011-05-10 17:38:08 -070049
50namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070051
52// static
Paul Stewartbf1861b2011-08-23 15:45:35 -070053const char DeviceInfo::kInterfaceUevent[] = "/sys/class/net/%s/uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080054const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Thieu Le8f1c8352012-04-16 11:02:12 -070055const char DeviceInfo::kInterfaceDevice[] = "/sys/class/net/%s/device";
Paul Stewartbf1861b2011-08-23 15:45:35 -070056const char DeviceInfo::kInterfaceDriver[] = "/sys/class/net/%s/device/driver";
Paul Stewartcba0f7f2012-02-29 16:33:05 -080057const char DeviceInfo::kInterfaceTunFlags[] = "/sys/class/net/%s/tun_flags";
Paul Stewart2001a422011-12-15 10:20:09 -080058const char DeviceInfo::kInterfaceType[] = "/sys/class/net/%s/type";
Paul Stewarte81eb702012-04-11 15:04:53 -070059const char DeviceInfo::kLoopbackDeviceName[] = "lo";
Thieu Le8f1c8352012-04-16 11:02:12 -070060const char DeviceInfo::kDriverCdcEther[] = "cdc_ether";
Paul Stewartb50f0b92011-05-16 16:31:42 -070061const char *DeviceInfo::kModemDrivers[] = {
62 "gobi",
63 "QCUSBNet2k",
Thieu Le8f1c8352012-04-16 11:02:12 -070064 "GobiNet"
Paul Stewartb50f0b92011-05-16 16:31:42 -070065};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080066const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewartb50f0b92011-05-16 16:31:42 -070067
68DeviceInfo::DeviceInfo(ControlInterface *control_interface,
69 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080070 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070071 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070072 : control_interface_(control_interface),
73 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080074 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070075 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050076 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
77 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070078 link_listener_(NULL),
79 address_listener_(NULL),
80 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070081}
82
Paul Stewarta3c56f92011-05-26 07:08:52 -070083DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070084
mukesh agrawal8f317b62011-07-15 11:53:23 -070085void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
86 black_list_.insert(device_name);
87}
88
Eric Shienbrood5e628a52012-03-21 16:56:59 -040089bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
90 return ContainsKey(black_list_, device_name);
91}
92
Paul Stewarta3c56f92011-05-26 07:08:52 -070093void DeviceInfo::Start() {
94 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -050095 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -070096 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -050097 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -070098 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
99 RTNLHandler::kRequestAddr);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700100}
101
Paul Stewarta3c56f92011-05-26 07:08:52 -0700102void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700103 link_listener_.reset();
104 address_listener_.reset();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700105}
106
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700107void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
108 VLOG(2) << __func__ << "(" << device->link_name() << ", "
109 << device->interface_index() << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700110 CHECK(!GetDevice(device->interface_index()).get());
111 infos_[device->interface_index()].device = device;
Paul Stewartfdd16072011-09-16 12:41:35 -0700112 if (device->TechnologyIs(Technology::kCellular) ||
113 device->TechnologyIs(Technology::kEthernet) ||
114 device->TechnologyIs(Technology::kWifi)) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700115 manager_->RegisterDevice(device);
116 }
117}
118
Jason Glasgowe9089492012-02-23 17:57:37 -0500119void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
120 int interface_index = device->interface_index();
121
122 VLOG(2) << __func__ << "(" << device->link_name() << ", "
123 << interface_index << ")";
124 CHECK(device->TechnologyIs(Technology::kCellular));
125
126 // Release reference to the device
127 map<int, Info>::iterator iter = infos_.find(interface_index);
128 if (iter != infos_.end()) {
129 VLOG(2) << "Removing device from info for index: " << interface_index;
130 manager_->DeregisterDevice(device);
131 // Release the reference to the device, but maintain the mapping
132 // for the index. That will be cleaned up by an RTNL message.
133 iter->second.device = NULL;
134 }
135}
136
Thieu Le8f1c8352012-04-16 11:02:12 -0700137// static
Paul Stewartfdd16072011-09-16 12:41:35 -0700138Technology::Identifier DeviceInfo::GetDeviceTechnology(
139 const string &iface_name) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800140 FilePath uevent_file(StringPrintf(kInterfaceUevent, iface_name.c_str()));
141 string contents;
142 if (!file_util::ReadFileToString(uevent_file, &contents)) {
143 VLOG(2) << StringPrintf("%s: device %s has no uevent file",
144 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700145 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800146 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700147
Thieu Le8f1c8352012-04-16 11:02:12 -0700148 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
149 // start of the file or after a newline, we can safely assume this
150 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800151 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
152 VLOG(2) << StringPrintf("%s: device %s has wifi signature in uevent file",
153 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800154 FilePath type_file(StringPrintf(kInterfaceType, iface_name.c_str()));
155 string type_string;
156 int type_val = 0;
157 if (file_util::ReadFileToString(type_file, &type_string) &&
158 TrimString(type_string, "\n", &type_string) &&
159 base::StringToInt(type_string, &type_val) &&
160 type_val == ARPHRD_IEEE80211_RADIOTAP) {
161 VLOG(2) << StringPrintf("%s: wifi device %s is in monitor mode",
162 __func__, iface_name.c_str());
163 return Technology::kWiFiMonitor;
164 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700165 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700166 }
167
Paul Stewart9364c4c2011-12-06 17:12:42 -0800168 FilePath driver_file(StringPrintf(kInterfaceDriver, iface_name.c_str()));
169 FilePath driver_path;
170 if (!file_util::ReadSymbolicLink(driver_file, &driver_path)) {
171 VLOG(2) << StringPrintf("%s: device %s has no device symlink",
172 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700173 if (iface_name == kLoopbackDeviceName) {
174 VLOG(2) << StringPrintf("%s: device %s is a loopback device",
175 __func__, iface_name.c_str());
176 return Technology::kLoopback;
177 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800178 FilePath tun_flags_file(StringPrintf(kInterfaceTunFlags,
179 iface_name.c_str()));
180 string tun_flags_string;
181 int tun_flags = 0;
182 if (file_util::ReadFileToString(tun_flags_file, &tun_flags_string) &&
183 TrimString(tun_flags_string, "\n", &tun_flags_string) &&
184 base::HexStringToInt(tun_flags_string, &tun_flags) &&
185 (tun_flags & IFF_TUN)) {
186 VLOG(2) << StringPrintf("%s: device %s is tun device",
187 __func__, iface_name.c_str());
188 return Technology::kTunnel;
189 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700190 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800191 }
192
193 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700194 // See if driver for this interface is in a list of known modem driver names.
195 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
196 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800197 if (driver_name == kModemDrivers[modem_idx]) {
198 VLOG(2) << StringPrintf("%s: device %s is matched with modem driver %s",
199 __func__, iface_name.c_str(),
200 driver_name.c_str());
201 return Technology::kCellular;
202 }
203 }
204
Thieu Le8f1c8352012-04-16 11:02:12 -0700205 // For cdc_ether devices, make sure it's a modem because this driver
206 // can be used for other ethernet devices.
207 if (driver_name == kDriverCdcEther &&
208 IsCdcEtherModemDevice(iface_name)) {
209 VLOG(2) << StringPrintf("%s: device %s is a modem cdc_ether device",
210 __func__, iface_name.c_str());
211 return Technology::kCellular;
212 }
213
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800214 VLOG(2) << StringPrintf("%s: device %s is defaulted to type ethernet",
Paul Stewart9364c4c2011-12-06 17:12:42 -0800215 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700216 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700217}
218
Thieu Le8f1c8352012-04-16 11:02:12 -0700219// static
220bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
221 // A cdc_ether device is a modem device if it also exposes tty interfaces.
222 // To determine this, we look for the existence of the tty interface in the
223 // USB device sysfs tree.
224 //
225 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
226 // follows:
227 //
228 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
229 // 1-2:1.0
230 // tty
231 // ttyACM0
232 // 1-2:1.1
233 // net
234 // usb0
235 // 1-2:1.2
236 // tty
237 // ttyACM1
238 // ...
239 //
240 // /sys/class/net/usb0/device symlinks to
241 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
242
243 FilePath device_file(StringPrintf(kInterfaceDevice, iface_name.c_str()));
244 FilePath device_path;
245 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
246 VLOG(2) << StringPrintf("%s: device %s has no device symlink",
247 __func__, iface_name.c_str());
248 return false;
249 }
250 if (!device_path.IsAbsolute()) {
251 device_path = device_file.DirName().Append(device_path);
252 file_util::AbsolutePath(&device_path);
253 }
254
255 // Look for tty interface by enumerating all directories under the parent
256 // USB device and seeing if there's a subdirectory "tty" inside of the
257 // enumerated directory. In other words, using the example dir hierarchy
258 // above, find /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2*/tty.
259 // If this exists, then this is a modem device.
260 FilePath usb_device_path(device_path.DirName());
261 string search_pattern(usb_device_path.BaseName().MaybeAsASCII() + "*");
262 return IsGrandchildSubdir(usb_device_path, search_pattern, "tty");
263}
264
265// static
266bool DeviceInfo::IsGrandchildSubdir(const FilePath &base_dir,
267 const string &children_filter,
268 const string &grandchild) {
269 file_util::FileEnumerator dir_enum(base_dir, false,
270 file_util::FileEnumerator::DIRECTORIES,
271 children_filter);
272 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
273 curr_dir = dir_enum.Next()) {
274 if (file_util::DirectoryExists(curr_dir.Append(grandchild)))
275 return true;
276 }
277 return false;
278}
279
Chris Masone2aa97072011-08-09 17:35:08 -0700280void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700281 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
282 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700283 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700284 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700285
Darin Petkove6193c02011-08-11 12:42:40 -0700286 unsigned int flags = msg.link_status().flags;
287 unsigned int change = msg.link_status().change;
Paul Stewart536820d2012-03-19 16:05:59 -0700288 bool new_device = !ContainsKey(infos_, dev_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700289 VLOG(2) << __func__ << "(index=" << dev_index
mukesh agrawal47009f82011-08-25 14:07:35 -0700290 << std::showbase << std::hex
291 << ", flags=" << flags << ", change=" << change << ")"
Paul Stewart536820d2012-03-19 16:05:59 -0700292 << std::dec << std::noshowbase
293 << ", new_device=" << new_device;
Darin Petkove6193c02011-08-11 12:42:40 -0700294 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700295
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700296 DeviceRefPtr device = GetDevice(dev_index);
297 if (!device.get()) {
Chris Masone2aa97072011-08-09 17:35:08 -0700298 if (!msg.HasAttribute(IFLA_IFNAME)) {
299 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
300 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700301 }
Chris Masone2aa97072011-08-09 17:35:08 -0700302 ByteString b(msg.GetAttribute(IFLA_IFNAME));
303 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Paul Stewarta3c56f92011-05-26 07:08:52 -0700304 VLOG(2) << "add link index " << dev_index << " name " << link_name;
305
Chris Masone2aa97072011-08-09 17:35:08 -0700306 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700307 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700308 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700309 } else {
310 technology = GetDeviceTechnology(link_name);
311 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700312 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800313 string address;
314 if (msg.HasAttribute(IFLA_ADDRESS)) {
315 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
316 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
317 VLOG(2) << "link index " << dev_index << " address "
318 << infos_[dev_index].mac_address.HexEncode();
319 } else if (technology != Technology::kTunnel) {
320 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
321 return;
322 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700323 switch (technology) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700324 case Technology::kCellular:
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700325 // Cellular devices are managed by ModemInfo.
326 VLOG(2) << "Cellular link " << link_name << " at index " << dev_index
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100327 << " -- notifying ModemInfo.";
328 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700329 return;
Paul Stewartfdd16072011-09-16 12:41:35 -0700330 case Technology::kEthernet:
Thieu Le3426c8f2012-01-11 17:35:11 -0800331 device = new Ethernet(control_interface_, dispatcher_, metrics_,
332 manager_, link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800333 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700334 break;
Paul Stewartfdd16072011-09-16 12:41:35 -0700335 case Technology::kWifi:
Thieu Le3426c8f2012-01-11 17:35:11 -0800336 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700337 link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800338 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700339 break;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800340 case Technology::kTunnel:
Paul Stewart536820d2012-03-19 16:05:59 -0700341 // Tunnel devices are managed by the VPN code. Notify the VPN Provider
342 // only if this is the first time we have seen this device index.
343 if (new_device) {
344 VLOG(2) << "Tunnel link " << link_name << " at index " << dev_index
345 << " -- notifying VPNProvider.";
346 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
347 dev_index)) {
348 // If VPN does not know anything about this tunnel, it is probably
349 // left over from a previous instance and should not exist.
350 VLOG(2) << "Tunnel link is unused. Deleting.";
351 DeleteInterface(dev_index);
352 }
Paul Stewartca6abd42012-03-01 15:45:29 -0800353 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800354 return;
Paul Stewarte81eb702012-04-11 15:04:53 -0700355 case Technology::kLoopback:
356 // Loopback devices are largely ignored, but we should make sure the
357 // link is enabled.
358 VLOG(2) << "Bringing up loopback device " << link_name << " at index "
359 << dev_index;
360 rtnl_handler_->SetInterfaceFlags(dev_index, IFF_UP, IFF_UP);
361 return;
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700362 default:
Thieu Le3426c8f2012-01-11 17:35:11 -0800363 device = new DeviceStub(control_interface_, dispatcher_, metrics_,
364 manager_, link_name, address, dev_index,
365 technology);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700366 break;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700367 }
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700368 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700369 }
Darin Petkove6193c02011-08-11 12:42:40 -0700370 device->LinkEvent(flags, change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700371}
372
Chris Masone2aa97072011-08-09 17:35:08 -0700373void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700374 VLOG(2) << __func__ << "(index=" << msg.interface_index() << ")";
375
Paul Stewart9a908082011-08-31 12:18:48 -0700376 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
377 msg.mode() == RTNLMessage::kModeDelete);
Paul Stewart2713d6c2011-08-25 15:38:15 -0700378 VLOG(2) << __func__ << "(index=" << msg.interface_index()
379 << std::showbase << std::hex
380 << ", flags=" << msg.link_status().flags
381 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700382 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700383}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700384
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700385DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
386 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700387 return info ? info->device : NULL;
388}
389
Paul Stewart32852962011-08-30 14:06:53 -0700390bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700391 const Info *info = GetInfo(interface_index);
392 if (!info) {
393 return false;
394 }
Paul Stewart32852962011-08-30 14:06:53 -0700395 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700396 return true;
397}
398
Paul Stewart9a908082011-08-31 12:18:48 -0700399bool DeviceInfo::GetAddresses(int interface_index,
400 vector<AddressData> *addresses) const {
401 const Info *info = GetInfo(interface_index);
402 if (!info) {
403 return false;
404 }
405 *addresses = info->ip_addresses;
406 return true;
407}
408
409void DeviceInfo::FlushAddresses(int interface_index) const {
Paul Stewart7355ce12011-09-02 10:47:01 -0700410 VLOG(2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700411 const Info *info = GetInfo(interface_index);
412 if (!info) {
413 return;
414 }
415 const vector<AddressData> &addresses = info->ip_addresses;
416 vector<AddressData>::const_iterator iter;
417 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700418 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700419 (iter->scope == RT_SCOPE_UNIVERSE &&
420 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
421 VLOG(2) << __func__ << ": removing ip address from interface "
422 << interface_index;
423 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
424 }
425 }
426}
427
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700428bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
429 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700430 if (!info) {
431 return false;
432 }
433 *flags = info->flags;
434 return true;
435}
436
Paul Stewartca6abd42012-03-01 15:45:29 -0800437bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800438 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
439 if (fd < 0) {
440 PLOG(ERROR) << "failed to open " << kTunDeviceName;
441 return false;
442 }
443 file_util::ScopedFD scoped_fd(&fd);
444
445 struct ifreq ifr;
446 memset(&ifr, 0, sizeof(ifr));
447 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
448 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
449 PLOG(ERROR) << "failed to create tunnel interface";
450 return false;
451 }
452
453 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
454 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
455 return false;
456 }
457
458 *interface_name = string(ifr.ifr_name);
459
460 return true;
461}
462
Paul Stewartca6abd42012-03-01 15:45:29 -0800463bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800464 return rtnl_handler_->RemoveInterface(interface_index);
465}
466
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700467const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
468 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700469 if (iter == infos_.end()) {
470 return NULL;
471 }
472 return &iter->second;
473}
474
475void DeviceInfo::RemoveInfo(int interface_index) {
476 map<int, Info>::iterator iter = infos_.find(interface_index);
477 if (iter != infos_.end()) {
478 VLOG(2) << "Removing info for device index: " << interface_index;
479 if (iter->second.device.get()) {
480 manager_->DeregisterDevice(iter->second.device);
481 }
482 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700483 } else {
484 VLOG(2) << __func__ << "unknown device index: " << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700485 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700486}
487
Chris Masone2aa97072011-08-09 17:35:08 -0700488void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700489 DCHECK(msg.type() == RTNLMessage::kTypeLink);
490 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700491 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700492 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700493 DelLinkMsgHandler(msg);
494 } else {
495 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700496 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700497}
498
Paul Stewart9a908082011-08-31 12:18:48 -0700499void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700500 VLOG(2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700501 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
502 int interface_index = msg.interface_index();
503 if (!ContainsKey(infos_, interface_index)) {
504 LOG(ERROR) << "Got address type message for unknown index "
505 << interface_index;
506 return;
507 }
508 const RTNLMessage::AddressStatus &status = msg.address_status();
509 IPAddress address(msg.family(),
510 msg.GetAttribute(IFA_ADDRESS),
511 status.prefix_len);
512
513 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
514 vector<AddressData>::iterator iter;
515 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
516 if (address.Equals(iter->address)) {
517 break;
518 }
519 }
520 if (iter != address_list.end()) {
521 if (msg.mode() == RTNLMessage::kModeDelete) {
522 VLOG(2) << "Delete address for interface " << interface_index;
523 address_list.erase(iter);
524 } else {
525 iter->flags = status.flags;
526 iter->scope = status.scope;
527 }
528 } else if (msg.mode() == RTNLMessage::kModeAdd) {
529 address_list.push_back(AddressData(address, status.flags, status.scope));
530 VLOG(2) << "Add address for interface " << interface_index;
531 }
532}
533
Paul Stewart0af98bf2011-05-10 17:38:08 -0700534} // namespace shill