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