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