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