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 | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame^] | 34 | #include "shill/rtnl_message.h" |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 35 | #include "shill/service.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 36 | #include "shill/wifi.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 37 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 38 | using base::hash_map; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 39 | using std::string; |
| 40 | |
| 41 | namespace shill { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 42 | |
| 43 | // static |
| 44 | const char *DeviceInfo::kInterfaceUevent= "/sys/class/net/%s/uevent"; |
| 45 | // static |
| 46 | const char *DeviceInfo::kInterfaceDriver= "/sys/class/net/%s/device/driver"; |
| 47 | // static |
| 48 | const char *DeviceInfo::kModemDrivers[] = { |
| 49 | "gobi", |
| 50 | "QCUSBNet2k", |
| 51 | "GobiNet", |
| 52 | NULL |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | DeviceInfo::DeviceInfo(ControlInterface *control_interface, |
| 57 | EventDispatcher *dispatcher, |
| 58 | Manager *manager) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 59 | : control_interface_(control_interface), |
| 60 | dispatcher_(dispatcher), |
| 61 | manager_(manager), |
| 62 | link_callback_(NewCallback(this, &DeviceInfo::LinkMsgHandler)), |
| 63 | link_listener_(NULL) { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 66 | DeviceInfo::~DeviceInfo() {} |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 67 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 68 | void DeviceInfo::AddDeviceToBlackList(const string &device_name) { |
| 69 | black_list_.insert(device_name); |
| 70 | } |
| 71 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 72 | void DeviceInfo::Start() { |
| 73 | link_listener_.reset( |
| 74 | new RTNLListener(RTNLHandler::kRequestLink, link_callback_.get())); |
| 75 | RTNLHandler::GetInstance()->RequestDump(RTNLHandler::kRequestLink); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 78 | void DeviceInfo::Stop() { |
| 79 | link_listener_.reset(NULL); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 82 | void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) { |
| 83 | VLOG(2) << __func__ << "(" << device->link_name() << ", " |
| 84 | << device->interface_index() << ")"; |
| 85 | CHECK(!ContainsKey(devices_, device->interface_index())); |
| 86 | devices_[device->interface_index()] = device; |
| 87 | if (device->TechnologyIs(Device::kCellular) || |
| 88 | device->TechnologyIs(Device::kEthernet) || |
| 89 | device->TechnologyIs(Device::kWifi)) { |
| 90 | manager_->RegisterDevice(device); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | DeviceRefPtr DeviceInfo::GetDevice(int interface_index) { |
| 95 | hash_map<int, DeviceRefPtr>::iterator iter = devices_.find(interface_index); |
| 96 | if (iter == devices_.end()) { |
| 97 | return NULL; |
| 98 | } |
| 99 | return iter->second; |
| 100 | } |
| 101 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame^] | 102 | Device::Technology DeviceInfo::GetDeviceTechnology(const string &iface_name) { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 103 | char contents[1024]; |
| 104 | int length; |
| 105 | int fd; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame^] | 106 | string uevent_file = StringPrintf(kInterfaceUevent, iface_name.c_str()); |
| 107 | string driver_file = StringPrintf(kInterfaceDriver, iface_name.c_str()); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 108 | const char *wifi_type; |
| 109 | const char *driver_name; |
| 110 | int modem_idx; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 111 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 112 | fd = open(uevent_file.c_str(), O_RDONLY); |
| 113 | if (fd < 0) |
| 114 | return Device::kUnknown; |
| 115 | |
| 116 | length = read(fd, contents, sizeof(contents) - 1); |
| 117 | if (length < 0) |
| 118 | return Device::kUnknown; |
| 119 | |
| 120 | /* |
| 121 | * If the "uevent" file contains the string "DEVTYPE=wlan\n" at the |
| 122 | * start of the file or after a newline, we can safely assume this |
| 123 | * is a wifi device. |
| 124 | */ |
| 125 | contents[length] = '\0'; |
| 126 | wifi_type = strstr(contents, "DEVTYPE=wlan\n"); |
| 127 | if (wifi_type != NULL && (wifi_type == contents || wifi_type[-1] == '\n')) |
| 128 | return Device::kWifi; |
| 129 | |
| 130 | length = readlink(driver_file.c_str(), contents, sizeof(contents)-1); |
| 131 | if (length < 0) |
| 132 | return Device::kUnknown; |
| 133 | |
| 134 | contents[length] = '\0'; |
| 135 | driver_name = strrchr(contents, '/'); |
| 136 | if (driver_name != NULL) { |
| 137 | driver_name++; |
| 138 | // See if driver for this interface is in a list of known modem driver names |
| 139 | for (modem_idx = 0; kModemDrivers[modem_idx] != NULL; modem_idx++) |
| 140 | if (strcmp(driver_name, kModemDrivers[modem_idx]) == 0) |
| 141 | return Device::kCellular; |
| 142 | } |
| 143 | |
| 144 | return Device::kEthernet; |
| 145 | } |
| 146 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame^] | 147 | void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) { |
| 148 | DCHECK(msg.type() == RTNLMessage::kMessageTypeLink && |
| 149 | msg.mode() == RTNLMessage::kMessageModeAdd); |
| 150 | int dev_index = msg.interface_index(); |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 151 | Device::Technology technology = Device::kUnknown; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 152 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 153 | VLOG(2) << __func__; |
| 154 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 155 | DeviceRefPtr device = GetDevice(dev_index); |
| 156 | if (!device.get()) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame^] | 157 | if (!msg.HasAttribute(IFLA_IFNAME)) { |
| 158 | LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!"; |
| 159 | return; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 160 | } |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame^] | 161 | ByteString b(msg.GetAttribute(IFLA_IFNAME)); |
| 162 | string link_name(reinterpret_cast<const char*>(b.GetConstData())); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 163 | VLOG(2) << "add link index " << dev_index << " name " << link_name; |
| 164 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame^] | 165 | if (!link_name.empty()) { |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 166 | if (ContainsKey(black_list_, link_name)) { |
| 167 | technology = Device::kBlacklisted; |
| 168 | } else { |
| 169 | technology = GetDeviceTechnology(link_name); |
| 170 | } |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 171 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 172 | |
| 173 | switch (technology) { |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 174 | case Device::kCellular: |
| 175 | // Cellular devices are managed by ModemInfo. |
| 176 | VLOG(2) << "Cellular link " << link_name << " at index " << dev_index |
| 177 | << " ignored."; |
| 178 | return; |
| 179 | case Device::kEthernet: |
| 180 | device = new Ethernet(control_interface_, dispatcher_, manager_, |
| 181 | link_name, dev_index); |
| 182 | break; |
| 183 | case Device::kWifi: |
| 184 | device = new WiFi(control_interface_, dispatcher_, manager_, |
| 185 | link_name, dev_index); |
| 186 | break; |
| 187 | default: |
| 188 | device = new DeviceStub(control_interface_, dispatcher_, manager_, |
| 189 | link_name, dev_index, technology); |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 190 | break; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 191 | } |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 192 | RegisterDevice(device); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame^] | 195 | device->LinkEvent(msg.link_status().flags, msg.link_status().change); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame^] | 198 | void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) { |
| 199 | DCHECK(msg.type() == RTNLMessage::kMessageTypeLink && |
| 200 | msg.mode() == RTNLMessage::kMessageModeDelete); |
| 201 | RemoveDevice(msg.interface_index()); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 202 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 203 | |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 204 | void DeviceInfo::RemoveDevice(int interface_index) { |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 205 | hash_map<int, DeviceRefPtr>::iterator ndev = devices_.find(interface_index); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 206 | if (ndev != devices_.end()) { |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 207 | VLOG(2) << "Removing device index: " << interface_index; |
| 208 | manager_->DeregisterDevice(ndev->second); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 209 | devices_.erase(ndev); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 210 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame^] | 213 | void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) { |
| 214 | DCHECK(msg.type() == RTNLMessage::kMessageTypeLink); |
| 215 | if (msg.mode() == RTNLMessage::kMessageModeAdd) { |
| 216 | AddLinkMsgHandler(msg); |
| 217 | } else if (msg.mode() == RTNLMessage::kMessageModeDelete) { |
| 218 | DelLinkMsgHandler(msg); |
| 219 | } else { |
| 220 | NOTREACHED(); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 221 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | } // namespace shill |