forked from opnsense/dhcp6c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
3546 lines (3129 loc) · 81.8 KB
/
common.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: common.c,v 1.129 2005/09/16 11:30:13 suz Exp $ */
/*
* Copyright (C) 1998 and 1999 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/ioctl.h>
#include <sys/queue.h>
#include <sys/stat.h>
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include <net/if.h>
#include <netinet/in.h>
#ifdef __KAME__
#include <net/if_types.h>
#ifdef __FreeBSD__
#include <net/if_var.h>
#endif
#include <net/if_dl.h>
#endif
#ifdef __linux__
#include <linux/if_packet.h>
#endif
#include <net/if_arp.h>
#ifdef __sun__
#include <sys/sockio.h>
#include <sys/dlpi.h>
#include <stropts.h>
#include <fcntl.h>
#include <libdevinfo.h>
#endif
#ifdef __KAME__
#include <netinet6/in6_var.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdarg.h>
#include <syslog.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <netdb.h>
#include <ifaddrs.h>
#include "dhcp6.h"
#include "config.h"
#include "common.h"
#include "timer.h"
#ifdef __linux__
/* from /usr/include/linux/ipv6.h */
struct in6_ifreq {
struct in6_addr ifr6_addr;
uint32_t ifr6_prefixlen;
unsigned int ifr6_ifindex;
};
#endif
#define MAXDNAME 255
int foreground;
int debug_thresh;
static int dhcp6_count_list(struct dhcp6_list *);
static int in6_matchflags(struct sockaddr *, char *, int);
static ssize_t dnsencode(const char *, char *, size_t);
static char *dnsdecode(u_char **, u_char *, char *, size_t);
static int copyout_option(char *, char *, struct dhcp6_listval *);
static int copyin_option(int, struct dhcp6opt *, struct dhcp6opt *,
struct dhcp6_list *);
static int copy_option(uint16_t, uint16_t, void *, struct dhcp6opt **,
struct dhcp6opt *, int *);
static ssize_t gethwid(char *, int, const char *, uint16_t *);
static char *sprint_uint64(char *, int, uint64_t);
static char *sprint_auth(struct dhcp6_optinfo *);
int
rawop_count_list(head)
struct rawop_list *head;
{
struct rawoption *op;
int i;
//d_printf(LOG_INFO, FNAME, "counting list at %p", (void*)head);
for (i = 0, op = TAILQ_FIRST(head); op; op = TAILQ_NEXT(op, link)) {
i++;
}
return (i);
}
void
rawop_clear_list(head)
struct rawop_list *head;
{
struct rawoption *op;
//d_printf(LOG_INFO, FNAME, "clearing %d rawops at %p", rawop_count_list(head), (void*)head);
while ((op = TAILQ_FIRST(head)) != NULL) {
//d_printf(LOG_INFO, FNAME, " current op: %p link: %p", (void*)op, op->link);
TAILQ_REMOVE(head, op, link);
if (op->data != NULL) {
d_printf(LOG_INFO, FNAME, " freeing op data at %p", (void*)op->data);
free(op->data);
}
free(op); // Needed? yes
}
return;
}
int
rawop_copy_list(dst, src)
struct rawop_list *dst, *src;
{
struct rawoption *op, *newop;
/*
d_printf(LOG_INFO, FNAME,
" copying rawop list %p to %p (%d ops)",
(void*)src, (void*)dst, rawop_count_list(src));
*/
for (op = TAILQ_FIRST(src); op; op = TAILQ_NEXT(op, link)) {
newop = NULL;
if ((newop = malloc(sizeof(*newop))) == NULL) {
d_printf(LOG_ERR, FNAME,
"failed to allocate memory for a new raw option");
goto fail;
}
memset(newop, 0, sizeof(*newop));
newop->opnum = op->opnum;
newop->datalen = op->datalen;
newop->data = NULL;
/* copy data */
if ((newop->data = malloc(newop->datalen)) == NULL) {
d_printf(LOG_ERR, FNAME,
"failed to allocate memory for new raw option data");
goto fail;
}
memcpy(newop->data, op->data, newop->datalen);
//d_printf(LOG_INFO, FNAME, " copied %d bytes of data at %p", newop->datalen, (void*)newop->data);
TAILQ_INSERT_TAIL(dst, newop, link);
}
return (0);
fail:
rawop_clear_list(dst);
return (-1);
}
void
rawop_move_list(dst, src)
struct rawop_list *dst, *src;
{
struct rawoption *op;
/*
d_printf(LOG_INFO, FNAME,
" moving rawop list of %d from %p to %p",
rawop_count_list(src), (void*)src, (void*)dst);
*/
while ((op = TAILQ_FIRST(src)) != NULL) {
TAILQ_REMOVE(src, op, link);
TAILQ_INSERT_TAIL(dst, op, link);
}
}
int
dhcp6_copy_list(struct dhcp6_list *dst, struct dhcp6_list *src)
{
struct dhcp6_listval *ent;
for (ent = TAILQ_FIRST(src); ent; ent = TAILQ_NEXT(ent, link)) {
if (dhcp6_add_listval(dst, ent->type,
&ent->uv, &ent->sublist) == NULL)
goto fail;
}
return (0);
fail:
dhcp6_clear_list(dst);
return (-1);
}
void
dhcp6_move_list(struct dhcp6_list *dst, struct dhcp6_list *src)
{
struct dhcp6_listval *v;
while ((v = TAILQ_FIRST(src)) != NULL) {
TAILQ_REMOVE(src, v, link);
TAILQ_INSERT_TAIL(dst, v, link);
}
}
void
dhcp6_clear_list(struct dhcp6_list *head)
{
struct dhcp6_listval *v;
while ((v = TAILQ_FIRST(head)) != NULL) {
TAILQ_REMOVE(head, v, link);
dhcp6_clear_listval(v);
}
return;
}
static int
dhcp6_count_list(struct dhcp6_list *head)
{
struct dhcp6_listval *v;
int i;
for (i = 0, v = TAILQ_FIRST(head); v; v = TAILQ_NEXT(v, link))
i++;
return (i);
}
void
dhcp6_clear_listval(struct dhcp6_listval *lv)
{
dhcp6_clear_list(&lv->sublist);
switch (lv->type) {
case DHCP6_LISTVAL_VBUF:
dhcp6_vbuf_free(&lv->val_vbuf);
break;
default: /* nothing to do */
break;
}
free(lv);
}
/*
* Note: this function only searches for the first entry that matches
* VAL. It also does not care about sublists.
*/
struct dhcp6_listval *
dhcp6_find_listval(struct dhcp6_list *head, dhcp6_listval_type_t type,
void *val, int option)
{
struct dhcp6_listval *lv;
for (lv = TAILQ_FIRST(head); lv; lv = TAILQ_NEXT(lv, link)) {
if (lv->type != type)
continue;
switch(type) {
case DHCP6_LISTVAL_NUM:
if (lv->val_num == *(int *)val)
return (lv);
break;
case DHCP6_LISTVAL_STCODE:
if (lv->val_num16 == *(uint16_t *)val)
return (lv);
break;
case DHCP6_LISTVAL_ADDR6:
if (IN6_ARE_ADDR_EQUAL(&lv->val_addr6,
(struct in6_addr *)val)) {
return (lv);
}
break;
case DHCP6_LISTVAL_PREFIX6:
if ((option & MATCHLIST_PREFIXLEN) &&
lv->val_prefix6.plen ==
((struct dhcp6_prefix *)val)->plen) {
return (lv);
} else if (IN6_ARE_ADDR_EQUAL(&lv->val_prefix6.addr,
&((struct dhcp6_prefix *)val)->addr) &&
lv->val_prefix6.plen ==
((struct dhcp6_prefix *)val)->plen) {
return (lv);
}
break;
case DHCP6_LISTVAL_STATEFULADDR6:
if (IN6_ARE_ADDR_EQUAL(&lv->val_statefuladdr6.addr,
&((struct dhcp6_prefix *)val)->addr)) {
return (lv);
}
break;
case DHCP6_LISTVAL_IAPD:
case DHCP6_LISTVAL_IANA:
if (lv->val_ia.iaid ==
((struct dhcp6_ia *)val)->iaid) {
return (lv);
}
break;
case DHCP6_LISTVAL_VBUF:
if (dhcp6_vbuf_cmp(&lv->val_vbuf,
(struct dhcp6_vbuf *)val) == 0) {
return (lv);
}
break;
}
}
return (NULL);
}
struct dhcp6_listval *
dhcp6_add_listval(struct dhcp6_list *head, dhcp6_listval_type_t type,
void *val, struct dhcp6_list *sublist)
{
struct dhcp6_listval *lv = NULL;
if ((lv = malloc(sizeof(*lv))) == NULL) {
d_printf(LOG_ERR, FNAME,
"failed to allocate memory for list entry");
goto fail;
}
memset(lv, 0, sizeof(*lv));
lv->type = type;
TAILQ_INIT(&lv->sublist);
switch(type) {
case DHCP6_LISTVAL_NUM:
lv->val_num = *(int *)val;
break;
case DHCP6_LISTVAL_STCODE:
lv->val_num16 = *(uint16_t *)val;
break;
case DHCP6_LISTVAL_ADDR6:
lv->val_addr6 = *(struct in6_addr *)val;
break;
case DHCP6_LISTVAL_PREFIX6:
lv->val_prefix6 = *(struct dhcp6_prefix *)val;
break;
case DHCP6_LISTVAL_STATEFULADDR6:
lv->val_statefuladdr6 = *(struct dhcp6_statefuladdr *)val;
break;
case DHCP6_LISTVAL_IAPD:
case DHCP6_LISTVAL_IANA:
lv->val_ia = *(struct dhcp6_ia *)val;
break;
case DHCP6_LISTVAL_VBUF:
if (dhcp6_vbuf_copy(&lv->val_vbuf, (struct dhcp6_vbuf *)val))
goto fail;
break;
default:
d_printf(LOG_ERR, FNAME,
"unexpected list value type (%d)", type);
goto fail;
}
if (sublist && dhcp6_copy_list(&lv->sublist, sublist))
goto fail;
TAILQ_INSERT_TAIL(head, lv, link);
return (lv);
fail:
if (lv)
free(lv);
return (NULL);
}
int
dhcp6_vbuf_copy(struct dhcp6_vbuf *dst, struct dhcp6_vbuf *src)
{
dst->dv_buf = malloc(src->dv_len);
if (dst->dv_buf == NULL)
return (-1);
dst->dv_len = src->dv_len;
memcpy(dst->dv_buf, src->dv_buf, dst->dv_len);
return (0);
}
void
dhcp6_vbuf_free(struct dhcp6_vbuf *vbuf)
{
free(vbuf->dv_buf);
vbuf->dv_len = 0;
vbuf->dv_buf = NULL;
}
int
dhcp6_vbuf_cmp(struct dhcp6_vbuf *vb1, struct dhcp6_vbuf *vb2)
{
if (vb1->dv_len != vb2->dv_len)
return (vb1->dv_len - vb2->dv_len);
return (memcmp(vb1->dv_buf, vb2->dv_buf, vb1->dv_len));
}
static int
dhcp6_get_addr(int optlen, void *cp, dhcp6_listval_type_t type,
struct dhcp6_list *list)
{
char *val;
if (optlen % sizeof(struct in6_addr) || optlen == 0) {
d_printf(LOG_INFO, FNAME,
"malformed DHCP option: type %d, len %d", type, optlen);
return -1;
}
for (val = (char *)cp; val < (char *)cp + optlen;
val += sizeof(struct in6_addr)) {
struct in6_addr valaddr;
memcpy(&valaddr, val, sizeof(valaddr));
if (dhcp6_find_listval(list,
DHCP6_LISTVAL_ADDR6, &valaddr, 0)) {
d_printf(LOG_INFO, FNAME, "duplicated %s address (%s)",
dhcp6optstr(type), in6addr2str(&valaddr, 0));
continue;
}
if (dhcp6_add_listval(list, DHCP6_LISTVAL_ADDR6,
&valaddr, NULL) == NULL) {
d_printf(LOG_ERR, FNAME,
"failed to copy %s address", dhcp6optstr(type));
return -1;
}
}
return 0;
}
static int
dhcp6_set_addr(type, list, p, optep, len)
dhcp6_listval_type_t type;
struct dhcp6_list *list;
struct dhcp6opt **p, *optep;
int *len;
{
struct in6_addr *in6;
struct dhcp6_listval *d;
int optlen;
if (TAILQ_EMPTY(list))
return 0;
optlen = dhcp6_count_list(list) * sizeof(struct in6_addr);
if ((in6 = malloc(optlen)) == NULL) {
d_printf(LOG_ERR, FNAME,
"memory allocation failed for %s options",
dhcp6optstr(type));
return -1;
}
for (d = TAILQ_FIRST(list); d; d = TAILQ_NEXT(d, link), in6++)
memcpy(in6, &d->val_addr6, sizeof(*in6));
if (copy_option(type, optlen, (char *)in6, p, optep, len) != 0) {
free(in6);
return -1;
}
free(in6);
return 0;
}
static int
dhcp6_get_domain(optlen, cp, type, list)
int optlen;
void *cp;
dhcp6_listval_type_t type;
struct dhcp6_list *list;
{
char *val;
val = (char *)cp;
while (val < (char *)cp + optlen) {
struct dhcp6_vbuf vb;
char name[MAXDNAME + 1];
if (dnsdecode((u_char **)(void *)&val,
(u_char *)((char *)cp + optlen), name, sizeof(name)) == NULL) {
d_printf(LOG_INFO, FNAME, "failed to "
"decode a %s domain name",
dhcp6optstr(type));
d_printf(LOG_INFO, FNAME,
"malformed DHCP option: type %d, len %d",
type, optlen);
return -1;
}
vb.dv_len = strlen(name) + 1;
vb.dv_buf = name;
if (dhcp6_add_listval(list,
DHCP6_LISTVAL_VBUF, &vb, NULL) == NULL) {
d_printf(LOG_ERR, FNAME, "failed to "
"copy a %s domain name", dhcp6optstr(type));
return -1;
}
}
return 0;
}
static int
dhcp6_set_domain(type, list, p, optep, len)
dhcp6_listval_type_t type;
struct dhcp6_list *list;
struct dhcp6opt **p, *optep;
int *len;
{
int optlen = 0;
struct dhcp6_listval *d;
char *tmpbuf;
char name[MAXDNAME], *cp, *ep;
if (TAILQ_EMPTY(list))
return 0;
for (d = TAILQ_FIRST(list); d; d = TAILQ_NEXT(d, link))
optlen += (d->val_vbuf.dv_len + 1);
if (optlen == 0) {
return 0;
}
tmpbuf = NULL;
if ((tmpbuf = malloc(optlen)) == NULL) {
d_printf(LOG_ERR, FNAME, "memory allocation failed for "
"%s domain options", dhcp6optstr(type));
return -1;
}
cp = tmpbuf;
ep = cp + optlen;
for (d = TAILQ_FIRST(list); d; d = TAILQ_NEXT(d, link)) {
int nlen;
nlen = dnsencode((const char *)d->val_vbuf.dv_buf,
name, sizeof (name));
if (nlen < 0) {
d_printf(LOG_ERR, FNAME,
"failed to encode a %s domain name",
dhcp6optstr(type));
free(tmpbuf);
return -1;
}
if (ep - cp < nlen) {
d_printf(LOG_ERR, FNAME,
"buffer length for %s domain name is too short",
dhcp6optstr(type));
free(tmpbuf);
return -1;
}
memcpy(cp, name, nlen);
cp += nlen;
}
if (copy_option(type, cp - tmpbuf, tmpbuf, p, optep, len) != 0) {
free(tmpbuf);
return -1;
}
free(tmpbuf);
return 0;
}
struct dhcp6_event *
dhcp6_create_event(ifp, state)
struct dhcp6_if *ifp;
int state;
{
struct dhcp6_event *ev;
if ((ev = malloc(sizeof(*ev))) == NULL) {
d_printf(LOG_ERR, FNAME,
"failed to allocate memory for an event");
return (NULL);
}
memset(ev, 0, sizeof(*ev));
ev->ifp = ifp;
ev->state = state;
TAILQ_INIT(&ev->data_list);
return (ev);
}
void
dhcp6_remove_event(ev)
struct dhcp6_event *ev;
{
struct dhcp6_serverinfo *sp, *sp_next;
d_printf(LOG_DEBUG, FNAME, "removing an event on %s, state=%s",
ev->ifp->ifname, dhcp6_event_statestr(ev));
dhcp6_remove_evdata(ev);
duidfree(&ev->serverid);
if (ev->timer)
dhcp6_remove_timer(&ev->timer);
TAILQ_REMOVE(&ev->ifp->event_list, ev, link);
for (sp = ev->servers; sp; sp = sp_next) {
sp_next = sp->next;
d_printf(LOG_DEBUG, FNAME, "removing server (ID: %s)",
duidstr(&sp->optinfo.serverID));
dhcp6_clear_options(&sp->optinfo);
if (sp->authparam != NULL)
free(sp->authparam);
free(sp);
}
if (ev->authparam != NULL)
free(ev->authparam);
free(ev);
}
void
dhcp6_remove_evdata(ev)
struct dhcp6_event *ev;
{
struct dhcp6_eventdata *evd;
while ((evd = TAILQ_FIRST(&ev->data_list)) != NULL) {
TAILQ_REMOVE(&ev->data_list, evd, link);
if (evd->destructor)
(*evd->destructor)(evd);
free(evd);
}
}
struct authparam *
new_authparam(proto, alg, rdm)
int proto, alg, rdm;
{
struct authparam *authparam;
if ((authparam = malloc(sizeof(*authparam))) == NULL)
return (NULL);
memset(authparam, 0, sizeof(*authparam));
authparam->authproto = proto;
authparam->authalgorithm = alg;
authparam->authrdm = rdm;
authparam->key = NULL;
authparam->flags |= AUTHPARAM_FLAGS_NOPREVRD;
authparam->prevrd = 0;
return (authparam);
}
struct authparam *
copy_authparam(authparam)
struct authparam *authparam;
{
struct authparam *dst;
if ((dst = malloc(sizeof(*dst))) == NULL)
return (NULL);
memcpy(dst, authparam, sizeof(*dst));
return (dst);
}
/*
* Home-brew function of a 64-bit version of ntohl.
* XXX: is there any standard for this?
*/
#if (BYTE_ORDER == LITTLE_ENDIAN)
static __inline uint64_t
ntohq(uint64_t x)
{
return (uint64_t)ntohl((uint32_t)(x >> 32)) |
(int64_t)ntohl((uint32_t)(x & 0xffffffff)) << 32;
}
#else /* (BYTE_ORDER == LITTLE_ENDIAN) */
#define ntohq(x) (x)
#endif
int
dhcp6_auth_replaycheck(method, prev, current)
int method;
uint64_t prev, current;
{
char bufprev[] = "ffff ffff ffff ffff";
char bufcurrent[] = "ffff ffff ffff ffff";
if (method != DHCP6_AUTHRDM_MONOCOUNTER) {
d_printf(LOG_ERR, FNAME, "unsupported replay detection "
"method (%d)", method);
return (-1);
}
(void)sprint_uint64(bufprev, sizeof(bufprev), prev);
(void)sprint_uint64(bufcurrent, sizeof(bufcurrent), current);
d_printf(LOG_DEBUG, FNAME, "previous: %s, current: %s",
bufprev, bufcurrent);
prev = ntohq(prev);
current = ntohq(current);
/*
* we call the singular point guilty, since we cannot guess
* whether the serial number is increasing or not.
*/
if (prev == (current ^ 0x8000000000000000ULL)) {
d_printf(LOG_INFO, FNAME, "detected a singular point");
return (1);
}
return (((int64_t)(current - prev) > 0) ? 0 : 1);
}
int
getifaddr(addr, ifnam, prefix, plen, strong, ignoreflags)
struct in6_addr *addr;
char *ifnam;
struct in6_addr *prefix;
int plen;
int strong; /* if strong host model is required or not */
int ignoreflags;
{
struct ifaddrs *ifap, *ifa;
struct sockaddr_in6 sin6;
int error = -1;
if (getifaddrs(&ifap) != 0) {
d_printf(LOG_WARNING, FNAME,
"getifaddrs failed: %s", strerror(errno));
return (-1);
}
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
int s1, s2;
if (strong && strcmp(ifnam, ifa->ifa_name) != 0)
continue;
/* in any case, ignore interfaces in different scope zones. */
if ((s1 = in6_addrscopebyif(prefix, ifnam)) < 0 ||
(s2 = in6_addrscopebyif(prefix, ifa->ifa_name)) < 0 ||
s1 != s2)
continue;
if (ifa->ifa_addr->sa_family != AF_INET6)
continue;
#ifdef HAVE_SA_LEN
if (ifa->ifa_addr->sa_len > sizeof(sin6))
continue;
#endif
if (in6_matchflags(ifa->ifa_addr, ifa->ifa_name, ignoreflags))
continue;
memcpy(&sin6, ifa->ifa_addr, sysdep_sa_len(ifa->ifa_addr));
#ifdef __KAME__
if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
sin6.sin6_addr.s6_addr[2] = 0;
sin6.sin6_addr.s6_addr[3] = 0;
}
#endif
if (plen % 8 == 0) {
if (memcmp(&sin6.sin6_addr, prefix, plen / 8) != 0)
continue;
} else {
struct in6_addr a, m;
int i;
memcpy(&a, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
memset(&m, 0, sizeof(m));
memset(&m, 0xff, plen / 8);
m.s6_addr[plen / 8] = (0xff00 >> (plen % 8)) & 0xff;
for (i = 0; i < (int)sizeof(a); i++)
a.s6_addr[i] &= m.s6_addr[i];
if (memcmp(&a, prefix, plen / 8) != 0 ||
a.s6_addr[plen / 8] !=
(prefix->s6_addr[plen / 8] & m.s6_addr[plen / 8]))
continue;
}
memcpy(addr, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
#ifdef __KAME__
if (IN6_IS_ADDR_LINKLOCAL(addr))
addr->s6_addr[2] = addr->s6_addr[3] = 0;
#endif
error = 0;
break;
}
freeifaddrs(ifap);
return (error);
}
int
getifidfromaddr(addr, ifidp)
struct in6_addr *addr;
unsigned int *ifidp;
{
struct ifaddrs *ifap, *ifa;
struct sockaddr_in6 *sa6;
unsigned int ifid;
int retval = -1;
if (getifaddrs(&ifap) != 0) {
d_printf(LOG_WARNING, FNAME,
"getifaddrs failed: %s", strerror(errno));
return (-1);
}
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr->sa_family != AF_INET6)
continue;
sa6 = (struct sockaddr_in6 *)(void *)ifa->ifa_addr;
if (IN6_ARE_ADDR_EQUAL(addr, &sa6->sin6_addr))
break;
}
if (ifa != NULL) {
if ((ifid = if_nametoindex(ifa->ifa_name)) == 0) {
d_printf(LOG_ERR, FNAME,
"if_nametoindex failed for %s", ifa->ifa_name);
goto end;
}
retval = 0;
*ifidp = ifid;
}
end:
freeifaddrs(ifap);
return (retval);
}
int
in6_addrscopebyif(addr, ifnam)
struct in6_addr *addr;
char *ifnam;
{
u_int ifindex;
if ((ifindex = if_nametoindex(ifnam)) == 0)
return (-1);
if (IN6_IS_ADDR_LINKLOCAL(addr) || IN6_IS_ADDR_MC_LINKLOCAL(addr))
return (ifindex);
if (IN6_IS_ADDR_SITELOCAL(addr) || IN6_IS_ADDR_MC_SITELOCAL(addr))
return (1); /* XXX */
if (IN6_IS_ADDR_MC_ORGLOCAL(addr))
return (1); /* XXX */
return (1); /* treat it as global */
}
int
transmit_sa(s, sa, buf, len)
int s;
struct sockaddr *sa;
char *buf;
size_t len;
{
ssize_t error;
error = sendto(s, buf, len, 0, sa, sysdep_sa_len(sa));
return (error != (ssize_t)len) ? -1 : 0;
}
long
random_between(x, y)
long x;
long y;
{
long ratio;
ratio = 1 << 16;
while ((y - x) * ratio < (y - x))
ratio = ratio / 2;
return (x + ((y - x) * (ratio - 1) / random() & (ratio - 1)));
}
int
prefix6_mask(in6, plen)
struct in6_addr *in6;
int plen;
{
struct sockaddr_in6 mask6;
int i;
if (sa6_plen2mask(&mask6, plen))
return (-1);
for (i = 0; i < 16; i++)
in6->s6_addr[i] &= mask6.sin6_addr.s6_addr[i];
return (0);
}
int
sa6_plen2mask(sa6, plen)
struct sockaddr_in6 *sa6;
int plen;
{
u_char *cp;
if (plen < 0 || plen > 128)
return (-1);
memset(sa6, 0, sizeof(*sa6));
sa6->sin6_family = AF_INET6;
#ifdef HAVE_SA_LEN
sa6->sin6_len = sizeof(*sa6);
#endif
for (cp = (u_char *)&sa6->sin6_addr; plen > 7; plen -= 8)
*cp++ = 0xff;
*cp = 0xff << (8 - plen);
return (0);
}
char *
addr2str(sa)
struct sockaddr *sa;
{
static char addrbuf[8][NI_MAXHOST];
static int round = 0;
char *cp;
round = (round + 1) & 7;
cp = addrbuf[round];
getnameinfo(sa, sysdep_sa_len(sa), cp, NI_MAXHOST,
NULL, 0, NI_NUMERICHOST);
return (cp);
}
char *
in6addr2str(in6, scopeid)
struct in6_addr *in6;
int scopeid;
{
struct sockaddr_in6 sa6;
memset(&sa6, 0, sizeof(sa6));
sa6.sin6_family = AF_INET6;
#ifdef HAVE_SA_LEN
sa6.sin6_len = sizeof(sa6);
#endif
sa6.sin6_addr = *in6;
sa6.sin6_scope_id = scopeid;
return (addr2str((struct sockaddr *)&sa6));
}
/* return IPv6 address scope type. caller assumes that smaller is narrower. */