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