summaryrefslogtreecommitdiff
path: root/src/core/nm-l3-ipv4ll.c
blob: 2aedab56089bbca61e97e343987a21db95c51e34 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "src/core/nm-default-daemon.h"

#include "nm-l3-ipv4ll.h"

#include <net/if.h>
#include <linux/if_ether.h>

#include "n-acd/src/n-acd.h"
#include "nm-core-utils.h"

#define ADDR_IPV4LL_PREFIX_LEN 16

#define TIMED_OUT_TIME_FACTOR 5u

/*****************************************************************************/

typedef enum {
    TIMED_OUT_STATE_IS_NOT_TIMED_OUT,
    TIMED_OUT_STATE_IS_TIMED_OUT,
    TIMED_OUT_STATE_HAVE_TIMER_RUNNING,
} TimedOutState;

struct _NML3IPv4LLRegistration {
    NML3IPv4LL *self;
    CList       reg_lst;
    guint       timeout_msec;
};

G_STATIC_ASSERT(G_STRUCT_OFFSET(NML3IPv4LLRegistration, self) == 0);

struct _NML3IPv4LL {
    NML3Cfg                 *l3cfg;
    int                      ref_count;
    in_addr_t                addr;
    guint                    reg_timeout_msec;
    CList                    reg_lst_head;
    NML3CfgCommitTypeHandle *l3cfg_commit_handle;
    GSource                 *state_change_on_idle_source;
    GSource                 *timed_out_source;
    const NML3ConfigData    *l3cd;
    const NMPObject         *plobj;
    struct {
        nm_le64_t value;
        nm_le64_t generation;
    } seed;
    gint64          timed_out_expiry_msec;
    gulong          l3cfg_signal_notify_id;
    NML3IPv4LLState state;
    NMEtherAddr     seed_mac;
    NMEtherAddr     mac;
    bool            seed_set : 1;
    bool            mac_set : 1;
    bool            notify_on_idle : 1;
    bool            reg_changed : 1;
    bool            l3cd_timeout_msec_changed : 1;

    /* not yet used. */
    bool seed_reset_generation : 1;
};

G_STATIC_ASSERT(G_STRUCT_OFFSET(NML3IPv4LL, ref_count) == sizeof(gpointer));

#define L3CD_TAG(self) (&(((const char *) self)[1]))

/*****************************************************************************/

#define _NMLOG_DOMAIN      LOGD_IP4
#define _NMLOG_PREFIX_NAME "ipv4ll"
#define _NMLOG(level, ...)                                                             \
    G_STMT_START                                                                       \
    {                                                                                  \
        nm_log((level),                                                                \
               (_NMLOG_DOMAIN),                                                        \
               NULL,                                                                   \
               NULL,                                                                   \
               _NMLOG_PREFIX_NAME "[" NM_HASH_OBFUSCATE_PTR_FMT                        \
                                  ",ifindex=%d]: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \
               NM_HASH_OBFUSCATE_PTR(self),                                            \
               nm_l3cfg_get_ifindex((self)->l3cfg) _NM_UTILS_MACRO_REST(__VA_ARGS__)); \
    }                                                                                  \
    G_STMT_END

/*****************************************************************************/

static void _ipv4ll_state_change_on_idle(NML3IPv4LL *self);

static void _ipv4ll_state_change(NML3IPv4LL *self, gboolean is_on_idle_handler);

static void _ipv4ll_set_timed_out_update(NML3IPv4LL *self, TimedOutState new_state);

/*****************************************************************************/

NM_UTILS_ENUM2STR_DEFINE(nm_l3_ipv4ll_state_to_string,
                         NML3IPv4LLState,
                         NM_UTILS_ENUM2STR(NM_L3_IPV4LL_STATE_UNKNOWN, "unknown"),
                         NM_UTILS_ENUM2STR(NM_L3_IPV4LL_STATE_DISABLED, "disabled"),
                         NM_UTILS_ENUM2STR(NM_L3_IPV4LL_STATE_WAIT_FOR_LINK, "wait-for-link"),
                         NM_UTILS_ENUM2STR(NM_L3_IPV4LL_STATE_EXTERNAL, "external"),
                         NM_UTILS_ENUM2STR(NM_L3_IPV4LL_STATE_PROBING, "probing"),
                         NM_UTILS_ENUM2STR(NM_L3_IPV4LL_STATE_DEFENDING, "defending"),
                         NM_UTILS_ENUM2STR(NM_L3_IPV4LL_STATE_READY, "ready"), );

/*****************************************************************************/

#define _ASSERT(self)                                                                        \
    G_STMT_START                                                                             \
    {                                                                                        \
        NML3IPv4LL *const _self = (self);                                                    \
                                                                                             \
        nm_assert(NM_IS_L3_IPV4LL(_self));                                                   \
        if (NM_MORE_ASSERTS > 5) {                                                           \
            nm_assert(_self->addr == 0u || nm_utils_ip4_address_is_link_local(_self->addr)); \
            nm_assert(!_self->l3cd || NM_IS_L3_CONFIG_DATA(_self->l3cd));                    \
        }                                                                                    \
    }                                                                                        \
    G_STMT_END

/*****************************************************************************/

NML3Cfg *
nm_l3_ipv4ll_get_l3cfg(NML3IPv4LL *self)
{
    nm_assert(NM_IS_L3_IPV4LL(self));

    return self->l3cfg;
}

int
nm_l3_ipv4ll_get_ifindex(NML3IPv4LL *self)
{
    nm_assert(NM_IS_L3_IPV4LL(self));

    return nm_l3cfg_get_ifindex(self->l3cfg);
}

NMPlatform *
nm_l3_ipv4ll_get_platform(NML3IPv4LL *self)
{
    nm_assert(NM_IS_L3_IPV4LL(self));

    return nm_l3cfg_get_platform(self->l3cfg);
}

/*****************************************************************************/

NML3IPv4LLState
nm_l3_ipv4ll_get_state(NML3IPv4LL *self)
{
    nm_assert(NM_IS_L3_IPV4LL(self));

    return self->state;
}

static gboolean
_ipv4ll_is_timed_out(NML3IPv4LL *self)
{
    _ASSERT(self);

    return self->timed_out_expiry_msec != 0 && !self->timed_out_source;
}

gboolean
nm_l3_ipv4ll_is_timed_out(NML3IPv4LL *self)
{
    nm_assert(NM_IS_L3_IPV4LL(self));

    return _ipv4ll_is_timed_out(self);
}

in_addr_t
nm_l3_ipv4ll_get_addr(NML3IPv4LL *self)
{
    nm_assert(NM_IS_L3_IPV4LL(self));

    return self->addr;
}

const NML3ConfigData *
nm_l3_ipv4ll_get_l3cd(NML3IPv4LL *self)
{
    nm_assert(NM_IS_L3_IPV4LL(self));

    return self->l3cd;
}

static void
_ipv4ll_emit_signal_notify(NML3IPv4LL *self)
{
    NML3ConfigNotifyData notify_data;

    self->notify_on_idle = FALSE;

    notify_data.notify_type  = NM_L3_CONFIG_NOTIFY_TYPE_IPV4LL_EVENT;
    notify_data.ipv4ll_event = (typeof(notify_data.ipv4ll_event)){
        .ipv4ll = self,
    };
    _nm_l3cfg_emit_signal_notify(self->l3cfg, &notify_data);
}

/*****************************************************************************/

static NML3IPv4LLRegistration *
_registration_update(NML3IPv4LL             *self,
                     NML3IPv4LLRegistration *reg,
                     gboolean                add,
                     guint                   timeout_msec)
{
    nm_auto_unref_l3ipv4ll NML3IPv4LL *self_unref_on_exit = NULL;
    gboolean                           changed            = FALSE;

    if (reg) {
        nm_assert(!self);
        _ASSERT(reg->self);
        self = reg->self;
        nm_assert(c_list_contains(&self->reg_lst_head, &reg->reg_lst));
        nm_assert(self == nm_l3_ipv4ll_register_get_instance(reg));
    } else {
        nm_assert(add);
        _ASSERT(self);
    }

    if (!add) {
        _LOGT("registration[" NM_HASH_OBFUSCATE_PTR_FMT "]: remove", NM_HASH_OBFUSCATE_PTR(reg));
        c_list_unlink_stale(&reg->reg_lst);
        if (c_list_is_empty(&self->reg_lst_head))
            self_unref_on_exit = self;
        nm_g_slice_free(reg);
        reg = NULL;
        goto out;
    }

    if (!reg) {
        reg  = g_slice_new(NML3IPv4LLRegistration);
        *reg = (NML3IPv4LLRegistration){
            .self         = self,
            .timeout_msec = timeout_msec,
        };

        if (c_list_is_empty(&self->reg_lst_head))
            nm_l3_ipv4ll_ref(self);
        c_list_link_tail(&self->reg_lst_head, &reg->reg_lst);
        changed = TRUE;
        _LOGT("registration[" NM_HASH_OBFUSCATE_PTR_FMT "]: add (timeout_msec=%u)",
              NM_HASH_OBFUSCATE_PTR(reg),
              timeout_msec);
    } else {
        if (reg->timeout_msec != timeout_msec) {
            reg->timeout_msec = timeout_msec;
            changed           = TRUE;
        }
        if (changed) {
            _LOGT("registration[" NM_HASH_OBFUSCATE_PTR_FMT "]: update (timeout_msec=%u)",
                  NM_HASH_OBFUSCATE_PTR(reg),
                  timeout_msec);
        }
    }

out:
    if (changed) {
        self->reg_changed = TRUE;
        _ipv4ll_state_change(self, FALSE);
    }
    return reg;
}

NML3IPv4LLRegistration *
nm_l3_ipv4ll_register_new(NML3IPv4LL *self, guint timeout_msec)
{
    return _registration_update(self, NULL, TRUE, timeout_msec);
}

NML3IPv4LLRegistration *
nm_l3_ipv4ll_register_update(NML3IPv4LLRegistration *reg, guint timeout_msec)
{
    return _registration_update(NULL, reg, TRUE, timeout_msec);
}

NML3IPv4LLRegistration *
nm_l3_ipv4ll_register_remove(NML3IPv4LLRegistration *reg)
{
    return _registration_update(NULL, reg, FALSE, 0);
}

/*****************************************************************************/

static gboolean
_ip4_address_is_link_local(const NMPlatformIP4Address *a)
{
    nm_assert(a);

    return nm_utils_ip4_address_is_link_local(a->address) && a->plen == ADDR_IPV4LL_PREFIX_LEN
           && a->address == a->peer_address;
}

static gboolean
_acd_info_is_good(const NML3AcdAddrInfo *acd_info)
{
    if (!acd_info)
        return TRUE;

    switch (acd_info->state) {
    case NM_L3_ACD_ADDR_STATE_INIT:
    case NM_L3_ACD_ADDR_STATE_PROBING:
    case NM_L3_ACD_ADDR_STATE_READY:
    case NM_L3_ACD_ADDR_STATE_DEFENDING:
        return TRUE;
    case NM_L3_ACD_ADDR_STATE_EXTERNAL_REMOVED:
    case NM_L3_ACD_ADDR_STATE_USED:
    case NM_L3_ACD_ADDR_STATE_CONFLICT:
        return FALSE;
    }
    nm_assert_not_reached();
    return FALSE;
}

/*****************************************************************************/

static const NML3ConfigData *
_l3cd_config_create(int ifindex, in_addr_t addr, NMDedupMultiIndex *multi_idx)
{
    nm_auto_unref_l3cd_init NML3ConfigData *l3cd = NULL;

    nm_assert(nm_utils_ip4_address_is_link_local(addr));
    nm_assert(ifindex > 0);
    nm_assert(multi_idx);

    l3cd = nm_l3_config_data_new(multi_idx, ifindex, NM_IP_CONFIG_SOURCE_IP4LL);

    nm_l3_config_data_add_address_4(
        l3cd,
        NM_PLATFORM_IP4_ADDRESS_INIT(.ifindex      = ifindex,
                                     .address      = addr,
                                     .peer_address = addr,
                                     .plen         = ADDR_IPV4LL_PREFIX_LEN,
                                     .addr_source  = NM_IP_CONFIG_SOURCE_IP4LL));

    nm_l3_config_data_add_route_4(l3cd,
                                  NM_PLATFORM_IP4_ROUTE_INIT(.ifindex   = ifindex,
                                                             .network   = htonl(0xE0000000u),
                                                             .plen      = 4,
                                                             .rt_source = NM_IP_CONFIG_SOURCE_IP4LL,
                                                             .table_any = TRUE,
                                                             .metric_any = TRUE));

    return nm_l3_config_data_seal(g_steal_pointer(&l3cd));
}

static in_addr_t
_l3cd_config_get_addr(const NML3ConfigData *l3cd)
{
    NMDedupMultiIter            iter;
    const NMPlatformIP4Address *pladdr;

    if (!l3cd)
        return 0u;

    nm_l3_config_data_iter_ip4_address_for_each (&iter, l3cd, &pladdr) {
        const in_addr_t addr = pladdr->address;

        nm_assert(_ip4_address_is_link_local(pladdr));
#if NM_MORE_ASSERTS > 10
        {
            nm_auto_unref_l3cd const NML3ConfigData *l3cd2 = NULL;

            l3cd2 = _l3cd_config_create(nm_l3_config_data_get_ifindex(l3cd),
                                        addr,
                                        nm_l3_config_data_get_multi_idx(l3cd));
            nm_assert(nm_l3_config_data_equal(l3cd2, l3cd));
        }
#endif
        return addr;
    }

    return nm_assert_unreachable_val(0u);
}

/*****************************************************************************/

static void
_ipv4ll_addrgen(NML3IPv4LL *self, gboolean generate_new_addr)
{
    CSipHash  state;
    char      sbuf_addr[NM_UTILS_INET_ADDRSTRLEN];
    gboolean  seed_changed = FALSE;
    in_addr_t addr_new;
    guint64   h;

    _ASSERT(self);

    /* MAC_HASH_KEY is the same as used by systemd. */
#define MAC_HASH_KEY          \
    ((const guint8[16]){0xdf, \
                        0x04, \
                        0x22, \
                        0x98, \
                        0x3f, \
                        0xad, \
                        0x14, \
                        0x52, \
                        0xf9, \
                        0x87, \
                        0x2e, \
                        0xd1, \
                        0x9c, \
                        0x70, \
                        0xe2, \
                        0xf2})

    if (self->mac_set && (!self->seed_set || !nm_ether_addr_equal(&self->mac, &self->seed_mac))) {
        /* systemd's ipv4ll library by default only hashes the MAC address (as we do here).
         * This is also what previous versions of NetworkManager did (whenn using sd_ipv4ll).
         *
         * On the other hand, systemd-networkd uses net_get_name_persistent() of the device
         * mixed with /etc/machine-id.
         *
         * See also: https://tools.ietf.org/html/rfc3927#section-2.1
         *
         * FIXME(l3cfg): At this point, maybe we should also mix it with nm_utils_host_id_get().
         * This would get the behavior closer to what systemd-networkd does.
         * Don't do that for now, because it would be a change in behavior compared
         * to earlier versions of NetworkManager.  */

        c_siphash_init(&state, MAC_HASH_KEY);
        c_siphash_append(&state, self->mac.ether_addr_octet, ETH_ALEN);
        h = c_siphash_finalize(&state);

        _LOGT("addr-gen: %sset seed (for " NM_ETHER_ADDR_FORMAT_STR ")",
              self->seed_set ? "re" : "",
              NM_ETHER_ADDR_FORMAT_VAL(&self->mac));

        self->seed_set              = TRUE;
        self->seed_mac              = self->mac;
        self->seed.generation       = htole64(0);
        self->seed.value            = htole64(h);
        self->seed_reset_generation = FALSE;
        self->addr                  = 0u;

        seed_changed = TRUE;
    }

    if (!self->seed_set) {
        /* we have no seed set (and consequently no MAC address set either).
         * We cannot generate an address. */
        nm_assert(self->addr == 0u);
        return;
    }

    nm_assert(seed_changed || self->seed.generation != htole64(0u));

    if (self->seed_reset_generation) {
        _LOGT("addr-gen: reset seed (generation only)");
        self->seed.generation = htole64(0);
        self->addr            = 0u;
        seed_changed          = TRUE;
    }

    if (!seed_changed && !generate_new_addr) {
        /* neither did the caller request a new address, nor was the seed changed. The current
         * address is still to be used. */
        nm_assert(nm_utils_ip4_address_is_link_local(self->addr));
        return;
    }

gen_addr:

#define PICK_HASH_KEY         \
    ((const guint8[16]){0x15, \
                        0xac, \
                        0x82, \
                        0xa6, \
                        0xd6, \
                        0x3f, \
                        0x49, \
                        0x78, \
                        0x98, \
                        0x77, \
                        0x5d, \
                        0x0c, \
                        0x69, \
                        0x02, \
                        0x94, \
                        0x0b})

    h = c_siphash_hash(PICK_HASH_KEY, (const guint8 *) &self->seed, sizeof(self->seed));

    self->seed.generation = htole64(le64toh(self->seed.generation) + 1u);

    addr_new = htonl(h & UINT32_C(0x0000FFFF)) | NM_IPV4LL_NETWORK;

    if (self->addr == addr_new || NM_IN_SET(ntohl(addr_new) & 0x0000FF00u, 0x0000u, 0xFF00u))
        goto gen_addr;

    nm_assert(nm_utils_ip4_address_is_link_local(addr_new));

    _LOGT("addr-gen: set address %s", _nm_utils_inet4_ntop(addr_new, sbuf_addr));
    self->addr = addr_new;
}

/*****************************************************************************/

static void
_ipv4ll_update_link(NML3IPv4LL *self, const NMPObject *plobj)
{
    char                            sbuf[ETH_ALEN * 3];
    nm_auto_nmpobj const NMPObject *pllink_old = NULL;
    const NMEtherAddr              *mac_new;
    gboolean                        changed;

    if (self->plobj == plobj)
        return;

    pllink_old  = g_steal_pointer(&self->plobj);
    self->plobj = nmp_object_ref(plobj);

    mac_new = NULL;
    if (plobj) {
        const NMPlatformLink *pllink = NMP_OBJECT_CAST_LINK(plobj);

        if (pllink->l_address.len == ETH_ALEN)
            mac_new = &pllink->l_address.ether_addr;
    }

    changed = FALSE;
    if (!mac_new) {
        if (self->mac_set) {
            changed       = TRUE;
            self->mac_set = FALSE;
        }
    } else {
        if (!self->mac_set || !nm_ether_addr_equal(mac_new, &self->mac)) {
            changed       = TRUE;
            self->mac_set = TRUE;
            self->mac     = *mac_new;
        }
    }

    if (changed) {
        _LOGT("mac changed: %s",
              self->mac_set ? _nm_utils_hwaddr_ntoa(&self->mac, ETH_ALEN, TRUE, sbuf, sizeof(sbuf))
                            : "unset");
    }
}

/*****************************************************************************/

static void
_l3cd_config_add(NML3IPv4LL *self)
{
    nm_auto_unref_l3cd const NML3ConfigData *l3cd = NULL;
    char                                     sbuf_addr[NM_UTILS_INET_ADDRSTRLEN];
    gboolean                                 changed;

    _ASSERT(self);
    nm_assert(self->addr != 0u);
    nm_assert(self->reg_timeout_msec > 0u);

    if (_l3cd_config_get_addr(self->l3cd) != self->addr) {
        l3cd = _l3cd_config_create(nm_l3_ipv4ll_get_ifindex(self),
                                   self->addr,
                                   nm_l3cfg_get_multi_idx(self->l3cfg));
        nm_assert(!nm_l3_config_data_equal(l3cd, self->l3cd));
        changed = TRUE;
    } else
        changed = FALSE;

    if (!changed && !self->l3cd_timeout_msec_changed)
        return;

    self->l3cd_timeout_msec_changed = FALSE;

    _LOGT("add l3cd config with %s (acd-timeout %u msec%s)",
          _nm_utils_inet4_ntop(self->addr, sbuf_addr),
          self->reg_timeout_msec,
          changed ? "" : " changed");

    if (changed) {
        NM_SWAP(&l3cd, &self->l3cd);
        self->notify_on_idle = TRUE;
    }

    if (!nm_l3cfg_add_config(self->l3cfg,
                             L3CD_TAG(self),
                             TRUE,
                             self->l3cd,
                             NM_L3CFG_CONFIG_PRIORITY_IPV4LL,
                             0,
                             0,
                             NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP4,
                             NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP6,
                             0,
                             0,
                             NM_DNS_PRIORITY_DEFAULT_NORMAL,
                             NM_DNS_PRIORITY_DEFAULT_NORMAL,
                             NM_L3_ACD_DEFEND_TYPE_ONCE,
                             self->reg_timeout_msec,
                             NM_L3CFG_CONFIG_FLAGS_ONLY_FOR_ACD,
                             NM_L3_CONFIG_MERGE_FLAGS_NONE))
        nm_assert_not_reached();

    self->l3cfg_commit_handle = nm_l3cfg_commit_type_register(self->l3cfg,
                                                              NM_L3_CFG_COMMIT_TYPE_UPDATE,
                                                              self->l3cfg_commit_handle,
                                                              "ipv4ll");
    nm_l3cfg_commit_on_idle_schedule(self->l3cfg, NM_L3_CFG_COMMIT_TYPE_AUTO);
}

static gboolean
_l3cd_config_remove(NML3IPv4LL *self)
{
    nm_auto_unref_l3cd const NML3ConfigData *l3cd = NULL;

    nm_assert(NM_IS_L3_IPV4LL(self));

    if (!self->l3cd)
        return FALSE;

    _LOGT("remove l3cd config");

    self->notify_on_idle = TRUE;

    l3cd = g_steal_pointer(&self->l3cd);
    if (!nm_l3cfg_remove_config(self->l3cfg, L3CD_TAG(self), l3cd))
        nm_assert_not_reached();

    nm_l3cfg_commit_type_unregister(self->l3cfg, g_steal_pointer(&self->l3cfg_commit_handle));
    nm_l3cfg_commit_on_idle_schedule(self->l3cfg, NM_L3_CFG_COMMIT_TYPE_AUTO);
    return TRUE;
}

/*****************************************************************************/

static const NMPlatformIP4Address *
_ipv4ll_platform_ip4_address_lookup(NML3IPv4LL *self, in_addr_t addr)
{
    const NMPlatformIP4Address *pladdr;

    if (addr == 0u)
        return NULL;

    nm_assert(nm_utils_ip4_address_is_link_local(addr));

    pladdr = nm_platform_ip4_address_get(nm_l3_ipv4ll_get_platform(self),
                                         nm_l3_ipv4ll_get_ifindex(self),
                                         addr,
                                         ADDR_IPV4LL_PREFIX_LEN,
                                         addr);

    nm_assert(!pladdr || pladdr->address == addr);
    nm_assert(!pladdr || _ip4_address_is_link_local(pladdr));
    return pladdr;
}

static const NML3AcdAddrInfo *
_ipv4ll_l3cfg_get_acd_addr_info(NML3IPv4LL *self, in_addr_t addr)
{
    if (addr == 0u)
        return NULL;

    nm_assert(nm_utils_ip4_address_is_link_local(addr));
    return nm_l3cfg_get_acd_addr_info(self->l3cfg, addr);
}

static const NMPlatformIP4Address *
_ipv4ll_platform_find_addr(NML3IPv4LL *self, const NML3AcdAddrInfo **out_acd_info)
{
    const NMPlatformIP4Address *addr_without_acd_info = NULL;
    NMDedupMultiIter            iter;
    NMPLookup                   lookup;
    const NMPObject            *obj;
    const NML3AcdAddrInfo      *acd_info;
    const NMPlatformIP4Address *addr;

    nmp_lookup_init_object(&lookup, NMP_OBJECT_TYPE_IP4_ADDRESS, nm_l3_ipv4ll_get_ifindex(self));
    nm_platform_iter_obj_for_each (&iter, nm_l3_ipv4ll_get_platform(self), &lookup, &obj) {
        addr = NMP_OBJECT_CAST_IP4_ADDRESS(obj);
        if (!_ip4_address_is_link_local(addr))
            continue;

        acd_info = _ipv4ll_l3cfg_get_acd_addr_info(self, addr->address);
        if (!_acd_info_is_good(acd_info))
            continue;

        if (acd_info) {
            /* We have a good acd_info. We won't find a better one. Return it. */
            NM_SET_OUT(out_acd_info, acd_info);
            return addr;
        }

        if (!addr_without_acd_info) {
            /* remember a potential candidate address that has no acd_info. */
            addr_without_acd_info = addr;
        }
    }

    if (addr_without_acd_info) {
        NM_SET_OUT(out_acd_info, NULL);
        return addr_without_acd_info;
    }

    return NULL;
}

/*****************************************************************************/

static gboolean
_ipv4ll_set_timed_out_timeout_cb(gpointer user_data)
{
    NML3IPv4LL *self = user_data;

    _ipv4ll_set_timed_out_update(self, TIMED_OUT_STATE_IS_TIMED_OUT);
    if (self->notify_on_idle)
        _ipv4ll_emit_signal_notify(self);
    return G_SOURCE_REMOVE;
}

static void
_ipv4ll_set_timed_out_update(NML3IPv4LL *self, TimedOutState new_state)
{
    gboolean before;

    before = _ipv4ll_is_timed_out(self);

    switch (new_state) {
    case TIMED_OUT_STATE_IS_TIMED_OUT:
        if (self->timed_out_expiry_msec == 0) {
            nm_assert(!self->timed_out_source);
            self->timed_out_expiry_msec = 1;
        }
        nm_clear_g_source_inst(&self->timed_out_source);
        break;
    case TIMED_OUT_STATE_IS_NOT_TIMED_OUT:
        self->timed_out_expiry_msec = 0;
        nm_clear_g_source_inst(&self->timed_out_source);
        break;
    case TIMED_OUT_STATE_HAVE_TIMER_RUNNING:
    {
        gint64 now_msec = nm_utils_get_monotonic_timestamp_msec();
        guint  timeout_msec;
        gint64 expiry_msec;

        nm_assert(self->reg_timeout_msec > 0u);

        timeout_msec = nm_mult_clamped_u(TIMED_OUT_TIME_FACTOR, self->reg_timeout_msec);
        expiry_msec  = now_msec + timeout_msec;

        if (self->timed_out_expiry_msec == 0 || self->timed_out_expiry_msec < expiry_msec) {
            self->timed_out_expiry_msec = expiry_msec;
            nm_clear_g_source_inst(&self->timed_out_source);
            self->timed_out_source =
                nm_g_timeout_add_source(timeout_msec, _ipv4ll_set_timed_out_timeout_cb, self);
        }
        break;
    }
    }

    if (before != _ipv4ll_is_timed_out(self)) {
        self->notify_on_idle = TRUE;
        _LOGT("state: set timed-out-is-bad=%d", (!before));
    }
}

static gboolean
_ipv4ll_set_state(NML3IPv4LL *self, NML3IPv4LLState state)
{
    char sbuf_addr[NM_UTILS_INET_ADDRSTRLEN];
    char sbuf100[100];

    if (self->state == state)
        return FALSE;
    self->state          = state;
    self->notify_on_idle = TRUE;
    _LOGT("state: set state %s (addr=%s)",
          nm_l3_ipv4ll_state_to_string(state, sbuf100, sizeof(sbuf100)),
          _nm_utils_inet4_ntop(self->addr, sbuf_addr));
    return TRUE;
}

static void
_ipv4ll_state_change(NML3IPv4LL *self, gboolean is_on_idle_handler)
{
    nm_auto_unref_l3ipv4ll NML3IPv4LL *self_keep_alive = NULL;
    const NMPlatformIP4Address        *pladdr;
    const NML3AcdAddrInfo             *acd_info;
    gboolean                           generate_new_addr;
    NML3IPv4LLState                    new_state;
    in_addr_t                          addr0;
    NML3IPv4LLRegistration            *reg;

    _ASSERT(self);

    self_keep_alive = nm_l3_ipv4ll_ref(self);

    nm_clear_g_source_inst(&self->state_change_on_idle_source);

    addr0 = self->addr;

    if (self->reg_changed) {
        guint timeout_msec = self->reg_timeout_msec;

        self->reg_changed = FALSE;

        if (c_list_is_empty(&self->reg_lst_head))
            timeout_msec = 0;
        else {
            timeout_msec = G_MAXUINT;
            c_list_for_each_entry (reg, &self->reg_lst_head, reg_lst) {
                if (reg->timeout_msec < timeout_msec)
                    timeout_msec = reg->timeout_msec;
                if (reg->timeout_msec == 0)
                    break;
            }
        }
        if (self->reg_timeout_msec != timeout_msec) {
            self->reg_timeout_msec          = timeout_msec;
            self->l3cd_timeout_msec_changed = TRUE;
        }
    }

    if (self->reg_timeout_msec == 0) {
        _ipv4ll_set_timed_out_update(self, TIMED_OUT_STATE_IS_NOT_TIMED_OUT);
        if (_ipv4ll_set_state(self, NM_L3_IPV4LL_STATE_DISABLED))
            _l3cd_config_remove(self);
        goto out_notify;
    }

    if (!self->mac_set) {
        _ipv4ll_set_timed_out_update(self, TIMED_OUT_STATE_HAVE_TIMER_RUNNING);
        if (_ipv4ll_set_state(self, NM_L3_IPV4LL_STATE_WAIT_FOR_LINK))
            _l3cd_config_remove(self);
        else
            nm_assert(!self->l3cd);
        goto out_notify;
    }

    if (self->state <= NM_L3_IPV4LL_STATE_EXTERNAL) {
        pladdr = _ipv4ll_platform_ip4_address_lookup(self, self->addr);
        if (pladdr) {
            if (!_acd_info_is_good(_ipv4ll_l3cfg_get_acd_addr_info(self, self->addr)))
                pladdr = NULL;
        }
        if (!pladdr)
            pladdr = _ipv4ll_platform_find_addr(self, NULL);

        if (pladdr) {
            /* we have an externally configured address. Check whether we can use it. */
            self->addr           = pladdr->address;
            self->notify_on_idle = TRUE;
            _ipv4ll_set_state(self, NM_L3_IPV4LL_STATE_EXTERNAL);
            _l3cd_config_add(self);
            _ipv4ll_set_timed_out_update(self, TIMED_OUT_STATE_IS_NOT_TIMED_OUT);
            goto out_notify;
        }
    }

    generate_new_addr = FALSE;
    while (TRUE) {
        _ipv4ll_addrgen(self, generate_new_addr);
        acd_info = _ipv4ll_l3cfg_get_acd_addr_info(self, self->addr);
        if (_acd_info_is_good(acd_info))
            break;
        generate_new_addr = TRUE;
    }

    nm_assert(_acd_info_is_good(acd_info));
    switch (acd_info ? acd_info->state : NM_L3_ACD_ADDR_STATE_INIT) {
    case NM_L3_ACD_ADDR_STATE_INIT:
    case NM_L3_ACD_ADDR_STATE_PROBING:
        new_state = NM_L3_IPV4LL_STATE_PROBING;
        _ipv4ll_set_timed_out_update(self, TIMED_OUT_STATE_HAVE_TIMER_RUNNING);
        goto out_is_good_1;
    case NM_L3_ACD_ADDR_STATE_READY:
        new_state = NM_L3_IPV4LL_STATE_READY;
        _ipv4ll_set_timed_out_update(self, TIMED_OUT_STATE_HAVE_TIMER_RUNNING);
        goto out_is_good_1;
    case NM_L3_ACD_ADDR_STATE_DEFENDING:
        new_state = NM_L3_IPV4LL_STATE_DEFENDING;
        _ipv4ll_set_timed_out_update(self, TIMED_OUT_STATE_IS_NOT_TIMED_OUT);
        goto out_is_good_1;
    case NM_L3_ACD_ADDR_STATE_EXTERNAL_REMOVED:
    case NM_L3_ACD_ADDR_STATE_USED:
    case NM_L3_ACD_ADDR_STATE_CONFLICT:
        nm_assert_not_reached();
        goto out_notify;
    }
    nm_assert_not_reached();
    goto out_notify;
out_is_good_1:
    _ipv4ll_set_state(self, new_state);
    _l3cd_config_add(self);
    if (self->addr != addr0)
        self->notify_on_idle = TRUE;
    goto out_notify;

out_notify:
    if (self->notify_on_idle) {
        if (is_on_idle_handler)
            _ipv4ll_emit_signal_notify(self);
        else
            _ipv4ll_state_change_on_idle(self);
    }
}

static gboolean
_ipv4ll_state_change_on_idle_cb(gpointer user_data)
{
    NML3IPv4LL *self = user_data;

    _ipv4ll_state_change(self, TRUE);
    return G_SOURCE_REMOVE;
}

static void
_ipv4ll_state_change_on_idle(NML3IPv4LL *self)
{
    nm_assert(NM_IS_L3_IPV4LL(self));

    if (!self->state_change_on_idle_source) {
        self->state_change_on_idle_source =
            nm_g_idle_add_source(_ipv4ll_state_change_on_idle_cb, self);
    }
}

/*****************************************************************************/

static void
_l3cfg_notify_cb(NML3Cfg *l3cfg, const NML3ConfigNotifyData *notify_data, NML3IPv4LL *self)
{
    if (notify_data->notify_type == NM_L3_CONFIG_NOTIFY_TYPE_PLATFORM_CHANGE_ON_IDLE) {
        /* NMl3Cfg only reloads the platform link during the idle handler. Pick it up now. */
        _ipv4ll_update_link(self, nm_l3cfg_get_plobj(l3cfg, FALSE));

        /* theoretically, this even is already on an idle handler. However, we share
         * the call with other signal handlers, so at this point we don't want to
         * emit additional signals. Thus pass %FALSE to _ipv4ll_state_change(). */
        _ipv4ll_state_change(self, FALSE);
        return;
    }

    if (notify_data->notify_type == NM_L3_CONFIG_NOTIFY_TYPE_ACD_EVENT) {
        if (self->l3cd
            && nm_l3_acd_addr_info_find_track_info(&notify_data->acd_event.info,
                                                   L3CD_TAG(self),
                                                   self->l3cd,
                                                   NULL)) {
            _ipv4ll_state_change(self, FALSE);
        }
        return;
    }
}

/*****************************************************************************/

NML3IPv4LL *
nm_l3_ipv4ll_new(NML3Cfg *l3cfg)
{
    NML3IPv4LL *self;

    g_return_val_if_fail(NM_IS_L3CFG(l3cfg), NULL);

    self  = g_slice_new(NML3IPv4LL);
    *self = (NML3IPv4LL){
        .l3cfg                       = g_object_ref(l3cfg),
        .ref_count                   = 1,
        .reg_lst_head                = C_LIST_INIT(self->reg_lst_head),
        .l3cfg_commit_handle         = NULL,
        .state_change_on_idle_source = NULL,
        .l3cd                        = NULL,
        .plobj                       = NULL,
        .addr                        = 0u,
        .state                       = NM_L3_IPV4LL_STATE_DISABLED,
        .reg_timeout_msec            = 0,
        .notify_on_idle              = TRUE,
        .l3cfg_signal_notify_id =
            g_signal_connect(l3cfg, NM_L3CFG_SIGNAL_NOTIFY, G_CALLBACK(_l3cfg_notify_cb), self),
        .seed_set              = FALSE,
        .seed_reset_generation = FALSE,
    };

    _LOGT("created: l3cfg=" NM_HASH_OBFUSCATE_PTR_FMT, NM_HASH_OBFUSCATE_PTR(l3cfg));

    _ipv4ll_update_link(self, nm_l3cfg_get_plobj(l3cfg, FALSE));
    _ipv4ll_state_change(self, FALSE);
    return self;
}

NML3IPv4LL *
nm_l3_ipv4ll_ref(NML3IPv4LL *self)
{
    if (!self)
        return NULL;

    _ASSERT(self);

    nm_assert(self->ref_count < G_MAXINT);
    self->ref_count++;
    return self;
}

void
nm_l3_ipv4ll_unref(NML3IPv4LL *self)
{
    if (!self)
        return;

    _ASSERT(self);

    if (--self->ref_count > 0)
        return;

    if (nm_l3cfg_get_ipv4ll(self->l3cfg) == self)
        _nm_l3cfg_unregister_ipv4ll(self->l3cfg);

    _LOGT("finalize");

    nm_assert(c_list_is_empty(&self->reg_lst_head));

    if (self->l3cd) {
        nm_auto_unref_l3cd const NML3ConfigData *l3cd = NULL;

        l3cd = g_steal_pointer(&self->l3cd);
        if (!nm_l3cfg_remove_config(self->l3cfg, L3CD_TAG(self), l3cd))
            nm_assert_not_reached();

        nm_l3cfg_commit_type_unregister(self->l3cfg, g_steal_pointer(&self->l3cfg_commit_handle));
        nm_l3cfg_commit_on_idle_schedule(self->l3cfg, NM_L3_CFG_COMMIT_TYPE_AUTO);
    } else
        nm_assert(!self->l3cfg_commit_handle);

    nm_clear_g_source_inst(&self->state_change_on_idle_source);
    nm_clear_g_source_inst(&self->timed_out_source);

    nm_clear_g_signal_handler(self->l3cfg, &self->l3cfg_signal_notify_id);

    g_clear_object(&self->l3cfg);
    nmp_object_unref(self->plobj);
    nm_g_slice_free(self);
}