blob: b5daa141ad6481d5dde39f209bdd4b71976ca51b [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 <dbus/dbus-glib.h>
14#include <glib.h>
Gilad Arnold1f847232014-04-07 12:07:49 -070015#include <policy/device_policy.h>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070016
Alex Deymof4867c42013-06-28 14:41:39 -070017#include "update_engine/prefs.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070018#include "update_engine/system_state.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070019#include "update_engine/utils.h"
20
Jay Srinivasan43488792012-06-19 00:25:31 -070021using std::set;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070022using std::string;
23
24namespace chromeos_update_engine {
25
26namespace {
27
28// Gets the DbusGProxy for FlimFlam. Must be free'd with ProxyUnref()
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080029bool GetFlimFlamProxy(DBusWrapperInterface* dbus_iface,
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070030 const char* path,
31 const char* interface,
32 DBusGProxy** out_proxy) {
33 DBusGConnection* bus;
34 DBusGProxy* proxy;
35 GError* error = NULL;
36
37 bus = dbus_iface->BusGet(DBUS_BUS_SYSTEM, &error);
38 if (!bus) {
39 LOG(ERROR) << "Failed to get system bus";
40 return false;
41 }
Gilad Arnoldb752fb32014-03-03 12:23:39 -080042 proxy = dbus_iface->ProxyNewForName(bus, shill::kFlimflamServiceName, path,
43 interface);
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070044 *out_proxy = proxy;
45 return true;
46}
47
48// On success, caller owns the GHashTable at out_hash_table.
49// Returns true on success.
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080050bool GetProperties(DBusWrapperInterface* dbus_iface,
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070051 const char* path,
52 const char* interface,
53 GHashTable** out_hash_table) {
54 DBusGProxy* proxy;
55 GError* error = NULL;
56
57 TEST_AND_RETURN_FALSE(GetFlimFlamProxy(dbus_iface,
58 path,
59 interface,
60 &proxy));
61
Gilad Arnoldb752fb32014-03-03 12:23:39 -080062 gboolean rc = dbus_iface->ProxyCall_0_1(proxy,
63 "GetProperties",
64 &error,
65 out_hash_table);
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070066 dbus_iface->ProxyUnref(proxy);
67 if (rc == FALSE) {
68 LOG(ERROR) << "dbus_g_proxy_call failed";
69 return false;
70 }
71
72 return true;
73}
74
75// Returns (via out_path) the default network path, or empty string if
76// there's no network up.
77// Returns true on success.
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080078bool GetDefaultServicePath(DBusWrapperInterface* dbus_iface, string* out_path) {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070079 GHashTable* hash_table = NULL;
80
81 TEST_AND_RETURN_FALSE(GetProperties(dbus_iface,
Ben Chanc6007e42013-09-19 23:49:22 -070082 shill::kFlimflamServicePath,
83 shill::kFlimflamManagerInterface,
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070084 &hash_table));
85
86 GValue* value = reinterpret_cast<GValue*>(g_hash_table_lookup(hash_table,
87 "Services"));
Alex Deymo5665d0c2014-05-28 17:45:43 -070088 GPtrArray* array = NULL;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070089 bool success = false;
mukesh agrawal88226ff2012-03-19 17:50:06 -070090 if (G_VALUE_HOLDS(value, DBUS_TYPE_G_OBJECT_PATH_ARRAY) &&
Alex Deymo5665d0c2014-05-28 17:45:43 -070091 (array = reinterpret_cast<GPtrArray*>(g_value_get_boxed(value))) &&
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070092 (array->len > 0)) {
Alex Deymo5665d0c2014-05-28 17:45:43 -070093 *out_path = static_cast<const char*>(g_ptr_array_index(array, 0));
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070094 success = true;
95 }
mukesh agrawal88226ff2012-03-19 17:50:06 -070096
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070097 g_hash_table_unref(hash_table);
98 return success;
99}
100
101NetworkConnectionType ParseConnectionType(const char* type_str) {
Ben Chanc6007e42013-09-19 23:49:22 -0700102 if (!strcmp(type_str, shill::kTypeEthernet)) {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700103 return kNetEthernet;
Ben Chanc6007e42013-09-19 23:49:22 -0700104 } else if (!strcmp(type_str, shill::kTypeWifi)) {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700105 return kNetWifi;
Ben Chanc6007e42013-09-19 23:49:22 -0700106 } else if (!strcmp(type_str, shill::kTypeWimax)) {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700107 return kNetWimax;
Ben Chanc6007e42013-09-19 23:49:22 -0700108 } else if (!strcmp(type_str, shill::kTypeBluetooth)) {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700109 return kNetBluetooth;
Ben Chanc6007e42013-09-19 23:49:22 -0700110 } else if (!strcmp(type_str, shill::kTypeCellular)) {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700111 return kNetCellular;
112 }
113 return kNetUnknown;
114}
115
Alex Deymo6ae91202014-03-10 19:21:25 -0700116NetworkTethering ParseTethering(const char* tethering_str) {
117 if (!strcmp(tethering_str, shill::kTetheringNotDetectedState)) {
118 return NetworkTethering::kNotDetected;
119 } else if (!strcmp(tethering_str, shill::kTetheringSuspectedState)) {
120 return NetworkTethering::kSuspected;
121 } else if (!strcmp(tethering_str, shill::kTetheringConfirmedState)) {
122 return NetworkTethering::kConfirmed;
123 }
124 LOG(WARNING) << "Unknown Tethering value: " << tethering_str;
125 return NetworkTethering::kUnknown;
126}
127
128bool GetServicePathProperties(DBusWrapperInterface* dbus_iface,
129 const string& path,
130 NetworkConnectionType* out_type,
131 NetworkTethering* out_tethering) {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700132 GHashTable* hash_table = NULL;
133
134 TEST_AND_RETURN_FALSE(GetProperties(dbus_iface,
135 path.c_str(),
Ben Chanc6007e42013-09-19 23:49:22 -0700136 shill::kFlimflamServiceInterface,
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700137 &hash_table));
138
Alex Deymo6ae91202014-03-10 19:21:25 -0700139 // Populate the out_tethering.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700140 GValue* value =
141 reinterpret_cast<GValue*>(g_hash_table_lookup(hash_table,
142 shill::kTetheringProperty));
Alex Deymo6ae91202014-03-10 19:21:25 -0700143 const char* tethering_str = NULL;
144
145 if (value != NULL)
146 tethering_str = g_value_get_string(value);
147 if (tethering_str != NULL) {
148 *out_tethering = ParseTethering(tethering_str);
149 } else {
150 // Set to Unknown if not present.
151 *out_tethering = NetworkTethering::kUnknown;
152 }
153
154 // Populate the out_type property.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700155 value = reinterpret_cast<GValue*>(g_hash_table_lookup(hash_table,
156 shill::kTypeProperty));
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700157 const char* type_str = NULL;
158 bool success = false;
159 if (value != NULL && (type_str = g_value_get_string(value)) != NULL) {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700160 success = true;
Ben Chanc6007e42013-09-19 23:49:22 -0700161 if (!strcmp(type_str, shill::kTypeVPN)) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700162 value = reinterpret_cast<GValue*>(
163 g_hash_table_lookup(hash_table, shill::kPhysicalTechnologyProperty));
Alex Deymo1c4e6382013-07-15 12:09:51 -0700164 if (value != NULL && (type_str = g_value_get_string(value)) != NULL) {
165 *out_type = ParseConnectionType(type_str);
166 } else {
167 LOG(ERROR) << "No PhysicalTechnology property found for a VPN"
168 << " connection (service: " << path << "). Returning default"
169 << " kNetUnknown value.";
170 *out_type = kNetUnknown;
171 }
172 } else {
173 *out_type = ParseConnectionType(type_str);
174 }
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700175 }
176 g_hash_table_unref(hash_table);
177 return success;
178}
179
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700180} // namespace
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700181
Jay Srinivasan43488792012-06-19 00:25:31 -0700182ConnectionManager::ConnectionManager(SystemState *system_state)
183 : system_state_(system_state) {}
184
Alex Deymo6ae91202014-03-10 19:21:25 -0700185bool ConnectionManager::IsUpdateAllowedOver(NetworkConnectionType type,
186 NetworkTethering tethering) const {
Jay Srinivasan43488792012-06-19 00:25:31 -0700187 switch (type) {
188 case kNetBluetooth:
189 return false;
190
191 case kNetCellular: {
192 set<string> allowed_types;
193 const policy::DevicePolicy* device_policy =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800194 system_state_->device_policy();
Alex Deymof4867c42013-06-28 14:41:39 -0700195
196 // A device_policy is loaded in a lazy way right before an update check,
197 // so the device_policy should be already loaded at this point. If it's
198 // not, return a safe value for this setting.
Jay Srinivasan43488792012-06-19 00:25:31 -0700199 if (!device_policy) {
Alex Deymof4867c42013-06-28 14:41:39 -0700200 LOG(INFO) << "Disabling updates over cellular networks as there's no "
201 "device policy loaded yet.";
Jay Srinivasan43488792012-06-19 00:25:31 -0700202 return false;
203 }
204
Alex Deymof4867c42013-06-28 14:41:39 -0700205 if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
206 // The update setting is enforced by the device policy.
Jay Srinivasan43488792012-06-19 00:25:31 -0700207
Gilad Arnold9a423ff2014-03-27 15:27:35 -0700208 if (!ContainsKey(allowed_types, shill::kTypeCellular)) {
Alex Deymof4867c42013-06-28 14:41:39 -0700209 LOG(INFO) << "Disabling updates over cellular connection as it's not "
210 "allowed in the device policy.";
211 return false;
212 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700213
Alex Deymof4867c42013-06-28 14:41:39 -0700214 LOG(INFO) << "Allowing updates over cellular per device policy.";
215 return true;
216 } else {
217 // There's no update setting in the device policy, using the local user
218 // setting.
219 PrefsInterface* prefs = system_state_->prefs();
220
221 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
222 LOG(INFO) << "Disabling updates over cellular connection as there's "
223 "no device policy setting nor user preference present.";
224 return false;
225 }
226
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700227 bool stored_value;
228 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission,
229 &stored_value)) {
Alex Deymof4867c42013-06-28 14:41:39 -0700230 return false;
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700231 }
Alex Deymof4867c42013-06-28 14:41:39 -0700232
233 if (!stored_value) {
234 LOG(INFO) << "Disabling updates over cellular connection per user "
235 "setting.";
236 return false;
237 }
238 LOG(INFO) << "Allowing updates over cellular per user setting.";
239 return true;
240 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700241 }
242
243 default:
Alex Deymo6ae91202014-03-10 19:21:25 -0700244 if (tethering == NetworkTethering::kConfirmed) {
245 // Treat this connection as if it is a cellular connection.
246 LOG(INFO) << "Current connection is confirmed tethered, using Cellular "
247 "setting.";
248 return IsUpdateAllowedOver(kNetCellular, NetworkTethering::kUnknown);
249 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700250 return true;
251 }
252}
253
254const char* ConnectionManager::StringForConnectionType(
255 NetworkConnectionType type) const {
Ben Chanc6007e42013-09-19 23:49:22 -0700256 static const char* const kValues[] = {shill::kTypeEthernet,
257 shill::kTypeWifi,
258 shill::kTypeWimax,
259 shill::kTypeBluetooth,
260 shill::kTypeCellular};
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700261 if (type < 0 || type >= static_cast<int>(arraysize(kValues))) {
262 return "Unknown";
263 }
264 return kValues[type];
265}
266
Alex Deymo6ae91202014-03-10 19:21:25 -0700267const char* ConnectionManager::StringForTethering(
268 NetworkTethering tethering) const {
269 switch (tethering) {
270 case NetworkTethering::kNotDetected:
271 return shill::kTetheringNotDetectedState;
272 case NetworkTethering::kSuspected:
273 return shill::kTetheringSuspectedState;
274 case NetworkTethering::kConfirmed:
275 return shill::kTetheringConfirmedState;
276 case NetworkTethering::kUnknown:
277 return "Unknown";
278 }
279 // The program shouldn't reach this point, but the compiler isn't smart
280 // enough to infer that.
281 return "Unknown";
282}
283
284bool ConnectionManager::GetConnectionProperties(
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800285 DBusWrapperInterface* dbus_iface,
Alex Deymo6ae91202014-03-10 19:21:25 -0700286 NetworkConnectionType* out_type,
287 NetworkTethering* out_tethering) const {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700288 string default_service_path;
289 TEST_AND_RETURN_FALSE(GetDefaultServicePath(dbus_iface,
290 &default_service_path));
Alex Deymo6ae91202014-03-10 19:21:25 -0700291 TEST_AND_RETURN_FALSE(GetServicePathProperties(dbus_iface,
292 default_service_path,
293 out_type, out_tethering));
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700294 return true;
295}
296
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700297} // namespace chromeos_update_engine