blob: 23945155934a9ee0db46739e918c1a1af2d51385 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75897df2011-04-27 09:05:53 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Chris Masoneb2e326b2011-07-12 13:28:51 -07005#include "shill/service.h"
6
Paul Stewart75897df2011-04-27 09:05:53 -07007#include <time.h>
Paul Stewart75897df2011-04-27 09:05:53 -07008#include <stdio.h>
Chris Masoneee929b72011-05-10 10:02:18 -07009
Chris Masone8fe2c7e2011-06-09 15:51:19 -070010#include <map>
Paul Stewart75897df2011-04-27 09:05:53 -070011#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070012#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070013
Chris Masone3bd3c8c2011-06-13 08:20:26 -070014#include <base/memory/scoped_ptr.h>
mukesh agrawal51a7e932011-07-27 16:18:26 -070015#include <base/string_number_conversions.h>
Paul Stewart2bf424f2012-04-11 18:59:39 -070016#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070017#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070018
Paul Stewartbe5f5b32011-12-07 17:11:11 -080019#include "shill/connection.h"
Paul Stewart75897df2011-04-27 09:05:53 -070020#include "shill/control_interface.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070021#include "shill/error.h"
Paul Stewartbe5f5b32011-12-07 17:11:11 -080022#include "shill/http_proxy.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070023#include "shill/logging.h"
Chris Masone6791a432011-07-12 13:23:19 -070024#include "shill/manager.h"
Thieu Le48e6d6d2011-12-06 00:40:27 +000025#include "shill/metrics.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070026#include "shill/profile.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070027#include "shill/property_accessor.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070028#include "shill/refptr_types.h"
Chris Masoned7732e42011-05-20 11:08:56 -070029#include "shill/service_dbus_adaptor.h"
Darin Petkov5eb05422012-05-11 15:45:25 +020030#include "shill/sockets.h"
Darin Petkovba40dd32011-07-11 20:06:39 -070031#include "shill/store_interface.h"
Paul Stewart75897df2011-04-27 09:05:53 -070032
Christopher Wiley0801d192012-09-24 11:57:15 -070033using base::Bind;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070034using std::map;
Paul Stewart75897df2011-04-27 09:05:53 -070035using std::string;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070036using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070037
38namespace shill {
Darin Petkovba40dd32011-07-11 20:06:39 -070039
mukesh agrawalbf14e942012-03-02 14:36:34 -080040const char Service::kAutoConnConnected[] = "connected";
41const char Service::kAutoConnConnecting[] = "connecting";
42const char Service::kAutoConnExplicitDisconnect[] = "explicitly disconnected";
43const char Service::kAutoConnNotConnectable[] = "not connectable";
Christopher Wiley0801d192012-09-24 11:57:15 -070044const char Service::kAutoConnThrottled[] = "throttled";
mukesh agrawalbf14e942012-03-02 14:36:34 -080045
Paul Stewartbc6e7392012-05-24 07:07:48 -070046const size_t Service::kEAPMaxCertificationElements = 10;
47
Darin Petkovba40dd32011-07-11 20:06:39 -070048const char Service::kCheckPortalAuto[] = "auto";
49const char Service::kCheckPortalFalse[] = "false";
50const char Service::kCheckPortalTrue[] = "true";
51
Paul Stewartac4ac002011-08-26 12:04:26 -070052const int Service::kPriorityNone = 0;
53
Darin Petkov2f903b32012-04-18 12:56:43 +020054const char Service::kServiceSortAutoConnect[] = "AutoConnect";
55const char Service::kServiceSortConnectable[] = "Connectable";
56const char Service::kServiceSortFavorite[] = "Favorite";
mukesh agrawalddc378f2012-02-17 18:26:20 -080057const char Service::kServiceSortIsConnected[] = "IsConnected";
58const char Service::kServiceSortIsConnecting[] = "IsConnecting";
59const char Service::kServiceSortIsFailed[] = "IsFailed";
Paul Stewarta121c442012-06-09 14:12:58 -070060const char Service::kServiceSortIsPortalled[] = "IsPortal";
Darin Petkov2f903b32012-04-18 12:56:43 +020061const char Service::kServiceSortPriority[] = "Priority";
mukesh agrawalddc378f2012-02-17 18:26:20 -080062const char Service::kServiceSortSecurityEtc[] = "SecurityEtc";
Darin Petkov2f903b32012-04-18 12:56:43 +020063const char Service::kServiceSortTechnology[] = "Technology";
mukesh agrawalddc378f2012-02-17 18:26:20 -080064const char Service::kServiceSortUniqueName[] = "UniqueName";
65
Darin Petkovba40dd32011-07-11 20:06:39 -070066const char Service::kStorageAutoConnect[] = "AutoConnect";
67const char Service::kStorageCheckPortal[] = "CheckPortal";
68const char Service::kStorageEapAnonymousIdentity[] = "EAP.AnonymousIdentity";
69const char Service::kStorageEapCACert[] = "EAP.CACert";
70const char Service::kStorageEapCACertID[] = "EAP.CACertID";
Paul Stewartecf4cd12012-04-17 11:08:39 -070071const char Service::kStorageEapCACertNSS[] = "EAP.CACertNSS";
Darin Petkovba40dd32011-07-11 20:06:39 -070072const char Service::kStorageEapCertID[] = "EAP.CertID";
73const char Service::kStorageEapClientCert[] = "EAP.ClientCert";
74const char Service::kStorageEapEap[] = "EAP.EAP";
75const char Service::kStorageEapIdentity[] = "EAP.Identity";
76const char Service::kStorageEapInnerEap[] = "EAP.InnerEAP";
77const char Service::kStorageEapKeyID[] = "EAP.KeyID";
78const char Service::kStorageEapKeyManagement[] = "EAP.KeyMgmt";
79const char Service::kStorageEapPIN[] = "EAP.PIN";
80const char Service::kStorageEapPassword[] = "EAP.Password";
81const char Service::kStorageEapPrivateKey[] = "EAP.PrivateKey";
82const char Service::kStorageEapPrivateKeyPassword[] = "EAP.PrivateKeyPassword";
83const char Service::kStorageEapUseSystemCAs[] = "EAP.UseSystemCAs";
Paul Stewart0756db92012-01-27 08:34:47 -080084const char Service::kStorageError[] = "Error";
Darin Petkovba40dd32011-07-11 20:06:39 -070085const char Service::kStorageFavorite[] = "Favorite";
Paul Stewart0756db92012-01-27 08:34:47 -080086const char Service::kStorageGUID[] = "GUID";
mukesh agrawalcf24a242012-05-21 16:46:11 -070087const char Service::kStorageHasEverConnected[] = "HasEverConnected";
Darin Petkovba40dd32011-07-11 20:06:39 -070088const char Service::kStorageName[] = "Name";
89const char Service::kStoragePriority[] = "Priority";
90const char Service::kStorageProxyConfig[] = "ProxyConfig";
91const char Service::kStorageSaveCredentials[] = "SaveCredentials";
Paul Stewart2706aaf2011-12-14 16:44:04 -080092const char Service::kStorageType[] = "Type";
Paul Stewart987e71e2011-12-05 09:45:06 -080093const char Service::kStorageUIData[] = "UIData";
Darin Petkovba40dd32011-07-11 20:06:39 -070094
mukesh agrawal8f3f7752012-02-17 19:42:09 -080095const uint8 Service::kStrengthMax = 100;
96const uint8 Service::kStrengthMin = 0;
97
Christopher Wiley0801d192012-09-24 11:57:15 -070098const uint64 Service::kMaxAutoConnectCooldownTimeMilliseconds = 30 * 60 * 1000;
99const uint64 Service::kMinAutoConnectCooldownTimeMilliseconds = 1000;
100const uint64 Service::kAutoConnectCooldownBackoffFactor = 2;
101
mukesh agrawal51a7e932011-07-27 16:18:26 -0700102// static
103unsigned int Service::serial_number_ = 0;
104
Paul Stewart75897df2011-04-27 09:05:53 -0700105Service::Service(ControlInterface *control_interface,
mukesh agrawalb54601c2011-06-07 17:39:22 -0700106 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -0800107 Metrics *metrics,
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700108 Manager *manager,
Gaurav Shah435de2c2011-11-17 19:01:07 -0800109 Technology::Identifier technology)
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400110 : state_(kStateIdle),
Paul Stewart03dba0b2011-08-22 16:32:45 -0700111 failure_(kFailureUnknown),
112 auto_connect_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700113 check_portal_(kCheckPortalAuto),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700114 connectable_(false),
Paul Stewartf2d60912012-07-15 08:37:30 -0700115 error_(ConnectFailureToString(failure_)),
mukesh agrawaladb68482012-01-17 16:31:51 -0800116 explicitly_disconnected_(false),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700117 favorite_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700118 priority_(kPriorityNone),
Paul Stewart1ca3e852011-11-04 07:50:49 -0700119 security_level_(0),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700120 strength_(0),
Darin Petkovba40dd32011-07-11 20:06:39 -0700121 save_credentials_(true),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800122 technology_(technology),
mukesh agrawal568b5c62012-02-28 14:44:47 -0800123 failed_time_(0),
mukesh agrawalcf24a242012-05-21 16:46:11 -0700124 has_ever_connected_(false),
Christopher Wiley0801d192012-09-24 11:57:15 -0700125 auto_connect_cooldown_milliseconds_(0),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700126 dispatcher_(dispatcher),
mukesh agrawald835b202011-10-07 15:26:47 -0700127 unique_name_(base::UintToString(serial_number_++)),
128 friendly_name_(unique_name_),
Chris Masone6791a432011-07-12 13:23:19 -0700129 adaptor_(control_interface->CreateServiceAdaptor(this)),
Thieu Le3426c8f2012-01-11 17:35:11 -0800130 metrics_(metrics),
Darin Petkov5eb05422012-05-11 15:45:25 +0200131 manager_(manager),
Christopher Wiley0801d192012-09-24 11:57:15 -0700132 sockets_(new Sockets()),
133 weak_ptr_factory_(this) {
Thieu Le284fe792012-01-31 17:53:19 -0800134 HelpRegisterDerivedBool(flimflam::kAutoConnectProperty,
135 &Service::GetAutoConnect,
136 &Service::SetAutoConnect);
Chris Masone4d42df82011-07-02 17:09:39 -0700137
138 // flimflam::kActivationStateProperty: Registered in CellularService
139 // flimflam::kCellularApnProperty: Registered in CellularService
140 // flimflam::kCellularLastGoodApnProperty: Registered in CellularService
141 // flimflam::kNetworkTechnologyProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700142 // flimflam::kOperatorNameProperty: DEPRECATED
143 // flimflam::kOperatorCodeProperty: DEPRECATED
Chris Masone4d42df82011-07-02 17:09:39 -0700144 // flimflam::kRoamingStateProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700145 // flimflam::kServingOperatorProperty: Registered in CellularService
Chris Masone4d42df82011-07-02 17:09:39 -0700146 // flimflam::kPaymentURLProperty: Registered in CellularService
147
Paul Stewartd215af62012-04-24 23:25:50 -0700148 HelpRegisterDerivedString(flimflam::kCheckPortalProperty,
149 &Service::GetCheckPortal,
150 &Service::SetCheckPortal);
Chris Masone27c4aa52011-07-02 13:10:14 -0700151 store_.RegisterConstBool(flimflam::kConnectableProperty, &connectable_);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400152 HelpRegisterDerivedRpcIdentifier(flimflam::kDeviceProperty,
153 &Service::GetDeviceRpcId,
154 NULL);
Paul Stewart4c561612012-03-21 12:49:01 -0700155 store_.RegisterString(flimflam::kGuidProperty, &guid_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700156
Chris Masoneb2e326b2011-07-12 13:28:51 -0700157 store_.RegisterString(flimflam::kEapIdentityProperty, &eap_.identity);
158 store_.RegisterString(flimflam::kEAPEAPProperty, &eap_.eap);
159 store_.RegisterString(flimflam::kEapPhase2AuthProperty, &eap_.inner_eap);
Chris Masone27c4aa52011-07-02 13:10:14 -0700160 store_.RegisterString(flimflam::kEapAnonymousIdentityProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700161 &eap_.anonymous_identity);
162 store_.RegisterString(flimflam::kEAPClientCertProperty, &eap_.client_cert);
163 store_.RegisterString(flimflam::kEAPCertIDProperty, &eap_.cert_id);
164 store_.RegisterString(flimflam::kEapPrivateKeyProperty, &eap_.private_key);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800165 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPrivateKeyPasswordProperty,
166 &Service::SetEAPPrivateKeyPassword,
167 NULL,
168 &eap_.private_key_password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700169 store_.RegisterString(flimflam::kEAPKeyIDProperty, &eap_.key_id);
170 store_.RegisterString(flimflam::kEapCaCertProperty, &eap_.ca_cert);
171 store_.RegisterString(flimflam::kEapCaCertIDProperty, &eap_.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700172 store_.RegisterString(flimflam::kEapCaCertNssProperty, &eap_.ca_cert_nss);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700173 store_.RegisterString(flimflam::kEAPPINProperty, &eap_.pin);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800174 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPasswordProperty,
175 &Service::SetEAPPassword,
176 NULL,
177 &eap_.password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700178 store_.RegisterString(flimflam::kEapKeyMgmtProperty, &eap_.key_management);
179 store_.RegisterBool(flimflam::kEapUseSystemCAsProperty, &eap_.use_system_cas);
Paul Stewartbc6e7392012-05-24 07:07:48 -0700180 store_.RegisterConstStrings(shill::kEapRemoteCertificationProperty,
181 &eap_.remote_certification);
182 store_.RegisterString(shill::kEapSubjectMatchProperty,
183 &eap_.subject_match);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700184
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400185 // TODO(ers): in flimflam clearing Error has the side-effect of
186 // setting the service state to IDLE. Is this important? I could
187 // see an autotest depending on it.
Chris Masone27c4aa52011-07-02 13:10:14 -0700188 store_.RegisterConstString(flimflam::kErrorProperty, &error_);
189 store_.RegisterConstBool(flimflam::kFavoriteProperty, &favorite_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800190 HelpRegisterDerivedUint16(shill::kHTTPProxyPortProperty,
191 &Service::GetHTTPProxyPort,
192 NULL);
Paul Stewart10241e32012-04-23 18:15:06 -0700193 HelpRegisterDerivedRpcIdentifier(shill::kIPConfigProperty,
194 &Service::GetIPConfigRpcIdentifier,
195 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -0700196 HelpRegisterDerivedBool(flimflam::kIsActiveProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700197 &Service::IsActive,
198 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700199 // flimflam::kModeProperty: Registered in WiFiService
Paul Stewart0c438332012-04-11 07:55:27 -0700200
201 // Although this is a read-only property, some callers want to blindly
202 // set this value to its current value.
203 HelpRegisterDerivedString(flimflam::kNameProperty,
204 &Service::GetNameProperty,
205 &Service::AssertTrivialSetNameProperty);
Chris Masone4d42df82011-07-02 17:09:39 -0700206 // flimflam::kPassphraseProperty: Registered in WiFiService
207 // flimflam::kPassphraseRequiredProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700208 store_.RegisterInt32(flimflam::kPriorityProperty, &priority_);
209 HelpRegisterDerivedString(flimflam::kProfileProperty,
210 &Service::GetProfileRpcId,
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800211 &Service::SetProfileRpcId);
Chris Masone4d42df82011-07-02 17:09:39 -0700212 store_.RegisterString(flimflam::kProxyConfigProperty, &proxy_config_);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700213 store_.RegisterBool(flimflam::kSaveCredentialsProperty, &save_credentials_);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800214 HelpRegisterDerivedString(flimflam::kTypeProperty,
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200215 &Service::CalculateTechnology,
Gaurav Shah435de2c2011-11-17 19:01:07 -0800216 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700217 // flimflam::kSecurityProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700218 HelpRegisterDerivedString(flimflam::kStateProperty,
219 &Service::CalculateState,
220 NULL);
Darin Petkovd78ee7e2012-01-12 11:21:10 +0100221 store_.RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800222 store_.RegisterString(flimflam::kUIDataProperty, &ui_data_);
Chris Masone4d42df82011-07-02 17:09:39 -0700223 // flimflam::kWifiAuthMode: Registered in WiFiService
224 // flimflam::kWifiHiddenSsid: Registered in WiFiService
225 // flimflam::kWifiFrequency: Registered in WiFiService
226 // flimflam::kWifiPhyMode: Registered in WiFiService
227 // flimflam::kWifiHexSsid: Registered in WiFiService
Thieu Le48e6d6d2011-12-06 00:40:27 +0000228
229 metrics_->RegisterService(this);
230
Paul Stewart1062d9d2012-04-27 10:42:27 -0700231 static_ip_parameters_.PlumbPropertyStore(&store_);
232
Paul Stewartcb59fed2012-03-21 21:14:46 -0700233 IgnoreParameterForConfigure(flimflam::kTypeProperty);
Paul Stewart7f61e522012-03-22 11:13:45 -0700234 IgnoreParameterForConfigure(flimflam::kProfileProperty);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700235
Ben Chanfad4a0b2012-04-18 15:49:59 -0700236 SLOG(Service, 2) << "Service initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -0700237}
238
Thieu Le48e6d6d2011-12-06 00:40:27 +0000239Service::~Service() {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200240 LOG(INFO) << "Service destroyed: " << friendly_name_;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000241 metrics_->DeregisterService(this);
242}
Paul Stewart75897df2011-04-27 09:05:53 -0700243
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000244void Service::AutoConnect() {
Darin Petkov3abc3be2012-06-27 10:48:23 +0200245 const char *reason = NULL;
246 if (IsAutoConnectable(&reason)) {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200247 LOG(INFO) << "Auto-connecting to " << friendly_name_;
Christopher Wiley0801d192012-09-24 11:57:15 -0700248 ThrottleFutureAutoConnects();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000249 Error error;
250 Connect(&error);
mukesh agrawal76d13882012-01-12 15:23:11 -0800251 } else {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200252 LOG(INFO) << "Suppressed autoconnect to " << friendly_name_ << " "
mukesh agrawalbf14e942012-03-02 14:36:34 -0800253 << "(" << reason << ")";
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000254 }
255}
256
mukesh agrawaladb68482012-01-17 16:31:51 -0800257void Service::Connect(Error */*error*/) {
258 explicitly_disconnected_ = false;
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400259 // clear any failure state from a previous connect attempt
260 SetState(kStateIdle);
mukesh agrawaladb68482012-01-17 16:31:51 -0800261}
262
263void Service::Disconnect(Error */*error*/) {
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700264 MemoryLog::GetInstance()->FlushToDisk();
mukesh agrawaladb68482012-01-17 16:31:51 -0800265}
266
Christopher Wileyabd3b502012-09-26 13:08:52 -0700267void Service::DisconnectWithFailure(ConnectFailure failure, Error *error) {
268 Disconnect(error);
269 SetFailure(failure);
270}
271
272void Service::UserInitiatedDisconnect(Error *error) {
273 Disconnect(error);
274 explicitly_disconnected_ = true;
275}
276
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500277void Service::ActivateCellularModem(const string &/*carrier*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500278 Error *error,
279 const ResultCallback &/*callback*/) {
280 Error::PopulateAndLog(error, Error::kNotSupported,
281 "Service doesn't support cellular modem activation.");
Darin Petkovc408e692011-08-17 13:47:15 -0700282}
283
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800284bool Service::IsActive(Error */*error*/) {
Paul Stewartbfb82552012-10-24 16:48:48 -0700285 return state() != kStateUnknown &&
286 state() != kStateIdle &&
287 state() != kStateFailure;
Paul Stewart03dba0b2011-08-22 16:32:45 -0700288}
289
290void Service::SetState(ConnectState state) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800291 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name_
292 << " state " << ConnectStateToString(state_) << " -> "
293 << ConnectStateToString(state);
mukesh agrawal7ec71312011-11-10 02:08:26 +0000294
Paul Stewart03dba0b2011-08-22 16:32:45 -0700295 if (state == state_) {
296 return;
297 }
298
299 state_ = state;
300 if (state != kStateFailure) {
301 failure_ = kFailureUnknown;
302 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700303 if (state == kStateConnected) {
Christopher Wiley0801d192012-09-24 11:57:15 -0700304 failed_time_ = 0;
mukesh agrawalcf24a242012-05-21 16:46:11 -0700305 has_ever_connected_ = true;
306 SaveToProfile();
Christopher Wiley0801d192012-09-24 11:57:15 -0700307 // When we succeed in connecting, forget that connects failed in the past.
308 // Give services one chance at a fast autoconnect retry by resetting the
309 // cooldown to 0 to indicate that the last connect was successful.
310 auto_connect_cooldown_milliseconds_ = 0;
311 reenable_auto_connect_task_.Cancel();
mukesh agrawalcf24a242012-05-21 16:46:11 -0700312 }
Paul Stewartf2d60912012-07-15 08:37:30 -0700313 UpdateErrorProperty();
Paul Stewart03dba0b2011-08-22 16:32:45 -0700314 manager_->UpdateService(this);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000315 metrics_->NotifyServiceStateChanged(this, state);
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200316 adaptor_->EmitStringChanged(flimflam::kStateProperty, GetStateString());
Paul Stewart03dba0b2011-08-22 16:32:45 -0700317}
318
Christopher Wiley0801d192012-09-24 11:57:15 -0700319void Service::ReEnableAutoConnectTask() {
320 // Kill the thing blocking AutoConnect().
321 reenable_auto_connect_task_.Cancel();
322 // Post to the manager, giving it an opportunity to AutoConnect again.
323 manager_->UpdateService(this);
324}
325
326void Service::ThrottleFutureAutoConnects() {
327 if (auto_connect_cooldown_milliseconds_ > 0) {
328 LOG(INFO) << "Throttling autoconnect to " << friendly_name_ << " for "
329 << auto_connect_cooldown_milliseconds_ << " milliseconds.";
330 reenable_auto_connect_task_.Reset(Bind(&Service::ReEnableAutoConnectTask,
331 weak_ptr_factory_.GetWeakPtr()));
332 dispatcher_->PostDelayedTask(reenable_auto_connect_task_.callback(),
333 auto_connect_cooldown_milliseconds_);
334 }
335 auto_connect_cooldown_milliseconds_ =
336 std::min(kMaxAutoConnectCooldownTimeMilliseconds,
337 std::max(kMinAutoConnectCooldownTimeMilliseconds,
338 auto_connect_cooldown_milliseconds_ *
339 kAutoConnectCooldownBackoffFactor));
340}
341
Paul Stewart03dba0b2011-08-22 16:32:45 -0700342void Service::SetFailure(ConnectFailure failure) {
343 failure_ = failure;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800344 failed_time_ = time(NULL);
Paul Stewartf2d60912012-07-15 08:37:30 -0700345 UpdateErrorProperty();
Paul Stewart03dba0b2011-08-22 16:32:45 -0700346 SetState(kStateFailure);
347}
348
mukesh agrawal568b5c62012-02-28 14:44:47 -0800349void Service::SetFailureSilent(ConnectFailure failure) {
350 // Note that order matters here, since SetState modifies |failure_| and
351 // |failed_time_|.
352 SetState(kStateIdle);
353 failure_ = failure;
Paul Stewartf2d60912012-07-15 08:37:30 -0700354 UpdateErrorProperty();
mukesh agrawal568b5c62012-02-28 14:44:47 -0800355 failed_time_ = time(NULL);
356}
357
Chris Masone6791a432011-07-12 13:23:19 -0700358string Service::GetRpcIdentifier() const {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700359 return adaptor_->GetRpcIdentifier();
360}
361
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700362bool Service::IsLoadableFrom(StoreInterface *storage) const {
363 return storage->ContainsGroup(GetStorageIdentifier());
364}
365
Chris Masone9d779932011-08-25 16:33:41 -0700366bool Service::Load(StoreInterface *storage) {
367 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700368 if (!storage->ContainsGroup(id)) {
369 LOG(WARNING) << "Service is not available in the persistent store: " << id;
370 return false;
371 }
372 storage->GetBool(id, kStorageAutoConnect, &auto_connect_);
373 storage->GetString(id, kStorageCheckPortal, &check_portal_);
374 storage->GetBool(id, kStorageFavorite, &favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700375 storage->GetString(id, kStorageGUID, &guid_);
376 storage->GetBool(id, kStorageHasEverConnected, &has_ever_connected_);
Chris Masone34af2182011-08-22 11:59:36 -0700377 storage->GetInt(id, kStoragePriority, &priority_);
378 storage->GetString(id, kStorageProxyConfig, &proxy_config_);
379 storage->GetBool(id, kStorageSaveCredentials, &save_credentials_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800380 storage->GetString(id, kStorageUIData, &ui_data_);
Chris Masone34af2182011-08-22 11:59:36 -0700381
382 LoadEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700383 static_ip_parameters_.Load(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700384 // TODO(petkov): Load these:
385
Chris Masone34af2182011-08-22 11:59:36 -0700386 // "Failure"
387 // "Modified"
388 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700389
mukesh agrawaladb68482012-01-17 16:31:51 -0800390 explicitly_disconnected_ = false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800391 favorite_ = true;
392
Chris Masone34af2182011-08-22 11:59:36 -0700393 return true;
Darin Petkovba40dd32011-07-11 20:06:39 -0700394}
395
Paul Stewart65512e12012-03-26 18:01:08 -0700396bool Service::Unload() {
Paul Stewart7fb09382012-07-18 17:32:27 -0700397 auto_connect_ = IsAutoConnectByDefault();
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800398 check_portal_ = kCheckPortalAuto;
Christopher Wileyabd3b502012-09-26 13:08:52 -0700399 explicitly_disconnected_ = false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800400 favorite_ = false;
Paul Stewarte7cce8f2012-09-11 10:56:38 -0700401 guid_ = "";
Paul Stewart88769de2012-09-21 13:14:36 -0700402 has_ever_connected_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800403 priority_ = kPriorityNone;
404 proxy_config_ = "";
405 save_credentials_ = true;
406 ui_data_ = "";
407
408 UnloadEapCredentials();
Wade Guthrie9e0c2502012-04-19 15:26:40 -0700409 Error error; // Ignored.
410 Disconnect(&error);
Paul Stewart65512e12012-03-26 18:01:08 -0700411 return false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800412}
413
Chris Masone9d779932011-08-25 16:33:41 -0700414bool Service::Save(StoreInterface *storage) {
415 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700416
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200417 storage->SetString(id, kStorageType, GetTechnologyString());
Paul Stewart2706aaf2011-12-14 16:44:04 -0800418
Chris Masone34af2182011-08-22 11:59:36 -0700419 // TODO(petkov): We could choose to simplify the saving code by removing most
420 // conditionals thus saving even default values.
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700421 storage->SetBool(id, kStorageAutoConnect, auto_connect_);
Chris Masone34af2182011-08-22 11:59:36 -0700422 if (check_portal_ == kCheckPortalAuto) {
423 storage->DeleteKey(id, kStorageCheckPortal);
424 } else {
425 storage->SetString(id, kStorageCheckPortal, check_portal_);
426 }
427 storage->SetBool(id, kStorageFavorite, favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700428 SaveString(storage, id, kStorageGUID, guid_, false, true);
429 storage->SetBool(id, kStorageHasEverConnected, has_ever_connected_);
mukesh agrawald835b202011-10-07 15:26:47 -0700430 storage->SetString(id, kStorageName, friendly_name_);
Chris Masone34af2182011-08-22 11:59:36 -0700431 if (priority_ != kPriorityNone) {
432 storage->SetInt(id, kStoragePriority, priority_);
433 } else {
434 storage->DeleteKey(id, kStoragePriority);
435 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800436 SaveString(storage, id, kStorageProxyConfig, proxy_config_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700437 if (save_credentials_) {
438 storage->DeleteKey(id, kStorageSaveCredentials);
439 } else {
440 storage->SetBool(id, kStorageSaveCredentials, false);
441 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800442 SaveString(storage, id, kStorageUIData, ui_data_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700443
444 SaveEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700445 static_ip_parameters_.Save(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700446
447 // TODO(petkov): Save these:
448
Chris Masone34af2182011-08-22 11:59:36 -0700449 // "Failure"
450 // "Modified"
451 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700452
453 return true;
454}
455
Gary Moraind93615e2012-04-27 11:50:03 -0700456void Service::SaveToCurrentProfile() {
457 // Some unittests do not specify a manager.
458 if (manager()) {
459 manager()->SaveServiceToProfile(this);
460 }
Thieu Led4e9e552012-02-16 16:26:07 -0800461}
462
Paul Stewartcb59fed2012-03-21 21:14:46 -0700463void Service::Configure(const KeyValueStore &args, Error *error) {
464 map<string, bool>::const_iterator bool_it;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700465 SLOG(Service, 5) << "Configuring bool properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700466 for (bool_it = args.bool_properties().begin();
467 bool_it != args.bool_properties().end();
468 ++bool_it) {
469 if (ContainsKey(parameters_ignored_for_configure_, bool_it->first)) {
470 continue;
471 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700472 SLOG(Service, 5) << " " << bool_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700473 Error set_error;
474 store_.SetBoolProperty(bool_it->first, bool_it->second, &set_error);
Christopher Wiley27b47232012-11-02 13:13:00 -0700475 OnPropertyChanged(bool_it->first);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700476 if (error->IsSuccess() && set_error.IsFailure()) {
477 error->CopyFrom(set_error);
478 }
479 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700480 SLOG(Service, 5) << "Configuring string properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700481 map<string, string>::const_iterator string_it;
482 for (string_it = args.string_properties().begin();
483 string_it != args.string_properties().end();
484 ++string_it) {
485 if (ContainsKey(parameters_ignored_for_configure_, string_it->first)) {
486 continue;
487 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700488 SLOG(Service, 5) << " " << string_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700489 Error set_error;
490 store_.SetStringProperty(string_it->first, string_it->second, &set_error);
Christopher Wiley27b47232012-11-02 13:13:00 -0700491 OnPropertyChanged(string_it->first);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700492 if (error->IsSuccess() && set_error.IsFailure()) {
493 error->CopyFrom(set_error);
494 }
495 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700496 SLOG(Service, 5) << "Configuring uint32 properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700497 map<string, uint32>::const_iterator int_it;
498 for (int_it = args.uint_properties().begin();
499 int_it != args.uint_properties().end();
500 ++int_it) {
501 if (ContainsKey(parameters_ignored_for_configure_, int_it->first)) {
502 continue;
503 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700504 SLOG(Service, 5) << " " << int_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700505 Error set_error;
506 store_.SetUint32Property(int_it->first, int_it->second, &set_error);
Christopher Wiley27b47232012-11-02 13:13:00 -0700507 OnPropertyChanged(int_it->first);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700508 if (error->IsSuccess() && set_error.IsFailure()) {
509 error->CopyFrom(set_error);
510 }
511 }
512}
513
Paul Stewart10ccbb32012-04-26 15:59:30 -0700514bool Service::IsRemembered() const {
515 return profile_ && !manager_->IsServiceEphemeral(this);
516}
517
mukesh agrawal00917ce2011-11-22 23:56:55 +0000518void Service::MakeFavorite() {
519 if (favorite_) {
520 // We do not want to clobber the value of auto_connect_ (it may
521 // be user-set). So return early.
522 return;
523 }
524
525 auto_connect_ = true;
526 favorite_ = true;
527}
528
Darin Petkov5eb05422012-05-11 15:45:25 +0200529void Service::SetConnection(const ConnectionRefPtr &connection) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800530 if (connection.get()) {
Paul Stewart1e3bc4962012-09-14 12:20:22 -0700531 // TODO(pstew): Make this function testable by using a factory here.
532 // http://crosbug.com/34528
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800533 http_proxy_.reset(new HTTPProxy(connection));
Darin Petkov5eb05422012-05-11 15:45:25 +0200534 http_proxy_->Start(dispatcher_, sockets_.get());
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800535 } else {
536 http_proxy_.reset();
Paul Stewartdef189e2012-08-02 20:12:09 -0700537 static_ip_parameters_.ClearSavedParameters();
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800538 }
539 connection_ = connection;
Paul Stewart1e3bc4962012-09-14 12:20:22 -0700540 Error error;
541 string ipconfig = GetIPConfigRpcIdentifier(&error);
542 if (error.IsSuccess()) {
543 adaptor_->EmitRpcIdentifierChanged(shill::kIPConfigProperty, ipconfig);
544 }
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800545}
546
Gaurav Shah10109f22011-11-11 20:16:22 -0800547bool Service::Is8021xConnectable() const {
548 // We mirror all the flimflam checks (see service.c:is_connectable()).
549
550 // Identity is required.
551 if (eap_.identity.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700552 SLOG(Service, 2) << "Not connectable: Identity is empty.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800553 return false;
554 }
555
556 if (!eap_.client_cert.empty() || !eap_.cert_id.empty()) {
557 // If a client certificate is being used, we must have a private key.
558 if (eap_.private_key.empty() && eap_.key_id.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700559 SLOG(Service, 2)
560 << "Not connectable. Client certificate but no private key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800561 return false;
562 }
563 }
564 if (!eap_.cert_id.empty() || !eap_.key_id.empty() ||
565 !eap_.ca_cert_id.empty()) {
566 // If PKCS#11 data is needed, a PIN is required.
567 if (eap_.pin.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700568 SLOG(Service, 2) << "Not connectable. PKCS#11 data but no PIN.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800569 return false;
570 }
571 }
572
573 // For EAP-TLS, a client certificate is required.
574 if (eap_.eap.empty() || eap_.eap == "TLS") {
Paul Stewart81426132012-05-16 10:05:10 -0700575 if ((!eap_.client_cert.empty() || !eap_.cert_id.empty()) &&
576 (!eap_.private_key.empty() || !eap_.key_id.empty())) {
577 SLOG(Service, 2) << "Connectable. EAP-TLS with a client cert and key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800578 return true;
579 }
580 }
581
582 // For EAP types other than TLS (e.g. EAP-TTLS or EAP-PEAP, password is the
583 // minimum requirement), at least an identity + password is required.
584 if (eap_.eap.empty() || eap_.eap != "TLS") {
585 if (!eap_.password.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700586 SLOG(Service, 2) << "Connectable. !EAP-TLS and has a password.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800587 return true;
588 }
589 }
590
Ben Chanfad4a0b2012-04-18 15:49:59 -0700591 SLOG(Service, 2)
592 << "Not connectable. No suitable EAP configuration was found.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800593 return false;
594}
595
Paul Stewartbc6e7392012-05-24 07:07:48 -0700596bool Service::AddEAPCertification(const string &name, size_t depth) {
597 if (depth >= kEAPMaxCertificationElements) {
598 LOG(WARNING) << "Ignoring certification " << name
599 << " because depth " << depth
600 << " exceeds our maximum of "
601 << kEAPMaxCertificationElements;
602 return false;
603 }
604
605 if (depth >= eap_.remote_certification.size()) {
606 eap_.remote_certification.resize(depth + 1);
607 } else if (name == eap_.remote_certification[depth]) {
608 return true;
609 }
610
611 eap_.remote_certification[depth] = name;
612 LOG(INFO) << "Received certification for "
613 << name
614 << " at depth "
615 << depth;
616 return true;
617}
618
619void Service::ClearEAPCertification() {
620 eap_.remote_certification.clear();
621}
622
Gaurav Shah10109f22011-11-11 20:16:22 -0800623void Service::set_eap(const EapCredentials &eap) {
624 eap_ = eap;
625 // Note: Connectability can only be updated by a subclass of Service
626 // with knowledge of whether the service actually uses 802.1x credentials.
627}
628
mukesh agrawal00917ce2011-11-22 23:56:55 +0000629// static
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800630const char *Service::ConnectFailureToString(const ConnectFailure &state) {
631 switch (state) {
632 case kFailureUnknown:
633 return "Unknown";
Paul Stewartf2d60912012-07-15 08:37:30 -0700634 case kFailureAAA:
635 return flimflam::kErrorAaaFailed;
636 case kFailureActivation:
637 return flimflam::kErrorActivationFailed;
638 case kFailureBadPassphrase:
639 return flimflam::kErrorBadPassphrase;
640 case kFailureBadWEPKey:
641 return flimflam::kErrorBadWEPKey;
642 case kFailureConnect:
643 return flimflam::kErrorConnectFailed;
644 case kFailureDNSLookup:
645 return flimflam::kErrorDNSLookupFailed;
646 case kFailureDHCP:
647 return flimflam::kErrorDhcpFailed;
648 case kFailureHTTPGet:
649 return flimflam::kErrorHTTPGetFailed;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800650 case kFailureNeedEVDO:
Paul Stewartf2d60912012-07-15 08:37:30 -0700651 return flimflam::kErrorNeedEvdo;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800652 case kFailureNeedHomeNetwork:
Paul Stewartf2d60912012-07-15 08:37:30 -0700653 return flimflam::kErrorNeedHomeNetwork;
654 case kFailureOTASP:
655 return flimflam::kErrorOtaspFailed;
656 case kFailureOutOfRange:
657 return flimflam::kErrorOutOfRange;
658 case kFailurePinMissing:
659 return flimflam::kErrorPinMissing;
660 case kFailurePPPAuth:
661 return flimflam::kErrorPppAuthFailed;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000662 case kFailureMax:
663 return "Max failure error code";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800664 }
665 return "Invalid";
666}
667
668// static
669const char *Service::ConnectStateToString(const ConnectState &state) {
670 switch (state) {
671 case kStateUnknown:
672 return "Unknown";
673 case kStateIdle:
674 return "Idle";
675 case kStateAssociating:
676 return "Associating";
677 case kStateConfiguring:
678 return "Configuring";
679 case kStateConnected:
680 return "Connected";
681 case kStateDisconnected:
682 return "Disconnected";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000683 case kStatePortal:
684 return "Portal";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800685 case kStateFailure:
686 return "Failure";
687 case kStateOnline:
688 return "Online";
689 }
690 return "Invalid";
691}
692
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200693string Service::GetTechnologyString() const {
Gaurav Shah435de2c2011-11-17 19:01:07 -0800694 return Technology::NameFromIdentifier(technology());
695}
696
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200697string Service::CalculateTechnology(Error */*error*/) {
698 return GetTechnologyString();
699}
700
Jason Glasgowb5790052012-01-27 01:03:52 -0500701// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700702bool Service::DecideBetween(int a, int b, bool *decision) {
703 if (a == b)
704 return false;
705 *decision = (a > b);
706 return true;
707}
708
mukesh agrawal00917ce2011-11-22 23:56:55 +0000709// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700710bool Service::Compare(ServiceRefPtr a,
711 ServiceRefPtr b,
mukesh agrawalddc378f2012-02-17 18:26:20 -0800712 const vector<Technology::Identifier> &tech_order,
713 const char **reason) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700714 bool ret;
715
716 if (a->state() != b->state()) {
717 if (DecideBetween(a->IsConnected(), b->IsConnected(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800718 *reason = kServiceSortIsConnected;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700719 return ret;
720 }
721
Paul Stewarta121c442012-06-09 14:12:58 -0700722 if (DecideBetween(!a->IsPortalled(), !b->IsPortalled(), &ret)) {
723 *reason = kServiceSortIsPortalled;
724 return ret;
725 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700726
727 if (DecideBetween(a->IsConnecting(), b->IsConnecting(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800728 *reason = kServiceSortIsConnecting;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700729 return ret;
730 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000731
732 if (DecideBetween(!a->IsFailed(), !b->IsFailed(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800733 *reason = kServiceSortIsFailed;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000734 return ret;
735 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700736 }
737
Darin Petkov2f903b32012-04-18 12:56:43 +0200738 if (DecideBetween(a->connectable(), b->connectable(), &ret)) {
739 *reason = kServiceSortConnectable;
740 return ret;
741 }
742
743 // Ignore the auto-connect property if both services are connected
744 // already. This allows connected non-autoconnectable VPN services to be
745 // sorted higher than other connected services based on technology order.
746 if (!a->IsConnected() &&
747 DecideBetween(a->auto_connect(), b->auto_connect(), &ret)) {
748 *reason = kServiceSortAutoConnect;
749 return ret;
750 }
751
752 if (DecideBetween(a->favorite(), b->favorite(), &ret)) {
753 *reason = kServiceSortFavorite;
754 return ret;
755 }
756
757 if (DecideBetween(a->priority(), b->priority(), &ret)) {
758 *reason = kServiceSortPriority;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700759 return ret;
760 }
761
762 // TODO(pstew): Below this point we are making value judgements on
763 // services that are not related to anything intrinsic or
764 // user-specified. These heuristics should be richer (contain
765 // historical information, for example) and be subject to user
766 // customization.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700767 for (vector<Technology::Identifier>::const_iterator it = tech_order.begin();
768 it != tech_order.end();
769 ++it) {
Joshua Kroll053fa822012-06-05 09:50:43 -0700770 if (DecideBetween(a->technology() == *it, b->technology() == *it, &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800771 *reason = kServiceSortTechnology;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700772 return ret;
mukesh agrawalddc378f2012-02-17 18:26:20 -0800773 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700774 }
775
Paul Stewart1ca3e852011-11-04 07:50:49 -0700776 if (DecideBetween(a->security_level(), b->security_level(), &ret) ||
Paul Stewart22aa71b2011-09-16 12:15:11 -0700777 DecideBetween(a->strength(), b->strength(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800778 *reason = kServiceSortSecurityEtc;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700779 return ret;
780 }
781
mukesh agrawalddc378f2012-02-17 18:26:20 -0800782 *reason = kServiceSortUniqueName;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700783 return a->UniqueName() < b->UniqueName();
784}
785
Chris Masone34af2182011-08-22 11:59:36 -0700786const ProfileRefPtr &Service::profile() const { return profile_; }
787
788void Service::set_profile(const ProfileRefPtr &p) { profile_ = p; }
789
Paul Stewartff14b022012-04-24 20:06:23 -0700790void Service::OnPropertyChanged(const string &property) {
Paul Stewart81426132012-05-16 10:05:10 -0700791 if (Is8021x() &&
792 (property == flimflam::kEAPCertIDProperty ||
793 property == flimflam::kEAPClientCertProperty ||
794 property == flimflam::kEAPKeyIDProperty ||
795 property == flimflam::kEAPPINProperty ||
796 property == flimflam::kEapCaCertIDProperty ||
797 property == flimflam::kEapIdentityProperty ||
Paul Stewartadf79d82012-07-18 16:09:56 -0700798 property == flimflam::kEapKeyMgmtProperty ||
Paul Stewart81426132012-05-16 10:05:10 -0700799 property == flimflam::kEapPasswordProperty ||
800 property == flimflam::kEapPrivateKeyProperty)) {
801 // This notifies subclassess that EAP parameters have been changed.
802 set_eap(eap_);
803 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700804 SaveToProfile();
Paul Stewartd215af62012-04-24 23:25:50 -0700805 if ((property == flimflam::kCheckPortalProperty ||
806 property == flimflam::kProxyConfigProperty) &&
807 (state_ == kStateConnected ||
808 state_ == kStatePortal ||
809 state_ == kStateOnline)) {
810 manager_->RecheckPortalOnService(this);
811 }
Paul Stewartff14b022012-04-24 20:06:23 -0700812}
813
Christopher Wiley0801d192012-09-24 11:57:15 -0700814void Service::OnAfterResume() {
815 // Forget old autoconnect failures across suspend/resume.
816 auto_connect_cooldown_milliseconds_ = 0;
817 reenable_auto_connect_task_.Cancel();
Christopher Wileya4c61ae2012-10-01 11:04:30 -0700818 // Forget if the user disconnected us, we might be able to connect now.
819 explicitly_disconnected_ = false;
Christopher Wiley0801d192012-09-24 11:57:15 -0700820}
821
Paul Stewart10241e32012-04-23 18:15:06 -0700822string Service::GetIPConfigRpcIdentifier(Error *error) {
823 if (!connection_) {
824 error->Populate(Error::kNotFound);
825 return "/";
826 }
827
828 string id = connection_->ipconfig_rpc_identifier();
829
830 if (id.empty()) {
831 // Do not return an empty IPConfig.
832 error->Populate(Error::kNotFound);
833 return "/";
834 }
835
836 return id;
837}
838
mukesh agrawal29c13a12011-11-24 00:09:19 +0000839void Service::set_connectable(bool connectable) {
840 connectable_ = connectable;
841 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
842}
843
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200844void Service::SetConnectable(bool connectable) {
845 if (connectable_ == connectable) {
846 return;
847 }
848 connectable_ = connectable;
849 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
850 if (manager_->HasService(this)) {
851 manager_->UpdateService(this);
852 }
853}
854
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200855string Service::GetStateString() const {
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700856 switch (state_) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800857 case kStateIdle:
858 return flimflam::kStateIdle;
859 case kStateAssociating:
860 return flimflam::kStateAssociation;
861 case kStateConfiguring:
862 return flimflam::kStateConfiguration;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700863 case kStateConnected:
Paul Stewart20088d82012-02-16 06:58:55 -0800864 return flimflam::kStateReady;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800865 case kStateDisconnected:
866 return flimflam::kStateDisconnect;
867 case kStateFailure:
868 return flimflam::kStateFailure;
Paul Stewart20088d82012-02-16 06:58:55 -0800869 case kStatePortal:
870 return flimflam::kStatePortal;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800871 case kStateOnline:
872 return flimflam::kStateOnline;
873 case kStateUnknown:
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700874 default:
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800875 return "";
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700876 }
877}
878
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200879string Service::CalculateState(Error */*error*/) {
880 return GetStateString();
881}
882
mukesh agrawalbf14e942012-03-02 14:36:34 -0800883bool Service::IsAutoConnectable(const char **reason) const {
884 if (!connectable()) {
885 *reason = kAutoConnNotConnectable;
886 return false;
887 }
888
889 if (IsConnected()) {
890 *reason = kAutoConnConnected;
891 return false;
892 }
893
894 if (IsConnecting()) {
895 *reason = kAutoConnConnecting;
896 return false;
897 }
898
899 if (explicitly_disconnected_) {
900 *reason = kAutoConnExplicitDisconnect;
901 return false;
902 }
903
Christopher Wiley0801d192012-09-24 11:57:15 -0700904 if (!reenable_auto_connect_task_.IsCancelled()) {
905 *reason = kAutoConnThrottled;
906 return false;
907 }
908
mukesh agrawalbf14e942012-03-02 14:36:34 -0800909 return true;
mukesh agrawal76d13882012-01-12 15:23:11 -0800910}
911
Paul Stewartd215af62012-04-24 23:25:50 -0700912bool Service::IsPortalDetectionDisabled() const {
913 return check_portal_ == kCheckPortalFalse;
914}
915
916bool Service::IsPortalDetectionAuto() const {
917 return check_portal_ == kCheckPortalAuto;
918}
919
mukesh agrawalffa3d042011-10-06 15:26:10 -0700920void Service::HelpRegisterDerivedBool(
921 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800922 bool(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700923 void(Service::*set)(const bool&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700924 store_.RegisterDerivedBool(
925 name,
926 BoolAccessor(new CustomAccessor<Service, bool>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700927}
928
mukesh agrawalffa3d042011-10-06 15:26:10 -0700929void Service::HelpRegisterDerivedString(
930 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800931 string(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700932 void(Service::*set)(const string&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700933 store_.RegisterDerivedString(
934 name,
935 StringAccessor(new CustomAccessor<Service, string>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700936}
937
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400938void Service::HelpRegisterDerivedRpcIdentifier(
939 const string &name,
940 RpcIdentifier(Service::*get)(Error *),
941 void(Service::*set)(const RpcIdentifier&, Error *)) {
942 store_.RegisterDerivedRpcIdentifier(
943 name,
944 RpcIdentifierAccessor(new CustomAccessor<Service, RpcIdentifier>(
945 this, get, set)));
946}
947
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800948void Service::HelpRegisterDerivedUint16(
949 const string &name,
950 uint16(Service::*get)(Error *),
951 void(Service::*set)(const uint16&, Error *)) {
952 store_.RegisterDerivedUint16(
953 name,
954 Uint16Accessor(new CustomAccessor<Service, uint16>(this, get, set)));
955}
956
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800957void Service::HelpRegisterWriteOnlyDerivedString(
958 const string &name,
959 void(Service::*set)(const string &, Error *),
960 void(Service::*clear)(Error *),
961 const string *default_value) {
962 store_.RegisterDerivedString(
963 name,
964 StringAccessor(
965 new CustomWriteOnlyAccessor<Service, string>(
966 this, set, clear, default_value)));
967}
968
Darin Petkovba40dd32011-07-11 20:06:39 -0700969void Service::SaveString(StoreInterface *storage,
Chris Masone34af2182011-08-22 11:59:36 -0700970 const string &id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700971 const string &key,
972 const string &value,
973 bool crypted,
974 bool save) {
975 if (value.empty() || !save) {
Chris Masone34af2182011-08-22 11:59:36 -0700976 storage->DeleteKey(id, key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700977 return;
978 }
979 if (crypted) {
Chris Masone34af2182011-08-22 11:59:36 -0700980 storage->SetCryptedString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700981 return;
982 }
Chris Masone34af2182011-08-22 11:59:36 -0700983 storage->SetString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700984}
985
Chris Masone34af2182011-08-22 11:59:36 -0700986void Service::LoadEapCredentials(StoreInterface *storage, const string &id) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800987 EapCredentials eap;
988 storage->GetCryptedString(id, kStorageEapIdentity, &eap.identity);
989 storage->GetString(id, kStorageEapEap, &eap.eap);
990 storage->GetString(id, kStorageEapInnerEap, &eap.inner_eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700991 storage->GetCryptedString(id,
992 kStorageEapAnonymousIdentity,
Gaurav Shah10109f22011-11-11 20:16:22 -0800993 &eap.anonymous_identity);
994 storage->GetString(id, kStorageEapClientCert, &eap.client_cert);
995 storage->GetString(id, kStorageEapCertID, &eap.cert_id);
996 storage->GetString(id, kStorageEapPrivateKey, &eap.private_key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700997 storage->GetCryptedString(id,
998 kStorageEapPrivateKeyPassword,
Gaurav Shah10109f22011-11-11 20:16:22 -0800999 &eap.private_key_password);
1000 storage->GetString(id, kStorageEapKeyID, &eap.key_id);
1001 storage->GetString(id, kStorageEapCACert, &eap.ca_cert);
1002 storage->GetString(id, kStorageEapCACertID, &eap.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -07001003 storage->GetString(id, kStorageEapCACertNSS, &eap.ca_cert_nss);
Gaurav Shah10109f22011-11-11 20:16:22 -08001004 storage->GetBool(id, kStorageEapUseSystemCAs, &eap.use_system_cas);
1005 storage->GetString(id, kStorageEapPIN, &eap.pin);
1006 storage->GetCryptedString(id, kStorageEapPassword, &eap.password);
1007 storage->GetString(id, kStorageEapKeyManagement, &eap.key_management);
1008 set_eap(eap);
Darin Petkovba40dd32011-07-11 20:06:39 -07001009}
1010
Chris Masone34af2182011-08-22 11:59:36 -07001011void Service::SaveEapCredentials(StoreInterface *storage, const string &id) {
Darin Petkovba40dd32011-07-11 20:06:39 -07001012 bool save = save_credentials_;
Chris Masone34af2182011-08-22 11:59:36 -07001013 SaveString(storage, id, kStorageEapIdentity, eap_.identity, true, save);
1014 SaveString(storage, id, kStorageEapEap, eap_.eap, false, true);
1015 SaveString(storage, id, kStorageEapInnerEap, eap_.inner_eap, false, true);
Darin Petkovba40dd32011-07-11 20:06:39 -07001016 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -07001017 id,
Darin Petkovba40dd32011-07-11 20:06:39 -07001018 kStorageEapAnonymousIdentity,
1019 eap_.anonymous_identity,
1020 true,
1021 save);
Chris Masone34af2182011-08-22 11:59:36 -07001022 SaveString(storage, id, kStorageEapClientCert, eap_.client_cert, false, save);
1023 SaveString(storage, id, kStorageEapCertID, eap_.cert_id, false, save);
1024 SaveString(storage, id, kStorageEapPrivateKey, eap_.private_key, false, save);
Darin Petkovba40dd32011-07-11 20:06:39 -07001025 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -07001026 id,
Darin Petkovba40dd32011-07-11 20:06:39 -07001027 kStorageEapPrivateKeyPassword,
1028 eap_.private_key_password,
1029 true,
1030 save);
Chris Masone34af2182011-08-22 11:59:36 -07001031 SaveString(storage, id, kStorageEapKeyID, eap_.key_id, false, save);
1032 SaveString(storage, id, kStorageEapCACert, eap_.ca_cert, false, true);
1033 SaveString(storage, id, kStorageEapCACertID, eap_.ca_cert_id, false, true);
Paul Stewartecf4cd12012-04-17 11:08:39 -07001034 SaveString(storage, id, kStorageEapCACertNSS, eap_.ca_cert_nss, false, true);
Chris Masone34af2182011-08-22 11:59:36 -07001035 storage->SetBool(id, kStorageEapUseSystemCAs, eap_.use_system_cas);
1036 SaveString(storage, id, kStorageEapPIN, eap_.pin, false, save);
1037 SaveString(storage, id, kStorageEapPassword, eap_.password, true, save);
Darin Petkovba40dd32011-07-11 20:06:39 -07001038 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -07001039 id,
Darin Petkovba40dd32011-07-11 20:06:39 -07001040 kStorageEapKeyManagement,
1041 eap_.key_management,
1042 false,
1043 true);
1044}
1045
Paul Stewartd8ad3c42012-01-09 12:39:38 -08001046void Service::UnloadEapCredentials() {
1047 eap_.identity = "";
1048 eap_.eap = "";
1049 eap_.inner_eap = "";
1050 eap_.anonymous_identity = "";
1051 eap_.client_cert = "";
1052 eap_.cert_id = "";
1053 eap_.private_key = "";
1054 eap_.private_key_password = "";
1055 eap_.key_id = "";
1056 eap_.ca_cert = "";
1057 eap_.ca_cert_id = "";
Paul Stewart20550982012-04-16 12:16:11 -07001058 eap_.use_system_cas = true;
Paul Stewartd8ad3c42012-01-09 12:39:38 -08001059 eap_.pin = "";
1060 eap_.password = "";
Paul Stewartd8ad3c42012-01-09 12:39:38 -08001061}
1062
Paul Stewartcb59fed2012-03-21 21:14:46 -07001063void Service::IgnoreParameterForConfigure(const string &parameter) {
1064 parameters_ignored_for_configure_.insert(parameter);
1065}
1066
Paul Stewartac4ac002011-08-26 12:04:26 -07001067const string &Service::GetEAPKeyManagement() const {
1068 return eap_.key_management;
1069}
1070
1071void Service::SetEAPKeyManagement(const string &key_management) {
1072 eap_.key_management = key_management;
1073}
1074
Thieu Le284fe792012-01-31 17:53:19 -08001075bool Service::GetAutoConnect(Error */*error*/) {
1076 return auto_connect();
1077}
1078
1079void Service::SetAutoConnect(const bool &connect, Error *error) {
Wade Guthrie4f28e8b2012-04-11 10:52:07 -07001080 set_auto_connect(connect);
Thieu Le284fe792012-01-31 17:53:19 -08001081}
1082
Paul Stewartd215af62012-04-24 23:25:50 -07001083string Service::GetCheckPortal(Error *error) {
1084 return check_portal_;
1085}
1086
1087void Service::SetCheckPortal(const string &check_portal, Error *error) {
1088 if (check_portal == check_portal_) {
1089 return;
1090 }
1091 if (check_portal != kCheckPortalFalse &&
1092 check_portal != kCheckPortalTrue &&
1093 check_portal != kCheckPortalAuto) {
1094 Error::PopulateAndLog(error, Error::kInvalidArguments,
1095 base::StringPrintf(
1096 "Invalid Service CheckPortal property value: %s",
1097 check_portal.c_str()));
1098 return;
1099 }
1100 check_portal_ = check_portal;
1101}
1102
Paul Stewart9f32d192012-01-30 20:37:50 -08001103void Service::SetEAPPassword(const string &password, Error */*error*/) {
1104 eap_.password = password;
1105}
1106
1107void Service::SetEAPPrivateKeyPassword(const string &password,
1108 Error */*error*/) {
1109 eap_.private_key_password = password;
1110}
1111
Paul Stewart0c438332012-04-11 07:55:27 -07001112string Service::GetNameProperty(Error *error) {
1113 return friendly_name_;
1114}
1115
1116void Service::AssertTrivialSetNameProperty(const string &name, Error *error) {
1117 if (name != friendly_name_) {
1118 Error::PopulateAndLog(error, Error::kInvalidArguments,
Paul Stewart2bf424f2012-04-11 18:59:39 -07001119 base::StringPrintf(
1120 "Service Name property cannot be modified "
1121 "(%s to %s)", friendly_name_.c_str(),
1122 name.c_str()));
Paul Stewart0c438332012-04-11 07:55:27 -07001123 }
1124}
1125
Paul Stewart1b1a7f22012-01-06 16:24:06 -08001126string Service::GetProfileRpcId(Error *error) {
1127 if (!profile_) {
1128 // This happens in some unit tests where profile_ is not set.
1129 error->Populate(Error::kNotFound);
1130 return "";
1131 }
1132 return profile_->GetRpcIdentifier();
1133}
1134
1135void Service::SetProfileRpcId(const string &profile, Error *error) {
1136 manager_->SetProfileForService(this, profile, error);
1137}
1138
Paul Stewartbe5f5b32011-12-07 17:11:11 -08001139uint16 Service::GetHTTPProxyPort(Error */*error*/) {
1140 if (http_proxy_.get()) {
1141 return static_cast<uint16>(http_proxy_->proxy_port());
1142 }
1143 return 0;
1144}
1145
mukesh agrawalcf24a242012-05-21 16:46:11 -07001146void Service::SaveToProfile() {
1147 if (profile_.get() && profile_->GetConstStorage()) {
1148 profile_->UpdateService(this);
1149 }
1150}
1151
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001152void Service::SetStrength(uint8 strength) {
1153 if (strength == strength_) {
1154 return;
1155 }
1156 strength_ = strength;
1157 adaptor_->EmitUint8Changed(flimflam::kSignalStrengthProperty, strength);
1158}
1159
Paul Stewartf2d60912012-07-15 08:37:30 -07001160void Service::UpdateErrorProperty() {
1161 const string error(ConnectFailureToString(failure_));
1162 if (error == error_) {
1163 return;
1164 }
1165 error_ = error;
1166 adaptor_->EmitStringChanged(flimflam::kErrorProperty, error);
1167}
1168
Paul Stewart75897df2011-04-27 09:05:53 -07001169} // namespace shill