blob: ed7d630f8b80bd669a19bb8227f30fbb6800105e [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>
Chris Masone0e1d1042011-05-09 18:07:03 -070020#include <base/hash_tables.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 Petkov6f9eaa32011-08-09 15:26:44 -070038using base::hash_map;
Paul Stewart0af98bf2011-05-10 17:38:08 -070039using std::string;
40
41namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070042
43// static
44const char *DeviceInfo::kInterfaceUevent= "/sys/class/net/%s/uevent";
45// static
46const char *DeviceInfo::kInterfaceDriver= "/sys/class/net/%s/device/driver";
47// static
48const char *DeviceInfo::kModemDrivers[] = {
49 "gobi",
50 "QCUSBNet2k",
51 "GobiNet",
52 NULL
53};
54
55
56DeviceInfo::DeviceInfo(ControlInterface *control_interface,
57 EventDispatcher *dispatcher,
58 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070059 : control_interface_(control_interface),
60 dispatcher_(dispatcher),
61 manager_(manager),
62 link_callback_(NewCallback(this, &DeviceInfo::LinkMsgHandler)),
63 link_listener_(NULL) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070064}
65
Paul Stewarta3c56f92011-05-26 07:08:52 -070066DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070067
mukesh agrawal8f317b62011-07-15 11:53:23 -070068void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
69 black_list_.insert(device_name);
70}
71
Paul Stewarta3c56f92011-05-26 07:08:52 -070072void DeviceInfo::Start() {
73 link_listener_.reset(
74 new RTNLListener(RTNLHandler::kRequestLink, link_callback_.get()));
75 RTNLHandler::GetInstance()->RequestDump(RTNLHandler::kRequestLink);
Paul Stewart0af98bf2011-05-10 17:38:08 -070076}
77
Paul Stewarta3c56f92011-05-26 07:08:52 -070078void DeviceInfo::Stop() {
79 link_listener_.reset(NULL);
Paul Stewart0af98bf2011-05-10 17:38:08 -070080}
81
Darin Petkov6f9eaa32011-08-09 15:26:44 -070082void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
83 VLOG(2) << __func__ << "(" << device->link_name() << ", "
84 << device->interface_index() << ")";
85 CHECK(!ContainsKey(devices_, device->interface_index()));
86 devices_[device->interface_index()] = device;
87 if (device->TechnologyIs(Device::kCellular) ||
88 device->TechnologyIs(Device::kEthernet) ||
89 device->TechnologyIs(Device::kWifi)) {
90 manager_->RegisterDevice(device);
91 }
92}
93
94DeviceRefPtr DeviceInfo::GetDevice(int interface_index) {
95 hash_map<int, DeviceRefPtr>::iterator iter = devices_.find(interface_index);
96 if (iter == devices_.end()) {
97 return NULL;
98 }
99 return iter->second;
100}
101
Chris Masone2aa97072011-08-09 17:35:08 -0700102Device::Technology DeviceInfo::GetDeviceTechnology(const string &iface_name) {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700103 char contents[1024];
104 int length;
105 int fd;
Chris Masone2aa97072011-08-09 17:35:08 -0700106 string uevent_file = StringPrintf(kInterfaceUevent, iface_name.c_str());
107 string driver_file = StringPrintf(kInterfaceDriver, iface_name.c_str());
Paul Stewartb50f0b92011-05-16 16:31:42 -0700108 const char *wifi_type;
109 const char *driver_name;
110 int modem_idx;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700111
Paul Stewartb50f0b92011-05-16 16:31:42 -0700112 fd = open(uevent_file.c_str(), O_RDONLY);
113 if (fd < 0)
114 return Device::kUnknown;
115
116 length = read(fd, contents, sizeof(contents) - 1);
117 if (length < 0)
118 return Device::kUnknown;
119
120 /*
121 * If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
122 * start of the file or after a newline, we can safely assume this
123 * is a wifi device.
124 */
125 contents[length] = '\0';
126 wifi_type = strstr(contents, "DEVTYPE=wlan\n");
127 if (wifi_type != NULL && (wifi_type == contents || wifi_type[-1] == '\n'))
128 return Device::kWifi;
129
130 length = readlink(driver_file.c_str(), contents, sizeof(contents)-1);
131 if (length < 0)
132 return Device::kUnknown;
133
134 contents[length] = '\0';
135 driver_name = strrchr(contents, '/');
136 if (driver_name != NULL) {
137 driver_name++;
138 // See if driver for this interface is in a list of known modem driver names
139 for (modem_idx = 0; kModemDrivers[modem_idx] != NULL; modem_idx++)
140 if (strcmp(driver_name, kModemDrivers[modem_idx]) == 0)
141 return Device::kCellular;
142 }
143
144 return Device::kEthernet;
145}
146
Chris Masone2aa97072011-08-09 17:35:08 -0700147void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
148 DCHECK(msg.type() == RTNLMessage::kMessageTypeLink &&
149 msg.mode() == RTNLMessage::kMessageModeAdd);
150 int dev_index = msg.interface_index();
mukesh agrawalf60e4062011-05-27 13:13:41 -0700151 Device::Technology technology = Device::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700152
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700153 VLOG(2) << __func__;
154
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700155 DeviceRefPtr device = GetDevice(dev_index);
156 if (!device.get()) {
Chris Masone2aa97072011-08-09 17:35:08 -0700157 if (!msg.HasAttribute(IFLA_IFNAME)) {
158 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
159 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700160 }
Chris Masone2aa97072011-08-09 17:35:08 -0700161 ByteString b(msg.GetAttribute(IFLA_IFNAME));
162 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Paul Stewarta3c56f92011-05-26 07:08:52 -0700163 VLOG(2) << "add link index " << dev_index << " name " << link_name;
164
Chris Masone2aa97072011-08-09 17:35:08 -0700165 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700166 if (ContainsKey(black_list_, link_name)) {
167 technology = Device::kBlacklisted;
168 } else {
169 technology = GetDeviceTechnology(link_name);
170 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700171 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700172
173 switch (technology) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700174 case Device::kCellular:
175 // Cellular devices are managed by ModemInfo.
176 VLOG(2) << "Cellular link " << link_name << " at index " << dev_index
177 << " ignored.";
178 return;
179 case Device::kEthernet:
180 device = new Ethernet(control_interface_, dispatcher_, manager_,
181 link_name, dev_index);
182 break;
183 case Device::kWifi:
184 device = new WiFi(control_interface_, dispatcher_, manager_,
185 link_name, dev_index);
186 break;
187 default:
188 device = new DeviceStub(control_interface_, dispatcher_, manager_,
189 link_name, dev_index, technology);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700190 break;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700191 }
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700192 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700193 }
194
Chris Masone2aa97072011-08-09 17:35:08 -0700195 device->LinkEvent(msg.link_status().flags, msg.link_status().change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700196}
197
Chris Masone2aa97072011-08-09 17:35:08 -0700198void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
199 DCHECK(msg.type() == RTNLMessage::kMessageTypeLink &&
200 msg.mode() == RTNLMessage::kMessageModeDelete);
201 RemoveDevice(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700202}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700203
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700204void DeviceInfo::RemoveDevice(int interface_index) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700205 hash_map<int, DeviceRefPtr>::iterator ndev = devices_.find(interface_index);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700206 if (ndev != devices_.end()) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700207 VLOG(2) << "Removing device index: " << interface_index;
208 manager_->DeregisterDevice(ndev->second);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700209 devices_.erase(ndev);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700210 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700211}
212
Chris Masone2aa97072011-08-09 17:35:08 -0700213void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
214 DCHECK(msg.type() == RTNLMessage::kMessageTypeLink);
215 if (msg.mode() == RTNLMessage::kMessageModeAdd) {
216 AddLinkMsgHandler(msg);
217 } else if (msg.mode() == RTNLMessage::kMessageModeDelete) {
218 DelLinkMsgHandler(msg);
219 } else {
220 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700221 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700222}
223
224} // namespace shill