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
*c
, int af
, ifindex_t ifidx
);
45 static void ldpe_check_filter_af(int, struct ldpd_af_conf
*, const char *);
47 struct ldpd_conf
*leconf
;
49 struct ldpd_sysdep sysdep
;
52 static struct imsgev
*iev_main
, *iev_main_sync
;
53 static struct imsgev
*iev_lde
;
55 static struct thread
*pfkey_ev
;
59 static zebra_capabilities_t _caps_p
[] =
65 struct zebra_privs_t ldpe_privs
=
67 #if defined(VTY_GROUP)
68 .vty_group
= VTY_GROUP
,
71 .cap_num_p
= array_size(_caps_p
),
75 /* SIGINT / SIGTERM handler. */
82 static struct quagga_signal_t ldpe_signals
[] =
98 char *pkt_ptr
; /* packet buffer */
100 /* label distribution protocol engine */
104 struct thread thread
;
106 #ifdef HAVE_SETPROCTITLE
107 setproctitle("ldp engine");
109 ldpd_process
= PROC_LDP_ENGINE
;
110 log_procname
= log_procnames
[ldpd_process
];
112 master
= thread_master_create(NULL
);
114 /* setup signal handler */
115 signal_init(master
, array_size(ldpe_signals
), ldpe_signals
);
117 /* setup pipes and event handlers to the parent process */
118 if ((iev_main
= calloc(1, sizeof(struct imsgev
))) == 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
,
125 iev_main
->handler_write
= ldp_write_handler
;
127 if ((iev_main_sync
= calloc(1, sizeof(struct imsgev
))) == NULL
)
129 imsg_init(&iev_main_sync
->ibuf
, LDPD_FD_SYNC
);
131 /* create base configuration */
132 leconf
= config_new_empty();
134 /* Fetch next active thread. */
135 while (thread_fetch(master
, &thread
))
136 thread_call(&thread
);
140 ldpe_init(struct ldpd_init
*init
)
143 /* This socket must be open before dropping privileges. */
144 global
.pfkeysock
= pfkey_init();
145 if (sysdep
.no_pfkey
== 0) {
147 thread_add_read(master
, ldpe_dispatch_pfkey
, NULL
, global
.pfkeysock
,
152 /* drop privileges */
153 ldpe_privs
.user
= init
->user
;
154 ldpe_privs
.group
= init
->group
;
155 zprivs_preinit(&ldpe_privs
);
156 zprivs_init(&ldpe_privs
);
158 /* listen on ldpd control socket */
159 strlcpy(ctl_sock_path
, init
->ctl_sock_path
, sizeof(ctl_sock_path
));
160 if (control_init(ctl_sock_path
) == -1)
161 fatalx("control socket setup failed");
162 TAILQ_INIT(&ctl_conns
);
165 LIST_INIT(&global
.addr_list
);
166 RB_INIT(global_adj_head
, &global
.adj_tree
);
167 TAILQ_INIT(&global
.pending_conns
);
168 if (inet_pton(AF_INET
, AllRouters_v4
, &global
.mcast_addr_v4
) != 1)
170 if (inet_pton(AF_INET6
, AllRouters_v6
, &global
.mcast_addr_v6
) != 1)
173 /* mark sockets as closed */
174 global
.ipv4
.ldp_disc_socket
= -1;
175 global
.ipv4
.ldp_edisc_socket
= -1;
176 global
.ipv4
.ldp_session_socket
= -1;
177 global
.ipv6
.ldp_disc_socket
= -1;
178 global
.ipv6
.ldp_edisc_socket
= -1;
179 global
.ipv6
.ldp_session_socket
= -1;
181 if ((pkt_ptr
= calloc(1, IBUF_READ_SIZE
)) == NULL
)
190 struct if_addr
*if_addr
;
195 msgbuf_clear(&iev_lde
->ibuf
.w
);
196 close(iev_lde
->ibuf
.fd
);
197 iev_lde
->ibuf
.fd
= -1;
199 msgbuf_clear(&iev_main
->ibuf
.w
);
200 close(iev_main
->ibuf
.fd
);
201 iev_main
->ibuf
.fd
= -1;
202 msgbuf_clear(&iev_main_sync
->ibuf
.w
);
203 close(iev_main_sync
->ibuf
.fd
);
204 iev_main_sync
->ibuf
.fd
= -1;
206 control_cleanup(ctl_sock_path
);
207 config_clear(leconf
);
210 if (sysdep
.no_pfkey
== 0) {
211 THREAD_READ_OFF(pfkey_ev
);
212 close(global
.pfkeysock
);
215 ldpe_close_sockets(AF_INET
);
216 ldpe_close_sockets(AF_INET6
);
218 /* remove addresses from global list */
219 while ((if_addr
= LIST_FIRST(&global
.addr_list
)) != NULL
) {
220 LIST_REMOVE(if_addr
, entry
);
221 assert(if_addr
!= LIST_FIRST(&global
.addr_list
));
224 while (!RB_EMPTY(global_adj_head
, &global
.adj_tree
)) {
225 adj
= RB_ROOT(global_adj_head
, &global
.adj_tree
);
227 adj_del(adj
, S_SHUTDOWN
);
237 log_info("ldp engine exiting");
246 ldpe_imsg_compose_parent(int type
, pid_t pid
, void *data
, uint16_t datalen
)
248 if (iev_main
->ibuf
.fd
== -1)
250 return (imsg_compose_event(iev_main
, type
, 0, pid
, -1, data
, datalen
));
254 ldpe_imsg_compose_parent_sync(int type
, pid_t pid
, void *data
, uint16_t datalen
)
256 if (iev_main_sync
->ibuf
.fd
== -1)
258 imsg_compose_event(iev_main_sync
, type
, 0, pid
, -1, data
, datalen
);
259 imsg_flush(&iev_main_sync
->ibuf
);
263 ldpe_imsg_compose_lde(int type
, uint32_t peerid
, pid_t pid
, void *data
,
266 if (iev_lde
->ibuf
.fd
== -1)
268 return (imsg_compose_event(iev_lde
, type
, peerid
, pid
, -1,
274 ldpe_dispatch_main(struct thread
*thread
)
276 static struct ldpd_conf
*nconf
;
277 struct iface
*niface
;
279 struct nbr_params
*nnbrp
;
280 static struct l2vpn
*l2vpn
, *nl2vpn
;
281 struct l2vpn_if
*lif
, *nlif
;
282 struct l2vpn_pw
*pw
, *npw
;
285 struct imsgev
*iev
= THREAD_ARG(thread
);
286 struct imsgbuf
*ibuf
= &iev
->ibuf
;
287 struct iface
*iface
= NULL
;
290 enum socket_type
*socket_type
;
291 static int disc_socket
= -1;
292 static int edisc_socket
= -1;
293 static int session_socket
= -1;
296 struct nbr_params
*nbrp
;
299 struct ldp_access
*laccess
;
300 struct ldp_igp_sync_if_state_req
*ldp_sync_if_state_req
;
304 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
305 fatal("imsg_read error");
306 if (n
== 0) /* connection closed */
310 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
311 fatal("ldpe_dispatch_main: imsg_get error");
315 switch (imsg
.hdr
.type
) {
317 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
319 fatalx("IFSTATUS imsg with wrong len");
322 iface
= if_lookup_name(leconf
, kif
->ifname
);
324 if_update_info(iface
, kif
);
325 ldp_if_update(iface
, AF_UNSPEC
);
329 RB_FOREACH(l2vpn
, l2vpn_head
, &leconf
->l2vpn_tree
) {
330 lif
= l2vpn_if_find(l2vpn
, kif
->ifname
);
332 l2vpn_if_update_info(lif
, kif
);
333 l2vpn_if_update(lif
);
336 pw
= l2vpn_pw_find(l2vpn
, kif
->ifname
);
338 l2vpn_pw_update_info(pw
, kif
);
344 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
345 sizeof(struct kaddr
))
346 fatalx("NEWADDR imsg with wrong len");
348 if_addr_add(imsg
.data
);
351 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
352 sizeof(struct kaddr
))
353 fatalx("DELADDR imsg with wrong len");
355 if_addr_del(imsg
.data
);
357 case IMSG_SOCKET_IPC
:
359 log_warnx("%s: received unexpected imsg fd to lde", __func__
);
362 if ((fd
= imsg
.fd
) == -1) {
363 log_warnx("%s: expected to receive imsg fd to lde but didn't receive any", __func__
);
367 if ((iev_lde
= malloc(sizeof(struct imsgev
))) == NULL
)
369 imsg_init(&iev_lde
->ibuf
, fd
);
370 iev_lde
->handler_read
= ldpe_dispatch_lde
;
371 iev_lde
->ev_read
= NULL
;
372 thread_add_read(master
, iev_lde
->handler_read
, iev_lde
, iev_lde
->ibuf
.fd
,
374 iev_lde
->handler_write
= ldp_write_handler
;
375 iev_lde
->ev_write
= NULL
;
378 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
379 sizeof(struct ldpd_init
))
380 fatalx("INIT imsg with wrong len");
382 memcpy(&init
, imsg
.data
, sizeof(init
));
385 case IMSG_CLOSE_SOCKETS
:
386 af
= imsg
.hdr
.peerid
;
388 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
391 session_shutdown(nbr
, S_SHUTDOWN
, 0, 0);
395 nbr
->auth
.method
= AUTH_NONE
;
397 ldpe_close_sockets(af
);
404 if ((ldp_af_conf_get(leconf
, af
))->flags
&
406 ldpe_imsg_compose_parent(IMSG_REQUEST_SOCKETS
,
409 case IMSG_SOCKET_NET
:
410 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
411 sizeof(enum socket_type
))
412 fatalx("SOCKET_NET imsg with wrong len");
413 socket_type
= imsg
.data
;
415 switch (*socket_type
) {
416 case LDP_SOCKET_DISC
:
417 disc_socket
= imsg
.fd
;
419 case LDP_SOCKET_EDISC
:
420 edisc_socket
= imsg
.fd
;
422 case LDP_SOCKET_SESSION
:
423 session_socket
= imsg
.fd
;
427 case IMSG_SETUP_SOCKETS
:
428 af
= imsg
.hdr
.peerid
;
429 if (disc_socket
== -1 || edisc_socket
== -1 ||
430 session_socket
== -1) {
431 if (disc_socket
!= -1)
433 if (edisc_socket
!= -1)
435 if (session_socket
!= -1)
436 close(session_socket
);
440 ldpe_setup_sockets(af
, disc_socket
, edisc_socket
,
444 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
447 nbr
->laddr
= (ldp_af_conf_get(leconf
,
450 nbrp
= nbr_params_find(leconf
, nbr
->id
);
452 nbr
->auth
.method
= nbrp
->auth
.method
;
453 if (pfkey_establish(nbr
, nbrp
) == -1)
454 fatalx("pfkey setup failed");
457 if (nbr_session_active_role(nbr
))
458 nbr_establish_connection(nbr
);
461 case IMSG_RTRID_UPDATE
:
462 memcpy(&global
.rtr_id
, imsg
.data
,
463 sizeof(global
.rtr_id
));
464 if (leconf
->rtr_id
.s_addr
== INADDR_ANY
) {
465 ldpe_reset_nbrs(AF_UNSPEC
);
467 if_update_all(AF_UNSPEC
);
468 tnbr_update_all(AF_UNSPEC
);
470 case IMSG_RECONF_CONF
:
471 if ((nconf
= malloc(sizeof(struct ldpd_conf
))) ==
474 memcpy(nconf
, imsg
.data
, sizeof(struct ldpd_conf
));
476 RB_INIT(iface_head
, &nconf
->iface_tree
);
477 RB_INIT(tnbr_head
, &nconf
->tnbr_tree
);
478 RB_INIT(nbrp_head
, &nconf
->nbrp_tree
);
479 RB_INIT(l2vpn_head
, &nconf
->l2vpn_tree
);
481 case IMSG_RECONF_IFACE
:
482 if ((niface
= malloc(sizeof(struct iface
))) == NULL
)
484 memcpy(niface
, imsg
.data
, sizeof(struct iface
));
486 RB_INSERT(iface_head
, &nconf
->iface_tree
, niface
);
488 case IMSG_RECONF_TNBR
:
489 if ((ntnbr
= malloc(sizeof(struct tnbr
))) == NULL
)
491 memcpy(ntnbr
, imsg
.data
, sizeof(struct tnbr
));
493 RB_INSERT(tnbr_head
, &nconf
->tnbr_tree
, ntnbr
);
495 case IMSG_RECONF_NBRP
:
496 if ((nnbrp
= malloc(sizeof(struct nbr_params
))) == NULL
)
498 memcpy(nnbrp
, imsg
.data
, sizeof(struct nbr_params
));
500 RB_INSERT(nbrp_head
, &nconf
->nbrp_tree
, nnbrp
);
502 case IMSG_RECONF_L2VPN
:
503 if ((nl2vpn
= malloc(sizeof(struct l2vpn
))) == NULL
)
505 memcpy(nl2vpn
, imsg
.data
, sizeof(struct l2vpn
));
507 RB_INIT(l2vpn_if_head
, &nl2vpn
->if_tree
);
508 RB_INIT(l2vpn_pw_head
, &nl2vpn
->pw_tree
);
509 RB_INIT(l2vpn_pw_head
, &nl2vpn
->pw_inactive_tree
);
511 RB_INSERT(l2vpn_head
, &nconf
->l2vpn_tree
, nl2vpn
);
513 case IMSG_RECONF_L2VPN_IF
:
514 if ((nlif
= malloc(sizeof(struct l2vpn_if
))) == NULL
)
516 memcpy(nlif
, imsg
.data
, sizeof(struct l2vpn_if
));
518 RB_INSERT(l2vpn_if_head
, &nl2vpn
->if_tree
, nlif
);
520 case IMSG_RECONF_L2VPN_PW
:
521 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
523 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
525 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_tree
, npw
);
527 case IMSG_RECONF_L2VPN_IPW
:
528 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
530 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
532 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_inactive_tree
, npw
);
534 case IMSG_RECONF_END
:
535 merge_config(leconf
, nconf
);
536 ldp_clear_config(nconf
);
538 global
.conf_seqnum
++;
541 control_imsg_relay(&imsg
);
543 case IMSG_DEBUG_UPDATE
:
544 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
546 log_warnx("%s: wrong imsg len", __func__
);
549 memcpy(&ldp_debug
, imsg
.data
, sizeof(ldp_debug
));
551 case IMSG_FILTER_UPDATE
:
552 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
553 sizeof(struct ldp_access
)) {
554 log_warnx("%s: wrong imsg len", __func__
);
558 ldpe_check_filter_af(AF_INET
, &leconf
->ipv4
,
560 ldpe_check_filter_af(AF_INET6
, &leconf
->ipv6
,
563 case IMSG_LDP_SYNC_IF_STATE_REQUEST
:
564 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
565 sizeof(struct ldp_igp_sync_if_state_req
)) {
566 log_warnx("%s: wrong imsg len", __func__
);
569 ldp_sync_if_state_req
= imsg
.data
;
570 ldp_sync_fsm_state_req(ldp_sync_if_state_req
);
573 log_debug("ldpe_dispatch_main: error handling imsg %d",
582 /* this pipe is dead, so remove the event handlers and exit */
583 THREAD_READ_OFF(iev
->ev_read
);
584 THREAD_WRITE_OFF(iev
->ev_write
);
593 ldpe_dispatch_lde(struct thread
*thread
)
595 struct imsgev
*iev
= THREAD_ARG(thread
);
596 struct imsgbuf
*ibuf
= &iev
->ibuf
;
599 struct notify_msg
*nm
;
605 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
606 fatal("imsg_read error");
607 if (n
== 0) /* connection closed */
611 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
612 fatal("ldpe_dispatch_lde: imsg_get error");
616 switch (imsg
.hdr
.type
) {
617 case IMSG_MAPPING_ADD
:
618 case IMSG_RELEASE_ADD
:
619 case IMSG_REQUEST_ADD
:
620 case IMSG_WITHDRAW_ADD
:
621 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
623 fatalx("invalid size of map request");
626 nbr
= nbr_find_peerid(imsg
.hdr
.peerid
);
629 if (nbr
->state
!= NBR_STA_OPER
)
632 switch (imsg
.hdr
.type
) {
633 case IMSG_MAPPING_ADD
:
634 mapping_list_add(&nbr
->mapping_list
, map
);
636 case IMSG_RELEASE_ADD
:
637 mapping_list_add(&nbr
->release_list
, map
);
639 case IMSG_REQUEST_ADD
:
640 mapping_list_add(&nbr
->request_list
, map
);
642 case IMSG_WITHDRAW_ADD
:
643 mapping_list_add(&nbr
->withdraw_list
, map
);
647 case IMSG_MAPPING_ADD_END
:
648 case IMSG_RELEASE_ADD_END
:
649 case IMSG_REQUEST_ADD_END
:
650 case IMSG_WITHDRAW_ADD_END
:
651 nbr
= nbr_find_peerid(imsg
.hdr
.peerid
);
654 if (nbr
->state
!= NBR_STA_OPER
)
657 switch (imsg
.hdr
.type
) {
658 case IMSG_MAPPING_ADD_END
:
659 send_labelmessage(nbr
, MSG_TYPE_LABELMAPPING
,
662 case IMSG_RELEASE_ADD_END
:
663 send_labelmessage(nbr
, MSG_TYPE_LABELRELEASE
,
666 case IMSG_REQUEST_ADD_END
:
667 send_labelmessage(nbr
, MSG_TYPE_LABELREQUEST
,
670 case IMSG_WITHDRAW_ADD_END
:
671 send_labelmessage(nbr
, MSG_TYPE_LABELWITHDRAW
,
672 &nbr
->withdraw_list
);
676 case IMSG_NOTIFICATION_SEND
:
677 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
678 sizeof(struct notify_msg
))
679 fatalx("invalid size of OE request");
682 nbr
= nbr_find_peerid(imsg
.hdr
.peerid
);
684 log_debug("ldpe_dispatch_lde: cannot find neighbor");
687 if (nbr
->state
!= NBR_STA_OPER
)
690 send_notification_full(nbr
->tcp
, nm
);
693 case IMSG_CTL_SHOW_LIB_BEGIN
:
694 case IMSG_CTL_SHOW_LIB_RCVD
:
695 case IMSG_CTL_SHOW_LIB_SENT
:
696 case IMSG_CTL_SHOW_LIB_END
:
697 case IMSG_CTL_SHOW_L2VPN_PW
:
698 case IMSG_CTL_SHOW_L2VPN_BINDING
:
699 control_imsg_relay(&imsg
);
701 case IMSG_NBR_SHUTDOWN
:
702 nbr
= nbr_find_peerid(imsg
.hdr
.peerid
);
704 log_debug("ldpe_dispatch_lde: cannot find neighbor");
707 if (nbr
->state
!= NBR_STA_OPER
)
709 session_shutdown(nbr
,S_SHUTDOWN
,0,0);
712 log_debug("ldpe_dispatch_lde: error handling imsg %d",
721 /* this pipe is dead, so remove the event handlers and exit */
722 THREAD_READ_OFF(iev
->ev_read
);
723 THREAD_WRITE_OFF(iev
->ev_write
);
733 ldpe_dispatch_pfkey(struct thread
*thread
)
735 int fd
= THREAD_FD(thread
);
738 thread_add_read(master
, ldpe_dispatch_pfkey
, NULL
, global
.pfkeysock
,
741 if (pfkey_read(fd
, NULL
) == -1)
742 fatal("pfkey_read failed, exiting...");
746 #endif /* __OpenBSD__ */
749 ldpe_setup_sockets(int af
, int disc_socket
, int edisc_socket
,
752 struct ldpd_af_global
*af_global
;
754 af_global
= ldp_af_global_get(&global
, af
);
756 /* discovery socket */
757 af_global
->ldp_disc_socket
= disc_socket
;
758 af_global
->disc_ev
= NULL
;
759 thread_add_read(master
, disc_recv_packet
, &af_global
->disc_ev
, af_global
->ldp_disc_socket
,
760 &af_global
->disc_ev
);
762 /* extended discovery socket */
763 af_global
->ldp_edisc_socket
= edisc_socket
;
764 af_global
->edisc_ev
= NULL
;
765 thread_add_read(master
, disc_recv_packet
, &af_global
->edisc_ev
, af_global
->ldp_edisc_socket
,
766 &af_global
->edisc_ev
);
769 af_global
->ldp_session_socket
= session_socket
;
770 accept_add(af_global
->ldp_session_socket
, session_accept
, NULL
);
774 ldpe_close_sockets(int af
)
776 struct ldpd_af_global
*af_global
;
778 af_global
= ldp_af_global_get(&global
, af
);
780 /* discovery socket */
781 THREAD_READ_OFF(af_global
->disc_ev
);
782 if (af_global
->ldp_disc_socket
!= -1) {
783 close(af_global
->ldp_disc_socket
);
784 af_global
->ldp_disc_socket
= -1;
787 /* extended discovery socket */
788 THREAD_READ_OFF(af_global
->edisc_ev
);
789 if (af_global
->ldp_edisc_socket
!= -1) {
790 close(af_global
->ldp_edisc_socket
);
791 af_global
->ldp_edisc_socket
= -1;
795 if (af_global
->ldp_session_socket
!= -1) {
796 accept_del(af_global
->ldp_session_socket
);
797 close(af_global
->ldp_session_socket
);
798 af_global
->ldp_session_socket
= -1;
803 ldpe_acl_check(char *acl_name
, int af
, union ldpd_addr
*addr
, uint8_t prefixlen
)
805 return ldp_acl_request(iev_main_sync
, acl_name
, af
, addr
, prefixlen
);
809 ldpe_reset_nbrs(int af
)
813 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
814 if (af
== AF_UNSPEC
|| nbr
->af
== af
)
815 session_shutdown(nbr
, S_SHUTDOWN
, 0, 0);
820 ldpe_reset_ds_nbrs(void)
824 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
826 session_shutdown(nbr
, S_SHUTDOWN
, 0, 0);
831 ldpe_remove_dynamic_tnbrs(int af
)
833 struct tnbr
*tnbr
, *safe
;
835 RB_FOREACH_SAFE(tnbr
, tnbr_head
, &leconf
->tnbr_tree
, safe
) {
839 tnbr
->flags
&= ~F_TNBR_DYNAMIC
;
840 tnbr_check(leconf
, tnbr
);
845 ldpe_stop_init_backoff(int af
)
849 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
850 if (nbr
->af
== af
&& nbr_pending_idtimer(nbr
)) {
851 nbr_stop_idtimer(nbr
);
852 nbr_establish_connection(nbr
);
858 ldpe_iface_af_ctl(struct ctl_conn
*c
, int af
, ifindex_t idx
)
862 struct ctl_iface
*ictl
;
864 RB_FOREACH(iface
, iface_head
, &leconf
->iface_tree
) {
865 if (idx
== 0 || idx
== iface
->ifindex
) {
866 ia
= iface_af_get(iface
, af
);
870 ictl
= if_to_ctl(ia
);
871 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_INTERFACE
,
872 0, 0, -1, ictl
, sizeof(struct ctl_iface
));
878 ldpe_iface_ctl(struct ctl_conn
*c
, ifindex_t idx
)
880 ldpe_iface_af_ctl(c
, AF_INET
, idx
);
881 ldpe_iface_af_ctl(c
, AF_INET6
, idx
);
885 ldpe_adj_ctl(struct ctl_conn
*c
)
888 struct ctl_adj
*actl
;
890 RB_FOREACH(adj
, global_adj_head
, &global
.adj_tree
) {
891 actl
= adj_to_ctl(adj
);
892 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISCOVERY
, 0, 0,
893 -1, actl
, sizeof(struct ctl_adj
));
896 imsg_compose_event(&c
->iev
, IMSG_CTL_END
, 0, 0, -1, NULL
, 0);
900 ldpe_adj_detail_ctl(struct ctl_conn
*c
)
905 struct ctl_adj
*actl
;
906 struct ctl_disc_if ictl
;
907 struct ctl_disc_tnbr tctl
;
909 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISCOVERY
, 0, 0, -1, NULL
, 0);
911 RB_FOREACH(iface
, iface_head
, &leconf
->iface_tree
) {
912 memset(&ictl
, 0, sizeof(ictl
));
913 ictl
.active_v4
= (iface
->ipv4
.state
== IF_STA_ACTIVE
);
914 ictl
.active_v6
= (iface
->ipv6
.state
== IF_STA_ACTIVE
);
916 if (!ictl
.active_v4
&& !ictl
.active_v6
)
919 strlcpy(ictl
.name
, iface
->name
, sizeof(ictl
.name
));
920 if (RB_EMPTY(ia_adj_head
, &iface
->ipv4
.adj_tree
) &&
921 RB_EMPTY(ia_adj_head
, &iface
->ipv6
.adj_tree
))
923 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_IFACE
, 0, 0,
924 -1, &ictl
, sizeof(ictl
));
926 RB_FOREACH(adj
, ia_adj_head
, &iface
->ipv4
.adj_tree
) {
927 actl
= adj_to_ctl(adj
);
928 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_ADJ
,
929 0, 0, -1, actl
, sizeof(struct ctl_adj
));
931 RB_FOREACH(adj
, ia_adj_head
, &iface
->ipv6
.adj_tree
) {
932 actl
= adj_to_ctl(adj
);
933 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_ADJ
,
934 0, 0, -1, actl
, sizeof(struct ctl_adj
));
938 RB_FOREACH(tnbr
, tnbr_head
, &leconf
->tnbr_tree
) {
939 memset(&tctl
, 0, sizeof(tctl
));
941 tctl
.addr
= tnbr
->addr
;
942 if (tnbr
->adj
== NULL
)
945 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_TNBR
, 0, 0,
946 -1, &tctl
, sizeof(tctl
));
948 if (tnbr
->adj
== NULL
)
951 actl
= adj_to_ctl(tnbr
->adj
);
952 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_ADJ
, 0, 0,
953 -1, actl
, sizeof(struct ctl_adj
));
956 imsg_compose_event(&c
->iev
, IMSG_CTL_END
, 0, 0, -1, NULL
, 0);
960 ldpe_nbr_ctl(struct ctl_conn
*c
)
963 struct ctl_adj
*actl
;
965 struct ctl_nbr
*nctl
;
967 RB_FOREACH(nbr
, nbr_addr_head
, &nbrs_by_addr
) {
968 if (nbr
->state
== NBR_STA_PRESENT
)
971 nctl
= nbr_to_ctl(nbr
);
972 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_NBR
, 0, 0, -1, nctl
,
973 sizeof(struct ctl_nbr
));
975 RB_FOREACH(adj
, nbr_adj_head
, &nbr
->adj_tree
) {
976 actl
= adj_to_ctl(adj
);
977 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_NBR_DISC
,
978 0, 0, -1, actl
, sizeof(struct ctl_adj
));
981 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_NBR_END
, 0, 0, -1,
984 imsg_compose_event(&c
->iev
, IMSG_CTL_END
, 0, 0, -1, NULL
, 0);
988 ldpe_ldp_sync_ctl(struct ctl_conn
*c
)
991 struct ctl_ldp_sync
*ictl
;
993 RB_FOREACH(iface
, iface_head
, &leconf
->iface_tree
) {
994 ictl
= ldp_sync_to_ctl(iface
);
995 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_LDP_SYNC
,
996 0, 0, -1, ictl
, sizeof(struct ctl_ldp_sync
));
998 imsg_compose_event(&c
->iev
, IMSG_CTL_END
, 0, 0, -1, NULL
, 0);
1002 mapping_list_add(struct mapping_head
*mh
, struct map
*map
)
1004 struct mapping_entry
*me
;
1006 me
= calloc(1, sizeof(*me
));
1011 TAILQ_INSERT_TAIL(mh
, me
, entry
);
1015 mapping_list_clr(struct mapping_head
*mh
)
1017 struct mapping_entry
*me
;
1019 while ((me
= TAILQ_FIRST(mh
)) != NULL
) {
1020 TAILQ_REMOVE(mh
, me
, entry
);
1021 assert(me
!= TAILQ_FIRST(mh
));
1027 ldpe_check_filter_af(int af
, struct ldpd_af_conf
*af_conf
,
1028 const char *filter_name
)
1030 if (strcmp(af_conf
->acl_thello_accept_from
, filter_name
) == 0)
1031 ldpe_remove_dynamic_tnbrs(af
);