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