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