blob: 70825832b05e15338acf3ac43ddf4712b7f943b7 [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"
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()),
Thieu Lefb46caf2012-03-08 11:57:15 -0800100 routing_table_(RoutingTable::GetInstance()),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700101 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700102 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -0700103
104 // flimflam::kBgscanMethodProperty: Registered in WiFi
105 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
106 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
107
108 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
109 // flimflam::kCarrierProperty: Registered in Cellular
110 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -0700111 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700112 // flimflam::kImeiProperty: Registered in Cellular
113 // flimflam::kImsiProperty: Registered in Cellular
114 // flimflam::kManufacturerProperty: Registered in Cellular
115 // flimflam::kMdnProperty: Registered in Cellular
116 // flimflam::kMeidProperty: Registered in Cellular
117 // flimflam::kMinProperty: Registered in Cellular
118 // flimflam::kModelIDProperty: Registered in Cellular
119 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
120 // flimflam::kHardwareRevisionProperty: Registered in Cellular
121 // flimflam::kPRLVersionProperty: Registered in Cellular
122 // flimflam::kSIMLockStatusProperty: Registered in Cellular
123 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -0700124 // flimflam::kDBusConnectionProperty: Registered in Cellular
125 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700126
Chris Masoneb925cc82011-06-22 15:39:57 -0700127 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700128 // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
129 HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty,
130 &Device::AvailableIPConfigs,
131 NULL);
132 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500133 store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_);
Jason Glasgowb5790052012-01-27 01:03:52 -0500134 HelpRegisterDerivedString(flimflam::kTypeProperty,
135 &Device::GetTechnologyString,
136 NULL);
137
Chris Masoneb925cc82011-06-22 15:39:57 -0700138 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700139 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700140
Chris Masone4e851612011-07-01 10:46:53 -0700141 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700142 // known_properties_.push_back(flimflam::kNetworksProperty);
143
Chris Masone4d42df82011-07-02 17:09:39 -0700144 // flimflam::kScanningProperty: Registered in WiFi, Cellular
145 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
146
147 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -0700148 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -0700149}
150
151Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700152 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700153}
154
mukesh agrawal1830fa12011-09-26 14:31:40 -0700155bool Device::TechnologyIs(const Technology::Identifier /*type*/) const {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700156 return false;
157}
158
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700159void Device::LinkEvent(unsigned flags, unsigned change) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700160 VLOG(2) << "Device " << link_name_
161 << std::showbase << std::hex
162 << " flags " << flags << " changed " << change
163 << std::dec << std::noshowbase;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700164}
165
Darin Petkovc0865312011-09-16 15:31:20 -0700166void Device::Scan(Error *error) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700167 VLOG(2) << "Device " << link_name_ << " scan requested.";
Paul Stewartbe005172011-11-02 18:10:29 -0700168 Error::PopulateAndLog(error, Error::kNotSupported,
169 "Device doesn't support scan.");
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700170}
171
Eric Shienbrood9a245532012-03-07 14:20:39 -0500172void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error,
173 const ResultCallback &/*callback*/) {
174 Error::PopulateAndLog(error, Error::kNotSupported,
Paul Stewartbe005172011-11-02 18:10:29 -0700175 "Device doesn't support network registration.");
Darin Petkov9ae310f2011-08-30 15:41:13 -0700176}
177
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100178void Device::RequirePIN(
Eric Shienbrood9a245532012-03-07 14:20:39 -0500179 const string &/*pin*/, bool /*require*/,
180 Error *error, const ResultCallback &/*callback*/) {
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100181 VLOG(2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500182 Error::PopulateAndLog(error, Error::kNotSupported,
183 "Device doesn't support RequirePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700184}
185
Eric Shienbrood9a245532012-03-07 14:20:39 -0500186void Device::EnterPIN(const string &/*pin*/,
187 Error *error, const ResultCallback &/*callback*/) {
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100188 VLOG(2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500189 Error::PopulateAndLog(error, Error::kNotSupported,
190 "Device doesn't support EnterPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700191}
192
mukesh agrawal1830fa12011-09-26 14:31:40 -0700193void Device::UnblockPIN(const string &/*unblock_code*/,
194 const string &/*pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500195 Error *error, const ResultCallback &/*callback*/) {
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100196 VLOG(2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500197 Error::PopulateAndLog(error, Error::kNotSupported,
198 "Device doesn't support UnblockPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700199}
200
mukesh agrawal1830fa12011-09-26 14:31:40 -0700201void Device::ChangePIN(const string &/*old_pin*/,
202 const string &/*new_pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500203 Error *error, const ResultCallback &/*callback*/) {
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100204 VLOG(2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500205 Error::PopulateAndLog(error, Error::kNotSupported,
206 "Device doesn't support ChangePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700207}
208
Paul Stewart2bf1d352011-12-06 15:02:55 -0800209void Device::DisableIPv6() {
210 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
211}
212
213void Device::EnableIPv6() {
214 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
215}
216
217void Device::EnableIPv6Privacy() {
218 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
219 kIPFlagUseTempAddrUsedAndDefault);
220}
221
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800222void Device::DisableReversePathFilter() {
223 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
224 // for IPv6. crosbug.com/24228
225 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
226 kIPFlagReversePathFilterLooseMode);
227}
228
229void Device::EnableReversePathFilter() {
230 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
231 kIPFlagReversePathFilterEnabled);
232}
233
Gaurav Shah435de2c2011-11-17 19:01:07 -0800234bool Device::IsConnected() const {
235 if (selected_service_)
236 return selected_service_->IsConnected();
237 return false;
238}
239
Chris Masone27c4aa52011-07-02 13:10:14 -0700240string Device::GetRpcIdentifier() {
241 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700242}
243
Chris Masone5dec5f42011-07-22 14:07:55 -0700244string Device::GetStorageIdentifier() {
245 string id = GetRpcIdentifier();
246 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700247 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700248 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700249 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700250 return id;
251}
252
Jason Glasgowb5790052012-01-27 01:03:52 -0500253string Device::GetTechnologyString(Error */*error*/) {
254 return Technology::NameFromIdentifier(technology());
255}
256
Chris Masone19e30402011-07-19 15:48:47 -0700257const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700258 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700259}
260
Chris Masone19e30402011-07-19 15:48:47 -0700261const string& Device::UniqueName() const {
262 return unique_id_;
263}
264
Chris Masone5dec5f42011-07-22 14:07:55 -0700265bool Device::Load(StoreInterface *storage) {
266 const string id = GetStorageIdentifier();
267 if (!storage->ContainsGroup(id)) {
268 LOG(WARNING) << "Device is not available in the persistent store: " << id;
269 return false;
270 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500271 enabled_persistent_ = true;
272 storage->GetBool(id, kStoragePowered, &enabled_persistent_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700273 // TODO(cmasone): What does it mean to load an IPConfig identifier??
274 return true;
275}
276
277bool Device::Save(StoreInterface *storage) {
278 const string id = GetStorageIdentifier();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500279 storage->SetBool(id, kStoragePowered, enabled_persistent_);
Chris Masone34af2182011-08-22 11:59:36 -0700280 if (ipconfig_.get()) {
281 // The _0 is an index into the list of IPConfigs that this device might
282 // have. We only have one IPConfig right now, and I hope to never have
283 // to support more, as sleffler indicates that associating IPConfigs
284 // with devices is wrong and due to be changed in flimflam anyhow.
285 string suffix = hardware_address_ + "_0";
286 ipconfig_->Save(storage, suffix);
287 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
288 }
Chris Masone5dec5f42011-07-22 14:07:55 -0700289 return true;
290}
291
Darin Petkovafa6fc42011-06-21 16:21:08 -0700292void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800293 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700294 if (ipconfig_.get()) {
295 ipconfig_->ReleaseIP();
296 ipconfig_ = NULL;
297 }
Paul Stewarte6132022011-08-16 09:11:02 -0700298 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700299}
300
Paul Stewart2bf1d352011-12-06 15:02:55 -0800301bool Device::AcquireIPConfig() {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700302 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800303 EnableIPv6();
Paul Stewartd32f4842012-01-11 16:08:13 -0800304 ipconfig_ = dhcp_provider_->CreateConfig(link_name_, manager_->GetHostName());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500305 ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated,
306 weak_ptr_factory_.GetWeakPtr()));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700307 return ipconfig_->RequestIP();
308}
309
Jason Glasgowb5790052012-01-27 01:03:52 -0500310void Device::HelpRegisterDerivedString(
311 const string &name,
312 string(Device::*get)(Error *error),
313 void(Device::*set)(const string &value, Error *error)) {
314 store_.RegisterDerivedString(
315 name,
316 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
317}
318
mukesh agrawalffa3d042011-10-06 15:26:10 -0700319void Device::HelpRegisterDerivedStrings(
320 const string &name,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500321 Strings(Device::*get)(Error *error),
322 void(Device::*set)(const Strings &value, Error *error)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700323 store_.RegisterDerivedStrings(
324 name,
325 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700326}
327
Darin Petkov79d74c92012-03-07 17:20:32 +0100328void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700329 VLOG(2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700330 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700331 CreateConnection();
332 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800333 // SetConnection must occur after the UpdateFromIPConfig so the
334 // service can use the values derived from the connection.
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800335 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800336 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800337 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800338 // The service state change needs to happen last, so that at the
339 // time we report the state change to the manager, the service
340 // has its connection.
341 SetServiceState(Service::kStateConnected);
Thieu Le85e050b2012-03-13 15:04:38 -0700342 portal_attempts_to_online_ = 0;
Paul Stewart20088d82012-02-16 06:58:55 -0800343 // Subtle: Start portal detection after transitioning the service
344 // to the Connected state because this call may immediately transition
345 // to the Online state.
346 StartPortalDetection();
Paul Stewarte6132022011-08-16 09:11:02 -0700347 } else {
348 // TODO(pstew): This logic gets more complex when multiple IPConfig types
349 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700350 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700351 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700352 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700353}
354
Paul Stewarte6132022011-08-16 09:11:02 -0700355void Device::CreateConnection() {
356 VLOG(2) << __func__;
357 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700358 connection_ = new Connection(interface_index_,
359 link_name_,
Paul Stewarte00600e2012-03-16 07:08:00 -0700360 technology_,
Paul Stewart9a908082011-08-31 12:18:48 -0700361 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700362 }
363}
364
365void Device::DestroyConnection() {
366 VLOG(2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800367 StopPortalDetection();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800368 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800369 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800370 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800371 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700372}
373
Paul Stewart03dba0b2011-08-22 16:32:45 -0700374void Device::SelectService(const ServiceRefPtr &service) {
Paul Stewarta2b8cd12011-08-25 14:55:32 -0700375 VLOG(2) << __func__ << ": "
mukesh agrawal15908392011-11-16 18:29:25 +0000376 << (service.get() ?
377 StringPrintf("%s (%s)",
378 service->UniqueName().c_str(),
379 service->friendly_name().c_str()) :
380 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000381
382 if (selected_service_.get() == service.get()) {
383 // No change to |selected_service_|. Return early to avoid
384 // changing its state.
385 return;
386 }
387
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800388 if (selected_service_.get()) {
389 if (selected_service_->state() != Service::kStateFailure) {
390 selected_service_->SetState(Service::kStateIdle);
391 }
392 // TODO(pstew): We need to revisit the model here: should the Device
393 // subclass be responsible for calling DestroyIPConfig() (which would
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800394 // trigger DestroyConnection() and Service::SetConnection(NULL))?
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800395 // Ethernet does, but WiFi currently does not. crosbug.com/23929
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800396 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700397 }
398 selected_service_ = service;
399}
400
401void Device::SetServiceState(Service::ConnectState state) {
402 if (selected_service_.get()) {
403 selected_service_->SetState(state);
404 }
405}
406
407void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
408 if (selected_service_.get()) {
409 selected_service_->SetFailure(failure_state);
410 }
411}
412
Chris Masone34af2182011-08-22 11:59:36 -0700413string Device::SerializeIPConfigs(const string &suffix) {
414 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700415}
416
Paul Stewart2bf1d352011-12-06 15:02:55 -0800417bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
418 const string &value) {
419 string ip_version;
420 if (family == IPAddress::kFamilyIPv4) {
421 ip_version = kIPFlagVersion4;
422 } else if (family == IPAddress::kFamilyIPv6) {
423 ip_version = kIPFlagVersion6;
424 } else {
425 NOTIMPLEMENTED();
426 }
427 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
428 link_name_.c_str(), flag.c_str()));
429 VLOG(2) << "Writing " << value << " to flag file " << flag_file.value();
430 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
431 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
432 value.c_str(), flag_file.value().c_str());
433 return false;
434 }
435 return true;
436}
437
Paul Stewartc681fa02012-03-02 19:40:04 -0800438bool Device::RequestPortalDetection() {
439 if (!selected_service_) {
440 VLOG(2) << FriendlyName()
441 << ": No selected service, so no need for portal check.";
442 return false;
443 }
444
445 if (!connection_.get()) {
446 VLOG(2) << FriendlyName()
447 << ": No connection, so no need for portal check.";
448 return false;
449 }
450
451 if (selected_service_->state() != Service::kStatePortal) {
452 VLOG(2) << FriendlyName()
453 << ": Service is not in portal state. No need to start check.";
454 return false;
455 }
456
457 if (!connection_->is_default()) {
458 VLOG(2) << FriendlyName()
459 << ": Service is not the default connection. Don't start check.";
460 return false;
461 }
462
463 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
464 VLOG(2) << FriendlyName() << ": Portal detection is already running.";
465 return true;
466 }
467
468 return StartPortalDetection();
469}
470
Paul Stewart20088d82012-02-16 06:58:55 -0800471bool Device::StartPortalDetection() {
472 if (!manager_->IsPortalDetectionEnabled(technology())) {
473 // If portal detection is disabled for this technology, immediately set
474 // the service state to "Online".
475 VLOG(2) << "Device " << FriendlyName()
476 << ": portal detection is disabled; marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800477 SetServiceConnectedState(Service::kStateOnline);
478 return false;
479 }
480
481 DCHECK(selected_service_.get());
482 if (selected_service_->HasProxyConfig()) {
483 // Services with HTTP proxy configurations should not be checked by the
484 // connection manager, since we don't have the ability to evaluate
485 // arbitrary proxy configs and their possible credentials.
486 VLOG(2) << "Device " << FriendlyName()
487 << ": service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800488 SetServiceConnectedState(Service::kStateOnline);
489 return false;
490 }
491
492 portal_detector_.reset(new PortalDetector(connection_,
493 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500494 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800495 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
496 LOG(ERROR) << "Device " << FriendlyName()
497 << ": Portal detection failed to start: likely bad URL: "
498 << manager_->GetPortalCheckURL();
499 SetServiceConnectedState(Service::kStateOnline);
500 return false;
501 }
502
503 VLOG(2) << "Device " << FriendlyName() << ": portal detection has started.";
504 return true;
505}
506
507void Device::StopPortalDetection() {
508 VLOG(2) << "Device " << FriendlyName() << ": portal detection has stopped.";
509 portal_detector_.reset();
510}
511
512void Device::SetServiceConnectedState(Service::ConnectState state) {
513 DCHECK(selected_service_.get());
514
515 if (!selected_service_.get()) {
516 LOG(ERROR) << FriendlyName() << ": "
517 << "Portal detection completed but no selected service exists!";
518 return;
519 }
520
521 if (!selected_service_->IsConnected()) {
522 LOG(ERROR) << FriendlyName() << ": "
523 << "Portal detection completed but selected service "
524 << selected_service_->UniqueName()
525 << " is in non-connected state.";
526 return;
527 }
528
Paul Stewartc681fa02012-03-02 19:40:04 -0800529 if (state == Service::kStatePortal && connection_->is_default() &&
530 manager_->GetPortalCheckInterval() != 0) {
531 CHECK(portal_detector_.get());
532 if (!portal_detector_->StartAfterDelay(
533 manager_->GetPortalCheckURL(),
534 manager_->GetPortalCheckInterval())) {
535 LOG(ERROR) << "Device " << FriendlyName()
536 << ": Portal detection failed to restart: likely bad URL: "
537 << manager_->GetPortalCheckURL();
538 SetServiceState(Service::kStateOnline);
539 portal_detector_.reset();
540 return;
541 }
542 VLOG(2) << "Device " << FriendlyName() << ": portal detection retrying.";
543 } else {
544 VLOG(2) << "Device " << FriendlyName() << ": portal will not retry.";
545 portal_detector_.reset();
546 }
547
Paul Stewart20088d82012-02-16 06:58:55 -0800548 SetServiceState(state);
549}
550
551void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
552 if (!result.final) {
553 VLOG(2) << "Device " << FriendlyName()
554 << ": received non-final status: "
555 << PortalDetector::StatusToString(result.status);
556 return;
557 }
558
559 VLOG(2) << "Device " << FriendlyName()
560 << ": received final status: "
561 << PortalDetector::StatusToString(result.status);
562
Thieu Le85e050b2012-03-13 15:04:38 -0700563 portal_attempts_to_online_ += result.num_attempts;
564
565 metrics()->SendEnumToUMA(
566 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
567 Metrics::PortalDetectionResultToEnum(result),
568 Metrics::kPortalResultMax);
569
Paul Stewart20088d82012-02-16 06:58:55 -0800570 if (result.status == PortalDetector::kStatusSuccess) {
571 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700572
573 metrics()->SendToUMA(
574 metrics()->GetFullMetricName(
575 Metrics::kMetricPortalAttemptsToOnline, technology()),
576 portal_attempts_to_online_,
577 Metrics::kMetricPortalAttemptsToOnlineMin,
578 Metrics::kMetricPortalAttemptsToOnlineMax,
579 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800580 } else {
581 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700582
583 metrics()->SendToUMA(
584 metrics()->GetFullMetricName(
585 Metrics::kMetricPortalAttempts, technology()),
586 result.num_attempts,
587 Metrics::kMetricPortalAttemptsMin,
588 Metrics::kMetricPortalAttemptsMax,
589 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800590 }
591}
592
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800593vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Chris Masone4e851612011-07-01 10:46:53 -0700594 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
595 return vector<string>(1, id);
596}
597
598string Device::GetRpcConnectionIdentifier() {
599 return adaptor_->GetRpcConnectionIdentifier();
600}
601
Eric Shienbrood9a245532012-03-07 14:20:39 -0500602// callback
603void Device::OnEnabledStateChanged(const ResultCallback &callback,
604 const Error &error) {
605 VLOG(2) << __func__ << "(" << enabled_pending_ << ")";
606 if (error.IsSuccess()) {
607 enabled_ = enabled_pending_;
608 manager_->UpdateEnabledTechnologies();
609 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
610 adaptor_->UpdateEnabled();
611 }
612 if (!callback.is_null())
613 callback.Run(error);
614}
615
616void Device::SetEnabled(bool enable) {
617 VLOG(2) << __func__ << "(" << enable << ")";
618 SetEnabledInternal(enable, false, NULL, ResultCallback());
619}
620
621void Device::SetEnabledPersistent(bool enable,
622 Error *error,
623 const ResultCallback &callback) {
624 SetEnabledInternal(enable, true, error, callback);
625}
626
627void Device::SetEnabledInternal(bool enable,
628 bool persist,
629 Error *error,
630 const ResultCallback &callback) {
631 VLOG(2) << "Device " << link_name_ << " "
632 << (enable ? "starting" : "stopping");
633 if (enable == enabled_) {
634 if (error)
635 error->Reset();
636 return;
637 }
638
639 if (enabled_pending_ == enable) {
640 if (error)
641 error->Populate(Error::kInProgress,
642 "Enable operation already in progress");
643 return;
644 }
645
646 if (persist) {
647 enabled_persistent_ = enable;
648 manager_->SaveActiveProfile();
649 }
650
651 enabled_pending_ = enable;
652 EnabledStateChangedCallback enabled_callback =
653 Bind(&Device::OnEnabledStateChanged,
654 weak_ptr_factory_.GetWeakPtr(), callback);
655 if (enable) {
656 running_ = true;
657 routing_table_->FlushRoutes(interface_index());
658 Start(error, enabled_callback);
659 } else {
660 running_ = false;
661 DestroyIPConfig(); // breaks a reference cycle
662 SelectService(NULL); // breaks a reference cycle
663 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
664 VLOG(3) << "Device " << link_name_ << " ipconfig_ "
665 << (ipconfig_ ? "is set." : "is not set.");
666 VLOG(3) << "Device " << link_name_ << " connection_ "
667 << (connection_ ? "is set." : "is not set.");
668 VLOG(3) << "Device " << link_name_ << " selected_service_ "
669 << (selected_service_ ? "is set." : "is not set.");
670 Stop(error, enabled_callback);
671 }
672}
673
Paul Stewart75897df2011-04-27 09:05:53 -0700674} // namespace shill