blob: 47bd7762fbf7874e3b227bcbb0d3d877ac1f7e40 [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
Alex Vakulenkod2779df2014-06-16 13:19:00 -070019#include <set>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070020#include <string>
21
Jay Srinivasan43488792012-06-19 00:25:31 -070022#include <base/stl_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070023#include <base/strings/string_util.h>
Gilad Arnold1f847232014-04-07 12:07:49 -070024#include <policy/device_policy.h>
Alex Deymod6deb1d2015-08-28 15:54:37 -070025#include <shill/dbus-constants.h>
26#include <shill/dbus-proxies.h>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/prefs.h"
29#include "update_engine/common/utils.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070030#include "update_engine/system_state.h"
Weidong Guo70063d92017-04-17 10:08:38 -070031#include "update_engine/update_attempter.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070032
Alex Deymo30534502015-07-20 15:06:33 -070033using org::chromium::flimflam::ManagerProxyInterface;
34using org::chromium::flimflam::ServiceProxyInterface;
Jay Srinivasan43488792012-06-19 00:25:31 -070035using std::set;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070036using std::string;
37
38namespace chromeos_update_engine {
39
40namespace {
41
Alex Deymo30534502015-07-20 15:06:33 -070042NetworkConnectionType ParseConnectionType(const string& type_str) {
43 if (type_str == shill::kTypeEthernet) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070044 return NetworkConnectionType::kEthernet;
Alex Deymo30534502015-07-20 15:06:33 -070045 } else if (type_str == shill::kTypeWifi) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070046 return NetworkConnectionType::kWifi;
Alex Deymo30534502015-07-20 15:06:33 -070047 } else if (type_str == shill::kTypeWimax) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070048 return NetworkConnectionType::kWimax;
Alex Deymo30534502015-07-20 15:06:33 -070049 } else if (type_str == shill::kTypeBluetooth) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070050 return NetworkConnectionType::kBluetooth;
Alex Deymo30534502015-07-20 15:06:33 -070051 } else if (type_str == shill::kTypeCellular) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070052 return NetworkConnectionType::kCellular;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070053 }
Alex Deymo75eac7e2015-07-29 13:39:14 -070054 return NetworkConnectionType::kUnknown;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070055}
56
Alex Deymo30534502015-07-20 15:06:33 -070057NetworkTethering ParseTethering(const string& tethering_str) {
58 if (tethering_str == shill::kTetheringNotDetectedState) {
Alex Deymo6ae91202014-03-10 19:21:25 -070059 return NetworkTethering::kNotDetected;
Alex Deymo30534502015-07-20 15:06:33 -070060 } else if (tethering_str == shill::kTetheringSuspectedState) {
Alex Deymo6ae91202014-03-10 19:21:25 -070061 return NetworkTethering::kSuspected;
Alex Deymo30534502015-07-20 15:06:33 -070062 } else if (tethering_str == shill::kTetheringConfirmedState) {
Alex Deymo6ae91202014-03-10 19:21:25 -070063 return NetworkTethering::kConfirmed;
64 }
65 LOG(WARNING) << "Unknown Tethering value: " << tethering_str;
66 return NetworkTethering::kUnknown;
67}
68
Alex Vakulenkod2779df2014-06-16 13:19:00 -070069} // namespace
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070070
Alex Deymo30534502015-07-20 15:06:33 -070071ConnectionManager::ConnectionManager(ShillProxyInterface* shill_proxy,
72 SystemState* system_state)
73 : shill_proxy_(shill_proxy), system_state_(system_state) {}
Jay Srinivasan43488792012-06-19 00:25:31 -070074
Alex Deymo6ae91202014-03-10 19:21:25 -070075bool ConnectionManager::IsUpdateAllowedOver(NetworkConnectionType type,
76 NetworkTethering tethering) const {
Jay Srinivasan43488792012-06-19 00:25:31 -070077 switch (type) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070078 case NetworkConnectionType::kBluetooth:
Jay Srinivasan43488792012-06-19 00:25:31 -070079 return false;
80
Alex Deymo75eac7e2015-07-29 13:39:14 -070081 case NetworkConnectionType::kCellular: {
Jay Srinivasan43488792012-06-19 00:25:31 -070082 set<string> allowed_types;
Weidong Guo70063d92017-04-17 10:08:38 -070083
Jay Srinivasan43488792012-06-19 00:25:31 -070084 const policy::DevicePolicy* device_policy =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080085 system_state_->device_policy();
Alex Deymof4867c42013-06-28 14:41:39 -070086
Weidong Guo70063d92017-04-17 10:08:38 -070087 // The device_policy is loaded in a lazy way before an update check. Load
88 // it now from the libbrillo cache if it wasn't already loaded.
Jay Srinivasan43488792012-06-19 00:25:31 -070089 if (!device_policy) {
Weidong Guo70063d92017-04-17 10:08:38 -070090 UpdateAttempter* update_attempter = system_state_->update_attempter();
91 if (update_attempter) {
92 update_attempter->RefreshDevicePolicy();
93 device_policy = system_state_->device_policy();
94 }
95 }
96
97 if (!device_policy) {
98 LOG(INFO) << "Disabling updates over cellular as device policy "
99 "fails to be loaded.";
Jay Srinivasan43488792012-06-19 00:25:31 -0700100 return false;
101 }
102
Alex Deymof4867c42013-06-28 14:41:39 -0700103 if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
104 // The update setting is enforced by the device policy.
Jay Srinivasan43488792012-06-19 00:25:31 -0700105
Gilad Arnold9a423ff2014-03-27 15:27:35 -0700106 if (!ContainsKey(allowed_types, shill::kTypeCellular)) {
Weidong Guo70063d92017-04-17 10:08:38 -0700107 LOG(INFO) << "Disabling updates over cellular per device policy.";
Alex Deymof4867c42013-06-28 14:41:39 -0700108 return false;
109 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700110
Alex Deymof4867c42013-06-28 14:41:39 -0700111 LOG(INFO) << "Allowing updates over cellular per device policy.";
Alex Deymof4867c42013-06-28 14:41:39 -0700112 }
Weidong Guo70063d92017-04-17 10:08:38 -0700113
114 // If there's no update setting in the device policy, we do not check
115 // the local user setting here, which should be checked by
116 // |OmahaRequestAction| during checking for update.
117 return true;
Jay Srinivasan43488792012-06-19 00:25:31 -0700118 }
119
120 default:
Alex Deymo6ae91202014-03-10 19:21:25 -0700121 if (tethering == NetworkTethering::kConfirmed) {
122 // Treat this connection as if it is a cellular connection.
123 LOG(INFO) << "Current connection is confirmed tethered, using Cellular "
124 "setting.";
Alex Deymo75eac7e2015-07-29 13:39:14 -0700125 return IsUpdateAllowedOver(NetworkConnectionType::kCellular,
126 NetworkTethering::kUnknown);
Alex Deymo6ae91202014-03-10 19:21:25 -0700127 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700128 return true;
129 }
130}
131
Weidong Guo70063d92017-04-17 10:08:38 -0700132bool ConnectionManager::IsAllowedConnectionTypesForUpdateSet() const {
133 const policy::DevicePolicy* device_policy = system_state_->device_policy();
134 if (!device_policy) {
135 LOG(INFO) << "There's no device policy loaded yet.";
136 return false;
137 }
138
139 set<string> allowed_types;
140 if (!device_policy->GetAllowedConnectionTypesForUpdate(
141 &allowed_types)) {
142 return false;
143 }
144
145 return true;
146}
147
Alex Deymof6ee0162015-07-31 12:35:22 -0700148// static
Jay Srinivasan43488792012-06-19 00:25:31 -0700149const char* ConnectionManager::StringForConnectionType(
Alex Deymof6ee0162015-07-31 12:35:22 -0700150 NetworkConnectionType type) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700151 switch (type) {
152 case NetworkConnectionType::kEthernet:
153 return shill::kTypeEthernet;
154 case NetworkConnectionType::kWifi:
155 return shill::kTypeWifi;
156 case NetworkConnectionType::kWimax:
157 return shill::kTypeWimax;
158 case NetworkConnectionType::kBluetooth:
159 return shill::kTypeBluetooth;
160 case NetworkConnectionType::kCellular:
161 return shill::kTypeCellular;
162 case NetworkConnectionType::kUnknown:
163 return "Unknown";
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700164 }
Alex Deymo75eac7e2015-07-29 13:39:14 -0700165 return "Unknown";
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700166}
167
Alex Deymo30534502015-07-20 15:06:33 -0700168bool ConnectionManager::GetConnectionProperties(
169 NetworkConnectionType* out_type,
170 NetworkTethering* out_tethering) {
Alex Deymo758dd532015-09-09 15:21:22 -0700171 dbus::ObjectPath default_service_path;
Alex Deymo30534502015-07-20 15:06:33 -0700172 TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path));
Alex Deymo758dd532015-09-09 15:21:22 -0700173 if (!default_service_path.IsValid())
Alex Deymo30534502015-07-20 15:06:33 -0700174 return false;
Alex Deymo1fbaac82015-11-04 04:41:40 -0800175 // Shill uses the "/" service path to indicate that it is not connected.
176 if (default_service_path.value() == "/")
177 return false;
Alex Deymo30534502015-07-20 15:06:33 -0700178 TEST_AND_RETURN_FALSE(
179 GetServicePathProperties(default_service_path, out_type, out_tethering));
180 return true;
Alex Deymo6ae91202014-03-10 19:21:25 -0700181}
182
Alex Deymo758dd532015-09-09 15:21:22 -0700183bool ConnectionManager::GetDefaultServicePath(dbus::ObjectPath* out_path) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700184 brillo::VariantDictionary properties;
185 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700186 ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy();
187 if (!manager_proxy)
188 return false;
189 TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error));
190
191 const auto& prop_default_service =
192 properties.find(shill::kDefaultServiceProperty);
193 if (prop_default_service == properties.end())
194 return false;
195
Alex Deymo758dd532015-09-09 15:21:22 -0700196 *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>();
197 return out_path->IsValid();
Alex Deymo30534502015-07-20 15:06:33 -0700198}
199
200bool ConnectionManager::GetServicePathProperties(
Alex Deymo758dd532015-09-09 15:21:22 -0700201 const dbus::ObjectPath& path,
Alex Deymo6ae91202014-03-10 19:21:25 -0700202 NetworkConnectionType* out_type,
Alex Deymo30534502015-07-20 15:06:33 -0700203 NetworkTethering* out_tethering) {
204 // We create and dispose the ServiceProxyInterface on every request.
205 std::unique_ptr<ServiceProxyInterface> service =
206 shill_proxy_->GetServiceForPath(path);
207
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700208 brillo::VariantDictionary properties;
209 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700210 TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error));
211
212 // Populate the out_tethering.
213 const auto& prop_tethering = properties.find(shill::kTetheringProperty);
214 if (prop_tethering == properties.end()) {
215 // Set to Unknown if not present.
216 *out_tethering = NetworkTethering::kUnknown;
217 } else {
218 // If the property doesn't contain a string value, the empty string will
219 // become kUnknown.
220 *out_tethering = ParseTethering(prop_tethering->second.TryGet<string>());
221 }
222
223 // Populate the out_type property.
224 const auto& prop_type = properties.find(shill::kTypeProperty);
225 if (prop_type == properties.end()) {
226 // Set to Unknown if not present.
227 *out_type = NetworkConnectionType::kUnknown;
228 return false;
229 }
230
231 string type_str = prop_type->second.TryGet<string>();
232 if (type_str == shill::kTypeVPN) {
233 const auto& prop_physical =
234 properties.find(shill::kPhysicalTechnologyProperty);
235 if (prop_physical == properties.end()) {
236 LOG(ERROR) << "No PhysicalTechnology property found for a VPN"
Alex Deymo758dd532015-09-09 15:21:22 -0700237 " connection (service: "
238 << path.value() << "). Returning default kUnknown value.";
Alex Deymo30534502015-07-20 15:06:33 -0700239 *out_type = NetworkConnectionType::kUnknown;
240 } else {
241 *out_type = ParseConnectionType(prop_physical->second.TryGet<string>());
242 }
243 } else {
244 *out_type = ParseConnectionType(type_str);
245 }
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700246 return true;
247}
248
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700249} // namespace chromeos_update_engine