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