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