1 /* $OpenBSD: rad.c,v 1.28 2022/10/15 13:26:15 florian Exp $ */ 2 3 /* 4 * Copyright (c) 2018 Florian Obser <florian@openbsd.org> 5 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> 6 * Copyright (c) 2004 Esben Norby <norby@openbsd.org> 7 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 8 * 9 * Permission to use, copy, modify, and distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 */ 21 #include <sys/types.h> 22 #include <sys/queue.h> 23 #include <sys/socket.h> 24 #include <sys/syslog.h> 25 #include <sys/uio.h> 26 #include <sys/wait.h> 27 28 #include <netinet/in.h> 29 #include <net/if.h> 30 #include <net/route.h> 31 #include <netinet/in.h> 32 #include <netinet/if_ether.h> 33 #include <netinet6/in6_var.h> 34 #include <netinet/icmp6.h> 35 36 #include <err.h> 37 #include <errno.h> 38 #include <event.h> 39 #include <fcntl.h> 40 #include <imsg.h> 41 #include <netdb.h> 42 #include <pwd.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <signal.h> 47 #include <unistd.h> 48 49 #include "log.h" 50 #include "rad.h" 51 #include "frontend.h" 52 #include "engine.h" 53 #include "control.h" 54 55 enum rad_process { 56 PROC_MAIN, 57 PROC_ENGINE, 58 PROC_FRONTEND 59 }; 60 61 __dead void usage(void); 62 __dead void main_shutdown(void); 63 64 void main_sig_handler(int, short, void *); 65 66 static pid_t start_child(enum rad_process, char *, int, int, int); 67 68 void main_dispatch_frontend(int, short, void *); 69 void main_dispatch_engine(int, short, void *); 70 void open_icmp6sock(int); 71 72 static int main_imsg_send_ipc_sockets(struct imsgbuf *, struct imsgbuf *); 73 static int main_imsg_send_config(struct rad_conf *); 74 75 int main_reload(void); 76 int main_sendboth(enum imsg_type, void *, uint16_t); 77 78 void in6_prefixlen2mask(struct in6_addr *, int len); 79 80 struct rad_conf *main_conf; 81 static struct imsgev *iev_frontend; 82 static struct imsgev *iev_engine; 83 char *conffile; 84 pid_t frontend_pid; 85 pid_t engine_pid; 86 uint32_t cmd_opts; 87 88 void 89 main_sig_handler(int sig, short event, void *arg) 90 { 91 /* 92 * Normal signal handler rules don't apply because libevent 93 * decouples for us. 94 */ 95 96 switch (sig) { 97 case SIGTERM: 98 case SIGINT: 99 main_shutdown(); 100 break; 101 case SIGHUP: 102 if (main_reload() == -1) 103 log_warnx("configuration reload failed"); 104 else 105 log_debug("configuration reloaded"); 106 break; 107 default: 108 fatalx("unexpected signal"); 109 } 110 } 111 112 __dead void 113 usage(void) 114 { 115 extern char *__progname; 116 117 fprintf(stderr, "usage: %s [-dnv] [-f file] [-s socket]\n", 118 __progname); 119 exit(1); 120 } 121 122 int 123 main(int argc, char *argv[]) 124 { 125 struct event ev_sigint, ev_sigterm, ev_sighup; 126 int ch; 127 int debug = 0, engine_flag = 0, frontend_flag = 0; 128 char *saved_argv0; 129 int pipe_main2frontend[2]; 130 int pipe_main2engine[2]; 131 int frontend_routesock, rtfilter; 132 int rtable_any = RTABLE_ANY; 133 int control_fd; 134 char *csock; 135 136 conffile = _PATH_CONF_FILE; 137 csock = _PATH_RAD_SOCKET; 138 139 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */ 140 log_setverbose(1); 141 142 saved_argv0 = argv[0]; 143 if (saved_argv0 == NULL) 144 saved_argv0 = "rad"; 145 146 while ((ch = getopt(argc, argv, "dEFf:ns:v")) != -1) { 147 switch (ch) { 148 case 'd': 149 debug = 1; 150 break; 151 case 'E': 152 engine_flag = 1; 153 break; 154 case 'F': 155 frontend_flag = 1; 156 break; 157 case 'f': 158 conffile = optarg; 159 break; 160 case 'n': 161 cmd_opts |= OPT_NOACTION; 162 break; 163 case 's': 164 csock = optarg; 165 break; 166 case 'v': 167 if (cmd_opts & OPT_VERBOSE) 168 cmd_opts |= OPT_VERBOSE2; 169 cmd_opts |= OPT_VERBOSE; 170 break; 171 default: 172 usage(); 173 } 174 } 175 176 argc -= optind; 177 argv += optind; 178 if (argc > 0 || (engine_flag && frontend_flag)) 179 usage(); 180 181 if (engine_flag) 182 engine(debug, cmd_opts & OPT_VERBOSE); 183 else if (frontend_flag) 184 frontend(debug, cmd_opts & OPT_VERBOSE); 185 186 /* parse config file */ 187 if ((main_conf = parse_config(conffile)) == NULL) { 188 exit(1); 189 } 190 191 if (cmd_opts & OPT_NOACTION) { 192 if (cmd_opts & OPT_VERBOSE) 193 print_config(main_conf); 194 else 195 fprintf(stderr, "configuration OK\n"); 196 exit(0); 197 } 198 199 /* Check for root privileges. */ 200 if (geteuid()) 201 errx(1, "need root privileges"); 202 203 /* Check for assigned daemon user */ 204 if (getpwnam(RAD_USER) == NULL) 205 errx(1, "unknown user %s", RAD_USER); 206 207 log_init(debug, LOG_DAEMON); 208 log_setverbose(cmd_opts & OPT_VERBOSE); 209 210 if (!debug) 211 daemon(1, 0); 212 213 log_info("startup"); 214 215 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 216 PF_UNSPEC, pipe_main2frontend) == -1) 217 fatal("main2frontend socketpair"); 218 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 219 PF_UNSPEC, pipe_main2engine) == -1) 220 fatal("main2engine socketpair"); 221 222 /* Start children. */ 223 engine_pid = start_child(PROC_ENGINE, saved_argv0, pipe_main2engine[1], 224 debug, cmd_opts & OPT_VERBOSE); 225 frontend_pid = start_child(PROC_FRONTEND, saved_argv0, 226 pipe_main2frontend[1], debug, cmd_opts & OPT_VERBOSE); 227 228 log_procinit("main"); 229 230 event_init(); 231 232 /* Setup signal handler. */ 233 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL); 234 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL); 235 signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL); 236 signal_add(&ev_sigint, NULL); 237 signal_add(&ev_sigterm, NULL); 238 signal_add(&ev_sighup, NULL); 239 signal(SIGPIPE, SIG_IGN); 240 241 /* Setup pipes to children. */ 242 243 if ((iev_frontend = malloc(sizeof(struct imsgev))) == NULL || 244 (iev_engine = malloc(sizeof(struct imsgev))) == NULL) 245 fatal(NULL); 246 imsg_init(&iev_frontend->ibuf, pipe_main2frontend[0]); 247 iev_frontend->handler = main_dispatch_frontend; 248 imsg_init(&iev_engine->ibuf, pipe_main2engine[0]); 249 iev_engine->handler = main_dispatch_engine; 250 251 /* Setup event handlers for pipes to engine & frontend. */ 252 iev_frontend->events = EV_READ; 253 event_set(&iev_frontend->ev, iev_frontend->ibuf.fd, 254 iev_frontend->events, iev_frontend->handler, iev_frontend); 255 event_add(&iev_frontend->ev, NULL); 256 257 iev_engine->events = EV_READ; 258 event_set(&iev_engine->ev, iev_engine->ibuf.fd, iev_engine->events, 259 iev_engine->handler, iev_engine); 260 event_add(&iev_engine->ev, NULL); 261 262 if (main_imsg_send_ipc_sockets(&iev_frontend->ibuf, &iev_engine->ibuf)) 263 fatal("could not establish imsg links"); 264 265 if ((frontend_routesock = socket(AF_ROUTE, SOCK_RAW | SOCK_CLOEXEC, 266 AF_INET6)) == -1) 267 fatal("route socket"); 268 269 rtfilter = ROUTE_FILTER(RTM_IFINFO) | ROUTE_FILTER(RTM_NEWADDR) | 270 ROUTE_FILTER(RTM_DELADDR) | ROUTE_FILTER(RTM_CHGADDRATTR); 271 if (setsockopt(frontend_routesock, AF_ROUTE, ROUTE_MSGFILTER, 272 &rtfilter, sizeof(rtfilter)) == -1) 273 fatal("setsockopt(ROUTE_MSGFILTER)"); 274 if (setsockopt(frontend_routesock, AF_ROUTE, ROUTE_TABLEFILTER, 275 &rtable_any, sizeof(rtable_any)) == -1) 276 fatal("setsockopt(ROUTE_TABLEFILTER)"); 277 278 if ((control_fd = control_init(csock)) == -1) 279 fatalx("control socket setup failed"); 280 281 main_imsg_compose_frontend(IMSG_ROUTESOCK, frontend_routesock, 282 NULL, 0); 283 main_imsg_compose_frontend(IMSG_CONTROLFD, control_fd, NULL, 0); 284 main_imsg_send_config(main_conf); 285 286 if (pledge("stdio inet rpath sendfd mcast wroute", NULL) == -1) 287 fatal("pledge"); 288 289 main_imsg_compose_frontend(IMSG_STARTUP, -1, NULL, 0); 290 291 event_dispatch(); 292 293 main_shutdown(); 294 return (0); 295 } 296 297 __dead void 298 main_shutdown(void) 299 { 300 pid_t pid; 301 int status; 302 303 /* Close pipes. */ 304 msgbuf_clear(&iev_frontend->ibuf.w); 305 close(iev_frontend->ibuf.fd); 306 msgbuf_clear(&iev_engine->ibuf.w); 307 close(iev_engine->ibuf.fd); 308 309 config_clear(main_conf); 310 311 log_debug("waiting for children to terminate"); 312 do { 313 pid = wait(&status); 314 if (pid == -1) { 315 if (errno != EINTR && errno != ECHILD) 316 fatal("wait"); 317 } else if (WIFSIGNALED(status)) 318 log_warnx("%s terminated; signal %d", 319 (pid == engine_pid) ? "engine" : 320 "frontend", WTERMSIG(status)); 321 } while (pid != -1 || (pid == -1 && errno == EINTR)); 322 323 free(iev_frontend); 324 free(iev_engine); 325 326 log_info("terminating"); 327 exit(0); 328 } 329 330 static pid_t 331 start_child(enum rad_process p, char *argv0, int fd, int debug, int verbose) 332 { 333 char *argv[6]; 334 int argc = 0; 335 pid_t pid; 336 337 switch (pid = fork()) { 338 case -1: 339 fatal("cannot fork"); 340 case 0: 341 break; 342 default: 343 close(fd); 344 return (pid); 345 } 346 347 if (fd != 3) { 348 if (dup2(fd, 3) == -1) 349 fatal("cannot setup imsg fd"); 350 } else if (fcntl(fd, F_SETFD, 0) == -1) 351 fatal("cannot setup imsg fd"); 352 353 argv[argc++] = argv0; 354 switch (p) { 355 case PROC_MAIN: 356 fatalx("Can not start main process"); 357 case PROC_ENGINE: 358 argv[argc++] = "-E"; 359 break; 360 case PROC_FRONTEND: 361 argv[argc++] = "-F"; 362 break; 363 } 364 if (debug) 365 argv[argc++] = "-d"; 366 if (verbose) 367 argv[argc++] = "-v"; 368 argv[argc++] = NULL; 369 370 execvp(argv0, argv); 371 fatal("execvp"); 372 } 373 374 void 375 main_dispatch_frontend(int fd, short event, void *bula) 376 { 377 struct imsgev *iev = bula; 378 struct imsgbuf *ibuf; 379 struct imsg imsg; 380 ssize_t n; 381 int shut = 0, verbose; 382 int rdomain; 383 384 ibuf = &iev->ibuf; 385 386 if (event & EV_READ) { 387 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 388 fatal("imsg_read error"); 389 if (n == 0) /* Connection closed. */ 390 shut = 1; 391 } 392 if (event & EV_WRITE) { 393 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 394 fatal("msgbuf_write"); 395 if (n == 0) /* Connection closed. */ 396 shut = 1; 397 } 398 399 for (;;) { 400 if ((n = imsg_get(ibuf, &imsg)) == -1) 401 fatal("imsg_get"); 402 if (n == 0) /* No more messages. */ 403 break; 404 405 switch (imsg.hdr.type) { 406 case IMSG_OPEN_ICMP6SOCK: 407 log_debug("IMSG_OPEN_ICMP6SOCK"); 408 if (IMSG_DATA_SIZE(imsg) != sizeof(rdomain)) 409 fatalx("%s: IMSG_OPEN_ICMP6SOCK wrong length: " 410 "%lu", __func__, IMSG_DATA_SIZE(imsg)); 411 memcpy(&rdomain, imsg.data, sizeof(rdomain)); 412 open_icmp6sock(rdomain); 413 break; 414 case IMSG_CTL_RELOAD: 415 if (main_reload() == -1) 416 log_warnx("configuration reload failed"); 417 else 418 log_warnx("configuration reloaded"); 419 break; 420 case IMSG_CTL_LOG_VERBOSE: 421 if (IMSG_DATA_SIZE(imsg) != sizeof(verbose)) 422 fatalx("%s: IMSG_CTL_LOG_VERBOSE wrong length: " 423 "%lu", __func__, IMSG_DATA_SIZE(imsg)); 424 memcpy(&verbose, imsg.data, sizeof(verbose)); 425 log_setverbose(verbose); 426 break; 427 default: 428 log_debug("%s: error handling imsg %d", __func__, 429 imsg.hdr.type); 430 break; 431 } 432 imsg_free(&imsg); 433 } 434 if (!shut) 435 imsg_event_add(iev); 436 else { 437 /* This pipe is dead. Remove its event handler */ 438 event_del(&iev->ev); 439 event_loopexit(NULL); 440 } 441 } 442 443 void 444 main_dispatch_engine(int fd, short event, void *bula) 445 { 446 struct imsgev *iev = bula; 447 struct imsgbuf *ibuf; 448 struct imsg imsg; 449 ssize_t n; 450 int shut = 0; 451 452 ibuf = &iev->ibuf; 453 454 if (event & EV_READ) { 455 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 456 fatal("imsg_read error"); 457 if (n == 0) /* Connection closed. */ 458 shut = 1; 459 } 460 if (event & EV_WRITE) { 461 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 462 fatal("msgbuf_write"); 463 if (n == 0) /* Connection closed. */ 464 shut = 1; 465 } 466 467 for (;;) { 468 if ((n = imsg_get(ibuf, &imsg)) == -1) 469 fatal("imsg_get"); 470 if (n == 0) /* No more messages. */ 471 break; 472 473 switch (imsg.hdr.type) { 474 default: 475 log_debug("%s: error handling imsg %d", __func__, 476 imsg.hdr.type); 477 break; 478 } 479 imsg_free(&imsg); 480 } 481 if (!shut) 482 imsg_event_add(iev); 483 else { 484 /* This pipe is dead. Remove its event handler. */ 485 event_del(&iev->ev); 486 event_loopexit(NULL); 487 } 488 } 489 490 int 491 main_imsg_compose_frontend(int type, int fd, void *data, uint16_t datalen) 492 { 493 if (iev_frontend) 494 return (imsg_compose_event(iev_frontend, type, 0, 0, fd, data, 495 datalen)); 496 else 497 return (-1); 498 } 499 500 void 501 main_imsg_compose_engine(int type, pid_t pid, void *data, uint16_t datalen) 502 { 503 if (iev_engine) 504 imsg_compose_event(iev_engine, type, 0, pid, -1, data, 505 datalen); 506 } 507 508 void 509 imsg_event_add(struct imsgev *iev) 510 { 511 iev->events = EV_READ; 512 if (iev->ibuf.w.queued) 513 iev->events |= EV_WRITE; 514 515 event_del(&iev->ev); 516 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev); 517 event_add(&iev->ev, NULL); 518 } 519 520 int 521 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid, 522 pid_t pid, int fd, void *data, uint16_t datalen) 523 { 524 int ret; 525 526 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, 527 datalen)) != -1) 528 imsg_event_add(iev); 529 530 return (ret); 531 } 532 533 static int 534 main_imsg_send_ipc_sockets(struct imsgbuf *frontend_buf, 535 struct imsgbuf *engine_buf) 536 { 537 int pipe_frontend2engine[2]; 538 539 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 540 PF_UNSPEC, pipe_frontend2engine) == -1) 541 return (-1); 542 543 if (imsg_compose(frontend_buf, IMSG_SOCKET_IPC, 0, 0, 544 pipe_frontend2engine[0], NULL, 0) == -1) 545 return (-1); 546 if (imsg_compose(engine_buf, IMSG_SOCKET_IPC, 0, 0, 547 pipe_frontend2engine[1], NULL, 0) == -1) 548 return (-1); 549 550 return (0); 551 } 552 553 int 554 main_reload(void) 555 { 556 struct rad_conf *xconf; 557 558 if ((xconf = parse_config(conffile)) == NULL) 559 return (-1); 560 561 if (main_imsg_send_config(xconf) == -1) 562 return (-1); 563 564 merge_config(main_conf, xconf); 565 566 return (0); 567 } 568 569 int 570 main_imsg_send_config(struct rad_conf *xconf) 571 { 572 struct ra_iface_conf *ra_iface_conf; 573 struct ra_prefix_conf *ra_prefix_conf; 574 struct ra_rdnss_conf *ra_rdnss_conf; 575 struct ra_dnssl_conf *ra_dnssl_conf; 576 struct ra_pref64_conf *pref64; 577 578 /* Send fixed part of config to children. */ 579 if (main_sendboth(IMSG_RECONF_CONF, xconf, sizeof(*xconf)) == -1) 580 return (-1); 581 582 /* send global dns options to children */ 583 SIMPLEQ_FOREACH(ra_rdnss_conf, &xconf->ra_options.ra_rdnss_list, 584 entry) { 585 if (main_sendboth(IMSG_RECONF_RA_RDNSS, ra_rdnss_conf, 586 sizeof(*ra_rdnss_conf)) == -1) 587 return (-1); 588 } 589 SIMPLEQ_FOREACH(ra_dnssl_conf, &xconf->ra_options.ra_dnssl_list, 590 entry) { 591 if (main_sendboth(IMSG_RECONF_RA_DNSSL, ra_dnssl_conf, 592 sizeof(*ra_dnssl_conf)) == -1) 593 return (-1); 594 } 595 596 /* send global pref64 list to children */ 597 SIMPLEQ_FOREACH(pref64, &xconf->ra_options.ra_pref64_list, 598 entry) { 599 if (main_sendboth(IMSG_RECONF_RA_PREF64, pref64, 600 sizeof(*pref64)) == -1) 601 return (-1); 602 } 603 604 /* Send the interface list to children. */ 605 SIMPLEQ_FOREACH(ra_iface_conf, &xconf->ra_iface_list, entry) { 606 if (main_sendboth(IMSG_RECONF_RA_IFACE, ra_iface_conf, 607 sizeof(*ra_iface_conf)) == -1) 608 return (-1); 609 if (ra_iface_conf->autoprefix) { 610 if (main_sendboth(IMSG_RECONF_RA_AUTOPREFIX, 611 ra_iface_conf->autoprefix, 612 sizeof(*ra_iface_conf->autoprefix)) == -1) 613 return (-1); 614 } 615 SIMPLEQ_FOREACH(ra_prefix_conf, &ra_iface_conf->ra_prefix_list, 616 entry) { 617 if (main_sendboth(IMSG_RECONF_RA_PREFIX, 618 ra_prefix_conf, sizeof(*ra_prefix_conf)) == -1) 619 return (-1); 620 } 621 SIMPLEQ_FOREACH(ra_rdnss_conf, 622 &ra_iface_conf->ra_options.ra_rdnss_list, entry) { 623 if (main_sendboth(IMSG_RECONF_RA_RDNSS, ra_rdnss_conf, 624 sizeof(*ra_rdnss_conf)) == -1) 625 return (-1); 626 } 627 SIMPLEQ_FOREACH(ra_dnssl_conf, 628 &ra_iface_conf->ra_options.ra_dnssl_list, entry) { 629 if (main_sendboth(IMSG_RECONF_RA_DNSSL, ra_dnssl_conf, 630 sizeof(*ra_dnssl_conf)) == -1) 631 return (-1); 632 } 633 SIMPLEQ_FOREACH(pref64, 634 &ra_iface_conf->ra_options.ra_pref64_list, entry) { 635 if (main_sendboth(IMSG_RECONF_RA_PREF64, pref64, 636 sizeof(*pref64)) == -1) 637 return (-1); 638 } 639 } 640 641 /* Tell children the revised config is now complete. */ 642 if (main_sendboth(IMSG_RECONF_END, NULL, 0) == -1) 643 return (-1); 644 645 return (0); 646 } 647 648 int 649 main_sendboth(enum imsg_type type, void *buf, uint16_t len) 650 { 651 if (imsg_compose_event(iev_frontend, type, 0, 0, -1, buf, len) == -1) 652 return (-1); 653 if (imsg_compose_event(iev_engine, type, 0, 0, -1, buf, len) == -1) 654 return (-1); 655 return (0); 656 } 657 658 void 659 free_ra_iface_conf(struct ra_iface_conf *ra_iface_conf) 660 { 661 struct ra_prefix_conf *prefix; 662 struct ra_pref64_conf *pref64; 663 664 if (!ra_iface_conf) 665 return; 666 667 free(ra_iface_conf->autoprefix); 668 669 while ((prefix = SIMPLEQ_FIRST(&ra_iface_conf->ra_prefix_list)) != 670 NULL) { 671 SIMPLEQ_REMOVE_HEAD(&ra_iface_conf->ra_prefix_list, entry); 672 free(prefix); 673 } 674 675 free_dns_options(&ra_iface_conf->ra_options); 676 677 while ((pref64 = 678 SIMPLEQ_FIRST(&ra_iface_conf->ra_options.ra_pref64_list)) != NULL) { 679 SIMPLEQ_REMOVE_HEAD(&ra_iface_conf->ra_options.ra_pref64_list, 680 entry); 681 free(pref64); 682 } 683 684 free(ra_iface_conf); 685 } 686 687 void 688 free_dns_options(struct ra_options_conf *ra_options) 689 { 690 struct ra_rdnss_conf *ra_rdnss; 691 struct ra_dnssl_conf *ra_dnssl; 692 693 while ((ra_rdnss = SIMPLEQ_FIRST(&ra_options->ra_rdnss_list)) != NULL) { 694 SIMPLEQ_REMOVE_HEAD(&ra_options->ra_rdnss_list, entry); 695 free(ra_rdnss); 696 } 697 ra_options->rdnss_count = 0; 698 699 while ((ra_dnssl = SIMPLEQ_FIRST(&ra_options->ra_dnssl_list)) != NULL) { 700 SIMPLEQ_REMOVE_HEAD(&ra_options->ra_dnssl_list, entry); 701 free(ra_dnssl); 702 } 703 ra_options->dnssl_len = 0; 704 } 705 706 void 707 merge_config(struct rad_conf *conf, struct rad_conf *xconf) 708 { 709 struct ra_iface_conf *ra_iface_conf; 710 struct ra_pref64_conf *pref64; 711 712 /* Remove & discard existing interfaces. */ 713 while ((ra_iface_conf = SIMPLEQ_FIRST(&conf->ra_iface_list)) != NULL) { 714 SIMPLEQ_REMOVE_HEAD(&conf->ra_iface_list, entry); 715 free_ra_iface_conf(ra_iface_conf); 716 } 717 free_dns_options(&conf->ra_options); 718 719 while ((pref64 = SIMPLEQ_FIRST(&conf->ra_options.ra_pref64_list)) 720 != NULL) { 721 SIMPLEQ_REMOVE_HEAD(&conf->ra_options.ra_pref64_list, entry); 722 free(pref64); 723 } 724 725 conf->ra_options = xconf->ra_options; 726 SIMPLEQ_INIT(&conf->ra_options.ra_rdnss_list); 727 SIMPLEQ_INIT(&conf->ra_options.ra_dnssl_list); 728 SIMPLEQ_INIT(&conf->ra_options.ra_pref64_list); 729 730 /* Add new interfaces. */ 731 SIMPLEQ_CONCAT(&conf->ra_iface_list, &xconf->ra_iface_list); 732 733 /* Add dns options */ 734 SIMPLEQ_CONCAT(&conf->ra_options.ra_rdnss_list, 735 &xconf->ra_options.ra_rdnss_list); 736 SIMPLEQ_CONCAT(&conf->ra_options.ra_dnssl_list, 737 &xconf->ra_options.ra_dnssl_list); 738 SIMPLEQ_CONCAT(&conf->ra_options.ra_pref64_list, 739 &xconf->ra_options.ra_pref64_list); 740 free(xconf); 741 } 742 743 struct rad_conf * 744 config_new_empty(void) 745 { 746 struct rad_conf *xconf; 747 748 xconf = calloc(1, sizeof(*xconf)); 749 if (xconf == NULL) 750 fatal(NULL); 751 752 SIMPLEQ_INIT(&xconf->ra_iface_list); 753 754 xconf->ra_options.dfr = 1; 755 xconf->ra_options.cur_hl = 0; 756 xconf->ra_options.m_flag = 0; 757 xconf->ra_options.o_flag = 0; 758 xconf->ra_options.router_lifetime = ADV_DEFAULT_LIFETIME; 759 xconf->ra_options.reachable_time = 0; 760 xconf->ra_options.retrans_timer = 0; 761 xconf->ra_options.mtu = 0; 762 xconf->ra_options.rdns_lifetime = DEFAULT_RDNS_LIFETIME; 763 SIMPLEQ_INIT(&xconf->ra_options.ra_rdnss_list); 764 SIMPLEQ_INIT(&xconf->ra_options.ra_dnssl_list); 765 SIMPLEQ_INIT(&xconf->ra_options.ra_pref64_list); 766 767 return (xconf); 768 } 769 770 void 771 config_clear(struct rad_conf *conf) 772 { 773 struct rad_conf *xconf; 774 775 /* Merge current config with an empty config. */ 776 xconf = config_new_empty(); 777 merge_config(conf, xconf); 778 779 free(conf); 780 } 781 782 void 783 mask_prefix(struct in6_addr* in6, int len) 784 { 785 uint8_t bitmask[8] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe}; 786 int i, skip; 787 788 if (len < 0 || len > 128) 789 fatalx("invalid prefix length: %d", len); 790 791 skip = len / 8; 792 793 if (skip < 16) 794 in6->s6_addr[skip] &= bitmask[len % 8]; 795 796 for (i = skip + 1; i < 16; i++) 797 in6->s6_addr[i] = 0; 798 } 799 800 const char* 801 sin6_to_str(struct sockaddr_in6 *sin6) 802 { 803 static char hbuf[NI_MAXHOST]; 804 int error; 805 806 error = getnameinfo((struct sockaddr *)sin6, sin6->sin6_len, hbuf, 807 sizeof(hbuf), NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV); 808 if (error) { 809 log_warnx("%s", gai_strerror(error)); 810 strlcpy(hbuf, "unknown", sizeof(hbuf)); 811 } 812 return hbuf; 813 } 814 815 const char* 816 in6_to_str(struct in6_addr *in6) 817 { 818 819 struct sockaddr_in6 sin6; 820 821 memset(&sin6, 0, sizeof(sin6)); 822 sin6.sin6_len = sizeof(sin6); 823 sin6.sin6_family = AF_INET6; 824 sin6.sin6_addr = *in6; 825 826 return (sin6_to_str(&sin6)); 827 } 828 829 void 830 open_icmp6sock(int rdomain) 831 { 832 int icmp6sock, on = 1, off = 0; 833 834 log_debug("%s: %d", __func__, rdomain); 835 836 if ((icmp6sock = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, 837 IPPROTO_ICMPV6)) == -1) 838 fatal("ICMPv6 socket"); 839 840 if (setsockopt(icmp6sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, 841 sizeof(on)) == -1) 842 fatal("IPV6_RECVPKTINFO"); 843 844 if (setsockopt(icmp6sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, 845 sizeof(on)) == -1) 846 fatal("IPV6_RECVHOPLIMIT"); 847 848 if (setsockopt(icmp6sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off, 849 sizeof(off)) == -1) 850 fatal("IPV6_RECVHOPLIMIT"); 851 852 if (setsockopt(icmp6sock, SOL_SOCKET, SO_RTABLE, &rdomain, 853 sizeof(rdomain)) == -1) { 854 /* we might race against removal of the rdomain */ 855 log_warn("setsockopt SO_RTABLE"); 856 close(icmp6sock); 857 return; 858 } 859 860 main_imsg_compose_frontend(IMSG_ICMP6SOCK, icmp6sock, &rdomain, 861 sizeof(rdomain)); 862 } 863