Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -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 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 5 | #include "update_engine/connection_manager.h" |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 6 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 7 | #include <set> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 10 | #include <base/stl_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 11 | #include <base/strings/string_util.h> |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 13 | #include <policy/device_policy.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 14 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 15 | #include "update_engine/prefs.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 16 | #include "update_engine/system_state.h" |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 17 | #include "update_engine/utils.h" |
| 18 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 19 | using org::chromium::flimflam::ManagerProxyInterface; |
| 20 | using org::chromium::flimflam::ServiceProxyInterface; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 21 | using std::set; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 22 | using std::string; |
| 23 | |
| 24 | namespace chromeos_update_engine { |
| 25 | |
| 26 | namespace { |
| 27 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 28 | NetworkConnectionType ParseConnectionType(const string& type_str) { |
| 29 | if (type_str == shill::kTypeEthernet) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 30 | return NetworkConnectionType::kEthernet; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 31 | } else if (type_str == shill::kTypeWifi) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 32 | return NetworkConnectionType::kWifi; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 33 | } else if (type_str == shill::kTypeWimax) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 34 | return NetworkConnectionType::kWimax; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 35 | } else if (type_str == shill::kTypeBluetooth) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 36 | return NetworkConnectionType::kBluetooth; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 37 | } else if (type_str == shill::kTypeCellular) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 38 | return NetworkConnectionType::kCellular; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 39 | } |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 40 | return NetworkConnectionType::kUnknown; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 43 | NetworkTethering ParseTethering(const string& tethering_str) { |
| 44 | if (tethering_str == shill::kTetheringNotDetectedState) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 45 | return NetworkTethering::kNotDetected; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 46 | } else if (tethering_str == shill::kTetheringSuspectedState) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 47 | return NetworkTethering::kSuspected; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 48 | } else if (tethering_str == shill::kTetheringConfirmedState) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 49 | return NetworkTethering::kConfirmed; |
| 50 | } |
| 51 | LOG(WARNING) << "Unknown Tethering value: " << tethering_str; |
| 52 | return NetworkTethering::kUnknown; |
| 53 | } |
| 54 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 55 | } // namespace |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 56 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 57 | ConnectionManager::ConnectionManager(ShillProxyInterface* shill_proxy, |
| 58 | SystemState* system_state) |
| 59 | : shill_proxy_(shill_proxy), system_state_(system_state) {} |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 60 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 61 | bool ConnectionManager::IsUpdateAllowedOver(NetworkConnectionType type, |
| 62 | NetworkTethering tethering) const { |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 63 | switch (type) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 64 | case NetworkConnectionType::kBluetooth: |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 65 | return false; |
| 66 | |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 67 | case NetworkConnectionType::kCellular: { |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 68 | set<string> allowed_types; |
| 69 | const policy::DevicePolicy* device_policy = |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 70 | system_state_->device_policy(); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 71 | |
| 72 | // A device_policy is loaded in a lazy way right before an update check, |
| 73 | // so the device_policy should be already loaded at this point. If it's |
| 74 | // not, return a safe value for this setting. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 75 | if (!device_policy) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 76 | LOG(INFO) << "Disabling updates over cellular networks as there's no " |
| 77 | "device policy loaded yet."; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 81 | if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) { |
| 82 | // The update setting is enforced by the device policy. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 83 | |
Gilad Arnold | 9a423ff | 2014-03-27 15:27:35 -0700 | [diff] [blame] | 84 | if (!ContainsKey(allowed_types, shill::kTypeCellular)) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 85 | LOG(INFO) << "Disabling updates over cellular connection as it's not " |
| 86 | "allowed in the device policy."; |
| 87 | return false; |
| 88 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 89 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 90 | LOG(INFO) << "Allowing updates over cellular per device policy."; |
| 91 | return true; |
| 92 | } else { |
| 93 | // There's no update setting in the device policy, using the local user |
| 94 | // setting. |
| 95 | PrefsInterface* prefs = system_state_->prefs(); |
| 96 | |
| 97 | if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) { |
| 98 | LOG(INFO) << "Disabling updates over cellular connection as there's " |
| 99 | "no device policy setting nor user preference present."; |
| 100 | return false; |
| 101 | } |
| 102 | |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 103 | bool stored_value; |
| 104 | if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, |
| 105 | &stored_value)) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 106 | return false; |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 107 | } |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 108 | |
| 109 | if (!stored_value) { |
| 110 | LOG(INFO) << "Disabling updates over cellular connection per user " |
| 111 | "setting."; |
| 112 | return false; |
| 113 | } |
| 114 | LOG(INFO) << "Allowing updates over cellular per user setting."; |
| 115 | return true; |
| 116 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | default: |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 120 | if (tethering == NetworkTethering::kConfirmed) { |
| 121 | // Treat this connection as if it is a cellular connection. |
| 122 | LOG(INFO) << "Current connection is confirmed tethered, using Cellular " |
| 123 | "setting."; |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 124 | return IsUpdateAllowedOver(NetworkConnectionType::kCellular, |
| 125 | NetworkTethering::kUnknown); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 126 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | } |
| 130 | |
Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 131 | // static |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 132 | const char* ConnectionManager::StringForConnectionType( |
Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 133 | NetworkConnectionType type) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 134 | switch (type) { |
| 135 | case NetworkConnectionType::kEthernet: |
| 136 | return shill::kTypeEthernet; |
| 137 | case NetworkConnectionType::kWifi: |
| 138 | return shill::kTypeWifi; |
| 139 | case NetworkConnectionType::kWimax: |
| 140 | return shill::kTypeWimax; |
| 141 | case NetworkConnectionType::kBluetooth: |
| 142 | return shill::kTypeBluetooth; |
| 143 | case NetworkConnectionType::kCellular: |
| 144 | return shill::kTypeCellular; |
| 145 | case NetworkConnectionType::kUnknown: |
| 146 | return "Unknown"; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 147 | } |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 148 | return "Unknown"; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 151 | bool ConnectionManager::GetConnectionProperties( |
| 152 | NetworkConnectionType* out_type, |
| 153 | NetworkTethering* out_tethering) { |
| 154 | string default_service_path; |
| 155 | TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path)); |
| 156 | if (default_service_path.empty()) |
| 157 | return false; |
| 158 | TEST_AND_RETURN_FALSE( |
| 159 | GetServicePathProperties(default_service_path, out_type, out_tethering)); |
| 160 | return true; |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 163 | bool ConnectionManager::GetDefaultServicePath(string* out_path) { |
| 164 | chromeos::VariantDictionary properties; |
| 165 | chromeos::ErrorPtr error; |
| 166 | ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy(); |
| 167 | if (!manager_proxy) |
| 168 | return false; |
| 169 | TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error)); |
| 170 | |
| 171 | const auto& prop_default_service = |
| 172 | properties.find(shill::kDefaultServiceProperty); |
| 173 | if (prop_default_service == properties.end()) |
| 174 | return false; |
| 175 | |
| 176 | *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>().value(); |
| 177 | return !out_path->empty(); |
| 178 | } |
| 179 | |
| 180 | bool ConnectionManager::GetServicePathProperties( |
| 181 | const string& path, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 182 | NetworkConnectionType* out_type, |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 183 | NetworkTethering* out_tethering) { |
| 184 | // We create and dispose the ServiceProxyInterface on every request. |
| 185 | std::unique_ptr<ServiceProxyInterface> service = |
| 186 | shill_proxy_->GetServiceForPath(path); |
| 187 | |
| 188 | chromeos::VariantDictionary properties; |
| 189 | chromeos::ErrorPtr error; |
| 190 | TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error)); |
| 191 | |
| 192 | // Populate the out_tethering. |
| 193 | const auto& prop_tethering = properties.find(shill::kTetheringProperty); |
| 194 | if (prop_tethering == properties.end()) { |
| 195 | // Set to Unknown if not present. |
| 196 | *out_tethering = NetworkTethering::kUnknown; |
| 197 | } else { |
| 198 | // If the property doesn't contain a string value, the empty string will |
| 199 | // become kUnknown. |
| 200 | *out_tethering = ParseTethering(prop_tethering->second.TryGet<string>()); |
| 201 | } |
| 202 | |
| 203 | // Populate the out_type property. |
| 204 | const auto& prop_type = properties.find(shill::kTypeProperty); |
| 205 | if (prop_type == properties.end()) { |
| 206 | // Set to Unknown if not present. |
| 207 | *out_type = NetworkConnectionType::kUnknown; |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | string type_str = prop_type->second.TryGet<string>(); |
| 212 | if (type_str == shill::kTypeVPN) { |
| 213 | const auto& prop_physical = |
| 214 | properties.find(shill::kPhysicalTechnologyProperty); |
| 215 | if (prop_physical == properties.end()) { |
| 216 | LOG(ERROR) << "No PhysicalTechnology property found for a VPN" |
| 217 | << " connection (service: " << path << "). Returning default" |
| 218 | << " kUnknown value."; |
| 219 | *out_type = NetworkConnectionType::kUnknown; |
| 220 | } else { |
| 221 | *out_type = ParseConnectionType(prop_physical->second.TryGet<string>()); |
| 222 | } |
| 223 | } else { |
| 224 | *out_type = ParseConnectionType(type_str); |
| 225 | } |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 229 | } // namespace chromeos_update_engine |