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"
38 static void ldpe_shutdown(void);
39 static int ldpe_dispatch_main(struct thread
*);
40 static int ldpe_dispatch_lde(struct thread
*);
42 static int ldpe_dispatch_pfkey(struct thread
*);
44 static void ldpe_setup_sockets(int, int, int, int);
45 static void ldpe_close_sockets(int);
46 static void ldpe_iface_af_ctl(struct ctl_conn
*c
, int af
, ifindex_t ifidx
);
47 static void ldpe_check_filter_af(int, struct ldpd_af_conf
*, const char *);
49 struct ldpd_conf
*leconf
;
51 struct ldpd_sysdep sysdep
;
54 static struct imsgev iev_main_data
;
55 static struct imsgev
*iev_main
, *iev_main_sync
;
56 static struct imsgev
*iev_lde
;
58 static struct thread
*pfkey_ev
;
62 static zebra_capabilities_t _caps_p
[] =
68 struct zebra_privs_t ldpe_privs
=
70 #if defined(VTY_GROUP)
71 .vty_group
= VTY_GROUP
,
74 .cap_num_p
= array_size(_caps_p
),
78 /* SIGINT / SIGTERM handler. */
85 static struct quagga_signal_t ldpe_signals
[] =
101 char *pkt_ptr
; /* packet buffer */
103 /* label distribution protocol engine */
107 #ifdef HAVE_SETPROCTITLE
108 setproctitle("ldp engine");
110 ldpd_process
= PROC_LDP_ENGINE
;
111 log_procname
= log_procnames
[ldpd_process
];
115 /* setup signal handler */
116 signal_init(master
, array_size(ldpe_signals
), ldpe_signals
);
118 /* setup pipes and event handlers to the parent process */
119 if ((iev_main
= calloc(1, sizeof(struct imsgev
))) == NULL
)
121 imsg_init(&iev_main
->ibuf
, LDPD_FD_ASYNC
);
122 iev_main
->handler_read
= ldpe_dispatch_main
;
123 iev_main
->ev_read
= NULL
;
124 thread_add_read(master
, iev_main
->handler_read
, iev_main
, iev_main
->ibuf
.fd
,
126 iev_main
->handler_write
= ldp_write_handler
;
128 memset(&iev_main_data
, 0, sizeof(iev_main_data
));
129 iev_main_sync
= &iev_main_data
;
130 imsg_init(&iev_main_sync
->ibuf
, LDPD_FD_SYNC
);
132 /* create base configuration */
133 leconf
= config_new_empty();
135 struct thread thread
;
136 while (thread_fetch(master
, &thread
))
137 thread_call(&thread
);
144 ldpe_init(struct ldpd_init
*init
)
147 /* This socket must be open before dropping privileges. */
148 global
.pfkeysock
= pfkey_init();
149 if (sysdep
.no_pfkey
== 0) {
151 thread_add_read(master
, ldpe_dispatch_pfkey
, NULL
, global
.pfkeysock
,
156 /* drop privileges */
157 ldpe_privs
.user
= init
->user
;
158 ldpe_privs
.group
= init
->group
;
159 zprivs_preinit(&ldpe_privs
);
160 zprivs_init(&ldpe_privs
);
162 /* listen on ldpd control socket */
163 strlcpy(ctl_sock_path
, init
->ctl_sock_path
, sizeof(ctl_sock_path
));
164 if (control_init(ctl_sock_path
) == -1)
165 fatalx("control socket setup failed");
166 TAILQ_INIT(&ctl_conns
);
169 LIST_INIT(&global
.addr_list
);
170 RB_INIT(global_adj_head
, &global
.adj_tree
);
171 TAILQ_INIT(&global
.pending_conns
);
172 if (inet_pton(AF_INET
, AllRouters_v4
, &global
.mcast_addr_v4
) != 1)
174 if (inet_pton(AF_INET6
, AllRouters_v6
, &global
.mcast_addr_v6
) != 1)
177 /* mark sockets as closed */
178 global
.ipv4
.ldp_disc_socket
= -1;
179 global
.ipv4
.ldp_edisc_socket
= -1;
180 global
.ipv4
.ldp_session_socket
= -1;
181 global
.ipv6
.ldp_disc_socket
= -1;
182 global
.ipv6
.ldp_edisc_socket
= -1;
183 global
.ipv6
.ldp_session_socket
= -1;
185 if ((pkt_ptr
= calloc(1, IBUF_READ_SIZE
)) == NULL
)
194 struct if_addr
*if_addr
;
199 msgbuf_clear(&iev_lde
->ibuf
.w
);
200 close(iev_lde
->ibuf
.fd
);
201 iev_lde
->ibuf
.fd
= -1;
203 msgbuf_clear(&iev_main
->ibuf
.w
);
204 close(iev_main
->ibuf
.fd
);
205 iev_main
->ibuf
.fd
= -1;
206 msgbuf_clear(&iev_main_sync
->ibuf
.w
);
207 close(iev_main_sync
->ibuf
.fd
);
208 iev_main_sync
->ibuf
.fd
= -1;
210 control_cleanup(ctl_sock_path
);
211 config_clear(leconf
);
214 if (sysdep
.no_pfkey
== 0) {
215 thread_cancel(&pfkey_ev
);
216 close(global
.pfkeysock
);
219 ldpe_close_sockets(AF_INET
);
220 ldpe_close_sockets(AF_INET6
);
222 /* remove addresses from global list */
223 while ((if_addr
= LIST_FIRST(&global
.addr_list
)) != NULL
) {
224 LIST_REMOVE(if_addr
, entry
);
225 assert(if_addr
!= LIST_FIRST(&global
.addr_list
));
228 while (!RB_EMPTY(global_adj_head
, &global
.adj_tree
)) {
229 adj
= RB_ROOT(global_adj_head
, &global
.adj_tree
);
231 adj_del(adj
, S_SHUTDOWN
);
240 log_info("ldp engine exiting");
249 ldpe_imsg_compose_parent(int type
, pid_t pid
, void *data
, uint16_t datalen
)
251 if (iev_main
->ibuf
.fd
== -1)
253 return (imsg_compose_event(iev_main
, type
, 0, pid
, -1, data
, datalen
));
257 ldpe_imsg_compose_parent_sync(int type
, pid_t pid
, void *data
, uint16_t datalen
)
259 if (iev_main_sync
->ibuf
.fd
== -1)
261 imsg_compose_event(iev_main_sync
, type
, 0, pid
, -1, data
, datalen
);
262 imsg_flush(&iev_main_sync
->ibuf
);
266 ldpe_imsg_compose_lde(int type
, uint32_t peerid
, pid_t pid
, void *data
,
269 if (iev_lde
->ibuf
.fd
== -1)
271 return (imsg_compose_event(iev_lde
, type
, peerid
, pid
, -1,
277 ldpe_dispatch_main(struct thread
*thread
)
279 static struct ldpd_conf
*nconf
;
280 struct iface
*niface
;
282 struct nbr_params
*nnbrp
;
283 static struct l2vpn
*l2vpn
, *nl2vpn
;
284 struct l2vpn_if
*lif
, *nlif
;
285 struct l2vpn_pw
*pw
, *npw
;
288 struct imsgev
*iev
= THREAD_ARG(thread
);
289 struct imsgbuf
*ibuf
= &iev
->ibuf
;
290 struct iface
*iface
= NULL
;
293 enum socket_type
*socket_type
;
294 static int disc_socket
= -1;
295 static int edisc_socket
= -1;
296 static int session_socket
= -1;
299 struct nbr_params
*nbrp
;
302 struct ldp_access
*laccess
;
303 struct ldp_igp_sync_if_state_req
*ldp_sync_if_state_req
;
304 struct ldp_rlfa_node
*rnode
, *rntmp
;
305 struct ldp_rlfa_client
*rclient
;
306 struct zapi_rlfa_request
*rlfa_req
;
307 struct zapi_rlfa_igp
*rlfa_igp
;
311 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
312 fatal("imsg_read error");
313 if (n
== 0) /* connection closed */
317 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
318 fatal("ldpe_dispatch_main: imsg_get error");
322 switch (imsg
.hdr
.type
) {
324 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
326 fatalx("IFSTATUS imsg with wrong len");
329 iface
= if_lookup_name(leconf
, kif
->ifname
);
331 if_update_info(iface
, kif
);
332 ldp_if_update(iface
, AF_UNSPEC
);
336 RB_FOREACH(l2vpn
, l2vpn_head
, &leconf
->l2vpn_tree
) {
337 lif
= l2vpn_if_find(l2vpn
, kif
->ifname
);
339 l2vpn_if_update_info(lif
, kif
);
340 l2vpn_if_update(lif
);
343 pw
= l2vpn_pw_find(l2vpn
, kif
->ifname
);
345 l2vpn_pw_update_info(pw
, kif
);
351 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
352 sizeof(struct kaddr
))
353 fatalx("NEWADDR imsg with wrong len");
355 if_addr_add(imsg
.data
);
358 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
359 sizeof(struct kaddr
))
360 fatalx("DELADDR imsg with wrong len");
362 if_addr_del(imsg
.data
);
364 case IMSG_SOCKET_IPC
:
366 log_warnx("%s: received unexpected imsg fd to lde", __func__
);
369 if ((fd
= imsg
.fd
) == -1) {
370 log_warnx("%s: expected to receive imsg fd to lde but didn't receive any", __func__
);
374 if ((iev_lde
= malloc(sizeof(struct imsgev
))) == NULL
)
376 imsg_init(&iev_lde
->ibuf
, fd
);
377 iev_lde
->handler_read
= ldpe_dispatch_lde
;
378 iev_lde
->ev_read
= NULL
;
379 thread_add_read(master
, iev_lde
->handler_read
, iev_lde
, iev_lde
->ibuf
.fd
,
381 iev_lde
->handler_write
= ldp_write_handler
;
382 iev_lde
->ev_write
= NULL
;
385 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
386 sizeof(struct ldpd_init
))
387 fatalx("INIT imsg with wrong len");
389 memcpy(&init
, imsg
.data
, sizeof(init
));
392 case IMSG_AGENTX_ENABLED
:
393 ldp_agentx_enabled();
395 case IMSG_CLOSE_SOCKETS
:
396 af
= imsg
.hdr
.peerid
;
398 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
401 session_shutdown(nbr
, S_SHUTDOWN
, 0, 0);
405 nbr
->auth
.method
= AUTH_NONE
;
407 ldpe_close_sockets(af
);
414 if ((ldp_af_conf_get(leconf
, af
))->flags
&
416 ldpe_imsg_compose_parent(IMSG_REQUEST_SOCKETS
,
419 case IMSG_SOCKET_NET
:
420 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
421 sizeof(enum socket_type
))
422 fatalx("SOCKET_NET imsg with wrong len");
423 socket_type
= imsg
.data
;
425 switch (*socket_type
) {
426 case LDP_SOCKET_DISC
:
427 disc_socket
= imsg
.fd
;
429 case LDP_SOCKET_EDISC
:
430 edisc_socket
= imsg
.fd
;
432 case LDP_SOCKET_SESSION
:
433 session_socket
= imsg
.fd
;
437 case IMSG_SETUP_SOCKETS
:
438 af
= imsg
.hdr
.peerid
;
439 if (disc_socket
== -1 || edisc_socket
== -1 ||
440 session_socket
== -1) {
441 if (disc_socket
!= -1)
443 if (edisc_socket
!= -1)
445 if (session_socket
!= -1)
446 close(session_socket
);
450 ldpe_setup_sockets(af
, disc_socket
, edisc_socket
,
454 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
457 nbr
->laddr
= (ldp_af_conf_get(leconf
,
460 nbrp
= nbr_params_find(leconf
, nbr
->id
);
462 nbr
->auth
.method
= nbrp
->auth
.method
;
463 if (pfkey_establish(nbr
, nbrp
) == -1)
464 fatalx("pfkey setup failed");
467 if (nbr_session_active_role(nbr
))
468 nbr_establish_connection(nbr
);
471 case IMSG_RTRID_UPDATE
:
472 memcpy(&global
.rtr_id
, imsg
.data
,
473 sizeof(global
.rtr_id
));
474 if (leconf
->rtr_id
.s_addr
== INADDR_ANY
) {
475 ldpe_reset_nbrs(AF_UNSPEC
);
477 if_update_all(AF_UNSPEC
);
478 tnbr_update_all(AF_UNSPEC
);
480 case IMSG_RECONF_CONF
:
481 if ((nconf
= malloc(sizeof(struct ldpd_conf
))) ==
484 memcpy(nconf
, imsg
.data
, sizeof(struct ldpd_conf
));
486 RB_INIT(iface_head
, &nconf
->iface_tree
);
487 RB_INIT(tnbr_head
, &nconf
->tnbr_tree
);
488 RB_INIT(nbrp_head
, &nconf
->nbrp_tree
);
489 RB_INIT(l2vpn_head
, &nconf
->l2vpn_tree
);
491 case IMSG_RECONF_IFACE
:
492 if ((niface
= malloc(sizeof(struct iface
))) == NULL
)
494 memcpy(niface
, imsg
.data
, sizeof(struct iface
));
496 RB_INSERT(iface_head
, &nconf
->iface_tree
, niface
);
498 case IMSG_RECONF_TNBR
:
499 if ((ntnbr
= malloc(sizeof(struct tnbr
))) == NULL
)
501 memcpy(ntnbr
, imsg
.data
, sizeof(struct tnbr
));
503 RB_INSERT(tnbr_head
, &nconf
->tnbr_tree
, ntnbr
);
505 case IMSG_RECONF_NBRP
:
506 if ((nnbrp
= malloc(sizeof(struct nbr_params
))) == NULL
)
508 memcpy(nnbrp
, imsg
.data
, sizeof(struct nbr_params
));
510 RB_INSERT(nbrp_head
, &nconf
->nbrp_tree
, nnbrp
);
512 case IMSG_RECONF_L2VPN
:
513 if ((nl2vpn
= malloc(sizeof(struct l2vpn
))) == NULL
)
515 memcpy(nl2vpn
, imsg
.data
, sizeof(struct l2vpn
));
517 RB_INIT(l2vpn_if_head
, &nl2vpn
->if_tree
);
518 RB_INIT(l2vpn_pw_head
, &nl2vpn
->pw_tree
);
519 RB_INIT(l2vpn_pw_head
, &nl2vpn
->pw_inactive_tree
);
521 RB_INSERT(l2vpn_head
, &nconf
->l2vpn_tree
, nl2vpn
);
523 case IMSG_RECONF_L2VPN_IF
:
524 if ((nlif
= malloc(sizeof(struct l2vpn_if
))) == NULL
)
526 memcpy(nlif
, imsg
.data
, sizeof(struct l2vpn_if
));
528 RB_INSERT(l2vpn_if_head
, &nl2vpn
->if_tree
, nlif
);
530 case IMSG_RECONF_L2VPN_PW
:
531 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
533 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
535 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_tree
, npw
);
537 case IMSG_RECONF_L2VPN_IPW
:
538 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
540 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
542 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_inactive_tree
, npw
);
544 case IMSG_RECONF_END
:
545 merge_config(leconf
, nconf
);
546 ldp_clear_config(nconf
);
548 global
.conf_seqnum
++;
551 control_imsg_relay(&imsg
);
553 case IMSG_DEBUG_UPDATE
:
554 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
556 log_warnx("%s: wrong imsg len", __func__
);
559 memcpy(&ldp_debug
, imsg
.data
, sizeof(ldp_debug
));
561 case IMSG_FILTER_UPDATE
:
562 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
563 sizeof(struct ldp_access
)) {
564 log_warnx("%s: wrong imsg len", __func__
);
568 ldpe_check_filter_af(AF_INET
, &leconf
->ipv4
,
570 ldpe_check_filter_af(AF_INET6
, &leconf
->ipv6
,
573 case IMSG_LDP_SYNC_IF_STATE_REQUEST
:
574 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
575 sizeof(struct ldp_igp_sync_if_state_req
)) {
576 log_warnx("%s: wrong imsg len", __func__
);
579 ldp_sync_if_state_req
= imsg
.data
;
580 ldp_sync_fsm_state_req(ldp_sync_if_state_req
);
583 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
584 sizeof(struct zapi_rlfa_request
)) {
585 log_warnx("%s: wrong imsg len", __func__
);
588 rlfa_req
= imsg
.data
;
590 rnode
= rlfa_node_find(&rlfa_req
->destination
,
591 rlfa_req
->pq_address
);
593 rnode
= rlfa_node_new(&rlfa_req
->destination
,
594 rlfa_req
->pq_address
);
595 rclient
= rlfa_client_find(rnode
, &rlfa_req
->igp
);
597 /* RLFA already registered - do nothing */
599 rclient
= rlfa_client_new(rnode
, &rlfa_req
->igp
);
600 ldpe_rlfa_init(rclient
);
602 case IMSG_RLFA_UNREG_ALL
:
603 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
604 sizeof(struct zapi_rlfa_igp
)) {
605 log_warnx("%s: wrong imsg len", __func__
);
608 rlfa_igp
= imsg
.data
;
610 RB_FOREACH_SAFE (rnode
, ldp_rlfa_node_head
,
611 &rlfa_node_tree
, rntmp
) {
612 rclient
= rlfa_client_find(rnode
, rlfa_igp
);
616 ldpe_rlfa_exit(rclient
);
617 rlfa_client_del(rclient
);
621 log_debug("ldpe_dispatch_main: error handling imsg %d",
630 /* this pipe is dead, so remove the event handlers and exit */
631 thread_cancel(&iev
->ev_read
);
632 thread_cancel(&iev
->ev_write
);
641 ldpe_dispatch_lde(struct thread
*thread
)
643 struct imsgev
*iev
= THREAD_ARG(thread
);
644 struct imsgbuf
*ibuf
= &iev
->ibuf
;
647 struct notify_msg
*nm
;
653 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
654 fatal("imsg_read error");
655 if (n
== 0) /* connection closed */
659 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
660 fatal("ldpe_dispatch_lde: imsg_get error");
664 switch (imsg
.hdr
.type
) {
665 case IMSG_MAPPING_ADD
:
666 case IMSG_RELEASE_ADD
:
667 case IMSG_REQUEST_ADD
:
668 case IMSG_WITHDRAW_ADD
:
669 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
671 fatalx("invalid size of map request");
674 nbr
= nbr_find_peerid(imsg
.hdr
.peerid
);
677 if (nbr
->state
!= NBR_STA_OPER
)
680 switch (imsg
.hdr
.type
) {
681 case IMSG_MAPPING_ADD
:
682 mapping_list_add(&nbr
->mapping_list
, map
);
684 case IMSG_RELEASE_ADD
:
685 mapping_list_add(&nbr
->release_list
, map
);
687 case IMSG_REQUEST_ADD
:
688 mapping_list_add(&nbr
->request_list
, map
);
690 case IMSG_WITHDRAW_ADD
:
691 mapping_list_add(&nbr
->withdraw_list
, map
);
695 case IMSG_MAPPING_ADD_END
:
696 case IMSG_RELEASE_ADD_END
:
697 case IMSG_REQUEST_ADD_END
:
698 case IMSG_WITHDRAW_ADD_END
:
699 nbr
= nbr_find_peerid(imsg
.hdr
.peerid
);
702 if (nbr
->state
!= NBR_STA_OPER
)
705 switch (imsg
.hdr
.type
) {
706 case IMSG_MAPPING_ADD_END
:
707 send_labelmessage(nbr
, MSG_TYPE_LABELMAPPING
,
710 case IMSG_RELEASE_ADD_END
:
711 send_labelmessage(nbr
, MSG_TYPE_LABELRELEASE
,
714 case IMSG_REQUEST_ADD_END
:
715 send_labelmessage(nbr
, MSG_TYPE_LABELREQUEST
,
718 case IMSG_WITHDRAW_ADD_END
:
719 send_labelmessage(nbr
, MSG_TYPE_LABELWITHDRAW
,
720 &nbr
->withdraw_list
);
724 case IMSG_NOTIFICATION_SEND
:
725 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
726 sizeof(struct notify_msg
))
727 fatalx("invalid size of OE request");
730 nbr
= nbr_find_peerid(imsg
.hdr
.peerid
);
732 log_debug("ldpe_dispatch_lde: cannot find neighbor");
735 if (nbr
->state
!= NBR_STA_OPER
)
738 send_notification_full(nbr
->tcp
, nm
);
741 case IMSG_CTL_SHOW_LIB_BEGIN
:
742 case IMSG_CTL_SHOW_LIB_RCVD
:
743 case IMSG_CTL_SHOW_LIB_SENT
:
744 case IMSG_CTL_SHOW_LIB_END
:
745 case IMSG_CTL_SHOW_L2VPN_PW
:
746 case IMSG_CTL_SHOW_L2VPN_BINDING
:
747 control_imsg_relay(&imsg
);
749 case IMSG_NBR_SHUTDOWN
:
750 nbr
= nbr_find_peerid(imsg
.hdr
.peerid
);
752 log_debug("ldpe_dispatch_lde: cannot find neighbor");
755 if (nbr
->state
!= NBR_STA_OPER
)
757 session_shutdown(nbr
,S_SHUTDOWN
,0,0);
760 log_debug("ldpe_dispatch_lde: error handling imsg %d",
769 /* this pipe is dead, so remove the event handlers and exit */
770 thread_cancel(&iev
->ev_read
);
771 thread_cancel(&iev
->ev_write
);
781 ldpe_dispatch_pfkey(struct thread
*thread
)
783 int fd
= THREAD_FD(thread
);
786 thread_add_read(master
, ldpe_dispatch_pfkey
, NULL
, global
.pfkeysock
,
789 if (pfkey_read(fd
, NULL
) == -1)
790 fatal("pfkey_read failed, exiting...");
794 #endif /* __OpenBSD__ */
797 ldpe_setup_sockets(int af
, int disc_socket
, int edisc_socket
,
800 struct ldpd_af_global
*af_global
;
802 af_global
= ldp_af_global_get(&global
, af
);
804 /* discovery socket */
805 af_global
->ldp_disc_socket
= disc_socket
;
806 af_global
->disc_ev
= NULL
;
807 thread_add_read(master
, disc_recv_packet
, &af_global
->disc_ev
, af_global
->ldp_disc_socket
,
808 &af_global
->disc_ev
);
810 /* extended discovery socket */
811 af_global
->ldp_edisc_socket
= edisc_socket
;
812 af_global
->edisc_ev
= NULL
;
813 thread_add_read(master
, disc_recv_packet
, &af_global
->edisc_ev
, af_global
->ldp_edisc_socket
,
814 &af_global
->edisc_ev
);
817 af_global
->ldp_session_socket
= session_socket
;
818 accept_add(af_global
->ldp_session_socket
, session_accept
, NULL
);
822 ldpe_close_sockets(int af
)
824 struct ldpd_af_global
*af_global
;
826 af_global
= ldp_af_global_get(&global
, af
);
828 /* discovery socket */
829 thread_cancel(&af_global
->disc_ev
);
830 if (af_global
->ldp_disc_socket
!= -1) {
831 close(af_global
->ldp_disc_socket
);
832 af_global
->ldp_disc_socket
= -1;
835 /* extended discovery socket */
836 thread_cancel(&af_global
->edisc_ev
);
837 if (af_global
->ldp_edisc_socket
!= -1) {
838 close(af_global
->ldp_edisc_socket
);
839 af_global
->ldp_edisc_socket
= -1;
843 if (af_global
->ldp_session_socket
!= -1) {
844 accept_del(af_global
->ldp_session_socket
);
845 close(af_global
->ldp_session_socket
);
846 af_global
->ldp_session_socket
= -1;
851 ldpe_acl_check(char *acl_name
, int af
, union ldpd_addr
*addr
, uint8_t prefixlen
)
853 return ldp_acl_request(iev_main_sync
, acl_name
, af
, addr
, prefixlen
);
857 ldpe_reset_nbrs(int af
)
861 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
862 if (af
== AF_UNSPEC
|| nbr
->af
== af
)
863 session_shutdown(nbr
, S_SHUTDOWN
, 0, 0);
868 ldpe_reset_ds_nbrs(void)
872 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
874 session_shutdown(nbr
, S_SHUTDOWN
, 0, 0);
879 ldpe_remove_dynamic_tnbrs(int af
)
881 struct tnbr
*tnbr
, *safe
;
883 RB_FOREACH_SAFE(tnbr
, tnbr_head
, &leconf
->tnbr_tree
, safe
) {
887 tnbr
->flags
&= ~F_TNBR_DYNAMIC
;
888 tnbr_check(leconf
, tnbr
);
893 ldpe_stop_init_backoff(int af
)
897 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
898 if (nbr
->af
== af
&& nbr_pending_idtimer(nbr
)) {
899 nbr_stop_idtimer(nbr
);
900 nbr_establish_connection(nbr
);
906 ldpe_iface_af_ctl(struct ctl_conn
*c
, int af
, ifindex_t idx
)
910 struct ctl_iface
*ictl
;
912 RB_FOREACH(iface
, iface_head
, &leconf
->iface_tree
) {
913 if (idx
== 0 || idx
== iface
->ifindex
) {
914 ia
= iface_af_get(iface
, af
);
918 ictl
= if_to_ctl(ia
);
919 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_INTERFACE
,
920 0, 0, -1, ictl
, sizeof(struct ctl_iface
));
926 ldpe_iface_ctl(struct ctl_conn
*c
, ifindex_t idx
)
928 ldpe_iface_af_ctl(c
, AF_INET
, idx
);
929 ldpe_iface_af_ctl(c
, AF_INET6
, idx
);
933 ldpe_adj_ctl(struct ctl_conn
*c
)
936 struct ctl_adj
*actl
;
938 RB_FOREACH(adj
, global_adj_head
, &global
.adj_tree
) {
939 actl
= adj_to_ctl(adj
);
940 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISCOVERY
, 0, 0,
941 -1, actl
, sizeof(struct ctl_adj
));
944 imsg_compose_event(&c
->iev
, IMSG_CTL_END
, 0, 0, -1, NULL
, 0);
948 ldpe_adj_detail_ctl(struct ctl_conn
*c
)
953 struct ctl_adj
*actl
;
954 struct ctl_disc_if ictl
;
955 struct ctl_disc_tnbr tctl
;
957 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISCOVERY
, 0, 0, -1, NULL
, 0);
959 RB_FOREACH(iface
, iface_head
, &leconf
->iface_tree
) {
960 memset(&ictl
, 0, sizeof(ictl
));
961 ictl
.active_v4
= (iface
->ipv4
.state
== IF_STA_ACTIVE
);
962 ictl
.active_v6
= (iface
->ipv6
.state
== IF_STA_ACTIVE
);
964 if (!ictl
.active_v4
&& !ictl
.active_v6
)
967 strlcpy(ictl
.name
, iface
->name
, sizeof(ictl
.name
));
968 if (RB_EMPTY(ia_adj_head
, &iface
->ipv4
.adj_tree
) &&
969 RB_EMPTY(ia_adj_head
, &iface
->ipv6
.adj_tree
))
971 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_IFACE
, 0, 0,
972 -1, &ictl
, sizeof(ictl
));
974 RB_FOREACH(adj
, ia_adj_head
, &iface
->ipv4
.adj_tree
) {
975 actl
= adj_to_ctl(adj
);
976 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_ADJ
,
977 0, 0, -1, actl
, sizeof(struct ctl_adj
));
979 RB_FOREACH(adj
, ia_adj_head
, &iface
->ipv6
.adj_tree
) {
980 actl
= adj_to_ctl(adj
);
981 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_ADJ
,
982 0, 0, -1, actl
, sizeof(struct ctl_adj
));
986 RB_FOREACH(tnbr
, tnbr_head
, &leconf
->tnbr_tree
) {
987 memset(&tctl
, 0, sizeof(tctl
));
989 tctl
.addr
= tnbr
->addr
;
990 if (tnbr
->adj
== NULL
)
993 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_TNBR
, 0, 0,
994 -1, &tctl
, sizeof(tctl
));
996 if (tnbr
->adj
== NULL
)
999 actl
= adj_to_ctl(tnbr
->adj
);
1000 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_DISC_ADJ
, 0, 0,
1001 -1, actl
, sizeof(struct ctl_adj
));
1004 imsg_compose_event(&c
->iev
, IMSG_CTL_END
, 0, 0, -1, NULL
, 0);
1008 ldpe_nbr_ctl(struct ctl_conn
*c
)
1011 struct ctl_adj
*actl
;
1013 struct ctl_nbr
*nctl
;
1015 RB_FOREACH(nbr
, nbr_addr_head
, &nbrs_by_addr
) {
1016 if (nbr
->state
== NBR_STA_PRESENT
)
1019 nctl
= nbr_to_ctl(nbr
);
1020 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_NBR
, 0, 0, -1, nctl
,
1021 sizeof(struct ctl_nbr
));
1023 RB_FOREACH(adj
, nbr_adj_head
, &nbr
->adj_tree
) {
1024 actl
= adj_to_ctl(adj
);
1025 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_NBR_DISC
,
1026 0, 0, -1, actl
, sizeof(struct ctl_adj
));
1029 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_NBR_END
, 0, 0, -1,
1032 imsg_compose_event(&c
->iev
, IMSG_CTL_END
, 0, 0, -1, NULL
, 0);
1036 ldpe_ldp_sync_ctl(struct ctl_conn
*c
)
1038 struct iface
*iface
;
1039 struct ctl_ldp_sync
*ictl
;
1041 RB_FOREACH(iface
, iface_head
, &leconf
->iface_tree
) {
1042 ictl
= ldp_sync_to_ctl(iface
);
1043 imsg_compose_event(&c
->iev
, IMSG_CTL_SHOW_LDP_SYNC
,
1044 0, 0, -1, ictl
, sizeof(struct ctl_ldp_sync
));
1046 imsg_compose_event(&c
->iev
, IMSG_CTL_END
, 0, 0, -1, NULL
, 0);
1050 mapping_list_add(struct mapping_head
*mh
, struct map
*map
)
1052 struct mapping_entry
*me
;
1054 me
= calloc(1, sizeof(*me
));
1059 TAILQ_INSERT_TAIL(mh
, me
, entry
);
1063 mapping_list_clr(struct mapping_head
*mh
)
1065 struct mapping_entry
*me
;
1067 while ((me
= TAILQ_FIRST(mh
)) != NULL
) {
1068 TAILQ_REMOVE(mh
, me
, entry
);
1069 assert(me
!= TAILQ_FIRST(mh
));
1075 ldpe_check_filter_af(int af
, struct ldpd_af_conf
*af_conf
,
1076 const char *filter_name
)
1078 if (strcmp(af_conf
->acl_thello_accept_from
, filter_name
) == 0)
1079 ldpe_remove_dynamic_tnbrs(af
);
1083 ldpe_set_config_change_time(void)
1085 /* SNMP update time when ever there is a config change */
1086 leconf
->config_change_time
= time(NULL
);