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