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 int ldp_zebra_read_pw_status_update(int, struct zclient
*,
58 zebra_size_t
, vrf_id_t
);
59 static void ldp_zebra_connected(struct zclient
*);
61 static struct zclient
*zclient
;
64 ifp2kif(struct interface
*ifp
, struct kif
*kif
)
66 memset(kif
, 0, sizeof(*kif
));
67 strlcpy(kif
->ifname
, ifp
->name
, sizeof(kif
->ifname
));
68 kif
->ifindex
= ifp
->ifindex
;
69 kif
->operative
= if_is_operative(ifp
);
70 if (ifp
->ll_type
== ZEBRA_LLT_ETHER
)
71 memcpy(kif
->mac
, ifp
->hw_addr
, ETHER_ADDR_LEN
);
75 ifc2kaddr(struct interface
*ifp
, struct connected
*ifc
, struct kaddr
*ka
)
77 memset(ka
, 0, sizeof(*ka
));
78 strlcpy(ka
->ifname
, ifp
->name
, sizeof(ka
->ifname
));
79 ka
->ifindex
= ifp
->ifindex
;
80 ka
->af
= ifc
->address
->family
;
81 ka
->prefixlen
= ifc
->address
->prefixlen
;
85 ka
->addr
.v4
= ifc
->address
->u
.prefix4
;
87 ka
->dstbrd
.v4
= ifc
->destination
->u
.prefix4
;
90 ka
->addr
.v6
= ifc
->address
->u
.prefix6
;
92 ka
->dstbrd
.v6
= ifc
->destination
->u
.prefix6
;
100 pw2zpw(struct l2vpn_pw
*pw
, struct zapi_pw
*zpw
)
102 memset(zpw
, 0, sizeof(*zpw
));
103 strlcpy(zpw
->ifname
, pw
->ifname
, sizeof(zpw
->ifname
));
104 zpw
->ifindex
= pw
->ifindex
;
105 zpw
->type
= pw
->l2vpn
->pw_type
;
107 zpw
->nexthop
.ipv6
= pw
->addr
.v6
;
108 zpw
->local_label
= NO_LABEL
;
109 zpw
->remote_label
= NO_LABEL
;
110 if (pw
->flags
& F_PW_CWORD
)
111 zpw
->flags
= F_PSEUDOWIRE_CWORD
;
112 zpw
->data
.ldp
.lsr_id
= pw
->lsr_id
;
113 zpw
->data
.ldp
.pwid
= pw
->pwid
;
114 strlcpy(zpw
->data
.ldp
.vpn_name
, pw
->l2vpn
->name
,
115 sizeof(zpw
->data
.ldp
.vpn_name
));
119 zebra_send_mpls_labels(int cmd
, struct kroute
*kr
)
123 if (kr
->local_label
< MPLS_LABEL_RESERVED_MAX
||
124 kr
->remote_label
== NO_LABEL
)
127 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
128 log_addr(kr
->af
, &kr
->prefix
), kr
->prefixlen
,
129 log_addr(kr
->af
, &kr
->nexthop
), kr
->ifindex
,
130 log_label(kr
->local_label
), log_label(kr
->remote_label
),
131 (cmd
== ZEBRA_MPLS_LABELS_ADD
) ? "add" : "delete");
137 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
138 stream_putc(s
, ZEBRA_LSP_LDP
);
139 stream_putl(s
, kr
->af
);
142 stream_put_in_addr(s
, &kr
->prefix
.v4
);
143 stream_putc(s
, kr
->prefixlen
);
144 stream_put_in_addr(s
, &kr
->nexthop
.v4
);
147 stream_write(s
, (u_char
*)&kr
->prefix
.v6
, 16);
148 stream_putc(s
, kr
->prefixlen
);
149 stream_write(s
, (u_char
*)&kr
->nexthop
.v6
, 16);
152 fatalx("kr_change: unknown af");
154 stream_putl(s
, kr
->ifindex
);
155 stream_putc(s
, kr
->priority
);
156 stream_putl(s
, kr
->local_label
);
157 stream_putl(s
, kr
->remote_label
);
159 /* Put length at the first point of the stream. */
160 stream_putw_at(s
, 0, stream_get_endp(s
));
162 return (zclient_send_message(zclient
));
166 kr_change(struct kroute
*kr
)
168 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, kr
));
172 kr_delete(struct kroute
*kr
)
174 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, kr
));
178 kmpw_add(struct zapi_pw
*zpw
)
180 debug_zebra_out("pseudowire %s nexthop %s (add)",
181 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
183 return (zebra_send_pw(zclient
, ZEBRA_PW_ADD
, zpw
));
187 kmpw_del(struct zapi_pw
*zpw
)
189 debug_zebra_out("pseudowire %s nexthop %s (del)",
190 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
192 return (zebra_send_pw(zclient
, ZEBRA_PW_DELETE
, zpw
));
196 kmpw_set(struct zapi_pw
*zpw
)
198 debug_zebra_out("pseudowire %s nexthop %s labels %u/%u (set)",
199 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
),
200 zpw
->local_label
, zpw
->remote_label
);
202 return (zebra_send_pw(zclient
, ZEBRA_PW_SET
, zpw
));
206 kmpw_unset(struct zapi_pw
*zpw
)
208 debug_zebra_out("pseudowire %s nexthop %s (unset)",
209 zpw
->ifname
, log_addr(zpw
->af
, (union ldpd_addr
*)&zpw
->nexthop
));
211 return (zebra_send_pw(zclient
, ZEBRA_PW_UNSET
, zpw
));
215 kif_redistribute(const char *ifname
)
217 struct listnode
*node
, *cnode
;
218 struct interface
*ifp
;
219 struct connected
*ifc
;
223 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT
), node
, ifp
)) {
224 if (ifname
&& strcmp(ifname
, ifp
->name
) != 0)
228 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
230 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
231 ifc2kaddr(ifp
, ifc
, &ka
);
232 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
239 ldp_router_id_update(int command
, struct zclient
*zclient
, zebra_size_t length
,
242 struct prefix router_id
;
244 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
246 if (bad_addr_v4(router_id
.u
.prefix4
))
249 debug_zebra_in("router-id update %s", inet_ntoa(router_id
.u
.prefix4
));
251 global
.rtr_id
.s_addr
= router_id
.u
.prefix4
.s_addr
;
252 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE
, 0, &global
.rtr_id
,
253 sizeof(global
.rtr_id
));
259 ldp_interface_add(int command
, struct zclient
*zclient
, zebra_size_t length
,
262 struct interface
*ifp
;
265 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
266 debug_zebra_in("interface add %s index %d mtu %d", ifp
->name
,
267 ifp
->ifindex
, ifp
->mtu
);
270 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
276 ldp_interface_delete(int command
, struct zclient
*zclient
, zebra_size_t length
,
279 struct interface
*ifp
;
282 /* zebra_interface_state_read() updates interface structure in iflist */
283 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
287 debug_zebra_in("interface delete %s index %d mtu %d", ifp
->name
,
288 ifp
->ifindex
, ifp
->mtu
);
290 /* To support pseudo interface do not free interface structure. */
291 /* if_delete(ifp); */
292 ifp
->ifindex
= IFINDEX_DELETED
;
295 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
301 ldp_interface_status_change(int command
, struct zclient
*zclient
,
302 zebra_size_t length
, vrf_id_t vrf_id
)
304 struct interface
*ifp
;
305 struct listnode
*node
;
306 struct connected
*ifc
;
311 * zebra_interface_state_read() updates interface structure in
314 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
318 debug_zebra_in("interface %s state update", ifp
->name
);
321 main_imsg_compose_both(IMSG_IFSTATUS
, &kif
, sizeof(kif
));
323 if (if_is_operative(ifp
)) {
324 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
325 ifc2kaddr(ifp
, ifc
, &ka
);
326 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
,
330 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
331 ifc2kaddr(ifp
, ifc
, &ka
);
332 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
,
341 ldp_interface_address_add(int command
, struct zclient
*zclient
,
342 zebra_size_t length
, vrf_id_t vrf_id
)
344 struct connected
*ifc
;
345 struct interface
*ifp
;
348 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
353 ifc2kaddr(ifp
, ifc
, &ka
);
355 /* Filter invalid addresses. */
356 if (bad_addr(ka
.af
, &ka
.addr
))
359 debug_zebra_in("address add %s/%u interface %s",
360 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
362 /* notify ldpe about new address */
363 main_imsg_compose_ldpe(IMSG_NEWADDR
, 0, &ka
, sizeof(ka
));
369 ldp_interface_address_delete(int command
, struct zclient
*zclient
,
370 zebra_size_t length
, vrf_id_t vrf_id
)
372 struct connected
*ifc
;
373 struct interface
*ifp
;
376 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
381 ifc2kaddr(ifp
, ifc
, &ka
);
384 /* Filter invalid addresses. */
385 if (bad_addr(ka
.af
, &ka
.addr
))
388 debug_zebra_in("address delete %s/%u interface %s",
389 log_addr(ka
.af
, &ka
.addr
), ka
.prefixlen
, ifp
->name
);
391 /* notify ldpe about removed address */
392 main_imsg_compose_ldpe(IMSG_DELADDR
, 0, &ka
, sizeof(ka
));
398 ldp_zebra_read_route(int command
, struct zclient
*zclient
, zebra_size_t length
,
403 u_char message_flags
;
405 int nhnum
= 0, nhlen
;
409 memset(&kr
, 0, sizeof(kr
));
412 type
= stream_getc(s
);
414 case ZEBRA_ROUTE_CONNECT
:
415 kr
.flags
|= F_CONNECTED
;
417 case ZEBRA_ROUTE_BGP
:
418 /* LDP should follow the IGP and ignore BGP routes */
424 stream_getl(s
); /* flags, unused */
425 stream_getw(s
); /* instance, unused */
426 message_flags
= stream_getc(s
);
429 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
430 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
432 nhlen
= sizeof(struct in_addr
);
434 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
435 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
437 nhlen
= sizeof(struct in6_addr
);
440 fatalx("ldp_zebra_read_route: unknown command");
442 kr
.prefixlen
= stream_getc(s
);
443 stream_get(&kr
.prefix
, s
, PSIZE(kr
.prefixlen
));
445 if (bad_addr(kr
.af
, &kr
.prefix
) ||
446 (kr
.af
== AF_INET6
&& IN6_IS_SCOPE_EMBED(&kr
.prefix
.v6
)))
449 if (kr
.af
== AF_INET6
&&
450 CHECK_FLAG(message_flags
, ZAPI_MESSAGE_SRCPFX
)) {
451 uint8_t src_prefixlen
;
453 src_prefixlen
= stream_getc(s
);
455 /* we completely ignore srcdest routes for now. */
460 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_NEXTHOP
)) {
461 nhnum
= stream_getc(s
);
462 nhmark
= stream_get_getp(s
);
463 stream_set_getp(s
, nhmark
+ nhnum
* (nhlen
+ 5));
466 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_DISTANCE
))
467 kr
.priority
= stream_getc(s
);
468 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_METRIC
))
469 stream_getl(s
); /* metric, not used */
471 if (CHECK_FLAG(message_flags
, ZAPI_MESSAGE_NEXTHOP
))
472 stream_set_getp(s
, nhmark
);
474 if (command
== ZEBRA_REDISTRIBUTE_IPV4_ADD
||
475 command
== ZEBRA_REDISTRIBUTE_IPV6_ADD
)
479 debug_zebra_in("route %s %s/%d (%s)", (add
) ? "add" : "delete",
480 log_addr(kr
.af
, &kr
.prefix
), kr
.prefixlen
,
481 zebra_route_string(type
));
483 /* loop through all the nexthops */
484 for (; nhnum
> 0; nhnum
--) {
487 kr
.nexthop
.v4
.s_addr
= stream_get_ipv4(s
);
490 stream_get(&kr
.nexthop
.v6
, s
, sizeof(kr
.nexthop
.v6
));
495 stream_getc(s
); /* ifindex_num, unused. */
496 kr
.ifindex
= stream_getl(s
);
498 debug_zebra_in("route %s %s/%d nexthop %s ifindex %u (%s)",
499 (add
) ? "add" : "delete", log_addr(kr
.af
, &kr
.prefix
),
500 kr
.prefixlen
, log_addr(kr
.af
, &kr
.nexthop
), kr
.ifindex
,
501 zebra_route_string(type
));
504 main_imsg_compose_lde(IMSG_NETWORK_ADD
, 0, &kr
,
508 main_imsg_compose_lde(IMSG_NETWORK_UPDATE
, 0, &kr
, sizeof(kr
));
514 * Receive PW status update from Zebra and send it to LDE process.
517 ldp_zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
518 zebra_size_t length
, vrf_id_t vrf_id
)
520 struct zapi_pw_status zpw
;
522 zebra_read_pw_status_update(command
, zclient
, length
, vrf_id
, &zpw
);
524 debug_zebra_in("pseudowire %s status %s", zpw
.ifname
,
525 (zpw
.status
== PW_STATUS_UP
) ? "up" : "down");
527 main_imsg_compose_lde(IMSG_PW_UPDATE
, 0, &zpw
, sizeof(zpw
));
533 ldp_zebra_connected(struct zclient
*zclient
)
535 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
536 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
537 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
538 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP6
,
539 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
543 ldp_zebra_init(struct thread_master
*master
)
545 /* Set default values. */
546 zclient
= zclient_new(master
);
547 zclient_init(zclient
, ZEBRA_ROUTE_LDP
, 0);
550 zclient
->zebra_connected
= ldp_zebra_connected
;
551 zclient
->router_id_update
= ldp_router_id_update
;
552 zclient
->interface_add
= ldp_interface_add
;
553 zclient
->interface_delete
= ldp_interface_delete
;
554 zclient
->interface_up
= ldp_interface_status_change
;
555 zclient
->interface_down
= ldp_interface_status_change
;
556 zclient
->interface_address_add
= ldp_interface_address_add
;
557 zclient
->interface_address_delete
= ldp_interface_address_delete
;
558 zclient
->redistribute_route_ipv4_add
= ldp_zebra_read_route
;
559 zclient
->redistribute_route_ipv4_del
= ldp_zebra_read_route
;
560 zclient
->redistribute_route_ipv6_add
= ldp_zebra_read_route
;
561 zclient
->redistribute_route_ipv6_del
= ldp_zebra_read_route
;
562 zclient
->pw_status_update
= ldp_zebra_read_pw_status_update
;
566 ldp_zebra_destroy(void)
568 zclient_stop(zclient
);
569 zclient_free(zclient
);