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_add(ZAPI_CALLBACK_ARGS
);
43 static int ldp_interface_delete(ZAPI_CALLBACK_ARGS
);
44 static int ldp_interface_status_change(ZAPI_CALLBACK_ARGS
);
45 static int ldp_interface_address_add(ZAPI_CALLBACK_ARGS
);
46 static int ldp_interface_address_delete(ZAPI_CALLBACK_ARGS
);
47 static int ldp_zebra_read_route(ZAPI_CALLBACK_ARGS
);
48 static int ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
);
49 static void ldp_zebra_connected(struct zclient
*);
51 static struct zclient
*zclient
;
54 ifp2kif(struct interface
*ifp
, struct kif
*kif
)
56 memset(kif
, 0, sizeof(*kif
));
57 strlcpy(kif
->ifname
, ifp
->name
, sizeof(kif
->ifname
));
58 kif
->ifindex
= ifp
->ifindex
;
59 kif
->operative
= if_is_operative(ifp
);
60 if (ifp
->ll_type
== ZEBRA_LLT_ETHER
)
61 memcpy(kif
->mac
, ifp
->hw_addr
, ETH_ALEN
);
65 ifc2kaddr(struct interface
*ifp
, struct connected
*ifc
, struct kaddr
*ka
)
67 memset(ka
, 0, sizeof(*ka
));
68 strlcpy(ka
->ifname
, ifp
->name
, sizeof(ka
->ifname
));
69 ka
->ifindex
= ifp
->ifindex
;
70 ka
->af
= ifc
->address
->family
;
71 ka
->prefixlen
= ifc
->address
->prefixlen
;
75 ka
->addr
.v4
= ifc
->address
->u
.prefix4
;
77 ka
->dstbrd
.v4
= ifc
->destination
->u
.prefix4
;
80 ka
->addr
.v6
= ifc
->address
->u
.prefix6
;
82 ka
->dstbrd
.v6
= ifc
->destination
->u
.prefix6
;
90 pw2zpw(struct l2vpn_pw
*pw
, struct zapi_pw
*zpw
)
92 memset(zpw
, 0, sizeof(*zpw
));
93 strlcpy(zpw
->ifname
, pw
->ifname
, sizeof(zpw
->ifname
));
94 zpw
->ifindex
= pw
->ifindex
;
95 zpw
->type
= pw
->l2vpn
->pw_type
;
97 zpw
->nexthop
.ipv6
= pw
->addr
.v6
;
98 zpw
->local_label
= NO_LABEL
;
99 zpw
->remote_label
= NO_LABEL
;
100 if (pw
->flags
& F_PW_CWORD
)
101 zpw
->flags
= F_PSEUDOWIRE_CWORD
;
102 zpw
->data
.ldp
.lsr_id
= pw
->lsr_id
;
103 zpw
->data
.ldp
.pwid
= pw
->pwid
;
104 strlcpy(zpw
->data
.ldp
.vpn_name
, pw
->l2vpn
->name
,
105 sizeof(zpw
->data
.ldp
.vpn_name
));
109 ldp_zebra_send_mpls_labels(int cmd
, struct kroute
*kr
)
111 struct zapi_labels zl
= {};
112 struct zapi_nexthop_label
*znh
;
114 if (kr
->local_label
< MPLS_LABEL_RESERVED_MAX
||
115 kr
->remote_label
== NO_LABEL
)
118 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
119 log_addr(kr
->af
, &kr
->prefix
), kr
->prefixlen
,
120 log_addr(kr
->af
, &kr
->nexthop
), kr
->ifindex
,
121 log_label(kr
->local_label
), log_label(kr
->remote_label
),
122 (cmd
== ZEBRA_MPLS_LABELS_ADD
) ? "add" : "delete");
124 zl
.type
= ZEBRA_LSP_LDP
;
125 zl
.local_label
= kr
->local_label
;
128 SET_FLAG(zl
.message
, ZAPI_LABELS_FTN
);
129 zl
.route
.prefix
.family
= kr
->af
;
132 zl
.route
.prefix
.u
.prefix4
= kr
->prefix
.v4
;
135 zl
.route
.prefix
.u
.prefix6
= kr
->prefix
.v6
;
138 fatalx("ldp_zebra_send_mpls_labels: unknown af");
140 zl
.route
.prefix
.prefixlen
= kr
->prefixlen
;
141 zl
.route
.type
= kr
->route_type
;
142 zl
.route
.instance
= kr
->route_instance
;
146 znh
= &zl
.nexthops
[0];
149 znh
->family
= AF_INET
;
150 znh
->address
.ipv4
= kr
->nexthop
.v4
;
152 znh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
154 znh
->type
= NEXTHOP_TYPE_IPV4
;
157 znh
->family
= AF_INET6
;
158 znh
->address
.ipv6
= kr
->nexthop
.v6
;
160 znh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
162 znh
->type
= NEXTHOP_TYPE_IPV6
;
167 znh
->ifindex
= kr
->ifindex
;
168 znh
->label
= kr
->remote_label
;
170 return zebra_send_mpls_labels(zclient
, cmd
, &zl
);
174 kr_change(struct kroute
*kr
)
176 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, kr
));
180 kr_delete(struct kroute
*kr
)
182 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, kr
));
186 kmpw_add(struct zapi_pw
*zpw
)
188 debug_zebra_out("pseudowire %s nexthop %s (add)",
189 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
191 return (zebra_send_pw(zclient
, ZEBRA_PW_ADD
, zpw
));
195 kmpw_del(struct zapi_pw
*zpw
)
197 debug_zebra_out("pseudowire %s nexthop %s (del)",
198 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
200 return (zebra_send_pw(zclient
, ZEBRA_PW_DELETE
, zpw
));
204 kmpw_set(struct zapi_pw
*zpw
)
206 debug_zebra_out("pseudowire %s nexthop %s labels %u/%u (set)",
207 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
),
208 zpw
->local_label
, zpw
->remote_label
);
210 return (zebra_send_pw(zclient
, ZEBRA_PW_SET
, zpw
));
214 kmpw_unset(struct zapi_pw
*zpw
)
216 debug_zebra_out("pseudowire %s nexthop %s (unset)",
217 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
219 return (zebra_send_pw(zclient
, ZEBRA_PW_UNSET
, zpw
));
223 kif_redistribute(const char *ifname
)
225 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
226 struct listnode
*cnode
;
227 struct interface
*ifp
;
228 struct connected
*ifc
;
232 FOR_ALL_INTERFACES (vrf
, ifp
) {
233 if (ifname
&& strcmp(ifname
, ifp
->name
) != 0)
237 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
239 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
240 ifc2kaddr(ifp
, ifc
, &ka
);
241 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
248 ldp_router_id_update(ZAPI_CALLBACK_ARGS
)
250 struct prefix router_id
;
252 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
254 if (bad_addr_v4(router_id
.u
.prefix4
))
257 debug_zebra_in("router-id update %s", inet_ntoa(router_id
.u
.prefix4
));
259 global
.rtr_id
.s_addr
= router_id
.u
.prefix4
.s_addr
;
260 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE
, 0, &global
.rtr_id
,
261 sizeof(global
.rtr_id
));
267 ldp_interface_add(ZAPI_CALLBACK_ARGS
)
269 struct interface
*ifp
;
272 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
273 debug_zebra_in("interface add %s index %d mtu %d", ifp
->name
,
274 ifp
->ifindex
, ifp
->mtu
);
277 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
283 ldp_interface_delete(ZAPI_CALLBACK_ARGS
)
285 struct interface
*ifp
;
288 /* zebra_interface_state_read() updates interface structure in iflist */
289 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
293 debug_zebra_in("interface delete %s index %d mtu %d", ifp
->name
,
294 ifp
->ifindex
, ifp
->mtu
);
296 /* To support pseudo interface do not free interface structure. */
297 /* if_delete(ifp); */
298 if_set_index(ifp
, IFINDEX_INTERNAL
);
301 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
307 ldp_interface_status_change(ZAPI_CALLBACK_ARGS
)
309 struct interface
*ifp
;
310 struct listnode
*node
;
311 struct connected
*ifc
;
316 * zebra_interface_state_read() updates interface structure in
319 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
323 debug_zebra_in("interface %s state update", ifp
->name
);
326 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
328 if (if_is_operative(ifp
)) {
329 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
330 ifc2kaddr(ifp
, ifc
, &ka
);
331 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
335 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
336 ifc2kaddr(ifp
, ifc
, &ka
);
337 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
,
346 ldp_interface_address_add(ZAPI_CALLBACK_ARGS
)
348 struct connected
*ifc
;
349 struct interface
*ifp
;
352 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
357 ifc2kaddr(ifp
, ifc
, &ka
);
359 /* Filter invalid addresses. */
360 if (bad_addr(ka
.af
, &ka
.addr
))
363 debug_zebra_in("address add %s/%u interface %s",
364 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
366 /* notify ldpe about new address */
367 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
, sizeof(ka
));
373 ldp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
375 struct connected
*ifc
;
376 struct interface
*ifp
;
379 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
384 ifc2kaddr(ifp
, ifc
, &ka
);
387 /* Filter invalid addresses. */
388 if (bad_addr(ka
.af
, &ka
.addr
))
391 debug_zebra_in("address delete %s/%u interface %s",
392 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
394 /* notify ldpe about removed address */
395 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
, sizeof(ka
));
401 ldp_zebra_read_route(ZAPI_CALLBACK_ARGS
)
403 struct zapi_route api
;
404 struct zapi_nexthop
*api_nh
;
408 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
411 /* we completely ignore srcdest routes for now. */
412 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
415 memset(&kr
, 0, sizeof(kr
));
416 kr
.af
= api
.prefix
.family
;
419 kr
.prefix
.v4
= api
.prefix
.u
.prefix4
;
422 kr
.prefix
.v6
= api
.prefix
.u
.prefix6
;
427 kr
.prefixlen
= api
.prefix
.prefixlen
;
428 kr
.route_type
= api
.type
;
429 kr
.route_instance
= api
.instance
;
432 case ZEBRA_ROUTE_CONNECT
:
433 kr
.flags
|= F_CONNECTED
;
435 case ZEBRA_ROUTE_BGP
:
436 /* LDP should follow the IGP and ignore BGP routes */
442 if (bad_addr(kr
.af
, &kr
.prefix
) ||
443 (kr
.af
== AF_INET6
&& IN6_IS_SCOPE_EMBED(&kr
.prefix
.v6
)))
446 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
449 if (api
.nexthop_num
== 0)
450 debug_zebra_in("route %s %s/%d (%s)", (add
) ? "add" : "delete",
451 log_addr(kr
.af
, &kr
.prefix
), kr
.prefixlen
,
452 zebra_route_string(api
.type
));
454 /* loop through all the nexthops */
455 for (i
= 0; i
< api
.nexthop_num
; i
++) {
456 api_nh
= &api
.nexthops
[i
];
457 switch (api_nh
->type
) {
458 case NEXTHOP_TYPE_IPV4
:
459 if (kr
.af
!= AF_INET
)
461 kr
.nexthop
.v4
= api_nh
->gate
.ipv4
;
464 case NEXTHOP_TYPE_IPV4_IFINDEX
:
465 if (kr
.af
!= AF_INET
)
467 kr
.nexthop
.v4
= api_nh
->gate
.ipv4
;
468 kr
.ifindex
= api_nh
->ifindex
;
470 case NEXTHOP_TYPE_IPV6
:
471 if (kr
.af
!= AF_INET6
)
473 kr
.nexthop
.v6
= api_nh
->gate
.ipv6
;
476 case NEXTHOP_TYPE_IPV6_IFINDEX
:
477 if (kr
.af
!= AF_INET6
)
479 kr
.nexthop
.v6
= api_nh
->gate
.ipv6
;
480 kr
.ifindex
= api_nh
->ifindex
;
482 case NEXTHOP_TYPE_IFINDEX
:
483 if (!(kr
.flags
& F_CONNECTED
))
490 debug_zebra_in("route %s %s/%d nexthop %s ifindex %u (%s)",
491 (add
) ? "add" : "delete", log_addr(kr
.af
, &kr
.prefix
),
492 kr
.prefixlen
, log_addr(kr
.af
, &kr
.nexthop
), kr
.ifindex
,
493 zebra_route_string(api
.type
));
496 main_imsg_compose_lde(IMSG_NETWORK_ADD
, 0, &kr
,
500 main_imsg_compose_lde(IMSG_NETWORK_UPDATE
, 0, &kr
, sizeof(kr
));
506 * Receive PW status update from Zebra and send it to LDE process.
509 ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
)
511 struct zapi_pw_status zpw
;
513 zebra_read_pw_status_update(cmd
, zclient
, length
, vrf_id
, &zpw
);
515 debug_zebra_in("pseudowire %s status %s", zpw
.ifname
,
516 (zpw
.status
== PW_STATUS_UP
) ? "up" : "down");
518 main_imsg_compose_lde(IMSG_PW_UPDATE
, 0, &zpw
, sizeof(zpw
));
524 ldp_zebra_connected(struct zclient
*zclient
)
526 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
527 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
528 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
529 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP6
,
530 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
533 extern struct zebra_privs_t ldpd_privs
;
536 ldp_zebra_init(struct thread_master
*master
)
538 /* Set default values. */
539 zclient
= zclient_new(master
, &zclient_options_default
);
540 zclient_init(zclient
, ZEBRA_ROUTE_LDP
, 0, &ldpd_privs
);
543 zclient
->zebra_connected
= ldp_zebra_connected
;
544 zclient
->router_id_update
= ldp_router_id_update
;
545 zclient
->interface_add
= ldp_interface_add
;
546 zclient
->interface_delete
= ldp_interface_delete
;
547 zclient
->interface_up
= ldp_interface_status_change
;
548 zclient
->interface_down
= ldp_interface_status_change
;
549 zclient
->interface_address_add
= ldp_interface_address_add
;
550 zclient
->interface_address_delete
= ldp_interface_address_delete
;
551 zclient
->redistribute_route_add
= ldp_zebra_read_route
;
552 zclient
->redistribute_route_del
= ldp_zebra_read_route
;
553 zclient
->pw_status_update
= ldp_zebra_read_pw_status_update
;
557 ldp_zebra_destroy(void)
559 zclient_stop(zclient
);
560 zclient_free(zclient
);