Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 1 | // 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 Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 16 | #include <fcntl.h> |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 17 | #include <string> |
| 18 | |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 19 | #include <base/callback_old.h> |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 20 | #include <base/hash_tables.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 21 | #include <base/logging.h> |
| 22 | #include <base/memory/scoped_ptr.h> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 23 | #include <base/stl_util-inl.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 24 | #include <base/stringprintf.h> |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 25 | |
| 26 | #include "shill/control_interface.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 27 | #include "shill/device.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 28 | #include "shill/device_info.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 29 | #include "shill/device_stub.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 30 | #include "shill/ethernet.h" |
| 31 | #include "shill/manager.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 32 | #include "shill/rtnl_handler.h" |
| 33 | #include "shill/rtnl_listener.h" |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 34 | #include "shill/service.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 35 | #include "shill/wifi.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 36 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame^] | 37 | using base::hash_map; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 38 | using std::string; |
| 39 | |
| 40 | namespace shill { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 41 | |
| 42 | // static |
| 43 | const char *DeviceInfo::kInterfaceUevent= "/sys/class/net/%s/uevent"; |
| 44 | // static |
| 45 | const char *DeviceInfo::kInterfaceDriver= "/sys/class/net/%s/device/driver"; |
| 46 | // static |
| 47 | const char *DeviceInfo::kModemDrivers[] = { |
| 48 | "gobi", |
| 49 | "QCUSBNet2k", |
| 50 | "GobiNet", |
| 51 | NULL |
| 52 | }; |
| 53 | |
| 54 | |
| 55 | DeviceInfo::DeviceInfo(ControlInterface *control_interface, |
| 56 | EventDispatcher *dispatcher, |
| 57 | Manager *manager) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 58 | : control_interface_(control_interface), |
| 59 | dispatcher_(dispatcher), |
| 60 | manager_(manager), |
| 61 | link_callback_(NewCallback(this, &DeviceInfo::LinkMsgHandler)), |
| 62 | link_listener_(NULL) { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 65 | DeviceInfo::~DeviceInfo() {} |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 66 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 67 | void DeviceInfo::AddDeviceToBlackList(const string &device_name) { |
| 68 | black_list_.insert(device_name); |
| 69 | } |
| 70 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 71 | void DeviceInfo::Start() { |
| 72 | link_listener_.reset( |
| 73 | new RTNLListener(RTNLHandler::kRequestLink, link_callback_.get())); |
| 74 | RTNLHandler::GetInstance()->RequestDump(RTNLHandler::kRequestLink); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 77 | void DeviceInfo::Stop() { |
| 78 | link_listener_.reset(NULL); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame^] | 81 | void 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 | |
| 93 | DeviceRefPtr 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 Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 101 | Device::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 Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 110 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 111 | 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 Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 146 | void DeviceInfo::AddLinkMsgHandler(struct nlmsghdr *hdr) { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 147 | struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr)); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 148 | int dev_index = msg->ifi_index; |
| 149 | struct rtattr *rta; |
| 150 | int rta_bytes; |
| 151 | char *link_name = NULL; |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 152 | Device::Technology technology = Device::kUnknown; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 153 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 154 | VLOG(2) << __func__; |
| 155 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame^] | 156 | DeviceRefPtr device = GetDevice(dev_index); |
| 157 | if (!device.get()) { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 158 | 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 Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 169 | VLOG(2) << "add link index " << dev_index << " name " << link_name; |
| 170 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 171 | if (link_name) { |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 172 | if (ContainsKey(black_list_, link_name)) { |
| 173 | technology = Device::kBlacklisted; |
| 174 | } else { |
| 175 | technology = GetDeviceTechnology(link_name); |
| 176 | } |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 177 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 178 | |
| 179 | switch (technology) { |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 180 | 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 Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame^] | 196 | break; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 197 | } |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame^] | 198 | RegisterDevice(device); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 201 | device->LinkEvent(msg->ifi_flags, msg->ifi_change); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 204 | void DeviceInfo::DelLinkMsgHandler(struct nlmsghdr *hdr) { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 205 | struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr)); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 206 | RemoveDevice(msg->ifi_index); |
| 207 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 208 | |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 209 | void DeviceInfo::RemoveDevice(int interface_index) { |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame^] | 210 | hash_map<int, DeviceRefPtr>::iterator ndev = devices_.find(interface_index); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 211 | if (ndev != devices_.end()) { |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 212 | VLOG(2) << "Removing device index: " << interface_index; |
| 213 | manager_->DeregisterDevice(ndev->second); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 214 | devices_.erase(ndev); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 215 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 218 | void 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 Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 223 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | } // namespace shill |