blob: 3ace719d78bd70a031b58f029d2489ed40d7ce19 [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"
Thieu Lefb46caf2012-03-08 11:57:15 -080034#include "shill/routing_table.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070035#include "shill/rtnl_handler.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070036#include "shill/scope_logger.h"
Chris Masone2b105542011-06-22 10:58:09 -070037#include "shill/service.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070038#include "shill/store_interface.h"
Gaurav Shah435de2c2011-11-17 19:01:07 -080039#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070040
Eric Shienbrood3e20a232012-02-16 11:35:56 -050041using base::Bind;
Chris Masone5dec5f42011-07-22 14:07:55 -070042using base::StringPrintf;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070043using std::string;
44using std::vector;
45
Paul Stewart75897df2011-04-27 09:05:53 -070046namespace shill {
Chris Masone5dec5f42011-07-22 14:07:55 -070047
48// static
Paul Stewart2bf1d352011-12-06 15:02:55 -080049const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s";
50// static
51const char Device::kIPFlagVersion4[] = "ipv4";
52// static
53const char Device::kIPFlagVersion6[] = "ipv6";
54// static
55const char Device::kIPFlagDisableIPv6[] = "disable_ipv6";
56// static
57const char Device::kIPFlagUseTempAddr[] = "use_tempaddr";
58// static
59const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2";
Paul Stewartc8f4bef2011-12-13 09:45:51 -080060// static
61const char Device::kIPFlagReversePathFilter[] = "rp_filter";
62// static
63const char Device::kIPFlagReversePathFilterEnabled[] = "1";
64// static
65const char Device::kIPFlagReversePathFilterLooseMode[] = "2";
Paul Stewart2bf1d352011-12-06 15:02:55 -080066
67// static
Chris Masone5dec5f42011-07-22 14:07:55 -070068const char Device::kStoragePowered[] = "Powered";
69
70// static
71const char Device::kStorageIPConfigs[] = "IPConfigs";
72
Paul Stewart75897df2011-04-27 09:05:53 -070073Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070074 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080075 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070076 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070077 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070078 const string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080079 int interface_index,
80 Technology::Identifier technology)
Eric Shienbrood9a245532012-03-07 14:20:39 -050081 : enabled_(false),
82 enabled_persistent_(true),
83 enabled_pending_(enabled_),
Chris Masoneb925cc82011-06-22 15:39:57 -070084 reconnect_(true),
Chris Masone626719f2011-08-18 16:58:48 -070085 hardware_address_(address),
mukesh agrawalf60e4062011-05-27 13:13:41 -070086 interface_index_(interface_index),
87 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070088 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070089 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070090 control_interface_(control_interface),
91 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080092 metrics_(metrics),
Chris Masone7df0c672011-07-15 10:24:54 -070093 manager_(manager),
Eric Shienbrood9a245532012-03-07 14:20:39 -050094 weak_ptr_factory_(this),
Darin Petkov77cb6812011-08-15 16:19:41 -070095 adaptor_(control_interface->CreateDeviceAdaptor(this)),
Eric Shienbrood9a245532012-03-07 14:20:39 -050096 portal_detector_callback_(Bind(&Device::PortalDetectorCallback,
97 weak_ptr_factory_.GetWeakPtr())),
Gaurav Shah435de2c2011-11-17 19:01:07 -080098 technology_(technology),
Thieu Le85e050b2012-03-13 15:04:38 -070099 portal_attempts_to_online_(0),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700100 dhcp_provider_(DHCPProvider::GetInstance()),
Thieu Lefb46caf2012-03-08 11:57:15 -0800101 routing_table_(RoutingTable::GetInstance()),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700102 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700103 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -0700104
105 // flimflam::kBgscanMethodProperty: Registered in WiFi
106 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
107 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
108
109 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
110 // flimflam::kCarrierProperty: Registered in Cellular
111 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -0700112 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700113 // flimflam::kImeiProperty: Registered in Cellular
114 // flimflam::kImsiProperty: Registered in Cellular
115 // flimflam::kManufacturerProperty: Registered in Cellular
116 // flimflam::kMdnProperty: Registered in Cellular
117 // flimflam::kMeidProperty: Registered in Cellular
118 // flimflam::kMinProperty: Registered in Cellular
119 // flimflam::kModelIDProperty: Registered in Cellular
120 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
121 // flimflam::kHardwareRevisionProperty: Registered in Cellular
122 // flimflam::kPRLVersionProperty: Registered in Cellular
123 // flimflam::kSIMLockStatusProperty: Registered in Cellular
124 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -0700125 // flimflam::kDBusConnectionProperty: Registered in Cellular
126 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700127
Jason Glasgowe8334fd2012-03-30 16:02:37 -0400128 store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
Jason Glasgow08afdff2012-04-03 10:22:26 -0400129 HelpRegisterConstDerivedRpcIdentifiers(flimflam::kIPConfigsProperty,
130 &Device::AvailableIPConfigs);
Chris Masone27c4aa52011-07-02 13:10:14 -0700131 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500132 store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_);
Jason Glasgowb5790052012-01-27 01:03:52 -0500133 HelpRegisterDerivedString(flimflam::kTypeProperty,
134 &Device::GetTechnologyString,
135 NULL);
136
Chris Masoneb925cc82011-06-22 15:39:57 -0700137 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700138 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700139
Chris Masone4e851612011-07-01 10:46:53 -0700140 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700141 // known_properties_.push_back(flimflam::kNetworksProperty);
142
Chris Masone4d42df82011-07-02 17:09:39 -0700143 // flimflam::kScanningProperty: Registered in WiFi, Cellular
144 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
145
146 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Ben Chanfad4a0b2012-04-18 15:49:59 -0700147 SLOG(Device, 2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -0700148}
149
150Device::~Device() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700151 SLOG(Device, 2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700152}
153
mukesh agrawal1830fa12011-09-26 14:31:40 -0700154bool Device::TechnologyIs(const Technology::Identifier /*type*/) const {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700155 return false;
156}
157
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700158void Device::LinkEvent(unsigned flags, unsigned change) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700159 SLOG(Device, 2) << "Device " << link_name_
160 << std::showbase << std::hex
161 << " flags " << flags << " changed " << change
162 << std::dec << std::noshowbase;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700163}
164
Darin Petkovc0865312011-09-16 15:31:20 -0700165void Device::Scan(Error *error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700166 SLOG(Device, 2) << "Device " << link_name_ << " scan requested.";
Paul Stewartbe005172011-11-02 18:10:29 -0700167 Error::PopulateAndLog(error, Error::kNotSupported,
168 "Device doesn't support scan.");
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700169}
170
Eric Shienbrood9a245532012-03-07 14:20:39 -0500171void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error,
172 const ResultCallback &/*callback*/) {
173 Error::PopulateAndLog(error, Error::kNotSupported,
Paul Stewartbe005172011-11-02 18:10:29 -0700174 "Device doesn't support network registration.");
Darin Petkov9ae310f2011-08-30 15:41:13 -0700175}
176
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100177void Device::RequirePIN(
Eric Shienbrood9a245532012-03-07 14:20:39 -0500178 const string &/*pin*/, bool /*require*/,
179 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700180 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500181 Error::PopulateAndLog(error, Error::kNotSupported,
182 "Device doesn't support RequirePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700183}
184
Eric Shienbrood9a245532012-03-07 14:20:39 -0500185void Device::EnterPIN(const string &/*pin*/,
186 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700187 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500188 Error::PopulateAndLog(error, Error::kNotSupported,
189 "Device doesn't support EnterPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700190}
191
mukesh agrawal1830fa12011-09-26 14:31:40 -0700192void Device::UnblockPIN(const string &/*unblock_code*/,
193 const string &/*pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500194 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700195 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500196 Error::PopulateAndLog(error, Error::kNotSupported,
197 "Device doesn't support UnblockPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700198}
199
mukesh agrawal1830fa12011-09-26 14:31:40 -0700200void Device::ChangePIN(const string &/*old_pin*/,
201 const string &/*new_pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500202 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700203 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500204 Error::PopulateAndLog(error, Error::kNotSupported,
205 "Device doesn't support ChangePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700206}
207
Paul Stewart2bf1d352011-12-06 15:02:55 -0800208void Device::DisableIPv6() {
209 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
210}
211
212void Device::EnableIPv6() {
213 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
214}
215
216void Device::EnableIPv6Privacy() {
217 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
218 kIPFlagUseTempAddrUsedAndDefault);
219}
220
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800221void Device::DisableReversePathFilter() {
222 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
223 // for IPv6. crosbug.com/24228
224 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
225 kIPFlagReversePathFilterLooseMode);
226}
227
228void Device::EnableReversePathFilter() {
229 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
230 kIPFlagReversePathFilterEnabled);
231}
232
Gaurav Shah435de2c2011-11-17 19:01:07 -0800233bool Device::IsConnected() const {
234 if (selected_service_)
235 return selected_service_->IsConnected();
236 return false;
237}
238
Chris Masone27c4aa52011-07-02 13:10:14 -0700239string Device::GetRpcIdentifier() {
240 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700241}
242
Chris Masone5dec5f42011-07-22 14:07:55 -0700243string Device::GetStorageIdentifier() {
244 string id = GetRpcIdentifier();
245 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700246 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700247 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700248 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700249 return id;
250}
251
Jason Glasgowb5790052012-01-27 01:03:52 -0500252string Device::GetTechnologyString(Error */*error*/) {
253 return Technology::NameFromIdentifier(technology());
254}
255
Chris Masone19e30402011-07-19 15:48:47 -0700256const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700257 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700258}
259
Chris Masone19e30402011-07-19 15:48:47 -0700260const string& Device::UniqueName() const {
261 return unique_id_;
262}
263
Chris Masone5dec5f42011-07-22 14:07:55 -0700264bool Device::Load(StoreInterface *storage) {
265 const string id = GetStorageIdentifier();
266 if (!storage->ContainsGroup(id)) {
267 LOG(WARNING) << "Device is not available in the persistent store: " << id;
268 return false;
269 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500270 enabled_persistent_ = true;
271 storage->GetBool(id, kStoragePowered, &enabled_persistent_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700272 // TODO(cmasone): What does it mean to load an IPConfig identifier??
273 return true;
274}
275
276bool Device::Save(StoreInterface *storage) {
277 const string id = GetStorageIdentifier();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500278 storage->SetBool(id, kStoragePowered, enabled_persistent_);
Chris Masone34af2182011-08-22 11:59:36 -0700279 if (ipconfig_.get()) {
280 // The _0 is an index into the list of IPConfigs that this device might
281 // have. We only have one IPConfig right now, and I hope to never have
282 // to support more, as sleffler indicates that associating IPConfigs
283 // with devices is wrong and due to be changed in flimflam anyhow.
284 string suffix = hardware_address_ + "_0";
285 ipconfig_->Save(storage, suffix);
286 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
287 }
Chris Masone5dec5f42011-07-22 14:07:55 -0700288 return true;
289}
290
Darin Petkovafa6fc42011-06-21 16:21:08 -0700291void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800292 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700293 if (ipconfig_.get()) {
294 ipconfig_->ReleaseIP();
295 ipconfig_ = NULL;
296 }
Paul Stewarte6132022011-08-16 09:11:02 -0700297 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700298}
299
Paul Stewart2bf1d352011-12-06 15:02:55 -0800300bool Device::AcquireIPConfig() {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700301 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800302 EnableIPv6();
Paul Stewartd32f4842012-01-11 16:08:13 -0800303 ipconfig_ = dhcp_provider_->CreateConfig(link_name_, manager_->GetHostName());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500304 ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated,
305 weak_ptr_factory_.GetWeakPtr()));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700306 return ipconfig_->RequestIP();
307}
308
Jason Glasgowb5790052012-01-27 01:03:52 -0500309void Device::HelpRegisterDerivedString(
310 const string &name,
311 string(Device::*get)(Error *error),
312 void(Device::*set)(const string &value, Error *error)) {
313 store_.RegisterDerivedString(
314 name,
315 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
316}
317
mukesh agrawalffa3d042011-10-06 15:26:10 -0700318void Device::HelpRegisterDerivedStrings(
319 const string &name,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500320 Strings(Device::*get)(Error *error),
321 void(Device::*set)(const Strings &value, Error *error)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700322 store_.RegisterDerivedStrings(
323 name,
324 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700325}
326
Jason Glasgow08afdff2012-04-03 10:22:26 -0400327void Device::HelpRegisterConstDerivedRpcIdentifiers(
328 const string &name,
329 RpcIdentifiers(Device::*get)(Error *)) {
330 store_.RegisterDerivedRpcIdentifiers(
331 name,
332 RpcIdentifiersAccessor(
333 new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL)));
334}
335
Darin Petkov79d74c92012-03-07 17:20:32 +0100336void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700337 SLOG(Device, 2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700338 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700339 CreateConnection();
340 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800341 // SetConnection must occur after the UpdateFromIPConfig so the
342 // service can use the values derived from the connection.
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800343 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800344 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800345 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800346 // The service state change needs to happen last, so that at the
347 // time we report the state change to the manager, the service
348 // has its connection.
349 SetServiceState(Service::kStateConnected);
Thieu Le85e050b2012-03-13 15:04:38 -0700350 portal_attempts_to_online_ = 0;
Paul Stewart20088d82012-02-16 06:58:55 -0800351 // Subtle: Start portal detection after transitioning the service
352 // to the Connected state because this call may immediately transition
353 // to the Online state.
354 StartPortalDetection();
Paul Stewarte6132022011-08-16 09:11:02 -0700355 } else {
356 // TODO(pstew): This logic gets more complex when multiple IPConfig types
357 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700358 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700359 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700360 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700361}
362
Paul Stewarte6132022011-08-16 09:11:02 -0700363void Device::CreateConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700364 SLOG(Device, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -0700365 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700366 connection_ = new Connection(interface_index_,
367 link_name_,
Paul Stewarte00600e2012-03-16 07:08:00 -0700368 technology_,
Paul Stewart9a908082011-08-31 12:18:48 -0700369 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700370 }
371}
372
373void Device::DestroyConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700374 SLOG(Device, 2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800375 StopPortalDetection();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800376 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800377 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800378 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800379 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700380}
381
Paul Stewart03dba0b2011-08-22 16:32:45 -0700382void Device::SelectService(const ServiceRefPtr &service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700383 SLOG(Device, 2) << __func__ << ": "
384 << (service.get() ?
385 StringPrintf("%s (%s)",
386 service->UniqueName().c_str(),
387 service->friendly_name().c_str()) :
388 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000389
390 if (selected_service_.get() == service.get()) {
391 // No change to |selected_service_|. Return early to avoid
392 // changing its state.
393 return;
394 }
395
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800396 if (selected_service_.get()) {
397 if (selected_service_->state() != Service::kStateFailure) {
398 selected_service_->SetState(Service::kStateIdle);
399 }
400 // TODO(pstew): We need to revisit the model here: should the Device
401 // subclass be responsible for calling DestroyIPConfig() (which would
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800402 // trigger DestroyConnection() and Service::SetConnection(NULL))?
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800403 // Ethernet does, but WiFi currently does not. crosbug.com/23929
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800404 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700405 }
406 selected_service_ = service;
407}
408
409void Device::SetServiceState(Service::ConnectState state) {
410 if (selected_service_.get()) {
411 selected_service_->SetState(state);
412 }
413}
414
415void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
416 if (selected_service_.get()) {
417 selected_service_->SetFailure(failure_state);
418 }
419}
420
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400421void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) {
422 if (selected_service_.get()) {
423 selected_service_->SetFailureSilent(failure_state);
424 }
425}
426
Chris Masone34af2182011-08-22 11:59:36 -0700427string Device::SerializeIPConfigs(const string &suffix) {
428 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700429}
430
Paul Stewart2bf1d352011-12-06 15:02:55 -0800431bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
432 const string &value) {
433 string ip_version;
434 if (family == IPAddress::kFamilyIPv4) {
435 ip_version = kIPFlagVersion4;
436 } else if (family == IPAddress::kFamilyIPv6) {
437 ip_version = kIPFlagVersion6;
438 } else {
439 NOTIMPLEMENTED();
440 }
441 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
442 link_name_.c_str(), flag.c_str()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700443 SLOG(Device, 2) << "Writing " << value << " to flag file "
444 << flag_file.value();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800445 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
446 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
447 value.c_str(), flag_file.value().c_str());
448 return false;
449 }
450 return true;
451}
452
Paul Stewartc681fa02012-03-02 19:40:04 -0800453bool Device::RequestPortalDetection() {
454 if (!selected_service_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700455 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800456 << ": No selected service, so no need for portal check.";
457 return false;
458 }
459
460 if (!connection_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700461 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800462 << ": No connection, so no need for portal check.";
463 return false;
464 }
465
466 if (selected_service_->state() != Service::kStatePortal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700467 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800468 << ": Service is not in portal state. No need to start check.";
469 return false;
470 }
471
472 if (!connection_->is_default()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700473 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800474 << ": Service is not the default connection. Don't start check.";
475 return false;
476 }
477
478 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700479 SLOG(Device, 2) << FriendlyName()
480 << ": Portal detection is already running.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800481 return true;
482 }
483
484 return StartPortalDetection();
485}
486
Paul Stewart20088d82012-02-16 06:58:55 -0800487bool Device::StartPortalDetection() {
488 if (!manager_->IsPortalDetectionEnabled(technology())) {
489 // If portal detection is disabled for this technology, immediately set
490 // the service state to "Online".
Ben Chanfad4a0b2012-04-18 15:49:59 -0700491 SLOG(Device, 2) << "Device " << FriendlyName()
492 << ": Portal detection is disabled; "
493 << "marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800494 SetServiceConnectedState(Service::kStateOnline);
495 return false;
496 }
497
498 DCHECK(selected_service_.get());
499 if (selected_service_->HasProxyConfig()) {
500 // Services with HTTP proxy configurations should not be checked by the
501 // connection manager, since we don't have the ability to evaluate
502 // arbitrary proxy configs and their possible credentials.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700503 SLOG(Device, 2) << "Device " << FriendlyName()
504 << ": Service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800505 SetServiceConnectedState(Service::kStateOnline);
506 return false;
507 }
508
509 portal_detector_.reset(new PortalDetector(connection_,
510 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500511 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800512 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
513 LOG(ERROR) << "Device " << FriendlyName()
514 << ": Portal detection failed to start: likely bad URL: "
515 << manager_->GetPortalCheckURL();
516 SetServiceConnectedState(Service::kStateOnline);
517 return false;
518 }
519
Ben Chanfad4a0b2012-04-18 15:49:59 -0700520 SLOG(Device, 2) << "Device " << FriendlyName()
521 << ": Portal detection has started.";
Paul Stewart20088d82012-02-16 06:58:55 -0800522 return true;
523}
524
525void Device::StopPortalDetection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700526 SLOG(Device, 2) << "Device " << FriendlyName()
527 << ": Portal detection has stopped.";
Paul Stewart20088d82012-02-16 06:58:55 -0800528 portal_detector_.reset();
529}
530
531void Device::SetServiceConnectedState(Service::ConnectState state) {
532 DCHECK(selected_service_.get());
533
534 if (!selected_service_.get()) {
535 LOG(ERROR) << FriendlyName() << ": "
536 << "Portal detection completed but no selected service exists!";
537 return;
538 }
539
540 if (!selected_service_->IsConnected()) {
541 LOG(ERROR) << FriendlyName() << ": "
542 << "Portal detection completed but selected service "
543 << selected_service_->UniqueName()
544 << " is in non-connected state.";
545 return;
546 }
547
Paul Stewartc681fa02012-03-02 19:40:04 -0800548 if (state == Service::kStatePortal && connection_->is_default() &&
549 manager_->GetPortalCheckInterval() != 0) {
550 CHECK(portal_detector_.get());
551 if (!portal_detector_->StartAfterDelay(
552 manager_->GetPortalCheckURL(),
553 manager_->GetPortalCheckInterval())) {
554 LOG(ERROR) << "Device " << FriendlyName()
555 << ": Portal detection failed to restart: likely bad URL: "
556 << manager_->GetPortalCheckURL();
557 SetServiceState(Service::kStateOnline);
558 portal_detector_.reset();
559 return;
560 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700561 SLOG(Device, 2) << "Device " << FriendlyName()
562 << ": Portal detection retrying.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800563 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700564 SLOG(Device, 2) << "Device " << FriendlyName()
565 << ": Portal will not retry.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800566 portal_detector_.reset();
567 }
568
Paul Stewart20088d82012-02-16 06:58:55 -0800569 SetServiceState(state);
570}
571
572void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
573 if (!result.final) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700574 SLOG(Device, 2) << "Device " << FriendlyName()
575 << ": Received non-final status: "
576 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800577 return;
578 }
579
Ben Chanfad4a0b2012-04-18 15:49:59 -0700580 SLOG(Device, 2) << "Device " << FriendlyName()
581 << ": Received final status: "
582 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800583
Thieu Le85e050b2012-03-13 15:04:38 -0700584 portal_attempts_to_online_ += result.num_attempts;
585
586 metrics()->SendEnumToUMA(
587 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
588 Metrics::PortalDetectionResultToEnum(result),
589 Metrics::kPortalResultMax);
590
Paul Stewart20088d82012-02-16 06:58:55 -0800591 if (result.status == PortalDetector::kStatusSuccess) {
592 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700593
594 metrics()->SendToUMA(
595 metrics()->GetFullMetricName(
596 Metrics::kMetricPortalAttemptsToOnline, technology()),
597 portal_attempts_to_online_,
598 Metrics::kMetricPortalAttemptsToOnlineMin,
599 Metrics::kMetricPortalAttemptsToOnlineMax,
600 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800601 } else {
602 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700603
604 metrics()->SendToUMA(
605 metrics()->GetFullMetricName(
606 Metrics::kMetricPortalAttempts, technology()),
607 result.num_attempts,
608 Metrics::kMetricPortalAttemptsMin,
609 Metrics::kMetricPortalAttemptsMax,
610 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800611 }
612}
613
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800614vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Jason Glasgow08afdff2012-04-03 10:22:26 -0400615 if (ipconfig_.get()) {
616 string id = ipconfig_->GetRpcIdentifier();
617 return vector<string>(1, id);
618 }
619 return vector<string>();
Chris Masone4e851612011-07-01 10:46:53 -0700620}
621
622string Device::GetRpcConnectionIdentifier() {
623 return adaptor_->GetRpcConnectionIdentifier();
624}
625
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400626bool Device::IsUnderlyingDeviceEnabled() const {
627 return false;
628}
629
Eric Shienbrood9a245532012-03-07 14:20:39 -0500630// callback
631void Device::OnEnabledStateChanged(const ResultCallback &callback,
632 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700633 SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500634 if (error.IsSuccess()) {
635 enabled_ = enabled_pending_;
636 manager_->UpdateEnabledTechnologies();
637 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
638 adaptor_->UpdateEnabled();
639 }
640 if (!callback.is_null())
641 callback.Run(error);
642}
643
644void Device::SetEnabled(bool enable) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700645 SLOG(Device, 2) << __func__ << "(" << enable << ")";
Jason Glasgow4a490792012-04-10 15:02:05 -0400646 Error error;
647 SetEnabledInternal(enable, false, &error, ResultCallback());
648 LOG_IF(ERROR, error.IsFailure())
649 << "Enabled failed, but no way to report the failure.";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500650}
651
652void Device::SetEnabledPersistent(bool enable,
653 Error *error,
654 const ResultCallback &callback) {
655 SetEnabledInternal(enable, true, error, callback);
656}
657
658void Device::SetEnabledInternal(bool enable,
659 bool persist,
660 Error *error,
661 const ResultCallback &callback) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400662 DCHECK(error);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700663 SLOG(Device, 2) << "Device " << link_name_ << " "
664 << (enable ? "starting" : "stopping");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500665 if (enable == enabled_) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400666 error->Reset();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500667 return;
668 }
669
670 if (enabled_pending_ == enable) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400671 Error::PopulateAndLog(error, Error::kInProgress,
672 "Enable operation already in progress");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500673 return;
674 }
675
676 if (persist) {
677 enabled_persistent_ = enable;
678 manager_->SaveActiveProfile();
679 }
680
681 enabled_pending_ = enable;
682 EnabledStateChangedCallback enabled_callback =
683 Bind(&Device::OnEnabledStateChanged,
684 weak_ptr_factory_.GetWeakPtr(), callback);
685 if (enable) {
686 running_ = true;
687 routing_table_->FlushRoutes(interface_index());
688 Start(error, enabled_callback);
689 } else {
690 running_ = false;
691 DestroyIPConfig(); // breaks a reference cycle
692 SelectService(NULL); // breaks a reference cycle
693 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700694 SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ "
695 << (ipconfig_ ? "is set." : "is not set.");
696 SLOG(Device, 3) << "Device " << link_name_ << " connection_ "
697 << (connection_ ? "is set." : "is not set.");
698 SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ "
699 << (selected_service_ ? "is set." : "is not set.");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500700 Stop(error, enabled_callback);
701 }
702}
703
Paul Stewart75897df2011-04-27 09:05:53 -0700704} // namespace shill