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> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 23 | #include <base/stringprintf.h> |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 24 | |
| 25 | #include "shill/control_interface.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 26 | #include "shill/device.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 27 | #include "shill/device_info.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 28 | #include "shill/device_stub.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 29 | #include "shill/ethernet.h" |
| 30 | #include "shill/manager.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 31 | #include "shill/rtnl_handler.h" |
| 32 | #include "shill/rtnl_listener.h" |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 33 | #include "shill/service.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 34 | #include "shill/wifi.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 35 | |
| 36 | using std::string; |
| 37 | |
| 38 | namespace shill { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 39 | |
| 40 | // static |
| 41 | const char *DeviceInfo::kInterfaceUevent= "/sys/class/net/%s/uevent"; |
| 42 | // static |
| 43 | const char *DeviceInfo::kInterfaceDriver= "/sys/class/net/%s/device/driver"; |
| 44 | // static |
| 45 | const char *DeviceInfo::kModemDrivers[] = { |
| 46 | "gobi", |
| 47 | "QCUSBNet2k", |
| 48 | "GobiNet", |
| 49 | NULL |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | DeviceInfo::DeviceInfo(ControlInterface *control_interface, |
| 54 | EventDispatcher *dispatcher, |
| 55 | Manager *manager) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 56 | : control_interface_(control_interface), |
| 57 | dispatcher_(dispatcher), |
| 58 | manager_(manager), |
| 59 | link_callback_(NewCallback(this, &DeviceInfo::LinkMsgHandler)), |
| 60 | link_listener_(NULL) { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 63 | DeviceInfo::~DeviceInfo() {} |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 64 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 65 | void DeviceInfo::Start() { |
| 66 | link_listener_.reset( |
| 67 | new RTNLListener(RTNLHandler::kRequestLink, link_callback_.get())); |
| 68 | RTNLHandler::GetInstance()->RequestDump(RTNLHandler::kRequestLink); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 71 | void DeviceInfo::Stop() { |
| 72 | link_listener_.reset(NULL); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 75 | Device::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 Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 84 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 85 | 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 Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 120 | void DeviceInfo::AddLinkMsgHandler(struct nlmsghdr *hdr) { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 121 | struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr)); |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 122 | base::hash_map<int, DeviceRefPtr>::iterator ndev = |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 123 | devices_.find(msg->ifi_index); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 124 | int bytes = IFLA_PAYLOAD(hdr); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 125 | int dev_index = msg->ifi_index; |
| 126 | struct rtattr *rta; |
| 127 | int rta_bytes; |
| 128 | char *link_name = NULL; |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 129 | DeviceRefPtr device; |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 130 | Device::Technology technology = Device::kUnknown; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 131 | bool is_stub = false; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 132 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 133 | VLOG(2) << __func__; |
| 134 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 135 | 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 Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 147 | VLOG(2) << "add link index " << dev_index << " name " << link_name; |
| 148 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 149 | if (link_name != NULL) |
| 150 | technology = GetDeviceTechnology(link_name); |
| 151 | |
| 152 | switch (technology) { |
| 153 | case Device::kEthernet: |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 154 | device = new Ethernet(control_interface_, dispatcher_, manager_, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 155 | link_name, dev_index); |
| 156 | break; |
| 157 | case Device::kWifi: |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 158 | device = new WiFi(control_interface_, dispatcher_, manager_, |
| 159 | link_name, dev_index); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 160 | break; |
| 161 | default: |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 162 | device = new DeviceStub(control_interface_, dispatcher_, manager_, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 163 | 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 Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 175 | device->LinkEvent(msg->ifi_flags, msg->ifi_change); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 178 | void DeviceInfo::DelLinkMsgHandler(struct nlmsghdr *hdr) { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 179 | struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr)); |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 180 | base::hash_map<int, DeviceRefPtr>::iterator ndev = |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 181 | devices_.find(msg->ifi_index); |
| 182 | int dev_index = msg->ifi_index; |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 183 | DeviceRefPtr device; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 184 | |
| 185 | if (ndev != devices_.end()) { |
| 186 | device = ndev->second; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 187 | manager_->DeregisterDevice(device.get()); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 188 | devices_.erase(ndev); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 189 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 192 | void 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 Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 197 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | } // namespace shill |