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_start_hello_timer(void);
55 static int ldp_sync_zebra_hello(struct thread
*thread
);
56 static void ldp_sync_zebra_init(void);
58 static struct zclient
*zclient
;
61 ifp2kif(struct interface
*ifp
, struct kif
*kif
)
63 memset(kif
, 0, sizeof(*kif
));
64 strlcpy(kif
->ifname
, ifp
->name
, sizeof(kif
->ifname
));
65 kif
->ifindex
= ifp
->ifindex
;
66 kif
->operative
= if_is_operative(ifp
);
67 if (ifp
->ll_type
== ZEBRA_LLT_ETHER
)
68 memcpy(kif
->mac
, ifp
->hw_addr
, ETH_ALEN
);
72 ifc2kaddr(struct interface
*ifp
, struct connected
*ifc
, struct kaddr
*ka
)
74 memset(ka
, 0, sizeof(*ka
));
75 strlcpy(ka
->ifname
, ifp
->name
, sizeof(ka
->ifname
));
76 ka
->ifindex
= ifp
->ifindex
;
77 ka
->af
= ifc
->address
->family
;
78 ka
->prefixlen
= ifc
->address
->prefixlen
;
82 ka
->addr
.v4
= ifc
->address
->u
.prefix4
;
84 ka
->dstbrd
.v4
= ifc
->destination
->u
.prefix4
;
87 ka
->addr
.v6
= ifc
->address
->u
.prefix6
;
89 ka
->dstbrd
.v6
= ifc
->destination
->u
.prefix6
;
97 pw2zpw(struct l2vpn_pw
*pw
, struct zapi_pw
*zpw
)
99 memset(zpw
, 0, sizeof(*zpw
));
100 strlcpy(zpw
->ifname
, pw
->ifname
, sizeof(zpw
->ifname
));
101 zpw
->ifindex
= pw
->ifindex
;
102 zpw
->type
= pw
->l2vpn
->pw_type
;
104 zpw
->nexthop
.ipv6
= pw
->addr
.v6
;
105 zpw
->local_label
= NO_LABEL
;
106 zpw
->remote_label
= NO_LABEL
;
107 if (pw
->flags
& F_PW_CWORD
)
108 zpw
->flags
= F_PSEUDOWIRE_CWORD
;
109 zpw
->data
.ldp
.lsr_id
= pw
->lsr_id
;
110 zpw
->data
.ldp
.pwid
= pw
->pwid
;
111 strlcpy(zpw
->data
.ldp
.vpn_name
, pw
->l2vpn
->name
,
112 sizeof(zpw
->data
.ldp
.vpn_name
));
116 ldp_zebra_opaque_register(void)
118 zclient_register_opaque(zclient
, LDP_IGP_SYNC_IF_STATE_REQUEST
);
122 ldp_zebra_opaque_unregister(void)
124 zclient_unregister_opaque(zclient
, LDP_IGP_SYNC_IF_STATE_REQUEST
);
128 ldp_sync_zebra_send_state_update(struct ldp_igp_sync_if_state
*state
)
130 return zclient_send_opaque(zclient
, LDP_IGP_SYNC_IF_STATE_UPDATE
,
131 (const uint8_t *) state
, sizeof(*state
));
135 ldp_sync_zebra_send_announce(void)
137 struct ldp_igp_sync_announce announce
;
138 announce
.proto
= ZEBRA_ROUTE_LDP
;
140 return zclient_send_opaque(zclient
, LDP_IGP_SYNC_ANNOUNCE_UPDATE
,
141 (const uint8_t *) &announce
, sizeof(announce
));
145 ldp_zebra_opaque_msg_handler(ZAPI_CALLBACK_ARGS
)
148 struct zapi_opaque_msg info
;
149 struct ldp_igp_sync_if_state_req state_req
;
153 if (zclient_opaque_decode(s
, &info
) != 0)
157 case LDP_IGP_SYNC_IF_STATE_REQUEST
:
158 STREAM_GET(&state_req
, s
, sizeof(state_req
));
159 main_imsg_compose_ldpe(IMSG_LDP_SYNC_IF_STATE_REQUEST
, 0, &state_req
,
171 ldp_sync_zebra_start_hello_timer(void)
173 thread_add_timer_msec(master
, ldp_sync_zebra_hello
, NULL
, 250, NULL
);
177 ldp_sync_zebra_hello(struct thread
*thread
)
179 static unsigned int sequence
= 0;
180 struct ldp_igp_sync_hello hello
;
184 hello
.proto
= ZEBRA_ROUTE_LDP
;
185 hello
.sequence
= sequence
;
187 zclient_send_opaque(zclient
, LDP_IGP_SYNC_HELLO_UPDATE
,
188 (const uint8_t *) &hello
, sizeof(hello
));
190 ldp_sync_zebra_start_hello_timer();
196 ldp_sync_zebra_init(void)
198 ldp_sync_zebra_send_announce();
200 ldp_sync_zebra_start_hello_timer();
205 ldp_zebra_send_mpls_labels(int cmd
, struct kroute
*kr
)
207 struct zapi_labels zl
= {};
208 struct zapi_nexthop
*znh
;
210 if (kr
->local_label
< MPLS_LABEL_RESERVED_MAX
)
213 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
214 log_addr(kr
->af
, &kr
->prefix
), kr
->prefixlen
,
215 log_addr(kr
->af
, &kr
->nexthop
), kr
->ifindex
,
216 log_label(kr
->local_label
), log_label(kr
->remote_label
),
217 (cmd
== ZEBRA_MPLS_LABELS_ADD
) ? "add" : "delete");
219 zl
.type
= ZEBRA_LSP_LDP
;
220 zl
.local_label
= kr
->local_label
;
223 if (kr
->remote_label
!= NO_LABEL
) {
224 SET_FLAG(zl
.message
, ZAPI_LABELS_FTN
);
225 zl
.route
.prefix
.family
= kr
->af
;
228 zl
.route
.prefix
.u
.prefix4
= kr
->prefix
.v4
;
231 zl
.route
.prefix
.u
.prefix6
= kr
->prefix
.v6
;
234 fatalx("ldp_zebra_send_mpls_labels: unknown af");
236 zl
.route
.prefix
.prefixlen
= kr
->prefixlen
;
237 zl
.route
.type
= kr
->route_type
;
238 zl
.route
.instance
= kr
->route_instance
;
242 * For broken LSPs, instruct the forwarding plane to pop the top-level
243 * label and forward packets normally. This is a best-effort attempt
244 * to deliver labeled IP packets to their final destination (instead of
247 if (kr
->remote_label
== NO_LABEL
)
248 kr
->remote_label
= MPLS_LABEL_IMPLICIT_NULL
;
252 znh
= &zl
.nexthops
[0];
255 znh
->gate
.ipv4
= kr
->nexthop
.v4
;
257 znh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
259 znh
->type
= NEXTHOP_TYPE_IPV4
;
262 znh
->gate
.ipv6
= kr
->nexthop
.v6
;
264 znh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
266 znh
->type
= NEXTHOP_TYPE_IPV6
;
271 znh
->ifindex
= kr
->ifindex
;
273 znh
->labels
[0] = kr
->remote_label
;
275 return zebra_send_mpls_labels(zclient
, cmd
, &zl
);
279 kr_change(struct kroute
*kr
)
281 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, kr
));
285 kr_delete(struct kroute
*kr
)
287 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, kr
));
291 kmpw_add(struct zapi_pw
*zpw
)
293 debug_zebra_out("pseudowire %s nexthop %s (add)",
294 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
296 return (zebra_send_pw(zclient
, ZEBRA_PW_ADD
, zpw
));
300 kmpw_del(struct zapi_pw
*zpw
)
302 debug_zebra_out("pseudowire %s nexthop %s (del)",
303 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
305 return (zebra_send_pw(zclient
, ZEBRA_PW_DELETE
, zpw
));
309 kmpw_set(struct zapi_pw
*zpw
)
311 debug_zebra_out("pseudowire %s nexthop %s labels %u/%u (set)",
312 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
),
313 zpw
->local_label
, zpw
->remote_label
);
315 return (zebra_send_pw(zclient
, ZEBRA_PW_SET
, zpw
));
319 kmpw_unset(struct zapi_pw
*zpw
)
321 debug_zebra_out("pseudowire %s nexthop %s (unset)",
322 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
324 return (zebra_send_pw(zclient
, ZEBRA_PW_UNSET
, zpw
));
328 kif_redistribute(const char *ifname
)
330 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
331 struct listnode
*cnode
;
332 struct interface
*ifp
;
333 struct connected
*ifc
;
337 FOR_ALL_INTERFACES (vrf
, ifp
) {
338 if (ifname
&& strcmp(ifname
, ifp
->name
) != 0)
342 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
344 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
345 ifc2kaddr(ifp
, ifc
, &ka
);
346 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
353 ldp_router_id_update(ZAPI_CALLBACK_ARGS
)
355 struct prefix router_id
;
357 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
359 if (bad_addr_v4(router_id
.u
.prefix4
))
362 debug_zebra_in("router-id update %s", inet_ntoa(router_id
.u
.prefix4
));
364 global
.rtr_id
.s_addr
= router_id
.u
.prefix4
.s_addr
;
365 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE
, 0, &global
.rtr_id
,
366 sizeof(global
.rtr_id
));
372 ldp_ifp_create(struct interface
*ifp
)
376 debug_zebra_in("interface add %s index %d mtu %d", ifp
->name
,
377 ifp
->ifindex
, ifp
->mtu
);
380 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
386 ldp_ifp_destroy(struct interface
*ifp
)
390 debug_zebra_in("interface delete %s index %d mtu %d", ifp
->name
,
391 ifp
->ifindex
, ifp
->mtu
);
394 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
400 ldp_interface_status_change(struct interface
*ifp
)
402 struct listnode
*node
;
403 struct connected
*ifc
;
407 debug_zebra_in("interface %s state update", ifp
->name
);
410 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
412 if (if_is_operative(ifp
)) {
413 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
414 ifc2kaddr(ifp
, ifc
, &ka
);
415 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
419 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
420 ifc2kaddr(ifp
, ifc
, &ka
);
421 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
,
429 static int ldp_ifp_up(struct interface
*ifp
)
431 return ldp_interface_status_change(ifp
);
434 static int ldp_ifp_down(struct interface
*ifp
)
436 return ldp_interface_status_change(ifp
);
440 ldp_interface_address_add(ZAPI_CALLBACK_ARGS
)
442 struct connected
*ifc
;
443 struct interface
*ifp
;
446 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
451 ifc2kaddr(ifp
, ifc
, &ka
);
453 /* Filter invalid addresses. */
454 if (bad_addr(ka
.af
, &ka
.addr
))
457 debug_zebra_in("address add %s/%u interface %s",
458 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
460 /* notify ldpe about new address */
461 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
, sizeof(ka
));
467 ldp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
469 struct connected
*ifc
;
470 struct interface
*ifp
;
473 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
478 ifc2kaddr(ifp
, ifc
, &ka
);
479 connected_free(&ifc
);
481 /* Filter invalid addresses. */
482 if (bad_addr(ka
.af
, &ka
.addr
))
485 debug_zebra_in("address delete %s/%u interface %s",
486 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
488 /* notify ldpe about removed address */
489 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
, sizeof(ka
));
495 ldp_zebra_read_route(ZAPI_CALLBACK_ARGS
)
497 struct zapi_route api
;
498 struct zapi_nexthop
*api_nh
;
502 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
505 /* we completely ignore srcdest routes for now. */
506 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
509 memset(&kr
, 0, sizeof(kr
));
510 kr
.af
= api
.prefix
.family
;
513 kr
.prefix
.v4
= api
.prefix
.u
.prefix4
;
516 kr
.prefix
.v6
= api
.prefix
.u
.prefix6
;
521 kr
.prefixlen
= api
.prefix
.prefixlen
;
522 kr
.route_type
= api
.type
;
523 kr
.route_instance
= api
.instance
;
526 case ZEBRA_ROUTE_CONNECT
:
527 kr
.flags
|= F_CONNECTED
;
529 case ZEBRA_ROUTE_BGP
:
530 /* LDP should follow the IGP and ignore BGP routes */
536 if (bad_addr(kr
.af
, &kr
.prefix
) ||
537 (kr
.af
== AF_INET6
&& IN6_IS_SCOPE_EMBED(&kr
.prefix
.v6
)))
540 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
543 if (api
.nexthop_num
== 0)
544 debug_zebra_in("route %s %s/%d (%s)", (add
) ? "add" : "delete",
545 log_addr(kr
.af
, &kr
.prefix
), kr
.prefixlen
,
546 zebra_route_string(api
.type
));
548 /* loop through all the nexthops */
549 for (i
= 0; i
< api
.nexthop_num
; i
++) {
550 api_nh
= &api
.nexthops
[i
];
551 switch (api_nh
->type
) {
552 case NEXTHOP_TYPE_IPV4
:
553 if (kr
.af
!= AF_INET
)
555 kr
.nexthop
.v4
= api_nh
->gate
.ipv4
;
558 case NEXTHOP_TYPE_IPV4_IFINDEX
:
559 if (kr
.af
!= AF_INET
)
561 kr
.nexthop
.v4
= api_nh
->gate
.ipv4
;
562 kr
.ifindex
= api_nh
->ifindex
;
564 case NEXTHOP_TYPE_IPV6
:
565 if (kr
.af
!= AF_INET6
)
567 kr
.nexthop
.v6
= api_nh
->gate
.ipv6
;
570 case NEXTHOP_TYPE_IPV6_IFINDEX
:
571 if (kr
.af
!= AF_INET6
)
573 kr
.nexthop
.v6
= api_nh
->gate
.ipv6
;
574 kr
.ifindex
= api_nh
->ifindex
;
576 case NEXTHOP_TYPE_IFINDEX
:
577 if (!(kr
.flags
& F_CONNECTED
))
584 debug_zebra_in("route %s %s/%d nexthop %s ifindex %u (%s)",
585 (add
) ? "add" : "delete", log_addr(kr
.af
, &kr
.prefix
),
586 kr
.prefixlen
, log_addr(kr
.af
, &kr
.nexthop
), kr
.ifindex
,
587 zebra_route_string(api
.type
));
590 main_imsg_compose_lde(IMSG_NETWORK_ADD
, 0, &kr
,
594 main_imsg_compose_lde(IMSG_NETWORK_UPDATE
, 0, &kr
, sizeof(kr
));
600 * Receive PW status update from Zebra and send it to LDE process.
603 ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
)
605 struct zapi_pw_status zpw
;
607 zebra_read_pw_status_update(cmd
, zclient
, length
, vrf_id
, &zpw
);
609 debug_zebra_in("pseudowire %s status %s 0x%x", zpw
.ifname
,
610 (zpw
.status
== PW_FORWARDING
) ? "up" : "down",
613 main_imsg_compose_lde(IMSG_PW_UPDATE
, 0, &zpw
, sizeof(zpw
));
619 ldp_zebra_connected(struct zclient
*zclient
)
621 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
622 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
623 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
624 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP6
,
625 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
627 ldp_zebra_opaque_register();
629 ldp_sync_zebra_init();
633 ldp_zebra_filter_update(struct access_list
*access
)
635 struct ldp_access laccess
;
637 if (access
&& access
->name
[0] != '\0') {
638 strlcpy(laccess
.name
, access
->name
, sizeof(laccess
.name
));
639 laccess
.type
= access
->type
;
640 debug_evt("%s ACL update filter name %s type %d", __func__
,
641 access
->name
, access
->type
);
643 main_imsg_compose_both(IMSG_FILTER_UPDATE
, &laccess
,
648 extern struct zebra_privs_t ldpd_privs
;
651 ldp_zebra_init(struct thread_master
*master
)
653 if_zapi_callbacks(ldp_ifp_create
, ldp_ifp_up
,
654 ldp_ifp_down
, ldp_ifp_destroy
);
656 /* Set default values. */
657 zclient
= zclient_new(master
, &zclient_options_default
);
658 zclient_init(zclient
, ZEBRA_ROUTE_LDP
, 0, &ldpd_privs
);
661 zclient
->zebra_connected
= ldp_zebra_connected
;
662 zclient
->router_id_update
= ldp_router_id_update
;
663 zclient
->interface_address_add
= ldp_interface_address_add
;
664 zclient
->interface_address_delete
= ldp_interface_address_delete
;
665 zclient
->redistribute_route_add
= ldp_zebra_read_route
;
666 zclient
->redistribute_route_del
= ldp_zebra_read_route
;
667 zclient
->pw_status_update
= ldp_zebra_read_pw_status_update
;
668 zclient
->opaque_msg_handler
= ldp_zebra_opaque_msg_handler
;
670 /* Access list initialize. */
671 access_list_add_hook(ldp_zebra_filter_update
);
672 access_list_delete_hook(ldp_zebra_filter_update
);
676 ldp_zebra_destroy(void)
678 ldp_zebra_opaque_unregister();
679 zclient_stop(zclient
);
680 zclient_free(zclient
);