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,
229 static void active_connection_parent_active (NMActiveConnection *active,
230 NMActiveConnection *parent_ac,
233 /* Returns: whether to notify D-Bus of the removal or not */
235 active_connection_remove (NMManager *self, NMActiveConnection *active)
237 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
238 gboolean notify = nm_exported_object_is_exported (NM_EXPORTED_OBJECT (active));
241 /* FIXME: switch to a GList for faster removal */
242 found = g_slist_find (priv->active_connections, active);
244 NMSettingsConnection *connection;
246 priv->active_connections = g_slist_remove (priv->active_connections, active);
247 g_signal_emit (self, signals[ACTIVE_CONNECTION_REMOVED], 0, active);
248 g_signal_handlers_disconnect_by_func (active, active_connection_state_changed, self);
249 g_signal_handlers_disconnect_by_func (active, active_connection_default_changed, self);
250 g_signal_handlers_disconnect_by_func (active, active_connection_parent_active, self);
252 if ( nm_active_connection_get_assumed (active)
253 && (connection = nm_active_connection_get_settings_connection (active))
254 && nm_settings_connection_get_nm_generated_assumed (connection))
255 g_object_ref (connection);
259 nm_exported_object_clear_and_unexport (&active);
262 && nm_settings_has_connection (priv->settings, connection)) {
263 _LOGD (LOGD_DEVICE, "assumed connection disconnected. Deleting generated connection '%s' (%s)",
264 nm_settings_connection_get_id (connection), nm_settings_connection_get_uuid (connection));
265 nm_settings_connection_delete (NM_SETTINGS_CONNECTION (connection), NULL, NULL);
266 g_object_unref (connection);
270 return found && notify;
274 _active_connection_cleanup (gpointer user_data)
276 NMManager *self = NM_MANAGER (user_data);
277 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
280 priv->ac_cleanup_id = 0;
282 g_object_freeze_notify (G_OBJECT (self));
283 iter = priv->active_connections;
285 NMActiveConnection *ac = iter->data;
288 if (nm_active_connection_get_state (ac) == NM_ACTIVE_CONNECTION_STATE_DEACTIVATED) {
289 if (active_connection_remove (self, ac))
290 g_object_notify (G_OBJECT (self), NM_MANAGER_ACTIVE_CONNECTIONS);
293 g_object_thaw_notify (G_OBJECT (self));
299 active_connection_state_changed (NMActiveConnection *active,
303 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
304 NMActiveConnectionState state;
306 state = nm_active_connection_get_state (active);
307 if (state == NM_ACTIVE_CONNECTION_STATE_DEACTIVATED) {
308 /* Destroy active connections from an idle handler to ensure that
309 * their last property change notifications go out, which wouldn't
310 * happen if we destroyed them immediately when their state was set
313 if (!priv->ac_cleanup_id)
314 priv->ac_cleanup_id = g_idle_add (_active_connection_cleanup, self);
317 nm_manager_update_state (self);
321 active_connection_default_changed (NMActiveConnection *active,
325 nm_manager_update_state (self);
329 * active_connection_add():
330 * @self: the #NMManager
331 * @active: the #NMActiveConnection to manage
333 * Begins to track and manage @active. Increases the refcount of @active.
336 active_connection_add (NMManager *self, NMActiveConnection *active)
338 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
340 g_return_if_fail (g_slist_find (priv->active_connections, active) == FALSE);
342 priv->active_connections = g_slist_prepend (priv->active_connections,
343 g_object_ref (active));
345 g_signal_connect (active,
346 "notify::" NM_ACTIVE_CONNECTION_STATE,
347 G_CALLBACK (active_connection_state_changed),
349 g_signal_connect (active,
350 "notify::" NM_ACTIVE_CONNECTION_DEFAULT,
351 G_CALLBACK (active_connection_default_changed),
353 g_signal_connect (active,
354 "notify::" NM_ACTIVE_CONNECTION_DEFAULT6,
355 G_CALLBACK (active_connection_default_changed),
358 g_signal_emit (self, signals[ACTIVE_CONNECTION_ADDED], 0, active);
360 /* Only notify D-Bus if the active connection is actually exported */
361 if (nm_exported_object_is_exported (NM_EXPORTED_OBJECT (active)))
362 g_object_notify (G_OBJECT (self), NM_MANAGER_ACTIVE_CONNECTIONS);
366 nm_manager_get_active_connections (NMManager *manager)
368 return NM_MANAGER_GET_PRIVATE (manager)->active_connections;
371 static NMActiveConnection *
372 find_ac_for_connection (NMManager *manager, NMConnection *connection)
374 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
376 const char *uuid = NULL;
377 gboolean is_settings_connection;
379 is_settings_connection = NM_IS_SETTINGS_CONNECTION (connection);
381 if (!is_settings_connection)
382 uuid = nm_connection_get_uuid (connection);
384 for (iter = priv->active_connections; iter; iter = iter->next) {
385 NMActiveConnection *ac = iter->data;
386 NMSettingsConnection *con;
388 con = nm_active_connection_get_settings_connection (ac);
390 /* depending on whether we have a NMSettingsConnection or a NMConnection,
391 * we lookup by UUID or by reference. */
392 if (is_settings_connection) {
393 if (con != (NMSettingsConnection *) connection)
396 if (strcmp (uuid, nm_connection_get_uuid (NM_CONNECTION (con))) != 0)
399 if (nm_active_connection_get_state (ac) < NM_ACTIVE_CONNECTION_STATE_DEACTIVATED)
406 /* Filter out connections that are already active.
407 * nm_settings_get_connections() returns sorted list. We need to preserve the
408 * order so that we didn't change auto-activation order (recent timestamps
410 * Caller is responsible for freeing the returned list with g_slist_free().
413 nm_manager_get_activatable_connections (NMManager *manager)
415 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
416 GSList *all_connections = nm_settings_get_connections (priv->settings);
417 GSList *connections = NULL, *iter;
418 NMSettingsConnection *connection;
420 for (iter = all_connections; iter; iter = iter->next) {
421 connection = iter->data;
423 if (!find_ac_for_connection (manager, NM_CONNECTION (connection)))
424 connections = g_slist_prepend (connections, connection);
427 g_slist_free (all_connections);
428 return g_slist_reverse (connections);
431 static NMActiveConnection *
432 active_connection_get_by_path (NMManager *manager, const char *path)
434 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
437 g_return_val_if_fail (manager != NULL, NULL);
438 g_return_val_if_fail (path != NULL, NULL);
440 for (iter = priv->active_connections; iter; iter = g_slist_next (iter)) {
441 NMActiveConnection *candidate = iter->data;
443 if (g_strcmp0 (path, nm_exported_object_get_path (NM_EXPORTED_OBJECT (candidate))) == 0)
449 /************************************************************************/
452 _config_changed_cb (NMConfig *config, NMConfigData *config_data, NMConfigChangeFlags changes, NMConfigData *old_data, NMManager *self)
454 g_object_set (NM_MANAGER_GET_PRIVATE (self)->connectivity,
455 NM_CONNECTIVITY_URI, nm_config_data_get_connectivity_uri (config_data),
456 NM_CONNECTIVITY_INTERVAL, nm_config_data_get_connectivity_interval (config_data),
457 NM_CONNECTIVITY_RESPONSE, nm_config_data_get_connectivity_response (config_data),
460 if (NM_FLAGS_HAS (changes, NM_CONFIG_CHANGE_GLOBAL_DNS_CONFIG))
461 g_object_notify (G_OBJECT (self), NM_MANAGER_GLOBAL_DNS_CONFIGURATION);
464 /************************************************************************/
467 nm_manager_get_device_by_path (NMManager *manager, const char *path)
471 g_return_val_if_fail (path != NULL, NULL);
473 for (iter = NM_MANAGER_GET_PRIVATE (manager)->devices; iter; iter = iter->next) {
474 if (!strcmp (nm_exported_object_get_path (NM_EXPORTED_OBJECT (iter->data)), path))
475 return NM_DEVICE (iter->data);
481 nm_manager_get_device_by_ifindex (NMManager *manager, int ifindex)
485 for (iter = NM_MANAGER_GET_PRIVATE (manager)->devices; iter; iter = iter->next) {
486 NMDevice *device = NM_DEVICE (iter->data);
488 if (nm_device_get_ifindex (device) == ifindex)
496 find_device_by_hw_addr (NMManager *manager, const char *hwaddr)
499 const char *device_addr;
501 g_return_val_if_fail (hwaddr != NULL, NULL);
503 if (nm_utils_hwaddr_valid (hwaddr, -1)) {
504 for (iter = NM_MANAGER_GET_PRIVATE (manager)->devices; iter; iter = iter->next) {
505 device_addr = nm_device_get_hw_address (NM_DEVICE (iter->data));
506 if (device_addr && nm_utils_hwaddr_matches (hwaddr, -1, device_addr, -1))
507 return NM_DEVICE (iter->data);
514 find_device_by_ip_iface (NMManager *self, const gchar *iface)
518 g_return_val_if_fail (iface != NULL, NULL);
520 for (iter = NM_MANAGER_GET_PRIVATE (self)->devices; iter; iter = g_slist_next (iter)) {
521 NMDevice *candidate = iter->data;
523 if ( nm_device_is_real (candidate)
524 && g_strcmp0 (nm_device_get_ip_iface (candidate), iface) == 0)
531 * find_device_by_iface:
532 * @self: the #NMManager
533 * @iface: the device interface to find
534 * @connection: a connection to ensure the returned device is compatible with
535 * @slave: a slave connection to ensure a master is compatible with
537 * Finds a device by interface name, preferring realized devices. If @slave
538 * is given, this function will only return master devices and will ensure
539 * @slave, when activated, can be a slave of the returned master device. If
540 * @connection is given, this function will only consider devices that are
541 * compatible with @connection.
543 * Returns: the matching #NMDevice
546 find_device_by_iface (NMManager *self,
548 NMConnection *connection,
551 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
552 NMDevice *fallback = NULL;
555 g_return_val_if_fail (iface != NULL, NULL);
557 for (iter = priv->devices; iter; iter = iter->next) {
558 NMDevice *candidate = iter->data;
560 if (strcmp (nm_device_get_iface (candidate), iface))
562 if (connection && !nm_device_check_connection_compatible (candidate, connection))
565 if (!nm_device_is_master (candidate))
567 if (!nm_device_check_slave_connection_compatible (candidate, slave))
571 if (nm_device_is_real (candidate))
574 fallback = candidate;
580 manager_sleeping (NMManager *self)
582 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
584 if (priv->sleeping || !priv->net_enabled)
590 _nm_state_to_string (NMState state)
593 case NM_STATE_ASLEEP:
595 case NM_STATE_DISCONNECTED:
596 return "DISCONNECTED";
597 case NM_STATE_DISCONNECTING:
598 return "DISCONNECTING";
599 case NM_STATE_CONNECTING:
601 case NM_STATE_CONNECTED_LOCAL:
602 return "CONNECTED_LOCAL";
603 case NM_STATE_CONNECTED_SITE:
604 return "CONNECTED_SITE";
605 case NM_STATE_CONNECTED_GLOBAL:
606 return "CONNECTED_GLOBAL";
607 case NM_STATE_UNKNOWN:
614 set_state (NMManager *self, NMState state)
616 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
618 if (priv->state == state)
623 _LOGI (LOGD_CORE, "NetworkManager state is now %s", _nm_state_to_string (state));
625 g_object_notify (G_OBJECT (self), NM_MANAGER_STATE);
626 g_signal_emit (self, signals[STATE_CHANGED], 0, priv->state);
630 checked_connectivity (GObject *object, GAsyncResult *result, gpointer user_data)
632 NMManager *manager = user_data;
633 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
634 NMConnectivityState connectivity;
636 if (priv->state == NM_STATE_CONNECTING || priv->state == NM_STATE_CONNECTED_SITE) {
637 connectivity = nm_connectivity_check_finish (priv->connectivity, result, NULL);
639 if (connectivity == NM_CONNECTIVITY_FULL)
640 set_state (manager, NM_STATE_CONNECTED_GLOBAL);
641 else if ( connectivity == NM_CONNECTIVITY_PORTAL
642 || connectivity == NM_CONNECTIVITY_LIMITED)
643 set_state (manager, NM_STATE_CONNECTED_SITE);
644 g_object_notify (G_OBJECT (manager), NM_MANAGER_CONNECTIVITY);
647 g_object_unref (manager);
651 find_best_device_state (NMManager *manager)
653 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
654 NMState best_state = NM_STATE_DISCONNECTED;
657 for (iter = priv->active_connections; iter; iter = iter->next) {
658 NMActiveConnection *ac = NM_ACTIVE_CONNECTION (iter->data);
659 NMActiveConnectionState ac_state = nm_active_connection_get_state (ac);
662 case NM_ACTIVE_CONNECTION_STATE_ACTIVATED:
663 if ( nm_active_connection_get_default (ac)
664 || nm_active_connection_get_default6 (ac)) {
665 if (nm_connectivity_get_state (priv->connectivity) == NM_CONNECTIVITY_FULL)
666 return NM_STATE_CONNECTED_GLOBAL;
668 best_state = NM_STATE_CONNECTED_SITE;
670 if (best_state < NM_STATE_CONNECTING)
671 best_state = NM_STATE_CONNECTED_LOCAL;
674 case NM_ACTIVE_CONNECTION_STATE_ACTIVATING:
675 if (!nm_active_connection_get_assumed (ac)) {
676 if (best_state != NM_STATE_CONNECTED_GLOBAL)
677 best_state = NM_STATE_CONNECTING;
680 case NM_ACTIVE_CONNECTION_STATE_DEACTIVATING:
681 if (!nm_active_connection_get_assumed (ac)) {
682 if (best_state < NM_STATE_DISCONNECTING)
683 best_state = NM_STATE_DISCONNECTING;
695 nm_manager_update_metered (NMManager *self)
697 NMManagerPrivate *priv;
699 NMMetered value = NM_METERED_UNKNOWN;
701 g_return_if_fail (NM_IS_MANAGER (self));
702 priv = NM_MANAGER_GET_PRIVATE (self);
704 if (priv->primary_connection) {
705 device = nm_active_connection_get_device (priv->primary_connection);
707 value = nm_device_get_metered (device);
710 if (value != priv->metered) {
711 priv->metered = value;
712 _LOGD (LOGD_CORE, "new metered value: %d", (int) priv->metered);
713 g_object_notify (G_OBJECT (self), NM_MANAGER_METERED);
718 nm_manager_update_state (NMManager *manager)
720 NMManagerPrivate *priv;
721 NMState new_state = NM_STATE_DISCONNECTED;
723 g_return_if_fail (NM_IS_MANAGER (manager));
725 priv = NM_MANAGER_GET_PRIVATE (manager);
727 if (manager_sleeping (manager))
728 new_state = NM_STATE_ASLEEP;
730 new_state = find_best_device_state (manager);
732 nm_connectivity_set_online (priv->connectivity, new_state >= NM_STATE_CONNECTED_LOCAL);
734 if (new_state == NM_STATE_CONNECTED_SITE) {
735 nm_connectivity_check_async (priv->connectivity,
736 checked_connectivity,
737 g_object_ref (manager));
740 set_state (manager, new_state);
744 manager_device_state_changed (NMDevice *device,
745 NMDeviceState new_state,
746 NMDeviceState old_state,
747 NMDeviceStateReason reason,
750 NMManager *self = NM_MANAGER (user_data);
751 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
754 case NM_DEVICE_STATE_UNMANAGED:
755 case NM_DEVICE_STATE_UNAVAILABLE:
756 case NM_DEVICE_STATE_DISCONNECTED:
757 case NM_DEVICE_STATE_PREPARE:
758 case NM_DEVICE_STATE_FAILED:
759 g_object_notify (G_OBJECT (self), NM_MANAGER_ACTIVE_CONNECTIONS);
765 if ( new_state == NM_DEVICE_STATE_UNAVAILABLE
766 || new_state == NM_DEVICE_STATE_DISCONNECTED)
767 nm_settings_device_added (priv->settings, device);
770 static void device_has_pending_action_changed (NMDevice *device,
775 check_if_startup_complete (NMManager *self)
777 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
783 if (!priv->devices_inited)
786 if (!nm_settings_get_startup_complete (priv->settings)) {
787 _LOGD (LOGD_CORE, "check_if_startup_complete returns FALSE because of NMSettings");
791 for (iter = priv->devices; iter; iter = iter->next) {
792 NMDevice *dev = iter->data;
794 if (nm_device_has_pending_action (dev)) {
795 _LOGD (LOGD_CORE, "check_if_startup_complete returns FALSE because of %s",
796 nm_device_get_iface (dev));
801 _LOGI (LOGD_CORE, "startup complete");
803 priv->startup = FALSE;
804 g_object_notify (G_OBJECT (self), "startup");
806 /* We don't have to watch notify::has-pending-action any more. */
807 for (iter = priv->devices; iter; iter = iter->next) {
808 NMDevice *dev = iter->data;
810 g_signal_handlers_disconnect_by_func (dev, G_CALLBACK (device_has_pending_action_changed), self);
813 if (nm_config_get_configure_and_quit (nm_config_get ()))
814 g_signal_emit (self, signals[CONFIGURE_QUIT], 0);
818 device_has_pending_action_changed (NMDevice *device,
822 check_if_startup_complete (self);
826 settings_startup_complete_changed (NMSettings *settings,
830 check_if_startup_complete (self);
834 remove_device (NMManager *self,
837 gboolean allow_unmanage)
839 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
841 _LOGD (LOGD_DEVICE, "(%s): removing device (allow_unmanage %d, managed %d)",
842 nm_device_get_iface (device), allow_unmanage, nm_device_get_managed (device, FALSE));
844 if (allow_unmanage && nm_device_get_managed (device, FALSE)) {
845 NMActRequest *req = nm_device_get_act_request (device);
846 gboolean unmanage = FALSE;
848 /* Leave activated interfaces up when quitting so their configuration
849 * can be taken over when NM restarts. This ensures connectivity while
850 * NM is stopped. Devices which do not support connection assumption
853 if (!quitting) /* Forced removal; device already gone */
855 else if (!nm_device_can_assume_active_connection (device))
862 nm_device_set_unmanaged_by_quitting (device);
864 nm_device_set_unmanaged_by_flags (device, NM_UNMANAGED_PLATFORM_INIT, TRUE, NM_DEVICE_STATE_REASON_REMOVED);
865 } else if (quitting && nm_config_get_configure_and_quit (nm_config_get ())) {
866 nm_device_spawn_iface_helper (device);
870 g_signal_handlers_disconnect_matched (device, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
872 nm_settings_device_removed (priv->settings, device, quitting);
873 priv->devices = g_slist_remove (priv->devices, device);
875 if (nm_device_is_real (device)) {
876 g_signal_emit (self, signals[DEVICE_REMOVED], 0, device);
877 g_object_notify (G_OBJECT (self), NM_MANAGER_DEVICES);
878 nm_device_removed (device);
880 g_signal_emit (self, signals[INTERNAL_DEVICE_REMOVED], 0, device);
881 g_object_notify (G_OBJECT (self), NM_MANAGER_ALL_DEVICES);
883 nm_exported_object_clear_and_unexport (&device);
885 check_if_startup_complete (self);
889 device_removed_cb (NMDevice *device, gpointer user_data)
891 remove_device (NM_MANAGER (user_data), device, FALSE, TRUE);
895 nm_manager_get_state (NMManager *manager)
897 g_return_val_if_fail (NM_IS_MANAGER (manager), NM_STATE_UNKNOWN);
899 return NM_MANAGER_GET_PRIVATE (manager)->state;
902 /***************************/
905 find_parent_device_for_connection (NMManager *self, NMConnection *connection, NMDeviceFactory *cached_factory)
907 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
908 NMDeviceFactory *factory;
909 const char *parent_name = NULL;
910 NMSettingsConnection *parent_connection;
911 NMDevice *parent, *first_compatible = NULL;
914 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
916 if (!cached_factory) {
917 factory = nm_device_factory_manager_find_factory_for_connection (connection);
921 factory = cached_factory;
923 parent_name = nm_device_factory_get_connection_parent (factory, connection);
927 /* Try as an interface name of a parent device */
928 parent = find_device_by_iface (self, parent_name, NULL, NULL);
932 /* Maybe a hardware address */
933 parent = find_device_by_hw_addr (self, parent_name);
937 /* Maybe a connection UUID */
938 parent_connection = nm_settings_get_connection_by_uuid (priv->settings, parent_name);
939 if (!parent_connection)
942 /* Check if the parent connection is currently activated or is comaptible
943 * with some known device.
945 for (iter = priv->devices; iter; iter = iter->next) {
946 NMDevice *candidate = iter->data;
948 if (nm_device_get_settings_connection (candidate) == parent_connection)
951 if ( !first_compatible
952 && nm_device_check_connection_compatible (candidate, NM_CONNECTION (parent_connection)))
953 first_compatible = candidate;
956 return first_compatible;
960 * nm_manager_get_connection_iface:
961 * @self: the #NMManager
962 * @connection: the #NMConnection to get the interface for
963 * @out_parent: on success, the parent device if any
964 * @error: an error if determining the virtual interface name failed
966 * Given @connection, returns the interface name that the connection
967 * would need to use when activated. %NULL is returned if the name
968 * is not specified in connection or a the name for a virtual device
969 * could not be generated.
971 * Returns: the expected interface name (caller takes ownership), or %NULL
974 nm_manager_get_connection_iface (NMManager *self,
975 NMConnection *connection,
976 NMDevice **out_parent,
979 NMDeviceFactory *factory;
981 NMDevice *parent = NULL;
986 factory = nm_device_factory_manager_find_factory_for_connection (connection);
990 NM_MANAGER_ERROR_FAILED,
991 "NetworkManager plugin for '%s' unavailable",
992 nm_connection_get_connection_type (connection));
997 && !NM_DEVICE_FACTORY_GET_INTERFACE (factory)->get_connection_iface) {
998 /* optimization. Shortcut lookup of the partent device. */
999 iface = g_strdup (nm_connection_get_interface_name (connection));
1003 NM_MANAGER_ERROR_FAILED,
1004 "failed to determine interface name: error determine name for %s",
1005 nm_connection_get_connection_type (connection));
1010 parent = find_parent_device_for_connection (self, connection, factory);
1011 iface = nm_device_factory_get_connection_iface (factory,
1013 parent ? nm_device_get_ip_iface (parent) : NULL,
1019 *out_parent = parent;
1024 * system_create_virtual_device:
1025 * @self: the #NMManager
1026 * @connection: the connection which might require a virtual device
1028 * If @connection requires a virtual device and one does not yet exist for it,
1029 * creates that device.
1031 * Returns: A #NMDevice that was just realized; %NULL if none
1034 system_create_virtual_device (NMManager *self, NMConnection *connection)
1036 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1037 NMDeviceFactory *factory;
1038 gs_free_slist GSList *connections = NULL;
1040 gs_free char *iface = NULL;
1041 NMDevice *device = NULL, *parent = NULL;
1042 GError *error = NULL;
1044 g_return_val_if_fail (NM_IS_MANAGER (self), NULL);
1045 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1047 iface = nm_manager_get_connection_iface (self, connection, &parent, &error);
1049 _LOGD (LOGD_DEVICE, "(%s) can't get a name of a virtual device: %s",
1050 nm_connection_get_id (connection), error->message);
1051 g_error_free (error);
1055 /* See if there's a device that is already compatible with this connection */
1056 for (iter = priv->devices; iter; iter = g_slist_next (iter)) {
1057 NMDevice *candidate = iter->data;
1059 if (nm_device_check_connection_compatible (candidate, connection)) {
1060 if (nm_device_is_real (candidate)) {
1061 _LOGD (LOGD_DEVICE, "(%s) already created virtual interface name %s",
1062 nm_connection_get_id (connection), iface);
1072 /* No matching device found. Proceed creating a new one. */
1074 factory = nm_device_factory_manager_find_factory_for_connection (connection);
1076 _LOGE (LOGD_DEVICE, "(%s:%s) NetworkManager plugin for '%s' unavailable",
1077 nm_connection_get_id (connection), iface,
1078 nm_connection_get_connection_type (connection));
1082 device = nm_device_factory_create_device (factory, iface, NULL, connection, NULL, &error);
1084 _LOGW (LOGD_DEVICE, "(%s) factory can't create the device: %s",
1085 nm_connection_get_id (connection), error->message);
1086 g_error_free (error);
1090 _LOGD (LOGD_DEVICE, "(%s) create virtual device %s",
1091 nm_connection_get_id (connection),
1092 nm_device_get_iface (device));
1094 if (!add_device (self, device, &error)) {
1095 _LOGW (LOGD_DEVICE, "(%s) can't register the device with manager: %s",
1096 nm_connection_get_id (connection), error->message);
1097 g_error_free (error);
1098 g_object_unref (device);
1102 /* Add device takes a reference that NMManager still owns, so it's
1103 * safe to unref here and still return @device.
1105 g_object_unref (device);
1108 /* Create backing resources if the device has any autoconnect connections */
1109 connections = nm_settings_get_connections (priv->settings);
1110 for (iter = connections; iter; iter = g_slist_next (iter)) {
1111 NMConnection *candidate = iter->data;
1112 NMSettingConnection *s_con;
1114 if (!nm_device_check_connection_compatible (device, candidate))
1117 s_con = nm_connection_get_setting_connection (candidate);
1119 if (!nm_setting_connection_get_autoconnect (s_con))
1122 /* Create any backing resources the device needs */
1123 if (!nm_device_create_and_realize (device, connection, parent, &error)) {
1124 _LOGW (LOGD_DEVICE, "(%s) couldn't create the device: %s",
1125 nm_connection_get_id (connection), error->message);
1126 g_error_free (error);
1127 remove_device (self, device, FALSE, TRUE);
1137 retry_connections_for_parent_device (NMManager *self, NMDevice *device)
1139 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1140 GSList *connections, *iter;
1142 g_return_if_fail (device);
1144 connections = nm_settings_get_connections (priv->settings);
1145 for (iter = connections; iter; iter = g_slist_next (iter)) {
1146 NMConnection *candidate = iter->data;
1147 gs_free_error GError *error = NULL;
1148 gs_free char *ifname = NULL;
1151 parent = find_parent_device_for_connection (self, candidate, NULL);
1152 if (parent == device) {
1153 /* Only try to activate devices that don't already exist */
1154 ifname = nm_manager_get_connection_iface (self, candidate, &parent, &error);
1156 if (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, ifname))
1157 connection_changed (priv->settings, candidate, self);
1162 g_slist_free (connections);
1166 connection_changed (NMSettings *settings,
1167 NMConnection *connection,
1172 if (!nm_connection_is_virtual (connection))
1175 device = system_create_virtual_device (manager, connection);
1179 /* Maybe the device that was created was needed by some other
1180 * connection's device (parent of a VLAN). Let the connections
1181 * can use the newly created device as a parent know. */
1182 retry_connections_for_parent_device (manager, device);
1186 connection_removed (NMSettings *settings,
1187 NMSettingsConnection *connection,
1191 * Do not delete existing virtual devices to keep connectivity up.
1192 * Virtual devices are reused when NetworkManager is restarted.
1197 system_unmanaged_devices_changed_cb (NMSettings *settings,
1201 NMManager *self = NM_MANAGER (user_data);
1202 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1203 const GSList *unmanaged_specs, *iter;
1205 unmanaged_specs = nm_settings_get_unmanaged_specs (priv->settings);
1206 for (iter = priv->devices; iter; iter = g_slist_next (iter))
1207 nm_device_set_unmanaged_by_user_config (NM_DEVICE (iter->data), unmanaged_specs);
1211 system_hostname_changed_cb (NMSettings *settings,
1215 NMManager *self = NM_MANAGER (user_data);
1216 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1219 hostname = nm_settings_get_hostname (priv->settings);
1221 /* nm_settings_get_hostname() does not return an empty hostname. */
1222 nm_assert (!hostname || *hostname);
1224 if (!hostname && !priv->hostname)
1226 if (hostname && priv->hostname && !strcmp (hostname, priv->hostname)) {
1231 /* realloc, to free possibly trailing data after NUL. */
1233 hostname = g_realloc (hostname, strlen (hostname) + 1);
1235 g_free (priv->hostname);
1236 priv->hostname = hostname;
1237 g_object_notify (G_OBJECT (self), NM_MANAGER_HOSTNAME);
1239 nm_dhcp_manager_set_default_hostname (nm_dhcp_manager_get (), priv->hostname);
1242 /*******************************************************************/
1243 /* General NMManager stuff */
1244 /*******************************************************************/
1246 /* Store value into key-file; supported types: boolean, int, string */
1248 write_value_to_state_file (const char *filename,
1258 gboolean ret = FALSE;
1260 g_return_val_if_fail (filename != NULL, FALSE);
1261 g_return_val_if_fail (group != NULL, FALSE);
1262 g_return_val_if_fail (key != NULL, FALSE);
1263 g_return_val_if_fail (value_type == G_TYPE_BOOLEAN ||
1264 value_type == G_TYPE_INT ||
1265 value_type == G_TYPE_STRING,
1268 key_file = g_key_file_new ();
1270 g_key_file_set_list_separator (key_file, ',');
1271 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_KEEP_COMMENTS, NULL);
1272 switch (value_type) {
1273 case G_TYPE_BOOLEAN:
1274 g_key_file_set_boolean (key_file, group, key, *((gboolean *) value));
1277 g_key_file_set_integer (key_file, group, key, *((gint *) value));
1280 g_key_file_set_string (key_file, group, key, *((const gchar **) value));
1284 data = g_key_file_to_data (key_file, &len, NULL);
1286 ret = g_file_set_contents (filename, data, len, error);
1289 g_key_file_free (key_file);
1295 radio_enabled_for_rstate (RadioState *rstate, gboolean check_changeable)
1299 enabled = rstate->user_enabled && rstate->hw_enabled;
1300 if (check_changeable)
1301 enabled &= rstate->sw_enabled;
1306 radio_enabled_for_type (NMManager *self, RfKillType rtype, gboolean check_changeable)
1308 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1310 return radio_enabled_for_rstate (&priv->radio_states[rtype], check_changeable);
1314 manager_update_radio_enabled (NMManager *self,
1318 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1321 /* Do nothing for radio types not yet implemented */
1325 g_object_notify (G_OBJECT (self), rstate->prop);
1327 /* Don't touch devices if asleep/networking disabled */
1328 if (manager_sleeping (self))
1331 /* enable/disable wireless devices as required */
1332 for (iter = priv->devices; iter; iter = iter->next) {
1333 NMDevice *device = NM_DEVICE (iter->data);
1335 if (nm_device_get_rfkill_type (device) == rstate->rtype) {
1336 _LOGD (LOGD_RFKILL, "(%s): setting radio %s",
1337 nm_device_get_iface (device),
1338 enabled ? "enabled" : "disabled");
1339 nm_device_set_enabled (device, enabled);
1345 update_rstate_from_rfkill (NMRfkillManager *rfkill_mgr, RadioState *rstate)
1347 switch (nm_rfkill_manager_get_rfkill_state (rfkill_mgr, rstate->rtype)) {
1348 case RFKILL_UNBLOCKED:
1349 rstate->sw_enabled = TRUE;
1350 rstate->hw_enabled = TRUE;
1352 case RFKILL_SOFT_BLOCKED:
1353 rstate->sw_enabled = FALSE;
1354 rstate->hw_enabled = TRUE;
1356 case RFKILL_HARD_BLOCKED:
1357 rstate->sw_enabled = FALSE;
1358 rstate->hw_enabled = FALSE;
1361 g_warn_if_reached ();
1367 manager_rfkill_update_one_type (NMManager *self,
1371 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1372 gboolean old_enabled, new_enabled, old_rfkilled, new_rfkilled, old_hwe;
1374 old_enabled = radio_enabled_for_rstate (rstate, TRUE);
1375 old_rfkilled = rstate->hw_enabled && rstate->sw_enabled;
1376 old_hwe = rstate->hw_enabled;
1378 /* recheck kernel rfkill state */
1379 update_rstate_from_rfkill (priv->rfkill_mgr, rstate);
1381 /* Print out all states affecting device enablement */
1383 _LOGD (LOGD_RFKILL, "%s hw-enabled %d sw-enabled %d",
1384 rstate->desc, rstate->hw_enabled, rstate->sw_enabled);
1387 /* Log new killswitch state */
1388 new_rfkilled = rstate->hw_enabled && rstate->sw_enabled;
1389 if (old_rfkilled != new_rfkilled) {
1390 _LOGI (LOGD_RFKILL, "%s now %s by radio killswitch",
1392 new_rfkilled ? "enabled" : "disabled");
1395 /* Send out property changed signal for HW enabled */
1396 if (rstate->hw_enabled != old_hwe) {
1397 if (rstate->hw_prop)
1398 g_object_notify (G_OBJECT (self), rstate->hw_prop);
1401 /* And finally update the actual device radio state itself; respect the
1402 * daemon state here because this is never called from user-triggered
1403 * radio changes and we only want to ignore the daemon enabled state when
1404 * handling user radio change requests.
1406 new_enabled = radio_enabled_for_rstate (rstate, TRUE);
1407 if (new_enabled != old_enabled)
1408 manager_update_radio_enabled (self, rstate, new_enabled);
1412 nm_manager_rfkill_update (NMManager *self, RfKillType rtype)
1414 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1417 if (rtype != RFKILL_TYPE_UNKNOWN)
1418 manager_rfkill_update_one_type (self, &priv->radio_states[rtype], rtype);
1420 /* Otherwise sync all radio types */
1421 for (i = 0; i < RFKILL_TYPE_MAX; i++)
1422 manager_rfkill_update_one_type (self, &priv->radio_states[i], i);
1427 device_auth_done_cb (NMAuthChain *chain,
1429 GDBusMethodInvocation *context,
1432 NMManager *self = NM_MANAGER (user_data);
1433 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1434 GError *error = NULL;
1435 NMAuthCallResult result;
1437 const char *permission;
1438 NMDeviceAuthRequestFunc callback;
1439 NMAuthSubject *subject;
1443 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
1445 permission = nm_auth_chain_get_data (chain, "requested-permission");
1446 g_assert (permission);
1447 callback = nm_auth_chain_get_data (chain, "callback");
1448 g_assert (callback);
1449 device = nm_auth_chain_get_data (chain, "device");
1452 result = nm_auth_chain_get_result (chain, permission);
1453 subject = nm_auth_chain_get_subject (chain);
1456 /* translate the auth error into a manager permission denied error */
1457 _LOGD (LOGD_CORE, "%s request failed: %s", permission, auth_error->message);
1458 error = g_error_new (NM_MANAGER_ERROR,
1459 NM_MANAGER_ERROR_PERMISSION_DENIED,
1460 "%s request failed: %s",
1461 permission, auth_error->message);
1462 } else if (result != NM_AUTH_CALL_RESULT_YES) {
1463 _LOGD (LOGD_CORE, "%s request failed: not authorized", permission);
1464 error = g_error_new (NM_MANAGER_ERROR,
1465 NM_MANAGER_ERROR_PERMISSION_DENIED,
1466 "%s request failed: not authorized",
1470 g_assert (error || (result == NM_AUTH_CALL_RESULT_YES));
1476 nm_auth_chain_get_data (chain, "user-data"));
1478 g_clear_error (&error);
1479 nm_auth_chain_unref (chain);
1483 device_auth_request_cb (NMDevice *device,
1484 GDBusMethodInvocation *context,
1485 NMConnection *connection,
1486 const char *permission,
1487 gboolean allow_interaction,
1488 NMDeviceAuthRequestFunc callback,
1492 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1493 GError *error = NULL;
1494 NMAuthSubject *subject = NULL;
1495 char *error_desc = NULL;
1498 /* Validate the caller */
1499 subject = nm_auth_subject_new_unix_process_from_context (context);
1501 error = g_error_new_literal (NM_MANAGER_ERROR,
1502 NM_MANAGER_ERROR_PERMISSION_DENIED,
1503 "Failed to get request UID.");
1507 /* Ensure the subject has permissions for this connection */
1508 if (connection && !nm_auth_is_subject_in_acl (connection,
1511 error = g_error_new_literal (NM_MANAGER_ERROR,
1512 NM_MANAGER_ERROR_PERMISSION_DENIED,
1514 g_free (error_desc);
1518 /* Validate the request */
1519 chain = nm_auth_chain_new_subject (subject, context, device_auth_done_cb, self);
1521 error = g_error_new_literal (NM_MANAGER_ERROR,
1522 NM_MANAGER_ERROR_PERMISSION_DENIED,
1523 "Unable to authenticate request.");
1527 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
1528 nm_auth_chain_set_data (chain, "device", g_object_ref (device), g_object_unref);
1529 nm_auth_chain_set_data (chain, "requested-permission", g_strdup (permission), g_free);
1530 nm_auth_chain_set_data (chain, "callback", callback, NULL);
1531 nm_auth_chain_set_data (chain, "user-data", user_data, NULL);
1532 nm_auth_chain_add_call (chain, permission, allow_interaction);
1536 callback (device, context, subject, error, user_data);
1538 g_clear_object (&subject);
1539 g_clear_error (&error);
1543 match_connection_filter (NMConnection *connection, gpointer user_data)
1545 if (nm_settings_connection_get_nm_generated_assumed (NM_SETTINGS_CONNECTION (connection)))
1548 return nm_device_check_connection_compatible (NM_DEVICE (user_data), connection);
1552 * get_existing_connection:
1553 * @manager: #NMManager instance
1554 * @device: #NMDevice instance
1555 * @out_generated: (allow-none): return TRUE, if the connection was generated.
1557 * Returns: a #NMSettingsConnection to be assumed by the device, or %NULL if
1558 * the device does not support assuming existing connections.
1560 static NMSettingsConnection *
1561 get_existing_connection (NMManager *self, NMDevice *device, gboolean *out_generated)
1563 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1564 gs_free_slist GSList *connections = nm_manager_get_activatable_connections (self);
1565 NMConnection *connection = NULL;
1566 NMSettingsConnection *matched;
1567 NMSettingsConnection *added = NULL;
1568 GError *error = NULL;
1569 NMDevice *master = NULL;
1570 int ifindex = nm_device_get_ifindex (device);
1573 *out_generated = FALSE;
1575 nm_device_capture_initial_config (device);
1578 int master_ifindex = nm_platform_link_get_master (NM_PLATFORM_GET, ifindex);
1580 if (master_ifindex) {
1581 master = nm_manager_get_device_by_ifindex (self, master_ifindex);
1583 _LOGD (LOGD_DEVICE, "(%s): cannot generate connection for slave before its master (%s/%d)",
1584 nm_device_get_iface (device), nm_platform_link_get_name (NM_PLATFORM_GET, master_ifindex), master_ifindex);
1587 if (!nm_device_get_act_request (master)) {
1588 _LOGD (LOGD_DEVICE, "(%s): cannot generate connection for slave before master %s activates",
1589 nm_device_get_iface (device), nm_device_get_iface (master));
1595 /* The core of the API is nm_device_generate_connection() function and
1596 * update_connection() virtual method and the convenient connection_type
1597 * class attribute. Subclasses supporting the new API must have
1598 * update_connection() implemented, otherwise nm_device_generate_connection()
1601 connection = nm_device_generate_connection (device, master);
1605 /* Now we need to compare the generated connection to each configured
1606 * connection. The comparison function is the heart of the connection
1607 * assumption implementation and it must compare the connections very
1608 * carefully to sort out various corner cases. Also, the comparison is
1609 * not entirely symmetric.
1611 * When no configured connection matches the generated connection, we keep
1612 * the generated connection instead.
1614 connections = g_slist_reverse (g_slist_sort (connections, nm_settings_sort_connections));
1615 matched = NM_SETTINGS_CONNECTION (nm_utils_match_connection (connections,
1617 nm_device_has_carrier (device),
1618 nm_device_get_ip4_route_metric (device),
1619 nm_device_get_ip6_route_metric (device),
1620 match_connection_filter,
1623 _LOGI (LOGD_DEVICE, "(%s): found matching connection '%s'",
1624 nm_device_get_iface (device),
1625 nm_settings_connection_get_id (matched));
1626 g_object_unref (connection);
1630 _LOGD (LOGD_DEVICE, "(%s): generated connection '%s'",
1631 nm_device_get_iface (device),
1632 nm_connection_get_id (connection));
1634 added = nm_settings_add_connection (priv->settings, connection, FALSE, &error);
1636 nm_settings_connection_set_flags (NM_SETTINGS_CONNECTION (added),
1637 NM_SETTINGS_CONNECTION_FLAGS_NM_GENERATED |
1638 NM_SETTINGS_CONNECTION_FLAGS_NM_GENERATED_ASSUMED,
1641 *out_generated = TRUE;
1643 _LOGW (LOGD_SETTINGS, "(%s) Couldn't save generated connection '%s': %s",
1644 nm_device_get_iface (device),
1645 nm_connection_get_id (connection),
1647 g_clear_error (&error);
1649 g_object_unref (connection);
1651 return added ? added : NULL;
1655 assume_connection (NMManager *self, NMDevice *device, NMSettingsConnection *connection)
1657 NMActiveConnection *active, *master_ac;
1658 NMAuthSubject *subject;
1659 GError *error = NULL;
1661 _LOGD (LOGD_DEVICE, "(%s): will attempt to assume connection",
1662 nm_device_get_iface (device));
1664 /* Move device to DISCONNECTED to activate the connection */
1665 if (nm_device_get_state (device) == NM_DEVICE_STATE_UNAVAILABLE) {
1666 nm_device_state_changed (device,
1667 NM_DEVICE_STATE_DISCONNECTED,
1668 NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED);
1670 g_return_val_if_fail (nm_device_get_state (device) >= NM_DEVICE_STATE_DISCONNECTED, FALSE);
1672 subject = nm_auth_subject_new_internal ();
1673 active = _new_active_connection (self, NM_CONNECTION (connection), NULL, device, subject, &error);
1674 g_object_unref (subject);
1677 _LOGW (LOGD_DEVICE, "assumed connection %s failed to activate: %s",
1678 nm_connection_get_path (NM_CONNECTION (connection)),
1680 g_error_free (error);
1684 /* If the device is a slave or VLAN, find the master ActiveConnection */
1686 if (find_master (self, NM_CONNECTION (connection), device, NULL, NULL, &master_ac, NULL) && master_ac)
1687 nm_active_connection_set_master (active, master_ac);
1689 nm_active_connection_set_assumed (active, TRUE);
1690 nm_exported_object_export (NM_EXPORTED_OBJECT (active));
1691 active_connection_add (self, active);
1692 nm_device_queue_activation (device, NM_ACT_REQUEST (active));
1693 g_object_unref (active);
1699 recheck_assume_connection (NMManager *self, NMDevice *device)
1701 NMSettingsConnection *connection;
1702 gboolean was_unmanaged = FALSE, success, generated = FALSE;
1703 NMDeviceState state;
1705 g_return_val_if_fail (NM_IS_MANAGER (self), FALSE);
1706 g_return_val_if_fail (NM_IS_DEVICE (device), FALSE);
1708 if (nm_device_get_is_nm_owned (device))
1711 if (!nm_device_get_managed (device, FALSE))
1714 state = nm_device_get_state (device);
1715 if (state > NM_DEVICE_STATE_DISCONNECTED)
1718 connection = get_existing_connection (self, device, &generated);
1720 _LOGD (LOGD_DEVICE, "(%s): can't assume; no connection",
1721 nm_device_get_iface (device));
1725 if (state == NM_DEVICE_STATE_UNMANAGED) {
1726 was_unmanaged = TRUE;
1727 nm_device_state_changed (device,
1728 NM_DEVICE_STATE_UNAVAILABLE,
1729 NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED);
1732 success = assume_connection (self, device, connection);
1734 if (was_unmanaged) {
1735 nm_device_state_changed (device,
1736 NM_DEVICE_STATE_UNAVAILABLE,
1737 NM_DEVICE_STATE_REASON_CONFIG_FAILED);
1741 _LOGD (LOGD_DEVICE, "(%s): connection assumption failed. Deleting generated connection",
1742 nm_device_get_iface (device));
1744 nm_settings_connection_delete (connection, NULL, NULL);
1752 recheck_assume_connection_cb (NMDevice *device, gpointer user_data)
1754 recheck_assume_connection (user_data, device);
1758 device_ip_iface_changed (NMDevice *device,
1762 const char *ip_iface = nm_device_get_ip_iface (device);
1765 /* Remove NMDevice objects that are actually child devices of others,
1766 * when the other device finally knows its IP interface name. For example,
1767 * remove the PPP interface that's a child of a WWAN device, since it's
1768 * not really a standalone NMDevice.
1770 for (iter = NM_MANAGER_GET_PRIVATE (self)->devices; iter; iter = iter->next) {
1771 NMDevice *candidate = NM_DEVICE (iter->data);
1773 if ( candidate != device
1774 && g_strcmp0 (nm_device_get_iface (candidate), ip_iface) == 0
1775 && nm_device_is_real (candidate)) {
1776 remove_device (self, candidate, FALSE, FALSE);
1783 device_iface_changed (NMDevice *device,
1787 /* Virtual connections may refer to the new device name as
1788 * parent device, retry to activate them.
1790 retry_connections_for_parent_device (self, device);
1795 device_realized (NMDevice *device,
1799 /* Emit D-Bus signals */
1800 g_signal_emit (self, signals[DEVICE_ADDED], 0, device);
1801 g_object_notify (G_OBJECT (self), NM_MANAGER_DEVICES);
1805 _device_realize_finish (NMManager *self, NMDevice *device, const NMPlatformLink *plink)
1807 g_return_if_fail (NM_IS_MANAGER (self));
1808 g_return_if_fail (NM_IS_DEVICE (device));
1810 nm_device_realize_finish (device, plink);
1812 if (!nm_device_get_managed (device, FALSE))
1815 if (recheck_assume_connection (self, device))
1818 /* if we failed to assume a connection for the managed device, but the device
1819 * is still unavailable. Set UNAVAILABLE state again, this time with NOW_MANAGED. */
1820 nm_device_state_changed (device,
1821 NM_DEVICE_STATE_UNAVAILABLE,
1822 NM_DEVICE_STATE_REASON_NOW_MANAGED);
1823 nm_device_emit_recheck_auto_activate (device);
1828 * @self: the #NMManager
1829 * @device: the #NMDevice to add
1830 * @error: (out): the #GError
1832 * If successful, this function will increase the references count of @device.
1833 * Callers should decrease the reference count.
1836 add_device (NMManager *self, NMDevice *device, GError **error)
1838 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1839 const char *iface, *type_desc;
1841 GSList *iter, *remove = NULL;
1843 const char *dbus_path;
1846 ifindex = nm_device_get_ifindex (device);
1847 if (ifindex > 0 && nm_manager_get_device_by_ifindex (self, ifindex)) {
1848 g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED,
1849 "A device with ifindex %d already exists", ifindex);
1853 /* Remove existing devices owned by the new device; eg remove ethernet
1854 * ports that are owned by a WWAN modem, since udev may announce them
1855 * before the modem is fully discovered.
1857 * FIXME: use parent/child device relationships instead of removing
1858 * the child NMDevice entirely
1860 for (iter = priv->devices; iter; iter = iter->next) {
1861 NMDevice *candidate = iter->data;
1863 iface = nm_device_get_ip_iface (candidate);
1864 if (nm_device_is_real (candidate) && nm_device_owns_iface (device, iface))
1865 remove = g_slist_prepend (remove, candidate);
1867 for (iter = remove; iter; iter = iter->next)
1868 remove_device (self, NM_DEVICE (iter->data), FALSE, FALSE);
1869 g_slist_free (remove);
1871 priv->devices = g_slist_append (priv->devices, g_object_ref (device));
1873 g_signal_connect (device, NM_DEVICE_STATE_CHANGED,
1874 G_CALLBACK (manager_device_state_changed),
1877 g_signal_connect (device, NM_DEVICE_AUTH_REQUEST,
1878 G_CALLBACK (device_auth_request_cb),
1881 g_signal_connect (device, NM_DEVICE_REMOVED,
1882 G_CALLBACK (device_removed_cb),
1885 g_signal_connect (device, NM_DEVICE_RECHECK_ASSUME,
1886 G_CALLBACK (recheck_assume_connection_cb),
1889 g_signal_connect (device, "notify::" NM_DEVICE_IP_IFACE,
1890 G_CALLBACK (device_ip_iface_changed),
1893 g_signal_connect (device, "notify::" NM_DEVICE_IFACE,
1894 G_CALLBACK (device_iface_changed),
1897 g_signal_connect (device, "notify::" NM_DEVICE_REAL,
1898 G_CALLBACK (device_realized),
1901 if (priv->startup) {
1902 g_signal_connect (device, "notify::" NM_DEVICE_HAS_PENDING_ACTION,
1903 G_CALLBACK (device_has_pending_action_changed),
1907 /* Update global rfkill state for this device type with the device's
1908 * rfkill state, and then set this device's rfkill state based on the
1911 rtype = nm_device_get_rfkill_type (device);
1912 if (rtype != RFKILL_TYPE_UNKNOWN) {
1913 nm_manager_rfkill_update (self, rtype);
1914 nm_device_set_enabled (device, radio_enabled_for_type (self, rtype, TRUE));
1917 iface = nm_device_get_iface (device);
1919 type_desc = nm_device_get_type_desc (device);
1920 g_assert (type_desc);
1922 nm_device_set_unmanaged_by_user_config (device, nm_settings_get_unmanaged_specs (priv->settings));
1924 nm_device_set_unmanaged_flags (device,
1925 NM_UNMANAGED_SLEEPING,
1926 manager_sleeping (self));
1928 dbus_path = nm_exported_object_export (NM_EXPORTED_OBJECT (device));
1929 _LOGI (LOGD_DEVICE, "(%s): new %s device (%s)", iface, type_desc, dbus_path);
1931 nm_settings_device_added (priv->settings, device);
1932 g_signal_emit (self, signals[INTERNAL_DEVICE_ADDED], 0, device);
1933 g_object_notify (G_OBJECT (self), NM_MANAGER_ALL_DEVICES);
1935 for (iter = priv->devices; iter; iter = iter->next) {
1936 NMDevice *d = iter->data;
1939 nm_device_notify_new_device_added (d, device);
1942 /* Virtual connections may refer to the new device as
1943 * parent device, retry to activate them.
1945 retry_connections_for_parent_device (self, device);
1950 /*******************************************************************/
1953 factory_device_added_cb (NMDeviceFactory *factory,
1957 NMManager *self = user_data;
1958 GError *error = NULL;
1960 g_return_if_fail (NM_IS_MANAGER (self));
1962 if (nm_device_realize_start (device, NULL, NULL, &error)) {
1963 add_device (self, device, NULL);
1964 _device_realize_finish (self, device, NULL);
1966 _LOGW (LOGD_DEVICE, "(%s): failed to realize device: %s",
1967 nm_device_get_iface (device), error->message);
1968 g_error_free (error);
1973 factory_component_added_cb (NMDeviceFactory *factory,
1979 g_return_val_if_fail (NM_IS_MANAGER (user_data), FALSE);
1981 for (iter = NM_MANAGER_GET_PRIVATE (user_data)->devices; iter; iter = iter->next) {
1982 if (nm_device_notify_component_added ((NMDevice *) iter->data, component))
1989 _register_device_factory (NMDeviceFactory *factory, gpointer user_data)
1991 NMManager *self = NM_MANAGER (user_data);
1993 g_signal_connect (factory,
1994 NM_DEVICE_FACTORY_DEVICE_ADDED,
1995 G_CALLBACK (factory_device_added_cb),
1997 g_signal_connect (factory,
1998 NM_DEVICE_FACTORY_COMPONENT_ADDED,
1999 G_CALLBACK (factory_component_added_cb),
2003 /*******************************************************************/
2006 platform_link_added (NMManager *self,
2008 const NMPlatformLink *plink)
2010 NMDeviceFactory *factory;
2011 NMDevice *device = NULL;
2012 GError *error = NULL;
2013 gboolean nm_plugin_missing = FALSE;
2016 g_return_if_fail (ifindex > 0);
2018 if (nm_manager_get_device_by_ifindex (self, ifindex))
2021 /* Let unrealized devices try to realize themselves with the link */
2022 for (iter = NM_MANAGER_GET_PRIVATE (self)->devices; iter; iter = iter->next) {
2023 NMDevice *candidate = iter->data;
2024 gboolean compatible = TRUE;
2026 if (strcmp (nm_device_get_iface (candidate), plink->name))
2029 if (nm_device_is_real (candidate)) {
2030 /* Ignore the link added event since there's already a realized
2031 * device with the link's name.
2034 } else if (nm_device_realize_start (candidate, plink, &compatible, &error)) {
2036 _device_realize_finish (self, candidate, plink);
2040 _LOGD (LOGD_DEVICE, "(%s): failed to realize from plink: '%s'",
2041 plink->name, error->message);
2042 g_clear_error (&error);
2044 /* Try next unrealized device */
2047 /* Try registered device factories */
2048 factory = nm_device_factory_manager_find_factory_for_link_type (plink->type);
2050 gboolean ignore = FALSE;
2052 device = nm_device_factory_create_device (factory, plink->name, plink, NULL, &ignore, &error);
2055 _LOGW (LOGD_HW, "%s: factory failed to create device: %s",
2056 plink->name, error->message);
2057 g_clear_error (&error);
2063 if (device == NULL) {
2064 switch (plink->type) {
2065 case NM_LINK_TYPE_WWAN_ETHERNET:
2066 case NM_LINK_TYPE_BNEP:
2067 case NM_LINK_TYPE_OLPC_MESH:
2068 case NM_LINK_TYPE_TEAM:
2069 case NM_LINK_TYPE_WIFI:
2070 _LOGI (LOGD_HW, "(%s): '%s' plugin not available; creating generic device",
2071 plink->name, nm_link_type_to_string (plink->type));
2072 nm_plugin_missing = TRUE;
2075 device = nm_device_generic_new (plink);
2081 if (nm_plugin_missing)
2082 nm_device_set_nm_plugin_missing (device, TRUE);
2083 if (nm_device_realize_start (device, plink, NULL, &error)) {
2084 add_device (self, device, NULL);
2085 _device_realize_finish (self, device, plink);
2087 _LOGW (LOGD_DEVICE, "%s: failed to realize device: %s",
2088 plink->name, error->message);
2089 g_clear_error (&error);
2091 g_object_unref (device);
2098 } PlatformLinkCbData;
2101 _platform_link_cb_idle (PlatformLinkCbData *data)
2103 NMManager *self = data->self;
2104 const NMPlatformLink *l;
2109 g_object_remove_weak_pointer (G_OBJECT (self), (gpointer *) &data->self);
2111 l = nm_platform_link_get (NM_PLATFORM_GET, data->ifindex);
2113 NMPlatformLink pllink;
2115 pllink = *l; /* make a copy of the link instance */
2116 platform_link_added (self, data->ifindex, &pllink);
2119 GError *error = NULL;
2121 device = nm_manager_get_device_by_ifindex (self, data->ifindex);
2123 if (nm_device_is_software (device)) {
2124 /* Our software devices stick around until their connection is removed */
2125 if (!nm_device_unrealize (device, FALSE, &error)) {
2126 _LOGW (LOGD_DEVICE, "(%s): failed to unrealize: %s",
2127 nm_device_get_iface (device),
2129 g_clear_error (&error);
2130 remove_device (self, device, FALSE, TRUE);
2133 /* Hardware and external devices always get removed when their kernel link is gone */
2134 remove_device (self, device, FALSE, TRUE);
2140 g_slice_free (PlatformLinkCbData, data);
2141 return G_SOURCE_REMOVE;
2145 platform_link_cb (NMPlatform *platform,
2146 NMPObjectType obj_type,
2148 NMPlatformLink *plink,
2149 NMPlatformSignalChangeType change_type,
2152 PlatformLinkCbData *data;
2154 switch (change_type) {
2155 case NM_PLATFORM_SIGNAL_ADDED:
2156 case NM_PLATFORM_SIGNAL_REMOVED:
2157 data = g_slice_new (PlatformLinkCbData);
2158 data->self = NM_MANAGER (user_data);
2159 data->ifindex = ifindex;
2160 g_object_add_weak_pointer (G_OBJECT (data->self), (gpointer *) &data->self);
2161 g_idle_add ((GSourceFunc) _platform_link_cb_idle, data);
2169 platform_query_devices (NMManager *self)
2171 GArray *links_array;
2172 NMPlatformLink *links;
2175 links_array = nm_platform_link_get_all (NM_PLATFORM_GET);
2176 links = (NMPlatformLink *) links_array->data;
2177 for (i = 0; i < links_array->len; i++)
2178 platform_link_added (self, links[i].ifindex, &links[i]);
2180 g_array_unref (links_array);
2184 rfkill_manager_rfkill_changed_cb (NMRfkillManager *rfkill_mgr,
2186 RfKillState udev_state,
2189 nm_manager_rfkill_update (NM_MANAGER (user_data), rtype);
2193 nm_manager_get_devices (NMManager *manager)
2195 g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);
2197 return NM_MANAGER_GET_PRIVATE (manager)->devices;
2201 nm_manager_get_connection_device (NMManager *self,
2202 NMConnection *connection)
2204 NMActiveConnection *ac = find_ac_for_connection (self, connection);
2208 return nm_active_connection_get_device (ac);
2212 nm_manager_get_best_device_for_connection (NMManager *self,
2213 NMConnection *connection,
2214 gboolean for_user_request)
2216 const GSList *devices, *iter;
2217 NMDevice *act_device = nm_manager_get_connection_device (self, connection);
2218 NMDeviceCheckConAvailableFlags flags;
2223 flags = for_user_request ? NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST : NM_DEVICE_CHECK_CON_AVAILABLE_NONE;
2225 /* Pick the first device that's compatible with the connection. */
2226 devices = nm_manager_get_devices (self);
2227 for (iter = devices; iter; iter = g_slist_next (iter)) {
2228 NMDevice *device = NM_DEVICE (iter->data);
2230 if (nm_device_check_connection_available (device, connection, flags, NULL))
2239 _get_devices (NMManager *self,
2240 GDBusMethodInvocation *context,
2241 gboolean all_devices)
2243 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
2244 gs_free const char **paths = NULL;
2248 paths = g_new (const char *, g_slist_length (priv->devices) + 1);
2250 for (i = 0, iter = priv->devices; iter; iter = iter->next) {
2253 path = nm_exported_object_get_path (NM_EXPORTED_OBJECT (iter->data));
2255 && (all_devices || nm_device_is_real (iter->data)))
2260 g_dbus_method_invocation_return_value (context,
2261 g_variant_new ("(^ao)", (char **) paths));
2265 impl_manager_get_devices (NMManager *self,
2266 GDBusMethodInvocation *context)
2268 _get_devices (self, context, FALSE);
2272 impl_manager_get_all_devices (NMManager *self,
2273 GDBusMethodInvocation *context)
2275 _get_devices (self, context, TRUE);
2279 impl_manager_get_device_by_ip_iface (NMManager *self,
2280 GDBusMethodInvocation *context,
2284 const char *path = NULL;
2286 device = find_device_by_ip_iface (self, iface);
2288 path = nm_exported_object_get_path (NM_EXPORTED_OBJECT (device));
2291 g_dbus_method_invocation_return_error (context,
2293 NM_MANAGER_ERROR_UNKNOWN_DEVICE,
2294 "No device found for the requested iface.");
2296 g_dbus_method_invocation_return_value (context,
2297 g_variant_new ("(o)", path));
2302 is_compatible_with_slave (NMConnection *master, NMConnection *slave)
2304 NMSettingConnection *s_con;
2306 g_return_val_if_fail (master, FALSE);
2307 g_return_val_if_fail (slave, FALSE);
2309 s_con = nm_connection_get_setting_connection (slave);
2312 return nm_connection_is_type (master, nm_setting_connection_get_slave_type (s_con));
2317 * @self: #NMManager object
2318 * @connection: the #NMConnection to find the master connection and device for
2319 * @device: the #NMDevice, if any, which will activate @connection
2320 * @out_master_connection: on success, the master connection of @connection if
2321 * that master connection was found
2322 * @out_master_device: on success, the master device of @connection if that
2323 * master device was found
2324 * @out_master_ac: on success, the master ActiveConnection of @connection if
2325 * there already is one
2326 * @error: the error, if an error occurred
2328 * Given an #NMConnection, attempts to find its master. If @connection has
2329 * no master, this will return %TRUE and @out_master_connection and
2330 * @out_master_device will be untouched.
2332 * If @connection does have a master, then the outputs depend on what is in its
2333 * #NMSettingConnection:master property:
2335 * If "master" is the ifname of an existing #NMDevice, and that device has a
2336 * compatible master connection activated or activating on it, then
2337 * @out_master_device, @out_master_connection, and @out_master_ac will all be
2338 * set. If the device exists and is idle, only @out_master_device will be set.
2339 * If the device exists and has an incompatible connection on it, an error
2342 * If "master" is the ifname of a non-existent device, then @out_master_device
2343 * will be %NULL, and @out_master_connection will be a connection whose
2344 * activation would cause the creation of that device. @out_master_ac MAY be
2345 * set in this case as well (if the connection has started activating, but has
2346 * not yet created its device).
2348 * If "master" is the UUID of a compatible master connection, then
2349 * @out_master_connection will be the identified connection, and @out_master_device
2350 * and/or @out_master_ac will be set if the connection is currently activating.
2351 * (@out_master_device will not be set if the device exists but does not have
2352 * @out_master_connection active/activating on it.)
2354 * Returns: %TRUE if the master device and/or connection could be found or if
2355 * the connection did not require a master, %FALSE otherwise
2358 find_master (NMManager *self,
2359 NMConnection *connection,
2361 NMSettingsConnection **out_master_connection,
2362 NMDevice **out_master_device,
2363 NMActiveConnection **out_master_ac,
2366 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
2367 NMSettingConnection *s_con;
2369 NMDevice *master_device = NULL;
2370 NMSettingsConnection *master_connection = NULL;
2373 s_con = nm_connection_get_setting_connection (connection);
2375 master = nm_setting_connection_get_master (s_con);
2378 return TRUE; /* success, but no master */
2380 /* Try as an interface name first */
2381 master_device = find_device_by_iface (self, master, NULL, connection);
2382 if (master_device) {
2383 if (master_device == device) {
2384 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_DEPENDENCY_FAILED,
2385 "Device cannot be its own master");
2389 master_connection = nm_device_get_settings_connection (master_device);
2390 if (master_connection && !is_compatible_with_slave (NM_CONNECTION (master_connection), connection)) {
2391 g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_DEPENDENCY_FAILED,
2392 "The active connection on %s is not compatible",
2393 nm_device_get_iface (master_device));
2397 /* Try master as a connection UUID */
2398 master_connection = nm_settings_get_connection_by_uuid (priv->settings, master);
2399 if (master_connection) {
2400 /* Check if the master connection is activated on some device already */
2401 for (iter = priv->devices; iter; iter = g_slist_next (iter)) {
2402 NMDevice *candidate = NM_DEVICE (iter->data);
2404 if (candidate == device)
2407 if (nm_device_get_settings_connection (candidate) == master_connection) {
2408 master_device = candidate;
2415 if (out_master_connection)
2416 *out_master_connection = master_connection;
2417 if (out_master_device)
2418 *out_master_device = master_device;
2419 if (out_master_ac && master_connection)
2420 *out_master_ac = find_ac_for_connection (self, NM_CONNECTION (master_connection));
2422 if (master_device || master_connection)
2425 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_DEVICE,
2426 "Master connection not found or invalid");
2432 * ensure_master_active_connection:
2433 * @self: the #NMManager
2434 * @subject: the #NMAuthSubject representing the requestor of this activation
2435 * @connection: the connection that should depend on @master_connection
2436 * @device: the #NMDevice, if any, which will activate @connection
2437 * @master_connection: the master connection, or %NULL
2438 * @master_device: the master device, or %NULL
2439 * @error: the error, if an error occurred
2441 * Determines whether a given #NMConnection depends on another connection to
2442 * be activated, and if so, finds that master connection or creates it.
2444 * If @master_device and @master_connection are both set then @master_connection
2445 * MUST already be activated or activating on @master_device, and the function will
2446 * return the existing #NMActiveConnection.
2448 * If only @master_device is set, and it has an #NMActiveConnection, then the
2449 * function will return it if it is a compatible master, or an error if not. If it
2450 * doesn't have an AC, then the function will create one if a compatible master
2451 * connection exists, or return an error if not.
2453 * If only @master_connection is set, then this will try to find or create a compatible
2454 * #NMDevice, and either activate @master_connection on that device or return an error.
2456 * Returns: the master #NMActiveConnection that the caller should depend on, or
2457 * %NULL if an error occurred
2459 static NMActiveConnection *
2460 ensure_master_active_connection (NMManager *self,
2461 NMAuthSubject *subject,
2462 NMConnection *connection,
2464 NMSettingsConnection *master_connection,
2465 NMDevice *master_device,
2468 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
2469 NMActiveConnection *master_ac = NULL;
2470 NMDeviceState master_state;
2473 g_assert (connection);
2474 g_assert (master_connection || master_device);
2476 /* If the master device isn't activated then we need to activate it using
2477 * compatible connection. If it's already activating we can just proceed.
2479 if (master_device) {
2480 NMSettingsConnection *device_connection = nm_device_get_settings_connection (master_device);
2482 /* If we're passed a connection and a device, we require that connection
2483 * be already activated on the device, eg returned from find_master().
2485 g_assert (!master_connection || master_connection == device_connection);
2486 if (device_connection && !is_compatible_with_slave (NM_CONNECTION (device_connection), connection)) {
2487 g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_DEPENDENCY_FAILED,
2488 "The active connection %s is not compatible",
2489 nm_connection_get_id (connection));
2493 master_state = nm_device_get_state (master_device);
2494 if ( (master_state == NM_DEVICE_STATE_ACTIVATED)
2495 || nm_device_is_activating (master_device)) {
2496 /* Device already using master_connection */
2497 g_assert (device_connection);
2498 return NM_ACTIVE_CONNECTION (nm_device_get_act_request (master_device));
2501 /* If the device is disconnected, find a compatible connection and
2502 * activate it on the device.
2504 if (master_state == NM_DEVICE_STATE_DISCONNECTED || !nm_device_is_real (master_device)) {
2505 GSList *connections;
2507 g_assert (master_connection == NULL);
2509 /* Find a compatible connection and activate this device using it */
2510 connections = nm_manager_get_activatable_connections (self);
2511 for (iter = connections; iter; iter = g_slist_next (iter)) {
2512 NMSettingsConnection *candidate = NM_SETTINGS_CONNECTION (iter->data);
2514 /* Ensure eg bond/team slave and the candidate master is a
2517 if (!is_compatible_with_slave (NM_CONNECTION (candidate), connection))
2520 if (nm_device_check_connection_available (master_device, NM_CONNECTION (candidate), NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST, NULL)) {
2521 master_ac = nm_manager_activate_connection (self,
2527 g_slist_free (connections);
2531 g_slist_free (connections);
2535 NM_MANAGER_ERROR_UNKNOWN_CONNECTION,
2536 "No compatible connection found.");
2540 /* Otherwise, the device is unmanaged, unavailable, or disconnecting */
2543 NM_MANAGER_ERROR_DEPENDENCY_FAILED,
2544 "Device unmanaged or not available for activation");
2545 } else if (master_connection) {
2546 gboolean found_device = FALSE;
2548 /* Find a compatible device and activate it using this connection */
2549 for (iter = priv->devices; iter; iter = g_slist_next (iter)) {
2550 NMDevice *candidate = NM_DEVICE (iter->data);
2552 if (candidate == device) {
2553 /* A device obviously can't be its own master */
2557 if (!nm_device_check_connection_available (candidate, NM_CONNECTION (master_connection), NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST, NULL))
2560 found_device = TRUE;
2561 if (!nm_device_is_software (candidate)) {
2562 master_state = nm_device_get_state (candidate);
2563 if (nm_device_is_real (candidate) && master_state != NM_DEVICE_STATE_DISCONNECTED)
2567 master_ac = nm_manager_activate_connection (self,
2578 NM_MANAGER_ERROR_UNKNOWN_DEVICE,
2579 "No device available");
2581 g_assert_not_reached ();
2588 * @manager: #NMManager object
2589 * @connection: the master #NMSettingsConnection to find slave connections for
2590 * @device: the master #NMDevice for the @connection
2592 * Given an #NMSettingsConnection, attempts to find its slaves. If @connection is not
2593 * master, or has not any slaves, this will return %NULL.
2595 * Returns: list of slave connections for given master @connection, or %NULL
2598 find_slaves (NMManager *manager,
2599 NMSettingsConnection *connection,
2602 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
2603 GSList *all_connections, *iter;
2604 GSList *slaves = NULL;
2605 NMSettingConnection *s_con;
2608 s_con = nm_connection_get_setting_connection (NM_CONNECTION (connection));
2610 master = nm_setting_connection_get_master (s_con);
2613 return NULL; /* connection is not master */
2615 /* Search through all connections, not only inactive ones, because
2616 * even if a slave was already active, it might be deactivated during
2617 * master reactivation.
2619 all_connections = nm_settings_get_connections (priv->settings);
2620 for (iter = all_connections; iter; iter = iter->next) {
2621 NMSettingsConnection *master_connection = NULL;
2622 NMDevice *master_device = NULL;
2623 NMConnection *candidate = iter->data;
2625 find_master (manager, candidate, NULL, &master_connection, &master_device, NULL, NULL);
2626 if ( (master_connection && master_connection == connection)
2627 || (master_device && master_device == device)) {
2628 slaves = g_slist_prepend (slaves, candidate);
2631 g_slist_free (all_connections);
2633 return g_slist_reverse (slaves);
2637 should_connect_slaves (NMConnection *connection, NMDevice *device)
2639 NMSettingConnection *s_con;
2640 NMSettingConnectionAutoconnectSlaves autoconnect_slaves;
2641 gs_free char *value = NULL;
2643 s_con = nm_connection_get_setting_connection (connection);
2646 /* Check autoconnect-slaves property */
2647 autoconnect_slaves = nm_setting_connection_get_autoconnect_slaves (s_con);
2648 if (autoconnect_slaves != NM_SETTING_CONNECTION_AUTOCONNECT_SLAVES_DEFAULT)
2651 /* Check configuration default for autoconnect-slaves property */
2652 value = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA,
2653 "connection.autoconnect-slaves", device);
2655 autoconnect_slaves = _nm_utils_ascii_str_to_int64 (value, 10, 0, 1, -1);
2658 if (autoconnect_slaves == NM_SETTING_CONNECTION_AUTOCONNECT_SLAVES_NO)
2660 if (autoconnect_slaves == NM_SETTING_CONNECTION_AUTOCONNECT_SLAVES_YES)
2666 autoconnect_slaves (NMManager *self,
2667 NMSettingsConnection *master_connection,
2668 NMDevice *master_device,
2669 NMAuthSubject *subject)
2671 GError *local_err = NULL;
2672 gboolean ret = FALSE;
2674 if (should_connect_slaves (NM_CONNECTION (master_connection), master_device)) {
2675 GSList *slaves, *iter;
2677 iter = slaves = find_slaves (self, master_connection, master_device);
2678 ret = slaves != NULL;
2681 NMSettingsConnection *slave_connection = iter->data;
2684 _LOGD (LOGD_CORE, "will activate slave connection '%s' (%s) as a dependency for master '%s' (%s)",
2685 nm_settings_connection_get_id (slave_connection),
2686 nm_settings_connection_get_uuid (slave_connection),
2687 nm_settings_connection_get_id (master_connection),
2688 nm_settings_connection_get_uuid (master_connection));
2690 /* Schedule slave activation */
2691 nm_manager_activate_connection (self,
2694 nm_manager_get_best_device_for_connection (self, NM_CONNECTION (slave_connection), FALSE),
2698 _LOGW (LOGD_CORE, "Slave connection activation failed: %s", local_err->message);
2699 g_error_free (local_err);
2702 g_slist_free (slaves);
2708 _internal_activate_vpn (NMManager *self, NMActiveConnection *active, GError **error)
2710 g_assert (NM_IS_VPN_CONNECTION (active));
2712 nm_exported_object_export (NM_EXPORTED_OBJECT (active));
2713 return nm_vpn_manager_activate_connection (NM_MANAGER_GET_PRIVATE (self)->vpn_manager,
2714 NM_VPN_CONNECTION (active),
2718 /* Traverse the device to disconnected state. This means that the device is ready
2719 * for connection and will proceed activating if there's an activation request
2723 unmanaged_to_disconnected (NMDevice *device)
2725 /* when creating the software device, it can happen that the device is
2726 * still unmanaged by NM_UNMANAGED_PLATFORM_INIT because we didn't yet
2727 * get the udev event. At this point, we can no longer delay the activation
2728 * and force the device to be managed. */
2729 nm_device_set_unmanaged_by_flags (device, NM_UNMANAGED_PLATFORM_INIT, FALSE, NM_DEVICE_STATE_REASON_USER_REQUESTED);
2731 nm_device_set_unmanaged_by_flags (device, NM_UNMANAGED_USER_EXPLICIT, FALSE, NM_DEVICE_STATE_REASON_USER_REQUESTED);
2733 g_return_if_fail (nm_device_get_managed (device, FALSE));
2735 if (nm_device_get_state (device) == NM_DEVICE_STATE_UNMANAGED) {
2736 nm_device_state_changed (device,
2737 NM_DEVICE_STATE_UNAVAILABLE,
2738 NM_DEVICE_STATE_REASON_USER_REQUESTED);
2741 if ( nm_device_is_available (device, NM_DEVICE_CHECK_DEV_AVAILABLE_FOR_USER_REQUEST)
2742 && (nm_device_get_state (device) == NM_DEVICE_STATE_UNAVAILABLE)) {
2743 nm_device_state_changed (device,
2744 NM_DEVICE_STATE_DISCONNECTED,
2745 NM_DEVICE_STATE_REASON_USER_REQUESTED);
2749 /* The parent connection is ready; we can proceed realizing the device and
2750 * progressing the device to disconencted state.
2753 active_connection_parent_active (NMActiveConnection *active,
2754 NMActiveConnection *parent_ac,
2757 NMDevice *device = nm_active_connection_get_device (active);
2758 GError *error = NULL;
2760 g_signal_handlers_disconnect_by_func (active,
2761 (GCallback) active_connection_parent_active,
2765 NMSettingsConnection *connection = nm_active_connection_get_settings_connection (active);
2766 NMDevice *parent = nm_active_connection_get_device (parent_ac);
2768 if (nm_device_create_and_realize (device, (NMConnection *) connection, parent, &error)) {
2769 /* We can now proceed to disconnected state so that activation proceeds. */
2770 unmanaged_to_disconnected (device);
2772 nm_log_warn (LOGD_CORE, "Could not realize device '%s': %s",
2773 nm_device_get_iface (device), error->message);
2774 nm_active_connection_set_state (active, NM_ACTIVE_CONNECTION_STATE_DEACTIVATED);
2777 nm_log_warn (LOGD_CORE, "The parent connection device '%s' depended on disappeared.",
2778 nm_device_get_iface (device));
2779 nm_active_connection_set_state (active, NM_ACTIVE_CONNECTION_STATE_DEACTIVATED);
2784 _internal_activate_device (NMManager *self, NMActiveConnection *active, GError **error)
2786 NMDevice *device, *existing, *master_device = NULL;
2787 NMConnection *applied;
2788 NMSettingsConnection *connection;
2789 NMSettingsConnection *master_connection = NULL;
2790 NMConnection *existing_connection = NULL;
2791 NMActiveConnection *master_ac = NULL;
2792 NMAuthSubject *subject;
2793 char *error_desc = NULL;
2795 g_return_val_if_fail (NM_IS_MANAGER (self), FALSE);
2796 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (active), FALSE);
2797 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2799 g_assert (NM_IS_VPN_CONNECTION (active) == FALSE);
2801 connection = nm_active_connection_get_settings_connection (active);
2802 g_assert (connection);
2804 applied = nm_active_connection_get_applied_connection (active);
2806 device = nm_active_connection_get_device (active);
2807 g_return_val_if_fail (device != NULL, FALSE);
2809 /* If the device is active and its connection is not visible to the
2810 * user that's requesting this new activation, fail, since other users
2811 * should not be allowed to implicitly deactivate private connections
2812 * by activating a connection of their own.
2814 existing_connection = nm_device_get_applied_connection (device);
2815 subject = nm_active_connection_get_subject (active);
2816 if (existing_connection &&
2817 !nm_auth_is_subject_in_acl (existing_connection,
2822 NM_MANAGER_ERROR_PERMISSION_DENIED,
2823 "Private connection already active on the device: %s",
2825 g_free (error_desc);
2829 /* Final connection must be available on device */
2830 if (!nm_device_check_connection_available (device, applied, NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST, NULL)) {
2831 g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_CONNECTION,
2832 "Connection '%s' is not available on the device %s at this time.",
2833 nm_settings_connection_get_id (connection), nm_device_get_iface (device));
2837 /* Create any backing resources the device needs */
2838 if (!nm_device_is_real (device)) {
2841 parent = find_parent_device_for_connection (self, (NMConnection *) connection, NULL);
2843 if (parent && !nm_device_is_real (parent)) {
2844 NMSettingsConnection *parent_con;
2845 NMActiveConnection *parent_ac;
2847 parent_con = nm_device_get_best_connection (parent, NULL, error);
2849 g_prefix_error (error, "%s failed to create parent: ", nm_device_get_iface (device));
2853 parent_ac = nm_manager_activate_connection (self, parent_con, NULL, parent, subject, error);
2855 g_prefix_error (error, "%s failed to activate parent: ", nm_device_get_iface (device));
2859 /* We can't realize now; defer until the parent device is ready. */
2860 g_signal_connect (active,
2861 NM_ACTIVE_CONNECTION_PARENT_ACTIVE,
2862 (GCallback) active_connection_parent_active,
2864 nm_active_connection_set_parent (active, parent_ac);
2866 /* We can realize now; no need to wait for a parent device. */
2867 if (!nm_device_create_and_realize (device, (NMConnection *) connection, parent, error)) {
2868 g_prefix_error (error, "%s failed to create resources: ", nm_device_get_iface (device));
2874 /* Try to find the master connection/device if the connection has a dependency */
2875 if (!find_master (self, applied, device,
2876 &master_connection, &master_device, &master_ac,
2878 g_prefix_error (error, "Can not find a master for %s: ",
2879 nm_settings_connection_get_id (connection));
2883 /* Ensure there's a master active connection the new connection we're
2884 * activating can depend on.
2886 if (master_connection || master_device) {
2887 if (master_connection) {
2888 _LOGD (LOGD_CORE, "Activation of '%s' requires master connection '%s'",
2889 nm_settings_connection_get_id (connection),
2890 nm_settings_connection_get_id (master_connection));
2892 if (master_device) {
2893 _LOGD (LOGD_CORE, "Activation of '%s' requires master device '%s'",
2894 nm_settings_connection_get_id (connection),
2895 nm_device_get_ip_iface (master_device));
2898 /* Ensure eg bond slave and the candidate master is a bond master */
2899 if (master_connection && !is_compatible_with_slave (NM_CONNECTION (master_connection), applied)) {
2900 g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_DEPENDENCY_FAILED,
2901 "The master connection '%s' is not compatible with '%s'",
2902 nm_settings_connection_get_id (master_connection),
2903 nm_settings_connection_get_id (connection));
2908 master_ac = ensure_master_active_connection (self,
2909 nm_active_connection_get_subject (active),
2916 if (master_device) {
2917 g_prefix_error (error, "Master device '%s' can't be activated: ",
2918 nm_device_get_ip_iface (device));
2920 g_prefix_error (error, "Master connection '%s' can't be activated: ",
2921 nm_settings_connection_get_id (connection));
2927 nm_active_connection_set_master (active, master_ac);
2928 _LOGD (LOGD_CORE, "Activation of '%s' depends on active connection %p %s",
2929 nm_settings_connection_get_id (connection),
2931 nm_exported_object_get_path (NM_EXPORTED_OBJECT (master_ac)) ?: "");
2934 /* Check slaves for master connection and possibly activate them */
2935 autoconnect_slaves (self, connection, device, nm_active_connection_get_subject (active));
2937 /* Disconnect the connection if connected or queued on another device */
2938 existing = nm_manager_get_connection_device (self, NM_CONNECTION (connection));
2940 nm_device_steal_connection (existing, connection);
2942 /* If the device is there, we can ready it for the activation. */
2943 if (nm_device_is_real (device))
2944 unmanaged_to_disconnected (device);
2946 /* Export the new ActiveConnection to clients and start it on the device */
2947 nm_exported_object_export (NM_EXPORTED_OBJECT (active));
2948 nm_device_queue_activation (device, NM_ACT_REQUEST (active));
2953 _internal_activate_generic (NMManager *self, NMActiveConnection *active, GError **error)
2955 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
2956 gboolean success = FALSE;
2958 /* Ensure activation request is still valid, eg that its device hasn't gone
2959 * away or that some other dependency has not failed.
2961 if (nm_active_connection_get_state (active) >= NM_ACTIVE_CONNECTION_STATE_DEACTIVATING) {
2962 g_set_error_literal (error,
2964 NM_MANAGER_ERROR_DEPENDENCY_FAILED,
2965 "Activation failed because dependencies failed.");
2969 if (NM_IS_VPN_CONNECTION (active))
2970 success = _internal_activate_vpn (self, active, error);
2972 success = _internal_activate_device (self, active, error);
2975 /* Force an update of the Manager's activating-connection property.
2976 * The device changes state before the AC gets exported, which causes
2977 * the manager's 'activating-connection' property to be NULL since the
2978 * AC only gets a D-Bus path when it's exported. So now that the AC
2979 * is exported, make sure the manager's activating-connection property
2982 active_connection_add (self, active);
2983 policy_activating_device_changed (G_OBJECT (priv->policy), NULL, self);
2989 static NMActiveConnection *
2990 _new_vpn_active_connection (NMManager *self,
2991 NMSettingsConnection *settings_connection,
2992 const char *specific_object,
2993 NMAuthSubject *subject,
2996 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
2997 NMActiveConnection *parent = NULL;
2998 NMDevice *device = NULL;
3000 g_return_val_if_fail (!settings_connection || NM_IS_SETTINGS_CONNECTION (settings_connection), NULL);
3002 if (specific_object) {
3003 /* Find the specific connection the client requested we use */
3004 parent = active_connection_get_by_path (self, specific_object);
3006 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_CONNECTION_NOT_ACTIVE,
3007 "Base connection for VPN connection not active.");
3011 parent = priv->primary_connection;
3014 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_CONNECTION,
3015 "Could not find source connection.");
3019 device = nm_active_connection_get_device (parent);
3021 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_DEVICE,
3022 "Source connection had no active device.");
3026 return (NMActiveConnection *) nm_vpn_connection_new (settings_connection,
3028 nm_exported_object_get_path (NM_EXPORTED_OBJECT (parent)),
3032 static NMActiveConnection *
3033 _new_active_connection (NMManager *self,
3034 NMConnection *connection,
3035 const char *specific_object,
3037 NMAuthSubject *subject,
3040 NMSettingsConnection *settings_connection = NULL;
3041 NMActiveConnection *existing_ac;
3044 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
3045 g_return_val_if_fail (NM_IS_AUTH_SUBJECT (subject), NULL);
3047 /* Can't create new AC for already-active connection */
3048 existing_ac = find_ac_for_connection (self, connection);
3049 if (NM_IS_VPN_CONNECTION (existing_ac)) {
3050 g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_CONNECTION_ALREADY_ACTIVE,
3051 "Connection '%s' is already active",
3052 nm_connection_get_id (connection));
3056 /* Normalize the specific object */
3057 if (specific_object && g_strcmp0 (specific_object, "/") == 0)
3058 specific_object = NULL;
3060 is_vpn = nm_connection_is_type (NM_CONNECTION (connection), NM_SETTING_VPN_SETTING_NAME);
3062 if (NM_IS_SETTINGS_CONNECTION (connection))
3063 settings_connection = (NMSettingsConnection *) connection;
3066 return _new_vpn_active_connection (self,
3067 settings_connection,
3073 return (NMActiveConnection *) nm_act_request_new (settings_connection,
3080 _internal_activation_failed (NMManager *self,
3081 NMActiveConnection *active,
3082 const char *error_desc)
3084 _LOGD (LOGD_CORE, "Failed to activate '%s': %s",
3085 nm_active_connection_get_settings_connection_id (active),
3088 if (nm_active_connection_get_state (active) <= NM_ACTIVE_CONNECTION_STATE_ACTIVATED) {
3089 nm_active_connection_set_state (active, NM_ACTIVE_CONNECTION_STATE_DEACTIVATING);
3090 nm_active_connection_set_state (active, NM_ACTIVE_CONNECTION_STATE_DEACTIVATED);
3095 _internal_activation_auth_done (NMActiveConnection *active,
3097 const char *error_desc,
3098 gpointer user_data1,
3099 gpointer user_data2)
3101 NMManager *self = user_data1;
3102 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3103 GError *error = NULL;
3105 priv->authorizing_connections = g_slist_remove (priv->authorizing_connections, active);
3108 if (_internal_activate_generic (self, active, &error)) {
3109 g_object_unref (active);
3114 g_assert (error_desc || error);
3115 _internal_activation_failed (self, active, error_desc ? error_desc : error->message);
3116 g_object_unref (active);
3117 g_clear_error (&error);
3121 * nm_manager_activate_connection():
3122 * @self: the #NMManager
3123 * @connection: the #NMSettingsConnection to activate on @device
3124 * @specific_object: the specific object path, if any, for the activation
3125 * @device: the #NMDevice to activate @connection on
3126 * @subject: the subject which requested activation
3127 * @error: return location for an error
3129 * Begins a new internally-initiated activation of @connection on @device.
3130 * @subject should be the subject of the activation that triggered this
3131 * one, or if this is an autoconnect request, a new internal subject.
3132 * The returned #NMActiveConnection is owned by the Manager and should be
3133 * referenced by the caller if the caller continues to use it.
3135 * Returns: (transfer none): the new #NMActiveConnection that tracks
3136 * activation of @connection on @device
3138 NMActiveConnection *
3139 nm_manager_activate_connection (NMManager *self,
3140 NMSettingsConnection *connection,
3141 const char *specific_object,
3143 NMAuthSubject *subject,
3146 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3147 NMActiveConnection *active;
3148 char *error_desc = NULL;
3151 g_return_val_if_fail (self != NULL, NULL);
3152 g_return_val_if_fail (connection != NULL, NULL);
3153 g_return_val_if_fail (error != NULL, NULL);
3154 g_return_val_if_fail (*error == NULL, NULL);
3156 /* Ensure the subject has permissions for this connection */
3157 if (!nm_auth_is_subject_in_acl (NM_CONNECTION (connection),
3160 g_set_error_literal (error,
3162 NM_MANAGER_ERROR_PERMISSION_DENIED,
3164 g_free (error_desc);
3168 /* Look for a active connection that's equivalent and is already pending authorization
3169 * and eventual activation. This is used to de-duplicate concurrent activations which would
3170 * otherwise race and cause the device to disconnect and reconnect repeatedly.
3171 * In particular, this allows the master and multiple slaves to concurrently auto-activate
3172 * while all the slaves would use the same active-connection. */
3173 for (iter = priv->authorizing_connections; iter; iter = g_slist_next (iter)) {
3174 active = iter->data;
3176 if ( connection == nm_active_connection_get_settings_connection (active)
3177 && g_strcmp0 (nm_active_connection_get_specific_object (active), specific_object) == 0
3178 && nm_active_connection_get_device (active) == device
3179 && nm_auth_subject_is_internal (nm_active_connection_get_subject (active))
3180 && nm_auth_subject_is_internal (subject))
3184 active = _new_active_connection (self,
3185 NM_CONNECTION (connection),
3191 priv->authorizing_connections = g_slist_prepend (priv->authorizing_connections, active);
3192 nm_active_connection_authorize (active, NULL, _internal_activation_auth_done, self, NULL);
3198 * validate_activation_request:
3199 * @self: the #NMManager
3200 * @context: the D-Bus context of the requestor
3201 * @connection: the partial or complete #NMConnection to be activated
3202 * @device_path: the object path of the device to be activated, or "/"
3203 * @out_device: on successful reutrn, the #NMDevice to be activated with @connection
3204 * @out_vpn: on successful return, %TRUE if @connection is a VPN connection
3205 * @error: location to store an error on failure
3207 * Performs basic validation on an activation request, including ensuring that
3208 * the requestor is a valid Unix process, is not disallowed in @connection
3209 * permissions, and that a device exists that can activate @connection.
3211 * Returns: on success, the #NMAuthSubject representing the requestor, or
3214 static NMAuthSubject *
3215 validate_activation_request (NMManager *self,
3216 GDBusMethodInvocation *context,
3217 NMConnection *connection,
3218 const char *device_path,
3219 NMDevice **out_device,
3223 NMDevice *device = NULL;
3224 gboolean vpn = FALSE;
3225 NMAuthSubject *subject = NULL;
3226 char *error_desc = NULL;
3228 g_assert (connection);
3229 g_assert (out_device);
3232 /* Validate the caller */
3233 subject = nm_auth_subject_new_unix_process_from_context (context);
3235 g_set_error_literal (error,
3237 NM_MANAGER_ERROR_PERMISSION_DENIED,
3238 "Failed to get request UID.");
3242 /* Ensure the subject has permissions for this connection */
3243 if (!nm_auth_is_subject_in_acl (connection,
3246 g_set_error_literal (error,
3248 NM_MANAGER_ERROR_PERMISSION_DENIED,
3250 g_free (error_desc);
3254 /* Not implemented yet, we want to fail early */
3255 if ( nm_connection_get_setting_connection (connection)
3256 && nm_connection_get_setting_ip6_config (connection)
3257 && !strcmp (nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG),
3258 NM_SETTING_IP6_CONFIG_METHOD_SHARED)) {
3259 g_set_error_literal (error,
3261 NM_MANAGER_ERROR_CONNECTION_NOT_AVAILABLE,
3262 "Sharing IPv6 connections is not supported yet.");
3266 /* Check whether it's a VPN or not */
3267 if ( nm_connection_get_setting_vpn (connection)
3268 || nm_connection_is_type (connection, NM_SETTING_VPN_SETTING_NAME))
3271 /* Normalize device path */
3272 if (device_path && g_strcmp0 (device_path, "/") == 0)
3275 /* And validate it */
3277 device = nm_manager_get_device_by_path (self, device_path);
3279 g_set_error_literal (error,
3281 NM_MANAGER_ERROR_UNKNOWN_DEVICE,
3282 "Device not found");
3286 device = nm_manager_get_best_device_for_connection (self, connection, TRUE);
3288 if (!device && !vpn) {
3289 gboolean is_software = nm_connection_is_virtual (connection);
3291 /* VPN and software-device connections don't need a device yet */
3293 g_set_error_literal (error,
3295 NM_MANAGER_ERROR_UNKNOWN_DEVICE,
3296 "No suitable device found for this connection.");
3303 /* Look for an existing device with the connection's interface name */
3304 iface = nm_manager_get_connection_iface (self, connection, NULL, error);
3308 device = find_device_by_iface (self, iface, connection, NULL);
3313 if ((!vpn || device_path) && !device) {
3314 g_set_error_literal (error,
3316 NM_MANAGER_ERROR_UNKNOWN_DEVICE,
3317 "Failed to find a compatible device for this connection");
3321 *out_device = device;
3326 g_object_unref (subject);
3330 /***********************************************************************/
3333 _activation_auth_done (NMActiveConnection *active,
3335 const char *error_desc,
3336 gpointer user_data1,
3337 gpointer user_data2)
3339 NMManager *self = user_data1;
3340 GDBusMethodInvocation *context = user_data2;
3341 GError *error = NULL;
3342 NMAuthSubject *subject;
3343 NMSettingsConnection *connection;
3345 subject = nm_active_connection_get_subject (active);
3346 connection = nm_active_connection_get_settings_connection (active);
3349 if (_internal_activate_generic (self, active, &error)) {
3350 g_dbus_method_invocation_return_value (context,
3351 g_variant_new ("(o)",
3352 nm_exported_object_get_path (NM_EXPORTED_OBJECT (active))));
3353 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ACTIVATE, connection, TRUE,
3355 g_object_unref (active);
3359 error = g_error_new_literal (NM_MANAGER_ERROR,
3360 NM_MANAGER_ERROR_PERMISSION_DENIED,
3365 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ACTIVATE, connection, FALSE,
3366 subject, error->message);
3367 _internal_activation_failed (self, active, error->message);
3369 g_object_unref (active);
3370 g_dbus_method_invocation_take_error (context, error);
3374 impl_manager_activate_connection (NMManager *self,
3375 GDBusMethodInvocation *context,
3376 const char *connection_path,
3377 const char *device_path,
3378 const char *specific_object_path)
3380 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3381 NMActiveConnection *active = NULL;
3382 NMAuthSubject *subject = NULL;
3383 NMSettingsConnection *connection = NULL;
3384 NMDevice *device = NULL;
3385 gboolean is_vpn = FALSE;
3386 GError *error = NULL;
3388 /* Normalize object paths */
3389 if (g_strcmp0 (connection_path, "/") == 0)
3390 connection_path = NULL;
3391 if (g_strcmp0 (specific_object_path, "/") == 0)
3392 specific_object_path = NULL;
3393 if (g_strcmp0 (device_path, "/") == 0)
3396 /* If the connection path is given and valid, that connection is activated.
3397 * Otherwise the "best" connection for the device is chosen and activated,
3398 * regardless of whether that connection is autoconnect-enabled or not
3399 * (since this is an explicit request, not an auto-activation request).
3401 if (connection_path) {
3402 connection = nm_settings_get_connection_by_path (priv->settings, connection_path);
3404 error = g_error_new_literal (NM_MANAGER_ERROR,
3405 NM_MANAGER_ERROR_UNKNOWN_CONNECTION,
3406 "Connection could not be found.");
3410 /* If no connection is given, find a suitable connection for the given device path */
3412 error = g_error_new_literal (NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_DEVICE,
3413 "Only devices may be activated without a specifying a connection");
3416 device = nm_manager_get_device_by_path (self, device_path);
3418 error = g_error_new (NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_DEVICE,
3419 "Can not activate an unknown device '%s'", device_path);
3423 connection = nm_device_get_best_connection (device, specific_object_path, &error);
3428 subject = validate_activation_request (self,
3430 NM_CONNECTION (connection),
3438 active = _new_active_connection (self,
3439 NM_CONNECTION (connection),
3440 specific_object_path,
3447 nm_active_connection_authorize (active, NULL, _activation_auth_done, self, context);
3448 g_clear_object (&subject);
3453 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ACTIVATE, connection, FALSE,
3454 subject, error->message);
3456 g_clear_object (&active);
3457 g_clear_object (&subject);
3460 g_dbus_method_invocation_take_error (context, error);
3463 /***********************************************************************/
3467 NMActiveConnection *active;
3468 } AddAndActivateInfo;
3471 activation_add_done (NMSettings *settings,
3472 NMSettingsConnection *new_connection,
3474 GDBusMethodInvocation *context,
3475 NMAuthSubject *subject,
3478 AddAndActivateInfo *info = user_data;
3480 gs_unref_object NMActiveConnection *active = NULL;
3481 GError *local = NULL;
3483 self = info->manager;
3484 active = info->active;
3485 g_slice_free (AddAndActivateInfo, info);
3488 nm_active_connection_set_settings_connection (active, new_connection);
3490 if (_internal_activate_generic (self, active, &local)) {
3491 nm_settings_connection_commit_changes (new_connection,
3492 NM_SETTINGS_CONNECTION_COMMIT_REASON_USER_ACTION | NM_SETTINGS_CONNECTION_COMMIT_REASON_ID_CHANGED,
3494 g_dbus_method_invocation_return_value (
3496 g_variant_new ("(oo)",
3497 nm_connection_get_path (NM_CONNECTION (new_connection)),
3498 nm_exported_object_get_path (NM_EXPORTED_OBJECT (active))));
3499 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ADD_ACTIVATE,
3500 nm_active_connection_get_settings_connection (active),
3502 nm_active_connection_get_subject (active),
3510 _internal_activation_failed (self, active, error->message);
3511 nm_settings_connection_delete (new_connection, NULL, NULL);
3512 g_dbus_method_invocation_return_gerror (context, error);
3513 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ADD_ACTIVATE,
3516 nm_active_connection_get_subject (active),
3518 g_object_unref (active);
3519 g_clear_error (&local);
3523 _add_and_activate_auth_done (NMActiveConnection *active,
3525 const char *error_desc,
3526 gpointer user_data1,
3527 gpointer user_data2)
3529 NMManager *self = user_data1;
3530 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3531 GDBusMethodInvocation *context = user_data2;
3532 AddAndActivateInfo *info;
3533 GError *error = NULL;
3536 NMConnection *connection;
3538 connection = g_object_steal_data (G_OBJECT (active),
3539 TAG_ACTIVE_CONNETION_ADD_AND_ACTIVATE);
3541 info = g_slice_new (AddAndActivateInfo);
3542 info->manager = self;
3543 info->active = g_object_ref (active);
3545 /* Basic sender auth checks performed; try to add the connection */
3546 nm_settings_add_connection_dbus (priv->settings,
3550 activation_add_done,
3552 g_object_unref (connection);
3554 g_assert (error_desc);
3555 error = g_error_new_literal (NM_MANAGER_ERROR,
3556 NM_MANAGER_ERROR_PERMISSION_DENIED,
3558 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ADD_ACTIVATE,
3561 nm_active_connection_get_subject (active),
3563 g_dbus_method_invocation_take_error (context, error);
3566 g_object_unref (active);
3570 impl_manager_add_and_activate_connection (NMManager *self,
3571 GDBusMethodInvocation *context,
3573 const char *device_path,
3574 const char *specific_object_path)
3576 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3577 NMConnection *connection = NULL;
3578 GSList *all_connections = NULL;
3579 NMActiveConnection *active = NULL;
3580 NMAuthSubject *subject = NULL;
3581 GError *error = NULL;
3582 NMDevice *device = NULL;
3583 gboolean vpn = FALSE;
3585 /* Normalize object paths */
3586 if (g_strcmp0 (specific_object_path, "/") == 0)
3587 specific_object_path = NULL;
3588 if (g_strcmp0 (device_path, "/") == 0)
3591 /* Try to create a new connection with the given settings.
3592 * We allow empty settings for AddAndActivateConnection(). In that case,
3593 * the connection will be completed in nm_utils_complete_generic() or
3594 * nm_device_complete_connection() below. Just make sure we don't expect
3595 * specific data being in the connection till then (especially in
3596 * validate_activation_request()).
3598 connection = nm_simple_connection_new ();
3599 if (settings && g_variant_n_children (settings))
3600 _nm_connection_replace_settings (connection, settings, NM_SETTING_PARSE_FLAGS_STRICT, NULL);
3602 subject = validate_activation_request (self,
3612 all_connections = nm_settings_get_connections (priv->settings);
3614 /* Try to fill the VPN's connection setting and name at least */
3615 if (!nm_connection_get_setting_vpn (connection)) {
3616 error = g_error_new_literal (NM_CONNECTION_ERROR,
3617 NM_CONNECTION_ERROR_MISSING_SETTING,
3618 "VPN connections require a 'vpn' setting");
3619 g_prefix_error (&error, "%s: ", NM_SETTING_VPN_SETTING_NAME);
3623 nm_utils_complete_generic (NM_PLATFORM_GET,
3625 NM_SETTING_VPN_SETTING_NAME,
3628 _("VPN connection"),
3630 FALSE); /* No IPv6 by default for now */
3632 /* Let each device subclass complete the connection */
3633 if (!nm_device_complete_connection (device,
3635 specific_object_path,
3640 g_slist_free (all_connections);
3641 all_connections = NULL;
3643 active = _new_active_connection (self,
3645 specific_object_path,
3652 g_object_set_data_full (G_OBJECT (active),
3653 TAG_ACTIVE_CONNETION_ADD_AND_ACTIVATE,
3657 nm_active_connection_authorize (active, connection, _add_and_activate_auth_done, self, context);
3658 g_object_unref (subject);
3662 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ADD_ACTIVATE, NULL, FALSE, subject, error->message);
3663 g_clear_object (&connection);
3664 g_slist_free (all_connections);
3665 g_clear_object (&subject);
3666 g_clear_object (&active);
3669 g_dbus_method_invocation_take_error (context, error);
3672 /***********************************************************************/
3675 nm_manager_deactivate_connection (NMManager *manager,
3676 const char *connection_path,
3677 NMDeviceStateReason reason,
3680 NMActiveConnection *active;
3681 gboolean success = FALSE;
3683 active = active_connection_get_by_path (manager, connection_path);
3685 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_CONNECTION_NOT_ACTIVE,
3686 "The connection was not active.");
3690 if (NM_IS_VPN_CONNECTION (active)) {
3691 NMVpnConnectionStateReason vpn_reason = NM_VPN_CONNECTION_STATE_REASON_USER_DISCONNECTED;
3693 if (reason == NM_DEVICE_STATE_REASON_CONNECTION_REMOVED)
3694 vpn_reason = NM_VPN_CONNECTION_STATE_REASON_CONNECTION_REMOVED;
3695 if (nm_vpn_connection_deactivate (NM_VPN_CONNECTION (active), vpn_reason, FALSE))
3698 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_CONNECTION_NOT_ACTIVE,
3699 "The VPN connection was not active.");
3701 g_assert (NM_IS_ACT_REQUEST (active));
3702 nm_device_state_changed (nm_active_connection_get_device (active),
3703 NM_DEVICE_STATE_DEACTIVATING,
3709 g_object_notify (G_OBJECT (manager), NM_MANAGER_ACTIVE_CONNECTIONS);
3715 deactivate_net_auth_done_cb (NMAuthChain *chain,
3717 GDBusMethodInvocation *context,
3720 NMManager *self = NM_MANAGER (user_data);
3721 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3722 GError *error = NULL;
3723 NMAuthCallResult result;
3724 NMActiveConnection *active;
3729 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
3731 path = nm_auth_chain_get_data (chain, "path");
3732 result = nm_auth_chain_get_result (chain, NM_AUTH_PERMISSION_NETWORK_CONTROL);
3735 _LOGD (LOGD_CORE, "Disconnect request failed: %s", auth_error->message);
3736 error = g_error_new (NM_MANAGER_ERROR,
3737 NM_MANAGER_ERROR_PERMISSION_DENIED,
3738 "Deactivate request failed: %s",
3739 auth_error->message);
3740 } else if (result != NM_AUTH_CALL_RESULT_YES) {
3741 error = g_error_new_literal (NM_MANAGER_ERROR,
3742 NM_MANAGER_ERROR_PERMISSION_DENIED,
3743 "Not authorized to deactivate connections");
3745 /* success; deactivation allowed */
3746 if (!nm_manager_deactivate_connection (self,
3748 NM_DEVICE_STATE_REASON_USER_REQUESTED,
3753 active = active_connection_get_by_path (self, path);
3755 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_DEACTIVATE,
3756 nm_active_connection_get_settings_connection (active),
3758 nm_auth_chain_get_subject (chain),
3759 error ? error->message : NULL);
3763 g_dbus_method_invocation_take_error (context, error);
3765 g_dbus_method_invocation_return_value (context, NULL);
3767 nm_auth_chain_unref (chain);
3771 impl_manager_deactivate_connection (NMManager *self,
3772 GDBusMethodInvocation *context,
3773 const char *active_path)
3775 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3776 NMActiveConnection *ac;
3777 NMSettingsConnection *connection = NULL;
3778 GError *error = NULL;
3779 NMAuthSubject *subject = NULL;
3781 char *error_desc = NULL;
3783 /* Find the connection by its object path */
3784 ac = active_connection_get_by_path (self, active_path);
3786 connection = nm_active_connection_get_settings_connection (ac);
3789 error = g_error_new_literal (NM_MANAGER_ERROR,
3790 NM_MANAGER_ERROR_CONNECTION_NOT_ACTIVE,
3791 "The connection was not active.");
3795 /* Validate the caller */
3796 subject = nm_auth_subject_new_unix_process_from_context (context);
3798 error = g_error_new_literal (NM_MANAGER_ERROR,
3799 NM_MANAGER_ERROR_PERMISSION_DENIED,
3800 "Failed to get request UID.");
3804 /* Ensure the subject has permissions for this connection */
3805 if (!nm_auth_is_subject_in_acl (NM_CONNECTION (connection),
3808 error = g_error_new_literal (NM_MANAGER_ERROR,
3809 NM_MANAGER_ERROR_PERMISSION_DENIED,
3811 g_free (error_desc);
3815 /* Validate the user request */
3816 chain = nm_auth_chain_new_subject (subject, context, deactivate_net_auth_done_cb, self);
3818 error = g_error_new_literal (NM_MANAGER_ERROR,
3819 NM_MANAGER_ERROR_PERMISSION_DENIED,
3820 "Unable to authenticate request.");
3824 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
3825 nm_auth_chain_set_data (chain, "path", g_strdup (active_path), g_free);
3826 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_NETWORK_CONTROL, TRUE);
3831 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_DEACTIVATE, connection, FALSE,
3832 subject, error->message);
3834 g_dbus_method_invocation_take_error (context, error);
3836 g_clear_object (&subject);
3840 device_is_wake_on_lan (NMDevice *device)
3842 return nm_platform_link_get_wake_on_lan (NM_PLATFORM_GET, nm_device_get_ip_ifindex (device));
3846 do_sleep_wake (NMManager *self, gboolean sleeping_changed)
3848 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3849 gboolean suspending, waking_from_suspend;
3852 suspending = sleeping_changed && priv->sleeping;
3853 waking_from_suspend = sleeping_changed && !priv->sleeping;
3855 if (manager_sleeping (self)) {
3856 _LOGI (LOGD_SUSPEND, "%s...", suspending ? "sleeping" : "disabling");
3858 /* FIXME: are there still hardware devices that need to be disabled around
3861 for (iter = priv->devices; iter; iter = iter->next) {
3862 NMDevice *device = iter->data;
3864 /* FIXME: shouldn't we be unmanaging software devices if !suspending? */
3865 if (nm_device_is_software (device))
3867 /* Wake-on-LAN devices will be taken down post-suspend rather than pre- */
3868 if (suspending && device_is_wake_on_lan (device))
3871 nm_device_set_unmanaged_by_flags (device, NM_UNMANAGED_SLEEPING, TRUE, NM_DEVICE_STATE_REASON_SLEEPING);
3874 _LOGI (LOGD_SUSPEND, "%s...", waking_from_suspend ? "waking up" : "re-enabling");
3876 if (waking_from_suspend) {
3877 /* Belatedly take down Wake-on-LAN devices; ideally we wouldn't have to do this
3878 * but for now it's the only way to make sure we re-check their connectivity.
3880 for (iter = priv->devices; iter; iter = iter->next) {
3881 NMDevice *device = iter->data;
3883 if (nm_device_is_software (device))
3885 if (device_is_wake_on_lan (device))
3886 nm_device_set_unmanaged_by_flags (device, NM_UNMANAGED_SLEEPING, TRUE, NM_DEVICE_STATE_REASON_SLEEPING);
3890 /* Ensure rfkill state is up-to-date since we don't respond to state
3891 * changes during sleep.
3893 nm_manager_rfkill_update (self, RFKILL_TYPE_UNKNOWN);
3895 /* Re-manage managed devices */
3896 for (iter = priv->devices; iter; iter = iter->next) {
3897 NMDevice *device = NM_DEVICE (iter->data);
3900 if (nm_device_is_software (device)) {
3901 /* We do not manage/unmanage software devices but
3902 * their dhcp leases could have gone stale so we need
3904 nm_device_update_dynamic_ip_setup (device);
3908 /* enable/disable wireless devices since that we don't respond
3909 * to killswitch changes during sleep.
3911 for (i = 0; i < RFKILL_TYPE_MAX; i++) {
3912 RadioState *rstate = &priv->radio_states[i];
3913 gboolean enabled = radio_enabled_for_rstate (rstate, TRUE);
3916 _LOGD (LOGD_RFKILL, "%s %s devices (hw_enabled %d, sw_enabled %d, user_enabled %d)",
3917 enabled ? "enabling" : "disabling",
3918 rstate->desc, rstate->hw_enabled, rstate->sw_enabled, rstate->user_enabled);
3921 if (nm_device_get_rfkill_type (device) == rstate->rtype)
3922 nm_device_set_enabled (device, enabled);
3925 nm_device_set_autoconnect (device, TRUE);
3927 nm_device_set_unmanaged_by_flags (device, NM_UNMANAGED_SLEEPING, FALSE, NM_DEVICE_STATE_REASON_NOW_MANAGED);
3931 nm_manager_update_state (self);
3935 _internal_sleep (NMManager *self, gboolean do_sleep)
3937 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3939 if (priv->sleeping == do_sleep)
3942 _LOGI (LOGD_SUSPEND, "%s requested (sleeping: %s enabled: %s)",
3943 do_sleep ? "sleep" : "wake",
3944 priv->sleeping ? "yes" : "no",
3945 priv->net_enabled ? "yes" : "no");
3947 priv->sleeping = do_sleep;
3949 do_sleep_wake (self, TRUE);
3951 g_object_notify (G_OBJECT (self), NM_MANAGER_SLEEPING);
3956 sleep_auth_done_cb (NMAuthChain *chain,
3958 GDBusMethodInvocation *context,
3961 NMManager *self = NM_MANAGER (user_data);
3962 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
3964 NMAuthCallResult result;
3967 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
3969 result = nm_auth_chain_get_result (chain, NM_AUTH_PERMISSION_SLEEP_WAKE);
3971 _LOGD (LOGD_SUSPEND, "Sleep/wake request failed: %s", error->message);
3972 ret_error = g_error_new (NM_MANAGER_ERROR,
3973 NM_MANAGER_ERROR_PERMISSION_DENIED,
3974 "Sleep/wake request failed: %s",
3976 g_dbus_method_invocation_take_error (context, ret_error);
3977 } else if (result != NM_AUTH_CALL_RESULT_YES) {
3978 ret_error = g_error_new_literal (NM_MANAGER_ERROR,
3979 NM_MANAGER_ERROR_PERMISSION_DENIED,
3980 "Not authorized to sleep/wake");
3981 g_dbus_method_invocation_take_error (context, ret_error);
3984 do_sleep = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, "sleep"));
3985 _internal_sleep (self, do_sleep);
3986 g_dbus_method_invocation_return_value (context, NULL);
3989 nm_auth_chain_unref (chain);
3994 impl_manager_sleep (NMManager *self,
3995 GDBusMethodInvocation *context,
3998 NMManagerPrivate *priv;
3999 GError *error = NULL;
4000 gs_unref_object NMAuthSubject *subject = NULL;
4003 const char *error_desc = NULL;
4006 g_return_if_fail (NM_IS_MANAGER (self));
4008 priv = NM_MANAGER_GET_PRIVATE (self);
4009 subject = nm_auth_subject_new_unix_process_from_context (context);
4011 if (priv->sleeping == do_sleep) {
4012 error = g_error_new (NM_MANAGER_ERROR,
4013 NM_MANAGER_ERROR_ALREADY_ASLEEP_OR_AWAKE,
4014 "Already %s", do_sleep ? "asleep" : "awake");
4015 nm_audit_log_control_op (NM_AUDIT_OP_SLEEP_CONTROL, do_sleep ? "on" : "off", FALSE, subject,
4017 g_dbus_method_invocation_take_error (context, error);
4021 /* Unconditionally allow the request. Previously it was polkit protected
4022 * but unfortunately that doesn't work for short-lived processes like
4023 * pm-utils. It uses dbus-send without --print-reply, which quits
4024 * immediately after sending the request, and NM is unable to obtain the
4025 * sender's UID as dbus-send has already dropped off the bus. Thus NM
4026 * fails the request. Instead, don't validate the request, but rely on
4027 * D-Bus permissions to restrict the call to root.
4029 _internal_sleep (self, do_sleep);
4030 nm_audit_log_control_op (NM_AUDIT_OP_SLEEP_CONTROL, do_sleep ? "on" : "off", TRUE, subject, NULL);
4031 g_dbus_method_invocation_return_value (context, NULL);
4035 chain = nm_auth_chain_new (context, sleep_auth_done_cb, self, &error_desc);
4037 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
4038 nm_auth_chain_set_data (chain, "sleep", GUINT_TO_POINTER (do_sleep), NULL);
4039 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SLEEP_WAKE, TRUE);
4041 error = g_error_new_literal (NM_MANAGER_ERROR,
4042 NM_MANAGER_ERROR_PERMISSION_DENIED,
4044 g_dbus_method_invocation_take_error (context, error);
4050 sleeping_cb (NMSleepMonitor *monitor, gpointer user_data)
4052 nm_log_dbg (LOGD_SUSPEND, "Received sleeping signal");
4053 _internal_sleep (NM_MANAGER (user_data), TRUE);
4057 resuming_cb (NMSleepMonitor *monitor, gpointer user_data)
4059 nm_log_dbg (LOGD_SUSPEND, "Received resuming signal");
4060 _internal_sleep (NM_MANAGER (user_data), FALSE);
4064 _internal_enable (NMManager *self, gboolean enable)
4066 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4069 /* Update "NetworkingEnabled" key in state file */
4070 if (priv->state_file) {
4071 if (!write_value_to_state_file (priv->state_file,
4072 "main", "NetworkingEnabled",
4073 G_TYPE_BOOLEAN, (gpointer) &enable,
4075 /* Not a hard error */
4076 _LOGW (LOGD_SUSPEND, "writing to state file %s failed: %s",
4082 _LOGI (LOGD_SUSPEND, "%s requested (sleeping: %s enabled: %s)",
4083 enable ? "enable" : "disable",
4084 priv->sleeping ? "yes" : "no",
4085 priv->net_enabled ? "yes" : "no");
4087 priv->net_enabled = enable;
4089 do_sleep_wake (self, FALSE);
4091 g_object_notify (G_OBJECT (self), NM_MANAGER_NETWORKING_ENABLED);
4095 enable_net_done_cb (NMAuthChain *chain,
4097 GDBusMethodInvocation *context,
4100 NMManager *self = NM_MANAGER (user_data);
4101 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4102 GError *ret_error = NULL;
4103 NMAuthCallResult result;
4105 NMAuthSubject *subject;
4109 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
4110 enable = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, "enable"));
4111 subject = nm_auth_chain_get_subject (chain);
4113 result = nm_auth_chain_get_result (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK);
4115 _LOGD (LOGD_CORE, "Enable request failed: %s", error->message);
4116 ret_error = g_error_new (NM_MANAGER_ERROR,
4117 NM_MANAGER_ERROR_PERMISSION_DENIED,
4118 "Enable request failed: %s",
4120 } else if (result != NM_AUTH_CALL_RESULT_YES) {
4121 ret_error = g_error_new_literal (NM_MANAGER_ERROR,
4122 NM_MANAGER_ERROR_PERMISSION_DENIED,
4123 "Not authorized to enable/disable networking");
4126 _internal_enable (self, enable);
4127 g_dbus_method_invocation_return_value (context, NULL);
4128 nm_audit_log_control_op (NM_AUDIT_OP_NET_CONTROL, enable ? "on" : "off", TRUE,
4133 nm_audit_log_control_op (NM_AUDIT_OP_NET_CONTROL, enable ? "on" : "off", FALSE,
4134 subject, ret_error->message);
4135 g_dbus_method_invocation_take_error (context, ret_error);
4138 nm_auth_chain_unref (chain);
4142 impl_manager_enable (NMManager *self,
4143 GDBusMethodInvocation *context,
4146 NMManagerPrivate *priv;
4148 GError *error = NULL;
4150 g_return_if_fail (NM_IS_MANAGER (self));
4152 priv = NM_MANAGER_GET_PRIVATE (self);
4154 if (priv->net_enabled == enable) {
4155 error = g_error_new (NM_MANAGER_ERROR,
4156 NM_MANAGER_ERROR_ALREADY_ENABLED_OR_DISABLED,
4157 "Already %s", enable ? "enabled" : "disabled");
4161 chain = nm_auth_chain_new_context (context, enable_net_done_cb, self);
4163 error = g_error_new_literal (NM_MANAGER_ERROR,
4164 NM_MANAGER_ERROR_PERMISSION_DENIED,
4165 "Unable to authenticate request.");
4169 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
4170 nm_auth_chain_set_data (chain, "enable", GUINT_TO_POINTER (enable), NULL);
4171 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK, TRUE);
4175 g_dbus_method_invocation_take_error (context, error);
4181 get_perm_add_result (NMAuthChain *chain, GVariantBuilder *results, const char *permission)
4183 NMAuthCallResult result;
4185 result = nm_auth_chain_get_result (chain, permission);
4186 if (result == NM_AUTH_CALL_RESULT_YES)
4187 g_variant_builder_add (results, "{ss}", permission, "yes");
4188 else if (result == NM_AUTH_CALL_RESULT_NO)
4189 g_variant_builder_add (results, "{ss}", permission, "no");
4190 else if (result == NM_AUTH_CALL_RESULT_AUTH)
4191 g_variant_builder_add (results, "{ss}", permission, "auth");
4193 nm_log_dbg (LOGD_CORE, "unknown auth chain result %d", result);
4198 get_permissions_done_cb (NMAuthChain *chain,
4200 GDBusMethodInvocation *context,
4203 NMManager *self = NM_MANAGER (user_data);
4204 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4206 GVariantBuilder results;
4210 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
4212 _LOGD (LOGD_CORE, "Permissions request failed: %s", error->message);
4213 ret_error = g_error_new (NM_MANAGER_ERROR,
4214 NM_MANAGER_ERROR_PERMISSION_DENIED,
4215 "Permissions request failed: %s",
4217 g_dbus_method_invocation_take_error (context, ret_error);
4219 g_variant_builder_init (&results, G_VARIANT_TYPE ("a{ss}"));
4221 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK);
4222 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_SLEEP_WAKE);
4223 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI);
4224 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN);
4225 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX);
4226 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_NETWORK_CONTROL);
4227 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED);
4228 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_WIFI_SHARE_OPEN);
4229 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM);
4230 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN);
4231 get_perm_add_result (chain, &results, NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME);
4233 g_dbus_method_invocation_return_value (context,
4234 g_variant_new ("(a{ss})", &results));
4237 nm_auth_chain_unref (chain);
4241 impl_manager_get_permissions (NMManager *self,
4242 GDBusMethodInvocation *context)
4244 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4246 GError *error = NULL;
4248 chain = nm_auth_chain_new_context (context, get_permissions_done_cb, self);
4250 error = g_error_new_literal (NM_MANAGER_ERROR,
4251 NM_MANAGER_ERROR_PERMISSION_DENIED,
4252 "Unable to authenticate request.");
4253 g_dbus_method_invocation_take_error (context, error);
4257 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
4258 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK, FALSE);
4259 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SLEEP_WAKE, FALSE);
4260 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI, FALSE);
4261 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN, FALSE);
4262 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX, FALSE);
4263 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_NETWORK_CONTROL, FALSE);
4264 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED, FALSE);
4265 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_WIFI_SHARE_OPEN, FALSE);
4266 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM, FALSE);
4267 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN, FALSE);
4268 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME, FALSE);
4272 impl_manager_get_state (NMManager *self,
4273 GDBusMethodInvocation *context)
4275 nm_manager_update_state (self);
4276 g_dbus_method_invocation_return_value (context,
4277 g_variant_new ("(u)", NM_MANAGER_GET_PRIVATE (self)->state));
4281 impl_manager_set_logging (NMManager *self,
4282 GDBusMethodInvocation *context,
4284 const char *domains)
4286 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4287 GError *error = NULL;
4288 gulong caller_uid = G_MAXULONG;
4290 if (!nm_bus_manager_get_caller_info (priv->dbus_mgr, context, NULL, &caller_uid, NULL)) {
4291 error = g_error_new_literal (NM_MANAGER_ERROR,
4292 NM_MANAGER_ERROR_PERMISSION_DENIED,
4293 "Failed to get request UID.");
4297 if (0 != caller_uid) {
4298 error = g_error_new_literal (NM_MANAGER_ERROR,
4299 NM_MANAGER_ERROR_PERMISSION_DENIED,
4300 "Permission denied");
4304 if (nm_logging_setup (level, domains, NULL, &error)) {
4305 _LOGI (LOGD_CORE, "logging: level '%s' domains '%s'",
4306 nm_logging_level_to_string (), nm_logging_domains_to_string ());
4311 g_dbus_method_invocation_take_error (context, error);
4313 g_dbus_method_invocation_return_value (context, NULL);
4317 impl_manager_get_logging (NMManager *manager,
4318 GDBusMethodInvocation *context)
4320 g_dbus_method_invocation_return_value (context,
4321 g_variant_new ("(ss)",
4322 nm_logging_level_to_string (),
4323 nm_logging_domains_to_string ()));
4327 connectivity_check_done (GObject *object,
4328 GAsyncResult *result,
4331 GDBusMethodInvocation *context = user_data;
4332 NMConnectivityState state;
4333 GError *error = NULL;
4335 state = nm_connectivity_check_finish (NM_CONNECTIVITY (object), result, &error);
4337 g_dbus_method_invocation_take_error (context, error);
4339 g_dbus_method_invocation_return_value (context,
4340 g_variant_new ("(u)", state));
4346 check_connectivity_auth_done_cb (NMAuthChain *chain,
4348 GDBusMethodInvocation *context,
4351 NMManager *self = NM_MANAGER (user_data);
4352 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4353 GError *error = NULL;
4354 NMAuthCallResult result;
4356 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
4358 result = nm_auth_chain_get_result (chain, NM_AUTH_PERMISSION_NETWORK_CONTROL);
4361 _LOGD (LOGD_CORE, "CheckConnectivity request failed: %s", auth_error->message);
4362 error = g_error_new (NM_MANAGER_ERROR,
4363 NM_MANAGER_ERROR_PERMISSION_DENIED,
4364 "Connectivity check request failed: %s",
4365 auth_error->message);
4366 } else if (result != NM_AUTH_CALL_RESULT_YES) {
4367 error = g_error_new_literal (NM_MANAGER_ERROR,
4368 NM_MANAGER_ERROR_PERMISSION_DENIED,
4369 "Not authorized to recheck connectivity");
4372 nm_connectivity_check_async (priv->connectivity,
4373 connectivity_check_done,
4378 g_dbus_method_invocation_take_error (context, error);
4379 nm_auth_chain_unref (chain);
4383 impl_manager_check_connectivity (NMManager *self,
4384 GDBusMethodInvocation *context)
4386 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4388 GError *error = NULL;
4390 /* Validate the request */
4391 chain = nm_auth_chain_new_context (context, check_connectivity_auth_done_cb, self);
4393 error = g_error_new_literal (NM_MANAGER_ERROR,
4394 NM_MANAGER_ERROR_PERMISSION_DENIED,
4395 "Unable to authenticate request.");
4396 g_dbus_method_invocation_take_error (context, error);
4400 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
4401 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_NETWORK_CONTROL, TRUE);
4405 start_factory (NMDeviceFactory *factory, gpointer user_data)
4407 nm_device_factory_start (factory);
4411 nm_manager_start (NMManager *self, GError **error)
4413 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4414 GSList *iter, *connections;
4417 if (!nm_settings_start (priv->settings, error))
4420 g_signal_connect (NM_PLATFORM_GET,
4421 NM_PLATFORM_SIGNAL_LINK_CHANGED,
4422 G_CALLBACK (platform_link_cb),
4425 /* Set initial radio enabled/disabled state */
4426 for (i = 0; i < RFKILL_TYPE_MAX; i++) {
4427 RadioState *rstate = &priv->radio_states[i];
4433 /* recheck kernel rfkill state */
4434 update_rstate_from_rfkill (priv->rfkill_mgr, rstate);
4437 _LOGI (LOGD_RFKILL, "%s %s by radio killswitch; %s by state file",
4439 (rstate->hw_enabled && rstate->sw_enabled) ? "enabled" : "disabled",
4440 rstate->user_enabled ? "enabled" : "disabled");
4442 enabled = radio_enabled_for_rstate (rstate, TRUE);
4443 manager_update_radio_enabled (self, rstate, enabled);
4446 /* Log overall networking status - enabled/disabled */
4447 _LOGI (LOGD_CORE, "Networking is %s by state file",
4448 priv->net_enabled ? "enabled" : "disabled");
4450 system_unmanaged_devices_changed_cb (priv->settings, NULL, self);
4451 system_hostname_changed_cb (priv->settings, NULL, self);
4453 /* Start device factories */
4454 nm_device_factory_manager_load_factories (_register_device_factory, self);
4455 nm_device_factory_manager_for_each_factory (start_factory, NULL);
4457 platform_query_devices (self);
4459 /* Load VPN plugins */
4460 priv->vpn_manager = g_object_ref (nm_vpn_manager_get ());
4462 /* Connections added before the manager is started do not emit
4463 * connection-added signals thus devices have to be created manually.
4465 _LOGD (LOGD_CORE, "creating virtual devices...");
4466 connections = nm_settings_get_connections (priv->settings);
4467 for (iter = connections; iter; iter = iter->next)
4468 connection_changed (priv->settings, NM_CONNECTION (iter->data), self);
4469 g_slist_free (connections);
4471 priv->devices_inited = TRUE;
4473 check_if_startup_complete (self);
4479 nm_manager_stop (NMManager *self)
4481 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4483 /* Remove all devices */
4484 while (priv->devices)
4485 remove_device (self, NM_DEVICE (priv->devices->data), TRUE, TRUE);
4487 _active_connection_cleanup (self);
4491 handle_firmware_changed (gpointer user_data)
4493 NMManager *self = NM_MANAGER (user_data);
4494 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4497 priv->fw_changed_id = 0;
4499 /* Try to re-enable devices with missing firmware */
4500 for (iter = priv->devices; iter; iter = iter->next) {
4501 NMDevice *candidate = NM_DEVICE (iter->data);
4502 NMDeviceState state = nm_device_get_state (candidate);
4504 if ( nm_device_get_firmware_missing (candidate)
4505 && (state == NM_DEVICE_STATE_UNAVAILABLE)) {
4506 _LOGI (LOGD_CORE, "(%s): firmware may now be available",
4507 nm_device_get_iface (candidate));
4509 /* Re-set unavailable state to try bringing the device up again */
4510 nm_device_state_changed (candidate,
4511 NM_DEVICE_STATE_UNAVAILABLE,
4512 NM_DEVICE_STATE_REASON_NONE);
4520 connectivity_changed (NMConnectivity *connectivity,
4524 NMManager *self = NM_MANAGER (user_data);
4526 _LOGD (LOGD_CORE, "connectivity checking indicates %s",
4527 nm_connectivity_state_to_string (nm_connectivity_get_state (connectivity)));
4529 nm_manager_update_state (self);
4530 g_object_notify (G_OBJECT (self), NM_MANAGER_CONNECTIVITY);
4534 firmware_dir_changed (GFileMonitor *monitor,
4537 GFileMonitorEvent event_type,
4540 NMManager *self = NM_MANAGER (user_data);
4541 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4543 switch (event_type) {
4544 case G_FILE_MONITOR_EVENT_CREATED:
4545 case G_FILE_MONITOR_EVENT_CHANGED:
4546 case G_FILE_MONITOR_EVENT_MOVED:
4547 case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
4548 case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
4549 if (!priv->fw_changed_id) {
4550 priv->fw_changed_id = g_timeout_add_seconds (4, handle_firmware_changed, self);
4551 _LOGI (LOGD_CORE, "kernel firmware directory '%s' changed",
4552 KERNEL_FIRMWARE_DIR);
4561 connection_metered_changed (GObject *object,
4565 nm_manager_update_metered (NM_MANAGER (user_data));
4569 policy_default_device_changed (GObject *object, GParamSpec *pspec, gpointer user_data)
4571 NMManager *self = NM_MANAGER (user_data);
4572 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4574 NMActiveConnection *ac;
4576 /* Note: this assumes that it's not possible for the IP4 default
4577 * route to be going over the default-ip6-device. If that changes,
4578 * we need something more complicated here.
4580 best = nm_policy_get_default_ip4_device (priv->policy);
4582 best = nm_policy_get_default_ip6_device (priv->policy);
4585 ac = NM_ACTIVE_CONNECTION (nm_device_get_act_request (best));
4589 if (ac != priv->primary_connection) {
4590 if (priv->primary_connection) {
4591 g_signal_handlers_disconnect_by_func (priv->primary_connection,
4592 G_CALLBACK (connection_metered_changed),
4594 g_clear_object (&priv->primary_connection);
4597 priv->primary_connection = ac ? g_object_ref (ac) : NULL;
4599 if (priv->primary_connection) {
4600 g_signal_connect (priv->primary_connection, NM_ACTIVE_CONNECTION_DEVICE_METERED_CHANGED,
4601 G_CALLBACK (connection_metered_changed), self);
4603 _LOGD (LOGD_CORE, "PrimaryConnection now %s", ac ? nm_active_connection_get_settings_connection_id (ac) : "(none)");
4604 g_object_notify (G_OBJECT (self), NM_MANAGER_PRIMARY_CONNECTION);
4605 g_object_notify (G_OBJECT (self), NM_MANAGER_PRIMARY_CONNECTION_TYPE);
4606 nm_manager_update_metered (self);
4611 policy_activating_device_changed (GObject *object, GParamSpec *pspec, gpointer user_data)
4613 NMManager *self = NM_MANAGER (user_data);
4614 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4615 NMDevice *activating, *best;
4616 NMActiveConnection *ac;
4618 /* We only look at activating-ip6-device if activating-ip4-device
4619 * AND default-ip4-device are NULL; if default-ip4-device is
4620 * non-NULL, then activating-ip6-device is irrelevant, since while
4621 * that device might become the new default-ip6-device, it can't
4622 * become primary-connection while default-ip4-device is set to
4625 activating = nm_policy_get_activating_ip4_device (priv->policy);
4626 best = nm_policy_get_default_ip4_device (priv->policy);
4627 if (!activating && !best)
4628 activating = nm_policy_get_activating_ip6_device (priv->policy);
4631 ac = NM_ACTIVE_CONNECTION (nm_device_get_act_request (activating));
4635 if (ac != priv->activating_connection) {
4636 g_clear_object (&priv->activating_connection);
4637 priv->activating_connection = ac ? g_object_ref (ac) : NULL;
4638 _LOGD (LOGD_CORE, "ActivatingConnection now %s", ac ? nm_active_connection_get_settings_connection_id (ac) : "(none)");
4639 g_object_notify (G_OBJECT (self), NM_MANAGER_ACTIVATING_CONNECTION);
4643 #define NM_PERM_DENIED_ERROR "org.freedesktop.NetworkManager.PermissionDenied"
4647 GDBusConnection *connection;
4648 GDBusMessage *message;
4649 NMAuthSubject *subject;
4650 const char *permission;
4651 const char *audit_op;
4652 char *audit_prop_value;
4653 GType interface_type;
4654 const char *glib_propname;
4655 } PropertyFilterData;
4658 free_property_filter_data (PropertyFilterData *pfd)
4660 g_object_unref (pfd->self);
4661 g_object_unref (pfd->connection);
4662 g_object_unref (pfd->message);
4663 g_clear_object (&pfd->subject);
4664 g_free (pfd->audit_prop_value);
4665 g_slice_free (PropertyFilterData, pfd);
4669 prop_set_auth_done_cb (NMAuthChain *chain,
4671 GDBusMethodInvocation *context, /* NULL */
4674 PropertyFilterData *pfd = user_data;
4675 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (pfd->self);
4676 NMAuthCallResult result;
4677 GDBusMessage *reply = NULL;
4678 const char *error_message;
4679 gs_unref_object NMExportedObject *object = NULL;
4680 const NMGlobalDnsConfig *global_dns;
4681 gs_unref_variant GVariant *value = NULL;
4684 priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
4685 result = nm_auth_chain_get_result (chain, pfd->permission);
4686 if (error || (result != NM_AUTH_CALL_RESULT_YES)) {
4687 reply = g_dbus_message_new_method_error (pfd->message,
4688 NM_PERM_DENIED_ERROR,
4689 (error_message = "Not authorized to perform this operation"));
4691 error_message = error->message;
4695 object = NM_EXPORTED_OBJECT (nm_bus_manager_get_registered_object (priv->dbus_mgr,
4696 g_dbus_message_get_path (pfd->message)));
4698 reply = g_dbus_message_new_method_error (pfd->message,
4699 "org.freedesktop.DBus.Error.UnknownObject",
4700 (error_message = "Object doesn't exist."));
4704 /* do some extra type checking... */
4705 if (!nm_exported_object_get_interface_by_type (object, pfd->interface_type)) {
4706 reply = g_dbus_message_new_method_error (pfd->message,
4707 "org.freedesktop.DBus.Error.InvalidArgs",
4708 (error_message = "Object is of unexpected type."));
4712 args = g_dbus_message_get_body (pfd->message);
4713 g_variant_get (args, "(&s&sv)", NULL, NULL, &value);
4714 g_assert (pfd->glib_propname);
4716 if (!strcmp (pfd->glib_propname, NM_MANAGER_GLOBAL_DNS_CONFIGURATION)) {
4717 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("a{sv}")));
4718 global_dns = nm_config_data_get_global_dns_config (nm_config_get_data (priv->config));
4720 if (global_dns && !nm_global_dns_config_is_internal (global_dns)) {
4721 reply = g_dbus_message_new_method_error (pfd->message,
4722 NM_PERM_DENIED_ERROR,
4723 (error_message = "Global DNS configuration already set via configuration file"));
4726 /* ... but set the property on the @object itself. It would be correct to set the property
4727 * on the skeleton interface, but as it is now, the result is the same. */
4728 g_object_set (object, pfd->glib_propname, value, NULL);
4730 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN));
4732 g_object_set (object, pfd->glib_propname, g_variant_get_boolean (value), NULL);
4735 reply = g_dbus_message_new_method_reply (pfd->message);
4736 g_dbus_message_set_body (reply, g_variant_new_tuple (NULL, 0));
4737 error_message = NULL;
4739 nm_audit_log_control_op (pfd->audit_op, pfd->audit_prop_value, !error_message, pfd->subject, error_message);
4741 g_dbus_connection_send_message (pfd->connection, reply,
4742 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
4744 g_object_unref (reply);
4745 nm_auth_chain_unref (chain);
4747 free_property_filter_data (pfd);
4751 do_set_property_check (gpointer user_data)
4753 PropertyFilterData *pfd = user_data;
4754 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (pfd->self);
4755 GDBusMessage *reply = NULL;
4757 const char *error_message = NULL;
4759 pfd->subject = nm_auth_subject_new_unix_process_from_message (pfd->connection, pfd->message);
4760 if (!pfd->subject) {
4761 reply = g_dbus_message_new_method_error (pfd->message,
4762 NM_PERM_DENIED_ERROR,
4763 (error_message = "Could not determine request UID."));
4767 /* Validate the user request */
4768 chain = nm_auth_chain_new_subject (pfd->subject, NULL, prop_set_auth_done_cb, pfd);
4770 reply = g_dbus_message_new_method_error (pfd->message,
4771 NM_PERM_DENIED_ERROR,
4772 (error_message = "Could not authenticate request."));
4776 priv->auth_chains = g_slist_append (priv->auth_chains, chain);
4777 nm_auth_chain_add_call (chain, pfd->permission, TRUE);
4781 nm_audit_log_control_op (pfd->audit_op, pfd->audit_prop_value, FALSE, pfd->subject, error_message);
4782 g_dbus_connection_send_message (pfd->connection, reply,
4783 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
4785 g_object_unref (reply);
4786 free_property_filter_data (pfd);
4792 static GDBusMessage *
4793 prop_filter (GDBusConnection *connection,
4794 GDBusMessage *message,
4798 gs_unref_object NMManager *self = NULL;
4800 const char *propiface = NULL;
4801 const char *propname = NULL;
4802 const char *glib_propname = NULL, *permission = NULL;
4803 const char *audit_op = NULL;
4804 GType interface_type = G_TYPE_INVALID;
4805 PropertyFilterData *pfd;
4806 const GVariantType *expected_type = G_VARIANT_TYPE_BOOLEAN;
4807 gs_unref_variant GVariant *value = NULL;
4809 self = g_weak_ref_get (user_data);
4813 /* The sole purpose of this function is to validate property accesses on the
4814 * NMManager object since gdbus doesn't give us this functionality.
4817 /* Only filter org.freedesktop.DBus.Properties.Set calls */
4819 || g_dbus_message_get_message_type (message) != G_DBUS_MESSAGE_TYPE_METHOD_CALL
4820 || g_strcmp0 (g_dbus_message_get_interface (message), DBUS_INTERFACE_PROPERTIES) != 0
4821 || g_strcmp0 (g_dbus_message_get_member (message), "Set") != 0)
4824 args = g_dbus_message_get_body (message);
4825 if (!g_variant_is_of_type (args, G_VARIANT_TYPE ("(ssv)")))
4827 g_variant_get (args, "(&s&sv)", &propiface, &propname, &value);
4829 /* Only filter calls to filtered properties, on existing objects */
4830 if (!strcmp (propiface, NM_DBUS_INTERFACE)) {
4831 if (!strcmp (propname, "WirelessEnabled")) {
4832 glib_propname = NM_MANAGER_WIRELESS_ENABLED;
4833 permission = NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI;
4834 audit_op = NM_AUDIT_OP_RADIO_CONTROL;
4835 } else if (!strcmp (propname, "WwanEnabled")) {
4836 glib_propname = NM_MANAGER_WWAN_ENABLED;
4837 permission = NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN;
4838 audit_op = NM_AUDIT_OP_RADIO_CONTROL;
4839 } else if (!strcmp (propname, "WimaxEnabled")) {
4840 glib_propname = NM_MANAGER_WIMAX_ENABLED;
4841 permission = NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX;
4842 audit_op = NM_AUDIT_OP_RADIO_CONTROL;
4843 } else if (!strcmp (propname, "GlobalDnsConfiguration")) {
4844 glib_propname = NM_MANAGER_GLOBAL_DNS_CONFIGURATION;
4845 permission = NM_AUTH_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS;
4846 audit_op = NM_AUDIT_OP_NET_CONTROL;
4847 expected_type = G_VARIANT_TYPE ("a{sv}");
4850 interface_type = NMDBUS_TYPE_MANAGER_SKELETON;
4851 } else if (!strcmp (propiface, NM_DBUS_INTERFACE_DEVICE)) {
4852 if (!strcmp (propname, "Autoconnect")) {
4853 glib_propname = NM_DEVICE_AUTOCONNECT;
4854 permission = NM_AUTH_PERMISSION_NETWORK_CONTROL;
4855 audit_op = NM_AUDIT_OP_DEVICE_AUTOCONNECT;
4856 } else if (!strcmp (propname, "Managed")) {
4857 glib_propname = NM_DEVICE_MANAGED;
4858 permission = NM_AUTH_PERMISSION_NETWORK_CONTROL;
4859 audit_op = NM_AUDIT_OP_DEVICE_MANAGED;
4862 interface_type = NMDBUS_TYPE_DEVICE_SKELETON;
4866 if (!g_variant_is_of_type (value, expected_type))
4869 /* This filter function is called from a gdbus worker thread which we can't
4870 * make other D-Bus calls from. In particular, we cannot call
4871 * org.freedesktop.DBus.GetConnectionUnixUser to find the remote UID.
4873 pfd = g_slice_new0 (PropertyFilterData);
4876 pfd->connection = g_object_ref (connection);
4877 pfd->message = message;
4878 pfd->permission = permission;
4879 pfd->interface_type = interface_type;
4880 pfd->glib_propname = glib_propname;
4881 pfd->audit_op = audit_op;
4882 if (g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN)) {
4883 pfd->audit_prop_value = g_strdup_printf ("%s:%d", pfd->glib_propname,
4884 g_variant_get_boolean (value));
4886 pfd->audit_prop_value = g_strdup (pfd->glib_propname);
4888 g_idle_add (do_set_property_check, pfd);
4893 /******************************************************************************/
4896 _set_prop_filter_free2 (gpointer user_data)
4898 g_slice_free (GWeakRef, user_data);
4899 return G_SOURCE_REMOVE;
4903 _set_prop_filter_free (gpointer user_data)
4905 g_weak_ref_clear (user_data);
4907 /* Delay the final deletion of the user_data. There is a race when
4908 * calling g_dbus_connection_remove_filter() that the callback and user_data
4909 * might have been copied and being executed after the destroy function
4910 * runs (bgo #704568).
4911 * This doesn't really fix the race, but it should work well enough. */
4912 g_timeout_add_seconds (2, _set_prop_filter_free2, user_data);
4916 _set_prop_filter (NMManager *self, GDBusConnection *connection)
4918 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
4920 nm_assert ((!priv->prop_filter.connection) == (!priv->prop_filter.id));
4922 if (priv->prop_filter.connection == connection)
4925 if (priv->prop_filter.connection) {
4926 g_dbus_connection_remove_filter (priv->prop_filter.connection, priv->prop_filter.id);
4927 priv->prop_filter.id = 0;
4928 g_clear_object (&priv->prop_filter.connection);
4933 wptr = g_slice_new (GWeakRef);
4934 g_weak_ref_init (wptr, self);
4935 priv->prop_filter.id = g_dbus_connection_add_filter (connection, prop_filter, wptr, _set_prop_filter_free);
4936 priv->prop_filter.connection = g_object_ref (connection);
4940 /******************************************************************************/
4943 authority_changed_cb (NMAuthManager *auth_manager, gpointer user_data)
4945 /* Let clients know they should re-check their authorization */
4946 g_signal_emit (NM_MANAGER (user_data), signals[CHECK_PERMISSIONS], 0);
4949 #define KERN_RFKILL_OP_CHANGE_ALL 3
4950 #define KERN_RFKILL_TYPE_WLAN 1
4951 #define KERN_RFKILL_TYPE_WWAN 5
4952 struct rfkill_event {
4957 } __attribute__((packed));
4960 rfkill_change (const char *desc, RfKillType rtype, gboolean enabled)
4963 struct rfkill_event event;
4966 g_return_if_fail (rtype == RFKILL_TYPE_WLAN || rtype == RFKILL_TYPE_WWAN);
4969 fd = open ("/dev/rfkill", O_RDWR);
4971 if (errno == EACCES)
4972 nm_log_warn (LOGD_RFKILL, "(%s): failed to open killswitch device", desc);
4976 if (fcntl (fd, F_SETFL, O_NONBLOCK) < 0) {
4977 nm_log_warn (LOGD_RFKILL, "(%s): failed to set killswitch device for "
4978 "non-blocking operation", desc);
4983 memset (&event, 0, sizeof (event));
4984 event.op = KERN_RFKILL_OP_CHANGE_ALL;
4986 case RFKILL_TYPE_WLAN:
4987 event.type = KERN_RFKILL_TYPE_WLAN;
4989 case RFKILL_TYPE_WWAN:
4990 event.type = KERN_RFKILL_TYPE_WWAN;
4993 g_assert_not_reached ();
4995 event.soft = enabled ? 0 : 1;
4997 len = write (fd, &event, sizeof (event));
4999 nm_log_warn (LOGD_RFKILL, "(%s): failed to change WiFi killswitch state: (%d) %s",
5000 desc, errno, g_strerror (errno));
5001 } else if (len == sizeof (event)) {
5002 nm_log_info (LOGD_RFKILL, "%s hardware radio set %s",
5003 desc, enabled ? "enabled" : "disabled");
5005 /* Failed to write full structure */
5006 nm_log_warn (LOGD_RFKILL, "(%s): failed to change WiFi killswitch state", desc);
5013 manager_radio_user_toggled (NMManager *self,
5017 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
5018 GError *error = NULL;
5019 gboolean old_enabled, new_enabled;
5021 /* Don't touch devices if asleep/networking disabled */
5022 if (manager_sleeping (self))
5026 _LOGD (LOGD_RFKILL, "(%s): setting radio %s by user",
5028 enabled ? "enabled" : "disabled");
5031 /* Update enabled key in state file */
5032 if (priv->state_file) {
5033 if (!write_value_to_state_file (priv->state_file,
5034 "main", rstate->key,
5035 G_TYPE_BOOLEAN, (gpointer) &enabled,
5037 _LOGW (LOGD_CORE, "writing to state file %s failed: %s",
5040 g_clear_error (&error);
5044 /* When the user toggles the radio, their request should override any
5045 * daemon (like ModemManager) enabled state that can be changed. For WWAN
5046 * for example, we want the WwanEnabled property to reflect the daemon state
5047 * too so that users can toggle the modem powered, but we don't want that
5048 * daemon state to affect whether or not the user *can* turn it on, which is
5049 * what the kernel rfkill state does. So we ignore daemon enabled state
5050 * when determining what the new state should be since it shouldn't block
5051 * the user's request.
5053 old_enabled = radio_enabled_for_rstate (rstate, TRUE);
5054 rstate->user_enabled = enabled;
5055 new_enabled = radio_enabled_for_rstate (rstate, FALSE);
5056 if (new_enabled != old_enabled) {
5057 /* Try to change the kernel rfkill state */
5058 if (rstate->rtype == RFKILL_TYPE_WLAN || rstate->rtype == RFKILL_TYPE_WWAN)
5059 rfkill_change (rstate->desc, rstate->rtype, new_enabled);
5061 manager_update_radio_enabled (self, rstate, new_enabled);
5066 periodic_update_active_connection_timestamps (gpointer user_data)
5068 NMManager *manager = NM_MANAGER (user_data);
5069 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
5072 for (iter = priv->active_connections; iter; iter = g_slist_next (iter)) {
5073 NMActiveConnection *ac = iter->data;
5074 NMSettingsConnection *connection;
5076 if (nm_active_connection_get_state (ac) == NM_ACTIVE_CONNECTION_STATE_ACTIVATED) {
5077 connection = nm_active_connection_get_settings_connection (ac);
5078 nm_settings_connection_update_timestamp (connection, (guint64) time (NULL), FALSE);
5086 dbus_connection_changed_cb (NMBusManager *dbus_mgr,
5087 GDBusConnection *connection,
5090 _set_prop_filter (NM_MANAGER (user_data), connection);
5093 /**********************************************************************/
5095 NM_DEFINE_SINGLETON_REGISTER (NMManager);
5098 nm_manager_get (void)
5100 g_return_val_if_fail (singleton_instance, NULL);
5101 return singleton_instance;
5104 NMConnectionProvider *
5105 nm_connection_provider_get (void)
5107 NMConnectionProvider *p;
5109 g_return_val_if_fail (singleton_instance, NULL);
5111 p = NM_CONNECTION_PROVIDER (NM_MANAGER_GET_PRIVATE (singleton_instance)->settings);
5112 g_return_val_if_fail (p, NULL);
5117 nm_manager_setup (const char *state_file,
5118 gboolean initial_net_enabled,
5119 gboolean initial_wifi_enabled,
5120 gboolean initial_wwan_enabled)
5124 g_return_val_if_fail (!singleton_instance, singleton_instance);
5126 self = g_object_new (NM_TYPE_MANAGER,
5127 NM_MANAGER_NETWORKING_ENABLED, initial_net_enabled,
5128 NM_MANAGER_WIRELESS_ENABLED, initial_wifi_enabled,
5129 NM_MANAGER_WWAN_ENABLED, initial_wwan_enabled,
5130 NM_MANAGER_STATE_FILE, state_file,
5132 nm_assert (NM_IS_MANAGER (self));
5133 singleton_instance = self;
5135 nm_singleton_instance_register ();
5136 _LOGD (LOGD_CORE, "setup %s singleton (%p)", "NMManager", singleton_instance);
5138 nm_exported_object_export ((NMExportedObject *) self);
5144 constructed (GObject *object)
5146 NMManager *self = NM_MANAGER (object);
5147 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
5148 NMConfigData *config_data;
5150 G_OBJECT_CLASS (nm_manager_parent_class)->constructed (object);
5152 _set_prop_filter (self, nm_bus_manager_get_connection (priv->dbus_mgr));
5154 priv->settings = nm_settings_new ();
5155 g_signal_connect (priv->settings, "notify::" NM_SETTINGS_STARTUP_COMPLETE,
5156 G_CALLBACK (settings_startup_complete_changed), self);
5157 g_signal_connect (priv->settings, "notify::" NM_SETTINGS_UNMANAGED_SPECS,
5158 G_CALLBACK (system_unmanaged_devices_changed_cb), self);
5159 g_signal_connect (priv->settings, "notify::" NM_SETTINGS_HOSTNAME,
5160 G_CALLBACK (system_hostname_changed_cb), self);
5161 g_signal_connect (priv->settings, NM_SETTINGS_SIGNAL_CONNECTION_ADDED,
5162 G_CALLBACK (connection_changed), self);
5163 g_signal_connect (priv->settings, NM_SETTINGS_SIGNAL_CONNECTION_UPDATED_BY_USER,
5164 G_CALLBACK (connection_changed), self);
5165 g_signal_connect (priv->settings, NM_SETTINGS_SIGNAL_CONNECTION_REMOVED,
5166 G_CALLBACK (connection_removed), self);
5168 priv->policy = nm_policy_new (self, priv->settings);
5169 g_signal_connect (priv->policy, "notify::" NM_POLICY_DEFAULT_IP4_DEVICE,
5170 G_CALLBACK (policy_default_device_changed), self);
5171 g_signal_connect (priv->policy, "notify::" NM_POLICY_DEFAULT_IP6_DEVICE,
5172 G_CALLBACK (policy_default_device_changed), self);
5173 g_signal_connect (priv->policy, "notify::" NM_POLICY_ACTIVATING_IP4_DEVICE,
5174 G_CALLBACK (policy_activating_device_changed), self);
5175 g_signal_connect (priv->policy, "notify::" NM_POLICY_ACTIVATING_IP6_DEVICE,
5176 G_CALLBACK (policy_activating_device_changed), self);
5178 priv->config = g_object_ref (nm_config_get ());
5179 g_signal_connect (G_OBJECT (priv->config),
5180 NM_CONFIG_SIGNAL_CONFIG_CHANGED,
5181 G_CALLBACK (_config_changed_cb),
5184 config_data = nm_config_get_data (priv->config);
5185 priv->connectivity = nm_connectivity_new (nm_config_data_get_connectivity_uri (config_data),
5186 nm_config_data_get_connectivity_interval (config_data),
5187 nm_config_data_get_connectivity_response (config_data));
5188 g_signal_connect (priv->connectivity, "notify::" NM_CONNECTIVITY_STATE,
5189 G_CALLBACK (connectivity_changed), self);
5191 priv->rfkill_mgr = nm_rfkill_manager_new ();
5192 g_signal_connect (priv->rfkill_mgr,
5194 G_CALLBACK (rfkill_manager_rfkill_changed_cb),
5197 /* Force kernel WiFi/WWAN rfkill state to follow NM saved WiFi/WWAN state
5198 * in case the BIOS doesn't save rfkill state, and to be consistent with user
5199 * changes to the WirelessEnabled/WWANEnabled properties which toggle kernel
5202 rfkill_change (priv->radio_states[RFKILL_TYPE_WLAN].desc, RFKILL_TYPE_WLAN, priv->radio_states[RFKILL_TYPE_WLAN].user_enabled);
5203 rfkill_change (priv->radio_states[RFKILL_TYPE_WWAN].desc, RFKILL_TYPE_WWAN, priv->radio_states[RFKILL_TYPE_WWAN].user_enabled);
5207 nm_manager_init (NMManager *self)
5209 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
5213 /* Initialize rfkill structures and states */
5214 memset (priv->radio_states, 0, sizeof (priv->radio_states));
5216 priv->radio_states[RFKILL_TYPE_WLAN].user_enabled = TRUE;
5217 priv->radio_states[RFKILL_TYPE_WLAN].key = "WirelessEnabled";
5218 priv->radio_states[RFKILL_TYPE_WLAN].prop = NM_MANAGER_WIRELESS_ENABLED;
5219 priv->radio_states[RFKILL_TYPE_WLAN].hw_prop = NM_MANAGER_WIRELESS_HARDWARE_ENABLED;
5220 priv->radio_states[RFKILL_TYPE_WLAN].desc = "WiFi";
5221 priv->radio_states[RFKILL_TYPE_WLAN].rtype = RFKILL_TYPE_WLAN;
5223 priv->radio_states[RFKILL_TYPE_WWAN].user_enabled = TRUE;
5224 priv->radio_states[RFKILL_TYPE_WWAN].key = "WWANEnabled";
5225 priv->radio_states[RFKILL_TYPE_WWAN].prop = NM_MANAGER_WWAN_ENABLED;
5226 priv->radio_states[RFKILL_TYPE_WWAN].hw_prop = NM_MANAGER_WWAN_HARDWARE_ENABLED;
5227 priv->radio_states[RFKILL_TYPE_WWAN].desc = "WWAN";
5228 priv->radio_states[RFKILL_TYPE_WWAN].rtype = RFKILL_TYPE_WWAN;
5230 for (i = 0; i < RFKILL_TYPE_MAX; i++)
5231 priv->radio_states[i].hw_enabled = TRUE;
5233 priv->sleeping = FALSE;
5234 priv->state = NM_STATE_DISCONNECTED;
5235 priv->startup = TRUE;
5237 priv->dbus_mgr = g_object_ref (nm_bus_manager_get ());
5238 g_signal_connect (priv->dbus_mgr,
5239 NM_BUS_MANAGER_DBUS_CONNECTION_CHANGED,
5240 G_CALLBACK (dbus_connection_changed_cb),
5243 /* sleep/wake handling */
5244 priv->sleep_monitor = g_object_ref (nm_sleep_monitor_get ());
5245 g_signal_connect (priv->sleep_monitor, NM_SLEEP_MONITOR_SLEEPING,
5246 G_CALLBACK (sleeping_cb), self);
5247 g_signal_connect (priv->sleep_monitor, NM_SLEEP_MONITOR_RESUMING,
5248 G_CALLBACK (resuming_cb), self);
5250 /* Listen for authorization changes */
5251 g_signal_connect (nm_auth_manager_get (),
5252 NM_AUTH_MANAGER_SIGNAL_CHANGED,
5253 G_CALLBACK (authority_changed_cb),
5257 /* Monitor the firmware directory */
5258 if (strlen (KERNEL_FIRMWARE_DIR)) {
5259 file = g_file_new_for_path (KERNEL_FIRMWARE_DIR "/");
5260 priv->fw_monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE, NULL, NULL);
5261 g_object_unref (file);
5264 if (priv->fw_monitor) {
5265 g_signal_connect (priv->fw_monitor, "changed",
5266 G_CALLBACK (firmware_dir_changed),
5268 _LOGI (LOGD_CORE, "monitoring kernel firmware directory '%s'.",
5269 KERNEL_FIRMWARE_DIR);
5271 _LOGW (LOGD_CORE, "failed to monitor kernel firmware directory '%s'.",
5272 KERNEL_FIRMWARE_DIR);
5275 /* Update timestamps in active connections */
5276 priv->timestamp_update_id = g_timeout_add_seconds (300, (GSourceFunc) periodic_update_active_connection_timestamps, self);
5278 priv->metered = NM_METERED_UNKNOWN;
5282 device_is_real (GObject *device, gpointer user_data)
5284 return nm_device_is_real (NM_DEVICE (device));
5288 get_property (GObject *object, guint prop_id,
5289 GValue *value, GParamSpec *pspec)
5291 NMManager *self = NM_MANAGER (object);
5292 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
5293 NMConfigData *config_data;
5294 const NMGlobalDnsConfig *dns_config;
5299 g_value_set_string (value, VERSION);
5302 nm_manager_update_state (self);
5303 g_value_set_uint (value, priv->state);
5306 g_value_set_boolean (value, priv->startup);
5308 case PROP_NETWORKING_ENABLED:
5309 g_value_set_boolean (value, priv->net_enabled);
5311 case PROP_WIRELESS_ENABLED:
5312 g_value_set_boolean (value, radio_enabled_for_type (self, RFKILL_TYPE_WLAN, TRUE));
5314 case PROP_WIRELESS_HARDWARE_ENABLED:
5315 g_value_set_boolean (value, priv->radio_states[RFKILL_TYPE_WLAN].hw_enabled);
5317 case PROP_WWAN_ENABLED:
5318 g_value_set_boolean (value, radio_enabled_for_type (self, RFKILL_TYPE_WWAN, TRUE));
5320 case PROP_WWAN_HARDWARE_ENABLED:
5321 g_value_set_boolean (value, priv->radio_states[RFKILL_TYPE_WWAN].hw_enabled);
5323 case PROP_WIMAX_ENABLED:
5324 g_value_set_boolean (value, FALSE);
5326 case PROP_WIMAX_HARDWARE_ENABLED:
5327 g_value_set_boolean (value, FALSE);
5329 case PROP_ACTIVE_CONNECTIONS:
5330 nm_utils_g_value_set_object_path_array (value, priv->active_connections, NULL, NULL);
5332 case PROP_CONNECTIVITY:
5333 g_value_set_uint (value, nm_connectivity_get_state (priv->connectivity));
5335 case PROP_PRIMARY_CONNECTION:
5336 nm_utils_g_value_set_object_path (value, priv->primary_connection);
5338 case PROP_PRIMARY_CONNECTION_TYPE:
5340 if (priv->primary_connection) {
5343 con = nm_active_connection_get_applied_connection (priv->primary_connection);
5345 type = nm_connection_get_connection_type (con);
5347 g_value_set_string (value, type ? type : "");
5349 case PROP_ACTIVATING_CONNECTION:
5350 nm_utils_g_value_set_object_path (value, priv->activating_connection);
5353 g_value_set_string (value, priv->hostname);
5356 g_value_set_boolean (value, priv->sleeping);
5359 nm_utils_g_value_set_object_path_array (value, priv->devices, device_is_real, NULL);
5362 g_value_set_uint (value, priv->metered);
5364 case PROP_GLOBAL_DNS_CONFIGURATION:
5365 config_data = nm_config_get_data (priv->config);
5366 dns_config = nm_config_data_get_global_dns_config (config_data);
5367 nm_global_dns_config_to_dbus (dns_config, value);
5369 case PROP_ALL_DEVICES:
5370 nm_utils_g_value_set_object_path_array (value, priv->devices, NULL, NULL);
5373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5379 set_property (GObject *object, guint prop_id,
5380 const GValue *value, GParamSpec *pspec)
5382 NMManager *self = NM_MANAGER (object);
5383 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
5384 NMGlobalDnsConfig *dns_config;
5385 GError *error = NULL;
5388 case PROP_STATE_FILE:
5389 /* construct-only */
5390 priv->state_file = g_value_dup_string (value);
5392 case PROP_NETWORKING_ENABLED:
5393 /* construct-only */
5394 priv->net_enabled = g_value_get_boolean (value);
5396 case PROP_WIRELESS_ENABLED:
5397 if (!priv->rfkill_mgr) {
5398 /* called during object construction. */
5399 priv->radio_states[RFKILL_TYPE_WLAN].user_enabled = g_value_get_boolean (value);
5401 manager_radio_user_toggled (NM_MANAGER (object),
5402 &priv->radio_states[RFKILL_TYPE_WLAN],
5403 g_value_get_boolean (value));
5406 case PROP_WWAN_ENABLED:
5407 if (!priv->rfkill_mgr) {
5408 /* called during object construction. */
5409 priv->radio_states[RFKILL_TYPE_WWAN].user_enabled = g_value_get_boolean (value);
5411 manager_radio_user_toggled (NM_MANAGER (object),
5412 &priv->radio_states[RFKILL_TYPE_WWAN],
5413 g_value_get_boolean (value));
5416 case PROP_WIMAX_ENABLED:
5417 /* WIMAX is depreacted. This does nothing. */
5419 case PROP_GLOBAL_DNS_CONFIGURATION:
5420 dns_config = nm_global_dns_config_from_dbus (value, &error);
5422 nm_config_set_global_dns (priv->config, dns_config, &error);
5424 nm_global_dns_config_free (dns_config);
5427 _LOGD (LOGD_CORE, "set global DNS failed with error: %s", error->message);
5428 g_error_free (error);
5432 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5438 _deinit_device_factory (NMDeviceFactory *factory, gpointer user_data)
5440 g_signal_handlers_disconnect_matched (factory, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, NM_MANAGER (user_data));
5444 dispose (GObject *object)
5446 NMManager *manager = NM_MANAGER (object);
5447 NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
5449 g_slist_free_full (priv->auth_chains, (GDestroyNotify) nm_auth_chain_unref);
5450 priv->auth_chains = NULL;
5452 g_signal_handlers_disconnect_by_func (nm_auth_manager_get (),
5453 G_CALLBACK (authority_changed_cb),
5456 g_assert (priv->devices == NULL);
5458 nm_clear_g_source (&priv->ac_cleanup_id);
5460 while (priv->active_connections)
5461 active_connection_remove (manager, NM_ACTIVE_CONNECTION (priv->active_connections->data));
5462 g_clear_pointer (&priv->active_connections, g_slist_free);
5463 g_clear_object (&priv->primary_connection);
5464 g_clear_object (&priv->activating_connection);
5467 g_signal_handlers_disconnect_by_func (priv->config, _config_changed_cb, manager);
5468 g_clear_object (&priv->config);
5470 if (priv->connectivity) {
5471 g_signal_handlers_disconnect_by_func (priv->connectivity, connectivity_changed, manager);
5472 g_clear_object (&priv->connectivity);
5475 g_free (priv->hostname);
5478 g_signal_handlers_disconnect_by_func (priv->policy, policy_default_device_changed, manager);
5479 g_signal_handlers_disconnect_by_func (priv->policy, policy_activating_device_changed, manager);
5480 g_clear_object (&priv->policy);
5483 if (priv->settings) {
5484 g_signal_handlers_disconnect_by_func (priv->settings, settings_startup_complete_changed, manager);
5485 g_signal_handlers_disconnect_by_func (priv->settings, system_unmanaged_devices_changed_cb, manager);
5486 g_signal_handlers_disconnect_by_func (priv->settings, system_hostname_changed_cb, manager);
5487 g_signal_handlers_disconnect_by_func (priv->settings, connection_changed, manager);
5488 g_signal_handlers_disconnect_by_func (priv->settings, connection_removed, manager);
5489 g_clear_object (&priv->settings);
5492 g_clear_pointer (&priv->state_file, g_free);
5493 g_clear_object (&priv->vpn_manager);
5495 /* Unregister property filter */
5496 if (priv->dbus_mgr) {
5497 g_signal_handlers_disconnect_by_func (priv->dbus_mgr, dbus_connection_changed_cb, manager);
5498 g_clear_object (&priv->dbus_mgr);
5500 _set_prop_filter (manager, NULL);
5502 if (priv->sleep_monitor) {
5503 g_signal_handlers_disconnect_by_func (priv->sleep_monitor, sleeping_cb, manager);
5504 g_signal_handlers_disconnect_by_func (priv->sleep_monitor, resuming_cb, manager);
5505 g_clear_object (&priv->sleep_monitor);
5508 if (priv->fw_monitor) {
5509 g_signal_handlers_disconnect_by_func (priv->fw_monitor, firmware_dir_changed, manager);
5511 nm_clear_g_source (&priv->fw_changed_id);
5513 g_file_monitor_cancel (priv->fw_monitor);
5514 g_clear_object (&priv->fw_monitor);
5517 if (priv->rfkill_mgr) {
5518 g_signal_handlers_disconnect_by_func (priv->rfkill_mgr, rfkill_manager_rfkill_changed_cb, manager);
5519 g_clear_object (&priv->rfkill_mgr);
5522 nm_device_factory_manager_for_each_factory (_deinit_device_factory, manager);
5524 nm_clear_g_source (&priv->timestamp_update_id);
5526 G_OBJECT_CLASS (nm_manager_parent_class)->dispose (object);
5530 nm_manager_class_init (NMManagerClass *manager_class)
5532 GObjectClass *object_class = G_OBJECT_CLASS (manager_class);
5533 NMExportedObjectClass *exported_object_class = NM_EXPORTED_OBJECT_CLASS (manager_class);
5535 g_type_class_add_private (manager_class, sizeof (NMManagerPrivate));
5537 exported_object_class->export_path = NM_DBUS_PATH;
5539 /* virtual methods */
5540 object_class->constructed = constructed;
5541 object_class->set_property = set_property;
5542 object_class->get_property = get_property;
5543 object_class->dispose = dispose;
5546 g_object_class_install_property
5547 (object_class, PROP_VERSION,
5548 g_param_spec_string (NM_MANAGER_VERSION, "", "",
5551 G_PARAM_STATIC_STRINGS));
5553 g_object_class_install_property (object_class,
5555 g_param_spec_string (NM_MANAGER_STATE_FILE, "", "",
5558 G_PARAM_CONSTRUCT_ONLY |
5559 G_PARAM_STATIC_STRINGS));
5561 g_object_class_install_property
5562 (object_class, PROP_STATE,
5563 g_param_spec_uint (NM_MANAGER_STATE, "", "",
5564 0, NM_STATE_DISCONNECTED, 0,
5566 G_PARAM_STATIC_STRINGS));
5568 g_object_class_install_property
5569 (object_class, PROP_STARTUP,
5570 g_param_spec_boolean (NM_MANAGER_STARTUP, "", "",
5573 G_PARAM_STATIC_STRINGS));
5575 g_object_class_install_property
5576 (object_class, PROP_NETWORKING_ENABLED,
5577 g_param_spec_boolean (NM_MANAGER_NETWORKING_ENABLED, "", "",
5579 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
5580 G_PARAM_STATIC_STRINGS));
5582 g_object_class_install_property
5583 (object_class, PROP_WIRELESS_ENABLED,
5584 g_param_spec_boolean (NM_MANAGER_WIRELESS_ENABLED, "", "",
5588 G_PARAM_STATIC_STRINGS));
5590 g_object_class_install_property
5591 (object_class, PROP_WIRELESS_HARDWARE_ENABLED,
5592 g_param_spec_boolean (NM_MANAGER_WIRELESS_HARDWARE_ENABLED, "", "",
5595 G_PARAM_STATIC_STRINGS));
5597 g_object_class_install_property
5598 (object_class, PROP_WWAN_ENABLED,
5599 g_param_spec_boolean (NM_MANAGER_WWAN_ENABLED, "", "",
5603 G_PARAM_STATIC_STRINGS));
5605 g_object_class_install_property
5606 (object_class, PROP_WWAN_HARDWARE_ENABLED,
5607 g_param_spec_boolean (NM_MANAGER_WWAN_HARDWARE_ENABLED, "", "",
5610 G_PARAM_STATIC_STRINGS));
5612 g_object_class_install_property
5613 (object_class, PROP_WIMAX_ENABLED,
5614 g_param_spec_boolean (NM_MANAGER_WIMAX_ENABLED, "", "",
5617 G_PARAM_STATIC_STRINGS));
5619 g_object_class_install_property
5620 (object_class, PROP_WIMAX_HARDWARE_ENABLED,
5621 g_param_spec_boolean (NM_MANAGER_WIMAX_HARDWARE_ENABLED, "", "",
5624 G_PARAM_STATIC_STRINGS));
5626 g_object_class_install_property
5627 (object_class, PROP_ACTIVE_CONNECTIONS,
5628 g_param_spec_boxed (NM_MANAGER_ACTIVE_CONNECTIONS, "", "",
5631 G_PARAM_STATIC_STRINGS));
5633 g_object_class_install_property
5634 (object_class, PROP_CONNECTIVITY,
5635 g_param_spec_uint (NM_MANAGER_CONNECTIVITY, "", "",
5636 NM_CONNECTIVITY_UNKNOWN, NM_CONNECTIVITY_FULL, NM_CONNECTIVITY_UNKNOWN,
5638 G_PARAM_STATIC_STRINGS));
5640 g_object_class_install_property
5641 (object_class, PROP_PRIMARY_CONNECTION,
5642 g_param_spec_string (NM_MANAGER_PRIMARY_CONNECTION, "", "",
5645 G_PARAM_STATIC_STRINGS));
5647 g_object_class_install_property
5648 (object_class, PROP_PRIMARY_CONNECTION_TYPE,
5649 g_param_spec_string (NM_MANAGER_PRIMARY_CONNECTION_TYPE, "", "",
5652 G_PARAM_STATIC_STRINGS));
5655 g_object_class_install_property
5656 (object_class, PROP_ACTIVATING_CONNECTION,
5657 g_param_spec_string (NM_MANAGER_ACTIVATING_CONNECTION, "", "",
5660 G_PARAM_STATIC_STRINGS));
5662 /* Hostname is not exported over D-Bus */
5663 g_object_class_install_property
5664 (object_class, PROP_HOSTNAME,
5665 g_param_spec_string (NM_MANAGER_HOSTNAME, "", "",
5668 G_PARAM_STATIC_STRINGS));
5670 /* Sleeping is not exported over D-Bus */
5671 g_object_class_install_property
5672 (object_class, PROP_SLEEPING,
5673 g_param_spec_boolean (NM_MANAGER_SLEEPING, "", "",
5676 G_PARAM_STATIC_STRINGS));
5678 g_object_class_install_property
5679 (object_class, PROP_DEVICES,
5680 g_param_spec_boxed (NM_MANAGER_DEVICES, "", "",
5683 G_PARAM_STATIC_STRINGS));
5686 * NMManager:metered:
5688 * Whether the connectivity is metered.
5692 g_object_class_install_property
5693 (object_class, PROP_METERED,
5694 g_param_spec_uint (NM_MANAGER_METERED, "", "",
5695 0, G_MAXUINT32, NM_METERED_UNKNOWN,
5697 G_PARAM_STATIC_STRINGS));
5700 * NMManager:global-dns-configuration:
5702 * The global DNS configuration.
5706 g_object_class_install_property
5707 (object_class, PROP_GLOBAL_DNS_CONFIGURATION,
5708 g_param_spec_variant (NM_MANAGER_GLOBAL_DNS_CONFIGURATION, "", "",
5709 G_VARIANT_TYPE ("a{sv}"),
5712 G_PARAM_STATIC_STRINGS));
5715 * NMManager:all-devices:
5717 * All devices, including those that are not realized.
5721 g_object_class_install_property
5722 (object_class, PROP_ALL_DEVICES,
5723 g_param_spec_boxed (NM_MANAGER_ALL_DEVICES, "", "",
5726 G_PARAM_STATIC_STRINGS));
5730 /* D-Bus exported; emitted only for realized devices */
5731 signals[DEVICE_ADDED] =
5732 g_signal_new ("device-added",
5733 G_OBJECT_CLASS_TYPE (object_class),
5735 G_STRUCT_OFFSET (NMManagerClass, device_added),
5737 G_TYPE_NONE, 1, NM_TYPE_DEVICE);
5739 /* Emitted for both realized devices and placeholder devices */
5740 signals[INTERNAL_DEVICE_ADDED] =
5741 g_signal_new ("internal-device-added",
5742 G_OBJECT_CLASS_TYPE (object_class),
5743 G_SIGNAL_RUN_FIRST, 0,
5745 G_TYPE_NONE, 1, G_TYPE_OBJECT);
5747 /* D-Bus exported; emitted only for realized devices */
5748 signals[DEVICE_REMOVED] =
5749 g_signal_new ("device-removed",
5750 G_OBJECT_CLASS_TYPE (object_class),
5752 G_STRUCT_OFFSET (NMManagerClass, device_removed),
5754 G_TYPE_NONE, 1, NM_TYPE_DEVICE);
5756 /* Emitted for both realized devices and placeholder devices */
5757 signals[INTERNAL_DEVICE_REMOVED] =
5758 g_signal_new ("internal-device-removed",
5759 G_OBJECT_CLASS_TYPE (object_class),
5760 G_SIGNAL_RUN_FIRST, 0,
5762 G_TYPE_NONE, 1, G_TYPE_OBJECT);
5764 signals[STATE_CHANGED] =
5765 g_signal_new (NM_MANAGER_STATE_CHANGED,
5766 G_OBJECT_CLASS_TYPE (object_class),
5768 G_STRUCT_OFFSET (NMManagerClass, state_changed),
5770 G_TYPE_NONE, 1, G_TYPE_UINT);
5772 signals[CHECK_PERMISSIONS] =
5773 g_signal_new ("check-permissions",
5774 G_OBJECT_CLASS_TYPE (object_class),
5776 0, NULL, NULL, NULL,
5779 signals[USER_PERMISSIONS_CHANGED] =
5780 g_signal_new ("user-permissions-changed",
5781 G_OBJECT_CLASS_TYPE (object_class),
5783 0, NULL, NULL, NULL,
5786 signals[ACTIVE_CONNECTION_ADDED] =
5787 g_signal_new (NM_MANAGER_ACTIVE_CONNECTION_ADDED,
5788 G_OBJECT_CLASS_TYPE (object_class),
5790 0, NULL, NULL, NULL,
5791 G_TYPE_NONE, 1, NM_TYPE_ACTIVE_CONNECTION);
5793 signals[ACTIVE_CONNECTION_REMOVED] =
5794 g_signal_new (NM_MANAGER_ACTIVE_CONNECTION_REMOVED,
5795 G_OBJECT_CLASS_TYPE (object_class),
5797 0, NULL, NULL, NULL,
5798 G_TYPE_NONE, 1, NM_TYPE_ACTIVE_CONNECTION);
5800 signals[CONFIGURE_QUIT] =
5801 g_signal_new (NM_MANAGER_CONFIGURE_QUIT,
5802 G_OBJECT_CLASS_TYPE (object_class),
5804 0, NULL, NULL, NULL,
5807 nm_exported_object_class_add_interface (NM_EXPORTED_OBJECT_CLASS (manager_class),
5808 NMDBUS_TYPE_MANAGER_SKELETON,
5809 "GetDevices", impl_manager_get_devices,
5810 "GetAllDevices", impl_manager_get_all_devices,
5811 "GetDeviceByIpIface", impl_manager_get_device_by_ip_iface,
5812 "ActivateConnection", impl_manager_activate_connection,
5813 "AddAndActivateConnection", impl_manager_add_and_activate_connection,
5814 "DeactivateConnection", impl_manager_deactivate_connection,
5815 "Sleep", impl_manager_sleep,
5816 "Enable", impl_manager_enable,
5817 "GetPermissions", impl_manager_get_permissions,
5818 "SetLogging", impl_manager_set_logging,
5819 "GetLogging", impl_manager_get_logging,
5820 "CheckConnectivity", impl_manager_check_connectivity,
5821 "state", impl_manager_get_state,