blob: 540a53820d56f2f6d19a104c15881ccc7aedb095 [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 {
392 // TODO(pstew): This logic gets more complex when multiple IPConfig types
393 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700394 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700395 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700396 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700397}
398
Paul Stewarte6132022011-08-16 09:11:02 -0700399void Device::CreateConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700400 SLOG(Device, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -0700401 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700402 connection_ = new Connection(interface_index_,
403 link_name_,
Paul Stewarte00600e2012-03-16 07:08:00 -0700404 technology_,
Paul Stewart9a908082011-08-31 12:18:48 -0700405 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700406 }
407}
408
409void Device::DestroyConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700410 SLOG(Device, 2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800411 StopPortalDetection();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800412 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800413 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800414 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800415 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700416}
417
Paul Stewart03dba0b2011-08-22 16:32:45 -0700418void Device::SelectService(const ServiceRefPtr &service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700419 SLOG(Device, 2) << __func__ << ": "
420 << (service.get() ?
421 StringPrintf("%s (%s)",
422 service->UniqueName().c_str(),
423 service->friendly_name().c_str()) :
424 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000425
426 if (selected_service_.get() == service.get()) {
427 // No change to |selected_service_|. Return early to avoid
428 // changing its state.
429 return;
430 }
431
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800432 if (selected_service_.get()) {
433 if (selected_service_->state() != Service::kStateFailure) {
434 selected_service_->SetState(Service::kStateIdle);
435 }
436 // TODO(pstew): We need to revisit the model here: should the Device
437 // subclass be responsible for calling DestroyIPConfig() (which would
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800438 // trigger DestroyConnection() and Service::SetConnection(NULL))?
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800439 // Ethernet does, but WiFi currently does not. crosbug.com/23929
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800440 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700441 }
442 selected_service_ = service;
443}
444
445void Device::SetServiceState(Service::ConnectState state) {
446 if (selected_service_.get()) {
447 selected_service_->SetState(state);
448 }
449}
450
451void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
452 if (selected_service_.get()) {
453 selected_service_->SetFailure(failure_state);
454 }
455}
456
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400457void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) {
458 if (selected_service_.get()) {
459 selected_service_->SetFailureSilent(failure_state);
460 }
461}
462
Chris Masone34af2182011-08-22 11:59:36 -0700463string Device::SerializeIPConfigs(const string &suffix) {
464 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700465}
466
Paul Stewart2bf1d352011-12-06 15:02:55 -0800467bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
468 const string &value) {
469 string ip_version;
470 if (family == IPAddress::kFamilyIPv4) {
471 ip_version = kIPFlagVersion4;
472 } else if (family == IPAddress::kFamilyIPv6) {
473 ip_version = kIPFlagVersion6;
474 } else {
475 NOTIMPLEMENTED();
476 }
477 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
478 link_name_.c_str(), flag.c_str()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700479 SLOG(Device, 2) << "Writing " << value << " to flag file "
480 << flag_file.value();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800481 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
482 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
483 value.c_str(), flag_file.value().c_str());
484 return false;
485 }
486 return true;
487}
488
Paul Stewartd215af62012-04-24 23:25:50 -0700489bool Device::RestartPortalDetection() {
490 StopPortalDetection();
491 return StartPortalDetection();
492}
493
Paul Stewartc681fa02012-03-02 19:40:04 -0800494bool Device::RequestPortalDetection() {
495 if (!selected_service_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700496 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800497 << ": No selected service, so no need for portal check.";
498 return false;
499 }
500
501 if (!connection_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700502 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800503 << ": No connection, so no need for portal check.";
504 return false;
505 }
506
507 if (selected_service_->state() != Service::kStatePortal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700508 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800509 << ": Service is not in portal state. No need to start check.";
510 return false;
511 }
512
513 if (!connection_->is_default()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700514 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800515 << ": Service is not the default connection. Don't start check.";
516 return false;
517 }
518
519 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700520 SLOG(Device, 2) << FriendlyName()
521 << ": Portal detection is already running.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800522 return true;
523 }
524
525 return StartPortalDetection();
526}
527
Paul Stewart20088d82012-02-16 06:58:55 -0800528bool Device::StartPortalDetection() {
Paul Stewartd215af62012-04-24 23:25:50 -0700529 DCHECK(selected_service_.get());
530 if (selected_service_->IsPortalDetectionDisabled()) {
531 SLOG(Device, 2) << "Service " << selected_service_->friendly_name()
532 << ": Portal detection is disabled; "
533 << "marking service online.";
534 SetServiceConnectedState(Service::kStateOnline);
535 return false;
536 }
537
538 if (selected_service_->IsPortalDetectionAuto() &&
539 !manager_->IsPortalDetectionEnabled(technology())) {
Paul Stewart20088d82012-02-16 06:58:55 -0800540 // If portal detection is disabled for this technology, immediately set
541 // the service state to "Online".
Ben Chanfad4a0b2012-04-18 15:49:59 -0700542 SLOG(Device, 2) << "Device " << FriendlyName()
543 << ": Portal detection is disabled; "
544 << "marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800545 SetServiceConnectedState(Service::kStateOnline);
546 return false;
547 }
548
Paul Stewart20088d82012-02-16 06:58:55 -0800549 if (selected_service_->HasProxyConfig()) {
550 // Services with HTTP proxy configurations should not be checked by the
551 // connection manager, since we don't have the ability to evaluate
552 // arbitrary proxy configs and their possible credentials.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700553 SLOG(Device, 2) << "Device " << FriendlyName()
554 << ": Service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800555 SetServiceConnectedState(Service::kStateOnline);
556 return false;
557 }
558
559 portal_detector_.reset(new PortalDetector(connection_,
560 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500561 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800562 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
563 LOG(ERROR) << "Device " << FriendlyName()
564 << ": Portal detection failed to start: likely bad URL: "
565 << manager_->GetPortalCheckURL();
566 SetServiceConnectedState(Service::kStateOnline);
567 return false;
568 }
569
Ben Chanfad4a0b2012-04-18 15:49:59 -0700570 SLOG(Device, 2) << "Device " << FriendlyName()
571 << ": Portal detection has started.";
Paul Stewart20088d82012-02-16 06:58:55 -0800572 return true;
573}
574
575void Device::StopPortalDetection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700576 SLOG(Device, 2) << "Device " << FriendlyName()
577 << ": Portal detection has stopped.";
Paul Stewart20088d82012-02-16 06:58:55 -0800578 portal_detector_.reset();
579}
580
581void Device::SetServiceConnectedState(Service::ConnectState state) {
582 DCHECK(selected_service_.get());
583
584 if (!selected_service_.get()) {
585 LOG(ERROR) << FriendlyName() << ": "
586 << "Portal detection completed but no selected service exists!";
587 return;
588 }
589
590 if (!selected_service_->IsConnected()) {
591 LOG(ERROR) << FriendlyName() << ": "
592 << "Portal detection completed but selected service "
593 << selected_service_->UniqueName()
594 << " is in non-connected state.";
595 return;
596 }
597
Paul Stewartc681fa02012-03-02 19:40:04 -0800598 if (state == Service::kStatePortal && connection_->is_default() &&
599 manager_->GetPortalCheckInterval() != 0) {
600 CHECK(portal_detector_.get());
601 if (!portal_detector_->StartAfterDelay(
602 manager_->GetPortalCheckURL(),
603 manager_->GetPortalCheckInterval())) {
604 LOG(ERROR) << "Device " << FriendlyName()
605 << ": Portal detection failed to restart: likely bad URL: "
606 << manager_->GetPortalCheckURL();
607 SetServiceState(Service::kStateOnline);
608 portal_detector_.reset();
609 return;
610 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700611 SLOG(Device, 2) << "Device " << FriendlyName()
612 << ": Portal detection retrying.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800613 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700614 SLOG(Device, 2) << "Device " << FriendlyName()
615 << ": Portal will not retry.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800616 portal_detector_.reset();
617 }
618
Paul Stewart20088d82012-02-16 06:58:55 -0800619 SetServiceState(state);
620}
621
622void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
623 if (!result.final) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700624 SLOG(Device, 2) << "Device " << FriendlyName()
625 << ": Received non-final status: "
626 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800627 return;
628 }
629
Ben Chanfad4a0b2012-04-18 15:49:59 -0700630 SLOG(Device, 2) << "Device " << FriendlyName()
631 << ": Received final status: "
632 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800633
Thieu Le85e050b2012-03-13 15:04:38 -0700634 portal_attempts_to_online_ += result.num_attempts;
635
636 metrics()->SendEnumToUMA(
637 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
638 Metrics::PortalDetectionResultToEnum(result),
639 Metrics::kPortalResultMax);
640
Paul Stewart20088d82012-02-16 06:58:55 -0800641 if (result.status == PortalDetector::kStatusSuccess) {
642 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700643
644 metrics()->SendToUMA(
645 metrics()->GetFullMetricName(
646 Metrics::kMetricPortalAttemptsToOnline, technology()),
647 portal_attempts_to_online_,
648 Metrics::kMetricPortalAttemptsToOnlineMin,
649 Metrics::kMetricPortalAttemptsToOnlineMax,
650 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800651 } else {
652 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700653
654 metrics()->SendToUMA(
655 metrics()->GetFullMetricName(
656 Metrics::kMetricPortalAttempts, technology()),
657 result.num_attempts,
658 Metrics::kMetricPortalAttemptsMin,
659 Metrics::kMetricPortalAttemptsMax,
660 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800661 }
662}
663
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800664vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Jason Glasgow08afdff2012-04-03 10:22:26 -0400665 if (ipconfig_.get()) {
666 string id = ipconfig_->GetRpcIdentifier();
667 return vector<string>(1, id);
668 }
669 return vector<string>();
Chris Masone4e851612011-07-01 10:46:53 -0700670}
671
672string Device::GetRpcConnectionIdentifier() {
673 return adaptor_->GetRpcConnectionIdentifier();
674}
675
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400676bool Device::IsUnderlyingDeviceEnabled() const {
677 return false;
678}
679
Eric Shienbrood9a245532012-03-07 14:20:39 -0500680// callback
681void Device::OnEnabledStateChanged(const ResultCallback &callback,
682 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700683 SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500684 if (error.IsSuccess()) {
685 enabled_ = enabled_pending_;
686 manager_->UpdateEnabledTechnologies();
687 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
688 adaptor_->UpdateEnabled();
689 }
Gary Morainbaeefdf2012-04-30 14:53:35 -0700690 enabled_pending_ = enabled_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500691 if (!callback.is_null())
692 callback.Run(error);
693}
694
695void Device::SetEnabled(bool enable) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700696 SLOG(Device, 2) << __func__ << "(" << enable << ")";
Jason Glasgow4a490792012-04-10 15:02:05 -0400697 Error error;
698 SetEnabledInternal(enable, false, &error, ResultCallback());
699 LOG_IF(ERROR, error.IsFailure())
700 << "Enabled failed, but no way to report the failure.";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500701}
702
703void Device::SetEnabledPersistent(bool enable,
704 Error *error,
705 const ResultCallback &callback) {
706 SetEnabledInternal(enable, true, error, callback);
707}
708
709void Device::SetEnabledInternal(bool enable,
710 bool persist,
711 Error *error,
712 const ResultCallback &callback) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400713 DCHECK(error);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700714 SLOG(Device, 2) << "Device " << link_name_ << " "
715 << (enable ? "starting" : "stopping");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500716 if (enable == enabled_) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400717 error->Reset();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500718 return;
719 }
720
721 if (enabled_pending_ == enable) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400722 Error::PopulateAndLog(error, Error::kInProgress,
723 "Enable operation already in progress");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500724 return;
725 }
726
727 if (persist) {
728 enabled_persistent_ = enable;
729 manager_->SaveActiveProfile();
730 }
731
732 enabled_pending_ = enable;
733 EnabledStateChangedCallback enabled_callback =
734 Bind(&Device::OnEnabledStateChanged,
735 weak_ptr_factory_.GetWeakPtr(), callback);
736 if (enable) {
737 running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500738 Start(error, enabled_callback);
739 } else {
740 running_ = false;
741 DestroyIPConfig(); // breaks a reference cycle
742 SelectService(NULL); // breaks a reference cycle
743 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700744 SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ "
745 << (ipconfig_ ? "is set." : "is not set.");
746 SLOG(Device, 3) << "Device " << link_name_ << " connection_ "
747 << (connection_ ? "is set." : "is not set.");
748 SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ "
749 << (selected_service_ ? "is set." : "is not set.");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500750 Stop(error, enabled_callback);
751 }
752}
753
Paul Stewart75897df2011-04-27 09:05:53 -0700754} // namespace shill