]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/ldp_zebra.c
Merge pull request #5288 from SumitAgarwal123/bfd_docs
[mirror_frr.git] / ldpd / ldp_zebra.c
1 /*
2 * Copyright (C) 2016 by Open Source Routing.
3 *
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.
8 *
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.
13 *
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
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"
29 #include "mpls.h"
30
31 #include "ldpd.h"
32 #include "ldpe.h"
33 #include "lde.h"
34 #include "log.h"
35 #include "ldp_debug.h"
36
37 static void ifp2kif(struct interface *, struct kif *);
38 static void ifc2kaddr(struct interface *, struct connected *,
39 struct kaddr *);
40 static int ldp_zebra_send_mpls_labels(int, struct kroute *);
41 static int ldp_router_id_update(ZAPI_CALLBACK_ARGS);
42 static int ldp_interface_address_add(ZAPI_CALLBACK_ARGS);
43 static int ldp_interface_address_delete(ZAPI_CALLBACK_ARGS);
44 static int ldp_zebra_read_route(ZAPI_CALLBACK_ARGS);
45 static int ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS);
46 static void ldp_zebra_connected(struct zclient *);
47
48 static struct zclient *zclient;
49
50 static void
51 ifp2kif(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;
56 kif->operative = if_is_operative(ifp);
57 if (ifp->ll_type == ZEBRA_LLT_ETHER)
58 memcpy(kif->mac, ifp->hw_addr, ETH_ALEN);
59 }
60
61 static void
62 ifc2kaddr(struct interface *ifp, struct connected *ifc, struct kaddr *ka)
63 {
64 memset(ka, 0, sizeof(*ka));
65 strlcpy(ka->ifname, ifp->name, sizeof(ka->ifname));
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
86 void
87 pw2zpw(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
105 static int
106 ldp_zebra_send_mpls_labels(int cmd, struct kroute *kr)
107 {
108 struct zapi_labels zl = {};
109 struct zapi_nexthop_label *znh;
110
111 if (kr->local_label < MPLS_LABEL_RESERVED_MAX ||
112 kr->remote_label == NO_LABEL)
113 return (0);
114
115 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
116 log_addr(kr->af, &kr->prefix), kr->prefixlen,
117 log_addr(kr->af, &kr->nexthop), kr->ifindex,
118 log_label(kr->local_label), log_label(kr->remote_label),
119 (cmd == ZEBRA_MPLS_LABELS_ADD) ? "add" : "delete");
120
121 zl.type = ZEBRA_LSP_LDP;
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;
127 switch (kr->af) {
128 case AF_INET:
129 zl.route.prefix.u.prefix4 = kr->prefix.v4;
130 break;
131 case AF_INET6:
132 zl.route.prefix.u.prefix6 = kr->prefix.v6;
133 break;
134 default:
135 fatalx("ldp_zebra_send_mpls_labels: unknown af");
136 }
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. */
142 zl.nexthop_num = 1;
143 znh = &zl.nexthops[0];
144 switch (kr->af) {
145 case AF_INET:
146 znh->family = AF_INET;
147 znh->address.ipv4 = kr->nexthop.v4;
148 if (kr->ifindex)
149 znh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
150 else
151 znh->type = NEXTHOP_TYPE_IPV4;
152 break;
153 case AF_INET6:
154 znh->family = AF_INET6;
155 znh->address.ipv6 = kr->nexthop.v6;
156 if (kr->ifindex)
157 znh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
158 else
159 znh->type = NEXTHOP_TYPE_IPV6;
160 break;
161 default:
162 break;
163 }
164 znh->ifindex = kr->ifindex;
165 znh->label = kr->remote_label;
166
167 return zebra_send_mpls_labels(zclient, cmd, &zl);
168 }
169
170 int
171 kr_change(struct kroute *kr)
172 {
173 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD, kr));
174 }
175
176 int
177 kr_delete(struct kroute *kr)
178 {
179 return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE, kr));
180 }
181
182 int
183 kmpw_add(struct zapi_pw *zpw)
184 {
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));
189 }
190
191 int
192 kmpw_del(struct zapi_pw *zpw)
193 {
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
200 int
201 kmpw_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
210 int
211 kmpw_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));
217 }
218
219 void
220 kif_redistribute(const char *ifname)
221 {
222 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
223 struct listnode *cnode;
224 struct interface *ifp;
225 struct connected *ifc;
226 struct kif kif;
227 struct kaddr ka;
228
229 FOR_ALL_INTERFACES (vrf, ifp) {
230 if (ifname && strcmp(ifname, ifp->name) != 0)
231 continue;
232
233 ifp2kif(ifp, &kif);
234 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
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
244 static int
245 ldp_router_id_update(ZAPI_CALLBACK_ARGS)
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
263 static int
264 ldp_ifp_create(struct interface *ifp)
265 {
266 struct kif kif;
267
268 debug_zebra_in("interface add %s index %d mtu %d", ifp->name,
269 ifp->ifindex, ifp->mtu);
270
271 ifp2kif(ifp, &kif);
272 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
273
274 return 0;
275 }
276
277 static int
278 ldp_ifp_destroy(struct interface *ifp)
279 {
280 struct kif kif;
281
282 debug_zebra_in("interface delete %s index %d mtu %d", ifp->name,
283 ifp->ifindex, ifp->mtu);
284
285 ifp2kif(ifp, &kif);
286 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
287
288 return (0);
289 }
290
291 static int
292 ldp_interface_status_change_helper(struct interface *ifp)
293 {
294 struct listnode *node;
295 struct connected *ifc;
296 struct kif kif;
297 struct kaddr ka;
298
299 debug_zebra_in("interface %s state update", ifp->name);
300
301 ifp2kif(ifp, &kif);
302 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
303
304 if (if_is_operative(ifp)) {
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 }
320
321 static int ldp_ifp_up(struct interface *ifp)
322 {
323 return ldp_interface_status_change_helper(ifp);
324 }
325
326 static int ldp_ifp_down(struct interface *ifp)
327 {
328 return ldp_interface_status_change_helper(ifp);
329 }
330
331 static int
332 ldp_interface_address_add(ZAPI_CALLBACK_ARGS)
333 {
334 struct connected *ifc;
335 struct interface *ifp;
336 struct kaddr ka;
337
338 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
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
349 debug_zebra_in("address add %s/%u interface %s",
350 log_addr(ka.af, &ka.addr), ka.prefixlen, ifp->name);
351
352 /* notify ldpe about new address */
353 main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka, sizeof(ka));
354
355 return (0);
356 }
357
358 static int
359 ldp_interface_address_delete(ZAPI_CALLBACK_ARGS)
360 {
361 struct connected *ifc;
362 struct interface *ifp;
363 struct kaddr ka;
364
365 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
366 if (ifc == NULL)
367 return (0);
368
369 ifp = ifc->ifp;
370 ifc2kaddr(ifp, ifc, &ka);
371 connected_free(&ifc);
372
373 /* Filter invalid addresses. */
374 if (bad_addr(ka.af, &ka.addr))
375 return (0);
376
377 debug_zebra_in("address delete %s/%u interface %s",
378 log_addr(ka.af, &ka.addr), ka.prefixlen, ifp->name);
379
380 /* notify ldpe about removed address */
381 main_imsg_compose_ldpe(IMSG_DELADDR, 0, &ka, sizeof(ka));
382
383 return (0);
384 }
385
386 static int
387 ldp_zebra_read_route(ZAPI_CALLBACK_ARGS)
388 {
389 struct zapi_route api;
390 struct zapi_nexthop *api_nh;
391 struct kroute kr;
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);
400
401 memset(&kr, 0, sizeof(kr));
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;
414 kr.route_type = api.type;
415 kr.route_instance = api.instance;
416
417 switch (api.type) {
418 case ZEBRA_ROUTE_CONNECT:
419 kr.flags |= F_CONNECTED;
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
428 if (bad_addr(kr.af, &kr.prefix) ||
429 (kr.af == AF_INET6 && IN6_IS_SCOPE_EMBED(&kr.prefix.v6)))
430 return (0);
431
432 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
433 add = 1;
434
435 if (api.nexthop_num == 0)
436 debug_zebra_in("route %s %s/%d (%s)", (add) ? "add" : "delete",
437 log_addr(kr.af, &kr.prefix), kr.prefixlen,
438 zebra_route_string(api.type));
439
440 /* loop through all the nexthops */
441 for (i = 0; i < api.nexthop_num; i++) {
442 api_nh = &api.nexthops[i];
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;
453 kr.nexthop.v4 = api_nh->gate.ipv4;
454 kr.ifindex = api_nh->ifindex;
455 break;
456 case NEXTHOP_TYPE_IPV6:
457 if (kr.af != AF_INET6)
458 continue;
459 kr.nexthop.v6 = api_nh->gate.ipv6;
460 kr.ifindex = 0;
461 break;
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;
471 break;
472 default:
473 continue;
474 }
475
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,
479 zebra_route_string(api.type));
480
481 if (add)
482 main_imsg_compose_lde(IMSG_NETWORK_ADD, 0, &kr,
483 sizeof(kr));
484 }
485
486 main_imsg_compose_lde(IMSG_NETWORK_UPDATE, 0, &kr, sizeof(kr));
487
488 return (0);
489 }
490
491 /*
492 * Receive PW status update from Zebra and send it to LDE process.
493 */
494 static int
495 ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS)
496 {
497 struct zapi_pw_status zpw;
498
499 zebra_read_pw_status_update(cmd, zclient, length, vrf_id, &zpw);
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
509 static void
510 ldp_zebra_connected(struct zclient *zclient)
511 {
512 zclient_send_reg_requests(zclient, VRF_DEFAULT);
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);
517 }
518
519 extern struct zebra_privs_t ldpd_privs;
520
521 void
522 ldp_zebra_init(struct thread_master *master)
523 {
524 if_zapi_callbacks(ldp_ifp_create, ldp_ifp_up,
525 ldp_ifp_down, ldp_ifp_destroy);
526
527 /* Set default values. */
528 zclient = zclient_new(master, &zclient_options_default);
529 zclient_init(zclient, ZEBRA_ROUTE_LDP, 0, &ldpd_privs);
530
531 /* set callbacks */
532 zclient->zebra_connected = ldp_zebra_connected;
533 zclient->router_id_update = ldp_router_id_update;
534 zclient->interface_address_add = ldp_interface_address_add;
535 zclient->interface_address_delete = ldp_interface_address_delete;
536 zclient->redistribute_route_add = ldp_zebra_read_route;
537 zclient->redistribute_route_del = ldp_zebra_read_route;
538 zclient->pw_status_update = ldp_zebra_read_pw_status_update;
539 }
540
541 void
542 ldp_zebra_destroy(void)
543 {
544 zclient_stop(zclient);
545 zclient_free(zclient);
546 zclient = NULL;
547 }