]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/lde.c
Merge pull request #6517 from vishaldhingra/submodule
[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 break;
472 }
473
474 RB_FOREACH(l2vpn, l2vpn_head, &ldeconf->l2vpn_tree) {
475 lif = l2vpn_if_find(l2vpn, kif->ifname);
476 if (lif) {
477 l2vpn_if_update_info(lif, kif);
478 break;
479 }
480 pw = l2vpn_pw_find(l2vpn, kif->ifname);
481 if (pw) {
482 l2vpn_pw_update_info(pw, kif);
483 break;
484 }
485 }
486 break;
487 case IMSG_PW_UPDATE:
488 if (imsg.hdr.len != IMSG_HEADER_SIZE +
489 sizeof(struct zapi_pw_status))
490 fatalx("PW_UPDATE imsg with wrong len");
491
492 if (l2vpn_pw_status_update(imsg.data) != 0)
493 log_warnx("%s: error updating PW status",
494 __func__);
495 break;
496 case IMSG_NETWORK_ADD:
497 case IMSG_NETWORK_UPDATE:
498 if (imsg.hdr.len != IMSG_HEADER_SIZE +
499 sizeof(struct kroute)) {
500 log_warnx("%s: wrong imsg len", __func__);
501 break;
502 }
503 kr = imsg.data;
504
505 switch (kr->af) {
506 case AF_INET:
507 fec.type = FEC_TYPE_IPV4;
508 fec.u.ipv4.prefix = kr->prefix.v4;
509 fec.u.ipv4.prefixlen = kr->prefixlen;
510 break;
511 case AF_INET6:
512 fec.type = FEC_TYPE_IPV6;
513 fec.u.ipv6.prefix = kr->prefix.v6;
514 fec.u.ipv6.prefixlen = kr->prefixlen;
515 break;
516 default:
517 fatalx("lde_dispatch_parent: unknown af");
518 }
519
520 switch (imsg.hdr.type) {
521 case IMSG_NETWORK_ADD:
522 lde_kernel_insert(&fec, kr->af, &kr->nexthop,
523 kr->ifindex, kr->route_type,
524 kr->route_instance,
525 kr->flags & F_CONNECTED, NULL);
526 break;
527 case IMSG_NETWORK_UPDATE:
528 lde_kernel_update(&fec);
529 break;
530 }
531 break;
532 case IMSG_SOCKET_IPC:
533 if (iev_ldpe) {
534 log_warnx("%s: received unexpected imsg fd to ldpe", __func__);
535 break;
536 }
537 if ((fd = imsg.fd) == -1) {
538 log_warnx("%s: expected to receive imsg fd to ldpe but didn't receive any", __func__);
539 break;
540 }
541
542 if ((iev_ldpe = malloc(sizeof(struct imsgev))) == NULL)
543 fatal(NULL);
544 imsg_init(&iev_ldpe->ibuf, fd);
545 iev_ldpe->handler_read = lde_dispatch_imsg;
546 iev_ldpe->ev_read = NULL;
547 thread_add_read(master, iev_ldpe->handler_read, iev_ldpe, iev_ldpe->ibuf.fd,
548 &iev_ldpe->ev_read);
549 iev_ldpe->handler_write = ldp_write_handler;
550 iev_ldpe->ev_write = NULL;
551 break;
552 case IMSG_INIT:
553 if (imsg.hdr.len != IMSG_HEADER_SIZE +
554 sizeof(struct ldpd_init))
555 fatalx("INIT imsg with wrong len");
556
557 memcpy(&init, imsg.data, sizeof(init));
558 lde_init(&init);
559 break;
560 case IMSG_RECONF_CONF:
561 if ((nconf = malloc(sizeof(struct ldpd_conf))) ==
562 NULL)
563 fatal(NULL);
564 memcpy(nconf, imsg.data, sizeof(struct ldpd_conf));
565
566 RB_INIT(iface_head, &nconf->iface_tree);
567 RB_INIT(tnbr_head, &nconf->tnbr_tree);
568 RB_INIT(nbrp_head, &nconf->nbrp_tree);
569 RB_INIT(l2vpn_head, &nconf->l2vpn_tree);
570 break;
571 case IMSG_RECONF_IFACE:
572 if ((niface = malloc(sizeof(struct iface))) == NULL)
573 fatal(NULL);
574 memcpy(niface, imsg.data, sizeof(struct iface));
575
576 RB_INSERT(iface_head, &nconf->iface_tree, niface);
577 break;
578 case IMSG_RECONF_TNBR:
579 if ((ntnbr = malloc(sizeof(struct tnbr))) == NULL)
580 fatal(NULL);
581 memcpy(ntnbr, imsg.data, sizeof(struct tnbr));
582
583 RB_INSERT(tnbr_head, &nconf->tnbr_tree, ntnbr);
584 break;
585 case IMSG_RECONF_NBRP:
586 if ((nnbrp = malloc(sizeof(struct nbr_params))) == NULL)
587 fatal(NULL);
588 memcpy(nnbrp, imsg.data, sizeof(struct nbr_params));
589
590 RB_INSERT(nbrp_head, &nconf->nbrp_tree, nnbrp);
591 break;
592 case IMSG_RECONF_L2VPN:
593 if ((nl2vpn = malloc(sizeof(struct l2vpn))) == NULL)
594 fatal(NULL);
595 memcpy(nl2vpn, imsg.data, sizeof(struct l2vpn));
596
597 RB_INIT(l2vpn_if_head, &nl2vpn->if_tree);
598 RB_INIT(l2vpn_pw_head, &nl2vpn->pw_tree);
599 RB_INIT(l2vpn_pw_head, &nl2vpn->pw_inactive_tree);
600
601 RB_INSERT(l2vpn_head, &nconf->l2vpn_tree, nl2vpn);
602 break;
603 case IMSG_RECONF_L2VPN_IF:
604 if ((nlif = malloc(sizeof(struct l2vpn_if))) == NULL)
605 fatal(NULL);
606 memcpy(nlif, imsg.data, sizeof(struct l2vpn_if));
607
608 RB_INSERT(l2vpn_if_head, &nl2vpn->if_tree, nlif);
609 break;
610 case IMSG_RECONF_L2VPN_PW:
611 if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL)
612 fatal(NULL);
613 memcpy(npw, imsg.data, sizeof(struct l2vpn_pw));
614
615 RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_tree, npw);
616 break;
617 case IMSG_RECONF_L2VPN_IPW:
618 if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL)
619 fatal(NULL);
620 memcpy(npw, imsg.data, sizeof(struct l2vpn_pw));
621
622 RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_inactive_tree, npw);
623 break;
624 case IMSG_RECONF_END:
625 merge_config(ldeconf, nconf);
626 ldp_clear_config(nconf);
627 nconf = NULL;
628 break;
629 case IMSG_DEBUG_UPDATE:
630 if (imsg.hdr.len != IMSG_HEADER_SIZE +
631 sizeof(ldp_debug)) {
632 log_warnx("%s: wrong imsg len", __func__);
633 break;
634 }
635 memcpy(&ldp_debug, imsg.data, sizeof(ldp_debug));
636 break;
637 case IMSG_FILTER_UPDATE:
638 if (imsg.hdr.len != IMSG_HEADER_SIZE +
639 sizeof(struct ldp_access)) {
640 log_warnx("%s: wrong imsg len", __func__);
641 break;
642 }
643 laccess = imsg.data;
644 lde_check_filter_af(AF_INET, &ldeconf->ipv4,
645 laccess->name);
646 lde_check_filter_af(AF_INET6, &ldeconf->ipv6,
647 laccess->name);
648 break;
649 default:
650 log_debug("%s: unexpected imsg %d", __func__,
651 imsg.hdr.type);
652 break;
653 }
654 imsg_free(&imsg);
655 }
656 if (!shut)
657 imsg_event_add(iev);
658 else {
659 /* this pipe is dead, so remove the event handlers and exit */
660 THREAD_READ_OFF(iev->ev_read);
661 THREAD_WRITE_OFF(iev->ev_write);
662 lde_shutdown();
663 }
664
665 return (0);
666 }
667
668 int
669 lde_acl_check(char *acl_name, int af, union ldpd_addr *addr, uint8_t prefixlen)
670 {
671 return ldp_acl_request(iev_main_sync, acl_name, af, addr, prefixlen);
672 }
673
674 static bool lde_fec_connected(const struct fec_node *fn)
675 {
676 struct fec_nh *fnh;
677
678 LIST_FOREACH(fnh, &fn->nexthops, entry)
679 if (fnh->flags & F_FEC_NH_CONNECTED)
680 return true;
681
682 return false;
683 }
684
685 static bool lde_fec_outside_mpls_network(const struct fec_node *fn)
686 {
687 struct fec_nh *fnh;
688
689 LIST_FOREACH(fnh, &fn->nexthops, entry)
690 if (!(fnh->flags & F_FEC_NH_NO_LDP))
691 return false;
692
693 return true;
694 }
695
696 uint32_t
697 lde_update_label(struct fec_node *fn)
698 {
699
700 /* should we allocate a label for this fec? */
701 switch (fn->fec.type) {
702 case FEC_TYPE_IPV4:
703 if ((ldeconf->ipv4.flags & F_LDPD_AF_ALLOCHOSTONLY) &&
704 fn->fec.u.ipv4.prefixlen != 32)
705 return (NO_LABEL);
706 if (lde_acl_check(ldeconf->ipv4.acl_label_allocate_for,
707 AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix,
708 fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT)
709 return (NO_LABEL);
710 break;
711 case FEC_TYPE_IPV6:
712 if ((ldeconf->ipv6.flags & F_LDPD_AF_ALLOCHOSTONLY) &&
713 fn->fec.u.ipv6.prefixlen != 128)
714 return (NO_LABEL);
715 if (lde_acl_check(ldeconf->ipv6.acl_label_allocate_for,
716 AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix,
717 fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT)
718 return (NO_LABEL);
719 break;
720 default:
721 break;
722 }
723
724 /*
725 * If connected interface act as egress for fec.
726 * If LDP is not configured on an interface but there
727 * are other NHs with interfaces configured with LDP
728 * then don't act as an egress for the fec, otherwise
729 * act as an egress for the fec
730 */
731 if (lde_fec_connected(fn) || lde_fec_outside_mpls_network(fn)) {
732 /* choose implicit or explicit-null depending on configuration */
733 switch (fn->fec.type) {
734 case FEC_TYPE_IPV4:
735 if (!(ldeconf->ipv4.flags & F_LDPD_AF_EXPNULL))
736 return (MPLS_LABEL_IMPLICIT_NULL);
737 if (lde_acl_check(ldeconf->ipv4.acl_label_expnull_for,
738 AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix,
739 fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT)
740 return (MPLS_LABEL_IMPLICIT_NULL);
741 return MPLS_LABEL_IPV4_EXPLICIT_NULL;
742 case FEC_TYPE_IPV6:
743 if (!(ldeconf->ipv6.flags & F_LDPD_AF_EXPNULL))
744 return (MPLS_LABEL_IMPLICIT_NULL);
745 if (lde_acl_check(ldeconf->ipv6.acl_label_expnull_for,
746 AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix,
747 fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT)
748 return (MPLS_LABEL_IMPLICIT_NULL);
749 return MPLS_LABEL_IPV6_EXPLICIT_NULL;
750 default:
751 break;
752 }
753 }
754
755 /* preserve current label if there's no need to update it */
756 if (fn->local_label != NO_LABEL &&
757 fn->local_label > MPLS_LABEL_RESERVED_MAX)
758 return (fn->local_label);
759
760 return (lde_get_next_label());
761 }
762
763 void
764 lde_send_change_klabel(struct fec_node *fn, struct fec_nh *fnh)
765 {
766 struct kroute kr;
767 struct zapi_pw zpw;
768 struct l2vpn_pw *pw;
769
770 /*
771 * Ordered Control: don't program label into HW until a
772 * labelmap msg has been received from upstream router
773 */
774 if (fnh->flags & F_FEC_NH_DEFER)
775 return;
776
777 switch (fn->fec.type) {
778 case FEC_TYPE_IPV4:
779 memset(&kr, 0, sizeof(kr));
780 kr.af = AF_INET;
781 kr.prefix.v4 = fn->fec.u.ipv4.prefix;
782 kr.prefixlen = fn->fec.u.ipv4.prefixlen;
783 kr.nexthop.v4 = fnh->nexthop.v4;
784 kr.ifindex = fnh->ifindex;
785 kr.local_label = fn->local_label;
786 kr.remote_label = fnh->remote_label;
787 kr.route_type = fnh->route_type;
788 kr.route_instance = fnh->route_instance;
789
790 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE, 0, &kr,
791 sizeof(kr));
792 break;
793 case FEC_TYPE_IPV6:
794 memset(&kr, 0, sizeof(kr));
795 kr.af = AF_INET6;
796 kr.prefix.v6 = fn->fec.u.ipv6.prefix;
797 kr.prefixlen = fn->fec.u.ipv6.prefixlen;
798 kr.nexthop.v6 = fnh->nexthop.v6;
799 kr.ifindex = fnh->ifindex;
800 kr.local_label = fn->local_label;
801 kr.remote_label = fnh->remote_label;
802 kr.route_type = fnh->route_type;
803 kr.route_instance = fnh->route_instance;
804
805 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE, 0, &kr,
806 sizeof(kr));
807 break;
808 case FEC_TYPE_PWID:
809 pw = (struct l2vpn_pw *) fn->data;
810 if (!pw || fn->local_label == NO_LABEL ||
811 fnh->remote_label == NO_LABEL)
812 return;
813
814 pw->enabled = true;
815 pw2zpw(pw, &zpw);
816 zpw.local_label = fn->local_label;
817 zpw.remote_label = fnh->remote_label;
818 lde_imsg_compose_parent(IMSG_KPW_SET, 0, &zpw, sizeof(zpw));
819 break;
820 }
821 }
822
823 void
824 lde_send_delete_klabel(struct fec_node *fn, struct fec_nh *fnh)
825 {
826 struct kroute kr;
827 struct zapi_pw zpw;
828 struct l2vpn_pw *pw;
829
830 switch (fn->fec.type) {
831 case FEC_TYPE_IPV4:
832 memset(&kr, 0, sizeof(kr));
833 kr.af = AF_INET;
834 kr.prefix.v4 = fn->fec.u.ipv4.prefix;
835 kr.prefixlen = fn->fec.u.ipv4.prefixlen;
836 kr.nexthop.v4 = fnh->nexthop.v4;
837 kr.ifindex = fnh->ifindex;
838 kr.local_label = fn->local_label;
839 kr.remote_label = fnh->remote_label;
840 kr.route_type = fnh->route_type;
841 kr.route_instance = fnh->route_instance;
842
843 lde_imsg_compose_parent(IMSG_KLABEL_DELETE, 0, &kr,
844 sizeof(kr));
845 break;
846 case FEC_TYPE_IPV6:
847 memset(&kr, 0, sizeof(kr));
848 kr.af = AF_INET6;
849 kr.prefix.v6 = fn->fec.u.ipv6.prefix;
850 kr.prefixlen = fn->fec.u.ipv6.prefixlen;
851 kr.nexthop.v6 = fnh->nexthop.v6;
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,
859 sizeof(kr));
860 break;
861 case FEC_TYPE_PWID:
862 pw = (struct l2vpn_pw *) fn->data;
863 if (!pw)
864 return;
865
866 pw->enabled = false;
867 pw2zpw(pw, &zpw);
868 zpw.local_label = fn->local_label;
869 zpw.remote_label = fnh->remote_label;
870 lde_imsg_compose_parent(IMSG_KPW_UNSET, 0, &zpw, sizeof(zpw));
871 break;
872 }
873 }
874
875 void
876 lde_fec2map(struct fec *fec, struct map *map)
877 {
878 memset(map, 0, sizeof(*map));
879
880 switch (fec->type) {
881 case FEC_TYPE_IPV4:
882 map->type = MAP_TYPE_PREFIX;
883 map->fec.prefix.af = AF_INET;
884 map->fec.prefix.prefix.v4 = fec->u.ipv4.prefix;
885 map->fec.prefix.prefixlen = fec->u.ipv4.prefixlen;
886 break;
887 case FEC_TYPE_IPV6:
888 map->type = MAP_TYPE_PREFIX;
889 map->fec.prefix.af = AF_INET6;
890 map->fec.prefix.prefix.v6 = fec->u.ipv6.prefix;
891 map->fec.prefix.prefixlen = fec->u.ipv6.prefixlen;
892 break;
893 case FEC_TYPE_PWID:
894 map->type = MAP_TYPE_PWID;
895 map->fec.pwid.type = fec->u.pwid.type;
896 map->fec.pwid.group_id = 0;
897 map->flags |= F_MAP_PW_ID;
898 map->fec.pwid.pwid = fec->u.pwid.pwid;
899 break;
900 }
901 }
902
903 void
904 lde_map2fec(struct map *map, struct in_addr lsr_id, struct fec *fec)
905 {
906 memset(fec, 0, sizeof(*fec));
907
908 switch (map->type) {
909 case MAP_TYPE_PREFIX:
910 switch (map->fec.prefix.af) {
911 case AF_INET:
912 fec->type = FEC_TYPE_IPV4;
913 fec->u.ipv4.prefix = map->fec.prefix.prefix.v4;
914 fec->u.ipv4.prefixlen = map->fec.prefix.prefixlen;
915 break;
916 case AF_INET6:
917 fec->type = FEC_TYPE_IPV6;
918 fec->u.ipv6.prefix = map->fec.prefix.prefix.v6;
919 fec->u.ipv6.prefixlen = map->fec.prefix.prefixlen;
920 break;
921 default:
922 fatalx("lde_map2fec: unknown af");
923 break;
924 }
925 break;
926 case MAP_TYPE_PWID:
927 fec->type = FEC_TYPE_PWID;
928 fec->u.pwid.type = map->fec.pwid.type;
929 fec->u.pwid.pwid = map->fec.pwid.pwid;
930 fec->u.pwid.lsr_id = lsr_id;
931 break;
932 }
933 }
934
935 void
936 lde_send_labelmapping(struct lde_nbr *ln, struct fec_node *fn, int single)
937 {
938 struct lde_wdraw *lw;
939 struct lde_map *me;
940 struct lde_req *lre;
941 struct map map;
942 struct l2vpn_pw *pw;
943 struct fec_nh *fnh;
944 bool allow = false;
945
946 /*
947 * Ordered Control: do not send a labelmap msg until
948 * a labelmap message is received from downstream router
949 * and don't send labelmap back to downstream router
950 */
951 if (ldeconf->flags & F_LDPD_ORDERED_CONTROL) {
952 LIST_FOREACH(fnh, &fn->nexthops, entry) {
953 if (fnh->flags & F_FEC_NH_DEFER)
954 continue;
955
956 if (lde_address_find(ln, fnh->af, &fnh->nexthop))
957 return;
958 allow = true;
959 break;
960 }
961 if (!allow)
962 return;
963 }
964
965 /*
966 * We shouldn't send a new label mapping if we have a pending
967 * label release to receive. In this case, schedule to send a
968 * label mapping as soon as a label release is received.
969 */
970 lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, &fn->fec);
971 if (lw) {
972 if (!fec_find(&ln->sent_map_pending, &fn->fec)) {
973 debug_evt("%s: FEC %s: scheduling to send label mapping later (waiting for pending label release)",
974 __func__, log_fec(&fn->fec));
975 lde_map_pending_add(ln, fn);
976 }
977 return;
978 }
979
980 /*
981 * This function skips SL.1 - 3 and SL.9 - 14 because the label
982 * allocation is done way earlier (because of the merging nature of
983 * ldpd).
984 */
985
986 lde_fec2map(&fn->fec, &map);
987 switch (fn->fec.type) {
988 case FEC_TYPE_IPV4:
989 if (!ln->v4_enabled)
990 return;
991 if (lde_acl_check(ldeconf->ipv4.acl_label_advertise_to,
992 AF_INET, (union ldpd_addr *)&ln->id, 32) != FILTER_PERMIT)
993 return;
994 if (lde_acl_check(ldeconf->ipv4.acl_label_advertise_for,
995 AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix,
996 fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT)
997 return;
998 break;
999 case FEC_TYPE_IPV6:
1000 if (!ln->v6_enabled)
1001 return;
1002 if (lde_acl_check(ldeconf->ipv6.acl_label_advertise_to,
1003 AF_INET, (union ldpd_addr *)&ln->id, 32) != FILTER_PERMIT)
1004 return;
1005 if (lde_acl_check(ldeconf->ipv6.acl_label_advertise_for,
1006 AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix,
1007 fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT)
1008 return;
1009 break;
1010 case FEC_TYPE_PWID:
1011 pw = (struct l2vpn_pw *) fn->data;
1012 if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr)
1013 /* not the remote end of the pseudowire */
1014 return;
1015
1016 map.flags |= F_MAP_PW_IFMTU;
1017 map.fec.pwid.ifmtu = pw->l2vpn->mtu;
1018 if (pw->flags & F_PW_CWORD)
1019 map.flags |= F_MAP_PW_CWORD;
1020 if (pw->flags & F_PW_STATUSTLV) {
1021 map.flags |= F_MAP_PW_STATUS;
1022 map.pw_status = pw->local_status;
1023 }
1024 break;
1025 }
1026 map.label = fn->local_label;
1027
1028 /* SL.6: is there a pending request for this mapping? */
1029 lre = (struct lde_req *)fec_find(&ln->recv_req, &fn->fec);
1030 if (lre) {
1031 /* set label request msg id in the mapping response. */
1032 map.requestid = lre->msg_id;
1033 map.flags = F_MAP_REQ_ID;
1034
1035 /* SL.7: delete record of pending request */
1036 lde_req_del(ln, lre, 0);
1037 }
1038
1039 /* SL.4: send label mapping */
1040 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD, ln->peerid, 0,
1041 &map, sizeof(map));
1042 if (single)
1043 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0,
1044 NULL, 0);
1045
1046 /* SL.5: record sent label mapping */
1047 me = (struct lde_map *)fec_find(&ln->sent_map, &fn->fec);
1048 if (me == NULL)
1049 me = lde_map_add(ln, fn, 1);
1050 me->map = map;
1051 }
1052
1053 void
1054 lde_send_labelwithdraw(struct lde_nbr *ln, struct fec_node *fn,
1055 struct map *wcard, struct status_tlv *st)
1056 {
1057 struct lde_wdraw *lw;
1058 struct map map;
1059 struct fec *f;
1060 struct l2vpn_pw *pw;
1061
1062 if (fn) {
1063 lde_fec2map(&fn->fec, &map);
1064 switch (fn->fec.type) {
1065 case FEC_TYPE_IPV4:
1066 if (!ln->v4_enabled)
1067 return;
1068 break;
1069 case FEC_TYPE_IPV6:
1070 if (!ln->v6_enabled)
1071 return;
1072 break;
1073 case FEC_TYPE_PWID:
1074 pw = (struct l2vpn_pw *) fn->data;
1075 if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr)
1076 /* not the remote end of the pseudowire */
1077 return;
1078
1079 if (pw->flags & F_PW_CWORD)
1080 map.flags |= F_MAP_PW_CWORD;
1081 break;
1082 }
1083 map.label = fn->local_label;
1084 } else
1085 memcpy(&map, wcard, sizeof(map));
1086
1087 if (st) {
1088 map.st.status_code = st->status_code;
1089 map.st.msg_id = st->msg_id;
1090 map.st.msg_type = st->msg_type;
1091 map.flags |= F_MAP_STATUS;
1092 }
1093
1094 /* SWd.1: send label withdraw. */
1095 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD, ln->peerid, 0,
1096 &map, sizeof(map));
1097 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD_END, ln->peerid, 0, NULL, 0);
1098
1099 /* SWd.2: record label withdraw. */
1100 if (fn) {
1101 lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, &fn->fec);
1102 if (lw == NULL)
1103 lw = lde_wdraw_add(ln, fn);
1104 lw->label = map.label;
1105 } else {
1106 struct lde_map *me;
1107
1108 RB_FOREACH(f, fec_tree, &ft) {
1109 fn = (struct fec_node *)f;
1110 me = (struct lde_map *)fec_find(&ln->sent_map, &fn->fec);
1111 if (lde_wildcard_apply(wcard, &fn->fec, me) == 0)
1112 continue;
1113
1114 lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw,
1115 &fn->fec);
1116 if (lw == NULL)
1117 lw = lde_wdraw_add(ln, fn);
1118 lw->label = map.label;
1119 }
1120 }
1121 }
1122
1123 void
1124 lde_send_labelwithdraw_wcard(struct lde_nbr *ln, uint32_t label)
1125 {
1126 struct map wcard;
1127
1128 memset(&wcard, 0, sizeof(wcard));
1129 wcard.type = MAP_TYPE_WILDCARD;
1130 wcard.label = label;
1131 lde_send_labelwithdraw(ln, NULL, &wcard, NULL);
1132 }
1133
1134 void
1135 lde_send_labelwithdraw_twcard_prefix(struct lde_nbr *ln, uint16_t af,
1136 uint32_t label)
1137 {
1138 struct map wcard;
1139
1140 memset(&wcard, 0, sizeof(wcard));
1141 wcard.type = MAP_TYPE_TYPED_WCARD;
1142 wcard.fec.twcard.type = MAP_TYPE_PREFIX;
1143 wcard.fec.twcard.u.prefix_af = af;
1144 wcard.label = label;
1145 lde_send_labelwithdraw(ln, NULL, &wcard, NULL);
1146 }
1147
1148 void
1149 lde_send_labelwithdraw_twcard_pwid(struct lde_nbr *ln, uint16_t pw_type,
1150 uint32_t label)
1151 {
1152 struct map wcard;
1153
1154 memset(&wcard, 0, sizeof(wcard));
1155 wcard.type = MAP_TYPE_TYPED_WCARD;
1156 wcard.fec.twcard.type = MAP_TYPE_PWID;
1157 wcard.fec.twcard.u.pw_type = pw_type;
1158 wcard.label = label;
1159 lde_send_labelwithdraw(ln, NULL, &wcard, NULL);
1160 }
1161
1162 void
1163 lde_send_labelwithdraw_pwid_wcard(struct lde_nbr *ln, uint16_t pw_type,
1164 uint32_t group_id)
1165 {
1166 struct map wcard;
1167
1168 memset(&wcard, 0, sizeof(wcard));
1169 wcard.type = MAP_TYPE_PWID;
1170 wcard.fec.pwid.type = pw_type;
1171 wcard.fec.pwid.group_id = group_id;
1172 /* we can not append a Label TLV when using PWid group wildcards. */
1173 wcard.label = NO_LABEL;
1174 lde_send_labelwithdraw(ln, NULL, &wcard, NULL);
1175 }
1176
1177 void
1178 lde_send_labelrelease(struct lde_nbr *ln, struct fec_node *fn,
1179 struct map *wcard, uint32_t label)
1180 {
1181 struct map map;
1182 struct l2vpn_pw *pw;
1183
1184 if (fn) {
1185 lde_fec2map(&fn->fec, &map);
1186 switch (fn->fec.type) {
1187 case FEC_TYPE_IPV4:
1188 if (!ln->v4_enabled)
1189 return;
1190 break;
1191 case FEC_TYPE_IPV6:
1192 if (!ln->v6_enabled)
1193 return;
1194 break;
1195 case FEC_TYPE_PWID:
1196 pw = (struct l2vpn_pw *) fn->data;
1197 if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr)
1198 /* not the remote end of the pseudowire */
1199 return;
1200
1201 if (pw->flags & F_PW_CWORD)
1202 map.flags |= F_MAP_PW_CWORD;
1203 break;
1204 }
1205 } else
1206 memcpy(&map, wcard, sizeof(map));
1207 map.label = label;
1208
1209 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD, ln->peerid, 0,
1210 &map, sizeof(map));
1211 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD_END, ln->peerid, 0, NULL, 0);
1212 }
1213
1214 void
1215 lde_send_labelrequest(struct lde_nbr *ln, struct fec_node *fn,
1216 struct map *wcard, int single)
1217 {
1218 struct map map;
1219 struct fec *f;
1220 struct lde_req *lre;
1221
1222 if (fn) {
1223 lde_fec2map(&fn->fec, &map);
1224 switch (fn->fec.type) {
1225 case FEC_TYPE_IPV4:
1226 if (!ln->v4_enabled)
1227 return;
1228 break;
1229 case FEC_TYPE_IPV6:
1230 if (!ln->v6_enabled)
1231 return;
1232 break;
1233 default:
1234 fatalx("lde_send_labelrequest: unknown af");
1235 }
1236 } else
1237 memcpy(&map, wcard, sizeof(map));
1238
1239 map.label = NO_LABEL;
1240
1241 if (fn) {
1242 /* SLR1.1: has label request for FEC been previously sent
1243 * and still outstanding just return,
1244 */
1245 lre = (struct lde_req *)fec_find(&ln->sent_req, &fn->fec);
1246 if (lre == NULL) {
1247 /* SLRq.3: send label request */
1248 lde_imsg_compose_ldpe(IMSG_REQUEST_ADD, ln->peerid, 0,
1249 &map, sizeof(map));
1250 if (single)
1251 lde_imsg_compose_ldpe(IMSG_REQUEST_ADD_END,
1252 ln->peerid, 0, NULL, 0);
1253
1254 /* SLRq.4: record sent request */
1255 lde_req_add(ln, &fn->fec, 1);
1256 }
1257 } else {
1258 /* if Wilcard just send label request */
1259 /* SLRq.3: send label request */
1260 lde_imsg_compose_ldpe(IMSG_REQUEST_ADD,
1261 ln->peerid, 0, &map, sizeof(map));
1262 if (single)
1263 lde_imsg_compose_ldpe(IMSG_REQUEST_ADD_END,
1264 ln->peerid, 0, NULL, 0);
1265
1266 /* SLRq.4: record sent request */
1267 RB_FOREACH(f, fec_tree, &ft) {
1268 fn = (struct fec_node *)f;
1269 lre = (struct lde_req *)fec_find(&ln->sent_req, &fn->fec);
1270 if (lde_wildcard_apply(wcard, &fn->fec, NULL) == 0)
1271 continue;
1272 if (lre == NULL)
1273 lde_req_add(ln, f, 1);
1274 }
1275 }
1276 }
1277
1278 void
1279 lde_send_labelrequest_wcard(struct lde_nbr *ln, uint16_t af)
1280 {
1281 struct map wcard;
1282
1283 memset(&wcard, 0, sizeof(wcard));
1284 wcard.type = MAP_TYPE_TYPED_WCARD;
1285 wcard.fec.twcard.type = MAP_TYPE_PREFIX;
1286 wcard.fec.twcard.u.prefix_af = af;
1287 lde_send_labelrequest(ln, NULL, &wcard, 1);
1288 }
1289
1290 void
1291 lde_send_notification(struct lde_nbr *ln, uint32_t status_code, uint32_t msg_id,
1292 uint16_t msg_type)
1293 {
1294 struct notify_msg nm;
1295
1296 memset(&nm, 0, sizeof(nm));
1297 nm.status_code = status_code;
1298 /* 'msg_id' and 'msg_type' should be in network byte order */
1299 nm.msg_id = msg_id;
1300 nm.msg_type = msg_type;
1301
1302 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0,
1303 &nm, sizeof(nm));
1304 }
1305
1306 void
1307 lde_send_notification_eol_prefix(struct lde_nbr *ln, int af)
1308 {
1309 struct notify_msg nm;
1310
1311 memset(&nm, 0, sizeof(nm));
1312 nm.status_code = S_ENDOFLIB;
1313 nm.fec.type = MAP_TYPE_TYPED_WCARD;
1314 nm.fec.fec.twcard.type = MAP_TYPE_PREFIX;
1315 nm.fec.fec.twcard.u.prefix_af = af;
1316 nm.flags |= F_NOTIF_FEC;
1317
1318 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0,
1319 &nm, sizeof(nm));
1320 }
1321
1322 void
1323 lde_send_notification_eol_pwid(struct lde_nbr *ln, uint16_t pw_type)
1324 {
1325 struct notify_msg nm;
1326
1327 memset(&nm, 0, sizeof(nm));
1328 nm.status_code = S_ENDOFLIB;
1329 nm.fec.type = MAP_TYPE_TYPED_WCARD;
1330 nm.fec.fec.twcard.type = MAP_TYPE_PWID;
1331 nm.fec.fec.twcard.u.pw_type = pw_type;
1332 nm.flags |= F_NOTIF_FEC;
1333
1334 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0,
1335 &nm, sizeof(nm));
1336 }
1337
1338 static __inline int
1339 lde_nbr_compare(const struct lde_nbr *a, const struct lde_nbr *b)
1340 {
1341 return (a->peerid - b->peerid);
1342 }
1343
1344 static struct lde_nbr *
1345 lde_nbr_new(uint32_t peerid, struct lde_nbr *new)
1346 {
1347 struct lde_nbr *ln;
1348
1349 if ((ln = calloc(1, sizeof(*ln))) == NULL)
1350 fatal(__func__);
1351
1352 ln->id = new->id;
1353 ln->v4_enabled = new->v4_enabled;
1354 ln->v6_enabled = new->v6_enabled;
1355 ln->flags = new->flags;
1356 ln->peerid = peerid;
1357 fec_init(&ln->recv_map);
1358 fec_init(&ln->sent_map);
1359 fec_init(&ln->sent_map_pending);
1360 fec_init(&ln->recv_req);
1361 fec_init(&ln->sent_req);
1362 fec_init(&ln->sent_wdraw);
1363
1364 TAILQ_INIT(&ln->addr_list);
1365
1366 if (RB_INSERT(nbr_tree, &lde_nbrs, ln) != NULL)
1367 fatalx("lde_nbr_new: RB_INSERT failed");
1368
1369 return (ln);
1370 }
1371
1372 static void
1373 lde_nbr_del(struct lde_nbr *ln)
1374 {
1375 struct fec *f;
1376 struct fec_node *fn;
1377 struct fec_nh *fnh;
1378 struct l2vpn_pw *pw;
1379 struct lde_nbr *lnbr;
1380
1381 if (ln == NULL)
1382 return;
1383
1384 /* uninstall received mappings */
1385 RB_FOREACH(f, fec_tree, &ft) {
1386 fn = (struct fec_node *)f;
1387
1388 LIST_FOREACH(fnh, &fn->nexthops, entry) {
1389 switch (f->type) {
1390 case FEC_TYPE_IPV4:
1391 case FEC_TYPE_IPV6:
1392 if (!lde_address_find(ln, fnh->af,
1393 &fnh->nexthop))
1394 continue;
1395
1396 /*
1397 * Ordered Control: must mark any non-connected
1398 * NH to wait until we receive a labelmap msg
1399 * before installing in kernel and sending to
1400 * peer, must do this as NHs are not removed
1401 * when lsps go down. Also send label withdraw
1402 * to other neighbors for all fecs from neighbor
1403 * going down
1404 */
1405 if (ldeconf->flags & F_LDPD_ORDERED_CONTROL) {
1406 fnh->flags |= F_FEC_NH_DEFER;
1407
1408 RB_FOREACH(lnbr, nbr_tree, &lde_nbrs) {
1409 if (ln->peerid == lnbr->peerid)
1410 continue;
1411 lde_send_labelwithdraw(lnbr, fn, NULL, NULL);
1412 }
1413 }
1414 break;
1415 case FEC_TYPE_PWID:
1416 if (f->u.pwid.lsr_id.s_addr != ln->id.s_addr)
1417 continue;
1418 pw = (struct l2vpn_pw *) fn->data;
1419 if (pw) {
1420 pw->reason = F_PW_NO_REMOTE_LABEL;
1421 l2vpn_pw_reset(pw);
1422 }
1423 break;
1424 default:
1425 break;
1426 }
1427
1428 lde_send_delete_klabel(fn, fnh);
1429 fnh->remote_label = NO_LABEL;
1430 }
1431 }
1432
1433 lde_address_list_free(ln);
1434
1435 fec_clear(&ln->recv_map, lde_map_free);
1436 fec_clear(&ln->sent_map, lde_map_free);
1437 fec_clear(&ln->sent_map_pending, free);
1438 fec_clear(&ln->recv_req, free);
1439 fec_clear(&ln->sent_req, free);
1440 fec_clear(&ln->sent_wdraw, free);
1441
1442 RB_REMOVE(nbr_tree, &lde_nbrs, ln);
1443
1444 free(ln);
1445 }
1446
1447 static struct lde_nbr *
1448 lde_nbr_find(uint32_t peerid)
1449 {
1450 struct lde_nbr ln;
1451
1452 ln.peerid = peerid;
1453
1454 return (RB_FIND(nbr_tree, &lde_nbrs, &ln));
1455 }
1456
1457 struct lde_nbr *
1458 lde_nbr_find_by_lsrid(struct in_addr addr)
1459 {
1460 struct lde_nbr *ln;
1461
1462 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1463 if (ln->id.s_addr == addr.s_addr)
1464 return (ln);
1465
1466 return (NULL);
1467 }
1468
1469 struct lde_nbr *
1470 lde_nbr_find_by_addr(int af, union ldpd_addr *addr)
1471 {
1472 struct lde_nbr *ln;
1473
1474 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1475 if (lde_address_find(ln, af, addr) != NULL)
1476 return (ln);
1477
1478 return (NULL);
1479 }
1480
1481 static void
1482 lde_nbr_clear(void)
1483 {
1484 struct lde_nbr *ln;
1485
1486 while (!RB_EMPTY(nbr_tree, &lde_nbrs)) {
1487 ln = RB_ROOT(nbr_tree, &lde_nbrs);
1488
1489 lde_nbr_del(ln);
1490 }
1491 }
1492
1493 static void
1494 lde_nbr_addr_update(struct lde_nbr *ln, struct lde_addr *lde_addr, int removed)
1495 {
1496 struct fec *fec;
1497 struct fec_node *fn;
1498 struct fec_nh *fnh;
1499 struct lde_map *me;
1500
1501 RB_FOREACH(fec, fec_tree, &ln->recv_map) {
1502 switch (fec->type) {
1503 case FEC_TYPE_IPV4:
1504 if (lde_addr->af != AF_INET)
1505 continue;
1506 break;
1507 case FEC_TYPE_IPV6:
1508 if (lde_addr->af != AF_INET6)
1509 continue;
1510 break;
1511 default:
1512 continue;
1513 }
1514
1515 fn = (struct fec_node *)fec_find(&ft, fec);
1516 if (fn == NULL)
1517 /* shouldn't happen */
1518 continue;
1519
1520 LIST_FOREACH(fnh, &fn->nexthops, entry) {
1521 if (ldp_addrcmp(fnh->af, &fnh->nexthop,
1522 &lde_addr->addr))
1523 continue;
1524
1525 if (removed) {
1526 lde_send_delete_klabel(fn, fnh);
1527 fnh->remote_label = NO_LABEL;
1528 } else {
1529 me = (struct lde_map *)fec;
1530 fnh->remote_label = me->map.label;
1531 lde_send_change_klabel(fn, fnh);
1532 }
1533 break;
1534 }
1535 }
1536 }
1537
1538 static __inline int
1539 lde_map_compare(const struct lde_map *a, const struct lde_map *b)
1540 {
1541 return (ldp_addrcmp(AF_INET, (union ldpd_addr *)&a->nexthop->id,
1542 (union ldpd_addr *)&b->nexthop->id));
1543 }
1544
1545 struct lde_map *
1546 lde_map_add(struct lde_nbr *ln, struct fec_node *fn, int sent)
1547 {
1548 struct lde_map *me;
1549
1550 me = calloc(1, sizeof(*me));
1551 if (me == NULL)
1552 fatal(__func__);
1553
1554 me->fec = fn->fec;
1555 me->nexthop = ln;
1556
1557 if (sent) {
1558 RB_INSERT(lde_map_head, &fn->upstream, me);
1559 me->head = &fn->upstream;
1560 if (fec_insert(&ln->sent_map, &me->fec))
1561 log_warnx("failed to add %s to sent map",
1562 log_fec(&me->fec));
1563 /* XXX on failure more cleanup is needed */
1564 } else {
1565 RB_INSERT(lde_map_head, &fn->downstream, me);
1566 me->head = &fn->downstream;
1567 if (fec_insert(&ln->recv_map, &me->fec))
1568 log_warnx("failed to add %s to recv map",
1569 log_fec(&me->fec));
1570 }
1571
1572 return (me);
1573 }
1574
1575 void
1576 lde_map_del(struct lde_nbr *ln, struct lde_map *me, int sent)
1577 {
1578 if (sent)
1579 fec_remove(&ln->sent_map, &me->fec);
1580 else
1581 fec_remove(&ln->recv_map, &me->fec);
1582
1583 lde_map_free(me);
1584 }
1585
1586 static void
1587 lde_map_free(void *ptr)
1588 {
1589 struct lde_map *map = ptr;
1590
1591 RB_REMOVE(lde_map_head, map->head, map);
1592 free(map);
1593 }
1594
1595 struct fec *
1596 lde_map_pending_add(struct lde_nbr *ln, struct fec_node *fn)
1597 {
1598 struct fec *map;
1599
1600 map = calloc(1, sizeof(*map));
1601 if (map == NULL)
1602 fatal(__func__);
1603
1604 *map = fn->fec;
1605 if (fec_insert(&ln->sent_map_pending, map))
1606 log_warnx("failed to add %s to sent map (pending)",
1607 log_fec(map));
1608
1609 return (map);
1610 }
1611
1612 void
1613 lde_map_pending_del(struct lde_nbr *ln, struct fec *map)
1614 {
1615 fec_remove(&ln->sent_map_pending, map);
1616 free(map);
1617 }
1618
1619 struct lde_req *
1620 lde_req_add(struct lde_nbr *ln, struct fec *fec, int sent)
1621 {
1622 struct fec_tree *t;
1623 struct lde_req *lre;
1624
1625 t = sent ? &ln->sent_req : &ln->recv_req;
1626
1627 lre = calloc(1, sizeof(*lre));
1628 if (lre != NULL) {
1629 lre->fec = *fec;
1630
1631 if (fec_insert(t, &lre->fec)) {
1632 log_warnx("failed to add %s to %s req",
1633 log_fec(&lre->fec), sent ? "sent" : "recv");
1634 free(lre);
1635 return (NULL);
1636 }
1637 }
1638
1639 return (lre);
1640 }
1641
1642 void
1643 lde_req_del(struct lde_nbr *ln, struct lde_req *lre, int sent)
1644 {
1645 if (sent)
1646 fec_remove(&ln->sent_req, &lre->fec);
1647 else
1648 fec_remove(&ln->recv_req, &lre->fec);
1649
1650 free(lre);
1651 }
1652
1653 struct lde_wdraw *
1654 lde_wdraw_add(struct lde_nbr *ln, struct fec_node *fn)
1655 {
1656 struct lde_wdraw *lw;
1657
1658 lw = calloc(1, sizeof(*lw));
1659 if (lw == NULL)
1660 fatal(__func__);
1661
1662 lw->fec = fn->fec;
1663
1664 if (fec_insert(&ln->sent_wdraw, &lw->fec))
1665 log_warnx("failed to add %s to sent wdraw",
1666 log_fec(&lw->fec));
1667
1668 return (lw);
1669 }
1670
1671 void
1672 lde_wdraw_del(struct lde_nbr *ln, struct lde_wdraw *lw)
1673 {
1674 fec_remove(&ln->sent_wdraw, &lw->fec);
1675 free(lw);
1676 }
1677
1678 void
1679 lde_change_egress_label(int af)
1680 {
1681 struct lde_nbr *ln;
1682 struct fec *f;
1683 struct fec_node *fn;
1684
1685 /* explicitly withdraw all null labels */
1686 RB_FOREACH(ln, nbr_tree, &lde_nbrs) {
1687 lde_send_labelwithdraw_wcard(ln, MPLS_LABEL_IMPLICIT_NULL);
1688 if (ln->v4_enabled)
1689 lde_send_labelwithdraw_wcard(
1690 ln,
1691 MPLS_LABEL_IPV4_EXPLICIT_NULL);
1692 if (ln->v6_enabled)
1693 lde_send_labelwithdraw_wcard(
1694 ln,
1695 MPLS_LABEL_IPV6_EXPLICIT_NULL);
1696 }
1697
1698 /* update label of connected routes */
1699 RB_FOREACH(f, fec_tree, &ft) {
1700 fn = (struct fec_node *)f;
1701 if (fn->local_label > MPLS_LABEL_RESERVED_MAX)
1702 continue;
1703
1704 switch (af) {
1705 case AF_INET:
1706 if (fn->fec.type != FEC_TYPE_IPV4)
1707 continue;
1708 break;
1709 case AF_INET6:
1710 if (fn->fec.type != FEC_TYPE_IPV6)
1711 continue;
1712 break;
1713 default:
1714 fatalx("lde_change_egress_label: unknown af");
1715 }
1716
1717 fn->local_label = lde_update_label(fn);
1718 if (fn->local_label != NO_LABEL)
1719 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1720 lde_send_labelmapping(ln, fn, 0);
1721 }
1722 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1723 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0,
1724 NULL, 0);
1725 }
1726
1727 void
1728 lde_change_allocate_filter(int af)
1729 {
1730 struct lde_nbr *ln;
1731 struct fec *f;
1732 struct fec_node *fn;
1733 uint32_t new_label;
1734
1735 /* reallocate labels for fecs that match this filter */
1736 RB_FOREACH(f, fec_tree, &ft) {
1737 fn = (struct fec_node *)f;
1738
1739 switch (af) {
1740 case AF_INET:
1741 if (fn->fec.type != FEC_TYPE_IPV4)
1742 continue;
1743 break;
1744 case AF_INET6:
1745 if (fn->fec.type != FEC_TYPE_IPV6)
1746 continue;
1747 break;
1748 default:
1749 fatalx("lde_change_allocate_filter: unknown af");
1750 }
1751
1752 /*
1753 * If the local label has changed to NO_LABEL, send a label
1754 * withdraw to all peers.
1755 * If the local label has changed and it's different from
1756 * NO_LABEL, send a label mapping to all peers advertising
1757 * the new label.
1758 * If the local label hasn't changed, do nothing
1759 */
1760 new_label = lde_update_label(fn);
1761 if (fn->local_label != new_label) {
1762 if (new_label == NO_LABEL)
1763 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1764 lde_send_labelwithdraw(ln, fn,
1765 NULL, NULL);
1766
1767 fn->local_label = new_label;
1768 if (fn->local_label != NO_LABEL)
1769 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1770 lde_send_labelmapping(ln, fn, 0);
1771 }
1772 }
1773 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1774 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0,
1775 NULL, 0);
1776 }
1777
1778 void
1779 lde_change_advertise_filter(int af)
1780 {
1781 struct lde_nbr *ln;
1782 struct fec *f;
1783 struct fec_node *fn;
1784 char *acl_to_filter;
1785 char *acl_for_filter;
1786 union ldpd_addr *prefix;
1787 uint8_t plen;
1788 struct lde_map *me;
1789
1790 /* advertise label for fecs to neighbors if matches advertise filters */
1791 switch (af) {
1792 case AF_INET:
1793 acl_to_filter = ldeconf->ipv4.acl_label_advertise_to;
1794 acl_for_filter = ldeconf->ipv4.acl_label_advertise_for;
1795 break;
1796 case AF_INET6:
1797 acl_to_filter = ldeconf->ipv6.acl_label_advertise_to;
1798 acl_for_filter = ldeconf->ipv6.acl_label_advertise_for;
1799 break;
1800 default:
1801 fatalx("lde_change_advertise_filter: unknown af");
1802 }
1803
1804 RB_FOREACH(ln, nbr_tree, &lde_nbrs) {
1805 if (lde_acl_check(acl_to_filter, af, (union ldpd_addr *)&ln->id,
1806 IPV4_MAX_BITLEN) != FILTER_PERMIT)
1807 lde_send_labelwithdraw_wcard(ln, NO_LABEL);
1808 else {
1809 /* This neighbor is allowed in to_filter, so
1810 * send labels if fec also matches for_filter
1811 */
1812 RB_FOREACH(f, fec_tree, &ft) {
1813 fn = (struct fec_node *)f;
1814 switch (af) {
1815 case AF_INET:
1816 if (fn->fec.type != FEC_TYPE_IPV4)
1817 continue;
1818 prefix = (union ldpd_addr *)
1819 &fn->fec.u.ipv4.prefix;
1820 plen = fn->fec.u.ipv4.prefixlen;
1821 break;
1822 case FEC_TYPE_IPV6:
1823 if (fn->fec.type != FEC_TYPE_IPV6)
1824 continue;
1825 prefix = (union ldpd_addr *)
1826 &fn->fec.u.ipv6.prefix;
1827 plen = fn->fec.u.ipv6.prefixlen;
1828 break;
1829 default:
1830 continue;
1831 }
1832 if (lde_acl_check(acl_for_filter, af,
1833 prefix, plen) != FILTER_PERMIT) {
1834 me = (struct lde_map *)fec_find(
1835 &ln->sent_map, &fn->fec);
1836 if (me)
1837 /* fec filtered withdraw */
1838 lde_send_labelwithdraw(ln, fn,
1839 NULL, NULL);
1840 } else
1841 /* fec allowed send map */
1842 lde_send_labelmapping(ln, fn, 0);
1843 }
1844 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END,
1845 ln->peerid, 0, NULL, 0);
1846 }
1847 }
1848 }
1849
1850
1851 void
1852 lde_change_accept_filter(int af)
1853 {
1854 struct lde_nbr *ln;
1855 struct fec *f;
1856 struct fec_node *fn;
1857 char *acl_for_filter;
1858 char *acl_from_filter;
1859 union ldpd_addr *prefix;
1860 uint8_t plen;
1861 struct lde_map *me;
1862 enum fec_type type;
1863
1864 /* accept labels from neighbors specified in the from_filter and for
1865 * fecs defined in the for_filter
1866 */
1867 switch (af) {
1868 case AF_INET:
1869 acl_for_filter = ldeconf->ipv4.acl_label_accept_for;
1870 acl_from_filter = ldeconf->ipv4.acl_label_accept_from;
1871 type = FEC_TYPE_IPV4;
1872 break;
1873 case AF_INET6:
1874 acl_for_filter = ldeconf->ipv6.acl_label_accept_for;
1875 acl_from_filter = ldeconf->ipv6.acl_label_accept_from;
1876 type = FEC_TYPE_IPV6;
1877 break;
1878 default:
1879 fatalx("lde_change_accept_filter: unknown af");
1880 }
1881
1882 RB_FOREACH(ln, nbr_tree, &lde_nbrs) {
1883 if (lde_acl_check(acl_from_filter, AF_INET, (union ldpd_addr *)
1884 &ln->id, IPV4_MAX_BITLEN) != FILTER_PERMIT) {
1885 /* This neighbor is now filtered so remove fecs from
1886 * recv list
1887 */
1888 RB_FOREACH(f, fec_tree, &ft) {
1889 fn = (struct fec_node *)f;
1890 if (fn->fec.type == type) {
1891 me = (struct lde_map *)fec_find(
1892 &ln->recv_map, &fn->fec);
1893 if (me)
1894 lde_map_del(ln, me, 0);
1895 }
1896 }
1897 } else if (ln->flags & F_NBR_CAP_TWCARD) {
1898 /* This neighbor is allowed and supports type
1899 * wildcard so send a labelrequest
1900 * to get any new labels from neighbor
1901 * and make sure any fecs we currently have
1902 * match for_filter.
1903 */
1904 RB_FOREACH(f, fec_tree, &ft) {
1905 fn = (struct fec_node *)f;
1906 switch (af) {
1907 case AF_INET:
1908 if (fn->fec.type != FEC_TYPE_IPV4)
1909 continue;
1910 prefix = (union ldpd_addr *)
1911 &fn->fec.u.ipv4.prefix;
1912 plen = fn->fec.u.ipv4.prefixlen;
1913 break;
1914 case AF_INET6:
1915 if (fn->fec.type != FEC_TYPE_IPV6)
1916 continue;
1917 prefix = (union ldpd_addr *)
1918 &fn->fec.u.ipv6.prefix;
1919 plen = fn->fec.u.ipv6.prefixlen;
1920 break;
1921 default:
1922 continue;
1923 }
1924 if (lde_acl_check(acl_for_filter, af,
1925 prefix, plen) != FILTER_PERMIT) {
1926 me = (struct lde_map *)fec_find(
1927 &ln->recv_map, &fn->fec);
1928 if (me)
1929 lde_map_del(ln, me, 0);
1930 }
1931 }
1932 lde_send_labelrequest_wcard(ln, af);
1933 } else
1934 /* Type Wildcard is not supported so restart session */
1935 lde_imsg_compose_ldpe(IMSG_NBR_SHUTDOWN, ln->peerid, 0,
1936 NULL, 0);
1937 }
1938 }
1939
1940 void
1941 lde_change_expnull_for_filter(int af)
1942 {
1943 struct lde_nbr *ln;
1944 struct fec *f;
1945 struct fec_node *fn;
1946 char *acl_name;
1947 uint32_t exp_label;
1948 union ldpd_addr *prefix;
1949 uint8_t plen;
1950
1951 /* Configure explicit-null advertisement for all fecs in this filter */
1952 RB_FOREACH(f, fec_tree, &ft) {
1953 fn = (struct fec_node *)f;
1954
1955 switch (af) {
1956 case AF_INET:
1957 if (fn->fec.type != FEC_TYPE_IPV4)
1958 continue;
1959 acl_name = ldeconf->ipv4.acl_label_expnull_for;
1960 prefix = (union ldpd_addr *)&fn->fec.u.ipv4.prefix;
1961 plen = fn->fec.u.ipv4.prefixlen;
1962 exp_label = MPLS_LABEL_IPV4_EXPLICIT_NULL;
1963 break;
1964 case AF_INET6:
1965 if (fn->fec.type != FEC_TYPE_IPV6)
1966 continue;
1967 acl_name = ldeconf->ipv6.acl_label_expnull_for;
1968 prefix = (union ldpd_addr *)&fn->fec.u.ipv6.prefix;
1969 plen = fn->fec.u.ipv6.prefixlen;
1970 exp_label = MPLS_LABEL_IPV6_EXPLICIT_NULL;
1971 break;
1972 default:
1973 fatalx("lde_change_expnull_for_filter: unknown af");
1974 }
1975
1976 if (lde_acl_check(acl_name, af, prefix, plen) == FILTER_PERMIT) {
1977 /* for this fec change any imp-null to exp-null */
1978 if (fn->local_label == MPLS_LABEL_IMPLICIT_NULL) {
1979 fn->local_label= lde_update_label(fn);
1980 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1981 lde_send_labelmapping(ln, fn, 0);
1982 }
1983 } else {
1984 /* for this fec change any exp-null back to imp-null */
1985 if (fn->local_label == exp_label) {
1986 fn->local_label = lde_update_label(fn);
1987 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1988 lde_send_labelmapping(ln, fn, 0);
1989 }
1990 }
1991 }
1992 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1993 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0,
1994 NULL, 0);
1995 }
1996
1997 static int
1998 lde_address_add(struct lde_nbr *ln, struct lde_addr *lde_addr)
1999 {
2000 struct lde_addr *new;
2001
2002 if (lde_address_find(ln, lde_addr->af, &lde_addr->addr) != NULL)
2003 return (-1);
2004
2005 if ((new = calloc(1, sizeof(*new))) == NULL)
2006 fatal(__func__);
2007
2008 new->af = lde_addr->af;
2009 new->addr = lde_addr->addr;
2010 TAILQ_INSERT_TAIL(&ln->addr_list, new, entry);
2011
2012 /* reevaluate the previously received mappings from this neighbor */
2013 lde_nbr_addr_update(ln, lde_addr, 0);
2014
2015 return (0);
2016 }
2017
2018 static int
2019 lde_address_del(struct lde_nbr *ln, struct lde_addr *lde_addr)
2020 {
2021 lde_addr = lde_address_find(ln, lde_addr->af, &lde_addr->addr);
2022 if (lde_addr == NULL)
2023 return (-1);
2024
2025 /* reevaluate the previously received mappings from this neighbor */
2026 lde_nbr_addr_update(ln, lde_addr, 1);
2027
2028 TAILQ_REMOVE(&ln->addr_list, lde_addr, entry);
2029 free(lde_addr);
2030
2031 return (0);
2032 }
2033
2034 struct lde_addr *
2035 lde_address_find(struct lde_nbr *ln, int af, union ldpd_addr *addr)
2036 {
2037 struct lde_addr *lde_addr;
2038
2039 TAILQ_FOREACH(lde_addr, &ln->addr_list, entry)
2040 if (lde_addr->af == af &&
2041 ldp_addrcmp(af, &lde_addr->addr, addr) == 0)
2042 return (lde_addr);
2043
2044 return (NULL);
2045 }
2046
2047 static void
2048 lde_address_list_free(struct lde_nbr *ln)
2049 {
2050 struct lde_addr *lde_addr;
2051
2052 while ((lde_addr = TAILQ_POP_FIRST(&ln->addr_list, entry)) != NULL)
2053 free(lde_addr);
2054 }
2055
2056 /*
2057 * Event callback used to retry the label-manager sync zapi session.
2058 */
2059 static int zclient_sync_retry(struct thread *thread)
2060 {
2061 zclient_sync_init();
2062
2063 return 0;
2064 }
2065
2066 /*
2067 * Initialize and open a synchronous zapi session. This is used by label chunk
2068 * management code, which acquires and releases blocks of labels from the
2069 * zebra label-manager module.
2070 */
2071 static void zclient_sync_init(void)
2072 {
2073 struct zclient_options options = zclient_options_default;
2074
2075 options.synchronous = true;
2076
2077 /* Initialize special zclient for synchronous message exchanges. */
2078 zclient_sync = zclient_new(master, &options);
2079 zclient_sync->sock = -1;
2080 zclient_sync->redist_default = ZEBRA_ROUTE_LDP;
2081 zclient_sync->session_id = 1; /* Distinguish from main session */
2082 zclient_sync->privs = &lde_privs;
2083
2084 if (zclient_socket_connect(zclient_sync) < 0) {
2085 log_warnx("Error connecting synchronous zclient!");
2086 goto retry;
2087 }
2088 /* make socket non-blocking */
2089 sock_set_nonblock(zclient_sync->sock);
2090
2091 /* Send hello to notify zebra this is a synchronous client */
2092 if (zclient_send_hello(zclient_sync) < 0) {
2093 log_warnx("Error sending hello for synchronous zclient!");
2094 goto retry;
2095 }
2096
2097 /* Connect to label manager */
2098 if (lm_label_manager_connect(zclient_sync, 0) != 0) {
2099 log_warnx("Error connecting to label manager!");
2100 goto retry;
2101 }
2102
2103 /* Finish label-manager init once the LM session is running */
2104 lde_label_list_init();
2105
2106 return;
2107
2108 retry:
2109
2110 /* Discard failed zclient object */
2111 zclient_stop(zclient_sync);
2112 zclient_free(zclient_sync);
2113 zclient_sync = NULL;
2114
2115 /* Retry using a timer */
2116 thread_add_timer(master, zclient_sync_retry, NULL, 1, NULL);
2117 }
2118
2119 static void
2120 lde_del_label_chunk(void *val)
2121 {
2122 free(val);
2123 }
2124
2125 static int
2126 lde_release_label_chunk(uint32_t start, uint32_t end)
2127 {
2128 int ret;
2129
2130 ret = lm_release_label_chunk(zclient_sync, start, end);
2131 if (ret < 0) {
2132 log_warnx("Error releasing label chunk!");
2133 return (-1);
2134 }
2135 return (0);
2136 }
2137
2138 static int
2139 lde_get_label_chunk(void)
2140 {
2141 int ret;
2142 uint32_t start, end;
2143
2144 debug_labels("getting label chunk (size %u)", CHUNK_SIZE);
2145 ret = lm_get_label_chunk(zclient_sync, 0, MPLS_LABEL_BASE_ANY,
2146 CHUNK_SIZE, &start, &end);
2147 if (ret < 0) {
2148 log_warnx("Error getting label chunk!");
2149 return -1;
2150 }
2151
2152 on_get_label_chunk_response(start, end);
2153
2154 return (0);
2155 }
2156
2157 static void
2158 lde_label_list_init(void)
2159 {
2160 label_chunk_list = list_new();
2161 label_chunk_list->del = lde_del_label_chunk;
2162
2163 /* get first chunk */
2164 while (lde_get_label_chunk () != 0) {
2165 log_warnx("Error getting first label chunk!");
2166 sleep(1);
2167 }
2168 }
2169
2170 static void
2171 on_get_label_chunk_response(uint32_t start, uint32_t end)
2172 {
2173 struct label_chunk *new_label_chunk;
2174
2175 debug_labels("label chunk assign: %u - %u", start, end);
2176
2177 new_label_chunk = calloc(1, sizeof(struct label_chunk));
2178 if (!new_label_chunk) {
2179 log_warn("Error trying to allocate label chunk %u - %u", start, end);
2180 return;
2181 }
2182
2183 new_label_chunk->start = start;
2184 new_label_chunk->end = end;
2185 new_label_chunk->used_mask = 0;
2186
2187 listnode_add(label_chunk_list, (void *)new_label_chunk);
2188
2189 /* let's update current if needed */
2190 if (!current_label_chunk)
2191 current_label_chunk = listtail(label_chunk_list);
2192 }
2193
2194 void
2195 lde_free_label(uint32_t label)
2196 {
2197 struct listnode *node;
2198 struct label_chunk *label_chunk;
2199 uint64_t pos;
2200
2201 for (ALL_LIST_ELEMENTS_RO(label_chunk_list, node, label_chunk)) {
2202 if (label <= label_chunk->end && label >= label_chunk->start) {
2203 pos = 1ULL << (label - label_chunk->start);
2204 label_chunk->used_mask &= ~pos;
2205 /* if nobody is using this chunk and it's not current_label_chunk, then free it */
2206 if (!label_chunk->used_mask && (current_label_chunk != node)) {
2207 if (lde_release_label_chunk(label_chunk->start, label_chunk->end) != 0)
2208 log_warnx("%s: Error releasing label chunk!", __func__);
2209 else {
2210 listnode_delete(label_chunk_list, label_chunk);
2211 lde_del_label_chunk(label_chunk);
2212 }
2213 }
2214 break;
2215 }
2216 }
2217 return;
2218 }
2219
2220 static uint32_t
2221 lde_get_next_label(void)
2222 {
2223 struct label_chunk *label_chunk;
2224 uint32_t i, size;
2225 uint64_t pos;
2226 uint32_t label = NO_LABEL;
2227
2228 while (current_label_chunk) {
2229 label_chunk = listgetdata(current_label_chunk);
2230 if (!label_chunk)
2231 goto end;
2232
2233 /* try to get next free label in currently used label chunk */
2234 size = label_chunk->end - label_chunk->start + 1;
2235 for (i = 0, pos = 1; i < size; i++, pos <<= 1) {
2236 if (!(pos & label_chunk->used_mask)) {
2237 label_chunk->used_mask |= pos;
2238 label = label_chunk->start + i;
2239 goto end;
2240 }
2241 }
2242 current_label_chunk = listnextnode(current_label_chunk);
2243 }
2244
2245 end:
2246 /* we moved till the last chunk, or were not able to find a label,
2247 so let's ask for another one */
2248 if (!current_label_chunk ||
2249 current_label_chunk == listtail(label_chunk_list) ||
2250 label == NO_LABEL) {
2251 if (lde_get_label_chunk() != 0)
2252 log_warn("%s: Error getting label chunk!", __func__);
2253
2254 }
2255
2256 return (label);
2257 }
2258
2259 static void
2260 lde_check_filter_af(int af, struct ldpd_af_conf *af_conf,
2261 const char *filter_name)
2262 {
2263 if (strcmp(af_conf->acl_label_allocate_for, filter_name) == 0)
2264 lde_change_allocate_filter(af);
2265 if ((strcmp(af_conf->acl_label_advertise_to, filter_name) == 0)
2266 || (strcmp(af_conf->acl_label_advertise_for, filter_name) == 0))
2267 lde_change_advertise_filter(af);
2268 if ((strcmp(af_conf->acl_label_accept_for, filter_name) == 0)
2269 || (strcmp(af_conf->acl_label_accept_from, filter_name) == 0))
2270 lde_change_accept_filter(af);
2271 if (strcmp(af_conf->acl_label_expnull_for, filter_name) == 0)
2272 lde_change_expnull_for_filter(af);
2273 }