blob: 778cba56f0ed8af9530892101114f6f8cb3dc233 [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"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070031
Alex Deymo30534502015-07-20 15:06:33 -070032using org::chromium::flimflam::ManagerProxyInterface;
33using org::chromium::flimflam::ServiceProxyInterface;
Jay Srinivasan43488792012-06-19 00:25:31 -070034using std::set;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070035using std::string;
36
37namespace chromeos_update_engine {
38
39namespace {
40
Alex Deymo30534502015-07-20 15:06:33 -070041NetworkConnectionType ParseConnectionType(const string& type_str) {
42 if (type_str == shill::kTypeEthernet) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070043 return NetworkConnectionType::kEthernet;
Alex Deymo30534502015-07-20 15:06:33 -070044 } else if (type_str == shill::kTypeWifi) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070045 return NetworkConnectionType::kWifi;
Alex Deymo30534502015-07-20 15:06:33 -070046 } else if (type_str == shill::kTypeWimax) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070047 return NetworkConnectionType::kWimax;
Alex Deymo30534502015-07-20 15:06:33 -070048 } else if (type_str == shill::kTypeBluetooth) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070049 return NetworkConnectionType::kBluetooth;
Alex Deymo30534502015-07-20 15:06:33 -070050 } else if (type_str == shill::kTypeCellular) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070051 return NetworkConnectionType::kCellular;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070052 }
Alex Deymo75eac7e2015-07-29 13:39:14 -070053 return NetworkConnectionType::kUnknown;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070054}
55
Alex Deymo30534502015-07-20 15:06:33 -070056NetworkTethering ParseTethering(const string& tethering_str) {
57 if (tethering_str == shill::kTetheringNotDetectedState) {
Alex Deymo6ae91202014-03-10 19:21:25 -070058 return NetworkTethering::kNotDetected;
Alex Deymo30534502015-07-20 15:06:33 -070059 } else if (tethering_str == shill::kTetheringSuspectedState) {
Alex Deymo6ae91202014-03-10 19:21:25 -070060 return NetworkTethering::kSuspected;
Alex Deymo30534502015-07-20 15:06:33 -070061 } else if (tethering_str == shill::kTetheringConfirmedState) {
Alex Deymo6ae91202014-03-10 19:21:25 -070062 return NetworkTethering::kConfirmed;
63 }
64 LOG(WARNING) << "Unknown Tethering value: " << tethering_str;
65 return NetworkTethering::kUnknown;
66}
67
Alex Vakulenkod2779df2014-06-16 13:19:00 -070068} // namespace
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070069
Alex Deymo30534502015-07-20 15:06:33 -070070ConnectionManager::ConnectionManager(ShillProxyInterface* shill_proxy,
71 SystemState* system_state)
72 : shill_proxy_(shill_proxy), system_state_(system_state) {}
Jay Srinivasan43488792012-06-19 00:25:31 -070073
Alex Deymo6ae91202014-03-10 19:21:25 -070074bool ConnectionManager::IsUpdateAllowedOver(NetworkConnectionType type,
75 NetworkTethering tethering) const {
Jay Srinivasan43488792012-06-19 00:25:31 -070076 switch (type) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070077 case NetworkConnectionType::kBluetooth:
Jay Srinivasan43488792012-06-19 00:25:31 -070078 return false;
79
Alex Deymo75eac7e2015-07-29 13:39:14 -070080 case NetworkConnectionType::kCellular: {
Jay Srinivasan43488792012-06-19 00:25:31 -070081 set<string> allowed_types;
82 const policy::DevicePolicy* device_policy =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080083 system_state_->device_policy();
Alex Deymof4867c42013-06-28 14:41:39 -070084
85 // A device_policy is loaded in a lazy way right before an update check,
86 // so the device_policy should be already loaded at this point. If it's
87 // not, return a safe value for this setting.
Jay Srinivasan43488792012-06-19 00:25:31 -070088 if (!device_policy) {
Alex Deymof4867c42013-06-28 14:41:39 -070089 LOG(INFO) << "Disabling updates over cellular networks as there's no "
90 "device policy loaded yet.";
Jay Srinivasan43488792012-06-19 00:25:31 -070091 return false;
92 }
93
Alex Deymof4867c42013-06-28 14:41:39 -070094 if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
95 // The update setting is enforced by the device policy.
Jay Srinivasan43488792012-06-19 00:25:31 -070096
Gilad Arnold9a423ff2014-03-27 15:27:35 -070097 if (!ContainsKey(allowed_types, shill::kTypeCellular)) {
Alex Deymof4867c42013-06-28 14:41:39 -070098 LOG(INFO) << "Disabling updates over cellular connection as it's not "
99 "allowed in the device policy.";
100 return false;
101 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700102
Alex Deymof4867c42013-06-28 14:41:39 -0700103 LOG(INFO) << "Allowing updates over cellular per device policy.";
104 return true;
105 } else {
106 // There's no update setting in the device policy, using the local user
107 // setting.
108 PrefsInterface* prefs = system_state_->prefs();
109
110 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
111 LOG(INFO) << "Disabling updates over cellular connection as there's "
112 "no device policy setting nor user preference present.";
113 return false;
114 }
115
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700116 bool stored_value;
117 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission,
118 &stored_value)) {
Alex Deymof4867c42013-06-28 14:41:39 -0700119 return false;
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700120 }
Alex Deymof4867c42013-06-28 14:41:39 -0700121
122 if (!stored_value) {
123 LOG(INFO) << "Disabling updates over cellular connection per user "
124 "setting.";
125 return false;
126 }
127 LOG(INFO) << "Allowing updates over cellular per user setting.";
128 return true;
129 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700130 }
131
132 default:
Alex Deymo6ae91202014-03-10 19:21:25 -0700133 if (tethering == NetworkTethering::kConfirmed) {
134 // Treat this connection as if it is a cellular connection.
135 LOG(INFO) << "Current connection is confirmed tethered, using Cellular "
136 "setting.";
Alex Deymo75eac7e2015-07-29 13:39:14 -0700137 return IsUpdateAllowedOver(NetworkConnectionType::kCellular,
138 NetworkTethering::kUnknown);
Alex Deymo6ae91202014-03-10 19:21:25 -0700139 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700140 return true;
141 }
142}
143
Alex Deymof6ee0162015-07-31 12:35:22 -0700144// static
Jay Srinivasan43488792012-06-19 00:25:31 -0700145const char* ConnectionManager::StringForConnectionType(
Alex Deymof6ee0162015-07-31 12:35:22 -0700146 NetworkConnectionType type) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700147 switch (type) {
148 case NetworkConnectionType::kEthernet:
149 return shill::kTypeEthernet;
150 case NetworkConnectionType::kWifi:
151 return shill::kTypeWifi;
152 case NetworkConnectionType::kWimax:
153 return shill::kTypeWimax;
154 case NetworkConnectionType::kBluetooth:
155 return shill::kTypeBluetooth;
156 case NetworkConnectionType::kCellular:
157 return shill::kTypeCellular;
158 case NetworkConnectionType::kUnknown:
159 return "Unknown";
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700160 }
Alex Deymo75eac7e2015-07-29 13:39:14 -0700161 return "Unknown";
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700162}
163
Alex Deymo30534502015-07-20 15:06:33 -0700164bool ConnectionManager::GetConnectionProperties(
165 NetworkConnectionType* out_type,
166 NetworkTethering* out_tethering) {
Alex Deymo758dd532015-09-09 15:21:22 -0700167 dbus::ObjectPath default_service_path;
Alex Deymo30534502015-07-20 15:06:33 -0700168 TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path));
Alex Deymo758dd532015-09-09 15:21:22 -0700169 if (!default_service_path.IsValid())
Alex Deymo30534502015-07-20 15:06:33 -0700170 return false;
Alex Deymo1fbaac82015-11-04 04:41:40 -0800171 // Shill uses the "/" service path to indicate that it is not connected.
172 if (default_service_path.value() == "/")
173 return false;
Alex Deymo30534502015-07-20 15:06:33 -0700174 TEST_AND_RETURN_FALSE(
175 GetServicePathProperties(default_service_path, out_type, out_tethering));
176 return true;
Alex Deymo6ae91202014-03-10 19:21:25 -0700177}
178
Alex Deymo758dd532015-09-09 15:21:22 -0700179bool ConnectionManager::GetDefaultServicePath(dbus::ObjectPath* out_path) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700180 brillo::VariantDictionary properties;
181 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700182 ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy();
183 if (!manager_proxy)
184 return false;
185 TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error));
186
187 const auto& prop_default_service =
188 properties.find(shill::kDefaultServiceProperty);
189 if (prop_default_service == properties.end())
190 return false;
191
Alex Deymo758dd532015-09-09 15:21:22 -0700192 *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>();
193 return out_path->IsValid();
Alex Deymo30534502015-07-20 15:06:33 -0700194}
195
196bool ConnectionManager::GetServicePathProperties(
Alex Deymo758dd532015-09-09 15:21:22 -0700197 const dbus::ObjectPath& path,
Alex Deymo6ae91202014-03-10 19:21:25 -0700198 NetworkConnectionType* out_type,
Alex Deymo30534502015-07-20 15:06:33 -0700199 NetworkTethering* out_tethering) {
200 // We create and dispose the ServiceProxyInterface on every request.
201 std::unique_ptr<ServiceProxyInterface> service =
202 shill_proxy_->GetServiceForPath(path);
203
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700204 brillo::VariantDictionary properties;
205 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700206 TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error));
207
208 // Populate the out_tethering.
209 const auto& prop_tethering = properties.find(shill::kTetheringProperty);
210 if (prop_tethering == properties.end()) {
211 // Set to Unknown if not present.
212 *out_tethering = NetworkTethering::kUnknown;
213 } else {
214 // If the property doesn't contain a string value, the empty string will
215 // become kUnknown.
216 *out_tethering = ParseTethering(prop_tethering->second.TryGet<string>());
217 }
218
219 // Populate the out_type property.
220 const auto& prop_type = properties.find(shill::kTypeProperty);
221 if (prop_type == properties.end()) {
222 // Set to Unknown if not present.
223 *out_type = NetworkConnectionType::kUnknown;
224 return false;
225 }
226
227 string type_str = prop_type->second.TryGet<string>();
228 if (type_str == shill::kTypeVPN) {
229 const auto& prop_physical =
230 properties.find(shill::kPhysicalTechnologyProperty);
231 if (prop_physical == properties.end()) {
232 LOG(ERROR) << "No PhysicalTechnology property found for a VPN"
Alex Deymo758dd532015-09-09 15:21:22 -0700233 " connection (service: "
234 << path.value() << "). Returning default kUnknown value.";
Alex Deymo30534502015-07-20 15:06:33 -0700235 *out_type = NetworkConnectionType::kUnknown;
236 } else {
237 *out_type = ParseConnectionType(prop_physical->second.TryGet<string>());
238 }
239 } else {
240 *out_type = ParseConnectionType(type_str);
241 }
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700242 return true;
243}
244
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700245} // namespace chromeos_update_engine