]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/ldp_zebra.c
Merge branch stable/2.0 into stable/3.0
[mirror_frr.git] / ldpd / ldp_zebra.c
1 /*
2 * Copyright (C) 2016 by Open Source Routing.
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
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
19 * 02111-1307, USA.
20 */
21
22 #include <zebra.h>
23
24 #include "prefix.h"
25 #include "stream.h"
26 #include "memory.h"
27 #include "zclient.h"
28 #include "command.h"
29 #include "network.h"
30 #include "linklist.h"
31 #include "mpls.h"
32
33 #include "ldpd.h"
34 #include "ldpe.h"
35 #include "lde.h"
36 #include "log.h"
37 #include "ldp_debug.h"
38
39 static void ifp2kif(struct interface *, struct kif *);
40 static void ifc2kaddr(struct interface *, struct connected *,
41 struct kaddr *);
42 static int zebra_send_mpls_labels(int, struct kroute *);
43 static int ldp_router_id_update(int, struct zclient *, zebra_size_t,
44 vrf_id_t);
45 static int ldp_interface_add(int, struct zclient *, zebra_size_t,
46 vrf_id_t);
47 static int ldp_interface_delete(int, struct zclient *, zebra_size_t,
48 vrf_id_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,
52 vrf_id_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,
56 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, ETHER_ADDR_LEN);
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 static int
98 zebra_send_mpls_labels(int cmd, struct kroute *kr)
99 {
100 struct stream *s;
101
102 if (kr->local_label < MPLS_LABEL_RESERVED_MAX ||
103 kr->remote_label == NO_LABEL)
104 return (0);
105
106 debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
107 log_addr(kr->af, &kr->prefix), kr->prefixlen,
108 log_addr(kr->af, &kr->nexthop), kr->ifindex,
109 log_label(kr->local_label), log_label(kr->remote_label),
110 (cmd == ZEBRA_MPLS_LABELS_ADD) ? "add" : "delete");
111
112 /* Reset stream. */
113 s = zclient->obuf;
114 stream_reset(s);
115
116 zclient_create_header(s, cmd, VRF_DEFAULT);
117 stream_putc(s, ZEBRA_LSP_LDP);
118 stream_putl(s, kr->af);
119 switch (kr->af) {
120 case AF_INET:
121 stream_put_in_addr(s, &kr->prefix.v4);
122 stream_putc(s, kr->prefixlen);
123 stream_put_in_addr(s, &kr->nexthop.v4);
124 break;
125 case AF_INET6:
126 stream_write(s, (u_char *)&kr->prefix.v6, 16);
127 stream_putc(s, kr->prefixlen);
128 stream_write(s, (u_char *)&kr->nexthop.v6, 16);
129 break;
130 default:
131 fatalx("kr_change: unknown af");
132 }
133 stream_putl(s, kr->ifindex);
134 stream_putc(s, kr->priority);
135 stream_putl(s, kr->local_label);
136 stream_putl(s, kr->remote_label);
137
138 /* Put length at the first point of the stream. */
139 stream_putw_at(s, 0, stream_get_endp(s));
140
141 return (zclient_send_message(zclient));
142 }
143
144 int
145 kr_change(struct kroute *kr)
146 {
147 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD, kr));
148 }
149
150 int
151 kr_delete(struct kroute *kr)
152 {
153 return (zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE, kr));
154 }
155
156 int
157 kmpw_set(struct kpw *kpw)
158 {
159 /* TODO */
160 return (0);
161 }
162
163 int
164 kmpw_unset(struct kpw *kpw)
165 {
166 /* TODO */
167 return (0);
168 }
169
170 void
171 kif_redistribute(const char *ifname)
172 {
173 struct listnode *node, *cnode;
174 struct interface *ifp;
175 struct connected *ifc;
176 struct kif kif;
177 struct kaddr ka;
178
179 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT), node, ifp)) {
180 if (ifname && strcmp(ifname, ifp->name) != 0)
181 continue;
182
183 ifp2kif(ifp, &kif);
184 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
185
186 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, ifc)) {
187 ifc2kaddr(ifp, ifc, &ka);
188 main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka,
189 sizeof(ka));
190 }
191 }
192 }
193
194 static int
195 ldp_router_id_update(int command, struct zclient *zclient, zebra_size_t length,
196 vrf_id_t vrf_id)
197 {
198 struct prefix router_id;
199
200 zebra_router_id_update_read(zclient->ibuf, &router_id);
201
202 if (bad_addr_v4(router_id.u.prefix4))
203 return (0);
204
205 debug_zebra_in("router-id update %s", inet_ntoa(router_id.u.prefix4));
206
207 global.rtr_id.s_addr = router_id.u.prefix4.s_addr;
208 main_imsg_compose_ldpe(IMSG_RTRID_UPDATE, 0, &global.rtr_id,
209 sizeof(global.rtr_id));
210
211 return (0);
212 }
213
214 static int
215 ldp_interface_add(int command, struct zclient *zclient, zebra_size_t length,
216 vrf_id_t vrf_id)
217 {
218 struct interface *ifp;
219 struct kif kif;
220
221 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
222 debug_zebra_in("interface add %s index %d mtu %d", ifp->name,
223 ifp->ifindex, ifp->mtu);
224
225 ifp2kif(ifp, &kif);
226 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
227
228 return (0);
229 }
230
231 static int
232 ldp_interface_delete(int command, struct zclient *zclient, zebra_size_t length,
233 vrf_id_t vrf_id)
234 {
235 struct interface *ifp;
236 struct kif kif;
237
238 /* zebra_interface_state_read() updates interface structure in iflist */
239 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
240 if (ifp == NULL)
241 return (0);
242
243 debug_zebra_in("interface delete %s index %d mtu %d", ifp->name,
244 ifp->ifindex, ifp->mtu);
245
246 /* To support pseudo interface do not free interface structure. */
247 /* if_delete(ifp); */
248 ifp->ifindex = IFINDEX_DELETED;
249
250 ifp2kif(ifp, &kif);
251 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
252
253 return (0);
254 }
255
256 static int
257 ldp_interface_status_change(int command, struct zclient *zclient,
258 zebra_size_t length, vrf_id_t vrf_id)
259 {
260 struct interface *ifp;
261 struct listnode *node;
262 struct connected *ifc;
263 struct kif kif;
264 struct kaddr ka;
265
266 /*
267 * zebra_interface_state_read() updates interface structure in
268 * iflist.
269 */
270 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
271 if (ifp == NULL)
272 return (0);
273
274 debug_zebra_in("interface %s state update", ifp->name);
275
276 ifp2kif(ifp, &kif);
277 main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));
278
279 if (if_is_operative(ifp)) {
280 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
281 ifc2kaddr(ifp, ifc, &ka);
282 main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka,
283 sizeof(ka));
284 }
285 } else {
286 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
287 ifc2kaddr(ifp, ifc, &ka);
288 main_imsg_compose_ldpe(IMSG_DELADDR, 0, &ka,
289 sizeof(ka));
290 }
291 }
292
293 return (0);
294 }
295
296 static int
297 ldp_interface_address_add(int command, struct zclient *zclient,
298 zebra_size_t length, vrf_id_t vrf_id)
299 {
300 struct connected *ifc;
301 struct interface *ifp;
302 struct kaddr ka;
303
304 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
305 if (ifc == NULL)
306 return (0);
307
308 ifp = ifc->ifp;
309 ifc2kaddr(ifp, ifc, &ka);
310
311 /* Filter invalid addresses. */
312 if (bad_addr(ka.af, &ka.addr))
313 return (0);
314
315 debug_zebra_in("address add %s/%u interface %s",
316 log_addr(ka.af, &ka.addr), ka.prefixlen, ifp->name);
317
318 /* notify ldpe about new address */
319 main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka, sizeof(ka));
320
321 return (0);
322 }
323
324 static int
325 ldp_interface_address_delete(int command, struct zclient *zclient,
326 zebra_size_t length, vrf_id_t vrf_id)
327 {
328 struct connected *ifc;
329 struct interface *ifp;
330 struct kaddr ka;
331
332 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
333 if (ifc == NULL)
334 return (0);
335
336 ifp = ifc->ifp;
337 ifc2kaddr(ifp, ifc, &ka);
338 connected_free(ifc);
339
340 /* Filter invalid addresses. */
341 if (bad_addr(ka.af, &ka.addr))
342 return (0);
343
344 debug_zebra_in("address delete %s/%u interface %s",
345 log_addr(ka.af, &ka.addr), ka.prefixlen, ifp->name);
346
347 /* notify ldpe about removed address */
348 main_imsg_compose_ldpe(IMSG_DELADDR, 0, &ka, sizeof(ka));
349
350 return (0);
351 }
352
353 static int
354 ldp_zebra_read_route(int command, struct zclient *zclient, zebra_size_t length,
355 vrf_id_t vrf_id)
356 {
357 struct stream *s;
358 u_char type;
359 u_char message_flags;
360 struct kroute kr;
361 int nhnum = 0, nhlen;
362 size_t nhmark;
363
364 memset(&kr, 0, sizeof(kr));
365 s = zclient->ibuf;
366
367 type = stream_getc(s);
368 switch (type) {
369 case ZEBRA_ROUTE_CONNECT:
370 kr.flags |= F_CONNECTED;
371 break;
372 case ZEBRA_ROUTE_BGP:
373 /* LDP should follow the IGP and ignore BGP routes */
374 return (0);
375 default:
376 break;
377 }
378
379 stream_getl(s); /* flags, unused */
380 stream_getw(s); /* instance, unused */
381 message_flags = stream_getc(s);
382
383 switch (command) {
384 case ZEBRA_REDISTRIBUTE_IPV4_ADD:
385 case ZEBRA_REDISTRIBUTE_IPV4_DEL:
386 kr.af = AF_INET;
387 nhlen = sizeof(struct in_addr);
388 break;
389 case ZEBRA_REDISTRIBUTE_IPV6_ADD:
390 case ZEBRA_REDISTRIBUTE_IPV6_DEL:
391 kr.af = AF_INET6;
392 nhlen = sizeof(struct in6_addr);
393 break;
394 default:
395 fatalx("ldp_zebra_read_route: unknown command");
396 }
397 kr.prefixlen = stream_getc(s);
398 stream_get(&kr.prefix, s, PSIZE(kr.prefixlen));
399
400 if (bad_addr(kr.af, &kr.prefix) ||
401 (kr.af == AF_INET6 && IN6_IS_SCOPE_EMBED(&kr.prefix.v6)))
402 return (0);
403
404 if (kr.af == AF_INET6 &&
405 CHECK_FLAG(message_flags, ZAPI_MESSAGE_SRCPFX)) {
406 uint8_t src_prefixlen;
407
408 src_prefixlen = stream_getc(s);
409
410 /* we completely ignore srcdest routes for now. */
411 if (src_prefixlen)
412 return (0);
413 }
414
415 if (CHECK_FLAG(message_flags, ZAPI_MESSAGE_NEXTHOP)) {
416 nhnum = stream_getc(s);
417 nhmark = stream_get_getp(s);
418 stream_set_getp(s, nhmark + nhnum * (nhlen + 5));
419 }
420
421 if (CHECK_FLAG(message_flags, ZAPI_MESSAGE_DISTANCE))
422 kr.priority = stream_getc(s);
423 if (CHECK_FLAG(message_flags, ZAPI_MESSAGE_METRIC))
424 stream_getl(s); /* metric, not used */
425
426 if (CHECK_FLAG(message_flags, ZAPI_MESSAGE_NEXTHOP))
427 stream_set_getp(s, nhmark);
428
429 if (nhnum == 0) {
430 switch (command) {
431 case ZEBRA_REDISTRIBUTE_IPV4_ADD:
432 case ZEBRA_REDISTRIBUTE_IPV6_ADD:
433 return (0);
434 case ZEBRA_REDISTRIBUTE_IPV4_DEL:
435 case ZEBRA_REDISTRIBUTE_IPV6_DEL:
436 debug_zebra_in("route delete %s/%d (%s)",
437 log_addr(kr.af, &kr.prefix), kr.prefixlen,
438 zebra_route_string(type));
439 break;
440 default:
441 fatalx("ldp_zebra_read_route: unknown command");
442 }
443 }
444
445 /* loop through all the nexthops */
446 for (; nhnum > 0; nhnum--) {
447 switch (kr.af) {
448 case AF_INET:
449 kr.nexthop.v4.s_addr = stream_get_ipv4(s);
450 break;
451 case AF_INET6:
452 stream_get(&kr.nexthop.v6, s, sizeof(kr.nexthop.v6));
453 break;
454 default:
455 break;
456 }
457 stream_getc(s); /* ifindex_num, unused. */
458 kr.ifindex = stream_getl(s);
459
460 switch (command) {
461 case ZEBRA_REDISTRIBUTE_IPV4_ADD:
462 case ZEBRA_REDISTRIBUTE_IPV6_ADD:
463 debug_zebra_in("route add %s/%d nexthop %s "
464 "ifindex %u (%s)", log_addr(kr.af, &kr.prefix),
465 kr.prefixlen, log_addr(kr.af, &kr.nexthop),
466 kr.ifindex, zebra_route_string(type));
467 main_imsg_compose_lde(IMSG_NETWORK_ADD, 0, &kr,
468 sizeof(kr));
469 break;
470 default:
471 break;
472 }
473 }
474
475 main_imsg_compose_lde(IMSG_NETWORK_UPDATE, 0, &kr, sizeof(kr));
476
477 return (0);
478 }
479
480 static void
481 ldp_zebra_connected(struct zclient *zclient)
482 {
483 zclient_send_reg_requests(zclient, VRF_DEFAULT);
484 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
485 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
486 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6,
487 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
488 }
489
490 void
491 ldp_zebra_init(struct thread_master *master)
492 {
493 /* Set default values. */
494 zclient = zclient_new(master);
495 zclient_init(zclient, ZEBRA_ROUTE_LDP, 0);
496
497 /* set callbacks */
498 zclient->zebra_connected = ldp_zebra_connected;
499 zclient->router_id_update = ldp_router_id_update;
500 zclient->interface_add = ldp_interface_add;
501 zclient->interface_delete = ldp_interface_delete;
502 zclient->interface_up = ldp_interface_status_change;
503 zclient->interface_down = ldp_interface_status_change;
504 zclient->interface_address_add = ldp_interface_address_add;
505 zclient->interface_address_delete = ldp_interface_address_delete;
506 zclient->redistribute_route_ipv4_add = ldp_zebra_read_route;
507 zclient->redistribute_route_ipv4_del = ldp_zebra_read_route;
508 zclient->redistribute_route_ipv6_add = ldp_zebra_read_route;
509 zclient->redistribute_route_ipv6_del = ldp_zebra_read_route;
510 }
511
512 void
513 ldp_zebra_destroy(void)
514 {
515 zclient_stop(zclient);
516 zclient_free(zclient);
517 zclient = NULL;
518 }