blob: d1452d29586317122baf1d47a9c4ee12a3ce7c06 [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>
Paul Stewartb50f0b92011-05-16 16:31:42 -070023#include <base/stringprintf.h>
Paul Stewart0af98bf2011-05-10 17:38:08 -070024
25#include "shill/control_interface.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070026#include "shill/device.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070027#include "shill/device_info.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070028#include "shill/device_stub.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070029#include "shill/ethernet.h"
30#include "shill/manager.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070031#include "shill/rtnl_handler.h"
32#include "shill/rtnl_listener.h"
Chris Masone487b8bf2011-05-13 16:27:57 -070033#include "shill/service.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070034#include "shill/wifi.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070035
36using std::string;
37
38namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070039
40// static
41const char *DeviceInfo::kInterfaceUevent= "/sys/class/net/%s/uevent";
42// static
43const char *DeviceInfo::kInterfaceDriver= "/sys/class/net/%s/device/driver";
44// static
45const char *DeviceInfo::kModemDrivers[] = {
46 "gobi",
47 "QCUSBNet2k",
48 "GobiNet",
49 NULL
50};
51
52
53DeviceInfo::DeviceInfo(ControlInterface *control_interface,
54 EventDispatcher *dispatcher,
55 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070056 : control_interface_(control_interface),
57 dispatcher_(dispatcher),
58 manager_(manager),
59 link_callback_(NewCallback(this, &DeviceInfo::LinkMsgHandler)),
60 link_listener_(NULL) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070061}
62
Paul Stewarta3c56f92011-05-26 07:08:52 -070063DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070064
Paul Stewarta3c56f92011-05-26 07:08:52 -070065void DeviceInfo::Start() {
66 link_listener_.reset(
67 new RTNLListener(RTNLHandler::kRequestLink, link_callback_.get()));
68 RTNLHandler::GetInstance()->RequestDump(RTNLHandler::kRequestLink);
Paul Stewart0af98bf2011-05-10 17:38:08 -070069}
70
Paul Stewarta3c56f92011-05-26 07:08:52 -070071void DeviceInfo::Stop() {
72 link_listener_.reset(NULL);
Paul Stewart0af98bf2011-05-10 17:38:08 -070073}
74
Paul Stewartb50f0b92011-05-16 16:31:42 -070075Device::Technology DeviceInfo::GetDeviceTechnology(const char *interface_name) {
76 char contents[1024];
77 int length;
78 int fd;
79 string uevent_file = StringPrintf(kInterfaceUevent, interface_name);
80 string driver_file = StringPrintf(kInterfaceDriver, interface_name);
81 const char *wifi_type;
82 const char *driver_name;
83 int modem_idx;
Paul Stewart0af98bf2011-05-10 17:38:08 -070084
Paul Stewartb50f0b92011-05-16 16:31:42 -070085 fd = open(uevent_file.c_str(), O_RDONLY);
86 if (fd < 0)
87 return Device::kUnknown;
88
89 length = read(fd, contents, sizeof(contents) - 1);
90 if (length < 0)
91 return Device::kUnknown;
92
93 /*
94 * If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
95 * start of the file or after a newline, we can safely assume this
96 * is a wifi device.
97 */
98 contents[length] = '\0';
99 wifi_type = strstr(contents, "DEVTYPE=wlan\n");
100 if (wifi_type != NULL && (wifi_type == contents || wifi_type[-1] == '\n'))
101 return Device::kWifi;
102
103 length = readlink(driver_file.c_str(), contents, sizeof(contents)-1);
104 if (length < 0)
105 return Device::kUnknown;
106
107 contents[length] = '\0';
108 driver_name = strrchr(contents, '/');
109 if (driver_name != NULL) {
110 driver_name++;
111 // See if driver for this interface is in a list of known modem driver names
112 for (modem_idx = 0; kModemDrivers[modem_idx] != NULL; modem_idx++)
113 if (strcmp(driver_name, kModemDrivers[modem_idx]) == 0)
114 return Device::kCellular;
115 }
116
117 return Device::kEthernet;
118}
119
Paul Stewarta3c56f92011-05-26 07:08:52 -0700120void DeviceInfo::AddLinkMsgHandler(struct nlmsghdr *hdr) {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700121 struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr));
122 base::hash_map<int, scoped_refptr<Device> >::iterator ndev =
123 devices_.find(msg->ifi_index);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700124 int bytes = IFLA_PAYLOAD(hdr);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700125 int dev_index = msg->ifi_index;
126 struct rtattr *rta;
127 int rta_bytes;
128 char *link_name = NULL;
129 Device *device;
130 Device::Technology technology;
131 bool is_stub = false;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700132
Paul Stewartb50f0b92011-05-16 16:31:42 -0700133 if (ndev == devices_.end()) {
134 rta_bytes = IFLA_PAYLOAD(hdr);
135 for (rta = IFLA_RTA(msg); RTA_OK(rta, rta_bytes);
136 rta = RTA_NEXT(rta, rta_bytes)) {
137 if (rta->rta_type == IFLA_IFNAME) {
138 link_name = reinterpret_cast<char *>(RTA_DATA(rta));
139 break;
140 } else {
141 VLOG(2) << " RTA type " << rta->rta_type;
142 }
143 }
144
Paul Stewarta3c56f92011-05-26 07:08:52 -0700145 VLOG(2) << "add link index " << dev_index << " name " << link_name;
146
Paul Stewartb50f0b92011-05-16 16:31:42 -0700147 if (link_name != NULL)
148 technology = GetDeviceTechnology(link_name);
149
150 switch (technology) {
151 case Device::kEthernet:
Paul Stewarta3c56f92011-05-26 07:08:52 -0700152 device = new Ethernet(control_interface_, dispatcher_, manager_,
Paul Stewartb50f0b92011-05-16 16:31:42 -0700153 link_name, dev_index);
154 break;
155 case Device::kWifi:
Paul Stewarta3c56f92011-05-26 07:08:52 -0700156 device = new WiFi(control_interface_, dispatcher_, manager_,
157 link_name, dev_index);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700158 break;
159 default:
Paul Stewarta3c56f92011-05-26 07:08:52 -0700160 device = new DeviceStub(control_interface_, dispatcher_, manager_,
Paul Stewartb50f0b92011-05-16 16:31:42 -0700161 link_name, dev_index, technology);
162 is_stub = true;
163 }
164
165 devices_[dev_index] = device;
166
167 if (!is_stub)
168 manager_->RegisterDevice(device);
169 } else {
170 device = ndev->second;
171 }
172
Paul Stewarta3c56f92011-05-26 07:08:52 -0700173 device->LinkEvent(msg->ifi_flags, msg->ifi_change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700174}
175
Paul Stewarta3c56f92011-05-26 07:08:52 -0700176void DeviceInfo::DelLinkMsgHandler(struct nlmsghdr *hdr) {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700177 struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr));
178 base::hash_map<int, scoped_refptr<Device> >::iterator ndev =
179 devices_.find(msg->ifi_index);
180 int dev_index = msg->ifi_index;
181 Device *device;
182
183 VLOG(2) << "del link index " << dev_index << " flags " <<
184 msg->ifi_flags;
185
186 if (ndev != devices_.end()) {
187 device = ndev->second;
188 devices_.erase(ndev);
189 manager_->DeregisterDevice(device);
190 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700191}
192
Paul Stewarta3c56f92011-05-26 07:08:52 -0700193void DeviceInfo::LinkMsgHandler(struct nlmsghdr *hdr) {
194 if (hdr->nlmsg_type == RTM_NEWLINK) {
195 AddLinkMsgHandler(hdr);
196 } else if (hdr->nlmsg_type == RTM_DELLINK) {
197 DelLinkMsgHandler(hdr);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700198 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700199}
200
201} // namespace shill