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