-
Notifications
You must be signed in to change notification settings - Fork 50
/
fsm.c
2742 lines (2302 loc) · 74 KB
/
fsm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DHCP6 supplicant -- finite client state machine.
*
* Copyright (C) 2010-2012 Olaf Kirch <[email protected]>
* Copyright (C) 2012 Marius Tomaschewski <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses/> or write
* to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <time.h>
#include <sys/time.h>
#include <wicked/logging.h>
#include <wicked/resolver.h>
#include "dhcp6/dhcp6.h"
#include "dhcp6/device.h"
#include "dhcp6/options.h"
#include "dhcp6/protocol.h"
#include "dhcp6/fsm.h"
#include "duid.h"
static void ni_dhcp6_fsm_timeout(ni_dhcp6_device_t *);
static void ni_dhcp6_fsm_fail_timeout(ni_dhcp6_device_t *);
static void __ni_dhcp6_fsm_timeout(void *, const ni_timer_t *);
static void ni_dhcp6_fsm_timer_cancel(ni_dhcp6_device_t *);
static int ni_dhcp6_fsm_solicit (ni_dhcp6_device_t *);
static int __ni_dhcp6_fsm_release (ni_dhcp6_device_t *, unsigned int);
static int ni_dhcp6_fsm_request_lease(ni_dhcp6_device_t *, const ni_addrconf_lease_t *);
static int ni_dhcp6_fsm_confirm_lease(ni_dhcp6_device_t *, const ni_addrconf_lease_t *);
static int ni_dhcp6_fsm_confirm_prefix(ni_dhcp6_device_t *, const ni_addrconf_lease_t *);
static int ni_dhcp6_fsm_confirm_address(ni_dhcp6_device_t *, const ni_addrconf_lease_t *);
static int ni_dhcp6_fsm_renew(ni_dhcp6_device_t *);
static int ni_dhcp6_fsm_rebind(ni_dhcp6_device_t *);
static int ni_dhcp6_fsm_decline(ni_dhcp6_device_t *);
static int ni_dhcp6_fsm_request_info (ni_dhcp6_device_t *);
static int ni_dhcp6_fsm_accept_offer(ni_dhcp6_device_t *dev);
static int ni_dhcp6_fsm_commit_lease (ni_dhcp6_device_t *, ni_addrconf_lease_t *);
static int ni_dhcp6_fsm_bound(ni_dhcp6_device_t *);
static unsigned int ni_dhcp6_fsm_get_renewal_timeout(ni_dhcp6_device_t *);
static unsigned int ni_dhcp6_fsm_get_rebind_timeout(ni_dhcp6_device_t *);
static unsigned int ni_dhcp6_fsm_get_expire_timeout(ni_dhcp6_device_t *);
static unsigned int ni_dhcp6_fsm_mark_renew_ia(ni_dhcp6_device_t *);
static unsigned int ni_dhcp6_fsm_mark_rebind_ia(ni_dhcp6_device_t *);
static void ni_dhcp6_send_event(enum ni_dhcp6_event, const ni_dhcp6_device_t *, ni_addrconf_lease_t *);
static int ni_dhcp6_fsm_parse_client_options(ni_dhcp6_device_t *, ni_dhcp6_message_t *, ni_buffer_t *);
/*
* How long to wait until an address is ready to use
* For now we use 2 sec: DAD default of 1 sec + time
* needed to send to wicked and apply to the system...
*/
#define NI_DHCP6_WAIT_IAADDR_READY 2000
/*
* Global fsm handler
*/
static ni_dhcp6_event_handler_t *ni_dhcp6_fsm_event_handler;
static ni_bool_t
ni_dhcp6_lease_with_active_address(const ni_addrconf_lease_t *lease)
{
struct timeval now;
if (!ni_addrconf_lease_is_valid(lease))
return FALSE;
if (lease->type != NI_ADDRCONF_DHCP || lease->family != AF_INET6)
return FALSE;
if (ni_timer_get_time(&now) < 0)
return FALSE;
return ni_dhcp6_ia_list_count_active(lease->dhcp6.ia_list, &now) > 0;
}
struct ni_dhcp6_ia_addr_counts {
unsigned int pd;
unsigned int na;
unsigned int ta;
};
static void
ni_dhcp6_fsm_count_iadr_type(unsigned int type, struct ni_dhcp6_ia_addr_counts *counts)
{
if (!counts)
return;
switch (type) {
case NI_DHCP6_OPTION_IA_PD:
counts->pd++;
break;
case NI_DHCP6_OPTION_IA_NA:
counts->na++;
break;
case NI_DHCP6_OPTION_IA_TA:
counts->ta++;
break;
}
}
static unsigned int
ni_dhcp6_fsm_count_lease_iadrs(ni_dhcp6_device_t *dev, const ni_addrconf_lease_t *lease,
struct ni_dhcp6_ia_addr_counts *usable,
struct ni_dhcp6_ia_addr_counts *expired)
{
const ni_dhcp6_ia_addr_t *iadr;
const ni_dhcp6_ia_t *ia;
struct timeval now;
unsigned int total = 0;
if (usable)
memset(usable, 0, sizeof(*usable));
if (expired)
memset(expired, 0, sizeof(*expired));
if (!dev || !lease || !lease->dhcp6.ia_list)
return 0;
if (ni_timer_get_time(&now) < 0)
return 0;
for (ia = lease->dhcp6.ia_list; ia; ia = ia->next) {
if (ia->status.code != NI_DHCP6_STATUS_SUCCESS)
continue;
for (iadr = ia->addrs; iadr; iadr = iadr->next) {
if (ia->status.code != NI_DHCP6_STATUS_SUCCESS)
continue;
if (!ni_dhcp6_ia_addr_is_usable(iadr))
continue;
if (ni_dhcp6_ia_addr_valid_lft(iadr, &ia->acquired, &now))
ni_dhcp6_fsm_count_iadr_type(ia->type, usable);
else
ni_dhcp6_fsm_count_iadr_type(ia->type, expired);
}
}
if (usable) {
if (dev->config->mode & NI_BIT(NI_DHCP6_MODE_PREFIX))
total += usable->pd;
if (dev->config->mode & NI_BIT(NI_DHCP6_MODE_MANAGED))
total += usable->na;
}
if (expired) {
if (dev->config->mode & NI_BIT(NI_DHCP6_MODE_PREFIX))
total += expired->pd;
if (dev->config->mode & NI_BIT(NI_DHCP6_MODE_MANAGED))
total += expired->na;
}
return total;
}
static unsigned int
ni_dhcp6_fsm_show_lease_iaddrs(ni_dhcp6_device_t *dev, ni_addrconf_lease_t *lease,
void (*show)(const char *, ...), const char *fmt, ...)
{
ni_stringbuf_t msg = NI_STRINGBUF_INIT_DYNAMIC;
const ni_dhcp6_ia_addr_t *iadr;
const ni_dhcp6_ia_t *ia;
unsigned int total = 0;
struct timeval now;
va_list ap;
if (!dev || !lease || !show)
return 0;
va_start(ap, fmt);
if (fmt)
ni_stringbuf_vprintf(&msg, fmt, ap);
va_end(ap);
if (ni_timer_get_time(&now) < 0)
return 0;
for (ia = lease->dhcp6.ia_list; ia; ia = ia->next) {
if (ia->status.code != NI_DHCP6_STATUS_SUCCESS)
continue;
for (iadr = ia->addrs; iadr; iadr = iadr->next) {
unsigned int valid_lft;
unsigned int pref_lft;
ni_sockaddr_t addr;
if (ia->status.code != NI_DHCP6_STATUS_SUCCESS)
continue;
if (!ni_dhcp6_ia_addr_is_usable(iadr))
continue;
pref_lft = ni_dhcp6_ia_addr_preferred_lft(iadr, &ia->acquired, &now);
valid_lft = ni_dhcp6_ia_addr_valid_lft(iadr, &ia->acquired, &now);
if (!ni_string_empty(msg.string)) {
show("%s", msg.string);
ni_stringbuf_clear(&msg);
}
total++;
ni_sockaddr_set_ipv6(&addr, iadr->addr, 0);
ni_stringbuf_printf(&msg, "%s %s%s.%s %s/%u, pref-lft %u, valid-lft %u",
dev->ifname, valid_lft ? "+" : "-",
ni_dhcp6_option_name(ia->type),
ni_dhcp6_ia_type_pd(ia) ? "prefix" : "address",
ni_sockaddr_print(&addr), iadr->plen,
pref_lft, valid_lft);
if (ni_dhcp6_ia_type_pd(ia) && iadr->excl) {
ni_sockaddr_set_ipv6(&addr, iadr->excl->addr, 0);
ni_stringbuf_printf(&msg, ", exclusion %s/%u",
ni_sockaddr_print(&addr), iadr->excl->plen);
}
if (!ni_string_empty(msg.string))
show("%s", msg.string);
ni_stringbuf_clear(&msg);
}
}
return total;
}
static void
ni_dhcp6_fsm_show_lease_ia_status(ni_dhcp6_device_t *dev, ni_addrconf_lease_t *lease,
void (*show)(const char *, ...))
{
const ni_dhcp6_ia_addr_t *iadr;
const ni_dhcp6_ia_t *ia;
const char *msg;
if (!dev || !lease)
return;
for (ia = lease->dhcp6.ia_list; ia; ia = ia->next) {
if (ia->status.code != NI_DHCP6_STATUS_SUCCESS) {
msg = ni_dhcp6_status_message(&ia->status);
show("%s: %s status %s%s%s", dev->ifname,
ni_dhcp6_option_name(ia->type),
ni_dhcp6_status_name(ia->status.code),
msg ? ": " : "", msg ? msg : "");
} else
for (iadr = ia->addrs; iadr; iadr = iadr->next) {
if (ia->status.code != NI_DHCP6_STATUS_SUCCESS) {
msg = ni_dhcp6_status_message(&ia->status);
show("%s: %s status %s%s%s", dev->ifname,
ni_dhcp6_option_name(ia->type),
ni_dhcp6_status_name(ia->status.code),
msg ? ": " : "", msg ? msg : "");
}
}
}
}
int
ni_dhcp6_fsm_start(ni_dhcp6_device_t *dev)
{
ni_stringbuf_t buf = NI_STRINGBUF_INIT_DYNAMIC;
if (!dev->config) {
ni_error("%s: cannot start fsm without configuration", dev->ifname);
return -1;
}
if (dev->config->mode & NI_BIT(NI_DHCP6_MODE_INFO)) {
ni_dhcp6_fsm_reset(dev);
ni_info("%s: fsm start in mode %s", dev->ifname,
ni_dhcp6_mode_format(&buf, dev->config->mode, NULL));
ni_stringbuf_destroy(&buf);
if (ni_dhcp6_lease_with_active_address(dev->lease)) {
return __ni_dhcp6_fsm_release(dev, 0);
}
ni_dhcp6_device_drop_lease(dev);
return ni_dhcp6_fsm_request_info(dev);
} else
if ((dev->config->mode & NI_BIT(NI_DHCP6_MODE_PREFIX)) ||
(dev->config->mode & NI_BIT(NI_DHCP6_MODE_MANAGED))) {
int ret;
ni_dhcp6_fsm_reset(dev);
ni_info("%s: fsm start in mode %s", dev->ifname,
ni_dhcp6_mode_format(&buf, dev->config->mode, NULL));
ni_stringbuf_destroy(&buf);
if (!(ret = ni_dhcp6_fsm_confirm_lease(dev, dev->lease)))
return ret;
ni_dhcp6_device_drop_lease(dev);
return ni_dhcp6_fsm_solicit(dev);
} else
if (dev->config->mode & NI_BIT(NI_DHCP6_MODE_AUTO)) {
/* this function should not be called in pure mode=auto
* and we should not arrive here, thus warn about ... */
ni_warn("%s: cannot start fsm in mode %s without IPv6 router RA",
dev->ifname,
ni_dhcp6_mode_format(&buf, dev->config->mode, NULL));
ni_stringbuf_destroy(&buf);
/* return without to report failure via dbus and wait until timeout
* before we defer ("maybe we get RA leter") or fail to acquire the
* lease -- as advised in config.
*/
return 1;
} else
if (dev->config->mode) {
ni_error("%s: cannot start fsm with invalid mode 0x%x configuration",
dev->ifname, dev->config->mode);
return -1;
} else {
ni_note("%s: DHCPv6 is disabled by IPv6 router RA", dev->ifname);
/* we have received an RA that disables DHCPv6, so we're done.
* It's useless to wait until defer/acquire timeout before we
* report it to the callers (nanny, ifup). Report it now:
*/
if (dev->fsm.fail_on_timeout)
ni_dhcp6_send_event(NI_DHCP6_EVENT_LOST, dev, NULL);
else
ni_dhcp6_send_event(NI_DHCP6_EVENT_DEFERRED, dev, NULL);
ni_dhcp6_fsm_reset(dev);
/* Reapply mode=auto continue to monitor RA for an "upgrade" (router
* reconfiguration) and return without to trigger a dbus failure. */
dev->config->mode |= NI_DHCP6_MODE_AUTO;
return 1;
}
}
void
ni_dhcp6_fsm_reset(ni_dhcp6_device_t *dev)
{
dev->fsm.state = NI_DHCP6_STATE_INIT;
ni_dhcp6_fsm_timer_cancel(dev);
ni_dhcp6_device_retransmit_disarm(dev);
/* device? It is temporary fsm data */
ni_dhcp6_device_drop_best_offer(dev);
}
static int
ni_dhcp6_fsm_restart(ni_dhcp6_device_t *dev)
{
ni_dhcp6_fsm_reset(dev);
return ni_dhcp6_fsm_start(dev);
}
int
ni_dhcp6_fsm_retransmit(ni_dhcp6_device_t *dev)
{
switch (dev->fsm.state) {
case NI_DHCP6_STATE_SELECTING:
return ni_dhcp6_fsm_solicit(dev);
case NI_DHCP6_STATE_REQUESTING:
return ni_dhcp6_fsm_request_lease(dev, dev->best_offer.lease);
case NI_DHCP6_STATE_CONFIRMING:
return ni_dhcp6_fsm_confirm_address(dev, dev->lease);
case NI_DHCP6_STATE_RENEWING:
return ni_dhcp6_fsm_renew(dev);
break;
case NI_DHCP6_STATE_REBINDING:
return ni_dhcp6_fsm_rebind(dev);
break;
case NI_DHCP6_STATE_DECLINING:
return ni_dhcp6_fsm_decline(dev);
break;
case NI_DHCP6_STATE_RELEASING:
return ni_dhcp6_fsm_release(dev);
break;
case NI_DHCP6_STATE_REQUESTING_INFO:
return ni_dhcp6_fsm_request_info(dev);
default:
return -1;
}
}
int
ni_dhcp6_fsm_retransmit_end(ni_dhcp6_device_t *dev)
{
switch (dev->fsm.state) {
case NI_DHCP6_STATE_RELEASING:
ni_dhcp6_fsm_commit_lease(dev, NULL);
ni_dhcp6_device_stop(dev);
return 0;
default:
return -1;
}
}
void
ni_dhcp6_fsm_set_timeout_msec(ni_dhcp6_device_t *dev, ni_timeout_t msec)
{
if (msec != 0) {
ni_debug_dhcp("%s: setting fsm timeout to %u.%03u sec", dev->ifname,
NI_TIMEOUT_SEC(msec), NI_TIMEOUT_MSEC(msec));
if (dev->fsm.timer) {
ni_timer_rearm(dev->fsm.timer, msec);
} else {
dev->fsm.timer = ni_timer_register(msec, __ni_dhcp6_fsm_timeout, dev);
}
} else if (dev->fsm.timer) {
ni_timer_cancel(dev->fsm.timer);
dev->fsm.timer = NULL;
}
}
void
ni_dhcp6_fsm_set_timeout_sec(ni_dhcp6_device_t *dev, unsigned int seconds)
{
ni_dhcp6_fsm_set_timeout_msec(dev, NI_TIMEOUT_FROM_SEC(seconds));
}
static void
ni_dhcp6_fsm_timer_cancel(ni_dhcp6_device_t *dev)
{
dev->fsm.fail_on_timeout = 0;
if (dev->fsm.timer) {
ni_timer_cancel(dev->fsm.timer);
dev->fsm.timer = NULL;
}
}
static void
__ni_dhcp6_fsm_timeout(void *user_data, const ni_timer_t *timer)
{
ni_dhcp6_device_t *dev = user_data;
if (dev->fsm.timer != timer) {
ni_warn("%s: bad timer handle", __func__);
return;
}
dev->fsm.timer = NULL;
ni_dhcp6_fsm_timeout(dev);
}
static void
__show_remaining_timeouts(ni_dhcp6_device_t *dev, const char *info)
{
if (dev->config->defer_timeout) {
ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP,
"%s: %s in state %s, remaining defer timeout: %u of %u sec",
dev->ifname, info,
ni_dhcp6_fsm_state_name(dev->fsm.state),
ni_lifetime_left(dev->config->defer_timeout,
&dev->start_time, NULL),
dev->config->defer_timeout);
}
if (dev->config->acquire_timeout) {
ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP,
"%s: %s in state %s, remaining acquire timeout: %u of %u sec",
dev->ifname, info,
ni_dhcp6_fsm_state_name(dev->fsm.state),
ni_lifetime_left(dev->config->acquire_timeout,
&dev->start_time, NULL),
dev->config->acquire_timeout);
}
}
static void
ni_dhcp6_fsm_fail_timeout(ni_dhcp6_device_t *dev)
{
switch (dev->fsm.state) {
case NI_DHCP6_STATE_INIT:
/* acquire timeout was specified and is over;
* no RA which would arm info/managed mode.
*/
__show_remaining_timeouts(dev, "FAILURE");
ni_dhcp6_send_event(NI_DHCP6_EVENT_LOST, dev, NULL);
ni_dhcp6_device_drop_best_offer(dev);
ni_dhcp6_device_drop_lease(dev);
break;
case NI_DHCP6_STATE_SELECTING:
case NI_DHCP6_STATE_REQUESTING_INFO:
/* acquire timeout was specified and is over;
* no usable dhcp server response received.
*/
__show_remaining_timeouts(dev, "FAILURE");
/* Hmm... can this happen somehow? IMO useless */
if (ni_dhcp6_fsm_accept_offer(dev) == 0)
return;
ni_dhcp6_send_event(NI_DHCP6_EVENT_LOST, dev, NULL);
ni_dhcp6_device_drop_best_offer(dev);
ni_dhcp6_device_drop_lease(dev);
break;
default:
break;
}
ni_dhcp6_device_stop(dev);
}
static void
ni_dhcp6_fsm_timeout(ni_dhcp6_device_t *dev)
{
if (dev->retrans.delay) {
ni_debug_dhcp("%s: starting to transmit after initial delay",
dev->ifname);
dev->retrans.delay = 0;
ni_dhcp6_device_transmit_start(dev);
return;
}
ni_debug_dhcp("%s: timeout in state %s%s",
dev->ifname, ni_dhcp6_fsm_state_name(dev->fsm.state),
dev->fsm.fail_on_timeout ? " (failure)" : "");
if (dev->fsm.fail_on_timeout) {
dev->fsm.fail_on_timeout = 0;
ni_dhcp6_fsm_fail_timeout(dev);
return;
}
switch (dev->fsm.state) {
case NI_DHCP6_STATE_INIT:
/*
* defer timeout was specified and is over;
* no RA which would arm info/managed mode.
*/
__show_remaining_timeouts(dev, "TIMEOUT");
if (dev->config->defer_timeout) {
unsigned int deadline;
/* Do we still need this safeguard? */
deadline = ni_lifetime_left(dev->config->defer_timeout,
&dev->start_time, NULL);
if (deadline) {
ni_dhcp6_fsm_set_timeout_sec(dev, deadline);
dev->fsm.fail_on_timeout = 0;
return;
}
}
ni_dhcp6_send_event(NI_DHCP6_EVENT_DEFERRED, dev, NULL);
if (dev->config->acquire_timeout) {
unsigned int deadline;
deadline = ni_lifetime_left(dev->config->acquire_timeout,
&dev->start_time, NULL);
if (deadline) {
ni_dhcp6_fsm_set_timeout_sec(dev, deadline);
dev->fsm.fail_on_timeout = 1;
return;
}
} /* infinite timeout, just continue monitoring */
break;
case NI_DHCP6_STATE_SELECTING:
case NI_DHCP6_STATE_REQUESTING_INFO:
/*
* defer timeout was specified and is over;
* no usable dhcp server response received
* [or no address in managed mode offers
* and we've discarded them (RFC MUST)].
*/
__show_remaining_timeouts(dev, "TIMEOUT");
if (ni_dhcp6_fsm_accept_offer(dev) == 0)
return;
ni_dhcp6_send_event(NI_DHCP6_EVENT_DEFERRED, dev, NULL);
if (dev->config->acquire_timeout) {
unsigned int deadline;
deadline = ni_lifetime_left(dev->config->acquire_timeout,
&dev->start_time, NULL);
if (deadline) {
ni_dhcp6_fsm_set_timeout_msec(dev, deadline);
dev->fsm.fail_on_timeout = 1;
return;
}
} /* infinite timeout, just continue selecting */
break;
case NI_DHCP6_STATE_CONFIRMING:
/*
* http://tools.ietf.org/html/rfc3315#section-18.1.2
*
* "[...]
* client SHOULD continue to use any IP addresses, using the
* last known lifetimes [...] and SHOULD continue to use any
* other previously obtained configuration parameters.
* [...]"
*/
if (dev->lease) {
/*
* Commit current lease to continue using it.
* A confirm is not extending any lifetimes.
*/
ni_dhcp6_fsm_reset(dev);
ni_dhcp6_fsm_commit_lease(dev, dev->lease);
} else {
/* Lease vanished?! Restart from request... */
ni_dhcp6_device_restart(dev);
}
break;
case NI_DHCP6_STATE_REQUESTING:
/* FIXME: */
break;
case NI_DHCP6_STATE_VALIDATING:
ni_dhcp6_fsm_bound(dev);
break;
case NI_DHCP6_STATE_BOUND:
if (dev->config->mode & NI_BIT(NI_DHCP6_MODE_INFO))
ni_dhcp6_fsm_request_info(dev);
else
ni_dhcp6_fsm_renew(dev);
break;
case NI_DHCP6_STATE_RENEWING:
ni_dhcp6_fsm_reset(dev);
ni_dhcp6_fsm_rebind(dev);
break;
case NI_DHCP6_STATE_REBINDING:
ni_dhcp6_device_drop_lease(dev);
ni_dhcp6_fsm_restart(dev);
break;
case NI_DHCP6_STATE_DECLINING:
/* FIXME: */
break;
case NI_DHCP6_STATE_RELEASING:
ni_dhcp6_fsm_commit_lease(dev, NULL);
ni_dhcp6_device_drop_lease(dev);
ni_dhcp6_device_stop(dev);
break;
default:
break;
}
}
static inline ni_bool_t
__fsm_select_best_offer(const ni_dhcp6_device_t *dev, const ni_addrconf_lease_t *lease, int pref, int weight)
{
/* when we don't have any or this is a better offer, remember it */
if (dev->best_offer.lease == NULL || dev->best_offer.weight < weight)
return TRUE;
/* ignore when we have an offer providing more requested things */
if (dev->best_offer.weight > weight) {
ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP,
"%s: we have a better offer than weight %d",
dev->ifname, weight);
return FALSE;
}
/* prefer offer from server with a higher server preference */
if (dev->best_offer.pref < pref) {
ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP,
"%s: prefer offer from server with higher preference %d",
dev->ifname, pref);
return TRUE;
}
/* prefer equal weight offer from the last server we've used */
if (dev->lease && dev->lease->dhcp6.server_id.len > 0 &&
!IN6_IS_ADDR_UNSPECIFIED(&dev->lease->dhcp6.server_addr)) {
if (IN6_ARE_ADDR_EQUAL(&dev->lease->dhcp6.server_addr, &lease->dhcp6.server_addr) ||
ni_opaque_eq(&dev->lease->dhcp6.server_id, &lease->dhcp6.server_id))
return TRUE;
}
return FALSE;
}
static int
ni_dhcp6_fsm_select_process_msg(ni_dhcp6_device_t *dev, ni_dhcp6_message_t *msg, ni_buffer_t *optbuf, char **hint)
{
unsigned int count = 0;
int weight = 0;
int pref = 0;
int rv = 1;
switch (msg->type) {
case NI_DHCP6_ADVERTISE:
if (ni_dhcp6_fsm_parse_client_options(dev, msg, optbuf) < 0)
return -1;
if (dev->config->max_rt) {
if (msg->max_rt != dev->config->max_rt)
dev->config->max_rt = -1U; /* inconsistent -> disable */
} else if (msg->max_rt) {
dev->config->max_rt = msg->max_rt; /* new setting -> apply */
}
if (msg->lease->dhcp6.rapid_commit) {
ni_string_printf(hint, "advertise with rapid commit option");
goto cleanup;
}
/*
* We've to (RFC MUST) discard all advertise messages with
* status NI_DHCP6_STATUS_NOADDRS; the another codes IMO
* don't fit here ... simply discard all unsuccessful codes.
*/
if (msg->lease->dhcp6.status &&
msg->lease->dhcp6.status->code != NI_DHCP6_STATUS_SUCCESS) {
ni_string_printf(hint, "status %s - %s",
ni_dhcp6_status_name(msg->lease->dhcp6.status->code),
msg->lease->dhcp6.status->message);
goto cleanup;
} else {
ni_dhcp6_fsm_show_lease_ia_status(dev, msg->lease, ni_info);
}
/* check if the config provides/overrides the preference */
if (!ni_dhcp6_config_server_preference( &msg->lease->dhcp6.server_addr,
&msg->lease->dhcp6.server_id,
&pref)) {
pref = msg->lease->dhcp6.server_pref;
ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP,
"%s: dhcp6 server preference %u",
dev->ifname, msg->lease->dhcp6.server_pref);
} else {
ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP,
"%s: dhcp6 server preference %u overridden by config to %d",
dev->ifname, msg->lease->dhcp6.server_pref, pref);
}
if (pref < 0) {
ni_string_printf(hint, "blacklisted server");
goto cleanup;
} else {
struct ni_dhcp6_ia_addr_counts usable;
if (!(count = ni_dhcp6_fsm_count_lease_iadrs(dev, msg->lease, &usable, NULL))) {
ni_string_printf(hint, "lease offer without usable address");
goto cleanup;
} else {
weight += count;
}
}
if (__fsm_select_best_offer(dev, msg->lease, pref, weight)) {
ni_dhcp6_device_set_best_offer(dev, msg->lease, pref, weight);
msg->lease = NULL;
ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP,
"%s: recorded regular offer with pref %d and weight %d",
dev->ifname, dev->best_offer.pref, dev->best_offer.weight);
} else if (!dev->best_offer.lease) {
ni_string_printf(hint, "%s: unacceptable regular offer with pref %d and weight %d",
dev->ifname, pref, weight);
goto cleanup;
}
if (dev->best_offer.lease && dev->retrans.count > 0) {
/* if the weight has maximum value, just accept this offer */
if (dev->best_offer.pref > 254) {
ni_dhcp6_fsm_timer_cancel(dev);
rv = ni_dhcp6_fsm_accept_offer(dev);
} else {
ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP,
"%s: waiting for better offers than pref %d and weight %d",
dev->ifname, dev->best_offer.pref, dev->best_offer.weight);
}
}
rv = 0;
break;
case NI_DHCP6_REPLY:
if (ni_dhcp6_fsm_parse_client_options(dev, msg, optbuf) < 0)
return -1;
if (dev->config->max_rt) {
if (msg->max_rt != dev->config->max_rt)
dev->config->max_rt = -1U; /* inconsistent -> disable */
} else if (msg->max_rt) {
dev->config->max_rt = msg->max_rt; /* new setting -> apply */
}
if (!msg->lease->dhcp6.rapid_commit) {
ni_string_printf(hint, "solicit reply without enabled rapid-commit");
goto cleanup;
}
if (msg->lease->dhcp6.status &&
msg->lease->dhcp6.status->code != NI_DHCP6_STATUS_SUCCESS) {
ni_string_printf(hint, "status %s - %s",
ni_dhcp6_status_name(msg->lease->dhcp6.status->code),
msg->lease->dhcp6.status->message);
goto cleanup;
} else {
ni_dhcp6_fsm_show_lease_ia_status(dev, msg->lease, ni_info);
}
/*
* 17.1.4. says it is our decision if we accept unrequested rapid-commit or not.
* The message is already filtered by last xid we've send.
*/
/* check if the config provides/overrides the preference */
if (!ni_dhcp6_config_server_preference( &msg->lease->dhcp6.server_addr,
&msg->lease->dhcp6.server_id,
&pref)) {
pref = msg->lease->dhcp6.server_pref;
ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP,
"%s: dhcp6 server preference %u",
dev->ifname, msg->lease->dhcp6.server_pref);
} else {
ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP,
"%s: dhcp6 server preference %u overridden by config to %d",
dev->ifname, msg->lease->dhcp6.server_pref, pref);
}
if (pref < 0) {
ni_string_printf(hint, "blacklisted server");
goto cleanup;
} else {
struct ni_dhcp6_ia_addr_counts usable;
if (!(count = ni_dhcp6_fsm_count_lease_iadrs(dev, msg->lease, &usable, NULL))) {
ni_string_printf(hint, "rapid-commit lease without usable address");
goto cleanup;
} else {
weight += count;
}
}
if (__fsm_select_best_offer(dev, msg->lease, pref, weight)) {
ni_dhcp6_device_set_best_offer(dev, msg->lease, pref, weight);
msg->lease = NULL;
ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP,
"%s: recorded rapid-commit offer with pref %d and weight %d",
dev->ifname, dev->best_offer.pref, dev->best_offer.weight);
} else if (!dev->best_offer.lease) {
ni_string_printf(hint, "%s: unacceptable rapid-commmit offer with pref %d and weight %d",
dev->ifname, pref, weight);
goto cleanup;
}
if (dev->best_offer.lease && dev->retrans.count > 0) {
/* if the weight has maximum value, just accept this offer */
if (dev->best_offer.pref > 254) {
ni_dhcp6_fsm_timer_cancel(dev);
rv = ni_dhcp6_fsm_accept_offer(dev);
} else {
ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP,
"%s: waiting for better offers than pref %d and weight %d",
dev->ifname, dev->best_offer.pref, dev->best_offer.weight);
}
}
rv = 0;
break;
default:
break;
}
cleanup:
return rv;
}
static int
ni_dhcp6_fsm_request_process_msg(ni_dhcp6_device_t *dev, ni_dhcp6_message_t *msg, ni_buffer_t *optbuf, char **hint)
{
int rv = 1;
switch (msg->type) {
case NI_DHCP6_REPLY:
if (ni_dhcp6_fsm_parse_client_options(dev, msg, optbuf) < 0)
return -1;
if (!dev->best_offer.lease) {
ni_string_printf(hint, "lease request reply without request");
goto cleanup;
}
/*
* best offer points to the lease we've requested -- check that the
* server in the reply matches the server we request the lease from.
*/
if (!IN6_ARE_ADDR_EQUAL(&dev->best_offer.lease->dhcp6.server_addr, &msg->lease->dhcp6.server_addr) ||
!ni_opaque_eq(&dev->best_offer.lease->dhcp6.server_id, &msg->lease->dhcp6.server_id)) {
ni_string_printf(hint, "lease request reply from another server");
goto cleanup;
}
if (msg->lease->dhcp6.status &&
msg->lease->dhcp6.status->code != NI_DHCP6_STATUS_SUCCESS) {
ni_string_printf(hint, "status %s - %s",
ni_dhcp6_status_name(msg->lease->dhcp6.status->code),
msg->lease->dhcp6.status->message);
ni_dhcp6_device_drop_best_offer(dev);
ni_dhcp6_device_restart(dev);
goto cleanup;
} else {
ni_dhcp6_ia_t *ia;
for (ia = msg->lease->dhcp6.ia_list; ia; ia = ia->next) {
if (ia->status.code == NI_DHCP6_STATUS_NOADDRS) {
ni_string_printf(hint, "status %s - %s",
ni_dhcp6_status_name(ia->status.code),
ia->status.message ? ia->status.message :
"no addresses available");
goto cleanup;
}
}
}
/*
* FIXME: exact match check!! for now, it needs at least equal address count
*/
if (ni_address_list_count(msg->lease->addrs) < ni_address_list_count(dev->best_offer.lease->addrs))
goto cleanup;
ni_dhcp6_fsm_timer_cancel(dev);
ni_dhcp6_device_retransmit_disarm(dev);
ni_dhcp6_fsm_commit_lease(dev, msg->lease);
msg->lease = NULL;
ni_dhcp6_device_drop_best_offer(dev);
rv = 0;
break;
default:
break;
}
cleanup:
return rv;
}
static const ni_dhcp6_ia_addr_t *
ni_dhcp6_fsm_confirm_process_find_ia_addrs_status(const ni_dhcp6_ia_addr_t *addrs, unsigned int status)
{
const ni_dhcp6_ia_addr_t *iaddr;
for (iaddr = addrs; iaddr; iaddr = iaddr->next) {
if (ni_dhcp6_status_code(&iaddr->status) == status)
return iaddr;
}
return NULL;
}
static const ni_dhcp6_ia_t *
ni_dhcp6_fsm_confirm_process_find_ia_status(const ni_dhcp6_ia_t *ia_list, unsigned int status,
const ni_dhcp6_ia_addr_t **iaddr)
{
const ni_dhcp6_ia_t *ia;
for (ia = ia_list; ia; ia = ia->next) {
if ((*iaddr = ni_dhcp6_fsm_confirm_process_find_ia_addrs_status(ia->addrs, status)))
return ia;
else if (ni_dhcp6_status_code(&ia->status) == status)
return ia;
}
return NULL;
}