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