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