1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* NetworkManager -- Network link manager
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Copyright (C) 2007 - 2009 Novell, Inc.
19 * Copyright (C) 2007 - 2012 Red Hat, Inc.
22 #include "nm-default.h"
30 #include "nm-manager.h"
31 #include "nm-bus-manager.h"
32 #include "nm-vpn-manager.h"
33 #include "nm-device.h"
34 #include "nm-device-generic.h"
35 #include "nm-platform.h"
36 #include "nm-rfkill-manager.h"
37 #include "nm-dhcp-manager.h"
38 #include "nm-settings.h"
39 #include "nm-settings-connection.h"
40 #include "nm-auth-utils.h"
41 #include "nm-auth-manager.h"
42 #include "NetworkManagerUtils.h"
43 #include "nm-device-factory.h"
44 #include "nm-enum-types.h"
45 #include "nm-sleep-monitor.h"
46 #include "nm-connectivity.h"
47 #include "nm-policy.h"
48 #include "nm-connection-provider.h"
49 #include "nm-session-monitor.h"
50 #include "nm-activation-request.h"
51 #include "nm-core-internal.h"
52 #include "nm-config.h"
53 #include "nm-audit-manager.h"
54 #include "nm-dbus-compat.h"
55 #include "NetworkManagerUtils.h"
57 #include "nmdbus-manager.h"
58 #include "nmdbus-device.h"
60 static gboolean add_device (NMManager *self, NMDevice *device, GError **error);
62 static NMActiveConnection *_new_active_connection (NMManager *self,
63 NMConnection *connection,
64 const char *specific_object,
66 NMAuthSubject *subject,
69 static void policy_activating_device_changed (GObject *object, GParamSpec *pspec, gpointer user_data);
71 static void rfkill_change (const char *desc, RfKillType rtype, gboolean enabled);
73 static gboolean find_master (NMManager *self,
74 NMConnection *connection,
76 NMSettingsConnection **out_master_connection,
77 NMDevice **out_master_device,
78 NMActiveConnection **out_master_ac,
81 static void nm_manager_update_state (NMManager *manager);
83 static void connection_changed (NMSettings *settings, NMConnection *connection,
86 #define TAG_ACTIVE_CONNETION_ADD_AND_ACTIVATE "act-con-add-and-activate"
89 gboolean user_enabled;
102 GSList *active_connections;
103 GSList *authorizing_connections;
105 NMActiveConnection *primary_connection;
106 NMActiveConnection *activating_connection;
112 NMConnectivity *connectivity;
116 NMBusManager *dbus_mgr;
118 GDBusConnection *connection;
121 NMRfkillManager *rfkill_mgr;
123 NMSettings *settings;
126 RadioState radio_states[RFKILL_TYPE_MAX];
128 gboolean net_enabled;
130 NMVpnManager *vpn_manager;
132 NMSleepMonitor *sleep_monitor;
136 /* Firmware dir monitor */
137 GFileMonitor *fw_monitor;
140 guint timestamp_update_id;
143 gboolean devices_inited;
146 #define NM_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_MANAGER, NMManagerPrivate))
148 G_DEFINE_TYPE (NMManager, nm_manager, NM_TYPE_EXPORTED_OBJECT)
152 INTERNAL_DEVICE_ADDED,
154 INTERNAL_DEVICE_REMOVED,
157 USER_PERMISSIONS_CHANGED,
158 ACTIVE_CONNECTION_ADDED,
159 ACTIVE_CONNECTION_REMOVED,
165 static guint signals[LAST_SIGNAL] = { 0 };
173 PROP_NETWORKING_ENABLED,
174 PROP_WIRELESS_ENABLED,
175 PROP_WIRELESS_HARDWARE_ENABLED,
177 PROP_WWAN_HARDWARE_ENABLED,
179 PROP_WIMAX_HARDWARE_ENABLED,
180 PROP_ACTIVE_CONNECTIONS,
182 PROP_PRIMARY_CONNECTION,
183 PROP_PRIMARY_CONNECTION_TYPE,
184 PROP_ACTIVATING_CONNECTION,
187 PROP_GLOBAL_DNS_CONFIGURATION,
197 NM_DEFINE_SINGLETON_INSTANCE (NMManager);
199 /************************************************************************/
201 #define _NMLOG_PREFIX_NAME "manager"
202 #define _NMLOG(level, domain, ...) \
204 const NMLogLevel __level = (level); \
205 const NMLogDomain __domain = (domain); \
207 if (nm_logging_enabled (__level, __domain)) { \
208 const NMManager *const __self = (self); \
211 _nm_log (__level, __domain, 0, \
212 "%s%s: " _NM_UTILS_MACRO_FIRST (__VA_ARGS__), \
213 _NMLOG_PREFIX_NAME, \
214 (__self && __self != singleton_instance) \
215 ? nm_sprintf_buf (__sbuf, "[%p]", __self) \
217 _NM_UTILS_MACRO_REST (__VA_ARGS__)); \
221 /************************************************************************/
223 static void active_connection_state_changed (NMActiveConnection *active,
226 static void active_connection_default_changed (NMActiveConnection *active,
230 /* Returns: whether to notify D-Bus of the removal or not */
232 active_connection_remove (NMManager *self, NMActiveConnection *active)
234 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
235 gboolean notify = nm_exported_object_is_exported (NM_EXPORTED_OBJECT (active));
238 /* FIXME: switch to a GList for faster removal */
239 found = g_slist_find (priv->active_connections, active);
241 NMSettingsConnection *connection;
243 priv->active_connections = g_slist_remove (priv->active_connections, active);
244 g_signal_emit (self, signals[ACTIVE_CONNECTION_REMOVED], 0, active);
245 g_signal_handlers_disconnect_by_func (active, active_connection_state_changed, self);
246 g_signal_handlers_disconnect_by_func (active, active_connection_default_changed, self);
248 if ( nm_active_connection_get_assumed (active)
249 && (connection = nm_active_connection_get_settings_connection (active))
250 && nm_settings_connection_get_nm_generated_assumed (connection))
251 g_object_ref (connection);
255 nm_exported_object_clear_and_unexport (&active);
258 && nm_settings_has_connection (priv->settings, connection)) {
259 _LOGD (LOGD_DEVICE, "assumed connection disconnected. Deleting generated connection '%s' (%s)",
260 nm_settings_connection_get_id (connection), nm_settings_connection_get_uuid (connection));
261 nm_settings_connection_delete (NM_SETTINGS_CONNECTION (connection), NULL, NULL);
262 g_object_unref (connection);
266 return found && notify;
270 _active_connection_cleanup (gpointer user_data)
272 NMManager *self = NM_MANAGER (user_data);
273 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
276 priv->ac_cleanup_id = 0;
278 g_object_freeze_notify (G_OBJECT (self));
279 iter = priv->active_connections;
281 NMActiveConnection *ac = iter->data;
284 if (nm_active_connection_get_state (ac) == NM_ACTIVE_CONNECTION_STATE_DEACTIVATED) {
285 if (active_connection_remove (self, ac))
286 g_object_notify (G_OBJECT (self), NM_MANAGER_ACTIVE_CONNECTIONS);
289 g_object_thaw_notify (G_OBJECT (self));
295 active_connection_state_changed (NMActiveConnection *active,
299 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
300 NMActiveConnectionState state;
302 state = nm_active_connection_get_state (active);
303 if (state == NM_ACTIVE_CONNECTION_STATE_DEACTIVATED) {
304 /* Destroy active connections from an idle handler to ensure that
305 * their last property change notifications go out, which wouldn't
306 * happen if we destroyed them immediately when their state was set
309 if (!priv->ac_cleanup_id)
310 priv->ac_cleanup_id = g_idle_add (_active_connection_cleanup, self);
313 nm_manager_update_state (self);
317 active_connection_default_changed (NMActiveConnection *active,
321 nm_manager_update_state (self);
325 * active_connection_add():
326 * @self: the #NMManager
327 * @active: the #NMActiveConnection to manage
329 * Begins to track and manage @active. Increases the refcount of @active.
332 active_connection_add (NMManager *self, NMActiveConnection *active)
334 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
336 g_return_if_fail (g_slist_find (priv->active_connections, active) == FALSE);
338 priv->active_connections = g_slist_prepend (priv->active_connections,
339 g_object_ref (active));
341 g_signal_connect (active,
342 "notify::" NM_ACTIVE_CONNECTION_STATE,
343 G_CALLBACK (active_connection_state_changed),
345 g_signal_connect (active,
346 "notify::" NM_ACTIVE_CONNECTION_DEFAULT,
347 G_CALLBACK (active_connection_default_changed),
349 g_signal_connect (active,
350 "notify::" NM_ACTIVE_CONNECTION_DEFAULT6,
351 G_CALLBACK (active_connection_default_changed),
354 g_signal_emit (self, signals[ACTIVE_CONNECTION_ADDED], 0, active);
356 /* Only notify D-Bus if the active connection is actually exported */
357 if (nm_exported_object_is_exported (NM_EXPORTED_OBJECT (active)))
358 g_object_notify (G_OBJECT (self), NM_MANAGER_ACTIVE_CONNECTIONS);
362 nm_manager_get_active_connections (NMManager *manager)
364 return NM_MANAGER_GET_PRIVATE (manager)->active_connections;
367 static NMActiveConnection *
368 find_ac_for_connection (NMManager *manager, NMConnection *connection)
370 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
372 const char *uuid = NULL;
373 gboolean is_settings_connection;
375 is_settings_connection = NM_IS_SETTINGS_CONNECTION (connection);
377 if (!is_settings_connection)
378 uuid = nm_connection_get_uuid (connection);
380 for (iter = priv->active_connections; iter; iter = iter->next) {
381 NMActiveConnection *ac = iter->data;
382 NMSettingsConnection *con;
384 con = nm_active_connection_get_settings_connection (ac);
386 /* depending on whether we have a NMSettingsConnection or a NMConnection,
387 * we lookup by UUID or by reference. */
388 if (is_settings_connection) {
389 if (con != (NMSettingsConnection *) connection)
392 if (strcmp (uuid, nm_connection_get_uuid (NM_CONNECTION (con))) != 0)
395 if (nm_active_connection_get_state (ac) < NM_ACTIVE_CONNECTION_STATE_DEACTIVATED)
402 /* Filter out connections that are already active.
403 * nm_settings_get_connections() returns sorted list. We need to preserve the
404 * order so that we didn't change auto-activation order (recent timestamps
406 * Caller is responsible for freeing the returned list with g_slist_free().
409 nm_manager_get_activatable_connections (NMManager *manager)
411 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
412 GSList *all_connections = nm_settings_get_connections (priv->settings);
413 GSList *connections = NULL, *iter;
414 NMSettingsConnection *connection;
416 for (iter = all_connections; iter; iter = iter->next) {
417 connection = iter->data;
419 if (!find_ac_for_connection (manager, NM_CONNECTION (connection)))
420 connections = g_slist_prepend (connections, connection);
423 g_slist_free (all_connections);
424 return g_slist_reverse (connections);
427 static NMActiveConnection *
428 active_connection_get_by_path (NMManager *manager, const char *path)
430 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
433 g_return_val_if_fail (manager != NULL, NULL);
434 g_return_val_if_fail (path != NULL, NULL);
436 for (iter = priv->active_connections; iter; iter = g_slist_next (iter)) {
437 NMActiveConnection *candidate = iter->data;
439 if (g_strcmp0 (path, nm_exported_object_get_path (NM_EXPORTED_OBJECT (candidate))) == 0)
445 /************************************************************************/
448 _config_changed_cb (NMConfig *config, NMConfigData *config_data, NMConfigChangeFlags changes, NMConfigData *old_data, NMManager *self)
450 g_object_set (NM_MANAGER_GET_PRIVATE (self)->connectivity,
451 NM_CONNECTIVITY_URI, nm_config_data_get_connectivity_uri (config_data),
452 NM_CONNECTIVITY_INTERVAL, nm_config_data_get_connectivity_interval (config_data),
453 NM_CONNECTIVITY_RESPONSE, nm_config_data_get_connectivity_response (config_data),
456 if (NM_FLAGS_HAS (changes, NM_CONFIG_CHANGE_GLOBAL_DNS_CONFIG))
457 g_object_notify (G_OBJECT (self), NM_MANAGER_GLOBAL_DNS_CONFIGURATION);
460 /************************************************************************/
463 nm_manager_get_device_by_path (NMManager *manager, const char *path)
467 g_return_val_if_fail (path != NULL, NULL);
469 for (iter = NM_MANAGER_GET_PRIVATE (manager)->devices; iter; iter = iter->next) {
470 if (!strcmp (nm_exported_object_get_path (NM_EXPORTED_OBJECT (iter->data)), path))
471 return NM_DEVICE (iter->data);
477 nm_manager_get_device_by_ifindex (NMManager *manager, int ifindex)
481 for (iter = NM_MANAGER_GET_PRIVATE (manager)->devices; iter; iter = iter->next) {
482 NMDevice *device = NM_DEVICE (iter->data);
484 if (nm_device_get_ifindex (device) == ifindex)
492 find_device_by_hw_addr (NMManager *manager, const char *hwaddr)
495 const char *device_addr;
497 g_return_val_if_fail (hwaddr != NULL, NULL);
499 if (nm_utils_hwaddr_valid (hwaddr, -1)) {
500 for (iter = NM_MANAGER_GET_PRIVATE (manager)->devices; iter; iter = iter->next) {
501 device_addr = nm_device_get_hw_address (NM_DEVICE (iter->data));
502 if (device_addr && nm_utils_hwaddr_matches (hwaddr, -1, device_addr, -1))
503 return NM_DEVICE (iter->data);
510 find_device_by_ip_iface (NMManager *self, const gchar *iface)
514 g_return_val_if_fail (iface != NULL, NULL);
516 for (iter = NM_MANAGER_GET_PRIVATE (self)->devices; iter; iter = g_slist_next (iter)) {
517 NMDevice *candidate = iter->data;
519 if ( nm_device_is_real (candidate)
520 && g_strcmp0 (nm_device_get_ip_iface (candidate), iface) == 0)
527 * find_device_by_iface:
528 * @self: the #NMManager
529 * @iface: the device interface to find
530 * @connection: a connection to ensure the returned device is compatible with
531 * @slave: a slave connection to ensure a master is compatible with
533 * Finds a device by interface name, preferring realized devices. If @slave
534 * is given, this function will only return master devices and will ensure
535 * @slave, when activated, can be a slave of the returned master device. If
536 * @connection is given, this function will only consider devices that are
537 * compatible with @connection.
539 * Returns: the matching #NMDevice
542 find_device_by_iface (NMManager *self,
544 NMConnection *connection,
547 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
548 NMDevice *fallback = NULL;
551 g_return_val_if_fail (iface != NULL, NULL);
553 for (iter = priv->devices; iter; iter = iter->next) {
554 NMDevice *candidate = iter->data;
556 if (strcmp (nm_device_get_iface (candidate), iface))
558 if (connection && !nm_device_check_connection_compatible (candidate, connection))
561 if (!nm_device_is_master (candidate))
563 if (!nm_device_check_slave_connection_compatible (candidate, slave))
567 if (nm_device_is_real (candidate))
570 fallback = candidate;
576 manager_sleeping (NMManager *self)
578 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
580 if (priv->sleeping || !priv->net_enabled)
586 _nm_state_to_string (NMState state)
589 case NM_STATE_ASLEEP:
591 case NM_STATE_DISCONNECTED:
592 return "DISCONNECTED";
593 case NM_STATE_DISCONNECTING:
594 return "DISCONNECTING";
595 case NM_STATE_CONNECTING:
597 case NM_STATE_CONNECTED_LOCAL:
598 return "CONNECTED_LOCAL";
599 case NM_STATE_CONNECTED_SITE:
600 return "CONNECTED_SITE";
601 case NM_STATE_CONNECTED_GLOBAL:
602 return "CONNECTED_GLOBAL";
603 case NM_STATE_UNKNOWN:
610 set_state (NMManager *self, NMState state)
612 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
614 if (priv->state == state)
619 _LOGI (LOGD_CORE, "NetworkManager state is now %s", _nm_state_to_string (state));
621 g_object_notify (G_OBJECT (self), NM_MANAGER_STATE);
622 g_signal_emit (self, signals[STATE_CHANGED], 0, priv->state);
626 checked_connectivity (GObject *object, GAsyncResult *result, gpointer user_data)
628 NMManager *manager = user_data;
629 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
630 NMConnectivityState connectivity;
632 if (priv->state == NM_STATE_CONNECTING || priv->state == NM_STATE_CONNECTED_SITE) {
633 connectivity = nm_connectivity_check_finish (priv->connectivity, result, NULL);
635 if (connectivity == NM_CONNECTIVITY_FULL)
636 set_state (manager, NM_STATE_CONNECTED_GLOBAL);
637 else if ( connectivity == NM_CONNECTIVITY_PORTAL
638 || connectivity == NM_CONNECTIVITY_LIMITED)
639 set_state (manager, NM_STATE_CONNECTED_SITE);
640 g_object_notify (G_OBJECT (manager), NM_MANAGER_CONNECTIVITY);
643 g_object_unref (manager);
647 find_best_device_state (NMManager *manager)
649 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
650 NMState best_state = NM_STATE_DISCONNECTED;
653 for (iter = priv->active_connections; iter; iter = iter->next) {
654 NMActiveConnection *ac = NM_ACTIVE_CONNECTION (iter->data);
655 NMActiveConnectionState ac_state = nm_active_connection_get_state (ac);
658 case NM_ACTIVE_CONNECTION_STATE_ACTIVATED:
659 if ( nm_active_connection_get_default (ac)
660 || nm_active_connection_get_default6 (ac)) {
661 if (nm_connectivity_get_state (priv->connectivity) == NM_CONNECTIVITY_FULL)
662 return NM_STATE_CONNECTED_GLOBAL;
664 best_state = NM_STATE_CONNECTED_SITE;
666 if (best_state < NM_STATE_CONNECTING)
667 best_state = NM_STATE_CONNECTED_LOCAL;
670 case NM_ACTIVE_CONNECTION_STATE_ACTIVATING:
671 if (!nm_active_connection_get_assumed (ac)) {
672 if (best_state != NM_STATE_CONNECTED_GLOBAL)
673 best_state = NM_STATE_CONNECTING;
676 case NM_ACTIVE_CONNECTION_STATE_DEACTIVATING:
677 if (!nm_active_connection_get_assumed (ac)) {
678 if (best_state < NM_STATE_DISCONNECTING)
679 best_state = NM_STATE_DISCONNECTING;
691 nm_manager_update_metered (NMManager *self)
693 NMManagerPrivate *priv;
695 NMMetered value = NM_METERED_UNKNOWN;
697 g_return_if_fail (NM_IS_MANAGER (self));
698 priv = NM_MANAGER_GET_PRIVATE (self);
700 if (priv->primary_connection) {
701 device = nm_active_connection_get_device (priv->primary_connection);
703 value = nm_device_get_metered (device);
706 if (value != priv->metered) {
707 priv->metered = value;
708 _LOGD (LOGD_CORE, "new metered value: %d", (int) priv->metered);
709 g_object_notify (G_OBJECT (self), NM_MANAGER_METERED);
714 nm_manager_update_state (NMManager *manager)
716 NMManagerPrivate *priv;
717 NMState new_state = NM_STATE_DISCONNECTED;
719 g_return_if_fail (NM_IS_MANAGER (manager));
721 priv = NM_MANAGER_GET_PRIVATE (manager);
723 if (manager_sleeping (manager))
724 new_state = NM_STATE_ASLEEP;
726 new_state = find_best_device_state (manager);
728 nm_connectivity_set_online (priv->connectivity, new_state >= NM_STATE_CONNECTED_LOCAL);
730 if (new_state == NM_STATE_CONNECTED_SITE) {
731 nm_connectivity_check_async (priv->connectivity,
732 checked_connectivity,
733 g_object_ref (manager));
736 set_state (manager, new_state);
740 manager_device_state_changed (NMDevice *device,
741 NMDeviceState new_state,
742 NMDeviceState old_state,
743 NMDeviceStateReason reason,
746 NMManager *self = NM_MANAGER (user_data);
747 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
750 case NM_DEVICE_STATE_UNMANAGED:
751 case NM_DEVICE_STATE_UNAVAILABLE:
752 case NM_DEVICE_STATE_DISCONNECTED:
753 case NM_DEVICE_STATE_PREPARE:
754 case NM_DEVICE_STATE_FAILED:
755 g_object_notify (G_OBJECT (self), NM_MANAGER_ACTIVE_CONNECTIONS);
761 if ( new_state == NM_DEVICE_STATE_UNAVAILABLE
762 || new_state == NM_DEVICE_STATE_DISCONNECTED)
763 nm_settings_device_added (priv->settings, device);
766 static void device_has_pending_action_changed (NMDevice *device,
771 check_if_startup_complete (NMManager *self)
773 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
779 if (!priv->devices_inited)
782 if (!nm_settings_get_startup_complete (priv->settings)) {
783 _LOGD (LOGD_CORE, "check_if_startup_complete returns FALSE because of NMSettings");
787 for (iter = priv->devices; iter; iter = iter->next) {
788 NMDevice *dev = iter->data;
790 if (nm_device_has_pending_action (dev)) {
791 _LOGD (LOGD_CORE, "check_if_startup_complete returns FALSE because of %s",
792 nm_device_get_iface (dev));
797 _LOGI (LOGD_CORE, "startup complete");
799 priv->startup = FALSE;
800 g_object_notify (G_OBJECT (self), "startup");
802 /* We don't have to watch notify::has-pending-action any more. */
803 for (iter = priv->devices; iter; iter = iter->next) {
804 NMDevice *dev = iter->data;
806 g_signal_handlers_disconnect_by_func (dev, G_CALLBACK (device_has_pending_action_changed), self);
809 if (nm_config_get_configure_and_quit (nm_config_get ()))
810 g_signal_emit (self, signals[CONFIGURE_QUIT], 0);
814 device_has_pending_action_changed (NMDevice *device,
818 check_if_startup_complete (self);
822 settings_startup_complete_changed (NMSettings *settings,
826 check_if_startup_complete (self);
830 remove_device (NMManager *self,
833 gboolean allow_unmanage)
835 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
837 _LOGD (LOGD_DEVICE, "(%s): removing device (allow_unmanage %d, managed %d)",
838 nm_device_get_iface (device), allow_unmanage, nm_device_get_managed (device, FALSE));
840 if (allow_unmanage && nm_device_get_managed (device, FALSE)) {
841 NMActRequest *req = nm_device_get_act_request (device);
842 gboolean unmanage = FALSE;
844 /* Leave activated interfaces up when quitting so their configuration
845 * can be taken over when NM restarts. This ensures connectivity while
846 * NM is stopped. Devices which do not support connection assumption
849 if (!quitting) /* Forced removal; device already gone */
851 else if (!nm_device_can_assume_active_connection (device))
858 nm_device_set_unmanaged_by_quitting (device);
860 nm_device_set_unmanaged_by_flags (device, NM_UNMANAGED_PLATFORM_INIT, TRUE, NM_DEVICE_STATE_REASON_REMOVED);
861 } else if (quitting && nm_config_get_configure_and_quit (nm_config_get ())) {
862 nm_device_spawn_iface_helper (device);
866 g_signal_handlers_disconnect_matched (device, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
868 nm_settings_device_removed (priv->settings, device, quitting);
869 priv->devices = g_slist_remove (priv->devices, device);
871 if (nm_device_is_real (device)) {
872 g_signal_emit (self, signals[DEVICE_REMOVED], 0, device);
873 g_object_notify (G_OBJECT (self), NM_MANAGER_DEVICES);
874 nm_device_removed (device);
876 g_signal_emit (self, signals[INTERNAL_DEVICE_REMOVED], 0, device);
877 g_object_notify (G_OBJECT (self), NM_MANAGER_ALL_DEVICES);
879 nm_exported_object_clear_and_unexport (&device);
881 check_if_startup_complete (self);
885 device_removed_cb (NMDevice *device, gpointer user_data)
887 remove_device (NM_MANAGER (user_data), device, FALSE, TRUE);
891 nm_manager_get_state (NMManager *manager)
893 g_return_val_if_fail (NM_IS_MANAGER (manager), NM_STATE_UNKNOWN);
895 return NM_MANAGER_GET_PRIVATE (manager)->state;
898 /***************************/
901 find_parent_device_for_connection (NMManager *self, NMConnection *connection, NMDeviceFactory *cached_factory)
903 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
904 NMDeviceFactory *factory;
905 const char *parent_name = NULL;
906 NMSettingsConnection *parent_connection;
907 NMDevice *parent, *first_compatible = NULL;
910 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
912 if (!cached_factory) {
913 factory = nm_device_factory_manager_find_factory_for_connection (connection);
917 factory = cached_factory;
919 parent_name = nm_device_factory_get_connection_parent (factory, connection);
923 /* Try as an interface name of a parent device */
924 parent = find_device_by_iface (self, parent_name, NULL, NULL);
928 /* Maybe a hardware address */
929 parent = find_device_by_hw_addr (self, parent_name);
933 /* Maybe a connection UUID */
934 parent_connection = nm_settings_get_connection_by_uuid (priv->settings, parent_name);
935 if (!parent_connection)
938 /* Check if the parent connection is currently activated or is comaptible
939 * with some known device.
941 for (iter = priv->devices; iter; iter = iter->next) {
942 NMDevice *candidate = iter->data;
944 if (nm_device_get_settings_connection (candidate) == parent_connection)
947 if ( !first_compatible
948 && nm_device_check_connection_compatible (candidate, NM_CONNECTION (parent_connection)))
949 first_compatible = candidate;
952 return first_compatible;
956 * nm_manager_get_connection_iface:
957 * @self: the #NMManager
958 * @connection: the #NMConnection to get the interface for
959 * @out_parent: on success, the parent device if any
960 * @error: an error if determining the virtual interface name failed
962 * Given @connection, returns the interface name that the connection
963 * would need to use when activated. %NULL is returned if the name
964 * is not specified in connection or a the name for a virtual device
965 * could not be generated.
967 * Returns: the expected interface name (caller takes ownership), or %NULL
970 nm_manager_get_connection_iface (NMManager *self,
971 NMConnection *connection,
972 NMDevice **out_parent,
975 NMDeviceFactory *factory;
977 NMDevice *parent = NULL;
982 factory = nm_device_factory_manager_find_factory_for_connection (connection);
986 NM_MANAGER_ERROR_FAILED,
987 "NetworkManager plugin for '%s' unavailable",
988 nm_connection_get_connection_type (connection));
993 && !NM_DEVICE_FACTORY_GET_INTERFACE (factory)->get_connection_iface) {
994 /* optimization. Shortcut lookup of the partent device. */
995 iface = g_strdup (nm_connection_get_interface_name (connection));
999 NM_MANAGER_ERROR_FAILED,
1000 "failed to determine interface name: error determine name for %s",
1001 nm_connection_get_connection_type (connection));
1006 parent = find_parent_device_for_connection (self, connection, factory);
1007 iface = nm_device_factory_get_connection_iface (factory,
1009 parent ? nm_device_get_ip_iface (parent) : NULL,
1015 *out_parent = parent;
1020 * system_create_virtual_device:
1021 * @self: the #NMManager
1022 * @connection: the connection which might require a virtual device
1024 * If @connection requires a virtual device and one does not yet exist for it,
1025 * creates that device.
1027 * Returns: A #NMDevice that was just realized; %NULL if none
1030 system_create_virtual_device (NMManager *self, NMConnection *connection)
1032 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1033 NMDeviceFactory *factory;
1034 gs_free_slist GSList *connections = NULL;
1036 gs_free char *iface = NULL;
1037 NMDevice *device = NULL, *parent = NULL;
1038 GError *error = NULL;
1040 g_return_val_if_fail (NM_IS_MANAGER (self), NULL);
1041 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1043 iface = nm_manager_get_connection_iface (self, connection, &parent, &error);
1045 _LOGW (LOGD_DEVICE, "(%s) can't get a name of a virtual device: %s",
1046 nm_connection_get_id (connection), error->message);
1047 g_error_free (error);
1051 /* See if there's a device that is already compatible with this connection */
1052 for (iter = priv->devices; iter; iter = g_slist_next (iter)) {
1053 NMDevice *candidate = iter->data;
1055 if (nm_device_check_connection_compatible (candidate, connection)) {
1056 if (nm_device_is_real (candidate)) {
1057 _LOGD (LOGD_DEVICE, "(%s) already created virtual interface name %s",
1058 nm_connection_get_id (connection), iface);
1068 /* No matching device found. Proceed creating a new one. */
1070 factory = nm_device_factory_manager_find_factory_for_connection (connection);
1072 _LOGE (LOGD_DEVICE, "(%s:%s) NetworkManager plugin for '%s' unavailable",
1073 nm_connection_get_id (connection), iface,
1074 nm_connection_get_connection_type (connection));
1078 device = nm_device_factory_create_device (factory, iface, NULL, connection, NULL, &error);
1080 _LOGW (LOGD_DEVICE, "(%s) factory can't create the device: %s",
1081 nm_connection_get_id (connection), error->message);
1082 g_error_free (error);
1086 if (!add_device (self, device, &error)) {
1087 _LOGW (LOGD_DEVICE, "(%s) can't register the device with manager: %s",
1088 nm_connection_get_id (connection), error->message);
1089 g_error_free (error);
1090 g_object_unref (device);
1094 /* Add device takes a reference that NMManager still owns, so it's
1095 * safe to unref here and still return @device.
1097 g_object_unref (device);
1100 /* Create backing resources if the device has any autoconnect connections */
1101 connections = nm_settings_get_connections (priv->settings);
1102 for (iter = connections; iter; iter = g_slist_next (iter)) {
1103 NMConnection *candidate = iter->data;
1104 NMSettingConnection *s_con;
1106 if (!nm_device_check_connection_compatible (device, candidate))
1109 s_con = nm_connection_get_setting_connection (candidate);
1111 if (!nm_setting_connection_get_autoconnect (s_con))
1114 /* Create any backing resources the device needs */
1115 if (!nm_device_create_and_realize (device, connection, parent, &error)) {
1116 _LOGW (LOGD_DEVICE, "(%s) couldn't create the device: %s",
1117 nm_connection_get_id (connection), error->message);
1118 g_error_free (error);
1119 remove_device (self, device, FALSE, TRUE);
1129 retry_connections_for_parent_device (NMManager *self, NMDevice *device)
1131 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1132 GSList *connections, *iter;
1134 g_return_if_fail (device);
1136 connections = nm_settings_get_connections (priv->settings);
1137 for (iter = connections; iter; iter = g_slist_next (iter)) {
1138 NMConnection *candidate = iter->data;
1139 gs_free_error GError *error = NULL;
1140 gs_free char *ifname = NULL;
1143 parent = find_parent_device_for_connection (self, candidate, NULL);
1144 if (parent == device) {
1145 /* Only try to activate devices that don't already exist */
1146 ifname = nm_manager_get_connection_iface (self, candidate, &parent, &error);
1148 if (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, ifname))
1149 connection_changed (priv->settings, candidate, self);
1154 g_slist_free (connections);
1158 connection_changed (NMSettings *settings,
1159 NMConnection *connection,
1164 if (!nm_connection_is_virtual (connection))
1167 device = system_create_virtual_device (manager, connection);
1171 /* Maybe the device that was created was needed by some other
1172 * connection's device (parent of a VLAN). Let the connections
1173 * can use the newly created device as a parent know. */
1174 retry_connections_for_parent_device (manager, device);
1178 connection_removed (NMSettings *settings,
1179 NMSettingsConnection *connection,
1183 * Do not delete existing virtual devices to keep connectivity up.
1184 * Virtual devices are reused when NetworkManager is restarted.
1189 system_unmanaged_devices_changed_cb (NMSettings *settings,
1193 NMManager *self = NM_MANAGER (user_data);
1194 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1195 const GSList *unmanaged_specs, *iter;
1197 unmanaged_specs = nm_settings_get_unmanaged_specs (priv->settings);
1198 for (iter = priv->devices; iter; iter = g_slist_next (iter))
1199 nm_device_set_unmanaged_by_user_config (NM_DEVICE (iter->data), unmanaged_specs);
1203 system_hostname_changed_cb (NMSettings *settings,
1207 NMManager *self = NM_MANAGER (user_data);
1208 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1211 hostname = nm_settings_get_hostname (priv->settings);
1213 /* nm_settings_get_hostname() does not return an empty hostname. */
1214 nm_assert (!hostname || *hostname);
1216 if (!hostname && !priv->hostname)
1218 if (hostname && priv->hostname && !strcmp (hostname, priv->hostname)) {
1223 /* realloc, to free possibly trailing data after NUL. */
1225 hostname = g_realloc (hostname, strlen (hostname) + 1);
1227 g_free (priv->hostname);
1228 priv->hostname = hostname;
1229 g_object_notify (G_OBJECT (self), NM_MANAGER_HOSTNAME);
1231 nm_dhcp_manager_set_default_hostname (nm_dhcp_manager_get (), priv->hostname);
1234 /*******************************************************************/
1235 /* General NMManager stuff */
1236 /*******************************************************************/
1238 /* Store value into key-file; supported types: boolean, int, string */
1240 write_value_to_state_file (const char *filename,
1250 gboolean ret = FALSE;
1252 g_return_val_if_fail (filename != NULL, FALSE);
1253 g_return_val_if_fail (group != NULL, FALSE);
1254 g_return_val_if_fail (key != NULL, FALSE);
1255 g_return_val_if_fail (value_type == G_TYPE_BOOLEAN ||
1256 value_type == G_TYPE_INT ||
1257 value_type == G_TYPE_STRING,
1260 key_file = g_key_file_new ();
1262 g_key_file_set_list_separator (key_file, ',');
1263 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_KEEP_COMMENTS, NULL);
1264 switch (value_type) {
1265 case G_TYPE_BOOLEAN:
1266 g_key_file_set_boolean (key_file, group, key, *((gboolean *) value));
1269 g_key_file_set_integer (key_file, group, key, *((gint *) value));
1272 g_key_file_set_string (key_file, group, key, *((const gchar **) value));
1276 data = g_key_file_to_data (key_file, &len, NULL);
1278 ret = g_file_set_contents (filename, data, len, error);
1281 g_key_file_free (key_file);
1287 radio_enabled_for_rstate (RadioState *rstate, gboolean check_changeable)
1291 enabled = rstate->user_enabled && rstate->hw_enabled;
1292 if (check_changeable)
1293 enabled &= rstate->sw_enabled;
1298 radio_enabled_for_type (NMManager *self, RfKillType rtype, gboolean check_changeable)
1300 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1302 return radio_enabled_for_rstate (&priv->radio_states[rtype], check_changeable);
1306 manager_update_radio_enabled (NMManager *self,
1310 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1313 /* Do nothing for radio types not yet implemented */
1317 g_object_notify (G_OBJECT (self), rstate->prop);
1319 /* Don't touch devices if asleep/networking disabled */
1320 if (manager_sleeping (self))
1323 /* enable/disable wireless devices as required */
1324 for (iter = priv->devices; iter; iter = iter->next) {
1325 NMDevice *device = NM_DEVICE (iter->data);
1327 if (nm_device_get_rfkill_type (device) == rstate->rtype) {
1328 _LOGD (LOGD_RFKILL, "(%s): setting radio %s",
1329 nm_device_get_iface (device),
1330 enabled ? "enabled" : "disabled");
1331 nm_device_set_enabled (device, enabled);
1337 update_rstate_from_rfkill (NMRfkillManager *rfkill_mgr, RadioState *rstate)
1339 switch (nm_rfkill_manager_get_rfkill_state (rfkill_mgr, rstate->rtype)) {
1340 case RFKILL_UNBLOCKED:
1341 rstate->sw_enabled = TRUE;
1342 rstate->hw_enabled = TRUE;
1344 case RFKILL_SOFT_BLOCKED:
1345 rstate->sw_enabled = FALSE;
1346 rstate->hw_enabled = TRUE;
1348 case RFKILL_HARD_BLOCKED:
1349 rstate->sw_enabled = FALSE;
1350 rstate->hw_enabled = FALSE;
1353 g_warn_if_reached ();
1359 manager_rfkill_update_one_type (NMManager *self,
1363 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1364 gboolean old_enabled, new_enabled, old_rfkilled, new_rfkilled, old_hwe;
1366 old_enabled = radio_enabled_for_rstate (rstate, TRUE);
1367 old_rfkilled = rstate->hw_enabled && rstate->sw_enabled;
1368 old_hwe = rstate->hw_enabled;
1370 /* recheck kernel rfkill state */
1371 update_rstate_from_rfkill (priv->rfkill_mgr, rstate);
1373 /* Print out all states affecting device enablement */
1375 _LOGD (LOGD_RFKILL, "%s hw-enabled %d sw-enabled %d",
1376 rstate->desc, rstate->hw_enabled, rstate->sw_enabled);
1379 /* Log new killswitch state */
1380 new_rfkilled = rstate->hw_enabled && rstate->sw_enabled;
1381 if (old_rfkilled != new_rfkilled) {
1382 _LOGI (LOGD_RFKILL, "%s now %s by radio killswitch",
1384 new_rfkilled ? "enabled" : "disabled");
1387 /* Send out property changed signal for HW enabled */
1388 if (rstate->hw_enabled != old_hwe) {
1389 if (rstate->hw_prop)
1390 g_object_notify (G_OBJECT (self), rstate->hw_prop);
1393 /* And finally update the actual device radio state itself; respect the
1394 * daemon state here because this is never called from user-triggered
1395 * radio changes and we only want to ignore the daemon enabled state when
1396 * handling user radio change requests.
1398 new_enabled = radio_enabled_for_rstate (rstate, TRUE);
1399 if (new_enabled != old_enabled)
1400 manager_update_radio_enabled (self, rstate, new_enabled);
1404 nm_manager_rfkill_update (NMManager *self, RfKillType rtype)
1406 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1409 if (rtype != RFKILL_TYPE_UNKNOWN)
1410 manager_rfkill_update_one_type (self, &priv->radio_states[rtype], rtype);
1412 /* Otherwise sync all radio types */
1413 for (i = 0; i < RFKILL_TYPE_MAX; i++)
1414 manager_rfkill_update_one_type (self, &priv->radio_states[i], i);
1419 device_auth_done_cb (NMAuthChain *chain,
1421 GDBusMethodInvocation *context,
1424 NMManager *self = NM_MANAGER (user_data);
1425 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1426 GError *error = NULL;
1427 NMAuthCallResult result;
1429 const char *permission;
1430 NMDeviceAuthRequestFunc callback;
1431 NMAuthSubject *subject;
1435 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
1437 permission = nm_auth_chain_get_data (chain, "requested-permission");
1438 g_assert (permission);
1439 callback = nm_auth_chain_get_data (chain, "callback");
1440 g_assert (callback);
1441 device = nm_auth_chain_get_data (chain, "device");
1444 result = nm_auth_chain_get_result (chain, permission);
1445 subject = nm_auth_chain_get_subject (chain);
1448 /* translate the auth error into a manager permission denied error */
1449 _LOGD (LOGD_CORE, "%s request failed: %s", permission, auth_error->message);
1450 error = g_error_new (NM_MANAGER_ERROR,
1451 NM_MANAGER_ERROR_PERMISSION_DENIED,
1452 "%s request failed: %s",
1453 permission, auth_error->message);
1454 } else if (result != NM_AUTH_CALL_RESULT_YES) {
1455 _LOGD (LOGD_CORE, "%s request failed: not authorized", permission);
1456 error = g_error_new (NM_MANAGER_ERROR,
1457 NM_MANAGER_ERROR_PERMISSION_DENIED,
1458 "%s request failed: not authorized",
1462 g_assert (error || (result == NM_AUTH_CALL_RESULT_YES));
1468 nm_auth_chain_get_data (chain, "user-data"));
1470 g_clear_error (&error);
1471 nm_auth_chain_unref (chain);
1475 device_auth_request_cb (NMDevice *device,
1476 GDBusMethodInvocation *context,
1477 NMConnection *connection,
1478 const char *permission,
1479 gboolean allow_interaction,
1480 NMDeviceAuthRequestFunc callback,
1484 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1485 GError *error = NULL;
1486 NMAuthSubject *subject = NULL;
1487 char *error_desc = NULL;
1490 /* Validate the caller */
1491 subject = nm_auth_subject_new_unix_process_from_context (context);
1493 error = g_error_new_literal (NM_MANAGER_ERROR,
1494 NM_MANAGER_ERROR_PERMISSION_DENIED,
1495 "Failed to get request UID.");
1499 /* Ensure the subject has permissions for this connection */
1500 if (connection && !nm_auth_is_subject_in_acl (connection,
1503 error = g_error_new_literal (NM_MANAGER_ERROR,
1504 NM_MANAGER_ERROR_PERMISSION_DENIED,
1506 g_free (error_desc);
1510 /* Validate the request */
1511 chain = nm_auth_chain_new_subject (subject, context, device_auth_done_cb, self);
1513 error = g_error_new_literal (NM_MANAGER_ERROR,
1514 NM_MANAGER_ERROR_PERMISSION_DENIED,
1515 "Unable to authenticate request.");
1519 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
1520 nm_auth_chain_set_data (chain, "device", g_object_ref (device), g_object_unref);
1521 nm_auth_chain_set_data (chain, "requested-permission", g_strdup (permission), g_free);
1522 nm_auth_chain_set_data (chain, "callback", callback, NULL);
1523 nm_auth_chain_set_data (chain, "user-data", user_data, NULL);
1524 nm_auth_chain_add_call (chain, permission, allow_interaction);
1528 callback (device, context, subject, error, user_data);
1530 g_clear_object (&subject);
1531 g_clear_error (&error);
1535 match_connection_filter (NMConnection *connection, gpointer user_data)
1537 if (nm_settings_connection_get_nm_generated_assumed (NM_SETTINGS_CONNECTION (connection)))
1540 return nm_device_check_connection_compatible (NM_DEVICE (user_data), connection);
1544 * get_existing_connection:
1545 * @manager: #NMManager instance
1546 * @device: #NMDevice instance
1547 * @out_generated: (allow-none): return TRUE, if the connection was generated.
1549 * Returns: a #NMSettingsConnection to be assumed by the device, or %NULL if
1550 * the device does not support assuming existing connections.
1552 static NMSettingsConnection *
1553 get_existing_connection (NMManager *self, NMDevice *device, gboolean *out_generated)
1555 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1556 gs_free_slist GSList *connections = nm_manager_get_activatable_connections (self);
1557 NMConnection *connection = NULL;
1558 NMSettingsConnection *matched;
1559 NMSettingsConnection *added = NULL;
1560 GError *error = NULL;
1561 NMDevice *master = NULL;
1562 int ifindex = nm_device_get_ifindex (device);
1565 *out_generated = FALSE;
1567 nm_device_capture_initial_config (device);
1570 int master_ifindex = nm_platform_link_get_master (NM_PLATFORM_GET, ifindex);
1572 if (master_ifindex) {
1573 master = nm_manager_get_device_by_ifindex (self, master_ifindex);
1575 _LOGD (LOGD_DEVICE, "(%s): cannot generate connection for slave before its master (%s/%d)",
1576 nm_device_get_iface (device), nm_platform_link_get_name (NM_PLATFORM_GET, master_ifindex), master_ifindex);
1579 if (!nm_device_get_act_request (master)) {
1580 _LOGD (LOGD_DEVICE, "(%s): cannot generate connection for slave before master %s activates",
1581 nm_device_get_iface (device), nm_device_get_iface (master));
1587 /* The core of the API is nm_device_generate_connection() function and
1588 * update_connection() virtual method and the convenient connection_type
1589 * class attribute. Subclasses supporting the new API must have
1590 * update_connection() implemented, otherwise nm_device_generate_connection()
1593 connection = nm_device_generate_connection (device, master);
1597 /* Now we need to compare the generated connection to each configured
1598 * connection. The comparison function is the heart of the connection
1599 * assumption implementation and it must compare the connections very
1600 * carefully to sort out various corner cases. Also, the comparison is
1601 * not entirely symmetric.
1603 * When no configured connection matches the generated connection, we keep
1604 * the generated connection instead.
1606 connections = g_slist_reverse (g_slist_sort (connections, nm_settings_sort_connections));
1607 matched = NM_SETTINGS_CONNECTION (nm_utils_match_connection (connections,
1609 nm_device_has_carrier (device),
1610 nm_device_get_ip4_route_metric (device),
1611 nm_device_get_ip6_route_metric (device),
1612 match_connection_filter,
1615 _LOGI (LOGD_DEVICE, "(%s): found matching connection '%s'",
1616 nm_device_get_iface (device),
1617 nm_settings_connection_get_id (matched));
1618 g_object_unref (connection);
1622 _LOGD (LOGD_DEVICE, "(%s): generated connection '%s'",
1623 nm_device_get_iface (device),
1624 nm_connection_get_id (connection));
1626 added = nm_settings_add_connection (priv->settings, connection, FALSE, &error);
1628 nm_settings_connection_set_flags (NM_SETTINGS_CONNECTION (added),
1629 NM_SETTINGS_CONNECTION_FLAGS_NM_GENERATED |
1630 NM_SETTINGS_CONNECTION_FLAGS_NM_GENERATED_ASSUMED,
1633 *out_generated = TRUE;
1635 _LOGW (LOGD_SETTINGS, "(%s) Couldn't save generated connection '%s': %s",
1636 nm_device_get_iface (device),
1637 nm_connection_get_id (connection),
1639 g_clear_error (&error);
1641 g_object_unref (connection);
1643 return added ? added : NULL;
1647 assume_connection (NMManager *self, NMDevice *device, NMSettingsConnection *connection)
1649 NMActiveConnection *active, *master_ac;
1650 NMAuthSubject *subject;
1651 GError *error = NULL;
1653 _LOGD (LOGD_DEVICE, "(%s): will attempt to assume connection",
1654 nm_device_get_iface (device));
1656 /* Move device to DISCONNECTED to activate the connection */
1657 if (nm_device_get_state (device) == NM_DEVICE_STATE_UNAVAILABLE) {
1658 nm_device_state_changed (device,
1659 NM_DEVICE_STATE_DISCONNECTED,
1660 NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED);
1662 g_return_val_if_fail (nm_device_get_state (device) >= NM_DEVICE_STATE_DISCONNECTED, FALSE);
1664 subject = nm_auth_subject_new_internal ();
1665 active = _new_active_connection (self, NM_CONNECTION (connection), NULL, device, subject, &error);
1666 g_object_unref (subject);
1669 _LOGW (LOGD_DEVICE, "assumed connection %s failed to activate: %s",
1670 nm_connection_get_path (NM_CONNECTION (connection)),
1672 g_error_free (error);
1676 /* If the device is a slave or VLAN, find the master ActiveConnection */
1678 if (find_master (self, NM_CONNECTION (connection), device, NULL, NULL, &master_ac, NULL) && master_ac)
1679 nm_active_connection_set_master (active, master_ac);
1681 nm_active_connection_set_assumed (active, TRUE);
1682 nm_exported_object_export (NM_EXPORTED_OBJECT (active));
1683 active_connection_add (self, active);
1684 nm_device_queue_activation (device, NM_ACT_REQUEST (active));
1685 g_object_unref (active);
1691 recheck_assume_connection (NMManager *self, NMDevice *device)
1693 NMSettingsConnection *connection;
1694 gboolean was_unmanaged = FALSE, success, generated = FALSE;
1695 NMDeviceState state;
1697 g_return_val_if_fail (NM_IS_MANAGER (self), FALSE);
1698 g_return_val_if_fail (NM_IS_DEVICE (device), FALSE);
1700 if (nm_device_get_is_nm_owned (device))
1703 if (!nm_device_get_managed (device, FALSE))
1706 state = nm_device_get_state (device);
1707 if (state > NM_DEVICE_STATE_DISCONNECTED)
1710 connection = get_existing_connection (self, device, &generated);
1712 _LOGD (LOGD_DEVICE, "(%s): can't assume; no connection",
1713 nm_device_get_iface (device));
1717 if (state == NM_DEVICE_STATE_UNMANAGED) {
1718 was_unmanaged = TRUE;
1719 nm_device_state_changed (device,
1720 NM_DEVICE_STATE_UNAVAILABLE,
1721 NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED);
1724 success = assume_connection (self, device, connection);
1726 if (was_unmanaged) {
1727 nm_device_state_changed (device,
1728 NM_DEVICE_STATE_UNAVAILABLE,
1729 NM_DEVICE_STATE_REASON_CONFIG_FAILED);
1733 _LOGD (LOGD_DEVICE, "(%s): connection assumption failed. Deleting generated connection",
1734 nm_device_get_iface (device));
1736 nm_settings_connection_delete (connection, NULL, NULL);
1744 recheck_assume_connection_cb (NMDevice *device, gpointer user_data)
1746 recheck_assume_connection (user_data, device);
1750 device_ip_iface_changed (NMDevice *device,
1754 const char *ip_iface = nm_device_get_ip_iface (device);
1757 /* Remove NMDevice objects that are actually child devices of others,
1758 * when the other device finally knows its IP interface name. For example,
1759 * remove the PPP interface that's a child of a WWAN device, since it's
1760 * not really a standalone NMDevice.
1762 for (iter = NM_MANAGER_GET_PRIVATE (self)->devices; iter; iter = iter->next) {
1763 NMDevice *candidate = NM_DEVICE (iter->data);
1765 if ( candidate != device
1766 && g_strcmp0 (nm_device_get_iface (candidate), ip_iface) == 0
1767 && nm_device_is_real (candidate)) {
1768 remove_device (self, candidate, FALSE, FALSE);
1775 device_iface_changed (NMDevice *device,
1779 /* Virtual connections may refer to the new device name as
1780 * parent device, retry to activate them.
1782 retry_connections_for_parent_device (self, device);
1787 device_realized (NMDevice *device,
1791 /* Emit D-Bus signals */
1792 g_signal_emit (self, signals[DEVICE_ADDED], 0, device);
1793 g_object_notify (G_OBJECT (self), NM_MANAGER_DEVICES);
1797 _device_realize_finish (NMManager *self, NMDevice *device, const NMPlatformLink *plink)
1799 g_return_if_fail (NM_IS_MANAGER (self));
1800 g_return_if_fail (NM_IS_DEVICE (device));
1802 nm_device_realize_finish (device, plink);
1804 if (!nm_device_get_managed (device, FALSE))
1807 if (recheck_assume_connection (self, device))
1810 /* if we failed to assume a connection for the managed device, but the device
1811 * is still unavailable. Set UNAVAILABLE state again, this time with NOW_MANAGED. */
1812 nm_device_state_changed (device,
1813 NM_DEVICE_STATE_UNAVAILABLE,
1814 NM_DEVICE_STATE_REASON_NOW_MANAGED);
1815 nm_device_emit_recheck_auto_activate (device);
1820 * @self: the #NMManager
1821 * @device: the #NMDevice to add
1822 * @error: (out): the #GError
1824 * If successful, this function will increase the references count of @device.
1825 * Callers should decrease the reference count.
1828 add_device (NMManager *self, NMDevice *device, GError **error)
1830 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1831 const char *iface, *type_desc;
1833 GSList *iter, *remove = NULL;
1835 const char *dbus_path;
1838 ifindex = nm_device_get_ifindex (device);
1839 if (ifindex > 0 && nm_manager_get_device_by_ifindex (self, ifindex)) {
1840 g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED,
1841 "A device with ifindex %d already exists", ifindex);
1845 /* Remove existing devices owned by the new device; eg remove ethernet
1846 * ports that are owned by a WWAN modem, since udev may announce them
1847 * before the modem is fully discovered.
1849 * FIXME: use parent/child device relationships instead of removing
1850 * the child NMDevice entirely
1852 for (iter = priv->devices; iter; iter = iter->next) {
1853 NMDevice *candidate = iter->data;
1855 iface = nm_device_get_ip_iface (candidate);
1856 if (nm_device_is_real (candidate) && nm_device_owns_iface (device, iface))
1857 remove = g_slist_prepend (remove, candidate);
1859 for (iter = remove; iter; iter = iter->next)
1860 remove_device (self, NM_DEVICE (iter->data), FALSE, FALSE);
1861 g_slist_free (remove);
1863 priv->devices = g_slist_append (priv->devices, g_object_ref (device));
1865 g_signal_connect (device, NM_DEVICE_STATE_CHANGED,
1866 G_CALLBACK (manager_device_state_changed),
1869 g_signal_connect (device, NM_DEVICE_AUTH_REQUEST,
1870 G_CALLBACK (device_auth_request_cb),
1873 g_signal_connect (device, NM_DEVICE_REMOVED,
1874 G_CALLBACK (device_removed_cb),
1877 g_signal_connect (device, NM_DEVICE_RECHECK_ASSUME,
1878 G_CALLBACK (recheck_assume_connection_cb),
1881 g_signal_connect (device, "notify::" NM_DEVICE_IP_IFACE,
1882 G_CALLBACK (device_ip_iface_changed),
1885 g_signal_connect (device, "notify::" NM_DEVICE_IFACE,
1886 G_CALLBACK (device_iface_changed),
1889 g_signal_connect (device, "notify::" NM_DEVICE_REAL,
1890 G_CALLBACK (device_realized),
1893 if (priv->startup) {
1894 g_signal_connect (device, "notify::" NM_DEVICE_HAS_PENDING_ACTION,
1895 G_CALLBACK (device_has_pending_action_changed),
1899 /* Update global rfkill state for this device type with the device's
1900 * rfkill state, and then set this device's rfkill state based on the
1903 rtype = nm_device_get_rfkill_type (device);
1904 if (rtype != RFKILL_TYPE_UNKNOWN) {
1905 nm_manager_rfkill_update (self, rtype);
1906 nm_device_set_enabled (device, radio_enabled_for_type (self, rtype, TRUE));
1909 iface = nm_device_get_iface (device);
1911 type_desc = nm_device_get_type_desc (device);
1912 g_assert (type_desc);
1914 nm_device_set_unmanaged_by_user_config (device, nm_settings_get_unmanaged_specs (priv->settings));
1916 nm_device_set_unmanaged_flags (device,
1917 NM_UNMANAGED_SLEEPING,
1918 manager_sleeping (self));
1920 dbus_path = nm_exported_object_export (NM_EXPORTED_OBJECT (device));
1921 _LOGI (LOGD_DEVICE, "(%s): new %s device (%s)", iface, type_desc, dbus_path);
1923 nm_settings_device_added (priv->settings, device);
1924 g_signal_emit (self, signals[INTERNAL_DEVICE_ADDED], 0, device);
1925 g_object_notify (G_OBJECT (self), NM_MANAGER_ALL_DEVICES);
1927 for (iter = priv->devices; iter; iter = iter->next) {
1928 NMDevice *d = iter->data;
1931 nm_device_notify_new_device_added (d, device);
1934 /* Virtual connections may refer to the new device as
1935 * parent device, retry to activate them.
1937 retry_connections_for_parent_device (self, device);
1942 /*******************************************************************/
1945 factory_device_added_cb (NMDeviceFactory *factory,
1949 NMManager *self = user_data;
1950 GError *error = NULL;
1952 g_return_if_fail (NM_IS_MANAGER (self));
1954 if (nm_device_realize_start (device, NULL, NULL, &error)) {
1955 add_device (self, device, NULL);
1956 _device_realize_finish (self, device, NULL);
1958 _LOGW (LOGD_DEVICE, "(%s): failed to realize device: %s",
1959 nm_device_get_iface (device), error->message);
1960 g_error_free (error);
1965 factory_component_added_cb (NMDeviceFactory *factory,
1971 g_return_val_if_fail (NM_IS_MANAGER (user_data), FALSE);
1973 for (iter = NM_MANAGER_GET_PRIVATE (user_data)->devices; iter; iter = iter->next) {
1974 if (nm_device_notify_component_added ((NMDevice *) iter->data, component))
1981 _register_device_factory (NMDeviceFactory *factory, gpointer user_data)
1983 NMManager *self = NM_MANAGER (user_data);
1985 g_signal_connect (factory,
1986 NM_DEVICE_FACTORY_DEVICE_ADDED,
1987 G_CALLBACK (factory_device_added_cb),
1989 g_signal_connect (factory,
1990 NM_DEVICE_FACTORY_COMPONENT_ADDED,
1991 G_CALLBACK (factory_component_added_cb),
1995 /*******************************************************************/
1998 platform_link_added (NMManager *self,
2000 const NMPlatformLink *plink)
2002 NMDeviceFactory *factory;
2003 NMDevice *device = NULL;
2004 GError *error = NULL;
2005 gboolean nm_plugin_missing = FALSE;
2008 g_return_if_fail (ifindex > 0);
2010 if (nm_manager_get_device_by_ifindex (self, ifindex))
2013 /* Let unrealized devices try to realize themselves with the link */
2014 for (iter = NM_MANAGER_GET_PRIVATE (self)->devices; iter; iter = iter->next) {
2015 NMDevice *candidate = iter->data;
2016 gboolean compatible = TRUE;
2018 if (strcmp (nm_device_get_iface (candidate), plink->name))
2021 if (nm_device_is_real (candidate)) {
2022 /* Ignore the link added event since there's already a realized
2023 * device with the link's name.
2026 } else if (nm_device_realize_start (candidate, plink, &compatible, &error)) {
2028 _device_realize_finish (self, candidate, plink);
2032 _LOGD (LOGD_DEVICE, "(%s): failed to realize from plink: '%s'",
2033 plink->name, error->message);
2034 g_clear_error (&error);
2036 /* Try next unrealized device */
2039 /* Try registered device factories */
2040 factory = nm_device_factory_manager_find_factory_for_link_type (plink->type);
2042 gboolean ignore = FALSE;
2044 device = nm_device_factory_create_device (factory, plink->name, plink, NULL, &ignore, &error);
2047 _LOGW (LOGD_HW, "%s: factory failed to create device: %s",
2048 plink->name, error->message);
2049 g_clear_error (&error);
2055 if (device == NULL) {
2056 switch (plink->type) {
2057 case NM_LINK_TYPE_WWAN_ETHERNET:
2058 case NM_LINK_TYPE_BNEP:
2059 case NM_LINK_TYPE_OLPC_MESH:
2060 case NM_LINK_TYPE_TEAM:
2061 case NM_LINK_TYPE_WIFI:
2062 _LOGI (LOGD_HW, "(%s): '%s' plugin not available; creating generic device",
2063 plink->name, nm_link_type_to_string (plink->type));
2064 nm_plugin_missing = TRUE;
2067 device = nm_device_generic_new (plink);
2073 if (nm_plugin_missing)
2074 nm_device_set_nm_plugin_missing (device, TRUE);
2075 if (nm_device_realize_start (device, plink, NULL, &error)) {
2076 add_device (self, device, NULL);
2077 _device_realize_finish (self, device, plink);
2079 _LOGW (LOGD_DEVICE, "%s: failed to realize device: %s",
2080 plink->name, error->message);
2081 g_clear_error (&error);
2083 g_object_unref (device);
2090 } PlatformLinkCbData;
2093 _platform_link_cb_idle (PlatformLinkCbData *data)
2095 NMManager *self = data->self;
2096 const NMPlatformLink *l;
2101 g_object_remove_weak_pointer (G_OBJECT (self), (gpointer *) &data->self);
2103 l = nm_platform_link_get (NM_PLATFORM_GET, data->ifindex);
2105 NMPlatformLink pllink;
2107 pllink = *l; /* make a copy of the link instance */
2108 platform_link_added (self, data->ifindex, &pllink);
2111 GError *error = NULL;
2113 device = nm_manager_get_device_by_ifindex (self, data->ifindex);
2115 if (nm_device_is_software (device)) {
2116 /* Our software devices stick around until their connection is removed */
2117 if (!nm_device_unrealize (device, FALSE, &error)) {
2118 _LOGW (LOGD_DEVICE, "(%s): failed to unrealize: %s",
2119 nm_device_get_iface (device),
2121 g_clear_error (&error);
2122 remove_device (self, device, FALSE, TRUE);
2125 /* Hardware and external devices always get removed when their kernel link is gone */
2126 remove_device (self, device, FALSE, TRUE);
2132 g_slice_free (PlatformLinkCbData, data);
2133 return G_SOURCE_REMOVE;
2137 platform_link_cb (NMPlatform *platform,
2138 NMPObjectType obj_type,
2140 NMPlatformLink *plink,
2141 NMPlatformSignalChangeType change_type,
2144 PlatformLinkCbData *data;
2146 switch (change_type) {
2147 case NM_PLATFORM_SIGNAL_ADDED:
2148 case NM_PLATFORM_SIGNAL_REMOVED:
2149 data = g_slice_new (PlatformLinkCbData);
2150 data->self = NM_MANAGER (user_data);
2151 data->ifindex = ifindex;
2152 g_object_add_weak_pointer (G_OBJECT (data->self), (gpointer *) &data->self);
2153 g_idle_add ((GSourceFunc) _platform_link_cb_idle, data);
2161 platform_query_devices (NMManager *self)
2163 GArray *links_array;
2164 NMPlatformLink *links;
2167 links_array = nm_platform_link_get_all (NM_PLATFORM_GET);
2168 links = (NMPlatformLink *) links_array->data;
2169 for (i = 0; i < links_array->len; i++)
2170 platform_link_added (self, links[i].ifindex, &links[i]);
2172 g_array_unref (links_array);
2176 rfkill_manager_rfkill_changed_cb (NMRfkillManager *rfkill_mgr,
2178 RfKillState udev_state,
2181 nm_manager_rfkill_update (NM_MANAGER (user_data), rtype);
2185 nm_manager_get_devices (NMManager *manager)
2187 g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);
2189 return NM_MANAGER_GET_PRIVATE (manager)->devices;
2193 nm_manager_get_connection_device (NMManager *self,
2194 NMConnection *connection)
2196 NMActiveConnection *ac = find_ac_for_connection (self, connection);
2200 return nm_active_connection_get_device (ac);
2204 nm_manager_get_best_device_for_connection (NMManager *self,
2205 NMConnection *connection,
2206 gboolean for_user_request)
2208 const GSList *devices, *iter;
2209 NMDevice *act_device = nm_manager_get_connection_device (self, connection);
2210 NMDeviceCheckConAvailableFlags flags;
2215 flags = for_user_request ? NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST : NM_DEVICE_CHECK_CON_AVAILABLE_NONE;
2217 /* Pick the first device that's compatible with the connection. */
2218 devices = nm_manager_get_devices (self);
2219 for (iter = devices; iter; iter = g_slist_next (iter)) {
2220 NMDevice *device = NM_DEVICE (iter->data);
2222 if (nm_device_check_connection_available (device, connection, flags, NULL))
2231 _get_devices (NMManager *self,
2232 GDBusMethodInvocation *context,
2233 gboolean all_devices)
2235 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
2236 gs_free const char **paths = NULL;
2240 paths = g_new (const char *, g_slist_length (priv->devices) + 1);
2242 for (i = 0, iter = priv->devices; iter; iter = iter->next) {
2245 path = nm_exported_object_get_path (NM_EXPORTED_OBJECT (iter->data));
2247 && (all_devices || nm_device_is_real (iter->data)))
2252 g_dbus_method_invocation_return_value (context,
2253 g_variant_new ("(^ao)", (char **) paths));
2257 impl_manager_get_devices (NMManager *self,
2258 GDBusMethodInvocation *context)
2260 _get_devices (self, context, FALSE);
2264 impl_manager_get_all_devices (NMManager *self,
2265 GDBusMethodInvocation *context)
2267 _get_devices (self, context, TRUE);
2271 impl_manager_get_device_by_ip_iface (NMManager *self,
2272 GDBusMethodInvocation *context,
2276 const char *path = NULL;
2278 device = find_device_by_ip_iface (self, iface);
2280 path = nm_exported_object_get_path (NM_EXPORTED_OBJECT (device));
2283 g_dbus_method_invocation_return_error (context,
2285 NM_MANAGER_ERROR_UNKNOWN_DEVICE,
2286 "No device found for the requested iface.");
2288 g_dbus_method_invocation_return_value (context,
2289 g_variant_new ("(o)", path));
2294 is_compatible_with_slave (NMConnection *master, NMConnection *slave)
2296 NMSettingConnection *s_con;
2298 g_return_val_if_fail (master, FALSE);
2299 g_return_val_if_fail (slave, FALSE);
2301 s_con = nm_connection_get_setting_connection (slave);
2304 return nm_connection_is_type (master, nm_setting_connection_get_slave_type (s_con));
2309 * @self: #NMManager object
2310 * @connection: the #NMConnection to find the master connection and device for
2311 * @device: the #NMDevice, if any, which will activate @connection
2312 * @out_master_connection: on success, the master connection of @connection if
2313 * that master connection was found
2314 * @out_master_device: on success, the master device of @connection if that
2315 * master device was found
2316 * @out_master_ac: on success, the master ActiveConnection of @connection if
2317 * there already is one
2318 * @error: the error, if an error occurred
2320 * Given an #NMConnection, attempts to find its master. If @connection has
2321 * no master, this will return %TRUE and @out_master_connection and
2322 * @out_master_device will be untouched.
2324 * If @connection does have a master, then the outputs depend on what is in its
2325 * #NMSettingConnection:master property:
2327 * If "master" is the ifname of an existing #NMDevice, and that device has a
2328 * compatible master connection activated or activating on it, then
2329 * @out_master_device, @out_master_connection, and @out_master_ac will all be
2330 * set. If the device exists and is idle, only @out_master_device will be set.
2331 * If the device exists and has an incompatible connection on it, an error
2334 * If "master" is the ifname of a non-existent device, then @out_master_device
2335 * will be %NULL, and @out_master_connection will be a connection whose
2336 * activation would cause the creation of that device. @out_master_ac MAY be
2337 * set in this case as well (if the connection has started activating, but has
2338 * not yet created its device).
2340 * If "master" is the UUID of a compatible master connection, then
2341 * @out_master_connection will be the identified connection, and @out_master_device
2342 * and/or @out_master_ac will be set if the connection is currently activating.
2343 * (@out_master_device will not be set if the device exists but does not have
2344 * @out_master_connection active/activating on it.)
2346 * Returns: %TRUE if the master device and/or connection could be found or if
2347 * the connection did not require a master, %FALSE otherwise
2350 find_master (NMManager *self,
2351 NMConnection *connection,
2353 NMSettingsConnection **out_master_connection,
2354 NMDevice **out_master_device,
2355 NMActiveConnection **out_master_ac,
2358 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
2359 NMSettingConnection *s_con;
2361 NMDevice *master_device = NULL;
2362 NMSettingsConnection *master_connection = NULL;
2365 s_con = nm_connection_get_setting_connection (connection);
2367 master = nm_setting_connection_get_master (s_con);
2370 return TRUE; /* success, but no master */
2372 /* Try as an interface name first */
2373 master_device = find_device_by_iface (self, master, NULL, connection);
2374 if (master_device) {
2375 if (master_device == device) {
2376 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_DEPENDENCY_FAILED,
2377 "Device cannot be its own master");
2381 master_connection = nm_device_get_settings_connection (master_device);
2382 if (master_connection && !is_compatible_with_slave (NM_CONNECTION (master_connection), connection)) {
2383 g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_DEPENDENCY_FAILED,
2384 "The active connection on %s is not a valid master for '%s'",
2385 nm_device_get_iface (master_device),
2386 nm_connection_get_id (connection));
2390 /* Try master as a connection UUID */
2391 master_connection = nm_settings_get_connection_by_uuid (priv->settings, master);
2392 if (master_connection) {
2393 /* Check if the master connection is activated on some device already */
2394 for (iter = priv->devices; iter; iter = g_slist_next (iter)) {
2395 NMDevice *candidate = NM_DEVICE (iter->data);
2397 if (candidate == device)
2400 if (nm_device_get_settings_connection (candidate) == master_connection) {
2401 master_device = candidate;
2408 if (out_master_connection)
2409 *out_master_connection = master_connection;
2410 if (out_master_device)
2411 *out_master_device = master_device;
2412 if (out_master_ac && master_connection)
2413 *out_master_ac = find_ac_for_connection (self, NM_CONNECTION (master_connection));
2415 if (master_device || master_connection)
2418 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_DEVICE,
2419 "Master connection not found or invalid");
2425 * ensure_master_active_connection:
2426 * @self: the #NMManager
2427 * @subject: the #NMAuthSubject representing the requestor of this activation
2428 * @connection: the connection that should depend on @master_connection
2429 * @device: the #NMDevice, if any, which will activate @connection
2430 * @master_connection: the master connection, or %NULL
2431 * @master_device: the master device, or %NULL
2432 * @error: the error, if an error occurred
2434 * Determines whether a given #NMConnection depends on another connection to
2435 * be activated, and if so, finds that master connection or creates it.
2437 * If @master_device and @master_connection are both set then @master_connection
2438 * MUST already be activated or activating on @master_device, and the function will
2439 * return the existing #NMActiveConnection.
2441 * If only @master_device is set, and it has an #NMActiveConnection, then the
2442 * function will return it if it is a compatible master, or an error if not. If it
2443 * doesn't have an AC, then the function will create one if a compatible master
2444 * connection exists, or return an error if not.
2446 * If only @master_connection is set, then this will try to find or create a compatible
2447 * #NMDevice, and either activate @master_connection on that device or return an error.
2449 * Returns: the master #NMActiveConnection that the caller should depend on, or
2450 * %NULL if an error occurred
2452 static NMActiveConnection *
2453 ensure_master_active_connection (NMManager *self,
2454 NMAuthSubject *subject,
2455 NMConnection *connection,
2457 NMSettingsConnection *master_connection,
2458 NMDevice *master_device,
2461 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
2462 NMActiveConnection *master_ac = NULL;
2463 NMDeviceState master_state;
2466 g_assert (connection);
2467 g_assert (master_connection || master_device);
2469 /* If the master device isn't activated then we need to activate it using
2470 * compatible connection. If it's already activating we can just proceed.
2472 if (master_device) {
2473 NMSettingsConnection *device_connection = nm_device_get_settings_connection (master_device);
2475 /* If we're passed a connection and a device, we require that connection
2476 * be already activated on the device, eg returned from find_master().
2478 g_assert (!master_connection || master_connection == device_connection);
2479 if (device_connection && !is_compatible_with_slave (NM_CONNECTION (device_connection), connection)) {
2480 g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_DEPENDENCY_FAILED,
2481 "The active connection on %s is not a valid master for '%s'",
2482 nm_device_get_iface (master_device),
2483 nm_connection_get_id (connection));
2487 master_state = nm_device_get_state (master_device);
2488 if ( (master_state == NM_DEVICE_STATE_ACTIVATED)
2489 || nm_device_is_activating (master_device)) {
2490 /* Device already using master_connection */
2491 g_assert (device_connection);
2492 return NM_ACTIVE_CONNECTION (nm_device_get_act_request (master_device));
2495 /* If the device is disconnected, find a compatible connection and
2496 * activate it on the device.
2498 if (master_state == NM_DEVICE_STATE_DISCONNECTED || !nm_device_is_real (master_device)) {
2499 GSList *connections;
2501 g_assert (master_connection == NULL);
2503 /* Find a compatible connection and activate this device using it */
2504 connections = nm_manager_get_activatable_connections (self);
2505 for (iter = connections; iter; iter = g_slist_next (iter)) {
2506 NMSettingsConnection *candidate = NM_SETTINGS_CONNECTION (iter->data);
2508 /* Ensure eg bond/team slave and the candidate master is a
2511 if (!is_compatible_with_slave (NM_CONNECTION (candidate), connection))
2514 if (nm_device_check_connection_available (master_device, NM_CONNECTION (candidate), NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST, NULL)) {
2515 master_ac = nm_manager_activate_connection (self,
2522 g_prefix_error (error, "%s", "Master device activation failed: ");
2523 g_slist_free (connections);
2527 g_slist_free (connections);
2531 NM_MANAGER_ERROR_UNKNOWN_CONNECTION,
2532 "No compatible connection found for master device %s.",
2533 nm_device_get_iface (master_device));
2537 /* Otherwise, the device is unmanaged, unavailable, or disconnecting */
2540 NM_MANAGER_ERROR_DEPENDENCY_FAILED,
2541 "Master device %s unmanaged or not available for activation",
2542 nm_device_get_iface (master_device));
2543 } else if (master_connection) {
2544 gboolean found_device = FALSE;
2546 /* Find a compatible device and activate it using this connection */
2547 for (iter = priv->devices; iter; iter = g_slist_next (iter)) {
2548 NMDevice *candidate = NM_DEVICE (iter->data);
2550 if (candidate == device) {
2551 /* A device obviously can't be its own master */
2555 if (!nm_device_check_connection_available (candidate, NM_CONNECTION (master_connection), NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST, NULL))
2558 found_device = TRUE;
2559 if (!nm_device_is_software (candidate)) {
2560 master_state = nm_device_get_state (candidate);
2561 if (nm_device_is_real (candidate) && master_state != NM_DEVICE_STATE_DISCONNECTED)
2565 master_ac = nm_manager_activate_connection (self,
2572 g_prefix_error (error, "%s", "Master device activation failed: ");
2578 NM_MANAGER_ERROR_UNKNOWN_DEVICE,
2579 "No compatible disconnected device found for master connection %s.",
2580 nm_settings_connection_get_uuid (master_connection));
2582 g_assert_not_reached ();
2589 * @manager: #NMManager object
2590 * @connection: the master #NMSettingsConnection to find slave connections for
2591 * @device: the master #NMDevice for the @connection
2593 * Given an #NMSettingsConnection, attempts to find its slaves. If @connection is not
2594 * master, or has not any slaves, this will return %NULL.
2596 * Returns: list of slave connections for given master @connection, or %NULL
2599 find_slaves (NMManager *manager,
2600 NMSettingsConnection *connection,
2603 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
2604 GSList *all_connections, *iter;
2605 GSList *slaves = NULL;
2606 NMSettingConnection *s_con;
2609 s_con = nm_connection_get_setting_connection (NM_CONNECTION (connection));
2611 master = nm_setting_connection_get_master (s_con);
2614 return NULL; /* connection is not master */
2616 /* Search through all connections, not only inactive ones, because
2617 * even if a slave was already active, it might be deactivated during
2618 * master reactivation.
2620 all_connections = nm_settings_get_connections (priv->settings);
2621 for (iter = all_connections; iter; iter = iter->next) {
2622 NMSettingsConnection *master_connection = NULL;
2623 NMDevice *master_device = NULL;
2624 NMConnection *candidate = iter->data;
2626 find_master (manager, candidate, NULL, &master_connection, &master_device, NULL, NULL);
2627 if ( (master_connection && master_connection == connection)
2628 || (master_device && master_device == device)) {
2629 slaves = g_slist_prepend (slaves, candidate);
2632 g_slist_free (all_connections);
2634 return g_slist_reverse (slaves);
2638 should_connect_slaves (NMConnection *connection, NMDevice *device)
2640 NMSettingConnection *s_con;
2641 NMSettingConnectionAutoconnectSlaves autoconnect_slaves;
2642 gs_free char *value = NULL;
2644 s_con = nm_connection_get_setting_connection (connection);
2647 /* Check autoconnect-slaves property */
2648 autoconnect_slaves = nm_setting_connection_get_autoconnect_slaves (s_con);
2649 if (autoconnect_slaves != NM_SETTING_CONNECTION_AUTOCONNECT_SLAVES_DEFAULT)
2652 /* Check configuration default for autoconnect-slaves property */
2653 value = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA,
2654 "connection.autoconnect-slaves", device);
2656 autoconnect_slaves = _nm_utils_ascii_str_to_int64 (value, 10, 0, 1, -1);
2659 if (autoconnect_slaves == NM_SETTING_CONNECTION_AUTOCONNECT_SLAVES_NO)
2661 if (autoconnect_slaves == NM_SETTING_CONNECTION_AUTOCONNECT_SLAVES_YES)
2667 autoconnect_slaves (NMManager *self,
2668 NMSettingsConnection *master_connection,
2669 NMDevice *master_device,
2670 NMAuthSubject *subject)
2672 GError *local_err = NULL;
2673 gboolean ret = FALSE;
2675 if (should_connect_slaves (NM_CONNECTION (master_connection), master_device)) {
2676 GSList *slaves, *iter;
2678 iter = slaves = find_slaves (self, master_connection, master_device);
2679 ret = slaves != NULL;
2682 NMSettingsConnection *slave_connection = iter->data;
2685 _LOGD (LOGD_CORE, "will activate slave connection '%s' (%s) as a dependency for master '%s' (%s)",
2686 nm_settings_connection_get_id (slave_connection),
2687 nm_settings_connection_get_uuid (slave_connection),
2688 nm_settings_connection_get_id (master_connection),
2689 nm_settings_connection_get_uuid (master_connection));
2691 /* Schedule slave activation */
2692 nm_manager_activate_connection (self,
2695 nm_manager_get_best_device_for_connection (self, NM_CONNECTION (slave_connection), FALSE),
2699 _LOGW (LOGD_CORE, "Slave connection activation failed: %s", local_err->message);
2700 g_error_free (local_err);
2703 g_slist_free (slaves);
2709 _internal_activate_vpn (NMManager *self, NMActiveConnection *active, GError **error)
2713 g_assert (NM_IS_VPN_CONNECTION (active));
2715 nm_exported_object_export (NM_EXPORTED_OBJECT (active));
2716 success = nm_vpn_manager_activate_connection (NM_MANAGER_GET_PRIVATE (self)->vpn_manager,
2717 NM_VPN_CONNECTION (active),
2720 g_object_notify (G_OBJECT (self), NM_MANAGER_ACTIVE_CONNECTIONS);
2722 nm_exported_object_unexport (NM_EXPORTED_OBJECT (active));
2726 /* Traverse the device to disconnected state. This means that the device is ready
2727 * for connection and will proceed activating if there's an activation request
2731 unmanaged_to_disconnected (NMDevice *device)
2733 /* when creating the software device, it can happen that the device is
2734 * still unmanaged by NM_UNMANAGED_PLATFORM_INIT because we didn't yet
2735 * get the udev event. At this point, we can no longer delay the activation
2736 * and force the device to be managed. */
2737 nm_device_set_unmanaged_by_flags (device, NM_UNMANAGED_PLATFORM_INIT, FALSE, NM_DEVICE_STATE_REASON_USER_REQUESTED);
2739 nm_device_set_unmanaged_by_flags (device, NM_UNMANAGED_USER_EXPLICIT, FALSE, NM_DEVICE_STATE_REASON_USER_REQUESTED);
2741 g_return_if_fail (nm_device_get_managed (device, FALSE));
2743 if (nm_device_get_state (device) == NM_DEVICE_STATE_UNMANAGED) {
2744 nm_device_state_changed (device,
2745 NM_DEVICE_STATE_UNAVAILABLE,
2746 NM_DEVICE_STATE_REASON_USER_REQUESTED);
2749 if ( nm_device_is_available (device, NM_DEVICE_CHECK_DEV_AVAILABLE_FOR_USER_REQUEST)
2750 && (nm_device_get_state (device) == NM_DEVICE_STATE_UNAVAILABLE)) {
2751 nm_device_state_changed (device,
2752 NM_DEVICE_STATE_DISCONNECTED,
2753 NM_DEVICE_STATE_REASON_USER_REQUESTED);
2758 _internal_activate_device (NMManager *self, NMActiveConnection *active, GError **error)
2760 NMDevice *device, *existing, *master_device = NULL;
2761 NMConnection *applied;
2762 NMSettingsConnection *connection;
2763 NMSettingsConnection *master_connection = NULL;
2764 NMConnection *existing_connection = NULL;
2765 NMActiveConnection *master_ac = NULL;
2766 NMAuthSubject *subject;
2767 char *error_desc = NULL;
2769 g_return_val_if_fail (NM_IS_MANAGER (self), FALSE);
2770 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (active), FALSE);
2771 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2773 g_assert (NM_IS_VPN_CONNECTION (active) == FALSE);
2775 connection = nm_active_connection_get_settings_connection (active);
2776 g_assert (connection);
2778 applied = nm_active_connection_get_applied_connection (active);
2780 device = nm_active_connection_get_device (active);
2781 g_return_val_if_fail (device != NULL, FALSE);
2783 /* If the device is active and its connection is not visible to the
2784 * user that's requesting this new activation, fail, since other users
2785 * should not be allowed to implicitly deactivate private connections
2786 * by activating a connection of their own.
2788 existing_connection = nm_device_get_applied_connection (device);
2789 subject = nm_active_connection_get_subject (active);
2790 if (existing_connection &&
2791 !nm_auth_is_subject_in_acl (existing_connection,
2796 NM_MANAGER_ERROR_PERMISSION_DENIED,
2797 "Private connection already active on the device: %s",
2799 g_free (error_desc);
2803 /* Final connection must be available on device */
2804 if (!nm_device_check_connection_available (device, applied, NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST, NULL)) {
2805 g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_CONNECTION,
2806 "Connection '%s' is not available on the device %s at this time.",
2807 nm_settings_connection_get_id (connection), nm_device_get_iface (device));
2811 /* Create any backing resources the device needs */
2812 if (!nm_device_is_real (device)) {
2815 parent = find_parent_device_for_connection (self, (NMConnection *) connection, NULL);
2816 if (!nm_device_create_and_realize (device, (NMConnection *) connection, parent, error)) {
2817 g_prefix_error (error, "%s failed to create resources: ", nm_device_get_iface (device));
2822 /* Try to find the master connection/device if the connection has a dependency */
2823 if (!find_master (self, applied, device,
2824 &master_connection, &master_device, &master_ac,
2828 /* Ensure there's a master active connection the new connection we're
2829 * activating can depend on.
2831 if (master_connection || master_device) {
2832 if (master_connection) {
2833 _LOGD (LOGD_CORE, "Activation of '%s' requires master connection '%s'",
2834 nm_settings_connection_get_id (connection),
2835 nm_settings_connection_get_id (master_connection));
2837 if (master_device) {
2838 _LOGD (LOGD_CORE, "Activation of '%s' requires master device '%s'",
2839 nm_settings_connection_get_id (connection),
2840 nm_device_get_ip_iface (master_device));
2843 /* Ensure eg bond slave and the candidate master is a bond master */
2844 if (master_connection && !is_compatible_with_slave (NM_CONNECTION (master_connection), applied)) {
2845 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_DEPENDENCY_FAILED,
2846 "The master connection was not compatible");
2851 master_ac = ensure_master_active_connection (self,
2852 nm_active_connection_get_subject (active),
2865 nm_active_connection_set_master (active, master_ac);
2866 _LOGD (LOGD_CORE, "Activation of '%s' depends on active connection %p %s",
2867 nm_settings_connection_get_id (connection),
2869 nm_exported_object_get_path (NM_EXPORTED_OBJECT (master_ac)) ?: "");
2872 /* Check slaves for master connection and possibly activate them */
2873 autoconnect_slaves (self, connection, device, nm_active_connection_get_subject (active));
2875 /* Disconnect the connection if connected or queued on another device */
2876 existing = nm_manager_get_connection_device (self, NM_CONNECTION (connection));
2878 nm_device_steal_connection (existing, connection);
2880 /* Make the device ready for activation. */
2881 unmanaged_to_disconnected (device);
2883 /* Export the new ActiveConnection to clients and start it on the device */
2884 nm_exported_object_export (NM_EXPORTED_OBJECT (active));
2885 g_object_notify (G_OBJECT (self), NM_MANAGER_ACTIVE_CONNECTIONS);
2886 nm_device_queue_activation (device, NM_ACT_REQUEST (active));
2891 _internal_activate_generic (NMManager *self, NMActiveConnection *active, GError **error)
2893 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
2894 gboolean success = FALSE;
2896 /* Ensure activation request is still valid, eg that its device hasn't gone
2897 * away or that some other dependency has not failed.
2899 if (nm_active_connection_get_state (active) >= NM_ACTIVE_CONNECTION_STATE_DEACTIVATING) {
2900 g_set_error_literal (error,
2902 NM_MANAGER_ERROR_DEPENDENCY_FAILED,
2903 "Activation failed because dependencies failed.");
2907 if (NM_IS_VPN_CONNECTION (active))
2908 success = _internal_activate_vpn (self, active, error);
2910 success = _internal_activate_device (self, active, error);
2913 /* Force an update of the Manager's activating-connection property.
2914 * The device changes state before the AC gets exported, which causes
2915 * the manager's 'activating-connection' property to be NULL since the
2916 * AC only gets a D-Bus path when it's exported. So now that the AC
2917 * is exported, make sure the manager's activating-connection property
2920 active_connection_add (self, active);
2921 policy_activating_device_changed (G_OBJECT (priv->policy), NULL, self);
2927 static NMActiveConnection *
2928 _new_vpn_active_connection (NMManager *self,
2929 NMSettingsConnection *settings_connection,
2930 const char *specific_object,
2931 NMAuthSubject *subject,
2934 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
2935 NMActiveConnection *parent = NULL;
2936 NMDevice *device = NULL;
2938 g_return_val_if_fail (!settings_connection || NM_IS_SETTINGS_CONNECTION (settings_connection), NULL);
2940 if (specific_object) {
2941 /* Find the specific connection the client requested we use */
2942 parent = active_connection_get_by_path (self, specific_object);
2944 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_CONNECTION_NOT_ACTIVE,
2945 "Base connection for VPN connection not active.");
2949 parent = priv->primary_connection;
2952 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_CONNECTION,
2953 "Could not find source connection.");
2957 device = nm_active_connection_get_device (parent);
2959 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_DEVICE,
2960 "Source connection had no active device.");
2964 return (NMActiveConnection *) nm_vpn_connection_new (settings_connection,
2966 nm_exported_object_get_path (NM_EXPORTED_OBJECT (parent)),
2970 static NMActiveConnection *
2971 _new_active_connection (NMManager *self,
2972 NMConnection *connection,
2973 const char *specific_object,
2975 NMAuthSubject *subject,
2978 NMSettingsConnection *settings_connection = NULL;
2979 NMActiveConnection *existing_ac;
2982 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2983 g_return_val_if_fail (NM_IS_AUTH_SUBJECT (subject), NULL);
2985 /* Can't create new AC for already-active connection */
2986 existing_ac = find_ac_for_connection (self, connection);
2987 if (NM_IS_VPN_CONNECTION (existing_ac)) {
2988 g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_CONNECTION_ALREADY_ACTIVE,
2989 "Connection '%s' is already active",
2990 nm_connection_get_id (connection));
2994 /* Normalize the specific object */
2995 if (specific_object && g_strcmp0 (specific_object, "/") == 0)
2996 specific_object = NULL;
2998 is_vpn = nm_connection_is_type (NM_CONNECTION (connection), NM_SETTING_VPN_SETTING_NAME);
3000 if (NM_IS_SETTINGS_CONNECTION (connection))
3001 settings_connection = (NMSettingsConnection *) connection;
3004 return _new_vpn_active_connection (self,
3005 settings_connection,
3011 return (NMActiveConnection *) nm_act_request_new (settings_connection,
3018 _internal_activation_failed (NMManager *self,
3019 NMActiveConnection *active,
3020 const char *error_desc)
3022 _LOGD (LOGD_CORE, "Failed to activate '%s': %s",
3023 nm_active_connection_get_settings_connection_id (active),
3026 if (nm_active_connection_get_state (active) <= NM_ACTIVE_CONNECTION_STATE_ACTIVATED) {
3027 nm_active_connection_set_state (active, NM_ACTIVE_CONNECTION_STATE_DEACTIVATING);
3028 nm_active_connection_set_state (active, NM_ACTIVE_CONNECTION_STATE_DEACTIVATED);
3033 _internal_activation_auth_done (NMActiveConnection *active,
3035 const char *error_desc,
3036 gpointer user_data1,
3037 gpointer user_data2)
3039 NMManager *self = user_data1;
3040 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3041 GError *error = NULL;
3043 priv->authorizing_connections = g_slist_remove (priv->authorizing_connections, active);
3046 if (_internal_activate_generic (self, active, &error)) {
3047 g_object_unref (active);
3052 g_assert (error_desc || error);
3053 _internal_activation_failed (self, active, error_desc ? error_desc : error->message);
3054 g_object_unref (active);
3055 g_clear_error (&error);
3059 * nm_manager_activate_connection():
3060 * @self: the #NMManager
3061 * @connection: the #NMSettingsConnection to activate on @device
3062 * @specific_object: the specific object path, if any, for the activation
3063 * @device: the #NMDevice to activate @connection on
3064 * @subject: the subject which requested activation
3065 * @error: return location for an error
3067 * Begins a new internally-initiated activation of @connection on @device.
3068 * @subject should be the subject of the activation that triggered this
3069 * one, or if this is an autoconnect request, a new internal subject.
3070 * The returned #NMActiveConnection is owned by the Manager and should be
3071 * referenced by the caller if the caller continues to use it.
3073 * Returns: (transfer none): the new #NMActiveConnection that tracks
3074 * activation of @connection on @device
3076 NMActiveConnection *
3077 nm_manager_activate_connection (NMManager *self,
3078 NMSettingsConnection *connection,
3079 const char *specific_object,
3081 NMAuthSubject *subject,
3084 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3085 NMActiveConnection *active;
3086 char *error_desc = NULL;
3089 g_return_val_if_fail (self != NULL, NULL);
3090 g_return_val_if_fail (connection != NULL, NULL);
3091 g_return_val_if_fail (error != NULL, NULL);
3092 g_return_val_if_fail (*error == NULL, NULL);
3094 /* Ensure the subject has permissions for this connection */
3095 if (!nm_auth_is_subject_in_acl (NM_CONNECTION (connection),
3098 g_set_error_literal (error,
3100 NM_MANAGER_ERROR_PERMISSION_DENIED,
3102 g_free (error_desc);
3106 /* Look for a active connection that's equivalent and is already pending authorization
3107 * and eventual activation. This is used to de-duplicate concurrent activations which would
3108 * otherwise race and cause the device to disconnect and reconnect repeatedly.
3109 * In particular, this allows the master and multiple slaves to concurrently auto-activate
3110 * while all the slaves would use the same active-connection. */
3111 for (iter = priv->authorizing_connections; iter; iter = g_slist_next (iter)) {
3112 active = iter->data;
3114 if ( connection == nm_active_connection_get_settings_connection (active)
3115 && g_strcmp0 (nm_active_connection_get_specific_object (active), specific_object) == 0
3116 && nm_active_connection_get_device (active) == device
3117 && nm_auth_subject_is_internal (nm_active_connection_get_subject (active))
3118 && nm_auth_subject_is_internal (subject))
3122 active = _new_active_connection (self,
3123 NM_CONNECTION (connection),
3129 priv->authorizing_connections = g_slist_prepend (priv->authorizing_connections, active);
3130 nm_active_connection_authorize (active, NULL, _internal_activation_auth_done, self, NULL);
3136 * validate_activation_request:
3137 * @self: the #NMManager
3138 * @context: the D-Bus context of the requestor
3139 * @connection: the partial or complete #NMConnection to be activated
3140 * @device_path: the object path of the device to be activated, or "/"
3141 * @out_device: on successful reutrn, the #NMDevice to be activated with @connection
3142 * @out_vpn: on successful return, %TRUE if @connection is a VPN connection
3143 * @error: location to store an error on failure
3145 * Performs basic validation on an activation request, including ensuring that
3146 * the requestor is a valid Unix process, is not disallowed in @connection
3147 * permissions, and that a device exists that can activate @connection.
3149 * Returns: on success, the #NMAuthSubject representing the requestor, or
3152 static NMAuthSubject *
3153 validate_activation_request (NMManager *self,
3154 GDBusMethodInvocation *context,
3155 NMConnection *connection,
3156 const char *device_path,
3157 NMDevice **out_device,
3161 NMDevice *device = NULL;
3162 gboolean vpn = FALSE;
3163 NMAuthSubject *subject = NULL;
3164 char *error_desc = NULL;
3166 g_assert (connection);
3167 g_assert (out_device);
3170 /* Validate the caller */
3171 subject = nm_auth_subject_new_unix_process_from_context (context);
3173 g_set_error_literal (error,
3175 NM_MANAGER_ERROR_PERMISSION_DENIED,
3176 "Failed to get request UID.");
3180 /* Ensure the subject has permissions for this connection */
3181 if (!nm_auth_is_subject_in_acl (connection,
3184 g_set_error_literal (error,
3186 NM_MANAGER_ERROR_PERMISSION_DENIED,
3188 g_free (error_desc);
3192 /* Not implemented yet, we want to fail early */
3193 if ( nm_connection_get_setting_connection (connection)
3194 && nm_connection_get_setting_ip6_config (connection)
3195 && !strcmp (nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG),
3196 NM_SETTING_IP6_CONFIG_METHOD_SHARED)) {
3197 g_set_error_literal (error,
3199 NM_MANAGER_ERROR_CONNECTION_NOT_AVAILABLE,
3200 "Sharing IPv6 connections is not supported yet.");
3204 /* Check whether it's a VPN or not */
3205 if ( nm_connection_get_setting_vpn (connection)
3206 || nm_connection_is_type (connection, NM_SETTING_VPN_SETTING_NAME))
3209 /* Normalize device path */
3210 if (device_path && g_strcmp0 (device_path, "/") == 0)
3213 /* And validate it */
3215 device = nm_manager_get_device_by_path (self, device_path);
3217 g_set_error_literal (error,
3219 NM_MANAGER_ERROR_UNKNOWN_DEVICE,
3220 "Device not found");
3224 device = nm_manager_get_best_device_for_connection (self, connection, TRUE);
3226 if (!device && !vpn) {
3227 gboolean is_software = nm_connection_is_virtual (connection);
3229 /* VPN and software-device connections don't need a device yet */
3231 g_set_error_literal (error,
3233 NM_MANAGER_ERROR_UNKNOWN_DEVICE,
3234 "No suitable device found for this connection.");
3241 /* Look for an existing device with the connection's interface name */
3242 iface = nm_manager_get_connection_iface (self, connection, NULL, error);
3246 device = find_device_by_iface (self, iface, connection, NULL);
3251 if ((!vpn || device_path) && !device) {
3252 g_set_error_literal (error,
3254 NM_MANAGER_ERROR_UNKNOWN_DEVICE,
3255 "Failed to find a compatible device for this connection");
3259 *out_device = device;
3264 g_object_unref (subject);
3268 /***********************************************************************/
3271 _activation_auth_done (NMActiveConnection *active,
3273 const char *error_desc,
3274 gpointer user_data1,
3275 gpointer user_data2)
3277 NMManager *self = user_data1;
3278 GDBusMethodInvocation *context = user_data2;
3279 GError *error = NULL;
3280 NMAuthSubject *subject;
3281 NMSettingsConnection *connection;
3283 subject = nm_active_connection_get_subject (active);
3284 connection = nm_active_connection_get_settings_connection (active);
3287 if (_internal_activate_generic (self, active, &error)) {
3288 g_dbus_method_invocation_return_value (context,
3289 g_variant_new ("(o)",
3290 nm_exported_object_get_path (NM_EXPORTED_OBJECT (active))));
3291 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ACTIVATE, connection, TRUE,
3293 g_object_unref (active);
3297 error = g_error_new_literal (NM_MANAGER_ERROR,
3298 NM_MANAGER_ERROR_PERMISSION_DENIED,
3303 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ACTIVATE, connection, FALSE,
3304 subject, error->message);
3305 _internal_activation_failed (self, active, error->message);
3307 g_object_unref (active);
3308 g_dbus_method_invocation_take_error (context, error);
3312 impl_manager_activate_connection (NMManager *self,
3313 GDBusMethodInvocation *context,
3314 const char *connection_path,
3315 const char *device_path,
3316 const char *specific_object_path)
3318 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3319 NMActiveConnection *active = NULL;
3320 NMAuthSubject *subject = NULL;
3321 NMSettingsConnection *connection = NULL;
3322 NMDevice *device = NULL;
3323 gboolean is_vpn = FALSE;
3324 GError *error = NULL;
3326 /* Normalize object paths */
3327 if (g_strcmp0 (connection_path, "/") == 0)
3328 connection_path = NULL;
3329 if (g_strcmp0 (specific_object_path, "/") == 0)
3330 specific_object_path = NULL;
3331 if (g_strcmp0 (device_path, "/") == 0)
3334 /* If the connection path is given and valid, that connection is activated.
3335 * Otherwise the "best" connection for the device is chosen and activated,
3336 * regardless of whether that connection is autoconnect-enabled or not
3337 * (since this is an explicit request, not an auto-activation request).
3339 if (connection_path) {
3340 connection = nm_settings_get_connection_by_path (priv->settings, connection_path);
3342 error = g_error_new_literal (NM_MANAGER_ERROR,
3343 NM_MANAGER_ERROR_UNKNOWN_CONNECTION,
3344 "Connection could not be found.");
3348 /* If no connection is given, find a suitable connection for the given device path */
3350 error = g_error_new_literal (NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_DEVICE,
3351 "Only devices may be activated without a specifying a connection");
3354 device = nm_manager_get_device_by_path (self, device_path);
3356 error = g_error_new (NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_DEVICE,
3357 "Cannot activate unknown device %s", device_path);
3361 connection = nm_device_get_best_connection (device, specific_object_path, &error);
3366 subject = validate_activation_request (self,
3368 NM_CONNECTION (connection),
3376 active = _new_active_connection (self,
3377 NM_CONNECTION (connection),
3378 specific_object_path,
3385 nm_active_connection_authorize (active, NULL, _activation_auth_done, self, context);
3386 g_clear_object (&subject);
3391 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ACTIVATE, connection, FALSE,
3392 subject, error->message);
3394 g_clear_object (&active);
3395 g_clear_object (&subject);
3398 g_dbus_method_invocation_take_error (context, error);
3401 /***********************************************************************/
3405 NMActiveConnection *active;
3406 } AddAndActivateInfo;
3409 activation_add_done (NMSettings *settings,
3410 NMSettingsConnection *new_connection,
3412 GDBusMethodInvocation *context,
3413 NMAuthSubject *subject,
3416 AddAndActivateInfo *info = user_data;
3418 gs_unref_object NMActiveConnection *active = NULL;
3419 GError *local = NULL;
3421 self = info->manager;
3422 active = info->active;
3423 g_slice_free (AddAndActivateInfo, info);
3426 nm_active_connection_set_settings_connection (active, new_connection);
3428 if (_internal_activate_generic (self, active, &local)) {
3429 nm_settings_connection_commit_changes (new_connection,
3430 NM_SETTINGS_CONNECTION_COMMIT_REASON_USER_ACTION | NM_SETTINGS_CONNECTION_COMMIT_REASON_ID_CHANGED,
3432 g_dbus_method_invocation_return_value (
3434 g_variant_new ("(oo)",
3435 nm_connection_get_path (NM_CONNECTION (new_connection)),
3436 nm_exported_object_get_path (NM_EXPORTED_OBJECT (active))));
3437 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ADD_ACTIVATE,
3438 nm_active_connection_get_settings_connection (active),
3440 nm_active_connection_get_subject (active),
3448 _internal_activation_failed (self, active, error->message);
3449 nm_settings_connection_delete (new_connection, NULL, NULL);
3450 g_dbus_method_invocation_return_gerror (context, error);
3451 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ADD_ACTIVATE,
3454 nm_active_connection_get_subject (active),
3456 g_clear_error (&local);
3460 _add_and_activate_auth_done (NMActiveConnection *active,
3462 const char *error_desc,
3463 gpointer user_data1,
3464 gpointer user_data2)
3466 NMManager *self = user_data1;
3467 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3468 GDBusMethodInvocation *context = user_data2;
3469 AddAndActivateInfo *info;
3470 GError *error = NULL;
3473 NMConnection *connection;
3475 connection = g_object_steal_data (G_OBJECT (active),
3476 TAG_ACTIVE_CONNETION_ADD_AND_ACTIVATE);
3478 info = g_slice_new (AddAndActivateInfo);
3479 info->manager = self;
3480 info->active = g_object_ref (active);
3482 /* Basic sender auth checks performed; try to add the connection */
3483 nm_settings_add_connection_dbus (priv->settings,
3487 activation_add_done,
3489 g_object_unref (connection);
3491 g_assert (error_desc);
3492 error = g_error_new_literal (NM_MANAGER_ERROR,
3493 NM_MANAGER_ERROR_PERMISSION_DENIED,
3495 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ADD_ACTIVATE,
3498 nm_active_connection_get_subject (active),
3500 g_dbus_method_invocation_take_error (context, error);
3503 g_object_unref (active);
3507 impl_manager_add_and_activate_connection (NMManager *self,
3508 GDBusMethodInvocation *context,
3510 const char *device_path,
3511 const char *specific_object_path)
3513 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3514 NMConnection *connection = NULL;
3515 GSList *all_connections = NULL;
3516 NMActiveConnection *active = NULL;
3517 NMAuthSubject *subject = NULL;
3518 GError *error = NULL;
3519 NMDevice *device = NULL;
3520 gboolean vpn = FALSE;
3522 /* Normalize object paths */
3523 if (g_strcmp0 (specific_object_path, "/") == 0)
3524 specific_object_path = NULL;
3525 if (g_strcmp0 (device_path, "/") == 0)
3528 /* Try to create a new connection with the given settings.
3529 * We allow empty settings for AddAndActivateConnection(). In that case,
3530 * the connection will be completed in nm_utils_complete_generic() or
3531 * nm_device_complete_connection() below. Just make sure we don't expect
3532 * specific data being in the connection till then (especially in
3533 * validate_activation_request()).
3535 connection = nm_simple_connection_new ();
3536 if (settings && g_variant_n_children (settings))
3537 nm_connection_replace_settings (connection, settings, NULL);
3539 subject = validate_activation_request (self,
3549 all_connections = nm_settings_get_connections (priv->settings);
3551 /* Try to fill the VPN's connection setting and name at least */
3552 if (!nm_connection_get_setting_vpn (connection)) {
3553 error = g_error_new_literal (NM_CONNECTION_ERROR,
3554 NM_CONNECTION_ERROR_MISSING_SETTING,
3555 "VPN connections require a 'vpn' setting");
3556 g_prefix_error (&error, "%s: ", NM_SETTING_VPN_SETTING_NAME);
3560 nm_utils_complete_generic (NM_PLATFORM_GET,
3562 NM_SETTING_VPN_SETTING_NAME,
3565 _("VPN connection"),
3567 FALSE); /* No IPv6 by default for now */
3569 /* Let each device subclass complete the connection */
3570 if (!nm_device_complete_connection (device,
3572 specific_object_path,
3577 g_slist_free (all_connections);
3578 all_connections = NULL;
3580 active = _new_active_connection (self,
3582 specific_object_path,
3589 g_object_set_data_full (G_OBJECT (active),
3590 TAG_ACTIVE_CONNETION_ADD_AND_ACTIVATE,
3594 nm_active_connection_authorize (active, connection, _add_and_activate_auth_done, self, context);
3595 g_object_unref (subject);
3599 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ADD_ACTIVATE, NULL, FALSE, subject, error->message);
3600 g_clear_object (&connection);
3601 g_slist_free (all_connections);
3602 g_clear_object (&subject);
3603 g_clear_object (&active);
3606 g_dbus_method_invocation_take_error (context, error);
3609 /***********************************************************************/
3612 nm_manager_deactivate_connection (NMManager *manager,
3613 const char *connection_path,
3614 NMDeviceStateReason reason,
3617 NMActiveConnection *active;
3618 gboolean success = FALSE;
3620 active = active_connection_get_by_path (manager, connection_path);
3622 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_CONNECTION_NOT_ACTIVE,
3623 "The connection was not active.");
3627 if (NM_IS_VPN_CONNECTION (active)) {
3628 NMVpnConnectionStateReason vpn_reason = NM_VPN_CONNECTION_STATE_REASON_USER_DISCONNECTED;
3630 if (reason == NM_DEVICE_STATE_REASON_CONNECTION_REMOVED)
3631 vpn_reason = NM_VPN_CONNECTION_STATE_REASON_CONNECTION_REMOVED;
3632 if (nm_vpn_connection_deactivate (NM_VPN_CONNECTION (active), vpn_reason, FALSE))
3635 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_CONNECTION_NOT_ACTIVE,
3636 "The VPN connection was not active.");
3638 g_assert (NM_IS_ACT_REQUEST (active));
3639 nm_device_state_changed (nm_active_connection_get_device (active),
3640 NM_DEVICE_STATE_DEACTIVATING,
3646 g_object_notify (G_OBJECT (manager), NM_MANAGER_ACTIVE_CONNECTIONS);
3652 deactivate_net_auth_done_cb (NMAuthChain *chain,
3654 GDBusMethodInvocation *context,
3657 NMManager *self = NM_MANAGER (user_data);
3658 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3659 GError *error = NULL;
3660 NMAuthCallResult result;
3661 NMActiveConnection *active;
3666 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
3668 path = nm_auth_chain_get_data (chain, "path");
3669 result = nm_auth_chain_get_result (chain, NM_AUTH_PERMISSION_NETWORK_CONTROL);
3672 _LOGD (LOGD_CORE, "Disconnect request failed: %s", auth_error->message);
3673 error = g_error_new (NM_MANAGER_ERROR,
3674 NM_MANAGER_ERROR_PERMISSION_DENIED,
3675 "Deactivate request failed: %s",
3676 auth_error->message);
3677 } else if (result != NM_AUTH_CALL_RESULT_YES) {
3678 error = g_error_new_literal (NM_MANAGER_ERROR,
3679 NM_MANAGER_ERROR_PERMISSION_DENIED,
3680 "Not authorized to deactivate connections");
3682 /* success; deactivation allowed */
3683 if (!nm_manager_deactivate_connection (self,
3685 NM_DEVICE_STATE_REASON_USER_REQUESTED,
3690 active = active_connection_get_by_path (self, path);
3692 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_DEACTIVATE,
3693 nm_active_connection_get_settings_connection (active),
3695 nm_auth_chain_get_subject (chain),
3696 error ? error->message : NULL);
3700 g_dbus_method_invocation_take_error (context, error);
3702 g_dbus_method_invocation_return_value (context, NULL);
3704 nm_auth_chain_unref (chain);
3708 impl_manager_deactivate_connection (NMManager *self,
3709 GDBusMethodInvocation *context,
3710 const char *active_path)
3712 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3713 NMActiveConnection *ac;
3714 NMSettingsConnection *connection = NULL;
3715 GError *error = NULL;
3716 NMAuthSubject *subject = NULL;
3718 char *error_desc = NULL;
3720 /* Find the connection by its object path */
3721 ac = active_connection_get_by_path (self, active_path);
3723 connection = nm_active_connection_get_settings_connection (ac);
3726 error = g_error_new_literal (NM_MANAGER_ERROR,
3727 NM_MANAGER_ERROR_CONNECTION_NOT_ACTIVE,
3728 "The connection was not active.");
3732 /* Validate the caller */
3733 subject = nm_auth_subject_new_unix_process_from_context (context);
3735 error = g_error_new_literal (NM_MANAGER_ERROR,
3736 NM_MANAGER_ERROR_PERMISSION_DENIED,
3737 "Failed to get request UID.");
3741 /* Ensure the subject has permissions for this connection */
3742 if (!nm_auth_is_subject_in_acl (NM_CONNECTION (connection),
3745 error = g_error_new_literal (NM_MANAGER_ERROR,
3746 NM_MANAGER_ERROR_PERMISSION_DENIED,
3748 g_free (error_desc);
3752 /* Validate the user request */
3753 chain = nm_auth_chain_new_subject (subject, context, deactivate_net_auth_done_cb, self);
3755 error = g_error_new_literal (NM_MANAGER_ERROR,
3756 NM_MANAGER_ERROR_PERMISSION_DENIED,
3757 "Unable to authenticate request.");
3761 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
3762 nm_auth_chain_set_data (chain, "path", g_strdup (active_path), g_free);
3763 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_NETWORK_CONTROL, TRUE);
3768 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_DEACTIVATE, connection, FALSE,
3769 subject, error->message);
3771 g_dbus_method_invocation_take_error (context, error);
3773 g_clear_object (&subject);
3777 device_is_wake_on_lan (NMDevice *device)
3779 return nm_platform_link_get_wake_on_lan (NM_PLATFORM_GET, nm_device_get_ip_ifindex (device));
3783 do_sleep_wake (NMManager *self, gboolean sleeping_changed)
3785 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3786 gboolean suspending, waking_from_suspend;
3789 suspending = sleeping_changed && priv->sleeping;
3790 waking_from_suspend = sleeping_changed && !priv->sleeping;
3792 if (manager_sleeping (self)) {
3793 _LOGI (LOGD_SUSPEND, "%s...", suspending ? "sleeping" : "disabling");
3795 /* FIXME: are there still hardware devices that need to be disabled around
3798 for (iter = priv->devices; iter; iter = iter->next) {
3799 NMDevice *device = iter->data;
3801 /* FIXME: shouldn't we be unmanaging software devices if !suspending? */
3802 if (nm_device_is_software (device))
3804 /* Wake-on-LAN devices will be taken down post-suspend rather than pre- */
3805 if (suspending && device_is_wake_on_lan (device))
3808 nm_device_set_unmanaged_by_flags (device, NM_UNMANAGED_SLEEPING, TRUE, NM_DEVICE_STATE_REASON_SLEEPING);
3811 _LOGI (LOGD_SUSPEND, "%s...", waking_from_suspend ? "waking up" : "re-enabling");
3813 if (waking_from_suspend) {
3814 /* Belatedly take down Wake-on-LAN devices; ideally we wouldn't have to do this
3815 * but for now it's the only way to make sure we re-check their connectivity.
3817 for (iter = priv->devices; iter; iter = iter->next) {
3818 NMDevice *device = iter->data;
3820 if (nm_device_is_software (device))
3822 if (device_is_wake_on_lan (device))
3823 nm_device_set_unmanaged_by_flags (device, NM_UNMANAGED_SLEEPING, TRUE, NM_DEVICE_STATE_REASON_SLEEPING);
3827 /* Ensure rfkill state is up-to-date since we don't respond to state
3828 * changes during sleep.
3830 nm_manager_rfkill_update (self, RFKILL_TYPE_UNKNOWN);
3832 /* Re-manage managed devices */
3833 for (iter = priv->devices; iter; iter = iter->next) {
3834 NMDevice *device = NM_DEVICE (iter->data);
3837 if (nm_device_is_software (device))
3840 /* enable/disable wireless devices since that we don't respond
3841 * to killswitch changes during sleep.
3843 for (i = 0; i < RFKILL_TYPE_MAX; i++) {
3844 RadioState *rstate = &priv->radio_states[i];
3845 gboolean enabled = radio_enabled_for_rstate (rstate, TRUE);
3848 _LOGD (LOGD_RFKILL, "%s %s devices (hw_enabled %d, sw_enabled %d, user_enabled %d)",
3849 enabled ? "enabling" : "disabling",
3850 rstate->desc, rstate->hw_enabled, rstate->sw_enabled, rstate->user_enabled);
3853 if (nm_device_get_rfkill_type (device) == rstate->rtype)
3854 nm_device_set_enabled (device, enabled);
3857 nm_device_set_autoconnect (device, TRUE);
3859 nm_device_set_unmanaged_by_flags (device, NM_UNMANAGED_SLEEPING, FALSE, NM_DEVICE_STATE_REASON_NOW_MANAGED);
3863 nm_manager_update_state (self);
3867 _internal_sleep (NMManager *self, gboolean do_sleep)
3869 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3871 if (priv->sleeping == do_sleep)
3874 _LOGI (LOGD_SUSPEND, "%s requested (sleeping: %s enabled: %s)",
3875 do_sleep ? "sleep" : "wake",
3876 priv->sleeping ? "yes" : "no",
3877 priv->net_enabled ? "yes" : "no");
3879 priv->sleeping = do_sleep;
3881 do_sleep_wake (self, TRUE);
3883 g_object_notify (G_OBJECT (self), NM_MANAGER_SLEEPING);
3888 sleep_auth_done_cb (NMAuthChain *chain,
3890 GDBusMethodInvocation *context,
3893 NMManager *self = NM_MANAGER (user_data);
3894 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3896 NMAuthCallResult result;
3899 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
3901 result = nm_auth_chain_get_result (chain, NM_AUTH_PERMISSION_SLEEP_WAKE);
3903 _LOGD (LOGD_SUSPEND, "Sleep/wake request failed: %s", error->message);
3904 ret_error = g_error_new (NM_MANAGER_ERROR,
3905 NM_MANAGER_ERROR_PERMISSION_DENIED,
3906 "Sleep/wake request failed: %s",
3908 g_dbus_method_invocation_take_error (context, ret_error);
3909 } else if (result != NM_AUTH_CALL_RESULT_YES) {
3910 ret_error = g_error_new_literal (NM_MANAGER_ERROR,
3911 NM_MANAGER_ERROR_PERMISSION_DENIED,
3912 "Not authorized to sleep/wake");
3913 g_dbus_method_invocation_take_error (context, ret_error);
3916 do_sleep = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, "sleep"));
3917 _internal_sleep (self, do_sleep);
3918 g_dbus_method_invocation_return_value (context, NULL);
3921 nm_auth_chain_unref (chain);
3926 impl_manager_sleep (NMManager *self,
3927 GDBusMethodInvocation *context,
3930 NMManagerPrivate *priv;
3931 GError *error = NULL;
3932 gs_unref_object NMAuthSubject *subject = NULL;
3935 const char *error_desc = NULL;
3938 g_return_if_fail (NM_IS_MANAGER (self));
3940 priv = NM_MANAGER_GET_PRIVATE (self);
3941 subject = nm_auth_subject_new_unix_process_from_context (context);
3943 if (priv->sleeping == do_sleep) {
3944 error = g_error_new (NM_MANAGER_ERROR,
3945 NM_MANAGER_ERROR_ALREADY_ASLEEP_OR_AWAKE,
3946 "Already %s", do_sleep ? "asleep" : "awake");
3947 nm_audit_log_control_op (NM_AUDIT_OP_SLEEP_CONTROL, do_sleep ? "on" : "off", FALSE, subject,
3949 g_dbus_method_invocation_take_error (context, error);
3953 /* Unconditionally allow the request. Previously it was polkit protected
3954 * but unfortunately that doesn't work for short-lived processes like
3955 * pm-utils. It uses dbus-send without --print-reply, which quits
3956 * immediately after sending the request, and NM is unable to obtain the
3957 * sender's UID as dbus-send has already dropped off the bus. Thus NM
3958 * fails the request. Instead, don't validate the request, but rely on
3959 * D-Bus permissions to restrict the call to root.
3961 _internal_sleep (self, do_sleep);
3962 nm_audit_log_control_op (NM_AUDIT_OP_SLEEP_CONTROL, do_sleep ? "on" : "off", TRUE, subject, NULL);
3963 g_dbus_method_invocation_return_value (context, NULL);
3967 chain = nm_auth_chain_new (context, sleep_auth_done_cb, self, &error_desc);
3969 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
3970 nm_auth_chain_set_data (chain, "sleep", GUINT_TO_POINTER (do_sleep), NULL);
3971 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SLEEP_WAKE, TRUE);
3973 error = g_error_new_literal (NM_MANAGER_ERROR,
3974 NM_MANAGER_ERROR_PERMISSION_DENIED,
3976 g_dbus_method_invocation_take_error (context, error);
3982 sleeping_cb (NMSleepMonitor *monitor, gpointer user_data)
3984 nm_log_dbg (LOGD_SUSPEND, "Received sleeping signal");
3985 _internal_sleep (NM_MANAGER (user_data), TRUE);
3989 resuming_cb (NMSleepMonitor *monitor, gpointer user_data)
3991 nm_log_dbg (LOGD_SUSPEND, "Received resuming signal");
3992 _internal_sleep (NM_MANAGER (user_data), FALSE);
3996 _internal_enable (NMManager *self, gboolean enable)
3998 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4001 /* Update "NetworkingEnabled" key in state file */
4002 if (priv->state_file) {
4003 if (!write_value_to_state_file (priv->state_file,
4004 "main", "NetworkingEnabled",
4005 G_TYPE_BOOLEAN, (gpointer) &enable,
4007 /* Not a hard error */
4008 _LOGW (LOGD_SUSPEND, "writing to state file %s failed: %s",
4014 _LOGI (LOGD_SUSPEND, "%s requested (sleeping: %s enabled: %s)",
4015 enable ? "enable" : "disable",
4016 priv->sleeping ? "yes" : "no",
4017 priv->net_enabled ? "yes" : "no");
4019 priv->net_enabled = enable;
4021 do_sleep_wake (self, FALSE);
4023 g_object_notify (G_OBJECT (self), NM_MANAGER_NETWORKING_ENABLED);
4027 enable_net_done_cb (NMAuthChain *chain,
4029 GDBusMethodInvocation *context,
4032 NMManager *self = NM_MANAGER (user_data);
4033 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4034 GError *ret_error = NULL;
4035 NMAuthCallResult result;
4037 NMAuthSubject *subject;
4041 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
4042 enable = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, "enable"));
4043 subject = nm_auth_chain_get_subject (chain);
4045 result = nm_auth_chain_get_result (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK);
4047 _LOGD (LOGD_CORE, "Enable request failed: %s", error->message);
4048 ret_error = g_error_new (NM_MANAGER_ERROR,
4049 NM_MANAGER_ERROR_PERMISSION_DENIED,
4050 "Enable request failed: %s",
4052 } else if (result != NM_AUTH_CALL_RESULT_YES) {
4053 ret_error = g_error_new_literal (NM_MANAGER_ERROR,
4054 NM_MANAGER_ERROR_PERMISSION_DENIED,
4055 "Not authorized to enable/disable networking");
4058 _internal_enable (self, enable);
4059 g_dbus_method_invocation_return_value (context, NULL);
4060 nm_audit_log_control_op (NM_AUDIT_OP_NET_CONTROL, enable ? "on" : "off", TRUE,
4065 nm_audit_log_control_op (NM_AUDIT_OP_NET_CONTROL, enable ? "on" : "off", FALSE,
4066 subject, ret_error->message);
4067 g_dbus_method_invocation_take_error (context, ret_error);
4070 nm_auth_chain_unref (chain);
4074 impl_manager_enable (NMManager *self,
4075 GDBusMethodInvocation *context,
4078 NMManagerPrivate *priv;
4080 GError *error = NULL;
4082 g_return_if_fail (NM_IS_MANAGER (self));
4084 priv = NM_MANAGER_GET_PRIVATE (self);
4086 if (priv->net_enabled == enable) {
4087 error = g_error_new (NM_MANAGER_ERROR,
4088 NM_MANAGER_ERROR_ALREADY_ENABLED_OR_DISABLED,
4089 "Already %s", enable ? "enabled" : "disabled");
4093 chain = nm_auth_chain_new_context (context, enable_net_done_cb, self);
4095 error = g_error_new_literal (NM_MANAGER_ERROR,
4096 NM_MANAGER_ERROR_PERMISSION_DENIED,
4097 "Unable to authenticate request.");
4101 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
4102 nm_auth_chain_set_data (chain, "enable", GUINT_TO_POINTER (enable), NULL);
4103 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK, TRUE);
4107 g_dbus_method_invocation_take_error (context, error);
4113 get_perm_add_result (NMAuthChain *chain, GVariantBuilder *results, const char *permission)
4115 NMAuthCallResult result;
4117 result = nm_auth_chain_get_result (chain, permission);
4118 if (result == NM_AUTH_CALL_RESULT_YES)
4119 g_variant_builder_add (results, "{ss}", permission, "yes");
4120 else if (result == NM_AUTH_CALL_RESULT_NO)
4121 g_variant_builder_add (results, "{ss}", permission, "no");
4122 else if (result == NM_AUTH_CALL_RESULT_AUTH)
4123 g_variant_builder_add (results, "{ss}", permission, "auth");
4125 nm_log_dbg (LOGD_CORE, "unknown auth chain result %d", result);
4130 get_permissions_done_cb (NMAuthChain *chain,
4132 GDBusMethodInvocation *context,
4135 NMManager *self = NM_MANAGER (user_data);
4136 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4138 GVariantBuilder results;
4142 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
4144 _LOGD (LOGD_CORE, "Permissions request failed: %s", error->message);
4145 ret_error = g_error_new (NM_MANAGER_ERROR,
4146 NM_MANAGER_ERROR_PERMISSION_DENIED,
4147 "Permissions request failed: %s",
4149 g_dbus_method_invocation_take_error (context, ret_error);
4151 g_variant_builder_init (&results, G_VARIANT_TYPE ("a{ss}"));
4153 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK);
4154 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_SLEEP_WAKE);
4155 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI);
4156 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN);
4157 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX);
4158 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_NETWORK_CONTROL);
4159 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED);
4160 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_WIFI_SHARE_OPEN);
4161 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM);
4162 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN);
4163 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME);
4165 g_dbus_method_invocation_return_value (context,
4166 g_variant_new ("(a{ss})", &results));
4169 nm_auth_chain_unref (chain);
4173 impl_manager_get_permissions (NMManager *self,
4174 GDBusMethodInvocation *context)
4176 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4178 GError *error = NULL;
4180 chain = nm_auth_chain_new_context (context, get_permissions_done_cb, self);
4182 error = g_error_new_literal (NM_MANAGER_ERROR,
4183 NM_MANAGER_ERROR_PERMISSION_DENIED,
4184 "Unable to authenticate request.");
4185 g_dbus_method_invocation_take_error (context, error);
4189 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
4190 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK, FALSE);
4191 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SLEEP_WAKE, FALSE);
4192 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI, FALSE);
4193 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN, FALSE);
4194 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX, FALSE);
4195 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_NETWORK_CONTROL, FALSE);
4196 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED, FALSE);
4197 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_WIFI_SHARE_OPEN, FALSE);
4198 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM, FALSE);
4199 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN, FALSE);
4200 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME, FALSE);
4204 impl_manager_get_state (NMManager *self,
4205 GDBusMethodInvocation *context)
4207 nm_manager_update_state (self);
4208 g_dbus_method_invocation_return_value (context,
4209 g_variant_new ("(u)", NM_MANAGER_GET_PRIVATE (self)->state));
4213 impl_manager_set_logging (NMManager *self,
4214 GDBusMethodInvocation *context,
4216 const char *domains)
4218 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4219 GError *error = NULL;
4220 gulong caller_uid = G_MAXULONG;
4222 if (!nm_bus_manager_get_caller_info (priv->dbus_mgr, context, NULL, &caller_uid, NULL)) {
4223 error = g_error_new_literal (NM_MANAGER_ERROR,
4224 NM_MANAGER_ERROR_PERMISSION_DENIED,
4225 "Failed to get request UID.");
4229 if (0 != caller_uid) {
4230 error = g_error_new_literal (NM_MANAGER_ERROR,
4231 NM_MANAGER_ERROR_PERMISSION_DENIED,
4232 "Permission denied");
4236 if (nm_logging_setup (level, domains, NULL, &error)) {
4237 _LOGI (LOGD_CORE, "logging: level '%s' domains '%s'",
4238 nm_logging_level_to_string (), nm_logging_domains_to_string ());
4243 g_dbus_method_invocation_take_error (context, error);
4245 g_dbus_method_invocation_return_value (context, NULL);
4249 impl_manager_get_logging (NMManager *manager,
4250 GDBusMethodInvocation *context)
4252 g_dbus_method_invocation_return_value (context,
4253 g_variant_new ("(ss)",
4254 nm_logging_level_to_string (),
4255 nm_logging_domains_to_string ()));
4259 connectivity_check_done (GObject *object,
4260 GAsyncResult *result,
4263 GDBusMethodInvocation *context = user_data;
4264 NMConnectivityState state;
4265 GError *error = NULL;
4267 state = nm_connectivity_check_finish (NM_CONNECTIVITY (object), result, &error);
4269 g_dbus_method_invocation_take_error (context, error);
4271 g_dbus_method_invocation_return_value (context,
4272 g_variant_new ("(u)", state));
4278 check_connectivity_auth_done_cb (NMAuthChain *chain,
4280 GDBusMethodInvocation *context,
4283 NMManager *self = NM_MANAGER (user_data);
4284 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4285 GError *error = NULL;
4286 NMAuthCallResult result;
4288 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
4290 result = nm_auth_chain_get_result (chain, NM_AUTH_PERMISSION_NETWORK_CONTROL);
4293 _LOGD (LOGD_CORE, "CheckConnectivity request failed: %s", auth_error->message);
4294 error = g_error_new (NM_MANAGER_ERROR,
4295 NM_MANAGER_ERROR_PERMISSION_DENIED,
4296 "Connectivity check request failed: %s",
4297 auth_error->message);
4298 } else if (result != NM_AUTH_CALL_RESULT_YES) {
4299 error = g_error_new_literal (NM_MANAGER_ERROR,
4300 NM_MANAGER_ERROR_PERMISSION_DENIED,
4301 "Not authorized to recheck connectivity");
4304 nm_connectivity_check_async (priv->connectivity,
4305 connectivity_check_done,
4310 g_dbus_method_invocation_take_error (context, error);
4311 nm_auth_chain_unref (chain);
4315 impl_manager_check_connectivity (NMManager *self,
4316 GDBusMethodInvocation *context)
4318 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4320 GError *error = NULL;
4322 /* Validate the request */
4323 chain = nm_auth_chain_new_context (context, check_connectivity_auth_done_cb, self);
4325 error = g_error_new_literal (NM_MANAGER_ERROR,
4326 NM_MANAGER_ERROR_PERMISSION_DENIED,
4327 "Unable to authenticate request.");
4328 g_dbus_method_invocation_take_error (context, error);
4332 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
4333 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_NETWORK_CONTROL, TRUE);
4337 start_factory (NMDeviceFactory *factory, gpointer user_data)
4339 nm_device_factory_start (factory);
4343 nm_manager_start (NMManager *self, GError **error)
4345 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4346 GSList *iter, *connections;
4349 if (!nm_settings_start (priv->settings, error))
4352 g_signal_connect (NM_PLATFORM_GET,
4353 NM_PLATFORM_SIGNAL_LINK_CHANGED,
4354 G_CALLBACK (platform_link_cb),
4357 /* Set initial radio enabled/disabled state */
4358 for (i = 0; i < RFKILL_TYPE_MAX; i++) {
4359 RadioState *rstate = &priv->radio_states[i];
4365 /* recheck kernel rfkill state */
4366 update_rstate_from_rfkill (priv->rfkill_mgr, rstate);
4369 _LOGI (LOGD_RFKILL, "%s %s by radio killswitch; %s by state file",
4371 (rstate->hw_enabled && rstate->sw_enabled) ? "enabled" : "disabled",
4372 rstate->user_enabled ? "enabled" : "disabled");
4374 enabled = radio_enabled_for_rstate (rstate, TRUE);
4375 manager_update_radio_enabled (self, rstate, enabled);
4378 /* Log overall networking status - enabled/disabled */
4379 _LOGI (LOGD_CORE, "Networking is %s by state file",
4380 priv->net_enabled ? "enabled" : "disabled");
4382 system_unmanaged_devices_changed_cb (priv->settings, NULL, self);
4383 system_hostname_changed_cb (priv->settings, NULL, self);
4385 /* Start device factories */
4386 nm_device_factory_manager_load_factories (_register_device_factory, self);
4387 nm_device_factory_manager_for_each_factory (start_factory, NULL);
4389 platform_query_devices (self);
4391 /* Load VPN plugins */
4392 priv->vpn_manager = g_object_ref (nm_vpn_manager_get ());
4394 /* Connections added before the manager is started do not emit
4395 * connection-added signals thus devices have to be created manually.
4397 _LOGD (LOGD_CORE, "creating virtual devices...");
4398 connections = nm_settings_get_connections (priv->settings);
4399 for (iter = connections; iter; iter = iter->next)
4400 connection_changed (priv->settings, NM_CONNECTION (iter->data), self);
4401 g_slist_free (connections);
4403 priv->devices_inited = TRUE;
4405 check_if_startup_complete (self);
4411 nm_manager_stop (NMManager *self)
4413 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4415 /* Remove all devices */
4416 while (priv->devices)
4417 remove_device (self, NM_DEVICE (priv->devices->data), TRUE, TRUE);
4419 _active_connection_cleanup (self);
4423 handle_firmware_changed (gpointer user_data)
4425 NMManager *self = NM_MANAGER (user_data);
4426 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4429 priv->fw_changed_id = 0;
4431 /* Try to re-enable devices with missing firmware */
4432 for (iter = priv->devices; iter; iter = iter->next) {
4433 NMDevice *candidate = NM_DEVICE (iter->data);
4434 NMDeviceState state = nm_device_get_state (candidate);
4436 if ( nm_device_get_firmware_missing (candidate)
4437 && (state == NM_DEVICE_STATE_UNAVAILABLE)) {
4438 _LOGI (LOGD_CORE, "(%s): firmware may now be available",
4439 nm_device_get_iface (candidate));
4441 /* Re-set unavailable state to try bringing the device up again */
4442 nm_device_state_changed (candidate,
4443 NM_DEVICE_STATE_UNAVAILABLE,
4444 NM_DEVICE_STATE_REASON_NONE);
4452 connectivity_changed (NMConnectivity *connectivity,
4456 NMManager *self = NM_MANAGER (user_data);
4458 _LOGD (LOGD_CORE, "connectivity checking indicates %s",
4459 nm_connectivity_state_to_string (nm_connectivity_get_state (connectivity)));
4461 nm_manager_update_state (self);
4462 g_object_notify (G_OBJECT (self), NM_MANAGER_CONNECTIVITY);
4466 firmware_dir_changed (GFileMonitor *monitor,
4469 GFileMonitorEvent event_type,
4472 NMManager *self = NM_MANAGER (user_data);
4473 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4475 switch (event_type) {
4476 case G_FILE_MONITOR_EVENT_CREATED:
4477 case G_FILE_MONITOR_EVENT_CHANGED:
4478 case G_FILE_MONITOR_EVENT_MOVED:
4479 case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
4480 case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
4481 if (!priv->fw_changed_id) {
4482 priv->fw_changed_id = g_timeout_add_seconds (4, handle_firmware_changed, self);
4483 _LOGI (LOGD_CORE, "kernel firmware directory '%s' changed",
4484 KERNEL_FIRMWARE_DIR);
4493 connection_metered_changed (GObject *object,
4497 nm_manager_update_metered (NM_MANAGER (user_data));
4501 policy_default_device_changed (GObject *object, GParamSpec *pspec, gpointer user_data)
4503 NMManager *self = NM_MANAGER (user_data);
4504 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4506 NMActiveConnection *ac;
4508 /* Note: this assumes that it's not possible for the IP4 default
4509 * route to be going over the default-ip6-device. If that changes,
4510 * we need something more complicated here.
4512 best = nm_policy_get_default_ip4_device (priv->policy);
4514 best = nm_policy_get_default_ip6_device (priv->policy);
4517 ac = NM_ACTIVE_CONNECTION (nm_device_get_act_request (best));
4521 if (ac != priv->primary_connection) {
4522 if (priv->primary_connection) {
4523 g_signal_handlers_disconnect_by_func (priv->primary_connection,
4524 G_CALLBACK (connection_metered_changed),
4526 g_clear_object (&priv->primary_connection);
4529 priv->primary_connection = ac ? g_object_ref (ac) : NULL;
4531 if (priv->primary_connection) {
4532 g_signal_connect (priv->primary_connection, NM_ACTIVE_CONNECTION_DEVICE_METERED_CHANGED,
4533 G_CALLBACK (connection_metered_changed), self);
4535 _LOGD (LOGD_CORE, "PrimaryConnection now %s", ac ? nm_active_connection_get_settings_connection_id (ac) : "(none)");
4536 g_object_notify (G_OBJECT (self), NM_MANAGER_PRIMARY_CONNECTION);
4537 g_object_notify (G_OBJECT (self), NM_MANAGER_PRIMARY_CONNECTION_TYPE);
4538 nm_manager_update_metered (self);
4543 policy_activating_device_changed (GObject *object, GParamSpec *pspec, gpointer user_data)
4545 NMManager *self = NM_MANAGER (user_data);
4546 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4547 NMDevice *activating, *best;
4548 NMActiveConnection *ac;
4550 /* We only look at activating-ip6-device if activating-ip4-device
4551 * AND default-ip4-device are NULL; if default-ip4-device is
4552 * non-NULL, then activating-ip6-device is irrelevant, since while
4553 * that device might become the new default-ip6-device, it can't
4554 * become primary-connection while default-ip4-device is set to
4557 activating = nm_policy_get_activating_ip4_device (priv->policy);
4558 best = nm_policy_get_default_ip4_device (priv->policy);
4559 if (!activating && !best)
4560 activating = nm_policy_get_activating_ip6_device (priv->policy);
4563 ac = NM_ACTIVE_CONNECTION (nm_device_get_act_request (activating));
4567 if (ac != priv->activating_connection) {
4568 g_clear_object (&priv->activating_connection);
4569 priv->activating_connection = ac ? g_object_ref (ac) : NULL;
4570 _LOGD (LOGD_CORE, "ActivatingConnection now %s", ac ? nm_active_connection_get_settings_connection_id (ac) : "(none)");
4571 g_object_notify (G_OBJECT (self), NM_MANAGER_ACTIVATING_CONNECTION);
4575 #define NM_PERM_DENIED_ERROR "org.freedesktop.NetworkManager.PermissionDenied"
4579 GDBusConnection *connection;
4580 GDBusMessage *message;
4581 NMAuthSubject *subject;
4582 const char *permission;
4583 const char *audit_op;
4584 char *audit_prop_value;
4585 GType interface_type;
4586 const char *glib_propname;
4587 } PropertyFilterData;
4590 free_property_filter_data (PropertyFilterData *pfd)
4592 g_object_unref (pfd->self);
4593 g_object_unref (pfd->connection);
4594 g_object_unref (pfd->message);
4595 g_clear_object (&pfd->subject);
4596 g_free (pfd->audit_prop_value);
4597 g_slice_free (PropertyFilterData, pfd);
4601 prop_set_auth_done_cb (NMAuthChain *chain,
4603 GDBusMethodInvocation *context, /* NULL */
4606 PropertyFilterData *pfd = user_data;
4607 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (pfd->self);
4608 NMAuthCallResult result;
4609 GDBusMessage *reply = NULL;
4610 const char *error_message;
4611 gs_unref_object NMExportedObject *object = NULL;
4612 const NMGlobalDnsConfig *global_dns;
4613 gs_unref_variant GVariant *value = NULL;
4616 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
4617 result = nm_auth_chain_get_result (chain, pfd->permission);
4618 if (error || (result != NM_AUTH_CALL_RESULT_YES)) {
4619 reply = g_dbus_message_new_method_error (pfd->message,
4620 NM_PERM_DENIED_ERROR,
4621 (error_message = "Not authorized to perform this operation"));
4623 error_message = error->message;
4627 object = NM_EXPORTED_OBJECT (nm_bus_manager_get_registered_object (priv->dbus_mgr,
4628 g_dbus_message_get_path (pfd->message)));
4630 reply = g_dbus_message_new_method_error (pfd->message,
4631 "org.freedesktop.DBus.Error.UnknownObject",
4632 (error_message = "Object doesn't exist."));
4636 /* do some extra type checking... */
4637 if (!nm_exported_object_get_interface_by_type (object, pfd->interface_type)) {
4638 reply = g_dbus_message_new_method_error (pfd->message,
4639 "org.freedesktop.DBus.Error.InvalidArgs",
4640 (error_message = "Object is of unexpected type."));
4644 args = g_dbus_message_get_body (pfd->message);
4645 g_variant_get (args, "(&s&sv)", NULL, NULL, &value);
4646 g_assert (pfd->glib_propname);
4648 if (!strcmp (pfd->glib_propname, NM_MANAGER_GLOBAL_DNS_CONFIGURATION)) {
4649 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("a{sv}")));
4650 global_dns = nm_config_data_get_global_dns_config (nm_config_get_data (priv->config));
4652 if (global_dns && !nm_global_dns_config_is_internal (global_dns)) {
4653 reply = g_dbus_message_new_method_error (pfd->message,
4654 NM_PERM_DENIED_ERROR,
4655 (error_message = "Global DNS configuration already set via configuration file"));
4658 /* ... but set the property on the @object itself. It would be correct to set the property
4659 * on the skeleton interface, but as it is now, the result is the same. */
4660 g_object_set (object, pfd->glib_propname, value, NULL);
4662 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN));
4664 g_object_set (object, pfd->glib_propname, g_variant_get_boolean (value), NULL);
4667 reply = g_dbus_message_new_method_reply (pfd->message);
4668 g_dbus_message_set_body (reply, g_variant_new_tuple (NULL, 0));
4669 error_message = NULL;
4671 nm_audit_log_control_op (pfd->audit_op, pfd->audit_prop_value, !error_message, pfd->subject, error_message);
4673 g_dbus_connection_send_message (pfd->connection, reply,
4674 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
4676 g_object_unref (reply);
4677 nm_auth_chain_unref (chain);
4679 free_property_filter_data (pfd);
4683 do_set_property_check (gpointer user_data)
4685 PropertyFilterData *pfd = user_data;
4686 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (pfd->self);
4687 GDBusMessage *reply = NULL;
4689 const char *error_message = NULL;
4691 pfd->subject = nm_auth_subject_new_unix_process_from_message (pfd->connection, pfd->message);
4692 if (!pfd->subject) {
4693 reply = g_dbus_message_new_method_error (pfd->message,
4694 NM_PERM_DENIED_ERROR,
4695 (error_message = "Could not determine request UID."));
4699 /* Validate the user request */
4700 chain = nm_auth_chain_new_subject (pfd->subject, NULL, prop_set_auth_done_cb, pfd);
4702 reply = g_dbus_message_new_method_error (pfd->message,
4703 NM_PERM_DENIED_ERROR,
4704 (error_message = "Could not authenticate request."));
4708 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
4709 nm_auth_chain_add_call (chain, pfd->permission, TRUE);
4713 nm_audit_log_control_op (pfd->audit_op, pfd->audit_prop_value, FALSE, pfd->subject, error_message);
4714 g_dbus_connection_send_message (pfd->connection, reply,
4715 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
4717 g_object_unref (reply);
4718 free_property_filter_data (pfd);
4724 static GDBusMessage *
4725 prop_filter (GDBusConnection *connection,
4726 GDBusMessage *message,
4730 gs_unref_object NMManager *self = NULL;
4732 const char *propiface = NULL;
4733 const char *propname = NULL;
4734 const char *glib_propname = NULL, *permission = NULL;
4735 const char *audit_op = NULL;
4736 GType interface_type = G_TYPE_INVALID;
4737 PropertyFilterData *pfd;
4738 const GVariantType *expected_type = G_VARIANT_TYPE_BOOLEAN;
4739 gs_unref_variant GVariant *value = NULL;
4741 self = g_weak_ref_get (user_data);
4745 /* The sole purpose of this function is to validate property accesses on the
4746 * NMManager object since gdbus doesn't give us this functionality.
4749 /* Only filter org.freedesktop.DBus.Properties.Set calls */
4751 || g_dbus_message_get_message_type (message) != G_DBUS_MESSAGE_TYPE_METHOD_CALL
4752 || g_strcmp0 (g_dbus_message_get_interface (message), DBUS_INTERFACE_PROPERTIES) != 0
4753 || g_strcmp0 (g_dbus_message_get_member (message), "Set") != 0)
4756 args = g_dbus_message_get_body (message);
4757 if (!g_variant_is_of_type (args, G_VARIANT_TYPE ("(ssv)")))
4759 g_variant_get (args, "(&s&sv)", &propiface, &propname, &value);
4761 /* Only filter calls to filtered properties, on existing objects */
4762 if (!strcmp (propiface, NM_DBUS_INTERFACE)) {
4763 if (!strcmp (propname, "WirelessEnabled")) {
4764 glib_propname = NM_MANAGER_WIRELESS_ENABLED;
4765 permission = NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI;
4766 audit_op = NM_AUDIT_OP_RADIO_CONTROL;
4767 } else if (!strcmp (propname, "WwanEnabled")) {
4768 glib_propname = NM_MANAGER_WWAN_ENABLED;
4769 permission = NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN;
4770 audit_op = NM_AUDIT_OP_RADIO_CONTROL;
4771 } else if (!strcmp (propname, "WimaxEnabled")) {
4772 glib_propname = NM_MANAGER_WIMAX_ENABLED;
4773 permission = NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX;
4774 audit_op = NM_AUDIT_OP_RADIO_CONTROL;
4775 } else if (!strcmp (propname, "GlobalDnsConfiguration")) {
4776 glib_propname = NM_MANAGER_GLOBAL_DNS_CONFIGURATION;
4777 permission = NM_AUTH_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS;
4778 audit_op = NM_AUDIT_OP_NET_CONTROL;
4779 expected_type = G_VARIANT_TYPE ("a{sv}");
4782 interface_type = NMDBUS_TYPE_MANAGER_SKELETON;
4783 } else if (!strcmp (propiface, NM_DBUS_INTERFACE_DEVICE)) {
4784 if (!strcmp (propname, "Autoconnect")) {
4785 glib_propname = NM_DEVICE_AUTOCONNECT;
4786 permission = NM_AUTH_PERMISSION_NETWORK_CONTROL;
4787 audit_op = NM_AUDIT_OP_DEVICE_AUTOCONNECT;
4788 } else if (!strcmp (propname, "Managed")) {
4789 glib_propname = NM_DEVICE_MANAGED;
4790 permission = NM_AUTH_PERMISSION_NETWORK_CONTROL;
4791 audit_op = NM_AUDIT_OP_DEVICE_MANAGED;
4794 interface_type = NMDBUS_TYPE_DEVICE_SKELETON;
4798 if (!g_variant_is_of_type (value, expected_type))
4801 /* This filter function is called from a gdbus worker thread which we can't
4802 * make other D-Bus calls from. In particular, we cannot call
4803 * org.freedesktop.DBus.GetConnectionUnixUser to find the remote UID.
4805 pfd = g_slice_new0 (PropertyFilterData);
4808 pfd->connection = g_object_ref (connection);
4809 pfd->message = message;
4810 pfd->permission = permission;
4811 pfd->interface_type = interface_type;
4812 pfd->glib_propname = glib_propname;
4813 pfd->audit_op = audit_op;
4814 if (g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN)) {
4815 pfd->audit_prop_value = g_strdup_printf ("%s:%d", pfd->glib_propname,
4816 g_variant_get_boolean (value));
4818 pfd->audit_prop_value = g_strdup (pfd->glib_propname);
4820 g_idle_add (do_set_property_check, pfd);
4825 /******************************************************************************/
4828 _set_prop_filter_free2 (gpointer user_data)
4830 g_slice_free (GWeakRef, user_data);
4831 return G_SOURCE_REMOVE;
4835 _set_prop_filter_free (gpointer user_data)
4837 g_weak_ref_clear (user_data);
4839 /* Delay the final deletion of the user_data. There is a race when
4840 * calling g_dbus_connection_remove_filter() that the callback and user_data
4841 * might have been copied and being executed after the destroy function
4842 * runs (bgo #704568).
4843 * This doesn't really fix the race, but it should work well enough. */
4844 g_timeout_add_seconds (2, _set_prop_filter_free2, user_data);
4848 _set_prop_filter (NMManager *self, GDBusConnection *connection)
4850 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4852 nm_assert ((!priv->prop_filter.connection) == (!priv->prop_filter.id));
4854 if (priv->prop_filter.connection == connection)
4857 if (priv->prop_filter.connection) {
4858 g_dbus_connection_remove_filter (priv->prop_filter.connection, priv->prop_filter.id);
4859 priv->prop_filter.id = 0;
4860 g_clear_object (&priv->prop_filter.connection);
4865 wptr = g_slice_new (GWeakRef);
4866 g_weak_ref_init (wptr, self);
4867 priv->prop_filter.id = g_dbus_connection_add_filter (connection, prop_filter, wptr, _set_prop_filter_free);
4868 priv->prop_filter.connection = g_object_ref (connection);
4872 /******************************************************************************/
4875 authority_changed_cb (NMAuthManager *auth_manager, gpointer user_data)
4877 /* Let clients know they should re-check their authorization */
4878 g_signal_emit (NM_MANAGER (user_data), signals[CHECK_PERMISSIONS], 0);
4881 #define KERN_RFKILL_OP_CHANGE_ALL 3
4882 #define KERN_RFKILL_TYPE_WLAN 1
4883 #define KERN_RFKILL_TYPE_WWAN 5
4884 struct rfkill_event {
4889 } __attribute__((packed));
4892 rfkill_change (const char *desc, RfKillType rtype, gboolean enabled)
4895 struct rfkill_event event;
4898 g_return_if_fail (rtype == RFKILL_TYPE_WLAN || rtype == RFKILL_TYPE_WWAN);
4901 fd = open ("/dev/rfkill", O_RDWR);
4903 if (errno == EACCES)
4904 nm_log_warn (LOGD_RFKILL, "(%s): failed to open killswitch device", desc);
4908 if (fcntl (fd, F_SETFL, O_NONBLOCK) < 0) {
4909 nm_log_warn (LOGD_RFKILL, "(%s): failed to set killswitch device for "
4910 "non-blocking operation", desc);
4915 memset (&event, 0, sizeof (event));
4916 event.op = KERN_RFKILL_OP_CHANGE_ALL;
4918 case RFKILL_TYPE_WLAN:
4919 event.type = KERN_RFKILL_TYPE_WLAN;
4921 case RFKILL_TYPE_WWAN:
4922 event.type = KERN_RFKILL_TYPE_WWAN;
4925 g_assert_not_reached ();
4927 event.soft = enabled ? 0 : 1;
4929 len = write (fd, &event, sizeof (event));
4931 nm_log_warn (LOGD_RFKILL, "(%s): failed to change WiFi killswitch state: (%d) %s",
4932 desc, errno, g_strerror (errno));
4933 } else if (len == sizeof (event)) {
4934 nm_log_info (LOGD_RFKILL, "%s hardware radio set %s",
4935 desc, enabled ? "enabled" : "disabled");
4937 /* Failed to write full structure */
4938 nm_log_warn (LOGD_RFKILL, "(%s): failed to change WiFi killswitch state", desc);
4945 manager_radio_user_toggled (NMManager *self,
4949 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4950 GError *error = NULL;
4951 gboolean old_enabled, new_enabled;
4953 /* Don't touch devices if asleep/networking disabled */
4954 if (manager_sleeping (self))
4958 _LOGD (LOGD_RFKILL, "(%s): setting radio %s by user",
4960 enabled ? "enabled" : "disabled");
4963 /* Update enabled key in state file */
4964 if (priv->state_file) {
4965 if (!write_value_to_state_file (priv->state_file,
4966 "main", rstate->key,
4967 G_TYPE_BOOLEAN, (gpointer) &enabled,
4969 _LOGW (LOGD_CORE, "writing to state file %s failed: %s",
4972 g_clear_error (&error);
4976 /* When the user toggles the radio, their request should override any
4977 * daemon (like ModemManager) enabled state that can be changed. For WWAN
4978 * for example, we want the WwanEnabled property to reflect the daemon state
4979 * too so that users can toggle the modem powered, but we don't want that
4980 * daemon state to affect whether or not the user *can* turn it on, which is
4981 * what the kernel rfkill state does. So we ignore daemon enabled state
4982 * when determining what the new state should be since it shouldn't block
4983 * the user's request.
4985 old_enabled = radio_enabled_for_rstate (rstate, TRUE);
4986 rstate->user_enabled = enabled;
4987 new_enabled = radio_enabled_for_rstate (rstate, FALSE);
4988 if (new_enabled != old_enabled) {
4989 /* Try to change the kernel rfkill state */
4990 if (rstate->rtype == RFKILL_TYPE_WLAN || rstate->rtype == RFKILL_TYPE_WWAN)
4991 rfkill_change (rstate->desc, rstate->rtype, new_enabled);
4993 manager_update_radio_enabled (self, rstate, new_enabled);
4998 periodic_update_active_connection_timestamps (gpointer user_data)
5000 NMManager *manager = NM_MANAGER (user_data);
5001 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
5004 for (iter = priv->active_connections; iter; iter = g_slist_next (iter)) {
5005 NMActiveConnection *ac = iter->data;
5006 NMSettingsConnection *connection;
5008 if (nm_active_connection_get_state (ac) == NM_ACTIVE_CONNECTION_STATE_ACTIVATED) {
5009 connection = nm_active_connection_get_settings_connection (ac);
5010 nm_settings_connection_update_timestamp (connection, (guint64) time (NULL), FALSE);
5018 dbus_connection_changed_cb (NMBusManager *dbus_mgr,
5019 GDBusConnection *connection,
5022 _set_prop_filter (NM_MANAGER (user_data), connection);
5025 /**********************************************************************/
5027 NM_DEFINE_SINGLETON_REGISTER (NMManager);
5030 nm_manager_get (void)
5032 g_return_val_if_fail (singleton_instance, NULL);
5033 return singleton_instance;
5036 NMConnectionProvider *
5037 nm_connection_provider_get (void)
5039 NMConnectionProvider *p;
5041 g_return_val_if_fail (singleton_instance, NULL);
5043 p = NM_CONNECTION_PROVIDER (NM_MANAGER_GET_PRIVATE (singleton_instance)->settings);
5044 g_return_val_if_fail (p, NULL);
5049 nm_manager_setup (const char *state_file,
5050 gboolean initial_net_enabled,
5051 gboolean initial_wifi_enabled,
5052 gboolean initial_wwan_enabled)
5056 g_return_val_if_fail (!singleton_instance, singleton_instance);
5058 self = g_object_new (NM_TYPE_MANAGER,
5059 NM_MANAGER_NETWORKING_ENABLED, initial_net_enabled,
5060 NM_MANAGER_WIRELESS_ENABLED, initial_wifi_enabled,
5061 NM_MANAGER_WWAN_ENABLED, initial_wwan_enabled,
5062 NM_MANAGER_STATE_FILE, state_file,
5064 nm_assert (NM_IS_MANAGER (self));
5065 singleton_instance = self;
5067 nm_singleton_instance_register ();
5068 _LOGD (LOGD_CORE, "setup %s singleton (%p)", "NMManager", singleton_instance);
5070 nm_exported_object_export ((NMExportedObject *) self);
5076 constructed (GObject *object)
5078 NMManager *self = NM_MANAGER (object);
5079 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
5080 NMConfigData *config_data;
5082 G_OBJECT_CLASS (nm_manager_parent_class)->constructed (object);
5084 _set_prop_filter (self, nm_bus_manager_get_connection (priv->dbus_mgr));
5086 priv->settings = nm_settings_new ();
5087 g_signal_connect (priv->settings, "notify::" NM_SETTINGS_STARTUP_COMPLETE,
5088 G_CALLBACK (settings_startup_complete_changed), self);
5089 g_signal_connect (priv->settings, "notify::" NM_SETTINGS_UNMANAGED_SPECS,
5090 G_CALLBACK (system_unmanaged_devices_changed_cb), self);
5091 g_signal_connect (priv->settings, "notify::" NM_SETTINGS_HOSTNAME,
5092 G_CALLBACK (system_hostname_changed_cb), self);
5093 g_signal_connect (priv->settings, NM_SETTINGS_SIGNAL_CONNECTION_ADDED,
5094 G_CALLBACK (connection_changed), self);
5095 g_signal_connect (priv->settings, NM_SETTINGS_SIGNAL_CONNECTION_UPDATED_BY_USER,
5096 G_CALLBACK (connection_changed), self);
5097 g_signal_connect (priv->settings, NM_SETTINGS_SIGNAL_CONNECTION_REMOVED,
5098 G_CALLBACK (connection_removed), self);
5100 priv->policy = nm_policy_new (self, priv->settings);
5101 g_signal_connect (priv->policy, "notify::" NM_POLICY_DEFAULT_IP4_DEVICE,
5102 G_CALLBACK (policy_default_device_changed), self);
5103 g_signal_connect (priv->policy, "notify::" NM_POLICY_DEFAULT_IP6_DEVICE,
5104 G_CALLBACK (policy_default_device_changed), self);
5105 g_signal_connect (priv->policy, "notify::" NM_POLICY_ACTIVATING_IP4_DEVICE,
5106 G_CALLBACK (policy_activating_device_changed), self);
5107 g_signal_connect (priv->policy, "notify::" NM_POLICY_ACTIVATING_IP6_DEVICE,
5108 G_CALLBACK (policy_activating_device_changed), self);
5110 priv->config = g_object_ref (nm_config_get ());
5111 g_signal_connect (G_OBJECT (priv->config),
5112 NM_CONFIG_SIGNAL_CONFIG_CHANGED,
5113 G_CALLBACK (_config_changed_cb),
5116 config_data = nm_config_get_data (priv->config);
5117 priv->connectivity = nm_connectivity_new (nm_config_data_get_connectivity_uri (config_data),
5118 nm_config_data_get_connectivity_interval (config_data),
5119 nm_config_data_get_connectivity_response (config_data));
5120 g_signal_connect (priv->connectivity, "notify::" NM_CONNECTIVITY_STATE,
5121 G_CALLBACK (connectivity_changed), self);
5123 priv->rfkill_mgr = nm_rfkill_manager_new ();
5124 g_signal_connect (priv->rfkill_mgr,
5126 G_CALLBACK (rfkill_manager_rfkill_changed_cb),
5129 /* Force kernel WiFi/WWAN rfkill state to follow NM saved WiFi/WWAN state
5130 * in case the BIOS doesn't save rfkill state, and to be consistent with user
5131 * changes to the WirelessEnabled/WWANEnabled properties which toggle kernel
5134 rfkill_change (priv->radio_states[RFKILL_TYPE_WLAN].desc, RFKILL_TYPE_WLAN, priv->radio_states[RFKILL_TYPE_WLAN].user_enabled);
5135 rfkill_change (priv->radio_states[RFKILL_TYPE_WWAN].desc, RFKILL_TYPE_WWAN, priv->radio_states[RFKILL_TYPE_WWAN].user_enabled);
5139 nm_manager_init (NMManager *self)
5141 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
5145 /* Initialize rfkill structures and states */
5146 memset (priv->radio_states, 0, sizeof (priv->radio_states));
5148 priv->radio_states[RFKILL_TYPE_WLAN].user_enabled = TRUE;
5149 priv->radio_states[RFKILL_TYPE_WLAN].key = "WirelessEnabled";
5150 priv->radio_states[RFKILL_TYPE_WLAN].prop = NM_MANAGER_WIRELESS_ENABLED;
5151 priv->radio_states[RFKILL_TYPE_WLAN].hw_prop = NM_MANAGER_WIRELESS_HARDWARE_ENABLED;
5152 priv->radio_states[RFKILL_TYPE_WLAN].desc = "WiFi";
5153 priv->radio_states[RFKILL_TYPE_WLAN].rtype = RFKILL_TYPE_WLAN;
5155 priv->radio_states[RFKILL_TYPE_WWAN].user_enabled = TRUE;
5156 priv->radio_states[RFKILL_TYPE_WWAN].key = "WWANEnabled";
5157 priv->radio_states[RFKILL_TYPE_WWAN].prop = NM_MANAGER_WWAN_ENABLED;
5158 priv->radio_states[RFKILL_TYPE_WWAN].hw_prop = NM_MANAGER_WWAN_HARDWARE_ENABLED;
5159 priv->radio_states[RFKILL_TYPE_WWAN].desc = "WWAN";
5160 priv->radio_states[RFKILL_TYPE_WWAN].rtype = RFKILL_TYPE_WWAN;
5162 for (i = 0; i < RFKILL_TYPE_MAX; i++)
5163 priv->radio_states[i].hw_enabled = TRUE;
5165 priv->sleeping = FALSE;
5166 priv->state = NM_STATE_DISCONNECTED;
5167 priv->startup = TRUE;
5169 priv->dbus_mgr = g_object_ref (nm_bus_manager_get ());
5170 g_signal_connect (priv->dbus_mgr,
5171 NM_BUS_MANAGER_DBUS_CONNECTION_CHANGED,
5172 G_CALLBACK (dbus_connection_changed_cb),
5175 /* sleep/wake handling */
5176 priv->sleep_monitor = g_object_ref (nm_sleep_monitor_get ());
5177 g_signal_connect (priv->sleep_monitor, NM_SLEEP_MONITOR_SLEEPING,
5178 G_CALLBACK (sleeping_cb), self);
5179 g_signal_connect (priv->sleep_monitor, NM_SLEEP_MONITOR_RESUMING,
5180 G_CALLBACK (resuming_cb), self);
5182 /* Listen for authorization changes */
5183 g_signal_connect (nm_auth_manager_get (),
5184 NM_AUTH_MANAGER_SIGNAL_CHANGED,
5185 G_CALLBACK (authority_changed_cb),
5189 /* Monitor the firmware directory */
5190 if (strlen (KERNEL_FIRMWARE_DIR)) {
5191 file = g_file_new_for_path (KERNEL_FIRMWARE_DIR "/");
5192 priv->fw_monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE, NULL, NULL);
5193 g_object_unref (file);
5196 if (priv->fw_monitor) {
5197 g_signal_connect (priv->fw_monitor, "changed",
5198 G_CALLBACK (firmware_dir_changed),
5200 _LOGI (LOGD_CORE, "monitoring kernel firmware directory '%s'.",
5201 KERNEL_FIRMWARE_DIR);
5203 _LOGW (LOGD_CORE, "failed to monitor kernel firmware directory '%s'.",
5204 KERNEL_FIRMWARE_DIR);
5207 /* Update timestamps in active connections */
5208 priv->timestamp_update_id = g_timeout_add_seconds (300, (GSourceFunc) periodic_update_active_connection_timestamps, self);
5210 priv->metered = NM_METERED_UNKNOWN;
5214 device_is_real (GObject *device, gpointer user_data)
5216 return nm_device_is_real (NM_DEVICE (device));
5220 get_property (GObject *object, guint prop_id,
5221 GValue *value, GParamSpec *pspec)
5223 NMManager *self = NM_MANAGER (object);
5224 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
5225 NMConfigData *config_data;
5226 const NMGlobalDnsConfig *dns_config;
5231 g_value_set_string (value, VERSION);
5234 nm_manager_update_state (self);
5235 g_value_set_uint (value, priv->state);
5238 g_value_set_boolean (value, priv->startup);
5240 case PROP_NETWORKING_ENABLED:
5241 g_value_set_boolean (value, priv->net_enabled);
5243 case PROP_WIRELESS_ENABLED:
5244 g_value_set_boolean (value, radio_enabled_for_type (self, RFKILL_TYPE_WLAN, TRUE));
5246 case PROP_WIRELESS_HARDWARE_ENABLED:
5247 g_value_set_boolean (value, priv->radio_states[RFKILL_TYPE_WLAN].hw_enabled);
5249 case PROP_WWAN_ENABLED:
5250 g_value_set_boolean (value, radio_enabled_for_type (self, RFKILL_TYPE_WWAN, TRUE));
5252 case PROP_WWAN_HARDWARE_ENABLED:
5253 g_value_set_boolean (value, priv->radio_states[RFKILL_TYPE_WWAN].hw_enabled);
5255 case PROP_WIMAX_ENABLED:
5256 g_value_set_boolean (value, FALSE);
5258 case PROP_WIMAX_HARDWARE_ENABLED:
5259 g_value_set_boolean (value, FALSE);
5261 case PROP_ACTIVE_CONNECTIONS:
5262 nm_utils_g_value_set_object_path_array (value, priv->active_connections, NULL, NULL);
5264 case PROP_CONNECTIVITY:
5265 g_value_set_uint (value, nm_connectivity_get_state (priv->connectivity));
5267 case PROP_PRIMARY_CONNECTION:
5268 nm_utils_g_value_set_object_path (value, priv->primary_connection);
5270 case PROP_PRIMARY_CONNECTION_TYPE:
5272 if (priv->primary_connection) {
5275 con = nm_active_connection_get_applied_connection (priv->primary_connection);
5277 type = nm_connection_get_connection_type (con);
5279 g_value_set_string (value, type ? type : "");
5281 case PROP_ACTIVATING_CONNECTION:
5282 nm_utils_g_value_set_object_path (value, priv->activating_connection);
5285 g_value_set_string (value, priv->hostname);
5288 g_value_set_boolean (value, priv->sleeping);
5291 nm_utils_g_value_set_object_path_array (value, priv->devices, device_is_real, NULL);
5294 g_value_set_uint (value, priv->metered);
5296 case PROP_GLOBAL_DNS_CONFIGURATION:
5297 config_data = nm_config_get_data (priv->config);
5298 dns_config = nm_config_data_get_global_dns_config (config_data);
5299 nm_global_dns_config_to_dbus (dns_config, value);
5301 case PROP_ALL_DEVICES:
5302 nm_utils_g_value_set_object_path_array (value, priv->devices, NULL, NULL);
5305 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5311 set_property (GObject *object, guint prop_id,
5312 const GValue *value, GParamSpec *pspec)
5314 NMManager *self = NM_MANAGER (object);
5315 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
5316 NMGlobalDnsConfig *dns_config;
5317 GError *error = NULL;
5320 case PROP_STATE_FILE:
5321 /* construct-only */
5322 priv->state_file = g_value_dup_string (value);
5324 case PROP_NETWORKING_ENABLED:
5325 /* construct-only */
5326 priv->net_enabled = g_value_get_boolean (value);
5328 case PROP_WIRELESS_ENABLED:
5329 if (!priv->rfkill_mgr) {
5330 /* called during object construction. */
5331 priv->radio_states[RFKILL_TYPE_WLAN].user_enabled = g_value_get_boolean (value);
5333 manager_radio_user_toggled (NM_MANAGER (object),
5334 &priv->radio_states[RFKILL_TYPE_WLAN],
5335 g_value_get_boolean (value));
5338 case PROP_WWAN_ENABLED:
5339 if (!priv->rfkill_mgr) {
5340 /* called during object construction. */
5341 priv->radio_states[RFKILL_TYPE_WWAN].user_enabled = g_value_get_boolean (value);
5343 manager_radio_user_toggled (NM_MANAGER (object),
5344 &priv->radio_states[RFKILL_TYPE_WWAN],
5345 g_value_get_boolean (value));
5348 case PROP_WIMAX_ENABLED:
5349 /* WIMAX is depreacted. This does nothing. */
5351 case PROP_GLOBAL_DNS_CONFIGURATION:
5352 dns_config = nm_global_dns_config_from_dbus (value, &error);
5354 nm_config_set_global_dns (priv->config, dns_config, &error);
5356 nm_global_dns_config_free (dns_config);
5359 _LOGD (LOGD_CORE, "set global DNS failed with error: %s", error->message);
5360 g_error_free (error);
5364 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5370 _deinit_device_factory (NMDeviceFactory *factory, gpointer user_data)
5372 g_signal_handlers_disconnect_matched (factory, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, NM_MANAGER (user_data));
5376 dispose (GObject *object)
5378 NMManager *manager = NM_MANAGER (object);
5379 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
5381 g_slist_free_full (priv->auth_chains, (GDestroyNotify) nm_auth_chain_unref);
5382 priv->auth_chains = NULL;
5384 g_signal_handlers_disconnect_by_func (nm_auth_manager_get (),
5385 G_CALLBACK (authority_changed_cb),
5388 g_assert (priv->devices == NULL);
5390 nm_clear_g_source (&priv->ac_cleanup_id);
5392 while (priv->active_connections)
5393 active_connection_remove (manager, NM_ACTIVE_CONNECTION (priv->active_connections->data));
5394 g_clear_pointer (&priv->active_connections, g_slist_free);
5395 g_clear_object (&priv->primary_connection);
5396 g_clear_object (&priv->activating_connection);
5399 g_signal_handlers_disconnect_by_func (priv->config, _config_changed_cb, manager);
5400 g_clear_object (&priv->config);
5402 if (priv->connectivity) {
5403 g_signal_handlers_disconnect_by_func (priv->connectivity, connectivity_changed, manager);
5404 g_clear_object (&priv->connectivity);
5407 g_free (priv->hostname);
5410 g_signal_handlers_disconnect_by_func (priv->policy, policy_default_device_changed, manager);
5411 g_signal_handlers_disconnect_by_func (priv->policy, policy_activating_device_changed, manager);
5412 g_clear_object (&priv->policy);
5415 if (priv->settings) {
5416 g_signal_handlers_disconnect_by_func (priv->settings, settings_startup_complete_changed, manager);
5417 g_signal_handlers_disconnect_by_func (priv->settings, system_unmanaged_devices_changed_cb, manager);
5418 g_signal_handlers_disconnect_by_func (priv->settings, system_hostname_changed_cb, manager);
5419 g_signal_handlers_disconnect_by_func (priv->settings, connection_changed, manager);
5420 g_signal_handlers_disconnect_by_func (priv->settings, connection_removed, manager);
5421 g_clear_object (&priv->settings);
5424 g_clear_pointer (&priv->state_file, g_free);
5425 g_clear_object (&priv->vpn_manager);
5427 /* Unregister property filter */
5428 if (priv->dbus_mgr) {
5429 g_signal_handlers_disconnect_by_func (priv->dbus_mgr, dbus_connection_changed_cb, manager);
5430 g_clear_object (&priv->dbus_mgr);
5432 _set_prop_filter (manager, NULL);
5434 if (priv->sleep_monitor) {
5435 g_signal_handlers_disconnect_by_func (priv->sleep_monitor, sleeping_cb, manager);
5436 g_signal_handlers_disconnect_by_func (priv->sleep_monitor, resuming_cb, manager);
5437 g_clear_object (&priv->sleep_monitor);
5440 if (priv->fw_monitor) {
5441 g_signal_handlers_disconnect_by_func (priv->fw_monitor, firmware_dir_changed, manager);
5443 nm_clear_g_source (&priv->fw_changed_id);
5445 g_file_monitor_cancel (priv->fw_monitor);
5446 g_clear_object (&priv->fw_monitor);
5449 if (priv->rfkill_mgr) {
5450 g_signal_handlers_disconnect_by_func (priv->rfkill_mgr, rfkill_manager_rfkill_changed_cb, manager);
5451 g_clear_object (&priv->rfkill_mgr);
5454 nm_device_factory_manager_for_each_factory (_deinit_device_factory, manager);
5456 nm_clear_g_source (&priv->timestamp_update_id);
5458 G_OBJECT_CLASS (nm_manager_parent_class)->dispose (object);
5462 nm_manager_class_init (NMManagerClass *manager_class)
5464 GObjectClass *object_class = G_OBJECT_CLASS (manager_class);
5465 NMExportedObjectClass *exported_object_class = NM_EXPORTED_OBJECT_CLASS (manager_class);
5467 g_type_class_add_private (manager_class, sizeof (NMManagerPrivate));
5469 exported_object_class->export_path = NM_DBUS_PATH;
5471 /* virtual methods */
5472 object_class->constructed = constructed;
5473 object_class->set_property = set_property;
5474 object_class->get_property = get_property;
5475 object_class->dispose = dispose;
5478 g_object_class_install_property
5479 (object_class, PROP_VERSION,
5480 g_param_spec_string (NM_MANAGER_VERSION, "", "",
5483 G_PARAM_STATIC_STRINGS));
5485 g_object_class_install_property (object_class,
5487 g_param_spec_string (NM_MANAGER_STATE_FILE, "", "",
5490 G_PARAM_CONSTRUCT_ONLY |
5491 G_PARAM_STATIC_STRINGS));
5493 g_object_class_install_property
5494 (object_class, PROP_STATE,
5495 g_param_spec_uint (NM_MANAGER_STATE, "", "",
5496 0, NM_STATE_DISCONNECTED, 0,
5498 G_PARAM_STATIC_STRINGS));
5500 g_object_class_install_property
5501 (object_class, PROP_STARTUP,
5502 g_param_spec_boolean (NM_MANAGER_STARTUP, "", "",
5505 G_PARAM_STATIC_STRINGS));
5507 g_object_class_install_property
5508 (object_class, PROP_NETWORKING_ENABLED,
5509 g_param_spec_boolean (NM_MANAGER_NETWORKING_ENABLED, "", "",
5511 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
5512 G_PARAM_STATIC_STRINGS));
5514 g_object_class_install_property
5515 (object_class, PROP_WIRELESS_ENABLED,
5516 g_param_spec_boolean (NM_MANAGER_WIRELESS_ENABLED, "", "",
5520 G_PARAM_STATIC_STRINGS));
5522 g_object_class_install_property
5523 (object_class, PROP_WIRELESS_HARDWARE_ENABLED,
5524 g_param_spec_boolean (NM_MANAGER_WIRELESS_HARDWARE_ENABLED, "", "",
5527 G_PARAM_STATIC_STRINGS));
5529 g_object_class_install_property
5530 (object_class, PROP_WWAN_ENABLED,
5531 g_param_spec_boolean (NM_MANAGER_WWAN_ENABLED, "", "",
5535 G_PARAM_STATIC_STRINGS));
5537 g_object_class_install_property
5538 (object_class, PROP_WWAN_HARDWARE_ENABLED,
5539 g_param_spec_boolean (NM_MANAGER_WWAN_HARDWARE_ENABLED, "", "",
5542 G_PARAM_STATIC_STRINGS));
5544 g_object_class_install_property
5545 (object_class, PROP_WIMAX_ENABLED,
5546 g_param_spec_boolean (NM_MANAGER_WIMAX_ENABLED, "", "",
5549 G_PARAM_STATIC_STRINGS));
5551 g_object_class_install_property
5552 (object_class, PROP_WIMAX_HARDWARE_ENABLED,
5553 g_param_spec_boolean (NM_MANAGER_WIMAX_HARDWARE_ENABLED, "", "",
5556 G_PARAM_STATIC_STRINGS));
5558 g_object_class_install_property
5559 (object_class, PROP_ACTIVE_CONNECTIONS,
5560 g_param_spec_boxed (NM_MANAGER_ACTIVE_CONNECTIONS, "", "",
5563 G_PARAM_STATIC_STRINGS));
5565 g_object_class_install_property
5566 (object_class, PROP_CONNECTIVITY,
5567 g_param_spec_uint (NM_MANAGER_CONNECTIVITY, "", "",
5568 NM_CONNECTIVITY_UNKNOWN, NM_CONNECTIVITY_FULL, NM_CONNECTIVITY_UNKNOWN,
5570 G_PARAM_STATIC_STRINGS));
5572 g_object_class_install_property
5573 (object_class, PROP_PRIMARY_CONNECTION,
5574 g_param_spec_string (NM_MANAGER_PRIMARY_CONNECTION, "", "",
5577 G_PARAM_STATIC_STRINGS));
5579 g_object_class_install_property
5580 (object_class, PROP_PRIMARY_CONNECTION_TYPE,
5581 g_param_spec_string (NM_MANAGER_PRIMARY_CONNECTION_TYPE, "", "",
5584 G_PARAM_STATIC_STRINGS));
5587 g_object_class_install_property
5588 (object_class, PROP_ACTIVATING_CONNECTION,
5589 g_param_spec_string (NM_MANAGER_ACTIVATING_CONNECTION, "", "",
5592 G_PARAM_STATIC_STRINGS));
5594 /* Hostname is not exported over D-Bus */
5595 g_object_class_install_property
5596 (object_class, PROP_HOSTNAME,
5597 g_param_spec_string (NM_MANAGER_HOSTNAME, "", "",
5600 G_PARAM_STATIC_STRINGS));
5602 /* Sleeping is not exported over D-Bus */
5603 g_object_class_install_property
5604 (object_class, PROP_SLEEPING,
5605 g_param_spec_boolean (NM_MANAGER_SLEEPING, "", "",
5608 G_PARAM_STATIC_STRINGS));
5610 g_object_class_install_property
5611 (object_class, PROP_DEVICES,
5612 g_param_spec_boxed (NM_MANAGER_DEVICES, "", "",
5615 G_PARAM_STATIC_STRINGS));
5618 * NMManager:metered:
5620 * Whether the connectivity is metered.
5624 g_object_class_install_property
5625 (object_class, PROP_METERED,
5626 g_param_spec_uint (NM_MANAGER_METERED, "", "",
5627 0, G_MAXUINT32, NM_METERED_UNKNOWN,
5629 G_PARAM_STATIC_STRINGS));
5632 * NMManager:global-dns-configuration:
5634 * The global DNS configuration.
5638 g_object_class_install_property
5639 (object_class, PROP_GLOBAL_DNS_CONFIGURATION,
5640 g_param_spec_variant (NM_MANAGER_GLOBAL_DNS_CONFIGURATION, "", "",
5641 G_VARIANT_TYPE ("a{sv}"),
5644 G_PARAM_STATIC_STRINGS));
5647 * NMManager:all-devices:
5649 * All devices, including those that are not realized.
5653 g_object_class_install_property
5654 (object_class, PROP_ALL_DEVICES,
5655 g_param_spec_boxed (NM_MANAGER_ALL_DEVICES, "", "",
5658 G_PARAM_STATIC_STRINGS));
5662 /* D-Bus exported; emitted only for realized devices */
5663 signals[DEVICE_ADDED] =
5664 g_signal_new ("device-added",
5665 G_OBJECT_CLASS_TYPE (object_class),
5667 G_STRUCT_OFFSET (NMManagerClass, device_added),
5669 G_TYPE_NONE, 1, NM_TYPE_DEVICE);
5671 /* Emitted for both realized devices and placeholder devices */
5672 signals[INTERNAL_DEVICE_ADDED] =
5673 g_signal_new ("internal-device-added",
5674 G_OBJECT_CLASS_TYPE (object_class),
5675 G_SIGNAL_RUN_FIRST, 0,
5677 G_TYPE_NONE, 1, G_TYPE_OBJECT);
5679 /* D-Bus exported; emitted only for realized devices */
5680 signals[DEVICE_REMOVED] =
5681 g_signal_new ("device-removed",
5682 G_OBJECT_CLASS_TYPE (object_class),
5684 G_STRUCT_OFFSET (NMManagerClass, device_removed),
5686 G_TYPE_NONE, 1, NM_TYPE_DEVICE);
5688 /* Emitted for both realized devices and placeholder devices */
5689 signals[INTERNAL_DEVICE_REMOVED] =
5690 g_signal_new ("internal-device-removed",
5691 G_OBJECT_CLASS_TYPE (object_class),
5692 G_SIGNAL_RUN_FIRST, 0,
5694 G_TYPE_NONE, 1, G_TYPE_OBJECT);
5696 signals[STATE_CHANGED] =
5697 g_signal_new (NM_MANAGER_STATE_CHANGED,
5698 G_OBJECT_CLASS_TYPE (object_class),
5700 G_STRUCT_OFFSET (NMManagerClass, state_changed),
5702 G_TYPE_NONE, 1, G_TYPE_UINT);
5704 signals[CHECK_PERMISSIONS] =
5705 g_signal_new ("check-permissions",
5706 G_OBJECT_CLASS_TYPE (object_class),
5708 0, NULL, NULL, NULL,
5711 signals[USER_PERMISSIONS_CHANGED] =
5712 g_signal_new ("user-permissions-changed",
5713 G_OBJECT_CLASS_TYPE (object_class),
5715 0, NULL, NULL, NULL,
5718 signals[ACTIVE_CONNECTION_ADDED] =
5719 g_signal_new (NM_MANAGER_ACTIVE_CONNECTION_ADDED,
5720 G_OBJECT_CLASS_TYPE (object_class),
5722 0, NULL, NULL, NULL,
5723 G_TYPE_NONE, 1, NM_TYPE_ACTIVE_CONNECTION);
5725 signals[ACTIVE_CONNECTION_REMOVED] =
5726 g_signal_new (NM_MANAGER_ACTIVE_CONNECTION_REMOVED,
5727 G_OBJECT_CLASS_TYPE (object_class),
5729 0, NULL, NULL, NULL,
5730 G_TYPE_NONE, 1, NM_TYPE_ACTIVE_CONNECTION);
5732 signals[CONFIGURE_QUIT] =
5733 g_signal_new (NM_MANAGER_CONFIGURE_QUIT,
5734 G_OBJECT_CLASS_TYPE (object_class),
5736 0, NULL, NULL, NULL,
5739 nm_exported_object_class_add_interface (NM_EXPORTED_OBJECT_CLASS (manager_class),
5740 NMDBUS_TYPE_MANAGER_SKELETON,
5741 "GetDevices", impl_manager_get_devices,
5742 "GetAllDevices", impl_manager_get_all_devices,
5743 "GetDeviceByIpIface", impl_manager_get_device_by_ip_iface,
5744 "ActivateConnection", impl_manager_activate_connection,
5745 "AddAndActivateConnection", impl_manager_add_and_activate_connection,
5746 "DeactivateConnection", impl_manager_deactivate_connection,
5747 "Sleep", impl_manager_sleep,
5748 "Enable", impl_manager_enable,
5749 "GetPermissions", impl_manager_get_permissions,
5750 "SetLogging", impl_manager_set_logging,
5751 "GetLogging", impl_manager_get_logging,
5752 "CheckConnectivity", impl_manager_check_connectivity,
5753 "state", impl_manager_get_state,