diff options
Diffstat (limited to 'src/devices')
50 files changed, 2987 insertions, 2094 deletions
diff --git a/src/devices/adsl/meson.build b/src/devices/adsl/meson.build index 7ac0e123..f92e809c 100644 --- a/src/devices/adsl/meson.build +++ b/src/devices/adsl/meson.build @@ -1,11 +1,11 @@ sources = files( 'nm-atm-manager.c', - 'nm-device-adsl.c' + 'nm-device-adsl.c', ) deps = [ libudev_dep, - nm_dep + nm_dep, ] libnm_device_plugin_adsl = shared_module( @@ -15,7 +15,7 @@ libnm_device_plugin_adsl = shared_module( link_args: ldflags_linker_script_devices, link_depends: linker_script_devices, install: true, - install_dir: nm_plugindir + install_dir: nm_plugindir, ) core_plugins += libnm_device_plugin_adsl diff --git a/src/devices/adsl/nm-device-adsl.c b/src/devices/adsl/nm-device-adsl.c index c9984f51..ddc03ce3 100644 --- a/src/devices/adsl/nm-device-adsl.c +++ b/src/devices/adsl/nm-device-adsl.c @@ -259,8 +259,9 @@ pppoe_vcc_config (NMDeviceAdsl *self) NMDevice *device = NM_DEVICE (self); NMSettingAdsl *s_adsl; - s_adsl = nm_connection_get_setting_adsl (nm_device_get_applied_connection (device)); - g_assert (s_adsl); + s_adsl = nm_device_get_applied_setting (device, NM_TYPE_SETTING_ADSL); + + g_return_val_if_fail (s_adsl, FALSE); /* Set up the VCC */ if (!br2684_assign_vcc (self, s_adsl)) @@ -389,7 +390,8 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *out_failure_reason) NMSettingAdsl *s_adsl; const char *protocol; - s_adsl = nm_connection_get_setting_adsl (nm_device_get_applied_connection (device)); + s_adsl = nm_device_get_applied_setting (device, NM_TYPE_SETTING_ADSL); + g_return_val_if_fail (s_adsl, NM_ACT_STAGE_RETURN_FAILURE); protocol = nm_setting_adsl_get_protocol (s_adsl); @@ -465,8 +467,11 @@ act_stage3_ip4_config_start (NMDevice *device, const char *ppp_iface; req = nm_device_get_act_request (device); + g_return_val_if_fail (req, NM_ACT_STAGE_RETURN_FAILURE); - s_adsl = (NMSettingAdsl *) nm_device_get_applied_setting (device, NM_TYPE_SETTING_ADSL); + + s_adsl = nm_device_get_applied_setting (device, NM_TYPE_SETTING_ADSL); + g_return_val_if_fail (s_adsl, NM_ACT_STAGE_RETURN_FAILURE); /* PPPoE uses the NAS interface, not the ATM interface */ @@ -523,7 +528,7 @@ adsl_cleanup (NMDeviceAdsl *self) if (priv->ppp_manager) { g_signal_handlers_disconnect_by_func (priv->ppp_manager, G_CALLBACK (ppp_state_changed), self); g_signal_handlers_disconnect_by_func (priv->ppp_manager, G_CALLBACK (ppp_ip4_config), self); - nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL); + nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL, NULL); g_clear_object (&priv->ppp_manager); } diff --git a/src/devices/bluetooth/meson.build b/src/devices/bluetooth/meson.build index 628a3bc8..b2f67ceb 100644 --- a/src/devices/bluetooth/meson.build +++ b/src/devices/bluetooth/meson.build @@ -5,12 +5,12 @@ sources = files( 'nm-bluez4-manager.c', 'nm-bluez5-manager.c', 'nm-bt-error.c', - 'nm-device-bt.c' + 'nm-device-bt.c', ) deps = [ libnm_wwan_dep, - nm_dep + nm_dep, ] if enable_bluez5_dun @@ -27,7 +27,7 @@ libnm_device_plugin_bluetooth = shared_module( link_depends: linker_script_devices, install: true, install_dir: nm_plugindir, - install_rpath: nm_plugindir + install_rpath: nm_plugindir, ) core_plugins += libnm_device_plugin_bluetooth diff --git a/src/devices/bluetooth/nm-bluez-device.c b/src/devices/bluetooth/nm-bluez-device.c index b722f692..8e2a96b7 100644 --- a/src/devices/bluetooth/nm-bluez-device.c +++ b/src/devices/bluetooth/nm-bluez-device.c @@ -451,6 +451,9 @@ nm_bluez_device_disconnect (NMBluezDevice *self) g_return_if_fail (priv->dbus_connection); + /* FIXME: if we are in the process of connecting and cancel the + * connection attempt, we must complete the pending connect request. + * However, we must also ensure that we don't leave a connected device. */ if (priv->connection_bt_type == NM_BT_CAPABILITY_DUN) { if (priv->bluez_version == 4) { /* Can't pass a NULL interface name through dbus to bluez, so just @@ -496,76 +499,109 @@ out: } static void -bluez_connect_cb (GDBusConnection *dbus_connection, +_connect_complete (NMBluezDevice *self, + const char *device, + NMBluezDeviceConnectCallback callback, + gpointer callback_user_data, + GError *error) +{ + NMBluezDevicePrivate *priv = NM_BLUEZ_DEVICE_GET_PRIVATE (self); + + nm_assert ((device || error) && !(device && error)); + + if ( device + && priv->bluez_version == 5) { + priv->connected = TRUE; + _notify (self, PROP_CONNECTED); + } + + if (callback) + callback (self, device, error, callback_user_data); +} + +static void +_connect_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (user_data); - GObject *result_object = g_async_result_get_source_object (G_ASYNC_RESULT (result)); - NMBluezDevice *self = NM_BLUEZ_DEVICE (result_object); - NMBluezDevicePrivate *priv = NM_BLUEZ_DEVICE_GET_PRIVATE (self); - GError *error = NULL; - char *device; - GVariant *variant; + gs_unref_object NMBluezDevice *self = NULL; + NMBluezDevicePrivate *priv; + NMBluezDeviceConnectCallback callback; + gpointer callback_user_data; + gs_free_error GError *error = NULL; + char *device = NULL; + gs_unref_variant GVariant *variant = NULL; - variant = g_dbus_connection_call_finish (dbus_connection, res, &error); + nm_utils_user_data_unpack (user_data, &self, &callback, &callback_user_data); - if (!variant) { - g_simple_async_result_take_error (result, error); - } else { - g_variant_get (variant, "(s)", &device); + priv = NM_BLUEZ_DEVICE_GET_PRIVATE (self); - g_simple_async_result_set_op_res_gpointer (result, - g_strdup (device), - g_free); + variant = _nm_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, G_VARIANT_TYPE ("(s)"), &error); + if (variant) { + g_variant_get (variant, "(s)", &device); priv->b4_iface = device; - g_variant_unref (variant); } - g_simple_async_result_complete (result); - g_object_unref (result); - g_object_unref (result_object); + _connect_complete (self, device, callback, callback_user_data, error); } #if WITH_BLUEZ5_DUN static void -bluez5_dun_connect_cb (NMBluez5DunContext *context, - const char *device, - GError *error, - gpointer user_data) +_connect_cb_bluez5_dun (NMBluez5DunContext *context, + const char *device, + GError *error, + gpointer user_data) { - GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (user_data); + gs_unref_object NMBluezDevice *self = NULL; + gs_unref_object GCancellable *cancellable = NULL; + NMBluezDeviceConnectCallback callback; + gpointer callback_user_data; + gs_free_error GError *cancelled_error = NULL; - if (error) { - g_simple_async_result_take_error (result, error); - } else { - g_simple_async_result_set_op_res_gpointer (result, - g_strdup (device), - g_free); - } + nm_utils_user_data_unpack (user_data, &self, &cancellable, &callback, &callback_user_data); + + /* FIXME(shutdown): the async operation nm_bluez5_dun_connect() should be cancellable. + * Fake it here. */ + if (g_cancellable_set_error_if_cancelled (cancellable, &cancelled_error)) + error = cancelled_error; - g_simple_async_result_complete (result); - g_object_unref (result); + _connect_complete (self, device, callback, callback_user_data, error); } -#endif +#else /* WITH_BLUEZ5_DUN */ +static void +_connect_cb_bluez5_dun_idle_no_b5 (gpointer user_data, + GCancellable *cancellable) +{ + gs_unref_object NMBluezDevice *self = NULL; + NMBluezDeviceConnectCallback callback; + gpointer callback_user_data; + gs_free_error GError *error = NULL; + + nm_utils_user_data_unpack (user_data, &self, &callback, &callback_user_data); + + if (!g_cancellable_set_error_if_cancelled (cancellable, &error)) { + g_set_error (&error, + NM_BT_ERROR, + NM_BT_ERROR_DUN_CONNECT_FAILED, + "NetworkManager built without support for Bluez 5"); + } + callback (self, NULL, error, callback_user_data); +} +#endif /* WITH_BLUEZ5_DUN */ void nm_bluez_device_connect_async (NMBluezDevice *self, NMBluetoothCapabilities connection_bt_type, - GAsyncReadyCallback callback, - gpointer user_data) + GCancellable *cancellable, + NMBluezDeviceConnectCallback callback, + gpointer callback_user_data) { - GSimpleAsyncResult *simple; NMBluezDevicePrivate *priv = NM_BLUEZ_DEVICE_GET_PRIVATE (self); const char *dbus_iface = NULL; const char *connect_type = NULL; g_return_if_fail (priv->capabilities & connection_bt_type & (NM_BT_CAPABILITY_DUN | NM_BT_CAPABILITY_NAP)); - simple = g_simple_async_result_new (G_OBJECT (self), - callback, - user_data, - nm_bluez_device_connect_async); priv->connection_bt_type = connection_bt_type; if (connection_bt_type == NM_BT_CAPABILITY_NAP) { @@ -582,19 +618,29 @@ nm_bluez_device_connect_async (NMBluezDevice *self, #if WITH_BLUEZ5_DUN if (priv->b5_dun_context == NULL) priv->b5_dun_context = nm_bluez5_dun_new (priv->adapter_address, priv->address); - nm_bluez5_dun_connect (priv->b5_dun_context, bluez5_dun_connect_cb, simple); + nm_bluez5_dun_connect (priv->b5_dun_context, + _connect_cb_bluez5_dun, + nm_utils_user_data_pack (g_object_ref (self), + nm_g_object_ref (cancellable), + callback, + callback_user_data)); #else - g_simple_async_result_set_error (simple, - NM_BT_ERROR, - NM_BT_ERROR_DUN_CONNECT_FAILED, - "NetworkManager built without support for Bluez 5"); - g_simple_async_result_complete (simple); + if (callback) { + nm_utils_invoke_on_idle (_connect_cb_bluez5_dun_idle_no_b5, + nm_utils_user_data_pack (g_object_ref (self), + callback, + callback_user_data), + cancellable); + } #endif return; } } else - g_assert_not_reached (); + g_return_if_reached (); + /* FIXME: we need to remember that a connect is in progress. + * So, if the request gets cancelled, that we disconnect the + * connection that was established in the meantime. */ g_dbus_connection_call (priv->dbus_connection, NM_BLUEZ_SERVICE, priv->path, @@ -604,37 +650,11 @@ nm_bluez_device_connect_async (NMBluezDevice *self, NULL, G_DBUS_CALL_FLAGS_NONE, 20000, - NULL, - (GAsyncReadyCallback) bluez_connect_cb, - simple); -} - -const char * -nm_bluez_device_connect_finish (NMBluezDevice *self, - GAsyncResult *result, - GError **error) -{ - NMBluezDevicePrivate *priv = NM_BLUEZ_DEVICE_GET_PRIVATE (self); - GSimpleAsyncResult *simple; - const char *device; - - g_return_val_if_fail (g_simple_async_result_is_valid (result, - G_OBJECT (self), - nm_bluez_device_connect_async), - NULL); - - simple = (GSimpleAsyncResult *) result; - - if (g_simple_async_result_propagate_error (simple, error)) - return NULL; - - device = (const char *) g_simple_async_result_get_op_res_gpointer (simple); - if (device && priv->bluez_version == 5) { - priv->connected = TRUE; - _notify (self, PROP_CONNECTED); - } - - return device; + cancellable, + _connect_cb, + nm_utils_user_data_pack (g_object_ref (self), + callback, + callback_user_data)); } /*****************************************************************************/ diff --git a/src/devices/bluetooth/nm-bluez-device.h b/src/devices/bluetooth/nm-bluez-device.h index f8a1872f..d2d0beb0 100644 --- a/src/devices/bluetooth/nm-bluez-device.h +++ b/src/devices/bluetooth/nm-bluez-device.h @@ -66,16 +66,17 @@ guint32 nm_bluez_device_get_capabilities (NMBluezDevice *self); gboolean nm_bluez_device_get_connected (NMBluezDevice *self); +typedef void (*NMBluezDeviceConnectCallback) (NMBluezDevice *self, + const char *device, + GError *error, + gpointer user_data); + void nm_bluez_device_connect_async (NMBluezDevice *self, NMBluetoothCapabilities connection_bt_type, - GAsyncReadyCallback callback, - gpointer user_data); - -const char * -nm_bluez_device_connect_finish (NMBluezDevice *self, - GAsyncResult *result, - GError **error); + GCancellable *cancellable, + NMBluezDeviceConnectCallback callback, + gpointer callback_user_data); void nm_bluez_device_disconnect (NMBluezDevice *self); diff --git a/src/devices/bluetooth/nm-bluez5-dun.c b/src/devices/bluetooth/nm-bluez5-dun.c index ca09b276..32f8da65 100644 --- a/src/devices/bluetooth/nm-bluez5-dun.c +++ b/src/devices/bluetooth/nm-bluez5-dun.c @@ -342,13 +342,14 @@ nm_bluez5_dun_connect (NMBluez5DunContext *context, if (context->rfcomm_channel != -1) { nm_log_dbg (LOGD_BT, "(%s): channel number on device %s cached: %d", - context->src_str, context->dst_str, context->rfcomm_channel); + context->src_str, context->dst_str, context->rfcomm_channel); + /* FIXME: don't invoke the callback synchronously. */ dun_connect (context); return; } nm_log_dbg (LOGD_BT, "(%s): starting channel number discovery for device %s", - context->src_str, context->dst_str); + context->src_str, context->dst_str); context->sdp_session = sdp_connect (&context->src, &context->dst, SDP_NON_BLOCKING); if (!context->sdp_session) { @@ -358,10 +359,12 @@ nm_bluez5_dun_connect (NMBluez5DunContext *context, error = g_error_new (NM_BT_ERROR, NM_BT_ERROR_DUN_CONNECT_FAILED, "Failed to connect to the SDP server: (%d) %s", err, strerror (err)); + /* FIXME: don't invoke the callback synchronously. */ context->callback (context, NULL, error, context->user_data); return; } + /* FIXME(shutdown): make connect cancellable. */ channel = g_io_channel_unix_new (sdp_get_socket (context->sdp_session)); context->sdp_watch_id = g_io_add_watch (channel, G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL, diff --git a/src/devices/bluetooth/nm-bluez5-dun.h b/src/devices/bluetooth/nm-bluez5-dun.h index 124c1a05..b75e4399 100644 --- a/src/devices/bluetooth/nm-bluez5-dun.h +++ b/src/devices/bluetooth/nm-bluez5-dun.h @@ -32,7 +32,8 @@ NMBluez5DunContext *nm_bluez5_dun_new (const char *adapter, const char *remote); void nm_bluez5_dun_connect (NMBluez5DunContext *context, - NMBluez5DunFunc callback, gpointer user_data); + NMBluez5DunFunc callback, + gpointer user_data); /* Clean up connection resources */ void nm_bluez5_dun_cleanup (NMBluez5DunContext *context); diff --git a/src/devices/bluetooth/nm-device-bt.c b/src/devices/bluetooth/nm-device-bt.c index 1f650d33..68209d8a 100644 --- a/src/devices/bluetooth/nm-device-bt.c +++ b/src/devices/bluetooth/nm-device-bt.c @@ -78,7 +78,9 @@ typedef struct { char *rfcomm_iface; NMModem *modem; - guint32 timeout_id; + guint timeout_id; + + GCancellable *cancellable; guint32 bt_type; /* BT type of the current connection */ } NMDeviceBtPrivate; @@ -318,12 +320,10 @@ complete_connection (NMDevice *device, if (s_gsm) { fallback_prefix = _("GSM connection"); - if (!nm_setting_gsm_get_number (s_gsm)) - g_object_set (G_OBJECT (s_gsm), NM_SETTING_GSM_NUMBER, "*99#", NULL); } else { fallback_prefix = _("CDMA connection"); if (!nm_setting_cdma_get_number (s_cdma)) - g_object_set (G_OBJECT (s_cdma), NM_SETTING_GSM_NUMBER, "#777", NULL); + g_object_set (G_OBJECT (s_cdma), NM_SETTING_CDMA_NUMBER, "#777", NULL); } } else { g_set_error_literal (error, @@ -672,6 +672,7 @@ component_added (NMDevice *device, GObject *component) /* Got the modem */ nm_clear_g_source (&priv->timeout_id); + nm_clear_g_cancellable (&priv->cancellable); /* Can only accept the modem in stage2, but since the interface matched * what we were expecting, don't let anything else claim the modem either. @@ -715,8 +716,11 @@ static gboolean modem_find_timeout (gpointer user_data) { NMDeviceBt *self = NM_DEVICE_BT (user_data); + NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (self); + + priv->timeout_id = 0; + nm_clear_g_cancellable (&priv->cancellable); - NM_DEVICE_BT_GET_PRIVATE (self)->timeout_id = 0; nm_device_state_changed (NM_DEVICE (self), NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_MODEM_NOT_FOUND); @@ -738,8 +742,8 @@ check_connect_continue (NMDeviceBt *self) "Activation: (bluetooth) Stage 2 of 5 (Device Configure) successful. Will connect via %s.", dun ? "DUN" : (pan ? "PAN" : "unknown")); - /* Kill the connect timeout since we're connected now */ nm_clear_g_source (&priv->timeout_id); + nm_clear_g_cancellable (&priv->cancellable); if (pan) { /* Bluez says we're connected now. Start IP config. */ @@ -755,25 +759,25 @@ check_connect_continue (NMDeviceBt *self) } static void -bluez_connect_cb (GObject *object, - GAsyncResult *res, - void *user_data) +bluez_connect_cb (NMBluezDevice *bt_device, + const char *device_name, + GError *error, + gpointer user_data) { - gs_unref_object NMDeviceBt *self = NM_DEVICE_BT (user_data); + gs_unref_object NMDeviceBt *self = user_data; NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (self); - GError *error = NULL; - const char *device; - device = nm_bluez_device_connect_finish (NM_BLUEZ_DEVICE (object), - res, &error); + if (nm_utils_error_is_cancelled (error, FALSE)) + return; + + nm_clear_g_source (&priv->timeout_id); + g_clear_object (&priv->cancellable); if (!nm_device_is_activating (NM_DEVICE (self))) return; - if (!device) { + if (!device_name) { _LOGW (LOGD_BT, "Error connecting with bluez: %s", error->message); - g_clear_error (&error); - nm_device_state_changed (NM_DEVICE (self), NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_BT_FAILED); @@ -782,10 +786,10 @@ bluez_connect_cb (GObject *object, if (priv->bt_type == NM_BT_CAPABILITY_DUN) { g_free (priv->rfcomm_iface); - priv->rfcomm_iface = g_strdup (device); + priv->rfcomm_iface = g_strdup (device_name); } else if (priv->bt_type == NM_BT_CAPABILITY_NAP) { - if (!nm_device_set_ip_iface (NM_DEVICE (self), device)) { - _LOGW (LOGD_BT, "Error connecting with bluez: cannot find device %s", device); + if (!nm_device_set_ip_iface (NM_DEVICE (self), device_name)) { + _LOGW (LOGD_BT, "Error connecting with bluez: cannot find device %s", device_name); nm_device_state_changed (NM_DEVICE (self), NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_BT_FAILED); @@ -843,10 +847,13 @@ static gboolean bt_connect_timeout (gpointer user_data) { NMDeviceBt *self = NM_DEVICE_BT (user_data); + NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (self); _LOGD (LOGD_BT, "initial connection timed out"); - NM_DEVICE_BT_GET_PRIVATE (self)->timeout_id = 0; + priv->timeout_id = 0; + nm_clear_g_cancellable (&priv->cancellable); + nm_device_state_changed (NM_DEVICE (self), NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_BT_FAILED); @@ -876,13 +883,17 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *out_failure_reason) _LOGD (LOGD_BT, "requesting connection to the device"); - /* Connect to the BT device */ - nm_bluez_device_connect_async (priv->bt_device, - priv->bt_type & (NM_BT_CAPABILITY_DUN | NM_BT_CAPABILITY_NAP), - bluez_connect_cb, g_object_ref (device)); - nm_clear_g_source (&priv->timeout_id); + nm_clear_g_cancellable (&priv->cancellable); + priv->timeout_id = g_timeout_add_seconds (30, bt_connect_timeout, device); + priv->cancellable = g_cancellable_new (); + + nm_bluez_device_connect_async (priv->bt_device, + priv->bt_type & (NM_BT_CAPABILITY_DUN | NM_BT_CAPABILITY_NAP), + priv->cancellable, + bluez_connect_cb, + g_object_ref (self)); return NM_ACT_STAGE_RETURN_POSTPONE; } @@ -925,6 +936,9 @@ deactivate (NMDevice *device) priv->have_iface = FALSE; priv->connected = FALSE; + nm_clear_g_source (&priv->timeout_id); + nm_clear_g_cancellable (&priv->cancellable); + if (priv->bt_type == NM_BT_CAPABILITY_DUN) { if (priv->modem) { nm_modem_deactivate (priv->modem, device); @@ -942,8 +956,6 @@ deactivate (NMDevice *device) if (priv->bt_type != NM_BT_CAPABILITY_NONE) nm_bluez_device_disconnect (priv->bt_device); - nm_clear_g_source (&priv->timeout_id); - priv->bt_type = NM_BT_CAPABILITY_NONE; g_free (priv->rfcomm_iface); @@ -1128,6 +1140,7 @@ dispose (GObject *object) NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE ((NMDeviceBt *) object); nm_clear_g_source (&priv->timeout_id); + nm_clear_g_cancellable (&priv->cancellable); g_signal_handlers_disconnect_matched (priv->bt_device, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, object); diff --git a/src/devices/nm-acd-manager.c b/src/devices/nm-acd-manager.c index 035487a3..a0a175be 100644 --- a/src/devices/nm-acd-manager.c +++ b/src/devices/nm-acd-manager.c @@ -39,40 +39,23 @@ typedef enum { typedef struct { in_addr_t address; gboolean duplicate; - NMAcdManager *manager; - NAcd *acd; - GIOChannel *channel; - guint event_id; + NAcdProbe *probe; } AddressInfo; -enum { - PROBE_TERMINATED, - LAST_SIGNAL, -}; - -static guint signals[LAST_SIGNAL] = { 0 }; - -typedef struct { +struct _NMAcdManager { int ifindex; guint8 hwaddr[ETH_ALEN]; State state; GHashTable *addresses; guint completed; -} NMAcdManagerPrivate; + NAcd *acd; + GIOChannel *channel; + guint event_id; -struct _NMAcdManager { - GObject parent; - NMAcdManagerPrivate _priv; + NMAcdCallbacks callbacks; + gpointer user_data; }; -struct _NMAcdManagerClass { - GObjectClass parent; -}; - -G_DEFINE_TYPE (NMAcdManager, nm_acd_manager, G_TYPE_OBJECT) - -#define NM_ACD_MANAGER_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMAcdManager, NM_IS_ACD_MANAGER) - /*****************************************************************************/ #define _NMLOG_DOMAIN LOGD_IP4 @@ -80,14 +63,13 @@ G_DEFINE_TYPE (NMAcdManager, nm_acd_manager, G_TYPE_OBJECT) #define _NMLOG(level, ...) \ G_STMT_START { \ char _sbuf[64]; \ - int _ifindex = (self) ? NM_ACD_MANAGER_GET_PRIVATE (self)->ifindex : 0; \ \ nm_log ((level), _NMLOG_DOMAIN, \ - nm_platform_link_get_name (NM_PLATFORM_GET, _ifindex), \ + self && self->ifindex > 0 ? nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex) : NULL, \ NULL, \ "%s%s: " _NM_UTILS_MACRO_FIRST (__VA_ARGS__), \ _NMLOG_PREFIX_NAME, \ - self ? nm_sprintf_buf (_sbuf, "[%p,%d]", self, _ifindex) : "" \ + self ? nm_sprintf_buf (_sbuf, "[%p,%d]", self, self->ifindex) : "" \ _NM_UTILS_MACRO_REST (__VA_ARGS__)); \ } G_STMT_END @@ -111,32 +93,25 @@ _acd_event_to_string (unsigned int event) return NULL; } -#define acd_event_to_string(event) NM_UTILS_LOOKUP_STR (_acd_event_to_string, event) +#define acd_event_to_string_a(event) NM_UTILS_LOOKUP_STR_A (_acd_event_to_string, event) static const char * -_acd_error_to_string (int error) +acd_error_to_string (int error) { if (error < 0) - return strerror(-error); + return g_strerror (-error); switch (error) { case _N_ACD_E_SUCCESS: return "success"; - case N_ACD_E_DONE: - return "no more events (engine running)"; - case N_ACD_E_STOPPED: - return "no more events (engine stopped)"; case N_ACD_E_PREEMPTED: return "preempted"; case N_ACD_E_INVALID_ARGUMENT: return "invalid argument"; - case N_ACD_E_BUSY: - return "busy"; } - return NULL; -} -#define acd_error_to_string(error) NM_UTILS_LOOKUP_STR (_acd_error_to_string, error) + g_return_val_if_reached (NULL); +} /*****************************************************************************/ @@ -152,21 +127,18 @@ _acd_error_to_string (int error) gboolean nm_acd_manager_add_address (NMAcdManager *self, in_addr_t address) { - NMAcdManagerPrivate *priv; AddressInfo *info; - g_return_val_if_fail (NM_IS_ACD_MANAGER (self), FALSE); - priv = NM_ACD_MANAGER_GET_PRIVATE (self); - g_return_val_if_fail (priv->state == STATE_INIT, FALSE); + g_return_val_if_fail (self, FALSE); + g_return_val_if_fail (self->state == STATE_INIT, FALSE); - if (g_hash_table_lookup (priv->addresses, GUINT_TO_POINTER (address))) + if (g_hash_table_lookup (self->addresses, GUINT_TO_POINTER (address))) return FALSE; info = g_slice_new0 (AddressInfo); info->address = address; - info->manager = self; - g_hash_table_insert (priv->addresses, GUINT_TO_POINTER (address), info); + g_hash_table_insert (self->addresses, GUINT_TO_POINTER (address), info); return TRUE; } @@ -174,115 +146,142 @@ nm_acd_manager_add_address (NMAcdManager *self, in_addr_t address) static gboolean acd_event (GIOChannel *source, GIOCondition condition, gpointer data) { - AddressInfo *info = data; - NMAcdManager *self = info->manager; - NMAcdManagerPrivate *priv = NM_ACD_MANAGER_GET_PRIVATE (self); + NMAcdManager *self = data; NAcdEvent *event; + AddressInfo *info; + gboolean emit_probe_terminated = FALSE; char address_str[INET_ADDRSTRLEN]; gs_free char *hwaddr_str = NULL; int r; - if ( n_acd_dispatch (info->acd) - || n_acd_pop_event (info->acd, &event)) + if (n_acd_dispatch (self->acd)) return G_SOURCE_CONTINUE; - switch (event->event) { - case N_ACD_EVENT_READY: - info->duplicate = FALSE; - if (priv->state == STATE_ANNOUNCING) { - r = n_acd_announce (info->acd, N_ACD_DEFEND_ONCE); - if (r) { - _LOGW ("couldn't announce address %s on interface '%s': %s", - nm_utils_inet4_ntop (info->address, address_str), - nm_platform_link_get_name (NM_PLATFORM_GET, priv->ifindex), - acd_error_to_string (r)); - } else { - _LOGD ("announcing address %s", - nm_utils_inet4_ntop (info->address, address_str)); + while ( !n_acd_pop_event (self->acd, &event) + && event) { + gboolean check_probing_done = FALSE; + + switch (event->event) { + case N_ACD_EVENT_READY: + n_acd_probe_get_userdata (event->ready.probe, (void **) &info); + info->duplicate = FALSE; + if (self->state == STATE_ANNOUNCING) { + /* fake probe ended, start announcing */ + r = n_acd_probe_announce (info->probe, N_ACD_DEFEND_ONCE); + if (r) { + _LOGW ("couldn't announce address %s on interface '%s': %s", + nm_utils_inet4_ntop (info->address, address_str), + nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex), + acd_error_to_string (r)); + } else { + _LOGD ("announcing address %s", + nm_utils_inet4_ntop (info->address, address_str)); + } } + check_probing_done = TRUE; + break; + case N_ACD_EVENT_USED: + n_acd_probe_get_userdata (event->used.probe, (void **) &info); + info->duplicate = TRUE; + check_probing_done = TRUE; + break; + case N_ACD_EVENT_DEFENDED: + n_acd_probe_get_userdata (event->defended.probe, (void **) &info); + _LOGD ("defended address %s from host %s", + nm_utils_inet4_ntop (info->address, address_str), + (hwaddr_str = nm_utils_hwaddr_ntoa (event->defended.sender, + event->defended.n_sender))); + break; + case N_ACD_EVENT_CONFLICT: + n_acd_probe_get_userdata (event->conflict.probe, (void **) &info); + _LOGW ("conflict for address %s detected with host %s on interface '%s'", + nm_utils_inet4_ntop (info->address, address_str), + (hwaddr_str = nm_utils_hwaddr_ntoa (event->defended.sender, + event->defended.n_sender)), + nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex)); + break; + default: + _LOGD ("unhandled event '%s'", acd_event_to_string_a (event->event)); + break; + } + + if ( check_probing_done + && self->state == STATE_PROBING + && ++self->completed == g_hash_table_size (self->addresses)) { + self->state = STATE_PROBE_DONE; + emit_probe_terminated = TRUE; } - break; - case N_ACD_EVENT_USED: - info->duplicate = TRUE; - break; - case N_ACD_EVENT_DEFENDED: - _LOGD ("defended address %s from host %s", - nm_utils_inet4_ntop (info->address, address_str), - (hwaddr_str = nm_utils_hwaddr_ntoa (event->defended.sender, - event->defended.n_sender))); - break; - case N_ACD_EVENT_CONFLICT: - _LOGW ("conflict for address %s detected with host %s on interface '%s'", - nm_utils_inet4_ntop (info->address, address_str), - (hwaddr_str = nm_utils_hwaddr_ntoa (event->defended.sender, - event->defended.n_sender)), - nm_platform_link_get_name (NM_PLATFORM_GET, priv->ifindex)); - break; - default: - _LOGD ("event '%s' for address %s", - acd_event_to_string (event->event), - nm_utils_inet4_ntop (info->address, address_str)); - return G_SOURCE_CONTINUE; } - if ( priv->state == STATE_PROBING - && ++priv->completed == g_hash_table_size (priv->addresses)) { - priv->state = STATE_PROBE_DONE; - g_signal_emit (self, signals[PROBE_TERMINATED], 0); + if (emit_probe_terminated) { + if (self->callbacks.probe_terminated_callback) { + self->callbacks.probe_terminated_callback (self, + self->user_data); + } } return G_SOURCE_CONTINUE; } static gboolean -acd_probe_start (NMAcdManager *self, - AddressInfo *info, - guint64 timeout) +acd_probe_add (NMAcdManager *self, + AddressInfo *info, + guint64 timeout) { - NMAcdManagerPrivate *priv = NM_ACD_MANAGER_GET_PRIVATE (self); - NAcdConfig *config; - int r, fd; + NAcdProbeConfig *probe_config; + int r; + char sbuf[NM_UTILS_INET_ADDRSTRLEN]; - r = n_acd_new (&info->acd); + r = n_acd_probe_config_new (&probe_config); if (r) { - _LOGW ("could not create ACD for %s on interface '%s': %s", - nm_utils_inet4_ntop (info->address, NULL), - nm_platform_link_get_name (NM_PLATFORM_GET, priv->ifindex), + _LOGW ("could not create probe config for %s on interface '%s': %s", + nm_utils_inet4_ntop (info->address, sbuf), + nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex), acd_error_to_string (r)); return FALSE; } - n_acd_get_fd (info->acd, &fd); - info->channel = g_io_channel_unix_new (fd); - info->event_id = g_io_add_watch (info->channel, G_IO_IN, acd_event, info); - - config = &(NAcdConfig) { - .ifindex = priv->ifindex, - .mac = priv->hwaddr, - .n_mac = ETH_ALEN, - .ip = info->address, - .timeout_msec = timeout, - .transport = N_ACD_TRANSPORT_ETHERNET, - }; + n_acd_probe_config_set_ip (probe_config, (struct in_addr) { info->address }); + n_acd_probe_config_set_timeout (probe_config, timeout); - r = n_acd_start (info->acd, config); + r = n_acd_probe (self->acd, &info->probe, probe_config); if (r) { _LOGW ("could not start probe for %s on interface '%s': %s", - nm_utils_inet4_ntop (info->address, NULL), - nm_platform_link_get_name (NM_PLATFORM_GET, priv->ifindex), + nm_utils_inet4_ntop (info->address, sbuf), + nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex), acd_error_to_string (r)); + n_acd_probe_config_free (probe_config); return FALSE; } - if (timeout) { - _LOGD ("started probe for %s with timeout %llu", - nm_utils_inet4_ntop (info->address, NULL), - (unsigned long long) timeout); - } + n_acd_probe_set_userdata (info->probe, info); + n_acd_probe_config_free (probe_config); return TRUE; } +static int +acd_init (NMAcdManager *self) +{ + NAcdConfig *config; + int r; + + if (self->acd) + return 0; + + r = n_acd_config_new (&config); + if (r) + return r; + + n_acd_config_set_ifindex (config, self->ifindex); + n_acd_config_set_transport (config, N_ACD_TRANSPORT_ETHERNET); + n_acd_config_set_mac (config, self->hwaddr, ETH_ALEN); + + r = n_acd_new (&self->acd, config); + n_acd_config_free (config); + return r; +} + /** * nm_acd_manager_start_probe: * @self: a #NMAcdManager @@ -297,59 +296,36 @@ acd_probe_start (NMAcdManager *self, gboolean nm_acd_manager_start_probe (NMAcdManager *self, guint timeout) { - NMAcdManagerPrivate *priv; GHashTableIter iter; AddressInfo *info; gboolean success = FALSE; + int fd, r; - g_return_val_if_fail (NM_IS_ACD_MANAGER (self), FALSE); - priv = NM_ACD_MANAGER_GET_PRIVATE (self); - g_return_val_if_fail (priv->state == STATE_INIT, FALSE); + g_return_val_if_fail (self, FALSE); + g_return_val_if_fail (self->state == STATE_INIT, FALSE); + + r = acd_init (self); + if (r) { + _LOGW ("couldn't init ACD for probing on interface '%s': %s", + nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex), + acd_error_to_string (r)); + return FALSE; + } - priv->completed = 0; + self->completed = 0; - g_hash_table_iter_init (&iter, priv->addresses); + g_hash_table_iter_init (&iter, self->addresses); while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &info)) - success |= acd_probe_start (self, info, timeout); + success |= acd_probe_add (self, info, timeout); if (success) - priv->state = STATE_PROBING; + self->state = STATE_PROBING; - return success; -} - -/** - * nm_acd_manager_reset: - * @self: a #NMAcdManager - * - * Stop any operation in progress and reset @self to the initial state. - */ -void -nm_acd_manager_reset (NMAcdManager *self) -{ - NMAcdManagerPrivate *priv; - - g_return_if_fail (NM_IS_ACD_MANAGER (self)); - priv = NM_ACD_MANAGER_GET_PRIVATE (self); - - g_hash_table_remove_all (priv->addresses); + n_acd_get_fd (self->acd, &fd); + self->channel = g_io_channel_unix_new (fd); + self->event_id = g_io_add_watch (self->channel, G_IO_IN, acd_event, self); - priv->state = STATE_INIT; -} - -/** - * nm_acd_manager_destroy: - * @self: the #NMAcdManager - * - * Calls nm_acd_manager_reset() and unrefs @self. - */ -void -nm_acd_manager_destroy (NMAcdManager *self) -{ - g_return_if_fail (NM_IS_ACD_MANAGER (self)); - - nm_acd_manager_reset (self); - g_object_unref (self); + return success; } /** @@ -365,15 +341,12 @@ nm_acd_manager_destroy (NMAcdManager *self) gboolean nm_acd_manager_check_address (NMAcdManager *self, in_addr_t address) { - NMAcdManagerPrivate *priv; AddressInfo *info; - g_return_val_if_fail (NM_IS_ACD_MANAGER (self), FALSE); - priv = NM_ACD_MANAGER_GET_PRIVATE (self); - g_return_val_if_fail ( priv->state == STATE_INIT - || priv->state == STATE_PROBE_DONE, FALSE); + g_return_val_if_fail (self, FALSE); + g_return_val_if_fail (NM_IN_SET (self->state, STATE_INIT, STATE_PROBE_DONE), FALSE); - info = g_hash_table_lookup (priv->addresses, GUINT_TO_POINTER (address)); + info = g_hash_table_lookup (self->addresses, GUINT_TO_POINTER (address)); g_return_val_if_fail (info, FALSE); return !info->duplicate; @@ -388,41 +361,43 @@ nm_acd_manager_check_address (NMAcdManager *self, in_addr_t address) void nm_acd_manager_announce_addresses (NMAcdManager *self) { - NMAcdManagerPrivate *priv = NM_ACD_MANAGER_GET_PRIVATE (self); GHashTableIter iter; AddressInfo *info; int r; - if (priv->state == STATE_INIT) { + r = acd_init (self); + if (r) { + _LOGW ("couldn't init ACD for announcing addresses on interface '%s': %s", + nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex), + acd_error_to_string (r)); + return; + } + + if (self->state == STATE_INIT) { /* n-acd can't announce without probing, therefore let's * start a fake probe with zero timeout and then perform - * the announce. */ - priv->state = STATE_ANNOUNCING; - g_hash_table_iter_init (&iter, priv->addresses); - while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &info)) { - if (!acd_probe_start (self, info, 0)) { - _LOGW ("couldn't announce address %s on interface '%s'", - nm_utils_inet4_ntop (info->address, NULL), - nm_platform_link_get_name (NM_PLATFORM_GET, priv->ifindex)); - } - } - } else if (priv->state == STATE_PROBE_DONE) { - priv->state = STATE_ANNOUNCING; - g_hash_table_iter_init (&iter, priv->addresses); + * the announcement. */ + g_hash_table_iter_init (&iter, self->addresses); + while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &info)) + acd_probe_add (self, info, 0); + self->state = STATE_ANNOUNCING; + } else if (self->state == STATE_ANNOUNCING) { + char sbuf[NM_UTILS_INET_ADDRSTRLEN]; + + g_hash_table_iter_init (&iter, self->addresses); while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &info)) { if (info->duplicate) continue; - r = n_acd_announce (info->acd, N_ACD_DEFEND_ONCE); + r = n_acd_probe_announce (info->probe, N_ACD_DEFEND_ONCE); if (r) { _LOGW ("couldn't announce address %s on interface '%s': %s", - nm_utils_inet4_ntop (info->address, NULL), - nm_platform_link_get_name (NM_PLATFORM_GET, priv->ifindex), + nm_utils_inet4_ntop (info->address, sbuf), + nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex), acd_error_to_string (r)); } else - _LOGD ("announcing address %s", nm_utils_inet4_ntop (info->address, NULL)); + _LOGD ("announcing address %s", nm_utils_inet4_ntop (info->address, sbuf)); } - } else - nm_assert_not_reached (); + } } static void @@ -430,64 +405,52 @@ destroy_address_info (gpointer data) { AddressInfo *info = (AddressInfo *) data; - g_clear_pointer (&info->channel, g_io_channel_unref); - g_clear_pointer (&info->acd, n_acd_free); - nm_clear_g_source (&info->event_id); + n_acd_probe_free (info->probe); g_slice_free (AddressInfo, info); } /*****************************************************************************/ -static void -nm_acd_manager_init (NMAcdManager *self) -{ - NMAcdManagerPrivate *priv = NM_ACD_MANAGER_GET_PRIVATE (self); - - priv->addresses = g_hash_table_new_full (nm_direct_hash, NULL, - NULL, destroy_address_info); - priv->state = STATE_INIT; -} - NMAcdManager * -nm_acd_manager_new (int ifindex, const guint8 *hwaddr, size_t hwaddr_len) +nm_acd_manager_new (int ifindex, + const guint8 *hwaddr, + guint hwaddr_len, + const NMAcdCallbacks *callbacks, + gpointer user_data) { NMAcdManager *self; - NMAcdManagerPrivate *priv; + g_return_val_if_fail (ifindex > 0, NULL); g_return_val_if_fail (hwaddr, NULL); g_return_val_if_fail (hwaddr_len == ETH_ALEN, NULL); - self = g_object_new (NM_TYPE_ACD_MANAGER, NULL); - priv = NM_ACD_MANAGER_GET_PRIVATE (self); - priv->ifindex = ifindex; - memcpy (priv->hwaddr, hwaddr, ETH_ALEN); + self = g_slice_new0 (NMAcdManager); + + if (callbacks) + self->callbacks = *callbacks; + self->user_data = user_data; + self->addresses = g_hash_table_new_full (nm_direct_hash, NULL, + NULL, destroy_address_info); + self->state = STATE_INIT; + self->ifindex = ifindex; + memcpy (self->hwaddr, hwaddr, ETH_ALEN); return self; } -static void -dispose (GObject *object) +void +nm_acd_manager_free (NMAcdManager *self) { - NMAcdManager *self = NM_ACD_MANAGER (object); - NMAcdManagerPrivate *priv = NM_ACD_MANAGER_GET_PRIVATE (self); + g_return_if_fail (self); - g_clear_pointer (&priv->addresses, g_hash_table_destroy); - - G_OBJECT_CLASS (nm_acd_manager_parent_class)->dispose (object); -} - -static void -nm_acd_manager_class_init (NMAcdManagerClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); + if (self->callbacks.user_data_destroy) + self->callbacks.user_data_destroy (self->user_data); - object_class->dispose = dispose; + nm_clear_pointer (&self->addresses, g_hash_table_destroy); + nm_clear_pointer (&self->channel, g_io_channel_unref); + nm_clear_g_source (&self->event_id); + nm_clear_pointer (&self->acd, n_acd_unref); - signals[PROBE_TERMINATED] = - g_signal_new (NM_ACD_MANAGER_PROBE_TERMINATED, - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_FIRST, - 0, NULL, NULL, NULL, - G_TYPE_NONE, 0); + g_slice_free (NMAcdManager, self); } diff --git a/src/devices/nm-acd-manager.h b/src/devices/nm-acd-manager.h index eeede5da..75884846 100644 --- a/src/devices/nm-acd-manager.h +++ b/src/devices/nm-acd-manager.h @@ -19,25 +19,25 @@ #include <netinet/in.h> -#define NM_TYPE_ACD_MANAGER (nm_acd_manager_get_type ()) -#define NM_ACD_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_ACD_MANAGER, NMAcdManager)) -#define NM_ACD_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_ACD_MANAGER, NMAcdManagerClass)) -#define NM_IS_ACD_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_ACD_MANAGER)) -#define NM_IS_ACD_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_ACD_MANAGER)) -#define NM_ACD_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_ACD_MANAGER, NMAcdManagerClass)) +typedef struct _NMAcdManager NMAcdManager; -#define NM_ACD_MANAGER_PROBE_TERMINATED "probe-terminated" +typedef struct { + void (*probe_terminated_callback) (NMAcdManager *self, + gpointer user_data); + GDestroyNotify user_data_destroy; +} NMAcdCallbacks; -typedef struct _NMAcdManagerClass NMAcdManagerClass; +NMAcdManager *nm_acd_manager_new (int ifindex, + const guint8 *hwaddr, + guint hwaddr_len, + const NMAcdCallbacks *callbacks, + gpointer user_data); -GType nm_acd_manager_get_type (void); +void nm_acd_manager_free (NMAcdManager *self); -NMAcdManager *nm_acd_manager_new (int ifindex, const guint8 *hwaddr, size_t hwaddr_len); -void nm_acd_manager_destroy (NMAcdManager *self); gboolean nm_acd_manager_add_address (NMAcdManager *self, in_addr_t address); gboolean nm_acd_manager_start_probe (NMAcdManager *self, guint timeout); gboolean nm_acd_manager_check_address (NMAcdManager *self, in_addr_t address); void nm_acd_manager_announce_addresses (NMAcdManager *self); -void nm_acd_manager_reset (NMAcdManager *self); #endif /* __NM_ACD_MANAGER__ */ diff --git a/src/devices/nm-device-6lowpan.c b/src/devices/nm-device-6lowpan.c index b6b9157c..40103747 100644 --- a/src/devices/nm-device-6lowpan.c +++ b/src/devices/nm-device-6lowpan.c @@ -110,9 +110,9 @@ create_and_realize (NMDevice *device, GError **error) { const char *iface = nm_device_get_iface (device); - NMPlatformError plerr; NMSetting6Lowpan *s_6lowpan; int parent_ifindex; + int r; s_6lowpan = NM_SETTING_6LOWPAN (nm_connection_get_setting (connection, NM_TYPE_SETTING_6LOWPAN)); g_return_val_if_fail (s_6lowpan, FALSE); @@ -126,13 +126,13 @@ create_and_realize (NMDevice *device, return FALSE; } - plerr = nm_platform_link_6lowpan_add (nm_device_get_platform (device), iface, parent_ifindex, out_plink); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_6lowpan_add (nm_device_get_platform (device), iface, parent_ifindex, out_plink); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create 6lowpan interface '%s' for '%s': %s", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } diff --git a/src/devices/nm-device-bond.c b/src/devices/nm-device-bond.c index 6e7e6ffc..78cba9bb 100644 --- a/src/devices/nm-device-bond.c +++ b/src/devices/nm-device-bond.c @@ -220,7 +220,6 @@ static NMActStageReturn apply_bonding_config (NMDevice *device) { NMDeviceBond *self = NM_DEVICE_BOND (device); - NMConnection *connection; NMSettingBond *s_bond; int ifindex = nm_device_get_ifindex (device); const char *mode_str, *value; @@ -241,10 +240,9 @@ apply_bonding_config (NMDevice *device) * arp_interval doesn't require miimon to be 0 */ - connection = nm_device_get_applied_connection (device); - g_assert (connection); - s_bond = nm_connection_get_setting_bond (connection); - g_assert (s_bond); + s_bond = nm_device_get_applied_setting (device, NM_TYPE_SETTING_BOND); + + g_return_val_if_fail (s_bond, NM_ACT_STAGE_RETURN_FAILURE); mode_str = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_MODE); if (!mode_str) @@ -461,17 +459,17 @@ create_and_realize (NMDevice *device, GError **error) { const char *iface = nm_device_get_iface (device); - NMPlatformError plerr; + int r; g_assert (iface); - plerr = nm_platform_link_bond_add (nm_device_get_platform (device), iface, out_plink); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_bond_add (nm_device_get_platform (device), iface, out_plink); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create bond interface '%s' for '%s': %s", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } return TRUE; diff --git a/src/devices/nm-device-bridge.c b/src/devices/nm-device-bridge.c index e79de95c..4c8921c0 100644 --- a/src/devices/nm-device-bridge.c +++ b/src/devices/nm-device-bridge.c @@ -168,26 +168,52 @@ complete_connection (NMDevice *device, typedef struct { const char *name; const char *sysname; - gboolean default_if_zero; - gboolean user_hz_compensate; + uint nm_min; + uint nm_max; + uint nm_default; + bool default_if_zero; + bool user_hz_compensate; + bool only_with_stp; } Option; static const Option master_options[] = { - { NM_SETTING_BRIDGE_STP, "stp_state", FALSE, FALSE }, - { NM_SETTING_BRIDGE_PRIORITY, "priority", TRUE, FALSE }, - { NM_SETTING_BRIDGE_FORWARD_DELAY, "forward_delay", TRUE, TRUE }, - { NM_SETTING_BRIDGE_HELLO_TIME, "hello_time", TRUE, TRUE }, - { NM_SETTING_BRIDGE_MAX_AGE, "max_age", TRUE, TRUE }, - { NM_SETTING_BRIDGE_AGEING_TIME, "ageing_time", TRUE, TRUE }, - { NM_SETTING_BRIDGE_GROUP_FORWARD_MASK, "group_fwd_mask", TRUE, FALSE }, - { NM_SETTING_BRIDGE_MULTICAST_SNOOPING, "multicast_snooping", FALSE, FALSE }, + { NM_SETTING_BRIDGE_STP, "stp_state", /* this must stay as the first item */ + 0, 1, 1, + FALSE, FALSE, FALSE }, + { NM_SETTING_BRIDGE_PRIORITY, "priority", + 0, G_MAXUINT16, 0x8000, + TRUE, FALSE, TRUE }, + { NM_SETTING_BRIDGE_FORWARD_DELAY, "forward_delay", + 0, NM_BR_MAX_FORWARD_DELAY, 15, + TRUE, TRUE, TRUE}, + { NM_SETTING_BRIDGE_HELLO_TIME, "hello_time", + 0, NM_BR_MAX_HELLO_TIME, 2, + TRUE, TRUE, TRUE }, + { NM_SETTING_BRIDGE_MAX_AGE, "max_age", + 0, NM_BR_MAX_MAX_AGE, 20, + TRUE, TRUE, TRUE }, + { NM_SETTING_BRIDGE_AGEING_TIME, "ageing_time", + NM_BR_MIN_AGEING_TIME, NM_BR_MAX_AGEING_TIME, 300, + TRUE, TRUE, FALSE }, + { NM_SETTING_BRIDGE_GROUP_FORWARD_MASK, "group_fwd_mask", + 0, 0xFFFF, 0, + TRUE, FALSE, FALSE }, + { NM_SETTING_BRIDGE_MULTICAST_SNOOPING, "multicast_snooping", + 0, 1, 1, + FALSE, FALSE, FALSE }, { NULL, NULL } }; static const Option slave_options[] = { - { NM_SETTING_BRIDGE_PORT_PRIORITY, "priority", TRUE, FALSE }, - { NM_SETTING_BRIDGE_PORT_PATH_COST, "path_cost", TRUE, FALSE }, - { NM_SETTING_BRIDGE_PORT_HAIRPIN_MODE, "hairpin_mode", FALSE, FALSE }, + { NM_SETTING_BRIDGE_PORT_PRIORITY, "priority", + 0, NM_BR_PORT_MAX_PRIORITY, NM_BR_PORT_DEF_PRIORITY, + TRUE, FALSE }, + { NM_SETTING_BRIDGE_PORT_PATH_COST, "path_cost", + 0, NM_BR_PORT_MAX_PATH_COST, 100, + TRUE, FALSE }, + { NM_SETTING_BRIDGE_PORT_HAIRPIN_MODE, "hairpin_mode", + 0, 1, 0, + FALSE, FALSE }, { NULL, NULL } }; @@ -275,23 +301,43 @@ update_connection (NMDevice *device, NMConnection *connection) NMSettingBridge *s_bridge = nm_connection_get_setting_bridge (connection); int ifindex = nm_device_get_ifindex (device); const Option *option; + gs_free char *stp = NULL; + int stp_value; if (!s_bridge) { s_bridge = (NMSettingBridge *) nm_setting_bridge_new (); nm_connection_add_setting (connection, (NMSetting *) s_bridge); } - for (option = master_options; option->name; option++) { + option = master_options; + nm_assert (nm_streq (option->sysname, "stp_state")); + + stp = nm_platform_sysctl_master_get_option (nm_device_get_platform (device), ifindex, option->sysname); + stp_value = _nm_utils_ascii_str_to_int64 (stp, 10, option->nm_min, option->nm_max, option->nm_default); + g_object_set (s_bridge, option->name, stp_value, NULL); + option++; + + for (; option->name; option++) { gs_free char *str = nm_platform_sysctl_master_get_option (nm_device_get_platform (device), ifindex, option->sysname); - int value; + uint value; - if (str) { - value = strtol (str, NULL, 10); + if (!stp_value && option->only_with_stp) + continue; + if (str) { /* See comments in set_sysfs_uint() about centiseconds. */ - if (option->user_hz_compensate) + if (option->user_hz_compensate) { + value = _nm_utils_ascii_str_to_int64 (str, 10, + option->nm_min * 100, + option->nm_max * 100, + option->nm_default * 100); value /= 100; - + } else { + value = _nm_utils_ascii_str_to_int64 (str, 10, + option->nm_min, + option->nm_max, + option->nm_default); + } g_object_set (s_bridge, option->name, value, NULL); } else _LOGW (LOGD_BRIDGE, "failed to read bridge setting '%s'", option->sysname); @@ -322,15 +368,22 @@ master_update_slave_connection (NMDevice *device, for (option = slave_options; option->name; option++) { gs_free char *str = nm_platform_sysctl_slave_get_option (nm_device_get_platform (device), ifindex_slave, option->sysname); - int value; + uint value; if (str) { - value = strtol (str, NULL, 10); - /* See comments in set_sysfs_uint() about centiseconds. */ - if (option->user_hz_compensate) + if (option->user_hz_compensate) { + value = _nm_utils_ascii_str_to_int64 (str, 10, + option->nm_min * 100, + option->nm_max * 100, + option->nm_default * 100); value /= 100; - + } else { + value = _nm_utils_ascii_str_to_int64 (str, 10, + option->nm_min, + option->nm_max, + option->nm_default); + } g_object_set (s_port, option->name, value, NULL); } else _LOGW (LOGD_BRIDGE, "failed to read bridge port setting '%s'", option->sysname); @@ -459,7 +512,7 @@ create_and_realize (NMDevice *device, const char *hwaddr; gs_free char *hwaddr_cloned = NULL; guint8 mac_address[NM_UTILS_HWADDR_LEN_MAX]; - NMPlatformError plerr; + int r; nm_assert (iface); @@ -486,17 +539,17 @@ create_and_realize (NMDevice *device, } } - plerr = nm_platform_link_bridge_add (nm_device_get_platform (device), - iface, - hwaddr ? mac_address : NULL, - hwaddr ? ETH_ALEN : 0, - out_plink); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_bridge_add (nm_device_get_platform (device), + iface, + hwaddr ? mac_address : NULL, + hwaddr ? ETH_ALEN : 0, + out_plink); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create bridge interface '%s' for '%s': %s", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } diff --git a/src/devices/nm-device-dummy.c b/src/devices/nm-device-dummy.c index a9059383..eb90456a 100644 --- a/src/devices/nm-device-dummy.c +++ b/src/devices/nm-device-dummy.c @@ -98,19 +98,19 @@ create_and_realize (NMDevice *device, GError **error) { const char *iface = nm_device_get_iface (device); - NMPlatformError plerr; NMSettingDummy *s_dummy; + int r; s_dummy = nm_connection_get_setting_dummy (connection); g_assert (s_dummy); - plerr = nm_platform_link_dummy_add (nm_device_get_platform (device), iface, out_plink); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_dummy_add (nm_device_get_platform (device), iface, out_plink); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create dummy interface '%s' for '%s': %s", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } diff --git a/src/devices/nm-device-ethernet.c b/src/devices/nm-device-ethernet.c index 0fb8fea4..3b6fb35b 100644 --- a/src/devices/nm-device-ethernet.c +++ b/src/devices/nm-device-ethernet.c @@ -556,7 +556,9 @@ build_supplicant_config (NMDeviceEthernet *self, guint32 mtu; connection = nm_device_get_applied_connection (NM_DEVICE (self)); - g_assert (connection); + + g_return_val_if_fail (connection, NULL); + con_uuid = nm_connection_get_uuid (connection); mtu = nm_platform_link_get_mtu (nm_device_get_platform (NM_DEVICE (self)), nm_device_get_ifindex (NM_DEVICE (self))); @@ -790,7 +792,7 @@ link_negotiation_set (NMDevice *device) guint32 speed = 0; guint32 link_speed; - s_wired = (NMSettingWired *) nm_device_get_applied_setting (device, NM_TYPE_SETTING_WIRED); + s_wired = nm_device_get_applied_setting (device, NM_TYPE_SETTING_WIRED); if (s_wired) { autoneg = nm_setting_wired_get_auto_negotiate (s_wired); speed = nm_setting_wired_get_speed (s_wired); @@ -880,8 +882,8 @@ act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *out_failure_reason) delay); g_assert (!priv->pppoe_wait_id); priv->pppoe_wait_id = g_timeout_add_seconds (delay, - pppoe_reconnect_delay, - self); + pppoe_reconnect_delay, + self); return NM_ACT_STAGE_RETURN_POSTPONE; } priv->last_pppoe_time = 0; @@ -900,6 +902,7 @@ nm_8021x_stage2_config (NMDeviceEthernet *self, NMDeviceStateReason *out_failure NMActStageReturn ret = NM_ACT_STAGE_RETURN_FAILURE; connection = nm_device_get_applied_connection (NM_DEVICE (self)); + g_return_val_if_fail (connection, NM_ACT_STAGE_RETURN_FAILURE); security = nm_connection_get_setting_802_1x (connection); @@ -995,10 +998,12 @@ pppoe_stage3_ip4_config_start (NMDeviceEthernet *self, NMDeviceStateReason *out_ NMActRequest *req; GError *err = NULL; - req = nm_device_get_act_request (NM_DEVICE (self)); + req = nm_device_get_act_request (device); + g_return_val_if_fail (req, NM_ACT_STAGE_RETURN_FAILURE); - s_pppoe = (NMSettingPppoe *) nm_device_get_applied_setting ((NMDevice *) self, NM_TYPE_SETTING_PPPOE); + s_pppoe = nm_device_get_applied_setting (device, NM_TYPE_SETTING_PPPOE); + g_return_val_if_fail (s_pppoe, NM_ACT_STAGE_RETURN_FAILURE); priv->ppp_manager = nm_ppp_manager_create (nm_device_get_iface (device), @@ -1069,8 +1074,10 @@ dcb_configure (NMDevice *device) nm_clear_g_source (&priv->dcb_timeout_id); - s_dcb = (NMSettingDcb *) nm_device_get_applied_setting (device, NM_TYPE_SETTING_DCB); - g_assert (s_dcb); + s_dcb = nm_device_get_applied_setting (device, NM_TYPE_SETTING_DCB); + + g_return_val_if_fail (s_dcb, FALSE); + if (!nm_dcb_setup (nm_device_get_iface (device), s_dcb, &error)) { _LOGW (LOGD_DCB, "Activation: (ethernet) failed to enable DCB/FCoE: %s", error->message); @@ -1199,7 +1206,8 @@ wake_on_lan_enable (NMDevice *device) NMSettingWired *s_wired; const char *password = NULL; - s_wired = (NMSettingWired *) nm_device_get_applied_setting (device, NM_TYPE_SETTING_WIRED); + s_wired = nm_device_get_applied_setting (device, NM_TYPE_SETTING_WIRED); + if (s_wired) { wol = nm_setting_wired_get_wake_on_lan (s_wired); password = nm_setting_wired_get_wake_on_lan_password (s_wired); @@ -1208,7 +1216,7 @@ wake_on_lan_enable (NMDevice *device) } wol = nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA, - "ethernet.wake-on-lan", + NM_CON_DEFAULT ("ethernet.wake-on-lan"), device, NM_SETTING_WIRED_WAKE_ON_LAN_NONE, G_MAXINT32, @@ -1240,8 +1248,8 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *out_failure_reason) NMActStageReturn ret = NM_ACT_STAGE_RETURN_SUCCESS; NMSettingDcb *s_dcb; - s_con = NM_SETTING_CONNECTION (nm_device_get_applied_setting (device, - NM_TYPE_SETTING_CONNECTION)); + s_con = nm_device_get_applied_setting (device, NM_TYPE_SETTING_CONNECTION); + g_return_val_if_fail (s_con, NM_ACT_STAGE_RETURN_FAILURE); nm_clear_g_source (&priv->dcb_timeout_id); @@ -1254,8 +1262,8 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *out_failure_reason) if (!strcmp (connection_type, NM_SETTING_WIRED_SETTING_NAME)) { NMSetting8021x *security; - security = (NMSetting8021x *) nm_device_get_applied_setting (device, - NM_TYPE_SETTING_802_1X); + security = nm_device_get_applied_setting (device, NM_TYPE_SETTING_802_1X); + if (security) { /* FIXME: for now 802.1x is mutually exclusive with DCB */ return nm_8021x_stage2_config (self, out_failure_reason); @@ -1265,7 +1273,7 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *out_failure_reason) wake_on_lan_enable (device); /* DCB and FCoE setup */ - s_dcb = (NMSettingDcb *) nm_device_get_applied_setting (device, NM_TYPE_SETTING_DCB); + s_dcb = nm_device_get_applied_setting (device, NM_TYPE_SETTING_DCB); if (s_dcb) { /* lldpad really really wants the carrier to be up */ if (nm_platform_link_is_connected (nm_device_get_platform (device), nm_device_get_ifindex (device))) { @@ -1288,7 +1296,7 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *out_failure_reason) NM_SETTING_PPPOE_SETTING_NAME)) { NMSettingPpp *s_ppp; - s_ppp = (NMSettingPpp *) nm_device_get_applied_setting (device, NM_TYPE_SETTING_PPP); + s_ppp = nm_device_get_applied_setting (device, NM_TYPE_SETTING_PPP); if (s_ppp) { guint32 mtu = 0, mru = 0, mxu; @@ -1316,7 +1324,8 @@ act_stage3_ip4_config_start (NMDevice *device, NMSettingConnection *s_con; const char *connection_type; - s_con = NM_SETTING_CONNECTION (nm_device_get_applied_setting (device, NM_TYPE_SETTING_CONNECTION)); + s_con = nm_device_get_applied_setting (device, NM_TYPE_SETTING_CONNECTION); + g_return_val_if_fail (s_con, NM_ACT_STAGE_RETURN_FAILURE); connection_type = nm_setting_connection_get_connection_type (s_con); @@ -1347,7 +1356,7 @@ deactivate (NMDevice *device) nm_clear_g_source (&priv->pppoe_wait_id); if (priv->ppp_manager) { - nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL); + nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL, NULL); g_clear_object (&priv->ppp_manager); } @@ -1358,7 +1367,7 @@ deactivate (NMDevice *device) priv->dcb_handle_carrier_changes = FALSE; /* Tear down DCB/FCoE if it was enabled */ - s_dcb = (NMSettingDcb *) nm_device_get_applied_setting (device, NM_TYPE_SETTING_DCB); + s_dcb = nm_device_get_applied_setting (device, NM_TYPE_SETTING_DCB); if (s_dcb) { if (!nm_dcb_cleanup (nm_device_get_iface (device), &error)) { _LOGW (LOGD_DEVICE | LOGD_PLATFORM, "failed to disable DCB/FCoE: %s", @@ -1446,7 +1455,6 @@ new_default_connection (NMDevice *self) const char *uprop = "0"; gs_free char *defname = NULL; gs_free char *uuid = NULL; - gs_free char *machine_id = NULL; guint i, n_connections; if (nm_config_get_no_auto_default_for_device (nm_config_get (), self)) @@ -1470,12 +1478,10 @@ new_default_connection (NMDevice *self) if (!defname) return NULL; - machine_id = nm_utils_machine_id_read (); - /* Create a stable UUID. The UUID is also the Network_ID for stable-privacy addr-gen-mode, * thus when it changes we will also generate different IPv6 addresses. */ uuid = _nm_utils_uuid_generate_from_strings ("default-wired", - machine_id ?: "", + nm_utils_machine_id_str (), defname, perm_hw_addr, NULL); diff --git a/src/devices/nm-device-infiniband.c b/src/devices/nm-device-infiniband.c index 41fac157..4db7d8a7 100644 --- a/src/devices/nm-device-infiniband.c +++ b/src/devices/nm-device-infiniband.c @@ -22,6 +22,7 @@ #include "nm-device-infiniband.h" +#include <linux/if.h> #include <linux/if_infiniband.h> #include "NetworkManagerUtils.h" @@ -86,7 +87,8 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason) if (ret != NM_ACT_STAGE_RETURN_SUCCESS) return ret; - s_infiniband = (NMSettingInfiniband *) nm_device_get_applied_setting (device, NM_TYPE_SETTING_INFINIBAND); + s_infiniband = nm_device_get_applied_setting (device, NM_TYPE_SETTING_INFINIBAND); + g_return_val_if_fail (s_infiniband, NM_ACT_STAGE_RETURN_FAILURE); transport_mode = nm_setting_infiniband_get_transport_mode (s_infiniband); @@ -233,7 +235,7 @@ create_and_realize (NMDevice *device, { NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE ((NMDeviceInfiniband *) device); NMSettingInfiniband *s_infiniband; - NMPlatformError plerr; + int r; s_infiniband = nm_connection_get_setting_infiniband (connection); g_assert (s_infiniband); @@ -267,13 +269,13 @@ create_and_realize (NMDevice *device, return FALSE; } - plerr = nm_platform_link_infiniband_add (nm_device_get_platform (device), priv->parent_ifindex, priv->p_key, out_plink); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_infiniband_add (nm_device_get_platform (device), priv->parent_ifindex, priv->p_key, out_plink); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create InfiniBand P_Key interface '%s' for '%s': %s", nm_device_get_iface (device), nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } @@ -285,7 +287,7 @@ static gboolean unrealize (NMDevice *device, GError **error) { NMDeviceInfinibandPrivate *priv; - NMPlatformError plerr; + int r; g_return_val_if_fail (NM_IS_DEVICE_INFINIBAND (device), FALSE); @@ -297,12 +299,12 @@ unrealize (NMDevice *device, GError **error) return FALSE; } - plerr = nm_platform_link_infiniband_delete (nm_device_get_platform (device), priv->parent_ifindex, priv->p_key); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_infiniband_delete (nm_device_get_platform (device), priv->parent_ifindex, priv->p_key); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to remove InfiniBand P_Key interface '%s': %s", nm_device_get_iface (device), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } diff --git a/src/devices/nm-device-ip-tunnel.c b/src/devices/nm-device-ip-tunnel.c index 568403c6..ca5f9c3f 100644 --- a/src/devices/nm-device-ip-tunnel.c +++ b/src/devices/nm-device-ip-tunnel.c @@ -27,6 +27,7 @@ #include <linux/if.h> #include <linux/ip.h> #include <linux/if_tunnel.h> +#include <linux/ip6_tunnel.h> #include "nm-device-private.h" #include "nm-manager.h" @@ -328,28 +329,28 @@ clear: if (!address_equal_pn (AF_INET, priv->local, &local4)) { g_clear_pointer (&priv->local, g_free); if (local4) - priv->local = g_strdup (nm_utils_inet4_ntop (local4, NULL)); + priv->local = nm_utils_inet4_ntop_dup (local4); _notify (self, PROP_LOCAL); } if (!address_equal_pn (AF_INET, priv->remote, &remote4)) { g_clear_pointer (&priv->remote, g_free); if (remote4) - priv->remote = g_strdup (nm_utils_inet4_ntop (remote4, NULL)); + priv->remote = nm_utils_inet4_ntop_dup (remote4); _notify (self, PROP_REMOTE); } } else { if (!address_equal_pn (AF_INET6, priv->local, &local6)) { g_clear_pointer (&priv->local, g_free); if (memcmp (&local6, &in6addr_any, sizeof (in6addr_any))) - priv->local = g_strdup (nm_utils_inet6_ntop (&local6, NULL)); + priv->local = nm_utils_inet6_ntop_dup (&local6); _notify (self, PROP_LOCAL); } if (!address_equal_pn (AF_INET6, priv->remote, &remote6)) { g_clear_pointer (&priv->remote, g_free); if (memcmp (&remote6, &in6addr_any, sizeof (in6addr_any))) - priv->remote = g_strdup (nm_utils_inet6_ntop (&remote6, NULL)); + priv->remote = nm_utils_inet6_ntop_dup (&remote6); _notify (self, PROP_REMOTE); } } @@ -659,7 +660,6 @@ create_and_realize (NMDevice *device, { const char *iface = nm_device_get_iface (device); NMSettingIPTunnel *s_ip_tunnel; - NMPlatformError plerr; NMPlatformLnkGre lnk_gre = { }; NMPlatformLnkSit lnk_sit = { }; NMPlatformLnkIpIp lnk_ipip = { }; @@ -667,6 +667,7 @@ create_and_realize (NMDevice *device, const char *str; gint64 val; NMIPTunnelMode mode; + int r; s_ip_tunnel = nm_connection_get_setting_ip_tunnel (connection); g_assert (s_ip_tunnel); @@ -712,13 +713,13 @@ create_and_realize (NMDevice *device, lnk_gre.output_flags = NM_GRE_KEY; } - plerr = nm_platform_link_gre_add (nm_device_get_platform (device), iface, &lnk_gre, out_plink); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_gre_add (nm_device_get_platform (device), iface, &lnk_gre, out_plink); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create GRE interface '%s' for '%s': %s", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } break; @@ -738,13 +739,13 @@ create_and_realize (NMDevice *device, lnk_sit.tos = nm_setting_ip_tunnel_get_tos (s_ip_tunnel); lnk_sit.path_mtu_discovery = nm_setting_ip_tunnel_get_path_mtu_discovery (s_ip_tunnel); - plerr = nm_platform_link_sit_add (nm_device_get_platform (device), iface, &lnk_sit, out_plink); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_sit_add (nm_device_get_platform (device), iface, &lnk_sit, out_plink); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create SIT interface '%s' for '%s': %s", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } break; @@ -764,13 +765,13 @@ create_and_realize (NMDevice *device, lnk_ipip.tos = nm_setting_ip_tunnel_get_tos (s_ip_tunnel); lnk_ipip.path_mtu_discovery = nm_setting_ip_tunnel_get_path_mtu_discovery (s_ip_tunnel); - plerr = nm_platform_link_ipip_add (nm_device_get_platform (device), iface, &lnk_ipip, out_plink); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_ipip_add (nm_device_get_platform (device), iface, &lnk_ipip, out_plink); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create IPIP interface '%s' for '%s': %s", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } break; @@ -819,21 +820,21 @@ create_and_realize (NMDevice *device, lnk_ip6tnl.is_gre = TRUE; lnk_ip6tnl.is_tap = (mode == NM_IP_TUNNEL_MODE_IP6GRETAP); - plerr = nm_platform_link_ip6gre_add (nm_device_get_platform (device), - iface, &lnk_ip6tnl, out_plink); + r = nm_platform_link_ip6gre_add (nm_device_get_platform (device), + iface, &lnk_ip6tnl, out_plink); } else { lnk_ip6tnl.proto = nm_setting_ip_tunnel_get_mode (s_ip_tunnel) == NM_IP_TUNNEL_MODE_IPIP6 ? IPPROTO_IPIP : IPPROTO_IPV6; - plerr = nm_platform_link_ip6tnl_add (nm_device_get_platform (device), - iface, &lnk_ip6tnl, out_plink); + r = nm_platform_link_ip6tnl_add (nm_device_get_platform (device), + iface, &lnk_ip6tnl, out_plink); } - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create IPv6 tunnel interface '%s' for '%s': %s", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } break; diff --git a/src/devices/nm-device-macsec.c b/src/devices/nm-device-macsec.c index 8ea4c8b5..1a6b64a4 100644 --- a/src/devices/nm-device-macsec.c +++ b/src/devices/nm-device-macsec.c @@ -210,7 +210,7 @@ update_properties (NMDevice *device) static NMSupplicantConfig * build_supplicant_config (NMDeviceMacsec *self, GError **error) { - NMSupplicantConfig *config = NULL; + gs_unref_object NMSupplicantConfig *config = NULL; NMSettingMacsec *s_macsec; NMSetting8021x *s_8021x; NMConnection *connection; @@ -218,19 +218,21 @@ build_supplicant_config (NMDeviceMacsec *self, GError **error) guint32 mtu; connection = nm_device_get_applied_connection (NM_DEVICE (self)); - g_assert (connection); + + g_return_val_if_fail (connection, NULL); + con_uuid = nm_connection_get_uuid (connection); mtu = nm_platform_link_get_mtu (nm_device_get_platform (NM_DEVICE (self)), nm_device_get_ifindex (NM_DEVICE (self))); config = nm_supplicant_config_new (FALSE, FALSE); - s_macsec = (NMSettingMacsec *) - nm_device_get_applied_setting (NM_DEVICE (self), NM_TYPE_SETTING_MACSEC); + s_macsec = nm_device_get_applied_setting (NM_DEVICE (self), NM_TYPE_SETTING_MACSEC); + + g_return_val_if_fail (s_macsec, NULL); if (!nm_supplicant_config_add_setting_macsec (config, s_macsec, error)) { g_prefix_error (error, "macsec-setting: "); - g_object_unref (config); return NULL; } @@ -238,11 +240,11 @@ build_supplicant_config (NMDeviceMacsec *self, GError **error) s_8021x = nm_connection_get_setting_802_1x (connection); if (!nm_supplicant_config_add_setting_8021x (config, s_8021x, con_uuid, mtu, TRUE, error)) { g_prefix_error (error, "802-1x-setting: "); - g_clear_object (&config); + return NULL; } } - return config; + return g_steal_pointer (&config); } static void @@ -588,6 +590,7 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *out_failure_reason) const char *setting_name; connection = nm_device_get_applied_connection (NM_DEVICE (self)); + g_return_val_if_fail (connection, NM_ACT_STAGE_RETURN_FAILURE); if (!priv->supplicant.mgr) @@ -654,7 +657,6 @@ create_and_realize (NMDevice *device, GError **error) { const char *iface = nm_device_get_iface (device); - NMPlatformError plerr; NMSettingMacsec *s_macsec; NMPlatformLnkMacsec lnk = { }; int parent_ifindex; @@ -666,6 +668,7 @@ create_and_realize (NMDevice *device, } s; guint64 u; } sci; + int r; s_macsec = nm_connection_get_setting_macsec (connection); g_assert (s_macsec); @@ -694,13 +697,13 @@ create_and_realize (NMDevice *device, parent_ifindex = nm_device_get_ifindex (parent); g_warn_if_fail (parent_ifindex > 0); - plerr = nm_platform_link_macsec_add (nm_device_get_platform (device), iface, parent_ifindex, &lnk, out_plink); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_macsec_add (nm_device_get_platform (device), iface, parent_ifindex, &lnk, out_plink); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create macsec interface '%s' for '%s': %s", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } diff --git a/src/devices/nm-device-macvlan.c b/src/devices/nm-device-macvlan.c index ff386c82..bb629713 100644 --- a/src/devices/nm-device-macvlan.c +++ b/src/devices/nm-device-macvlan.c @@ -23,6 +23,7 @@ #include "nm-device-macvlan.h" #include <string.h> +#include <linux/if_link.h> #include "nm-device-private.h" #include "settings/nm-settings.h" @@ -226,10 +227,10 @@ create_and_realize (NMDevice *device, GError **error) { const char *iface = nm_device_get_iface (device); - NMPlatformError plerr; NMSettingMacvlan *s_macvlan; NMPlatformLnkMacvlan lnk = { }; int parent_ifindex; + int r; s_macvlan = nm_connection_get_setting_macvlan (connection); g_return_val_if_fail (s_macvlan, FALSE); @@ -254,14 +255,14 @@ create_and_realize (NMDevice *device, lnk.no_promisc = !nm_setting_macvlan_get_promiscuous (s_macvlan); lnk.tap = nm_setting_macvlan_get_tap (s_macvlan); - plerr = nm_platform_link_macvlan_add (nm_device_get_platform (device), iface, parent_ifindex, &lnk, out_plink); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_macvlan_add (nm_device_get_platform (device), iface, parent_ifindex, &lnk, out_plink); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create %s interface '%s' for '%s': %s", lnk.tap ? "macvtap" : "macvlan", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } diff --git a/src/devices/nm-device-ppp.c b/src/devices/nm-device-ppp.c index 74b4d710..2f565d4b 100644 --- a/src/devices/nm-device-ppp.c +++ b/src/devices/nm-device-ppp.c @@ -125,10 +125,12 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *out_failure_reason) NMActRequest *req; GError *error = NULL; - req = nm_device_get_act_request (NM_DEVICE (self)); + req = nm_device_get_act_request (device); + g_return_val_if_fail (req, NM_ACT_STAGE_RETURN_FAILURE); - s_pppoe = (NMSettingPppoe *) nm_device_get_applied_setting ((NMDevice *) self, NM_TYPE_SETTING_PPPOE); + s_pppoe = nm_device_get_applied_setting (device, NM_TYPE_SETTING_PPPOE); + g_return_val_if_fail (s_pppoe, NM_ACT_STAGE_RETURN_FAILURE); g_clear_object (&priv->ip4_config); @@ -221,7 +223,7 @@ deactivate (NMDevice *device) NMDevicePppPrivate *priv = NM_DEVICE_PPP_GET_PRIVATE (self); if (priv->ppp_manager) { - nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL); + nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL, NULL); g_clear_object (&priv->ppp_manager); } } diff --git a/src/devices/nm-device-private.h b/src/devices/nm-device-private.h index 66c715de..6c0f473d 100644 --- a/src/devices/nm-device-private.h +++ b/src/devices/nm-device-private.h @@ -34,7 +34,7 @@ enum NMActStageReturn { NM_ACT_STAGE_RETURN_IP_DONE, /* IP config stage is done (state IP_DONE), For the ip-config stage, this is similar to NM_ACT_STAGE_RETURN_SUCCESS, except that no - IP config should be commited. */ + IP config should be committed. */ NM_ACT_STAGE_RETURN_IP_FAIL, /* IP config stage failed (state IP_FAIL), activation may proceed */ }; @@ -111,9 +111,12 @@ void nm_device_set_wwan_ip6_config (NMDevice *device, NMIP6Config *config); gboolean nm_device_hw_addr_is_explict (NMDevice *device); -void nm_device_ip_method_failed (NMDevice *self, int family, NMDeviceStateReason reason); +void nm_device_ip_method_failed (NMDevice *self, int addr_family, NMDeviceStateReason reason); -gboolean nm_device_ipv6_sysctl_set (NMDevice *self, const char *property, const char *value); +gboolean nm_device_sysctl_ip_conf_set (NMDevice *self, + int addr_family, + const char *property, + const char *value); /*****************************************************************************/ @@ -147,9 +150,9 @@ void nm_device_commit_mtu (NMDevice *self); ) gboolean _nm_device_hash_check_invalid_keys (GHashTable *hash, const char *setting_name, - GError **error, const char **whitelist); + GError **error, const char *const*whitelist); #define nm_device_hash_check_invalid_keys(hash, setting_name, error, ...) \ - _nm_device_hash_check_invalid_keys (hash, setting_name, error, ((const char *[]) { __VA_ARGS__, NULL })) + _nm_device_hash_check_invalid_keys (hash, setting_name, error, NM_MAKE_STRV (__VA_ARGS__)) gboolean nm_device_match_parent (NMDevice *device, const char *parent); gboolean nm_device_match_parent_hwaddr (NMDevice *device, diff --git a/src/devices/nm-device-tun.c b/src/devices/nm-device-tun.c index 0f76b23a..3fe3dfd4 100644 --- a/src/devices/nm-device-tun.c +++ b/src/devices/nm-device-tun.c @@ -231,9 +231,10 @@ create_and_realize (NMDevice *device, { const char *iface = nm_device_get_iface (device); NMPlatformLnkTun props = { }; - NMPlatformError plerr; NMSettingTun *s_tun; - gint64 owner, group; + gint64 owner; + gint64 group; + int r; s_tun = nm_connection_get_setting_tun (connection); g_return_val_if_fail (s_tun, FALSE); @@ -261,17 +262,17 @@ create_and_realize (NMDevice *device, props.multi_queue = nm_setting_tun_get_multi_queue (s_tun); props.persist = TRUE; - plerr = nm_platform_link_tun_add (nm_device_get_platform (device), - iface, - &props, - out_plink, - NULL); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_tun_add (nm_device_get_platform (device), + iface, + &props, + out_plink, + NULL); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create TUN/TAP interface '%s' for '%s': %s", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } diff --git a/src/devices/nm-device-vlan.c b/src/devices/nm-device-vlan.c index b7f0c4e7..ace6a24b 100644 --- a/src/devices/nm-device-vlan.c +++ b/src/devices/nm-device-vlan.c @@ -241,7 +241,7 @@ create_and_realize (NMDevice *device, NMSettingVlan *s_vlan; int parent_ifindex; guint vlan_id; - NMPlatformError plerr; + int r; s_vlan = nm_connection_get_setting_vlan (connection); g_assert (s_vlan); @@ -271,18 +271,18 @@ create_and_realize (NMDevice *device, vlan_id = nm_setting_vlan_get_id (s_vlan); - plerr = nm_platform_link_vlan_add (nm_device_get_platform (device), - iface, - parent_ifindex, - vlan_id, - nm_setting_vlan_get_flags (s_vlan), - out_plink); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_vlan_add (nm_device_get_platform (device), + iface, + parent_ifindex, + vlan_id, + nm_setting_vlan_get_flags (s_vlan), + out_plink); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create VLAN interface '%s' for '%s': %s", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } @@ -493,7 +493,7 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason) parent_mtu_maybe_changed (parent_device, NULL, device); } - s_vlan = (NMSettingVlan *) nm_device_get_applied_setting (device, NM_TYPE_SETTING_VLAN); + s_vlan = nm_device_get_applied_setting (device, NM_TYPE_SETTING_VLAN); if (s_vlan) { gs_free NMVlanQosMapping *ingress_map = NULL; gs_free NMVlanQosMapping *egress_map = NULL; diff --git a/src/devices/nm-device-vxlan.c b/src/devices/nm-device-vxlan.c index c34f4142..50730320 100644 --- a/src/devices/nm-device-vxlan.c +++ b/src/devices/nm-device-vxlan.c @@ -34,6 +34,7 @@ #include "settings/nm-settings.h" #include "nm-act-request.h" #include "nm-ip4-config.h" +#include "nm-core-internal.h" #include "nm-device-logging.h" _LOG_DECLARE_SELF(NMDeviceVxlan); @@ -170,11 +171,11 @@ create_and_realize (NMDevice *device, GError **error) { const char *iface = nm_device_get_iface (device); - NMPlatformError plerr; NMPlatformLnkVxlan props = { }; NMSettingVxlan *s_vxlan; const char *str; int ret; + int r; s_vxlan = nm_connection_get_setting_vxlan (connection); g_assert (s_vxlan); @@ -213,13 +214,13 @@ create_and_realize (NMDevice *device, props.l2miss = nm_setting_vxlan_get_l2_miss (s_vxlan); props.l3miss = nm_setting_vxlan_get_l3_miss (s_vxlan); - plerr = nm_platform_link_vxlan_add (nm_device_get_platform (device), iface, &props, out_plink); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_vxlan_add (nm_device_get_platform (device), iface, &props, out_plink); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create VXLAN interface '%s' for '%s': %s", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } @@ -386,6 +387,7 @@ update_connection (NMDevice *device, NMConnection *connection) { NMDeviceVxlanPrivate *priv = NM_DEVICE_VXLAN_GET_PRIVATE ((NMDeviceVxlan *) device); NMSettingVxlan *s_vxlan = nm_connection_get_setting_vxlan (connection); + char sbuf[NM_UTILS_INET_ADDRSTRLEN]; if (!s_vxlan) { s_vxlan = (NMSettingVxlan *) nm_setting_vxlan_new (); @@ -404,11 +406,11 @@ update_connection (NMDevice *device, NMConnection *connection) if (!address_matches (nm_setting_vxlan_get_remote (s_vxlan), priv->props.group, &priv->props.group6)) { if (priv->props.group) { g_object_set (s_vxlan, NM_SETTING_VXLAN_REMOTE, - nm_utils_inet4_ntop (priv->props.group, NULL), + nm_utils_inet4_ntop (priv->props.group, sbuf), NULL); } else { g_object_set (s_vxlan, NM_SETTING_VXLAN_REMOTE, - nm_utils_inet6_ntop (&priv->props.group6, NULL), + nm_utils_inet6_ntop (&priv->props.group6, sbuf), NULL); } } @@ -416,11 +418,11 @@ update_connection (NMDevice *device, NMConnection *connection) if (!address_matches (nm_setting_vxlan_get_local (s_vxlan), priv->props.local, &priv->props.local6)) { if (priv->props.local) { g_object_set (s_vxlan, NM_SETTING_VXLAN_LOCAL, - nm_utils_inet4_ntop (priv->props.local, NULL), + nm_utils_inet4_ntop (priv->props.local, sbuf), NULL); } else if (memcmp (&priv->props.local6, &in6addr_any, sizeof (in6addr_any))) { g_object_set (s_vxlan, NM_SETTING_VXLAN_LOCAL, - nm_utils_inet6_ntop (&priv->props.local6, NULL), + nm_utils_inet6_ntop (&priv->props.local6, sbuf), NULL); } } @@ -510,15 +512,15 @@ get_property (GObject *object, guint prop_id, break; case PROP_GROUP: if (priv->props.group) - g_value_set_string (value, nm_utils_inet4_ntop (priv->props.group, NULL)); + g_value_take_string (value, nm_utils_inet4_ntop_dup (priv->props.group)); else if (!IN6_IS_ADDR_UNSPECIFIED (&priv->props.group6)) - g_value_set_string (value, nm_utils_inet6_ntop (&priv->props.group6, NULL)); + g_value_take_string (value, nm_utils_inet6_ntop_dup (&priv->props.group6)); break; case PROP_LOCAL: if (priv->props.local) - g_value_set_string (value, nm_utils_inet4_ntop (priv->props.local, NULL)); + g_value_take_string (value, nm_utils_inet4_ntop_dup (priv->props.local)); else if (!IN6_IS_ADDR_UNSPECIFIED (&priv->props.local6)) - g_value_set_string (value, nm_utils_inet6_ntop (&priv->props.local6, NULL)); + g_value_take_string (value, nm_utils_inet6_ntop_dup (&priv->props.local6)); break; case PROP_TOS: g_value_set_uchar (value, priv->props.tos); diff --git a/src/devices/nm-device-wpan.c b/src/devices/nm-device-wpan.c index f910b0b8..c2fa7f4e 100644 --- a/src/devices/nm-device-wpan.c +++ b/src/devices/nm-device-wpan.c @@ -25,6 +25,7 @@ #include <stdlib.h> #include <string.h> #include <sys/types.h> +#include <linux/if.h> #include "nm-act-request.h" #include "nm-device-private.h" @@ -118,11 +119,11 @@ static NMActStageReturn act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason) { NMDeviceWpan *self = NM_DEVICE_WPAN (device); - NMConnection *connection; NMSettingWpan *s_wpan; NMPlatform *platform; guint16 pan_id; guint16 short_address; + gint16 page, channel; int ifindex; const guint8 *hwaddr; gsize hwaddr_len = 0; @@ -138,12 +139,11 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason) g_return_val_if_fail (platform, NM_ACT_STAGE_RETURN_FAILURE); ifindex = nm_device_get_ifindex (device); + g_return_val_if_fail (ifindex > 0, NM_ACT_STAGE_RETURN_FAILURE); - connection = nm_device_get_applied_connection (device); - g_return_val_if_fail (connection, NM_ACT_STAGE_RETURN_FAILURE); + s_wpan = nm_device_get_applied_setting (device, NM_TYPE_SETTING_WPAN); - s_wpan = NM_SETTING_WPAN (nm_connection_get_setting (connection, NM_TYPE_SETTING_WPAN)); g_return_val_if_fail (s_wpan, NM_ACT_STAGE_RETURN_FAILURE); hwaddr = nm_platform_link_get_address (platform, ifindex, &hwaddr_len); @@ -182,9 +182,18 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason) } } + channel = nm_setting_wpan_get_channel (s_wpan); + if (channel != NM_SETTING_WPAN_CHANNEL_DEFAULT) { + page = nm_setting_wpan_get_page (s_wpan); + if (!nm_platform_wpan_set_channel (platform, ifindex, page, channel)) { + _LOGW (LOGD_DEVICE, "unable to set the channel"); + goto out; + } + } + ret = NM_ACT_STAGE_RETURN_SUCCESS; out: - nm_device_bring_up (device, TRUE, NULL); + nm_device_bring_up (device, TRUE, NULL); if (lowpan_device) nm_device_bring_up (lowpan_device, TRUE, NULL); diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index c5a152d8..9ded911c 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -37,7 +37,6 @@ #include <linux/if_arp.h> #include <linux/rtnetlink.h> #include <linux/pkt_sched.h> -#include <uuid/uuid.h> #include "nm-utils/nm-dedup-multi.h" #include "nm-utils/nm-random-utils.h" @@ -68,6 +67,7 @@ #include "settings/nm-settings.h" #include "nm-setting-ethtool.h" #include "nm-auth-utils.h" +#include "nm-keep-alive.h" #include "nm-netns.h" #include "nm-dispatcher.h" #include "nm-config.h" @@ -175,6 +175,7 @@ struct _NMDeviceConnectivityHandle { bool is_periodic:1; bool is_periodic_bump:1; bool is_periodic_bump_on_complete:1; + int addr_family; }; typedef struct { @@ -196,7 +197,6 @@ enum { REMOVED, RECHECK_AUTO_ACTIVATE, RECHECK_ASSUME, - CONNECTIVITY_CHANGED, LAST_SIGNAL, }; static guint signals[LAST_SIGNAL] = { 0 }; @@ -242,7 +242,8 @@ NM_GOBJECT_PROPERTIES_DEFINE (NMDevice, PROP_REFRESH_RATE_MS, PROP_TX_BYTES, PROP_RX_BYTES, - PROP_CONNECTIVITY, + PROP_IP4_CONNECTIVITY, + PROP_IP6_CONNECTIVITY, ); typedef struct _NMDevicePrivate { @@ -386,13 +387,15 @@ typedef struct _NMDevicePrivate { bool v4_route_table_initialized:1; bool v6_route_table_initialized:1; - NMDeviceAutoconnectBlockedFlags autoconnect_blocked_flags:4; + NMDeviceAutoconnectBlockedFlags autoconnect_blocked_flags:5; bool is_enslaved:1; bool master_ready_handled:1; bool ipv6ll_handle:1; /* TRUE if NM handles the device's IPv6LL address */ bool ipv6ll_has:1; + bool ndisc_started:1; + bool device_link_changed_down:1; /* Generic DHCP stuff */ char * dhcp_anycast_address; @@ -455,9 +458,6 @@ typedef struct _NMDevicePrivate { AppliedConfig wwan_ip_config_x[2]; }; - bool v4_has_shadowed_routes; - const char *ip4_rp_filter; - /* DHCPv4 tracking */ struct { NMDhcpClient * client; @@ -555,24 +555,24 @@ typedef struct _NMDevicePrivate { NMLldpListener *lldp_listener; NMConnectivity *concheck_mgr; + CList concheck_lst_head; + struct { + /* if periodic checks are enabled, this is the source id for the next check. */ + guint p_cur_id; - /* if periodic checks are enabled, this is the source id for the next check. */ - guint concheck_p_cur_id; - - /* the currently configured max periodic interval. */ - guint concheck_p_max_interval; - - /* the current interval. If we are probing, the interval might be lower - * then the configured max interval. */ - guint concheck_p_cur_interval; + /* the currently configured max periodic interval. */ + guint p_max_interval; - /* the timestamp, when we last scheduled the timer concheck_p_cur_id with current interval - * concheck_p_cur_interval. */ - gint64 concheck_p_cur_basetime_ns; + /* the current interval. If we are probing, the interval might be lower + * then the configured max interval. */ + guint p_cur_interval; - NMConnectivityState connectivity_state; + /* the timestamp, when we last scheduled the timer p_cur_id with current interval + * p_cur_interval. */ + gint64 p_cur_basetime_ns; - CList concheck_lst_head; + NMConnectivityState state; + } concheck_x[2]; guint check_delete_unrealized_id; @@ -643,7 +643,10 @@ static void _set_mtu (NMDevice *self, guint32 mtu); static void _commit_mtu (NMDevice *self, const NMIP4Config *config); static void _cancel_activation (NMDevice *self); -static void concheck_update_state (NMDevice *self, NMConnectivityState state, gboolean is_periodic); +static void concheck_update_state (NMDevice *self, + int addr_family, + NMConnectivityState state, + gboolean is_periodic); /*****************************************************************************/ @@ -741,8 +744,7 @@ NM_UTILS_LOOKUP_STR_DEFINE (nm_device_state_reason_to_str, NMDeviceStateReason, NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SRIOV_CONFIGURATION_FAILED, "sriov-configuration-failed"), ); -#define reason_to_string(reason) \ - NM_UTILS_LOOKUP_STR (nm_device_state_reason_to_str, reason) +#define reason_to_string_a(reason) NM_UTILS_LOOKUP_STR_A (nm_device_state_reason_to_str, reason) NM_UTILS_LOOKUP_STR_DEFINE_STATIC (mtu_source_to_str, NMDeviceMtuSource, NM_UTILS_LOOKUP_DEFAULT_NM_ASSERT ("unknown"), @@ -1116,8 +1118,8 @@ init_ip_config_dns_priority (NMDevice *self, NMIPConfig *config) int priority; property = (nm_ip_config_get_addr_family (config) == AF_INET) - ? "ipv4.dns-priority" - : "ipv6.dns-priority"; + ? NM_CON_DEFAULT ("ipv4.dns-priority") + : NM_CON_DEFAULT ("ipv6.dns-priority"); priority = nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA, property, @@ -1130,96 +1132,85 @@ init_ip_config_dns_priority (NMDevice *self, NMIPConfig *config) /*****************************************************************************/ -static gboolean -nm_device_ipv4_sysctl_set (NMDevice *self, const char *property, const char *value) +static char * +nm_device_sysctl_ip_conf_get (NMDevice *self, + int addr_family, + const char *property) { - NMPlatform *platform = nm_device_get_platform (self); - gs_free char *value_to_free = NULL; - const char *value_to_set; - char buf[NM_UTILS_SYSCTL_IP_CONF_PATH_BUFSIZE]; - - if (!nm_device_get_ip_ifindex (self)) - return FALSE; + const char *ifname; - if (value) { - value_to_set = value; - } else { - /* Set to a default value when we've got a NULL @value. */ - value_to_free = nm_platform_sysctl_get (platform, - NMP_SYSCTL_PATHID_ABSOLUTE (nm_utils_sysctl_ip_conf_path (AF_INET, buf, "default", property))); - value_to_set = value_to_free; - } + nm_assert_addr_family (addr_family); - return nm_platform_sysctl_set (platform, - NMP_SYSCTL_PATHID_ABSOLUTE (nm_utils_sysctl_ip_conf_path (AF_INET, buf, nm_device_get_ip_iface (self), property)), - value_to_set); + ifname = nm_device_get_ip_iface_from_platform (self); + if (!ifname) + return NULL; + return nm_platform_sysctl_ip_conf_get (nm_device_get_platform (self), addr_family, ifname, property); } -static guint32 -nm_device_ipv4_sysctl_get_effective_uint32 (NMDevice *self, const char *property, guint32 fallback) +static gint64 +nm_device_sysctl_ip_conf_get_int_checked (NMDevice *self, + int addr_family, + const char *property, + guint base, + gint64 min, + gint64 max, + gint64 fallback) { - char buf[NM_UTILS_SYSCTL_IP_CONF_PATH_BUFSIZE]; - gint64 v, v_all; + const char *ifname; - if (!nm_device_get_ip_ifindex (self)) - return fallback; + nm_assert_addr_family (addr_family); - /* for this kind of sysctl (e.g. "rp_filter"), kernel effectively uses the - * MAX of the per-device value and the "all" value. - * - * Also do that, by reading both sysctls and return the maximum. */ - - v = nm_platform_sysctl_get_int_checked (nm_device_get_platform (self), - NMP_SYSCTL_PATHID_ABSOLUTE (nm_utils_sysctl_ip_conf_path (AF_INET, - buf, - nm_device_get_ip_iface (self), - property)), - 10, - 0, - G_MAXUINT32, - -1); - - v_all = nm_platform_sysctl_get_int_checked (nm_device_get_platform (self), - NMP_SYSCTL_PATHID_ABSOLUTE (nm_utils_sysctl_ip_conf_path (AF_INET, - buf, - "all", - property)), - 10, - 0, - G_MAXUINT32, - -1); - - v = NM_MAX (v, v_all); - return v > -1 ? (guint32) v : fallback; + ifname = nm_device_get_ip_iface_from_platform (self); + if (!ifname) { + errno = EINVAL; + return fallback; + } + return nm_platform_sysctl_ip_conf_get_int_checked (nm_device_get_platform (self), + addr_family, + ifname, + property, + base, + min, + max, + fallback); } gboolean -nm_device_ipv6_sysctl_set (NMDevice *self, const char *property, const char *value) +nm_device_sysctl_ip_conf_set (NMDevice *self, + int addr_family, + const char *property, + const char *value) { - char buf[NM_UTILS_SYSCTL_IP_CONF_PATH_BUFSIZE]; - - if (!nm_device_get_ip_ifindex (self)) - return FALSE; + NMPlatform *platform = nm_device_get_platform (self); + gs_free char *value_to_free = NULL; + const char *ifname; - return nm_platform_sysctl_set (nm_device_get_platform (self), NMP_SYSCTL_PATHID_ABSOLUTE (nm_utils_sysctl_ip_conf_path (AF_INET6, buf, nm_device_get_ip_iface (self), property)), value); -} + nm_assert_addr_family (addr_family); -static guint32 -nm_device_ipv6_sysctl_get_uint32 (NMDevice *self, const char *property, guint32 fallback) -{ - char buf[NM_UTILS_SYSCTL_IP_CONF_PATH_BUFSIZE]; + ifname = nm_device_get_ip_iface_from_platform (self); + if (!ifname) + return FALSE; - if (!nm_device_get_ip_ifindex (self)) - return fallback; + if (!value) { + /* Set to a default value when we've got a NULL @value. */ + value_to_free = nm_platform_sysctl_ip_conf_get (platform, + addr_family, + "default", + property); + value = value_to_free; + if (!value) + return FALSE; + } - return nm_platform_sysctl_get_int_checked (nm_device_get_platform (self), - NMP_SYSCTL_PATHID_ABSOLUTE (nm_utils_sysctl_ip_conf_path (AF_INET6, buf, nm_device_get_ip_iface (self), property)), - 10, - 0, - G_MAXUINT32, - fallback); + return nm_platform_sysctl_ip_conf_set (platform, + addr_family, + ifname, + property, + value); } +/*****************************************************************************/ + gboolean nm_device_has_capability (NMDevice *self, NMDeviceCapabilities caps) { @@ -1264,6 +1255,8 @@ _get_stable_id (NMDevice *self, gs_free char *generated = NULL; NMUtilsStableType stable_type; NMSettingConnection *s_con; + gboolean hwaddr_is_fake; + const char *hwaddr; const char *stable_id; const char *uuid; @@ -1273,16 +1266,22 @@ _get_stable_id (NMDevice *self, if (!stable_id) { default_id = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA, - "connection.stable-id", + NM_CON_DEFAULT ("connection.stable-id"), self); stable_id = default_id; } uuid = nm_connection_get_uuid (connection); + /* the cloned-mac-address may be generated based on the stable-id. + * Thus, at this point, we can only use the permanant MAC address + * as seed. */ + hwaddr = nm_device_get_permanent_hw_address_full (self, TRUE, &hwaddr_is_fake); + stable_type = nm_utils_stable_id_parse (stable_id, nm_device_get_ip_iface (self), - NULL, + !hwaddr_is_fake ? hwaddr : NULL, + nm_utils_boot_id_str (), uuid, &generated); @@ -1465,6 +1464,18 @@ nm_device_get_ip_iface (NMDevice *self) return priv->ip_iface ?: priv->iface; } +const char * +nm_device_get_ip_iface_from_platform (NMDevice *self) +{ + int ifindex; + + ifindex = nm_device_get_ip_ifindex (self); + if (ifindex <= 0) + return NULL; + + return nm_platform_link_get_name (nm_device_get_platform (self), ifindex); +} + int nm_device_get_ip_ifindex (const NMDevice *self) { @@ -1949,9 +1960,10 @@ nm_device_get_ip_iface_identifier (NMDevice *self, NMUtilsIPv6IfaceId *iid, gboo g_return_val_if_fail (NM_IS_DEVICE (self), FALSE); if (!ignore_token) { - s_ip6 = (NMSettingIP6Config *) - nm_device_get_applied_setting (self, NM_TYPE_SETTING_IP6_CONFIG); + s_ip6 = nm_device_get_applied_setting (self, NM_TYPE_SETTING_IP6_CONFIG); + g_return_val_if_fail (s_ip6, FALSE); + token = nm_setting_ip6_config_get_token (s_ip6); } if (token) @@ -2092,13 +2104,14 @@ nm_device_get_route_metric_default (NMDeviceType device_type) } static gboolean -default_route_metric_penalty_detect (NMDevice *self) +default_route_metric_penalty_detect (NMDevice *self, int addr_family) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); + const gboolean IS_IPv4 = (addr_family == AF_INET); /* currently we don't differentiate between IPv4 and IPv6 when detecting * connectivity. */ - if ( priv->connectivity_state != NM_CONNECTIVITY_FULL + if ( priv->concheck_x[IS_IPv4].state != NM_CONNECTIVITY_FULL && nm_connectivity_check_enabled (concheck_get_mgr (self))) return TRUE; @@ -2151,7 +2164,9 @@ nm_device_get_route_metric (NMDevice *self, /* use the current NMConfigData, which makes this configuration reloadable. * Note that that means that the route-metric might change between SIGHUP. * You must cache the returned value if that is a problem. */ - property = addr_family == AF_INET ? "ipv4.route-metric" : "ipv6.route-metric"; + property = addr_family == AF_INET + ? NM_CON_DEFAULT ("ipv4.route-metric") + : NM_CON_DEFAULT ("ipv6.route-metric"); route_metric = nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA, property, self, @@ -2181,7 +2196,7 @@ _get_mdns (NMDevice *self) return mdns; return nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA, - "connection.mdns", + NM_CON_DEFAULT ("connection.mdns"), self, NM_SETTING_CONNECTION_MDNS_NO, NM_SETTING_CONNECTION_MDNS_YES, @@ -2203,7 +2218,7 @@ _get_llmnr (NMDevice *self) return llmnr; return nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA, - "connection.llmnr", + NM_CON_DEFAULT ("connection.llmnr"), self, NM_SETTING_CONNECTION_LLMNR_NO, NM_SETTING_CONNECTION_LLMNR_YES, @@ -2252,7 +2267,9 @@ nm_device_get_route_table (NMDevice *self, if (route_table == 0) { const char *property; - property = addr_family == AF_INET ? "ipv4.route-table" : "ipv6.route-table"; + property = addr_family == AF_INET + ? NM_CON_DEFAULT ("ipv4.route-table") + : NM_CON_DEFAULT ("ipv6.route-table"); route_table = nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA, property, self, @@ -2365,10 +2382,27 @@ nm_device_get_act_request (NMDevice *self) return NM_DEVICE_GET_PRIVATE (self)->act_request.obj; } +NMActivationStateFlags +nm_device_get_activation_state_flags (NMDevice *self) +{ + NMActRequest *ac; + + g_return_val_if_fail (NM_IS_DEVICE (self), NM_ACTIVATION_STATE_FLAG_NONE); + + ac = NM_DEVICE_GET_PRIVATE (self)->act_request.obj; + if (!ac) + return NM_ACTIVATION_STATE_FLAG_NONE; + return nm_active_connection_get_state_flags (NM_ACTIVE_CONNECTION (ac)); +} + NMSettingsConnection * nm_device_get_settings_connection (NMDevice *self) { - NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); + NMDevicePrivate *priv; + + g_return_val_if_fail (NM_IS_DEVICE (self), NULL); + + priv = NM_DEVICE_GET_PRIVATE (self); return priv->act_request.obj ? nm_act_request_get_settings_connection (priv->act_request.obj) : NULL; } @@ -2412,7 +2446,7 @@ nm_device_has_unmodified_applied_connection (NMDevice *self, NMSettingCompareFla return nm_active_connection_has_unmodified_applied_connection ((NMActiveConnection *) priv->act_request.obj, compare_flags); } -NMSetting * +gpointer nm_device_get_applied_setting (NMDevice *self, GType setting_type) { NMConnection *connection; @@ -2448,24 +2482,37 @@ typedef enum { } ConcheckScheduleMode; static NMDeviceConnectivityHandle *concheck_start (NMDevice *self, + int addr_family, NMDeviceConnectivityCallback callback, gpointer user_data, gboolean is_periodic); static void concheck_periodic_schedule_set (NMDevice *self, + int addr_family, ConcheckScheduleMode mode); static gboolean -concheck_periodic_timeout_cb (gpointer user_data) +_concheck_periodic_timeout_cb (NMDevice *self, int addr_family) { - NMDevice *self = user_data; - - _LOGt (LOGD_CONCHECK, "connectivity: periodic timeout"); - concheck_periodic_schedule_set (self, CONCHECK_SCHEDULE_CHECK_PERIODIC); + _LOGt (LOGD_CONCHECK, "connectivity: [IPv%c] periodic timeout", + nm_utils_addr_family_to_char (addr_family)); + concheck_periodic_schedule_set (self, addr_family, CONCHECK_SCHEDULE_CHECK_PERIODIC); return G_SOURCE_REMOVE; } static gboolean +concheck_ip4_periodic_timeout_cb (gpointer user_data) +{ + return _concheck_periodic_timeout_cb (user_data, AF_INET); +} + +static gboolean +concheck_ip6_periodic_timeout_cb (gpointer user_data) +{ + return _concheck_periodic_timeout_cb (user_data, AF_INET6); +} + +static gboolean concheck_is_possible (NMDevice *self) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); @@ -2483,17 +2530,18 @@ concheck_is_possible (NMDevice *self) } static gboolean -concheck_periodic_schedule_do (NMDevice *self, gint64 now_ns) +concheck_periodic_schedule_do (NMDevice *self, int addr_family, gint64 now_ns) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); gboolean periodic_check_disabled = FALSE; gint64 expiry, tdiff; + const gboolean IS_IPv4 = (addr_family == AF_INET); /* we always cancel whatever was pending. */ - if (nm_clear_g_source (&priv->concheck_p_cur_id)) + if (nm_clear_g_source (&priv->concheck_x[IS_IPv4].p_cur_id)) periodic_check_disabled = TRUE; - if (priv->concheck_p_max_interval == 0) { + if (priv->concheck_x[IS_IPv4].p_max_interval == 0) { /* periodic checks are disabled */ goto out; } @@ -2502,46 +2550,50 @@ concheck_periodic_schedule_do (NMDevice *self, gint64 now_ns) goto out; nm_assert (now_ns > 0); - nm_assert (priv->concheck_p_cur_interval > 0); + nm_assert (priv->concheck_x[IS_IPv4].p_cur_interval > 0); /* we schedule the timeout based on our current settings cur-interval and cur-basetime. * Before calling concheck_periodic_schedule_do(), make sure that these properties are * correct. */ - expiry = priv->concheck_p_cur_basetime_ns + (priv->concheck_p_cur_interval * NM_UTILS_NS_PER_SECOND); + expiry = priv->concheck_x[IS_IPv4].p_cur_basetime_ns + (priv->concheck_x[IS_IPv4].p_cur_interval * NM_UTILS_NS_PER_SECOND); tdiff = expiry - now_ns; - _LOGT (LOGD_CONCHECK, "connectivity: periodic-check: %sscheduled in %lld milliseconds (%u seconds interval)", + _LOGT (LOGD_CONCHECK, "connectivity: [IPv%c] periodic-check: %sscheduled in %lld milliseconds (%u seconds interval)", + nm_utils_addr_family_to_char (addr_family), periodic_check_disabled ? "re-" : "", (long long) (tdiff / NM_UTILS_NS_PER_MSEC), - priv->concheck_p_cur_interval); + priv->concheck_x[IS_IPv4].p_cur_interval); - priv->concheck_p_cur_id = g_timeout_add (NM_MAX ((gint64) 0, tdiff) / NM_UTILS_NS_PER_MSEC, - concheck_periodic_timeout_cb, - self); + priv->concheck_x[IS_IPv4].p_cur_id = + g_timeout_add (NM_MAX ((gint64) 0, tdiff) / NM_UTILS_NS_PER_MSEC, + IS_IPv4 ? concheck_ip4_periodic_timeout_cb : concheck_ip6_periodic_timeout_cb, + self); return TRUE; out: - if (periodic_check_disabled) - _LOGT (LOGD_CONCHECK, "connectivity: periodic-check: unscheduled"); + if (periodic_check_disabled) { + _LOGT (LOGD_CONCHECK, "connectivity: [IPv%c] periodic-check: unscheduled", + nm_utils_addr_family_to_char (addr_family)); + } return FALSE; } #define CONCHECK_P_PROBE_INTERVAL 1 static void -concheck_periodic_schedule_set (NMDevice *self, - ConcheckScheduleMode mode) +concheck_periodic_schedule_set (NMDevice *self, int addr_family, ConcheckScheduleMode mode) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); gint64 new_expiry, exp_expiry, cur_expiry, tdiff; gint64 now_ns = 0; + const gboolean IS_IPv4 = (addr_family == AF_INET); - if (priv->concheck_p_max_interval == 0) { + if (priv->concheck_x[IS_IPv4].p_max_interval == 0) { /* periodic check is disabled. Nothing to do. */ return; } - if (!priv->concheck_p_cur_id) { + if (!priv->concheck_x[IS_IPv4].p_cur_id) { /* we currently don't have a timeout scheduled. No need to reschedule * another one... */ if (NM_IN_SET (mode, CONCHECK_SCHEDULE_UPDATE_INTERVAL, @@ -2555,19 +2607,19 @@ concheck_periodic_schedule_set (NMDevice *self, switch (mode) { case CONCHECK_SCHEDULE_UPDATE_INTERVAL_RESTART: - priv->concheck_p_cur_interval = NM_MIN (priv->concheck_p_max_interval, CONCHECK_P_PROBE_INTERVAL); - priv->concheck_p_cur_basetime_ns = nm_utils_get_monotonic_timestamp_ns_cached (&now_ns); - if (concheck_periodic_schedule_do (self, now_ns)) - concheck_start (self, NULL, NULL, TRUE); + priv->concheck_x[IS_IPv4].p_cur_interval = NM_MIN (priv->concheck_x[IS_IPv4].p_max_interval, CONCHECK_P_PROBE_INTERVAL); + priv->concheck_x[IS_IPv4].p_cur_basetime_ns = nm_utils_get_monotonic_timestamp_ns_cached (&now_ns); + if (concheck_periodic_schedule_do (self, addr_family, now_ns)) + concheck_start (self, addr_family, NULL, NULL, TRUE); return; case CONCHECK_SCHEDULE_UPDATE_INTERVAL: - /* called with "UPDATE_INTERVAL" and already have a concheck_p_cur_id scheduled. */ + /* called with "UPDATE_INTERVAL" and already have a p_cur_id scheduled. */ - nm_assert (priv->concheck_p_max_interval > 0); - nm_assert (priv->concheck_p_cur_interval > 0); + nm_assert (priv->concheck_x[IS_IPv4].p_max_interval > 0); + nm_assert (priv->concheck_x[IS_IPv4].p_cur_interval > 0); - if (priv->concheck_p_cur_interval <= priv->concheck_p_max_interval) { + if (priv->concheck_x[IS_IPv4].p_cur_interval <= priv->concheck_x[IS_IPv4].p_max_interval) { /* we currently have a shorter interval set, than what we now have. Either, * because we are probing, or because the previous max interval was shorter. * @@ -2576,17 +2628,17 @@ concheck_periodic_schedule_set (NMDevice *self, return; } - cur_expiry = priv->concheck_p_cur_basetime_ns + (priv->concheck_p_max_interval * NM_UTILS_NS_PER_SECOND); + cur_expiry = priv->concheck_x[IS_IPv4].p_cur_basetime_ns + (priv->concheck_x[IS_IPv4].p_max_interval * NM_UTILS_NS_PER_SECOND); nm_utils_get_monotonic_timestamp_ns_cached (&now_ns); - priv->concheck_p_cur_interval = priv->concheck_p_max_interval; + priv->concheck_x[IS_IPv4].p_cur_interval = priv->concheck_x[IS_IPv4].p_max_interval; if (cur_expiry <= now_ns) { /* Since the last time we scheduled a periodic check, already more than the * new max_interval passed. We need to start a check right away (and * schedule a timeout in cur-interval in the future). */ - priv->concheck_p_cur_basetime_ns = now_ns; - if (concheck_periodic_schedule_do (self, now_ns)) - concheck_start (self, NULL, NULL, TRUE); + priv->concheck_x[IS_IPv4].p_cur_basetime_ns = now_ns; + if (concheck_periodic_schedule_do (self, addr_family, now_ns)) + concheck_start (self, addr_family, NULL, NULL, TRUE); } else { /* we are reducing the max-interval to a shorter interval that we have currently * scheduled (with cur_interval). @@ -2594,24 +2646,26 @@ concheck_periodic_schedule_set (NMDevice *self, * However, since the last time we scheduled the check, not even the new max-interval * expired. All we need to do, is reschedule the timer to expire sooner. The cur_basetime * is unchanged. */ - concheck_periodic_schedule_do (self, now_ns); + concheck_periodic_schedule_do (self, addr_family, now_ns); } return; case CONCHECK_SCHEDULE_CHECK_EXTERNAL: /* a external connectivity check delays our periodic check. We reset the counter. */ - priv->concheck_p_cur_basetime_ns = nm_utils_get_monotonic_timestamp_ns_cached (&now_ns); - concheck_periodic_schedule_do (self, now_ns); + priv->concheck_x[IS_IPv4].p_cur_basetime_ns = nm_utils_get_monotonic_timestamp_ns_cached (&now_ns); + concheck_periodic_schedule_do (self, addr_family, now_ns); return; case CONCHECK_SCHEDULE_CHECK_PERIODIC: { gboolean any_periodic_pending; NMDeviceConnectivityHandle *handle; - guint old_interval = priv->concheck_p_cur_interval; + guint old_interval = priv->concheck_x[IS_IPv4].p_cur_interval; any_periodic_pending = FALSE; c_list_for_each_entry (handle, &priv->concheck_lst_head, concheck_lst) { + if (handle->addr_family != addr_family) + continue; if (handle->is_periodic_bump) { handle->is_periodic_bump = FALSE; handle->is_periodic_bump_on_complete = FALSE; @@ -2622,7 +2676,7 @@ concheck_periodic_schedule_set (NMDevice *self, /* we reached a timeout to schedule a new periodic request, however we still * have period requests pending that didn't complete yet. We need to bump the * interval already. */ - priv->concheck_p_cur_interval = NM_MIN (old_interval * 2, priv->concheck_p_max_interval); + priv->concheck_x[IS_IPv4].p_cur_interval = NM_MIN (old_interval * 2, priv->concheck_x[IS_IPv4].p_max_interval); } /* we just reached a timeout. The expected expiry (exp_expiry) should be @@ -2630,13 +2684,13 @@ concheck_periodic_schedule_set (NMDevice *self, * * We want to reschedule the timeout at exp_expiry (aka now) + cur_interval. */ nm_utils_get_monotonic_timestamp_ns_cached (&now_ns); - exp_expiry = priv->concheck_p_cur_basetime_ns + (old_interval * NM_UTILS_NS_PER_SECOND); - new_expiry = exp_expiry + (priv->concheck_p_cur_interval * NM_UTILS_NS_PER_SECOND); + exp_expiry = priv->concheck_x[IS_IPv4].p_cur_basetime_ns + (old_interval * NM_UTILS_NS_PER_SECOND); + new_expiry = exp_expiry + (priv->concheck_x[IS_IPv4].p_cur_interval * NM_UTILS_NS_PER_SECOND); tdiff = NM_MAX (new_expiry - now_ns, 0); - priv->concheck_p_cur_basetime_ns = (now_ns + tdiff) - (priv->concheck_p_cur_interval * NM_UTILS_NS_PER_SECOND); - if (concheck_periodic_schedule_do (self, now_ns)) { - handle = concheck_start (self, NULL, NULL, TRUE); - if (old_interval != priv->concheck_p_cur_interval) { + priv->concheck_x[IS_IPv4].p_cur_basetime_ns = (now_ns + tdiff) - (priv->concheck_x[IS_IPv4].p_cur_interval * NM_UTILS_NS_PER_SECOND); + if (concheck_periodic_schedule_do (self, addr_family, now_ns)) { + handle = concheck_start (self, addr_family, NULL, NULL, TRUE); + if (old_interval != priv->concheck_x[IS_IPv4].p_cur_interval) { /* we just bumped the interval already when scheduling this check. * When the handle returns, don't bump a second time. * @@ -2651,54 +2705,57 @@ concheck_periodic_schedule_set (NMDevice *self, /* we just got an event that we lost connectivity (that is, concheck returned). We reset * the interval to min/max or increase the probe interval (bump). */ case CONCHECK_SCHEDULE_RETURNED_MIN: - priv->concheck_p_cur_interval = NM_MIN (priv->concheck_p_max_interval, CONCHECK_P_PROBE_INTERVAL); + priv->concheck_x[IS_IPv4].p_cur_interval = NM_MIN (priv->concheck_x[IS_IPv4].p_max_interval, CONCHECK_P_PROBE_INTERVAL); break; case CONCHECK_SCHEDULE_RETURNED_MAX: - priv->concheck_p_cur_interval = priv->concheck_p_max_interval; + priv->concheck_x[IS_IPv4].p_cur_interval = priv->concheck_x[IS_IPv4].p_max_interval; break; case CONCHECK_SCHEDULE_RETURNED_BUMP: - priv->concheck_p_cur_interval = NM_MIN (priv->concheck_p_cur_interval * 2, priv->concheck_p_max_interval); + priv->concheck_x[IS_IPv4].p_cur_interval = NM_MIN (priv->concheck_x[IS_IPv4].p_cur_interval * 2, priv->concheck_x[IS_IPv4].p_max_interval); break; } /* we are here, because we returned from a connectivity check and adjust the current interval. * * But note that we calculate the new timeout based on the time when we scheduled the - * last check, instead of counting from now. The reaons is, that we want that the times + * last check, instead of counting from now. The reason is that we want that the times * when we schedule checks be at precise intervals, without including the time it took for * the connectivity check. */ - new_expiry = priv->concheck_p_cur_basetime_ns + (priv->concheck_p_cur_interval * NM_UTILS_NS_PER_SECOND); + new_expiry = priv->concheck_x[IS_IPv4].p_cur_basetime_ns + (priv->concheck_x[IS_IPv4].p_cur_interval * NM_UTILS_NS_PER_SECOND); tdiff = NM_MAX (new_expiry - nm_utils_get_monotonic_timestamp_ns_cached (&now_ns), 0); - priv->concheck_p_cur_basetime_ns = now_ns + tdiff - (priv->concheck_p_cur_interval * NM_UTILS_NS_PER_SECOND); - concheck_periodic_schedule_do (self, now_ns); + priv->concheck_x[IS_IPv4].p_cur_basetime_ns = now_ns + tdiff - (priv->concheck_x[IS_IPv4].p_cur_interval * NM_UTILS_NS_PER_SECOND); + concheck_periodic_schedule_do (self, addr_family, now_ns); } static void -concheck_update_interval (NMDevice *self, gboolean check_now) +concheck_update_interval (NMDevice *self, int addr_family, gboolean check_now) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); guint new_interval; + const gboolean IS_IPv4 = (addr_family == AF_INET); new_interval = nm_connectivity_get_interval (concheck_get_mgr (self)); new_interval = NM_MIN (new_interval, 7 *24 * 3600); - if (new_interval != priv->concheck_p_max_interval) { - _LOGT (LOGD_CONCHECK, "connectivity: periodic-check: set interval to %u seconds", new_interval); - priv->concheck_p_max_interval = new_interval; + if (new_interval != priv->concheck_x[IS_IPv4].p_max_interval) { + _LOGT (LOGD_CONCHECK, "connectivity: [IPv%c] periodic-check: set interval to %u seconds", + nm_utils_addr_family_to_char (addr_family), new_interval); + priv->concheck_x[IS_IPv4].p_max_interval = new_interval; } if (!new_interval) { /* this will cancel any potentially pending timeout because max-interval is zero. * But it logs a nice message... */ - concheck_periodic_schedule_do (self, 0); + concheck_periodic_schedule_do (self, addr_family, 0); /* also update the fake connectivity state. */ - concheck_update_state (self, NM_CONNECTIVITY_FAKE, TRUE); + concheck_update_state (self, addr_family, NM_CONNECTIVITY_FAKE, TRUE); return; } concheck_periodic_schedule_set (self, + addr_family, check_now ? CONCHECK_SCHEDULE_UPDATE_INTERVAL_RESTART : CONCHECK_SCHEDULE_UPDATE_INTERVAL); @@ -2707,13 +2764,16 @@ concheck_update_interval (NMDevice *self, gboolean check_now) void nm_device_check_connectivity_update_interval (NMDevice *self) { - concheck_update_interval (self, FALSE); + concheck_update_interval (self, AF_INET, TRUE); + concheck_update_interval (self, AF_INET6, TRUE); } static void -concheck_update_state (NMDevice *self, NMConnectivityState state, gboolean allow_periodic_bump) +concheck_update_state (NMDevice *self, int addr_family, + NMConnectivityState state, gboolean allow_periodic_bump) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); + const gboolean IS_IPv4 = (addr_family == AF_INET); /* @state is a result of the connectivity check. We only expect a precise * number of possible values. */ @@ -2726,7 +2786,7 @@ concheck_update_state (NMDevice *self, NMConnectivityState state, gboolean allow if (state == NM_CONNECTIVITY_ERROR) { /* on error, we don't change the current connectivity state, * except making UNKNOWN to NONE. */ - state = priv->connectivity_state; + state = priv->concheck_x[IS_IPv4].state; if (state == NM_CONNECTIVITY_UNKNOWN) state = NM_CONNECTIVITY_NONE; } else if (state == NM_CONNECTIVITY_FAKE) { @@ -2745,11 +2805,11 @@ concheck_update_state (NMDevice *self, NMConnectivityState state, gboolean allow state = NM_CONNECTIVITY_NONE; } - if (priv->connectivity_state == state) { + if (priv->concheck_x[IS_IPv4].state == state) { /* we got a connectivty update, but the state didn't change. If we were probing, * we bump the probe frequency. */ if (allow_periodic_bump) - concheck_periodic_schedule_set (self, CONCHECK_SCHEDULE_RETURNED_BUMP); + concheck_periodic_schedule_set (self, addr_family, CONCHECK_SCHEDULE_RETURNED_BUMP); return; } /* we need to update the probe interval before emitting signals. Emitting @@ -2758,23 +2818,22 @@ concheck_update_state (NMDevice *self, NMConnectivityState state, gboolean allow if (state == NM_CONNECTIVITY_FULL) { /* we reached full connectivity state. Stop probing by setting the * interval to the max. */ - concheck_periodic_schedule_set (self, CONCHECK_SCHEDULE_RETURNED_MAX); - } else if (priv->connectivity_state == NM_CONNECTIVITY_FULL) { + concheck_periodic_schedule_set (self, addr_family, CONCHECK_SCHEDULE_RETURNED_MAX); + } else if (priv->concheck_x[IS_IPv4].state == NM_CONNECTIVITY_FULL) { /* we are about to loose connectivity. (re)start probing by setting * the timeout interval to the min. */ - concheck_periodic_schedule_set (self, CONCHECK_SCHEDULE_RETURNED_MIN); + concheck_periodic_schedule_set (self, addr_family, CONCHECK_SCHEDULE_RETURNED_MIN); } else { if (allow_periodic_bump) - concheck_periodic_schedule_set (self, CONCHECK_SCHEDULE_RETURNED_BUMP); + concheck_periodic_schedule_set (self, addr_family, CONCHECK_SCHEDULE_RETURNED_BUMP); } _LOGD (LOGD_CONCHECK, "connectivity state changed from %s to %s", - nm_connectivity_state_to_string (priv->connectivity_state), + nm_connectivity_state_to_string (priv->concheck_x[IS_IPv4].state), nm_connectivity_state_to_string (state)); - priv->connectivity_state = state; + priv->concheck_x[IS_IPv4].state = state; - _notify (self, PROP_CONNECTIVITY); - g_signal_emit (self, signals[CONNECTIVITY_CHANGED], 0); + _notify (self, IS_IPv4 ? PROP_IP4_CONNECTIVITY : PROP_IP6_CONNECTIVITY); if ( priv->state == NM_DEVICE_STATE_ACTIVATED && !nm_device_sys_iface_state_is_external (self)) { @@ -2787,10 +2846,39 @@ concheck_update_state (NMDevice *self, NMConnectivityState state, gboolean allow } } +static const char * +nm_device_get_effective_ip_config_method (NMDevice *self, + int addr_family) +{ + NMDeviceClass *klass; + NMConnection *connection = nm_device_get_applied_connection (self); + const char *method; + + g_return_val_if_fail (NM_IS_CONNECTION (connection), "" /* bogus */); + nm_assert_addr_family (addr_family); + + method = nm_utils_get_ip_config_method (connection, addr_family); + + if ( (addr_family == AF_INET && nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) + || (addr_family == AF_INET6 && nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO))) { + klass = NM_DEVICE_GET_CLASS (self); + if (klass->get_auto_ip_config_method) { + const char *auto_method; + + auto_method = klass->get_auto_ip_config_method (self, addr_family); + if (auto_method) + return auto_method; + } + } + + return method; +} + static void -concheck_handle_complete (NMDeviceConnectivityHandle *handle, - GError *error) +concheck_handle_complete (NMDeviceConnectivityHandle *handle, GError *error) { + const gboolean IS_IPv4 = (handle->addr_family == AF_INET); + /* The moment we invoke the callback, we unlink it. It signals * that @handle is handled -- as far as the callee of callback * is concerned. */ @@ -2802,7 +2890,7 @@ concheck_handle_complete (NMDeviceConnectivityHandle *handle, if (handle->callback) { handle->callback (handle->self, handle, - NM_DEVICE_GET_PRIVATE (handle->self)->connectivity_state, + NM_DEVICE_GET_PRIVATE (handle->self)->concheck_x[IS_IPv4].state, error, handle->user_data); } @@ -2838,7 +2926,8 @@ concheck_cb (NMConnectivity *connectivity, /* the only place where we nm_connectivity_check_cancel(@c_handle), is * from inside concheck_handle_complete(). This is a recursive call, * nothing to do. */ - _LOGT (LOGD_CONCHECK, "connectivity: complete check (seq:%llu, cancelled)", + _LOGT (LOGD_CONCHECK, "connectivity: [IPv%c] complete check (seq:%llu, cancelled)", + nm_utils_addr_family_to_char (handle->addr_family), (long long unsigned) handle->seq); return; } @@ -2848,7 +2937,8 @@ concheck_cb (NMConnectivity *connectivity, self_keep_alive = g_object_ref (self); - _LOGT (LOGD_CONCHECK, "connectivity: complete check (seq:%llu, state:%s)", + _LOGT (LOGD_CONCHECK, "connectivity: [Ipv%c] complete check (seq:%llu, state:%s)", + nm_utils_addr_family_to_char (handle->addr_family), (long long unsigned) handle->seq, nm_connectivity_state_to_string (state)); @@ -2864,6 +2954,8 @@ concheck_cb (NMConnectivity *connectivity, any_periodic_before = FALSE; any_periodic_after = FALSE; c_list_for_each_entry (other_handle, &priv->concheck_lst_head, concheck_lst) { + if (other_handle->addr_family != handle->addr_family) + continue; if (other_handle->is_periodic_bump_on_complete) { if (other_handle->seq < seq) any_periodic_before = TRUE; @@ -2890,7 +2982,7 @@ concheck_cb (NMConnectivity *connectivity, } /* first update the new state, and emit signals. */ - concheck_update_state (self, state, allow_periodic_bump); + concheck_update_state (self, handle->addr_family, state, allow_periodic_bump); handle_is_alive = FALSE; @@ -2902,6 +2994,8 @@ concheck_cb (NMConnectivity *connectivity, * @handle, as they are automatically obsoleted. */ check_handles: c_list_for_each_entry (other_handle, &priv->concheck_lst_head, concheck_lst) { + if (other_handle->addr_family != handle->addr_family) + continue; if (other_handle->seq >= seq) { /* it's not guaranteed that @handle is still in the list. It might already * be canceled while invoking callbacks for a previous other_handle. @@ -2939,6 +3033,7 @@ check_handles: static NMDeviceConnectivityHandle * concheck_start (NMDevice *self, + int addr_family, NMDeviceConnectivityCallback callback, gpointer user_data, gboolean is_periodic) @@ -2959,14 +3054,18 @@ concheck_start (NMDevice *self, handle->is_periodic = is_periodic; handle->is_periodic_bump = is_periodic; handle->is_periodic_bump_on_complete = is_periodic; + handle->addr_family = addr_family; c_list_link_tail (&priv->concheck_lst_head, &handle->concheck_lst); - _LOGT (LOGD_CONCHECK, "connectivity: start check (seq:%llu%s)", + _LOGT (LOGD_CONCHECK, "connectivity: [IPv%c] start check (seq:%llu%s)", + nm_utils_addr_family_to_char (addr_family), (long long unsigned) handle->seq, is_periodic ? ", periodic-check" : ""); handle->c_handle = nm_connectivity_check_start (concheck_get_mgr (self), + handle->addr_family, + nm_device_get_ip_ifindex (self), nm_device_get_ip_iface (self), concheck_cb, handle); @@ -2975,6 +3074,7 @@ concheck_start (NMDevice *self, NMDeviceConnectivityHandle * nm_device_check_connectivity (NMDevice *self, + int addr_family, NMDeviceConnectivityCallback callback, gpointer user_data) { @@ -2983,8 +3083,8 @@ nm_device_check_connectivity (NMDevice *self, if (!concheck_is_possible (self)) return NULL; - concheck_periodic_schedule_set (self, CONCHECK_SCHEDULE_CHECK_EXTERNAL); - handle = concheck_start (self, callback, user_data, FALSE); + concheck_periodic_schedule_set (self, addr_family, CONCHECK_SCHEDULE_CHECK_EXTERNAL); + handle = concheck_start (self, addr_family, callback, user_data, FALSE); return handle; } @@ -3006,11 +3106,26 @@ nm_device_check_connectivity_cancel (NMDeviceConnectivityHandle *handle) } NMConnectivityState -nm_device_get_connectivity_state (NMDevice *self) +nm_device_get_connectivity_state (NMDevice *self, int addr_family) { + NMDevicePrivate *priv; + const gboolean IS_IPv4 = (addr_family == AF_INET); + g_return_val_if_fail (NM_IS_DEVICE (self), NM_CONNECTIVITY_UNKNOWN); + nm_assert_addr_family (addr_family); - return NM_DEVICE_GET_PRIVATE (self)->connectivity_state; + priv = NM_DEVICE_GET_PRIVATE (self); + + switch (addr_family) { + case AF_INET: + case AF_INET6: + return priv->concheck_x[IS_IPv4].state; + default: + nm_assert (addr_family == AF_UNSPEC); + return NM_MAX_WITH_CMP (nm_connectivity_state_cmp, + priv->concheck_x[0].state, + priv->concheck_x[1].state); + } } /*****************************************************************************/ @@ -3413,11 +3528,14 @@ nm_device_set_carrier (NMDevice *self, gboolean carrier) static void nm_device_set_carrier_from_platform (NMDevice *self) { + int ifindex; + if (nm_device_has_capability (self, NM_DEVICE_CAP_CARRIER_DETECT)) { - if (!nm_device_has_capability (self, NM_DEVICE_CAP_NONSTANDARD_CARRIER)) { + if ( !nm_device_has_capability (self, NM_DEVICE_CAP_NONSTANDARD_CARRIER) + && (ifindex = nm_device_get_ip_ifindex (self)) > 0) { nm_device_set_carrier (self, nm_platform_link_is_connected (nm_device_get_platform (self), - nm_device_get_ip_ifindex (self))); + ifindex)); } } else { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); @@ -3585,10 +3703,14 @@ device_link_changed (NMDevice *self) gboolean was_up; gboolean update_unmanaged_specs = FALSE; gboolean got_hw_addr = FALSE, had_hw_addr; + gboolean seen_down = priv->device_link_changed_down; priv->device_link_changed_id = 0; + priv->device_link_changed_down = FALSE; ifindex = nm_device_get_ifindex (self); + if (ifindex <= 0) + return G_SOURCE_REMOVE; pllink = nm_platform_link_get (nm_device_get_platform (self), ifindex); if (!pllink) return G_SOURCE_REMOVE; @@ -3696,7 +3818,7 @@ device_link_changed (NMDevice *self) device_recheck_slave_status (self, pllink); - if (priv->up && !was_up) { + if (priv->up && (!was_up || seen_down)) { /* the link was down and just came up. That happens for example, while changing MTU. * We must restore IP configuration. */ if (priv->ip4_state == IP_DONE) { @@ -3772,6 +3894,8 @@ link_changed_cb (NMPlatform *platform, priv = NM_DEVICE_GET_PRIVATE (self); if (ifindex == nm_device_get_ifindex (self)) { + if (!(info->n_ifi_flags & IFF_UP)) + priv->device_link_changed_down = TRUE; if (!priv->device_link_changed_id) { priv->device_link_changed_id = g_idle_add ((GSourceFunc) device_link_changed, self); _LOGD (LOGD_DEVICE, "queued link change for ifindex %d", ifindex); @@ -3786,126 +3910,6 @@ link_changed_cb (NMPlatform *platform, /*****************************************************************************/ -typedef struct { - in_addr_t network; - guint8 plen; -} IP4RPFilterData; - -static guint -_v4_has_shadowed_routes_detect_hash (const IP4RPFilterData *d) -{ - NMHashState h; - - nm_hash_init (&h, 1105201169u); - nm_hash_update_vals (&h, - d->network, - d->plen); - return nm_hash_complete (&h); -} - -static gboolean -_v4_has_shadowed_routes_detect_equal (const IP4RPFilterData *d1, const IP4RPFilterData *d2) -{ - return d1->network == d2->network && d1->plen == d2->plen; -} - -static gboolean -_v4_has_shadowed_routes_detect (NMDevice *self) -{ - NMPlatform *platform; - int ifindex; - NMPLookup lookup; - const NMDedupMultiHeadEntry *head_entry; - NMDedupMultiIter iter; - const NMPObject *o; - guint data_len; - gs_unref_hashtable GHashTable *data_hash = NULL; - gs_free IP4RPFilterData *data_arr = NULL; - - ifindex = nm_device_get_ip_ifindex (self); - if (ifindex <= 0) - return FALSE; - - platform = nm_device_get_platform (self); - - head_entry = nm_platform_lookup (platform, - nmp_lookup_init_object (&lookup, - NMP_OBJECT_TYPE_IP4_ROUTE, - ifindex)); - if (!head_entry) - return FALSE; - - /* first, create a lookup index @data_hash for all network/plen pairs. */ - data_len = 0; - data_arr = g_new (IP4RPFilterData, head_entry->len); - data_hash = g_hash_table_new ((GHashFunc) _v4_has_shadowed_routes_detect_hash, - (GEqualFunc) _v4_has_shadowed_routes_detect_equal); - - nmp_cache_iter_for_each (&iter, head_entry, &o) { - const NMPlatformIP4Route *r = NMP_OBJECT_CAST_IP4_ROUTE (o); - IP4RPFilterData *d; - - nm_assert (r->ifindex == ifindex); - - if ( NM_PLATFORM_IP_ROUTE_IS_DEFAULT (r) - || r->table_coerced) - continue; - - d = &data_arr[data_len++]; - d->network = nm_utils_ip4_address_clear_host_address (r->network, r->plen); - d->plen = r->plen; - g_hash_table_add (data_hash, d); - } - - /* then, search if there is any route on another interface with the same - * network/plen destination. If yes, we consider this a multihoming - * setup. */ - head_entry = nm_platform_lookup (platform, - nmp_lookup_init_obj_type (&lookup, - NMP_OBJECT_TYPE_IP4_ROUTE)); - nmp_cache_iter_for_each (&iter, head_entry, &o) { - const NMPlatformIP4Route *r = NMP_OBJECT_CAST_IP4_ROUTE (o); - IP4RPFilterData d; - - if ( r->ifindex == ifindex - || NM_PLATFORM_IP_ROUTE_IS_DEFAULT (r) - || r->table_coerced) - continue; - - d.network = nm_utils_ip4_address_clear_host_address (r->network, r->plen); - d.plen = r->plen; - if (g_hash_table_contains (data_hash, &d)) - return TRUE; - } - - return FALSE; -} - -static void -ip4_rp_filter_update (NMDevice *self) -{ - NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); - const char *ip4_rp_filter; - - if ( priv->v4_has_shadowed_routes - || nm_device_get_best_default_route (self, AF_INET)) { - if (nm_device_ipv4_sysctl_get_effective_uint32 (self, "rp_filter", 0) != 1) { - /* Don't touch the rp_filter if it's not strict. */ - return; - } - /* Loose rp_filter */ - ip4_rp_filter = "2"; - } else { - /* Default rp_filter */ - ip4_rp_filter = NULL; - } - - if (ip4_rp_filter != priv->ip4_rp_filter) { - nm_device_ipv4_sysctl_set (self, "rp_filter", ip4_rp_filter); - priv->ip4_rp_filter = ip4_rp_filter; - } -} - static void link_changed (NMDevice *self, const NMPlatformLink *pllink) { @@ -4133,7 +4137,7 @@ device_init_static_sriov_num_vfs (NMDevice *self) num_vfs = _nm_utils_ascii_str_to_int64 (value, 10, 0, G_MAXINT32, -1); if (num_vfs >= 0) { nm_platform_link_set_sriov_params (nm_device_get_platform (self), - priv->ifindex, num_vfs, -1); + priv->ifindex, num_vfs, NM_TERNARY_DEFAULT); } } } @@ -4296,8 +4300,6 @@ realize_start_setup (NMDevice *self, nm_device_set_carrier_from_platform (self); - device_init_static_sriov_num_vfs (self); - nm_assert (!priv->stats.timeout_id); real_rate = _stats_refresh_rate_real (priv->stats.refresh_rate_ms); if (real_rate) @@ -4794,7 +4796,8 @@ nm_device_master_release_slaves (NMDevice *self) if (priv->state == NM_DEVICE_STATE_FAILED) reason = NM_DEVICE_STATE_REASON_DEPENDENCY_FAILED; - if (!nm_platform_link_get (nm_device_get_platform (self), priv->ifindex)) + if ( priv->ifindex <= 0 + || !nm_platform_link_get (nm_device_get_platform (self), priv->ifindex)) configure = FALSE; c_list_for_each_safe (iter, safe, &priv->slaves) { @@ -4893,12 +4896,12 @@ check_ip_state (NMDevice *self, gboolean may_fail, gboolean full_state_update) && !priv->is_enslaved) return; - s_ip4 = (NMSettingIPConfig *) nm_device_get_applied_setting (self, NM_TYPE_SETTING_IP4_CONFIG); + s_ip4 = nm_device_get_applied_setting (self, NM_TYPE_SETTING_IP4_CONFIG); if (s_ip4 && nm_streq0 (nm_setting_ip_config_get_method (s_ip4), NM_SETTING_IP4_CONFIG_METHOD_DISABLED)) ip4_disabled = TRUE; - s_ip6 = (NMSettingIPConfig *) nm_device_get_applied_setting (self, NM_TYPE_SETTING_IP6_CONFIG); + s_ip6 = nm_device_get_applied_setting (self, NM_TYPE_SETTING_IP6_CONFIG); if (s_ip6 && nm_streq0 (nm_setting_ip_config_get_method (s_ip6), NM_SETTING_IP6_CONFIG_METHOD_IGNORE)) ip6_ignore = TRUE; @@ -5257,7 +5260,7 @@ nm_device_autoconnect_allowed (NMDevice *self) /* The 'autoconnect-allowed' signal is emitted on a device to allow * other listeners to block autoconnect on the device if they wish. * This is mainly used by the OLPC Mesh devices to block autoconnect - * on their companion WiFi device as they share radio resources and + * on their companion Wi-Fi device as they share radio resources and * cannot be connected at the same time. */ @@ -5476,10 +5479,13 @@ nm_device_generate_connection (NMDevice *self, nm_connection_add_setting (connection, nm_setting_proxy_new ()); pllink = nm_platform_link_get (nm_device_get_platform (self), priv->ifindex); - if (pllink && pllink->inet6_token.id) { + if ( pllink + && pllink->inet6_token.id) { + char sbuf[NM_UTILS_INET_ADDRSTRLEN]; + g_object_set (s_ip6, NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE, NM_IN6_ADDR_GEN_MODE_EUI64, - NM_SETTING_IP6_CONFIG_TOKEN, nm_utils_inet6_interface_identifier_to_token (pllink->inet6_token, NULL), + NM_SETTING_IP6_CONFIG_TOKEN, nm_utils_inet6_interface_identifier_to_token (pllink->inet6_token, sbuf), NULL); } } @@ -5497,8 +5503,8 @@ nm_device_generate_connection (NMDevice *self, /* Ignore the connection if it has no IP configuration, * no slave configuration, and is not a master interface. */ - ip4_method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); - ip6_method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG); + ip4_method = nm_utils_get_ip_config_method (connection, AF_INET); + ip6_method = nm_utils_get_ip_config_method (connection, AF_INET6); if ( g_strcmp0 (ip4_method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) == 0 && g_strcmp0 (ip6_method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) == 0 && !nm_setting_connection_get_master (NM_SETTING_CONNECTION (s_con)) @@ -5778,7 +5784,7 @@ unmanaged_on_quit (NMDevice *self) /* the only exception are IPv4 shared connections. We unmanage them on quit. */ connection = nm_device_get_applied_connection (self); if (connection) { - if (NM_IN_STRSET (nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG), + if (NM_IN_STRSET (nm_utils_get_ip_config_method (connection, AF_INET), NM_SETTING_IP4_CONFIG_METHOD_SHARED)) { /* shared connections are to be unmangaed. */ return TRUE; @@ -6102,7 +6108,7 @@ lldp_rx_enabled (NMDevice *self) lldp = nm_setting_connection_get_lldp (s_con); if (lldp == NM_SETTING_CONNECTION_LLDP_DEFAULT) { lldp = nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA, - "connection.lldp", + NM_CON_DEFAULT ("connection.lldp"), self, NM_SETTING_CONNECTION_LLDP_DEFAULT, NM_SETTING_CONNECTION_LLDP_ENABLE_RX, @@ -6197,16 +6203,16 @@ act_stage1_prepare (NMDevice *self, NMDeviceStateReason *out_failure_reason) if ( priv->ifindex > 0 && nm_device_has_capability (self, NM_DEVICE_CAP_SRIOV) - && (s_sriov = (NMSettingSriov *) nm_device_get_applied_setting (self, NM_TYPE_SETTING_SRIOV))) { + && (s_sriov = nm_device_get_applied_setting (self, NM_TYPE_SETTING_SRIOV))) { nm_auto_freev NMPlatformVF **plat_vfs = NULL; gs_free_error GError *error = NULL; NMSriovVF *vf; - int autoprobe; + NMTernary autoprobe; autoprobe = nm_setting_sriov_get_autoprobe_drivers (s_sriov); if (autoprobe == NM_TERNARY_DEFAULT) { autoprobe = nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA, - "sriov.autoprobe-drivers", + NM_CON_DEFAULT ("sriov.autoprobe-drivers"), self, NM_TERNARY_FALSE, NM_TERNARY_TRUE, @@ -6576,7 +6582,7 @@ get_ipv4_dad_timeout (NMDevice *self) return timeout; return nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA, - "ipv4.dad-timeout", + NM_CON_DEFAULT ("ipv4.dad-timeout"), self, 0, NM_SETTING_IP_CONFIG_DAD_TIMEOUT_MAX, @@ -6584,17 +6590,15 @@ get_ipv4_dad_timeout (NMDevice *self) } static void -acd_data_destroy (gpointer ptr, GClosure *closure) +acd_data_destroy (gpointer ptr) { AcdData *data = ptr; int i; - if (data) { - for (i = 0; data->configs && data->configs[i]; i++) - g_object_unref (data->configs[i]); - g_free (data->configs); - g_slice_free (AcdData, data); - } + for (i = 0; data->configs && data->configs[i]; i++) + g_object_unref (data->configs[i]); + g_free (data->configs); + g_slice_free (AcdData, data); } static void @@ -6606,7 +6610,7 @@ ipv4_manual_method_apply (NMDevice *self, NMIP4Config **configs, gboolean succes connection = nm_device_get_applied_connection (self); nm_assert (connection); - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); + method = nm_utils_get_ip_config_method (connection, AF_INET); nm_assert (NM_IN_STRSET (method, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NM_SETTING_IP4_CONFIG_METHOD_AUTO)); @@ -6628,8 +6632,9 @@ ipv4_manual_method_apply (NMDevice *self, NMIP4Config **configs, gboolean succes } static void -acd_manager_probe_terminated (NMAcdManager *acd_manager, AcdData *data) +acd_manager_probe_terminated (NMAcdManager *acd_manager, gpointer user_data) { + AcdData *data = user_data; NMDevice *self; NMDevicePrivate *priv; NMDedupMultiIter ipconf_iter; @@ -6643,13 +6648,15 @@ acd_manager_probe_terminated (NMAcdManager *acd_manager, AcdData *data) for (i = 0; data->configs && data->configs[i]; i++) { nm_ip_config_iter_ip4_address_for_each (&ipconf_iter, data->configs[i], &address) { + char sbuf[NM_UTILS_INET_ADDRSTRLEN]; + result = nm_acd_manager_check_address (acd_manager, address->address); success &= result; _NMLOG (result ? LOGL_DEBUG : LOGL_WARN, LOGD_DEVICE, "IPv4 DAD result: address %s is %s", - nm_utils_inet4_ntop (address->address, NULL), + nm_utils_inet4_ntop (address->address, sbuf), result ? "unique" : "duplicate"); } } @@ -6657,7 +6664,7 @@ acd_manager_probe_terminated (NMAcdManager *acd_manager, AcdData *data) data->callback (self, data->configs, success); priv->acd.dad_list = g_slist_remove (priv->acd.dad_list, acd_manager); - nm_acd_manager_destroy (acd_manager); + nm_acd_manager_free (acd_manager); } /** @@ -6673,6 +6680,10 @@ acd_manager_probe_terminated (NMAcdManager *acd_manager, AcdData *data) static void ipv4_dad_start (NMDevice *self, NMIP4Config **configs, AcdCallback cb) { + static const NMAcdCallbacks acd_callbacks = { + .probe_terminated_callback = acd_manager_probe_terminated, + .user_data_destroy = acd_data_destroy, + }; NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); NMAcdManager *acd_manager; const NMPlatformIP4Address *address; @@ -6716,26 +6727,23 @@ ipv4_dad_start (NMDevice *self, NMIP4Config **configs, AcdCallback cb) return; } - /* don't take additional references of @acd_manager that outlive @self. - * Otherwise, the callback can be invoked on a dangling pointer as we don't - * disconnect the handler. */ - acd_manager = nm_acd_manager_new (nm_device_get_ip_ifindex (self), hwaddr_arr, length); - priv->acd.dad_list = g_slist_append (priv->acd.dad_list, acd_manager); - data = g_slice_new0 (AcdData); data->configs = configs; data->callback = cb; data->device = self; + acd_manager = nm_acd_manager_new (nm_device_get_ip_ifindex (self), + hwaddr_arr, + length, + &acd_callbacks, + data); + priv->acd.dad_list = g_slist_append (priv->acd.dad_list, acd_manager); + for (i = 0; configs[i]; i++) { nm_ip_config_iter_ip4_address_for_each (&ipconf_iter, configs[i], &address) nm_acd_manager_add_address (acd_manager, address->address); } - g_signal_connect_data (acd_manager, NM_ACD_MANAGER_PROBE_TERMINATED, - G_CALLBACK (acd_manager_probe_terminated), data, - acd_data_destroy, 0); - ret = nm_acd_manager_start_probe (acd_manager, timeout); if (!ret) { @@ -6745,7 +6753,7 @@ ipv4_dad_start (NMDevice *self, NMIP4Config **configs, AcdCallback cb) cb (self, configs, TRUE); priv->acd.dad_list = g_slist_remove (priv->acd.dad_list, acd_manager); - nm_acd_manager_destroy (acd_manager); + nm_acd_manager_free (acd_manager); } } @@ -6801,8 +6809,6 @@ nm_device_handle_ipv4ll_event (sd_ipv4ll *ll, int event, void *data) { NMDevice *self = data; NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); - NMConnection *connection = NULL; - const char *method; struct in_addr address; NMIP4Config *config; int r; @@ -6810,13 +6816,8 @@ nm_device_handle_ipv4ll_event (sd_ipv4ll *ll, int event, void *data) if (priv->act_request.obj == NULL) return; - connection = nm_act_request_get_applied_connection (priv->act_request.obj); - g_assert (connection); - - /* Ignore if the connection isn't an AutoIP connection */ - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); - if (g_strcmp0 (method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL) != 0) - return; + nm_assert (nm_streq (nm_device_get_effective_ip_config_method (self, AF_INET), + NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL)); switch (event) { case SD_IPV4LL_EVENT_BIND: @@ -6988,6 +6989,7 @@ dhcp4_cleanup (NMDevice *self, CleanupType cleanup_type, gboolean release) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); + priv->dhcp4.was_active = FALSE; nm_clear_g_source (&priv->dhcp4.grace_id); g_clear_pointer (&priv->dhcp4.pac_url, g_free); g_clear_pointer (&priv->dhcp4.root_path, g_free); @@ -7100,7 +7102,7 @@ ip_config_merge_and_apply (NMDevice *self, if (commit) { gboolean v; - v = default_route_metric_penalty_detect (self); + v = default_route_metric_penalty_detect (self, addr_family); if (IS_IPv4) priv->default_route_metric_penalty_ip4_has = v; else @@ -7202,15 +7204,15 @@ ip_config_merge_and_apply (NMDevice *self, } if (!IS_IPv4) { - if (commit) { - NMUtilsIPv6IfaceId iid; + NMUtilsIPv6IfaceId iid; - if ( ip6_addr_gen_token - && nm_utils_ipv6_interface_identifier_get_from_token (&iid, ip6_addr_gen_token)) { - nm_platform_link_set_ipv6_token (nm_device_get_platform (self), - nm_device_get_ip_ifindex (self), - iid); - } + if ( commit + && priv->ndisc_started + && ip6_addr_gen_token + && nm_utils_ipv6_interface_identifier_get_from_token (&iid, ip6_addr_gen_token)) { + nm_platform_link_set_ipv6_token (nm_device_get_platform (self), + nm_device_get_ip_ifindex (self), + iid); } } @@ -7266,12 +7268,13 @@ dhcp4_grace_period_expired (gpointer user_data) } static void -dhcp4_fail (NMDevice *self) +dhcp4_fail (NMDevice *self, NMDhcpState dhcp_state) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); - _LOGD (LOGD_DHCP4, "DHCPv4 failed (ip_state %s)", - _ip_state_to_string (priv->ip4_state)); + _LOGD (LOGD_DHCP4, "DHCPv4 failed (ip_state %s, was_active %d)", + _ip_state_to_string (priv->ip4_state), + priv->dhcp4.was_active); /* Keep client running if there are static addresses configured * on the interface. @@ -7281,11 +7284,14 @@ dhcp4_fail (NMDevice *self) && nm_ip4_config_get_num_addresses (priv->con_ip_config_4) > 0) goto clear_config; - /* Fail the method in case of timeout or failure during initial - * configuration. + /* Fail the method when one of the following is true: + * 1) the DHCP client terminated: it does not make sense to start a grace + * period without a client running; + * 2) we failed to get an initial lease AND the client was + * not active before. */ - if ( !priv->dhcp4.was_active - && priv->ip4_state == IP_CONF) { + if ( dhcp_state == NM_DHCP_STATE_TERMINATED + || (!priv->dhcp4.was_active && priv->ip4_state == IP_CONF)) { dhcp4_cleanup (self, CLEANUP_TYPE_DECONFIGURE, FALSE); nm_device_activate_schedule_ip4_config_timeout (self); return; @@ -7348,7 +7354,7 @@ dhcp4_state_changed (NMDhcpClient *client, case NM_DHCP_STATE_BOUND: if (!ip4_config) { _LOGW (LOGD_DHCP4, "failed to get IPv4 config in response to DHCP event."); - dhcp4_fail (self); + dhcp4_fail (self, state); break; } @@ -7391,11 +7397,11 @@ dhcp4_state_changed (NMDhcpClient *client, if (dhcp4_lease_change (self, ip4_config)) nm_device_update_metered (self); else - dhcp4_fail (self); + dhcp4_fail (self, state); } break; case NM_DHCP_STATE_TIMEOUT: - dhcp4_fail (self); + dhcp4_fail (self, state); break; case NM_DHCP_STATE_EXPIRE: /* Ignore expiry before we even have a lease (NAK, old lease, etc) */ @@ -7404,7 +7410,8 @@ dhcp4_state_changed (NMDhcpClient *client, /* fall through */ case NM_DHCP_STATE_DONE: case NM_DHCP_STATE_FAIL: - dhcp4_fail (self); + case NM_DHCP_STATE_TERMINATED: + dhcp4_fail (self, state); break; default: break; @@ -7435,8 +7442,8 @@ get_dhcp_timeout (NMDevice *self, int addr_family) timeout = nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA, addr_family == AF_INET - ? "ipv4.dhcp-timeout" - : "ipv6.dhcp-timeout", + ? NM_CON_DEFAULT ("ipv4.dhcp-timeout") + : NM_CON_DEFAULT ("ipv6.dhcp-timeout"), self, 0, G_MAXINT32, 0); if (timeout) @@ -7450,18 +7457,6 @@ get_dhcp_timeout (NMDevice *self, int addr_family) } static GBytes * -dhcp4_get_client_id_mac (const guint8 *hwaddr /* ETH_ALEN bytes */) -{ - guint8 *client_id_buf; - guint8 hwaddr_type = ARPHRD_ETHER; - - client_id_buf = g_malloc (ETH_ALEN + 1); - client_id_buf[0] = hwaddr_type; - memcpy (&client_id_buf[1], hwaddr, ETH_ALEN); - return g_bytes_new_take (client_id_buf, ETH_ALEN + 1); -} - -static GBytes * dhcp4_get_client_id (NMDevice *self, NMConnection *connection, GBytes *hwaddr) @@ -7473,6 +7468,7 @@ dhcp4_get_client_id (NMDevice *self, const char *fail_reason; guint8 hwaddr_bin_buf[NM_UTILS_HWADDR_LEN_MAX]; const guint8 *hwaddr_bin; + int arp_type; gsize hwaddr_len; GBytes *result; gs_free char *logstr1 = NULL; @@ -7482,7 +7478,8 @@ dhcp4_get_client_id (NMDevice *self, if (!client_id) { client_id_default = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA, - "ipv4.dhcp-client-id", self); + NM_CON_DEFAULT ("ipv4.dhcp-client-id"), + self); if (client_id_default && client_id_default[0]) { /* a non-empty client-id is always valid, see nm_dhcp_utils_client_id_string_to_bytes(). */ client_id = client_id_default; @@ -7491,23 +7488,24 @@ dhcp4_get_client_id (NMDevice *self, if (!client_id) { _LOGD (LOGD_DEVICE | LOGD_DHCP4 | LOGD_IP4, - "ipv4.dhcp-client-id: no explicity client-id configured"); + "ipv4.dhcp-client-id: no explicit client-id configured"); return NULL; } if (nm_streq (client_id, "mac")) { if (!hwaddr) { - fail_reason = "failed to get current MAC address"; + fail_reason = "missing link-layer address"; goto out_fail; } hwaddr_bin = g_bytes_get_data (hwaddr, &hwaddr_len); - if (hwaddr_len != ETH_ALEN) { - fail_reason = "MAC address is not ethernet"; + arp_type = nm_utils_arp_type_detect_from_hwaddrlen (hwaddr_len); + if (arp_type < 0) { + fail_reason = "unsupported link-layer address"; goto out_fail; } - result = dhcp4_get_client_id_mac (hwaddr_bin); + result = nm_utils_dhcp_client_id_mac (arp_type, hwaddr_bin, hwaddr_len); goto out_good; } @@ -7516,32 +7514,37 @@ dhcp4_get_client_id (NMDevice *self, hwaddr_str = nm_device_get_permanent_hw_address (self); if (!hwaddr_str) { - fail_reason = "failed to get permanent MAC address"; + fail_reason = "missing permanent link-layer address"; goto out_fail; } if (!_nm_utils_hwaddr_aton (hwaddr_str, hwaddr_bin_buf, sizeof (hwaddr_bin_buf), &hwaddr_len)) g_return_val_if_reached (NULL); - if (hwaddr_len != ETH_ALEN) { - /* unsupported type. */ - fail_reason = "MAC address is not ethernet"; + arp_type = nm_utils_arp_type_detect_from_hwaddrlen (hwaddr_len); + if (arp_type < 0) { + fail_reason = "unsupported permanent link-layer address"; goto out_fail; } - result = dhcp4_get_client_id_mac (hwaddr_bin_buf); + result = nm_utils_dhcp_client_id_mac (arp_type, hwaddr_bin_buf, hwaddr_len); + goto out_good; + } + + if (nm_streq (client_id, "duid")) { + result = nm_utils_dhcp_client_id_systemd_node_specific (TRUE, + nm_device_get_ip_iface (self)); goto out_good; } if (nm_streq (client_id, "stable")) { + nm_auto_free_checksum GChecksum *sum = NULL; + guint8 digest[NM_UTILS_CHECKSUM_LENGTH_SHA1]; NMUtilsStableType stable_type; const char *stable_id; - GChecksum *sum; - guint8 buf[20]; - gsize buf_size; guint32 salted_header; - const guint8 *secret_key; - gsize secret_key_len; + const guint8 *host_id; + gsize host_id_len; stable_id = _get_stable_id (self, connection, &stable_type); if (!stable_id) @@ -7549,23 +7552,17 @@ dhcp4_get_client_id (NMDevice *self, salted_header = htonl (2011610591 + stable_type); - nm_utils_secret_key_get (&secret_key, &secret_key_len); + nm_utils_host_id_get (&host_id, &host_id_len); sum = g_checksum_new (G_CHECKSUM_SHA1); - g_checksum_update (sum, (const guchar *) &salted_header, sizeof (salted_header)); g_checksum_update (sum, (const guchar *) stable_id, strlen (stable_id) + 1); - g_checksum_update (sum, (const guchar *) secret_key, secret_key_len); - - buf_size = sizeof (buf); - g_checksum_get_digest (sum, buf, &buf_size); - nm_assert (buf_size == sizeof (buf)); - - g_checksum_free (sum); + g_checksum_update (sum, (const guchar *) host_id, host_id_len); + nm_utils_checksum_get_digest (sum, digest); client_id_buf = g_malloc (1 + 15); client_id_buf[0] = 0; - memcpy (&client_id_buf[1], buf, 15); + memcpy (&client_id_buf[1], digest, 15); result = g_bytes_new_take (client_id_buf, 1 + 15); goto out_good; } @@ -7741,34 +7738,26 @@ static gboolean connection_ip4_method_requires_carrier (NMConnection *connection, gboolean *out_ip4_enabled) { - const char *method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); - static const char *ip4_carrier_methods[] = { - NM_SETTING_IP4_CONFIG_METHOD_AUTO, - NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL, - NULL - }; + const char *method; - if (out_ip4_enabled) - *out_ip4_enabled = !!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED); - return g_strv_contains (ip4_carrier_methods, method); + method = nm_utils_get_ip_config_method (connection, AF_INET); + NM_SET_OUT (out_ip4_enabled, !nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED)); + return NM_IN_STRSET (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL); } static gboolean connection_ip6_method_requires_carrier (NMConnection *connection, gboolean *out_ip6_enabled) { - const char *method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG); - static const char *ip6_carrier_methods[] = { - NM_SETTING_IP6_CONFIG_METHOD_AUTO, - NM_SETTING_IP6_CONFIG_METHOD_DHCP, - NM_SETTING_IP6_CONFIG_METHOD_SHARED, - NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL, - NULL - }; + const char *method; - if (out_ip6_enabled) - *out_ip6_enabled = !!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE); - return g_strv_contains (ip6_carrier_methods, method); + method = nm_utils_get_ip_config_method (connection, AF_INET6); + NM_SET_OUT (out_ip6_enabled, !nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE)); + return NM_IN_STRSET (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP6_CONFIG_METHOD_DHCP, + NM_SETTING_IP6_CONFIG_METHOD_SHARED, + NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL); } static gboolean @@ -7837,12 +7826,12 @@ have_any_ready_slaves (NMDevice *self) } static gboolean -ip4_requires_slaves (NMConnection *connection) +ip4_requires_slaves (NMDevice *self) { const char *method; - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); - return strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0; + method = nm_device_get_effective_ip_config_method (self, AF_INET); + return nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO); } static NMActStageReturn @@ -7866,7 +7855,7 @@ act_stage3_ip4_config_start (NMDevice *self, return NM_ACT_STAGE_RETURN_IP_WAIT; } - if (nm_device_is_master (self) && ip4_requires_slaves (connection)) { + if (nm_device_is_master (self) && ip4_requires_slaves (self)) { /* If the master has no ready slaves, and depends on slaves for * a successful IPv4 attempt, then postpone IPv4 addressing. */ @@ -7877,7 +7866,8 @@ act_stage3_ip4_config_start (NMDevice *self, } } - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); + method = nm_device_get_effective_ip_config_method (self, AF_INET); + _LOGD (LOGD_IP4 | LOGD_DEVICE, "IPv4 config method is %s", method); if (NM_IN_STRSET (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO, @@ -7945,6 +7935,7 @@ dhcp6_cleanup (NMDevice *self, CleanupType cleanup_type, gboolean release) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); + priv->dhcp6.was_active = FALSE; priv->dhcp6.mode = NM_NDISC_DHCP_LEVEL_NONE; applied_config_clear (&priv->dhcp6.ip6_config); g_clear_pointer (&priv->dhcp6.event_id, g_free); @@ -8018,12 +8009,14 @@ dhcp6_grace_period_expired (gpointer user_data) } static void -dhcp6_fail (NMDevice *self, gboolean timeout) +dhcp6_fail (NMDevice *self, NMDhcpState dhcp_state) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); gboolean is_dhcp_managed; - _LOGD (LOGD_DHCP6, "DHCPv6 failed%s", timeout ? " (timeout)" : ""); + _LOGD (LOGD_DHCP6, "DHCPv6 failed (ip_state %s, was_active %d)", + _ip_state_to_string (priv->ip6_state), + priv->dhcp6.was_active); is_dhcp_managed = (priv->dhcp6.mode == NM_NDISC_DHCP_LEVEL_MANAGED); @@ -8036,11 +8029,14 @@ dhcp6_fail (NMDevice *self, gboolean timeout) && nm_ip6_config_get_num_addresses (priv->con_ip_config_6)) goto clear_config; - /* Fail the method in case of timeout or failure during initial - * configuration. + /* Fail the method when one of the following is true: + * 1) the DHCP client terminated: it does not make sense to start a grace + * period without a client running; + * 2) we failed to get an initial lease AND the client was + * not active before. */ - if ( !priv->dhcp6.was_active - && (timeout || priv->ip6_state == IP_CONF)) { + if ( dhcp_state == NM_DHCP_STATE_TERMINATED + || (!priv->dhcp6.was_active && priv->ip6_state == IP_CONF)) { dhcp6_cleanup (self, CLEANUP_TYPE_DECONFIGURE, FALSE); nm_device_activate_schedule_ip6_config_timeout (self); return; @@ -8077,21 +8073,6 @@ clear_config: } static void -dhcp6_timeout (NMDevice *self, NMDhcpClient *client) -{ - NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); - - if (priv->dhcp6.mode == NM_NDISC_DHCP_LEVEL_MANAGED) - dhcp6_fail (self, TRUE); - else { - /* not a hard failure; just live with the RA info */ - dhcp6_cleanup (self, CLEANUP_TYPE_DECONFIGURE, FALSE); - if (priv->ip6_state == IP_CONF) - nm_device_activate_schedule_ip6_config_result (self); - } -} - -static void dhcp6_state_changed (NMDhcpClient *client, NMDhcpState state, NMIP6Config *ip6_config, @@ -8148,17 +8129,24 @@ dhcp6_state_changed (NMDhcpClient *client, nm_device_activate_schedule_ip6_config_result (self); } else if (priv->ip6_state == IP_DONE) if (!dhcp6_lease_change (self)) - dhcp6_fail (self, FALSE); + dhcp6_fail (self, state); break; case NM_DHCP_STATE_TIMEOUT: - dhcp6_timeout (self, client); + if (priv->dhcp6.mode == NM_NDISC_DHCP_LEVEL_MANAGED) + dhcp6_fail (self, state); + else { + /* not a hard failure; just live with the RA info */ + dhcp6_cleanup (self, CLEANUP_TYPE_DECONFIGURE, FALSE); + if (priv->ip6_state == IP_CONF) + nm_device_activate_schedule_ip6_config_result (self); + } break; case NM_DHCP_STATE_EXPIRE: /* Ignore expiry before we even have a lease (NAK, old lease, etc) */ if (priv->ip6_state != IP_CONF) - dhcp6_fail (self, FALSE); + dhcp6_fail (self, state); break; - case NM_DHCP_STATE_DONE: + case NM_DHCP_STATE_TERMINATED: /* In IPv6 info-only mode, the client doesn't handle leases so it * may exit right after getting a response from the server. That's * normal. In that case we just ignore the exit. @@ -8166,8 +8154,9 @@ dhcp6_state_changed (NMDhcpClient *client, if (priv->dhcp6.mode == NM_NDISC_DHCP_LEVEL_OTHERCONF) break; /* fall through */ + case NM_DHCP_STATE_DONE: case NM_DHCP_STATE_FAIL: - dhcp6_fail (self, FALSE); + dhcp6_fail (self, state); break; default: break; @@ -8187,53 +8176,63 @@ dhcp6_prefix_delegated (NMDhcpClient *client, g_signal_emit (self, signals[IP6_PREFIX_DELEGATED], 0, prefix); } +/*****************************************************************************/ + /* RFC 3315 defines the epoch for the DUID-LLT time field on Jan 1st 2000. */ #define EPOCH_DATETIME_200001010000 946684800 static GBytes * -generate_duid_llt (const guint8 *hwaddr /* ETH_ALEN bytes */, +generate_duid_llt (int arp_type, + const guint8 *hwaddr, + gsize hwaddr_len, gint64 time) { guint8 *arr; const guint16 duid_type = htons (1); - const guint16 hw_type = htons (ARPHRD_ETHER); + const guint16 hw_type = htons (arp_type); const guint32 duid_time = htonl (NM_MAX (0, time - EPOCH_DATETIME_200001010000)); - arr = g_new (guint8, 2 + 2 + 4 + ETH_ALEN); + if (!nm_utils_arp_type_get_hwaddr_relevant_part (arp_type, &hwaddr, &hwaddr_len)) + nm_assert_not_reached (); + + arr = g_new (guint8, 2 + 2 + 4 + hwaddr_len); memcpy (&arr[0], &duid_type, 2); memcpy (&arr[2], &hw_type, 2); memcpy (&arr[4], &duid_time, 4); - memcpy (&arr[8], hwaddr, ETH_ALEN); + memcpy (&arr[8], hwaddr, hwaddr_len); - return g_bytes_new_take (arr, 2 + 2 + 4 + ETH_ALEN); + return g_bytes_new_take (arr, 2 + 2 + 4 + hwaddr_len); } static GBytes * -generate_duid_ll (const guint8 *hwaddr /* ETH_ALEN bytes */) +generate_duid_ll (int arp_type, + const guint8 *hwaddr, + gsize hwaddr_len) { guint8 *arr; const guint16 duid_type = htons (3); - const guint16 hw_type = htons (ARPHRD_ETHER); + const guint16 hw_type = htons (arp_type); + + if (!nm_utils_arp_type_get_hwaddr_relevant_part (arp_type, &hwaddr, &hwaddr_len)) + nm_assert_not_reached (); - arr = g_new (guint8, 2 + 2 + ETH_ALEN); + arr = g_new (guint8, 2 + 2 + hwaddr_len); memcpy (&arr[0], &duid_type, 2); memcpy (&arr[2], &hw_type, 2); - memcpy (&arr[4], hwaddr, ETH_ALEN); + memcpy (&arr[4], hwaddr, hwaddr_len); - return g_bytes_new_take (arr, 2 + 2 + ETH_ALEN); + return g_bytes_new_take (arr, 2 + 2 + hwaddr_len); } static GBytes * -generate_duid_uuid (guint8 *data, gsize data_len) +generate_duid_uuid (const NMUuid *uuid) { - const guint16 duid_type = g_htons (4); - const int DUID_SIZE = 18; + const guint16 duid_type = htons (4); guint8 *duid_buffer; - nm_assert (data); - nm_assert (data_len >= 16); + nm_assert (uuid); /* Generate a DHCP Unique Identifier for DHCPv6 using the * DUID-UUID method (see RFC 6355 section 4). Format is: @@ -8241,44 +8240,47 @@ generate_duid_uuid (guint8 *data, gsize data_len) * u16: type (DUID-UUID = 4) * u8[16]: UUID bytes */ - duid_buffer = g_malloc (DUID_SIZE); - G_STATIC_ASSERT_EXPR (sizeof (duid_type) == 2); + G_STATIC_ASSERT_EXPR (sizeof (*uuid) == 16); + duid_buffer = g_malloc (18); memcpy (&duid_buffer[0], &duid_type, 2); - - /* UUID is 128 bits, we just take the first 128 bits - * (regardless of data size) as the DUID-UUID. - */ - memcpy (&duid_buffer[2], data, 16); - - return g_bytes_new_take (duid_buffer, DUID_SIZE); + memcpy (&duid_buffer[2], uuid, 16); + return g_bytes_new_take (duid_buffer, 18); } static GBytes * generate_duid_from_machine_id (void) { - gs_free const char *machine_id_s = NULL; - uuid_t uuid; - GChecksum *sum; - guint8 sha256_digest[32]; - gsize len = sizeof (sha256_digest); - static GBytes *global_duid = NULL; + static GBytes *volatile global_duid = NULL; + GBytes *p; - if (global_duid) - return g_bytes_ref (global_duid); +again: + p = g_atomic_pointer_get (&global_duid); + if (G_UNLIKELY (!p)) { + nm_auto_free_checksum GChecksum *sum = NULL; + const NMUuid *machine_id; + union { + guint8 sha256[NM_UTILS_CHECKSUM_LENGTH_SHA256]; + NMUuid uuid; + } digest; + + machine_id = nm_utils_machine_id_bin (); + + /* Hash the machine ID so it's not leaked to the network */ + sum = g_checksum_new (G_CHECKSUM_SHA256); + g_checksum_update (sum, (const guchar *) machine_id, sizeof (*machine_id)); + nm_utils_checksum_get_digest (sum, digest.sha256); - machine_id_s = nm_utils_machine_id_read (); - if (!nm_utils_machine_id_parse (machine_id_s, uuid)) - return NULL; + G_STATIC_ASSERT_EXPR (sizeof (digest.sha256) > sizeof (digest.uuid)); + p = generate_duid_uuid (&digest.uuid); - /* Hash the machine ID so it's not leaked to the network */ - sum = g_checksum_new (G_CHECKSUM_SHA256); - g_checksum_update (sum, (const guchar *) &uuid, sizeof (uuid)); - g_checksum_get_digest (sum, sha256_digest, &len); - g_checksum_free (sum); + if (!g_atomic_pointer_compare_and_exchange (&global_duid, NULL, p)) { + g_bytes_unref (p); + goto again; + } + } - global_duid = generate_duid_uuid (sha256_digest, len); - return g_bytes_ref (global_duid); + return g_bytes_ref (p); } static GBytes * @@ -8289,17 +8291,19 @@ dhcp6_get_duid (NMDevice *self, NMConnection *connection, GBytes *hwaddr, gboole gs_free char *duid_default = NULL; const char *duid_error; GBytes *duid_out; - guint8 sha256_digest[32]; - gsize len = sizeof (sha256_digest); gboolean duid_enforce = TRUE; gs_free char *logstr1 = NULL; + const guint8 *hwaddr_bin; + gsize hwaddr_len; + int arp_type; s_ip6 = nm_connection_get_setting_ip6_config (connection); duid = nm_setting_ip6_config_get_dhcp_duid (NM_SETTING_IP6_CONFIG (s_ip6)); if (!duid) { duid_default = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA, - "ipv6.dhcp-duid", self); + NM_CON_DEFAULT ("ipv6.dhcp-duid"), + self); duid = duid_default; if (!duid) duid = "lease"; @@ -8308,10 +8312,6 @@ dhcp6_get_duid (NMDevice *self, NMConnection *connection, GBytes *hwaddr, gboole if (nm_streq (duid, "lease")) { duid_enforce = FALSE; duid_out = generate_duid_from_machine_id (); - if (!duid_out) { - duid_error = "failure to read machine-id"; - goto out_fail; - } goto out_good; } @@ -8328,78 +8328,138 @@ dhcp6_get_duid (NMDevice *self, NMConnection *connection, GBytes *hwaddr, gboole duid_error = "missing link-layer address"; goto out_fail; } - if (g_bytes_get_size (hwaddr) != ETH_ALEN) { + + hwaddr_bin = g_bytes_get_data (hwaddr, &hwaddr_len); + arp_type = nm_utils_arp_type_detect_from_hwaddrlen (hwaddr_len); + if (arp_type < 0) { duid_error = "unsupported link-layer address"; goto out_fail; } - if (nm_streq (duid, "ll")) { - duid_out = generate_duid_ll (g_bytes_get_data (hwaddr, NULL)); - } else { - gint64 time; - - time = nm_utils_secret_key_get_timestamp (); - if (!time) { - duid_error = "cannot retrieve the secret key timestamp"; - goto out_fail; - } - - duid_out = generate_duid_llt (g_bytes_get_data (hwaddr, NULL), time); + if (nm_streq (duid, "ll")) + duid_out = generate_duid_ll (arp_type, hwaddr_bin, hwaddr_len); + else { + duid_out = generate_duid_llt (arp_type, hwaddr_bin, hwaddr_len, + nm_utils_host_id_get_timestamp_ns () / NM_UTILS_NS_PER_SECOND); } goto out_good; } if (NM_IN_STRSET (duid, "stable-ll", "stable-llt", "stable-uuid")) { + /* preferably, we would salt the checksum differently for each @duid type. We missed + * to do that initially, so most types use the DEFAULT_SALT. + * + * Implemenations that are added later, should use a distinct salt instead, + * like "stable-ll"/"stable-llt" with ARPHRD_INFINIBAND below. */ + const guint32 DEFAULT_SALT = 670531087u; + nm_auto_free_checksum GChecksum *sum = NULL; NMUtilsStableType stable_type; const char *stable_id = NULL; guint32 salted_header; - GChecksum *sum; - const guint8 *secret_key; - gsize secret_key_len; + const guint8 *host_id; + gsize host_id_len; + union { + guint8 sha256[NM_UTILS_CHECKSUM_LENGTH_SHA256]; + guint8 hwaddr_eth[ETH_ALEN]; + guint8 hwaddr_infiniband[INFINIBAND_ALEN]; + NMUuid uuid; + struct _nm_packed { + guint8 hwaddr[ETH_ALEN]; + guint32 timestamp; + } llt_eth; + struct _nm_packed { + guint8 hwaddr[INFINIBAND_ALEN]; + guint32 timestamp; + } llt_infiniband; + } digest; stable_id = _get_stable_id (self, connection, &stable_type); if (!stable_id) g_return_val_if_reached (NULL); - salted_header = htonl (670531087 + stable_type); + if (NM_IN_STRSET (duid, "stable-ll", "stable-llt")) { + /* for stable LL/LLT DUIDs, we still need a hardware address to detect + * the arp-type. Alternatively, we would be able to detect it based on + * other means (e.g. NMDevice type), but instead require the hardware + * address to be present. This is at least consistent with the "ll"/"llt" + * modes above. */ + if (!hwaddr) { + duid_error = "missing link-layer address"; + goto out_fail; + } + if ((arp_type = nm_utils_arp_type_detect_from_hwaddrlen (g_bytes_get_size (hwaddr))) < 0) { + duid_error = "unsupported link-layer address"; + goto out_fail; + } - nm_utils_secret_key_get (&secret_key, &secret_key_len); + if (arp_type == ARPHRD_ETHER) + salted_header = DEFAULT_SALT; + else { + nm_assert (arp_type == ARPHRD_INFINIBAND); + salted_header = 0x42492CEFu + ((guint32) arp_type); + } + } else { + salted_header = DEFAULT_SALT; + arp_type = -1; + } - sum = g_checksum_new (G_CHECKSUM_SHA256); + salted_header = htonl (salted_header + ((guint32) stable_type)); + + nm_utils_host_id_get (&host_id, &host_id_len); + sum = g_checksum_new (G_CHECKSUM_SHA256); g_checksum_update (sum, (const guchar *) &salted_header, sizeof (salted_header)); g_checksum_update (sum, (const guchar *) stable_id, -1); - g_checksum_update (sum, (const guchar *) secret_key, secret_key_len); + g_checksum_update (sum, (const guchar *) host_id, host_id_len); + nm_utils_checksum_get_digest (sum, digest.sha256); - g_checksum_get_digest (sum, sha256_digest, &len); - g_checksum_free (sum); + G_STATIC_ASSERT_EXPR (sizeof (digest) == sizeof (digest.sha256)); if (nm_streq (duid, "stable-ll")) { - duid_out = generate_duid_ll (sha256_digest); + switch (arp_type) { + case ARPHRD_ETHER: + duid_out = generate_duid_ll (arp_type, digest.hwaddr_eth, sizeof (digest.hwaddr_eth)); + break; + case ARPHRD_INFINIBAND: + duid_out = generate_duid_ll (arp_type, digest.hwaddr_infiniband, sizeof (digest.hwaddr_infiniband)); + break; + default: + g_return_val_if_reached (NULL); + } } else if (nm_streq (duid, "stable-llt")) { gint64 time; + guint32 timestamp; #define EPOCH_DATETIME_THREE_YEARS (356 * 24 * 3600 * 3) - /* We want a variable time between the secret_key timestamp and three years + /* We want a variable time between the host_id timestamp and three years * before. Let's compute the time (in seconds) from 0 to 3 years; then we'll - * subtract it from the secret_key timestamp. + * subtract it from the host_id timestamp. */ - time = nm_utils_secret_key_get_timestamp (); - if (!time) { - duid_error = "cannot retrieve the secret key timestamp"; - goto out_fail; - } + time = nm_utils_host_id_get_timestamp_ns () / NM_UTILS_NS_PER_SECOND; + /* don't use too old timestamps. They cannot be expressed in DUID-LLT and * would all be truncated to zero. */ time = NM_MAX (time, EPOCH_DATETIME_200001010000 + EPOCH_DATETIME_THREE_YEARS); - time -= (unaligned_read_be32 (&sha256_digest[ETH_ALEN]) % EPOCH_DATETIME_THREE_YEARS); - duid_out = generate_duid_llt (sha256_digest, time); + switch (arp_type) { + case ARPHRD_ETHER: + timestamp = unaligned_read_be32 (&digest.llt_eth.timestamp); + time -= timestamp % EPOCH_DATETIME_THREE_YEARS; + duid_out = generate_duid_llt (arp_type, digest.llt_eth.hwaddr, sizeof (digest.llt_eth.hwaddr), time); + break; + case ARPHRD_INFINIBAND: + timestamp = unaligned_read_be32 (&digest.llt_infiniband.timestamp); + time -= timestamp % EPOCH_DATETIME_THREE_YEARS; + duid_out = generate_duid_llt (arp_type, digest.llt_infiniband.hwaddr, sizeof (digest.llt_infiniband.hwaddr), time); + break; + default: + g_return_val_if_reached (NULL); + } } else { nm_assert (nm_streq (duid, "stable-uuid")); - duid_out = generate_duid_uuid (sha256_digest, len); + duid_out = generate_duid_uuid (&digest.uuid); } goto out_good; @@ -8410,14 +8470,14 @@ dhcp6_get_duid (NMDevice *self, NMConnection *connection, GBytes *hwaddr, gboole out_fail: nm_assert (!duid_out && duid_error); { - guint8 uuid[16]; + NMUuid uuid; _LOGW (LOGD_IP6 | LOGD_DHCP6, "ipv6.dhcp-duid: failure to generate %s DUID: %s. Fallback to random DUID-UUID.", duid, duid_error); - nm_utils_random_bytes (uuid, sizeof (uuid)); - duid_out = generate_duid_uuid (uuid, sizeof (uuid)); + nm_utils_random_bytes (&uuid, sizeof (uuid)); + duid_out = generate_duid_uuid (&uuid); } out_good: @@ -8432,6 +8492,8 @@ out_good: return duid_out; } +/*****************************************************************************/ + static gboolean dhcp6_start_with_link_ready (NMDevice *self, NMConnection *connection) { @@ -8593,6 +8655,7 @@ nm_device_use_ip6_subnet (NMDevice *self, const NMPlatformIP6Address *subnet) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); NMPlatformIP6Address address = *subnet; + char sbuf[NM_UTILS_INET_ADDRSTRLEN]; if (!applied_config_get_current (&priv->ac_ip6_config)) applied_config_init_new (&priv->ac_ip6_config, self, AF_INET6); @@ -8602,7 +8665,7 @@ nm_device_use_ip6_subnet (NMDevice *self, const NMPlatformIP6Address *subnet) applied_config_add_address (&priv->ac_ip6_config, NM_PLATFORM_IP_ADDRESS_CAST (&address)); _LOGD (LOGD_IP6, "ipv6-pd: using %s address (preferred for %u seconds)", - nm_utils_inet6_ntop (&address.address, NULL), + nm_utils_inet6_ntop (&address.address, sbuf), subnet->preferred); /* This also updates the ndisc if there are actual changes. */ @@ -8694,19 +8757,19 @@ linklocal6_check_complete (NMDevice *self) connection = nm_device_get_applied_connection (self); g_assert (connection); - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG); + method = nm_device_get_effective_ip_config_method (self, AF_INET6); _LOGD (LOGD_DEVICE, "linklocal6: waiting for link-local addresses successful, continue with method %s", method); - if ( strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0 - || strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_SHARED) == 0) + if (NM_IN_STRSET (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP6_CONFIG_METHOD_SHARED)) addrconf6_start_with_link_ready (self); - else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP) == 0) { + else if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP)) { if (!dhcp6_start_with_link_ready (self, connection)) { /* Time out IPv6 instead of failing the entire activation */ nm_device_activate_schedule_ip6_config_timeout (self); } - } else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0) + } else if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL)) nm_device_activate_schedule_ip6_config_result (self); else g_return_if_fail (FALSE); @@ -8721,6 +8784,7 @@ check_and_add_ipv6ll_addr (NMDevice *self) NMSettingIP6Config *s_ip6 = NULL; GError *error = NULL; const char *addr_type; + char sbuf[NM_UTILS_INET_ADDRSTRLEN]; if (!priv->ipv6ll_handle) return; @@ -8781,7 +8845,8 @@ check_and_add_ipv6ll_addr (NMDevice *self) addr_type = "EUI-64"; } - _LOGD (LOGD_IP6, "linklocal6: generated %s IPv6LL address %s", addr_type, nm_utils_inet6_ntop (&lladdr, NULL)); + _LOGD (LOGD_IP6, "linklocal6: generated %s IPv6LL address %s", + addr_type, nm_utils_inet6_ntop (&lladdr, sbuf)); priv->ipv6ll_has = TRUE; priv->ipv6ll_addr = lladdr; ip_config_merge_and_apply (self, AF_INET6, TRUE); @@ -8791,8 +8856,6 @@ static gboolean linklocal6_start (NMDevice *self) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); - NMConnection *connection; - const char *method; nm_clear_g_source (&priv->linklocal6_timeout_id); @@ -8802,11 +8865,8 @@ linklocal6_start (NMDevice *self) | NM_PLATFORM_MATCH_WITH_ADDRSTATE_NORMAL)) return TRUE; - connection = nm_device_get_applied_connection (self); - g_assert (connection); - - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG); - _LOGD (LOGD_DEVICE, "linklocal6: starting IPv6 with method '%s', but the device has no link-local addresses configured. Wait.", method); + _LOGD (LOGD_DEVICE, "linklocal6: starting IPv6 with method '%s', but the device has no link-local addresses configured. Wait.", + nm_device_get_effective_ip_config_method (self, AF_INET6)); check_and_add_ipv6ll_addr (self); @@ -8854,19 +8914,19 @@ nm_device_get_configured_mtu_from_connection (NMDevice *self, if (setting_type == NM_TYPE_SETTING_WIRED) { if (setting) mtu = nm_setting_wired_get_mtu (NM_SETTING_WIRED (setting)); - global_property_name = "ethernet.mtu"; + global_property_name = NM_CON_DEFAULT ("ethernet.mtu"); } else if (setting_type == NM_TYPE_SETTING_WIRELESS) { if (setting) mtu = nm_setting_wireless_get_mtu (NM_SETTING_WIRELESS (setting)); - global_property_name = "wifi.mtu"; + global_property_name = NM_CON_DEFAULT ("wifi.mtu"); } else if (setting_type == NM_TYPE_SETTING_INFINIBAND) { if (setting) mtu = nm_setting_infiniband_get_mtu (NM_SETTING_INFINIBAND (setting)); - global_property_name = "infiniband.mtu"; + global_property_name = NM_CON_DEFAULT ("infiniband.mtu"); } else if (setting_type == NM_TYPE_SETTING_IP_TUNNEL) { if (setting) mtu = nm_setting_ip_tunnel_get_mtu (NM_SETTING_IP_TUNNEL (setting)); - global_property_name = "ip-tunnel.mtu"; + global_property_name = NM_CON_DEFAULT ("ip-tunnel.mtu"); } else g_return_val_if_reached (0); @@ -8946,7 +9006,7 @@ _commit_mtu (NMDevice *self, const NMIP4Config *config) { guint32 mtu = 0; - /* preferably, get the MTU from explict user-configuration. + /* preferably, get the MTU from explicit user-configuration. * Only if that fails, look at the current @config (which contains * MTUs from DHCP/PPP) or maybe fallback to a device-specific MTU. */ @@ -8981,7 +9041,7 @@ _commit_mtu (NMDevice *self, const NMIP4Config *config) if (mtu_desired && mtu_desired < 1280) { NMSettingIPConfig *s_ip6; - s_ip6 = (NMSettingIPConfig *) nm_device_get_applied_setting (self, NM_TYPE_SETTING_IP6_CONFIG); + s_ip6 = nm_device_get_applied_setting (self, NM_TYPE_SETTING_IP6_CONFIG); if ( s_ip6 && !NM_IN_STRSET (nm_setting_ip_config_get_method (s_ip6), NM_SETTING_IP6_CONFIG_METHOD_IGNORE)) { @@ -9036,7 +9096,7 @@ _commit_mtu (NMDevice *self, const NMIP4Config *config) #define _IP6_MTU_SYS() \ ({ \ if (!ip6_mtu_sysctl.initialized) { \ - ip6_mtu_sysctl.value = nm_device_ipv6_sysctl_get_uint32 (self, "mtu", 0); \ + ip6_mtu_sysctl.value = nm_device_sysctl_ip_conf_get_int_checked (self, AF_INET6, "mtu", 10, 0, G_MAXUINT32, 0); \ ip6_mtu_sysctl.initialized = TRUE; \ } \ ip6_mtu_sysctl.value; \ @@ -9053,7 +9113,10 @@ _commit_mtu (NMDevice *self, const NMIP4Config *config) } if (mtu_desired && mtu_desired != mtu_plat) { - if (nm_platform_link_set_mtu (nm_device_get_platform (self), ifindex, mtu_desired) == NM_PLATFORM_ERROR_CANT_SET_MTU) { + int r; + + r = nm_platform_link_set_mtu (nm_device_get_platform (self), ifindex, mtu_desired); + if (r == -NME_PL_CANT_SET_MTU) { anticipated_failure = TRUE; success = FALSE; _LOGW (LOGD_DEVICE, "mtu: failure to set MTU. %s", @@ -9067,8 +9130,8 @@ _commit_mtu (NMDevice *self, const NMIP4Config *config) } if (ip6_mtu && ip6_mtu != _IP6_MTU_SYS ()) { - if (!nm_device_ipv6_sysctl_set (self, "mtu", - nm_sprintf_buf (sbuf, "%u", (unsigned) ip6_mtu))) { + if (!nm_device_sysctl_ip_conf_set (self, AF_INET6, "mtu", + nm_sprintf_buf (sbuf, "%u", (unsigned) ip6_mtu))) { int errsv = errno; _NMLOG (anticipated_failure && errsv == EINVAL ? LOGL_DEBUG : LOGL_WARN, @@ -9210,7 +9273,7 @@ ndisc_config_changed (NMNDisc *ndisc, const NMNDiscData *rdata, guint changed_in } if (changed & NM_NDISC_CONFIG_HOP_LIMIT) - nm_platform_sysctl_set_ip6_hop_limit_safe (nm_device_get_platform (self), nm_device_get_ip_iface (self), rdata->hop_limit); + nm_platform_sysctl_ip_conf_set_ipv6_hop_limit_safe (nm_device_get_platform (self), nm_device_get_ip_iface (self), rdata->hop_limit); if (changed & NM_NDISC_CONFIG_MTU) { if (priv->ip6_mtu != rdata->mtu) { @@ -9266,7 +9329,7 @@ addrconf6_start_with_link_ready (NMDevice *self) } else { /* Don't abort the addrconf at this point -- if ndisc needs the iid * it will notice this itself. */ - _LOGI (LOGD_IP6, "addrconf6: no interface identifier; IPv6 adddress creation may fail"); + _LOGI (LOGD_IP6, "addrconf6: no interface identifier; IPv6 address creation may fail"); } /* Apply any manual configuration before starting RA */ @@ -9277,14 +9340,14 @@ addrconf6_start_with_link_ready (NMDevice *self) switch (nm_ndisc_get_node_type (priv->ndisc)) { case NM_NDISC_NODE_TYPE_HOST: /* Accepting prefixes from discovered routers. */ - nm_device_ipv6_sysctl_set (self, "accept_ra", "1"); - nm_device_ipv6_sysctl_set (self, "accept_ra_defrtr", "0"); - nm_device_ipv6_sysctl_set (self, "accept_ra_pinfo", "0"); - nm_device_ipv6_sysctl_set (self, "accept_ra_rtr_pref", "0"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "accept_ra", "1"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "accept_ra_defrtr", "0"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "accept_ra_pinfo", "0"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "accept_ra_rtr_pref", "0"); break; case NM_NDISC_NODE_TYPE_ROUTER: /* We're the router. */ - nm_device_ipv6_sysctl_set (self, "forwarding", "1"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "forwarding", "1"); nm_device_activate_schedule_ip6_config_result (self); priv->needs_ip6_subnet = TRUE; g_signal_emit (self, signals[IP6_SUBNET_NEEDED], 0); @@ -9304,22 +9367,17 @@ addrconf6_start_with_link_ready (NMDevice *self) ndisc_set_router_config (priv->ndisc, self); nm_ndisc_start (priv->ndisc); + priv->ndisc_started = TRUE; return; } static NMNDiscNodeType ndisc_node_type (NMDevice *self) { - NMConnection *connection; - - connection = nm_device_get_applied_connection (self); - g_assert (connection); - - if (strcmp (nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG), - NM_SETTING_IP4_CONFIG_METHOD_SHARED) == 0) + if (nm_streq (nm_device_get_effective_ip_config_method (self, AF_INET6), + NM_SETTING_IP4_CONFIG_METHOD_SHARED)) return NM_NDISC_NODE_TYPE_ROUTER; - else - return NM_NDISC_NODE_TYPE_HOST; + return NM_NDISC_NODE_TYPE_HOST; } static gboolean @@ -9388,6 +9446,7 @@ addrconf6_cleanup (NMDevice *self) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); + priv->ndisc_started = FALSE; nm_clear_g_signal_handler (priv->ndisc, &priv->ndisc_changed_id); nm_clear_g_signal_handler (priv->ndisc, &priv->ndisc_timeout_id); @@ -9401,34 +9460,36 @@ addrconf6_cleanup (NMDevice *self) /*****************************************************************************/ -static const char *ip6_properties_to_save[] = { - "accept_ra", - "accept_ra_defrtr", - "accept_ra_pinfo", - "accept_ra_rtr_pref", - "forwarding", - "disable_ipv6", - "hop_limit", - "use_tempaddr", -}; - static void save_ip6_properties (NMDevice *self) { + static const char *const ip6_properties_to_save[] = { + "accept_ra", + "accept_ra_defrtr", + "accept_ra_pinfo", + "accept_ra_rtr_pref", + "forwarding", + "disable_ipv6", + "hop_limit", + "use_tempaddr", + }; NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); - const char *ifname = nm_device_get_ip_iface (self); + NMPlatform *platform = nm_device_get_platform (self); + const char *ifname; char *value; int i; g_hash_table_remove_all (priv->ip6_saved_properties); - if (!nm_device_get_ip_ifindex (self)) + ifname = nm_device_get_ip_iface_from_platform (self); + if (!ifname) return; for (i = 0; i < G_N_ELEMENTS (ip6_properties_to_save); i++) { - char buf[NM_UTILS_SYSCTL_IP_CONF_PATH_BUFSIZE]; - - value = nm_platform_sysctl_get (nm_device_get_platform (self), NMP_SYSCTL_PATHID_ABSOLUTE (nm_utils_sysctl_ip_conf_path (AF_INET6, buf, ifname, ip6_properties_to_save[i]))); + value = nm_platform_sysctl_ip_conf_get (platform, + AF_INET6, + ifname, + ip6_properties_to_save[i]); if (value) { g_hash_table_insert (priv->ip6_saved_properties, (char *) ip6_properties_to_save[i], @@ -9450,7 +9511,7 @@ restore_ip6_properties (NMDevice *self) if ( priv->ipv6ll_handle && nm_streq (key, "disable_ipv6")) continue; - nm_device_ipv6_sysctl_set (self, key, value); + nm_device_sysctl_ip_conf_set (self, AF_INET6, key, value); } } @@ -9459,7 +9520,7 @@ set_disable_ipv6 (NMDevice *self, const char *value) { /* We only touch disable_ipv6 when NM is not managing the IPv6LL address */ if (!NM_DEVICE_GET_PRIVATE (self)->ipv6ll_handle) - nm_device_ipv6_sysctl_set (self, "disable_ipv6", value); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "disable_ipv6", value); } static inline void @@ -9467,7 +9528,6 @@ set_nm_ipv6ll (NMDevice *self, gboolean enable) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); int ifindex = nm_device_get_ip_ifindex (self); - char *value; if (!nm_platform_check_kernel_support (nm_device_get_platform (self), NM_PLATFORM_KERNEL_SUPPORT_USER_IPV6LL)) @@ -9475,32 +9535,34 @@ set_nm_ipv6ll (NMDevice *self, gboolean enable) priv->ipv6ll_handle = enable; if (ifindex > 0) { - NMPlatformError plerr; const char *detail = enable ? "enable" : "disable"; + int r; _LOGD (LOGD_IP6, "will %s userland IPv6LL", detail); - plerr = nm_platform_link_set_user_ipv6ll_enabled (nm_device_get_platform (self), ifindex, enable); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { - _NMLOG (( plerr == NM_PLATFORM_ERROR_NOT_FOUND - || plerr == NM_PLATFORM_ERROR_OPNOTSUPP) ? LOGL_DEBUG : LOGL_WARN, + r = nm_platform_link_set_user_ipv6ll_enabled (nm_device_get_platform (self), ifindex, enable); + if (r < 0) { + _NMLOG ( NM_IN_SET (r, -NME_PL_NOT_FOUND + -NME_PL_OPNOTSUPP) + ? LOGL_DEBUG + : LOGL_WARN, LOGD_IP6, "failed to %s userspace IPv6LL address handling (%s)", detail, - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); } if (enable) { - char buf[NM_UTILS_SYSCTL_IP_CONF_PATH_BUFSIZE]; + gs_free char *value = NULL; /* Bounce IPv6 to ensure the kernel stops IPv6LL address generation */ - value = nm_platform_sysctl_get (nm_device_get_platform (self), - NMP_SYSCTL_PATHID_ABSOLUTE (nm_utils_sysctl_ip_conf_path (AF_INET6, buf, nm_device_get_ip_iface (self), "disable_ipv6"))); - if (g_strcmp0 (value, "0") == 0) - nm_device_ipv6_sysctl_set (self, "disable_ipv6", "1"); - g_free (value); + value = nm_device_sysctl_ip_conf_get (self, + AF_INET6, + "disable_ipv6"); + if (nm_streq0 (value, "0")) + nm_device_sysctl_ip_conf_set (self, AF_INET6, "disable_ipv6", "1"); /* Ensure IPv6 is enabled */ - nm_device_ipv6_sysctl_set (self, "disable_ipv6", "0"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "disable_ipv6", "0"); } } @@ -9545,7 +9607,7 @@ _ip6_privacy_get (NMDevice *self) /* 2.) use the default value from the configuration. */ ip6_privacy = nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA, - "ipv6.ip6-privacy", + NM_CON_DEFAULT ("ipv6.ip6-privacy"), self, NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN, NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR, @@ -9561,24 +9623,26 @@ _ip6_privacy_get (NMDevice *self) * Instead of reading static config files in /etc, just read the current sysctl value. * This works as NM only writes to "/proc/sys/net/ipv6/conf/IFNAME/use_tempaddr", but leaves * the "default" entry untouched. */ - ip6_privacy = nm_platform_sysctl_get_int32 (nm_device_get_platform (self), NMP_SYSCTL_PATHID_ABSOLUTE ("/proc/sys/net/ipv6/conf/default/use_tempaddr"), NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN); + ip6_privacy = nm_platform_sysctl_get_int32 (nm_device_get_platform (self), + NMP_SYSCTL_PATHID_ABSOLUTE ("/proc/sys/net/ipv6/conf/default/use_tempaddr"), + NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN); return _ip6_privacy_clamp (ip6_privacy); } /*****************************************************************************/ static gboolean -ip6_requires_slaves (NMConnection *connection) +ip6_requires_slaves (NMDevice *self) { const char *method; - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG); + method = nm_device_get_effective_ip_config_method (self, AF_INET6); /* SLAAC, DHCP, and Link-Local depend on connectivity (and thus slaves) * to complete addressing. SLAAC and DHCP need a peer to provide a prefix. */ - return strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0 - || strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP) == 0; + return NM_IN_STRSET (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP6_CONFIG_METHOD_DHCP); } static NMActStageReturn @@ -9604,7 +9668,7 @@ act_stage3_ip6_config_start (NMDevice *self, return NM_ACT_STAGE_RETURN_IP_WAIT; } - if (nm_device_is_master (self) && ip6_requires_slaves (connection)) { + if (nm_device_is_master (self) && ip6_requires_slaves (self)) { /* If the master has no ready slaves, and depends on slaves for * a successful IPv6 attempt, then postpone IPv6 addressing. */ @@ -9616,9 +9680,8 @@ act_stage3_ip6_config_start (NMDevice *self, } priv->dhcp6.mode = NM_NDISC_DHCP_LEVEL_NONE; - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG); - - if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) == 0) { + method = nm_device_get_effective_ip_config_method (self, AF_INET6); + if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE)) { if ( !priv->master && !nm_device_sys_iface_state_is_external (self)) { gboolean ipv6ll_handle_old = priv->ipv6ll_handle; @@ -9629,7 +9692,7 @@ act_stage3_ip6_config_start (NMDevice *self, */ set_nm_ipv6ll (self, FALSE); if (ipv6ll_handle_old) - nm_device_ipv6_sysctl_set (self, "disable_ipv6", "1"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "disable_ipv6", "1"); restore_ip6_properties (self); } return NM_ACT_STAGE_RETURN_IP_DONE; @@ -9664,27 +9727,27 @@ act_stage3_ip6_config_start (NMDevice *self, ip6_privacy = _ip6_privacy_get (self); - if ( strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0 - || strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_SHARED) == 0) { + if (NM_IN_STRSET (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP6_CONFIG_METHOD_SHARED)) { if (!addrconf6_start (self, ip6_privacy)) { /* IPv6 might be disabled; allow IPv4 to proceed */ ret = NM_ACT_STAGE_RETURN_IP_FAIL; } else ret = NM_ACT_STAGE_RETURN_POSTPONE; - } else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0) { + } else if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL)) { ret = linklocal6_start (self) ? NM_ACT_STAGE_RETURN_SUCCESS : NM_ACT_STAGE_RETURN_POSTPONE; - } else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP) == 0) { + } else if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP)) { priv->dhcp6.mode = NM_NDISC_DHCP_LEVEL_MANAGED; if (!dhcp6_start (self, TRUE)) { /* IPv6 might be disabled; allow IPv4 to proceed */ ret = NM_ACT_STAGE_RETURN_IP_FAIL; } else ret = NM_ACT_STAGE_RETURN_POSTPONE; - } else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL) == 0) { + } else if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) ret = NM_ACT_STAGE_RETURN_SUCCESS; - } else + else _LOGW (LOGD_IP6, "unhandled IPv6 config method '%s'; will fail", method); if ( ret != NM_ACT_STAGE_RETURN_FAILURE @@ -9701,7 +9764,7 @@ act_stage3_ip6_config_start (NMDevice *self, ip6_privacy_str = "2"; break; } - nm_device_ipv6_sysctl_set (self, "use_tempaddr", ip6_privacy_str); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "use_tempaddr", ip6_privacy_str); } return ret; @@ -9814,6 +9877,8 @@ nm_device_activate_stage3_ip6_start (NMDevice *self) static void activate_stage3_ip_config_start (NMDevice *self) { + int ifindex; + _set_ip_state (self, AF_INET, IP_WAIT); _set_ip_state (self, AF_INET6, IP_WAIT); @@ -9823,7 +9888,8 @@ activate_stage3_ip_config_start (NMDevice *self) nm_device_state_changed (self, NM_DEVICE_STATE_IP_CONFIG, NM_DEVICE_STATE_REASON_NONE); /* Device should be up before we can do anything with it */ - if (!nm_platform_link_is_up (nm_device_get_platform (self), nm_device_get_ip_ifindex (self))) + if ( (ifindex = nm_device_get_ip_ifindex (self)) > 0 + && !nm_platform_link_is_up (nm_device_get_platform (self), ifindex)) _LOGW (LOGD_DEVICE, "interface %s not up for IP configuration", nm_device_get_ip_iface (self)); /* IPv4 */ @@ -10108,6 +10174,9 @@ start_sharing (NMDevice *self, NMIP4Config *config, GError **error) const NMPlatformIP4Address *ip4_addr = NULL; const char *ip_iface; GError *local = NULL; + NMConnection *conn; + NMSettingConnection *s_con; + gboolean announce_android_metered; g_return_val_if_fail (config, FALSE); @@ -10129,7 +10198,7 @@ start_sharing (NMDevice *self, NMIP4Config *config, GError **error) return FALSE; req = nm_device_get_act_request (self); - g_assert (req); + g_return_val_if_fail (req, FALSE); netmask = _nm_utils_ip4_prefix_to_netmask (ip4_addr->plen); nm_utils_inet4_ntop (netmask, str_mask); @@ -10150,7 +10219,35 @@ start_sharing (NMDevice *self, NMIP4Config *config, GError **error) nm_act_request_set_shared (req, TRUE); - if (!nm_dnsmasq_manager_start (priv->dnsmasq_manager, config, &local)) { + conn = nm_act_request_get_applied_connection (req); + s_con = nm_connection_get_setting_connection (conn); + + switch (nm_setting_connection_get_metered (s_con)) { + case NM_METERED_YES: + /* honor the metered flag. Note that reapply on the device does not affect + * the metered setting. This is different from other profiles, where the + * metered flag of an activated profile can be changed (reapplied). */ + announce_android_metered = TRUE; + break; + case NM_METERED_UNKNOWN: + /* we pick up the current value and announce it. But again, we cannot update + * the announced setting without restarting dnsmasq. That means, if the default + * route changes w.r.t. being metered, then the shared connection does not get + * updated before reactivating. */ + announce_android_metered = NM_IN_SET (nm_manager_get_metered (nm_manager_get ()), + NM_METERED_YES, + NM_METERED_GUESS_YES); + break; + default: + announce_android_metered = FALSE; + break; + } + + + if (!nm_dnsmasq_manager_start (priv->dnsmasq_manager, + config, + announce_android_metered, + &local)) { g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, "could not start dnsmasq due to %s", local->message); g_error_free (local); @@ -10169,10 +10266,7 @@ arp_cleanup (NMDevice *self) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); - if (priv->acd.announcing) { - nm_acd_manager_destroy (priv->acd.announcing); - priv->acd.announcing = NULL; - } + nm_clear_pointer (&priv->acd.announcing, nm_acd_manager_free); } void @@ -10207,7 +10301,11 @@ nm_device_arp_announce (NMDevice *self) if (num == 0) return; - priv->acd.announcing = nm_acd_manager_new (nm_device_get_ip_ifindex (self), hw_addr, hw_addr_len); + priv->acd.announcing = nm_acd_manager_new (nm_device_get_ip_ifindex (self), + hw_addr, + hw_addr_len, + NULL, + NULL); for (i = 0; i < num; i++) { NMIPAddress *ip = nm_setting_ip_config_get_address (s_ip4, i); @@ -10228,13 +10326,10 @@ activate_stage5_ip4_config_result (NMDevice *self) NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); NMActRequest *req; const char *method; - NMConnection *connection; int ip_ifindex; req = nm_device_get_act_request (self); g_assert (req); - connection = nm_act_request_get_applied_connection (req); - g_assert (connection); /* Interface must be IFF_UP before IP config can be applied */ ip_ifindex = nm_device_get_ip_ifindex (self); @@ -10251,9 +10346,8 @@ activate_stage5_ip4_config_result (NMDevice *self) } /* Start IPv4 sharing if we need it */ - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); - - if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) == 0) { + method = nm_device_get_effective_ip_config_method (self, AF_INET); + if (nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED)) { gs_free_error GError *error = NULL; if (!start_sharing (self, priv->ip_config_4, &error)) { @@ -10399,14 +10493,11 @@ activate_stage5_ip6_config_commit (NMDevice *self) NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); NMActRequest *req; const char *method; - NMConnection *connection; int ip_ifindex; int errsv; req = nm_device_get_act_request (self); g_assert (req); - connection = nm_act_request_get_applied_connection (req); - g_assert (connection); /* Interface must be IFF_UP before IP config can be applied */ ip_ifindex = nm_device_get_ip_ifindex (self); @@ -10438,9 +10529,8 @@ activate_stage5_ip6_config_commit (NMDevice *self) nm_device_remove_pending_action (self, NM_PENDING_ACTION_AUTOCONF6, FALSE); /* Start IPv6 forwarding if we need it */ - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG); - - if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_SHARED) == 0) { + method = nm_device_get_effective_ip_config_method (self, AF_INET6); + if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_SHARED)) { if (!nm_platform_sysctl_set (nm_device_get_platform (self), NMP_SYSCTL_PATHID_ABSOLUTE ("/proc/sys/net/ipv6/conf/all/forwarding"), "1")) { errsv = errno; _LOGE (LOGD_SHARING, "share: error enabling IPv6 forwarding: (%d) %s", errsv, strerror (errsv)); @@ -10682,7 +10772,7 @@ _cleanup_ip_pre (NMDevice *self, int addr_family, CleanupType cleanup_type) arp_cleanup (self); dnsmasq_cleanup (self); ipv4ll_cleanup (self); - g_slist_free_full (priv->acd.dad_list, (GDestroyNotify) nm_acd_manager_destroy); + g_slist_free_full (priv->acd.dad_list, (GDestroyNotify) nm_acd_manager_free); priv->acd.dad_list = NULL; } else { g_slist_free_full (priv->dad6_failed_addrs, (GDestroyNotify) nmp_object_unref); @@ -10695,8 +10785,10 @@ _cleanup_ip_pre (NMDevice *self, int addr_family, CleanupType cleanup_type) } gboolean -_nm_device_hash_check_invalid_keys (GHashTable *hash, const char *setting_name, - GError **error, const char **whitelist) +_nm_device_hash_check_invalid_keys (GHashTable *hash, + const char *setting_name, + GError **error, + const char *const*whitelist) { guint found_whitelisted_keys = 0; guint i; @@ -10875,7 +10967,7 @@ nm_device_reactivate_ip6_config (NMDevice *self, if (s_ip6_old && s_ip6_new) { gint64 metric_old, metric_new; - /* See comment in nm_device_reactivate_ip6_config() */ + /* See comment in nm_device_reactivate_ip4_config() */ metric_old = nm_setting_ip_config_get_route_metric (s_ip6_old); metric_new = nm_setting_ip_config_get_route_metric (s_ip6_new); @@ -11512,14 +11604,15 @@ disconnect_cb (NMDevice *self, } static void -_clear_queued_act_request (NMDevicePrivate *priv) +_clear_queued_act_request (NMDevicePrivate *priv, + NMActiveConnectionStateReason active_reason) { if (priv->queued_act_request) { gs_unref_object NMActRequest *ac = NULL; ac = g_steal_pointer (&priv->queued_act_request); nm_active_connection_set_state_fail ((NMActiveConnection *) ac, - NM_ACTIVE_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED, + active_reason, NULL); } } @@ -11661,7 +11754,8 @@ _carrier_wait_check_queued_act_request (NMDevice *self) priv->queued_act_request_is_waiting_for_carrier = FALSE; if (!priv->carrier) { _LOGD (LOGD_DEVICE, "Cancel queued activation request as we have no carrier after timeout"); - _clear_queued_act_request (priv); + _clear_queued_act_request (priv, + NM_ACTIVE_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED); } else { gs_unref_object NMActRequest *queued_req = NULL; @@ -11716,7 +11810,9 @@ _carrier_wait_check_act_request_must_queue (NMDevice *self, NMActRequest *req) } void -nm_device_disconnect_active_connection (NMActiveConnection *active) +nm_device_disconnect_active_connection (NMActiveConnection *active, + NMDeviceStateReason device_reason, + NMActiveConnectionStateReason active_reason) { NMDevice *self; NMDevicePrivate *priv; @@ -11724,38 +11820,59 @@ nm_device_disconnect_active_connection (NMActiveConnection *active) g_return_if_fail (NM_IS_ACTIVE_CONNECTION (active)); self = nm_active_connection_get_device (active); - if (!self) { /* hm, no device? Just fail the active connection. */ - nm_active_connection_set_state_fail (active, - NM_ACTIVE_CONNECTION_STATE_REASON_UNKNOWN, - NULL); - return; + goto do_fail; } priv = NM_DEVICE_GET_PRIVATE (self); if (NM_ACTIVE_CONNECTION (priv->queued_act_request) == active) { - _clear_queued_act_request (priv); + _clear_queued_act_request (priv, active_reason); return; } + if (NM_ACTIVE_CONNECTION (priv->act_request.obj) == active) { if (priv->state < NM_DEVICE_STATE_DEACTIVATING) { nm_device_state_changed (self, NM_DEVICE_STATE_DEACTIVATING, - NM_DEVICE_STATE_REASON_NEW_ACTIVATION); + device_reason); } else { - /* it's going down already... */ + /* @active is the current ac of @self, but it's going down already. + * Nothing to do. */ } + return; } + + /* the active connection references this device, but it's neither the + * queued_act_request nor the current act_request. Just set it to fail... */ +do_fail: + nm_active_connection_set_state_fail (active, + active_reason, + NULL); } void nm_device_queue_activation (NMDevice *self, NMActRequest *req) { - NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); + NMDevicePrivate *priv; gboolean must_queue; + g_return_if_fail (NM_IS_DEVICE (self)); + g_return_if_fail (NM_IS_ACT_REQUEST (req)); + + nm_keep_alive_arm (nm_active_connection_get_keep_alive (NM_ACTIVE_CONNECTION (req))); + + if (nm_active_connection_get_state (NM_ACTIVE_CONNECTION (req)) >= NM_ACTIVE_CONNECTION_STATE_DEACTIVATING) { + /* it's already deactivating. Nothing to do. */ + nm_assert (NM_IN_SET (nm_active_connection_get_device (NM_ACTIVE_CONNECTION (req)), NULL, self)); + return; + } + + nm_assert (self == nm_active_connection_get_device (NM_ACTIVE_CONNECTION (req))); + + priv = NM_DEVICE_GET_PRIVATE (self); + must_queue = _carrier_wait_check_act_request_must_queue (self, req); if ( !priv->act_request.obj @@ -11765,8 +11882,9 @@ nm_device_queue_activation (NMDevice *self, NMActRequest *req) return; } - /* supercede any already-queued request */ - _clear_queued_act_request (priv); + /* supersede any already-queued request */ + _clear_queued_act_request (priv, + NM_ACTIVE_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED); priv->queued_act_request = g_object_ref (req); priv->queued_act_request_is_waiting_for_carrier = must_queue; @@ -11961,11 +12079,6 @@ nm_device_set_ip_config (NMDevice *self, priv->needs_ip6_subnet = FALSE; } - if (IS_IPv4) { - if (!nm_device_sys_iface_state_is_external_or_assume (self)) - ip4_rp_filter_update (self); - } - if (has_changes) { if (IS_IPv4) @@ -12798,6 +12911,7 @@ queued_ip_config_change (NMDevice *self, int addr_family) if ( priv->state > NM_DEVICE_STATE_DISCONNECTED && priv->state < NM_DEVICE_STATE_DEACTIVATING + && priv->ifindex > 0 && !nm_device_sys_iface_state_is_external (self) && (platform = nm_device_get_platform (self)) && nm_platform_link_get (platform, priv->ifindex)) { @@ -12859,13 +12973,6 @@ queued_ip_config_change (NMDevice *self, int addr_family) set_unmanaged_external_down (self, TRUE); - if (IS_IPv4) { - if (!nm_device_sys_iface_state_is_external_or_assume (self)) { - priv->v4_has_shadowed_routes = _v4_has_shadowed_routes_detect (self);; - ip4_rp_filter_update (self); - } - } - return G_SOURCE_REMOVE; } @@ -13018,10 +13125,10 @@ _get_managed_by_flags(NMUnmanagedFlags flags, NMUnmanagedFlags mask, gboolean fo /* @for_user_request can make the result only ~more~ managed. * If the flags already indicate a managed state for a non-user-request, - * then it is also managed for an explict user-request. + * then it is also managed for an explicit user-request. * * Effectively, this check is redundant, as the code below already - * already ensures that. Still, express this invariant explictly here. */ + * already ensures that. Still, express this invariant explicitly here. */ if (_get_managed_by_flags (flags, mask, FALSE)) return TRUE; @@ -13077,7 +13184,7 @@ _get_managed_by_flags(NMUnmanagedFlags flags, NMUnmanagedFlags mask, gboolean fo /** * nm_device_get_managed: * @self: the #NMDevice - * @for_user_request: whether to check the flags for an explict user-request + * @for_user_request: whether to check the flags for an explicit user-request * * Whether the device is unmanaged according to the unmanaged flags. * @@ -13255,7 +13362,7 @@ _set_unmanaged_flags (NMDevice *self, flags, NM_PRINT_FMT_QUOTED (allow_state_transition, ", reason ", - reason_to_string (reason), + reason_to_string_a (reason), transition_state ? ", transition-state" : "", "")); @@ -14030,22 +14137,32 @@ nm_device_remove_pending_action (NMDevice *self, const char *action, gboolean as return FALSE; } -gboolean -nm_device_has_pending_action (NMDevice *self) +const char * +nm_device_has_pending_action_reason (NMDevice *self) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); - if (priv->pending_actions) - return TRUE; + if (priv->pending_actions) { + + if ( !priv->pending_actions->next + && nm_device_get_state (self) == NM_DEVICE_STATE_ACTIVATED + && nm_streq (priv->pending_actions->data, NM_PENDING_ACTION_CARRIER_WAIT)) { + /* if the device is already in activated state, and the only reason + * why it appears still busy is "carrier-wait", then we are already complete. */ + return NULL; + } + + return priv->pending_actions->data; + } if ( nm_device_is_real (self) && nm_device_get_unmanaged_flags (self, NM_UNMANAGED_PLATFORM_INIT)) { /* as long as the platform link is not yet initialized, we have a pending * action. */ - return TRUE; + return NM_PENDING_ACTION_LINK_INIT; } - return FALSE; + return NULL; } /*****************************************************************************/ @@ -14171,7 +14288,7 @@ _cleanup_generic_post (NMDevice *self, CleanupType cleanup_type) delete_on_deactivate_check_and_schedule (self, nm_device_get_ip_ifindex (self)); } - /* ip_iface should be cleared after flushing all routes and addreses, since + /* ip_iface should be cleared after flushing all routes and addresses, since * those are identified by ip_iface, not by iface (which might be a tty * or ATM device). */ @@ -14195,7 +14312,7 @@ nm_device_cleanup (NMDevice *self, NMDeviceStateReason reason, CleanupType clean if (reason == NM_DEVICE_STATE_REASON_NOW_MANAGED) _LOGD (LOGD_DEVICE, "preparing device"); else - _LOGD (LOGD_DEVICE, "deactivating device (reason '%s') [%d]", reason_to_string (reason), reason); + _LOGD (LOGD_DEVICE, "deactivating device (reason '%s') [%d]", reason_to_string_a (reason), reason); /* Save whether or not we tried IPv6 for later */ priv = NM_DEVICE_GET_PRIVATE (self); @@ -14205,8 +14322,8 @@ nm_device_cleanup (NMDevice *self, NMDeviceStateReason reason, CleanupType clean /* Turn off kernel IPv6 */ if (cleanup_type == CLEANUP_TYPE_DECONFIGURE) { set_disable_ipv6 (self, "1"); - nm_device_ipv6_sysctl_set (self, "accept_ra", "0"); - nm_device_ipv6_sysctl_set (self, "use_tempaddr", "0"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "accept_ra", "0"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "use_tempaddr", "0"); } /* Call device type-specific deactivation */ @@ -14235,6 +14352,7 @@ nm_device_cleanup (NMDevice *self, NMDeviceStateReason reason, CleanupType clean /* slave: mark no longer enslaved */ if ( priv->master + && priv->ifindex > 0 && nm_platform_link_get_master (nm_device_get_platform (self), priv->ifindex) <= 0) nm_device_master_release_one_slave (priv->master, self, FALSE, NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED); @@ -14271,8 +14389,8 @@ nm_device_cleanup (NMDevice *self, NMDeviceStateReason reason, CleanupType clean if (priv->ip6_mtu_initial) { char sbuf[64]; - nm_device_ipv6_sysctl_set (self, "mtu", - nm_sprintf_buf (sbuf, "%u", (unsigned) priv->ip6_mtu_initial)); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "mtu", + nm_sprintf_buf (sbuf, "%u", (unsigned) priv->ip6_mtu_initial)); } } priv->mtu_initial = 0; @@ -14302,7 +14420,7 @@ find_dhcp4_address (NMDevice *self) nm_ip_config_iter_ip4_address_for_each (&ipconf_iter, priv->ip_config_4, &a) { if (a->addr_source == NM_IP_CONFIG_SOURCE_DHCP) - return g_strdup (nm_utils_inet4_ntop (a->address, NULL)); + return nm_utils_inet4_ntop_dup (a->address); } return NULL; } @@ -14327,7 +14445,8 @@ nm_device_spawn_iface_helper (NMDevice *self) return; connection = nm_device_get_applied_connection (self); - g_assert (connection); + + g_return_if_fail (connection); argv = g_ptr_array_sized_new (10); g_ptr_array_set_free_func (argv, g_free); @@ -14361,8 +14480,8 @@ nm_device_spawn_iface_helper (NMDevice *self) dhcp4_address = find_dhcp4_address (self); - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); - if (g_strcmp0 (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0) { + method = nm_device_get_effective_ip_config_method (self, AF_INET); + if (nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) { NMSettingIPConfig *s_ip4; s_ip4 = nm_connection_get_setting_ip4_config (connection); @@ -14384,9 +14503,11 @@ nm_device_spawn_iface_helper (NMDevice *self) if (client_id) { g_ptr_array_add (argv, g_strdup ("--dhcp4-clientid")); g_ptr_array_add (argv, - _nm_utils_bin2str (g_bytes_get_data (client_id, NULL), - g_bytes_get_size (client_id), - FALSE)); + _nm_utils_bin2hexstr_full (g_bytes_get_data (client_id, NULL), + g_bytes_get_size (client_id), + ':', + FALSE, + NULL)); } hostname = nm_dhcp_client_get_hostname (priv->dhcp4.client); @@ -14402,8 +14523,8 @@ nm_device_spawn_iface_helper (NMDevice *self) configured = TRUE; } - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG); - if (g_strcmp0 (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0) { + method = nm_utils_get_ip_config_method (connection, AF_INET6); + if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO)) { NMSettingIPConfig *s_ip6; NMUtilsIPv6IfaceId iid = NM_UTILS_IPV6_IFACE_ID_INIT; @@ -14424,9 +14545,11 @@ nm_device_spawn_iface_helper (NMDevice *self) if (nm_device_get_ip_iface_identifier (self, &iid, FALSE)) { g_ptr_array_add (argv, g_strdup ("--iid")); g_ptr_array_add (argv, - _nm_utils_bin2str (iid.id_u8, - sizeof (NMUtilsIPv6IfaceId), - FALSE)); + _nm_utils_bin2hexstr_full (iid.id_u8, + sizeof (NMUtilsIPv6IfaceId), + ':', + FALSE, + NULL)); } g_ptr_array_add (argv, g_strdup ("--addr-gen-mode")); @@ -14485,39 +14608,34 @@ ip6_managed_setup (NMDevice *self) { set_nm_ipv6ll (self, TRUE); set_disable_ipv6 (self, "1"); - nm_device_ipv6_sysctl_set (self, "accept_ra_defrtr", "0"); - nm_device_ipv6_sysctl_set (self, "accept_ra_pinfo", "0"); - nm_device_ipv6_sysctl_set (self, "accept_ra_rtr_pref", "0"); - nm_device_ipv6_sysctl_set (self, "use_tempaddr", "0"); - nm_device_ipv6_sysctl_set (self, "forwarding", "0"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "accept_ra_defrtr", "0"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "accept_ra_pinfo", "0"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "accept_ra_rtr_pref", "0"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "use_tempaddr", "0"); + nm_device_sysctl_ip_conf_set (self, AF_INET6, "forwarding", "0"); } static void deactivate_async_ready (NMDevice *self, - GAsyncResult *res, + GError *error, gpointer user_data) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); NMDeviceStateReason reason = GPOINTER_TO_UINT (user_data); - GError *error = NULL; - - NM_DEVICE_GET_CLASS (self)->deactivate_async_finish (self, res, &error); - /* If operation cancelled, just return */ - if ( g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) - || (priv->deactivating_cancellable && g_cancellable_is_cancelled (priv->deactivating_cancellable))) { - _LOGW (LOGD_DEVICE, "Deactivation cancelled"); - } else { - /* In every other case, transition to the DISCONNECTED state */ - if (error) { - _LOGW (LOGD_DEVICE, "Deactivation failed: %s", - error->message); - } - nm_device_queue_state (self, NM_DEVICE_STATE_DISCONNECTED, reason); + if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { + _LOGD (LOGD_DEVICE, "Deactivation cancelled"); + return; } g_clear_object (&priv->deactivating_cancellable); - g_clear_error (&error); + + /* In every other case, transition to the DISCONNECTED state */ + if (error) { + _LOGW (LOGD_DEVICE, "Deactivation failed: %s", + error->message); + } + nm_device_queue_state (self, NM_DEVICE_STATE_DISCONNECTED, reason); } static void @@ -14537,14 +14655,13 @@ deactivate_dispatcher_complete (guint call_id, gpointer user_data) priv->dispatcher.post_state_reason = NM_DEVICE_STATE_REASON_NONE; if (nm_clear_g_cancellable (&priv->deactivating_cancellable)) - g_warn_if_reached (); + nm_assert_not_reached (); - if ( NM_DEVICE_GET_CLASS (self)->deactivate_async - && NM_DEVICE_GET_CLASS (self)->deactivate_async_finish) { + if (NM_DEVICE_GET_CLASS (self)->deactivate_async) { priv->deactivating_cancellable = g_cancellable_new (); NM_DEVICE_GET_CLASS (self)->deactivate_async (self, priv->deactivating_cancellable, - (GAsyncReadyCallback) deactivate_async_ready, + deactivate_async_ready, GUINT_TO_POINTER (reason)); } else nm_device_queue_state (self, NM_DEVICE_STATE_DISCONNECTED, reason); @@ -14561,6 +14678,7 @@ _set_state_full (NMDevice *self, NMActRequest *req; gboolean no_firmware = FALSE; NMSettingsConnection *sett_conn; + NMSettingSriov *s_sriov; g_return_if_fail (NM_IS_DEVICE (self)); @@ -14581,7 +14699,7 @@ _set_state_full (NMDevice *self, _LOGD (LOGD_DEVICE, "state change: %s -> %s (reason '%s', sys-iface-state: '%s'%s)", nm_device_state_to_str (old_state), nm_device_state_to_str (state), - reason_to_string (reason), + reason_to_string_a (reason), _sys_iface_state_to_str (priv->sys_iface_state), priv->firmware_missing ? ", missing firmware" : ""); return; @@ -14590,7 +14708,7 @@ _set_state_full (NMDevice *self, _LOGI (LOGD_DEVICE, "state change: %s -> %s (reason '%s', sys-iface-state: '%s')", nm_device_state_to_str (old_state), nm_device_state_to_str (state), - reason_to_string (reason), + reason_to_string_a (reason), _sys_iface_state_to_str (priv->sys_iface_state)); priv->in_state_changed = TRUE; @@ -14601,8 +14719,8 @@ _set_state_full (NMDevice *self, queued_state_clear (self); dispatcher_cleanup (self); - if (priv->deactivating_cancellable) - g_cancellable_cancel (priv->deactivating_cancellable); + + nm_clear_g_cancellable (&priv->deactivating_cancellable); /* Cache the activation request for the dispatcher */ req = nm_g_object_ref (priv->act_request.obj); @@ -14626,8 +14744,10 @@ _set_state_full (NMDevice *self, if (state <= NM_DEVICE_STATE_UNAVAILABLE) { if (available_connections_del_all (self)) _notify (self, PROP_AVAILABLE_CONNECTIONS); - if (old_state > NM_DEVICE_STATE_UNAVAILABLE) - _clear_queued_act_request (priv); + if (old_state > NM_DEVICE_STATE_UNAVAILABLE) { + _clear_queued_act_request (priv, + NM_ACTIVE_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED); + } } /* Update the available connections list when a device first becomes available */ @@ -14668,6 +14788,7 @@ _set_state_full (NMDevice *self, save_ip6_properties (self); if (priv->sys_iface_state == NM_DEVICE_SYS_IFACE_STATE_MANAGED) ip6_managed_setup (self); + device_init_static_sriov_num_vfs (self); } if (priv->sys_iface_state == NM_DEVICE_SYS_IFACE_STATE_MANAGED) { @@ -14756,6 +14877,12 @@ _set_state_full (NMDevice *self, } break; case NM_DEVICE_STATE_DEACTIVATING: + if ( (s_sriov = nm_device_get_applied_setting (self, NM_TYPE_SETTING_SRIOV)) + && priv->ifindex > 0) { + nm_platform_link_set_sriov_params (nm_device_get_platform (self), + priv->ifindex, 0, NM_TERNARY_TRUE); + } + _cancel_activation (self); /* We cache the ignore_carrier state to not react on config-reloads while the connection @@ -14885,8 +15012,8 @@ _set_state_full (NMDevice *self, if (ip_config_valid (old_state) && !ip_config_valid (state)) notify_ip_properties (self); - concheck_update_interval (self, - state == NM_DEVICE_STATE_ACTIVATED); + concheck_update_interval (self, AF_INET, state == NM_DEVICE_STATE_ACTIVATED); + concheck_update_interval (self, AF_INET6, state == NM_DEVICE_STATE_ACTIVATED); /* Dispose of the cached activation request */ if (req) @@ -14918,7 +15045,7 @@ queued_state_set (gpointer user_data) _LOGD (LOGD_DEVICE, "queue-state[%s, reason:%s, id:%u]: %s", nm_device_state_to_str (priv->queued_state.state), - reason_to_string (priv->queued_state.reason), + reason_to_string_a (priv->queued_state.reason), priv->queued_state.id, "change state"); @@ -14949,23 +15076,23 @@ nm_device_queue_state (NMDevice *self, if (priv->queued_state.id && priv->queued_state.state == state) { _LOGD (LOGD_DEVICE, "queue-state[%s, reason:%s, id:%u]: %s%s%s%s", nm_device_state_to_str (priv->queued_state.state), - reason_to_string (priv->queued_state.reason), + reason_to_string_a (priv->queued_state.reason), priv->queued_state.id, "ignore queuing same state change", NM_PRINT_FMT_QUOTED (priv->queued_state.reason != reason, - " (reason differs: ", reason_to_string (reason), ")", "")); + " (reason differs: ", reason_to_string_a (reason), ")", "")); return; } /* Add pending action for the new state before clearing the queued states, so - * that we don't accidently pop all pending states and reach 'startup complete' */ + * that we don't accidentally pop all pending states and reach 'startup complete' */ nm_device_add_pending_action (self, queued_state_to_string (state), TRUE); /* We should only ever have one delayed state transition at a time */ if (priv->queued_state.id) { _LOGW (LOGD_DEVICE, "queue-state[%s, reason:%s, id:%u]: %s", nm_device_state_to_str (priv->queued_state.state), - reason_to_string (priv->queued_state.reason), + reason_to_string_a (priv->queued_state.reason), priv->queued_state.id, "replace previously queued state change"); nm_clear_g_source (&priv->queued_state.id); @@ -14978,7 +15105,7 @@ nm_device_queue_state (NMDevice *self, _LOGD (LOGD_DEVICE, "queue-state[%s, reason:%s, id:%u]: %s", nm_device_state_to_str (state), - reason_to_string (reason), + reason_to_string_a (reason), priv->queued_state.id, "queue state change"); } @@ -14993,7 +15120,7 @@ queued_state_clear (NMDevice *self) _LOGD (LOGD_DEVICE, "queue-state[%s, reason:%s, id:%u]: %s", nm_device_state_to_str (priv->queued_state.state), - reason_to_string (priv->queued_state.reason), + reason_to_string_a (priv->queued_state.reason), priv->queued_state.id, "clear queued state change"); nm_clear_g_source (&priv->queued_state.id); @@ -15083,7 +15210,7 @@ nm_device_update_hw_address (NMDevice *self) && priv->state < NM_DEVICE_STATE_PREPARE && !nm_device_is_activating (self))) { /* when we get a hw_addr the first time or while the device - * is not activated (with no explict hw address set), always + * is not activated (with no explicit hw address set), always * update our initial hw-address as well. */ nm_device_update_initial_hw_address (self); } @@ -15100,7 +15227,7 @@ nm_device_update_initial_hw_address (NMDevice *self) if ( priv->hw_addr_initial && priv->hw_addr_type != HW_ADDR_TYPE_UNSET) { /* once we have the initial hw address set, we only allow - * update if the currenty type is "unset". */ + * update if the currently type is "unset". */ return; } g_free (priv->hw_addr_initial); @@ -15225,7 +15352,9 @@ _get_cloned_mac_address_setting (NMDevice *self, NMConnection *connection, gbool gs_free char *a = NULL; a = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA, - is_wifi ? "wifi.cloned-mac-address" : "ethernet.cloned-mac-address", + is_wifi + ? NM_CON_DEFAULT ("wifi.cloned-mac-address") + : NM_CON_DEFAULT ("ethernet.cloned-mac-address"), self); addr = NM_CLONED_MAC_PRESERVE; @@ -15236,7 +15365,7 @@ _get_cloned_mac_address_setting (NMDevice *self, NMConnection *connection, gbool /* for backward compatibility, read the deprecated wifi.mac-address-randomization setting. */ a = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA, - "wifi." NM_SETTING_WIRELESS_MAC_ADDRESS_RANDOMIZATION, + NM_CON_DEFAULT ("wifi.mac-address-randomization"), self); v = _nm_utils_ascii_str_to_int64 (a, 10, NM_SETTING_MAC_RANDOMIZATION_DEFAULT, @@ -15273,7 +15402,9 @@ _get_generate_mac_address_mask_setting (NMDevice *self, NMConnection *connection } a = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA, - is_wifi ? "wifi.generate-mac-address-mask" : "ethernet.generate-mac-mac-address-mask", + is_wifi + ? NM_CON_DEFAULT ("wifi.generate-mac-address-mask") + : NM_CON_DEFAULT ("ethernet.generate-mac-address-mask"), self); if (!a) return NULL; @@ -15311,10 +15442,10 @@ _hw_addr_set (NMDevice *self, { NMDevicePrivate *priv; gboolean success = FALSE; - NMPlatformError plerr; + int r; guint8 addr_bytes[NM_UTILS_HWADDR_LEN_MAX]; gsize addr_len; - gboolean was_taken_down; + gboolean was_taken_down = FALSE; gboolean retry_down; nm_assert (NM_IS_DEVICE (self)); @@ -15338,24 +15469,31 @@ _hw_addr_set (NMDevice *self, _LOGT (LOGD_DEVICE, "set-hw-addr: setting MAC address to '%s' (%s, %s)...", addr, operation, detail); - was_taken_down = FALSE; + if (nm_device_get_device_type (self) == NM_DEVICE_TYPE_WIFI) { + /* Always take the device down for Wi-Fi because + * wpa_supplicant needs it to properly detect the MAC + * change. */ + retry_down = FALSE; + was_taken_down = TRUE; + nm_device_take_down (self, FALSE); + } again: - plerr = nm_platform_link_set_address (nm_device_get_platform (self), nm_device_get_ip_ifindex (self), addr_bytes, addr_len); - success = (plerr == NM_PLATFORM_ERROR_SUCCESS); + r = nm_platform_link_set_address (nm_device_get_platform (self), nm_device_get_ip_ifindex (self), addr_bytes, addr_len); + success = (r >= 0); if (!success) { retry_down = !was_taken_down - && plerr != NM_PLATFORM_ERROR_NOT_FOUND + && r != -NME_PL_NOT_FOUND && nm_platform_link_is_up (nm_device_get_platform (self), nm_device_get_ip_ifindex (self)); - _NMLOG ( retry_down - || plerr == NM_PLATFORM_ERROR_NOT_FOUND + _NMLOG ( ( retry_down + || r == -NME_PL_NOT_FOUND) ? LOGL_DEBUG : LOGL_WARN, LOGD_DEVICE, "set-hw-addr: failed to %s MAC address to %s (%s) (%s)%s", operation, addr, detail, - nm_platform_error_to_string_a (plerr), + nm_strerror (r), retry_down ? " (retry with taking down)" : ""); } else { /* MAC address successfully changed; update the current MAC to match */ @@ -15378,7 +15516,7 @@ again: * that is rather complicated and it is not expected that this case * happens for regular drivers. * Note that brcmfmac can block NetworkManager for 500 msec while - * taking down the device. Let's add annother 100 msec to that. + * taking down the device. Let's add another 100 msec to that. * * wait/poll up to 100 msec until it changes. */ @@ -15754,7 +15892,8 @@ nm_device_spec_match_list_full (NMDevice *self, const GSList *specs, int no_matc nm_device_get_driver (self), nm_device_get_driver_version (self), nm_device_get_permanent_hw_address (self), - klass->get_s390_subchannels ? klass->get_s390_subchannels (self) : NULL); + klass->get_s390_subchannels ? klass->get_s390_subchannels (self) : NULL, + nm_dhcp_manager_get_config (nm_dhcp_manager_get ())); switch (m) { case NM_MATCH_SPEC_MATCH: @@ -15779,7 +15918,9 @@ nm_device_get_supplicant_timeout (NMDevice *self) g_return_val_if_fail (NM_IS_DEVICE (self), SUPPLICANT_DEFAULT_TIMEOUT); connection = nm_device_get_applied_connection (self); + g_return_val_if_fail (connection, SUPPLICANT_DEFAULT_TIMEOUT); + s_8021x = nm_connection_get_setting_802_1x (connection); if (s_8021x) { timeout = nm_setting_802_1x_get_auth_timeout (s_8021x); @@ -15788,7 +15929,7 @@ nm_device_get_supplicant_timeout (NMDevice *self) } return nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA, - "802-1x.auth-timeout", + NM_CON_DEFAULT ("802-1x.auth-timeout"), self, 1, G_MAXINT32, @@ -15810,13 +15951,13 @@ nm_device_auth_retries_try_next (NMDevice *self) if (G_UNLIKELY (auth_retries == NM_DEVICE_AUTH_RETRIES_UNSET)) { auth_retries = -1; - s_con = NM_SETTING_CONNECTION (nm_device_get_applied_setting (self, NM_TYPE_SETTING_CONNECTION)); + s_con = nm_device_get_applied_setting (self, NM_TYPE_SETTING_CONNECTION); if (s_con) auth_retries = nm_setting_connection_get_auth_retries (s_con); if (auth_retries == -1) { auth_retries = nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA, - "connection.auth-retries", + NM_CON_DEFAULT ("connection.auth-retries"), self, -1, G_MAXINT32, -1); } @@ -15876,7 +16017,8 @@ nm_device_init (NMDevice *self) c_list_init (&self->devices_lst); c_list_init (&priv->slaves); - priv->connectivity_state = NM_CONNECTIVITY_UNKNOWN; + priv->concheck_x[0].state = NM_CONNECTIVITY_UNKNOWN; + priv->concheck_x[1].state = NM_CONNECTIVITY_UNKNOWN; nm_dbus_track_obj_path_init (&priv->parent_device, G_OBJECT (self), obj_properties[PROP_PARENT]); nm_dbus_track_obj_path_init (&priv->act_request, G_OBJECT (self), obj_properties[PROP_ACTIVE_CONNECTION]); @@ -16064,7 +16206,8 @@ dispose (GObject *object) if (nm_clear_g_source (&priv->carrier_wait_id)) nm_device_remove_pending_action (self, NM_PENDING_ACTION_CARRIER_WAIT, FALSE); - _clear_queued_act_request (priv); + _clear_queued_act_request (priv, + NM_ACTIVE_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED); nm_clear_g_source (&priv->device_link_changed_id); nm_clear_g_source (&priv->device_ip_link_changed_id); @@ -16077,12 +16220,13 @@ dispose (GObject *object) g_clear_object (&priv->lldp_listener); } - nm_clear_g_source (&priv->concheck_p_cur_id); + nm_clear_g_source (&priv->concheck_x[0].p_cur_id); + nm_clear_g_source (&priv->concheck_x[1].p_cur_id); G_OBJECT_CLASS (nm_device_parent_class)->dispose (object); if (nm_clear_g_source (&priv->queued_state.id)) { - /* FIXME: we'd expect the queud_state to be alredy cleared and this statement + /* FIXME: we'd expect the queud_state to be already cleared and this statement * not being necessary. Add this check here to hopefully investigate crash * rh#1270247. */ g_return_if_reached (); @@ -16414,8 +16558,11 @@ get_property (GObject *object, guint prop_id, case PROP_RX_BYTES: g_value_set_uint64 (value, priv->stats.rx_bytes); break; - case PROP_CONNECTIVITY: - g_value_set_uint (value, priv->connectivity_state); + case PROP_IP4_CONNECTIVITY: + g_value_set_uint (value, priv->concheck_x[1].state); + break; + case PROP_IP6_CONNECTIVITY: + g_value_set_uint (value, priv->concheck_x[0].state); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -16503,6 +16650,8 @@ static const NMDBusInterfaceInfoExtended interface_info_device = { NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE_L ("Metered", "u", NM_DEVICE_METERED), NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE_L ("LldpNeighbors", "aa{sv}", NM_DEVICE_LLDP_NEIGHBORS), NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE_L ("Real", "b", NM_DEVICE_REAL), + NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE ("Ip4Connectivity", "u", NM_DEVICE_IP4_CONNECTIVITY), + NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE ("Ip6Connectivity", "u", NM_DEVICE_IP6_CONNECTIVITY), ), ), }; @@ -16779,8 +16928,13 @@ nm_device_class_init (NMDeviceClass *klass) G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); - obj_properties[PROP_CONNECTIVITY] = - g_param_spec_uint (NM_DEVICE_CONNECTIVITY, "", "", + obj_properties[PROP_IP4_CONNECTIVITY] = + g_param_spec_uint (NM_DEVICE_IP4_CONNECTIVITY, "", "", + NM_CONNECTIVITY_UNKNOWN, NM_CONNECTIVITY_FULL, NM_CONNECTIVITY_UNKNOWN, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS); + obj_properties[PROP_IP6_CONNECTIVITY] = + g_param_spec_uint (NM_DEVICE_IP6_CONNECTIVITY, "", "", NM_CONNECTIVITY_UNKNOWN, NM_CONNECTIVITY_FULL, NM_CONNECTIVITY_UNKNOWN, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); @@ -16860,12 +17014,12 @@ nm_device_class_init (NMDeviceClass *klass) G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0); - - signals[CONNECTIVITY_CHANGED] = - g_signal_new (NM_DEVICE_CONNECTIVITY_CHANGED, - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_FIRST, - 0, NULL, NULL, - g_cclosure_marshal_VOID__VOID, - G_TYPE_NONE, 0); } + +/* Connection defaults from plugins */ +NM_CON_DEFAULT_NOP ("cdma.mtu"); +NM_CON_DEFAULT_NOP ("gsm.mtu"); +NM_CON_DEFAULT_NOP ("wifi.powersave"); +NM_CON_DEFAULT_NOP ("wifi.wake-on-wlan"); +NM_CON_DEFAULT_NOP ("wifi-sec.pmf"); +NM_CON_DEFAULT_NOP ("wifi-sec.fils"); diff --git a/src/devices/nm-device.h b/src/devices/nm-device.h index 8f376b20..c2e3c474 100644 --- a/src/devices/nm-device.h +++ b/src/devices/nm-device.h @@ -54,7 +54,7 @@ typedef enum { static inline NMDeviceStateReason nm_device_state_reason_check (NMDeviceStateReason reason) { - /* the device-state-reason serves mostly informational purpse during a state + /* the device-state-reason serves mostly informational purpose during a state * change. In some cases however, decisions are made based on the reason. * I tend to think that interpreting the state reason to derive some behaviors * is confusing, because the cause and effect are so far apart. @@ -76,6 +76,7 @@ nm_device_state_reason_check (NMDeviceStateReason reason) #define NM_PENDING_ACTION_WAITING_FOR_SUPPLICANT "waiting-for-supplicant" #define NM_PENDING_ACTION_WIFI_SCAN "wifi-scan" #define NM_PENDING_ACTION_WAITING_FOR_COMPANION "waiting-for-companion" +#define NM_PENDING_ACTION_LINK_INIT "link-init" #define NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "queued-state-change-" #define NM_PENDING_ACTIONPREFIX_ACTIVATION "activation-" @@ -142,13 +143,13 @@ nm_device_state_reason_check (NMDeviceStateReason reason) #define NM_DEVICE_STATE_CHANGED "state-changed" #define NM_DEVICE_LINK_INITIALIZED "link-initialized" #define NM_DEVICE_AUTOCONNECT_ALLOWED "autoconnect-allowed" -#define NM_DEVICE_CONNECTIVITY_CHANGED "connectivity-changed" #define NM_DEVICE_STATISTICS_REFRESH_RATE_MS "refresh-rate-ms" #define NM_DEVICE_STATISTICS_TX_BYTES "tx-bytes" #define NM_DEVICE_STATISTICS_RX_BYTES "rx-bytes" -#define NM_DEVICE_CONNECTIVITY "connectivity" +#define NM_DEVICE_IP4_CONNECTIVITY "ip4-connectivity" +#define NM_DEVICE_IP6_CONNECTIVITY "ip6-connectivity" #define NM_TYPE_DEVICE (nm_device_get_type ()) #define NM_DEVICE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DEVICE, NMDevice)) @@ -221,6 +222,10 @@ typedef enum { /*< skip >*/ NM_DEVICE_CHECK_DEV_AVAILABLE_ALL = (1L << 1) - 1, } NMDeviceCheckDevAvailableFlags; +typedef void (*NMDeviceDeactivateCallback) (NMDevice *self, + GError *error, + gpointer user_data); + typedef struct _NMDeviceClass { NMDBusObjectClass parent; @@ -278,7 +283,7 @@ typedef struct _NMDeviceClass { * Hook for derived classes to be notfied during realize_start_setup() * and perform additional setup. * - * The default implemention of NMDevice calls link_changed(). + * The default implementation of NMDevice calls link_changed(). */ void (*realize_start_notify) (NMDevice *self, const NMPlatformLink *pllink); @@ -327,9 +332,11 @@ typedef struct _NMDeviceClass { guint32 (*get_configured_mtu) (NMDevice *self, NMDeviceMtuSource *out_source); + const char *(*get_auto_ip_config_method) (NMDevice *self, int addr_family); + /* Checks whether the connection is compatible with the device using * only the devices type and characteristics. Does not use any live - * network information like WiFi scan lists etc. + * network information like Wi-Fi scan lists etc. */ gboolean (* check_connection_compatible) (NMDevice *self, NMConnection *connection, @@ -378,11 +385,8 @@ typedef struct _NMDeviceClass { /* Async deactivating (in the DEACTIVATING phase) */ void (* deactivate_async) (NMDevice *self, GCancellable *cancellable, - GAsyncReadyCallback callback, + NMDeviceDeactivateCallback callback, gpointer user_data); - gboolean (* deactivate_async_finish) (NMDevice *self, - GAsyncResult *res, - GError **error); void (* deactivate_reset_hw_addr) (NMDevice *self); @@ -481,6 +485,7 @@ int nm_device_get_ifindex (NMDevice *dev); gboolean nm_device_is_software (NMDevice *dev); gboolean nm_device_is_real (NMDevice *dev); const char * nm_device_get_ip_iface (NMDevice *dev); +const char * nm_device_get_ip_iface_from_platform (NMDevice *dev); int nm_device_get_ip_ifindex (const NMDevice *dev); const char * nm_device_get_driver (NMDevice *dev); const char * nm_device_get_driver_version (NMDevice *dev); @@ -541,7 +546,10 @@ NMConnection * nm_device_get_settings_connection_get_connection (NMDevice *self NMConnection * nm_device_get_applied_connection (NMDevice *dev); gboolean nm_device_has_unmodified_applied_connection (NMDevice *self, NMSettingCompareFlags compare_flags); -NMSetting * nm_device_get_applied_setting (NMDevice *dev, GType setting_type); +NMActivationStateFlags nm_device_get_activation_state_flags (NMDevice *self); + +gpointer /* (NMSetting *) */ nm_device_get_applied_setting (NMDevice *dev, + GType setting_type); void nm_device_removed (NMDevice *self, gboolean unconfigure_ip_config); @@ -617,7 +625,7 @@ void nm_device_copy_ip6_dns_config (NMDevice *self, NMDevice *from_device); * the settings plugin (for example keyfile.unmanaged-devices or ifcfg-rh's * NM_CONTROLLED=no). Although this is user-configuration (provided from * the settings plugins, such as NM_CONTROLLED=no in ifcfg-rh), it cannot - * be overruled and is authorative. That is because users may depend on + * be overruled and is authoritative. That is because users may depend on * dropping a ifcfg-rh file to ensure the device is unmanaged. * @NM_UNMANAGED_USER_CONF: %TRUE when unmanaged by user decision via * the NetworkManager.conf ("unmanaged" in the [device] section). @@ -634,7 +642,7 @@ void nm_device_copy_ip6_dns_config (NMDevice *self, NMDevice *from_device); typedef enum { /*< skip >*/ NM_UNMANAGED_NONE = 0, - /* these flags are authorative. If one of them is set, + /* these flags are authoritative. If one of them is set, * the device cannot be managed. */ NM_UNMANAGED_SLEEPING = (1LL << 0), NM_UNMANAGED_QUITTING = (1LL << 1), @@ -722,6 +730,8 @@ typedef enum { NM_DEVICE_AUTOCONNECT_BLOCKED_WRONG_PIN = (1LL << 1), NM_DEVICE_AUTOCONNECT_BLOCKED_MANUAL_DISCONNECT = (1LL << 2), + NM_DEVICE_AUTOCONNECT_BLOCKED_SIM_MISSING = (1LL << 3), + NM_DEVICE_AUTOCONNECT_BLOCKED_INIT_FAILED = (1LL << 4), _NM_DEVICE_AUTOCONNECT_BLOCKED_LAST, @@ -765,7 +775,9 @@ void nm_device_queue_state (NMDevice *self, gboolean nm_device_get_firmware_missing (NMDevice *self); -void nm_device_disconnect_active_connection (NMActiveConnection *active); +void nm_device_disconnect_active_connection (NMActiveConnection *active, + NMDeviceStateReason device_reason, + NMActiveConnectionStateReason active_reason); void nm_device_queue_activation (NMDevice *device, NMActRequest *req); @@ -773,7 +785,13 @@ gboolean nm_device_supports_vlans (NMDevice *device); gboolean nm_device_add_pending_action (NMDevice *device, const char *action, gboolean assert_not_yet_pending); gboolean nm_device_remove_pending_action (NMDevice *device, const char *action, gboolean assert_is_pending); -gboolean nm_device_has_pending_action (NMDevice *device); +const char *nm_device_has_pending_action_reason (NMDevice *device); + +static inline gboolean +nm_device_has_pending_action (NMDevice *device) +{ + return !!nm_device_has_pending_action_reason (device); +} NMSettingsConnection *nm_device_get_best_connection (NMDevice *device, const char *specific_object, @@ -836,12 +854,13 @@ typedef void (*NMDeviceConnectivityCallback) (NMDevice *self, void nm_device_check_connectivity_update_interval (NMDevice *self); NMDeviceConnectivityHandle *nm_device_check_connectivity (NMDevice *self, + int addr_family, NMDeviceConnectivityCallback callback, gpointer user_data); void nm_device_check_connectivity_cancel (NMDeviceConnectivityHandle *handle); -NMConnectivityState nm_device_get_connectivity_state (NMDevice *self); +NMConnectivityState nm_device_get_connectivity_state (NMDevice *self, int addr_family); typedef struct _NMBtVTableNetworkServer NMBtVTableNetworkServer; struct _NMBtVTableNetworkServer { diff --git a/src/devices/nm-lldp-listener.c b/src/devices/nm-lldp-listener.c index c0484ed3..ae180637 100644 --- a/src/devices/nm-lldp-listener.c +++ b/src/devices/nm-lldp-listener.c @@ -128,7 +128,7 @@ typedef struct { int _ifindex = (self) ? NM_LLDP_LISTENER_GET_PRIVATE (self)->ifindex : 0; \ \ _nm_log (_level, _NMLOG_DOMAIN, 0, \ - nm_platform_link_get_name (NM_PLATFORM_GET, _ifindex), \ + _ifindex > 0 ? nm_platform_link_get_name (NM_PLATFORM_GET, _ifindex) : NULL, \ NULL, \ "%s%s: " _NM_UTILS_MACRO_FIRST (__VA_ARGS__), \ _NMLOG_PREFIX_NAME, \ diff --git a/src/devices/ovs/meson.build b/src/devices/ovs/meson.build index 9d637fe9..834b27b0 100644 --- a/src/devices/ovs/meson.build +++ b/src/devices/ovs/meson.build @@ -3,12 +3,12 @@ sources = files( 'nm-device-ovs-interface.c', 'nm-device-ovs-port.c', 'nm-ovsdb.c', - 'nm-ovs-factory.c' + 'nm-ovs-factory.c', ) deps = [ jansson_dep, - nm_dep + nm_dep, ] libnm_device_plugin_ovs = shared_module( @@ -18,7 +18,7 @@ libnm_device_plugin_ovs = shared_module( link_args: ldflags_linker_script_devices, link_depends: linker_script_devices, install: true, - install_dir: nm_plugindir + install_dir: nm_plugindir, ) core_plugins += libnm_device_plugin_ovs diff --git a/src/devices/ovs/nm-device-ovs-interface.c b/src/devices/ovs/nm-device-ovs-interface.c index 2b48fae6..960b8f35 100644 --- a/src/devices/ovs/nm-device-ovs-interface.c +++ b/src/devices/ovs/nm-device-ovs-interface.c @@ -121,12 +121,13 @@ link_changed (NMDevice *device, static gboolean _is_internal_interface (NMDevice *device) { - NMConnection *connection = nm_device_get_applied_connection (device); - NMSettingOvsInterface *s_ovs_iface = nm_connection_get_setting_ovs_interface (connection); + NMSettingOvsInterface *s_ovs_iface; + + s_ovs_iface = nm_device_get_applied_setting (device, NM_TYPE_SETTING_OVS_INTERFACE); g_return_val_if_fail (s_ovs_iface, FALSE); - return strcmp (nm_setting_ovs_interface_get_interface_type (s_ovs_iface), "internal") == 0; + return nm_streq (nm_setting_ovs_interface_get_interface_type (s_ovs_iface), "internal"); } static NMActStageReturn diff --git a/src/devices/ovs/nm-ovsdb.c b/src/devices/ovs/nm-ovsdb.c index b00397cf..48f6b4f3 100644 --- a/src/devices/ovs/nm-ovsdb.c +++ b/src/devices/ovs/nm-ovsdb.c @@ -1126,7 +1126,7 @@ ovsdb_got_msg (NMOvsdb *self, json_t *msg) ovsdb_disconnect (self, FALSE); return; } - /* Cool, we found a corresponsing call. Finish it. */ + /* Cool, we found a corresponding call. Finish it. */ _call_trace ("response", call, msg); diff --git a/src/devices/team/meson.build b/src/devices/team/meson.build index 0f0763bd..3f755012 100644 --- a/src/devices/team/meson.build +++ b/src/devices/team/meson.build @@ -1,12 +1,12 @@ sources = files( 'nm-device-team.c', - 'nm-team-factory.c' + 'nm-team-factory.c', ) deps = [ jansson_dep, libteamdctl_dep, - nm_dep + nm_dep, ] libnm_device_plugin_team = shared_module( @@ -16,7 +16,7 @@ libnm_device_plugin_team = shared_module( link_args: ldflags_linker_script_devices, link_depends: linker_script_devices, install: true, - install_dir: nm_plugindir + install_dir: nm_plugindir, ) core_plugins += libnm_device_plugin_team diff --git a/src/devices/team/nm-device-team.c b/src/devices/team/nm-device-team.c index 899932fd..38a6dd8c 100644 --- a/src/devices/team/nm-device-team.c +++ b/src/devices/team/nm-device-team.c @@ -34,6 +34,7 @@ #include "NetworkManagerUtils.h" #include "devices/nm-device-private.h" #include "platform/nm-platform.h" +#include "nm-config.h" #include "nm-core-internal.h" #include "nm-ip4-config.h" #include "nm-dbus-compat.h" @@ -479,11 +480,25 @@ teamd_child_setup (gpointer user_data) signal (SIGPIPE, SIG_IGN); } +static const char ** +teamd_env (void) +{ + const char **env = g_new0 (const char *, 2); + + if (nm_config_get_is_debug (nm_config_get ())) + env[0] = "TEAM_LOG_OUTPUT=stderr"; + else + env[0] = "TEAM_LOG_OUTPUT=syslog"; + + return env; +} + static gboolean teamd_kill (NMDeviceTeam *self, const char *teamd_binary, GError **error) { gs_unref_ptrarray GPtrArray *argv = NULL; gs_free char *tmp_str = NULL; + gs_free const char **envp = NULL; if (!teamd_binary) { teamd_binary = nm_utils_find_helper ("teamd", NULL, error); @@ -500,8 +515,11 @@ teamd_kill (NMDeviceTeam *self, const char *teamd_binary, GError **error) g_ptr_array_add (argv, (gpointer) nm_device_get_iface (NM_DEVICE (self))); g_ptr_array_add (argv, NULL); + envp = teamd_env (); + _LOGD (LOGD_TEAM, "running: %s", (tmp_str = g_strjoinv (" ", (char **) argv->pdata))); - return g_spawn_sync ("/", (char **) argv->pdata, NULL, 0, teamd_child_setup, NULL, NULL, NULL, NULL, error); + return g_spawn_sync ("/", (char **) argv->pdata, (char **) envp, 0, + teamd_child_setup, NULL, NULL, NULL, NULL, error); } static gboolean @@ -518,6 +536,7 @@ teamd_start (NMDevice *device, NMConnection *connection) nm_auto_free const char *config_free = NULL; NMSettingTeam *s_team; gs_free char *cloned_mac = NULL; + gs_free const char **envp = NULL; s_team = nm_connection_get_setting_team (connection); g_return_val_if_fail (s_team, FALSE); @@ -588,8 +607,10 @@ teamd_start (NMDevice *device, NMConnection *connection) g_ptr_array_add (argv, (gpointer) "-gg"); g_ptr_array_add (argv, NULL); + envp = teamd_env (); + _LOGD (LOGD_TEAM, "running: %s", (tmp_str = g_strjoinv (" ", (char **) argv->pdata))); - if (!g_spawn_async ("/", (char **) argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD, + if (!g_spawn_async ("/", (char **) argv->pdata, (char **) envp, G_SPAWN_DO_NOT_REAP_CHILD, teamd_child_setup, NULL, &priv->teamd_pid, &error)) { _LOGW (LOGD_TEAM, "Activation: (team) failed to start teamd: %s", error->message); teamd_cleanup (device, TRUE); @@ -626,6 +647,7 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason) connection = nm_device_get_applied_connection (device); g_return_val_if_fail (connection, NM_ACT_STAGE_RETURN_FAILURE); + s_team = nm_connection_get_setting_team (connection); g_return_val_if_fail (s_team, NM_ACT_STAGE_RETURN_FAILURE); @@ -782,15 +804,15 @@ create_and_realize (NMDevice *device, GError **error) { const char *iface = nm_device_get_iface (device); - NMPlatformError plerr; + int r; - plerr = nm_platform_link_team_add (nm_device_get_platform (device), iface, out_plink); - if (plerr != NM_PLATFORM_ERROR_SUCCESS) { + r = nm_platform_link_team_add (nm_device_get_platform (device), iface, out_plink); + if (r < 0) { g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED, "Failed to create team master interface '%s' for '%s': %s", iface, nm_connection_get_id (connection), - nm_platform_error_to_string_a (plerr)); + nm_strerror (r)); return FALSE; } diff --git a/src/devices/tests/meson.build b/src/devices/tests/meson.build index 02c61ced..11f35a05 100644 --- a/src/devices/tests/meson.build +++ b/src/devices/tests/meson.build @@ -1,18 +1,18 @@ test_units = [ 'test-acd', - 'test-lldp' + 'test-lldp', ] foreach test_unit: test_units exe = executable( test_unit, test_unit + '.c', - dependencies: test_nm_dep + dependencies: test_nm_dep, ) test( 'devices/' + test_unit, test_script, - args: test_args + [exe.full_path()] + args: test_args + [exe.full_path()], ) endforeach diff --git a/src/devices/tests/test-acd.c b/src/devices/tests/test-acd.c index 8a2852a2..aff71825 100644 --- a/src/devices/tests/test-acd.c +++ b/src/devices/tests/test-acd.c @@ -20,6 +20,8 @@ #include "nm-default.h" +#include "n-acd/src/n-acd.h" + #include "devices/nm-acd-manager.h" #include "platform/tests/test-common.h" @@ -31,6 +33,46 @@ #define ADDR3 0x03030303 #define ADDR4 0x04040404 +/*****************************************************************************/ + +static gboolean +_skip_acd_test_check (void) +{ + NAcd *acd; + NAcdConfig *config; + const guint8 hwaddr[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + int r; + static int skip = -1; + + if (skip == -1) { + r = n_acd_config_new (&config); + g_assert (r == 0); + + n_acd_config_set_ifindex (config, 1); + n_acd_config_set_transport (config, N_ACD_TRANSPORT_ETHERNET); + n_acd_config_set_mac (config, hwaddr, sizeof (hwaddr)); + + r = n_acd_new (&acd, config); + n_acd_config_free (config); + if (r == 0) + n_acd_unref (acd); + + skip = (r != 0); + } + return skip; +} + +#define _skip_acd_test() \ + ({ \ + gboolean _skip = _skip_acd_test_check (); \ + \ + if (_skip) \ + g_test_skip ("Cannot create NAcd. Running under valgind?"); \ + _skip; \ + }) + +/*****************************************************************************/ + typedef struct { int ifindex0; int ifindex1; @@ -61,20 +103,26 @@ typedef struct { } TestInfo; static void -acd_manager_probe_terminated (NMAcdManager *acd_manager, GMainLoop *loop) +acd_manager_probe_terminated (NMAcdManager *acd_manager, gpointer user_data) { - g_main_loop_quit (loop); + g_main_loop_quit (user_data); } static void test_acd_common (test_fixture *fixture, TestInfo *info) { - gs_unref_object NMAcdManager *manager = NULL; + NMAcdManager *manager; GMainLoop *loop; int i; const guint WAIT_TIME_OPTIMISTIC = 50; guint wait_time; - gulong signal_id; + static const NMAcdCallbacks callbacks = { + .probe_terminated_callback = acd_manager_probe_terminated, + .user_data_destroy = (GDestroyNotify) g_main_loop_unref, + }; + + if (_skip_acd_test ()) + return; /* first, try with a short waittime. We hope that this is long enough * to successfully complete the test. Only if that's not the case, we @@ -83,7 +131,13 @@ test_acd_common (test_fixture *fixture, TestInfo *info) wait_time = WAIT_TIME_OPTIMISTIC; again: - manager = nm_acd_manager_new (fixture->ifindex0, fixture->hwaddr0, fixture->hwaddr0_len); + loop = g_main_loop_new (NULL, FALSE); + + manager = nm_acd_manager_new (fixture->ifindex0, + fixture->hwaddr0, + fixture->hwaddr0_len, + &callbacks, + g_main_loop_ref (loop)); g_assert (manager != NULL); for (i = 0; info->addresses[i]; i++) @@ -94,16 +148,13 @@ again: 24, 0, 3600, 1800, 0, NULL); } - loop = g_main_loop_new (NULL, FALSE); - signal_id = g_signal_connect (manager, NM_ACD_MANAGER_PROBE_TERMINATED, - G_CALLBACK (acd_manager_probe_terminated), loop); g_assert (nm_acd_manager_start_probe (manager, wait_time)); g_assert (nmtst_main_loop_run (loop, 2000)); - g_signal_handler_disconnect (manager, signal_id); g_main_loop_unref (loop); for (i = 0; info->addresses[i]; i++) { gboolean val; + char sbuf[NM_UTILS_INET_ADDRSTRLEN]; val = nm_acd_manager_check_address (manager, info->addresses[i]); if (val == info->expected_result[i]) @@ -113,14 +164,16 @@ again: /* probably we just had a glitch and the system took longer than * expected. Re-verify with a large timeout this time. */ wait_time = 1000; - g_clear_object (&manager); + nm_clear_pointer (&manager, nm_acd_manager_free); goto again; } g_error ("expected check for address #%d (%s) to %s, but it didn't", - i, nm_utils_inet4_ntop (info->addresses[i], NULL), + i, nm_utils_inet4_ntop (info->addresses[i], sbuf), info->expected_result[i] ? "detect no duplicated" : "detect a duplicate"); } + + nm_acd_manager_free (manager); } static void @@ -146,10 +199,17 @@ test_acd_probe_2 (test_fixture *fixture, gconstpointer user_data) static void test_acd_announce (test_fixture *fixture, gconstpointer user_data) { - gs_unref_object NMAcdManager *manager = NULL; + NMAcdManager *manager; GMainLoop *loop; - manager = nm_acd_manager_new (fixture->ifindex0, fixture->hwaddr0, fixture->hwaddr0_len); + if (_skip_acd_test ()) + return; + + manager = nm_acd_manager_new (fixture->ifindex0, + fixture->hwaddr0, + fixture->hwaddr0_len, + NULL, + NULL); g_assert (manager != NULL); g_assert (nm_acd_manager_add_address (manager, ADDR1)); @@ -159,6 +219,8 @@ test_acd_announce (test_fixture *fixture, gconstpointer user_data) nm_acd_manager_announce_addresses (manager); g_assert (!nmtst_main_loop_run (loop, 200)); g_main_loop_unref (loop); + + nm_acd_manager_free (manager); } static void diff --git a/src/devices/wifi/meson.build b/src/devices/wifi/meson.build index 2745040b..dd2be540 100644 --- a/src/devices/wifi/meson.build +++ b/src/devices/wifi/meson.build @@ -1,13 +1,13 @@ common_sources = files( 'nm-wifi-ap.c', - 'nm-wifi-utils.c' + 'nm-wifi-utils.c', ) sources = common_sources + files( 'nm-wifi-factory.c', 'nm-wifi-common.c', 'nm-device-wifi.c', - 'nm-device-olpc-mesh.c' + 'nm-device-olpc-mesh.c', ) if enable_iwd @@ -18,7 +18,7 @@ if enable_iwd endif deps = [ - nm_dep + nm_dep, ] libnm_device_plugin_wifi = shared_module( @@ -28,7 +28,7 @@ libnm_device_plugin_wifi = shared_module( link_args: ldflags_linker_script_devices, link_depends: linker_script_devices, install: true, - install_dir: nm_plugindir + install_dir: nm_plugindir, ) core_plugins += libnm_device_plugin_wifi diff --git a/src/devices/wifi/nm-device-iwd.c b/src/devices/wifi/nm-device-iwd.c index 1d1be742..71b8d151 100644 --- a/src/devices/wifi/nm-device-iwd.c +++ b/src/devices/wifi/nm-device-iwd.c @@ -41,6 +41,7 @@ #include "nm-config.h" #include "nm-iwd-manager.h" #include "nm-dbus-manager.h" +#include "nm-dbus-compat.h" #include "devices/nm-device-logging.h" _LOG_DECLARE_SELF(NMDeviceIwd); @@ -69,6 +70,8 @@ typedef struct { GDBusObject * dbus_obj; GDBusProxy * dbus_device_proxy; GDBusProxy * dbus_station_proxy; + GDBusProxy * dbus_ap_proxy; + GDBusProxy * dbus_adhoc_proxy; CList aps_lst_head; NMWifiAP * current_ap; GCancellable * cancellable; @@ -80,6 +83,7 @@ typedef struct { bool can_connect:1; bool scanning:1; bool scan_requested:1; + bool act_mode_switch:1; gint64 last_scan; } NMDeviceIwdPrivate; @@ -104,7 +108,7 @@ G_DEFINE_TYPE (NMDeviceIwd, nm_device_iwd, NM_TYPE_DEVICE) /*****************************************************************************/ static void schedule_periodic_scan (NMDeviceIwd *self, - NMDeviceState current_state); + gboolean initial_scan); /*****************************************************************************/ @@ -229,7 +233,11 @@ vardict_from_network_type (const char *type) } static void -insert_ap_from_network (GHashTable *aps, const char *path, int16_t signal, uint32_t ap_id) +insert_ap_from_network (NMDeviceIwd *self, + GHashTable *aps, + const char *path, + int16_t signal, + uint32_t ap_id) { gs_unref_object GDBusProxy *network_proxy = NULL; gs_unref_variant GVariant *name_value = NULL, *type_value = NULL; @@ -240,6 +248,11 @@ insert_ap_from_network (GHashTable *aps, const char *path, int16_t signal, uint3 uint8_t bssid[6]; NMWifiAP *ap; + if (g_hash_table_lookup (aps, path)) { + _LOGD (LOGD_WIFI, "Duplicate network at %s", path); + return; + } + network_proxy = nm_iwd_manager_get_dbus_interface (nm_iwd_manager_get (), path, NM_IWD_NETWORK_INTERFACE); @@ -304,7 +317,7 @@ static void get_ordered_networks_cb (GObject *source, GAsyncResult *res, gpointer user_data) { NMDeviceIwd *self = user_data; - NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); + NMDeviceIwdPrivate *priv; gs_free_error GError *error = NULL; gs_unref_variant GVariant *variant = NULL; GVariantIter *networks; @@ -314,33 +327,42 @@ get_ordered_networks_cb (GObject *source, GAsyncResult *res, gpointer user_data) gboolean changed = FALSE; GHashTableIter ap_iter; gs_unref_hashtable GHashTable *new_aps = NULL; - /* Depending on whether we're using the Station interface or the Device - * interface for compatibility with IWD <= 0.7, the return signature of - * GetOrderedNetworks will be different. - */ - gboolean compat = priv->dbus_station_proxy == priv->dbus_device_proxy; - const char *return_sig = compat ? "(a(osns))" : "(a(on))"; + gboolean compat; + const char *return_sig; static uint32_t ap_id = 0; - variant = _nm_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, - G_VARIANT_TYPE (return_sig), - &error); + variant = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, &error); if (!variant) { _LOGE (LOGD_WIFI, "Station.GetOrderedNetworks failed: %s", error->message); return; } + priv = NM_DEVICE_IWD_GET_PRIVATE (self); + + /* Depending on whether we're using the Station interface or the Device + * interface for compatibility with IWD <= 0.7, the return signature of + * GetOrderedNetworks will be different. + */ + compat = priv->dbus_station_proxy == priv->dbus_device_proxy; + return_sig = compat ? "(a(osns))" : "(a(on))"; + + if (!g_variant_is_of_type (variant, G_VARIANT_TYPE (return_sig))) { + _LOGE (LOGD_WIFI, "Station.GetOrderedNetworks returned type %s instead of %s", + g_variant_get_type_string (variant), return_sig); + return; + } + new_aps = g_hash_table_new_full (nm_str_hash, g_str_equal, NULL, g_object_unref); g_variant_get (variant, return_sig, &networks); if (compat) { while (g_variant_iter_next (networks, "(&o&sn&s)", &path, &name, &signal, &type)) - insert_ap_from_network (new_aps, path, signal, ap_id++); + insert_ap_from_network (self, new_aps, path, signal, ap_id++); } else { while (g_variant_iter_next (networks, "(&on)", &path, &signal)) - insert_ap_from_network (new_aps, path, signal, ap_id++); + insert_ap_from_network (self, new_aps, path, signal, ap_id++); } g_variant_iter_free (networks); @@ -393,7 +415,7 @@ update_aps (NMDeviceIwd *self) priv->cancellable = g_cancellable_new (); g_dbus_proxy_call (priv->dbus_station_proxy, "GetOrderedNetworks", - g_variant_new ("()"), G_DBUS_CALL_FLAGS_NONE, + NULL, G_DBUS_CALL_FLAGS_NONE, 2000, priv->cancellable, get_ordered_networks_cb, self); } @@ -403,8 +425,8 @@ send_disconnect (NMDeviceIwd *self) { NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); - g_dbus_proxy_call (priv->dbus_station_proxy, "Disconnect", g_variant_new ("()"), - G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL); + g_dbus_proxy_call (priv->dbus_station_proxy, "Disconnect", + NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL); } static void @@ -431,55 +453,104 @@ cleanup_association_attempt (NMDeviceIwd *self, gboolean disconnect) } static void +reset_mode (NMDeviceIwd *self, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); + + g_dbus_proxy_call (priv->dbus_device_proxy, + DBUS_INTERFACE_PROPERTIES ".Set", + g_variant_new ("(ssv)", NM_IWD_DEVICE_INTERFACE, + "Mode", + g_variant_new_string ("station")), + G_DBUS_CALL_FLAGS_NONE, 2000, + cancellable, + callback, + user_data); +} + +static void deactivate (NMDevice *device) { - cleanup_association_attempt (NM_DEVICE_IWD (device), TRUE); + NMDeviceIwd *self = NM_DEVICE_IWD (device); + NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); + + if (!priv->dbus_obj) + return; + + cleanup_association_attempt (self, TRUE); + priv->act_mode_switch = FALSE; + + if (!priv->dbus_station_proxy) + reset_mode (self, NULL, NULL, NULL); } -static gboolean -deactivate_async_finish (NMDevice *device, GAsyncResult *res, GError **error) +static void +disconnect_cb (GObject *source, GAsyncResult *res, gpointer user_data) { - NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (NM_DEVICE_IWD (device)); + gs_unref_object NMDeviceIwd *self = NULL; + NMDeviceDeactivateCallback callback; + gpointer callback_user_data; gs_unref_variant GVariant *variant = NULL; + gs_free_error GError *error = NULL; - variant = g_dbus_proxy_call_finish (priv->dbus_station_proxy, res, error); - return variant != NULL; -} + nm_utils_user_data_unpack (user_data, &self, &callback, &callback_user_data); -typedef struct { - NMDeviceIwd *self; - GAsyncReadyCallback callback; - gpointer user_data; -} DeactivateContext; + variant = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, &error); + callback (NM_DEVICE (self), error, callback_user_data); +} static void -disconnect_cb (GObject *source, GAsyncResult *res, gpointer user_data) +disconnect_cb_on_idle (gpointer user_data, + GCancellable *cancellable) { - DeactivateContext *ctx = user_data; + gs_unref_object NMDeviceIwd *self = NULL; + NMDeviceDeactivateCallback callback; + gpointer callback_user_data; + gs_free_error GError *cancelled_error = NULL; - ctx->callback (G_OBJECT (ctx->self), res, ctx->user_data); + nm_utils_user_data_unpack (user_data, &self, &callback, &callback_user_data); - g_object_unref (ctx->self); - g_slice_free (DeactivateContext, ctx); + g_cancellable_set_error_if_cancelled (cancellable, &cancelled_error); + callback (NM_DEVICE (self), cancelled_error, callback_user_data); } static void deactivate_async (NMDevice *device, GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data) + NMDeviceDeactivateCallback callback, + gpointer callback_user_data) { NMDeviceIwd *self = NM_DEVICE_IWD (device); NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); - DeactivateContext *ctx; + gpointer user_data; + + nm_assert (G_IS_CANCELLABLE (cancellable)); + nm_assert (callback); - ctx = g_slice_new0 (DeactivateContext); - ctx->self = g_object_ref (self); - ctx->callback = callback; - ctx->user_data = user_data; + user_data = nm_utils_user_data_pack (g_object_ref (self), callback, callback_user_data); - g_dbus_proxy_call (priv->dbus_station_proxy, "Disconnect", g_variant_new ("()"), - G_DBUS_CALL_FLAGS_NONE, -1, cancellable, disconnect_cb, ctx); + if (!priv->dbus_obj) { + nm_utils_invoke_on_idle (disconnect_cb_on_idle, user_data, cancellable); + return; + } + + cleanup_association_attempt (self, FALSE); + priv->act_mode_switch = FALSE; + + if (priv->dbus_station_proxy) { + g_dbus_proxy_call (priv->dbus_station_proxy, + "Disconnect", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + cancellable, + disconnect_cb, + user_data); + } else + reset_mode (self, cancellable, disconnect_cb, user_data); } static gboolean @@ -510,13 +581,37 @@ is_connection_known_network (NMConnection *connection) } static gboolean +is_ap_known_network (NMWifiAP *ap) +{ + GDBusProxy *network_proxy; + gs_unref_variant GVariant *known_network = NULL; + + network_proxy = nm_iwd_manager_get_dbus_interface (nm_iwd_manager_get (), + nm_wifi_ap_get_supplicant_path (ap), + NM_IWD_NETWORK_INTERFACE); + if (!network_proxy) + return FALSE; + + known_network = g_dbus_proxy_get_cached_property (network_proxy, "KnownNetwork"); + g_object_unref (network_proxy); + + return known_network + && g_variant_is_of_type (known_network, G_VARIANT_TYPE_OBJECT_PATH); +} + +static gboolean check_connection_compatible (NMDevice *device, NMConnection *connection, GError **error) { + NMDeviceIwd *self = NM_DEVICE_IWD (device); + NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); NMSettingWireless *s_wireless; const char *mac; const char * const *mac_blacklist; int i; const char *perm_hw_addr; + const char *mode; + NMIwdNetworkSecurity security; + gboolean mapped; if (!NM_DEVICE_CLASS (nm_device_iwd_parent_class)->check_connection_compatible (device, connection, error)) return FALSE; @@ -549,23 +644,65 @@ check_connection_compatible (NMDevice *device, NMConnection *connection, GError return FALSE; } - if (!NM_IN_STRSET (nm_setting_wireless_get_mode (s_wireless), - NULL, - NM_SETTING_WIRELESS_MODE_INFRA)) { + /* Hidden SSIDs not supported in any mode (client or AP) */ + if (nm_setting_wireless_get_hidden (s_wireless)) { nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, - "IWD only support infrastructure type profiles"); + "hidden networks not supported by the IWD backend"); return FALSE; } - /* 8021x networks can only be used if they've been provisioned on the IWD side and - * thus are Known Networks. - */ - if (nm_wifi_connection_get_iwd_security (connection, NULL) == NM_IWD_NETWORK_SECURITY_8021X) { - if (!is_connection_known_network (connection)) { + security = nm_wifi_connection_get_iwd_security (connection, &mapped); + if (!mapped) { + nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, + "connection authentication type not supported by IWD backend"); + return FALSE; + } + + mode = nm_setting_wireless_get_mode (s_wireless); + if (NM_IN_STRSET (mode, NULL, NM_SETTING_WIRELESS_MODE_INFRA)) { + /* 8021x networks can only be used if they've been provisioned on the IWD side and + * thus are Known Networks. + */ + if (security == NM_IWD_NETWORK_SECURITY_8021X) { + if (!is_connection_known_network (connection)) { + nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, + "802.1x connections must have IWD provisioning files"); + return FALSE; + } + } else if (!NM_IN_SET (security, NM_IWD_NETWORK_SECURITY_NONE, NM_IWD_NETWORK_SECURITY_PSK)) { + nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, + "IWD backend only supports Open, PSK and 802.1x network " + "authentication in Infrastructure mode"); + return FALSE; + } + } else if (nm_streq (mode, NM_SETTING_WIRELESS_MODE_AP)) { + if (!(priv->capabilities & NM_WIFI_DEVICE_CAP_AP)) { + nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, + "device does not support Access Point mode"); + return FALSE; + } + + if (!NM_IN_SET (security, NM_IWD_NETWORK_SECURITY_PSK)) { + nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, + "IWD backend only supports PSK authentication in AP mode"); + return FALSE; + } + } else if (nm_streq (mode, NM_SETTING_WIRELESS_MODE_ADHOC)) { + if (!(priv->capabilities & NM_WIFI_DEVICE_CAP_ADHOC)) { + nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, + "device does not support Ad-Hoc mode"); + return FALSE; + } + + if (!NM_IN_SET (security, NM_IWD_NETWORK_SECURITY_NONE, NM_IWD_NETWORK_SECURITY_PSK)) { nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, - "802.1x profile is not a known network"); + "IWD backend only supports Open and PSK authentication in Ad-Hoc mode"); return FALSE; } + } else { + nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, + "%s type profiles not supported by IWD backend"); + return FALSE; } return TRUE; @@ -582,42 +719,15 @@ check_connection_available (NMDevice *device, NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); NMSettingWireless *s_wifi; const char *mode; + NMWifiAP *ap = NULL; s_wifi = nm_connection_get_setting_wireless (connection); g_return_val_if_fail (s_wifi, FALSE); - /* Only Infrastrusture mode at this time */ - mode = nm_setting_wireless_get_mode (s_wifi); - if (!NM_IN_STRSET (mode, NULL, NM_SETTING_WIRELESS_MODE_INFRA)) { - nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, - "iwd only supports infrastructure mode connections"); - return FALSE; - } - - /* Hidden SSIDs not supported yet */ - if (nm_setting_wireless_get_hidden (s_wifi)) { - nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, - "hidden networks not supported by iwd"); - return FALSE; - } - - /* 8021x networks can only be used if they've been provisioned on the IWD side and - * thus are Known Networks. - */ - if (nm_wifi_connection_get_iwd_security (connection, NULL) == NM_IWD_NETWORK_SECURITY_8021X) { - if (!is_connection_known_network (connection)) { - nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, - "network is not known to iwd"); - return FALSE; - } - } - /* a connection that is available for a certain @specific_object, MUST * also be available in general (without @specific_object). */ if (specific_object) { - NMWifiAP *ap; - ap = nm_wifi_ap_lookup_for_device (NM_DEVICE (self), specific_object); if (!ap) { nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, @@ -629,18 +739,36 @@ check_connection_available (NMDevice *device, "requested access point is not compatible with profile"); return FALSE; } - return TRUE; } + /* AP and Ad-Hoc connections can be activated independent of the scan list */ + mode = nm_setting_wireless_get_mode (s_wifi); + if (NM_IN_STRSET (mode, NM_SETTING_WIRELESS_MODE_AP, NM_SETTING_WIRELESS_MODE_ADHOC)) + return TRUE; + if (NM_FLAGS_HAS (flags, _NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST_IGNORE_AP)) return TRUE; - if (!nm_wifi_aps_find_first_compatible (&priv->aps_lst_head, connection)) { + if (!ap) + ap = nm_wifi_aps_find_first_compatible (&priv->aps_lst_head, connection); + + if (!ap) { nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, "no compatible access point found"); return FALSE; } + /* 8021x networks can only be used if they've been provisioned on the IWD side and + * thus are Known Networks. + */ + if (nm_wifi_connection_get_iwd_security (connection, NULL) == NM_IWD_NETWORK_SECURITY_8021X) { + if (!is_ap_known_network (ap)) { + nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, + "802.1x network is not an IWD Known Network (missing provisioning file?)"); + return FALSE; + } + } + return TRUE; } @@ -666,15 +794,11 @@ complete_connection (NMDevice *device, mode = s_wifi ? nm_setting_wireless_get_mode (s_wifi) : NULL; - if (mode && !nm_streq0 (mode, NM_SETTING_WIRELESS_MODE_INFRA)) { - g_set_error_literal (error, - NM_DEVICE_ERROR, - NM_DEVICE_ERROR_INVALID_CONNECTION, - "Only Infrastructure mode is supported."); - return FALSE; - } - - if (!specific_object) { + if (nm_streq0 (mode, NM_SETTING_WIRELESS_MODE_AP)) { + if (!nm_setting_verify (NM_SETTING (s_wifi), connection, error)) + return FALSE; + ap = NULL; + } else if (!specific_object) { /* If not given a specific object, we need at minimum an SSID */ if (!s_wifi) { g_set_error_literal (error, @@ -696,11 +820,16 @@ complete_connection (NMDevice *device, /* Find a compatible AP in the scan list */ ap = nm_wifi_aps_find_first_compatible (&priv->aps_lst_head, connection); if (!ap) { - g_set_error_literal (error, - NM_DEVICE_ERROR, - NM_DEVICE_ERROR_INVALID_CONNECTION, - "No compatible AP in the scan list and hidden SSIDs not supported."); - return FALSE; + if (!nm_streq0 (mode, NM_SETTING_WIRELESS_MODE_ADHOC)) { + g_set_error_literal (error, + NM_DEVICE_ERROR, + NM_DEVICE_ERROR_INVALID_CONNECTION, + "No compatible AP in the scan list and hidden SSIDs not supported."); + return FALSE; + } + + if (!nm_setting_verify (NM_SETTING (s_wifi), connection, error)) + return FALSE; } } else { ap = nm_wifi_ap_lookup_for_device (NM_DEVICE (self), specific_object); @@ -720,7 +849,10 @@ complete_connection (NMDevice *device, nm_connection_add_setting (connection, NM_SETTING (s_wifi)); } - ssid = nm_wifi_ap_get_ssid (ap); + ssid = nm_setting_wireless_get_ssid (s_wifi); + if (!ssid && ap) + ssid = nm_wifi_ap_get_ssid (ap); + if (!ssid) { g_set_error_literal (error, NM_DEVICE_ERROR, @@ -729,11 +861,13 @@ complete_connection (NMDevice *device, return FALSE; } - if (!nm_wifi_ap_complete_connection (ap, - connection, - nm_wifi_utils_is_manf_default_ssid (ssid), - error)) - return FALSE; + if (ap) { + if (!nm_wifi_ap_complete_connection (ap, + connection, + nm_wifi_utils_is_manf_default_ssid (ssid), + error)) + return FALSE; + } ssid_utf8 = _nm_utils_ssid_to_utf8 (ssid); nm_utils_complete_generic (nm_device_get_platform (device), @@ -745,19 +879,6 @@ complete_connection (NMDevice *device, NULL, TRUE); - /* 8021x networks can only be used if they've been provisioned on the IWD side and - * thus are Known Networks. - */ - if (nm_wifi_connection_get_iwd_security (connection, NULL) == NM_IWD_NETWORK_SECURITY_8021X) { - if (!is_connection_known_network (connection)) { - g_set_error_literal (error, - NM_CONNECTION_ERROR, - NM_DEVICE_ERROR_INVALID_CONNECTION, - "This 8021x network has not been provisioned on this machine"); - return FALSE; - } - } - perm_hw_addr = nm_device_get_permanent_hw_address (device); if (perm_hw_addr) { setting_mac = nm_setting_wireless_get_mac_address (s_wifi); @@ -820,8 +941,21 @@ is_available (NMDevice *device, NMDeviceCheckDevAvailableFlags flags) { NMDeviceIwd *self = NM_DEVICE_IWD (device); NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); - - return priv->enabled && priv->dbus_station_proxy; + NMDeviceState state = nm_device_get_state (device); + + /* Available if either the device is UP and in station mode + * or in AP/Ad-Hoc modes while activating or activated. Device + * may be temporarily DOWN while activating or deactivating and + * we don't want it to be marked unavailable because of this. + * + * For reference: + * We call nm_device_queue_recheck_available whenever + * priv->enabled changes or priv->dbus_station_proxy changes. + */ + return priv->dbus_obj + && priv->enabled + && ( priv->dbus_station_proxy + || (state >= NM_DEVICE_STATE_CONFIG && state <= NM_DEVICE_STATE_DEACTIVATING)); } static gboolean @@ -829,8 +963,7 @@ get_autoconnect_allowed (NMDevice *device) { NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (NM_DEVICE_IWD (device)); - return is_available (device, NM_DEVICE_CHECK_DEV_AVAILABLE_NONE) - && priv->can_connect; + return priv->can_connect; } static gboolean @@ -856,7 +989,9 @@ can_auto_connect (NMDevice *device, s_wifi = nm_connection_get_setting_wireless (connection); g_return_val_if_fail (s_wifi, FALSE); - /* Only Infrastrusture mode */ + /* Don't auto-activate AP or Ad-Hoc connections. + * Note the wpa_supplicant backend has the opposite policy. + */ mode = nm_setting_wireless_get_mode (s_wifi); if (mode && g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_INFRA) != 0) return FALSE; @@ -870,14 +1005,6 @@ can_auto_connect (NMDevice *device, return FALSE; } - /* 8021x networks can only be used if they've been provisioned on the IWD side and - * thus are Known Networks. - */ - if (nm_wifi_connection_get_iwd_security (connection, NULL) == NM_IWD_NETWORK_SECURITY_8021X) { - if (!is_connection_known_network (connection)) - return FALSE; - } - ap = nm_wifi_aps_find_first_compatible (&priv->aps_lst_head, connection); if (ap) { /* All good; connection is usable */ @@ -908,11 +1035,11 @@ scan_cb (GObject *source, GAsyncResult *res, gpointer user_data) { NMDeviceIwd *self = user_data; NMDeviceIwdPrivate *priv; + gs_unref_variant GVariant *variant = NULL; gs_free_error GError *error = NULL; - if ( !_nm_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, - G_VARIANT_TYPE ("()"), &error) - && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + variant = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, &error); + if (!variant && nm_utils_error_is_cancelled (error, FALSE)) return; priv = NM_DEVICE_IWD_GET_PRIVATE (self); @@ -925,11 +1052,8 @@ scan_cb (GObject *source, GAsyncResult *res, gpointer user_data) * scheduled when priv->scanning goes back to false. On error, * schedule a retry now. */ - if (error && !priv->scanning) { - NMDeviceState state = nm_device_get_state (NM_DEVICE (self)); - - schedule_periodic_scan (self, state); - } + if (error && !priv->scanning) + schedule_periodic_scan (self, FALSE); } static void @@ -958,9 +1082,7 @@ dbus_request_scan_cb (NMDevice *device, priv = NM_DEVICE_IWD_GET_PRIVATE (self); - if ( !priv->can_scan - || nm_device_get_state (device) < NM_DEVICE_STATE_DISCONNECTED - || nm_device_is_activating (device)) { + if (!priv->can_scan) { g_dbus_method_invocation_return_error_literal (context, NM_DEVICE_ERROR, NM_DEVICE_ERROR_NOT_ALLOWED, @@ -982,8 +1104,7 @@ dbus_request_scan_cb (NMDevice *device, if (!priv->scanning && !priv->scan_requested) { g_dbus_proxy_call (priv->dbus_station_proxy, "Scan", - g_variant_new ("()"), - G_DBUS_CALL_FLAGS_NONE, -1, + NULL, G_DBUS_CALL_FLAGS_NONE, -1, priv->cancellable, scan_cb, self); priv->scan_requested = TRUE; } @@ -999,9 +1120,7 @@ _nm_device_iwd_request_scan (NMDeviceIwd *self, NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); NMDevice *device = NM_DEVICE (self); - if ( !priv->can_scan - || nm_device_get_state (device) < NM_DEVICE_STATE_DISCONNECTED - || nm_device_is_activating (device)) { + if (!priv->can_scan) { g_dbus_method_invocation_return_error_literal (invocation, NM_DEVICE_ERROR, NM_DEVICE_ERROR_NOT_ALLOWED, @@ -1041,8 +1160,6 @@ scanning_prohibited (NMDeviceIwd *self, gboolean periodic) return TRUE; case NM_DEVICE_STATE_DISCONNECTED: case NM_DEVICE_STATE_FAILED: - /* Can always scan when disconnected */ - return FALSE; case NM_DEVICE_STATE_ACTIVATED: break; } @@ -1199,7 +1316,7 @@ wifi_secrets_cb (NMActRequest *req, priv->wifi_secrets_id = NULL; - if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { + if (nm_utils_error_is_cancelled (error, FALSE)) { g_dbus_method_invocation_return_error_literal (invocation, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INVALID_CONNECTION, "NM secrets request cancelled"); @@ -1274,6 +1391,7 @@ network_connect_cb (GObject *source, GAsyncResult *res, gpointer user_data) NMDeviceIwd *self = user_data; NMDevice *device = NM_DEVICE (self); NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); + gs_unref_variant GVariant *variant = NULL; gs_free_error GError *error = NULL; NMConnection *connection; NMSettingWireless *s_wifi; @@ -1282,9 +1400,8 @@ network_connect_cb (GObject *source, GAsyncResult *res, gpointer user_data) NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED; GVariant *value; - if (!_nm_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, - G_VARIANT_TYPE ("()"), - &error)) { + variant = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, &error); + if (!variant) { gs_free char *dbus_error = NULL; /* Connection failed; radio problems or if the network wasn't @@ -1295,7 +1412,7 @@ network_connect_cb (GObject *source, GAsyncResult *res, gpointer user_data) "Activation: (wifi) Network.Connect failed: %s", error->message); - if (nm_utils_error_is_cancelled (error, TRUE)) + if (nm_utils_error_is_cancelled (error, FALSE)) return; if (!NM_IN_SET (nm_device_get_state (device), NM_DEVICE_STATE_CONFIG, NM_DEVICE_STATE_NEED_AUTH)) @@ -1359,12 +1476,262 @@ failed: } static void +act_failed_cb (GObject *source, GAsyncResult *res, gpointer user_data) +{ + NMDeviceIwd *self = user_data; + NMDevice *device = NM_DEVICE (self); + gs_unref_variant GVariant *variant = NULL; + gs_free_error GError *error = NULL; + + variant = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, &error); + if (!variant && nm_utils_error_is_cancelled (error, FALSE)) + return; + + /* Change state to FAILED unless already done by state_changed + * which may have been triggered by the station interface + * appearing on DBus. + */ + if (nm_device_get_state (device) == NM_DEVICE_STATE_CONFIG) + nm_device_queue_state (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED); +} + +static void +act_start_cb (GObject *source, GAsyncResult *res, gpointer user_data) +{ + NMDeviceIwd *self = user_data; + NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); + NMDevice *device = NM_DEVICE (self); + gs_unref_variant GVariant *variant = NULL; + gs_free_error GError *error = NULL; + NMSettingWireless *s_wireless; + GBytes *ssid; + gs_free char *ssid_utf8 = NULL; + + variant = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, &error); + if (!variant) { + _LOGE (LOGD_DEVICE | LOGD_WIFI, + "Activation: (wifi) Network.Connect failed: %s", + error->message); + + if (nm_utils_error_is_cancelled (error, FALSE)) + return; + + if (!NM_IN_SET (nm_device_get_state (device), NM_DEVICE_STATE_CONFIG)) + return; + + goto error; + } + + nm_assert (nm_device_get_state (device) == NM_DEVICE_STATE_CONFIG); + + s_wireless = nm_device_get_applied_setting (device, NM_TYPE_SETTING_WIRELESS); + if (!s_wireless) + goto error; + + ssid = nm_setting_wireless_get_ssid (s_wireless); + if (!ssid) + goto error; + + ssid_utf8 = _nm_utils_ssid_to_utf8 (ssid); + + _LOGI (LOGD_DEVICE | LOGD_WIFI, + "Activation: (wifi) Stage 2 of 5 (Device Configure) successful. Started '%s'.", + ssid_utf8); + + nm_device_activate_schedule_stage3_ip_config_start (device); + return; + +error: + reset_mode (self, priv->cancellable, act_failed_cb, self); +} + +/* Check if we're activating an AP/AdHoc connection and if the target + * DBus interface has appeared already. If so proceed to call Start or + * StartOpen on that interface. + */ +static void act_check_interface (NMDeviceIwd *self) +{ + NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); + NMDevice *device = NM_DEVICE (self); + gs_free_error GError *error = NULL; + NMSettingWireless *s_wireless; + NMSettingWirelessSecurity *s_wireless_sec; + GDBusProxy *proxy = NULL; + GBytes *ssid; + gs_free char *ssid_utf8 = NULL; + const char *mode; + + if (!priv->act_mode_switch) + return; + + s_wireless = (NMSettingWireless *) nm_device_get_applied_setting (device, NM_TYPE_SETTING_WIRELESS); + + mode = nm_setting_wireless_get_mode (s_wireless); + if (nm_streq0 (mode, NM_SETTING_WIRELESS_MODE_AP)) + proxy = priv->dbus_ap_proxy; + else if (nm_streq0 (mode, NM_SETTING_WIRELESS_MODE_ADHOC)) + proxy = priv->dbus_adhoc_proxy; + + if (!proxy) + return; + + priv->act_mode_switch = FALSE; + + if (!NM_IN_SET (nm_device_get_state (device), NM_DEVICE_STATE_CONFIG)) + return; + + ssid = nm_setting_wireless_get_ssid (s_wireless); + if (!ssid) + goto failed; + + ssid_utf8 = _nm_utils_ssid_to_utf8 (ssid); + + s_wireless_sec = (NMSettingWirelessSecurity *) nm_device_get_applied_setting (device, NM_TYPE_SETTING_WIRELESS_SECURITY); + + if (!s_wireless_sec) { + g_dbus_proxy_call (proxy, "StartOpen", + g_variant_new ("(s)", ssid_utf8), + G_DBUS_CALL_FLAGS_NONE, G_MAXINT, + priv->cancellable, act_start_cb, self); + } else { + const char *psk = nm_setting_wireless_security_get_psk (s_wireless_sec); + + if (!psk) { + _LOGE (LOGD_DEVICE | LOGD_WIFI, + "Activation: (wifi) No PSK for '%s'.", + ssid_utf8); + goto failed; + } + + g_dbus_proxy_call (proxy, "Start", + g_variant_new ("(ss)", ssid_utf8, psk), + G_DBUS_CALL_FLAGS_NONE, G_MAXINT, + priv->cancellable, act_start_cb, self); + } + + _LOGD (LOGD_DEVICE | LOGD_WIFI, + "Activation: (wifi) Called Start('%s').", + ssid_utf8); + return; + +failed: + reset_mode (self, priv->cancellable, act_failed_cb, self); +} + +static void +act_set_mode_cb (GObject *source, GAsyncResult *res, gpointer user_data) +{ + NMDeviceIwd *self = user_data; + NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); + NMDevice *device = NM_DEVICE (self); + gs_unref_variant GVariant *variant = NULL; + gs_free_error GError *error = NULL; + + variant = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, &error); + if (!variant) { + _LOGE (LOGD_DEVICE | LOGD_WIFI, + "Activation: (wifi) Setting Device.Mode failed: %s", + error->message); + + if (nm_utils_error_is_cancelled (error, FALSE)) + return; + + if ( !NM_IN_SET (nm_device_get_state (device), NM_DEVICE_STATE_CONFIG) + || !priv->act_mode_switch) + return; + + priv->act_mode_switch = FALSE; + nm_device_queue_state (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED); + return; + } + + _LOGD (LOGD_DEVICE | LOGD_WIFI, "Activation: (wifi) IWD Device.Mode set successfully"); + + act_check_interface (self); +} + +static void +act_set_mode (NMDeviceIwd *self) +{ + NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); + NMDevice *device = NM_DEVICE (self); + const char *iwd_mode; + const char *mode; + NMSettingWireless *s_wireless; + + s_wireless = (NMSettingWireless *) nm_device_get_applied_setting (device, NM_TYPE_SETTING_WIRELESS); + mode = nm_setting_wireless_get_mode (s_wireless); + + /* We need to first set interface mode (Device.Mode) to ap or ad-hoc. + * We can't directly queue a call to the Start/StartOpen method on + * the DBus interface that's going to be created after the property + * set call returns. + */ + iwd_mode = nm_streq (mode, NM_SETTING_WIRELESS_MODE_AP) ? "ap" : "ad-hoc"; + + if (!priv->cancellable) + priv->cancellable = g_cancellable_new (); + + g_dbus_proxy_call (priv->dbus_device_proxy, + DBUS_INTERFACE_PROPERTIES ".Set", + g_variant_new ("(ssv)", NM_IWD_DEVICE_INTERFACE, + "Mode", + g_variant_new ("s", iwd_mode)), + G_DBUS_CALL_FLAGS_NONE, 2000, + priv->cancellable, act_set_mode_cb, self); + priv->act_mode_switch = TRUE; +} + +static void +act_psk_cb (NMActRequest *req, + NMActRequestGetSecretsCallId *call_id, + NMSettingsConnection *s_connection, + GError *error, + gpointer user_data) +{ + NMDeviceIwd *self = user_data; + NMDeviceIwdPrivate *priv; + NMDevice *device; + + if (nm_utils_error_is_cancelled (error, FALSE)) + return; + + priv = NM_DEVICE_IWD_GET_PRIVATE (self); + device = NM_DEVICE (self); + + g_return_if_fail (priv->wifi_secrets_id == call_id); + priv->wifi_secrets_id = NULL; + + g_return_if_fail (req == nm_device_get_act_request (device)); + g_return_if_fail (nm_act_request_get_settings_connection (req) == s_connection); + + if (nm_device_get_state (device) != NM_DEVICE_STATE_NEED_AUTH) + goto secrets_error; + + if (error) { + _LOGW (LOGD_WIFI, "%s", error->message); + goto secrets_error; + } + + _LOGD (LOGD_DEVICE | LOGD_WIFI, "Activation: (wifi) missing PSK request completed"); + + /* Change state back to what it was before NEED_AUTH */ + nm_device_state_changed (device, NM_DEVICE_STATE_CONFIG, NM_DEVICE_STATE_REASON_NONE); + act_set_mode (self); + return; + +secrets_error: + nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_NO_SECRETS); + cleanup_association_attempt (self, FALSE); +} + +static void set_powered (NMDeviceIwd *self, gboolean powered) { NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); g_dbus_proxy_call (priv->dbus_device_proxy, - "org.freedesktop.DBus.Properties.Set", + DBUS_INTERFACE_PROPERTIES ".Set", g_variant_new ("(ssv)", NM_IWD_DEVICE_INTERFACE, "Powered", g_variant_new ("b", powered)), @@ -1384,6 +1751,7 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason) NMActRequest *req; NMConnection *connection; NMSettingWireless *s_wireless; + const char *mode; const char *ap_path; ret = NM_DEVICE_CLASS (nm_device_iwd_parent_class)->act_stage1_prepare (device, out_failure_reason); @@ -1399,20 +1767,51 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason) s_wireless = nm_connection_get_setting_wireless (connection); g_return_val_if_fail (s_wireless, NM_ACT_STAGE_RETURN_FAILURE); + /* AP mode never uses a specific object or existing scanned AP */ + mode = nm_setting_wireless_get_mode (s_wireless); + if (nm_streq0 (mode, NM_SETTING_WIRELESS_MODE_AP)) + goto add_new; + ap_path = nm_active_connection_get_specific_object (NM_ACTIVE_CONNECTION (req)); ap = ap_path ? nm_wifi_ap_lookup_for_device (NM_DEVICE (self), ap_path) : NULL; - if (!ap) { - ap = nm_wifi_aps_find_first_compatible (&priv->aps_lst_head, connection); - if (!ap) { - NM_SET_OUT (out_failure_reason, NM_DEVICE_STATE_REASON_CONFIG_FAILED); - return NM_ACT_STAGE_RETURN_FAILURE; - } + if (ap) { + set_current_ap (self, ap, TRUE); + return NM_ACT_STAGE_RETURN_SUCCESS; + } + ap = nm_wifi_aps_find_first_compatible (&priv->aps_lst_head, connection); + if (ap) { nm_active_connection_set_specific_object (NM_ACTIVE_CONNECTION (req), nm_dbus_object_get_path (NM_DBUS_OBJECT (ap))); + set_current_ap (self, ap, TRUE); + return NM_ACT_STAGE_RETURN_SUCCESS; } + if (nm_streq0 (mode, NM_SETTING_WIRELESS_MODE_INFRA)) { + /* Hidden networks not supported at this time */ + return NM_ACT_STAGE_RETURN_FAILURE; + } + +add_new: + /* If the user is trying to connect to an AP that NM doesn't yet know about + * (hidden network or something) or starting a Hotspot, create an fake AP + * from the security settings in the connection. This "fake" AP gets used + * until the real one is found in the scan list (Ad-Hoc or Hidden), or until + * the device is deactivated (Ad-Hoc or Hotspot). + */ + ap = nm_wifi_ap_new_fake_from_connection (connection); + g_return_val_if_fail (ap != NULL, NM_ACT_STAGE_RETURN_FAILURE); + + if (nm_wifi_ap_is_hotspot (ap)) + nm_wifi_ap_set_address (ap, nm_device_get_hw_address (device)); + + g_object_freeze_notify (G_OBJECT (self)); + ap_add_remove (self, TRUE, ap, FALSE); + g_object_thaw_notify (G_OBJECT (self)); set_current_ap (self, ap, FALSE); + nm_active_connection_set_specific_object (NM_ACTIVE_CONNECTION (req), + nm_dbus_object_get_path (NM_DBUS_OBJECT (ap))); + g_object_unref (ap); return NM_ACT_STAGE_RETURN_SUCCESS; } @@ -1423,60 +1822,85 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *out_failure_reason) NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); NMActStageReturn ret = NM_ACT_STAGE_RETURN_FAILURE; NMActRequest *req; - NMWifiAP *ap; NMConnection *connection; - GDBusProxy *network_proxy; + NMSettingWireless *s_wireless; + const char *mode; req = nm_device_get_act_request (device); g_return_val_if_fail (req, NM_ACT_STAGE_RETURN_FAILURE); - ap = priv->current_ap; - if (!ap) { - NM_SET_OUT (out_failure_reason, NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED); - goto out; - } - connection = nm_act_request_get_applied_connection (req); g_assert (connection); - /* 802.1x networks that are not IWD Known Networks will definitely - * fail, for other combinations we will let the Connect call fail - * or ask us for any missing secrets through the Agent. - */ - if ( !is_connection_known_network (connection) - && nm_connection_get_setting_802_1x (connection)) { - _LOGI (LOGD_DEVICE | LOGD_WIFI, - "Activation: (wifi) access point '%s' has 802.1x security, but is not configured.", - nm_connection_get_id (connection)); + s_wireless = nm_connection_get_setting_wireless (connection); + g_return_val_if_fail (s_wireless, NM_ACT_STAGE_RETURN_FAILURE); - NM_SET_OUT (out_failure_reason, NM_DEVICE_STATE_REASON_NO_SECRETS); - ret = NM_ACT_STAGE_RETURN_FAILURE; - goto out; - } + mode = nm_setting_wireless_get_mode (s_wireless); + if (NM_IN_STRSET (mode, NULL, NM_SETTING_WIRELESS_MODE_INFRA)) { + GDBusProxy *network_proxy; + NMWifiAP *ap = priv->current_ap; - network_proxy = nm_iwd_manager_get_dbus_interface (nm_iwd_manager_get (), - nm_wifi_ap_get_supplicant_path (ap), - NM_IWD_NETWORK_INTERFACE); - if (!network_proxy) { - _LOGE (LOGD_DEVICE | LOGD_WIFI, - "Activation: (wifi) could not get Network interface proxy for %s", - nm_wifi_ap_get_supplicant_path (ap)); - NM_SET_OUT (out_failure_reason, NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED); - goto out; - } + if (!ap) { + NM_SET_OUT (out_failure_reason, NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED); + goto out; + } - if (!priv->cancellable) - priv->cancellable = g_cancellable_new (); + /* 802.1x networks that are not IWD Known Networks will definitely + * fail, for other combinations we will let the Connect call fail + * or ask us for any missing secrets through the Agent. + */ + if ( nm_connection_get_setting_802_1x (connection) + && !is_ap_known_network (ap)) { + _LOGI (LOGD_DEVICE | LOGD_WIFI, + "Activation: (wifi) access point '%s' has 802.1x security but is not configured in IWD.", + nm_connection_get_id (connection)); + + NM_SET_OUT (out_failure_reason, NM_DEVICE_STATE_REASON_NO_SECRETS); + goto out; + } - /* Call Network.Connect. No timeout because IWD already handles - * timeouts. - */ - g_dbus_proxy_call (network_proxy, "Connect", - g_variant_new ("()"), - G_DBUS_CALL_FLAGS_NONE, G_MAXINT, - priv->cancellable, network_connect_cb, self); + network_proxy = nm_iwd_manager_get_dbus_interface (nm_iwd_manager_get (), + nm_wifi_ap_get_supplicant_path (ap), + NM_IWD_NETWORK_INTERFACE); + if (!network_proxy) { + _LOGE (LOGD_DEVICE | LOGD_WIFI, + "Activation: (wifi) could not get Network interface proxy for %s", + nm_wifi_ap_get_supplicant_path (ap)); + NM_SET_OUT (out_failure_reason, NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED); + goto out; + } - g_object_unref (network_proxy); + if (!priv->cancellable) + priv->cancellable = g_cancellable_new (); + + /* Call Network.Connect. No timeout because IWD already handles + * timeouts. + */ + g_dbus_proxy_call (network_proxy, "Connect", + NULL, G_DBUS_CALL_FLAGS_NONE, G_MAXINT, + priv->cancellable, network_connect_cb, self); + + g_object_unref (network_proxy); + } else if (NM_IN_STRSET (mode, NM_SETTING_WIRELESS_MODE_AP, NM_SETTING_WIRELESS_MODE_ADHOC)) { + NMSettingWirelessSecurity *s_wireless_sec; + + s_wireless_sec = nm_connection_get_setting_wireless_security (connection); + if (s_wireless_sec && !nm_setting_wireless_security_get_psk (s_wireless_sec)) { + /* PSK is missing from the settings, have to request it */ + + wifi_secrets_cancel (self); + + priv->wifi_secrets_id = nm_act_request_get_secrets (req, + TRUE, + NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, + NM_SECRET_AGENT_GET_SECRETS_FLAG_ALLOW_INTERACTION, + "psk", + act_psk_cb, + self); + nm_device_state_changed (device, NM_DEVICE_STATE_NEED_AUTH, NM_DEVICE_STATE_REASON_NONE); + } else + act_set_mode (self); + } /* We'll get stage3 started when the supplicant connects */ ret = NM_ACT_STAGE_RETURN_POSTPONE; @@ -1507,8 +1931,8 @@ periodic_scan_timeout_cb (gpointer user_data) if (priv->scanning || priv->scan_requested) return FALSE; - g_dbus_proxy_call (priv->dbus_station_proxy, "Scan", g_variant_new ("()"), - G_DBUS_CALL_FLAGS_NONE, -1, + g_dbus_proxy_call (priv->dbus_station_proxy, "Scan", + NULL, G_DBUS_CALL_FLAGS_NONE, -1, priv->cancellable, scan_cb, self); priv->scan_requested = TRUE; @@ -1516,18 +1940,37 @@ periodic_scan_timeout_cb (gpointer user_data) } static void -schedule_periodic_scan (NMDeviceIwd *self, NMDeviceState current_state) +schedule_periodic_scan (NMDeviceIwd *self, gboolean initial_scan) { NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); + GVariant *value; + gboolean disconnected; guint interval; - if (current_state <= NM_DEVICE_STATE_UNAVAILABLE) + if (!priv->can_scan || priv->scan_requested) return; - if (current_state == NM_DEVICE_STATE_DISCONNECTED) - interval = 10; + value = g_dbus_proxy_get_cached_property (priv->dbus_station_proxy, "State"); + disconnected = nm_streq0 (get_variant_state (value), "disconnected"); + g_variant_unref (value); + + /* Start scan immediately after a disconnect, mode change or + * device UP, otherwise wait a period dependent on the current + * state. + * + * (initial_scan && disconnected) override priv->scanning below + * because of an IWD quirk where a device will often be in the + * autoconnect state and scanning at the time of our initial_scan, + * but our logic will the send it a Disconnect() causeing IWD to + * exit autoconnect and interrupt the ongoing scan, meaning that + * we still want a new scan ASAP. + */ + if (initial_scan && disconnected) + interval = 0; + else if (!priv->periodic_scan_id && !priv->scanning) + interval = disconnected ? 10 : 20; else - interval = 20; + return; nm_clear_g_source (&priv->periodic_scan_id); priv->periodic_scan_id = g_timeout_add_seconds (interval, @@ -1536,6 +1979,19 @@ schedule_periodic_scan (NMDeviceIwd *self, NMDeviceState current_state) } static void +set_can_scan (NMDeviceIwd *self, gboolean can_scan) +{ + NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); + + if (priv->can_scan == can_scan) + return; + + priv->can_scan = can_scan; + + schedule_periodic_scan (self, TRUE); +} + +static void device_state_changed (NMDevice *device, NMDeviceState new_state, NMDeviceState old_state, @@ -1544,14 +2000,6 @@ device_state_changed (NMDevice *device, NMDeviceIwd *self = NM_DEVICE_IWD (device); NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); - if (new_state <= NM_DEVICE_STATE_UNAVAILABLE) { - remove_all_aps (self); - nm_clear_g_source (&priv->periodic_scan_id); - } else if (old_state <= NM_DEVICE_STATE_UNAVAILABLE) { - update_aps (self); - schedule_periodic_scan (self, new_state); - } - switch (new_state) { case NM_DEVICE_STATE_UNMANAGED: break; @@ -1669,10 +2117,13 @@ get_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_MODE: - if (priv->current_ap) - g_value_set_uint (value, NM_802_11_MODE_INFRA); - else + if (!priv->current_ap) g_value_set_uint (value, NM_802_11_MODE_UNKNOWN); + else if (nm_wifi_ap_is_hotspot (priv->current_ap)) + g_value_set_uint (value, NM_802_11_MODE_AP); + else + g_value_set_uint (value, nm_wifi_ap_get_mode (priv->current_ap)); + break; case PROP_BITRATE: g_value_set_uint (value, 65000); @@ -1702,24 +2153,6 @@ get_property (GObject *object, guint prop_id, } } -static void -set_property (GObject *object, guint prop_id, - const GValue *value, GParamSpec *pspec) -{ - NMDeviceIwd *device = NM_DEVICE_IWD (object); - NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (device); - - switch (prop_id) { - case PROP_CAPABILITIES: - /* construct-only */ - priv->capabilities = g_value_get_uint (value); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - /*****************************************************************************/ static void @@ -1729,7 +2162,7 @@ state_changed (NMDeviceIwd *self, const char *new_state) NMDevice *device = NM_DEVICE (self); NMDeviceState dev_state = nm_device_get_state (device); gboolean iwd_connection = FALSE; - gboolean can_connect; + gboolean can_connect = priv->can_connect; _LOGI (LOGD_DEVICE | LOGD_WIFI, "new IWD device state is %s", new_state); @@ -1738,7 +2171,9 @@ state_changed (NMDeviceIwd *self, const char *new_state) iwd_connection = TRUE; /* Don't allow scanning while connecting, disconnecting or roaming */ - priv->can_scan = NM_IN_STRSET (new_state, "connected", "disconnected"); + set_can_scan (self, NM_IN_STRSET (new_state, "connected", "disconnected")); + + priv->can_connect = FALSE; if (NM_IN_STRSET (new_state, "connecting", "connected", "roaming")) { /* If we were connecting, do nothing, the confirmation of @@ -1766,7 +2201,7 @@ state_changed (NMDeviceIwd *self, const char *new_state) * callback will have more information on the specific failure * reason. */ - if (dev_state == NM_DEVICE_STATE_CONFIG) + if (NM_IN_SET (dev_state, NM_DEVICE_STATE_CONFIG, NM_DEVICE_STATE_NEED_AUTH)) return; if (iwd_connection) @@ -1781,10 +2216,10 @@ state_changed (NMDeviceIwd *self, const char *new_state) /* Don't allow new connection until iwd exits disconnecting and no * Connect callback is pending. */ - can_connect = NM_IN_STRSET (new_state, "disconnected"); - if (can_connect != priv->can_connect) { - priv->can_connect = can_connect; - nm_device_emit_recheck_auto_activate (device); + if (NM_IN_STRSET (new_state, "disconnected")) { + priv->can_connect = TRUE; + if (!can_connect) + nm_device_emit_recheck_auto_activate (device); } } @@ -1792,7 +2227,6 @@ static void scanning_changed (NMDeviceIwd *self, gboolean new_scanning) { NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); - NMDeviceState state = nm_device_get_state (NM_DEVICE (self)); if (new_scanning == priv->scanning) return; @@ -1805,7 +2239,7 @@ scanning_changed (NMDeviceIwd *self, gboolean new_scanning) update_aps (self); if (!priv->scan_requested) - schedule_periodic_scan (self, state); + schedule_periodic_scan (self, FALSE); } } @@ -1814,40 +2248,81 @@ station_properties_changed (GDBusProxy *proxy, GVariant *changed_properties, GStrv invalidate_properties, gpointer user_data) { NMDeviceIwd *self = user_data; - GVariantIter *iter; - const char *key; - GVariant *value; + const char *new_str; + gboolean new_bool; - g_variant_get (changed_properties, "a{sv}", &iter); - while (g_variant_iter_next (iter, "{&sv}", &key, &value)) { - if (!strcmp (key, "State")) - state_changed (self, get_variant_state (value)); + if (g_variant_lookup (changed_properties, "State", "&s", &new_str)) + state_changed (self, new_str); - if (!strcmp (key, "Scanning")) - scanning_changed (self, get_variant_boolean (value, "Scanning")); + if (g_variant_lookup (changed_properties, "Scanning", "b", &new_bool)) + scanning_changed (self, new_bool); +} - g_variant_unref (value); - } +static void +ap_adhoc_properties_changed (GDBusProxy *proxy, GVariant *changed_properties, + GStrv invalidate_properties, gpointer user_data) +{ + NMDeviceIwd *self = user_data; + gboolean new_bool; - g_variant_iter_free (iter); + if (g_variant_lookup (changed_properties, "Started", "b", &new_bool)) + _LOGI (LOGD_DEVICE | LOGD_WIFI, "IWD AP/AdHoc state is now %s", new_bool ? "Started" : "Stopped"); } static void powered_changed (NMDeviceIwd *self, gboolean new_powered) { NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); + GDBusInterface *interface; + GVariant *value; nm_device_queue_recheck_available (NM_DEVICE (self), NM_DEVICE_STATE_REASON_SUPPLICANT_AVAILABLE, NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED); - if (new_powered) { - GDBusInterface *interface; - GVariant *value; + interface = new_powered ? g_dbus_object_get_interface (priv->dbus_obj, NM_IWD_AP_INTERFACE) : NULL; - if (priv->dbus_station_proxy) - return; + if (priv->dbus_ap_proxy) { + g_signal_handlers_disconnect_by_func (priv->dbus_ap_proxy, + ap_adhoc_properties_changed, self); + g_clear_object (&priv->dbus_ap_proxy); + } + + if (interface) { + priv->dbus_ap_proxy = G_DBUS_PROXY (interface); + g_signal_connect (priv->dbus_ap_proxy, "g-properties-changed", + G_CALLBACK (ap_adhoc_properties_changed), self); + + if (priv->act_mode_switch) + act_check_interface (self); + else + reset_mode (self, NULL, NULL, NULL); + } + + interface = new_powered ? g_dbus_object_get_interface (priv->dbus_obj, NM_IWD_ADHOC_INTERFACE) : NULL; + + if (priv->dbus_adhoc_proxy) { + g_signal_handlers_disconnect_by_func (priv->dbus_adhoc_proxy, + ap_adhoc_properties_changed, self); + g_clear_object (&priv->dbus_adhoc_proxy); + } + + if (interface) { + priv->dbus_adhoc_proxy = G_DBUS_PROXY (interface); + g_signal_connect (priv->dbus_adhoc_proxy, "g-properties-changed", + G_CALLBACK (ap_adhoc_properties_changed), self); + if (priv->act_mode_switch) + act_check_interface (self); + else + reset_mode (self, NULL, NULL, NULL); + } + + /* We expect one of the three interfaces to always be present when + * device is Powered so if AP and AdHoc are not present we should + * be in station mode. + */ + if (new_powered && !priv->dbus_ap_proxy && !priv->dbus_adhoc_proxy) { interface = g_dbus_object_get_interface (priv->dbus_obj, NM_IWD_STATION_INTERFACE); if (!interface) { /* No Station interface on the device object. Check if the @@ -1858,18 +2333,30 @@ powered_changed (NMDeviceIwd *self, gboolean new_powered) * priv->dbus_station_proxy both point at the Device interface. */ value = g_dbus_proxy_get_cached_property (priv->dbus_device_proxy, "State"); - if (!value) { + if (value) { + g_variant_unref (value); + interface = g_object_ref (priv->dbus_device_proxy); + } else { _LOGE (LOGD_WIFI, "Interface %s not found on obj %s", NM_IWD_STATION_INTERFACE, g_dbus_object_get_object_path (priv->dbus_obj)); - return; + interface = NULL; } - g_variant_unref (value); - interface = g_object_ref (priv->dbus_device_proxy); } + } else + interface = NULL; + + if (priv->dbus_station_proxy) { + g_signal_handlers_disconnect_by_func (priv->dbus_station_proxy, + station_properties_changed, self); + g_clear_object (&priv->dbus_station_proxy); + } + if (interface) { priv->dbus_station_proxy = G_DBUS_PROXY (interface); + g_signal_connect (priv->dbus_station_proxy, "g-properties-changed", + G_CALLBACK (station_properties_changed), self); value = g_dbus_proxy_get_cached_property (priv->dbus_station_proxy, "Scanning"); priv->scanning = get_variant_boolean (value, "Scanning"); @@ -1879,27 +2366,15 @@ powered_changed (NMDeviceIwd *self, gboolean new_powered) state_changed (self, get_variant_state (value)); g_variant_unref (value); - g_signal_connect (priv->dbus_station_proxy, "g-properties-changed", - G_CALLBACK (station_properties_changed), self); - - /* Call Disconnect to make sure IWD's autoconnect is disabled. - * Autoconnect is the default state after device is brought UP. - */ - if (priv->enabled) - send_disconnect (self); + update_aps (self); } else { - if (!priv->dbus_station_proxy) - return; - - g_signal_handlers_disconnect_by_func (priv->dbus_station_proxy, - station_properties_changed, self); - g_clear_object (&priv->dbus_station_proxy); - - priv->can_scan = FALSE; + set_can_scan (self, FALSE); + nm_clear_g_source (&priv->periodic_scan_id); priv->scanning = FALSE; priv->scan_requested = FALSE; priv->can_connect = FALSE; cleanup_association_attempt (self, FALSE); + remove_all_aps (self); } } @@ -1908,19 +2383,10 @@ device_properties_changed (GDBusProxy *proxy, GVariant *changed_properties, GStrv invalidate_properties, gpointer user_data) { NMDeviceIwd *self = user_data; - GVariantIter *iter; - const char *key; - GVariant *value; - - g_variant_get (changed_properties, "a{sv}", &iter); - while (g_variant_iter_next (iter, "{&sv}", &key, &value)) { - if (!strcmp (key, "Powered")) - powered_changed (self, get_variant_boolean (value, "Powered")); + gboolean new_bool; - g_variant_unref (value); - } - - g_variant_iter_free (iter); + if (g_variant_lookup (changed_properties, "Powered", "b", &new_bool)) + powered_changed (self, new_bool); } void @@ -1928,24 +2394,24 @@ nm_device_iwd_set_dbus_object (NMDeviceIwd *self, GDBusObject *object) { NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); GDBusInterface *interface; - GVariant *value; + gs_unref_variant GVariant *value = NULL; + gs_unref_object GDBusProxy *adapter_proxy = NULL; + GVariantIter *iter; + const char *mode; gboolean powered; + NMDeviceWifiCapabilities capabilities; if (!nm_g_object_ref_set ((GObject **) &priv->dbus_obj, (GObject *) object)) return; - if (priv->enabled && priv->dbus_station_proxy) { - nm_device_queue_recheck_available (NM_DEVICE (self), - NM_DEVICE_STATE_REASON_SUPPLICANT_AVAILABLE, - NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED); - } - if (priv->dbus_device_proxy) { g_signal_handlers_disconnect_by_func (priv->dbus_device_proxy, device_properties_changed, self); g_clear_object (&priv->dbus_device_proxy); powered_changed (self, FALSE); + + priv->act_mode_switch = FALSE; } if (!object) @@ -1965,14 +2431,63 @@ nm_device_iwd_set_dbus_object (NMDeviceIwd *self, GDBusObject *object) g_signal_connect (priv->dbus_device_proxy, "g-properties-changed", G_CALLBACK (device_properties_changed), self); + /* Parse list of interface modes supported by adapter (wiphy) */ + + value = g_dbus_proxy_get_cached_property (priv->dbus_device_proxy, "Adapter"); + if (!value || !g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH)) { + nm_log_warn (LOGD_DEVICE | LOGD_WIFI, + "Adapter property not cached or not an object path"); + goto error; + } + + adapter_proxy = nm_iwd_manager_get_dbus_interface (nm_iwd_manager_get (), + g_variant_get_string (value, NULL), + NM_IWD_WIPHY_INTERFACE); + if (!adapter_proxy) { + nm_log_warn (LOGD_DEVICE | LOGD_WIFI, + "Can't get DBus proxy for IWD Adapter for IWD Device"); + goto error; + } + + g_variant_unref (value); + value = g_dbus_proxy_get_cached_property (adapter_proxy, "SupportedModes"); + if (!value || !g_variant_is_of_type (value, G_VARIANT_TYPE_STRING_ARRAY)) { + nm_log_warn (LOGD_DEVICE | LOGD_WIFI, + "SupportedModes property not cached or not a string array"); + goto error; + } + + capabilities = NM_WIFI_DEVICE_CAP_CIPHER_CCMP | NM_WIFI_DEVICE_CAP_RSN; + + g_variant_get (value, "as", &iter); + while (g_variant_iter_next (iter, "&s", &mode)) { + if (nm_streq (mode, "ap")) + capabilities |= NM_WIFI_DEVICE_CAP_AP; + else if (nm_streq (mode, "ad-hoc")) + capabilities |= NM_WIFI_DEVICE_CAP_ADHOC; + } + g_variant_iter_free (iter); + + if (priv->capabilities != capabilities) { + priv->capabilities = capabilities; + _notify (self, PROP_CAPABILITIES); + } + + g_variant_unref (value); value = g_dbus_proxy_get_cached_property (priv->dbus_device_proxy, "Powered"); powered = get_variant_boolean (value, "Powered"); - g_variant_unref (value); if (powered != priv->enabled) set_powered (self, priv->enabled); else if (powered) powered_changed (self, TRUE); + + return; + +error: + g_signal_handlers_disconnect_by_func (priv->dbus_device_proxy, + device_properties_changed, self); + g_clear_object (&priv->dbus_device_proxy); } gboolean @@ -2041,15 +2556,14 @@ nm_device_iwd_init (NMDeviceIwd *self) } NMDevice * -nm_device_iwd_new (const char *iface, NMDeviceWifiCapabilities capabilities) +nm_device_iwd_new (const char *iface) { return g_object_new (NM_TYPE_DEVICE_IWD, NM_DEVICE_IFACE, iface, - NM_DEVICE_TYPE_DESC, "802.11 WiFi", + NM_DEVICE_TYPE_DESC, "802.11 Wi-Fi", NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_WIFI, NM_DEVICE_LINK_TYPE, NM_LINK_TYPE_WIFI, NM_DEVICE_RFKILL_TYPE, RFKILL_TYPE_WLAN, - NM_DEVICE_IWD_CAPABILITIES, (guint) capabilities, NULL); } @@ -2067,6 +2581,8 @@ dispose (GObject *object) g_clear_object (&priv->dbus_device_proxy); g_clear_object (&priv->dbus_station_proxy); + g_clear_object (&priv->dbus_ap_proxy); + g_clear_object (&priv->dbus_adhoc_proxy); g_clear_object (&priv->dbus_obj); remove_all_aps (self); @@ -2084,7 +2600,6 @@ nm_device_iwd_class_init (NMDeviceIwdClass *klass) NMDeviceClass *device_class = NM_DEVICE_CLASS (klass); object_class->get_property = get_property; - object_class->set_property = set_property; object_class->dispose = dispose; dbus_object_class->interface_infos = NM_DBUS_INTERFACE_INFOS (&nm_interface_info_device_wireless); @@ -2108,7 +2623,6 @@ nm_device_iwd_class_init (NMDeviceIwdClass *klass) device_class->get_configured_mtu = get_configured_mtu; device_class->deactivate = deactivate; device_class->deactivate_async = deactivate_async; - device_class->deactivate_async_finish = deactivate_async_finish; device_class->can_reapply_change = can_reapply_change; device_class->state_changed = device_state_changed; @@ -2144,8 +2658,7 @@ nm_device_iwd_class_init (NMDeviceIwdClass *klass) obj_properties[PROP_CAPABILITIES] = g_param_spec_uint (NM_DEVICE_IWD_CAPABILITIES, "", "", 0, G_MAXUINT32, NM_WIFI_DEVICE_CAP_NONE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT_ONLY | + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); obj_properties[PROP_SCANNING] = diff --git a/src/devices/wifi/nm-device-iwd.h b/src/devices/wifi/nm-device-iwd.h index 825123b1..4a2bd31e 100644 --- a/src/devices/wifi/nm-device-iwd.h +++ b/src/devices/wifi/nm-device-iwd.h @@ -47,7 +47,7 @@ typedef struct _NMDeviceIwdClass NMDeviceIwdClass; GType nm_device_iwd_get_type (void); -NMDevice *nm_device_iwd_new (const char *iface, NMDeviceWifiCapabilities capabilities); +NMDevice *nm_device_iwd_new (const char *iface); void nm_device_iwd_set_dbus_object (NMDeviceIwd *device, GDBusObject *object); diff --git a/src/devices/wifi/nm-device-olpc-mesh.c b/src/devices/wifi/nm-device-olpc-mesh.c index 9ae81139..193779a7 100644 --- a/src/devices/wifi/nm-device-olpc-mesh.c +++ b/src/devices/wifi/nm-device-olpc-mesh.c @@ -82,7 +82,19 @@ G_DEFINE_TYPE (NMDeviceOlpcMesh, nm_device_olpc_mesh, NM_TYPE_DEVICE) static gboolean get_autoconnect_allowed (NMDevice *device) { - return FALSE; + NMDeviceOlpcMesh *self = NM_DEVICE_OLPC_MESH (device); + NMDeviceOlpcMeshPrivate *priv = NM_DEVICE_OLPC_MESH_GET_PRIVATE (self); + + /* We shall always have a companion if we're >= DISCONENCTED, and this + * ought not be called until then. */ + g_return_val_if_fail (priv->companion, FALSE); + + /* We must not attempt to autoconnect when the companion is connected or + * connecting, * because we'd tear down its connection. */ + if (nm_device_get_state (priv->companion) > NM_DEVICE_STATE_DISCONNECTED) + return FALSE; + + return TRUE; } #define DEFAULT_SSID "olpc-mesh" @@ -154,7 +166,7 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason) nm_device_get_iface (priv->companion)); } - /* wait with continuing configuration untill the companion device is done scanning */ + /* wait with continuing configuration until the companion device is done scanning */ g_object_get (priv->companion, NM_DEVICE_WIFI_SCANNING, &scanning, NULL); if (scanning) { priv->stage1_waiting = TRUE; @@ -181,16 +193,13 @@ static NMActStageReturn act_stage2_config (NMDevice *device, NMDeviceStateReason *out_failure_reason) { NMDeviceOlpcMesh *self = NM_DEVICE_OLPC_MESH (device); - NMConnection *connection; NMSettingOlpcMesh *s_mesh; guint32 channel; GBytes *ssid; const char *anycast_addr; - connection = nm_device_get_applied_connection (device); - g_return_val_if_fail (connection, NM_ACT_STAGE_RETURN_FAILURE); + s_mesh = nm_device_get_applied_setting (device, NM_TYPE_SETTING_OLPC_MESH); - s_mesh = nm_connection_get_setting_olpc_mesh (connection); g_return_val_if_fail (s_mesh, NM_ACT_STAGE_RETURN_FAILURE); channel = nm_setting_olpc_mesh_get_channel (s_mesh); @@ -265,6 +274,11 @@ companion_state_changed_cb (NMDeviceWifi *companion, NMDeviceOlpcMesh *self = NM_DEVICE_OLPC_MESH (user_data); NMDeviceState self_state = nm_device_get_state (NM_DEVICE (self)); + if ( old_state > NM_DEVICE_STATE_DISCONNECTED + && state <= NM_DEVICE_STATE_DISCONNECTED) { + nm_device_emit_recheck_auto_activate (NM_DEVICE (self)); + } + if ( self_state < NM_DEVICE_STATE_PREPARE || self_state > NM_DEVICE_STATE_ACTIVATED || state < NM_DEVICE_STATE_PREPARE @@ -316,7 +330,7 @@ check_companion (NMDeviceOlpcMesh *self, NMDevice *other) g_assert (priv->companion == NULL); priv->companion = g_object_ref (other); - _LOGI (LOGD_OLPC, "found companion WiFi device %s", + _LOGI (LOGD_OLPC, "found companion Wi-Fi device %s", nm_device_get_iface (other)); g_signal_connect (G_OBJECT (other), NM_DEVICE_STATE_CHANGED, diff --git a/src/devices/wifi/nm-device-wifi.c b/src/devices/wifi/nm-device-wifi.c index 2ce84618..e0be38c3 100644 --- a/src/devices/wifi/nm-device-wifi.c +++ b/src/devices/wifi/nm-device-wifi.c @@ -732,7 +732,7 @@ check_connection_available (NMDevice *device, /* Hidden SSIDs obviously don't always appear in the scan list either. * - * For an explict user-activation-request, a connection is considered + * For an explicit user-activation-request, a connection is considered * available because for hidden Wi-Fi, clients didn't consistently * set the 'hidden' property to indicate hidden SSID networks. If * activating but the network isn't available let the device recheck @@ -838,7 +838,7 @@ complete_connection (NMDevice *device, ssid = nm_wifi_ap_get_ssid (ap); if (ssid == NULL) { - /* The AP must be hidden. Connecting to a WiFi AP requires the SSID + /* The AP must be hidden. Connecting to a Wi-Fi AP requires the SSID * as part of the initial handshake, so check the connection details * for the SSID. The AP object will still be used for encryption * settings and such. @@ -909,18 +909,6 @@ complete_connection (NMDevice *device, g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MAC_ADDRESS); return FALSE; } - } else { - guint8 tmp[ETH_ALEN]; - - /* Lock the connection to this device by default if it uses a - * permanent MAC address (ie not a 'locally administered' one) - */ - nm_utils_hwaddr_aton (perm_hw_addr, tmp, ETH_ALEN); - if (!(tmp[0] & 0x02)) { - g_object_set (G_OBJECT (s_wifi), - NM_SETTING_WIRELESS_MAC_ADDRESS, perm_hw_addr, - NULL); - } } } @@ -981,7 +969,7 @@ can_auto_connect (NMDevice *device, g_return_val_if_fail (s_wifi, FALSE); /* Always allow autoconnect for AP and non-autoconf Ad-Hoc */ - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); + method = nm_utils_get_ip_config_method (connection, AF_INET); mode = nm_setting_wireless_get_mode (s_wifi); if (nm_streq0 (mode, NM_SETTING_WIRELESS_MODE_AP)) return TRUE; @@ -1236,7 +1224,7 @@ scanning_prohibited (NMDeviceWifi *self, gboolean periodic) return FALSE; case NM_DEVICE_STATE_ACTIVATED: /* Prohibit periodic scans when connected; we ask the supplicant to - * background scan for us, unless the connection is locked to a specifc + * background scan for us, unless the connection is locked to a specific * BSSID. */ if (periodic) @@ -1685,20 +1673,37 @@ wifi_secrets_cb (NMActRequest *req, g_return_if_fail (nm_act_request_get_settings_connection (req) == connection); if (error) { - _LOGW (LOGD_WIFI, "%s", error->message); - - if (g_error_matches (error, NM_AGENT_MANAGER_ERROR, - NM_AGENT_MANAGER_ERROR_USER_CANCELED)) { - /* Don't wait for WPS timeout on an explicit cancel. */ - nm_clear_g_source (&priv->wps_timeout_id); - } + _LOGW (LOGD_WIFI, "no secrets: %s", error->message); - if (!priv->wps_timeout_id) { - /* Fail the device only if the WPS period is over too. */ - nm_device_state_changed (device, - NM_DEVICE_STATE_FAILED, - NM_DEVICE_STATE_REASON_NO_SECRETS); - } + /* Even if WPS is still pending, let's abort the activation when the secret + * request returns. + * + * This means, a user can only effectively use WPS when also running a secret + * agent, and pressing the push button while being prompted for the password. + * Note, that in the secret prompt the user can see that WPS is in progress + * (via the NM_SECRET_AGENT_GET_SECRETS_FLAG_WPS_PBC_ACTIVE flag). + * + * Previously, WPS was not cancelled when the secret request returns. + * Note that in common use-cases WPS is enabled in the connection profile + * but it won't succeed (because it's disabled in the AP or because the + * user is not prepared to press the push button). + * That means for example, during boot we would try to autoconnect with WPS. + * At that point, there is no secret-agent running, and WPS is pending for + * full 30 seconds. If in the meantime a secret agent registers (because + * of logging into the DE), the profile is still busy waiting for WPS to time + * out. Only after that delay, autoconnect starts again (note that autoconnect gets + * not blocked in this case, because a secret agent registered in the meantime). + * + * It seems wrong to continue doing WPS if the user is not aware + * that WPS is ongoing. The user is required to perform an action (push button), + * and must be told via the secret prompt. + * If no secret-agent is running, if the user cancels the secret-request, or any + * other error to obtain secrets, the user apparently does not want WPS either. + */ + nm_clear_g_source (&priv->wps_timeout_id); + nm_device_state_changed (device, + NM_DEVICE_STATE_FAILED, + NM_DEVICE_STATE_REASON_NO_SECRETS); } else nm_device_activate_schedule_stage1_device_prepare (device); } @@ -1851,9 +1856,10 @@ need_new_8021x_secrets (NMDeviceWifi *self, NMSettingSecretFlags secret_flags = NM_SETTING_SECRET_FLAG_NONE; NMConnection *connection; - g_assert (setting_name != NULL); + g_return_val_if_fail (setting_name, FALSE); connection = nm_device_get_applied_connection (NM_DEVICE (self)); + g_return_val_if_fail (connection != NULL, FALSE); /* 802.1x stuff only happens in the supplicant's ASSOCIATED state when it's @@ -1905,10 +1911,11 @@ need_new_wpa_psk (NMDeviceWifi *self, NMConnection *connection; const char *key_mgmt = NULL; - g_assert (setting_name != NULL); + g_return_val_if_fail (setting_name, FALSE); connection = nm_device_get_applied_connection (NM_DEVICE (self)); - g_return_val_if_fail (connection != NULL, FALSE); + + g_return_val_if_fail (connection, FALSE); /* A bad PSK will cause the supplicant to disconnect during the 4-way handshake */ if (old_state != NM_SUPPLICANT_INTERFACE_STATE_4WAY_HANDSHAKE) @@ -2040,15 +2047,12 @@ supplicant_iface_state_cb (NMSupplicantInterface *iface, * schedule the next activation stage. */ if (devstate == NM_DEVICE_STATE_CONFIG) { - NMConnection *connection; NMSettingWireless *s_wifi; GBytes *ssid; gs_free char *ssid_str = NULL; - connection = nm_device_get_applied_connection (NM_DEVICE (self)); - g_return_if_fail (connection); + s_wifi = nm_device_get_applied_setting (NM_DEVICE (self), NM_TYPE_SETTING_WIRELESS); - s_wifi = nm_connection_get_setting_wireless (connection); g_return_if_fail (s_wifi); ssid = nm_setting_wireless_get_ssid (s_wifi); @@ -2469,7 +2473,7 @@ wake_on_wlan_enable (NMDeviceWifi *self) NMSettingWirelessWakeOnWLan wowl; NMSettingWireless *s_wireless; - s_wireless = (NMSettingWireless *) nm_device_get_applied_setting (NM_DEVICE (self), NM_TYPE_SETTING_WIRELESS); + s_wireless = nm_device_get_applied_setting (NM_DEVICE (self), NM_TYPE_SETTING_WIRELESS); if (s_wireless) { wowl = nm_setting_wireless_get_wake_on_wlan (s_wireless); if (wowl != NM_SETTING_WIRELESS_WAKE_ON_WLAN_DEFAULT) @@ -2646,7 +2650,8 @@ set_powersave (NMDevice *device) NMSettingWireless *s_wireless; NMSettingWirelessPowersave val; - s_wireless = (NMSettingWireless *) nm_device_get_applied_setting (device, NM_TYPE_SETTING_WIRELESS); + s_wireless = nm_device_get_applied_setting (device, NM_TYPE_SETTING_WIRELESS); + g_return_if_fail (s_wireless); val = nm_setting_wireless_get_powersave (s_wireless); @@ -2796,6 +2801,7 @@ act_stage3_ip4_config_start (NMDevice *device, const char *method = NM_SETTING_IP4_CONFIG_METHOD_AUTO; connection = nm_device_get_applied_connection (device); + g_return_val_if_fail (connection, NM_ACT_STAGE_RETURN_FAILURE); s_ip4 = nm_connection_get_setting_ip4_config (connection); @@ -2819,6 +2825,7 @@ act_stage3_ip6_config_start (NMDevice *device, const char *method = NM_SETTING_IP6_CONFIG_METHOD_AUTO; connection = nm_device_get_applied_connection (device); + g_return_val_if_fail (connection, NM_ACT_STAGE_RETURN_FAILURE); s_ip6 = nm_connection_get_setting_ip6_config (connection); @@ -2883,7 +2890,7 @@ handle_ip_config_timeout (NMDeviceWifi *self, /* If IP configuration times out and it's a static WEP connection, that * usually means the WEP key is wrong. WEP's Open System auth mode has * no provision for figuring out if the WEP key is wrong, so you just have - * to wait for DHCP to fail to figure it out. For all other WiFi security + * to wait for DHCP to fail to figure it out. For all other Wi-Fi security * types (open, WPA, 802.1x, etc) if the secrets/certs were wrong the * connection would have failed before IP configuration. */ @@ -2918,6 +2925,7 @@ act_stage4_ip4_config_timeout (NMDevice *device, NMDeviceStateReason *out_failur NMActStageReturn ret; connection = nm_device_get_applied_connection (device); + g_return_val_if_fail (connection, NM_ACT_STAGE_RETURN_FAILURE); s_ip4 = nm_connection_get_setting_ip4_config (connection); @@ -2939,6 +2947,7 @@ act_stage4_ip6_config_timeout (NMDevice *device, NMDeviceStateReason *out_failur NMActStageReturn ret; connection = nm_device_get_applied_connection (device); + g_return_val_if_fail (connection, NM_ACT_STAGE_RETURN_FAILURE); s_ip6 = nm_connection_get_setting_ip6_config (connection); @@ -3279,7 +3288,7 @@ nm_device_wifi_new (const char *iface, NMDeviceWifiCapabilities capabilities) { return g_object_new (NM_TYPE_DEVICE_WIFI, NM_DEVICE_IFACE, iface, - NM_DEVICE_TYPE_DESC, "802.11 WiFi", + NM_DEVICE_TYPE_DESC, "802.11 Wi-Fi", NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_WIFI, NM_DEVICE_LINK_TYPE, NM_LINK_TYPE_WIFI, NM_DEVICE_RFKILL_TYPE, RFKILL_TYPE_WLAN, diff --git a/src/devices/wifi/nm-iwd-manager.c b/src/devices/wifi/nm-iwd-manager.c index a3da9791..736a1a23 100644 --- a/src/devices/wifi/nm-iwd-manager.c +++ b/src/devices/wifi/nm-iwd-manager.c @@ -48,6 +48,7 @@ typedef struct { typedef struct { NMManager *manager; + NMSettings *settings; GCancellable *cancellable; gboolean running; GDBusObjectManager *object_manager; @@ -363,31 +364,35 @@ set_device_dbus_object (NMIwdManager *self, GDBusProxy *proxy, nm_device_iwd_set_dbus_object (NM_DEVICE_IWD (device), object); } -/* Create an in-memory NMConnection for a WPA2-Enterprise network that - * has been preprovisioned with an IWD config file so that NM autoconnect - * mechanism and the clients know this networks needs no additional EAP - * configuration from the user. Only do this if no existing connection - * SSID and security type match that network yet. +/* Look up an existing NMSettingsConnection for a WPA2-Enterprise network + * that has been preprovisioned with an IWD config file, or create a new + * in-memory connection object so that NM autoconnect mechanism and the + * clients know this networks needs no additional EAP configuration from + * the user. */ static NMSettingsConnection * mirror_8021x_connection (NMIwdManager *self, - const char *name) + const char *name, + gboolean create_new) { - NMSettings *settings = NM_SETTINGS_GET; + NMIwdManagerPrivate *priv = NM_IWD_MANAGER_GET_PRIVATE (self); NMSettingsConnection *const*iter; gs_unref_object NMConnection *connection = NULL; - NMSettingsConnection *settings_connection; + NMSettingsConnection *settings_connection = NULL; char uuid[37]; NMSetting *setting; GError *error = NULL; gs_unref_bytes GBytes *new_ssid = NULL; - for (iter = nm_settings_get_connections (settings, NULL); *iter; iter++) { + for (iter = nm_settings_get_connections (priv->settings, NULL); *iter; iter++) { NMSettingsConnection *sett_conn = *iter; NMConnection *conn = nm_settings_connection_get_connection (sett_conn); NMIwdNetworkSecurity security; gs_free char *ssid_name = NULL; NMSettingWireless *s_wifi; + NMSetting8021x *s_8021x; + gboolean external = FALSE; + guint i; security = nm_wifi_connection_get_iwd_security (conn, NULL); if (security != NM_IWD_NETWORK_SECURITY_8021X) @@ -399,14 +404,30 @@ mirror_8021x_connection (NMIwdManager *self, ssid_name = _nm_utils_ssid_to_utf8 (nm_setting_wireless_get_ssid (s_wifi)); - /* We already have an NMSettingsConnection matching this - * KnownNetwork, whether it's saved or an in-memory connection - * potentially created by ourselves. Nothing to do here. - */ - if (nm_streq (ssid_name, name)) - return NULL; + if (!nm_streq (ssid_name, name)) + continue; + + s_8021x = nm_connection_get_setting_802_1x (conn); + for (i = 0; i < nm_setting_802_1x_get_num_eap_methods (s_8021x); i++) { + if (nm_streq (nm_setting_802_1x_get_eap_method (s_8021x, i), "external")) { + external = TRUE; + break; + } + } + + /* Prefer returning connections for EAP method "external" */ + if (!settings_connection || external) + settings_connection = sett_conn; } + /* If we already have an NMSettingsConnection matching this + * KnownNetwork, whether it's saved or an in-memory connection + * potentially created by ourselves then we have nothing left to + * do here. + */ + if (settings_connection || !create_new) + return settings_connection; + connection = nm_simple_connection_new (); setting = NM_SETTING (g_object_new (NM_TYPE_SETTING_CONNECTION, @@ -446,7 +467,7 @@ mirror_8021x_connection (NMIwdManager *self, if (!nm_connection_normalize (connection, NULL, NULL, NULL)) return NULL; - settings_connection = nm_settings_add_connection (settings, connection, + settings_connection = nm_settings_add_connection (priv->settings, connection, FALSE, &error); if (!settings_connection) { _LOGW ("failed to add a mirror NMConnection for IWD's Known Network '%s': %s", @@ -526,16 +547,19 @@ interface_added (GDBusObjectManager *object_manager, GDBusObject *object, id = known_network_id_new (name, security); data = g_hash_table_lookup (priv->known_networks, id); - if (data) + if (data) { + _LOGW ("DBus error: KnownNetwork already exists ('%s', %s)", + name, type_str); g_free (id); - else { + nm_g_object_ref_set (&data->known_network, proxy); + } else { data = g_slice_new0 (KnownNetworkData); data->known_network = g_object_ref (proxy); g_hash_table_insert (priv->known_networks, id, data); } if (security == NM_IWD_NETWORK_SECURITY_8021X) { - sett_conn = mirror_8021x_connection (self, name); + sett_conn = mirror_8021x_connection (self, name, TRUE); if ( sett_conn && sett_conn != data->mirror_connection) { @@ -593,6 +617,55 @@ interface_removed (GDBusObjectManager *object_manager, GDBusObject *object, } } +static void +connection_removed (NMSettings *settings, + NMSettingsConnection *sett_conn, + gpointer user_data) +{ + NMIwdManager *self = user_data; + NMIwdManagerPrivate *priv = NM_IWD_MANAGER_GET_PRIVATE (self); + NMConnection *conn = nm_settings_connection_get_connection (sett_conn); + NMSettingWireless *s_wireless; + gboolean mapped; + KnownNetworkData *data; + KnownNetworkId id; + + id.security = nm_wifi_connection_get_iwd_security (conn, &mapped); + if (!mapped) + return; + + s_wireless = nm_connection_get_setting_wireless (conn); + id.name = _nm_utils_ssid_to_utf8 (nm_setting_wireless_get_ssid (s_wireless)); + data = g_hash_table_lookup (priv->known_networks, &id); + g_free ((char *) id.name); + if (!data) + return; + + if (id.security == NM_IWD_NETWORK_SECURITY_8021X) { + NMSettingsConnection *new_mirror_conn; + + if (data->mirror_connection != sett_conn) + return; + + g_clear_object (&data->mirror_connection); + + /* Don't call Forget for an 8021x network until there's no + * longer *any* matching NMSettingsConnection (debatable) + */ + new_mirror_conn = mirror_8021x_connection (self, id.name, FALSE); + if (new_mirror_conn) { + data->mirror_connection = g_object_ref (new_mirror_conn); + return; + } + } + + if (!priv->running) + return; + + g_dbus_proxy_call (data->known_network, "Forget", + NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL); +} + static gboolean _om_has_name_owner (GDBusObjectManager *object_manager) { @@ -744,7 +817,7 @@ got_object_manager (GObject *object, GAsyncResult *result, gpointer user_data) &priv->agent_path, &error); if (!priv->agent_id) { - _LOGE ("failed to export the IWD Agent: PSK/8021x WiFi networks may not work: %s", + _LOGE ("failed to export the IWD Agent: PSK/8021x Wi-Fi networks may not work: %s", error->message); g_clear_error (&error); } @@ -824,6 +897,10 @@ nm_iwd_manager_init (NMIwdManager *self) g_signal_connect (priv->manager, NM_MANAGER_DEVICE_ADDED, G_CALLBACK (device_added), self); + priv->settings = g_object_ref (nm_settings_get ()); + g_signal_connect (priv->settings, NM_SETTINGS_SIGNAL_CONNECTION_REMOVED, + G_CALLBACK (connection_removed), self); + priv->cancellable = g_cancellable_new (); priv->known_networks = g_hash_table_new_full ((GHashFunc) known_network_id_hash, @@ -844,6 +921,14 @@ dispose (GObject *object) nm_clear_g_cancellable (&priv->cancellable); + if (priv->settings) { + g_signal_handlers_disconnect_by_data (priv->settings, self); + g_clear_object (&priv->settings); + } + + /* This may trigger mirror connection removals so it happens + * after the g_signal_handlers_disconnect_by_data above. + */ nm_clear_pointer (&priv->known_networks, g_hash_table_destroy); if (priv->manager) { diff --git a/src/devices/wifi/nm-wifi-factory.c b/src/devices/wifi/nm-wifi-factory.c index 6b8e5fb8..4e9d1ecb 100644 --- a/src/devices/wifi/nm-wifi-factory.c +++ b/src/devices/wifi/nm-wifi-factory.c @@ -75,8 +75,6 @@ create_device (NMDeviceFactory *factory, NMConnection *connection, gboolean *out_ignore) { - NMDeviceWifiCapabilities capabilities; - NM80211Mode mode; gs_free char *backend = NULL; g_return_val_if_fail (iface != NULL, NULL); @@ -84,23 +82,6 @@ create_device (NMDeviceFactory *factory, g_return_val_if_fail (g_strcmp0 (iface, plink->name) == 0, NULL); g_return_val_if_fail (NM_IN_SET (plink->type, NM_LINK_TYPE_WIFI, NM_LINK_TYPE_OLPC_MESH), NULL); - if (!nm_platform_wifi_get_capabilities (NM_PLATFORM_GET, - plink->ifindex, - &capabilities)) { - nm_log_warn (LOGD_PLATFORM | LOGD_WIFI, "(%s) failed to initialize Wi-Fi driver for ifindex %d", iface, plink->ifindex); - return NULL; - } - - /* Ignore monitor-mode and other unhandled interface types. - * FIXME: keep TYPE_MONITOR devices in UNAVAILABLE state and manage - * them if/when they change to a handled type. - */ - mode = nm_platform_wifi_get_mode (NM_PLATFORM_GET, plink->ifindex); - if (mode == NM_802_11_MODE_UNKNOWN) { - *out_ignore = TRUE; - return NULL; - } - if (plink->type != NM_LINK_TYPE_WIFI) return nm_device_olpc_mesh_new (iface); @@ -116,11 +97,34 @@ create_device (NMDeviceFactory *factory, iface, NM_PRINT_FMT_QUOTE_STRING (backend), WITH_IWD ? " (iwd support enabled)" : ""); - if (!backend || !strcasecmp (backend, "wpa_supplicant")) + if (!backend || !strcasecmp (backend, "wpa_supplicant")) { + NMDeviceWifiCapabilities capabilities; + NM80211Mode mode; + + if (!nm_platform_wifi_get_capabilities (NM_PLATFORM_GET, + plink->ifindex, + &capabilities)) { + nm_log_warn (LOGD_PLATFORM | LOGD_WIFI, + "(%s) failed to initialize Wi-Fi driver for ifindex %d", + iface, plink->ifindex); + return NULL; + } + + /* Ignore monitor-mode and other unhandled interface types. + * FIXME: keep TYPE_MONITOR devices in UNAVAILABLE state and manage + * them if/when they change to a handled type. + */ + mode = nm_platform_wifi_get_mode (NM_PLATFORM_GET, plink->ifindex); + if (mode == NM_802_11_MODE_UNKNOWN) { + *out_ignore = TRUE; + return NULL; + } + return nm_device_wifi_new (iface, capabilities); + } #if WITH_IWD else if (!strcasecmp (backend, "iwd")) - return nm_device_iwd_new (iface, capabilities); + return nm_device_iwd_new (iface); #endif nm_log_warn (LOGD_PLATFORM | LOGD_WIFI, "(%s) config: unknown or unsupported wifi-backend %s", iface, backend); diff --git a/src/devices/wifi/tests/meson.build b/src/devices/wifi/tests/meson.build index bb8f7c27..ee60f349 100644 --- a/src/devices/wifi/tests/meson.build +++ b/src/devices/wifi/tests/meson.build @@ -3,11 +3,11 @@ test_unit = 'test-general' exe = executable( 'wifi-' + test_unit, [test_unit + '.c'] + common_sources, - dependencies: test_nm_dep + dependencies: test_nm_dep, ) test( 'devices/wifi/' + test_unit, test_script, - args: test_args + [exe.full_path()] + args: test_args + [exe.full_path()], ) diff --git a/src/devices/wwan/libnm-wwan.ver b/src/devices/wwan/libnm-wwan.ver index 70b954c5..ea966afe 100644 --- a/src/devices/wwan/libnm-wwan.ver +++ b/src/devices/wwan/libnm-wwan.ver @@ -6,7 +6,6 @@ global: nm_modem_complete_connection; nm_modem_deactivate; nm_modem_deactivate_async; - nm_modem_deactivate_async_finish; nm_modem_device_state_changed; nm_modem_get_capabilities; nm_modem_get_configured_mtu; diff --git a/src/devices/wwan/meson.build b/src/devices/wwan/meson.build index 5fe6e433..482dc205 100644 --- a/src/devices/wwan/meson.build +++ b/src/devices/wwan/meson.build @@ -1,13 +1,13 @@ sources = files( 'nm-modem-broadband.c', 'nm-modem.c', - 'nm-modem-manager.c' + 'nm-modem-manager.c', ) deps = [ libsystemd_dep, mm_glib_dep, - nm_dep + nm_dep, ] if enable_ofono @@ -25,12 +25,12 @@ libnm_wwan = shared_module( ], link_depends: linker_script, install: true, - install_dir: nm_plugindir + install_dir: nm_plugindir, ) libnm_wwan_dep = declare_dependency( include_directories: include_directories('.'), - link_with: libnm_wwan + link_with: libnm_wwan, ) core_plugins += libnm_wwan @@ -43,7 +43,7 @@ test( sources = files( 'nm-device-modem.c', - 'nm-wwan-factory.c' + 'nm-wwan-factory.c', ) libnm_device_plugin_wwan = shared_module( @@ -55,7 +55,7 @@ libnm_device_plugin_wwan = shared_module( link_depends: linker_script_devices, install: true, install_dir: nm_plugindir, - install_rpath: nm_plugindir + install_rpath: nm_plugindir, ) core_plugins += libnm_device_plugin_wwan @@ -63,7 +63,7 @@ core_plugins += libnm_device_plugin_wwan run_target( 'check-local-devices-wwan', command: [check_exports, libnm_device_plugin_wwan.full_path(), linker_script_devices], - depends: libnm_device_plugin_wwan + depends: libnm_device_plugin_wwan, ) # FIXME: check_so_symbols replacement diff --git a/src/devices/wwan/nm-device-modem.c b/src/devices/wwan/nm-device-modem.c index 4119d598..bd9ee3bb 100644 --- a/src/devices/wwan/nm-device-modem.c +++ b/src/devices/wwan/nm-device-modem.c @@ -61,7 +61,7 @@ struct _NMDeviceModemClass { G_DEFINE_TYPE (NMDeviceModem, nm_device_modem, NM_TYPE_DEVICE) -#define NM_DEVICE_MODEM_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMDeviceModem, NM_IS_DEVICE_MODEM) +#define NM_DEVICE_MODEM_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMDeviceModem, NM_IS_DEVICE_MODEM, NMDevice) /*****************************************************************************/ @@ -125,14 +125,40 @@ modem_prepare_result (NMModem *modem, if (success) nm_device_activate_schedule_stage2_device_config (device); else { - if (nm_device_state_reason_check (reason) == NM_DEVICE_STATE_REASON_SIM_PIN_INCORRECT) { - /* If the connect failed because the SIM PIN was wrong don't allow - * the device to be auto-activated anymore, which would risk locking - * the SIM if the incorrect PIN continues to be used. - */ + /* There are several reasons to block autoconnection at device level: + * + * - Wrong SIM-PIN: The device won't autoconnect because it doesn't make sense + * to retry the connection with the same PIN. This error also makes autoconnection + * blocked at settings level, so not even a modem unplug and replug will allow + * autoconnection again. It is somewhat redundant to block autoconnection at + * both device and setting level really. + * + * - SIM wrong or not inserted: If the modem is reporting a SIM not inserted error, + * we can block autoconnection at device level, so that if the same device is + * unplugged and replugged with a SIM (or if a SIM hotplug event happens in MM, + * recreating the device completely), we can try the autoconnection again. + * + * - Modem initialization failed: For some reason unknown to NM, the modem wasn't + * initialized correctly, which leads to an unusable device. A device unplug and + * replug may solve the issue, so make it a device-level autoconnection blocking + * reason. + */ + switch (nm_device_state_reason_check (reason)) { + case NM_DEVICE_STATE_REASON_GSM_SIM_PIN_REQUIRED: + case NM_DEVICE_STATE_REASON_GSM_SIM_PUK_REQUIRED: + case NM_DEVICE_STATE_REASON_SIM_PIN_INCORRECT: nm_device_autoconnect_blocked_set (device, NM_DEVICE_AUTOCONNECT_BLOCKED_WRONG_PIN); + break; + case NM_DEVICE_STATE_REASON_GSM_SIM_NOT_INSERTED: + case NM_DEVICE_STATE_REASON_GSM_SIM_WRONG: + nm_device_autoconnect_blocked_set (device, NM_DEVICE_AUTOCONNECT_BLOCKED_SIM_MISSING); + break; + case NM_DEVICE_STATE_REASON_MODEM_INIT_FAILED: + nm_device_autoconnect_blocked_set (device, NM_DEVICE_AUTOCONNECT_BLOCKED_INIT_FAILED); + break; + default: + break; } - nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, reason); } } @@ -218,7 +244,7 @@ modem_ip6_config_result (NMModem *modem, } /* Re-enable IPv6 on the interface */ - nm_device_ipv6_sysctl_set (device, "disable_ipv6", "0"); + nm_device_sysctl_ip_conf_set (device, AF_INET6, "disable_ipv6", "0"); if (config) nm_device_set_wwan_ip6_config (device, config); @@ -277,7 +303,7 @@ ip_ifindex_changed_cb (NMModem *modem, GParamSpec *pspec, gpointer user_data) * internally, and leaving it enabled could allow the kernel's IPv6 * RA handling code to run before NM is ready. */ - nm_device_ipv6_sysctl_set (device, "disable_ipv6", "1"); + nm_device_sysctl_ip_conf_set (device, AF_INET6, "disable_ipv6", "1"); } static void @@ -448,7 +474,7 @@ check_connection_available (NMDevice *device, state = nm_modem_get_state (priv->modem); if (state <= NM_MODEM_STATE_INITIALIZING) { nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, - "modem not initalized"); + "modem not initialized"); return FALSE; } @@ -483,44 +509,35 @@ deactivate (NMDevice *device) /*****************************************************************************/ -static gboolean -deactivate_async_finish (NMDevice *self, - GAsyncResult *res, - GError **error) -{ - return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error); -} - static void -modem_deactivate_async_ready (NMModem *modem, - GAsyncResult *res, - GSimpleAsyncResult *simple) +modem_deactivate_async_cb (NMModem *modem, + GError *error, + gpointer user_data) { - GError *error = NULL; + gs_unref_object NMDevice *self = NULL; + NMDeviceDeactivateCallback callback; + gpointer callback_user_data; - if (!nm_modem_deactivate_async_finish (modem, res, &error)) - g_simple_async_result_take_error (simple, error); - g_simple_async_result_complete (simple); - g_object_unref (simple); + nm_utils_user_data_unpack (user_data, &self, &callback, &callback_user_data); + callback (self, error, callback_user_data); } static void deactivate_async (NMDevice *self, GCancellable *cancellable, - GAsyncReadyCallback callback, + NMDeviceDeactivateCallback callback, gpointer user_data) { - GSimpleAsyncResult *simple; + nm_assert (G_IS_CANCELLABLE (cancellable)); + nm_assert (callback); - simple = g_simple_async_result_new (G_OBJECT (self), - callback, - user_data, - deactivate_async); - nm_modem_deactivate_async (NM_DEVICE_MODEM_GET_PRIVATE ((NMDeviceModem *) self)->modem, + nm_modem_deactivate_async (NM_DEVICE_MODEM_GET_PRIVATE (self)->modem, self, cancellable, - (GAsyncReadyCallback) modem_deactivate_async_ready, - simple); + modem_deactivate_async_cb, + nm_utils_user_data_pack (g_object_ref (self), + callback, + user_data)); } /*****************************************************************************/ @@ -805,7 +822,6 @@ nm_device_modem_class_init (NMDeviceModemClass *klass) device_class->check_connection_available = check_connection_available; device_class->complete_connection = complete_connection; device_class->deactivate_async = deactivate_async; - device_class->deactivate_async_finish = deactivate_async_finish; device_class->deactivate = deactivate; device_class->act_stage1_prepare = act_stage1_prepare; device_class->act_stage2_config = act_stage2_config; diff --git a/src/devices/wwan/nm-modem-broadband.c b/src/devices/wwan/nm-modem-broadband.c index 04cb8599..82e9e2f1 100644 --- a/src/devices/wwan/nm-modem-broadband.c +++ b/src/devices/wwan/nm-modem-broadband.c @@ -279,11 +279,6 @@ create_gsm_connect_properties (NMConnection *connection) setting = nm_connection_get_setting_gsm (connection); properties = mm_simple_connect_properties_new (); - /* TODO: not needed */ - str = nm_setting_gsm_get_number (setting); - if (str) - mm_simple_connect_properties_set_number (properties, str); - /* Blank APN ("") means the default subscription APN */ str = nm_setting_gsm_get_apn (setting); mm_simple_connect_properties_set_apn (properties, str ?: ""); @@ -693,10 +688,6 @@ complete_connection (NMModem *_self, return FALSE; } - /* TODO: This is not needed */ - if (!nm_setting_gsm_get_number (s_gsm)) - g_object_set (G_OBJECT (s_gsm), NM_SETTING_GSM_NUMBER, "*99#", NULL); - nm_utils_complete_generic (NM_PLATFORM_GET, connection, NM_SETTING_GSM_SETTING_NAME, @@ -884,7 +875,7 @@ static_stage3_ip4_done (NMModemBroadband *self) /* Fully fail if invalid IP address retrieved */ address_string = mm_bearer_ip_config_get_address (self->_priv.ipv4_config); if ( !address_string - || !nm_utils_parse_inaddr_bin (AF_INET, address_string, &address_network)) { + || !nm_utils_parse_inaddr_bin (AF_INET, address_string, NULL, &address_network)) { error = g_error_new (NM_DEVICE_ERROR, NM_DEVICE_ERROR_INVALID_CONNECTION, "(%s) retrieving IP4 configuration failed: invalid address given %s%s%s", @@ -896,7 +887,7 @@ static_stage3_ip4_done (NMModemBroadband *self) /* Missing gateway not a hard failure */ gw_string = mm_bearer_ip_config_get_gateway (self->_priv.ipv4_config); if ( gw_string - && !nm_utils_parse_inaddr_bin (AF_INET, gw_string, &gw)) { + && !nm_utils_parse_inaddr_bin (AF_INET, gw_string, NULL, &gw)) { error = g_error_new (NM_DEVICE_ERROR, NM_DEVICE_ERROR_INVALID_CONNECTION, "(%s) retrieving IP4 configuration failed: invalid gateway address \"%s\"", @@ -937,7 +928,7 @@ static_stage3_ip4_done (NMModemBroadband *self) /* DNS servers */ dns = mm_bearer_ip_config_get_dns (self->_priv.ipv4_config); for (i = 0; dns && dns[i]; i++) { - if ( nm_utils_parse_inaddr_bin (AF_INET, dns[i], &address_network) + if ( nm_utils_parse_inaddr_bin (AF_INET, dns[i], NULL, &address_network) && address_network > 0) { nm_ip4_config_add_nameserver (config, address_network); _LOGI (" DNS %s", dns[i]); @@ -1098,100 +1089,101 @@ stage3_ip6_config_request (NMModem *modem, NMDeviceStateReason *out_failure_reas typedef struct { NMModemBroadband *self; - GSimpleAsyncResult *result; + _NMModemDisconnectCallback callback; + gpointer callback_user_data; GCancellable *cancellable; gboolean warn; } DisconnectContext; static void -disconnect_context_complete (DisconnectContext *ctx) +disconnect_context_complete (DisconnectContext *ctx, GError *error) { - g_simple_async_result_complete_in_idle (ctx->result); - if (ctx->cancellable) - g_object_unref (ctx->cancellable); - g_object_unref (ctx->result); + if (ctx->callback) + ctx->callback (NM_MODEM (ctx->self), error, ctx->callback_user_data); + nm_g_object_unref (ctx->cancellable); g_object_unref (ctx->self); g_slice_free (DisconnectContext, ctx); } -static gboolean -disconnect_finish (NMModem *self, - GAsyncResult *res, - GError **error) +static void +disconnect_context_complete_on_idle (gpointer user_data, + GCancellable *cancellable) { - return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error); + DisconnectContext *ctx = user_data; + gs_free_error GError *cancelled_error = NULL; + + g_cancellable_set_error_if_cancelled (cancellable, &cancelled_error); + disconnect_context_complete (ctx, cancelled_error); } static void -simple_disconnect_ready (MMModemSimple *modem_iface, +simple_disconnect_ready (GObject *source_object, GAsyncResult *res, - DisconnectContext *ctx) + gpointer user_data) { + MMModemSimple *modem_iface = MM_MODEM_SIMPLE (source_object); + DisconnectContext *ctx = user_data; GError *error = NULL; if (!mm_modem_simple_disconnect_finish (modem_iface, res, &error)) { - if (ctx->warn && !g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN)) { + if ( ctx->warn + && !g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN)) { NMModemBroadband *self = ctx->self; _LOGW ("failed to disconnect modem: %s", error->message); } - g_simple_async_result_take_error (ctx->result, error); } - disconnect_context_complete (ctx); + disconnect_context_complete (ctx, error); } static void disconnect (NMModem *modem, gboolean warn, GCancellable *cancellable, - GAsyncReadyCallback callback, + _NMModemDisconnectCallback callback, gpointer user_data) { NMModemBroadband *self = NM_MODEM_BROADBAND (modem); DisconnectContext *ctx; - GError *error = NULL; connect_context_clear (self); - ctx = g_slice_new (DisconnectContext); - ctx->cancellable = NULL; + + ctx = g_slice_new0 (DisconnectContext); ctx->self = g_object_ref (self); - ctx->result = g_simple_async_result_new (G_OBJECT (self), - callback, - user_data, - disconnect); + ctx->cancellable = nm_g_object_ref (cancellable); + ctx->callback = callback; + ctx->callback_user_data = user_data; + /* Don't bother warning on FAILED since the modem is already gone */ ctx->warn = warn; - /* Already cancelled? */ - if (g_cancellable_set_error_if_cancelled (cancellable, &error)) { - g_simple_async_result_take_error (ctx->result, error); - disconnect_context_complete (ctx); - return; - } - - /* If no simple iface, we're done */ - if (!ctx->self->_priv.simple_iface) { - disconnect_context_complete (ctx); + /* Already cancelled or no simple-iface? We are done. */ + if ( !ctx->self->_priv.simple_iface + || g_cancellable_is_cancelled (cancellable)) { + nm_utils_invoke_on_idle (disconnect_context_complete_on_idle, + ctx, + cancellable); return; } _LOGD ("notifying ModemManager about the modem disconnection"); - ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL; - mm_modem_simple_disconnect (ctx->self->_priv.simple_iface, + mm_modem_simple_disconnect (self->_priv.simple_iface, NULL, /* bearer path; if NULL given ALL get disconnected */ cancellable, - (GAsyncReadyCallback) simple_disconnect_ready, + simple_disconnect_ready, ctx); } /*****************************************************************************/ static void -deactivate_cleanup (NMModem *_self, NMDevice *device) +deactivate_cleanup (NMModem *modem, + NMDevice *device, + gboolean stop_ppp_manager) { - NMModemBroadband *self = NM_MODEM_BROADBAND (_self); + NMModemBroadband *self = NM_MODEM_BROADBAND (modem); /* TODO: cancel SimpleConnect() if any */ @@ -1202,8 +1194,9 @@ deactivate_cleanup (NMModem *_self, NMDevice *device) self->_priv.pin_tries = 0; - /* Chain up parent's */ - NM_MODEM_CLASS (nm_modem_broadband_parent_class)->deactivate_cleanup (_self, device); + NM_MODEM_CLASS (nm_modem_broadband_parent_class)->deactivate_cleanup (modem, + device, + stop_ppp_manager); } /*****************************************************************************/ @@ -1468,7 +1461,6 @@ nm_modem_broadband_class_init (NMModemBroadbandClass *klass) modem_class->static_stage3_ip4_config_start = static_stage3_ip4_config_start; modem_class->stage3_ip6_config_request = stage3_ip6_config_request; modem_class->disconnect = disconnect; - modem_class->disconnect_finish = disconnect_finish; modem_class->deactivate_cleanup = deactivate_cleanup; modem_class->set_mm_enabled = set_mm_enabled; modem_class->get_user_pass = get_user_pass; diff --git a/src/devices/wwan/nm-modem-ofono.c b/src/devices/wwan/nm-modem-ofono.c index ea668590..8efe0253 100644 --- a/src/devices/wwan/nm-modem-ofono.c +++ b/src/devices/wwan/nm-modem-ofono.c @@ -146,30 +146,36 @@ update_modem_state (NMModemOfono *self) /* Disconnect */ typedef struct { NMModemOfono *self; - GSimpleAsyncResult *result; + _NMModemDisconnectCallback callback; + gpointer callback_user_data; GCancellable *cancellable; gboolean warn; } DisconnectContext; static void -disconnect_context_complete (DisconnectContext *ctx) +disconnect_context_complete (DisconnectContext *ctx, GError *error) { - if (ctx->cancellable) - g_object_unref (ctx->cancellable); - if (ctx->result) { - g_simple_async_result_complete_in_idle (ctx->result); - g_object_unref (ctx->result); - } + if (ctx->callback) + ctx->callback (NM_MODEM (ctx->self), error, ctx->callback_user_data); + nm_g_object_unref (ctx->cancellable); g_object_unref (ctx->self); g_slice_free (DisconnectContext, ctx); } -static gboolean -disconnect_finish (NMModem *self, - GAsyncResult *result, - GError **error) +static void +disconnect_context_complete_on_idle (gpointer user_data, + GCancellable *cancellable) { - return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error); + DisconnectContext *ctx = user_data; + gs_free_error GError *error = NULL; + + if (!g_cancellable_set_error_if_cancelled (cancellable, &error)) { + g_set_error_literal (&error, + NM_UTILS_ERROR, + NM_UTILS_ERROR_UNKNOWN, + ("modem is currently not connected")); + } + disconnect_context_complete (ctx, error); } static void @@ -177,16 +183,14 @@ disconnect_done (GObject *source, GAsyncResult *result, gpointer user_data) { - DisconnectContext *ctx = (DisconnectContext*) user_data; + DisconnectContext *ctx = user_data; NMModemOfono *self = ctx->self; gs_free_error GError *error = NULL; gs_unref_variant GVariant *v = NULL; v = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), result, &error); if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { - if (ctx->result) - g_simple_async_result_take_error (ctx->result, g_steal_pointer (&error)); - disconnect_context_complete (ctx); + disconnect_context_complete (ctx, error); return; } @@ -196,21 +200,21 @@ disconnect_done (GObject *source, _LOGD ("modem disconnected"); update_modem_state (self); - disconnect_context_complete (ctx); + disconnect_context_complete (ctx, error); } static void disconnect (NMModem *modem, gboolean warn, GCancellable *cancellable, - GAsyncReadyCallback callback, + _NMModemDisconnectCallback callback, gpointer user_data) { NMModemOfono *self = NM_MODEM_OFONO (modem); NMModemOfonoPrivate *priv = NM_MODEM_OFONO_GET_PRIVATE (self); DisconnectContext *ctx; NMModemState state = nm_modem_get_state (NM_MODEM (self)); - GError *error = NULL; + gs_free_error GError *error = NULL; _LOGD ("warn: %s modem_state: %s", warn ? "TRUE" : "FALSE", @@ -218,37 +222,19 @@ disconnect (NMModem *modem, ctx = g_slice_new0 (DisconnectContext); ctx->self = g_object_ref (self); + ctx->cancellable = nm_g_object_ref (cancellable); ctx->warn = warn; - if (callback) { - ctx->result = g_simple_async_result_new (G_OBJECT (self), - callback, - user_data, - disconnect); - } - - if (state != NM_MODEM_STATE_CONNECTED) { - if (ctx->result) { - g_set_error_literal (&error, - NM_UTILS_ERROR, - NM_UTILS_ERROR_UNKNOWN, - ("modem is currently not connected")); - g_simple_async_result_take_error (ctx->result, error); - } - disconnect_context_complete (ctx); + ctx->callback = callback; + ctx->callback_user_data = user_data; + + if ( state != NM_MODEM_STATE_CONNECTED + || g_cancellable_is_cancelled (cancellable)) { + nm_utils_invoke_on_idle (disconnect_context_complete_on_idle, + ctx, + cancellable); return; } - if (g_cancellable_set_error_if_cancelled (cancellable, &error)) { - if (ctx->result) - g_simple_async_result_take_error (ctx->result, error); - else - g_clear_error (&error); - disconnect_context_complete (ctx); - return; - } - - ctx->cancellable = nm_g_object_ref (cancellable); - nm_modem_set_state (NM_MODEM (self), NM_MODEM_STATE_DISCONNECTING, nm_modem_state_to_string (NM_MODEM_STATE_DISCONNECTING)); @@ -266,7 +252,9 @@ disconnect (NMModem *modem, } static void -deactivate_cleanup (NMModem *modem, NMDevice *device) +deactivate_cleanup (NMModem *modem, + NMDevice *device, + gboolean stop_ppp_manager) { NMModemOfono *self = NM_MODEM_OFONO (modem); NMModemOfonoPrivate *priv = NM_MODEM_OFONO_GET_PRIVATE (self); @@ -275,7 +263,9 @@ deactivate_cleanup (NMModem *modem, NMDevice *device) g_clear_object (&priv->ip4_config); - NM_MODEM_CLASS (nm_modem_ofono_parent_class)->deactivate_cleanup (modem, device); + NM_MODEM_CLASS (nm_modem_ofono_parent_class)->deactivate_cleanup (modem, + device, + stop_ppp_manager); } static gboolean @@ -883,7 +873,7 @@ context_property_changed (GDBusProxy *proxy, goto out; } if ( !s - || !nm_utils_parse_inaddr_bin (AF_INET, s, &address_network)) { + || !nm_utils_parse_inaddr_bin (AF_INET, s, NULL, &address_network)) { _LOGW ("can't convert 'Address' %s to addr", s ?: ""); goto out; } @@ -897,7 +887,7 @@ context_property_changed (GDBusProxy *proxy, goto out; } if ( !s - || !nm_utils_parse_inaddr_bin (AF_INET, s, &address_network)) { + || !nm_utils_parse_inaddr_bin (AF_INET, s, NULL, &address_network)) { _LOGW ("invalid 'Netmask': %s", s ?: ""); goto out; } @@ -911,7 +901,7 @@ context_property_changed (GDBusProxy *proxy, _LOGW ("Settings 'Gateway' missing"); goto out; } - if (!nm_utils_parse_inaddr_bin (AF_INET, s, &gateway_network)) { + if (!nm_utils_parse_inaddr_bin (AF_INET, s, NULL, &gateway_network)) { _LOGW ("invalid 'Gateway': %s", s); goto out; } @@ -938,7 +928,7 @@ context_property_changed (GDBusProxy *proxy, } if (array) { for (iter = array; *iter; iter++) { - if ( nm_utils_parse_inaddr_bin (AF_INET, *iter, &address_network) + if ( nm_utils_parse_inaddr_bin (AF_INET, *iter, NULL, &address_network) && address_network) { _LOGI ("DNS: %s", *iter); nm_ip4_config_add_nameserver (priv->ip4_config, address_network); @@ -958,7 +948,7 @@ context_property_changed (GDBusProxy *proxy, if (g_variant_lookup (v_dict, "MessageProxy", "&s", &s)) { _LOGI ("MessageProxy: %s", s); if ( s - && nm_utils_parse_inaddr_bin (AF_INET, s, &address_network)) { + && nm_utils_parse_inaddr_bin (AF_INET, s, NULL, &address_network)) { nm_modem_get_route_parameters (NM_MODEM (self), &ip4_route_table, &ip4_route_metric, @@ -1319,7 +1309,6 @@ nm_modem_ofono_class_init (NMModemOfonoClass *klass) modem_class->get_capabilities = get_capabilities; modem_class->disconnect = disconnect; - modem_class->disconnect_finish = disconnect_finish; modem_class->deactivate_cleanup = deactivate_cleanup; modem_class->check_connection_compatible_with_modem = check_connection_compatible_with_modem; diff --git a/src/devices/wwan/nm-modem.c b/src/devices/wwan/nm-modem.c index 59b081e9..11ada549 100644 --- a/src/devices/wwan/nm-modem.c +++ b/src/devices/wwan/nm-modem.c @@ -706,13 +706,13 @@ nm_modem_stage3_ip4_config_start (NMModem *self, nm_modem_set_route_parameters_from_device (self, device); - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); + method = nm_utils_get_ip_config_method (connection, AF_INET); /* Only Disabled and Auto methods make sense for WWAN */ - if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) == 0) + if (nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED)) return NM_ACT_STAGE_RETURN_SUCCESS; - if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) != 0) { + if (!nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) { _LOGE ("unhandled WWAN IPv4 method '%s'; will fail", method); NM_SET_OUT (out_failure_reason, NM_DEVICE_STATE_REASON_IP_METHOD_UNSUPPORTED); return NM_ACT_STAGE_RETURN_FAILURE; @@ -823,13 +823,13 @@ nm_modem_stage3_ip6_config_start (NMModem *self, nm_modem_set_route_parameters_from_device (self, device); - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG); + method = nm_utils_get_ip_config_method (connection, AF_INET6); /* Only Ignore and Auto methods make sense for WWAN */ - if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) == 0) + if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE)) return NM_ACT_STAGE_RETURN_IP_DONE; - if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) != 0) { + if (!nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO)) { _LOGW ("unhandled WWAN IPv6 method '%s'; will fail", method); NM_SET_OUT (out_failure_reason, NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE); @@ -1105,7 +1105,9 @@ nm_modem_complete_connection (NMModem *self, /*****************************************************************************/ static void -deactivate_cleanup (NMModem *self, NMDevice *device) +deactivate_cleanup (NMModem *self, + NMDevice *device, + gboolean stop_ppp_manager) { NMModemPrivate *priv; int ifindex; @@ -1126,7 +1128,8 @@ deactivate_cleanup (NMModem *self, NMDevice *device) if (priv->ppp_manager) { g_signal_handlers_disconnect_by_data (priv->ppp_manager, self); - nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL); + if (stop_ppp_manager) + nm_ppp_manager_stop (priv->ppp_manager, NULL, NULL, NULL); g_clear_object (&priv->ppp_manager); } @@ -1157,189 +1160,109 @@ deactivate_cleanup (NMModem *self, NMDevice *device) /*****************************************************************************/ -typedef enum { - DEACTIVATE_CONTEXT_STEP_FIRST, - DEACTIVATE_CONTEXT_STEP_CLEANUP, - DEACTIVATE_CONTEXT_STEP_PPP_MANAGER_STOP, - DEACTIVATE_CONTEXT_STEP_MM_DISCONNECT, - DEACTIVATE_CONTEXT_STEP_LAST -} DeactivateContextStep; - typedef struct { NMModem *self; NMDevice *device; GCancellable *cancellable; - GSimpleAsyncResult *result; - DeactivateContextStep step; - NMPPPManager *ppp_manager; - NMPPPManagerStopHandle *ppp_stop_handle; - gulong ppp_stop_cancellable_id; + NMModemDeactivateCallback callback; + gpointer callback_user_data; } DeactivateContext; static void -deactivate_context_complete (DeactivateContext *ctx) +deactivate_context_complete (DeactivateContext *ctx, GError *error) { - if (ctx->ppp_stop_handle) - nm_ppp_manager_stop_cancel (ctx->ppp_stop_handle); + NMModem *self = ctx->self; - nm_assert (!ctx->ppp_stop_handle); - nm_assert (ctx->ppp_stop_cancellable_id == 0); + _LOGD ("modem deactivation finished %s%s%s", + NM_PRINT_FMT_QUOTED (error, "with failure: ", error->message, "", "successfully")); - if (ctx->ppp_manager) - g_object_unref (ctx->ppp_manager); - if (ctx->cancellable) - g_object_unref (ctx->cancellable); - g_simple_async_result_complete_in_idle (ctx->result); - g_object_unref (ctx->result); + if (ctx->callback) + ctx->callback (ctx->self, error, ctx->callback_user_data); + nm_g_object_unref (ctx->cancellable); g_object_unref (ctx->device); g_object_unref (ctx->self); g_slice_free (DeactivateContext, ctx); } -gboolean -nm_modem_deactivate_async_finish (NMModem *self, - GAsyncResult *res, - GError **error) -{ - return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error); -} - -static void deactivate_step (DeactivateContext *ctx); - static void -disconnect_ready (NMModem *self, - GAsyncResult *res, - DeactivateContext *ctx) +_deactivate_call_disconnect_cb (NMModem *self, + GError *error, + gpointer user_data) { - GError *error = NULL; - - if (!NM_MODEM_GET_CLASS (self)->disconnect_finish (self, res, &error)) { - g_simple_async_result_take_error (ctx->result, error); - deactivate_context_complete (ctx); - return; - } - - /* Go on */ - ctx->step++; - deactivate_step (ctx); + deactivate_context_complete (user_data, error); } static void -ppp_manager_stop_ready (NMPPPManager *ppp_manager, - NMPPPManagerStopHandle *handle, - gboolean was_cancelled, - gpointer user_data) +_deactivate_call_disconnect (DeactivateContext *ctx) { - DeactivateContext *ctx = user_data; - - nm_assert (ctx->ppp_stop_handle == handle); - ctx->ppp_stop_handle = NULL; - - if (ctx->ppp_stop_cancellable_id) { - g_cancellable_disconnect (ctx->cancellable, - nm_steal_int (&ctx->ppp_stop_cancellable_id)); - } - - if (was_cancelled) - return; - - ctx->step++; - deactivate_step (ctx); + NM_MODEM_GET_CLASS (ctx->self)->disconnect (ctx->self, + FALSE, + ctx->cancellable, + _deactivate_call_disconnect_cb, + ctx); } static void -ppp_manager_stop_cancelled (GCancellable *cancellable, - gpointer user_data) +_deactivate_ppp_manager_stop_cb (NMPPPManager *ppp_manager, + NMPPPManagerStopHandle *handle, + gboolean was_cancelled, + gpointer user_data) { DeactivateContext *ctx = user_data; - nm_ppp_manager_stop_cancel (ctx->ppp_stop_handle); -} - -static void -deactivate_step (DeactivateContext *ctx) -{ - NMModem *self = ctx->self; - NMModemPrivate *priv = NM_MODEM_GET_PRIVATE (self); - GError *error = NULL; + g_object_unref (ppp_manager); - /* Check cancellable in each step */ - if (g_cancellable_set_error_if_cancelled (ctx->cancellable, &error)) { - g_simple_async_result_take_error (ctx->result, error); - deactivate_context_complete (ctx); - return; - } - - switch (ctx->step) { - case DEACTIVATE_CONTEXT_STEP_FIRST: - ctx->step++; - /* fall through */ - case DEACTIVATE_CONTEXT_STEP_CLEANUP: - /* Make sure we keep a ref to the PPP manager if there is one */ - if (priv->ppp_manager) - ctx->ppp_manager = g_object_ref (priv->ppp_manager); - /* Run cleanup */ - NM_MODEM_GET_CLASS (self)->deactivate_cleanup (self, ctx->device); - ctx->step++; - /* fall through */ - case DEACTIVATE_CONTEXT_STEP_PPP_MANAGER_STOP: - /* If we have a PPP manager, stop it */ - if (ctx->ppp_manager) { - nm_assert (!ctx->ppp_stop_handle); - if (ctx->cancellable) { - ctx->ppp_stop_cancellable_id = g_cancellable_connect (ctx->cancellable, - G_CALLBACK (ppp_manager_stop_cancelled), - ctx, - NULL); - } - ctx->ppp_stop_handle = nm_ppp_manager_stop (ctx->ppp_manager, - ppp_manager_stop_ready, - ctx); - return; - } - ctx->step++; - /* fall through */ - case DEACTIVATE_CONTEXT_STEP_MM_DISCONNECT: - /* Disconnect asynchronously */ - NM_MODEM_GET_CLASS (self)->disconnect (self, - FALSE, - ctx->cancellable, - (GAsyncReadyCallback) disconnect_ready, - ctx); - return; + if (was_cancelled) { + gs_free_error GError *error = NULL; - case DEACTIVATE_CONTEXT_STEP_LAST: - _LOGD ("modem deactivation finished"); - deactivate_context_complete (ctx); + if (!g_cancellable_set_error_if_cancelled (ctx->cancellable, &error)) + nm_assert_not_reached (); + deactivate_context_complete (ctx, error); return; } - g_assert_not_reached (); + nm_assert (!g_cancellable_is_cancelled (ctx->cancellable)); + _deactivate_call_disconnect (ctx); } void nm_modem_deactivate_async (NMModem *self, NMDevice *device, GCancellable *cancellable, - GAsyncReadyCallback callback, + NMModemDeactivateCallback callback, gpointer user_data) { + NMModemPrivate *priv = NM_MODEM_GET_PRIVATE (self); DeactivateContext *ctx; + NMPPPManager *ppp_manager; + + g_return_if_fail (NM_IS_MODEM (self)); + g_return_if_fail (NM_IS_DEVICE (device)); + g_return_if_fail (G_IS_CANCELLABLE (cancellable)); - ctx = g_slice_new0 (DeactivateContext); + ctx = g_slice_new (DeactivateContext); ctx->self = g_object_ref (self); ctx->device = g_object_ref (device); - ctx->result = g_simple_async_result_new (G_OBJECT (self), - callback, - user_data, - nm_modem_deactivate_async); - /* FIXME(shutdown): we always require a cancellable, otherwise we cannot - * do a coordinated shutdown. */ - ctx->cancellable = nm_g_object_ref (cancellable); + ctx->cancellable = g_object_ref (cancellable); + ctx->callback = callback; + ctx->callback_user_data = user_data; + + ppp_manager = nm_g_object_ref (priv->ppp_manager); + + NM_MODEM_GET_CLASS (self)->deactivate_cleanup (self, ctx->device, FALSE); + + if (ppp_manager) { + /* If we have a PPP manager, stop it. + * + * Pass on the reference in @ppp_manager. */ + nm_ppp_manager_stop (ppp_manager, + ctx->cancellable, + _deactivate_ppp_manager_stop_cb, + ctx); + return; + } - /* Start */ - ctx->step = DEACTIVATE_CONTEXT_STEP_FIRST; - deactivate_step (ctx); + _deactivate_call_disconnect (ctx); } /*****************************************************************************/ @@ -1348,7 +1271,7 @@ void nm_modem_deactivate (NMModem *self, NMDevice *device) { /* First cleanup */ - NM_MODEM_GET_CLASS (self)->deactivate_cleanup (self, device); + NM_MODEM_GET_CLASS (self)->deactivate_cleanup (self, device, TRUE); /* Then disconnect without waiting */ NM_MODEM_GET_CLASS (self)->disconnect (self, FALSE, NULL, NULL, NULL); } @@ -1387,7 +1310,7 @@ nm_modem_device_state_changed (NMModem *self, if (new_state == NM_DEVICE_STATE_FAILED || new_state == NM_DEVICE_STATE_DISCONNECTED) warn = FALSE; /* First cleanup */ - NM_MODEM_GET_CLASS (self)->deactivate_cleanup (self, NULL); + NM_MODEM_GET_CLASS (self)->deactivate_cleanup (self, NULL, TRUE); NM_MODEM_GET_CLASS (self)->disconnect (self, warn, NULL, NULL, NULL); } break; diff --git a/src/devices/wwan/nm-modem.h b/src/devices/wwan/nm-modem.h index c73745ce..f7b6bfe9 100644 --- a/src/devices/wwan/nm-modem.h +++ b/src/devices/wwan/nm-modem.h @@ -109,6 +109,10 @@ struct _NMModem { typedef struct _NMModem NMModem; +typedef void (*_NMModemDisconnectCallback) (NMModem *modem, + GError *error, + gpointer user_data); + typedef struct { GObjectClass parent; @@ -149,13 +153,12 @@ typedef struct { void (*disconnect) (NMModem *self, gboolean warn, GCancellable *cancellable, - GAsyncReadyCallback callback, + _NMModemDisconnectCallback callback, gpointer user_data); - gboolean (*disconnect_finish) (NMModem *self, - GAsyncResult *res, - GError **error); - void (*deactivate_cleanup) (NMModem *self, NMDevice *device); + void (*deactivate_cleanup) (NMModem *self, + NMDevice *device, + gboolean stop_ppp_manager); gboolean (*owns_port) (NMModem *self, const char *iface); } NMModemClass; @@ -236,14 +239,15 @@ void nm_modem_get_secrets (NMModem *modem, void nm_modem_deactivate (NMModem *modem, NMDevice *device); +typedef void (*NMModemDeactivateCallback) (NMModem *self, + GError *error, + gpointer user_data); + void nm_modem_deactivate_async (NMModem *self, NMDevice *device, GCancellable *cancellable, - GAsyncReadyCallback callback, + NMModemDeactivateCallback callback, gpointer user_data); -gboolean nm_modem_deactivate_async_finish (NMModem *self, - GAsyncResult *res, - GError **error); void nm_modem_device_state_changed (NMModem *modem, NMDeviceState new_state, |