2 * Copyright (C) 2016 by Open Source Routing.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
36 #include "ldp_debug.h"
38 static void ifp2kif(struct interface
*, struct kif
*);
39 static void ifc2kaddr(struct interface
*, struct connected
*,
41 static int ldp_zebra_send_mpls_labels(int, struct kroute
*);
42 static int ldp_router_id_update(ZAPI_CALLBACK_ARGS
);
43 static int ldp_interface_address_add(ZAPI_CALLBACK_ARGS
);
44 static int ldp_interface_address_delete(ZAPI_CALLBACK_ARGS
);
45 static int ldp_zebra_read_route(ZAPI_CALLBACK_ARGS
);
46 static int ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
);
47 static void ldp_zebra_connected(struct zclient
*);
48 static void ldp_zebra_filter_update(struct access_list
*access
);
50 static void ldp_zebra_opaque_register(void);
51 static void ldp_zebra_opaque_unregister(void);
52 static int ldp_sync_zebra_send_announce(void);
53 static int ldp_zebra_opaque_msg_handler(ZAPI_CALLBACK_ARGS
);
54 static void ldp_sync_zebra_init(void);
56 static struct zclient
*zclient
;
57 static bool zebra_registered
= false;
60 ifp2kif(struct interface
*ifp
, struct kif
*kif
)
62 memset(kif
, 0, sizeof(*kif
));
63 strlcpy(kif
->ifname
, ifp
->name
, sizeof(kif
->ifname
));
64 kif
->ifindex
= ifp
->ifindex
;
65 kif
->operative
= if_is_operative(ifp
);
66 if (ifp
->ll_type
== ZEBRA_LLT_ETHER
)
67 memcpy(kif
->mac
, ifp
->hw_addr
, ETH_ALEN
);
71 ifc2kaddr(struct interface
*ifp
, struct connected
*ifc
, struct kaddr
*ka
)
73 memset(ka
, 0, sizeof(*ka
));
74 strlcpy(ka
->ifname
, ifp
->name
, sizeof(ka
->ifname
));
75 ka
->ifindex
= ifp
->ifindex
;
76 ka
->af
= ifc
->address
->family
;
77 ka
->prefixlen
= ifc
->address
->prefixlen
;
81 ka
->addr
.v4
= ifc
->address
->u
.prefix4
;
83 ka
->dstbrd
.v4
= ifc
->destination
->u
.prefix4
;
86 ka
->addr
.v6
= ifc
->address
->u
.prefix6
;
88 ka
->dstbrd
.v6
= ifc
->destination
->u
.prefix6
;
96 pw2zpw(struct l2vpn_pw
*pw
, struct zapi_pw
*zpw
)
98 memset(zpw
, 0, sizeof(*zpw
));
99 strlcpy(zpw
->ifname
, pw
->ifname
, sizeof(zpw
->ifname
));
100 zpw
->ifindex
= pw
->ifindex
;
101 zpw
->type
= pw
->l2vpn
->pw_type
;
103 zpw
->nexthop
.ipv6
= pw
->addr
.v6
;
104 zpw
->local_label
= NO_LABEL
;
105 zpw
->remote_label
= NO_LABEL
;
106 if (pw
->flags
& F_PW_CWORD
)
107 zpw
->flags
= F_PSEUDOWIRE_CWORD
;
108 zpw
->data
.ldp
.lsr_id
= pw
->lsr_id
;
109 zpw
->data
.ldp
.pwid
= pw
->pwid
;
110 strlcpy(zpw
->data
.ldp
.vpn_name
, pw
->l2vpn
->name
,
111 sizeof(zpw
->data
.ldp
.vpn_name
));
115 ldp_zebra_opaque_register(void)
117 zclient_register_opaque(zclient
, LDP_IGP_SYNC_IF_STATE_REQUEST
);
118 zclient_register_opaque(zclient
, LDP_RLFA_REGISTER
);
119 zclient_register_opaque(zclient
, LDP_RLFA_UNREGISTER_ALL
);
123 ldp_zebra_opaque_unregister(void)
125 zclient_unregister_opaque(zclient
, LDP_IGP_SYNC_IF_STATE_REQUEST
);
126 zclient_unregister_opaque(zclient
, LDP_RLFA_REGISTER
);
127 zclient_unregister_opaque(zclient
, LDP_RLFA_UNREGISTER_ALL
);
131 ldp_sync_zebra_send_state_update(struct ldp_igp_sync_if_state
*state
)
133 if (zclient_send_opaque(zclient
, LDP_IGP_SYNC_IF_STATE_UPDATE
,
134 (const uint8_t *)state
, sizeof(*state
))
135 == ZCLIENT_SEND_FAILURE
)
142 ldp_sync_zebra_send_announce(void)
144 struct ldp_igp_sync_announce announce
;
145 announce
.proto
= ZEBRA_ROUTE_LDP
;
147 if (zclient_send_opaque(zclient
, LDP_IGP_SYNC_ANNOUNCE_UPDATE
,
148 (const uint8_t *)&announce
, sizeof(announce
))
149 == ZCLIENT_SEND_FAILURE
)
155 int ldp_zebra_send_rlfa_labels(struct zapi_rlfa_response
*rlfa_labels
)
159 ret
= zclient_send_opaque(zclient
, LDP_RLFA_LABELS
,
160 (const uint8_t *)rlfa_labels
,
161 sizeof(*rlfa_labels
));
162 if (ret
== ZCLIENT_SEND_FAILURE
) {
163 log_warn("failed to send RLFA labels to IGP");
171 ldp_zebra_opaque_msg_handler(ZAPI_CALLBACK_ARGS
)
174 struct zapi_opaque_msg info
;
175 struct ldp_igp_sync_if_state_req state_req
;
176 struct zapi_rlfa_igp igp
;
177 struct zapi_rlfa_request rlfa
;
181 if (zclient_opaque_decode(s
, &info
) != 0)
185 case LDP_IGP_SYNC_IF_STATE_REQUEST
:
186 STREAM_GET(&state_req
, s
, sizeof(state_req
));
187 main_imsg_compose_ldpe(IMSG_LDP_SYNC_IF_STATE_REQUEST
, 0, &state_req
,
190 case LDP_RLFA_REGISTER
:
191 STREAM_GET(&rlfa
, s
, sizeof(rlfa
));
192 main_imsg_compose_both(IMSG_RLFA_REG
, &rlfa
, sizeof(rlfa
));
194 case LDP_RLFA_UNREGISTER_ALL
:
195 STREAM_GET(&igp
, s
, sizeof(igp
));
196 main_imsg_compose_both(IMSG_RLFA_UNREG_ALL
, &igp
, sizeof(igp
));
207 ldp_sync_zebra_init(void)
209 ldp_sync_zebra_send_announce();
213 ldp_zebra_send_mpls_labels(int cmd
, struct kroute
*kr
)
215 struct zapi_labels zl
= {};
216 struct zapi_nexthop
*znh
;
218 if (kr
->local_label
< MPLS_LABEL_RESERVED_MAX
)
221 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
222 log_addr(kr
->af
, &kr
->prefix
), kr
->prefixlen
,
223 log_addr(kr
->af
, &kr
->nexthop
), kr
->ifindex
,
224 log_label(kr
->local_label
), log_label(kr
->remote_label
),
225 (cmd
== ZEBRA_MPLS_LABELS_ADD
) ? "add" : "delete");
227 zl
.type
= ZEBRA_LSP_LDP
;
228 zl
.local_label
= kr
->local_label
;
231 if (kr
->remote_label
!= NO_LABEL
) {
232 SET_FLAG(zl
.message
, ZAPI_LABELS_FTN
);
233 zl
.route
.prefix
.family
= kr
->af
;
236 zl
.route
.prefix
.u
.prefix4
= kr
->prefix
.v4
;
239 zl
.route
.prefix
.u
.prefix6
= kr
->prefix
.v6
;
242 fatalx("ldp_zebra_send_mpls_labels: unknown af");
244 zl
.route
.prefix
.prefixlen
= kr
->prefixlen
;
245 zl
.route
.type
= kr
->route_type
;
246 zl
.route
.instance
= kr
->route_instance
;
249 /* If allow-broken-lsps is enabled then if an lsp is received with
250 * no remote label, instruct the forwarding plane to pop the top-level
251 * label and forward packets normally. This is a best-effort attempt
252 * to deliver labeled IP packets to their final destination (instead of
255 if (kr
->remote_label
== NO_LABEL
256 && !(ldpd_conf
->flags
& F_LDPD_ALLOW_BROKEN_LSP
)
257 && cmd
== ZEBRA_MPLS_LABELS_ADD
)
260 if (kr
->remote_label
== NO_LABEL
)
261 kr
->remote_label
= MPLS_LABEL_IMPLICIT_NULL
;
265 znh
= &zl
.nexthops
[0];
268 znh
->gate
.ipv4
= kr
->nexthop
.v4
;
270 znh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
272 znh
->type
= NEXTHOP_TYPE_IPV4
;
275 znh
->gate
.ipv6
= kr
->nexthop
.v6
;
277 znh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
279 znh
->type
= NEXTHOP_TYPE_IPV6
;
284 znh
->ifindex
= kr
->ifindex
;
286 znh
->labels
[0] = kr
->remote_label
;
288 if (zebra_send_mpls_labels(zclient
, cmd
, &zl
) == ZCLIENT_SEND_FAILURE
)
295 kr_change(struct kroute
*kr
)
297 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, kr
));
301 kr_delete(struct kroute
*kr
)
303 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, kr
));
307 kmpw_add(struct zapi_pw
*zpw
)
309 debug_zebra_out("pseudowire %s nexthop %s (add)",
310 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
312 return zebra_send_pw(zclient
, ZEBRA_PW_ADD
, zpw
)
313 == ZCLIENT_SEND_FAILURE
;
317 kmpw_del(struct zapi_pw
*zpw
)
319 debug_zebra_out("pseudowire %s nexthop %s (del)",
320 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
322 return zebra_send_pw(zclient
, ZEBRA_PW_DELETE
, zpw
)
323 == ZCLIENT_SEND_FAILURE
;
327 kmpw_set(struct zapi_pw
*zpw
)
329 debug_zebra_out("pseudowire %s nexthop %s labels %u/%u (set)",
330 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
),
331 zpw
->local_label
, zpw
->remote_label
);
333 return zebra_send_pw(zclient
, ZEBRA_PW_SET
, zpw
)
334 == ZCLIENT_SEND_FAILURE
;
338 kmpw_unset(struct zapi_pw
*zpw
)
340 debug_zebra_out("pseudowire %s nexthop %s (unset)",
341 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
343 return zebra_send_pw(zclient
, ZEBRA_PW_UNSET
, zpw
)
344 == ZCLIENT_SEND_FAILURE
;
348 kif_redistribute(const char *ifname
)
350 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
351 struct listnode
*cnode
;
352 struct interface
*ifp
;
353 struct connected
*ifc
;
357 FOR_ALL_INTERFACES (vrf
, ifp
) {
358 if (ifname
&& strcmp(ifname
, ifp
->name
) != 0)
362 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
364 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
365 ifc2kaddr(ifp
, ifc
, &ka
);
366 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
373 ldp_router_id_update(ZAPI_CALLBACK_ARGS
)
375 struct prefix router_id
;
377 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
379 if (bad_addr_v4(router_id
.u
.prefix4
))
382 debug_zebra_in("router-id update %pI4", &router_id
.u
.prefix4
);
384 global
.rtr_id
.s_addr
= router_id
.u
.prefix4
.s_addr
;
385 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE
, 0, &global
.rtr_id
,
386 sizeof(global
.rtr_id
));
392 ldp_ifp_create(struct interface
*ifp
)
396 debug_zebra_in("interface add %s index %d mtu %d", ifp
->name
,
397 ifp
->ifindex
, ifp
->mtu
);
400 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
406 ldp_ifp_destroy(struct interface
*ifp
)
410 debug_zebra_in("interface delete %s index %d mtu %d", ifp
->name
,
411 ifp
->ifindex
, ifp
->mtu
);
414 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
420 ldp_interface_status_change(struct interface
*ifp
)
422 struct listnode
*node
;
423 struct connected
*ifc
;
427 debug_zebra_in("interface %s state update", ifp
->name
);
430 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
432 if (if_is_operative(ifp
)) {
433 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
434 ifc2kaddr(ifp
, ifc
, &ka
);
435 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
439 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
440 ifc2kaddr(ifp
, ifc
, &ka
);
441 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
,
449 static int ldp_ifp_up(struct interface
*ifp
)
451 return ldp_interface_status_change(ifp
);
454 static int ldp_ifp_down(struct interface
*ifp
)
456 return ldp_interface_status_change(ifp
);
460 ldp_interface_address_add(ZAPI_CALLBACK_ARGS
)
462 struct connected
*ifc
;
463 struct interface
*ifp
;
466 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
471 ifc2kaddr(ifp
, ifc
, &ka
);
473 /* Filter invalid addresses. */
474 if (bad_addr(ka
.af
, &ka
.addr
))
477 debug_zebra_in("address add %s/%u interface %s",
478 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
480 /* notify ldpe about new address */
481 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
, sizeof(ka
));
487 ldp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
489 struct connected
*ifc
;
490 struct interface
*ifp
;
493 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
498 ifc2kaddr(ifp
, ifc
, &ka
);
499 connected_free(&ifc
);
501 /* Filter invalid addresses. */
502 if (bad_addr(ka
.af
, &ka
.addr
))
505 debug_zebra_in("address delete %s/%u interface %s",
506 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
508 /* notify ldpe about removed address */
509 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
, sizeof(ka
));
515 ldp_zebra_read_route(ZAPI_CALLBACK_ARGS
)
517 struct zapi_route api
;
518 struct zapi_nexthop
*api_nh
;
522 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
525 /* we completely ignore srcdest routes for now. */
526 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
529 memset(&kr
, 0, sizeof(kr
));
530 kr
.af
= api
.prefix
.family
;
533 kr
.prefix
.v4
= api
.prefix
.u
.prefix4
;
536 kr
.prefix
.v6
= api
.prefix
.u
.prefix6
;
541 kr
.prefixlen
= api
.prefix
.prefixlen
;
542 kr
.route_type
= api
.type
;
543 kr
.route_instance
= api
.instance
;
546 case ZEBRA_ROUTE_CONNECT
:
547 kr
.flags
|= F_CONNECTED
;
549 case ZEBRA_ROUTE_BGP
:
550 /* LDP should follow the IGP and ignore BGP routes */
556 if (bad_addr(kr
.af
, &kr
.prefix
) ||
557 (kr
.af
== AF_INET6
&& IN6_IS_SCOPE_EMBED(&kr
.prefix
.v6
)))
560 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
563 if (api
.nexthop_num
== 0)
564 debug_zebra_in("route %s %s/%d (%s)", (add
) ? "add" : "delete",
565 log_addr(kr
.af
, &kr
.prefix
), kr
.prefixlen
,
566 zebra_route_string(api
.type
));
568 /* loop through all the nexthops */
569 for (i
= 0; i
< api
.nexthop_num
; i
++) {
570 api_nh
= &api
.nexthops
[i
];
571 switch (api_nh
->type
) {
572 case NEXTHOP_TYPE_IPV4
:
573 if (kr
.af
!= AF_INET
)
575 kr
.nexthop
.v4
= api_nh
->gate
.ipv4
;
578 case NEXTHOP_TYPE_IPV4_IFINDEX
:
579 if (kr
.af
!= AF_INET
)
581 kr
.nexthop
.v4
= api_nh
->gate
.ipv4
;
582 kr
.ifindex
= api_nh
->ifindex
;
584 case NEXTHOP_TYPE_IPV6
:
585 if (kr
.af
!= AF_INET6
)
587 kr
.nexthop
.v6
= api_nh
->gate
.ipv6
;
590 case NEXTHOP_TYPE_IPV6_IFINDEX
:
591 if (kr
.af
!= AF_INET6
)
593 kr
.nexthop
.v6
= api_nh
->gate
.ipv6
;
594 kr
.ifindex
= api_nh
->ifindex
;
596 case NEXTHOP_TYPE_IFINDEX
:
597 if (!(kr
.flags
& F_CONNECTED
))
604 debug_zebra_in("route %s %s/%d nexthop %s ifindex %u (%s)",
605 (add
) ? "add" : "delete", log_addr(kr
.af
, &kr
.prefix
),
606 kr
.prefixlen
, log_addr(kr
.af
, &kr
.nexthop
), kr
.ifindex
,
607 zebra_route_string(api
.type
));
610 main_imsg_compose_lde(IMSG_NETWORK_ADD
, 0, &kr
,
614 main_imsg_compose_lde(IMSG_NETWORK_UPDATE
, 0, &kr
, sizeof(kr
));
620 * Receive PW status update from Zebra and send it to LDE process.
623 ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
)
625 struct zapi_pw_status zpw
;
627 zebra_read_pw_status_update(cmd
, zclient
, length
, vrf_id
, &zpw
);
629 debug_zebra_in("pseudowire %s status %s 0x%x", zpw
.ifname
,
630 (zpw
.status
== PW_FORWARDING
) ? "up" : "down",
633 main_imsg_compose_lde(IMSG_PW_UPDATE
, 0, &zpw
, sizeof(zpw
));
638 void ldp_zebra_regdereg_zebra_info(bool want_register
)
640 if (zebra_registered
== want_register
)
643 log_debug("%s to receive default VRF information",
644 want_register
? "Register" : "De-register");
647 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
648 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
649 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
650 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
,
651 AFI_IP6
, ZEBRA_ROUTE_ALL
, 0,
654 zclient_send_dereg_requests(zclient
, VRF_DEFAULT
);
655 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
,
656 AFI_IP
, ZEBRA_ROUTE_ALL
, 0,
658 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
,
659 AFI_IP6
, ZEBRA_ROUTE_ALL
, 0,
662 zebra_registered
= want_register
;
666 ldp_zebra_connected(struct zclient
*zclient
)
668 zebra_registered
= false;
670 /* if MPLS was already enabled and we are re-connecting, register again
672 if (vty_conf
->flags
& F_LDPD_ENABLED
)
673 ldp_zebra_regdereg_zebra_info(true);
675 ldp_zebra_opaque_register();
677 ldp_sync_zebra_init();
681 ldp_zebra_filter_update(struct access_list
*access
)
683 struct ldp_access laccess
;
685 if (access
&& access
->name
[0] != '\0') {
686 strlcpy(laccess
.name
, access
->name
, sizeof(laccess
.name
));
687 debug_evt("%s ACL update filter name %s", __func__
,
690 main_imsg_compose_both(IMSG_FILTER_UPDATE
, &laccess
,
695 extern struct zebra_privs_t ldpd_privs
;
698 ldp_zebra_init(struct thread_master
*master
)
700 if_zapi_callbacks(ldp_ifp_create
, ldp_ifp_up
,
701 ldp_ifp_down
, ldp_ifp_destroy
);
703 /* Set default values. */
704 zclient
= zclient_new(master
, &zclient_options_default
);
705 zclient_init(zclient
, ZEBRA_ROUTE_LDP
, 0, &ldpd_privs
);
708 zclient
->zebra_connected
= ldp_zebra_connected
;
709 zclient
->router_id_update
= ldp_router_id_update
;
710 zclient
->interface_address_add
= ldp_interface_address_add
;
711 zclient
->interface_address_delete
= ldp_interface_address_delete
;
712 zclient
->redistribute_route_add
= ldp_zebra_read_route
;
713 zclient
->redistribute_route_del
= ldp_zebra_read_route
;
714 zclient
->pw_status_update
= ldp_zebra_read_pw_status_update
;
715 zclient
->opaque_msg_handler
= ldp_zebra_opaque_msg_handler
;
717 /* Access list initialize. */
718 access_list_add_hook(ldp_zebra_filter_update
);
719 access_list_delete_hook(ldp_zebra_filter_update
);
723 ldp_zebra_destroy(void)
725 ldp_zebra_opaque_unregister();
726 zclient_stop(zclient
);
727 zclient_free(zclient
);