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,
35 #include "ldp_debug.h"
37 static void ifp2kif(struct interface
*, struct kif
*);
38 static void ifc2kaddr(struct interface
*, struct connected
*,
40 static int ldp_zebra_send_mpls_labels(int, struct kroute
*);
41 static int ldp_router_id_update(ZAPI_CALLBACK_ARGS
);
42 static int ldp_interface_address_add(ZAPI_CALLBACK_ARGS
);
43 static int ldp_interface_address_delete(ZAPI_CALLBACK_ARGS
);
44 static int ldp_zebra_read_route(ZAPI_CALLBACK_ARGS
);
45 static int ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
);
46 static void ldp_zebra_connected(struct zclient
*);
47 static void ldp_zebra_filter_update(struct access_list
*access
);
49 static struct zclient
*zclient
;
52 ifp2kif(struct interface
*ifp
, struct kif
*kif
)
54 memset(kif
, 0, sizeof(*kif
));
55 strlcpy(kif
->ifname
, ifp
->name
, sizeof(kif
->ifname
));
56 kif
->ifindex
= ifp
->ifindex
;
57 kif
->operative
= if_is_operative(ifp
);
58 if (ifp
->ll_type
== ZEBRA_LLT_ETHER
)
59 memcpy(kif
->mac
, ifp
->hw_addr
, ETH_ALEN
);
63 ifc2kaddr(struct interface
*ifp
, struct connected
*ifc
, struct kaddr
*ka
)
65 memset(ka
, 0, sizeof(*ka
));
66 strlcpy(ka
->ifname
, ifp
->name
, sizeof(ka
->ifname
));
67 ka
->ifindex
= ifp
->ifindex
;
68 ka
->af
= ifc
->address
->family
;
69 ka
->prefixlen
= ifc
->address
->prefixlen
;
73 ka
->addr
.v4
= ifc
->address
->u
.prefix4
;
75 ka
->dstbrd
.v4
= ifc
->destination
->u
.prefix4
;
78 ka
->addr
.v6
= ifc
->address
->u
.prefix6
;
80 ka
->dstbrd
.v6
= ifc
->destination
->u
.prefix6
;
88 pw2zpw(struct l2vpn_pw
*pw
, struct zapi_pw
*zpw
)
90 memset(zpw
, 0, sizeof(*zpw
));
91 strlcpy(zpw
->ifname
, pw
->ifname
, sizeof(zpw
->ifname
));
92 zpw
->ifindex
= pw
->ifindex
;
93 zpw
->type
= pw
->l2vpn
->pw_type
;
95 zpw
->nexthop
.ipv6
= pw
->addr
.v6
;
96 zpw
->local_label
= NO_LABEL
;
97 zpw
->remote_label
= NO_LABEL
;
98 if (pw
->flags
& F_PW_CWORD
)
99 zpw
->flags
= F_PSEUDOWIRE_CWORD
;
100 zpw
->data
.ldp
.lsr_id
= pw
->lsr_id
;
101 zpw
->data
.ldp
.pwid
= pw
->pwid
;
102 strlcpy(zpw
->data
.ldp
.vpn_name
, pw
->l2vpn
->name
,
103 sizeof(zpw
->data
.ldp
.vpn_name
));
107 ldp_zebra_send_mpls_labels(int cmd
, struct kroute
*kr
)
109 struct zapi_labels zl
= {};
110 struct zapi_nexthop
*znh
;
112 if (kr
->local_label
< MPLS_LABEL_RESERVED_MAX
)
115 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
116 log_addr(kr
->af
, &kr
->prefix
), kr
->prefixlen
,
117 log_addr(kr
->af
, &kr
->nexthop
), kr
->ifindex
,
118 log_label(kr
->local_label
), log_label(kr
->remote_label
),
119 (cmd
== ZEBRA_MPLS_LABELS_ADD
) ? "add" : "delete");
121 zl
.type
= ZEBRA_LSP_LDP
;
122 zl
.local_label
= kr
->local_label
;
125 if (kr
->remote_label
!= NO_LABEL
) {
126 SET_FLAG(zl
.message
, ZAPI_LABELS_FTN
);
127 zl
.route
.prefix
.family
= kr
->af
;
130 zl
.route
.prefix
.u
.prefix4
= kr
->prefix
.v4
;
133 zl
.route
.prefix
.u
.prefix6
= kr
->prefix
.v6
;
136 fatalx("ldp_zebra_send_mpls_labels: unknown af");
138 zl
.route
.prefix
.prefixlen
= kr
->prefixlen
;
139 zl
.route
.type
= kr
->route_type
;
140 zl
.route
.instance
= kr
->route_instance
;
144 * For broken LSPs, instruct the forwarding plane to pop the top-level
145 * label and forward packets normally. This is a best-effort attempt
146 * to deliver labeled IP packets to their final destination (instead of
149 if (kr
->remote_label
== NO_LABEL
)
150 kr
->remote_label
= MPLS_LABEL_IMPLICIT_NULL
;
154 znh
= &zl
.nexthops
[0];
157 znh
->gate
.ipv4
= kr
->nexthop
.v4
;
159 znh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
161 znh
->type
= NEXTHOP_TYPE_IPV4
;
164 znh
->gate
.ipv6
= kr
->nexthop
.v6
;
166 znh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
168 znh
->type
= NEXTHOP_TYPE_IPV6
;
173 znh
->ifindex
= kr
->ifindex
;
175 znh
->labels
[0] = kr
->remote_label
;
177 return zebra_send_mpls_labels(zclient
, cmd
, &zl
);
181 kr_change(struct kroute
*kr
)
183 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, kr
));
187 kr_delete(struct kroute
*kr
)
189 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, kr
));
193 kmpw_add(struct zapi_pw
*zpw
)
195 debug_zebra_out("pseudowire %s nexthop %s (add)",
196 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
198 return (zebra_send_pw(zclient
, ZEBRA_PW_ADD
, zpw
));
202 kmpw_del(struct zapi_pw
*zpw
)
204 debug_zebra_out("pseudowire %s nexthop %s (del)",
205 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
207 return (zebra_send_pw(zclient
, ZEBRA_PW_DELETE
, zpw
));
211 kmpw_set(struct zapi_pw
*zpw
)
213 debug_zebra_out("pseudowire %s nexthop %s labels %u/%u (set)",
214 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
),
215 zpw
->local_label
, zpw
->remote_label
);
217 return (zebra_send_pw(zclient
, ZEBRA_PW_SET
, zpw
));
221 kmpw_unset(struct zapi_pw
*zpw
)
223 debug_zebra_out("pseudowire %s nexthop %s (unset)",
224 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
226 return (zebra_send_pw(zclient
, ZEBRA_PW_UNSET
, zpw
));
230 kif_redistribute(const char *ifname
)
232 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
233 struct listnode
*cnode
;
234 struct interface
*ifp
;
235 struct connected
*ifc
;
239 FOR_ALL_INTERFACES (vrf
, ifp
) {
240 if (ifname
&& strcmp(ifname
, ifp
->name
) != 0)
244 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
246 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
247 ifc2kaddr(ifp
, ifc
, &ka
);
248 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
255 ldp_router_id_update(ZAPI_CALLBACK_ARGS
)
257 struct prefix router_id
;
259 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
261 if (bad_addr_v4(router_id
.u
.prefix4
))
264 debug_zebra_in("router-id update %s", inet_ntoa(router_id
.u
.prefix4
));
266 global
.rtr_id
.s_addr
= router_id
.u
.prefix4
.s_addr
;
267 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE
, 0, &global
.rtr_id
,
268 sizeof(global
.rtr_id
));
274 ldp_ifp_create(struct interface
*ifp
)
278 debug_zebra_in("interface add %s index %d mtu %d", ifp
->name
,
279 ifp
->ifindex
, ifp
->mtu
);
282 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
288 ldp_ifp_destroy(struct interface
*ifp
)
292 debug_zebra_in("interface delete %s index %d mtu %d", ifp
->name
,
293 ifp
->ifindex
, ifp
->mtu
);
296 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
302 ldp_interface_status_change_helper(struct interface
*ifp
)
304 struct listnode
*node
;
305 struct connected
*ifc
;
309 debug_zebra_in("interface %s state update", ifp
->name
);
312 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
314 if (if_is_operative(ifp
)) {
315 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
316 ifc2kaddr(ifp
, ifc
, &ka
);
317 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
321 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
322 ifc2kaddr(ifp
, ifc
, &ka
);
323 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
,
331 static int ldp_ifp_up(struct interface
*ifp
)
333 return ldp_interface_status_change_helper(ifp
);
336 static int ldp_ifp_down(struct interface
*ifp
)
338 return ldp_interface_status_change_helper(ifp
);
342 ldp_interface_address_add(ZAPI_CALLBACK_ARGS
)
344 struct connected
*ifc
;
345 struct interface
*ifp
;
348 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
353 ifc2kaddr(ifp
, ifc
, &ka
);
355 /* Filter invalid addresses. */
356 if (bad_addr(ka
.af
, &ka
.addr
))
359 debug_zebra_in("address add %s/%u interface %s",
360 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
362 /* notify ldpe about new address */
363 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
, sizeof(ka
));
369 ldp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
371 struct connected
*ifc
;
372 struct interface
*ifp
;
375 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
380 ifc2kaddr(ifp
, ifc
, &ka
);
381 connected_free(&ifc
);
383 /* Filter invalid addresses. */
384 if (bad_addr(ka
.af
, &ka
.addr
))
387 debug_zebra_in("address delete %s/%u interface %s",
388 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
390 /* notify ldpe about removed address */
391 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
, sizeof(ka
));
397 ldp_zebra_read_route(ZAPI_CALLBACK_ARGS
)
399 struct zapi_route api
;
400 struct zapi_nexthop
*api_nh
;
404 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
407 /* we completely ignore srcdest routes for now. */
408 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
411 memset(&kr
, 0, sizeof(kr
));
412 kr
.af
= api
.prefix
.family
;
415 kr
.prefix
.v4
= api
.prefix
.u
.prefix4
;
418 kr
.prefix
.v6
= api
.prefix
.u
.prefix6
;
423 kr
.prefixlen
= api
.prefix
.prefixlen
;
424 kr
.route_type
= api
.type
;
425 kr
.route_instance
= api
.instance
;
428 case ZEBRA_ROUTE_CONNECT
:
429 kr
.flags
|= F_CONNECTED
;
431 case ZEBRA_ROUTE_BGP
:
432 /* LDP should follow the IGP and ignore BGP routes */
438 if (bad_addr(kr
.af
, &kr
.prefix
) ||
439 (kr
.af
== AF_INET6
&& IN6_IS_SCOPE_EMBED(&kr
.prefix
.v6
)))
442 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
445 if (api
.nexthop_num
== 0)
446 debug_zebra_in("route %s %s/%d (%s)", (add
) ? "add" : "delete",
447 log_addr(kr
.af
, &kr
.prefix
), kr
.prefixlen
,
448 zebra_route_string(api
.type
));
450 /* loop through all the nexthops */
451 for (i
= 0; i
< api
.nexthop_num
; i
++) {
452 api_nh
= &api
.nexthops
[i
];
453 switch (api_nh
->type
) {
454 case NEXTHOP_TYPE_IPV4
:
455 if (kr
.af
!= AF_INET
)
457 kr
.nexthop
.v4
= api_nh
->gate
.ipv4
;
460 case NEXTHOP_TYPE_IPV4_IFINDEX
:
461 if (kr
.af
!= AF_INET
)
463 kr
.nexthop
.v4
= api_nh
->gate
.ipv4
;
464 kr
.ifindex
= api_nh
->ifindex
;
466 case NEXTHOP_TYPE_IPV6
:
467 if (kr
.af
!= AF_INET6
)
469 kr
.nexthop
.v6
= api_nh
->gate
.ipv6
;
472 case NEXTHOP_TYPE_IPV6_IFINDEX
:
473 if (kr
.af
!= AF_INET6
)
475 kr
.nexthop
.v6
= api_nh
->gate
.ipv6
;
476 kr
.ifindex
= api_nh
->ifindex
;
478 case NEXTHOP_TYPE_IFINDEX
:
479 if (!(kr
.flags
& F_CONNECTED
))
486 debug_zebra_in("route %s %s/%d nexthop %s ifindex %u (%s)",
487 (add
) ? "add" : "delete", log_addr(kr
.af
, &kr
.prefix
),
488 kr
.prefixlen
, log_addr(kr
.af
, &kr
.nexthop
), kr
.ifindex
,
489 zebra_route_string(api
.type
));
492 main_imsg_compose_lde(IMSG_NETWORK_ADD
, 0, &kr
,
496 main_imsg_compose_lde(IMSG_NETWORK_UPDATE
, 0, &kr
, sizeof(kr
));
502 * Receive PW status update from Zebra and send it to LDE process.
505 ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
)
507 struct zapi_pw_status zpw
;
509 zebra_read_pw_status_update(cmd
, zclient
, length
, vrf_id
, &zpw
);
511 debug_zebra_in("pseudowire %s status %s 0x%x", zpw
.ifname
,
512 (zpw
.status
== PW_FORWARDING
) ? "up" : "down",
515 main_imsg_compose_lde(IMSG_PW_UPDATE
, 0, &zpw
, sizeof(zpw
));
521 ldp_zebra_connected(struct zclient
*zclient
)
523 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
524 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
525 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
526 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP6
,
527 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
531 ldp_zebra_filter_update(struct access_list
*access
)
533 struct ldp_access laccess
;
535 if (access
&& access
->name
[0] != '\0') {
536 strlcpy(laccess
.name
, access
->name
, sizeof(laccess
.name
));
537 laccess
.type
= access
->type
;
538 debug_evt("%s ACL update filter name %s type %d", __func__
,
539 access
->name
, access
->type
);
541 main_imsg_compose_both(IMSG_FILTER_UPDATE
, &laccess
,
546 extern struct zebra_privs_t ldpd_privs
;
549 ldp_zebra_init(struct thread_master
*master
)
551 if_zapi_callbacks(ldp_ifp_create
, ldp_ifp_up
,
552 ldp_ifp_down
, ldp_ifp_destroy
);
554 /* Set default values. */
555 zclient
= zclient_new(master
, &zclient_options_default
);
556 zclient_init(zclient
, ZEBRA_ROUTE_LDP
, 0, &ldpd_privs
);
559 zclient
->zebra_connected
= ldp_zebra_connected
;
560 zclient
->router_id_update
= ldp_router_id_update
;
561 zclient
->interface_address_add
= ldp_interface_address_add
;
562 zclient
->interface_address_delete
= ldp_interface_address_delete
;
563 zclient
->redistribute_route_add
= ldp_zebra_read_route
;
564 zclient
->redistribute_route_del
= ldp_zebra_read_route
;
565 zclient
->pw_status_update
= ldp_zebra_read_pw_status_update
;
567 /* Access list initialize. */
568 access_list_add_hook(ldp_zebra_filter_update
);
569 access_list_delete_hook(ldp_zebra_filter_update
);
573 ldp_zebra_destroy(void)
575 zclient_stop(zclient
);
576 zclient_free(zclient
);