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>
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.
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.
29 #include "ldp_debug.h"
36 #include <lib/linklist.h>
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
*,
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);
63 RB_GENERATE(nbr_tree
, lde_nbr
, entry
, lde_nbr_compare
)
64 RB_GENERATE(lde_map_head
, lde_map
, entry
, lde_map_compare
)
66 struct ldpd_conf
*ldeconf
;
67 struct nbr_tree lde_nbrs
= RB_INITIALIZER(&lde_nbrs
);
69 static struct imsgev
*iev_ldpe
;
70 static struct imsgev
*iev_main
, *iev_main_sync
;
72 /* Master of threads. */
73 struct thread_master
*master
;
76 static zebra_capabilities_t _caps_p
[] =
81 static struct zebra_privs_t lde_privs
=
83 #if defined(VTY_GROUP)
84 .vty_group
= VTY_GROUP
,
87 .cap_num_p
= array_size(_caps_p
),
91 /* List of chunks of labels externally assigned by Zebra */
92 struct list
*label_chunk_list
;
93 struct listnode
*current_label_chunk
;
95 /* SIGINT / SIGTERM handler. */
102 static struct quagga_signal_t lde_signals
[] =
118 struct zclient
*zclient_sync
= NULL
;
120 zclient_sync_init(u_short instance
)
122 /* Initialize special zclient for synchronous message exchanges. */
123 log_debug("Initializing synchronous zclient for label manager");
124 zclient_sync
= zclient_new(master
);
125 zclient_sync
->sock
= -1;
126 zclient_sync
->redist_default
= ZEBRA_ROUTE_LDP
;
127 zclient_sync
->instance
= instance
;
128 while (zclient_socket_connect (zclient_sync
) < 0) {
129 log_warnx("Error connecting synchronous zclient!");
132 /* make socket non-blocking */
133 sock_set_nonblock(zclient_sync
->sock
);
135 /* Connect to label manager */
136 while (lm_label_manager_connect (zclient_sync
) != 0) {
137 log_warnx("Error connecting to label manager!");
142 /* label decision engine */
146 struct thread thread
;
148 #ifdef HAVE_SETPROCTITLE
149 setproctitle("label decision engine");
151 ldpd_process
= PROC_LDE_ENGINE
;
152 log_procname
= log_procnames
[PROC_LDE_ENGINE
];
154 master
= thread_master_create();
156 /* setup signal handler */
157 signal_init(master
, array_size(lde_signals
), lde_signals
);
159 /* setup pipes and event handlers to the parent process */
160 if ((iev_main
= calloc(1, sizeof(struct imsgev
))) == NULL
)
162 imsg_init(&iev_main
->ibuf
, LDPD_FD_ASYNC
);
163 iev_main
->handler_read
= lde_dispatch_parent
;
164 iev_main
->ev_read
= thread_add_read(master
, iev_main
->handler_read
,
165 iev_main
, iev_main
->ibuf
.fd
);
166 iev_main
->handler_write
= ldp_write_handler
;
168 if ((iev_main_sync
= calloc(1, sizeof(struct imsgev
))) == NULL
)
170 imsg_init(&iev_main_sync
->ibuf
, LDPD_FD_SYNC
);
172 /* create base configuration */
173 ldeconf
= config_new_empty();
175 /* Fetch next active thread. */
176 while (thread_fetch(master
, &thread
))
177 thread_call(&thread
);
181 lde_init(struct ldpd_init
*init
)
183 /* drop privileges */
184 lde_privs
.user
= init
->user
;
185 lde_privs
.group
= init
->group
;
186 zprivs_init(&lde_privs
);
189 if (pledge("stdio recvfd unix", NULL
) == -1)
193 /* start the LIB garbage collector */
194 lde_gc_start_timer();
196 /* Init synchronous zclient and label list */
197 zclient_serv_path_set(init
->zclient_serv_path
);
198 zclient_sync_init(init
->instance
);
199 lde_label_list_init();
207 msgbuf_clear(&iev_ldpe
->ibuf
.w
);
208 close(iev_ldpe
->ibuf
.fd
);
210 msgbuf_clear(&iev_main
->ibuf
.w
);
211 close(iev_main
->ibuf
.fd
);
212 msgbuf_clear(&iev_main_sync
->ibuf
.w
);
213 close(iev_main_sync
->ibuf
.fd
);
219 config_clear(ldeconf
);
226 log_info("label decision engine exiting");
232 lde_imsg_compose_parent(int type
, pid_t pid
, void *data
, uint16_t datalen
)
234 return (imsg_compose_event(iev_main
, type
, 0, pid
, -1, data
, datalen
));
238 lde_imsg_compose_parent_sync(int type
, pid_t pid
, void *data
, uint16_t datalen
)
240 imsg_compose_event(iev_main_sync
, type
, 0, pid
, -1, data
, datalen
);
241 imsg_flush(&iev_main_sync
->ibuf
);
245 lde_imsg_compose_ldpe(int type
, uint32_t peerid
, pid_t pid
, void *data
,
248 return (imsg_compose_event(iev_ldpe
, type
, peerid
, pid
,
254 lde_dispatch_imsg(struct thread
*thread
)
256 struct imsgev
*iev
= THREAD_ARG(thread
);
257 struct imsgbuf
*ibuf
= &iev
->ibuf
;
261 struct lde_addr
*lde_addr
;
262 struct notify_msg
*nm
;
268 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
269 fatal("imsg_read error");
270 if (n
== 0) /* connection closed */
274 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
275 fatal("lde_dispatch_imsg: imsg_get error");
279 switch (imsg
.hdr
.type
) {
280 case IMSG_LABEL_MAPPING_FULL
:
281 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
283 log_debug("%s: cannot find lde neighbor",
290 case IMSG_LABEL_MAPPING
:
291 case IMSG_LABEL_REQUEST
:
292 case IMSG_LABEL_RELEASE
:
293 case IMSG_LABEL_WITHDRAW
:
294 case IMSG_LABEL_ABORT
:
295 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
297 fatalx("lde_dispatch_imsg: wrong imsg len");
300 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
302 log_debug("%s: cannot find lde neighbor",
307 switch (imsg
.hdr
.type
) {
308 case IMSG_LABEL_MAPPING
:
309 lde_check_mapping(map
, ln
);
311 case IMSG_LABEL_REQUEST
:
312 lde_check_request(map
, ln
);
314 case IMSG_LABEL_RELEASE
:
315 lde_check_release(map
, ln
);
317 case IMSG_LABEL_WITHDRAW
:
318 lde_check_withdraw(map
, ln
);
320 case IMSG_LABEL_ABORT
:
325 case IMSG_ADDRESS_ADD
:
326 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
327 sizeof(struct lde_addr
))
328 fatalx("lde_dispatch_imsg: wrong imsg len");
329 lde_addr
= imsg
.data
;
331 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
333 log_debug("%s: cannot find lde neighbor",
337 if (lde_address_add(ln
, lde_addr
) < 0) {
338 log_debug("%s: cannot add address %s, it "
339 "already exists", __func__
,
340 log_addr(lde_addr
->af
, &lde_addr
->addr
));
343 case IMSG_ADDRESS_DEL
:
344 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
345 sizeof(struct lde_addr
))
346 fatalx("lde_dispatch_imsg: wrong imsg len");
347 lde_addr
= imsg
.data
;
349 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
351 log_debug("%s: cannot find lde neighbor",
355 if (lde_address_del(ln
, lde_addr
) < 0) {
356 log_debug("%s: cannot delete address %s, it "
357 "does not exist", __func__
,
358 log_addr(lde_addr
->af
, &lde_addr
->addr
));
361 case IMSG_NOTIFICATION
:
362 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
363 sizeof(struct notify_msg
))
364 fatalx("lde_dispatch_imsg: wrong imsg len");
367 ln
= lde_nbr_find(imsg
.hdr
.peerid
);
369 log_debug("%s: cannot find lde neighbor",
374 switch (nm
->status_code
) {
376 l2vpn_recv_pw_status(ln
, nm
);
380 * Do nothing for now. Should be useful in
381 * the future when we implement LDP-IGP
382 * Synchronization (RFC 5443) and Graceful
383 * Restart (RFC 3478).
389 case IMSG_NEIGHBOR_UP
:
390 if (imsg
.hdr
.len
- IMSG_HEADER_SIZE
!=
391 sizeof(struct lde_nbr
))
392 fatalx("lde_dispatch_imsg: wrong imsg len");
394 if (lde_nbr_find(imsg
.hdr
.peerid
))
395 fatalx("lde_dispatch_imsg: "
396 "neighbor already exists");
397 lde_nbr_new(imsg
.hdr
.peerid
, imsg
.data
);
399 case IMSG_NEIGHBOR_DOWN
:
400 lde_nbr_del(lde_nbr_find(imsg
.hdr
.peerid
));
402 case IMSG_CTL_SHOW_LIB
:
403 rt_dump(imsg
.hdr
.pid
);
405 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
406 imsg
.hdr
.pid
, NULL
, 0);
408 case IMSG_CTL_SHOW_L2VPN_PW
:
409 l2vpn_pw_ctl(imsg
.hdr
.pid
);
411 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
412 imsg
.hdr
.pid
, NULL
, 0);
414 case IMSG_CTL_SHOW_L2VPN_BINDING
:
415 l2vpn_binding_ctl(imsg
.hdr
.pid
);
417 lde_imsg_compose_ldpe(IMSG_CTL_END
, 0,
418 imsg
.hdr
.pid
, NULL
, 0);
421 log_debug("%s: unexpected imsg %d", __func__
,
430 /* this pipe is dead, so remove the event handlers and exit */
431 THREAD_READ_OFF(iev
->ev_read
);
432 THREAD_WRITE_OFF(iev
->ev_write
);
441 lde_dispatch_parent(struct thread
*thread
)
443 static struct ldpd_conf
*nconf
;
444 struct iface
*iface
, *niface
;
446 struct nbr_params
*nnbrp
;
447 static struct l2vpn
*l2vpn
, *nl2vpn
;
448 struct l2vpn_if
*lif
, *nlif
;
449 struct l2vpn_pw
*pw
, *npw
;
453 int fd
= THREAD_FD(thread
);
454 struct imsgev
*iev
= THREAD_ARG(thread
);
455 struct imsgbuf
*ibuf
= &iev
->ibuf
;
462 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
463 fatal("imsg_read error");
464 if (n
== 0) /* connection closed */
468 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
469 fatal("lde_dispatch_parent: imsg_get error");
473 switch (imsg
.hdr
.type
) {
475 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
477 fatalx("IFSTATUS imsg with wrong len");
480 iface
= if_lookup_name(ldeconf
, kif
->ifname
);
482 if_update_info(iface
, kif
);
486 RB_FOREACH(l2vpn
, l2vpn_head
, &ldeconf
->l2vpn_tree
) {
487 lif
= l2vpn_if_find(l2vpn
, kif
->ifname
);
489 l2vpn_if_update_info(lif
, kif
);
492 pw
= l2vpn_pw_find(l2vpn
, kif
->ifname
);
494 l2vpn_pw_update_info(pw
, kif
);
499 case IMSG_NETWORK_ADD
:
500 case IMSG_NETWORK_UPDATE
:
501 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
502 sizeof(struct kroute
)) {
503 log_warnx("%s: wrong imsg len", __func__
);
510 fec
.type
= FEC_TYPE_IPV4
;
511 fec
.u
.ipv4
.prefix
= kr
->prefix
.v4
;
512 fec
.u
.ipv4
.prefixlen
= kr
->prefixlen
;
515 fec
.type
= FEC_TYPE_IPV6
;
516 fec
.u
.ipv6
.prefix
= kr
->prefix
.v6
;
517 fec
.u
.ipv6
.prefixlen
= kr
->prefixlen
;
520 fatalx("lde_dispatch_parent: unknown af");
523 switch (imsg
.hdr
.type
) {
524 case IMSG_NETWORK_ADD
:
525 lde_kernel_insert(&fec
, kr
->af
, &kr
->nexthop
,
526 kr
->ifindex
, kr
->priority
,
527 kr
->flags
& F_CONNECTED
, NULL
);
529 case IMSG_NETWORK_UPDATE
:
530 lde_kernel_update(&fec
);
534 case IMSG_SOCKET_IPC
:
536 log_warnx("%s: received unexpected imsg fd "
537 "to ldpe", __func__
);
540 if ((fd
= imsg
.fd
) == -1) {
541 log_warnx("%s: expected to receive imsg fd to "
542 "ldpe but didn't receive any", __func__
);
546 if ((iev_ldpe
= malloc(sizeof(struct imsgev
))) == NULL
)
548 imsg_init(&iev_ldpe
->ibuf
, fd
);
549 iev_ldpe
->handler_read
= lde_dispatch_imsg
;
550 iev_ldpe
->ev_read
= thread_add_read(master
,
551 iev_ldpe
->handler_read
, iev_ldpe
, iev_ldpe
->ibuf
.fd
);
552 iev_ldpe
->handler_write
= ldp_write_handler
;
553 iev_ldpe
->ev_write
= NULL
;
556 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
557 sizeof(struct ldpd_init
))
558 fatalx("INIT imsg with wrong len");
560 memcpy(&init
, imsg
.data
, sizeof(init
));
563 case IMSG_RECONF_CONF
:
564 if ((nconf
= malloc(sizeof(struct ldpd_conf
))) ==
567 memcpy(nconf
, imsg
.data
, sizeof(struct ldpd_conf
));
569 RB_INIT(&nconf
->iface_tree
);
570 RB_INIT(&nconf
->tnbr_tree
);
571 RB_INIT(&nconf
->nbrp_tree
);
572 RB_INIT(&nconf
->l2vpn_tree
);
574 case IMSG_RECONF_IFACE
:
575 if ((niface
= malloc(sizeof(struct iface
))) == NULL
)
577 memcpy(niface
, imsg
.data
, sizeof(struct iface
));
579 RB_INSERT(iface_head
, &nconf
->iface_tree
, niface
);
581 case IMSG_RECONF_TNBR
:
582 if ((ntnbr
= malloc(sizeof(struct tnbr
))) == NULL
)
584 memcpy(ntnbr
, imsg
.data
, sizeof(struct tnbr
));
586 RB_INSERT(tnbr_head
, &nconf
->tnbr_tree
, ntnbr
);
588 case IMSG_RECONF_NBRP
:
589 if ((nnbrp
= malloc(sizeof(struct nbr_params
))) == NULL
)
591 memcpy(nnbrp
, imsg
.data
, sizeof(struct nbr_params
));
593 RB_INSERT(nbrp_head
, &nconf
->nbrp_tree
, nnbrp
);
595 case IMSG_RECONF_L2VPN
:
596 if ((nl2vpn
= malloc(sizeof(struct l2vpn
))) == NULL
)
598 memcpy(nl2vpn
, imsg
.data
, sizeof(struct l2vpn
));
600 RB_INIT(&nl2vpn
->if_tree
);
601 RB_INIT(&nl2vpn
->pw_tree
);
602 RB_INIT(&nl2vpn
->pw_inactive_tree
);
604 RB_INSERT(l2vpn_head
, &nconf
->l2vpn_tree
, nl2vpn
);
606 case IMSG_RECONF_L2VPN_IF
:
607 if ((nlif
= malloc(sizeof(struct l2vpn_if
))) == NULL
)
609 memcpy(nlif
, imsg
.data
, sizeof(struct l2vpn_if
));
611 RB_INSERT(l2vpn_if_head
, &nl2vpn
->if_tree
, nlif
);
613 case IMSG_RECONF_L2VPN_PW
:
614 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
616 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
618 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_tree
, npw
);
620 case IMSG_RECONF_L2VPN_IPW
:
621 if ((npw
= malloc(sizeof(struct l2vpn_pw
))) == NULL
)
623 memcpy(npw
, imsg
.data
, sizeof(struct l2vpn_pw
));
625 RB_INSERT(l2vpn_pw_head
, &nl2vpn
->pw_inactive_tree
, npw
);
627 case IMSG_RECONF_END
:
628 merge_config(ldeconf
, nconf
);
629 ldp_clear_config(nconf
);
632 case IMSG_DEBUG_UPDATE
:
633 if (imsg
.hdr
.len
!= IMSG_HEADER_SIZE
+
635 log_warnx("%s: wrong imsg len", __func__
);
638 memcpy(&ldp_debug
, imsg
.data
, sizeof(ldp_debug
));
641 log_debug("%s: unexpected imsg %d", __func__
,
650 /* this pipe is dead, so remove the event handlers and exit */
651 THREAD_READ_OFF(iev
->ev_read
);
652 THREAD_WRITE_OFF(iev
->ev_write
);
660 lde_acl_check(char *acl_name
, int af
, union ldpd_addr
*addr
, uint8_t prefixlen
)
662 return ldp_acl_request(iev_main_sync
, acl_name
, af
, addr
, prefixlen
);
666 lde_update_label(struct fec_node
*fn
)
671 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
672 if (fnh
->flags
& F_FEC_NH_CONNECTED
) {
678 /* should we allocate a label for this fec? */
679 switch (fn
->fec
.type
) {
681 if ((ldeconf
->ipv4
.flags
& F_LDPD_AF_ALLOCHOSTONLY
) &&
682 fn
->fec
.u
.ipv4
.prefixlen
!= 32)
684 if (lde_acl_check(ldeconf
->ipv4
.acl_label_allocate_for
,
685 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
686 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
690 if ((ldeconf
->ipv6
.flags
& F_LDPD_AF_ALLOCHOSTONLY
) &&
691 fn
->fec
.u
.ipv6
.prefixlen
!= 128)
693 if (lde_acl_check(ldeconf
->ipv6
.acl_label_allocate_for
,
694 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
695 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
703 /* choose implicit or explicit-null depending on configuration */
704 switch (fn
->fec
.type
) {
706 if (!(ldeconf
->ipv4
.flags
& F_LDPD_AF_EXPNULL
))
707 return (MPLS_LABEL_IMPLNULL
);
708 if (lde_acl_check(ldeconf
->ipv4
.acl_label_expnull_for
,
709 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
710 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
711 return (MPLS_LABEL_IMPLNULL
);
712 return (MPLS_LABEL_IPV4NULL
);
714 if (!(ldeconf
->ipv6
.flags
& F_LDPD_AF_EXPNULL
))
715 return (MPLS_LABEL_IMPLNULL
);
716 if (lde_acl_check(ldeconf
->ipv6
.acl_label_expnull_for
,
717 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
718 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
719 return (MPLS_LABEL_IMPLNULL
);
720 return (MPLS_LABEL_IPV6NULL
);
722 fatalx("lde_update_label: unexpected fec type");
727 /* preserve current label if there's no need to update it */
728 if (fn
->local_label
!= NO_LABEL
&&
729 fn
->local_label
> MPLS_LABEL_RESERVED_MAX
)
730 return (fn
->local_label
);
732 return lde_get_next_label ();
736 lde_send_change_klabel(struct fec_node
*fn
, struct fec_nh
*fnh
)
742 switch (fn
->fec
.type
) {
744 memset(&kr
, 0, sizeof(kr
));
746 kr
.prefix
.v4
= fn
->fec
.u
.ipv4
.prefix
;
747 kr
.prefixlen
= fn
->fec
.u
.ipv4
.prefixlen
;
748 kr
.nexthop
.v4
= fnh
->nexthop
.v4
;
749 kr
.ifindex
= fnh
->ifindex
;
750 kr
.local_label
= fn
->local_label
;
751 kr
.remote_label
= fnh
->remote_label
;
752 kr
.priority
= fnh
->priority
;
754 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE
, 0, &kr
,
758 memset(&kr
, 0, sizeof(kr
));
760 kr
.prefix
.v6
= fn
->fec
.u
.ipv6
.prefix
;
761 kr
.prefixlen
= fn
->fec
.u
.ipv6
.prefixlen
;
762 kr
.nexthop
.v6
= fnh
->nexthop
.v6
;
763 kr
.ifindex
= fnh
->ifindex
;
764 kr
.local_label
= fn
->local_label
;
765 kr
.remote_label
= fnh
->remote_label
;
766 kr
.priority
= fnh
->priority
;
768 lde_imsg_compose_parent(IMSG_KLABEL_CHANGE
, 0, &kr
,
772 if (fn
->local_label
== NO_LABEL
||
773 fnh
->remote_label
== NO_LABEL
)
776 pw
= (struct l2vpn_pw
*) fn
->data
;
777 pw
->flags
|= F_PW_STATUS_UP
;
779 memset(&kpw
, 0, sizeof(kpw
));
780 kpw
.ifindex
= pw
->ifindex
;
781 kpw
.pw_type
= fn
->fec
.u
.pwid
.type
;
783 kpw
.nexthop
= pw
->addr
;
784 kpw
.local_label
= fn
->local_label
;
785 kpw
.remote_label
= fnh
->remote_label
;
786 kpw
.flags
= pw
->flags
;
788 lde_imsg_compose_parent(IMSG_KPWLABEL_CHANGE
, 0, &kpw
,
795 lde_send_delete_klabel(struct fec_node
*fn
, struct fec_nh
*fnh
)
801 switch (fn
->fec
.type
) {
803 memset(&kr
, 0, sizeof(kr
));
805 kr
.prefix
.v4
= fn
->fec
.u
.ipv4
.prefix
;
806 kr
.prefixlen
= fn
->fec
.u
.ipv4
.prefixlen
;
807 kr
.nexthop
.v4
= fnh
->nexthop
.v4
;
808 kr
.ifindex
= fnh
->ifindex
;
809 kr
.local_label
= fn
->local_label
;
810 kr
.remote_label
= fnh
->remote_label
;
811 kr
.priority
= fnh
->priority
;
813 lde_imsg_compose_parent(IMSG_KLABEL_DELETE
, 0, &kr
,
817 memset(&kr
, 0, sizeof(kr
));
819 kr
.prefix
.v6
= fn
->fec
.u
.ipv6
.prefix
;
820 kr
.prefixlen
= fn
->fec
.u
.ipv6
.prefixlen
;
821 kr
.nexthop
.v6
= fnh
->nexthop
.v6
;
822 kr
.ifindex
= fnh
->ifindex
;
823 kr
.local_label
= fn
->local_label
;
824 kr
.remote_label
= fnh
->remote_label
;
825 kr
.priority
= fnh
->priority
;
827 lde_imsg_compose_parent(IMSG_KLABEL_DELETE
, 0, &kr
,
831 pw
= (struct l2vpn_pw
*) fn
->data
;
832 if (!(pw
->flags
& F_PW_STATUS_UP
))
834 pw
->flags
&= ~F_PW_STATUS_UP
;
836 memset(&kpw
, 0, sizeof(kpw
));
837 kpw
.ifindex
= pw
->ifindex
;
838 kpw
.pw_type
= fn
->fec
.u
.pwid
.type
;
840 kpw
.nexthop
= pw
->addr
;
841 kpw
.local_label
= fn
->local_label
;
842 kpw
.remote_label
= fnh
->remote_label
;
843 kpw
.flags
= pw
->flags
;
845 lde_imsg_compose_parent(IMSG_KPWLABEL_DELETE
, 0, &kpw
,
852 lde_fec2map(struct fec
*fec
, struct map
*map
)
854 memset(map
, 0, sizeof(*map
));
858 map
->type
= MAP_TYPE_PREFIX
;
859 map
->fec
.prefix
.af
= AF_INET
;
860 map
->fec
.prefix
.prefix
.v4
= fec
->u
.ipv4
.prefix
;
861 map
->fec
.prefix
.prefixlen
= fec
->u
.ipv4
.prefixlen
;
864 map
->type
= MAP_TYPE_PREFIX
;
865 map
->fec
.prefix
.af
= AF_INET6
;
866 map
->fec
.prefix
.prefix
.v6
= fec
->u
.ipv6
.prefix
;
867 map
->fec
.prefix
.prefixlen
= fec
->u
.ipv6
.prefixlen
;
870 map
->type
= MAP_TYPE_PWID
;
871 map
->fec
.pwid
.type
= fec
->u
.pwid
.type
;
872 map
->fec
.pwid
.group_id
= 0;
873 map
->flags
|= F_MAP_PW_ID
;
874 map
->fec
.pwid
.pwid
= fec
->u
.pwid
.pwid
;
880 lde_map2fec(struct map
*map
, struct in_addr lsr_id
, struct fec
*fec
)
882 memset(fec
, 0, sizeof(*fec
));
885 case MAP_TYPE_PREFIX
:
886 switch (map
->fec
.prefix
.af
) {
888 fec
->type
= FEC_TYPE_IPV4
;
889 fec
->u
.ipv4
.prefix
= map
->fec
.prefix
.prefix
.v4
;
890 fec
->u
.ipv4
.prefixlen
= map
->fec
.prefix
.prefixlen
;
893 fec
->type
= FEC_TYPE_IPV6
;
894 fec
->u
.ipv6
.prefix
= map
->fec
.prefix
.prefix
.v6
;
895 fec
->u
.ipv6
.prefixlen
= map
->fec
.prefix
.prefixlen
;
898 fatalx("lde_map2fec: unknown af");
903 fec
->type
= FEC_TYPE_PWID
;
904 fec
->u
.pwid
.type
= map
->fec
.pwid
.type
;
905 fec
->u
.pwid
.pwid
= map
->fec
.pwid
.pwid
;
906 fec
->u
.pwid
.lsr_id
= lsr_id
;
912 lde_send_labelmapping(struct lde_nbr
*ln
, struct fec_node
*fn
, int single
)
914 struct lde_wdraw
*lw
;
921 * We shouldn't send a new label mapping if we have a pending
922 * label release to receive. In this case, schedule to send a
923 * label mapping as soon as a label release is received.
925 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
, &fn
->fec
);
927 if (!fec_find(&ln
->sent_map_pending
, &fn
->fec
))
928 lde_map_pending_add(ln
, fn
);
933 * This function skips SL.1 - 3 and SL.9 - 14 because the label
934 * allocation is done way earlier (because of the merging nature of
938 lde_fec2map(&fn
->fec
, &map
);
939 switch (fn
->fec
.type
) {
943 if (lde_acl_check(ldeconf
->ipv4
.acl_label_advertise_to
,
944 AF_INET
, (union ldpd_addr
*)&ln
->id
, 32) != FILTER_PERMIT
)
946 if (lde_acl_check(ldeconf
->ipv4
.acl_label_advertise_for
,
947 AF_INET
, (union ldpd_addr
*)&fn
->fec
.u
.ipv4
.prefix
,
948 fn
->fec
.u
.ipv4
.prefixlen
) != FILTER_PERMIT
)
954 if (lde_acl_check(ldeconf
->ipv6
.acl_label_advertise_to
,
955 AF_INET
, (union ldpd_addr
*)&ln
->id
, 32) != FILTER_PERMIT
)
957 if (lde_acl_check(ldeconf
->ipv6
.acl_label_advertise_for
,
958 AF_INET6
, (union ldpd_addr
*)&fn
->fec
.u
.ipv6
.prefix
,
959 fn
->fec
.u
.ipv6
.prefixlen
) != FILTER_PERMIT
)
963 pw
= (struct l2vpn_pw
*) fn
->data
;
964 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
965 /* not the remote end of the pseudowire */
968 map
.flags
|= F_MAP_PW_IFMTU
;
969 map
.fec
.pwid
.ifmtu
= pw
->l2vpn
->mtu
;
970 if (pw
->flags
& F_PW_CWORD
)
971 map
.flags
|= F_MAP_PW_CWORD
;
972 if (pw
->flags
& F_PW_STATUSTLV
) {
973 map
.flags
|= F_MAP_PW_STATUS
;
974 /* VPLS are always up */
975 map
.pw_status
= PW_FORWARDING
;
979 map
.label
= fn
->local_label
;
981 /* SL.6: is there a pending request for this mapping? */
982 lre
= (struct lde_req
*)fec_find(&ln
->recv_req
, &fn
->fec
);
984 /* set label request msg id in the mapping response. */
985 map
.requestid
= lre
->msg_id
;
986 map
.flags
= F_MAP_REQ_ID
;
988 /* SL.7: delete record of pending request */
989 lde_req_del(ln
, lre
, 0);
992 /* SL.4: send label mapping */
993 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD
, ln
->peerid
, 0,
996 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
, ln
->peerid
, 0,
999 /* SL.5: record sent label mapping */
1000 me
= (struct lde_map
*)fec_find(&ln
->sent_map
, &fn
->fec
);
1002 me
= lde_map_add(ln
, fn
, 1);
1007 lde_send_labelwithdraw(struct lde_nbr
*ln
, struct fec_node
*fn
,
1008 struct map
*wcard
, struct status_tlv
*st
)
1010 struct lde_wdraw
*lw
;
1013 struct l2vpn_pw
*pw
;
1016 lde_fec2map(&fn
->fec
, &map
);
1017 switch (fn
->fec
.type
) {
1019 if (!ln
->v4_enabled
)
1023 if (!ln
->v6_enabled
)
1027 pw
= (struct l2vpn_pw
*) fn
->data
;
1028 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
1029 /* not the remote end of the pseudowire */
1032 if (pw
->flags
& F_PW_CWORD
)
1033 map
.flags
|= F_MAP_PW_CWORD
;
1036 map
.label
= fn
->local_label
;
1038 memcpy(&map
, wcard
, sizeof(map
));
1041 map
.st
.status_code
= st
->status_code
;
1042 map
.st
.msg_id
= st
->msg_id
;
1043 map
.st
.msg_type
= st
->msg_type
;
1044 map
.flags
|= F_MAP_STATUS
;
1047 /* SWd.1: send label withdraw. */
1048 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD
, ln
->peerid
, 0,
1050 lde_imsg_compose_ldpe(IMSG_WITHDRAW_ADD_END
, ln
->peerid
, 0, NULL
, 0);
1052 /* SWd.2: record label withdraw. */
1054 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
, &fn
->fec
);
1056 lw
= lde_wdraw_add(ln
, fn
);
1057 lw
->label
= map
.label
;
1061 RB_FOREACH(f
, fec_tree
, &ft
) {
1062 fn
= (struct fec_node
*)f
;
1063 me
= (struct lde_map
*)fec_find(&ln
->sent_map
, &fn
->fec
);
1064 if (lde_wildcard_apply(wcard
, &fn
->fec
, me
) == 0)
1067 lw
= (struct lde_wdraw
*)fec_find(&ln
->sent_wdraw
,
1070 lw
= lde_wdraw_add(ln
, fn
);
1071 lw
->label
= map
.label
;
1077 lde_send_labelwithdraw_wcard(struct lde_nbr
*ln
, uint32_t label
)
1081 memset(&wcard
, 0, sizeof(wcard
));
1082 wcard
.type
= MAP_TYPE_WILDCARD
;
1083 wcard
.label
= label
;
1084 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1088 lde_send_labelwithdraw_twcard_prefix(struct lde_nbr
*ln
, uint16_t af
,
1093 memset(&wcard
, 0, sizeof(wcard
));
1094 wcard
.type
= MAP_TYPE_TYPED_WCARD
;
1095 wcard
.fec
.twcard
.type
= MAP_TYPE_PREFIX
;
1096 wcard
.fec
.twcard
.u
.prefix_af
= af
;
1097 wcard
.label
= label
;
1098 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1102 lde_send_labelwithdraw_twcard_pwid(struct lde_nbr
*ln
, uint16_t pw_type
,
1107 memset(&wcard
, 0, sizeof(wcard
));
1108 wcard
.type
= MAP_TYPE_TYPED_WCARD
;
1109 wcard
.fec
.twcard
.type
= MAP_TYPE_PWID
;
1110 wcard
.fec
.twcard
.u
.pw_type
= pw_type
;
1111 wcard
.label
= label
;
1112 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1116 lde_send_labelwithdraw_pwid_wcard(struct lde_nbr
*ln
, uint16_t pw_type
,
1121 memset(&wcard
, 0, sizeof(wcard
));
1122 wcard
.type
= MAP_TYPE_PWID
;
1123 wcard
.fec
.pwid
.type
= pw_type
;
1124 wcard
.fec
.pwid
.group_id
= group_id
;
1125 /* we can not append a Label TLV when using PWid group wildcards. */
1126 wcard
.label
= NO_LABEL
;
1127 lde_send_labelwithdraw(ln
, NULL
, &wcard
, NULL
);
1131 lde_send_labelrelease(struct lde_nbr
*ln
, struct fec_node
*fn
,
1132 struct map
*wcard
, uint32_t label
)
1135 struct l2vpn_pw
*pw
;
1138 lde_fec2map(&fn
->fec
, &map
);
1139 switch (fn
->fec
.type
) {
1141 if (!ln
->v4_enabled
)
1145 if (!ln
->v6_enabled
)
1149 pw
= (struct l2vpn_pw
*) fn
->data
;
1150 if (pw
== NULL
|| pw
->lsr_id
.s_addr
!= ln
->id
.s_addr
)
1151 /* not the remote end of the pseudowire */
1154 if (pw
->flags
& F_PW_CWORD
)
1155 map
.flags
|= F_MAP_PW_CWORD
;
1159 memcpy(&map
, wcard
, sizeof(map
));
1162 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD
, ln
->peerid
, 0,
1164 lde_imsg_compose_ldpe(IMSG_RELEASE_ADD_END
, ln
->peerid
, 0, NULL
, 0);
1168 lde_send_notification(struct lde_nbr
*ln
, uint32_t status_code
, uint32_t msg_id
,
1171 struct notify_msg nm
;
1173 memset(&nm
, 0, sizeof(nm
));
1174 nm
.status_code
= status_code
;
1175 /* 'msg_id' and 'msg_type' should be in network byte order */
1177 nm
.msg_type
= msg_type
;
1179 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1184 lde_send_notification_eol_prefix(struct lde_nbr
*ln
, int af
)
1186 struct notify_msg nm
;
1188 memset(&nm
, 0, sizeof(nm
));
1189 nm
.status_code
= S_ENDOFLIB
;
1190 nm
.fec
.type
= MAP_TYPE_TYPED_WCARD
;
1191 nm
.fec
.fec
.twcard
.type
= MAP_TYPE_PREFIX
;
1192 nm
.fec
.fec
.twcard
.u
.prefix_af
= af
;
1193 nm
.flags
|= F_NOTIF_FEC
;
1195 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1200 lde_send_notification_eol_pwid(struct lde_nbr
*ln
, uint16_t pw_type
)
1202 struct notify_msg nm
;
1204 memset(&nm
, 0, sizeof(nm
));
1205 nm
.status_code
= S_ENDOFLIB
;
1206 nm
.fec
.type
= MAP_TYPE_TYPED_WCARD
;
1207 nm
.fec
.fec
.twcard
.type
= MAP_TYPE_PWID
;
1208 nm
.fec
.fec
.twcard
.u
.pw_type
= pw_type
;
1209 nm
.flags
|= F_NOTIF_FEC
;
1211 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0,
1216 lde_nbr_compare(struct lde_nbr
*a
, struct lde_nbr
*b
)
1218 return (a
->peerid
- b
->peerid
);
1221 static struct lde_nbr
*
1222 lde_nbr_new(uint32_t peerid
, struct lde_nbr
*new)
1226 if ((ln
= calloc(1, sizeof(*ln
))) == NULL
)
1230 ln
->v4_enabled
= new->v4_enabled
;
1231 ln
->v6_enabled
= new->v6_enabled
;
1232 ln
->flags
= new->flags
;
1233 ln
->peerid
= peerid
;
1234 fec_init(&ln
->recv_map
);
1235 fec_init(&ln
->sent_map
);
1236 fec_init(&ln
->sent_map_pending
);
1237 fec_init(&ln
->recv_req
);
1238 fec_init(&ln
->sent_req
);
1239 fec_init(&ln
->sent_wdraw
);
1241 TAILQ_INIT(&ln
->addr_list
);
1243 if (RB_INSERT(nbr_tree
, &lde_nbrs
, ln
) != NULL
)
1244 fatalx("lde_nbr_new: RB_INSERT failed");
1250 lde_nbr_del(struct lde_nbr
*ln
)
1253 struct fec_node
*fn
;
1255 struct l2vpn_pw
*pw
;
1260 /* uninstall received mappings */
1261 RB_FOREACH(f
, fec_tree
, &ft
) {
1262 fn
= (struct fec_node
*)f
;
1264 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
1268 if (!lde_address_find(ln
, fnh
->af
,
1273 if (f
->u
.pwid
.lsr_id
.s_addr
!= ln
->id
.s_addr
)
1275 pw
= (struct l2vpn_pw
*) fn
->data
;
1283 lde_send_delete_klabel(fn
, fnh
);
1284 fnh
->remote_label
= NO_LABEL
;
1288 lde_address_list_free(ln
);
1290 fec_clear(&ln
->recv_map
, lde_map_free
);
1291 fec_clear(&ln
->sent_map
, lde_map_free
);
1292 fec_clear(&ln
->sent_map_pending
, free
);
1293 fec_clear(&ln
->recv_req
, free
);
1294 fec_clear(&ln
->sent_req
, free
);
1295 fec_clear(&ln
->sent_wdraw
, free
);
1297 RB_REMOVE(nbr_tree
, &lde_nbrs
, ln
);
1302 static struct lde_nbr
*
1303 lde_nbr_find(uint32_t peerid
)
1309 return (RB_FIND(nbr_tree
, &lde_nbrs
, &ln
));
1313 lde_nbr_find_by_lsrid(struct in_addr addr
)
1317 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1318 if (ln
->id
.s_addr
== addr
.s_addr
)
1325 lde_nbr_find_by_addr(int af
, union ldpd_addr
*addr
)
1329 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1330 if (lde_address_find(ln
, af
, addr
) != NULL
)
1341 while ((ln
= RB_ROOT(&lde_nbrs
)) != NULL
)
1346 lde_nbr_addr_update(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
, int removed
)
1349 struct fec_node
*fn
;
1353 RB_FOREACH(fec
, fec_tree
, &ln
->recv_map
) {
1354 fn
= (struct fec_node
*)fec_find(&ft
, fec
);
1355 switch (fec
->type
) {
1357 if (lde_addr
->af
!= AF_INET
)
1361 if (lde_addr
->af
!= AF_INET6
)
1368 LIST_FOREACH(fnh
, &fn
->nexthops
, entry
) {
1369 if (ldp_addrcmp(fnh
->af
, &fnh
->nexthop
,
1374 lde_send_delete_klabel(fn
, fnh
);
1375 fnh
->remote_label
= NO_LABEL
;
1377 me
= (struct lde_map
*)fec
;
1378 fnh
->remote_label
= me
->map
.label
;
1379 lde_send_change_klabel(fn
, fnh
);
1387 lde_map_compare(struct lde_map
*a
, struct lde_map
*b
)
1389 return (ldp_addrcmp(AF_INET
, (union ldpd_addr
*)&a
->nexthop
->id
,
1390 (union ldpd_addr
*)&b
->nexthop
->id
));
1394 lde_map_add(struct lde_nbr
*ln
, struct fec_node
*fn
, int sent
)
1398 me
= calloc(1, sizeof(*me
));
1406 RB_INSERT(lde_map_head
, &fn
->upstream
, me
);
1407 me
->head
= &fn
->upstream
;
1408 if (fec_insert(&ln
->sent_map
, &me
->fec
))
1409 log_warnx("failed to add %s to sent map",
1411 /* XXX on failure more cleanup is needed */
1413 RB_INSERT(lde_map_head
, &fn
->downstream
, me
);
1414 me
->head
= &fn
->downstream
;
1415 if (fec_insert(&ln
->recv_map
, &me
->fec
))
1416 log_warnx("failed to add %s to recv map",
1424 lde_map_del(struct lde_nbr
*ln
, struct lde_map
*me
, int sent
)
1427 fec_remove(&ln
->sent_map
, &me
->fec
);
1429 fec_remove(&ln
->recv_map
, &me
->fec
);
1435 lde_map_free(void *ptr
)
1437 struct lde_map
*map
= ptr
;
1439 RB_REMOVE(lde_map_head
, map
->head
, map
);
1444 lde_map_pending_add(struct lde_nbr
*ln
, struct fec_node
*fn
)
1448 map
= calloc(1, sizeof(*map
));
1453 if (fec_insert(&ln
->sent_map_pending
, map
))
1454 log_warnx("failed to add %s to sent map (pending)",
1461 lde_map_pending_del(struct lde_nbr
*ln
, struct fec
*map
)
1463 fec_remove(&ln
->sent_map_pending
, map
);
1468 lde_req_add(struct lde_nbr
*ln
, struct fec
*fec
, int sent
)
1471 struct lde_req
*lre
;
1473 t
= sent
? &ln
->sent_req
: &ln
->recv_req
;
1475 lre
= calloc(1, sizeof(*lre
));
1479 if (fec_insert(t
, &lre
->fec
)) {
1480 log_warnx("failed to add %s to %s req",
1481 log_fec(&lre
->fec
), sent
? "sent" : "recv");
1491 lde_req_del(struct lde_nbr
*ln
, struct lde_req
*lre
, int sent
)
1494 fec_remove(&ln
->sent_req
, &lre
->fec
);
1496 fec_remove(&ln
->recv_req
, &lre
->fec
);
1502 lde_wdraw_add(struct lde_nbr
*ln
, struct fec_node
*fn
)
1504 struct lde_wdraw
*lw
;
1506 lw
= calloc(1, sizeof(*lw
));
1512 if (fec_insert(&ln
->sent_wdraw
, &lw
->fec
))
1513 log_warnx("failed to add %s to sent wdraw",
1520 lde_wdraw_del(struct lde_nbr
*ln
, struct lde_wdraw
*lw
)
1522 fec_remove(&ln
->sent_wdraw
, &lw
->fec
);
1527 lde_change_egress_label(int af
)
1531 struct fec_node
*fn
;
1533 /* explicitly withdraw all null labels */
1534 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
) {
1535 lde_send_labelwithdraw_wcard(ln
, MPLS_LABEL_IMPLNULL
);
1537 lde_send_labelwithdraw_wcard(ln
, MPLS_LABEL_IPV4NULL
);
1539 lde_send_labelwithdraw_wcard(ln
, MPLS_LABEL_IPV6NULL
);
1542 /* update label of connected routes */
1543 RB_FOREACH(f
, fec_tree
, &ft
) {
1544 fn
= (struct fec_node
*)f
;
1545 if (fn
->local_label
> MPLS_LABEL_RESERVED_MAX
)
1550 if (fn
->fec
.type
!= FEC_TYPE_IPV4
)
1554 if (fn
->fec
.type
!= FEC_TYPE_IPV6
)
1558 fatalx("lde_change_egress_label: unknown af");
1561 fn
->local_label
= lde_update_label(fn
);
1562 if (fn
->local_label
!= NO_LABEL
)
1563 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1564 lde_send_labelmapping(ln
, fn
, 0);
1566 RB_FOREACH(ln
, nbr_tree
, &lde_nbrs
)
1567 lde_imsg_compose_ldpe(IMSG_MAPPING_ADD_END
, ln
->peerid
, 0,
1572 lde_address_add(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
)
1574 struct lde_addr
*new;
1576 if (lde_address_find(ln
, lde_addr
->af
, &lde_addr
->addr
) != NULL
)
1579 if ((new = calloc(1, sizeof(*new))) == NULL
)
1582 new->af
= lde_addr
->af
;
1583 new->addr
= lde_addr
->addr
;
1584 TAILQ_INSERT_TAIL(&ln
->addr_list
, new, entry
);
1586 /* reevaluate the previously received mappings from this neighbor */
1587 lde_nbr_addr_update(ln
, lde_addr
, 0);
1593 lde_address_del(struct lde_nbr
*ln
, struct lde_addr
*lde_addr
)
1595 lde_addr
= lde_address_find(ln
, lde_addr
->af
, &lde_addr
->addr
);
1596 if (lde_addr
== NULL
)
1599 /* reevaluate the previously received mappings from this neighbor */
1600 lde_nbr_addr_update(ln
, lde_addr
, 1);
1602 TAILQ_REMOVE(&ln
->addr_list
, lde_addr
, entry
);
1609 lde_address_find(struct lde_nbr
*ln
, int af
, union ldpd_addr
*addr
)
1611 struct lde_addr
*lde_addr
;
1613 TAILQ_FOREACH(lde_addr
, &ln
->addr_list
, entry
)
1614 if (lde_addr
->af
== af
&&
1615 ldp_addrcmp(af
, &lde_addr
->addr
, addr
) == 0)
1622 lde_address_list_free(struct lde_nbr
*ln
)
1624 struct lde_addr
*lde_addr
;
1626 while ((lde_addr
= TAILQ_FIRST(&ln
->addr_list
)) != NULL
) {
1627 TAILQ_REMOVE(&ln
->addr_list
, lde_addr
, entry
);
1633 lde_del_label_chunk(void *val
)
1638 lde_get_label_chunk(void)
1641 uint32_t start
, end
;
1643 log_debug("Getting label chunk");
1644 ret
= lm_get_label_chunk(zclient_sync
, 0, CHUNK_SIZE
, &start
, &end
);
1647 log_warnx("Error getting label chunk!");
1651 on_get_label_chunk_response(start
, end
);
1656 lde_label_list_init(void)
1658 label_chunk_list
= list_new();
1659 label_chunk_list
->del
= lde_del_label_chunk
;
1661 /* get first chunk */
1662 while (lde_get_label_chunk () != 0) {
1663 log_warnx("Error getting first label chunk!");
1669 on_get_label_chunk_response(uint32_t start
, uint32_t end
)
1671 struct label_chunk
*new_label_chunk
;
1673 log_debug("Label Chunk assign: %u - %u", start
, end
);
1675 new_label_chunk
= calloc(1, sizeof(struct label_chunk
));
1676 if (!new_label_chunk
) {
1677 log_warn("Error trying to allocate label chunk %u - %u", start
, end
);
1681 new_label_chunk
->start
= start
;
1682 new_label_chunk
->end
= end
;
1683 new_label_chunk
->used_mask
= 0;
1685 listnode_add(label_chunk_list
, (void *)new_label_chunk
);
1687 /* let's update current if needed */
1688 if (!current_label_chunk
)
1689 current_label_chunk
= listtail(label_chunk_list
);
1693 lde_get_next_label(void)
1695 struct label_chunk
*label_chunk
;
1696 uint32_t i
, pos
, size
;
1697 uint32_t label
= NO_LABEL
;
1699 while (current_label_chunk
) {
1700 label_chunk
= listgetdata(current_label_chunk
);
1704 /* try to get next free label in currently used label chunk */
1705 size
= label_chunk
->end
- label_chunk
->start
+ 1;
1706 for (i
= 0, pos
= 1; i
< size
; i
++, pos
<<= 1) {
1707 if (!(pos
& label_chunk
->used_mask
)) {
1708 label_chunk
->used_mask
|= pos
;
1709 label
= label_chunk
->start
+ i
;
1713 current_label_chunk
= listnextnode(current_label_chunk
);
1717 /* we moved till the last chunk, or were not able to find a label,
1718 so let's ask for another one */
1719 if (!current_label_chunk
|| current_label_chunk
== listtail(label_chunk_list
)
1720 || label
== NO_LABEL
) {
1721 if (lde_get_label_chunk() != 0)
1722 log_warn("%s: Error getting label chunk!", __func__
);