blob: be707bc4a2d3c7fa7406de49b3ef36cb624d253c [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"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070034
Alex Deymo30534502015-07-20 15:06:33 -070035using org::chromium::flimflam::ManagerProxyInterface;
36using org::chromium::flimflam::ServiceProxyInterface;
Jay Srinivasan43488792012-06-19 00:25:31 -070037using std::set;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070038using std::string;
39
40namespace chromeos_update_engine {
41
Sen Jiangf5bebae2016-06-03 15:36:54 -070042namespace connection_manager {
43std::unique_ptr<ConnectionManagerInterface> CreateConnectionManager(
44 SystemState* system_state) {
45 return std::unique_ptr<ConnectionManagerInterface>(
46 new ConnectionManager(new ShillProxy(), system_state));
47}
48}
49
Alex Deymo30534502015-07-20 15:06:33 -070050ConnectionManager::ConnectionManager(ShillProxyInterface* shill_proxy,
51 SystemState* system_state)
52 : shill_proxy_(shill_proxy), system_state_(system_state) {}
Jay Srinivasan43488792012-06-19 00:25:31 -070053
Sen Jiang255e22b2016-05-20 16:15:29 -070054bool ConnectionManager::IsUpdateAllowedOver(
55 ConnectionType type, ConnectionTethering tethering) const {
Jay Srinivasan43488792012-06-19 00:25:31 -070056 switch (type) {
Sen Jiang255e22b2016-05-20 16:15:29 -070057 case ConnectionType::kBluetooth:
Jay Srinivasan43488792012-06-19 00:25:31 -070058 return false;
59
Sen Jiang255e22b2016-05-20 16:15:29 -070060 case ConnectionType::kCellular: {
Jay Srinivasan43488792012-06-19 00:25:31 -070061 set<string> allowed_types;
62 const policy::DevicePolicy* device_policy =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080063 system_state_->device_policy();
Alex Deymof4867c42013-06-28 14:41:39 -070064
Tao Bao5688d162017-06-06 13:09:06 -070065 // A device_policy is loaded in a lazy way right before an update check,
66 // so the device_policy should be already loaded at this point. If it's
67 // not, return a safe value for this setting.
Jay Srinivasan43488792012-06-19 00:25:31 -070068 if (!device_policy) {
Tao Bao5688d162017-06-06 13:09:06 -070069 LOG(INFO) << "Disabling updates over cellular networks as there's no "
70 "device policy loaded yet.";
Jay Srinivasan43488792012-06-19 00:25:31 -070071 return false;
72 }
73
Alex Deymof4867c42013-06-28 14:41:39 -070074 if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
75 // The update setting is enforced by the device policy.
Jay Srinivasan43488792012-06-19 00:25:31 -070076
Hidehiko Abe2b9d2412017-12-13 18:56:18 +090077 if (!base::ContainsKey(allowed_types, shill::kTypeCellular)) {
Tao Bao5688d162017-06-06 13:09:06 -070078 LOG(INFO) << "Disabling updates over cellular connection as it's not "
79 "allowed in the device policy.";
Alex Deymof4867c42013-06-28 14:41:39 -070080 return false;
81 }
Jay Srinivasan43488792012-06-19 00:25:31 -070082
Alex Deymof4867c42013-06-28 14:41:39 -070083 LOG(INFO) << "Allowing updates over cellular per device policy.";
Tao Bao5688d162017-06-06 13:09:06 -070084 return true;
85 } else {
86 // There's no update setting in the device policy, using the local user
87 // setting.
88 PrefsInterface* prefs = system_state_->prefs();
Weidong Guo4b0d6032017-04-17 10:08:38 -070089
Tao Bao5688d162017-06-06 13:09:06 -070090 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
91 LOG(INFO) << "Disabling updates over cellular connection as there's "
92 "no device policy setting nor user preference present.";
93 return false;
94 }
95
96 bool stored_value;
97 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission,
98 &stored_value)) {
99 return false;
100 }
101
102 if (!stored_value) {
103 LOG(INFO) << "Disabling updates over cellular connection per user "
104 "setting.";
105 return false;
106 }
107 LOG(INFO) << "Allowing updates over cellular per user setting.";
108 return true;
109 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700110 }
111
112 default:
Sen Jiang255e22b2016-05-20 16:15:29 -0700113 if (tethering == ConnectionTethering::kConfirmed) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700114 // Treat this connection as if it is a cellular connection.
115 LOG(INFO) << "Current connection is confirmed tethered, using Cellular "
116 "setting.";
Sen Jiang255e22b2016-05-20 16:15:29 -0700117 return IsUpdateAllowedOver(ConnectionType::kCellular,
118 ConnectionTethering::kUnknown);
Alex Deymo6ae91202014-03-10 19:21:25 -0700119 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700120 return true;
121 }
122}
123
Alex Deymo30534502015-07-20 15:06:33 -0700124bool ConnectionManager::GetConnectionProperties(
Sen Jiang255e22b2016-05-20 16:15:29 -0700125 ConnectionType* out_type, ConnectionTethering* out_tethering) {
Alex Deymo758dd532015-09-09 15:21:22 -0700126 dbus::ObjectPath default_service_path;
Alex Deymo30534502015-07-20 15:06:33 -0700127 TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path));
Alex Deymo758dd532015-09-09 15:21:22 -0700128 if (!default_service_path.IsValid())
Alex Deymo30534502015-07-20 15:06:33 -0700129 return false;
Alex Deymo1fbaac82015-11-04 04:41:40 -0800130 // Shill uses the "/" service path to indicate that it is not connected.
131 if (default_service_path.value() == "/")
132 return false;
Alex Deymo30534502015-07-20 15:06:33 -0700133 TEST_AND_RETURN_FALSE(
134 GetServicePathProperties(default_service_path, out_type, out_tethering));
135 return true;
Alex Deymo6ae91202014-03-10 19:21:25 -0700136}
137
Alex Deymo758dd532015-09-09 15:21:22 -0700138bool ConnectionManager::GetDefaultServicePath(dbus::ObjectPath* out_path) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700139 brillo::VariantDictionary properties;
140 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700141 ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy();
142 if (!manager_proxy)
143 return false;
144 TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error));
145
146 const auto& prop_default_service =
147 properties.find(shill::kDefaultServiceProperty);
148 if (prop_default_service == properties.end())
149 return false;
150
Alex Deymo758dd532015-09-09 15:21:22 -0700151 *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>();
152 return out_path->IsValid();
Alex Deymo30534502015-07-20 15:06:33 -0700153}
154
155bool ConnectionManager::GetServicePathProperties(
Alex Deymo758dd532015-09-09 15:21:22 -0700156 const dbus::ObjectPath& path,
Sen Jiang255e22b2016-05-20 16:15:29 -0700157 ConnectionType* out_type,
158 ConnectionTethering* out_tethering) {
Alex Deymo30534502015-07-20 15:06:33 -0700159 // We create and dispose the ServiceProxyInterface on every request.
160 std::unique_ptr<ServiceProxyInterface> service =
161 shill_proxy_->GetServiceForPath(path);
162
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700163 brillo::VariantDictionary properties;
164 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700165 TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error));
166
167 // Populate the out_tethering.
168 const auto& prop_tethering = properties.find(shill::kTetheringProperty);
169 if (prop_tethering == properties.end()) {
170 // Set to Unknown if not present.
Sen Jiang255e22b2016-05-20 16:15:29 -0700171 *out_tethering = ConnectionTethering::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700172 } else {
173 // If the property doesn't contain a string value, the empty string will
174 // become kUnknown.
Sen Jiang255e22b2016-05-20 16:15:29 -0700175 *out_tethering = connection_utils::ParseConnectionTethering(
176 prop_tethering->second.TryGet<string>());
Alex Deymo30534502015-07-20 15:06:33 -0700177 }
178
179 // Populate the out_type property.
180 const auto& prop_type = properties.find(shill::kTypeProperty);
181 if (prop_type == properties.end()) {
182 // Set to Unknown if not present.
Sen Jiang255e22b2016-05-20 16:15:29 -0700183 *out_type = ConnectionType::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700184 return false;
185 }
186
187 string type_str = prop_type->second.TryGet<string>();
188 if (type_str == shill::kTypeVPN) {
189 const auto& prop_physical =
190 properties.find(shill::kPhysicalTechnologyProperty);
191 if (prop_physical == properties.end()) {
192 LOG(ERROR) << "No PhysicalTechnology property found for a VPN"
Alex Deymo758dd532015-09-09 15:21:22 -0700193 " connection (service: "
194 << path.value() << "). Returning default kUnknown value.";
Sen Jiang255e22b2016-05-20 16:15:29 -0700195 *out_type = ConnectionType::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700196 } else {
Sen Jiang255e22b2016-05-20 16:15:29 -0700197 *out_type = connection_utils::ParseConnectionType(
198 prop_physical->second.TryGet<string>());
Alex Deymo30534502015-07-20 15:06:33 -0700199 }
200 } else {
Sen Jiang255e22b2016-05-20 16:15:29 -0700201 *out_type = connection_utils::ParseConnectionType(type_str);
Alex Deymo30534502015-07-20 15:06:33 -0700202 }
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700203 return true;
204}
205
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700206} // namespace chromeos_update_engine