blob: 6d9799df6d795fe6fe375dda5ee957e0c3dbc66f [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>
Jay Srinivasan43488792012-06-19 00:25:31 -070024#include <chromeos/dbus/service_constants.h>
Gilad Arnold1f847232014-04-07 12:07:49 -070025#include <policy/device_policy.h>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070026
Alex Deymof4867c42013-06-28 14:41:39 -070027#include "update_engine/prefs.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070028#include "update_engine/system_state.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070029#include "update_engine/utils.h"
30
Alex Deymo30534502015-07-20 15:06:33 -070031using org::chromium::flimflam::ManagerProxyInterface;
32using org::chromium::flimflam::ServiceProxyInterface;
Jay Srinivasan43488792012-06-19 00:25:31 -070033using std::set;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070034using std::string;
35
36namespace chromeos_update_engine {
37
38namespace {
39
Alex Deymo30534502015-07-20 15:06:33 -070040NetworkConnectionType ParseConnectionType(const string& type_str) {
41 if (type_str == shill::kTypeEthernet) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070042 return NetworkConnectionType::kEthernet;
Alex Deymo30534502015-07-20 15:06:33 -070043 } else if (type_str == shill::kTypeWifi) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070044 return NetworkConnectionType::kWifi;
Alex Deymo30534502015-07-20 15:06:33 -070045 } else if (type_str == shill::kTypeWimax) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070046 return NetworkConnectionType::kWimax;
Alex Deymo30534502015-07-20 15:06:33 -070047 } else if (type_str == shill::kTypeBluetooth) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070048 return NetworkConnectionType::kBluetooth;
Alex Deymo30534502015-07-20 15:06:33 -070049 } else if (type_str == shill::kTypeCellular) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070050 return NetworkConnectionType::kCellular;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070051 }
Alex Deymo75eac7e2015-07-29 13:39:14 -070052 return NetworkConnectionType::kUnknown;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070053}
54
Alex Deymo30534502015-07-20 15:06:33 -070055NetworkTethering ParseTethering(const string& tethering_str) {
56 if (tethering_str == shill::kTetheringNotDetectedState) {
Alex Deymo6ae91202014-03-10 19:21:25 -070057 return NetworkTethering::kNotDetected;
Alex Deymo30534502015-07-20 15:06:33 -070058 } else if (tethering_str == shill::kTetheringSuspectedState) {
Alex Deymo6ae91202014-03-10 19:21:25 -070059 return NetworkTethering::kSuspected;
Alex Deymo30534502015-07-20 15:06:33 -070060 } else if (tethering_str == shill::kTetheringConfirmedState) {
Alex Deymo6ae91202014-03-10 19:21:25 -070061 return NetworkTethering::kConfirmed;
62 }
63 LOG(WARNING) << "Unknown Tethering value: " << tethering_str;
64 return NetworkTethering::kUnknown;
65}
66
Alex Vakulenkod2779df2014-06-16 13:19:00 -070067} // namespace
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070068
Alex Deymo30534502015-07-20 15:06:33 -070069ConnectionManager::ConnectionManager(ShillProxyInterface* shill_proxy,
70 SystemState* system_state)
71 : shill_proxy_(shill_proxy), system_state_(system_state) {}
Jay Srinivasan43488792012-06-19 00:25:31 -070072
Alex Deymo6ae91202014-03-10 19:21:25 -070073bool ConnectionManager::IsUpdateAllowedOver(NetworkConnectionType type,
74 NetworkTethering tethering) const {
Jay Srinivasan43488792012-06-19 00:25:31 -070075 switch (type) {
Alex Deymo75eac7e2015-07-29 13:39:14 -070076 case NetworkConnectionType::kBluetooth:
Jay Srinivasan43488792012-06-19 00:25:31 -070077 return false;
78
Alex Deymo75eac7e2015-07-29 13:39:14 -070079 case NetworkConnectionType::kCellular: {
Jay Srinivasan43488792012-06-19 00:25:31 -070080 set<string> allowed_types;
81 const policy::DevicePolicy* device_policy =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080082 system_state_->device_policy();
Alex Deymof4867c42013-06-28 14:41:39 -070083
84 // A device_policy is loaded in a lazy way right before an update check,
85 // so the device_policy should be already loaded at this point. If it's
86 // not, return a safe value for this setting.
Jay Srinivasan43488792012-06-19 00:25:31 -070087 if (!device_policy) {
Alex Deymof4867c42013-06-28 14:41:39 -070088 LOG(INFO) << "Disabling updates over cellular networks as there's no "
89 "device policy loaded yet.";
Jay Srinivasan43488792012-06-19 00:25:31 -070090 return false;
91 }
92
Alex Deymof4867c42013-06-28 14:41:39 -070093 if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
94 // The update setting is enforced by the device policy.
Jay Srinivasan43488792012-06-19 00:25:31 -070095
Gilad Arnold9a423ff2014-03-27 15:27:35 -070096 if (!ContainsKey(allowed_types, shill::kTypeCellular)) {
Alex Deymof4867c42013-06-28 14:41:39 -070097 LOG(INFO) << "Disabling updates over cellular connection as it's not "
98 "allowed in the device policy.";
99 return false;
100 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700101
Alex Deymof4867c42013-06-28 14:41:39 -0700102 LOG(INFO) << "Allowing updates over cellular per device policy.";
103 return true;
104 } else {
105 // There's no update setting in the device policy, using the local user
106 // setting.
107 PrefsInterface* prefs = system_state_->prefs();
108
109 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
110 LOG(INFO) << "Disabling updates over cellular connection as there's "
111 "no device policy setting nor user preference present.";
112 return false;
113 }
114
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700115 bool stored_value;
116 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission,
117 &stored_value)) {
Alex Deymof4867c42013-06-28 14:41:39 -0700118 return false;
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700119 }
Alex Deymof4867c42013-06-28 14:41:39 -0700120
121 if (!stored_value) {
122 LOG(INFO) << "Disabling updates over cellular connection per user "
123 "setting.";
124 return false;
125 }
126 LOG(INFO) << "Allowing updates over cellular per user setting.";
127 return true;
128 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700129 }
130
131 default:
Alex Deymo6ae91202014-03-10 19:21:25 -0700132 if (tethering == NetworkTethering::kConfirmed) {
133 // Treat this connection as if it is a cellular connection.
134 LOG(INFO) << "Current connection is confirmed tethered, using Cellular "
135 "setting.";
Alex Deymo75eac7e2015-07-29 13:39:14 -0700136 return IsUpdateAllowedOver(NetworkConnectionType::kCellular,
137 NetworkTethering::kUnknown);
Alex Deymo6ae91202014-03-10 19:21:25 -0700138 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700139 return true;
140 }
141}
142
Alex Deymof6ee0162015-07-31 12:35:22 -0700143// static
Jay Srinivasan43488792012-06-19 00:25:31 -0700144const char* ConnectionManager::StringForConnectionType(
Alex Deymof6ee0162015-07-31 12:35:22 -0700145 NetworkConnectionType type) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700146 switch (type) {
147 case NetworkConnectionType::kEthernet:
148 return shill::kTypeEthernet;
149 case NetworkConnectionType::kWifi:
150 return shill::kTypeWifi;
151 case NetworkConnectionType::kWimax:
152 return shill::kTypeWimax;
153 case NetworkConnectionType::kBluetooth:
154 return shill::kTypeBluetooth;
155 case NetworkConnectionType::kCellular:
156 return shill::kTypeCellular;
157 case NetworkConnectionType::kUnknown:
158 return "Unknown";
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700159 }
Alex Deymo75eac7e2015-07-29 13:39:14 -0700160 return "Unknown";
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700161}
162
Alex Deymo30534502015-07-20 15:06:33 -0700163bool ConnectionManager::GetConnectionProperties(
164 NetworkConnectionType* out_type,
165 NetworkTethering* out_tethering) {
166 string default_service_path;
167 TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path));
168 if (default_service_path.empty())
169 return false;
170 TEST_AND_RETURN_FALSE(
171 GetServicePathProperties(default_service_path, out_type, out_tethering));
172 return true;
Alex Deymo6ae91202014-03-10 19:21:25 -0700173}
174
Alex Deymo30534502015-07-20 15:06:33 -0700175bool ConnectionManager::GetDefaultServicePath(string* out_path) {
176 chromeos::VariantDictionary properties;
177 chromeos::ErrorPtr error;
178 ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy();
179 if (!manager_proxy)
180 return false;
181 TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error));
182
183 const auto& prop_default_service =
184 properties.find(shill::kDefaultServiceProperty);
185 if (prop_default_service == properties.end())
186 return false;
187
188 *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>().value();
189 return !out_path->empty();
190}
191
192bool ConnectionManager::GetServicePathProperties(
193 const string& path,
Alex Deymo6ae91202014-03-10 19:21:25 -0700194 NetworkConnectionType* out_type,
Alex Deymo30534502015-07-20 15:06:33 -0700195 NetworkTethering* out_tethering) {
196 // We create and dispose the ServiceProxyInterface on every request.
197 std::unique_ptr<ServiceProxyInterface> service =
198 shill_proxy_->GetServiceForPath(path);
199
200 chromeos::VariantDictionary properties;
201 chromeos::ErrorPtr error;
202 TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error));
203
204 // Populate the out_tethering.
205 const auto& prop_tethering = properties.find(shill::kTetheringProperty);
206 if (prop_tethering == properties.end()) {
207 // Set to Unknown if not present.
208 *out_tethering = NetworkTethering::kUnknown;
209 } else {
210 // If the property doesn't contain a string value, the empty string will
211 // become kUnknown.
212 *out_tethering = ParseTethering(prop_tethering->second.TryGet<string>());
213 }
214
215 // Populate the out_type property.
216 const auto& prop_type = properties.find(shill::kTypeProperty);
217 if (prop_type == properties.end()) {
218 // Set to Unknown if not present.
219 *out_type = NetworkConnectionType::kUnknown;
220 return false;
221 }
222
223 string type_str = prop_type->second.TryGet<string>();
224 if (type_str == shill::kTypeVPN) {
225 const auto& prop_physical =
226 properties.find(shill::kPhysicalTechnologyProperty);
227 if (prop_physical == properties.end()) {
228 LOG(ERROR) << "No PhysicalTechnology property found for a VPN"
229 << " connection (service: " << path << "). Returning default"
230 << " kUnknown value.";
231 *out_type = NetworkConnectionType::kUnknown;
232 } else {
233 *out_type = ParseConnectionType(prop_physical->second.TryGet<string>());
234 }
235 } else {
236 *out_type = ParseConnectionType(type_str);
237 }
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700238 return true;
239}
240
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700241} // namespace chromeos_update_engine