blob: f73a21776412e48616a62f4d9af8b5ebdbc5c402 [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"
Thieu Lefb46caf2012-03-08 11:57:15 -080032#include "shill/routing_table.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070033#include "shill/rtnl_handler.h"
Chris Masone2b105542011-06-22 10:58:09 -070034#include "shill/service.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070035#include "shill/store_interface.h"
Gaurav Shah435de2c2011-11-17 19:01:07 -080036#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070037
Chris Masone5dec5f42011-07-22 14:07:55 -070038using base::StringPrintf;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070039using std::string;
40using std::vector;
41
Paul Stewart75897df2011-04-27 09:05:53 -070042namespace shill {
Chris Masone5dec5f42011-07-22 14:07:55 -070043
44// static
Paul Stewart2bf1d352011-12-06 15:02:55 -080045const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s";
46// static
47const char Device::kIPFlagVersion4[] = "ipv4";
48// static
49const char Device::kIPFlagVersion6[] = "ipv6";
50// static
51const char Device::kIPFlagDisableIPv6[] = "disable_ipv6";
52// static
53const char Device::kIPFlagUseTempAddr[] = "use_tempaddr";
54// static
55const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2";
Paul Stewartc8f4bef2011-12-13 09:45:51 -080056// static
57const char Device::kIPFlagReversePathFilter[] = "rp_filter";
58// static
59const char Device::kIPFlagReversePathFilterEnabled[] = "1";
60// static
61const char Device::kIPFlagReversePathFilterLooseMode[] = "2";
Paul Stewart2bf1d352011-12-06 15:02:55 -080062
63// static
Chris Masone5dec5f42011-07-22 14:07:55 -070064const char Device::kStoragePowered[] = "Powered";
65
66// static
67const char Device::kStorageIPConfigs[] = "IPConfigs";
68
Paul Stewart75897df2011-04-27 09:05:53 -070069Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070070 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080071 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070072 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070073 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070074 const string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080075 int interface_index,
76 Technology::Identifier technology)
Chris Masone853b81b2011-06-24 14:11:41 -070077 : powered_(true),
Chris Masoneb925cc82011-06-22 15:39:57 -070078 reconnect_(true),
Chris Masone626719f2011-08-18 16:58:48 -070079 hardware_address_(address),
mukesh agrawalf60e4062011-05-27 13:13:41 -070080 interface_index_(interface_index),
81 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070082 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070083 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070084 control_interface_(control_interface),
85 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080086 metrics_(metrics),
Chris Masone7df0c672011-07-15 10:24:54 -070087 manager_(manager),
Darin Petkov77cb6812011-08-15 16:19:41 -070088 adaptor_(control_interface->CreateDeviceAdaptor(this)),
Paul Stewart20088d82012-02-16 06:58:55 -080089 portal_detector_callback_(
90 NewCallback(this, &Device::PortalDetectorCallback)),
Gaurav Shah435de2c2011-11-17 19:01:07 -080091 technology_(technology),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070092 dhcp_provider_(DHCPProvider::GetInstance()),
Thieu Lefb46caf2012-03-08 11:57:15 -080093 routing_table_(RoutingTable::GetInstance()),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070094 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -070095 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -070096
97 // flimflam::kBgscanMethodProperty: Registered in WiFi
98 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
99 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
100
101 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
102 // flimflam::kCarrierProperty: Registered in Cellular
103 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -0700104 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700105 // flimflam::kImeiProperty: Registered in Cellular
106 // flimflam::kImsiProperty: Registered in Cellular
107 // flimflam::kManufacturerProperty: Registered in Cellular
108 // flimflam::kMdnProperty: Registered in Cellular
109 // flimflam::kMeidProperty: Registered in Cellular
110 // flimflam::kMinProperty: Registered in Cellular
111 // flimflam::kModelIDProperty: Registered in Cellular
112 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
113 // flimflam::kHardwareRevisionProperty: Registered in Cellular
114 // flimflam::kPRLVersionProperty: Registered in Cellular
115 // flimflam::kSIMLockStatusProperty: Registered in Cellular
116 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -0700117 // flimflam::kDBusConnectionProperty: Registered in Cellular
118 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700119
Chris Masoneb925cc82011-06-22 15:39:57 -0700120 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700121 // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
122 HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty,
123 &Device::AvailableIPConfigs,
124 NULL);
125 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
126 store_.RegisterBool(flimflam::kPoweredProperty, &powered_);
Jason Glasgowb5790052012-01-27 01:03:52 -0500127 HelpRegisterDerivedString(flimflam::kTypeProperty,
128 &Device::GetTechnologyString,
129 NULL);
130
Chris Masoneb925cc82011-06-22 15:39:57 -0700131 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700132 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700133
Chris Masone4e851612011-07-01 10:46:53 -0700134 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700135 // known_properties_.push_back(flimflam::kNetworksProperty);
136
Chris Masone4d42df82011-07-02 17:09:39 -0700137 // flimflam::kScanningProperty: Registered in WiFi, Cellular
138 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
139
140 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -0700141 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -0700142}
143
144Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700145 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700146}
147
148void Device::Start() {
149 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700150 VLOG(2) << "Device " << link_name_ << " starting.";
Thieu Lefb46caf2012-03-08 11:57:15 -0800151 routing_table_->FlushRoutes(interface_index());
Chris Masone413a3192011-05-09 17:10:05 -0700152 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700153}
154
155void Device::Stop() {
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700156 VLOG(2) << "Device " << link_name_ << " stopping.";
Paul Stewart75897df2011-04-27 09:05:53 -0700157 running_ = false;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700158 DestroyIPConfig(); // breaks a reference cycle
159 SelectService(NULL); // breaks a reference cycle
Chris Masone413a3192011-05-09 17:10:05 -0700160 adaptor_->UpdateEnabled();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700161 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
162
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700163 VLOG(3) << "Device " << link_name_ << " ipconfig_ "
164 << (ipconfig_.get() ? "is set." : "is not set.");
165 VLOG(3) << "Device " << link_name_ << " connection_ "
166 << (connection_.get() ? "is set." : "is not set.");
167 VLOG(3) << "Device " << link_name_ << " selected_service_ "
168 << (selected_service_.get() ? "is set." : "is not set.");
Paul Stewart75897df2011-04-27 09:05:53 -0700169}
170
mukesh agrawal1830fa12011-09-26 14:31:40 -0700171bool Device::TechnologyIs(const Technology::Identifier /*type*/) const {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700172 return false;
173}
174
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700175void Device::LinkEvent(unsigned flags, unsigned change) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700176 VLOG(2) << "Device " << link_name_
177 << std::showbase << std::hex
178 << " flags " << flags << " changed " << change
179 << std::dec << std::noshowbase;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700180}
181
Darin Petkovc0865312011-09-16 15:31:20 -0700182void Device::Scan(Error *error) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700183 VLOG(2) << "Device " << link_name_ << " scan requested.";
Paul Stewartbe005172011-11-02 18:10:29 -0700184 Error::PopulateAndLog(error, Error::kNotSupported,
185 "Device doesn't support scan.");
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700186}
187
mukesh agrawal1830fa12011-09-26 14:31:40 -0700188void Device::RegisterOnNetwork(const std::string &/*network_id*/,
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500189 ReturnerInterface *returner) {
190 Error error;
191 Error::PopulateAndLog(&error, Error::kNotSupported,
Paul Stewartbe005172011-11-02 18:10:29 -0700192 "Device doesn't support network registration.");
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500193 returner->ReturnError(error);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700194}
195
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100196void Device::RequirePIN(
197 const string &/*pin*/, bool /*require*/, ReturnerInterface *returner) {
198 VLOG(2) << __func__;
199 Error error;
200 Error::PopulateAndLog(
201 &error, Error::kNotSupported, "Device doesn't support RequirePIN.");
202 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700203}
204
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100205void Device::EnterPIN(const string &/*pin*/, ReturnerInterface *returner) {
206 VLOG(2) << __func__;
207 Error error;
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100208 Error::PopulateAndLog(
209 &error, Error::kNotSupported, "Device doesn't support EnterPIN.");
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100210 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700211}
212
mukesh agrawal1830fa12011-09-26 14:31:40 -0700213void Device::UnblockPIN(const string &/*unblock_code*/,
214 const string &/*pin*/,
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100215 ReturnerInterface *returner) {
216 VLOG(2) << __func__;
217 Error error;
218 Error::PopulateAndLog(
219 &error, Error::kNotSupported, "Device doesn't support UnblockPIN.");
220 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700221}
222
mukesh agrawal1830fa12011-09-26 14:31:40 -0700223void Device::ChangePIN(const string &/*old_pin*/,
224 const string &/*new_pin*/,
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100225 ReturnerInterface *returner) {
226 VLOG(2) << __func__;
227 Error error;
228 Error::PopulateAndLog(
229 &error, Error::kNotSupported, "Device doesn't support ChangePIN.");
230 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700231}
232
Paul Stewart2bf1d352011-12-06 15:02:55 -0800233void Device::DisableIPv6() {
234 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
235}
236
237void Device::EnableIPv6() {
238 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
239}
240
241void Device::EnableIPv6Privacy() {
242 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
243 kIPFlagUseTempAddrUsedAndDefault);
244}
245
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800246void Device::DisableReversePathFilter() {
247 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
248 // for IPv6. crosbug.com/24228
249 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
250 kIPFlagReversePathFilterLooseMode);
251}
252
253void Device::EnableReversePathFilter() {
254 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
255 kIPFlagReversePathFilterEnabled);
256}
257
Gaurav Shah435de2c2011-11-17 19:01:07 -0800258bool Device::IsConnected() const {
259 if (selected_service_)
260 return selected_service_->IsConnected();
261 return false;
262}
263
Chris Masone27c4aa52011-07-02 13:10:14 -0700264string Device::GetRpcIdentifier() {
265 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700266}
267
Chris Masone5dec5f42011-07-22 14:07:55 -0700268string Device::GetStorageIdentifier() {
269 string id = GetRpcIdentifier();
270 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700271 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700272 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700273 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700274 return id;
275}
276
Jason Glasgowb5790052012-01-27 01:03:52 -0500277string Device::GetTechnologyString(Error */*error*/) {
278 return Technology::NameFromIdentifier(technology());
279}
280
Chris Masone19e30402011-07-19 15:48:47 -0700281const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700282 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700283}
284
Chris Masone19e30402011-07-19 15:48:47 -0700285const string& Device::UniqueName() const {
286 return unique_id_;
287}
288
Chris Masone5dec5f42011-07-22 14:07:55 -0700289bool Device::Load(StoreInterface *storage) {
290 const string id = GetStorageIdentifier();
291 if (!storage->ContainsGroup(id)) {
292 LOG(WARNING) << "Device is not available in the persistent store: " << id;
293 return false;
294 }
295 storage->GetBool(id, kStoragePowered, &powered_);
296 // TODO(cmasone): What does it mean to load an IPConfig identifier??
297 return true;
298}
299
300bool Device::Save(StoreInterface *storage) {
301 const string id = GetStorageIdentifier();
302 storage->SetBool(id, kStoragePowered, powered_);
Chris Masone34af2182011-08-22 11:59:36 -0700303 if (ipconfig_.get()) {
304 // The _0 is an index into the list of IPConfigs that this device might
305 // have. We only have one IPConfig right now, and I hope to never have
306 // to support more, as sleffler indicates that associating IPConfigs
307 // with devices is wrong and due to be changed in flimflam anyhow.
308 string suffix = hardware_address_ + "_0";
309 ipconfig_->Save(storage, suffix);
310 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
311 }
Chris Masone5dec5f42011-07-22 14:07:55 -0700312 return true;
313}
314
Darin Petkovafa6fc42011-06-21 16:21:08 -0700315void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800316 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700317 if (ipconfig_.get()) {
318 ipconfig_->ReleaseIP();
319 ipconfig_ = NULL;
320 }
Paul Stewarte6132022011-08-16 09:11:02 -0700321 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700322}
323
Paul Stewart2bf1d352011-12-06 15:02:55 -0800324bool Device::AcquireIPConfig() {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700325 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800326 EnableIPv6();
Paul Stewartd32f4842012-01-11 16:08:13 -0800327 ipconfig_ = dhcp_provider_->CreateConfig(link_name_, manager_->GetHostName());
Darin Petkovafa6fc42011-06-21 16:21:08 -0700328 ipconfig_->RegisterUpdateCallback(
Darin Petkov79d74c92012-03-07 17:20:32 +0100329 NewCallback(this, &Device::OnIPConfigUpdated));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700330 return ipconfig_->RequestIP();
331}
332
Jason Glasgowb5790052012-01-27 01:03:52 -0500333void Device::HelpRegisterDerivedString(
334 const string &name,
335 string(Device::*get)(Error *error),
336 void(Device::*set)(const string &value, Error *error)) {
337 store_.RegisterDerivedString(
338 name,
339 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
340}
341
mukesh agrawalffa3d042011-10-06 15:26:10 -0700342void Device::HelpRegisterDerivedStrings(
343 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800344 Strings(Device::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700345 void(Device::*set)(const Strings&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700346 store_.RegisterDerivedStrings(
347 name,
348 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700349}
350
Darin Petkov79d74c92012-03-07 17:20:32 +0100351void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700352 VLOG(2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700353 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700354 CreateConnection();
355 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800356 // SetConnection must occur after the UpdateFromIPConfig so the
357 // service can use the values derived from the connection.
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800358 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800359 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800360 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800361 // The service state change needs to happen last, so that at the
362 // time we report the state change to the manager, the service
363 // has its connection.
364 SetServiceState(Service::kStateConnected);
Paul Stewart20088d82012-02-16 06:58:55 -0800365 // Subtle: Start portal detection after transitioning the service
366 // to the Connected state because this call may immediately transition
367 // to the Online state.
368 StartPortalDetection();
Paul Stewarte6132022011-08-16 09:11:02 -0700369 } else {
370 // TODO(pstew): This logic gets more complex when multiple IPConfig types
371 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700372 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700373 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700374 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700375}
376
Paul Stewarte6132022011-08-16 09:11:02 -0700377void Device::CreateConnection() {
378 VLOG(2) << __func__;
379 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700380 connection_ = new Connection(interface_index_,
381 link_name_,
382 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700383 }
384}
385
386void Device::DestroyConnection() {
387 VLOG(2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800388 StopPortalDetection();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800389 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800390 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800391 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800392 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700393}
394
Paul Stewart03dba0b2011-08-22 16:32:45 -0700395void Device::SelectService(const ServiceRefPtr &service) {
Paul Stewarta2b8cd12011-08-25 14:55:32 -0700396 VLOG(2) << __func__ << ": "
mukesh agrawal15908392011-11-16 18:29:25 +0000397 << (service.get() ?
398 StringPrintf("%s (%s)",
399 service->UniqueName().c_str(),
400 service->friendly_name().c_str()) :
401 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000402
403 if (selected_service_.get() == service.get()) {
404 // No change to |selected_service_|. Return early to avoid
405 // changing its state.
406 return;
407 }
408
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800409 if (selected_service_.get()) {
410 if (selected_service_->state() != Service::kStateFailure) {
411 selected_service_->SetState(Service::kStateIdle);
412 }
413 // TODO(pstew): We need to revisit the model here: should the Device
414 // subclass be responsible for calling DestroyIPConfig() (which would
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800415 // trigger DestroyConnection() and Service::SetConnection(NULL))?
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800416 // Ethernet does, but WiFi currently does not. crosbug.com/23929
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800417 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700418 }
419 selected_service_ = service;
420}
421
422void Device::SetServiceState(Service::ConnectState state) {
423 if (selected_service_.get()) {
424 selected_service_->SetState(state);
425 }
426}
427
428void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
429 if (selected_service_.get()) {
430 selected_service_->SetFailure(failure_state);
431 }
432}
433
Chris Masone34af2182011-08-22 11:59:36 -0700434string Device::SerializeIPConfigs(const string &suffix) {
435 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700436}
437
Paul Stewart2bf1d352011-12-06 15:02:55 -0800438bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
439 const string &value) {
440 string ip_version;
441 if (family == IPAddress::kFamilyIPv4) {
442 ip_version = kIPFlagVersion4;
443 } else if (family == IPAddress::kFamilyIPv6) {
444 ip_version = kIPFlagVersion6;
445 } else {
446 NOTIMPLEMENTED();
447 }
448 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
449 link_name_.c_str(), flag.c_str()));
450 VLOG(2) << "Writing " << value << " to flag file " << flag_file.value();
451 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
452 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
453 value.c_str(), flag_file.value().c_str());
454 return false;
455 }
456 return true;
457}
458
Paul Stewartc681fa02012-03-02 19:40:04 -0800459bool Device::RequestPortalDetection() {
460 if (!selected_service_) {
461 VLOG(2) << FriendlyName()
462 << ": No selected service, so no need for portal check.";
463 return false;
464 }
465
466 if (!connection_.get()) {
467 VLOG(2) << FriendlyName()
468 << ": No connection, so no need for portal check.";
469 return false;
470 }
471
472 if (selected_service_->state() != Service::kStatePortal) {
473 VLOG(2) << FriendlyName()
474 << ": Service is not in portal state. No need to start check.";
475 return false;
476 }
477
478 if (!connection_->is_default()) {
479 VLOG(2) << FriendlyName()
480 << ": Service is not the default connection. Don't start check.";
481 return false;
482 }
483
484 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
485 VLOG(2) << FriendlyName() << ": Portal detection is already running.";
486 return true;
487 }
488
489 return StartPortalDetection();
490}
491
Paul Stewart20088d82012-02-16 06:58:55 -0800492bool Device::StartPortalDetection() {
493 if (!manager_->IsPortalDetectionEnabled(technology())) {
494 // If portal detection is disabled for this technology, immediately set
495 // the service state to "Online".
496 VLOG(2) << "Device " << FriendlyName()
497 << ": portal detection is disabled; marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800498 SetServiceConnectedState(Service::kStateOnline);
499 return false;
500 }
501
502 DCHECK(selected_service_.get());
503 if (selected_service_->HasProxyConfig()) {
504 // Services with HTTP proxy configurations should not be checked by the
505 // connection manager, since we don't have the ability to evaluate
506 // arbitrary proxy configs and their possible credentials.
507 VLOG(2) << "Device " << FriendlyName()
508 << ": service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800509 SetServiceConnectedState(Service::kStateOnline);
510 return false;
511 }
512
513 portal_detector_.reset(new PortalDetector(connection_,
514 dispatcher_,
515 portal_detector_callback_.get()));
516 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
517 LOG(ERROR) << "Device " << FriendlyName()
518 << ": Portal detection failed to start: likely bad URL: "
519 << manager_->GetPortalCheckURL();
520 SetServiceConnectedState(Service::kStateOnline);
521 return false;
522 }
523
524 VLOG(2) << "Device " << FriendlyName() << ": portal detection has started.";
525 return true;
526}
527
528void Device::StopPortalDetection() {
529 VLOG(2) << "Device " << FriendlyName() << ": portal detection has stopped.";
530 portal_detector_.reset();
531}
532
533void Device::SetServiceConnectedState(Service::ConnectState state) {
534 DCHECK(selected_service_.get());
535
536 if (!selected_service_.get()) {
537 LOG(ERROR) << FriendlyName() << ": "
538 << "Portal detection completed but no selected service exists!";
539 return;
540 }
541
542 if (!selected_service_->IsConnected()) {
543 LOG(ERROR) << FriendlyName() << ": "
544 << "Portal detection completed but selected service "
545 << selected_service_->UniqueName()
546 << " is in non-connected state.";
547 return;
548 }
549
Paul Stewartc681fa02012-03-02 19:40:04 -0800550 if (state == Service::kStatePortal && connection_->is_default() &&
551 manager_->GetPortalCheckInterval() != 0) {
552 CHECK(portal_detector_.get());
553 if (!portal_detector_->StartAfterDelay(
554 manager_->GetPortalCheckURL(),
555 manager_->GetPortalCheckInterval())) {
556 LOG(ERROR) << "Device " << FriendlyName()
557 << ": Portal detection failed to restart: likely bad URL: "
558 << manager_->GetPortalCheckURL();
559 SetServiceState(Service::kStateOnline);
560 portal_detector_.reset();
561 return;
562 }
563 VLOG(2) << "Device " << FriendlyName() << ": portal detection retrying.";
564 } else {
565 VLOG(2) << "Device " << FriendlyName() << ": portal will not retry.";
566 portal_detector_.reset();
567 }
568
Paul Stewart20088d82012-02-16 06:58:55 -0800569 SetServiceState(state);
570}
571
572void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
573 if (!result.final) {
574 VLOG(2) << "Device " << FriendlyName()
575 << ": received non-final status: "
576 << PortalDetector::StatusToString(result.status);
577 return;
578 }
579
580 VLOG(2) << "Device " << FriendlyName()
581 << ": received final status: "
582 << PortalDetector::StatusToString(result.status);
583
584 if (result.status == PortalDetector::kStatusSuccess) {
585 SetServiceConnectedState(Service::kStateOnline);
586 } else {
587 SetServiceConnectedState(Service::kStatePortal);
588 }
589}
590
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800591vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Chris Masone4e851612011-07-01 10:46:53 -0700592 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
593 return vector<string>(1, id);
594}
595
596string Device::GetRpcConnectionIdentifier() {
597 return adaptor_->GetRpcConnectionIdentifier();
598}
599
Paul Stewart75897df2011-04-27 09:05:53 -0700600} // namespace shill