blob: 68a1cf2e643d880401eb331e6ab8cb1b958c6d8d [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// static
Chris Masone5dec5f42011-07-22 14:07:55 -070066const char Device::kStoragePowered[] = "Powered";
Chris Masone5dec5f42011-07-22 14:07:55 -070067// static
68const char Device::kStorageIPConfigs[] = "IPConfigs";
Paul Stewart6ff27f52012-07-11 06:51:41 -070069// static
70const char Device::kStorageReceiveByteCount[] = "ReceiveByteCount";
71// static
72const char Device::kStorageTransmitByteCount[] = "TransmitByteCount";
Chris Masone5dec5f42011-07-22 14:07:55 -070073
Paul Stewart75897df2011-04-27 09:05:53 -070074Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070075 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080076 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070077 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070078 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070079 const string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080080 int interface_index,
81 Technology::Identifier technology)
Eric Shienbrood9a245532012-03-07 14:20:39 -050082 : enabled_(false),
83 enabled_persistent_(true),
84 enabled_pending_(enabled_),
Chris Masoneb925cc82011-06-22 15:39:57 -070085 reconnect_(true),
Chris Masone626719f2011-08-18 16:58:48 -070086 hardware_address_(address),
mukesh agrawalf60e4062011-05-27 13:13:41 -070087 interface_index_(interface_index),
88 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070089 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070090 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070091 control_interface_(control_interface),
92 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080093 metrics_(metrics),
Chris Masone7df0c672011-07-15 10:24:54 -070094 manager_(manager),
Eric Shienbrood9a245532012-03-07 14:20:39 -050095 weak_ptr_factory_(this),
Darin Petkov77cb6812011-08-15 16:19:41 -070096 adaptor_(control_interface->CreateDeviceAdaptor(this)),
Eric Shienbrood9a245532012-03-07 14:20:39 -050097 portal_detector_callback_(Bind(&Device::PortalDetectorCallback,
98 weak_ptr_factory_.GetWeakPtr())),
Gaurav Shah435de2c2011-11-17 19:01:07 -080099 technology_(technology),
Thieu Le85e050b2012-03-13 15:04:38 -0700100 portal_attempts_to_online_(0),
Paul Stewart6ff27f52012-07-11 06:51:41 -0700101 receive_byte_offset_(0),
102 transmit_byte_offset_(0),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700103 dhcp_provider_(DHCPProvider::GetInstance()),
104 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700105 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -0700106
107 // flimflam::kBgscanMethodProperty: Registered in WiFi
108 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
109 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
110
111 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
112 // flimflam::kCarrierProperty: Registered in Cellular
113 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -0700114 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700115 // flimflam::kImeiProperty: Registered in Cellular
Ben Chana5e27082012-07-31 14:30:28 -0700116 // flimflam::kIccidProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700117 // flimflam::kImsiProperty: Registered in Cellular
118 // flimflam::kManufacturerProperty: Registered in Cellular
119 // flimflam::kMdnProperty: Registered in Cellular
120 // flimflam::kMeidProperty: Registered in Cellular
121 // flimflam::kMinProperty: Registered in Cellular
122 // flimflam::kModelIDProperty: Registered in Cellular
123 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
124 // flimflam::kHardwareRevisionProperty: Registered in Cellular
125 // flimflam::kPRLVersionProperty: Registered in Cellular
126 // flimflam::kSIMLockStatusProperty: Registered in Cellular
127 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -0700128 // flimflam::kDBusConnectionProperty: Registered in Cellular
129 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700130
Jason Glasgowe8334fd2012-03-30 16:02:37 -0400131 store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
Jason Glasgow08afdff2012-04-03 10:22:26 -0400132 HelpRegisterConstDerivedRpcIdentifiers(flimflam::kIPConfigsProperty,
133 &Device::AvailableIPConfigs);
Chris Masone27c4aa52011-07-02 13:10:14 -0700134 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500135 store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_);
Jason Glasgowb5790052012-01-27 01:03:52 -0500136 HelpRegisterDerivedString(flimflam::kTypeProperty,
137 &Device::GetTechnologyString,
138 NULL);
139
Chris Masoneb925cc82011-06-22 15:39:57 -0700140 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700141 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700142
Chris Masone4e851612011-07-01 10:46:53 -0700143 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700144 // known_properties_.push_back(flimflam::kNetworksProperty);
145
Chris Masone4d42df82011-07-02 17:09:39 -0700146 // flimflam::kScanningProperty: Registered in WiFi, Cellular
147 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
Paul Stewart6ff27f52012-07-11 06:51:41 -0700148
149 if (manager_ && manager_->device_info()) { // Unit tests may not have these.
150 manager_->device_info()->GetByteCounts(
151 interface_index_, &receive_byte_offset_, &transmit_byte_offset_);
152 HelpRegisterConstDerivedUint64(shill::kReceiveByteCountProperty,
153 &Device::GetReceiveByteCount);
154 HelpRegisterConstDerivedUint64(shill::kTransmitByteCountProperty,
155 &Device::GetTransmitByteCount);
156 }
157
Darin Petkova0a0efe2012-06-27 12:50:01 +0200158 LOG(INFO) << "Device created: " << link_name_
159 << " index " << interface_index_;
Paul Stewart75897df2011-04-27 09:05:53 -0700160}
161
162Device::~Device() {
Darin Petkova0a0efe2012-06-27 12:50:01 +0200163 LOG(INFO) << "Device destructed: " << link_name_
164 << " index " << interface_index_;
Paul Stewart75897df2011-04-27 09:05:53 -0700165}
166
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700167void Device::LinkEvent(unsigned flags, unsigned change) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700168 SLOG(Device, 2) << "Device " << link_name_
169 << std::showbase << std::hex
170 << " flags " << flags << " changed " << change
171 << std::dec << std::noshowbase;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700172}
173
Darin Petkovc0865312011-09-16 15:31:20 -0700174void Device::Scan(Error *error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700175 SLOG(Device, 2) << "Device " << link_name_ << " scan requested.";
Paul Stewartbe005172011-11-02 18:10:29 -0700176 Error::PopulateAndLog(error, Error::kNotSupported,
177 "Device doesn't support scan.");
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700178}
179
Eric Shienbrood9a245532012-03-07 14:20:39 -0500180void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error,
181 const ResultCallback &/*callback*/) {
182 Error::PopulateAndLog(error, Error::kNotSupported,
Paul Stewartbe005172011-11-02 18:10:29 -0700183 "Device doesn't support network registration.");
Darin Petkov9ae310f2011-08-30 15:41:13 -0700184}
185
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100186void Device::RequirePIN(
Eric Shienbrood9a245532012-03-07 14:20:39 -0500187 const string &/*pin*/, bool /*require*/,
188 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700189 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500190 Error::PopulateAndLog(error, Error::kNotSupported,
191 "Device doesn't support RequirePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700192}
193
Eric Shienbrood9a245532012-03-07 14:20:39 -0500194void Device::EnterPIN(const string &/*pin*/,
195 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700196 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500197 Error::PopulateAndLog(error, Error::kNotSupported,
198 "Device doesn't support EnterPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700199}
200
mukesh agrawal1830fa12011-09-26 14:31:40 -0700201void Device::UnblockPIN(const string &/*unblock_code*/,
202 const string &/*pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500203 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700204 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500205 Error::PopulateAndLog(error, Error::kNotSupported,
206 "Device doesn't support UnblockPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700207}
208
mukesh agrawal1830fa12011-09-26 14:31:40 -0700209void Device::ChangePIN(const string &/*old_pin*/,
210 const string &/*new_pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500211 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700212 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500213 Error::PopulateAndLog(error, Error::kNotSupported,
214 "Device doesn't support ChangePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700215}
216
Paul Stewart2bf1d352011-12-06 15:02:55 -0800217void Device::DisableIPv6() {
218 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
219}
220
221void Device::EnableIPv6() {
222 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
223}
224
225void Device::EnableIPv6Privacy() {
226 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
227 kIPFlagUseTempAddrUsedAndDefault);
228}
229
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800230void Device::DisableReversePathFilter() {
231 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
232 // for IPv6. crosbug.com/24228
233 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
234 kIPFlagReversePathFilterLooseMode);
235}
236
237void Device::EnableReversePathFilter() {
238 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
239 kIPFlagReversePathFilterEnabled);
240}
241
Gaurav Shah435de2c2011-11-17 19:01:07 -0800242bool Device::IsConnected() const {
243 if (selected_service_)
244 return selected_service_->IsConnected();
245 return false;
246}
247
Paul Stewartd215af62012-04-24 23:25:50 -0700248bool Device::IsConnectedToService(const ServiceRefPtr &service) const {
249 return service == selected_service_ && IsConnected();
250}
251
Chris Masone27c4aa52011-07-02 13:10:14 -0700252string Device::GetRpcIdentifier() {
253 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700254}
255
Chris Masone5dec5f42011-07-22 14:07:55 -0700256string Device::GetStorageIdentifier() {
257 string id = GetRpcIdentifier();
258 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700259 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700260 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700261 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700262 return id;
263}
264
Jason Glasgowb5790052012-01-27 01:03:52 -0500265string Device::GetTechnologyString(Error */*error*/) {
266 return Technology::NameFromIdentifier(technology());
267}
268
Chris Masone19e30402011-07-19 15:48:47 -0700269const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700270 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700271}
272
Chris Masone19e30402011-07-19 15:48:47 -0700273const string& Device::UniqueName() const {
274 return unique_id_;
275}
276
Chris Masone5dec5f42011-07-22 14:07:55 -0700277bool Device::Load(StoreInterface *storage) {
278 const string id = GetStorageIdentifier();
279 if (!storage->ContainsGroup(id)) {
280 LOG(WARNING) << "Device is not available in the persistent store: " << id;
281 return false;
282 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500283 enabled_persistent_ = true;
284 storage->GetBool(id, kStoragePowered, &enabled_persistent_);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700285 uint64 rx_byte_count = 0, tx_byte_count = 0;
286
287 manager_->device_info()->GetByteCounts(
288 interface_index_, &rx_byte_count, &tx_byte_count);
289 // If there is a byte-count present in the profile, the return value
290 // of Device::Get*ByteCount() should be the this stored value plus
291 // whatever additional bytes we receive since time-of-load. We
292 // accomplish this by the subtractions below, which can validly
293 // roll over "negative" in the subtractions below and in Get*ByteCount.
294 uint64 profile_byte_count;
295 if (storage->GetUint64(id, kStorageReceiveByteCount, &profile_byte_count)) {
296 receive_byte_offset_ = rx_byte_count - profile_byte_count;
297 }
298 if (storage->GetUint64(id, kStorageTransmitByteCount, &profile_byte_count)) {
299 transmit_byte_offset_ = tx_byte_count - profile_byte_count;
300 }
301
Chris Masone5dec5f42011-07-22 14:07:55 -0700302 return true;
303}
304
305bool Device::Save(StoreInterface *storage) {
306 const string id = GetStorageIdentifier();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500307 storage->SetBool(id, kStoragePowered, enabled_persistent_);
Chris Masone34af2182011-08-22 11:59:36 -0700308 if (ipconfig_.get()) {
309 // The _0 is an index into the list of IPConfigs that this device might
310 // have. We only have one IPConfig right now, and I hope to never have
311 // to support more, as sleffler indicates that associating IPConfigs
312 // with devices is wrong and due to be changed in flimflam anyhow.
313 string suffix = hardware_address_ + "_0";
314 ipconfig_->Save(storage, suffix);
315 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
316 }
Paul Stewart6ff27f52012-07-11 06:51:41 -0700317 storage->SetUint64(id, kStorageReceiveByteCount, GetReceiveByteCount(NULL));
318 storage->SetUint64(id, kStorageTransmitByteCount, GetTransmitByteCount(NULL));
Chris Masone5dec5f42011-07-22 14:07:55 -0700319 return true;
320}
321
mukesh agrawal784566d2012-08-08 18:32:58 -0700322void Device::OnBeforeSuspend() {
323 // Nothing to be done in the general case.
324}
325
326void Device::OnAfterResume() {
327 if (ipconfig_) {
328 SLOG(Device, 3) << "Renewing IP address on resume.";
329 ipconfig_->RenewIP();
330 }
331}
332
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200333void Device::DropConnection() {
334 SLOG(Device, 2) << __func__;
335 DestroyIPConfig();
336 SelectService(NULL);
337}
338
Darin Petkovafa6fc42011-06-21 16:21:08 -0700339void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800340 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700341 if (ipconfig_.get()) {
342 ipconfig_->ReleaseIP();
343 ipconfig_ = NULL;
344 }
Paul Stewarte6132022011-08-16 09:11:02 -0700345 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700346}
347
Paul Stewart2bf1d352011-12-06 15:02:55 -0800348bool Device::AcquireIPConfig() {
Paul Stewartd408fdf2012-05-07 17:15:57 -0700349 return AcquireIPConfigWithLeaseName(string());
350}
351
352bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700353 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800354 EnableIPv6();
Paul Stewartd408fdf2012-05-07 17:15:57 -0700355 ipconfig_ = dhcp_provider_->CreateConfig(link_name_,
356 manager_->GetHostName(),
357 lease_name,
358 manager_->GetArpGateway());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500359 ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated,
360 weak_ptr_factory_.GetWeakPtr()));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700361 dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask,
362 weak_ptr_factory_.GetWeakPtr()));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700363 return ipconfig_->RequestIP();
364}
365
Jason Glasgowb5790052012-01-27 01:03:52 -0500366void Device::HelpRegisterDerivedString(
367 const string &name,
368 string(Device::*get)(Error *error),
369 void(Device::*set)(const string &value, Error *error)) {
370 store_.RegisterDerivedString(
371 name,
372 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
373}
374
mukesh agrawalffa3d042011-10-06 15:26:10 -0700375void Device::HelpRegisterDerivedStrings(
376 const string &name,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500377 Strings(Device::*get)(Error *error),
378 void(Device::*set)(const Strings &value, Error *error)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700379 store_.RegisterDerivedStrings(
380 name,
381 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700382}
383
Jason Glasgow08afdff2012-04-03 10:22:26 -0400384void Device::HelpRegisterConstDerivedRpcIdentifiers(
385 const string &name,
386 RpcIdentifiers(Device::*get)(Error *)) {
387 store_.RegisterDerivedRpcIdentifiers(
388 name,
389 RpcIdentifiersAccessor(
390 new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL)));
391}
392
Paul Stewart6ff27f52012-07-11 06:51:41 -0700393void Device::HelpRegisterConstDerivedUint64(
394 const string &name,
395 uint64(Device::*get)(Error *)) {
396 store_.RegisterDerivedUint64(
397 name,
398 Uint64Accessor(
399 new CustomAccessor<Device, uint64>(this, get, NULL)));
400}
401
Paul Stewart1062d9d2012-04-27 10:42:27 -0700402void Device::ConfigureStaticIPTask() {
403 SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get()
404 << " ipconfig " << ipconfig_.get();
405
406 if (!selected_service_ || !ipconfig_) {
407 return;
408 }
409
410 const StaticIPParameters &static_ip_parameters =
411 selected_service_->static_ip_parameters();
412 if (static_ip_parameters.ContainsAddress()) {
413 SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters.";
414 // If the parameters contain an IP address, apply them now and bring
415 // the interface up. When DHCP information arrives, it will supplement
416 // the static information.
417 OnIPConfigUpdated(ipconfig_, true);
418 } else {
419 SLOG(Device, 2) << __func__ << " " << " no static IP address.";
420 }
421}
422
Darin Petkov79d74c92012-03-07 17:20:32 +0100423void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700424 SLOG(Device, 2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700425 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700426 CreateConnection();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700427 if (selected_service_) {
428 ipconfig->ApplyStaticIPParameters(
Paul Stewartdef189e2012-08-02 20:12:09 -0700429 selected_service_->mutable_static_ip_parameters());
Paul Stewart1062d9d2012-04-27 10:42:27 -0700430 }
Paul Stewarte6132022011-08-16 09:11:02 -0700431 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800432 // SetConnection must occur after the UpdateFromIPConfig so the
433 // service can use the values derived from the connection.
Paul Stewart1062d9d2012-04-27 10:42:27 -0700434 if (selected_service_) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800435 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800436 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800437 // The service state change needs to happen last, so that at the
438 // time we report the state change to the manager, the service
439 // has its connection.
440 SetServiceState(Service::kStateConnected);
Thieu Le85e050b2012-03-13 15:04:38 -0700441 portal_attempts_to_online_ = 0;
Paul Stewart20088d82012-02-16 06:58:55 -0800442 // Subtle: Start portal detection after transitioning the service
443 // to the Connected state because this call may immediately transition
444 // to the Online state.
445 StartPortalDetection();
Paul Stewarte6132022011-08-16 09:11:02 -0700446 } else {
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700447 // TODO(pstew): This logic gets yet more complex when multiple
448 // IPConfig types are run in parallel (e.g. DHCP and DHCP6)
449 if (selected_service_ &&
450 selected_service_->static_ip_parameters().ContainsAddress()) {
451 // Consider three cases:
452 //
453 // 1. We're here because DHCP failed while starting up. There
454 // are two subcases:
455 // a. DHCP has failed, and Static IP config has _not yet_
456 // completed. It's fine to do nothing, because we'll
457 // apply the static config shortly.
458 // b. DHCP has failed, and Static IP config has _already_
459 // completed. It's fine to do nothing, because we can
460 // continue to use the static config that's already
461 // been applied.
462 //
463 // 2. We're here because a previously valid DHCP configuration
464 // is no longer valid. There's still a static IP config,
465 // because the condition in the if clause evaluated to true.
466 // Furthermore, the static config includes an IP address for
467 // us to use.
468 //
469 // The current configuration may include some DHCP
470 // parameters, overriden by any static parameters
471 // provided. We continue to use this configuration, because
472 // the only configuration element that is leased to us (IP
473 // address) will be overriden by a static parameter.
474 return;
475 }
476
Paul Stewart03dba0b2011-08-22 16:32:45 -0700477 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700478 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700479 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700480}
481
Paul Stewarte6132022011-08-16 09:11:02 -0700482void Device::CreateConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700483 SLOG(Device, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -0700484 if (!connection_.get()) {
Paul Stewartbf667612012-06-29 14:49:54 -0700485 connection_ = new Connection(
486 interface_index_,
487 link_name_,
488 technology_,
489 manager_->device_info(),
490 manager_->IsTechnologyShortDNSTimeoutEnabled(technology_));
Paul Stewarte6132022011-08-16 09:11:02 -0700491 }
492}
493
494void Device::DestroyConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700495 SLOG(Device, 2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800496 StopPortalDetection();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800497 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800498 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800499 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800500 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700501}
502
Paul Stewart03dba0b2011-08-22 16:32:45 -0700503void Device::SelectService(const ServiceRefPtr &service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700504 SLOG(Device, 2) << __func__ << ": "
505 << (service.get() ?
506 StringPrintf("%s (%s)",
507 service->UniqueName().c_str(),
508 service->friendly_name().c_str()) :
509 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000510
511 if (selected_service_.get() == service.get()) {
512 // No change to |selected_service_|. Return early to avoid
513 // changing its state.
514 return;
515 }
516
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800517 if (selected_service_.get()) {
518 if (selected_service_->state() != Service::kStateFailure) {
519 selected_service_->SetState(Service::kStateIdle);
520 }
Paul Stewart20b0a092012-05-22 20:39:57 -0700521 // Just in case the Device subclass has not already done so, make
522 // sure the previously selected service has its connection removed.
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800523 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700524 }
525 selected_service_ = service;
526}
527
528void Device::SetServiceState(Service::ConnectState state) {
529 if (selected_service_.get()) {
530 selected_service_->SetState(state);
531 }
532}
533
534void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
535 if (selected_service_.get()) {
536 selected_service_->SetFailure(failure_state);
537 }
538}
539
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400540void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) {
541 if (selected_service_.get()) {
542 selected_service_->SetFailureSilent(failure_state);
543 }
544}
545
Chris Masone34af2182011-08-22 11:59:36 -0700546string Device::SerializeIPConfigs(const string &suffix) {
547 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700548}
549
Paul Stewart2bf1d352011-12-06 15:02:55 -0800550bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
551 const string &value) {
552 string ip_version;
553 if (family == IPAddress::kFamilyIPv4) {
554 ip_version = kIPFlagVersion4;
555 } else if (family == IPAddress::kFamilyIPv6) {
556 ip_version = kIPFlagVersion6;
557 } else {
558 NOTIMPLEMENTED();
559 }
560 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
561 link_name_.c_str(), flag.c_str()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700562 SLOG(Device, 2) << "Writing " << value << " to flag file "
563 << flag_file.value();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800564 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
565 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
566 value.c_str(), flag_file.value().c_str());
567 return false;
568 }
569 return true;
570}
571
Paul Stewart6ff27f52012-07-11 06:51:41 -0700572void Device::ResetByteCounters() {
573 manager_->device_info()->GetByteCounts(
574 interface_index_, &receive_byte_offset_, &transmit_byte_offset_);
575 manager_->UpdateDevice(this);
576}
577
Paul Stewartd215af62012-04-24 23:25:50 -0700578bool Device::RestartPortalDetection() {
579 StopPortalDetection();
580 return StartPortalDetection();
581}
582
Paul Stewartc681fa02012-03-02 19:40:04 -0800583bool Device::RequestPortalDetection() {
584 if (!selected_service_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700585 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800586 << ": No selected service, so no need for portal check.";
587 return false;
588 }
589
590 if (!connection_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700591 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800592 << ": No connection, so no need for portal check.";
593 return false;
594 }
595
596 if (selected_service_->state() != Service::kStatePortal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700597 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800598 << ": Service is not in portal state. No need to start check.";
599 return false;
600 }
601
602 if (!connection_->is_default()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700603 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800604 << ": Service is not the default connection. Don't start check.";
605 return false;
606 }
607
608 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700609 SLOG(Device, 2) << FriendlyName()
610 << ": Portal detection is already running.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800611 return true;
612 }
613
614 return StartPortalDetection();
615}
616
Paul Stewart20088d82012-02-16 06:58:55 -0800617bool Device::StartPortalDetection() {
Paul Stewartd215af62012-04-24 23:25:50 -0700618 DCHECK(selected_service_.get());
619 if (selected_service_->IsPortalDetectionDisabled()) {
620 SLOG(Device, 2) << "Service " << selected_service_->friendly_name()
621 << ": Portal detection is disabled; "
622 << "marking service online.";
623 SetServiceConnectedState(Service::kStateOnline);
624 return false;
625 }
626
627 if (selected_service_->IsPortalDetectionAuto() &&
628 !manager_->IsPortalDetectionEnabled(technology())) {
Paul Stewart20088d82012-02-16 06:58:55 -0800629 // If portal detection is disabled for this technology, immediately set
630 // the service state to "Online".
Ben Chanfad4a0b2012-04-18 15:49:59 -0700631 SLOG(Device, 2) << "Device " << FriendlyName()
632 << ": Portal detection is disabled; "
633 << "marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800634 SetServiceConnectedState(Service::kStateOnline);
635 return false;
636 }
637
Paul Stewart20088d82012-02-16 06:58:55 -0800638 if (selected_service_->HasProxyConfig()) {
639 // Services with HTTP proxy configurations should not be checked by the
640 // connection manager, since we don't have the ability to evaluate
641 // arbitrary proxy configs and their possible credentials.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700642 SLOG(Device, 2) << "Device " << FriendlyName()
643 << ": Service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800644 SetServiceConnectedState(Service::kStateOnline);
645 return false;
646 }
647
648 portal_detector_.reset(new PortalDetector(connection_,
649 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500650 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800651 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
652 LOG(ERROR) << "Device " << FriendlyName()
653 << ": Portal detection failed to start: likely bad URL: "
654 << manager_->GetPortalCheckURL();
655 SetServiceConnectedState(Service::kStateOnline);
656 return false;
657 }
658
Ben Chanfad4a0b2012-04-18 15:49:59 -0700659 SLOG(Device, 2) << "Device " << FriendlyName()
660 << ": Portal detection has started.";
Paul Stewart20088d82012-02-16 06:58:55 -0800661 return true;
662}
663
664void Device::StopPortalDetection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700665 SLOG(Device, 2) << "Device " << FriendlyName()
666 << ": Portal detection has stopped.";
Paul Stewart20088d82012-02-16 06:58:55 -0800667 portal_detector_.reset();
668}
669
670void Device::SetServiceConnectedState(Service::ConnectState state) {
671 DCHECK(selected_service_.get());
672
673 if (!selected_service_.get()) {
674 LOG(ERROR) << FriendlyName() << ": "
675 << "Portal detection completed but no selected service exists!";
676 return;
677 }
678
679 if (!selected_service_->IsConnected()) {
680 LOG(ERROR) << FriendlyName() << ": "
681 << "Portal detection completed but selected service "
682 << selected_service_->UniqueName()
683 << " is in non-connected state.";
684 return;
685 }
686
Paul Stewartc681fa02012-03-02 19:40:04 -0800687 if (state == Service::kStatePortal && connection_->is_default() &&
688 manager_->GetPortalCheckInterval() != 0) {
689 CHECK(portal_detector_.get());
690 if (!portal_detector_->StartAfterDelay(
691 manager_->GetPortalCheckURL(),
692 manager_->GetPortalCheckInterval())) {
693 LOG(ERROR) << "Device " << FriendlyName()
694 << ": Portal detection failed to restart: likely bad URL: "
695 << manager_->GetPortalCheckURL();
696 SetServiceState(Service::kStateOnline);
697 portal_detector_.reset();
698 return;
699 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700700 SLOG(Device, 2) << "Device " << FriendlyName()
701 << ": Portal detection retrying.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800702 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700703 SLOG(Device, 2) << "Device " << FriendlyName()
704 << ": Portal will not retry.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800705 portal_detector_.reset();
706 }
707
Paul Stewart20088d82012-02-16 06:58:55 -0800708 SetServiceState(state);
709}
710
711void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
712 if (!result.final) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700713 SLOG(Device, 2) << "Device " << FriendlyName()
714 << ": Received non-final status: "
715 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800716 return;
717 }
718
Ben Chanfad4a0b2012-04-18 15:49:59 -0700719 SLOG(Device, 2) << "Device " << FriendlyName()
720 << ": Received final status: "
721 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800722
Thieu Le85e050b2012-03-13 15:04:38 -0700723 portal_attempts_to_online_ += result.num_attempts;
724
725 metrics()->SendEnumToUMA(
726 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
727 Metrics::PortalDetectionResultToEnum(result),
728 Metrics::kPortalResultMax);
729
Paul Stewart20088d82012-02-16 06:58:55 -0800730 if (result.status == PortalDetector::kStatusSuccess) {
731 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700732
733 metrics()->SendToUMA(
734 metrics()->GetFullMetricName(
735 Metrics::kMetricPortalAttemptsToOnline, technology()),
736 portal_attempts_to_online_,
737 Metrics::kMetricPortalAttemptsToOnlineMin,
738 Metrics::kMetricPortalAttemptsToOnlineMax,
739 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800740 } else {
741 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700742
743 metrics()->SendToUMA(
744 metrics()->GetFullMetricName(
745 Metrics::kMetricPortalAttempts, technology()),
746 result.num_attempts,
747 Metrics::kMetricPortalAttemptsMin,
748 Metrics::kMetricPortalAttemptsMax,
749 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800750 }
751}
752
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800753vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Jason Glasgow08afdff2012-04-03 10:22:26 -0400754 if (ipconfig_.get()) {
755 string id = ipconfig_->GetRpcIdentifier();
756 return vector<string>(1, id);
757 }
758 return vector<string>();
Chris Masone4e851612011-07-01 10:46:53 -0700759}
760
761string Device::GetRpcConnectionIdentifier() {
762 return adaptor_->GetRpcConnectionIdentifier();
763}
764
Paul Stewart6ff27f52012-07-11 06:51:41 -0700765uint64 Device::GetReceiveByteCount(Error */*error*/) {
766 uint64 rx_byte_count = 0, tx_byte_count = 0;
767 manager_->device_info()->GetByteCounts(
768 interface_index_, &rx_byte_count, &tx_byte_count);
769 return rx_byte_count - receive_byte_offset_;
770}
771
772uint64 Device::GetTransmitByteCount(Error */*error*/) {
773 uint64 rx_byte_count = 0, tx_byte_count = 0;
774 manager_->device_info()->GetByteCounts(
775 interface_index_, &rx_byte_count, &tx_byte_count);
776 return tx_byte_count - transmit_byte_offset_;
777}
778
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400779bool Device::IsUnderlyingDeviceEnabled() const {
780 return false;
781}
782
Eric Shienbrood9a245532012-03-07 14:20:39 -0500783// callback
784void Device::OnEnabledStateChanged(const ResultCallback &callback,
785 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700786 SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500787 if (error.IsSuccess()) {
788 enabled_ = enabled_pending_;
789 manager_->UpdateEnabledTechnologies();
790 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
791 adaptor_->UpdateEnabled();
792 }
Gary Morainbaeefdf2012-04-30 14:53:35 -0700793 enabled_pending_ = enabled_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500794 if (!callback.is_null())
795 callback.Run(error);
796}
797
798void Device::SetEnabled(bool enable) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700799 SLOG(Device, 2) << __func__ << "(" << enable << ")";
Jason Glasgow4a490792012-04-10 15:02:05 -0400800 Error error;
801 SetEnabledInternal(enable, false, &error, ResultCallback());
Jason Glasgow7234ec32012-05-23 16:01:21 -0400802 LOG_IF(ERROR, error.IsFailure() && !error.IsOngoing())
Jason Glasgow4a490792012-04-10 15:02:05 -0400803 << "Enabled failed, but no way to report the failure.";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500804}
805
806void Device::SetEnabledPersistent(bool enable,
807 Error *error,
808 const ResultCallback &callback) {
809 SetEnabledInternal(enable, true, error, callback);
810}
811
812void Device::SetEnabledInternal(bool enable,
813 bool persist,
814 Error *error,
815 const ResultCallback &callback) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400816 DCHECK(error);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700817 SLOG(Device, 2) << "Device " << link_name_ << " "
818 << (enable ? "starting" : "stopping");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500819 if (enable == enabled_) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400820 error->Reset();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500821 return;
822 }
823
824 if (enabled_pending_ == enable) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400825 Error::PopulateAndLog(error, Error::kInProgress,
826 "Enable operation already in progress");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500827 return;
828 }
829
830 if (persist) {
831 enabled_persistent_ = enable;
Darin Petkove7c6ad32012-06-29 10:22:09 +0200832 manager_->UpdateDevice(this);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500833 }
834
835 enabled_pending_ = enable;
836 EnabledStateChangedCallback enabled_callback =
837 Bind(&Device::OnEnabledStateChanged,
838 weak_ptr_factory_.GetWeakPtr(), callback);
839 if (enable) {
840 running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500841 Start(error, enabled_callback);
842 } else {
843 running_ = false;
844 DestroyIPConfig(); // breaks a reference cycle
845 SelectService(NULL); // breaks a reference cycle
846 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700847 SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ "
848 << (ipconfig_ ? "is set." : "is not set.");
849 SLOG(Device, 3) << "Device " << link_name_ << " connection_ "
850 << (connection_ ? "is set." : "is not set.");
851 SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ "
852 << (selected_service_ ? "is set." : "is not set.");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500853 Stop(error, enabled_callback);
854 }
855}
856
Paul Stewart75897df2011-04-27 09:05:53 -0700857} // namespace shill