blob: ea7c5c5f5ffcac3a781becbef0e23da05b9ef80b [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() {
Paul Stewartd408fdf2012-05-07 17:15:57 -0700303 return AcquireIPConfigWithLeaseName(string());
304}
305
306bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700307 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800308 EnableIPv6();
Paul Stewartd408fdf2012-05-07 17:15:57 -0700309 ipconfig_ = dhcp_provider_->CreateConfig(link_name_,
310 manager_->GetHostName(),
311 lease_name,
312 manager_->GetArpGateway());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500313 ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated,
314 weak_ptr_factory_.GetWeakPtr()));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700315 dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask,
316 weak_ptr_factory_.GetWeakPtr()));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700317 return ipconfig_->RequestIP();
318}
319
Jason Glasgowb5790052012-01-27 01:03:52 -0500320void Device::HelpRegisterDerivedString(
321 const string &name,
322 string(Device::*get)(Error *error),
323 void(Device::*set)(const string &value, Error *error)) {
324 store_.RegisterDerivedString(
325 name,
326 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
327}
328
mukesh agrawalffa3d042011-10-06 15:26:10 -0700329void Device::HelpRegisterDerivedStrings(
330 const string &name,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500331 Strings(Device::*get)(Error *error),
332 void(Device::*set)(const Strings &value, Error *error)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700333 store_.RegisterDerivedStrings(
334 name,
335 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700336}
337
Jason Glasgow08afdff2012-04-03 10:22:26 -0400338void Device::HelpRegisterConstDerivedRpcIdentifiers(
339 const string &name,
340 RpcIdentifiers(Device::*get)(Error *)) {
341 store_.RegisterDerivedRpcIdentifiers(
342 name,
343 RpcIdentifiersAccessor(
344 new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL)));
345}
346
Paul Stewart1062d9d2012-04-27 10:42:27 -0700347void Device::ConfigureStaticIPTask() {
348 SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get()
349 << " ipconfig " << ipconfig_.get();
350
351 if (!selected_service_ || !ipconfig_) {
352 return;
353 }
354
355 const StaticIPParameters &static_ip_parameters =
356 selected_service_->static_ip_parameters();
357 if (static_ip_parameters.ContainsAddress()) {
358 SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters.";
359 // If the parameters contain an IP address, apply them now and bring
360 // the interface up. When DHCP information arrives, it will supplement
361 // the static information.
362 OnIPConfigUpdated(ipconfig_, true);
363 } else {
364 SLOG(Device, 2) << __func__ << " " << " no static IP address.";
365 }
366}
367
Darin Petkov79d74c92012-03-07 17:20:32 +0100368void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700369 SLOG(Device, 2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700370 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700371 CreateConnection();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700372 if (selected_service_) {
373 ipconfig->ApplyStaticIPParameters(
374 selected_service_->static_ip_parameters());
375 }
Paul Stewarte6132022011-08-16 09:11:02 -0700376 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800377 // SetConnection must occur after the UpdateFromIPConfig so the
378 // service can use the values derived from the connection.
Paul Stewart1062d9d2012-04-27 10:42:27 -0700379 if (selected_service_) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800380 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800381 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800382 // The service state change needs to happen last, so that at the
383 // time we report the state change to the manager, the service
384 // has its connection.
385 SetServiceState(Service::kStateConnected);
Thieu Le85e050b2012-03-13 15:04:38 -0700386 portal_attempts_to_online_ = 0;
Paul Stewart20088d82012-02-16 06:58:55 -0800387 // Subtle: Start portal detection after transitioning the service
388 // to the Connected state because this call may immediately transition
389 // to the Online state.
390 StartPortalDetection();
Paul Stewarte6132022011-08-16 09:11:02 -0700391 } else {
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700392 // TODO(pstew): This logic gets yet more complex when multiple
393 // IPConfig types are run in parallel (e.g. DHCP and DHCP6)
394 if (selected_service_ &&
395 selected_service_->static_ip_parameters().ContainsAddress()) {
396 // Consider three cases:
397 //
398 // 1. We're here because DHCP failed while starting up. There
399 // are two subcases:
400 // a. DHCP has failed, and Static IP config has _not yet_
401 // completed. It's fine to do nothing, because we'll
402 // apply the static config shortly.
403 // b. DHCP has failed, and Static IP config has _already_
404 // completed. It's fine to do nothing, because we can
405 // continue to use the static config that's already
406 // been applied.
407 //
408 // 2. We're here because a previously valid DHCP configuration
409 // is no longer valid. There's still a static IP config,
410 // because the condition in the if clause evaluated to true.
411 // Furthermore, the static config includes an IP address for
412 // us to use.
413 //
414 // The current configuration may include some DHCP
415 // parameters, overriden by any static parameters
416 // provided. We continue to use this configuration, because
417 // the only configuration element that is leased to us (IP
418 // address) will be overriden by a static parameter.
419 return;
420 }
421
Paul Stewart03dba0b2011-08-22 16:32:45 -0700422 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700423 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700424 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700425}
426
Paul Stewarte6132022011-08-16 09:11:02 -0700427void Device::CreateConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700428 SLOG(Device, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -0700429 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700430 connection_ = new Connection(interface_index_,
431 link_name_,
Paul Stewarte00600e2012-03-16 07:08:00 -0700432 technology_,
Paul Stewart9a908082011-08-31 12:18:48 -0700433 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700434 }
435}
436
437void Device::DestroyConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700438 SLOG(Device, 2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800439 StopPortalDetection();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800440 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800441 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800442 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800443 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700444}
445
Paul Stewart03dba0b2011-08-22 16:32:45 -0700446void Device::SelectService(const ServiceRefPtr &service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700447 SLOG(Device, 2) << __func__ << ": "
448 << (service.get() ?
449 StringPrintf("%s (%s)",
450 service->UniqueName().c_str(),
451 service->friendly_name().c_str()) :
452 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000453
454 if (selected_service_.get() == service.get()) {
455 // No change to |selected_service_|. Return early to avoid
456 // changing its state.
457 return;
458 }
459
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800460 if (selected_service_.get()) {
461 if (selected_service_->state() != Service::kStateFailure) {
462 selected_service_->SetState(Service::kStateIdle);
463 }
Paul Stewart20b0a092012-05-22 20:39:57 -0700464 // Just in case the Device subclass has not already done so, make
465 // sure the previously selected service has its connection removed.
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800466 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700467 }
468 selected_service_ = service;
469}
470
471void Device::SetServiceState(Service::ConnectState state) {
472 if (selected_service_.get()) {
473 selected_service_->SetState(state);
474 }
475}
476
477void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
478 if (selected_service_.get()) {
479 selected_service_->SetFailure(failure_state);
480 }
481}
482
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400483void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) {
484 if (selected_service_.get()) {
485 selected_service_->SetFailureSilent(failure_state);
486 }
487}
488
Chris Masone34af2182011-08-22 11:59:36 -0700489string Device::SerializeIPConfigs(const string &suffix) {
490 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700491}
492
Paul Stewart2bf1d352011-12-06 15:02:55 -0800493bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
494 const string &value) {
495 string ip_version;
496 if (family == IPAddress::kFamilyIPv4) {
497 ip_version = kIPFlagVersion4;
498 } else if (family == IPAddress::kFamilyIPv6) {
499 ip_version = kIPFlagVersion6;
500 } else {
501 NOTIMPLEMENTED();
502 }
503 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
504 link_name_.c_str(), flag.c_str()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700505 SLOG(Device, 2) << "Writing " << value << " to flag file "
506 << flag_file.value();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800507 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
508 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
509 value.c_str(), flag_file.value().c_str());
510 return false;
511 }
512 return true;
513}
514
Paul Stewartd215af62012-04-24 23:25:50 -0700515bool Device::RestartPortalDetection() {
516 StopPortalDetection();
517 return StartPortalDetection();
518}
519
Paul Stewartc681fa02012-03-02 19:40:04 -0800520bool Device::RequestPortalDetection() {
521 if (!selected_service_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700522 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800523 << ": No selected service, so no need for portal check.";
524 return false;
525 }
526
527 if (!connection_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700528 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800529 << ": No connection, so no need for portal check.";
530 return false;
531 }
532
533 if (selected_service_->state() != Service::kStatePortal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700534 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800535 << ": Service is not in portal state. No need to start check.";
536 return false;
537 }
538
539 if (!connection_->is_default()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700540 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800541 << ": Service is not the default connection. Don't start check.";
542 return false;
543 }
544
545 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700546 SLOG(Device, 2) << FriendlyName()
547 << ": Portal detection is already running.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800548 return true;
549 }
550
551 return StartPortalDetection();
552}
553
Paul Stewart20088d82012-02-16 06:58:55 -0800554bool Device::StartPortalDetection() {
Paul Stewartd215af62012-04-24 23:25:50 -0700555 DCHECK(selected_service_.get());
556 if (selected_service_->IsPortalDetectionDisabled()) {
557 SLOG(Device, 2) << "Service " << selected_service_->friendly_name()
558 << ": Portal detection is disabled; "
559 << "marking service online.";
560 SetServiceConnectedState(Service::kStateOnline);
561 return false;
562 }
563
564 if (selected_service_->IsPortalDetectionAuto() &&
565 !manager_->IsPortalDetectionEnabled(technology())) {
Paul Stewart20088d82012-02-16 06:58:55 -0800566 // If portal detection is disabled for this technology, immediately set
567 // the service state to "Online".
Ben Chanfad4a0b2012-04-18 15:49:59 -0700568 SLOG(Device, 2) << "Device " << FriendlyName()
569 << ": Portal detection is disabled; "
570 << "marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800571 SetServiceConnectedState(Service::kStateOnline);
572 return false;
573 }
574
Paul Stewart20088d82012-02-16 06:58:55 -0800575 if (selected_service_->HasProxyConfig()) {
576 // Services with HTTP proxy configurations should not be checked by the
577 // connection manager, since we don't have the ability to evaluate
578 // arbitrary proxy configs and their possible credentials.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700579 SLOG(Device, 2) << "Device " << FriendlyName()
580 << ": Service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800581 SetServiceConnectedState(Service::kStateOnline);
582 return false;
583 }
584
585 portal_detector_.reset(new PortalDetector(connection_,
586 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500587 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800588 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
589 LOG(ERROR) << "Device " << FriendlyName()
590 << ": Portal detection failed to start: likely bad URL: "
591 << manager_->GetPortalCheckURL();
592 SetServiceConnectedState(Service::kStateOnline);
593 return false;
594 }
595
Ben Chanfad4a0b2012-04-18 15:49:59 -0700596 SLOG(Device, 2) << "Device " << FriendlyName()
597 << ": Portal detection has started.";
Paul Stewart20088d82012-02-16 06:58:55 -0800598 return true;
599}
600
601void Device::StopPortalDetection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700602 SLOG(Device, 2) << "Device " << FriendlyName()
603 << ": Portal detection has stopped.";
Paul Stewart20088d82012-02-16 06:58:55 -0800604 portal_detector_.reset();
605}
606
607void Device::SetServiceConnectedState(Service::ConnectState state) {
608 DCHECK(selected_service_.get());
609
610 if (!selected_service_.get()) {
611 LOG(ERROR) << FriendlyName() << ": "
612 << "Portal detection completed but no selected service exists!";
613 return;
614 }
615
616 if (!selected_service_->IsConnected()) {
617 LOG(ERROR) << FriendlyName() << ": "
618 << "Portal detection completed but selected service "
619 << selected_service_->UniqueName()
620 << " is in non-connected state.";
621 return;
622 }
623
Paul Stewartc681fa02012-03-02 19:40:04 -0800624 if (state == Service::kStatePortal && connection_->is_default() &&
625 manager_->GetPortalCheckInterval() != 0) {
626 CHECK(portal_detector_.get());
627 if (!portal_detector_->StartAfterDelay(
628 manager_->GetPortalCheckURL(),
629 manager_->GetPortalCheckInterval())) {
630 LOG(ERROR) << "Device " << FriendlyName()
631 << ": Portal detection failed to restart: likely bad URL: "
632 << manager_->GetPortalCheckURL();
633 SetServiceState(Service::kStateOnline);
634 portal_detector_.reset();
635 return;
636 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700637 SLOG(Device, 2) << "Device " << FriendlyName()
638 << ": Portal detection retrying.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800639 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700640 SLOG(Device, 2) << "Device " << FriendlyName()
641 << ": Portal will not retry.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800642 portal_detector_.reset();
643 }
644
Paul Stewart20088d82012-02-16 06:58:55 -0800645 SetServiceState(state);
646}
647
648void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
649 if (!result.final) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700650 SLOG(Device, 2) << "Device " << FriendlyName()
651 << ": Received non-final status: "
652 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800653 return;
654 }
655
Ben Chanfad4a0b2012-04-18 15:49:59 -0700656 SLOG(Device, 2) << "Device " << FriendlyName()
657 << ": Received final status: "
658 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800659
Thieu Le85e050b2012-03-13 15:04:38 -0700660 portal_attempts_to_online_ += result.num_attempts;
661
662 metrics()->SendEnumToUMA(
663 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
664 Metrics::PortalDetectionResultToEnum(result),
665 Metrics::kPortalResultMax);
666
Paul Stewart20088d82012-02-16 06:58:55 -0800667 if (result.status == PortalDetector::kStatusSuccess) {
668 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700669
670 metrics()->SendToUMA(
671 metrics()->GetFullMetricName(
672 Metrics::kMetricPortalAttemptsToOnline, technology()),
673 portal_attempts_to_online_,
674 Metrics::kMetricPortalAttemptsToOnlineMin,
675 Metrics::kMetricPortalAttemptsToOnlineMax,
676 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800677 } else {
678 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700679
680 metrics()->SendToUMA(
681 metrics()->GetFullMetricName(
682 Metrics::kMetricPortalAttempts, technology()),
683 result.num_attempts,
684 Metrics::kMetricPortalAttemptsMin,
685 Metrics::kMetricPortalAttemptsMax,
686 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800687 }
688}
689
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800690vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Jason Glasgow08afdff2012-04-03 10:22:26 -0400691 if (ipconfig_.get()) {
692 string id = ipconfig_->GetRpcIdentifier();
693 return vector<string>(1, id);
694 }
695 return vector<string>();
Chris Masone4e851612011-07-01 10:46:53 -0700696}
697
698string Device::GetRpcConnectionIdentifier() {
699 return adaptor_->GetRpcConnectionIdentifier();
700}
701
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400702bool Device::IsUnderlyingDeviceEnabled() const {
703 return false;
704}
705
Eric Shienbrood9a245532012-03-07 14:20:39 -0500706// callback
707void Device::OnEnabledStateChanged(const ResultCallback &callback,
708 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700709 SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500710 if (error.IsSuccess()) {
711 enabled_ = enabled_pending_;
712 manager_->UpdateEnabledTechnologies();
713 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
714 adaptor_->UpdateEnabled();
715 }
Gary Morainbaeefdf2012-04-30 14:53:35 -0700716 enabled_pending_ = enabled_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500717 if (!callback.is_null())
718 callback.Run(error);
719}
720
721void Device::SetEnabled(bool enable) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700722 SLOG(Device, 2) << __func__ << "(" << enable << ")";
Jason Glasgow4a490792012-04-10 15:02:05 -0400723 Error error;
724 SetEnabledInternal(enable, false, &error, ResultCallback());
Jason Glasgow7234ec32012-05-23 16:01:21 -0400725 LOG_IF(ERROR, error.IsFailure() && !error.IsOngoing())
Jason Glasgow4a490792012-04-10 15:02:05 -0400726 << "Enabled failed, but no way to report the failure.";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500727}
728
729void Device::SetEnabledPersistent(bool enable,
730 Error *error,
731 const ResultCallback &callback) {
732 SetEnabledInternal(enable, true, error, callback);
733}
734
735void Device::SetEnabledInternal(bool enable,
736 bool persist,
737 Error *error,
738 const ResultCallback &callback) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400739 DCHECK(error);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700740 SLOG(Device, 2) << "Device " << link_name_ << " "
741 << (enable ? "starting" : "stopping");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500742 if (enable == enabled_) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400743 error->Reset();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500744 return;
745 }
746
747 if (enabled_pending_ == enable) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400748 Error::PopulateAndLog(error, Error::kInProgress,
749 "Enable operation already in progress");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500750 return;
751 }
752
753 if (persist) {
754 enabled_persistent_ = enable;
755 manager_->SaveActiveProfile();
756 }
757
758 enabled_pending_ = enable;
759 EnabledStateChangedCallback enabled_callback =
760 Bind(&Device::OnEnabledStateChanged,
761 weak_ptr_factory_.GetWeakPtr(), callback);
762 if (enable) {
763 running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500764 Start(error, enabled_callback);
765 } else {
766 running_ = false;
767 DestroyIPConfig(); // breaks a reference cycle
768 SelectService(NULL); // breaks a reference cycle
769 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700770 SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ "
771 << (ipconfig_ ? "is set." : "is not set.");
772 SLOG(Device, 3) << "Device " << link_name_ << " connection_ "
773 << (connection_ ? "is set." : "is not set.");
774 SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ "
775 << (selected_service_ ? "is set." : "is not set.");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500776 Stop(error, enabled_callback);
777 }
778}
779
Paul Stewart75897df2011-04-27 09:05:53 -0700780} // namespace shill