blob: 908a2f93d2d941b885e925f0f7b593206fedfa19 [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 Masone487b8bf2011-05-13 16:27:57 -070034#include "shill/service.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070035#include "shill/wifi.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070036
Darin Petkov6f9eaa32011-08-09 15:26:44 -070037using base::hash_map;
Paul Stewart0af98bf2011-05-10 17:38:08 -070038using std::string;
39
40namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070041
42// static
43const char *DeviceInfo::kInterfaceUevent= "/sys/class/net/%s/uevent";
44// static
45const char *DeviceInfo::kInterfaceDriver= "/sys/class/net/%s/device/driver";
46// static
47const char *DeviceInfo::kModemDrivers[] = {
48 "gobi",
49 "QCUSBNet2k",
50 "GobiNet",
51 NULL
52};
53
54
55DeviceInfo::DeviceInfo(ControlInterface *control_interface,
56 EventDispatcher *dispatcher,
57 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070058 : control_interface_(control_interface),
59 dispatcher_(dispatcher),
60 manager_(manager),
61 link_callback_(NewCallback(this, &DeviceInfo::LinkMsgHandler)),
62 link_listener_(NULL) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070063}
64
Paul Stewarta3c56f92011-05-26 07:08:52 -070065DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070066
mukesh agrawal8f317b62011-07-15 11:53:23 -070067void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
68 black_list_.insert(device_name);
69}
70
Paul Stewarta3c56f92011-05-26 07:08:52 -070071void DeviceInfo::Start() {
72 link_listener_.reset(
73 new RTNLListener(RTNLHandler::kRequestLink, link_callback_.get()));
74 RTNLHandler::GetInstance()->RequestDump(RTNLHandler::kRequestLink);
Paul Stewart0af98bf2011-05-10 17:38:08 -070075}
76
Paul Stewarta3c56f92011-05-26 07:08:52 -070077void DeviceInfo::Stop() {
78 link_listener_.reset(NULL);
Paul Stewart0af98bf2011-05-10 17:38:08 -070079}
80
Darin Petkov6f9eaa32011-08-09 15:26:44 -070081void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
82 VLOG(2) << __func__ << "(" << device->link_name() << ", "
83 << device->interface_index() << ")";
84 CHECK(!ContainsKey(devices_, device->interface_index()));
85 devices_[device->interface_index()] = device;
86 if (device->TechnologyIs(Device::kCellular) ||
87 device->TechnologyIs(Device::kEthernet) ||
88 device->TechnologyIs(Device::kWifi)) {
89 manager_->RegisterDevice(device);
90 }
91}
92
93DeviceRefPtr DeviceInfo::GetDevice(int interface_index) {
94 hash_map<int, DeviceRefPtr>::iterator iter = devices_.find(interface_index);
95 if (iter == devices_.end()) {
96 return NULL;
97 }
98 return iter->second;
99}
100
Paul Stewartb50f0b92011-05-16 16:31:42 -0700101Device::Technology DeviceInfo::GetDeviceTechnology(const char *interface_name) {
102 char contents[1024];
103 int length;
104 int fd;
105 string uevent_file = StringPrintf(kInterfaceUevent, interface_name);
106 string driver_file = StringPrintf(kInterfaceDriver, interface_name);
107 const char *wifi_type;
108 const char *driver_name;
109 int modem_idx;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700110
Paul Stewartb50f0b92011-05-16 16:31:42 -0700111 fd = open(uevent_file.c_str(), O_RDONLY);
112 if (fd < 0)
113 return Device::kUnknown;
114
115 length = read(fd, contents, sizeof(contents) - 1);
116 if (length < 0)
117 return Device::kUnknown;
118
119 /*
120 * If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
121 * start of the file or after a newline, we can safely assume this
122 * is a wifi device.
123 */
124 contents[length] = '\0';
125 wifi_type = strstr(contents, "DEVTYPE=wlan\n");
126 if (wifi_type != NULL && (wifi_type == contents || wifi_type[-1] == '\n'))
127 return Device::kWifi;
128
129 length = readlink(driver_file.c_str(), contents, sizeof(contents)-1);
130 if (length < 0)
131 return Device::kUnknown;
132
133 contents[length] = '\0';
134 driver_name = strrchr(contents, '/');
135 if (driver_name != NULL) {
136 driver_name++;
137 // See if driver for this interface is in a list of known modem driver names
138 for (modem_idx = 0; kModemDrivers[modem_idx] != NULL; modem_idx++)
139 if (strcmp(driver_name, kModemDrivers[modem_idx]) == 0)
140 return Device::kCellular;
141 }
142
143 return Device::kEthernet;
144}
145
Paul Stewarta3c56f92011-05-26 07:08:52 -0700146void DeviceInfo::AddLinkMsgHandler(struct nlmsghdr *hdr) {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700147 struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr));
Paul Stewartb50f0b92011-05-16 16:31:42 -0700148 int dev_index = msg->ifi_index;
149 struct rtattr *rta;
150 int rta_bytes;
151 char *link_name = NULL;
mukesh agrawalf60e4062011-05-27 13:13:41 -0700152 Device::Technology technology = Device::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700153
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700154 VLOG(2) << __func__;
155
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700156 DeviceRefPtr device = GetDevice(dev_index);
157 if (!device.get()) {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700158 rta_bytes = IFLA_PAYLOAD(hdr);
159 for (rta = IFLA_RTA(msg); RTA_OK(rta, rta_bytes);
160 rta = RTA_NEXT(rta, rta_bytes)) {
161 if (rta->rta_type == IFLA_IFNAME) {
162 link_name = reinterpret_cast<char *>(RTA_DATA(rta));
163 break;
164 } else {
165 VLOG(2) << " RTA type " << rta->rta_type;
166 }
167 }
168
Paul Stewarta3c56f92011-05-26 07:08:52 -0700169 VLOG(2) << "add link index " << dev_index << " name " << link_name;
170
Darin Petkov633ac6f2011-07-08 13:56:13 -0700171 if (link_name) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700172 if (ContainsKey(black_list_, link_name)) {
173 technology = Device::kBlacklisted;
174 } else {
175 technology = GetDeviceTechnology(link_name);
176 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700177 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700178
179 switch (technology) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700180 case Device::kCellular:
181 // Cellular devices are managed by ModemInfo.
182 VLOG(2) << "Cellular link " << link_name << " at index " << dev_index
183 << " ignored.";
184 return;
185 case Device::kEthernet:
186 device = new Ethernet(control_interface_, dispatcher_, manager_,
187 link_name, dev_index);
188 break;
189 case Device::kWifi:
190 device = new WiFi(control_interface_, dispatcher_, manager_,
191 link_name, dev_index);
192 break;
193 default:
194 device = new DeviceStub(control_interface_, dispatcher_, manager_,
195 link_name, dev_index, technology);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700196 break;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700197 }
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700198 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700199 }
200
Paul Stewarta3c56f92011-05-26 07:08:52 -0700201 device->LinkEvent(msg->ifi_flags, msg->ifi_change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700202}
203
Paul Stewarta3c56f92011-05-26 07:08:52 -0700204void DeviceInfo::DelLinkMsgHandler(struct nlmsghdr *hdr) {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700205 struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr));
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700206 RemoveDevice(msg->ifi_index);
207}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700208
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700209void DeviceInfo::RemoveDevice(int interface_index) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700210 hash_map<int, DeviceRefPtr>::iterator ndev = devices_.find(interface_index);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700211 if (ndev != devices_.end()) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700212 VLOG(2) << "Removing device index: " << interface_index;
213 manager_->DeregisterDevice(ndev->second);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700214 devices_.erase(ndev);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700215 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700216}
217
Paul Stewarta3c56f92011-05-26 07:08:52 -0700218void DeviceInfo::LinkMsgHandler(struct nlmsghdr *hdr) {
219 if (hdr->nlmsg_type == RTM_NEWLINK) {
220 AddLinkMsgHandler(hdr);
221 } else if (hdr->nlmsg_type == RTM_DELLINK) {
222 DelLinkMsgHandler(hdr);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700223 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700224}
225
226} // namespace shill