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
*);
48 static struct zclient
*zclient
;
51 ifp2kif(struct interface
*ifp
, struct kif
*kif
)
53 memset(kif
, 0, sizeof(*kif
));
54 strlcpy(kif
->ifname
, ifp
->name
, sizeof(kif
->ifname
));
55 kif
->ifindex
= ifp
->ifindex
;
56 kif
->operative
= if_is_operative(ifp
);
57 if (ifp
->ll_type
== ZEBRA_LLT_ETHER
)
58 memcpy(kif
->mac
, ifp
->hw_addr
, ETH_ALEN
);
62 ifc2kaddr(struct interface
*ifp
, struct connected
*ifc
, struct kaddr
*ka
)
64 memset(ka
, 0, sizeof(*ka
));
65 strlcpy(ka
->ifname
, ifp
->name
, sizeof(ka
->ifname
));
66 ka
->ifindex
= ifp
->ifindex
;
67 ka
->af
= ifc
->address
->family
;
68 ka
->prefixlen
= ifc
->address
->prefixlen
;
72 ka
->addr
.v4
= ifc
->address
->u
.prefix4
;
74 ka
->dstbrd
.v4
= ifc
->destination
->u
.prefix4
;
77 ka
->addr
.v6
= ifc
->address
->u
.prefix6
;
79 ka
->dstbrd
.v6
= ifc
->destination
->u
.prefix6
;
87 pw2zpw(struct l2vpn_pw
*pw
, struct zapi_pw
*zpw
)
89 memset(zpw
, 0, sizeof(*zpw
));
90 strlcpy(zpw
->ifname
, pw
->ifname
, sizeof(zpw
->ifname
));
91 zpw
->ifindex
= pw
->ifindex
;
92 zpw
->type
= pw
->l2vpn
->pw_type
;
94 zpw
->nexthop
.ipv6
= pw
->addr
.v6
;
95 zpw
->local_label
= NO_LABEL
;
96 zpw
->remote_label
= NO_LABEL
;
97 if (pw
->flags
& F_PW_CWORD
)
98 zpw
->flags
= F_PSEUDOWIRE_CWORD
;
99 zpw
->data
.ldp
.lsr_id
= pw
->lsr_id
;
100 zpw
->data
.ldp
.pwid
= pw
->pwid
;
101 strlcpy(zpw
->data
.ldp
.vpn_name
, pw
->l2vpn
->name
,
102 sizeof(zpw
->data
.ldp
.vpn_name
));
106 ldp_zebra_send_mpls_labels(int cmd
, struct kroute
*kr
)
108 struct zapi_labels zl
= {};
109 struct zapi_nexthop_label
*znh
;
111 if (kr
->local_label
< MPLS_LABEL_RESERVED_MAX
||
112 kr
->remote_label
== NO_LABEL
)
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 SET_FLAG(zl
.message
, ZAPI_LABELS_FTN
);
126 zl
.route
.prefix
.family
= kr
->af
;
129 zl
.route
.prefix
.u
.prefix4
= kr
->prefix
.v4
;
132 zl
.route
.prefix
.u
.prefix6
= kr
->prefix
.v6
;
135 fatalx("ldp_zebra_send_mpls_labels: unknown af");
137 zl
.route
.prefix
.prefixlen
= kr
->prefixlen
;
138 zl
.route
.type
= kr
->route_type
;
139 zl
.route
.instance
= kr
->route_instance
;
143 znh
= &zl
.nexthops
[0];
146 znh
->family
= AF_INET
;
147 znh
->address
.ipv4
= kr
->nexthop
.v4
;
149 znh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
151 znh
->type
= NEXTHOP_TYPE_IPV4
;
154 znh
->family
= AF_INET6
;
155 znh
->address
.ipv6
= kr
->nexthop
.v6
;
157 znh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
159 znh
->type
= NEXTHOP_TYPE_IPV6
;
164 znh
->ifindex
= kr
->ifindex
;
165 znh
->label
= kr
->remote_label
;
167 return zebra_send_mpls_labels(zclient
, cmd
, &zl
);
171 kr_change(struct kroute
*kr
)
173 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, kr
));
177 kr_delete(struct kroute
*kr
)
179 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, kr
));
183 kmpw_add(struct zapi_pw
*zpw
)
185 debug_zebra_out("pseudowire %s nexthop %s (add)",
186 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
188 return (zebra_send_pw(zclient
, ZEBRA_PW_ADD
, zpw
));
192 kmpw_del(struct zapi_pw
*zpw
)
194 debug_zebra_out("pseudowire %s nexthop %s (del)",
195 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
197 return (zebra_send_pw(zclient
, ZEBRA_PW_DELETE
, zpw
));
201 kmpw_set(struct zapi_pw
*zpw
)
203 debug_zebra_out("pseudowire %s nexthop %s labels %u/%u (set)",
204 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
),
205 zpw
->local_label
, zpw
->remote_label
);
207 return (zebra_send_pw(zclient
, ZEBRA_PW_SET
, zpw
));
211 kmpw_unset(struct zapi_pw
*zpw
)
213 debug_zebra_out("pseudowire %s nexthop %s (unset)",
214 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
216 return (zebra_send_pw(zclient
, ZEBRA_PW_UNSET
, zpw
));
220 kif_redistribute(const char *ifname
)
222 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
223 struct listnode
*cnode
;
224 struct interface
*ifp
;
225 struct connected
*ifc
;
229 FOR_ALL_INTERFACES (vrf
, ifp
) {
230 if (ifname
&& strcmp(ifname
, ifp
->name
) != 0)
234 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
236 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
237 ifc2kaddr(ifp
, ifc
, &ka
);
238 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
245 ldp_router_id_update(ZAPI_CALLBACK_ARGS
)
247 struct prefix router_id
;
249 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
251 if (bad_addr_v4(router_id
.u
.prefix4
))
254 debug_zebra_in("router-id update %s", inet_ntoa(router_id
.u
.prefix4
));
256 global
.rtr_id
.s_addr
= router_id
.u
.prefix4
.s_addr
;
257 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE
, 0, &global
.rtr_id
,
258 sizeof(global
.rtr_id
));
264 ldp_ifp_create(struct interface
*ifp
)
268 debug_zebra_in("interface add %s index %d mtu %d", ifp
->name
,
269 ifp
->ifindex
, ifp
->mtu
);
272 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
278 ldp_ifp_destroy(struct interface
*ifp
)
282 debug_zebra_in("interface delete %s index %d mtu %d", ifp
->name
,
283 ifp
->ifindex
, ifp
->mtu
);
286 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
292 ldp_interface_status_change_helper(struct interface
*ifp
)
294 struct listnode
*node
;
295 struct connected
*ifc
;
299 debug_zebra_in("interface %s state update", ifp
->name
);
302 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
304 if (if_is_operative(ifp
)) {
305 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
306 ifc2kaddr(ifp
, ifc
, &ka
);
307 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
311 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
312 ifc2kaddr(ifp
, ifc
, &ka
);
313 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
,
321 static int ldp_ifp_up(struct interface
*ifp
)
323 return ldp_interface_status_change_helper(ifp
);
326 static int ldp_ifp_down(struct interface
*ifp
)
328 return ldp_interface_status_change_helper(ifp
);
332 ldp_interface_address_add(ZAPI_CALLBACK_ARGS
)
334 struct connected
*ifc
;
335 struct interface
*ifp
;
338 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
343 ifc2kaddr(ifp
, ifc
, &ka
);
345 /* Filter invalid addresses. */
346 if (bad_addr(ka
.af
, &ka
.addr
))
349 debug_zebra_in("address add %s/%u interface %s",
350 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
352 /* notify ldpe about new address */
353 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
, sizeof(ka
));
359 ldp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
361 struct connected
*ifc
;
362 struct interface
*ifp
;
365 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
370 ifc2kaddr(ifp
, ifc
, &ka
);
373 /* Filter invalid addresses. */
374 if (bad_addr(ka
.af
, &ka
.addr
))
377 debug_zebra_in("address delete %s/%u interface %s",
378 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
380 /* notify ldpe about removed address */
381 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
, sizeof(ka
));
387 ldp_zebra_read_route(ZAPI_CALLBACK_ARGS
)
389 struct zapi_route api
;
390 struct zapi_nexthop
*api_nh
;
394 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
397 /* we completely ignore srcdest routes for now. */
398 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
401 memset(&kr
, 0, sizeof(kr
));
402 kr
.af
= api
.prefix
.family
;
405 kr
.prefix
.v4
= api
.prefix
.u
.prefix4
;
408 kr
.prefix
.v6
= api
.prefix
.u
.prefix6
;
413 kr
.prefixlen
= api
.prefix
.prefixlen
;
414 kr
.route_type
= api
.type
;
415 kr
.route_instance
= api
.instance
;
418 case ZEBRA_ROUTE_CONNECT
:
419 kr
.flags
|= F_CONNECTED
;
421 case ZEBRA_ROUTE_BGP
:
422 /* LDP should follow the IGP and ignore BGP routes */
428 if (bad_addr(kr
.af
, &kr
.prefix
) ||
429 (kr
.af
== AF_INET6
&& IN6_IS_SCOPE_EMBED(&kr
.prefix
.v6
)))
432 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
435 if (api
.nexthop_num
== 0)
436 debug_zebra_in("route %s %s/%d (%s)", (add
) ? "add" : "delete",
437 log_addr(kr
.af
, &kr
.prefix
), kr
.prefixlen
,
438 zebra_route_string(api
.type
));
440 /* loop through all the nexthops */
441 for (i
= 0; i
< api
.nexthop_num
; i
++) {
442 api_nh
= &api
.nexthops
[i
];
443 switch (api_nh
->type
) {
444 case NEXTHOP_TYPE_IPV4
:
445 if (kr
.af
!= AF_INET
)
447 kr
.nexthop
.v4
= api_nh
->gate
.ipv4
;
450 case NEXTHOP_TYPE_IPV4_IFINDEX
:
451 if (kr
.af
!= AF_INET
)
453 kr
.nexthop
.v4
= api_nh
->gate
.ipv4
;
454 kr
.ifindex
= api_nh
->ifindex
;
456 case NEXTHOP_TYPE_IPV6
:
457 if (kr
.af
!= AF_INET6
)
459 kr
.nexthop
.v6
= api_nh
->gate
.ipv6
;
462 case NEXTHOP_TYPE_IPV6_IFINDEX
:
463 if (kr
.af
!= AF_INET6
)
465 kr
.nexthop
.v6
= api_nh
->gate
.ipv6
;
466 kr
.ifindex
= api_nh
->ifindex
;
468 case NEXTHOP_TYPE_IFINDEX
:
469 if (!(kr
.flags
& F_CONNECTED
))
476 debug_zebra_in("route %s %s/%d nexthop %s ifindex %u (%s)",
477 (add
) ? "add" : "delete", log_addr(kr
.af
, &kr
.prefix
),
478 kr
.prefixlen
, log_addr(kr
.af
, &kr
.nexthop
), kr
.ifindex
,
479 zebra_route_string(api
.type
));
482 main_imsg_compose_lde(IMSG_NETWORK_ADD
, 0, &kr
,
486 main_imsg_compose_lde(IMSG_NETWORK_UPDATE
, 0, &kr
, sizeof(kr
));
492 * Receive PW status update from Zebra and send it to LDE process.
495 ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
)
497 struct zapi_pw_status zpw
;
499 zebra_read_pw_status_update(cmd
, zclient
, length
, vrf_id
, &zpw
);
501 debug_zebra_in("pseudowire %s status %s", zpw
.ifname
,
502 (zpw
.status
== PW_STATUS_UP
) ? "up" : "down");
504 main_imsg_compose_lde(IMSG_PW_UPDATE
, 0, &zpw
, sizeof(zpw
));
510 ldp_zebra_connected(struct zclient
*zclient
)
512 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
513 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
514 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
515 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP6
,
516 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
519 extern struct zebra_privs_t ldpd_privs
;
522 ldp_zebra_init(struct thread_master
*master
)
524 if_zapi_callbacks(ldp_ifp_create
, ldp_ifp_up
,
525 ldp_ifp_down
, ldp_ifp_destroy
);
527 /* Set default values. */
528 zclient
= zclient_new(master
, &zclient_options_default
);
529 zclient_init(zclient
, ZEBRA_ROUTE_LDP
, 0, &ldpd_privs
);
532 zclient
->zebra_connected
= ldp_zebra_connected
;
533 zclient
->router_id_update
= ldp_router_id_update
;
534 zclient
->interface_address_add
= ldp_interface_address_add
;
535 zclient
->interface_address_delete
= ldp_interface_address_delete
;
536 zclient
->redistribute_route_add
= ldp_zebra_read_route
;
537 zclient
->redistribute_route_del
= ldp_zebra_read_route
;
538 zclient
->pw_status_update
= ldp_zebra_read_pw_status_update
;
542 ldp_zebra_destroy(void)
544 zclient_stop(zclient
);
545 zclient_free(zclient
);