blob: 3d1d0106688b65481d734427654a2369ac519388 [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 }
464 // TODO(pstew): We need to revisit the model here: should the Device
465 // subclass be responsible for calling DestroyIPConfig() (which would
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800466 // trigger DestroyConnection() and Service::SetConnection(NULL))?
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800467 // Ethernet does, but WiFi currently does not. crosbug.com/23929
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800468 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700469 }
470 selected_service_ = service;
471}
472
473void Device::SetServiceState(Service::ConnectState state) {
474 if (selected_service_.get()) {
475 selected_service_->SetState(state);
476 }
477}
478
479void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
480 if (selected_service_.get()) {
481 selected_service_->SetFailure(failure_state);
482 }
483}
484
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400485void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) {
486 if (selected_service_.get()) {
487 selected_service_->SetFailureSilent(failure_state);
488 }
489}
490
Chris Masone34af2182011-08-22 11:59:36 -0700491string Device::SerializeIPConfigs(const string &suffix) {
492 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700493}
494
Paul Stewart2bf1d352011-12-06 15:02:55 -0800495bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
496 const string &value) {
497 string ip_version;
498 if (family == IPAddress::kFamilyIPv4) {
499 ip_version = kIPFlagVersion4;
500 } else if (family == IPAddress::kFamilyIPv6) {
501 ip_version = kIPFlagVersion6;
502 } else {
503 NOTIMPLEMENTED();
504 }
505 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
506 link_name_.c_str(), flag.c_str()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700507 SLOG(Device, 2) << "Writing " << value << " to flag file "
508 << flag_file.value();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800509 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
510 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
511 value.c_str(), flag_file.value().c_str());
512 return false;
513 }
514 return true;
515}
516
Paul Stewartd215af62012-04-24 23:25:50 -0700517bool Device::RestartPortalDetection() {
518 StopPortalDetection();
519 return StartPortalDetection();
520}
521
Paul Stewartc681fa02012-03-02 19:40:04 -0800522bool Device::RequestPortalDetection() {
523 if (!selected_service_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700524 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800525 << ": No selected service, so no need for portal check.";
526 return false;
527 }
528
529 if (!connection_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700530 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800531 << ": No connection, so no need for portal check.";
532 return false;
533 }
534
535 if (selected_service_->state() != Service::kStatePortal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700536 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800537 << ": Service is not in portal state. No need to start check.";
538 return false;
539 }
540
541 if (!connection_->is_default()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700542 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800543 << ": Service is not the default connection. Don't start check.";
544 return false;
545 }
546
547 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700548 SLOG(Device, 2) << FriendlyName()
549 << ": Portal detection is already running.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800550 return true;
551 }
552
553 return StartPortalDetection();
554}
555
Paul Stewart20088d82012-02-16 06:58:55 -0800556bool Device::StartPortalDetection() {
Paul Stewartd215af62012-04-24 23:25:50 -0700557 DCHECK(selected_service_.get());
558 if (selected_service_->IsPortalDetectionDisabled()) {
559 SLOG(Device, 2) << "Service " << selected_service_->friendly_name()
560 << ": Portal detection is disabled; "
561 << "marking service online.";
562 SetServiceConnectedState(Service::kStateOnline);
563 return false;
564 }
565
566 if (selected_service_->IsPortalDetectionAuto() &&
567 !manager_->IsPortalDetectionEnabled(technology())) {
Paul Stewart20088d82012-02-16 06:58:55 -0800568 // If portal detection is disabled for this technology, immediately set
569 // the service state to "Online".
Ben Chanfad4a0b2012-04-18 15:49:59 -0700570 SLOG(Device, 2) << "Device " << FriendlyName()
571 << ": Portal detection is disabled; "
572 << "marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800573 SetServiceConnectedState(Service::kStateOnline);
574 return false;
575 }
576
Paul Stewart20088d82012-02-16 06:58:55 -0800577 if (selected_service_->HasProxyConfig()) {
578 // Services with HTTP proxy configurations should not be checked by the
579 // connection manager, since we don't have the ability to evaluate
580 // arbitrary proxy configs and their possible credentials.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700581 SLOG(Device, 2) << "Device " << FriendlyName()
582 << ": Service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800583 SetServiceConnectedState(Service::kStateOnline);
584 return false;
585 }
586
587 portal_detector_.reset(new PortalDetector(connection_,
588 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500589 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800590 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
591 LOG(ERROR) << "Device " << FriendlyName()
592 << ": Portal detection failed to start: likely bad URL: "
593 << manager_->GetPortalCheckURL();
594 SetServiceConnectedState(Service::kStateOnline);
595 return false;
596 }
597
Ben Chanfad4a0b2012-04-18 15:49:59 -0700598 SLOG(Device, 2) << "Device " << FriendlyName()
599 << ": Portal detection has started.";
Paul Stewart20088d82012-02-16 06:58:55 -0800600 return true;
601}
602
603void Device::StopPortalDetection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700604 SLOG(Device, 2) << "Device " << FriendlyName()
605 << ": Portal detection has stopped.";
Paul Stewart20088d82012-02-16 06:58:55 -0800606 portal_detector_.reset();
607}
608
609void Device::SetServiceConnectedState(Service::ConnectState state) {
610 DCHECK(selected_service_.get());
611
612 if (!selected_service_.get()) {
613 LOG(ERROR) << FriendlyName() << ": "
614 << "Portal detection completed but no selected service exists!";
615 return;
616 }
617
618 if (!selected_service_->IsConnected()) {
619 LOG(ERROR) << FriendlyName() << ": "
620 << "Portal detection completed but selected service "
621 << selected_service_->UniqueName()
622 << " is in non-connected state.";
623 return;
624 }
625
Paul Stewartc681fa02012-03-02 19:40:04 -0800626 if (state == Service::kStatePortal && connection_->is_default() &&
627 manager_->GetPortalCheckInterval() != 0) {
628 CHECK(portal_detector_.get());
629 if (!portal_detector_->StartAfterDelay(
630 manager_->GetPortalCheckURL(),
631 manager_->GetPortalCheckInterval())) {
632 LOG(ERROR) << "Device " << FriendlyName()
633 << ": Portal detection failed to restart: likely bad URL: "
634 << manager_->GetPortalCheckURL();
635 SetServiceState(Service::kStateOnline);
636 portal_detector_.reset();
637 return;
638 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700639 SLOG(Device, 2) << "Device " << FriendlyName()
640 << ": Portal detection retrying.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800641 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700642 SLOG(Device, 2) << "Device " << FriendlyName()
643 << ": Portal will not retry.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800644 portal_detector_.reset();
645 }
646
Paul Stewart20088d82012-02-16 06:58:55 -0800647 SetServiceState(state);
648}
649
650void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
651 if (!result.final) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700652 SLOG(Device, 2) << "Device " << FriendlyName()
653 << ": Received non-final status: "
654 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800655 return;
656 }
657
Ben Chanfad4a0b2012-04-18 15:49:59 -0700658 SLOG(Device, 2) << "Device " << FriendlyName()
659 << ": Received final status: "
660 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800661
Thieu Le85e050b2012-03-13 15:04:38 -0700662 portal_attempts_to_online_ += result.num_attempts;
663
664 metrics()->SendEnumToUMA(
665 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
666 Metrics::PortalDetectionResultToEnum(result),
667 Metrics::kPortalResultMax);
668
Paul Stewart20088d82012-02-16 06:58:55 -0800669 if (result.status == PortalDetector::kStatusSuccess) {
670 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700671
672 metrics()->SendToUMA(
673 metrics()->GetFullMetricName(
674 Metrics::kMetricPortalAttemptsToOnline, technology()),
675 portal_attempts_to_online_,
676 Metrics::kMetricPortalAttemptsToOnlineMin,
677 Metrics::kMetricPortalAttemptsToOnlineMax,
678 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800679 } else {
680 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700681
682 metrics()->SendToUMA(
683 metrics()->GetFullMetricName(
684 Metrics::kMetricPortalAttempts, technology()),
685 result.num_attempts,
686 Metrics::kMetricPortalAttemptsMin,
687 Metrics::kMetricPortalAttemptsMax,
688 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800689 }
690}
691
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800692vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Jason Glasgow08afdff2012-04-03 10:22:26 -0400693 if (ipconfig_.get()) {
694 string id = ipconfig_->GetRpcIdentifier();
695 return vector<string>(1, id);
696 }
697 return vector<string>();
Chris Masone4e851612011-07-01 10:46:53 -0700698}
699
700string Device::GetRpcConnectionIdentifier() {
701 return adaptor_->GetRpcConnectionIdentifier();
702}
703
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400704bool Device::IsUnderlyingDeviceEnabled() const {
705 return false;
706}
707
Eric Shienbrood9a245532012-03-07 14:20:39 -0500708// callback
709void Device::OnEnabledStateChanged(const ResultCallback &callback,
710 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700711 SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500712 if (error.IsSuccess()) {
713 enabled_ = enabled_pending_;
714 manager_->UpdateEnabledTechnologies();
715 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
716 adaptor_->UpdateEnabled();
717 }
Gary Morainbaeefdf2012-04-30 14:53:35 -0700718 enabled_pending_ = enabled_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500719 if (!callback.is_null())
720 callback.Run(error);
721}
722
723void Device::SetEnabled(bool enable) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700724 SLOG(Device, 2) << __func__ << "(" << enable << ")";
Jason Glasgow4a490792012-04-10 15:02:05 -0400725 Error error;
726 SetEnabledInternal(enable, false, &error, ResultCallback());
727 LOG_IF(ERROR, error.IsFailure())
728 << "Enabled failed, but no way to report the failure.";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500729}
730
731void Device::SetEnabledPersistent(bool enable,
732 Error *error,
733 const ResultCallback &callback) {
734 SetEnabledInternal(enable, true, error, callback);
735}
736
737void Device::SetEnabledInternal(bool enable,
738 bool persist,
739 Error *error,
740 const ResultCallback &callback) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400741 DCHECK(error);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700742 SLOG(Device, 2) << "Device " << link_name_ << " "
743 << (enable ? "starting" : "stopping");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500744 if (enable == enabled_) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400745 error->Reset();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500746 return;
747 }
748
749 if (enabled_pending_ == enable) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400750 Error::PopulateAndLog(error, Error::kInProgress,
751 "Enable operation already in progress");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500752 return;
753 }
754
755 if (persist) {
756 enabled_persistent_ = enable;
757 manager_->SaveActiveProfile();
758 }
759
760 enabled_pending_ = enable;
761 EnabledStateChangedCallback enabled_callback =
762 Bind(&Device::OnEnabledStateChanged,
763 weak_ptr_factory_.GetWeakPtr(), callback);
764 if (enable) {
765 running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500766 Start(error, enabled_callback);
767 } else {
768 running_ = false;
769 DestroyIPConfig(); // breaks a reference cycle
770 SelectService(NULL); // breaks a reference cycle
771 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700772 SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ "
773 << (ipconfig_ ? "is set." : "is not set.");
774 SLOG(Device, 3) << "Device " << link_name_ << " connection_ "
775 << (connection_ ? "is set." : "is not set.");
776 SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ "
777 << (selected_service_ ? "is set." : "is not set.");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500778 Stop(error, enabled_callback);
779 }
780}
781
Paul Stewart75897df2011-04-27 09:05:53 -0700782} // namespace shill