blob: ab93828263f2e64c9ff0736d5ace4d154e6cdc66 [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;
44using base::Unretained;
Darin Petkove6193c02011-08-11 12:42:40 -070045using std::map;
Paul Stewart0af98bf2011-05-10 17:38:08 -070046using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070047using std::vector;
Paul Stewart0af98bf2011-05-10 17:38:08 -070048
49namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070050
51// static
Paul Stewartbf1861b2011-08-23 15:45:35 -070052const char DeviceInfo::kInterfaceUevent[] = "/sys/class/net/%s/uevent";
Paul Stewartb50f0b92011-05-16 16:31:42 -070053// static
Paul Stewart9364c4c2011-12-06 17:12:42 -080054const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
55// static
Paul Stewartbf1861b2011-08-23 15:45:35 -070056const char DeviceInfo::kInterfaceDriver[] = "/sys/class/net/%s/device/driver";
Paul Stewartb50f0b92011-05-16 16:31:42 -070057// static
Paul Stewartcba0f7f2012-02-29 16:33:05 -080058const char DeviceInfo::kInterfaceTunFlags[] = "/sys/class/net/%s/tun_flags";
59// static
Paul Stewart2001a422011-12-15 10:20:09 -080060const char DeviceInfo::kInterfaceType[] = "/sys/class/net/%s/type";
61// static
Paul Stewartb50f0b92011-05-16 16:31:42 -070062const char *DeviceInfo::kModemDrivers[] = {
63 "gobi",
64 "QCUSBNet2k",
65 "GobiNet",
Eric Shienbrood39cd5642012-01-19 10:53:52 -050066 "cdc_ether",
Paul Stewartb50f0b92011-05-16 16:31:42 -070067 NULL
68};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080069// static
70const 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),
84 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070085}
86
Paul Stewarta3c56f92011-05-26 07:08:52 -070087DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070088
mukesh agrawal8f317b62011-07-15 11:53:23 -070089void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
90 black_list_.insert(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
Paul Stewartfdd16072011-09-16 12:41:35 -0700137Technology::Identifier DeviceInfo::GetDeviceTechnology(
138 const string &iface_name) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800139 FilePath uevent_file(StringPrintf(kInterfaceUevent, iface_name.c_str()));
140 string contents;
141 if (!file_util::ReadFileToString(uevent_file, &contents)) {
142 VLOG(2) << StringPrintf("%s: device %s has no uevent file",
143 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700144 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800145 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700146
147 /*
148 * 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.
151 */
Paul Stewart9364c4c2011-12-06 17:12:42 -0800152 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
153 VLOG(2) << StringPrintf("%s: device %s has wifi signature in uevent file",
154 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800155 FilePath type_file(StringPrintf(kInterfaceType, iface_name.c_str()));
156 string type_string;
157 int type_val = 0;
158 if (file_util::ReadFileToString(type_file, &type_string) &&
159 TrimString(type_string, "\n", &type_string) &&
160 base::StringToInt(type_string, &type_val) &&
161 type_val == ARPHRD_IEEE80211_RADIOTAP) {
162 VLOG(2) << StringPrintf("%s: wifi device %s is in monitor mode",
163 __func__, iface_name.c_str());
164 return Technology::kWiFiMonitor;
165 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700166 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700167 }
168
Paul Stewart9364c4c2011-12-06 17:12:42 -0800169 FilePath driver_file(StringPrintf(kInterfaceDriver, iface_name.c_str()));
170 FilePath driver_path;
171 if (!file_util::ReadSymbolicLink(driver_file, &driver_path)) {
172 VLOG(2) << StringPrintf("%s: device %s has no device symlink",
173 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800174 FilePath tun_flags_file(StringPrintf(kInterfaceTunFlags,
175 iface_name.c_str()));
176 string tun_flags_string;
177 int tun_flags = 0;
178 if (file_util::ReadFileToString(tun_flags_file, &tun_flags_string) &&
179 TrimString(tun_flags_string, "\n", &tun_flags_string) &&
180 base::HexStringToInt(tun_flags_string, &tun_flags) &&
181 (tun_flags & IFF_TUN)) {
182 VLOG(2) << StringPrintf("%s: device %s is tun device",
183 __func__, iface_name.c_str());
184 return Technology::kTunnel;
185 }
186 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800187 }
188
189 string driver_name(driver_path.BaseName().value());
190 // See if driver for this interface is in a list of known modem driver names
191 for (int modem_idx = 0; kModemDrivers[modem_idx] != NULL; ++modem_idx) {
Eric Shienbrood39cd5642012-01-19 10:53:52 -0500192 // TODO(ers) should have additional checks to make sure a cdc_ether
193 // device is really a modem. flimflam uses udev to make such checks,
194 // looking to see whether a ttyACM or ttyUSB device is associated.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800195 if (driver_name == kModemDrivers[modem_idx]) {
196 VLOG(2) << StringPrintf("%s: device %s is matched with modem driver %s",
197 __func__, iface_name.c_str(),
198 driver_name.c_str());
199 return Technology::kCellular;
200 }
201 }
202
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800203 VLOG(2) << StringPrintf("%s: device %s is defaulted to type ethernet",
Paul Stewart9364c4c2011-12-06 17:12:42 -0800204 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700205 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700206}
207
Chris Masone2aa97072011-08-09 17:35:08 -0700208void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700209 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
210 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700211 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700212 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700213
Darin Petkove6193c02011-08-11 12:42:40 -0700214 unsigned int flags = msg.link_status().flags;
215 unsigned int change = msg.link_status().change;
Paul Stewart536820d2012-03-19 16:05:59 -0700216 bool new_device = !ContainsKey(infos_, dev_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700217 VLOG(2) << __func__ << "(index=" << dev_index
mukesh agrawal47009f82011-08-25 14:07:35 -0700218 << std::showbase << std::hex
219 << ", flags=" << flags << ", change=" << change << ")"
Paul Stewart536820d2012-03-19 16:05:59 -0700220 << std::dec << std::noshowbase
221 << ", new_device=" << new_device;
Darin Petkove6193c02011-08-11 12:42:40 -0700222 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700223
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700224 DeviceRefPtr device = GetDevice(dev_index);
225 if (!device.get()) {
Chris Masone2aa97072011-08-09 17:35:08 -0700226 if (!msg.HasAttribute(IFLA_IFNAME)) {
227 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
228 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700229 }
Chris Masone2aa97072011-08-09 17:35:08 -0700230 ByteString b(msg.GetAttribute(IFLA_IFNAME));
231 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Paul Stewarta3c56f92011-05-26 07:08:52 -0700232 VLOG(2) << "add link index " << dev_index << " name " << link_name;
233
Chris Masone2aa97072011-08-09 17:35:08 -0700234 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700235 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700236 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700237 } else {
238 technology = GetDeviceTechnology(link_name);
239 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700240 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800241 string address;
242 if (msg.HasAttribute(IFLA_ADDRESS)) {
243 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
244 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
245 VLOG(2) << "link index " << dev_index << " address "
246 << infos_[dev_index].mac_address.HexEncode();
247 } else if (technology != Technology::kTunnel) {
248 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
249 return;
250 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700251 switch (technology) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700252 case Technology::kCellular:
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700253 // Cellular devices are managed by ModemInfo.
254 VLOG(2) << "Cellular link " << link_name << " at index " << dev_index
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100255 << " -- notifying ModemInfo.";
256 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700257 return;
Paul Stewartfdd16072011-09-16 12:41:35 -0700258 case Technology::kEthernet:
Thieu Le3426c8f2012-01-11 17:35:11 -0800259 device = new Ethernet(control_interface_, dispatcher_, metrics_,
260 manager_, link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800261 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700262 break;
Paul Stewartfdd16072011-09-16 12:41:35 -0700263 case Technology::kWifi:
Thieu Le3426c8f2012-01-11 17:35:11 -0800264 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700265 link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800266 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700267 break;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800268 case Technology::kTunnel:
Paul Stewart536820d2012-03-19 16:05:59 -0700269 // Tunnel devices are managed by the VPN code. Notify the VPN Provider
270 // only if this is the first time we have seen this device index.
271 if (new_device) {
272 VLOG(2) << "Tunnel link " << link_name << " at index " << dev_index
273 << " -- notifying VPNProvider.";
274 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
275 dev_index)) {
276 // If VPN does not know anything about this tunnel, it is probably
277 // left over from a previous instance and should not exist.
278 VLOG(2) << "Tunnel link is unused. Deleting.";
279 DeleteInterface(dev_index);
280 }
Paul Stewartca6abd42012-03-01 15:45:29 -0800281 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800282 return;
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700283 default:
Thieu Le3426c8f2012-01-11 17:35:11 -0800284 device = new DeviceStub(control_interface_, dispatcher_, metrics_,
285 manager_, link_name, address, dev_index,
286 technology);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700287 break;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700288 }
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700289 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700290 }
Darin Petkove6193c02011-08-11 12:42:40 -0700291 device->LinkEvent(flags, change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700292}
293
Chris Masone2aa97072011-08-09 17:35:08 -0700294void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700295 VLOG(2) << __func__ << "(index=" << msg.interface_index() << ")";
296
Paul Stewart9a908082011-08-31 12:18:48 -0700297 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
298 msg.mode() == RTNLMessage::kModeDelete);
Paul Stewart2713d6c2011-08-25 15:38:15 -0700299 VLOG(2) << __func__ << "(index=" << msg.interface_index()
300 << std::showbase << std::hex
301 << ", flags=" << msg.link_status().flags
302 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700303 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700304}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700305
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700306DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
307 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700308 return info ? info->device : NULL;
309}
310
Paul Stewart32852962011-08-30 14:06:53 -0700311bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700312 const Info *info = GetInfo(interface_index);
313 if (!info) {
314 return false;
315 }
Paul Stewart32852962011-08-30 14:06:53 -0700316 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700317 return true;
318}
319
Paul Stewart9a908082011-08-31 12:18:48 -0700320bool DeviceInfo::GetAddresses(int interface_index,
321 vector<AddressData> *addresses) const {
322 const Info *info = GetInfo(interface_index);
323 if (!info) {
324 return false;
325 }
326 *addresses = info->ip_addresses;
327 return true;
328}
329
330void DeviceInfo::FlushAddresses(int interface_index) const {
Paul Stewart7355ce12011-09-02 10:47:01 -0700331 VLOG(2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700332 const Info *info = GetInfo(interface_index);
333 if (!info) {
334 return;
335 }
336 const vector<AddressData> &addresses = info->ip_addresses;
337 vector<AddressData>::const_iterator iter;
338 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700339 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700340 (iter->scope == RT_SCOPE_UNIVERSE &&
341 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
342 VLOG(2) << __func__ << ": removing ip address from interface "
343 << interface_index;
344 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
345 }
346 }
347}
348
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700349bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
350 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700351 if (!info) {
352 return false;
353 }
354 *flags = info->flags;
355 return true;
356}
357
Paul Stewartca6abd42012-03-01 15:45:29 -0800358bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800359 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
360 if (fd < 0) {
361 PLOG(ERROR) << "failed to open " << kTunDeviceName;
362 return false;
363 }
364 file_util::ScopedFD scoped_fd(&fd);
365
366 struct ifreq ifr;
367 memset(&ifr, 0, sizeof(ifr));
368 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
369 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
370 PLOG(ERROR) << "failed to create tunnel interface";
371 return false;
372 }
373
374 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
375 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
376 return false;
377 }
378
379 *interface_name = string(ifr.ifr_name);
380
381 return true;
382}
383
Paul Stewartca6abd42012-03-01 15:45:29 -0800384bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800385 return rtnl_handler_->RemoveInterface(interface_index);
386}
387
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700388const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
389 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700390 if (iter == infos_.end()) {
391 return NULL;
392 }
393 return &iter->second;
394}
395
396void DeviceInfo::RemoveInfo(int interface_index) {
397 map<int, Info>::iterator iter = infos_.find(interface_index);
398 if (iter != infos_.end()) {
399 VLOG(2) << "Removing info for device index: " << interface_index;
400 if (iter->second.device.get()) {
401 manager_->DeregisterDevice(iter->second.device);
402 }
403 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700404 } else {
405 VLOG(2) << __func__ << "unknown device index: " << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700406 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700407}
408
Chris Masone2aa97072011-08-09 17:35:08 -0700409void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700410 DCHECK(msg.type() == RTNLMessage::kTypeLink);
411 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700412 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700413 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700414 DelLinkMsgHandler(msg);
415 } else {
416 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700417 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700418}
419
Paul Stewart9a908082011-08-31 12:18:48 -0700420void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700421 VLOG(2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700422 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
423 int interface_index = msg.interface_index();
424 if (!ContainsKey(infos_, interface_index)) {
425 LOG(ERROR) << "Got address type message for unknown index "
426 << interface_index;
427 return;
428 }
429 const RTNLMessage::AddressStatus &status = msg.address_status();
430 IPAddress address(msg.family(),
431 msg.GetAttribute(IFA_ADDRESS),
432 status.prefix_len);
433
434 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
435 vector<AddressData>::iterator iter;
436 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
437 if (address.Equals(iter->address)) {
438 break;
439 }
440 }
441 if (iter != address_list.end()) {
442 if (msg.mode() == RTNLMessage::kModeDelete) {
443 VLOG(2) << "Delete address for interface " << interface_index;
444 address_list.erase(iter);
445 } else {
446 iter->flags = status.flags;
447 iter->scope = status.scope;
448 }
449 } else if (msg.mode() == RTNLMessage::kModeAdd) {
450 address_list.push_back(AddressData(address, status.flags, status.scope));
451 VLOG(2) << "Add address for interface " << interface_index;
452 }
453}
454
Paul Stewart0af98bf2011-05-10 17:38:08 -0700455} // namespace shill