blob: e89c1182c994808c4fa0650fe44d14ba5534385e [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()));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700310 dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask,
311 weak_ptr_factory_.GetWeakPtr()));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700312 return ipconfig_->RequestIP();
313}
314
Jason Glasgowb5790052012-01-27 01:03:52 -0500315void Device::HelpRegisterDerivedString(
316 const string &name,
317 string(Device::*get)(Error *error),
318 void(Device::*set)(const string &value, Error *error)) {
319 store_.RegisterDerivedString(
320 name,
321 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
322}
323
mukesh agrawalffa3d042011-10-06 15:26:10 -0700324void Device::HelpRegisterDerivedStrings(
325 const string &name,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500326 Strings(Device::*get)(Error *error),
327 void(Device::*set)(const Strings &value, Error *error)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700328 store_.RegisterDerivedStrings(
329 name,
330 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700331}
332
Jason Glasgow08afdff2012-04-03 10:22:26 -0400333void Device::HelpRegisterConstDerivedRpcIdentifiers(
334 const string &name,
335 RpcIdentifiers(Device::*get)(Error *)) {
336 store_.RegisterDerivedRpcIdentifiers(
337 name,
338 RpcIdentifiersAccessor(
339 new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL)));
340}
341
Paul Stewart1062d9d2012-04-27 10:42:27 -0700342void Device::ConfigureStaticIPTask() {
343 SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get()
344 << " ipconfig " << ipconfig_.get();
345
346 if (!selected_service_ || !ipconfig_) {
347 return;
348 }
349
350 const StaticIPParameters &static_ip_parameters =
351 selected_service_->static_ip_parameters();
352 if (static_ip_parameters.ContainsAddress()) {
353 SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters.";
354 // If the parameters contain an IP address, apply them now and bring
355 // the interface up. When DHCP information arrives, it will supplement
356 // the static information.
357 OnIPConfigUpdated(ipconfig_, true);
358 } else {
359 SLOG(Device, 2) << __func__ << " " << " no static IP address.";
360 }
361}
362
Darin Petkov79d74c92012-03-07 17:20:32 +0100363void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700364 SLOG(Device, 2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700365 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700366 CreateConnection();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700367 if (selected_service_) {
368 ipconfig->ApplyStaticIPParameters(
369 selected_service_->static_ip_parameters());
370 }
Paul Stewarte6132022011-08-16 09:11:02 -0700371 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800372 // SetConnection must occur after the UpdateFromIPConfig so the
373 // service can use the values derived from the connection.
Paul Stewart1062d9d2012-04-27 10:42:27 -0700374 if (selected_service_) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800375 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800376 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800377 // The service state change needs to happen last, so that at the
378 // time we report the state change to the manager, the service
379 // has its connection.
380 SetServiceState(Service::kStateConnected);
Thieu Le85e050b2012-03-13 15:04:38 -0700381 portal_attempts_to_online_ = 0;
Paul Stewart20088d82012-02-16 06:58:55 -0800382 // Subtle: Start portal detection after transitioning the service
383 // to the Connected state because this call may immediately transition
384 // to the Online state.
385 StartPortalDetection();
Paul Stewarte6132022011-08-16 09:11:02 -0700386 } else {
387 // TODO(pstew): This logic gets more complex when multiple IPConfig types
388 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700389 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700390 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700391 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700392}
393
Paul Stewarte6132022011-08-16 09:11:02 -0700394void Device::CreateConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700395 SLOG(Device, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -0700396 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700397 connection_ = new Connection(interface_index_,
398 link_name_,
Paul Stewarte00600e2012-03-16 07:08:00 -0700399 technology_,
Paul Stewart9a908082011-08-31 12:18:48 -0700400 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700401 }
402}
403
404void Device::DestroyConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700405 SLOG(Device, 2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800406 StopPortalDetection();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800407 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800408 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800409 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800410 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700411}
412
Paul Stewart03dba0b2011-08-22 16:32:45 -0700413void Device::SelectService(const ServiceRefPtr &service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700414 SLOG(Device, 2) << __func__ << ": "
415 << (service.get() ?
416 StringPrintf("%s (%s)",
417 service->UniqueName().c_str(),
418 service->friendly_name().c_str()) :
419 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000420
421 if (selected_service_.get() == service.get()) {
422 // No change to |selected_service_|. Return early to avoid
423 // changing its state.
424 return;
425 }
426
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800427 if (selected_service_.get()) {
428 if (selected_service_->state() != Service::kStateFailure) {
429 selected_service_->SetState(Service::kStateIdle);
430 }
431 // TODO(pstew): We need to revisit the model here: should the Device
432 // subclass be responsible for calling DestroyIPConfig() (which would
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800433 // trigger DestroyConnection() and Service::SetConnection(NULL))?
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800434 // Ethernet does, but WiFi currently does not. crosbug.com/23929
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800435 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700436 }
437 selected_service_ = service;
438}
439
440void Device::SetServiceState(Service::ConnectState state) {
441 if (selected_service_.get()) {
442 selected_service_->SetState(state);
443 }
444}
445
446void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
447 if (selected_service_.get()) {
448 selected_service_->SetFailure(failure_state);
449 }
450}
451
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400452void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) {
453 if (selected_service_.get()) {
454 selected_service_->SetFailureSilent(failure_state);
455 }
456}
457
Chris Masone34af2182011-08-22 11:59:36 -0700458string Device::SerializeIPConfigs(const string &suffix) {
459 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700460}
461
Paul Stewart2bf1d352011-12-06 15:02:55 -0800462bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
463 const string &value) {
464 string ip_version;
465 if (family == IPAddress::kFamilyIPv4) {
466 ip_version = kIPFlagVersion4;
467 } else if (family == IPAddress::kFamilyIPv6) {
468 ip_version = kIPFlagVersion6;
469 } else {
470 NOTIMPLEMENTED();
471 }
472 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
473 link_name_.c_str(), flag.c_str()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700474 SLOG(Device, 2) << "Writing " << value << " to flag file "
475 << flag_file.value();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800476 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
477 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
478 value.c_str(), flag_file.value().c_str());
479 return false;
480 }
481 return true;
482}
483
Paul Stewartd215af62012-04-24 23:25:50 -0700484bool Device::RestartPortalDetection() {
485 StopPortalDetection();
486 return StartPortalDetection();
487}
488
Paul Stewartc681fa02012-03-02 19:40:04 -0800489bool Device::RequestPortalDetection() {
490 if (!selected_service_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700491 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800492 << ": No selected service, so no need for portal check.";
493 return false;
494 }
495
496 if (!connection_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700497 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800498 << ": No connection, so no need for portal check.";
499 return false;
500 }
501
502 if (selected_service_->state() != Service::kStatePortal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700503 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800504 << ": Service is not in portal state. No need to start check.";
505 return false;
506 }
507
508 if (!connection_->is_default()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700509 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800510 << ": Service is not the default connection. Don't start check.";
511 return false;
512 }
513
514 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700515 SLOG(Device, 2) << FriendlyName()
516 << ": Portal detection is already running.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800517 return true;
518 }
519
520 return StartPortalDetection();
521}
522
Paul Stewart20088d82012-02-16 06:58:55 -0800523bool Device::StartPortalDetection() {
Paul Stewartd215af62012-04-24 23:25:50 -0700524 DCHECK(selected_service_.get());
525 if (selected_service_->IsPortalDetectionDisabled()) {
526 SLOG(Device, 2) << "Service " << selected_service_->friendly_name()
527 << ": Portal detection is disabled; "
528 << "marking service online.";
529 SetServiceConnectedState(Service::kStateOnline);
530 return false;
531 }
532
533 if (selected_service_->IsPortalDetectionAuto() &&
534 !manager_->IsPortalDetectionEnabled(technology())) {
Paul Stewart20088d82012-02-16 06:58:55 -0800535 // If portal detection is disabled for this technology, immediately set
536 // the service state to "Online".
Ben Chanfad4a0b2012-04-18 15:49:59 -0700537 SLOG(Device, 2) << "Device " << FriendlyName()
538 << ": Portal detection is disabled; "
539 << "marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800540 SetServiceConnectedState(Service::kStateOnline);
541 return false;
542 }
543
Paul Stewart20088d82012-02-16 06:58:55 -0800544 if (selected_service_->HasProxyConfig()) {
545 // Services with HTTP proxy configurations should not be checked by the
546 // connection manager, since we don't have the ability to evaluate
547 // arbitrary proxy configs and their possible credentials.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700548 SLOG(Device, 2) << "Device " << FriendlyName()
549 << ": Service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800550 SetServiceConnectedState(Service::kStateOnline);
551 return false;
552 }
553
554 portal_detector_.reset(new PortalDetector(connection_,
555 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500556 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800557 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
558 LOG(ERROR) << "Device " << FriendlyName()
559 << ": Portal detection failed to start: likely bad URL: "
560 << manager_->GetPortalCheckURL();
561 SetServiceConnectedState(Service::kStateOnline);
562 return false;
563 }
564
Ben Chanfad4a0b2012-04-18 15:49:59 -0700565 SLOG(Device, 2) << "Device " << FriendlyName()
566 << ": Portal detection has started.";
Paul Stewart20088d82012-02-16 06:58:55 -0800567 return true;
568}
569
570void Device::StopPortalDetection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700571 SLOG(Device, 2) << "Device " << FriendlyName()
572 << ": Portal detection has stopped.";
Paul Stewart20088d82012-02-16 06:58:55 -0800573 portal_detector_.reset();
574}
575
576void Device::SetServiceConnectedState(Service::ConnectState state) {
577 DCHECK(selected_service_.get());
578
579 if (!selected_service_.get()) {
580 LOG(ERROR) << FriendlyName() << ": "
581 << "Portal detection completed but no selected service exists!";
582 return;
583 }
584
585 if (!selected_service_->IsConnected()) {
586 LOG(ERROR) << FriendlyName() << ": "
587 << "Portal detection completed but selected service "
588 << selected_service_->UniqueName()
589 << " is in non-connected state.";
590 return;
591 }
592
Paul Stewartc681fa02012-03-02 19:40:04 -0800593 if (state == Service::kStatePortal && connection_->is_default() &&
594 manager_->GetPortalCheckInterval() != 0) {
595 CHECK(portal_detector_.get());
596 if (!portal_detector_->StartAfterDelay(
597 manager_->GetPortalCheckURL(),
598 manager_->GetPortalCheckInterval())) {
599 LOG(ERROR) << "Device " << FriendlyName()
600 << ": Portal detection failed to restart: likely bad URL: "
601 << manager_->GetPortalCheckURL();
602 SetServiceState(Service::kStateOnline);
603 portal_detector_.reset();
604 return;
605 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700606 SLOG(Device, 2) << "Device " << FriendlyName()
607 << ": Portal detection retrying.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800608 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700609 SLOG(Device, 2) << "Device " << FriendlyName()
610 << ": Portal will not retry.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800611 portal_detector_.reset();
612 }
613
Paul Stewart20088d82012-02-16 06:58:55 -0800614 SetServiceState(state);
615}
616
617void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
618 if (!result.final) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700619 SLOG(Device, 2) << "Device " << FriendlyName()
620 << ": Received non-final status: "
621 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800622 return;
623 }
624
Ben Chanfad4a0b2012-04-18 15:49:59 -0700625 SLOG(Device, 2) << "Device " << FriendlyName()
626 << ": Received final status: "
627 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800628
Thieu Le85e050b2012-03-13 15:04:38 -0700629 portal_attempts_to_online_ += result.num_attempts;
630
631 metrics()->SendEnumToUMA(
632 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
633 Metrics::PortalDetectionResultToEnum(result),
634 Metrics::kPortalResultMax);
635
Paul Stewart20088d82012-02-16 06:58:55 -0800636 if (result.status == PortalDetector::kStatusSuccess) {
637 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700638
639 metrics()->SendToUMA(
640 metrics()->GetFullMetricName(
641 Metrics::kMetricPortalAttemptsToOnline, technology()),
642 portal_attempts_to_online_,
643 Metrics::kMetricPortalAttemptsToOnlineMin,
644 Metrics::kMetricPortalAttemptsToOnlineMax,
645 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800646 } else {
647 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700648
649 metrics()->SendToUMA(
650 metrics()->GetFullMetricName(
651 Metrics::kMetricPortalAttempts, technology()),
652 result.num_attempts,
653 Metrics::kMetricPortalAttemptsMin,
654 Metrics::kMetricPortalAttemptsMax,
655 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800656 }
657}
658
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800659vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Jason Glasgow08afdff2012-04-03 10:22:26 -0400660 if (ipconfig_.get()) {
661 string id = ipconfig_->GetRpcIdentifier();
662 return vector<string>(1, id);
663 }
664 return vector<string>();
Chris Masone4e851612011-07-01 10:46:53 -0700665}
666
667string Device::GetRpcConnectionIdentifier() {
668 return adaptor_->GetRpcConnectionIdentifier();
669}
670
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400671bool Device::IsUnderlyingDeviceEnabled() const {
672 return false;
673}
674
Eric Shienbrood9a245532012-03-07 14:20:39 -0500675// callback
676void Device::OnEnabledStateChanged(const ResultCallback &callback,
677 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700678 SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500679 if (error.IsSuccess()) {
680 enabled_ = enabled_pending_;
681 manager_->UpdateEnabledTechnologies();
682 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
683 adaptor_->UpdateEnabled();
684 }
685 if (!callback.is_null())
686 callback.Run(error);
687}
688
689void Device::SetEnabled(bool enable) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700690 SLOG(Device, 2) << __func__ << "(" << enable << ")";
Jason Glasgow4a490792012-04-10 15:02:05 -0400691 Error error;
692 SetEnabledInternal(enable, false, &error, ResultCallback());
693 LOG_IF(ERROR, error.IsFailure())
694 << "Enabled failed, but no way to report the failure.";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500695}
696
697void Device::SetEnabledPersistent(bool enable,
698 Error *error,
699 const ResultCallback &callback) {
700 SetEnabledInternal(enable, true, error, callback);
701}
702
703void Device::SetEnabledInternal(bool enable,
704 bool persist,
705 Error *error,
706 const ResultCallback &callback) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400707 DCHECK(error);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700708 SLOG(Device, 2) << "Device " << link_name_ << " "
709 << (enable ? "starting" : "stopping");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500710 if (enable == enabled_) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400711 error->Reset();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500712 return;
713 }
714
715 if (enabled_pending_ == enable) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400716 Error::PopulateAndLog(error, Error::kInProgress,
717 "Enable operation already in progress");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500718 return;
719 }
720
721 if (persist) {
722 enabled_persistent_ = enable;
723 manager_->SaveActiveProfile();
724 }
725
726 enabled_pending_ = enable;
727 EnabledStateChangedCallback enabled_callback =
728 Bind(&Device::OnEnabledStateChanged,
729 weak_ptr_factory_.GetWeakPtr(), callback);
730 if (enable) {
731 running_ = true;
732 routing_table_->FlushRoutes(interface_index());
733 Start(error, enabled_callback);
734 } else {
735 running_ = false;
736 DestroyIPConfig(); // breaks a reference cycle
737 SelectService(NULL); // breaks a reference cycle
738 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700739 SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ "
740 << (ipconfig_ ? "is set." : "is not set.");
741 SLOG(Device, 3) << "Device " << link_name_ << " connection_ "
742 << (connection_ ? "is set." : "is not set.");
743 SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ "
744 << (selected_service_ ? "is set." : "is not set.");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500745 Stop(error, enabled_callback);
746 }
747}
748
Paul Stewart75897df2011-04-27 09:05:53 -0700749} // namespace shill