summaryrefslogtreecommitdiff
path: root/src/libsystemd-network/sd-dhcp6-client.c
blob: ef5fb44c4da049f981f3613e4bf7c271ce23d91e (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
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright (C) 2014 Intel Corporation. All rights reserved.

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>

#include "udev.h"
#include "udev-util.h"
#include "virt.h"
#include "siphash24.h"
#include "util.h"
#include "refcnt.h"

#include "network-internal.h"
#include "sd-dhcp6-client.h"
#include "dhcp6-protocol.h"
#include "dhcp6-internal.h"
#include "dhcp6-lease-internal.h"

#define SYSTEMD_PEN 43793
#define HASH_KEY SD_ID128_MAKE(80,11,8c,c2,fe,4a,03,ee,3e,d6,0c,6f,36,39,14,09)

struct sd_dhcp6_client {
        RefCount n_ref;

        enum DHCP6State state;
        sd_event *event;
        int event_priority;
        int index;
        struct ether_addr mac_addr;
        DHCP6IA ia_na;
        be32_t transaction_id;
        struct sd_dhcp6_lease *lease;
        int fd;
        sd_event_source *receive_message;
        usec_t retransmit_time;
        uint8_t retransmit_count;
        sd_event_source *timeout_resend;
        sd_event_source *timeout_resend_expire;
        sd_dhcp6_client_cb_t cb;
        void *userdata;

        struct duid_en {
                uint16_t type; /* DHCP6_DUID_EN */
                uint32_t pen;
                uint8_t id[8];
        } _packed_ duid;
};

const char * dhcp6_message_type_table[_DHCP6_MESSAGE_MAX] = {
        [DHCP6_SOLICIT] = "SOLICIT",
        [DHCP6_ADVERTISE] = "ADVERTISE",
        [DHCP6_REQUEST] = "REQUEST",
        [DHCP6_CONFIRM] = "CONFIRM",
        [DHCP6_RENEW] = "RENEW",
        [DHCP6_REBIND] = "REBIND",
        [DHCP6_REPLY] = "REPLY",
        [DHCP6_RELEASE] = "RELEASE",
        [DHCP6_DECLINE] = "DECLINE",
        [DHCP6_RECONFIGURE] = "RECONFIGURE",
        [DHCP6_INFORMATION_REQUEST] = "INFORMATION-REQUEST",
        [DHCP6_RELAY_FORW] = "RELAY-FORW",
        [DHCP6_RELAY_REPL] = "RELAY-REPL",
};

DEFINE_STRING_TABLE_LOOKUP(dhcp6_message_type, int);

const char * dhcp6_message_status_table[_DHCP6_STATUS_MAX] = {
        [DHCP6_STATUS_SUCCESS] = "Success",
        [DHCP6_STATUS_UNSPEC_FAIL] = "Unspecified failure",
        [DHCP6_STATUS_NO_ADDRS_AVAIL] = "No addresses available",
        [DHCP6_STATUS_NO_BINDING] = "Binding unavailable",
        [DHCP6_STATUS_NOT_ON_LINK] = "Not on link",
        [DHCP6_STATUS_USE_MULTICAST] = "Use multicast",
};

DEFINE_STRING_TABLE_LOOKUP(dhcp6_message_status, int);

static int client_start(sd_dhcp6_client *client, enum DHCP6State state);

int sd_dhcp6_client_set_callback(sd_dhcp6_client *client,
                                 sd_dhcp6_client_cb_t cb, void *userdata)
{
        assert_return(client, -EINVAL);

        client->cb = cb;
        client->userdata = userdata;

        return 0;
}

int sd_dhcp6_client_set_index(sd_dhcp6_client *client, int interface_index)
{
        assert_return(client, -EINVAL);
        assert_return(interface_index >= -1, -EINVAL);

        client->index = interface_index;

        return 0;
}

int sd_dhcp6_client_set_mac(sd_dhcp6_client *client,
                            const struct ether_addr *mac_addr)
{
        assert_return(client, -EINVAL);

        if (mac_addr)
                memcpy(&client->mac_addr, mac_addr, sizeof(client->mac_addr));
        else
                memset(&client->mac_addr, 0x00, sizeof(client->mac_addr));

        return 0;
}

int sd_dhcp6_client_get_lease(sd_dhcp6_client *client, sd_dhcp6_lease **ret) {
        assert_return(client, -EINVAL);
        assert_return(ret, -EINVAL);

        if (!client->lease)
                return -ENOMSG;

        *ret = sd_dhcp6_lease_ref(client->lease);

        return 0;
}

static sd_dhcp6_client *client_notify(sd_dhcp6_client *client, int event) {
        if (client->cb) {
                client = sd_dhcp6_client_ref(client);
                client->cb(client, event, client->userdata);
                client = sd_dhcp6_client_unref(client);
        }

        return client;
}

static int client_reset(sd_dhcp6_client *client) {
        assert_return(client, -EINVAL);

        client->receive_message =
                sd_event_source_unref(client->receive_message);

        client->fd = safe_close(client->fd);

        client->transaction_id = 0;

        client->ia_na.timeout_t1 =
                sd_event_source_unref(client->ia_na.timeout_t1);
        client->ia_na.timeout_t2 =
                sd_event_source_unref(client->ia_na.timeout_t2);

        client->retransmit_time = 0;
        client->retransmit_count = 0;
        client->timeout_resend = sd_event_source_unref(client->timeout_resend);
        client->timeout_resend_expire =
                sd_event_source_unref(client->timeout_resend_expire);

        client->state = DHCP6_STATE_STOPPED;

        return 0;
}

static sd_dhcp6_client *client_stop(sd_dhcp6_client *client, int error) {
        assert_return(client, NULL);

        client = client_notify(client, error);
        if (client)
                client_reset(client);

        return client;
}

static int client_send_message(sd_dhcp6_client *client) {
        _cleanup_free_ DHCP6Message *message = NULL;
        struct in6_addr all_servers =
                IN6ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS_INIT;
        size_t len, optlen = 512;
        uint8_t *opt;
        int r;

        len = sizeof(DHCP6Message) + optlen;

        message = malloc0(len);
        if (!message)
                return -ENOMEM;

        opt = (uint8_t *)(message + 1);

        message->transaction_id = client->transaction_id;

        switch(client->state) {
        case DHCP6_STATE_SOLICITATION:
                message->type = DHCP6_SOLICIT;

                r = dhcp6_option_append_ia(&opt, &optlen, &client->ia_na);
                if (r < 0)
                        return r;

                break;

        case DHCP6_STATE_REQUEST:
                message->type = DHCP6_REQUEST;

                r = dhcp6_option_append(&opt, &optlen, DHCP6_OPTION_SERVERID,
                                        client->lease->serverid_len,
                                        client->lease->serverid);
                if (r < 0)
                        return r;

                r = dhcp6_option_append_ia(&opt, &optlen, &client->lease->ia);
                if (r < 0)
                        return r;

                break;

        case DHCP6_STATE_STOPPED:
        case DHCP6_STATE_RS:
        case DHCP6_STATE_BOUND:
                return -EINVAL;
        }

        r = dhcp6_option_append(&opt, &optlen, DHCP6_OPTION_CLIENTID,
                                sizeof(client->duid), &client->duid);
        if (r < 0)
                return r;

        r = dhcp6_network_send_udp_socket(client->fd, &all_servers, message,
                                          len - optlen);
        if (r < 0)
                return r;

        log_dhcp6_client(client, "Sent %s",
                         dhcp6_message_type_to_string(message->type));

        return 0;
}

static int client_timeout_t2(sd_event_source *s, uint64_t usec,
                             void *userdata) {
        sd_dhcp6_client *client = userdata;

        assert_return(s, -EINVAL);
        assert_return(client, -EINVAL);
        assert_return(client->lease, -EINVAL);

        client->lease->ia.timeout_t2 =
                sd_event_source_unref(client->lease->ia.timeout_t2);

        log_dhcp6_client(client, "Timeout T2");

        return 0;
}

static int client_timeout_t1(sd_event_source *s, uint64_t usec,
                             void *userdata) {
        sd_dhcp6_client *client = userdata;

        assert_return(s, -EINVAL);
        assert_return(client, -EINVAL);
        assert_return(client->lease, -EINVAL);

        client->lease->ia.timeout_t1 =
                sd_event_source_unref(client->lease->ia.timeout_t1);

        log_dhcp6_client(client, "Timeout T1");

        return 0;
}

static int client_timeout_resend_expire(sd_event_source *s, uint64_t usec,
                                        void *userdata) {
        sd_dhcp6_client *client = userdata;

        assert(s);
        assert(client);
        assert(client->event);

        client_stop(client, DHCP6_EVENT_RESEND_EXPIRE);

        return 0;
}

static usec_t client_timeout_compute_random(usec_t val) {
        return val - val / 10 +
                (random_u32() % (2 * USEC_PER_SEC)) * val / 10 / USEC_PER_SEC;
}

static int client_timeout_resend(sd_event_source *s, uint64_t usec,
                                 void *userdata) {
        int r = 0;
        sd_dhcp6_client *client = userdata;
        usec_t time_now, init_retransmit_time, max_retransmit_time;
        usec_t max_retransmit_duration;
        uint8_t max_retransmit_count;
        char time_string[FORMAT_TIMESPAN_MAX];

        assert(s);
        assert(client);
        assert(client->event);

        client->timeout_resend = sd_event_source_unref(client->timeout_resend);

        switch (client->state) {
        case DHCP6_STATE_SOLICITATION:

                if (client->retransmit_count && client->lease) {
                        client_start(client, DHCP6_STATE_REQUEST);
                        return 0;
                }

                init_retransmit_time = DHCP6_SOL_TIMEOUT;
                max_retransmit_time = DHCP6_SOL_MAX_RT;
                max_retransmit_count = 0;
                max_retransmit_duration = 0;

                break;

        case DHCP6_STATE_REQUEST:
                init_retransmit_time = DHCP6_REQ_TIMEOUT;
                max_retransmit_time = DHCP6_REQ_MAX_RT;
                max_retransmit_count = DHCP6_REQ_MAX_RC;
                max_retransmit_duration = 0;

                break;

        case DHCP6_STATE_STOPPED:
        case DHCP6_STATE_RS:
        case DHCP6_STATE_BOUND:
                return 0;
        }

        if (max_retransmit_count &&
            client->retransmit_count >= max_retransmit_count) {
                client_stop(client, DHCP6_EVENT_RETRANS_MAX);
                return 0;
        }

        r = client_send_message(client);
        if (r >= 0)
                client->retransmit_count++;


        r = sd_event_now(client->event, CLOCK_MONOTONIC, &time_now);
        if (r < 0)
                goto error;

        if (!client->retransmit_time) {
                client->retransmit_time =
                        client_timeout_compute_random(init_retransmit_time);

                if (client->state == DHCP6_STATE_SOLICITATION)
                        client->retransmit_time += init_retransmit_time / 10;

        } else {
                if (max_retransmit_time &&
                    client->retransmit_time > max_retransmit_time / 2)
                        client->retransmit_time = client_timeout_compute_random(max_retransmit_time);
                else
                        client->retransmit_time += client_timeout_compute_random(client->retransmit_time);
        }

        log_dhcp6_client(client, "Next retransmission in %s",
                         format_timespan(time_string, FORMAT_TIMESPAN_MAX,
                                         client->retransmit_time, 0));

        r = sd_event_add_time(client->event, &client->timeout_resend,
                              CLOCK_MONOTONIC,
                              time_now + client->retransmit_time,
                              10 * USEC_PER_MSEC, client_timeout_resend,
                              client);
        if (r < 0)
                goto error;

        r = sd_event_source_set_priority(client->timeout_resend,
                                         client->event_priority);
        if (r < 0)
                goto error;

        if (max_retransmit_duration && !client->timeout_resend_expire) {

                log_dhcp6_client(client, "Max retransmission duration %"PRIu64" secs",
                                 max_retransmit_duration / USEC_PER_SEC);

                r = sd_event_add_time(client->event,
                                      &client->timeout_resend_expire,
                                      CLOCK_MONOTONIC,
                                      time_now + max_retransmit_duration,
                                      USEC_PER_SEC,
                                      client_timeout_resend_expire, client);
                if (r < 0)
                        goto error;

                r = sd_event_source_set_priority(client->timeout_resend_expire,
                                                 client->event_priority);
                if (r < 0)
                        goto error;
        }

error:
        if (r < 0)
                client_stop(client, r);

        return 0;
}

static int client_ensure_iaid(sd_dhcp6_client *client) {
        const char *name = NULL;
        uint64_t id;

        assert(client);

        if (client->ia_na.id)
                return 0;

        if (detect_container(NULL) <= 0) {
                /* not in a container, udev will be around */
                _cleanup_udev_unref_ struct udev *udev;
                _cleanup_udev_device_unref_ struct udev_device *device;
                char ifindex_str[2 + DECIMAL_STR_MAX(int)];

                udev = udev_new();
                if (!udev)
                        return -ENOMEM;

                sprintf(ifindex_str, "n%d", client->index);
                device = udev_device_new_from_device_id(udev, ifindex_str);
                if (!device)
                        return -errno;

                if (udev_device_get_is_initialized(device) <= 0)
                        /* not yet ready */
                        return -EBUSY;

                name = net_get_name(device);
        }

        if (name)
                siphash24((uint8_t*)&id, name, strlen(name), HASH_KEY.bytes);
        else
                /* fall back to mac address if no predictable name available */
                siphash24((uint8_t*)&id, &client->mac_addr, ETH_ALEN,
                          HASH_KEY.bytes);

        /* fold into 32 bits */
        client->ia_na.id = (id & 0xffffffff) ^ (id >> 32);

        return 0;
}

static int client_parse_message(sd_dhcp6_client *client,
                                DHCP6Message *message, size_t len,
                                sd_dhcp6_lease *lease) {
        int r;
        uint8_t *optval, *option = (uint8_t *)(message + 1), *id = NULL;
        uint16_t optcode, status;
        size_t optlen, id_len;
        bool clientid = false;
        be32_t iaid_lease;

        while ((r = dhcp6_option_parse(&option, &len, &optcode, &optlen,
                                       &optval)) >= 0) {
                switch (optcode) {
                case DHCP6_OPTION_CLIENTID:
                        if (clientid) {
                                log_dhcp6_client(client, "%s contains multiple clientids",
                                                 dhcp6_message_type_to_string(message->type));
                                return -EINVAL;
                        }

                        if (optlen != sizeof(client->duid) ||
                            memcmp(&client->duid, optval, optlen) != 0) {
                                log_dhcp6_client(client, "%s DUID does not match",
                                                 dhcp6_message_type_to_string(message->type));

                                return -EINVAL;
                        }
                        clientid = true;

                        break;

                case DHCP6_OPTION_SERVERID:
                        r = dhcp6_lease_get_serverid(lease, &id, &id_len);
                        if (r >= 0 && id) {
                                log_dhcp6_client(client, "%s contains multiple serverids",
                                                 dhcp6_message_type_to_string(message->type));
                                return -EINVAL;
                        }

                        r = dhcp6_lease_set_serverid(lease, optval, optlen);
                        if (r < 0)
                                return r;

                        break;

                case DHCP6_OPTION_PREFERENCE:
                        if (optlen != 1)
                                return -EINVAL;

                        r = dhcp6_lease_set_preference(lease, *optval);
                        if (r < 0)
                                return r;

                        break;

                case DHCP6_OPTION_STATUS_CODE:
                        if (optlen < 2)
                                return -EINVAL;

                        status = optval[0] << 8 | optval[1];
                        if (status) {
                                log_dhcp6_client(client, "%s Status %s",
                                                 dhcp6_message_type_to_string(message->type),
                                                 dhcp6_message_status_to_string(status));
                                return -EINVAL;
                        }

                        break;

                case DHCP6_OPTION_IA_NA:
                        r = dhcp6_option_parse_ia(&optval, &optlen, optcode,
                                                  &lease->ia);
                        if (r < 0 && r != -ENOMSG)
                                return r;

                        r = dhcp6_lease_get_iaid(lease, &iaid_lease);
                        if (r < 0)
                                return r;

                        if (client->ia_na.id != iaid_lease) {
                                log_dhcp6_client(client, "%s has wrong IAID",
                                                 dhcp6_message_type_to_string(message->type));
                                return -EINVAL;
                        }

                        break;
                }
        }

        if ((r < 0 && r != -ENOMSG) || !clientid) {
                log_dhcp6_client(client, "%s has incomplete options",
                                 dhcp6_message_type_to_string(message->type));
                return -EINVAL;
        }

        r = dhcp6_lease_get_serverid(lease, &id, &id_len);
        if (r < 0)
                log_dhcp6_client(client, "%s has no server id",
                                 dhcp6_message_type_to_string(message->type));

        return r;
}

static int client_receive_reply(sd_dhcp6_client *client, DHCP6Message *reply,
                                size_t len)
{
        int r;
        _cleanup_dhcp6_lease_free_ sd_dhcp6_lease *lease = NULL;

        if (reply->type != DHCP6_REPLY)
                return -EINVAL;

        r = dhcp6_lease_new(&lease);
        if (r < 0)
                return -ENOMEM;

        r = client_parse_message(client, reply, len, lease);
        if (r < 0)
                return r;

        dhcp6_lease_clear_timers(&client->lease->ia);

        client->lease = sd_dhcp6_lease_unref(client->lease);
        client->lease = lease;
        lease = NULL;

        return DHCP6_STATE_BOUND;
}

static int client_receive_advertise(sd_dhcp6_client *client,
                                    DHCP6Message *advertise, size_t len) {
        int r;
        _cleanup_dhcp6_lease_free_ sd_dhcp6_lease *lease = NULL;
        uint8_t pref_advertise = 0, pref_lease = 0;

        if (advertise->type != DHCP6_ADVERTISE)
                return -EINVAL;

        r = dhcp6_lease_new(&lease);
        if (r < 0)
                return r;

        r = client_parse_message(client, advertise, len, lease);
        if (r < 0)
                return r;

        r = dhcp6_lease_get_preference(lease, &pref_advertise);
        if (r < 0)
                return r;

        r = dhcp6_lease_get_preference(client->lease, &pref_lease);
        if (!client->lease || r < 0 || pref_advertise > pref_lease) {
                sd_dhcp6_lease_unref(client->lease);
                client->lease = lease;
                lease = NULL;
                r = 0;
        }

        if (pref_advertise == 255 || client->retransmit_count > 1)
                r = DHCP6_STATE_REQUEST;

        return r;
}

static int client_receive_message(sd_event_source *s, int fd, uint32_t revents,
                                  void *userdata) {
        sd_dhcp6_client *client = userdata;
        _cleanup_free_ DHCP6Message *message;
        int r, buflen, len;

        assert(s);
        assert(client);
        assert(client->event);

        r = ioctl(fd, FIONREAD, &buflen);
        if (r < 0 || buflen <= 0)
                buflen = DHCP6_MIN_OPTIONS_SIZE;

        message = malloc0(buflen);
        if (!message)
                return -ENOMEM;

        len = read(fd, message, buflen);
        if ((size_t)len < sizeof(DHCP6Message)) {
                log_dhcp6_client(client, "could not receive message from UDP socket: %s", strerror(errno));
                return 0;
        }

        switch(message->type) {
        case DHCP6_SOLICIT:
        case DHCP6_REQUEST:
        case DHCP6_CONFIRM:
        case DHCP6_RENEW:
        case DHCP6_REBIND:
        case DHCP6_RELEASE:
        case DHCP6_DECLINE:
        case DHCP6_INFORMATION_REQUEST:
        case DHCP6_RELAY_FORW:
        case DHCP6_RELAY_REPL:
                return 0;

        case DHCP6_ADVERTISE:
        case DHCP6_REPLY:
        case DHCP6_RECONFIGURE:
                break;

        default:
                log_dhcp6_client(client, "unknown message type %d",
                                 message->type);
                return 0;
        }

        if (client->transaction_id != (message->transaction_id &
                                       htobe32(0x00ffffff)))
                return 0;

        switch (client->state) {
        case DHCP6_STATE_SOLICITATION:
                r = client_receive_advertise(client, message, len);

                if (r == DHCP6_STATE_REQUEST)
                        client_start(client, r);

                break;

        case DHCP6_STATE_REQUEST:
                r = client_receive_reply(client, message, len);
                if (r < 0)
                        return 0;

                if (r == DHCP6_STATE_BOUND) {

                        r = client_start(client, DHCP6_STATE_BOUND);
                        if (r < 0) {
                                client_stop(client, r);
                                return 0;
                        }

                        client = client_notify(client, DHCP6_EVENT_IP_ACQUIRE);
                        if (!client)
                                return 0;
                }

                break;

        case DHCP6_STATE_BOUND:

                break;

        case DHCP6_STATE_STOPPED:
        case DHCP6_STATE_RS:
                return 0;
        }

        if (r >= 0) {
                log_dhcp6_client(client, "Recv %s",
                                 dhcp6_message_type_to_string(message->type));
        }

        return 0;
}

static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
{
        int r;
        usec_t timeout, time_now;
        char time_string[FORMAT_TIMESPAN_MAX];

        assert_return(client, -EINVAL);
        assert_return(client->event, -EINVAL);
        assert_return(client->index > 0, -EINVAL);
        assert_return(client->state != state, -EINVAL);

        client->timeout_resend_expire =
                sd_event_source_unref(client->timeout_resend_expire);
        client->timeout_resend = sd_event_source_unref(client->timeout_resend);
        client->retransmit_time = 0;
        client->retransmit_count = 0;

        switch (state) {
        case DHCP6_STATE_STOPPED:
        case DHCP6_STATE_RS:
        case DHCP6_STATE_SOLICITATION:

                r = client_ensure_iaid(client);
                if (r < 0)
                        return r;

                r = dhcp6_network_bind_udp_socket(client->index, NULL);
                if (r < 0)
                        return r;

                client->fd = r;

                r = sd_event_add_io(client->event, &client->receive_message,
                                    client->fd, EPOLLIN, client_receive_message,
                                    client);
                if (r < 0)
                        return r;

                r = sd_event_source_set_priority(client->receive_message,
                                                 client->event_priority);
                if (r < 0)
                        return r;

                client->state = DHCP6_STATE_SOLICITATION;

                break;

        case DHCP6_STATE_REQUEST:

                client->state = state;

                break;

        case DHCP6_STATE_BOUND:

                r = sd_event_now(client->event, CLOCK_MONOTONIC, &time_now);
                if (r < 0)
                        return r;

                if (client->lease->ia.lifetime_t1 == 0xffffffff ||
                    client->lease->ia.lifetime_t2 == 0xffffffff) {

                        log_dhcp6_client(client, "infinite T1 0x%08x or T2 0x%08x",
                                         be32toh(client->lease->ia.lifetime_t1),
                                         be32toh(client->lease->ia.lifetime_t2));

                        return 0;
                }

                timeout = client_timeout_compute_random(be32toh(client->lease->ia.lifetime_t1) * USEC_PER_SEC);

                log_dhcp6_client(client, "T1 expires in %s",
                                 format_timespan(time_string,
                                                 FORMAT_TIMESPAN_MAX,
                                                 timeout, 0));

                r = sd_event_add_time(client->event,
                                      &client->lease->ia.timeout_t1,
                                      CLOCK_MONOTONIC, time_now + timeout,
                                      10 * USEC_PER_SEC, client_timeout_t1,
                                      client);
                if (r < 0)
                        return r;

                r = sd_event_source_set_priority(client->lease->ia.timeout_t1,
                                                 client->event_priority);
                if (r < 0)
                        return r;

                timeout = client_timeout_compute_random(be32toh(client->lease->ia.lifetime_t2) * USEC_PER_SEC);

                log_dhcp6_client(client, "T2 expires in %s",
                                 format_timespan(time_string,
                                                 FORMAT_TIMESPAN_MAX,
                                                 timeout, 0));

                r = sd_event_add_time(client->event,
                                      &client->lease->ia.timeout_t2,
                                      CLOCK_MONOTONIC, time_now + timeout,
                                      10 * USEC_PER_SEC, client_timeout_t2,
                                      client);
                if (r < 0)
                        return r;

                r = sd_event_source_set_priority(client->lease->ia.timeout_t2,
                                                 client->event_priority);
                if (r < 0)
                        return r;

                return 0;
        }

        client->transaction_id = random_u32() & htobe32(0x00ffffff);

        r = sd_event_add_time(client->event, &client->timeout_resend,
                              CLOCK_MONOTONIC, 0, 0, client_timeout_resend,
                              client);
        if (r < 0)
                return r;

        r = sd_event_source_set_priority(client->timeout_resend,
                                         client->event_priority);
        if (r < 0)
                return r;

        return 0;
}

int sd_dhcp6_client_stop(sd_dhcp6_client *client)
{
        client_stop(client, DHCP6_EVENT_STOP);

        return 0;
}

int sd_dhcp6_client_start(sd_dhcp6_client *client)
{
        int r = 0;

        assert_return(client, -EINVAL);
        assert_return(client->event, -EINVAL);
        assert_return(client->index > 0, -EINVAL);

        r = client_reset(client);
        if (r < 0)
                return r;

        return client_start(client, DHCP6_STATE_SOLICITATION);
}

int sd_dhcp6_client_attach_event(sd_dhcp6_client *client, sd_event *event,
                                 int priority)
{
        int r;

        assert_return(client, -EINVAL);
        assert_return(!client->event, -EBUSY);

        if (event)
                client->event = sd_event_ref(event);
        else {
                r = sd_event_default(&client->event);
                if (r < 0)
                        return 0;
        }

        client->event_priority = priority;

        return 0;
}

int sd_dhcp6_client_detach_event(sd_dhcp6_client *client) {
        assert_return(client, -EINVAL);

        client->event = sd_event_unref(client->event);

        return 0;
}

sd_event *sd_dhcp6_client_get_event(sd_dhcp6_client *client) {
        if (!client)
                return NULL;

        return client->event;
}

sd_dhcp6_client *sd_dhcp6_client_ref(sd_dhcp6_client *client) {
        if (client)
                assert_se(REFCNT_INC(client->n_ref) >= 2);

        return client;
}

sd_dhcp6_client *sd_dhcp6_client_unref(sd_dhcp6_client *client) {
        if (client && REFCNT_DEC(client->n_ref) <= 0) {
                client_reset(client);

                sd_dhcp6_client_detach_event(client);

                free(client);

                return NULL;
        }

        return client;
}

DEFINE_TRIVIAL_CLEANUP_FUNC(sd_dhcp6_client*, sd_dhcp6_client_unref);
#define _cleanup_dhcp6_client_free_ _cleanup_(sd_dhcp6_client_unrefp)

int sd_dhcp6_client_new(sd_dhcp6_client **ret)
{
        _cleanup_dhcp6_client_free_ sd_dhcp6_client *client = NULL;
        sd_id128_t machine_id;
        int r;

        assert_return(ret, -EINVAL);

        client = new0(sd_dhcp6_client, 1);
        if (!client)
                return -ENOMEM;

        client->n_ref = REFCNT_INIT;

        client->ia_na.type = DHCP6_OPTION_IA_NA;

        client->index = -1;

        client->fd = -1;

        /* initialize DUID */
        client->duid.type = htobe16(DHCP6_DUID_EN);
        client->duid.pen = htobe32(SYSTEMD_PEN);

        r = sd_id128_get_machine(&machine_id);
        if (r < 0)
                return r;

        /* a bit of snake-oil perhaps, but no need to expose the machine-id
           directly */
        siphash24(client->duid.id, &machine_id, sizeof(machine_id),
                  HASH_KEY.bytes);

        *ret = client;
        client = NULL;

        return 0;
}