1 /*	$OpenBSD$ */
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 <zebra.h>
23 #include <sys/wait.h>
24 
25 #include "ldpd.h"
26 #include "ldpe.h"
27 #include "lde.h"
28 #include "log.h"
29 #include "ldp_vty.h"
30 #include "ldp_debug.h"
31 
32 #include <lib/version.h>
33 #include <lib/log.h>
34 #include "getopt.h"
35 #include "vty.h"
36 #include "command.h"
37 #include "memory.h"
38 #include "privs.h"
39 #include "sigevent.h"
40 #include "zclient.h"
41 #include "vrf.h"
42 #include "filter.h"
43 #include "qobj.h"
44 #include "libfrr.h"
45 #include "lib_errors.h"
46 
47 static void		 ldpd_shutdown(void);
48 static pid_t		 start_child(enum ldpd_process, char *, int, int);
49 static int		 main_dispatch_ldpe(struct thread *);
50 static int		 main_dispatch_lde(struct thread *);
51 static int		 main_imsg_send_ipc_sockets(struct imsgbuf *,
52 			    struct imsgbuf *);
53 static void		 main_imsg_send_net_sockets(int);
54 static void		 main_imsg_send_net_socket(int, enum socket_type);
55 static int		 main_imsg_send_config(struct ldpd_conf *);
56 static void		 ldp_config_normalize(struct ldpd_conf *);
57 static void		 ldp_config_reset(struct ldpd_conf *);
58 static void		 ldp_config_reset_main(struct ldpd_conf *);
59 static void		 ldp_config_reset_af(struct ldpd_conf *, int);
60 static void		 ldp_config_reset_l2vpns(struct ldpd_conf *);
61 static void		 merge_global(struct ldpd_conf *, struct ldpd_conf *);
62 static void		 merge_af(int, struct ldpd_af_conf *,
63 			    struct ldpd_af_conf *);
64 static void		 merge_ifaces(struct ldpd_conf *, struct ldpd_conf *);
65 static void		 merge_iface_af(struct iface_af *, struct iface_af *);
66 static void		 merge_tnbrs(struct ldpd_conf *, struct ldpd_conf *);
67 static void		 merge_nbrps(struct ldpd_conf *, struct ldpd_conf *);
68 static void		 merge_l2vpns(struct ldpd_conf *, struct ldpd_conf *);
69 static void		 merge_l2vpn(struct ldpd_conf *, struct l2vpn *,
70 			    struct l2vpn *);
71 
72 DEFINE_QOBJ_TYPE(iface)
73 DEFINE_QOBJ_TYPE(tnbr)
74 DEFINE_QOBJ_TYPE(nbr_params)
75 DEFINE_QOBJ_TYPE(l2vpn_if)
76 DEFINE_QOBJ_TYPE(l2vpn_pw)
77 DEFINE_QOBJ_TYPE(l2vpn)
78 DEFINE_QOBJ_TYPE(ldpd_conf)
79 
80 struct ldpd_global	 global;
81 struct ldpd_init	 init;
82 struct ldpd_conf	*ldpd_conf, *vty_conf;
83 
84 static struct imsgev	*iev_ldpe, *iev_ldpe_sync;
85 static struct imsgev	*iev_lde, *iev_lde_sync;
86 static pid_t		 ldpe_pid;
87 static pid_t		 lde_pid;
88 
89 enum ldpd_process ldpd_process;
90 
91 #define LDP_DEFAULT_CONFIG	"ldpd.conf"
92 #define LDP_VTY_PORT		2612
93 
94 /* Master of threads. */
95 struct thread_master *master;
96 
97 static struct frr_daemon_info ldpd_di;
98 
99 /* ldpd privileges */
100 static zebra_capabilities_t _caps_p [] =
101 {
102 	ZCAP_BIND,
103 	ZCAP_NET_ADMIN
104 };
105 
106 struct zebra_privs_t ldpd_privs =
107 {
108 #if defined(FRR_USER) && defined(FRR_GROUP)
109 	.user = FRR_USER,
110 	.group = FRR_GROUP,
111 #endif
112 #if defined(VTY_GROUP)
113 	.vty_group = VTY_GROUP,
114 #endif
115 	.caps_p = _caps_p,
116 	.cap_num_p = array_size(_caps_p),
117 	.cap_num_i = 0
118 };
119 
120 /* CTL Socket path */
121 char ctl_sock_path[MAXPATHLEN];
122 
123 /* LDPd options. */
124 #define OPTION_CTLSOCK 1001
125 static const struct option longopts[] =
126 {
127 	{ "ctl_socket",  required_argument, NULL, OPTION_CTLSOCK},
128 	{ "instance",    required_argument, NULL, 'n'},
129 	{ 0 }
130 };
131 
132 /* SIGHUP handler. */
133 static void
sighup(void)134 sighup(void)
135 {
136 	log_info("SIGHUP received");
137 
138 	/*
139 	 * Do a full configuration reload. In other words, reset vty_conf
140 	 * and build a new configuartion from scratch.
141 	 */
142 	ldp_config_reset(vty_conf);
143 	vty_read_config(NULL, ldpd_di.config_file, config_default);
144 	ldp_config_apply(NULL, vty_conf);
145 }
146 
147 /* SIGINT / SIGTERM handler. */
148 static void
sigint(void)149 sigint(void)
150 {
151 	log_info("SIGINT received");
152 	ldpd_shutdown();
153 }
154 
155 /* SIGUSR1 handler. */
156 static void
sigusr1(void)157 sigusr1(void)
158 {
159 	zlog_rotate();
160 }
161 
162 static struct quagga_signal_t ldp_signals[] =
163 {
164 	{
165 		.signal = SIGHUP,
166 		.handler = &sighup,
167 	},
168 	{
169 		.signal = SIGINT,
170 		.handler = &sigint,
171 	},
172 	{
173 		.signal = SIGTERM,
174 		.handler = &sigint,
175 	},
176 	{
177 		.signal = SIGUSR1,
178 		.handler = &sigusr1,
179 	}
180 };
181 
182 static const struct frr_yang_module_info *const ldpd_yang_modules[] = {
183 	&frr_filter_info,
184 	&frr_vrf_info,
185 };
186 
187 FRR_DAEMON_INFO(ldpd, LDP,
188 	.vty_port = LDP_VTY_PORT,
189 
190 	.proghelp = "Implementation of the LDP protocol.",
191 
192 	.signals = ldp_signals,
193 	.n_signals = array_size(ldp_signals),
194 
195 	.privs = &ldpd_privs,
196 
197 	.yang_modules = ldpd_yang_modules,
198 	.n_yang_modules = array_size(ldpd_yang_modules),
199 )
200 
ldp_config_fork_apply(struct thread * t)201 static int ldp_config_fork_apply(struct thread *t)
202 {
203 	/*
204 	 * So the frr_config_fork() function schedules
205 	 * the read of the vty config( if there is a
206 	 * non-integrated config ) to be after the
207 	 * end of startup and we are starting the
208 	 * main process loop.  We need to schedule
209 	 * the application of this if necessary
210 	 * after the read in of the config.
211 	 */
212 	ldp_config_apply(NULL, vty_conf);
213 
214 	return 0;
215 }
216 
217 int
main(int argc,char * argv[])218 main(int argc, char *argv[])
219 {
220 	char			*saved_argv0;
221 	int			 lflag = 0, eflag = 0;
222 	int			 pipe_parent2ldpe[2], pipe_parent2ldpe_sync[2];
223 	int			 pipe_parent2lde[2], pipe_parent2lde_sync[2];
224 	char			*ctl_sock_name;
225 	struct thread           *thread = NULL;
226 	bool                    ctl_sock_used = false;
227 
228 	snprintf(ctl_sock_path, sizeof(ctl_sock_path), LDPD_SOCKET,
229 		 "", "");
230 
231 	ldpd_process = PROC_MAIN;
232 	log_procname = log_procnames[ldpd_process];
233 
234 	saved_argv0 = argv[0];
235 	if (saved_argv0 == NULL)
236 		saved_argv0 = (char *)"ldpd";
237 
238 	frr_preinit(&ldpd_di, argc, argv);
239 	frr_opt_add("LEn:", longopts,
240 		"      --ctl_socket   Override ctl socket path\n"
241 		"  -n, --instance     Instance id\n");
242 
243 	while (1) {
244 		int opt;
245 
246 		opt = frr_getopt(argc, argv, NULL);
247 
248 		if (opt == EOF)
249 			break;
250 
251 		switch (opt) {
252 		case 0:
253 			break;
254 		case OPTION_CTLSOCK:
255 			ctl_sock_used = true;
256 			ctl_sock_name = strrchr(LDPD_SOCKET, '/');
257 			if (ctl_sock_name)
258 				/* skip '/' */
259 				ctl_sock_name++;
260 			else
261 				/*
262 				 * LDPD_SOCKET configured as relative path
263 				 * during config? Should really never happen for
264 				 * sensible config
265 				 */
266 				ctl_sock_name = (char *)LDPD_SOCKET;
267 			strlcpy(ctl_sock_path, optarg, sizeof(ctl_sock_path));
268 			strlcat(ctl_sock_path, "/", sizeof(ctl_sock_path));
269 			strlcat(ctl_sock_path, ctl_sock_name,
270 			    sizeof(ctl_sock_path));
271 			break;
272 		case 'n':
273 			init.instance = atoi(optarg);
274 			if (init.instance < 1)
275 				exit(0);
276 			break;
277 		case 'L':
278 			lflag = 1;
279 			break;
280 		case 'E':
281 			eflag = 1;
282 			break;
283 		default:
284 			frr_help_exit(1);
285 			break;
286 		}
287 	}
288 
289 	if (ldpd_di.pathspace && !ctl_sock_used)
290 		snprintf(ctl_sock_path, sizeof(ctl_sock_path), LDPD_SOCKET,
291 			 "/", ldpd_di.pathspace);
292 
293 	strlcpy(init.user, ldpd_privs.user, sizeof(init.user));
294 	strlcpy(init.group, ldpd_privs.group, sizeof(init.group));
295 	strlcpy(init.ctl_sock_path, ctl_sock_path, sizeof(init.ctl_sock_path));
296 	strlcpy(init.zclient_serv_path, frr_zclientpath,
297 	    sizeof(init.zclient_serv_path));
298 
299 	argc -= optind;
300 	if (argc > 0 || (lflag && eflag))
301 		frr_help_exit(1);
302 
303 	/* check for root privileges  */
304 	if (geteuid() != 0) {
305 		errno = EPERM;
306 		perror(ldpd_di.progname);
307 		exit(1);
308 	}
309 
310 	if (lflag || eflag) {
311 		struct zprivs_ids_t ids;
312 
313 		zprivs_preinit(&ldpd_privs);
314 		zprivs_get_ids(&ids);
315 
316 		zlog_init(ldpd_di.progname, "LDP", 0,
317 			  ids.uid_normal, ids.gid_normal);
318 	}
319 	if (lflag)
320 		lde();
321 	else if (eflag)
322 		ldpe();
323 
324 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_parent2ldpe) == -1)
325 		fatal("socketpair");
326 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC,
327 	    pipe_parent2ldpe_sync) == -1)
328 		fatal("socketpair");
329 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_parent2lde) == -1)
330 		fatal("socketpair");
331 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC,
332 	    pipe_parent2lde_sync) == -1)
333 		fatal("socketpair");
334 	sock_set_nonblock(pipe_parent2ldpe[0]);
335 	sock_set_cloexec(pipe_parent2ldpe[0]);
336 	sock_set_nonblock(pipe_parent2ldpe[1]);
337 	sock_set_cloexec(pipe_parent2ldpe[1]);
338 	sock_set_nonblock(pipe_parent2ldpe_sync[0]);
339 	sock_set_cloexec(pipe_parent2ldpe_sync[0]);
340 	sock_set_cloexec(pipe_parent2ldpe_sync[1]);
341 	sock_set_nonblock(pipe_parent2lde[0]);
342 	sock_set_cloexec(pipe_parent2lde[0]);
343 	sock_set_nonblock(pipe_parent2lde[1]);
344 	sock_set_cloexec(pipe_parent2lde[1]);
345 	sock_set_nonblock(pipe_parent2lde_sync[0]);
346 	sock_set_cloexec(pipe_parent2lde_sync[0]);
347 	sock_set_cloexec(pipe_parent2lde_sync[1]);
348 
349 	/* start children */
350 	lde_pid = start_child(PROC_LDE_ENGINE, saved_argv0,
351 	    pipe_parent2lde[1], pipe_parent2lde_sync[1]);
352 	ldpe_pid = start_child(PROC_LDP_ENGINE, saved_argv0,
353 	    pipe_parent2ldpe[1], pipe_parent2ldpe_sync[1]);
354 
355 	master = frr_init();
356 
357 	vrf_init(NULL, NULL, NULL, NULL, NULL);
358 	access_list_init();
359 	ldp_vty_init();
360 	ldp_zebra_init(master);
361 
362 	/*
363 	 * Create base configuration with sane defaults. All configuration
364 	 * requests (e.g. CLI) act on vty_conf and then call ldp_config_apply()
365 	 * to merge the changes into ldpd_conf, which contains the actual
366 	 * running configuration.
367 	 */
368 	ldpd_conf = config_new_empty();
369 	vty_conf = config_new_empty();
370 	QOBJ_REG(vty_conf, ldpd_conf);
371 
372 	/* read configuration file and daemonize  */
373 	frr_config_fork();
374 
375 	/* apply configuration */
376 	thread_add_event(master, ldp_config_fork_apply, NULL, 0, &thread);
377 
378 	/* setup pipes to children */
379 	if ((iev_ldpe = calloc(1, sizeof(struct imsgev))) == NULL ||
380 	    (iev_ldpe_sync = calloc(1, sizeof(struct imsgev))) == NULL ||
381 	    (iev_lde = calloc(1, sizeof(struct imsgev))) == NULL ||
382 	    (iev_lde_sync = calloc(1, sizeof(struct imsgev))) == NULL)
383 		fatal(NULL);
384 	imsg_init(&iev_ldpe->ibuf, pipe_parent2ldpe[0]);
385 	iev_ldpe->handler_read = main_dispatch_ldpe;
386 	iev_ldpe->ev_read = NULL;
387 	thread_add_read(master, iev_ldpe->handler_read, iev_ldpe, iev_ldpe->ibuf.fd,
388 			&iev_ldpe->ev_read);
389 	iev_ldpe->handler_write = ldp_write_handler;
390 
391 	imsg_init(&iev_ldpe_sync->ibuf, pipe_parent2ldpe_sync[0]);
392 	iev_ldpe_sync->handler_read = main_dispatch_ldpe;
393 	iev_ldpe_sync->ev_read = NULL;
394 	thread_add_read(master, iev_ldpe_sync->handler_read, iev_ldpe_sync, iev_ldpe_sync->ibuf.fd,
395 			&iev_ldpe_sync->ev_read);
396 	iev_ldpe_sync->handler_write = ldp_write_handler;
397 
398 	imsg_init(&iev_lde->ibuf, pipe_parent2lde[0]);
399 	iev_lde->handler_read = main_dispatch_lde;
400 	iev_lde->ev_read = NULL;
401 	thread_add_read(master, iev_lde->handler_read, iev_lde, iev_lde->ibuf.fd,
402 			&iev_lde->ev_read);
403 	iev_lde->handler_write = ldp_write_handler;
404 
405 	imsg_init(&iev_lde_sync->ibuf, pipe_parent2lde_sync[0]);
406 	iev_lde_sync->handler_read = main_dispatch_lde;
407 	iev_lde_sync->ev_read = NULL;
408 	thread_add_read(master, iev_lde_sync->handler_read, iev_lde_sync, iev_lde_sync->ibuf.fd,
409 			&iev_lde_sync->ev_read);
410 	iev_lde_sync->handler_write = ldp_write_handler;
411 
412 	if (main_imsg_send_ipc_sockets(&iev_ldpe->ibuf, &iev_lde->ibuf))
413 		fatal("could not establish imsg links");
414 	main_imsg_compose_both(IMSG_DEBUG_UPDATE, &ldp_debug,
415 	    sizeof(ldp_debug));
416 	main_imsg_compose_both(IMSG_INIT, &init, sizeof(init));
417 	main_imsg_send_config(ldpd_conf);
418 
419 	if (ldpd_conf->ipv4.flags & F_LDPD_AF_ENABLED)
420 		main_imsg_send_net_sockets(AF_INET);
421 	if (ldpd_conf->ipv6.flags & F_LDPD_AF_ENABLED)
422 		main_imsg_send_net_sockets(AF_INET6);
423 
424 	frr_run(master);
425 
426 	/* NOTREACHED */
427 	return (0);
428 }
429 
430 static void
ldpd_shutdown(void)431 ldpd_shutdown(void)
432 {
433 	pid_t		 pid;
434 	int		 status;
435 
436 	frr_early_fini();
437 
438 	/* close pipes */
439 	msgbuf_clear(&iev_ldpe->ibuf.w);
440 	close(iev_ldpe->ibuf.fd);
441 	msgbuf_clear(&iev_lde->ibuf.w);
442 	close(iev_lde->ibuf.fd);
443 
444 	config_clear(ldpd_conf);
445 
446 	ldp_config_reset(vty_conf);
447 	QOBJ_UNREG(vty_conf);
448 	free(vty_conf);
449 
450 	log_debug("waiting for children to terminate");
451 
452 	while (true) {
453 		/* Wait for child process. */
454 		pid = wait(&status);
455 		if (pid == -1) {
456 			/* We got interrupted, try again. */
457 			if (errno == EINTR)
458 				continue;
459 			/* No more processes were found. */
460 			if (errno == ECHILD)
461 				break;
462 
463 			/* Unhandled errno condition. */
464 			fatal("wait");
465 			/* UNREACHABLE */
466 		}
467 
468 		/* We found something, lets announce it. */
469 		if (WIFSIGNALED(status))
470 			log_warnx("%s terminated; signal %d",
471 				  (pid == lde_pid ? "label decision engine"
472 						  : "ldp engine"),
473 				  WTERMSIG(status));
474 
475 		/* Repeat until there are no more child processes. */
476 	}
477 
478 	free(iev_ldpe);
479 	free(iev_lde);
480 
481 	log_info("terminating");
482 
483 	vrf_terminate();
484 	access_list_reset();
485 	ldp_zebra_destroy();
486 
487 	frr_fini();
488 	exit(0);
489 }
490 
491 static pid_t
start_child(enum ldpd_process p,char * argv0,int fd_async,int fd_sync)492 start_child(enum ldpd_process p, char *argv0, int fd_async, int fd_sync)
493 {
494 	char	*argv[7];
495 	int	 argc = 0, nullfd;
496 	pid_t	 pid;
497 
498 	pid = fork();
499 	switch (pid) {
500 	case -1:
501 		fatal("cannot fork");
502 	case 0:
503 		break;
504 	default:
505 		close(fd_async);
506 		close(fd_sync);
507 		return (pid);
508 	}
509 
510 	nullfd = open("/dev/null", O_RDONLY | O_NOCTTY);
511 	if (nullfd == -1) {
512 		flog_err_sys(EC_LIB_SYSTEM_CALL,
513 			     "%s: failed to open /dev/null: %s", __func__,
514 			     safe_strerror(errno));
515 	} else {
516 		dup2(nullfd, 0);
517 		dup2(nullfd, 1);
518 		dup2(nullfd, 2);
519 		close(nullfd);
520 	}
521 
522 	if (dup2(fd_async, LDPD_FD_ASYNC) == -1)
523 		fatal("cannot setup imsg async fd");
524 	if (dup2(fd_sync, LDPD_FD_SYNC) == -1)
525 		fatal("cannot setup imsg sync fd");
526 
527 	argv[argc++] = argv0;
528 	switch (p) {
529 	case PROC_MAIN:
530 		fatalx("Can not start main process");
531 	case PROC_LDE_ENGINE:
532 		argv[argc++] = (char *)"-L";
533 		break;
534 	case PROC_LDP_ENGINE:
535 		argv[argc++] = (char *)"-E";
536 		break;
537 	}
538 
539 	argv[argc++] = (char *)"-u";
540 	argv[argc++] = (char *)ldpd_privs.user;
541 	argv[argc++] = (char *)"-g";
542 	argv[argc++] = (char *)ldpd_privs.group;
543 	argv[argc++] = NULL;
544 
545 	execvp(argv0, argv);
546 	fatal("execvp");
547 }
548 
549 /* imsg handling */
550 /* ARGSUSED */
551 static int
main_dispatch_ldpe(struct thread * thread)552 main_dispatch_ldpe(struct thread *thread)
553 {
554 	struct imsgev		*iev = THREAD_ARG(thread);
555 	struct imsgbuf		*ibuf = &iev->ibuf;
556 	struct imsg		 imsg;
557 	int			 af;
558 	ssize_t			 n;
559 	int			 shut = 0;
560 
561 	iev->ev_read = NULL;
562 
563 	if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
564 		fatal("imsg_read error");
565 	if (n == 0)	/* connection closed */
566 		shut = 1;
567 
568 	for (;;) {
569 		if ((n = imsg_get(ibuf, &imsg)) == -1)
570 			fatal("imsg_get");
571 
572 		if (n == 0)
573 			break;
574 
575 		switch (imsg.hdr.type) {
576 		case IMSG_LOG:
577 			logit(imsg.hdr.pid, "%s", (const char *)imsg.data);
578 			break;
579 		case IMSG_REQUEST_SOCKETS:
580 			af = imsg.hdr.pid;
581 			main_imsg_send_net_sockets(af);
582 			break;
583 		case IMSG_ACL_CHECK:
584 			if (imsg.hdr.len != IMSG_HEADER_SIZE +
585 			    sizeof(struct acl_check))
586 				fatalx("IMSG_ACL_CHECK imsg with wrong len");
587 			ldp_acl_reply(iev, (struct acl_check *)imsg.data);
588 			break;
589 		default:
590 			log_debug("%s: error handling imsg %d", __func__,
591 			    imsg.hdr.type);
592 			break;
593 		}
594 		imsg_free(&imsg);
595 	}
596 	if (!shut)
597 		imsg_event_add(iev);
598 	else {
599 		/* this pipe is dead, so remove the event handlers and exit */
600 		THREAD_READ_OFF(iev->ev_read);
601 		THREAD_WRITE_OFF(iev->ev_write);
602 		ldpe_pid = 0;
603 		if (lde_pid == 0)
604 			ldpd_shutdown();
605 		else
606 			kill(lde_pid, SIGTERM);
607 	}
608 
609 	return (0);
610 }
611 
612 /* ARGSUSED */
613 static int
main_dispatch_lde(struct thread * thread)614 main_dispatch_lde(struct thread *thread)
615 {
616 	struct imsgev	*iev = THREAD_ARG(thread);
617 	struct imsgbuf	*ibuf = &iev->ibuf;
618 	struct imsg	 imsg;
619 	ssize_t		 n;
620 	int		 shut = 0;
621 
622 	iev->ev_read = NULL;
623 
624 	if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
625 		fatal("imsg_read error");
626 	if (n == 0)	/* connection closed */
627 		shut = 1;
628 
629 	for (;;) {
630 		if ((n = imsg_get(ibuf, &imsg)) == -1)
631 			fatal("imsg_get");
632 
633 		if (n == 0)
634 			break;
635 
636 		switch (imsg.hdr.type) {
637 		case IMSG_LOG:
638 			logit(imsg.hdr.pid, "%s", (const char *)imsg.data);
639 			break;
640 		case IMSG_KLABEL_CHANGE:
641 			if (imsg.hdr.len - IMSG_HEADER_SIZE !=
642 			    sizeof(struct kroute))
643 				fatalx("invalid size of IMSG_KLABEL_CHANGE");
644 			if (kr_change(imsg.data))
645 				log_warnx("%s: error changing route", __func__);
646 			break;
647 		case IMSG_KLABEL_DELETE:
648 			if (imsg.hdr.len - IMSG_HEADER_SIZE !=
649 			    sizeof(struct kroute))
650 				fatalx("invalid size of IMSG_KLABEL_DELETE");
651 			if (kr_delete(imsg.data))
652 				log_warnx("%s: error deleting route", __func__);
653 			break;
654 		case IMSG_KPW_ADD:
655 		case IMSG_KPW_DELETE:
656 		case IMSG_KPW_SET:
657 		case IMSG_KPW_UNSET:
658 			if (imsg.hdr.len - IMSG_HEADER_SIZE !=
659 			    sizeof(struct zapi_pw))
660 				fatalx("invalid size of IMSG_KPWLABEL_CHANGE");
661 
662 			switch (imsg.hdr.type) {
663 			case IMSG_KPW_ADD:
664 				if (kmpw_add(imsg.data))
665 					log_warnx("%s: error adding pseudowire", __func__);
666 				break;
667 			case IMSG_KPW_DELETE:
668 				if (kmpw_del(imsg.data))
669 					log_warnx("%s: error deleting pseudowire", __func__);
670 				break;
671 			case IMSG_KPW_SET:
672 				if (kmpw_set(imsg.data))
673 					log_warnx("%s: error setting pseudowire", __func__);
674 				break;
675 			case IMSG_KPW_UNSET:
676 				if (kmpw_unset(imsg.data))
677 					log_warnx("%s: error unsetting pseudowire", __func__);
678 				break;
679 			}
680 			break;
681 		case IMSG_ACL_CHECK:
682 			if (imsg.hdr.len != IMSG_HEADER_SIZE +
683 			    sizeof(struct acl_check))
684 				fatalx("IMSG_ACL_CHECK imsg with wrong len");
685 			ldp_acl_reply(iev, (struct acl_check *)imsg.data);
686 			break;
687 		default:
688 			log_debug("%s: error handling imsg %d", __func__,
689 			    imsg.hdr.type);
690 			break;
691 		}
692 		imsg_free(&imsg);
693 	}
694 	if (!shut)
695 		imsg_event_add(iev);
696 	else {
697 		/* this pipe is dead, so remove the event handlers and exit */
698 		THREAD_READ_OFF(iev->ev_read);
699 		THREAD_WRITE_OFF(iev->ev_write);
700 		lde_pid = 0;
701 		if (ldpe_pid == 0)
702 			ldpd_shutdown();
703 		else
704 			kill(ldpe_pid, SIGTERM);
705 	}
706 
707 	return (0);
708 }
709 
710 /* ARGSUSED */
711 int
ldp_write_handler(struct thread * thread)712 ldp_write_handler(struct thread *thread)
713 {
714 	struct imsgev	*iev = THREAD_ARG(thread);
715 	struct imsgbuf	*ibuf = &iev->ibuf;
716 	ssize_t		 n;
717 
718 	iev->ev_write = NULL;
719 
720 	if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
721 		fatal("msgbuf_write");
722 	if (n == 0) {
723 		/* this pipe is dead, so remove the event handlers */
724 		THREAD_READ_OFF(iev->ev_read);
725 		THREAD_WRITE_OFF(iev->ev_write);
726 		return (0);
727 	}
728 
729 	imsg_event_add(iev);
730 
731 	return (0);
732 }
733 
734 void
main_imsg_compose_ldpe(int type,pid_t pid,void * data,uint16_t datalen)735 main_imsg_compose_ldpe(int type, pid_t pid, void *data, uint16_t datalen)
736 {
737 	if (iev_ldpe == NULL)
738 		return;
739 	imsg_compose_event(iev_ldpe, type, 0, pid, -1, data, datalen);
740 }
741 
742 void
main_imsg_compose_lde(int type,pid_t pid,void * data,uint16_t datalen)743 main_imsg_compose_lde(int type, pid_t pid, void *data, uint16_t datalen)
744 {
745 	imsg_compose_event(iev_lde, type, 0, pid, -1, data, datalen);
746 }
747 
748 int
main_imsg_compose_both(enum imsg_type type,void * buf,uint16_t len)749 main_imsg_compose_both(enum imsg_type type, void *buf, uint16_t len)
750 {
751 	if (iev_ldpe == NULL || iev_lde == NULL)
752 		return (0);
753 	if (imsg_compose_event(iev_ldpe, type, 0, 0, -1, buf, len) == -1)
754 		return (-1);
755 	if (imsg_compose_event(iev_lde, type, 0, 0, -1, buf, len) == -1)
756 		return (-1);
757 	return (0);
758 }
759 
760 void
imsg_event_add(struct imsgev * iev)761 imsg_event_add(struct imsgev *iev)
762 {
763 	if (iev->handler_read)
764 		thread_add_read(master, iev->handler_read, iev, iev->ibuf.fd,
765 				&iev->ev_read);
766 
767 	if (iev->handler_write && iev->ibuf.w.queued)
768 		thread_add_write(master, iev->handler_write, iev,
769 				 iev->ibuf.fd, &iev->ev_write);
770 }
771 
772 int
imsg_compose_event(struct imsgev * iev,uint16_t type,uint32_t peerid,pid_t pid,int fd,void * data,uint16_t datalen)773 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
774     pid_t pid, int fd, void *data, uint16_t datalen)
775 {
776 	int	ret;
777 
778 	if ((ret = imsg_compose(&iev->ibuf, type, peerid,
779 	    pid, fd, data, datalen)) != -1)
780 		imsg_event_add(iev);
781 	return (ret);
782 }
783 
784 void
evbuf_enqueue(struct evbuf * eb,struct ibuf * buf)785 evbuf_enqueue(struct evbuf *eb, struct ibuf *buf)
786 {
787 	ibuf_close(&eb->wbuf, buf);
788 	evbuf_event_add(eb);
789 }
790 
791 void
evbuf_event_add(struct evbuf * eb)792 evbuf_event_add(struct evbuf *eb)
793 {
794 	if (eb->wbuf.queued)
795 		thread_add_write(master, eb->handler, eb->arg, eb->wbuf.fd,
796 				 &eb->ev);
797 }
798 
799 void
evbuf_init(struct evbuf * eb,int fd,int (* handler)(struct thread *),void * arg)800 evbuf_init(struct evbuf *eb, int fd, int (*handler)(struct thread *),
801     void *arg)
802 {
803 	msgbuf_init(&eb->wbuf);
804 	eb->wbuf.fd = fd;
805 	eb->handler = handler;
806 	eb->arg = arg;
807 }
808 
809 void
evbuf_clear(struct evbuf * eb)810 evbuf_clear(struct evbuf *eb)
811 {
812 	THREAD_WRITE_OFF(eb->ev);
813 	msgbuf_clear(&eb->wbuf);
814 	eb->wbuf.fd = -1;
815 }
816 
817 static int
main_imsg_send_ipc_sockets(struct imsgbuf * ldpe_buf,struct imsgbuf * lde_buf)818 main_imsg_send_ipc_sockets(struct imsgbuf *ldpe_buf, struct imsgbuf *lde_buf)
819 {
820 	int pipe_ldpe2lde[2];
821 
822 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_ldpe2lde) == -1)
823 		return (-1);
824 	sock_set_nonblock(pipe_ldpe2lde[0]);
825 	sock_set_nonblock(pipe_ldpe2lde[1]);
826 
827 	if (imsg_compose(ldpe_buf, IMSG_SOCKET_IPC, 0, 0, pipe_ldpe2lde[0],
828 	    NULL, 0) == -1)
829 		return (-1);
830 	if (imsg_compose(lde_buf, IMSG_SOCKET_IPC, 0, 0, pipe_ldpe2lde[1],
831 	    NULL, 0) == -1)
832 		return (-1);
833 
834 	return (0);
835 }
836 
837 static void
main_imsg_send_net_sockets(int af)838 main_imsg_send_net_sockets(int af)
839 {
840 	if (!ldp_addrisset(af, &(ldp_af_conf_get(ldpd_conf, af))->trans_addr))
841 		return;
842 
843 	main_imsg_send_net_socket(af, LDP_SOCKET_DISC);
844 	main_imsg_send_net_socket(af, LDP_SOCKET_EDISC);
845 	main_imsg_send_net_socket(af, LDP_SOCKET_SESSION);
846 	imsg_compose_event(iev_ldpe, IMSG_SETUP_SOCKETS, af, 0, -1, NULL, 0);
847 }
848 
849 static void
main_imsg_send_net_socket(int af,enum socket_type type)850 main_imsg_send_net_socket(int af, enum socket_type type)
851 {
852 	int			 fd;
853 
854 	fd = ldp_create_socket(af, type);
855 	if (fd == -1) {
856 		log_warnx("%s: failed to create %s socket for address-family %s", __func__, socket_name(type), af_name(af));
857 		return;
858 	}
859 
860 	imsg_compose_event(iev_ldpe, IMSG_SOCKET_NET, af, 0, fd, &type,
861 	    sizeof(type));
862 }
863 
864 int
ldp_acl_request(struct imsgev * iev,char * acl_name,int af,union ldpd_addr * addr,uint8_t prefixlen)865 ldp_acl_request(struct imsgev *iev, char *acl_name, int af,
866     union ldpd_addr *addr, uint8_t prefixlen)
867 {
868 	struct imsg	 imsg;
869 	struct acl_check acl_check;
870 
871 	if (acl_name[0] == '\0')
872 		return FILTER_PERMIT;
873 
874 	/* build request */
875 	strlcpy(acl_check.acl, acl_name, sizeof(acl_check.acl));
876 	acl_check.af = af;
877 	acl_check.addr = *addr;
878 	acl_check.prefixlen = prefixlen;
879 
880 	/* send (blocking) */
881 	imsg_compose_event(iev, IMSG_ACL_CHECK, 0, 0, -1, &acl_check,
882 	    sizeof(acl_check));
883 	imsg_flush(&iev->ibuf);
884 
885 	/* receive (blocking) and parse result */
886 	if (imsg_read(&iev->ibuf) == -1)
887 		fatal("imsg_read error");
888 	if (imsg_get(&iev->ibuf, &imsg) == -1)
889 		fatal("imsg_get");
890 	if (imsg.hdr.type != IMSG_ACL_CHECK ||
891 	    imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(int))
892 		fatalx("ldp_acl_request: invalid response");
893 
894 	return (*((int *)imsg.data));
895 }
896 
897 void
ldp_acl_reply(struct imsgev * iev,struct acl_check * acl_check)898 ldp_acl_reply(struct imsgev *iev, struct acl_check *acl_check)
899 {
900 	struct access_list	*alist;
901 	struct prefix		 prefix;
902 	int			 result;
903 
904 	alist = access_list_lookup(family2afi(acl_check->af), acl_check->acl);
905 	if (alist == NULL)
906 		result = FILTER_DENY;
907 	else {
908 		prefix.family = acl_check->af;
909 		switch (prefix.family) {
910 		case AF_INET:
911 			prefix.u.prefix4 = acl_check->addr.v4;
912 			break;
913 		case AF_INET6:
914 			prefix.u.prefix6 = acl_check->addr.v6;
915 			break;
916 		default:
917 			fatalx("ldp_acl_reply: unknown af");
918 		}
919 		prefix.prefixlen = acl_check->prefixlen;
920 		result = access_list_apply(alist, &prefix);
921 	}
922 
923 	imsg_compose_event(iev, IMSG_ACL_CHECK, 0, 0, -1, &result,
924 	    sizeof(result));
925 }
926 
927 struct ldpd_af_conf *
ldp_af_conf_get(struct ldpd_conf * xconf,int af)928 ldp_af_conf_get(struct ldpd_conf *xconf, int af)
929 {
930 	switch (af) {
931 	case AF_INET:
932 		return (&xconf->ipv4);
933 	case AF_INET6:
934 		return (&xconf->ipv6);
935 	default:
936 		fatalx("ldp_af_conf_get: unknown af");
937 	}
938 }
939 
940 struct ldpd_af_global *
ldp_af_global_get(struct ldpd_global * xglobal,int af)941 ldp_af_global_get(struct ldpd_global *xglobal, int af)
942 {
943 	switch (af) {
944 	case AF_INET:
945 		return (&xglobal->ipv4);
946 	case AF_INET6:
947 		return (&xglobal->ipv6);
948 	default:
949 		fatalx("ldp_af_global_get: unknown af");
950 	}
951 }
952 
953 int
ldp_is_dual_stack(struct ldpd_conf * xconf)954 ldp_is_dual_stack(struct ldpd_conf *xconf)
955 {
956 	return ((xconf->ipv4.flags & F_LDPD_AF_ENABLED) &&
957 	    (xconf->ipv6.flags & F_LDPD_AF_ENABLED));
958 }
959 
960 in_addr_t
ldp_rtr_id_get(struct ldpd_conf * xconf)961 ldp_rtr_id_get(struct ldpd_conf *xconf)
962 {
963 	if (xconf->rtr_id.s_addr != INADDR_ANY)
964 		return (xconf->rtr_id.s_addr);
965 	else
966 		return (global.rtr_id.s_addr);
967 }
968 
969 static int
main_imsg_send_config(struct ldpd_conf * xconf)970 main_imsg_send_config(struct ldpd_conf *xconf)
971 {
972 	struct iface		*iface;
973 	struct tnbr		*tnbr;
974 	struct nbr_params	*nbrp;
975 	struct l2vpn		*l2vpn;
976 	struct l2vpn_if		*lif;
977 	struct l2vpn_pw		*pw;
978 
979 	if (main_imsg_compose_both(IMSG_RECONF_CONF, xconf,
980 	    sizeof(*xconf)) == -1)
981 		return (-1);
982 
983 	RB_FOREACH(iface, iface_head, &xconf->iface_tree) {
984 		if (main_imsg_compose_both(IMSG_RECONF_IFACE, iface,
985 		    sizeof(*iface)) == -1)
986 			return (-1);
987 	}
988 
989 	RB_FOREACH(tnbr, tnbr_head, &xconf->tnbr_tree) {
990 		if (main_imsg_compose_both(IMSG_RECONF_TNBR, tnbr,
991 		    sizeof(*tnbr)) == -1)
992 			return (-1);
993 	}
994 
995 	RB_FOREACH(nbrp, nbrp_head, &xconf->nbrp_tree) {
996 		if (main_imsg_compose_both(IMSG_RECONF_NBRP, nbrp,
997 		    sizeof(*nbrp)) == -1)
998 			return (-1);
999 	}
1000 
1001 	RB_FOREACH(l2vpn, l2vpn_head, &xconf->l2vpn_tree) {
1002 		if (main_imsg_compose_both(IMSG_RECONF_L2VPN, l2vpn,
1003 		    sizeof(*l2vpn)) == -1)
1004 			return (-1);
1005 
1006 		RB_FOREACH(lif, l2vpn_if_head, &l2vpn->if_tree) {
1007 			if (main_imsg_compose_both(IMSG_RECONF_L2VPN_IF, lif,
1008 			    sizeof(*lif)) == -1)
1009 				return (-1);
1010 		}
1011 		RB_FOREACH(pw, l2vpn_pw_head, &l2vpn->pw_tree) {
1012 			if (main_imsg_compose_both(IMSG_RECONF_L2VPN_PW, pw,
1013 			    sizeof(*pw)) == -1)
1014 				return (-1);
1015 		}
1016 		RB_FOREACH(pw, l2vpn_pw_head, &l2vpn->pw_inactive_tree) {
1017 			if (main_imsg_compose_both(IMSG_RECONF_L2VPN_IPW, pw,
1018 			    sizeof(*pw)) == -1)
1019 				return (-1);
1020 		}
1021 	}
1022 
1023 	if (main_imsg_compose_both(IMSG_RECONF_END, NULL, 0) == -1)
1024 		return (-1);
1025 
1026 	return (0);
1027 }
1028 
1029 int
ldp_config_apply(struct vty * vty,struct ldpd_conf * xconf)1030 ldp_config_apply(struct vty *vty, struct ldpd_conf *xconf)
1031 {
1032 	/*
1033 	 * When reading from a configuration file (startup and sighup), we
1034 	 * call merge_config() only once after the whole config has been read.
1035 	 * This is the optimal and least disruptive way to update the running
1036 	 * configuration.
1037 	 */
1038 	if (vty && vty->type == VTY_FILE)
1039 		return (0);
1040 
1041 	ldp_config_normalize(xconf);
1042 
1043 	if (main_imsg_send_config(xconf) == -1)
1044 		return (-1);
1045 
1046 	merge_config(ldpd_conf, xconf);
1047 
1048 	return (0);
1049 }
1050 
1051 static void
ldp_config_normalize(struct ldpd_conf * xconf)1052 ldp_config_normalize(struct ldpd_conf *xconf)
1053 {
1054 	struct iface		*iface, *itmp;
1055 	struct nbr_params	*nbrp, *ntmp;
1056 	struct l2vpn		*l2vpn;
1057 	struct l2vpn_pw		*pw, *ptmp;
1058 
1059 	if (!(xconf->flags & F_LDPD_ENABLED))
1060 		ldp_config_reset_main(xconf);
1061 	else {
1062 		if (!(xconf->ipv4.flags & F_LDPD_AF_ENABLED))
1063 			ldp_config_reset_af(xconf, AF_INET);
1064 		if (!(xconf->ipv6.flags & F_LDPD_AF_ENABLED))
1065 			ldp_config_reset_af(xconf, AF_INET6);
1066 
1067 		RB_FOREACH_SAFE(iface, iface_head, &xconf->iface_tree, itmp) {
1068 			if (iface->ipv4.enabled || iface->ipv6.enabled)
1069 				continue;
1070 
1071 			QOBJ_UNREG(iface);
1072 			RB_REMOVE(iface_head, &vty_conf->iface_tree, iface);
1073 			free(iface);
1074 		}
1075 
1076 		RB_FOREACH_SAFE(nbrp, nbrp_head, &xconf->nbrp_tree, ntmp) {
1077 			if (nbrp->flags & (F_NBRP_KEEPALIVE|F_NBRP_GTSM))
1078 				continue;
1079 			if (nbrp->auth.method != AUTH_NONE)
1080 				continue;
1081 
1082 			QOBJ_UNREG(nbrp);
1083 			RB_REMOVE(nbrp_head, &vty_conf->nbrp_tree, nbrp);
1084 			free(nbrp);
1085 		}
1086 	}
1087 
1088 	RB_FOREACH(l2vpn, l2vpn_head, &xconf->l2vpn_tree) {
1089 		RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_tree, ptmp) {
1090 			if (!(pw->flags & F_PW_STATIC_NBR_ADDR)) {
1091 				pw->af = AF_INET;
1092 				pw->addr.v4 = pw->lsr_id;
1093 			}
1094 
1095 			if (pw->lsr_id.s_addr != INADDR_ANY && pw->pwid != 0)
1096 				continue;
1097 			RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw);
1098 			RB_INSERT(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
1099 		}
1100 		RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_inactive_tree,
1101 		    ptmp) {
1102 			if (!(pw->flags & F_PW_STATIC_NBR_ADDR)) {
1103 				pw->af = AF_INET;
1104 				pw->addr.v4 = pw->lsr_id;
1105 			}
1106 
1107 			if (pw->lsr_id.s_addr == INADDR_ANY || pw->pwid == 0)
1108 				continue;
1109 			RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
1110 			RB_INSERT(l2vpn_pw_head, &l2vpn->pw_tree, pw);
1111 		}
1112 	}
1113 }
1114 
1115 static void
ldp_config_reset(struct ldpd_conf * conf)1116 ldp_config_reset(struct ldpd_conf *conf)
1117 {
1118 	ldp_config_reset_main(conf);
1119 	ldp_config_reset_l2vpns(conf);
1120 }
1121 
1122 static void
ldp_config_reset_main(struct ldpd_conf * conf)1123 ldp_config_reset_main(struct ldpd_conf *conf)
1124 {
1125 	struct iface		*iface;
1126 	struct nbr_params	*nbrp;
1127 
1128 	while (!RB_EMPTY(iface_head, &conf->iface_tree)) {
1129 		iface = RB_ROOT(iface_head, &conf->iface_tree);
1130 
1131 		QOBJ_UNREG(iface);
1132 		RB_REMOVE(iface_head, &conf->iface_tree, iface);
1133 		free(iface);
1134 	}
1135 
1136 	while (!RB_EMPTY(nbrp_head, &conf->nbrp_tree)) {
1137 		nbrp = RB_ROOT(nbrp_head, &conf->nbrp_tree);
1138 
1139 		QOBJ_UNREG(nbrp);
1140 		RB_REMOVE(nbrp_head, &conf->nbrp_tree, nbrp);
1141 		free(nbrp);
1142 	}
1143 
1144 	conf->rtr_id.s_addr = INADDR_ANY;
1145 	ldp_config_reset_af(conf, AF_INET);
1146 	ldp_config_reset_af(conf, AF_INET6);
1147 	conf->lhello_holdtime = LINK_DFLT_HOLDTIME;
1148 	conf->lhello_interval = DEFAULT_HELLO_INTERVAL;
1149 	conf->thello_holdtime = TARGETED_DFLT_HOLDTIME;
1150 	conf->thello_interval = DEFAULT_HELLO_INTERVAL;
1151 	conf->trans_pref = DUAL_STACK_LDPOV6;
1152 	conf->flags = 0;
1153 }
1154 
1155 static void
ldp_config_reset_af(struct ldpd_conf * conf,int af)1156 ldp_config_reset_af(struct ldpd_conf *conf, int af)
1157 {
1158 	struct ldpd_af_conf	*af_conf;
1159 	struct iface		*iface;
1160 	struct iface_af		*ia;
1161 	struct tnbr		*tnbr, *ttmp;
1162 
1163 	RB_FOREACH(iface, iface_head, &conf->iface_tree) {
1164 		ia = iface_af_get(iface, af);
1165 		ia->enabled = 0;
1166 	}
1167 
1168 	RB_FOREACH_SAFE(tnbr, tnbr_head, &conf->tnbr_tree, ttmp) {
1169 		if (tnbr->af != af)
1170 			continue;
1171 
1172 		QOBJ_UNREG(tnbr);
1173 		RB_REMOVE(tnbr_head, &conf->tnbr_tree, tnbr);
1174 		free(tnbr);
1175 	}
1176 
1177 	af_conf = ldp_af_conf_get(conf, af);
1178 	af_conf->keepalive = 180;
1179 	af_conf->lhello_holdtime = 0;
1180 	af_conf->lhello_interval = 0;
1181 	af_conf->thello_holdtime = 0;
1182 	af_conf->thello_interval = 0;
1183 	memset(&af_conf->trans_addr, 0, sizeof(af_conf->trans_addr));
1184 	af_conf->flags = 0;
1185 }
1186 
1187 static void
ldp_config_reset_l2vpns(struct ldpd_conf * conf)1188 ldp_config_reset_l2vpns(struct ldpd_conf *conf)
1189 {
1190 	struct l2vpn		*l2vpn;
1191 	struct l2vpn_if		*lif;
1192 	struct l2vpn_pw		*pw;
1193 
1194 	while (!RB_EMPTY(l2vpn_head, &conf->l2vpn_tree)) {
1195 		l2vpn = RB_ROOT(l2vpn_head, &conf->l2vpn_tree);
1196 		while (!RB_EMPTY(l2vpn_if_head, &l2vpn->if_tree)) {
1197 			lif = RB_ROOT(l2vpn_if_head, &l2vpn->if_tree);
1198 
1199 			QOBJ_UNREG(lif);
1200 			RB_REMOVE(l2vpn_if_head, &l2vpn->if_tree, lif);
1201 			free(lif);
1202 		}
1203 		while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_tree)) {
1204 			pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_tree);
1205 
1206 			QOBJ_UNREG(pw);
1207 			RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw);
1208 			free(pw);
1209 		}
1210 		while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_inactive_tree)) {
1211 			pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_inactive_tree);
1212 
1213 			QOBJ_UNREG(pw);
1214 			RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
1215 			free(pw);
1216 		}
1217 		QOBJ_UNREG(l2vpn);
1218 		RB_REMOVE(l2vpn_head, &conf->l2vpn_tree, l2vpn);
1219 		free(l2vpn);
1220 	}
1221 }
1222 
1223 void
ldp_clear_config(struct ldpd_conf * xconf)1224 ldp_clear_config(struct ldpd_conf *xconf)
1225 {
1226 	struct iface		*iface;
1227 	struct tnbr		*tnbr;
1228 	struct nbr_params	*nbrp;
1229 	struct l2vpn		*l2vpn;
1230 
1231 	while (!RB_EMPTY(iface_head, &xconf->iface_tree)) {
1232 		iface = RB_ROOT(iface_head, &xconf->iface_tree);
1233 
1234 		RB_REMOVE(iface_head, &xconf->iface_tree, iface);
1235 		free(iface);
1236 	}
1237 	while (!RB_EMPTY(tnbr_head, &xconf->tnbr_tree)) {
1238 		tnbr = RB_ROOT(tnbr_head, &xconf->tnbr_tree);
1239 
1240 		RB_REMOVE(tnbr_head, &xconf->tnbr_tree, tnbr);
1241 		free(tnbr);
1242 	}
1243 	while (!RB_EMPTY(nbrp_head, &xconf->nbrp_tree)) {
1244 		nbrp = RB_ROOT(nbrp_head, &xconf->nbrp_tree);
1245 
1246 		RB_REMOVE(nbrp_head, &xconf->nbrp_tree, nbrp);
1247 		free(nbrp);
1248 	}
1249 	while (!RB_EMPTY(l2vpn_head, &xconf->l2vpn_tree)) {
1250 		l2vpn = RB_ROOT(l2vpn_head, &xconf->l2vpn_tree);
1251 
1252 		RB_REMOVE(l2vpn_head, &xconf->l2vpn_tree, l2vpn);
1253 		l2vpn_del(l2vpn);
1254 	}
1255 
1256 	free(xconf);
1257 }
1258 
1259 #define COPY(a, b) do { \
1260 		a = malloc(sizeof(*a)); \
1261 		if (a == NULL) \
1262 			fatal(__func__); \
1263 		*a = *b; \
1264 	} while (0)
1265 
1266 void
merge_config(struct ldpd_conf * conf,struct ldpd_conf * xconf)1267 merge_config(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1268 {
1269 	merge_global(conf, xconf);
1270 	merge_af(AF_INET, &conf->ipv4, &xconf->ipv4);
1271 	merge_af(AF_INET6, &conf->ipv6, &xconf->ipv6);
1272 	merge_ifaces(conf, xconf);
1273 	merge_tnbrs(conf, xconf);
1274 	merge_nbrps(conf, xconf);
1275 	merge_l2vpns(conf, xconf);
1276 }
1277 
1278 static void
merge_global(struct ldpd_conf * conf,struct ldpd_conf * xconf)1279 merge_global(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1280 {
1281 	/* change of router-id requires resetting all neighborships */
1282 	if (conf->rtr_id.s_addr != xconf->rtr_id.s_addr) {
1283 		if (ldpd_process == PROC_LDP_ENGINE) {
1284 			ldpe_reset_nbrs(AF_UNSPEC);
1285 			if (conf->rtr_id.s_addr == INADDR_ANY ||
1286 			    xconf->rtr_id.s_addr == INADDR_ANY) {
1287 				if_update_all(AF_UNSPEC);
1288 				tnbr_update_all(AF_UNSPEC);
1289 			}
1290 		}
1291 		conf->rtr_id = xconf->rtr_id;
1292 	}
1293 
1294 	/*
1295 	 * Configuration of ordered-control or independent-control
1296 	 * requires resetting all neighborships.
1297 	 */
1298 	if ((conf->flags & F_LDPD_ORDERED_CONTROL) !=
1299 	    (xconf->flags & F_LDPD_ORDERED_CONTROL))
1300 		ldpe_reset_nbrs(AF_UNSPEC);
1301 
1302 	conf->lhello_holdtime = xconf->lhello_holdtime;
1303 	conf->lhello_interval = xconf->lhello_interval;
1304 	conf->thello_holdtime = xconf->thello_holdtime;
1305 	conf->thello_interval = xconf->thello_interval;
1306 
1307 	if (conf->trans_pref != xconf->trans_pref) {
1308 		if (ldpd_process == PROC_LDP_ENGINE)
1309 			ldpe_reset_ds_nbrs();
1310 		conf->trans_pref = xconf->trans_pref;
1311 	}
1312 
1313 	if ((conf->flags & F_LDPD_DS_CISCO_INTEROP) !=
1314 	    (xconf->flags & F_LDPD_DS_CISCO_INTEROP)) {
1315 		if (ldpd_process == PROC_LDP_ENGINE)
1316 			ldpe_reset_ds_nbrs();
1317 	}
1318 
1319 	conf->flags = xconf->flags;
1320 }
1321 
1322 static void
merge_af(int af,struct ldpd_af_conf * af_conf,struct ldpd_af_conf * xa)1323 merge_af(int af, struct ldpd_af_conf *af_conf, struct ldpd_af_conf *xa)
1324 {
1325 	int		 stop_init_backoff = 0;
1326 	int 		 remove_dynamic_tnbrs = 0;
1327 	int		 change_egress_label = 0;
1328 	int		 change_host_label = 0;
1329 	int		 reset_nbrs_ipv4 = 0;
1330 	int		 reset_nbrs = 0;
1331 	int		 update_sockets = 0;
1332 	int		 change_ldp_disabled = 0;
1333 
1334 	/* update timers */
1335 	if (af_conf->keepalive != xa->keepalive) {
1336 		af_conf->keepalive = xa->keepalive;
1337 		stop_init_backoff = 1;
1338 	}
1339 	af_conf->lhello_holdtime = xa->lhello_holdtime;
1340 	af_conf->lhello_interval = xa->lhello_interval;
1341 	af_conf->thello_holdtime = xa->thello_holdtime;
1342 	af_conf->thello_interval = xa->thello_interval;
1343 
1344 	/* update flags */
1345 	if ((af_conf->flags & F_LDPD_AF_THELLO_ACCEPT) &&
1346 	    !(xa->flags & F_LDPD_AF_THELLO_ACCEPT))
1347 		remove_dynamic_tnbrs = 1;
1348 	if ((af_conf->flags & F_LDPD_AF_NO_GTSM) !=
1349 	    (xa->flags & F_LDPD_AF_NO_GTSM)) {
1350 		if (af == AF_INET6)
1351 			/* need to set/unset IPV6_MINHOPCOUNT */
1352 			update_sockets = 1;
1353 		else
1354 			/* for LDPv4 just resetting the neighbors is enough */
1355 			reset_nbrs_ipv4 = 1;
1356 	}
1357 	if ((af_conf->flags & F_LDPD_AF_EXPNULL) !=
1358 	    (xa->flags & F_LDPD_AF_EXPNULL))
1359 		change_egress_label = 1;
1360 
1361 	/* changing config of host only fec filtering */
1362 	if ((af_conf->flags & F_LDPD_AF_ALLOCHOSTONLY)
1363 	    != (xa->flags & F_LDPD_AF_ALLOCHOSTONLY))
1364 		change_host_label = 1;
1365 
1366 	/* disabling LDP for address family */
1367 	if ((af_conf->flags & F_LDPD_AF_ENABLED) &&
1368 	    !(xa->flags & F_LDPD_AF_ENABLED))
1369 		change_ldp_disabled = 1;
1370 
1371 	af_conf->flags = xa->flags;
1372 
1373 	/* update the transport address */
1374 	if (ldp_addrcmp(af, &af_conf->trans_addr, &xa->trans_addr)) {
1375 		af_conf->trans_addr = xa->trans_addr;
1376 		update_sockets = 1;
1377 	}
1378 
1379 	/* update ACLs */
1380 	if (strcmp(af_conf->acl_label_allocate_for, xa->acl_label_allocate_for))
1381 		change_host_label = 1;
1382 
1383 	if (strcmp(af_conf->acl_label_advertise_to,
1384 	    xa->acl_label_advertise_to) ||
1385 	    strcmp(af_conf->acl_label_advertise_for,
1386 	    xa->acl_label_advertise_for) ||
1387 	    strcmp(af_conf->acl_label_accept_from,
1388 	    xa->acl_label_accept_from) ||
1389 	    strcmp(af_conf->acl_label_accept_for,
1390 	    xa->acl_label_accept_for))
1391 		reset_nbrs = 1;
1392 	if (strcmp(af_conf->acl_thello_accept_from, xa->acl_thello_accept_from))
1393 		remove_dynamic_tnbrs = 1;
1394 	if (strcmp(af_conf->acl_label_expnull_for, xa->acl_label_expnull_for))
1395 		change_egress_label = 1;
1396 	strlcpy(af_conf->acl_thello_accept_from, xa->acl_thello_accept_from,
1397 	    sizeof(af_conf->acl_thello_accept_from));
1398 	strlcpy(af_conf->acl_label_allocate_for, xa->acl_label_allocate_for,
1399 	    sizeof(af_conf->acl_label_allocate_for));
1400 	strlcpy(af_conf->acl_label_advertise_to, xa->acl_label_advertise_to,
1401 	    sizeof(af_conf->acl_label_advertise_to));
1402 	strlcpy(af_conf->acl_label_advertise_for, xa->acl_label_advertise_for,
1403 	    sizeof(af_conf->acl_label_advertise_for));
1404 	strlcpy(af_conf->acl_label_accept_from, xa->acl_label_accept_from,
1405 	    sizeof(af_conf->acl_label_accept_from));
1406 	strlcpy(af_conf->acl_label_accept_for, xa->acl_label_accept_for,
1407 	    sizeof(af_conf->acl_label_accept_for));
1408 	strlcpy(af_conf->acl_label_expnull_for, xa->acl_label_expnull_for,
1409 	    sizeof(af_conf->acl_label_expnull_for));
1410 
1411 	/* apply the new configuration */
1412 	switch (ldpd_process) {
1413 	case PROC_LDE_ENGINE:
1414 		if (change_egress_label)
1415 			lde_change_egress_label(af);
1416 		if (change_host_label)
1417 			lde_change_allocate_filter(af);
1418 		if (change_ldp_disabled)
1419 			lde_route_update_release_all(af);
1420 
1421 		break;
1422 	case PROC_LDP_ENGINE:
1423 		if (stop_init_backoff)
1424 			ldpe_stop_init_backoff(af);
1425 		if (remove_dynamic_tnbrs)
1426 			ldpe_remove_dynamic_tnbrs(af);
1427 		if (reset_nbrs)
1428 			ldpe_reset_nbrs(AF_UNSPEC);
1429 		else if (reset_nbrs_ipv4)
1430 			ldpe_reset_nbrs(AF_INET);
1431 		break;
1432 	case PROC_MAIN:
1433 		if (update_sockets && iev_ldpe)
1434 			imsg_compose_event(iev_ldpe, IMSG_CLOSE_SOCKETS, af,
1435 			    0, -1, NULL, 0);
1436 		break;
1437 	}
1438 }
1439 
1440 static void
merge_ifaces(struct ldpd_conf * conf,struct ldpd_conf * xconf)1441 merge_ifaces(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1442 {
1443 	struct iface		*iface, *itmp, *xi;
1444 
1445 	RB_FOREACH_SAFE(iface, iface_head, &conf->iface_tree, itmp) {
1446 		/* find deleted interfaces, which occurs when LDP is removed
1447 		 * for all address families
1448 		 */
1449 		if (if_lookup_name(xconf, iface->name) == NULL) {
1450 			switch (ldpd_process) {
1451 			case PROC_LDP_ENGINE:
1452 				ldpe_if_exit(iface);
1453 				break;
1454 			case PROC_LDE_ENGINE:
1455 				if (iface->ipv4.enabled)
1456 					lde_route_update_release(iface,
1457 					    AF_INET);
1458 				if (iface->ipv6.enabled)
1459 					lde_route_update_release(iface,
1460 					    AF_INET6);
1461 				break;
1462 			case PROC_MAIN:
1463 				break;
1464 			}
1465 			RB_REMOVE(iface_head, &conf->iface_tree, iface);
1466 			free(iface);
1467 		}
1468 	}
1469 	RB_FOREACH_SAFE(xi, iface_head, &xconf->iface_tree, itmp) {
1470 		/* find new interfaces */
1471 		if ((iface = if_lookup_name(conf, xi->name)) == NULL) {
1472 			COPY(iface, xi);
1473 			RB_INSERT(iface_head, &conf->iface_tree, iface);
1474 
1475 			switch (ldpd_process) {
1476 			case PROC_LDP_ENGINE:
1477 				ldpe_if_init(iface);
1478 				break;
1479 			case PROC_LDE_ENGINE:
1480 				break;
1481 			case PROC_MAIN:
1482 				/* resend addresses to activate new interfaces */
1483 				kif_redistribute(iface->name);
1484 				break;
1485 			}
1486 			continue;
1487 		}
1488 
1489 		/* update labels when adding or removing ldp on an
1490 		 * interface
1491 		 */
1492 		if (ldpd_process == PROC_LDE_ENGINE) {
1493 			/* if we are removing lpd config for an address
1494 			 * family on an interface then advertise routes
1495 			 * learned over this interface as if they were
1496 			 * connected routes
1497 			 */
1498 			if (iface->ipv4.enabled && !xi->ipv4.enabled)
1499 				lde_route_update_release(iface, AF_INET);
1500 			if (iface->ipv6.enabled && !xi->ipv6.enabled)
1501 				lde_route_update_release(iface, AF_INET6);
1502 
1503 			/* if we are adding lpd config for an address
1504 			 * family on an interface then add proper labels
1505 			 */
1506 			if (!iface->ipv4.enabled && xi->ipv4.enabled)
1507 				lde_route_update(iface, AF_INET);
1508 			if (!iface->ipv6.enabled && xi->ipv6.enabled)
1509 				lde_route_update(iface, AF_INET6);
1510 		}
1511 
1512 		/* update existing interfaces */
1513 		merge_iface_af(&iface->ipv4, &xi->ipv4);
1514 		merge_iface_af(&iface->ipv6, &xi->ipv6);
1515 	}
1516 }
1517 
1518 static void
merge_iface_af(struct iface_af * ia,struct iface_af * xi)1519 merge_iface_af(struct iface_af *ia, struct iface_af *xi)
1520 {
1521 	if (ia->enabled != xi->enabled) {
1522 		ia->enabled = xi->enabled;
1523 		if (ldpd_process == PROC_LDP_ENGINE)
1524 			ldp_if_update(ia->iface, ia->af);
1525 	}
1526 	ia->hello_holdtime = xi->hello_holdtime;
1527 	ia->hello_interval = xi->hello_interval;
1528 }
1529 
1530 static void
merge_tnbrs(struct ldpd_conf * conf,struct ldpd_conf * xconf)1531 merge_tnbrs(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1532 {
1533 	struct tnbr		*tnbr, *ttmp, *xt;
1534 
1535 	RB_FOREACH_SAFE(tnbr, tnbr_head, &conf->tnbr_tree, ttmp) {
1536 		if (!(tnbr->flags & F_TNBR_CONFIGURED))
1537 			continue;
1538 
1539 		/* find deleted tnbrs */
1540 		if (tnbr_find(xconf, tnbr->af, &tnbr->addr) == NULL) {
1541 			switch (ldpd_process) {
1542 			case PROC_LDP_ENGINE:
1543 				tnbr->flags &= ~F_TNBR_CONFIGURED;
1544 				tnbr_check(conf, tnbr);
1545 				break;
1546 			case PROC_LDE_ENGINE:
1547 			case PROC_MAIN:
1548 				RB_REMOVE(tnbr_head, &conf->tnbr_tree, tnbr);
1549 				free(tnbr);
1550 				break;
1551 			}
1552 		}
1553 	}
1554 	RB_FOREACH_SAFE(xt, tnbr_head, &xconf->tnbr_tree, ttmp) {
1555 		/* find new tnbrs */
1556 		if ((tnbr = tnbr_find(conf, xt->af, &xt->addr)) == NULL) {
1557 			COPY(tnbr, xt);
1558 			RB_INSERT(tnbr_head, &conf->tnbr_tree, tnbr);
1559 
1560 			switch (ldpd_process) {
1561 			case PROC_LDP_ENGINE:
1562 				tnbr_update(tnbr);
1563 				break;
1564 			case PROC_LDE_ENGINE:
1565 			case PROC_MAIN:
1566 				break;
1567 			}
1568 			continue;
1569 		}
1570 
1571 		/* update existing tnbrs */
1572 		if (!(tnbr->flags & F_TNBR_CONFIGURED))
1573 			tnbr->flags |= F_TNBR_CONFIGURED;
1574 	}
1575 }
1576 
1577 static void
merge_nbrps(struct ldpd_conf * conf,struct ldpd_conf * xconf)1578 merge_nbrps(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1579 {
1580 	struct nbr_params	*nbrp, *ntmp, *xn;
1581 	struct nbr		*nbr;
1582 	int			 nbrp_changed;
1583 
1584 	RB_FOREACH_SAFE(nbrp, nbrp_head, &conf->nbrp_tree, ntmp) {
1585 		/* find deleted nbrps */
1586 		if (nbr_params_find(xconf, nbrp->lsr_id) != NULL)
1587 			continue;
1588 
1589 		switch (ldpd_process) {
1590 		case PROC_LDP_ENGINE:
1591 			nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr);
1592 			if (nbr) {
1593 				session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1594 #ifdef __OpenBSD__
1595 				pfkey_remove(nbr);
1596 #else
1597 				sock_set_md5sig(
1598 					(ldp_af_global_get(&global, nbr->af))
1599 						->ldp_session_socket,
1600 					nbr->af, &nbr->raddr, NULL);
1601 #endif
1602 				nbr->auth.method = AUTH_NONE;
1603 				if (nbr_session_active_role(nbr))
1604 					nbr_establish_connection(nbr);
1605 			}
1606 			break;
1607 		case PROC_LDE_ENGINE:
1608 		case PROC_MAIN:
1609 			break;
1610 		}
1611 		RB_REMOVE(nbrp_head, &conf->nbrp_tree, nbrp);
1612 		free(nbrp);
1613 	}
1614 
1615 	RB_FOREACH_SAFE(xn, nbrp_head, &xconf->nbrp_tree, ntmp) {
1616 		/* find new nbrps */
1617 		if ((nbrp = nbr_params_find(conf, xn->lsr_id)) == NULL) {
1618 			COPY(nbrp, xn);
1619 			RB_INSERT(nbrp_head, &conf->nbrp_tree, nbrp);
1620 
1621 			switch (ldpd_process) {
1622 			case PROC_LDP_ENGINE:
1623 				nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr);
1624 				if (nbr) {
1625 					session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1626 					nbr->auth.method = nbrp->auth.method;
1627 #ifdef __OpenBSD__
1628 					if (pfkey_establish(nbr, nbrp) == -1)
1629 						fatalx("pfkey setup failed");
1630 #else
1631 					sock_set_md5sig(
1632 					    (ldp_af_global_get(&global,
1633 					    nbr->af))->ldp_session_socket,
1634 					    nbr->af, &nbr->raddr,
1635 					    nbrp->auth.md5key);
1636 #endif
1637 					if (nbr_session_active_role(nbr))
1638 						nbr_establish_connection(nbr);
1639 				}
1640 				break;
1641 			case PROC_LDE_ENGINE:
1642 			case PROC_MAIN:
1643 				break;
1644 			}
1645 			continue;
1646 		}
1647 
1648 		/* update existing nbrps */
1649 		if (nbrp->flags != xn->flags ||
1650 		    nbrp->keepalive != xn->keepalive ||
1651 		    nbrp->gtsm_enabled != xn->gtsm_enabled ||
1652 		    nbrp->gtsm_hops != xn->gtsm_hops ||
1653 		    nbrp->auth.method != xn->auth.method ||
1654 		    strcmp(nbrp->auth.md5key, xn->auth.md5key) != 0)
1655 			nbrp_changed = 1;
1656 		else
1657 			nbrp_changed = 0;
1658 
1659 		nbrp->keepalive = xn->keepalive;
1660 		nbrp->gtsm_enabled = xn->gtsm_enabled;
1661 		nbrp->gtsm_hops = xn->gtsm_hops;
1662 		nbrp->auth.method = xn->auth.method;
1663 		strlcpy(nbrp->auth.md5key, xn->auth.md5key,
1664 		    sizeof(nbrp->auth.md5key));
1665 		nbrp->auth.md5key_len = xn->auth.md5key_len;
1666 		nbrp->flags = xn->flags;
1667 
1668 		if (ldpd_process == PROC_LDP_ENGINE) {
1669 			nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr);
1670 			if (nbr && nbrp_changed) {
1671 				session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1672 #ifdef __OpenBSD__
1673 				pfkey_remove(nbr);
1674 				nbr->auth.method = nbrp->auth.method;
1675 				if (pfkey_establish(nbr, nbrp) == -1)
1676 					fatalx("pfkey setup failed");
1677 #else
1678 				nbr->auth.method = nbrp->auth.method;
1679 				sock_set_md5sig((ldp_af_global_get(&global,
1680 				    nbr->af))->ldp_session_socket, nbr->af,
1681 				    &nbr->raddr, nbrp->auth.md5key);
1682 #endif
1683 				if (nbr_session_active_role(nbr))
1684 					nbr_establish_connection(nbr);
1685 			}
1686 		}
1687 	}
1688 }
1689 
1690 static void
merge_l2vpns(struct ldpd_conf * conf,struct ldpd_conf * xconf)1691 merge_l2vpns(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1692 {
1693 	struct l2vpn		*l2vpn, *ltmp, *xl;
1694 
1695 	RB_FOREACH_SAFE(l2vpn, l2vpn_head, &conf->l2vpn_tree, ltmp) {
1696 		/* find deleted l2vpns */
1697 		if (l2vpn_find(xconf, l2vpn->name) == NULL) {
1698 			switch (ldpd_process) {
1699 			case PROC_LDE_ENGINE:
1700 				l2vpn_exit(l2vpn);
1701 				break;
1702 			case PROC_LDP_ENGINE:
1703 				ldpe_l2vpn_exit(l2vpn);
1704 				break;
1705 			case PROC_MAIN:
1706 				break;
1707 			}
1708 			RB_REMOVE(l2vpn_head, &conf->l2vpn_tree, l2vpn);
1709 			l2vpn_del(l2vpn);
1710 		}
1711 	}
1712 	RB_FOREACH_SAFE(xl, l2vpn_head, &xconf->l2vpn_tree, ltmp) {
1713 		/* find new l2vpns */
1714 		if ((l2vpn = l2vpn_find(conf, xl->name)) == NULL) {
1715 			COPY(l2vpn, xl);
1716 			RB_INSERT(l2vpn_head, &conf->l2vpn_tree, l2vpn);
1717 			RB_INIT(l2vpn_if_head, &l2vpn->if_tree);
1718 			RB_INIT(l2vpn_pw_head, &l2vpn->pw_tree);
1719 			RB_INIT(l2vpn_pw_head, &l2vpn->pw_inactive_tree);
1720 
1721 			switch (ldpd_process) {
1722 			case PROC_LDE_ENGINE:
1723 				l2vpn_init(l2vpn);
1724 				break;
1725 			case PROC_LDP_ENGINE:
1726 				ldpe_l2vpn_init(l2vpn);
1727 				break;
1728 			case PROC_MAIN:
1729 				break;
1730 			}
1731 		}
1732 
1733 		/* update existing l2vpns */
1734 		merge_l2vpn(conf, l2vpn, xl);
1735 	}
1736 }
1737 
1738 static void
merge_l2vpn(struct ldpd_conf * xconf,struct l2vpn * l2vpn,struct l2vpn * xl)1739 merge_l2vpn(struct ldpd_conf *xconf, struct l2vpn *l2vpn, struct l2vpn *xl)
1740 {
1741 	struct l2vpn_if		*lif, *ftmp, *xf;
1742 	struct l2vpn_pw		*pw, *ptmp, *xp;
1743 	struct nbr		*nbr;
1744 	int			 reset_nbr, reinstall_pwfec, reinstall_tnbr;
1745 	int			 previous_pw_type, previous_mtu;
1746 
1747 	previous_pw_type = l2vpn->pw_type;
1748 	previous_mtu = l2vpn->mtu;
1749 
1750 	/* merge intefaces */
1751 	RB_FOREACH_SAFE(lif, l2vpn_if_head, &l2vpn->if_tree, ftmp) {
1752 		/* find deleted interfaces */
1753 		if (l2vpn_if_find(xl, lif->ifname) == NULL) {
1754 			RB_REMOVE(l2vpn_if_head, &l2vpn->if_tree, lif);
1755 			free(lif);
1756 		}
1757 	}
1758 	RB_FOREACH_SAFE(xf, l2vpn_if_head, &xl->if_tree, ftmp) {
1759 		/* find new interfaces */
1760 		if (l2vpn_if_find(l2vpn, xf->ifname) == NULL) {
1761 			COPY(lif, xf);
1762 			RB_INSERT(l2vpn_if_head, &l2vpn->if_tree, lif);
1763 			lif->l2vpn = l2vpn;
1764 
1765 			switch (ldpd_process) {
1766 			case PROC_LDP_ENGINE:
1767 			case PROC_LDE_ENGINE:
1768 				break;
1769 			case PROC_MAIN:
1770 				kif_redistribute(lif->ifname);
1771 				break;
1772 			}
1773 		}
1774 	}
1775 
1776 	/* merge active pseudowires */
1777 	RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_tree, ptmp) {
1778 		/* find deleted active pseudowires */
1779 		if (l2vpn_pw_find_active(xl, pw->ifname) == NULL) {
1780 			switch (ldpd_process) {
1781 			case PROC_LDE_ENGINE:
1782 				l2vpn_pw_exit(pw);
1783 				break;
1784 			case PROC_LDP_ENGINE:
1785 				ldpe_l2vpn_pw_exit(pw);
1786 				break;
1787 			case PROC_MAIN:
1788 				break;
1789 			}
1790 
1791 			RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw);
1792 			free(pw);
1793 		}
1794 	}
1795 	RB_FOREACH_SAFE(xp, l2vpn_pw_head, &xl->pw_tree, ptmp) {
1796 		/* find new active pseudowires */
1797 		if ((pw = l2vpn_pw_find_active(l2vpn, xp->ifname)) == NULL) {
1798 			COPY(pw, xp);
1799 			RB_INSERT(l2vpn_pw_head, &l2vpn->pw_tree, pw);
1800 			pw->l2vpn = l2vpn;
1801 
1802 			switch (ldpd_process) {
1803 			case PROC_LDE_ENGINE:
1804 				l2vpn_pw_init(pw);
1805 				break;
1806 			case PROC_LDP_ENGINE:
1807 				ldpe_l2vpn_pw_init(pw);
1808 				break;
1809 			case PROC_MAIN:
1810 				kif_redistribute(pw->ifname);
1811 				break;
1812 			}
1813 			continue;
1814 		}
1815 
1816 		/* update existing active pseudowire */
1817     		if (pw->af != xp->af ||
1818 		    ldp_addrcmp(pw->af, &pw->addr, &xp->addr))
1819 			reinstall_tnbr = 1;
1820 		else
1821 			reinstall_tnbr = 0;
1822 
1823 		/* changes that require a session restart */
1824 		if ((pw->flags & (F_PW_STATUSTLV_CONF|F_PW_CWORD_CONF)) !=
1825 		    (xp->flags & (F_PW_STATUSTLV_CONF|F_PW_CWORD_CONF)))
1826 			reset_nbr = 1;
1827 		else
1828 			reset_nbr = 0;
1829 
1830 		if (l2vpn->pw_type != xl->pw_type || l2vpn->mtu != xl->mtu ||
1831 		    pw->pwid != xp->pwid || reinstall_tnbr || reset_nbr ||
1832 		    pw->lsr_id.s_addr != xp->lsr_id.s_addr)
1833 			reinstall_pwfec = 1;
1834 		else
1835 			reinstall_pwfec = 0;
1836 
1837 		if (ldpd_process == PROC_LDP_ENGINE) {
1838 			if (reinstall_tnbr)
1839 				ldpe_l2vpn_pw_exit(pw);
1840 			if (reset_nbr) {
1841 				nbr = nbr_find_ldpid(pw->lsr_id.s_addr);
1842 				if (nbr && nbr->state == NBR_STA_OPER)
1843 					session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1844 			}
1845 		}
1846 		if (ldpd_process == PROC_LDE_ENGINE && reinstall_pwfec)
1847 			l2vpn_pw_exit(pw);
1848 		pw->lsr_id = xp->lsr_id;
1849 		pw->af = xp->af;
1850 		pw->addr = xp->addr;
1851 		pw->pwid = xp->pwid;
1852 		strlcpy(pw->ifname, xp->ifname, sizeof(pw->ifname));
1853 		pw->ifindex = xp->ifindex;
1854 		if (xp->flags & F_PW_CWORD_CONF)
1855 			pw->flags |= F_PW_CWORD_CONF;
1856 		else
1857 			pw->flags &= ~F_PW_CWORD_CONF;
1858 		if (xp->flags & F_PW_STATUSTLV_CONF)
1859 			pw->flags |= F_PW_STATUSTLV_CONF;
1860 		else
1861 			pw->flags &= ~F_PW_STATUSTLV_CONF;
1862 		if (xp->flags & F_PW_STATIC_NBR_ADDR)
1863 			pw->flags |= F_PW_STATIC_NBR_ADDR;
1864 		else
1865 			pw->flags &= ~F_PW_STATIC_NBR_ADDR;
1866 		if (ldpd_process == PROC_LDP_ENGINE && reinstall_tnbr)
1867 			ldpe_l2vpn_pw_init(pw);
1868 		if (ldpd_process == PROC_LDE_ENGINE && reinstall_pwfec) {
1869 			l2vpn->pw_type = xl->pw_type;
1870 			l2vpn->mtu = xl->mtu;
1871 			l2vpn_pw_init(pw);
1872 			l2vpn->pw_type = previous_pw_type;
1873 			l2vpn->mtu = previous_mtu;
1874 		}
1875 	}
1876 
1877 	/* merge inactive pseudowires */
1878 	RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_inactive_tree, ptmp) {
1879 		/* find deleted inactive pseudowires */
1880 		if (l2vpn_pw_find_inactive(xl, pw->ifname) == NULL) {
1881 			RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
1882 			free(pw);
1883 		}
1884 	}
1885 	RB_FOREACH_SAFE(xp, l2vpn_pw_head, &xl->pw_inactive_tree, ptmp) {
1886 		/* find new inactive pseudowires */
1887 		if ((pw = l2vpn_pw_find_inactive(l2vpn, xp->ifname)) == NULL) {
1888 			COPY(pw, xp);
1889 			RB_INSERT(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
1890 			pw->l2vpn = l2vpn;
1891 
1892 			switch (ldpd_process) {
1893 			case PROC_LDE_ENGINE:
1894 			case PROC_LDP_ENGINE:
1895 				break;
1896 			case PROC_MAIN:
1897 				kif_redistribute(pw->ifname);
1898 				break;
1899 			}
1900 			continue;
1901 		}
1902 
1903 		/* update existing inactive pseudowire */
1904 		pw->lsr_id.s_addr = xp->lsr_id.s_addr;
1905 		pw->af = xp->af;
1906 		pw->addr = xp->addr;
1907 		pw->pwid = xp->pwid;
1908 		strlcpy(pw->ifname, xp->ifname, sizeof(pw->ifname));
1909 		pw->ifindex = xp->ifindex;
1910 		pw->flags = xp->flags;
1911 	}
1912 
1913 	l2vpn->pw_type = xl->pw_type;
1914 	l2vpn->mtu = xl->mtu;
1915 	strlcpy(l2vpn->br_ifname, xl->br_ifname, sizeof(l2vpn->br_ifname));
1916 	l2vpn->br_ifindex = xl->br_ifindex;
1917 }
1918 
1919 struct ldpd_conf *
config_new_empty(void)1920 config_new_empty(void)
1921 {
1922 	struct ldpd_conf	*xconf;
1923 
1924 	xconf = calloc(1, sizeof(*xconf));
1925 	if (xconf == NULL)
1926 		fatal(NULL);
1927 
1928 	RB_INIT(iface_head, &xconf->iface_tree);
1929 	RB_INIT(tnbr_head, &xconf->tnbr_tree);
1930 	RB_INIT(nbrp_head, &xconf->nbrp_tree);
1931 	RB_INIT(l2vpn_head, &xconf->l2vpn_tree);
1932 
1933 	/* set default values */
1934 	ldp_config_reset(xconf);
1935 
1936 	return (xconf);
1937 }
1938 
1939 void
config_clear(struct ldpd_conf * conf)1940 config_clear(struct ldpd_conf *conf)
1941 {
1942 	struct ldpd_conf	*xconf;
1943 
1944 	/*
1945 	 * Merge current config with an empty config, this will deactivate
1946 	 * and deallocate all the interfaces, pseudowires and so on. Before
1947 	 * merging, copy the router-id and other variables to avoid some
1948 	 * unnecessary operations, like trying to reset the neighborships.
1949 	 */
1950 	xconf = config_new_empty();
1951 	xconf->ipv4 = conf->ipv4;
1952 	xconf->ipv6 = conf->ipv6;
1953 	xconf->rtr_id = conf->rtr_id;
1954 	xconf->trans_pref = conf->trans_pref;
1955 	xconf->flags = conf->flags;
1956 	merge_config(conf, xconf);
1957 	free(xconf);
1958 	free(conf);
1959 }
1960