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