blob: a424a565767ad895498afc54a1d35e4f756bf1a0 [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 Stewarte81eb702012-04-11 15:04:53 -070062const char DeviceInfo::kLoopbackDeviceName[] = "lo";
63// static
Paul Stewartb50f0b92011-05-16 16:31:42 -070064const char *DeviceInfo::kModemDrivers[] = {
65 "gobi",
66 "QCUSBNet2k",
67 "GobiNet",
Eric Shienbrood39cd5642012-01-19 10:53:52 -050068 "cdc_ether",
Paul Stewartb50f0b92011-05-16 16:31:42 -070069 NULL
70};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080071// static
72const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewartb50f0b92011-05-16 16:31:42 -070073
74DeviceInfo::DeviceInfo(ControlInterface *control_interface,
75 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080076 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070077 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070078 : control_interface_(control_interface),
79 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080080 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070081 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050082 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
83 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070084 link_listener_(NULL),
85 address_listener_(NULL),
86 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070087}
88
Paul Stewarta3c56f92011-05-26 07:08:52 -070089DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070090
mukesh agrawal8f317b62011-07-15 11:53:23 -070091void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
92 black_list_.insert(device_name);
93}
94
Eric Shienbrood5e628a52012-03-21 16:56:59 -040095bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
96 return ContainsKey(black_list_, device_name);
97}
98
Paul Stewarta3c56f92011-05-26 07:08:52 -070099void DeviceInfo::Start() {
100 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500101 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700102 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500103 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700104 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
105 RTNLHandler::kRequestAddr);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700106}
107
Paul Stewarta3c56f92011-05-26 07:08:52 -0700108void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700109 link_listener_.reset();
110 address_listener_.reset();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700111}
112
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700113void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
114 VLOG(2) << __func__ << "(" << device->link_name() << ", "
115 << device->interface_index() << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700116 CHECK(!GetDevice(device->interface_index()).get());
117 infos_[device->interface_index()].device = device;
Paul Stewartfdd16072011-09-16 12:41:35 -0700118 if (device->TechnologyIs(Technology::kCellular) ||
119 device->TechnologyIs(Technology::kEthernet) ||
120 device->TechnologyIs(Technology::kWifi)) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700121 manager_->RegisterDevice(device);
122 }
123}
124
Jason Glasgowe9089492012-02-23 17:57:37 -0500125void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
126 int interface_index = device->interface_index();
127
128 VLOG(2) << __func__ << "(" << device->link_name() << ", "
129 << interface_index << ")";
130 CHECK(device->TechnologyIs(Technology::kCellular));
131
132 // Release reference to the device
133 map<int, Info>::iterator iter = infos_.find(interface_index);
134 if (iter != infos_.end()) {
135 VLOG(2) << "Removing device from info for index: " << interface_index;
136 manager_->DeregisterDevice(device);
137 // Release the reference to the device, but maintain the mapping
138 // for the index. That will be cleaned up by an RTNL message.
139 iter->second.device = NULL;
140 }
141}
142
Paul Stewartfdd16072011-09-16 12:41:35 -0700143Technology::Identifier DeviceInfo::GetDeviceTechnology(
144 const string &iface_name) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800145 FilePath uevent_file(StringPrintf(kInterfaceUevent, iface_name.c_str()));
146 string contents;
147 if (!file_util::ReadFileToString(uevent_file, &contents)) {
148 VLOG(2) << StringPrintf("%s: device %s has no uevent file",
149 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700150 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800151 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700152
153 /*
154 * If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
155 * start of the file or after a newline, we can safely assume this
156 * is a wifi device.
157 */
Paul Stewart9364c4c2011-12-06 17:12:42 -0800158 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
159 VLOG(2) << StringPrintf("%s: device %s has wifi signature in uevent file",
160 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800161 FilePath type_file(StringPrintf(kInterfaceType, iface_name.c_str()));
162 string type_string;
163 int type_val = 0;
164 if (file_util::ReadFileToString(type_file, &type_string) &&
165 TrimString(type_string, "\n", &type_string) &&
166 base::StringToInt(type_string, &type_val) &&
167 type_val == ARPHRD_IEEE80211_RADIOTAP) {
168 VLOG(2) << StringPrintf("%s: wifi device %s is in monitor mode",
169 __func__, iface_name.c_str());
170 return Technology::kWiFiMonitor;
171 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700172 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700173 }
174
Paul Stewart9364c4c2011-12-06 17:12:42 -0800175 FilePath driver_file(StringPrintf(kInterfaceDriver, iface_name.c_str()));
176 FilePath driver_path;
177 if (!file_util::ReadSymbolicLink(driver_file, &driver_path)) {
178 VLOG(2) << StringPrintf("%s: device %s has no device symlink",
179 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700180 if (iface_name == kLoopbackDeviceName) {
181 VLOG(2) << StringPrintf("%s: device %s is a loopback device",
182 __func__, iface_name.c_str());
183 return Technology::kLoopback;
184 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800185 FilePath tun_flags_file(StringPrintf(kInterfaceTunFlags,
186 iface_name.c_str()));
187 string tun_flags_string;
188 int tun_flags = 0;
189 if (file_util::ReadFileToString(tun_flags_file, &tun_flags_string) &&
190 TrimString(tun_flags_string, "\n", &tun_flags_string) &&
191 base::HexStringToInt(tun_flags_string, &tun_flags) &&
192 (tun_flags & IFF_TUN)) {
193 VLOG(2) << StringPrintf("%s: device %s is tun device",
194 __func__, iface_name.c_str());
195 return Technology::kTunnel;
196 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700197 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800198 }
199
200 string driver_name(driver_path.BaseName().value());
201 // See if driver for this interface is in a list of known modem driver names
202 for (int modem_idx = 0; kModemDrivers[modem_idx] != NULL; ++modem_idx) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500203 // TODO(ers): should have additional checks to make sure a cdc_ether
Eric Shienbrood39cd5642012-01-19 10:53:52 -0500204 // device is really a modem. flimflam uses udev to make such checks,
205 // looking to see whether a ttyACM or ttyUSB device is associated.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800206 if (driver_name == kModemDrivers[modem_idx]) {
207 VLOG(2) << StringPrintf("%s: device %s is matched with modem driver %s",
208 __func__, iface_name.c_str(),
209 driver_name.c_str());
210 return Technology::kCellular;
211 }
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
Chris Masone2aa97072011-08-09 17:35:08 -0700219void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700220 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
221 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700222 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700223 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700224
Darin Petkove6193c02011-08-11 12:42:40 -0700225 unsigned int flags = msg.link_status().flags;
226 unsigned int change = msg.link_status().change;
Paul Stewart536820d2012-03-19 16:05:59 -0700227 bool new_device = !ContainsKey(infos_, dev_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700228 VLOG(2) << __func__ << "(index=" << dev_index
mukesh agrawal47009f82011-08-25 14:07:35 -0700229 << std::showbase << std::hex
230 << ", flags=" << flags << ", change=" << change << ")"
Paul Stewart536820d2012-03-19 16:05:59 -0700231 << std::dec << std::noshowbase
232 << ", new_device=" << new_device;
Darin Petkove6193c02011-08-11 12:42:40 -0700233 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700234
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700235 DeviceRefPtr device = GetDevice(dev_index);
236 if (!device.get()) {
Chris Masone2aa97072011-08-09 17:35:08 -0700237 if (!msg.HasAttribute(IFLA_IFNAME)) {
238 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
239 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700240 }
Chris Masone2aa97072011-08-09 17:35:08 -0700241 ByteString b(msg.GetAttribute(IFLA_IFNAME));
242 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Paul Stewarta3c56f92011-05-26 07:08:52 -0700243 VLOG(2) << "add link index " << dev_index << " name " << link_name;
244
Chris Masone2aa97072011-08-09 17:35:08 -0700245 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700246 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700247 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700248 } else {
249 technology = GetDeviceTechnology(link_name);
250 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700251 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800252 string address;
253 if (msg.HasAttribute(IFLA_ADDRESS)) {
254 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
255 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
256 VLOG(2) << "link index " << dev_index << " address "
257 << infos_[dev_index].mac_address.HexEncode();
258 } else if (technology != Technology::kTunnel) {
259 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
260 return;
261 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700262 switch (technology) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700263 case Technology::kCellular:
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700264 // Cellular devices are managed by ModemInfo.
265 VLOG(2) << "Cellular link " << link_name << " at index " << dev_index
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100266 << " -- notifying ModemInfo.";
267 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700268 return;
Paul Stewartfdd16072011-09-16 12:41:35 -0700269 case Technology::kEthernet:
Thieu Le3426c8f2012-01-11 17:35:11 -0800270 device = new Ethernet(control_interface_, dispatcher_, metrics_,
271 manager_, link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800272 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700273 break;
Paul Stewartfdd16072011-09-16 12:41:35 -0700274 case Technology::kWifi:
Thieu Le3426c8f2012-01-11 17:35:11 -0800275 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700276 link_name, address, dev_index);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800277 device->EnableIPv6Privacy();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700278 break;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800279 case Technology::kTunnel:
Paul Stewart536820d2012-03-19 16:05:59 -0700280 // Tunnel devices are managed by the VPN code. Notify the VPN Provider
281 // only if this is the first time we have seen this device index.
282 if (new_device) {
283 VLOG(2) << "Tunnel link " << link_name << " at index " << dev_index
284 << " -- notifying VPNProvider.";
285 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
286 dev_index)) {
287 // If VPN does not know anything about this tunnel, it is probably
288 // left over from a previous instance and should not exist.
289 VLOG(2) << "Tunnel link is unused. Deleting.";
290 DeleteInterface(dev_index);
291 }
Paul Stewartca6abd42012-03-01 15:45:29 -0800292 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800293 return;
Paul Stewarte81eb702012-04-11 15:04:53 -0700294 case Technology::kLoopback:
295 // Loopback devices are largely ignored, but we should make sure the
296 // link is enabled.
297 VLOG(2) << "Bringing up loopback device " << link_name << " at index "
298 << dev_index;
299 rtnl_handler_->SetInterfaceFlags(dev_index, IFF_UP, IFF_UP);
300 return;
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700301 default:
Thieu Le3426c8f2012-01-11 17:35:11 -0800302 device = new DeviceStub(control_interface_, dispatcher_, metrics_,
303 manager_, link_name, address, dev_index,
304 technology);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700305 break;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700306 }
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700307 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700308 }
Darin Petkove6193c02011-08-11 12:42:40 -0700309 device->LinkEvent(flags, change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700310}
311
Chris Masone2aa97072011-08-09 17:35:08 -0700312void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700313 VLOG(2) << __func__ << "(index=" << msg.interface_index() << ")";
314
Paul Stewart9a908082011-08-31 12:18:48 -0700315 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
316 msg.mode() == RTNLMessage::kModeDelete);
Paul Stewart2713d6c2011-08-25 15:38:15 -0700317 VLOG(2) << __func__ << "(index=" << msg.interface_index()
318 << std::showbase << std::hex
319 << ", flags=" << msg.link_status().flags
320 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700321 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700322}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700323
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700324DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
325 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700326 return info ? info->device : NULL;
327}
328
Paul Stewart32852962011-08-30 14:06:53 -0700329bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700330 const Info *info = GetInfo(interface_index);
331 if (!info) {
332 return false;
333 }
Paul Stewart32852962011-08-30 14:06:53 -0700334 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700335 return true;
336}
337
Paul Stewart9a908082011-08-31 12:18:48 -0700338bool DeviceInfo::GetAddresses(int interface_index,
339 vector<AddressData> *addresses) const {
340 const Info *info = GetInfo(interface_index);
341 if (!info) {
342 return false;
343 }
344 *addresses = info->ip_addresses;
345 return true;
346}
347
348void DeviceInfo::FlushAddresses(int interface_index) const {
Paul Stewart7355ce12011-09-02 10:47:01 -0700349 VLOG(2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700350 const Info *info = GetInfo(interface_index);
351 if (!info) {
352 return;
353 }
354 const vector<AddressData> &addresses = info->ip_addresses;
355 vector<AddressData>::const_iterator iter;
356 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700357 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700358 (iter->scope == RT_SCOPE_UNIVERSE &&
359 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
360 VLOG(2) << __func__ << ": removing ip address from interface "
361 << interface_index;
362 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
363 }
364 }
365}
366
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700367bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
368 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700369 if (!info) {
370 return false;
371 }
372 *flags = info->flags;
373 return true;
374}
375
Paul Stewartca6abd42012-03-01 15:45:29 -0800376bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800377 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
378 if (fd < 0) {
379 PLOG(ERROR) << "failed to open " << kTunDeviceName;
380 return false;
381 }
382 file_util::ScopedFD scoped_fd(&fd);
383
384 struct ifreq ifr;
385 memset(&ifr, 0, sizeof(ifr));
386 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
387 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
388 PLOG(ERROR) << "failed to create tunnel interface";
389 return false;
390 }
391
392 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
393 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
394 return false;
395 }
396
397 *interface_name = string(ifr.ifr_name);
398
399 return true;
400}
401
Paul Stewartca6abd42012-03-01 15:45:29 -0800402bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800403 return rtnl_handler_->RemoveInterface(interface_index);
404}
405
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700406const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
407 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700408 if (iter == infos_.end()) {
409 return NULL;
410 }
411 return &iter->second;
412}
413
414void DeviceInfo::RemoveInfo(int interface_index) {
415 map<int, Info>::iterator iter = infos_.find(interface_index);
416 if (iter != infos_.end()) {
417 VLOG(2) << "Removing info for device index: " << interface_index;
418 if (iter->second.device.get()) {
419 manager_->DeregisterDevice(iter->second.device);
420 }
421 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700422 } else {
423 VLOG(2) << __func__ << "unknown device index: " << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700424 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700425}
426
Chris Masone2aa97072011-08-09 17:35:08 -0700427void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700428 DCHECK(msg.type() == RTNLMessage::kTypeLink);
429 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700430 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700431 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700432 DelLinkMsgHandler(msg);
433 } else {
434 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700435 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700436}
437
Paul Stewart9a908082011-08-31 12:18:48 -0700438void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700439 VLOG(2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700440 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
441 int interface_index = msg.interface_index();
442 if (!ContainsKey(infos_, interface_index)) {
443 LOG(ERROR) << "Got address type message for unknown index "
444 << interface_index;
445 return;
446 }
447 const RTNLMessage::AddressStatus &status = msg.address_status();
448 IPAddress address(msg.family(),
449 msg.GetAttribute(IFA_ADDRESS),
450 status.prefix_len);
451
452 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
453 vector<AddressData>::iterator iter;
454 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
455 if (address.Equals(iter->address)) {
456 break;
457 }
458 }
459 if (iter != address_list.end()) {
460 if (msg.mode() == RTNLMessage::kModeDelete) {
461 VLOG(2) << "Delete address for interface " << interface_index;
462 address_list.erase(iter);
463 } else {
464 iter->flags = status.flags;
465 iter->scope = status.scope;
466 }
467 } else if (msg.mode() == RTNLMessage::kModeAdd) {
468 address_list.push_back(AddressData(address, status.flags, status.scope));
469 VLOG(2) << "Add address for interface " << interface_index;
470 }
471}
472
Paul Stewart0af98bf2011-05-10 17:38:08 -0700473} // namespace shill