summaryrefslogtreecommitdiffstats
path: root/src/network/networkd-dhcp6.c
blob: 6d1496306c9e81b1a993079399ebc6f3e60db698 (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
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
/* SPDX-License-Identifier: LGPL-2.1+ */
/***
  Copyright © 2014 Intel Corporation. All rights reserved.
***/

#include <netinet/in.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include "sd-radv.h"

#include "sd-dhcp6-client.h"

#include "hashmap.h"
#include "hostname-util.h"
#include "missing_network.h"
#include "network-internal.h"
#include "networkd-dhcp6.h"
#include "networkd-link.h"
#include "networkd-manager.h"
#include "siphash24.h"
#include "string-util.h"
#include "radv-internal.h"

static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link);
static Link *dhcp6_prefix_get(Manager *m, struct in6_addr *addr);
static int dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link);
static int dhcp6_prefix_remove_all(Manager *m, Link *link);
static bool dhcp6_link_has_dhcpv6_prefix(Link *link);
static int dhcp6_assign_delegated_prefix(Link *link, const struct in6_addr *prefix,
                                         uint8_t prefix_len,
                                         uint32_t lifetime_preferred,
                                         uint32_t lifetime_valid);

static bool dhcp6_get_prefix_delegation(Link *link) {
        if (!link->network)
                return false;

        return IN_SET(link->network->router_prefix_delegation,
                      RADV_PREFIX_DELEGATION_DHCP6,
                      RADV_PREFIX_DELEGATION_BOTH);
}

static bool dhcp6_has_preferred_subnet_id(Link *link) {
        if (!link->network)
                return false;

        return link->network->router_prefix_subnet_id >= 0;
}

static int dhcp6_get_preferred_delegated_prefix(
                Manager* manager,
                Link *link,
                const struct in6_addr *pd_prefix,
                uint8_t pd_prefix_len,
                struct in6_addr *ret_addr) {
        int r;
        union in_addr_union pd_prefix_union = {
                .in6 = *pd_prefix,
        };
        int64_t subnet_id = link->network->router_prefix_subnet_id;

        assert(pd_prefix_len <= 64);

        uint8_t prefix_bits = 64 - pd_prefix_len;
        uint64_t n_prefixes = UINT64_C(1) << prefix_bits;
        _cleanup_free_ char *assigned_buf = NULL;

        /* We start off with the original PD prefix we have been assigned and
         * iterate from there */
        union in_addr_union prefix = {
                .in6 = *pd_prefix,
        };

        if (subnet_id >= 0) {
                /* If the link has a preference for a particular subnet id try to allocate that */
                if ((uint64_t)subnet_id >= n_prefixes)
                        return log_link_debug_errno(link,
                                        SYNTHETIC_ERRNO(ERANGE),
                                        "subnet id %" PRIi64 " is out of range. Only have %" PRIu64 " subnets.",
                                        subnet_id,
                                        n_prefixes);

                r = in_addr_prefix_nth(AF_INET6, &prefix, 64, subnet_id);
                if (r < 0)
                        return log_link_debug_errno(link,
                                        r,
                                        "subnet id %" PRIi64 " is out of range. Only have %" PRIu64 " subnets.",
                                        subnet_id,
                                        n_prefixes);

                /* Verify that the prefix we did calculate fits in the pd prefix.
                 * This should not fail as we checked the prefix size beforehand */
                assert_se(in_addr_prefix_covers(AF_INET6, &pd_prefix_union, pd_prefix_len, &prefix) > 0);

                Link* assigned_link = dhcp6_prefix_get(manager, &prefix.in6);

                (void) in_addr_to_string(AF_INET6, &prefix, &assigned_buf);

                if (assigned_link && assigned_link != link)
                        return log_link_error_errno(link, SYNTHETIC_ERRNO(EAGAIN),
                                       "The requested prefix %s is already assigned to another link: %s",
                                       strnull(assigned_buf),
                                       strnull(assigned_link->ifname));

                *ret_addr = prefix.in6;

                log_link_debug(link, "The requested prefix %s is available. Using it.",
                               strnull(assigned_buf));
                return 0;
        } else {
                for (uint64_t n = 0; n < n_prefixes; n++) {
                        /* if we do not have an allocation preference just iterate
                         * through the address space and return the first free prefix. */
                        Link* assigned_link = dhcp6_prefix_get(manager, &prefix.in6);

                        if (!assigned_link || assigned_link == link) {
                                *ret_addr = prefix.in6;
                                return 0;
                        }

                        r = in_addr_prefix_next(AF_INET6, &prefix, 64);
                        if (r < 0)
                                return log_link_error_errno(link,
                                                r,
                                                "Can't allocate another prefix. Out of address space?");
                }

                log_link_warning(link, "Couldn't find a suitable prefix. Ran out of address space.");
        }

        return -ERANGE;
}

static bool dhcp6_enable_prefix_delegation(Link *dhcp6_link) {
        Manager *manager;
        Link *l;
        Iterator i;

        assert(dhcp6_link);

        manager = dhcp6_link->manager;
        assert(manager);

        HASHMAP_FOREACH(l, manager->links, i) {
                if (l == dhcp6_link)
                        continue;

                if (!dhcp6_get_prefix_delegation(l))
                        continue;

                return true;
        }

        return false;
}

static int dhcp6_lease_information_acquired(sd_dhcp6_client *client,
                                        Link *link) {
        return 0;
}

static int dhcp6_pd_prefix_assign(Link *link, struct in6_addr *prefix,
                                  uint8_t prefix_len,
                                  uint32_t lifetime_preferred,
                                  uint32_t lifetime_valid) {
        sd_radv *radv = link->radv;
        int r;
        _cleanup_(sd_radv_prefix_unrefp) sd_radv_prefix *p = NULL;

        r = sd_radv_prefix_new(&p);
        if (r < 0)
                return r;

        r = sd_radv_prefix_set_prefix(p, prefix, prefix_len);
        if (r < 0)
                return r;

        r = sd_radv_prefix_set_preferred_lifetime(p, lifetime_preferred);
        if (r < 0)
                return r;

        r = sd_radv_prefix_set_valid_lifetime(p, lifetime_valid);
        if (r < 0)
                return r;

        r = sd_radv_stop(radv);
        if (r < 0)
                return r;

        r = sd_radv_add_prefix(radv, p, true);
        if (r < 0 && r != -EEXIST)
                return r;

        r = dhcp6_prefix_add(link->manager, prefix, link);
        if (r < 0)
                return r;

        if (link->network->dhcp6_pd_assign_prefix) {
                r = dhcp6_assign_delegated_prefix(link, prefix, prefix_len, lifetime_preferred, lifetime_valid);
                if (r < 0)
                        return r;
        }

        return sd_radv_start(radv);
}

static int dhcp6_route_remove_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
        int r;

        assert(link);

        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
                return 1;

        r = sd_netlink_message_get_errno(m);
        if (r < 0)
                log_link_message_warning_errno(link, m, r, "Received error on unreachable route removal for DHCPv6 delegated subnet");

        return 1;
}

int dhcp6_lease_pd_prefix_lost(sd_dhcp6_client *client, Link* link) {
        int r;
        sd_dhcp6_lease *lease;
        union in_addr_union pd_prefix;
        uint8_t pd_prefix_len;
        uint32_t lifetime_preferred, lifetime_valid;

        r = sd_dhcp6_client_get_lease(client, &lease);
        if (r < 0)
                return r;

        sd_dhcp6_lease_reset_pd_prefix_iter(lease);

        while (sd_dhcp6_lease_get_pd(lease, &pd_prefix.in6, &pd_prefix_len,
                                     &lifetime_preferred,
                                     &lifetime_valid) >= 0) {
                _cleanup_free_ char *buf = NULL;
                _cleanup_(route_freep) Route *route = NULL;

                if (pd_prefix_len >= 64)
                        continue;

                (void) in_addr_to_string(AF_INET6, &pd_prefix, &buf);

                r = route_new(&route);
                if (r < 0)
                        return r;

                route->family = AF_INET6;
                route->dst = pd_prefix;
                route->dst_prefixlen = pd_prefix_len;
                route->type = RTN_UNREACHABLE;

                r = route_remove(route, link, dhcp6_route_remove_handler);
                if (r < 0) {
                        log_link_warning_errno(link, r, "Cannot delete unreachable route for DHCPv6 delegated subnet %s/%u: %m",
                                               strnull(buf),
                                               pd_prefix_len);
                        continue;
                }

                log_link_debug(link, "Removing unreachable route %s/%u",
                               strnull(buf), pd_prefix_len);
        }

        return 0;
}

static int dhcp6_pd_prefix_distribute(Link *dhcp6_link,
                                      struct in6_addr *pd_prefix,
                                      uint8_t pd_prefix_len,
                                      uint32_t lifetime_preferred,
                                      uint32_t lifetime_valid,
                                      bool assign_preferred_subnet_id) {
        Iterator i;
        Link *link;
        Manager *manager = dhcp6_link->manager;
        union in_addr_union prefix = {
                .in6 = *pd_prefix,
        };
        uint64_t n_prefixes;
        _cleanup_free_ char *buf = NULL;
        _cleanup_free_ char *assigned_buf = NULL;
        int r;
        bool pool_depleted = false;

        assert(manager);
        assert(pd_prefix_len <= 64);

        r = in_addr_mask(AF_INET6, &prefix, pd_prefix_len);
        if (r < 0)
                return r;

        n_prefixes = UINT64_C(1) << (64 - pd_prefix_len);

        (void) in_addr_to_string(AF_INET6, &prefix, &buf);
        log_link_debug(dhcp6_link, "Assigning up to %" PRIu64 " prefixes from %s/%u",
                       n_prefixes, strnull(buf), pd_prefix_len);

        HASHMAP_FOREACH(link, manager->links, i) {
                union in_addr_union assigned_prefix;

                if (link == dhcp6_link)
                        continue;

                if (!dhcp6_get_prefix_delegation(link))
                        continue;

                if (dhcp6_link_has_dhcpv6_prefix(link))
                        continue;

                if (assign_preferred_subnet_id != dhcp6_has_preferred_subnet_id(link))
                        continue;

                r = dhcp6_get_preferred_delegated_prefix(manager, link, &prefix.in6, pd_prefix_len,
                                                        &assigned_prefix.in6);

                if (assign_preferred_subnet_id && r == -EAGAIN) {
                        /* A link has a preferred subnet_id but that one is
                         * already taken by another link. Now all the remaining
                         * links will also not obtain a prefix. */
                        pool_depleted = true;
                        continue;
                } else if (r < 0)
                        return r;

                (void) in_addr_to_string(AF_INET6, &assigned_prefix, &assigned_buf);
                r = dhcp6_pd_prefix_assign(link, &assigned_prefix.in6, 64,
                                           lifetime_preferred, lifetime_valid);
                if (r < 0) {
                        log_link_error_errno(link, r, "Unable to assign/update prefix %s/64 from %s/%u for link: %m",
                                             strnull(assigned_buf),
                                             strnull(buf), pd_prefix_len);
                } else
                        log_link_debug(link, "Assigned prefix %s/64 from %s/%u to link",
                                       strnull(assigned_buf),
                                       strnull(buf), pd_prefix_len);
        }

        /* If one of the link requests couldn't be fulfilled, signal that we
           should try again with another prefix. */
        if (pool_depleted)
                return -EAGAIN;

        return 0;
}

static int dhcp6_route_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
        int r;

        assert(link);

        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
                return 1;

        r = sd_netlink_message_get_errno(m);
        if (r < 0 && r != -EEXIST)
                log_link_message_warning_errno(link, m, r, "Received error when adding unreachable route for DHCPv6 delegated subnet");

        return 1;
}

static int dhcp6_lease_pd_prefix_acquired(sd_dhcp6_client *client, Link *link) {
        int r;
        sd_dhcp6_lease *lease;
        union in_addr_union pd_prefix;
        uint8_t pd_prefix_len;
        uint32_t lifetime_preferred, lifetime_valid;

        r = sd_dhcp6_client_get_lease(client, &lease);
        if (r < 0)
                return r;

        sd_dhcp6_lease_reset_pd_prefix_iter(lease);

        while (sd_dhcp6_lease_get_pd(lease, &pd_prefix.in6, &pd_prefix_len,
                                     &lifetime_preferred,
                                     &lifetime_valid) >= 0) {

                _cleanup_free_ char *buf = NULL;

                (void) in_addr_to_string(AF_INET6, &pd_prefix, &buf);

                if (pd_prefix_len > 64) {
                        log_link_debug(link, "PD Prefix length > 64, ignoring prefix %s/%u",
                                       strnull(buf), pd_prefix_len);
                        continue;
                }

                if (pd_prefix_len < 48)
                        log_link_warning(link, "PD Prefix length < 48, looks unusual %s/%u",
                                       strnull(buf), pd_prefix_len);

                if (pd_prefix_len < 64) {
                        _cleanup_(route_freep) Route *route = NULL;

                        r = route_new(&route);
                        if (r < 0)
                                return r;

                        route->family = AF_INET6;
                        route->dst = pd_prefix;
                        route->dst_prefixlen = pd_prefix_len;
                        route->table = link_get_dhcp_route_table(link);
                        route->type = RTN_UNREACHABLE;

                        r = route_configure(route, link, dhcp6_route_handler);
                        if (r < 0) {
                                log_link_warning_errno(link, r, "Cannot configure unreachable route for delegated subnet %s/%u: %m",
                                                       strnull(buf),
                                                       pd_prefix_len);
                                continue;
                        }

                        log_link_debug(link, "Configuring unreachable route for %s/%u",
                                       strnull(buf), pd_prefix_len);
                } else
                        log_link_debug(link, "Not adding a blocking route since distributed prefix is /64");

                /* We are doing prefix allocation in two steps:
                 * 1. all those links that have a preferred subnet id will be assigned their subnet
                 * 2. all those links that remain will receive prefixes in sequential
                 *    order. Prefixes that were previously already allocated to another
                 *    link will be skipped.

                 * If a subnet id request couldn't be fulfilled the failure will be logged (as error)
                 * and no further attempts at obtaining a prefix will be made.

                 * The assignment has to be split in two phases since subnet id
                 * preferences should be honored. Meaning that any subnet id should be
                 * handed out to the requesting link and not to some link that didn't
                 * specify any preference. */

                r = dhcp6_pd_prefix_distribute(link, &pd_prefix.in6,
                                               pd_prefix_len,
                                               lifetime_preferred,
                                               lifetime_valid,
                                               true);
                if (r < 0 && r != -EAGAIN)
                        return r;

                /* if r == -EAGAIN then the allocation failed because we ran
                 * out of addresses for the preferred subnet id's. This doesn't
                 * mean we can't fulfill other prefix requests.
                 *
                 * Since we do not have dedicated lists of links that request
                 * specific subnet id's and those that accept any prefix we
                 * *must* reset the iterator to the start as otherwise some
                 * links might not get their requested prefix. */

                r = dhcp6_pd_prefix_distribute(link, &pd_prefix.in6,
                                               pd_prefix_len,
                                               lifetime_preferred,
                                               lifetime_valid,
                                               false);
                if (r < 0 && r != -EAGAIN)
                        return r;

                /* If the prefix distribution did return -EAGAIN we will try to
                 * fulfill those with the next available pd delegated prefix. */
        }

        return 0;
}

int dhcp6_request_prefix_delegation(Link *link) {
        Link *l;
        Iterator i;

        assert_return(link, -EINVAL);
        assert_return(link->manager, -EOPNOTSUPP);

        if (dhcp6_get_prefix_delegation(link) <= 0)
                return 0;

        log_link_debug(link, "Requesting DHCPv6 prefixes to be delegated for new link");

        HASHMAP_FOREACH(l, link->manager->links, i) {
                int r, enabled;

                if (l == link)
                        continue;

                if (!l->dhcp6_client)
                        continue;

                r = sd_dhcp6_client_get_prefix_delegation(l->dhcp6_client, &enabled);
                if (r < 0) {
                        log_link_warning_errno(l, r, "Cannot get prefix delegation when adding new link");
                        continue;
                }

                if (enabled == 0) {
                        r = sd_dhcp6_client_set_prefix_delegation(l->dhcp6_client, 1);
                        if (r < 0) {
                                log_link_warning_errno(l, r, "Cannot enable prefix delegation when adding new link");
                                continue;
                        }
                }

                r = sd_dhcp6_client_is_running(l->dhcp6_client);
                if (r <= 0)
                        continue;

                if (enabled != 0) {
                        log_link_debug(l, "Requesting re-assignment of delegated prefixes after adding new link");
                        (void) dhcp6_lease_pd_prefix_acquired(l->dhcp6_client, l);

                        continue;
                }

                r = sd_dhcp6_client_stop(l->dhcp6_client);
                if (r < 0) {
                        log_link_warning_errno(l, r, "Cannot stop DHCPv6 prefix delegation client after adding new link");
                        continue;
                }

                r = sd_dhcp6_client_start(l->dhcp6_client);
                if (r < 0) {
                        log_link_warning_errno(l, r, "Cannot restart DHCPv6 prefix delegation client after adding new link");
                        continue;
                }

                log_link_debug(l, "Restarted DHCPv6 client to acquire prefix delegations after adding new link");
        }

        return 0;
}

static int dhcp6_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
        int r;

        assert(link);

        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
                return 1;

        r = sd_netlink_message_get_errno(m);
        if (r < 0 && r != -EEXIST) {
                log_link_message_warning_errno(link, m, r, "Could not set DHCPv6 address");
                link_enter_failed(link);
                return 1;
        } else if (r >= 0)
                (void) manager_rtnl_process_address(rtnl, m, link->manager);

        r = link_request_set_routes(link);
        if (r < 0) {
                link_enter_failed(link);
                return 1;
        }

        return 1;
}

static int dhcp6_address_change(
                Link *link,
                struct in6_addr *ip6_addr,
                uint32_t lifetime_preferred,
                uint32_t lifetime_valid) {

        _cleanup_(address_freep) Address *addr = NULL;
        _cleanup_free_ char *buffer = NULL;
        int r;

        r = address_new(&addr);
        if (r < 0)
                return r;

        addr->family = AF_INET6;
        addr->in_addr.in6 = *ip6_addr;
        addr->flags = IFA_F_NOPREFIXROUTE;
        addr->prefixlen = 128;
        addr->cinfo.ifa_prefered = lifetime_preferred;
        addr->cinfo.ifa_valid = lifetime_valid;

        (void) in_addr_to_string(addr->family, &addr->in_addr, &buffer);
        log_link_info(link,
                      "DHCPv6 address %s/%d timeout preferred %d valid %d",
                      strnull(buffer), addr->prefixlen, lifetime_preferred, lifetime_valid);

        r = address_configure(addr, link, dhcp6_address_handler, true);
        if (r < 0)
                return log_link_warning_errno(link, r, "Could not assign DHCPv6 address: %m");

        return 0;
}

static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link) {
        int r;
        sd_dhcp6_lease *lease;
        struct in6_addr ip6_addr;
        uint32_t lifetime_preferred, lifetime_valid;

        r = sd_dhcp6_client_get_lease(client, &lease);
        if (r < 0)
                return r;

        sd_dhcp6_lease_reset_address_iter(lease);

        while (sd_dhcp6_lease_get_address(lease, &ip6_addr,
                                                 &lifetime_preferred,
                                                 &lifetime_valid) >= 0) {

                r = dhcp6_address_change(link, &ip6_addr, lifetime_preferred, lifetime_valid);
                if (r < 0)
                        return r;
        }

        return 0;
}

static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) {
        int r;
        Link *link = userdata;

        assert(link);
        assert(link->network);

        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
                return;

        switch(event) {
        case SD_DHCP6_CLIENT_EVENT_STOP:
        case SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE:
        case SD_DHCP6_CLIENT_EVENT_RETRANS_MAX:
                if (sd_dhcp6_client_get_lease(client, NULL) >= 0)
                        log_link_warning(link, "DHCPv6 lease lost");

                (void) dhcp6_lease_pd_prefix_lost(client, link);
                (void) dhcp6_prefix_remove_all(link->manager, link);

                link_dirty(link);
                link->dhcp6_configured = false;
                break;

        case SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE:
                r = dhcp6_lease_address_acquired(client, link);
                if (r < 0) {
                        link_enter_failed(link);
                        return;
                }

                r = dhcp6_lease_pd_prefix_acquired(client, link);
                if (r < 0)
                        log_link_debug(link, "DHCPv6 did not receive prefixes to delegate");

                _fallthrough_;
        case SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST:
                r = dhcp6_lease_information_acquired(client, link);
                if (r < 0) {
                        link_enter_failed(link);
                        return;
                }

                link_dirty(link);
                link->dhcp6_configured = true;
                break;

        default:
                if (event < 0)
                        log_link_warning_errno(link, event, "DHCPv6 error: %m");
                else
                        log_link_warning(link, "DHCPv6 unknown event: %d", event);
                return;
        }

        link_check_ready(link);
}

int dhcp6_request_address(Link *link, int ir) {
        int r, inf_req, pd;
        bool running;

        assert(link);
        assert(link->dhcp6_client);
        assert(link->network);
        assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0);

        r = sd_dhcp6_client_is_running(link->dhcp6_client);
        if (r < 0)
                return r;
        running = r;

        r = sd_dhcp6_client_get_prefix_delegation(link->dhcp6_client, &pd);
        if (r < 0)
                return r;

        if (pd && ir && link->network->dhcp6_force_pd_other_information) {
                log_link_debug(link, "Enabling managed mode to request DHCPv6 PD with 'Other Information' set");

                r = sd_dhcp6_client_set_address_request(link->dhcp6_client,
                                                        false);
                if (r < 0 )
                        return r;

                ir = false;
        }

        if (running) {
                r = sd_dhcp6_client_get_information_request(link->dhcp6_client, &inf_req);
                if (r < 0)
                        return r;

                if (inf_req == ir)
                        return 0;

                r = sd_dhcp6_client_stop(link->dhcp6_client);
                if (r < 0)
                        return r;
        } else {
                r = sd_dhcp6_client_set_local_address(link->dhcp6_client, &link->ipv6ll_address);
                if (r < 0)
                        return r;
        }

        r = sd_dhcp6_client_set_information_request(link->dhcp6_client, ir);
        if (r < 0)
                return r;

        r = sd_dhcp6_client_start(link->dhcp6_client);
        if (r < 0)
                return r;

        return 0;
}

static int dhcp6_set_hostname(sd_dhcp6_client *client, Link *link) {
        _cleanup_free_ char *hostname = NULL;
        const char *hn;
        int r;

        assert(link);

        if (!link->network->dhcp_send_hostname)
                hn = NULL;
        else if (link->network->dhcp_hostname)
                hn = link->network->dhcp_hostname;
        else {
                r = gethostname_strict(&hostname);
                if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
                        return r;

                hn = hostname;
        }

        r = sd_dhcp6_client_set_fqdn(client, hn);
        if (r == -EINVAL && hostname)
                /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
                log_link_warning_errno(link, r, "DHCP6 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
        else if (r < 0)
                return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set hostname: %m");

        return 0;
}

int dhcp6_configure(Link *link) {
        _cleanup_(sd_dhcp6_client_unrefp) sd_dhcp6_client *client = NULL;
        sd_dhcp6_option *send_option;
        void *request_options;
        const DUID *duid;
        Iterator i;
        int r;

        assert(link);
        assert(link->network);

        if (link->dhcp6_client)
                return 0;

        r = sd_dhcp6_client_new(&client);
        if (r == -ENOMEM)
                return log_oom();
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to create DHCP6 client: %m");

        r = sd_dhcp6_client_attach_event(client, NULL, 0);
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to attach event: %m");

        r = sd_dhcp6_client_set_mac(client,
                                    (const uint8_t *) &link->mac,
                                    sizeof (link->mac), ARPHRD_ETHER);
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set MAC address: %m");

        if (link->network->iaid_set) {
                r = sd_dhcp6_client_set_iaid(client, link->network->iaid);
                if (r < 0)
                        return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set IAID: %m");
        }

        duid = link_get_duid(link);
        if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
                r = sd_dhcp6_client_set_duid_llt(client, duid->llt_time);
        else
                r = sd_dhcp6_client_set_duid(client,
                                             duid->type,
                                             duid->raw_data_len > 0 ? duid->raw_data : NULL,
                                             duid->raw_data_len);
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set DUID: %m");

        ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp6_client_send_options, i) {
                r = sd_dhcp6_client_add_option(client, send_option);
                if (r == -EEXIST)
                        continue;
                if (r < 0)
                        return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set option: %m");
        }

        r = dhcp6_set_hostname(client, link);
        if (r < 0)
                return r;

        r = sd_dhcp6_client_set_ifindex(client, link->ifindex);
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set ifindex: %m");

        if (link->network->rapid_commit) {
                r = sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_RAPID_COMMIT);
                if (r < 0)
                        return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set request flag for rapid commit: %m");
        }

        if (link->network->dhcp6_mudurl) {
                r = sd_dhcp6_client_set_request_mud_url(client, link->network->dhcp6_mudurl);
                if (r < 0)
                        return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set MUD URL: %m");
        }

        SET_FOREACH(request_options, link->network->dhcp6_request_options, i) {
                uint32_t option = PTR_TO_UINT32(request_options);

                r = sd_dhcp6_client_set_request_option(client, option);
                if (r == -EEXIST) {
                        log_link_debug(link, "DHCP6 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option);
                        continue;
                }

                if (r < 0)
                        return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set request flag for '%u': %m", option);
        }

        if (link->network->dhcp6_user_class) {
                r = sd_dhcp6_client_set_request_user_class(client, link->network->dhcp6_user_class);
                if (r < 0)
                        return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set user class: %m");
        }

        if (link->network->dhcp6_vendor_class) {
                r = sd_dhcp6_client_set_request_vendor_class(client, link->network->dhcp6_vendor_class);
                if (r < 0)
                        return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set vendor class: %m");
        }

        r = sd_dhcp6_client_set_callback(client, dhcp6_handler, link);
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set callback: %m");

        if (dhcp6_enable_prefix_delegation(link)) {
                r = sd_dhcp6_client_set_prefix_delegation(client, true);
                if (r < 0)
                        return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set prefix delegation: %m");
        }

        if (link->network->dhcp6_pd_length > 0) {
                r = sd_dhcp6_client_set_prefix_delegation_hint(client, link->network->dhcp6_pd_length, &link->network->dhcp6_pd_address);
                if (r < 0)
                        return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set prefix hint: %m");
        }

        link->dhcp6_client = TAKE_PTR(client);

        return 0;
}

static Link *dhcp6_prefix_get(Manager *m, struct in6_addr *addr) {
        assert_return(m, NULL);
        assert_return(addr, NULL);

        return hashmap_get(m->dhcp6_prefixes, addr);
}

static int dhcp6_route_add_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
        int r;

        assert(link);

        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
                return 1;

        r = sd_netlink_message_get_errno(m);
        if (r < 0 && r != -EEXIST) {
                log_link_message_warning_errno(link, m, r, "Received error adding DHCPv6 Prefix Delegation route");
                link_enter_failed(link);
                return 1;
        }

        return 1;
}

static int dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) {
        _cleanup_(route_freep) Route *route = NULL;
        _cleanup_free_ struct in6_addr *a = NULL;
        _cleanup_free_ char *buf = NULL;
        Link *assigned_link;
        int r;

        assert_return(m, -EINVAL);
        assert_return(addr, -EINVAL);

        r = route_new(&route);
        if (r < 0)
                return r;

        route->family = AF_INET6;
        route->dst.in6 = *addr;
        route->dst_prefixlen = 64;

        r = route_configure(route, link, dhcp6_route_add_handler);
        if (r < 0)
                return r;

        (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf);
        log_link_debug(link, "Adding prefix route %s/64", strnull(buf));

        assigned_link = hashmap_get(m->dhcp6_prefixes, addr);
        if (assigned_link) {
                assert(assigned_link == link);
                return 0;
        }

        a = newdup(struct in6_addr, addr, 1);
        if (!a)
                return -ENOMEM;

        r = hashmap_ensure_allocated(&m->dhcp6_prefixes, &in6_addr_hash_ops);
        if (r < 0)
                return r;

        r = hashmap_put(m->dhcp6_prefixes, a, link);
        if (r < 0)
                return r;

        TAKE_PTR(a);
        link_ref(link);
        return 0;
}

static int dhcp6_prefix_remove_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
        int r;

        assert(link);

        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
                return 1;

        r = sd_netlink_message_get_errno(m);
        if (r < 0) {
                log_link_message_warning_errno(link, m, r, "Received error on DHCPv6 Prefix Delegation route removal");
                link_enter_failed(link);
                return 1;
        }

        return 1;
}

int dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) {
        _cleanup_free_ struct in6_addr *a = NULL;
        _cleanup_(link_unrefp) Link *l = NULL;
        _cleanup_(route_freep) Route *route = NULL;
        _cleanup_free_ char *buf = NULL;
        int r;

        assert_return(m, -EINVAL);
        assert_return(addr, -EINVAL);

        l = hashmap_remove2(m->dhcp6_prefixes, addr, (void **) &a);
        if (!l)
                return -EINVAL;

        (void) sd_radv_remove_prefix(l->radv, addr, 64);

        r = route_new(&route);
        if (r < 0)
                return r;

        route->family = AF_INET6;
        route->dst.in6 = *addr;
        route->dst_prefixlen = 64;

        r = route_remove(route, l, dhcp6_prefix_remove_handler);
        if (r < 0)
                return r;

        (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf);
        log_link_debug(l, "Removing prefix route %s/64", strnull(buf));

        return 0;
}

static int dhcp6_prefix_remove_all(Manager *m, Link *link) {
        struct in6_addr *addr;
        Iterator i;
        Link *l;

        assert_return(m, -EINVAL);
        assert_return(link, -EINVAL);

        HASHMAP_FOREACH_KEY(l, addr, m->dhcp6_prefixes, i)
                if (l == link)
                        (void) dhcp6_prefix_remove(m, addr);

        return 0;
}

static bool dhcp6_link_has_dhcpv6_prefix(Link *link) {
        Iterator i;
        Link *l;

        assert(link);
        assert(link->manager);

        HASHMAP_FOREACH(l, link->manager->dhcp6_prefixes, i)
                if (link == l)
                        return true;

        return false;
}

static int dhcp6_assign_delegated_prefix(Link *link,
                                         const struct in6_addr *prefix,
                                         uint8_t prefix_len,
                                         uint32_t lifetime_preferred,
                                         uint32_t lifetime_valid) {
        _cleanup_(address_freep) Address *address = NULL;
        int r;

        assert(link);
        assert(link->network);
        assert(prefix);

        if (!link->network->dhcp6_pd_assign_prefix)
                return 0;

        r = address_new(&address);
        if (r < 0)
                return log_link_error_errno(link, r, "Could not allocate address: %m");

        address->in_addr.in6 = *prefix;
        r = generate_ipv6_eui_64_address(link, &address->in_addr.in6);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to generate EUI64 address for DHCPv6 acquired delegated prefix: %m");

        address->prefixlen = prefix_len;
        address->family = AF_INET6;
        address->cinfo.ifa_prefered = lifetime_preferred;
        address->cinfo.ifa_valid = lifetime_valid;

        link_set_state(link, LINK_STATE_CONFIGURING);

        r = address_configure(address, link, address_handler, true);
        if (r < 0)
                return log_link_warning_errno(link, r, "Could not set addresses: %m");
        if (r > 0)
                link->address_messages++;

        return 0;
}