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