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