xref: /openbsd-src/usr.sbin/ospfd/ospfd.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: ospfd.c,v 1.81 2014/07/12 20:16:38 krw 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 ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
388 			fatal("msgbuf_write");
389 		if (n == 0)	/* connection closed */
390 			shut = 1;
391 	}
392 
393 	for (;;) {
394 		if ((n = imsg_get(ibuf, &imsg)) == -1)
395 			fatal("imsg_get");
396 
397 		if (n == 0)
398 			break;
399 
400 		switch (imsg.hdr.type) {
401 		case IMSG_CTL_RELOAD:
402 			if (ospf_reload() == -1)
403 				log_warnx("configuration reload failed");
404 			else
405 				log_debug("configuration reloaded");
406 			break;
407 		case IMSG_CTL_FIB_COUPLE:
408 			kr_fib_couple();
409 			break;
410 		case IMSG_CTL_FIB_DECOUPLE:
411 			kr_fib_decouple();
412 			break;
413 		case IMSG_CTL_FIB_RELOAD:
414 			kr_fib_reload();
415 			break;
416 		case IMSG_CTL_KROUTE:
417 		case IMSG_CTL_KROUTE_ADDR:
418 			kr_show_route(&imsg);
419 			break;
420 		case IMSG_CTL_IFINFO:
421 			if (imsg.hdr.len == IMSG_HEADER_SIZE)
422 				kr_ifinfo(NULL, imsg.hdr.pid);
423 			else if (imsg.hdr.len == IMSG_HEADER_SIZE + IFNAMSIZ)
424 				kr_ifinfo(imsg.data, imsg.hdr.pid);
425 			else
426 				log_warnx("IFINFO request with wrong len");
427 			break;
428 		case IMSG_DEMOTE:
429 			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(dmsg))
430 				fatalx("invalid size of OE request");
431 			memcpy(&dmsg, imsg.data, sizeof(dmsg));
432 			carp_demote_set(dmsg.demote_group, dmsg.level);
433 			break;
434 		case IMSG_CTL_LOG_VERBOSE:
435 			/* already checked by ospfe */
436 			memcpy(&verbose, imsg.data, sizeof(verbose));
437 			log_verbose(verbose);
438 			break;
439 		default:
440 			log_debug("main_dispatch_ospfe: error handling imsg %d",
441 			    imsg.hdr.type);
442 			break;
443 		}
444 		imsg_free(&imsg);
445 	}
446 	if (!shut)
447 		imsg_event_add(iev);
448 	else {
449 		/* this pipe is dead, so remove the event handler */
450 		event_del(&iev->ev);
451 		event_loopexit(NULL);
452 	}
453 }
454 
455 /* ARGSUSED */
456 void
457 main_dispatch_rde(int fd, short event, void *bula)
458 {
459 	struct imsgev	*iev = bula;
460 	struct imsgbuf  *ibuf;
461 	struct imsg	 imsg;
462 	ssize_t		 n;
463 	int		 count, shut = 0;
464 
465 	ibuf = &iev->ibuf;
466 
467 	if (event & EV_READ) {
468 		if ((n = imsg_read(ibuf)) == -1)
469 			fatal("imsg_read error");
470 		if (n == 0)	/* connection closed */
471 			shut = 1;
472 	}
473 	if (event & EV_WRITE) {
474 		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
475 			fatal("msgbuf_write");
476 		if (n == 0)	/* connection closed */
477 			shut = 1;
478 	}
479 
480 	for (;;) {
481 		if ((n = imsg_get(ibuf, &imsg)) == -1)
482 			fatal("imsg_get");
483 
484 		if (n == 0)
485 			break;
486 
487 		switch (imsg.hdr.type) {
488 		case IMSG_KROUTE_CHANGE:
489 			count = (imsg.hdr.len - IMSG_HEADER_SIZE) /
490 			    sizeof(struct kroute);
491 			if (kr_change(imsg.data, count))
492 				log_warn("main_dispatch_rde: error changing "
493 				    "route");
494 			break;
495 		case IMSG_KROUTE_DELETE:
496 			if (kr_delete(imsg.data))
497 				log_warn("main_dispatch_rde: error deleting "
498 				    "route");
499 			break;
500 		default:
501 			log_debug("main_dispatch_rde: error handling imsg %d",
502 			    imsg.hdr.type);
503 			break;
504 		}
505 		imsg_free(&imsg);
506 	}
507 	if (!shut)
508 		imsg_event_add(iev);
509 	else {
510 		/* this pipe is dead, so remove the event handler */
511 		event_del(&iev->ev);
512 		event_loopexit(NULL);
513 	}
514 }
515 
516 void
517 main_imsg_compose_ospfe(int type, pid_t pid, void *data, u_int16_t datalen)
518 {
519 	imsg_compose_event(iev_ospfe, type, 0, pid, -1, data, datalen);
520 }
521 
522 void
523 main_imsg_compose_rde(int type, pid_t pid, void *data, u_int16_t datalen)
524 {
525 	imsg_compose_event(iev_rde, type, 0, pid, -1, data, datalen);
526 }
527 
528 void
529 imsg_event_add(struct imsgev *iev)
530 {
531 	iev->events = EV_READ;
532 	if (iev->ibuf.w.queued)
533 		iev->events |= EV_WRITE;
534 
535 	event_del(&iev->ev);
536 	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
537 	event_add(&iev->ev, NULL);
538 }
539 
540 int
541 imsg_compose_event(struct imsgev *iev, u_int16_t type, u_int32_t peerid,
542     pid_t pid, int fd, void *data, u_int16_t datalen)
543 {
544 	int	ret;
545 
546 	if ((ret = imsg_compose(&iev->ibuf, type, peerid,
547 	    pid, fd, data, datalen)) != -1)
548 		imsg_event_add(iev);
549 	return (ret);
550 }
551 
552 int
553 ospf_redistribute(struct kroute *kr, u_int32_t *metric)
554 {
555 	struct redistribute	*r;
556 	u_int8_t		 is_default = 0;
557 
558 	/* only allow 0.0.0.0/0 via REDIST_DEFAULT */
559 	if (kr->prefix.s_addr == INADDR_ANY && kr->prefixlen == 0)
560 		is_default = 1;
561 
562 	SIMPLEQ_FOREACH(r, &ospfd_conf->redist_list, entry) {
563 		switch (r->type & ~REDIST_NO) {
564 		case REDIST_LABEL:
565 			if (kr->rtlabel == r->label) {
566 				*metric = r->metric;
567 				return (r->type & REDIST_NO ? 0 : 1);
568 			}
569 			break;
570 		case REDIST_STATIC:
571 			/*
572 			 * Dynamic routes are not redistributable. Placed here
573 			 * so that link local addresses can be redistributed
574 			 * via a rtlabel.
575 			 */
576 			if (is_default)
577 				continue;
578 			if (kr->flags & F_DYNAMIC)
579 				continue;
580 			if (kr->flags & F_STATIC) {
581 				*metric = r->metric;
582 				return (r->type & REDIST_NO ? 0 : 1);
583 			}
584 			break;
585 		case REDIST_CONNECTED:
586 			if (is_default)
587 				continue;
588 			if (kr->flags & F_DYNAMIC)
589 				continue;
590 			if (kr->flags & F_CONNECTED) {
591 				*metric = r->metric;
592 				return (r->type & REDIST_NO ? 0 : 1);
593 			}
594 			break;
595 		case REDIST_ADDR:
596 			if (kr->flags & F_DYNAMIC)
597 				continue;
598 
599 			if (r->addr.s_addr == INADDR_ANY &&
600 			    r->mask.s_addr == INADDR_ANY) {
601 				if (is_default) {
602 					*metric = r->metric;
603 					return (r->type & REDIST_NO ? 0 : 1);
604 				} else
605 					return (0);
606 			}
607 
608 			if ((kr->prefix.s_addr & r->mask.s_addr) ==
609 			    (r->addr.s_addr & r->mask.s_addr) &&
610 			    kr->prefixlen >= mask2prefixlen(r->mask.s_addr)) {
611 				*metric = r->metric;
612 				return (r->type & REDIST_NO ? 0 : 1);
613 			}
614 			break;
615 		case REDIST_DEFAULT:
616 			if (is_default) {
617 				*metric = r->metric;
618 				return (r->type & REDIST_NO ? 0 : 1);
619 			}
620 			break;
621 		}
622 	}
623 
624 	return (0);
625 }
626 
627 int
628 ospf_reload(void)
629 {
630 	struct area		*area;
631 	struct iface		*iface;
632 	struct ospfd_conf	*xconf;
633 	struct redistribute	*r;
634 
635 	if ((xconf = parse_config(conffile, ospfd_conf->opts)) == NULL)
636 		return (-1);
637 
638 	/* send config to childs */
639 	if (ospf_sendboth(IMSG_RECONF_CONF, xconf, sizeof(*xconf)) == -1)
640 		return (-1);
641 
642 	/* send interfaces */
643 	LIST_FOREACH(area, &xconf->area_list, entry) {
644 		if (ospf_sendboth(IMSG_RECONF_AREA, area, sizeof(*area)) == -1)
645 			return (-1);
646 
647 		SIMPLEQ_FOREACH(r, &area->redist_list, entry) {
648 			main_imsg_compose_rde(IMSG_RECONF_REDIST, 0, r,
649 			    sizeof(*r));
650 		}
651 		LIST_FOREACH(iface, &area->iface_list, entry) {
652 			if (ospf_sendboth(IMSG_RECONF_IFACE, iface,
653 			    sizeof(*iface)) == -1)
654 				return (-1);
655 			if (iface->auth_type == AUTH_CRYPT)
656 				if (md_list_send(&iface->auth_md_list,
657 				    iev_ospfe) == -1)
658 					return (-1);
659 		}
660 	}
661 
662 	if (ospf_sendboth(IMSG_RECONF_END, NULL, 0) == -1)
663 		return (-1);
664 
665 	merge_config(ospfd_conf, xconf);
666 	/* update redistribute lists */
667 	kr_reload();
668 	return (0);
669 }
670 
671 int
672 ospf_sendboth(enum imsg_type type, void *buf, u_int16_t len)
673 {
674 	if (imsg_compose_event(iev_ospfe, type, 0, 0, -1, buf, len) == -1)
675 		return (-1);
676 	if (imsg_compose_event(iev_rde, type, 0, 0, -1, buf, len) == -1)
677 		return (-1);
678 	return (0);
679 }
680 
681 void
682 merge_config(struct ospfd_conf *conf, struct ospfd_conf *xconf)
683 {
684 	struct area		*a, *xa, *na;
685 	struct iface		*iface;
686 	struct redistribute	*r;
687 	int			 rchange = 0;
688 
689 	/* change of rtr_id needs a restart */
690 	conf->flags = xconf->flags;
691 	conf->spf_delay = xconf->spf_delay;
692 	conf->spf_hold_time = xconf->spf_hold_time;
693 	if (SIMPLEQ_EMPTY(&conf->redist_list) !=
694 	    SIMPLEQ_EMPTY(&xconf->redist_list))
695 		rchange = 1;
696 	conf->rfc1583compat = xconf->rfc1583compat;
697 
698 	if (ospfd_process == PROC_MAIN) {
699 		/* main process does neither use areas nor interfaces */
700 		while ((r = SIMPLEQ_FIRST(&conf->redist_list)) != NULL) {
701 			SIMPLEQ_REMOVE_HEAD(&conf->redist_list, entry);
702 			free(r);
703 		}
704 		while ((r = SIMPLEQ_FIRST(&xconf->redist_list)) != NULL) {
705 			SIMPLEQ_REMOVE_HEAD(&xconf->redist_list, entry);
706 			SIMPLEQ_INSERT_TAIL(&conf->redist_list, r, entry);
707 		}
708 		goto done;
709 	}
710 
711 	/* merge areas and interfaces */
712 	for (a = LIST_FIRST(&conf->area_list); a != NULL; a = na) {
713 		na = LIST_NEXT(a, entry);
714 		/* find deleted areas */
715 		if ((xa = area_find(xconf, a->id)) == NULL) {
716 			if (ospfd_process == PROC_OSPF_ENGINE) {
717 				LIST_FOREACH(iface, &a->iface_list, entry)
718 					if_fsm(iface, IF_EVT_DOWN);
719 			}
720 			LIST_REMOVE(a, entry);
721 			area_del(a);
722 		}
723 	}
724 
725 	for (xa = LIST_FIRST(&xconf->area_list); xa != NULL; xa = na) {
726 		na = LIST_NEXT(xa, entry);
727 		if ((a = area_find(conf, xa->id)) == NULL) {
728 			LIST_REMOVE(xa, entry);
729 			LIST_INSERT_HEAD(&conf->area_list, xa, entry);
730 			if (ospfd_process == PROC_OSPF_ENGINE) {
731 				/* start interfaces */
732 				ospfe_demote_area(xa, 0);
733 				LIST_FOREACH(iface, &xa->iface_list, entry) {
734 					if_init(conf, iface);
735 					if (if_fsm(iface, IF_EVT_UP)) {
736 						log_debug("error starting "
737 						    "interface %s",
738 						    iface->name);
739 					}
740 				}
741 			}
742 			/* no need to merge interfaces */
743 			continue;
744 		}
745 		/*
746 		 * stub is not yet used but switching between stub and normal
747 		 * will be another painful job.
748 		 */
749 		if (a->stub != xa->stub && ospfd_process == PROC_OSPF_ENGINE)
750 			a->dirty = 1; /* force rtr LSA update */
751 		if (xa->stub && ospfd_process == PROC_RDE_ENGINE) {
752 			while ((r = SIMPLEQ_FIRST(&a->redist_list)) != NULL) {
753 				SIMPLEQ_REMOVE_HEAD(&a->redist_list, entry);
754 				free(r);
755 			}
756 
757 			while ((r = SIMPLEQ_FIRST(&xa->redist_list)) != NULL) {
758 				SIMPLEQ_REMOVE_HEAD(&xa->redist_list, entry);
759 				SIMPLEQ_INSERT_TAIL(&a->redist_list, r, entry);
760 			}
761 		}
762 
763 		a->stub = xa->stub;
764 		a->stub_default_cost = xa->stub_default_cost;
765 		if (ospfd_process == PROC_RDE_ENGINE)
766 			a->dirty = 1; /* force SPF tree recalculation */
767 
768 		/* merge interfaces */
769 		if (merge_interfaces(a, xa) &&
770 		    ospfd_process == PROC_OSPF_ENGINE)
771 			a->dirty = 1; /* force rtr LSA update */
772 	}
773 
774 	if (ospfd_process == PROC_OSPF_ENGINE) {
775 		LIST_FOREACH(a, &conf->area_list, entry) {
776 			LIST_FOREACH(iface, &a->iface_list, entry) {
777 				if (iface->state == IF_STA_NEW) {
778 					iface->state = IF_STA_DOWN;
779 					if_init(conf, iface);
780 					if (if_fsm(iface, IF_EVT_UP)) {
781 						log_debug("error starting "
782 						    "interface %s",
783 						    iface->name);
784 					}
785 				}
786 			}
787 			if (a->dirty || rchange) {
788 				a->dirty = 0;
789 				orig_rtr_lsa(a);
790 			}
791 		}
792 	}
793 	if (ospfd_process == PROC_RDE_ENGINE) {
794 		LIST_FOREACH(a, &conf->area_list, entry) {
795 			if (a->dirty) {
796 				start_spf_timer();
797 				break;
798 			}
799 		}
800 	}
801 
802 done:
803 	while ((a = LIST_FIRST(&xconf->area_list)) != NULL) {
804 		LIST_REMOVE(a, entry);
805 		area_del(a);
806 	}
807 	free(xconf);
808 }
809 
810 int
811 merge_interfaces(struct area *a, struct area *xa)
812 {
813 	struct iface	*i, *xi, *ni;
814 	int		 dirty = 0;
815 
816 	/* problems:
817 	 * - new interfaces (easy)
818 	 * - deleted interfaces (needs to be done via fsm?)
819 	 * - changing passive (painful?)
820 	 */
821 	for (i = LIST_FIRST(&a->iface_list); i != NULL; i = ni) {
822 		ni = LIST_NEXT(i, entry);
823 		if (iface_lookup(xa, i) == NULL) {
824 			log_debug("merge_interfaces: proc %d area %s removing "
825 			    "interface %s", ospfd_process, inet_ntoa(a->id),
826 			    i->name);
827 			if (ospfd_process == PROC_OSPF_ENGINE)
828 				if_fsm(i, IF_EVT_DOWN);
829 			LIST_REMOVE(i, entry);
830 			if_del(i);
831 		}
832 	}
833 
834 	for (xi = LIST_FIRST(&xa->iface_list); xi != NULL; xi = ni) {
835 		ni = LIST_NEXT(xi, entry);
836 		if ((i = iface_lookup(a, xi)) == NULL) {
837 			/* new interface but delay initialisation */
838 			log_debug("merge_interfaces: proc %d area %s adding "
839 			    "interface %s", ospfd_process, inet_ntoa(a->id),
840 			    xi->name);
841 			LIST_REMOVE(xi, entry);
842 			LIST_INSERT_HEAD(&a->iface_list, xi, entry);
843 			xi->area = a;
844 			if (ospfd_process == PROC_OSPF_ENGINE)
845 				xi->state = IF_STA_NEW;
846 			continue;
847 		}
848 		log_debug("merge_interfaces: proc %d area %s merging "
849 		    "interface %s", ospfd_process, inet_ntoa(a->id), i->name);
850 		i->dst = xi->dst;
851 		i->abr_id = xi->abr_id;
852 		i->baudrate = xi->baudrate;
853 		i->dead_interval = xi->dead_interval;
854 		i->mtu = xi->mtu;
855 		i->transmit_delay = xi->transmit_delay;
856 		i->hello_interval = xi->hello_interval;
857 		i->rxmt_interval = xi->rxmt_interval;
858 		if (i->metric != xi->metric)
859 			dirty = 1;
860 		i->metric = xi->metric;
861 		i->priority = xi->priority;
862 		if (i->self)
863 			i->self->priority = i->priority;
864 		i->flags = xi->flags; /* needed? */
865 		i->type = xi->type; /* needed? */
866 		i->media_type = xi->media_type; /* needed? */
867 		i->linkstate = xi->linkstate; /* needed? */
868 
869 		i->auth_type = xi->auth_type;
870 		strncpy(i->auth_key, xi->auth_key, MAX_SIMPLE_AUTH_LEN);
871 		md_list_clr(&i->auth_md_list);
872 		md_list_copy(&i->auth_md_list, &xi->auth_md_list);
873 
874 		if (i->passive != xi->passive) {
875 			/* need to restart interface to cope with this change */
876 			if (ospfd_process == PROC_OSPF_ENGINE)
877 				if_fsm(i, IF_EVT_DOWN);
878 			i->passive = xi->passive;
879 			if (ospfd_process == PROC_OSPF_ENGINE)
880 				if_fsm(i, IF_EVT_UP);
881 		}
882 	}
883 	return (dirty);
884 }
885 
886 struct iface *
887 iface_lookup(struct area *area, struct iface *iface)
888 {
889 	struct iface	*i;
890 
891 	LIST_FOREACH(i, &area->iface_list, entry)
892 		if (i->ifindex == iface->ifindex &&
893 		    i->addr.s_addr == iface->addr.s_addr &&
894 		    i->mask.s_addr == iface->mask.s_addr)
895 			return (i);
896 	return (NULL);
897 }
898