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 void ldp_zebra_connected(struct zclient
*);
57 static struct zclient
*zclient
;
60 ifp2kif(struct interface
*ifp
, struct kif
*kif
)
62 memset(kif
, 0, sizeof(*kif
));
63 strlcpy(kif
->ifname
, ifp
->name
, sizeof(kif
->ifname
));
64 kif
->ifindex
= ifp
->ifindex
;
65 kif
->operative
= if_is_operative(ifp
);
66 if (ifp
->ll_type
== ZEBRA_LLT_ETHER
)
67 memcpy(kif
->mac
, ifp
->hw_addr
, ETHER_ADDR_LEN
);
71 ifc2kaddr(struct interface
*ifp
, struct connected
*ifc
, struct kaddr
*ka
)
73 memset(ka
, 0, sizeof(*ka
));
74 strlcpy(ka
->ifname
, ifp
->name
, sizeof(ka
->ifname
));
75 ka
->ifindex
= ifp
->ifindex
;
76 ka
->af
= ifc
->address
->family
;
77 ka
->prefixlen
= ifc
->address
->prefixlen
;
81 ka
->addr
.v4
= ifc
->address
->u
.prefix4
;
83 ka
->dstbrd
.v4
= ifc
->destination
->u
.prefix4
;
86 ka
->addr
.v6
= ifc
->address
->u
.prefix6
;
88 ka
->dstbrd
.v6
= ifc
->destination
->u
.prefix6
;
96 zebra_send_mpls_labels(int cmd
, struct kroute
*kr
)
100 if (kr
->local_label
< MPLS_LABEL_RESERVED_MAX
||
101 kr
->remote_label
== NO_LABEL
)
104 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
105 log_addr(kr
->af
, &kr
->prefix
), kr
->prefixlen
,
106 log_addr(kr
->af
, &kr
->nexthop
), kr
->ifindex
,
107 log_label(kr
->local_label
), log_label(kr
->remote_label
),
108 (cmd
== ZEBRA_MPLS_LABELS_ADD
) ? "add" : "delete");
114 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
115 stream_putc(s
, ZEBRA_LSP_LDP
);
116 stream_putl(s
, kr
->af
);
119 stream_put_in_addr(s
, &kr
->prefix
.v4
);
120 stream_putc(s
, kr
->prefixlen
);
121 stream_put_in_addr(s
, &kr
->nexthop
.v4
);
124 stream_write(s
, (u_char
*)&kr
->prefix
.v6
, 16);
125 stream_putc(s
, kr
->prefixlen
);
126 stream_write(s
, (u_char
*)&kr
->nexthop
.v6
, 16);
129 fatalx("kr_change: unknown af");
131 stream_putl(s
, kr
->ifindex
);
132 stream_putc(s
, kr
->priority
);
133 stream_putl(s
, kr
->local_label
);
134 stream_putl(s
, kr
->remote_label
);
136 /* Put length at the first point of the stream. */
137 stream_putw_at(s
, 0, stream_get_endp(s
));
139 return (zclient_send_message(zclient
));
143 kr_change(struct kroute
*kr
)
145 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, kr
));
149 kr_delete(struct kroute
*kr
)
151 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, kr
));
155 kmpw_set(struct kpw
*kpw
)
162 kmpw_unset(struct kpw
*kpw
)
169 kif_redistribute(const char *ifname
)
171 struct listnode
*node
, *cnode
;
172 struct interface
*ifp
;
173 struct connected
*ifc
;
177 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT
), node
, ifp
)) {
178 if (ifname
&& strcmp(ifname
, ifp
->name
) != 0)
182 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
184 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
185 ifc2kaddr(ifp
, ifc
, &ka
);
186 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
193 ldp_router_id_update(int command
, struct zclient
*zclient
, zebra_size_t length
,
196 struct prefix router_id
;
198 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
200 if (bad_addr_v4(router_id
.u
.prefix4
))
203 debug_zebra_in("router-id update %s", inet_ntoa(router_id
.u
.prefix4
));
205 global
.rtr_id
.s_addr
= router_id
.u
.prefix4
.s_addr
;
206 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE
, 0, &global
.rtr_id
,
207 sizeof(global
.rtr_id
));
213 ldp_interface_add(int command
, struct zclient
*zclient
, zebra_size_t length
,
216 struct interface
*ifp
;
219 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
220 debug_zebra_in("interface add %s index %d mtu %d", ifp
->name
,
221 ifp
->ifindex
, ifp
->mtu
);
224 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
230 ldp_interface_delete(int command
, struct zclient
*zclient
, zebra_size_t length
,
233 struct interface
*ifp
;
236 /* zebra_interface_state_read() updates interface structure in iflist */
237 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
241 debug_zebra_in("interface delete %s index %d mtu %d", ifp
->name
,
242 ifp
->ifindex
, ifp
->mtu
);
244 /* To support pseudo interface do not free interface structure. */
245 /* if_delete(ifp); */
246 ifp
->ifindex
= IFINDEX_DELETED
;
249 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
255 ldp_interface_status_change(int command
, struct zclient
*zclient
,
256 zebra_size_t length
, vrf_id_t vrf_id
)
258 struct interface
*ifp
;
259 struct listnode
*node
;
260 struct connected
*ifc
;
265 * zebra_interface_state_read() updates interface structure in
268 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
272 debug_zebra_in("interface %s state update", ifp
->name
);
275 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
277 if (if_is_operative(ifp
)) {
278 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
279 ifc2kaddr(ifp
, ifc
, &ka
);
280 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
284 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
285 ifc2kaddr(ifp
, ifc
, &ka
);
286 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
,
295 ldp_interface_address_add(int command
, struct zclient
*zclient
,
296 zebra_size_t length
, vrf_id_t vrf_id
)
298 struct connected
*ifc
;
299 struct interface
*ifp
;
302 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
307 ifc2kaddr(ifp
, ifc
, &ka
);
309 /* Filter invalid addresses. */
310 if (bad_addr(ka
.af
, &ka
.addr
))
313 debug_zebra_in("address add %s/%u interface %s",
314 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
316 /* notify ldpe about new address */
317 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
, sizeof(ka
));
323 ldp_interface_address_delete(int command
, struct zclient
*zclient
,
324 zebra_size_t length
, vrf_id_t vrf_id
)
326 struct connected
*ifc
;
327 struct interface
*ifp
;
330 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
335 ifc2kaddr(ifp
, ifc
, &ka
);
338 /* Filter invalid addresses. */
339 if (bad_addr(ka
.af
, &ka
.addr
))
342 debug_zebra_in("address delete %s/%u interface %s",
343 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
345 /* notify ldpe about removed address */
346 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
, sizeof(ka
));
352 ldp_zebra_read_route(int command
, struct zclient
*zclient
, zebra_size_t length
,
357 u_char message_flags
;
359 int nhnum
= 0, nhlen
;
363 memset(&kr
, 0, sizeof(kr
));
366 type
= stream_getc(s
);
368 case ZEBRA_ROUTE_CONNECT
:
369 kr
.flags
|= F_CONNECTED
;
371 case ZEBRA_ROUTE_BGP
:
372 /* LDP should follow the IGP and ignore BGP routes */
378 stream_getl(s
); /* flags, unused */
379 stream_getw(s
); /* instance, unused */
380 message_flags
= stream_getc(s
);
383 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
384 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
386 nhlen
= sizeof(struct in_addr
);
388 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
389 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
391 nhlen
= sizeof(struct in6_addr
);
394 fatalx("ldp_zebra_read_route: unknown command");
396 kr
.prefixlen
= stream_getc(s
);
397 stream_get(&kr
.prefix
, s
, PSIZE(kr
.prefixlen
));
399 if (bad_addr(kr
.af
, &kr
.prefix
) ||
400 (kr
.af
== AF_INET6
&& IN6_IS_SCOPE_EMBED(&kr
.prefix
.v6
)))
403 if (kr
.af
== AF_INET6
&&
404 CHECK_FLAG(message_flags
, ZAPI_MESSAGE_SRCPFX
)) {
405 uint8_t src_prefixlen
;
407 src_prefixlen
= stream_getc(s
);
409 /* we completely ignore srcdest routes for now. */
414 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_NEXTHOP
)) {
415 nhnum
= stream_getc(s
);
416 nhmark
= stream_get_getp(s
);
417 stream_set_getp(s
, nhmark
+ nhnum
* (nhlen
+ 5));
420 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_DISTANCE
))
421 kr
.priority
= stream_getc(s
);
422 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_METRIC
))
423 stream_getl(s
); /* metric, not used */
425 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_NEXTHOP
))
426 stream_set_getp(s
, nhmark
);
428 if (command
== ZEBRA_REDISTRIBUTE_IPV4_ADD
||
429 command
== ZEBRA_REDISTRIBUTE_IPV6_ADD
)
433 debug_zebra_in("route %s %s/%d (%s)", (add
) ? "add" : "delete",
434 log_addr(kr
.af
, &kr
.prefix
), kr
.prefixlen
,
435 zebra_route_string(type
));
437 /* loop through all the nexthops */
438 for (; nhnum
> 0; nhnum
--) {
441 kr
.nexthop
.v4
.s_addr
= stream_get_ipv4(s
);
444 stream_get(&kr
.nexthop
.v6
, s
, sizeof(kr
.nexthop
.v6
));
449 stream_getc(s
); /* ifindex_num, unused. */
450 kr
.ifindex
= stream_getl(s
);
452 debug_zebra_in("route %s %s/%d nexthop %s ifindex %u (%s)",
453 (add
) ? "add" : "delete", log_addr(kr
.af
, &kr
.prefix
),
454 kr
.prefixlen
, log_addr(kr
.af
, &kr
.nexthop
), kr
.ifindex
,
455 zebra_route_string(type
));
458 main_imsg_compose_lde(IMSG_NETWORK_ADD
, 0, &kr
,
462 main_imsg_compose_lde(IMSG_NETWORK_UPDATE
, 0, &kr
, sizeof(kr
));
468 ldp_zebra_connected(struct zclient
*zclient
)
470 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
471 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
472 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
473 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP6
,
474 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
478 ldp_zebra_init(struct thread_master
*master
)
480 /* Set default values. */
481 zclient
= zclient_new(master
);
482 zclient_init(zclient
, ZEBRA_ROUTE_LDP
, 0);
485 zclient
->zebra_connected
= ldp_zebra_connected
;
486 zclient
->router_id_update
= ldp_router_id_update
;
487 zclient
->interface_add
= ldp_interface_add
;
488 zclient
->interface_delete
= ldp_interface_delete
;
489 zclient
->interface_up
= ldp_interface_status_change
;
490 zclient
->interface_down
= ldp_interface_status_change
;
491 zclient
->interface_address_add
= ldp_interface_address_add
;
492 zclient
->interface_address_delete
= ldp_interface_address_delete
;
493 zclient
->redistribute_route_ipv4_add
= ldp_zebra_read_route
;
494 zclient
->redistribute_route_ipv4_del
= ldp_zebra_read_route
;
495 zclient
->redistribute_route_ipv6_add
= ldp_zebra_read_route
;
496 zclient
->redistribute_route_ipv6_del
= ldp_zebra_read_route
;
500 ldp_zebra_destroy(void)
502 zclient_stop(zclient
);
503 zclient_free(zclient
);