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>
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.
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.
29 #include "ldp_debug.h"
36 static void ldpe_shutdown(void);
37 static int ldpe_dispatch_main(struct thread
*);
38 static int ldpe_dispatch_lde(struct thread
*);
40 static int ldpe_dispatch_pfkey(struct thread
*);
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);
46 struct ldpd_conf
*leconf
;
48 struct ldpd_sysdep sysdep
;
51 static struct imsgev
*iev_main
, *iev_main_sync
;
52 static struct imsgev
*iev_lde
;
54 static struct thread
*pfkey_ev
;
57 /* Master of threads. */
58 struct thread_master
*master
;
61 static zebra_capabilities_t _caps_p
[] =
67 struct zebra_privs_t ldpe_privs
=
69 #if defined(FRR_USER) && defined(FRR_GROUP)
73 #if defined(VTY_GROUP)
74 .vty_group
= VTY_GROUP
,
77 .cap_num_p
= array_size(_caps_p
),
81 /* SIGINT / SIGTERM handler. */
88 static struct quagga_signal_t ldpe_signals
[] =
104 /* label distribution protocol engine */
106 ldpe(const char *user
, const char *group
, const char *ctl_path
)
108 struct thread thread
;
110 leconf
= config_new_empty();
112 #ifdef HAVE_SETPROCTITLE
113 setproctitle("ldp engine");
115 ldpd_process
= PROC_LDP_ENGINE
;
117 LIST_INIT(&global
.addr_list
);
118 RB_INIT(&global
.adj_tree
);
119 TAILQ_INIT(&global
.pending_conns
);
120 if (inet_pton(AF_INET
, AllRouters_v4
, &global
.mcast_addr_v4
) != 1)
122 if (inet_pton(AF_INET6
, AllRouters_v6
, &global
.mcast_addr_v6
) != 1)
125 global
.pfkeysock
= pfkey_init();
128 /* drop privileges */
130 ldpe_privs
.user
= user
;
132 ldpe_privs
.group
= group
;
133 zprivs_init(&ldpe_privs
);
135 strlcpy(ctl_sock_path
, ctl_path
, sizeof(ctl_sock_path
));
136 if (control_init() == -1)
137 fatalx("control socket setup failed");
140 if (pledge("stdio cpath inet mcast recvfd", NULL
) == -1)
144 master
= thread_master_create();
147 /* setup signal handler */
148 signal_init(master
, array_size(ldpe_signals
), ldpe_signals
);
150 /* setup pipes and event handlers to the parent process */
151 if ((iev_main
= calloc(1, sizeof(struct imsgev
))) == NULL
)
153 imsg_init(&iev_main
->ibuf
, LDPD_FD_ASYNC
);
154 iev_main
->handler_read
= ldpe_dispatch_main
;
155 iev_main
->ev_read
= thread_add_read(master
, iev_main
->handler_read
,
156 iev_main
, iev_main
->ibuf
.fd
);
157 iev_main
->handler_write
= ldp_write_handler
;
159 if ((iev_main_sync
= calloc(1, sizeof(struct imsgev
))) == NULL
)
161 imsg_init(&iev_main_sync
->ibuf
, LDPD_FD_SYNC
);
164 if (sysdep
.no_pfkey
== 0)
165 pfkey_ev
= thread_add_read(master
, ldpe_dispatch_pfkey
,
166 NULL
, global
.pfkeysock
);
169 /* mark sockets as closed */
170 global
.ipv4
.ldp_disc_socket
= -1;
171 global
.ipv4
.ldp_edisc_socket
= -1;
172 global
.ipv4
.ldp_session_socket
= -1;
173 global
.ipv6
.ldp_disc_socket
= -1;
174 global
.ipv6
.ldp_edisc_socket
= -1;
175 global
.ipv6
.ldp_session_socket
= -1;
177 /* listen on ldpd control socket */
178 TAILQ_INIT(&ctl_conns
);
181 if ((pkt_ptr
= calloc(1, IBUF_READ_SIZE
)) == NULL
)
184 /* Fetch next active thread. */
185 while (thread_fetch(master
, &thread
))
186 thread_call(&thread
);
192 struct if_addr
*if_addr
;
196 msgbuf_write(&iev_lde
->ibuf
.w
);
197 msgbuf_clear(&iev_lde
->ibuf
.w
);
198 close(iev_lde
->ibuf
.fd
);
199 msgbuf_write(&iev_main
->ibuf
.w
);
200 msgbuf_clear(&iev_main
->ibuf
.w
);
201 close(iev_main
->ibuf
.fd
);
202 msgbuf_clear(&iev_main_sync
->ibuf
.w
);
203 close(iev_main_sync
->ibuf
.fd
);
206 config_clear(leconf
);
209 if (sysdep
.no_pfkey
== 0) {
210 THREAD_READ_OFF(pfkey_ev
);
211 close(global
.pfkeysock
);
214 ldpe_close_sockets(AF_INET
);
215 ldpe_close_sockets(AF_INET6
);
217 /* remove addresses from global list */
218 while ((if_addr
= LIST_FIRST(&global
.addr_list
)) != NULL
) {
219 LIST_REMOVE(if_addr
, entry
);
222 while ((adj
= RB_ROOT(&global
.adj_tree
)) != NULL
)
223 adj_del(adj
, S_SHUTDOWN
);
231 log_info("ldp engine exiting");
237 ldpe_imsg_compose_parent(int type
, pid_t pid
, void *data
, uint16_t datalen
)
239 return (imsg_compose_event(iev_main
, type
, 0, pid
, -1, data
, datalen
));
243 ldpe_imsg_compose_lde(int type
, uint32_t peerid
, pid_t pid
, void *data
,
246 return (imsg_compose_event(iev_lde
, type
, peerid
, pid
, -1,
252 ldpe_dispatch_main(struct thread
*thread
)
254 static struct ldpd_conf
*nconf
;
255 struct iface
*niface
;
257 struct nbr_params
*nnbrp
;
258 static struct l2vpn
*l2vpn
, *nl2vpn
;
259 struct l2vpn_if
*lif
, *nlif
;
260 struct l2vpn_pw
*pw
, *npw
;
262 int fd
= THREAD_FD(thread
);
263 struct imsgev
*iev
= THREAD_ARG(thread
);
264 struct imsgbuf
*ibuf
= &iev
->ibuf
;
265 struct iface
*iface
= NULL
;
268 enum socket_type
*socket_type
;
269 static int disc_socket
= -1;
270 static int edisc_socket
= -1;
271 static int session_socket
= -1;
274 struct nbr_params
*nbrp
;
280 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
281 fatal("imsg_read error");
282 if (n
== 0) /* connection closed */
286 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
287 fatal("ldpe_dispatch_main: imsg_get error");
291 switch (imsg
.hdr
.type
) {
293 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
295 fatalx("IFSTATUS imsg with wrong len");
298 iface
= if_lookup_name(leconf
, kif
->ifname
);
300 if_update_info(iface
, kif
);
301 ldp_if_update(iface
, AF_UNSPEC
);
305 RB_FOREACH(l2vpn
, l2vpn_head
, &leconf
->l2vpn_tree
) {
306 lif
= l2vpn_if_find(l2vpn
, kif
->ifname
);
308 l2vpn_if_update_info(lif
, kif
);
309 l2vpn_if_update(lif
);
312 pw
= l2vpn_pw_find(l2vpn
, kif
->ifname
);
314 l2vpn_pw_update_info(pw
, kif
);
320 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
321 sizeof(struct kaddr
))
322 fatalx("NEWADDR imsg with wrong len");
324 if_addr_add(imsg
.data
);
327 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
328 sizeof(struct kaddr
))
329 fatalx("DELADDR imsg with wrong len");
331 if_addr_del(imsg
.data
);
333 case IMSG_SOCKET_IPC
:
335 log_warnx("%s: received unexpected imsg fd "
339 if ((fd
= imsg
.fd
) == -1) {
340 log_warnx("%s: expected to receive imsg fd to "
341 "lde but didn't receive any", __func__
);
345 if ((iev_lde
= malloc(sizeof(struct imsgev
))) == NULL
)
347 imsg_init(&iev_lde
->ibuf
, fd
);
348 iev_lde
->handler_read
= ldpe_dispatch_lde
;
349 iev_lde
->ev_read
= thread_add_read(master
,
350 iev_lde
->handler_read
, iev_lde
, iev_lde
->ibuf
.fd
);
351 iev_lde
->handler_write
= ldp_write_handler
;
352 iev_lde
->ev_write
= NULL
;
354 case IMSG_CLOSE_SOCKETS
:
355 af
= imsg
.hdr
.peerid
;
357 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
360 session_shutdown(nbr
, S_SHUTDOWN
, 0, 0);
364 nbr
->auth
.method
= AUTH_NONE
;
366 ldpe_close_sockets(af
);
373 if ((ldp_af_conf_get(leconf
, af
))->flags
&
375 ldpe_imsg_compose_parent(IMSG_REQUEST_SOCKETS
,
378 case IMSG_SOCKET_NET
:
379 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
380 sizeof(enum socket_type
))
381 fatalx("SOCKET_NET imsg with wrong len");
382 socket_type
= imsg
.data
;
384 switch (*socket_type
) {
385 case LDP_SOCKET_DISC
:
386 disc_socket
= imsg
.fd
;
388 case LDP_SOCKET_EDISC
:
389 edisc_socket
= imsg
.fd
;
391 case LDP_SOCKET_SESSION
:
392 session_socket
= imsg
.fd
;
396 case IMSG_SETUP_SOCKETS
:
397 af
= imsg
.hdr
.peerid
;
398 if (disc_socket
== -1 || edisc_socket
== -1 ||
399 session_socket
== -1) {
400 if (disc_socket
!= -1)
402 if (edisc_socket
!= -1)
404 if (session_socket
!= -1)
405 close(session_socket
);
409 ldpe_setup_sockets(af
, disc_socket
, edisc_socket
,
413 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
416 nbr
->laddr
= (ldp_af_conf_get(leconf
,
419 nbrp
= nbr_params_find(leconf
, nbr
->id
);
421 nbr
->auth
.method
= nbrp
->auth
.method
;
422 if (pfkey_establish(nbr
, nbrp
) == -1)
423 fatalx("pfkey setup failed");
426 if (nbr_session_active_role(nbr
))
427 nbr_establish_connection(nbr
);
430 case IMSG_RTRID_UPDATE
:
431 memcpy(&global
.rtr_id
, imsg
.data
,
432 sizeof(global
.rtr_id
));
433 if (leconf
->rtr_id
.s_addr
== INADDR_ANY
) {
434 ldpe_reset_nbrs(AF_UNSPEC
);
436 if_update_all(AF_UNSPEC
);
437 tnbr_update_all(AF_UNSPEC
);
439 case IMSG_RECONF_CONF
:
440 if ((nconf
= malloc(sizeof(struct ldpd_conf
))) ==
443 memcpy(nconf
, imsg
.data
, sizeof(struct ldpd_conf
));
445 RB_INIT(&nconf
->iface_tree
);
446 RB_INIT(&nconf
->tnbr_tree
);
447 RB_INIT(&nconf
->nbrp_tree
);
448 RB_INIT(&nconf
->l2vpn_tree
);
450 case IMSG_RECONF_IFACE
:
451 if ((niface
= malloc(sizeof(struct iface
))) == NULL
)
453 memcpy(niface
, imsg
.data
, sizeof(struct iface
));
455 RB_INSERT(iface_head
, &nconf
->iface_tree
, niface
);
457 case IMSG_RECONF_TNBR
:
458 if ((ntnbr
= malloc(sizeof(struct tnbr
))) == NULL
)
460 memcpy(ntnbr
, imsg
.data
, sizeof(struct tnbr
));
462 RB_INSERT(tnbr_head
, &nconf
->tnbr_tree
, ntnbr
);
464 case IMSG_RECONF_NBRP
:
465 if ((nnbrp
= malloc(sizeof(struct nbr_params
))) == NULL
)
467 memcpy(nnbrp
, imsg
.data
, sizeof(struct nbr_params
));
469 RB_INSERT(nbrp_head
, &nconf
->nbrp_tree
, nnbrp
);
471 case IMSG_RECONF_L2VPN
:
472 if ((nl2vpn
= malloc(sizeof(struct l2vpn
))) == NULL
)
474 memcpy(nl2vpn
, imsg
.data
, sizeof(struct l2vpn
));
476 RB_INIT(&nl2vpn
->if_tree
);
477 RB_INIT(&nl2vpn
->pw_tree
);
478 RB_INIT(&nl2vpn
->pw_inactive_tree
);
480 RB_INSERT(l2vpn_head
, &nconf
->l2vpn_tree
, nl2vpn
);
482 case IMSG_RECONF_L2VPN_IF
:
483 if ((nlif
= malloc(sizeof(struct l2vpn_if
))) == NULL
)
485 memcpy(nlif
, imsg
.data
, sizeof(struct l2vpn_if
));
487 RB_INSERT(l2vpn_if_head
, &nl2vpn
->if_tree
, nlif
);
489 case IMSG_RECONF_L2VPN_PW
:
490 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
492 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
494 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_tree
, npw
);
496 case IMSG_RECONF_L2VPN_IPW
:
497 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
499 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
501 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_inactive_tree
, npw
);
503 case IMSG_RECONF_END
:
504 merge_config(leconf
, nconf
);
505 ldp_clear_config(nconf
);
507 global
.conf_seqnum
++;
510 control_imsg_relay(&imsg
);
512 case IMSG_DEBUG_UPDATE
:
513 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
515 log_warnx("%s: wrong imsg len", __func__
);
518 memcpy(&ldp_debug
, imsg
.data
, sizeof(ldp_debug
));
521 log_debug("ldpe_dispatch_main: error handling imsg %d",
530 /* this pipe is dead, so remove the event handlers and exit */
531 THREAD_READ_OFF(iev
->ev_read
);
532 THREAD_WRITE_OFF(iev
->ev_write
);
541 ldpe_dispatch_lde(struct thread
*thread
)
543 struct imsgev
*iev
= THREAD_ARG(thread
);
544 struct imsgbuf
*ibuf
= &iev
->ibuf
;
547 struct notify_msg
*nm
;
553 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
554 fatal("imsg_read error");
555 if (n
== 0) /* connection closed */
559 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
560 fatal("ldpe_dispatch_lde: imsg_get error");
564 switch (imsg
.hdr
.type
) {
565 case IMSG_MAPPING_ADD
:
566 case IMSG_RELEASE_ADD
:
567 case IMSG_REQUEST_ADD
:
568 case IMSG_WITHDRAW_ADD
:
569 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
571 fatalx("invalid size of map request");
574 nbr
= nbr_find_peerid(imsg
.hdr
.peerid
);
576 log_debug("ldpe_dispatch_lde: cannot find "
580 if (nbr
->state
!= NBR_STA_OPER
)
583 switch (imsg
.hdr
.type
) {
584 case IMSG_MAPPING_ADD
:
585 mapping_list_add(&nbr
->mapping_list
, map
);
587 case IMSG_RELEASE_ADD
:
588 mapping_list_add(&nbr
->release_list
, map
);
590 case IMSG_REQUEST_ADD
:
591 mapping_list_add(&nbr
->request_list
, map
);
593 case IMSG_WITHDRAW_ADD
:
594 mapping_list_add(&nbr
->withdraw_list
, map
);
598 case IMSG_MAPPING_ADD_END
:
599 case IMSG_RELEASE_ADD_END
:
600 case IMSG_REQUEST_ADD_END
:
601 case IMSG_WITHDRAW_ADD_END
:
602 nbr
= nbr_find_peerid(imsg
.hdr
.peerid
);
604 log_debug("ldpe_dispatch_lde: cannot find "
608 if (nbr
->state
!= NBR_STA_OPER
)
611 switch (imsg
.hdr
.type
) {
612 case IMSG_MAPPING_ADD_END
:
613 send_labelmessage(nbr
, MSG_TYPE_LABELMAPPING
,
616 case IMSG_RELEASE_ADD_END
:
617 send_labelmessage(nbr
, MSG_TYPE_LABELRELEASE
,
620 case IMSG_REQUEST_ADD_END
:
621 send_labelmessage(nbr
, MSG_TYPE_LABELREQUEST
,
624 case IMSG_WITHDRAW_ADD_END
:
625 send_labelmessage(nbr
, MSG_TYPE_LABELWITHDRAW
,
626 &nbr
->withdraw_list
);
630 case IMSG_NOTIFICATION_SEND
:
631 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
632 sizeof(struct notify_msg
))
633 fatalx("invalid size of OE request");
636 nbr
= nbr_find_peerid(imsg
.hdr
.peerid
);
638 log_debug("ldpe_dispatch_lde: cannot find "
642 if (nbr
->state
!= NBR_STA_OPER
)
645 send_notification_full(nbr
->tcp
, nm
);
648 case IMSG_CTL_SHOW_LIB_BEGIN
:
649 case IMSG_CTL_SHOW_LIB_RCVD
:
650 case IMSG_CTL_SHOW_LIB_SENT
:
651 case IMSG_CTL_SHOW_LIB_END
:
652 case IMSG_CTL_SHOW_L2VPN_PW
:
653 case IMSG_CTL_SHOW_L2VPN_BINDING
:
654 control_imsg_relay(&imsg
);
657 log_debug("ldpe_dispatch_lde: error handling imsg %d",
666 /* this pipe is dead, so remove the event handlers and exit */
667 THREAD_READ_OFF(iev
->ev_read
);
668 THREAD_WRITE_OFF(iev
->ev_write
);
678 ldpe_dispatch_pfkey(struct thread
*thread
)
680 int fd
= THREAD_FD(thread
);
682 pfkey_ev
= thread_add_read(master
, ldpe_dispatch_pfkey
,
683 NULL
, global
.pfkeysock
);
685 if (pfkey_read(fd
, NULL
) == -1)
686 fatal("pfkey_read failed, exiting...");
690 #endif /* __OpenBSD__ */
693 ldpe_setup_sockets(int af
, int disc_socket
, int edisc_socket
,
696 struct ldpd_af_global
*af_global
;
698 af_global
= ldp_af_global_get(&global
, af
);
700 /* discovery socket */
701 af_global
->ldp_disc_socket
= disc_socket
;
702 af_global
->disc_ev
= thread_add_read(master
, disc_recv_packet
,
703 &af_global
->disc_ev
, af_global
->ldp_disc_socket
);
705 /* extended discovery socket */
706 af_global
->ldp_edisc_socket
= edisc_socket
;
707 af_global
->edisc_ev
= thread_add_read(master
, disc_recv_packet
,
708 &af_global
->edisc_ev
, af_global
->ldp_edisc_socket
);
711 af_global
->ldp_session_socket
= session_socket
;
712 accept_add(af_global
->ldp_session_socket
, session_accept
, NULL
);
716 ldpe_close_sockets(int af
)
718 struct ldpd_af_global
*af_global
;
720 af_global
= ldp_af_global_get(&global
, af
);
722 /* discovery socket */
723 THREAD_READ_OFF(af_global
->disc_ev
);
724 if (af_global
->ldp_disc_socket
!= -1) {
725 close(af_global
->ldp_disc_socket
);
726 af_global
->ldp_disc_socket
= -1;
729 /* extended discovery socket */
730 THREAD_READ_OFF(af_global
->edisc_ev
);
731 if (af_global
->ldp_edisc_socket
!= -1) {
732 close(af_global
->ldp_edisc_socket
);
733 af_global
->ldp_edisc_socket
= -1;
737 if (af_global
->ldp_session_socket
!= -1) {
738 accept_del(af_global
->ldp_session_socket
);
739 close(af_global
->ldp_session_socket
);
740 af_global
->ldp_session_socket
= -1;
745 ldpe_acl_check(char *acl_name
, int af
, union ldpd_addr
*addr
, uint8_t prefixlen
)
747 return ldp_acl_request(iev_main_sync
, acl_name
, af
, addr
, prefixlen
);
751 ldpe_reset_nbrs(int af
)
755 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
756 if (af
== AF_UNSPEC
|| nbr
->af
== af
)
757 session_shutdown(nbr
, S_SHUTDOWN
, 0, 0);
762 ldpe_reset_ds_nbrs(void)
766 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
768 session_shutdown(nbr
, S_SHUTDOWN
, 0, 0);
773 ldpe_remove_dynamic_tnbrs(int af
)
775 struct tnbr
*tnbr
, *safe
;
777 RB_FOREACH_SAFE(tnbr
, tnbr_head
, &leconf
->tnbr_tree
, safe
) {
781 tnbr
->flags
&= ~F_TNBR_DYNAMIC
;
782 tnbr_check(leconf
, tnbr
);
787 ldpe_stop_init_backoff(int af
)
791 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
792 if (nbr
->af
== af
&& nbr_pending_idtimer(nbr
)) {
793 nbr_stop_idtimer(nbr
);
794 nbr_establish_connection(nbr
);
800 ldpe_iface_af_ctl(struct ctl_conn
*c
, int af
, unsigned int idx
)
804 struct ctl_iface
*ictl
;
806 RB_FOREACH(iface
, iface_head
, &leconf
->iface_tree
) {
807 if (idx
== 0 || idx
== iface
->ifindex
) {
808 ia
= iface_af_get(iface
, af
);
812 ictl
= if_to_ctl(ia
);
813 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_INTERFACE
,
814 0, 0, -1, ictl
, sizeof(struct ctl_iface
));
820 ldpe_iface_ctl(struct ctl_conn
*c
, unsigned int idx
)
822 ldpe_iface_af_ctl(c
, AF_INET
, idx
);
823 ldpe_iface_af_ctl(c
, AF_INET6
, idx
);
827 ldpe_adj_ctl(struct ctl_conn
*c
)
830 struct ctl_adj
*actl
;
832 RB_FOREACH(adj
, global_adj_head
, &global
.adj_tree
) {
833 actl
= adj_to_ctl(adj
);
834 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISCOVERY
, 0, 0,
835 -1, actl
, sizeof(struct ctl_adj
));
838 imsg_compose_event(&c
->iev
, IMSG_CTL_END
, 0, 0, -1, NULL
, 0);
842 ldpe_adj_detail_ctl(struct ctl_conn
*c
)
847 struct ctl_adj
*actl
;
848 struct ctl_disc_if ictl
;
849 struct ctl_disc_tnbr tctl
;
851 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISCOVERY
, 0, 0, -1, NULL
, 0);
853 RB_FOREACH(iface
, iface_head
, &leconf
->iface_tree
) {
854 memset(&ictl
, 0, sizeof(ictl
));
855 ictl
.active_v4
= (iface
->ipv4
.state
== IF_STA_ACTIVE
);
856 ictl
.active_v6
= (iface
->ipv6
.state
== IF_STA_ACTIVE
);
858 if (!ictl
.active_v4
&& !ictl
.active_v6
)
861 strlcpy(ictl
.name
, iface
->name
, sizeof(ictl
.name
));
862 if (RB_EMPTY(&iface
->ipv4
.adj_tree
) &&
863 RB_EMPTY(&iface
->ipv6
.adj_tree
))
865 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_IFACE
, 0, 0,
866 -1, &ictl
, sizeof(ictl
));
868 RB_FOREACH(adj
, ia_adj_head
, &iface
->ipv4
.adj_tree
) {
869 actl
= adj_to_ctl(adj
);
870 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_ADJ
,
871 0, 0, -1, actl
, sizeof(struct ctl_adj
));
873 RB_FOREACH(adj
, ia_adj_head
, &iface
->ipv6
.adj_tree
) {
874 actl
= adj_to_ctl(adj
);
875 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_ADJ
,
876 0, 0, -1, actl
, sizeof(struct ctl_adj
));
880 RB_FOREACH(tnbr
, tnbr_head
, &leconf
->tnbr_tree
) {
881 memset(&tctl
, 0, sizeof(tctl
));
883 tctl
.addr
= tnbr
->addr
;
884 if (tnbr
->adj
== NULL
)
887 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_TNBR
, 0, 0,
888 -1, &tctl
, sizeof(tctl
));
890 if (tnbr
->adj
== NULL
)
893 actl
= adj_to_ctl(tnbr
->adj
);
894 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_ADJ
, 0, 0,
895 -1, actl
, sizeof(struct ctl_adj
));
898 imsg_compose_event(&c
->iev
, IMSG_CTL_END
, 0, 0, -1, NULL
, 0);
902 ldpe_nbr_ctl(struct ctl_conn
*c
)
905 struct ctl_adj
*actl
;
907 struct ctl_nbr
*nctl
;
909 RB_FOREACH(nbr
, nbr_addr_head
, &nbrs_by_addr
) {
910 if (nbr
->state
== NBR_STA_PRESENT
)
913 nctl
= nbr_to_ctl(nbr
);
914 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_NBR
, 0, 0, -1, nctl
,
915 sizeof(struct ctl_nbr
));
917 RB_FOREACH(adj
, nbr_adj_head
, &nbr
->adj_tree
) {
918 actl
= adj_to_ctl(adj
);
919 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_NBR_DISC
,
920 0, 0, -1, actl
, sizeof(struct ctl_adj
));
923 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_NBR_END
, 0, 0, -1,
926 imsg_compose_event(&c
->iev
, IMSG_CTL_END
, 0, 0, -1, NULL
, 0);
930 mapping_list_add(struct mapping_head
*mh
, struct map
*map
)
932 struct mapping_entry
*me
;
934 me
= calloc(1, sizeof(*me
));
939 TAILQ_INSERT_TAIL(mh
, me
, entry
);
943 mapping_list_clr(struct mapping_head
*mh
)
945 struct mapping_entry
*me
;
947 while ((me
= TAILQ_FIRST(mh
)) != NULL
) {
948 TAILQ_REMOVE(mh
, me
, entry
);