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