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