| 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 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/cros/connection_manager.h" | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 18 |  | 
| Eric Caruso | be8b755 | 2018-01-23 14:59:50 -0800 | [diff] [blame] | 19 | #include <memory> | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 20 | #include <set> | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 21 | #include <string> | 
|  | 22 |  | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 23 | #include <base/stl_util.h> | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 24 | #include <base/strings/string_util.h> | 
| Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 25 | #include <policy/device_policy.h> | 
| Alex Deymo | d6deb1d | 2015-08-28 15:54:37 -0700 | [diff] [blame] | 26 | #include <shill/dbus-constants.h> | 
|  | 27 | #include <shill/dbus-proxies.h> | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 28 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 29 | #include "update_engine/common/connection_utils.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | #include "update_engine/common/prefs.h" | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 31 | #include "update_engine/common/system_state.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 32 | #include "update_engine/common/utils.h" | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 33 | #include "update_engine/cros/shill_proxy.h" | 
|  | 34 | #include "update_engine/cros/update_attempter.h" | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 35 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 36 | using org::chromium::flimflam::ManagerProxyInterface; | 
|  | 37 | using org::chromium::flimflam::ServiceProxyInterface; | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 38 | using std::set; | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 39 | using std::string; | 
|  | 40 |  | 
|  | 41 | namespace chromeos_update_engine { | 
|  | 42 |  | 
| Sen Jiang | f5bebae | 2016-06-03 15:36:54 -0700 | [diff] [blame] | 43 | namespace connection_manager { | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 44 | std::unique_ptr<ConnectionManagerInterface> CreateConnectionManager() { | 
| Sen Jiang | f5bebae | 2016-06-03 15:36:54 -0700 | [diff] [blame] | 45 | return std::unique_ptr<ConnectionManagerInterface>( | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 46 | new ConnectionManager(new ShillProxy())); | 
| Sen Jiang | f5bebae | 2016-06-03 15:36:54 -0700 | [diff] [blame] | 47 | } | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 48 | }  // namespace connection_manager | 
| Sen Jiang | f5bebae | 2016-06-03 15:36:54 -0700 | [diff] [blame] | 49 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 50 | ConnectionManager::ConnectionManager(ShillProxyInterface* shill_proxy) | 
|  | 51 | : shill_proxy_(shill_proxy) {} | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 52 |  | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 53 | bool ConnectionManager::IsUpdateAllowedOver( | 
|  | 54 | ConnectionType type, ConnectionTethering tethering) const { | 
| Alex Khouderchah | bfa8226 | 2019-08-13 15:00:34 -0700 | [diff] [blame] | 55 | if (type != ConnectionType::kCellular) { | 
|  | 56 | if (tethering != ConnectionTethering::kConfirmed) { | 
| Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 57 | return true; | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 58 | } | 
|  | 59 |  | 
| Alex Khouderchah | bfa8226 | 2019-08-13 15:00:34 -0700 | [diff] [blame] | 60 | // Treat this connection as if it is a cellular connection. | 
|  | 61 | LOG(INFO) | 
|  | 62 | << "Current connection is confirmed tethered, using Cellular setting."; | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 63 | } | 
| Alex Khouderchah | bfa8226 | 2019-08-13 15:00:34 -0700 | [diff] [blame] | 64 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 65 | const policy::DevicePolicy* device_policy = | 
|  | 66 | SystemState::Get()->device_policy(); | 
| Alex Khouderchah | bfa8226 | 2019-08-13 15:00:34 -0700 | [diff] [blame] | 67 |  | 
|  | 68 | // The device_policy is loaded in a lazy way before an update check. Load | 
|  | 69 | // it now from the libbrillo cache if it wasn't already loaded. | 
|  | 70 | if (!device_policy) { | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 71 | UpdateAttempter* update_attempter = SystemState::Get()->update_attempter(); | 
| Alex Khouderchah | bfa8226 | 2019-08-13 15:00:34 -0700 | [diff] [blame] | 72 | if (update_attempter) { | 
|  | 73 | update_attempter->RefreshDevicePolicy(); | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 74 | device_policy = SystemState::Get()->device_policy(); | 
| Alex Khouderchah | bfa8226 | 2019-08-13 15:00:34 -0700 | [diff] [blame] | 75 | } | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | if (!device_policy) { | 
|  | 79 | // Device policy fails to be loaded (possibly due to guest account). We | 
|  | 80 | // do not check the local user setting here, which should be checked by | 
|  | 81 | // |OmahaRequestAction| during checking for update. | 
|  | 82 | LOG(INFO) << "Allowing updates over cellular as device policy fails to be " | 
|  | 83 | "loaded."; | 
|  | 84 | return true; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | set<string> allowed_types; | 
|  | 88 | if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) { | 
|  | 89 | // The update setting is enforced by the device policy. | 
|  | 90 |  | 
| hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame] | 91 | // TODO(crbug.com/1054279): Use base::Contains after uprev to r680000. | 
|  | 92 | if (allowed_types.find(shill::kTypeCellular) == allowed_types.end()) { | 
| Alex Khouderchah | bfa8226 | 2019-08-13 15:00:34 -0700 | [diff] [blame] | 93 | LOG(INFO) << "Disabling updates over cellular connection as it's not " | 
|  | 94 | "allowed in the device policy."; | 
|  | 95 | return false; | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | LOG(INFO) << "Allowing updates over cellular per device policy."; | 
|  | 99 | return true; | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | // If there's no update setting in the device policy, we do not check | 
|  | 103 | // the local user setting here, which should be checked by | 
|  | 104 | // |OmahaRequestAction| during checking for update. | 
|  | 105 | LOG(INFO) << "Allowing updates over cellular as device policy does " | 
|  | 106 | "not include update setting."; | 
|  | 107 | return true; | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
| Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 110 | bool ConnectionManager::IsAllowedConnectionTypesForUpdateSet() const { | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 111 | const policy::DevicePolicy* device_policy = | 
|  | 112 | SystemState::Get()->device_policy(); | 
| Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 113 | if (!device_policy) { | 
|  | 114 | LOG(INFO) << "There's no device policy loaded yet."; | 
|  | 115 | return false; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | set<string> allowed_types; | 
|  | 119 | if (!device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) { | 
|  | 120 | return false; | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | return true; | 
|  | 124 | } | 
|  | 125 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 126 | bool ConnectionManager::GetConnectionProperties( | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 127 | ConnectionType* out_type, ConnectionTethering* out_tethering) { | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 128 | dbus::ObjectPath default_service_path; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 129 | TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path)); | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 130 | if (!default_service_path.IsValid()) | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 131 | return false; | 
| Alex Deymo | 1fbaac8 | 2015-11-04 04:41:40 -0800 | [diff] [blame] | 132 | // Shill uses the "/" service path to indicate that it is not connected. | 
| Colin Howes | c9e98d6 | 2018-09-18 10:35:20 -0700 | [diff] [blame] | 133 | if (default_service_path.value() == "/") { | 
|  | 134 | *out_type = ConnectionType::kDisconnected; | 
|  | 135 | *out_tethering = ConnectionTethering::kUnknown; | 
|  | 136 | return true; | 
|  | 137 | } | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 138 | TEST_AND_RETURN_FALSE( | 
|  | 139 | GetServicePathProperties(default_service_path, out_type, out_tethering)); | 
|  | 140 | return true; | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 141 | } | 
|  | 142 |  | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 143 | bool ConnectionManager::GetDefaultServicePath(dbus::ObjectPath* out_path) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 144 | brillo::VariantDictionary properties; | 
|  | 145 | brillo::ErrorPtr error; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 146 | ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy(); | 
|  | 147 | if (!manager_proxy) | 
|  | 148 | return false; | 
|  | 149 | TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error)); | 
|  | 150 |  | 
|  | 151 | const auto& prop_default_service = | 
|  | 152 | properties.find(shill::kDefaultServiceProperty); | 
|  | 153 | if (prop_default_service == properties.end()) | 
|  | 154 | return false; | 
|  | 155 |  | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 156 | *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>(); | 
|  | 157 | return out_path->IsValid(); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 158 | } | 
|  | 159 |  | 
|  | 160 | bool ConnectionManager::GetServicePathProperties( | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 161 | const dbus::ObjectPath& path, | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 162 | ConnectionType* out_type, | 
|  | 163 | ConnectionTethering* out_tethering) { | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 164 | // We create and dispose the ServiceProxyInterface on every request. | 
|  | 165 | std::unique_ptr<ServiceProxyInterface> service = | 
|  | 166 | shill_proxy_->GetServiceForPath(path); | 
|  | 167 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 168 | brillo::VariantDictionary properties; | 
|  | 169 | brillo::ErrorPtr error; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 170 | TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error)); | 
|  | 171 |  | 
|  | 172 | // Populate the out_tethering. | 
|  | 173 | const auto& prop_tethering = properties.find(shill::kTetheringProperty); | 
|  | 174 | if (prop_tethering == properties.end()) { | 
|  | 175 | // Set to Unknown if not present. | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 176 | *out_tethering = ConnectionTethering::kUnknown; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 177 | } else { | 
|  | 178 | // If the property doesn't contain a string value, the empty string will | 
|  | 179 | // become kUnknown. | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 180 | *out_tethering = connection_utils::ParseConnectionTethering( | 
|  | 181 | prop_tethering->second.TryGet<string>()); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 182 | } | 
|  | 183 |  | 
|  | 184 | // Populate the out_type property. | 
|  | 185 | const auto& prop_type = properties.find(shill::kTypeProperty); | 
|  | 186 | if (prop_type == properties.end()) { | 
|  | 187 | // Set to Unknown if not present. | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 188 | *out_type = ConnectionType::kUnknown; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 189 | return false; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | string type_str = prop_type->second.TryGet<string>(); | 
|  | 193 | if (type_str == shill::kTypeVPN) { | 
|  | 194 | const auto& prop_physical = | 
|  | 195 | properties.find(shill::kPhysicalTechnologyProperty); | 
|  | 196 | if (prop_physical == properties.end()) { | 
|  | 197 | LOG(ERROR) << "No PhysicalTechnology property found for a VPN" | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 198 | " connection (service: " | 
|  | 199 | << path.value() << "). Returning default kUnknown value."; | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 200 | *out_type = ConnectionType::kUnknown; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 201 | } else { | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 202 | *out_type = connection_utils::ParseConnectionType( | 
|  | 203 | prop_physical->second.TryGet<string>()); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 204 | } | 
|  | 205 | } else { | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 206 | *out_type = connection_utils::ParseConnectionType(type_str); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 207 | } | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 208 | return true; | 
|  | 209 | } | 
|  | 210 |  | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 211 | }  // namespace chromeos_update_engine |