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