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