Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 5 | #include "shill/device_info.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 6 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 7 | #include <arpa/inet.h> |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 8 | #include <fcntl.h> |
| 9 | #include <linux/if_tun.h> |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 10 | #include <linux/netlink.h> |
| 11 | #include <linux/rtnetlink.h> |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 12 | #include <net/if.h> |
| 13 | #include <net/if_arp.h> |
| 14 | #include <netinet/ether.h> |
| 15 | #include <string.h> |
| 16 | #include <sys/ioctl.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <time.h> |
| 19 | #include <unistd.h> |
| 20 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 23 | #include <base/bind.h> |
Paul Stewart | bf1861b | 2011-08-23 15:45:35 -0700 | [diff] [blame] | 24 | #include <base/file_util.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 25 | #include <base/memory/scoped_ptr.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 26 | #include <base/stl_util.h> |
Paul Stewart | 2001a42 | 2011-12-15 10:20:09 -0800 | [diff] [blame] | 27 | #include <base/string_number_conversions.h> |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 28 | #include <base/string_util.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 29 | #include <base/stringprintf.h> |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 30 | |
| 31 | #include "shill/control_interface.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 32 | #include "shill/device.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 33 | #include "shill/device_stub.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 34 | #include "shill/ethernet.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 35 | #include "shill/logging.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 36 | #include "shill/manager.h" |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 37 | #include "shill/routing_table.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 38 | #include "shill/rtnl_handler.h" |
| 39 | #include "shill/rtnl_listener.h" |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 40 | #include "shill/rtnl_message.h" |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 41 | #include "shill/service.h" |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 42 | #include "shill/virtio_ethernet.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 43 | #include "shill/wifi.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 44 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 45 | using base::Bind; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 46 | using base::StringPrintf; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 47 | using base::Unretained; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 48 | using std::map; |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 49 | using std::set; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 50 | using std::string; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 51 | using std::vector; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 52 | |
| 53 | namespace shill { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 54 | |
| 55 | // static |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 56 | const char DeviceInfo::kModemPseudoDeviceNamePrefix[] = "pseudomodem"; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 57 | const char DeviceInfo::kDeviceInfoRoot[] = "/sys/class/net"; |
Ben Chan | 4e64d2d | 2012-05-16 00:02:25 -0700 | [diff] [blame] | 58 | const char DeviceInfo::kDriverCdcEther[] = "cdc_ether"; |
| 59 | const char DeviceInfo::kDriverGdmWiMax[] = "gdm_wimax"; |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 60 | const char DeviceInfo::kDriverVirtioNet[] = "virtio_net"; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 61 | const char DeviceInfo::kInterfaceUevent[] = "uevent"; |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 62 | const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n"; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 63 | const char DeviceInfo::kInterfaceDevice[] = "device"; |
| 64 | const char DeviceInfo::kInterfaceDriver[] = "device/driver"; |
| 65 | const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags"; |
| 66 | const char DeviceInfo::kInterfaceType[] = "type"; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 67 | const char *DeviceInfo::kModemDrivers[] = { |
| 68 | "gobi", |
| 69 | "QCUSBNet2k", |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 70 | "GobiNet" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 71 | }; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 72 | const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun"; |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 73 | const int DeviceInfo::kDelayedDeviceCreationSeconds = 5; |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 74 | const int DeviceInfo::kRequestLinkStatisticsIntervalSeconds = 60; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 75 | |
| 76 | DeviceInfo::DeviceInfo(ControlInterface *control_interface, |
| 77 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 78 | Metrics *metrics, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 79 | Manager *manager) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 80 | : control_interface_(control_interface), |
| 81 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 82 | metrics_(metrics), |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 83 | manager_(manager), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 84 | link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))), |
| 85 | address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 86 | link_listener_(NULL), |
| 87 | address_listener_(NULL), |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 88 | device_info_root_(kDeviceInfoRoot), |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 89 | routing_table_(RoutingTable::GetInstance()), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 90 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 93 | DeviceInfo::~DeviceInfo() {} |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 94 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 95 | void DeviceInfo::AddDeviceToBlackList(const string &device_name) { |
| 96 | black_list_.insert(device_name); |
| 97 | } |
| 98 | |
Eric Shienbrood | 5e628a5 | 2012-03-21 16:56:59 -0400 | [diff] [blame] | 99 | bool DeviceInfo::IsDeviceBlackListed(const string &device_name) { |
| 100 | return ContainsKey(black_list_, device_name); |
| 101 | } |
| 102 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 103 | void DeviceInfo::Start() { |
| 104 | link_listener_.reset( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 105 | new RTNLListener(RTNLHandler::kRequestLink, link_callback_)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 106 | address_listener_.reset( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 107 | new RTNLListener(RTNLHandler::kRequestAddr, address_callback_)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 108 | rtnl_handler_->RequestDump(RTNLHandler::kRequestLink | |
| 109 | RTNLHandler::kRequestAddr); |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 110 | request_link_statistics_callback_.Reset( |
| 111 | Bind(&DeviceInfo::RequestLinkStatistics, AsWeakPtr())); |
| 112 | dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(), |
| 113 | kRequestLinkStatisticsIntervalSeconds * 1000); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 116 | void DeviceInfo::Stop() { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 117 | link_listener_.reset(); |
| 118 | address_listener_.reset(); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 119 | infos_.clear(); |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 120 | request_link_statistics_callback_.Cancel(); |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 121 | delayed_devices_callback_.Cancel(); |
| 122 | delayed_devices_.clear(); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 125 | void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 126 | SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", " |
| 127 | << device->interface_index() << ")"; |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 128 | delayed_devices_.erase(device->interface_index()); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 129 | CHECK(!GetDevice(device->interface_index()).get()); |
| 130 | infos_[device->interface_index()].device = device; |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 131 | if ((device->technology() == Technology::kCellular) || |
| 132 | (device->technology() == Technology::kEthernet) || |
| 133 | (device->technology() == Technology::kWifi) || |
| 134 | (device->technology() == Technology::kWiMax)) { |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 135 | manager_->RegisterDevice(device); |
| 136 | } |
| 137 | } |
| 138 | |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 139 | void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) { |
| 140 | int interface_index = device->interface_index(); |
| 141 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 142 | SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", " |
| 143 | << interface_index << ")"; |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 144 | CHECK((device->technology() == Technology::kCellular) || |
| 145 | (device->technology() == Technology::kWiMax)); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 146 | |
| 147 | // Release reference to the device |
| 148 | map<int, Info>::iterator iter = infos_.find(interface_index); |
| 149 | if (iter != infos_.end()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 150 | SLOG(Device, 2) << "Removing device from info for index: " |
| 151 | << interface_index; |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 152 | manager_->DeregisterDevice(device); |
| 153 | // Release the reference to the device, but maintain the mapping |
| 154 | // for the index. That will be cleaned up by an RTNL message. |
| 155 | iter->second.device = NULL; |
| 156 | } |
| 157 | } |
| 158 | |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 159 | FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name, |
| 160 | const string &path_name) { |
| 161 | return device_info_root_.Append(iface_name).Append(path_name); |
| 162 | } |
| 163 | |
| 164 | bool DeviceInfo::GetDeviceInfoContents(const string &iface_name, |
| 165 | const string &path_name, |
| 166 | string *contents_out) { |
| 167 | return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name), |
| 168 | contents_out); |
| 169 | } |
| 170 | bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name, |
Ben Chan | 4e64d2d | 2012-05-16 00:02:25 -0700 | [diff] [blame] | 171 | const string &path_name, |
| 172 | FilePath *path_out) { |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 173 | return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name), |
| 174 | path_out); |
| 175 | } |
| 176 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 177 | Technology::Identifier DeviceInfo::GetDeviceTechnology( |
| 178 | const string &iface_name) { |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 179 | string type_string; |
| 180 | int arp_type = ARPHRD_VOID;; |
| 181 | if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) && |
| 182 | TrimString(type_string, "\n", &type_string) && |
| 183 | !base::StringToInt(type_string, &arp_type)) { |
| 184 | arp_type = ARPHRD_VOID; |
| 185 | } |
| 186 | |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 187 | string contents; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 188 | if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) { |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 189 | LOG(INFO) << StringPrintf("%s: device %s has no uevent file", |
| 190 | __func__, iface_name.c_str()); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 191 | return Technology::kUnknown; |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 192 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 193 | |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 194 | // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the |
| 195 | // start of the file or after a newline, we can safely assume this |
| 196 | // is a wifi device. |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 197 | if (contents.find(kInterfaceUeventWifiSignature) != string::npos) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 198 | SLOG(Device, 2) |
| 199 | << StringPrintf("%s: device %s has wifi signature in uevent file", |
| 200 | __func__, iface_name.c_str()); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 201 | if (arp_type == ARPHRD_IEEE80211_RADIOTAP) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 202 | SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode", |
| 203 | __func__, iface_name.c_str()); |
Paul Stewart | 2001a42 | 2011-12-15 10:20:09 -0800 | [diff] [blame] | 204 | return Technology::kWiFiMonitor; |
| 205 | } |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 206 | return Technology::kWifi; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Jason Glasgow | 7904d14 | 2012-05-18 13:36:20 -0400 | [diff] [blame] | 209 | // Special case for pseudo modems which are used for testing |
| 210 | if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) { |
| 211 | SLOG(Device, 2) << StringPrintf( |
| 212 | "%s: device %s is a pseudo modem for testing", |
| 213 | __func__, iface_name.c_str()); |
| 214 | return Technology::kCellular; |
| 215 | } |
| 216 | |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 217 | FilePath driver_path; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 218 | if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 219 | SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink", |
| 220 | __func__, iface_name.c_str()); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 221 | if (arp_type == ARPHRD_LOOPBACK) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 222 | SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device", |
| 223 | __func__, iface_name.c_str()); |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 224 | return Technology::kLoopback; |
| 225 | } |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 226 | if (arp_type == ARPHRD_PPP) { |
| 227 | SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device", |
| 228 | __func__, iface_name.c_str()); |
| 229 | return Technology::kPPP; |
| 230 | } |
| 231 | string tun_flags_str; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 232 | int tun_flags = 0; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 233 | if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) && |
| 234 | TrimString(tun_flags_str, "\n", &tun_flags_str) && |
| 235 | base::HexStringToInt(tun_flags_str, &tun_flags) && |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 236 | (tun_flags & IFF_TUN)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 237 | SLOG(Device, 2) << StringPrintf("%s: device %s is tun device", |
| 238 | __func__, iface_name.c_str()); |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 239 | return Technology::kTunnel; |
| 240 | } |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 241 | return Technology::kUnknown; |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | string driver_name(driver_path.BaseName().value()); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 245 | // See if driver for this interface is in a list of known modem driver names. |
| 246 | for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers); |
| 247 | ++modem_idx) { |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 248 | if (driver_name == kModemDrivers[modem_idx]) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 249 | SLOG(Device, 2) |
| 250 | << StringPrintf("%s: device %s is matched with modem driver %s", |
| 251 | __func__, iface_name.c_str(), driver_name.c_str()); |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 252 | return Technology::kCellular; |
| 253 | } |
| 254 | } |
| 255 | |
Ben Chan | 4e64d2d | 2012-05-16 00:02:25 -0700 | [diff] [blame] | 256 | if (driver_name == kDriverGdmWiMax) { |
| 257 | SLOG(Device, 2) << StringPrintf("%s: device %s is a WiMAX device", |
| 258 | __func__, iface_name.c_str()); |
| 259 | return Technology::kWiMax; |
| 260 | } |
| 261 | |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 262 | // For cdc_ether devices, make sure it's a modem because this driver |
| 263 | // can be used for other ethernet devices. |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 264 | if (driver_name == kDriverCdcEther) { |
| 265 | if (IsCdcEtherModemDevice(iface_name)) { |
| 266 | LOG(INFO) << StringPrintf("%s: device %s is a modem cdc_ether " |
| 267 | "device", __func__, iface_name.c_str()); |
| 268 | return Technology::kCellular; |
| 269 | } |
| 270 | SLOG(Device, 2) << StringPrintf("%s: device %s is a cdc_ether " |
| 271 | "device", __func__, iface_name.c_str()); |
| 272 | return Technology::kCDCEthernet; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 273 | } |
| 274 | |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 275 | // Special case for the virtio driver, used when run under KVM. See also |
| 276 | // the comment in VirtioEthernet::Start. |
| 277 | if (driver_name == kDriverVirtioNet) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 278 | SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet", |
| 279 | __func__, iface_name.c_str()); |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 280 | return Technology::kVirtioEthernet; |
| 281 | } |
| 282 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 283 | SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, " |
| 284 | "is defaulted to type ethernet", |
| 285 | __func__, iface_name.c_str(), |
| 286 | driver_name.c_str()); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 287 | return Technology::kEthernet; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 290 | bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) { |
| 291 | // A cdc_ether device is a modem device if it also exposes tty interfaces. |
| 292 | // To determine this, we look for the existence of the tty interface in the |
| 293 | // USB device sysfs tree. |
| 294 | // |
| 295 | // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as |
| 296 | // follows: |
| 297 | // |
| 298 | // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2 |
| 299 | // 1-2:1.0 |
| 300 | // tty |
| 301 | // ttyACM0 |
| 302 | // 1-2:1.1 |
| 303 | // net |
| 304 | // usb0 |
| 305 | // 1-2:1.2 |
| 306 | // tty |
| 307 | // ttyACM1 |
| 308 | // ... |
| 309 | // |
| 310 | // /sys/class/net/usb0/device symlinks to |
| 311 | // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1 |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 312 | // |
| 313 | // Note that some modem devices have the tty directory one level deeper |
| 314 | // (eg. E362), so the device tree for the tty interface is: |
| 315 | // /sys/devices/pci0000:00/0000:00:1d.7/usb/1-2/1-2:1.0/ttyUSB0/tty/ttyUSB0 |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 316 | |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 317 | FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 318 | FilePath device_path; |
| 319 | if (!file_util::ReadSymbolicLink(device_file, &device_path)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 320 | SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink", |
| 321 | __func__, iface_name.c_str()); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 322 | return false; |
| 323 | } |
| 324 | if (!device_path.IsAbsolute()) { |
| 325 | device_path = device_file.DirName().Append(device_path); |
| 326 | file_util::AbsolutePath(&device_path); |
| 327 | } |
| 328 | |
| 329 | // Look for tty interface by enumerating all directories under the parent |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 330 | // USB device and see if there's a subdirectory "tty" inside. In other |
| 331 | // words, using the example dir hierarchy above, find |
| 332 | // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty. |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 333 | // If this exists, then this is a modem device. |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 334 | return HasSubdir(device_path.DirName(), FilePath("tty")); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // static |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 338 | bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) { |
| 339 | file_util::FileEnumerator::FileType type = |
| 340 | static_cast<file_util::FileEnumerator::FileType>( |
| 341 | file_util::FileEnumerator::DIRECTORIES | |
| 342 | file_util::FileEnumerator::SHOW_SYM_LINKS); |
| 343 | file_util::FileEnumerator dir_enum(base_dir, true, type); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 344 | for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty(); |
| 345 | curr_dir = dir_enum.Next()) { |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 346 | if (curr_dir.BaseName() == subdir) |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 347 | return true; |
| 348 | } |
| 349 | return false; |
| 350 | } |
| 351 | |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 352 | DeviceRefPtr DeviceInfo::CreateDevice(const string &link_name, |
| 353 | const string &address, |
| 354 | int interface_index, |
| 355 | Technology::Identifier technology) { |
| 356 | DeviceRefPtr device; |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 357 | delayed_devices_.erase(interface_index); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 358 | |
| 359 | switch (technology) { |
| 360 | case Technology::kCellular: |
| 361 | // Cellular devices are managed by ModemInfo. |
| 362 | SLOG(Device, 2) << "Cellular link " << link_name |
| 363 | << " at index " << interface_index |
| 364 | << " -- notifying ModemInfo."; |
Gary Morain | bf74a67 | 2012-07-30 16:27:19 -0700 | [diff] [blame] | 365 | |
| 366 | // The MAC address provided by RTNL is not reliable for Gobi 2K modems. |
| 367 | // Clear it here, and it will be fetched from the kernel is |
| 368 | // GetMacAddress(). |
| 369 | infos_[interface_index].mac_address.Clear(); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 370 | manager_->modem_info()->OnDeviceInfoAvailable(link_name); |
| 371 | break; |
| 372 | case Technology::kEthernet: |
| 373 | device = new Ethernet(control_interface_, dispatcher_, metrics_, |
| 374 | manager_, link_name, address, interface_index); |
| 375 | device->EnableIPv6Privacy(); |
| 376 | break; |
| 377 | case Technology::kVirtioEthernet: |
| 378 | device = new VirtioEthernet(control_interface_, dispatcher_, metrics_, |
| 379 | manager_, link_name, address, |
| 380 | interface_index); |
| 381 | device->EnableIPv6Privacy(); |
| 382 | break; |
| 383 | case Technology::kWifi: |
| 384 | device = new WiFi(control_interface_, dispatcher_, metrics_, manager_, |
| 385 | link_name, address, interface_index); |
| 386 | device->EnableIPv6Privacy(); |
| 387 | break; |
Ben Chan | 4e64d2d | 2012-05-16 00:02:25 -0700 | [diff] [blame] | 388 | case Technology::kWiMax: |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 389 | // WiMax devices are managed by WiMaxProvider. |
| 390 | SLOG(Device, 2) << "WiMax link " << link_name |
| 391 | << " at index " << interface_index |
| 392 | << " -- notifying WiMaxProvider."; |
| 393 | manager_->wimax_provider()->OnDeviceInfoAvailable(link_name); |
Ben Chan | 4e64d2d | 2012-05-16 00:02:25 -0700 | [diff] [blame] | 394 | break; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 395 | case Technology::kPPP: |
| 396 | case Technology::kTunnel: |
| 397 | // Tunnel and PPP devices are managed by the VPN code (PPP for |
| 398 | // l2tpipsec). Notify the VPN Provider of the interface's presence. |
| 399 | // Since CreateDevice is only called once in the lifetime of an |
| 400 | // interface index, this notification will only occur the first |
| 401 | // time the device is seen. |
| 402 | SLOG(Device, 2) << "Tunnel / PPP link " << link_name |
| 403 | << " at index " << interface_index |
| 404 | << " -- notifying VPNProvider."; |
| 405 | if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name, |
| 406 | interface_index) && |
| 407 | technology == Technology::kTunnel) { |
| 408 | // If VPN does not know anything about this tunnel, it is probably |
| 409 | // left over from a previous instance and should not exist. |
| 410 | SLOG(Device, 2) << "Tunnel link is unused. Deleting."; |
| 411 | DeleteInterface(interface_index); |
| 412 | } |
| 413 | break; |
| 414 | case Technology::kLoopback: |
| 415 | // Loopback devices are largely ignored, but we should make sure the |
| 416 | // link is enabled. |
| 417 | SLOG(Device, 2) << "Bringing up loopback device " << link_name |
| 418 | << " at index " << interface_index; |
| 419 | rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP); |
| 420 | return NULL; |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 421 | case Technology::kCDCEthernet: |
| 422 | // CDCEnternet devices are of indeterminate type when they are |
| 423 | // initially created. Some time later, tty devices may or may |
| 424 | // not appear under the same USB device root, which will identify |
| 425 | // it as a modem. Alternatively, ModemManager may discover the |
| 426 | // device and create and register a Cellular device. In either |
| 427 | // case, we should delay creating a Device until we can make a |
| 428 | // better determination of what type this Device should be. |
| 429 | LOG(INFO) << "Delaying creation of device for " << link_name |
| 430 | << " at index " << interface_index; |
| 431 | DelayDeviceCreation(interface_index); |
| 432 | return NULL; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 433 | default: |
| 434 | // We will not manage this device in shill. Do not create a device |
| 435 | // object or do anything to change its state. We create a stub object |
| 436 | // which is useful for testing. |
| 437 | return new DeviceStub(control_interface_, dispatcher_, metrics_, |
| 438 | manager_, link_name, address, interface_index, |
Ben Chan | 4e64d2d | 2012-05-16 00:02:25 -0700 | [diff] [blame] | 439 | technology); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // Reset the routing table and addresses. |
| 443 | routing_table_->FlushRoutes(interface_index); |
| 444 | FlushAddresses(interface_index); |
| 445 | |
| 446 | return device; |
| 447 | } |
| 448 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 449 | void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 450 | DCHECK(msg.type() == RTNLMessage::kTypeLink && |
| 451 | msg.mode() == RTNLMessage::kModeAdd); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 452 | int dev_index = msg.interface_index(); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 453 | Technology::Identifier technology = Technology::kUnknown; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 454 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 455 | unsigned int flags = msg.link_status().flags; |
| 456 | unsigned int change = msg.link_status().change; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 457 | bool new_device = |
| 458 | !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only; |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 459 | SLOG(Device, 2) << __func__ << "(index=" << dev_index |
| 460 | << std::showbase << std::hex |
| 461 | << ", flags=" << flags << ", change=" << change << ")" |
| 462 | << std::dec << std::noshowbase |
| 463 | << ", new_device=" << new_device; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 464 | infos_[dev_index].has_addresses_only = false; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 465 | infos_[dev_index].flags = flags; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 466 | |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 467 | RetrieveLinkStatistics(dev_index, msg); |
| 468 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 469 | DeviceRefPtr device = GetDevice(dev_index); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 470 | if (new_device) { |
| 471 | CHECK(!device); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 472 | if (!msg.HasAttribute(IFLA_IFNAME)) { |
| 473 | LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!"; |
| 474 | return; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 475 | } |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 476 | ByteString b(msg.GetAttribute(IFLA_IFNAME)); |
| 477 | string link_name(reinterpret_cast<const char*>(b.GetConstData())); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 478 | SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name; |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 479 | infos_[dev_index].name = link_name; |
| 480 | indices_[link_name] = dev_index; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 481 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 482 | if (!link_name.empty()) { |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 483 | if (ContainsKey(black_list_, link_name)) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 484 | technology = Technology::kBlacklisted; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 485 | } else { |
| 486 | technology = GetDeviceTechnology(link_name); |
| 487 | } |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 488 | } |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 489 | string address; |
| 490 | if (msg.HasAttribute(IFLA_ADDRESS)) { |
| 491 | infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS); |
| 492 | address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode()); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 493 | SLOG(Device, 2) << "link index " << dev_index << " address " |
| 494 | << infos_[dev_index].mac_address.HexEncode(); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 495 | } else if (technology != Technology::kTunnel && |
| 496 | technology != Technology::kPPP) { |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 497 | LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!"; |
| 498 | return; |
| 499 | } |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 500 | device = CreateDevice(link_name, address, dev_index, technology); |
| 501 | if (device) { |
| 502 | RegisterDevice(device); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 503 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 504 | } |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 505 | if (device) { |
| 506 | device->LinkEvent(flags, change); |
| 507 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 510 | void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 511 | SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")"; |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 512 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 513 | DCHECK(msg.type() == RTNLMessage::kTypeLink && |
| 514 | msg.mode() == RTNLMessage::kModeDelete); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 515 | SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() |
| 516 | << std::showbase << std::hex |
| 517 | << ", flags=" << msg.link_status().flags |
| 518 | << ", change=" << msg.link_status().change << ")"; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 519 | RemoveInfo(msg.interface_index()); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 520 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 521 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 522 | DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const { |
| 523 | const Info *info = GetInfo(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 524 | return info ? info->device : NULL; |
| 525 | } |
| 526 | |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 527 | int DeviceInfo::GetIndex(const string &interface_name) const { |
| 528 | map<string, int>::const_iterator it = indices_.find(interface_name); |
| 529 | return it == indices_.end() ? -1 : it->second; |
| 530 | } |
| 531 | |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 532 | bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const { |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 533 | const Info *info = GetInfo(interface_index); |
| 534 | if (!info) { |
| 535 | return false; |
| 536 | } |
Gary Morain | bf74a67 | 2012-07-30 16:27:19 -0700 | [diff] [blame] | 537 | // |mac_address| from RTNL is not used for some devices, in which case it will |
| 538 | // be empty here. |
| 539 | if (!info->mac_address.IsEmpty()) { |
| 540 | *address = info->mac_address; |
| 541 | return true; |
| 542 | } |
| 543 | |
| 544 | // Ask the kernel for the MAC address. |
| 545 | *address = GetMACAddressFromKernel(interface_index); |
| 546 | return !address->IsEmpty(); |
| 547 | } |
| 548 | |
| 549 | ByteString DeviceInfo::GetMACAddressFromKernel(int interface_index) const { |
| 550 | ByteString mac_address; |
| 551 | mac_address.Clear(); |
| 552 | // TODO(gmorain): Use ScopedSocketCloser instead. |
| 553 | const int fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 554 | if (fd < 0) { |
| 555 | LOG(ERROR) << __func__ << ": Unable to open socket: " << fd; |
| 556 | return mac_address; |
| 557 | } |
| 558 | struct ifreq ifr; |
| 559 | memset(&ifr, 0, sizeof(ifr)); |
| 560 | ifr.ifr_ifindex = interface_index; |
| 561 | int err = HANDLE_EINTR(ioctl(fd, SIOCGIFNAME, &ifr)); |
| 562 | if (err < 0) { |
| 563 | LOG(ERROR) << __func__ << ": Unable to read ifname: " << errno; |
| 564 | close(fd); |
| 565 | return mac_address; |
| 566 | } |
| 567 | err = HANDLE_EINTR(ioctl(fd, SIOCGIFHWADDR, &ifr)); |
| 568 | close(fd); |
| 569 | if (err < 0) { |
| 570 | LOG(ERROR) << __func__ << ": Unable to read MAC address: " << errno; |
| 571 | return mac_address; |
| 572 | } |
| 573 | mac_address.Resize(IFHWADDRLEN); |
| 574 | memcpy(mac_address.GetData(), ifr.ifr_hwaddr.sa_data, IFHWADDRLEN); |
| 575 | return mac_address; |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 578 | bool DeviceInfo::GetAddresses(int interface_index, |
| 579 | vector<AddressData> *addresses) const { |
| 580 | const Info *info = GetInfo(interface_index); |
| 581 | if (!info) { |
| 582 | return false; |
| 583 | } |
| 584 | *addresses = info->ip_addresses; |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | void DeviceInfo::FlushAddresses(int interface_index) const { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 589 | SLOG(Device, 2) << __func__ << "(" << interface_index << ")"; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 590 | const Info *info = GetInfo(interface_index); |
| 591 | if (!info) { |
| 592 | return; |
| 593 | } |
| 594 | const vector<AddressData> &addresses = info->ip_addresses; |
| 595 | vector<AddressData>::const_iterator iter; |
| 596 | for (iter = addresses.begin(); iter != addresses.end(); ++iter) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 597 | if (iter->address.family() == IPAddress::kFamilyIPv4 || |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 598 | (iter->scope == RT_SCOPE_UNIVERSE && |
| 599 | (iter->flags & ~IFA_F_TEMPORARY) == 0)) { |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 600 | SLOG(Device, 2) << __func__ << ": removing ip address " |
| 601 | << iter->address.ToString() |
| 602 | << " from interface " << interface_index; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 603 | rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 608 | bool DeviceInfo::HasOtherAddress( |
| 609 | int interface_index, const IPAddress &this_address) const { |
| 610 | SLOG(Device, 3) << __func__ << "(" << interface_index << ")"; |
| 611 | const Info *info = GetInfo(interface_index); |
| 612 | if (!info) { |
| 613 | return false; |
| 614 | } |
| 615 | const vector<AddressData> &addresses = info->ip_addresses; |
| 616 | vector<AddressData>::const_iterator iter; |
| 617 | bool has_other_address = false; |
| 618 | bool has_this_address = false; |
| 619 | for (iter = addresses.begin(); iter != addresses.end(); ++iter) { |
| 620 | if (iter->address.family() != this_address.family()) { |
| 621 | continue; |
| 622 | } |
| 623 | if (iter->address.address().Equals(this_address.address())) { |
| 624 | has_this_address = true; |
| 625 | } else if (this_address.family() == IPAddress::kFamilyIPv4) { |
| 626 | has_other_address = true; |
| 627 | } else if ((iter->scope == RT_SCOPE_UNIVERSE && |
| 628 | (iter->flags & IFA_F_TEMPORARY) == 0)) { |
| 629 | has_other_address = true; |
| 630 | } |
| 631 | } |
| 632 | return has_other_address && !has_this_address; |
| 633 | } |
| 634 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 635 | bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const { |
| 636 | const Info *info = GetInfo(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 637 | if (!info) { |
| 638 | return false; |
| 639 | } |
| 640 | *flags = info->flags; |
| 641 | return true; |
| 642 | } |
| 643 | |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 644 | bool DeviceInfo::GetByteCounts(int interface_index, |
| 645 | uint64 *rx_bytes, |
| 646 | uint64 *tx_bytes) const { |
| 647 | const Info *info = GetInfo(interface_index); |
| 648 | if (!info) { |
| 649 | return false; |
| 650 | } |
| 651 | *rx_bytes = info->rx_bytes; |
| 652 | *tx_bytes = info->tx_bytes; |
| 653 | return true; |
| 654 | } |
| 655 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 656 | bool DeviceInfo::CreateTunnelInterface(string *interface_name) const { |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 657 | int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR)); |
| 658 | if (fd < 0) { |
| 659 | PLOG(ERROR) << "failed to open " << kTunDeviceName; |
| 660 | return false; |
| 661 | } |
| 662 | file_util::ScopedFD scoped_fd(&fd); |
| 663 | |
| 664 | struct ifreq ifr; |
| 665 | memset(&ifr, 0, sizeof(ifr)); |
| 666 | ifr.ifr_flags = IFF_TUN | IFF_NO_PI; |
| 667 | if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) { |
| 668 | PLOG(ERROR) << "failed to create tunnel interface"; |
| 669 | return false; |
| 670 | } |
| 671 | |
| 672 | if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) { |
| 673 | PLOG(ERROR) << "failed to set tunnel interface to be persistent"; |
| 674 | return false; |
| 675 | } |
| 676 | |
| 677 | *interface_name = string(ifr.ifr_name); |
| 678 | |
| 679 | return true; |
| 680 | } |
| 681 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 682 | bool DeviceInfo::DeleteInterface(int interface_index) const { |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 683 | return rtnl_handler_->RemoveInterface(interface_index); |
| 684 | } |
| 685 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 686 | const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const { |
| 687 | map<int, Info>::const_iterator iter = infos_.find(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 688 | if (iter == infos_.end()) { |
| 689 | return NULL; |
| 690 | } |
| 691 | return &iter->second; |
| 692 | } |
| 693 | |
| 694 | void DeviceInfo::RemoveInfo(int interface_index) { |
| 695 | map<int, Info>::iterator iter = infos_.find(interface_index); |
| 696 | if (iter != infos_.end()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 697 | SLOG(Device, 2) << "Removing info for device index: " << interface_index; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 698 | if (iter->second.device.get()) { |
| 699 | manager_->DeregisterDevice(iter->second.device); |
| 700 | } |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 701 | indices_.erase(iter->second.name); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 702 | infos_.erase(iter); |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 703 | delayed_devices_.erase(interface_index); |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 704 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 705 | SLOG(Device, 2) << __func__ << ": Unknown device index: " |
| 706 | << interface_index; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 707 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 710 | void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 711 | DCHECK(msg.type() == RTNLMessage::kTypeLink); |
| 712 | if (msg.mode() == RTNLMessage::kModeAdd) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 713 | AddLinkMsgHandler(msg); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 714 | } else if (msg.mode() == RTNLMessage::kModeDelete) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 715 | DelLinkMsgHandler(msg); |
| 716 | } else { |
| 717 | NOTREACHED(); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 718 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 721 | void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 722 | SLOG(Device, 2) << __func__; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 723 | DCHECK(msg.type() == RTNLMessage::kTypeAddress); |
| 724 | int interface_index = msg.interface_index(); |
| 725 | if (!ContainsKey(infos_, interface_index)) { |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 726 | SLOG(Device, 2) << "Got advance address information for unknown index " |
| 727 | << interface_index; |
| 728 | infos_[interface_index].has_addresses_only = true; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 729 | } |
| 730 | const RTNLMessage::AddressStatus &status = msg.address_status(); |
| 731 | IPAddress address(msg.family(), |
Ben Chan | 682c5d4 | 2012-06-18 14:11:04 -0700 | [diff] [blame] | 732 | msg.HasAttribute(IFA_LOCAL) ? |
| 733 | msg.GetAttribute(IFA_LOCAL) : msg.GetAttribute(IFA_ADDRESS), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 734 | status.prefix_len); |
| 735 | |
Ben Chan | 682c5d4 | 2012-06-18 14:11:04 -0700 | [diff] [blame] | 736 | SLOG_IF(Device, 2, msg.HasAttribute(IFA_LOCAL)) |
| 737 | << "Found local address attribute for interface " << interface_index; |
| 738 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 739 | vector<AddressData> &address_list = infos_[interface_index].ip_addresses; |
| 740 | vector<AddressData>::iterator iter; |
| 741 | for (iter = address_list.begin(); iter != address_list.end(); ++iter) { |
| 742 | if (address.Equals(iter->address)) { |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | if (iter != address_list.end()) { |
| 747 | if (msg.mode() == RTNLMessage::kModeDelete) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 748 | SLOG(Device, 2) << "Delete address for interface " << interface_index; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 749 | address_list.erase(iter); |
| 750 | } else { |
| 751 | iter->flags = status.flags; |
| 752 | iter->scope = status.scope; |
| 753 | } |
| 754 | } else if (msg.mode() == RTNLMessage::kModeAdd) { |
| 755 | address_list.push_back(AddressData(address, status.flags, status.scope)); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 756 | SLOG(Device, 2) << "Add address " << address.ToString() |
| 757 | << " for interface " << interface_index; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 761 | void DeviceInfo::DelayDeviceCreation(int interface_index) { |
| 762 | delayed_devices_.insert(interface_index); |
| 763 | delayed_devices_callback_.Reset( |
| 764 | Bind(&DeviceInfo::DelayedDeviceCreationTask, AsWeakPtr())); |
| 765 | dispatcher_->PostDelayedTask(delayed_devices_callback_.callback(), |
| 766 | kDelayedDeviceCreationSeconds * 1000); |
| 767 | } |
| 768 | |
| 769 | // Re-evaluate the technology type for each delayed device. |
| 770 | void DeviceInfo::DelayedDeviceCreationTask() { |
| 771 | while (!delayed_devices_.empty()) { |
| 772 | set<int>::iterator it = delayed_devices_.begin(); |
| 773 | int dev_index = *it; |
| 774 | delayed_devices_.erase(it); |
| 775 | |
| 776 | DCHECK(ContainsKey(infos_, dev_index)); |
| 777 | DCHECK(!GetDevice(dev_index)); |
| 778 | |
| 779 | const string &link_name = infos_[dev_index].name; |
| 780 | Technology::Identifier technology = GetDeviceTechnology(link_name); |
| 781 | |
| 782 | if (technology == Technology::kCDCEthernet) { |
| 783 | LOG(INFO) << "In " << __func__ << ": device " << link_name |
| 784 | << " is now assumed to be regular Ethernet."; |
| 785 | technology = Technology::kEthernet; |
| 786 | } else if (technology != Technology::kCellular) { |
| 787 | LOG(WARNING) << "In " << __func__ << ": device " << link_name |
| 788 | << " is unexpected technology " |
| 789 | << Technology::NameFromIdentifier(technology); |
| 790 | } |
| 791 | string address = |
| 792 | StringToLowerASCII(infos_[dev_index].mac_address.HexEncode()); |
| 793 | DCHECK(!address.empty()); |
| 794 | |
| 795 | DeviceRefPtr device = CreateDevice(link_name, address, dev_index, |
| 796 | technology); |
| 797 | if (device) { |
| 798 | RegisterDevice(device); |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 803 | void DeviceInfo::RetrieveLinkStatistics(int interface_index, |
| 804 | const RTNLMessage &msg) { |
| 805 | if (!msg.HasAttribute(IFLA_STATS64)) { |
| 806 | return; |
| 807 | } |
| 808 | ByteString stats_bytes(msg.GetAttribute(IFLA_STATS64)); |
| 809 | struct rtnl_link_stats64 stats; |
| 810 | if (stats_bytes.GetLength() < sizeof(stats)) { |
| 811 | LOG(WARNING) << "Link statistics size is too small: " |
| 812 | << stats_bytes.GetLength() << " < " << sizeof(stats); |
| 813 | return; |
| 814 | } |
| 815 | |
| 816 | memcpy(&stats, stats_bytes.GetConstData(), sizeof(stats)); |
| 817 | SLOG(Device, 2) << "Link statistics for " |
| 818 | << " interface index " << interface_index << ": " |
| 819 | << "receive: " << stats.rx_bytes << "; " |
| 820 | << "transmit: " << stats.tx_bytes << "."; |
| 821 | infos_[interface_index].rx_bytes = stats.rx_bytes; |
| 822 | infos_[interface_index].tx_bytes = stats.tx_bytes; |
| 823 | } |
| 824 | |
| 825 | void DeviceInfo::RequestLinkStatistics() { |
| 826 | rtnl_handler_->RequestDump(RTNLHandler::kRequestLink); |
| 827 | dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(), |
| 828 | kRequestLinkStatisticsIntervalSeconds * 1000); |
| 829 | } |
| 830 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 831 | } // namespace shill |