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