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