blob: a804cd5c936f3d812499789234dbe85ad202c68d [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;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700129 scoped_refptr<Device> device;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700130 Device::Technology technology;
131 bool is_stub = false;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700132
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700133 VLOG(2) << __func__;
134
Paul Stewartb50f0b92011-05-16 16:31:42 -0700135 if (ndev == devices_.end()) {
136 rta_bytes = IFLA_PAYLOAD(hdr);
137 for (rta = IFLA_RTA(msg); RTA_OK(rta, rta_bytes);
138 rta = RTA_NEXT(rta, rta_bytes)) {
139 if (rta->rta_type == IFLA_IFNAME) {
140 link_name = reinterpret_cast<char *>(RTA_DATA(rta));
141 break;
142 } else {
143 VLOG(2) << " RTA type " << rta->rta_type;
144 }
145 }
146
Paul Stewarta3c56f92011-05-26 07:08:52 -0700147 VLOG(2) << "add link index " << dev_index << " name " << link_name;
148
Paul Stewartb50f0b92011-05-16 16:31:42 -0700149 if (link_name != NULL)
150 technology = GetDeviceTechnology(link_name);
151
152 switch (technology) {
153 case Device::kEthernet:
Paul Stewarta3c56f92011-05-26 07:08:52 -0700154 device = new Ethernet(control_interface_, dispatcher_, manager_,
Paul Stewartb50f0b92011-05-16 16:31:42 -0700155 link_name, dev_index);
156 break;
157 case Device::kWifi:
Paul Stewarta3c56f92011-05-26 07:08:52 -0700158 device = new WiFi(control_interface_, dispatcher_, manager_,
159 link_name, dev_index);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700160 break;
161 default:
Paul Stewarta3c56f92011-05-26 07:08:52 -0700162 device = new DeviceStub(control_interface_, dispatcher_, manager_,
Paul Stewartb50f0b92011-05-16 16:31:42 -0700163 link_name, dev_index, technology);
164 is_stub = true;
165 }
166
167 devices_[dev_index] = device;
168
169 if (!is_stub)
170 manager_->RegisterDevice(device);
171 } else {
172 device = ndev->second;
173 }
174
Paul Stewarta3c56f92011-05-26 07:08:52 -0700175 device->LinkEvent(msg->ifi_flags, msg->ifi_change);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700176}
177
Paul Stewarta3c56f92011-05-26 07:08:52 -0700178void DeviceInfo::DelLinkMsgHandler(struct nlmsghdr *hdr) {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700179 struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr));
180 base::hash_map<int, scoped_refptr<Device> >::iterator ndev =
181 devices_.find(msg->ifi_index);
182 int dev_index = msg->ifi_index;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700183 scoped_refptr<Device> device;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700184
185 if (ndev != devices_.end()) {
186 device = ndev->second;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700187 manager_->DeregisterDevice(device.get());
Paul Stewartb50f0b92011-05-16 16:31:42 -0700188 devices_.erase(ndev);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700189 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700190}
191
Paul Stewarta3c56f92011-05-26 07:08:52 -0700192void DeviceInfo::LinkMsgHandler(struct nlmsghdr *hdr) {
193 if (hdr->nlmsg_type == RTM_NEWLINK) {
194 AddLinkMsgHandler(hdr);
195 } else if (hdr->nlmsg_type == RTM_DELLINK) {
196 DelLinkMsgHandler(hdr);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700197 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700198}
199
200} // namespace shill