blob: 9e61026f2a862d7cd8b8d4d67da1bac822079e47 [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
37using std::string;
38
39namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070040
41// static
42const char *DeviceInfo::kInterfaceUevent= "/sys/class/net/%s/uevent";
43// static
44const char *DeviceInfo::kInterfaceDriver= "/sys/class/net/%s/device/driver";
45// static
46const char *DeviceInfo::kModemDrivers[] = {
47 "gobi",
48 "QCUSBNet2k",
49 "GobiNet",
50 NULL
51};
52
53
54DeviceInfo::DeviceInfo(ControlInterface *control_interface,
55 EventDispatcher *dispatcher,
56 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070057 : control_interface_(control_interface),
58 dispatcher_(dispatcher),
59 manager_(manager),
60 link_callback_(NewCallback(this, &DeviceInfo::LinkMsgHandler)),
61 link_listener_(NULL) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070062}
63
Paul Stewarta3c56f92011-05-26 07:08:52 -070064DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070065
mukesh agrawal8f317b62011-07-15 11:53:23 -070066void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
67 black_list_.insert(device_name);
68}
69
Paul Stewarta3c56f92011-05-26 07:08:52 -070070void DeviceInfo::Start() {
71 link_listener_.reset(
72 new RTNLListener(RTNLHandler::kRequestLink, link_callback_.get()));
73 RTNLHandler::GetInstance()->RequestDump(RTNLHandler::kRequestLink);
Paul Stewart0af98bf2011-05-10 17:38:08 -070074}
75
Paul Stewarta3c56f92011-05-26 07:08:52 -070076void DeviceInfo::Stop() {
77 link_listener_.reset(NULL);
Paul Stewart0af98bf2011-05-10 17:38:08 -070078}
79
Paul Stewartb50f0b92011-05-16 16:31:42 -070080Device::Technology DeviceInfo::GetDeviceTechnology(const char *interface_name) {
81 char contents[1024];
82 int length;
83 int fd;
84 string uevent_file = StringPrintf(kInterfaceUevent, interface_name);
85 string driver_file = StringPrintf(kInterfaceDriver, interface_name);
86 const char *wifi_type;
87 const char *driver_name;
88 int modem_idx;
Paul Stewart0af98bf2011-05-10 17:38:08 -070089
Paul Stewartb50f0b92011-05-16 16:31:42 -070090 fd = open(uevent_file.c_str(), O_RDONLY);
91 if (fd < 0)
92 return Device::kUnknown;
93
94 length = read(fd, contents, sizeof(contents) - 1);
95 if (length < 0)
96 return Device::kUnknown;
97
98 /*
99 * If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
100 * start of the file or after a newline, we can safely assume this
101 * is a wifi device.
102 */
103 contents[length] = '\0';
104 wifi_type = strstr(contents, "DEVTYPE=wlan\n");
105 if (wifi_type != NULL && (wifi_type == contents || wifi_type[-1] == '\n'))
106 return Device::kWifi;
107
108 length = readlink(driver_file.c_str(), contents, sizeof(contents)-1);
109 if (length < 0)
110 return Device::kUnknown;
111
112 contents[length] = '\0';
113 driver_name = strrchr(contents, '/');
114 if (driver_name != NULL) {
115 driver_name++;
116 // See if driver for this interface is in a list of known modem driver names
117 for (modem_idx = 0; kModemDrivers[modem_idx] != NULL; modem_idx++)
118 if (strcmp(driver_name, kModemDrivers[modem_idx]) == 0)
119 return Device::kCellular;
120 }
121
122 return Device::kEthernet;
123}
124
Paul Stewarta3c56f92011-05-26 07:08:52 -0700125void DeviceInfo::AddLinkMsgHandler(struct nlmsghdr *hdr) {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700126 struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr));
Chris Masonec1e50412011-06-07 13:04:53 -0700127 base::hash_map<int, DeviceRefPtr>::iterator ndev =
Paul Stewartb50f0b92011-05-16 16:31:42 -0700128 devices_.find(msg->ifi_index);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700129 int dev_index = msg->ifi_index;
130 struct rtattr *rta;
131 int rta_bytes;
132 char *link_name = NULL;
Chris Masonec1e50412011-06-07 13:04:53 -0700133 DeviceRefPtr device;
mukesh agrawalf60e4062011-05-27 13:13:41 -0700134 Device::Technology technology = Device::kUnknown;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700135 bool is_stub = false;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700136
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700137 VLOG(2) << __func__;
138
Paul Stewartb50f0b92011-05-16 16:31:42 -0700139 if (ndev == devices_.end()) {
140 rta_bytes = IFLA_PAYLOAD(hdr);
141 for (rta = IFLA_RTA(msg); RTA_OK(rta, rta_bytes);
142 rta = RTA_NEXT(rta, rta_bytes)) {
143 if (rta->rta_type == IFLA_IFNAME) {
144 link_name = reinterpret_cast<char *>(RTA_DATA(rta));
145 break;
146 } else {
147 VLOG(2) << " RTA type " << rta->rta_type;
148 }
149 }
150
Paul Stewarta3c56f92011-05-26 07:08:52 -0700151 VLOG(2) << "add link index " << dev_index << " name " << link_name;
152
Darin Petkov633ac6f2011-07-08 13:56:13 -0700153 if (link_name) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700154 if (ContainsKey(black_list_, link_name)) {
155 technology = Device::kBlacklisted;
156 } else {
157 technology = GetDeviceTechnology(link_name);
158 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700159 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700160
161 switch (technology) {
162 case Device::kEthernet:
Paul Stewarta3c56f92011-05-26 07:08:52 -0700163 device = new Ethernet(control_interface_, dispatcher_, manager_,
Paul Stewartb50f0b92011-05-16 16:31:42 -0700164 link_name, dev_index);
165 break;
166 case Device::kWifi:
Paul Stewarta3c56f92011-05-26 07:08:52 -0700167 device = new WiFi(control_interface_, dispatcher_, manager_,
168 link_name, dev_index);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700169 break;
170 default:
Paul Stewarta3c56f92011-05-26 07:08:52 -0700171 device = new DeviceStub(control_interface_, dispatcher_, manager_,
Paul Stewartb50f0b92011-05-16 16:31:42 -0700172 link_name, dev_index, technology);
173 is_stub = true;
174 }
175
176 devices_[dev_index] = device;
177
178 if (!is_stub)
179 manager_->RegisterDevice(device);
180 } else {
181 device = ndev->second;
182 }
183
Paul Stewarta3c56f92011-05-26 07:08:52 -0700184 device->LinkEvent(msg->ifi_flags, msg->ifi_change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700185}
186
Paul Stewarta3c56f92011-05-26 07:08:52 -0700187void DeviceInfo::DelLinkMsgHandler(struct nlmsghdr *hdr) {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700188 struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr));
Chris Masonec1e50412011-06-07 13:04:53 -0700189 base::hash_map<int, DeviceRefPtr>::iterator ndev =
Paul Stewartb50f0b92011-05-16 16:31:42 -0700190 devices_.find(msg->ifi_index);
191 int dev_index = msg->ifi_index;
Chris Masonec1e50412011-06-07 13:04:53 -0700192 DeviceRefPtr device;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700193
194 if (ndev != devices_.end()) {
195 device = ndev->second;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700196 manager_->DeregisterDevice(device.get());
Paul Stewartb50f0b92011-05-16 16:31:42 -0700197 devices_.erase(ndev);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700198 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700199}
200
Paul Stewarta3c56f92011-05-26 07:08:52 -0700201void DeviceInfo::LinkMsgHandler(struct nlmsghdr *hdr) {
202 if (hdr->nlmsg_type == RTM_NEWLINK) {
203 AddLinkMsgHandler(hdr);
204 } else if (hdr->nlmsg_type == RTM_DELLINK) {
205 DelLinkMsgHandler(hdr);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700206 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700207}
208
209} // namespace shill