67
68
HostnameThread *lookup;
70
gint reset_retries_id; /* idle handler for resetting the retries count */
69
72
char *orig_hostname; /* hostname at NM start time */
70
73
char *cur_hostname; /* hostname we want to assign */
73
76
#define RETRIES_TAG "autoconnect-retries"
74
77
#define RETRIES_DEFAULT 4
78
#define RESET_RETRIES_TIMESTAMP_TAG "reset-retries-timestamp-tag"
79
#define RESET_RETRIES_TIMER 300
80
#define FAILURE_REASON_TAG "failure-reason"
77
83
get_best_ip4_device (NMManager *manager, NMActRequest **out_req)
316
322
/* Grab a hostname out of the device's DHCP4 config */
317
323
dhcp4_config = nm_device_get_dhcp4_config (best4);
318
324
if (dhcp4_config) {
319
p = dhcp_hostname = nm_dhcp4_config_get_option (dhcp4_config, "host_name");
325
p = dhcp_hostname = nm_dhcp4_config_get_option (dhcp4_config, "new_host_name");
320
326
if (dhcp_hostname && strlen (dhcp_hostname)) {
321
327
/* Sanity check; strip leading spaces */
323
329
if (!isblank (*p++)) {
324
_set_hostname (policy, TRUE, dhcp_hostname, "from DHCPv4");
330
_set_hostname (policy, TRUE, p-1, "from DHCPv4");
328
334
nm_log_warn (LOGD_DNS, "DHCPv4-provided hostname '%s' looks invalid; ignoring it",
332
338
} else if (best6) {
333
339
NMDHCP6Config *dhcp6_config;
335
/* Grab a hostname out of the device's DHCP4 config */
341
/* Grab a hostname out of the device's DHCP6 config */
336
342
dhcp6_config = nm_device_get_dhcp6_config (best6);
337
343
if (dhcp6_config) {
338
p = dhcp_hostname = nm_dhcp6_config_get_option (dhcp6_config, "host_name");
344
p = dhcp_hostname = nm_dhcp6_config_get_option (dhcp6_config, "new_host_name");
339
345
if (dhcp_hostname && strlen (dhcp_hostname)) {
340
346
/* Sanity check; strip leading spaces */
342
348
if (!isblank (*p++)) {
343
_set_hostname (policy, TRUE, dhcp_hostname, "from DHCPv6");
349
_set_hostname (policy, TRUE, p-1, "from DHCPv6");
347
353
nm_log_warn (LOGD_DNS, "DHCPv6-provided hostname '%s' looks invalid; ignoring it",
723
729
/* Remove connections that shouldn't be auto-activated */
725
NMConnection *candidate = NM_CONNECTION (iter->data);
731
NMSettingsConnection *candidate = NM_SETTINGS_CONNECTION (iter->data);
732
gboolean remove_it = FALSE;
733
const char *permission;
727
735
/* Grab next item before we possibly delete the current item */
728
736
iter = g_slist_next (iter);
730
738
/* Ignore connections that were tried too many times or are not visible
731
* to any logged-in users.
739
* to any logged-in users. Also ignore shared wifi connections for
740
* which no user has the shared wifi permission.
733
if ( get_connection_auto_retries (candidate) == 0
734
|| nm_settings_connection_is_visible (NM_SETTINGS_CONNECTION (candidate)) == FALSE)
742
if ( get_connection_auto_retries (NM_CONNECTION (candidate)) == 0
743
|| nm_settings_connection_is_visible (candidate) == FALSE)
746
permission = nm_utils_get_shared_wifi_permission (NM_CONNECTION (candidate));
748
if (nm_settings_connection_check_permission (candidate, permission) == FALSE)
735
754
connections = g_slist_remove (connections, candidate);
856
reset_retries_for_failed_secrets (NMSettings *settings)
858
GSList *connections, *iter;
860
connections = nm_settings_get_connections (settings);
861
for (iter = connections; iter; iter = g_slist_next (iter)) {
862
NMDeviceStateReason reason = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (iter->data), FAILURE_REASON_TAG));
864
if (reason == NM_DEVICE_STATE_REASON_NO_SECRETS) {
865
set_connection_auto_retries (NM_CONNECTION (iter->data), RETRIES_DEFAULT);
866
g_object_set_data (G_OBJECT (iter->data), FAILURE_REASON_TAG, GUINT_TO_POINTER (0));
869
g_slist_free (connections);
837
873
sleeping_changed (NMManager *manager, GParamSpec *pspec, gpointer user_data)
839
875
NMPolicy *policy = user_data;
910
reset_connections_retries (gpointer user_data)
912
NMPolicy *policy = (NMPolicy *) user_data;
913
GSList *connections, *iter;
914
time_t con_stamp, min_stamp, now;
916
policy->reset_retries_id = 0;
918
min_stamp = now = time (NULL);
919
connections = nm_settings_get_connections (policy->settings);
920
for (iter = connections; iter; iter = g_slist_next (iter)) {
921
con_stamp = GPOINTER_TO_SIZE (g_object_get_data (G_OBJECT (iter->data), RESET_RETRIES_TIMESTAMP_TAG));
924
if (con_stamp + RESET_RETRIES_TIMER <= now) {
925
set_connection_auto_retries (NM_CONNECTION (iter->data), RETRIES_DEFAULT);
926
g_object_set_data (G_OBJECT (iter->data), RESET_RETRIES_TIMESTAMP_TAG, GSIZE_TO_POINTER (0));
929
if (con_stamp < min_stamp)
930
min_stamp = con_stamp;
932
g_slist_free (connections);
934
/* Schedule the handler again if there are some stamps left */
935
if (min_stamp != now)
936
policy->reset_retries_id = g_timeout_add_seconds (RESET_RETRIES_TIMER - (now - min_stamp), reset_connections_retries, policy);
873
940
static NMConnection *
874
941
get_device_connection (NMDevice *device)
892
959
NMPolicy *policy = (NMPolicy *) user_data;
893
960
NMConnection *connection = get_device_connection (device);
963
g_object_set_data (G_OBJECT (connection), FAILURE_REASON_TAG, GUINT_TO_POINTER (0));
895
965
switch (new_state) {
896
966
case NM_DEVICE_STATE_FAILED:
897
967
/* Mark the connection invalid if it failed during activation so that
906
976
* automatically retrying because it's just going to fail anyway.
908
978
set_connection_auto_retries (connection, 0);
980
/* Mark the connection as failed due to missing secrets so that we can reset
981
* RETRIES_TAG and automatically re-try when an secret agent registers.
983
g_object_set_data (G_OBJECT (connection), FAILURE_REASON_TAG, GUINT_TO_POINTER (NM_DEVICE_STATE_REASON_NO_SECRETS));
909
984
} else if (tries > 0) {
910
985
/* Otherwise if it's a random failure, just decrease the number
911
986
* of automatic retries so that the connection gets tried again
914
989
set_connection_auto_retries (connection, tries - 1);
917
if (get_connection_auto_retries (connection) == 0)
992
if (get_connection_auto_retries (connection) == 0) {
918
993
nm_log_info (LOGD_DEVICE, "Marking connection '%s' invalid.", nm_connection_get_id (connection));
994
/* Schedule a handler to reset retries count */
995
g_object_set_data (G_OBJECT (connection), RESET_RETRIES_TIMESTAMP_TAG, GSIZE_TO_POINTER ((gsize) time (NULL)));
996
if (!policy->reset_retries_id)
997
policy->reset_retries_id = g_timeout_add_seconds (RESET_RETRIES_TIMER, reset_connections_retries, policy);
919
999
nm_connection_clear_secrets (connection);
921
1001
schedule_activate_check (policy, device, 3);
938
1018
update_routing_and_dns (policy, FALSE);
940
1020
case NM_DEVICE_STATE_DISCONNECTED:
941
/* Clear INVALID_TAG when carrier on. If cable was unplugged
1021
/* Reset RETRIES_TAG when carrier on. If cable was unplugged
942
1022
* and plugged again, we should try to reconnect */
943
1023
if (reason == NM_DEVICE_STATE_REASON_CARRIER && old_state == NM_DEVICE_STATE_UNAVAILABLE)
944
1024
reset_retries_all (policy->settings, device);
1216
secret_agent_registered (NMSettings *settings,
1217
NMSecretAgent *agent,
1220
/* The registered secret agent may provide some missing secrets. Thus we
1221
* reset retries count here and schedule activation, so that the
1222
* connections failed due to missing secrets may re-try auto-connection.
1224
reset_retries_for_failed_secrets (settings);
1225
schedule_activate_all ((NMPolicy *) user_data);
1136
1229
_connect_manager_signal (NMPolicy *policy, const char *name, gpointer callback)
1200
1293
_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_CONNECTION_REMOVED, connection_removed);
1201
1294
_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_CONNECTION_VISIBILITY_CHANGED,
1202
1295
connection_visibility_changed);
1296
_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_AGENT_REGISTERED, secret_agent_registered);
1204
1298
/* Initialize connections' auto-retries */
1205
1299
reset_retries_all (policy->settings, NULL);
1247
1341
g_slist_free (policy->dev_ids);
1343
if (policy->reset_retries_id)
1344
g_source_remove (policy->reset_retries_id);
1249
1346
g_free (policy->orig_hostname);
1250
1347
g_free (policy->cur_hostname);