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