]> git.proxmox.com Git - mirror_frr.git/blame - ospf6d/ospf6_zebra.c
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / ospf6d / ospf6_zebra.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
718e3744 2/*
508e53e2 3 * Copyright (C) 2003 Yasuhiro Ohara
718e3744 4 */
5
508e53e2 6#include <zebra.h>
7
8#include "log.h"
9#include "vty.h"
10#include "command.h"
11#include "prefix.h"
12#include "stream.h"
13#include "zclient.h"
14#include "memory.h"
5b5d66c4 15#include "route_opaque.h"
2376c3f2 16#include "lib/bfd.h"
4ba03be5 17#include "lib_errors.h"
718e3744 18
508e53e2 19#include "ospf6_proto.h"
20#include "ospf6_top.h"
718e3744 21#include "ospf6_interface.h"
508e53e2 22#include "ospf6_route.h"
23#include "ospf6_lsa.h"
049207c3 24#include "ospf6_lsdb.h"
718e3744 25#include "ospf6_asbr.h"
6735622c 26#include "ospf6_nssa.h"
508e53e2 27#include "ospf6_zebra.h"
049207c3 28#include "ospf6d.h"
beadc736 29#include "ospf6_area.h"
71165098 30#include "ospf6_gr.h"
9ebb75c5 31#include "lib/json.h"
718e3744 32
bf8d3d6a 33DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_DISTANCE, "OSPF6 distance");
baff583e 34
508e53e2 35unsigned char conf_debug_ospf6_zebra = 0;
718e3744 36
37/* information about zebra. */
38struct zclient *zclient = NULL;
39
beadc736 40void ospf6_zebra_vrf_register(struct ospf6 *ospf6)
41{
42 if (!zclient || zclient->sock < 0 || !ospf6)
43 return;
44
45 if (ospf6->vrf_id != VRF_UNKNOWN) {
46 if (IS_OSPF6_DEBUG_ZEBRA(RECV)) {
47 zlog_debug("%s: Register VRF %s id %u", __func__,
48 ospf6_vrf_id_to_name(ospf6->vrf_id),
49 ospf6->vrf_id);
50 }
51 zclient_send_reg_requests(zclient, ospf6->vrf_id);
52 }
53}
54
55void ospf6_zebra_vrf_deregister(struct ospf6 *ospf6)
56{
57 if (!zclient || zclient->sock < 0 || !ospf6)
58 return;
59
60 if (ospf6->vrf_id != VRF_DEFAULT && ospf6->vrf_id != VRF_UNKNOWN) {
61 if (IS_OSPF6_DEBUG_ZEBRA(RECV)) {
62 zlog_debug("%s: De-Register VRF %s id %u to Zebra.",
63 __func__,
64 ospf6_vrf_id_to_name(ospf6->vrf_id),
65 ospf6->vrf_id);
66 }
67 /* Deregister for router-id, interfaces,
68 * redistributed routes. */
69 zclient_send_dereg_requests(zclient, ospf6->vrf_id);
70 }
71}
72
18a6dce6 73/* Router-id update message from zebra. */
121f9dee 74static int ospf6_router_id_update_zebra(ZAPI_CALLBACK_ARGS)
18a6dce6 75{
d62a17ae 76 struct prefix router_id;
beadc736 77 struct ospf6 *o;
18a6dce6 78
d62a17ae 79 zebra_router_id_update_read(zclient->ibuf, &router_id);
18a6dce6 80
3c5122c5
IR
81 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
82 zlog_debug("Zebra router-id update %pI4 vrf %s id %u",
83 &router_id.u.prefix4, ospf6_vrf_id_to_name(vrf_id),
84 vrf_id);
85
beadc736 86 o = ospf6_lookup_by_vrf_id(vrf_id);
d62a17ae 87 if (o == NULL)
88 return 0;
c1ba9e8a 89
3c5122c5 90 o->router_id_zebra = router_id.u.prefix4.s_addr;
d6927cf3 91
436a55a1 92 ospf6_router_id_update(o, false);
18a6dce6 93
d62a17ae 94 return 0;
18a6dce6 95}
96
718e3744 97/* redistribute function */
c5d28568 98void ospf6_zebra_redistribute(int type, vrf_id_t vrf_id)
718e3744 99{
c5d28568 100 if (vrf_bitmap_check(zclient->redist[AFI_IP6][type], vrf_id))
d62a17ae 101 return;
c5d28568 102 vrf_bitmap_set(zclient->redist[AFI_IP6][type], vrf_id);
7076bb2f 103
d62a17ae 104 if (zclient->sock > 0)
105 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient,
c5d28568 106 AFI_IP6, type, 0, vrf_id);
718e3744 107}
108
c5d28568 109void ospf6_zebra_no_redistribute(int type, vrf_id_t vrf_id)
718e3744 110{
c5d28568 111 if (!vrf_bitmap_check(zclient->redist[AFI_IP6][type], vrf_id))
d62a17ae 112 return;
c5d28568 113 vrf_bitmap_unset(zclient->redist[AFI_IP6][type], vrf_id);
d62a17ae 114 if (zclient->sock > 0)
115 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
c5d28568 116 AFI_IP6, type, 0, vrf_id);
718e3744 117}
118
6735622c
RW
119void ospf6_zebra_import_default_route(struct ospf6 *ospf6, bool unreg)
120{
121 struct prefix prefix = {};
122 int command;
123
124 if (zclient->sock < 0) {
125 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
126 zlog_debug(" Not connected to Zebra");
127 return;
128 }
129
130 prefix.family = AF_INET6;
131 prefix.prefixlen = 0;
132
133 if (unreg)
ad5c8f67 134 command = ZEBRA_NEXTHOP_UNREGISTER;
6735622c 135 else
ad5c8f67 136 command = ZEBRA_NEXTHOP_REGISTER;
6735622c
RW
137
138 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
139 zlog_debug("%s: sending cmd %s for %pFX (vrf %u)", __func__,
140 zserv_command_string(command), &prefix,
141 ospf6->vrf_id);
142
eb3c9d97
DL
143 if (zclient_send_rnh(zclient, command, &prefix, SAFI_UNICAST, false,
144 true, ospf6->vrf_id)
6735622c
RW
145 == ZCLIENT_SEND_FAILURE)
146 flog_err(EC_LIB_ZAPI_SOCKET, "%s: zclient_send_rnh() failed",
147 __func__);
148}
149
150static int ospf6_zebra_import_check_update(ZAPI_CALLBACK_ARGS)
151{
152 struct ospf6 *ospf6;
153 struct zapi_route nhr;
06e4e901 154 struct prefix matched;
6735622c
RW
155
156 ospf6 = ospf6_lookup_by_vrf_id(vrf_id);
157 if (ospf6 == NULL || !IS_OSPF6_ASBR(ospf6))
158 return 0;
159
06e4e901 160 if (!zapi_nexthop_update_decode(zclient->ibuf, &matched, &nhr)) {
6735622c
RW
161 zlog_err("%s[%u]: Failure to decode route", __func__,
162 ospf6->vrf_id);
163 return -1;
164 }
165
06e4e901
DS
166 if (matched.family != AF_INET6 || matched.prefixlen != 0 ||
167 nhr.type == ZEBRA_ROUTE_OSPF6)
6735622c
RW
168 return 0;
169
170 ospf6->nssa_default_import_check.status = !!nhr.nexthop_num;
171 ospf6_abr_nssa_type_7_defaults(ospf6);
172
173 return 0;
174}
175
121f9dee 176static int ospf6_zebra_if_address_update_add(ZAPI_CALLBACK_ARGS)
718e3744 177{
d62a17ae 178 struct connected *c;
d62a17ae 179
180 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD,
181 zclient->ibuf, vrf_id);
182 if (c == NULL)
183 return 0;
184
185 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
299ce4b3 186 zlog_debug("Zebra Interface address add: %s %5s %pFX",
d62a17ae 187 c->ifp->name, prefix_family_str(c->address),
299ce4b3 188 c->address);
d62a17ae 189
190 if (c->address->family == AF_INET6) {
191 ospf6_interface_state_update(c->ifp);
192 ospf6_interface_connected_route_update(c->ifp);
193 }
194 return 0;
718e3744 195}
196
121f9dee 197static int ospf6_zebra_if_address_update_delete(ZAPI_CALLBACK_ARGS)
718e3744 198{
d62a17ae 199 struct connected *c;
d62a17ae 200
201 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE,
202 zclient->ibuf, vrf_id);
203 if (c == NULL)
204 return 0;
205
206 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
299ce4b3 207 zlog_debug("Zebra Interface address delete: %s %5s %pFX",
d62a17ae 208 c->ifp->name, prefix_family_str(c->address),
299ce4b3 209 c->address);
d62a17ae 210
211 if (c->address->family == AF_INET6) {
212 ospf6_interface_connected_route_update(c->ifp);
213 ospf6_interface_state_update(c->ifp);
214 }
718e3744 215
721c0857 216 connected_free(&c);
718e3744 217
d62a17ae 218 return 0;
219}
718e3744 220
71165098
RW
221static int ospf6_zebra_gr_update(struct ospf6 *ospf6, int command,
222 uint32_t stale_time)
223{
224 struct zapi_cap api;
225
226 if (!zclient || zclient->sock < 0 || !ospf6)
227 return 1;
228
6006b807 229 memset(&api, 0, sizeof(api));
71165098
RW
230 api.cap = command;
231 api.stale_removal_time = stale_time;
232 api.vrf_id = ospf6->vrf_id;
233
234 (void)zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient,
235 &api);
236
237 return 0;
238}
239
240int ospf6_zebra_gr_enable(struct ospf6 *ospf6, uint32_t stale_time)
241{
d8cf2fc5
RW
242 if (IS_DEBUG_OSPF6_GR)
243 zlog_debug("Zebra enable GR [stale time %u]", stale_time);
244
71165098
RW
245 return ospf6_zebra_gr_update(ospf6, ZEBRA_CLIENT_GR_CAPABILITIES,
246 stale_time);
247}
248
249int ospf6_zebra_gr_disable(struct ospf6 *ospf6)
250{
d8cf2fc5
RW
251 if (IS_DEBUG_OSPF6_GR)
252 zlog_debug("Zebra disable GR");
253
71165098
RW
254 return ospf6_zebra_gr_update(ospf6, ZEBRA_CLIENT_GR_DISABLE, 0);
255}
256
121f9dee 257static int ospf6_zebra_read_route(ZAPI_CALLBACK_ARGS)
d62a17ae 258{
74489921 259 struct zapi_route api;
d62a17ae 260 unsigned long ifindex;
6861a5e4 261 const struct in6_addr *nexthop = &in6addr_any;
beadc736 262 struct ospf6 *ospf6;
b19502d3 263 struct prefix_ipv6 p;
beadc736 264
265 ospf6 = ospf6_lookup_by_vrf_id(vrf_id);
d62a17ae 266
267 if (ospf6 == NULL)
268 return 0;
269
74489921
RW
270 if (zapi_route_decode(zclient->ibuf, &api) < 0)
271 return -1;
d62a17ae 272
74489921
RW
273 /* we completely ignore srcdest routes for now. */
274 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
d62a17ae 275 return 0;
276
9fb2b879
DS
277 if (IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
278 return 0;
279
74489921 280 ifindex = api.nexthops[0].ifindex;
6861a5e4
RW
281 if (api.nexthops[0].type == NEXTHOP_TYPE_IPV6
282 || api.nexthops[0].type == NEXTHOP_TYPE_IPV6_IFINDEX)
283 nexthop = &api.nexthops[0].gate.ipv6;
d62a17ae 284
2dbe669b 285 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
d62a17ae 286 zlog_debug(
2dbe669b 287 "Zebra Receive route %s: %s %pFX nexthop %pI6 ifindex %ld tag %" ROUTE_TAG_PRI,
121f9dee
QY
288 (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD ? "add"
289 : "delete"),
2dbe669b 290 zebra_route_string(api.type), &api.prefix, nexthop,
d62a17ae 291 ifindex, api.tag);
718e3744 292
b19502d3 293 memcpy(&p, &api.prefix, sizeof(p));
1fe59b44 294 if (is_default_prefix6(&p))
b19502d3
YR
295 api.type = DEFAULT_ROUTE;
296
121f9dee 297 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
74489921 298 ospf6_asbr_redistribute_add(api.type, ifindex, &api.prefix,
beadc736 299 api.nexthop_num, nexthop, api.tag,
300 ospf6);
d62a17ae 301 else
beadc736 302 ospf6_asbr_redistribute_remove(api.type, ifindex, &api.prefix,
303 ospf6);
718e3744 304
d62a17ae 305 return 0;
718e3744 306}
307
9ebb75c5 308DEFUN(show_zebra,
309 show_ospf6_zebra_cmd,
310 "show ipv6 ospf6 zebra [json]",
311 SHOW_STR
312 IPV6_STR
313 OSPF6_STR
314 ZEBRA_STR
315 JSON_STR)
718e3744 316{
d62a17ae 317 int i;
9ebb75c5 318 bool uj = use_json(argc, argv);
319 json_object *json;
320 json_object *json_zebra;
321 json_object *json_array;
322
d62a17ae 323 if (zclient == NULL) {
324 vty_out(vty, "Not connected to zebra\n");
325 return CMD_SUCCESS;
326 }
327
9ebb75c5 328 if (uj) {
329 json = json_object_new_object();
330 json_zebra = json_object_new_object();
331 json_array = json_object_new_array();
332
333 json_object_int_add(json_zebra, "fail", zclient->fail);
334 json_object_int_add(
335 json_zebra, "redistributeDefault",
336 vrf_bitmap_check(zclient->default_information[AFI_IP6],
337 VRF_DEFAULT));
338 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
339 if (vrf_bitmap_check(zclient->redist[AFI_IP6][i],
340 VRF_DEFAULT))
341 json_object_array_add(
342 json_array,
343 json_object_new_string(
344 zebra_route_string(i)));
345 }
346 json_object_object_add(json_zebra, "redistribute", json_array);
347 json_object_object_add(json, "zebraInformation", json_zebra);
348
8b3f1d41 349 vty_json(vty, json);
9ebb75c5 350 } else {
bbf5104c 351 vty_out(vty, "Zebra Information\n");
9ebb75c5 352 vty_out(vty, " fail: %d\n", zclient->fail);
353 vty_out(vty, " redistribute default: %d\n",
354 vrf_bitmap_check(zclient->default_information[AFI_IP6],
355 VRF_DEFAULT));
356 vty_out(vty, " redistribute:");
357 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
358 if (vrf_bitmap_check(zclient->redist[AFI_IP6][i],
359 VRF_DEFAULT))
360 vty_out(vty, " %s", zebra_route_string(i));
361 }
362 vty_out(vty, "\n");
d62a17ae 363 }
d62a17ae 364 return CMD_SUCCESS;
718e3744 365}
366
5b5d66c4
RW
367static void ospf6_zebra_append_opaque_attr(struct ospf6_route *request,
368 struct zapi_route *api)
369{
370 struct ospf_zebra_opaque ospf_opaque = {};
371
372 /* OSPF path type */
373 snprintf(ospf_opaque.path_type, sizeof(ospf_opaque.path_type), "%s",
374 OSPF6_PATH_TYPE_NAME(request->path.type));
375
376 switch (request->path.type) {
377 case OSPF6_PATH_TYPE_INTRA:
378 case OSPF6_PATH_TYPE_INTER:
379 /* OSPF area ID */
380 (void)inet_ntop(AF_INET, &request->path.area_id,
381 ospf_opaque.area_id,
382 sizeof(ospf_opaque.area_id));
383 break;
384 case OSPF6_PATH_TYPE_EXTERNAL1:
385 case OSPF6_PATH_TYPE_EXTERNAL2:
386 /* OSPF route tag */
387 snprintf(ospf_opaque.tag, sizeof(ospf_opaque.tag), "%u",
388 request->path.tag);
389 break;
390 default:
391 break;
392 }
393
394 SET_FLAG(api->message, ZAPI_MESSAGE_OPAQUE);
395 api->opaque.length = sizeof(struct ospf_zebra_opaque);
396 memcpy(api->opaque.data, &ospf_opaque, api->opaque.length);
397}
398
718e3744 399#define ADD 0
508e53e2 400#define REM 1
beadc736 401static void ospf6_zebra_route_update(int type, struct ospf6_route *request,
402 struct ospf6 *ospf6)
718e3744 403{
5afa1c6b 404 struct zapi_route api;
d62a17ae 405 int nhcount;
d62a17ae 406 int ret = 0;
5afa1c6b 407 struct prefix *dest;
d62a17ae 408
2dbe669b 409 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
f85b7619 410 zlog_debug("Zebra Send %s route: %pFX",
2dbe669b 411 (type == REM ? "remove" : "add"), &request->prefix);
d62a17ae 412
413 if (zclient->sock < 0) {
414 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
415 zlog_debug(" Not connected to Zebra");
416 return;
417 }
418
419 if (request->path.origin.adv_router == ospf6->router_id
420 && (request->path.type == OSPF6_PATH_TYPE_EXTERNAL1
421 || request->path.type == OSPF6_PATH_TYPE_EXTERNAL2)) {
422 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
423 zlog_debug(" Ignore self-originated external route");
424 return;
425 }
426
427 /* If removing is the best path and if there's another path,
0f9e2c91
MS
428 * treat this request as add the secondary path - if there are
429 * nexthops.
430 */
431 if (type == REM && ospf6_route_is_best(request) && request->next &&
432 ospf6_route_is_same(request, request->next) &&
433 ospf6_route_num_nexthops(request->next) > 0) {
d62a17ae 434 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
435 zlog_debug(
84b7a388 436 " Best-path removal resulted Secondary addition");
d62a17ae 437 type = ADD;
438 request = request->next;
439 }
440
441 /* Only the best path will be sent to zebra. */
442 if (!ospf6_route_is_best(request)) {
443 /* this is not preferred best route, ignore */
444 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
445 zlog_debug(" Ignore non-best route");
446 return;
447 }
448
449 nhcount = ospf6_route_num_nexthops(request);
450 if (nhcount == 0) {
0f9e2c91
MS
451 if (type == ADD) {
452 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
453 zlog_debug(" No nexthop, ignore");
454 return;
455 } else if (IS_OSPF6_DEBUG_ZEBRA(SEND))
456 zlog_debug(" No nexthop, rem ok");
d62a17ae 457 }
458
5afa1c6b 459 dest = &request->prefix;
d62a17ae 460
5afa1c6b 461 memset(&api, 0, sizeof(api));
c5d28568 462 api.vrf_id = ospf6->vrf_id;
d62a17ae 463 api.type = ZEBRA_ROUTE_OSPF6;
d62a17ae 464 api.safi = SAFI_UNICAST;
5afa1c6b 465 api.prefix = *dest;
1958143e
MR
466
467 if (nhcount > ospf6->max_multipath) {
468 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
469 zlog_debug(
470 " Nexthop count is greater than configured maximum-path, hence ignore the extra nexthops");
471 }
0f9e2c91 472
1958143e 473 api.nexthop_num = MIN(nhcount, ospf6->max_multipath);
0f9e2c91
MS
474 if (api.nexthop_num > 0) {
475 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
476 ospf6_route_zebra_copy_nexthops(request, api.nexthops,
477 api.nexthop_num, api.vrf_id);
478 }
1958143e 479
d62a17ae 480 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
481 api.metric = (request->path.metric_type == 2 ? request->path.u.cost_e2
482 : request->path.cost);
483 if (request->path.tag) {
484 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
485 api.tag = request->path.tag;
486 }
487
d62a17ae 488 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
beadc736 489 api.distance = ospf6_distance_apply((struct prefix_ipv6 *)dest, request,
490 ospf6);
d62a17ae 491
5b5d66c4
RW
492 if (type == ADD
493 && CHECK_FLAG(ospf6->config_flags, OSPF6_SEND_EXTRA_DATA_TO_ZEBRA))
494 ospf6_zebra_append_opaque_attr(request, &api);
495
d62a17ae 496 if (type == REM)
5afa1c6b 497 ret = zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
d62a17ae 498 else
5afa1c6b 499 ret = zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
d62a17ae 500
8a3f8f2e 501 if (ret == ZCLIENT_SEND_FAILURE)
450971aa 502 flog_err(EC_LIB_ZAPI_SOCKET,
1c50c1c0
QY
503 "zclient_route_send() %s failed: %s",
504 (type == REM ? "delete" : "add"),
505 safe_strerror(errno));
d62a17ae 506
d62a17ae 507 return;
718e3744 508}
509
beadc736 510void ospf6_zebra_route_update_add(struct ospf6_route *request,
511 struct ospf6 *ospf6)
718e3744 512{
71165098
RW
513 if (ospf6->gr_info.restart_in_progress
514 || ospf6->gr_info.prepare_in_progress) {
515 if (IS_DEBUG_OSPF6_GR)
516 zlog_debug(
517 "Zebra: Graceful Restart in progress -- not installing %pFX",
518 &request->prefix);
519 return;
520 }
521
beadc736 522 ospf6_zebra_route_update(ADD, request, ospf6);
718e3744 523}
524
beadc736 525void ospf6_zebra_route_update_remove(struct ospf6_route *request,
526 struct ospf6 *ospf6)
718e3744 527{
71165098
RW
528 if (ospf6->gr_info.restart_in_progress
529 || ospf6->gr_info.prepare_in_progress) {
530 if (IS_DEBUG_OSPF6_GR)
531 zlog_debug(
532 "Zebra: Graceful Restart in progress -- not uninstalling %pFX",
533 &request->prefix);
534 return;
535 }
536
beadc736 537 ospf6_zebra_route_update(REM, request, ospf6);
718e3744 538}
539
beadc736 540void ospf6_zebra_add_discard(struct ospf6_route *request, struct ospf6 *ospf6)
c3c0ac83 541{
5afa1c6b 542 struct zapi_route api;
5afa1c6b 543 struct prefix *dest = &request->prefix;
d62a17ae 544
71165098
RW
545 if (ospf6->gr_info.restart_in_progress
546 || ospf6->gr_info.prepare_in_progress) {
547 if (IS_DEBUG_OSPF6_GR)
548 zlog_debug(
549 "Zebra: Graceful Restart in progress -- not installing %pFX",
550 &request->prefix);
551 return;
552 }
553
d00061ea 554 if (!CHECK_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED)) {
5afa1c6b 555 memset(&api, 0, sizeof(api));
c5d28568 556 api.vrf_id = ospf6->vrf_id;
d00061ea 557 api.type = ZEBRA_ROUTE_OSPF6;
d00061ea 558 api.safi = SAFI_UNICAST;
5afa1c6b 559 api.prefix = *dest;
09a484dd 560 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
d00061ea 561
5afa1c6b 562 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
d00061ea
RW
563
564 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
299ce4b3 565 zlog_debug("Zebra: Route add discard %pFX", dest);
d00061ea
RW
566
567 SET_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED);
568 } else {
d00061ea
RW
569 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
570 zlog_debug(
299ce4b3
DS
571 "Zebra: Blackhole route present already %pFX",
572 dest);
c3c0ac83 573 }
c3c0ac83
DS
574}
575
beadc736 576void ospf6_zebra_delete_discard(struct ospf6_route *request,
577 struct ospf6 *ospf6)
c3c0ac83 578{
5afa1c6b 579 struct zapi_route api;
5afa1c6b 580 struct prefix *dest = &request->prefix;
d62a17ae 581
71165098
RW
582 if (ospf6->gr_info.restart_in_progress
583 || ospf6->gr_info.prepare_in_progress) {
584 if (IS_DEBUG_OSPF6_GR)
585 zlog_debug(
586 "Zebra: Graceful Restart in progress -- not uninstalling %pFX",
587 &request->prefix);
588 return;
589 }
590
d00061ea 591 if (CHECK_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED)) {
5afa1c6b 592 memset(&api, 0, sizeof(api));
c5d28568 593 api.vrf_id = ospf6->vrf_id;
d00061ea 594 api.type = ZEBRA_ROUTE_OSPF6;
d00061ea 595 api.safi = SAFI_UNICAST;
5afa1c6b 596 api.prefix = *dest;
09a484dd 597 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
d00061ea 598
5afa1c6b 599 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
d00061ea
RW
600
601 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
299ce4b3 602 zlog_debug("Zebra: Route delete discard %pFX", dest);
d00061ea
RW
603
604 UNSET_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED);
605 } else {
d00061ea
RW
606 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
607 zlog_debug(
299ce4b3
DS
608 "Zebra: Blackhole route already deleted %pFX",
609 dest);
c3c0ac83 610 }
c3c0ac83
DS
611}
612
d62a17ae 613static struct ospf6_distance *ospf6_distance_new(void)
baff583e 614{
d62a17ae 615 return XCALLOC(MTYPE_OSPF6_DISTANCE, sizeof(struct ospf6_distance));
baff583e
MZ
616}
617
d62a17ae 618static void ospf6_distance_free(struct ospf6_distance *odistance)
baff583e 619{
d62a17ae 620 XFREE(MTYPE_OSPF6_DISTANCE, odistance);
baff583e
MZ
621}
622
d62a17ae 623int ospf6_distance_set(struct vty *vty, struct ospf6 *o,
624 const char *distance_str, const char *ip_str,
625 const char *access_list_str)
baff583e 626{
d62a17ae 627 int ret;
628 struct prefix_ipv6 p;
d7c0a89a 629 uint8_t distance;
d62a17ae 630 struct route_node *rn;
631 struct ospf6_distance *odistance;
632
633 ret = str2prefix_ipv6(ip_str, &p);
634 if (ret == 0) {
635 vty_out(vty, "Malformed prefix\n");
636 return CMD_WARNING_CONFIG_FAILED;
637 }
638
639 distance = atoi(distance_str);
640
641 /* Get OSPF6 distance node. */
642 rn = route_node_get(o->distance_table, (struct prefix *)&p);
643 if (rn->info) {
644 odistance = rn->info;
645 route_unlock_node(rn);
646 } else {
647 odistance = ospf6_distance_new();
648 rn->info = odistance;
649 }
650
651 /* Set distance value. */
652 odistance->distance = distance;
653
654 /* Reset access-list configuration. */
655 if (odistance->access_list) {
656 free(odistance->access_list);
657 odistance->access_list = NULL;
658 }
659 if (access_list_str)
660 odistance->access_list = strdup(access_list_str);
661
662 return CMD_SUCCESS;
baff583e
MZ
663}
664
d62a17ae 665int ospf6_distance_unset(struct vty *vty, struct ospf6 *o,
666 const char *distance_str, const char *ip_str,
667 const char *access_list_str)
baff583e 668{
d62a17ae 669 int ret;
670 struct prefix_ipv6 p;
671 struct route_node *rn;
672 struct ospf6_distance *odistance;
673
674 ret = str2prefix_ipv6(ip_str, &p);
675 if (ret == 0) {
676 vty_out(vty, "Malformed prefix\n");
677 return CMD_WARNING_CONFIG_FAILED;
678 }
679
680 rn = route_node_lookup(o->distance_table, (struct prefix *)&p);
681 if (!rn) {
682 vty_out(vty, "Cant't find specified prefix\n");
683 return CMD_WARNING_CONFIG_FAILED;
684 }
685
686 odistance = rn->info;
687
688 if (odistance->access_list)
689 free(odistance->access_list);
690 ospf6_distance_free(odistance);
691
692 rn->info = NULL;
693 route_unlock_node(rn);
694 route_unlock_node(rn);
695
696 return CMD_SUCCESS;
baff583e
MZ
697}
698
d62a17ae 699void ospf6_distance_reset(struct ospf6 *o)
baff583e 700{
d62a17ae 701 struct route_node *rn;
702 struct ospf6_distance *odistance;
703
704 for (rn = route_top(o->distance_table); rn; rn = route_next(rn))
705 if ((odistance = rn->info) != NULL) {
706 if (odistance->access_list)
707 free(odistance->access_list);
708 ospf6_distance_free(odistance);
709 rn->info = NULL;
710 route_unlock_node(rn);
711 }
baff583e
MZ
712}
713
beadc736 714uint8_t ospf6_distance_apply(struct prefix_ipv6 *p, struct ospf6_route * or,
715 struct ospf6 *ospf6)
baff583e 716{
d62a17ae 717 struct ospf6 *o;
baff583e 718
d62a17ae 719 o = ospf6;
720 if (o == NULL)
721 return 0;
baff583e 722
d62a17ae 723 if (o->distance_intra)
724 if (or->path.type == OSPF6_PATH_TYPE_INTRA)
725 return o->distance_intra;
baff583e 726
d62a17ae 727 if (o->distance_inter)
728 if (or->path.type == OSPF6_PATH_TYPE_INTER)
729 return o->distance_inter;
baff583e 730
d62a17ae 731 if (o->distance_external)
732 if (or->path.type == OSPF6_PATH_TYPE_EXTERNAL1 ||
733 or->path.type == OSPF6_PATH_TYPE_EXTERNAL2)
734 return o->distance_external;
baff583e 735
d62a17ae 736 if (o->distance_all)
737 return o->distance_all;
baff583e 738
d62a17ae 739 return 0;
baff583e
MZ
740}
741
d62a17ae 742static void ospf6_zebra_connected(struct zclient *zclient)
7076bb2f 743{
88b3d5e5
RW
744 struct ospf6 *ospf6;
745 struct listnode *node;
746
d62a17ae 747 /* Send the client registration */
0945d5ed 748 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
2376c3f2 749
d62a17ae 750 zclient_send_reg_requests(zclient, VRF_DEFAULT);
88b3d5e5
RW
751
752 /* Activate graceful restart if configured. */
753 for (ALL_LIST_ELEMENTS_RO(om6->ospf6, node, ospf6)) {
754 if (!ospf6->gr_info.restart_support)
755 continue;
756 (void)ospf6_zebra_gr_enable(ospf6, ospf6->gr_info.grace_period);
757 }
7076bb2f
FL
758}
759
a243d1db
DL
760static zclient_handler *const ospf6_handlers[] = {
761 [ZEBRA_ROUTER_ID_UPDATE] = ospf6_router_id_update_zebra,
762 [ZEBRA_INTERFACE_ADDRESS_ADD] = ospf6_zebra_if_address_update_add,
763 [ZEBRA_INTERFACE_ADDRESS_DELETE] = ospf6_zebra_if_address_update_delete,
764 [ZEBRA_REDISTRIBUTE_ROUTE_ADD] = ospf6_zebra_read_route,
765 [ZEBRA_REDISTRIBUTE_ROUTE_DEL] = ospf6_zebra_read_route,
766 [ZEBRA_NEXTHOP_UPDATE] = ospf6_zebra_import_check_update,
767};
768
cd9d0537 769void ospf6_zebra_init(struct event_loop *master)
718e3744 770{
d62a17ae 771 /* Allocate zebra structure. */
a243d1db
DL
772 zclient = zclient_new(master, &zclient_options_default, ospf6_handlers,
773 array_size(ospf6_handlers));
342213ea 774 zclient_init(zclient, ZEBRA_ROUTE_OSPF6, 0, &ospf6d_privs);
d62a17ae 775 zclient->zebra_connected = ospf6_zebra_connected;
d62a17ae 776
d62a17ae 777 /* Install command element for zebra node. */
778 install_element(VIEW_NODE, &show_ospf6_zebra_cmd);
718e3744 779}
780
508e53e2 781/* Debug */
6b0655a2 782
508e53e2 783DEFUN (debug_ospf6_zebra_sendrecv,
784 debug_ospf6_zebra_sendrecv_cmd,
1d68dbfe 785 "debug ospf6 zebra [<send|recv>]",
508e53e2 786 DEBUG_STR
787 OSPF6_STR
788 "Debug connection between zebra\n"
789 "Debug Sending zebra\n"
790 "Debug Receiving zebra\n"
791 )
792{
d62a17ae 793 int idx_send_recv = 3;
794 unsigned char level = 0;
795
796 if (argc == 4) {
797 if (strmatch(argv[idx_send_recv]->text, "send"))
798 level = OSPF6_DEBUG_ZEBRA_SEND;
799 else if (strmatch(argv[idx_send_recv]->text, "recv"))
800 level = OSPF6_DEBUG_ZEBRA_RECV;
801 } else
802 level = OSPF6_DEBUG_ZEBRA_SEND | OSPF6_DEBUG_ZEBRA_RECV;
803
804 OSPF6_DEBUG_ZEBRA_ON(level);
805 return CMD_SUCCESS;
508e53e2 806}
807
508e53e2 808DEFUN (no_debug_ospf6_zebra_sendrecv,
809 no_debug_ospf6_zebra_sendrecv_cmd,
1d68dbfe 810 "no debug ospf6 zebra [<send|recv>]",
508e53e2 811 NO_STR
812 DEBUG_STR
813 OSPF6_STR
814 "Debug connection between zebra\n"
815 "Debug Sending zebra\n"
816 "Debug Receiving zebra\n"
817 )
818{
d62a17ae 819 int idx_send_recv = 4;
820 unsigned char level = 0;
821
822 if (argc == 5) {
823 if (strmatch(argv[idx_send_recv]->text, "send"))
824 level = OSPF6_DEBUG_ZEBRA_SEND;
825 else if (strmatch(argv[idx_send_recv]->text, "recv"))
826 level = OSPF6_DEBUG_ZEBRA_RECV;
827 } else
828 level = OSPF6_DEBUG_ZEBRA_SEND | OSPF6_DEBUG_ZEBRA_RECV;
829
830 OSPF6_DEBUG_ZEBRA_OFF(level);
831 return CMD_SUCCESS;
508e53e2 832}
833
508e53e2 834
d62a17ae 835int config_write_ospf6_debug_zebra(struct vty *vty)
508e53e2 836{
d62a17ae 837 if (IS_OSPF6_DEBUG_ZEBRA(SEND) && IS_OSPF6_DEBUG_ZEBRA(RECV))
838 vty_out(vty, "debug ospf6 zebra\n");
839 else {
840 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
841 vty_out(vty, "debug ospf6 zebra send\n");
842 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
843 vty_out(vty, "debug ospf6 zebra recv\n");
844 }
845 return 0;
508e53e2 846}
847
d62a17ae 848void install_element_ospf6_debug_zebra(void)
718e3744 849{
d62a17ae 850 install_element(ENABLE_NODE, &debug_ospf6_zebra_sendrecv_cmd);
851 install_element(ENABLE_NODE, &no_debug_ospf6_zebra_sendrecv_cmd);
852 install_element(CONFIG_NODE, &debug_ospf6_zebra_sendrecv_cmd);
853 install_element(CONFIG_NODE, &no_debug_ospf6_zebra_sendrecv_cmd);
718e3744 854}