]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/ldpd.c
Merge pull request #1764 from Orange-OpenSource/SR-dev
[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 (!RB_EMPTY(iface_head, &conf->iface_tree)) {
1070 iface = RB_ROOT(iface_head, &conf->iface_tree);
1071
1072 QOBJ_UNREG(iface);
1073 RB_REMOVE(iface_head, &conf->iface_tree, iface);
1074 free(iface);
1075 }
1076
1077 while (!RB_EMPTY(nbrp_head, &conf->nbrp_tree)) {
1078 nbrp = RB_ROOT(nbrp_head, &conf->nbrp_tree);
1079
1080 QOBJ_UNREG(nbrp);
1081 RB_REMOVE(nbrp_head, &conf->nbrp_tree, nbrp);
1082 free(nbrp);
1083 }
1084
1085 conf->rtr_id.s_addr = INADDR_ANY;
1086 ldp_config_reset_af(conf, AF_INET);
1087 ldp_config_reset_af(conf, AF_INET6);
1088 conf->lhello_holdtime = LINK_DFLT_HOLDTIME;
1089 conf->lhello_interval = DEFAULT_HELLO_INTERVAL;
1090 conf->thello_holdtime = TARGETED_DFLT_HOLDTIME;
1091 conf->thello_interval = DEFAULT_HELLO_INTERVAL;
1092 conf->trans_pref = DUAL_STACK_LDPOV6;
1093 conf->flags = 0;
1094 }
1095
1096 static void
1097 ldp_config_reset_af(struct ldpd_conf *conf, int af)
1098 {
1099 struct ldpd_af_conf *af_conf;
1100 struct iface *iface;
1101 struct iface_af *ia;
1102 struct tnbr *tnbr, *ttmp;
1103
1104 RB_FOREACH(iface, iface_head, &conf->iface_tree) {
1105 ia = iface_af_get(iface, af);
1106 ia->enabled = 0;
1107 }
1108
1109 RB_FOREACH_SAFE(tnbr, tnbr_head, &conf->tnbr_tree, ttmp) {
1110 if (tnbr->af != af)
1111 continue;
1112
1113 QOBJ_UNREG(tnbr);
1114 RB_REMOVE(tnbr_head, &conf->tnbr_tree, tnbr);
1115 free(tnbr);
1116 }
1117
1118 af_conf = ldp_af_conf_get(conf, af);
1119 af_conf->keepalive = 180;
1120 af_conf->lhello_holdtime = 0;
1121 af_conf->lhello_interval = 0;
1122 af_conf->thello_holdtime = 0;
1123 af_conf->thello_interval = 0;
1124 memset(&af_conf->trans_addr, 0, sizeof(af_conf->trans_addr));
1125 af_conf->flags = 0;
1126 }
1127
1128 static void
1129 ldp_config_reset_l2vpns(struct ldpd_conf *conf)
1130 {
1131 struct l2vpn *l2vpn;
1132 struct l2vpn_if *lif;
1133 struct l2vpn_pw *pw;
1134
1135 while (!RB_EMPTY(l2vpn_head, &conf->l2vpn_tree)) {
1136 l2vpn = RB_ROOT(l2vpn_head, &conf->l2vpn_tree);
1137 while (!RB_EMPTY(l2vpn_if_head, &l2vpn->if_tree)) {
1138 lif = RB_ROOT(l2vpn_if_head, &l2vpn->if_tree);
1139
1140 QOBJ_UNREG(lif);
1141 RB_REMOVE(l2vpn_if_head, &l2vpn->if_tree, lif);
1142 free(lif);
1143 }
1144 while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_tree)) {
1145 pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_tree);
1146
1147 QOBJ_UNREG(pw);
1148 RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw);
1149 free(pw);
1150 }
1151 while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_inactive_tree)) {
1152 pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_inactive_tree);
1153
1154 QOBJ_UNREG(pw);
1155 RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
1156 free(pw);
1157 }
1158 QOBJ_UNREG(l2vpn);
1159 RB_REMOVE(l2vpn_head, &conf->l2vpn_tree, l2vpn);
1160 free(l2vpn);
1161 }
1162 }
1163
1164 void
1165 ldp_clear_config(struct ldpd_conf *xconf)
1166 {
1167 struct iface *iface;
1168 struct tnbr *tnbr;
1169 struct nbr_params *nbrp;
1170 struct l2vpn *l2vpn;
1171
1172 while (!RB_EMPTY(iface_head, &xconf->iface_tree)) {
1173 iface = RB_ROOT(iface_head, &xconf->iface_tree);
1174
1175 RB_REMOVE(iface_head, &xconf->iface_tree, iface);
1176 free(iface);
1177 }
1178 while (!RB_EMPTY(tnbr_head, &xconf->tnbr_tree)) {
1179 tnbr = RB_ROOT(tnbr_head, &xconf->tnbr_tree);
1180
1181 RB_REMOVE(tnbr_head, &xconf->tnbr_tree, tnbr);
1182 free(tnbr);
1183 }
1184 while (!RB_EMPTY(nbrp_head, &xconf->nbrp_tree)) {
1185 nbrp = RB_ROOT(nbrp_head, &xconf->nbrp_tree);
1186
1187 RB_REMOVE(nbrp_head, &xconf->nbrp_tree, nbrp);
1188 free(nbrp);
1189 }
1190 while (!RB_EMPTY(l2vpn_head, &xconf->l2vpn_tree)) {
1191 l2vpn = RB_ROOT(l2vpn_head, &xconf->l2vpn_tree);
1192
1193 RB_REMOVE(l2vpn_head, &xconf->l2vpn_tree, l2vpn);
1194 l2vpn_del(l2vpn);
1195 }
1196
1197 free(xconf);
1198 }
1199
1200 #define COPY(a, b) do { \
1201 a = malloc(sizeof(*a)); \
1202 if (a == NULL) \
1203 fatal(__func__); \
1204 *a = *b; \
1205 } while (0)
1206
1207 void
1208 merge_config(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1209 {
1210 merge_global(conf, xconf);
1211 merge_af(AF_INET, &conf->ipv4, &xconf->ipv4);
1212 merge_af(AF_INET6, &conf->ipv6, &xconf->ipv6);
1213 merge_ifaces(conf, xconf);
1214 merge_tnbrs(conf, xconf);
1215 merge_nbrps(conf, xconf);
1216 merge_l2vpns(conf, xconf);
1217 }
1218
1219 static void
1220 merge_global(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1221 {
1222 /* change of router-id requires resetting all neighborships */
1223 if (conf->rtr_id.s_addr != xconf->rtr_id.s_addr) {
1224 if (ldpd_process == PROC_LDP_ENGINE) {
1225 ldpe_reset_nbrs(AF_UNSPEC);
1226 if (conf->rtr_id.s_addr == INADDR_ANY ||
1227 xconf->rtr_id.s_addr == INADDR_ANY) {
1228 if_update_all(AF_UNSPEC);
1229 tnbr_update_all(AF_UNSPEC);
1230 }
1231 }
1232 conf->rtr_id = xconf->rtr_id;
1233 }
1234
1235 conf->lhello_holdtime = xconf->lhello_holdtime;
1236 conf->lhello_interval = xconf->lhello_interval;
1237 conf->thello_holdtime = xconf->thello_holdtime;
1238 conf->thello_interval = xconf->thello_interval;
1239
1240 if (conf->trans_pref != xconf->trans_pref) {
1241 if (ldpd_process == PROC_LDP_ENGINE)
1242 ldpe_reset_ds_nbrs();
1243 conf->trans_pref = xconf->trans_pref;
1244 }
1245
1246 if ((conf->flags & F_LDPD_DS_CISCO_INTEROP) !=
1247 (xconf->flags & F_LDPD_DS_CISCO_INTEROP)) {
1248 if (ldpd_process == PROC_LDP_ENGINE)
1249 ldpe_reset_ds_nbrs();
1250 }
1251
1252 conf->flags = xconf->flags;
1253 }
1254
1255 static void
1256 merge_af(int af, struct ldpd_af_conf *af_conf, struct ldpd_af_conf *xa)
1257 {
1258 int stop_init_backoff = 0;
1259 int remove_dynamic_tnbrs = 0;
1260 int change_egress_label = 0;
1261 int reset_nbrs_ipv4 = 0;
1262 int reset_nbrs = 0;
1263 int update_sockets = 0;
1264
1265 /* update timers */
1266 if (af_conf->keepalive != xa->keepalive) {
1267 af_conf->keepalive = xa->keepalive;
1268 stop_init_backoff = 1;
1269 }
1270 af_conf->lhello_holdtime = xa->lhello_holdtime;
1271 af_conf->lhello_interval = xa->lhello_interval;
1272 af_conf->thello_holdtime = xa->thello_holdtime;
1273 af_conf->thello_interval = xa->thello_interval;
1274
1275 /* update flags */
1276 if ((af_conf->flags & F_LDPD_AF_THELLO_ACCEPT) &&
1277 !(xa->flags & F_LDPD_AF_THELLO_ACCEPT))
1278 remove_dynamic_tnbrs = 1;
1279 if ((af_conf->flags & F_LDPD_AF_NO_GTSM) !=
1280 (xa->flags & F_LDPD_AF_NO_GTSM)) {
1281 if (af == AF_INET6)
1282 /* need to set/unset IPV6_MINHOPCOUNT */
1283 update_sockets = 1;
1284 else
1285 /* for LDPv4 just resetting the neighbors is enough */
1286 reset_nbrs_ipv4 = 1;
1287 }
1288 if ((af_conf->flags & F_LDPD_AF_EXPNULL) !=
1289 (xa->flags & F_LDPD_AF_EXPNULL))
1290 change_egress_label = 1;
1291 af_conf->flags = xa->flags;
1292
1293 /* update the transport address */
1294 if (ldp_addrcmp(af, &af_conf->trans_addr, &xa->trans_addr)) {
1295 af_conf->trans_addr = xa->trans_addr;
1296 update_sockets = 1;
1297 }
1298
1299 /* update ACLs */
1300 if (strcmp(af_conf->acl_label_advertise_to,
1301 xa->acl_label_advertise_to) ||
1302 strcmp(af_conf->acl_label_advertise_for,
1303 xa->acl_label_advertise_for) ||
1304 strcmp(af_conf->acl_label_accept_from,
1305 xa->acl_label_accept_from) ||
1306 strcmp(af_conf->acl_label_accept_for,
1307 xa->acl_label_accept_for))
1308 reset_nbrs = 1;
1309 if (strcmp(af_conf->acl_thello_accept_from, xa->acl_thello_accept_from))
1310 remove_dynamic_tnbrs = 1;
1311 if (strcmp(af_conf->acl_label_expnull_for, xa->acl_label_expnull_for))
1312 change_egress_label = 1;
1313 strlcpy(af_conf->acl_thello_accept_from, xa->acl_thello_accept_from,
1314 sizeof(af_conf->acl_thello_accept_from));
1315 strlcpy(af_conf->acl_label_allocate_for, xa->acl_label_allocate_for,
1316 sizeof(af_conf->acl_label_allocate_for));
1317 strlcpy(af_conf->acl_label_advertise_to, xa->acl_label_advertise_to,
1318 sizeof(af_conf->acl_label_advertise_to));
1319 strlcpy(af_conf->acl_label_advertise_for, xa->acl_label_advertise_for,
1320 sizeof(af_conf->acl_label_advertise_for));
1321 strlcpy(af_conf->acl_label_accept_from, xa->acl_label_accept_from,
1322 sizeof(af_conf->acl_label_accept_from));
1323 strlcpy(af_conf->acl_label_accept_for, xa->acl_label_accept_for,
1324 sizeof(af_conf->acl_label_accept_for));
1325 strlcpy(af_conf->acl_label_expnull_for, xa->acl_label_expnull_for,
1326 sizeof(af_conf->acl_label_expnull_for));
1327
1328 /* apply the new configuration */
1329 switch (ldpd_process) {
1330 case PROC_LDE_ENGINE:
1331 if (change_egress_label)
1332 lde_change_egress_label(af);
1333 break;
1334 case PROC_LDP_ENGINE:
1335 if (stop_init_backoff)
1336 ldpe_stop_init_backoff(af);
1337 if (remove_dynamic_tnbrs)
1338 ldpe_remove_dynamic_tnbrs(af);
1339 if (reset_nbrs)
1340 ldpe_reset_nbrs(AF_UNSPEC);
1341 else if (reset_nbrs_ipv4)
1342 ldpe_reset_nbrs(AF_INET);
1343 break;
1344 case PROC_MAIN:
1345 if (update_sockets && iev_ldpe)
1346 imsg_compose_event(iev_ldpe, IMSG_CLOSE_SOCKETS, af,
1347 0, -1, NULL, 0);
1348 break;
1349 }
1350 }
1351
1352 static void
1353 merge_ifaces(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1354 {
1355 struct iface *iface, *itmp, *xi;
1356
1357 RB_FOREACH_SAFE(iface, iface_head, &conf->iface_tree, itmp) {
1358 /* find deleted interfaces */
1359 if ((xi = if_lookup_name(xconf, iface->name)) == NULL) {
1360 switch (ldpd_process) {
1361 case PROC_LDP_ENGINE:
1362 ldpe_if_exit(iface);
1363 break;
1364 case PROC_LDE_ENGINE:
1365 case PROC_MAIN:
1366 break;
1367 }
1368 RB_REMOVE(iface_head, &conf->iface_tree, iface);
1369 free(iface);
1370 }
1371 }
1372 RB_FOREACH_SAFE(xi, iface_head, &xconf->iface_tree, itmp) {
1373 /* find new interfaces */
1374 if ((iface = if_lookup_name(conf, xi->name)) == NULL) {
1375 COPY(iface, xi);
1376 RB_INSERT(iface_head, &conf->iface_tree, iface);
1377
1378 switch (ldpd_process) {
1379 case PROC_LDP_ENGINE:
1380 ldpe_if_init(iface);
1381 break;
1382 case PROC_LDE_ENGINE:
1383 break;
1384 case PROC_MAIN:
1385 /* resend addresses to activate new interfaces */
1386 kif_redistribute(iface->name);
1387 break;
1388 }
1389 continue;
1390 }
1391
1392 /* update existing interfaces */
1393 merge_iface_af(&iface->ipv4, &xi->ipv4);
1394 merge_iface_af(&iface->ipv6, &xi->ipv6);
1395 }
1396 }
1397
1398 static void
1399 merge_iface_af(struct iface_af *ia, struct iface_af *xi)
1400 {
1401 if (ia->enabled != xi->enabled) {
1402 ia->enabled = xi->enabled;
1403 if (ldpd_process == PROC_LDP_ENGINE)
1404 ldp_if_update(ia->iface, ia->af);
1405 }
1406 ia->hello_holdtime = xi->hello_holdtime;
1407 ia->hello_interval = xi->hello_interval;
1408 }
1409
1410 static void
1411 merge_tnbrs(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1412 {
1413 struct tnbr *tnbr, *ttmp, *xt;
1414
1415 RB_FOREACH_SAFE(tnbr, tnbr_head, &conf->tnbr_tree, ttmp) {
1416 if (!(tnbr->flags & F_TNBR_CONFIGURED))
1417 continue;
1418
1419 /* find deleted tnbrs */
1420 if ((xt = tnbr_find(xconf, tnbr->af, &tnbr->addr)) == NULL) {
1421 switch (ldpd_process) {
1422 case PROC_LDP_ENGINE:
1423 tnbr->flags &= ~F_TNBR_CONFIGURED;
1424 tnbr_check(conf, tnbr);
1425 break;
1426 case PROC_LDE_ENGINE:
1427 case PROC_MAIN:
1428 RB_REMOVE(tnbr_head, &conf->tnbr_tree, tnbr);
1429 free(tnbr);
1430 break;
1431 }
1432 }
1433 }
1434 RB_FOREACH_SAFE(xt, tnbr_head, &xconf->tnbr_tree, ttmp) {
1435 /* find new tnbrs */
1436 if ((tnbr = tnbr_find(conf, xt->af, &xt->addr)) == NULL) {
1437 COPY(tnbr, xt);
1438 RB_INSERT(tnbr_head, &conf->tnbr_tree, tnbr);
1439
1440 switch (ldpd_process) {
1441 case PROC_LDP_ENGINE:
1442 tnbr_update(tnbr);
1443 break;
1444 case PROC_LDE_ENGINE:
1445 case PROC_MAIN:
1446 break;
1447 }
1448 continue;
1449 }
1450
1451 /* update existing tnbrs */
1452 if (!(tnbr->flags & F_TNBR_CONFIGURED))
1453 tnbr->flags |= F_TNBR_CONFIGURED;
1454 }
1455 }
1456
1457 static void
1458 merge_nbrps(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1459 {
1460 struct nbr_params *nbrp, *ntmp, *xn;
1461 struct nbr *nbr;
1462 int nbrp_changed;
1463
1464 RB_FOREACH_SAFE(nbrp, nbrp_head, &conf->nbrp_tree, ntmp) {
1465 /* find deleted nbrps */
1466 if ((xn = nbr_params_find(xconf, nbrp->lsr_id)) == NULL) {
1467 switch (ldpd_process) {
1468 case PROC_LDP_ENGINE:
1469 nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr);
1470 if (nbr) {
1471 session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1472 #ifdef __OpenBSD__
1473 pfkey_remove(nbr);
1474 #else
1475 sock_set_md5sig(
1476 (ldp_af_global_get(&global,
1477 nbr->af))->ldp_session_socket,
1478 nbr->af, &nbr->raddr, NULL);
1479 #endif
1480 nbr->auth.method = AUTH_NONE;
1481 if (nbr_session_active_role(nbr))
1482 nbr_establish_connection(nbr);
1483 }
1484 break;
1485 case PROC_LDE_ENGINE:
1486 case PROC_MAIN:
1487 break;
1488 }
1489 RB_REMOVE(nbrp_head, &conf->nbrp_tree, nbrp);
1490 free(nbrp);
1491 }
1492 }
1493 RB_FOREACH_SAFE(xn, nbrp_head, &xconf->nbrp_tree, ntmp) {
1494 /* find new nbrps */
1495 if ((nbrp = nbr_params_find(conf, xn->lsr_id)) == NULL) {
1496 COPY(nbrp, xn);
1497 RB_INSERT(nbrp_head, &conf->nbrp_tree, nbrp);
1498
1499 switch (ldpd_process) {
1500 case PROC_LDP_ENGINE:
1501 nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr);
1502 if (nbr) {
1503 session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1504 nbr->auth.method = nbrp->auth.method;
1505 #ifdef __OpenBSD__
1506 if (pfkey_establish(nbr, nbrp) == -1)
1507 fatalx("pfkey setup failed");
1508 #else
1509 sock_set_md5sig(
1510 (ldp_af_global_get(&global,
1511 nbr->af))->ldp_session_socket,
1512 nbr->af, &nbr->raddr,
1513 nbrp->auth.md5key);
1514 #endif
1515 if (nbr_session_active_role(nbr))
1516 nbr_establish_connection(nbr);
1517 }
1518 break;
1519 case PROC_LDE_ENGINE:
1520 case PROC_MAIN:
1521 break;
1522 }
1523 continue;
1524 }
1525
1526 /* update existing nbrps */
1527 if (nbrp->flags != xn->flags ||
1528 nbrp->keepalive != xn->keepalive ||
1529 nbrp->gtsm_enabled != xn->gtsm_enabled ||
1530 nbrp->gtsm_hops != xn->gtsm_hops ||
1531 nbrp->auth.method != xn->auth.method ||
1532 strcmp(nbrp->auth.md5key, xn->auth.md5key) != 0)
1533 nbrp_changed = 1;
1534 else
1535 nbrp_changed = 0;
1536
1537 nbrp->keepalive = xn->keepalive;
1538 nbrp->gtsm_enabled = xn->gtsm_enabled;
1539 nbrp->gtsm_hops = xn->gtsm_hops;
1540 nbrp->auth.method = xn->auth.method;
1541 strlcpy(nbrp->auth.md5key, xn->auth.md5key,
1542 sizeof(nbrp->auth.md5key));
1543 nbrp->auth.md5key_len = xn->auth.md5key_len;
1544 nbrp->flags = xn->flags;
1545
1546 if (ldpd_process == PROC_LDP_ENGINE) {
1547 nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr);
1548 if (nbr && nbrp_changed) {
1549 session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1550 #ifdef __OpenBSD__
1551 pfkey_remove(nbr);
1552 nbr->auth.method = nbrp->auth.method;
1553 if (pfkey_establish(nbr, nbrp) == -1)
1554 fatalx("pfkey setup failed");
1555 #else
1556 nbr->auth.method = nbrp->auth.method;
1557 sock_set_md5sig((ldp_af_global_get(&global,
1558 nbr->af))->ldp_session_socket, nbr->af,
1559 &nbr->raddr, nbrp->auth.md5key);
1560 #endif
1561 if (nbr_session_active_role(nbr))
1562 nbr_establish_connection(nbr);
1563 }
1564 }
1565 }
1566 }
1567
1568 static void
1569 merge_l2vpns(struct ldpd_conf *conf, struct ldpd_conf *xconf)
1570 {
1571 struct l2vpn *l2vpn, *ltmp, *xl;
1572
1573 RB_FOREACH_SAFE(l2vpn, l2vpn_head, &conf->l2vpn_tree, ltmp) {
1574 /* find deleted l2vpns */
1575 if ((xl = l2vpn_find(xconf, l2vpn->name)) == NULL) {
1576 switch (ldpd_process) {
1577 case PROC_LDE_ENGINE:
1578 l2vpn_exit(l2vpn);
1579 break;
1580 case PROC_LDP_ENGINE:
1581 ldpe_l2vpn_exit(l2vpn);
1582 break;
1583 case PROC_MAIN:
1584 break;
1585 }
1586 RB_REMOVE(l2vpn_head, &conf->l2vpn_tree, l2vpn);
1587 l2vpn_del(l2vpn);
1588 }
1589 }
1590 RB_FOREACH_SAFE(xl, l2vpn_head, &xconf->l2vpn_tree, ltmp) {
1591 /* find new l2vpns */
1592 if ((l2vpn = l2vpn_find(conf, xl->name)) == NULL) {
1593 COPY(l2vpn, xl);
1594 RB_INSERT(l2vpn_head, &conf->l2vpn_tree, l2vpn);
1595 RB_INIT(l2vpn_if_head, &l2vpn->if_tree);
1596 RB_INIT(l2vpn_pw_head, &l2vpn->pw_tree);
1597 RB_INIT(l2vpn_pw_head, &l2vpn->pw_inactive_tree);
1598
1599 switch (ldpd_process) {
1600 case PROC_LDE_ENGINE:
1601 l2vpn_init(l2vpn);
1602 break;
1603 case PROC_LDP_ENGINE:
1604 ldpe_l2vpn_init(l2vpn);
1605 break;
1606 case PROC_MAIN:
1607 break;
1608 }
1609 }
1610
1611 /* update existing l2vpns */
1612 merge_l2vpn(conf, l2vpn, xl);
1613 }
1614 }
1615
1616 static void
1617 merge_l2vpn(struct ldpd_conf *xconf, struct l2vpn *l2vpn, struct l2vpn *xl)
1618 {
1619 struct l2vpn_if *lif, *ftmp, *xf;
1620 struct l2vpn_pw *pw, *ptmp, *xp;
1621 struct nbr *nbr;
1622 int reset_nbr, reinstall_pwfec, reinstall_tnbr;
1623 int previous_pw_type, previous_mtu;
1624
1625 previous_pw_type = l2vpn->pw_type;
1626 previous_mtu = l2vpn->mtu;
1627
1628 /* merge intefaces */
1629 RB_FOREACH_SAFE(lif, l2vpn_if_head, &l2vpn->if_tree, ftmp) {
1630 /* find deleted interfaces */
1631 if ((xf = l2vpn_if_find(xl, lif->ifname)) == NULL) {
1632 RB_REMOVE(l2vpn_if_head, &l2vpn->if_tree, lif);
1633 free(lif);
1634 }
1635 }
1636 RB_FOREACH_SAFE(xf, l2vpn_if_head, &xl->if_tree, ftmp) {
1637 /* find new interfaces */
1638 if ((lif = l2vpn_if_find(l2vpn, xf->ifname)) == NULL) {
1639 COPY(lif, xf);
1640 RB_INSERT(l2vpn_if_head, &l2vpn->if_tree, lif);
1641 lif->l2vpn = l2vpn;
1642
1643 switch (ldpd_process) {
1644 case PROC_LDP_ENGINE:
1645 case PROC_LDE_ENGINE:
1646 break;
1647 case PROC_MAIN:
1648 kif_redistribute(lif->ifname);
1649 break;
1650 }
1651 }
1652 }
1653
1654 /* merge active pseudowires */
1655 RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_tree, ptmp) {
1656 /* find deleted active pseudowires */
1657 if ((xp = l2vpn_pw_find_active(xl, pw->ifname)) == NULL) {
1658 switch (ldpd_process) {
1659 case PROC_LDE_ENGINE:
1660 l2vpn_pw_exit(pw);
1661 break;
1662 case PROC_LDP_ENGINE:
1663 ldpe_l2vpn_pw_exit(pw);
1664 break;
1665 case PROC_MAIN:
1666 break;
1667 }
1668
1669 RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw);
1670 free(pw);
1671 }
1672 }
1673 RB_FOREACH_SAFE(xp, l2vpn_pw_head, &xl->pw_tree, ptmp) {
1674 /* find new active pseudowires */
1675 if ((pw = l2vpn_pw_find_active(l2vpn, xp->ifname)) == NULL) {
1676 COPY(pw, xp);
1677 RB_INSERT(l2vpn_pw_head, &l2vpn->pw_tree, pw);
1678 pw->l2vpn = l2vpn;
1679
1680 switch (ldpd_process) {
1681 case PROC_LDE_ENGINE:
1682 l2vpn_pw_init(pw);
1683 break;
1684 case PROC_LDP_ENGINE:
1685 ldpe_l2vpn_pw_init(pw);
1686 break;
1687 case PROC_MAIN:
1688 kif_redistribute(pw->ifname);
1689 break;
1690 }
1691 continue;
1692 }
1693
1694 /* update existing active pseudowire */
1695 if (pw->af != xp->af ||
1696 ldp_addrcmp(pw->af, &pw->addr, &xp->addr))
1697 reinstall_tnbr = 1;
1698 else
1699 reinstall_tnbr = 0;
1700
1701 /* changes that require a session restart */
1702 if ((pw->flags & (F_PW_STATUSTLV_CONF|F_PW_CWORD_CONF)) !=
1703 (xp->flags & (F_PW_STATUSTLV_CONF|F_PW_CWORD_CONF)))
1704 reset_nbr = 1;
1705 else
1706 reset_nbr = 0;
1707
1708 if (l2vpn->pw_type != xl->pw_type || l2vpn->mtu != xl->mtu ||
1709 pw->pwid != xp->pwid || reinstall_tnbr || reset_nbr ||
1710 pw->lsr_id.s_addr != xp->lsr_id.s_addr)
1711 reinstall_pwfec = 1;
1712 else
1713 reinstall_pwfec = 0;
1714
1715 if (ldpd_process == PROC_LDP_ENGINE) {
1716 if (reinstall_tnbr)
1717 ldpe_l2vpn_pw_exit(pw);
1718 if (reset_nbr) {
1719 nbr = nbr_find_ldpid(pw->lsr_id.s_addr);
1720 if (nbr && nbr->state == NBR_STA_OPER)
1721 session_shutdown(nbr, S_SHUTDOWN, 0, 0);
1722 }
1723 }
1724 if (ldpd_process == PROC_LDE_ENGINE && reinstall_pwfec)
1725 l2vpn_pw_exit(pw);
1726 pw->lsr_id = xp->lsr_id;
1727 pw->af = xp->af;
1728 pw->addr = xp->addr;
1729 pw->pwid = xp->pwid;
1730 strlcpy(pw->ifname, xp->ifname, sizeof(pw->ifname));
1731 pw->ifindex = xp->ifindex;
1732 if (xp->flags & F_PW_CWORD_CONF)
1733 pw->flags |= F_PW_CWORD_CONF;
1734 else
1735 pw->flags &= ~F_PW_CWORD_CONF;
1736 if (xp->flags & F_PW_STATUSTLV_CONF)
1737 pw->flags |= F_PW_STATUSTLV_CONF;
1738 else
1739 pw->flags &= ~F_PW_STATUSTLV_CONF;
1740 if (xp->flags & F_PW_STATIC_NBR_ADDR)
1741 pw->flags |= F_PW_STATIC_NBR_ADDR;
1742 else
1743 pw->flags &= ~F_PW_STATIC_NBR_ADDR;
1744 if (ldpd_process == PROC_LDP_ENGINE && reinstall_tnbr)
1745 ldpe_l2vpn_pw_init(pw);
1746 if (ldpd_process == PROC_LDE_ENGINE && reinstall_pwfec) {
1747 l2vpn->pw_type = xl->pw_type;
1748 l2vpn->mtu = xl->mtu;
1749 l2vpn_pw_init(pw);
1750 l2vpn->pw_type = previous_pw_type;
1751 l2vpn->mtu = previous_mtu;
1752 }
1753 }
1754
1755 /* merge inactive pseudowires */
1756 RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_inactive_tree, ptmp) {
1757 /* find deleted inactive pseudowires */
1758 if ((xp = l2vpn_pw_find_inactive(xl, pw->ifname)) == NULL) {
1759 RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
1760 free(pw);
1761 }
1762 }
1763 RB_FOREACH_SAFE(xp, l2vpn_pw_head, &xl->pw_inactive_tree, ptmp) {
1764 /* find new inactive pseudowires */
1765 if ((pw = l2vpn_pw_find_inactive(l2vpn, xp->ifname)) == NULL) {
1766 COPY(pw, xp);
1767 RB_INSERT(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
1768 pw->l2vpn = l2vpn;
1769
1770 switch (ldpd_process) {
1771 case PROC_LDE_ENGINE:
1772 case PROC_LDP_ENGINE:
1773 break;
1774 case PROC_MAIN:
1775 kif_redistribute(pw->ifname);
1776 break;
1777 }
1778 continue;
1779 }
1780
1781 /* update existing inactive pseudowire */
1782 pw->lsr_id.s_addr = xp->lsr_id.s_addr;
1783 pw->af = xp->af;
1784 pw->addr = xp->addr;
1785 pw->pwid = xp->pwid;
1786 strlcpy(pw->ifname, xp->ifname, sizeof(pw->ifname));
1787 pw->ifindex = xp->ifindex;
1788 pw->flags = xp->flags;
1789 }
1790
1791 l2vpn->pw_type = xl->pw_type;
1792 l2vpn->mtu = xl->mtu;
1793 strlcpy(l2vpn->br_ifname, xl->br_ifname, sizeof(l2vpn->br_ifname));
1794 l2vpn->br_ifindex = xl->br_ifindex;
1795 }
1796
1797 struct ldpd_conf *
1798 config_new_empty(void)
1799 {
1800 struct ldpd_conf *xconf;
1801
1802 xconf = calloc(1, sizeof(*xconf));
1803 if (xconf == NULL)
1804 fatal(NULL);
1805
1806 RB_INIT(iface_head, &xconf->iface_tree);
1807 RB_INIT(tnbr_head, &xconf->tnbr_tree);
1808 RB_INIT(nbrp_head, &xconf->nbrp_tree);
1809 RB_INIT(l2vpn_head, &xconf->l2vpn_tree);
1810
1811 /* set default values */
1812 ldp_config_reset(xconf);
1813
1814 return (xconf);
1815 }
1816
1817 void
1818 config_clear(struct ldpd_conf *conf)
1819 {
1820 struct ldpd_conf *xconf;
1821
1822 /*
1823 * Merge current config with an empty config, this will deactivate
1824 * and deallocate all the interfaces, pseudowires and so on. Before
1825 * merging, copy the router-id and other variables to avoid some
1826 * unnecessary operations, like trying to reset the neighborships.
1827 */
1828 xconf = config_new_empty();
1829 xconf->ipv4 = conf->ipv4;
1830 xconf->ipv6 = conf->ipv6;
1831 xconf->rtr_id = conf->rtr_id;
1832 xconf->trans_pref = conf->trans_pref;
1833 xconf->flags = conf->flags;
1834 merge_config(conf, xconf);
1835 free(xconf);
1836 free(conf);
1837 }