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