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