blob: d22c68bbc75c79c55be9124492299a01c3a1432c [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
Paul Stewart2bf1d352011-12-06 15:02:55 -080015#include <base/file_util.h>
Chris Masoneee929b72011-05-10 10:02:18 -070016#include <base/logging.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070017#include <base/memory/ref_counted.h>
Chris Masone5dec5f42011-07-22 14:07:55 -070018#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070019#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070020
Paul Stewarte6132022011-08-16 09:11:02 -070021#include "shill/connection.h"
Paul Stewart75897df2011-04-27 09:05:53 -070022#include "shill/control_interface.h"
Chris Masoned7732e42011-05-20 11:08:56 -070023#include "shill/device_dbus_adaptor.h"
Chris Masone2b105542011-06-22 10:58:09 -070024#include "shill/dhcp_config.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070025#include "shill/dhcp_provider.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070026#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070027#include "shill/event_dispatcher.h"
Paul Stewartf65320c2011-10-13 14:34:52 -070028#include "shill/http_proxy.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070029#include "shill/manager.h"
Chris Masone95207da2011-06-29 16:50:49 -070030#include "shill/property_accessor.h"
Chris Masone2b105542011-06-22 10:58:09 -070031#include "shill/refptr_types.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070032#include "shill/rtnl_handler.h"
Chris Masone2b105542011-06-22 10:58:09 -070033#include "shill/service.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070034#include "shill/store_interface.h"
Gaurav Shah435de2c2011-11-17 19:01:07 -080035#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070036
Chris Masone5dec5f42011-07-22 14:07:55 -070037using base::StringPrintf;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070038using std::string;
39using std::vector;
40
Paul Stewart75897df2011-04-27 09:05:53 -070041namespace shill {
Chris Masone5dec5f42011-07-22 14:07:55 -070042
43// static
Paul Stewart2bf1d352011-12-06 15:02:55 -080044const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s";
45// static
46const char Device::kIPFlagVersion4[] = "ipv4";
47// static
48const char Device::kIPFlagVersion6[] = "ipv6";
49// static
50const char Device::kIPFlagDisableIPv6[] = "disable_ipv6";
51// static
52const char Device::kIPFlagUseTempAddr[] = "use_tempaddr";
53// static
54const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2";
Paul Stewartc8f4bef2011-12-13 09:45:51 -080055// static
56const char Device::kIPFlagReversePathFilter[] = "rp_filter";
57// static
58const char Device::kIPFlagReversePathFilterEnabled[] = "1";
59// static
60const char Device::kIPFlagReversePathFilterLooseMode[] = "2";
Paul Stewart2bf1d352011-12-06 15:02:55 -080061
62// static
Chris Masone5dec5f42011-07-22 14:07:55 -070063const char Device::kStoragePowered[] = "Powered";
64
65// static
66const char Device::kStorageIPConfigs[] = "IPConfigs";
67
Paul Stewart75897df2011-04-27 09:05:53 -070068Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070069 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080070 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070071 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070072 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070073 const string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080074 int interface_index,
75 Technology::Identifier technology)
Chris Masone853b81b2011-06-24 14:11:41 -070076 : powered_(true),
Chris Masoneb925cc82011-06-22 15:39:57 -070077 reconnect_(true),
Chris Masone626719f2011-08-18 16:58:48 -070078 hardware_address_(address),
mukesh agrawalf60e4062011-05-27 13:13:41 -070079 interface_index_(interface_index),
80 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070081 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070082 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070083 control_interface_(control_interface),
84 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080085 metrics_(metrics),
Chris Masone7df0c672011-07-15 10:24:54 -070086 manager_(manager),
Darin Petkov77cb6812011-08-15 16:19:41 -070087 adaptor_(control_interface->CreateDeviceAdaptor(this)),
Paul Stewart20088d82012-02-16 06:58:55 -080088 portal_detector_callback_(
89 NewCallback(this, &Device::PortalDetectorCallback)),
Gaurav Shah435de2c2011-11-17 19:01:07 -080090 technology_(technology),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070091 dhcp_provider_(DHCPProvider::GetInstance()),
92 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -070093 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -070094
95 // flimflam::kBgscanMethodProperty: Registered in WiFi
96 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
97 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
98
99 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
100 // flimflam::kCarrierProperty: Registered in Cellular
101 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -0700102 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700103 // flimflam::kImeiProperty: Registered in Cellular
104 // flimflam::kImsiProperty: Registered in Cellular
105 // flimflam::kManufacturerProperty: Registered in Cellular
106 // flimflam::kMdnProperty: Registered in Cellular
107 // flimflam::kMeidProperty: Registered in Cellular
108 // flimflam::kMinProperty: Registered in Cellular
109 // flimflam::kModelIDProperty: Registered in Cellular
110 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
111 // flimflam::kHardwareRevisionProperty: Registered in Cellular
112 // flimflam::kPRLVersionProperty: Registered in Cellular
113 // flimflam::kSIMLockStatusProperty: Registered in Cellular
114 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -0700115 // flimflam::kDBusConnectionProperty: Registered in Cellular
116 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700117
Chris Masoneb925cc82011-06-22 15:39:57 -0700118 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700119 // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
120 HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty,
121 &Device::AvailableIPConfigs,
122 NULL);
123 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
124 store_.RegisterBool(flimflam::kPoweredProperty, &powered_);
Jason Glasgowb5790052012-01-27 01:03:52 -0500125 HelpRegisterDerivedString(flimflam::kTypeProperty,
126 &Device::GetTechnologyString,
127 NULL);
128
Chris Masoneb925cc82011-06-22 15:39:57 -0700129 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700130 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700131
Chris Masone4e851612011-07-01 10:46:53 -0700132 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700133 // known_properties_.push_back(flimflam::kNetworksProperty);
134
Chris Masone4d42df82011-07-02 17:09:39 -0700135 // flimflam::kScanningProperty: Registered in WiFi, Cellular
136 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
137
138 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -0700139 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -0700140}
141
142Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700143 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700144}
145
146void Device::Start() {
147 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700148 VLOG(2) << "Device " << link_name_ << " starting.";
Chris Masone413a3192011-05-09 17:10:05 -0700149 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700150}
151
152void Device::Stop() {
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700153 VLOG(2) << "Device " << link_name_ << " stopping.";
Paul Stewart75897df2011-04-27 09:05:53 -0700154 running_ = false;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700155 DestroyIPConfig(); // breaks a reference cycle
156 SelectService(NULL); // breaks a reference cycle
Chris Masone413a3192011-05-09 17:10:05 -0700157 adaptor_->UpdateEnabled();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700158 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
159
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700160 VLOG(3) << "Device " << link_name_ << " ipconfig_ "
161 << (ipconfig_.get() ? "is set." : "is not set.");
162 VLOG(3) << "Device " << link_name_ << " connection_ "
163 << (connection_.get() ? "is set." : "is not set.");
164 VLOG(3) << "Device " << link_name_ << " selected_service_ "
165 << (selected_service_.get() ? "is set." : "is not set.");
Paul Stewart75897df2011-04-27 09:05:53 -0700166}
167
mukesh agrawal1830fa12011-09-26 14:31:40 -0700168bool Device::TechnologyIs(const Technology::Identifier /*type*/) const {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700169 return false;
170}
171
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700172void Device::LinkEvent(unsigned flags, unsigned change) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700173 VLOG(2) << "Device " << link_name_
174 << std::showbase << std::hex
175 << " flags " << flags << " changed " << change
176 << std::dec << std::noshowbase;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700177}
178
Darin Petkovc0865312011-09-16 15:31:20 -0700179void Device::Scan(Error *error) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700180 VLOG(2) << "Device " << link_name_ << " scan requested.";
Paul Stewartbe005172011-11-02 18:10:29 -0700181 Error::PopulateAndLog(error, Error::kNotSupported,
182 "Device doesn't support scan.");
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700183}
184
mukesh agrawal1830fa12011-09-26 14:31:40 -0700185void Device::RegisterOnNetwork(const std::string &/*network_id*/,
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500186 ReturnerInterface *returner) {
187 Error error;
188 Error::PopulateAndLog(&error, Error::kNotSupported,
Paul Stewartbe005172011-11-02 18:10:29 -0700189 "Device doesn't support network registration.");
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500190 returner->ReturnError(error);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700191}
192
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100193void Device::RequirePIN(
194 const string &/*pin*/, bool /*require*/, ReturnerInterface *returner) {
195 VLOG(2) << __func__;
196 Error error;
197 Error::PopulateAndLog(
198 &error, Error::kNotSupported, "Device doesn't support RequirePIN.");
199 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700200}
201
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100202void Device::EnterPIN(const string &/*pin*/, ReturnerInterface *returner) {
203 VLOG(2) << __func__;
204 Error error;
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100205 Error::PopulateAndLog(
206 &error, Error::kNotSupported, "Device doesn't support EnterPIN.");
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100207 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700208}
209
mukesh agrawal1830fa12011-09-26 14:31:40 -0700210void Device::UnblockPIN(const string &/*unblock_code*/,
211 const string &/*pin*/,
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100212 ReturnerInterface *returner) {
213 VLOG(2) << __func__;
214 Error error;
215 Error::PopulateAndLog(
216 &error, Error::kNotSupported, "Device doesn't support UnblockPIN.");
217 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700218}
219
mukesh agrawal1830fa12011-09-26 14:31:40 -0700220void Device::ChangePIN(const string &/*old_pin*/,
221 const string &/*new_pin*/,
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100222 ReturnerInterface *returner) {
223 VLOG(2) << __func__;
224 Error error;
225 Error::PopulateAndLog(
226 &error, Error::kNotSupported, "Device doesn't support ChangePIN.");
227 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700228}
229
Paul Stewart2bf1d352011-12-06 15:02:55 -0800230void Device::DisableIPv6() {
231 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
232}
233
234void Device::EnableIPv6() {
235 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
236}
237
238void Device::EnableIPv6Privacy() {
239 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
240 kIPFlagUseTempAddrUsedAndDefault);
241}
242
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800243void Device::DisableReversePathFilter() {
244 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
245 // for IPv6. crosbug.com/24228
246 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
247 kIPFlagReversePathFilterLooseMode);
248}
249
250void Device::EnableReversePathFilter() {
251 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
252 kIPFlagReversePathFilterEnabled);
253}
254
Gaurav Shah435de2c2011-11-17 19:01:07 -0800255bool Device::IsConnected() const {
256 if (selected_service_)
257 return selected_service_->IsConnected();
258 return false;
259}
260
Chris Masone27c4aa52011-07-02 13:10:14 -0700261string Device::GetRpcIdentifier() {
262 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700263}
264
Chris Masone5dec5f42011-07-22 14:07:55 -0700265string Device::GetStorageIdentifier() {
266 string id = GetRpcIdentifier();
267 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700268 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700269 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700270 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700271 return id;
272}
273
Jason Glasgowb5790052012-01-27 01:03:52 -0500274string Device::GetTechnologyString(Error */*error*/) {
275 return Technology::NameFromIdentifier(technology());
276}
277
Chris Masone19e30402011-07-19 15:48:47 -0700278const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700279 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700280}
281
Chris Masone19e30402011-07-19 15:48:47 -0700282const string& Device::UniqueName() const {
283 return unique_id_;
284}
285
Chris Masone5dec5f42011-07-22 14:07:55 -0700286bool Device::Load(StoreInterface *storage) {
287 const string id = GetStorageIdentifier();
288 if (!storage->ContainsGroup(id)) {
289 LOG(WARNING) << "Device is not available in the persistent store: " << id;
290 return false;
291 }
292 storage->GetBool(id, kStoragePowered, &powered_);
293 // TODO(cmasone): What does it mean to load an IPConfig identifier??
294 return true;
295}
296
297bool Device::Save(StoreInterface *storage) {
298 const string id = GetStorageIdentifier();
299 storage->SetBool(id, kStoragePowered, powered_);
Chris Masone34af2182011-08-22 11:59:36 -0700300 if (ipconfig_.get()) {
301 // The _0 is an index into the list of IPConfigs that this device might
302 // have. We only have one IPConfig right now, and I hope to never have
303 // to support more, as sleffler indicates that associating IPConfigs
304 // with devices is wrong and due to be changed in flimflam anyhow.
305 string suffix = hardware_address_ + "_0";
306 ipconfig_->Save(storage, suffix);
307 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
308 }
Chris Masone5dec5f42011-07-22 14:07:55 -0700309 return true;
310}
311
Darin Petkovafa6fc42011-06-21 16:21:08 -0700312void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800313 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700314 if (ipconfig_.get()) {
315 ipconfig_->ReleaseIP();
316 ipconfig_ = NULL;
317 }
Paul Stewarte6132022011-08-16 09:11:02 -0700318 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700319}
320
Paul Stewart2bf1d352011-12-06 15:02:55 -0800321bool Device::AcquireIPConfig() {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700322 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800323 EnableIPv6();
Paul Stewartd32f4842012-01-11 16:08:13 -0800324 ipconfig_ = dhcp_provider_->CreateConfig(link_name_, manager_->GetHostName());
Darin Petkovafa6fc42011-06-21 16:21:08 -0700325 ipconfig_->RegisterUpdateCallback(
Darin Petkov79d74c92012-03-07 17:20:32 +0100326 NewCallback(this, &Device::OnIPConfigUpdated));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700327 return ipconfig_->RequestIP();
328}
329
Jason Glasgowb5790052012-01-27 01:03:52 -0500330void Device::HelpRegisterDerivedString(
331 const string &name,
332 string(Device::*get)(Error *error),
333 void(Device::*set)(const string &value, Error *error)) {
334 store_.RegisterDerivedString(
335 name,
336 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
337}
338
mukesh agrawalffa3d042011-10-06 15:26:10 -0700339void Device::HelpRegisterDerivedStrings(
340 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800341 Strings(Device::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700342 void(Device::*set)(const Strings&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700343 store_.RegisterDerivedStrings(
344 name,
345 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700346}
347
Darin Petkov79d74c92012-03-07 17:20:32 +0100348void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700349 VLOG(2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700350 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700351 CreateConnection();
352 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800353 // SetConnection must occur after the UpdateFromIPConfig so the
354 // service can use the values derived from the connection.
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800355 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800356 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800357 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800358 // The service state change needs to happen last, so that at the
359 // time we report the state change to the manager, the service
360 // has its connection.
361 SetServiceState(Service::kStateConnected);
Paul Stewart20088d82012-02-16 06:58:55 -0800362 // Subtle: Start portal detection after transitioning the service
363 // to the Connected state because this call may immediately transition
364 // to the Online state.
365 StartPortalDetection();
Paul Stewarte6132022011-08-16 09:11:02 -0700366 } else {
367 // TODO(pstew): This logic gets more complex when multiple IPConfig types
368 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700369 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700370 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700371 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700372}
373
Paul Stewarte6132022011-08-16 09:11:02 -0700374void Device::CreateConnection() {
375 VLOG(2) << __func__;
376 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700377 connection_ = new Connection(interface_index_,
378 link_name_,
379 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700380 }
381}
382
383void Device::DestroyConnection() {
384 VLOG(2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800385 StopPortalDetection();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800386 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800387 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800388 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800389 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700390}
391
Paul Stewart03dba0b2011-08-22 16:32:45 -0700392void Device::SelectService(const ServiceRefPtr &service) {
Paul Stewarta2b8cd12011-08-25 14:55:32 -0700393 VLOG(2) << __func__ << ": "
mukesh agrawal15908392011-11-16 18:29:25 +0000394 << (service.get() ?
395 StringPrintf("%s (%s)",
396 service->UniqueName().c_str(),
397 service->friendly_name().c_str()) :
398 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000399
400 if (selected_service_.get() == service.get()) {
401 // No change to |selected_service_|. Return early to avoid
402 // changing its state.
403 return;
404 }
405
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800406 if (selected_service_.get()) {
407 if (selected_service_->state() != Service::kStateFailure) {
408 selected_service_->SetState(Service::kStateIdle);
409 }
410 // TODO(pstew): We need to revisit the model here: should the Device
411 // subclass be responsible for calling DestroyIPConfig() (which would
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800412 // trigger DestroyConnection() and Service::SetConnection(NULL))?
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800413 // Ethernet does, but WiFi currently does not. crosbug.com/23929
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800414 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700415 }
416 selected_service_ = service;
417}
418
419void Device::SetServiceState(Service::ConnectState state) {
420 if (selected_service_.get()) {
421 selected_service_->SetState(state);
422 }
423}
424
425void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
426 if (selected_service_.get()) {
427 selected_service_->SetFailure(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()));
447 VLOG(2) << "Writing " << value << " to flag file " << flag_file.value();
448 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
449 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
450 value.c_str(), flag_file.value().c_str());
451 return false;
452 }
453 return true;
454}
455
Paul Stewartc681fa02012-03-02 19:40:04 -0800456bool Device::RequestPortalDetection() {
457 if (!selected_service_) {
458 VLOG(2) << FriendlyName()
459 << ": No selected service, so no need for portal check.";
460 return false;
461 }
462
463 if (!connection_.get()) {
464 VLOG(2) << FriendlyName()
465 << ": No connection, so no need for portal check.";
466 return false;
467 }
468
469 if (selected_service_->state() != Service::kStatePortal) {
470 VLOG(2) << FriendlyName()
471 << ": Service is not in portal state. No need to start check.";
472 return false;
473 }
474
475 if (!connection_->is_default()) {
476 VLOG(2) << FriendlyName()
477 << ": Service is not the default connection. Don't start check.";
478 return false;
479 }
480
481 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
482 VLOG(2) << FriendlyName() << ": Portal detection is already running.";
483 return true;
484 }
485
486 return StartPortalDetection();
487}
488
Paul Stewart20088d82012-02-16 06:58:55 -0800489bool Device::StartPortalDetection() {
490 if (!manager_->IsPortalDetectionEnabled(technology())) {
491 // If portal detection is disabled for this technology, immediately set
492 // the service state to "Online".
493 VLOG(2) << "Device " << FriendlyName()
494 << ": portal detection is disabled; marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800495 SetServiceConnectedState(Service::kStateOnline);
496 return false;
497 }
498
499 DCHECK(selected_service_.get());
500 if (selected_service_->HasProxyConfig()) {
501 // Services with HTTP proxy configurations should not be checked by the
502 // connection manager, since we don't have the ability to evaluate
503 // arbitrary proxy configs and their possible credentials.
504 VLOG(2) << "Device " << FriendlyName()
505 << ": service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800506 SetServiceConnectedState(Service::kStateOnline);
507 return false;
508 }
509
510 portal_detector_.reset(new PortalDetector(connection_,
511 dispatcher_,
512 portal_detector_callback_.get()));
513 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
514 LOG(ERROR) << "Device " << FriendlyName()
515 << ": Portal detection failed to start: likely bad URL: "
516 << manager_->GetPortalCheckURL();
517 SetServiceConnectedState(Service::kStateOnline);
518 return false;
519 }
520
521 VLOG(2) << "Device " << FriendlyName() << ": portal detection has started.";
522 return true;
523}
524
525void Device::StopPortalDetection() {
526 VLOG(2) << "Device " << FriendlyName() << ": portal detection has stopped.";
527 portal_detector_.reset();
528}
529
530void Device::SetServiceConnectedState(Service::ConnectState state) {
531 DCHECK(selected_service_.get());
532
533 if (!selected_service_.get()) {
534 LOG(ERROR) << FriendlyName() << ": "
535 << "Portal detection completed but no selected service exists!";
536 return;
537 }
538
539 if (!selected_service_->IsConnected()) {
540 LOG(ERROR) << FriendlyName() << ": "
541 << "Portal detection completed but selected service "
542 << selected_service_->UniqueName()
543 << " is in non-connected state.";
544 return;
545 }
546
Paul Stewartc681fa02012-03-02 19:40:04 -0800547 if (state == Service::kStatePortal && connection_->is_default() &&
548 manager_->GetPortalCheckInterval() != 0) {
549 CHECK(portal_detector_.get());
550 if (!portal_detector_->StartAfterDelay(
551 manager_->GetPortalCheckURL(),
552 manager_->GetPortalCheckInterval())) {
553 LOG(ERROR) << "Device " << FriendlyName()
554 << ": Portal detection failed to restart: likely bad URL: "
555 << manager_->GetPortalCheckURL();
556 SetServiceState(Service::kStateOnline);
557 portal_detector_.reset();
558 return;
559 }
560 VLOG(2) << "Device " << FriendlyName() << ": portal detection retrying.";
561 } else {
562 VLOG(2) << "Device " << FriendlyName() << ": portal will not retry.";
563 portal_detector_.reset();
564 }
565
Paul Stewart20088d82012-02-16 06:58:55 -0800566 SetServiceState(state);
567}
568
569void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
570 if (!result.final) {
571 VLOG(2) << "Device " << FriendlyName()
572 << ": received non-final status: "
573 << PortalDetector::StatusToString(result.status);
574 return;
575 }
576
577 VLOG(2) << "Device " << FriendlyName()
578 << ": received final status: "
579 << PortalDetector::StatusToString(result.status);
580
581 if (result.status == PortalDetector::kStatusSuccess) {
582 SetServiceConnectedState(Service::kStateOnline);
583 } else {
584 SetServiceConnectedState(Service::kStatePortal);
585 }
586}
587
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800588vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Chris Masone4e851612011-07-01 10:46:53 -0700589 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
590 return vector<string>(1, id);
591}
592
593string Device::GetRpcConnectionIdentifier() {
594 return adaptor_->GetRpcConnectionIdentifier();
595}
596
Paul Stewart75897df2011-04-27 09:05:53 -0700597} // namespace shill