blob: a5c6ac810dd6561da4a9a18f5b9fa8452fe1d748 [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
Paul Stewartd215af62012-04-24 23:25:50 -0700239bool Device::IsConnectedToService(const ServiceRefPtr &service) const {
240 return service == selected_service_ && IsConnected();
241}
242
Chris Masone27c4aa52011-07-02 13:10:14 -0700243string Device::GetRpcIdentifier() {
244 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700245}
246
Chris Masone5dec5f42011-07-22 14:07:55 -0700247string Device::GetStorageIdentifier() {
248 string id = GetRpcIdentifier();
249 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700250 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700251 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700252 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700253 return id;
254}
255
Jason Glasgowb5790052012-01-27 01:03:52 -0500256string Device::GetTechnologyString(Error */*error*/) {
257 return Technology::NameFromIdentifier(technology());
258}
259
Chris Masone19e30402011-07-19 15:48:47 -0700260const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700261 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700262}
263
Chris Masone19e30402011-07-19 15:48:47 -0700264const string& Device::UniqueName() const {
265 return unique_id_;
266}
267
Chris Masone5dec5f42011-07-22 14:07:55 -0700268bool Device::Load(StoreInterface *storage) {
269 const string id = GetStorageIdentifier();
270 if (!storage->ContainsGroup(id)) {
271 LOG(WARNING) << "Device is not available in the persistent store: " << id;
272 return false;
273 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500274 enabled_persistent_ = true;
275 storage->GetBool(id, kStoragePowered, &enabled_persistent_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700276 // TODO(cmasone): What does it mean to load an IPConfig identifier??
277 return true;
278}
279
280bool Device::Save(StoreInterface *storage) {
281 const string id = GetStorageIdentifier();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500282 storage->SetBool(id, kStoragePowered, enabled_persistent_);
Chris Masone34af2182011-08-22 11:59:36 -0700283 if (ipconfig_.get()) {
284 // The _0 is an index into the list of IPConfigs that this device might
285 // have. We only have one IPConfig right now, and I hope to never have
286 // to support more, as sleffler indicates that associating IPConfigs
287 // with devices is wrong and due to be changed in flimflam anyhow.
288 string suffix = hardware_address_ + "_0";
289 ipconfig_->Save(storage, suffix);
290 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
291 }
Chris Masone5dec5f42011-07-22 14:07:55 -0700292 return true;
293}
294
Darin Petkovafa6fc42011-06-21 16:21:08 -0700295void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800296 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700297 if (ipconfig_.get()) {
298 ipconfig_->ReleaseIP();
299 ipconfig_ = NULL;
300 }
Paul Stewarte6132022011-08-16 09:11:02 -0700301 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700302}
303
Paul Stewart2bf1d352011-12-06 15:02:55 -0800304bool Device::AcquireIPConfig() {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700305 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800306 EnableIPv6();
Paul Stewartd32f4842012-01-11 16:08:13 -0800307 ipconfig_ = dhcp_provider_->CreateConfig(link_name_, manager_->GetHostName());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500308 ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated,
309 weak_ptr_factory_.GetWeakPtr()));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700310 return ipconfig_->RequestIP();
311}
312
Jason Glasgowb5790052012-01-27 01:03:52 -0500313void Device::HelpRegisterDerivedString(
314 const string &name,
315 string(Device::*get)(Error *error),
316 void(Device::*set)(const string &value, Error *error)) {
317 store_.RegisterDerivedString(
318 name,
319 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
320}
321
mukesh agrawalffa3d042011-10-06 15:26:10 -0700322void Device::HelpRegisterDerivedStrings(
323 const string &name,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500324 Strings(Device::*get)(Error *error),
325 void(Device::*set)(const Strings &value, Error *error)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700326 store_.RegisterDerivedStrings(
327 name,
328 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700329}
330
Jason Glasgow08afdff2012-04-03 10:22:26 -0400331void Device::HelpRegisterConstDerivedRpcIdentifiers(
332 const string &name,
333 RpcIdentifiers(Device::*get)(Error *)) {
334 store_.RegisterDerivedRpcIdentifiers(
335 name,
336 RpcIdentifiersAccessor(
337 new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL)));
338}
339
Darin Petkov79d74c92012-03-07 17:20:32 +0100340void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700341 SLOG(Device, 2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700342 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700343 CreateConnection();
344 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800345 // SetConnection must occur after the UpdateFromIPConfig so the
346 // service can use the values derived from the connection.
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800347 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800348 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800349 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800350 // The service state change needs to happen last, so that at the
351 // time we report the state change to the manager, the service
352 // has its connection.
353 SetServiceState(Service::kStateConnected);
Thieu Le85e050b2012-03-13 15:04:38 -0700354 portal_attempts_to_online_ = 0;
Paul Stewart20088d82012-02-16 06:58:55 -0800355 // Subtle: Start portal detection after transitioning the service
356 // to the Connected state because this call may immediately transition
357 // to the Online state.
358 StartPortalDetection();
Paul Stewarte6132022011-08-16 09:11:02 -0700359 } else {
360 // TODO(pstew): This logic gets more complex when multiple IPConfig types
361 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700362 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700363 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700364 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700365}
366
Paul Stewarte6132022011-08-16 09:11:02 -0700367void Device::CreateConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700368 SLOG(Device, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -0700369 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700370 connection_ = new Connection(interface_index_,
371 link_name_,
Paul Stewarte00600e2012-03-16 07:08:00 -0700372 technology_,
Paul Stewart9a908082011-08-31 12:18:48 -0700373 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700374 }
375}
376
377void Device::DestroyConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700378 SLOG(Device, 2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800379 StopPortalDetection();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800380 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800381 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800382 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800383 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700384}
385
Paul Stewart03dba0b2011-08-22 16:32:45 -0700386void Device::SelectService(const ServiceRefPtr &service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700387 SLOG(Device, 2) << __func__ << ": "
388 << (service.get() ?
389 StringPrintf("%s (%s)",
390 service->UniqueName().c_str(),
391 service->friendly_name().c_str()) :
392 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000393
394 if (selected_service_.get() == service.get()) {
395 // No change to |selected_service_|. Return early to avoid
396 // changing its state.
397 return;
398 }
399
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800400 if (selected_service_.get()) {
401 if (selected_service_->state() != Service::kStateFailure) {
402 selected_service_->SetState(Service::kStateIdle);
403 }
404 // TODO(pstew): We need to revisit the model here: should the Device
405 // subclass be responsible for calling DestroyIPConfig() (which would
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800406 // trigger DestroyConnection() and Service::SetConnection(NULL))?
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800407 // Ethernet does, but WiFi currently does not. crosbug.com/23929
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800408 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700409 }
410 selected_service_ = service;
411}
412
413void Device::SetServiceState(Service::ConnectState state) {
414 if (selected_service_.get()) {
415 selected_service_->SetState(state);
416 }
417}
418
419void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
420 if (selected_service_.get()) {
421 selected_service_->SetFailure(failure_state);
422 }
423}
424
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400425void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) {
426 if (selected_service_.get()) {
427 selected_service_->SetFailureSilent(failure_state);
428 }
429}
430
Chris Masone34af2182011-08-22 11:59:36 -0700431string Device::SerializeIPConfigs(const string &suffix) {
432 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700433}
434
Paul Stewart2bf1d352011-12-06 15:02:55 -0800435bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
436 const string &value) {
437 string ip_version;
438 if (family == IPAddress::kFamilyIPv4) {
439 ip_version = kIPFlagVersion4;
440 } else if (family == IPAddress::kFamilyIPv6) {
441 ip_version = kIPFlagVersion6;
442 } else {
443 NOTIMPLEMENTED();
444 }
445 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
446 link_name_.c_str(), flag.c_str()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700447 SLOG(Device, 2) << "Writing " << value << " to flag file "
448 << flag_file.value();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800449 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
450 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
451 value.c_str(), flag_file.value().c_str());
452 return false;
453 }
454 return true;
455}
456
Paul Stewartd215af62012-04-24 23:25:50 -0700457bool Device::RestartPortalDetection() {
458 StopPortalDetection();
459 return StartPortalDetection();
460}
461
Paul Stewartc681fa02012-03-02 19:40:04 -0800462bool Device::RequestPortalDetection() {
463 if (!selected_service_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700464 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800465 << ": No selected service, so no need for portal check.";
466 return false;
467 }
468
469 if (!connection_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700470 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800471 << ": No connection, so no need for portal check.";
472 return false;
473 }
474
475 if (selected_service_->state() != Service::kStatePortal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700476 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800477 << ": Service is not in portal state. No need to start check.";
478 return false;
479 }
480
481 if (!connection_->is_default()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700482 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800483 << ": Service is not the default connection. Don't start check.";
484 return false;
485 }
486
487 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700488 SLOG(Device, 2) << FriendlyName()
489 << ": Portal detection is already running.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800490 return true;
491 }
492
493 return StartPortalDetection();
494}
495
Paul Stewart20088d82012-02-16 06:58:55 -0800496bool Device::StartPortalDetection() {
Paul Stewartd215af62012-04-24 23:25:50 -0700497 DCHECK(selected_service_.get());
498 if (selected_service_->IsPortalDetectionDisabled()) {
499 SLOG(Device, 2) << "Service " << selected_service_->friendly_name()
500 << ": Portal detection is disabled; "
501 << "marking service online.";
502 SetServiceConnectedState(Service::kStateOnline);
503 return false;
504 }
505
506 if (selected_service_->IsPortalDetectionAuto() &&
507 !manager_->IsPortalDetectionEnabled(technology())) {
Paul Stewart20088d82012-02-16 06:58:55 -0800508 // If portal detection is disabled for this technology, immediately set
509 // the service state to "Online".
Ben Chanfad4a0b2012-04-18 15:49:59 -0700510 SLOG(Device, 2) << "Device " << FriendlyName()
511 << ": Portal detection is disabled; "
512 << "marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800513 SetServiceConnectedState(Service::kStateOnline);
514 return false;
515 }
516
Paul Stewart20088d82012-02-16 06:58:55 -0800517 if (selected_service_->HasProxyConfig()) {
518 // Services with HTTP proxy configurations should not be checked by the
519 // connection manager, since we don't have the ability to evaluate
520 // arbitrary proxy configs and their possible credentials.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700521 SLOG(Device, 2) << "Device " << FriendlyName()
522 << ": Service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800523 SetServiceConnectedState(Service::kStateOnline);
524 return false;
525 }
526
527 portal_detector_.reset(new PortalDetector(connection_,
528 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500529 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800530 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
531 LOG(ERROR) << "Device " << FriendlyName()
532 << ": Portal detection failed to start: likely bad URL: "
533 << manager_->GetPortalCheckURL();
534 SetServiceConnectedState(Service::kStateOnline);
535 return false;
536 }
537
Ben Chanfad4a0b2012-04-18 15:49:59 -0700538 SLOG(Device, 2) << "Device " << FriendlyName()
539 << ": Portal detection has started.";
Paul Stewart20088d82012-02-16 06:58:55 -0800540 return true;
541}
542
543void Device::StopPortalDetection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700544 SLOG(Device, 2) << "Device " << FriendlyName()
545 << ": Portal detection has stopped.";
Paul Stewart20088d82012-02-16 06:58:55 -0800546 portal_detector_.reset();
547}
548
549void Device::SetServiceConnectedState(Service::ConnectState state) {
550 DCHECK(selected_service_.get());
551
552 if (!selected_service_.get()) {
553 LOG(ERROR) << FriendlyName() << ": "
554 << "Portal detection completed but no selected service exists!";
555 return;
556 }
557
558 if (!selected_service_->IsConnected()) {
559 LOG(ERROR) << FriendlyName() << ": "
560 << "Portal detection completed but selected service "
561 << selected_service_->UniqueName()
562 << " is in non-connected state.";
563 return;
564 }
565
Paul Stewartc681fa02012-03-02 19:40:04 -0800566 if (state == Service::kStatePortal && connection_->is_default() &&
567 manager_->GetPortalCheckInterval() != 0) {
568 CHECK(portal_detector_.get());
569 if (!portal_detector_->StartAfterDelay(
570 manager_->GetPortalCheckURL(),
571 manager_->GetPortalCheckInterval())) {
572 LOG(ERROR) << "Device " << FriendlyName()
573 << ": Portal detection failed to restart: likely bad URL: "
574 << manager_->GetPortalCheckURL();
575 SetServiceState(Service::kStateOnline);
576 portal_detector_.reset();
577 return;
578 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700579 SLOG(Device, 2) << "Device " << FriendlyName()
580 << ": Portal detection retrying.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800581 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700582 SLOG(Device, 2) << "Device " << FriendlyName()
583 << ": Portal will not retry.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800584 portal_detector_.reset();
585 }
586
Paul Stewart20088d82012-02-16 06:58:55 -0800587 SetServiceState(state);
588}
589
590void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
591 if (!result.final) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700592 SLOG(Device, 2) << "Device " << FriendlyName()
593 << ": Received non-final status: "
594 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800595 return;
596 }
597
Ben Chanfad4a0b2012-04-18 15:49:59 -0700598 SLOG(Device, 2) << "Device " << FriendlyName()
599 << ": Received final status: "
600 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800601
Thieu Le85e050b2012-03-13 15:04:38 -0700602 portal_attempts_to_online_ += result.num_attempts;
603
604 metrics()->SendEnumToUMA(
605 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
606 Metrics::PortalDetectionResultToEnum(result),
607 Metrics::kPortalResultMax);
608
Paul Stewart20088d82012-02-16 06:58:55 -0800609 if (result.status == PortalDetector::kStatusSuccess) {
610 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700611
612 metrics()->SendToUMA(
613 metrics()->GetFullMetricName(
614 Metrics::kMetricPortalAttemptsToOnline, technology()),
615 portal_attempts_to_online_,
616 Metrics::kMetricPortalAttemptsToOnlineMin,
617 Metrics::kMetricPortalAttemptsToOnlineMax,
618 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800619 } else {
620 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700621
622 metrics()->SendToUMA(
623 metrics()->GetFullMetricName(
624 Metrics::kMetricPortalAttempts, technology()),
625 result.num_attempts,
626 Metrics::kMetricPortalAttemptsMin,
627 Metrics::kMetricPortalAttemptsMax,
628 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800629 }
630}
631
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800632vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Jason Glasgow08afdff2012-04-03 10:22:26 -0400633 if (ipconfig_.get()) {
634 string id = ipconfig_->GetRpcIdentifier();
635 return vector<string>(1, id);
636 }
637 return vector<string>();
Chris Masone4e851612011-07-01 10:46:53 -0700638}
639
640string Device::GetRpcConnectionIdentifier() {
641 return adaptor_->GetRpcConnectionIdentifier();
642}
643
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400644bool Device::IsUnderlyingDeviceEnabled() const {
645 return false;
646}
647
Eric Shienbrood9a245532012-03-07 14:20:39 -0500648// callback
649void Device::OnEnabledStateChanged(const ResultCallback &callback,
650 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700651 SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500652 if (error.IsSuccess()) {
653 enabled_ = enabled_pending_;
654 manager_->UpdateEnabledTechnologies();
655 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
656 adaptor_->UpdateEnabled();
657 }
658 if (!callback.is_null())
659 callback.Run(error);
660}
661
662void Device::SetEnabled(bool enable) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700663 SLOG(Device, 2) << __func__ << "(" << enable << ")";
Jason Glasgow4a490792012-04-10 15:02:05 -0400664 Error error;
665 SetEnabledInternal(enable, false, &error, ResultCallback());
666 LOG_IF(ERROR, error.IsFailure())
667 << "Enabled failed, but no way to report the failure.";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500668}
669
670void Device::SetEnabledPersistent(bool enable,
671 Error *error,
672 const ResultCallback &callback) {
673 SetEnabledInternal(enable, true, error, callback);
674}
675
676void Device::SetEnabledInternal(bool enable,
677 bool persist,
678 Error *error,
679 const ResultCallback &callback) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400680 DCHECK(error);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700681 SLOG(Device, 2) << "Device " << link_name_ << " "
682 << (enable ? "starting" : "stopping");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500683 if (enable == enabled_) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400684 error->Reset();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500685 return;
686 }
687
688 if (enabled_pending_ == enable) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400689 Error::PopulateAndLog(error, Error::kInProgress,
690 "Enable operation already in progress");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500691 return;
692 }
693
694 if (persist) {
695 enabled_persistent_ = enable;
696 manager_->SaveActiveProfile();
697 }
698
699 enabled_pending_ = enable;
700 EnabledStateChangedCallback enabled_callback =
701 Bind(&Device::OnEnabledStateChanged,
702 weak_ptr_factory_.GetWeakPtr(), callback);
703 if (enable) {
704 running_ = true;
705 routing_table_->FlushRoutes(interface_index());
706 Start(error, enabled_callback);
707 } else {
708 running_ = false;
709 DestroyIPConfig(); // breaks a reference cycle
710 SelectService(NULL); // breaks a reference cycle
711 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700712 SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ "
713 << (ipconfig_ ? "is set." : "is not set.");
714 SLOG(Device, 3) << "Device " << link_name_ << " connection_ "
715 << (connection_ ? "is set." : "is not set.");
716 SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ "
717 << (selected_service_ ? "is set." : "is not set.");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500718 Stop(error, enabled_callback);
719 }
720}
721
Paul Stewart75897df2011-04-27 09:05:53 -0700722} // namespace shill