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 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 zebra_send_mpls_labels(int cmd
, struct kroute
*kr
)
113 if (kr
->local_label
< MPLS_LABEL_RESERVED_MAX
||
114 kr
->remote_label
== NO_LABEL
)
117 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
118 log_addr(kr
->af
, &kr
->prefix
), kr
->prefixlen
,
119 log_addr(kr
->af
, &kr
->nexthop
), kr
->ifindex
,
120 log_label(kr
->local_label
), log_label(kr
->remote_label
),
121 (cmd
== ZEBRA_MPLS_LABELS_ADD
) ? "add" : "delete");
127 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
128 stream_putc(s
, ZEBRA_LSP_LDP
);
129 stream_putl(s
, kr
->af
);
132 stream_put_in_addr(s
, &kr
->prefix
.v4
);
133 stream_putc(s
, kr
->prefixlen
);
134 stream_put_in_addr(s
, &kr
->nexthop
.v4
);
137 stream_write(s
, (uint8_t *)&kr
->prefix
.v6
, 16);
138 stream_putc(s
, kr
->prefixlen
);
139 stream_write(s
, (uint8_t *)&kr
->nexthop
.v6
, 16);
142 fatalx("kr_change: unknown af");
144 stream_putl(s
, kr
->ifindex
);
145 stream_putc(s
, kr
->priority
);
146 stream_putl(s
, kr
->local_label
);
147 stream_putl(s
, kr
->remote_label
);
149 /* Put length at the first point of the stream. */
150 stream_putw_at(s
, 0, stream_get_endp(s
));
152 return (zclient_send_message(zclient
));
156 kr_change(struct kroute
*kr
)
158 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, kr
));
162 kr_delete(struct kroute
*kr
)
164 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, kr
));
168 kmpw_add(struct zapi_pw
*zpw
)
170 debug_zebra_out("pseudowire %s nexthop %s (add)",
171 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
173 return (zebra_send_pw(zclient
, ZEBRA_PW_ADD
, zpw
));
177 kmpw_del(struct zapi_pw
*zpw
)
179 debug_zebra_out("pseudowire %s nexthop %s (del)",
180 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
182 return (zebra_send_pw(zclient
, ZEBRA_PW_DELETE
, zpw
));
186 kmpw_set(struct zapi_pw
*zpw
)
188 debug_zebra_out("pseudowire %s nexthop %s labels %u/%u (set)",
189 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
),
190 zpw
->local_label
, zpw
->remote_label
);
192 return (zebra_send_pw(zclient
, ZEBRA_PW_SET
, zpw
));
196 kmpw_unset(struct zapi_pw
*zpw
)
198 debug_zebra_out("pseudowire %s nexthop %s (unset)",
199 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
201 return (zebra_send_pw(zclient
, ZEBRA_PW_UNSET
, zpw
));
205 kif_redistribute(const char *ifname
)
207 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
208 struct listnode
*cnode
;
209 struct interface
*ifp
;
210 struct connected
*ifc
;
214 FOR_ALL_INTERFACES (vrf
, ifp
) {
215 if (ifname
&& strcmp(ifname
, ifp
->name
) != 0)
219 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
221 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
222 ifc2kaddr(ifp
, ifc
, &ka
);
223 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
230 ldp_router_id_update(ZAPI_CALLBACK_ARGS
)
232 struct prefix router_id
;
234 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
236 if (bad_addr_v4(router_id
.u
.prefix4
))
239 debug_zebra_in("router-id update %s", inet_ntoa(router_id
.u
.prefix4
));
241 global
.rtr_id
.s_addr
= router_id
.u
.prefix4
.s_addr
;
242 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE
, 0, &global
.rtr_id
,
243 sizeof(global
.rtr_id
));
249 ldp_interface_add(ZAPI_CALLBACK_ARGS
)
251 struct interface
*ifp
;
254 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
255 debug_zebra_in("interface add %s index %d mtu %d", ifp
->name
,
256 ifp
->ifindex
, ifp
->mtu
);
259 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
265 ldp_interface_delete(ZAPI_CALLBACK_ARGS
)
267 struct interface
*ifp
;
270 /* zebra_interface_state_read() updates interface structure in iflist */
271 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
275 debug_zebra_in("interface delete %s index %d mtu %d", ifp
->name
,
276 ifp
->ifindex
, ifp
->mtu
);
278 /* To support pseudo interface do not free interface structure. */
279 /* if_delete(ifp); */
280 if_set_index(ifp
, IFINDEX_INTERNAL
);
283 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
289 ldp_interface_status_change(ZAPI_CALLBACK_ARGS
)
291 struct interface
*ifp
;
292 struct listnode
*node
;
293 struct connected
*ifc
;
298 * zebra_interface_state_read() updates interface structure in
301 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
305 debug_zebra_in("interface %s state update", ifp
->name
);
308 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
310 if (if_is_operative(ifp
)) {
311 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
312 ifc2kaddr(ifp
, ifc
, &ka
);
313 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
317 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
318 ifc2kaddr(ifp
, ifc
, &ka
);
319 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
,
328 ldp_interface_address_add(ZAPI_CALLBACK_ARGS
)
330 struct connected
*ifc
;
331 struct interface
*ifp
;
334 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
339 ifc2kaddr(ifp
, ifc
, &ka
);
341 /* Filter invalid addresses. */
342 if (bad_addr(ka
.af
, &ka
.addr
))
345 debug_zebra_in("address add %s/%u interface %s",
346 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
348 /* notify ldpe about new address */
349 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
, sizeof(ka
));
355 ldp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
357 struct connected
*ifc
;
358 struct interface
*ifp
;
361 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
366 ifc2kaddr(ifp
, ifc
, &ka
);
369 /* Filter invalid addresses. */
370 if (bad_addr(ka
.af
, &ka
.addr
))
373 debug_zebra_in("address delete %s/%u interface %s",
374 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
376 /* notify ldpe about removed address */
377 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
, sizeof(ka
));
383 ldp_zebra_read_route(ZAPI_CALLBACK_ARGS
)
385 struct zapi_route api
;
386 struct zapi_nexthop
*api_nh
;
390 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
393 /* we completely ignore srcdest routes for now. */
394 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
397 memset(&kr
, 0, sizeof(kr
));
398 kr
.af
= api
.prefix
.family
;
401 kr
.prefix
.v4
= api
.prefix
.u
.prefix4
;
404 kr
.prefix
.v6
= api
.prefix
.u
.prefix6
;
409 kr
.prefixlen
= api
.prefix
.prefixlen
;
410 kr
.priority
= api
.distance
;
413 case ZEBRA_ROUTE_CONNECT
:
414 kr
.flags
|= F_CONNECTED
;
416 case ZEBRA_ROUTE_BGP
:
417 /* LDP should follow the IGP and ignore BGP routes */
423 if (bad_addr(kr
.af
, &kr
.prefix
) ||
424 (kr
.af
== AF_INET6
&& IN6_IS_SCOPE_EMBED(&kr
.prefix
.v6
)))
427 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
430 if (api
.nexthop_num
== 0)
431 debug_zebra_in("route %s %s/%d (%s)", (add
) ? "add" : "delete",
432 log_addr(kr
.af
, &kr
.prefix
), kr
.prefixlen
,
433 zebra_route_string(api
.type
));
435 /* loop through all the nexthops */
436 for (i
= 0; i
< api
.nexthop_num
; i
++) {
437 api_nh
= &api
.nexthops
[i
];
438 switch (api_nh
->type
) {
439 case NEXTHOP_TYPE_IPV4
:
440 if (kr
.af
!= AF_INET
)
442 kr
.nexthop
.v4
= api_nh
->gate
.ipv4
;
445 case NEXTHOP_TYPE_IPV4_IFINDEX
:
446 if (kr
.af
!= AF_INET
)
448 kr
.nexthop
.v4
= api_nh
->gate
.ipv4
;
449 kr
.ifindex
= api_nh
->ifindex
;
451 case NEXTHOP_TYPE_IPV6
:
452 if (kr
.af
!= AF_INET6
)
454 kr
.nexthop
.v6
= api_nh
->gate
.ipv6
;
457 case NEXTHOP_TYPE_IPV6_IFINDEX
:
458 if (kr
.af
!= AF_INET6
)
460 kr
.nexthop
.v6
= api_nh
->gate
.ipv6
;
461 kr
.ifindex
= api_nh
->ifindex
;
463 case NEXTHOP_TYPE_IFINDEX
:
464 if (!(kr
.flags
& F_CONNECTED
))
471 debug_zebra_in("route %s %s/%d nexthop %s ifindex %u (%s)",
472 (add
) ? "add" : "delete", log_addr(kr
.af
, &kr
.prefix
),
473 kr
.prefixlen
, log_addr(kr
.af
, &kr
.nexthop
), kr
.ifindex
,
474 zebra_route_string(api
.type
));
477 main_imsg_compose_lde(IMSG_NETWORK_ADD
, 0, &kr
,
481 main_imsg_compose_lde(IMSG_NETWORK_UPDATE
, 0, &kr
, sizeof(kr
));
487 * Receive PW status update from Zebra and send it to LDE process.
490 ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
)
492 struct zapi_pw_status zpw
;
494 zebra_read_pw_status_update(cmd
, zclient
, length
, vrf_id
, &zpw
);
496 debug_zebra_in("pseudowire %s status %s", zpw
.ifname
,
497 (zpw
.status
== PW_STATUS_UP
) ? "up" : "down");
499 main_imsg_compose_lde(IMSG_PW_UPDATE
, 0, &zpw
, sizeof(zpw
));
505 ldp_zebra_connected(struct zclient
*zclient
)
507 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
508 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
509 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
510 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP6
,
511 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
514 extern struct zebra_privs_t ldpd_privs
;
517 ldp_zebra_init(struct thread_master
*master
)
519 /* Set default values. */
520 zclient
= zclient_new(master
, &zclient_options_default
);
521 zclient_init(zclient
, ZEBRA_ROUTE_LDP
, 0, &ldpd_privs
);
524 zclient
->zebra_connected
= ldp_zebra_connected
;
525 zclient
->router_id_update
= ldp_router_id_update
;
526 zclient
->interface_add
= ldp_interface_add
;
527 zclient
->interface_delete
= ldp_interface_delete
;
528 zclient
->interface_up
= ldp_interface_status_change
;
529 zclient
->interface_down
= ldp_interface_status_change
;
530 zclient
->interface_address_add
= ldp_interface_address_add
;
531 zclient
->interface_address_delete
= ldp_interface_address_delete
;
532 zclient
->redistribute_route_add
= ldp_zebra_read_route
;
533 zclient
->redistribute_route_del
= ldp_zebra_read_route
;
534 zclient
->pw_status_update
= ldp_zebra_read_pw_status_update
;
538 ldp_zebra_destroy(void)
540 zclient_stop(zclient
);
541 zclient_free(zclient
);