-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdhcp6s.c
3681 lines (3253 loc) · 98.6 KB
/
dhcp6s.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: dhcp6s.c,v 1.162 2005/10/04 11:53:32 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/uio.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 <errno.h>
#include <net/if.h>
#ifdef __FreeBSD__
#include <net/if_var.h>
#endif
#include <netinet/in.h>
#ifdef __KAME__
#include <netinet6/in6_var.h>
#endif
#include <arpa/inet.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 <limits.h>
#include <dhcp6.h>
#include <config.h>
#include <common.h>
#include <timer.h>
#include <auth.h>
#include <base64.h>
#include <control.h>
#include <dhcp6_ctl.h>
#include <signal.h>
#include <lease.h>
#define DUID_FILE LOCALDBDIR "/dhcp6s_duid"
#define DHCP6S_CONF SYSCONFDIR "/dhcp6s.conf"
#define DEFAULT_KEYFILE SYSCONFDIR "/dhcp6sctlkey"
#define DHCP6S_PIDFILE "/var/run/dhcp6s.pid"
#define CTLSKEW 300
typedef enum { DHCP6_BINDING_IA } dhcp6_bindingtype_t;
struct dhcp6_binding {
TAILQ_ENTRY(dhcp6_binding) link;
dhcp6_bindingtype_t type;
/* identifier of the binding */
struct duid clientid;
/* additional identifiers for IA-based bindings */
int iatype;
u_int32_t iaid;
/*
* configuration information of this binding,
* which is type-dependent.
*/
union {
struct dhcp6_list uv_list;
} val;
#define val_list val.uv_list
u_int32_t duration;
time_t updatetime;
struct dhcp6_timer *timer;
};
static TAILQ_HEAD(, dhcp6_binding) dhcp6_binding_head;
struct relayinfo {
TAILQ_ENTRY(relayinfo) link;
u_int hcnt; /* hop count */
struct in6_addr linkaddr; /* link address */
struct in6_addr peeraddr; /* peer address */
struct dhcp6_vbuf relay_ifid; /* Interface ID (if provided) */
struct dhcp6_vbuf relay_msg; /* relay message */
};
TAILQ_HEAD(relayinfolist, relayinfo);
static int debug = 0;
static sig_atomic_t sig_flags = 0;
#define SIGF_TERM 0x1
const dhcp6_mode_t dhcp6_mode = DHCP6_MODE_SERVER;
char *device = NULL;
int ifidx;
int insock; /* inbound UDP port */
int outsock; /* outbound UDP port */
int ctlsock = -1; /* control TCP port */
char *ctladdr = DEFAULT_SERVER_CONTROL_ADDR;
char *ctlport = DEFAULT_SERVER_CONTROL_PORT;
static const struct sockaddr_in6 *sa6_any_downstream, *sa6_any_relay;
static struct msghdr rmh;
static char rdatabuf[BUFSIZ];
static int rmsgctllen;
static char *conffile = DHCP6S_CONF;
static char *rmsgctlbuf;
static struct duid server_duid;
static struct dhcp6_list arg_dnslist;
static char *ctlkeyfile = DEFAULT_KEYFILE;
static struct keyinfo *ctlkey = NULL;
static int ctldigestlen;
static char *pid_file = DHCP6S_PIDFILE;
static inline int get_val32 __P((char **, int *, u_int32_t *));
static inline int get_val __P((char **, int *, void *, size_t));
static void usage __P((void));
static void server6_init __P((void));
static void server6_mainloop __P((void));
static int server6_do_ctlcommand __P((char *, ssize_t));
static void server6_reload __P((void));
static void server6_stop __P((void));
static void server6_recv __P((int));
static void process_signals __P((void));
static void server6_signal __P((int));
static void free_relayinfo __P((struct relayinfo *));
static int process_relayforw __P((struct dhcp6 **, struct dhcp6opt **,
struct relayinfolist *, struct sockaddr *));
static int set_statelessinfo __P((int, struct dhcp6_optinfo *));
static int react_solicit __P((struct dhcp6_if *, struct dhcp6 *, ssize_t,
struct dhcp6_optinfo *, struct sockaddr *, int, struct relayinfolist *));
static int react_request __P((struct dhcp6_if *, struct in6_pktinfo *,
struct dhcp6 *, ssize_t, struct dhcp6_optinfo *, struct sockaddr *, int,
struct relayinfolist *));
static int react_renew __P((struct dhcp6_if *, struct in6_pktinfo *,
struct dhcp6 *, ssize_t, struct dhcp6_optinfo *, struct sockaddr *, int,
struct relayinfolist *));
static int react_rebind __P((struct dhcp6_if *, struct dhcp6 *, ssize_t,
struct dhcp6_optinfo *, struct sockaddr *, int, struct relayinfolist *));
static int react_release __P((struct dhcp6_if *, struct in6_pktinfo *,
struct dhcp6 *, ssize_t, struct dhcp6_optinfo *, struct sockaddr *, int,
struct relayinfolist *));
static int react_decline __P((struct dhcp6_if *, struct in6_pktinfo *,
struct dhcp6 *, ssize_t, struct dhcp6_optinfo *, struct sockaddr *, int,
struct relayinfolist *));
static int react_confirm __P((struct dhcp6_if *, struct in6_pktinfo *,
struct dhcp6 *, ssize_t,
struct dhcp6_optinfo *, struct sockaddr *, int, struct relayinfolist *));
static int react_informreq __P((struct dhcp6_if *, struct dhcp6 *, ssize_t,
struct dhcp6_optinfo *, struct sockaddr *, int, struct relayinfolist *));
static int server6_send __P((int, struct dhcp6_if *, struct dhcp6 *,
struct dhcp6_optinfo *, struct sockaddr *, int, struct dhcp6_optinfo *,
struct relayinfolist *, struct host_conf *));
static int make_ia_stcode __P((int, u_int32_t, u_int16_t,
struct dhcp6_list *));
static int update_ia __P((int, struct dhcp6_listval *,
struct dhcp6_list *, struct dhcp6_optinfo *));
static int release_binding_ia __P((struct dhcp6_listval *, struct dhcp6_list *,
struct dhcp6_optinfo *));
static int decline_binding_ia __P((struct dhcp6_listval *, struct dhcp6_list *,
struct dhcp6_optinfo *));
static int make_ia __P((struct dhcp6_listval *, struct dhcp6_list *,
struct dhcp6_list *, struct host_conf *, int));
static int make_match_ia __P((struct dhcp6_listval *, struct dhcp6_list *,
struct dhcp6_list *));
static int make_iana_from_pool __P((struct dhcp6_poolspec *,
struct dhcp6_listval *, struct dhcp6_list *));
static void calc_ia_timo __P((struct dhcp6_ia *, struct dhcp6_list *,
struct host_conf *));
static void update_binding_duration __P((struct dhcp6_binding *));
static struct dhcp6_binding *add_binding __P((struct duid *,
dhcp6_bindingtype_t, int, u_int32_t, void *));
static struct dhcp6_binding *find_binding __P((struct duid *,
dhcp6_bindingtype_t, int, u_int32_t));
static void update_binding __P((struct dhcp6_binding *));
static void remove_binding __P((struct dhcp6_binding *));
static void free_binding __P((struct dhcp6_binding *));
static struct dhcp6_timer *binding_timo __P((void *));
static struct dhcp6_listval *find_binding_ia __P((struct dhcp6_listval *,
struct dhcp6_binding *));
static char *bindingstr __P((struct dhcp6_binding *));
static int process_auth __P((struct dhcp6 *, ssize_t, struct host_conf *,
struct dhcp6_optinfo *, struct dhcp6_optinfo *));
static inline char *clientstr __P((struct host_conf *, struct duid *));
int
main(argc, argv)
int argc;
char **argv;
{
int ch, pid;
struct in6_addr a;
struct dhcp6_listval *dlv;
char *progname;
FILE *pidfp;
if ((progname = strrchr(*argv, '/')) == NULL)
progname = *argv;
else
progname++;
TAILQ_INIT(&arg_dnslist);
TAILQ_INIT(&dnslist);
TAILQ_INIT(&dnsnamelist);
TAILQ_INIT(&siplist);
TAILQ_INIT(&sipnamelist);
TAILQ_INIT(&ntplist);
TAILQ_INIT(&nislist);
TAILQ_INIT(&nisnamelist);
TAILQ_INIT(&nisplist);
TAILQ_INIT(&nispnamelist);
TAILQ_INIT(&bcmcslist);
TAILQ_INIT(&bcmcsnamelist);
srandom(time(NULL) & getpid());
while ((ch = getopt(argc, argv, "c:dDfk:n:p:P:")) != -1) {
switch (ch) {
case 'c':
conffile = optarg;
break;
case 'd':
debug = 1;
break;
case 'D':
debug = 2;
break;
case 'f':
foreground++;
break;
case 'k':
ctlkeyfile = optarg;
break;
case 'n':
warnx("-n dnsserv option was obsoleted. "
"use configuration file.");
if (inet_pton(AF_INET6, optarg, &a) != 1) {
errx(1, "invalid DNS server %s", optarg);
/* NOTREACHED */
}
if ((dlv = malloc(sizeof *dlv)) == NULL) {
errx(1, "malloc failed for a DNS server");
/* NOTREACHED */
}
dlv->val_addr6 = a;
TAILQ_INSERT_TAIL(&arg_dnslist, dlv, link);
break;
case 'p':
ctlport = optarg;
break;
case 'P':
pid_file = optarg;
break;
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc != 1) {
usage();
/* NOTREACHED */
}
device = argv[0];
if (foreground == 0)
openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
setloglevel(debug);
if (ifinit(device) == NULL)
exit(1);
if ((cfparse(conffile)) != 0) {
dprintf(LOG_ERR, FNAME, "failed to parse configuration file");
exit(1);
}
if (foreground == 0) {
if (daemon(0, 0) < 0)
err(1, "daemon");
}
/* dump current PID */
pid = getpid();
if ((pidfp = fopen(pid_file, "w")) != NULL) {
fprintf(pidfp, "%d\n", pid);
fclose(pidfp);
}
/* prohibit a mixture of old and new style of DNS server config */
if (!TAILQ_EMPTY(&arg_dnslist)) {
if (!TAILQ_EMPTY(&dnslist)) {
dprintf(LOG_INFO, FNAME, "do not specify DNS servers "
"both by command line and by configuration file.");
exit(1);
}
dhcp6_move_list(&dnslist, &arg_dnslist);
TAILQ_INIT(&arg_dnslist);
}
server6_init();
server6_mainloop();
exit(0);
}
static void
usage()
{
fprintf(stderr,
"usage: dhcp6s [-c configfile] [-dDf] [-k ctlkeyfile] "
"[-p ctlport] [-P pidfile] intface\n");
exit(0);
}
/*------------------------------------------------------------*/
void
server6_init()
{
struct addrinfo hints;
struct addrinfo *res, *res2;
int error;
int on = 1;
struct ipv6_mreq mreq6;
static struct iovec iov;
static struct sockaddr_in6 sa6_any_downstream_storage;
static struct sockaddr_in6 sa6_any_relay_storage;
TAILQ_INIT(&dhcp6_binding_head);
if (lease_init() != 0) {
dprintf(LOG_ERR, FNAME, "failed to initialize the lease table");
exit(1);
}
ifidx = if_nametoindex(device);
if (ifidx == 0) {
dprintf(LOG_ERR, FNAME, "invalid interface %s", device);
exit(1);
}
/* get our DUID */
if (get_duid(DUID_FILE, &server_duid)) {
dprintf(LOG_ERR, FNAME, "failed to get a DUID");
exit(1);
}
if (dhcp6_ctl_authinit(ctlkeyfile, &ctlkey, &ctldigestlen) != 0) {
dprintf(LOG_NOTICE, FNAME,
"failed to initialize control message authentication");
/* run the server anyway */
}
/* initialize send/receive buffer */
iov.iov_base = (caddr_t)rdatabuf;
iov.iov_len = sizeof(rdatabuf);
rmh.msg_iov = &iov;
rmh.msg_iovlen = 1;
rmsgctllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
if ((rmsgctlbuf = (char *)malloc(rmsgctllen)) == NULL) {
dprintf(LOG_ERR, FNAME, "memory allocation failed");
exit(1);
}
/* initialize socket */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE;
error = getaddrinfo(NULL, DH6PORT_UPSTREAM, &hints, &res);
if (error) {
dprintf(LOG_ERR, FNAME, "getaddrinfo: %s",
gai_strerror(error));
exit(1);
}
insock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (insock < 0) {
dprintf(LOG_ERR, FNAME, "socket(insock): %s",
strerror(errno));
exit(1);
}
if (setsockopt(insock, SOL_SOCKET, SO_REUSEPORT, &on,
sizeof(on)) < 0) {
dprintf(LOG_ERR, FNAME, "setsockopt(insock, SO_REUSEPORT): %s",
strerror(errno));
exit(1);
}
if (setsockopt(insock, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on)) < 0) {
dprintf(LOG_ERR, FNAME, "setsockopt(insock, SO_REUSEADDR): %s",
strerror(errno));
exit(1);
}
#ifdef IPV6_RECVPKTINFO
if (setsockopt(insock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
sizeof(on)) < 0) {
dprintf(LOG_ERR, FNAME,
"setsockopt(inbound, IPV6_RECVPKTINFO): %s",
strerror(errno));
exit(1);
}
#else
if (setsockopt(insock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
sizeof(on)) < 0) {
dprintf(LOG_ERR, FNAME,
"setsockopt(inbound, IPV6_PKTINFO): %s",
strerror(errno));
exit(1);
}
#endif
#ifdef IPV6_V6ONLY
if (setsockopt(insock, IPPROTO_IPV6, IPV6_V6ONLY,
&on, sizeof(on)) < 0) {
dprintf(LOG_ERR, FNAME,
"setsockopt(inbound, IPV6_V6ONLY): %s", strerror(errno));
exit(1);
}
#endif
if (bind(insock, res->ai_addr, res->ai_addrlen) < 0) {
dprintf(LOG_ERR, FNAME, "bind(insock): %s", strerror(errno));
exit(1);
}
freeaddrinfo(res);
hints.ai_flags = 0;
error = getaddrinfo(DH6ADDR_ALLAGENT, DH6PORT_UPSTREAM, &hints, &res2);
if (error) {
dprintf(LOG_ERR, FNAME, "getaddrinfo: %s",
gai_strerror(error));
exit(1);
}
memset(&mreq6, 0, sizeof(mreq6));
mreq6.ipv6mr_interface = ifidx;
memcpy(&mreq6.ipv6mr_multiaddr,
&((struct sockaddr_in6 *)res2->ai_addr)->sin6_addr,
sizeof(mreq6.ipv6mr_multiaddr));
if (setsockopt(insock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
&mreq6, sizeof(mreq6))) {
dprintf(LOG_ERR, FNAME,
"setsockopt(insock, IPV6_JOIN_GROUP): %s",
strerror(errno));
exit(1);
}
freeaddrinfo(res2);
hints.ai_flags = 0;
error = getaddrinfo(DH6ADDR_ALLSERVER, DH6PORT_UPSTREAM,
&hints, &res2);
if (error) {
dprintf(LOG_ERR, FNAME, "getaddrinfo: %s",
gai_strerror(error));
exit(1);
}
memset(&mreq6, 0, sizeof(mreq6));
mreq6.ipv6mr_interface = ifidx;
memcpy(&mreq6.ipv6mr_multiaddr,
&((struct sockaddr_in6 *)res2->ai_addr)->sin6_addr,
sizeof(mreq6.ipv6mr_multiaddr));
if (setsockopt(insock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
&mreq6, sizeof(mreq6))) {
dprintf(LOG_ERR, FNAME,
"setsockopt(insock, IPV6_JOIN_GROUP): %s",
strerror(errno));
exit(1);
}
freeaddrinfo(res2);
hints.ai_flags = 0;
error = getaddrinfo(NULL, DH6PORT_DOWNSTREAM, &hints, &res);
if (error) {
dprintf(LOG_ERR, FNAME, "getaddrinfo: %s",
gai_strerror(error));
exit(1);
}
outsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (outsock < 0) {
dprintf(LOG_ERR, FNAME, "socket(outsock): %s",
strerror(errno));
exit(1);
}
/* set outgoing interface of multicast packets for DHCP reconfig */
if (setsockopt(outsock, IPPROTO_IPV6, IPV6_MULTICAST_IF,
&ifidx, sizeof(ifidx)) < 0) {
dprintf(LOG_ERR, FNAME,
"setsockopt(outsock, IPV6_MULTICAST_IF): %s",
strerror(errno));
exit(1);
}
#if !defined(__linux__) && !defined(__sun__)
/* make the socket write-only */
if (shutdown(outsock, 0)) {
dprintf(LOG_ERR, FNAME, "shutdown(outbound, 0): %s",
strerror(errno));
exit(1);
}
#endif
freeaddrinfo(res);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
error = getaddrinfo("::", DH6PORT_DOWNSTREAM, &hints, &res);
if (error) {
dprintf(LOG_ERR, FNAME, "getaddrinfo: %s",
gai_strerror(error));
exit(1);
}
memcpy(&sa6_any_downstream_storage, res->ai_addr, res->ai_addrlen);
sa6_any_downstream =
(const struct sockaddr_in6*)&sa6_any_downstream_storage;
freeaddrinfo(res);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
error = getaddrinfo("::", DH6PORT_UPSTREAM, &hints, &res);
if (error) {
dprintf(LOG_ERR, FNAME, "getaddrinfo: %s",
gai_strerror(error));
exit(1);
}
memcpy(&sa6_any_relay_storage, res->ai_addr, res->ai_addrlen);
sa6_any_relay =
(const struct sockaddr_in6*)&sa6_any_relay_storage;
freeaddrinfo(res);
/* set up control socket */
if (ctlkey == NULL)
dprintf(LOG_NOTICE, FNAME, "skip opening control port");
else if (dhcp6_ctl_init(ctladdr, ctlport,
DHCP6CTL_DEF_COMMANDQUEUELEN, &ctlsock)) {
dprintf(LOG_ERR, FNAME,
"failed to initialize control channel");
exit(1);
}
if (signal(SIGTERM, server6_signal) == SIG_ERR) {
dprintf(LOG_WARNING, FNAME, "failed to set signal: %s",
strerror(errno));
exit(1);
}
return;
}
static void
process_signals()
{
if ((sig_flags & SIGF_TERM)) {
unlink(pid_file);
exit(0);
}
}
static void
server6_mainloop()
{
struct timeval *w;
int ret;
fd_set r;
int maxsock;
while (1) {
if (sig_flags)
process_signals();
w = dhcp6_check_timer();
FD_ZERO(&r);
FD_SET(insock, &r);
maxsock = insock;
if (ctlsock >= 0) {
FD_SET(ctlsock, &r);
maxsock = (insock > ctlsock) ? insock : ctlsock;
(void)dhcp6_ctl_setreadfds(&r, &maxsock);
}
ret = select(maxsock + 1, &r, NULL, NULL, w);
switch (ret) {
case -1:
if (errno != EINTR) {
dprintf(LOG_ERR, FNAME, "select: %s",
strerror(errno));
exit(1);
}
continue;
case 0: /* timeout */
break;
default:
break;
}
if (FD_ISSET(insock, &r))
server6_recv(insock);
if (ctlsock >= 0) {
if (FD_ISSET(ctlsock, &r)) {
(void)dhcp6_ctl_acceptcommand(ctlsock,
server6_do_ctlcommand);
}
(void)dhcp6_ctl_readcommand(&r);
}
}
}
static inline int
get_val32(bpp, lenp, valp)
char **bpp;
int *lenp;
u_int32_t *valp;
{
char *bp = *bpp;
int len = *lenp;
u_int32_t i32;
if (len < sizeof(*valp))
return (-1);
memcpy(&i32, bp, sizeof(i32));
*valp = ntohl(i32);
*bpp = bp + sizeof(*valp);
*lenp = len - sizeof(*valp);
return (0);
}
static inline int
get_val(bpp, lenp, valp, vallen)
char **bpp;
int *lenp;
void *valp;
size_t vallen;
{
char *bp = *bpp;
int len = *lenp;
if (len < vallen)
return (-1);
memcpy(valp, bp, vallen);
*bpp = bp + vallen;
*lenp = len - vallen;
return (0);
}
static int
server6_do_ctlcommand(buf, len)
char *buf;
ssize_t len;
{
struct dhcp6ctl *ctlhead;
struct dhcp6ctl_iaspec iaspec;
u_int16_t command, version;
u_int32_t p32, iaid, duidlen, ts, ts0;
struct duid duid;
struct dhcp6_binding *binding;
int commandlen;
char *bp;
time_t now;
ctlhead = (struct dhcp6ctl *)buf;
command = ntohs(ctlhead->command);
commandlen = (int)(ntohs(ctlhead->len));
version = ntohs(ctlhead->version);
if (len != sizeof(struct dhcp6ctl) + commandlen) {
dprintf(LOG_ERR, FNAME,
"assumption failure: command length mismatch");
return (DHCP6CTL_R_FAILURE);
}
/* replay protection and message authentication */
if ((now = time(NULL)) < 0) {
dprintf(LOG_ERR, FNAME, "failed to get current time: %s",
strerror(errno));
return (DHCP6CTL_R_FAILURE);
}
ts0 = (u_int32_t)now;
ts = ntohl(ctlhead->timestamp);
if (ts + CTLSKEW < ts0 || (ts - CTLSKEW) > ts0) {
dprintf(LOG_INFO, FNAME, "timestamp is out of range");
return (DHCP6CTL_R_FAILURE);
}
if (ctlkey == NULL) { /* should not happen!! */
dprintf(LOG_ERR, FNAME, "no secret key for control channel");
return (DHCP6CTL_R_FAILURE);
}
if (dhcp6_verify_mac(buf, len, DHCP6CTL_AUTHPROTO_UNDEF,
DHCP6CTL_AUTHALG_HMACMD5, sizeof(*ctlhead), ctlkey) != 0) {
dprintf(LOG_INFO, FNAME, "authentication failure");
return (DHCP6CTL_R_FAILURE);
}
bp = buf + sizeof(*ctlhead) + ctldigestlen;
commandlen -= ctldigestlen;
if (version > DHCP6CTL_VERSION) {
dprintf(LOG_INFO, FNAME, "unsupported version: %d", version);
return (DHCP6CTL_R_FAILURE);
}
switch (command) {
case DHCP6CTL_COMMAND_RELOAD:
if (commandlen != 0) {
dprintf(LOG_INFO, FNAME, "invalid command length "
"for reload: %d", commandlen);
return (DHCP6CTL_R_DONE);
}
server6_reload();
break;
case DHCP6CTL_COMMAND_STOP:
if (commandlen != 0) {
dprintf(LOG_INFO, FNAME, "invalid command length "
"for stop: %d", commandlen);
return (DHCP6CTL_R_DONE);
}
server6_stop();
break;
case DHCP6CTL_COMMAND_REMOVE:
if (get_val32(&bp, &commandlen, &p32))
return (DHCP6CTL_R_FAILURE);
if (p32 != DHCP6CTL_BINDING) {
dprintf(LOG_INFO, FNAME,
"unknown remove target: %ul", p32);
return (DHCP6CTL_R_FAILURE);
}
if (get_val32(&bp, &commandlen, &p32))
return (DHCP6CTL_R_FAILURE);
if (p32 != DHCP6CTL_BINDING_IA) {
dprintf(LOG_INFO, FNAME, "unknown binding type: %ul",
p32);
return (DHCP6CTL_R_FAILURE);
}
if (get_val(&bp, &commandlen, &iaspec, sizeof(iaspec)))
return (DHCP6CTL_R_FAILURE);
if (ntohl(iaspec.type) != DHCP6CTL_IA_PD &&
ntohl(iaspec.type) != DHCP6CTL_IA_NA) {
dprintf(LOG_INFO, FNAME, "unknown IA type: %ul",
ntohl(iaspec.type));
return (DHCP6CTL_R_FAILURE);
}
iaid = ntohl(iaspec.id);
duidlen = ntohl(iaspec.duidlen);
if (duidlen > commandlen) {
dprintf(LOG_INFO, FNAME, "DUID length mismatch");
return (DHCP6CTL_R_FAILURE);
}
duid.duid_len = (size_t)duidlen;
duid.duid_id = bp;
binding = find_binding(&duid, DHCP6_BINDING_IA,
DHCP6_LISTVAL_IAPD, iaid);
if (binding == NULL) {
binding = find_binding(&duid, DHCP6_BINDING_IA,
DHCP6_LISTVAL_IANA, iaid);
if (binding == NULL) {
dprintf(LOG_INFO, FNAME, "no such binding");
return (DHCP6CTL_R_FAILURE);
}
}
remove_binding(binding);
break;
default:
dprintf(LOG_INFO, FNAME,
"unknown control command: %d (len=%d)",
(int)command, commandlen);
return (DHCP6CTL_R_FAILURE);
}
return (DHCP6CTL_R_DONE);
}
static void
server6_reload()
{
/* reload the configuration file */
if (cfparse(conffile) != 0) {
dprintf(LOG_WARNING, FNAME,
"failed to reload configuration file");
return;
}
dprintf(LOG_NOTICE, FNAME, "server reloaded");
return;
}
static void
server6_stop()
{
/* Right now, we simply stop running */
dprintf(LOG_NOTICE, FNAME, "exiting");
exit (0);
}
static void
server6_recv(s)
int s;
{
ssize_t len;
struct sockaddr_storage from;
int fromlen;
struct msghdr mhdr;
struct iovec iov;
char cmsgbuf[BUFSIZ];
struct cmsghdr *cm;
struct in6_pktinfo *pi = NULL;
struct dhcp6_if *ifp;
struct dhcp6 *dh6;
struct dhcp6_optinfo optinfo;
struct dhcp6opt *optend;
struct relayinfolist relayinfohead;
struct relayinfo *relayinfo;
TAILQ_INIT(&relayinfohead);
memset(&iov, 0, sizeof(iov));
memset(&mhdr, 0, sizeof(mhdr));
iov.iov_base = rdatabuf;
iov.iov_len = sizeof(rdatabuf);
mhdr.msg_name = &from;
mhdr.msg_namelen = sizeof(from);
mhdr.msg_iov = &iov;
mhdr.msg_iovlen = 1;
mhdr.msg_control = (caddr_t)cmsgbuf;
mhdr.msg_controllen = sizeof(cmsgbuf);
if ((len = recvmsg(insock, &mhdr, 0)) < 0) {
dprintf(LOG_ERR, FNAME, "recvmsg: %s", strerror(errno));
return;
}
fromlen = mhdr.msg_namelen;
for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&mhdr); cm;
cm = (struct cmsghdr *)CMSG_NXTHDR(&mhdr, cm)) {
if (cm->cmsg_level == IPPROTO_IPV6 &&
cm->cmsg_type == IPV6_PKTINFO &&
cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
}
}
if (pi == NULL) {
dprintf(LOG_NOTICE, FNAME, "failed to get packet info");
return;
}
/*
* DHCPv6 server may receive a DHCPv6 packet from a non-listening
* interface, when a DHCPv6 relay agent is running on that interface.
* This check prevents such reception.
*/
if (pi->ipi6_ifindex != ifidx)
return;
if ((ifp = find_ifconfbyid((unsigned int)pi->ipi6_ifindex)) == NULL) {
dprintf(LOG_INFO, FNAME, "unexpected interface (%d)",
(unsigned int)pi->ipi6_ifindex);
return;
}
dh6 = (struct dhcp6 *)rdatabuf;
if (len < sizeof(*dh6)) {
dprintf(LOG_INFO, FNAME, "short packet (%d bytes)", len);
return;
}
dprintf(LOG_DEBUG, FNAME, "received %s from %s",
dhcp6msgstr(dh6->dh6_msgtype),
addr2str((struct sockaddr *)&from));
/*
* A server MUST discard any Solicit, Confirm, Rebind or
* Information-request messages it receives with a unicast
* destination address.
* [RFC3315 Section 15.]
*/
if (!IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) &&
(dh6->dh6_msgtype == DH6_SOLICIT ||
dh6->dh6_msgtype == DH6_CONFIRM ||
dh6->dh6_msgtype == DH6_REBIND ||
dh6->dh6_msgtype == DH6_INFORM_REQ)) {
dprintf(LOG_INFO, FNAME, "invalid unicast message");
return;
}
/*
* A server never receives a relay reply message. Since relay
* replay messages will annoy option parser below, we explicitly
* reject them here.
*/
if (dh6->dh6_msgtype == DH6_RELAY_REPLY) {
dprintf(LOG_INFO, FNAME, "relay reply message from %s",
addr2str((struct sockaddr *)&from));
return;
}
optend = (struct dhcp6opt *)(rdatabuf + len);
if (dh6->dh6_msgtype == DH6_RELAY_FORW) {
if (process_relayforw(&dh6, &optend, &relayinfohead,
(struct sockaddr *)&from)) {
goto end;
}
/* dh6 and optend should have been updated. */
len = (ssize_t)((char *)optend - (char *)dh6);
}
/*
* parse and validate options in the message
*/
dhcp6_init_options(&optinfo);
if (dhcp6_get_options((struct dhcp6opt *)(dh6 + 1),
optend, &optinfo) < 0) {
dprintf(LOG_INFO, FNAME, "failed to parse options");
goto end;
}
switch (dh6->dh6_msgtype) {
case DH6_SOLICIT:
(void)react_solicit(ifp, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_REQUEST:
(void)react_request(ifp, pi, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_RENEW:
(void)react_renew(ifp, pi, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_REBIND:
(void)react_rebind(ifp, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_RELEASE:
(void)react_release(ifp, pi, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_DECLINE:
(void)react_decline(ifp, pi, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_CONFIRM:
(void)react_confirm(ifp, pi, dh6, len, &optinfo,
(struct sockaddr *)&from, fromlen, &relayinfohead);
break;
case DH6_INFORM_REQ: