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