]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_zebra.c
Merge pull request #7261 from Niral-Networks/niral_dev_vrf_ospf6
[mirror_frr.git] / ospf6d / ospf6_zebra.c
1 /*
2 * Copyright (C) 2003 Yasuhiro Ohara
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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "log.h"
24 #include "vty.h"
25 #include "command.h"
26 #include "prefix.h"
27 #include "stream.h"
28 #include "zclient.h"
29 #include "memory.h"
30 #include "lib/bfd.h"
31 #include "lib_errors.h"
32
33 #include "ospf6_proto.h"
34 #include "ospf6_top.h"
35 #include "ospf6_interface.h"
36 #include "ospf6_route.h"
37 #include "ospf6_lsa.h"
38 #include "ospf6_lsdb.h"
39 #include "ospf6_asbr.h"
40 #include "ospf6_zebra.h"
41 #include "ospf6d.h"
42 #include "ospf6_area.h"
43
44 DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_DISTANCE, "OSPF6 distance")
45
46 unsigned char conf_debug_ospf6_zebra = 0;
47
48 /* information about zebra. */
49 struct zclient *zclient = NULL;
50
51 void ospf6_zebra_vrf_register(struct ospf6 *ospf6)
52 {
53 if (!zclient || zclient->sock < 0 || !ospf6)
54 return;
55
56 if (ospf6->vrf_id != VRF_UNKNOWN) {
57 if (IS_OSPF6_DEBUG_ZEBRA(RECV)) {
58 zlog_debug("%s: Register VRF %s id %u", __func__,
59 ospf6_vrf_id_to_name(ospf6->vrf_id),
60 ospf6->vrf_id);
61 }
62 zclient_send_reg_requests(zclient, ospf6->vrf_id);
63 }
64 }
65
66 void ospf6_zebra_vrf_deregister(struct ospf6 *ospf6)
67 {
68 if (!zclient || zclient->sock < 0 || !ospf6)
69 return;
70
71 if (ospf6->vrf_id != VRF_DEFAULT && ospf6->vrf_id != VRF_UNKNOWN) {
72 if (IS_OSPF6_DEBUG_ZEBRA(RECV)) {
73 zlog_debug("%s: De-Register VRF %s id %u to Zebra.",
74 __func__,
75 ospf6_vrf_id_to_name(ospf6->vrf_id),
76 ospf6->vrf_id);
77 }
78 /* Deregister for router-id, interfaces,
79 * redistributed routes. */
80 zclient_send_dereg_requests(zclient, ospf6->vrf_id);
81 }
82 }
83
84 /* Router-id update message from zebra. */
85 static int ospf6_router_id_update_zebra(ZAPI_CALLBACK_ARGS)
86 {
87 struct prefix router_id;
88 struct ospf6 *o;
89
90 zebra_router_id_update_read(zclient->ibuf, &router_id);
91
92 om6->zebra_router_id = router_id.u.prefix4.s_addr;
93 o = ospf6_lookup_by_vrf_id(vrf_id);
94 if (o == NULL)
95 return 0;
96
97 o->router_id_zebra = router_id.u.prefix4;
98 if (IS_OSPF6_DEBUG_ZEBRA(RECV)) {
99 char buf[INET_ADDRSTRLEN];
100
101 zlog_debug("%s: zebra router-id %s update", __func__,
102 inet_ntop(AF_INET, &router_id.u.prefix4, buf,
103 INET_ADDRSTRLEN));
104 }
105
106 ospf6_router_id_update(o);
107
108 return 0;
109 }
110
111 /* redistribute function */
112 void ospf6_zebra_redistribute(int type, vrf_id_t vrf_id)
113 {
114 if (vrf_bitmap_check(zclient->redist[AFI_IP6][type], vrf_id))
115 return;
116 vrf_bitmap_set(zclient->redist[AFI_IP6][type], vrf_id);
117
118 if (zclient->sock > 0)
119 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient,
120 AFI_IP6, type, 0, vrf_id);
121 }
122
123 void ospf6_zebra_no_redistribute(int type, vrf_id_t vrf_id)
124 {
125 if (!vrf_bitmap_check(zclient->redist[AFI_IP6][type], vrf_id))
126 return;
127 vrf_bitmap_unset(zclient->redist[AFI_IP6][type], vrf_id);
128 if (zclient->sock > 0)
129 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
130 AFI_IP6, type, 0, vrf_id);
131 }
132
133 static int ospf6_zebra_if_address_update_add(ZAPI_CALLBACK_ARGS)
134 {
135 struct connected *c;
136
137 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD,
138 zclient->ibuf, vrf_id);
139 if (c == NULL)
140 return 0;
141
142 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
143 zlog_debug("Zebra Interface address add: %s %5s %pFX",
144 c->ifp->name, prefix_family_str(c->address),
145 c->address);
146
147 if (c->address->family == AF_INET6) {
148 ospf6_interface_state_update(c->ifp);
149 ospf6_interface_connected_route_update(c->ifp);
150 }
151 return 0;
152 }
153
154 static int ospf6_zebra_if_address_update_delete(ZAPI_CALLBACK_ARGS)
155 {
156 struct connected *c;
157
158 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE,
159 zclient->ibuf, vrf_id);
160 if (c == NULL)
161 return 0;
162
163 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
164 zlog_debug("Zebra Interface address delete: %s %5s %pFX",
165 c->ifp->name, prefix_family_str(c->address),
166 c->address);
167
168 if (c->address->family == AF_INET6) {
169 ospf6_interface_connected_route_update(c->ifp);
170 ospf6_interface_state_update(c->ifp);
171 }
172
173 connected_free(&c);
174
175 return 0;
176 }
177
178 static int ospf6_zebra_read_route(ZAPI_CALLBACK_ARGS)
179 {
180 struct zapi_route api;
181 unsigned long ifindex;
182 struct in6_addr *nexthop;
183 struct ospf6 *ospf6;
184
185 ospf6 = ospf6_lookup_by_vrf_id(vrf_id);
186
187 if (ospf6 == NULL)
188 return 0;
189
190 if (zapi_route_decode(zclient->ibuf, &api) < 0)
191 return -1;
192
193 /* we completely ignore srcdest routes for now. */
194 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
195 return 0;
196
197 if (IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
198 return 0;
199
200 ifindex = api.nexthops[0].ifindex;
201 nexthop = &api.nexthops[0].gate.ipv6;
202
203 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
204 zlog_debug(
205 "Zebra Receive route %s: %s %pFX nexthop %pI6 ifindex %ld tag %" ROUTE_TAG_PRI,
206 (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD ? "add"
207 : "delete"),
208 zebra_route_string(api.type), &api.prefix, nexthop,
209 ifindex, api.tag);
210
211 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
212 ospf6_asbr_redistribute_add(api.type, ifindex, &api.prefix,
213 api.nexthop_num, nexthop, api.tag,
214 ospf6);
215 else
216 ospf6_asbr_redistribute_remove(api.type, ifindex, &api.prefix,
217 ospf6);
218
219 return 0;
220 }
221
222 DEFUN (show_zebra,
223 show_ospf6_zebra_cmd,
224 "show ipv6 ospf6 zebra",
225 SHOW_STR
226 IPV6_STR
227 OSPF6_STR
228 ZEBRA_STR)
229 {
230 int i;
231 if (zclient == NULL) {
232 vty_out(vty, "Not connected to zebra\n");
233 return CMD_SUCCESS;
234 }
235
236 vty_out(vty, "Zebra Information\n");
237 vty_out(vty, " fail: %d\n", zclient->fail);
238 vty_out(vty, " redistribute default: %d\n",
239 vrf_bitmap_check(zclient->default_information[AFI_IP6],
240 VRF_DEFAULT));
241 vty_out(vty, " redistribute:");
242 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
243 if (vrf_bitmap_check(zclient->redist[AFI_IP6][i], VRF_DEFAULT))
244 vty_out(vty, " %s", zebra_route_string(i));
245 }
246 vty_out(vty, "\n");
247 return CMD_SUCCESS;
248 }
249
250 #define ADD 0
251 #define REM 1
252 static void ospf6_zebra_route_update(int type, struct ospf6_route *request,
253 struct ospf6 *ospf6)
254 {
255 struct zapi_route api;
256 int nhcount;
257 int ret = 0;
258 struct prefix *dest;
259
260 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
261 zlog_debug("Send %s route: %pFX",
262 (type == REM ? "remove" : "add"), &request->prefix);
263
264 if (zclient->sock < 0) {
265 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
266 zlog_debug(" Not connected to Zebra");
267 return;
268 }
269
270 if (request->path.origin.adv_router == ospf6->router_id
271 && (request->path.type == OSPF6_PATH_TYPE_EXTERNAL1
272 || request->path.type == OSPF6_PATH_TYPE_EXTERNAL2)) {
273 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
274 zlog_debug(" Ignore self-originated external route");
275 return;
276 }
277
278 /* If removing is the best path and if there's another path,
279 treat this request as add the secondary path */
280 if (type == REM && ospf6_route_is_best(request) && request->next
281 && ospf6_route_is_same(request, request->next)) {
282 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
283 zlog_debug(
284 " Best-path removal resulted Secondary addition");
285 type = ADD;
286 request = request->next;
287 }
288
289 /* Only the best path will be sent to zebra. */
290 if (!ospf6_route_is_best(request)) {
291 /* this is not preferred best route, ignore */
292 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
293 zlog_debug(" Ignore non-best route");
294 return;
295 }
296
297 nhcount = ospf6_route_num_nexthops(request);
298 if (nhcount == 0) {
299 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
300 zlog_debug(" No nexthop, ignore");
301 return;
302 }
303
304 dest = &request->prefix;
305
306 memset(&api, 0, sizeof(api));
307 api.vrf_id = ospf6->vrf_id;
308 api.type = ZEBRA_ROUTE_OSPF6;
309 api.safi = SAFI_UNICAST;
310 api.prefix = *dest;
311 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
312 api.nexthop_num = MIN(nhcount, MULTIPATH_NUM);
313 ospf6_route_zebra_copy_nexthops(request, api.nexthops, api.nexthop_num);
314 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
315 api.metric = (request->path.metric_type == 2 ? request->path.u.cost_e2
316 : request->path.cost);
317 if (request->path.tag) {
318 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
319 api.tag = request->path.tag;
320 }
321
322 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
323 api.distance = ospf6_distance_apply((struct prefix_ipv6 *)dest, request,
324 ospf6);
325
326 if (type == REM)
327 ret = zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
328 else
329 ret = zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
330
331 if (ret < 0)
332 flog_err(EC_LIB_ZAPI_SOCKET,
333 "zclient_route_send() %s failed: %s",
334 (type == REM ? "delete" : "add"),
335 safe_strerror(errno));
336
337 return;
338 }
339
340 void ospf6_zebra_route_update_add(struct ospf6_route *request,
341 struct ospf6 *ospf6)
342 {
343 ospf6_zebra_route_update(ADD, request, ospf6);
344 }
345
346 void ospf6_zebra_route_update_remove(struct ospf6_route *request,
347 struct ospf6 *ospf6)
348 {
349 ospf6_zebra_route_update(REM, request, ospf6);
350 }
351
352 void ospf6_zebra_add_discard(struct ospf6_route *request, struct ospf6 *ospf6)
353 {
354 struct zapi_route api;
355 struct prefix *dest = &request->prefix;
356
357 if (!CHECK_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED)) {
358 memset(&api, 0, sizeof(api));
359 api.vrf_id = ospf6->vrf_id;
360 api.type = ZEBRA_ROUTE_OSPF6;
361 api.safi = SAFI_UNICAST;
362 api.prefix = *dest;
363 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
364
365 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
366
367 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
368 zlog_debug("Zebra: Route add discard %pFX", dest);
369
370 SET_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED);
371 } else {
372 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
373 zlog_debug(
374 "Zebra: Blackhole route present already %pFX",
375 dest);
376 }
377 }
378
379 void ospf6_zebra_delete_discard(struct ospf6_route *request,
380 struct ospf6 *ospf6)
381 {
382 struct zapi_route api;
383 struct prefix *dest = &request->prefix;
384
385 if (CHECK_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED)) {
386 memset(&api, 0, sizeof(api));
387 api.vrf_id = ospf6->vrf_id;
388 api.type = ZEBRA_ROUTE_OSPF6;
389 api.safi = SAFI_UNICAST;
390 api.prefix = *dest;
391 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
392
393 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
394
395 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
396 zlog_debug("Zebra: Route delete discard %pFX", dest);
397
398 UNSET_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED);
399 } else {
400 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
401 zlog_debug(
402 "Zebra: Blackhole route already deleted %pFX",
403 dest);
404 }
405 }
406
407 static struct ospf6_distance *ospf6_distance_new(void)
408 {
409 return XCALLOC(MTYPE_OSPF6_DISTANCE, sizeof(struct ospf6_distance));
410 }
411
412 static void ospf6_distance_free(struct ospf6_distance *odistance)
413 {
414 XFREE(MTYPE_OSPF6_DISTANCE, odistance);
415 }
416
417 int ospf6_distance_set(struct vty *vty, struct ospf6 *o,
418 const char *distance_str, const char *ip_str,
419 const char *access_list_str)
420 {
421 int ret;
422 struct prefix_ipv6 p;
423 uint8_t distance;
424 struct route_node *rn;
425 struct ospf6_distance *odistance;
426
427 ret = str2prefix_ipv6(ip_str, &p);
428 if (ret == 0) {
429 vty_out(vty, "Malformed prefix\n");
430 return CMD_WARNING_CONFIG_FAILED;
431 }
432
433 distance = atoi(distance_str);
434
435 /* Get OSPF6 distance node. */
436 rn = route_node_get(o->distance_table, (struct prefix *)&p);
437 if (rn->info) {
438 odistance = rn->info;
439 route_unlock_node(rn);
440 } else {
441 odistance = ospf6_distance_new();
442 rn->info = odistance;
443 }
444
445 /* Set distance value. */
446 odistance->distance = distance;
447
448 /* Reset access-list configuration. */
449 if (odistance->access_list) {
450 free(odistance->access_list);
451 odistance->access_list = NULL;
452 }
453 if (access_list_str)
454 odistance->access_list = strdup(access_list_str);
455
456 return CMD_SUCCESS;
457 }
458
459 int ospf6_distance_unset(struct vty *vty, struct ospf6 *o,
460 const char *distance_str, const char *ip_str,
461 const char *access_list_str)
462 {
463 int ret;
464 struct prefix_ipv6 p;
465 struct route_node *rn;
466 struct ospf6_distance *odistance;
467
468 ret = str2prefix_ipv6(ip_str, &p);
469 if (ret == 0) {
470 vty_out(vty, "Malformed prefix\n");
471 return CMD_WARNING_CONFIG_FAILED;
472 }
473
474 rn = route_node_lookup(o->distance_table, (struct prefix *)&p);
475 if (!rn) {
476 vty_out(vty, "Cant't find specified prefix\n");
477 return CMD_WARNING_CONFIG_FAILED;
478 }
479
480 odistance = rn->info;
481
482 if (odistance->access_list)
483 free(odistance->access_list);
484 ospf6_distance_free(odistance);
485
486 rn->info = NULL;
487 route_unlock_node(rn);
488 route_unlock_node(rn);
489
490 return CMD_SUCCESS;
491 }
492
493 void ospf6_distance_reset(struct ospf6 *o)
494 {
495 struct route_node *rn;
496 struct ospf6_distance *odistance;
497
498 for (rn = route_top(o->distance_table); rn; rn = route_next(rn))
499 if ((odistance = rn->info) != NULL) {
500 if (odistance->access_list)
501 free(odistance->access_list);
502 ospf6_distance_free(odistance);
503 rn->info = NULL;
504 route_unlock_node(rn);
505 }
506 }
507
508 uint8_t ospf6_distance_apply(struct prefix_ipv6 *p, struct ospf6_route * or,
509 struct ospf6 *ospf6)
510 {
511 struct ospf6 *o;
512
513 o = ospf6;
514 if (o == NULL)
515 return 0;
516
517 if (o->distance_intra)
518 if (or->path.type == OSPF6_PATH_TYPE_INTRA)
519 return o->distance_intra;
520
521 if (o->distance_inter)
522 if (or->path.type == OSPF6_PATH_TYPE_INTER)
523 return o->distance_inter;
524
525 if (o->distance_external)
526 if (or->path.type == OSPF6_PATH_TYPE_EXTERNAL1 ||
527 or->path.type == OSPF6_PATH_TYPE_EXTERNAL2)
528 return o->distance_external;
529
530 if (o->distance_all)
531 return o->distance_all;
532
533 return 0;
534 }
535
536 static void ospf6_zebra_connected(struct zclient *zclient)
537 {
538 /* Send the client registration */
539 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
540
541 zclient_send_reg_requests(zclient, VRF_DEFAULT);
542 }
543
544 void ospf6_zebra_init(struct thread_master *master)
545 {
546 /* Allocate zebra structure. */
547 zclient = zclient_new(master, &zclient_options_default);
548 zclient_init(zclient, ZEBRA_ROUTE_OSPF6, 0, &ospf6d_privs);
549 zclient->zebra_connected = ospf6_zebra_connected;
550 zclient->router_id_update = ospf6_router_id_update_zebra;
551 zclient->interface_address_add = ospf6_zebra_if_address_update_add;
552 zclient->interface_address_delete =
553 ospf6_zebra_if_address_update_delete;
554 zclient->redistribute_route_add = ospf6_zebra_read_route;
555 zclient->redistribute_route_del = ospf6_zebra_read_route;
556
557 /* Install command element for zebra node. */
558 install_element(VIEW_NODE, &show_ospf6_zebra_cmd);
559 }
560
561 /* Debug */
562
563 DEFUN (debug_ospf6_zebra_sendrecv,
564 debug_ospf6_zebra_sendrecv_cmd,
565 "debug ospf6 zebra [<send|recv>]",
566 DEBUG_STR
567 OSPF6_STR
568 "Debug connection between zebra\n"
569 "Debug Sending zebra\n"
570 "Debug Receiving zebra\n"
571 )
572 {
573 int idx_send_recv = 3;
574 unsigned char level = 0;
575
576 if (argc == 4) {
577 if (strmatch(argv[idx_send_recv]->text, "send"))
578 level = OSPF6_DEBUG_ZEBRA_SEND;
579 else if (strmatch(argv[idx_send_recv]->text, "recv"))
580 level = OSPF6_DEBUG_ZEBRA_RECV;
581 } else
582 level = OSPF6_DEBUG_ZEBRA_SEND | OSPF6_DEBUG_ZEBRA_RECV;
583
584 OSPF6_DEBUG_ZEBRA_ON(level);
585 return CMD_SUCCESS;
586 }
587
588 DEFUN (no_debug_ospf6_zebra_sendrecv,
589 no_debug_ospf6_zebra_sendrecv_cmd,
590 "no debug ospf6 zebra [<send|recv>]",
591 NO_STR
592 DEBUG_STR
593 OSPF6_STR
594 "Debug connection between zebra\n"
595 "Debug Sending zebra\n"
596 "Debug Receiving zebra\n"
597 )
598 {
599 int idx_send_recv = 4;
600 unsigned char level = 0;
601
602 if (argc == 5) {
603 if (strmatch(argv[idx_send_recv]->text, "send"))
604 level = OSPF6_DEBUG_ZEBRA_SEND;
605 else if (strmatch(argv[idx_send_recv]->text, "recv"))
606 level = OSPF6_DEBUG_ZEBRA_RECV;
607 } else
608 level = OSPF6_DEBUG_ZEBRA_SEND | OSPF6_DEBUG_ZEBRA_RECV;
609
610 OSPF6_DEBUG_ZEBRA_OFF(level);
611 return CMD_SUCCESS;
612 }
613
614
615 int config_write_ospf6_debug_zebra(struct vty *vty)
616 {
617 if (IS_OSPF6_DEBUG_ZEBRA(SEND) && IS_OSPF6_DEBUG_ZEBRA(RECV))
618 vty_out(vty, "debug ospf6 zebra\n");
619 else {
620 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
621 vty_out(vty, "debug ospf6 zebra send\n");
622 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
623 vty_out(vty, "debug ospf6 zebra recv\n");
624 }
625 return 0;
626 }
627
628 void install_element_ospf6_debug_zebra(void)
629 {
630 install_element(ENABLE_NODE, &debug_ospf6_zebra_sendrecv_cmd);
631 install_element(ENABLE_NODE, &no_debug_ospf6_zebra_sendrecv_cmd);
632 install_element(CONFIG_NODE, &debug_ospf6_zebra_sendrecv_cmd);
633 install_element(CONFIG_NODE, &no_debug_ospf6_zebra_sendrecv_cmd);
634 }