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