2 * Copyright (C) 2016 by Open Source Routing.
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
37 #include "ldp_debug.h"
39 static void ifp2kif(struct interface
*, struct kif
*);
40 static void ifc2kaddr(struct interface
*, struct connected
*,
42 static int zebra_send_mpls_labels(int, struct kroute
*);
43 static int ldp_router_id_update(int, struct zclient
*, zebra_size_t
,
45 static int ldp_interface_add(int, struct zclient
*, zebra_size_t
,
47 static int ldp_interface_delete(int, struct zclient
*, zebra_size_t
,
49 static int ldp_interface_status_change(int command
, struct zclient
*,
50 zebra_size_t
, vrf_id_t
);
51 static int ldp_interface_address_add(int, struct zclient
*, zebra_size_t
,
53 static int ldp_interface_address_delete(int, struct zclient
*,
54 zebra_size_t
, vrf_id_t
);
55 static int ldp_zebra_read_route(int, struct zclient
*, zebra_size_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
, ETHER_ADDR_LEN
);
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 zebra_send_mpls_labels(int cmd
, struct kroute
*kr
)
102 if (kr
->local_label
< MPLS_LABEL_RESERVED_MAX
||
103 kr
->remote_label
== NO_LABEL
)
106 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
107 log_addr(kr
->af
, &kr
->prefix
), kr
->prefixlen
,
108 log_addr(kr
->af
, &kr
->nexthop
), kr
->ifindex
,
109 log_label(kr
->local_label
), log_label(kr
->remote_label
),
110 (cmd
== ZEBRA_MPLS_LABELS_ADD
) ? "add" : "delete");
116 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
117 stream_putc(s
, ZEBRA_LSP_LDP
);
118 stream_putl(s
, kr
->af
);
121 stream_put_in_addr(s
, &kr
->prefix
.v4
);
122 stream_putc(s
, kr
->prefixlen
);
123 stream_put_in_addr(s
, &kr
->nexthop
.v4
);
126 stream_write(s
, (u_char
*)&kr
->prefix
.v6
, 16);
127 stream_putc(s
, kr
->prefixlen
);
128 stream_write(s
, (u_char
*)&kr
->nexthop
.v6
, 16);
131 fatalx("kr_change: unknown af");
133 stream_putl(s
, kr
->ifindex
);
134 stream_putc(s
, kr
->priority
);
135 stream_putl(s
, kr
->local_label
);
136 stream_putl(s
, kr
->remote_label
);
138 /* Put length at the first point of the stream. */
139 stream_putw_at(s
, 0, stream_get_endp(s
));
141 return (zclient_send_message(zclient
));
145 kr_change(struct kroute
*kr
)
147 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, kr
));
151 kr_delete(struct kroute
*kr
)
153 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, kr
));
157 kmpw_set(struct kpw
*kpw
)
164 kmpw_unset(struct kpw
*kpw
)
171 kif_redistribute(const char *ifname
)
173 struct listnode
*node
, *cnode
;
174 struct interface
*ifp
;
175 struct connected
*ifc
;
179 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT
), node
, ifp
)) {
180 if (ifname
&& strcmp(ifname
, ifp
->name
) != 0)
184 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
186 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
187 ifc2kaddr(ifp
, ifc
, &ka
);
188 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
195 ldp_router_id_update(int command
, struct zclient
*zclient
, zebra_size_t length
,
198 struct prefix router_id
;
200 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
202 if (bad_addr_v4(router_id
.u
.prefix4
))
205 debug_zebra_in("router-id update %s", inet_ntoa(router_id
.u
.prefix4
));
207 global
.rtr_id
.s_addr
= router_id
.u
.prefix4
.s_addr
;
208 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE
, 0, &global
.rtr_id
,
209 sizeof(global
.rtr_id
));
215 ldp_interface_add(int command
, struct zclient
*zclient
, zebra_size_t length
,
218 struct interface
*ifp
;
221 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
222 debug_zebra_in("interface add %s index %d mtu %d", ifp
->name
,
223 ifp
->ifindex
, ifp
->mtu
);
226 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
232 ldp_interface_delete(int command
, struct zclient
*zclient
, zebra_size_t length
,
235 struct interface
*ifp
;
238 /* zebra_interface_state_read() updates interface structure in iflist */
239 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
243 debug_zebra_in("interface delete %s index %d mtu %d", ifp
->name
,
244 ifp
->ifindex
, ifp
->mtu
);
246 /* To support pseudo interface do not free interface structure. */
247 /* if_delete(ifp); */
248 ifp
->ifindex
= IFINDEX_DELETED
;
251 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
257 ldp_interface_status_change(int command
, struct zclient
*zclient
,
258 zebra_size_t length
, vrf_id_t vrf_id
)
260 struct interface
*ifp
;
261 struct listnode
*node
;
262 struct connected
*ifc
;
267 * zebra_interface_state_read() updates interface structure in
270 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
274 debug_zebra_in("interface %s state update", ifp
->name
);
277 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
279 if (if_is_operative(ifp
)) {
280 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
281 ifc2kaddr(ifp
, ifc
, &ka
);
282 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
286 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
287 ifc2kaddr(ifp
, ifc
, &ka
);
288 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
,
297 ldp_interface_address_add(int command
, struct zclient
*zclient
,
298 zebra_size_t length
, vrf_id_t vrf_id
)
300 struct connected
*ifc
;
301 struct interface
*ifp
;
304 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
309 ifc2kaddr(ifp
, ifc
, &ka
);
311 /* Filter invalid addresses. */
312 if (bad_addr(ka
.af
, &ka
.addr
))
315 debug_zebra_in("address add %s/%u interface %s",
316 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
318 /* notify ldpe about new address */
319 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
, sizeof(ka
));
325 ldp_interface_address_delete(int command
, struct zclient
*zclient
,
326 zebra_size_t length
, vrf_id_t vrf_id
)
328 struct connected
*ifc
;
329 struct interface
*ifp
;
332 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
337 ifc2kaddr(ifp
, ifc
, &ka
);
340 /* Filter invalid addresses. */
341 if (bad_addr(ka
.af
, &ka
.addr
))
344 debug_zebra_in("address delete %s/%u interface %s",
345 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
347 /* notify ldpe about removed address */
348 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
, sizeof(ka
));
354 ldp_zebra_read_route(int command
, struct zclient
*zclient
, zebra_size_t length
,
359 u_char message_flags
;
361 int nhnum
= 0, nhlen
;
365 memset(&kr
, 0, sizeof(kr
));
368 type
= stream_getc(s
);
370 case ZEBRA_ROUTE_CONNECT
:
371 kr
.flags
|= F_CONNECTED
;
373 case ZEBRA_ROUTE_BGP
:
374 /* LDP should follow the IGP and ignore BGP routes */
380 stream_getl(s
); /* flags, unused */
381 stream_getw(s
); /* instance, unused */
382 message_flags
= stream_getc(s
);
385 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
386 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
388 nhlen
= sizeof(struct in_addr
);
390 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
391 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
393 nhlen
= sizeof(struct in6_addr
);
396 fatalx("ldp_zebra_read_route: unknown command");
398 kr
.prefixlen
= stream_getc(s
);
399 stream_get(&kr
.prefix
, s
, PSIZE(kr
.prefixlen
));
401 if (bad_addr(kr
.af
, &kr
.prefix
) ||
402 (kr
.af
== AF_INET6
&& IN6_IS_SCOPE_EMBED(&kr
.prefix
.v6
)))
405 if (kr
.af
== AF_INET6
&&
406 CHECK_FLAG(message_flags
, ZAPI_MESSAGE_SRCPFX
)) {
407 uint8_t src_prefixlen
;
409 src_prefixlen
= stream_getc(s
);
411 /* we completely ignore srcdest routes for now. */
416 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_NEXTHOP
)) {
417 nhnum
= stream_getc(s
);
418 nhmark
= stream_get_getp(s
);
419 stream_set_getp(s
, nhmark
+ nhnum
* (nhlen
+ 5));
422 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_DISTANCE
))
423 kr
.priority
= stream_getc(s
);
424 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_METRIC
))
425 stream_getl(s
); /* metric, not used */
427 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_NEXTHOP
))
428 stream_set_getp(s
, nhmark
);
430 if (command
== ZEBRA_REDISTRIBUTE_IPV4_ADD
||
431 command
== ZEBRA_REDISTRIBUTE_IPV6_ADD
)
435 debug_zebra_in("route %s %s/%d (%s)", (add
) ? "add" : "delete",
436 log_addr(kr
.af
, &kr
.prefix
), kr
.prefixlen
,
437 zebra_route_string(type
));
439 /* loop through all the nexthops */
440 for (; nhnum
> 0; nhnum
--) {
443 kr
.nexthop
.v4
.s_addr
= stream_get_ipv4(s
);
446 stream_get(&kr
.nexthop
.v6
, s
, sizeof(kr
.nexthop
.v6
));
451 stream_getc(s
); /* ifindex_num, unused. */
452 kr
.ifindex
= stream_getl(s
);
454 debug_zebra_in("route %s %s/%d nexthop %s ifindex %u (%s)",
455 (add
) ? "add" : "delete", log_addr(kr
.af
, &kr
.prefix
),
456 kr
.prefixlen
, log_addr(kr
.af
, &kr
.nexthop
), kr
.ifindex
,
457 zebra_route_string(type
));
460 main_imsg_compose_lde(IMSG_NETWORK_ADD
, 0, &kr
,
464 main_imsg_compose_lde(IMSG_NETWORK_UPDATE
, 0, &kr
, sizeof(kr
));
470 ldp_zebra_connected(struct zclient
*zclient
)
472 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
473 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
474 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
475 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP6
,
476 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
480 ldp_zebra_init(struct thread_master
*master
)
482 /* Set default values. */
483 zclient
= zclient_new(master
);
484 zclient_init(zclient
, ZEBRA_ROUTE_LDP
, 0);
487 zclient
->zebra_connected
= ldp_zebra_connected
;
488 zclient
->router_id_update
= ldp_router_id_update
;
489 zclient
->interface_add
= ldp_interface_add
;
490 zclient
->interface_delete
= ldp_interface_delete
;
491 zclient
->interface_up
= ldp_interface_status_change
;
492 zclient
->interface_down
= ldp_interface_status_change
;
493 zclient
->interface_address_add
= ldp_interface_address_add
;
494 zclient
->interface_address_delete
= ldp_interface_address_delete
;
495 zclient
->redistribute_route_ipv4_add
= ldp_zebra_read_route
;
496 zclient
->redistribute_route_ipv4_del
= ldp_zebra_read_route
;
497 zclient
->redistribute_route_ipv6_add
= ldp_zebra_read_route
;
498 zclient
->redistribute_route_ipv6_del
= ldp_zebra_read_route
;
502 ldp_zebra_destroy(void)
504 zclient_stop(zclient
);
505 zclient_free(zclient
);