]> git.proxmox.com Git - mirror_frr.git/blame - ldpd/ldp_zebra.c
sharpd: Add missing yang route-map callbacks
[mirror_frr.git] / ldpd / ldp_zebra.c
CommitLineData
eac6e3f0
RW
1/*
2 * Copyright (C) 2016 by Open Source Routing.
3 *
180fc2cd
RW
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.
eac6e3f0 8 *
180fc2cd 9 * This program is distributed in the hope that it will be useful, but
eac6e3f0
RW
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.
13 *
180fc2cd
RW
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,
17 * MA 02110-1301 USA
eac6e3f0
RW
18 */
19
20#include <zebra.h>
21
22#include "prefix.h"
23#include "stream.h"
24#include "memory.h"
25#include "zclient.h"
26#include "command.h"
27#include "network.h"
28#include "linklist.h"
ce549947 29#include "mpls.h"
eac6e3f0
RW
30
31#include "ldpd.h"
32#include "ldpe.h"
33#include "lde.h"
34#include "log.h"
35#include "ldp_debug.h"
36
37static void ifp2kif(struct interface *, struct kif *);
38static void ifc2kaddr(struct interface *, struct connected *,
39 struct kaddr *);
bad6b0e7 40static int ldp_zebra_send_mpls_labels(int, struct kroute *);
121f9dee 41static int ldp_router_id_update(ZAPI_CALLBACK_ARGS);
121f9dee
QY
42static int ldp_interface_address_add(ZAPI_CALLBACK_ARGS);
43static int ldp_interface_address_delete(ZAPI_CALLBACK_ARGS);
44static int ldp_zebra_read_route(ZAPI_CALLBACK_ARGS);
45static int ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS);
eac6e3f0
RW
46static void ldp_zebra_connected(struct zclient *);
47
48static struct zclient *zclient;
49
50static void
51ifp2kif(struct interface *ifp, struct kif *kif)
52{
53 memset(kif, 0, sizeof(*kif));
54 strlcpy(kif->ifname, ifp->name, sizeof(kif->ifname));
55 kif->ifindex = ifp->ifindex;
988ded8d 56 kif->operative = if_is_operative(ifp);
26519d8c 57 if (ifp->ll_type == ZEBRA_LLT_ETHER)
9bff8057 58 memcpy(kif->mac, ifp->hw_addr, ETH_ALEN);
eac6e3f0
RW
59}
60
61static void
62ifc2kaddr(struct interface *ifp, struct connected *ifc, struct kaddr *ka)
63{
64 memset(ka, 0, sizeof(*ka));
9cf67225 65 strlcpy(ka->ifname, ifp->name, sizeof(ka->ifname));
eac6e3f0
RW
66 ka->ifindex = ifp->ifindex;
67 ka->af = ifc->address->family;
68 ka->prefixlen = ifc->address->prefixlen;
69
70 switch (ka->af) {
71 case AF_INET:
72 ka->addr.v4 = ifc->address->u.prefix4;
73 if (ifc->destination)
74 ka->dstbrd.v4 = ifc->destination->u.prefix4;
75 break;
76 case AF_INET6:
77 ka->addr.v6 = ifc->address->u.prefix6;
78 if (ifc->destination)
79 ka->dstbrd.v6 = ifc->destination->u.prefix6;
80 break;
81 default:
82 break;
83 }
84}
85
87b5f1b7
RW
86void
87pw2zpw(struct l2vpn_pw *pw, struct zapi_pw *zpw)
88{
89 memset(zpw, 0, sizeof(*zpw));
90 strlcpy(zpw->ifname, pw->ifname, sizeof(zpw->ifname));
91 zpw->ifindex = pw->ifindex;
92 zpw->type = pw->l2vpn->pw_type;
93 zpw->af = pw->af;
94 zpw->nexthop.ipv6 = pw->addr.v6;
95 zpw->local_label = NO_LABEL;
96 zpw->remote_label = NO_LABEL;
97 if (pw->flags & F_PW_CWORD)
98 zpw->flags = F_PSEUDOWIRE_CWORD;
99 zpw->data.ldp.lsr_id = pw->lsr_id;
100 zpw->data.ldp.pwid = pw->pwid;
101 strlcpy(zpw->data.ldp.vpn_name, pw->l2vpn->name,
102 sizeof(zpw->data.ldp.vpn_name));
103}
104
ce549947 105static int
bad6b0e7 106ldp_zebra_send_mpls_labels(int cmd, struct kroute *kr)
ce549947 107{
bad6b0e7 108 struct zapi_labels zl = {};
ea6b290b 109 struct zapi_nexthop_label *znh;
ce549947
RW
110
111 if (kr->local_label < MPLS_LABEL_RESERVED_MAX ||
112 kr->remote_label == NO_LABEL)
113 return (0);
114
88d88a9c 115 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
ce549947 116 log_addr(kr->af, &kr->prefix), kr->prefixlen,
88d88a9c
RW
117 log_addr(kr->af, &kr->nexthop), kr->ifindex,
118 log_label(kr->local_label), log_label(kr->remote_label),
ce549947
RW
119 (cmd == ZEBRA_MPLS_LABELS_ADD) ? "add" : "delete");
120
bad6b0e7 121 zl.type = ZEBRA_LSP_LDP;
b3c49d0e
RW
122 zl.local_label = kr->local_label;
123
124 /* Set prefix. */
125 SET_FLAG(zl.message, ZAPI_LABELS_FTN);
126 zl.route.prefix.family = kr->af;
ce549947
RW
127 switch (kr->af) {
128 case AF_INET:
b3c49d0e 129 zl.route.prefix.u.prefix4 = kr->prefix.v4;
ce549947
RW
130 break;
131 case AF_INET6:
b3c49d0e 132 zl.route.prefix.u.prefix6 = kr->prefix.v6;
ce549947
RW
133 break;
134 default:
b3c49d0e 135 fatalx("ldp_zebra_send_mpls_labels: unknown af");
ce549947 136 }
b3c49d0e
RW
137 zl.route.prefix.prefixlen = kr->prefixlen;
138 zl.route.type = kr->route_type;
139 zl.route.instance = kr->route_instance;
140
141 /* Set nexthop. */
ea6b290b
RW
142 zl.nexthop_num = 1;
143 znh = &zl.nexthops[0];
b3c49d0e
RW
144 switch (kr->af) {
145 case AF_INET:
ea6b290b
RW
146 znh->family = AF_INET;
147 znh->address.ipv4 = kr->nexthop.v4;
b3c49d0e 148 if (kr->ifindex)
ea6b290b 149 znh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
b3c49d0e 150 else
ea6b290b 151 znh->type = NEXTHOP_TYPE_IPV4;
b3c49d0e
RW
152 break;
153 case AF_INET6:
ea6b290b
RW
154 znh->family = AF_INET6;
155 znh->address.ipv6 = kr->nexthop.v6;
b3c49d0e 156 if (kr->ifindex)
ea6b290b 157 znh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
b3c49d0e 158 else
ea6b290b 159 znh->type = NEXTHOP_TYPE_IPV6;
b3c49d0e
RW
160 break;
161 default:
162 break;
163 }
ea6b290b
RW
164 znh->ifindex = kr->ifindex;
165 znh->label = kr->remote_label;
ce549947 166
bad6b0e7 167 return zebra_send_mpls_labels(zclient, cmd, &zl);
ce549947
RW
168}
169
eac6e3f0
RW
170int
171kr_change(struct kroute *kr)
172{
bad6b0e7 173 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD, kr));
eac6e3f0
RW
174}
175
176int
177kr_delete(struct kroute *kr)
178{
bad6b0e7 179 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE, kr));
eac6e3f0
RW
180}
181
182int
87b5f1b7 183kmpw_add(struct zapi_pw *zpw)
eac6e3f0 184{
87b5f1b7
RW
185 debug_zebra_out("pseudowire %s nexthop %s (add)",
186 zpw->ifname, log_addr(zpw->af, (union ldpd_addr *)&zpw->nexthop));
187
188 return (zebra_send_pw(zclient, ZEBRA_PW_ADD, zpw));
eac6e3f0
RW
189}
190
191int
87b5f1b7 192kmpw_del(struct zapi_pw *zpw)
eac6e3f0 193{
87b5f1b7
RW
194 debug_zebra_out("pseudowire %s nexthop %s (del)",
195 zpw->ifname, log_addr(zpw->af, (union ldpd_addr *)&zpw->nexthop));
196
197 return (zebra_send_pw(zclient, ZEBRA_PW_DELETE, zpw));
198}
199
200int
201kmpw_set(struct zapi_pw *zpw)
202{
203 debug_zebra_out("pseudowire %s nexthop %s labels %u/%u (set)",
204 zpw->ifname, log_addr(zpw->af, (union ldpd_addr *)&zpw->nexthop),
205 zpw->local_label, zpw->remote_label);
206
207 return (zebra_send_pw(zclient, ZEBRA_PW_SET, zpw));
208}
209
210int
211kmpw_unset(struct zapi_pw *zpw)
212{
213 debug_zebra_out("pseudowire %s nexthop %s (unset)",
214 zpw->ifname, log_addr(zpw->af, (union ldpd_addr *)&zpw->nexthop));
215
216 return (zebra_send_pw(zclient, ZEBRA_PW_UNSET, zpw));
eac6e3f0
RW
217}
218
219void
220kif_redistribute(const char *ifname)
221{
f4e14fdb
RW
222 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
223 struct listnode *cnode;
eac6e3f0
RW
224 struct interface *ifp;
225 struct connected *ifc;
226 struct kif kif;
227 struct kaddr ka;
228
451fda4f 229 FOR_ALL_INTERFACES (vrf, ifp) {
eac6e3f0
RW
230 if (ifname && strcmp(ifname, ifp->name) != 0)
231 continue;
232
233 ifp2kif(ifp, &kif);
52b530fc 234 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
eac6e3f0
RW
235
236 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, ifc)) {
237 ifc2kaddr(ifp, ifc, &ka);
238 main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka,
239 sizeof(ka));
240 }
241 }
242}
243
244static int
121f9dee 245ldp_router_id_update(ZAPI_CALLBACK_ARGS)
eac6e3f0
RW
246{
247 struct prefix router_id;
248
249 zebra_router_id_update_read(zclient->ibuf, &router_id);
250
251 if (bad_addr_v4(router_id.u.prefix4))
252 return (0);
253
254 debug_zebra_in("router-id update %s", inet_ntoa(router_id.u.prefix4));
255
256 global.rtr_id.s_addr = router_id.u.prefix4.s_addr;
257 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE, 0, &global.rtr_id,
258 sizeof(global.rtr_id));
259
260 return (0);
261}
262
263static int
ef7bd2a3 264ldp_ifp_create(struct interface *ifp)
eac6e3f0 265{
eac6e3f0
RW
266 struct kif kif;
267
eac6e3f0
RW
268 debug_zebra_in("interface add %s index %d mtu %d", ifp->name,
269 ifp->ifindex, ifp->mtu);
270
271 ifp2kif(ifp, &kif);
52b530fc 272 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
eac6e3f0 273
ef7bd2a3 274 return 0;
eac6e3f0
RW
275}
276
277static int
3c3c3252 278ldp_ifp_destroy(struct interface *ifp)
eac6e3f0 279{
9cf67225 280 struct kif kif;
eac6e3f0 281
eac6e3f0
RW
282 debug_zebra_in("interface delete %s index %d mtu %d", ifp->name,
283 ifp->ifindex, ifp->mtu);
284
9cf67225
RW
285 ifp2kif(ifp, &kif);
286 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
eac6e3f0
RW
287
288 return (0);
289}
290
291static int
ddbf3e60 292ldp_interface_status_change_helper(struct interface *ifp)
eac6e3f0 293{
eac6e3f0
RW
294 struct listnode *node;
295 struct connected *ifc;
296 struct kif kif;
297 struct kaddr ka;
eac6e3f0 298
eac6e3f0
RW
299 debug_zebra_in("interface %s state update", ifp->name);
300
301 ifp2kif(ifp, &kif);
52b530fc 302 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
eac6e3f0 303
988ded8d 304 if (if_is_operative(ifp)) {
eac6e3f0
RW
305 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
306 ifc2kaddr(ifp, ifc, &ka);
307 main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka,
308 sizeof(ka));
309 }
310 } else {
311 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
312 ifc2kaddr(ifp, ifc, &ka);
313 main_imsg_compose_ldpe(IMSG_DELADDR, 0, &ka,
314 sizeof(ka));
315 }
316 }
317
318 return (0);
319}
ddbf3e60 320
b0b69e59
DS
321static int ldp_ifp_up(struct interface *ifp)
322{
323 return ldp_interface_status_change_helper(ifp);
324}
ddbf3e60 325
b0b69e59
DS
326static int ldp_ifp_down(struct interface *ifp)
327{
ddbf3e60
DS
328 return ldp_interface_status_change_helper(ifp);
329}
eac6e3f0
RW
330
331static int
121f9dee 332ldp_interface_address_add(ZAPI_CALLBACK_ARGS)
eac6e3f0
RW
333{
334 struct connected *ifc;
335 struct interface *ifp;
336 struct kaddr ka;
337
121f9dee 338 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
eac6e3f0
RW
339 if (ifc == NULL)
340 return (0);
341
342 ifp = ifc->ifp;
343 ifc2kaddr(ifp, ifc, &ka);
344
345 /* Filter invalid addresses. */
346 if (bad_addr(ka.af, &ka.addr))
347 return (0);
348
9cf67225
RW
349 debug_zebra_in("address add %s/%u interface %s",
350 log_addr(ka.af, &ka.addr), ka.prefixlen, ifp->name);
eac6e3f0
RW
351
352 /* notify ldpe about new address */
353 main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka, sizeof(ka));
354
355 return (0);
356}
357
358static int
121f9dee 359ldp_interface_address_delete(ZAPI_CALLBACK_ARGS)
eac6e3f0
RW
360{
361 struct connected *ifc;
362 struct interface *ifp;
363 struct kaddr ka;
364
121f9dee 365 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
eac6e3f0
RW
366 if (ifc == NULL)
367 return (0);
368
369 ifp = ifc->ifp;
370 ifc2kaddr(ifp, ifc, &ka);
721c0857 371 connected_free(&ifc);
eac6e3f0
RW
372
373 /* Filter invalid addresses. */
374 if (bad_addr(ka.af, &ka.addr))
375 return (0);
376
9cf67225
RW
377 debug_zebra_in("address delete %s/%u interface %s",
378 log_addr(ka.af, &ka.addr), ka.prefixlen, ifp->name);
eac6e3f0
RW
379
380 /* notify ldpe about removed address */
381 main_imsg_compose_ldpe(IMSG_DELADDR, 0, &ka, sizeof(ka));
382
383 return (0);
384}
385
386static int
121f9dee 387ldp_zebra_read_route(ZAPI_CALLBACK_ARGS)
eac6e3f0 388{
74489921
RW
389 struct zapi_route api;
390 struct zapi_nexthop *api_nh;
eac6e3f0 391 struct kroute kr;
74489921
RW
392 int i, add = 0;
393
394 if (zapi_route_decode(zclient->ibuf, &api) < 0)
395 return -1;
396
397 /* we completely ignore srcdest routes for now. */
398 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
399 return (0);
eac6e3f0
RW
400
401 memset(&kr, 0, sizeof(kr));
74489921
RW
402 kr.af = api.prefix.family;
403 switch (kr.af) {
404 case AF_INET:
405 kr.prefix.v4 = api.prefix.u.prefix4;
406 break;
407 case AF_INET6:
408 kr.prefix.v6 = api.prefix.u.prefix6;
409 break;
410 default:
411 break;
412 }
413 kr.prefixlen = api.prefix.prefixlen;
e132dea0
RW
414 kr.route_type = api.type;
415 kr.route_instance = api.instance;
eac6e3f0 416
74489921 417 switch (api.type) {
a695cc7b 418 case ZEBRA_ROUTE_CONNECT:
eac6e3f0 419 kr.flags |= F_CONNECTED;
a695cc7b
RW
420 break;
421 case ZEBRA_ROUTE_BGP:
422 /* LDP should follow the IGP and ignore BGP routes */
423 return (0);
424 default:
425 break;
426 }
427
eac6e3f0
RW
428 if (bad_addr(kr.af, &kr.prefix) ||
429 (kr.af == AF_INET6 && IN6_IS_SCOPE_EMBED(&kr.prefix.v6)))
430 return (0);
431
121f9dee 432 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
1f2ff5b5
RW
433 add = 1;
434
74489921 435 if (api.nexthop_num == 0)
1f2ff5b5
RW
436 debug_zebra_in("route %s %s/%d (%s)", (add) ? "add" : "delete",
437 log_addr(kr.af, &kr.prefix), kr.prefixlen,
74489921 438 zebra_route_string(api.type));
eac6e3f0
RW
439
440 /* loop through all the nexthops */
74489921
RW
441 for (i = 0; i < api.nexthop_num; i++) {
442 api_nh = &api.nexthops[i];
cddef813
RW
443 switch (api_nh->type) {
444 case NEXTHOP_TYPE_IPV4:
445 if (kr.af != AF_INET)
446 continue;
447 kr.nexthop.v4 = api_nh->gate.ipv4;
448 kr.ifindex = 0;
449 break;
450 case NEXTHOP_TYPE_IPV4_IFINDEX:
451 if (kr.af != AF_INET)
452 continue;
74489921 453 kr.nexthop.v4 = api_nh->gate.ipv4;
cddef813 454 kr.ifindex = api_nh->ifindex;
eac6e3f0 455 break;
cddef813
RW
456 case NEXTHOP_TYPE_IPV6:
457 if (kr.af != AF_INET6)
458 continue;
74489921 459 kr.nexthop.v6 = api_nh->gate.ipv6;
cddef813 460 kr.ifindex = 0;
eac6e3f0 461 break;
cddef813
RW
462 case NEXTHOP_TYPE_IPV6_IFINDEX:
463 if (kr.af != AF_INET6)
464 continue;
465 kr.nexthop.v6 = api_nh->gate.ipv6;
466 kr.ifindex = api_nh->ifindex;
467 break;
468 case NEXTHOP_TYPE_IFINDEX:
469 if (!(kr.flags & F_CONNECTED))
470 continue;
eac6e3f0 471 break;
cddef813
RW
472 default:
473 continue;
eac6e3f0 474 }
eac6e3f0 475
1f2ff5b5
RW
476 debug_zebra_in("route %s %s/%d nexthop %s ifindex %u (%s)",
477 (add) ? "add" : "delete", log_addr(kr.af, &kr.prefix),
478 kr.prefixlen, log_addr(kr.af, &kr.nexthop), kr.ifindex,
74489921 479 zebra_route_string(api.type));
1f2ff5b5
RW
480
481 if (add)
eac6e3f0
RW
482 main_imsg_compose_lde(IMSG_NETWORK_ADD, 0, &kr,
483 sizeof(kr));
eac6e3f0
RW
484 }
485
8cb1fc45 486 main_imsg_compose_lde(IMSG_NETWORK_UPDATE, 0, &kr, sizeof(kr));
134970a2 487
eac6e3f0
RW
488 return (0);
489}
490
87b5f1b7
RW
491/*
492 * Receive PW status update from Zebra and send it to LDE process.
493 */
494static int
121f9dee 495ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS)
87b5f1b7
RW
496{
497 struct zapi_pw_status zpw;
498
121f9dee 499 zebra_read_pw_status_update(cmd, zclient, length, vrf_id, &zpw);
87b5f1b7
RW
500
501 debug_zebra_in("pseudowire %s status %s", zpw.ifname,
502 (zpw.status == PW_STATUS_UP) ? "up" : "down");
503
504 main_imsg_compose_lde(IMSG_PW_UPDATE, 0, &zpw, sizeof(zpw));
505
506 return (0);
507}
508
eac6e3f0
RW
509static void
510ldp_zebra_connected(struct zclient *zclient)
511{
eac6e3f0 512 zclient_send_reg_requests(zclient, VRF_DEFAULT);
a695cc7b
RW
513 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
514 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
515 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6,
516 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
eac6e3f0
RW
517}
518
342213ea
DS
519extern struct zebra_privs_t ldpd_privs;
520
eac6e3f0
RW
521void
522ldp_zebra_init(struct thread_master *master)
523{
138c5a74
DS
524 if_zapi_callbacks(ldp_ifp_create, ldp_ifp_up,
525 ldp_ifp_down, ldp_ifp_destroy);
526
eac6e3f0 527 /* Set default values. */
26f63a1e 528 zclient = zclient_new(master, &zclient_options_default);
342213ea 529 zclient_init(zclient, ZEBRA_ROUTE_LDP, 0, &ldpd_privs);
eac6e3f0
RW
530
531 /* set callbacks */
532 zclient->zebra_connected = ldp_zebra_connected;
533 zclient->router_id_update = ldp_router_id_update;
eac6e3f0
RW
534 zclient->interface_address_add = ldp_interface_address_add;
535 zclient->interface_address_delete = ldp_interface_address_delete;
74489921
RW
536 zclient->redistribute_route_add = ldp_zebra_read_route;
537 zclient->redistribute_route_del = ldp_zebra_read_route;
87b5f1b7 538 zclient->pw_status_update = ldp_zebra_read_pw_status_update;
eac6e3f0 539}
64dffe25
RW
540
541void
542ldp_zebra_destroy(void)
543{
544 zclient_stop(zclient);
545 zclient_free(zclient);
546 zclient = NULL;
547}