blob: 611e039d4b5b93337af71e14d249383fb1ce42a9 [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>
Paul Stewartb50f0b92011-05-16 16:31:42 -070024#include <base/stringprintf.h>
Paul Stewart0af98bf2011-05-10 17:38:08 -070025
26#include "shill/control_interface.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070027#include "shill/device.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070028#include "shill/device_info.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070029#include "shill/device_stub.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070030#include "shill/ethernet.h"
31#include "shill/manager.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070032#include "shill/rtnl_handler.h"
33#include "shill/rtnl_listener.h"
Chris Masone2aa97072011-08-09 17:35:08 -070034#include "shill/rtnl_message.h"
Chris Masone487b8bf2011-05-13 16:27:57 -070035#include "shill/service.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070036#include "shill/wifi.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070037
Darin Petkove6193c02011-08-11 12:42:40 -070038using std::map;
Paul Stewart0af98bf2011-05-10 17:38:08 -070039using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070040using std::vector;
Paul Stewart0af98bf2011-05-10 17:38:08 -070041
42namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070043
44// static
Paul Stewartbf1861b2011-08-23 15:45:35 -070045const char DeviceInfo::kInterfaceUevent[] = "/sys/class/net/%s/uevent";
Paul Stewartb50f0b92011-05-16 16:31:42 -070046// static
Paul Stewartbf1861b2011-08-23 15:45:35 -070047const char DeviceInfo::kInterfaceDriver[] = "/sys/class/net/%s/device/driver";
Paul Stewartb50f0b92011-05-16 16:31:42 -070048// static
49const char *DeviceInfo::kModemDrivers[] = {
50 "gobi",
51 "QCUSBNet2k",
52 "GobiNet",
53 NULL
54};
Paul Stewartbf1861b2011-08-23 15:45:35 -070055// static
56const char DeviceInfo::kInterfaceIPv6Privacy[] =
57 "/proc/sys/net/ipv6/conf/%s/use_tempaddr";
Paul Stewartb50f0b92011-05-16 16:31:42 -070058
59
60DeviceInfo::DeviceInfo(ControlInterface *control_interface,
61 EventDispatcher *dispatcher,
62 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070063 : control_interface_(control_interface),
64 dispatcher_(dispatcher),
65 manager_(manager),
66 link_callback_(NewCallback(this, &DeviceInfo::LinkMsgHandler)),
Paul Stewart9a908082011-08-31 12:18:48 -070067 address_callback_(NewCallback(this, &DeviceInfo::AddressMsgHandler)),
68 link_listener_(NULL),
69 address_listener_(NULL),
70 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070071}
72
Paul Stewarta3c56f92011-05-26 07:08:52 -070073DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070074
mukesh agrawal8f317b62011-07-15 11:53:23 -070075void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
76 black_list_.insert(device_name);
77}
78
Paul Stewarta3c56f92011-05-26 07:08:52 -070079void DeviceInfo::Start() {
80 link_listener_.reset(
81 new RTNLListener(RTNLHandler::kRequestLink, link_callback_.get()));
Paul Stewart9a908082011-08-31 12:18:48 -070082 address_listener_.reset(
83 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_.get()));
84 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
85 RTNLHandler::kRequestAddr);
Paul Stewart0af98bf2011-05-10 17:38:08 -070086}
87
Paul Stewarta3c56f92011-05-26 07:08:52 -070088void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -070089 link_listener_.reset();
90 address_listener_.reset();
Paul Stewart0af98bf2011-05-10 17:38:08 -070091}
92
Darin Petkov6f9eaa32011-08-09 15:26:44 -070093void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
94 VLOG(2) << __func__ << "(" << device->link_name() << ", "
95 << device->interface_index() << ")";
Darin Petkove6193c02011-08-11 12:42:40 -070096 CHECK(!GetDevice(device->interface_index()).get());
97 infos_[device->interface_index()].device = device;
Darin Petkov6f9eaa32011-08-09 15:26:44 -070098 if (device->TechnologyIs(Device::kCellular) ||
99 device->TechnologyIs(Device::kEthernet) ||
100 device->TechnologyIs(Device::kWifi)) {
101 manager_->RegisterDevice(device);
102 }
103}
104
Chris Masone2aa97072011-08-09 17:35:08 -0700105Device::Technology DeviceInfo::GetDeviceTechnology(const string &iface_name) {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700106 char contents[1024];
107 int length;
108 int fd;
Chris Masone2aa97072011-08-09 17:35:08 -0700109 string uevent_file = StringPrintf(kInterfaceUevent, iface_name.c_str());
110 string driver_file = StringPrintf(kInterfaceDriver, iface_name.c_str());
Paul Stewartb50f0b92011-05-16 16:31:42 -0700111 const char *wifi_type;
112 const char *driver_name;
113 int modem_idx;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700114
Paul Stewartb50f0b92011-05-16 16:31:42 -0700115 fd = open(uevent_file.c_str(), O_RDONLY);
116 if (fd < 0)
117 return Device::kUnknown;
118
119 length = read(fd, contents, sizeof(contents) - 1);
120 if (length < 0)
121 return Device::kUnknown;
122
123 /*
124 * If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
125 * start of the file or after a newline, we can safely assume this
126 * is a wifi device.
127 */
128 contents[length] = '\0';
129 wifi_type = strstr(contents, "DEVTYPE=wlan\n");
130 if (wifi_type != NULL && (wifi_type == contents || wifi_type[-1] == '\n'))
131 return Device::kWifi;
132
133 length = readlink(driver_file.c_str(), contents, sizeof(contents)-1);
134 if (length < 0)
135 return Device::kUnknown;
136
137 contents[length] = '\0';
138 driver_name = strrchr(contents, '/');
139 if (driver_name != NULL) {
140 driver_name++;
141 // See if driver for this interface is in a list of known modem driver names
142 for (modem_idx = 0; kModemDrivers[modem_idx] != NULL; modem_idx++)
143 if (strcmp(driver_name, kModemDrivers[modem_idx]) == 0)
144 return Device::kCellular;
145 }
146
147 return Device::kEthernet;
148}
149
Chris Masone2aa97072011-08-09 17:35:08 -0700150void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700151 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
152 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700153 int dev_index = msg.interface_index();
mukesh agrawalf60e4062011-05-27 13:13:41 -0700154 Device::Technology technology = Device::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700155
Darin Petkove6193c02011-08-11 12:42:40 -0700156 unsigned int flags = msg.link_status().flags;
157 unsigned int change = msg.link_status().change;
158 VLOG(2) << __func__ << "(index=" << dev_index
mukesh agrawal47009f82011-08-25 14:07:35 -0700159 << std::showbase << std::hex
160 << ", flags=" << flags << ", change=" << change << ")"
161 << std::dec << std::noshowbase;
Darin Petkove6193c02011-08-11 12:42:40 -0700162 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700163
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700164 DeviceRefPtr device = GetDevice(dev_index);
165 if (!device.get()) {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700166 if (msg.HasAttribute(IFLA_ADDRESS)) {
Paul Stewart32852962011-08-30 14:06:53 -0700167 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700168 VLOG(2) << "link index " << dev_index << " address "
Paul Stewart32852962011-08-30 14:06:53 -0700169 << infos_[dev_index].mac_address.HexEncode();
Chris Masone626719f2011-08-18 16:58:48 -0700170 } else {
171 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
172 return;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700173 }
Chris Masone2aa97072011-08-09 17:35:08 -0700174 if (!msg.HasAttribute(IFLA_IFNAME)) {
175 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
176 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700177 }
Chris Masone2aa97072011-08-09 17:35:08 -0700178 ByteString b(msg.GetAttribute(IFLA_IFNAME));
179 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Paul Stewarta3c56f92011-05-26 07:08:52 -0700180 VLOG(2) << "add link index " << dev_index << " name " << link_name;
181
Chris Masone2aa97072011-08-09 17:35:08 -0700182 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700183 if (ContainsKey(black_list_, link_name)) {
184 technology = Device::kBlacklisted;
185 } else {
186 technology = GetDeviceTechnology(link_name);
187 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700188 }
Paul Stewart32852962011-08-30 14:06:53 -0700189 string address = infos_[dev_index].mac_address.HexEncode();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700190 switch (technology) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700191 case Device::kCellular:
192 // Cellular devices are managed by ModemInfo.
193 VLOG(2) << "Cellular link " << link_name << " at index " << dev_index
194 << " ignored.";
195 return;
196 case Device::kEthernet:
Paul Stewartbf1861b2011-08-23 15:45:35 -0700197 EnableDeviceIPv6Privacy(link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700198 device = new Ethernet(control_interface_, dispatcher_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700199 link_name, address, dev_index);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700200 break;
201 case Device::kWifi:
Paul Stewartbf1861b2011-08-23 15:45:35 -0700202 EnableDeviceIPv6Privacy(link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700203 device = new WiFi(control_interface_, dispatcher_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700204 link_name, address, dev_index);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700205 break;
206 default:
207 device = new DeviceStub(control_interface_, dispatcher_, manager_,
Chris Masone626719f2011-08-18 16:58:48 -0700208 link_name, address, dev_index, technology);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700209 break;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700210 }
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700211 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700212 }
Darin Petkove6193c02011-08-11 12:42:40 -0700213 device->LinkEvent(flags, change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700214}
215
Chris Masone2aa97072011-08-09 17:35:08 -0700216void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700217 VLOG(2) << __func__ << "(index=" << msg.interface_index() << ")";
218
Paul Stewart9a908082011-08-31 12:18:48 -0700219 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
220 msg.mode() == RTNLMessage::kModeDelete);
Paul Stewart2713d6c2011-08-25 15:38:15 -0700221 VLOG(2) << __func__ << "(index=" << msg.interface_index()
222 << std::showbase << std::hex
223 << ", flags=" << msg.link_status().flags
224 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700225 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700226}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700227
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700228DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
229 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700230 return info ? info->device : NULL;
231}
232
Paul Stewart32852962011-08-30 14:06:53 -0700233bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700234 const Info *info = GetInfo(interface_index);
235 if (!info) {
236 return false;
237 }
Paul Stewart32852962011-08-30 14:06:53 -0700238 *address = info->mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700239 return true;
240}
241
Paul Stewart9a908082011-08-31 12:18:48 -0700242bool DeviceInfo::GetAddresses(int interface_index,
243 vector<AddressData> *addresses) const {
244 const Info *info = GetInfo(interface_index);
245 if (!info) {
246 return false;
247 }
248 *addresses = info->ip_addresses;
249 return true;
250}
251
252void DeviceInfo::FlushAddresses(int interface_index) const {
Paul Stewart7355ce12011-09-02 10:47:01 -0700253 VLOG(2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700254 const Info *info = GetInfo(interface_index);
255 if (!info) {
256 return;
257 }
258 const vector<AddressData> &addresses = info->ip_addresses;
259 vector<AddressData>::const_iterator iter;
260 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700261 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700262 (iter->scope == RT_SCOPE_UNIVERSE &&
263 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
264 VLOG(2) << __func__ << ": removing ip address from interface "
265 << interface_index;
266 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
267 }
268 }
269}
270
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700271bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
272 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700273 if (!info) {
274 return false;
275 }
276 *flags = info->flags;
277 return true;
278}
279
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700280const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
281 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700282 if (iter == infos_.end()) {
283 return NULL;
284 }
285 return &iter->second;
286}
287
288void DeviceInfo::RemoveInfo(int interface_index) {
289 map<int, Info>::iterator iter = infos_.find(interface_index);
290 if (iter != infos_.end()) {
291 VLOG(2) << "Removing info for device index: " << interface_index;
292 if (iter->second.device.get()) {
293 manager_->DeregisterDevice(iter->second.device);
294 }
295 infos_.erase(iter);
mukesh agrawal47009f82011-08-25 14:07:35 -0700296 } else {
297 VLOG(2) << __func__ << "unknown device index: " << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700298 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700299}
300
Chris Masone2aa97072011-08-09 17:35:08 -0700301void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700302 DCHECK(msg.type() == RTNLMessage::kTypeLink);
303 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700304 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700305 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700306 DelLinkMsgHandler(msg);
307 } else {
308 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700309 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700310}
311
Paul Stewart9a908082011-08-31 12:18:48 -0700312void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700313 VLOG(2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700314 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
315 int interface_index = msg.interface_index();
316 if (!ContainsKey(infos_, interface_index)) {
317 LOG(ERROR) << "Got address type message for unknown index "
318 << interface_index;
319 return;
320 }
321 const RTNLMessage::AddressStatus &status = msg.address_status();
322 IPAddress address(msg.family(),
323 msg.GetAttribute(IFA_ADDRESS),
324 status.prefix_len);
325
326 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
327 vector<AddressData>::iterator iter;
328 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
329 if (address.Equals(iter->address)) {
330 break;
331 }
332 }
333 if (iter != address_list.end()) {
334 if (msg.mode() == RTNLMessage::kModeDelete) {
335 VLOG(2) << "Delete address for interface " << interface_index;
336 address_list.erase(iter);
337 } else {
338 iter->flags = status.flags;
339 iter->scope = status.scope;
340 }
341 } else if (msg.mode() == RTNLMessage::kModeAdd) {
342 address_list.push_back(AddressData(address, status.flags, status.scope));
343 VLOG(2) << "Add address for interface " << interface_index;
344 }
345}
346
Paul Stewartbf1861b2011-08-23 15:45:35 -0700347void DeviceInfo::EnableDeviceIPv6Privacy(const string &iface_name) {
348 FilePath priv_file(StringPrintf(kInterfaceIPv6Privacy, iface_name.c_str()));
Paul Stewart5c206e12011-08-26 08:40:37 -0700349 LOG_IF(ERROR, file_util::WriteFile(priv_file, "2", 1) != 1)
Paul Stewartbf1861b2011-08-23 15:45:35 -0700350 << "Write failed for use_tempaddr: " << priv_file.value();
351}
352
Paul Stewart0af98bf2011-05-10 17:38:08 -0700353} // namespace shill