]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/lde.c
Merge branch 'frr/pull/672' into stable/3.0
[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
41 static void lde_shutdown(void);
42 static int lde_dispatch_imsg(struct thread *);
43 static int lde_dispatch_parent(struct thread *);
44 static __inline int lde_nbr_compare(struct lde_nbr *,
45 struct lde_nbr *);
46 static struct lde_nbr *lde_nbr_new(uint32_t, struct lde_nbr *);
47 static void lde_nbr_del(struct lde_nbr *);
48 static struct lde_nbr *lde_nbr_find(uint32_t);
49 static void lde_nbr_clear(void);
50 static void lde_nbr_addr_update(struct lde_nbr *,
51 struct lde_addr *, int);
52 static __inline int lde_map_compare(struct lde_map *, struct lde_map *);
53 static void lde_map_free(void *);
54 static int lde_address_add(struct lde_nbr *, struct lde_addr *);
55 static int lde_address_del(struct lde_nbr *, struct lde_addr *);
56 static void lde_address_list_free(struct lde_nbr *);
57 static void zclient_sync_init (u_short instance);
58 static void lde_label_list_init(void);
59 static int lde_get_label_chunk (void);
60 static void on_get_label_chunk_response(uint32_t start, uint32_t end);
61 static uint32_t lde_get_next_label(void);
62
63 RB_GENERATE(nbr_tree, lde_nbr, entry, lde_nbr_compare)
64 RB_GENERATE(lde_map_head, lde_map, entry, lde_map_compare)
65
66 struct ldpd_conf *ldeconf;
67 struct nbr_tree lde_nbrs = RB_INITIALIZER(&lde_nbrs);
68
69 static struct imsgev *iev_ldpe;
70 static struct imsgev *iev_main, *iev_main_sync;
71
72 /* Master of threads. */
73 struct thread_master *master;
74
75 /* lde privileges */
76 static zebra_capabilities_t _caps_p [] =
77 {
78 /* none */
79 };
80
81 static struct zebra_privs_t lde_privs =
82 {
83 #if defined(VTY_GROUP)
84 .vty_group = VTY_GROUP,
85 #endif
86 .caps_p = _caps_p,
87 .cap_num_p = array_size(_caps_p),
88 .cap_num_i = 0
89 };
90
91 /* List of chunks of labels externally assigned by Zebra */
92 struct list *label_chunk_list;
93 struct listnode *current_label_chunk;
94
95 /* SIGINT / SIGTERM handler. */
96 static void
97 sigint(void)
98 {
99 lde_shutdown();
100 }
101
102 static struct quagga_signal_t lde_signals[] =
103 {
104 {
105 .signal = SIGHUP,
106 /* ignore */
107 },
108 {
109 .signal = SIGINT,
110 .handler = &sigint,
111 },
112 {
113 .signal = SIGTERM,
114 .handler = &sigint,
115 },
116 };
117
118 struct zclient *zclient_sync = NULL;
119 static void
120 zclient_sync_init(u_short instance)
121 {
122 /* Initialize special zclient for synchronous message exchanges. */
123 log_debug("Initializing synchronous zclient for label manager");
124 zclient_sync = zclient_new(master);
125 zclient_sync->sock = -1;
126 zclient_sync->redist_default = ZEBRA_ROUTE_LDP;
127 zclient_sync->instance = instance;
128 while (zclient_socket_connect (zclient_sync) < 0) {
129 log_warnx("Error connecting synchronous zclient!");
130 sleep(1);
131 }
132 /* make socket non-blocking */
133 sock_set_nonblock(zclient_sync->sock);
134
135 /* Connect to label manager */
136 while (lm_label_manager_connect (zclient_sync) != 0) {
137 log_warnx("Error connecting to label manager!");
138 sleep(1);
139 }
140 }
141
142 /* label decision engine */
143 void
144 lde(void)
145 {
146 struct thread thread;
147
148 #ifdef HAVE_SETPROCTITLE
149 setproctitle("label decision engine");
150 #endif
151 ldpd_process = PROC_LDE_ENGINE;
152 log_procname = log_procnames[PROC_LDE_ENGINE];
153
154 master = thread_master_create();
155
156 /* setup signal handler */
157 signal_init(master, array_size(lde_signals), lde_signals);
158
159 /* setup pipes and event handlers to the parent process */
160 if ((iev_main = calloc(1, sizeof(struct imsgev))) == NULL)
161 fatal(NULL);
162 imsg_init(&iev_main->ibuf, LDPD_FD_ASYNC);
163 iev_main->handler_read = lde_dispatch_parent;
164 iev_main->ev_read = thread_add_read(master, iev_main->handler_read,
165 iev_main, iev_main->ibuf.fd);
166 iev_main->handler_write = ldp_write_handler;
167
168 if ((iev_main_sync = calloc(1, sizeof(struct imsgev))) == NULL)
169 fatal(NULL);
170 imsg_init(&iev_main_sync->ibuf, LDPD_FD_SYNC);
171
172 /* create base configuration */
173 ldeconf = config_new_empty();
174
175 /* Fetch next active thread. */
176 while (thread_fetch(master, &thread))
177 thread_call(&thread);
178 }
179
180 void
181 lde_init(struct ldpd_init *init)
182 {
183 /* drop privileges */
184 lde_privs.user = init->user;
185 lde_privs.group = init->group;
186 zprivs_init(&lde_privs);
187
188 #ifdef HAVE_PLEDGE
189 if (pledge("stdio recvfd unix", NULL) == -1)
190 fatal("pledge");
191 #endif
192
193 /* start the LIB garbage collector */
194 lde_gc_start_timer();
195
196 /* Init synchronous zclient and label list */
197 zclient_serv_path_set(init->zclient_serv_path);
198 zclient_sync_init(init->instance);
199 lde_label_list_init();
200 }
201
202 static void
203 lde_shutdown(void)
204 {
205 /* close pipes */
206 if (iev_ldpe) {
207 msgbuf_clear(&iev_ldpe->ibuf.w);
208 close(iev_ldpe->ibuf.fd);
209 }
210 msgbuf_clear(&iev_main->ibuf.w);
211 close(iev_main->ibuf.fd);
212 msgbuf_clear(&iev_main_sync->ibuf.w);
213 close(iev_main_sync->ibuf.fd);
214
215 lde_gc_stop_timer();
216 lde_nbr_clear();
217 fec_tree_clear();
218
219 config_clear(ldeconf);
220
221 if (iev_ldpe)
222 free(iev_ldpe);
223 free(iev_main);
224 free(iev_main_sync);
225
226 log_info("label decision engine exiting");
227 exit(0);
228 }
229
230 /* imesg */
231 int
232 lde_imsg_compose_parent(int type, pid_t pid, void *data, uint16_t datalen)
233 {
234 return (imsg_compose_event(iev_main, type, 0, pid, -1, data, datalen));
235 }
236
237 void
238 lde_imsg_compose_parent_sync(int type, pid_t pid, void *data, uint16_t datalen)
239 {
240 imsg_compose_event(iev_main_sync, type, 0, pid, -1, data, datalen);
241 imsg_flush(&iev_main_sync->ibuf);
242 }
243
244 int
245 lde_imsg_compose_ldpe(int type, uint32_t peerid, pid_t pid, void *data,
246 uint16_t datalen)
247 {
248 return (imsg_compose_event(iev_ldpe, type, peerid, pid,
249 -1, data, datalen));
250 }
251
252 /* ARGSUSED */
253 static int
254 lde_dispatch_imsg(struct thread *thread)
255 {
256 struct imsgev *iev = THREAD_ARG(thread);
257 struct imsgbuf *ibuf = &iev->ibuf;
258 struct imsg imsg;
259 struct lde_nbr *ln;
260 struct map *map;
261 struct lde_addr *lde_addr;
262 struct notify_msg *nm;
263 ssize_t n;
264 int shut = 0;
265
266 iev->ev_read = NULL;
267
268 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
269 fatal("imsg_read error");
270 if (n == 0) /* connection closed */
271 shut = 1;
272
273 for (;;) {
274 if ((n = imsg_get(ibuf, &imsg)) == -1)
275 fatal("lde_dispatch_imsg: imsg_get error");
276 if (n == 0)
277 break;
278
279 switch (imsg.hdr.type) {
280 case IMSG_LABEL_MAPPING_FULL:
281 ln = lde_nbr_find(imsg.hdr.peerid);
282 if (ln == NULL) {
283 log_debug("%s: cannot find lde neighbor",
284 __func__);
285 break;
286 }
287
288 fec_snap(ln);
289 break;
290 case IMSG_LABEL_MAPPING:
291 case IMSG_LABEL_REQUEST:
292 case IMSG_LABEL_RELEASE:
293 case IMSG_LABEL_WITHDRAW:
294 case IMSG_LABEL_ABORT:
295 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
296 sizeof(struct map))
297 fatalx("lde_dispatch_imsg: wrong imsg len");
298 map = imsg.data;
299
300 ln = lde_nbr_find(imsg.hdr.peerid);
301 if (ln == NULL) {
302 log_debug("%s: cannot find lde neighbor",
303 __func__);
304 break;
305 }
306
307 switch (imsg.hdr.type) {
308 case IMSG_LABEL_MAPPING:
309 lde_check_mapping(map, ln);
310 break;
311 case IMSG_LABEL_REQUEST:
312 lde_check_request(map, ln);
313 break;
314 case IMSG_LABEL_RELEASE:
315 lde_check_release(map, ln);
316 break;
317 case IMSG_LABEL_WITHDRAW:
318 lde_check_withdraw(map, ln);
319 break;
320 case IMSG_LABEL_ABORT:
321 /* not necessary */
322 break;
323 }
324 break;
325 case IMSG_ADDRESS_ADD:
326 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
327 sizeof(struct lde_addr))
328 fatalx("lde_dispatch_imsg: wrong imsg len");
329 lde_addr = imsg.data;
330
331 ln = lde_nbr_find(imsg.hdr.peerid);
332 if (ln == NULL) {
333 log_debug("%s: cannot find lde neighbor",
334 __func__);
335 break;
336 }
337 if (lde_address_add(ln, lde_addr) < 0) {
338 log_debug("%s: cannot add address %s, it "
339 "already exists", __func__,
340 log_addr(lde_addr->af, &lde_addr->addr));
341 }
342 break;
343 case IMSG_ADDRESS_DEL:
344 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
345 sizeof(struct lde_addr))
346 fatalx("lde_dispatch_imsg: wrong imsg len");
347 lde_addr = imsg.data;
348
349 ln = lde_nbr_find(imsg.hdr.peerid);
350 if (ln == NULL) {
351 log_debug("%s: cannot find lde neighbor",
352 __func__);
353 break;
354 }
355 if (lde_address_del(ln, lde_addr) < 0) {
356 log_debug("%s: cannot delete address %s, it "
357 "does not exist", __func__,
358 log_addr(lde_addr->af, &lde_addr->addr));
359 }
360 break;
361 case IMSG_NOTIFICATION:
362 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
363 sizeof(struct notify_msg))
364 fatalx("lde_dispatch_imsg: wrong imsg len");
365 nm = imsg.data;
366
367 ln = lde_nbr_find(imsg.hdr.peerid);
368 if (ln == NULL) {
369 log_debug("%s: cannot find lde neighbor",
370 __func__);
371 break;
372 }
373
374 switch (nm->status_code) {
375 case S_PW_STATUS:
376 l2vpn_recv_pw_status(ln, nm);
377 break;
378 case S_ENDOFLIB:
379 /*
380 * Do nothing for now. Should be useful in
381 * the future when we implement LDP-IGP
382 * Synchronization (RFC 5443) and Graceful
383 * Restart (RFC 3478).
384 */
385 default:
386 break;
387 }
388 break;
389 case IMSG_NEIGHBOR_UP:
390 if (imsg.hdr.len - IMSG_HEADER_SIZE !=
391 sizeof(struct lde_nbr))
392 fatalx("lde_dispatch_imsg: wrong imsg len");
393
394 if (lde_nbr_find(imsg.hdr.peerid))
395 fatalx("lde_dispatch_imsg: "
396 "neighbor already exists");
397 lde_nbr_new(imsg.hdr.peerid, imsg.data);
398 break;
399 case IMSG_NEIGHBOR_DOWN:
400 lde_nbr_del(lde_nbr_find(imsg.hdr.peerid));
401 break;
402 case IMSG_CTL_SHOW_LIB:
403 rt_dump(imsg.hdr.pid);
404
405 lde_imsg_compose_ldpe(IMSG_CTL_END, 0,
406 imsg.hdr.pid, NULL, 0);
407 break;
408 case IMSG_CTL_SHOW_L2VPN_PW:
409 l2vpn_pw_ctl(imsg.hdr.pid);
410
411 lde_imsg_compose_ldpe(IMSG_CTL_END, 0,
412 imsg.hdr.pid, NULL, 0);
413 break;
414 case IMSG_CTL_SHOW_L2VPN_BINDING:
415 l2vpn_binding_ctl(imsg.hdr.pid);
416
417 lde_imsg_compose_ldpe(IMSG_CTL_END, 0,
418 imsg.hdr.pid, NULL, 0);
419 break;
420 default:
421 log_debug("%s: unexpected imsg %d", __func__,
422 imsg.hdr.type);
423 break;
424 }
425 imsg_free(&imsg);
426 }
427 if (!shut)
428 imsg_event_add(iev);
429 else {
430 /* this pipe is dead, so remove the event handlers and exit */
431 THREAD_READ_OFF(iev->ev_read);
432 THREAD_WRITE_OFF(iev->ev_write);
433 lde_shutdown();
434 }
435
436 return (0);
437 }
438
439 /* ARGSUSED */
440 static int
441 lde_dispatch_parent(struct thread *thread)
442 {
443 static struct ldpd_conf *nconf;
444 struct iface *iface, *niface;
445 struct tnbr *ntnbr;
446 struct nbr_params *nnbrp;
447 static struct l2vpn *l2vpn, *nl2vpn;
448 struct l2vpn_if *lif, *nlif;
449 struct l2vpn_pw *pw, *npw;
450 struct imsg imsg;
451 struct kif *kif;
452 struct kroute *kr;
453 int fd = THREAD_FD(thread);
454 struct imsgev *iev = THREAD_ARG(thread);
455 struct imsgbuf *ibuf = &iev->ibuf;
456 ssize_t n;
457 int shut = 0;
458 struct fec fec;
459
460 iev->ev_read = NULL;
461
462 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
463 fatal("imsg_read error");
464 if (n == 0) /* connection closed */
465 shut = 1;
466
467 for (;;) {
468 if ((n = imsg_get(ibuf, &imsg)) == -1)
469 fatal("lde_dispatch_parent: imsg_get error");
470 if (n == 0)
471 break;
472
473 switch (imsg.hdr.type) {
474 case IMSG_IFSTATUS:
475 if (imsg.hdr.len != IMSG_HEADER_SIZE +
476 sizeof(struct kif))
477 fatalx("IFSTATUS imsg with wrong len");
478 kif = imsg.data;
479
480 iface = if_lookup_name(ldeconf, kif->ifname);
481 if (iface) {
482 if_update_info(iface, kif);
483 break;
484 }
485
486 RB_FOREACH(l2vpn, l2vpn_head, &ldeconf->l2vpn_tree) {
487 lif = l2vpn_if_find(l2vpn, kif->ifname);
488 if (lif) {
489 l2vpn_if_update_info(lif, kif);
490 break;
491 }
492 pw = l2vpn_pw_find(l2vpn, kif->ifname);
493 if (pw) {
494 l2vpn_pw_update_info(pw, kif);
495 break;
496 }
497 }
498 break;
499 case IMSG_NETWORK_ADD:
500 case IMSG_NETWORK_UPDATE:
501 if (imsg.hdr.len != IMSG_HEADER_SIZE +
502 sizeof(struct kroute)) {
503 log_warnx("%s: wrong imsg len", __func__);
504 break;
505 }
506 kr = imsg.data;
507
508 switch (kr->af) {
509 case AF_INET:
510 fec.type = FEC_TYPE_IPV4;
511 fec.u.ipv4.prefix = kr->prefix.v4;
512 fec.u.ipv4.prefixlen = kr->prefixlen;
513 break;
514 case AF_INET6:
515 fec.type = FEC_TYPE_IPV6;
516 fec.u.ipv6.prefix = kr->prefix.v6;
517 fec.u.ipv6.prefixlen = kr->prefixlen;
518 break;
519 default:
520 fatalx("lde_dispatch_parent: unknown af");
521 }
522
523 switch (imsg.hdr.type) {
524 case IMSG_NETWORK_ADD:
525 lde_kernel_insert(&fec, kr->af, &kr->nexthop,
526 kr->ifindex, kr->priority,
527 kr->flags & F_CONNECTED, NULL);
528 break;
529 case IMSG_NETWORK_UPDATE:
530 lde_kernel_update(&fec);
531 break;
532 }
533 break;
534 case IMSG_SOCKET_IPC:
535 if (iev_ldpe) {
536 log_warnx("%s: received unexpected imsg fd "
537 "to ldpe", __func__);
538 break;
539 }
540 if ((fd = imsg.fd) == -1) {
541 log_warnx("%s: expected to receive imsg fd to "
542 "ldpe but didn't receive any", __func__);
543 break;
544 }
545
546 if ((iev_ldpe = malloc(sizeof(struct imsgev))) == NULL)
547 fatal(NULL);
548 imsg_init(&iev_ldpe->ibuf, fd);
549 iev_ldpe->handler_read = lde_dispatch_imsg;
550 iev_ldpe->ev_read = thread_add_read(master,
551 iev_ldpe->handler_read, iev_ldpe, iev_ldpe->ibuf.fd);
552 iev_ldpe->handler_write = ldp_write_handler;
553 iev_ldpe->ev_write = NULL;
554 break;
555 case IMSG_INIT:
556 if (imsg.hdr.len != IMSG_HEADER_SIZE +
557 sizeof(struct ldpd_init))
558 fatalx("INIT imsg with wrong len");
559
560 memcpy(&init, imsg.data, sizeof(init));
561 lde_init(&init);
562 break;
563 case IMSG_RECONF_CONF:
564 if ((nconf = malloc(sizeof(struct ldpd_conf))) ==
565 NULL)
566 fatal(NULL);
567 memcpy(nconf, imsg.data, sizeof(struct ldpd_conf));
568
569 RB_INIT(&nconf->iface_tree);
570 RB_INIT(&nconf->tnbr_tree);
571 RB_INIT(&nconf->nbrp_tree);
572 RB_INIT(&nconf->l2vpn_tree);
573 break;
574 case IMSG_RECONF_IFACE:
575 if ((niface = malloc(sizeof(struct iface))) == NULL)
576 fatal(NULL);
577 memcpy(niface, imsg.data, sizeof(struct iface));
578
579 RB_INSERT(iface_head, &nconf->iface_tree, niface);
580 break;
581 case IMSG_RECONF_TNBR:
582 if ((ntnbr = malloc(sizeof(struct tnbr))) == NULL)
583 fatal(NULL);
584 memcpy(ntnbr, imsg.data, sizeof(struct tnbr));
585
586 RB_INSERT(tnbr_head, &nconf->tnbr_tree, ntnbr);
587 break;
588 case IMSG_RECONF_NBRP:
589 if ((nnbrp = malloc(sizeof(struct nbr_params))) == NULL)
590 fatal(NULL);
591 memcpy(nnbrp, imsg.data, sizeof(struct nbr_params));
592
593 RB_INSERT(nbrp_head, &nconf->nbrp_tree, nnbrp);
594 break;
595 case IMSG_RECONF_L2VPN:
596 if ((nl2vpn = malloc(sizeof(struct l2vpn))) == NULL)
597 fatal(NULL);
598 memcpy(nl2vpn, imsg.data, sizeof(struct l2vpn));
599
600 RB_INIT(&nl2vpn->if_tree);
601 RB_INIT(&nl2vpn->pw_tree);
602 RB_INIT(&nl2vpn->pw_inactive_tree);
603
604 RB_INSERT(l2vpn_head, &nconf->l2vpn_tree, nl2vpn);
605 break;
606 case IMSG_RECONF_L2VPN_IF:
607 if ((nlif = malloc(sizeof(struct l2vpn_if))) == NULL)
608 fatal(NULL);
609 memcpy(nlif, imsg.data, sizeof(struct l2vpn_if));
610
611 RB_INSERT(l2vpn_if_head, &nl2vpn->if_tree, nlif);
612 break;
613 case IMSG_RECONF_L2VPN_PW:
614 if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL)
615 fatal(NULL);
616 memcpy(npw, imsg.data, sizeof(struct l2vpn_pw));
617
618 RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_tree, npw);
619 break;
620 case IMSG_RECONF_L2VPN_IPW:
621 if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL)
622 fatal(NULL);
623 memcpy(npw, imsg.data, sizeof(struct l2vpn_pw));
624
625 RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_inactive_tree, npw);
626 break;
627 case IMSG_RECONF_END:
628 merge_config(ldeconf, nconf);
629 ldp_clear_config(nconf);
630 nconf = NULL;
631 break;
632 case IMSG_DEBUG_UPDATE:
633 if (imsg.hdr.len != IMSG_HEADER_SIZE +
634 sizeof(ldp_debug)) {
635 log_warnx("%s: wrong imsg len", __func__);
636 break;
637 }
638 memcpy(&ldp_debug, imsg.data, sizeof(ldp_debug));
639 break;
640 default:
641 log_debug("%s: unexpected imsg %d", __func__,
642 imsg.hdr.type);
643 break;
644 }
645 imsg_free(&imsg);
646 }
647 if (!shut)
648 imsg_event_add(iev);
649 else {
650 /* this pipe is dead, so remove the event handlers and exit */
651 THREAD_READ_OFF(iev->ev_read);
652 THREAD_WRITE_OFF(iev->ev_write);
653 lde_shutdown();
654 }
655
656 return (0);
657 }
658
659 int
660 lde_acl_check(char *acl_name, int af, union ldpd_addr *addr, uint8_t prefixlen)
661 {
662 return ldp_acl_request(iev_main_sync, acl_name, af, addr, prefixlen);
663 }
664
665 uint32_t
666 lde_update_label(struct fec_node *fn)
667 {
668 struct fec_nh *fnh;
669 int connected = 0;
670
671 LIST_FOREACH(fnh, &fn->nexthops, entry) {
672 if (fnh->flags & F_FEC_NH_CONNECTED) {
673 connected = 1;
674 break;
675 }
676 }
677
678 /* should we allocate a label for this fec? */
679 switch (fn->fec.type) {
680 case FEC_TYPE_IPV4:
681 if ((ldeconf->ipv4.flags & F_LDPD_AF_ALLOCHOSTONLY) &&
682 fn->fec.u.ipv4.prefixlen != 32)
683 return (NO_LABEL);
684 if (lde_acl_check(ldeconf->ipv4.acl_label_allocate_for,
685 AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix,
686 fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT)
687 return (NO_LABEL);
688 break;
689 case FEC_TYPE_IPV6:
690 if ((ldeconf->ipv6.flags & F_LDPD_AF_ALLOCHOSTONLY) &&
691 fn->fec.u.ipv6.prefixlen != 128)
692 return (NO_LABEL);
693 if (lde_acl_check(ldeconf->ipv6.acl_label_allocate_for,
694 AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix,
695 fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT)
696 return (NO_LABEL);
697 break;
698 default:
699 break;
700 }
701
702 if (connected) {
703 /* choose implicit or explicit-null depending on configuration */
704 switch (fn->fec.type) {
705 case FEC_TYPE_IPV4:
706 if (!(ldeconf->ipv4.flags & F_LDPD_AF_EXPNULL))
707 return (MPLS_LABEL_IMPLNULL);
708 if (lde_acl_check(ldeconf->ipv4.acl_label_expnull_for,
709 AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix,
710 fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT)
711 return (MPLS_LABEL_IMPLNULL);
712 return (MPLS_LABEL_IPV4NULL);
713 case FEC_TYPE_IPV6:
714 if (!(ldeconf->ipv6.flags & F_LDPD_AF_EXPNULL))
715 return (MPLS_LABEL_IMPLNULL);
716 if (lde_acl_check(ldeconf->ipv6.acl_label_expnull_for,
717 AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix,
718 fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT)
719 return (MPLS_LABEL_IMPLNULL);
720 return (MPLS_LABEL_IPV6NULL);
721 default:
722 fatalx("lde_update_label: unexpected fec type");
723 break;
724 }
725 }
726
727 /* preserve current label if there's no need to update it */
728 if (fn->local_label != NO_LABEL &&
729 fn->local_label > MPLS_LABEL_RESERVED_MAX)
730 return (fn->local_label);
731
732 return lde_get_next_label ();
733 }
734
735 void
736 lde_send_change_klabel(struct fec_node *fn, struct fec_nh *fnh)
737 {
738 struct kroute kr;
739 struct kpw kpw;
740 struct l2vpn_pw *pw;
741
742 switch (fn->fec.type) {
743 case FEC_TYPE_IPV4:
744 memset(&kr, 0, sizeof(kr));
745 kr.af = AF_INET;
746 kr.prefix.v4 = fn->fec.u.ipv4.prefix;
747 kr.prefixlen = fn->fec.u.ipv4.prefixlen;
748 kr.nexthop.v4 = fnh->nexthop.v4;
749 kr.ifindex = fnh->ifindex;
750 kr.local_label = fn->local_label;
751 kr.remote_label = fnh->remote_label;
752 kr.priority = fnh->priority;
753
754 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE, 0, &kr,
755 sizeof(kr));
756 break;
757 case FEC_TYPE_IPV6:
758 memset(&kr, 0, sizeof(kr));
759 kr.af = AF_INET6;
760 kr.prefix.v6 = fn->fec.u.ipv6.prefix;
761 kr.prefixlen = fn->fec.u.ipv6.prefixlen;
762 kr.nexthop.v6 = fnh->nexthop.v6;
763 kr.ifindex = fnh->ifindex;
764 kr.local_label = fn->local_label;
765 kr.remote_label = fnh->remote_label;
766 kr.priority = fnh->priority;
767
768 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE, 0, &kr,
769 sizeof(kr));
770 break;
771 case FEC_TYPE_PWID:
772 if (fn->local_label == NO_LABEL ||
773 fnh->remote_label == NO_LABEL)
774 return;
775
776 pw = (struct l2vpn_pw *) fn->data;
777 pw->flags |= F_PW_STATUS_UP;
778
779 memset(&kpw, 0, sizeof(kpw));
780 kpw.ifindex = pw->ifindex;
781 kpw.pw_type = fn->fec.u.pwid.type;
782 kpw.af = pw->af;
783 kpw.nexthop = pw->addr;
784 kpw.local_label = fn->local_label;
785 kpw.remote_label = fnh->remote_label;
786 kpw.flags = pw->flags;
787
788 lde_imsg_compose_parent(IMSG_KPWLABEL_CHANGE, 0, &kpw,
789 sizeof(kpw));
790 break;
791 }
792 }
793
794 void
795 lde_send_delete_klabel(struct fec_node *fn, struct fec_nh *fnh)
796 {
797 struct kroute kr;
798 struct kpw kpw;
799 struct l2vpn_pw *pw;
800
801 switch (fn->fec.type) {
802 case FEC_TYPE_IPV4:
803 memset(&kr, 0, sizeof(kr));
804 kr.af = AF_INET;
805 kr.prefix.v4 = fn->fec.u.ipv4.prefix;
806 kr.prefixlen = fn->fec.u.ipv4.prefixlen;
807 kr.nexthop.v4 = fnh->nexthop.v4;
808 kr.ifindex = fnh->ifindex;
809 kr.local_label = fn->local_label;
810 kr.remote_label = fnh->remote_label;
811 kr.priority = fnh->priority;
812
813 lde_imsg_compose_parent(IMSG_KLABEL_DELETE, 0, &kr,
814 sizeof(kr));
815 break;
816 case FEC_TYPE_IPV6:
817 memset(&kr, 0, sizeof(kr));
818 kr.af = AF_INET6;
819 kr.prefix.v6 = fn->fec.u.ipv6.prefix;
820 kr.prefixlen = fn->fec.u.ipv6.prefixlen;
821 kr.nexthop.v6 = fnh->nexthop.v6;
822 kr.ifindex = fnh->ifindex;
823 kr.local_label = fn->local_label;
824 kr.remote_label = fnh->remote_label;
825 kr.priority = fnh->priority;
826
827 lde_imsg_compose_parent(IMSG_KLABEL_DELETE, 0, &kr,
828 sizeof(kr));
829 break;
830 case FEC_TYPE_PWID:
831 pw = (struct l2vpn_pw *) fn->data;
832 if (!(pw->flags & F_PW_STATUS_UP))
833 return;
834 pw->flags &= ~F_PW_STATUS_UP;
835
836 memset(&kpw, 0, sizeof(kpw));
837 kpw.ifindex = pw->ifindex;
838 kpw.pw_type = fn->fec.u.pwid.type;
839 kpw.af = pw->af;
840 kpw.nexthop = pw->addr;
841 kpw.local_label = fn->local_label;
842 kpw.remote_label = fnh->remote_label;
843 kpw.flags = pw->flags;
844
845 lde_imsg_compose_parent(IMSG_KPWLABEL_DELETE, 0, &kpw,
846 sizeof(kpw));
847 break;
848 }
849 }
850
851 void
852 lde_fec2map(struct fec *fec, struct map *map)
853 {
854 memset(map, 0, sizeof(*map));
855
856 switch (fec->type) {
857 case FEC_TYPE_IPV4:
858 map->type = MAP_TYPE_PREFIX;
859 map->fec.prefix.af = AF_INET;
860 map->fec.prefix.prefix.v4 = fec->u.ipv4.prefix;
861 map->fec.prefix.prefixlen = fec->u.ipv4.prefixlen;
862 break;
863 case FEC_TYPE_IPV6:
864 map->type = MAP_TYPE_PREFIX;
865 map->fec.prefix.af = AF_INET6;
866 map->fec.prefix.prefix.v6 = fec->u.ipv6.prefix;
867 map->fec.prefix.prefixlen = fec->u.ipv6.prefixlen;
868 break;
869 case FEC_TYPE_PWID:
870 map->type = MAP_TYPE_PWID;
871 map->fec.pwid.type = fec->u.pwid.type;
872 map->fec.pwid.group_id = 0;
873 map->flags |= F_MAP_PW_ID;
874 map->fec.pwid.pwid = fec->u.pwid.pwid;
875 break;
876 }
877 }
878
879 void
880 lde_map2fec(struct map *map, struct in_addr lsr_id, struct fec *fec)
881 {
882 memset(fec, 0, sizeof(*fec));
883
884 switch (map->type) {
885 case MAP_TYPE_PREFIX:
886 switch (map->fec.prefix.af) {
887 case AF_INET:
888 fec->type = FEC_TYPE_IPV4;
889 fec->u.ipv4.prefix = map->fec.prefix.prefix.v4;
890 fec->u.ipv4.prefixlen = map->fec.prefix.prefixlen;
891 break;
892 case AF_INET6:
893 fec->type = FEC_TYPE_IPV6;
894 fec->u.ipv6.prefix = map->fec.prefix.prefix.v6;
895 fec->u.ipv6.prefixlen = map->fec.prefix.prefixlen;
896 break;
897 default:
898 fatalx("lde_map2fec: unknown af");
899 break;
900 }
901 break;
902 case MAP_TYPE_PWID:
903 fec->type = FEC_TYPE_PWID;
904 fec->u.pwid.type = map->fec.pwid.type;
905 fec->u.pwid.pwid = map->fec.pwid.pwid;
906 fec->u.pwid.lsr_id = lsr_id;
907 break;
908 }
909 }
910
911 void
912 lde_send_labelmapping(struct lde_nbr *ln, struct fec_node *fn, int single)
913 {
914 struct lde_wdraw *lw;
915 struct lde_map *me;
916 struct lde_req *lre;
917 struct map map;
918 struct l2vpn_pw *pw;
919
920 /*
921 * We shouldn't send a new label mapping if we have a pending
922 * label release to receive. In this case, schedule to send a
923 * label mapping as soon as a label release is received.
924 */
925 lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, &fn->fec);
926 if (lw) {
927 if (!fec_find(&ln->sent_map_pending, &fn->fec))
928 lde_map_pending_add(ln, fn);
929 return;
930 }
931
932 /*
933 * This function skips SL.1 - 3 and SL.9 - 14 because the label
934 * allocation is done way earlier (because of the merging nature of
935 * ldpd).
936 */
937
938 lde_fec2map(&fn->fec, &map);
939 switch (fn->fec.type) {
940 case FEC_TYPE_IPV4:
941 if (!ln->v4_enabled)
942 return;
943 if (lde_acl_check(ldeconf->ipv4.acl_label_advertise_to,
944 AF_INET, (union ldpd_addr *)&ln->id, 32) != FILTER_PERMIT)
945 return;
946 if (lde_acl_check(ldeconf->ipv4.acl_label_advertise_for,
947 AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix,
948 fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT)
949 return;
950 break;
951 case FEC_TYPE_IPV6:
952 if (!ln->v6_enabled)
953 return;
954 if (lde_acl_check(ldeconf->ipv6.acl_label_advertise_to,
955 AF_INET, (union ldpd_addr *)&ln->id, 32) != FILTER_PERMIT)
956 return;
957 if (lde_acl_check(ldeconf->ipv6.acl_label_advertise_for,
958 AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix,
959 fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT)
960 return;
961 break;
962 case FEC_TYPE_PWID:
963 pw = (struct l2vpn_pw *) fn->data;
964 if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr)
965 /* not the remote end of the pseudowire */
966 return;
967
968 map.flags |= F_MAP_PW_IFMTU;
969 map.fec.pwid.ifmtu = pw->l2vpn->mtu;
970 if (pw->flags & F_PW_CWORD)
971 map.flags |= F_MAP_PW_CWORD;
972 if (pw->flags & F_PW_STATUSTLV) {
973 map.flags |= F_MAP_PW_STATUS;
974 /* VPLS are always up */
975 map.pw_status = PW_FORWARDING;
976 }
977 break;
978 }
979 map.label = fn->local_label;
980
981 /* SL.6: is there a pending request for this mapping? */
982 lre = (struct lde_req *)fec_find(&ln->recv_req, &fn->fec);
983 if (lre) {
984 /* set label request msg id in the mapping response. */
985 map.requestid = lre->msg_id;
986 map.flags = F_MAP_REQ_ID;
987
988 /* SL.7: delete record of pending request */
989 lde_req_del(ln, lre, 0);
990 }
991
992 /* SL.4: send label mapping */
993 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD, ln->peerid, 0,
994 &map, sizeof(map));
995 if (single)
996 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0,
997 NULL, 0);
998
999 /* SL.5: record sent label mapping */
1000 me = (struct lde_map *)fec_find(&ln->sent_map, &fn->fec);
1001 if (me == NULL)
1002 me = lde_map_add(ln, fn, 1);
1003 me->map = map;
1004 }
1005
1006 void
1007 lde_send_labelwithdraw(struct lde_nbr *ln, struct fec_node *fn,
1008 struct map *wcard, struct status_tlv *st)
1009 {
1010 struct lde_wdraw *lw;
1011 struct map map;
1012 struct fec *f;
1013 struct l2vpn_pw *pw;
1014
1015 if (fn) {
1016 lde_fec2map(&fn->fec, &map);
1017 switch (fn->fec.type) {
1018 case FEC_TYPE_IPV4:
1019 if (!ln->v4_enabled)
1020 return;
1021 break;
1022 case FEC_TYPE_IPV6:
1023 if (!ln->v6_enabled)
1024 return;
1025 break;
1026 case FEC_TYPE_PWID:
1027 pw = (struct l2vpn_pw *) fn->data;
1028 if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr)
1029 /* not the remote end of the pseudowire */
1030 return;
1031
1032 if (pw->flags & F_PW_CWORD)
1033 map.flags |= F_MAP_PW_CWORD;
1034 break;
1035 }
1036 map.label = fn->local_label;
1037 } else
1038 memcpy(&map, wcard, sizeof(map));
1039
1040 if (st) {
1041 map.st.status_code = st->status_code;
1042 map.st.msg_id = st->msg_id;
1043 map.st.msg_type = st->msg_type;
1044 map.flags |= F_MAP_STATUS;
1045 }
1046
1047 /* SWd.1: send label withdraw. */
1048 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD, ln->peerid, 0,
1049 &map, sizeof(map));
1050 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD_END, ln->peerid, 0, NULL, 0);
1051
1052 /* SWd.2: record label withdraw. */
1053 if (fn) {
1054 lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw, &fn->fec);
1055 if (lw == NULL)
1056 lw = lde_wdraw_add(ln, fn);
1057 lw->label = map.label;
1058 } else {
1059 struct lde_map *me;
1060
1061 RB_FOREACH(f, fec_tree, &ft) {
1062 fn = (struct fec_node *)f;
1063 me = (struct lde_map *)fec_find(&ln->sent_map, &fn->fec);
1064 if (lde_wildcard_apply(wcard, &fn->fec, me) == 0)
1065 continue;
1066
1067 lw = (struct lde_wdraw *)fec_find(&ln->sent_wdraw,
1068 &fn->fec);
1069 if (lw == NULL)
1070 lw = lde_wdraw_add(ln, fn);
1071 lw->label = map.label;
1072 }
1073 }
1074 }
1075
1076 void
1077 lde_send_labelwithdraw_wcard(struct lde_nbr *ln, uint32_t label)
1078 {
1079 struct map wcard;
1080
1081 memset(&wcard, 0, sizeof(wcard));
1082 wcard.type = MAP_TYPE_WILDCARD;
1083 wcard.label = label;
1084 lde_send_labelwithdraw(ln, NULL, &wcard, NULL);
1085 }
1086
1087 void
1088 lde_send_labelwithdraw_twcard_prefix(struct lde_nbr *ln, uint16_t af,
1089 uint32_t label)
1090 {
1091 struct map wcard;
1092
1093 memset(&wcard, 0, sizeof(wcard));
1094 wcard.type = MAP_TYPE_TYPED_WCARD;
1095 wcard.fec.twcard.type = MAP_TYPE_PREFIX;
1096 wcard.fec.twcard.u.prefix_af = af;
1097 wcard.label = label;
1098 lde_send_labelwithdraw(ln, NULL, &wcard, NULL);
1099 }
1100
1101 void
1102 lde_send_labelwithdraw_twcard_pwid(struct lde_nbr *ln, uint16_t pw_type,
1103 uint32_t label)
1104 {
1105 struct map wcard;
1106
1107 memset(&wcard, 0, sizeof(wcard));
1108 wcard.type = MAP_TYPE_TYPED_WCARD;
1109 wcard.fec.twcard.type = MAP_TYPE_PWID;
1110 wcard.fec.twcard.u.pw_type = pw_type;
1111 wcard.label = label;
1112 lde_send_labelwithdraw(ln, NULL, &wcard, NULL);
1113 }
1114
1115 void
1116 lde_send_labelwithdraw_pwid_wcard(struct lde_nbr *ln, uint16_t pw_type,
1117 uint32_t group_id)
1118 {
1119 struct map wcard;
1120
1121 memset(&wcard, 0, sizeof(wcard));
1122 wcard.type = MAP_TYPE_PWID;
1123 wcard.fec.pwid.type = pw_type;
1124 wcard.fec.pwid.group_id = group_id;
1125 /* we can not append a Label TLV when using PWid group wildcards. */
1126 wcard.label = NO_LABEL;
1127 lde_send_labelwithdraw(ln, NULL, &wcard, NULL);
1128 }
1129
1130 void
1131 lde_send_labelrelease(struct lde_nbr *ln, struct fec_node *fn,
1132 struct map *wcard, uint32_t label)
1133 {
1134 struct map map;
1135 struct l2vpn_pw *pw;
1136
1137 if (fn) {
1138 lde_fec2map(&fn->fec, &map);
1139 switch (fn->fec.type) {
1140 case FEC_TYPE_IPV4:
1141 if (!ln->v4_enabled)
1142 return;
1143 break;
1144 case FEC_TYPE_IPV6:
1145 if (!ln->v6_enabled)
1146 return;
1147 break;
1148 case FEC_TYPE_PWID:
1149 pw = (struct l2vpn_pw *) fn->data;
1150 if (pw == NULL || pw->lsr_id.s_addr != ln->id.s_addr)
1151 /* not the remote end of the pseudowire */
1152 return;
1153
1154 if (pw->flags & F_PW_CWORD)
1155 map.flags |= F_MAP_PW_CWORD;
1156 break;
1157 }
1158 } else
1159 memcpy(&map, wcard, sizeof(map));
1160 map.label = label;
1161
1162 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD, ln->peerid, 0,
1163 &map, sizeof(map));
1164 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD_END, ln->peerid, 0, NULL, 0);
1165 }
1166
1167 void
1168 lde_send_notification(struct lde_nbr *ln, uint32_t status_code, uint32_t msg_id,
1169 uint16_t msg_type)
1170 {
1171 struct notify_msg nm;
1172
1173 memset(&nm, 0, sizeof(nm));
1174 nm.status_code = status_code;
1175 /* 'msg_id' and 'msg_type' should be in network byte order */
1176 nm.msg_id = msg_id;
1177 nm.msg_type = msg_type;
1178
1179 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0,
1180 &nm, sizeof(nm));
1181 }
1182
1183 void
1184 lde_send_notification_eol_prefix(struct lde_nbr *ln, int af)
1185 {
1186 struct notify_msg nm;
1187
1188 memset(&nm, 0, sizeof(nm));
1189 nm.status_code = S_ENDOFLIB;
1190 nm.fec.type = MAP_TYPE_TYPED_WCARD;
1191 nm.fec.fec.twcard.type = MAP_TYPE_PREFIX;
1192 nm.fec.fec.twcard.u.prefix_af = af;
1193 nm.flags |= F_NOTIF_FEC;
1194
1195 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0,
1196 &nm, sizeof(nm));
1197 }
1198
1199 void
1200 lde_send_notification_eol_pwid(struct lde_nbr *ln, uint16_t pw_type)
1201 {
1202 struct notify_msg nm;
1203
1204 memset(&nm, 0, sizeof(nm));
1205 nm.status_code = S_ENDOFLIB;
1206 nm.fec.type = MAP_TYPE_TYPED_WCARD;
1207 nm.fec.fec.twcard.type = MAP_TYPE_PWID;
1208 nm.fec.fec.twcard.u.pw_type = pw_type;
1209 nm.flags |= F_NOTIF_FEC;
1210
1211 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0,
1212 &nm, sizeof(nm));
1213 }
1214
1215 static __inline int
1216 lde_nbr_compare(struct lde_nbr *a, struct lde_nbr *b)
1217 {
1218 return (a->peerid - b->peerid);
1219 }
1220
1221 static struct lde_nbr *
1222 lde_nbr_new(uint32_t peerid, struct lde_nbr *new)
1223 {
1224 struct lde_nbr *ln;
1225
1226 if ((ln = calloc(1, sizeof(*ln))) == NULL)
1227 fatal(__func__);
1228
1229 ln->id = new->id;
1230 ln->v4_enabled = new->v4_enabled;
1231 ln->v6_enabled = new->v6_enabled;
1232 ln->flags = new->flags;
1233 ln->peerid = peerid;
1234 fec_init(&ln->recv_map);
1235 fec_init(&ln->sent_map);
1236 fec_init(&ln->sent_map_pending);
1237 fec_init(&ln->recv_req);
1238 fec_init(&ln->sent_req);
1239 fec_init(&ln->sent_wdraw);
1240
1241 TAILQ_INIT(&ln->addr_list);
1242
1243 if (RB_INSERT(nbr_tree, &lde_nbrs, ln) != NULL)
1244 fatalx("lde_nbr_new: RB_INSERT failed");
1245
1246 return (ln);
1247 }
1248
1249 static void
1250 lde_nbr_del(struct lde_nbr *ln)
1251 {
1252 struct fec *f;
1253 struct fec_node *fn;
1254 struct fec_nh *fnh;
1255 struct l2vpn_pw *pw;
1256
1257 if (ln == NULL)
1258 return;
1259
1260 /* uninstall received mappings */
1261 RB_FOREACH(f, fec_tree, &ft) {
1262 fn = (struct fec_node *)f;
1263
1264 LIST_FOREACH(fnh, &fn->nexthops, entry) {
1265 switch (f->type) {
1266 case FEC_TYPE_IPV4:
1267 case FEC_TYPE_IPV6:
1268 if (!lde_address_find(ln, fnh->af,
1269 &fnh->nexthop))
1270 continue;
1271 break;
1272 case FEC_TYPE_PWID:
1273 if (f->u.pwid.lsr_id.s_addr != ln->id.s_addr)
1274 continue;
1275 pw = (struct l2vpn_pw *) fn->data;
1276 if (pw)
1277 l2vpn_pw_reset(pw);
1278 break;
1279 default:
1280 break;
1281 }
1282
1283 lde_send_delete_klabel(fn, fnh);
1284 fnh->remote_label = NO_LABEL;
1285 }
1286 }
1287
1288 lde_address_list_free(ln);
1289
1290 fec_clear(&ln->recv_map, lde_map_free);
1291 fec_clear(&ln->sent_map, lde_map_free);
1292 fec_clear(&ln->sent_map_pending, free);
1293 fec_clear(&ln->recv_req, free);
1294 fec_clear(&ln->sent_req, free);
1295 fec_clear(&ln->sent_wdraw, free);
1296
1297 RB_REMOVE(nbr_tree, &lde_nbrs, ln);
1298
1299 free(ln);
1300 }
1301
1302 static struct lde_nbr *
1303 lde_nbr_find(uint32_t peerid)
1304 {
1305 struct lde_nbr ln;
1306
1307 ln.peerid = peerid;
1308
1309 return (RB_FIND(nbr_tree, &lde_nbrs, &ln));
1310 }
1311
1312 struct lde_nbr *
1313 lde_nbr_find_by_lsrid(struct in_addr addr)
1314 {
1315 struct lde_nbr *ln;
1316
1317 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1318 if (ln->id.s_addr == addr.s_addr)
1319 return (ln);
1320
1321 return (NULL);
1322 }
1323
1324 struct lde_nbr *
1325 lde_nbr_find_by_addr(int af, union ldpd_addr *addr)
1326 {
1327 struct lde_nbr *ln;
1328
1329 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1330 if (lde_address_find(ln, af, addr) != NULL)
1331 return (ln);
1332
1333 return (NULL);
1334 }
1335
1336 static void
1337 lde_nbr_clear(void)
1338 {
1339 struct lde_nbr *ln;
1340
1341 while ((ln = RB_ROOT(&lde_nbrs)) != NULL)
1342 lde_nbr_del(ln);
1343 }
1344
1345 static void
1346 lde_nbr_addr_update(struct lde_nbr *ln, struct lde_addr *lde_addr, int removed)
1347 {
1348 struct fec *fec;
1349 struct fec_node *fn;
1350 struct fec_nh *fnh;
1351 struct lde_map *me;
1352
1353 RB_FOREACH(fec, fec_tree, &ln->recv_map) {
1354 fn = (struct fec_node *)fec_find(&ft, fec);
1355 switch (fec->type) {
1356 case FEC_TYPE_IPV4:
1357 if (lde_addr->af != AF_INET)
1358 continue;
1359 break;
1360 case FEC_TYPE_IPV6:
1361 if (lde_addr->af != AF_INET6)
1362 continue;
1363 break;
1364 default:
1365 continue;
1366 }
1367
1368 LIST_FOREACH(fnh, &fn->nexthops, entry) {
1369 if (ldp_addrcmp(fnh->af, &fnh->nexthop,
1370 &lde_addr->addr))
1371 continue;
1372
1373 if (removed) {
1374 lde_send_delete_klabel(fn, fnh);
1375 fnh->remote_label = NO_LABEL;
1376 } else {
1377 me = (struct lde_map *)fec;
1378 fnh->remote_label = me->map.label;
1379 lde_send_change_klabel(fn, fnh);
1380 }
1381 break;
1382 }
1383 }
1384 }
1385
1386 static __inline int
1387 lde_map_compare(struct lde_map *a, struct lde_map *b)
1388 {
1389 return (ldp_addrcmp(AF_INET, (union ldpd_addr *)&a->nexthop->id,
1390 (union ldpd_addr *)&b->nexthop->id));
1391 }
1392
1393 struct lde_map *
1394 lde_map_add(struct lde_nbr *ln, struct fec_node *fn, int sent)
1395 {
1396 struct lde_map *me;
1397
1398 me = calloc(1, sizeof(*me));
1399 if (me == NULL)
1400 fatal(__func__);
1401
1402 me->fec = fn->fec;
1403 me->nexthop = ln;
1404
1405 if (sent) {
1406 RB_INSERT(lde_map_head, &fn->upstream, me);
1407 me->head = &fn->upstream;
1408 if (fec_insert(&ln->sent_map, &me->fec))
1409 log_warnx("failed to add %s to sent map",
1410 log_fec(&me->fec));
1411 /* XXX on failure more cleanup is needed */
1412 } else {
1413 RB_INSERT(lde_map_head, &fn->downstream, me);
1414 me->head = &fn->downstream;
1415 if (fec_insert(&ln->recv_map, &me->fec))
1416 log_warnx("failed to add %s to recv map",
1417 log_fec(&me->fec));
1418 }
1419
1420 return (me);
1421 }
1422
1423 void
1424 lde_map_del(struct lde_nbr *ln, struct lde_map *me, int sent)
1425 {
1426 if (sent)
1427 fec_remove(&ln->sent_map, &me->fec);
1428 else
1429 fec_remove(&ln->recv_map, &me->fec);
1430
1431 lde_map_free(me);
1432 }
1433
1434 static void
1435 lde_map_free(void *ptr)
1436 {
1437 struct lde_map *map = ptr;
1438
1439 RB_REMOVE(lde_map_head, map->head, map);
1440 free(map);
1441 }
1442
1443 struct fec *
1444 lde_map_pending_add(struct lde_nbr *ln, struct fec_node *fn)
1445 {
1446 struct fec *map;
1447
1448 map = calloc(1, sizeof(*map));
1449 if (map == NULL)
1450 fatal(__func__);
1451
1452 *map = fn->fec;
1453 if (fec_insert(&ln->sent_map_pending, map))
1454 log_warnx("failed to add %s to sent map (pending)",
1455 log_fec(map));
1456
1457 return (map);
1458 }
1459
1460 void
1461 lde_map_pending_del(struct lde_nbr *ln, struct fec *map)
1462 {
1463 fec_remove(&ln->sent_map_pending, map);
1464 free(map);
1465 }
1466
1467 struct lde_req *
1468 lde_req_add(struct lde_nbr *ln, struct fec *fec, int sent)
1469 {
1470 struct fec_tree *t;
1471 struct lde_req *lre;
1472
1473 t = sent ? &ln->sent_req : &ln->recv_req;
1474
1475 lre = calloc(1, sizeof(*lre));
1476 if (lre != NULL) {
1477 lre->fec = *fec;
1478
1479 if (fec_insert(t, &lre->fec)) {
1480 log_warnx("failed to add %s to %s req",
1481 log_fec(&lre->fec), sent ? "sent" : "recv");
1482 free(lre);
1483 return (NULL);
1484 }
1485 }
1486
1487 return (lre);
1488 }
1489
1490 void
1491 lde_req_del(struct lde_nbr *ln, struct lde_req *lre, int sent)
1492 {
1493 if (sent)
1494 fec_remove(&ln->sent_req, &lre->fec);
1495 else
1496 fec_remove(&ln->recv_req, &lre->fec);
1497
1498 free(lre);
1499 }
1500
1501 struct lde_wdraw *
1502 lde_wdraw_add(struct lde_nbr *ln, struct fec_node *fn)
1503 {
1504 struct lde_wdraw *lw;
1505
1506 lw = calloc(1, sizeof(*lw));
1507 if (lw == NULL)
1508 fatal(__func__);
1509
1510 lw->fec = fn->fec;
1511
1512 if (fec_insert(&ln->sent_wdraw, &lw->fec))
1513 log_warnx("failed to add %s to sent wdraw",
1514 log_fec(&lw->fec));
1515
1516 return (lw);
1517 }
1518
1519 void
1520 lde_wdraw_del(struct lde_nbr *ln, struct lde_wdraw *lw)
1521 {
1522 fec_remove(&ln->sent_wdraw, &lw->fec);
1523 free(lw);
1524 }
1525
1526 void
1527 lde_change_egress_label(int af)
1528 {
1529 struct lde_nbr *ln;
1530 struct fec *f;
1531 struct fec_node *fn;
1532
1533 /* explicitly withdraw all null labels */
1534 RB_FOREACH(ln, nbr_tree, &lde_nbrs) {
1535 lde_send_labelwithdraw_wcard(ln, MPLS_LABEL_IMPLNULL);
1536 if (ln->v4_enabled)
1537 lde_send_labelwithdraw_wcard(ln, MPLS_LABEL_IPV4NULL);
1538 if (ln->v6_enabled)
1539 lde_send_labelwithdraw_wcard(ln, MPLS_LABEL_IPV6NULL);
1540 }
1541
1542 /* update label of connected routes */
1543 RB_FOREACH(f, fec_tree, &ft) {
1544 fn = (struct fec_node *)f;
1545 if (fn->local_label > MPLS_LABEL_RESERVED_MAX)
1546 continue;
1547
1548 switch (af) {
1549 case AF_INET:
1550 if (fn->fec.type != FEC_TYPE_IPV4)
1551 continue;
1552 break;
1553 case AF_INET6:
1554 if (fn->fec.type != FEC_TYPE_IPV6)
1555 continue;
1556 break;
1557 default:
1558 fatalx("lde_change_egress_label: unknown af");
1559 }
1560
1561 fn->local_label = lde_update_label(fn);
1562 if (fn->local_label != NO_LABEL)
1563 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1564 lde_send_labelmapping(ln, fn, 0);
1565 }
1566 RB_FOREACH(ln, nbr_tree, &lde_nbrs)
1567 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END, ln->peerid, 0,
1568 NULL, 0);
1569 }
1570
1571 static int
1572 lde_address_add(struct lde_nbr *ln, struct lde_addr *lde_addr)
1573 {
1574 struct lde_addr *new;
1575
1576 if (lde_address_find(ln, lde_addr->af, &lde_addr->addr) != NULL)
1577 return (-1);
1578
1579 if ((new = calloc(1, sizeof(*new))) == NULL)
1580 fatal(__func__);
1581
1582 new->af = lde_addr->af;
1583 new->addr = lde_addr->addr;
1584 TAILQ_INSERT_TAIL(&ln->addr_list, new, entry);
1585
1586 /* reevaluate the previously received mappings from this neighbor */
1587 lde_nbr_addr_update(ln, lde_addr, 0);
1588
1589 return (0);
1590 }
1591
1592 static int
1593 lde_address_del(struct lde_nbr *ln, struct lde_addr *lde_addr)
1594 {
1595 lde_addr = lde_address_find(ln, lde_addr->af, &lde_addr->addr);
1596 if (lde_addr == NULL)
1597 return (-1);
1598
1599 /* reevaluate the previously received mappings from this neighbor */
1600 lde_nbr_addr_update(ln, lde_addr, 1);
1601
1602 TAILQ_REMOVE(&ln->addr_list, lde_addr, entry);
1603 free(lde_addr);
1604
1605 return (0);
1606 }
1607
1608 struct lde_addr *
1609 lde_address_find(struct lde_nbr *ln, int af, union ldpd_addr *addr)
1610 {
1611 struct lde_addr *lde_addr;
1612
1613 TAILQ_FOREACH(lde_addr, &ln->addr_list, entry)
1614 if (lde_addr->af == af &&
1615 ldp_addrcmp(af, &lde_addr->addr, addr) == 0)
1616 return (lde_addr);
1617
1618 return (NULL);
1619 }
1620
1621 static void
1622 lde_address_list_free(struct lde_nbr *ln)
1623 {
1624 struct lde_addr *lde_addr;
1625
1626 while ((lde_addr = TAILQ_FIRST(&ln->addr_list)) != NULL) {
1627 TAILQ_REMOVE(&ln->addr_list, lde_addr, entry);
1628 free(lde_addr);
1629 }
1630 }
1631
1632 static void
1633 lde_del_label_chunk(void *val)
1634 {
1635 free(val);
1636 }
1637 static int
1638 lde_get_label_chunk(void)
1639 {
1640 int ret;
1641 uint32_t start, end;
1642
1643 log_debug("Getting label chunk");
1644 ret = lm_get_label_chunk(zclient_sync, 0, CHUNK_SIZE, &start, &end);
1645 if (ret < 0)
1646 {
1647 log_warnx("Error getting label chunk!");
1648 return -1;
1649 }
1650
1651 on_get_label_chunk_response(start, end);
1652
1653 return 0;
1654 }
1655 static void
1656 lde_label_list_init(void)
1657 {
1658 label_chunk_list = list_new();
1659 label_chunk_list->del = lde_del_label_chunk;
1660
1661 /* get first chunk */
1662 while (lde_get_label_chunk () != 0) {
1663 log_warnx("Error getting first label chunk!");
1664 sleep(1);
1665 }
1666 }
1667
1668 static void
1669 on_get_label_chunk_response(uint32_t start, uint32_t end)
1670 {
1671 struct label_chunk *new_label_chunk;
1672
1673 log_debug("Label Chunk assign: %u - %u", start, end);
1674
1675 new_label_chunk = calloc(1, sizeof(struct label_chunk));
1676 if (!new_label_chunk) {
1677 log_warn("Error trying to allocate label chunk %u - %u", start, end);
1678 return;
1679 }
1680
1681 new_label_chunk->start = start;
1682 new_label_chunk->end = end;
1683 new_label_chunk->used_mask = 0;
1684
1685 listnode_add(label_chunk_list, (void *)new_label_chunk);
1686
1687 /* let's update current if needed */
1688 if (!current_label_chunk)
1689 current_label_chunk = listtail(label_chunk_list);
1690 }
1691
1692 static uint32_t
1693 lde_get_next_label(void)
1694 {
1695 struct label_chunk *label_chunk;
1696 uint32_t i, pos, size;
1697 uint32_t label = NO_LABEL;
1698
1699 while (current_label_chunk) {
1700 label_chunk = listgetdata(current_label_chunk);
1701 if (!label_chunk)
1702 goto end;
1703
1704 /* try to get next free label in currently used label chunk */
1705 size = label_chunk->end - label_chunk->start + 1;
1706 for (i = 0, pos = 1; i < size; i++, pos <<= 1) {
1707 if (!(pos & label_chunk->used_mask)) {
1708 label_chunk->used_mask |= pos;
1709 label = label_chunk->start + i;
1710 goto end;
1711 }
1712 }
1713 current_label_chunk = listnextnode(current_label_chunk);
1714 }
1715
1716 end:
1717 /* we moved till the last chunk, or were not able to find a label,
1718 so let's ask for another one */
1719 if (!current_label_chunk || current_label_chunk == listtail(label_chunk_list)
1720 || label == NO_LABEL) {
1721 if (lde_get_label_chunk() != 0)
1722 log_warn("%s: Error getting label chunk!", __func__);
1723
1724 }
1725
1726 return label;
1727 }