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