mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 5 | #include "shill/device.h" |
| 6 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 7 | #include <netinet/in.h> |
| 8 | #include <linux/if.h> // Needs definitions from netinet/in.h |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 10 | #include <time.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 11 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 12 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 13 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 14 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 15 | #include <base/file_util.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 16 | #include <base/logging.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 17 | #include <base/memory/ref_counted.h> |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 18 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 19 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 20 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 21 | #include "shill/connection.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 22 | #include "shill/control_interface.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 23 | #include "shill/device_dbus_adaptor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 24 | #include "shill/dhcp_config.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 25 | #include "shill/dhcp_provider.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 26 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 27 | #include "shill/event_dispatcher.h" |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 28 | #include "shill/http_proxy.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 29 | #include "shill/manager.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 30 | #include "shill/property_accessor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 31 | #include "shill/refptr_types.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 32 | #include "shill/rtnl_handler.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 33 | #include "shill/service.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 34 | #include "shill/store_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 35 | #include "shill/technology.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 36 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 37 | using base::StringPrintf; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 38 | using std::string; |
| 39 | using std::vector; |
| 40 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 41 | namespace shill { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 42 | |
| 43 | // static |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 44 | const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s"; |
| 45 | // static |
| 46 | const char Device::kIPFlagVersion4[] = "ipv4"; |
| 47 | // static |
| 48 | const char Device::kIPFlagVersion6[] = "ipv6"; |
| 49 | // static |
| 50 | const char Device::kIPFlagDisableIPv6[] = "disable_ipv6"; |
| 51 | // static |
| 52 | const char Device::kIPFlagUseTempAddr[] = "use_tempaddr"; |
| 53 | // static |
| 54 | const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2"; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 55 | // static |
| 56 | const char Device::kIPFlagReversePathFilter[] = "rp_filter"; |
| 57 | // static |
| 58 | const char Device::kIPFlagReversePathFilterEnabled[] = "1"; |
| 59 | // static |
| 60 | const char Device::kIPFlagReversePathFilterLooseMode[] = "2"; |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 61 | |
| 62 | // static |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 63 | const char Device::kStoragePowered[] = "Powered"; |
| 64 | |
| 65 | // static |
| 66 | const char Device::kStorageIPConfigs[] = "IPConfigs"; |
| 67 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 68 | Device::Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 69 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 70 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 71 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 72 | const string &link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 73 | const string &address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 74 | int interface_index, |
| 75 | Technology::Identifier technology) |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 76 | : powered_(true), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 77 | reconnect_(true), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 78 | hardware_address_(address), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 79 | interface_index_(interface_index), |
| 80 | running_(false), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 81 | link_name_(link_name), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 82 | unique_id_(link_name), |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 83 | control_interface_(control_interface), |
| 84 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 85 | metrics_(metrics), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 86 | manager_(manager), |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 87 | adaptor_(control_interface->CreateDeviceAdaptor(this)), |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame^] | 88 | portal_detector_callback_( |
| 89 | NewCallback(this, &Device::PortalDetectorCallback)), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 90 | technology_(technology), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 91 | dhcp_provider_(DHCPProvider::GetInstance()), |
| 92 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 93 | store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 94 | |
| 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 Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 102 | // flimflam::kHomeProviderProperty: Registered in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 103 | // 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 Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 115 | // flimflam::kDBusConnectionProperty: Registered in Cellular |
| 116 | // flimflam::kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 117 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 118 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 119 | // 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 Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 125 | HelpRegisterDerivedString(flimflam::kTypeProperty, |
| 126 | &Device::GetTechnologyString, |
| 127 | NULL); |
| 128 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 129 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 130 | // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 131 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 132 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 133 | // known_properties_.push_back(flimflam::kNetworksProperty); |
| 134 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 135 | // 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 Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 139 | VLOG(2) << "Device " << link_name_ << " index " << interface_index; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | Device::~Device() { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 143 | VLOG(2) << "Device " << link_name_ << " destroyed."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void Device::Start() { |
| 147 | running_ = true; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 148 | VLOG(2) << "Device " << link_name_ << " starting."; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 149 | adaptor_->UpdateEnabled(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void Device::Stop() { |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 153 | VLOG(2) << "Device " << link_name_ << " stopping."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 154 | running_ = false; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 155 | DestroyIPConfig(); // breaks a reference cycle |
| 156 | SelectService(NULL); // breaks a reference cycle |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 157 | adaptor_->UpdateEnabled(); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 158 | rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP); |
| 159 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 160 | 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 Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 166 | } |
| 167 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 168 | bool Device::TechnologyIs(const Technology::Identifier /*type*/) const { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 172 | void Device::LinkEvent(unsigned flags, unsigned change) { |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 173 | VLOG(2) << "Device " << link_name_ |
| 174 | << std::showbase << std::hex |
| 175 | << " flags " << flags << " changed " << change |
| 176 | << std::dec << std::noshowbase; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 179 | void Device::Scan(Error *error) { |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 180 | VLOG(2) << "Device " << link_name_ << " scan requested."; |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 181 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 182 | "Device doesn't support scan."); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 183 | } |
| 184 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 185 | void Device::RegisterOnNetwork(const std::string &/*network_id*/, |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 186 | ReturnerInterface *returner) { |
| 187 | Error error; |
| 188 | Error::PopulateAndLog(&error, Error::kNotSupported, |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 189 | "Device doesn't support network registration."); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 190 | returner->ReturnError(error); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 193 | void 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 Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 202 | void Device::EnterPIN(const string &/*pin*/, ReturnerInterface *returner) { |
| 203 | VLOG(2) << __func__; |
| 204 | Error error; |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 205 | Error::PopulateAndLog( |
| 206 | &error, Error::kNotSupported, "Device doesn't support EnterPIN."); |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 207 | returner->ReturnError(error); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 208 | } |
| 209 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 210 | void Device::UnblockPIN(const string &/*unblock_code*/, |
| 211 | const string &/*pin*/, |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 212 | 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 Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 218 | } |
| 219 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 220 | void Device::ChangePIN(const string &/*old_pin*/, |
| 221 | const string &/*new_pin*/, |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 222 | 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 Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 230 | void Device::DisableIPv6() { |
| 231 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1"); |
| 232 | } |
| 233 | |
| 234 | void Device::EnableIPv6() { |
| 235 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0"); |
| 236 | } |
| 237 | |
| 238 | void Device::EnableIPv6Privacy() { |
| 239 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr, |
| 240 | kIPFlagUseTempAddrUsedAndDefault); |
| 241 | } |
| 242 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 243 | void 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 | |
| 250 | void Device::EnableReversePathFilter() { |
| 251 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 252 | kIPFlagReversePathFilterEnabled); |
| 253 | } |
| 254 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 255 | bool Device::IsConnected() const { |
| 256 | if (selected_service_) |
| 257 | return selected_service_->IsConnected(); |
| 258 | return false; |
| 259 | } |
| 260 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 261 | string Device::GetRpcIdentifier() { |
| 262 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 265 | string Device::GetStorageIdentifier() { |
| 266 | string id = GetRpcIdentifier(); |
| 267 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 268 | size_t needle = id.find('_'); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 269 | DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 270 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 271 | return id; |
| 272 | } |
| 273 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 274 | string Device::GetTechnologyString(Error */*error*/) { |
| 275 | return Technology::NameFromIdentifier(technology()); |
| 276 | } |
| 277 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 278 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 279 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 282 | const string& Device::UniqueName() const { |
| 283 | return unique_id_; |
| 284 | } |
| 285 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 286 | bool 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 | |
| 297 | bool Device::Save(StoreInterface *storage) { |
| 298 | const string id = GetStorageIdentifier(); |
| 299 | storage->SetBool(id, kStoragePowered, powered_); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 300 | 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 Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 309 | return true; |
| 310 | } |
| 311 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 312 | void Device::DestroyIPConfig() { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 313 | DisableIPv6(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 314 | if (ipconfig_.get()) { |
| 315 | ipconfig_->ReleaseIP(); |
| 316 | ipconfig_ = NULL; |
| 317 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 318 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 321 | bool Device::AcquireIPConfig() { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 322 | DestroyIPConfig(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 323 | EnableIPv6(); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 324 | ipconfig_ = dhcp_provider_->CreateConfig(link_name_, manager_->GetHostName()); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 325 | ipconfig_->RegisterUpdateCallback( |
| 326 | NewCallback(this, &Device::IPConfigUpdatedCallback)); |
| 327 | return ipconfig_->RequestIP(); |
| 328 | } |
| 329 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 330 | void 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 agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 339 | void Device::HelpRegisterDerivedStrings( |
| 340 | const string &name, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 341 | Strings(Device::*get)(Error *), |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 342 | void(Device::*set)(const Strings&, Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 343 | store_.RegisterDerivedStrings( |
| 344 | name, |
| 345 | StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 348 | void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, |
| 349 | bool success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 350 | VLOG(2) << __func__ << " " << " success: " << success; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 351 | if (success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 352 | CreateConnection(); |
| 353 | connection_->UpdateFromIPConfig(ipconfig); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 354 | // SetConnection must occur after the UpdateFromIPConfig so the |
| 355 | // service can use the values derived from the connection. |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 356 | if (selected_service_.get()) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 357 | selected_service_->SetConnection(connection_); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 358 | } |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 359 | // The service state change needs to happen last, so that at the |
| 360 | // time we report the state change to the manager, the service |
| 361 | // has its connection. |
| 362 | SetServiceState(Service::kStateConnected); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame^] | 363 | // Subtle: Start portal detection after transitioning the service |
| 364 | // to the Connected state because this call may immediately transition |
| 365 | // to the Online state. |
| 366 | StartPortalDetection(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 367 | } else { |
| 368 | // TODO(pstew): This logic gets more complex when multiple IPConfig types |
| 369 | // are run in parallel (e.g. DHCP and DHCP6) |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 370 | SetServiceState(Service::kStateDisconnected); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 371 | DestroyConnection(); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 372 | } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 375 | void Device::CreateConnection() { |
| 376 | VLOG(2) << __func__; |
| 377 | if (!connection_.get()) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 378 | connection_ = new Connection(interface_index_, |
| 379 | link_name_, |
| 380 | manager_->device_info()); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
| 384 | void Device::DestroyConnection() { |
| 385 | VLOG(2) << __func__; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame^] | 386 | StopPortalDetection(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 387 | if (selected_service_.get()) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 388 | selected_service_->SetConnection(NULL); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 389 | } |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 390 | connection_ = NULL; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 393 | void Device::SelectService(const ServiceRefPtr &service) { |
Paul Stewart | a2b8cd1 | 2011-08-25 14:55:32 -0700 | [diff] [blame] | 394 | VLOG(2) << __func__ << ": " |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 395 | << (service.get() ? |
| 396 | StringPrintf("%s (%s)", |
| 397 | service->UniqueName().c_str(), |
| 398 | service->friendly_name().c_str()) : |
| 399 | "*reset*"); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 400 | |
| 401 | if (selected_service_.get() == service.get()) { |
| 402 | // No change to |selected_service_|. Return early to avoid |
| 403 | // changing its state. |
| 404 | return; |
| 405 | } |
| 406 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 407 | if (selected_service_.get()) { |
| 408 | if (selected_service_->state() != Service::kStateFailure) { |
| 409 | selected_service_->SetState(Service::kStateIdle); |
| 410 | } |
| 411 | // TODO(pstew): We need to revisit the model here: should the Device |
| 412 | // subclass be responsible for calling DestroyIPConfig() (which would |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 413 | // trigger DestroyConnection() and Service::SetConnection(NULL))? |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 414 | // Ethernet does, but WiFi currently does not. crosbug.com/23929 |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 415 | selected_service_->SetConnection(NULL); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 416 | } |
| 417 | selected_service_ = service; |
| 418 | } |
| 419 | |
| 420 | void Device::SetServiceState(Service::ConnectState state) { |
| 421 | if (selected_service_.get()) { |
| 422 | selected_service_->SetState(state); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | void Device::SetServiceFailure(Service::ConnectFailure failure_state) { |
| 427 | if (selected_service_.get()) { |
| 428 | selected_service_->SetFailure(failure_state); |
| 429 | } |
| 430 | } |
| 431 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 432 | string Device::SerializeIPConfigs(const string &suffix) { |
| 433 | return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 436 | bool Device::SetIPFlag(IPAddress::Family family, const string &flag, |
| 437 | const string &value) { |
| 438 | string ip_version; |
| 439 | if (family == IPAddress::kFamilyIPv4) { |
| 440 | ip_version = kIPFlagVersion4; |
| 441 | } else if (family == IPAddress::kFamilyIPv6) { |
| 442 | ip_version = kIPFlagVersion6; |
| 443 | } else { |
| 444 | NOTIMPLEMENTED(); |
| 445 | } |
| 446 | FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(), |
| 447 | link_name_.c_str(), flag.c_str())); |
| 448 | VLOG(2) << "Writing " << value << " to flag file " << flag_file.value(); |
| 449 | if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) { |
| 450 | LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s", |
| 451 | value.c_str(), flag_file.value().c_str()); |
| 452 | return false; |
| 453 | } |
| 454 | return true; |
| 455 | } |
| 456 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame^] | 457 | bool Device::StartPortalDetection() { |
| 458 | if (!manager_->IsPortalDetectionEnabled(technology())) { |
| 459 | // If portal detection is disabled for this technology, immediately set |
| 460 | // the service state to "Online". |
| 461 | VLOG(2) << "Device " << FriendlyName() |
| 462 | << ": portal detection is disabled; marking service online."; |
| 463 | portal_detector_.reset(); |
| 464 | SetServiceConnectedState(Service::kStateOnline); |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | DCHECK(selected_service_.get()); |
| 469 | if (selected_service_->HasProxyConfig()) { |
| 470 | // Services with HTTP proxy configurations should not be checked by the |
| 471 | // connection manager, since we don't have the ability to evaluate |
| 472 | // arbitrary proxy configs and their possible credentials. |
| 473 | VLOG(2) << "Device " << FriendlyName() |
| 474 | << ": service has proxy config; marking it online."; |
| 475 | portal_detector_.reset(); |
| 476 | SetServiceConnectedState(Service::kStateOnline); |
| 477 | return false; |
| 478 | } |
| 479 | |
| 480 | portal_detector_.reset(new PortalDetector(connection_, |
| 481 | dispatcher_, |
| 482 | portal_detector_callback_.get())); |
| 483 | if (!portal_detector_->Start(manager_->GetPortalCheckURL())) { |
| 484 | LOG(ERROR) << "Device " << FriendlyName() |
| 485 | << ": Portal detection failed to start: likely bad URL: " |
| 486 | << manager_->GetPortalCheckURL(); |
| 487 | SetServiceConnectedState(Service::kStateOnline); |
| 488 | return false; |
| 489 | } |
| 490 | |
| 491 | VLOG(2) << "Device " << FriendlyName() << ": portal detection has started."; |
| 492 | return true; |
| 493 | } |
| 494 | |
| 495 | void Device::StopPortalDetection() { |
| 496 | VLOG(2) << "Device " << FriendlyName() << ": portal detection has stopped."; |
| 497 | portal_detector_.reset(); |
| 498 | } |
| 499 | |
| 500 | void Device::SetServiceConnectedState(Service::ConnectState state) { |
| 501 | DCHECK(selected_service_.get()); |
| 502 | |
| 503 | if (!selected_service_.get()) { |
| 504 | LOG(ERROR) << FriendlyName() << ": " |
| 505 | << "Portal detection completed but no selected service exists!"; |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | if (!selected_service_->IsConnected()) { |
| 510 | LOG(ERROR) << FriendlyName() << ": " |
| 511 | << "Portal detection completed but selected service " |
| 512 | << selected_service_->UniqueName() |
| 513 | << " is in non-connected state."; |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | SetServiceState(state); |
| 518 | } |
| 519 | |
| 520 | void Device::PortalDetectorCallback(const PortalDetector::Result &result) { |
| 521 | if (!result.final) { |
| 522 | VLOG(2) << "Device " << FriendlyName() |
| 523 | << ": received non-final status: " |
| 524 | << PortalDetector::StatusToString(result.status); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | VLOG(2) << "Device " << FriendlyName() |
| 529 | << ": received final status: " |
| 530 | << PortalDetector::StatusToString(result.status); |
| 531 | |
| 532 | if (result.status == PortalDetector::kStatusSuccess) { |
| 533 | SetServiceConnectedState(Service::kStateOnline); |
| 534 | } else { |
| 535 | SetServiceConnectedState(Service::kStatePortal); |
| 536 | } |
| 537 | } |
| 538 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 539 | vector<string> Device::AvailableIPConfigs(Error */*error*/) { |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 540 | string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string()); |
| 541 | return vector<string>(1, id); |
| 542 | } |
| 543 | |
| 544 | string Device::GetRpcConnectionIdentifier() { |
| 545 | return adaptor_->GetRpcConnectionIdentifier(); |
| 546 | } |
| 547 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 548 | } // namespace shill |