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