blob: d0d33920192c8cef3cbf65ddcccba78eaaa3a0ae [file] [log] [blame]
Jay Srinivasan43488792012-06-19 00:25:31 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Jay Srinivasan43488792012-06-19 00:25:31 -07005#include "update_engine/connection_manager.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07006
Alex Vakulenkod2779df2014-06-16 13:19:00 -07007#include <set>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07008#include <string>
9
Jay Srinivasan43488792012-06-19 00:25:31 -070010#include <base/stl_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070011#include <base/strings/string_util.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070012#include <chromeos/dbus/service_constants.h>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070013#include <glib.h>
Gilad Arnold1f847232014-04-07 12:07:49 -070014#include <policy/device_policy.h>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070015
Alex Deymof4867c42013-06-28 14:41:39 -070016#include "update_engine/prefs.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070017#include "update_engine/system_state.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070018#include "update_engine/utils.h"
19
Alex Deymo30534502015-07-20 15:06:33 -070020using org::chromium::flimflam::ManagerProxyInterface;
21using org::chromium::flimflam::ServiceProxyInterface;
Jay Srinivasan43488792012-06-19 00:25:31 -070022using std::set;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070023using std::string;
24
25namespace chromeos_update_engine {
26
27namespace {
28
Alex Deymo30534502015-07-20 15:06:33 -070029NetworkConnectionType ParseConnectionType(const string& type_str) {
30 if (type_str == shill::kTypeEthernet) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070031 return NetworkConnectionType::kEthernet;
Alex Deymo30534502015-07-20 15:06:33 -070032 } else if (type_str == shill::kTypeWifi) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070033 return NetworkConnectionType::kWifi;
Alex Deymo30534502015-07-20 15:06:33 -070034 } else if (type_str == shill::kTypeWimax) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070035 return NetworkConnectionType::kWimax;
Alex Deymo30534502015-07-20 15:06:33 -070036 } else if (type_str == shill::kTypeBluetooth) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070037 return NetworkConnectionType::kBluetooth;
Alex Deymo30534502015-07-20 15:06:33 -070038 } else if (type_str == shill::kTypeCellular) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070039 return NetworkConnectionType::kCellular;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070040 }
Alex Deymo75eac7e2015-07-29 13:39:14 -070041 return NetworkConnectionType::kUnknown;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070042}
43
Alex Deymo30534502015-07-20 15:06:33 -070044NetworkTethering ParseTethering(const string& tethering_str) {
45 if (tethering_str == shill::kTetheringNotDetectedState) {
Alex Deymo6ae91202014-03-10 19:21:25 -070046 return NetworkTethering::kNotDetected;
Alex Deymo30534502015-07-20 15:06:33 -070047 } else if (tethering_str == shill::kTetheringSuspectedState) {
Alex Deymo6ae91202014-03-10 19:21:25 -070048 return NetworkTethering::kSuspected;
Alex Deymo30534502015-07-20 15:06:33 -070049 } else if (tethering_str == shill::kTetheringConfirmedState) {
Alex Deymo6ae91202014-03-10 19:21:25 -070050 return NetworkTethering::kConfirmed;
51 }
52 LOG(WARNING) << "Unknown Tethering value: " << tethering_str;
53 return NetworkTethering::kUnknown;
54}
55
Alex Vakulenkod2779df2014-06-16 13:19:00 -070056} // namespace
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070057
Alex Deymo30534502015-07-20 15:06:33 -070058ConnectionManager::ConnectionManager(ShillProxyInterface* shill_proxy,
59 SystemState* system_state)
60 : shill_proxy_(shill_proxy), system_state_(system_state) {}
Jay Srinivasan43488792012-06-19 00:25:31 -070061
Alex Deymo6ae91202014-03-10 19:21:25 -070062bool ConnectionManager::IsUpdateAllowedOver(NetworkConnectionType type,
63 NetworkTethering tethering) const {
Jay Srinivasan43488792012-06-19 00:25:31 -070064 switch (type) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070065 case NetworkConnectionType::kBluetooth:
Jay Srinivasan43488792012-06-19 00:25:31 -070066 return false;
67
Alex Deymo75eac7e2015-07-29 13:39:14 -070068 case NetworkConnectionType::kCellular: {
Jay Srinivasan43488792012-06-19 00:25:31 -070069 set<string> allowed_types;
70 const policy::DevicePolicy* device_policy =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080071 system_state_->device_policy();
Alex Deymof4867c42013-06-28 14:41:39 -070072
73 // A device_policy is loaded in a lazy way right before an update check,
74 // so the device_policy should be already loaded at this point. If it's
75 // not, return a safe value for this setting.
Jay Srinivasan43488792012-06-19 00:25:31 -070076 if (!device_policy) {
Alex Deymof4867c42013-06-28 14:41:39 -070077 LOG(INFO) << "Disabling updates over cellular networks as there's no "
78 "device policy loaded yet.";
Jay Srinivasan43488792012-06-19 00:25:31 -070079 return false;
80 }
81
Alex Deymof4867c42013-06-28 14:41:39 -070082 if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
83 // The update setting is enforced by the device policy.
Jay Srinivasan43488792012-06-19 00:25:31 -070084
Gilad Arnold9a423ff2014-03-27 15:27:35 -070085 if (!ContainsKey(allowed_types, shill::kTypeCellular)) {
Alex Deymof4867c42013-06-28 14:41:39 -070086 LOG(INFO) << "Disabling updates over cellular connection as it's not "
87 "allowed in the device policy.";
88 return false;
89 }
Jay Srinivasan43488792012-06-19 00:25:31 -070090
Alex Deymof4867c42013-06-28 14:41:39 -070091 LOG(INFO) << "Allowing updates over cellular per device policy.";
92 return true;
93 } else {
94 // There's no update setting in the device policy, using the local user
95 // setting.
96 PrefsInterface* prefs = system_state_->prefs();
97
98 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
99 LOG(INFO) << "Disabling updates over cellular connection as there's "
100 "no device policy setting nor user preference present.";
101 return false;
102 }
103
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700104 bool stored_value;
105 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission,
106 &stored_value)) {
Alex Deymof4867c42013-06-28 14:41:39 -0700107 return false;
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700108 }
Alex Deymof4867c42013-06-28 14:41:39 -0700109
110 if (!stored_value) {
111 LOG(INFO) << "Disabling updates over cellular connection per user "
112 "setting.";
113 return false;
114 }
115 LOG(INFO) << "Allowing updates over cellular per user setting.";
116 return true;
117 }
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
Alex Deymof6ee0162015-07-31 12:35:22 -0700132// static
Jay Srinivasan43488792012-06-19 00:25:31 -0700133const char* ConnectionManager::StringForConnectionType(
Alex Deymof6ee0162015-07-31 12:35:22 -0700134 NetworkConnectionType type) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700135 switch (type) {
136 case NetworkConnectionType::kEthernet:
137 return shill::kTypeEthernet;
138 case NetworkConnectionType::kWifi:
139 return shill::kTypeWifi;
140 case NetworkConnectionType::kWimax:
141 return shill::kTypeWimax;
142 case NetworkConnectionType::kBluetooth:
143 return shill::kTypeBluetooth;
144 case NetworkConnectionType::kCellular:
145 return shill::kTypeCellular;
146 case NetworkConnectionType::kUnknown:
147 return "Unknown";
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700148 }
Alex Deymo75eac7e2015-07-29 13:39:14 -0700149 return "Unknown";
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700150}
151
Alex Deymo30534502015-07-20 15:06:33 -0700152bool ConnectionManager::GetConnectionProperties(
153 NetworkConnectionType* out_type,
154 NetworkTethering* out_tethering) {
155 string default_service_path;
156 TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path));
157 if (default_service_path.empty())
158 return false;
159 TEST_AND_RETURN_FALSE(
160 GetServicePathProperties(default_service_path, out_type, out_tethering));
161 return true;
Alex Deymo6ae91202014-03-10 19:21:25 -0700162}
163
Alex Deymo30534502015-07-20 15:06:33 -0700164bool ConnectionManager::GetDefaultServicePath(string* out_path) {
165 chromeos::VariantDictionary properties;
166 chromeos::ErrorPtr error;
167 ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy();
168 if (!manager_proxy)
169 return false;
170 TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error));
171
172 const auto& prop_default_service =
173 properties.find(shill::kDefaultServiceProperty);
174 if (prop_default_service == properties.end())
175 return false;
176
177 *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>().value();
178 return !out_path->empty();
179}
180
181bool ConnectionManager::GetServicePathProperties(
182 const string& path,
Alex Deymo6ae91202014-03-10 19:21:25 -0700183 NetworkConnectionType* out_type,
Alex Deymo30534502015-07-20 15:06:33 -0700184 NetworkTethering* out_tethering) {
185 // We create and dispose the ServiceProxyInterface on every request.
186 std::unique_ptr<ServiceProxyInterface> service =
187 shill_proxy_->GetServiceForPath(path);
188
189 chromeos::VariantDictionary properties;
190 chromeos::ErrorPtr error;
191 TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error));
192
193 // Populate the out_tethering.
194 const auto& prop_tethering = properties.find(shill::kTetheringProperty);
195 if (prop_tethering == properties.end()) {
196 // Set to Unknown if not present.
197 *out_tethering = NetworkTethering::kUnknown;
198 } else {
199 // If the property doesn't contain a string value, the empty string will
200 // become kUnknown.
201 *out_tethering = ParseTethering(prop_tethering->second.TryGet<string>());
202 }
203
204 // Populate the out_type property.
205 const auto& prop_type = properties.find(shill::kTypeProperty);
206 if (prop_type == properties.end()) {
207 // Set to Unknown if not present.
208 *out_type = NetworkConnectionType::kUnknown;
209 return false;
210 }
211
212 string type_str = prop_type->second.TryGet<string>();
213 if (type_str == shill::kTypeVPN) {
214 const auto& prop_physical =
215 properties.find(shill::kPhysicalTechnologyProperty);
216 if (prop_physical == properties.end()) {
217 LOG(ERROR) << "No PhysicalTechnology property found for a VPN"
218 << " connection (service: " << path << "). Returning default"
219 << " kUnknown value.";
220 *out_type = NetworkConnectionType::kUnknown;
221 } else {
222 *out_type = ParseConnectionType(prop_physical->second.TryGet<string>());
223 }
224 } else {
225 *out_type = ParseConnectionType(type_str);
226 }
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700227 return true;
228}
229
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700230} // namespace chromeos_update_engine