blob: f5aa4a0f9088b8282a83cffc5bdbc01ac56f510d [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"
mukesh agrawal93a29ed2012-04-17 16:13:01 -070041#include "shill/virtio_ethernet.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070042#include "shill/wifi.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070043
Eric Shienbrood3e20a232012-02-16 11:35:56 -050044using base::Bind;
Thieu Le8f1c8352012-04-16 11:02:12 -070045using base::StringPrintf;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050046using base::Unretained;
Darin Petkove6193c02011-08-11 12:42:40 -070047using std::map;
Paul Stewart0af98bf2011-05-10 17:38:08 -070048using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070049using std::vector;
Paul Stewart0af98bf2011-05-10 17:38:08 -070050
51namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070052
53// static
mukesh agrawal93a29ed2012-04-17 16:13:01 -070054const char DeviceInfo::kDriverVirtioNet[] = "virtio_net";
Paul Stewartbf1861b2011-08-23 15:45:35 -070055const char DeviceInfo::kInterfaceUevent[] = "/sys/class/net/%s/uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080056const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Thieu Le8f1c8352012-04-16 11:02:12 -070057const char DeviceInfo::kInterfaceDevice[] = "/sys/class/net/%s/device";
Paul Stewartbf1861b2011-08-23 15:45:35 -070058const char DeviceInfo::kInterfaceDriver[] = "/sys/class/net/%s/device/driver";
Paul Stewartcba0f7f2012-02-29 16:33:05 -080059const char DeviceInfo::kInterfaceTunFlags[] = "/sys/class/net/%s/tun_flags";
Paul Stewart2001a422011-12-15 10:20:09 -080060const char DeviceInfo::kInterfaceType[] = "/sys/class/net/%s/type";
Paul Stewarte81eb702012-04-11 15:04:53 -070061const char DeviceInfo::kLoopbackDeviceName[] = "lo";
Thieu Le8f1c8352012-04-16 11:02:12 -070062const char DeviceInfo::kDriverCdcEther[] = "cdc_ether";
Paul Stewartb50f0b92011-05-16 16:31:42 -070063const char *DeviceInfo::kModemDrivers[] = {
64 "gobi",
65 "QCUSBNet2k",
Thieu Le8f1c8352012-04-16 11:02:12 -070066 "GobiNet"
Paul Stewartb50f0b92011-05-16 16:31:42 -070067};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080068const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewartb50f0b92011-05-16 16:31:42 -070069
70DeviceInfo::DeviceInfo(ControlInterface *control_interface,
71 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080072 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070073 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070074 : control_interface_(control_interface),
75 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080076 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070077 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050078 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
79 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070080 link_listener_(NULL),
81 address_listener_(NULL),
82 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070083}
84
Paul Stewarta3c56f92011-05-26 07:08:52 -070085DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070086
mukesh agrawal8f317b62011-07-15 11:53:23 -070087void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
88 black_list_.insert(device_name);
89}
90
Eric Shienbrood5e628a52012-03-21 16:56:59 -040091bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
92 return ContainsKey(black_list_, device_name);
93}
94
Paul Stewarta3c56f92011-05-26 07:08:52 -070095void DeviceInfo::Start() {
96 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -050097 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -070098 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -050099 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700100 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
101 RTNLHandler::kRequestAddr);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700102}
103
Paul Stewarta3c56f92011-05-26 07:08:52 -0700104void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700105 link_listener_.reset();
106 address_listener_.reset();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700107}
108
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700109void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
110 VLOG(2) << __func__ << "(" << device->link_name() << ", "
111 << device->interface_index() << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700112 CHECK(!GetDevice(device->interface_index()).get());
113 infos_[device->interface_index()].device = device;
Paul Stewartfdd16072011-09-16 12:41:35 -0700114 if (device->TechnologyIs(Technology::kCellular) ||
115 device->TechnologyIs(Technology::kEthernet) ||
116 device->TechnologyIs(Technology::kWifi)) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700117 manager_->RegisterDevice(device);
118 }
119}
120
Jason Glasgowe9089492012-02-23 17:57:37 -0500121void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
122 int interface_index = device->interface_index();
123
124 VLOG(2) << __func__ << "(" << device->link_name() << ", "
125 << interface_index << ")";
126 CHECK(device->TechnologyIs(Technology::kCellular));
127
128 // Release reference to the device
129 map<int, Info>::iterator iter = infos_.find(interface_index);
130 if (iter != infos_.end()) {
131 VLOG(2) << "Removing device from info for index: " << interface_index;
132 manager_->DeregisterDevice(device);
133 // Release the reference to the device, but maintain the mapping
134 // for the index. That will be cleaned up by an RTNL message.
135 iter->second.device = NULL;
136 }
137}
138
Thieu Le8f1c8352012-04-16 11:02:12 -0700139// static
Paul Stewartfdd16072011-09-16 12:41:35 -0700140Technology::Identifier DeviceInfo::GetDeviceTechnology(
141 const string &iface_name) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800142 FilePath uevent_file(StringPrintf(kInterfaceUevent, iface_name.c_str()));
143 string contents;
144 if (!file_util::ReadFileToString(uevent_file, &contents)) {
145 VLOG(2) << StringPrintf("%s: device %s has no uevent file",
146 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700147 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800148 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700149
Thieu Le8f1c8352012-04-16 11:02:12 -0700150 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
151 // start of the file or after a newline, we can safely assume this
152 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800153 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
154 VLOG(2) << StringPrintf("%s: device %s has wifi signature in uevent file",
155 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800156 FilePath type_file(StringPrintf(kInterfaceType, iface_name.c_str()));
157 string type_string;
158 int type_val = 0;
159 if (file_util::ReadFileToString(type_file, &type_string) &&
160 TrimString(type_string, "\n", &type_string) &&
161 base::StringToInt(type_string, &type_val) &&
162 type_val == ARPHRD_IEEE80211_RADIOTAP) {
163 VLOG(2) << StringPrintf("%s: wifi device %s is in monitor mode",
164 __func__, iface_name.c_str());
165 return Technology::kWiFiMonitor;
166 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700167 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700168 }
169
Paul Stewart9364c4c2011-12-06 17:12:42 -0800170 FilePath driver_file(StringPrintf(kInterfaceDriver, iface_name.c_str()));
171 FilePath driver_path;
172 if (!file_util::ReadSymbolicLink(driver_file, &driver_path)) {
173 VLOG(2) << StringPrintf("%s: device %s has no device symlink",
174 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700175 if (iface_name == kLoopbackDeviceName) {
176 VLOG(2) << StringPrintf("%s: device %s is a loopback device",
177 __func__, iface_name.c_str());
178 return Technology::kLoopback;
179 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800180 FilePath tun_flags_file(StringPrintf(kInterfaceTunFlags,
181 iface_name.c_str()));
182 string tun_flags_string;
183 int tun_flags = 0;
184 if (file_util::ReadFileToString(tun_flags_file, &tun_flags_string) &&
185 TrimString(tun_flags_string, "\n", &tun_flags_string) &&
186 base::HexStringToInt(tun_flags_string, &tun_flags) &&
187 (tun_flags & IFF_TUN)) {
188 VLOG(2) << StringPrintf("%s: device %s is tun device",
189 __func__, iface_name.c_str());
190 return Technology::kTunnel;
191 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700192 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800193 }
194
195 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700196 // See if driver for this interface is in a list of known modem driver names.
197 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
198 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800199 if (driver_name == kModemDrivers[modem_idx]) {
200 VLOG(2) << StringPrintf("%s: device %s is matched with modem driver %s",
201 __func__, iface_name.c_str(),
202 driver_name.c_str());
203 return Technology::kCellular;
204 }
205 }
206
Thieu Le8f1c8352012-04-16 11:02:12 -0700207 // For cdc_ether devices, make sure it's a modem because this driver
208 // can be used for other ethernet devices.
209 if (driver_name == kDriverCdcEther &&
210 IsCdcEtherModemDevice(iface_name)) {
211 VLOG(2) << StringPrintf("%s: device %s is a modem cdc_ether device",
212 __func__, iface_name.c_str());
213 return Technology::kCellular;
214 }
215
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700216 // Special case for the virtio driver, used when run under KVM. See also
217 // the comment in VirtioEthernet::Start.
218 if (driver_name == kDriverVirtioNet) {
219 VLOG(2) << StringPrintf("%s: device %s is virtio ethernet", __func__,
220 iface_name.c_str());
221 return Technology::kVirtioEthernet;
222 }
223
224 VLOG(2) << StringPrintf("%s: device %s, with driver %s, "
225 "is defaulted to type ethernet",
226 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700227 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700228}
229
Thieu Le8f1c8352012-04-16 11:02:12 -0700230// static
231bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
232 // A cdc_ether device is a modem device if it also exposes tty interfaces.
233 // To determine this, we look for the existence of the tty interface in the
234 // USB device sysfs tree.
235 //
236 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
237 // follows:
238 //
239 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
240 // 1-2:1.0
241 // tty
242 // ttyACM0
243 // 1-2:1.1
244 // net
245 // usb0
246 // 1-2:1.2
247 // tty
248 // ttyACM1
249 // ...
250 //
251 // /sys/class/net/usb0/device symlinks to
252 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
253
254 FilePath device_file(StringPrintf(kInterfaceDevice, iface_name.c_str()));
255 FilePath device_path;
256 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
257 VLOG(2) << StringPrintf("%s: device %s has no device symlink",
258 __func__, iface_name.c_str());
259 return false;
260 }
261 if (!device_path.IsAbsolute()) {
262 device_path = device_file.DirName().Append(device_path);
263 file_util::AbsolutePath(&device_path);
264 }
265
266 // Look for tty interface by enumerating all directories under the parent
267 // USB device and seeing if there's a subdirectory "tty" inside of the
268 // enumerated directory. In other words, using the example dir hierarchy
269 // above, find /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2*/tty.
270 // If this exists, then this is a modem device.
271 FilePath usb_device_path(device_path.DirName());
272 string search_pattern(usb_device_path.BaseName().MaybeAsASCII() + "*");
273 return IsGrandchildSubdir(usb_device_path, search_pattern, "tty");
274}
275
276// static
277bool DeviceInfo::IsGrandchildSubdir(const FilePath &base_dir,
278 const string &children_filter,
279 const string &grandchild) {
280 file_util::FileEnumerator dir_enum(base_dir, false,
281 file_util::FileEnumerator::DIRECTORIES,
282 children_filter);
283 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
284 curr_dir = dir_enum.Next()) {
285 if (file_util::DirectoryExists(curr_dir.Append(grandchild)))
286 return true;
287 }
288 return false;
289}
290
Chris Masone2aa97072011-08-09 17:35:08 -0700291void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700292 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
293 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700294 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700295 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700296
Darin Petkove6193c02011-08-11 12:42:40 -0700297 unsigned int flags = msg.link_status().flags;
298 unsigned int change = msg.link_status().change;
Paul Stewart536820d2012-03-19 16:05:59 -0700299 bool new_device = !ContainsKey(infos_, dev_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700300 VLOG(2) << __func__ << "(index=" << dev_index
mukesh agrawal47009f82011-08-25 14:07:35 -0700301 << std::showbase << std::hex
302 << ", flags=" << flags << ", change=" << change << ")"
Paul Stewart536820d2012-03-19 16:05:59 -0700303 << std::dec << std::noshowbase
304 << ", new_device=" << new_device;
Darin Petkove6193c02011-08-11 12:42:40 -0700305 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700306
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700307 DeviceRefPtr device = GetDevice(dev_index);
308 if (!device.get()) {
Chris Masone2aa97072011-08-09 17:35:08 -0700309 if (!msg.HasAttribute(IFLA_IFNAME)) {
310 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
311 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700312 }
Chris Masone2aa97072011-08-09 17:35:08 -0700313 ByteString b(msg.GetAttribute(IFLA_IFNAME));
314 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Paul Stewarta3c56f92011-05-26 07:08:52 -0700315 VLOG(2) << "add link index " << dev_index << " name " << link_name;
316
Chris Masone2aa97072011-08-09 17:35:08 -0700317 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700318 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700319 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700320 } else {
321 technology = GetDeviceTechnology(link_name);
322 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700323 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800324 string address;
325 if (msg.HasAttribute(IFLA_ADDRESS)) {
326 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
327 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
328 VLOG(2) << "link index " << dev_index << " address "
329 << infos_[dev_index].mac_address.HexEncode();
330 } else if (technology != Technology::kTunnel) {
331 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
332 return;
333 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700334 switch (technology) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700335 case Technology::kCellular:
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700336 // Cellular devices are managed by ModemInfo.
337 VLOG(2) << "Cellular link " << link_name << " at index " << dev_index
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100338 << " -- notifying ModemInfo.";
339 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700340 return;
Paul Stewartfdd16072011-09-16 12:41:35 -0700341 case Technology::kEthernet:
Thieu Le3426c8f2012-01-11 17:35:11 -0800342 device = new Ethernet(control_interface_, dispatcher_, metrics_,
343 manager_, link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800344 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700345 break;
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700346 case Technology::kVirtioEthernet:
347 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
348 manager_, link_name, address, dev_index);
349 device->EnableIPv6Privacy();
350 break;
Paul Stewartfdd16072011-09-16 12:41:35 -0700351 case Technology::kWifi:
Thieu Le3426c8f2012-01-11 17:35:11 -0800352 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700353 link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800354 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700355 break;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800356 case Technology::kTunnel:
Paul Stewart536820d2012-03-19 16:05:59 -0700357 // Tunnel devices are managed by the VPN code. Notify the VPN Provider
358 // only if this is the first time we have seen this device index.
359 if (new_device) {
360 VLOG(2) << "Tunnel link " << link_name << " at index " << dev_index
361 << " -- notifying VPNProvider.";
362 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
363 dev_index)) {
364 // If VPN does not know anything about this tunnel, it is probably
365 // left over from a previous instance and should not exist.
366 VLOG(2) << "Tunnel link is unused. Deleting.";
367 DeleteInterface(dev_index);
368 }
Paul Stewartca6abd42012-03-01 15:45:29 -0800369 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800370 return;
Paul Stewarte81eb702012-04-11 15:04:53 -0700371 case Technology::kLoopback:
372 // Loopback devices are largely ignored, but we should make sure the
373 // link is enabled.
374 VLOG(2) << "Bringing up loopback device " << link_name << " at index "
375 << dev_index;
376 rtnl_handler_->SetInterfaceFlags(dev_index, IFF_UP, IFF_UP);
377 return;
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700378 default:
Thieu Le3426c8f2012-01-11 17:35:11 -0800379 device = new DeviceStub(control_interface_, dispatcher_, metrics_,
380 manager_, link_name, address, dev_index,
381 technology);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700382 break;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700383 }
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700384 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700385 }
Darin Petkove6193c02011-08-11 12:42:40 -0700386 device->LinkEvent(flags, change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700387}
388
Chris Masone2aa97072011-08-09 17:35:08 -0700389void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700390 VLOG(2) << __func__ << "(index=" << msg.interface_index() << ")";
391
Paul Stewart9a908082011-08-31 12:18:48 -0700392 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
393 msg.mode() == RTNLMessage::kModeDelete);
Paul Stewart2713d6c2011-08-25 15:38:15 -0700394 VLOG(2) << __func__ << "(index=" << msg.interface_index()
395 << std::showbase << std::hex
396 << ", flags=" << msg.link_status().flags
397 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700398 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700399}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700400
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700401DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
402 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700403 return info ? info->device : NULL;
404}
405
Paul Stewart32852962011-08-30 14:06:53 -0700406bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700407 const Info *info = GetInfo(interface_index);
408 if (!info) {
409 return false;
410 }
Paul Stewart32852962011-08-30 14:06:53 -0700411 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700412 return true;
413}
414
Paul Stewart9a908082011-08-31 12:18:48 -0700415bool DeviceInfo::GetAddresses(int interface_index,
416 vector<AddressData> *addresses) const {
417 const Info *info = GetInfo(interface_index);
418 if (!info) {
419 return false;
420 }
421 *addresses = info->ip_addresses;
422 return true;
423}
424
425void DeviceInfo::FlushAddresses(int interface_index) const {
Paul Stewart7355ce12011-09-02 10:47:01 -0700426 VLOG(2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700427 const Info *info = GetInfo(interface_index);
428 if (!info) {
429 return;
430 }
431 const vector<AddressData> &addresses = info->ip_addresses;
432 vector<AddressData>::const_iterator iter;
433 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700434 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700435 (iter->scope == RT_SCOPE_UNIVERSE &&
436 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
437 VLOG(2) << __func__ << ": removing ip address from interface "
438 << interface_index;
439 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
440 }
441 }
442}
443
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700444bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
445 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700446 if (!info) {
447 return false;
448 }
449 *flags = info->flags;
450 return true;
451}
452
Paul Stewartca6abd42012-03-01 15:45:29 -0800453bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800454 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
455 if (fd < 0) {
456 PLOG(ERROR) << "failed to open " << kTunDeviceName;
457 return false;
458 }
459 file_util::ScopedFD scoped_fd(&fd);
460
461 struct ifreq ifr;
462 memset(&ifr, 0, sizeof(ifr));
463 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
464 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
465 PLOG(ERROR) << "failed to create tunnel interface";
466 return false;
467 }
468
469 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
470 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
471 return false;
472 }
473
474 *interface_name = string(ifr.ifr_name);
475
476 return true;
477}
478
Paul Stewartca6abd42012-03-01 15:45:29 -0800479bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800480 return rtnl_handler_->RemoveInterface(interface_index);
481}
482
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700483const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
484 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700485 if (iter == infos_.end()) {
486 return NULL;
487 }
488 return &iter->second;
489}
490
491void DeviceInfo::RemoveInfo(int interface_index) {
492 map<int, Info>::iterator iter = infos_.find(interface_index);
493 if (iter != infos_.end()) {
494 VLOG(2) << "Removing info for device index: " << interface_index;
495 if (iter->second.device.get()) {
496 manager_->DeregisterDevice(iter->second.device);
497 }
498 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700499 } else {
500 VLOG(2) << __func__ << "unknown device index: " << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700501 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700502}
503
Chris Masone2aa97072011-08-09 17:35:08 -0700504void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700505 DCHECK(msg.type() == RTNLMessage::kTypeLink);
506 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700507 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700508 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700509 DelLinkMsgHandler(msg);
510 } else {
511 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700512 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700513}
514
Paul Stewart9a908082011-08-31 12:18:48 -0700515void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700516 VLOG(2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700517 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
518 int interface_index = msg.interface_index();
519 if (!ContainsKey(infos_, interface_index)) {
520 LOG(ERROR) << "Got address type message for unknown index "
521 << interface_index;
522 return;
523 }
524 const RTNLMessage::AddressStatus &status = msg.address_status();
525 IPAddress address(msg.family(),
526 msg.GetAttribute(IFA_ADDRESS),
527 status.prefix_len);
528
529 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
530 vector<AddressData>::iterator iter;
531 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
532 if (address.Equals(iter->address)) {
533 break;
534 }
535 }
536 if (iter != address_list.end()) {
537 if (msg.mode() == RTNLMessage::kModeDelete) {
538 VLOG(2) << "Delete address for interface " << interface_index;
539 address_list.erase(iter);
540 } else {
541 iter->flags = status.flags;
542 iter->scope = status.scope;
543 }
544 } else if (msg.mode() == RTNLMessage::kModeAdd) {
545 address_list.push_back(AddressData(address, status.flags, status.scope));
546 VLOG(2) << "Add address for interface " << interface_index;
547 }
548}
549
Paul Stewart0af98bf2011-05-10 17:38:08 -0700550} // namespace shill