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