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 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 7 | #include <netinet/in.h> |
| 8 | #include <linux/if.h> // Needs definitions from netinet/in.h |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 10 | #include <time.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 11 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 12 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 13 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 14 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 15 | #include <base/bind.h> |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 16 | #include <base/file_util.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 17 | #include <base/memory/ref_counted.h> |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 18 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 19 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 20 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 21 | #include "shill/connection.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 22 | #include "shill/control_interface.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 23 | #include "shill/device_dbus_adaptor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 24 | #include "shill/dhcp_config.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 25 | #include "shill/dhcp_provider.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 26 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 27 | #include "shill/event_dispatcher.h" |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 28 | #include "shill/http_proxy.h" |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 29 | #include "shill/link_monitor.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 30 | #include "shill/logging.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 31 | #include "shill/manager.h" |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 32 | #include "shill/metrics.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 33 | #include "shill/property_accessor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 34 | #include "shill/refptr_types.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 35 | #include "shill/rtnl_handler.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 36 | #include "shill/service.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 37 | #include "shill/store_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 38 | #include "shill/technology.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 39 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 40 | using base::Bind; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 41 | using base::StringPrintf; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 42 | using std::string; |
| 43 | using std::vector; |
| 44 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 45 | namespace shill { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 46 | |
| 47 | // static |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 48 | const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s"; |
| 49 | // static |
| 50 | const char Device::kIPFlagVersion4[] = "ipv4"; |
| 51 | // static |
| 52 | const char Device::kIPFlagVersion6[] = "ipv6"; |
| 53 | // static |
| 54 | const char Device::kIPFlagDisableIPv6[] = "disable_ipv6"; |
| 55 | // static |
| 56 | const char Device::kIPFlagUseTempAddr[] = "use_tempaddr"; |
| 57 | // static |
| 58 | const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2"; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 59 | // static |
| 60 | const char Device::kIPFlagReversePathFilter[] = "rp_filter"; |
| 61 | // static |
| 62 | const char Device::kIPFlagReversePathFilterEnabled[] = "1"; |
| 63 | // static |
| 64 | const char Device::kIPFlagReversePathFilterLooseMode[] = "2"; |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 65 | // static |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 66 | const char Device::kStoragePowered[] = "Powered"; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 67 | // static |
| 68 | const char Device::kStorageIPConfigs[] = "IPConfigs"; |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 69 | // static |
| 70 | const char Device::kStorageReceiveByteCount[] = "ReceiveByteCount"; |
| 71 | // static |
| 72 | const char Device::kStorageTransmitByteCount[] = "TransmitByteCount"; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 73 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 74 | Device::Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 75 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 76 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 77 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 78 | const string &link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 79 | const string &address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 80 | int interface_index, |
| 81 | Technology::Identifier technology) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 82 | : enabled_(false), |
| 83 | enabled_persistent_(true), |
| 84 | enabled_pending_(enabled_), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 85 | reconnect_(true), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 86 | hardware_address_(address), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 87 | interface_index_(interface_index), |
| 88 | running_(false), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 89 | link_name_(link_name), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 90 | unique_id_(link_name), |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 91 | control_interface_(control_interface), |
| 92 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 93 | metrics_(metrics), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 94 | manager_(manager), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 95 | weak_ptr_factory_(this), |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 96 | adaptor_(control_interface->CreateDeviceAdaptor(this)), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 97 | portal_detector_callback_(Bind(&Device::PortalDetectorCallback, |
| 98 | weak_ptr_factory_.GetWeakPtr())), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 99 | technology_(technology), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 100 | portal_attempts_to_online_(0), |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 101 | receive_byte_offset_(0), |
| 102 | transmit_byte_offset_(0), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 103 | dhcp_provider_(DHCPProvider::GetInstance()), |
| 104 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 105 | store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 106 | |
| 107 | // flimflam::kBgscanMethodProperty: Registered in WiFi |
| 108 | // flimflam::kBgscanShortIntervalProperty: Registered in WiFi |
| 109 | // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi |
| 110 | |
| 111 | // flimflam::kCellularAllowRoamingProperty: Registered in Cellular |
| 112 | // flimflam::kCarrierProperty: Registered in Cellular |
| 113 | // flimflam::kEsnProperty: Registered in Cellular |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 114 | // flimflam::kHomeProviderProperty: Registered in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 115 | // flimflam::kImeiProperty: Registered in Cellular |
Ben Chan | a5e2708 | 2012-07-31 14:30:28 -0700 | [diff] [blame] | 116 | // flimflam::kIccidProperty: Registered in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 117 | // flimflam::kImsiProperty: Registered in Cellular |
| 118 | // flimflam::kManufacturerProperty: Registered in Cellular |
| 119 | // flimflam::kMdnProperty: Registered in Cellular |
| 120 | // flimflam::kMeidProperty: Registered in Cellular |
| 121 | // flimflam::kMinProperty: Registered in Cellular |
| 122 | // flimflam::kModelIDProperty: Registered in Cellular |
| 123 | // flimflam::kFirmwareRevisionProperty: Registered in Cellular |
| 124 | // flimflam::kHardwareRevisionProperty: Registered in Cellular |
| 125 | // flimflam::kPRLVersionProperty: Registered in Cellular |
| 126 | // flimflam::kSIMLockStatusProperty: Registered in Cellular |
| 127 | // flimflam::kFoundNetworksProperty: Registered in Cellular |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 128 | // flimflam::kDBusConnectionProperty: Registered in Cellular |
| 129 | // flimflam::kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 130 | |
Jason Glasgow | e8334fd | 2012-03-30 16:02:37 -0400 | [diff] [blame] | 131 | store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_); |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 132 | HelpRegisterConstDerivedRpcIdentifiers(flimflam::kIPConfigsProperty, |
| 133 | &Device::AvailableIPConfigs); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 134 | store_.RegisterConstString(flimflam::kNameProperty, &link_name_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 135 | store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_); |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 136 | HelpRegisterDerivedString(flimflam::kTypeProperty, |
| 137 | &Device::GetTechnologyString, |
| 138 | NULL); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 139 | HelpRegisterConstDerivedUint64(shill::kLinkMonitorResponseTimeProperty, |
| 140 | &Device::GetLinkMonitorResponseTime); |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 141 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 142 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 143 | // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 144 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 145 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 146 | // known_properties_.push_back(flimflam::kNetworksProperty); |
| 147 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 148 | // flimflam::kScanningProperty: Registered in WiFi, Cellular |
| 149 | // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 150 | |
| 151 | if (manager_ && manager_->device_info()) { // Unit tests may not have these. |
| 152 | manager_->device_info()->GetByteCounts( |
| 153 | interface_index_, &receive_byte_offset_, &transmit_byte_offset_); |
| 154 | HelpRegisterConstDerivedUint64(shill::kReceiveByteCountProperty, |
| 155 | &Device::GetReceiveByteCount); |
| 156 | HelpRegisterConstDerivedUint64(shill::kTransmitByteCountProperty, |
| 157 | &Device::GetTransmitByteCount); |
| 158 | } |
| 159 | |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 160 | LOG(INFO) << "Device created: " << link_name_ |
| 161 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | Device::~Device() { |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 165 | LOG(INFO) << "Device destructed: " << link_name_ |
| 166 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 169 | void Device::LinkEvent(unsigned flags, unsigned change) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 170 | SLOG(Device, 2) << "Device " << link_name_ |
| 171 | << std::showbase << std::hex |
| 172 | << " flags " << flags << " changed " << change |
| 173 | << std::dec << std::noshowbase; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 176 | void Device::Scan(Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 177 | SLOG(Device, 2) << "Device " << link_name_ << " scan requested."; |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 178 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 179 | "Device doesn't support scan."); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 182 | void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error, |
| 183 | const ResultCallback &/*callback*/) { |
| 184 | Error::PopulateAndLog(error, Error::kNotSupported, |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 185 | "Device doesn't support network registration."); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 188 | void Device::RequirePIN( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 189 | const string &/*pin*/, bool /*require*/, |
| 190 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 191 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 192 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 193 | "Device doesn't support RequirePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 196 | void Device::EnterPIN(const string &/*pin*/, |
| 197 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 198 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 199 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 200 | "Device doesn't support EnterPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 201 | } |
| 202 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 203 | void Device::UnblockPIN(const string &/*unblock_code*/, |
| 204 | const string &/*pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 205 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 206 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 207 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 208 | "Device doesn't support UnblockPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 209 | } |
| 210 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 211 | void Device::ChangePIN(const string &/*old_pin*/, |
| 212 | const string &/*new_pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 213 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 214 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 215 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 216 | "Device doesn't support ChangePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Darin Petkov | c37a9c4 | 2012-09-06 15:28:22 +0200 | [diff] [blame] | 219 | void Device::SetCarrier(const string &/*carrier*/, |
| 220 | Error *error, const ResultCallback &/*callback*/) { |
| 221 | SLOG(Device, 2) << __func__; |
| 222 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 223 | "Device doesn't support SetCarrier."); |
| 224 | } |
| 225 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 226 | void Device::DisableIPv6() { |
| 227 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1"); |
| 228 | } |
| 229 | |
| 230 | void Device::EnableIPv6() { |
| 231 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0"); |
| 232 | } |
| 233 | |
| 234 | void Device::EnableIPv6Privacy() { |
| 235 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr, |
| 236 | kIPFlagUseTempAddrUsedAndDefault); |
| 237 | } |
| 238 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 239 | void Device::DisableReversePathFilter() { |
| 240 | // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag |
| 241 | // for IPv6. crosbug.com/24228 |
| 242 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 243 | kIPFlagReversePathFilterLooseMode); |
| 244 | } |
| 245 | |
| 246 | void Device::EnableReversePathFilter() { |
| 247 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 248 | kIPFlagReversePathFilterEnabled); |
| 249 | } |
| 250 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 251 | bool Device::IsConnected() const { |
| 252 | if (selected_service_) |
| 253 | return selected_service_->IsConnected(); |
| 254 | return false; |
| 255 | } |
| 256 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 257 | bool Device::IsConnectedToService(const ServiceRefPtr &service) const { |
| 258 | return service == selected_service_ && IsConnected(); |
| 259 | } |
| 260 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 261 | string Device::GetRpcIdentifier() { |
| 262 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 265 | string Device::GetStorageIdentifier() { |
| 266 | string id = GetRpcIdentifier(); |
| 267 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 268 | size_t needle = id.find('_'); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 269 | DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 270 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 271 | return id; |
| 272 | } |
| 273 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 274 | string Device::GetTechnologyString(Error */*error*/) { |
| 275 | return Technology::NameFromIdentifier(technology()); |
| 276 | } |
| 277 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 278 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 279 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 282 | const string& Device::UniqueName() const { |
| 283 | return unique_id_; |
| 284 | } |
| 285 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 286 | bool Device::Load(StoreInterface *storage) { |
| 287 | const string id = GetStorageIdentifier(); |
| 288 | if (!storage->ContainsGroup(id)) { |
| 289 | LOG(WARNING) << "Device is not available in the persistent store: " << id; |
| 290 | return false; |
| 291 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 292 | enabled_persistent_ = true; |
| 293 | storage->GetBool(id, kStoragePowered, &enabled_persistent_); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 294 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 295 | |
| 296 | manager_->device_info()->GetByteCounts( |
| 297 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 298 | // If there is a byte-count present in the profile, the return value |
| 299 | // of Device::Get*ByteCount() should be the this stored value plus |
| 300 | // whatever additional bytes we receive since time-of-load. We |
| 301 | // accomplish this by the subtractions below, which can validly |
| 302 | // roll over "negative" in the subtractions below and in Get*ByteCount. |
| 303 | uint64 profile_byte_count; |
| 304 | if (storage->GetUint64(id, kStorageReceiveByteCount, &profile_byte_count)) { |
| 305 | receive_byte_offset_ = rx_byte_count - profile_byte_count; |
| 306 | } |
| 307 | if (storage->GetUint64(id, kStorageTransmitByteCount, &profile_byte_count)) { |
| 308 | transmit_byte_offset_ = tx_byte_count - profile_byte_count; |
| 309 | } |
| 310 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 311 | return true; |
| 312 | } |
| 313 | |
| 314 | bool Device::Save(StoreInterface *storage) { |
| 315 | const string id = GetStorageIdentifier(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 316 | storage->SetBool(id, kStoragePowered, enabled_persistent_); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 317 | if (ipconfig_.get()) { |
| 318 | // The _0 is an index into the list of IPConfigs that this device might |
| 319 | // have. We only have one IPConfig right now, and I hope to never have |
| 320 | // to support more, as sleffler indicates that associating IPConfigs |
| 321 | // with devices is wrong and due to be changed in flimflam anyhow. |
| 322 | string suffix = hardware_address_ + "_0"; |
| 323 | ipconfig_->Save(storage, suffix); |
| 324 | storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix)); |
| 325 | } |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 326 | storage->SetUint64(id, kStorageReceiveByteCount, GetReceiveByteCount(NULL)); |
| 327 | storage->SetUint64(id, kStorageTransmitByteCount, GetTransmitByteCount(NULL)); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 328 | return true; |
| 329 | } |
| 330 | |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 331 | void Device::OnBeforeSuspend() { |
| 332 | // Nothing to be done in the general case. |
| 333 | } |
| 334 | |
| 335 | void Device::OnAfterResume() { |
| 336 | if (ipconfig_) { |
| 337 | SLOG(Device, 3) << "Renewing IP address on resume."; |
| 338 | ipconfig_->RenewIP(); |
| 339 | } |
| 340 | } |
| 341 | |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 342 | void Device::DropConnection() { |
| 343 | SLOG(Device, 2) << __func__; |
| 344 | DestroyIPConfig(); |
| 345 | SelectService(NULL); |
| 346 | } |
| 347 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 348 | void Device::DestroyIPConfig() { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 349 | DisableIPv6(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 350 | if (ipconfig_.get()) { |
| 351 | ipconfig_->ReleaseIP(); |
| 352 | ipconfig_ = NULL; |
| 353 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 354 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 357 | bool Device::AcquireIPConfig() { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 358 | return AcquireIPConfigWithLeaseName(string()); |
| 359 | } |
| 360 | |
| 361 | bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 362 | DestroyIPConfig(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 363 | EnableIPv6(); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 364 | ipconfig_ = dhcp_provider_->CreateConfig(link_name_, |
| 365 | manager_->GetHostName(), |
| 366 | lease_name, |
| 367 | manager_->GetArpGateway()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 368 | ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated, |
| 369 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 370 | dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask, |
| 371 | weak_ptr_factory_.GetWeakPtr())); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 372 | return ipconfig_->RequestIP(); |
| 373 | } |
| 374 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 375 | void Device::HelpRegisterDerivedString( |
| 376 | const string &name, |
| 377 | string(Device::*get)(Error *error), |
| 378 | void(Device::*set)(const string &value, Error *error)) { |
| 379 | store_.RegisterDerivedString( |
| 380 | name, |
| 381 | StringAccessor(new CustomAccessor<Device, string>(this, get, set))); |
| 382 | } |
| 383 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 384 | void Device::HelpRegisterDerivedStrings( |
| 385 | const string &name, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 386 | Strings(Device::*get)(Error *error), |
| 387 | void(Device::*set)(const Strings &value, Error *error)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 388 | store_.RegisterDerivedStrings( |
| 389 | name, |
| 390 | StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 393 | void Device::HelpRegisterConstDerivedRpcIdentifiers( |
| 394 | const string &name, |
| 395 | RpcIdentifiers(Device::*get)(Error *)) { |
| 396 | store_.RegisterDerivedRpcIdentifiers( |
| 397 | name, |
| 398 | RpcIdentifiersAccessor( |
| 399 | new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL))); |
| 400 | } |
| 401 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 402 | void Device::HelpRegisterConstDerivedUint64( |
| 403 | const string &name, |
| 404 | uint64(Device::*get)(Error *)) { |
| 405 | store_.RegisterDerivedUint64( |
| 406 | name, |
| 407 | Uint64Accessor( |
| 408 | new CustomAccessor<Device, uint64>(this, get, NULL))); |
| 409 | } |
| 410 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 411 | void Device::ConfigureStaticIPTask() { |
| 412 | SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get() |
| 413 | << " ipconfig " << ipconfig_.get(); |
| 414 | |
| 415 | if (!selected_service_ || !ipconfig_) { |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | const StaticIPParameters &static_ip_parameters = |
| 420 | selected_service_->static_ip_parameters(); |
| 421 | if (static_ip_parameters.ContainsAddress()) { |
| 422 | SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters."; |
| 423 | // If the parameters contain an IP address, apply them now and bring |
| 424 | // the interface up. When DHCP information arrives, it will supplement |
| 425 | // the static information. |
| 426 | OnIPConfigUpdated(ipconfig_, true); |
| 427 | } else { |
| 428 | SLOG(Device, 2) << __func__ << " " << " no static IP address."; |
| 429 | } |
| 430 | } |
| 431 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 432 | void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 433 | SLOG(Device, 2) << __func__ << " " << " success: " << success; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 434 | if (success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 435 | CreateConnection(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 436 | if (selected_service_) { |
| 437 | ipconfig->ApplyStaticIPParameters( |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 438 | selected_service_->mutable_static_ip_parameters()); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 439 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 440 | connection_->UpdateFromIPConfig(ipconfig); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 441 | // SetConnection must occur after the UpdateFromIPConfig so the |
| 442 | // service can use the values derived from the connection. |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 443 | if (selected_service_) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 444 | selected_service_->SetConnection(connection_); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 445 | } |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 446 | // The service state change needs to happen last, so that at the |
| 447 | // time we report the state change to the manager, the service |
| 448 | // has its connection. |
| 449 | SetServiceState(Service::kStateConnected); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 450 | portal_attempts_to_online_ = 0; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 451 | // Subtle: Start portal detection after transitioning the service |
| 452 | // to the Connected state because this call may immediately transition |
| 453 | // to the Online state. |
Thieu Le | d176092 | 2012-09-11 14:15:35 -0700 | [diff] [blame] | 454 | if (selected_service_) { |
| 455 | StartPortalDetection(); |
| 456 | } |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 457 | StartLinkMonitor(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 458 | } else { |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 459 | // TODO(pstew): This logic gets yet more complex when multiple |
| 460 | // IPConfig types are run in parallel (e.g. DHCP and DHCP6) |
| 461 | if (selected_service_ && |
| 462 | selected_service_->static_ip_parameters().ContainsAddress()) { |
| 463 | // Consider three cases: |
| 464 | // |
| 465 | // 1. We're here because DHCP failed while starting up. There |
| 466 | // are two subcases: |
| 467 | // a. DHCP has failed, and Static IP config has _not yet_ |
| 468 | // completed. It's fine to do nothing, because we'll |
| 469 | // apply the static config shortly. |
| 470 | // b. DHCP has failed, and Static IP config has _already_ |
| 471 | // completed. It's fine to do nothing, because we can |
| 472 | // continue to use the static config that's already |
| 473 | // been applied. |
| 474 | // |
| 475 | // 2. We're here because a previously valid DHCP configuration |
| 476 | // is no longer valid. There's still a static IP config, |
| 477 | // because the condition in the if clause evaluated to true. |
| 478 | // Furthermore, the static config includes an IP address for |
| 479 | // us to use. |
| 480 | // |
| 481 | // The current configuration may include some DHCP |
| 482 | // parameters, overriden by any static parameters |
| 483 | // provided. We continue to use this configuration, because |
| 484 | // the only configuration element that is leased to us (IP |
| 485 | // address) will be overriden by a static parameter. |
| 486 | return; |
| 487 | } |
| 488 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 489 | SetServiceState(Service::kStateDisconnected); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 490 | DestroyConnection(); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 491 | } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 494 | void Device::CreateConnection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 495 | SLOG(Device, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 496 | if (!connection_.get()) { |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 497 | connection_ = new Connection( |
| 498 | interface_index_, |
| 499 | link_name_, |
| 500 | technology_, |
| 501 | manager_->device_info(), |
| 502 | manager_->IsTechnologyShortDNSTimeoutEnabled(technology_)); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
| 506 | void Device::DestroyConnection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 507 | SLOG(Device, 2) << __func__; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 508 | StopPortalDetection(); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 509 | StopLinkMonitor(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 510 | if (selected_service_.get()) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 511 | selected_service_->SetConnection(NULL); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 512 | } |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 513 | connection_ = NULL; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 516 | void Device::SelectService(const ServiceRefPtr &service) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 517 | SLOG(Device, 2) << __func__ << ": " |
| 518 | << (service.get() ? |
| 519 | StringPrintf("%s (%s)", |
| 520 | service->UniqueName().c_str(), |
| 521 | service->friendly_name().c_str()) : |
| 522 | "*reset*"); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 523 | |
| 524 | if (selected_service_.get() == service.get()) { |
| 525 | // No change to |selected_service_|. Return early to avoid |
| 526 | // changing its state. |
| 527 | return; |
| 528 | } |
| 529 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 530 | if (selected_service_.get()) { |
| 531 | if (selected_service_->state() != Service::kStateFailure) { |
| 532 | selected_service_->SetState(Service::kStateIdle); |
| 533 | } |
Paul Stewart | 20b0a09 | 2012-05-22 20:39:57 -0700 | [diff] [blame] | 534 | // Just in case the Device subclass has not already done so, make |
| 535 | // sure the previously selected service has its connection removed. |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 536 | selected_service_->SetConnection(NULL); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 537 | } |
| 538 | selected_service_ = service; |
| 539 | } |
| 540 | |
| 541 | void Device::SetServiceState(Service::ConnectState state) { |
| 542 | if (selected_service_.get()) { |
| 543 | selected_service_->SetState(state); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | void Device::SetServiceFailure(Service::ConnectFailure failure_state) { |
| 548 | if (selected_service_.get()) { |
| 549 | selected_service_->SetFailure(failure_state); |
| 550 | } |
| 551 | } |
| 552 | |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 553 | void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) { |
| 554 | if (selected_service_.get()) { |
| 555 | selected_service_->SetFailureSilent(failure_state); |
| 556 | } |
| 557 | } |
| 558 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 559 | string Device::SerializeIPConfigs(const string &suffix) { |
| 560 | return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 563 | bool Device::SetIPFlag(IPAddress::Family family, const string &flag, |
| 564 | const string &value) { |
| 565 | string ip_version; |
| 566 | if (family == IPAddress::kFamilyIPv4) { |
| 567 | ip_version = kIPFlagVersion4; |
| 568 | } else if (family == IPAddress::kFamilyIPv6) { |
| 569 | ip_version = kIPFlagVersion6; |
| 570 | } else { |
| 571 | NOTIMPLEMENTED(); |
| 572 | } |
| 573 | FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(), |
| 574 | link_name_.c_str(), flag.c_str())); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 575 | SLOG(Device, 2) << "Writing " << value << " to flag file " |
| 576 | << flag_file.value(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 577 | if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) { |
| 578 | LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s", |
| 579 | value.c_str(), flag_file.value().c_str()); |
| 580 | return false; |
| 581 | } |
| 582 | return true; |
| 583 | } |
| 584 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 585 | void Device::ResetByteCounters() { |
| 586 | manager_->device_info()->GetByteCounts( |
| 587 | interface_index_, &receive_byte_offset_, &transmit_byte_offset_); |
| 588 | manager_->UpdateDevice(this); |
| 589 | } |
| 590 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 591 | bool Device::RestartPortalDetection() { |
| 592 | StopPortalDetection(); |
| 593 | return StartPortalDetection(); |
| 594 | } |
| 595 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 596 | bool Device::RequestPortalDetection() { |
| 597 | if (!selected_service_) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 598 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 599 | << ": No selected service, so no need for portal check."; |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | if (!connection_.get()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 604 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 605 | << ": No connection, so no need for portal check."; |
| 606 | return false; |
| 607 | } |
| 608 | |
| 609 | if (selected_service_->state() != Service::kStatePortal) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 610 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 611 | << ": Service is not in portal state. No need to start check."; |
| 612 | return false; |
| 613 | } |
| 614 | |
| 615 | if (!connection_->is_default()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 616 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 617 | << ": Service is not the default connection. Don't start check."; |
| 618 | return false; |
| 619 | } |
| 620 | |
| 621 | if (portal_detector_.get() && portal_detector_->IsInProgress()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 622 | SLOG(Device, 2) << FriendlyName() |
| 623 | << ": Portal detection is already running."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 624 | return true; |
| 625 | } |
| 626 | |
| 627 | return StartPortalDetection(); |
| 628 | } |
| 629 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 630 | bool Device::StartPortalDetection() { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 631 | DCHECK(selected_service_.get()); |
| 632 | if (selected_service_->IsPortalDetectionDisabled()) { |
| 633 | SLOG(Device, 2) << "Service " << selected_service_->friendly_name() |
| 634 | << ": Portal detection is disabled; " |
| 635 | << "marking service online."; |
| 636 | SetServiceConnectedState(Service::kStateOnline); |
| 637 | return false; |
| 638 | } |
| 639 | |
| 640 | if (selected_service_->IsPortalDetectionAuto() && |
| 641 | !manager_->IsPortalDetectionEnabled(technology())) { |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 642 | // If portal detection is disabled for this technology, immediately set |
| 643 | // the service state to "Online". |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 644 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 645 | << ": Portal detection is disabled; " |
| 646 | << "marking service online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 647 | SetServiceConnectedState(Service::kStateOnline); |
| 648 | return false; |
| 649 | } |
| 650 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 651 | if (selected_service_->HasProxyConfig()) { |
| 652 | // Services with HTTP proxy configurations should not be checked by the |
| 653 | // connection manager, since we don't have the ability to evaluate |
| 654 | // arbitrary proxy configs and their possible credentials. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 655 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 656 | << ": Service has proxy config; marking it online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 657 | SetServiceConnectedState(Service::kStateOnline); |
| 658 | return false; |
| 659 | } |
| 660 | |
| 661 | portal_detector_.reset(new PortalDetector(connection_, |
| 662 | dispatcher_, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 663 | portal_detector_callback_)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 664 | if (!portal_detector_->Start(manager_->GetPortalCheckURL())) { |
| 665 | LOG(ERROR) << "Device " << FriendlyName() |
| 666 | << ": Portal detection failed to start: likely bad URL: " |
| 667 | << manager_->GetPortalCheckURL(); |
| 668 | SetServiceConnectedState(Service::kStateOnline); |
| 669 | return false; |
| 670 | } |
| 671 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 672 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 673 | << ": Portal detection has started."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 674 | return true; |
| 675 | } |
| 676 | |
| 677 | void Device::StopPortalDetection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 678 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 679 | << ": Portal detection has stopped."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 680 | portal_detector_.reset(); |
| 681 | } |
| 682 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 683 | void Device::set_link_monitor(LinkMonitor *link_monitor) { |
| 684 | link_monitor_.reset(link_monitor); |
| 685 | } |
| 686 | |
| 687 | bool Device::StartLinkMonitor() { |
| 688 | if (!manager_->IsTechnologyLinkMonitorEnabled(technology())) { |
| 689 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 690 | << ": Link Monitoring is disabled."; |
| 691 | return false; |
| 692 | } |
| 693 | |
| 694 | if (!link_monitor()) { |
| 695 | set_link_monitor( |
| 696 | new LinkMonitor( |
| 697 | connection_, dispatcher_, metrics(), manager_->device_info(), |
| 698 | Bind(&Device::OnLinkMonitorFailure, weak_ptr_factory_.GetWeakPtr()))); |
| 699 | } |
| 700 | |
| 701 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 702 | << ": Link Monitor starting."; |
| 703 | return link_monitor_->Start(); |
| 704 | } |
| 705 | |
| 706 | void Device::StopLinkMonitor() { |
| 707 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 708 | << ": Link Monitor stopping."; |
| 709 | link_monitor_.reset(); |
| 710 | } |
| 711 | |
| 712 | void Device::OnLinkMonitorFailure() { |
| 713 | LOG(ERROR) << "Device " << FriendlyName() |
| 714 | << ": Link Monitor indicates failure."; |
| 715 | } |
| 716 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 717 | void Device::SetServiceConnectedState(Service::ConnectState state) { |
| 718 | DCHECK(selected_service_.get()); |
| 719 | |
| 720 | if (!selected_service_.get()) { |
| 721 | LOG(ERROR) << FriendlyName() << ": " |
| 722 | << "Portal detection completed but no selected service exists!"; |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | if (!selected_service_->IsConnected()) { |
| 727 | LOG(ERROR) << FriendlyName() << ": " |
| 728 | << "Portal detection completed but selected service " |
| 729 | << selected_service_->UniqueName() |
| 730 | << " is in non-connected state."; |
| 731 | return; |
| 732 | } |
| 733 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 734 | if (state == Service::kStatePortal && connection_->is_default() && |
| 735 | manager_->GetPortalCheckInterval() != 0) { |
| 736 | CHECK(portal_detector_.get()); |
| 737 | if (!portal_detector_->StartAfterDelay( |
| 738 | manager_->GetPortalCheckURL(), |
| 739 | manager_->GetPortalCheckInterval())) { |
| 740 | LOG(ERROR) << "Device " << FriendlyName() |
| 741 | << ": Portal detection failed to restart: likely bad URL: " |
| 742 | << manager_->GetPortalCheckURL(); |
| 743 | SetServiceState(Service::kStateOnline); |
| 744 | portal_detector_.reset(); |
| 745 | return; |
| 746 | } |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 747 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 748 | << ": Portal detection retrying."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 749 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 750 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 751 | << ": Portal will not retry."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 752 | portal_detector_.reset(); |
| 753 | } |
| 754 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 755 | SetServiceState(state); |
| 756 | } |
| 757 | |
| 758 | void Device::PortalDetectorCallback(const PortalDetector::Result &result) { |
| 759 | if (!result.final) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 760 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 761 | << ": Received non-final status: " |
| 762 | << PortalDetector::StatusToString(result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 763 | return; |
| 764 | } |
| 765 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 766 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 767 | << ": Received final status: " |
| 768 | << PortalDetector::StatusToString(result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 769 | |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 770 | portal_attempts_to_online_ += result.num_attempts; |
| 771 | |
| 772 | metrics()->SendEnumToUMA( |
| 773 | metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()), |
| 774 | Metrics::PortalDetectionResultToEnum(result), |
| 775 | Metrics::kPortalResultMax); |
| 776 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 777 | if (result.status == PortalDetector::kStatusSuccess) { |
| 778 | SetServiceConnectedState(Service::kStateOnline); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 779 | |
| 780 | metrics()->SendToUMA( |
| 781 | metrics()->GetFullMetricName( |
| 782 | Metrics::kMetricPortalAttemptsToOnline, technology()), |
| 783 | portal_attempts_to_online_, |
| 784 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 785 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 786 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 787 | } else { |
| 788 | SetServiceConnectedState(Service::kStatePortal); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 789 | |
| 790 | metrics()->SendToUMA( |
| 791 | metrics()->GetFullMetricName( |
| 792 | Metrics::kMetricPortalAttempts, technology()), |
| 793 | result.num_attempts, |
| 794 | Metrics::kMetricPortalAttemptsMin, |
| 795 | Metrics::kMetricPortalAttemptsMax, |
| 796 | Metrics::kMetricPortalAttemptsNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 800 | vector<string> Device::AvailableIPConfigs(Error */*error*/) { |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 801 | if (ipconfig_.get()) { |
| 802 | string id = ipconfig_->GetRpcIdentifier(); |
| 803 | return vector<string>(1, id); |
| 804 | } |
| 805 | return vector<string>(); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | string Device::GetRpcConnectionIdentifier() { |
| 809 | return adaptor_->GetRpcConnectionIdentifier(); |
| 810 | } |
| 811 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 812 | uint64 Device::GetLinkMonitorResponseTime(Error *error) { |
| 813 | if (!link_monitor_.get()) { |
| 814 | // It is not strictly an error that the link monitor does not |
| 815 | // exist, but returning an error here allows the GetProperties |
| 816 | // call in our Adaptor to omit this parameter. |
| 817 | error->Populate(Error::kNotFound, "Device is not running LinkMonitor"); |
| 818 | return 0; |
| 819 | } |
| 820 | return link_monitor_->GetResponseTimeMilliseconds(); |
| 821 | } |
| 822 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 823 | uint64 Device::GetReceiveByteCount(Error */*error*/) { |
| 824 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 825 | manager_->device_info()->GetByteCounts( |
| 826 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 827 | return rx_byte_count - receive_byte_offset_; |
| 828 | } |
| 829 | |
| 830 | uint64 Device::GetTransmitByteCount(Error */*error*/) { |
| 831 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 832 | manager_->device_info()->GetByteCounts( |
| 833 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 834 | return tx_byte_count - transmit_byte_offset_; |
| 835 | } |
| 836 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 837 | bool Device::IsUnderlyingDeviceEnabled() const { |
| 838 | return false; |
| 839 | } |
| 840 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 841 | // callback |
| 842 | void Device::OnEnabledStateChanged(const ResultCallback &callback, |
| 843 | const Error &error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 844 | SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")"; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 845 | if (error.IsSuccess()) { |
| 846 | enabled_ = enabled_pending_; |
| 847 | manager_->UpdateEnabledTechnologies(); |
| 848 | adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_); |
| 849 | adaptor_->UpdateEnabled(); |
| 850 | } |
Gary Morain | baeefdf | 2012-04-30 14:53:35 -0700 | [diff] [blame] | 851 | enabled_pending_ = enabled_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 852 | if (!callback.is_null()) |
| 853 | callback.Run(error); |
| 854 | } |
| 855 | |
| 856 | void Device::SetEnabled(bool enable) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 857 | SLOG(Device, 2) << __func__ << "(" << enable << ")"; |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 858 | Error error; |
| 859 | SetEnabledInternal(enable, false, &error, ResultCallback()); |
Jason Glasgow | 7234ec3 | 2012-05-23 16:01:21 -0400 | [diff] [blame] | 860 | LOG_IF(ERROR, error.IsFailure() && !error.IsOngoing()) |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 861 | << "Enabled failed, but no way to report the failure."; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | void Device::SetEnabledPersistent(bool enable, |
| 865 | Error *error, |
| 866 | const ResultCallback &callback) { |
| 867 | SetEnabledInternal(enable, true, error, callback); |
| 868 | } |
| 869 | |
| 870 | void Device::SetEnabledInternal(bool enable, |
| 871 | bool persist, |
| 872 | Error *error, |
| 873 | const ResultCallback &callback) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 874 | DCHECK(error); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 875 | SLOG(Device, 2) << "Device " << link_name_ << " " |
| 876 | << (enable ? "starting" : "stopping"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 877 | if (enable == enabled_) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 878 | error->Reset(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 879 | return; |
| 880 | } |
| 881 | |
| 882 | if (enabled_pending_ == enable) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 883 | Error::PopulateAndLog(error, Error::kInProgress, |
| 884 | "Enable operation already in progress"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 885 | return; |
| 886 | } |
| 887 | |
| 888 | if (persist) { |
| 889 | enabled_persistent_ = enable; |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 890 | manager_->UpdateDevice(this); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | enabled_pending_ = enable; |
| 894 | EnabledStateChangedCallback enabled_callback = |
| 895 | Bind(&Device::OnEnabledStateChanged, |
| 896 | weak_ptr_factory_.GetWeakPtr(), callback); |
| 897 | if (enable) { |
| 898 | running_ = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 899 | Start(error, enabled_callback); |
| 900 | } else { |
| 901 | running_ = false; |
| 902 | DestroyIPConfig(); // breaks a reference cycle |
| 903 | SelectService(NULL); // breaks a reference cycle |
| 904 | rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 905 | SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ " |
| 906 | << (ipconfig_ ? "is set." : "is not set."); |
| 907 | SLOG(Device, 3) << "Device " << link_name_ << " connection_ " |
| 908 | << (connection_ ? "is set." : "is not set."); |
| 909 | SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ " |
| 910 | << (selected_service_ ? "is set." : "is not set."); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 911 | Stop(error, enabled_callback); |
| 912 | } |
| 913 | } |
| 914 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 915 | } // namespace shill |