Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 16 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 17 | #include "update_engine/connection_manager.h" |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 18 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 19 | #include <set> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 22 | #include <base/stl_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 23 | #include <base/strings/string_util.h> |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 24 | #include <policy/device_policy.h> |
Alex Deymo | d6deb1d | 2015-08-28 15:54:37 -0700 | [diff] [blame] | 25 | #include <shill/dbus-constants.h> |
| 26 | #include <shill/dbus-proxies.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 27 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #include "update_engine/common/prefs.h" |
| 29 | #include "update_engine/common/utils.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 30 | #include "update_engine/system_state.h" |
Weidong Guo | 70063d9 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 31 | #include "update_engine/update_attempter.h" |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 32 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 33 | using org::chromium::flimflam::ManagerProxyInterface; |
| 34 | using org::chromium::flimflam::ServiceProxyInterface; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 35 | using std::set; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 36 | using std::string; |
| 37 | |
| 38 | namespace chromeos_update_engine { |
| 39 | |
| 40 | namespace { |
| 41 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 42 | NetworkConnectionType ParseConnectionType(const string& type_str) { |
| 43 | if (type_str == shill::kTypeEthernet) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 44 | return NetworkConnectionType::kEthernet; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 45 | } else if (type_str == shill::kTypeWifi) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 46 | return NetworkConnectionType::kWifi; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 47 | } else if (type_str == shill::kTypeWimax) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 48 | return NetworkConnectionType::kWimax; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 49 | } else if (type_str == shill::kTypeBluetooth) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 50 | return NetworkConnectionType::kBluetooth; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 51 | } else if (type_str == shill::kTypeCellular) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 52 | return NetworkConnectionType::kCellular; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 53 | } |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 54 | return NetworkConnectionType::kUnknown; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 57 | NetworkTethering ParseTethering(const string& tethering_str) { |
| 58 | if (tethering_str == shill::kTetheringNotDetectedState) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 59 | return NetworkTethering::kNotDetected; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 60 | } else if (tethering_str == shill::kTetheringSuspectedState) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 61 | return NetworkTethering::kSuspected; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 62 | } else if (tethering_str == shill::kTetheringConfirmedState) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 63 | return NetworkTethering::kConfirmed; |
| 64 | } |
| 65 | LOG(WARNING) << "Unknown Tethering value: " << tethering_str; |
| 66 | return NetworkTethering::kUnknown; |
| 67 | } |
| 68 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 69 | } // namespace |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 70 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 71 | ConnectionManager::ConnectionManager(ShillProxyInterface* shill_proxy, |
| 72 | SystemState* system_state) |
| 73 | : shill_proxy_(shill_proxy), system_state_(system_state) {} |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 74 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 75 | bool ConnectionManager::IsUpdateAllowedOver(NetworkConnectionType type, |
| 76 | NetworkTethering tethering) const { |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 77 | switch (type) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 78 | case NetworkConnectionType::kBluetooth: |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 79 | return false; |
| 80 | |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 81 | case NetworkConnectionType::kCellular: { |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 82 | set<string> allowed_types; |
Weidong Guo | 70063d9 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 83 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 84 | const policy::DevicePolicy* device_policy = |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 85 | system_state_->device_policy(); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 86 | |
Weidong Guo | 70063d9 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 87 | // The device_policy is loaded in a lazy way before an update check. Load |
| 88 | // it now from the libbrillo cache if it wasn't already loaded. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 89 | if (!device_policy) { |
Weidong Guo | 70063d9 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 90 | UpdateAttempter* update_attempter = system_state_->update_attempter(); |
| 91 | if (update_attempter) { |
| 92 | update_attempter->RefreshDevicePolicy(); |
| 93 | device_policy = system_state_->device_policy(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (!device_policy) { |
| 98 | LOG(INFO) << "Disabling updates over cellular as device policy " |
| 99 | "fails to be loaded."; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 103 | if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) { |
| 104 | // The update setting is enforced by the device policy. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 105 | |
Gilad Arnold | 9a423ff | 2014-03-27 15:27:35 -0700 | [diff] [blame] | 106 | if (!ContainsKey(allowed_types, shill::kTypeCellular)) { |
Weidong Guo | 70063d9 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 107 | LOG(INFO) << "Disabling updates over cellular per device policy."; |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 108 | return false; |
| 109 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 110 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 111 | LOG(INFO) << "Allowing updates over cellular per device policy."; |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 112 | } |
Weidong Guo | 70063d9 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 113 | |
| 114 | // If there's no update setting in the device policy, we do not check |
| 115 | // the local user setting here, which should be checked by |
| 116 | // |OmahaRequestAction| during checking for update. |
| 117 | return true; |
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 | |
Weidong Guo | 70063d9 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 132 | bool ConnectionManager::IsAllowedConnectionTypesForUpdateSet() const { |
| 133 | const policy::DevicePolicy* device_policy = system_state_->device_policy(); |
| 134 | if (!device_policy) { |
| 135 | LOG(INFO) << "There's no device policy loaded yet."; |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | set<string> allowed_types; |
| 140 | if (!device_policy->GetAllowedConnectionTypesForUpdate( |
| 141 | &allowed_types)) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 148 | // static |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 149 | const char* ConnectionManager::StringForConnectionType( |
Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 150 | NetworkConnectionType type) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 151 | switch (type) { |
| 152 | case NetworkConnectionType::kEthernet: |
| 153 | return shill::kTypeEthernet; |
| 154 | case NetworkConnectionType::kWifi: |
| 155 | return shill::kTypeWifi; |
| 156 | case NetworkConnectionType::kWimax: |
| 157 | return shill::kTypeWimax; |
| 158 | case NetworkConnectionType::kBluetooth: |
| 159 | return shill::kTypeBluetooth; |
| 160 | case NetworkConnectionType::kCellular: |
| 161 | return shill::kTypeCellular; |
| 162 | case NetworkConnectionType::kUnknown: |
| 163 | return "Unknown"; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 164 | } |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 165 | return "Unknown"; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 168 | bool ConnectionManager::GetConnectionProperties( |
| 169 | NetworkConnectionType* out_type, |
| 170 | NetworkTethering* out_tethering) { |
Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 171 | dbus::ObjectPath default_service_path; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 172 | TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path)); |
Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 173 | if (!default_service_path.IsValid()) |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 174 | return false; |
Alex Deymo | 1fbaac8 | 2015-11-04 04:41:40 -0800 | [diff] [blame] | 175 | // Shill uses the "/" service path to indicate that it is not connected. |
| 176 | if (default_service_path.value() == "/") |
| 177 | return false; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 178 | TEST_AND_RETURN_FALSE( |
| 179 | GetServicePathProperties(default_service_path, out_type, out_tethering)); |
| 180 | return true; |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 183 | bool ConnectionManager::GetDefaultServicePath(dbus::ObjectPath* out_path) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 184 | brillo::VariantDictionary properties; |
| 185 | brillo::ErrorPtr error; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 186 | ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy(); |
| 187 | if (!manager_proxy) |
| 188 | return false; |
| 189 | TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error)); |
| 190 | |
| 191 | const auto& prop_default_service = |
| 192 | properties.find(shill::kDefaultServiceProperty); |
| 193 | if (prop_default_service == properties.end()) |
| 194 | return false; |
| 195 | |
Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 196 | *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>(); |
| 197 | return out_path->IsValid(); |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | bool ConnectionManager::GetServicePathProperties( |
Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 201 | const dbus::ObjectPath& path, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 202 | NetworkConnectionType* out_type, |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 203 | NetworkTethering* out_tethering) { |
| 204 | // We create and dispose the ServiceProxyInterface on every request. |
| 205 | std::unique_ptr<ServiceProxyInterface> service = |
| 206 | shill_proxy_->GetServiceForPath(path); |
| 207 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 208 | brillo::VariantDictionary properties; |
| 209 | brillo::ErrorPtr error; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 210 | TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error)); |
| 211 | |
| 212 | // Populate the out_tethering. |
| 213 | const auto& prop_tethering = properties.find(shill::kTetheringProperty); |
| 214 | if (prop_tethering == properties.end()) { |
| 215 | // Set to Unknown if not present. |
| 216 | *out_tethering = NetworkTethering::kUnknown; |
| 217 | } else { |
| 218 | // If the property doesn't contain a string value, the empty string will |
| 219 | // become kUnknown. |
| 220 | *out_tethering = ParseTethering(prop_tethering->second.TryGet<string>()); |
| 221 | } |
| 222 | |
| 223 | // Populate the out_type property. |
| 224 | const auto& prop_type = properties.find(shill::kTypeProperty); |
| 225 | if (prop_type == properties.end()) { |
| 226 | // Set to Unknown if not present. |
| 227 | *out_type = NetworkConnectionType::kUnknown; |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | string type_str = prop_type->second.TryGet<string>(); |
| 232 | if (type_str == shill::kTypeVPN) { |
| 233 | const auto& prop_physical = |
| 234 | properties.find(shill::kPhysicalTechnologyProperty); |
| 235 | if (prop_physical == properties.end()) { |
| 236 | LOG(ERROR) << "No PhysicalTechnology property found for a VPN" |
Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 237 | " connection (service: " |
| 238 | << path.value() << "). Returning default kUnknown value."; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 239 | *out_type = NetworkConnectionType::kUnknown; |
| 240 | } else { |
| 241 | *out_type = ParseConnectionType(prop_physical->second.TryGet<string>()); |
| 242 | } |
| 243 | } else { |
| 244 | *out_type = ParseConnectionType(type_str); |
| 245 | } |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 246 | return true; |
| 247 | } |
| 248 | |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 249 | } // namespace chromeos_update_engine |