blob: 366f74fac0e46367451d8d66e88650219673bfec [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
Eric Shienbrood5e628a52012-03-21 16:56:59 -040093bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
94 return ContainsKey(black_list_, device_name);
95}
96
Paul Stewarta3c56f92011-05-26 07:08:52 -070097void DeviceInfo::Start() {
98 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -050099 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700100 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500101 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700102 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
103 RTNLHandler::kRequestAddr);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700104}
105
Paul Stewarta3c56f92011-05-26 07:08:52 -0700106void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700107 link_listener_.reset();
108 address_listener_.reset();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700109}
110
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700111void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
112 VLOG(2) << __func__ << "(" << device->link_name() << ", "
113 << device->interface_index() << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700114 CHECK(!GetDevice(device->interface_index()).get());
115 infos_[device->interface_index()].device = device;
Paul Stewartfdd16072011-09-16 12:41:35 -0700116 if (device->TechnologyIs(Technology::kCellular) ||
117 device->TechnologyIs(Technology::kEthernet) ||
118 device->TechnologyIs(Technology::kWifi)) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700119 manager_->RegisterDevice(device);
120 }
121}
122
Jason Glasgowe9089492012-02-23 17:57:37 -0500123void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
124 int interface_index = device->interface_index();
125
126 VLOG(2) << __func__ << "(" << device->link_name() << ", "
127 << interface_index << ")";
128 CHECK(device->TechnologyIs(Technology::kCellular));
129
130 // Release reference to the device
131 map<int, Info>::iterator iter = infos_.find(interface_index);
132 if (iter != infos_.end()) {
133 VLOG(2) << "Removing device from info for index: " << interface_index;
134 manager_->DeregisterDevice(device);
135 // Release the reference to the device, but maintain the mapping
136 // for the index. That will be cleaned up by an RTNL message.
137 iter->second.device = NULL;
138 }
139}
140
Paul Stewartfdd16072011-09-16 12:41:35 -0700141Technology::Identifier DeviceInfo::GetDeviceTechnology(
142 const string &iface_name) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800143 FilePath uevent_file(StringPrintf(kInterfaceUevent, iface_name.c_str()));
144 string contents;
145 if (!file_util::ReadFileToString(uevent_file, &contents)) {
146 VLOG(2) << StringPrintf("%s: device %s has no uevent file",
147 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700148 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800149 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700150
151 /*
152 * If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
153 * start of the file or after a newline, we can safely assume this
154 * is a wifi device.
155 */
Paul Stewart9364c4c2011-12-06 17:12:42 -0800156 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
157 VLOG(2) << StringPrintf("%s: device %s has wifi signature in uevent file",
158 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800159 FilePath type_file(StringPrintf(kInterfaceType, iface_name.c_str()));
160 string type_string;
161 int type_val = 0;
162 if (file_util::ReadFileToString(type_file, &type_string) &&
163 TrimString(type_string, "\n", &type_string) &&
164 base::StringToInt(type_string, &type_val) &&
165 type_val == ARPHRD_IEEE80211_RADIOTAP) {
166 VLOG(2) << StringPrintf("%s: wifi device %s is in monitor mode",
167 __func__, iface_name.c_str());
168 return Technology::kWiFiMonitor;
169 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700170 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700171 }
172
Paul Stewart9364c4c2011-12-06 17:12:42 -0800173 FilePath driver_file(StringPrintf(kInterfaceDriver, iface_name.c_str()));
174 FilePath driver_path;
175 if (!file_util::ReadSymbolicLink(driver_file, &driver_path)) {
176 VLOG(2) << StringPrintf("%s: device %s has no device symlink",
177 __func__, iface_name.c_str());
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 }
190 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800191 }
192
193 string driver_name(driver_path.BaseName().value());
194 // See if driver for this interface is in a list of known modem driver names
195 for (int modem_idx = 0; kModemDrivers[modem_idx] != NULL; ++modem_idx) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500196 // TODO(ers): should have additional checks to make sure a cdc_ether
Eric Shienbrood39cd5642012-01-19 10:53:52 -0500197 // device is really a modem. flimflam uses udev to make such checks,
198 // looking to see whether a ttyACM or ttyUSB device is associated.
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
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800207 VLOG(2) << StringPrintf("%s: device %s is defaulted to type ethernet",
Paul Stewart9364c4c2011-12-06 17:12:42 -0800208 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700209 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700210}
211
Chris Masone2aa97072011-08-09 17:35:08 -0700212void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700213 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
214 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700215 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700216 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700217
Darin Petkove6193c02011-08-11 12:42:40 -0700218 unsigned int flags = msg.link_status().flags;
219 unsigned int change = msg.link_status().change;
Paul Stewart536820d2012-03-19 16:05:59 -0700220 bool new_device = !ContainsKey(infos_, dev_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700221 VLOG(2) << __func__ << "(index=" << dev_index
mukesh agrawal47009f82011-08-25 14:07:35 -0700222 << std::showbase << std::hex
223 << ", flags=" << flags << ", change=" << change << ")"
Paul Stewart536820d2012-03-19 16:05:59 -0700224 << std::dec << std::noshowbase
225 << ", new_device=" << new_device;
Darin Petkove6193c02011-08-11 12:42:40 -0700226 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700227
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700228 DeviceRefPtr device = GetDevice(dev_index);
229 if (!device.get()) {
Chris Masone2aa97072011-08-09 17:35:08 -0700230 if (!msg.HasAttribute(IFLA_IFNAME)) {
231 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
232 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700233 }
Chris Masone2aa97072011-08-09 17:35:08 -0700234 ByteString b(msg.GetAttribute(IFLA_IFNAME));
235 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Paul Stewarta3c56f92011-05-26 07:08:52 -0700236 VLOG(2) << "add link index " << dev_index << " name " << link_name;
237
Chris Masone2aa97072011-08-09 17:35:08 -0700238 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700239 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700240 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700241 } else {
242 technology = GetDeviceTechnology(link_name);
243 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700244 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800245 string address;
246 if (msg.HasAttribute(IFLA_ADDRESS)) {
247 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
248 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
249 VLOG(2) << "link index " << dev_index << " address "
250 << infos_[dev_index].mac_address.HexEncode();
251 } else if (technology != Technology::kTunnel) {
252 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
253 return;
254 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700255 switch (technology) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700256 case Technology::kCellular:
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700257 // Cellular devices are managed by ModemInfo.
258 VLOG(2) << "Cellular link " << link_name << " at index " << dev_index
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100259 << " -- notifying ModemInfo.";
260 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700261 return;
Paul Stewartfdd16072011-09-16 12:41:35 -0700262 case Technology::kEthernet:
Thieu Le3426c8f2012-01-11 17:35:11 -0800263 device = new Ethernet(control_interface_, dispatcher_, metrics_,
264 manager_, link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800265 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700266 break;
Paul Stewartfdd16072011-09-16 12:41:35 -0700267 case Technology::kWifi:
Thieu Le3426c8f2012-01-11 17:35:11 -0800268 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700269 link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800270 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700271 break;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800272 case Technology::kTunnel:
Paul Stewart536820d2012-03-19 16:05:59 -0700273 // Tunnel devices are managed by the VPN code. Notify the VPN Provider
274 // only if this is the first time we have seen this device index.
275 if (new_device) {
276 VLOG(2) << "Tunnel link " << link_name << " at index " << dev_index
277 << " -- notifying VPNProvider.";
278 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
279 dev_index)) {
280 // If VPN does not know anything about this tunnel, it is probably
281 // left over from a previous instance and should not exist.
282 VLOG(2) << "Tunnel link is unused. Deleting.";
283 DeleteInterface(dev_index);
284 }
Paul Stewartca6abd42012-03-01 15:45:29 -0800285 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800286 return;
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700287 default:
Thieu Le3426c8f2012-01-11 17:35:11 -0800288 device = new DeviceStub(control_interface_, dispatcher_, metrics_,
289 manager_, link_name, address, dev_index,
290 technology);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700291 break;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700292 }
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700293 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700294 }
Darin Petkove6193c02011-08-11 12:42:40 -0700295 device->LinkEvent(flags, change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700296}
297
Chris Masone2aa97072011-08-09 17:35:08 -0700298void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700299 VLOG(2) << __func__ << "(index=" << msg.interface_index() << ")";
300
Paul Stewart9a908082011-08-31 12:18:48 -0700301 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
302 msg.mode() == RTNLMessage::kModeDelete);
Paul Stewart2713d6c2011-08-25 15:38:15 -0700303 VLOG(2) << __func__ << "(index=" << msg.interface_index()
304 << std::showbase << std::hex
305 << ", flags=" << msg.link_status().flags
306 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700307 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700308}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700309
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700310DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
311 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700312 return info ? info->device : NULL;
313}
314
Paul Stewart32852962011-08-30 14:06:53 -0700315bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700316 const Info *info = GetInfo(interface_index);
317 if (!info) {
318 return false;
319 }
Paul Stewart32852962011-08-30 14:06:53 -0700320 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700321 return true;
322}
323
Paul Stewart9a908082011-08-31 12:18:48 -0700324bool DeviceInfo::GetAddresses(int interface_index,
325 vector<AddressData> *addresses) const {
326 const Info *info = GetInfo(interface_index);
327 if (!info) {
328 return false;
329 }
330 *addresses = info->ip_addresses;
331 return true;
332}
333
334void DeviceInfo::FlushAddresses(int interface_index) const {
Paul Stewart7355ce12011-09-02 10:47:01 -0700335 VLOG(2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700336 const Info *info = GetInfo(interface_index);
337 if (!info) {
338 return;
339 }
340 const vector<AddressData> &addresses = info->ip_addresses;
341 vector<AddressData>::const_iterator iter;
342 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700343 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700344 (iter->scope == RT_SCOPE_UNIVERSE &&
345 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
346 VLOG(2) << __func__ << ": removing ip address from interface "
347 << interface_index;
348 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
349 }
350 }
351}
352
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700353bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
354 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700355 if (!info) {
356 return false;
357 }
358 *flags = info->flags;
359 return true;
360}
361
Paul Stewartca6abd42012-03-01 15:45:29 -0800362bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800363 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
364 if (fd < 0) {
365 PLOG(ERROR) << "failed to open " << kTunDeviceName;
366 return false;
367 }
368 file_util::ScopedFD scoped_fd(&fd);
369
370 struct ifreq ifr;
371 memset(&ifr, 0, sizeof(ifr));
372 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
373 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
374 PLOG(ERROR) << "failed to create tunnel interface";
375 return false;
376 }
377
378 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
379 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
380 return false;
381 }
382
383 *interface_name = string(ifr.ifr_name);
384
385 return true;
386}
387
Paul Stewartca6abd42012-03-01 15:45:29 -0800388bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800389 return rtnl_handler_->RemoveInterface(interface_index);
390}
391
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700392const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
393 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700394 if (iter == infos_.end()) {
395 return NULL;
396 }
397 return &iter->second;
398}
399
400void DeviceInfo::RemoveInfo(int interface_index) {
401 map<int, Info>::iterator iter = infos_.find(interface_index);
402 if (iter != infos_.end()) {
403 VLOG(2) << "Removing info for device index: " << interface_index;
404 if (iter->second.device.get()) {
405 manager_->DeregisterDevice(iter->second.device);
406 }
407 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700408 } else {
409 VLOG(2) << __func__ << "unknown device index: " << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700410 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700411}
412
Chris Masone2aa97072011-08-09 17:35:08 -0700413void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700414 DCHECK(msg.type() == RTNLMessage::kTypeLink);
415 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700416 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700417 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700418 DelLinkMsgHandler(msg);
419 } else {
420 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700421 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700422}
423
Paul Stewart9a908082011-08-31 12:18:48 -0700424void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700425 VLOG(2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700426 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
427 int interface_index = msg.interface_index();
428 if (!ContainsKey(infos_, interface_index)) {
429 LOG(ERROR) << "Got address type message for unknown index "
430 << interface_index;
431 return;
432 }
433 const RTNLMessage::AddressStatus &status = msg.address_status();
434 IPAddress address(msg.family(),
435 msg.GetAttribute(IFA_ADDRESS),
436 status.prefix_len);
437
438 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
439 vector<AddressData>::iterator iter;
440 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
441 if (address.Equals(iter->address)) {
442 break;
443 }
444 }
445 if (iter != address_list.end()) {
446 if (msg.mode() == RTNLMessage::kModeDelete) {
447 VLOG(2) << "Delete address for interface " << interface_index;
448 address_list.erase(iter);
449 } else {
450 iter->flags = status.flags;
451 iter->scope = status.scope;
452 }
453 } else if (msg.mode() == RTNLMessage::kModeAdd) {
454 address_list.push_back(AddressData(address, status.flags, status.scope));
455 VLOG(2) << "Add address for interface " << interface_index;
456 }
457}
458
Paul Stewart0af98bf2011-05-10 17:38:08 -0700459} // namespace shill