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 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame^] | 7 | #include <set> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 10 | #include <base/stl_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 11 | #include <base/strings/string_util.h> |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 13 | #include <dbus/dbus-glib.h> |
| 14 | #include <glib.h> |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 15 | #include <policy/device_policy.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 16 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 17 | #include "update_engine/prefs.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 18 | #include "update_engine/system_state.h" |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 19 | #include "update_engine/utils.h" |
| 20 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 21 | using std::set; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 22 | using std::string; |
| 23 | |
| 24 | namespace chromeos_update_engine { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | // Gets the DbusGProxy for FlimFlam. Must be free'd with ProxyUnref() |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 29 | bool GetFlimFlamProxy(DBusWrapperInterface* dbus_iface, |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 30 | 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 Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 42 | proxy = dbus_iface->ProxyNewForName(bus, shill::kFlimflamServiceName, path, |
| 43 | interface); |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 44 | *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 Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 50 | bool GetProperties(DBusWrapperInterface* dbus_iface, |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 51 | 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 Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 62 | gboolean rc = dbus_iface->ProxyCall_0_1(proxy, |
| 63 | "GetProperties", |
| 64 | &error, |
| 65 | out_hash_table); |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 66 | 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 Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 78 | bool GetDefaultServicePath(DBusWrapperInterface* dbus_iface, string* out_path) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 79 | GHashTable* hash_table = NULL; |
| 80 | |
| 81 | TEST_AND_RETURN_FALSE(GetProperties(dbus_iface, |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 82 | shill::kFlimflamServicePath, |
| 83 | shill::kFlimflamManagerInterface, |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 84 | &hash_table)); |
| 85 | |
| 86 | GValue* value = reinterpret_cast<GValue*>(g_hash_table_lookup(hash_table, |
| 87 | "Services")); |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 88 | GPtrArray* array = NULL; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 89 | bool success = false; |
mukesh agrawal | 88226ff | 2012-03-19 17:50:06 -0700 | [diff] [blame] | 90 | if (G_VALUE_HOLDS(value, DBUS_TYPE_G_OBJECT_PATH_ARRAY) && |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 91 | (array = reinterpret_cast<GPtrArray*>(g_value_get_boxed(value))) && |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 92 | (array->len > 0)) { |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 93 | *out_path = static_cast<const char*>(g_ptr_array_index(array, 0)); |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 94 | success = true; |
| 95 | } |
mukesh agrawal | 88226ff | 2012-03-19 17:50:06 -0700 | [diff] [blame] | 96 | |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 97 | g_hash_table_unref(hash_table); |
| 98 | return success; |
| 99 | } |
| 100 | |
| 101 | NetworkConnectionType ParseConnectionType(const char* type_str) { |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 102 | if (!strcmp(type_str, shill::kTypeEthernet)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 103 | return kNetEthernet; |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 104 | } else if (!strcmp(type_str, shill::kTypeWifi)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 105 | return kNetWifi; |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 106 | } else if (!strcmp(type_str, shill::kTypeWimax)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 107 | return kNetWimax; |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 108 | } else if (!strcmp(type_str, shill::kTypeBluetooth)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 109 | return kNetBluetooth; |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 110 | } else if (!strcmp(type_str, shill::kTypeCellular)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 111 | return kNetCellular; |
| 112 | } |
| 113 | return kNetUnknown; |
| 114 | } |
| 115 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 116 | NetworkTethering 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 | |
| 128 | bool GetServicePathProperties(DBusWrapperInterface* dbus_iface, |
| 129 | const string& path, |
| 130 | NetworkConnectionType* out_type, |
| 131 | NetworkTethering* out_tethering) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 132 | GHashTable* hash_table = NULL; |
| 133 | |
| 134 | TEST_AND_RETURN_FALSE(GetProperties(dbus_iface, |
| 135 | path.c_str(), |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 136 | shill::kFlimflamServiceInterface, |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 137 | &hash_table)); |
| 138 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 139 | // Populate the out_tethering. |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame^] | 140 | GValue* value = |
| 141 | reinterpret_cast<GValue*>(g_hash_table_lookup(hash_table, |
| 142 | shill::kTetheringProperty)); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 143 | 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 Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame^] | 155 | value = reinterpret_cast<GValue*>(g_hash_table_lookup(hash_table, |
| 156 | shill::kTypeProperty)); |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 157 | const char* type_str = NULL; |
| 158 | bool success = false; |
| 159 | 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] | 160 | success = true; |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 161 | if (!strcmp(type_str, shill::kTypeVPN)) { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame^] | 162 | value = reinterpret_cast<GValue*>( |
| 163 | g_hash_table_lookup(hash_table, shill::kPhysicalTechnologyProperty)); |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 164 | 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 Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 175 | } |
| 176 | g_hash_table_unref(hash_table); |
| 177 | return success; |
| 178 | } |
| 179 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame^] | 180 | } // namespace |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 181 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 182 | ConnectionManager::ConnectionManager(SystemState *system_state) |
| 183 | : system_state_(system_state) {} |
| 184 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 185 | bool ConnectionManager::IsUpdateAllowedOver(NetworkConnectionType type, |
| 186 | NetworkTethering tethering) const { |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 187 | switch (type) { |
| 188 | case kNetBluetooth: |
| 189 | return false; |
| 190 | |
| 191 | case kNetCellular: { |
| 192 | set<string> allowed_types; |
| 193 | const policy::DevicePolicy* device_policy = |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 194 | system_state_->device_policy(); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 195 | |
| 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 Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 199 | if (!device_policy) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 200 | LOG(INFO) << "Disabling updates over cellular networks as there's no " |
| 201 | "device policy loaded yet."; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 205 | if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) { |
| 206 | // The update setting is enforced by the device policy. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 207 | |
Gilad Arnold | 9a423ff | 2014-03-27 15:27:35 -0700 | [diff] [blame] | 208 | if (!ContainsKey(allowed_types, shill::kTypeCellular)) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 209 | LOG(INFO) << "Disabling updates over cellular connection as it's not " |
| 210 | "allowed in the device policy."; |
| 211 | return false; |
| 212 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 213 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 214 | 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 Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 227 | bool stored_value; |
| 228 | if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, |
| 229 | &stored_value)) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 230 | return false; |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 231 | } |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 232 | |
| 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 Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | default: |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 244 | 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 Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 250 | return true; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | const char* ConnectionManager::StringForConnectionType( |
| 255 | NetworkConnectionType type) const { |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 256 | static const char* const kValues[] = {shill::kTypeEthernet, |
| 257 | shill::kTypeWifi, |
| 258 | shill::kTypeWimax, |
| 259 | shill::kTypeBluetooth, |
| 260 | shill::kTypeCellular}; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 261 | if (type < 0 || type >= static_cast<int>(arraysize(kValues))) { |
| 262 | return "Unknown"; |
| 263 | } |
| 264 | return kValues[type]; |
| 265 | } |
| 266 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 267 | const 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 | |
| 284 | bool ConnectionManager::GetConnectionProperties( |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 285 | DBusWrapperInterface* dbus_iface, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 286 | NetworkConnectionType* out_type, |
| 287 | NetworkTethering* out_tethering) const { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 288 | string default_service_path; |
| 289 | TEST_AND_RETURN_FALSE(GetDefaultServicePath(dbus_iface, |
| 290 | &default_service_path)); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 291 | TEST_AND_RETURN_FALSE(GetServicePathProperties(dbus_iface, |
| 292 | default_service_path, |
| 293 | out_type, out_tethering)); |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 294 | return true; |
| 295 | } |
| 296 | |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 297 | } // namespace chromeos_update_engine |