]>
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 | 55 | static void ldp_config_normalize(struct ldpd_conf *); |
e4a82008 | 56 | static void ldp_config_reset(struct ldpd_conf *); |
56b8a8d2 RW |
57 | static void ldp_config_reset_main(struct ldpd_conf *); |
58 | static void ldp_config_reset_af(struct ldpd_conf *, int); | |
1d75a89d | 59 | static void ldp_config_reset_l2vpns(struct ldpd_conf *); |
8429abe0 RW |
60 | static void merge_global(struct ldpd_conf *, struct ldpd_conf *); |
61 | static void merge_af(int, struct ldpd_af_conf *, | |
62 | struct ldpd_af_conf *); | |
56b8a8d2 | 63 | static void merge_ifaces(struct ldpd_conf *, struct ldpd_conf *); |
8429abe0 | 64 | static void merge_iface_af(struct iface_af *, struct iface_af *); |
56b8a8d2 RW |
65 | static void merge_tnbrs(struct ldpd_conf *, struct ldpd_conf *); |
66 | static void merge_nbrps(struct ldpd_conf *, struct ldpd_conf *); | |
67 | static void merge_l2vpns(struct ldpd_conf *, struct ldpd_conf *); | |
8429abe0 | 68 | static void merge_l2vpn(struct ldpd_conf *, struct l2vpn *, |
56b8a8d2 | 69 | struct l2vpn *); |
8429abe0 | 70 | |
4af8997d RW |
71 | DEFINE_QOBJ_TYPE(iface) |
72 | DEFINE_QOBJ_TYPE(tnbr) | |
73 | DEFINE_QOBJ_TYPE(nbr_params) | |
74 | DEFINE_QOBJ_TYPE(l2vpn_if) | |
75 | DEFINE_QOBJ_TYPE(l2vpn_pw) | |
76 | DEFINE_QOBJ_TYPE(l2vpn) | |
77 | DEFINE_QOBJ_TYPE(ldpd_conf) | |
78 | ||
8429abe0 | 79 | struct ldpd_global global; |
274f5abf | 80 | struct ldpd_init init; |
56b8a8d2 | 81 | struct ldpd_conf *ldpd_conf, *vty_conf; |
8429abe0 | 82 | |
28e8294c RW |
83 | static struct imsgev *iev_ldpe, *iev_ldpe_sync; |
84 | static struct imsgev *iev_lde, *iev_lde_sync; | |
8429abe0 RW |
85 | static pid_t ldpe_pid; |
86 | static pid_t lde_pid; | |
87 | ||
eac6e3f0 RW |
88 | #define LDP_DEFAULT_CONFIG "ldpd.conf" |
89 | #define LDP_VTY_PORT 2612 | |
90 | ||
91 | /* Master of threads. */ | |
92 | struct thread_master *master; | |
93 | ||
dbce3582 RW |
94 | static struct frr_daemon_info ldpd_di; |
95 | ||
eac6e3f0 RW |
96 | /* ldpd privileges */ |
97 | static zebra_capabilities_t _caps_p [] = | |
8429abe0 | 98 | { |
eac6e3f0 RW |
99 | ZCAP_BIND, |
100 | ZCAP_NET_ADMIN | |
101 | }; | |
102 | ||
103 | struct zebra_privs_t ldpd_privs = | |
104 | { | |
b2f36157 DL |
105 | #if defined(FRR_USER) && defined(FRR_GROUP) |
106 | .user = FRR_USER, | |
107 | .group = FRR_GROUP, | |
eac6e3f0 RW |
108 | #endif |
109 | #if defined(VTY_GROUP) | |
110 | .vty_group = VTY_GROUP, | |
111 | #endif | |
112 | .caps_p = _caps_p, | |
113 | .cap_num_p = array_size(_caps_p), | |
114 | .cap_num_i = 0 | |
115 | }; | |
116 | ||
372b8bd3 RW |
117 | /* CTL Socket path */ |
118 | char ctl_sock_path[MAXPATHLEN] = LDPD_SOCKET; | |
119 | ||
eac6e3f0 | 120 | /* LDPd options. */ |
372b8bd3 | 121 | #define OPTION_CTLSOCK 1001 |
eac6e3f0 RW |
122 | static struct option longopts[] = |
123 | { | |
372b8bd3 | 124 | { "ctl_socket", required_argument, NULL, OPTION_CTLSOCK}, |
fea12efb | 125 | { "instance", required_argument, NULL, 'n'}, |
eac6e3f0 RW |
126 | { 0 } |
127 | }; | |
128 | ||
eac6e3f0 RW |
129 | /* SIGHUP handler. */ |
130 | static void | |
131 | sighup(void) | |
132 | { | |
133 | log_info("SIGHUP received"); | |
dbce3582 | 134 | |
dbce3582 | 135 | /* |
e4a82008 RW |
136 | * Do a full configuration reload. In other words, reset vty_conf |
137 | * and build a new configuartion from scratch. | |
dbce3582 | 138 | */ |
e4a82008 RW |
139 | ldp_config_reset(vty_conf); |
140 | vty_read_config(ldpd_di.config_file, config_default); | |
141 | ldp_config_apply(NULL, vty_conf); | |
eac6e3f0 RW |
142 | } |
143 | ||
144 | /* SIGINT / SIGTERM handler. */ | |
145 | static void | |
146 | sigint(void) | |
8429abe0 | 147 | { |
eac6e3f0 RW |
148 | log_info("SIGINT received"); |
149 | ldpd_shutdown(); | |
150 | } | |
8429abe0 | 151 | |
eac6e3f0 RW |
152 | /* SIGUSR1 handler. */ |
153 | static void | |
154 | sigusr1(void) | |
155 | { | |
dd8376fe | 156 | zlog_rotate(); |
8429abe0 RW |
157 | } |
158 | ||
eac6e3f0 RW |
159 | static struct quagga_signal_t ldp_signals[] = |
160 | { | |
161 | { | |
162 | .signal = SIGHUP, | |
163 | .handler = &sighup, | |
164 | }, | |
165 | { | |
166 | .signal = SIGINT, | |
167 | .handler = &sigint, | |
168 | }, | |
169 | { | |
170 | .signal = SIGTERM, | |
171 | .handler = &sigint, | |
172 | }, | |
173 | { | |
174 | .signal = SIGUSR1, | |
175 | .handler = &sigusr1, | |
176 | } | |
177 | }; | |
178 | ||
4f04a76b DL |
179 | FRR_DAEMON_INFO(ldpd, LDP, |
180 | .vty_port = LDP_VTY_PORT, | |
181 | ||
182 | .proghelp = "Implementation of the LDP protocol.", | |
183 | ||
184 | .signals = ldp_signals, | |
185 | .n_signals = array_size(ldp_signals), | |
186 | ||
187 | .privs = &ldpd_privs, | |
188 | ) | |
189 | ||
8429abe0 RW |
190 | int |
191 | main(int argc, char *argv[]) | |
192 | { | |
8429abe0 | 193 | char *saved_argv0; |
eac6e3f0 | 194 | int lflag = 0, eflag = 0; |
28e8294c RW |
195 | int pipe_parent2ldpe[2], pipe_parent2ldpe_sync[2]; |
196 | int pipe_parent2lde[2], pipe_parent2lde_sync[2]; | |
372b8bd3 | 197 | char *ctl_sock_name; |
8429abe0 | 198 | |
8429abe0 | 199 | ldpd_process = PROC_MAIN; |
fa68f9da | 200 | log_procname = log_procnames[ldpd_process]; |
8429abe0 | 201 | |
8429abe0 RW |
202 | saved_argv0 = argv[0]; |
203 | if (saved_argv0 == NULL) | |
eac6e3f0 | 204 | saved_argv0 = (char *)"ldpd"; |
8429abe0 | 205 | |
4f04a76b | 206 | frr_preinit(&ldpd_di, argc, argv); |
fea12efb | 207 | frr_opt_add("LEn:", longopts, |
208 | " --ctl_socket Override ctl socket path\n" | |
209 | "-n, --instance Instance id\n"); | |
4f04a76b | 210 | |
eac6e3f0 RW |
211 | while (1) { |
212 | int opt; | |
213 | ||
4f04a76b | 214 | opt = frr_getopt(argc, argv, NULL); |
eac6e3f0 RW |
215 | |
216 | if (opt == EOF) | |
8429abe0 | 217 | break; |
eac6e3f0 RW |
218 | |
219 | switch (opt) { | |
220 | case 0: | |
221 | break; | |
372b8bd3 RW |
222 | case OPTION_CTLSOCK: |
223 | ctl_sock_name = strrchr(LDPD_SOCKET, '/'); | |
224 | if (ctl_sock_name) | |
225 | /* skip '/' */ | |
226 | ctl_sock_name++; | |
227 | else | |
228 | /* | |
229 | * LDPD_SOCKET configured as relative path | |
230 | * during config? Should really never happen for | |
231 | * sensible config | |
232 | */ | |
233 | ctl_sock_name = (char *)LDPD_SOCKET; | |
274f5abf | 234 | strlcpy(ctl_sock_path, optarg, sizeof(ctl_sock_path)); |
372b8bd3 RW |
235 | strlcat(ctl_sock_path, "/", sizeof(ctl_sock_path)); |
236 | strlcat(ctl_sock_path, ctl_sock_name, | |
237 | sizeof(ctl_sock_path)); | |
238 | break; | |
fea12efb | 239 | case 'n': |
274f5abf RW |
240 | init.instance = atoi(optarg); |
241 | if (init.instance < 1) | |
fea12efb | 242 | exit(0); |
243 | break; | |
8429abe0 RW |
244 | case 'L': |
245 | lflag = 1; | |
246 | break; | |
247 | case 'E': | |
248 | eflag = 1; | |
249 | break; | |
250 | default: | |
4f04a76b | 251 | frr_help_exit(1); |
eac6e3f0 | 252 | break; |
8429abe0 RW |
253 | } |
254 | } | |
255 | ||
274f5abf RW |
256 | strlcpy(init.user, ldpd_privs.user, sizeof(init.user)); |
257 | strlcpy(init.group, ldpd_privs.group, sizeof(init.group)); | |
258 | strlcpy(init.ctl_sock_path, ctl_sock_path, sizeof(init.ctl_sock_path)); | |
689f5a8c | 259 | strlcpy(init.zclient_serv_path, frr_zclientpath, |
274f5abf | 260 | sizeof(init.zclient_serv_path)); |
4f04a76b | 261 | |
8429abe0 RW |
262 | argc -= optind; |
263 | argv += optind; | |
264 | if (argc > 0 || (lflag && eflag)) | |
4f04a76b | 265 | frr_help_exit(1); |
8429abe0 | 266 | |
eac6e3f0 RW |
267 | /* check for root privileges */ |
268 | if (geteuid() != 0) { | |
269 | errno = EPERM; | |
4f04a76b | 270 | perror(ldpd_di.progname); |
8429abe0 RW |
271 | exit(1); |
272 | } | |
273 | ||
16156c10 DL |
274 | if (lflag || eflag) |
275 | openzlog(ldpd_di.progname, "LDP", 0, | |
276 | LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON); | |
eac6e3f0 | 277 | if (lflag) |
274f5abf | 278 | lde(); |
eac6e3f0 | 279 | else if (eflag) |
274f5abf | 280 | ldpe(); |
8429abe0 | 281 | |
eac6e3f0 | 282 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_parent2ldpe) == -1) |
8429abe0 | 283 | fatal("socketpair"); |
28e8294c RW |
284 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, |
285 | pipe_parent2ldpe_sync) == -1) | |
286 | fatal("socketpair"); | |
eac6e3f0 | 287 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_parent2lde) == -1) |
8429abe0 | 288 | fatal("socketpair"); |
28e8294c RW |
289 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, |
290 | pipe_parent2lde_sync) == -1) | |
291 | fatal("socketpair"); | |
eac6e3f0 RW |
292 | sock_set_nonblock(pipe_parent2ldpe[0]); |
293 | sock_set_cloexec(pipe_parent2ldpe[0]); | |
294 | sock_set_nonblock(pipe_parent2ldpe[1]); | |
295 | sock_set_cloexec(pipe_parent2ldpe[1]); | |
28e8294c RW |
296 | sock_set_nonblock(pipe_parent2ldpe_sync[0]); |
297 | sock_set_cloexec(pipe_parent2ldpe_sync[0]); | |
298 | sock_set_cloexec(pipe_parent2ldpe_sync[1]); | |
eac6e3f0 RW |
299 | sock_set_nonblock(pipe_parent2lde[0]); |
300 | sock_set_cloexec(pipe_parent2lde[0]); | |
301 | sock_set_nonblock(pipe_parent2lde[1]); | |
302 | sock_set_cloexec(pipe_parent2lde[1]); | |
28e8294c RW |
303 | sock_set_nonblock(pipe_parent2lde_sync[0]); |
304 | sock_set_cloexec(pipe_parent2lde_sync[0]); | |
305 | sock_set_cloexec(pipe_parent2lde_sync[1]); | |
8429abe0 RW |
306 | |
307 | /* start children */ | |
308 | lde_pid = start_child(PROC_LDE_ENGINE, saved_argv0, | |
274f5abf | 309 | pipe_parent2lde[1], pipe_parent2lde_sync[1]); |
8429abe0 | 310 | ldpe_pid = start_child(PROC_LDP_ENGINE, saved_argv0, |
274f5abf | 311 | pipe_parent2ldpe[1], pipe_parent2ldpe_sync[1]); |
8429abe0 | 312 | |
16156c10 | 313 | master = frr_init(); |
83139e57 | 314 | |
83139e57 | 315 | vty_config_lockless(); |
6df85364 | 316 | vrf_init(NULL, NULL, NULL, NULL); |
83139e57 RW |
317 | access_list_init(); |
318 | ldp_vty_init(); | |
eac6e3f0 | 319 | ldp_zebra_init(master); |
8429abe0 | 320 | |
56b8a8d2 | 321 | /* |
e4a82008 RW |
322 | * Create base configuration with sane defaults. All configuration |
323 | * requests (e.g. CLI) act on vty_conf and then call ldp_config_apply() | |
324 | * to merge the changes into ldpd_conf, which contains the actual | |
325 | * running configuration. | |
56b8a8d2 | 326 | */ |
e4a82008 | 327 | ldpd_conf = config_new_empty(); |
1d75a89d | 328 | vty_conf = config_new_empty(); |
1d75a89d | 329 | QOBJ_REG(vty_conf, ldpd_conf); |
56b8a8d2 RW |
330 | |
331 | /* read configuration file and daemonize */ | |
83139e57 RW |
332 | frr_config_fork(); |
333 | ||
e4a82008 RW |
334 | /* apply configuration */ |
335 | ldp_config_apply(NULL, vty_conf); | |
336 | ||
8429abe0 | 337 | /* setup pipes to children */ |
28e8294c RW |
338 | if ((iev_ldpe = calloc(1, sizeof(struct imsgev))) == NULL || |
339 | (iev_ldpe_sync = calloc(1, sizeof(struct imsgev))) == NULL || | |
340 | (iev_lde = calloc(1, sizeof(struct imsgev))) == NULL || | |
341 | (iev_lde_sync = calloc(1, sizeof(struct imsgev))) == NULL) | |
8429abe0 RW |
342 | fatal(NULL); |
343 | imsg_init(&iev_ldpe->ibuf, pipe_parent2ldpe[0]); | |
eac6e3f0 | 344 | iev_ldpe->handler_read = main_dispatch_ldpe; |
66e78ae6 QY |
345 | iev_ldpe->ev_read = NULL; |
346 | thread_add_read(master, iev_ldpe->handler_read, iev_ldpe, iev_ldpe->ibuf.fd, | |
347 | &iev_ldpe->ev_read); | |
eac6e3f0 | 348 | iev_ldpe->handler_write = ldp_write_handler; |
28e8294c RW |
349 | |
350 | imsg_init(&iev_ldpe_sync->ibuf, pipe_parent2ldpe_sync[0]); | |
351 | iev_ldpe_sync->handler_read = main_dispatch_ldpe; | |
66e78ae6 QY |
352 | iev_ldpe_sync->ev_read = NULL; |
353 | thread_add_read(master, iev_ldpe_sync->handler_read, iev_ldpe_sync, iev_ldpe_sync->ibuf.fd, | |
354 | &iev_ldpe_sync->ev_read); | |
28e8294c | 355 | iev_ldpe_sync->handler_write = ldp_write_handler; |
8429abe0 | 356 | |
eac6e3f0 RW |
357 | imsg_init(&iev_lde->ibuf, pipe_parent2lde[0]); |
358 | iev_lde->handler_read = main_dispatch_lde; | |
66e78ae6 QY |
359 | iev_lde->ev_read = NULL; |
360 | thread_add_read(master, iev_lde->handler_read, iev_lde, iev_lde->ibuf.fd, | |
361 | &iev_lde->ev_read); | |
eac6e3f0 | 362 | iev_lde->handler_write = ldp_write_handler; |
28e8294c RW |
363 | |
364 | imsg_init(&iev_lde_sync->ibuf, pipe_parent2lde_sync[0]); | |
365 | iev_lde_sync->handler_read = main_dispatch_lde; | |
66e78ae6 QY |
366 | iev_lde_sync->ev_read = NULL; |
367 | thread_add_read(master, iev_lde_sync->handler_read, iev_lde_sync, iev_lde_sync->ibuf.fd, | |
368 | &iev_lde_sync->ev_read); | |
28e8294c | 369 | iev_lde_sync->handler_write = ldp_write_handler; |
8429abe0 RW |
370 | |
371 | if (main_imsg_send_ipc_sockets(&iev_ldpe->ibuf, &iev_lde->ibuf)) | |
372 | fatal("could not establish imsg links"); | |
eac6e3f0 RW |
373 | main_imsg_compose_both(IMSG_DEBUG_UPDATE, &ldp_debug, |
374 | sizeof(ldp_debug)); | |
08e4b244 | 375 | main_imsg_compose_both(IMSG_INIT, &init, sizeof(init)); |
8429abe0 RW |
376 | main_imsg_send_config(ldpd_conf); |
377 | ||
8429abe0 RW |
378 | if (ldpd_conf->ipv4.flags & F_LDPD_AF_ENABLED) |
379 | main_imsg_send_net_sockets(AF_INET); | |
380 | if (ldpd_conf->ipv6.flags & F_LDPD_AF_ENABLED) | |
381 | main_imsg_send_net_sockets(AF_INET6); | |
382 | ||
16077f2f | 383 | frr_run(master); |
8429abe0 | 384 | |
8429abe0 RW |
385 | /* NOTREACHED */ |
386 | return (0); | |
387 | } | |
388 | ||
eac6e3f0 | 389 | static void |
8429abe0 RW |
390 | ldpd_shutdown(void) |
391 | { | |
392 | pid_t pid; | |
393 | int status; | |
394 | ||
03951374 DL |
395 | frr_early_fini(); |
396 | ||
8429abe0 RW |
397 | /* close pipes */ |
398 | msgbuf_clear(&iev_ldpe->ibuf.w); | |
399 | close(iev_ldpe->ibuf.fd); | |
400 | msgbuf_clear(&iev_lde->ibuf.w); | |
401 | close(iev_lde->ibuf.fd); | |
402 | ||
8429abe0 | 403 | config_clear(ldpd_conf); |
1d75a89d | 404 | |
e4a82008 | 405 | ldp_config_reset(vty_conf); |
1d75a89d RW |
406 | QOBJ_UNREG(vty_conf); |
407 | free(vty_conf); | |
8429abe0 RW |
408 | |
409 | log_debug("waiting for children to terminate"); | |
410 | do { | |
411 | pid = wait(&status); | |
412 | if (pid == -1) { | |
413 | if (errno != EINTR && errno != ECHILD) | |
414 | fatal("wait"); | |
415 | } else if (WIFSIGNALED(status)) | |
416 | log_warnx("%s terminated; signal %d", | |
417 | (pid == lde_pid) ? "label decision engine" : | |
418 | "ldp engine", WTERMSIG(status)); | |
419 | } while (pid != -1 || (pid == -1 && errno == EINTR)); | |
420 | ||
421 | free(iev_ldpe); | |
422 | free(iev_lde); | |
423 | ||
424 | log_info("terminating"); | |
64dffe25 RW |
425 | |
426 | vrf_terminate(); | |
45a8eba9 | 427 | access_list_reset(); |
64dffe25 | 428 | ldp_zebra_destroy(); |
64dffe25 | 429 | |
03951374 | 430 | frr_fini(); |
8429abe0 RW |
431 | exit(0); |
432 | } | |
433 | ||
434 | static pid_t | |
274f5abf | 435 | start_child(enum ldpd_process p, char *argv0, int fd_async, int fd_sync) |
8429abe0 | 436 | { |
274f5abf | 437 | char *argv[3]; |
8429abe0 RW |
438 | int argc = 0; |
439 | pid_t pid; | |
440 | ||
441 | switch (pid = fork()) { | |
442 | case -1: | |
443 | fatal("cannot fork"); | |
444 | case 0: | |
445 | break; | |
446 | default: | |
28e8294c RW |
447 | close(fd_async); |
448 | close(fd_sync); | |
8429abe0 RW |
449 | return (pid); |
450 | } | |
451 | ||
28e8294c RW |
452 | if (dup2(fd_async, LDPD_FD_ASYNC) == -1) |
453 | fatal("cannot setup imsg async fd"); | |
454 | if (dup2(fd_sync, LDPD_FD_SYNC) == -1) | |
455 | fatal("cannot setup imsg sync fd"); | |
8429abe0 RW |
456 | |
457 | argv[argc++] = argv0; | |
458 | switch (p) { | |
459 | case PROC_MAIN: | |
460 | fatalx("Can not start main process"); | |
461 | case PROC_LDE_ENGINE: | |
eac6e3f0 | 462 | argv[argc++] = (char *)"-L"; |
8429abe0 RW |
463 | break; |
464 | case PROC_LDP_ENGINE: | |
eac6e3f0 | 465 | argv[argc++] = (char *)"-E"; |
8429abe0 RW |
466 | break; |
467 | } | |
8429abe0 RW |
468 | argv[argc++] = NULL; |
469 | ||
470 | execvp(argv0, argv); | |
471 | fatal("execvp"); | |
472 | } | |
473 | ||
474 | /* imsg handling */ | |
475 | /* ARGSUSED */ | |
eac6e3f0 RW |
476 | static int |
477 | main_dispatch_ldpe(struct thread *thread) | |
8429abe0 | 478 | { |
eac6e3f0 | 479 | struct imsgev *iev = THREAD_ARG(thread); |
8429abe0 RW |
480 | struct imsgbuf *ibuf = &iev->ibuf; |
481 | struct imsg imsg; | |
482 | int af; | |
483 | ssize_t n; | |
eac6e3f0 | 484 | int shut = 0; |
8429abe0 | 485 | |
eac6e3f0 RW |
486 | iev->ev_read = NULL; |
487 | ||
488 | if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) | |
489 | fatal("imsg_read error"); | |
490 | if (n == 0) /* connection closed */ | |
491 | shut = 1; | |
8429abe0 RW |
492 | |
493 | for (;;) { | |
494 | if ((n = imsg_get(ibuf, &imsg)) == -1) | |
495 | fatal("imsg_get"); | |
496 | ||
497 | if (n == 0) | |
498 | break; | |
499 | ||
500 | switch (imsg.hdr.type) { | |
eac6e3f0 RW |
501 | case IMSG_LOG: |
502 | logit(imsg.hdr.pid, "%s", (const char *)imsg.data); | |
503 | break; | |
8429abe0 RW |
504 | case IMSG_REQUEST_SOCKETS: |
505 | af = imsg.hdr.pid; | |
506 | main_imsg_send_net_sockets(af); | |
507 | break; | |
45a8eba9 RW |
508 | case IMSG_ACL_CHECK: |
509 | if (imsg.hdr.len != IMSG_HEADER_SIZE + | |
510 | sizeof(struct acl_check)) | |
511 | fatalx("IMSG_ACL_CHECK imsg with wrong len"); | |
512 | ldp_acl_reply(iev, (struct acl_check *)imsg.data); | |
513 | break; | |
8429abe0 RW |
514 | default: |
515 | log_debug("%s: error handling imsg %d", __func__, | |
516 | imsg.hdr.type); | |
517 | break; | |
518 | } | |
519 | imsg_free(&imsg); | |
520 | } | |
521 | if (!shut) | |
522 | imsg_event_add(iev); | |
523 | else { | |
eac6e3f0 RW |
524 | /* this pipe is dead, so remove the event handlers and exit */ |
525 | THREAD_READ_OFF(iev->ev_read); | |
526 | THREAD_WRITE_OFF(iev->ev_write); | |
527 | ldpe_pid = 0; | |
528 | if (lde_pid == 0) | |
529 | ldpd_shutdown(); | |
530 | else | |
531 | kill(lde_pid, SIGTERM); | |
8429abe0 | 532 | } |
eac6e3f0 RW |
533 | |
534 | return (0); | |
8429abe0 RW |
535 | } |
536 | ||
537 | /* ARGSUSED */ | |
eac6e3f0 RW |
538 | static int |
539 | main_dispatch_lde(struct thread *thread) | |
8429abe0 | 540 | { |
eac6e3f0 | 541 | struct imsgev *iev = THREAD_ARG(thread); |
8429abe0 RW |
542 | struct imsgbuf *ibuf = &iev->ibuf; |
543 | struct imsg imsg; | |
544 | ssize_t n; | |
545 | int shut = 0; | |
546 | ||
eac6e3f0 RW |
547 | iev->ev_read = NULL; |
548 | ||
549 | if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) | |
550 | fatal("imsg_read error"); | |
551 | if (n == 0) /* connection closed */ | |
552 | shut = 1; | |
8429abe0 RW |
553 | |
554 | for (;;) { | |
555 | if ((n = imsg_get(ibuf, &imsg)) == -1) | |
556 | fatal("imsg_get"); | |
557 | ||
558 | if (n == 0) | |
559 | break; | |
560 | ||
561 | switch (imsg.hdr.type) { | |
eac6e3f0 RW |
562 | case IMSG_LOG: |
563 | logit(imsg.hdr.pid, "%s", (const char *)imsg.data); | |
564 | break; | |
8429abe0 RW |
565 | case IMSG_KLABEL_CHANGE: |
566 | if (imsg.hdr.len - IMSG_HEADER_SIZE != | |
567 | sizeof(struct kroute)) | |
568 | fatalx("invalid size of IMSG_KLABEL_CHANGE"); | |
569 | if (kr_change(imsg.data)) | |
570 | log_warnx("%s: error changing route", __func__); | |
571 | break; | |
572 | case IMSG_KLABEL_DELETE: | |
573 | if (imsg.hdr.len - IMSG_HEADER_SIZE != | |
574 | sizeof(struct kroute)) | |
575 | fatalx("invalid size of IMSG_KLABEL_DELETE"); | |
576 | if (kr_delete(imsg.data)) | |
577 | log_warnx("%s: error deleting route", __func__); | |
578 | break; | |
87b5f1b7 RW |
579 | case IMSG_KPW_ADD: |
580 | case IMSG_KPW_DELETE: | |
581 | case IMSG_KPW_SET: | |
582 | case IMSG_KPW_UNSET: | |
8429abe0 | 583 | if (imsg.hdr.len - IMSG_HEADER_SIZE != |
87b5f1b7 | 584 | sizeof(struct zapi_pw)) |
8429abe0 | 585 | fatalx("invalid size of IMSG_KPWLABEL_CHANGE"); |
87b5f1b7 RW |
586 | |
587 | switch (imsg.hdr.type) { | |
588 | case IMSG_KPW_ADD: | |
589 | if (kmpw_add(imsg.data)) | |
590 | log_warnx("%s: error adding " | |
591 | "pseudowire", __func__); | |
592 | break; | |
593 | case IMSG_KPW_DELETE: | |
594 | if (kmpw_del(imsg.data)) | |
595 | log_warnx("%s: error deleting " | |
596 | "pseudowire", __func__); | |
597 | break; | |
598 | case IMSG_KPW_SET: | |
599 | if (kmpw_set(imsg.data)) | |
600 | log_warnx("%s: error setting " | |
601 | "pseudowire", __func__); | |
602 | break; | |
603 | case IMSG_KPW_UNSET: | |
604 | if (kmpw_unset(imsg.data)) | |
605 | log_warnx("%s: error unsetting " | |
606 | "pseudowire", __func__); | |
607 | break; | |
608 | } | |
8429abe0 | 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 | 692 | if (iev->handler_read) |
ffa2c898 QY |
693 | thread_add_read(master, iev->handler_read, iev, iev->ibuf.fd, |
694 | &iev->ev_read); | |
8429abe0 | 695 | |
28e8294c | 696 | if (iev->handler_write && iev->ibuf.w.queued) |
ffa2c898 QY |
697 | thread_add_write(master, iev->handler_write, iev, |
698 | iev->ibuf.fd, &iev->ev_write); | |
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) | |
ffa2c898 QY |
724 | thread_add_write(master, eb->handler, eb->arg, eb->wbuf.fd, |
725 | &eb->ev); | |
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 |
e4a82008 | 961 | ldp_config_apply(struct vty *vty, struct ldpd_conf *xconf) |
8429abe0 | 962 | { |
e4a82008 RW |
963 | /* |
964 | * When reading from a configuration file (startup and sighup), we | |
965 | * call merge_config() only once after the whole config has been read. | |
966 | * This is the optimal and least disruptive way to update the running | |
967 | * configuration. | |
968 | */ | |
969 | if (vty && vty->type == VTY_FILE) | |
dbce3582 RW |
970 | return (0); |
971 | ||
56b8a8d2 | 972 | ldp_config_normalize(xconf); |
8429abe0 RW |
973 | |
974 | if (main_imsg_send_config(xconf) == -1) | |
975 | return (-1); | |
976 | ||
56b8a8d2 | 977 | merge_config(ldpd_conf, xconf); |
8429abe0 | 978 | |
56b8a8d2 | 979 | return (0); |
b67c140b RW |
980 | } |
981 | ||
eac6e3f0 | 982 | static void |
56b8a8d2 | 983 | ldp_config_normalize(struct ldpd_conf *xconf) |
eac6e3f0 | 984 | { |
7d0eeac1 RW |
985 | struct iface *iface, *itmp; |
986 | struct nbr_params *nbrp, *ntmp; | |
eac6e3f0 | 987 | struct l2vpn *l2vpn; |
7d0eeac1 | 988 | struct l2vpn_pw *pw, *ptmp; |
eac6e3f0 RW |
989 | |
990 | if (!(xconf->flags & F_LDPD_ENABLED)) | |
56b8a8d2 | 991 | ldp_config_reset_main(xconf); |
eac6e3f0 RW |
992 | else { |
993 | if (!(xconf->ipv4.flags & F_LDPD_AF_ENABLED)) | |
56b8a8d2 | 994 | ldp_config_reset_af(xconf, AF_INET); |
eac6e3f0 | 995 | if (!(xconf->ipv6.flags & F_LDPD_AF_ENABLED)) |
56b8a8d2 | 996 | ldp_config_reset_af(xconf, AF_INET6); |
eac6e3f0 | 997 | |
7d0eeac1 RW |
998 | RB_FOREACH_SAFE(iface, iface_head, &xconf->iface_tree, itmp) { |
999 | if (iface->ipv4.enabled || iface->ipv6.enabled) | |
eac6e3f0 RW |
1000 | continue; |
1001 | ||
1d75a89d | 1002 | QOBJ_UNREG(iface); |
7d0eeac1 RW |
1003 | RB_REMOVE(iface_head, &vty_conf->iface_tree, iface); |
1004 | free(iface); | |
eac6e3f0 | 1005 | } |
7d0eeac1 RW |
1006 | |
1007 | RB_FOREACH_SAFE(nbrp, nbrp_head, &xconf->nbrp_tree, ntmp) { | |
1008 | if (nbrp->flags & (F_NBRP_KEEPALIVE|F_NBRP_GTSM)) | |
1009 | continue; | |
1010 | if (nbrp->auth.method != AUTH_NONE) | |
1011 | continue; | |
1012 | ||
1d75a89d | 1013 | QOBJ_UNREG(nbrp); |
7d0eeac1 RW |
1014 | RB_REMOVE(nbrp_head, &vty_conf->nbrp_tree, nbrp); |
1015 | free(nbrp); | |
1016 | } | |
1017 | } | |
1018 | ||
1019 | RB_FOREACH(l2vpn, l2vpn_head, &xconf->l2vpn_tree) { | |
1020 | RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_tree, ptmp) { | |
1021 | if (!(pw->flags & F_PW_STATIC_NBR_ADDR)) { | |
1022 | pw->af = AF_INET; | |
1023 | pw->addr.v4 = pw->lsr_id; | |
1024 | } | |
1025 | ||
1026 | if (pw->lsr_id.s_addr != INADDR_ANY && pw->pwid != 0) | |
eac6e3f0 | 1027 | continue; |
7d0eeac1 RW |
1028 | RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw); |
1029 | RB_INSERT(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw); | |
1030 | } | |
1031 | RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_inactive_tree, | |
1032 | ptmp) { | |
1033 | if (!(pw->flags & F_PW_STATIC_NBR_ADDR)) { | |
1034 | pw->af = AF_INET; | |
1035 | pw->addr.v4 = pw->lsr_id; | |
1036 | } | |
eac6e3f0 | 1037 | |
7d0eeac1 RW |
1038 | if (pw->lsr_id.s_addr == INADDR_ANY || pw->pwid == 0) |
1039 | continue; | |
1040 | RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw); | |
1041 | RB_INSERT(l2vpn_pw_head, &l2vpn->pw_tree, pw); | |
eac6e3f0 RW |
1042 | } |
1043 | } | |
1044 | } | |
1045 | ||
e4a82008 RW |
1046 | static void |
1047 | ldp_config_reset(struct ldpd_conf *conf) | |
1048 | { | |
1049 | ldp_config_reset_main(conf); | |
1050 | ldp_config_reset_l2vpns(conf); | |
1051 | } | |
1052 | ||
eac6e3f0 | 1053 | static void |
56b8a8d2 | 1054 | ldp_config_reset_main(struct ldpd_conf *conf) |
eac6e3f0 RW |
1055 | { |
1056 | struct iface *iface; | |
1057 | struct nbr_params *nbrp; | |
1058 | ||
45926e58 | 1059 | while ((iface = RB_ROOT(iface_head, &conf->iface_tree)) != NULL) { |
1d75a89d | 1060 | QOBJ_UNREG(iface); |
7d3d7491 | 1061 | RB_REMOVE(iface_head, &conf->iface_tree, iface); |
eac6e3f0 RW |
1062 | free(iface); |
1063 | } | |
1064 | ||
45926e58 | 1065 | while ((nbrp = RB_ROOT(nbrp_head, &conf->nbrp_tree)) != NULL) { |
1d75a89d | 1066 | QOBJ_UNREG(nbrp); |
76c4abd1 | 1067 | RB_REMOVE(nbrp_head, &conf->nbrp_tree, nbrp); |
eac6e3f0 RW |
1068 | free(nbrp); |
1069 | } | |
1070 | ||
1071 | conf->rtr_id.s_addr = INADDR_ANY; | |
56b8a8d2 RW |
1072 | ldp_config_reset_af(conf, AF_INET); |
1073 | ldp_config_reset_af(conf, AF_INET6); | |
eac6e3f0 RW |
1074 | conf->lhello_holdtime = LINK_DFLT_HOLDTIME; |
1075 | conf->lhello_interval = DEFAULT_HELLO_INTERVAL; | |
1076 | conf->thello_holdtime = TARGETED_DFLT_HOLDTIME; | |
1077 | conf->thello_interval = DEFAULT_HELLO_INTERVAL; | |
1078 | conf->trans_pref = DUAL_STACK_LDPOV6; | |
1079 | conf->flags = 0; | |
1080 | } | |
1081 | ||
1082 | static void | |
56b8a8d2 | 1083 | ldp_config_reset_af(struct ldpd_conf *conf, int af) |
eac6e3f0 RW |
1084 | { |
1085 | struct ldpd_af_conf *af_conf; | |
1086 | struct iface *iface; | |
1087 | struct iface_af *ia; | |
1088 | struct tnbr *tnbr, *ttmp; | |
1089 | ||
7d3d7491 | 1090 | RB_FOREACH(iface, iface_head, &conf->iface_tree) { |
eac6e3f0 RW |
1091 | ia = iface_af_get(iface, af); |
1092 | ia->enabled = 0; | |
1093 | } | |
1094 | ||
7989cdba | 1095 | RB_FOREACH_SAFE(tnbr, tnbr_head, &conf->tnbr_tree, ttmp) { |
eac6e3f0 RW |
1096 | if (tnbr->af != af) |
1097 | continue; | |
1098 | ||
1d75a89d | 1099 | QOBJ_UNREG(tnbr); |
7989cdba | 1100 | RB_REMOVE(tnbr_head, &conf->tnbr_tree, tnbr); |
eac6e3f0 RW |
1101 | free(tnbr); |
1102 | } | |
1103 | ||
1104 | af_conf = ldp_af_conf_get(conf, af); | |
1105 | af_conf->keepalive = 180; | |
1106 | af_conf->lhello_holdtime = 0; | |
1107 | af_conf->lhello_interval = 0; | |
1108 | af_conf->thello_holdtime = 0; | |
1109 | af_conf->thello_interval = 0; | |
1110 | memset(&af_conf->trans_addr, 0, sizeof(af_conf->trans_addr)); | |
1111 | af_conf->flags = 0; | |
1112 | } | |
1113 | ||
1d75a89d RW |
1114 | static void |
1115 | ldp_config_reset_l2vpns(struct ldpd_conf *conf) | |
eac6e3f0 | 1116 | { |
1d75a89d RW |
1117 | struct l2vpn *l2vpn; |
1118 | struct l2vpn_if *lif; | |
1119 | struct l2vpn_pw *pw; | |
eac6e3f0 | 1120 | |
45926e58 RZ |
1121 | while ((l2vpn = RB_ROOT(l2vpn_head, &conf->l2vpn_tree)) != NULL) { |
1122 | while ((lif = RB_ROOT(l2vpn_if_head, | |
1123 | &l2vpn->if_tree)) != NULL) { | |
1d75a89d RW |
1124 | QOBJ_UNREG(lif); |
1125 | RB_REMOVE(l2vpn_if_head, &l2vpn->if_tree, lif); | |
1126 | free(lif); | |
eac6e3f0 | 1127 | } |
45926e58 | 1128 | while ((pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_tree)) != NULL) { |
1d75a89d RW |
1129 | QOBJ_UNREG(pw); |
1130 | RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw); | |
1131 | free(pw); | |
eac6e3f0 | 1132 | } |
45926e58 RZ |
1133 | while ((pw = RB_ROOT(l2vpn_pw_head, |
1134 | &l2vpn->pw_inactive_tree)) != NULL) { | |
1d75a89d RW |
1135 | QOBJ_UNREG(pw); |
1136 | RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw); | |
1137 | free(pw); | |
eac6e3f0 | 1138 | } |
1d75a89d RW |
1139 | QOBJ_UNREG(l2vpn); |
1140 | RB_REMOVE(l2vpn_head, &conf->l2vpn_tree, l2vpn); | |
1141 | free(l2vpn); | |
eac6e3f0 | 1142 | } |
eac6e3f0 RW |
1143 | } |
1144 | ||
1145 | void | |
1146 | ldp_clear_config(struct ldpd_conf *xconf) | |
1147 | { | |
1148 | struct iface *iface; | |
1149 | struct tnbr *tnbr; | |
1150 | struct nbr_params *nbrp; | |
1151 | struct l2vpn *l2vpn; | |
1152 | ||
45926e58 | 1153 | while ((iface = RB_ROOT(iface_head, &xconf->iface_tree)) != NULL) { |
7d3d7491 | 1154 | RB_REMOVE(iface_head, &xconf->iface_tree, iface); |
eac6e3f0 RW |
1155 | free(iface); |
1156 | } | |
45926e58 | 1157 | while ((tnbr = RB_ROOT(tnbr_head, &xconf->tnbr_tree)) != NULL) { |
7989cdba | 1158 | RB_REMOVE(tnbr_head, &xconf->tnbr_tree, tnbr); |
eac6e3f0 RW |
1159 | free(tnbr); |
1160 | } | |
45926e58 | 1161 | while ((nbrp = RB_ROOT(nbrp_head, &xconf->nbrp_tree)) != NULL) { |
76c4abd1 | 1162 | RB_REMOVE(nbrp_head, &xconf->nbrp_tree, nbrp); |
eac6e3f0 RW |
1163 | free(nbrp); |
1164 | } | |
45926e58 | 1165 | while ((l2vpn = RB_ROOT(l2vpn_head, &xconf->l2vpn_tree)) != NULL) { |
90d7e7bd | 1166 | RB_REMOVE(l2vpn_head, &xconf->l2vpn_tree, l2vpn); |
eac6e3f0 RW |
1167 | l2vpn_del(l2vpn); |
1168 | } | |
1169 | ||
1170 | free(xconf); | |
1171 | } | |
1172 | ||
1d75a89d RW |
1173 | #define COPY(a, b) do { \ |
1174 | a = malloc(sizeof(*a)); \ | |
1175 | if (a == NULL) \ | |
1176 | fatal(__func__); \ | |
1177 | *a = *b; \ | |
1178 | } while (0) | |
1179 | ||
56b8a8d2 RW |
1180 | void |
1181 | merge_config(struct ldpd_conf *conf, struct ldpd_conf *xconf) | |
8429abe0 RW |
1182 | { |
1183 | merge_global(conf, xconf); | |
1184 | merge_af(AF_INET, &conf->ipv4, &xconf->ipv4); | |
1185 | merge_af(AF_INET6, &conf->ipv6, &xconf->ipv6); | |
56b8a8d2 RW |
1186 | merge_ifaces(conf, xconf); |
1187 | merge_tnbrs(conf, xconf); | |
1188 | merge_nbrps(conf, xconf); | |
1189 | merge_l2vpns(conf, xconf); | |
8429abe0 RW |
1190 | } |
1191 | ||
1192 | static void | |
1193 | merge_global(struct ldpd_conf *conf, struct ldpd_conf *xconf) | |
1194 | { | |
1195 | /* change of router-id requires resetting all neighborships */ | |
1196 | if (conf->rtr_id.s_addr != xconf->rtr_id.s_addr) { | |
1197 | if (ldpd_process == PROC_LDP_ENGINE) { | |
45a8eba9 | 1198 | ldpe_reset_nbrs(AF_UNSPEC); |
8429abe0 RW |
1199 | if (conf->rtr_id.s_addr == INADDR_ANY || |
1200 | xconf->rtr_id.s_addr == INADDR_ANY) { | |
1201 | if_update_all(AF_UNSPEC); | |
1202 | tnbr_update_all(AF_UNSPEC); | |
1203 | } | |
1204 | } | |
1205 | conf->rtr_id = xconf->rtr_id; | |
1206 | } | |
1207 | ||
eac6e3f0 RW |
1208 | conf->lhello_holdtime = xconf->lhello_holdtime; |
1209 | conf->lhello_interval = xconf->lhello_interval; | |
1210 | conf->thello_holdtime = xconf->thello_holdtime; | |
1211 | conf->thello_interval = xconf->thello_interval; | |
1212 | ||
8429abe0 RW |
1213 | if (conf->trans_pref != xconf->trans_pref) { |
1214 | if (ldpd_process == PROC_LDP_ENGINE) | |
1215 | ldpe_reset_ds_nbrs(); | |
1216 | conf->trans_pref = xconf->trans_pref; | |
1217 | } | |
1218 | ||
1219 | if ((conf->flags & F_LDPD_DS_CISCO_INTEROP) != | |
1220 | (xconf->flags & F_LDPD_DS_CISCO_INTEROP)) { | |
1221 | if (ldpd_process == PROC_LDP_ENGINE) | |
1222 | ldpe_reset_ds_nbrs(); | |
1223 | } | |
1224 | ||
1225 | conf->flags = xconf->flags; | |
1226 | } | |
1227 | ||
1228 | static void | |
1229 | merge_af(int af, struct ldpd_af_conf *af_conf, struct ldpd_af_conf *xa) | |
1230 | { | |
45a8eba9 RW |
1231 | int stop_init_backoff = 0; |
1232 | int remove_dynamic_tnbrs = 0; | |
1233 | int change_egress_label = 0; | |
1234 | int reset_nbrs_ipv4 = 0; | |
1235 | int reset_nbrs = 0; | |
1236 | int update_sockets = 0; | |
1237 | ||
1238 | /* update timers */ | |
8429abe0 RW |
1239 | if (af_conf->keepalive != xa->keepalive) { |
1240 | af_conf->keepalive = xa->keepalive; | |
45a8eba9 | 1241 | stop_init_backoff = 1; |
8429abe0 | 1242 | } |
eac6e3f0 RW |
1243 | af_conf->lhello_holdtime = xa->lhello_holdtime; |
1244 | af_conf->lhello_interval = xa->lhello_interval; | |
8429abe0 RW |
1245 | af_conf->thello_holdtime = xa->thello_holdtime; |
1246 | af_conf->thello_interval = xa->thello_interval; | |
1247 | ||
1248 | /* update flags */ | |
45a8eba9 | 1249 | if ((af_conf->flags & F_LDPD_AF_THELLO_ACCEPT) && |
8429abe0 | 1250 | !(xa->flags & F_LDPD_AF_THELLO_ACCEPT)) |
45a8eba9 | 1251 | remove_dynamic_tnbrs = 1; |
8429abe0 RW |
1252 | if ((af_conf->flags & F_LDPD_AF_NO_GTSM) != |
1253 | (xa->flags & F_LDPD_AF_NO_GTSM)) { | |
1254 | if (af == AF_INET6) | |
1255 | /* need to set/unset IPV6_MINHOPCOUNT */ | |
1256 | update_sockets = 1; | |
45a8eba9 | 1257 | else |
8429abe0 | 1258 | /* for LDPv4 just resetting the neighbors is enough */ |
45a8eba9 | 1259 | reset_nbrs_ipv4 = 1; |
8429abe0 | 1260 | } |
8429abe0 RW |
1261 | if ((af_conf->flags & F_LDPD_AF_EXPNULL) != |
1262 | (xa->flags & F_LDPD_AF_EXPNULL)) | |
45a8eba9 | 1263 | change_egress_label = 1; |
8429abe0 RW |
1264 | af_conf->flags = xa->flags; |
1265 | ||
45a8eba9 | 1266 | /* update the transport address */ |
8429abe0 RW |
1267 | if (ldp_addrcmp(af, &af_conf->trans_addr, &xa->trans_addr)) { |
1268 | af_conf->trans_addr = xa->trans_addr; | |
1269 | update_sockets = 1; | |
1270 | } | |
1271 | ||
45a8eba9 RW |
1272 | /* update ACLs */ |
1273 | if (strcmp(af_conf->acl_label_advertise_to, | |
1274 | xa->acl_label_advertise_to) || | |
1275 | strcmp(af_conf->acl_label_advertise_for, | |
1276 | xa->acl_label_advertise_for) || | |
1277 | strcmp(af_conf->acl_label_accept_from, | |
1278 | xa->acl_label_accept_from) || | |
1279 | strcmp(af_conf->acl_label_accept_for, | |
1280 | xa->acl_label_accept_for)) | |
1281 | reset_nbrs = 1; | |
1282 | if (strcmp(af_conf->acl_thello_accept_from, xa->acl_thello_accept_from)) | |
1283 | remove_dynamic_tnbrs = 1; | |
1284 | if (strcmp(af_conf->acl_label_expnull_for, xa->acl_label_expnull_for)) | |
1285 | change_egress_label = 1; | |
1286 | strlcpy(af_conf->acl_thello_accept_from, xa->acl_thello_accept_from, | |
1287 | sizeof(af_conf->acl_thello_accept_from)); | |
1288 | strlcpy(af_conf->acl_label_allocate_for, xa->acl_label_allocate_for, | |
1289 | sizeof(af_conf->acl_label_allocate_for)); | |
1290 | strlcpy(af_conf->acl_label_advertise_to, xa->acl_label_advertise_to, | |
1291 | sizeof(af_conf->acl_label_advertise_to)); | |
1292 | strlcpy(af_conf->acl_label_advertise_for, xa->acl_label_advertise_for, | |
1293 | sizeof(af_conf->acl_label_advertise_for)); | |
1294 | strlcpy(af_conf->acl_label_accept_from, xa->acl_label_accept_from, | |
1295 | sizeof(af_conf->acl_label_accept_from)); | |
1296 | strlcpy(af_conf->acl_label_accept_for, xa->acl_label_accept_for, | |
1297 | sizeof(af_conf->acl_label_accept_for)); | |
1298 | strlcpy(af_conf->acl_label_expnull_for, xa->acl_label_expnull_for, | |
1299 | sizeof(af_conf->acl_label_expnull_for)); | |
1300 | ||
1301 | /* apply the new configuration */ | |
1302 | switch (ldpd_process) { | |
1303 | case PROC_LDE_ENGINE: | |
1304 | if (change_egress_label) | |
1305 | lde_change_egress_label(af); | |
1306 | break; | |
1307 | case PROC_LDP_ENGINE: | |
1308 | if (stop_init_backoff) | |
1309 | ldpe_stop_init_backoff(af); | |
1310 | if (remove_dynamic_tnbrs) | |
1311 | ldpe_remove_dynamic_tnbrs(af); | |
1312 | if (reset_nbrs) | |
1313 | ldpe_reset_nbrs(AF_UNSPEC); | |
1314 | else if (reset_nbrs_ipv4) | |
1315 | ldpe_reset_nbrs(AF_INET); | |
1316 | break; | |
1317 | case PROC_MAIN: | |
1318 | if (update_sockets && iev_ldpe) | |
1319 | imsg_compose_event(iev_ldpe, IMSG_CLOSE_SOCKETS, af, | |
1320 | 0, -1, NULL, 0); | |
1321 | break; | |
1322 | } | |
8429abe0 RW |
1323 | } |
1324 | ||
1325 | static void | |
56b8a8d2 | 1326 | merge_ifaces(struct ldpd_conf *conf, struct ldpd_conf *xconf) |
8429abe0 RW |
1327 | { |
1328 | struct iface *iface, *itmp, *xi; | |
1329 | ||
7d3d7491 | 1330 | RB_FOREACH_SAFE(iface, iface_head, &conf->iface_tree, itmp) { |
8429abe0 | 1331 | /* find deleted interfaces */ |
eac6e3f0 | 1332 | if ((xi = if_lookup_name(xconf, iface->name)) == NULL) { |
4af8997d | 1333 | switch (ldpd_process) { |
4af8997d | 1334 | case PROC_LDP_ENGINE: |
1d75a89d | 1335 | ldpe_if_exit(iface); |
4af8997d | 1336 | break; |
1d75a89d | 1337 | case PROC_LDE_ENGINE: |
4af8997d | 1338 | case PROC_MAIN: |
4af8997d RW |
1339 | break; |
1340 | } | |
1d75a89d | 1341 | RB_REMOVE(iface_head, &conf->iface_tree, iface); |
8429abe0 RW |
1342 | free(iface); |
1343 | } | |
1344 | } | |
7d3d7491 | 1345 | RB_FOREACH_SAFE(xi, iface_head, &xconf->iface_tree, itmp) { |
8429abe0 | 1346 | /* find new interfaces */ |
eac6e3f0 | 1347 | if ((iface = if_lookup_name(conf, xi->name)) == NULL) { |
1d75a89d RW |
1348 | COPY(iface, xi); |
1349 | RB_INSERT(iface_head, &conf->iface_tree, iface); | |
8429abe0 | 1350 | |
1d75a89d RW |
1351 | switch (ldpd_process) { |
1352 | case PROC_LDP_ENGINE: | |
1353 | ldpe_if_init(iface); | |
1354 | break; | |
1355 | case PROC_LDE_ENGINE: | |
1356 | break; | |
1357 | case PROC_MAIN: | |
4af8997d | 1358 | /* resend addresses to activate new interfaces */ |
1d75a89d RW |
1359 | kif_redistribute(iface->name); |
1360 | break; | |
4af8997d | 1361 | } |
8429abe0 RW |
1362 | continue; |
1363 | } | |
1364 | ||
1365 | /* update existing interfaces */ | |
1366 | merge_iface_af(&iface->ipv4, &xi->ipv4); | |
1367 | merge_iface_af(&iface->ipv6, &xi->ipv6); | |
8429abe0 RW |
1368 | } |
1369 | } | |
1370 | ||
1371 | static void | |
1372 | merge_iface_af(struct iface_af *ia, struct iface_af *xi) | |
1373 | { | |
1374 | if (ia->enabled != xi->enabled) { | |
1375 | ia->enabled = xi->enabled; | |
1376 | if (ldpd_process == PROC_LDP_ENGINE) | |
61cd1100 | 1377 | ldp_if_update(ia->iface, ia->af); |
8429abe0 RW |
1378 | } |
1379 | ia->hello_holdtime = xi->hello_holdtime; | |
1380 | ia->hello_interval = xi->hello_interval; | |
1381 | } | |
1382 | ||
1383 | static void | |
56b8a8d2 | 1384 | merge_tnbrs(struct ldpd_conf *conf, struct ldpd_conf *xconf) |
8429abe0 RW |
1385 | { |
1386 | struct tnbr *tnbr, *ttmp, *xt; | |
1387 | ||
7989cdba | 1388 | RB_FOREACH_SAFE(tnbr, tnbr_head, &conf->tnbr_tree, ttmp) { |
8429abe0 RW |
1389 | if (!(tnbr->flags & F_TNBR_CONFIGURED)) |
1390 | continue; | |
1391 | ||
1392 | /* find deleted tnbrs */ | |
1393 | if ((xt = tnbr_find(xconf, tnbr->af, &tnbr->addr)) == NULL) { | |
4af8997d | 1394 | switch (ldpd_process) { |
4af8997d | 1395 | case PROC_LDP_ENGINE: |
8429abe0 | 1396 | tnbr->flags &= ~F_TNBR_CONFIGURED; |
7989cdba | 1397 | tnbr_check(conf, tnbr); |
4af8997d | 1398 | break; |
1d75a89d | 1399 | case PROC_LDE_ENGINE: |
4af8997d | 1400 | case PROC_MAIN: |
7989cdba | 1401 | RB_REMOVE(tnbr_head, &conf->tnbr_tree, tnbr); |
8429abe0 | 1402 | free(tnbr); |
4af8997d | 1403 | break; |
8429abe0 RW |
1404 | } |
1405 | } | |
1406 | } | |
7989cdba | 1407 | RB_FOREACH_SAFE(xt, tnbr_head, &xconf->tnbr_tree, ttmp) { |
8429abe0 RW |
1408 | /* find new tnbrs */ |
1409 | if ((tnbr = tnbr_find(conf, xt->af, &xt->addr)) == NULL) { | |
1d75a89d RW |
1410 | COPY(tnbr, xt); |
1411 | RB_INSERT(tnbr_head, &conf->tnbr_tree, tnbr); | |
8429abe0 | 1412 | |
4af8997d | 1413 | switch (ldpd_process) { |
4af8997d | 1414 | case PROC_LDP_ENGINE: |
1d75a89d | 1415 | tnbr_update(tnbr); |
4af8997d | 1416 | break; |
1d75a89d | 1417 | case PROC_LDE_ENGINE: |
4af8997d | 1418 | case PROC_MAIN: |
4af8997d RW |
1419 | break; |
1420 | } | |
8429abe0 RW |
1421 | continue; |
1422 | } | |
1423 | ||
1424 | /* update existing tnbrs */ | |
1425 | if (!(tnbr->flags & F_TNBR_CONFIGURED)) | |
1426 | tnbr->flags |= F_TNBR_CONFIGURED; | |
8429abe0 RW |
1427 | } |
1428 | } | |
1429 | ||
1430 | static void | |
56b8a8d2 | 1431 | merge_nbrps(struct ldpd_conf *conf, struct ldpd_conf *xconf) |
8429abe0 RW |
1432 | { |
1433 | struct nbr_params *nbrp, *ntmp, *xn; | |
1434 | struct nbr *nbr; | |
1435 | int nbrp_changed; | |
1436 | ||
76c4abd1 | 1437 | RB_FOREACH_SAFE(nbrp, nbrp_head, &conf->nbrp_tree, ntmp) { |
8429abe0 RW |
1438 | /* find deleted nbrps */ |
1439 | if ((xn = nbr_params_find(xconf, nbrp->lsr_id)) == NULL) { | |
4af8997d | 1440 | switch (ldpd_process) { |
4af8997d | 1441 | case PROC_LDP_ENGINE: |
8429abe0 RW |
1442 | nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr); |
1443 | if (nbr) { | |
1444 | session_shutdown(nbr, S_SHUTDOWN, 0, 0); | |
eac6e3f0 | 1445 | #ifdef __OpenBSD__ |
8429abe0 | 1446 | pfkey_remove(nbr); |
eac6e3f0 RW |
1447 | #else |
1448 | sock_set_md5sig( | |
1449 | (ldp_af_global_get(&global, | |
1450 | nbr->af))->ldp_session_socket, | |
1451 | nbr->af, &nbr->raddr, NULL); | |
1452 | #endif | |
0f7b5df9 | 1453 | nbr->auth.method = AUTH_NONE; |
8429abe0 RW |
1454 | if (nbr_session_active_role(nbr)) |
1455 | nbr_establish_connection(nbr); | |
1456 | } | |
4af8997d | 1457 | break; |
1d75a89d | 1458 | case PROC_LDE_ENGINE: |
4af8997d | 1459 | case PROC_MAIN: |
4af8997d | 1460 | break; |
8429abe0 | 1461 | } |
76c4abd1 | 1462 | RB_REMOVE(nbrp_head, &conf->nbrp_tree, nbrp); |
8429abe0 RW |
1463 | free(nbrp); |
1464 | } | |
1465 | } | |
76c4abd1 | 1466 | RB_FOREACH_SAFE(xn, nbrp_head, &xconf->nbrp_tree, ntmp) { |
8429abe0 RW |
1467 | /* find new nbrps */ |
1468 | if ((nbrp = nbr_params_find(conf, xn->lsr_id)) == NULL) { | |
1d75a89d RW |
1469 | COPY(nbrp, xn); |
1470 | RB_INSERT(nbrp_head, &conf->nbrp_tree, nbrp); | |
8429abe0 | 1471 | |
4af8997d | 1472 | switch (ldpd_process) { |
4af8997d | 1473 | case PROC_LDP_ENGINE: |
1d75a89d | 1474 | nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr); |
8429abe0 RW |
1475 | if (nbr) { |
1476 | session_shutdown(nbr, S_SHUTDOWN, 0, 0); | |
1d75a89d | 1477 | nbr->auth.method = nbrp->auth.method; |
eac6e3f0 | 1478 | #ifdef __OpenBSD__ |
1d75a89d | 1479 | if (pfkey_establish(nbr, nbrp) == -1) |
8429abe0 | 1480 | fatalx("pfkey setup failed"); |
eac6e3f0 RW |
1481 | #else |
1482 | sock_set_md5sig( | |
1483 | (ldp_af_global_get(&global, | |
1484 | nbr->af))->ldp_session_socket, | |
1485 | nbr->af, &nbr->raddr, | |
1d75a89d | 1486 | nbrp->auth.md5key); |
eac6e3f0 | 1487 | #endif |
8429abe0 RW |
1488 | if (nbr_session_active_role(nbr)) |
1489 | nbr_establish_connection(nbr); | |
1490 | } | |
4af8997d | 1491 | break; |
1d75a89d | 1492 | case PROC_LDE_ENGINE: |
4af8997d | 1493 | case PROC_MAIN: |
4af8997d | 1494 | break; |
8429abe0 RW |
1495 | } |
1496 | continue; | |
1497 | } | |
1498 | ||
1499 | /* update existing nbrps */ | |
1500 | if (nbrp->flags != xn->flags || | |
1501 | nbrp->keepalive != xn->keepalive || | |
1502 | nbrp->gtsm_enabled != xn->gtsm_enabled || | |
1503 | nbrp->gtsm_hops != xn->gtsm_hops || | |
1504 | nbrp->auth.method != xn->auth.method || | |
1505 | strcmp(nbrp->auth.md5key, xn->auth.md5key) != 0) | |
1506 | nbrp_changed = 1; | |
1507 | else | |
1508 | nbrp_changed = 0; | |
1509 | ||
1510 | nbrp->keepalive = xn->keepalive; | |
1511 | nbrp->gtsm_enabled = xn->gtsm_enabled; | |
1512 | nbrp->gtsm_hops = xn->gtsm_hops; | |
1513 | nbrp->auth.method = xn->auth.method; | |
1514 | strlcpy(nbrp->auth.md5key, xn->auth.md5key, | |
1515 | sizeof(nbrp->auth.md5key)); | |
1516 | nbrp->auth.md5key_len = xn->auth.md5key_len; | |
1517 | nbrp->flags = xn->flags; | |
1518 | ||
1519 | if (ldpd_process == PROC_LDP_ENGINE) { | |
1520 | nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr); | |
1521 | if (nbr && nbrp_changed) { | |
1522 | session_shutdown(nbr, S_SHUTDOWN, 0, 0); | |
eac6e3f0 | 1523 | #ifdef __OpenBSD__ |
8429abe0 | 1524 | pfkey_remove(nbr); |
0f7b5df9 | 1525 | nbr->auth.method = nbrp->auth.method; |
8429abe0 RW |
1526 | if (pfkey_establish(nbr, nbrp) == -1) |
1527 | fatalx("pfkey setup failed"); | |
eac6e3f0 | 1528 | #else |
0f7b5df9 | 1529 | nbr->auth.method = nbrp->auth.method; |
eac6e3f0 RW |
1530 | sock_set_md5sig((ldp_af_global_get(&global, |
1531 | nbr->af))->ldp_session_socket, nbr->af, | |
1532 | &nbr->raddr, nbrp->auth.md5key); | |
1533 | #endif | |
8429abe0 RW |
1534 | if (nbr_session_active_role(nbr)) |
1535 | nbr_establish_connection(nbr); | |
1536 | } | |
1537 | } | |
8429abe0 RW |
1538 | } |
1539 | } | |
1540 | ||
1541 | static void | |
56b8a8d2 | 1542 | merge_l2vpns(struct ldpd_conf *conf, struct ldpd_conf *xconf) |
8429abe0 RW |
1543 | { |
1544 | struct l2vpn *l2vpn, *ltmp, *xl; | |
1545 | ||
90d7e7bd | 1546 | RB_FOREACH_SAFE(l2vpn, l2vpn_head, &conf->l2vpn_tree, ltmp) { |
8429abe0 RW |
1547 | /* find deleted l2vpns */ |
1548 | if ((xl = l2vpn_find(xconf, l2vpn->name)) == NULL) { | |
8429abe0 RW |
1549 | switch (ldpd_process) { |
1550 | case PROC_LDE_ENGINE: | |
1551 | l2vpn_exit(l2vpn); | |
1552 | break; | |
1553 | case PROC_LDP_ENGINE: | |
1554 | ldpe_l2vpn_exit(l2vpn); | |
1555 | break; | |
1556 | case PROC_MAIN: | |
1557 | break; | |
1558 | } | |
1d75a89d | 1559 | RB_REMOVE(l2vpn_head, &conf->l2vpn_tree, l2vpn); |
8429abe0 RW |
1560 | l2vpn_del(l2vpn); |
1561 | } | |
1562 | } | |
90d7e7bd | 1563 | RB_FOREACH_SAFE(xl, l2vpn_head, &xconf->l2vpn_tree, ltmp) { |
8429abe0 RW |
1564 | /* find new l2vpns */ |
1565 | if ((l2vpn = l2vpn_find(conf, xl->name)) == NULL) { | |
1d75a89d RW |
1566 | COPY(l2vpn, xl); |
1567 | RB_INSERT(l2vpn_head, &conf->l2vpn_tree, l2vpn); | |
45926e58 RZ |
1568 | RB_INIT(l2vpn_if_head, &l2vpn->if_tree); |
1569 | RB_INIT(l2vpn_pw_head, &l2vpn->pw_tree); | |
1570 | RB_INIT(l2vpn_pw_head, &l2vpn->pw_inactive_tree); | |
8429abe0 RW |
1571 | |
1572 | switch (ldpd_process) { | |
1573 | case PROC_LDE_ENGINE: | |
1d75a89d | 1574 | l2vpn_init(l2vpn); |
8429abe0 RW |
1575 | break; |
1576 | case PROC_LDP_ENGINE: | |
1d75a89d | 1577 | ldpe_l2vpn_init(l2vpn); |
8429abe0 RW |
1578 | break; |
1579 | case PROC_MAIN: | |
1580 | break; | |
1581 | } | |
8429abe0 RW |
1582 | } |
1583 | ||
1584 | /* update existing l2vpns */ | |
56b8a8d2 | 1585 | merge_l2vpn(conf, l2vpn, xl); |
8429abe0 RW |
1586 | } |
1587 | } | |
1588 | ||
1589 | static void | |
56b8a8d2 | 1590 | merge_l2vpn(struct ldpd_conf *xconf, struct l2vpn *l2vpn, struct l2vpn *xl) |
8429abe0 RW |
1591 | { |
1592 | struct l2vpn_if *lif, *ftmp, *xf; | |
1593 | struct l2vpn_pw *pw, *ptmp, *xp; | |
1594 | struct nbr *nbr; | |
1595 | int reset_nbr, reinstall_pwfec, reinstall_tnbr; | |
1596 | int previous_pw_type, previous_mtu; | |
1597 | ||
1598 | previous_pw_type = l2vpn->pw_type; | |
1599 | previous_mtu = l2vpn->mtu; | |
1600 | ||
1601 | /* merge intefaces */ | |
029c1958 | 1602 | RB_FOREACH_SAFE(lif, l2vpn_if_head, &l2vpn->if_tree, ftmp) { |
8429abe0 | 1603 | /* find deleted interfaces */ |
52bd4c23 | 1604 | if ((xf = l2vpn_if_find(xl, lif->ifname)) == NULL) { |
029c1958 | 1605 | RB_REMOVE(l2vpn_if_head, &l2vpn->if_tree, lif); |
8429abe0 RW |
1606 | free(lif); |
1607 | } | |
1608 | } | |
029c1958 | 1609 | RB_FOREACH_SAFE(xf, l2vpn_if_head, &xl->if_tree, ftmp) { |
8429abe0 | 1610 | /* find new interfaces */ |
52bd4c23 | 1611 | if ((lif = l2vpn_if_find(l2vpn, xf->ifname)) == NULL) { |
1d75a89d RW |
1612 | COPY(lif, xf); |
1613 | RB_INSERT(l2vpn_if_head, &l2vpn->if_tree, lif); | |
1614 | lif->l2vpn = l2vpn; | |
1615 | ||
1616 | switch (ldpd_process) { | |
1617 | case PROC_LDP_ENGINE: | |
1618 | case PROC_LDE_ENGINE: | |
1619 | break; | |
1620 | case PROC_MAIN: | |
1621 | kif_redistribute(lif->ifname); | |
1622 | break; | |
52b530fc | 1623 | } |
8429abe0 | 1624 | } |
8429abe0 RW |
1625 | } |
1626 | ||
eac6e3f0 | 1627 | /* merge active pseudowires */ |
20bacaeb | 1628 | RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_tree, ptmp) { |
eac6e3f0 | 1629 | /* find deleted active pseudowires */ |
5c3f00af | 1630 | if ((xp = l2vpn_pw_find_active(xl, pw->ifname)) == NULL) { |
8429abe0 RW |
1631 | switch (ldpd_process) { |
1632 | case PROC_LDE_ENGINE: | |
1633 | l2vpn_pw_exit(pw); | |
1634 | break; | |
1635 | case PROC_LDP_ENGINE: | |
1636 | ldpe_l2vpn_pw_exit(pw); | |
1637 | break; | |
1638 | case PROC_MAIN: | |
1639 | break; | |
1640 | } | |
1641 | ||
20bacaeb | 1642 | RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw); |
8429abe0 RW |
1643 | free(pw); |
1644 | } | |
1645 | } | |
20bacaeb | 1646 | RB_FOREACH_SAFE(xp, l2vpn_pw_head, &xl->pw_tree, ptmp) { |
eac6e3f0 | 1647 | /* find new active pseudowires */ |
5c3f00af | 1648 | if ((pw = l2vpn_pw_find_active(l2vpn, xp->ifname)) == NULL) { |
1d75a89d RW |
1649 | COPY(pw, xp); |
1650 | RB_INSERT(l2vpn_pw_head, &l2vpn->pw_tree, pw); | |
1651 | pw->l2vpn = l2vpn; | |
8429abe0 RW |
1652 | |
1653 | switch (ldpd_process) { | |
1654 | case PROC_LDE_ENGINE: | |
1d75a89d | 1655 | l2vpn_pw_init(pw); |
8429abe0 RW |
1656 | break; |
1657 | case PROC_LDP_ENGINE: | |
1d75a89d | 1658 | ldpe_l2vpn_pw_init(pw); |
8429abe0 RW |
1659 | break; |
1660 | case PROC_MAIN: | |
1d75a89d | 1661 | kif_redistribute(pw->ifname); |
8429abe0 RW |
1662 | break; |
1663 | } | |
1664 | continue; | |
1665 | } | |
1666 | ||
eac6e3f0 | 1667 | /* update existing active pseudowire */ |
8429abe0 RW |
1668 | if (pw->af != xp->af || |
1669 | ldp_addrcmp(pw->af, &pw->addr, &xp->addr)) | |
1670 | reinstall_tnbr = 1; | |
1671 | else | |
1672 | reinstall_tnbr = 0; | |
1673 | ||
1674 | /* changes that require a session restart */ | |
1675 | if ((pw->flags & (F_PW_STATUSTLV_CONF|F_PW_CWORD_CONF)) != | |
1676 | (xp->flags & (F_PW_STATUSTLV_CONF|F_PW_CWORD_CONF))) | |
1677 | reset_nbr = 1; | |
1678 | else | |
1679 | reset_nbr = 0; | |
1680 | ||
1681 | if (l2vpn->pw_type != xl->pw_type || l2vpn->mtu != xl->mtu || | |
1682 | pw->pwid != xp->pwid || reinstall_tnbr || reset_nbr || | |
1683 | pw->lsr_id.s_addr != xp->lsr_id.s_addr) | |
1684 | reinstall_pwfec = 1; | |
1685 | else | |
1686 | reinstall_pwfec = 0; | |
1687 | ||
1688 | if (ldpd_process == PROC_LDP_ENGINE) { | |
1689 | if (reinstall_tnbr) | |
1690 | ldpe_l2vpn_pw_exit(pw); | |
1691 | if (reset_nbr) { | |
1692 | nbr = nbr_find_ldpid(pw->lsr_id.s_addr); | |
1693 | if (nbr && nbr->state == NBR_STA_OPER) | |
1694 | session_shutdown(nbr, S_SHUTDOWN, 0, 0); | |
1695 | } | |
1696 | } | |
be0fbdeb | 1697 | if (ldpd_process == PROC_LDE_ENGINE && reinstall_pwfec) |
8429abe0 RW |
1698 | l2vpn_pw_exit(pw); |
1699 | pw->lsr_id = xp->lsr_id; | |
1700 | pw->af = xp->af; | |
1701 | pw->addr = xp->addr; | |
1702 | pw->pwid = xp->pwid; | |
1703 | strlcpy(pw->ifname, xp->ifname, sizeof(pw->ifname)); | |
1704 | pw->ifindex = xp->ifindex; | |
1705 | if (xp->flags & F_PW_CWORD_CONF) | |
1706 | pw->flags |= F_PW_CWORD_CONF; | |
1707 | else | |
1708 | pw->flags &= ~F_PW_CWORD_CONF; | |
1709 | if (xp->flags & F_PW_STATUSTLV_CONF) | |
1710 | pw->flags |= F_PW_STATUSTLV_CONF; | |
1711 | else | |
1712 | pw->flags &= ~F_PW_STATUSTLV_CONF; | |
eac6e3f0 RW |
1713 | if (xp->flags & F_PW_STATIC_NBR_ADDR) |
1714 | pw->flags |= F_PW_STATIC_NBR_ADDR; | |
1715 | else | |
1716 | pw->flags &= ~F_PW_STATIC_NBR_ADDR; | |
8429abe0 RW |
1717 | if (ldpd_process == PROC_LDP_ENGINE && reinstall_tnbr) |
1718 | ldpe_l2vpn_pw_init(pw); | |
be0fbdeb | 1719 | if (ldpd_process == PROC_LDE_ENGINE && reinstall_pwfec) { |
8429abe0 RW |
1720 | l2vpn->pw_type = xl->pw_type; |
1721 | l2vpn->mtu = xl->mtu; | |
1722 | l2vpn_pw_init(pw); | |
1723 | l2vpn->pw_type = previous_pw_type; | |
1724 | l2vpn->mtu = previous_mtu; | |
1725 | } | |
8429abe0 RW |
1726 | } |
1727 | ||
eac6e3f0 | 1728 | /* merge inactive pseudowires */ |
20bacaeb | 1729 | RB_FOREACH_SAFE(pw, l2vpn_pw_head, &l2vpn->pw_inactive_tree, ptmp) { |
eac6e3f0 | 1730 | /* find deleted inactive pseudowires */ |
5c3f00af | 1731 | if ((xp = l2vpn_pw_find_inactive(xl, pw->ifname)) == NULL) { |
20bacaeb | 1732 | RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw); |
eac6e3f0 RW |
1733 | free(pw); |
1734 | } | |
1735 | } | |
20bacaeb | 1736 | RB_FOREACH_SAFE(xp, l2vpn_pw_head, &xl->pw_inactive_tree, ptmp) { |
eac6e3f0 | 1737 | /* find new inactive pseudowires */ |
5c3f00af | 1738 | if ((pw = l2vpn_pw_find_inactive(l2vpn, xp->ifname)) == NULL) { |
1d75a89d RW |
1739 | COPY(pw, xp); |
1740 | RB_INSERT(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw); | |
1741 | pw->l2vpn = l2vpn; | |
1742 | ||
1743 | switch (ldpd_process) { | |
1744 | case PROC_LDE_ENGINE: | |
1745 | case PROC_LDP_ENGINE: | |
1746 | break; | |
1747 | case PROC_MAIN: | |
1748 | kif_redistribute(pw->ifname); | |
1749 | break; | |
52b530fc | 1750 | } |
eac6e3f0 RW |
1751 | continue; |
1752 | } | |
1753 | ||
1754 | /* update existing inactive pseudowire */ | |
1755 | pw->lsr_id.s_addr = xp->lsr_id.s_addr; | |
1756 | pw->af = xp->af; | |
1757 | pw->addr = xp->addr; | |
1758 | pw->pwid = xp->pwid; | |
1759 | strlcpy(pw->ifname, xp->ifname, sizeof(pw->ifname)); | |
1760 | pw->ifindex = xp->ifindex; | |
1761 | pw->flags = xp->flags; | |
eac6e3f0 | 1762 | } |
eac6e3f0 | 1763 | |
8429abe0 RW |
1764 | l2vpn->pw_type = xl->pw_type; |
1765 | l2vpn->mtu = xl->mtu; | |
1766 | strlcpy(l2vpn->br_ifname, xl->br_ifname, sizeof(l2vpn->br_ifname)); | |
1767 | l2vpn->br_ifindex = xl->br_ifindex; | |
1768 | } | |
1769 | ||
1770 | struct ldpd_conf * | |
1771 | config_new_empty(void) | |
1772 | { | |
1773 | struct ldpd_conf *xconf; | |
1774 | ||
1775 | xconf = calloc(1, sizeof(*xconf)); | |
1776 | if (xconf == NULL) | |
1777 | fatal(NULL); | |
1778 | ||
45926e58 RZ |
1779 | RB_INIT(iface_head, &xconf->iface_tree); |
1780 | RB_INIT(tnbr_head, &xconf->tnbr_tree); | |
1781 | RB_INIT(nbrp_head, &xconf->nbrp_tree); | |
1782 | RB_INIT(l2vpn_head, &xconf->l2vpn_tree); | |
8429abe0 | 1783 | |
e4a82008 RW |
1784 | /* set default values */ |
1785 | ldp_config_reset(xconf); | |
1786 | ||
8429abe0 RW |
1787 | return (xconf); |
1788 | } | |
1789 | ||
1790 | void | |
1791 | config_clear(struct ldpd_conf *conf) | |
1792 | { | |
1793 | struct ldpd_conf *xconf; | |
1794 | ||
1795 | /* | |
1796 | * Merge current config with an empty config, this will deactivate | |
1797 | * and deallocate all the interfaces, pseudowires and so on. Before | |
1798 | * merging, copy the router-id and other variables to avoid some | |
1799 | * unnecessary operations, like trying to reset the neighborships. | |
1800 | */ | |
1801 | xconf = config_new_empty(); | |
1802 | xconf->ipv4 = conf->ipv4; | |
1803 | xconf->ipv6 = conf->ipv6; | |
1804 | xconf->rtr_id = conf->rtr_id; | |
1805 | xconf->trans_pref = conf->trans_pref; | |
1806 | xconf->flags = conf->flags; | |
1807 | merge_config(conf, xconf); | |
1d75a89d | 1808 | free(xconf); |
8429abe0 RW |
1809 | free(conf); |
1810 | } |