blob: 4063f249e6bbde9e5f9fede0bd6c48e959c9adce [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}
49}
50
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 {
Jay Srinivasan43488792012-06-19 00:25:31 -070057 switch (type) {
Sen Jiang255e22b2016-05-20 16:15:29 -070058 case ConnectionType::kBluetooth:
Jay Srinivasan43488792012-06-19 00:25:31 -070059 return false;
60
Sen Jiang255e22b2016-05-20 16:15:29 -070061 case ConnectionType::kCellular: {
Jay Srinivasan43488792012-06-19 00:25:31 -070062 set<string> allowed_types;
Weidong Guo421ff332017-04-17 10:08:38 -070063
Jay Srinivasan43488792012-06-19 00:25:31 -070064 const policy::DevicePolicy* device_policy =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080065 system_state_->device_policy();
Alex Deymof4867c42013-06-28 14:41:39 -070066
Weidong Guo421ff332017-04-17 10:08:38 -070067 // The device_policy is loaded in a lazy way before an update check. Load
68 // it now from the libbrillo cache if it wasn't already loaded.
Jay Srinivasan43488792012-06-19 00:25:31 -070069 if (!device_policy) {
Weidong Guo421ff332017-04-17 10:08:38 -070070 UpdateAttempter* update_attempter = system_state_->update_attempter();
71 if (update_attempter) {
72 update_attempter->RefreshDevicePolicy();
73 device_policy = system_state_->device_policy();
74 }
75 }
76
77 if (!device_policy) {
78 // Device policy fails to be loaded (possibly due to guest account). We
79 // do not check the local user setting here, which should be checked by
80 // |OmahaRequestAction| during checking for update.
81 LOG(INFO) << "Allowing updates over cellular as device policy "
82 "fails to be loaded.";
83 return true;
Jay Srinivasan43488792012-06-19 00:25:31 -070084 }
85
Alex Deymof4867c42013-06-28 14:41:39 -070086 if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
87 // The update setting is enforced by the device policy.
Jay Srinivasan43488792012-06-19 00:25:31 -070088
Hidehiko Abe2b9d2412017-12-13 18:56:18 +090089 if (!base::ContainsKey(allowed_types, shill::kTypeCellular)) {
Tao Bao5688d162017-06-06 13:09:06 -070090 LOG(INFO) << "Disabling updates over cellular connection as it's not "
91 "allowed in the device policy.";
Alex Deymof4867c42013-06-28 14:41:39 -070092 return false;
93 }
Jay Srinivasan43488792012-06-19 00:25:31 -070094
Alex Deymof4867c42013-06-28 14:41:39 -070095 LOG(INFO) << "Allowing updates over cellular per device policy.";
Tao Bao5688d162017-06-06 13:09:06 -070096 return true;
Tao Bao5688d162017-06-06 13:09:06 -070097 }
Weidong Guo421ff332017-04-17 10:08:38 -070098
99 // If there's no update setting in the device policy, we do not check
100 // the local user setting here, which should be checked by
101 // |OmahaRequestAction| during checking for update.
102 LOG(INFO) << "Allowing updates over cellular as device policy does "
103 "not include update setting.";
104 return true;
Jay Srinivasan43488792012-06-19 00:25:31 -0700105 }
106
107 default:
Sen Jiang255e22b2016-05-20 16:15:29 -0700108 if (tethering == ConnectionTethering::kConfirmed) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700109 // Treat this connection as if it is a cellular connection.
110 LOG(INFO) << "Current connection is confirmed tethered, using Cellular "
111 "setting.";
Sen Jiang255e22b2016-05-20 16:15:29 -0700112 return IsUpdateAllowedOver(ConnectionType::kCellular,
113 ConnectionTethering::kUnknown);
Alex Deymo6ae91202014-03-10 19:21:25 -0700114 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700115 return true;
116 }
117}
118
Weidong Guo421ff332017-04-17 10:08:38 -0700119bool ConnectionManager::IsAllowedConnectionTypesForUpdateSet() const {
120 const policy::DevicePolicy* device_policy = system_state_->device_policy();
121 if (!device_policy) {
122 LOG(INFO) << "There's no device policy loaded yet.";
123 return false;
124 }
125
126 set<string> allowed_types;
127 if (!device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
128 return false;
129 }
130
131 return true;
132}
133
Alex Deymo30534502015-07-20 15:06:33 -0700134bool ConnectionManager::GetConnectionProperties(
Sen Jiang255e22b2016-05-20 16:15:29 -0700135 ConnectionType* out_type, ConnectionTethering* out_tethering) {
Alex Deymo758dd532015-09-09 15:21:22 -0700136 dbus::ObjectPath default_service_path;
Alex Deymo30534502015-07-20 15:06:33 -0700137 TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path));
Alex Deymo758dd532015-09-09 15:21:22 -0700138 if (!default_service_path.IsValid())
Alex Deymo30534502015-07-20 15:06:33 -0700139 return false;
Alex Deymo1fbaac82015-11-04 04:41:40 -0800140 // Shill uses the "/" service path to indicate that it is not connected.
141 if (default_service_path.value() == "/")
142 return false;
Alex Deymo30534502015-07-20 15:06:33 -0700143 TEST_AND_RETURN_FALSE(
144 GetServicePathProperties(default_service_path, out_type, out_tethering));
145 return true;
Alex Deymo6ae91202014-03-10 19:21:25 -0700146}
147
Alex Deymo758dd532015-09-09 15:21:22 -0700148bool ConnectionManager::GetDefaultServicePath(dbus::ObjectPath* out_path) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700149 brillo::VariantDictionary properties;
150 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700151 ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy();
152 if (!manager_proxy)
153 return false;
154 TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error));
155
156 const auto& prop_default_service =
157 properties.find(shill::kDefaultServiceProperty);
158 if (prop_default_service == properties.end())
159 return false;
160
Alex Deymo758dd532015-09-09 15:21:22 -0700161 *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>();
162 return out_path->IsValid();
Alex Deymo30534502015-07-20 15:06:33 -0700163}
164
165bool ConnectionManager::GetServicePathProperties(
Alex Deymo758dd532015-09-09 15:21:22 -0700166 const dbus::ObjectPath& path,
Sen Jiang255e22b2016-05-20 16:15:29 -0700167 ConnectionType* out_type,
168 ConnectionTethering* out_tethering) {
Alex Deymo30534502015-07-20 15:06:33 -0700169 // We create and dispose the ServiceProxyInterface on every request.
170 std::unique_ptr<ServiceProxyInterface> service =
171 shill_proxy_->GetServiceForPath(path);
172
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700173 brillo::VariantDictionary properties;
174 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700175 TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error));
176
177 // Populate the out_tethering.
178 const auto& prop_tethering = properties.find(shill::kTetheringProperty);
179 if (prop_tethering == properties.end()) {
180 // Set to Unknown if not present.
Sen Jiang255e22b2016-05-20 16:15:29 -0700181 *out_tethering = ConnectionTethering::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700182 } else {
183 // If the property doesn't contain a string value, the empty string will
184 // become kUnknown.
Sen Jiang255e22b2016-05-20 16:15:29 -0700185 *out_tethering = connection_utils::ParseConnectionTethering(
186 prop_tethering->second.TryGet<string>());
Alex Deymo30534502015-07-20 15:06:33 -0700187 }
188
189 // Populate the out_type property.
190 const auto& prop_type = properties.find(shill::kTypeProperty);
191 if (prop_type == properties.end()) {
192 // Set to Unknown if not present.
Sen Jiang255e22b2016-05-20 16:15:29 -0700193 *out_type = ConnectionType::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700194 return false;
195 }
196
197 string type_str = prop_type->second.TryGet<string>();
198 if (type_str == shill::kTypeVPN) {
199 const auto& prop_physical =
200 properties.find(shill::kPhysicalTechnologyProperty);
201 if (prop_physical == properties.end()) {
202 LOG(ERROR) << "No PhysicalTechnology property found for a VPN"
Alex Deymo758dd532015-09-09 15:21:22 -0700203 " connection (service: "
204 << path.value() << "). Returning default kUnknown value.";
Sen Jiang255e22b2016-05-20 16:15:29 -0700205 *out_type = ConnectionType::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700206 } else {
Sen Jiang255e22b2016-05-20 16:15:29 -0700207 *out_type = connection_utils::ParseConnectionType(
208 prop_physical->second.TryGet<string>());
Alex Deymo30534502015-07-20 15:06:33 -0700209 }
210 } else {
Sen Jiang255e22b2016-05-20 16:15:29 -0700211 *out_type = connection_utils::ParseConnectionType(type_str);
Alex Deymo30534502015-07-20 15:06:33 -0700212 }
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700213 return true;
214}
215
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700216} // namespace chromeos_update_engine