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 | |
| 37 | using std::string; |
| 38 | |
| 39 | namespace shill { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 40 | |
| 41 | // static |
| 42 | const char *DeviceInfo::kInterfaceUevent= "/sys/class/net/%s/uevent"; |
| 43 | // static |
| 44 | const char *DeviceInfo::kInterfaceDriver= "/sys/class/net/%s/device/driver"; |
| 45 | // static |
| 46 | const char *DeviceInfo::kModemDrivers[] = { |
| 47 | "gobi", |
| 48 | "QCUSBNet2k", |
| 49 | "GobiNet", |
| 50 | NULL |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | DeviceInfo::DeviceInfo(ControlInterface *control_interface, |
| 55 | EventDispatcher *dispatcher, |
| 56 | Manager *manager) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 57 | : control_interface_(control_interface), |
| 58 | dispatcher_(dispatcher), |
| 59 | manager_(manager), |
| 60 | link_callback_(NewCallback(this, &DeviceInfo::LinkMsgHandler)), |
| 61 | link_listener_(NULL) { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 64 | DeviceInfo::~DeviceInfo() {} |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 65 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame^] | 66 | void DeviceInfo::AddDeviceToBlackList(const string &device_name) { |
| 67 | black_list_.insert(device_name); |
| 68 | } |
| 69 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 70 | void DeviceInfo::Start() { |
| 71 | link_listener_.reset( |
| 72 | new RTNLListener(RTNLHandler::kRequestLink, link_callback_.get())); |
| 73 | RTNLHandler::GetInstance()->RequestDump(RTNLHandler::kRequestLink); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 76 | void DeviceInfo::Stop() { |
| 77 | link_listener_.reset(NULL); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 80 | Device::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 Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 89 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 90 | 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 Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 125 | void DeviceInfo::AddLinkMsgHandler(struct nlmsghdr *hdr) { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 126 | struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr)); |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 127 | base::hash_map<int, DeviceRefPtr>::iterator ndev = |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 128 | devices_.find(msg->ifi_index); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 129 | int dev_index = msg->ifi_index; |
| 130 | struct rtattr *rta; |
| 131 | int rta_bytes; |
| 132 | char *link_name = NULL; |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 133 | DeviceRefPtr device; |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 134 | Device::Technology technology = Device::kUnknown; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 135 | bool is_stub = false; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 136 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 137 | VLOG(2) << __func__; |
| 138 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 139 | 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 Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 151 | VLOG(2) << "add link index " << dev_index << " name " << link_name; |
| 152 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 153 | if (link_name) { |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame^] | 154 | if (ContainsKey(black_list_, link_name)) { |
| 155 | technology = Device::kBlacklisted; |
| 156 | } else { |
| 157 | technology = GetDeviceTechnology(link_name); |
| 158 | } |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 159 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 160 | |
| 161 | switch (technology) { |
| 162 | case Device::kEthernet: |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 163 | device = new Ethernet(control_interface_, dispatcher_, manager_, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 164 | link_name, dev_index); |
| 165 | break; |
| 166 | case Device::kWifi: |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 167 | device = new WiFi(control_interface_, dispatcher_, manager_, |
| 168 | link_name, dev_index); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 169 | break; |
| 170 | default: |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 171 | device = new DeviceStub(control_interface_, dispatcher_, manager_, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 172 | 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 Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 184 | device->LinkEvent(msg->ifi_flags, msg->ifi_change); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 187 | void DeviceInfo::DelLinkMsgHandler(struct nlmsghdr *hdr) { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 188 | struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr)); |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 189 | base::hash_map<int, DeviceRefPtr>::iterator ndev = |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 190 | devices_.find(msg->ifi_index); |
| 191 | int dev_index = msg->ifi_index; |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 192 | DeviceRefPtr device; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 193 | |
| 194 | if (ndev != devices_.end()) { |
| 195 | device = ndev->second; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 196 | manager_->DeregisterDevice(device.get()); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 197 | devices_.erase(ndev); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 198 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 201 | void 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 Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 206 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | } // namespace shill |