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