mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -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 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 5 | #include "shill/device.h" |
| 6 | |
Matthew Wein | 08add48 | 2015-04-20 13:26:48 -0700 | [diff] [blame] | 7 | #include <errno.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 8 | #include <netinet/in.h> |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 9 | #include <linux/if.h> // NOLINT - Needs definitions from netinet/in.h |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 10 | #include <stdio.h> |
Paul Stewart | 208a97e | 2015-05-13 09:11:12 -0700 | [diff] [blame] | 11 | #include <sys/param.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 12 | #include <time.h> |
Matthew Wein | 08add48 | 2015-04-20 13:26:48 -0700 | [diff] [blame] | 13 | #include <unistd.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 14 | |
Samuel Tan | 787a1ce | 2014-11-11 17:17:27 -0800 | [diff] [blame] | 15 | #include <algorithm> |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 16 | #include <set> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 17 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 18 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 19 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 20 | #include <base/bind.h> |
Ben Chan | 11c213f | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 21 | #include <base/files/file_util.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 22 | #include <base/memory/ref_counted.h> |
Paul Stewart | 38fcf16 | 2015-06-12 09:52:17 -0700 | [diff] [blame] | 23 | #include <base/stl_util.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 24 | #include <base/strings/stringprintf.h> |
Peter Qiu | 62abf31 | 2015-05-05 12:58:05 -0700 | [diff] [blame] | 25 | #include <base/strings/string_number_conversions.h> |
| 26 | #include <base/strings/string_util.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 27 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 28 | |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 29 | #include "shill/async_connection.h" |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 30 | #include "shill/connection.h" |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 31 | #include "shill/connection_tester.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 32 | #include "shill/control_interface.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 33 | #include "shill/device_dbus_adaptor.h" |
Peter Qiu | 675d0b0 | 2015-06-03 13:08:09 -0700 | [diff] [blame] | 34 | #include "shill/dhcp/dhcp_config.h" |
| 35 | #include "shill/dhcp/dhcp_provider.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 36 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 37 | #include "shill/event_dispatcher.h" |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 38 | #include "shill/geolocation_info.h" |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 39 | #include "shill/http_proxy.h" |
Peter Qiu | 62abf31 | 2015-05-05 12:58:05 -0700 | [diff] [blame] | 40 | #include "shill/icmp.h" |
Samuel Tan | fe73467 | 2014-08-07 15:50:48 -0700 | [diff] [blame] | 41 | #include "shill/ip_address_store.h" |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 42 | #include "shill/link_monitor.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 43 | #include "shill/logging.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 44 | #include "shill/manager.h" |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 45 | #include "shill/metrics.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 46 | #include "shill/net/ip_address.h" |
| 47 | #include "shill/net/ndisc.h" |
| 48 | #include "shill/net/rtnl_handler.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 49 | #include "shill/property_accessor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 50 | #include "shill/refptr_types.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 51 | #include "shill/service.h" |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 52 | #include "shill/socket_info_reader.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 53 | #include "shill/store_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 54 | #include "shill/technology.h" |
Paul Stewart | fa11e28 | 2013-12-02 22:04:25 -0800 | [diff] [blame] | 55 | #include "shill/tethering.h" |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 56 | #include "shill/traffic_monitor.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 57 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 58 | using base::Bind; |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 59 | using base::Callback; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 60 | using base::FilePath; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 61 | using base::StringPrintf; |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 62 | using std::set; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 63 | using std::string; |
| 64 | using std::vector; |
| 65 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 66 | namespace shill { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 67 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 68 | namespace Logging { |
| 69 | static auto kModuleLogScope = ScopeLogger::kDevice; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 70 | static string ObjectID(Device* d) { return d->GetRpcIdentifier(); } |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 73 | // static |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 74 | const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s"; |
| 75 | // static |
| 76 | const char Device::kIPFlagVersion4[] = "ipv4"; |
| 77 | // static |
| 78 | const char Device::kIPFlagVersion6[] = "ipv6"; |
| 79 | // static |
| 80 | const char Device::kIPFlagDisableIPv6[] = "disable_ipv6"; |
| 81 | // static |
| 82 | const char Device::kIPFlagUseTempAddr[] = "use_tempaddr"; |
| 83 | // static |
| 84 | const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2"; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 85 | // static |
| 86 | const char Device::kIPFlagReversePathFilter[] = "rp_filter"; |
| 87 | // static |
| 88 | const char Device::kIPFlagReversePathFilterEnabled[] = "1"; |
| 89 | // static |
| 90 | const char Device::kIPFlagReversePathFilterLooseMode[] = "2"; |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 91 | // static |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 92 | const char Device::kIPFlagArpAnnounce[] = "arp_announce"; |
| 93 | // static |
| 94 | const char Device::kIPFlagArpAnnounceDefault[] = "0"; |
| 95 | // static |
| 96 | const char Device::kIPFlagArpAnnounceBestLocal[] = "2"; |
| 97 | // static |
| 98 | const char Device::kIPFlagArpIgnore[] = "arp_ignore"; |
| 99 | // static |
| 100 | const char Device::kIPFlagArpIgnoreDefault[] = "0"; |
| 101 | // static |
| 102 | const char Device::kIPFlagArpIgnoreLocalOnly[] = "1"; |
| 103 | // static |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 104 | const char Device::kStoragePowered[] = "Powered"; |
| 105 | // static |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 106 | const char Device::kStorageReceiveByteCount[] = "ReceiveByteCount"; |
| 107 | // static |
| 108 | const char Device::kStorageTransmitByteCount[] = "TransmitByteCount"; |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 109 | // static |
| 110 | const char Device::kFallbackDnsTestHostname[] = "www.gstatic.com"; |
| 111 | // static |
| 112 | const char* Device::kFallbackDnsServers[] = { |
| 113 | "8.8.8.8", |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 114 | "8.8.4.4" |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | // static |
| 118 | const int Device::kDNSTimeoutMilliseconds = 5000; |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 119 | const int Device::kLinkUnreliableThresholdSeconds = 60 * 60; |
Peter Qiu | 62abf31 | 2015-05-05 12:58:05 -0700 | [diff] [blame] | 120 | const size_t Device::kHardwareAddressLength = 6U; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 121 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 122 | Device::Device(ControlInterface* control_interface, |
| 123 | EventDispatcher* dispatcher, |
| 124 | Metrics* metrics, |
| 125 | Manager* manager, |
| 126 | const string& link_name, |
| 127 | const string& address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 128 | int interface_index, |
| 129 | Technology::Identifier technology) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 130 | : enabled_(false), |
| 131 | enabled_persistent_(true), |
| 132 | enabled_pending_(enabled_), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 133 | reconnect_(true), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 134 | hardware_address_(address), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 135 | interface_index_(interface_index), |
| 136 | running_(false), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 137 | link_name_(link_name), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 138 | unique_id_(link_name), |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 139 | control_interface_(control_interface), |
| 140 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 141 | metrics_(metrics), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 142 | manager_(manager), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 143 | weak_ptr_factory_(this), |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 144 | adaptor_(control_interface->CreateDeviceAdaptor(this)), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 145 | portal_detector_callback_(Bind(&Device::PortalDetectorCallback, |
| 146 | weak_ptr_factory_.GetWeakPtr())), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 147 | technology_(technology), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 148 | portal_attempts_to_online_(0), |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 149 | receive_byte_offset_(0), |
| 150 | transmit_byte_offset_(0), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 151 | dhcp_provider_(DHCPProvider::GetInstance()), |
Peter Qiu | a057203 | 2014-09-26 10:07:37 -0700 | [diff] [blame] | 152 | rtnl_handler_(RTNLHandler::GetInstance()), |
| 153 | time_(Time::GetInstance()), |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 154 | last_link_monitor_failed_time_(0), |
| 155 | connection_tester_callback_(Bind(&Device::ConnectionTesterCallback, |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 156 | weak_ptr_factory_.GetWeakPtr())), |
| 157 | is_loose_routing_(false), |
| 158 | is_multi_homed_(false) { |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 159 | store_.RegisterConstString(kAddressProperty, &hardware_address_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 160 | |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 161 | // kBgscanMethodProperty: Registered in WiFi |
| 162 | // kBgscanShortIntervalProperty: Registered in WiFi |
| 163 | // kBgscanSignalThresholdProperty: Registered in WiFi |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 164 | |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 165 | // kCellularAllowRoamingProperty: Registered in Cellular |
| 166 | // kCarrierProperty: Registered in Cellular |
| 167 | // kEsnProperty: Registered in Cellular |
| 168 | // kHomeProviderProperty: Registered in Cellular |
| 169 | // kImeiProperty: Registered in Cellular |
| 170 | // kIccidProperty: Registered in Cellular |
| 171 | // kImsiProperty: Registered in Cellular |
| 172 | // kManufacturerProperty: Registered in Cellular |
| 173 | // kMdnProperty: Registered in Cellular |
| 174 | // kMeidProperty: Registered in Cellular |
| 175 | // kMinProperty: Registered in Cellular |
| 176 | // kModelIDProperty: Registered in Cellular |
| 177 | // kFirmwareRevisionProperty: Registered in Cellular |
| 178 | // kHardwareRevisionProperty: Registered in Cellular |
| 179 | // kPRLVersionProperty: Registered in Cellular |
| 180 | // kSIMLockStatusProperty: Registered in Cellular |
| 181 | // kFoundNetworksProperty: Registered in Cellular |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 182 | // kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 183 | |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 184 | store_.RegisterConstString(kInterfaceProperty, &link_name_); |
Christopher Wiley | 674598d | 2014-12-12 10:21:39 -0800 | [diff] [blame] | 185 | HelpRegisterConstDerivedRpcIdentifier( |
| 186 | kSelectedServiceProperty, &Device::GetSelectedServiceRpcIdentifier); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 187 | HelpRegisterConstDerivedRpcIdentifiers(kIPConfigsProperty, |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 188 | &Device::AvailableIPConfigs); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 189 | store_.RegisterConstString(kNameProperty, &link_name_); |
| 190 | store_.RegisterConstBool(kPoweredProperty, &enabled_); |
| 191 | HelpRegisterConstDerivedString(kTypeProperty, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 192 | &Device::GetTechnologyString); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 193 | HelpRegisterConstDerivedUint64(kLinkMonitorResponseTimeProperty, |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 194 | &Device::GetLinkMonitorResponseTime); |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 195 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 196 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 197 | // store_.RegisterConstBool(kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 198 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 199 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 200 | // known_properties_.push_back(kNetworksProperty); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 201 | |
Wade Guthrie | 227c774 | 2013-10-10 11:06:33 -0700 | [diff] [blame] | 202 | // kRoamThresholdProperty: Registered in WiFi |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 203 | // kScanningProperty: Registered in WiFi, Cellular |
| 204 | // kScanIntervalProperty: Registered in WiFi, Cellular |
Samuel Tan | 96e35cf | 2014-10-28 22:27:38 -0700 | [diff] [blame] | 205 | // kWakeOnWiFiFeaturesEnabledProperty: Registered in WiFi |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 206 | |
| 207 | if (manager_ && manager_->device_info()) { // Unit tests may not have these. |
| 208 | manager_->device_info()->GetByteCounts( |
| 209 | interface_index_, &receive_byte_offset_, &transmit_byte_offset_); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 210 | HelpRegisterConstDerivedUint64(kReceiveByteCountProperty, |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 211 | &Device::GetReceiveByteCountProperty); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 212 | HelpRegisterConstDerivedUint64(kTransmitByteCountProperty, |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 213 | &Device::GetTransmitByteCountProperty); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 216 | DisableArpFiltering(); |
| 217 | EnableReversePathFilter(); |
| 218 | |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 219 | LOG(INFO) << "Device created: " << link_name_ |
| 220 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | Device::~Device() { |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 224 | LOG(INFO) << "Device destructed: " << link_name_ |
| 225 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 228 | void Device::LinkEvent(unsigned flags, unsigned change) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 229 | SLOG(this, 2) << "Device " << link_name_ |
| 230 | << std::showbase << std::hex |
| 231 | << " flags " << flags << " changed " << change |
| 232 | << std::dec << std::noshowbase; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 235 | void Device::Scan(ScanType scan_type, Error* error, const string& reason) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 236 | SLOG(this, 2) << __func__ << " [Device] on " << link_name() << " from " |
| 237 | << reason; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 238 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 239 | "Device doesn't support scan."); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 242 | void Device::SetSchedScan(bool enable, Error* error) { |
Peter Qiu | d51b244 | 2015-02-23 10:41:38 -0800 | [diff] [blame] | 243 | SLOG(this, 2) << __func__ << " [Device] on " << link_name(); |
| 244 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
| 245 | "Device doesn't support scheduled scan."); |
| 246 | } |
| 247 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 248 | void Device::RegisterOnNetwork(const std::string& /*network_id*/, Error* error, |
| 249 | const ResultCallback& /*callback*/) { |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 250 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 251 | "Device doesn't support network registration."); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 254 | void Device::RequirePIN( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 255 | const string& /*pin*/, bool /*require*/, |
| 256 | Error* error, const ResultCallback& /*callback*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 257 | SLOG(this, 2) << __func__; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 258 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 259 | "Device doesn't support RequirePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 262 | void Device::EnterPIN(const string& /*pin*/, |
| 263 | Error* error, const ResultCallback& /*callback*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 264 | SLOG(this, 2) << __func__; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 265 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 266 | "Device doesn't support EnterPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 269 | void Device::UnblockPIN(const string& /*unblock_code*/, |
| 270 | const string& /*pin*/, |
| 271 | Error* error, const ResultCallback& /*callback*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 272 | SLOG(this, 2) << __func__; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 273 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 274 | "Device doesn't support UnblockPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 277 | void Device::ChangePIN(const string& /*old_pin*/, |
| 278 | const string& /*new_pin*/, |
| 279 | Error* error, const ResultCallback& /*callback*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 280 | SLOG(this, 2) << __func__; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 281 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 282 | "Device doesn't support ChangePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 285 | void Device::Reset(Error* error, const ResultCallback& /*callback*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 286 | SLOG(this, 2) << __func__; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 287 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Ben Chan | ad663e1 | 2013-01-08 01:58:47 -0800 | [diff] [blame] | 288 | "Device doesn't support Reset."); |
| 289 | } |
| 290 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 291 | void Device::SetCarrier(const string& /*carrier*/, |
| 292 | Error* error, const ResultCallback& /*callback*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 293 | SLOG(this, 2) << __func__; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 294 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Darin Petkov | c37a9c4 | 2012-09-06 15:28:22 +0200 | [diff] [blame] | 295 | "Device doesn't support SetCarrier."); |
| 296 | } |
| 297 | |
Ben Chan | bcc6e01 | 2013-11-04 14:28:37 -0800 | [diff] [blame] | 298 | bool Device::IsIPv6Allowed() const { |
| 299 | return true; |
| 300 | } |
| 301 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 302 | void Device::DisableIPv6() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 303 | SLOG(this, 2) << __func__; |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 304 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1"); |
| 305 | } |
| 306 | |
| 307 | void Device::EnableIPv6() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 308 | SLOG(this, 2) << __func__; |
Ben Chan | bcc6e01 | 2013-11-04 14:28:37 -0800 | [diff] [blame] | 309 | if (!IsIPv6Allowed()) { |
| 310 | LOG(INFO) << "Skip enabling IPv6 on " << link_name_ |
| 311 | << " as it is not allowed."; |
| 312 | return; |
| 313 | } |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 314 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0"); |
| 315 | } |
| 316 | |
| 317 | void Device::EnableIPv6Privacy() { |
| 318 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr, |
| 319 | kIPFlagUseTempAddrUsedAndDefault); |
| 320 | } |
| 321 | |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 322 | void Device::SetLooseRouting(bool is_loose_routing) { |
| 323 | if (is_loose_routing == is_loose_routing_) { |
| 324 | return; |
| 325 | } |
| 326 | is_loose_routing_ = is_loose_routing; |
| 327 | if (is_multi_homed_) { |
| 328 | // Nothing to do: loose routing is already enabled, and should remain so. |
| 329 | return; |
| 330 | } |
| 331 | if (is_loose_routing) { |
| 332 | DisableReversePathFilter(); |
| 333 | } else { |
| 334 | EnableReversePathFilter(); |
| 335 | } |
| 336 | } |
| 337 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 338 | void Device::DisableReversePathFilter() { |
| 339 | // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 340 | // for IPv6. crbug.com/207193 |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 341 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 342 | kIPFlagReversePathFilterLooseMode); |
| 343 | } |
| 344 | |
| 345 | void Device::EnableReversePathFilter() { |
| 346 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 347 | kIPFlagReversePathFilterEnabled); |
| 348 | } |
| 349 | |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 350 | void Device::SetIsMultiHomed(bool is_multi_homed) { |
| 351 | if (is_multi_homed == is_multi_homed_) { |
| 352 | return; |
| 353 | } |
| 354 | LOG(INFO) << "Device " << FriendlyName() << " multi-home state is now " |
| 355 | << is_multi_homed; |
| 356 | is_multi_homed_ = is_multi_homed; |
| 357 | if (is_multi_homed) { |
| 358 | EnableArpFiltering(); |
| 359 | if (!is_loose_routing_) { |
| 360 | DisableReversePathFilter(); |
| 361 | } |
| 362 | } else { |
| 363 | DisableArpFiltering(); |
| 364 | if (!is_loose_routing_) { |
| 365 | EnableReversePathFilter(); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void Device::DisableArpFiltering() { |
| 371 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagArpAnnounce, |
| 372 | kIPFlagArpAnnounceDefault); |
| 373 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagArpIgnore, kIPFlagArpIgnoreDefault); |
| 374 | } |
| 375 | |
| 376 | void Device::EnableArpFiltering() { |
| 377 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagArpAnnounce, |
| 378 | kIPFlagArpAnnounceBestLocal); |
| 379 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagArpIgnore, |
| 380 | kIPFlagArpIgnoreLocalOnly); |
| 381 | } |
| 382 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 383 | bool Device::IsConnected() const { |
| 384 | if (selected_service_) |
| 385 | return selected_service_->IsConnected(); |
| 386 | return false; |
| 387 | } |
| 388 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 389 | bool Device::IsConnectedToService(const ServiceRefPtr& service) const { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 390 | return service == selected_service_ && IsConnected(); |
| 391 | } |
| 392 | |
Paul Stewart | fa11e28 | 2013-12-02 22:04:25 -0800 | [diff] [blame] | 393 | bool Device::IsConnectedViaTether() const { |
| 394 | return |
| 395 | ipconfig_.get() && |
| 396 | ipconfig_->properties().vendor_encapsulated_options == |
| 397 | Tethering::kAndroidVendorEncapsulatedOptions; |
| 398 | } |
| 399 | |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 400 | string Device::GetRpcIdentifier() const { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 401 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 402 | } |
| 403 | |
mukesh agrawal | 515873d | 2014-07-21 17:01:35 -0700 | [diff] [blame] | 404 | string Device::GetStorageIdentifier() const { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 405 | string id = GetRpcIdentifier(); |
| 406 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 407 | size_t needle = id.find('_'); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 408 | DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 409 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 410 | return id; |
| 411 | } |
| 412 | |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 413 | vector<GeolocationInfo> Device::GetGeolocationObjects() const { |
| 414 | return vector<GeolocationInfo>(); |
Wade Guthrie | 227c774 | 2013-10-10 11:06:33 -0700 | [diff] [blame] | 415 | } |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 416 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 417 | string Device::GetTechnologyString(Error* /*error*/) { |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 418 | return Technology::NameFromIdentifier(technology()); |
| 419 | } |
| 420 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 421 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 422 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 425 | const string& Device::UniqueName() const { |
| 426 | return unique_id_; |
| 427 | } |
| 428 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 429 | bool Device::Load(StoreInterface* storage) { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 430 | const string id = GetStorageIdentifier(); |
| 431 | if (!storage->ContainsGroup(id)) { |
Paul Stewart | 34b3f12 | 2015-06-12 10:30:24 -0700 | [diff] [blame] | 432 | SLOG(this, 2) << "Device is not available in the persistent store: " << id; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 433 | return false; |
| 434 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 435 | enabled_persistent_ = true; |
| 436 | storage->GetBool(id, kStoragePowered, &enabled_persistent_); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 437 | uint64_t rx_byte_count = 0, tx_byte_count = 0; |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 438 | |
| 439 | manager_->device_info()->GetByteCounts( |
| 440 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 441 | // If there is a byte-count present in the profile, the return value |
| 442 | // of Device::Get*ByteCount() should be the this stored value plus |
| 443 | // whatever additional bytes we receive since time-of-load. We |
| 444 | // accomplish this by the subtractions below, which can validly |
| 445 | // roll over "negative" in the subtractions below and in Get*ByteCount. |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 446 | uint64_t profile_byte_count; |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 447 | if (storage->GetUint64(id, kStorageReceiveByteCount, &profile_byte_count)) { |
| 448 | receive_byte_offset_ = rx_byte_count - profile_byte_count; |
| 449 | } |
| 450 | if (storage->GetUint64(id, kStorageTransmitByteCount, &profile_byte_count)) { |
| 451 | transmit_byte_offset_ = tx_byte_count - profile_byte_count; |
| 452 | } |
| 453 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 454 | return true; |
| 455 | } |
| 456 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 457 | bool Device::Save(StoreInterface* storage) { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 458 | const string id = GetStorageIdentifier(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 459 | storage->SetBool(id, kStoragePowered, enabled_persistent_); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 460 | storage->SetUint64(id, kStorageReceiveByteCount, GetReceiveByteCount()); |
| 461 | storage->SetUint64(id, kStorageTransmitByteCount, GetTransmitByteCount()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 462 | return true; |
| 463 | } |
| 464 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 465 | void Device::OnBeforeSuspend(const ResultCallback& callback) { |
Samuel Tan | fbe8d2b | 2014-09-15 20:23:59 -0700 | [diff] [blame] | 466 | // Nothing to be done in the general case, so immediately report success. |
| 467 | callback.Run(Error(Error::kSuccess)); |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | void Device::OnAfterResume() { |
Samuel Tan | 787a1ce | 2014-11-11 17:17:27 -0800 | [diff] [blame] | 471 | RenewDHCPLease(); |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 472 | if (link_monitor_) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 473 | SLOG(this, 3) << "Informing Link Monitor of resume."; |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 474 | link_monitor_->OnAfterResume(); |
| 475 | } |
Peter Qiu | a057203 | 2014-09-26 10:07:37 -0700 | [diff] [blame] | 476 | // Resume from sleep, could be in different location now. |
| 477 | // Ignore previous link monitor failures. |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 478 | if (selected_service_) { |
| 479 | selected_service_->set_unreliable(false); |
| 480 | reliable_link_callback_.Cancel(); |
| 481 | } |
Peter Qiu | a057203 | 2014-09-26 10:07:37 -0700 | [diff] [blame] | 482 | last_link_monitor_failed_time_ = 0; |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 485 | void Device::OnDarkResume(const ResultCallback& callback) { |
Samuel Tan | 68b73d2 | 2014-10-28 17:00:56 -0700 | [diff] [blame] | 486 | // Nothing to be done in the general case, so immediately report success. |
| 487 | callback.Run(Error(Error::kSuccess)); |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 490 | void Device::DropConnection() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 491 | SLOG(this, 2) << __func__; |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 492 | DestroyIPConfig(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 493 | SelectService(nullptr); |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 494 | } |
| 495 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 496 | void Device::DestroyIPConfig() { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 497 | DisableIPv6(); |
Peter Qiu | d48fa0c | 2015-06-10 12:20:48 -0700 | [diff] [blame] | 498 | bool ipconfig_changed = false; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 499 | if (ipconfig_.get()) { |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 500 | ipconfig_->ReleaseIP(IPConfig::kReleaseReasonDisconnect); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 501 | ipconfig_ = nullptr; |
Peter Qiu | d48fa0c | 2015-06-10 12:20:48 -0700 | [diff] [blame] | 502 | ipconfig_changed = true; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 503 | } |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 504 | if (ip6config_.get()) { |
| 505 | StopIPv6DNSServerTimer(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 506 | ip6config_ = nullptr; |
Peter Qiu | d48fa0c | 2015-06-10 12:20:48 -0700 | [diff] [blame] | 507 | ipconfig_changed = true; |
| 508 | } |
| 509 | if (dhcpv6_config_.get()) { |
| 510 | dhcpv6_config_->ReleaseIP(IPConfig::kReleaseReasonDisconnect); |
| 511 | dhcpv6_config_ = nullptr; |
| 512 | ipconfig_changed = true; |
| 513 | } |
| 514 | // Emit updated IP configs if there are any changes. |
| 515 | if (ipconfig_changed) { |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 516 | UpdateIPConfigsProperty(); |
| 517 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 518 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 521 | void Device::OnIPv6AddressChanged() { |
| 522 | IPAddress address(IPAddress::kFamilyIPv6); |
| 523 | if (!manager_->device_info()->GetPrimaryIPv6Address( |
| 524 | interface_index_, &address)) { |
| 525 | if (ip6config_) { |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 526 | ip6config_ = nullptr; |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 527 | UpdateIPConfigsProperty(); |
| 528 | } |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | IPConfig::Properties properties; |
| 533 | if (!address.IntoString(&properties.address)) { |
| 534 | LOG(ERROR) << "Unable to convert IPv6 address into a string!"; |
| 535 | return; |
| 536 | } |
| 537 | properties.subnet_prefix = address.prefix(); |
| 538 | |
| 539 | if (!ip6config_) { |
| 540 | ip6config_ = new IPConfig(control_interface_, link_name_); |
| 541 | } else if (properties.address == ip6config_->properties().address && |
| 542 | properties.subnet_prefix == |
| 543 | ip6config_->properties().subnet_prefix) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 544 | SLOG(this, 2) << __func__ << " primary address for " |
| 545 | << link_name_ << " is unchanged."; |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 546 | return; |
| 547 | } |
| 548 | |
| 549 | properties.address_family = IPAddress::kFamilyIPv6; |
| 550 | properties.method = kTypeIPv6; |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 551 | // It is possible for device to receive DNS server notification before IP |
| 552 | // address notification, so preserve the saved DNS server if it exist. |
| 553 | properties.dns_servers = ip6config_->properties().dns_servers; |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 554 | PrependDNSServers(IPAddress::kFamilyIPv6, &properties.dns_servers); |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 555 | ip6config_->set_properties(properties); |
| 556 | UpdateIPConfigsProperty(); |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 557 | OnIPv6ConfigUpdated(); |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Peter Qiu | 9855170 | 2014-07-28 13:28:53 -0700 | [diff] [blame] | 560 | void Device::OnIPv6DnsServerAddressesChanged() { |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 561 | vector<IPAddress> server_addresses; |
Paul Stewart | 60a922e | 2015-06-16 11:28:29 -0700 | [diff] [blame] | 562 | uint32 lifetime = 0; |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 563 | |
| 564 | // Stop any existing timer. |
| 565 | StopIPv6DNSServerTimer(); |
| 566 | |
| 567 | if (!manager_->device_info()->GetIPv6DnsServerAddresses( |
| 568 | interface_index_, &server_addresses, &lifetime) || lifetime == 0) { |
| 569 | IPv6DNSServerExpired(); |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | vector<string> addresses_str; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 574 | for (const auto& ip : server_addresses) { |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 575 | string address_str; |
| 576 | if (!ip.IntoString(&address_str)) { |
| 577 | LOG(ERROR) << "Unable to convert IPv6 address into a string!"; |
| 578 | IPv6DNSServerExpired(); |
| 579 | return; |
| 580 | } |
| 581 | addresses_str.push_back(address_str); |
| 582 | } |
| 583 | |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 584 | if (!ip6config_) { |
| 585 | ip6config_ = new IPConfig(control_interface_, link_name_); |
| 586 | } |
| 587 | |
Samuel Tan | 815a6fb | 2014-10-23 16:53:59 -0700 | [diff] [blame] | 588 | if (lifetime != ND_OPT_LIFETIME_INFINITY) { |
| 589 | // Setup timer to monitor DNS server lifetime if not infinite lifetime. |
| 590 | StartIPv6DNSServerTimer(lifetime); |
| 591 | ip6config_->UpdateLeaseExpirationTime(lifetime); |
| 592 | } else { |
| 593 | ip6config_->ResetLeaseExpirationTime(); |
| 594 | } |
| 595 | |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 596 | PrependDNSServers(IPAddress::kFamilyIPv6, &addresses_str); |
| 597 | |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 598 | // Done if no change in server addresses. |
| 599 | if (ip6config_->properties().dns_servers == addresses_str) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 600 | SLOG(this, 2) << __func__ << " IPv6 DNS server list for " |
| 601 | << link_name_ << " is unchanged."; |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 602 | return; |
| 603 | } |
| 604 | |
| 605 | ip6config_->UpdateDNSServers(addresses_str); |
| 606 | UpdateIPConfigsProperty(); |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 607 | OnIPv6ConfigUpdated(); |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | void Device::StartIPv6DNSServerTimer(uint32 lifetime_seconds) { |
| 611 | int64 delay = static_cast<int64>(lifetime_seconds) * 1000; |
| 612 | ipv6_dns_server_expired_callback_.Reset( |
| 613 | base::Bind(&Device::IPv6DNSServerExpired, base::Unretained(this))); |
| 614 | dispatcher_->PostDelayedTask(ipv6_dns_server_expired_callback_.callback(), |
| 615 | delay); |
| 616 | } |
| 617 | |
| 618 | void Device::StopIPv6DNSServerTimer() { |
| 619 | ipv6_dns_server_expired_callback_.Cancel(); |
| 620 | } |
| 621 | |
| 622 | void Device::IPv6DNSServerExpired() { |
| 623 | if (!ip6config_) { |
| 624 | return; |
| 625 | } |
| 626 | ip6config_->UpdateDNSServers(vector<string>()); |
| 627 | UpdateIPConfigsProperty(); |
| 628 | } |
| 629 | |
| 630 | void Device::StopAllActivities() { |
| 631 | StopTrafficMonitor(); |
| 632 | StopPortalDetection(); |
Paul Stewart | e8303eb | 2014-10-08 22:51:14 -0700 | [diff] [blame] | 633 | StopConnectivityTest(); |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 634 | StopLinkMonitor(); |
| 635 | StopDNSTest(); |
| 636 | StopIPv6DNSServerTimer(); |
Peter Qiu | 9855170 | 2014-07-28 13:28:53 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 639 | void Device::AddWakeOnPacketConnection(const string& ip_endpoint, |
| 640 | Error* error) { |
Samuel Tan | 5412de0 | 2014-08-15 17:56:26 -0700 | [diff] [blame] | 641 | Error::PopulateAndLog( |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 642 | FROM_HERE, error, Error::kNotSupported, |
Samuel Tan | fe73467 | 2014-08-07 15:50:48 -0700 | [diff] [blame] | 643 | "AddWakeOnPacketConnection not implemented for " + link_name_ + "."); |
| 644 | return; |
| 645 | } |
| 646 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 647 | void Device::RemoveWakeOnPacketConnection(const string& ip_endpoint, |
| 648 | Error* error) { |
Samuel Tan | 5412de0 | 2014-08-15 17:56:26 -0700 | [diff] [blame] | 649 | Error::PopulateAndLog( |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 650 | FROM_HERE, error, Error::kNotSupported, |
Samuel Tan | fe73467 | 2014-08-07 15:50:48 -0700 | [diff] [blame] | 651 | "RemoveWakeOnPacketConnection not implemented for " + link_name_ + "."); |
| 652 | return; |
| 653 | } |
| 654 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 655 | void Device::RemoveAllWakeOnPacketConnections(Error* error) { |
Samuel Tan | 5412de0 | 2014-08-15 17:56:26 -0700 | [diff] [blame] | 656 | Error::PopulateAndLog( |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 657 | FROM_HERE, error, Error::kNotSupported, |
Samuel Tan | fe73467 | 2014-08-07 15:50:48 -0700 | [diff] [blame] | 658 | "RemoveAllWakeOnPacketConnections not implemented for " + link_name_ + |
| 659 | "."); |
| 660 | return; |
| 661 | } |
| 662 | |
Samuel Tan | 787a1ce | 2014-11-11 17:17:27 -0800 | [diff] [blame] | 663 | void Device::RenewDHCPLease() { |
| 664 | LOG(INFO) << __func__; |
| 665 | |
| 666 | if (ipconfig_) { |
| 667 | SLOG(this, 3) << "Renewing IPv4 Address"; |
| 668 | ipconfig_->RenewIP(); |
| 669 | } |
| 670 | if (ip6config_) { |
| 671 | SLOG(this, 3) << "Waiting for new IPv6 configuration"; |
| 672 | // Invalidate the old IPv6 configuration, will receive notifications |
| 673 | // from kernel for new IPv6 configuration if there is one. |
| 674 | StopIPv6DNSServerTimer(); |
| 675 | ip6config_ = nullptr; |
| 676 | UpdateIPConfigsProperty(); |
| 677 | } |
Peter Qiu | d48fa0c | 2015-06-10 12:20:48 -0700 | [diff] [blame] | 678 | if (dhcpv6_config_) { |
| 679 | SLOG(this, 3) << "Renewing DHCPv6 lease"; |
| 680 | dhcpv6_config_->RenewIP(); |
| 681 | } |
Samuel Tan | 787a1ce | 2014-11-11 17:17:27 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 684 | bool Device::ShouldUseArpGateway() const { |
| 685 | return false; |
| 686 | } |
| 687 | |
Paul Stewart | 316acef | 2014-05-29 18:40:48 -0700 | [diff] [blame] | 688 | bool Device::IsUsingStaticIP() const { |
| 689 | if (!selected_service_) { |
| 690 | return false; |
| 691 | } |
| 692 | return selected_service_->HasStaticIPAddress(); |
| 693 | } |
| 694 | |
Garret Kelly | d01b5cc | 2015-03-12 16:20:55 -0400 | [diff] [blame] | 695 | bool Device::IsUsingStaticNameServers() const { |
| 696 | if (!selected_service_) { |
| 697 | return false; |
| 698 | } |
| 699 | return selected_service_->HasStaticNameServers(); |
| 700 | } |
| 701 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 702 | bool Device::AcquireIPConfig() { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 703 | return AcquireIPConfigWithLeaseName(string()); |
| 704 | } |
| 705 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 706 | bool Device::AcquireIPConfigWithLeaseName(const string& lease_name) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 707 | DestroyIPConfig(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 708 | EnableIPv6(); |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 709 | bool arp_gateway = manager_->GetArpGateway() && ShouldUseArpGateway(); |
Peter Qiu | 8e0151e | 2015-06-04 09:41:47 -0700 | [diff] [blame] | 710 | auto dhcp_config = dhcp_provider_->CreateIPv4Config( |
| 711 | link_name_, manager_->GetHostName(), lease_name, arp_gateway); |
Garret Kelly | 782cdce | 2015-04-01 16:39:16 -0400 | [diff] [blame] | 712 | const int minimum_mtu = manager()->GetMinimumMTU(); |
| 713 | if (minimum_mtu != IPConfig::kUndefinedMTU) { |
| 714 | dhcp_config->set_minimum_mtu(minimum_mtu); |
| 715 | } |
| 716 | |
| 717 | ipconfig_ = dhcp_config; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 718 | ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated, |
| 719 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 720 | ipconfig_->RegisterFailureCallback(Bind(&Device::OnIPConfigFailed, |
| 721 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 722 | ipconfig_->RegisterRefreshCallback(Bind(&Device::OnIPConfigRefreshed, |
| 723 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 724 | ipconfig_->RegisterExpireCallback(Bind(&Device::OnIPConfigExpired, |
| 725 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 726 | dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask, |
| 727 | weak_ptr_factory_.GetWeakPtr())); |
Peter Qiu | d48fa0c | 2015-06-10 12:20:48 -0700 | [diff] [blame] | 728 | if (!ipconfig_->RequestIP()) { |
| 729 | return false; |
| 730 | } |
| 731 | |
| 732 | #ifndef DISABLE_DHCPV6 |
| 733 | // Request DHCPv6 configurations. DHCPv6 configurations will not be use to |
| 734 | // setup connection for the device. |
| 735 | if (manager_->IsDHCPv6EnabledForDevice(link_name_)) { |
| 736 | auto dhcpv6_config = |
| 737 | dhcp_provider_->CreateIPv6Config(link_name_, lease_name); |
| 738 | dhcpv6_config_ = dhcpv6_config; |
| 739 | dhcpv6_config_->RegisterUpdateCallback( |
| 740 | Bind(&Device::OnDHCPv6ConfigUpdated, weak_ptr_factory_.GetWeakPtr())); |
| 741 | dhcpv6_config_->RegisterFailureCallback( |
| 742 | Bind(&Device::OnDHCPv6ConfigFailed, weak_ptr_factory_.GetWeakPtr())); |
| 743 | dhcpv6_config_->RegisterExpireCallback( |
| 744 | Bind(&Device::OnDHCPv6ConfigExpired, weak_ptr_factory_.GetWeakPtr())); |
| 745 | if (!dhcpv6_config_->RequestIP()) { |
| 746 | return false; |
| 747 | } |
| 748 | } |
| 749 | #endif // DISABLE_DHCPV6 |
| 750 | |
| 751 | return true; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 754 | void Device::AssignIPConfig(const IPConfig::Properties& properties) { |
Ben Chan | 539ab02 | 2014-02-03 16:34:57 -0800 | [diff] [blame] | 755 | DestroyIPConfig(); |
| 756 | EnableIPv6(); |
| 757 | ipconfig_ = new IPConfig(control_interface_, link_name_); |
| 758 | ipconfig_->set_properties(properties); |
| 759 | dispatcher_->PostTask(Bind(&Device::OnIPConfigUpdated, |
Samuel Tan | 3c3c36a | 2014-12-16 16:53:19 -0800 | [diff] [blame] | 760 | weak_ptr_factory_.GetWeakPtr(), ipconfig_, true)); |
Ben Chan | 539ab02 | 2014-02-03 16:34:57 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 763 | void Device::DestroyIPConfigLease(const string& name) { |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 764 | dhcp_provider_->DestroyLease(name); |
| 765 | } |
| 766 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 767 | void Device::HelpRegisterConstDerivedString( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 768 | const string& name, |
| 769 | string(Device::*get)(Error* error)) { |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 770 | store_.RegisterDerivedString( |
| 771 | name, |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 772 | StringAccessor(new CustomAccessor<Device, string>(this, get, nullptr))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Christopher Wiley | 674598d | 2014-12-12 10:21:39 -0800 | [diff] [blame] | 775 | void Device::HelpRegisterConstDerivedRpcIdentifier( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 776 | const string& name, |
| 777 | RpcIdentifier(Device::*get)(Error* error)) { |
Christopher Wiley | 674598d | 2014-12-12 10:21:39 -0800 | [diff] [blame] | 778 | store_.RegisterDerivedRpcIdentifier( |
| 779 | name, |
| 780 | RpcIdentifierAccessor( |
| 781 | new CustomAccessor<Device, RpcIdentifier>(this, get, nullptr))); |
| 782 | } |
| 783 | |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 784 | void Device::HelpRegisterConstDerivedRpcIdentifiers( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 785 | const string& name, |
| 786 | RpcIdentifiers(Device::*get)(Error*)) { |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 787 | store_.RegisterDerivedRpcIdentifiers( |
| 788 | name, |
| 789 | RpcIdentifiersAccessor( |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 790 | new CustomAccessor<Device, RpcIdentifiers>(this, get, nullptr))); |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 791 | } |
| 792 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 793 | void Device::HelpRegisterConstDerivedUint64( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 794 | const string& name, |
| 795 | uint64_t(Device::*get)(Error*)) { |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 796 | store_.RegisterDerivedUint64( |
| 797 | name, |
| 798 | Uint64Accessor( |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 799 | new CustomAccessor<Device, uint64_t>(this, get, nullptr))); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 802 | void Device::ConnectionTesterCallback() { |
| 803 | LOG(INFO) << "Device " << FriendlyName() << ": Completed Connectivity Test"; |
| 804 | return; |
| 805 | } |
| 806 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 807 | void Device::ConfigureStaticIPTask() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 808 | SLOG(this, 2) << __func__ << " selected_service " << selected_service_.get() |
| 809 | << " ipconfig " << ipconfig_.get(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 810 | |
| 811 | if (!selected_service_ || !ipconfig_) { |
| 812 | return; |
| 813 | } |
| 814 | |
Paul Stewart | 316acef | 2014-05-29 18:40:48 -0700 | [diff] [blame] | 815 | if (IsUsingStaticIP()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 816 | SLOG(this, 2) << __func__ << " " << " configuring static IP parameters."; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 817 | // If the parameters contain an IP address, apply them now and bring |
| 818 | // the interface up. When DHCP information arrives, it will supplement |
| 819 | // the static information. |
Samuel Tan | 3c3c36a | 2014-12-16 16:53:19 -0800 | [diff] [blame] | 820 | OnIPConfigUpdated(ipconfig_, true); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 821 | } else { |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 822 | // Either |ipconfig_| has just been created in AcquireIPConfig() or |
| 823 | // we're being called by OnIPConfigRefreshed(). In either case a |
| 824 | // DHCP client has been started, and will take care of calling |
| 825 | // OnIPConfigUpdated() when it completes. |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 826 | SLOG(this, 2) << __func__ << " " << " no static IP address."; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 830 | bool Device::IPConfigCompleted(const IPConfigRefPtr& ipconfig) { |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 831 | return ipconfig && !ipconfig->properties().address.empty() && |
| 832 | !ipconfig->properties().dns_servers.empty(); |
| 833 | } |
| 834 | |
| 835 | void Device::OnIPv6ConfigUpdated() { |
| 836 | // Setup connection using IPv6 configuration only if the IPv6 configuration |
| 837 | // is ready for connection (contained both IP address and DNS servers), and |
| 838 | // there is no existing IPv4 connection. We always prefer IPv4 |
| 839 | // configuration over IPv6. |
| 840 | if (IPConfigCompleted(ip6config_) && |
| 841 | (!connection_ || connection_->IsIPv6())) { |
| 842 | SetupConnection(ip6config_); |
| 843 | } |
| 844 | } |
| 845 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 846 | void Device::SetupConnection(const IPConfigRefPtr& ipconfig) { |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 847 | CreateConnection(); |
| 848 | connection_->UpdateFromIPConfig(ipconfig); |
| 849 | |
Peter Qiu | 300769e | 2014-08-27 11:48:45 -0700 | [diff] [blame] | 850 | // Report connection type. |
| 851 | Metrics::NetworkConnectionIPType ip_type = |
| 852 | connection_->IsIPv6() ? Metrics::kNetworkConnectionIPTypeIPv6 |
| 853 | : Metrics::kNetworkConnectionIPTypeIPv4; |
| 854 | metrics_->NotifyNetworkConnectionIPType(technology_, ip_type); |
| 855 | |
| 856 | // Report if device have IPv6 connectivity |
| 857 | bool ipv6_connectivity = IPConfigCompleted(ip6config_); |
| 858 | metrics_->NotifyIPv6ConnectivityStatus(technology_, ipv6_connectivity); |
| 859 | |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 860 | // SetConnection must occur after the UpdateFromIPConfig so the |
| 861 | // service can use the values derived from the connection. |
| 862 | if (selected_service_) { |
| 863 | selected_service_->SetConnection(connection_); |
| 864 | |
| 865 | // The service state change needs to happen last, so that at the |
| 866 | // time we report the state change to the manager, the service |
| 867 | // has its connection. |
| 868 | SetServiceState(Service::kStateConnected); |
| 869 | OnConnected(); |
| 870 | portal_attempts_to_online_ = 0; |
| 871 | |
| 872 | // Subtle: Start portal detection after transitioning the service |
| 873 | // to the Connected state because this call may immediately transition |
| 874 | // to the Online state. |
| 875 | StartPortalDetection(); |
| 876 | } |
| 877 | |
Paul Stewart | 208a97e | 2015-05-13 09:11:12 -0700 | [diff] [blame] | 878 | SetHostname(ipconfig->properties().accepted_hostname); |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 879 | StartLinkMonitor(); |
| 880 | StartTrafficMonitor(); |
| 881 | } |
| 882 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 883 | bool Device::SetHostname(const std::string& hostname) { |
Paul Stewart | 208a97e | 2015-05-13 09:11:12 -0700 | [diff] [blame] | 884 | if (hostname.empty() || !manager()->ShouldAcceptHostnameFrom(link_name_)) { |
| 885 | return false; |
| 886 | } |
| 887 | |
| 888 | string fixed_hostname = hostname; |
| 889 | if (fixed_hostname.length() > MAXHOSTNAMELEN) { |
| 890 | auto truncate_length = fixed_hostname.find('.'); |
| 891 | if (truncate_length == string::npos || truncate_length > MAXHOSTNAMELEN) { |
| 892 | truncate_length = MAXHOSTNAMELEN; |
| 893 | } |
| 894 | fixed_hostname.resize(truncate_length); |
| 895 | } |
| 896 | |
| 897 | return manager_->device_info()->SetHostname(fixed_hostname); |
| 898 | } |
| 899 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 900 | void Device::PrependDNSServersIntoIPConfig(const IPConfigRefPtr& ipconfig) { |
| 901 | const auto& properties = ipconfig->properties(); |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 902 | |
| 903 | vector<string> servers(properties.dns_servers.begin(), |
| 904 | properties.dns_servers.end()); |
| 905 | PrependDNSServers(properties.address_family, &servers); |
| 906 | if (servers == properties.dns_servers) { |
| 907 | // If the server list is the same after being augmented then there's no need |
| 908 | // to update the config's list of servers. |
| 909 | return; |
| 910 | } |
| 911 | |
| 912 | ipconfig->UpdateDNSServers(servers); |
| 913 | } |
| 914 | |
| 915 | void Device::PrependDNSServers(const IPAddress::Family family, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 916 | vector<string>* servers) { |
Paul Stewart | 1ce231c | 2015-06-12 19:44:22 -0700 | [diff] [blame] | 917 | vector<string>output_servers = |
| 918 | manager_->FilterPrependDNSServersByFamily(family); |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 919 | |
Paul Stewart | 1ce231c | 2015-06-12 19:44:22 -0700 | [diff] [blame] | 920 | set<string> unique(output_servers.begin(), output_servers.end()); |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 921 | for (const auto& server : *servers) { |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 922 | if (unique.find(server) == unique.end()) { |
Paul Stewart | 1ce231c | 2015-06-12 19:44:22 -0700 | [diff] [blame] | 923 | output_servers.push_back(server); |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 924 | unique.insert(server); |
| 925 | } |
| 926 | } |
Paul Stewart | 1ce231c | 2015-06-12 19:44:22 -0700 | [diff] [blame] | 927 | servers->swap(output_servers); |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 928 | } |
| 929 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 930 | void Device::OnIPConfigUpdated(const IPConfigRefPtr& ipconfig, |
Samuel Tan | 3c3c36a | 2014-12-16 16:53:19 -0800 | [diff] [blame] | 931 | bool /*new_lease_acquired*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 932 | SLOG(this, 2) << __func__; |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 933 | if (selected_service_) { |
| 934 | ipconfig->ApplyStaticIPParameters( |
| 935 | selected_service_->mutable_static_ip_parameters()); |
Paul Stewart | 316acef | 2014-05-29 18:40:48 -0700 | [diff] [blame] | 936 | if (IsUsingStaticIP()) { |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 937 | // If we are using a statically configured IP address instead |
| 938 | // of a leased IP address, release any acquired lease so it may |
| 939 | // be used by others. This allows us to merge other non-leased |
| 940 | // parameters (like DNS) when they're available from a DHCP server |
| 941 | // and not overridden by static parameters, but at the same time |
| 942 | // we avoid taking up a dynamic IP address the DHCP server could |
| 943 | // assign to someone else who might actually use it. |
| 944 | ipconfig->ReleaseIP(IPConfig::kReleaseReasonStaticIP); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 945 | } |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 946 | } |
Garret Kelly | d01b5cc | 2015-03-12 16:20:55 -0400 | [diff] [blame] | 947 | if (!IsUsingStaticNameServers()) { |
| 948 | PrependDNSServersIntoIPConfig(ipconfig); |
| 949 | } |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 950 | SetupConnection(ipconfig); |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 951 | UpdateIPConfigsProperty(); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 952 | } |
| 953 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 954 | void Device::OnIPConfigFailed(const IPConfigRefPtr& ipconfig) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 955 | SLOG(this, 2) << __func__; |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 956 | // TODO(pstew): This logic gets yet more complex when multiple |
| 957 | // IPConfig types are run in parallel (e.g. DHCP and DHCP6) |
| 958 | if (selected_service_) { |
Paul Stewart | 316acef | 2014-05-29 18:40:48 -0700 | [diff] [blame] | 959 | if (IsUsingStaticIP()) { |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 960 | // Consider three cases: |
| 961 | // |
| 962 | // 1. We're here because DHCP failed while starting up. There |
| 963 | // are two subcases: |
| 964 | // a. DHCP has failed, and Static IP config has _not yet_ |
| 965 | // completed. It's fine to do nothing, because we'll |
| 966 | // apply the static config shortly. |
| 967 | // b. DHCP has failed, and Static IP config has _already_ |
| 968 | // completed. It's fine to do nothing, because we can |
| 969 | // continue to use the static config that's already |
| 970 | // been applied. |
| 971 | // |
| 972 | // 2. We're here because a previously valid DHCP configuration |
| 973 | // is no longer valid. There's still a static IP config, |
| 974 | // because the condition in the if clause evaluated to true. |
| 975 | // Furthermore, the static config includes an IP address for |
| 976 | // us to use. |
| 977 | // |
| 978 | // The current configuration may include some DHCP |
| 979 | // parameters, overriden by any static parameters |
| 980 | // provided. We continue to use this configuration, because |
| 981 | // the only configuration element that is leased to us (IP |
| 982 | // address) will be overriden by a static parameter. |
| 983 | return; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | ipconfig->ResetProperties(); |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 988 | UpdateIPConfigsProperty(); |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 989 | |
| 990 | // Fallback to IPv6 if we have an IPv6 configuration that's ready for |
| 991 | // connection, and we're not currently using an IPv6 connection. |
| 992 | if (IPConfigCompleted(ip6config_) && |
| 993 | (!connection_ || !connection_->IsIPv6())) { |
| 994 | SetupConnection(ip6config_); |
| 995 | return; |
| 996 | } |
| 997 | |
| 998 | OnIPConfigFailure(); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 999 | DestroyConnection(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1002 | void Device::OnIPConfigRefreshed(const IPConfigRefPtr& ipconfig) { |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 1003 | // Clear the previously applied static IP parameters. |
| 1004 | ipconfig->RestoreSavedIPParameters( |
| 1005 | selected_service_->mutable_static_ip_parameters()); |
| 1006 | |
| 1007 | dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask, |
| 1008 | weak_ptr_factory_.GetWeakPtr())); |
| 1009 | } |
| 1010 | |
Paul Stewart | f6f9648 | 2013-07-12 12:49:15 -0700 | [diff] [blame] | 1011 | void Device::OnIPConfigFailure() { |
| 1012 | if (selected_service_) { |
| 1013 | Error error; |
Samuel Tan | 0d06119 | 2014-07-07 15:45:15 -0700 | [diff] [blame] | 1014 | selected_service_->DisconnectWithFailure(Service::kFailureDHCP, |
| 1015 | &error, |
| 1016 | __func__); |
Paul Stewart | f6f9648 | 2013-07-12 12:49:15 -0700 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1020 | void Device::OnIPConfigExpired(const IPConfigRefPtr& ipconfig) { |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 1021 | metrics()->SendToUMA( |
| 1022 | metrics()->GetFullMetricName( |
mukesh agrawal | 132e96f | 2014-04-24 11:49:42 -0700 | [diff] [blame] | 1023 | Metrics::kMetricExpiredLeaseLengthSecondsSuffix, technology()), |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 1024 | ipconfig->properties().lease_duration_seconds, |
| 1025 | Metrics::kMetricExpiredLeaseLengthSecondsMin, |
| 1026 | Metrics::kMetricExpiredLeaseLengthSecondsMax, |
| 1027 | Metrics::kMetricExpiredLeaseLengthSecondsNumBuckets); |
| 1028 | } |
| 1029 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1030 | void Device::OnDHCPv6ConfigUpdated(const IPConfigRefPtr& ipconfig, |
Peter Qiu | d48fa0c | 2015-06-10 12:20:48 -0700 | [diff] [blame] | 1031 | bool /*new_lease_acquired*/) { |
| 1032 | // Emit configuration update. |
| 1033 | UpdateIPConfigsProperty(); |
| 1034 | } |
| 1035 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1036 | void Device::OnDHCPv6ConfigFailed(const IPConfigRefPtr& ipconfig) { |
Peter Qiu | d48fa0c | 2015-06-10 12:20:48 -0700 | [diff] [blame] | 1037 | // Reset configuration data. |
| 1038 | ipconfig->ResetProperties(); |
| 1039 | UpdateIPConfigsProperty(); |
| 1040 | } |
| 1041 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1042 | void Device::OnDHCPv6ConfigExpired(const IPConfigRefPtr& ipconfig) { |
Peter Qiu | d48fa0c | 2015-06-10 12:20:48 -0700 | [diff] [blame] | 1043 | // Reset configuration data. |
| 1044 | ipconfig->ResetProperties(); |
| 1045 | UpdateIPConfigsProperty(); |
| 1046 | } |
| 1047 | |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 1048 | void Device::OnConnected() { |
| 1049 | if (selected_service_->unreliable()) { |
| 1050 | // Post a delayed task to reset link back to reliable if no link |
| 1051 | // failure is detected in the next 5 minutes. |
| 1052 | reliable_link_callback_.Reset( |
| 1053 | base::Bind(&Device::OnReliableLink, base::Unretained(this))); |
| 1054 | dispatcher_->PostDelayedTask( |
| 1055 | reliable_link_callback_.callback(), |
| 1056 | kLinkUnreliableThresholdSeconds * 1000); |
| 1057 | } |
| 1058 | } |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 1059 | |
Paul Stewart | 8596f9f | 2013-03-14 07:58:26 -0700 | [diff] [blame] | 1060 | void Device::OnConnectionUpdated() { |
| 1061 | if (selected_service_) { |
| 1062 | manager_->UpdateService(selected_service_); |
| 1063 | } |
| 1064 | } |
| 1065 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 1066 | void Device::CreateConnection() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1067 | SLOG(this, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 1068 | if (!connection_.get()) { |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 1069 | connection_ = new Connection(interface_index_, |
| 1070 | link_name_, |
| 1071 | technology_, |
| 1072 | manager_->device_info()); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | void Device::DestroyConnection() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1077 | SLOG(this, 2) << __func__ << " on " << link_name_; |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 1078 | StopAllActivities(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 1079 | if (selected_service_.get()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1080 | SLOG(this, 3) << "Clearing connection of service " |
| 1081 | << selected_service_->unique_name(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 1082 | selected_service_->SetConnection(nullptr); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 1083 | } |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 1084 | connection_ = nullptr; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1087 | void Device::SelectService(const ServiceRefPtr& service) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1088 | SLOG(this, 2) << __func__ << ": service " |
| 1089 | << (service ? service->unique_name() : "*reset*") |
| 1090 | << " on " << link_name_; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1091 | |
| 1092 | if (selected_service_.get() == service.get()) { |
| 1093 | // No change to |selected_service_|. Return early to avoid |
| 1094 | // changing its state. |
| 1095 | return; |
| 1096 | } |
| 1097 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 1098 | if (selected_service_.get()) { |
| 1099 | if (selected_service_->state() != Service::kStateFailure) { |
| 1100 | selected_service_->SetState(Service::kStateIdle); |
| 1101 | } |
Paul Stewart | 20b0a09 | 2012-05-22 20:39:57 -0700 | [diff] [blame] | 1102 | // Just in case the Device subclass has not already done so, make |
| 1103 | // sure the previously selected service has its connection removed. |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 1104 | selected_service_->SetConnection(nullptr); |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 1105 | // Reset link status for the previously selected service. |
| 1106 | selected_service_->set_unreliable(false); |
| 1107 | reliable_link_callback_.Cancel(); |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 1108 | StopAllActivities(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 1109 | } |
Peter Qiu | a057203 | 2014-09-26 10:07:37 -0700 | [diff] [blame] | 1110 | |
| 1111 | // Newly selected service (network), previous failures doesn't apply |
| 1112 | // anymore. |
| 1113 | last_link_monitor_failed_time_ = 0; |
| 1114 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 1115 | selected_service_ = service; |
Christopher Wiley | 674598d | 2014-12-12 10:21:39 -0800 | [diff] [blame] | 1116 | adaptor_->EmitRpcIdentifierChanged( |
| 1117 | kSelectedServiceProperty, GetSelectedServiceRpcIdentifier(nullptr)); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | void Device::SetServiceState(Service::ConnectState state) { |
| 1121 | if (selected_service_.get()) { |
| 1122 | selected_service_->SetState(state); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | void Device::SetServiceFailure(Service::ConnectFailure failure_state) { |
| 1127 | if (selected_service_.get()) { |
| 1128 | selected_service_->SetFailure(failure_state); |
| 1129 | } |
| 1130 | } |
| 1131 | |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 1132 | void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) { |
| 1133 | if (selected_service_.get()) { |
| 1134 | selected_service_->SetFailureSilent(failure_state); |
| 1135 | } |
| 1136 | } |
| 1137 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1138 | bool Device::SetIPFlag(IPAddress::Family family, const string& flag, |
| 1139 | const string& value) { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 1140 | string ip_version; |
| 1141 | if (family == IPAddress::kFamilyIPv4) { |
| 1142 | ip_version = kIPFlagVersion4; |
| 1143 | } else if (family == IPAddress::kFamilyIPv6) { |
| 1144 | ip_version = kIPFlagVersion6; |
| 1145 | } else { |
| 1146 | NOTIMPLEMENTED(); |
| 1147 | } |
| 1148 | FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(), |
| 1149 | link_name_.c_str(), flag.c_str())); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1150 | SLOG(this, 2) << "Writing " << value << " to flag file " |
| 1151 | << flag_file.value(); |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 1152 | if (base::WriteFile(flag_file, value.c_str(), value.length()) != 1) { |
Paul Stewart | 38fcf16 | 2015-06-12 09:52:17 -0700 | [diff] [blame] | 1153 | string message = StringPrintf("IP flag write failed: %s to %s", |
| 1154 | value.c_str(), flag_file.value().c_str()); |
| 1155 | if (!base::PathExists(flag_file) && |
| 1156 | ContainsValue(written_flags_, flag_file.value())) { |
| 1157 | SLOG(this, 2) << message << " (device is no longer present?)"; |
| 1158 | } else { |
| 1159 | LOG(ERROR) << message; |
| 1160 | } |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 1161 | return false; |
Paul Stewart | 38fcf16 | 2015-06-12 09:52:17 -0700 | [diff] [blame] | 1162 | } else { |
| 1163 | written_flags_.insert(flag_file.value()); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 1164 | } |
| 1165 | return true; |
| 1166 | } |
| 1167 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1168 | string Device::PerformTDLSOperation(const string& /* operation */, |
| 1169 | const string& /* peer */, |
| 1170 | Error* /* error */) { |
Paul Stewart | c6fbad9 | 2013-11-13 14:50:52 -0800 | [diff] [blame] | 1171 | return ""; |
| 1172 | } |
| 1173 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 1174 | void Device::ResetByteCounters() { |
| 1175 | manager_->device_info()->GetByteCounts( |
| 1176 | interface_index_, &receive_byte_offset_, &transmit_byte_offset_); |
| 1177 | manager_->UpdateDevice(this); |
| 1178 | } |
| 1179 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1180 | bool Device::RestartPortalDetection() { |
| 1181 | StopPortalDetection(); |
| 1182 | return StartPortalDetection(); |
| 1183 | } |
| 1184 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1185 | bool Device::RequestPortalDetection() { |
| 1186 | if (!selected_service_) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1187 | SLOG(this, 2) << FriendlyName() |
| 1188 | << ": No selected service, so no need for portal check."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1189 | return false; |
| 1190 | } |
| 1191 | |
| 1192 | if (!connection_.get()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1193 | SLOG(this, 2) << FriendlyName() |
| 1194 | << ": No connection, so no need for portal check."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1195 | return false; |
| 1196 | } |
| 1197 | |
| 1198 | if (selected_service_->state() != Service::kStatePortal) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1199 | SLOG(this, 2) << FriendlyName() |
| 1200 | << ": Service is not in portal state. " |
| 1201 | << "No need to start check."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1202 | return false; |
| 1203 | } |
| 1204 | |
| 1205 | if (!connection_->is_default()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1206 | SLOG(this, 2) << FriendlyName() |
| 1207 | << ": Service is not the default connection. " |
| 1208 | << "Don't start check."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1209 | return false; |
| 1210 | } |
| 1211 | |
| 1212 | if (portal_detector_.get() && portal_detector_->IsInProgress()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1213 | SLOG(this, 2) << FriendlyName() |
| 1214 | << ": Portal detection is already running."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1215 | return true; |
| 1216 | } |
| 1217 | |
| 1218 | return StartPortalDetection(); |
| 1219 | } |
| 1220 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1221 | bool Device::StartPortalDetection() { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1222 | DCHECK(selected_service_); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1223 | if (selected_service_->IsPortalDetectionDisabled()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1224 | SLOG(this, 2) << "Service " << selected_service_->unique_name() |
| 1225 | << ": Portal detection is disabled; " |
| 1226 | << "marking service online."; |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1227 | SetServiceConnectedState(Service::kStateOnline); |
| 1228 | return false; |
| 1229 | } |
| 1230 | |
| 1231 | if (selected_service_->IsPortalDetectionAuto() && |
| 1232 | !manager_->IsPortalDetectionEnabled(technology())) { |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1233 | // If portal detection is disabled for this technology, immediately set |
| 1234 | // the service state to "Online". |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1235 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1236 | << ": Portal detection is disabled; " |
| 1237 | << "marking service online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1238 | SetServiceConnectedState(Service::kStateOnline); |
| 1239 | return false; |
| 1240 | } |
| 1241 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1242 | if (selected_service_->HasProxyConfig()) { |
| 1243 | // Services with HTTP proxy configurations should not be checked by the |
| 1244 | // connection manager, since we don't have the ability to evaluate |
| 1245 | // arbitrary proxy configs and their possible credentials. |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1246 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1247 | << ": Service has proxy config; marking it online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1248 | SetServiceConnectedState(Service::kStateOnline); |
| 1249 | return false; |
| 1250 | } |
| 1251 | |
| 1252 | portal_detector_.reset(new PortalDetector(connection_, |
| 1253 | dispatcher_, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 1254 | portal_detector_callback_)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1255 | if (!portal_detector_->Start(manager_->GetPortalCheckURL())) { |
| 1256 | LOG(ERROR) << "Device " << FriendlyName() |
| 1257 | << ": Portal detection failed to start: likely bad URL: " |
| 1258 | << manager_->GetPortalCheckURL(); |
| 1259 | SetServiceConnectedState(Service::kStateOnline); |
| 1260 | return false; |
| 1261 | } |
| 1262 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1263 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1264 | << ": Portal detection has started."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1265 | return true; |
| 1266 | } |
| 1267 | |
| 1268 | void Device::StopPortalDetection() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1269 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1270 | << ": Portal detection stopping."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1271 | portal_detector_.reset(); |
| 1272 | } |
| 1273 | |
Rebecca Silberstein | 6862b38 | 2014-09-11 08:24:51 -0700 | [diff] [blame] | 1274 | bool Device::StartConnectivityTest() { |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 1275 | LOG(INFO) << "Device " << FriendlyName() << " starting connectivity test."; |
Rebecca Silberstein | 6862b38 | 2014-09-11 08:24:51 -0700 | [diff] [blame] | 1276 | |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 1277 | connection_tester_.reset(new ConnectionTester(connection_, |
| 1278 | dispatcher_, |
| 1279 | connection_tester_callback_)); |
| 1280 | connection_tester_->Start(); |
| 1281 | return true; |
Rebecca Silberstein | 6862b38 | 2014-09-11 08:24:51 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
Paul Stewart | e8303eb | 2014-10-08 22:51:14 -0700 | [diff] [blame] | 1284 | void Device::StopConnectivityTest() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1285 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1286 | << ": Connectivity test stopping."; |
Paul Stewart | e8303eb | 2014-10-08 22:51:14 -0700 | [diff] [blame] | 1287 | connection_tester_.reset(); |
| 1288 | } |
| 1289 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1290 | void Device::set_link_monitor(LinkMonitor* link_monitor) { |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1291 | link_monitor_.reset(link_monitor); |
| 1292 | } |
| 1293 | |
| 1294 | bool Device::StartLinkMonitor() { |
| 1295 | if (!manager_->IsTechnologyLinkMonitorEnabled(technology())) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1296 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1297 | << ": Link Monitoring is disabled."; |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1298 | return false; |
| 1299 | } |
| 1300 | |
Peter Qiu | d49760e | 2014-09-19 16:13:42 -0700 | [diff] [blame] | 1301 | if (selected_service_ && selected_service_->link_monitor_disabled()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1302 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1303 | << ": Link Monitoring is disabled for the selected service"; |
Peter Qiu | d49760e | 2014-09-19 16:13:42 -0700 | [diff] [blame] | 1304 | return false; |
| 1305 | } |
| 1306 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1307 | if (!link_monitor()) { |
| 1308 | set_link_monitor( |
| 1309 | new LinkMonitor( |
| 1310 | connection_, dispatcher_, metrics(), manager_->device_info(), |
Peter Qiu | b5d124f | 2014-04-14 12:05:02 -0700 | [diff] [blame] | 1311 | Bind(&Device::OnLinkMonitorFailure, weak_ptr_factory_.GetWeakPtr()), |
| 1312 | Bind(&Device::OnLinkMonitorGatewayChange, |
| 1313 | weak_ptr_factory_.GetWeakPtr()))); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1316 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1317 | << ": Link Monitor starting."; |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1318 | return link_monitor_->Start(); |
| 1319 | } |
| 1320 | |
| 1321 | void Device::StopLinkMonitor() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1322 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1323 | << ": Link Monitor stopping."; |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1324 | link_monitor_.reset(); |
| 1325 | } |
| 1326 | |
Peter Qiu | 8e1ad16 | 2014-10-02 15:58:24 -0700 | [diff] [blame] | 1327 | void Device::OnUnreliableLink() { |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 1328 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1329 | << ": Link is unreliable."; |
| 1330 | selected_service_->set_unreliable(true); |
| 1331 | reliable_link_callback_.Cancel(); |
Peter Qiu | 8e1ad16 | 2014-10-02 15:58:24 -0700 | [diff] [blame] | 1332 | metrics_->NotifyUnreliableLinkSignalStrength( |
| 1333 | technology_, selected_service_->strength()); |
| 1334 | } |
| 1335 | |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 1336 | void Device::OnReliableLink() { |
| 1337 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1338 | << ": Link is reliable."; |
| 1339 | selected_service_->set_unreliable(false); |
| 1340 | // TODO(zqiu): report signal strength to UMA. |
| 1341 | } |
| 1342 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1343 | void Device::OnLinkMonitorFailure() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1344 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1345 | << ": Link Monitor indicates failure."; |
Peter Qiu | a057203 | 2014-09-26 10:07:37 -0700 | [diff] [blame] | 1346 | if (!selected_service_) { |
| 1347 | return; |
| 1348 | } |
| 1349 | |
| 1350 | time_t now; |
| 1351 | time_->GetSecondsBoottime(&now); |
| 1352 | |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 1353 | if (last_link_monitor_failed_time_ != 0 && |
| 1354 | now - last_link_monitor_failed_time_ < kLinkUnreliableThresholdSeconds) { |
Peter Qiu | 8e1ad16 | 2014-10-02 15:58:24 -0700 | [diff] [blame] | 1355 | OnUnreliableLink(); |
Peter Qiu | a057203 | 2014-09-26 10:07:37 -0700 | [diff] [blame] | 1356 | } |
| 1357 | last_link_monitor_failed_time_ = now; |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
Peter Qiu | 9d58193 | 2014-04-14 16:37:37 -0700 | [diff] [blame] | 1360 | void Device::OnLinkMonitorGatewayChange() { |
| 1361 | string gateway_mac = link_monitor()->gateway_mac_address().HexEncode(); |
| 1362 | int connection_id = manager_->CalcConnectionId( |
| 1363 | ipconfig_->properties().gateway, gateway_mac); |
| 1364 | |
| 1365 | CHECK(selected_service_); |
| 1366 | selected_service_->set_connection_id(connection_id); |
| 1367 | |
| 1368 | manager_->ReportServicesOnSameNetwork(connection_id); |
| 1369 | } |
| 1370 | |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1371 | bool Device::StartDNSTest( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1372 | const vector<string>& dns_servers, |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1373 | bool retry_until_success, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1374 | const Callback<void(const DNSServerTester::Status)>& callback) { |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1375 | if (dns_server_tester_.get()) { |
| 1376 | LOG(ERROR) << FriendlyName() << ": " |
| 1377 | << "Failed to start DNS Test: current test still running"; |
| 1378 | return false; |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1381 | dns_server_tester_.reset(new DNSServerTester(connection_, |
| 1382 | dispatcher_, |
| 1383 | dns_servers, |
| 1384 | retry_until_success, |
| 1385 | callback)); |
| 1386 | dns_server_tester_->Start(); |
| 1387 | return true; |
| 1388 | } |
| 1389 | |
| 1390 | void Device::StopDNSTest() { |
| 1391 | dns_server_tester_.reset(); |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 1394 | void Device::FallbackDNSResultCallback(const DNSServerTester::Status status) { |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1395 | StopDNSTest(); |
Peter Qiu | f18e771 | 2014-05-20 09:59:46 -0700 | [diff] [blame] | 1396 | int result = Metrics::kFallbackDNSTestResultFailure; |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 1397 | if (status == DNSServerTester::kStatusSuccess) { |
Peter Qiu | f18e771 | 2014-05-20 09:59:46 -0700 | [diff] [blame] | 1398 | result = Metrics::kFallbackDNSTestResultSuccess; |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 1399 | |
| 1400 | // Switch to fallback DNS server if service is configured to allow DNS |
| 1401 | // fallback. |
| 1402 | CHECK(selected_service_); |
| 1403 | if (selected_service_->is_dns_auto_fallback_allowed()) { |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1404 | LOG(INFO) << "Device " << FriendlyName() |
| 1405 | << ": Switching to fallback DNS servers."; |
| 1406 | // Save the DNS servers from ipconfig. |
| 1407 | config_dns_servers_ = ipconfig_->properties().dns_servers; |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 1408 | SwitchDNSServers(vector<string>(std::begin(kFallbackDnsServers), |
| 1409 | std::end(kFallbackDnsServers))); |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1410 | // Start DNS test for configured DNS servers. |
| 1411 | StartDNSTest(config_dns_servers_, |
| 1412 | true, |
| 1413 | Bind(&Device::ConfigDNSResultCallback, |
| 1414 | weak_ptr_factory_.GetWeakPtr())); |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 1415 | } |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1416 | } |
Peter Qiu | f18e771 | 2014-05-20 09:59:46 -0700 | [diff] [blame] | 1417 | metrics()->NotifyFallbackDNSTestResult(technology_, result); |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | void Device::ConfigDNSResultCallback(const DNSServerTester::Status status) { |
| 1421 | StopDNSTest(); |
| 1422 | // DNS test failed to start due to internal error. |
| 1423 | if (status == DNSServerTester::kStatusFailure) { |
| 1424 | return; |
| 1425 | } |
| 1426 | |
| 1427 | // Switch back to the configured DNS servers. |
| 1428 | LOG(INFO) << "Device " << FriendlyName() |
| 1429 | << ": Switching back to configured DNS servers."; |
| 1430 | SwitchDNSServers(config_dns_servers_); |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1433 | void Device::SwitchDNSServers(const vector<string>& dns_servers) { |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 1434 | CHECK(ipconfig_); |
| 1435 | CHECK(connection_); |
| 1436 | // Push new DNS servers setting to the IP config object. |
| 1437 | ipconfig_->UpdateDNSServers(dns_servers); |
| 1438 | // Push new DNS servers setting to the current connection, so the resolver |
| 1439 | // will be updated to use the new DNS servers. |
| 1440 | connection_->UpdateDNSServers(dns_servers); |
| 1441 | // Allow the service to notify Chrome of ipconfig changes. |
| 1442 | selected_service_->NotifyIPConfigChanges(); |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1443 | // Restart the portal detection with the new DNS setting. |
| 1444 | RestartPortalDetection(); |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 1445 | } |
| 1446 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1447 | void Device::set_traffic_monitor(TrafficMonitor* traffic_monitor) { |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 1448 | traffic_monitor_.reset(traffic_monitor); |
| 1449 | } |
| 1450 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1451 | bool Device::TimeToNextDHCPLeaseRenewal(uint32_t* result) { |
Samuel Tan | 787a1ce | 2014-11-11 17:17:27 -0800 | [diff] [blame] | 1452 | if (!ipconfig() && !ip6config()) { |
| 1453 | return false; |
| 1454 | } |
| 1455 | uint32_t time_to_ipv4_lease_expiry = UINT32_MAX; |
| 1456 | uint32_t time_to_ipv6_lease_expiry = UINT32_MAX; |
| 1457 | if (ipconfig()) { |
| 1458 | ipconfig()->TimeToLeaseExpiry(&time_to_ipv4_lease_expiry); |
| 1459 | } |
| 1460 | if (ip6config()) { |
| 1461 | ip6config()->TimeToLeaseExpiry(&time_to_ipv6_lease_expiry); |
| 1462 | } |
| 1463 | *result = std::min(time_to_ipv4_lease_expiry, time_to_ipv6_lease_expiry); |
| 1464 | return true; |
| 1465 | } |
| 1466 | |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 1467 | bool Device::IsTrafficMonitorEnabled() const { |
| 1468 | return false; |
| 1469 | } |
| 1470 | |
| 1471 | void Device::StartTrafficMonitor() { |
| 1472 | // Return if traffic monitor is not enabled for this device. |
| 1473 | if (!IsTrafficMonitorEnabled()) { |
| 1474 | return; |
| 1475 | } |
| 1476 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1477 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1478 | << ": Traffic Monitor starting."; |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 1479 | if (!traffic_monitor_.get()) { |
| 1480 | traffic_monitor_.reset(new TrafficMonitor(this, dispatcher_)); |
| 1481 | traffic_monitor_->set_network_problem_detected_callback( |
| 1482 | Bind(&Device::OnEncounterNetworkProblem, |
| 1483 | weak_ptr_factory_.GetWeakPtr())); |
| 1484 | } |
| 1485 | traffic_monitor_->Start(); |
| 1486 | } |
| 1487 | |
| 1488 | void Device::StopTrafficMonitor() { |
| 1489 | // Return if traffic monitor is not enabled for this device. |
| 1490 | if (!IsTrafficMonitorEnabled()) { |
| 1491 | return; |
| 1492 | } |
| 1493 | |
| 1494 | if (traffic_monitor_.get()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1495 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1496 | << ": Traffic Monitor stopping."; |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 1497 | traffic_monitor_->Stop(); |
| 1498 | } |
| 1499 | traffic_monitor_.reset(); |
| 1500 | } |
| 1501 | |
| 1502 | void Device::OnEncounterNetworkProblem(int reason) { |
| 1503 | int metric_code; |
| 1504 | switch (reason) { |
| 1505 | case TrafficMonitor::kNetworkProblemCongestedTxQueue: |
| 1506 | metric_code = Metrics::kNetworkProblemCongestedTCPTxQueue; |
| 1507 | break; |
| 1508 | case TrafficMonitor::kNetworkProblemDNSFailure: |
| 1509 | metric_code = Metrics::kNetworkProblemDNSFailure; |
| 1510 | break; |
| 1511 | default: |
| 1512 | LOG(ERROR) << "Invalid network problem code: " << reason; |
| 1513 | return; |
| 1514 | } |
| 1515 | |
| 1516 | metrics()->NotifyNetworkProblemDetected(technology_, metric_code); |
| 1517 | // Stop the traffic monitor, only report the first network problem detected |
| 1518 | // on the connection for now. |
| 1519 | StopTrafficMonitor(); |
| 1520 | } |
| 1521 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1522 | void Device::SetServiceConnectedState(Service::ConnectState state) { |
| 1523 | DCHECK(selected_service_.get()); |
| 1524 | |
| 1525 | if (!selected_service_.get()) { |
| 1526 | LOG(ERROR) << FriendlyName() << ": " |
| 1527 | << "Portal detection completed but no selected service exists!"; |
| 1528 | return; |
| 1529 | } |
| 1530 | |
| 1531 | if (!selected_service_->IsConnected()) { |
| 1532 | LOG(ERROR) << FriendlyName() << ": " |
| 1533 | << "Portal detection completed but selected service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1534 | << selected_service_->unique_name() |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1535 | << " is in non-connected state."; |
| 1536 | return; |
| 1537 | } |
| 1538 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1539 | if (state == Service::kStatePortal && connection_->is_default() && |
| 1540 | manager_->GetPortalCheckInterval() != 0) { |
| 1541 | CHECK(portal_detector_.get()); |
| 1542 | if (!portal_detector_->StartAfterDelay( |
| 1543 | manager_->GetPortalCheckURL(), |
| 1544 | manager_->GetPortalCheckInterval())) { |
| 1545 | LOG(ERROR) << "Device " << FriendlyName() |
| 1546 | << ": Portal detection failed to restart: likely bad URL: " |
| 1547 | << manager_->GetPortalCheckURL(); |
| 1548 | SetServiceState(Service::kStateOnline); |
| 1549 | portal_detector_.reset(); |
| 1550 | return; |
| 1551 | } |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1552 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1553 | << ": Portal detection retrying."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1554 | } else { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1555 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1556 | << ": Portal will not retry."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1557 | portal_detector_.reset(); |
| 1558 | } |
| 1559 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1560 | SetServiceState(state); |
| 1561 | } |
| 1562 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1563 | void Device::PortalDetectorCallback(const PortalDetector::Result& result) { |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1564 | if (!result.final) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1565 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1566 | << ": Received non-final status: " |
| 1567 | << ConnectivityTrial::StatusToString( |
| 1568 | result.trial_result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1569 | return; |
| 1570 | } |
| 1571 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1572 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1573 | << ": Received final status: " |
| 1574 | << ConnectivityTrial::StatusToString( |
| 1575 | result.trial_result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1576 | |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1577 | portal_attempts_to_online_ += result.num_attempts; |
| 1578 | |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1579 | int portal_status = Metrics::PortalDetectionResultToEnum(result); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1580 | metrics()->SendEnumToUMA( |
mukesh agrawal | 132e96f | 2014-04-24 11:49:42 -0700 | [diff] [blame] | 1581 | metrics()->GetFullMetricName(Metrics::kMetricPortalResultSuffix, |
| 1582 | technology()), |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1583 | portal_status, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1584 | Metrics::kPortalResultMax); |
| 1585 | |
Rebecca Silberstein | 3d49ea4 | 2014-08-21 11:20:50 -0700 | [diff] [blame] | 1586 | if (result.trial_result.status == ConnectivityTrial::kStatusSuccess) { |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1587 | SetServiceConnectedState(Service::kStateOnline); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1588 | |
| 1589 | metrics()->SendToUMA( |
| 1590 | metrics()->GetFullMetricName( |
mukesh agrawal | 132e96f | 2014-04-24 11:49:42 -0700 | [diff] [blame] | 1591 | Metrics::kMetricPortalAttemptsToOnlineSuffix, technology()), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1592 | portal_attempts_to_online_, |
| 1593 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 1594 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 1595 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1596 | } else { |
Peter Qiu | 9b83c89 | 2014-08-09 23:06:02 -0700 | [diff] [blame] | 1597 | // Set failure phase and status. |
| 1598 | if (selected_service_.get()) { |
| 1599 | selected_service_->SetPortalDetectionFailure( |
Rebecca Silberstein | 3d49ea4 | 2014-08-21 11:20:50 -0700 | [diff] [blame] | 1600 | ConnectivityTrial::PhaseToString(result.trial_result.phase), |
| 1601 | ConnectivityTrial::StatusToString(result.trial_result.status)); |
Peter Qiu | 9b83c89 | 2014-08-09 23:06:02 -0700 | [diff] [blame] | 1602 | } |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1603 | SetServiceConnectedState(Service::kStatePortal); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1604 | |
| 1605 | metrics()->SendToUMA( |
| 1606 | metrics()->GetFullMetricName( |
mukesh agrawal | 132e96f | 2014-04-24 11:49:42 -0700 | [diff] [blame] | 1607 | Metrics::kMetricPortalAttemptsSuffix, technology()), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1608 | result.num_attempts, |
| 1609 | Metrics::kMetricPortalAttemptsMin, |
| 1610 | Metrics::kMetricPortalAttemptsMax, |
| 1611 | Metrics::kMetricPortalAttemptsNumBuckets); |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1612 | |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 1613 | // TODO(zqiu): Only support fallback DNS server for IPv4 for now. |
| 1614 | if (connection_->IsIPv6()) { |
| 1615 | return; |
| 1616 | } |
| 1617 | |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1618 | // Perform fallback DNS test if the portal failure is DNS related. |
| 1619 | // The test will send a DNS request to Google's DNS server to determine |
| 1620 | // if the DNS failure is due to bad DNS server settings. |
| 1621 | if ((portal_status == Metrics::kPortalResultDNSFailure) || |
| 1622 | (portal_status == Metrics::kPortalResultDNSTimeout)) { |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1623 | StartDNSTest(vector<string>(std::begin(kFallbackDnsServers), |
| 1624 | std::end(kFallbackDnsServers)), |
| 1625 | false, |
| 1626 | Bind(&Device::FallbackDNSResultCallback, |
| 1627 | weak_ptr_factory_.GetWeakPtr())); |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1628 | } |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1629 | } |
| 1630 | } |
| 1631 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1632 | string Device::GetSelectedServiceRpcIdentifier(Error* /*error*/) { |
Christopher Wiley | 674598d | 2014-12-12 10:21:39 -0800 | [diff] [blame] | 1633 | if (!selected_service_) { |
| 1634 | return "/"; |
| 1635 | } |
| 1636 | return selected_service_->GetRpcIdentifier(); |
| 1637 | } |
| 1638 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1639 | vector<string> Device::AvailableIPConfigs(Error* /*error*/) { |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 1640 | vector<string> ipconfigs; |
| 1641 | if (ipconfig_) { |
| 1642 | ipconfigs.push_back(ipconfig_->GetRpcIdentifier()); |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 1643 | } |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 1644 | if (ip6config_) { |
| 1645 | ipconfigs.push_back(ip6config_->GetRpcIdentifier()); |
| 1646 | } |
Peter Qiu | d48fa0c | 2015-06-10 12:20:48 -0700 | [diff] [blame] | 1647 | if (dhcpv6_config_) { |
| 1648 | ipconfigs.push_back(dhcpv6_config_->GetRpcIdentifier()); |
| 1649 | } |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 1650 | return ipconfigs; |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | string Device::GetRpcConnectionIdentifier() { |
| 1654 | return adaptor_->GetRpcConnectionIdentifier(); |
| 1655 | } |
| 1656 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1657 | uint64_t Device::GetLinkMonitorResponseTime(Error* error) { |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1658 | if (!link_monitor_.get()) { |
| 1659 | // It is not strictly an error that the link monitor does not |
| 1660 | // exist, but returning an error here allows the GetProperties |
| 1661 | // call in our Adaptor to omit this parameter. |
| 1662 | error->Populate(Error::kNotFound, "Device is not running LinkMonitor"); |
| 1663 | return 0; |
| 1664 | } |
| 1665 | return link_monitor_->GetResponseTimeMilliseconds(); |
| 1666 | } |
| 1667 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 1668 | uint64_t Device::GetReceiveByteCount() { |
| 1669 | uint64_t rx_byte_count = 0, tx_byte_count = 0; |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 1670 | manager_->device_info()->GetByteCounts( |
| 1671 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 1672 | return rx_byte_count - receive_byte_offset_; |
| 1673 | } |
| 1674 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 1675 | uint64_t Device::GetTransmitByteCount() { |
| 1676 | uint64_t rx_byte_count = 0, tx_byte_count = 0; |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 1677 | manager_->device_info()->GetByteCounts( |
| 1678 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 1679 | return tx_byte_count - transmit_byte_offset_; |
| 1680 | } |
| 1681 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1682 | uint64_t Device::GetReceiveByteCountProperty(Error* /*error*/) { |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 1683 | return GetReceiveByteCount(); |
| 1684 | } |
| 1685 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1686 | uint64_t Device::GetTransmitByteCountProperty(Error* /*error*/) { |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 1687 | return GetTransmitByteCount(); |
| 1688 | } |
| 1689 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 1690 | bool Device::IsUnderlyingDeviceEnabled() const { |
| 1691 | return false; |
| 1692 | } |
| 1693 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1694 | // callback |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1695 | void Device::OnEnabledStateChanged(const ResultCallback& callback, |
| 1696 | const Error& error) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1697 | SLOG(this, 2) << __func__ |
| 1698 | << " (target: " << enabled_pending_ << "," |
| 1699 | << " success: " << error.IsSuccess() << ")" |
| 1700 | << " on " << link_name_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1701 | if (error.IsSuccess()) { |
| 1702 | enabled_ = enabled_pending_; |
| 1703 | manager_->UpdateEnabledTechnologies(); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1704 | adaptor_->EmitBoolChanged(kPoweredProperty, enabled_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1705 | } |
Gary Morain | baeefdf | 2012-04-30 14:53:35 -0700 | [diff] [blame] | 1706 | enabled_pending_ = enabled_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1707 | if (!callback.is_null()) |
| 1708 | callback.Run(error); |
| 1709 | } |
| 1710 | |
| 1711 | void Device::SetEnabled(bool enable) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1712 | SLOG(this, 2) << __func__ << "(" << enable << ")"; |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1713 | Error error; |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1714 | SetEnabledChecked(enable, false, &error, ResultCallback()); |
Arman Uguray | 8bf6a5c | 2013-08-12 14:18:03 -0700 | [diff] [blame] | 1715 | |
| 1716 | // SetEnabledInternal might fail here if there is an unfinished enable or |
| 1717 | // disable operation. Don't log error in this case, as this method is only |
| 1718 | // called when the underlying device is already in the target state and the |
| 1719 | // pending operation should eventually bring the device to the expected |
| 1720 | // state. |
| 1721 | LOG_IF(ERROR, |
| 1722 | error.IsFailure() && |
| 1723 | !error.IsOngoing() && |
| 1724 | error.type() != Error::kInProgress) |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1725 | << "Enabled failed, but no way to report the failure."; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1726 | } |
| 1727 | |
Ben Chan | 9f3dcf8 | 2013-09-25 18:04:58 -0700 | [diff] [blame] | 1728 | void Device::SetEnabledNonPersistent(bool enable, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1729 | Error* error, |
| 1730 | const ResultCallback& callback) { |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1731 | SetEnabledChecked(enable, false, error, callback); |
Ben Chan | 9f3dcf8 | 2013-09-25 18:04:58 -0700 | [diff] [blame] | 1732 | } |
| 1733 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1734 | void Device::SetEnabledPersistent(bool enable, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1735 | Error* error, |
| 1736 | const ResultCallback& callback) { |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1737 | SetEnabledChecked(enable, true, error, callback); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1738 | } |
| 1739 | |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1740 | void Device::SetEnabledChecked(bool enable, |
| 1741 | bool persist, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1742 | Error* error, |
| 1743 | const ResultCallback& callback) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1744 | DCHECK(error); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1745 | SLOG(this, 2) << "Device " << link_name_ << " " |
| 1746 | << (enable ? "starting" : "stopping"); |
Paul Stewart | ff6be29 | 2014-11-24 17:05:03 -0800 | [diff] [blame] | 1747 | if (enable && manager_->IsTechnologyProhibited(technology())) { |
| 1748 | error->Populate(Error::kPermissionDenied, "The " + |
| 1749 | Technology::NameFromIdentifier(technology()) + |
| 1750 | " technology is prohibited"); |
| 1751 | return; |
| 1752 | } |
| 1753 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1754 | if (enable == enabled_) { |
Arman Uguray | 2f352e6 | 2013-08-28 19:12:53 -0700 | [diff] [blame] | 1755 | if (enable != enabled_pending_ && persist) { |
| 1756 | // Return an error, as there is an ongoing operation to achieve the |
| 1757 | // opposite. |
| 1758 | Error::PopulateAndLog( |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 1759 | FROM_HERE, error, Error::kOperationFailed, |
Arman Uguray | 2f352e6 | 2013-08-28 19:12:53 -0700 | [diff] [blame] | 1760 | enable ? "Cannot enable while the device is disabling." : |
| 1761 | "Cannot disable while the device is enabling."); |
| 1762 | return; |
| 1763 | } |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1764 | LOG(INFO) << "Already in desired enable state."; |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1765 | error->Reset(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1766 | return; |
| 1767 | } |
| 1768 | |
| 1769 | if (enabled_pending_ == enable) { |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 1770 | Error::PopulateAndLog(FROM_HERE, error, Error::kInProgress, |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1771 | "Enable operation already in progress"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1772 | return; |
| 1773 | } |
| 1774 | |
| 1775 | if (persist) { |
| 1776 | enabled_persistent_ = enable; |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 1777 | manager_->UpdateDevice(this); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1778 | } |
| 1779 | |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1780 | SetEnabledUnchecked(enable, error, callback); |
| 1781 | } |
| 1782 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1783 | void Device::SetEnabledUnchecked(bool enable, Error* error, |
| 1784 | const ResultCallback& on_enable_complete) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1785 | enabled_pending_ = enable; |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1786 | EnabledStateChangedCallback chained_callback = |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1787 | Bind(&Device::OnEnabledStateChanged, |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1788 | weak_ptr_factory_.GetWeakPtr(), on_enable_complete); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1789 | if (enable) { |
| 1790 | running_ = true; |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1791 | Start(error, chained_callback); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1792 | } else { |
| 1793 | running_ = false; |
| 1794 | DestroyIPConfig(); // breaks a reference cycle |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 1795 | SelectService(nullptr); // breaks a reference cycle |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1796 | rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1797 | SLOG(this, 3) << "Device " << link_name_ << " ipconfig_ " |
| 1798 | << (ipconfig_ ? "is set." : "is not set."); |
| 1799 | SLOG(this, 3) << "Device " << link_name_ << " ip6config_ " |
| 1800 | << (ip6config_ ? "is set." : "is not set."); |
| 1801 | SLOG(this, 3) << "Device " << link_name_ << " connection_ " |
| 1802 | << (connection_ ? "is set." : "is not set."); |
| 1803 | SLOG(this, 3) << "Device " << link_name_ << " selected_service_ " |
| 1804 | << (selected_service_ ? "is set." : "is not set."); |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1805 | Stop(error, chained_callback); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1806 | } |
| 1807 | } |
| 1808 | |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 1809 | void Device::UpdateIPConfigsProperty() { |
| 1810 | adaptor_->EmitRpcIdentifierArrayChanged( |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 1811 | kIPConfigsProperty, AvailableIPConfigs(nullptr)); |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1814 | bool Device::ResolvePeerMacAddress(const string& input, |
| 1815 | string* output, |
| 1816 | Error* error) { |
Peter Qiu | 62abf31 | 2015-05-05 12:58:05 -0700 | [diff] [blame] | 1817 | if (!MakeHardwareAddressFromString(input).empty()) { |
| 1818 | // Input is already a MAC address. |
| 1819 | *output = input; |
| 1820 | return true; |
| 1821 | } |
| 1822 | |
| 1823 | IPAddress ip_address(IPAddress::kFamilyIPv4); |
| 1824 | if (!ip_address.SetAddressFromString(input)) { |
| 1825 | Error::PopulateAndLog(FROM_HERE, error, Error::kInvalidArguments, |
| 1826 | "Peer is neither an IP Address nor a MAC address"); |
| 1827 | return false; |
| 1828 | } |
| 1829 | |
| 1830 | // Peer address was specified as an IP address which we need to resolve. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1831 | const DeviceInfo* device_info = manager()->device_info(); |
Peter Qiu | 62abf31 | 2015-05-05 12:58:05 -0700 | [diff] [blame] | 1832 | if (!device_info->HasDirectConnectivityTo(interface_index_, ip_address)) { |
| 1833 | Error::PopulateAndLog(FROM_HERE, error, Error::kInvalidArguments, |
| 1834 | "IP address is not local to this interface"); |
| 1835 | return false; |
| 1836 | } |
| 1837 | |
| 1838 | ByteString mac_address; |
| 1839 | if (device_info->GetMACAddressOfPeer(interface_index_, |
| 1840 | ip_address, |
| 1841 | &mac_address)) { |
| 1842 | *output = MakeStringFromHardwareAddress( |
| 1843 | vector<uint8_t>(mac_address.GetConstData(), |
| 1844 | mac_address.GetConstData() + |
| 1845 | mac_address.GetLength())); |
| 1846 | SLOG(this, 2) << "ARP cache lookup returned peer: " << *output; |
| 1847 | return true; |
| 1848 | } |
| 1849 | |
| 1850 | if (!Icmp().TransmitEchoRequest(ip_address)) { |
| 1851 | Error::PopulateAndLog(FROM_HERE, error, Error::kOperationFailed, |
| 1852 | "Failed to send ICMP request to peer to setup ARP"); |
| 1853 | } else { |
| 1854 | // ARP request was transmitted successfully, address resolution is still |
| 1855 | // pending. |
| 1856 | error->Populate(Error::kInProgress, |
| 1857 | "Peer MAC address was not found in the ARP cache, " |
| 1858 | "but an ARP request was sent to find it. " |
| 1859 | "Please try again."); |
| 1860 | } |
| 1861 | return false; |
| 1862 | } |
| 1863 | |
| 1864 | // static |
| 1865 | vector<uint8_t> Device::MakeHardwareAddressFromString( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1866 | const string& address_string) { |
Peter Qiu | 62abf31 | 2015-05-05 12:58:05 -0700 | [diff] [blame] | 1867 | string address_nosep; |
| 1868 | base::RemoveChars(address_string, ":", &address_nosep); |
| 1869 | vector<uint8_t> address_bytes; |
| 1870 | base::HexStringToBytes(address_nosep, &address_bytes); |
| 1871 | if (address_bytes.size() != kHardwareAddressLength) { |
| 1872 | return vector<uint8_t>(); |
| 1873 | } |
| 1874 | return address_bytes; |
| 1875 | } |
| 1876 | |
| 1877 | // static |
| 1878 | string Device::MakeStringFromHardwareAddress( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 1879 | const vector<uint8_t>& address_bytes) { |
Peter Qiu | 62abf31 | 2015-05-05 12:58:05 -0700 | [diff] [blame] | 1880 | CHECK_EQ(kHardwareAddressLength, address_bytes.size()); |
| 1881 | return StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", |
| 1882 | address_bytes[0], address_bytes[1], address_bytes[2], |
| 1883 | address_bytes[3], address_bytes[4], address_bytes[5]); |
| 1884 | } |
| 1885 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1886 | } // namespace shill |