/
nm-policy.c
3067 lines (2581 loc) · 112 KB
/
nm-policy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2004 - 2013 Red Hat, Inc.
* Copyright (C) 2007 - 2008 Novell, Inc.
*/
#include "src/core/nm-default-daemon.h"
#include "nm-policy.h"
#include <unistd.h>
#include <netdb.h>
#include "libnm-core-intern/nm-core-internal.h"
#include "libnm-platform/nm-platform.h"
#include "libnm-platform/nmp-object.h"
#include "NetworkManagerUtils.h"
#include "devices/nm-device.h"
#include "dns/nm-dns-manager.h"
#include "nm-act-request.h"
#include "nm-auth-utils.h"
#include "nm-config.h"
#include "nm-dhcp-config.h"
#include "nm-dispatcher.h"
#include "nm-firewalld-manager.h"
#include "nm-hostname-manager.h"
#include "nm-keep-alive.h"
#include "nm-l3-config-data.h"
#include "nm-manager.h"
#include "nm-netns.h"
#include "nm-setting-connection.h"
#include "nm-setting-ip4-config.h"
#include "nm-utils.h"
#include "settings/nm-agent-manager.h"
#include "settings/nm-settings-connection.h"
#include "settings/nm-settings.h"
#include "vpn/nm-vpn-manager.h"
/*****************************************************************************/
NM_GOBJECT_PROPERTIES_DEFINE(NMPolicy,
PROP_MANAGER,
PROP_SETTINGS,
PROP_DEFAULT_IP4_AC,
PROP_DEFAULT_IP6_AC,
PROP_ACTIVATING_IP4_AC,
PROP_ACTIVATING_IP6_AC, );
typedef struct {
NMManager *manager;
NMNetns *netns;
NMFirewalldManager *firewalld_manager;
CList policy_auto_activate_lst_head;
NMAgentManager *agent_mgr;
GHashTable *devices;
GHashTable *pending_active_connections;
GSList *pending_secondaries;
NMSettings *settings;
GSource *device_recheck_auto_activate_all_idle_source;
GSource *reset_connections_retries_idle_source;
NMHostnameManager *hostname_manager;
NMActiveConnection *default_ac4, *activating_ac4;
NMActiveConnection *default_ac6, *activating_ac6;
NMDnsManager *dns_manager;
gulong config_changed_id;
NMPolicyHostnameMode hostname_mode;
char *orig_hostname; /* hostname at NM start time */
char *cur_hostname; /* hostname we want to assign */
char *cur_hostname_full; /* similar to @last_hostname, but before shortening */
char *
last_hostname; /* last hostname NM set (to detect if someone else changed it in the meanwhile) */
bool changing_hostname : 1; /* hostname set operation in progress */
bool dhcp_hostname : 1; /* current hostname was set from dhcp */
bool updating_dns : 1;
GArray *ip6_prefix_delegations; /* pool of ip6 prefixes delegated to all devices */
} NMPolicyPrivate;
struct _NMPolicy {
GObject parent;
NMPolicyPrivate _priv;
};
struct _NMPolicyClass {
GObjectClass parent;
};
G_DEFINE_TYPE(NMPolicy, nm_policy, G_TYPE_OBJECT)
#define NM_POLICY_GET_PRIVATE(self) _NM_GET_PRIVATE(self, NMPolicy, NM_IS_POLICY)
static NMPolicy *
_PRIV_TO_SELF(NMPolicyPrivate *priv)
{
NMPolicy *self;
nm_assert(priv);
self = NM_CAST_ALIGN(NMPolicy, (((char *) priv) - G_STRUCT_OFFSET(NMPolicy, _priv)));
nm_assert(NM_IS_POLICY(self));
return self;
}
/*****************************************************************************/
#define _NMLOG_PREFIX_NAME "policy"
#undef _NMLOG_ENABLED
#define _NMLOG_ENABLED(level, domain) (nm_logging_enabled((level), (domain)))
#define _NMLOG(level, domain, ...) \
G_STMT_START \
{ \
nm_log((level), \
(domain), \
NULL, \
NULL, \
"%s" _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \
_NMLOG_PREFIX_NAME ": " _NM_UTILS_MACRO_REST(__VA_ARGS__)); \
} \
G_STMT_END
/*****************************************************************************/
static void update_system_hostname(NMPolicy *self, const char *msg);
static void nm_policy_device_recheck_auto_activate_all_schedule(NMPolicy *self);
static NMDevice *get_default_device(NMPolicy *self, int addr_family);
/*****************************************************************************/
typedef struct {
NMPlatformIP6Address prefix;
NMDevice *device; /* The requesting ("uplink") device */
guint64 next_subnet; /* Cache of the next subnet number to be
* assigned from this prefix */
GHashTable *subnets; /* ifindex -> NMPlatformIP6Address */
} IP6PrefixDelegation;
static void
_clear_ip6_subnet(gpointer key, gpointer value, gpointer user_data)
{
NMPlatformIP6Address *subnet = value;
NMDevice *device = nm_manager_get_device_by_ifindex(NM_MANAGER_GET, GPOINTER_TO_INT(key));
if (device) {
/* We can not remove a subnet we already started announcing.
* Just un-prefer it. */
subnet->preferred = 0;
nm_device_use_ip6_subnet(device, subnet);
}
g_slice_free(NMPlatformIP6Address, subnet);
}
static void
clear_ip6_prefix_delegation(gpointer data)
{
IP6PrefixDelegation *delegation = data;
char sbuf[NM_INET_ADDRSTRLEN];
_LOGD(LOGD_IP6,
"ipv6-pd: undelegating prefix %s/%d",
nm_inet6_ntop(&delegation->prefix.address, sbuf),
delegation->prefix.plen);
g_hash_table_foreach(delegation->subnets, _clear_ip6_subnet, NULL);
g_hash_table_destroy(delegation->subnets);
}
static void
expire_ip6_delegations(NMPolicy *self)
{
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self);
guint32 now = nm_utils_get_monotonic_timestamp_sec();
IP6PrefixDelegation *delegation = NULL;
guint i;
for (i = 0; i < priv->ip6_prefix_delegations->len; i++) {
delegation = &nm_g_array_index(priv->ip6_prefix_delegations, IP6PrefixDelegation, i);
if (delegation->prefix.timestamp + delegation->prefix.lifetime < now)
g_array_remove_index_fast(priv->ip6_prefix_delegations, i);
}
}
/*
* Try to obtain a new subnet for a particular active connection from given
* delegated prefix, possibly reusing the existing subnet.
* Return value of FALSE indicates no more subnets are available from
* this prefix (and other prefix should be used -- and requested if necessary).
*/
static gboolean
ip6_subnet_from_delegation(IP6PrefixDelegation *delegation, NMDevice *device)
{
NMPlatformIP6Address *subnet;
int ifindex = nm_device_get_ifindex(device);
char sbuf[NM_INET_ADDRSTRLEN];
subnet = g_hash_table_lookup(delegation->subnets, GINT_TO_POINTER(ifindex));
if (!subnet) {
/* Check for out-of-prefixes condition. */
if (delegation->next_subnet >= (1 << (64 - delegation->prefix.plen))) {
_LOGD(LOGD_IP6,
"ipv6-pd: no more prefixes in %s/%d",
nm_inet6_ntop(&delegation->prefix.address, sbuf),
delegation->prefix.plen);
return FALSE;
}
/* Allocate a new subnet. */
subnet = g_slice_new0(NMPlatformIP6Address);
g_hash_table_insert(delegation->subnets, GINT_TO_POINTER(ifindex), subnet);
subnet->plen = 64;
subnet->address.s6_addr32[0] =
delegation->prefix.address.s6_addr32[0] | htonl(delegation->next_subnet >> 32);
subnet->address.s6_addr32[1] =
delegation->prefix.address.s6_addr32[1] | htonl(delegation->next_subnet);
/* Out subnet pool management is pretty unsophisticated. We only add
* the subnets and index them by ifindex. That keeps the implementation
* simple and the dead entries make it easy to reuse the same subnet on
* subsequent activations. On the other hand they may waste the subnet
* space. */
delegation->next_subnet++;
}
subnet->timestamp = delegation->prefix.timestamp;
subnet->lifetime = delegation->prefix.lifetime;
subnet->preferred = delegation->prefix.preferred;
_LOGD(LOGD_IP6,
"ipv6-pd: %s allocated from a /%d prefix on %s",
nm_inet6_ntop(&subnet->address, sbuf),
delegation->prefix.plen,
nm_device_get_iface(device));
nm_device_use_ip6_subnet(device, subnet);
return TRUE;
}
/*
* Try to obtain a subnet from each prefix delegated to given requesting
* ("uplink") device and assign it to the downlink device.
* Requests a new prefix if no subnet could be found.
*/
static void
ip6_subnet_from_device(NMPolicy *self, NMDevice *from_device, NMDevice *device)
{
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self);
IP6PrefixDelegation *delegation = NULL;
gboolean got_subnet = FALSE;
guint have_prefixes = 0;
guint i;
expire_ip6_delegations(self);
for (i = 0; i < priv->ip6_prefix_delegations->len; i++) {
delegation = &nm_g_array_index(priv->ip6_prefix_delegations, IP6PrefixDelegation, i);
if (delegation->device != from_device)
continue;
if (ip6_subnet_from_delegation(delegation, device))
got_subnet = TRUE;
have_prefixes++;
}
if (!got_subnet) {
_LOGI(LOGD_IP6,
"ipv6-pd: none of %u prefixes of %s can be shared on %s",
have_prefixes,
nm_device_get_iface(from_device),
nm_device_get_iface(device));
nm_device_request_ip6_prefixes(from_device, have_prefixes + 1);
}
}
static void
ip6_remove_device_prefix_delegations(NMPolicy *self, NMDevice *device)
{
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self);
IP6PrefixDelegation *delegation = NULL;
guint i;
for (i = 0; i < priv->ip6_prefix_delegations->len; i++) {
delegation = &nm_g_array_index(priv->ip6_prefix_delegations, IP6PrefixDelegation, i);
if (delegation->device == device)
g_array_remove_index_fast(priv->ip6_prefix_delegations, i);
}
}
static void
device_ip6_prefix_delegated(NMDevice *device,
const NMPlatformIP6Address *prefix,
gpointer user_data)
{
NMPolicyPrivate *priv = user_data;
NMPolicy *self = _PRIV_TO_SELF(priv);
IP6PrefixDelegation *delegation = NULL;
guint i;
const CList *tmp_list;
NMActiveConnection *ac;
char sbuf[NM_INET_ADDRSTRLEN];
_LOGI(LOGD_IP6,
"ipv6-pd: received a prefix %s/%d from %s",
nm_inet6_ntop(&prefix->address, sbuf),
prefix->plen,
nm_device_get_iface(device));
expire_ip6_delegations(self);
for (i = 0; i < priv->ip6_prefix_delegations->len; i++) {
/* Look for an already known prefix to update. */
delegation = &nm_g_array_index(priv->ip6_prefix_delegations, IP6PrefixDelegation, i);
if (IN6_ARE_ADDR_EQUAL(&delegation->prefix.address, &prefix->address))
break;
}
if (i == priv->ip6_prefix_delegations->len) {
/* Allocate a delegation for new prefix. */
delegation = nm_g_array_append_new(priv->ip6_prefix_delegations, IP6PrefixDelegation);
delegation->subnets = g_hash_table_new(nm_direct_hash, NULL);
delegation->next_subnet = 0;
}
delegation->device = device;
delegation->prefix = *prefix;
/* The newly activated connections are added to the end of the list,
* so traversing it from the end makes it likely for newly
* activated connections that have no subnet assigned to be served
* first. That is a simple yet fair policy, which is good. */
nm_manager_for_each_active_connection_prev (priv->manager, ac, tmp_list) {
NMDevice *to_device;
to_device = nm_active_connection_get_device(ac);
if (nm_device_needs_ip6_subnet(to_device))
ip6_subnet_from_delegation(delegation, to_device);
}
}
static void
device_ip6_subnet_needed(NMDevice *device, gpointer user_data)
{
NMPolicyPrivate *priv = user_data;
NMPolicy *self = _PRIV_TO_SELF(priv);
_LOGD(LOGD_IP6, "ipv6-pd: %s needs a subnet", nm_device_get_iface(device));
if (!priv->default_ac6) {
/* We request the prefixes when the default IPv6 device is set. */
_LOGI(LOGD_IP6,
"ipv6-pd: no device to obtain a subnet to share on %s from",
nm_device_get_iface(device));
return;
}
ip6_subnet_from_device(self, get_default_device(self, AF_INET6), device);
nm_device_copy_ip6_dns_config(device, get_default_device(self, AF_INET6));
}
/*****************************************************************************/
static NMDevice *
get_default_device(NMPolicy *self, int addr_family)
{
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self);
NMActiveConnection *ac;
nm_assert_addr_family(addr_family);
ac = (addr_family == AF_INET) ? priv->default_ac4 : priv->default_ac6;
return ac ? nm_active_connection_get_device(ac) : NULL;
}
static NMActiveConnection *
get_best_active_connection(NMPolicy *self, int addr_family, gboolean fully_activated)
{
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self);
const CList *tmp_lst;
NMDevice *device;
guint32 best_metric = G_MAXUINT32;
gboolean best_is_fully_activated = FALSE;
NMActiveConnection *best_ac, *prev_ac;
nm_assert(NM_IN_SET(addr_family, AF_INET, AF_INET6));
/* we prefer the current AC in case of identical metric.
* Hence, try that one first. */
prev_ac = addr_family == AF_INET ? (fully_activated ? priv->default_ac4 : priv->activating_ac4)
: (fully_activated ? priv->default_ac6 : priv->activating_ac6);
best_ac = NULL;
nm_manager_for_each_device (priv->manager, device, tmp_lst) {
NMDeviceState state;
const NMPObject *r;
NMActiveConnection *ac;
NMConnection *connection;
guint32 metric;
gboolean is_fully_activated;
state = nm_device_get_state(device);
if (state <= NM_DEVICE_STATE_DISCONNECTED || state >= NM_DEVICE_STATE_DEACTIVATING)
continue;
if (nm_device_sys_iface_state_is_external(device))
continue;
r = nm_device_get_best_default_route(device, addr_family);
if (r) {
/* NOTE: the best route might have rt_source NM_IP_CONFIG_SOURCE_VPN,
* which means it was injected by a VPN, not added by device.
*
* In this case, is it really the best device? Why do we even need the best
* device?? */
metric = NMP_OBJECT_CAST_IP_ROUTE(r)->metric;
is_fully_activated = TRUE;
} else if (!fully_activated && (connection = nm_device_get_applied_connection(device))
&& nm_utils_connection_has_default_route(connection, addr_family, NULL)) {
metric = nm_device_get_route_metric(device, addr_family);
is_fully_activated = FALSE;
} else
continue;
ac = (NMActiveConnection *) nm_device_get_act_request(device);
nm_assert(ac);
if (!best_ac || (!best_is_fully_activated && is_fully_activated)
|| (metric < best_metric || (metric == best_metric && ac == prev_ac))) {
best_ac = ac;
best_metric = metric;
best_is_fully_activated = is_fully_activated;
}
}
if (!fully_activated && best_ac && best_is_fully_activated) {
/* There's a best activating AC only if the best device
* among all activating and already-activated devices is a
* still-activating one. */
return NULL;
}
return best_ac;
}
static gboolean
any_devices_active(NMPolicy *self)
{
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self);
const CList *tmp_lst;
NMDevice *device;
nm_manager_for_each_device (priv->manager, device, tmp_lst) {
NMDeviceState state;
state = nm_device_get_state(device);
if (state <= NM_DEVICE_STATE_DISCONNECTED || state >= NM_DEVICE_STATE_DEACTIVATING)
continue;
if (nm_device_sys_iface_state_is_external(device))
continue;
return TRUE;
}
return FALSE;
}
#define FALLBACK_HOSTNAME4 "localhost.localdomain"
static void
settings_set_hostname_cb(const char *hostname, gboolean result, gpointer user_data)
{
NMPolicy *self = NM_POLICY(user_data);
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self);
int ret = 0;
int errsv;
if (!result) {
_LOGT(LOGD_DNS, "set-hostname: hostname set via dbus failed, fallback to \"sethostname\"");
ret = sethostname(hostname, strlen(hostname));
if (ret != 0) {
errsv = errno;
_LOGW(LOGD_DNS,
"set-hostname: couldn't set the system hostname to '%s': (%d) %s",
hostname,
errsv,
nm_strerror_native(errsv));
if (errsv == EPERM)
_LOGW(
LOGD_DNS,
"set-hostname: you should use hostnamed when systemd hardening is in effect!");
}
}
priv->changing_hostname = FALSE;
if (!ret)
nm_dispatcher_call_hostname(NULL, NULL, NULL);
g_object_unref(self);
}
#define HOST_NAME_BUFSIZE (HOST_NAME_MAX + 2)
static char *
_get_hostname(NMPolicy *self)
{
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self);
char *hostname = NULL;
int errsv;
/* If there is an in-progress hostname change, return
* the last hostname set as would be set soon...
*/
if (priv->changing_hostname) {
_LOGT(LOGD_DNS, "get-hostname: \"%s\" (last on set)", priv->last_hostname);
return g_strdup(priv->last_hostname);
}
/* try to get the hostname via dbus... */
if (nm_hostname_manager_get_transient_hostname(priv->hostname_manager, &hostname)) {
_LOGT(LOGD_DNS, "get-hostname: \"%s\" (from dbus)", hostname);
return hostname;
}
/* ...or retrieve it by yourself */
hostname = g_malloc(HOST_NAME_BUFSIZE);
if (gethostname(hostname, HOST_NAME_BUFSIZE - 1) != 0) {
errsv = errno;
_LOGT(LOGD_DNS,
"get-hostname: couldn't get the system hostname: (%d) %s",
errsv,
nm_strerror_native(errsv));
g_free(hostname);
return NULL;
}
/* the name may be truncated... */
hostname[HOST_NAME_BUFSIZE - 1] = '\0';
if (strlen(hostname) >= HOST_NAME_BUFSIZE - 1) {
_LOGT(LOGD_DNS, "get-hostname: system hostname too long: \"%s\"", hostname);
g_free(hostname);
return NULL;
}
_LOGT(LOGD_DNS, "get-hostname: \"%s\"", hostname);
return hostname;
}
static void
_set_hostname(NMPolicy *self, const char *new_hostname, const char *msg)
{
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self);
gs_free char *old_hostname = NULL;
gboolean cur_hostname_full_changed;
const char *name;
/* The incoming hostname *can* be NULL, which will get translated to
* 'localhost.localdomain' or such in the hostname policy code, but we
* keep cur_hostname = NULL in the case because we need to know that
* there was no valid hostname to start with.
*/
if (nm_strdup_reset(&priv->cur_hostname_full, new_hostname)) {
gs_free char *shortened = NULL;
cur_hostname_full_changed = TRUE;
if (priv->cur_hostname_full
&& !nm_utils_shorten_hostname(priv->cur_hostname_full, &shortened)) {
_LOGW(LOGD_DNS,
"set-hostname: hostname '%s' %s is invalid",
priv->cur_hostname_full,
msg);
return;
}
if (shortened) {
_LOGI(LOGD_DNS,
"set-hostname: shortened hostname %s from '%s' to '%s'",
msg,
priv->cur_hostname_full,
shortened);
nm_strdup_reset_take(&priv->cur_hostname, g_steal_pointer(&shortened));
} else
nm_strdup_reset(&priv->cur_hostname, priv->cur_hostname_full);
} else
cur_hostname_full_changed = FALSE;
/* Update the DNS only if the hostname is actually
* going to change.
*/
if (cur_hostname_full_changed) {
/* Notify the DNS manager of the hostname change so that the domain part, if
* present, can be added to the search list. Set the @updating_dns flag
* so that dns_config_changed() doesn't try again to restart DNS lookup.
*/
priv->updating_dns = TRUE;
nm_dns_manager_set_hostname(priv->dns_manager,
priv->cur_hostname_full,
!any_devices_active(self));
priv->updating_dns = FALSE;
}
/* Finally, set kernel hostname */
nm_assert(!priv->cur_hostname || priv->cur_hostname[0]);
name = priv->cur_hostname ?: FALLBACK_HOSTNAME4;
/* Don't set the hostname if it isn't actually changing */
if ((old_hostname = _get_hostname(self)) && (nm_streq(name, old_hostname))) {
_LOGT(LOGD_DNS, "set-hostname: hostname already set to '%s' (%s)", name, msg);
return;
}
/* Keep track of the last set hostname */
nm_strdup_reset(&priv->last_hostname, name);
priv->changing_hostname = TRUE;
_LOGI(LOGD_DNS, "set-hostname: set hostname to '%s' (%s)", name, msg);
/* Ask NMSettings to update the transient hostname using its
* systemd-hostnamed proxy.
*
* FIXME(shutdown): an async request must be cancellable, so we are guaranteed
* to tear down in a reasonable time.*/
nm_hostname_manager_set_transient_hostname(priv->hostname_manager,
name,
settings_set_hostname_cb,
g_object_ref(self));
}
typedef struct {
NMDevice *device;
int priority;
bool from_dhcp : 1;
bool from_dns : 1;
bool IS_IPv4 : 1;
bool is_default : 1;
} DeviceHostnameInfo;
static int
device_hostname_info_compare(gconstpointer a, gconstpointer b)
{
const DeviceHostnameInfo *info1 = a;
const DeviceHostnameInfo *info2 = b;
NM_CMP_FIELD(info1, info2, priority);
NM_CMP_FIELD_UNSAFE(info2, info1, is_default);
NM_CMP_FIELD_UNSAFE(info2, info1, IS_IPv4);
return 0;
}
NM_CON_DEFAULT_NOP("hostname.from-dhcp");
NM_CON_DEFAULT_NOP("hostname.from-dns-lookup");
NM_CON_DEFAULT_NOP("hostname.only-from-default");
static gboolean
device_get_hostname_property_boolean(NMDevice *device, const char *name)
{
NMSettingHostname *s_hostname;
char buf[128];
int value;
NMTernary default_value;
nm_assert(NM_IN_STRSET(name,
NM_SETTING_HOSTNAME_FROM_DHCP,
NM_SETTING_HOSTNAME_FROM_DNS_LOOKUP,
NM_SETTING_HOSTNAME_ONLY_FROM_DEFAULT));
s_hostname = nm_device_get_applied_setting(device, NM_TYPE_SETTING_HOSTNAME);
if (s_hostname) {
g_object_get(s_hostname, name, &value, NULL);
if (NM_IN_SET(value, NM_TERNARY_FALSE, NM_TERNARY_TRUE))
return value;
}
if (nm_streq(name, NM_SETTING_HOSTNAME_ONLY_FROM_DEFAULT))
default_value = NM_TERNARY_FALSE;
else
default_value = NM_TERNARY_TRUE;
return nm_config_data_get_connection_default_int64(NM_CONFIG_GET_DATA,
nm_sprintf_buf(buf, "hostname.%s", name),
device,
NM_TERNARY_FALSE,
NM_TERNARY_TRUE,
default_value);
}
static int
device_get_hostname_priority(NMDevice *device)
{
NMSettingHostname *s_hostname;
int priority;
s_hostname = nm_device_get_applied_setting(device, NM_TYPE_SETTING_HOSTNAME);
if (s_hostname) {
priority = nm_setting_hostname_get_priority(s_hostname);
if (priority != 0)
return priority;
}
return nm_config_data_get_connection_default_int64(NM_CONFIG_GET_DATA,
NM_CON_DEFAULT("hostname.priority"),
device,
G_MININT,
G_MAXINT,
100);
}
static GArray *
build_device_hostname_infos(NMPolicy *self)
{
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self);
const CList *tmp_clist;
NMActiveConnection *ac;
GArray *array = NULL;
nm_manager_for_each_active_connection (priv->manager, ac, tmp_clist) {
DeviceHostnameInfo *info;
NMDevice *device;
gboolean only_from_default;
gboolean is_default;
int IS_IPv4;
device = nm_active_connection_get_device(ac);
if (!device)
continue;
if (nm_device_sys_iface_state_is_external(device))
continue;
only_from_default =
device_get_hostname_property_boolean(device, NM_SETTING_HOSTNAME_ONLY_FROM_DEFAULT);
for (IS_IPv4 = 1; IS_IPv4 >= 0; IS_IPv4--) {
is_default = (ac == (IS_IPv4 ? priv->default_ac4 : priv->default_ac6));
if (only_from_default && !is_default)
continue;
if (!array)
array = g_array_sized_new(FALSE, FALSE, sizeof(DeviceHostnameInfo), 4);
info = nm_g_array_append_new(array, DeviceHostnameInfo);
*info = (DeviceHostnameInfo){
.device = device,
.priority = device_get_hostname_priority(device),
.from_dhcp =
device_get_hostname_property_boolean(device, NM_SETTING_HOSTNAME_FROM_DHCP),
.from_dns =
device_get_hostname_property_boolean(device,
NM_SETTING_HOSTNAME_FROM_DNS_LOOKUP),
.IS_IPv4 = IS_IPv4,
.is_default = is_default,
};
}
}
if (array && array->len > 1) {
const DeviceHostnameInfo *info0;
guint i;
g_array_sort(array, device_hostname_info_compare);
info0 = &nm_g_array_first(array, DeviceHostnameInfo);
if (info0->priority < 0) {
for (i = 1; i < array->len; i++) {
const DeviceHostnameInfo *info = &nm_g_array_index(array, DeviceHostnameInfo, i);
if (info->priority > info0->priority) {
g_array_set_size(array, i);
break;
}
}
}
}
return array;
}
static void
device_dns_lookup_done(NMDevice *device, gpointer user_data)
{
NMPolicy *self = user_data;
g_signal_handlers_disconnect_by_func(device, device_dns_lookup_done, self);
update_system_hostname(self, "lookup finished");
}
static void
device_carrier_changed(NMDevice *device, GParamSpec *pspec, gpointer user_data)
{
NMPolicyPrivate *priv = user_data;
NMPolicy *self = _PRIV_TO_SELF(priv);
gs_free char *msg = NULL;
if (nm_device_has_carrier(device)) {
g_signal_handlers_disconnect_by_func(device, device_carrier_changed, priv);
msg = g_strdup_printf("device '%s' got carrier", nm_device_get_iface(device));
update_system_hostname(self, msg);
}
}
static void
update_system_hostname(NMPolicy *self, const char *msg)
{
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self);
const char *configured_hostname;
gs_free char *temp_hostname = NULL;
const char *dhcp_hostname, *p;
gboolean external_hostname = FALSE;
NMDhcpConfig *dhcp_config;
gs_unref_array GArray *infos = NULL;
DeviceHostnameInfo *info;
guint i;
int addr_family;
g_return_if_fail(self != NULL);
if (priv->hostname_mode == NM_POLICY_HOSTNAME_MODE_NONE) {
_LOGT(LOGD_DNS, "set-hostname: hostname is unmanaged");
return;
}
_LOGT(LOGD_DNS, "set-hostname: updating hostname (%s)", msg);
/* Check if the hostname was set externally to NM, so that in that case
* we can avoid to fallback to the one we got when we started.
* Consider "not specific" hostnames as equal. */
if ((temp_hostname = _get_hostname(self)) && !nm_streq0(temp_hostname, priv->last_hostname)
&& (nm_utils_is_specific_hostname(temp_hostname)
|| nm_utils_is_specific_hostname(priv->last_hostname))) {
external_hostname = TRUE;
_LOGD(LOGD_DNS,
"set-hostname: current hostname was changed outside NetworkManager: '%s'",
temp_hostname);
priv->dhcp_hostname = FALSE;
if (!nm_utils_is_specific_hostname(temp_hostname))
nm_clear_g_free(&temp_hostname);
if (!nm_streq0(temp_hostname, priv->orig_hostname)) {
/* Update original (fallback) hostname */
g_free(priv->orig_hostname);
priv->orig_hostname = g_steal_pointer(&temp_hostname);
_LOGT(LOGD_DNS,
"hostname-original: update to %s%s%s",
NM_PRINT_FMT_QUOTE_STRING(priv->orig_hostname));
}
}
/* Hostname precedence order:
*
* 1) a configured hostname (from settings)
* 2) automatic hostname from DHCP of eligible interfaces
* 3) reverse-DNS lookup of the first address on eligible interfaces
* 4) the last hostname set outside NM
*/
/* Try a persistent hostname first */
configured_hostname = nm_hostname_manager_get_static_hostname(priv->hostname_manager);
if (configured_hostname && nm_utils_is_specific_hostname(configured_hostname)) {
_set_hostname(self, configured_hostname, "from system configuration");
priv->dhcp_hostname = FALSE;
return;
}
infos = build_device_hostname_infos(self);
if (infos && _LOGT_ENABLED(LOGD_DNS)) {
_LOGT(LOGD_DNS, "device hostname info:");
for (i = 0; i < infos->len; i++) {
info = &nm_g_array_index(infos, DeviceHostnameInfo, i);
_LOGT(LOGD_DNS,
" - prio:%5d ipv%c%s %s %s dev:%s",
info->priority,
info->IS_IPv4 ? '4' : '6',
info->is_default ? " (def)" : " ",
info->from_dhcp ? "dhcp " : " ",
info->from_dns ? "dns " : " ",
nm_device_get_iface(info->device));
}
}
for (i = 0; infos && i < infos->len; i++) {
info = &nm_g_array_index(infos, DeviceHostnameInfo, i);
addr_family = info->IS_IPv4 ? AF_INET : AF_INET6;
g_signal_handlers_disconnect_by_func(info->device, device_dns_lookup_done, self);
g_signal_handlers_disconnect_by_func(info->device, device_carrier_changed, priv);
if (info->from_dhcp) {
dhcp_config = nm_device_get_dhcp_config(info->device, addr_family);
if (dhcp_config) {
dhcp_hostname =
nm_dhcp_config_get_option(dhcp_config,
info->IS_IPv4 ? "host_name" : "fqdn_fqdn");
if (dhcp_hostname && dhcp_hostname[0]) {
p = nm_str_skip_leading_spaces(dhcp_hostname);
if (p[0]) {
_set_hostname(self, p, info->IS_IPv4 ? "from DHCPv4" : "from DHCPv6");
priv->dhcp_hostname = TRUE;
return;
}
_LOGW(LOGD_DNS,
"set-hostname: DHCPv%c-provided hostname '%s' looks invalid; "
"ignoring it",
nm_utils_addr_family_to_char(addr_family),
dhcp_hostname);
}
}
}
if (priv->hostname_mode != NM_POLICY_HOSTNAME_MODE_DHCP) {
if (info->from_dns) {
const char *result = NULL;
gboolean wait = FALSE;
if (nm_device_has_carrier(info->device)) {
result =
nm_device_get_hostname_from_dns_lookup(info->device, addr_family, &wait);
} else {
g_signal_connect(info->device,
"notify::" NM_DEVICE_CARRIER,
G_CALLBACK(device_carrier_changed),
priv);
}
if (result) {
_set_hostname(self, result, "from address lookup");
return;
}
if (wait) {
g_signal_connect(info->device,
NM_DEVICE_DNS_LOOKUP_DONE,
G_CALLBACK(device_dns_lookup_done),
self);
return;
}
}
}
}
/* If an hostname was set outside NetworkManager keep it */
if (external_hostname)
return;
if (priv->hostname_mode == NM_POLICY_HOSTNAME_MODE_DHCP) {
/* In dhcp hostname-mode, the hostname is updated only if it comes from
* a DHCP host-name option: if last set was from a host-name option and
* we are here than that connection is gone (with its host-name option),
* so reset the hostname to the previous value
*/
if (priv->dhcp_hostname) {
_set_hostname(self, priv->orig_hostname, "reset dhcp hostname");
priv->dhcp_hostname = FALSE;
}
return;
}
priv->dhcp_hostname = FALSE;
/* If no automatically-configured hostname, try using the last hostname
* set externally to NM
*/
if (priv->orig_hostname) {
_set_hostname(self, priv->orig_hostname, "from system startup");
return;
}
_set_hostname(self, NULL, "no hostname found");
}
static void
update_default_ac(NMPolicy *self, int addr_family, NMActiveConnection *best)
{
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self);
const CList *tmp_list;
NMActiveConnection *ac;
/* Clear the 'default[6]' flag on all active connections that aren't the new
* default active connection. We'll set the new default after; this ensures
* we don't ever have two marked 'default[6]' simultaneously.
*/
nm_manager_for_each_active_connection (priv->manager, ac, tmp_list) {
if (ac != best)
nm_active_connection_set_default(ac, addr_family, FALSE);
}
/* Mark new default active connection */
if (best)