1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/* NetworkManager -- Network link manager
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License along
15
* with this program; if not, write to the Free Software Foundation, Inc.,
16
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
* Copyright (C) 2004 - 2011 Red Hat, Inc.
19
* Copyright (C) 2007 - 2008 Novell, Inc.
29
#include "nm-policy.h"
30
#include "NetworkManagerUtils.h"
31
#include "nm-wifi-ap.h"
32
#include "nm-activation-request.h"
33
#include "nm-logging.h"
34
#include "nm-device-interface.h"
35
#include "nm-device.h"
36
#include "nm-device-wifi.h"
37
#include "nm-device-ethernet.h"
38
#include "nm-device-modem.h"
40
#include "nm-device-wimax.h"
42
#include "nm-dbus-manager.h"
43
#include "nm-setting-ip4-config.h"
44
#include "nm-setting-connection.h"
45
#include "nm-system.h"
46
#include "nm-dns-manager.h"
47
#include "nm-vpn-manager.h"
48
#include "nm-policy-hostname.h"
49
#include "nm-manager-auth.h"
53
guint update_state_id;
54
GSList *pending_activation_checks;
59
NMVPNManager *vpn_manager;
60
gulong vpn_activated_id;
61
gulong vpn_deactivated_id;
65
NMDevice *default_device4;
66
NMDevice *default_device6;
68
HostnameThread *lookup;
70
gint reset_retries_id; /* idle handler for resetting the retries count */
72
char *orig_hostname; /* hostname at NM start time */
73
char *cur_hostname; /* hostname we want to assign */
76
#define RETRIES_TAG "autoconnect-retries"
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"
83
get_best_ip4_device (NMManager *manager, NMActRequest **out_req)
85
GSList *devices, *iter;
86
NMDevice *best = NULL;
87
int best_prio = G_MAXINT;
89
g_return_val_if_fail (manager != NULL, NULL);
90
g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);
91
g_return_val_if_fail (out_req != NULL, NULL);
92
g_return_val_if_fail (*out_req == NULL, NULL);
94
devices = nm_manager_get_devices (manager);
95
for (iter = devices; iter; iter = g_slist_next (iter)) {
96
NMDevice *dev = NM_DEVICE (iter->data);
98
NMConnection *connection;
99
NMIP4Config *ip4_config;
100
NMSettingIP4Config *s_ip4;
103
gboolean can_default = FALSE;
104
const char *method = NULL;
106
if (nm_device_get_state (dev) != NM_DEVICE_STATE_ACTIVATED)
109
ip4_config = nm_device_get_ip4_config (dev);
113
req = nm_device_get_act_request (dev);
115
connection = nm_act_request_get_connection (req);
116
g_assert (connection);
118
/* Never set the default route through an IPv4LL-addressed device */
119
s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
121
method = nm_setting_ip4_config_get_method (s_ip4);
123
if (s_ip4 && !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL))
126
/* Make sure at least one of this device's IP addresses has a gateway */
127
for (i = 0; i < nm_ip4_config_get_num_addresses (ip4_config); i++) {
130
addr = nm_ip4_config_get_address (ip4_config, i);
131
if (nm_ip4_address_get_gateway (addr)) {
137
if (!can_default && !NM_IS_DEVICE_MODEM (dev))
140
/* 'never-default' devices can't ever be the default */
141
if ( (s_ip4 && nm_setting_ip4_config_get_never_default (s_ip4))
142
|| nm_ip4_config_get_never_default (ip4_config))
145
prio = nm_device_get_priority (dev);
146
if (prio > 0 && prio < best_prio) {
157
get_best_ip6_device (NMManager *manager, NMActRequest **out_req)
159
GSList *devices, *iter;
160
NMDevice *best = NULL;
161
int best_prio = G_MAXINT;
163
g_return_val_if_fail (manager != NULL, NULL);
164
g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);
165
g_return_val_if_fail (out_req != NULL, NULL);
166
g_return_val_if_fail (*out_req == NULL, NULL);
168
devices = nm_manager_get_devices (manager);
169
for (iter = devices; iter; iter = g_slist_next (iter)) {
170
NMDevice *dev = NM_DEVICE (iter->data);
172
NMConnection *connection;
173
NMIP6Config *ip6_config;
174
NMSettingIP6Config *s_ip6;
177
gboolean can_default = FALSE;
178
const char *method = NULL;
180
if (nm_device_get_state (dev) != NM_DEVICE_STATE_ACTIVATED)
183
ip6_config = nm_device_get_ip6_config (dev);
187
req = nm_device_get_act_request (dev);
189
connection = nm_act_request_get_connection (req);
190
g_assert (connection);
192
/* Never set the default route through an IPv4LL-addressed device */
193
s_ip6 = (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG);
195
method = nm_setting_ip6_config_get_method (s_ip6);
197
if (method && !strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL))
200
/* Make sure at least one of this device's IP addresses has a gateway */
201
for (i = 0; i < nm_ip6_config_get_num_addresses (ip6_config); i++) {
204
addr = nm_ip6_config_get_address (ip6_config, i);
205
if (nm_ip6_address_get_gateway (addr)) {
211
if (!can_default && !NM_IS_DEVICE_MODEM (dev))
214
/* 'never-default' devices can't ever be the default */
215
if (s_ip6 && nm_setting_ip6_config_get_never_default (s_ip6))
218
prio = nm_device_get_priority (dev);
219
if (prio > 0 && prio < best_prio) {
230
_set_hostname (NMPolicy *policy,
231
gboolean change_hostname,
232
const char *new_hostname,
235
if (change_hostname) {
236
NMDnsManager *dns_mgr;
238
g_free (policy->cur_hostname);
239
policy->cur_hostname = g_strdup (new_hostname);
241
dns_mgr = nm_dns_manager_get (NULL);
242
nm_dns_manager_set_hostname (dns_mgr, policy->cur_hostname);
243
g_object_unref (dns_mgr);
246
if (nm_policy_set_system_hostname (policy->cur_hostname, msg))
247
nm_utils_call_dispatcher ("hostname", NULL, NULL, NULL, NULL, NULL);
251
lookup_callback (HostnameThread *thread,
253
const char *hostname,
256
NMPolicy *policy = (NMPolicy *) user_data;
259
/* Update the hostname if the calling lookup thread is the in-progress one */
260
if (!hostname_thread_is_dead (thread) && (thread == policy->lookup)) {
261
policy->lookup = NULL;
263
/* Fall back to localhost.localdomain */
264
msg = g_strdup_printf ("address lookup failed: %d", result);
265
_set_hostname (policy, TRUE, NULL, msg);
268
_set_hostname (policy, TRUE, hostname, "from address lookup");
270
hostname_thread_free (thread);
274
update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6)
276
char *configured_hostname = NULL;
277
NMActRequest *best_req4 = NULL;
278
NMActRequest *best_req6 = NULL;
279
const char *dhcp_hostname, *p;
281
g_return_if_fail (policy != NULL);
283
if (policy->lookup) {
284
hostname_thread_kill (policy->lookup);
285
policy->lookup = NULL;
288
/* Hostname precedence order:
290
* 1) a configured hostname (from settings)
291
* 2) automatic hostname from the default device's config (DHCP, VPN, etc)
292
* 3) the original hostname when NM started
293
* 4) reverse-DNS of the best device's IPv4 address
297
/* Try a persistent hostname first */
298
g_object_get (G_OBJECT (policy->manager), NM_MANAGER_HOSTNAME, &configured_hostname, NULL);
299
if (configured_hostname) {
300
_set_hostname (policy, TRUE, configured_hostname, "from system configuration");
301
g_free (configured_hostname);
305
/* Try automatically determined hostname from the best device's IP config */
307
best4 = get_best_ip4_device (policy->manager, &best_req4);
309
best6 = get_best_ip6_device (policy->manager, &best_req6);
311
if (!best4 && !best6) {
312
/* No best device; fall back to original hostname or if there wasn't
313
* one, 'localhost.localdomain'
315
_set_hostname (policy, TRUE, policy->orig_hostname, "no default device");
320
NMDHCP4Config *dhcp4_config;
322
/* Grab a hostname out of the device's DHCP4 config */
323
dhcp4_config = nm_device_get_dhcp4_config (best4);
325
p = dhcp_hostname = nm_dhcp4_config_get_option (dhcp4_config, "new_host_name");
326
if (dhcp_hostname && strlen (dhcp_hostname)) {
327
/* Sanity check; strip leading spaces */
329
if (!isblank (*p++)) {
330
_set_hostname (policy, TRUE, p-1, "from DHCPv4");
334
nm_log_warn (LOGD_DNS, "DHCPv4-provided hostname '%s' looks invalid; ignoring it",
339
NMDHCP6Config *dhcp6_config;
341
/* Grab a hostname out of the device's DHCP6 config */
342
dhcp6_config = nm_device_get_dhcp6_config (best6);
344
p = dhcp_hostname = nm_dhcp6_config_get_option (dhcp6_config, "new_host_name");
345
if (dhcp_hostname && strlen (dhcp_hostname)) {
346
/* Sanity check; strip leading spaces */
348
if (!isblank (*p++)) {
349
_set_hostname (policy, TRUE, p-1, "from DHCPv6");
353
nm_log_warn (LOGD_DNS, "DHCPv6-provided hostname '%s' looks invalid; ignoring it",
359
/* If no automatically-configured hostname, try using the hostname from
360
* when NM started up.
362
if (policy->orig_hostname) {
363
_set_hostname (policy, TRUE, policy->orig_hostname, "from system startup");
367
/* No configured hostname, no automatically determined hostname, and no
368
* bootup hostname. Start reverse DNS of the current IPv4 or IPv6 address.
371
NMIP4Config *ip4_config;
374
ip4_config = nm_device_get_ip4_config (best4);
376
|| (nm_ip4_config_get_num_nameservers (ip4_config) == 0)
377
|| (nm_ip4_config_get_num_addresses (ip4_config) == 0)) {
378
/* No valid IP4 config (!!); fall back to localhost.localdomain */
379
_set_hostname (policy, TRUE, NULL, "no IPv4 config");
383
addr4 = nm_ip4_config_get_address (ip4_config, 0);
384
g_assert (addr4); /* checked for > 1 address above */
386
/* Start the hostname lookup thread */
387
policy->lookup = hostname4_thread_new (nm_ip4_address_get_address (addr4), lookup_callback, policy);
389
NMIP6Config *ip6_config;
392
ip6_config = nm_device_get_ip6_config (best6);
394
|| (nm_ip6_config_get_num_nameservers (ip6_config) == 0)
395
|| (nm_ip6_config_get_num_addresses (ip6_config) == 0)) {
396
/* No valid IP6 config (!!); fall back to localhost.localdomain */
397
_set_hostname (policy, TRUE, NULL, "no IPv6 config");
401
addr6 = nm_ip6_config_get_address (ip6_config, 0);
402
g_assert (addr6); /* checked for > 1 address above */
404
/* Start the hostname lookup thread */
405
policy->lookup = hostname6_thread_new (nm_ip6_address_get_address (addr6), lookup_callback, policy);
408
if (!policy->lookup) {
409
/* Fall back to 'localhost.localdomain' */
410
_set_hostname (policy, TRUE, NULL, "error starting hostname thread");
415
update_ip4_routing_and_dns (NMPolicy *policy, gboolean force_update)
417
NMDnsIPConfigType dns_type = NM_DNS_IP_CONFIG_TYPE_BEST_DEVICE;
418
NMDevice *best = NULL;
419
NMActRequest *best_req = NULL;
420
NMDnsManager *dns_mgr;
421
GSList *devices = NULL, *iter, *vpns;
422
NMIP4Config *ip4_config = NULL;
424
const char *ip_iface = NULL;
425
NMConnection *connection = NULL;
426
NMSettingConnection *s_con = NULL;
427
const char *connection_id;
429
best = get_best_ip4_device (policy->manager, &best_req);
432
if (!force_update && (best == policy->default_device4))
435
/* If a VPN connection is active, it is preferred */
436
vpns = nm_vpn_manager_get_active_connections (policy->vpn_manager);
437
for (iter = vpns; iter; iter = g_slist_next (iter)) {
438
NMVPNConnection *candidate = NM_VPN_CONNECTION (iter->data);
439
NMConnection *vpn_connection;
440
NMSettingIP4Config *s_ip4;
441
gboolean can_default = TRUE;
442
NMVPNConnectionState vpn_state;
444
/* If it's marked 'never-default', don't make it default */
445
vpn_connection = nm_vpn_connection_get_connection (candidate);
446
g_assert (vpn_connection);
448
/* Check the active IP4 config from the VPN service daemon */
449
ip4_config = nm_vpn_connection_get_ip4_config (candidate);
450
if (ip4_config && nm_ip4_config_get_never_default (ip4_config))
453
/* Check the user's preference from the NMConnection */
454
s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (vpn_connection, NM_TYPE_SETTING_IP4_CONFIG);
455
if (s_ip4 && nm_setting_ip4_config_get_never_default (s_ip4))
458
vpn_state = nm_vpn_connection_get_vpn_state (candidate);
459
if (can_default && (vpn_state == NM_VPN_CONNECTION_STATE_ACTIVATED)) {
460
NMIP4Config *parent_ip4;
463
ip_iface = nm_vpn_connection_get_ip_iface (candidate);
464
connection = nm_vpn_connection_get_connection (candidate);
465
addr = nm_ip4_config_get_address (ip4_config, 0);
467
parent = nm_vpn_connection_get_parent_device (candidate);
468
parent_ip4 = nm_device_get_ip4_config (parent);
470
nm_system_replace_default_ip4_route_vpn (ip_iface,
471
nm_ip4_address_get_gateway (addr),
472
nm_vpn_connection_get_ip4_internal_gateway (candidate),
473
nm_ip4_config_get_mss (ip4_config),
474
nm_device_get_ip_iface (parent),
475
nm_ip4_config_get_mss (parent_ip4));
477
dns_type = NM_DNS_IP_CONFIG_TYPE_VPN;
482
/* The best device gets the default route if a VPN connection didn't */
483
if (!ip_iface || !ip4_config) {
484
connection = nm_act_request_get_connection (best_req);
485
ip_iface = nm_device_get_ip_iface (best);
486
ip4_config = nm_device_get_ip4_config (best);
487
g_assert (ip4_config);
488
addr = nm_ip4_config_get_address (ip4_config, 0);
490
nm_system_replace_default_ip4_route (ip_iface, nm_ip4_address_get_gateway (addr), nm_ip4_config_get_mss (ip4_config));
492
dns_type = NM_DNS_IP_CONFIG_TYPE_BEST_DEVICE;
495
if (!ip_iface || !ip4_config) {
496
nm_log_warn (LOGD_CORE, "couldn't determine IP interface (%p) or IPv4 config (%p)!",
497
ip_iface, ip4_config);
501
/* Update the default active connection. Only mark the new default
502
* active connection after setting default = FALSE on all other connections
503
* first. The order is important, we don't want two connections marked
504
* default at the same time ever.
506
devices = nm_manager_get_devices (policy->manager);
507
for (iter = devices; iter; iter = g_slist_next (iter)) {
508
NMDevice *dev = NM_DEVICE (iter->data);
511
req = nm_device_get_act_request (dev);
512
if (req && (req != best_req))
513
nm_act_request_set_default (req, FALSE);
516
dns_mgr = nm_dns_manager_get (NULL);
517
nm_dns_manager_add_ip4_config (dns_mgr, ip_iface, ip4_config, dns_type);
518
g_object_unref (dns_mgr);
520
/* Now set new default active connection _after_ updating DNS info, so that
521
* if the connection is shared dnsmasq picks up the right stuff.
524
nm_act_request_set_default (best_req, TRUE);
527
s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
529
connection_id = s_con ? nm_setting_connection_get_id (s_con) : NULL;
531
nm_log_info (LOGD_CORE, "Policy set '%s' (%s) as default for IPv4 routing and DNS.", connection_id, ip_iface);
533
nm_log_info (LOGD_CORE, "Policy set (%s) as default for IPv4 routing and DNS.", ip_iface);
537
policy->default_device4 = best;
541
update_ip6_routing_and_dns (NMPolicy *policy, gboolean force_update)
543
NMDnsIPConfigType dns_type = NM_DNS_IP_CONFIG_TYPE_BEST_DEVICE;
544
NMDevice *best = NULL;
545
NMActRequest *best_req = NULL;
546
NMDnsManager *dns_mgr;
547
GSList *devices = NULL, *iter;
551
NMIP6Config *ip6_config = NULL;
553
const char *ip_iface = NULL;
554
NMConnection *connection = NULL;
555
NMSettingConnection *s_con = NULL;
556
const char *connection_id;
558
best = get_best_ip6_device (policy->manager, &best_req);
561
if (!force_update && (best == policy->default_device6))
565
/* If a VPN connection is active, it is preferred */
566
vpns = nm_vpn_manager_get_active_connections (policy->vpn_manager);
567
for (iter = vpns; iter; iter = g_slist_next (iter)) {
568
NMVPNConnection *candidate = NM_VPN_CONNECTION (iter->data);
569
NMConnection *vpn_connection;
570
NMSettingIP6Config *s_ip6;
571
gboolean can_default = TRUE;
572
NMVPNConnectionState vpn_state;
574
/* If it's marked 'never-default', don't make it default */
575
vpn_connection = nm_vpn_connection_get_connection (candidate);
576
g_assert (vpn_connection);
577
s_ip6 = (NMSettingIP6Config *) nm_connection_get_setting (vpn_connection, NM_TYPE_SETTING_IP6_CONFIG);
578
if (s_ip6 && nm_setting_ip6_config_get_never_default (s_ip6))
581
vpn_state = nm_vpn_connection_get_vpn_state (candidate);
582
if (can_default && (vpn_state == NM_VPN_CONNECTION_STATE_ACTIVATED)) {
583
NMIP6Config *parent_ip6;
586
ip_iface = nm_vpn_connection_get_ip_iface (candidate);
587
connection = nm_vpn_connection_get_connection (candidate);
588
ip6_config = nm_vpn_connection_get_ip6_config (candidate);
589
addr = nm_ip6_config_get_address (ip6_config, 0);
591
parent = nm_vpn_connection_get_parent_device (candidate);
592
parent_ip6 = nm_device_get_ip6_config (parent);
594
nm_system_replace_default_ip6_route_vpn (ip_iface,
595
nm_ip6_address_get_gateway (addr),
596
nm_vpn_connection_get_ip4_internal_gateway (candidate),
597
nm_ip6_config_get_mss (ip4_config),
598
nm_device_get_ip_iface (parent),
599
nm_ip6_config_get_mss (parent_ip4));
601
dns_type = NM_DNS_IP_CONFIG_TYPE_VPN;
607
/* The best device gets the default route if a VPN connection didn't */
608
if (!ip_iface || !ip6_config) {
609
connection = nm_act_request_get_connection (best_req);
610
ip_iface = nm_device_get_ip_iface (best);
611
ip6_config = nm_device_get_ip6_config (best);
612
g_assert (ip6_config);
613
addr = nm_ip6_config_get_address (ip6_config, 0);
615
nm_system_replace_default_ip6_route (ip_iface, nm_ip6_address_get_gateway (addr));
617
dns_type = NM_DNS_IP_CONFIG_TYPE_BEST_DEVICE;
620
if (!ip_iface || !ip6_config) {
621
nm_log_warn (LOGD_CORE, "couldn't determine IP interface (%p) or IPv6 config (%p)!",
622
ip_iface, ip6_config);
626
/* Update the default active connection. Only mark the new default
627
* active connection after setting default = FALSE on all other connections
628
* first. The order is important, we don't want two connections marked
629
* default at the same time ever.
631
devices = nm_manager_get_devices (policy->manager);
632
for (iter = devices; iter; iter = g_slist_next (iter)) {
633
NMDevice *dev = NM_DEVICE (iter->data);
636
req = nm_device_get_act_request (dev);
637
if (req && (req != best_req))
638
nm_act_request_set_default6 (req, FALSE);
641
dns_mgr = nm_dns_manager_get (NULL);
642
nm_dns_manager_add_ip6_config (dns_mgr, ip_iface, ip6_config, dns_type);
643
g_object_unref (dns_mgr);
645
/* Now set new default active connection _after_ updating DNS info, so that
646
* if the connection is shared dnsmasq picks up the right stuff.
649
nm_act_request_set_default6 (best_req, TRUE);
652
s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
654
connection_id = s_con ? nm_setting_connection_get_id (s_con) : NULL;
656
nm_log_info (LOGD_CORE, "Policy set '%s' (%s) as default for IPv6 routing and DNS.", connection_id, ip_iface);
658
nm_log_info (LOGD_CORE, "Policy set (%s) as default for IPv6 routing and DNS.", ip_iface);
662
policy->default_device6 = best;
666
update_routing_and_dns (NMPolicy *policy, gboolean force_update)
668
update_ip4_routing_and_dns (policy, force_update);
669
update_ip6_routing_and_dns (policy, force_update);
671
/* Update the system hostname */
672
update_system_hostname (policy, policy->default_device4, policy->default_device6);
676
set_connection_auto_retries (NMConnection *connection, guint retries)
678
/* add +1 so that the tag still exists if the # retries is 0 */
679
g_object_set_data (G_OBJECT (connection), RETRIES_TAG, GUINT_TO_POINTER (retries + 1));
683
get_connection_auto_retries (NMConnection *connection)
685
/* subtract 1 to handle the +1 from set_connection_auto_retries() */
686
return GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (connection), RETRIES_TAG)) - 1;
696
activate_data_free (ActivateData *data)
699
g_source_remove (data->id);
700
g_object_unref (data->device);
701
memset (data, 0, sizeof (*data));
706
auto_activate_device (gpointer user_data)
708
ActivateData *data = (ActivateData *) user_data;
710
NMConnection *best_connection;
711
char *specific_object = NULL;
712
GSList *connections, *iter;
715
policy = data->policy;
718
policy->pending_activation_checks = g_slist_remove (policy->pending_activation_checks, data);
720
// FIXME: if a device is already activating (or activated) with a connection
721
// but another connection now overrides the current one for that device,
722
// deactivate the device and activate the new connection instead of just
723
// bailing if the device is already active
724
if (nm_device_get_act_request (data->device))
727
iter = connections = nm_settings_get_connections (policy->settings);
729
/* Remove connections that shouldn't be auto-activated */
731
NMSettingsConnection *candidate = NM_SETTINGS_CONNECTION (iter->data);
732
gboolean remove_it = FALSE;
733
const char *permission;
735
/* Grab next item before we possibly delete the current item */
736
iter = g_slist_next (iter);
738
/* Ignore connections that were tried too many times or are not visible
739
* to any logged-in users. Also ignore shared wifi connections for
740
* which no user has the shared wifi permission.
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)
754
connections = g_slist_remove (connections, candidate);
757
best_connection = nm_device_get_best_auto_connection (data->device, connections, &specific_object);
758
if (best_connection) {
759
GError *error = NULL;
761
nm_log_info (LOGD_DEVICE, "Auto-activating connection '%s'.",
762
nm_connection_get_id (best_connection));
763
if (!nm_manager_activate_connection (policy->manager,
766
nm_device_get_path (data->device),
769
nm_log_info (LOGD_DEVICE, "Connection '%s' auto-activation failed: (%d) %s",
770
nm_connection_get_id (best_connection), error->code, error->message);
771
g_error_free (error);
775
g_slist_free (connections);
778
activate_data_free (data);
782
static ActivateData *
783
activate_data_new (NMPolicy *policy, NMDevice *device, guint delay_seconds)
787
data = g_malloc0 (sizeof (ActivateData));
788
data->policy = policy;
789
data->device = g_object_ref (device);
790
if (delay_seconds > 0)
791
data->id = g_timeout_add_seconds (delay_seconds, auto_activate_device, data);
793
data->id = g_idle_add (auto_activate_device, data);
797
static ActivateData *
798
find_pending_activation (GSList *list, NMDevice *device)
802
for (iter = list; iter; iter = g_slist_next (iter)) {
803
if (((ActivateData *) iter->data)->device == device)
809
/*****************************************************************************/
812
vpn_connection_activated (NMVPNManager *manager,
813
NMVPNConnection *vpn,
816
update_routing_and_dns ((NMPolicy *) user_data, TRUE);
820
vpn_connection_deactivated (NMVPNManager *manager,
821
NMVPNConnection *vpn,
822
NMVPNConnectionState state,
823
NMVPNConnectionStateReason reason,
826
update_routing_and_dns ((NMPolicy *) user_data, TRUE);
830
global_state_changed (NMManager *manager, NMState state, gpointer user_data)
835
hostname_changed (NMManager *manager, GParamSpec *pspec, gpointer user_data)
837
update_system_hostname ((NMPolicy *) user_data, NULL, NULL);
841
reset_retries_all (NMSettings *settings, NMDevice *device)
843
GSList *connections, *iter;
844
GError *error = NULL;
846
connections = nm_settings_get_connections (settings);
847
for (iter = connections; iter; iter = g_slist_next (iter)) {
848
if (!device || nm_device_interface_check_connection_compatible (NM_DEVICE_INTERFACE (device), iter->data, &error))
849
set_connection_auto_retries (NM_CONNECTION (iter->data), RETRIES_DEFAULT);
850
g_clear_error (&error);
852
g_slist_free (connections);
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);
873
sleeping_changed (NMManager *manager, GParamSpec *pspec, gpointer user_data)
875
NMPolicy *policy = user_data;
876
gboolean sleeping = FALSE, enabled = FALSE;
878
g_object_get (G_OBJECT (manager), NM_MANAGER_SLEEPING, &sleeping, NULL);
879
g_object_get (G_OBJECT (manager), NM_MANAGER_NETWORKING_ENABLED, &enabled, NULL);
881
/* Reset retries on all connections so they'll checked on wakeup */
882
if (sleeping || !enabled)
883
reset_retries_all (policy->settings, NULL);
887
schedule_activate_check (NMPolicy *policy, NMDevice *device, guint delay_seconds)
892
if (nm_manager_get_state (policy->manager) == NM_STATE_ASLEEP)
895
state = nm_device_interface_get_state (NM_DEVICE_INTERFACE (device));
896
if (state < NM_DEVICE_STATE_DISCONNECTED)
899
if (!nm_device_autoconnect_allowed (device))
902
/* Schedule an auto-activation if there isn't one already for this device */
903
if (find_pending_activation (policy->pending_activation_checks, device) == NULL) {
904
data = activate_data_new (policy, device, delay_seconds);
905
policy->pending_activation_checks = g_slist_append (policy->pending_activation_checks, 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);
940
static NMConnection *
941
get_device_connection (NMDevice *device)
945
req = nm_device_get_act_request (device);
949
return nm_act_request_get_connection (req);
953
device_state_changed (NMDevice *device,
954
NMDeviceState new_state,
955
NMDeviceState old_state,
956
NMDeviceStateReason reason,
959
NMPolicy *policy = (NMPolicy *) user_data;
960
NMConnection *connection = get_device_connection (device);
963
g_object_set_data (G_OBJECT (connection), FAILURE_REASON_TAG, GUINT_TO_POINTER (0));
966
case NM_DEVICE_STATE_FAILED:
967
/* Mark the connection invalid if it failed during activation so that
968
* it doesn't get automatically chosen over and over and over again.
970
if (connection && IS_ACTIVATING_STATE (old_state)) {
971
guint32 tries = get_connection_auto_retries (connection);
973
if (reason == NM_DEVICE_STATE_REASON_NO_SECRETS) {
974
/* If the connection couldn't get the secrets it needed (ex because
975
* the user canceled, or no secrets exist), there's no point in
976
* automatically retrying because it's just going to fail anyway.
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));
984
} else if (tries > 0) {
985
/* Otherwise if it's a random failure, just decrease the number
986
* of automatic retries so that the connection gets tried again
987
* if it still has a retry count.
989
set_connection_auto_retries (connection, tries - 1);
992
if (get_connection_auto_retries (connection) == 0) {
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);
999
nm_connection_clear_secrets (connection);
1001
schedule_activate_check (policy, device, 3);
1003
case NM_DEVICE_STATE_ACTIVATED:
1005
/* Reset auto retries back to default since connection was successful */
1006
set_connection_auto_retries (connection, RETRIES_DEFAULT);
1008
/* And clear secrets so they will always be requested from the
1009
* settings service when the next connection is made.
1011
nm_connection_clear_secrets (connection);
1014
update_routing_and_dns (policy, FALSE);
1016
case NM_DEVICE_STATE_UNMANAGED:
1017
case NM_DEVICE_STATE_UNAVAILABLE:
1018
update_routing_and_dns (policy, FALSE);
1020
case NM_DEVICE_STATE_DISCONNECTED:
1021
/* Reset RETRIES_TAG when carrier on. If cable was unplugged
1022
* and plugged again, we should try to reconnect */
1023
if (reason == NM_DEVICE_STATE_REASON_CARRIER && old_state == NM_DEVICE_STATE_UNAVAILABLE)
1024
reset_retries_all (policy->settings, device);
1026
/* Device is now available for auto-activation */
1027
update_routing_and_dns (policy, FALSE);
1028
schedule_activate_check (policy, device, 0);
1036
device_ip_config_changed (NMDevice *device,
1040
update_routing_and_dns ((NMPolicy *) user_data, TRUE);
1044
wireless_networks_changed (NMDeviceWifi *device, NMAccessPoint *ap, gpointer user_data)
1046
schedule_activate_check ((NMPolicy *) user_data, NM_DEVICE (device), 0);
1051
nsps_changed (NMDeviceWimax *device, NMWimaxNsp *nsp, gpointer user_data)
1053
schedule_activate_check ((NMPolicy *) user_data, NM_DEVICE (device), 0);
1063
_connect_device_signal (NMPolicy *policy, NMDevice *device, const char *name, gpointer callback)
1065
DeviceSignalId *data;
1067
data = g_slice_new0 (DeviceSignalId);
1069
data->id = g_signal_connect (device, name, callback, policy);
1070
data->device = device;
1071
policy->dev_ids = g_slist_prepend (policy->dev_ids, data);
1075
device_added (NMManager *manager, NMDevice *device, gpointer user_data)
1077
NMPolicy *policy = (NMPolicy *) user_data;
1079
_connect_device_signal (policy, device, "state-changed", device_state_changed);
1080
_connect_device_signal (policy, device, "notify::" NM_DEVICE_INTERFACE_IP4_CONFIG, device_ip_config_changed);
1081
_connect_device_signal (policy, device, "notify::" NM_DEVICE_INTERFACE_IP6_CONFIG, device_ip_config_changed);
1083
if (NM_IS_DEVICE_WIFI (device)) {
1084
_connect_device_signal (policy, device, "access-point-added", wireless_networks_changed);
1085
_connect_device_signal (policy, device, "access-point-removed", wireless_networks_changed);
1087
} else if (NM_IS_DEVICE_WIMAX (device)) {
1088
_connect_device_signal (policy, device, "nsp-added", nsps_changed);
1089
_connect_device_signal (policy, device, "nsp-removed", nsps_changed);
1095
device_removed (NMManager *manager, NMDevice *device, gpointer user_data)
1097
NMPolicy *policy = (NMPolicy *) user_data;
1101
/* Clear any idle callbacks for this device */
1102
tmp = find_pending_activation (policy->pending_activation_checks, device);
1104
policy->pending_activation_checks = g_slist_remove (policy->pending_activation_checks, tmp);
1105
activate_data_free (tmp);
1108
/* Clear any signal handlers for this device */
1109
iter = policy->dev_ids;
1111
DeviceSignalId *data = iter->data;
1112
GSList *next = g_slist_next (iter);
1114
if (data->device == device) {
1115
g_signal_handler_disconnect (data->device, data->id);
1116
g_slice_free (DeviceSignalId, data);
1117
policy->dev_ids = g_slist_delete_link (policy->dev_ids, iter);
1122
update_routing_and_dns (policy, FALSE);
1126
schedule_activate_all (NMPolicy *policy)
1128
GSList *iter, *devices;
1130
devices = nm_manager_get_devices (policy->manager);
1131
for (iter = devices; iter; iter = g_slist_next (iter))
1132
schedule_activate_check (policy, NM_DEVICE (iter->data), 0);
1136
connection_added (NMSettings *settings,
1137
NMConnection *connection,
1140
set_connection_auto_retries (connection, RETRIES_DEFAULT);
1141
schedule_activate_all ((NMPolicy *) user_data);
1145
connections_loaded (NMSettings *settings, gpointer user_data)
1147
// FIXME: "connections-loaded" signal is emmitted *before* we connect to it
1148
// in nm_policy_new(). So this function is never called. Currently we work around
1149
// that by calling reset_retries_all() in nm_policy_new()
1151
/* Initialize connections' auto-retries */
1152
reset_retries_all (settings, NULL);
1154
schedule_activate_all ((NMPolicy *) user_data);
1158
connection_updated (NMSettings *settings,
1159
NMConnection *connection,
1162
/* Reset auto retries back to default since connection was updated */
1163
set_connection_auto_retries (connection, RETRIES_DEFAULT);
1165
schedule_activate_all ((NMPolicy *) user_data);
1169
_deactivate_if_active (NMManager *manager, NMConnection *connection)
1174
list = nm_manager_get_active_connections_by_connection (manager, connection);
1178
for (i = 0; i < list->len; i++) {
1179
char *path = g_ptr_array_index (list, i);
1180
GError *error = NULL;
1182
if (!nm_manager_deactivate_connection (manager, path, NM_DEVICE_STATE_REASON_CONNECTION_REMOVED, &error)) {
1183
nm_log_warn (LOGD_DEVICE, "Connection '%s' disappeared, but error deactivating it: (%d) %s",
1184
nm_connection_get_id (connection), error->code, error->message);
1185
g_error_free (error);
1189
g_ptr_array_free (list, TRUE);
1193
connection_removed (NMSettings *settings,
1194
NMConnection *connection,
1197
NMPolicy *policy = user_data;
1199
_deactivate_if_active (policy->manager, connection);
1203
connection_visibility_changed (NMSettings *settings,
1204
NMSettingsConnection *connection,
1207
NMPolicy *policy = user_data;
1209
if (nm_settings_connection_is_visible (connection))
1210
schedule_activate_all (policy);
1212
_deactivate_if_active (policy->manager, NM_CONNECTION (connection));
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);
1229
_connect_manager_signal (NMPolicy *policy, const char *name, gpointer callback)
1233
id = g_signal_connect (policy->manager, name, callback, policy);
1234
policy->manager_ids = g_slist_prepend (policy->manager_ids, GUINT_TO_POINTER (id));
1238
_connect_settings_signal (NMPolicy *policy, const char *name, gpointer callback)
1242
id = g_signal_connect (policy->settings, name, callback, policy);
1243
policy->settings_ids = g_slist_prepend (policy->settings_ids, GUINT_TO_POINTER (id));
1247
nm_policy_new (NMManager *manager,
1248
NMVPNManager *vpn_manager,
1249
NMSettings *settings)
1252
static gboolean initialized = FALSE;
1254
char hostname[HOST_NAME_MAX + 2];
1256
g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);
1257
g_return_val_if_fail (initialized == FALSE, NULL);
1259
policy = g_malloc0 (sizeof (NMPolicy));
1260
policy->manager = g_object_ref (manager);
1261
policy->settings = g_object_ref (settings);
1262
policy->update_state_id = 0;
1264
/* Grab hostname on startup and use that if nothing provides one */
1265
memset (hostname, 0, sizeof (hostname));
1266
if (gethostname (&hostname[0], HOST_NAME_MAX) == 0) {
1267
/* only cache it if it's a valid hostname */
1268
if ( strlen (hostname)
1269
&& strcmp (hostname, "localhost")
1270
&& strcmp (hostname, "localhost.localdomain")
1271
&& strcmp (hostname, "(none)"))
1272
policy->orig_hostname = g_strdup (hostname);
1275
policy->vpn_manager = g_object_ref (vpn_manager);
1276
id = g_signal_connect (policy->vpn_manager, "connection-activated",
1277
G_CALLBACK (vpn_connection_activated), policy);
1278
policy->vpn_activated_id = id;
1279
id = g_signal_connect (policy->vpn_manager, "connection-deactivated",
1280
G_CALLBACK (vpn_connection_deactivated), policy);
1281
policy->vpn_deactivated_id = id;
1283
_connect_manager_signal (policy, "state-changed", global_state_changed);
1284
_connect_manager_signal (policy, "notify::" NM_MANAGER_HOSTNAME, hostname_changed);
1285
_connect_manager_signal (policy, "notify::" NM_MANAGER_SLEEPING, sleeping_changed);
1286
_connect_manager_signal (policy, "notify::" NM_MANAGER_NETWORKING_ENABLED, sleeping_changed);
1287
_connect_manager_signal (policy, "device-added", device_added);
1288
_connect_manager_signal (policy, "device-removed", device_removed);
1290
_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_CONNECTIONS_LOADED, connections_loaded);
1291
_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_CONNECTION_ADDED, connection_added);
1292
_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_CONNECTION_UPDATED, connection_updated);
1293
_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_CONNECTION_REMOVED, connection_removed);
1294
_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_CONNECTION_VISIBILITY_CHANGED,
1295
connection_visibility_changed);
1296
_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_AGENT_REGISTERED, secret_agent_registered);
1298
/* Initialize connections' auto-retries */
1299
reset_retries_all (policy->settings, NULL);
1306
nm_policy_destroy (NMPolicy *policy)
1310
g_return_if_fail (policy != NULL);
1312
/* Tell any existing hostname lookup thread to die, it'll get cleaned up
1313
* by the lookup thread callback.
1315
if (policy->lookup) {
1316
hostname_thread_kill (policy->lookup);
1317
policy->lookup = NULL;
1320
g_slist_foreach (policy->pending_activation_checks, (GFunc) activate_data_free, NULL);
1321
g_slist_free (policy->pending_activation_checks);
1323
g_signal_handler_disconnect (policy->vpn_manager, policy->vpn_activated_id);
1324
g_signal_handler_disconnect (policy->vpn_manager, policy->vpn_deactivated_id);
1325
g_object_unref (policy->vpn_manager);
1327
for (iter = policy->manager_ids; iter; iter = g_slist_next (iter))
1328
g_signal_handler_disconnect (policy->manager, GPOINTER_TO_UINT (iter->data));
1329
g_slist_free (policy->manager_ids);
1331
for (iter = policy->settings_ids; iter; iter = g_slist_next (iter))
1332
g_signal_handler_disconnect (policy->settings, GPOINTER_TO_UINT (iter->data));
1333
g_slist_free (policy->settings_ids);
1335
for (iter = policy->dev_ids; iter; iter = g_slist_next (iter)) {
1336
DeviceSignalId *data = iter->data;
1338
g_signal_handler_disconnect (data->device, data->id);
1339
g_slice_free (DeviceSignalId, data);
1341
g_slist_free (policy->dev_ids);
1343
if (policy->reset_retries_id)
1344
g_source_remove (policy->reset_retries_id);
1346
g_free (policy->orig_hostname);
1347
g_free (policy->cur_hostname);
1349
g_object_unref (policy->settings);
1350
g_object_unref (policy->manager);