4 * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
5 * Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org>
6 * Copyright (c) 2004 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 #include <lib/linklist.h>
42 static void lde_shutdown(void);
43 static int lde_dispatch_imsg(struct thread
*);
44 static int lde_dispatch_parent(struct thread
*);
45 static __inline
int lde_nbr_compare(const struct lde_nbr
*,
46 const struct lde_nbr
*);
47 static struct lde_nbr
*lde_nbr_new(uint32_t, struct lde_nbr
*);
48 static void lde_nbr_del(struct lde_nbr
*);
49 static struct lde_nbr
*lde_nbr_find(uint32_t);
50 static void lde_nbr_clear(void);
51 static void lde_nbr_addr_update(struct lde_nbr
*,
52 struct lde_addr
*, int);
53 static __inline
int lde_map_compare(const struct lde_map
*,
54 const struct lde_map
*);
55 static void lde_map_free(void *);
56 static int lde_address_add(struct lde_nbr
*, struct lde_addr
*);
57 static int lde_address_del(struct lde_nbr
*, struct lde_addr
*);
58 static void lde_address_list_free(struct lde_nbr
*);
59 static void zclient_sync_init(void);
60 static void lde_label_list_init(void);
61 static int lde_get_label_chunk(void);
62 static void on_get_label_chunk_response(uint32_t start
, uint32_t end
);
63 static uint32_t lde_get_next_label(void);
64 static bool lde_fec_connected(const struct fec_node
*);
65 static bool lde_fec_outside_mpls_network(const struct fec_node
*);
66 static void lde_check_filter_af(int, struct ldpd_af_conf
*,
69 RB_GENERATE(nbr_tree
, lde_nbr
, entry
, lde_nbr_compare
)
70 RB_GENERATE(lde_map_head
, lde_map
, entry
, lde_map_compare
)
72 struct ldpd_conf
*ldeconf
;
73 struct nbr_tree lde_nbrs
= RB_INITIALIZER(&lde_nbrs
);
75 static struct imsgev
*iev_ldpe
;
76 static struct imsgev
*iev_main
, *iev_main_sync
;
79 static zebra_capabilities_t _caps_p
[] =
84 static struct zebra_privs_t lde_privs
=
86 #if defined(VTY_GROUP)
87 .vty_group
= VTY_GROUP
,
90 .cap_num_p
= array_size(_caps_p
),
94 /* List of chunks of labels externally assigned by Zebra */
95 static struct list
*label_chunk_list
;
96 static struct listnode
*current_label_chunk
;
98 /* Synchronous zclient to request labels */
99 static struct zclient
*zclient_sync
;
101 /* SIGINT / SIGTERM handler. */
108 static struct quagga_signal_t lde_signals
[] =
124 /* label decision engine */
128 struct thread thread
;
130 #ifdef HAVE_SETPROCTITLE
131 setproctitle("label decision engine");
133 ldpd_process
= PROC_LDE_ENGINE
;
134 log_procname
= log_procnames
[PROC_LDE_ENGINE
];
136 master
= thread_master_create(NULL
);
138 /* setup signal handler */
139 signal_init(master
, array_size(lde_signals
), lde_signals
);
141 /* setup pipes and event handlers to the parent process */
142 if ((iev_main
= calloc(1, sizeof(struct imsgev
))) == NULL
)
144 imsg_init(&iev_main
->ibuf
, LDPD_FD_ASYNC
);
145 iev_main
->handler_read
= lde_dispatch_parent
;
146 iev_main
->ev_read
= NULL
;
147 thread_add_read(master
, iev_main
->handler_read
, iev_main
, iev_main
->ibuf
.fd
,
149 iev_main
->handler_write
= ldp_write_handler
;
151 if ((iev_main_sync
= calloc(1, sizeof(struct imsgev
))) == NULL
)
153 imsg_init(&iev_main_sync
->ibuf
, LDPD_FD_SYNC
);
155 /* create base configuration */
156 ldeconf
= config_new_empty();
158 /* Fetch next active thread. */
159 while (thread_fetch(master
, &thread
))
160 thread_call(&thread
);
164 lde_init(struct ldpd_init
*init
)
166 /* drop privileges */
167 lde_privs
.user
= init
->user
;
168 lde_privs
.group
= init
->group
;
169 zprivs_preinit(&lde_privs
);
170 zprivs_init(&lde_privs
);
172 /* start the LIB garbage collector */
173 lde_gc_start_timer();
175 /* Init synchronous zclient and label list */
176 frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
177 init
->zclient_serv_path
);
186 msgbuf_clear(&iev_ldpe
->ibuf
.w
);
187 close(iev_ldpe
->ibuf
.fd
);
188 iev_ldpe
->ibuf
.fd
= -1;
190 msgbuf_clear(&iev_main
->ibuf
.w
);
191 close(iev_main
->ibuf
.fd
);
192 iev_main
->ibuf
.fd
= -1;
193 msgbuf_clear(&iev_main_sync
->ibuf
.w
);
194 close(iev_main_sync
->ibuf
.fd
);
195 iev_main_sync
->ibuf
.fd
= -1;
201 config_clear(ldeconf
);
208 log_info("label decision engine exiting");
216 lde_imsg_compose_parent(int type
, pid_t pid
, void *data
, uint16_t datalen
)
218 if (iev_main
->ibuf
.fd
== -1)
220 return (imsg_compose_event(iev_main
, type
, 0, pid
, -1, data
, datalen
));
224 lde_imsg_compose_parent_sync(int type
, pid_t pid
, void *data
, uint16_t datalen
)
226 if (iev_main_sync
->ibuf
.fd
== -1)
228 imsg_compose_event(iev_main_sync
, type
, 0, pid
, -1, data
, datalen
);
229 imsg_flush(&iev_main_sync
->ibuf
);
233 lde_imsg_compose_ldpe(int type
, uint32_t peerid
, pid_t pid
, void *data
,
236 if (iev_ldpe
->ibuf
.fd
== -1)
238 return (imsg_compose_event(iev_ldpe
, type
, peerid
, pid
,
244 lde_dispatch_imsg(struct thread
*thread
)
246 struct imsgev
*iev
= THREAD_ARG(thread
);
247 struct imsgbuf
*ibuf
= &iev
->ibuf
;
251 struct lde_addr
*lde_addr
;
252 struct notify_msg
*nm
;
258 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
259 fatal("imsg_read error");
260 if (n
== 0) /* connection closed */
264 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
265 fatal("lde_dispatch_imsg: imsg_get error");
269 switch (imsg
.hdr
.type
) {
270 case IMSG_LABEL_MAPPING_FULL
:
271 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
273 log_debug("%s: cannot find lde neighbor",
280 case IMSG_LABEL_MAPPING
:
281 case IMSG_LABEL_REQUEST
:
282 case IMSG_LABEL_RELEASE
:
283 case IMSG_LABEL_WITHDRAW
:
284 case IMSG_LABEL_ABORT
:
285 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
287 fatalx("lde_dispatch_imsg: wrong imsg len");
290 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
292 log_debug("%s: cannot find lde neighbor",
297 switch (imsg
.hdr
.type
) {
298 case IMSG_LABEL_MAPPING
:
299 lde_check_mapping(map
, ln
, 1);
301 case IMSG_LABEL_REQUEST
:
302 lde_check_request(map
, ln
);
304 case IMSG_LABEL_RELEASE
:
305 lde_check_release(map
, ln
);
307 case IMSG_LABEL_WITHDRAW
:
308 lde_check_withdraw(map
, ln
);
310 case IMSG_LABEL_ABORT
:
315 case IMSG_ADDRESS_ADD
:
316 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
317 sizeof(struct lde_addr
))
318 fatalx("lde_dispatch_imsg: wrong imsg len");
319 lde_addr
= imsg
.data
;
321 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
323 log_debug("%s: cannot find lde neighbor",
327 if (lde_address_add(ln
, lde_addr
) < 0) {
328 log_debug("%s: cannot add address %s, it already exists", __func__
,
329 log_addr(lde_addr
->af
, &lde_addr
->addr
));
332 case IMSG_ADDRESS_DEL
:
333 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
334 sizeof(struct lde_addr
))
335 fatalx("lde_dispatch_imsg: wrong imsg len");
336 lde_addr
= imsg
.data
;
338 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
340 log_debug("%s: cannot find lde neighbor",
344 if (lde_address_del(ln
, lde_addr
) < 0) {
345 log_debug("%s: cannot delete address %s, it does not exist", __func__
,
346 log_addr(lde_addr
->af
, &lde_addr
->addr
));
349 case IMSG_NOTIFICATION
:
350 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
351 sizeof(struct notify_msg
))
352 fatalx("lde_dispatch_imsg: wrong imsg len");
355 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
357 log_debug("%s: cannot find lde neighbor",
362 switch (nm
->status_code
) {
364 l2vpn_recv_pw_status(ln
, nm
);
368 * Do nothing for now. Should be useful in
369 * the future when we implement LDP-IGP
370 * Synchronization (RFC 5443) and Graceful
371 * Restart (RFC 3478).
377 case IMSG_NEIGHBOR_UP
:
378 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
379 sizeof(struct lde_nbr
))
380 fatalx("lde_dispatch_imsg: wrong imsg len");
382 if (lde_nbr_find(imsg
.hdr
.peerid
))
383 fatalx("lde_dispatch_imsg: neighbor already exists");
384 lde_nbr_new(imsg
.hdr
.peerid
, imsg
.data
);
386 case IMSG_NEIGHBOR_DOWN
:
387 lde_nbr_del(lde_nbr_find(imsg
.hdr
.peerid
));
389 case IMSG_CTL_SHOW_LIB
:
390 rt_dump(imsg
.hdr
.pid
);
392 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
393 imsg
.hdr
.pid
, NULL
, 0);
395 case IMSG_CTL_SHOW_L2VPN_PW
:
396 l2vpn_pw_ctl(imsg
.hdr
.pid
);
398 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
399 imsg
.hdr
.pid
, NULL
, 0);
401 case IMSG_CTL_SHOW_L2VPN_BINDING
:
402 l2vpn_binding_ctl(imsg
.hdr
.pid
);
404 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
405 imsg
.hdr
.pid
, NULL
, 0);
408 log_debug("%s: unexpected imsg %d", __func__
,
417 /* this pipe is dead, so remove the event handlers and exit */
418 THREAD_READ_OFF(iev
->ev_read
);
419 THREAD_WRITE_OFF(iev
->ev_write
);
428 lde_dispatch_parent(struct thread
*thread
)
430 static struct ldpd_conf
*nconf
;
431 struct iface
*iface
, *niface
;
433 struct nbr_params
*nnbrp
;
434 static struct l2vpn
*l2vpn
, *nl2vpn
;
435 struct l2vpn_if
*lif
, *nlif
;
436 struct l2vpn_pw
*pw
, *npw
;
441 struct imsgev
*iev
= THREAD_ARG(thread
);
442 struct imsgbuf
*ibuf
= &iev
->ibuf
;
446 struct ldp_access
*laccess
;
450 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
451 fatal("imsg_read error");
452 if (n
== 0) /* connection closed */
456 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
457 fatal("lde_dispatch_parent: imsg_get error");
461 switch (imsg
.hdr
.type
) {
463 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
465 fatalx("IFSTATUS imsg with wrong len");
468 iface
= if_lookup_name(ldeconf
, kif
->ifname
);
470 if_update_info(iface
, kif
);
474 RB_FOREACH(l2vpn
, l2vpn_head
, &ldeconf
->l2vpn_tree
) {
475 lif
= l2vpn_if_find(l2vpn
, kif
->ifname
);
477 l2vpn_if_update_info(lif
, kif
);
480 pw
= l2vpn_pw_find(l2vpn
, kif
->ifname
);
482 l2vpn_pw_update_info(pw
, kif
);
488 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
489 sizeof(struct zapi_pw_status
))
490 fatalx("PW_UPDATE imsg with wrong len");
492 if (l2vpn_pw_status_update(imsg
.data
) != 0)
493 log_warnx("%s: error updating PW status",
496 case IMSG_NETWORK_ADD
:
497 case IMSG_NETWORK_UPDATE
:
498 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
499 sizeof(struct kroute
)) {
500 log_warnx("%s: wrong imsg len", __func__
);
507 fec
.type
= FEC_TYPE_IPV4
;
508 fec
.u
.ipv4
.prefix
= kr
->prefix
.v4
;
509 fec
.u
.ipv4
.prefixlen
= kr
->prefixlen
;
512 fec
.type
= FEC_TYPE_IPV6
;
513 fec
.u
.ipv6
.prefix
= kr
->prefix
.v6
;
514 fec
.u
.ipv6
.prefixlen
= kr
->prefixlen
;
517 fatalx("lde_dispatch_parent: unknown af");
520 switch (imsg
.hdr
.type
) {
521 case IMSG_NETWORK_ADD
:
522 lde_kernel_insert(&fec
, kr
->af
, &kr
->nexthop
,
523 kr
->ifindex
, kr
->route_type
,
525 kr
->flags
& F_CONNECTED
, NULL
);
527 case IMSG_NETWORK_UPDATE
:
528 lde_kernel_update(&fec
);
532 case IMSG_SOCKET_IPC
:
534 log_warnx("%s: received unexpected imsg fd to ldpe", __func__
);
537 if ((fd
= imsg
.fd
) == -1) {
538 log_warnx("%s: expected to receive imsg fd to ldpe but didn't receive any", __func__
);
542 if ((iev_ldpe
= malloc(sizeof(struct imsgev
))) == NULL
)
544 imsg_init(&iev_ldpe
->ibuf
, fd
);
545 iev_ldpe
->handler_read
= lde_dispatch_imsg
;
546 iev_ldpe
->ev_read
= NULL
;
547 thread_add_read(master
, iev_ldpe
->handler_read
, iev_ldpe
, iev_ldpe
->ibuf
.fd
,
549 iev_ldpe
->handler_write
= ldp_write_handler
;
550 iev_ldpe
->ev_write
= NULL
;
553 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
554 sizeof(struct ldpd_init
))
555 fatalx("INIT imsg with wrong len");
557 memcpy(&init
, imsg
.data
, sizeof(init
));
560 case IMSG_RECONF_CONF
:
561 if ((nconf
= malloc(sizeof(struct ldpd_conf
))) ==
564 memcpy(nconf
, imsg
.data
, sizeof(struct ldpd_conf
));
566 RB_INIT(iface_head
, &nconf
->iface_tree
);
567 RB_INIT(tnbr_head
, &nconf
->tnbr_tree
);
568 RB_INIT(nbrp_head
, &nconf
->nbrp_tree
);
569 RB_INIT(l2vpn_head
, &nconf
->l2vpn_tree
);
571 case IMSG_RECONF_IFACE
:
572 if ((niface
= malloc(sizeof(struct iface
))) == NULL
)
574 memcpy(niface
, imsg
.data
, sizeof(struct iface
));
576 RB_INSERT(iface_head
, &nconf
->iface_tree
, niface
);
578 case IMSG_RECONF_TNBR
:
579 if ((ntnbr
= malloc(sizeof(struct tnbr
))) == NULL
)
581 memcpy(ntnbr
, imsg
.data
, sizeof(struct tnbr
));
583 RB_INSERT(tnbr_head
, &nconf
->tnbr_tree
, ntnbr
);
585 case IMSG_RECONF_NBRP
:
586 if ((nnbrp
= malloc(sizeof(struct nbr_params
))) == NULL
)
588 memcpy(nnbrp
, imsg
.data
, sizeof(struct nbr_params
));
590 RB_INSERT(nbrp_head
, &nconf
->nbrp_tree
, nnbrp
);
592 case IMSG_RECONF_L2VPN
:
593 if ((nl2vpn
= malloc(sizeof(struct l2vpn
))) == NULL
)
595 memcpy(nl2vpn
, imsg
.data
, sizeof(struct l2vpn
));
597 RB_INIT(l2vpn_if_head
, &nl2vpn
->if_tree
);
598 RB_INIT(l2vpn_pw_head
, &nl2vpn
->pw_tree
);
599 RB_INIT(l2vpn_pw_head
, &nl2vpn
->pw_inactive_tree
);
601 RB_INSERT(l2vpn_head
, &nconf
->l2vpn_tree
, nl2vpn
);
603 case IMSG_RECONF_L2VPN_IF
:
604 if ((nlif
= malloc(sizeof(struct l2vpn_if
))) == NULL
)
606 memcpy(nlif
, imsg
.data
, sizeof(struct l2vpn_if
));
608 RB_INSERT(l2vpn_if_head
, &nl2vpn
->if_tree
, nlif
);
610 case IMSG_RECONF_L2VPN_PW
:
611 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
613 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
615 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_tree
, npw
);
617 case IMSG_RECONF_L2VPN_IPW
:
618 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
620 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
622 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_inactive_tree
, npw
);
624 case IMSG_RECONF_END
:
625 merge_config(ldeconf
, nconf
);
626 ldp_clear_config(nconf
);
629 case IMSG_DEBUG_UPDATE
:
630 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
632 log_warnx("%s: wrong imsg len", __func__
);
635 memcpy(&ldp_debug
, imsg
.data
, sizeof(ldp_debug
));
637 case IMSG_FILTER_UPDATE
:
638 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
639 sizeof(struct ldp_access
)) {
640 log_warnx("%s: wrong imsg len", __func__
);
644 lde_check_filter_af(AF_INET
, &ldeconf
->ipv4
,
646 lde_check_filter_af(AF_INET6
, &ldeconf
->ipv6
,
650 log_debug("%s: unexpected imsg %d", __func__
,
659 /* this pipe is dead, so remove the event handlers and exit */
660 THREAD_READ_OFF(iev
->ev_read
);
661 THREAD_WRITE_OFF(iev
->ev_write
);
669 lde_acl_check(char *acl_name
, int af
, union ldpd_addr
*addr
, uint8_t prefixlen
)
671 return ldp_acl_request(iev_main_sync
, acl_name
, af
, addr
, prefixlen
);
674 static bool lde_fec_connected(const struct fec_node
*fn
)
678 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
)
679 if (fnh
->flags
& F_FEC_NH_CONNECTED
)
685 static bool lde_fec_outside_mpls_network(const struct fec_node
*fn
)
689 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
)
690 if (!(fnh
->flags
& F_FEC_NH_NO_LDP
))
697 lde_update_label(struct fec_node
*fn
)
700 /* should we allocate a label for this fec? */
701 switch (fn
->fec
.type
) {
703 if ((ldeconf
->ipv4
.flags
& F_LDPD_AF_ALLOCHOSTONLY
) &&
704 fn
->fec
.u
.ipv4
.prefixlen
!= 32)
706 if (lde_acl_check(ldeconf
->ipv4
.acl_label_allocate_for
,
707 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
708 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
712 if ((ldeconf
->ipv6
.flags
& F_LDPD_AF_ALLOCHOSTONLY
) &&
713 fn
->fec
.u
.ipv6
.prefixlen
!= 128)
715 if (lde_acl_check(ldeconf
->ipv6
.acl_label_allocate_for
,
716 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
717 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
725 * If connected interface act as egress for fec.
726 * If LDP is not configured on an interface but there
727 * are other NHs with interfaces configured with LDP
728 * then don't act as an egress for the fec, otherwise
729 * act as an egress for the fec
731 if (lde_fec_connected(fn
) || lde_fec_outside_mpls_network(fn
)) {
732 /* choose implicit or explicit-null depending on configuration */
733 switch (fn
->fec
.type
) {
735 if (!(ldeconf
->ipv4
.flags
& F_LDPD_AF_EXPNULL
))
736 return (MPLS_LABEL_IMPLICIT_NULL
);
737 if (lde_acl_check(ldeconf
->ipv4
.acl_label_expnull_for
,
738 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
739 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
740 return (MPLS_LABEL_IMPLICIT_NULL
);
741 return MPLS_LABEL_IPV4_EXPLICIT_NULL
;
743 if (!(ldeconf
->ipv6
.flags
& F_LDPD_AF_EXPNULL
))
744 return (MPLS_LABEL_IMPLICIT_NULL
);
745 if (lde_acl_check(ldeconf
->ipv6
.acl_label_expnull_for
,
746 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
747 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
748 return (MPLS_LABEL_IMPLICIT_NULL
);
749 return MPLS_LABEL_IPV6_EXPLICIT_NULL
;
755 /* preserve current label if there's no need to update it */
756 if (fn
->local_label
!= NO_LABEL
&&
757 fn
->local_label
> MPLS_LABEL_RESERVED_MAX
)
758 return (fn
->local_label
);
760 return (lde_get_next_label());
764 lde_send_change_klabel(struct fec_node
*fn
, struct fec_nh
*fnh
)
771 * Ordered Control: don't program label into HW until a
772 * labelmap msg has been received from upstream router
774 if (fnh
->flags
& F_FEC_NH_DEFER
)
777 switch (fn
->fec
.type
) {
779 memset(&kr
, 0, sizeof(kr
));
781 kr
.prefix
.v4
= fn
->fec
.u
.ipv4
.prefix
;
782 kr
.prefixlen
= fn
->fec
.u
.ipv4
.prefixlen
;
783 kr
.nexthop
.v4
= fnh
->nexthop
.v4
;
784 kr
.ifindex
= fnh
->ifindex
;
785 kr
.local_label
= fn
->local_label
;
786 kr
.remote_label
= fnh
->remote_label
;
787 kr
.route_type
= fnh
->route_type
;
788 kr
.route_instance
= fnh
->route_instance
;
790 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE
, 0, &kr
,
794 memset(&kr
, 0, sizeof(kr
));
796 kr
.prefix
.v6
= fn
->fec
.u
.ipv6
.prefix
;
797 kr
.prefixlen
= fn
->fec
.u
.ipv6
.prefixlen
;
798 kr
.nexthop
.v6
= fnh
->nexthop
.v6
;
799 kr
.ifindex
= fnh
->ifindex
;
800 kr
.local_label
= fn
->local_label
;
801 kr
.remote_label
= fnh
->remote_label
;
802 kr
.route_type
= fnh
->route_type
;
803 kr
.route_instance
= fnh
->route_instance
;
805 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE
, 0, &kr
,
809 pw
= (struct l2vpn_pw
*) fn
->data
;
810 if (!pw
|| fn
->local_label
== NO_LABEL
||
811 fnh
->remote_label
== NO_LABEL
)
816 zpw
.local_label
= fn
->local_label
;
817 zpw
.remote_label
= fnh
->remote_label
;
818 lde_imsg_compose_parent(IMSG_KPW_SET
, 0, &zpw
, sizeof(zpw
));
824 lde_send_delete_klabel(struct fec_node
*fn
, struct fec_nh
*fnh
)
830 switch (fn
->fec
.type
) {
832 memset(&kr
, 0, sizeof(kr
));
834 kr
.prefix
.v4
= fn
->fec
.u
.ipv4
.prefix
;
835 kr
.prefixlen
= fn
->fec
.u
.ipv4
.prefixlen
;
836 kr
.nexthop
.v4
= fnh
->nexthop
.v4
;
837 kr
.ifindex
= fnh
->ifindex
;
838 kr
.local_label
= fn
->local_label
;
839 kr
.remote_label
= fnh
->remote_label
;
840 kr
.route_type
= fnh
->route_type
;
841 kr
.route_instance
= fnh
->route_instance
;
843 lde_imsg_compose_parent(IMSG_KLABEL_DELETE
, 0, &kr
,
847 memset(&kr
, 0, sizeof(kr
));
849 kr
.prefix
.v6
= fn
->fec
.u
.ipv6
.prefix
;
850 kr
.prefixlen
= fn
->fec
.u
.ipv6
.prefixlen
;
851 kr
.nexthop
.v6
= fnh
->nexthop
.v6
;
852 kr
.ifindex
= fnh
->ifindex
;
853 kr
.local_label
= fn
->local_label
;
854 kr
.remote_label
= fnh
->remote_label
;
855 kr
.route_type
= fnh
->route_type
;
856 kr
.route_instance
= fnh
->route_instance
;
858 lde_imsg_compose_parent(IMSG_KLABEL_DELETE
, 0, &kr
,
862 pw
= (struct l2vpn_pw
*) fn
->data
;
868 zpw
.local_label
= fn
->local_label
;
869 zpw
.remote_label
= fnh
->remote_label
;
870 lde_imsg_compose_parent(IMSG_KPW_UNSET
, 0, &zpw
, sizeof(zpw
));
876 lde_fec2map(struct fec
*fec
, struct map
*map
)
878 memset(map
, 0, sizeof(*map
));
882 map
->type
= MAP_TYPE_PREFIX
;
883 map
->fec
.prefix
.af
= AF_INET
;
884 map
->fec
.prefix
.prefix
.v4
= fec
->u
.ipv4
.prefix
;
885 map
->fec
.prefix
.prefixlen
= fec
->u
.ipv4
.prefixlen
;
888 map
->type
= MAP_TYPE_PREFIX
;
889 map
->fec
.prefix
.af
= AF_INET6
;
890 map
->fec
.prefix
.prefix
.v6
= fec
->u
.ipv6
.prefix
;
891 map
->fec
.prefix
.prefixlen
= fec
->u
.ipv6
.prefixlen
;
894 map
->type
= MAP_TYPE_PWID
;
895 map
->fec
.pwid
.type
= fec
->u
.pwid
.type
;
896 map
->fec
.pwid
.group_id
= 0;
897 map
->flags
|= F_MAP_PW_ID
;
898 map
->fec
.pwid
.pwid
= fec
->u
.pwid
.pwid
;
904 lde_map2fec(struct map
*map
, struct in_addr lsr_id
, struct fec
*fec
)
906 memset(fec
, 0, sizeof(*fec
));
909 case MAP_TYPE_PREFIX
:
910 switch (map
->fec
.prefix
.af
) {
912 fec
->type
= FEC_TYPE_IPV4
;
913 fec
->u
.ipv4
.prefix
= map
->fec
.prefix
.prefix
.v4
;
914 fec
->u
.ipv4
.prefixlen
= map
->fec
.prefix
.prefixlen
;
917 fec
->type
= FEC_TYPE_IPV6
;
918 fec
->u
.ipv6
.prefix
= map
->fec
.prefix
.prefix
.v6
;
919 fec
->u
.ipv6
.prefixlen
= map
->fec
.prefix
.prefixlen
;
922 fatalx("lde_map2fec: unknown af");
927 fec
->type
= FEC_TYPE_PWID
;
928 fec
->u
.pwid
.type
= map
->fec
.pwid
.type
;
929 fec
->u
.pwid
.pwid
= map
->fec
.pwid
.pwid
;
930 fec
->u
.pwid
.lsr_id
= lsr_id
;
936 lde_send_labelmapping(struct lde_nbr
*ln
, struct fec_node
*fn
, int single
)
938 struct lde_wdraw
*lw
;
947 * Ordered Control: do not send a labelmap msg until
948 * a labelmap message is received from downstream router
949 * and don't send labelmap back to downstream router
951 if (ldeconf
->flags
& F_LDPD_ORDERED_CONTROL
) {
952 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
953 if (fnh
->flags
& F_FEC_NH_DEFER
)
956 if (lde_address_find(ln
, fnh
->af
, &fnh
->nexthop
))
966 * We shouldn't send a new label mapping if we have a pending
967 * label release to receive. In this case, schedule to send a
968 * label mapping as soon as a label release is received.
970 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
, &fn
->fec
);
972 if (!fec_find(&ln
->sent_map_pending
, &fn
->fec
)) {
973 debug_evt("%s: FEC %s: scheduling to send label mapping later (waiting for pending label release)",
974 __func__
, log_fec(&fn
->fec
));
975 lde_map_pending_add(ln
, fn
);
981 * This function skips SL.1 - 3 and SL.9 - 14 because the label
982 * allocation is done way earlier (because of the merging nature of
986 lde_fec2map(&fn
->fec
, &map
);
987 switch (fn
->fec
.type
) {
991 if (lde_acl_check(ldeconf
->ipv4
.acl_label_advertise_to
,
992 AF_INET
, (union ldpd_addr
*)&ln
->id
, 32) != FILTER_PERMIT
)
994 if (lde_acl_check(ldeconf
->ipv4
.acl_label_advertise_for
,
995 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
996 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
1000 if (!ln
->v6_enabled
)
1002 if (lde_acl_check(ldeconf
->ipv6
.acl_label_advertise_to
,
1003 AF_INET
, (union ldpd_addr
*)&ln
->id
, 32) != FILTER_PERMIT
)
1005 if (lde_acl_check(ldeconf
->ipv6
.acl_label_advertise_for
,
1006 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
1007 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
1011 pw
= (struct l2vpn_pw
*) fn
->data
;
1012 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
1013 /* not the remote end of the pseudowire */
1016 map
.flags
|= F_MAP_PW_IFMTU
;
1017 map
.fec
.pwid
.ifmtu
= pw
->l2vpn
->mtu
;
1018 if (pw
->flags
& F_PW_CWORD
)
1019 map
.flags
|= F_MAP_PW_CWORD
;
1020 if (pw
->flags
& F_PW_STATUSTLV
) {
1021 map
.flags
|= F_MAP_PW_STATUS
;
1022 map
.pw_status
= pw
->local_status
;
1026 map
.label
= fn
->local_label
;
1028 /* SL.6: is there a pending request for this mapping? */
1029 lre
= (struct lde_req
*)fec_find(&ln
->recv_req
, &fn
->fec
);
1031 /* set label request msg id in the mapping response. */
1032 map
.requestid
= lre
->msg_id
;
1033 map
.flags
= F_MAP_REQ_ID
;
1035 /* SL.7: delete record of pending request */
1036 lde_req_del(ln
, lre
, 0);
1039 /* SL.4: send label mapping */
1040 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD
, ln
->peerid
, 0,
1043 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
, ln
->peerid
, 0,
1046 /* SL.5: record sent label mapping */
1047 me
= (struct lde_map
*)fec_find(&ln
->sent_map
, &fn
->fec
);
1049 me
= lde_map_add(ln
, fn
, 1);
1054 lde_send_labelwithdraw(struct lde_nbr
*ln
, struct fec_node
*fn
,
1055 struct map
*wcard
, struct status_tlv
*st
)
1057 struct lde_wdraw
*lw
;
1060 struct l2vpn_pw
*pw
;
1063 lde_fec2map(&fn
->fec
, &map
);
1064 switch (fn
->fec
.type
) {
1066 if (!ln
->v4_enabled
)
1070 if (!ln
->v6_enabled
)
1074 pw
= (struct l2vpn_pw
*) fn
->data
;
1075 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
1076 /* not the remote end of the pseudowire */
1079 if (pw
->flags
& F_PW_CWORD
)
1080 map
.flags
|= F_MAP_PW_CWORD
;
1083 map
.label
= fn
->local_label
;
1085 memcpy(&map
, wcard
, sizeof(map
));
1088 map
.st
.status_code
= st
->status_code
;
1089 map
.st
.msg_id
= st
->msg_id
;
1090 map
.st
.msg_type
= st
->msg_type
;
1091 map
.flags
|= F_MAP_STATUS
;
1094 /* SWd.1: send label withdraw. */
1095 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD
, ln
->peerid
, 0,
1097 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD_END
, ln
->peerid
, 0, NULL
, 0);
1099 /* SWd.2: record label withdraw. */
1101 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
, &fn
->fec
);
1103 lw
= lde_wdraw_add(ln
, fn
);
1104 lw
->label
= map
.label
;
1108 RB_FOREACH(f
, fec_tree
, &ft
) {
1109 fn
= (struct fec_node
*)f
;
1110 me
= (struct lde_map
*)fec_find(&ln
->sent_map
, &fn
->fec
);
1111 if (lde_wildcard_apply(wcard
, &fn
->fec
, me
) == 0)
1114 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
,
1117 lw
= lde_wdraw_add(ln
, fn
);
1118 lw
->label
= map
.label
;
1124 lde_send_labelwithdraw_wcard(struct lde_nbr
*ln
, uint32_t label
)
1128 memset(&wcard
, 0, sizeof(wcard
));
1129 wcard
.type
= MAP_TYPE_WILDCARD
;
1130 wcard
.label
= label
;
1131 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1135 lde_send_labelwithdraw_twcard_prefix(struct lde_nbr
*ln
, uint16_t af
,
1140 memset(&wcard
, 0, sizeof(wcard
));
1141 wcard
.type
= MAP_TYPE_TYPED_WCARD
;
1142 wcard
.fec
.twcard
.type
= MAP_TYPE_PREFIX
;
1143 wcard
.fec
.twcard
.u
.prefix_af
= af
;
1144 wcard
.label
= label
;
1145 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1149 lde_send_labelwithdraw_twcard_pwid(struct lde_nbr
*ln
, uint16_t pw_type
,
1154 memset(&wcard
, 0, sizeof(wcard
));
1155 wcard
.type
= MAP_TYPE_TYPED_WCARD
;
1156 wcard
.fec
.twcard
.type
= MAP_TYPE_PWID
;
1157 wcard
.fec
.twcard
.u
.pw_type
= pw_type
;
1158 wcard
.label
= label
;
1159 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1163 lde_send_labelwithdraw_pwid_wcard(struct lde_nbr
*ln
, uint16_t pw_type
,
1168 memset(&wcard
, 0, sizeof(wcard
));
1169 wcard
.type
= MAP_TYPE_PWID
;
1170 wcard
.fec
.pwid
.type
= pw_type
;
1171 wcard
.fec
.pwid
.group_id
= group_id
;
1172 /* we can not append a Label TLV when using PWid group wildcards. */
1173 wcard
.label
= NO_LABEL
;
1174 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1178 lde_send_labelrelease(struct lde_nbr
*ln
, struct fec_node
*fn
,
1179 struct map
*wcard
, uint32_t label
)
1182 struct l2vpn_pw
*pw
;
1185 lde_fec2map(&fn
->fec
, &map
);
1186 switch (fn
->fec
.type
) {
1188 if (!ln
->v4_enabled
)
1192 if (!ln
->v6_enabled
)
1196 pw
= (struct l2vpn_pw
*) fn
->data
;
1197 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
1198 /* not the remote end of the pseudowire */
1201 if (pw
->flags
& F_PW_CWORD
)
1202 map
.flags
|= F_MAP_PW_CWORD
;
1206 memcpy(&map
, wcard
, sizeof(map
));
1209 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD
, ln
->peerid
, 0,
1211 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD_END
, ln
->peerid
, 0, NULL
, 0);
1215 lde_send_labelrequest(struct lde_nbr
*ln
, struct fec_node
*fn
,
1216 struct map
*wcard
, int single
)
1220 struct lde_req
*lre
;
1223 lde_fec2map(&fn
->fec
, &map
);
1224 switch (fn
->fec
.type
) {
1226 if (!ln
->v4_enabled
)
1230 if (!ln
->v6_enabled
)
1234 fatalx("lde_send_labelrequest: unknown af");
1237 memcpy(&map
, wcard
, sizeof(map
));
1239 map
.label
= NO_LABEL
;
1242 /* SLR1.1: has label request for FEC been previously sent
1243 * and still outstanding just return,
1245 lre
= (struct lde_req
*)fec_find(&ln
->sent_req
, &fn
->fec
);
1247 /* SLRq.3: send label request */
1248 lde_imsg_compose_ldpe(IMSG_REQUEST_ADD
, ln
->peerid
, 0,
1251 lde_imsg_compose_ldpe(IMSG_REQUEST_ADD_END
,
1252 ln
->peerid
, 0, NULL
, 0);
1254 /* SLRq.4: record sent request */
1255 lde_req_add(ln
, &fn
->fec
, 1);
1258 /* if Wilcard just send label request */
1259 /* SLRq.3: send label request */
1260 lde_imsg_compose_ldpe(IMSG_REQUEST_ADD
,
1261 ln
->peerid
, 0, &map
, sizeof(map
));
1263 lde_imsg_compose_ldpe(IMSG_REQUEST_ADD_END
,
1264 ln
->peerid
, 0, NULL
, 0);
1266 /* SLRq.4: record sent request */
1267 RB_FOREACH(f
, fec_tree
, &ft
) {
1268 fn
= (struct fec_node
*)f
;
1269 lre
= (struct lde_req
*)fec_find(&ln
->sent_req
, &fn
->fec
);
1270 if (lde_wildcard_apply(wcard
, &fn
->fec
, NULL
) == 0)
1273 lde_req_add(ln
, f
, 1);
1279 lde_send_labelrequest_wcard(struct lde_nbr
*ln
, uint16_t af
)
1283 memset(&wcard
, 0, sizeof(wcard
));
1284 wcard
.type
= MAP_TYPE_TYPED_WCARD
;
1285 wcard
.fec
.twcard
.type
= MAP_TYPE_PREFIX
;
1286 wcard
.fec
.twcard
.u
.prefix_af
= af
;
1287 lde_send_labelrequest(ln
, NULL
, &wcard
, 1);
1291 lde_send_notification(struct lde_nbr
*ln
, uint32_t status_code
, uint32_t msg_id
,
1294 struct notify_msg nm
;
1296 memset(&nm
, 0, sizeof(nm
));
1297 nm
.status_code
= status_code
;
1298 /* 'msg_id' and 'msg_type' should be in network byte order */
1300 nm
.msg_type
= msg_type
;
1302 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1307 lde_send_notification_eol_prefix(struct lde_nbr
*ln
, int af
)
1309 struct notify_msg nm
;
1311 memset(&nm
, 0, sizeof(nm
));
1312 nm
.status_code
= S_ENDOFLIB
;
1313 nm
.fec
.type
= MAP_TYPE_TYPED_WCARD
;
1314 nm
.fec
.fec
.twcard
.type
= MAP_TYPE_PREFIX
;
1315 nm
.fec
.fec
.twcard
.u
.prefix_af
= af
;
1316 nm
.flags
|= F_NOTIF_FEC
;
1318 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1323 lde_send_notification_eol_pwid(struct lde_nbr
*ln
, uint16_t pw_type
)
1325 struct notify_msg nm
;
1327 memset(&nm
, 0, sizeof(nm
));
1328 nm
.status_code
= S_ENDOFLIB
;
1329 nm
.fec
.type
= MAP_TYPE_TYPED_WCARD
;
1330 nm
.fec
.fec
.twcard
.type
= MAP_TYPE_PWID
;
1331 nm
.fec
.fec
.twcard
.u
.pw_type
= pw_type
;
1332 nm
.flags
|= F_NOTIF_FEC
;
1334 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1339 lde_nbr_compare(const struct lde_nbr
*a
, const struct lde_nbr
*b
)
1341 return (a
->peerid
- b
->peerid
);
1344 static struct lde_nbr
*
1345 lde_nbr_new(uint32_t peerid
, struct lde_nbr
*new)
1349 if ((ln
= calloc(1, sizeof(*ln
))) == NULL
)
1353 ln
->v4_enabled
= new->v4_enabled
;
1354 ln
->v6_enabled
= new->v6_enabled
;
1355 ln
->flags
= new->flags
;
1356 ln
->peerid
= peerid
;
1357 fec_init(&ln
->recv_map
);
1358 fec_init(&ln
->sent_map
);
1359 fec_init(&ln
->sent_map_pending
);
1360 fec_init(&ln
->recv_req
);
1361 fec_init(&ln
->sent_req
);
1362 fec_init(&ln
->sent_wdraw
);
1364 TAILQ_INIT(&ln
->addr_list
);
1366 if (RB_INSERT(nbr_tree
, &lde_nbrs
, ln
) != NULL
)
1367 fatalx("lde_nbr_new: RB_INSERT failed");
1373 lde_nbr_del(struct lde_nbr
*ln
)
1376 struct fec_node
*fn
;
1378 struct l2vpn_pw
*pw
;
1379 struct lde_nbr
*lnbr
;
1384 /* uninstall received mappings */
1385 RB_FOREACH(f
, fec_tree
, &ft
) {
1386 fn
= (struct fec_node
*)f
;
1388 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
1392 if (!lde_address_find(ln
, fnh
->af
,
1397 * Ordered Control: must mark any non-connected
1398 * NH to wait until we receive a labelmap msg
1399 * before installing in kernel and sending to
1400 * peer, must do this as NHs are not removed
1401 * when lsps go down. Also send label withdraw
1402 * to other neighbors for all fecs from neighbor
1405 if (ldeconf
->flags
& F_LDPD_ORDERED_CONTROL
) {
1406 fnh
->flags
|= F_FEC_NH_DEFER
;
1408 RB_FOREACH(lnbr
, nbr_tree
, &lde_nbrs
) {
1409 if (ln
->peerid
== lnbr
->peerid
)
1411 lde_send_labelwithdraw(lnbr
, fn
, NULL
, NULL
);
1416 if (f
->u
.pwid
.lsr_id
.s_addr
!= ln
->id
.s_addr
)
1418 pw
= (struct l2vpn_pw
*) fn
->data
;
1420 pw
->reason
= F_PW_NO_REMOTE_LABEL
;
1428 lde_send_delete_klabel(fn
, fnh
);
1429 fnh
->remote_label
= NO_LABEL
;
1433 lde_address_list_free(ln
);
1435 fec_clear(&ln
->recv_map
, lde_map_free
);
1436 fec_clear(&ln
->sent_map
, lde_map_free
);
1437 fec_clear(&ln
->sent_map_pending
, free
);
1438 fec_clear(&ln
->recv_req
, free
);
1439 fec_clear(&ln
->sent_req
, free
);
1440 fec_clear(&ln
->sent_wdraw
, free
);
1442 RB_REMOVE(nbr_tree
, &lde_nbrs
, ln
);
1447 static struct lde_nbr
*
1448 lde_nbr_find(uint32_t peerid
)
1454 return (RB_FIND(nbr_tree
, &lde_nbrs
, &ln
));
1458 lde_nbr_find_by_lsrid(struct in_addr addr
)
1462 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1463 if (ln
->id
.s_addr
== addr
.s_addr
)
1470 lde_nbr_find_by_addr(int af
, union ldpd_addr
*addr
)
1474 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1475 if (lde_address_find(ln
, af
, addr
) != NULL
)
1486 while (!RB_EMPTY(nbr_tree
, &lde_nbrs
)) {
1487 ln
= RB_ROOT(nbr_tree
, &lde_nbrs
);
1494 lde_nbr_addr_update(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
, int removed
)
1497 struct fec_node
*fn
;
1501 RB_FOREACH(fec
, fec_tree
, &ln
->recv_map
) {
1502 switch (fec
->type
) {
1504 if (lde_addr
->af
!= AF_INET
)
1508 if (lde_addr
->af
!= AF_INET6
)
1515 fn
= (struct fec_node
*)fec_find(&ft
, fec
);
1517 /* shouldn't happen */
1520 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
1521 if (ldp_addrcmp(fnh
->af
, &fnh
->nexthop
,
1526 lde_send_delete_klabel(fn
, fnh
);
1527 fnh
->remote_label
= NO_LABEL
;
1529 me
= (struct lde_map
*)fec
;
1530 fnh
->remote_label
= me
->map
.label
;
1531 lde_send_change_klabel(fn
, fnh
);
1539 lde_map_compare(const struct lde_map
*a
, const struct lde_map
*b
)
1541 return (ldp_addrcmp(AF_INET
, (union ldpd_addr
*)&a
->nexthop
->id
,
1542 (union ldpd_addr
*)&b
->nexthop
->id
));
1546 lde_map_add(struct lde_nbr
*ln
, struct fec_node
*fn
, int sent
)
1550 me
= calloc(1, sizeof(*me
));
1558 RB_INSERT(lde_map_head
, &fn
->upstream
, me
);
1559 me
->head
= &fn
->upstream
;
1560 if (fec_insert(&ln
->sent_map
, &me
->fec
))
1561 log_warnx("failed to add %s to sent map",
1563 /* XXX on failure more cleanup is needed */
1565 RB_INSERT(lde_map_head
, &fn
->downstream
, me
);
1566 me
->head
= &fn
->downstream
;
1567 if (fec_insert(&ln
->recv_map
, &me
->fec
))
1568 log_warnx("failed to add %s to recv map",
1576 lde_map_del(struct lde_nbr
*ln
, struct lde_map
*me
, int sent
)
1579 fec_remove(&ln
->sent_map
, &me
->fec
);
1581 fec_remove(&ln
->recv_map
, &me
->fec
);
1587 lde_map_free(void *ptr
)
1589 struct lde_map
*map
= ptr
;
1591 RB_REMOVE(lde_map_head
, map
->head
, map
);
1596 lde_map_pending_add(struct lde_nbr
*ln
, struct fec_node
*fn
)
1600 map
= calloc(1, sizeof(*map
));
1605 if (fec_insert(&ln
->sent_map_pending
, map
))
1606 log_warnx("failed to add %s to sent map (pending)",
1613 lde_map_pending_del(struct lde_nbr
*ln
, struct fec
*map
)
1615 fec_remove(&ln
->sent_map_pending
, map
);
1620 lde_req_add(struct lde_nbr
*ln
, struct fec
*fec
, int sent
)
1623 struct lde_req
*lre
;
1625 t
= sent
? &ln
->sent_req
: &ln
->recv_req
;
1627 lre
= calloc(1, sizeof(*lre
));
1631 if (fec_insert(t
, &lre
->fec
)) {
1632 log_warnx("failed to add %s to %s req",
1633 log_fec(&lre
->fec
), sent
? "sent" : "recv");
1643 lde_req_del(struct lde_nbr
*ln
, struct lde_req
*lre
, int sent
)
1646 fec_remove(&ln
->sent_req
, &lre
->fec
);
1648 fec_remove(&ln
->recv_req
, &lre
->fec
);
1654 lde_wdraw_add(struct lde_nbr
*ln
, struct fec_node
*fn
)
1656 struct lde_wdraw
*lw
;
1658 lw
= calloc(1, sizeof(*lw
));
1664 if (fec_insert(&ln
->sent_wdraw
, &lw
->fec
))
1665 log_warnx("failed to add %s to sent wdraw",
1672 lde_wdraw_del(struct lde_nbr
*ln
, struct lde_wdraw
*lw
)
1674 fec_remove(&ln
->sent_wdraw
, &lw
->fec
);
1679 lde_change_egress_label(int af
)
1683 struct fec_node
*fn
;
1685 /* explicitly withdraw all null labels */
1686 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
) {
1687 lde_send_labelwithdraw_wcard(ln
, MPLS_LABEL_IMPLICIT_NULL
);
1689 lde_send_labelwithdraw_wcard(
1691 MPLS_LABEL_IPV4_EXPLICIT_NULL
);
1693 lde_send_labelwithdraw_wcard(
1695 MPLS_LABEL_IPV6_EXPLICIT_NULL
);
1698 /* update label of connected routes */
1699 RB_FOREACH(f
, fec_tree
, &ft
) {
1700 fn
= (struct fec_node
*)f
;
1701 if (fn
->local_label
> MPLS_LABEL_RESERVED_MAX
)
1706 if (fn
->fec
.type
!= FEC_TYPE_IPV4
)
1710 if (fn
->fec
.type
!= FEC_TYPE_IPV6
)
1714 fatalx("lde_change_egress_label: unknown af");
1717 fn
->local_label
= lde_update_label(fn
);
1718 if (fn
->local_label
!= NO_LABEL
)
1719 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1720 lde_send_labelmapping(ln
, fn
, 0);
1722 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1723 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
, ln
->peerid
, 0,
1728 lde_change_allocate_filter(int af
)
1732 struct fec_node
*fn
;
1735 /* reallocate labels for fecs that match this filter */
1736 RB_FOREACH(f
, fec_tree
, &ft
) {
1737 fn
= (struct fec_node
*)f
;
1741 if (fn
->fec
.type
!= FEC_TYPE_IPV4
)
1745 if (fn
->fec
.type
!= FEC_TYPE_IPV6
)
1749 fatalx("lde_change_allocate_filter: unknown af");
1753 * If the local label has changed to NO_LABEL, send a label
1754 * withdraw to all peers.
1755 * If the local label has changed and it's different from
1756 * NO_LABEL, send a label mapping to all peers advertising
1758 * If the local label hasn't changed, do nothing
1760 new_label
= lde_update_label(fn
);
1761 if (fn
->local_label
!= new_label
) {
1762 if (new_label
== NO_LABEL
)
1763 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1764 lde_send_labelwithdraw(ln
, fn
,
1767 fn
->local_label
= new_label
;
1768 if (fn
->local_label
!= NO_LABEL
)
1769 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1770 lde_send_labelmapping(ln
, fn
, 0);
1773 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1774 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
, ln
->peerid
, 0,
1779 lde_change_advertise_filter(int af
)
1783 struct fec_node
*fn
;
1784 char *acl_to_filter
;
1785 char *acl_for_filter
;
1786 union ldpd_addr
*prefix
;
1790 /* advertise label for fecs to neighbors if matches advertise filters */
1793 acl_to_filter
= ldeconf
->ipv4
.acl_label_advertise_to
;
1794 acl_for_filter
= ldeconf
->ipv4
.acl_label_advertise_for
;
1797 acl_to_filter
= ldeconf
->ipv6
.acl_label_advertise_to
;
1798 acl_for_filter
= ldeconf
->ipv6
.acl_label_advertise_for
;
1801 fatalx("lde_change_advertise_filter: unknown af");
1804 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
) {
1805 if (lde_acl_check(acl_to_filter
, af
, (union ldpd_addr
*)&ln
->id
,
1806 IPV4_MAX_BITLEN
) != FILTER_PERMIT
)
1807 lde_send_labelwithdraw_wcard(ln
, NO_LABEL
);
1809 /* This neighbor is allowed in to_filter, so
1810 * send labels if fec also matches for_filter
1812 RB_FOREACH(f
, fec_tree
, &ft
) {
1813 fn
= (struct fec_node
*)f
;
1816 if (fn
->fec
.type
!= FEC_TYPE_IPV4
)
1818 prefix
= (union ldpd_addr
*)
1819 &fn
->fec
.u
.ipv4
.prefix
;
1820 plen
= fn
->fec
.u
.ipv4
.prefixlen
;
1823 if (fn
->fec
.type
!= FEC_TYPE_IPV6
)
1825 prefix
= (union ldpd_addr
*)
1826 &fn
->fec
.u
.ipv6
.prefix
;
1827 plen
= fn
->fec
.u
.ipv6
.prefixlen
;
1832 if (lde_acl_check(acl_for_filter
, af
,
1833 prefix
, plen
) != FILTER_PERMIT
) {
1834 me
= (struct lde_map
*)fec_find(
1835 &ln
->sent_map
, &fn
->fec
);
1837 /* fec filtered withdraw */
1838 lde_send_labelwithdraw(ln
, fn
,
1841 /* fec allowed send map */
1842 lde_send_labelmapping(ln
, fn
, 0);
1844 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
,
1845 ln
->peerid
, 0, NULL
, 0);
1852 lde_change_accept_filter(int af
)
1856 struct fec_node
*fn
;
1857 char *acl_for_filter
;
1858 char *acl_from_filter
;
1859 union ldpd_addr
*prefix
;
1864 /* accept labels from neighbors specified in the from_filter and for
1865 * fecs defined in the for_filter
1869 acl_for_filter
= ldeconf
->ipv4
.acl_label_accept_for
;
1870 acl_from_filter
= ldeconf
->ipv4
.acl_label_accept_from
;
1871 type
= FEC_TYPE_IPV4
;
1874 acl_for_filter
= ldeconf
->ipv6
.acl_label_accept_for
;
1875 acl_from_filter
= ldeconf
->ipv6
.acl_label_accept_from
;
1876 type
= FEC_TYPE_IPV6
;
1879 fatalx("lde_change_accept_filter: unknown af");
1882 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
) {
1883 if (lde_acl_check(acl_from_filter
, AF_INET
, (union ldpd_addr
*)
1884 &ln
->id
, IPV4_MAX_BITLEN
) != FILTER_PERMIT
) {
1885 /* This neighbor is now filtered so remove fecs from
1888 RB_FOREACH(f
, fec_tree
, &ft
) {
1889 fn
= (struct fec_node
*)f
;
1890 if (fn
->fec
.type
== type
) {
1891 me
= (struct lde_map
*)fec_find(
1892 &ln
->recv_map
, &fn
->fec
);
1894 lde_map_del(ln
, me
, 0);
1897 } else if (ln
->flags
& F_NBR_CAP_TWCARD
) {
1898 /* This neighbor is allowed and supports type
1899 * wildcard so send a labelrequest
1900 * to get any new labels from neighbor
1901 * and make sure any fecs we currently have
1904 RB_FOREACH(f
, fec_tree
, &ft
) {
1905 fn
= (struct fec_node
*)f
;
1908 if (fn
->fec
.type
!= FEC_TYPE_IPV4
)
1910 prefix
= (union ldpd_addr
*)
1911 &fn
->fec
.u
.ipv4
.prefix
;
1912 plen
= fn
->fec
.u
.ipv4
.prefixlen
;
1915 if (fn
->fec
.type
!= FEC_TYPE_IPV6
)
1917 prefix
= (union ldpd_addr
*)
1918 &fn
->fec
.u
.ipv6
.prefix
;
1919 plen
= fn
->fec
.u
.ipv6
.prefixlen
;
1924 if (lde_acl_check(acl_for_filter
, af
,
1925 prefix
, plen
) != FILTER_PERMIT
) {
1926 me
= (struct lde_map
*)fec_find(
1927 &ln
->recv_map
, &fn
->fec
);
1929 lde_map_del(ln
, me
, 0);
1932 lde_send_labelrequest_wcard(ln
, af
);
1934 /* Type Wildcard is not supported so restart session */
1935 lde_imsg_compose_ldpe(IMSG_NBR_SHUTDOWN
, ln
->peerid
, 0,
1941 lde_change_expnull_for_filter(int af
)
1945 struct fec_node
*fn
;
1948 union ldpd_addr
*prefix
;
1951 /* Configure explicit-null advertisement for all fecs in this filter */
1952 RB_FOREACH(f
, fec_tree
, &ft
) {
1953 fn
= (struct fec_node
*)f
;
1957 if (fn
->fec
.type
!= FEC_TYPE_IPV4
)
1959 acl_name
= ldeconf
->ipv4
.acl_label_expnull_for
;
1960 prefix
= (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
;
1961 plen
= fn
->fec
.u
.ipv4
.prefixlen
;
1962 exp_label
= MPLS_LABEL_IPV4_EXPLICIT_NULL
;
1965 if (fn
->fec
.type
!= FEC_TYPE_IPV6
)
1967 acl_name
= ldeconf
->ipv6
.acl_label_expnull_for
;
1968 prefix
= (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
;
1969 plen
= fn
->fec
.u
.ipv6
.prefixlen
;
1970 exp_label
= MPLS_LABEL_IPV6_EXPLICIT_NULL
;
1973 fatalx("lde_change_expnull_for_filter: unknown af");
1976 if (lde_acl_check(acl_name
, af
, prefix
, plen
) == FILTER_PERMIT
) {
1977 /* for this fec change any imp-null to exp-null */
1978 if (fn
->local_label
== MPLS_LABEL_IMPLICIT_NULL
) {
1979 fn
->local_label
= lde_update_label(fn
);
1980 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1981 lde_send_labelmapping(ln
, fn
, 0);
1984 /* for this fec change any exp-null back to imp-null */
1985 if (fn
->local_label
== exp_label
) {
1986 fn
->local_label
= lde_update_label(fn
);
1987 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1988 lde_send_labelmapping(ln
, fn
, 0);
1992 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1993 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
, ln
->peerid
, 0,
1998 lde_address_add(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
)
2000 struct lde_addr
*new;
2002 if (lde_address_find(ln
, lde_addr
->af
, &lde_addr
->addr
) != NULL
)
2005 if ((new = calloc(1, sizeof(*new))) == NULL
)
2008 new->af
= lde_addr
->af
;
2009 new->addr
= lde_addr
->addr
;
2010 TAILQ_INSERT_TAIL(&ln
->addr_list
, new, entry
);
2012 /* reevaluate the previously received mappings from this neighbor */
2013 lde_nbr_addr_update(ln
, lde_addr
, 0);
2019 lde_address_del(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
)
2021 lde_addr
= lde_address_find(ln
, lde_addr
->af
, &lde_addr
->addr
);
2022 if (lde_addr
== NULL
)
2025 /* reevaluate the previously received mappings from this neighbor */
2026 lde_nbr_addr_update(ln
, lde_addr
, 1);
2028 TAILQ_REMOVE(&ln
->addr_list
, lde_addr
, entry
);
2035 lde_address_find(struct lde_nbr
*ln
, int af
, union ldpd_addr
*addr
)
2037 struct lde_addr
*lde_addr
;
2039 TAILQ_FOREACH(lde_addr
, &ln
->addr_list
, entry
)
2040 if (lde_addr
->af
== af
&&
2041 ldp_addrcmp(af
, &lde_addr
->addr
, addr
) == 0)
2048 lde_address_list_free(struct lde_nbr
*ln
)
2050 struct lde_addr
*lde_addr
;
2052 while ((lde_addr
= TAILQ_POP_FIRST(&ln
->addr_list
, entry
)) != NULL
)
2057 * Event callback used to retry the label-manager sync zapi session.
2059 static int zclient_sync_retry(struct thread
*thread
)
2061 zclient_sync_init();
2067 * Initialize and open a synchronous zapi session. This is used by label chunk
2068 * management code, which acquires and releases blocks of labels from the
2069 * zebra label-manager module.
2071 static void zclient_sync_init(void)
2073 struct zclient_options options
= zclient_options_default
;
2075 options
.synchronous
= true;
2077 /* Initialize special zclient for synchronous message exchanges. */
2078 zclient_sync
= zclient_new(master
, &options
);
2079 zclient_sync
->sock
= -1;
2080 zclient_sync
->redist_default
= ZEBRA_ROUTE_LDP
;
2081 zclient_sync
->session_id
= 1; /* Distinguish from main session */
2082 zclient_sync
->privs
= &lde_privs
;
2084 if (zclient_socket_connect(zclient_sync
) < 0) {
2085 log_warnx("Error connecting synchronous zclient!");
2088 /* make socket non-blocking */
2089 sock_set_nonblock(zclient_sync
->sock
);
2091 /* Send hello to notify zebra this is a synchronous client */
2092 if (zclient_send_hello(zclient_sync
) < 0) {
2093 log_warnx("Error sending hello for synchronous zclient!");
2097 /* Connect to label manager */
2098 if (lm_label_manager_connect(zclient_sync
, 0) != 0) {
2099 log_warnx("Error connecting to label manager!");
2103 /* Finish label-manager init once the LM session is running */
2104 lde_label_list_init();
2110 /* Discard failed zclient object */
2111 zclient_stop(zclient_sync
);
2112 zclient_free(zclient_sync
);
2113 zclient_sync
= NULL
;
2115 /* Retry using a timer */
2116 thread_add_timer(master
, zclient_sync_retry
, NULL
, 1, NULL
);
2120 lde_del_label_chunk(void *val
)
2126 lde_release_label_chunk(uint32_t start
, uint32_t end
)
2130 ret
= lm_release_label_chunk(zclient_sync
, start
, end
);
2132 log_warnx("Error releasing label chunk!");
2139 lde_get_label_chunk(void)
2142 uint32_t start
, end
;
2144 debug_labels("getting label chunk (size %u)", CHUNK_SIZE
);
2145 ret
= lm_get_label_chunk(zclient_sync
, 0, MPLS_LABEL_BASE_ANY
,
2146 CHUNK_SIZE
, &start
, &end
);
2148 log_warnx("Error getting label chunk!");
2152 on_get_label_chunk_response(start
, end
);
2158 lde_label_list_init(void)
2160 label_chunk_list
= list_new();
2161 label_chunk_list
->del
= lde_del_label_chunk
;
2163 /* get first chunk */
2164 while (lde_get_label_chunk () != 0) {
2165 log_warnx("Error getting first label chunk!");
2171 on_get_label_chunk_response(uint32_t start
, uint32_t end
)
2173 struct label_chunk
*new_label_chunk
;
2175 debug_labels("label chunk assign: %u - %u", start
, end
);
2177 new_label_chunk
= calloc(1, sizeof(struct label_chunk
));
2178 if (!new_label_chunk
) {
2179 log_warn("Error trying to allocate label chunk %u - %u", start
, end
);
2183 new_label_chunk
->start
= start
;
2184 new_label_chunk
->end
= end
;
2185 new_label_chunk
->used_mask
= 0;
2187 listnode_add(label_chunk_list
, (void *)new_label_chunk
);
2189 /* let's update current if needed */
2190 if (!current_label_chunk
)
2191 current_label_chunk
= listtail(label_chunk_list
);
2195 lde_free_label(uint32_t label
)
2197 struct listnode
*node
;
2198 struct label_chunk
*label_chunk
;
2201 for (ALL_LIST_ELEMENTS_RO(label_chunk_list
, node
, label_chunk
)) {
2202 if (label
<= label_chunk
->end
&& label
>= label_chunk
->start
) {
2203 pos
= 1ULL << (label
- label_chunk
->start
);
2204 label_chunk
->used_mask
&= ~pos
;
2205 /* if nobody is using this chunk and it's not current_label_chunk, then free it */
2206 if (!label_chunk
->used_mask
&& (current_label_chunk
!= node
)) {
2207 if (lde_release_label_chunk(label_chunk
->start
, label_chunk
->end
) != 0)
2208 log_warnx("%s: Error releasing label chunk!", __func__
);
2210 listnode_delete(label_chunk_list
, label_chunk
);
2211 lde_del_label_chunk(label_chunk
);
2221 lde_get_next_label(void)
2223 struct label_chunk
*label_chunk
;
2226 uint32_t label
= NO_LABEL
;
2228 while (current_label_chunk
) {
2229 label_chunk
= listgetdata(current_label_chunk
);
2233 /* try to get next free label in currently used label chunk */
2234 size
= label_chunk
->end
- label_chunk
->start
+ 1;
2235 for (i
= 0, pos
= 1; i
< size
; i
++, pos
<<= 1) {
2236 if (!(pos
& label_chunk
->used_mask
)) {
2237 label_chunk
->used_mask
|= pos
;
2238 label
= label_chunk
->start
+ i
;
2242 current_label_chunk
= listnextnode(current_label_chunk
);
2246 /* we moved till the last chunk, or were not able to find a label,
2247 so let's ask for another one */
2248 if (!current_label_chunk
||
2249 current_label_chunk
== listtail(label_chunk_list
) ||
2250 label
== NO_LABEL
) {
2251 if (lde_get_label_chunk() != 0)
2252 log_warn("%s: Error getting label chunk!", __func__
);
2260 lde_check_filter_af(int af
, struct ldpd_af_conf
*af_conf
,
2261 const char *filter_name
)
2263 if (strcmp(af_conf
->acl_label_allocate_for
, filter_name
) == 0)
2264 lde_change_allocate_filter(af
);
2265 if ((strcmp(af_conf
->acl_label_advertise_to
, filter_name
) == 0)
2266 || (strcmp(af_conf
->acl_label_advertise_for
, filter_name
) == 0))
2267 lde_change_advertise_filter(af
);
2268 if ((strcmp(af_conf
->acl_label_accept_for
, filter_name
) == 0)
2269 || (strcmp(af_conf
->acl_label_accept_from
, filter_name
) == 0))
2270 lde_change_accept_filter(af
);
2271 if (strcmp(af_conf
->acl_label_expnull_for
, filter_name
) == 0)
2272 lde_change_expnull_for_filter(af
);