blob: 17d277eafcd28a0f4e7cfa088f2cdde0ceb6df3c [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 Masone2b105542011-06-22 10:58:09 -07005#include "shill/device.h"
6
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07007#include <netinet/in.h>
8#include <linux/if.h> // Needs definitions from netinet/in.h
Paul Stewart75897df2011-04-27 09:05:53 -07009#include <stdio.h>
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070010#include <time.h>
Chris Masoneee929b72011-05-10 10:02:18 -070011
Paul Stewart75897df2011-04-27 09:05:53 -070012#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070013#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070014
Eric Shienbrood3e20a232012-02-16 11:35:56 -050015#include <base/bind.h>
Paul Stewart2bf1d352011-12-06 15:02:55 -080016#include <base/file_util.h>
Chris Masoneee929b72011-05-10 10:02:18 -070017#include <base/logging.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070018#include <base/memory/ref_counted.h>
Chris Masone5dec5f42011-07-22 14:07:55 -070019#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070020#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070021
Paul Stewarte6132022011-08-16 09:11:02 -070022#include "shill/connection.h"
Paul Stewart75897df2011-04-27 09:05:53 -070023#include "shill/control_interface.h"
Chris Masoned7732e42011-05-20 11:08:56 -070024#include "shill/device_dbus_adaptor.h"
Chris Masone2b105542011-06-22 10:58:09 -070025#include "shill/dhcp_config.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070026#include "shill/dhcp_provider.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070027#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070028#include "shill/event_dispatcher.h"
Paul Stewartf65320c2011-10-13 14:34:52 -070029#include "shill/http_proxy.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070030#include "shill/manager.h"
Thieu Le85e050b2012-03-13 15:04:38 -070031#include "shill/metrics.h"
Chris Masone95207da2011-06-29 16:50:49 -070032#include "shill/property_accessor.h"
Chris Masone2b105542011-06-22 10:58:09 -070033#include "shill/refptr_types.h"
Thieu Lefb46caf2012-03-08 11:57:15 -080034#include "shill/routing_table.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070035#include "shill/rtnl_handler.h"
Chris Masone2b105542011-06-22 10:58:09 -070036#include "shill/service.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070037#include "shill/store_interface.h"
Gaurav Shah435de2c2011-11-17 19:01:07 -080038#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070039
Eric Shienbrood3e20a232012-02-16 11:35:56 -050040using base::Bind;
Chris Masone5dec5f42011-07-22 14:07:55 -070041using base::StringPrintf;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070042using std::string;
43using std::vector;
44
Paul Stewart75897df2011-04-27 09:05:53 -070045namespace shill {
Chris Masone5dec5f42011-07-22 14:07:55 -070046
47// static
Paul Stewart2bf1d352011-12-06 15:02:55 -080048const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s";
49// static
50const char Device::kIPFlagVersion4[] = "ipv4";
51// static
52const char Device::kIPFlagVersion6[] = "ipv6";
53// static
54const char Device::kIPFlagDisableIPv6[] = "disable_ipv6";
55// static
56const char Device::kIPFlagUseTempAddr[] = "use_tempaddr";
57// static
58const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2";
Paul Stewartc8f4bef2011-12-13 09:45:51 -080059// static
60const char Device::kIPFlagReversePathFilter[] = "rp_filter";
61// static
62const char Device::kIPFlagReversePathFilterEnabled[] = "1";
63// static
64const char Device::kIPFlagReversePathFilterLooseMode[] = "2";
Paul Stewart2bf1d352011-12-06 15:02:55 -080065
66// static
Chris Masone5dec5f42011-07-22 14:07:55 -070067const char Device::kStoragePowered[] = "Powered";
68
69// static
70const char Device::kStorageIPConfigs[] = "IPConfigs";
71
Paul Stewart75897df2011-04-27 09:05:53 -070072Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070073 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080074 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070075 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070076 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070077 const string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080078 int interface_index,
79 Technology::Identifier technology)
Eric Shienbrood9a245532012-03-07 14:20:39 -050080 : enabled_(false),
81 enabled_persistent_(true),
82 enabled_pending_(enabled_),
Chris Masoneb925cc82011-06-22 15:39:57 -070083 reconnect_(true),
Chris Masone626719f2011-08-18 16:58:48 -070084 hardware_address_(address),
mukesh agrawalf60e4062011-05-27 13:13:41 -070085 interface_index_(interface_index),
86 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070087 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070088 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070089 control_interface_(control_interface),
90 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080091 metrics_(metrics),
Chris Masone7df0c672011-07-15 10:24:54 -070092 manager_(manager),
Eric Shienbrood9a245532012-03-07 14:20:39 -050093 weak_ptr_factory_(this),
Darin Petkov77cb6812011-08-15 16:19:41 -070094 adaptor_(control_interface->CreateDeviceAdaptor(this)),
Eric Shienbrood9a245532012-03-07 14:20:39 -050095 portal_detector_callback_(Bind(&Device::PortalDetectorCallback,
96 weak_ptr_factory_.GetWeakPtr())),
Gaurav Shah435de2c2011-11-17 19:01:07 -080097 technology_(technology),
Thieu Le85e050b2012-03-13 15:04:38 -070098 portal_attempts_to_online_(0),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070099 dhcp_provider_(DHCPProvider::GetInstance()),
Thieu Lefb46caf2012-03-08 11:57:15 -0800100 routing_table_(RoutingTable::GetInstance()),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700101 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700102 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -0700103
104 // flimflam::kBgscanMethodProperty: Registered in WiFi
105 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
106 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
107
108 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
109 // flimflam::kCarrierProperty: Registered in Cellular
110 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -0700111 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700112 // flimflam::kImeiProperty: Registered in Cellular
113 // flimflam::kImsiProperty: Registered in Cellular
114 // flimflam::kManufacturerProperty: Registered in Cellular
115 // flimflam::kMdnProperty: Registered in Cellular
116 // flimflam::kMeidProperty: Registered in Cellular
117 // flimflam::kMinProperty: Registered in Cellular
118 // flimflam::kModelIDProperty: Registered in Cellular
119 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
120 // flimflam::kHardwareRevisionProperty: Registered in Cellular
121 // flimflam::kPRLVersionProperty: Registered in Cellular
122 // flimflam::kSIMLockStatusProperty: Registered in Cellular
123 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -0700124 // flimflam::kDBusConnectionProperty: Registered in Cellular
125 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700126
Jason Glasgowe8334fd2012-03-30 16:02:37 -0400127 store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
Jason Glasgow08afdff2012-04-03 10:22:26 -0400128 HelpRegisterConstDerivedRpcIdentifiers(flimflam::kIPConfigsProperty,
129 &Device::AvailableIPConfigs);
Chris Masone27c4aa52011-07-02 13:10:14 -0700130 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500131 store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_);
Jason Glasgowb5790052012-01-27 01:03:52 -0500132 HelpRegisterDerivedString(flimflam::kTypeProperty,
133 &Device::GetTechnologyString,
134 NULL);
135
Chris Masoneb925cc82011-06-22 15:39:57 -0700136 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700137 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700138
Chris Masone4e851612011-07-01 10:46:53 -0700139 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700140 // known_properties_.push_back(flimflam::kNetworksProperty);
141
Chris Masone4d42df82011-07-02 17:09:39 -0700142 // flimflam::kScanningProperty: Registered in WiFi, Cellular
143 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
144
145 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -0700146 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -0700147}
148
149Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700150 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700151}
152
mukesh agrawal1830fa12011-09-26 14:31:40 -0700153bool Device::TechnologyIs(const Technology::Identifier /*type*/) const {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700154 return false;
155}
156
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700157void Device::LinkEvent(unsigned flags, unsigned change) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700158 VLOG(2) << "Device " << link_name_
159 << std::showbase << std::hex
160 << " flags " << flags << " changed " << change
161 << std::dec << std::noshowbase;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700162}
163
Darin Petkovc0865312011-09-16 15:31:20 -0700164void Device::Scan(Error *error) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700165 VLOG(2) << "Device " << link_name_ << " scan requested.";
Paul Stewartbe005172011-11-02 18:10:29 -0700166 Error::PopulateAndLog(error, Error::kNotSupported,
167 "Device doesn't support scan.");
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700168}
169
Eric Shienbrood9a245532012-03-07 14:20:39 -0500170void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error,
171 const ResultCallback &/*callback*/) {
172 Error::PopulateAndLog(error, Error::kNotSupported,
Paul Stewartbe005172011-11-02 18:10:29 -0700173 "Device doesn't support network registration.");
Darin Petkov9ae310f2011-08-30 15:41:13 -0700174}
175
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100176void Device::RequirePIN(
Eric Shienbrood9a245532012-03-07 14:20:39 -0500177 const string &/*pin*/, bool /*require*/,
178 Error *error, const ResultCallback &/*callback*/) {
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100179 VLOG(2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500180 Error::PopulateAndLog(error, Error::kNotSupported,
181 "Device doesn't support RequirePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700182}
183
Eric Shienbrood9a245532012-03-07 14:20:39 -0500184void Device::EnterPIN(const string &/*pin*/,
185 Error *error, const ResultCallback &/*callback*/) {
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100186 VLOG(2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500187 Error::PopulateAndLog(error, Error::kNotSupported,
188 "Device doesn't support EnterPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700189}
190
mukesh agrawal1830fa12011-09-26 14:31:40 -0700191void Device::UnblockPIN(const string &/*unblock_code*/,
192 const string &/*pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500193 Error *error, const ResultCallback &/*callback*/) {
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100194 VLOG(2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500195 Error::PopulateAndLog(error, Error::kNotSupported,
196 "Device doesn't support UnblockPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700197}
198
mukesh agrawal1830fa12011-09-26 14:31:40 -0700199void Device::ChangePIN(const string &/*old_pin*/,
200 const string &/*new_pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500201 Error *error, const ResultCallback &/*callback*/) {
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100202 VLOG(2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500203 Error::PopulateAndLog(error, Error::kNotSupported,
204 "Device doesn't support ChangePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700205}
206
Paul Stewart2bf1d352011-12-06 15:02:55 -0800207void Device::DisableIPv6() {
208 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
209}
210
211void Device::EnableIPv6() {
212 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
213}
214
215void Device::EnableIPv6Privacy() {
216 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
217 kIPFlagUseTempAddrUsedAndDefault);
218}
219
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800220void Device::DisableReversePathFilter() {
221 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
222 // for IPv6. crosbug.com/24228
223 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
224 kIPFlagReversePathFilterLooseMode);
225}
226
227void Device::EnableReversePathFilter() {
228 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
229 kIPFlagReversePathFilterEnabled);
230}
231
Gaurav Shah435de2c2011-11-17 19:01:07 -0800232bool Device::IsConnected() const {
233 if (selected_service_)
234 return selected_service_->IsConnected();
235 return false;
236}
237
Chris Masone27c4aa52011-07-02 13:10:14 -0700238string Device::GetRpcIdentifier() {
239 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700240}
241
Chris Masone5dec5f42011-07-22 14:07:55 -0700242string Device::GetStorageIdentifier() {
243 string id = GetRpcIdentifier();
244 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700245 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700246 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700247 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700248 return id;
249}
250
Jason Glasgowb5790052012-01-27 01:03:52 -0500251string Device::GetTechnologyString(Error */*error*/) {
252 return Technology::NameFromIdentifier(technology());
253}
254
Chris Masone19e30402011-07-19 15:48:47 -0700255const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700256 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700257}
258
Chris Masone19e30402011-07-19 15:48:47 -0700259const string& Device::UniqueName() const {
260 return unique_id_;
261}
262
Chris Masone5dec5f42011-07-22 14:07:55 -0700263bool Device::Load(StoreInterface *storage) {
264 const string id = GetStorageIdentifier();
265 if (!storage->ContainsGroup(id)) {
266 LOG(WARNING) << "Device is not available in the persistent store: " << id;
267 return false;
268 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500269 enabled_persistent_ = true;
270 storage->GetBool(id, kStoragePowered, &enabled_persistent_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700271 // TODO(cmasone): What does it mean to load an IPConfig identifier??
272 return true;
273}
274
275bool Device::Save(StoreInterface *storage) {
276 const string id = GetStorageIdentifier();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500277 storage->SetBool(id, kStoragePowered, enabled_persistent_);
Chris Masone34af2182011-08-22 11:59:36 -0700278 if (ipconfig_.get()) {
279 // The _0 is an index into the list of IPConfigs that this device might
280 // have. We only have one IPConfig right now, and I hope to never have
281 // to support more, as sleffler indicates that associating IPConfigs
282 // with devices is wrong and due to be changed in flimflam anyhow.
283 string suffix = hardware_address_ + "_0";
284 ipconfig_->Save(storage, suffix);
285 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
286 }
Chris Masone5dec5f42011-07-22 14:07:55 -0700287 return true;
288}
289
Darin Petkovafa6fc42011-06-21 16:21:08 -0700290void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800291 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700292 if (ipconfig_.get()) {
293 ipconfig_->ReleaseIP();
294 ipconfig_ = NULL;
295 }
Paul Stewarte6132022011-08-16 09:11:02 -0700296 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700297}
298
Paul Stewart2bf1d352011-12-06 15:02:55 -0800299bool Device::AcquireIPConfig() {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700300 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800301 EnableIPv6();
Paul Stewartd32f4842012-01-11 16:08:13 -0800302 ipconfig_ = dhcp_provider_->CreateConfig(link_name_, manager_->GetHostName());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500303 ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated,
304 weak_ptr_factory_.GetWeakPtr()));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700305 return ipconfig_->RequestIP();
306}
307
Jason Glasgowb5790052012-01-27 01:03:52 -0500308void Device::HelpRegisterDerivedString(
309 const string &name,
310 string(Device::*get)(Error *error),
311 void(Device::*set)(const string &value, Error *error)) {
312 store_.RegisterDerivedString(
313 name,
314 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
315}
316
mukesh agrawalffa3d042011-10-06 15:26:10 -0700317void Device::HelpRegisterDerivedStrings(
318 const string &name,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500319 Strings(Device::*get)(Error *error),
320 void(Device::*set)(const Strings &value, Error *error)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700321 store_.RegisterDerivedStrings(
322 name,
323 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700324}
325
Jason Glasgow08afdff2012-04-03 10:22:26 -0400326void Device::HelpRegisterConstDerivedRpcIdentifiers(
327 const string &name,
328 RpcIdentifiers(Device::*get)(Error *)) {
329 store_.RegisterDerivedRpcIdentifiers(
330 name,
331 RpcIdentifiersAccessor(
332 new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL)));
333}
334
Darin Petkov79d74c92012-03-07 17:20:32 +0100335void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700336 VLOG(2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700337 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700338 CreateConnection();
339 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800340 // SetConnection must occur after the UpdateFromIPConfig so the
341 // service can use the values derived from the connection.
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800342 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800343 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800344 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800345 // The service state change needs to happen last, so that at the
346 // time we report the state change to the manager, the service
347 // has its connection.
348 SetServiceState(Service::kStateConnected);
Thieu Le85e050b2012-03-13 15:04:38 -0700349 portal_attempts_to_online_ = 0;
Paul Stewart20088d82012-02-16 06:58:55 -0800350 // Subtle: Start portal detection after transitioning the service
351 // to the Connected state because this call may immediately transition
352 // to the Online state.
353 StartPortalDetection();
Paul Stewarte6132022011-08-16 09:11:02 -0700354 } else {
355 // TODO(pstew): This logic gets more complex when multiple IPConfig types
356 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700357 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700358 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700359 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700360}
361
Paul Stewarte6132022011-08-16 09:11:02 -0700362void Device::CreateConnection() {
363 VLOG(2) << __func__;
364 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700365 connection_ = new Connection(interface_index_,
366 link_name_,
Paul Stewarte00600e2012-03-16 07:08:00 -0700367 technology_,
Paul Stewart9a908082011-08-31 12:18:48 -0700368 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700369 }
370}
371
372void Device::DestroyConnection() {
373 VLOG(2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800374 StopPortalDetection();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800375 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800376 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800377 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800378 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700379}
380
Paul Stewart03dba0b2011-08-22 16:32:45 -0700381void Device::SelectService(const ServiceRefPtr &service) {
Paul Stewarta2b8cd12011-08-25 14:55:32 -0700382 VLOG(2) << __func__ << ": "
mukesh agrawal15908392011-11-16 18:29:25 +0000383 << (service.get() ?
384 StringPrintf("%s (%s)",
385 service->UniqueName().c_str(),
386 service->friendly_name().c_str()) :
387 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000388
389 if (selected_service_.get() == service.get()) {
390 // No change to |selected_service_|. Return early to avoid
391 // changing its state.
392 return;
393 }
394
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800395 if (selected_service_.get()) {
396 if (selected_service_->state() != Service::kStateFailure) {
397 selected_service_->SetState(Service::kStateIdle);
398 }
399 // TODO(pstew): We need to revisit the model here: should the Device
400 // subclass be responsible for calling DestroyIPConfig() (which would
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800401 // trigger DestroyConnection() and Service::SetConnection(NULL))?
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800402 // Ethernet does, but WiFi currently does not. crosbug.com/23929
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800403 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700404 }
405 selected_service_ = service;
406}
407
408void Device::SetServiceState(Service::ConnectState state) {
409 if (selected_service_.get()) {
410 selected_service_->SetState(state);
411 }
412}
413
414void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
415 if (selected_service_.get()) {
416 selected_service_->SetFailure(failure_state);
417 }
418}
419
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400420void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) {
421 if (selected_service_.get()) {
422 selected_service_->SetFailureSilent(failure_state);
423 }
424}
425
Chris Masone34af2182011-08-22 11:59:36 -0700426string Device::SerializeIPConfigs(const string &suffix) {
427 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700428}
429
Paul Stewart2bf1d352011-12-06 15:02:55 -0800430bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
431 const string &value) {
432 string ip_version;
433 if (family == IPAddress::kFamilyIPv4) {
434 ip_version = kIPFlagVersion4;
435 } else if (family == IPAddress::kFamilyIPv6) {
436 ip_version = kIPFlagVersion6;
437 } else {
438 NOTIMPLEMENTED();
439 }
440 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
441 link_name_.c_str(), flag.c_str()));
442 VLOG(2) << "Writing " << value << " to flag file " << flag_file.value();
443 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
444 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
445 value.c_str(), flag_file.value().c_str());
446 return false;
447 }
448 return true;
449}
450
Paul Stewartc681fa02012-03-02 19:40:04 -0800451bool Device::RequestPortalDetection() {
452 if (!selected_service_) {
453 VLOG(2) << FriendlyName()
454 << ": No selected service, so no need for portal check.";
455 return false;
456 }
457
458 if (!connection_.get()) {
459 VLOG(2) << FriendlyName()
460 << ": No connection, so no need for portal check.";
461 return false;
462 }
463
464 if (selected_service_->state() != Service::kStatePortal) {
465 VLOG(2) << FriendlyName()
466 << ": Service is not in portal state. No need to start check.";
467 return false;
468 }
469
470 if (!connection_->is_default()) {
471 VLOG(2) << FriendlyName()
472 << ": Service is not the default connection. Don't start check.";
473 return false;
474 }
475
476 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
477 VLOG(2) << FriendlyName() << ": Portal detection is already running.";
478 return true;
479 }
480
481 return StartPortalDetection();
482}
483
Paul Stewart20088d82012-02-16 06:58:55 -0800484bool Device::StartPortalDetection() {
485 if (!manager_->IsPortalDetectionEnabled(technology())) {
486 // If portal detection is disabled for this technology, immediately set
487 // the service state to "Online".
488 VLOG(2) << "Device " << FriendlyName()
489 << ": portal detection is disabled; marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800490 SetServiceConnectedState(Service::kStateOnline);
491 return false;
492 }
493
494 DCHECK(selected_service_.get());
495 if (selected_service_->HasProxyConfig()) {
496 // Services with HTTP proxy configurations should not be checked by the
497 // connection manager, since we don't have the ability to evaluate
498 // arbitrary proxy configs and their possible credentials.
499 VLOG(2) << "Device " << FriendlyName()
500 << ": service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800501 SetServiceConnectedState(Service::kStateOnline);
502 return false;
503 }
504
505 portal_detector_.reset(new PortalDetector(connection_,
506 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500507 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800508 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
509 LOG(ERROR) << "Device " << FriendlyName()
510 << ": Portal detection failed to start: likely bad URL: "
511 << manager_->GetPortalCheckURL();
512 SetServiceConnectedState(Service::kStateOnline);
513 return false;
514 }
515
516 VLOG(2) << "Device " << FriendlyName() << ": portal detection has started.";
517 return true;
518}
519
520void Device::StopPortalDetection() {
521 VLOG(2) << "Device " << FriendlyName() << ": portal detection has stopped.";
522 portal_detector_.reset();
523}
524
525void Device::SetServiceConnectedState(Service::ConnectState state) {
526 DCHECK(selected_service_.get());
527
528 if (!selected_service_.get()) {
529 LOG(ERROR) << FriendlyName() << ": "
530 << "Portal detection completed but no selected service exists!";
531 return;
532 }
533
534 if (!selected_service_->IsConnected()) {
535 LOG(ERROR) << FriendlyName() << ": "
536 << "Portal detection completed but selected service "
537 << selected_service_->UniqueName()
538 << " is in non-connected state.";
539 return;
540 }
541
Paul Stewartc681fa02012-03-02 19:40:04 -0800542 if (state == Service::kStatePortal && connection_->is_default() &&
543 manager_->GetPortalCheckInterval() != 0) {
544 CHECK(portal_detector_.get());
545 if (!portal_detector_->StartAfterDelay(
546 manager_->GetPortalCheckURL(),
547 manager_->GetPortalCheckInterval())) {
548 LOG(ERROR) << "Device " << FriendlyName()
549 << ": Portal detection failed to restart: likely bad URL: "
550 << manager_->GetPortalCheckURL();
551 SetServiceState(Service::kStateOnline);
552 portal_detector_.reset();
553 return;
554 }
555 VLOG(2) << "Device " << FriendlyName() << ": portal detection retrying.";
556 } else {
557 VLOG(2) << "Device " << FriendlyName() << ": portal will not retry.";
558 portal_detector_.reset();
559 }
560
Paul Stewart20088d82012-02-16 06:58:55 -0800561 SetServiceState(state);
562}
563
564void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
565 if (!result.final) {
566 VLOG(2) << "Device " << FriendlyName()
567 << ": received non-final status: "
568 << PortalDetector::StatusToString(result.status);
569 return;
570 }
571
572 VLOG(2) << "Device " << FriendlyName()
573 << ": received final status: "
574 << PortalDetector::StatusToString(result.status);
575
Thieu Le85e050b2012-03-13 15:04:38 -0700576 portal_attempts_to_online_ += result.num_attempts;
577
578 metrics()->SendEnumToUMA(
579 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
580 Metrics::PortalDetectionResultToEnum(result),
581 Metrics::kPortalResultMax);
582
Paul Stewart20088d82012-02-16 06:58:55 -0800583 if (result.status == PortalDetector::kStatusSuccess) {
584 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700585
586 metrics()->SendToUMA(
587 metrics()->GetFullMetricName(
588 Metrics::kMetricPortalAttemptsToOnline, technology()),
589 portal_attempts_to_online_,
590 Metrics::kMetricPortalAttemptsToOnlineMin,
591 Metrics::kMetricPortalAttemptsToOnlineMax,
592 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800593 } else {
594 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700595
596 metrics()->SendToUMA(
597 metrics()->GetFullMetricName(
598 Metrics::kMetricPortalAttempts, technology()),
599 result.num_attempts,
600 Metrics::kMetricPortalAttemptsMin,
601 Metrics::kMetricPortalAttemptsMax,
602 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800603 }
604}
605
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800606vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Jason Glasgow08afdff2012-04-03 10:22:26 -0400607 if (ipconfig_.get()) {
608 string id = ipconfig_->GetRpcIdentifier();
609 return vector<string>(1, id);
610 }
611 return vector<string>();
Chris Masone4e851612011-07-01 10:46:53 -0700612}
613
614string Device::GetRpcConnectionIdentifier() {
615 return adaptor_->GetRpcConnectionIdentifier();
616}
617
Eric Shienbrood9a245532012-03-07 14:20:39 -0500618// callback
619void Device::OnEnabledStateChanged(const ResultCallback &callback,
620 const Error &error) {
621 VLOG(2) << __func__ << "(" << enabled_pending_ << ")";
622 if (error.IsSuccess()) {
623 enabled_ = enabled_pending_;
624 manager_->UpdateEnabledTechnologies();
625 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
626 adaptor_->UpdateEnabled();
627 }
628 if (!callback.is_null())
629 callback.Run(error);
630}
631
632void Device::SetEnabled(bool enable) {
633 VLOG(2) << __func__ << "(" << enable << ")";
634 SetEnabledInternal(enable, false, NULL, ResultCallback());
635}
636
637void Device::SetEnabledPersistent(bool enable,
638 Error *error,
639 const ResultCallback &callback) {
640 SetEnabledInternal(enable, true, error, callback);
641}
642
643void Device::SetEnabledInternal(bool enable,
644 bool persist,
645 Error *error,
646 const ResultCallback &callback) {
647 VLOG(2) << "Device " << link_name_ << " "
648 << (enable ? "starting" : "stopping");
649 if (enable == enabled_) {
650 if (error)
651 error->Reset();
652 return;
653 }
654
655 if (enabled_pending_ == enable) {
656 if (error)
657 error->Populate(Error::kInProgress,
658 "Enable operation already in progress");
659 return;
660 }
661
662 if (persist) {
663 enabled_persistent_ = enable;
664 manager_->SaveActiveProfile();
665 }
666
667 enabled_pending_ = enable;
668 EnabledStateChangedCallback enabled_callback =
669 Bind(&Device::OnEnabledStateChanged,
670 weak_ptr_factory_.GetWeakPtr(), callback);
671 if (enable) {
672 running_ = true;
673 routing_table_->FlushRoutes(interface_index());
674 Start(error, enabled_callback);
675 } else {
676 running_ = false;
677 DestroyIPConfig(); // breaks a reference cycle
678 SelectService(NULL); // breaks a reference cycle
679 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
680 VLOG(3) << "Device " << link_name_ << " ipconfig_ "
681 << (ipconfig_ ? "is set." : "is not set.");
682 VLOG(3) << "Device " << link_name_ << " connection_ "
683 << (connection_ ? "is set." : "is not set.");
684 VLOG(3) << "Device " << link_name_ << " selected_service_ "
685 << (selected_service_ ? "is set." : "is not set.");
686 Stop(error, enabled_callback);
687 }
688}
689
Paul Stewart75897df2011-04-27 09:05:53 -0700690} // namespace shill