blob: 5ade55d20c8e6869e5e8c2bb41081cf3e67c09fc [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"
Paul Stewartc39f1132011-06-22 12:02:28 -070034#include "shill/rtnl_handler.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070035#include "shill/scope_logger.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()),
100 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700101 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -0700102
103 // flimflam::kBgscanMethodProperty: Registered in WiFi
104 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
105 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
106
107 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
108 // flimflam::kCarrierProperty: Registered in Cellular
109 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -0700110 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700111 // flimflam::kImeiProperty: Registered in Cellular
112 // flimflam::kImsiProperty: Registered in Cellular
113 // flimflam::kManufacturerProperty: Registered in Cellular
114 // flimflam::kMdnProperty: Registered in Cellular
115 // flimflam::kMeidProperty: Registered in Cellular
116 // flimflam::kMinProperty: Registered in Cellular
117 // flimflam::kModelIDProperty: Registered in Cellular
118 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
119 // flimflam::kHardwareRevisionProperty: Registered in Cellular
120 // flimflam::kPRLVersionProperty: Registered in Cellular
121 // flimflam::kSIMLockStatusProperty: Registered in Cellular
122 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -0700123 // flimflam::kDBusConnectionProperty: Registered in Cellular
124 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700125
Jason Glasgowe8334fd2012-03-30 16:02:37 -0400126 store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
Jason Glasgow08afdff2012-04-03 10:22:26 -0400127 HelpRegisterConstDerivedRpcIdentifiers(flimflam::kIPConfigsProperty,
128 &Device::AvailableIPConfigs);
Chris Masone27c4aa52011-07-02 13:10:14 -0700129 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500130 store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_);
Jason Glasgowb5790052012-01-27 01:03:52 -0500131 HelpRegisterDerivedString(flimflam::kTypeProperty,
132 &Device::GetTechnologyString,
133 NULL);
134
Chris Masoneb925cc82011-06-22 15:39:57 -0700135 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700136 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700137
Chris Masone4e851612011-07-01 10:46:53 -0700138 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700139 // known_properties_.push_back(flimflam::kNetworksProperty);
140
Chris Masone4d42df82011-07-02 17:09:39 -0700141 // flimflam::kScanningProperty: Registered in WiFi, Cellular
142 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
143
144 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Ben Chanfad4a0b2012-04-18 15:49:59 -0700145 SLOG(Device, 2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -0700146}
147
148Device::~Device() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700149 SLOG(Device, 2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700150}
151
mukesh agrawal1830fa12011-09-26 14:31:40 -0700152bool Device::TechnologyIs(const Technology::Identifier /*type*/) const {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700153 return false;
154}
155
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700156void Device::LinkEvent(unsigned flags, unsigned change) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700157 SLOG(Device, 2) << "Device " << link_name_
158 << std::showbase << std::hex
159 << " flags " << flags << " changed " << change
160 << std::dec << std::noshowbase;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700161}
162
Darin Petkovc0865312011-09-16 15:31:20 -0700163void Device::Scan(Error *error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700164 SLOG(Device, 2) << "Device " << link_name_ << " scan requested.";
Paul Stewartbe005172011-11-02 18:10:29 -0700165 Error::PopulateAndLog(error, Error::kNotSupported,
166 "Device doesn't support scan.");
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700167}
168
Eric Shienbrood9a245532012-03-07 14:20:39 -0500169void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error,
170 const ResultCallback &/*callback*/) {
171 Error::PopulateAndLog(error, Error::kNotSupported,
Paul Stewartbe005172011-11-02 18:10:29 -0700172 "Device doesn't support network registration.");
Darin Petkov9ae310f2011-08-30 15:41:13 -0700173}
174
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100175void Device::RequirePIN(
Eric Shienbrood9a245532012-03-07 14:20:39 -0500176 const string &/*pin*/, bool /*require*/,
177 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700178 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500179 Error::PopulateAndLog(error, Error::kNotSupported,
180 "Device doesn't support RequirePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700181}
182
Eric Shienbrood9a245532012-03-07 14:20:39 -0500183void Device::EnterPIN(const string &/*pin*/,
184 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700185 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500186 Error::PopulateAndLog(error, Error::kNotSupported,
187 "Device doesn't support EnterPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700188}
189
mukesh agrawal1830fa12011-09-26 14:31:40 -0700190void Device::UnblockPIN(const string &/*unblock_code*/,
191 const string &/*pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500192 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700193 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500194 Error::PopulateAndLog(error, Error::kNotSupported,
195 "Device doesn't support UnblockPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700196}
197
mukesh agrawal1830fa12011-09-26 14:31:40 -0700198void Device::ChangePIN(const string &/*old_pin*/,
199 const string &/*new_pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500200 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700201 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500202 Error::PopulateAndLog(error, Error::kNotSupported,
203 "Device doesn't support ChangePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700204}
205
Paul Stewart2bf1d352011-12-06 15:02:55 -0800206void Device::DisableIPv6() {
207 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
208}
209
210void Device::EnableIPv6() {
211 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
212}
213
214void Device::EnableIPv6Privacy() {
215 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
216 kIPFlagUseTempAddrUsedAndDefault);
217}
218
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800219void Device::DisableReversePathFilter() {
220 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
221 // for IPv6. crosbug.com/24228
222 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
223 kIPFlagReversePathFilterLooseMode);
224}
225
226void Device::EnableReversePathFilter() {
227 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
228 kIPFlagReversePathFilterEnabled);
229}
230
Gaurav Shah435de2c2011-11-17 19:01:07 -0800231bool Device::IsConnected() const {
232 if (selected_service_)
233 return selected_service_->IsConnected();
234 return false;
235}
236
Paul Stewartd215af62012-04-24 23:25:50 -0700237bool Device::IsConnectedToService(const ServiceRefPtr &service) const {
238 return service == selected_service_ && IsConnected();
239}
240
Chris Masone27c4aa52011-07-02 13:10:14 -0700241string Device::GetRpcIdentifier() {
242 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700243}
244
Chris Masone5dec5f42011-07-22 14:07:55 -0700245string Device::GetStorageIdentifier() {
246 string id = GetRpcIdentifier();
247 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700248 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700249 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700250 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700251 return id;
252}
253
Jason Glasgowb5790052012-01-27 01:03:52 -0500254string Device::GetTechnologyString(Error */*error*/) {
255 return Technology::NameFromIdentifier(technology());
256}
257
Chris Masone19e30402011-07-19 15:48:47 -0700258const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700259 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700260}
261
Chris Masone19e30402011-07-19 15:48:47 -0700262const string& Device::UniqueName() const {
263 return unique_id_;
264}
265
Chris Masone5dec5f42011-07-22 14:07:55 -0700266bool Device::Load(StoreInterface *storage) {
267 const string id = GetStorageIdentifier();
268 if (!storage->ContainsGroup(id)) {
269 LOG(WARNING) << "Device is not available in the persistent store: " << id;
270 return false;
271 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500272 enabled_persistent_ = true;
273 storage->GetBool(id, kStoragePowered, &enabled_persistent_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700274 // TODO(cmasone): What does it mean to load an IPConfig identifier??
275 return true;
276}
277
278bool Device::Save(StoreInterface *storage) {
279 const string id = GetStorageIdentifier();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500280 storage->SetBool(id, kStoragePowered, enabled_persistent_);
Chris Masone34af2182011-08-22 11:59:36 -0700281 if (ipconfig_.get()) {
282 // The _0 is an index into the list of IPConfigs that this device might
283 // have. We only have one IPConfig right now, and I hope to never have
284 // to support more, as sleffler indicates that associating IPConfigs
285 // with devices is wrong and due to be changed in flimflam anyhow.
286 string suffix = hardware_address_ + "_0";
287 ipconfig_->Save(storage, suffix);
288 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
289 }
Chris Masone5dec5f42011-07-22 14:07:55 -0700290 return true;
291}
292
Darin Petkovafa6fc42011-06-21 16:21:08 -0700293void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800294 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700295 if (ipconfig_.get()) {
296 ipconfig_->ReleaseIP();
297 ipconfig_ = NULL;
298 }
Paul Stewarte6132022011-08-16 09:11:02 -0700299 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700300}
301
Paul Stewart2bf1d352011-12-06 15:02:55 -0800302bool Device::AcquireIPConfig() {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700303 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800304 EnableIPv6();
Paul Stewartd32f4842012-01-11 16:08:13 -0800305 ipconfig_ = dhcp_provider_->CreateConfig(link_name_, manager_->GetHostName());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500306 ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated,
307 weak_ptr_factory_.GetWeakPtr()));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700308 dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask,
309 weak_ptr_factory_.GetWeakPtr()));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700310 return ipconfig_->RequestIP();
311}
312
Jason Glasgowb5790052012-01-27 01:03:52 -0500313void Device::HelpRegisterDerivedString(
314 const string &name,
315 string(Device::*get)(Error *error),
316 void(Device::*set)(const string &value, Error *error)) {
317 store_.RegisterDerivedString(
318 name,
319 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
320}
321
mukesh agrawalffa3d042011-10-06 15:26:10 -0700322void Device::HelpRegisterDerivedStrings(
323 const string &name,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500324 Strings(Device::*get)(Error *error),
325 void(Device::*set)(const Strings &value, Error *error)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700326 store_.RegisterDerivedStrings(
327 name,
328 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700329}
330
Jason Glasgow08afdff2012-04-03 10:22:26 -0400331void Device::HelpRegisterConstDerivedRpcIdentifiers(
332 const string &name,
333 RpcIdentifiers(Device::*get)(Error *)) {
334 store_.RegisterDerivedRpcIdentifiers(
335 name,
336 RpcIdentifiersAccessor(
337 new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL)));
338}
339
Paul Stewart1062d9d2012-04-27 10:42:27 -0700340void Device::ConfigureStaticIPTask() {
341 SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get()
342 << " ipconfig " << ipconfig_.get();
343
344 if (!selected_service_ || !ipconfig_) {
345 return;
346 }
347
348 const StaticIPParameters &static_ip_parameters =
349 selected_service_->static_ip_parameters();
350 if (static_ip_parameters.ContainsAddress()) {
351 SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters.";
352 // If the parameters contain an IP address, apply them now and bring
353 // the interface up. When DHCP information arrives, it will supplement
354 // the static information.
355 OnIPConfigUpdated(ipconfig_, true);
356 } else {
357 SLOG(Device, 2) << __func__ << " " << " no static IP address.";
358 }
359}
360
Darin Petkov79d74c92012-03-07 17:20:32 +0100361void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700362 SLOG(Device, 2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700363 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700364 CreateConnection();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700365 if (selected_service_) {
366 ipconfig->ApplyStaticIPParameters(
367 selected_service_->static_ip_parameters());
368 }
Paul Stewarte6132022011-08-16 09:11:02 -0700369 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800370 // SetConnection must occur after the UpdateFromIPConfig so the
371 // service can use the values derived from the connection.
Paul Stewart1062d9d2012-04-27 10:42:27 -0700372 if (selected_service_) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800373 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800374 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800375 // The service state change needs to happen last, so that at the
376 // time we report the state change to the manager, the service
377 // has its connection.
378 SetServiceState(Service::kStateConnected);
Thieu Le85e050b2012-03-13 15:04:38 -0700379 portal_attempts_to_online_ = 0;
Paul Stewart20088d82012-02-16 06:58:55 -0800380 // Subtle: Start portal detection after transitioning the service
381 // to the Connected state because this call may immediately transition
382 // to the Online state.
383 StartPortalDetection();
Paul Stewarte6132022011-08-16 09:11:02 -0700384 } else {
385 // TODO(pstew): This logic gets more complex when multiple IPConfig types
386 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700387 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700388 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700389 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700390}
391
Paul Stewarte6132022011-08-16 09:11:02 -0700392void Device::CreateConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700393 SLOG(Device, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -0700394 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700395 connection_ = new Connection(interface_index_,
396 link_name_,
Paul Stewarte00600e2012-03-16 07:08:00 -0700397 technology_,
Paul Stewart9a908082011-08-31 12:18:48 -0700398 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700399 }
400}
401
402void Device::DestroyConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700403 SLOG(Device, 2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800404 StopPortalDetection();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800405 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800406 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800407 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800408 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700409}
410
Paul Stewart03dba0b2011-08-22 16:32:45 -0700411void Device::SelectService(const ServiceRefPtr &service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700412 SLOG(Device, 2) << __func__ << ": "
413 << (service.get() ?
414 StringPrintf("%s (%s)",
415 service->UniqueName().c_str(),
416 service->friendly_name().c_str()) :
417 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000418
419 if (selected_service_.get() == service.get()) {
420 // No change to |selected_service_|. Return early to avoid
421 // changing its state.
422 return;
423 }
424
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800425 if (selected_service_.get()) {
426 if (selected_service_->state() != Service::kStateFailure) {
427 selected_service_->SetState(Service::kStateIdle);
428 }
429 // TODO(pstew): We need to revisit the model here: should the Device
430 // subclass be responsible for calling DestroyIPConfig() (which would
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800431 // trigger DestroyConnection() and Service::SetConnection(NULL))?
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800432 // Ethernet does, but WiFi currently does not. crosbug.com/23929
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800433 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700434 }
435 selected_service_ = service;
436}
437
438void Device::SetServiceState(Service::ConnectState state) {
439 if (selected_service_.get()) {
440 selected_service_->SetState(state);
441 }
442}
443
444void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
445 if (selected_service_.get()) {
446 selected_service_->SetFailure(failure_state);
447 }
448}
449
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400450void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) {
451 if (selected_service_.get()) {
452 selected_service_->SetFailureSilent(failure_state);
453 }
454}
455
Chris Masone34af2182011-08-22 11:59:36 -0700456string Device::SerializeIPConfigs(const string &suffix) {
457 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700458}
459
Paul Stewart2bf1d352011-12-06 15:02:55 -0800460bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
461 const string &value) {
462 string ip_version;
463 if (family == IPAddress::kFamilyIPv4) {
464 ip_version = kIPFlagVersion4;
465 } else if (family == IPAddress::kFamilyIPv6) {
466 ip_version = kIPFlagVersion6;
467 } else {
468 NOTIMPLEMENTED();
469 }
470 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
471 link_name_.c_str(), flag.c_str()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700472 SLOG(Device, 2) << "Writing " << value << " to flag file "
473 << flag_file.value();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800474 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
475 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
476 value.c_str(), flag_file.value().c_str());
477 return false;
478 }
479 return true;
480}
481
Paul Stewartd215af62012-04-24 23:25:50 -0700482bool Device::RestartPortalDetection() {
483 StopPortalDetection();
484 return StartPortalDetection();
485}
486
Paul Stewartc681fa02012-03-02 19:40:04 -0800487bool Device::RequestPortalDetection() {
488 if (!selected_service_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700489 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800490 << ": No selected service, so no need for portal check.";
491 return false;
492 }
493
494 if (!connection_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700495 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800496 << ": No connection, so no need for portal check.";
497 return false;
498 }
499
500 if (selected_service_->state() != Service::kStatePortal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700501 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800502 << ": Service is not in portal state. No need to start check.";
503 return false;
504 }
505
506 if (!connection_->is_default()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700507 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800508 << ": Service is not the default connection. Don't start check.";
509 return false;
510 }
511
512 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700513 SLOG(Device, 2) << FriendlyName()
514 << ": Portal detection is already running.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800515 return true;
516 }
517
518 return StartPortalDetection();
519}
520
Paul Stewart20088d82012-02-16 06:58:55 -0800521bool Device::StartPortalDetection() {
Paul Stewartd215af62012-04-24 23:25:50 -0700522 DCHECK(selected_service_.get());
523 if (selected_service_->IsPortalDetectionDisabled()) {
524 SLOG(Device, 2) << "Service " << selected_service_->friendly_name()
525 << ": Portal detection is disabled; "
526 << "marking service online.";
527 SetServiceConnectedState(Service::kStateOnline);
528 return false;
529 }
530
531 if (selected_service_->IsPortalDetectionAuto() &&
532 !manager_->IsPortalDetectionEnabled(technology())) {
Paul Stewart20088d82012-02-16 06:58:55 -0800533 // If portal detection is disabled for this technology, immediately set
534 // the service state to "Online".
Ben Chanfad4a0b2012-04-18 15:49:59 -0700535 SLOG(Device, 2) << "Device " << FriendlyName()
536 << ": Portal detection is disabled; "
537 << "marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800538 SetServiceConnectedState(Service::kStateOnline);
539 return false;
540 }
541
Paul Stewart20088d82012-02-16 06:58:55 -0800542 if (selected_service_->HasProxyConfig()) {
543 // Services with HTTP proxy configurations should not be checked by the
544 // connection manager, since we don't have the ability to evaluate
545 // arbitrary proxy configs and their possible credentials.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700546 SLOG(Device, 2) << "Device " << FriendlyName()
547 << ": Service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800548 SetServiceConnectedState(Service::kStateOnline);
549 return false;
550 }
551
552 portal_detector_.reset(new PortalDetector(connection_,
553 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500554 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800555 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
556 LOG(ERROR) << "Device " << FriendlyName()
557 << ": Portal detection failed to start: likely bad URL: "
558 << manager_->GetPortalCheckURL();
559 SetServiceConnectedState(Service::kStateOnline);
560 return false;
561 }
562
Ben Chanfad4a0b2012-04-18 15:49:59 -0700563 SLOG(Device, 2) << "Device " << FriendlyName()
564 << ": Portal detection has started.";
Paul Stewart20088d82012-02-16 06:58:55 -0800565 return true;
566}
567
568void Device::StopPortalDetection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700569 SLOG(Device, 2) << "Device " << FriendlyName()
570 << ": Portal detection has stopped.";
Paul Stewart20088d82012-02-16 06:58:55 -0800571 portal_detector_.reset();
572}
573
574void Device::SetServiceConnectedState(Service::ConnectState state) {
575 DCHECK(selected_service_.get());
576
577 if (!selected_service_.get()) {
578 LOG(ERROR) << FriendlyName() << ": "
579 << "Portal detection completed but no selected service exists!";
580 return;
581 }
582
583 if (!selected_service_->IsConnected()) {
584 LOG(ERROR) << FriendlyName() << ": "
585 << "Portal detection completed but selected service "
586 << selected_service_->UniqueName()
587 << " is in non-connected state.";
588 return;
589 }
590
Paul Stewartc681fa02012-03-02 19:40:04 -0800591 if (state == Service::kStatePortal && connection_->is_default() &&
592 manager_->GetPortalCheckInterval() != 0) {
593 CHECK(portal_detector_.get());
594 if (!portal_detector_->StartAfterDelay(
595 manager_->GetPortalCheckURL(),
596 manager_->GetPortalCheckInterval())) {
597 LOG(ERROR) << "Device " << FriendlyName()
598 << ": Portal detection failed to restart: likely bad URL: "
599 << manager_->GetPortalCheckURL();
600 SetServiceState(Service::kStateOnline);
601 portal_detector_.reset();
602 return;
603 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700604 SLOG(Device, 2) << "Device " << FriendlyName()
605 << ": Portal detection retrying.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800606 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700607 SLOG(Device, 2) << "Device " << FriendlyName()
608 << ": Portal will not retry.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800609 portal_detector_.reset();
610 }
611
Paul Stewart20088d82012-02-16 06:58:55 -0800612 SetServiceState(state);
613}
614
615void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
616 if (!result.final) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700617 SLOG(Device, 2) << "Device " << FriendlyName()
618 << ": Received non-final status: "
619 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800620 return;
621 }
622
Ben Chanfad4a0b2012-04-18 15:49:59 -0700623 SLOG(Device, 2) << "Device " << FriendlyName()
624 << ": Received final status: "
625 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800626
Thieu Le85e050b2012-03-13 15:04:38 -0700627 portal_attempts_to_online_ += result.num_attempts;
628
629 metrics()->SendEnumToUMA(
630 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
631 Metrics::PortalDetectionResultToEnum(result),
632 Metrics::kPortalResultMax);
633
Paul Stewart20088d82012-02-16 06:58:55 -0800634 if (result.status == PortalDetector::kStatusSuccess) {
635 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700636
637 metrics()->SendToUMA(
638 metrics()->GetFullMetricName(
639 Metrics::kMetricPortalAttemptsToOnline, technology()),
640 portal_attempts_to_online_,
641 Metrics::kMetricPortalAttemptsToOnlineMin,
642 Metrics::kMetricPortalAttemptsToOnlineMax,
643 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800644 } else {
645 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700646
647 metrics()->SendToUMA(
648 metrics()->GetFullMetricName(
649 Metrics::kMetricPortalAttempts, technology()),
650 result.num_attempts,
651 Metrics::kMetricPortalAttemptsMin,
652 Metrics::kMetricPortalAttemptsMax,
653 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800654 }
655}
656
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800657vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Jason Glasgow08afdff2012-04-03 10:22:26 -0400658 if (ipconfig_.get()) {
659 string id = ipconfig_->GetRpcIdentifier();
660 return vector<string>(1, id);
661 }
662 return vector<string>();
Chris Masone4e851612011-07-01 10:46:53 -0700663}
664
665string Device::GetRpcConnectionIdentifier() {
666 return adaptor_->GetRpcConnectionIdentifier();
667}
668
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400669bool Device::IsUnderlyingDeviceEnabled() const {
670 return false;
671}
672
Eric Shienbrood9a245532012-03-07 14:20:39 -0500673// callback
674void Device::OnEnabledStateChanged(const ResultCallback &callback,
675 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700676 SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500677 if (error.IsSuccess()) {
678 enabled_ = enabled_pending_;
679 manager_->UpdateEnabledTechnologies();
680 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
681 adaptor_->UpdateEnabled();
682 }
Gary Morainbaeefdf2012-04-30 14:53:35 -0700683 enabled_pending_ = enabled_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500684 if (!callback.is_null())
685 callback.Run(error);
686}
687
688void Device::SetEnabled(bool enable) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700689 SLOG(Device, 2) << __func__ << "(" << enable << ")";
Jason Glasgow4a490792012-04-10 15:02:05 -0400690 Error error;
691 SetEnabledInternal(enable, false, &error, ResultCallback());
692 LOG_IF(ERROR, error.IsFailure())
693 << "Enabled failed, but no way to report the failure.";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500694}
695
696void Device::SetEnabledPersistent(bool enable,
697 Error *error,
698 const ResultCallback &callback) {
699 SetEnabledInternal(enable, true, error, callback);
700}
701
702void Device::SetEnabledInternal(bool enable,
703 bool persist,
704 Error *error,
705 const ResultCallback &callback) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400706 DCHECK(error);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700707 SLOG(Device, 2) << "Device " << link_name_ << " "
708 << (enable ? "starting" : "stopping");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500709 if (enable == enabled_) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400710 error->Reset();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500711 return;
712 }
713
714 if (enabled_pending_ == enable) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400715 Error::PopulateAndLog(error, Error::kInProgress,
716 "Enable operation already in progress");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500717 return;
718 }
719
720 if (persist) {
721 enabled_persistent_ = enable;
722 manager_->SaveActiveProfile();
723 }
724
725 enabled_pending_ = enable;
726 EnabledStateChangedCallback enabled_callback =
727 Bind(&Device::OnEnabledStateChanged,
728 weak_ptr_factory_.GetWeakPtr(), callback);
729 if (enable) {
730 running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500731 Start(error, enabled_callback);
732 } else {
733 running_ = false;
734 DestroyIPConfig(); // breaks a reference cycle
735 SelectService(NULL); // breaks a reference cycle
736 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700737 SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ "
738 << (ipconfig_ ? "is set." : "is not set.");
739 SLOG(Device, 3) << "Device " << link_name_ << " connection_ "
740 << (connection_ ? "is set." : "is not set.");
741 SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ "
742 << (selected_service_ ? "is set." : "is not set.");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500743 Stop(error, enabled_callback);
744 }
745}
746
Paul Stewart75897df2011-04-27 09:05:53 -0700747} // namespace shill