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 om6
->zebra_router_id
= router_id
.u
.prefix4
.s_addr
;
94 o
= ospf6_lookup_by_vrf_id(vrf_id
);
98 o
->router_id_zebra
= router_id
.u
.prefix4
;
99 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
100 zlog_debug("%s: zebra router-id %pI4 update", __func__
,
101 &router_id
.u
.prefix4
);
103 ospf6_router_id_update(o
);
108 /* redistribute function */
109 void ospf6_zebra_redistribute(int type
, vrf_id_t vrf_id
)
111 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], vrf_id
))
113 vrf_bitmap_set(zclient
->redist
[AFI_IP6
][type
], vrf_id
);
115 if (zclient
->sock
> 0)
116 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
,
117 AFI_IP6
, type
, 0, vrf_id
);
120 void ospf6_zebra_no_redistribute(int type
, vrf_id_t vrf_id
)
122 if (!vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], vrf_id
))
124 vrf_bitmap_unset(zclient
->redist
[AFI_IP6
][type
], vrf_id
);
125 if (zclient
->sock
> 0)
126 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
,
127 AFI_IP6
, type
, 0, vrf_id
);
130 static int ospf6_zebra_if_address_update_add(ZAPI_CALLBACK_ARGS
)
133 struct ospf6_interface
*oi
;
136 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
137 zclient
->ibuf
, vrf_id
);
141 oi
= (struct ospf6_interface
*)c
->ifp
->info
;
143 oi
= ospf6_interface_create(c
->ifp
);
146 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
147 zlog_debug("Zebra Interface address add: %s %5s %pFX",
148 c
->ifp
->name
, prefix_family_str(c
->address
),
151 ipv6_count
= connected_count_by_family(c
->ifp
, AF_INET6
);
152 if (oi
->ifmtu
== OSPF6_DEFAULT_MTU
&& ipv6_count
> OSPF6_MAX_IF_ADDRS
) {
154 "Zebra Interface : %s has too many interface addresses %d only support %d, increase MTU",
155 c
->ifp
->name
, ipv6_count
, OSPF6_MAX_IF_ADDRS
);
157 } else if (oi
->ifmtu
>= OSPF6_JUMBO_MTU
158 && ipv6_count
> OSPF6_MAX_IF_ADDRS_JUMBO
) {
160 "Zebra Interface : %s has too many interface addresses %d only support %d",
161 c
->ifp
->name
, ipv6_count
, OSPF6_MAX_IF_ADDRS_JUMBO
);
165 if (c
->address
->family
== AF_INET6
) {
166 ospf6_interface_state_update(c
->ifp
);
167 ospf6_interface_connected_route_update(c
->ifp
);
172 static int ospf6_zebra_if_address_update_delete(ZAPI_CALLBACK_ARGS
)
176 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
177 zclient
->ibuf
, vrf_id
);
181 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
182 zlog_debug("Zebra Interface address delete: %s %5s %pFX",
183 c
->ifp
->name
, prefix_family_str(c
->address
),
186 if (c
->address
->family
== AF_INET6
) {
187 ospf6_interface_connected_route_update(c
->ifp
);
188 ospf6_interface_state_update(c
->ifp
);
196 static int is_prefix_default(struct prefix_ipv6
*p
)
198 struct prefix_ipv6 q
= {};
203 return prefix_same((struct prefix
*)p
, (struct prefix
*)&q
);
206 static int ospf6_zebra_read_route(ZAPI_CALLBACK_ARGS
)
208 struct zapi_route api
;
209 unsigned long ifindex
;
210 struct in6_addr
*nexthop
;
212 struct prefix_ipv6 p
;
214 ospf6
= ospf6_lookup_by_vrf_id(vrf_id
);
219 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
222 /* we completely ignore srcdest routes for now. */
223 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
226 if (IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
229 ifindex
= api
.nexthops
[0].ifindex
;
230 nexthop
= &api
.nexthops
[0].gate
.ipv6
;
232 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
234 "Zebra Receive route %s: %s %pFX nexthop %pI6 ifindex %ld tag %" ROUTE_TAG_PRI
,
235 (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
? "add"
237 zebra_route_string(api
.type
), &api
.prefix
, nexthop
,
240 memcpy(&p
, &api
.prefix
, sizeof(p
));
241 if (is_prefix_default(&p
))
242 api
.type
= DEFAULT_ROUTE
;
244 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
245 ospf6_asbr_redistribute_add(api
.type
, ifindex
, &api
.prefix
,
246 api
.nexthop_num
, nexthop
, api
.tag
,
249 ospf6_asbr_redistribute_remove(api
.type
, ifindex
, &api
.prefix
,
256 show_ospf6_zebra_cmd
,
257 "show ipv6 ospf6 zebra [json]",
265 bool uj
= use_json(argc
, argv
);
267 json_object
*json_zebra
;
268 json_object
*json_array
;
270 if (zclient
== NULL
) {
271 vty_out(vty
, "Not connected to zebra\n");
276 json
= json_object_new_object();
277 json_zebra
= json_object_new_object();
278 json_array
= json_object_new_array();
280 json_object_int_add(json_zebra
, "fail", zclient
->fail
);
282 json_zebra
, "redistributeDefault",
283 vrf_bitmap_check(zclient
->default_information
[AFI_IP6
],
285 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
286 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][i
],
288 json_object_array_add(
290 json_object_new_string(
291 zebra_route_string(i
)));
293 json_object_object_add(json_zebra
, "redistribute", json_array
);
294 json_object_object_add(json
, "zebraInformation", json_zebra
);
297 json_object_to_json_string_ext(
298 json
, JSON_C_TO_STRING_PRETTY
));
300 vty_out(vty
, "Zebra Infomation\n");
301 vty_out(vty
, " fail: %d\n", zclient
->fail
);
302 vty_out(vty
, " redistribute default: %d\n",
303 vrf_bitmap_check(zclient
->default_information
[AFI_IP6
],
305 vty_out(vty
, " redistribute:");
306 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
307 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][i
],
309 vty_out(vty
, " %s", zebra_route_string(i
));
318 static void ospf6_zebra_route_update(int type
, struct ospf6_route
*request
,
321 struct zapi_route api
;
326 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
327 zlog_debug("Zebra Send %s route: %pFX",
328 (type
== REM
? "remove" : "add"), &request
->prefix
);
330 if (zclient
->sock
< 0) {
331 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
332 zlog_debug(" Not connected to Zebra");
336 if (request
->path
.origin
.adv_router
== ospf6
->router_id
337 && (request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
338 || request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)) {
339 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
340 zlog_debug(" Ignore self-originated external route");
344 /* If removing is the best path and if there's another path,
345 treat this request as add the secondary path */
346 if (type
== REM
&& ospf6_route_is_best(request
) && request
->next
347 && ospf6_route_is_same(request
, request
->next
)) {
348 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
350 " Best-path removal resulted Secondary addition");
352 request
= request
->next
;
355 /* Only the best path will be sent to zebra. */
356 if (!ospf6_route_is_best(request
)) {
357 /* this is not preferred best route, ignore */
358 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
359 zlog_debug(" Ignore non-best route");
363 nhcount
= ospf6_route_num_nexthops(request
);
365 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
366 zlog_debug(" No nexthop, ignore");
370 dest
= &request
->prefix
;
372 memset(&api
, 0, sizeof(api
));
373 api
.vrf_id
= ospf6
->vrf_id
;
374 api
.type
= ZEBRA_ROUTE_OSPF6
;
375 api
.safi
= SAFI_UNICAST
;
377 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
379 if (nhcount
> ospf6
->max_multipath
) {
380 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
382 " Nexthop count is greater than configured maximum-path, hence ignore the extra nexthops");
384 api
.nexthop_num
= MIN(nhcount
, ospf6
->max_multipath
);
386 ospf6_route_zebra_copy_nexthops(request
, api
.nexthops
, api
.nexthop_num
,
388 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
389 api
.metric
= (request
->path
.metric_type
== 2 ? request
->path
.u
.cost_e2
390 : request
->path
.cost
);
391 if (request
->path
.tag
) {
392 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
393 api
.tag
= request
->path
.tag
;
396 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
397 api
.distance
= ospf6_distance_apply((struct prefix_ipv6
*)dest
, request
,
401 ret
= zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
403 ret
= zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
405 if (ret
== ZCLIENT_SEND_FAILURE
)
406 flog_err(EC_LIB_ZAPI_SOCKET
,
407 "zclient_route_send() %s failed: %s",
408 (type
== REM
? "delete" : "add"),
409 safe_strerror(errno
));
414 void ospf6_zebra_route_update_add(struct ospf6_route
*request
,
417 ospf6_zebra_route_update(ADD
, request
, ospf6
);
420 void ospf6_zebra_route_update_remove(struct ospf6_route
*request
,
423 ospf6_zebra_route_update(REM
, request
, ospf6
);
426 void ospf6_zebra_add_discard(struct ospf6_route
*request
, struct ospf6
*ospf6
)
428 struct zapi_route api
;
429 struct prefix
*dest
= &request
->prefix
;
431 if (!CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
432 memset(&api
, 0, sizeof(api
));
433 api
.vrf_id
= ospf6
->vrf_id
;
434 api
.type
= ZEBRA_ROUTE_OSPF6
;
435 api
.safi
= SAFI_UNICAST
;
437 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
439 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
441 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
442 zlog_debug("Zebra: Route add discard %pFX", dest
);
444 SET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
446 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
448 "Zebra: Blackhole route present already %pFX",
453 void ospf6_zebra_delete_discard(struct ospf6_route
*request
,
456 struct zapi_route api
;
457 struct prefix
*dest
= &request
->prefix
;
459 if (CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
460 memset(&api
, 0, sizeof(api
));
461 api
.vrf_id
= ospf6
->vrf_id
;
462 api
.type
= ZEBRA_ROUTE_OSPF6
;
463 api
.safi
= SAFI_UNICAST
;
465 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
467 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
469 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
470 zlog_debug("Zebra: Route delete discard %pFX", dest
);
472 UNSET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
474 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
476 "Zebra: Blackhole route already deleted %pFX",
481 static struct ospf6_distance
*ospf6_distance_new(void)
483 return XCALLOC(MTYPE_OSPF6_DISTANCE
, sizeof(struct ospf6_distance
));
486 static void ospf6_distance_free(struct ospf6_distance
*odistance
)
488 XFREE(MTYPE_OSPF6_DISTANCE
, odistance
);
491 int ospf6_distance_set(struct vty
*vty
, struct ospf6
*o
,
492 const char *distance_str
, const char *ip_str
,
493 const char *access_list_str
)
496 struct prefix_ipv6 p
;
498 struct route_node
*rn
;
499 struct ospf6_distance
*odistance
;
501 ret
= str2prefix_ipv6(ip_str
, &p
);
503 vty_out(vty
, "Malformed prefix\n");
504 return CMD_WARNING_CONFIG_FAILED
;
507 distance
= atoi(distance_str
);
509 /* Get OSPF6 distance node. */
510 rn
= route_node_get(o
->distance_table
, (struct prefix
*)&p
);
512 odistance
= rn
->info
;
513 route_unlock_node(rn
);
515 odistance
= ospf6_distance_new();
516 rn
->info
= odistance
;
519 /* Set distance value. */
520 odistance
->distance
= distance
;
522 /* Reset access-list configuration. */
523 if (odistance
->access_list
) {
524 free(odistance
->access_list
);
525 odistance
->access_list
= NULL
;
528 odistance
->access_list
= strdup(access_list_str
);
533 int ospf6_distance_unset(struct vty
*vty
, struct ospf6
*o
,
534 const char *distance_str
, const char *ip_str
,
535 const char *access_list_str
)
538 struct prefix_ipv6 p
;
539 struct route_node
*rn
;
540 struct ospf6_distance
*odistance
;
542 ret
= str2prefix_ipv6(ip_str
, &p
);
544 vty_out(vty
, "Malformed prefix\n");
545 return CMD_WARNING_CONFIG_FAILED
;
548 rn
= route_node_lookup(o
->distance_table
, (struct prefix
*)&p
);
550 vty_out(vty
, "Cant't find specified prefix\n");
551 return CMD_WARNING_CONFIG_FAILED
;
554 odistance
= rn
->info
;
556 if (odistance
->access_list
)
557 free(odistance
->access_list
);
558 ospf6_distance_free(odistance
);
561 route_unlock_node(rn
);
562 route_unlock_node(rn
);
567 void ospf6_distance_reset(struct ospf6
*o
)
569 struct route_node
*rn
;
570 struct ospf6_distance
*odistance
;
572 for (rn
= route_top(o
->distance_table
); rn
; rn
= route_next(rn
))
573 if ((odistance
= rn
->info
) != NULL
) {
574 if (odistance
->access_list
)
575 free(odistance
->access_list
);
576 ospf6_distance_free(odistance
);
578 route_unlock_node(rn
);
582 uint8_t ospf6_distance_apply(struct prefix_ipv6
*p
, struct ospf6_route
* or,
591 if (o
->distance_intra
)
592 if (or->path
.type
== OSPF6_PATH_TYPE_INTRA
)
593 return o
->distance_intra
;
595 if (o
->distance_inter
)
596 if (or->path
.type
== OSPF6_PATH_TYPE_INTER
)
597 return o
->distance_inter
;
599 if (o
->distance_external
)
600 if (or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
||
601 or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)
602 return o
->distance_external
;
605 return o
->distance_all
;
610 static void ospf6_zebra_connected(struct zclient
*zclient
)
612 /* Send the client registration */
613 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
615 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
618 void ospf6_zebra_init(struct thread_master
*master
)
620 /* Allocate zebra structure. */
621 zclient
= zclient_new(master
, &zclient_options_default
);
622 zclient_init(zclient
, ZEBRA_ROUTE_OSPF6
, 0, &ospf6d_privs
);
623 zclient
->zebra_connected
= ospf6_zebra_connected
;
624 zclient
->router_id_update
= ospf6_router_id_update_zebra
;
625 zclient
->interface_address_add
= ospf6_zebra_if_address_update_add
;
626 zclient
->interface_address_delete
=
627 ospf6_zebra_if_address_update_delete
;
628 zclient
->redistribute_route_add
= ospf6_zebra_read_route
;
629 zclient
->redistribute_route_del
= ospf6_zebra_read_route
;
631 /* Install command element for zebra node. */
632 install_element(VIEW_NODE
, &show_ospf6_zebra_cmd
);
637 DEFUN (debug_ospf6_zebra_sendrecv
,
638 debug_ospf6_zebra_sendrecv_cmd
,
639 "debug ospf6 zebra [<send|recv>]",
642 "Debug connection between zebra\n"
643 "Debug Sending zebra\n"
644 "Debug Receiving zebra\n"
647 int idx_send_recv
= 3;
648 unsigned char level
= 0;
651 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
652 level
= OSPF6_DEBUG_ZEBRA_SEND
;
653 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
654 level
= OSPF6_DEBUG_ZEBRA_RECV
;
656 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
658 OSPF6_DEBUG_ZEBRA_ON(level
);
662 DEFUN (no_debug_ospf6_zebra_sendrecv
,
663 no_debug_ospf6_zebra_sendrecv_cmd
,
664 "no debug ospf6 zebra [<send|recv>]",
668 "Debug connection between zebra\n"
669 "Debug Sending zebra\n"
670 "Debug Receiving zebra\n"
673 int idx_send_recv
= 4;
674 unsigned char level
= 0;
677 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
678 level
= OSPF6_DEBUG_ZEBRA_SEND
;
679 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
680 level
= OSPF6_DEBUG_ZEBRA_RECV
;
682 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
684 OSPF6_DEBUG_ZEBRA_OFF(level
);
689 int config_write_ospf6_debug_zebra(struct vty
*vty
)
691 if (IS_OSPF6_DEBUG_ZEBRA(SEND
) && IS_OSPF6_DEBUG_ZEBRA(RECV
))
692 vty_out(vty
, "debug ospf6 zebra\n");
694 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
695 vty_out(vty
, "debug ospf6 zebra send\n");
696 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
697 vty_out(vty
, "debug ospf6 zebra recv\n");
702 void install_element_ospf6_debug_zebra(void)
704 install_element(ENABLE_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
705 install_element(ENABLE_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);
706 install_element(CONFIG_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
707 install_element(CONFIG_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);