]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/lde_lib.c
release: FRR 3.0-rc1
[mirror_frr.git] / ldpd / lde_lib.c
1 /* $OpenBSD$ */
2
3 /*
4 * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
5 * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
6 *
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.
10 *
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.
18 */
19
20 #include <zebra.h>
21
22 #include "ldpd.h"
23 #include "lde.h"
24 #include "log.h"
25
26 #include "mpls.h"
27
28 static __inline int fec_compare(struct fec *, struct fec *);
29 static int lde_nbr_is_nexthop(struct fec_node *,
30 struct lde_nbr *);
31 static void fec_free(void *);
32 static struct fec_node *fec_add(struct fec *fec);
33 static struct fec_nh *fec_nh_add(struct fec_node *, int, union ldpd_addr *,
34 ifindex_t, uint8_t);
35 static void fec_nh_del(struct fec_nh *);
36
37 RB_GENERATE(fec_tree, fec, entry, fec_compare)
38
39 struct fec_tree ft = RB_INITIALIZER(&ft);
40 struct thread *gc_timer;
41
42 /* FEC tree functions */
43 void
44 fec_init(struct fec_tree *fh)
45 {
46 RB_INIT(fh);
47 }
48
49 static __inline int
50 fec_compare(struct fec *a, struct fec *b)
51 {
52 if (a->type < b->type)
53 return (-1);
54 if (a->type > b->type)
55 return (1);
56
57 switch (a->type) {
58 case FEC_TYPE_IPV4:
59 if (ntohl(a->u.ipv4.prefix.s_addr) <
60 ntohl(b->u.ipv4.prefix.s_addr))
61 return (-1);
62 if (ntohl(a->u.ipv4.prefix.s_addr) >
63 ntohl(b->u.ipv4.prefix.s_addr))
64 return (1);
65 if (a->u.ipv4.prefixlen < b->u.ipv4.prefixlen)
66 return (-1);
67 if (a->u.ipv4.prefixlen > b->u.ipv4.prefixlen)
68 return (1);
69 return (0);
70 case FEC_TYPE_IPV6:
71 if (memcmp(&a->u.ipv6.prefix, &b->u.ipv6.prefix,
72 sizeof(struct in6_addr)) < 0)
73 return (-1);
74 if (memcmp(&a->u.ipv6.prefix, &b->u.ipv6.prefix,
75 sizeof(struct in6_addr)) > 0)
76 return (1);
77 if (a->u.ipv6.prefixlen < b->u.ipv6.prefixlen)
78 return (-1);
79 if (a->u.ipv6.prefixlen > b->u.ipv6.prefixlen)
80 return (1);
81 return (0);
82 case FEC_TYPE_PWID:
83 if (a->u.pwid.type < b->u.pwid.type)
84 return (-1);
85 if (a->u.pwid.type > b->u.pwid.type)
86 return (1);
87 if (a->u.pwid.pwid < b->u.pwid.pwid)
88 return (-1);
89 if (a->u.pwid.pwid > b->u.pwid.pwid)
90 return (1);
91 if (ntohl(a->u.pwid.lsr_id.s_addr) <
92 ntohl(b->u.pwid.lsr_id.s_addr))
93 return (-1);
94 if (ntohl(a->u.pwid.lsr_id.s_addr) >
95 ntohl(b->u.pwid.lsr_id.s_addr))
96 return (1);
97 return (0);
98 }
99
100 return (-1);
101 }
102
103 struct fec *
104 fec_find(struct fec_tree *fh, struct fec *f)
105 {
106 return (RB_FIND(fec_tree, fh, f));
107 }
108
109 int
110 fec_insert(struct fec_tree *fh, struct fec *f)
111 {
112 if (RB_INSERT(fec_tree, fh, f) != NULL)
113 return (-1);
114 return (0);
115 }
116
117 int
118 fec_remove(struct fec_tree *fh, struct fec *f)
119 {
120 if (RB_REMOVE(fec_tree, fh, f) == NULL) {
121 log_warnx("%s failed for %s", __func__, log_fec(f));
122 return (-1);
123 }
124 return (0);
125 }
126
127 void
128 fec_clear(struct fec_tree *fh, void (*free_cb)(void *))
129 {
130 struct fec *f;
131
132 while ((f = RB_ROOT(fh)) != NULL) {
133 fec_remove(fh, f);
134 free_cb(f);
135 }
136 }
137
138 /* routing table functions */
139 static int
140 lde_nbr_is_nexthop(struct fec_node *fn, struct lde_nbr *ln)
141 {
142 struct fec_nh *fnh;
143
144 LIST_FOREACH(fnh, &fn->nexthops, entry)
145 if (lde_address_find(ln, fnh->af, &fnh->nexthop))
146 return (1);
147
148 return (0);
149 }
150
151 void
152 rt_dump(pid_t pid)
153 {
154 struct fec *f;
155 struct fec_node *fn;
156 struct lde_map *me;
157 static struct ctl_rt rtctl;
158
159 RB_FOREACH(f, fec_tree, &ft) {
160 fn = (struct fec_node *)f;
161 if (fn->local_label == NO_LABEL &&
162 RB_EMPTY(&fn->downstream))
163 continue;
164
165 memset(&rtctl, 0, sizeof(rtctl));
166 switch (fn->fec.type) {
167 case FEC_TYPE_IPV4:
168 rtctl.af = AF_INET;
169 rtctl.prefix.v4 = fn->fec.u.ipv4.prefix;
170 rtctl.prefixlen = fn->fec.u.ipv4.prefixlen;
171 break;
172 case FEC_TYPE_IPV6:
173 rtctl.af = AF_INET6;
174 rtctl.prefix.v6 = fn->fec.u.ipv6.prefix;
175 rtctl.prefixlen = fn->fec.u.ipv6.prefixlen;
176 break;
177 default:
178 continue;
179 }
180
181 rtctl.local_label = fn->local_label;
182 if (RB_EMPTY(&fn->downstream)) {
183 rtctl.in_use = 0;
184 rtctl.nexthop.s_addr = INADDR_ANY;
185 rtctl.remote_label = NO_LABEL;
186 rtctl.no_downstream = 1;
187 }
188 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_LIB_BEGIN, 0, pid, &rtctl,
189 sizeof(rtctl));
190
191 RB_FOREACH(me, lde_map_head, &fn->upstream) {
192 rtctl.nexthop = me->nexthop->id;
193 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_LIB_SENT, 0, pid,
194 &rtctl, sizeof(rtctl));
195 }
196
197 RB_FOREACH(me, lde_map_head, &fn->downstream) {
198 rtctl.in_use = lde_nbr_is_nexthop(fn, me->nexthop);
199 rtctl.nexthop = me->nexthop->id;
200 rtctl.remote_label = me->map.label;
201 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_LIB_RCVD, 0, pid,
202 &rtctl, sizeof(rtctl));
203 }
204 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_LIB_END, 0, pid, &rtctl,
205 sizeof(rtctl));
206 }
207 }
208
209 void
210 fec_snap(struct lde_nbr *ln)
211 {
212 struct fec *f;
213 struct fec_node *fn;
214
215 RB_FOREACH(f, fec_tree, &ft) {
216 fn = (struct fec_node *)f;
217 if (fn->local_label == NO_LABEL)
218 continue;
219
220 lde_send_labelmapping(ln, fn, 0);
221 }
222
223 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0, NULL, 0);
224 }
225
226 static void
227 fec_free(void *arg)
228 {
229 struct fec_node *fn = arg;
230 struct fec_nh *fnh;
231
232 while ((fnh = LIST_FIRST(&fn->nexthops)))
233 fec_nh_del(fnh);
234 if (!RB_EMPTY(&fn->downstream))
235 log_warnx("%s: fec %s downstream list not empty", __func__,
236 log_fec(&fn->fec));
237 if (!RB_EMPTY(&fn->upstream))
238 log_warnx("%s: fec %s upstream list not empty", __func__,
239 log_fec(&fn->fec));
240
241 free(fn);
242 }
243
244 void
245 fec_tree_clear(void)
246 {
247 fec_clear(&ft, fec_free);
248 }
249
250 static struct fec_node *
251 fec_add(struct fec *fec)
252 {
253 struct fec_node *fn;
254
255 fn = calloc(1, sizeof(*fn));
256 if (fn == NULL)
257 fatal(__func__);
258
259 fn->fec = *fec;
260 fn->local_label = NO_LABEL;
261 RB_INIT(&fn->upstream);
262 RB_INIT(&fn->downstream);
263 LIST_INIT(&fn->nexthops);
264
265 if (fec_insert(&ft, &fn->fec))
266 log_warnx("failed to add %s to ft tree",
267 log_fec(&fn->fec));
268
269 return (fn);
270 }
271
272 struct fec_nh *
273 fec_nh_find(struct fec_node *fn, int af, union ldpd_addr *nexthop,
274 ifindex_t ifindex, uint8_t priority)
275 {
276 struct fec_nh *fnh;
277
278 LIST_FOREACH(fnh, &fn->nexthops, entry)
279 if (fnh->af == af &&
280 ldp_addrcmp(af, &fnh->nexthop, nexthop) == 0 &&
281 fnh->ifindex == ifindex &&
282 fnh->priority == priority)
283 return (fnh);
284
285 return (NULL);
286 }
287
288 static struct fec_nh *
289 fec_nh_add(struct fec_node *fn, int af, union ldpd_addr *nexthop,
290 ifindex_t ifindex, uint8_t priority)
291 {
292 struct fec_nh *fnh;
293
294 fnh = calloc(1, sizeof(*fnh));
295 if (fnh == NULL)
296 fatal(__func__);
297
298 fnh->af = af;
299 fnh->nexthop = *nexthop;
300 fnh->ifindex = ifindex;
301 fnh->remote_label = NO_LABEL;
302 fnh->priority = priority;
303 LIST_INSERT_HEAD(&fn->nexthops, fnh, entry);
304
305 return (fnh);
306 }
307
308 static void
309 fec_nh_del(struct fec_nh *fnh)
310 {
311 LIST_REMOVE(fnh, entry);
312 free(fnh);
313 }
314
315 void
316 lde_kernel_insert(struct fec *fec, int af, union ldpd_addr *nexthop,
317 ifindex_t ifindex, uint8_t priority, int connected, void *data)
318 {
319 struct fec_node *fn;
320 struct fec_nh *fnh;
321
322 fn = (struct fec_node *)fec_find(&ft, fec);
323 if (fn == NULL)
324 fn = fec_add(fec);
325 if (data)
326 fn->data = data;
327
328 fnh = fec_nh_find(fn, af, nexthop, ifindex, priority);
329 if (fnh == NULL)
330 fnh = fec_nh_add(fn, af, nexthop, ifindex, priority);
331 fnh->flags |= F_FEC_NH_NEW;
332 if (connected)
333 fnh->flags |= F_FEC_NH_CONNECTED;
334 }
335
336 void
337 lde_kernel_remove(struct fec *fec, int af, union ldpd_addr *nexthop,
338 ifindex_t ifindex, uint8_t priority)
339 {
340 struct fec_node *fn;
341 struct fec_nh *fnh;
342
343 fn = (struct fec_node *)fec_find(&ft, fec);
344 if (fn == NULL)
345 /* route lost */
346 return;
347 fnh = fec_nh_find(fn, af, nexthop, ifindex, priority);
348 if (fnh == NULL)
349 /* route lost */
350 return;
351
352 lde_send_delete_klabel(fn, fnh);
353 fec_nh_del(fnh);
354 }
355
356 /*
357 * Whenever a route is changed, zebra advertises its new version without
358 * withdrawing the old one. So, after processing a ZEBRA_REDISTRIBUTE_IPV[46]_ADD
359 * message, we need to check for nexthops that were removed and, for each of
360 * them (if any), withdraw the associated labels from zebra.
361 */
362 void
363 lde_kernel_update(struct fec *fec)
364 {
365 struct fec_node *fn;
366 struct fec_nh *fnh, *safe;
367 struct lde_nbr *ln;
368 struct lde_map *me;
369
370 fn = (struct fec_node *)fec_find(&ft, fec);
371 if (fn == NULL)
372 return;
373
374 LIST_FOREACH_SAFE(fnh, &fn->nexthops, entry, safe) {
375 if (fnh->flags & F_FEC_NH_NEW)
376 fnh->flags &= ~F_FEC_NH_NEW;
377 else {
378 lde_send_delete_klabel(fn, fnh);
379 fec_nh_del(fnh);
380 }
381 }
382
383 if (LIST_EMPTY(&fn->nexthops)) {
384 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
385 lde_send_labelwithdraw(ln, fn, NULL, NULL);
386 fn->data = NULL;
387
388 /*
389 * Do not deallocate the local label now, do that only in the
390 * LIB garbage collector. This will prevent ldpd from changing
391 * the input label of some prefixes too often when running on
392 * an unstable network. Also, restart the garbage collector
393 * timer so that labels are deallocated only when the network
394 * is stabilized.
395 */
396 lde_gc_start_timer();
397 } else {
398 fn->local_label = lde_update_label(fn);
399 if (fn->local_label != NO_LABEL)
400 /* FEC.1: perform lsr label distribution procedure */
401 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
402 lde_send_labelmapping(ln, fn, 1);
403 }
404
405 LIST_FOREACH(fnh, &fn->nexthops, entry) {
406 lde_send_change_klabel(fn, fnh);
407
408 switch (fn->fec.type) {
409 case FEC_TYPE_IPV4:
410 case FEC_TYPE_IPV6:
411 ln = lde_nbr_find_by_addr(fnh->af, &fnh->nexthop);
412 break;
413 case FEC_TYPE_PWID:
414 ln = lde_nbr_find_by_lsrid(fn->fec.u.pwid.lsr_id);
415 break;
416 default:
417 ln = NULL;
418 break;
419 }
420
421 if (ln) {
422 /* FEC.2 */
423 me = (struct lde_map *)fec_find(&ln->recv_map, &fn->fec);
424 if (me)
425 /* FEC.5 */
426 lde_check_mapping(&me->map, ln);
427 }
428 }
429 }
430
431 void
432 lde_check_mapping(struct map *map, struct lde_nbr *ln)
433 {
434 struct fec fec;
435 struct fec_node *fn;
436 struct fec_nh *fnh;
437 struct lde_req *lre;
438 struct lde_map *me;
439 struct l2vpn_pw *pw;
440 int msgsource = 0;
441
442 lde_map2fec(map, ln->id, &fec);
443
444 switch (fec.type) {
445 case FEC_TYPE_IPV4:
446 if (lde_acl_check(ldeconf->ipv4.acl_label_accept_from,
447 AF_INET, (union ldpd_addr *)&ln->id, 32) != FILTER_PERMIT)
448 return;
449 if (lde_acl_check(ldeconf->ipv4.acl_label_accept_for,
450 AF_INET, (union ldpd_addr *)&fec.u.ipv4.prefix,
451 fec.u.ipv4.prefixlen) != FILTER_PERMIT)
452 return;
453 break;
454 case FEC_TYPE_IPV6:
455 if (lde_acl_check(ldeconf->ipv6.acl_label_accept_from,
456 AF_INET, (union ldpd_addr *)&ln->id, 32) != FILTER_PERMIT)
457 return;
458 if (lde_acl_check(ldeconf->ipv6.acl_label_accept_for,
459 AF_INET6, (union ldpd_addr *)&fec.u.ipv6.prefix,
460 fec.u.ipv6.prefixlen) != FILTER_PERMIT)
461 return;
462 break;
463 default:
464 break;
465 }
466
467 fn = (struct fec_node *)fec_find(&ft, &fec);
468 if (fn == NULL)
469 fn = fec_add(&fec);
470
471 /* LMp.1: first check if we have a pending request running */
472 lre = (struct lde_req *)fec_find(&ln->sent_req, &fn->fec);
473 if (lre)
474 /* LMp.2: delete record of outstanding label request */
475 lde_req_del(ln, lre, 1);
476
477 /* RFC 4447 control word and status tlv negotiation */
478 if (map->type == MAP_TYPE_PWID && l2vpn_pw_negotiate(ln, fn, map))
479 return;
480
481 /*
482 * LMp.3 - LMp.8: loop detection - unnecessary for frame-mode
483 * mpls networks.
484 */
485
486 /* LMp.9 */
487 me = (struct lde_map *)fec_find(&ln->recv_map, &fn->fec);
488 if (me) {
489 /* LMp.10 */
490 if (me->map.label != map->label && lre == NULL) {
491 /* LMp.10a */
492 lde_send_labelrelease(ln, fn, NULL, me->map.label);
493
494 /*
495 * Can not use lde_nbr_find_by_addr() because there's
496 * the possibility of multipath.
497 */
498 LIST_FOREACH(fnh, &fn->nexthops, entry) {
499 if (lde_address_find(ln, fnh->af,
500 &fnh->nexthop) == NULL)
501 continue;
502
503 lde_send_delete_klabel(fn, fnh);
504 fnh->remote_label = NO_LABEL;
505 }
506 }
507 }
508
509 /*
510 * LMp.11 - 12: consider multiple nexthops in order to
511 * support multipath
512 */
513 LIST_FOREACH(fnh, &fn->nexthops, entry) {
514 /* LMp.15: install FEC in FIB */
515 switch (fec.type) {
516 case FEC_TYPE_IPV4:
517 case FEC_TYPE_IPV6:
518 if (!lde_address_find(ln, fnh->af, &fnh->nexthop))
519 continue;
520
521 fnh->remote_label = map->label;
522 lde_send_change_klabel(fn, fnh);
523 break;
524 case FEC_TYPE_PWID:
525 pw = (struct l2vpn_pw *) fn->data;
526 if (pw == NULL)
527 continue;
528
529 pw->remote_group = map->fec.pwid.group_id;
530 if (map->flags & F_MAP_PW_IFMTU)
531 pw->remote_mtu = map->fec.pwid.ifmtu;
532 if (map->flags & F_MAP_PW_STATUS)
533 pw->remote_status = map->pw_status;
534 else
535 pw->remote_status = PW_FORWARDING;
536 fnh->remote_label = map->label;
537 if (l2vpn_pw_ok(pw, fnh))
538 lde_send_change_klabel(fn, fnh);
539 break;
540 default:
541 break;
542 }
543
544 msgsource = 1;
545 }
546 /* LMp.13 & LMp.16: Record the mapping from this peer */
547 if (me == NULL)
548 me = lde_map_add(ln, fn, 0);
549 me->map = *map;
550
551 if (msgsource == 0)
552 /* LMp.13: just return since we use liberal lbl retention */
553 return;
554
555 /*
556 * LMp.17 - LMp.27 are unnecessary since we don't need to implement
557 * loop detection. LMp.28 - LMp.30 are unnecessary because we are
558 * merging capable.
559 */
560 }
561
562 void
563 lde_check_request(struct map *map, struct lde_nbr *ln)
564 {
565 struct fec fec;
566 struct lde_req *lre;
567 struct fec_node *fn;
568 struct fec_nh *fnh;
569
570 /* wildcard label request */
571 if (map->type == MAP_TYPE_TYPED_WCARD) {
572 lde_check_request_wcard(map, ln);
573 return;
574 }
575
576 /* LRq.1: skip loop detection (not necessary) */
577
578 /* LRq.2: is there a next hop for fec? */
579 lde_map2fec(map, ln->id, &fec);
580 fn = (struct fec_node *)fec_find(&ft, &fec);
581 if (fn == NULL || LIST_EMPTY(&fn->nexthops)) {
582 /* LRq.5: send No Route notification */
583 lde_send_notification(ln, S_NO_ROUTE, map->msg_id,
584 htons(MSG_TYPE_LABELREQUEST));
585 return;
586 }
587
588 /* LRq.3: is MsgSource the next hop? */
589 LIST_FOREACH(fnh, &fn->nexthops, entry) {
590 switch (fec.type) {
591 case FEC_TYPE_IPV4:
592 case FEC_TYPE_IPV6:
593 if (!lde_address_find(ln, fnh->af, &fnh->nexthop))
594 continue;
595
596 /* LRq.4: send Loop Detected notification */
597 lde_send_notification(ln, S_LOOP_DETECTED, map->msg_id,
598 htons(MSG_TYPE_LABELREQUEST));
599 return;
600 default:
601 break;
602 }
603 }
604
605 /* LRq.6: first check if we have a pending request running */
606 lre = (struct lde_req *)fec_find(&ln->recv_req, &fn->fec);
607 if (lre != NULL)
608 /* LRq.7: duplicate request */
609 return;
610
611 /* LRq.8: record label request */
612 lre = lde_req_add(ln, &fn->fec, 0);
613 if (lre != NULL)
614 lre->msg_id = ntohl(map->msg_id);
615
616 /* LRq.9: perform LSR label distribution */
617 lde_send_labelmapping(ln, fn, 1);
618
619 /*
620 * LRq.10: do nothing (Request Never) since we use liberal
621 * label retention.
622 * LRq.11 - 12 are unnecessary since we are merging capable.
623 */
624 }
625
626 void
627 lde_check_request_wcard(struct map *map, struct lde_nbr *ln)
628 {
629 struct fec *f;
630 struct fec_node *fn;
631 struct lde_req *lre;
632
633 RB_FOREACH(f, fec_tree, &ft) {
634 fn = (struct fec_node *)f;
635
636 /* only a typed wildcard is possible here */
637 if (lde_wildcard_apply(map, &fn->fec, NULL) == 0)
638 continue;
639
640 /* LRq.2: is there a next hop for fec? */
641 if (LIST_EMPTY(&fn->nexthops))
642 continue;
643
644 /* LRq.6: first check if we have a pending request running */
645 lre = (struct lde_req *)fec_find(&ln->recv_req, &fn->fec);
646 if (lre != NULL)
647 /* LRq.7: duplicate request */
648 continue;
649
650 /* LRq.8: record label request */
651 lre = lde_req_add(ln, &fn->fec, 0);
652 if (lre != NULL)
653 lre->msg_id = ntohl(map->msg_id);
654
655 /* LRq.9: perform LSR label distribution */
656 lde_send_labelmapping(ln, fn, 1);
657 }
658 }
659
660 void
661 lde_check_release(struct map *map, struct lde_nbr *ln)
662 {
663 struct fec fec;
664 struct fec_node *fn;
665 struct lde_wdraw *lw;
666 struct lde_map *me;
667 struct fec *pending_map;
668
669 /* wildcard label release */
670 if (map->type == MAP_TYPE_WILDCARD ||
671 map->type == MAP_TYPE_TYPED_WCARD ||
672 (map->type == MAP_TYPE_PWID && !(map->flags & F_MAP_PW_ID))) {
673 lde_check_release_wcard(map, ln);
674 return;
675 }
676
677 lde_map2fec(map, ln->id, &fec);
678 fn = (struct fec_node *)fec_find(&ft, &fec);
679 /* LRl.1: does FEC match a known FEC? */
680 if (fn == NULL)
681 return;
682
683 /* LRl.6: check sent map list and remove it if available */
684 me = (struct lde_map *)fec_find(&ln->sent_map, &fn->fec);
685 if (me && (map->label == NO_LABEL || map->label == me->map.label))
686 lde_map_del(ln, me, 1);
687
688 /* LRl.3: first check if we have a pending withdraw running */
689 lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, &fn->fec);
690 if (lw && (map->label == NO_LABEL || map->label == lw->label)) {
691 /* LRl.4: delete record of outstanding label withdraw */
692 lde_wdraw_del(ln, lw);
693
694 /* send pending label mapping if any */
695 pending_map = fec_find(&ln->sent_map_pending, &fn->fec);
696 if (pending_map) {
697 lde_send_labelmapping(ln, fn, 1);
698 lde_map_pending_del(ln, pending_map);
699 }
700 }
701
702 /*
703 * LRl.11 - 13 are unnecessary since we remove the label from
704 * forwarding/switching as soon as the FEC is unreachable.
705 */
706 }
707
708 void
709 lde_check_release_wcard(struct map *map, struct lde_nbr *ln)
710 {
711 struct fec *f;
712 struct fec_node *fn;
713 struct lde_wdraw *lw;
714 struct lde_map *me;
715 struct fec *pending_map;
716
717 RB_FOREACH(f, fec_tree, &ft) {
718 fn = (struct fec_node *)f;
719 me = (struct lde_map *)fec_find(&ln->sent_map, &fn->fec);
720
721 /* LRl.1: does FEC match a known FEC? */
722 if (lde_wildcard_apply(map, &fn->fec, me) == 0)
723 continue;
724
725 /* LRl.6: check sent map list and remove it if available */
726 if (me &&
727 (map->label == NO_LABEL || map->label == me->map.label))
728 lde_map_del(ln, me, 1);
729
730 /* LRl.3: first check if we have a pending withdraw running */
731 lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, &fn->fec);
732 if (lw && (map->label == NO_LABEL || map->label == lw->label)) {
733 /* LRl.4: delete record of outstanding lbl withdraw */
734 lde_wdraw_del(ln, lw);
735
736 /* send pending label mapping if any */
737 pending_map = fec_find(&ln->sent_map_pending, &fn->fec);
738 if (pending_map) {
739 lde_send_labelmapping(ln, fn, 1);
740 lde_map_pending_del(ln, pending_map);
741 }
742 }
743
744 /*
745 * LRl.11 - 13 are unnecessary since we remove the label from
746 * forwarding/switching as soon as the FEC is unreachable.
747 */
748 }
749 }
750
751 void
752 lde_check_withdraw(struct map *map, struct lde_nbr *ln)
753 {
754 struct fec fec;
755 struct fec_node *fn;
756 struct fec_nh *fnh;
757 struct lde_map *me;
758 struct l2vpn_pw *pw;
759
760 /* wildcard label withdraw */
761 if (map->type == MAP_TYPE_WILDCARD ||
762 map->type == MAP_TYPE_TYPED_WCARD ||
763 (map->type == MAP_TYPE_PWID && !(map->flags & F_MAP_PW_ID))) {
764 lde_check_withdraw_wcard(map, ln);
765 return;
766 }
767
768 lde_map2fec(map, ln->id, &fec);
769 fn = (struct fec_node *)fec_find(&ft, &fec);
770 if (fn == NULL)
771 fn = fec_add(&fec);
772
773 /* LWd.1: remove label from forwarding/switching use */
774 LIST_FOREACH(fnh, &fn->nexthops, entry) {
775 switch (fec.type) {
776 case FEC_TYPE_IPV4:
777 case FEC_TYPE_IPV6:
778 if (!lde_address_find(ln, fnh->af, &fnh->nexthop))
779 continue;
780 break;
781 case FEC_TYPE_PWID:
782 pw = (struct l2vpn_pw *) fn->data;
783 if (pw == NULL)
784 continue;
785 pw->remote_status = PW_NOT_FORWARDING;
786 break;
787 default:
788 break;
789 }
790 if (map->label != NO_LABEL && map->label != fnh->remote_label)
791 continue;
792
793 lde_send_delete_klabel(fn, fnh);
794 fnh->remote_label = NO_LABEL;
795 }
796
797 /* LWd.2: send label release */
798 lde_send_labelrelease(ln, fn, NULL, map->label);
799
800 /* LWd.3: check previously received label mapping */
801 me = (struct lde_map *)fec_find(&ln->recv_map, &fn->fec);
802 if (me && (map->label == NO_LABEL || map->label == me->map.label))
803 /* LWd.4: remove record of previously received lbl mapping */
804 lde_map_del(ln, me, 0);
805 }
806
807 void
808 lde_check_withdraw_wcard(struct map *map, struct lde_nbr *ln)
809 {
810 struct fec *f;
811 struct fec_node *fn;
812 struct fec_nh *fnh;
813 struct lde_map *me;
814 struct l2vpn_pw *pw;
815
816 /* LWd.2: send label release */
817 lde_send_labelrelease(ln, NULL, map, map->label);
818
819 RB_FOREACH(f, fec_tree, &ft) {
820 fn = (struct fec_node *)f;
821 me = (struct lde_map *)fec_find(&ln->recv_map, &fn->fec);
822
823 if (lde_wildcard_apply(map, &fn->fec, me) == 0)
824 continue;
825
826 /* LWd.1: remove label from forwarding/switching use */
827 LIST_FOREACH(fnh, &fn->nexthops, entry) {
828 switch (f->type) {
829 case FEC_TYPE_IPV4:
830 case FEC_TYPE_IPV6:
831 if (!lde_address_find(ln, fnh->af,
832 &fnh->nexthop))
833 continue;
834 break;
835 case FEC_TYPE_PWID:
836 if (f->u.pwid.lsr_id.s_addr != ln->id.s_addr)
837 continue;
838 pw = (struct l2vpn_pw *) fn->data;
839 if (pw)
840 pw->remote_status = PW_NOT_FORWARDING;
841 break;
842 default:
843 break;
844 }
845 if (map->label != NO_LABEL && map->label !=
846 fnh->remote_label)
847 continue;
848
849 lde_send_delete_klabel(fn, fnh);
850 fnh->remote_label = NO_LABEL;
851 }
852
853 /* LWd.3: check previously received label mapping */
854 if (me && (map->label == NO_LABEL ||
855 map->label == me->map.label))
856 /*
857 * LWd.4: remove record of previously received
858 * label mapping
859 */
860 lde_map_del(ln, me, 0);
861 }
862 }
863
864 int
865 lde_wildcard_apply(struct map *wcard, struct fec *fec, struct lde_map *me)
866 {
867 switch (wcard->type) {
868 case MAP_TYPE_WILDCARD:
869 /* full wildcard */
870 return (1);
871 case MAP_TYPE_TYPED_WCARD:
872 switch (wcard->fec.twcard.type) {
873 case MAP_TYPE_PREFIX:
874 if (wcard->fec.twcard.u.prefix_af == AF_INET &&
875 fec->type != FEC_TYPE_IPV4)
876 return (0);
877 if (wcard->fec.twcard.u.prefix_af == AF_INET6 &&
878 fec->type != FEC_TYPE_IPV6)
879 return (0);
880 return (1);
881 case MAP_TYPE_PWID:
882 if (fec->type != FEC_TYPE_PWID)
883 return (0);
884 if (wcard->fec.twcard.u.pw_type != PW_TYPE_WILDCARD &&
885 wcard->fec.twcard.u.pw_type != fec->u.pwid.type)
886 return (0);
887 return (1);
888 default:
889 fatalx("lde_wildcard_apply: unexpected fec type");
890 }
891 break;
892 case MAP_TYPE_PWID:
893 /* RFC4447 pw-id group wildcard */
894 if (fec->type != FEC_TYPE_PWID)
895 return (0);
896 if (fec->u.pwid.type != wcard->fec.pwid.type)
897 return (0);
898 if (me == NULL || (me->map.fec.pwid.group_id !=
899 wcard->fec.pwid.group_id))
900 return (0);
901 return (1);
902 default:
903 fatalx("lde_wildcard_apply: unexpected fec type");
904 }
905 }
906
907 /* gabage collector timer: timer to remove dead entries from the LIB */
908
909 /* ARGSUSED */
910 int
911 lde_gc_timer(struct thread *thread)
912 {
913 struct fec *fec, *safe;
914 struct fec_node *fn;
915 int count = 0;
916
917 RB_FOREACH_SAFE(fec, fec_tree, &ft, safe) {
918 fn = (struct fec_node *) fec;
919
920 if (!LIST_EMPTY(&fn->nexthops) ||
921 !RB_EMPTY(&fn->downstream) ||
922 !RB_EMPTY(&fn->upstream))
923 continue;
924
925 fec_remove(&ft, &fn->fec);
926 free(fn);
927 count++;
928 }
929
930 if (count > 0)
931 log_debug("%s: %u entries removed", __func__, count);
932
933 lde_gc_start_timer();
934
935 return (0);
936 }
937
938 void
939 lde_gc_start_timer(void)
940 {
941 THREAD_TIMER_OFF(gc_timer);
942 gc_timer = thread_add_timer(master, lde_gc_timer, NULL,
943 LDE_GC_INTERVAL);
944 }
945
946 void
947 lde_gc_stop_timer(void)
948 {
949 THREAD_TIMER_OFF(gc_timer);
950 }