]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/ldp_zebra.c
Merge pull request #3069 from donaldsharp/bgp_nexthop_address
[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 zebra_send_mpls_labels(int, struct kroute *);
41 static int ldp_router_id_update(int, struct zclient *, zebra_size_t,
42 vrf_id_t);
43 static int ldp_interface_add(int, struct zclient *, zebra_size_t,
44 vrf_id_t);
45 static int ldp_interface_delete(int, struct zclient *, zebra_size_t,
46 vrf_id_t);
47 static int ldp_interface_status_change(int command, struct zclient *,
48 zebra_size_t, vrf_id_t);
49 static int ldp_interface_address_add(int, struct zclient *, zebra_size_t,
50 vrf_id_t);
51 static int ldp_interface_address_delete(int, struct zclient *,
52 zebra_size_t, vrf_id_t);
53 static int ldp_zebra_read_route(int, struct zclient *, zebra_size_t,
54 vrf_id_t);
55 static int ldp_zebra_read_pw_status_update(int, struct zclient *,
56 zebra_size_t, vrf_id_t);
57 static void ldp_zebra_connected(struct zclient *);
58
59 static struct zclient *zclient;
60
61 static void
62 ifp2kif(struct interface *ifp, struct kif *kif)
63 {
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, ETH_ALEN);
70 }
71
72 static void
73 ifc2kaddr(struct interface *ifp, struct connected *ifc, struct kaddr *ka)
74 {
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;
80
81 switch (ka->af) {
82 case AF_INET:
83 ka->addr.v4 = ifc->address->u.prefix4;
84 if (ifc->destination)
85 ka->dstbrd.v4 = ifc->destination->u.prefix4;
86 break;
87 case AF_INET6:
88 ka->addr.v6 = ifc->address->u.prefix6;
89 if (ifc->destination)
90 ka->dstbrd.v6 = ifc->destination->u.prefix6;
91 break;
92 default:
93 break;
94 }
95 }
96
97 void
98 pw2zpw(struct l2vpn_pw *pw, struct zapi_pw *zpw)
99 {
100 memset(zpw, 0, sizeof(*zpw));
101 strlcpy(zpw->ifname, pw->ifname, sizeof(zpw->ifname));
102 zpw->ifindex = pw->ifindex;
103 zpw->type = pw->l2vpn->pw_type;
104 zpw->af = pw->af;
105 zpw->nexthop.ipv6 = pw->addr.v6;
106 zpw->local_label = NO_LABEL;
107 zpw->remote_label = NO_LABEL;
108 if (pw->flags & F_PW_CWORD)
109 zpw->flags = F_PSEUDOWIRE_CWORD;
110 zpw->data.ldp.lsr_id = pw->lsr_id;
111 zpw->data.ldp.pwid = pw->pwid;
112 strlcpy(zpw->data.ldp.vpn_name, pw->l2vpn->name,
113 sizeof(zpw->data.ldp.vpn_name));
114 }
115
116 static int
117 zebra_send_mpls_labels(int cmd, struct kroute *kr)
118 {
119 struct stream *s;
120
121 if (kr->local_label < MPLS_LABEL_RESERVED_MAX ||
122 kr->remote_label == NO_LABEL)
123 return (0);
124
125 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
126 log_addr(kr->af, &kr->prefix), kr->prefixlen,
127 log_addr(kr->af, &kr->nexthop), kr->ifindex,
128 log_label(kr->local_label), log_label(kr->remote_label),
129 (cmd == ZEBRA_MPLS_LABELS_ADD) ? "add" : "delete");
130
131 /* Reset stream. */
132 s = zclient->obuf;
133 stream_reset(s);
134
135 zclient_create_header(s, cmd, VRF_DEFAULT);
136 stream_putc(s, ZEBRA_LSP_LDP);
137 stream_putl(s, kr->af);
138 switch (kr->af) {
139 case AF_INET:
140 stream_put_in_addr(s, &kr->prefix.v4);
141 stream_putc(s, kr->prefixlen);
142 stream_put_in_addr(s, &kr->nexthop.v4);
143 break;
144 case AF_INET6:
145 stream_write(s, (uint8_t *)&kr->prefix.v6, 16);
146 stream_putc(s, kr->prefixlen);
147 stream_write(s, (uint8_t *)&kr->nexthop.v6, 16);
148 break;
149 default:
150 fatalx("kr_change: unknown af");
151 }
152 stream_putl(s, kr->ifindex);
153 stream_putc(s, kr->priority);
154 stream_putl(s, kr->local_label);
155 stream_putl(s, kr->remote_label);
156
157 /* Put length at the first point of the stream. */
158 stream_putw_at(s, 0, stream_get_endp(s));
159
160 return (zclient_send_message(zclient));
161 }
162
163 int
164 kr_change(struct kroute *kr)
165 {
166 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD, kr));
167 }
168
169 int
170 kr_delete(struct kroute *kr)
171 {
172 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE, kr));
173 }
174
175 int
176 kmpw_add(struct zapi_pw *zpw)
177 {
178 debug_zebra_out("pseudowire %s nexthop %s (add)",
179 zpw->ifname, log_addr(zpw->af, (union ldpd_addr *)&zpw->nexthop));
180
181 return (zebra_send_pw(zclient, ZEBRA_PW_ADD, zpw));
182 }
183
184 int
185 kmpw_del(struct zapi_pw *zpw)
186 {
187 debug_zebra_out("pseudowire %s nexthop %s (del)",
188 zpw->ifname, log_addr(zpw->af, (union ldpd_addr *)&zpw->nexthop));
189
190 return (zebra_send_pw(zclient, ZEBRA_PW_DELETE, zpw));
191 }
192
193 int
194 kmpw_set(struct zapi_pw *zpw)
195 {
196 debug_zebra_out("pseudowire %s nexthop %s labels %u/%u (set)",
197 zpw->ifname, log_addr(zpw->af, (union ldpd_addr *)&zpw->nexthop),
198 zpw->local_label, zpw->remote_label);
199
200 return (zebra_send_pw(zclient, ZEBRA_PW_SET, zpw));
201 }
202
203 int
204 kmpw_unset(struct zapi_pw *zpw)
205 {
206 debug_zebra_out("pseudowire %s nexthop %s (unset)",
207 zpw->ifname, log_addr(zpw->af, (union ldpd_addr *)&zpw->nexthop));
208
209 return (zebra_send_pw(zclient, ZEBRA_PW_UNSET, zpw));
210 }
211
212 void
213 kif_redistribute(const char *ifname)
214 {
215 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
216 struct listnode *cnode;
217 struct interface *ifp;
218 struct connected *ifc;
219 struct kif kif;
220 struct kaddr ka;
221
222 FOR_ALL_INTERFACES (vrf, ifp) {
223 if (ifname && strcmp(ifname, ifp->name) != 0)
224 continue;
225
226 ifp2kif(ifp, &kif);
227 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
228
229 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, ifc)) {
230 ifc2kaddr(ifp, ifc, &ka);
231 main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka,
232 sizeof(ka));
233 }
234 }
235 }
236
237 static int
238 ldp_router_id_update(int command, struct zclient *zclient, zebra_size_t length,
239 vrf_id_t vrf_id)
240 {
241 struct prefix router_id;
242
243 zebra_router_id_update_read(zclient->ibuf, &router_id);
244
245 if (bad_addr_v4(router_id.u.prefix4))
246 return (0);
247
248 debug_zebra_in("router-id update %s", inet_ntoa(router_id.u.prefix4));
249
250 global.rtr_id.s_addr = router_id.u.prefix4.s_addr;
251 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE, 0, &global.rtr_id,
252 sizeof(global.rtr_id));
253
254 return (0);
255 }
256
257 static int
258 ldp_interface_add(int command, struct zclient *zclient, zebra_size_t length,
259 vrf_id_t vrf_id)
260 {
261 struct interface *ifp;
262 struct kif kif;
263
264 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
265 debug_zebra_in("interface add %s index %d mtu %d", ifp->name,
266 ifp->ifindex, ifp->mtu);
267
268 ifp2kif(ifp, &kif);
269 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
270
271 return (0);
272 }
273
274 static int
275 ldp_interface_delete(int command, struct zclient *zclient, zebra_size_t length,
276 vrf_id_t vrf_id)
277 {
278 struct interface *ifp;
279 struct kif kif;
280
281 /* zebra_interface_state_read() updates interface structure in iflist */
282 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
283 if (ifp == NULL)
284 return (0);
285
286 debug_zebra_in("interface delete %s index %d mtu %d", ifp->name,
287 ifp->ifindex, ifp->mtu);
288
289 /* To support pseudo interface do not free interface structure. */
290 /* if_delete(ifp); */
291 if_set_index(ifp, IFINDEX_INTERNAL);
292
293 ifp2kif(ifp, &kif);
294 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
295
296 return (0);
297 }
298
299 static int
300 ldp_interface_status_change(int command, struct zclient *zclient,
301 zebra_size_t length, vrf_id_t vrf_id)
302 {
303 struct interface *ifp;
304 struct listnode *node;
305 struct connected *ifc;
306 struct kif kif;
307 struct kaddr ka;
308
309 /*
310 * zebra_interface_state_read() updates interface structure in
311 * iflist.
312 */
313 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
314 if (ifp == NULL)
315 return (0);
316
317 debug_zebra_in("interface %s state update", ifp->name);
318
319 ifp2kif(ifp, &kif);
320 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
321
322 if (if_is_operative(ifp)) {
323 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
324 ifc2kaddr(ifp, ifc, &ka);
325 main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka,
326 sizeof(ka));
327 }
328 } else {
329 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
330 ifc2kaddr(ifp, ifc, &ka);
331 main_imsg_compose_ldpe(IMSG_DELADDR, 0, &ka,
332 sizeof(ka));
333 }
334 }
335
336 return (0);
337 }
338
339 static int
340 ldp_interface_address_add(int command, struct zclient *zclient,
341 zebra_size_t length, vrf_id_t vrf_id)
342 {
343 struct connected *ifc;
344 struct interface *ifp;
345 struct kaddr ka;
346
347 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
348 if (ifc == NULL)
349 return (0);
350
351 ifp = ifc->ifp;
352 ifc2kaddr(ifp, ifc, &ka);
353
354 /* Filter invalid addresses. */
355 if (bad_addr(ka.af, &ka.addr))
356 return (0);
357
358 debug_zebra_in("address add %s/%u interface %s",
359 log_addr(ka.af, &ka.addr), ka.prefixlen, ifp->name);
360
361 /* notify ldpe about new address */
362 main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka, sizeof(ka));
363
364 return (0);
365 }
366
367 static int
368 ldp_interface_address_delete(int command, struct zclient *zclient,
369 zebra_size_t length, vrf_id_t vrf_id)
370 {
371 struct connected *ifc;
372 struct interface *ifp;
373 struct kaddr ka;
374
375 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
376 if (ifc == NULL)
377 return (0);
378
379 ifp = ifc->ifp;
380 ifc2kaddr(ifp, ifc, &ka);
381 connected_free(ifc);
382
383 /* Filter invalid addresses. */
384 if (bad_addr(ka.af, &ka.addr))
385 return (0);
386
387 debug_zebra_in("address delete %s/%u interface %s",
388 log_addr(ka.af, &ka.addr), ka.prefixlen, ifp->name);
389
390 /* notify ldpe about removed address */
391 main_imsg_compose_ldpe(IMSG_DELADDR, 0, &ka, sizeof(ka));
392
393 return (0);
394 }
395
396 static int
397 ldp_zebra_read_route(int command, struct zclient *zclient, zebra_size_t length,
398 vrf_id_t vrf_id)
399 {
400 struct zapi_route api;
401 struct zapi_nexthop *api_nh;
402 struct kroute kr;
403 int i, add = 0;
404
405 if (zapi_route_decode(zclient->ibuf, &api) < 0)
406 return -1;
407
408 /* we completely ignore srcdest routes for now. */
409 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
410 return (0);
411
412 memset(&kr, 0, sizeof(kr));
413 kr.af = api.prefix.family;
414 switch (kr.af) {
415 case AF_INET:
416 kr.prefix.v4 = api.prefix.u.prefix4;
417 break;
418 case AF_INET6:
419 kr.prefix.v6 = api.prefix.u.prefix6;
420 break;
421 default:
422 break;
423 }
424 kr.prefixlen = api.prefix.prefixlen;
425 kr.priority = api.distance;
426
427 switch (api.type) {
428 case ZEBRA_ROUTE_CONNECT:
429 kr.flags |= F_CONNECTED;
430 break;
431 case ZEBRA_ROUTE_BGP:
432 /* LDP should follow the IGP and ignore BGP routes */
433 return (0);
434 default:
435 break;
436 }
437
438 if (bad_addr(kr.af, &kr.prefix) ||
439 (kr.af == AF_INET6 && IN6_IS_SCOPE_EMBED(&kr.prefix.v6)))
440 return (0);
441
442 if (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
443 add = 1;
444
445 if (api.nexthop_num == 0)
446 debug_zebra_in("route %s %s/%d (%s)", (add) ? "add" : "delete",
447 log_addr(kr.af, &kr.prefix), kr.prefixlen,
448 zebra_route_string(api.type));
449
450 /* loop through all the nexthops */
451 for (i = 0; i < api.nexthop_num; i++) {
452 api_nh = &api.nexthops[i];
453 switch (api_nh->type) {
454 case NEXTHOP_TYPE_IPV4:
455 if (kr.af != AF_INET)
456 continue;
457 kr.nexthop.v4 = api_nh->gate.ipv4;
458 kr.ifindex = 0;
459 break;
460 case NEXTHOP_TYPE_IPV4_IFINDEX:
461 if (kr.af != AF_INET)
462 continue;
463 kr.nexthop.v4 = api_nh->gate.ipv4;
464 kr.ifindex = api_nh->ifindex;
465 break;
466 case NEXTHOP_TYPE_IPV6:
467 if (kr.af != AF_INET6)
468 continue;
469 kr.nexthop.v6 = api_nh->gate.ipv6;
470 kr.ifindex = 0;
471 break;
472 case NEXTHOP_TYPE_IPV6_IFINDEX:
473 if (kr.af != AF_INET6)
474 continue;
475 kr.nexthop.v6 = api_nh->gate.ipv6;
476 kr.ifindex = api_nh->ifindex;
477 break;
478 case NEXTHOP_TYPE_IFINDEX:
479 if (!(kr.flags & F_CONNECTED))
480 continue;
481 break;
482 default:
483 continue;
484 }
485
486 debug_zebra_in("route %s %s/%d nexthop %s ifindex %u (%s)",
487 (add) ? "add" : "delete", log_addr(kr.af, &kr.prefix),
488 kr.prefixlen, log_addr(kr.af, &kr.nexthop), kr.ifindex,
489 zebra_route_string(api.type));
490
491 if (add)
492 main_imsg_compose_lde(IMSG_NETWORK_ADD, 0, &kr,
493 sizeof(kr));
494 }
495
496 main_imsg_compose_lde(IMSG_NETWORK_UPDATE, 0, &kr, sizeof(kr));
497
498 return (0);
499 }
500
501 /*
502 * Receive PW status update from Zebra and send it to LDE process.
503 */
504 static int
505 ldp_zebra_read_pw_status_update(int command, struct zclient *zclient,
506 zebra_size_t length, vrf_id_t vrf_id)
507 {
508 struct zapi_pw_status zpw;
509
510 zebra_read_pw_status_update(command, zclient, length, vrf_id, &zpw);
511
512 debug_zebra_in("pseudowire %s status %s", zpw.ifname,
513 (zpw.status == PW_STATUS_UP) ? "up" : "down");
514
515 main_imsg_compose_lde(IMSG_PW_UPDATE, 0, &zpw, sizeof(zpw));
516
517 return (0);
518 }
519
520 static void
521 ldp_zebra_connected(struct zclient *zclient)
522 {
523 zclient_send_reg_requests(zclient, VRF_DEFAULT);
524 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
525 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
526 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6,
527 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
528 }
529
530 extern struct zebra_privs_t ldpd_privs;
531
532 void
533 ldp_zebra_init(struct thread_master *master)
534 {
535 /* Set default values. */
536 zclient = zclient_new_notify(master, &zclient_options_default);
537 zclient_init(zclient, ZEBRA_ROUTE_LDP, 0, &ldpd_privs);
538
539 /* set callbacks */
540 zclient->zebra_connected = ldp_zebra_connected;
541 zclient->router_id_update = ldp_router_id_update;
542 zclient->interface_add = ldp_interface_add;
543 zclient->interface_delete = ldp_interface_delete;
544 zclient->interface_up = ldp_interface_status_change;
545 zclient->interface_down = ldp_interface_status_change;
546 zclient->interface_address_add = ldp_interface_address_add;
547 zclient->interface_address_delete = ldp_interface_address_delete;
548 zclient->redistribute_route_add = ldp_zebra_read_route;
549 zclient->redistribute_route_del = ldp_zebra_read_route;
550 zclient->pw_status_update = ldp_zebra_read_pw_status_update;
551 }
552
553 void
554 ldp_zebra_destroy(void)
555 {
556 zclient_stop(zclient);
557 zclient_free(zclient);
558 zclient = NULL;
559 }