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