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