Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 5 | #include "update_engine/connection_manager.h" |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 9 | #include <base/stl_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 10 | #include <base/strings/string_util.h> |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 11 | #include <chromeos/dbus/service_constants.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 12 | #include <dbus/dbus-glib.h> |
| 13 | #include <glib.h> |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame^] | 14 | #include <policy/device_policy.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 15 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 16 | #include "update_engine/prefs.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 17 | #include "update_engine/system_state.h" |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 18 | #include "update_engine/utils.h" |
| 19 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 20 | using std::set; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 21 | using std::string; |
| 22 | |
| 23 | namespace chromeos_update_engine { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | // Gets the DbusGProxy for FlimFlam. Must be free'd with ProxyUnref() |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 28 | bool GetFlimFlamProxy(DBusWrapperInterface* dbus_iface, |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 29 | const char* path, |
| 30 | const char* interface, |
| 31 | DBusGProxy** out_proxy) { |
| 32 | DBusGConnection* bus; |
| 33 | DBusGProxy* proxy; |
| 34 | GError* error = NULL; |
| 35 | |
| 36 | bus = dbus_iface->BusGet(DBUS_BUS_SYSTEM, &error); |
| 37 | if (!bus) { |
| 38 | LOG(ERROR) << "Failed to get system bus"; |
| 39 | return false; |
| 40 | } |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 41 | proxy = dbus_iface->ProxyNewForName(bus, shill::kFlimflamServiceName, path, |
| 42 | interface); |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 43 | *out_proxy = proxy; |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | // On success, caller owns the GHashTable at out_hash_table. |
| 48 | // Returns true on success. |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 49 | bool GetProperties(DBusWrapperInterface* dbus_iface, |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 50 | const char* path, |
| 51 | const char* interface, |
| 52 | GHashTable** out_hash_table) { |
| 53 | DBusGProxy* proxy; |
| 54 | GError* error = NULL; |
| 55 | |
| 56 | TEST_AND_RETURN_FALSE(GetFlimFlamProxy(dbus_iface, |
| 57 | path, |
| 58 | interface, |
| 59 | &proxy)); |
| 60 | |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 61 | gboolean rc = dbus_iface->ProxyCall_0_1(proxy, |
| 62 | "GetProperties", |
| 63 | &error, |
| 64 | out_hash_table); |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 65 | dbus_iface->ProxyUnref(proxy); |
| 66 | if (rc == FALSE) { |
| 67 | LOG(ERROR) << "dbus_g_proxy_call failed"; |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | // Returns (via out_path) the default network path, or empty string if |
| 75 | // there's no network up. |
| 76 | // Returns true on success. |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 77 | bool GetDefaultServicePath(DBusWrapperInterface* dbus_iface, string* out_path) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 78 | GHashTable* hash_table = NULL; |
| 79 | |
| 80 | TEST_AND_RETURN_FALSE(GetProperties(dbus_iface, |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 81 | shill::kFlimflamServicePath, |
| 82 | shill::kFlimflamManagerInterface, |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 83 | &hash_table)); |
| 84 | |
| 85 | GValue* value = reinterpret_cast<GValue*>(g_hash_table_lookup(hash_table, |
| 86 | "Services")); |
| 87 | GArray* array = NULL; |
| 88 | bool success = false; |
mukesh agrawal | 88226ff | 2012-03-19 17:50:06 -0700 | [diff] [blame] | 89 | if (G_VALUE_HOLDS(value, DBUS_TYPE_G_OBJECT_PATH_ARRAY) && |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 90 | (array = reinterpret_cast<GArray*>(g_value_get_boxed(value))) && |
| 91 | (array->len > 0)) { |
| 92 | *out_path = g_array_index(array, const char*, 0); |
| 93 | success = true; |
| 94 | } |
mukesh agrawal | 88226ff | 2012-03-19 17:50:06 -0700 | [diff] [blame] | 95 | |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 96 | g_hash_table_unref(hash_table); |
| 97 | return success; |
| 98 | } |
| 99 | |
| 100 | NetworkConnectionType ParseConnectionType(const char* type_str) { |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 101 | if (!strcmp(type_str, shill::kTypeEthernet)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 102 | return kNetEthernet; |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 103 | } else if (!strcmp(type_str, shill::kTypeWifi)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 104 | return kNetWifi; |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 105 | } else if (!strcmp(type_str, shill::kTypeWimax)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 106 | return kNetWimax; |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 107 | } else if (!strcmp(type_str, shill::kTypeBluetooth)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 108 | return kNetBluetooth; |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 109 | } else if (!strcmp(type_str, shill::kTypeCellular)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 110 | return kNetCellular; |
| 111 | } |
| 112 | return kNetUnknown; |
| 113 | } |
| 114 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 115 | NetworkTethering ParseTethering(const char* tethering_str) { |
| 116 | if (!strcmp(tethering_str, shill::kTetheringNotDetectedState)) { |
| 117 | return NetworkTethering::kNotDetected; |
| 118 | } else if (!strcmp(tethering_str, shill::kTetheringSuspectedState)) { |
| 119 | return NetworkTethering::kSuspected; |
| 120 | } else if (!strcmp(tethering_str, shill::kTetheringConfirmedState)) { |
| 121 | return NetworkTethering::kConfirmed; |
| 122 | } |
| 123 | LOG(WARNING) << "Unknown Tethering value: " << tethering_str; |
| 124 | return NetworkTethering::kUnknown; |
| 125 | } |
| 126 | |
| 127 | bool GetServicePathProperties(DBusWrapperInterface* dbus_iface, |
| 128 | const string& path, |
| 129 | NetworkConnectionType* out_type, |
| 130 | NetworkTethering* out_tethering) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 131 | GHashTable* hash_table = NULL; |
| 132 | |
| 133 | TEST_AND_RETURN_FALSE(GetProperties(dbus_iface, |
| 134 | path.c_str(), |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 135 | shill::kFlimflamServiceInterface, |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 136 | &hash_table)); |
| 137 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 138 | // Populate the out_tethering. |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 139 | GValue* value = (GValue*)g_hash_table_lookup(hash_table, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 140 | shill::kTetheringProperty); |
| 141 | const char* tethering_str = NULL; |
| 142 | |
| 143 | if (value != NULL) |
| 144 | tethering_str = g_value_get_string(value); |
| 145 | if (tethering_str != NULL) { |
| 146 | *out_tethering = ParseTethering(tethering_str); |
| 147 | } else { |
| 148 | // Set to Unknown if not present. |
| 149 | *out_tethering = NetworkTethering::kUnknown; |
| 150 | } |
| 151 | |
| 152 | // Populate the out_type property. |
| 153 | value = (GValue*)g_hash_table_lookup(hash_table, |
| 154 | shill::kTypeProperty); |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 155 | const char* type_str = NULL; |
| 156 | bool success = false; |
| 157 | if (value != NULL && (type_str = g_value_get_string(value)) != NULL) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 158 | success = true; |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 159 | if (!strcmp(type_str, shill::kTypeVPN)) { |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 160 | value = (GValue*)g_hash_table_lookup(hash_table, |
| 161 | shill::kPhysicalTechnologyProperty); |
| 162 | if (value != NULL && (type_str = g_value_get_string(value)) != NULL) { |
| 163 | *out_type = ParseConnectionType(type_str); |
| 164 | } else { |
| 165 | LOG(ERROR) << "No PhysicalTechnology property found for a VPN" |
| 166 | << " connection (service: " << path << "). Returning default" |
| 167 | << " kNetUnknown value."; |
| 168 | *out_type = kNetUnknown; |
| 169 | } |
| 170 | } else { |
| 171 | *out_type = ParseConnectionType(type_str); |
| 172 | } |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 173 | } |
| 174 | g_hash_table_unref(hash_table); |
| 175 | return success; |
| 176 | } |
| 177 | |
| 178 | } // namespace {} |
| 179 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 180 | ConnectionManager::ConnectionManager(SystemState *system_state) |
| 181 | : system_state_(system_state) {} |
| 182 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 183 | bool ConnectionManager::IsUpdateAllowedOver(NetworkConnectionType type, |
| 184 | NetworkTethering tethering) const { |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 185 | switch (type) { |
| 186 | case kNetBluetooth: |
| 187 | return false; |
| 188 | |
| 189 | case kNetCellular: { |
| 190 | set<string> allowed_types; |
| 191 | const policy::DevicePolicy* device_policy = |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 192 | system_state_->device_policy(); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 193 | |
| 194 | // A device_policy is loaded in a lazy way right before an update check, |
| 195 | // so the device_policy should be already loaded at this point. If it's |
| 196 | // not, return a safe value for this setting. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 197 | if (!device_policy) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 198 | LOG(INFO) << "Disabling updates over cellular networks as there's no " |
| 199 | "device policy loaded yet."; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 203 | if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) { |
| 204 | // The update setting is enforced by the device policy. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 205 | |
Gilad Arnold | 9a423ff | 2014-03-27 15:27:35 -0700 | [diff] [blame] | 206 | if (!ContainsKey(allowed_types, shill::kTypeCellular)) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 207 | LOG(INFO) << "Disabling updates over cellular connection as it's not " |
| 208 | "allowed in the device policy."; |
| 209 | return false; |
| 210 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 211 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 212 | LOG(INFO) << "Allowing updates over cellular per device policy."; |
| 213 | return true; |
| 214 | } else { |
| 215 | // There's no update setting in the device policy, using the local user |
| 216 | // setting. |
| 217 | PrefsInterface* prefs = system_state_->prefs(); |
| 218 | |
| 219 | if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) { |
| 220 | LOG(INFO) << "Disabling updates over cellular connection as there's " |
| 221 | "no device policy setting nor user preference present."; |
| 222 | return false; |
| 223 | } |
| 224 | |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 225 | bool stored_value; |
| 226 | if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, |
| 227 | &stored_value)) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 228 | return false; |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 229 | } |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 230 | |
| 231 | if (!stored_value) { |
| 232 | LOG(INFO) << "Disabling updates over cellular connection per user " |
| 233 | "setting."; |
| 234 | return false; |
| 235 | } |
| 236 | LOG(INFO) << "Allowing updates over cellular per user setting."; |
| 237 | return true; |
| 238 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | default: |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 242 | if (tethering == NetworkTethering::kConfirmed) { |
| 243 | // Treat this connection as if it is a cellular connection. |
| 244 | LOG(INFO) << "Current connection is confirmed tethered, using Cellular " |
| 245 | "setting."; |
| 246 | return IsUpdateAllowedOver(kNetCellular, NetworkTethering::kUnknown); |
| 247 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | const char* ConnectionManager::StringForConnectionType( |
| 253 | NetworkConnectionType type) const { |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 254 | static const char* const kValues[] = {shill::kTypeEthernet, |
| 255 | shill::kTypeWifi, |
| 256 | shill::kTypeWimax, |
| 257 | shill::kTypeBluetooth, |
| 258 | shill::kTypeCellular}; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 259 | if (type < 0 || type >= static_cast<int>(arraysize(kValues))) { |
| 260 | return "Unknown"; |
| 261 | } |
| 262 | return kValues[type]; |
| 263 | } |
| 264 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 265 | const char* ConnectionManager::StringForTethering( |
| 266 | NetworkTethering tethering) const { |
| 267 | switch (tethering) { |
| 268 | case NetworkTethering::kNotDetected: |
| 269 | return shill::kTetheringNotDetectedState; |
| 270 | case NetworkTethering::kSuspected: |
| 271 | return shill::kTetheringSuspectedState; |
| 272 | case NetworkTethering::kConfirmed: |
| 273 | return shill::kTetheringConfirmedState; |
| 274 | case NetworkTethering::kUnknown: |
| 275 | return "Unknown"; |
| 276 | } |
| 277 | // The program shouldn't reach this point, but the compiler isn't smart |
| 278 | // enough to infer that. |
| 279 | return "Unknown"; |
| 280 | } |
| 281 | |
| 282 | bool ConnectionManager::GetConnectionProperties( |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 283 | DBusWrapperInterface* dbus_iface, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 284 | NetworkConnectionType* out_type, |
| 285 | NetworkTethering* out_tethering) const { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 286 | string default_service_path; |
| 287 | TEST_AND_RETURN_FALSE(GetDefaultServicePath(dbus_iface, |
| 288 | &default_service_path)); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 289 | TEST_AND_RETURN_FALSE(GetServicePathProperties(dbus_iface, |
| 290 | default_service_path, |
| 291 | out_type, out_tethering)); |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 295 | } // namespace chromeos_update_engine |