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
->flags
= ifp
->flags
;
71 ifc2kaddr(struct interface
*ifp
, struct connected
*ifc
, struct kaddr
*ka
)
73 memset(ka
, 0, sizeof(*ka
));
74 ka
->ifindex
= ifp
->ifindex
;
75 ka
->af
= ifc
->address
->family
;
76 ka
->prefixlen
= ifc
->address
->prefixlen
;
80 ka
->addr
.v4
= ifc
->address
->u
.prefix4
;
82 ka
->dstbrd
.v4
= ifc
->destination
->u
.prefix4
;
85 ka
->addr
.v6
= ifc
->address
->u
.prefix6
;
87 ka
->dstbrd
.v6
= ifc
->destination
->u
.prefix6
;
95 zebra_send_mpls_labels(int cmd
, struct kroute
*kr
)
99 if (kr
->local_label
< MPLS_LABEL_RESERVED_MAX
||
100 kr
->remote_label
== NO_LABEL
)
103 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
104 log_addr(kr
->af
, &kr
->prefix
), kr
->prefixlen
,
105 log_addr(kr
->af
, &kr
->nexthop
), kr
->ifindex
,
106 log_label(kr
->local_label
), log_label(kr
->remote_label
),
107 (cmd
== ZEBRA_MPLS_LABELS_ADD
) ? "add" : "delete");
113 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
114 stream_putc(s
, ZEBRA_LSP_LDP
);
115 stream_putl(s
, kr
->af
);
118 stream_put_in_addr(s
, &kr
->prefix
.v4
);
119 stream_putc(s
, kr
->prefixlen
);
120 stream_put_in_addr(s
, &kr
->nexthop
.v4
);
123 stream_write(s
, (u_char
*)&kr
->prefix
.v6
, 16);
124 stream_putc(s
, kr
->prefixlen
);
125 stream_write(s
, (u_char
*)&kr
->nexthop
.v6
, 16);
128 fatalx("kr_change: unknown af");
130 stream_putl(s
, kr
->ifindex
);
131 stream_putc(s
, kr
->priority
);
132 stream_putl(s
, kr
->local_label
);
133 stream_putl(s
, kr
->remote_label
);
135 /* Put length at the first point of the stream. */
136 stream_putw_at(s
, 0, stream_get_endp(s
));
138 return (zclient_send_message(zclient
));
142 kr_change(struct kroute
*kr
)
144 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, kr
));
148 kr_delete(struct kroute
*kr
)
150 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, kr
));
154 kmpw_set(struct kpw
*kpw
)
161 kmpw_unset(struct kpw
*kpw
)
168 kif_redistribute(const char *ifname
)
170 struct listnode
*node
, *cnode
;
171 struct interface
*ifp
;
172 struct connected
*ifc
;
176 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT
), node
, ifp
)) {
177 if (ifname
&& strcmp(ifname
, ifp
->name
) != 0)
181 main_imsg_compose_ldpe(IMSG_IFSTATUS
, 0, &kif
, sizeof(kif
));
183 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
184 ifc2kaddr(ifp
, ifc
, &ka
);
185 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
192 ldp_router_id_update(int command
, struct zclient
*zclient
, zebra_size_t length
,
195 struct prefix router_id
;
197 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
199 if (bad_addr_v4(router_id
.u
.prefix4
))
202 debug_zebra_in("router-id update %s", inet_ntoa(router_id
.u
.prefix4
));
204 global
.rtr_id
.s_addr
= router_id
.u
.prefix4
.s_addr
;
205 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE
, 0, &global
.rtr_id
,
206 sizeof(global
.rtr_id
));
212 ldp_interface_add(int command
, struct zclient
*zclient
, zebra_size_t length
,
215 struct interface
*ifp
;
218 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
219 debug_zebra_in("interface add %s index %d mtu %d", ifp
->name
,
220 ifp
->ifindex
, ifp
->mtu
);
223 main_imsg_compose_ldpe(IMSG_IFSTATUS
, 0, &kif
, sizeof(kif
));
229 ldp_interface_delete(int command
, struct zclient
*zclient
, zebra_size_t length
,
232 struct interface
*ifp
;
234 /* zebra_interface_state_read() updates interface structure in iflist */
235 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
239 debug_zebra_in("interface delete %s index %d mtu %d", ifp
->name
,
240 ifp
->ifindex
, ifp
->mtu
);
242 /* To support pseudo interface do not free interface structure. */
243 /* if_delete(ifp); */
244 ifp
->ifindex
= IFINDEX_INTERNAL
;
250 ldp_interface_status_change(int command
, struct zclient
*zclient
,
251 zebra_size_t length
, vrf_id_t vrf_id
)
253 struct interface
*ifp
;
254 struct listnode
*node
;
255 struct connected
*ifc
;
261 * zebra_interface_state_read() updates interface structure in
264 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
268 debug_zebra_in("interface %s state update", ifp
->name
);
271 main_imsg_compose_ldpe(IMSG_IFSTATUS
, 0, &kif
, sizeof(kif
));
273 link_new
= (ifp
->flags
& IFF_UP
) && (ifp
->flags
& IFF_RUNNING
);
275 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
276 ifc2kaddr(ifp
, ifc
, &ka
);
277 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
281 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
282 ifc2kaddr(ifp
, ifc
, &ka
);
283 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
,
292 ldp_interface_address_add(int command
, struct zclient
*zclient
,
293 zebra_size_t length
, vrf_id_t vrf_id
)
295 struct connected
*ifc
;
296 struct interface
*ifp
;
299 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
304 ifc2kaddr(ifp
, ifc
, &ka
);
306 /* Filter invalid addresses. */
307 if (bad_addr(ka
.af
, &ka
.addr
))
310 debug_zebra_in("address add %s/%u", log_addr(ka
.af
, &ka
.addr
),
313 /* notify ldpe about new address */
314 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
, sizeof(ka
));
320 ldp_interface_address_delete(int command
, struct zclient
*zclient
,
321 zebra_size_t length
, vrf_id_t vrf_id
)
323 struct connected
*ifc
;
324 struct interface
*ifp
;
327 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
332 ifc2kaddr(ifp
, ifc
, &ka
);
335 /* Filter invalid addresses. */
336 if (bad_addr(ka
.af
, &ka
.addr
))
339 debug_zebra_in("address delete %s/%u", log_addr(ka
.af
, &ka
.addr
),
342 /* notify ldpe about removed address */
343 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
, sizeof(ka
));
349 ldp_zebra_read_route(int command
, struct zclient
*zclient
, zebra_size_t length
,
354 u_char message_flags
;
356 int nhnum
= 0, nhlen
;
359 memset(&kr
, 0, sizeof(kr
));
362 type
= stream_getc(s
);
364 case ZEBRA_ROUTE_CONNECT
:
365 kr
.flags
|= F_CONNECTED
;
367 case ZEBRA_ROUTE_BGP
:
368 /* LDP should follow the IGP and ignore BGP routes */
374 stream_getl(s
); /* flags, unused */
375 stream_getw(s
); /* instance, unused */
376 message_flags
= stream_getc(s
);
379 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
380 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
382 nhlen
= sizeof(struct in_addr
);
384 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
385 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
387 nhlen
= sizeof(struct in6_addr
);
390 fatalx("ldp_zebra_read_route: unknown command");
392 kr
.prefixlen
= stream_getc(s
);
393 stream_get(&kr
.prefix
, s
, PSIZE(kr
.prefixlen
));
395 if (bad_addr(kr
.af
, &kr
.prefix
) ||
396 (kr
.af
== AF_INET6
&& IN6_IS_SCOPE_EMBED(&kr
.prefix
.v6
)))
399 if (kr
.af
== AF_INET6
&&
400 CHECK_FLAG(message_flags
, ZAPI_MESSAGE_SRCPFX
)) {
401 uint8_t src_prefixlen
;
403 src_prefixlen
= stream_getc(s
);
405 /* we completely ignore srcdest routes for now. */
410 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_NEXTHOP
)) {
411 nhnum
= stream_getc(s
);
412 nhmark
= stream_get_getp(s
);
413 stream_set_getp(s
, nhmark
+ nhnum
* (nhlen
+ 5));
416 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_DISTANCE
))
417 kr
.priority
= stream_getc(s
);
418 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_METRIC
))
419 stream_getl(s
); /* metric, not used */
421 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_NEXTHOP
))
422 stream_set_getp(s
, nhmark
);
426 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
427 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
429 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
430 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
431 debug_zebra_in("route delete %s/%d (%s)",
432 log_addr(kr
.af
, &kr
.prefix
), kr
.prefixlen
,
433 zebra_route_string(type
));
436 fatalx("ldp_zebra_read_route: unknown command");
440 /* loop through all the nexthops */
441 for (; nhnum
> 0; nhnum
--) {
444 kr
.nexthop
.v4
.s_addr
= stream_get_ipv4(s
);
447 stream_get(&kr
.nexthop
.v6
, s
, sizeof(kr
.nexthop
.v6
));
452 stream_getc(s
); /* ifindex_num, unused. */
453 kr
.ifindex
= stream_getl(s
);
456 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
457 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
458 debug_zebra_in("route add %s/%d nexthop %s "
459 "ifindex %u (%s)", log_addr(kr
.af
, &kr
.prefix
),
460 kr
.prefixlen
, log_addr(kr
.af
, &kr
.nexthop
),
461 kr
.ifindex
, zebra_route_string(type
));
462 main_imsg_compose_lde(IMSG_NETWORK_ADD
, 0, &kr
,
470 main_imsg_compose_lde(IMSG_NETWORK_UPDATE
, 0, &kr
, sizeof(kr
));
476 ldp_zebra_connected(struct zclient
*zclient
)
478 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
479 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
480 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
481 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP6
,
482 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
486 ldp_zebra_init(struct thread_master
*master
)
488 /* Set default values. */
489 zclient
= zclient_new(master
);
490 zclient_init(zclient
, ZEBRA_ROUTE_LDP
, 0);
493 zclient
->zebra_connected
= ldp_zebra_connected
;
494 zclient
->router_id_update
= ldp_router_id_update
;
495 zclient
->interface_add
= ldp_interface_add
;
496 zclient
->interface_delete
= ldp_interface_delete
;
497 zclient
->interface_up
= ldp_interface_status_change
;
498 zclient
->interface_down
= ldp_interface_status_change
;
499 zclient
->interface_address_add
= ldp_interface_address_add
;
500 zclient
->interface_address_delete
= ldp_interface_address_delete
;
501 zclient
->redistribute_route_ipv4_add
= ldp_zebra_read_route
;
502 zclient
->redistribute_route_ipv4_del
= ldp_zebra_read_route
;
503 zclient
->redistribute_route_ipv6_add
= ldp_zebra_read_route
;
504 zclient
->redistribute_route_ipv6_del
= ldp_zebra_read_route
;
508 ldp_zebra_destroy(void)
510 zclient_stop(zclient
);
511 zclient_free(zclient
);