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