2 * Copyright (C) 2003 Yasuhiro Ohara
4 * This file is part of GNU Zebra.
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
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.
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
31 #include "lib_errors.h"
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"
42 #include "ospf6_area.h"
45 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_DISTANCE
, "OSPF6 distance");
47 unsigned char conf_debug_ospf6_zebra
= 0;
49 /* information about zebra. */
50 struct zclient
*zclient
= NULL
;
52 void ospf6_zebra_vrf_register(struct ospf6
*ospf6
)
54 if (!zclient
|| zclient
->sock
< 0 || !ospf6
)
57 if (ospf6
->vrf_id
!= VRF_UNKNOWN
) {
58 if (IS_OSPF6_DEBUG_ZEBRA(RECV
)) {
59 zlog_debug("%s: Register VRF %s id %u", __func__
,
60 ospf6_vrf_id_to_name(ospf6
->vrf_id
),
63 zclient_send_reg_requests(zclient
, ospf6
->vrf_id
);
67 void ospf6_zebra_vrf_deregister(struct ospf6
*ospf6
)
69 if (!zclient
|| zclient
->sock
< 0 || !ospf6
)
72 if (ospf6
->vrf_id
!= VRF_DEFAULT
&& ospf6
->vrf_id
!= VRF_UNKNOWN
) {
73 if (IS_OSPF6_DEBUG_ZEBRA(RECV
)) {
74 zlog_debug("%s: De-Register VRF %s id %u to Zebra.",
76 ospf6_vrf_id_to_name(ospf6
->vrf_id
),
79 /* Deregister for router-id, interfaces,
80 * redistributed routes. */
81 zclient_send_dereg_requests(zclient
, ospf6
->vrf_id
);
85 /* Router-id update message from zebra. */
86 static int ospf6_router_id_update_zebra(ZAPI_CALLBACK_ARGS
)
88 struct prefix router_id
;
91 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
93 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
94 zlog_debug("Zebra router-id update %pI4 vrf %s id %u",
95 &router_id
.u
.prefix4
, ospf6_vrf_id_to_name(vrf_id
),
98 o
= ospf6_lookup_by_vrf_id(vrf_id
);
102 o
->router_id_zebra
= router_id
.u
.prefix4
.s_addr
;
104 ospf6_router_id_update(o
);
109 /* redistribute function */
110 void ospf6_zebra_redistribute(int type
, vrf_id_t vrf_id
)
112 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], vrf_id
))
114 vrf_bitmap_set(zclient
->redist
[AFI_IP6
][type
], vrf_id
);
116 if (zclient
->sock
> 0)
117 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
,
118 AFI_IP6
, type
, 0, vrf_id
);
121 void ospf6_zebra_no_redistribute(int type
, vrf_id_t vrf_id
)
123 if (!vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], vrf_id
))
125 vrf_bitmap_unset(zclient
->redist
[AFI_IP6
][type
], vrf_id
);
126 if (zclient
->sock
> 0)
127 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
,
128 AFI_IP6
, type
, 0, vrf_id
);
131 static int ospf6_zebra_if_address_update_add(ZAPI_CALLBACK_ARGS
)
134 struct ospf6_interface
*oi
;
137 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
138 zclient
->ibuf
, vrf_id
);
142 oi
= (struct ospf6_interface
*)c
->ifp
->info
;
144 oi
= ospf6_interface_create(c
->ifp
);
147 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
148 zlog_debug("Zebra Interface address add: %s %5s %pFX",
149 c
->ifp
->name
, prefix_family_str(c
->address
),
152 ipv6_count
= connected_count_by_family(c
->ifp
, AF_INET6
);
153 if (oi
->ifmtu
== OSPF6_DEFAULT_MTU
&& ipv6_count
> OSPF6_MAX_IF_ADDRS
) {
155 "Zebra Interface : %s has too many interface addresses %d only support %d, increase MTU",
156 c
->ifp
->name
, ipv6_count
, OSPF6_MAX_IF_ADDRS
);
158 } else if (oi
->ifmtu
>= OSPF6_JUMBO_MTU
159 && ipv6_count
> OSPF6_MAX_IF_ADDRS_JUMBO
) {
161 "Zebra Interface : %s has too many interface addresses %d only support %d",
162 c
->ifp
->name
, ipv6_count
, OSPF6_MAX_IF_ADDRS_JUMBO
);
166 if (c
->address
->family
== AF_INET6
) {
167 ospf6_interface_state_update(c
->ifp
);
168 ospf6_interface_connected_route_update(c
->ifp
);
173 static int ospf6_zebra_if_address_update_delete(ZAPI_CALLBACK_ARGS
)
177 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
178 zclient
->ibuf
, vrf_id
);
182 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
183 zlog_debug("Zebra Interface address delete: %s %5s %pFX",
184 c
->ifp
->name
, prefix_family_str(c
->address
),
187 if (c
->address
->family
== AF_INET6
) {
188 ospf6_interface_connected_route_update(c
->ifp
);
189 ospf6_interface_state_update(c
->ifp
);
197 static int is_prefix_default(struct prefix_ipv6
*p
)
199 struct prefix_ipv6 q
= {};
204 return prefix_same((struct prefix
*)p
, (struct prefix
*)&q
);
207 static int ospf6_zebra_read_route(ZAPI_CALLBACK_ARGS
)
209 struct zapi_route api
;
210 unsigned long ifindex
;
211 struct in6_addr
*nexthop
;
213 struct prefix_ipv6 p
;
215 ospf6
= ospf6_lookup_by_vrf_id(vrf_id
);
220 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
223 /* we completely ignore srcdest routes for now. */
224 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
227 if (IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
230 ifindex
= api
.nexthops
[0].ifindex
;
231 nexthop
= &api
.nexthops
[0].gate
.ipv6
;
233 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
235 "Zebra Receive route %s: %s %pFX nexthop %pI6 ifindex %ld tag %" ROUTE_TAG_PRI
,
236 (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
? "add"
238 zebra_route_string(api
.type
), &api
.prefix
, nexthop
,
241 memcpy(&p
, &api
.prefix
, sizeof(p
));
242 if (is_prefix_default(&p
))
243 api
.type
= DEFAULT_ROUTE
;
245 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
246 ospf6_asbr_redistribute_add(api
.type
, ifindex
, &api
.prefix
,
247 api
.nexthop_num
, nexthop
, api
.tag
,
250 ospf6_asbr_redistribute_remove(api
.type
, ifindex
, &api
.prefix
,
257 show_ospf6_zebra_cmd
,
258 "show ipv6 ospf6 zebra [json]",
266 bool uj
= use_json(argc
, argv
);
268 json_object
*json_zebra
;
269 json_object
*json_array
;
271 if (zclient
== NULL
) {
272 vty_out(vty
, "Not connected to zebra\n");
277 json
= json_object_new_object();
278 json_zebra
= json_object_new_object();
279 json_array
= json_object_new_array();
281 json_object_int_add(json_zebra
, "fail", zclient
->fail
);
283 json_zebra
, "redistributeDefault",
284 vrf_bitmap_check(zclient
->default_information
[AFI_IP6
],
286 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
287 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][i
],
289 json_object_array_add(
291 json_object_new_string(
292 zebra_route_string(i
)));
294 json_object_object_add(json_zebra
, "redistribute", json_array
);
295 json_object_object_add(json
, "zebraInformation", json_zebra
);
298 json_object_to_json_string_ext(
299 json
, JSON_C_TO_STRING_PRETTY
));
301 vty_out(vty
, "Zebra Infomation\n");
302 vty_out(vty
, " fail: %d\n", zclient
->fail
);
303 vty_out(vty
, " redistribute default: %d\n",
304 vrf_bitmap_check(zclient
->default_information
[AFI_IP6
],
306 vty_out(vty
, " redistribute:");
307 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
308 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][i
],
310 vty_out(vty
, " %s", zebra_route_string(i
));
319 static void ospf6_zebra_route_update(int type
, struct ospf6_route
*request
,
322 struct zapi_route api
;
327 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
328 zlog_debug("Zebra Send %s route: %pFX",
329 (type
== REM
? "remove" : "add"), &request
->prefix
);
331 if (zclient
->sock
< 0) {
332 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
333 zlog_debug(" Not connected to Zebra");
337 if (request
->path
.origin
.adv_router
== ospf6
->router_id
338 && (request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
339 || request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)) {
340 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
341 zlog_debug(" Ignore self-originated external route");
345 /* If removing is the best path and if there's another path,
346 treat this request as add the secondary path */
347 if (type
== REM
&& ospf6_route_is_best(request
) && request
->next
348 && ospf6_route_is_same(request
, request
->next
)) {
349 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
351 " Best-path removal resulted Secondary addition");
353 request
= request
->next
;
356 /* Only the best path will be sent to zebra. */
357 if (!ospf6_route_is_best(request
)) {
358 /* this is not preferred best route, ignore */
359 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
360 zlog_debug(" Ignore non-best route");
364 nhcount
= ospf6_route_num_nexthops(request
);
366 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
367 zlog_debug(" No nexthop, ignore");
371 dest
= &request
->prefix
;
373 memset(&api
, 0, sizeof(api
));
374 api
.vrf_id
= ospf6
->vrf_id
;
375 api
.type
= ZEBRA_ROUTE_OSPF6
;
376 api
.safi
= SAFI_UNICAST
;
378 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
380 if (nhcount
> ospf6
->max_multipath
) {
381 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
383 " Nexthop count is greater than configured maximum-path, hence ignore the extra nexthops");
385 api
.nexthop_num
= MIN(nhcount
, ospf6
->max_multipath
);
387 ospf6_route_zebra_copy_nexthops(request
, api
.nexthops
, api
.nexthop_num
,
389 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
390 api
.metric
= (request
->path
.metric_type
== 2 ? request
->path
.u
.cost_e2
391 : request
->path
.cost
);
392 if (request
->path
.tag
) {
393 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
394 api
.tag
= request
->path
.tag
;
397 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
398 api
.distance
= ospf6_distance_apply((struct prefix_ipv6
*)dest
, request
,
402 ret
= zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
404 ret
= zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
406 if (ret
== ZCLIENT_SEND_FAILURE
)
407 flog_err(EC_LIB_ZAPI_SOCKET
,
408 "zclient_route_send() %s failed: %s",
409 (type
== REM
? "delete" : "add"),
410 safe_strerror(errno
));
415 void ospf6_zebra_route_update_add(struct ospf6_route
*request
,
418 ospf6_zebra_route_update(ADD
, request
, ospf6
);
421 void ospf6_zebra_route_update_remove(struct ospf6_route
*request
,
424 ospf6_zebra_route_update(REM
, request
, ospf6
);
427 void ospf6_zebra_add_discard(struct ospf6_route
*request
, struct ospf6
*ospf6
)
429 struct zapi_route api
;
430 struct prefix
*dest
= &request
->prefix
;
432 if (!CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
433 memset(&api
, 0, sizeof(api
));
434 api
.vrf_id
= ospf6
->vrf_id
;
435 api
.type
= ZEBRA_ROUTE_OSPF6
;
436 api
.safi
= SAFI_UNICAST
;
438 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
440 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
442 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
443 zlog_debug("Zebra: Route add discard %pFX", dest
);
445 SET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
447 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
449 "Zebra: Blackhole route present already %pFX",
454 void ospf6_zebra_delete_discard(struct ospf6_route
*request
,
457 struct zapi_route api
;
458 struct prefix
*dest
= &request
->prefix
;
460 if (CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
461 memset(&api
, 0, sizeof(api
));
462 api
.vrf_id
= ospf6
->vrf_id
;
463 api
.type
= ZEBRA_ROUTE_OSPF6
;
464 api
.safi
= SAFI_UNICAST
;
466 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
468 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
470 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
471 zlog_debug("Zebra: Route delete discard %pFX", dest
);
473 UNSET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
475 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
477 "Zebra: Blackhole route already deleted %pFX",
482 static struct ospf6_distance
*ospf6_distance_new(void)
484 return XCALLOC(MTYPE_OSPF6_DISTANCE
, sizeof(struct ospf6_distance
));
487 static void ospf6_distance_free(struct ospf6_distance
*odistance
)
489 XFREE(MTYPE_OSPF6_DISTANCE
, odistance
);
492 int ospf6_distance_set(struct vty
*vty
, struct ospf6
*o
,
493 const char *distance_str
, const char *ip_str
,
494 const char *access_list_str
)
497 struct prefix_ipv6 p
;
499 struct route_node
*rn
;
500 struct ospf6_distance
*odistance
;
502 ret
= str2prefix_ipv6(ip_str
, &p
);
504 vty_out(vty
, "Malformed prefix\n");
505 return CMD_WARNING_CONFIG_FAILED
;
508 distance
= atoi(distance_str
);
510 /* Get OSPF6 distance node. */
511 rn
= route_node_get(o
->distance_table
, (struct prefix
*)&p
);
513 odistance
= rn
->info
;
514 route_unlock_node(rn
);
516 odistance
= ospf6_distance_new();
517 rn
->info
= odistance
;
520 /* Set distance value. */
521 odistance
->distance
= distance
;
523 /* Reset access-list configuration. */
524 if (odistance
->access_list
) {
525 free(odistance
->access_list
);
526 odistance
->access_list
= NULL
;
529 odistance
->access_list
= strdup(access_list_str
);
534 int ospf6_distance_unset(struct vty
*vty
, struct ospf6
*o
,
535 const char *distance_str
, const char *ip_str
,
536 const char *access_list_str
)
539 struct prefix_ipv6 p
;
540 struct route_node
*rn
;
541 struct ospf6_distance
*odistance
;
543 ret
= str2prefix_ipv6(ip_str
, &p
);
545 vty_out(vty
, "Malformed prefix\n");
546 return CMD_WARNING_CONFIG_FAILED
;
549 rn
= route_node_lookup(o
->distance_table
, (struct prefix
*)&p
);
551 vty_out(vty
, "Cant't find specified prefix\n");
552 return CMD_WARNING_CONFIG_FAILED
;
555 odistance
= rn
->info
;
557 if (odistance
->access_list
)
558 free(odistance
->access_list
);
559 ospf6_distance_free(odistance
);
562 route_unlock_node(rn
);
563 route_unlock_node(rn
);
568 void ospf6_distance_reset(struct ospf6
*o
)
570 struct route_node
*rn
;
571 struct ospf6_distance
*odistance
;
573 for (rn
= route_top(o
->distance_table
); rn
; rn
= route_next(rn
))
574 if ((odistance
= rn
->info
) != NULL
) {
575 if (odistance
->access_list
)
576 free(odistance
->access_list
);
577 ospf6_distance_free(odistance
);
579 route_unlock_node(rn
);
583 uint8_t ospf6_distance_apply(struct prefix_ipv6
*p
, struct ospf6_route
* or,
592 if (o
->distance_intra
)
593 if (or->path
.type
== OSPF6_PATH_TYPE_INTRA
)
594 return o
->distance_intra
;
596 if (o
->distance_inter
)
597 if (or->path
.type
== OSPF6_PATH_TYPE_INTER
)
598 return o
->distance_inter
;
600 if (o
->distance_external
)
601 if (or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
||
602 or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)
603 return o
->distance_external
;
606 return o
->distance_all
;
611 static void ospf6_zebra_connected(struct zclient
*zclient
)
613 /* Send the client registration */
614 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
616 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
619 void ospf6_zebra_init(struct thread_master
*master
)
621 /* Allocate zebra structure. */
622 zclient
= zclient_new(master
, &zclient_options_default
);
623 zclient_init(zclient
, ZEBRA_ROUTE_OSPF6
, 0, &ospf6d_privs
);
624 zclient
->zebra_connected
= ospf6_zebra_connected
;
625 zclient
->router_id_update
= ospf6_router_id_update_zebra
;
626 zclient
->interface_address_add
= ospf6_zebra_if_address_update_add
;
627 zclient
->interface_address_delete
=
628 ospf6_zebra_if_address_update_delete
;
629 zclient
->redistribute_route_add
= ospf6_zebra_read_route
;
630 zclient
->redistribute_route_del
= ospf6_zebra_read_route
;
632 /* Install command element for zebra node. */
633 install_element(VIEW_NODE
, &show_ospf6_zebra_cmd
);
638 DEFUN (debug_ospf6_zebra_sendrecv
,
639 debug_ospf6_zebra_sendrecv_cmd
,
640 "debug ospf6 zebra [<send|recv>]",
643 "Debug connection between zebra\n"
644 "Debug Sending zebra\n"
645 "Debug Receiving zebra\n"
648 int idx_send_recv
= 3;
649 unsigned char level
= 0;
652 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
653 level
= OSPF6_DEBUG_ZEBRA_SEND
;
654 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
655 level
= OSPF6_DEBUG_ZEBRA_RECV
;
657 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
659 OSPF6_DEBUG_ZEBRA_ON(level
);
663 DEFUN (no_debug_ospf6_zebra_sendrecv
,
664 no_debug_ospf6_zebra_sendrecv_cmd
,
665 "no debug ospf6 zebra [<send|recv>]",
669 "Debug connection between zebra\n"
670 "Debug Sending zebra\n"
671 "Debug Receiving zebra\n"
674 int idx_send_recv
= 4;
675 unsigned char level
= 0;
678 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
679 level
= OSPF6_DEBUG_ZEBRA_SEND
;
680 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
681 level
= OSPF6_DEBUG_ZEBRA_RECV
;
683 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
685 OSPF6_DEBUG_ZEBRA_OFF(level
);
690 int config_write_ospf6_debug_zebra(struct vty
*vty
)
692 if (IS_OSPF6_DEBUG_ZEBRA(SEND
) && IS_OSPF6_DEBUG_ZEBRA(RECV
))
693 vty_out(vty
, "debug ospf6 zebra\n");
695 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
696 vty_out(vty
, "debug ospf6 zebra send\n");
697 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
698 vty_out(vty
, "debug ospf6 zebra recv\n");
703 void install_element_ospf6_debug_zebra(void)
705 install_element(ENABLE_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
706 install_element(ENABLE_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);
707 install_element(CONFIG_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
708 install_element(CONFIG_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);