xref: /openbsd-src/usr.sbin/ospfd/ospfd.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: ospfd.c,v 1.78 2011/08/20 11:16:09 sthen Exp $ */
2 
3 /*
4  * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
5  * Copyright (c) 2004 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 "ospfd.h"
44 #include "ospf.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 [-cdnv] [-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 	struct area		*a;
130 	int			 ch, opts = 0;
131 	int			 debug = 0;
132 	int			 ipforwarding;
133 	int			 mib[4];
134 	size_t			 len;
135 	char			*sockname;
136 
137 	conffile = CONF_FILE;
138 	ospfd_process = PROC_MAIN;
139 	sockname = OSPFD_SOCKET;
140 
141 	log_init(1);	/* log to stderr until daemonized */
142 	log_verbose(1);
143 
144 	while ((ch = getopt(argc, argv, "cdD:f:ns:v")) != -1) {
145 		switch (ch) {
146 		case 'c':
147 			opts |= OSPFD_OPT_FORCE_DEMOTE;
148 			break;
149 		case 'd':
150 			debug = 1;
151 			break;
152 		case 'D':
153 			if (cmdline_symset(optarg) < 0)
154 				log_warnx("could not parse macro definition %s",
155 				    optarg);
156 			break;
157 		case 'f':
158 			conffile = optarg;
159 			break;
160 		case 'n':
161 			opts |= OSPFD_OPT_NOACTION;
162 			break;
163 		case 's':
164 			sockname = optarg;
165 			break;
166 		case 'v':
167 			if (opts & OSPFD_OPT_VERBOSE)
168 				opts |= OSPFD_OPT_VERBOSE2;
169 			opts |= OSPFD_OPT_VERBOSE;
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_INET;
184 	mib[2] = IPPROTO_IP;
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: IP forwarding NOT enabled, "
192 		    "running as stub router");
193 		opts |= OSPFD_OPT_STUB_ROUTER;
194 	}
195 
196 
197 	/* fetch interfaces early */
198 	kif_init();
199 
200 	/* parse config file */
201 	if ((ospfd_conf = parse_config(conffile, opts)) == NULL) {
202 		kr_shutdown();
203 		exit(1);
204 	}
205 	ospfd_conf->csock = sockname;
206 
207 	if (ospfd_conf->opts & OSPFD_OPT_NOACTION) {
208 		if (ospfd_conf->opts & OSPFD_OPT_VERBOSE)
209 			print_config(ospfd_conf);
210 		else
211 			fprintf(stderr, "configuration OK\n");
212 		kr_shutdown();
213 		exit(0);
214 	}
215 
216 	/* check for root privileges  */
217 	if (geteuid())
218 		errx(1, "need root privileges");
219 
220 	/* check for ospfd user */
221 	if (getpwnam(OSPFD_USER) == NULL)
222 		errx(1, "unknown user %s", OSPFD_USER);
223 
224 	log_init(debug);
225 	log_verbose(ospfd_conf->opts & OSPFD_OPT_VERBOSE);
226 
227 	if (!debug)
228 		daemon(1, 0);
229 
230 	log_info("startup");
231 
232 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC,
233 	    pipe_parent2ospfe) == -1)
234 		fatal("socketpair");
235 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_parent2rde) == -1)
236 		fatal("socketpair");
237 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_ospfe2rde) == -1)
238 		fatal("socketpair");
239 	session_socket_blockmode(pipe_parent2ospfe[0], BM_NONBLOCK);
240 	session_socket_blockmode(pipe_parent2ospfe[1], BM_NONBLOCK);
241 	session_socket_blockmode(pipe_parent2rde[0], BM_NONBLOCK);
242 	session_socket_blockmode(pipe_parent2rde[1], BM_NONBLOCK);
243 	session_socket_blockmode(pipe_ospfe2rde[0], BM_NONBLOCK);
244 	session_socket_blockmode(pipe_ospfe2rde[1], BM_NONBLOCK);
245 
246 	/* start children */
247 	rde_pid = rde(ospfd_conf, pipe_parent2rde, pipe_ospfe2rde,
248 	    pipe_parent2ospfe);
249 	ospfe_pid = ospfe(ospfd_conf, pipe_parent2ospfe, pipe_ospfe2rde,
250 	    pipe_parent2rde);
251 
252 	/* show who we are */
253 	setproctitle("parent");
254 
255 	event_init();
256 
257 	/* setup signal handler */
258 	signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
259 	signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
260 	signal_set(&ev_sigchld, SIGCHLD, main_sig_handler, NULL);
261 	signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL);
262 	signal_add(&ev_sigint, NULL);
263 	signal_add(&ev_sigterm, NULL);
264 	signal_add(&ev_sigchld, NULL);
265 	signal_add(&ev_sighup, NULL);
266 	signal(SIGPIPE, SIG_IGN);
267 
268 	/* setup pipes to children */
269 	close(pipe_parent2ospfe[1]);
270 	close(pipe_parent2rde[1]);
271 	close(pipe_ospfe2rde[0]);
272 	close(pipe_ospfe2rde[1]);
273 
274 	if ((iev_ospfe = malloc(sizeof(struct imsgev))) == NULL ||
275 	    (iev_rde = malloc(sizeof(struct imsgev))) == NULL)
276 		fatal(NULL);
277 	imsg_init(&iev_ospfe->ibuf, pipe_parent2ospfe[0]);
278 	iev_ospfe->handler = main_dispatch_ospfe;
279 	imsg_init(&iev_rde->ibuf, pipe_parent2rde[0]);
280 	iev_rde->handler = main_dispatch_rde;
281 
282 	/* setup event handler */
283 	iev_ospfe->events = EV_READ;
284 	event_set(&iev_ospfe->ev, iev_ospfe->ibuf.fd, iev_ospfe->events,
285 	    iev_ospfe->handler, iev_ospfe);
286 	event_add(&iev_ospfe->ev, NULL);
287 
288 	iev_rde->events = EV_READ;
289 	event_set(&iev_rde->ev, iev_rde->ibuf.fd, iev_rde->events,
290 	    iev_rde->handler, iev_rde);
291 	event_add(&iev_rde->ev, NULL);
292 
293 	if (kr_init(!(ospfd_conf->flags & OSPFD_FLAG_NO_FIB_UPDATE),
294 	    ospfd_conf->rdomain) == -1)
295 		fatalx("kr_init failed");
296 
297 	/* remove unneded stuff from config */
298 	while ((a = LIST_FIRST(&ospfd_conf->area_list)) != NULL) {
299 		LIST_REMOVE(a, entry);
300 		area_del(a);
301 	}
302 
303 	event_dispatch();
304 
305 	ospfd_shutdown();
306 	/* NOTREACHED */
307 	return (0);
308 }
309 
310 void
311 ospfd_shutdown(void)
312 {
313 	pid_t		 	 pid;
314 	struct redistribute	*r;
315 
316 	if (ospfe_pid)
317 		kill(ospfe_pid, SIGTERM);
318 
319 	if (rde_pid)
320 		kill(rde_pid, SIGTERM);
321 
322 	control_cleanup(ospfd_conf->csock);
323 	while ((r = SIMPLEQ_FIRST(&ospfd_conf->redist_list)) != NULL) {
324 		SIMPLEQ_REMOVE_HEAD(&ospfd_conf->redist_list, entry);
325 		free(r);
326 	}
327 	kr_shutdown();
328 	carp_demote_shutdown();
329 
330 	do {
331 		if ((pid = wait(NULL)) == -1 &&
332 		    errno != EINTR && errno != ECHILD)
333 			fatal("wait");
334 	} while (pid != -1 || (pid == -1 && errno == EINTR));
335 
336 	msgbuf_clear(&iev_ospfe->ibuf.w);
337 	free(iev_ospfe);
338 	msgbuf_clear(&iev_rde->ibuf.w);
339 	free(iev_rde);
340 	free(ospfd_conf);
341 
342 	log_info("terminating");
343 	exit(0);
344 }
345 
346 int
347 check_child(pid_t pid, const char *pname)
348 {
349 	int	status;
350 
351 	if (waitpid(pid, &status, WNOHANG) > 0) {
352 		if (WIFEXITED(status)) {
353 			log_warnx("lost child: %s exited", pname);
354 			return (1);
355 		}
356 		if (WIFSIGNALED(status)) {
357 			log_warnx("lost child: %s terminated; signal %d",
358 			    pname, WTERMSIG(status));
359 			return (1);
360 		}
361 	}
362 
363 	return (0);
364 }
365 
366 /* imsg handling */
367 /* ARGSUSED */
368 void
369 main_dispatch_ospfe(int fd, short event, void *bula)
370 {
371 	struct imsgev		*iev = bula;
372 	struct imsgbuf		*ibuf;
373 	struct imsg		 imsg;
374 	struct demote_msg	 dmsg;
375 	ssize_t			 n;
376 	int			 shut = 0, verbose;
377 
378 	ibuf = &iev->ibuf;
379 
380 	if (event & EV_READ) {
381 		if ((n = imsg_read(ibuf)) == -1)
382 			fatal("imsg_read error");
383 		if (n == 0)	/* connection closed */
384 			shut = 1;
385 	}
386 	if (event & EV_WRITE) {
387 		if (msgbuf_write(&ibuf->w) == -1)
388 			fatal("msgbuf_write");
389 	}
390 
391 	for (;;) {
392 		if ((n = imsg_get(ibuf, &imsg)) == -1)
393 			fatal("imsg_get");
394 
395 		if (n == 0)
396 			break;
397 
398 		switch (imsg.hdr.type) {
399 		case IMSG_CTL_RELOAD:
400 			if (ospf_reload() == -1)
401 				log_warnx("configuration reload failed");
402 			else
403 				log_debug("configuration reloaded");
404 			break;
405 		case IMSG_CTL_FIB_COUPLE:
406 			kr_fib_couple();
407 			break;
408 		case IMSG_CTL_FIB_DECOUPLE:
409 			kr_fib_decouple();
410 			break;
411 		case IMSG_CTL_FIB_RELOAD:
412 			kr_fib_reload();
413 			break;
414 		case IMSG_CTL_KROUTE:
415 		case IMSG_CTL_KROUTE_ADDR:
416 			kr_show_route(&imsg);
417 			break;
418 		case IMSG_CTL_IFINFO:
419 			if (imsg.hdr.len == IMSG_HEADER_SIZE)
420 				kr_ifinfo(NULL, imsg.hdr.pid);
421 			else if (imsg.hdr.len == IMSG_HEADER_SIZE + IFNAMSIZ)
422 				kr_ifinfo(imsg.data, imsg.hdr.pid);
423 			else
424 				log_warnx("IFINFO request with wrong len");
425 			break;
426 		case IMSG_DEMOTE:
427 			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(dmsg))
428 				fatalx("invalid size of OE request");
429 			memcpy(&dmsg, imsg.data, sizeof(dmsg));
430 			carp_demote_set(dmsg.demote_group, dmsg.level);
431 			break;
432 		case IMSG_CTL_LOG_VERBOSE:
433 			/* already checked by ospfe */
434 			memcpy(&verbose, imsg.data, sizeof(verbose));
435 			log_verbose(verbose);
436 			break;
437 		default:
438 			log_debug("main_dispatch_ospfe: error handling imsg %d",
439 			    imsg.hdr.type);
440 			break;
441 		}
442 		imsg_free(&imsg);
443 	}
444 	if (!shut)
445 		imsg_event_add(iev);
446 	else {
447 		/* this pipe is dead, so remove the event handler */
448 		event_del(&iev->ev);
449 		event_loopexit(NULL);
450 	}
451 }
452 
453 /* ARGSUSED */
454 void
455 main_dispatch_rde(int fd, short event, void *bula)
456 {
457 	struct imsgev	*iev = bula;
458 	struct imsgbuf  *ibuf;
459 	struct imsg	 imsg;
460 	ssize_t		 n;
461 	int		 count, shut = 0;
462 
463 	ibuf = &iev->ibuf;
464 
465 	if (event & EV_READ) {
466 		if ((n = imsg_read(ibuf)) == -1)
467 			fatal("imsg_read error");
468 		if (n == 0)	/* connection closed */
469 			shut = 1;
470 	}
471 	if (event & EV_WRITE) {
472 		if (msgbuf_write(&ibuf->w) == -1)
473 			fatal("msgbuf_write");
474 	}
475 
476 	for (;;) {
477 		if ((n = imsg_get(ibuf, &imsg)) == -1)
478 			fatal("imsg_get");
479 
480 		if (n == 0)
481 			break;
482 
483 		switch (imsg.hdr.type) {
484 		case IMSG_KROUTE_CHANGE:
485 			count = (imsg.hdr.len - IMSG_HEADER_SIZE) /
486 			    sizeof(struct kroute);
487 			if (kr_change(imsg.data, count))
488 				log_warn("main_dispatch_rde: error changing "
489 				    "route");
490 			break;
491 		case IMSG_KROUTE_DELETE:
492 			if (kr_delete(imsg.data))
493 				log_warn("main_dispatch_rde: error deleting "
494 				    "route");
495 			break;
496 		default:
497 			log_debug("main_dispatch_rde: error handling imsg %d",
498 			    imsg.hdr.type);
499 			break;
500 		}
501 		imsg_free(&imsg);
502 	}
503 	if (!shut)
504 		imsg_event_add(iev);
505 	else {
506 		/* this pipe is dead, so remove the event handler */
507 		event_del(&iev->ev);
508 		event_loopexit(NULL);
509 	}
510 }
511 
512 void
513 main_imsg_compose_ospfe(int type, pid_t pid, void *data, u_int16_t datalen)
514 {
515 	imsg_compose_event(iev_ospfe, type, 0, pid, -1, data, datalen);
516 }
517 
518 void
519 main_imsg_compose_rde(int type, pid_t pid, void *data, u_int16_t datalen)
520 {
521 	imsg_compose_event(iev_rde, type, 0, pid, -1, data, datalen);
522 }
523 
524 void
525 imsg_event_add(struct imsgev *iev)
526 {
527 	iev->events = EV_READ;
528 	if (iev->ibuf.w.queued)
529 		iev->events |= EV_WRITE;
530 
531 	event_del(&iev->ev);
532 	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
533 	event_add(&iev->ev, NULL);
534 }
535 
536 int
537 imsg_compose_event(struct imsgev *iev, u_int16_t type, u_int32_t peerid,
538     pid_t pid, int fd, void *data, u_int16_t datalen)
539 {
540 	int	ret;
541 
542 	if ((ret = imsg_compose(&iev->ibuf, type, peerid,
543 	    pid, fd, data, datalen)) != -1)
544 		imsg_event_add(iev);
545 	return (ret);
546 }
547 
548 int
549 ospf_redistribute(struct kroute *kr, u_int32_t *metric)
550 {
551 	struct redistribute	*r;
552 	u_int8_t		 is_default = 0;
553 
554 	/* only allow 0.0.0.0/0 via REDIST_DEFAULT */
555 	if (kr->prefix.s_addr == INADDR_ANY && kr->prefixlen == 0)
556 		is_default = 1;
557 
558 	SIMPLEQ_FOREACH(r, &ospfd_conf->redist_list, entry) {
559 		switch (r->type & ~REDIST_NO) {
560 		case REDIST_LABEL:
561 			if (kr->rtlabel == r->label) {
562 				*metric = r->metric;
563 				return (r->type & REDIST_NO ? 0 : 1);
564 			}
565 			break;
566 		case REDIST_STATIC:
567 			/*
568 			 * Dynamic routes are not redistributable. Placed here
569 			 * so that link local addresses can be redistributed
570 			 * via a rtlabel.
571 			 */
572 			if (is_default)
573 				continue;
574 			if (kr->flags & F_DYNAMIC)
575 				continue;
576 			if (kr->flags & F_STATIC) {
577 				*metric = r->metric;
578 				return (r->type & REDIST_NO ? 0 : 1);
579 			}
580 			break;
581 		case REDIST_CONNECTED:
582 			if (is_default)
583 				continue;
584 			if (kr->flags & F_DYNAMIC)
585 				continue;
586 			if (kr->flags & F_CONNECTED) {
587 				*metric = r->metric;
588 				return (r->type & REDIST_NO ? 0 : 1);
589 			}
590 			break;
591 		case REDIST_ADDR:
592 			if (kr->flags & F_DYNAMIC)
593 				continue;
594 
595 			if (r->addr.s_addr == INADDR_ANY &&
596 			    r->mask.s_addr == INADDR_ANY) {
597 				if (is_default) {
598 					*metric = r->metric;
599 					return (r->type & REDIST_NO? 0 : 1);
600 				} else
601 					return (0);
602 			}
603 
604 			if ((kr->prefix.s_addr & r->mask.s_addr) ==
605 			    (r->addr.s_addr & r->mask.s_addr) &&
606 			    kr->prefixlen >= mask2prefixlen(r->mask.s_addr)) {
607 				*metric = r->metric;
608 				return (r->type & REDIST_NO ? 0 : 1);
609 			}
610 			break;
611 		case REDIST_DEFAULT:
612 			if (is_default) {
613 				*metric = r->metric;
614 				return (r->type & REDIST_NO ? 0 : 1);
615 			}
616 			break;
617 		}
618 	}
619 
620 	return (0);
621 }
622 
623 int
624 ospf_reload(void)
625 {
626 	struct area		*area;
627 	struct iface		*iface;
628 	struct ospfd_conf	*xconf;
629 	struct redistribute	*r;
630 
631 	if ((xconf = parse_config(conffile, ospfd_conf->opts)) == NULL)
632 		return (-1);
633 
634 	/* send config to childs */
635 	if (ospf_sendboth(IMSG_RECONF_CONF, xconf, sizeof(*xconf)) == -1)
636 		return (-1);
637 
638 	/* send interfaces */
639 	LIST_FOREACH(area, &xconf->area_list, entry) {
640 		if (ospf_sendboth(IMSG_RECONF_AREA, area, sizeof(*area)) == -1)
641 			return (-1);
642 
643 		SIMPLEQ_FOREACH(r, &area->redist_list, entry) {
644 			main_imsg_compose_rde(IMSG_RECONF_REDIST, 0, r,
645 			    sizeof(*r));
646 		}
647 		LIST_FOREACH(iface, &area->iface_list, entry) {
648 			if (ospf_sendboth(IMSG_RECONF_IFACE, iface,
649 			    sizeof(*iface)) == -1)
650 				return (-1);
651 			if (iface->auth_type == AUTH_CRYPT)
652 				if (md_list_send(&iface->auth_md_list,
653 				    iev_ospfe) == -1)
654 					return (-1);
655 		}
656 	}
657 
658 	if (ospf_sendboth(IMSG_RECONF_END, NULL, 0) == -1)
659 		return (-1);
660 
661 	merge_config(ospfd_conf, xconf);
662 	/* update redistribute lists */
663 	kr_reload();
664 	return (0);
665 }
666 
667 int
668 ospf_sendboth(enum imsg_type type, void *buf, u_int16_t len)
669 {
670 	if (imsg_compose_event(iev_ospfe, type, 0, 0, -1, buf, len) == -1)
671 		return (-1);
672 	if (imsg_compose_event(iev_rde, type, 0, 0, -1, buf, len) == -1)
673 		return (-1);
674 	return (0);
675 }
676 
677 void
678 merge_config(struct ospfd_conf *conf, struct ospfd_conf *xconf)
679 {
680 	struct area		*a, *xa, *na;
681 	struct iface		*iface;
682 	struct redistribute	*r;
683 	int			 rchange = 0;
684 
685 	/* change of rtr_id needs a restart */
686 	conf->flags = xconf->flags;
687 	conf->spf_delay = xconf->spf_delay;
688 	conf->spf_hold_time = xconf->spf_hold_time;
689 	if (SIMPLEQ_EMPTY(&conf->redist_list) !=
690 	    SIMPLEQ_EMPTY(&xconf->redist_list))
691 		rchange = 1;
692 	conf->rfc1583compat = xconf->rfc1583compat;
693 
694 	if (ospfd_process == PROC_MAIN) {
695 		/* main process does neither use areas nor interfaces */
696 		while ((r = SIMPLEQ_FIRST(&conf->redist_list)) != NULL) {
697 			SIMPLEQ_REMOVE_HEAD(&conf->redist_list, entry);
698 			free(r);
699 		}
700 		while ((r = SIMPLEQ_FIRST(&xconf->redist_list)) != NULL) {
701 			SIMPLEQ_REMOVE_HEAD(&xconf->redist_list, entry);
702 			SIMPLEQ_INSERT_TAIL(&conf->redist_list, r, entry);
703 		}
704 		goto done;
705 	}
706 
707 	/* merge areas and interfaces */
708 	for (a = LIST_FIRST(&conf->area_list); a != NULL; a = na) {
709 		na = LIST_NEXT(a, entry);
710 		/* find deleted areas */
711 		if ((xa = area_find(xconf, a->id)) == NULL) {
712 			if (ospfd_process == PROC_OSPF_ENGINE) {
713 				LIST_FOREACH(iface, &a->iface_list, entry)
714 					if_fsm(iface, IF_EVT_DOWN);
715 			}
716 			LIST_REMOVE(a, entry);
717 			area_del(a);
718 		}
719 	}
720 
721 	for (xa = LIST_FIRST(&xconf->area_list); xa != NULL; xa = na) {
722 		na = LIST_NEXT(xa, entry);
723 		if ((a = area_find(conf, xa->id)) == NULL) {
724 			LIST_REMOVE(xa, entry);
725 			LIST_INSERT_HEAD(&conf->area_list, xa, entry);
726 			if (ospfd_process == PROC_OSPF_ENGINE) {
727 				/* start interfaces */
728 				ospfe_demote_area(xa, 0);
729 				LIST_FOREACH(iface, &xa->iface_list, entry) {
730 					if_init(conf, iface);
731 					if (if_fsm(iface, IF_EVT_UP)) {
732 						log_debug("error starting "
733 						    "interface %s",
734 						    iface->name);
735 					}
736 				}
737 			}
738 			/* no need to merge interfaces */
739 			continue;
740 		}
741 		/*
742 		 * stub is not yet used but switching between stub and normal
743 		 * will be another painful job.
744 		 */
745 		if (a->stub != xa->stub && ospfd_process == PROC_OSPF_ENGINE)
746 			a->dirty = 1; /* force rtr LSA update */
747 		if (xa->stub && ospfd_process == PROC_RDE_ENGINE) {
748 			while ((r = SIMPLEQ_FIRST(&a->redist_list)) != NULL) {
749 				SIMPLEQ_REMOVE_HEAD(&a->redist_list, entry);
750 				free(r);
751 			}
752 
753 			while ((r = SIMPLEQ_FIRST(&xa->redist_list)) != NULL) {
754 				SIMPLEQ_REMOVE_HEAD(&xa->redist_list, entry);
755 				SIMPLEQ_INSERT_TAIL(&a->redist_list, r, entry);
756 			}
757 		}
758 
759 		a->stub = xa->stub;
760 		a->stub_default_cost = xa->stub_default_cost;
761 		if (ospfd_process == PROC_RDE_ENGINE)
762 			a->dirty = 1; /* force SPF tree recalculation */
763 
764 		/* merge interfaces */
765 		if (merge_interfaces(a, xa) &&
766 		    ospfd_process == PROC_OSPF_ENGINE)
767 			a->dirty = 1; /* force rtr LSA update */
768 	}
769 
770 	if (ospfd_process == PROC_OSPF_ENGINE) {
771 		LIST_FOREACH(a, &conf->area_list, entry) {
772 			LIST_FOREACH(iface, &a->iface_list, entry) {
773 				if (iface->state == IF_STA_NEW) {
774 					iface->state = IF_STA_DOWN;
775 					if_init(conf, iface);
776 					if (if_fsm(iface, IF_EVT_UP)) {
777 						log_debug("error starting "
778 						    "interface %s",
779 						    iface->name);
780 					}
781 				}
782 			}
783 			if (a->dirty || rchange) {
784 				a->dirty = 0;
785 				orig_rtr_lsa(a);
786 			}
787 		}
788 	}
789 	if (ospfd_process == PROC_RDE_ENGINE) {
790 		LIST_FOREACH(a, &conf->area_list, entry) {
791 			if (a->dirty) {
792 				start_spf_timer();
793 				break;
794 			}
795 		}
796 	}
797 
798 done:
799 	while ((a = LIST_FIRST(&xconf->area_list)) != NULL) {
800 		LIST_REMOVE(a, entry);
801 		area_del(a);
802 	}
803 	free(xconf);
804 }
805 
806 int
807 merge_interfaces(struct area *a, struct area *xa)
808 {
809 	struct iface	*i, *xi, *ni;
810 	int		 dirty = 0;
811 
812 	/* problems:
813 	 * - new interfaces (easy)
814 	 * - deleted interfaces (needs to be done via fsm?)
815 	 * - changing passive (painful?)
816 	 */
817 	for (i = LIST_FIRST(&a->iface_list); i != NULL; i = ni) {
818 		ni = LIST_NEXT(i, entry);
819 		if (iface_lookup(xa, i) == NULL) {
820 			log_debug("merge_interfaces: proc %d area %s removing "
821 			    "interface %s", ospfd_process, inet_ntoa(a->id),
822 			    i->name);
823 			if (ospfd_process == PROC_OSPF_ENGINE)
824 				if_fsm(i, IF_EVT_DOWN);
825 			LIST_REMOVE(i, entry);
826 			if_del(i);
827 		}
828 	}
829 
830 	for (xi = LIST_FIRST(&xa->iface_list); xi != NULL; xi = ni) {
831 		ni = LIST_NEXT(xi, entry);
832 		if ((i = iface_lookup(a, xi)) == NULL) {
833 			/* new interface but delay initialisation */
834 			log_debug("merge_interfaces: proc %d area %s adding "
835 			    "interface %s", ospfd_process, inet_ntoa(a->id),
836 			    xi->name);
837 			LIST_REMOVE(xi, entry);
838 			LIST_INSERT_HEAD(&a->iface_list, xi, entry);
839 			xi->area = a;
840 			if (ospfd_process == PROC_OSPF_ENGINE)
841 				xi->state = IF_STA_NEW;
842 			continue;
843 		}
844 		log_debug("merge_interfaces: proc %d area %s merging "
845 		    "interface %s", ospfd_process, inet_ntoa(a->id), i->name);
846 		i->dst = xi->dst;
847 		i->abr_id = xi->abr_id;
848 		i->baudrate = xi->baudrate;
849 		i->dead_interval = xi->dead_interval;
850 		i->mtu = xi->mtu;
851 		i->transmit_delay = xi->transmit_delay;
852 		i->hello_interval = xi->hello_interval;
853 		i->rxmt_interval = xi->rxmt_interval;
854 		if (i->metric != xi->metric)
855 			dirty = 1;
856 		i->metric = xi->metric;
857 		i->priority = xi->priority;
858 		if (i->self)
859 			i->self->priority = i->priority;
860 		i->flags = xi->flags; /* needed? */
861 		i->type = xi->type; /* needed? */
862 		i->media_type = xi->media_type; /* needed? */
863 		i->linkstate = xi->linkstate; /* needed? */
864 
865 		i->auth_type = xi->auth_type;
866 		strncpy(i->auth_key, xi->auth_key, MAX_SIMPLE_AUTH_LEN);
867 		md_list_clr(&i->auth_md_list);
868 		md_list_copy(&i->auth_md_list, &xi->auth_md_list);
869 
870 		if (i->passive != xi->passive) {
871 			/* need to restart interface to cope with this change */
872 			if (ospfd_process == PROC_OSPF_ENGINE)
873 				if_fsm(i, IF_EVT_DOWN);
874 			i->passive = xi->passive;
875 			if (ospfd_process == PROC_OSPF_ENGINE)
876 				if_fsm(i, IF_EVT_UP);
877 		}
878 	}
879 	return (dirty);
880 }
881 
882 struct iface *
883 iface_lookup(struct area *area, struct iface *iface)
884 {
885 	struct iface	*i;
886 
887 	LIST_FOREACH(i, &area->iface_list, entry)
888 		if (i->ifindex == iface->ifindex &&
889 		    i->addr.s_addr == iface->addr.s_addr &&
890 		    i->mask.s_addr == iface->mask.s_addr)
891 			return (i);
892 	return (NULL);
893 }
894