]>
git.proxmox.com Git - mirror_frr.git/blob - ldpd/lde_lib.c
4 * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
5 * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 static __inline
int fec_compare(const struct fec
*, const struct fec
*);
30 static int lde_nbr_is_nexthop(struct fec_node
*,
32 static void fec_free(void *);
33 static struct fec_node
*fec_add(struct fec
*fec
);
34 static struct fec_nh
*fec_nh_add(struct fec_node
*, int, union ldpd_addr
*,
35 ifindex_t
, uint8_t, unsigned short);
36 static void fec_nh_del(struct fec_nh
*);
38 RB_GENERATE(fec_tree
, fec
, entry
, fec_compare
)
40 struct fec_tree ft
= RB_INITIALIZER(&ft
);
41 struct thread
*gc_timer
;
43 /* FEC tree functions */
45 fec_init(struct fec_tree
*fh
)
47 RB_INIT(fec_tree
, fh
);
51 fec_compare(const struct fec
*a
, const struct fec
*b
)
53 if (a
->type
< b
->type
)
55 if (a
->type
> b
->type
)
60 if (ntohl(a
->u
.ipv4
.prefix
.s_addr
) <
61 ntohl(b
->u
.ipv4
.prefix
.s_addr
))
63 if (ntohl(a
->u
.ipv4
.prefix
.s_addr
) >
64 ntohl(b
->u
.ipv4
.prefix
.s_addr
))
66 if (a
->u
.ipv4
.prefixlen
< b
->u
.ipv4
.prefixlen
)
68 if (a
->u
.ipv4
.prefixlen
> b
->u
.ipv4
.prefixlen
)
72 if (memcmp(&a
->u
.ipv6
.prefix
, &b
->u
.ipv6
.prefix
,
73 sizeof(struct in6_addr
)) < 0)
75 if (memcmp(&a
->u
.ipv6
.prefix
, &b
->u
.ipv6
.prefix
,
76 sizeof(struct in6_addr
)) > 0)
78 if (a
->u
.ipv6
.prefixlen
< b
->u
.ipv6
.prefixlen
)
80 if (a
->u
.ipv6
.prefixlen
> b
->u
.ipv6
.prefixlen
)
84 if (a
->u
.pwid
.type
< b
->u
.pwid
.type
)
86 if (a
->u
.pwid
.type
> b
->u
.pwid
.type
)
88 if (a
->u
.pwid
.pwid
< b
->u
.pwid
.pwid
)
90 if (a
->u
.pwid
.pwid
> b
->u
.pwid
.pwid
)
92 if (ntohl(a
->u
.pwid
.lsr_id
.s_addr
) <
93 ntohl(b
->u
.pwid
.lsr_id
.s_addr
))
95 if (ntohl(a
->u
.pwid
.lsr_id
.s_addr
) >
96 ntohl(b
->u
.pwid
.lsr_id
.s_addr
))
105 fec_find(struct fec_tree
*fh
, struct fec
*f
)
107 return (RB_FIND(fec_tree
, fh
, f
));
111 fec_insert(struct fec_tree
*fh
, struct fec
*f
)
113 if (RB_INSERT(fec_tree
, fh
, f
) != NULL
)
119 fec_remove(struct fec_tree
*fh
, struct fec
*f
)
121 if (RB_REMOVE(fec_tree
, fh
, f
) == NULL
) {
122 log_warnx("%s failed for %s", __func__
, log_fec(f
));
129 fec_clear(struct fec_tree
*fh
, void (*free_cb
)(void *))
133 while (!RB_EMPTY(fec_tree
, fh
)) {
134 f
= RB_ROOT(fec_tree
, fh
);
141 /* routing table functions */
143 lde_nbr_is_nexthop(struct fec_node
*fn
, struct lde_nbr
*ln
)
147 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
)
148 if (lde_address_find(ln
, fnh
->af
, &fnh
->nexthop
))
160 static struct ctl_rt rtctl
;
162 RB_FOREACH(f
, fec_tree
, &ft
) {
163 fn
= (struct fec_node
*)f
;
164 if (fn
->local_label
== NO_LABEL
&&
165 RB_EMPTY(lde_map_head
, &fn
->downstream
))
168 memset(&rtctl
, 0, sizeof(rtctl
));
169 switch (fn
->fec
.type
) {
172 rtctl
.prefix
.v4
= fn
->fec
.u
.ipv4
.prefix
;
173 rtctl
.prefixlen
= fn
->fec
.u
.ipv4
.prefixlen
;
177 rtctl
.prefix
.v6
= fn
->fec
.u
.ipv6
.prefix
;
178 rtctl
.prefixlen
= fn
->fec
.u
.ipv6
.prefixlen
;
184 rtctl
.local_label
= fn
->local_label
;
185 if (RB_EMPTY(lde_map_head
, &fn
->downstream
)) {
187 rtctl
.nexthop
.s_addr
= INADDR_ANY
;
188 rtctl
.remote_label
= NO_LABEL
;
189 rtctl
.no_downstream
= 1;
191 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_LIB_BEGIN
, 0, pid
, &rtctl
,
194 RB_FOREACH(me
, lde_map_head
, &fn
->upstream
) {
195 rtctl
.nexthop
= me
->nexthop
->id
;
196 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_LIB_SENT
, 0, pid
,
197 &rtctl
, sizeof(rtctl
));
200 RB_FOREACH(me
, lde_map_head
, &fn
->downstream
) {
201 rtctl
.in_use
= lde_nbr_is_nexthop(fn
, me
->nexthop
);
202 rtctl
.nexthop
= me
->nexthop
->id
;
203 rtctl
.remote_label
= me
->map
.label
;
204 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_LIB_RCVD
, 0, pid
,
205 &rtctl
, sizeof(rtctl
));
207 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_LIB_END
, 0, pid
, &rtctl
,
213 fec_snap(struct lde_nbr
*ln
)
218 RB_FOREACH(f
, fec_tree
, &ft
) {
219 fn
= (struct fec_node
*)f
;
220 if (fn
->local_label
== NO_LABEL
)
223 lde_send_labelmapping(ln
, fn
, 0);
226 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
, ln
->peerid
, 0, NULL
, 0);
232 struct fec_node
*fn
= arg
;
235 while ((fnh
= LIST_FIRST(&fn
->nexthops
))) {
237 assert(fnh
!= LIST_FIRST(&fn
->nexthops
));
239 if (!RB_EMPTY(lde_map_head
, &fn
->downstream
))
240 log_warnx("%s: fec %s downstream list not empty", __func__
,
242 if (!RB_EMPTY(lde_map_head
, &fn
->upstream
))
243 log_warnx("%s: fec %s upstream list not empty", __func__
,
252 fec_clear(&ft
, fec_free
);
255 static struct fec_node
*
256 fec_add(struct fec
*fec
)
260 fn
= calloc(1, sizeof(*fn
));
265 fn
->local_label
= NO_LABEL
;
266 RB_INIT(lde_map_head
, &fn
->upstream
);
267 RB_INIT(lde_map_head
, &fn
->downstream
);
268 LIST_INIT(&fn
->nexthops
);
270 if (fec_insert(&ft
, &fn
->fec
))
271 log_warnx("failed to add %s to ft tree",
278 fec_nh_find(struct fec_node
*fn
, int af
, union ldpd_addr
*nexthop
,
279 ifindex_t ifindex
, uint8_t route_type
, unsigned short route_instance
)
283 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
)
285 ldp_addrcmp(af
, &fnh
->nexthop
, nexthop
) == 0 &&
286 fnh
->ifindex
== ifindex
&&
287 fnh
->route_type
== route_type
&&
288 fnh
->route_instance
== route_instance
)
294 static struct fec_nh
*
295 fec_nh_add(struct fec_node
*fn
, int af
, union ldpd_addr
*nexthop
,
296 ifindex_t ifindex
, uint8_t route_type
, unsigned short route_instance
)
300 fnh
= calloc(1, sizeof(*fnh
));
305 fnh
->nexthop
= *nexthop
;
306 fnh
->ifindex
= ifindex
;
307 fnh
->remote_label
= NO_LABEL
;
308 fnh
->route_type
= route_type
;
309 fnh
->route_instance
= route_instance
;
310 LIST_INSERT_HEAD(&fn
->nexthops
, fnh
, entry
);
316 fec_nh_del(struct fec_nh
*fnh
)
318 LIST_REMOVE(fnh
, entry
);
323 lde_kernel_insert(struct fec
*fec
, int af
, union ldpd_addr
*nexthop
,
324 ifindex_t ifindex
, uint8_t route_type
, unsigned short route_instance
,
325 int connected
, void *data
)
331 fn
= (struct fec_node
*)fec_find(&ft
, fec
);
337 fnh
= fec_nh_find(fn
, af
, nexthop
, ifindex
, route_type
, route_instance
);
339 fnh
= fec_nh_add(fn
, af
, nexthop
, ifindex
, route_type
,
342 * Ordered Control: if not a connected route and not a route
343 * learned over an interface not running LDP and not a PW
344 * then mark to wait until we receive labelmap msg before
345 * installing in kernel and sending to peer
347 iface
= if_lookup(ldeconf
, ifindex
);
348 if ((ldeconf
->flags
& F_LDPD_ORDERED_CONTROL
) &&
349 !connected
&& iface
!= NULL
&& fec
->type
!= FEC_TYPE_PWID
)
350 fnh
->flags
|= F_FEC_NH_DEFER
;
353 fnh
->flags
|= F_FEC_NH_NEW
;
355 fnh
->flags
|= F_FEC_NH_CONNECTED
;
359 lde_kernel_remove(struct fec
*fec
, int af
, union ldpd_addr
*nexthop
,
360 ifindex_t ifindex
, uint8_t route_type
, unsigned short route_instance
)
365 fn
= (struct fec_node
*)fec_find(&ft
, fec
);
369 fnh
= fec_nh_find(fn
, af
, nexthop
, ifindex
, route_type
, route_instance
);
374 lde_send_delete_klabel(fn
, fnh
);
379 * Whenever a route is changed, zebra advertises its new version without
380 * withdrawing the old one. So, after processing a ZEBRA_REDISTRIBUTE_IPV[46]_ADD
381 * message, we need to check for nexthops that were removed and, for each of
382 * them (if any), withdraw the associated labels from zebra.
385 lde_kernel_update(struct fec
*fec
)
388 struct fec_nh
*fnh
, *safe
;
393 fn
= (struct fec_node
*)fec_find(&ft
, fec
);
397 LIST_FOREACH_SAFE(fnh
, &fn
->nexthops
, entry
, safe
) {
398 if (fnh
->flags
& F_FEC_NH_NEW
) {
399 fnh
->flags
&= ~F_FEC_NH_NEW
;
401 * if LDP configured on interface or a static route
402 * clear flag else treat fec as a connected route
404 iface
= if_lookup(ldeconf
,fnh
->ifindex
);
405 if (iface
|| fnh
->route_type
== ZEBRA_ROUTE_STATIC
)
406 fnh
->flags
&=~F_FEC_NH_NO_LDP
;
408 fnh
->flags
|= F_FEC_NH_NO_LDP
;
410 lde_send_delete_klabel(fn
, fnh
);
415 if (LIST_EMPTY(&fn
->nexthops
)) {
416 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
417 lde_send_labelwithdraw(ln
, fn
, NULL
, NULL
);
421 * Do not deallocate the local label now, do that only in the
422 * LIB garbage collector. This will prevent ldpd from changing
423 * the input label of some prefixes too often when running on
424 * an unstable network. Also, restart the garbage collector
425 * timer so that labels are deallocated only when the network
428 lde_gc_start_timer();
430 fn
->local_label
= lde_update_label(fn
);
431 if (fn
->local_label
!= NO_LABEL
)
432 /* FEC.1: perform lsr label distribution procedure */
433 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
434 lde_send_labelmapping(ln
, fn
, 1);
437 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
438 lde_send_change_klabel(fn
, fnh
);
440 switch (fn
->fec
.type
) {
443 ln
= lde_nbr_find_by_addr(fnh
->af
, &fnh
->nexthop
);
446 ln
= lde_nbr_find_by_lsrid(fn
->fec
.u
.pwid
.lsr_id
);
455 me
= (struct lde_map
*)fec_find(&ln
->recv_map
, &fn
->fec
);
458 lde_check_mapping(&me
->map
, ln
);
464 lde_check_mapping(struct map
*map
, struct lde_nbr
*ln
)
472 bool send_map
= false;
474 lde_map2fec(map
, ln
->id
, &fec
);
478 if (lde_acl_check(ldeconf
->ipv4
.acl_label_accept_from
,
479 AF_INET
, (union ldpd_addr
*)&ln
->id
, 32) != FILTER_PERMIT
)
481 if (lde_acl_check(ldeconf
->ipv4
.acl_label_accept_for
,
482 AF_INET
, (union ldpd_addr
*)&fec
.u
.ipv4
.prefix
,
483 fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
487 if (lde_acl_check(ldeconf
->ipv6
.acl_label_accept_from
,
488 AF_INET
, (union ldpd_addr
*)&ln
->id
, 32) != FILTER_PERMIT
)
490 if (lde_acl_check(ldeconf
->ipv6
.acl_label_accept_for
,
491 AF_INET6
, (union ldpd_addr
*)&fec
.u
.ipv6
.prefix
,
492 fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
499 fn
= (struct fec_node
*)fec_find(&ft
, &fec
);
503 /* LMp.1: first check if we have a pending request running */
504 lre
= (struct lde_req
*)fec_find(&ln
->sent_req
, &fn
->fec
);
506 /* LMp.2: delete record of outstanding label request */
507 lde_req_del(ln
, lre
, 1);
509 /* RFC 4447 control word and status tlv negotiation */
510 if (map
->type
== MAP_TYPE_PWID
&& l2vpn_pw_negotiate(ln
, fn
, map
))
514 * LMp.3 - LMp.8: loop detection - unnecessary for frame-mode
519 me
= (struct lde_map
*)fec_find(&ln
->recv_map
, &fn
->fec
);
522 if (me
->map
.label
!= map
->label
&& lre
== NULL
) {
524 lde_send_labelrelease(ln
, fn
, NULL
, me
->map
.label
);
527 * Can not use lde_nbr_find_by_addr() because there's
528 * the possibility of multipath.
530 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
531 if (lde_address_find(ln
, fnh
->af
,
532 &fnh
->nexthop
) == NULL
)
535 lde_send_delete_klabel(fn
, fnh
);
536 fnh
->remote_label
= NO_LABEL
;
542 * LMp.11 - 12: consider multiple nexthops in order to
545 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
546 /* LMp.15: install FEC in FIB */
550 if (!lde_address_find(ln
, fnh
->af
, &fnh
->nexthop
))
554 * Ordered Control: labelmap msg received from
555 * NH so clear flag and send labelmap msg to
558 if (ldeconf
->flags
& F_LDPD_ORDERED_CONTROL
) {
560 fnh
->flags
&= ~F_FEC_NH_DEFER
;
562 fnh
->remote_label
= map
->label
;
563 lde_send_change_klabel(fn
, fnh
);
566 pw
= (struct l2vpn_pw
*) fn
->data
;
570 pw
->remote_group
= map
->fec
.pwid
.group_id
;
571 if (map
->flags
& F_MAP_PW_IFMTU
)
572 pw
->remote_mtu
= map
->fec
.pwid
.ifmtu
;
573 if (map
->flags
& F_MAP_PW_STATUS
)
574 pw
->remote_status
= map
->pw_status
;
576 pw
->remote_status
= PW_FORWARDING
;
577 fnh
->remote_label
= map
->label
;
578 if (l2vpn_pw_ok(pw
, fnh
))
579 lde_send_change_klabel(fn
, fnh
);
585 /* LMp.13 & LMp.16: Record the mapping from this peer */
587 me
= lde_map_add(ln
, fn
, 0);
591 * LMp.17 - LMp.27 are unnecessary since we don't need to implement
592 * loop detection. LMp.28 - LMp.30 are unnecessary because we are
597 * Ordered Control: just received a labelmap for this fec from NH so
598 * need to send labelmap to all peers
599 * LMp.20 - LMp21 Execute procedure to send Label Mapping
601 if (send_map
&& fn
->local_label
!= NO_LABEL
)
602 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
603 lde_send_labelmapping(ln
, fn
, 1);
607 lde_check_request(struct map
*map
, struct lde_nbr
*ln
)
614 /* wildcard label request */
615 if (map
->type
== MAP_TYPE_TYPED_WCARD
) {
616 lde_check_request_wcard(map
, ln
);
620 /* LRq.1: skip loop detection (not necessary) */
622 /* LRq.2: is there a next hop for fec? */
623 lde_map2fec(map
, ln
->id
, &fec
);
624 fn
= (struct fec_node
*)fec_find(&ft
, &fec
);
625 if (fn
== NULL
|| LIST_EMPTY(&fn
->nexthops
)) {
626 /* LRq.5: send No Route notification */
627 lde_send_notification(ln
, S_NO_ROUTE
, map
->msg_id
,
628 htons(MSG_TYPE_LABELREQUEST
));
632 /* LRq.3: is MsgSource the next hop? */
633 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
637 if (!lde_address_find(ln
, fnh
->af
, &fnh
->nexthop
))
640 /* LRq.4: send Loop Detected notification */
641 lde_send_notification(ln
, S_LOOP_DETECTED
, map
->msg_id
,
642 htons(MSG_TYPE_LABELREQUEST
));
649 /* LRq.6: first check if we have a pending request running */
650 lre
= (struct lde_req
*)fec_find(&ln
->recv_req
, &fn
->fec
);
652 /* LRq.7: duplicate request */
655 /* LRq.8: record label request */
656 lre
= lde_req_add(ln
, &fn
->fec
, 0);
658 lre
->msg_id
= ntohl(map
->msg_id
);
660 /* LRq.9: perform LSR label distribution */
661 lde_send_labelmapping(ln
, fn
, 1);
664 * LRq.10: do nothing (Request Never) since we use liberal
666 * LRq.11 - 12 are unnecessary since we are merging capable.
671 lde_check_request_wcard(struct map
*map
, struct lde_nbr
*ln
)
677 RB_FOREACH(f
, fec_tree
, &ft
) {
678 fn
= (struct fec_node
*)f
;
680 /* only a typed wildcard is possible here */
681 if (lde_wildcard_apply(map
, &fn
->fec
, NULL
) == 0)
684 /* LRq.2: is there a next hop for fec? */
685 if (LIST_EMPTY(&fn
->nexthops
))
688 /* LRq.6: first check if we have a pending request running */
689 lre
= (struct lde_req
*)fec_find(&ln
->recv_req
, &fn
->fec
);
691 /* LRq.7: duplicate request */
694 /* LRq.8: record label request */
695 lre
= lde_req_add(ln
, &fn
->fec
, 0);
697 lre
->msg_id
= ntohl(map
->msg_id
);
699 /* LRq.9: perform LSR label distribution */
700 lde_send_labelmapping(ln
, fn
, 1);
705 lde_check_release(struct map
*map
, struct lde_nbr
*ln
)
709 struct lde_wdraw
*lw
;
711 struct fec
*pending_map
;
713 /* wildcard label release */
714 if (map
->type
== MAP_TYPE_WILDCARD
||
715 map
->type
== MAP_TYPE_TYPED_WCARD
||
716 (map
->type
== MAP_TYPE_PWID
&& !(map
->flags
& F_MAP_PW_ID
))) {
717 lde_check_release_wcard(map
, ln
);
721 lde_map2fec(map
, ln
->id
, &fec
);
722 fn
= (struct fec_node
*)fec_find(&ft
, &fec
);
723 /* LRl.1: does FEC match a known FEC? */
727 /* LRl.6: check sent map list and remove it if available */
728 me
= (struct lde_map
*)fec_find(&ln
->sent_map
, &fn
->fec
);
729 if (me
&& (map
->label
== NO_LABEL
|| map
->label
== me
->map
.label
))
730 lde_map_del(ln
, me
, 1);
732 /* LRl.3: first check if we have a pending withdraw running */
733 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
, &fn
->fec
);
734 if (lw
&& (map
->label
== NO_LABEL
|| map
->label
== lw
->label
)) {
735 /* LRl.4: delete record of outstanding label withdraw */
736 lde_wdraw_del(ln
, lw
);
738 /* send pending label mapping if any */
739 pending_map
= fec_find(&ln
->sent_map_pending
, &fn
->fec
);
741 lde_send_labelmapping(ln
, fn
, 1);
742 lde_map_pending_del(ln
, pending_map
);
747 * LRl.11 - 13 are unnecessary since we remove the label from
748 * forwarding/switching as soon as the FEC is unreachable.
753 lde_check_release_wcard(struct map
*map
, struct lde_nbr
*ln
)
757 struct lde_wdraw
*lw
;
759 struct fec
*pending_map
;
761 RB_FOREACH(f
, fec_tree
, &ft
) {
762 fn
= (struct fec_node
*)f
;
763 me
= (struct lde_map
*)fec_find(&ln
->sent_map
, &fn
->fec
);
765 /* LRl.1: does FEC match a known FEC? */
766 if (lde_wildcard_apply(map
, &fn
->fec
, me
) == 0)
769 /* LRl.6: check sent map list and remove it if available */
771 (map
->label
== NO_LABEL
|| map
->label
== me
->map
.label
))
772 lde_map_del(ln
, me
, 1);
774 /* LRl.3: first check if we have a pending withdraw running */
775 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
, &fn
->fec
);
776 if (lw
&& (map
->label
== NO_LABEL
|| map
->label
== lw
->label
)) {
777 /* LRl.4: delete record of outstanding lbl withdraw */
778 lde_wdraw_del(ln
, lw
);
780 /* send pending label mapping if any */
781 pending_map
= fec_find(&ln
->sent_map_pending
, &fn
->fec
);
783 lde_send_labelmapping(ln
, fn
, 1);
784 lde_map_pending_del(ln
, pending_map
);
789 * LRl.11 - 13 are unnecessary since we remove the label from
790 * forwarding/switching as soon as the FEC is unreachable.
796 lde_check_withdraw(struct map
*map
, struct lde_nbr
*ln
)
803 struct lde_nbr
*lnbr
;
805 /* wildcard label withdraw */
806 if (map
->type
== MAP_TYPE_WILDCARD
||
807 map
->type
== MAP_TYPE_TYPED_WCARD
||
808 (map
->type
== MAP_TYPE_PWID
&& !(map
->flags
& F_MAP_PW_ID
))) {
809 lde_check_withdraw_wcard(map
, ln
);
813 lde_map2fec(map
, ln
->id
, &fec
);
814 fn
= (struct fec_node
*)fec_find(&ft
, &fec
);
818 /* LWd.1: remove label from forwarding/switching use */
819 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
823 if (!lde_address_find(ln
, fnh
->af
, &fnh
->nexthop
))
827 pw
= (struct l2vpn_pw
*) fn
->data
;
830 pw
->remote_status
= PW_NOT_FORWARDING
;
835 if (map
->label
!= NO_LABEL
&& map
->label
!= fnh
->remote_label
)
838 lde_send_delete_klabel(fn
, fnh
);
839 fnh
->remote_label
= NO_LABEL
;
842 /* LWd.2: send label release */
843 lde_send_labelrelease(ln
, fn
, NULL
, map
->label
);
845 /* LWd.3: check previously received label mapping */
846 me
= (struct lde_map
*)fec_find(&ln
->recv_map
, &fn
->fec
);
847 if (me
&& (map
->label
== NO_LABEL
|| map
->label
== me
->map
.label
))
848 /* LWd.4: remove record of previously received lbl mapping */
849 lde_map_del(ln
, me
, 0);
851 /* Ordered Control: additional withdraw steps */
852 if (ldeconf
->flags
& F_LDPD_ORDERED_CONTROL
) {
853 /* LWd.8: for each neighbor other that src of withdraw msg */
854 RB_FOREACH(lnbr
, nbr_tree
, &lde_nbrs
) {
855 if (ln
->peerid
== lnbr
->peerid
)
858 /* LWd.9: check if previously sent a label mapping */
859 me
= (struct lde_map
*)fec_find(&lnbr
->sent_map
,
862 * LWd.10: does label sent to peer "map" to withdraw
866 /* LWd.11: send label withdraw */
867 lde_send_labelwithdraw(lnbr
, fn
, NULL
, NULL
);
873 lde_check_withdraw_wcard(struct map
*map
, struct lde_nbr
*ln
)
880 struct lde_nbr
*lnbr
;
882 /* LWd.2: send label release */
883 lde_send_labelrelease(ln
, NULL
, map
, map
->label
);
885 RB_FOREACH(f
, fec_tree
, &ft
) {
886 fn
= (struct fec_node
*)f
;
887 me
= (struct lde_map
*)fec_find(&ln
->recv_map
, &fn
->fec
);
889 if (lde_wildcard_apply(map
, &fn
->fec
, me
) == 0)
892 /* LWd.1: remove label from forwarding/switching use */
893 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
897 if (!lde_address_find(ln
, fnh
->af
,
902 if (f
->u
.pwid
.lsr_id
.s_addr
!= ln
->id
.s_addr
)
904 pw
= (struct l2vpn_pw
*) fn
->data
;
906 pw
->remote_status
= PW_NOT_FORWARDING
;
911 if (map
->label
!= NO_LABEL
&& map
->label
!=
915 lde_send_delete_klabel(fn
, fnh
);
916 fnh
->remote_label
= NO_LABEL
;
919 /* LWd.3: check previously received label mapping */
920 if (me
&& (map
->label
== NO_LABEL
||
921 map
->label
== me
->map
.label
))
923 * LWd.4: remove record of previously received
926 lde_map_del(ln
, me
, 0);
928 /* Ordered Control: additional withdraw steps */
929 if (ldeconf
->flags
& F_LDPD_ORDERED_CONTROL
) {
930 /* LWd.8: for each neighbor other that src of withdraw msg */
931 RB_FOREACH(lnbr
, nbr_tree
, &lde_nbrs
) {
932 if (ln
->peerid
== lnbr
->peerid
)
935 /* LWd.9: check if previously sent a label mapping */
936 me
= (struct lde_map
*)fec_find(&lnbr
->sent_map
,
939 * LWd.10: does label sent to peer "map" to withdraw
943 /* LWd.11: send label withdraw */
944 lde_send_labelwithdraw(lnbr
, fn
, NULL
, NULL
);
951 lde_wildcard_apply(struct map
*wcard
, struct fec
*fec
, struct lde_map
*me
)
953 switch (wcard
->type
) {
954 case MAP_TYPE_WILDCARD
:
957 case MAP_TYPE_TYPED_WCARD
:
958 switch (wcard
->fec
.twcard
.type
) {
959 case MAP_TYPE_PREFIX
:
960 if (wcard
->fec
.twcard
.u
.prefix_af
== AF_INET
&&
961 fec
->type
!= FEC_TYPE_IPV4
)
963 if (wcard
->fec
.twcard
.u
.prefix_af
== AF_INET6
&&
964 fec
->type
!= FEC_TYPE_IPV6
)
968 if (fec
->type
!= FEC_TYPE_PWID
)
970 if (wcard
->fec
.twcard
.u
.pw_type
!= PW_TYPE_WILDCARD
&&
971 wcard
->fec
.twcard
.u
.pw_type
!= fec
->u
.pwid
.type
)
975 fatalx("lde_wildcard_apply: unexpected fec type");
979 /* RFC4447 pw-id group wildcard */
980 if (fec
->type
!= FEC_TYPE_PWID
)
982 if (fec
->u
.pwid
.type
!= wcard
->fec
.pwid
.type
)
984 if (me
== NULL
|| (me
->map
.fec
.pwid
.group_id
!=
985 wcard
->fec
.pwid
.group_id
))
989 fatalx("lde_wildcard_apply: unexpected fec type");
993 /* gabage collector timer: timer to remove dead entries from the LIB */
997 lde_gc_timer(struct thread
*thread
)
999 struct fec
*fec
, *safe
;
1000 struct fec_node
*fn
;
1003 RB_FOREACH_SAFE(fec
, fec_tree
, &ft
, safe
) {
1004 fn
= (struct fec_node
*) fec
;
1006 if (!LIST_EMPTY(&fn
->nexthops
) ||
1007 !RB_EMPTY(lde_map_head
, &fn
->downstream
) ||
1008 !RB_EMPTY(lde_map_head
, &fn
->upstream
))
1011 if (fn
->local_label
!= NO_LABEL
)
1012 lde_free_label(fn
->local_label
);
1014 fec_remove(&ft
, &fn
->fec
);
1020 log_debug("%s: %u entries removed", __func__
, count
);
1022 lde_gc_start_timer();
1028 lde_gc_start_timer(void)
1030 THREAD_TIMER_OFF(gc_timer
);
1032 thread_add_timer(master
, lde_gc_timer
, NULL
, LDE_GC_INTERVAL
,
1037 lde_gc_stop_timer(void)
1039 THREAD_TIMER_OFF(gc_timer
);