]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/ldpd.c
*: integrate remaining daemons with filter
[mirror_frr.git] / ldpd / ldpd.c
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
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
149 sigint(void)
150 {
151 log_info("SIGINT received");
152 ldpd_shutdown();
153 }
154
155 /* SIGUSR1 handler. */
156 static 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
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
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
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
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 switch (pid = fork()) {
499 case -1:
500 fatal("cannot fork");
501 case 0:
502 break;
503 default:
504 close(fd_async);
505 close(fd_sync);
506 return (pid);
507 }
508
509 nullfd = open("/dev/null", O_RDONLY | O_NOCTTY);
510 if (nullfd == -1) {
511 flog_err_sys(EC_LIB_SYSTEM_CALL,
512 "%s: failed to open /dev/null: %s", __func__,
513 safe_strerror(errno));
514 } else {
515 dup2(nullfd, 0);
516 dup2(nullfd, 1);
517 dup2(nullfd, 2);
518 close(nullfd);
519 }
520
521 if (dup2(fd_async, LDPD_FD_ASYNC) == -1)
522 fatal("cannot setup imsg async fd");
523 if (dup2(fd_sync, LDPD_FD_SYNC) == -1)
524 fatal("cannot setup imsg sync fd");
525
526 argv[argc++] = argv0;
527 switch (p) {
528 case PROC_MAIN:
529 fatalx("Can not start main process");
530 case PROC_LDE_ENGINE:
531 argv[argc++] = (char *)"-L";
532 break;
533 case PROC_LDP_ENGINE:
534 argv[argc++] = (char *)"-E";
535 break;
536 }
537
538 argv[argc++] = (char *)"-u";
539 argv[argc++] = (char *)ldpd_privs.user;
540 argv[argc++] = (char *)"-g";
541 argv[argc++] = (char *)ldpd_privs.group;
542 argv[argc++] = NULL;
543
544 execvp(argv0, argv);
545 fatal("execvp");
546 }
547
548 /* imsg handling */
549 /* ARGSUSED */
550 static int
551 main_dispatch_ldpe(struct thread *thread)
552 {
553 struct imsgev *iev = THREAD_ARG(thread);
554 struct imsgbuf *ibuf = &iev->ibuf;
555 struct imsg imsg;
556 int af;
557 ssize_t n;
558 int shut = 0;
559
560 iev->ev_read = NULL;
561
562 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
563 fatal("imsg_read error");
564 if (n == 0) /* connection closed */
565 shut = 1;
566
567 for (;;) {
568 if ((n = imsg_get(ibuf, &imsg)) == -1)
569 fatal("imsg_get");
570
571 if (n == 0)
572 break;
573
574 switch (imsg.hdr.type) {
575 case IMSG_LOG:
576 logit(imsg.hdr.pid, "%s", (const char *)imsg.data);
577 break;
578 case IMSG_REQUEST_SOCKETS:
579 af = imsg.hdr.pid;
580 main_imsg_send_net_sockets(af);
581 break;
582 case IMSG_ACL_CHECK:
583 if (imsg.hdr.len != IMSG_HEADER_SIZE +
584 sizeof(struct acl_check))
585 fatalx("IMSG_ACL_CHECK imsg with wrong len");
586 ldp_acl_reply(iev, (struct acl_check *)imsg.data);
587 break;
588 default:
589 log_debug("%s: error handling imsg %d", __func__,
590 imsg.hdr.type);
591 break;
592 }
593 imsg_free(&imsg);
594 }
595 if (!shut)
596 imsg_event_add(iev);
597 else {
598 /* this pipe is dead, so remove the event handlers and exit */
599 THREAD_READ_OFF(iev->ev_read);
600 THREAD_WRITE_OFF(iev->ev_write);
601 ldpe_pid = 0;
602 if (lde_pid == 0)
603 ldpd_shutdown();
604 else
605 kill(lde_pid, SIGTERM);
606 }
607
608 return (0);
609 }
610
611 /* ARGSUSED */
612 static int
613 main_dispatch_lde(struct thread *thread)
614 {
615 struct imsgev *iev = THREAD_ARG(thread);
616 struct imsgbuf *ibuf = &iev->ibuf;
617 struct imsg imsg;
618 ssize_t n;
619 int shut = 0;
620
621 iev->ev_read = NULL;
622
623 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
624 fatal("imsg_read error");
625 if (n == 0) /* connection closed */
626 shut = 1;
627
628 for (;;) {
629 if ((n = imsg_get(ibuf, &imsg)) == -1)
630 fatal("imsg_get");
631
632 if (n == 0)
633 break;
634
635 switch (imsg.hdr.type) {
636 case IMSG_LOG:
637 logit(imsg.hdr.pid, "%s", (const char *)imsg.data);
638 break;
639 case IMSG_KLABEL_CHANGE:
640 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
641 sizeof(struct kroute))
642 fatalx("invalid size of IMSG_KLABEL_CHANGE");
643 if (kr_change(imsg.data))
644 log_warnx("%s: error changing route", __func__);
645 break;
646 case IMSG_KLABEL_DELETE:
647 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
648 sizeof(struct kroute))
649 fatalx("invalid size of IMSG_KLABEL_DELETE");
650 if (kr_delete(imsg.data))
651 log_warnx("%s: error deleting route", __func__);
652 break;
653 case IMSG_KPW_ADD:
654 case IMSG_KPW_DELETE:
655 case IMSG_KPW_SET:
656 case IMSG_KPW_UNSET:
657 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
658 sizeof(struct zapi_pw))
659 fatalx("invalid size of IMSG_KPWLABEL_CHANGE");
660
661 switch (imsg.hdr.type) {
662 case IMSG_KPW_ADD:
663 if (kmpw_add(imsg.data))
664 log_warnx("%s: error adding "
665 "pseudowire", __func__);
666 break;
667 case IMSG_KPW_DELETE:
668 if (kmpw_del(imsg.data))
669 log_warnx("%s: error deleting "
670 "pseudowire", __func__);
671 break;
672 case IMSG_KPW_SET:
673 if (kmpw_set(imsg.data))
674 log_warnx("%s: error setting "
675 "pseudowire", __func__);
676 break;
677 case IMSG_KPW_UNSET:
678 if (kmpw_unset(imsg.data))
679 log_warnx("%s: error unsetting "
680 "pseudowire", __func__);
681 break;
682 }
683 break;
684 case IMSG_ACL_CHECK:
685 if (imsg.hdr.len != IMSG_HEADER_SIZE +
686 sizeof(struct acl_check))
687 fatalx("IMSG_ACL_CHECK imsg with wrong len");
688 ldp_acl_reply(iev, (struct acl_check *)imsg.data);
689 break;
690 default:
691 log_debug("%s: error handling imsg %d", __func__,
692 imsg.hdr.type);
693 break;
694 }
695 imsg_free(&imsg);
696 }
697 if (!shut)
698 imsg_event_add(iev);
699 else {
700 /* this pipe is dead, so remove the event handlers and exit */
701 THREAD_READ_OFF(iev->ev_read);
702 THREAD_WRITE_OFF(iev->ev_write);
703 lde_pid = 0;
704 if (ldpe_pid == 0)
705 ldpd_shutdown();
706 else
707 kill(ldpe_pid, SIGTERM);
708 }
709
710 return (0);
711 }
712
713 /* ARGSUSED */
714 int
715 ldp_write_handler(struct thread *thread)
716 {
717 struct imsgev *iev = THREAD_ARG(thread);
718 struct imsgbuf *ibuf = &iev->ibuf;
719 ssize_t n;
720
721 iev->ev_write = NULL;
722
723 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
724 fatal("msgbuf_write");
725 if (n == 0) {
726 /* this pipe is dead, so remove the event handlers */
727 THREAD_READ_OFF(iev->ev_read);
728 THREAD_WRITE_OFF(iev->ev_write);
729 return (0);
730 }
731
732 imsg_event_add(iev);
733
734 return (0);
735 }
736
737 void
738 main_imsg_compose_ldpe(int type, pid_t pid, void *data, uint16_t datalen)
739 {
740 if (iev_ldpe == NULL)
741 return;
742 imsg_compose_event(iev_ldpe, type, 0, pid, -1, data, datalen);
743 }
744
745 void
746 main_imsg_compose_lde(int type, pid_t pid, void *data, uint16_t datalen)
747 {
748 imsg_compose_event(iev_lde, type, 0, pid, -1, data, datalen);
749 }
750
751 int
752 main_imsg_compose_both(enum imsg_type type, void *buf, uint16_t len)
753 {
754 if (iev_ldpe == NULL || iev_lde == NULL)
755 return (0);
756 if (imsg_compose_event(iev_ldpe, type, 0, 0, -1, buf, len) == -1)
757 return (-1);
758 if (imsg_compose_event(iev_lde, type, 0, 0, -1, buf, len) == -1)
759 return (-1);
760 return (0);
761 }
762
763 void
764 imsg_event_add(struct imsgev *iev)
765 {
766 if (iev->handler_read)
767 thread_add_read(master, iev->handler_read, iev, iev->ibuf.fd,
768 &iev->ev_read);
769
770 if (iev->handler_write && iev->ibuf.w.queued)
771 thread_add_write(master, iev->handler_write, iev,
772 iev->ibuf.fd, &iev->ev_write);
773 }
774
775 int
776 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
777 pid_t pid, int fd, void *data, uint16_t datalen)
778 {
779 int ret;
780
781 if ((ret = imsg_compose(&iev->ibuf, type, peerid,
782 pid, fd, data, datalen)) != -1)
783 imsg_event_add(iev);
784 return (ret);
785 }
786
787 void
788 evbuf_enqueue(struct evbuf *eb, struct ibuf *buf)
789 {
790 ibuf_close(&eb->wbuf, buf);
791 evbuf_event_add(eb);
792 }
793
794 void
795 evbuf_event_add(struct evbuf *eb)
796 {
797 if (eb->wbuf.queued)
798 thread_add_write(master, eb->handler, eb->arg, eb->wbuf.fd,
799 &eb->ev);
800 }
801
802 void
803 evbuf_init(struct evbuf *eb, int fd, int (*handler)(struct thread *),
804 void *arg)
805 {
806 msgbuf_init(&eb->wbuf);
807 eb->wbuf.fd = fd;
808 eb->handler = handler;
809 eb->arg = arg;
810 }
811
812 void
813 evbuf_clear(struct evbuf *eb)
814 {
815 THREAD_WRITE_OFF(eb->ev);
816 msgbuf_clear(&eb->wbuf);
817 eb->wbuf.fd = -1;
818 }
819
820 static int
821 main_imsg_send_ipc_sockets(struct imsgbuf *ldpe_buf, struct imsgbuf *lde_buf)
822 {
823 int pipe_ldpe2lde[2];
824
825 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_ldpe2lde) == -1)
826 return (-1);
827 sock_set_nonblock(pipe_ldpe2lde[0]);
828 sock_set_nonblock(pipe_ldpe2lde[1]);
829
830 if (imsg_compose(ldpe_buf, IMSG_SOCKET_IPC, 0, 0, pipe_ldpe2lde[0],
831 NULL, 0) == -1)
832 return (-1);
833 if (imsg_compose(lde_buf, IMSG_SOCKET_IPC, 0, 0, pipe_ldpe2lde[1],
834 NULL, 0) == -1)
835 return (-1);
836
837 return (0);
838 }
839
840 static void
841 main_imsg_send_net_sockets(int af)
842 {
843 if (!ldp_addrisset(af, &(ldp_af_conf_get(ldpd_conf, af))->trans_addr))
844 return;
845
846 main_imsg_send_net_socket(af, LDP_SOCKET_DISC);
847 main_imsg_send_net_socket(af, LDP_SOCKET_EDISC);
848 main_imsg_send_net_socket(af, LDP_SOCKET_SESSION);
849 imsg_compose_event(iev_ldpe, IMSG_SETUP_SOCKETS, af, 0, -1, NULL, 0);
850 }
851
852 static void
853 main_imsg_send_net_socket(int af, enum socket_type type)
854 {
855 int fd;
856
857 fd = ldp_create_socket(af, type);
858 if (fd == -1) {
859 log_warnx("%s: failed to create %s socket for address-family "
860 "%s", __func__, socket_name(type), af_name(af));
861 return;
862 }
863
864 imsg_compose_event(iev_ldpe, IMSG_SOCKET_NET, af, 0, fd, &type,
865 sizeof(type));
866 }
867
868 int
869 ldp_acl_request(struct imsgev *iev, char *acl_name, int af,
870 union ldpd_addr *addr, uint8_t prefixlen)
871 {
872 struct imsg imsg;
873 struct acl_check acl_check;
874
875 if (acl_name[0] == '\0')
876 return FILTER_PERMIT;
877
878 /* build request */
879 strlcpy(acl_check.acl, acl_name, sizeof(acl_check.acl));
880 acl_check.af = af;
881 acl_check.addr = *addr;
882 acl_check.prefixlen = prefixlen;
883
884 /* send (blocking) */
885 imsg_compose_event(iev, IMSG_ACL_CHECK, 0, 0, -1, &acl_check,
886 sizeof(acl_check));
887 imsg_flush(&iev->ibuf);
888
889 /* receive (blocking) and parse result */
890 if (imsg_read(&iev->ibuf) == -1)
891 fatal("imsg_read error");
892 if (imsg_get(&iev->ibuf, &imsg) == -1)
893 fatal("imsg_get");
894 if (imsg.hdr.type != IMSG_ACL_CHECK ||
895 imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(int))
896 fatalx("ldp_acl_request: invalid response");
897
898 return (*((int *)imsg.data));
899 }
900
901 void
902 ldp_acl_reply(struct imsgev *iev, struct acl_check *acl_check)
903 {
904 struct access_list *alist;
905 struct prefix prefix;
906 int result;
907
908 alist = access_list_lookup(family2afi(acl_check->af), acl_check->acl);
909 if (alist == NULL)
910 result = FILTER_DENY;
911 else {
912 prefix.family = acl_check->af;
913 switch (prefix.family) {
914 case AF_INET:
915 prefix.u.prefix4 = acl_check->addr.v4;
916 break;
917 case AF_INET6:
918 prefix.u.prefix6 = acl_check->addr.v6;
919 break;
920 default:
921 fatalx("ldp_acl_reply: unknown af");
922 }
923 prefix.prefixlen = acl_check->prefixlen;
924 result = access_list_apply(alist, &prefix);
925 }
926
927 imsg_compose_event(iev, IMSG_ACL_CHECK, 0, 0, -1, &result,
928 sizeof(result));
929 }
930
931 struct ldpd_af_conf *
932 ldp_af_conf_get(struct ldpd_conf *xconf, int af)
933 {
934 switch (af) {
935 case AF_INET:
936 return (&xconf->ipv4);
937 case AF_INET6:
938 return (&xconf->ipv6);
939 default:
940 fatalx("ldp_af_conf_get: unknown af");
941 }
942 }
943
944 struct ldpd_af_global *
945 ldp_af_global_get(struct ldpd_global *xglobal, int af)
946 {
947 switch (af) {
948 case AF_INET:
949 return (&xglobal->ipv4);
950 case AF_INET6:
951 return (&xglobal->ipv6);
952 default:
953 fatalx("ldp_af_global_get: unknown af");
954 }
955 }
956
957 int
958 ldp_is_dual_stack(struct ldpd_conf *xconf)
959 {
960 return ((xconf->ipv4.flags & F_LDPD_AF_ENABLED) &&
961 (xconf->ipv6.flags & F_LDPD_AF_ENABLED));
962 }
963
964 in_addr_t
965 ldp_rtr_id_get(struct ldpd_conf *xconf)
966 {
967 if (xconf->rtr_id.s_addr != INADDR_ANY)
968 return (xconf->rtr_id.s_addr);
969 else
970 return (global.rtr_id.s_addr);
971 }
972
973 static int
974 main_imsg_send_config(struct ldpd_conf *xconf)
975 {
976 struct iface *iface;
977 struct tnbr *tnbr;
978 struct nbr_params *nbrp;
979 struct l2vpn *l2vpn;
980 struct l2vpn_if *lif;
981 struct l2vpn_pw *pw;
982
983 if (main_imsg_compose_both(IMSG_RECONF_CONF, xconf,
984 sizeof(*xconf)) == -1)
985 return (-1);
986
987 RB_FOREACH(iface, iface_head, &xconf->iface_tree) {
988 if (main_imsg_compose_both(IMSG_RECONF_IFACE, iface,
989 sizeof(*iface)) == -1)
990 return (-1);
991 }
992
993 RB_FOREACH(tnbr, tnbr_head, &xconf->tnbr_tree) {
994 if (main_imsg_compose_both(IMSG_RECONF_TNBR, tnbr,
995 sizeof(*tnbr)) == -1)
996 return (-1);
997 }
998
999 RB_FOREACH(nbrp, nbrp_head, &xconf->nbrp_tree) {
1000 if (main_imsg_compose_both(IMSG_RECONF_NBRP, nbrp,
1001 sizeof(*nbrp)) == -1)
1002 return (-1);
1003 }
1004
1005 RB_FOREACH(l2vpn, l2vpn_head, &xconf->l2vpn_tree) {
1006 if (main_imsg_compose_both(IMSG_RECONF_L2VPN, l2vpn,
1007 sizeof(*l2vpn)) == -1)
1008 return (-1);
1009
1010 RB_FOREACH(lif, l2vpn_if_head, &l2vpn->if_tree) {
1011 if (main_imsg_compose_both(IMSG_RECONF_L2VPN_IF, lif,
1012 sizeof(*lif)) == -1)
1013 return (-1);
1014 }
1015 RB_FOREACH(pw, l2vpn_pw_head, &l2vpn->pw_tree) {
1016 if (main_imsg_compose_both(IMSG_RECONF_L2VPN_PW, pw,
1017 sizeof(*pw)) == -1)
1018 return (-1);
1019 }
1020 RB_FOREACH(pw, l2vpn_pw_head, &l2vpn->pw_inactive_tree) {
1021 if (main_imsg_compose_both(IMSG_RECONF_L2VPN_IPW, pw,
1022 sizeof(*pw)) == -1)
1023 return (-1);
1024 }
1025 }
1026
1027 if (main_imsg_compose_both(IMSG_RECONF_END, NULL, 0) == -1)
1028 return (-1);
1029
1030 return (0);
1031 }
1032
1033 int
1034 ldp_config_apply(struct vty *vty, struct ldpd_conf *xconf)
1035 {
1036 /*
1037 * When reading from a configuration file (startup and sighup), we
1038 * call merge_config() only once after the whole config has been read.
1039 * This is the optimal and least disruptive way to update the running
1040 * configuration.
1041 */
1042 if (vty && vty->type == VTY_FILE)
1043 return (0);
1044
1045 ldp_config_normalize(xconf);
1046
1047 if (main_imsg_send_config(xconf) == -1)
1048 return (-1);
1049
1050 merge_config(ldpd_conf, xconf);
1051
1052 return (0);
1053 }
1054
1055 static void
1056 ldp_config_normalize(struct ldpd_conf *xconf)
1057 {
1058 struct iface *iface, *itmp;
1059 struct nbr_params *nbrp, *ntmp;
1060 struct l2vpn *l2vpn;
1061 struct l2vpn_pw *pw, *ptmp;
1062
1063 if (!(xconf->flags & F_LDPD_ENABLED))
1064 ldp_config_reset_main(xconf);
1065 else {
1066 if (!(xconf->ipv4.flags & F_LDPD_AF_ENABLED))
1067 ldp_config_reset_af(xconf, AF_INET);
1068 if (!(xconf->ipv6.flags & F_LDPD_AF_ENABLED))
1069 ldp_config_reset_af(xconf, AF_INET6);
1070
1071 RB_FOREACH_SAFE(iface, iface_head, &xconf->iface_tree, itmp) {
1072 if (iface->ipv4.enabled || iface->ipv6.enabled)
1073 continue;
1074
1075 QOBJ_UNREG(iface);
1076 RB_REMOVE(iface_head, &vty_conf->iface_tree, iface);
1077 free(iface);
1078 }
1079
1080 RB_FOREACH_SAFE(nbrp, nbrp_head, &xconf->nbrp_tree, ntmp) {
1081 if (nbrp->flags & (F_NBRP_KEEPALIVE|F_NBRP_GTSM))
1082 continue;
1083 if (nbrp->auth.method != AUTH_NONE)
1084 continue;
1085
1086 QOBJ_UNREG(nbrp);
1087 RB_REMOVE(nbrp_head, &vty_conf->nbrp_tree, nbrp);
1088 free(nbrp);
1089 }
1090 }
1091
1092 RB_FOREACH(l2vpn, l2vpn_head, &xconf->l2vpn_tree) {
1093 RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_tree, ptmp) {
1094 if (!(pw->flags & F_PW_STATIC_NBR_ADDR)) {
1095 pw->af = AF_INET;
1096 pw->addr.v4 = pw->lsr_id;
1097 }
1098
1099 if (pw->lsr_id.s_addr != INADDR_ANY && pw->pwid != 0)
1100 continue;
1101 RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw);
1102 RB_INSERT(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
1103 }
1104 RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_inactive_tree,
1105 ptmp) {
1106 if (!(pw->flags & F_PW_STATIC_NBR_ADDR)) {
1107 pw->af = AF_INET;
1108 pw->addr.v4 = pw->lsr_id;
1109 }
1110
1111 if (pw->lsr_id.s_addr == INADDR_ANY || pw->pwid == 0)
1112 continue;
1113 RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
1114 RB_INSERT(l2vpn_pw_head, &l2vpn->pw_tree, pw);
1115 }
1116 }
1117 }
1118
1119 static void
1120 ldp_config_reset(struct ldpd_conf *conf)
1121 {
1122 ldp_config_reset_main(conf);
1123 ldp_config_reset_l2vpns(conf);
1124 }
1125
1126 static void
1127 ldp_config_reset_main(struct ldpd_conf *conf)
1128 {
1129 struct iface *iface;
1130 struct nbr_params *nbrp;
1131
1132 while (!RB_EMPTY(iface_head, &conf->iface_tree)) {
1133 iface = RB_ROOT(iface_head, &conf->iface_tree);
1134
1135 QOBJ_UNREG(iface);
1136 RB_REMOVE(iface_head, &conf->iface_tree, iface);
1137 free(iface);
1138 }
1139
1140 while (!RB_EMPTY(nbrp_head, &conf->nbrp_tree)) {
1141 nbrp = RB_ROOT(nbrp_head, &conf->nbrp_tree);
1142
1143 QOBJ_UNREG(nbrp);
1144 RB_REMOVE(nbrp_head, &conf->nbrp_tree, nbrp);
1145 free(nbrp);
1146 }
1147
1148 conf->rtr_id.s_addr = INADDR_ANY;
1149 ldp_config_reset_af(conf, AF_INET);
1150 ldp_config_reset_af(conf, AF_INET6);
1151 conf->lhello_holdtime = LINK_DFLT_HOLDTIME;
1152 conf->lhello_interval = DEFAULT_HELLO_INTERVAL;
1153 conf->thello_holdtime = TARGETED_DFLT_HOLDTIME;
1154 conf->thello_interval = DEFAULT_HELLO_INTERVAL;
1155 conf->trans_pref = DUAL_STACK_LDPOV6;
1156 conf->flags = 0;
1157 }
1158
1159 static void
1160 ldp_config_reset_af(struct ldpd_conf *conf, int af)
1161 {
1162 struct ldpd_af_conf *af_conf;
1163 struct iface *iface;
1164 struct iface_af *ia;
1165 struct tnbr *tnbr, *ttmp;
1166
1167 RB_FOREACH(iface, iface_head, &conf->iface_tree) {
1168 ia = iface_af_get(iface, af);
1169 ia->enabled = 0;
1170 }
1171
1172 RB_FOREACH_SAFE(tnbr, tnbr_head, &conf->tnbr_tree, ttmp) {
1173 if (tnbr->af != af)
1174 continue;
1175
1176 QOBJ_UNREG(tnbr);
1177 RB_REMOVE(tnbr_head, &conf->tnbr_tree, tnbr);
1178 free(tnbr);
1179 }
1180
1181 af_conf = ldp_af_conf_get(conf, af);
1182 af_conf->keepalive = 180;
1183 af_conf->lhello_holdtime = 0;
1184 af_conf->lhello_interval = 0;
1185 af_conf->thello_holdtime = 0;
1186 af_conf->thello_interval = 0;
1187 memset(&af_conf->trans_addr, 0, sizeof(af_conf->trans_addr));
1188 af_conf->flags = 0;
1189 }
1190
1191 static void
1192 ldp_config_reset_l2vpns(struct ldpd_conf *conf)
1193 {
1194 struct l2vpn *l2vpn;
1195 struct l2vpn_if *lif;
1196 struct l2vpn_pw *pw;
1197
1198 while (!RB_EMPTY(l2vpn_head, &conf->l2vpn_tree)) {
1199 l2vpn = RB_ROOT(l2vpn_head, &conf->l2vpn_tree);
1200 while (!RB_EMPTY(l2vpn_if_head, &l2vpn->if_tree)) {
1201 lif = RB_ROOT(l2vpn_if_head, &l2vpn->if_tree);
1202
1203 QOBJ_UNREG(lif);
1204 RB_REMOVE(l2vpn_if_head, &l2vpn->if_tree, lif);
1205 free(lif);
1206 }
1207 while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_tree)) {
1208 pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_tree);
1209
1210 QOBJ_UNREG(pw);
1211 RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw);
1212 free(pw);
1213 }
1214 while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_inactive_tree)) {
1215 pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_inactive_tree);
1216
1217 QOBJ_UNREG(pw);
1218 RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
1219 free(pw);
1220 }
1221 QOBJ_UNREG(l2vpn);
1222 RB_REMOVE(l2vpn_head, &conf->l2vpn_tree, l2vpn);
1223 free(l2vpn);
1224 }
1225 }
1226
1227 void
1228 ldp_clear_config(struct ldpd_conf *xconf)
1229 {
1230 struct iface *iface;
1231 struct tnbr *tnbr;
1232 struct nbr_params *nbrp;
1233 struct l2vpn *l2vpn;
1234
1235 while (!RB_EMPTY(iface_head, &xconf->iface_tree)) {
1236 iface = RB_ROOT(iface_head, &xconf->iface_tree);
1237
1238 RB_REMOVE(iface_head, &xconf->iface_tree, iface);
1239 free(iface);
1240 }
1241 while (!RB_EMPTY(tnbr_head, &xconf->tnbr_tree)) {
1242 tnbr = RB_ROOT(tnbr_head, &xconf->tnbr_tree);
1243
1244 RB_REMOVE(tnbr_head, &xconf->tnbr_tree, tnbr);
1245 free(tnbr);
1246 }
1247 while (!RB_EMPTY(nbrp_head, &xconf->nbrp_tree)) {
1248 nbrp = RB_ROOT(nbrp_head, &xconf->nbrp_tree);
1249
1250 RB_REMOVE(nbrp_head, &xconf->nbrp_tree, nbrp);
1251 free(nbrp);
1252 }
1253 while (!RB_EMPTY(l2vpn_head, &xconf->l2vpn_tree)) {
1254 l2vpn = RB_ROOT(l2vpn_head, &xconf->l2vpn_tree);
1255
1256 RB_REMOVE(l2vpn_head, &xconf->l2vpn_tree, l2vpn);
1257 l2vpn_del(l2vpn);
1258 }
1259
1260 free(xconf);
1261 }
1262
1263 #define COPY(a, b) do { \
1264 a = malloc(sizeof(*a)); \
1265 if (a == NULL) \
1266 fatal(__func__); \
1267 *a = *b; \
1268 } while (0)
1269
1270 void
1271 merge_config(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1272 {
1273 merge_global(conf, xconf);
1274 merge_af(AF_INET, &conf->ipv4, &xconf->ipv4);
1275 merge_af(AF_INET6, &conf->ipv6, &xconf->ipv6);
1276 merge_ifaces(conf, xconf);
1277 merge_tnbrs(conf, xconf);
1278 merge_nbrps(conf, xconf);
1279 merge_l2vpns(conf, xconf);
1280 }
1281
1282 static void
1283 merge_global(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1284 {
1285 /* change of router-id requires resetting all neighborships */
1286 if (conf->rtr_id.s_addr != xconf->rtr_id.s_addr) {
1287 if (ldpd_process == PROC_LDP_ENGINE) {
1288 ldpe_reset_nbrs(AF_UNSPEC);
1289 if (conf->rtr_id.s_addr == INADDR_ANY ||
1290 xconf->rtr_id.s_addr == INADDR_ANY) {
1291 if_update_all(AF_UNSPEC);
1292 tnbr_update_all(AF_UNSPEC);
1293 }
1294 }
1295 conf->rtr_id = xconf->rtr_id;
1296 }
1297
1298 /*
1299 * Configuration of ordered-control or independent-control
1300 * requires resetting all neighborships.
1301 */
1302 if ((conf->flags & F_LDPD_ORDERED_CONTROL) !=
1303 (xconf->flags & F_LDPD_ORDERED_CONTROL))
1304 ldpe_reset_nbrs(AF_UNSPEC);
1305
1306 conf->lhello_holdtime = xconf->lhello_holdtime;
1307 conf->lhello_interval = xconf->lhello_interval;
1308 conf->thello_holdtime = xconf->thello_holdtime;
1309 conf->thello_interval = xconf->thello_interval;
1310
1311 if (conf->trans_pref != xconf->trans_pref) {
1312 if (ldpd_process == PROC_LDP_ENGINE)
1313 ldpe_reset_ds_nbrs();
1314 conf->trans_pref = xconf->trans_pref;
1315 }
1316
1317 if ((conf->flags & F_LDPD_DS_CISCO_INTEROP) !=
1318 (xconf->flags & F_LDPD_DS_CISCO_INTEROP)) {
1319 if (ldpd_process == PROC_LDP_ENGINE)
1320 ldpe_reset_ds_nbrs();
1321 }
1322
1323 conf->flags = xconf->flags;
1324 }
1325
1326 static void
1327 merge_af(int af, struct ldpd_af_conf *af_conf, struct ldpd_af_conf *xa)
1328 {
1329 int stop_init_backoff = 0;
1330 int remove_dynamic_tnbrs = 0;
1331 int change_egress_label = 0;
1332 int change_host_label = 0;
1333 int reset_nbrs_ipv4 = 0;
1334 int reset_nbrs = 0;
1335 int update_sockets = 0;
1336
1337 /* update timers */
1338 if (af_conf->keepalive != xa->keepalive) {
1339 af_conf->keepalive = xa->keepalive;
1340 stop_init_backoff = 1;
1341 }
1342 af_conf->lhello_holdtime = xa->lhello_holdtime;
1343 af_conf->lhello_interval = xa->lhello_interval;
1344 af_conf->thello_holdtime = xa->thello_holdtime;
1345 af_conf->thello_interval = xa->thello_interval;
1346
1347 /* update flags */
1348 if ((af_conf->flags & F_LDPD_AF_THELLO_ACCEPT) &&
1349 !(xa->flags & F_LDPD_AF_THELLO_ACCEPT))
1350 remove_dynamic_tnbrs = 1;
1351 if ((af_conf->flags & F_LDPD_AF_NO_GTSM) !=
1352 (xa->flags & F_LDPD_AF_NO_GTSM)) {
1353 if (af == AF_INET6)
1354 /* need to set/unset IPV6_MINHOPCOUNT */
1355 update_sockets = 1;
1356 else
1357 /* for LDPv4 just resetting the neighbors is enough */
1358 reset_nbrs_ipv4 = 1;
1359 }
1360 if ((af_conf->flags & F_LDPD_AF_EXPNULL) !=
1361 (xa->flags & F_LDPD_AF_EXPNULL))
1362 change_egress_label = 1;
1363
1364 /* changing config of host only fec filtering */
1365 if ((af_conf->flags & F_LDPD_AF_ALLOCHOSTONLY)
1366 != (xa->flags & F_LDPD_AF_ALLOCHOSTONLY))
1367 change_host_label = 1;
1368
1369 af_conf->flags = xa->flags;
1370
1371 /* update the transport address */
1372 if (ldp_addrcmp(af, &af_conf->trans_addr, &xa->trans_addr)) {
1373 af_conf->trans_addr = xa->trans_addr;
1374 update_sockets = 1;
1375 }
1376
1377 /* update ACLs */
1378 if (strcmp(af_conf->acl_label_allocate_for, xa->acl_label_allocate_for))
1379 change_host_label = 1;
1380
1381 if (strcmp(af_conf->acl_label_advertise_to,
1382 xa->acl_label_advertise_to) ||
1383 strcmp(af_conf->acl_label_advertise_for,
1384 xa->acl_label_advertise_for) ||
1385 strcmp(af_conf->acl_label_accept_from,
1386 xa->acl_label_accept_from) ||
1387 strcmp(af_conf->acl_label_accept_for,
1388 xa->acl_label_accept_for))
1389 reset_nbrs = 1;
1390 if (strcmp(af_conf->acl_thello_accept_from, xa->acl_thello_accept_from))
1391 remove_dynamic_tnbrs = 1;
1392 if (strcmp(af_conf->acl_label_expnull_for, xa->acl_label_expnull_for))
1393 change_egress_label = 1;
1394 strlcpy(af_conf->acl_thello_accept_from, xa->acl_thello_accept_from,
1395 sizeof(af_conf->acl_thello_accept_from));
1396 strlcpy(af_conf->acl_label_allocate_for, xa->acl_label_allocate_for,
1397 sizeof(af_conf->acl_label_allocate_for));
1398 strlcpy(af_conf->acl_label_advertise_to, xa->acl_label_advertise_to,
1399 sizeof(af_conf->acl_label_advertise_to));
1400 strlcpy(af_conf->acl_label_advertise_for, xa->acl_label_advertise_for,
1401 sizeof(af_conf->acl_label_advertise_for));
1402 strlcpy(af_conf->acl_label_accept_from, xa->acl_label_accept_from,
1403 sizeof(af_conf->acl_label_accept_from));
1404 strlcpy(af_conf->acl_label_accept_for, xa->acl_label_accept_for,
1405 sizeof(af_conf->acl_label_accept_for));
1406 strlcpy(af_conf->acl_label_expnull_for, xa->acl_label_expnull_for,
1407 sizeof(af_conf->acl_label_expnull_for));
1408
1409 /* apply the new configuration */
1410 switch (ldpd_process) {
1411 case PROC_LDE_ENGINE:
1412 if (change_egress_label)
1413 lde_change_egress_label(af);
1414 if (change_host_label)
1415 lde_change_allocate_filter(af);
1416 break;
1417 case PROC_LDP_ENGINE:
1418 if (stop_init_backoff)
1419 ldpe_stop_init_backoff(af);
1420 if (remove_dynamic_tnbrs)
1421 ldpe_remove_dynamic_tnbrs(af);
1422 if (reset_nbrs)
1423 ldpe_reset_nbrs(AF_UNSPEC);
1424 else if (reset_nbrs_ipv4)
1425 ldpe_reset_nbrs(AF_INET);
1426 break;
1427 case PROC_MAIN:
1428 if (update_sockets && iev_ldpe)
1429 imsg_compose_event(iev_ldpe, IMSG_CLOSE_SOCKETS, af,
1430 0, -1, NULL, 0);
1431 break;
1432 }
1433 }
1434
1435 static void
1436 merge_ifaces(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1437 {
1438 struct iface *iface, *itmp, *xi;
1439
1440 RB_FOREACH_SAFE(iface, iface_head, &conf->iface_tree, itmp) {
1441 /* find deleted interfaces */
1442 if (if_lookup_name(xconf, iface->name) == NULL) {
1443 switch (ldpd_process) {
1444 case PROC_LDP_ENGINE:
1445 ldpe_if_exit(iface);
1446 break;
1447 case PROC_LDE_ENGINE:
1448 case PROC_MAIN:
1449 break;
1450 }
1451 RB_REMOVE(iface_head, &conf->iface_tree, iface);
1452 free(iface);
1453 }
1454 }
1455 RB_FOREACH_SAFE(xi, iface_head, &xconf->iface_tree, itmp) {
1456 /* find new interfaces */
1457 if ((iface = if_lookup_name(conf, xi->name)) == NULL) {
1458 COPY(iface, xi);
1459 RB_INSERT(iface_head, &conf->iface_tree, iface);
1460
1461 switch (ldpd_process) {
1462 case PROC_LDP_ENGINE:
1463 ldpe_if_init(iface);
1464 break;
1465 case PROC_LDE_ENGINE:
1466 break;
1467 case PROC_MAIN:
1468 /* resend addresses to activate new interfaces */
1469 kif_redistribute(iface->name);
1470 break;
1471 }
1472 continue;
1473 }
1474
1475 /* update existing interfaces */
1476 merge_iface_af(&iface->ipv4, &xi->ipv4);
1477 merge_iface_af(&iface->ipv6, &xi->ipv6);
1478 }
1479 }
1480
1481 static void
1482 merge_iface_af(struct iface_af *ia, struct iface_af *xi)
1483 {
1484 if (ia->enabled != xi->enabled) {
1485 ia->enabled = xi->enabled;
1486 if (ldpd_process == PROC_LDP_ENGINE)
1487 ldp_if_update(ia->iface, ia->af);
1488 }
1489 ia->hello_holdtime = xi->hello_holdtime;
1490 ia->hello_interval = xi->hello_interval;
1491 }
1492
1493 static void
1494 merge_tnbrs(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1495 {
1496 struct tnbr *tnbr, *ttmp, *xt;
1497
1498 RB_FOREACH_SAFE(tnbr, tnbr_head, &conf->tnbr_tree, ttmp) {
1499 if (!(tnbr->flags & F_TNBR_CONFIGURED))
1500 continue;
1501
1502 /* find deleted tnbrs */
1503 if (tnbr_find(xconf, tnbr->af, &tnbr->addr) == NULL) {
1504 switch (ldpd_process) {
1505 case PROC_LDP_ENGINE:
1506 tnbr->flags &= ~F_TNBR_CONFIGURED;
1507 tnbr_check(conf, tnbr);
1508 break;
1509 case PROC_LDE_ENGINE:
1510 case PROC_MAIN:
1511 RB_REMOVE(tnbr_head, &conf->tnbr_tree, tnbr);
1512 free(tnbr);
1513 break;
1514 }
1515 }
1516 }
1517 RB_FOREACH_SAFE(xt, tnbr_head, &xconf->tnbr_tree, ttmp) {
1518 /* find new tnbrs */
1519 if ((tnbr = tnbr_find(conf, xt->af, &xt->addr)) == NULL) {
1520 COPY(tnbr, xt);
1521 RB_INSERT(tnbr_head, &conf->tnbr_tree, tnbr);
1522
1523 switch (ldpd_process) {
1524 case PROC_LDP_ENGINE:
1525 tnbr_update(tnbr);
1526 break;
1527 case PROC_LDE_ENGINE:
1528 case PROC_MAIN:
1529 break;
1530 }
1531 continue;
1532 }
1533
1534 /* update existing tnbrs */
1535 if (!(tnbr->flags & F_TNBR_CONFIGURED))
1536 tnbr->flags |= F_TNBR_CONFIGURED;
1537 }
1538 }
1539
1540 static void
1541 merge_nbrps(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1542 {
1543 struct nbr_params *nbrp, *ntmp, *xn;
1544 struct nbr *nbr;
1545 int nbrp_changed;
1546
1547 RB_FOREACH_SAFE(nbrp, nbrp_head, &conf->nbrp_tree, ntmp) {
1548 /* find deleted nbrps */
1549 if (nbr_params_find(xconf, nbrp->lsr_id) != NULL)
1550 continue;
1551
1552 switch (ldpd_process) {
1553 case PROC_LDP_ENGINE:
1554 nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr);
1555 if (nbr) {
1556 session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1557 #ifdef __OpenBSD__
1558 pfkey_remove(nbr);
1559 #else
1560 sock_set_md5sig(
1561 (ldp_af_global_get(&global, nbr->af))
1562 ->ldp_session_socket,
1563 nbr->af, &nbr->raddr, NULL);
1564 #endif
1565 nbr->auth.method = AUTH_NONE;
1566 if (nbr_session_active_role(nbr))
1567 nbr_establish_connection(nbr);
1568 }
1569 break;
1570 case PROC_LDE_ENGINE:
1571 case PROC_MAIN:
1572 break;
1573 }
1574 RB_REMOVE(nbrp_head, &conf->nbrp_tree, nbrp);
1575 free(nbrp);
1576 }
1577
1578 RB_FOREACH_SAFE(xn, nbrp_head, &xconf->nbrp_tree, ntmp) {
1579 /* find new nbrps */
1580 if ((nbrp = nbr_params_find(conf, xn->lsr_id)) == NULL) {
1581 COPY(nbrp, xn);
1582 RB_INSERT(nbrp_head, &conf->nbrp_tree, nbrp);
1583
1584 switch (ldpd_process) {
1585 case PROC_LDP_ENGINE:
1586 nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr);
1587 if (nbr) {
1588 session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1589 nbr->auth.method = nbrp->auth.method;
1590 #ifdef __OpenBSD__
1591 if (pfkey_establish(nbr, nbrp) == -1)
1592 fatalx("pfkey setup failed");
1593 #else
1594 sock_set_md5sig(
1595 (ldp_af_global_get(&global,
1596 nbr->af))->ldp_session_socket,
1597 nbr->af, &nbr->raddr,
1598 nbrp->auth.md5key);
1599 #endif
1600 if (nbr_session_active_role(nbr))
1601 nbr_establish_connection(nbr);
1602 }
1603 break;
1604 case PROC_LDE_ENGINE:
1605 case PROC_MAIN:
1606 break;
1607 }
1608 continue;
1609 }
1610
1611 /* update existing nbrps */
1612 if (nbrp->flags != xn->flags ||
1613 nbrp->keepalive != xn->keepalive ||
1614 nbrp->gtsm_enabled != xn->gtsm_enabled ||
1615 nbrp->gtsm_hops != xn->gtsm_hops ||
1616 nbrp->auth.method != xn->auth.method ||
1617 strcmp(nbrp->auth.md5key, xn->auth.md5key) != 0)
1618 nbrp_changed = 1;
1619 else
1620 nbrp_changed = 0;
1621
1622 nbrp->keepalive = xn->keepalive;
1623 nbrp->gtsm_enabled = xn->gtsm_enabled;
1624 nbrp->gtsm_hops = xn->gtsm_hops;
1625 nbrp->auth.method = xn->auth.method;
1626 strlcpy(nbrp->auth.md5key, xn->auth.md5key,
1627 sizeof(nbrp->auth.md5key));
1628 nbrp->auth.md5key_len = xn->auth.md5key_len;
1629 nbrp->flags = xn->flags;
1630
1631 if (ldpd_process == PROC_LDP_ENGINE) {
1632 nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr);
1633 if (nbr && nbrp_changed) {
1634 session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1635 #ifdef __OpenBSD__
1636 pfkey_remove(nbr);
1637 nbr->auth.method = nbrp->auth.method;
1638 if (pfkey_establish(nbr, nbrp) == -1)
1639 fatalx("pfkey setup failed");
1640 #else
1641 nbr->auth.method = nbrp->auth.method;
1642 sock_set_md5sig((ldp_af_global_get(&global,
1643 nbr->af))->ldp_session_socket, nbr->af,
1644 &nbr->raddr, nbrp->auth.md5key);
1645 #endif
1646 if (nbr_session_active_role(nbr))
1647 nbr_establish_connection(nbr);
1648 }
1649 }
1650 }
1651 }
1652
1653 static void
1654 merge_l2vpns(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1655 {
1656 struct l2vpn *l2vpn, *ltmp, *xl;
1657
1658 RB_FOREACH_SAFE(l2vpn, l2vpn_head, &conf->l2vpn_tree, ltmp) {
1659 /* find deleted l2vpns */
1660 if (l2vpn_find(xconf, l2vpn->name) == NULL) {
1661 switch (ldpd_process) {
1662 case PROC_LDE_ENGINE:
1663 l2vpn_exit(l2vpn);
1664 break;
1665 case PROC_LDP_ENGINE:
1666 ldpe_l2vpn_exit(l2vpn);
1667 break;
1668 case PROC_MAIN:
1669 break;
1670 }
1671 RB_REMOVE(l2vpn_head, &conf->l2vpn_tree, l2vpn);
1672 l2vpn_del(l2vpn);
1673 }
1674 }
1675 RB_FOREACH_SAFE(xl, l2vpn_head, &xconf->l2vpn_tree, ltmp) {
1676 /* find new l2vpns */
1677 if ((l2vpn = l2vpn_find(conf, xl->name)) == NULL) {
1678 COPY(l2vpn, xl);
1679 RB_INSERT(l2vpn_head, &conf->l2vpn_tree, l2vpn);
1680 RB_INIT(l2vpn_if_head, &l2vpn->if_tree);
1681 RB_INIT(l2vpn_pw_head, &l2vpn->pw_tree);
1682 RB_INIT(l2vpn_pw_head, &l2vpn->pw_inactive_tree);
1683
1684 switch (ldpd_process) {
1685 case PROC_LDE_ENGINE:
1686 l2vpn_init(l2vpn);
1687 break;
1688 case PROC_LDP_ENGINE:
1689 ldpe_l2vpn_init(l2vpn);
1690 break;
1691 case PROC_MAIN:
1692 break;
1693 }
1694 }
1695
1696 /* update existing l2vpns */
1697 merge_l2vpn(conf, l2vpn, xl);
1698 }
1699 }
1700
1701 static void
1702 merge_l2vpn(struct ldpd_conf *xconf, struct l2vpn *l2vpn, struct l2vpn *xl)
1703 {
1704 struct l2vpn_if *lif, *ftmp, *xf;
1705 struct l2vpn_pw *pw, *ptmp, *xp;
1706 struct nbr *nbr;
1707 int reset_nbr, reinstall_pwfec, reinstall_tnbr;
1708 int previous_pw_type, previous_mtu;
1709
1710 previous_pw_type = l2vpn->pw_type;
1711 previous_mtu = l2vpn->mtu;
1712
1713 /* merge intefaces */
1714 RB_FOREACH_SAFE(lif, l2vpn_if_head, &l2vpn->if_tree, ftmp) {
1715 /* find deleted interfaces */
1716 if (l2vpn_if_find(xl, lif->ifname) == NULL) {
1717 RB_REMOVE(l2vpn_if_head, &l2vpn->if_tree, lif);
1718 free(lif);
1719 }
1720 }
1721 RB_FOREACH_SAFE(xf, l2vpn_if_head, &xl->if_tree, ftmp) {
1722 /* find new interfaces */
1723 if (l2vpn_if_find(l2vpn, xf->ifname) == NULL) {
1724 COPY(lif, xf);
1725 RB_INSERT(l2vpn_if_head, &l2vpn->if_tree, lif);
1726 lif->l2vpn = l2vpn;
1727
1728 switch (ldpd_process) {
1729 case PROC_LDP_ENGINE:
1730 case PROC_LDE_ENGINE:
1731 break;
1732 case PROC_MAIN:
1733 kif_redistribute(lif->ifname);
1734 break;
1735 }
1736 }
1737 }
1738
1739 /* merge active pseudowires */
1740 RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_tree, ptmp) {
1741 /* find deleted active pseudowires */
1742 if (l2vpn_pw_find_active(xl, pw->ifname) == NULL) {
1743 switch (ldpd_process) {
1744 case PROC_LDE_ENGINE:
1745 l2vpn_pw_exit(pw);
1746 break;
1747 case PROC_LDP_ENGINE:
1748 ldpe_l2vpn_pw_exit(pw);
1749 break;
1750 case PROC_MAIN:
1751 break;
1752 }
1753
1754 RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw);
1755 free(pw);
1756 }
1757 }
1758 RB_FOREACH_SAFE(xp, l2vpn_pw_head, &xl->pw_tree, ptmp) {
1759 /* find new active pseudowires */
1760 if ((pw = l2vpn_pw_find_active(l2vpn, xp->ifname)) == NULL) {
1761 COPY(pw, xp);
1762 RB_INSERT(l2vpn_pw_head, &l2vpn->pw_tree, pw);
1763 pw->l2vpn = l2vpn;
1764
1765 switch (ldpd_process) {
1766 case PROC_LDE_ENGINE:
1767 l2vpn_pw_init(pw);
1768 break;
1769 case PROC_LDP_ENGINE:
1770 ldpe_l2vpn_pw_init(pw);
1771 break;
1772 case PROC_MAIN:
1773 kif_redistribute(pw->ifname);
1774 break;
1775 }
1776 continue;
1777 }
1778
1779 /* update existing active pseudowire */
1780 if (pw->af != xp->af ||
1781 ldp_addrcmp(pw->af, &pw->addr, &xp->addr))
1782 reinstall_tnbr = 1;
1783 else
1784 reinstall_tnbr = 0;
1785
1786 /* changes that require a session restart */
1787 if ((pw->flags & (F_PW_STATUSTLV_CONF|F_PW_CWORD_CONF)) !=
1788 (xp->flags & (F_PW_STATUSTLV_CONF|F_PW_CWORD_CONF)))
1789 reset_nbr = 1;
1790 else
1791 reset_nbr = 0;
1792
1793 if (l2vpn->pw_type != xl->pw_type || l2vpn->mtu != xl->mtu ||
1794 pw->pwid != xp->pwid || reinstall_tnbr || reset_nbr ||
1795 pw->lsr_id.s_addr != xp->lsr_id.s_addr)
1796 reinstall_pwfec = 1;
1797 else
1798 reinstall_pwfec = 0;
1799
1800 if (ldpd_process == PROC_LDP_ENGINE) {
1801 if (reinstall_tnbr)
1802 ldpe_l2vpn_pw_exit(pw);
1803 if (reset_nbr) {
1804 nbr = nbr_find_ldpid(pw->lsr_id.s_addr);
1805 if (nbr && nbr->state == NBR_STA_OPER)
1806 session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1807 }
1808 }
1809 if (ldpd_process == PROC_LDE_ENGINE && reinstall_pwfec)
1810 l2vpn_pw_exit(pw);
1811 pw->lsr_id = xp->lsr_id;
1812 pw->af = xp->af;
1813 pw->addr = xp->addr;
1814 pw->pwid = xp->pwid;
1815 strlcpy(pw->ifname, xp->ifname, sizeof(pw->ifname));
1816 pw->ifindex = xp->ifindex;
1817 if (xp->flags & F_PW_CWORD_CONF)
1818 pw->flags |= F_PW_CWORD_CONF;
1819 else
1820 pw->flags &= ~F_PW_CWORD_CONF;
1821 if (xp->flags & F_PW_STATUSTLV_CONF)
1822 pw->flags |= F_PW_STATUSTLV_CONF;
1823 else
1824 pw->flags &= ~F_PW_STATUSTLV_CONF;
1825 if (xp->flags & F_PW_STATIC_NBR_ADDR)
1826 pw->flags |= F_PW_STATIC_NBR_ADDR;
1827 else
1828 pw->flags &= ~F_PW_STATIC_NBR_ADDR;
1829 if (ldpd_process == PROC_LDP_ENGINE && reinstall_tnbr)
1830 ldpe_l2vpn_pw_init(pw);
1831 if (ldpd_process == PROC_LDE_ENGINE && reinstall_pwfec) {
1832 l2vpn->pw_type = xl->pw_type;
1833 l2vpn->mtu = xl->mtu;
1834 l2vpn_pw_init(pw);
1835 l2vpn->pw_type = previous_pw_type;
1836 l2vpn->mtu = previous_mtu;
1837 }
1838 }
1839
1840 /* merge inactive pseudowires */
1841 RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_inactive_tree, ptmp) {
1842 /* find deleted inactive pseudowires */
1843 if (l2vpn_pw_find_inactive(xl, pw->ifname) == NULL) {
1844 RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
1845 free(pw);
1846 }
1847 }
1848 RB_FOREACH_SAFE(xp, l2vpn_pw_head, &xl->pw_inactive_tree, ptmp) {
1849 /* find new inactive pseudowires */
1850 if ((pw = l2vpn_pw_find_inactive(l2vpn, xp->ifname)) == NULL) {
1851 COPY(pw, xp);
1852 RB_INSERT(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
1853 pw->l2vpn = l2vpn;
1854
1855 switch (ldpd_process) {
1856 case PROC_LDE_ENGINE:
1857 case PROC_LDP_ENGINE:
1858 break;
1859 case PROC_MAIN:
1860 kif_redistribute(pw->ifname);
1861 break;
1862 }
1863 continue;
1864 }
1865
1866 /* update existing inactive pseudowire */
1867 pw->lsr_id.s_addr = xp->lsr_id.s_addr;
1868 pw->af = xp->af;
1869 pw->addr = xp->addr;
1870 pw->pwid = xp->pwid;
1871 strlcpy(pw->ifname, xp->ifname, sizeof(pw->ifname));
1872 pw->ifindex = xp->ifindex;
1873 pw->flags = xp->flags;
1874 }
1875
1876 l2vpn->pw_type = xl->pw_type;
1877 l2vpn->mtu = xl->mtu;
1878 strlcpy(l2vpn->br_ifname, xl->br_ifname, sizeof(l2vpn->br_ifname));
1879 l2vpn->br_ifindex = xl->br_ifindex;
1880 }
1881
1882 struct ldpd_conf *
1883 config_new_empty(void)
1884 {
1885 struct ldpd_conf *xconf;
1886
1887 xconf = calloc(1, sizeof(*xconf));
1888 if (xconf == NULL)
1889 fatal(NULL);
1890
1891 RB_INIT(iface_head, &xconf->iface_tree);
1892 RB_INIT(tnbr_head, &xconf->tnbr_tree);
1893 RB_INIT(nbrp_head, &xconf->nbrp_tree);
1894 RB_INIT(l2vpn_head, &xconf->l2vpn_tree);
1895
1896 /* set default values */
1897 ldp_config_reset(xconf);
1898
1899 return (xconf);
1900 }
1901
1902 void
1903 config_clear(struct ldpd_conf *conf)
1904 {
1905 struct ldpd_conf *xconf;
1906
1907 /*
1908 * Merge current config with an empty config, this will deactivate
1909 * and deallocate all the interfaces, pseudowires and so on. Before
1910 * merging, copy the router-id and other variables to avoid some
1911 * unnecessary operations, like trying to reset the neighborships.
1912 */
1913 xconf = config_new_empty();
1914 xconf->ipv4 = conf->ipv4;
1915 xconf->ipv6 = conf->ipv6;
1916 xconf->rtr_id = conf->rtr_id;
1917 xconf->trans_pref = conf->trans_pref;
1918 xconf->flags = conf->flags;
1919 merge_config(conf, xconf);
1920 free(xconf);
1921 free(conf);
1922 }