blob: 3517a969f7fe52d02cd6edd73598f95296df0938 [file] [log] [blame]
Paul Stewart0af98bf2011-05-10 17:38:08 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <time.h>
6
7#include <unistd.h>
8#include <string.h>
9#include <sys/socket.h>
10#include <arpa/inet.h>
11#include <netinet/ether.h>
12#include <net/if.h>
13#include <net/if_arp.h>
14#include <linux/netlink.h>
15#include <linux/rtnetlink.h>
Paul Stewartb50f0b92011-05-16 16:31:42 -070016#include <fcntl.h>
Paul Stewart0af98bf2011-05-10 17:38:08 -070017#include <string>
18
Chris Masone487b8bf2011-05-13 16:27:57 -070019#include <base/callback_old.h>
Paul Stewartbf1861b2011-08-23 15:45:35 -070020#include <base/file_util.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070021#include <base/logging.h>
22#include <base/memory/scoped_ptr.h>
mukesh agrawal8f317b62011-07-15 11:53:23 -070023#include <base/stl_util-inl.h>
Chris Masone877ff982011-09-21 16:18:24 -070024#include <base/string_util.h>
Paul Stewartb50f0b92011-05-16 16:31:42 -070025#include <base/stringprintf.h>
Paul Stewart0af98bf2011-05-10 17:38:08 -070026
27#include "shill/control_interface.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070028#include "shill/device.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070029#include "shill/device_info.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070030#include "shill/device_stub.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070031#include "shill/ethernet.h"
32#include "shill/manager.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070033#include "shill/rtnl_handler.h"
34#include "shill/rtnl_listener.h"
Chris Masone2aa97072011-08-09 17:35:08 -070035#include "shill/rtnl_message.h"
Chris Masone487b8bf2011-05-13 16:27:57 -070036#include "shill/service.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070037#include "shill/wifi.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070038
Darin Petkove6193c02011-08-11 12:42:40 -070039using std::map;
Paul Stewart0af98bf2011-05-10 17:38:08 -070040using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070041using std::vector;
Paul Stewart0af98bf2011-05-10 17:38:08 -070042
43namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070044
45// static
Paul Stewartbf1861b2011-08-23 15:45:35 -070046const char DeviceInfo::kInterfaceUevent[] = "/sys/class/net/%s/uevent";
Paul Stewartb50f0b92011-05-16 16:31:42 -070047// static
Paul Stewartbf1861b2011-08-23 15:45:35 -070048const char DeviceInfo::kInterfaceDriver[] = "/sys/class/net/%s/device/driver";
Paul Stewartb50f0b92011-05-16 16:31:42 -070049// static
50const char *DeviceInfo::kModemDrivers[] = {
51 "gobi",
52 "QCUSBNet2k",
53 "GobiNet",
54 NULL
55};
Paul Stewartbf1861b2011-08-23 15:45:35 -070056// static
57const char DeviceInfo::kInterfaceIPv6Privacy[] =
58 "/proc/sys/net/ipv6/conf/%s/use_tempaddr";
Paul Stewartb50f0b92011-05-16 16:31:42 -070059
60
61DeviceInfo::DeviceInfo(ControlInterface *control_interface,
62 EventDispatcher *dispatcher,
63 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070064 : control_interface_(control_interface),
65 dispatcher_(dispatcher),
66 manager_(manager),
67 link_callback_(NewCallback(this, &DeviceInfo::LinkMsgHandler)),
Paul Stewart9a908082011-08-31 12:18:48 -070068 address_callback_(NewCallback(this, &DeviceInfo::AddressMsgHandler)),
69 link_listener_(NULL),
70 address_listener_(NULL),
71 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070072}
73
Paul Stewarta3c56f92011-05-26 07:08:52 -070074DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070075
mukesh agrawal8f317b62011-07-15 11:53:23 -070076void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
77 black_list_.insert(device_name);
78}
79
Paul Stewarta3c56f92011-05-26 07:08:52 -070080void DeviceInfo::Start() {
81 link_listener_.reset(
82 new RTNLListener(RTNLHandler::kRequestLink, link_callback_.get()));
Paul Stewart9a908082011-08-31 12:18:48 -070083 address_listener_.reset(
84 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_.get()));
85 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
86 RTNLHandler::kRequestAddr);
Paul Stewart0af98bf2011-05-10 17:38:08 -070087}
88
Paul Stewarta3c56f92011-05-26 07:08:52 -070089void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -070090 link_listener_.reset();
91 address_listener_.reset();
Paul Stewart0af98bf2011-05-10 17:38:08 -070092}
93
Darin Petkov6f9eaa32011-08-09 15:26:44 -070094void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
95 VLOG(2) << __func__ << "(" << device->link_name() << ", "
96 << device->interface_index() << ")";
Darin Petkove6193c02011-08-11 12:42:40 -070097 CHECK(!GetDevice(device->interface_index()).get());
98 infos_[device->interface_index()].device = device;
Darin Petkov6f9eaa32011-08-09 15:26:44 -070099 if (device->TechnologyIs(Device::kCellular) ||
100 device->TechnologyIs(Device::kEthernet) ||
101 device->TechnologyIs(Device::kWifi)) {
102 manager_->RegisterDevice(device);
103 }
104}
105
Chris Masone2aa97072011-08-09 17:35:08 -0700106Device::Technology DeviceInfo::GetDeviceTechnology(const string &iface_name) {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700107 char contents[1024];
108 int length;
109 int fd;
Chris Masone2aa97072011-08-09 17:35:08 -0700110 string uevent_file = StringPrintf(kInterfaceUevent, iface_name.c_str());
111 string driver_file = StringPrintf(kInterfaceDriver, iface_name.c_str());
Paul Stewartb50f0b92011-05-16 16:31:42 -0700112 const char *wifi_type;
113 const char *driver_name;
114 int modem_idx;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700115
Paul Stewartb50f0b92011-05-16 16:31:42 -0700116 fd = open(uevent_file.c_str(), O_RDONLY);
117 if (fd < 0)
118 return Device::kUnknown;
119
120 length = read(fd, contents, sizeof(contents) - 1);
121 if (length < 0)
122 return Device::kUnknown;
123
124 /*
125 * If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
126 * start of the file or after a newline, we can safely assume this
127 * is a wifi device.
128 */
129 contents[length] = '\0';
130 wifi_type = strstr(contents, "DEVTYPE=wlan\n");
131 if (wifi_type != NULL && (wifi_type == contents || wifi_type[-1] == '\n'))
132 return Device::kWifi;
133
134 length = readlink(driver_file.c_str(), contents, sizeof(contents)-1);
135 if (length < 0)
136 return Device::kUnknown;
137
138 contents[length] = '\0';
139 driver_name = strrchr(contents, '/');
140 if (driver_name != NULL) {
141 driver_name++;
142 // See if driver for this interface is in a list of known modem driver names
143 for (modem_idx = 0; kModemDrivers[modem_idx] != NULL; modem_idx++)
144 if (strcmp(driver_name, kModemDrivers[modem_idx]) == 0)
145 return Device::kCellular;
146 }
147
148 return Device::kEthernet;
149}
150
Chris Masone2aa97072011-08-09 17:35:08 -0700151void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700152 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
153 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700154 int dev_index = msg.interface_index();
mukesh agrawalf60e4062011-05-27 13:13:41 -0700155 Device::Technology technology = Device::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700156
Darin Petkove6193c02011-08-11 12:42:40 -0700157 unsigned int flags = msg.link_status().flags;
158 unsigned int change = msg.link_status().change;
159 VLOG(2) << __func__ << "(index=" << dev_index
mukesh agrawal47009f82011-08-25 14:07:35 -0700160 << std::showbase << std::hex
161 << ", flags=" << flags << ", change=" << change << ")"
162 << std::dec << std::noshowbase;
Darin Petkove6193c02011-08-11 12:42:40 -0700163 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700164
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700165 DeviceRefPtr device = GetDevice(dev_index);
166 if (!device.get()) {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700167 if (msg.HasAttribute(IFLA_ADDRESS)) {
Paul Stewart32852962011-08-30 14:06:53 -0700168 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700169 VLOG(2) << "link index " << dev_index << " address "
Paul Stewart32852962011-08-30 14:06:53 -0700170 << infos_[dev_index].mac_address.HexEncode();
Chris Masone626719f2011-08-18 16:58:48 -0700171 } else {
172 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
173 return;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700174 }
Chris Masone2aa97072011-08-09 17:35:08 -0700175 if (!msg.HasAttribute(IFLA_IFNAME)) {
176 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
177 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700178 }
Chris Masone2aa97072011-08-09 17:35:08 -0700179 ByteString b(msg.GetAttribute(IFLA_IFNAME));
180 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Paul Stewarta3c56f92011-05-26 07:08:52 -0700181 VLOG(2) << "add link index " << dev_index << " name " << link_name;
182
Chris Masone2aa97072011-08-09 17:35:08 -0700183 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700184 if (ContainsKey(black_list_, link_name)) {
185 technology = Device::kBlacklisted;
186 } else {
187 technology = GetDeviceTechnology(link_name);
188 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700189 }
Chris Masone877ff982011-09-21 16:18:24 -0700190 string address =
191 StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Paul Stewartb50f0b92011-05-16 16:31:42 -0700192 switch (technology) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700193 case Device::kCellular:
194 // Cellular devices are managed by ModemInfo.
195 VLOG(2) << "Cellular link " << link_name << " at index " << dev_index
196 << " ignored.";
197 return;
198 case Device::kEthernet:
Paul Stewartbf1861b2011-08-23 15:45:35 -0700199 EnableDeviceIPv6Privacy(link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700200 device = new Ethernet(control_interface_, dispatcher_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700201 link_name, address, dev_index);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700202 break;
203 case Device::kWifi:
Paul Stewartbf1861b2011-08-23 15:45:35 -0700204 EnableDeviceIPv6Privacy(link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700205 device = new WiFi(control_interface_, dispatcher_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700206 link_name, address, dev_index);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700207 break;
208 default:
209 device = new DeviceStub(control_interface_, dispatcher_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700210 link_name, address, dev_index, technology);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700211 break;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700212 }
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700213 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700214 }
Darin Petkove6193c02011-08-11 12:42:40 -0700215 device->LinkEvent(flags, change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700216}
217
Chris Masone2aa97072011-08-09 17:35:08 -0700218void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700219 VLOG(2) << __func__ << "(index=" << msg.interface_index() << ")";
220
Paul Stewart9a908082011-08-31 12:18:48 -0700221 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
222 msg.mode() == RTNLMessage::kModeDelete);
Paul Stewart2713d6c2011-08-25 15:38:15 -0700223 VLOG(2) << __func__ << "(index=" << msg.interface_index()
224 << std::showbase << std::hex
225 << ", flags=" << msg.link_status().flags
226 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700227 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700228}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700229
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700230DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
231 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700232 return info ? info->device : NULL;
233}
234
Paul Stewart32852962011-08-30 14:06:53 -0700235bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700236 const Info *info = GetInfo(interface_index);
237 if (!info) {
238 return false;
239 }
Paul Stewart32852962011-08-30 14:06:53 -0700240 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700241 return true;
242}
243
Paul Stewart9a908082011-08-31 12:18:48 -0700244bool DeviceInfo::GetAddresses(int interface_index,
245 vector<AddressData> *addresses) const {
246 const Info *info = GetInfo(interface_index);
247 if (!info) {
248 return false;
249 }
250 *addresses = info->ip_addresses;
251 return true;
252}
253
254void DeviceInfo::FlushAddresses(int interface_index) const {
Paul Stewart7355ce12011-09-02 10:47:01 -0700255 VLOG(2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700256 const Info *info = GetInfo(interface_index);
257 if (!info) {
258 return;
259 }
260 const vector<AddressData> &addresses = info->ip_addresses;
261 vector<AddressData>::const_iterator iter;
262 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700263 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700264 (iter->scope == RT_SCOPE_UNIVERSE &&
265 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
266 VLOG(2) << __func__ << ": removing ip address from interface "
267 << interface_index;
268 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
269 }
270 }
271}
272
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700273bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
274 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700275 if (!info) {
276 return false;
277 }
278 *flags = info->flags;
279 return true;
280}
281
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700282const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
283 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700284 if (iter == infos_.end()) {
285 return NULL;
286 }
287 return &iter->second;
288}
289
290void DeviceInfo::RemoveInfo(int interface_index) {
291 map<int, Info>::iterator iter = infos_.find(interface_index);
292 if (iter != infos_.end()) {
293 VLOG(2) << "Removing info for device index: " << interface_index;
294 if (iter->second.device.get()) {
295 manager_->DeregisterDevice(iter->second.device);
296 }
297 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700298 } else {
299 VLOG(2) << __func__ << "unknown device index: " << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700300 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700301}
302
Chris Masone2aa97072011-08-09 17:35:08 -0700303void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700304 DCHECK(msg.type() == RTNLMessage::kTypeLink);
305 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700306 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700307 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700308 DelLinkMsgHandler(msg);
309 } else {
310 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700311 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700312}
313
Paul Stewart9a908082011-08-31 12:18:48 -0700314void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700315 VLOG(2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700316 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
317 int interface_index = msg.interface_index();
318 if (!ContainsKey(infos_, interface_index)) {
319 LOG(ERROR) << "Got address type message for unknown index "
320 << interface_index;
321 return;
322 }
323 const RTNLMessage::AddressStatus &status = msg.address_status();
324 IPAddress address(msg.family(),
325 msg.GetAttribute(IFA_ADDRESS),
326 status.prefix_len);
327
328 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
329 vector<AddressData>::iterator iter;
330 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
331 if (address.Equals(iter->address)) {
332 break;
333 }
334 }
335 if (iter != address_list.end()) {
336 if (msg.mode() == RTNLMessage::kModeDelete) {
337 VLOG(2) << "Delete address for interface " << interface_index;
338 address_list.erase(iter);
339 } else {
340 iter->flags = status.flags;
341 iter->scope = status.scope;
342 }
343 } else if (msg.mode() == RTNLMessage::kModeAdd) {
344 address_list.push_back(AddressData(address, status.flags, status.scope));
345 VLOG(2) << "Add address for interface " << interface_index;
346 }
347}
348
Paul Stewartbf1861b2011-08-23 15:45:35 -0700349void DeviceInfo::EnableDeviceIPv6Privacy(const string &iface_name) {
350 FilePath priv_file(StringPrintf(kInterfaceIPv6Privacy, iface_name.c_str()));
Paul Stewart5c206e12011-08-26 08:40:37 -0700351 LOG_IF(ERROR, file_util::WriteFile(priv_file, "2", 1) != 1)
Paul Stewartbf1861b2011-08-23 15:45:35 -0700352 << "Write failed for use_tempaddr: " << priv_file.value();
353}
354
Paul Stewart0af98bf2011-05-10 17:38:08 -0700355} // namespace shill