xref: /openbsd-src/usr.sbin/ospf6d/ospf6d.c (revision 4b70baf6e17fc8b27fc1f7fa7929335753fa94c3)
1 /*	$OpenBSD: ospf6d.c,v 1.44 2019/03/25 20:53:33 jca Exp $ */
2 
3 /*
4  * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
5  * Copyright (c) 2004, 2007 Esben Norby <norby@openbsd.org>
6  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/queue.h>
24 #include <sys/time.h>
25 #include <sys/stat.h>
26 #include <sys/wait.h>
27 #include <sys/sysctl.h>
28 #include <syslog.h>
29 
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <net/if_types.h>
33 
34 #include <event.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <pwd.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <signal.h>
42 #include <unistd.h>
43 
44 #include "ospf6d.h"
45 #include "ospf6.h"
46 #include "ospfe.h"
47 #include "control.h"
48 #include "log.h"
49 #include "rde.h"
50 
51 void		main_sig_handler(int, short, void *);
52 __dead void	usage(void);
53 __dead void	ospfd_shutdown(void);
54 
55 void	main_dispatch_ospfe(int, short, void *);
56 void	main_dispatch_rde(int, short, void *);
57 
58 int	ospf_reload(void);
59 int	ospf_sendboth(enum imsg_type, void *, u_int16_t);
60 int	merge_interfaces(struct area *, struct area *);
61 struct iface *iface_lookup(struct area *, struct iface *);
62 
63 int	pipe_parent2ospfe[2];
64 int	pipe_parent2rde[2];
65 int	pipe_ospfe2rde[2];
66 
67 struct ospfd_conf	*ospfd_conf = NULL;
68 struct imsgev		*iev_ospfe;
69 struct imsgev		*iev_rde;
70 char			*conffile;
71 
72 pid_t			 ospfe_pid = 0;
73 pid_t			 rde_pid = 0;
74 
75 /* ARGSUSED */
76 void
77 main_sig_handler(int sig, short event, void *arg)
78 {
79 	/* signal handler rules don't apply, libevent decouples for us */
80 	switch (sig) {
81 	case SIGTERM:
82 	case SIGINT:
83 		ospfd_shutdown();
84 		/* NOTREACHED */
85 	case SIGHUP:
86 		if (ospf_reload() == -1)
87 			log_warnx("configuration reload failed");
88 		else
89 			log_debug("configuration reloaded");
90 		break;
91 	default:
92 		fatalx("unexpected signal");
93 		/* NOTREACHED */
94 	}
95 }
96 
97 __dead void
98 usage(void)
99 {
100 	extern char *__progname;
101 
102 	fprintf(stderr, "usage: %s [-dnv] [-D macro=value]"
103 	    " [-f file] [-s socket]\n",
104 	    __progname);
105 	exit(1);
106 }
107 
108 int
109 main(int argc, char *argv[])
110 {
111 	struct event		 ev_sigint, ev_sigterm, ev_sighup;
112 	int			 ch, opts = 0;
113 	int			 debug = 0;
114 	int			 ipforwarding;
115 	int			 mib[4];
116 	size_t			 len;
117 	char			*sockname = NULL;
118 	int			 control_fd;
119 
120 	conffile = CONF_FILE;
121 	ospfd_process = PROC_MAIN;
122 
123 	log_init(1, LOG_DAEMON);	/* log to stderr until daemonized */
124 	log_procinit(log_procnames[ospfd_process]);
125 
126 	while ((ch = getopt(argc, argv, "cdD:f:s:nv")) != -1) {
127 		switch (ch) {
128 		case 'c':
129 			opts |= OSPFD_OPT_FORCE_DEMOTE;
130 			break;
131 		case 'd':
132 			debug = 1;
133 			break;
134 		case 'D':
135 			if (cmdline_symset(optarg) < 0)
136 				log_warnx("could not parse macro definition %s",
137 				    optarg);
138 			break;
139 		case 'f':
140 			conffile = optarg;
141 			break;
142 		case 'n':
143 			opts |= OSPFD_OPT_NOACTION;
144 			break;
145 		case 's':
146 			sockname = optarg;
147 			break;
148 		case 'v':
149 			if (opts & OSPFD_OPT_VERBOSE)
150 				opts |= OSPFD_OPT_VERBOSE2;
151 			opts |= OSPFD_OPT_VERBOSE;
152 			log_setverbose(1);
153 			break;
154 		default:
155 			usage();
156 			/* NOTREACHED */
157 		}
158 	}
159 
160 	argc -= optind;
161 	argv += optind;
162 	if (argc > 0)
163 		usage();
164 
165 	mib[0] = CTL_NET;
166 	mib[1] = PF_INET6;
167 	mib[2] = IPPROTO_IPV6;
168 	mib[3] = IPCTL_FORWARDING;
169 	len = sizeof(ipforwarding);
170 	if (sysctl(mib, 4, &ipforwarding, &len, NULL, 0) == -1)
171 		err(1, "sysctl");
172 
173 	if (ipforwarding != 1) {
174 		log_warnx("WARNING: IPv6 forwarding NOT enabled, "
175 		    "running as stub router");
176 		opts |= OSPFD_OPT_STUB_ROUTER;
177 	}
178 
179 	/* prepare and fetch interfaces early */
180 	if_init();
181 
182 	/* parse config file */
183 	if ((ospfd_conf = parse_config(conffile, opts)) == NULL )
184 		exit(1);
185 
186 	if (sockname == NULL) {
187 		if (asprintf(&sockname, "%s.%d", OSPF6D_SOCKET,
188 		    ospfd_conf->rdomain) == -1)
189 			err(1, "asprintf");
190 	}
191 
192 	ospfd_conf->csock = sockname;
193 
194 	if (ospfd_conf->opts & OSPFD_OPT_NOACTION) {
195 		if (ospfd_conf->opts & OSPFD_OPT_VERBOSE)
196 			print_config(ospfd_conf);
197 		else
198 			fprintf(stderr, "configuration OK\n");
199 		exit(0);
200 	}
201 
202 	/* check for root privileges  */
203 	if (geteuid())
204 		errx(1, "need root privileges");
205 
206 	/* check for ospfd user */
207 	if (getpwnam(OSPF6D_USER) == NULL)
208 		errx(1, "unknown user %s", OSPF6D_USER);
209 
210 	log_init(debug, LOG_DAEMON);
211 	log_setverbose(ospfd_conf->opts & OSPFD_OPT_VERBOSE);
212 
213 	if ((control_check(ospfd_conf->csock)) == -1)
214 		fatalx("ospf6d already running");
215 
216 	if (!debug)
217 		daemon(1, 0);
218 
219 	log_info("startup");
220 
221 	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
222 	    PF_UNSPEC, pipe_parent2ospfe) == -1)
223 		fatal("socketpair");
224 	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
225 	    PF_UNSPEC, pipe_parent2rde) == -1)
226 		fatal("socketpair");
227 	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
228 	    PF_UNSPEC, pipe_ospfe2rde) == -1)
229 		fatal("socketpair");
230 
231 	/* start children */
232 	rde_pid = rde(ospfd_conf, pipe_parent2rde, pipe_ospfe2rde,
233 	    pipe_parent2ospfe);
234 	ospfe_pid = ospfe(ospfd_conf, pipe_parent2ospfe, pipe_ospfe2rde,
235 	    pipe_parent2rde);
236 
237 	event_init();
238 
239 	/* setup signal handler */
240 	signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
241 	signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
242 	signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL);
243 	signal_add(&ev_sigint, NULL);
244 	signal_add(&ev_sigterm, NULL);
245 	signal_add(&ev_sighup, NULL);
246 	signal(SIGPIPE, SIG_IGN);
247 
248 	/* setup pipes to children */
249 	close(pipe_parent2ospfe[1]);
250 	close(pipe_parent2rde[1]);
251 	close(pipe_ospfe2rde[0]);
252 	close(pipe_ospfe2rde[1]);
253 
254 	if ((iev_ospfe = malloc(sizeof(struct imsgev))) == NULL ||
255 	    (iev_rde = malloc(sizeof(struct imsgev))) == NULL)
256 		fatal(NULL);
257 	imsg_init(&iev_ospfe->ibuf, pipe_parent2ospfe[0]);
258 	iev_ospfe->handler = main_dispatch_ospfe;
259 	imsg_init(&iev_rde->ibuf, pipe_parent2rde[0]);
260 	iev_rde->handler = main_dispatch_rde;
261 
262 	/* setup event handler */
263 	iev_ospfe->events = EV_READ;
264 	event_set(&iev_ospfe->ev, iev_ospfe->ibuf.fd, iev_ospfe->events,
265 	    iev_ospfe->handler, iev_ospfe);
266 	event_add(&iev_ospfe->ev, NULL);
267 
268 	iev_rde->events = EV_READ;
269 	event_set(&iev_rde->ev, iev_rde->ibuf.fd, iev_rde->events,
270 	    iev_rde->handler, iev_rde);
271 	event_add(&iev_rde->ev, NULL);
272 
273 	if ((control_fd = control_init(ospfd_conf->csock)) == -1)
274 		fatalx("control socket setup failed");
275 	main_imsg_compose_ospfe_fd(IMSG_CONTROLFD, 0, control_fd);
276 
277 	if (unveil(ospfd_conf->csock, "c") == -1)
278 		fatal("unveil");
279 	if (unveil(NULL, NULL) == -1)
280 		fatal("unveil");
281 
282 	if (kr_init(!(ospfd_conf->flags & OSPFD_FLAG_NO_FIB_UPDATE),
283 	    ospfd_conf->rdomain, ospfd_conf->fib_priority) == -1)
284 		fatalx("kr_init failed");
285 
286 	event_dispatch();
287 
288 	ospfd_shutdown();
289 	/* NOTREACHED */
290 	return (0);
291 }
292 
293 __dead void
294 ospfd_shutdown(void)
295 {
296 	pid_t	pid;
297 	int	status;
298 
299 	/* close pipes */
300 	msgbuf_clear(&iev_ospfe->ibuf.w);
301 	close(iev_ospfe->ibuf.fd);
302 	msgbuf_clear(&iev_rde->ibuf.w);
303 	close(iev_rde->ibuf.fd);
304 
305 	control_cleanup(ospfd_conf->csock);
306 	kr_shutdown();
307 	carp_demote_shutdown();
308 
309 	log_debug("waiting for children to terminate");
310 	do {
311 		pid = wait(&status);
312 		if (pid == -1) {
313 			if (errno != EINTR && errno != ECHILD)
314 				fatal("wait");
315 		} else if (WIFSIGNALED(status))
316 			log_warnx("%s terminated; signal %d",
317 			    (pid == rde_pid) ? "route decision engine" :
318 			    "ospf engine", WTERMSIG(status));
319 	} while (pid != -1 || (pid == -1 && errno == EINTR));
320 
321 	free(iev_ospfe);
322 	free(iev_rde);
323 	free(ospfd_conf);
324 
325 	log_info("terminating");
326 	exit(0);
327 }
328 
329 /* imsg handling */
330 /* ARGSUSED */
331 void
332 main_dispatch_ospfe(int fd, short event, void *bula)
333 {
334 	struct imsgev		*iev = bula;
335 	struct imsgbuf		*ibuf = &iev->ibuf;
336 	struct imsg		 imsg;
337 	struct demote_msg	 dmsg;
338 	ssize_t			 n;
339 	int			 shut = 0, verbose;
340 
341 	if (event & EV_READ) {
342 		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
343 			fatal("imsg_read error");
344 		if (n == 0)	/* connection closed */
345 			shut = 1;
346 	}
347 	if (event & EV_WRITE) {
348 		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
349 			fatal("msgbuf_write");
350 		if (n == 0)	/* connection closed */
351 			shut = 1;
352 	}
353 
354 	for (;;) {
355 		if ((n = imsg_get(ibuf, &imsg)) == -1)
356 			fatal("imsg_get");
357 
358 		if (n == 0)
359 			break;
360 
361 		switch (imsg.hdr.type) {
362 		case IMSG_CTL_RELOAD:
363 			if (ospf_reload() == -1)
364 				log_warnx("configuration reload failed");
365 			else
366 				log_debug("configuration reloaded");
367 			break;
368 		case IMSG_CTL_FIB_COUPLE:
369 			kr_fib_couple();
370 			break;
371 		case IMSG_CTL_FIB_DECOUPLE:
372 			kr_fib_decouple();
373 			break;
374 		case IMSG_CTL_KROUTE:
375 		case IMSG_CTL_KROUTE_ADDR:
376 			kr_show_route(&imsg);
377 			break;
378 		case IMSG_DEMOTE:
379 			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(dmsg))
380 				fatalx("invalid size of OE request");
381 			memcpy(&dmsg, imsg.data, sizeof(dmsg));
382 			carp_demote_set(dmsg.demote_group, dmsg.level);
383 			break;
384 		case IMSG_CTL_LOG_VERBOSE:
385 			/* already checked by ospfe */
386 			memcpy(&verbose, imsg.data, sizeof(verbose));
387 			log_setverbose(verbose);
388 			break;
389 		default:
390 			log_debug("main_dispatch_ospfe: error handling imsg %d",
391 			    imsg.hdr.type);
392 			break;
393 		}
394 		imsg_free(&imsg);
395 	}
396 	if (!shut)
397 		imsg_event_add(iev);
398 	else {
399 		/* this pipe is dead, so remove the event handler */
400 		event_del(&iev->ev);
401 		event_loopexit(NULL);
402 	}
403 }
404 
405 /* ARGSUSED */
406 void
407 main_dispatch_rde(int fd, short event, void *bula)
408 {
409 	struct imsgev	*iev = bula;
410 	struct imsgbuf	*ibuf = &iev->ibuf;
411 	struct imsg	 imsg;
412 	ssize_t		 n;
413 	int		 count, shut = 0;
414 
415 	if (event & EV_READ) {
416 		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
417 			fatal("imsg_read error");
418 		if (n == 0)	/* connection closed */
419 			shut = 1;
420 	}
421 	if (event & EV_WRITE) {
422 		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
423 			fatal("msgbuf_write");
424 		if (n == 0)	/* connection closed */
425 			shut = 1;
426 	}
427 
428 	for (;;) {
429 		if ((n = imsg_get(ibuf, &imsg)) == -1)
430 			fatal("imsg_get");
431 
432 		if (n == 0)
433 			break;
434 
435 		switch (imsg.hdr.type) {
436 		case IMSG_KROUTE_CHANGE:
437 			count = (imsg.hdr.len - IMSG_HEADER_SIZE) /
438 			    sizeof(struct kroute);
439 			if (kr_change(imsg.data, count))
440 				log_warn("main_dispatch_rde: error changing "
441 				    "route");
442 			break;
443 		case IMSG_KROUTE_DELETE:
444 			if (kr_delete(imsg.data))
445 				log_warn("main_dispatch_rde: error deleting "
446 				    "route");
447 			break;
448 		default:
449 			log_debug("main_dispatch_rde: error handling imsg %d",
450 			    imsg.hdr.type);
451 			break;
452 		}
453 		imsg_free(&imsg);
454 	}
455 	if (!shut)
456 		imsg_event_add(iev);
457 	else {
458 		/* this pipe is dead, so remove the event handler */
459 		event_del(&iev->ev);
460 		event_loopexit(NULL);
461 	}
462 }
463 
464 void
465 main_imsg_compose_ospfe(int type, pid_t pid, void *data, u_int16_t datalen)
466 {
467 	if (iev_ospfe == NULL)
468 		return;
469 	imsg_compose_event(iev_ospfe, type, 0, pid, -1, data, datalen);
470 }
471 
472 void
473 main_imsg_compose_ospfe_fd(int type, pid_t pid, int fd)
474 {
475 	if (iev_ospfe == NULL)
476 		return;
477 	imsg_compose_event(iev_ospfe, type, 0, pid, fd, NULL, 0);
478 }
479 
480 void
481 main_imsg_compose_rde(int type, pid_t pid, void *data, u_int16_t datalen)
482 {
483 	if (iev_rde == NULL)
484 		return;
485 	imsg_compose_event(iev_rde, type, 0, pid, -1, data, datalen);
486 }
487 
488 void
489 imsg_event_add(struct imsgev *iev)
490 {
491 	iev->events = EV_READ;
492 	if (iev->ibuf.w.queued)
493 		iev->events |= EV_WRITE;
494 
495 	event_del(&iev->ev);
496 	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
497 	event_add(&iev->ev, NULL);
498 }
499 
500 int
501 imsg_compose_event(struct imsgev *iev, u_int16_t type, u_int32_t peerid,
502     pid_t pid, int fd, void *data, u_int16_t datalen)
503 {
504 	int	ret;
505 
506 	if ((ret = imsg_compose(&iev->ibuf, type, peerid,
507 	    pid, fd, data, datalen)) != -1)
508 		imsg_event_add(iev);
509 	return (ret);
510 }
511 
512 int
513 ospf_redistribute(struct kroute *kr, u_int32_t *metric)
514 {
515 	struct redistribute	*r;
516 	struct in6_addr		 ina, inb;
517 	struct iface		*iface;
518 	u_int8_t		 is_default = 0;
519 	int			 depend_ok;
520 
521 	/* only allow ::/0 via REDIST_DEFAULT */
522 	if (IN6_IS_ADDR_UNSPECIFIED(&kr->prefix) && kr->prefixlen == 0)
523 		is_default = 1;
524 
525 	SIMPLEQ_FOREACH(r, &ospfd_conf->redist_list, entry) {
526 		if (r->dependon[0] != '\0') {
527 			if ((iface = if_findname(r->dependon)))
528 				depend_ok = ifstate_is_up(iface);
529 			else
530 				depend_ok = 0;
531 		} else
532 			depend_ok = 1;
533 
534 		switch (r->type & ~REDIST_NO) {
535 		case REDIST_LABEL:
536 			if (kr->rtlabel == r->label) {
537 				*metric = depend_ok ? r->metric :
538 				    r->metric | MAX_METRIC;
539 				return (r->type & REDIST_NO ? 0 : 1);
540 			}
541 			break;
542 		case REDIST_STATIC:
543 			/*
544 			 * Dynamic routes are not redistributable. Placed here
545 			 * so that link local addresses can be redistributed
546 			 * via a rtlabel.
547 			 */
548 			if (is_default)
549 				continue;
550 			if (kr->flags & F_DYNAMIC)
551 				continue;
552 			if (kr->flags & F_STATIC) {
553 				*metric = depend_ok ? r->metric :
554 				    r->metric | MAX_METRIC;
555 				return (r->type & REDIST_NO ? 0 : 1);
556 			}
557 			break;
558 		case REDIST_CONNECTED:
559 			if (is_default)
560 				continue;
561 			if (kr->flags & F_DYNAMIC)
562 				continue;
563 			if (kr->flags & F_CONNECTED) {
564 				*metric = depend_ok ? r->metric :
565 				    r->metric | MAX_METRIC;
566 				return (r->type & REDIST_NO ? 0 : 1);
567 			}
568 			break;
569 		case REDIST_ADDR:
570 			if (kr->flags & F_DYNAMIC)
571 				continue;
572 
573 			if (IN6_IS_ADDR_UNSPECIFIED(&r->addr) &&
574 			    r->prefixlen == 0) {
575 				if (is_default) {
576 					*metric = depend_ok ? r->metric :
577 					    r->metric | MAX_METRIC;
578 					return (r->type & REDIST_NO ? 0 : 1);
579 				} else
580 					return (0);
581 			}
582 
583 			inet6applymask(&ina, &kr->prefix, r->prefixlen);
584 			inet6applymask(&inb, &r->addr, r->prefixlen);
585 			if (IN6_ARE_ADDR_EQUAL(&ina, &inb) &&
586 			    kr->prefixlen >= r->prefixlen) {
587 				*metric = depend_ok ? r->metric :
588 				    r->metric | MAX_METRIC;
589 				return (r->type & REDIST_NO ? 0 : 1);
590 			}
591 			break;
592 		case REDIST_DEFAULT:
593 			if (is_default) {
594 				*metric = depend_ok ? r->metric :
595 				    r->metric | MAX_METRIC;
596 				return (r->type & REDIST_NO ? 0 : 1);
597 			}
598 			break;
599 		}
600 	}
601 
602 	return (0);
603 }
604 
605 int
606 ospf_reload(void)
607 {
608 #ifdef notyet
609 	struct area		*area;
610 	struct ospfd_conf	*xconf;
611 
612 	if ((xconf = parse_config(conffile, ospfd_conf->opts)) == NULL)
613 		return (-1);
614 
615 	/* XXX bail out if router-id changed */
616 
617 	/* send config to childs */
618 	if (ospf_sendboth(IMSG_RECONF_CONF, xconf, sizeof(*xconf)) == -1)
619 		return (-1);
620 
621 	/* send areas, interfaces happen out of band */
622 	LIST_FOREACH(area, &xconf->area_list, entry) {
623 		if (ospf_sendboth(IMSG_RECONF_AREA, area, sizeof(*area)) == -1)
624 			return (-1);
625 	}
626 
627 	if (ospf_sendboth(IMSG_RECONF_END, NULL, 0) == -1)
628 		return (-1);
629 
630 	/* XXX send newly available interfaces to the childs */
631 
632 	merge_config(ospfd_conf, xconf);
633 	/* update redistribute lists */
634 	kr_reload();
635 	return (0);
636 #else
637 	return (-1);
638 #endif
639 }
640 
641 int
642 ospf_sendboth(enum imsg_type type, void *buf, u_int16_t len)
643 {
644 	if (imsg_compose_event(iev_ospfe, type, 0, 0, -1, buf, len) == -1)
645 		return (-1);
646 	if (imsg_compose_event(iev_rde, type, 0, 0, -1, buf, len) == -1)
647 		return (-1);
648 	return (0);
649 }
650 
651 void
652 merge_config(struct ospfd_conf *conf, struct ospfd_conf *xconf)
653 {
654 	struct area		*a, *xa, *na;
655 	struct iface		*iface;
656 	struct redistribute	*r;
657 
658 	/* change of rtr_id needs a restart */
659 	conf->flags = xconf->flags;
660 	conf->spf_delay = xconf->spf_delay;
661 	conf->spf_hold_time = xconf->spf_hold_time;
662 	conf->redistribute = xconf->redistribute;
663 
664 	if (ospfd_process == PROC_MAIN) {
665 		/* main process does neither use areas nor interfaces */
666 		while ((r = SIMPLEQ_FIRST(&conf->redist_list)) != NULL) {
667 			SIMPLEQ_REMOVE_HEAD(&conf->redist_list, entry);
668 			free(r);
669 		}
670 		while ((r = SIMPLEQ_FIRST(&xconf->redist_list)) != NULL) {
671 			SIMPLEQ_REMOVE_HEAD(&xconf->redist_list, entry);
672 			SIMPLEQ_INSERT_TAIL(&conf->redist_list, r, entry);
673 		}
674 		goto done;
675 	}
676 
677 	/* merge areas and interfaces */
678 	for (a = LIST_FIRST(&conf->area_list); a != NULL; a = na) {
679 		na = LIST_NEXT(a, entry);
680 		/* find deleted areas */
681 		if ((xa = area_find(xconf, a->id)) == NULL) {
682 			if (ospfd_process == PROC_OSPF_ENGINE) {
683 				LIST_FOREACH(iface, &a->iface_list, entry)
684 					if_fsm(iface, IF_EVT_DOWN);
685 			}
686 			LIST_REMOVE(a, entry);
687 			area_del(a);
688 		}
689 	}
690 
691 	for (xa = LIST_FIRST(&xconf->area_list); xa != NULL; xa = na) {
692 		na = LIST_NEXT(xa, entry);
693 		if ((a = area_find(conf, xa->id)) == NULL) {
694 			LIST_REMOVE(xa, entry);
695 			LIST_INSERT_HEAD(&conf->area_list, xa, entry);
696 			if (ospfd_process == PROC_OSPF_ENGINE) {
697 				/* start interfaces */
698 				ospfe_demote_area(xa, 0);
699 				LIST_FOREACH(iface, &xa->iface_list, entry)
700 					if_start(conf, iface);
701 			}
702 			/* no need to merge interfaces */
703 			continue;
704 		}
705 		/*
706 		 * stub is not yet used but switching between stub and normal
707 		 * will be another painful job.
708 		 */
709 		a->stub = xa->stub;
710 		a->stub_default_cost = xa->stub_default_cost;
711 		if (ospfd_process == PROC_RDE_ENGINE)
712 			a->dirty = 1; /* force SPF tree recalculation */
713 
714 		/* merge interfaces */
715 		if (merge_interfaces(a, xa) &&
716 		    ospfd_process == PROC_OSPF_ENGINE)
717 			a->dirty = 1; /* force rtr LSA update */
718 	}
719 
720 	if (ospfd_process == PROC_OSPF_ENGINE) {
721 		LIST_FOREACH(a, &conf->area_list, entry) {
722 			LIST_FOREACH(iface, &a->iface_list, entry) {
723 				if (iface->state == IF_STA_NEW) {
724 					iface->state = IF_STA_DOWN;
725 					if_start(conf, iface);
726 				}
727 			}
728 			if (a->dirty) {
729 				a->dirty = 0;
730 				orig_rtr_lsa(LIST_FIRST(&a->iface_list));
731 			}
732 		}
733 	}
734 
735 done:
736 	while ((a = LIST_FIRST(&xconf->area_list)) != NULL) {
737 		LIST_REMOVE(a, entry);
738 		area_del(a);
739 	}
740 	free(xconf);
741 }
742 
743 int
744 merge_interfaces(struct area *a, struct area *xa)
745 {
746 	struct iface	*i, *xi, *ni;
747 	int		 dirty = 0;
748 
749 	/* problems:
750 	 * - new interfaces (easy)
751 	 * - deleted interfaces (needs to be done via fsm?)
752 	 * - changing passive (painful?)
753 	 */
754 	for (i = LIST_FIRST(&a->iface_list); i != NULL; i = ni) {
755 		ni = LIST_NEXT(i, entry);
756 		if (iface_lookup(xa, i) == NULL) {
757 			log_debug("merge_interfaces: proc %d area %s removing "
758 			    "interface %s", ospfd_process, inet_ntoa(a->id),
759 			    i->name);
760 			if (ospfd_process == PROC_OSPF_ENGINE)
761 				if_fsm(i, IF_EVT_DOWN);
762 			LIST_REMOVE(i, entry);
763 			if_del(i);
764 		}
765 	}
766 
767 	for (xi = LIST_FIRST(&xa->iface_list); xi != NULL; xi = ni) {
768 		ni = LIST_NEXT(xi, entry);
769 		if ((i = iface_lookup(a, xi)) == NULL) {
770 			/* new interface but delay initialisation */
771 			log_debug("merge_interfaces: proc %d area %s adding "
772 			    "interface %s", ospfd_process, inet_ntoa(a->id),
773 			    xi->name);
774 			LIST_REMOVE(xi, entry);
775 			LIST_INSERT_HEAD(&a->iface_list, xi, entry);
776 			if (ospfd_process == PROC_OSPF_ENGINE)
777 				xi->state = IF_STA_NEW;
778 			continue;
779 		}
780 		log_debug("merge_interfaces: proc %d area %s merging "
781 		    "interface %s", ospfd_process, inet_ntoa(a->id), i->name);
782 		i->addr = xi->addr;
783 		i->dst = xi->dst;
784 		i->abr_id = xi->abr_id;
785 		i->baudrate = xi->baudrate;
786 		i->dead_interval = xi->dead_interval;
787 		i->mtu = xi->mtu;
788 		i->transmit_delay = xi->transmit_delay;
789 		i->hello_interval = xi->hello_interval;
790 		i->rxmt_interval = xi->rxmt_interval;
791 		if (i->metric != xi->metric)
792 			dirty = 1;
793 		i->metric = xi->metric;
794 		i->priority = xi->priority;
795 		i->flags = xi->flags; /* needed? */
796 		i->type = xi->type; /* needed? */
797 		i->if_type = xi->if_type; /* needed? */
798 		i->linkstate = xi->linkstate; /* needed? */
799 
800 #if 0 /* XXX needs some kind of love */
801 		if (i->passive != xi->passive) {
802 			/* need to restart interface to cope with this change */
803 			if (ospfd_process == PROC_OSPF_ENGINE)
804 				if_fsm(i, IF_EVT_DOWN);
805 			i->passive = xi->passive;
806 			if (ospfd_process == PROC_OSPF_ENGINE)
807 				if_fsm(i, IF_EVT_UP);
808 		}
809 #endif
810 	}
811 	return (dirty);
812 }
813 
814 struct iface *
815 iface_lookup(struct area *area, struct iface *iface)
816 {
817 	struct iface	*i;
818 
819 	LIST_FOREACH(i, &area->iface_list, entry)
820 		if (i->ifindex == iface->ifindex)
821 			return (i);
822 	return (NULL);
823 }
824 
825 int
826 ifstate_is_up(struct iface *iface)
827 {
828 	if (!(iface->flags & IFF_UP))
829 		return (0);
830 	if (iface->if_type == IFT_CARP &&
831 	    iface->linkstate == LINK_STATE_UNKNOWN)
832 		return (0);
833 	return LINK_STATE_IS_UP(iface->linkstate);
834 }
835