]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/lde.c
Merge pull request #9953 from donaldsharp/system_route_replace
[mirror_frr.git] / ldpd / lde.c
1 /* $OpenBSD$ */
2
3 /*
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>
8 *
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.
12 *
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.
20 */
21
22 #include <zebra.h>
23
24 #include "ldp.h"
25 #include "ldpd.h"
26 #include "ldpe.h"
27 #include "log.h"
28 #include "lde.h"
29 #include "ldp_debug.h"
30 #include "rlfa.h"
31
32 #include <lib/log.h>
33 #include "memory.h"
34 #include "privs.h"
35 #include "sigevent.h"
36 #include "mpls.h"
37 #include <lib/linklist.h>
38 #include "zclient.h"
39 #include "stream.h"
40 #include "network.h"
41 #include "libfrr.h"
42
43 static void lde_shutdown(void);
44 static void lde_dispatch_imsg(struct thread *thread);
45 static void lde_dispatch_parent(struct thread *thread);
46 static __inline int lde_nbr_compare(const struct lde_nbr *,
47 const struct lde_nbr *);
48 static struct lde_nbr *lde_nbr_new(uint32_t, struct lde_nbr *);
49 static void lde_nbr_del(struct lde_nbr *);
50 static struct lde_nbr *lde_nbr_find(uint32_t);
51 static void lde_nbr_clear(void);
52 static void lde_nbr_addr_update(struct lde_nbr *,
53 struct lde_addr *, int);
54 static __inline int lde_map_compare(const struct lde_map *,
55 const struct lde_map *);
56 static void lde_map_free(void *);
57 static int lde_address_add(struct lde_nbr *, struct lde_addr *);
58 static int lde_address_del(struct lde_nbr *, struct lde_addr *);
59 static void lde_address_list_free(struct lde_nbr *);
60 static void zclient_sync_init(void);
61 static void lde_label_list_init(void);
62 static int lde_get_label_chunk(void);
63 static void on_get_label_chunk_response(uint32_t start, uint32_t end);
64 static uint32_t lde_get_next_label(void);
65 static bool lde_fec_connected(const struct fec_node *);
66 static bool lde_fec_outside_mpls_network(const struct fec_node *);
67 static void lde_check_filter_af(int, struct ldpd_af_conf *,
68 const char *);
69
70 RB_GENERATE(nbr_tree, lde_nbr, entry, lde_nbr_compare)
71 RB_GENERATE(lde_map_head, lde_map, entry, lde_map_compare)
72
73 struct ldpd_conf *ldeconf;
74 struct nbr_tree lde_nbrs = RB_INITIALIZER(&lde_nbrs);
75
76 static struct imsgev *iev_ldpe;
77 static struct imsgev iev_main_sync_data;
78 static struct imsgev *iev_main, *iev_main_sync;
79
80 /* lde privileges */
81 static zebra_capabilities_t _caps_p [] =
82 {
83 ZCAP_NET_ADMIN
84 };
85
86 static struct zebra_privs_t lde_privs =
87 {
88 #if defined(VTY_GROUP)
89 .vty_group = VTY_GROUP,
90 #endif
91 .caps_p = _caps_p,
92 .cap_num_p = array_size(_caps_p),
93 .cap_num_i = 0
94 };
95
96 /* List of chunks of labels externally assigned by Zebra */
97 static struct list *label_chunk_list;
98 static struct listnode *current_label_chunk;
99
100 /* Synchronous zclient to request labels */
101 static struct zclient *zclient_sync;
102
103 /* SIGINT / SIGTERM handler. */
104 static void
105 sigint(void)
106 {
107 lde_shutdown();
108 }
109
110 static struct frr_signal_t lde_signals[] =
111 {
112 {
113 .signal = SIGHUP,
114 /* ignore */
115 },
116 {
117 .signal = SIGINT,
118 .handler = &sigint,
119 },
120 {
121 .signal = SIGTERM,
122 .handler = &sigint,
123 },
124 };
125
126 /* label decision engine */
127 void
128 lde(void)
129 {
130 #ifdef HAVE_SETPROCTITLE
131 setproctitle("label decision engine");
132 #endif
133 ldpd_process = PROC_LDE_ENGINE;
134 log_procname = log_procnames[PROC_LDE_ENGINE];
135
136 master = frr_init();
137 /* no frr_config_fork() here, allow frr_pthread to create threads */
138 frr_is_after_fork = true;
139
140 /* setup signal handler */
141 signal_init(master, array_size(lde_signals), lde_signals);
142
143 /* setup pipes and event handlers to the parent process */
144 if ((iev_main = calloc(1, sizeof(struct imsgev))) == NULL)
145 fatal(NULL);
146 imsg_init(&iev_main->ibuf, LDPD_FD_ASYNC);
147 iev_main->handler_read = lde_dispatch_parent;
148 thread_add_read(master, iev_main->handler_read, iev_main, iev_main->ibuf.fd,
149 &iev_main->ev_read);
150 iev_main->handler_write = ldp_write_handler;
151
152 memset(&iev_main_sync_data, 0, sizeof(iev_main_sync_data));
153 iev_main_sync = &iev_main_sync_data;
154 imsg_init(&iev_main_sync->ibuf, LDPD_FD_SYNC);
155
156 /* create base configuration */
157 ldeconf = config_new_empty();
158
159 struct thread thread;
160 while (thread_fetch(master, &thread))
161 thread_call(&thread);
162
163 /* NOTREACHED */
164 return;
165 }
166
167 void
168 lde_init(struct ldpd_init *init)
169 {
170 /* drop privileges */
171 lde_privs.user = init->user;
172 lde_privs.group = init->group;
173 zprivs_preinit(&lde_privs);
174 zprivs_init(&lde_privs);
175
176 /* start the LIB garbage collector */
177 lde_gc_start_timer();
178
179 /* Init synchronous zclient and label list */
180 frr_zclient_addr(&zclient_addr, &zclient_addr_len,
181 init->zclient_serv_path);
182 zclient_sync_init();
183 }
184
185 static void
186 lde_shutdown(void)
187 {
188 /* close pipes */
189 if (iev_ldpe) {
190 msgbuf_clear(&iev_ldpe->ibuf.w);
191 close(iev_ldpe->ibuf.fd);
192 iev_ldpe->ibuf.fd = -1;
193 }
194 msgbuf_clear(&iev_main->ibuf.w);
195 close(iev_main->ibuf.fd);
196 iev_main->ibuf.fd = -1;
197 msgbuf_clear(&iev_main_sync->ibuf.w);
198 close(iev_main_sync->ibuf.fd);
199 iev_main_sync->ibuf.fd = -1;
200
201 lde_gc_stop_timer();
202 lde_nbr_clear();
203 fec_tree_clear();
204
205 config_clear(ldeconf);
206
207 if (iev_ldpe)
208 free(iev_ldpe);
209 free(iev_main);
210
211 log_info("label decision engine exiting");
212
213 zlog_fini();
214 exit(0);
215 }
216
217 /* imesg */
218 int
219 lde_imsg_compose_parent(int type, pid_t pid, void *data, uint16_t datalen)
220 {
221 if (iev_main->ibuf.fd == -1)
222 return (0);
223 return (imsg_compose_event(iev_main, type, 0, pid, -1, data, datalen));
224 }
225
226 void
227 lde_imsg_compose_parent_sync(int type, pid_t pid, void *data, uint16_t datalen)
228 {
229 if (iev_main_sync->ibuf.fd == -1)
230 return;
231 imsg_compose_event(iev_main_sync, type, 0, pid, -1, data, datalen);
232 imsg_flush(&iev_main_sync->ibuf);
233 }
234
235 int
236 lde_imsg_compose_ldpe(int type, uint32_t peerid, pid_t pid, void *data,
237 uint16_t datalen)
238 {
239 if (iev_ldpe->ibuf.fd == -1)
240 return (0);
241 return (imsg_compose_event(iev_ldpe, type, peerid, pid,
242 -1, data, datalen));
243 }
244
245 /* ARGSUSED */
246 static void lde_dispatch_imsg(struct thread *thread)
247 {
248 struct imsgev *iev = THREAD_ARG(thread);
249 struct imsgbuf *ibuf = &iev->ibuf;
250 struct imsg imsg;
251 struct lde_nbr *ln;
252 struct map *map;
253 struct lde_addr *lde_addr;
254 struct notify_msg *nm;
255 ssize_t n;
256 int shut = 0;
257
258 iev->ev_read = NULL;
259
260 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
261 fatal("imsg_read error");
262 if (n == 0) /* connection closed */
263 shut = 1;
264
265 for (;;) {
266 if ((n = imsg_get(ibuf, &imsg)) == -1)
267 fatal("lde_dispatch_imsg: imsg_get error");
268 if (n == 0)
269 break;
270
271 switch (imsg.hdr.type) {
272 case IMSG_LABEL_MAPPING_FULL:
273 ln = lde_nbr_find(imsg.hdr.peerid);
274 if (ln == NULL) {
275 log_debug("%s: cannot find lde neighbor",
276 __func__);
277 break;
278 }
279
280 fec_snap(ln);
281 break;
282 case IMSG_LABEL_MAPPING:
283 case IMSG_LABEL_REQUEST:
284 case IMSG_LABEL_RELEASE:
285 case IMSG_LABEL_WITHDRAW:
286 case IMSG_LABEL_ABORT:
287 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
288 sizeof(struct map))
289 fatalx("lde_dispatch_imsg: wrong imsg len");
290 map = imsg.data;
291
292 ln = lde_nbr_find(imsg.hdr.peerid);
293 if (ln == NULL) {
294 log_debug("%s: cannot find lde neighbor",
295 __func__);
296 break;
297 }
298
299 switch (imsg.hdr.type) {
300 case IMSG_LABEL_MAPPING:
301 lde_check_mapping(map, ln, 1);
302 break;
303 case IMSG_LABEL_REQUEST:
304 lde_check_request(map, ln);
305 break;
306 case IMSG_LABEL_RELEASE:
307 lde_check_release(map, ln);
308 break;
309 case IMSG_LABEL_WITHDRAW:
310 lde_check_withdraw(map, ln);
311 break;
312 case IMSG_LABEL_ABORT:
313 /* not necessary */
314 break;
315 }
316 break;
317 case IMSG_ADDRESS_ADD:
318 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
319 sizeof(struct lde_addr))
320 fatalx("lde_dispatch_imsg: wrong imsg len");
321 lde_addr = imsg.data;
322
323 ln = lde_nbr_find(imsg.hdr.peerid);
324 if (ln == NULL) {
325 log_debug("%s: cannot find lde neighbor",
326 __func__);
327 break;
328 }
329 if (lde_address_add(ln, lde_addr) < 0) {
330 log_debug("%s: cannot add address %s, it already exists", __func__,
331 log_addr(lde_addr->af, &lde_addr->addr));
332 }
333 break;
334 case IMSG_ADDRESS_DEL:
335 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
336 sizeof(struct lde_addr))
337 fatalx("lde_dispatch_imsg: wrong imsg len");
338 lde_addr = imsg.data;
339
340 ln = lde_nbr_find(imsg.hdr.peerid);
341 if (ln == NULL) {
342 log_debug("%s: cannot find lde neighbor",
343 __func__);
344 break;
345 }
346 if (lde_address_del(ln, lde_addr) < 0) {
347 log_debug("%s: cannot delete address %s, it does not exist", __func__,
348 log_addr(lde_addr->af, &lde_addr->addr));
349 }
350 break;
351 case IMSG_NOTIFICATION:
352 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
353 sizeof(struct notify_msg))
354 fatalx("lde_dispatch_imsg: wrong imsg len");
355 nm = imsg.data;
356
357 ln = lde_nbr_find(imsg.hdr.peerid);
358 if (ln == NULL) {
359 log_debug("%s: cannot find lde neighbor",
360 __func__);
361 break;
362 }
363
364 switch (nm->status_code) {
365 case S_PW_STATUS:
366 l2vpn_recv_pw_status(ln, nm);
367 break;
368 case S_ENDOFLIB:
369 /*
370 * Do nothing for now. Should be useful in
371 * the future when we implement LDP-IGP
372 * Synchronization (RFC 5443) and Graceful
373 * Restart (RFC 3478).
374 */
375 default:
376 break;
377 }
378 break;
379 case IMSG_NEIGHBOR_UP:
380 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
381 sizeof(struct lde_nbr))
382 fatalx("lde_dispatch_imsg: wrong imsg len");
383
384 if (lde_nbr_find(imsg.hdr.peerid))
385 fatalx("lde_dispatch_imsg: neighbor already exists");
386 lde_nbr_new(imsg.hdr.peerid, imsg.data);
387 break;
388 case IMSG_NEIGHBOR_DOWN:
389 lde_nbr_del(lde_nbr_find(imsg.hdr.peerid));
390 break;
391 case IMSG_CTL_SHOW_LIB:
392 rt_dump(imsg.hdr.pid);
393
394 lde_imsg_compose_ldpe(IMSG_CTL_END, 0,
395 imsg.hdr.pid, NULL, 0);
396 break;
397 case IMSG_CTL_SHOW_L2VPN_PW:
398 l2vpn_pw_ctl(imsg.hdr.pid);
399
400 lde_imsg_compose_ldpe(IMSG_CTL_END, 0,
401 imsg.hdr.pid, NULL, 0);
402 break;
403 case IMSG_CTL_SHOW_L2VPN_BINDING:
404 l2vpn_binding_ctl(imsg.hdr.pid);
405
406 lde_imsg_compose_ldpe(IMSG_CTL_END, 0,
407 imsg.hdr.pid, NULL, 0);
408 break;
409 default:
410 log_debug("%s: unexpected imsg %d", __func__,
411 imsg.hdr.type);
412 break;
413 }
414 imsg_free(&imsg);
415 }
416 if (!shut)
417 imsg_event_add(iev);
418 else {
419 /* this pipe is dead, so remove the event handlers and exit */
420 thread_cancel(&iev->ev_read);
421 thread_cancel(&iev->ev_write);
422 lde_shutdown();
423 }
424 }
425
426 /* ARGSUSED */
427 static void lde_dispatch_parent(struct thread *thread)
428 {
429 static struct ldpd_conf *nconf;
430 struct iface *iface, *niface;
431 struct tnbr *ntnbr;
432 struct nbr_params *nnbrp;
433 static struct l2vpn *l2vpn, *nl2vpn;
434 struct l2vpn_if *lif, *nlif;
435 struct l2vpn_pw *pw, *npw;
436 struct imsg imsg;
437 struct kif *kif;
438 struct kroute *kr;
439 int fd;
440 struct imsgev *iev = THREAD_ARG(thread);
441 struct imsgbuf *ibuf = &iev->ibuf;
442 ssize_t n;
443 int shut = 0;
444 struct fec fec;
445 struct ldp_access *laccess;
446 struct ldp_rlfa_node *rnode, *rntmp;
447 struct ldp_rlfa_client *rclient;
448 struct zapi_rlfa_request *rlfa_req;
449 struct zapi_rlfa_igp *rlfa_igp;
450
451 iev->ev_read = NULL;
452
453 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
454 fatal("imsg_read error");
455 if (n == 0) /* connection closed */
456 shut = 1;
457
458 for (;;) {
459 if ((n = imsg_get(ibuf, &imsg)) == -1)
460 fatal("lde_dispatch_parent: imsg_get error");
461 if (n == 0)
462 break;
463
464 switch (imsg.hdr.type) {
465 case IMSG_IFSTATUS:
466 if (imsg.hdr.len != IMSG_HEADER_SIZE +
467 sizeof(struct kif))
468 fatalx("IFSTATUS imsg with wrong len");
469 kif = imsg.data;
470
471 iface = if_lookup_name(ldeconf, kif->ifname);
472 if (iface) {
473 if_update_info(iface, kif);
474
475 /* if up see if any labels need to be updated */
476 if (kif->operative)
477 lde_route_update(iface, AF_UNSPEC);
478 break;
479 }
480
481 RB_FOREACH(l2vpn, l2vpn_head, &ldeconf->l2vpn_tree) {
482 lif = l2vpn_if_find(l2vpn, kif->ifname);
483 if (lif) {
484 l2vpn_if_update_info(lif, kif);
485 break;
486 }
487 pw = l2vpn_pw_find(l2vpn, kif->ifname);
488 if (pw) {
489 l2vpn_pw_update_info(pw, kif);
490 break;
491 }
492 }
493 break;
494 case IMSG_PW_UPDATE:
495 if (imsg.hdr.len != IMSG_HEADER_SIZE +
496 sizeof(struct zapi_pw_status))
497 fatalx("PW_UPDATE imsg with wrong len");
498
499 if (l2vpn_pw_status_update(imsg.data) != 0)
500 log_warnx("%s: error updating PW status",
501 __func__);
502 break;
503 case IMSG_NETWORK_ADD:
504 case IMSG_NETWORK_UPDATE:
505 if (imsg.hdr.len != IMSG_HEADER_SIZE +
506 sizeof(struct kroute)) {
507 log_warnx("%s: wrong imsg len", __func__);
508 break;
509 }
510 kr = imsg.data;
511
512 switch (kr->af) {
513 case AF_INET:
514 fec.type = FEC_TYPE_IPV4;
515 fec.u.ipv4.prefix = kr->prefix.v4;
516 fec.u.ipv4.prefixlen = kr->prefixlen;
517 break;
518 case AF_INET6:
519 fec.type = FEC_TYPE_IPV6;
520 fec.u.ipv6.prefix = kr->prefix.v6;
521 fec.u.ipv6.prefixlen = kr->prefixlen;
522 break;
523 default:
524 fatalx("lde_dispatch_parent: unknown af");
525 }
526
527 switch (imsg.hdr.type) {
528 case IMSG_NETWORK_ADD:
529 lde_kernel_insert(&fec, kr->af, &kr->nexthop,
530 kr->ifindex, kr->route_type,
531 kr->route_instance,
532 kr->flags & F_CONNECTED, NULL);
533 break;
534 case IMSG_NETWORK_UPDATE:
535 lde_kernel_update(&fec);
536 break;
537 }
538 break;
539 case IMSG_SOCKET_IPC:
540 if (iev_ldpe) {
541 log_warnx("%s: received unexpected imsg fd to ldpe", __func__);
542 break;
543 }
544 if ((fd = imsg.fd) == -1) {
545 log_warnx("%s: expected to receive imsg fd to ldpe but didn't receive any", __func__);
546 break;
547 }
548
549 if ((iev_ldpe = malloc(sizeof(struct imsgev))) == NULL)
550 fatal(NULL);
551 imsg_init(&iev_ldpe->ibuf, fd);
552 iev_ldpe->handler_read = lde_dispatch_imsg;
553 thread_add_read(master, iev_ldpe->handler_read, iev_ldpe, iev_ldpe->ibuf.fd,
554 &iev_ldpe->ev_read);
555 iev_ldpe->handler_write = ldp_write_handler;
556 iev_ldpe->ev_write = NULL;
557 break;
558 case IMSG_INIT:
559 if (imsg.hdr.len != IMSG_HEADER_SIZE +
560 sizeof(struct ldpd_init))
561 fatalx("INIT imsg with wrong len");
562
563 memcpy(&init, imsg.data, sizeof(init));
564 lde_init(&init);
565 break;
566 case IMSG_AGENTX_ENABLED:
567 ldp_agentx_enabled();
568 break;
569 case IMSG_RECONF_CONF:
570 if ((nconf = malloc(sizeof(struct ldpd_conf))) ==
571 NULL)
572 fatal(NULL);
573 memcpy(nconf, imsg.data, sizeof(struct ldpd_conf));
574
575 RB_INIT(iface_head, &nconf->iface_tree);
576 RB_INIT(tnbr_head, &nconf->tnbr_tree);
577 RB_INIT(nbrp_head, &nconf->nbrp_tree);
578 RB_INIT(l2vpn_head, &nconf->l2vpn_tree);
579 break;
580 case IMSG_RECONF_IFACE:
581 if ((niface = malloc(sizeof(struct iface))) == NULL)
582 fatal(NULL);
583 memcpy(niface, imsg.data, sizeof(struct iface));
584
585 RB_INSERT(iface_head, &nconf->iface_tree, niface);
586 break;
587 case IMSG_RECONF_TNBR:
588 if ((ntnbr = malloc(sizeof(struct tnbr))) == NULL)
589 fatal(NULL);
590 memcpy(ntnbr, imsg.data, sizeof(struct tnbr));
591
592 RB_INSERT(tnbr_head, &nconf->tnbr_tree, ntnbr);
593 break;
594 case IMSG_RECONF_NBRP:
595 if ((nnbrp = malloc(sizeof(struct nbr_params))) == NULL)
596 fatal(NULL);
597 memcpy(nnbrp, imsg.data, sizeof(struct nbr_params));
598
599 RB_INSERT(nbrp_head, &nconf->nbrp_tree, nnbrp);
600 break;
601 case IMSG_RECONF_L2VPN:
602 if ((nl2vpn = malloc(sizeof(struct l2vpn))) == NULL)
603 fatal(NULL);
604 memcpy(nl2vpn, imsg.data, sizeof(struct l2vpn));
605
606 RB_INIT(l2vpn_if_head, &nl2vpn->if_tree);
607 RB_INIT(l2vpn_pw_head, &nl2vpn->pw_tree);
608 RB_INIT(l2vpn_pw_head, &nl2vpn->pw_inactive_tree);
609
610 RB_INSERT(l2vpn_head, &nconf->l2vpn_tree, nl2vpn);
611 break;
612 case IMSG_RECONF_L2VPN_IF:
613 if ((nlif = malloc(sizeof(struct l2vpn_if))) == NULL)
614 fatal(NULL);
615 memcpy(nlif, imsg.data, sizeof(struct l2vpn_if));
616
617 RB_INSERT(l2vpn_if_head, &nl2vpn->if_tree, nlif);
618 break;
619 case IMSG_RECONF_L2VPN_PW:
620 if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL)
621 fatal(NULL);
622 memcpy(npw, imsg.data, sizeof(struct l2vpn_pw));
623
624 RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_tree, npw);
625 break;
626 case IMSG_RECONF_L2VPN_IPW:
627 if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL)
628 fatal(NULL);
629 memcpy(npw, imsg.data, sizeof(struct l2vpn_pw));
630
631 RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_inactive_tree, npw);
632 break;
633 case IMSG_RECONF_END:
634 merge_config(ldeconf, nconf);
635 ldp_clear_config(nconf);
636 nconf = NULL;
637 break;
638 case IMSG_DEBUG_UPDATE:
639 if (imsg.hdr.len != IMSG_HEADER_SIZE +
640 sizeof(ldp_debug)) {
641 log_warnx("%s: wrong imsg len", __func__);
642 break;
643 }
644 memcpy(&ldp_debug, imsg.data, sizeof(ldp_debug));
645 break;
646 case IMSG_FILTER_UPDATE:
647 if (imsg.hdr.len != IMSG_HEADER_SIZE +
648 sizeof(struct ldp_access)) {
649 log_warnx("%s: wrong imsg len", __func__);
650 break;
651 }
652 laccess = imsg.data;
653 lde_check_filter_af(AF_INET, &ldeconf->ipv4,
654 laccess->name);
655 lde_check_filter_af(AF_INET6, &ldeconf->ipv6,
656 laccess->name);
657 break;
658 case IMSG_RLFA_REG:
659 if (imsg.hdr.len != IMSG_HEADER_SIZE +
660 sizeof(struct zapi_rlfa_request)) {
661 log_warnx("%s: wrong imsg len", __func__);
662 break;
663 }
664 rlfa_req = imsg.data;
665 rnode = rlfa_node_find(&rlfa_req->destination,
666 rlfa_req->pq_address);
667 if (!rnode)
668 rnode = rlfa_node_new(&rlfa_req->destination,
669 rlfa_req->pq_address);
670 rclient = rlfa_client_find(rnode, &rlfa_req->igp);
671 if (rclient)
672 /* RLFA already registered - do nothing */
673 break;
674 rclient = rlfa_client_new(rnode, &rlfa_req->igp);
675 lde_rlfa_check(rclient);
676 break;
677 case IMSG_RLFA_UNREG_ALL:
678 if (imsg.hdr.len != IMSG_HEADER_SIZE +
679 sizeof(struct zapi_rlfa_igp)) {
680 log_warnx("%s: wrong imsg len", __func__);
681 break;
682 }
683 rlfa_igp = imsg.data;
684
685 RB_FOREACH_SAFE (rnode, ldp_rlfa_node_head,
686 &rlfa_node_tree, rntmp) {
687 rclient = rlfa_client_find(rnode, rlfa_igp);
688 if (!rclient)
689 continue;
690
691 rlfa_client_del(rclient);
692 }
693 break;
694 default:
695 log_debug("%s: unexpected imsg %d", __func__,
696 imsg.hdr.type);
697 break;
698 }
699 imsg_free(&imsg);
700 }
701 if (!shut)
702 imsg_event_add(iev);
703 else {
704 /* this pipe is dead, so remove the event handlers and exit */
705 thread_cancel(&iev->ev_read);
706 thread_cancel(&iev->ev_write);
707 lde_shutdown();
708 }
709 }
710
711 int
712 lde_acl_check(char *acl_name, int af, union ldpd_addr *addr, uint8_t prefixlen)
713 {
714 return ldp_acl_request(iev_main_sync, acl_name, af, addr, prefixlen);
715 }
716
717 static bool lde_fec_connected(const struct fec_node *fn)
718 {
719 struct fec_nh *fnh;
720
721 LIST_FOREACH(fnh, &fn->nexthops, entry)
722 if (fnh->flags & F_FEC_NH_CONNECTED)
723 return true;
724
725 return false;
726 }
727
728 static bool lde_fec_outside_mpls_network(const struct fec_node *fn)
729 {
730 struct fec_nh *fnh;
731
732 LIST_FOREACH(fnh, &fn->nexthops, entry)
733 if (!(fnh->flags & F_FEC_NH_NO_LDP))
734 return false;
735
736 return true;
737 }
738
739 uint32_t
740 lde_update_label(struct fec_node *fn)
741 {
742
743 /* should we allocate a label for this fec? */
744 switch (fn->fec.type) {
745 case FEC_TYPE_IPV4:
746 if ((ldeconf->ipv4.flags & F_LDPD_AF_ALLOCHOSTONLY)
747 && fn->fec.u.ipv4.prefixlen != IPV4_MAX_BITLEN)
748 return (NO_LABEL);
749 if (lde_acl_check(ldeconf->ipv4.acl_label_allocate_for,
750 AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix,
751 fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT)
752 return (NO_LABEL);
753 break;
754 case FEC_TYPE_IPV6:
755 if ((ldeconf->ipv6.flags & F_LDPD_AF_ALLOCHOSTONLY)
756 && fn->fec.u.ipv6.prefixlen != IPV6_MAX_BITLEN)
757 return (NO_LABEL);
758 if (lde_acl_check(ldeconf->ipv6.acl_label_allocate_for,
759 AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix,
760 fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT)
761 return (NO_LABEL);
762 break;
763 default:
764 break;
765 }
766
767 /*
768 * If connected interface act as egress for fec.
769 * If LDP is not configured on an interface but there
770 * are other NHs with interfaces configured with LDP
771 * then don't act as an egress for the fec, otherwise
772 * act as an egress for the fec
773 */
774 if (lde_fec_connected(fn) || lde_fec_outside_mpls_network(fn)) {
775 /* choose implicit or explicit-null depending on configuration */
776 switch (fn->fec.type) {
777 case FEC_TYPE_IPV4:
778 if (!(ldeconf->ipv4.flags & F_LDPD_AF_EXPNULL))
779 return (MPLS_LABEL_IMPLICIT_NULL);
780 if (lde_acl_check(ldeconf->ipv4.acl_label_expnull_for,
781 AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix,
782 fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT)
783 return (MPLS_LABEL_IMPLICIT_NULL);
784 return MPLS_LABEL_IPV4_EXPLICIT_NULL;
785 case FEC_TYPE_IPV6:
786 if (!(ldeconf->ipv6.flags & F_LDPD_AF_EXPNULL))
787 return (MPLS_LABEL_IMPLICIT_NULL);
788 if (lde_acl_check(ldeconf->ipv6.acl_label_expnull_for,
789 AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix,
790 fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT)
791 return (MPLS_LABEL_IMPLICIT_NULL);
792 return MPLS_LABEL_IPV6_EXPLICIT_NULL;
793 default:
794 break;
795 }
796 }
797
798 /* preserve current label if there's no need to update it */
799 if (fn->local_label != NO_LABEL &&
800 fn->local_label > MPLS_LABEL_RESERVED_MAX)
801 return (fn->local_label);
802
803 return (lde_get_next_label());
804 }
805
806 void
807 lde_send_change_klabel(struct fec_node *fn, struct fec_nh *fnh)
808 {
809 struct kroute kr;
810 struct zapi_pw zpw;
811 struct l2vpn_pw *pw;
812
813 /*
814 * Ordered Control: don't program label into HW until a
815 * labelmap msg has been received from upstream router
816 */
817 if (fnh->flags & F_FEC_NH_DEFER)
818 return;
819
820 switch (fn->fec.type) {
821 case FEC_TYPE_IPV4:
822 memset(&kr, 0, sizeof(kr));
823 kr.af = AF_INET;
824 kr.prefix.v4 = fn->fec.u.ipv4.prefix;
825 kr.prefixlen = fn->fec.u.ipv4.prefixlen;
826 kr.nexthop.v4 = fnh->nexthop.v4;
827 kr.ifindex = fnh->ifindex;
828 kr.local_label = fn->local_label;
829 kr.remote_label = fnh->remote_label;
830 kr.route_type = fnh->route_type;
831 kr.route_instance = fnh->route_instance;
832 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE, 0, &kr,
833 sizeof(kr));
834 break;
835 case FEC_TYPE_IPV6:
836 memset(&kr, 0, sizeof(kr));
837 kr.af = AF_INET6;
838 kr.prefix.v6 = fn->fec.u.ipv6.prefix;
839 kr.prefixlen = fn->fec.u.ipv6.prefixlen;
840 kr.nexthop.v6 = fnh->nexthop.v6;
841 kr.ifindex = fnh->ifindex;
842 kr.local_label = fn->local_label;
843 kr.remote_label = fnh->remote_label;
844 kr.route_type = fnh->route_type;
845 kr.route_instance = fnh->route_instance;
846
847 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE, 0, &kr,
848 sizeof(kr));
849 break;
850 case FEC_TYPE_PWID:
851 pw = (struct l2vpn_pw *) fn->data;
852 if (!pw || fn->local_label == NO_LABEL ||
853 fnh->remote_label == NO_LABEL)
854 return;
855
856 pw->enabled = true;
857 pw2zpw(pw, &zpw);
858 zpw.local_label = fn->local_label;
859 zpw.remote_label = fnh->remote_label;
860 lde_imsg_compose_parent(IMSG_KPW_SET, 0, &zpw, sizeof(zpw));
861 break;
862 }
863 }
864
865 void
866 lde_send_delete_klabel(struct fec_node *fn, struct fec_nh *fnh)
867 {
868 struct kroute kr;
869 struct zapi_pw zpw;
870 struct l2vpn_pw *pw;
871
872 switch (fn->fec.type) {
873 case FEC_TYPE_IPV4:
874 memset(&kr, 0, sizeof(kr));
875 kr.af = AF_INET;
876 kr.prefix.v4 = fn->fec.u.ipv4.prefix;
877 kr.prefixlen = fn->fec.u.ipv4.prefixlen;
878 kr.nexthop.v4 = fnh->nexthop.v4;
879 kr.ifindex = fnh->ifindex;
880 kr.local_label = fn->local_label;
881 kr.remote_label = fnh->remote_label;
882 kr.route_type = fnh->route_type;
883 kr.route_instance = fnh->route_instance;
884
885 lde_imsg_compose_parent(IMSG_KLABEL_DELETE, 0, &kr,
886 sizeof(kr));
887 break;
888 case FEC_TYPE_IPV6:
889 memset(&kr, 0, sizeof(kr));
890 kr.af = AF_INET6;
891 kr.prefix.v6 = fn->fec.u.ipv6.prefix;
892 kr.prefixlen = fn->fec.u.ipv6.prefixlen;
893 kr.nexthop.v6 = fnh->nexthop.v6;
894 kr.ifindex = fnh->ifindex;
895 kr.local_label = fn->local_label;
896 kr.remote_label = fnh->remote_label;
897 kr.route_type = fnh->route_type;
898 kr.route_instance = fnh->route_instance;
899
900 lde_imsg_compose_parent(IMSG_KLABEL_DELETE, 0, &kr,
901 sizeof(kr));
902 break;
903 case FEC_TYPE_PWID:
904 pw = (struct l2vpn_pw *) fn->data;
905 if (!pw)
906 return;
907
908 pw->enabled = false;
909 pw2zpw(pw, &zpw);
910 zpw.local_label = fn->local_label;
911 zpw.remote_label = fnh->remote_label;
912 lde_imsg_compose_parent(IMSG_KPW_UNSET, 0, &zpw, sizeof(zpw));
913 break;
914 }
915 }
916
917 void
918 lde_fec2prefix(const struct fec *fec, struct prefix *prefix)
919 {
920 memset(prefix, 0, sizeof(*prefix));
921 switch (fec->type) {
922 case FEC_TYPE_IPV4:
923 prefix->family = AF_INET;
924 prefix->u.prefix4 = fec->u.ipv4.prefix;
925 prefix->prefixlen = fec->u.ipv4.prefixlen;
926 break;
927 case FEC_TYPE_IPV6:
928 prefix->family = AF_INET6;
929 prefix->u.prefix6 = fec->u.ipv6.prefix;
930 prefix->prefixlen = fec->u.ipv6.prefixlen;
931 break;
932 default:
933 prefix->family = AF_UNSPEC;
934 break;
935 }
936 }
937
938 void
939 lde_prefix2fec(const struct prefix *prefix, struct fec *fec)
940 {
941 memset(fec, 0, sizeof(*fec));
942 switch (prefix->family) {
943 case AF_INET:
944 fec->type = FEC_TYPE_IPV4;
945 fec->u.ipv4.prefix = prefix->u.prefix4;
946 fec->u.ipv4.prefixlen = prefix->prefixlen;
947 break;
948 case AF_INET6:
949 fec->type = FEC_TYPE_IPV6;
950 fec->u.ipv6.prefix = prefix->u.prefix6;
951 fec->u.ipv6.prefixlen = prefix->prefixlen;
952 break;
953 default:
954 fatalx("lde_prefix2fec: unknown af");
955 break;
956 }
957 }
958
959 void
960 lde_fec2map(struct fec *fec, struct map *map)
961 {
962 memset(map, 0, sizeof(*map));
963
964 switch (fec->type) {
965 case FEC_TYPE_IPV4:
966 map->type = MAP_TYPE_PREFIX;
967 map->fec.prefix.af = AF_INET;
968 map->fec.prefix.prefix.v4 = fec->u.ipv4.prefix;
969 map->fec.prefix.prefixlen = fec->u.ipv4.prefixlen;
970 break;
971 case FEC_TYPE_IPV6:
972 map->type = MAP_TYPE_PREFIX;
973 map->fec.prefix.af = AF_INET6;
974 map->fec.prefix.prefix.v6 = fec->u.ipv6.prefix;
975 map->fec.prefix.prefixlen = fec->u.ipv6.prefixlen;
976 break;
977 case FEC_TYPE_PWID:
978 map->type = MAP_TYPE_PWID;
979 map->fec.pwid.type = fec->u.pwid.type;
980 map->fec.pwid.group_id = 0;
981 map->flags |= F_MAP_PW_ID;
982 map->fec.pwid.pwid = fec->u.pwid.pwid;
983 break;
984 }
985 }
986
987 void
988 lde_map2fec(struct map *map, struct in_addr lsr_id, struct fec *fec)
989 {
990 memset(fec, 0, sizeof(*fec));
991
992 switch (map->type) {
993 case MAP_TYPE_PREFIX:
994 switch (map->fec.prefix.af) {
995 case AF_INET:
996 fec->type = FEC_TYPE_IPV4;
997 fec->u.ipv4.prefix = map->fec.prefix.prefix.v4;
998 fec->u.ipv4.prefixlen = map->fec.prefix.prefixlen;
999 break;
1000 case AF_INET6:
1001 fec->type = FEC_TYPE_IPV6;
1002 fec->u.ipv6.prefix = map->fec.prefix.prefix.v6;
1003 fec->u.ipv6.prefixlen = map->fec.prefix.prefixlen;
1004 break;
1005 default:
1006 fatalx("lde_map2fec: unknown af");
1007 break;
1008 }
1009 break;
1010 case MAP_TYPE_PWID:
1011 fec->type = FEC_TYPE_PWID;
1012 fec->u.pwid.type = map->fec.pwid.type;
1013 fec->u.pwid.pwid = map->fec.pwid.pwid;
1014 fec->u.pwid.lsr_id = lsr_id;
1015 break;
1016 }
1017 }
1018
1019 void
1020 lde_send_labelmapping(struct lde_nbr *ln, struct fec_node *fn, int single)
1021 {
1022 struct lde_wdraw *lw;
1023 struct lde_map *me;
1024 struct lde_req *lre;
1025 struct map map;
1026 struct l2vpn_pw *pw;
1027 struct fec_nh *fnh;
1028 bool allow = false;
1029
1030 /*
1031 * Ordered Control: do not send a labelmap msg until
1032 * a labelmap message is received from downstream router
1033 * and don't send labelmap back to downstream router
1034 */
1035 if (ldeconf->flags & F_LDPD_ORDERED_CONTROL) {
1036 LIST_FOREACH(fnh, &fn->nexthops, entry) {
1037 if (fnh->flags & F_FEC_NH_DEFER)
1038 continue;
1039
1040 if (lde_address_find(ln, fnh->af, &fnh->nexthop))
1041 return;
1042 allow = true;
1043 break;
1044 }
1045 if (!allow)
1046 return;
1047 }
1048
1049 /*
1050 * We shouldn't send a new label mapping if we have a pending
1051 * label release to receive. In this case, schedule to send a
1052 * label mapping as soon as a label release is received.
1053 */
1054 lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, &fn->fec);
1055 if (lw) {
1056 if (!fec_find(&ln->sent_map_pending, &fn->fec)) {
1057 debug_evt("%s: FEC %s: scheduling to send label mapping later (waiting for pending label release)",
1058 __func__, log_fec(&fn->fec));
1059 lde_map_pending_add(ln, fn);
1060 }
1061 return;
1062 }
1063
1064 /*
1065 * This function skips SL.1 - 3 and SL.9 - 14 because the label
1066 * allocation is done way earlier (because of the merging nature of
1067 * ldpd).
1068 */
1069
1070 lde_fec2map(&fn->fec, &map);
1071 switch (fn->fec.type) {
1072 case FEC_TYPE_IPV4:
1073 if (!ln->v4_enabled)
1074 return;
1075 if (lde_acl_check(ldeconf->ipv4.acl_label_advertise_to,
1076 AF_INET, (union ldpd_addr *)&ln->id, 32) != FILTER_PERMIT)
1077 return;
1078 if (lde_acl_check(ldeconf->ipv4.acl_label_advertise_for,
1079 AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix,
1080 fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT)
1081 return;
1082 break;
1083 case FEC_TYPE_IPV6:
1084 if (!ln->v6_enabled)
1085 return;
1086 if (lde_acl_check(ldeconf->ipv6.acl_label_advertise_to,
1087 AF_INET, (union ldpd_addr *)&ln->id, 32) != FILTER_PERMIT)
1088 return;
1089 if (lde_acl_check(ldeconf->ipv6.acl_label_advertise_for,
1090 AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix,
1091 fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT)
1092 return;
1093 break;
1094 case FEC_TYPE_PWID:
1095 pw = (struct l2vpn_pw *) fn->data;
1096 if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr)
1097 /* not the remote end of the pseudowire */
1098 return;
1099
1100 map.flags |= F_MAP_PW_IFMTU;
1101 map.fec.pwid.ifmtu = pw->l2vpn->mtu;
1102 if (pw->flags & F_PW_CWORD)
1103 map.flags |= F_MAP_PW_CWORD;
1104 if (pw->flags & F_PW_STATUSTLV) {
1105 map.flags |= F_MAP_PW_STATUS;
1106 map.pw_status = pw->local_status;
1107 }
1108 break;
1109 }
1110 map.label = fn->local_label;
1111
1112 /* SL.6: is there a pending request for this mapping? */
1113 lre = (struct lde_req *)fec_find(&ln->recv_req, &fn->fec);
1114 if (lre) {
1115 /* set label request msg id in the mapping response. */
1116 map.requestid = lre->msg_id;
1117 map.flags = F_MAP_REQ_ID;
1118
1119 /* SL.7: delete record of pending request */
1120 lde_req_del(ln, lre, 0);
1121 }
1122
1123 /* SL.4: send label mapping */
1124 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD, ln->peerid, 0,
1125 &map, sizeof(map));
1126 if (single)
1127 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0,
1128 NULL, 0);
1129
1130 /* SL.5: record sent label mapping */
1131 me = (struct lde_map *)fec_find(&ln->sent_map, &fn->fec);
1132 if (me == NULL)
1133 me = lde_map_add(ln, fn, 1);
1134 me->map = map;
1135 }
1136
1137 void
1138 lde_send_labelwithdraw(struct lde_nbr *ln, struct fec_node *fn,
1139 struct map *wcard, struct status_tlv *st)
1140 {
1141 struct lde_wdraw *lw;
1142 struct map map;
1143 struct fec *f;
1144 struct l2vpn_pw *pw;
1145
1146 if (fn) {
1147 lde_fec2map(&fn->fec, &map);
1148 switch (fn->fec.type) {
1149 case FEC_TYPE_IPV4:
1150 if (!ln->v4_enabled)
1151 return;
1152 break;
1153 case FEC_TYPE_IPV6:
1154 if (!ln->v6_enabled)
1155 return;
1156 break;
1157 case FEC_TYPE_PWID:
1158 pw = (struct l2vpn_pw *) fn->data;
1159 if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr)
1160 /* not the remote end of the pseudowire */
1161 return;
1162
1163 if (pw->flags & F_PW_CWORD)
1164 map.flags |= F_MAP_PW_CWORD;
1165 break;
1166 }
1167 map.label = fn->local_label;
1168 } else
1169 memcpy(&map, wcard, sizeof(map));
1170
1171 if (st) {
1172 map.st.status_code = st->status_code;
1173 map.st.msg_id = st->msg_id;
1174 map.st.msg_type = st->msg_type;
1175 map.flags |= F_MAP_STATUS;
1176 }
1177
1178 /* SWd.1: send label withdraw. */
1179 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD, ln->peerid, 0,
1180 &map, sizeof(map));
1181 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD_END, ln->peerid, 0, NULL, 0);
1182
1183 /* SWd.2: record label withdraw. */
1184 if (fn) {
1185 lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, &fn->fec);
1186 if (lw == NULL)
1187 lw = lde_wdraw_add(ln, fn);
1188 lw->label = map.label;
1189 } else {
1190 struct lde_map *me;
1191
1192 RB_FOREACH(f, fec_tree, &ft) {
1193 fn = (struct fec_node *)f;
1194 me = (struct lde_map *)fec_find(&ln->sent_map, &fn->fec);
1195 if (lde_wildcard_apply(wcard, &fn->fec, me) == 0)
1196 continue;
1197
1198 lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw,
1199 &fn->fec);
1200 if (lw == NULL)
1201 lw = lde_wdraw_add(ln, fn);
1202 lw->label = map.label;
1203 }
1204 }
1205 }
1206
1207 void
1208 lde_send_labelwithdraw_wcard(struct lde_nbr *ln, uint32_t label)
1209 {
1210 struct map wcard;
1211
1212 memset(&wcard, 0, sizeof(wcard));
1213 wcard.type = MAP_TYPE_WILDCARD;
1214 wcard.label = label;
1215 lde_send_labelwithdraw(ln, NULL, &wcard, NULL);
1216 }
1217
1218 void
1219 lde_send_labelwithdraw_twcard_prefix(struct lde_nbr *ln, uint16_t af,
1220 uint32_t label)
1221 {
1222 struct map wcard;
1223
1224 memset(&wcard, 0, sizeof(wcard));
1225 wcard.type = MAP_TYPE_TYPED_WCARD;
1226 wcard.fec.twcard.type = MAP_TYPE_PREFIX;
1227 wcard.fec.twcard.u.prefix_af = af;
1228 wcard.label = label;
1229 lde_send_labelwithdraw(ln, NULL, &wcard, NULL);
1230 }
1231
1232 void
1233 lde_send_labelwithdraw_twcard_pwid(struct lde_nbr *ln, uint16_t pw_type,
1234 uint32_t label)
1235 {
1236 struct map wcard;
1237
1238 memset(&wcard, 0, sizeof(wcard));
1239 wcard.type = MAP_TYPE_TYPED_WCARD;
1240 wcard.fec.twcard.type = MAP_TYPE_PWID;
1241 wcard.fec.twcard.u.pw_type = pw_type;
1242 wcard.label = label;
1243 lde_send_labelwithdraw(ln, NULL, &wcard, NULL);
1244 }
1245
1246 void
1247 lde_send_labelwithdraw_pwid_wcard(struct lde_nbr *ln, uint16_t pw_type,
1248 uint32_t group_id)
1249 {
1250 struct map wcard;
1251
1252 memset(&wcard, 0, sizeof(wcard));
1253 wcard.type = MAP_TYPE_PWID;
1254 wcard.fec.pwid.type = pw_type;
1255 wcard.fec.pwid.group_id = group_id;
1256 /* we can not append a Label TLV when using PWid group wildcards. */
1257 wcard.label = NO_LABEL;
1258 lde_send_labelwithdraw(ln, NULL, &wcard, NULL);
1259 }
1260
1261 void
1262 lde_send_labelrelease(struct lde_nbr *ln, struct fec_node *fn,
1263 struct map *wcard, uint32_t label)
1264 {
1265 struct map map;
1266 struct l2vpn_pw *pw;
1267
1268 if (fn) {
1269 lde_fec2map(&fn->fec, &map);
1270 switch (fn->fec.type) {
1271 case FEC_TYPE_IPV4:
1272 if (!ln->v4_enabled)
1273 return;
1274 break;
1275 case FEC_TYPE_IPV6:
1276 if (!ln->v6_enabled)
1277 return;
1278 break;
1279 case FEC_TYPE_PWID:
1280 pw = (struct l2vpn_pw *) fn->data;
1281 if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr)
1282 /* not the remote end of the pseudowire */
1283 return;
1284
1285 if (pw->flags & F_PW_CWORD)
1286 map.flags |= F_MAP_PW_CWORD;
1287 break;
1288 }
1289 } else
1290 memcpy(&map, wcard, sizeof(map));
1291 map.label = label;
1292
1293 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD, ln->peerid, 0,
1294 &map, sizeof(map));
1295 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD_END, ln->peerid, 0, NULL, 0);
1296 }
1297
1298 void
1299 lde_send_labelrequest(struct lde_nbr *ln, struct fec_node *fn,
1300 struct map *wcard, int single)
1301 {
1302 struct map map;
1303 struct fec *f;
1304 struct lde_req *lre;
1305
1306 if (fn) {
1307 lde_fec2map(&fn->fec, &map);
1308 switch (fn->fec.type) {
1309 case FEC_TYPE_IPV4:
1310 if (!ln->v4_enabled)
1311 return;
1312 break;
1313 case FEC_TYPE_IPV6:
1314 if (!ln->v6_enabled)
1315 return;
1316 break;
1317 default:
1318 fatalx("lde_send_labelrequest: unknown af");
1319 }
1320 } else
1321 memcpy(&map, wcard, sizeof(map));
1322
1323 map.label = NO_LABEL;
1324
1325 if (fn) {
1326 /* SLR1.1: has label request for FEC been previously sent
1327 * and still outstanding just return,
1328 */
1329 lre = (struct lde_req *)fec_find(&ln->sent_req, &fn->fec);
1330 if (lre == NULL) {
1331 /* SLRq.3: send label request */
1332 lde_imsg_compose_ldpe(IMSG_REQUEST_ADD, ln->peerid, 0,
1333 &map, sizeof(map));
1334 if (single)
1335 lde_imsg_compose_ldpe(IMSG_REQUEST_ADD_END,
1336 ln->peerid, 0, NULL, 0);
1337
1338 /* SLRq.4: record sent request */
1339 lde_req_add(ln, &fn->fec, 1);
1340 }
1341 } else {
1342 /* if Wilcard just send label request */
1343 /* SLRq.3: send label request */
1344 lde_imsg_compose_ldpe(IMSG_REQUEST_ADD,
1345 ln->peerid, 0, &map, sizeof(map));
1346 if (single)
1347 lde_imsg_compose_ldpe(IMSG_REQUEST_ADD_END,
1348 ln->peerid, 0, NULL, 0);
1349
1350 /* SLRq.4: record sent request */
1351 RB_FOREACH(f, fec_tree, &ft) {
1352 fn = (struct fec_node *)f;
1353 lre = (struct lde_req *)fec_find(&ln->sent_req, &fn->fec);
1354 if (lde_wildcard_apply(wcard, &fn->fec, NULL) == 0)
1355 continue;
1356 if (lre == NULL)
1357 lde_req_add(ln, f, 1);
1358 }
1359 }
1360 }
1361
1362 void
1363 lde_send_labelrequest_wcard(struct lde_nbr *ln, uint16_t af)
1364 {
1365 struct map wcard;
1366
1367 memset(&wcard, 0, sizeof(wcard));
1368 wcard.type = MAP_TYPE_TYPED_WCARD;
1369 wcard.fec.twcard.type = MAP_TYPE_PREFIX;
1370 wcard.fec.twcard.u.prefix_af = af;
1371 lde_send_labelrequest(ln, NULL, &wcard, 1);
1372 }
1373
1374 void
1375 lde_send_notification(struct lde_nbr *ln, uint32_t status_code, uint32_t msg_id,
1376 uint16_t msg_type)
1377 {
1378 struct notify_msg nm;
1379
1380 memset(&nm, 0, sizeof(nm));
1381 nm.status_code = status_code;
1382 /* 'msg_id' and 'msg_type' should be in network byte order */
1383 nm.msg_id = msg_id;
1384 nm.msg_type = msg_type;
1385
1386 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0,
1387 &nm, sizeof(nm));
1388 }
1389
1390 void
1391 lde_send_notification_eol_prefix(struct lde_nbr *ln, int af)
1392 {
1393 struct notify_msg nm;
1394
1395 memset(&nm, 0, sizeof(nm));
1396 nm.status_code = S_ENDOFLIB;
1397 nm.fec.type = MAP_TYPE_TYPED_WCARD;
1398 nm.fec.fec.twcard.type = MAP_TYPE_PREFIX;
1399 nm.fec.fec.twcard.u.prefix_af = af;
1400 nm.flags |= F_NOTIF_FEC;
1401
1402 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0,
1403 &nm, sizeof(nm));
1404 }
1405
1406 void
1407 lde_send_notification_eol_pwid(struct lde_nbr *ln, uint16_t pw_type)
1408 {
1409 struct notify_msg nm;
1410
1411 memset(&nm, 0, sizeof(nm));
1412 nm.status_code = S_ENDOFLIB;
1413 nm.fec.type = MAP_TYPE_TYPED_WCARD;
1414 nm.fec.fec.twcard.type = MAP_TYPE_PWID;
1415 nm.fec.fec.twcard.u.pw_type = pw_type;
1416 nm.flags |= F_NOTIF_FEC;
1417
1418 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0,
1419 &nm, sizeof(nm));
1420 }
1421
1422 static __inline int
1423 lde_nbr_compare(const struct lde_nbr *a, const struct lde_nbr *b)
1424 {
1425 return (a->peerid - b->peerid);
1426 }
1427
1428 static struct lde_nbr *
1429 lde_nbr_new(uint32_t peerid, struct lde_nbr *new)
1430 {
1431 struct lde_nbr *ln;
1432
1433 if ((ln = calloc(1, sizeof(*ln))) == NULL)
1434 fatal(__func__);
1435
1436 ln->id = new->id;
1437 ln->v4_enabled = new->v4_enabled;
1438 ln->v6_enabled = new->v6_enabled;
1439 ln->flags = new->flags;
1440 ln->peerid = peerid;
1441 fec_init(&ln->recv_map);
1442 fec_init(&ln->sent_map);
1443 fec_init(&ln->sent_map_pending);
1444 fec_init(&ln->recv_req);
1445 fec_init(&ln->sent_req);
1446 fec_init(&ln->sent_wdraw);
1447
1448 TAILQ_INIT(&ln->addr_list);
1449
1450 if (RB_INSERT(nbr_tree, &lde_nbrs, ln) != NULL)
1451 fatalx("lde_nbr_new: RB_INSERT failed");
1452
1453 return (ln);
1454 }
1455
1456 static void
1457 lde_nbr_del(struct lde_nbr *ln)
1458 {
1459 struct fec *f;
1460 struct fec_node *fn;
1461 struct fec_nh *fnh;
1462 struct l2vpn_pw *pw;
1463 struct lde_nbr *lnbr;
1464
1465 if (ln == NULL)
1466 return;
1467
1468 /* uninstall received mappings */
1469 RB_FOREACH(f, fec_tree, &ft) {
1470 fn = (struct fec_node *)f;
1471
1472 /* Update RLFA clients. */
1473 lde_rlfa_update_clients(f, ln, MPLS_INVALID_LABEL);
1474
1475 LIST_FOREACH(fnh, &fn->nexthops, entry) {
1476 switch (f->type) {
1477 case FEC_TYPE_IPV4:
1478 case FEC_TYPE_IPV6:
1479 if (!lde_address_find(ln, fnh->af,
1480 &fnh->nexthop))
1481 continue;
1482
1483 /*
1484 * Ordered Control: must mark any non-connected
1485 * NH to wait until we receive a labelmap msg
1486 * before installing in kernel and sending to
1487 * peer, must do this as NHs are not removed
1488 * when lsps go down. Also send label withdraw
1489 * to other neighbors for all fecs from neighbor
1490 * going down
1491 */
1492 if (ldeconf->flags & F_LDPD_ORDERED_CONTROL) {
1493 fnh->flags |= F_FEC_NH_DEFER;
1494
1495 RB_FOREACH(lnbr, nbr_tree, &lde_nbrs) {
1496 if (ln->peerid == lnbr->peerid)
1497 continue;
1498 lde_send_labelwithdraw(lnbr, fn, NULL, NULL);
1499 }
1500 }
1501 break;
1502 case FEC_TYPE_PWID:
1503 if (f->u.pwid.lsr_id.s_addr != ln->id.s_addr)
1504 continue;
1505 pw = (struct l2vpn_pw *) fn->data;
1506 if (pw) {
1507 pw->reason = F_PW_NO_REMOTE_LABEL;
1508 l2vpn_pw_reset(pw);
1509 }
1510 break;
1511 default:
1512 break;
1513 }
1514
1515 lde_send_delete_klabel(fn, fnh);
1516 fnh->remote_label = NO_LABEL;
1517 }
1518 }
1519
1520 lde_address_list_free(ln);
1521
1522 fec_clear(&ln->recv_map, lde_map_free);
1523 fec_clear(&ln->sent_map, lde_map_free);
1524 fec_clear(&ln->sent_map_pending, free);
1525 fec_clear(&ln->recv_req, free);
1526 fec_clear(&ln->sent_req, free);
1527 fec_clear(&ln->sent_wdraw, free);
1528
1529 RB_REMOVE(nbr_tree, &lde_nbrs, ln);
1530
1531 free(ln);
1532 }
1533
1534 static struct lde_nbr *
1535 lde_nbr_find(uint32_t peerid)
1536 {
1537 struct lde_nbr ln;
1538
1539 ln.peerid = peerid;
1540
1541 return (RB_FIND(nbr_tree, &lde_nbrs, &ln));
1542 }
1543
1544 struct lde_nbr *
1545 lde_nbr_find_by_lsrid(struct in_addr addr)
1546 {
1547 struct lde_nbr *ln;
1548
1549 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1550 if (ln->id.s_addr == addr.s_addr)
1551 return (ln);
1552
1553 return (NULL);
1554 }
1555
1556 struct lde_nbr *
1557 lde_nbr_find_by_addr(int af, union ldpd_addr *addr)
1558 {
1559 struct lde_nbr *ln;
1560
1561 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1562 if (lde_address_find(ln, af, addr) != NULL)
1563 return (ln);
1564
1565 return (NULL);
1566 }
1567
1568 static void
1569 lde_nbr_clear(void)
1570 {
1571 struct lde_nbr *ln;
1572
1573 while (!RB_EMPTY(nbr_tree, &lde_nbrs)) {
1574 ln = RB_ROOT(nbr_tree, &lde_nbrs);
1575
1576 lde_nbr_del(ln);
1577 }
1578 }
1579
1580 static void
1581 lde_nbr_addr_update(struct lde_nbr *ln, struct lde_addr *lde_addr, int removed)
1582 {
1583 struct fec *fec;
1584 struct fec_node *fn;
1585 struct fec_nh *fnh;
1586 struct lde_map *me;
1587
1588 RB_FOREACH(fec, fec_tree, &ln->recv_map) {
1589 switch (fec->type) {
1590 case FEC_TYPE_IPV4:
1591 if (lde_addr->af != AF_INET)
1592 continue;
1593 break;
1594 case FEC_TYPE_IPV6:
1595 if (lde_addr->af != AF_INET6)
1596 continue;
1597 break;
1598 default:
1599 continue;
1600 }
1601
1602 fn = (struct fec_node *)fec_find(&ft, fec);
1603 if (fn == NULL)
1604 /* shouldn't happen */
1605 continue;
1606
1607 LIST_FOREACH(fnh, &fn->nexthops, entry) {
1608 if (ldp_addrcmp(fnh->af, &fnh->nexthop,
1609 &lde_addr->addr))
1610 continue;
1611
1612 if (removed) {
1613 lde_send_delete_klabel(fn, fnh);
1614 fnh->remote_label = NO_LABEL;
1615 } else {
1616 me = (struct lde_map *)fec;
1617 fnh->remote_label = me->map.label;
1618 lde_send_change_klabel(fn, fnh);
1619 }
1620 break;
1621 }
1622 }
1623 }
1624
1625 void
1626 lde_allow_broken_lsp_update(int new_config)
1627 {
1628 struct fec_node *fn;
1629 struct fec_nh *fnh;
1630 struct fec *f;
1631
1632 RB_FOREACH(f, fec_tree, &ft) {
1633 fn = (struct fec_node *)f;
1634
1635 LIST_FOREACH(fnh, &fn->nexthops, entry) {
1636 /* allow-broken-lsp config is changing so
1637 * we need to reprogram labeled routes to
1638 * have proper top-level label
1639 */
1640 if (!(new_config & F_LDPD_ALLOW_BROKEN_LSP))
1641 lde_send_delete_klabel(fn, fnh);
1642
1643 if (fn->local_label != NO_LABEL)
1644 lde_send_change_klabel(fn, fnh);
1645 }
1646 }
1647 }
1648
1649 static __inline int
1650 lde_map_compare(const struct lde_map *a, const struct lde_map *b)
1651 {
1652 return (ldp_addrcmp(AF_INET, (union ldpd_addr *)&a->nexthop->id,
1653 (union ldpd_addr *)&b->nexthop->id));
1654 }
1655
1656 struct lde_map *
1657 lde_map_add(struct lde_nbr *ln, struct fec_node *fn, int sent)
1658 {
1659 struct lde_map *me;
1660
1661 me = calloc(1, sizeof(*me));
1662 if (me == NULL)
1663 fatal(__func__);
1664
1665 me->fec = fn->fec;
1666 me->nexthop = ln;
1667
1668 if (sent) {
1669 RB_INSERT(lde_map_head, &fn->upstream, me);
1670 me->head = &fn->upstream;
1671 if (fec_insert(&ln->sent_map, &me->fec))
1672 log_warnx("failed to add %s to sent map",
1673 log_fec(&me->fec));
1674 /* XXX on failure more cleanup is needed */
1675 } else {
1676 RB_INSERT(lde_map_head, &fn->downstream, me);
1677 me->head = &fn->downstream;
1678 if (fec_insert(&ln->recv_map, &me->fec))
1679 log_warnx("failed to add %s to recv map",
1680 log_fec(&me->fec));
1681 }
1682
1683 return (me);
1684 }
1685
1686 void
1687 lde_map_del(struct lde_nbr *ln, struct lde_map *me, int sent)
1688 {
1689 if (sent)
1690 fec_remove(&ln->sent_map, &me->fec);
1691 else
1692 fec_remove(&ln->recv_map, &me->fec);
1693
1694 lde_map_free(me);
1695 }
1696
1697 static void
1698 lde_map_free(void *ptr)
1699 {
1700 struct lde_map *map = ptr;
1701
1702 RB_REMOVE(lde_map_head, map->head, map);
1703 free(map);
1704 }
1705
1706 struct fec *
1707 lde_map_pending_add(struct lde_nbr *ln, struct fec_node *fn)
1708 {
1709 struct fec *map;
1710
1711 map = calloc(1, sizeof(*map));
1712 if (map == NULL)
1713 fatal(__func__);
1714
1715 *map = fn->fec;
1716 if (fec_insert(&ln->sent_map_pending, map))
1717 log_warnx("failed to add %s to sent map (pending)",
1718 log_fec(map));
1719
1720 return (map);
1721 }
1722
1723 void
1724 lde_map_pending_del(struct lde_nbr *ln, struct fec *map)
1725 {
1726 fec_remove(&ln->sent_map_pending, map);
1727 free(map);
1728 }
1729
1730 struct lde_req *
1731 lde_req_add(struct lde_nbr *ln, struct fec *fec, int sent)
1732 {
1733 struct fec_tree *t;
1734 struct lde_req *lre;
1735
1736 t = sent ? &ln->sent_req : &ln->recv_req;
1737
1738 lre = calloc(1, sizeof(*lre));
1739 if (lre != NULL) {
1740 lre->fec = *fec;
1741
1742 if (fec_insert(t, &lre->fec)) {
1743 log_warnx("failed to add %s to %s req",
1744 log_fec(&lre->fec), sent ? "sent" : "recv");
1745 free(lre);
1746 return (NULL);
1747 }
1748 }
1749
1750 return (lre);
1751 }
1752
1753 void
1754 lde_req_del(struct lde_nbr *ln, struct lde_req *lre, int sent)
1755 {
1756 if (sent)
1757 fec_remove(&ln->sent_req, &lre->fec);
1758 else
1759 fec_remove(&ln->recv_req, &lre->fec);
1760
1761 free(lre);
1762 }
1763
1764 struct lde_wdraw *
1765 lde_wdraw_add(struct lde_nbr *ln, struct fec_node *fn)
1766 {
1767 struct lde_wdraw *lw;
1768
1769 lw = calloc(1, sizeof(*lw));
1770 if (lw == NULL)
1771 fatal(__func__);
1772
1773 lw->fec = fn->fec;
1774
1775 if (fec_insert(&ln->sent_wdraw, &lw->fec))
1776 log_warnx("failed to add %s to sent wdraw",
1777 log_fec(&lw->fec));
1778
1779 return (lw);
1780 }
1781
1782 void
1783 lde_wdraw_del(struct lde_nbr *ln, struct lde_wdraw *lw)
1784 {
1785 fec_remove(&ln->sent_wdraw, &lw->fec);
1786 free(lw);
1787 }
1788
1789 void
1790 lde_change_egress_label(int af)
1791 {
1792 struct lde_nbr *ln;
1793 struct fec *f;
1794 struct fec_node *fn;
1795
1796 /* explicitly withdraw all null labels */
1797 RB_FOREACH(ln, nbr_tree, &lde_nbrs) {
1798 lde_send_labelwithdraw_wcard(ln, MPLS_LABEL_IMPLICIT_NULL);
1799 if (ln->v4_enabled)
1800 lde_send_labelwithdraw_wcard(
1801 ln,
1802 MPLS_LABEL_IPV4_EXPLICIT_NULL);
1803 if (ln->v6_enabled)
1804 lde_send_labelwithdraw_wcard(
1805 ln,
1806 MPLS_LABEL_IPV6_EXPLICIT_NULL);
1807 }
1808
1809 /* update label of connected routes */
1810 RB_FOREACH(f, fec_tree, &ft) {
1811 fn = (struct fec_node *)f;
1812 if (fn->local_label > MPLS_LABEL_RESERVED_MAX)
1813 continue;
1814
1815 switch (af) {
1816 case AF_INET:
1817 if (fn->fec.type != FEC_TYPE_IPV4)
1818 continue;
1819 break;
1820 case AF_INET6:
1821 if (fn->fec.type != FEC_TYPE_IPV6)
1822 continue;
1823 break;
1824 default:
1825 fatalx("lde_change_egress_label: unknown af");
1826 }
1827
1828 fn->local_label = lde_update_label(fn);
1829 if (fn->local_label != NO_LABEL)
1830 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1831 lde_send_labelmapping(ln, fn, 0);
1832 }
1833 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1834 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0,
1835 NULL, 0);
1836 }
1837
1838 void
1839 lde_change_allocate_filter(int af)
1840 {
1841 struct lde_nbr *ln;
1842 struct fec *f;
1843 struct fec_node *fn;
1844 uint32_t new_label;
1845
1846 /* reallocate labels for fecs that match this filter */
1847 RB_FOREACH(f, fec_tree, &ft) {
1848 fn = (struct fec_node *)f;
1849
1850 switch (af) {
1851 case AF_INET:
1852 if (fn->fec.type != FEC_TYPE_IPV4)
1853 continue;
1854 break;
1855 case AF_INET6:
1856 if (fn->fec.type != FEC_TYPE_IPV6)
1857 continue;
1858 break;
1859 default:
1860 fatalx("lde_change_allocate_filter: unknown af");
1861 }
1862
1863 /*
1864 * If the local label has changed to NO_LABEL, send a label
1865 * withdraw to all peers.
1866 * If the local label has changed and it's different from
1867 * NO_LABEL, send a label mapping to all peers advertising
1868 * the new label.
1869 * If the local label hasn't changed, do nothing
1870 */
1871 new_label = lde_update_label(fn);
1872 if (fn->local_label != new_label) {
1873 if (new_label == NO_LABEL)
1874 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1875 lde_send_labelwithdraw(ln, fn,
1876 NULL, NULL);
1877
1878 fn->local_label = new_label;
1879 if (fn->local_label != NO_LABEL)
1880 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1881 lde_send_labelmapping(ln, fn, 0);
1882 }
1883 }
1884 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1885 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0,
1886 NULL, 0);
1887 }
1888
1889 void
1890 lde_change_advertise_filter(int af)
1891 {
1892 struct lde_nbr *ln;
1893 struct fec *f;
1894 struct fec_node *fn;
1895 char *acl_to_filter;
1896 char *acl_for_filter;
1897 union ldpd_addr *prefix;
1898 uint8_t plen;
1899 struct lde_map *me;
1900
1901 /* advertise label for fecs to neighbors if matches advertise filters */
1902 switch (af) {
1903 case AF_INET:
1904 acl_to_filter = ldeconf->ipv4.acl_label_advertise_to;
1905 acl_for_filter = ldeconf->ipv4.acl_label_advertise_for;
1906 break;
1907 case AF_INET6:
1908 acl_to_filter = ldeconf->ipv6.acl_label_advertise_to;
1909 acl_for_filter = ldeconf->ipv6.acl_label_advertise_for;
1910 break;
1911 default:
1912 fatalx("lde_change_advertise_filter: unknown af");
1913 }
1914
1915 RB_FOREACH(ln, nbr_tree, &lde_nbrs) {
1916 if (lde_acl_check(acl_to_filter, af, (union ldpd_addr *)&ln->id,
1917 IPV4_MAX_BITLEN) != FILTER_PERMIT)
1918 lde_send_labelwithdraw_wcard(ln, NO_LABEL);
1919 else {
1920 /* This neighbor is allowed in to_filter, so
1921 * send labels if fec also matches for_filter
1922 */
1923 RB_FOREACH(f, fec_tree, &ft) {
1924 fn = (struct fec_node *)f;
1925 switch (af) {
1926 case AF_INET:
1927 if (fn->fec.type != FEC_TYPE_IPV4)
1928 continue;
1929 prefix = (union ldpd_addr *)
1930 &fn->fec.u.ipv4.prefix;
1931 plen = fn->fec.u.ipv4.prefixlen;
1932 break;
1933 case FEC_TYPE_IPV6:
1934 if (fn->fec.type != FEC_TYPE_IPV6)
1935 continue;
1936 prefix = (union ldpd_addr *)
1937 &fn->fec.u.ipv6.prefix;
1938 plen = fn->fec.u.ipv6.prefixlen;
1939 break;
1940 default:
1941 continue;
1942 }
1943 if (lde_acl_check(acl_for_filter, af,
1944 prefix, plen) != FILTER_PERMIT) {
1945 me = (struct lde_map *)fec_find(
1946 &ln->sent_map, &fn->fec);
1947 if (me)
1948 /* fec filtered withdraw */
1949 lde_send_labelwithdraw(ln, fn,
1950 NULL, NULL);
1951 } else
1952 /* fec allowed send map */
1953 lde_send_labelmapping(ln, fn, 0);
1954 }
1955 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END,
1956 ln->peerid, 0, NULL, 0);
1957 }
1958 }
1959 }
1960
1961
1962 void
1963 lde_change_accept_filter(int af)
1964 {
1965 struct lde_nbr *ln;
1966 struct fec *f;
1967 struct fec_node *fn;
1968 char *acl_for_filter;
1969 char *acl_from_filter;
1970 union ldpd_addr *prefix;
1971 uint8_t plen;
1972 struct lde_map *me;
1973 enum fec_type type;
1974
1975 /* accept labels from neighbors specified in the from_filter and for
1976 * fecs defined in the for_filter
1977 */
1978 switch (af) {
1979 case AF_INET:
1980 acl_for_filter = ldeconf->ipv4.acl_label_accept_for;
1981 acl_from_filter = ldeconf->ipv4.acl_label_accept_from;
1982 type = FEC_TYPE_IPV4;
1983 break;
1984 case AF_INET6:
1985 acl_for_filter = ldeconf->ipv6.acl_label_accept_for;
1986 acl_from_filter = ldeconf->ipv6.acl_label_accept_from;
1987 type = FEC_TYPE_IPV6;
1988 break;
1989 default:
1990 fatalx("lde_change_accept_filter: unknown af");
1991 }
1992
1993 RB_FOREACH(ln, nbr_tree, &lde_nbrs) {
1994 if (lde_acl_check(acl_from_filter, AF_INET, (union ldpd_addr *)
1995 &ln->id, IPV4_MAX_BITLEN) != FILTER_PERMIT) {
1996 /* This neighbor is now filtered so remove fecs from
1997 * recv list
1998 */
1999 RB_FOREACH(f, fec_tree, &ft) {
2000 fn = (struct fec_node *)f;
2001 if (fn->fec.type == type) {
2002 me = (struct lde_map *)fec_find(
2003 &ln->recv_map, &fn->fec);
2004 if (me)
2005 lde_map_del(ln, me, 0);
2006 }
2007 }
2008 } else if (ln->flags & F_NBR_CAP_TWCARD) {
2009 /* This neighbor is allowed and supports type
2010 * wildcard so send a labelrequest
2011 * to get any new labels from neighbor
2012 * and make sure any fecs we currently have
2013 * match for_filter.
2014 */
2015 RB_FOREACH(f, fec_tree, &ft) {
2016 fn = (struct fec_node *)f;
2017 switch (af) {
2018 case AF_INET:
2019 if (fn->fec.type != FEC_TYPE_IPV4)
2020 continue;
2021 prefix = (union ldpd_addr *)
2022 &fn->fec.u.ipv4.prefix;
2023 plen = fn->fec.u.ipv4.prefixlen;
2024 break;
2025 case AF_INET6:
2026 if (fn->fec.type != FEC_TYPE_IPV6)
2027 continue;
2028 prefix = (union ldpd_addr *)
2029 &fn->fec.u.ipv6.prefix;
2030 plen = fn->fec.u.ipv6.prefixlen;
2031 break;
2032 default:
2033 continue;
2034 }
2035 if (lde_acl_check(acl_for_filter, af,
2036 prefix, plen) != FILTER_PERMIT) {
2037 me = (struct lde_map *)fec_find(
2038 &ln->recv_map, &fn->fec);
2039 if (me)
2040 lde_map_del(ln, me, 0);
2041 }
2042 }
2043 lde_send_labelrequest_wcard(ln, af);
2044 } else
2045 /* Type Wildcard is not supported so restart session */
2046 lde_imsg_compose_ldpe(IMSG_NBR_SHUTDOWN, ln->peerid, 0,
2047 NULL, 0);
2048 }
2049 }
2050
2051 void
2052 lde_change_expnull_for_filter(int af)
2053 {
2054 struct lde_nbr *ln;
2055 struct fec *f;
2056 struct fec_node *fn;
2057 char *acl_name;
2058 uint32_t exp_label;
2059 union ldpd_addr *prefix;
2060 uint8_t plen;
2061
2062 /* Configure explicit-null advertisement for all fecs in this filter */
2063 RB_FOREACH(f, fec_tree, &ft) {
2064 fn = (struct fec_node *)f;
2065
2066 switch (af) {
2067 case AF_INET:
2068 if (fn->fec.type != FEC_TYPE_IPV4)
2069 continue;
2070 acl_name = ldeconf->ipv4.acl_label_expnull_for;
2071 prefix = (union ldpd_addr *)&fn->fec.u.ipv4.prefix;
2072 plen = fn->fec.u.ipv4.prefixlen;
2073 exp_label = MPLS_LABEL_IPV4_EXPLICIT_NULL;
2074 break;
2075 case AF_INET6:
2076 if (fn->fec.type != FEC_TYPE_IPV6)
2077 continue;
2078 acl_name = ldeconf->ipv6.acl_label_expnull_for;
2079 prefix = (union ldpd_addr *)&fn->fec.u.ipv6.prefix;
2080 plen = fn->fec.u.ipv6.prefixlen;
2081 exp_label = MPLS_LABEL_IPV6_EXPLICIT_NULL;
2082 break;
2083 default:
2084 fatalx("lde_change_expnull_for_filter: unknown af");
2085 }
2086
2087 if (lde_acl_check(acl_name, af, prefix, plen) == FILTER_PERMIT) {
2088 /* for this fec change any imp-null to exp-null */
2089 if (fn->local_label == MPLS_LABEL_IMPLICIT_NULL) {
2090 fn->local_label= lde_update_label(fn);
2091 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
2092 lde_send_labelmapping(ln, fn, 0);
2093 }
2094 } else {
2095 /* for this fec change any exp-null back to imp-null */
2096 if (fn->local_label == exp_label) {
2097 fn->local_label = lde_update_label(fn);
2098 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
2099 lde_send_labelmapping(ln, fn, 0);
2100 }
2101 }
2102 }
2103 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
2104 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0,
2105 NULL, 0);
2106 }
2107
2108 static int
2109 lde_address_add(struct lde_nbr *ln, struct lde_addr *lde_addr)
2110 {
2111 struct lde_addr *new;
2112
2113 if (lde_address_find(ln, lde_addr->af, &lde_addr->addr) != NULL)
2114 return (-1);
2115
2116 if ((new = calloc(1, sizeof(*new))) == NULL)
2117 fatal(__func__);
2118
2119 new->af = lde_addr->af;
2120 new->addr = lde_addr->addr;
2121 TAILQ_INSERT_TAIL(&ln->addr_list, new, entry);
2122
2123 /* reevaluate the previously received mappings from this neighbor */
2124 lde_nbr_addr_update(ln, lde_addr, 0);
2125
2126 return (0);
2127 }
2128
2129 static int
2130 lde_address_del(struct lde_nbr *ln, struct lde_addr *lde_addr)
2131 {
2132 lde_addr = lde_address_find(ln, lde_addr->af, &lde_addr->addr);
2133 if (lde_addr == NULL)
2134 return (-1);
2135
2136 /* reevaluate the previously received mappings from this neighbor */
2137 lde_nbr_addr_update(ln, lde_addr, 1);
2138
2139 TAILQ_REMOVE(&ln->addr_list, lde_addr, entry);
2140 free(lde_addr);
2141
2142 return (0);
2143 }
2144
2145 struct lde_addr *
2146 lde_address_find(struct lde_nbr *ln, int af, union ldpd_addr *addr)
2147 {
2148 struct lde_addr *lde_addr;
2149
2150 TAILQ_FOREACH(lde_addr, &ln->addr_list, entry)
2151 if (lde_addr->af == af &&
2152 ldp_addrcmp(af, &lde_addr->addr, addr) == 0)
2153 return (lde_addr);
2154
2155 return (NULL);
2156 }
2157
2158 static void
2159 lde_address_list_free(struct lde_nbr *ln)
2160 {
2161 struct lde_addr *lde_addr;
2162
2163 while ((lde_addr = TAILQ_POP_FIRST(&ln->addr_list, entry)) != NULL)
2164 free(lde_addr);
2165 }
2166
2167 /*
2168 * Event callback used to retry the label-manager sync zapi session.
2169 */
2170 static void zclient_sync_retry(struct thread *thread)
2171 {
2172 zclient_sync_init();
2173 }
2174
2175 /*
2176 * Initialize and open a synchronous zapi session. This is used by label chunk
2177 * management code, which acquires and releases blocks of labels from the
2178 * zebra label-manager module.
2179 */
2180 static void zclient_sync_init(void)
2181 {
2182 struct zclient_options options = zclient_options_default;
2183
2184 options.synchronous = true;
2185
2186 /* Initialize special zclient for synchronous message exchanges. */
2187 zclient_sync = zclient_new(master, &options, NULL, 0);
2188 zclient_sync->sock = -1;
2189 zclient_sync->redist_default = ZEBRA_ROUTE_LDP;
2190 zclient_sync->session_id = 1; /* Distinguish from main session */
2191 zclient_sync->privs = &lde_privs;
2192
2193 if (zclient_socket_connect(zclient_sync) < 0) {
2194 log_warnx("Error connecting synchronous zclient!");
2195 goto retry;
2196 }
2197 /* make socket non-blocking */
2198 sock_set_nonblock(zclient_sync->sock);
2199
2200 /* Send hello to notify zebra this is a synchronous client */
2201 if (zclient_send_hello(zclient_sync) == ZCLIENT_SEND_FAILURE) {
2202 log_warnx("Error sending hello for synchronous zclient!");
2203 goto retry;
2204 }
2205
2206 /* Connect to label manager */
2207 if (lm_label_manager_connect(zclient_sync, 0) != 0) {
2208 log_warnx("Error connecting to label manager!");
2209 goto retry;
2210 }
2211
2212 /* Finish label-manager init once the LM session is running */
2213 lde_label_list_init();
2214
2215 return;
2216
2217 retry:
2218
2219 /* Discard failed zclient object */
2220 zclient_stop(zclient_sync);
2221 zclient_free(zclient_sync);
2222 zclient_sync = NULL;
2223
2224 /* Retry using a timer */
2225 thread_add_timer(master, zclient_sync_retry, NULL, 1, NULL);
2226 }
2227
2228 static void
2229 lde_del_label_chunk(void *val)
2230 {
2231 free(val);
2232 }
2233
2234 static int
2235 lde_release_label_chunk(uint32_t start, uint32_t end)
2236 {
2237 int ret;
2238
2239 ret = lm_release_label_chunk(zclient_sync, start, end);
2240 if (ret < 0) {
2241 log_warnx("Error releasing label chunk!");
2242 return (-1);
2243 }
2244 return (0);
2245 }
2246
2247 static int
2248 lde_get_label_chunk(void)
2249 {
2250 int ret;
2251 uint32_t start, end;
2252
2253 debug_labels("getting label chunk (size %u)", CHUNK_SIZE);
2254 ret = lm_get_label_chunk(zclient_sync, 0, MPLS_LABEL_BASE_ANY,
2255 CHUNK_SIZE, &start, &end);
2256 if (ret < 0) {
2257 log_warnx("Error getting label chunk!");
2258 return -1;
2259 }
2260
2261 on_get_label_chunk_response(start, end);
2262
2263 return (0);
2264 }
2265
2266 static void
2267 lde_label_list_init(void)
2268 {
2269 label_chunk_list = list_new();
2270 label_chunk_list->del = lde_del_label_chunk;
2271
2272 /* get first chunk */
2273 while (lde_get_label_chunk () != 0) {
2274 log_warnx("Error getting first label chunk!");
2275 sleep(1);
2276 }
2277 }
2278
2279 static void
2280 on_get_label_chunk_response(uint32_t start, uint32_t end)
2281 {
2282 struct label_chunk *new_label_chunk;
2283
2284 debug_labels("label chunk assign: %u - %u", start, end);
2285
2286 new_label_chunk = calloc(1, sizeof(struct label_chunk));
2287 if (!new_label_chunk) {
2288 log_warn("Error trying to allocate label chunk %u - %u", start, end);
2289 return;
2290 }
2291
2292 new_label_chunk->start = start;
2293 new_label_chunk->end = end;
2294 new_label_chunk->used_mask = 0;
2295
2296 listnode_add(label_chunk_list, (void *)new_label_chunk);
2297
2298 /* let's update current if needed */
2299 if (!current_label_chunk)
2300 current_label_chunk = listtail(label_chunk_list);
2301 }
2302
2303 void
2304 lde_free_label(uint32_t label)
2305 {
2306 struct listnode *node;
2307 struct label_chunk *label_chunk;
2308 uint64_t pos;
2309
2310 for (ALL_LIST_ELEMENTS_RO(label_chunk_list, node, label_chunk)) {
2311 if (label <= label_chunk->end && label >= label_chunk->start) {
2312 pos = 1ULL << (label - label_chunk->start);
2313 label_chunk->used_mask &= ~pos;
2314 /* if nobody is using this chunk and it's not current_label_chunk, then free it */
2315 if (!label_chunk->used_mask && (current_label_chunk != node)) {
2316 if (lde_release_label_chunk(label_chunk->start, label_chunk->end) != 0)
2317 log_warnx("%s: Error releasing label chunk!", __func__);
2318 else {
2319 listnode_delete(label_chunk_list, label_chunk);
2320 lde_del_label_chunk(label_chunk);
2321 }
2322 }
2323 break;
2324 }
2325 }
2326 return;
2327 }
2328
2329 static uint32_t
2330 lde_get_next_label(void)
2331 {
2332 struct label_chunk *label_chunk;
2333 uint32_t i, size;
2334 uint64_t pos;
2335 uint32_t label = NO_LABEL;
2336
2337 while (current_label_chunk) {
2338 label_chunk = listgetdata(current_label_chunk);
2339 if (!label_chunk)
2340 goto end;
2341
2342 /* try to get next free label in currently used label chunk */
2343 size = label_chunk->end - label_chunk->start + 1;
2344 for (i = 0, pos = 1; i < size; i++, pos <<= 1) {
2345 if (!(pos & label_chunk->used_mask)) {
2346 label_chunk->used_mask |= pos;
2347 label = label_chunk->start + i;
2348 goto end;
2349 }
2350 }
2351 current_label_chunk = listnextnode(current_label_chunk);
2352 }
2353
2354 end:
2355 /* we moved till the last chunk, or were not able to find a label,
2356 so let's ask for another one */
2357 if (!current_label_chunk ||
2358 current_label_chunk == listtail(label_chunk_list) ||
2359 label == NO_LABEL) {
2360 if (lde_get_label_chunk() != 0)
2361 log_warn("%s: Error getting label chunk!", __func__);
2362
2363 }
2364
2365 return (label);
2366 }
2367
2368 static void
2369 lde_check_filter_af(int af, struct ldpd_af_conf *af_conf,
2370 const char *filter_name)
2371 {
2372 if (strcmp(af_conf->acl_label_allocate_for, filter_name) == 0)
2373 lde_change_allocate_filter(af);
2374 if ((strcmp(af_conf->acl_label_advertise_to, filter_name) == 0)
2375 || (strcmp(af_conf->acl_label_advertise_for, filter_name) == 0))
2376 lde_change_advertise_filter(af);
2377 if ((strcmp(af_conf->acl_label_accept_for, filter_name) == 0)
2378 || (strcmp(af_conf->acl_label_accept_from, filter_name) == 0))
2379 lde_change_accept_filter(af);
2380 if (strcmp(af_conf->acl_label_expnull_for, filter_name) == 0)
2381 lde_change_expnull_for_filter(af);
2382 }
2383
2384 void lde_route_update(struct iface *iface, int af)
2385 {
2386 struct fec *f;
2387 struct fec_node *fn;
2388 struct fec_nh *fnh;
2389 struct lde_nbr *ln;
2390
2391 /* update label of non-connected routes */
2392 log_debug("update labels for interface %s", iface->name);
2393 RB_FOREACH(f, fec_tree, &ft) {
2394 fn = (struct fec_node *)f;
2395 if (IS_MPLS_UNRESERVED_LABEL(fn->local_label))
2396 continue;
2397
2398 switch (af) {
2399 case AF_INET:
2400 if (fn->fec.type != FEC_TYPE_IPV4)
2401 continue;
2402 break;
2403 case AF_INET6:
2404 if (fn->fec.type != FEC_TYPE_IPV6)
2405 continue;
2406 break;
2407 default:
2408 /* unspecified so process both address families */
2409 break;
2410 }
2411
2412 LIST_FOREACH(fnh, &fn->nexthops, entry) {
2413 /*
2414 * If connected leave existing label. If LDP
2415 * configured on interface or a static route
2416 * may need new label. If no LDP configured
2417 * treat fec as a connected route
2418 */
2419 if (fnh->flags & F_FEC_NH_CONNECTED)
2420 break;
2421
2422 if (fnh->ifindex != iface->ifindex)
2423 continue;
2424
2425 fnh->flags &= ~F_FEC_NH_NO_LDP;
2426 if (IS_MPLS_RESERVED_LABEL(fn->local_label)) {
2427 fn->local_label = NO_LABEL;
2428 fn->local_label = lde_update_label(fn);
2429 if (fn->local_label != NO_LABEL)
2430 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
2431 lde_send_labelmapping(
2432 ln, fn, 0);
2433 }
2434 break;
2435 }
2436 }
2437 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
2438 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid,
2439 0, NULL, 0);
2440 }
2441
2442 void lde_route_update_release(struct iface *iface, int af)
2443 {
2444 struct lde_nbr *ln;
2445 struct fec *f;
2446 struct fec_node *fn;
2447 struct fec_nh *fnh;
2448
2449 /* update label of interfaces no longer running LDP */
2450 log_debug("release all labels for interface %s af %s", iface->name,
2451 af == AF_INET ? "ipv4" : "ipv6");
2452 RB_FOREACH(f, fec_tree, &ft) {
2453 fn = (struct fec_node *)f;
2454
2455 switch (af) {
2456 case AF_INET:
2457 if (fn->fec.type != FEC_TYPE_IPV4)
2458 continue;
2459 break;
2460 case AF_INET6:
2461 if (fn->fec.type != FEC_TYPE_IPV6)
2462 continue;
2463 break;
2464 default:
2465 fatalx("lde_route_update_release: unknown af");
2466 }
2467
2468 if (fn->local_label == NO_LABEL)
2469 continue;
2470
2471 LIST_FOREACH(fnh, &fn->nexthops, entry) {
2472 /*
2473 * If connected leave existing label. If LDP
2474 * removed from interface may need new label
2475 * and would be treated as a connected route
2476 */
2477 if (fnh->flags & F_FEC_NH_CONNECTED)
2478 break;
2479
2480 if (fnh->ifindex != iface->ifindex)
2481 continue;
2482
2483 fnh->flags |= F_FEC_NH_NO_LDP;
2484 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
2485 lde_send_labelwithdraw(ln, fn, NULL, NULL);
2486 lde_free_label(fn->local_label);
2487 fn->local_label = NO_LABEL;
2488 fn->local_label = lde_update_label(fn);
2489 if (fn->local_label != NO_LABEL)
2490 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
2491 lde_send_labelmapping(ln, fn, 0);
2492 break;
2493 }
2494 }
2495 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
2496 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid,
2497 0, NULL, 0);
2498 }
2499
2500 void lde_route_update_release_all(int af)
2501 {
2502 struct lde_nbr *ln;
2503 struct fec *f;
2504 struct fec_node *fn;
2505 struct fec_nh *fnh;
2506
2507 /* remove labels from all interfaces as LDP is no longer running for
2508 * this address family
2509 */
2510 log_debug("release all labels for address family %s",
2511 af == AF_INET ? "ipv4" : "ipv6");
2512 RB_FOREACH(f, fec_tree, &ft) {
2513 fn = (struct fec_node *)f;
2514 switch (af) {
2515 case AF_INET:
2516 if (fn->fec.type != FEC_TYPE_IPV4)
2517 continue;
2518 break;
2519 case AF_INET6:
2520 if (fn->fec.type != FEC_TYPE_IPV6)
2521 continue;
2522 break;
2523 default:
2524 fatalx("lde_route_update_release: unknown af");
2525 }
2526
2527 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
2528 lde_send_labelwithdraw(ln, fn, NULL, NULL);
2529
2530 LIST_FOREACH(fnh, &fn->nexthops, entry) {
2531 fnh->flags |= F_FEC_NH_NO_LDP;
2532 lde_send_delete_klabel(fn, fnh);
2533 }
2534 }
2535 }