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