ldpd.c revision 1.61
1/*	$OpenBSD: ldpd.c,v 1.61 2017/03/03 23:30:57 renato Exp $ */
2
3/*
4 * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
5 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
6 * Copyright (c) 2004, 2008 Esben Norby <norby@openbsd.org>
7 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22#include <sys/types.h>
23#include <sys/wait.h>
24#include <err.h>
25#include <errno.h>
26#include <pwd.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <signal.h>
31#include <unistd.h>
32
33#include "ldpd.h"
34#include "ldpe.h"
35#include "lde.h"
36#include "log.h"
37
38static void		 main_sig_handler(int, short, void *);
39static __dead void	 usage(void);
40static __dead void	 ldpd_shutdown(void);
41static pid_t		 start_child(enum ldpd_process, char *, int, int, int,
42			    char *);
43static void		 main_dispatch_ldpe(int, short, void *);
44static void		 main_dispatch_lde(int, short, void *);
45static int		 main_imsg_compose_both(enum imsg_type, void *,
46			    uint16_t);
47static int		 main_imsg_send_ipc_sockets(struct imsgbuf *,
48			    struct imsgbuf *);
49static void		 main_imsg_send_net_sockets(int);
50static void		 main_imsg_send_net_socket(int, enum socket_type);
51static int		 main_imsg_send_config(struct ldpd_conf *);
52static int		 ldp_reload(void);
53static void		 merge_global(struct ldpd_conf *, struct ldpd_conf *);
54static void		 merge_af(int, struct ldpd_af_conf *,
55			    struct ldpd_af_conf *);
56static void		 merge_ifaces(struct ldpd_conf *, struct ldpd_conf *);
57static void		 merge_iface_af(struct iface_af *, struct iface_af *);
58static void		 merge_tnbrs(struct ldpd_conf *, struct ldpd_conf *);
59static void		 merge_nbrps(struct ldpd_conf *, struct ldpd_conf *);
60static void		 merge_l2vpns(struct ldpd_conf *, struct ldpd_conf *);
61static void		 merge_l2vpn(struct ldpd_conf *, struct l2vpn *,
62			    struct l2vpn *);
63
64struct ldpd_global	 global;
65struct ldpd_conf	*ldpd_conf;
66
67static char		*conffile;
68static struct imsgev	*iev_ldpe;
69static struct imsgev	*iev_lde;
70static pid_t		 ldpe_pid;
71static pid_t		 lde_pid;
72
73/* ARGSUSED */
74static void
75main_sig_handler(int sig, short event, void *arg)
76{
77	/* signal handler rules don't apply, libevent decouples for us */
78	switch (sig) {
79	case SIGTERM:
80	case SIGINT:
81		ldpd_shutdown();
82		/* NOTREACHED */
83	case SIGHUP:
84		if (ldp_reload() == -1)
85			log_warnx("configuration reload failed");
86		else
87			log_debug("configuration reloaded");
88		break;
89	default:
90		fatalx("unexpected signal");
91		/* NOTREACHED */
92	}
93}
94
95static __dead void
96usage(void)
97{
98	extern char *__progname;
99
100	fprintf(stderr, "usage: %s [-dnv] [-D macro=value] [-f file]"
101	    " [-s socket]\n", __progname);
102	exit(1);
103}
104
105int
106main(int argc, char *argv[])
107{
108	struct event		 ev_sigint, ev_sigterm, ev_sighup;
109	char			*saved_argv0;
110	int			 ch;
111	int			 debug = 0, lflag = 0, eflag = 0;
112	char			*sockname;
113	int			 pipe_parent2ldpe[2];
114	int			 pipe_parent2lde[2];
115
116	conffile = CONF_FILE;
117	ldpd_process = PROC_MAIN;
118	log_procname = log_procnames[ldpd_process];
119	sockname = LDPD_SOCKET;
120
121	log_init(1);	/* log to stderr until daemonized */
122	log_verbose(1);
123
124	saved_argv0 = argv[0];
125	if (saved_argv0 == NULL)
126		saved_argv0 = "ldpd";
127
128	while ((ch = getopt(argc, argv, "dD:f:ns:vLE")) != -1) {
129		switch (ch) {
130		case 'd':
131			debug = 1;
132			break;
133		case 'D':
134			if (cmdline_symset(optarg) < 0)
135				log_warnx("could not parse macro definition %s",
136				    optarg);
137			break;
138		case 'f':
139			conffile = optarg;
140			break;
141		case 'n':
142			global.cmd_opts |= LDPD_OPT_NOACTION;
143			break;
144		case 's':
145			sockname = optarg;
146			break;
147		case 'v':
148			if (global.cmd_opts & LDPD_OPT_VERBOSE)
149				global.cmd_opts |= LDPD_OPT_VERBOSE2;
150			global.cmd_opts |= LDPD_OPT_VERBOSE;
151			break;
152		case 'L':
153			lflag = 1;
154			break;
155		case 'E':
156			eflag = 1;
157			break;
158		default:
159			usage();
160			/* NOTREACHED */
161		}
162	}
163
164	argc -= optind;
165	argv += optind;
166	if (argc > 0 || (lflag && eflag))
167		usage();
168
169	if (lflag)
170		lde(debug, global.cmd_opts & LDPD_OPT_VERBOSE);
171	else if (eflag)
172		ldpe(debug, global.cmd_opts & LDPD_OPT_VERBOSE, sockname);
173
174	/* fetch interfaces early */
175	kif_init();
176
177	/* parse config file */
178	if ((ldpd_conf = parse_config(conffile)) == NULL ) {
179		kif_clear();
180		exit(1);
181	}
182
183	if (global.cmd_opts & LDPD_OPT_NOACTION) {
184		if (global.cmd_opts & LDPD_OPT_VERBOSE)
185			print_config(ldpd_conf);
186		else
187			fprintf(stderr, "configuration OK\n");
188		kif_clear();
189		exit(0);
190	}
191
192	/* check for root privileges  */
193	if (geteuid())
194		errx(1, "need root privileges");
195
196	/* check for ldpd user */
197	if (getpwnam(LDPD_USER) == NULL)
198		errx(1, "unknown user %s", LDPD_USER);
199
200	log_init(debug);
201	log_verbose(global.cmd_opts & (LDPD_OPT_VERBOSE | LDPD_OPT_VERBOSE2));
202
203	if (!debug)
204		daemon(1, 0);
205
206	log_info("startup");
207
208	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
209	    PF_UNSPEC, pipe_parent2ldpe) == -1)
210		fatal("socketpair");
211	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
212	    PF_UNSPEC, pipe_parent2lde) == -1)
213		fatal("socketpair");
214
215	/* start children */
216	lde_pid = start_child(PROC_LDE_ENGINE, saved_argv0,
217	    pipe_parent2lde[1], debug, global.cmd_opts & LDPD_OPT_VERBOSE,
218	    NULL);
219	ldpe_pid = start_child(PROC_LDP_ENGINE, saved_argv0,
220	    pipe_parent2ldpe[1], debug, global.cmd_opts & LDPD_OPT_VERBOSE,
221	    sockname);
222
223	event_init();
224
225	/* setup signal handler */
226	signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
227	signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
228	signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL);
229	signal_add(&ev_sigint, NULL);
230	signal_add(&ev_sigterm, NULL);
231	signal_add(&ev_sighup, NULL);
232	signal(SIGPIPE, SIG_IGN);
233
234	/* setup pipes to children */
235	if ((iev_ldpe = malloc(sizeof(struct imsgev))) == NULL ||
236	    (iev_lde = malloc(sizeof(struct imsgev))) == NULL)
237		fatal(NULL);
238	imsg_init(&iev_ldpe->ibuf, pipe_parent2ldpe[0]);
239	iev_ldpe->handler = main_dispatch_ldpe;
240	imsg_init(&iev_lde->ibuf, pipe_parent2lde[0]);
241	iev_lde->handler = main_dispatch_lde;
242
243	/* setup event handler */
244	iev_ldpe->events = EV_READ;
245	event_set(&iev_ldpe->ev, iev_ldpe->ibuf.fd, iev_ldpe->events,
246	    iev_ldpe->handler, iev_ldpe);
247	event_add(&iev_ldpe->ev, NULL);
248
249	iev_lde->events = EV_READ;
250	event_set(&iev_lde->ev, iev_lde->ibuf.fd, iev_lde->events,
251	    iev_lde->handler, iev_lde);
252	event_add(&iev_lde->ev, NULL);
253
254	if (main_imsg_send_ipc_sockets(&iev_ldpe->ibuf, &iev_lde->ibuf))
255		fatal("could not establish imsg links");
256	main_imsg_send_config(ldpd_conf);
257
258	/* notify ldpe about existing interfaces and addresses */
259	kif_redistribute(NULL);
260
261	if (kr_init(!(ldpd_conf->flags & F_LDPD_NO_FIB_UPDATE)) == -1)
262		fatalx("kr_init failed");
263
264	if (ldpd_conf->ipv4.flags & F_LDPD_AF_ENABLED)
265		main_imsg_send_net_sockets(AF_INET);
266	if (ldpd_conf->ipv6.flags & F_LDPD_AF_ENABLED)
267		main_imsg_send_net_sockets(AF_INET6);
268
269	/* remove unneeded stuff from config */
270		/* ... */
271
272	event_dispatch();
273
274	ldpd_shutdown();
275	/* NOTREACHED */
276	return (0);
277}
278
279static __dead void
280ldpd_shutdown(void)
281{
282	pid_t		 pid;
283	int		 status;
284
285	/* close pipes */
286	msgbuf_clear(&iev_ldpe->ibuf.w);
287	close(iev_ldpe->ibuf.fd);
288	msgbuf_clear(&iev_lde->ibuf.w);
289	close(iev_lde->ibuf.fd);
290
291	kr_shutdown();
292	config_clear(ldpd_conf);
293
294	log_debug("waiting for children to terminate");
295	do {
296		pid = wait(&status);
297		if (pid == -1) {
298			if (errno != EINTR && errno != ECHILD)
299				fatal("wait");
300		} else if (WIFSIGNALED(status))
301			log_warnx("%s terminated; signal %d",
302			    (pid == lde_pid) ? "label decision engine" :
303			    "ldp engine", WTERMSIG(status));
304	} while (pid != -1 || (pid == -1 && errno == EINTR));
305
306	free(iev_ldpe);
307	free(iev_lde);
308
309	log_info("terminating");
310	exit(0);
311}
312
313static pid_t
314start_child(enum ldpd_process p, char *argv0, int fd, int debug, int verbose,
315    char *sockname)
316{
317	char	*argv[5];
318	int	 argc = 0;
319	pid_t	 pid;
320
321	switch (pid = fork()) {
322	case -1:
323		fatal("cannot fork");
324	case 0:
325		break;
326	default:
327		close(fd);
328		return (pid);
329	}
330
331	if (dup2(fd, 3) == -1)
332		fatal("cannot setup imsg fd");
333
334	argv[argc++] = argv0;
335	switch (p) {
336	case PROC_MAIN:
337		fatalx("Can not start main process");
338	case PROC_LDE_ENGINE:
339		argv[argc++] = "-L";
340		break;
341	case PROC_LDP_ENGINE:
342		argv[argc++] = "-E";
343		break;
344	}
345	if (debug)
346		argv[argc++] = "-d";
347	if (verbose)
348		argv[argc++] = "-v";
349	if (sockname) {
350		argv[argc++] = "-s";
351		argv[argc++] = sockname;
352	}
353	argv[argc++] = NULL;
354
355	execvp(argv0, argv);
356	fatal("execvp");
357}
358
359/* imsg handling */
360/* ARGSUSED */
361static void
362main_dispatch_ldpe(int fd, short event, void *bula)
363{
364	struct imsgev		*iev = bula;
365	struct imsgbuf		*ibuf = &iev->ibuf;
366	struct imsg		 imsg;
367	int			 af;
368	ssize_t			 n;
369	int			 shut = 0, verbose;
370
371	if (event & EV_READ) {
372		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
373			fatal("imsg_read error");
374		if (n == 0)	/* connection closed */
375			shut = 1;
376	}
377	if (event & EV_WRITE) {
378		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
379			fatal("msgbuf_write");
380		if (n == 0)
381			shut = 1;
382	}
383
384	for (;;) {
385		if ((n = imsg_get(ibuf, &imsg)) == -1)
386			fatal("imsg_get");
387
388		if (n == 0)
389			break;
390
391		switch (imsg.hdr.type) {
392		case IMSG_REQUEST_SOCKETS:
393			af = imsg.hdr.pid;
394			main_imsg_send_net_sockets(af);
395			break;
396		case IMSG_CTL_RELOAD:
397			if (ldp_reload() == -1)
398				log_warnx("configuration reload failed");
399			else
400				log_debug("configuration reloaded");
401			break;
402		case IMSG_CTL_FIB_COUPLE:
403			kr_fib_couple();
404			break;
405		case IMSG_CTL_FIB_DECOUPLE:
406			kr_fib_decouple();
407			break;
408		case IMSG_CTL_KROUTE:
409		case IMSG_CTL_KROUTE_ADDR:
410			kr_show_route(&imsg);
411			break;
412		case IMSG_CTL_IFINFO:
413			if (imsg.hdr.len == IMSG_HEADER_SIZE)
414				kr_ifinfo(NULL, imsg.hdr.pid);
415			else if (imsg.hdr.len == IMSG_HEADER_SIZE + IFNAMSIZ)
416				kr_ifinfo(imsg.data, imsg.hdr.pid);
417			else
418				log_warnx("IFINFO request with wrong len");
419			break;
420		case IMSG_CTL_LOG_VERBOSE:
421			/* already checked by ldpe */
422			memcpy(&verbose, imsg.data, sizeof(verbose));
423			log_verbose(verbose);
424			break;
425		default:
426			log_debug("%s: error handling imsg %d", __func__,
427			    imsg.hdr.type);
428			break;
429		}
430		imsg_free(&imsg);
431	}
432	if (!shut)
433		imsg_event_add(iev);
434	else {
435		/* this pipe is dead, so remove the event handler */
436		event_del(&iev->ev);
437		event_loopexit(NULL);
438	}
439}
440
441/* ARGSUSED */
442static void
443main_dispatch_lde(int fd, short event, void *bula)
444{
445	struct imsgev	*iev = bula;
446	struct imsgbuf	*ibuf = &iev->ibuf;
447	struct imsg	 imsg;
448	ssize_t		 n;
449	int		 shut = 0;
450
451	if (event & EV_READ) {
452		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
453			fatal("imsg_read error");
454		if (n == 0)	/* connection closed */
455			shut = 1;
456	}
457	if (event & EV_WRITE) {
458		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
459			fatal("msgbuf_write");
460		if (n == 0)
461			shut = 1;
462	}
463
464	for (;;) {
465		if ((n = imsg_get(ibuf, &imsg)) == -1)
466			fatal("imsg_get");
467
468		if (n == 0)
469			break;
470
471		switch (imsg.hdr.type) {
472		case IMSG_KLABEL_CHANGE:
473			if (imsg.hdr.len - IMSG_HEADER_SIZE !=
474			    sizeof(struct kroute))
475				fatalx("invalid size of IMSG_KLABEL_CHANGE");
476			if (kr_change(imsg.data))
477				log_warnx("%s: error changing route", __func__);
478			break;
479		case IMSG_KLABEL_DELETE:
480			if (imsg.hdr.len - IMSG_HEADER_SIZE !=
481			    sizeof(struct kroute))
482				fatalx("invalid size of IMSG_KLABEL_DELETE");
483			if (kr_delete(imsg.data))
484				log_warnx("%s: error deleting route", __func__);
485			break;
486		case IMSG_KPWLABEL_CHANGE:
487			if (imsg.hdr.len - IMSG_HEADER_SIZE !=
488			    sizeof(struct kpw))
489				fatalx("invalid size of IMSG_KPWLABEL_CHANGE");
490			if (kmpw_set(imsg.data))
491				log_warnx("%s: error changing pseudowire",
492				    __func__);
493			break;
494		case IMSG_KPWLABEL_DELETE:
495			if (imsg.hdr.len - IMSG_HEADER_SIZE !=
496			    sizeof(struct kpw))
497				fatalx("invalid size of IMSG_KPWLABEL_DELETE");
498			if (kmpw_unset(imsg.data))
499				log_warnx("%s: error unsetting pseudowire",
500				    __func__);
501			break;
502		default:
503			log_debug("%s: error handling imsg %d", __func__,
504			    imsg.hdr.type);
505			break;
506		}
507		imsg_free(&imsg);
508	}
509	if (!shut)
510		imsg_event_add(iev);
511	else {
512		/* this pipe is dead, so remove the event handler */
513		event_del(&iev->ev);
514		event_loopexit(NULL);
515	}
516}
517
518void
519main_imsg_compose_ldpe(int type, pid_t pid, void *data, uint16_t datalen)
520{
521	if (iev_ldpe == NULL)
522		return;
523	imsg_compose_event(iev_ldpe, type, 0, pid, -1, data, datalen);
524}
525
526void
527main_imsg_compose_lde(int type, pid_t pid, void *data, uint16_t datalen)
528{
529	imsg_compose_event(iev_lde, type, 0, pid, -1, data, datalen);
530}
531
532static int
533main_imsg_compose_both(enum imsg_type type, void *buf, uint16_t len)
534{
535	if (imsg_compose_event(iev_ldpe, type, 0, 0, -1, buf, len) == -1)
536		return (-1);
537	if (imsg_compose_event(iev_lde, type, 0, 0, -1, buf, len) == -1)
538		return (-1);
539	return (0);
540}
541
542void
543imsg_event_add(struct imsgev *iev)
544{
545	iev->events = EV_READ;
546	if (iev->ibuf.w.queued)
547		iev->events |= EV_WRITE;
548
549	event_del(&iev->ev);
550	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
551	event_add(&iev->ev, NULL);
552}
553
554int
555imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
556    pid_t pid, int fd, void *data, uint16_t datalen)
557{
558	int	ret;
559
560	if ((ret = imsg_compose(&iev->ibuf, type, peerid,
561	    pid, fd, data, datalen)) != -1)
562		imsg_event_add(iev);
563	return (ret);
564}
565
566void
567evbuf_enqueue(struct evbuf *eb, struct ibuf *buf)
568{
569	ibuf_close(&eb->wbuf, buf);
570	evbuf_event_add(eb);
571}
572
573void
574evbuf_event_add(struct evbuf *eb)
575{
576	if (eb->wbuf.queued)
577		event_add(&eb->ev, NULL);
578}
579
580void
581evbuf_init(struct evbuf *eb, int fd, void (*handler)(int, short, void *),
582    void *arg)
583{
584	msgbuf_init(&eb->wbuf);
585	eb->wbuf.fd = fd;
586	event_set(&eb->ev, eb->wbuf.fd, EV_WRITE, handler, arg);
587}
588
589void
590evbuf_clear(struct evbuf *eb)
591{
592	event_del(&eb->ev);
593	msgbuf_clear(&eb->wbuf);
594	eb->wbuf.fd = -1;
595}
596
597static int
598main_imsg_send_ipc_sockets(struct imsgbuf *ldpe_buf, struct imsgbuf *lde_buf)
599{
600	int pipe_ldpe2lde[2];
601
602	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
603	    PF_UNSPEC, pipe_ldpe2lde) == -1)
604		return (-1);
605
606	if (imsg_compose(ldpe_buf, IMSG_SOCKET_IPC, 0, 0, pipe_ldpe2lde[0],
607	    NULL, 0) == -1)
608		return (-1);
609	if (imsg_compose(lde_buf, IMSG_SOCKET_IPC, 0, 0, pipe_ldpe2lde[1],
610	    NULL, 0) == -1)
611		return (-1);
612
613	return (0);
614}
615
616static void
617main_imsg_send_net_sockets(int af)
618{
619	main_imsg_send_net_socket(af, LDP_SOCKET_DISC);
620	main_imsg_send_net_socket(af, LDP_SOCKET_EDISC);
621	main_imsg_send_net_socket(af, LDP_SOCKET_SESSION);
622	imsg_compose_event(iev_ldpe, IMSG_SETUP_SOCKETS, af, 0, -1, NULL, 0);
623}
624
625static void
626main_imsg_send_net_socket(int af, enum socket_type type)
627{
628	int			 fd;
629
630	fd = ldp_create_socket(af, type);
631	if (fd == -1) {
632		log_warnx("%s: failed to create %s socket for address-family "
633		    "%s", __func__, socket_name(type), af_name(af));
634		return;
635	}
636
637	imsg_compose_event(iev_ldpe, IMSG_SOCKET_NET, af, 0, fd, &type,
638	    sizeof(type));
639}
640
641struct ldpd_af_conf *
642ldp_af_conf_get(struct ldpd_conf *xconf, int af)
643{
644	switch (af) {
645	case AF_INET:
646		return (&xconf->ipv4);
647	case AF_INET6:
648		return (&xconf->ipv6);
649	default:
650		fatalx("ldp_af_conf_get: unknown af");
651	}
652}
653
654struct ldpd_af_global *
655ldp_af_global_get(struct ldpd_global *xglobal, int af)
656{
657	switch (af) {
658	case AF_INET:
659		return (&xglobal->ipv4);
660	case AF_INET6:
661		return (&xglobal->ipv6);
662	default:
663		fatalx("ldp_af_global_get: unknown af");
664	}
665}
666
667int
668ldp_is_dual_stack(struct ldpd_conf *xconf)
669{
670	return ((xconf->ipv4.flags & F_LDPD_AF_ENABLED) &&
671	    (xconf->ipv6.flags & F_LDPD_AF_ENABLED));
672}
673
674static int
675main_imsg_send_config(struct ldpd_conf *xconf)
676{
677	struct iface		*iface;
678	struct tnbr		*tnbr;
679	struct nbr_params	*nbrp;
680	struct l2vpn		*l2vpn;
681	struct l2vpn_if		*lif;
682	struct l2vpn_pw		*pw;
683
684	if (main_imsg_compose_both(IMSG_RECONF_CONF, xconf,
685	    sizeof(*xconf)) == -1)
686		return (-1);
687
688	LIST_FOREACH(iface, &xconf->iface_list, entry) {
689		if (main_imsg_compose_both(IMSG_RECONF_IFACE, iface,
690		    sizeof(*iface)) == -1)
691			return (-1);
692	}
693
694	LIST_FOREACH(tnbr, &xconf->tnbr_list, entry) {
695		if (main_imsg_compose_both(IMSG_RECONF_TNBR, tnbr,
696		    sizeof(*tnbr)) == -1)
697			return (-1);
698	}
699
700	LIST_FOREACH(nbrp, &xconf->nbrp_list, entry) {
701		if (main_imsg_compose_both(IMSG_RECONF_NBRP, nbrp,
702		    sizeof(*nbrp)) == -1)
703			return (-1);
704	}
705
706	LIST_FOREACH(l2vpn, &xconf->l2vpn_list, entry) {
707		if (main_imsg_compose_both(IMSG_RECONF_L2VPN, l2vpn,
708		    sizeof(*l2vpn)) == -1)
709			return (-1);
710
711		LIST_FOREACH(lif, &l2vpn->if_list, entry) {
712			if (main_imsg_compose_both(IMSG_RECONF_L2VPN_IF, lif,
713			    sizeof(*lif)) == -1)
714				return (-1);
715		}
716		LIST_FOREACH(pw, &l2vpn->pw_list, entry) {
717			if (main_imsg_compose_both(IMSG_RECONF_L2VPN_PW, pw,
718			    sizeof(*pw)) == -1)
719				return (-1);
720		}
721	}
722
723	if (main_imsg_compose_both(IMSG_RECONF_END, NULL, 0) == -1)
724		return (-1);
725
726	return (0);
727}
728
729static int
730ldp_reload(void)
731{
732	struct ldpd_conf	*xconf;
733
734	if ((xconf = parse_config(conffile)) == NULL)
735		return (-1);
736
737	if (main_imsg_send_config(xconf) == -1)
738		return (-1);
739
740	merge_config(ldpd_conf, xconf);
741
742	return (0);
743}
744
745void
746merge_config(struct ldpd_conf *conf, struct ldpd_conf *xconf)
747{
748	merge_global(conf, xconf);
749	merge_af(AF_INET, &conf->ipv4, &xconf->ipv4);
750	merge_af(AF_INET6, &conf->ipv6, &xconf->ipv6);
751	merge_ifaces(conf, xconf);
752	merge_tnbrs(conf, xconf);
753	merge_nbrps(conf, xconf);
754	merge_l2vpns(conf, xconf);
755	free(xconf);
756}
757
758static void
759merge_global(struct ldpd_conf *conf, struct ldpd_conf *xconf)
760{
761	/* change of router-id requires resetting all neighborships */
762	if (conf->rtr_id.s_addr != xconf->rtr_id.s_addr) {
763		if (ldpd_process == PROC_LDP_ENGINE) {
764			ldpe_reset_nbrs(AF_INET);
765			ldpe_reset_nbrs(AF_INET6);
766			if (conf->rtr_id.s_addr == INADDR_ANY ||
767			    xconf->rtr_id.s_addr == INADDR_ANY) {
768				if_update_all(AF_UNSPEC);
769				tnbr_update_all(AF_UNSPEC);
770			}
771		}
772		conf->rtr_id = xconf->rtr_id;
773	}
774
775	if (conf->trans_pref != xconf->trans_pref) {
776		if (ldpd_process == PROC_LDP_ENGINE)
777			ldpe_reset_ds_nbrs();
778		conf->trans_pref = xconf->trans_pref;
779	}
780
781	if ((conf->flags & F_LDPD_DS_CISCO_INTEROP) !=
782	    (xconf->flags & F_LDPD_DS_CISCO_INTEROP)) {
783		if (ldpd_process == PROC_LDP_ENGINE)
784			ldpe_reset_ds_nbrs();
785	}
786
787	conf->flags = xconf->flags;
788}
789
790static void
791merge_af(int af, struct ldpd_af_conf *af_conf, struct ldpd_af_conf *xa)
792{
793	int			 egress_label_changed = 0;
794	int			 update_sockets = 0;
795
796	if (af_conf->keepalive != xa->keepalive) {
797		af_conf->keepalive = xa->keepalive;
798		if (ldpd_process == PROC_LDP_ENGINE)
799			ldpe_stop_init_backoff(af);
800	}
801	af_conf->thello_holdtime = xa->thello_holdtime;
802	af_conf->thello_interval = xa->thello_interval;
803
804	/* update flags */
805	if (ldpd_process == PROC_LDP_ENGINE &&
806	    (af_conf->flags & F_LDPD_AF_THELLO_ACCEPT) &&
807	    !(xa->flags & F_LDPD_AF_THELLO_ACCEPT))
808		ldpe_remove_dynamic_tnbrs(af);
809
810	if ((af_conf->flags & F_LDPD_AF_NO_GTSM) !=
811	    (xa->flags & F_LDPD_AF_NO_GTSM)) {
812		if (af == AF_INET6)
813			/* need to set/unset IPV6_MINHOPCOUNT */
814			update_sockets = 1;
815		else if (ldpd_process == PROC_LDP_ENGINE)
816			/* for LDPv4 just resetting the neighbors is enough */
817			ldpe_reset_nbrs(af);
818	}
819
820	if ((af_conf->flags & F_LDPD_AF_EXPNULL) !=
821	    (xa->flags & F_LDPD_AF_EXPNULL))
822		egress_label_changed = 1;
823
824	af_conf->flags = xa->flags;
825
826	if (egress_label_changed) {
827		switch (ldpd_process) {
828		case PROC_LDE_ENGINE:
829			lde_change_egress_label(af, af_conf->flags &
830			    F_LDPD_AF_EXPNULL);
831			break;
832		case PROC_MAIN:
833			kr_change_egress_label(af, af_conf->flags &
834			    F_LDPD_AF_EXPNULL);
835			break;
836		default:
837			break;
838		}
839	}
840
841	if (ldp_addrcmp(af, &af_conf->trans_addr, &xa->trans_addr)) {
842		af_conf->trans_addr = xa->trans_addr;
843		update_sockets = 1;
844	}
845
846	if (ldpd_process == PROC_MAIN && update_sockets)
847		imsg_compose_event(iev_ldpe, IMSG_CLOSE_SOCKETS, af, 0, -1,
848		    NULL, 0);
849}
850
851static void
852merge_ifaces(struct ldpd_conf *conf, struct ldpd_conf *xconf)
853{
854	struct iface		*iface, *itmp, *xi;
855
856	LIST_FOREACH_SAFE(iface, &conf->iface_list, entry, itmp) {
857		/* find deleted interfaces */
858		if ((xi = if_lookup(xconf, iface->ifindex)) == NULL) {
859			LIST_REMOVE(iface, entry);
860			if (ldpd_process == PROC_LDP_ENGINE)
861				if_exit(iface);
862			free(iface);
863		}
864	}
865	LIST_FOREACH_SAFE(xi, &xconf->iface_list, entry, itmp) {
866		/* find new interfaces */
867		if ((iface = if_lookup(conf, xi->ifindex)) == NULL) {
868			LIST_REMOVE(xi, entry);
869			LIST_INSERT_HEAD(&conf->iface_list, xi, entry);
870
871			/* resend addresses to activate new interfaces */
872			if (ldpd_process == PROC_MAIN)
873				kif_redistribute(xi->name);
874			continue;
875		}
876
877		/* update existing interfaces */
878		merge_iface_af(&iface->ipv4, &xi->ipv4);
879		merge_iface_af(&iface->ipv6, &xi->ipv6);
880		LIST_REMOVE(xi, entry);
881		free(xi);
882	}
883}
884
885static void
886merge_iface_af(struct iface_af *ia, struct iface_af *xi)
887{
888	if (ia->enabled != xi->enabled) {
889		ia->enabled = xi->enabled;
890		if (ldpd_process == PROC_LDP_ENGINE)
891			if_update(ia->iface, ia->af);
892	}
893	ia->hello_holdtime = xi->hello_holdtime;
894	ia->hello_interval = xi->hello_interval;
895}
896
897static void
898merge_tnbrs(struct ldpd_conf *conf, struct ldpd_conf *xconf)
899{
900	struct tnbr		*tnbr, *ttmp, *xt;
901
902	LIST_FOREACH_SAFE(tnbr, &conf->tnbr_list, entry, ttmp) {
903		if (!(tnbr->flags & F_TNBR_CONFIGURED))
904			continue;
905
906		/* find deleted tnbrs */
907		if ((xt = tnbr_find(xconf, tnbr->af, &tnbr->addr)) == NULL) {
908			if (ldpd_process == PROC_LDP_ENGINE) {
909				tnbr->flags &= ~F_TNBR_CONFIGURED;
910				tnbr_check(tnbr);
911			} else {
912				LIST_REMOVE(tnbr, entry);
913				free(tnbr);
914			}
915		}
916	}
917	LIST_FOREACH_SAFE(xt, &xconf->tnbr_list, entry, ttmp) {
918		/* find new tnbrs */
919		if ((tnbr = tnbr_find(conf, xt->af, &xt->addr)) == NULL) {
920			LIST_REMOVE(xt, entry);
921			LIST_INSERT_HEAD(&conf->tnbr_list, xt, entry);
922
923			if (ldpd_process == PROC_LDP_ENGINE)
924				tnbr_update(xt);
925			continue;
926		}
927
928		/* update existing tnbrs */
929		if (!(tnbr->flags & F_TNBR_CONFIGURED))
930			tnbr->flags |= F_TNBR_CONFIGURED;
931		tnbr->hello_holdtime = xt->hello_holdtime;
932		tnbr->hello_interval = xt->hello_interval;
933		LIST_REMOVE(xt, entry);
934		free(xt);
935	}
936}
937
938static void
939merge_nbrps(struct ldpd_conf *conf, struct ldpd_conf *xconf)
940{
941	struct nbr_params	*nbrp, *ntmp, *xn;
942	struct nbr		*nbr;
943	int			 nbrp_changed;
944
945	LIST_FOREACH_SAFE(nbrp, &conf->nbrp_list, entry, ntmp) {
946		/* find deleted nbrps */
947		if ((xn = nbr_params_find(xconf, nbrp->lsr_id)) == NULL) {
948			if (ldpd_process == PROC_LDP_ENGINE) {
949				nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr);
950				if (nbr) {
951					session_shutdown(nbr, S_SHUTDOWN, 0, 0);
952					pfkey_remove(nbr);
953					if (nbr_session_active_role(nbr))
954						nbr_establish_connection(nbr);
955				}
956			}
957			LIST_REMOVE(nbrp, entry);
958			free(nbrp);
959		}
960	}
961	LIST_FOREACH_SAFE(xn, &xconf->nbrp_list, entry, ntmp) {
962		/* find new nbrps */
963		if ((nbrp = nbr_params_find(conf, xn->lsr_id)) == NULL) {
964			LIST_REMOVE(xn, entry);
965			LIST_INSERT_HEAD(&conf->nbrp_list, xn, entry);
966
967			if (ldpd_process == PROC_LDP_ENGINE) {
968				nbr = nbr_find_ldpid(xn->lsr_id.s_addr);
969				if (nbr) {
970					session_shutdown(nbr, S_SHUTDOWN, 0, 0);
971					if (pfkey_establish(nbr, xn) == -1)
972						fatalx("pfkey setup failed");
973					if (nbr_session_active_role(nbr))
974						nbr_establish_connection(nbr);
975				}
976			}
977			continue;
978		}
979
980		/* update existing nbrps */
981		if (nbrp->flags != xn->flags ||
982		    nbrp->keepalive != xn->keepalive ||
983		    nbrp->gtsm_enabled != xn->gtsm_enabled ||
984		    nbrp->gtsm_hops != xn->gtsm_hops ||
985		    nbrp->auth.method != xn->auth.method ||
986		    strcmp(nbrp->auth.md5key, xn->auth.md5key) != 0)
987			nbrp_changed = 1;
988		else
989			nbrp_changed = 0;
990
991		nbrp->keepalive = xn->keepalive;
992		nbrp->gtsm_enabled = xn->gtsm_enabled;
993		nbrp->gtsm_hops = xn->gtsm_hops;
994		nbrp->auth.method = xn->auth.method;
995		strlcpy(nbrp->auth.md5key, xn->auth.md5key,
996		    sizeof(nbrp->auth.md5key));
997		nbrp->auth.md5key_len = xn->auth.md5key_len;
998		nbrp->flags = xn->flags;
999
1000		if (ldpd_process == PROC_LDP_ENGINE) {
1001			nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr);
1002			if (nbr && nbrp_changed) {
1003				session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1004				pfkey_remove(nbr);
1005				if (pfkey_establish(nbr, nbrp) == -1)
1006					fatalx("pfkey setup failed");
1007				if (nbr_session_active_role(nbr))
1008					nbr_establish_connection(nbr);
1009			}
1010		}
1011		LIST_REMOVE(xn, entry);
1012		free(xn);
1013	}
1014}
1015
1016static void
1017merge_l2vpns(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1018{
1019	struct l2vpn		*l2vpn, *ltmp, *xl;
1020
1021	LIST_FOREACH_SAFE(l2vpn, &conf->l2vpn_list, entry, ltmp) {
1022		/* find deleted l2vpns */
1023		if ((xl = l2vpn_find(xconf, l2vpn->name)) == NULL) {
1024			LIST_REMOVE(l2vpn, entry);
1025
1026			switch (ldpd_process) {
1027			case PROC_LDE_ENGINE:
1028				l2vpn_exit(l2vpn);
1029				break;
1030			case PROC_LDP_ENGINE:
1031				ldpe_l2vpn_exit(l2vpn);
1032				break;
1033			case PROC_MAIN:
1034				break;
1035			}
1036			l2vpn_del(l2vpn);
1037		}
1038	}
1039	LIST_FOREACH_SAFE(xl, &xconf->l2vpn_list, entry, ltmp) {
1040		/* find new l2vpns */
1041		if ((l2vpn = l2vpn_find(conf, xl->name)) == NULL) {
1042			LIST_REMOVE(xl, entry);
1043			LIST_INSERT_HEAD(&conf->l2vpn_list, xl, entry);
1044
1045			switch (ldpd_process) {
1046			case PROC_LDE_ENGINE:
1047				l2vpn_init(xl);
1048				break;
1049			case PROC_LDP_ENGINE:
1050				ldpe_l2vpn_init(xl);
1051				break;
1052			case PROC_MAIN:
1053				break;
1054			}
1055			continue;
1056		}
1057
1058		/* update existing l2vpns */
1059		merge_l2vpn(conf, l2vpn, xl);
1060		LIST_REMOVE(xl, entry);
1061		free(xl);
1062	}
1063}
1064
1065static void
1066merge_l2vpn(struct ldpd_conf *xconf, struct l2vpn *l2vpn, struct l2vpn *xl)
1067{
1068	struct l2vpn_if		*lif, *ftmp, *xf;
1069	struct l2vpn_pw		*pw, *ptmp, *xp;
1070	struct nbr		*nbr;
1071	int			 reset_nbr, reinstall_pwfec, reinstall_tnbr;
1072	int			 previous_pw_type, previous_mtu;
1073
1074	previous_pw_type = l2vpn->pw_type;
1075	previous_mtu = l2vpn->mtu;
1076
1077	/* merge intefaces */
1078	LIST_FOREACH_SAFE(lif, &l2vpn->if_list, entry, ftmp) {
1079		/* find deleted interfaces */
1080		if ((xf = l2vpn_if_find(xl, lif->ifindex)) == NULL) {
1081			LIST_REMOVE(lif, entry);
1082			free(lif);
1083		}
1084	}
1085	LIST_FOREACH_SAFE(xf, &xl->if_list, entry, ftmp) {
1086		/* find new interfaces */
1087		if ((lif = l2vpn_if_find(l2vpn, xf->ifindex)) == NULL) {
1088			LIST_REMOVE(xf, entry);
1089			LIST_INSERT_HEAD(&l2vpn->if_list, xf, entry);
1090			xf->l2vpn = l2vpn;
1091			continue;
1092		}
1093
1094		LIST_REMOVE(xf, entry);
1095		free(xf);
1096	}
1097
1098	/* merge pseudowires */
1099	LIST_FOREACH_SAFE(pw, &l2vpn->pw_list, entry, ptmp) {
1100		/* find deleted pseudowires */
1101		if ((xp = l2vpn_pw_find(xl, pw->ifindex)) == NULL) {
1102			switch (ldpd_process) {
1103			case PROC_LDE_ENGINE:
1104				l2vpn_pw_exit(pw);
1105				break;
1106			case PROC_LDP_ENGINE:
1107				ldpe_l2vpn_pw_exit(pw);
1108				break;
1109			case PROC_MAIN:
1110				break;
1111			}
1112
1113			LIST_REMOVE(pw, entry);
1114			free(pw);
1115		}
1116	}
1117	LIST_FOREACH_SAFE(xp, &xl->pw_list, entry, ptmp) {
1118		/* find new pseudowires */
1119		if ((pw = l2vpn_pw_find(l2vpn, xp->ifindex)) == NULL) {
1120			LIST_REMOVE(xp, entry);
1121			LIST_INSERT_HEAD(&l2vpn->pw_list, xp, entry);
1122			xp->l2vpn = l2vpn;
1123
1124			switch (ldpd_process) {
1125			case PROC_LDE_ENGINE:
1126				l2vpn_pw_init(xp);
1127				break;
1128			case PROC_LDP_ENGINE:
1129				ldpe_l2vpn_pw_init(xp);
1130				break;
1131			case PROC_MAIN:
1132				break;
1133			}
1134			continue;
1135		}
1136
1137		/* update existing pseudowire */
1138    		if (pw->af != xp->af ||
1139		    ldp_addrcmp(pw->af, &pw->addr, &xp->addr))
1140			reinstall_tnbr = 1;
1141		else
1142			reinstall_tnbr = 0;
1143
1144		/* changes that require a session restart */
1145		if ((pw->flags & (F_PW_STATUSTLV_CONF|F_PW_CWORD_CONF)) !=
1146		    (xp->flags & (F_PW_STATUSTLV_CONF|F_PW_CWORD_CONF)))
1147			reset_nbr = 1;
1148		else
1149			reset_nbr = 0;
1150
1151		if (l2vpn->pw_type != xl->pw_type || l2vpn->mtu != xl->mtu ||
1152		    pw->pwid != xp->pwid || reinstall_tnbr || reset_nbr ||
1153		    pw->lsr_id.s_addr != xp->lsr_id.s_addr)
1154			reinstall_pwfec = 1;
1155		else
1156			reinstall_pwfec = 0;
1157
1158		if (ldpd_process == PROC_LDP_ENGINE) {
1159			if (reinstall_tnbr)
1160				ldpe_l2vpn_pw_exit(pw);
1161			if (reset_nbr) {
1162				nbr = nbr_find_ldpid(pw->lsr_id.s_addr);
1163				if (nbr && nbr->state == NBR_STA_OPER)
1164					session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1165			}
1166		}
1167		if (ldpd_process == PROC_LDE_ENGINE &&
1168		    !reset_nbr && reinstall_pwfec)
1169			l2vpn_pw_exit(pw);
1170		pw->lsr_id = xp->lsr_id;
1171		pw->af = xp->af;
1172		pw->addr = xp->addr;
1173		pw->pwid = xp->pwid;
1174		strlcpy(pw->ifname, xp->ifname, sizeof(pw->ifname));
1175		pw->ifindex = xp->ifindex;
1176		if (xp->flags & F_PW_CWORD_CONF)
1177			pw->flags |= F_PW_CWORD_CONF;
1178		else
1179			pw->flags &= ~F_PW_CWORD_CONF;
1180		if (xp->flags & F_PW_STATUSTLV_CONF)
1181			pw->flags |= F_PW_STATUSTLV_CONF;
1182		else
1183			pw->flags &= ~F_PW_STATUSTLV_CONF;
1184		if (ldpd_process == PROC_LDP_ENGINE && reinstall_tnbr)
1185			ldpe_l2vpn_pw_init(pw);
1186		if (ldpd_process == PROC_LDE_ENGINE &&
1187		    !reset_nbr && reinstall_pwfec) {
1188			l2vpn->pw_type = xl->pw_type;
1189			l2vpn->mtu = xl->mtu;
1190			l2vpn_pw_init(pw);
1191			l2vpn->pw_type = previous_pw_type;
1192			l2vpn->mtu = previous_mtu;
1193		}
1194
1195		LIST_REMOVE(xp, entry);
1196		free(xp);
1197	}
1198
1199	l2vpn->pw_type = xl->pw_type;
1200	l2vpn->mtu = xl->mtu;
1201	strlcpy(l2vpn->br_ifname, xl->br_ifname, sizeof(l2vpn->br_ifname));
1202	l2vpn->br_ifindex = xl->br_ifindex;
1203}
1204
1205struct ldpd_conf *
1206config_new_empty(void)
1207{
1208	struct ldpd_conf	*xconf;
1209
1210	xconf = calloc(1, sizeof(*xconf));
1211	if (xconf == NULL)
1212		fatal(NULL);
1213
1214	LIST_INIT(&xconf->iface_list);
1215	LIST_INIT(&xconf->tnbr_list);
1216	LIST_INIT(&xconf->nbrp_list);
1217	LIST_INIT(&xconf->l2vpn_list);
1218
1219	return (xconf);
1220}
1221
1222void
1223config_clear(struct ldpd_conf *conf)
1224{
1225	struct ldpd_conf	*xconf;
1226
1227	/*
1228	 * Merge current config with an empty config, this will deactivate
1229	 * and deallocate all the interfaces, pseudowires and so on. Before
1230	 * merging, copy the router-id and other variables to avoid some
1231	 * unnecessary operations, like trying to reset the neighborships.
1232	 */
1233	xconf = config_new_empty();
1234	xconf->ipv4 = conf->ipv4;
1235	xconf->ipv6 = conf->ipv6;
1236	xconf->rtr_id = conf->rtr_id;
1237	xconf->trans_pref = conf->trans_pref;
1238	xconf->flags = conf->flags;
1239	merge_config(conf, xconf);
1240	free(conf);
1241}
1242