-
Notifications
You must be signed in to change notification settings - Fork 31
/
config.c
2365 lines (2075 loc) · 55.9 KB
/
config.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
/* $KAME: config.c,v 1.53 2005/09/16 11:30:14 suz Exp $ */
/*
* Copyright (C) 2002 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#ifdef __KAME__
#include <net/if_dl.h>
#endif
#ifdef __linux__
#include <linux/if_packet.h>
#endif
#include <syslog.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ifaddrs.h>
#include <errno.h>
#ifdef __linux__
#define __USE_XOPEN
#include <time.h>
#endif
#include "dhcp6.h"
#include "config.h"
#include "common.h"
#include "auth.h"
#include "base64.h"
#include "lease.h"
extern int errno;
struct prefix_ifconf *prefix_ifconflist;
struct dhcp6_list siplist, sipnamelist, dnslist, dnsnamelist, ntplist;
struct dhcp6_list nislist, nisnamelist;
struct dhcp6_list nisplist, nispnamelist;
struct dhcp6_list bcmcslist, bcmcsnamelist;
long long optrefreshtime;
static struct dhcp6_ifconf *dhcp6_ifconflist;
static struct ia_conflist ia_conflist0;
static struct host_conf *host_conflist0, *host_conflist;
static struct keyinfo *key_list, *key_list0;
static struct authinfo *auth_list, *auth_list0;
static struct dhcp6_list siplist0, sipnamelist0, dnslist0, dnsnamelist0, ntplist0;
static struct dhcp6_list nislist0, nisnamelist0;
static struct dhcp6_list nisplist0, nispnamelist0;
static struct dhcp6_list bcmcslist0, bcmcsnamelist0;
static long long optrefreshtime0 = -1;
#ifndef DHCP6_DYNAMIC_HOSTCONF_MAX
#define DHCP6_DYNAMIC_HOSTCONF_MAX 1024
#endif
struct dynamic_hostconf {
TAILQ_ENTRY(dynamic_hostconf) link;
struct host_conf *host;
};
static TAILQ_HEAD(dynamic_hostconf_listhead, dynamic_hostconf)
dynamic_hostconf_head;
static unsigned int dynamic_hostconf_count;
static struct pool_conf *pool_conflist, *pool_conflist0;
enum { DHCPOPTCODE_SEND, DHCPOPTCODE_REQUEST, DHCPOPTCODE_ALLOW };
/* temporary configuration structure for DHCP interface */
struct dhcp6_ifconf {
struct dhcp6_ifconf *next;
char *ifname;
/* configuration flags */
u_long send_flags;
u_long allow_flags;
int server_pref; /* server preference (server only) */
char *scriptpath; /* path to config script (client only) */
struct duid duid;
struct rawop_list rawops;
struct dhcp6_list reqopt_list;
struct ia_conflist iaconf_list;
struct authinfo *authinfo; /* authentication information
* (no need to clear) */
struct dhcp6_poolspec pool;
};
extern struct cf_list *cf_dns_list, *cf_dns_name_list, *cf_ntp_list;
extern struct cf_list *cf_sip_list, *cf_sip_name_list;
extern struct cf_list *cf_nis_list, *cf_nis_name_list;
extern struct cf_list *cf_nisp_list, *cf_nisp_name_list;
extern struct cf_list *cf_bcmcs_list, *cf_bcmcs_name_list;
extern long long cf_refreshtime;
extern char *configfilename;
static struct keyinfo *find_keybyname(struct keyinfo *, char *);
static int add_pd_pif(struct iapd_conf *, struct cf_list *, u_int32_t);
static int add_options(int, struct dhcp6_ifconf *, struct cf_list *);
static int add_prefix(struct dhcp6_list *, const char *, int,
struct dhcp6_prefix *);
static void clear_pd_pif(struct iapd_conf *);
static void clear_ifconf(struct dhcp6_ifconf *);
static void clear_iaconf(struct ia_conflist *);
static void clear_hostconf(struct host_conf *);
static void clear_keys(struct keyinfo *);
static void clear_authinfo(struct authinfo *);
static int configure_duid(char *, struct duid *);
static int configure_addr(struct cf_list *, struct dhcp6_list *, const char *);
static int configure_domain(struct cf_list *, struct dhcp6_list *,
const char *);
static int set_default_ifid(struct prefix_ifconf *);
static int set_current_ifid(struct prefix_ifconf *, unsigned long long);
static int set_random_ifid(struct prefix_ifconf *);
static void clear_poolconf(struct pool_conf *);
static struct pool_conf *create_pool(char *, struct dhcp6_range *);
struct host_conf *find_dynamic_hostconf(struct duid *);
static int in6_addr_cmp(struct in6_addr *, struct in6_addr *);
static void in6_addr_inc(struct in6_addr *);
/* a debug helper to complete someday if needed... or delete*/
void list_cfl (char *tag,struct cf_namelist *head)
{
struct cf_namelist *ifp;
printf("LIST CFL %s\n",tag);
for (ifp = head; ifp; ifp = ifp->next) {
printf("Do ifp->%s\n", ifp->name);
struct cf_list *cfl;
for (cfl = ifp->params; cfl; cfl = cfl->next) {
printf("Do cfltype->%i lineconf %i ptr=%p sublist=%p\n", cfl->type,cfl->line, cfl->ptr, cfl->list);
switch(cfl->type) {
case DECL_SEND:{
struct cf_list *cf0 = (void*)cfl->list;
for (; cf0; cf0 = cf0->next) {
//printf("SEND opt type %i\n",cf0->type);
switch (cf0->type) {
case DHCPOPT_RAW:{
struct rawoption *op = cf0->ptr;
printf("rawop %i datalen %i\n", op->opnum, op->datalen);
}
break;
default:;
}
}
}
break;
case DECL_SCRIPT:
printf("script %s\n", (char *)cfl->ptr);
break;
default:
printf("Unknown option type %i\n", cfl->type);
}
/*
case DHCPOPT_RAW:
case DHCPOPT_RAPID_COMMIT:
case DHCPOPT_AUTHINFO:
case DHCPOPT_IA_PD:
case DHCPOPT_IA_NA:
case DHCPOPT_SIP:
case DHCPOPT_SIPNAME:
case DHCPOPT_DNS:
case DHCPOPT_DNSNAME:
case DHCPOPT_NTP:
case DHCPOPT_NIS:
case DHCPOPT_NISNAME:
case DHCPOPT_NISP:
case DHCPOPT_NISPNAME:
case DHCPOPT_BCMCS:
case DHCPOPT_BCMCSNAME:
case DHCPOPT_REFRESHTIME:
printf("Known option type %i\n", cfl->type);
break;
default:
printf("Unknown option type %i\n", cfl->type);
*/
}
}
}
int
configure_interface(struct cf_namelist *iflist)
{
struct cf_namelist *ifp;
struct dhcp6_ifconf *ifc;
char *cp;
for (ifp = iflist; ifp; ifp = ifp->next) {
struct cf_list *cfl;
if (if_nametoindex(ifp->name) == 0) {
d_printf(LOG_ERR, FNAME, "invalid interface(%s): %s",
ifp->name, strerror(errno));
goto bad;
}
if ((ifc = malloc(sizeof(*ifc))) == NULL) {
d_printf(LOG_ERR, FNAME,
"memory allocation for %s failed", ifp->name);
goto bad;
}
memset(ifc, 0, sizeof(*ifc));
ifc->next = dhcp6_ifconflist;
dhcp6_ifconflist = ifc;
if ((ifc->ifname = strdup(ifp->name)) == NULL) {
d_printf(LOG_ERR, FNAME, "failed to copy ifname");
goto bad;
}
ifc->server_pref = DH6OPT_PREF_UNDEF;
TAILQ_INIT(&ifc->reqopt_list);
TAILQ_INIT(&ifc->iaconf_list);
TAILQ_INIT(&ifc->rawops);
for (cfl = ifp->params; cfl; cfl = cfl->next) {
switch(cfl->type) {
case DECL_REQUEST:
if (dhcp6_mode != DHCP6_MODE_CLIENT) {
d_printf(LOG_INFO, FNAME, "%s:%d "
"client-only configuration",
configfilename,
cfl->line);
goto bad;
}
if (add_options(DHCPOPTCODE_REQUEST,
ifc, cfl->list)) {
goto bad;
}
break;
case DECL_SEND:
if (add_options(DHCPOPTCODE_SEND,
ifc, cfl->list)) {
goto bad;
}
break;
case DECL_ALLOW:
if (add_options(DHCPOPTCODE_ALLOW,
ifc, cfl->list)) {
goto bad;
}
break;
case DECL_DUID:
if ((configure_duid((char *)cfl->ptr,
&ifc->duid)) != 0) {
d_printf(LOG_ERR, FNAME, "%s:%d "
"failed to configure "
"DUID for %s",
configfilename, cfl->line,
ifc->ifname);
goto bad;
}
d_printf(LOG_DEBUG, FNAME,
"configure DUID for %s: %s",
ifc->ifname, duidstr(&ifc->duid));
break;
case DECL_INFO_ONLY:
if (dhcp6_mode != DHCP6_MODE_CLIENT) {
d_printf(LOG_INFO, FNAME, "%s:%d "
"client-only configuration",
configfilename, cfl->line);
goto bad;
}
ifc->send_flags |= DHCIFF_INFO_ONLY;
break;
case DECL_PREFERENCE:
if (dhcp6_mode != DHCP6_MODE_SERVER) {
d_printf(LOG_INFO, FNAME, "%s:%d "
"server-only configuration",
configfilename, cfl->line);
goto bad;
}
ifc->server_pref = (int)cfl->num;
if (ifc->server_pref < 0 ||
ifc->server_pref > 255) {
d_printf(LOG_INFO, FNAME, "%s:%d "
"bad value: %d",
configfilename, cfl->line,
ifc->server_pref);
goto bad;
}
break;
case DECL_SCRIPT:
if (dhcp6_mode != DHCP6_MODE_CLIENT) {
d_printf(LOG_INFO, FNAME, "%s:%d "
"client-only configuration",
configfilename, cfl->line);
goto bad;
}
if (ifc->scriptpath) {
d_printf(LOG_INFO, FNAME,
"%s:%d duplicated configuration",
configfilename, cfl->line);
goto bad;
}
cp = cfl->ptr;
ifc->scriptpath = strdup(cp + 1);
if (ifc->scriptpath == NULL) {
d_printf(LOG_NOTICE, FNAME,
"failed to copy script path");
goto bad;
}
cp = ifc->scriptpath;
if (*cp != '/') {
d_printf(LOG_INFO, FNAME,
"script must be an absolute path");
goto bad;
}
cp += strlen(ifc->scriptpath) - 1;
*cp = '\0'; /* clear the terminating quote */
break;
case DECL_ADDRESSPOOL:
{
struct dhcp6_poolspec* spec;
struct pool_conf* pool;
spec = (struct dhcp6_poolspec *)cfl->ptr;
for (pool = pool_conflist0; pool; pool = pool->next)
if (strcmp(spec->name, pool->name) == 0)
break;
if (pool == NULL) {
d_printf(LOG_ERR, FNAME, "%s:%d "
"pool '%s' not found",
configfilename, cfl->line,
spec->name);
goto bad;
}
if (spec->vltime != DHCP6_DURATION_INFINITE &&
(spec->pltime == DHCP6_DURATION_INFINITE ||
spec->pltime > spec->vltime)) {
d_printf(LOG_ERR, FNAME, "%s:%d ",
configfilename, cfl->line,
"specified a larger preferred lifetime "
"than valid lifetime");
goto bad;
}
ifc->pool = *spec;
if ((ifc->pool.name = strdup(spec->name)) == NULL) {
d_printf(LOG_ERR, FNAME,
"memory allocation failed");
goto bad;
}
d_printf(LOG_DEBUG, FNAME,
"pool '%s' is specified to the interface '%s'",
ifc->pool.name, ifc->ifname);
}
break;
default:
d_printf(LOG_ERR, FNAME, "%s:%d "
"invalid interface configuration",
configfilename, cfl->line);
goto bad;
}
}
if (use_all_config_if) {
if (ifinit(ifp->name) == NULL) {
d_printf(LOG_ERR, FNAME, "failed to initialize %s", ifp->name);
/* safe to exit here as still parsing */
exit(1);
}
}
}
return (0);
bad:
clear_ifconf(dhcp6_ifconflist);
dhcp6_ifconflist = NULL;
return (-1);
}
int
configure_ia(struct cf_namelist *ialist, iatype_t iatype)
{
struct cf_namelist *iap;
struct ia_conf *iac = NULL;
size_t confsize;
static int init = 1;
u_int32_t if_count;
if (init) {
TAILQ_INIT(&ia_conflist0);
init = 0;
}
switch(iatype) {
case IATYPE_PD:
confsize = sizeof(struct iapd_conf);
break;
case IATYPE_NA:
confsize = sizeof(struct iana_conf);
break;
default:
d_printf(LOG_ERR, FNAME, "internal error");
goto bad;
}
for (iap = ialist; iap; iap = iap->next) {
struct cf_list *cfl;
if ((iac = malloc(confsize)) == NULL) {
d_printf(LOG_ERR, FNAME,
"memory allocation for IA %s failed",
iap->name);
goto bad;
}
memset(iac, 0, confsize);
/* common initialization */
iac->type = iatype;
iac->iaid = (uint32_t)atoi(iap->name);
TAILQ_INIT(&iac->iadata);
TAILQ_INSERT_TAIL(&ia_conflist0, iac, link);
/* IA-type specific initialization */
switch(iatype) {
case IATYPE_PD:
TAILQ_INIT(&((struct iapd_conf *)iac)->iapd_prefix_list);
TAILQ_INIT(&((struct iapd_conf *)iac)->iapd_pif_list);
break;
case IATYPE_NA:
TAILQ_INIT(&((struct iana_conf *)iac)->iana_address_list);
break;
}
if_count = 0;
/* set up parameters for the IA */
for (cfl = iap->params; cfl; cfl = cfl->next) {
struct iapd_conf *pdp = (struct iapd_conf *) iac;
struct iana_conf *nap = (struct iana_conf *) iac;
switch (iatype) {
case IATYPE_PD:
switch(cfl->type) {
case IACONF_PIF:
if (add_pd_pif(pdp, cfl, if_count))
goto bad;
if_count++;
break;
case IACONF_PREFIX:
if (add_prefix(&pdp->iapd_prefix_list,
"IAPD", DHCP6_LISTVAL_PREFIX6,
cfl->ptr)) {
d_printf(LOG_NOTICE, FNAME, "failed "
"to configure prefix");
goto bad;
}
break;
default:
d_printf(LOG_ERR, FNAME, "%s:%d "
"invalid configuration",
configfilename, cfl->line);
goto bad;
}
break;
case IATYPE_NA:
switch(cfl->type) {
case IACONF_ADDR:
if (add_prefix(&nap->iana_address_list,
"IANA", DHCP6_LISTVAL_STATEFULADDR6,
cfl->ptr)) {
d_printf(LOG_NOTICE, FNAME, "failed "
"to configure address");
goto bad;
}
break;
default:
d_printf(LOG_ERR, FNAME, "%s:%d "
"invalid configuration",
configfilename, cfl->line);
goto bad;
}
break;
default:
d_printf(LOG_ERR, FNAME, "%s:%d "
"invalid iatype %d",
configfilename, cfl->line, iatype);
goto bad;
}
}
}
return (0);
bad:
return (-1);
}
static int
add_pd_pif(struct iapd_conf *iapdc, struct cf_list *cfl0, u_int32_t if_count)
{
struct cf_list *cfl;
struct prefix_ifconf *pif;
int ifid_done = 0;
/* duplication check */
for (pif = TAILQ_FIRST(&iapdc->iapd_pif_list); pif;
pif = TAILQ_NEXT(pif, link)) {
if (strcmp(pif->ifname, cfl0->ptr) == 0) {
d_printf(LOG_NOTICE, FNAME, "%s:%d "
"duplicated prefix interface: %s",
configfilename, cfl0->line, cfl0->ptr);
return (0); /* ignore it */
}
}
if ((pif = malloc(sizeof(*pif))) == NULL) {
d_printf(LOG_ERR, FNAME,
"memory allocation for %s failed", cfl0->ptr);
goto bad;
}
memset(pif, 0, sizeof(*pif));
/* validate and copy ifname */
if (if_nametoindex(cfl0->ptr) == 0) {
d_printf(LOG_ERR, FNAME, "%s:%d invalid interface (%s): %s",
configfilename, cfl0->line,
cfl0->ptr, strerror(errno));
goto bad;
}
if ((pif->ifname = strdup(cfl0->ptr)) == NULL) {
d_printf(LOG_ERR, FNAME, "failed to copy ifname");
goto bad;
}
pif->ifid_len = IFID_LEN_DEFAULT;
pif->sla_len = SLA_LEN_DEFAULT;
pif->sla_id = if_count;
for (cfl = cfl0->list; cfl; cfl = cfl->next) {
switch(cfl->type) {
case IFPARAM_SLA_ID:
pif->sla_id = (uint32_t)cfl->num;
break;
case IFPARAM_SLA_LEN:
pif->sla_len = (int)cfl->num;
if (pif->sla_len < 0 || pif->sla_len > 128) {
d_printf(LOG_ERR, FNAME, "%s:%d "
"invalid SLA length: %d",
configfilename, cfl->line, pif->sla_len);
goto bad;
}
break;
case IFPARAM_IFID_EUI64:
if (set_default_ifid(pif)) {
d_printf(LOG_NOTICE, FNAME,
"failed to get default IF ID for %s",
pif->ifname);
goto bad;
}
ifid_done = 1;
break;
case IFPARAM_IFID_RAND:
set_random_ifid(pif);
ifid_done = 1;
break;
case IFPARAM_IFID:
set_current_ifid(pif, cfl->num);
ifid_done = 1;
break;
default:
d_printf(LOG_ERR, FNAME, "%s:%d internal error: "
"invalid configuration",
configfilename, cfl->line);
goto bad;
}
}
if (!ifid_done && set_default_ifid(pif)) {
d_printf(LOG_NOTICE, FNAME,
"failed to get default IF ID for %s, using random ID",
pif->ifname);
set_random_ifid(pif);
}
TAILQ_INSERT_TAIL(&iapdc->iapd_pif_list, pif, link);
return (0);
bad:
if (pif->ifname)
free(pif->ifname);
free(pif);
return (-1);
}
int
configure_host(struct cf_namelist *hostlist)
{
struct cf_namelist *host;
struct host_conf *hconf;
for (host = hostlist; host; host = host->next) {
struct cf_list *cfl;
if ((hconf = malloc(sizeof(*hconf))) == NULL) {
d_printf(LOG_ERR, FNAME, "memory allocation failed "
"for host %s", host->name);
goto bad;
}
memset(hconf, 0, sizeof(*hconf));
TAILQ_INIT(&hconf->prefix_list);
TAILQ_INIT(&hconf->addr_list);
hconf->next = host_conflist0;
host_conflist0 = hconf;
if ((hconf->name = strdup(host->name)) == NULL) {
d_printf(LOG_ERR, FNAME, "failed to copy host name: %s",
host->name);
goto bad;
}
for (cfl = host->params; cfl; cfl = cfl->next) {
switch(cfl->type) {
case DECL_DUID:
if (hconf->duid.duid_id) {
d_printf(LOG_ERR, FNAME, "%s:%d "
"duplicated DUID for %s",
configfilename,
cfl->line, host->name);
goto bad;
}
if ((configure_duid((char *)cfl->ptr,
&hconf->duid)) != 0) {
d_printf(LOG_ERR, FNAME, "%s:%d "
"failed to configure "
"DUID for %s",
configfilename, cfl->line,
host->name);
goto bad;
}
d_printf(LOG_DEBUG, FNAME,
"configure DUID for %s: %s",
host->name, duidstr(&hconf->duid));
break;
case DECL_PREFIX:
if (add_prefix(&hconf->prefix_list,
hconf->name, DHCP6_LISTVAL_PREFIX6,
cfl->ptr)) {
d_printf(LOG_ERR, FNAME, "failed "
"to configure prefix for %s",
host->name);
goto bad;
}
break;
case DECL_ADDRESS:
if (add_prefix(&hconf->addr_list,
hconf->name, DHCP6_LISTVAL_STATEFULADDR6,
cfl->ptr)) {
d_printf(LOG_ERR, FNAME, "failed "
"to configure address for %s",
host->name);
goto bad;
}
break;
case DECL_DELAYEDKEY:
if (hconf->delayedkey != NULL) {
d_printf(LOG_WARNING, FNAME,
"%s:%d: duplicate key %s for %s"
" (ignored)", configfilename,
cfl->line, cfl->ptr, host->name);
continue;
}
if ((hconf->delayedkey =
find_keybyname(key_list0, cfl->ptr))
== NULL) {
d_printf(LOG_ERR, FNAME, "failed to "
"find key information for %s",
cfl->ptr);
goto bad;
}
d_printf(LOG_DEBUG, FNAME, "configure key for "
"delayed auth with %s (keyid=%08x)",
host->name, hconf->delayedkey->keyid);
break;
case DECL_ADDRESSPOOL:
{
struct dhcp6_poolspec* spec;
struct pool_conf *pool;
spec = (struct dhcp6_poolspec *)cfl->ptr;
for (pool = pool_conflist0; pool; pool = pool->next)
if (strcmp(spec->name, pool->name) == 0)
break;
if (pool == NULL) {
d_printf(LOG_ERR, FNAME, "%s:%d "
"pool '%s' not found",
configfilename, cfl->line,
spec->name);
goto bad;
}
if (spec->vltime != DHCP6_DURATION_INFINITE &&
(spec->pltime == DHCP6_DURATION_INFINITE ||
spec->pltime > spec->vltime)) {
d_printf(LOG_ERR, FNAME, "%s:%d ",
configfilename, cfl->line,
"specified a larger preferred lifetime "
"than valid lifetime");
goto bad;
}
hconf->pool = *spec;
if ((hconf->pool.name = strdup(spec->name)) == NULL) {
d_printf(LOG_ERR, FNAME,
"memory allocation failed");
goto bad;
}
d_printf(LOG_DEBUG, FNAME,
"pool '%s' is specified to the host '%s'",
hconf->pool.name, hconf->name);
}
break;
default:
d_printf(LOG_ERR, FNAME, "%s:%d "
"invalid host configuration for %s",
configfilename, cfl->line,
host->name);
goto bad;
}
}
}
return (0);
bad:
/* there is currently nothing special to recover the error */
return (-1);
}
int
configure_keys(struct cf_namelist *keylist)
{
struct cf_namelist *key;
char *secretstr;
char secret[1024];
int secretlen;
struct keyinfo *kinfo;
long long keyid;
char *expire = NULL;
for (key = keylist; key; key = key->next) {
struct cf_list *cfl;
if ((kinfo = malloc(sizeof(*kinfo))) == NULL) {
d_printf(LOG_ERR, FNAME, "memory allocation failed "
"for key %s", key->name);
goto bad;
}
memset(kinfo, 0, sizeof(*kinfo));
kinfo->next = key_list0;
key_list0 = kinfo;
if ((kinfo->name = strdup(key->name)) == NULL) {
d_printf(LOG_ERR, FNAME, "failed to copy key name: %s",
key->name);
goto bad;
}
keyid = -1;
expire = NULL;
for (cfl = key->params; cfl; cfl = cfl->next) {
switch (cfl->type) {
case KEYPARAM_REALM:
if (kinfo->realm != NULL) {
d_printf(LOG_WARNING, FNAME,
"%s:%d duplicate realm for key %s "
"(ignored)", configfilename,
cfl->line, key->name);
continue;
}
kinfo->realm = qstrdup(cfl->ptr);
if (kinfo->realm == NULL) {
d_printf(LOG_WARNING, FNAME,
"failed to allocate memory for "
"realm");
goto bad;
}
kinfo->realmlen = strlen(kinfo->realm);
break;
case KEYPARAM_KEYID:
if (keyid != -1) {
d_printf(LOG_WARNING, FNAME,
"%s:%d duplicate realm for key %s "
"(ignored)",
configfilename, cfl->line);
continue;
}
keyid = cfl->num;
if (keyid < 0 || keyid > 0xffffffff) {
d_printf(LOG_WARNING, FNAME,
"%s:%d key ID overflow",
configfilename, cfl->line);
goto bad;
}
break;
case KEYPARAM_SECRET:
/* duplicate check */
if (kinfo->secret != NULL) {
d_printf(LOG_WARNING, FNAME,
"%s:%d duplicate secret "
"for key %s (ignored)",
configfilename, cfl->line,
key->name);
continue; /* ignored */
}
/* convert base64 string to binary secret */
if ((secretstr = qstrdup(cfl->ptr)) == NULL) {
d_printf(LOG_WARNING, FNAME,
"failed to make a copy of secret");
goto bad;
}
memset(secret, 0, sizeof(secret));
secretlen = base64_decodestring(secretstr,
secret, sizeof(secret));
if (secretlen < 0) {
d_printf(LOG_ERR, FNAME,
"%s:%d failed to parse base64 key",
configfilename, cfl->line);
free(secretstr);
goto bad;
}
free(secretstr);
/* set the binary secret */
kinfo->secret = malloc(secretlen);
if (kinfo->secret == NULL) {
d_printf(LOG_WARNING, FNAME,
"failed to allocate memory "
"for secret");
goto bad;
}
memcpy(kinfo->secret, secret, secretlen);
kinfo->secretlen = secretlen;
break;
case KEYPARAM_EXPIRE:
if (expire != NULL) {
d_printf(LOG_WARNING, FNAME,
"%s:%d duplicate expire for key "
"%s (ignored)", configfilename,
cfl->line, key->name);
continue;
}
expire = qstrdup(cfl->ptr);
break;
default:
d_printf(LOG_ERR, FNAME,
"%s:%d invalid key parameter for %s",
configfilename, cfl->line, key->name);
goto bad;
}
}
/* check for mandatory parameters or use default */
if (kinfo->realm == NULL) {
d_printf(LOG_ERR, FNAME,
"realm not specified for key %s", key->name);
goto bad;
}
if (keyid == -1) {
d_printf(LOG_ERR, FNAME,
"key ID not specified for key %s", key->name);
goto bad;
}
kinfo->keyid = keyid;
if (kinfo->secret == NULL) {
d_printf(LOG_ERR, FNAME,
"secret not specified for key %s", key->name);
goto bad;
}
kinfo->expire = 0;
if (expire != NULL) {
if (strcmp(expire, "forever") != 0) {
time_t now, expire_time;
struct tm *lt;
if (time(&now) == -1) {
d_printf(LOG_ERR, FNAME, "cannot get "
"current time: %s",
strerror(errno));
goto bad;
}
lt = localtime(&now);
lt->tm_sec = 0;
if (strptime(expire, "%Y-%m-%d %H:%M", lt)
== NULL &&
strptime(expire, "%m-%d %H:%M", lt)
== NULL &&
strptime(expire, "%H:%M", lt) == NULL) {
d_printf(LOG_ERR, FNAME, "invalid "
"expiration time: %s");
goto bad;
}
expire_time = mktime(lt);
if (expire_time < now) {
d_printf(LOG_ERR, FNAME, "past "
"expiration time specified: %s",
expire);
goto bad;
}
kinfo->expire = expire_time;
}
}
}
return (0);
bad:
if (expire != NULL)
free(expire);
return (-1);
}
static struct keyinfo *
find_keybyname(struct keyinfo *head, char *kname)
{
struct keyinfo *kinfo;
for (kinfo = head; kinfo != NULL; kinfo = kinfo->next) {
if (strcmp(kname, kinfo->name) == 0)
return (kinfo);
}
return (NULL);
}
int
configure_authinfo(struct cf_namelist *authlist)
{
struct cf_namelist *auth;
struct authinfo *ainfo;
for (auth = authlist; auth; auth = auth->next) {
struct cf_list *cfl;
if ((ainfo = malloc(sizeof(*ainfo))) == NULL) {
d_printf(LOG_ERR, FNAME, "memory allocation failed "
"for auth info %s", auth->name);
goto bad;
}
memset(ainfo, 0, sizeof(*ainfo));
ainfo->next = auth_list0;
auth_list0 = ainfo;
ainfo->protocol = DHCP6_AUTHPROTO_UNDEF;
ainfo->algorithm = DHCP6_AUTHALG_UNDEF;
ainfo->rdm = DHCP6_AUTHRDM_UNDEF;
if ((ainfo->name = strdup(auth->name)) == NULL) {
d_printf(LOG_ERR, FNAME,
"failed to copy auth info name: %s", auth->name);
goto bad;
}
for (cfl = auth->params; cfl; cfl = cfl->next) {
switch (cfl->type) {
case AUTHPARAM_PROTO:
if (ainfo->protocol != DHCP6_AUTHPROTO_UNDEF) {
d_printf(LOG_WARNING, FNAME,
"%s:%d duplicate protocol "
"for auth info %s "
"(ignored)",
configfilename, cfl->line,