]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/ldpe.c
Merge pull request #707 from donaldsharp/debian_babel
[mirror_frr.git] / ldpd / ldpe.c
1 /* $OpenBSD$ */
2
3 /*
4 * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
5 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
6 * Copyright (c) 2004, 2008 Esben Norby <norby@openbsd.org>
7 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22 #include <zebra.h>
23
24 #include "ldpd.h"
25 #include "ldpe.h"
26 #include "lde.h"
27 #include "control.h"
28 #include "log.h"
29 #include "ldp_debug.h"
30
31 #include <lib/log.h>
32 #include "memory.h"
33 #include "privs.h"
34 #include "sigevent.h"
35
36 static void ldpe_shutdown(void);
37 static int ldpe_dispatch_main(struct thread *);
38 static int ldpe_dispatch_lde(struct thread *);
39 #ifdef __OpenBSD__
40 static int ldpe_dispatch_pfkey(struct thread *);
41 #endif
42 static void ldpe_setup_sockets(int, int, int, int);
43 static void ldpe_close_sockets(int);
44 static void ldpe_iface_af_ctl(struct ctl_conn *, int, unsigned int);
45
46 struct ldpd_conf *leconf;
47 #ifdef __OpenBSD__
48 struct ldpd_sysdep sysdep;
49 #endif
50
51 static struct imsgev *iev_main, *iev_main_sync;
52 static struct imsgev *iev_lde;
53 #ifdef __OpenBSD__
54 static struct thread *pfkey_ev;
55 #endif
56
57 /* Master of threads. */
58 struct thread_master *master;
59
60 /* ldpe privileges */
61 static zebra_capabilities_t _caps_p [] =
62 {
63 ZCAP_BIND,
64 ZCAP_NET_ADMIN
65 };
66
67 struct zebra_privs_t ldpe_privs =
68 {
69 #if defined(VTY_GROUP)
70 .vty_group = VTY_GROUP,
71 #endif
72 .caps_p = _caps_p,
73 .cap_num_p = array_size(_caps_p),
74 .cap_num_i = 0
75 };
76
77 /* SIGINT / SIGTERM handler. */
78 static void
79 sigint(void)
80 {
81 ldpe_shutdown();
82 }
83
84 static struct quagga_signal_t ldpe_signals[] =
85 {
86 {
87 .signal = SIGHUP,
88 /* ignore */
89 },
90 {
91 .signal = SIGINT,
92 .handler = &sigint,
93 },
94 {
95 .signal = SIGTERM,
96 .handler = &sigint,
97 },
98 };
99
100 /* label distribution protocol engine */
101 void
102 ldpe(void)
103 {
104 struct thread thread;
105
106 #ifdef HAVE_SETPROCTITLE
107 setproctitle("ldp engine");
108 #endif
109 ldpd_process = PROC_LDP_ENGINE;
110 log_procname = log_procnames[ldpd_process];
111
112 master = thread_master_create();
113
114 /* setup signal handler */
115 signal_init(master, array_size(ldpe_signals), ldpe_signals);
116
117 /* setup pipes and event handlers to the parent process */
118 if ((iev_main = calloc(1, sizeof(struct imsgev))) == NULL)
119 fatal(NULL);
120 imsg_init(&iev_main->ibuf, LDPD_FD_ASYNC);
121 iev_main->handler_read = ldpe_dispatch_main;
122 iev_main->ev_read = NULL;
123 thread_add_read(master, iev_main->handler_read, iev_main, iev_main->ibuf.fd,
124 &iev_main->ev_read);
125 iev_main->handler_write = ldp_write_handler;
126
127 if ((iev_main_sync = calloc(1, sizeof(struct imsgev))) == NULL)
128 fatal(NULL);
129 imsg_init(&iev_main_sync->ibuf, LDPD_FD_SYNC);
130
131 /* create base configuration */
132 leconf = config_new_empty();
133
134 /* Fetch next active thread. */
135 while (thread_fetch(master, &thread))
136 thread_call(&thread);
137 }
138
139 void
140 ldpe_init(struct ldpd_init *init)
141 {
142 /* drop privileges */
143 ldpe_privs.user = init->user;
144 ldpe_privs.group = init->group;
145 zprivs_init(&ldpe_privs);
146
147 /* listen on ldpd control socket */
148 strlcpy(ctl_sock_path, init->ctl_sock_path, sizeof(ctl_sock_path));
149 if (control_init(ctl_sock_path) == -1)
150 fatalx("control socket setup failed");
151 TAILQ_INIT(&ctl_conns);
152 control_listen();
153
154 LIST_INIT(&global.addr_list);
155 RB_INIT(global_adj_head, &global.adj_tree);
156 TAILQ_INIT(&global.pending_conns);
157 if (inet_pton(AF_INET, AllRouters_v4, &global.mcast_addr_v4) != 1)
158 fatal("inet_pton");
159 if (inet_pton(AF_INET6, AllRouters_v6, &global.mcast_addr_v6) != 1)
160 fatal("inet_pton");
161 #ifdef __OpenBSD__
162 global.pfkeysock = pfkey_init();
163 if (sysdep.no_pfkey == 0) {
164 pfkey_ev = NULL;
165 thread_add_read(master, ldpe_dispatch_pfkey, NULL, global.pfkeysock,
166 &pfkey_ev);
167 }
168 #endif
169
170 /* mark sockets as closed */
171 global.ipv4.ldp_disc_socket = -1;
172 global.ipv4.ldp_edisc_socket = -1;
173 global.ipv4.ldp_session_socket = -1;
174 global.ipv6.ldp_disc_socket = -1;
175 global.ipv6.ldp_edisc_socket = -1;
176 global.ipv6.ldp_session_socket = -1;
177
178 if ((pkt_ptr = calloc(1, IBUF_READ_SIZE)) == NULL)
179 fatal(__func__);
180
181 accept_init();
182 }
183
184 static void
185 ldpe_shutdown(void)
186 {
187 struct if_addr *if_addr;
188 struct adj *adj;
189
190 /* close pipes */
191 if (iev_lde) {
192 msgbuf_write(&iev_lde->ibuf.w);
193 msgbuf_clear(&iev_lde->ibuf.w);
194 close(iev_lde->ibuf.fd);
195 }
196 msgbuf_write(&iev_main->ibuf.w);
197 msgbuf_clear(&iev_main->ibuf.w);
198 close(iev_main->ibuf.fd);
199 msgbuf_clear(&iev_main_sync->ibuf.w);
200 close(iev_main_sync->ibuf.fd);
201
202 control_cleanup(ctl_sock_path);
203 config_clear(leconf);
204
205 #ifdef __OpenBSD__
206 if (sysdep.no_pfkey == 0) {
207 THREAD_READ_OFF(pfkey_ev);
208 close(global.pfkeysock);
209 }
210 #endif
211 ldpe_close_sockets(AF_INET);
212 ldpe_close_sockets(AF_INET6);
213
214 /* remove addresses from global list */
215 while ((if_addr = LIST_FIRST(&global.addr_list)) != NULL) {
216 LIST_REMOVE(if_addr, entry);
217 free(if_addr);
218 }
219 while ((adj = RB_ROOT(global_adj_head, &global.adj_tree)) != NULL)
220 adj_del(adj, S_SHUTDOWN);
221
222 /* clean up */
223 if (iev_lde)
224 free(iev_lde);
225 free(iev_main);
226 free(iev_main_sync);
227 free(pkt_ptr);
228
229 log_info("ldp engine exiting");
230 exit(0);
231 }
232
233 /* imesg */
234 int
235 ldpe_imsg_compose_parent(int type, pid_t pid, void *data, uint16_t datalen)
236 {
237 return (imsg_compose_event(iev_main, type, 0, pid, -1, data, datalen));
238 }
239
240 void
241 ldpe_imsg_compose_parent_sync(int type, pid_t pid, void *data, uint16_t datalen)
242 {
243 imsg_compose_event(iev_main_sync, type, 0, pid, -1, data, datalen);
244 imsg_flush(&iev_main_sync->ibuf);
245 }
246
247 int
248 ldpe_imsg_compose_lde(int type, uint32_t peerid, pid_t pid, void *data,
249 uint16_t datalen)
250 {
251 return (imsg_compose_event(iev_lde, type, peerid, pid, -1,
252 data, datalen));
253 }
254
255 /* ARGSUSED */
256 static int
257 ldpe_dispatch_main(struct thread *thread)
258 {
259 static struct ldpd_conf *nconf;
260 struct iface *niface;
261 struct tnbr *ntnbr;
262 struct nbr_params *nnbrp;
263 static struct l2vpn *l2vpn, *nl2vpn;
264 struct l2vpn_if *lif, *nlif;
265 struct l2vpn_pw *pw, *npw;
266 struct imsg imsg;
267 int fd = THREAD_FD(thread);
268 struct imsgev *iev = THREAD_ARG(thread);
269 struct imsgbuf *ibuf = &iev->ibuf;
270 struct iface *iface = NULL;
271 struct kif *kif;
272 int af;
273 enum socket_type *socket_type;
274 static int disc_socket = -1;
275 static int edisc_socket = -1;
276 static int session_socket = -1;
277 struct nbr *nbr;
278 #ifdef __OpenBSD__
279 struct nbr_params *nbrp;
280 #endif
281 int n, shut = 0;
282
283 iev->ev_read = NULL;
284
285 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
286 fatal("imsg_read error");
287 if (n == 0) /* connection closed */
288 shut = 1;
289
290 for (;;) {
291 if ((n = imsg_get(ibuf, &imsg)) == -1)
292 fatal("ldpe_dispatch_main: imsg_get error");
293 if (n == 0)
294 break;
295
296 switch (imsg.hdr.type) {
297 case IMSG_IFSTATUS:
298 if (imsg.hdr.len != IMSG_HEADER_SIZE +
299 sizeof(struct kif))
300 fatalx("IFSTATUS imsg with wrong len");
301 kif = imsg.data;
302
303 iface = if_lookup_name(leconf, kif->ifname);
304 if (iface) {
305 if_update_info(iface, kif);
306 ldp_if_update(iface, AF_UNSPEC);
307 break;
308 }
309
310 RB_FOREACH(l2vpn, l2vpn_head, &leconf->l2vpn_tree) {
311 lif = l2vpn_if_find(l2vpn, kif->ifname);
312 if (lif) {
313 l2vpn_if_update_info(lif, kif);
314 l2vpn_if_update(lif);
315 break;
316 }
317 pw = l2vpn_pw_find(l2vpn, kif->ifname);
318 if (pw) {
319 l2vpn_pw_update_info(pw, kif);
320 break;
321 }
322 }
323 break;
324 case IMSG_NEWADDR:
325 if (imsg.hdr.len != IMSG_HEADER_SIZE +
326 sizeof(struct kaddr))
327 fatalx("NEWADDR imsg with wrong len");
328
329 if_addr_add(imsg.data);
330 break;
331 case IMSG_DELADDR:
332 if (imsg.hdr.len != IMSG_HEADER_SIZE +
333 sizeof(struct kaddr))
334 fatalx("DELADDR imsg with wrong len");
335
336 if_addr_del(imsg.data);
337 break;
338 case IMSG_SOCKET_IPC:
339 if (iev_lde) {
340 log_warnx("%s: received unexpected imsg fd "
341 "to lde", __func__);
342 break;
343 }
344 if ((fd = imsg.fd) == -1) {
345 log_warnx("%s: expected to receive imsg fd to "
346 "lde but didn't receive any", __func__);
347 break;
348 }
349
350 if ((iev_lde = malloc(sizeof(struct imsgev))) == NULL)
351 fatal(NULL);
352 imsg_init(&iev_lde->ibuf, fd);
353 iev_lde->handler_read = ldpe_dispatch_lde;
354 iev_lde->ev_read = NULL;
355 thread_add_read(master, iev_lde->handler_read, iev_lde, iev_lde->ibuf.fd,
356 &iev_lde->ev_read);
357 iev_lde->handler_write = ldp_write_handler;
358 iev_lde->ev_write = NULL;
359 break;
360 case IMSG_INIT:
361 if (imsg.hdr.len != IMSG_HEADER_SIZE +
362 sizeof(struct ldpd_init))
363 fatalx("INIT imsg with wrong len");
364
365 memcpy(&init, imsg.data, sizeof(init));
366 ldpe_init(&init);
367 break;
368 case IMSG_CLOSE_SOCKETS:
369 af = imsg.hdr.peerid;
370
371 RB_FOREACH(nbr, nbr_id_head, &nbrs_by_id) {
372 if (nbr->af != af)
373 continue;
374 session_shutdown(nbr, S_SHUTDOWN, 0, 0);
375 #ifdef __OpenBSD__
376 pfkey_remove(nbr);
377 #endif
378 nbr->auth.method = AUTH_NONE;
379 }
380 ldpe_close_sockets(af);
381 if_update_all(af);
382 tnbr_update_all(af);
383
384 disc_socket = -1;
385 edisc_socket = -1;
386 session_socket = -1;
387 if ((ldp_af_conf_get(leconf, af))->flags &
388 F_LDPD_AF_ENABLED)
389 ldpe_imsg_compose_parent(IMSG_REQUEST_SOCKETS,
390 af, NULL, 0);
391 break;
392 case IMSG_SOCKET_NET:
393 if (imsg.hdr.len != IMSG_HEADER_SIZE +
394 sizeof(enum socket_type))
395 fatalx("SOCKET_NET imsg with wrong len");
396 socket_type = imsg.data;
397
398 switch (*socket_type) {
399 case LDP_SOCKET_DISC:
400 disc_socket = imsg.fd;
401 break;
402 case LDP_SOCKET_EDISC:
403 edisc_socket = imsg.fd;
404 break;
405 case LDP_SOCKET_SESSION:
406 session_socket = imsg.fd;
407 break;
408 }
409 break;
410 case IMSG_SETUP_SOCKETS:
411 af = imsg.hdr.peerid;
412 if (disc_socket == -1 || edisc_socket == -1 ||
413 session_socket == -1) {
414 if (disc_socket != -1)
415 close(disc_socket);
416 if (edisc_socket != -1)
417 close(edisc_socket);
418 if (session_socket != -1)
419 close(session_socket);
420 break;
421 }
422
423 ldpe_setup_sockets(af, disc_socket, edisc_socket,
424 session_socket);
425 if_update_all(af);
426 tnbr_update_all(af);
427 RB_FOREACH(nbr, nbr_id_head, &nbrs_by_id) {
428 if (nbr->af != af)
429 continue;
430 nbr->laddr = (ldp_af_conf_get(leconf,
431 af))->trans_addr;
432 #ifdef __OpenBSD__
433 nbrp = nbr_params_find(leconf, nbr->id);
434 if (nbrp) {
435 nbr->auth.method = nbrp->auth.method;
436 if (pfkey_establish(nbr, nbrp) == -1)
437 fatalx("pfkey setup failed");
438 }
439 #endif
440 if (nbr_session_active_role(nbr))
441 nbr_establish_connection(nbr);
442 }
443 break;
444 case IMSG_RTRID_UPDATE:
445 memcpy(&global.rtr_id, imsg.data,
446 sizeof(global.rtr_id));
447 if (leconf->rtr_id.s_addr == INADDR_ANY) {
448 ldpe_reset_nbrs(AF_UNSPEC);
449 }
450 if_update_all(AF_UNSPEC);
451 tnbr_update_all(AF_UNSPEC);
452 break;
453 case IMSG_RECONF_CONF:
454 if ((nconf = malloc(sizeof(struct ldpd_conf))) ==
455 NULL)
456 fatal(NULL);
457 memcpy(nconf, imsg.data, sizeof(struct ldpd_conf));
458
459 RB_INIT(iface_head, &nconf->iface_tree);
460 RB_INIT(tnbr_head, &nconf->tnbr_tree);
461 RB_INIT(nbrp_head, &nconf->nbrp_tree);
462 RB_INIT(l2vpn_head, &nconf->l2vpn_tree);
463 break;
464 case IMSG_RECONF_IFACE:
465 if ((niface = malloc(sizeof(struct iface))) == NULL)
466 fatal(NULL);
467 memcpy(niface, imsg.data, sizeof(struct iface));
468
469 RB_INSERT(iface_head, &nconf->iface_tree, niface);
470 break;
471 case IMSG_RECONF_TNBR:
472 if ((ntnbr = malloc(sizeof(struct tnbr))) == NULL)
473 fatal(NULL);
474 memcpy(ntnbr, imsg.data, sizeof(struct tnbr));
475
476 RB_INSERT(tnbr_head, &nconf->tnbr_tree, ntnbr);
477 break;
478 case IMSG_RECONF_NBRP:
479 if ((nnbrp = malloc(sizeof(struct nbr_params))) == NULL)
480 fatal(NULL);
481 memcpy(nnbrp, imsg.data, sizeof(struct nbr_params));
482
483 RB_INSERT(nbrp_head, &nconf->nbrp_tree, nnbrp);
484 break;
485 case IMSG_RECONF_L2VPN:
486 if ((nl2vpn = malloc(sizeof(struct l2vpn))) == NULL)
487 fatal(NULL);
488 memcpy(nl2vpn, imsg.data, sizeof(struct l2vpn));
489
490 RB_INIT(l2vpn_if_head, &nl2vpn->if_tree);
491 RB_INIT(l2vpn_pw_head, &nl2vpn->pw_tree);
492 RB_INIT(l2vpn_pw_head, &nl2vpn->pw_inactive_tree);
493
494 RB_INSERT(l2vpn_head, &nconf->l2vpn_tree, nl2vpn);
495 break;
496 case IMSG_RECONF_L2VPN_IF:
497 if ((nlif = malloc(sizeof(struct l2vpn_if))) == NULL)
498 fatal(NULL);
499 memcpy(nlif, imsg.data, sizeof(struct l2vpn_if));
500
501 RB_INSERT(l2vpn_if_head, &nl2vpn->if_tree, nlif);
502 break;
503 case IMSG_RECONF_L2VPN_PW:
504 if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL)
505 fatal(NULL);
506 memcpy(npw, imsg.data, sizeof(struct l2vpn_pw));
507
508 RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_tree, npw);
509 break;
510 case IMSG_RECONF_L2VPN_IPW:
511 if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL)
512 fatal(NULL);
513 memcpy(npw, imsg.data, sizeof(struct l2vpn_pw));
514
515 RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_inactive_tree, npw);
516 break;
517 case IMSG_RECONF_END:
518 merge_config(leconf, nconf);
519 ldp_clear_config(nconf);
520 nconf = NULL;
521 global.conf_seqnum++;
522 break;
523 case IMSG_CTL_END:
524 control_imsg_relay(&imsg);
525 break;
526 case IMSG_DEBUG_UPDATE:
527 if (imsg.hdr.len != IMSG_HEADER_SIZE +
528 sizeof(ldp_debug)) {
529 log_warnx("%s: wrong imsg len", __func__);
530 break;
531 }
532 memcpy(&ldp_debug, imsg.data, sizeof(ldp_debug));
533 break;
534 default:
535 log_debug("ldpe_dispatch_main: error handling imsg %d",
536 imsg.hdr.type);
537 break;
538 }
539 imsg_free(&imsg);
540 }
541 if (!shut)
542 imsg_event_add(iev);
543 else {
544 /* this pipe is dead, so remove the event handlers and exit */
545 THREAD_READ_OFF(iev->ev_read);
546 THREAD_WRITE_OFF(iev->ev_write);
547 ldpe_shutdown();
548 }
549
550 return (0);
551 }
552
553 /* ARGSUSED */
554 static int
555 ldpe_dispatch_lde(struct thread *thread)
556 {
557 struct imsgev *iev = THREAD_ARG(thread);
558 struct imsgbuf *ibuf = &iev->ibuf;
559 struct imsg imsg;
560 struct map *map;
561 struct notify_msg *nm;
562 struct nbr *nbr;
563 int n, shut = 0;
564
565 iev->ev_read = NULL;
566
567 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
568 fatal("imsg_read error");
569 if (n == 0) /* connection closed */
570 shut = 1;
571
572 for (;;) {
573 if ((n = imsg_get(ibuf, &imsg)) == -1)
574 fatal("ldpe_dispatch_lde: imsg_get error");
575 if (n == 0)
576 break;
577
578 switch (imsg.hdr.type) {
579 case IMSG_MAPPING_ADD:
580 case IMSG_RELEASE_ADD:
581 case IMSG_REQUEST_ADD:
582 case IMSG_WITHDRAW_ADD:
583 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
584 sizeof(struct map))
585 fatalx("invalid size of map request");
586 map = imsg.data;
587
588 nbr = nbr_find_peerid(imsg.hdr.peerid);
589 if (nbr == NULL) {
590 log_debug("ldpe_dispatch_lde: cannot find "
591 "neighbor");
592 break;
593 }
594 if (nbr->state != NBR_STA_OPER)
595 break;
596
597 switch (imsg.hdr.type) {
598 case IMSG_MAPPING_ADD:
599 mapping_list_add(&nbr->mapping_list, map);
600 break;
601 case IMSG_RELEASE_ADD:
602 mapping_list_add(&nbr->release_list, map);
603 break;
604 case IMSG_REQUEST_ADD:
605 mapping_list_add(&nbr->request_list, map);
606 break;
607 case IMSG_WITHDRAW_ADD:
608 mapping_list_add(&nbr->withdraw_list, map);
609 break;
610 }
611 break;
612 case IMSG_MAPPING_ADD_END:
613 case IMSG_RELEASE_ADD_END:
614 case IMSG_REQUEST_ADD_END:
615 case IMSG_WITHDRAW_ADD_END:
616 nbr = nbr_find_peerid(imsg.hdr.peerid);
617 if (nbr == NULL) {
618 log_debug("ldpe_dispatch_lde: cannot find "
619 "neighbor");
620 break;
621 }
622 if (nbr->state != NBR_STA_OPER)
623 break;
624
625 switch (imsg.hdr.type) {
626 case IMSG_MAPPING_ADD_END:
627 send_labelmessage(nbr, MSG_TYPE_LABELMAPPING,
628 &nbr->mapping_list);
629 break;
630 case IMSG_RELEASE_ADD_END:
631 send_labelmessage(nbr, MSG_TYPE_LABELRELEASE,
632 &nbr->release_list);
633 break;
634 case IMSG_REQUEST_ADD_END:
635 send_labelmessage(nbr, MSG_TYPE_LABELREQUEST,
636 &nbr->request_list);
637 break;
638 case IMSG_WITHDRAW_ADD_END:
639 send_labelmessage(nbr, MSG_TYPE_LABELWITHDRAW,
640 &nbr->withdraw_list);
641 break;
642 }
643 break;
644 case IMSG_NOTIFICATION_SEND:
645 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
646 sizeof(struct notify_msg))
647 fatalx("invalid size of OE request");
648 nm = imsg.data;
649
650 nbr = nbr_find_peerid(imsg.hdr.peerid);
651 if (nbr == NULL) {
652 log_debug("ldpe_dispatch_lde: cannot find "
653 "neighbor");
654 break;
655 }
656 if (nbr->state != NBR_STA_OPER)
657 break;
658
659 send_notification_full(nbr->tcp, nm);
660 break;
661 case IMSG_CTL_END:
662 case IMSG_CTL_SHOW_LIB_BEGIN:
663 case IMSG_CTL_SHOW_LIB_RCVD:
664 case IMSG_CTL_SHOW_LIB_SENT:
665 case IMSG_CTL_SHOW_LIB_END:
666 case IMSG_CTL_SHOW_L2VPN_PW:
667 case IMSG_CTL_SHOW_L2VPN_BINDING:
668 control_imsg_relay(&imsg);
669 break;
670 default:
671 log_debug("ldpe_dispatch_lde: error handling imsg %d",
672 imsg.hdr.type);
673 break;
674 }
675 imsg_free(&imsg);
676 }
677 if (!shut)
678 imsg_event_add(iev);
679 else {
680 /* this pipe is dead, so remove the event handlers and exit */
681 THREAD_READ_OFF(iev->ev_read);
682 THREAD_WRITE_OFF(iev->ev_write);
683 ldpe_shutdown();
684 }
685
686 return (0);
687 }
688
689 #ifdef __OpenBSD__
690 /* ARGSUSED */
691 static int
692 ldpe_dispatch_pfkey(struct thread *thread)
693 {
694 int fd = THREAD_FD(thread);
695
696 pfkey_ev = NULL;
697 thread_add_read(master, ldpe_dispatch_pfkey, NULL, global.pfkeysock,
698 &pfkey_ev);
699
700 if (pfkey_read(fd, NULL) == -1)
701 fatal("pfkey_read failed, exiting...");
702
703 return (0);
704 }
705 #endif /* __OpenBSD__ */
706
707 static void
708 ldpe_setup_sockets(int af, int disc_socket, int edisc_socket,
709 int session_socket)
710 {
711 struct ldpd_af_global *af_global;
712
713 af_global = ldp_af_global_get(&global, af);
714
715 /* discovery socket */
716 af_global->ldp_disc_socket = disc_socket;
717 af_global->disc_ev = NULL;
718 thread_add_read(master, disc_recv_packet, &af_global->disc_ev, af_global->ldp_disc_socket,
719 &af_global->disc_ev);
720
721 /* extended discovery socket */
722 af_global->ldp_edisc_socket = edisc_socket;
723 af_global->edisc_ev = NULL;
724 thread_add_read(master, disc_recv_packet, &af_global->edisc_ev, af_global->ldp_edisc_socket,
725 &af_global->edisc_ev);
726
727 /* session socket */
728 af_global->ldp_session_socket = session_socket;
729 accept_add(af_global->ldp_session_socket, session_accept, NULL);
730 }
731
732 static void
733 ldpe_close_sockets(int af)
734 {
735 struct ldpd_af_global *af_global;
736
737 af_global = ldp_af_global_get(&global, af);
738
739 /* discovery socket */
740 THREAD_READ_OFF(af_global->disc_ev);
741 if (af_global->ldp_disc_socket != -1) {
742 close(af_global->ldp_disc_socket);
743 af_global->ldp_disc_socket = -1;
744 }
745
746 /* extended discovery socket */
747 THREAD_READ_OFF(af_global->edisc_ev);
748 if (af_global->ldp_edisc_socket != -1) {
749 close(af_global->ldp_edisc_socket);
750 af_global->ldp_edisc_socket = -1;
751 }
752
753 /* session socket */
754 if (af_global->ldp_session_socket != -1) {
755 accept_del(af_global->ldp_session_socket);
756 close(af_global->ldp_session_socket);
757 af_global->ldp_session_socket = -1;
758 }
759 }
760
761 int
762 ldpe_acl_check(char *acl_name, int af, union ldpd_addr *addr, uint8_t prefixlen)
763 {
764 return ldp_acl_request(iev_main_sync, acl_name, af, addr, prefixlen);
765 }
766
767 void
768 ldpe_reset_nbrs(int af)
769 {
770 struct nbr *nbr;
771
772 RB_FOREACH(nbr, nbr_id_head, &nbrs_by_id) {
773 if (af == AF_UNSPEC || nbr->af == af)
774 session_shutdown(nbr, S_SHUTDOWN, 0, 0);
775 }
776 }
777
778 void
779 ldpe_reset_ds_nbrs(void)
780 {
781 struct nbr *nbr;
782
783 RB_FOREACH(nbr, nbr_id_head, &nbrs_by_id) {
784 if (nbr->ds_tlv)
785 session_shutdown(nbr, S_SHUTDOWN, 0, 0);
786 }
787 }
788
789 void
790 ldpe_remove_dynamic_tnbrs(int af)
791 {
792 struct tnbr *tnbr, *safe;
793
794 RB_FOREACH_SAFE(tnbr, tnbr_head, &leconf->tnbr_tree, safe) {
795 if (tnbr->af != af)
796 continue;
797
798 tnbr->flags &= ~F_TNBR_DYNAMIC;
799 tnbr_check(leconf, tnbr);
800 }
801 }
802
803 void
804 ldpe_stop_init_backoff(int af)
805 {
806 struct nbr *nbr;
807
808 RB_FOREACH(nbr, nbr_id_head, &nbrs_by_id) {
809 if (nbr->af == af && nbr_pending_idtimer(nbr)) {
810 nbr_stop_idtimer(nbr);
811 nbr_establish_connection(nbr);
812 }
813 }
814 }
815
816 static void
817 ldpe_iface_af_ctl(struct ctl_conn *c, int af, unsigned int idx)
818 {
819 struct iface *iface;
820 struct iface_af *ia;
821 struct ctl_iface *ictl;
822
823 RB_FOREACH(iface, iface_head, &leconf->iface_tree) {
824 if (idx == 0 || idx == iface->ifindex) {
825 ia = iface_af_get(iface, af);
826 if (!ia->enabled)
827 continue;
828
829 ictl = if_to_ctl(ia);
830 imsg_compose_event(&c->iev, IMSG_CTL_SHOW_INTERFACE,
831 0, 0, -1, ictl, sizeof(struct ctl_iface));
832 }
833 }
834 }
835
836 void
837 ldpe_iface_ctl(struct ctl_conn *c, unsigned int idx)
838 {
839 ldpe_iface_af_ctl(c, AF_INET, idx);
840 ldpe_iface_af_ctl(c, AF_INET6, idx);
841 }
842
843 void
844 ldpe_adj_ctl(struct ctl_conn *c)
845 {
846 struct adj *adj;
847 struct ctl_adj *actl;
848
849 RB_FOREACH(adj, global_adj_head, &global.adj_tree) {
850 actl = adj_to_ctl(adj);
851 imsg_compose_event(&c->iev, IMSG_CTL_SHOW_DISCOVERY, 0, 0,
852 -1, actl, sizeof(struct ctl_adj));
853 }
854
855 imsg_compose_event(&c->iev, IMSG_CTL_END, 0, 0, -1, NULL, 0);
856 }
857
858 void
859 ldpe_adj_detail_ctl(struct ctl_conn *c)
860 {
861 struct iface *iface;
862 struct tnbr *tnbr;
863 struct adj *adj;
864 struct ctl_adj *actl;
865 struct ctl_disc_if ictl;
866 struct ctl_disc_tnbr tctl;
867
868 imsg_compose_event(&c->iev, IMSG_CTL_SHOW_DISCOVERY, 0, 0, -1, NULL, 0);
869
870 RB_FOREACH(iface, iface_head, &leconf->iface_tree) {
871 memset(&ictl, 0, sizeof(ictl));
872 ictl.active_v4 = (iface->ipv4.state == IF_STA_ACTIVE);
873 ictl.active_v6 = (iface->ipv6.state == IF_STA_ACTIVE);
874
875 if (!ictl.active_v4 && !ictl.active_v6)
876 continue;
877
878 strlcpy(ictl.name, iface->name, sizeof(ictl.name));
879 if (RB_EMPTY(ia_adj_head, &iface->ipv4.adj_tree) &&
880 RB_EMPTY(ia_adj_head, &iface->ipv6.adj_tree))
881 ictl.no_adj = 1;
882 imsg_compose_event(&c->iev, IMSG_CTL_SHOW_DISC_IFACE, 0, 0,
883 -1, &ictl, sizeof(ictl));
884
885 RB_FOREACH(adj, ia_adj_head, &iface->ipv4.adj_tree) {
886 actl = adj_to_ctl(adj);
887 imsg_compose_event(&c->iev, IMSG_CTL_SHOW_DISC_ADJ,
888 0, 0, -1, actl, sizeof(struct ctl_adj));
889 }
890 RB_FOREACH(adj, ia_adj_head, &iface->ipv6.adj_tree) {
891 actl = adj_to_ctl(adj);
892 imsg_compose_event(&c->iev, IMSG_CTL_SHOW_DISC_ADJ,
893 0, 0, -1, actl, sizeof(struct ctl_adj));
894 }
895 }
896
897 RB_FOREACH(tnbr, tnbr_head, &leconf->tnbr_tree) {
898 memset(&tctl, 0, sizeof(tctl));
899 tctl.af = tnbr->af;
900 tctl.addr = tnbr->addr;
901 if (tnbr->adj == NULL)
902 tctl.no_adj = 1;
903
904 imsg_compose_event(&c->iev, IMSG_CTL_SHOW_DISC_TNBR, 0, 0,
905 -1, &tctl, sizeof(tctl));
906
907 if (tnbr->adj == NULL)
908 continue;
909
910 actl = adj_to_ctl(tnbr->adj);
911 imsg_compose_event(&c->iev, IMSG_CTL_SHOW_DISC_ADJ, 0, 0,
912 -1, actl, sizeof(struct ctl_adj));
913 }
914
915 imsg_compose_event(&c->iev, IMSG_CTL_END, 0, 0, -1, NULL, 0);
916 }
917
918 void
919 ldpe_nbr_ctl(struct ctl_conn *c)
920 {
921 struct adj *adj;
922 struct ctl_adj *actl;
923 struct nbr *nbr;
924 struct ctl_nbr *nctl;
925
926 RB_FOREACH(nbr, nbr_addr_head, &nbrs_by_addr) {
927 if (nbr->state == NBR_STA_PRESENT)
928 continue;
929
930 nctl = nbr_to_ctl(nbr);
931 imsg_compose_event(&c->iev, IMSG_CTL_SHOW_NBR, 0, 0, -1, nctl,
932 sizeof(struct ctl_nbr));
933
934 RB_FOREACH(adj, nbr_adj_head, &nbr->adj_tree) {
935 actl = adj_to_ctl(adj);
936 imsg_compose_event(&c->iev, IMSG_CTL_SHOW_NBR_DISC,
937 0, 0, -1, actl, sizeof(struct ctl_adj));
938 }
939
940 imsg_compose_event(&c->iev, IMSG_CTL_SHOW_NBR_END, 0, 0, -1,
941 NULL, 0);
942 }
943 imsg_compose_event(&c->iev, IMSG_CTL_END, 0, 0, -1, NULL, 0);
944 }
945
946 void
947 mapping_list_add(struct mapping_head *mh, struct map *map)
948 {
949 struct mapping_entry *me;
950
951 me = calloc(1, sizeof(*me));
952 if (me == NULL)
953 fatal(__func__);
954 me->map = *map;
955
956 TAILQ_INSERT_TAIL(mh, me, entry);
957 }
958
959 void
960 mapping_list_clr(struct mapping_head *mh)
961 {
962 struct mapping_entry *me;
963
964 while ((me = TAILQ_FIRST(mh)) != NULL) {
965 TAILQ_REMOVE(mh, me, entry);
966 free(me);
967 }
968 }