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(int, struct zclient
*, zebra_size_t
,
43 static int ldp_interface_add(int, struct zclient
*, zebra_size_t
,
45 static int ldp_interface_delete(int, struct zclient
*, zebra_size_t
,
47 static int ldp_interface_status_change(int command
, struct zclient
*,
48 zebra_size_t
, vrf_id_t
);
49 static int ldp_interface_address_add(int, struct zclient
*, zebra_size_t
,
51 static int ldp_interface_address_delete(int, struct zclient
*,
52 zebra_size_t
, vrf_id_t
);
53 static int ldp_zebra_read_route(int, struct zclient
*, zebra_size_t
,
55 static int ldp_zebra_read_pw_status_update(int, struct zclient
*,
56 zebra_size_t
, vrf_id_t
);
57 static void ldp_zebra_connected(struct zclient
*);
59 static struct zclient
*zclient
;
62 ifp2kif(struct interface
*ifp
, struct kif
*kif
)
64 memset(kif
, 0, sizeof(*kif
));
65 strlcpy(kif
->ifname
, ifp
->name
, sizeof(kif
->ifname
));
66 kif
->ifindex
= ifp
->ifindex
;
67 kif
->operative
= if_is_operative(ifp
);
68 if (ifp
->ll_type
== ZEBRA_LLT_ETHER
)
69 memcpy(kif
->mac
, ifp
->hw_addr
, ETH_ALEN
);
73 ifc2kaddr(struct interface
*ifp
, struct connected
*ifc
, struct kaddr
*ka
)
75 memset(ka
, 0, sizeof(*ka
));
76 strlcpy(ka
->ifname
, ifp
->name
, sizeof(ka
->ifname
));
77 ka
->ifindex
= ifp
->ifindex
;
78 ka
->af
= ifc
->address
->family
;
79 ka
->prefixlen
= ifc
->address
->prefixlen
;
83 ka
->addr
.v4
= ifc
->address
->u
.prefix4
;
85 ka
->dstbrd
.v4
= ifc
->destination
->u
.prefix4
;
88 ka
->addr
.v6
= ifc
->address
->u
.prefix6
;
90 ka
->dstbrd
.v6
= ifc
->destination
->u
.prefix6
;
98 pw2zpw(struct l2vpn_pw
*pw
, struct zapi_pw
*zpw
)
100 memset(zpw
, 0, sizeof(*zpw
));
101 strlcpy(zpw
->ifname
, pw
->ifname
, sizeof(zpw
->ifname
));
102 zpw
->ifindex
= pw
->ifindex
;
103 zpw
->type
= pw
->l2vpn
->pw_type
;
105 zpw
->nexthop
.ipv6
= pw
->addr
.v6
;
106 zpw
->local_label
= NO_LABEL
;
107 zpw
->remote_label
= NO_LABEL
;
108 if (pw
->flags
& F_PW_CWORD
)
109 zpw
->flags
= F_PSEUDOWIRE_CWORD
;
110 zpw
->data
.ldp
.lsr_id
= pw
->lsr_id
;
111 zpw
->data
.ldp
.pwid
= pw
->pwid
;
112 strlcpy(zpw
->data
.ldp
.vpn_name
, pw
->l2vpn
->name
,
113 sizeof(zpw
->data
.ldp
.vpn_name
));
117 zebra_send_mpls_labels(int cmd
, struct kroute
*kr
)
121 if (kr
->local_label
< MPLS_LABEL_RESERVED_MAX
||
122 kr
->remote_label
== NO_LABEL
)
125 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
126 log_addr(kr
->af
, &kr
->prefix
), kr
->prefixlen
,
127 log_addr(kr
->af
, &kr
->nexthop
), kr
->ifindex
,
128 log_label(kr
->local_label
), log_label(kr
->remote_label
),
129 (cmd
== ZEBRA_MPLS_LABELS_ADD
) ? "add" : "delete");
135 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
136 stream_putc(s
, ZEBRA_LSP_LDP
);
137 stream_putl(s
, kr
->af
);
140 stream_put_in_addr(s
, &kr
->prefix
.v4
);
141 stream_putc(s
, kr
->prefixlen
);
142 stream_put_in_addr(s
, &kr
->nexthop
.v4
);
145 stream_write(s
, (uint8_t *)&kr
->prefix
.v6
, 16);
146 stream_putc(s
, kr
->prefixlen
);
147 stream_write(s
, (uint8_t *)&kr
->nexthop
.v6
, 16);
150 fatalx("kr_change: unknown af");
152 stream_putl(s
, kr
->ifindex
);
153 stream_putc(s
, kr
->priority
);
154 stream_putl(s
, kr
->local_label
);
155 stream_putl(s
, kr
->remote_label
);
157 /* Put length at the first point of the stream. */
158 stream_putw_at(s
, 0, stream_get_endp(s
));
160 return (zclient_send_message(zclient
));
164 kr_change(struct kroute
*kr
)
166 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, kr
));
170 kr_delete(struct kroute
*kr
)
172 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, kr
));
176 kmpw_add(struct zapi_pw
*zpw
)
178 debug_zebra_out("pseudowire %s nexthop %s (add)",
179 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
181 return (zebra_send_pw(zclient
, ZEBRA_PW_ADD
, zpw
));
185 kmpw_del(struct zapi_pw
*zpw
)
187 debug_zebra_out("pseudowire %s nexthop %s (del)",
188 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
190 return (zebra_send_pw(zclient
, ZEBRA_PW_DELETE
, zpw
));
194 kmpw_set(struct zapi_pw
*zpw
)
196 debug_zebra_out("pseudowire %s nexthop %s labels %u/%u (set)",
197 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
),
198 zpw
->local_label
, zpw
->remote_label
);
200 return (zebra_send_pw(zclient
, ZEBRA_PW_SET
, zpw
));
204 kmpw_unset(struct zapi_pw
*zpw
)
206 debug_zebra_out("pseudowire %s nexthop %s (unset)",
207 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
209 return (zebra_send_pw(zclient
, ZEBRA_PW_UNSET
, zpw
));
213 kif_redistribute(const char *ifname
)
215 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
216 struct listnode
*cnode
;
217 struct interface
*ifp
;
218 struct connected
*ifc
;
222 FOR_ALL_INTERFACES (vrf
, ifp
) {
223 if (ifname
&& strcmp(ifname
, ifp
->name
) != 0)
227 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
229 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
230 ifc2kaddr(ifp
, ifc
, &ka
);
231 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
238 ldp_router_id_update(int command
, struct zclient
*zclient
, zebra_size_t length
,
241 struct prefix router_id
;
243 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
245 if (bad_addr_v4(router_id
.u
.prefix4
))
248 debug_zebra_in("router-id update %s", inet_ntoa(router_id
.u
.prefix4
));
250 global
.rtr_id
.s_addr
= router_id
.u
.prefix4
.s_addr
;
251 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE
, 0, &global
.rtr_id
,
252 sizeof(global
.rtr_id
));
258 ldp_interface_add(int command
, struct zclient
*zclient
, zebra_size_t length
,
261 struct interface
*ifp
;
264 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
265 debug_zebra_in("interface add %s index %d mtu %d", ifp
->name
,
266 ifp
->ifindex
, ifp
->mtu
);
269 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
275 ldp_interface_delete(int command
, struct zclient
*zclient
, zebra_size_t length
,
278 struct interface
*ifp
;
281 /* zebra_interface_state_read() updates interface structure in iflist */
282 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
286 debug_zebra_in("interface delete %s index %d mtu %d", ifp
->name
,
287 ifp
->ifindex
, ifp
->mtu
);
289 /* To support pseudo interface do not free interface structure. */
290 /* if_delete(ifp); */
291 if_set_index(ifp
, IFINDEX_INTERNAL
);
294 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
300 ldp_interface_status_change(int command
, struct zclient
*zclient
,
301 zebra_size_t length
, vrf_id_t vrf_id
)
303 struct interface
*ifp
;
304 struct listnode
*node
;
305 struct connected
*ifc
;
310 * zebra_interface_state_read() updates interface structure in
313 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
317 debug_zebra_in("interface %s state update", ifp
->name
);
320 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
322 if (if_is_operative(ifp
)) {
323 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
324 ifc2kaddr(ifp
, ifc
, &ka
);
325 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
329 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
330 ifc2kaddr(ifp
, ifc
, &ka
);
331 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
,
340 ldp_interface_address_add(int command
, struct zclient
*zclient
,
341 zebra_size_t length
, vrf_id_t vrf_id
)
343 struct connected
*ifc
;
344 struct interface
*ifp
;
347 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
352 ifc2kaddr(ifp
, ifc
, &ka
);
354 /* Filter invalid addresses. */
355 if (bad_addr(ka
.af
, &ka
.addr
))
358 debug_zebra_in("address add %s/%u interface %s",
359 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
361 /* notify ldpe about new address */
362 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
, sizeof(ka
));
368 ldp_interface_address_delete(int command
, struct zclient
*zclient
,
369 zebra_size_t length
, vrf_id_t vrf_id
)
371 struct connected
*ifc
;
372 struct interface
*ifp
;
375 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
380 ifc2kaddr(ifp
, ifc
, &ka
);
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(int command
, struct zclient
*zclient
, zebra_size_t length
,
400 struct zapi_route api
;
401 struct zapi_nexthop
*api_nh
;
405 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
408 /* we completely ignore srcdest routes for now. */
409 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
412 memset(&kr
, 0, sizeof(kr
));
413 kr
.af
= api
.prefix
.family
;
416 kr
.prefix
.v4
= api
.prefix
.u
.prefix4
;
419 kr
.prefix
.v6
= api
.prefix
.u
.prefix6
;
424 kr
.prefixlen
= api
.prefix
.prefixlen
;
425 kr
.priority
= api
.distance
;
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 (command
== 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(int command
, struct zclient
*zclient
,
506 zebra_size_t length
, vrf_id_t vrf_id
)
508 struct zapi_pw_status zpw
;
510 zebra_read_pw_status_update(command
, zclient
, length
, vrf_id
, &zpw
);
512 debug_zebra_in("pseudowire %s status %s", zpw
.ifname
,
513 (zpw
.status
== PW_STATUS_UP
) ? "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
);
530 extern struct zebra_privs_t ldpd_privs
;
533 ldp_zebra_init(struct thread_master
*master
)
535 /* Set default values. */
536 zclient
= zclient_new_notify(master
, &zclient_options_default
);
537 zclient_init(zclient
, ZEBRA_ROUTE_LDP
, 0, &ldpd_privs
);
540 zclient
->zebra_connected
= ldp_zebra_connected
;
541 zclient
->router_id_update
= ldp_router_id_update
;
542 zclient
->interface_add
= ldp_interface_add
;
543 zclient
->interface_delete
= ldp_interface_delete
;
544 zclient
->interface_up
= ldp_interface_status_change
;
545 zclient
->interface_down
= ldp_interface_status_change
;
546 zclient
->interface_address_add
= ldp_interface_address_add
;
547 zclient
->interface_address_delete
= ldp_interface_address_delete
;
548 zclient
->redistribute_route_add
= ldp_zebra_read_route
;
549 zclient
->redistribute_route_del
= ldp_zebra_read_route
;
550 zclient
->pw_status_update
= ldp_zebra_read_pw_status_update
;
554 ldp_zebra_destroy(void)
556 zclient_stop(zclient
);
557 zclient_free(zclient
);