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