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