blob: ad7e5f65062beeb344dabe5c8cb44e9214c77204 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Reyesd57d1472010-10-21 13:34:08 -070016
Jay Srinivasan43488792012-06-19 00:25:31 -070017#include "update_engine/connection_manager.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070018
Eric Carusobe8b7552018-01-23 14:59:50 -080019#include <memory>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070020#include <set>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070021#include <string>
22
Jay Srinivasan43488792012-06-19 00:25:31 -070023#include <base/stl_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070024#include <base/strings/string_util.h>
Gilad Arnold1f847232014-04-07 12:07:49 -070025#include <policy/device_policy.h>
Alex Deymod6deb1d2015-08-28 15:54:37 -070026#include <shill/dbus-constants.h>
27#include <shill/dbus-proxies.h>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070028
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/prefs.h"
30#include "update_engine/common/utils.h"
Sen Jiang255e22b2016-05-20 16:15:29 -070031#include "update_engine/connection_utils.h"
Sen Jiangf5bebae2016-06-03 15:36:54 -070032#include "update_engine/shill_proxy.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070033#include "update_engine/system_state.h"
Weidong Guo421ff332017-04-17 10:08:38 -070034#include "update_engine/update_attempter.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070035
Alex Deymo30534502015-07-20 15:06:33 -070036using org::chromium::flimflam::ManagerProxyInterface;
37using org::chromium::flimflam::ServiceProxyInterface;
Jay Srinivasan43488792012-06-19 00:25:31 -070038using std::set;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070039using std::string;
40
41namespace chromeos_update_engine {
42
Sen Jiangf5bebae2016-06-03 15:36:54 -070043namespace connection_manager {
44std::unique_ptr<ConnectionManagerInterface> CreateConnectionManager(
45 SystemState* system_state) {
46 return std::unique_ptr<ConnectionManagerInterface>(
47 new ConnectionManager(new ShillProxy(), system_state));
48}
Amin Hassani7cc8bb02019-01-14 16:29:47 -080049} // namespace connection_manager
Sen Jiangf5bebae2016-06-03 15:36:54 -070050
Alex Deymo30534502015-07-20 15:06:33 -070051ConnectionManager::ConnectionManager(ShillProxyInterface* shill_proxy,
52 SystemState* system_state)
53 : shill_proxy_(shill_proxy), system_state_(system_state) {}
Jay Srinivasan43488792012-06-19 00:25:31 -070054
Sen Jiang255e22b2016-05-20 16:15:29 -070055bool ConnectionManager::IsUpdateAllowedOver(
56 ConnectionType type, ConnectionTethering tethering) const {
Alex Khouderchahbfa82262019-08-13 15:00:34 -070057 if (type != ConnectionType::kCellular) {
58 if (tethering != ConnectionTethering::kConfirmed) {
Weidong Guo421ff332017-04-17 10:08:38 -070059 return true;
Jay Srinivasan43488792012-06-19 00:25:31 -070060 }
61
Alex Khouderchahbfa82262019-08-13 15:00:34 -070062 // Treat this connection as if it is a cellular connection.
63 LOG(INFO)
64 << "Current connection is confirmed tethered, using Cellular setting.";
Jay Srinivasan43488792012-06-19 00:25:31 -070065 }
Alex Khouderchahbfa82262019-08-13 15:00:34 -070066
67 const policy::DevicePolicy* device_policy = system_state_->device_policy();
68
69 // The device_policy is loaded in a lazy way before an update check. Load
70 // it now from the libbrillo cache if it wasn't already loaded.
71 if (!device_policy) {
72 UpdateAttempter* update_attempter = system_state_->update_attempter();
73 if (update_attempter) {
74 update_attempter->RefreshDevicePolicy();
75 device_policy = system_state_->device_policy();
76 }
77 }
78
79 if (!device_policy) {
80 // Device policy fails to be loaded (possibly due to guest account). We
81 // do not check the local user setting here, which should be checked by
82 // |OmahaRequestAction| during checking for update.
83 LOG(INFO) << "Allowing updates over cellular as device policy fails to be "
84 "loaded.";
85 return true;
86 }
87
88 set<string> allowed_types;
89 if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
90 // The update setting is enforced by the device policy.
91
92 if (!base::ContainsKey(allowed_types, shill::kTypeCellular)) {
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 Srinivasan43488792012-06-19 00:25:31 -0700108}
109
Weidong Guo421ff332017-04-17 10:08:38 -0700110bool ConnectionManager::IsAllowedConnectionTypesForUpdateSet() const {
111 const policy::DevicePolicy* device_policy = system_state_->device_policy();
112 if (!device_policy) {
113 LOG(INFO) << "There's no device policy loaded yet.";
114 return false;
115 }
116
117 set<string> allowed_types;
118 if (!device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
119 return false;
120 }
121
122 return true;
123}
124
Alex Deymo30534502015-07-20 15:06:33 -0700125bool ConnectionManager::GetConnectionProperties(
Sen Jiang255e22b2016-05-20 16:15:29 -0700126 ConnectionType* out_type, ConnectionTethering* out_tethering) {
Alex Deymo758dd532015-09-09 15:21:22 -0700127 dbus::ObjectPath default_service_path;
Alex Deymo30534502015-07-20 15:06:33 -0700128 TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path));
Alex Deymo758dd532015-09-09 15:21:22 -0700129 if (!default_service_path.IsValid())
Alex Deymo30534502015-07-20 15:06:33 -0700130 return false;
Alex Deymo1fbaac82015-11-04 04:41:40 -0800131 // Shill uses the "/" service path to indicate that it is not connected.
Colin Howesc9e98d62018-09-18 10:35:20 -0700132 if (default_service_path.value() == "/") {
133 *out_type = ConnectionType::kDisconnected;
134 *out_tethering = ConnectionTethering::kUnknown;
135 return true;
136 }
Alex Deymo30534502015-07-20 15:06:33 -0700137 TEST_AND_RETURN_FALSE(
138 GetServicePathProperties(default_service_path, out_type, out_tethering));
139 return true;
Alex Deymo6ae91202014-03-10 19:21:25 -0700140}
141
Alex Deymo758dd532015-09-09 15:21:22 -0700142bool ConnectionManager::GetDefaultServicePath(dbus::ObjectPath* out_path) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700143 brillo::VariantDictionary properties;
144 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700145 ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy();
146 if (!manager_proxy)
147 return false;
148 TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error));
149
150 const auto& prop_default_service =
151 properties.find(shill::kDefaultServiceProperty);
152 if (prop_default_service == properties.end())
153 return false;
154
Alex Deymo758dd532015-09-09 15:21:22 -0700155 *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>();
156 return out_path->IsValid();
Alex Deymo30534502015-07-20 15:06:33 -0700157}
158
159bool ConnectionManager::GetServicePathProperties(
Alex Deymo758dd532015-09-09 15:21:22 -0700160 const dbus::ObjectPath& path,
Sen Jiang255e22b2016-05-20 16:15:29 -0700161 ConnectionType* out_type,
162 ConnectionTethering* out_tethering) {
Alex Deymo30534502015-07-20 15:06:33 -0700163 // We create and dispose the ServiceProxyInterface on every request.
164 std::unique_ptr<ServiceProxyInterface> service =
165 shill_proxy_->GetServiceForPath(path);
166
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700167 brillo::VariantDictionary properties;
168 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700169 TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error));
170
171 // Populate the out_tethering.
172 const auto& prop_tethering = properties.find(shill::kTetheringProperty);
173 if (prop_tethering == properties.end()) {
174 // Set to Unknown if not present.
Sen Jiang255e22b2016-05-20 16:15:29 -0700175 *out_tethering = ConnectionTethering::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700176 } else {
177 // If the property doesn't contain a string value, the empty string will
178 // become kUnknown.
Sen Jiang255e22b2016-05-20 16:15:29 -0700179 *out_tethering = connection_utils::ParseConnectionTethering(
180 prop_tethering->second.TryGet<string>());
Alex Deymo30534502015-07-20 15:06:33 -0700181 }
182
183 // Populate the out_type property.
184 const auto& prop_type = properties.find(shill::kTypeProperty);
185 if (prop_type == properties.end()) {
186 // Set to Unknown if not present.
Sen Jiang255e22b2016-05-20 16:15:29 -0700187 *out_type = ConnectionType::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700188 return false;
189 }
190
191 string type_str = prop_type->second.TryGet<string>();
192 if (type_str == shill::kTypeVPN) {
193 const auto& prop_physical =
194 properties.find(shill::kPhysicalTechnologyProperty);
195 if (prop_physical == properties.end()) {
196 LOG(ERROR) << "No PhysicalTechnology property found for a VPN"
Alex Deymo758dd532015-09-09 15:21:22 -0700197 " connection (service: "
198 << path.value() << "). Returning default kUnknown value.";
Sen Jiang255e22b2016-05-20 16:15:29 -0700199 *out_type = ConnectionType::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700200 } else {
Sen Jiang255e22b2016-05-20 16:15:29 -0700201 *out_type = connection_utils::ParseConnectionType(
202 prop_physical->second.TryGet<string>());
Alex Deymo30534502015-07-20 15:06:33 -0700203 }
204 } else {
Sen Jiang255e22b2016-05-20 16:15:29 -0700205 *out_type = connection_utils::ParseConnectionType(type_str);
Alex Deymo30534502015-07-20 15:06:33 -0700206 }
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700207 return true;
208}
209
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700210} // namespace chromeos_update_engine