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