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"
44 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_DISTANCE
, "OSPF6 distance")
46 unsigned char conf_debug_ospf6_zebra
= 0;
48 /* information about zebra. */
49 struct zclient
*zclient
= NULL
;
51 void ospf6_zebra_vrf_register(struct ospf6
*ospf6
)
53 if (!zclient
|| zclient
->sock
< 0 || !ospf6
)
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
),
62 zclient_send_reg_requests(zclient
, ospf6
->vrf_id
);
66 void ospf6_zebra_vrf_deregister(struct ospf6
*ospf6
)
68 if (!zclient
|| zclient
->sock
< 0 || !ospf6
)
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.",
75 ospf6_vrf_id_to_name(ospf6
->vrf_id
),
78 /* Deregister for router-id, interfaces,
79 * redistributed routes. */
80 zclient_send_dereg_requests(zclient
, ospf6
->vrf_id
);
84 /* Router-id update message from zebra. */
85 static int ospf6_router_id_update_zebra(ZAPI_CALLBACK_ARGS
)
87 struct prefix router_id
;
90 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
92 om6
->zebra_router_id
= router_id
.u
.prefix4
.s_addr
;
93 o
= ospf6_lookup_by_vrf_id(vrf_id
);
97 o
->router_id_zebra
= router_id
.u
.prefix4
;
98 if (IS_OSPF6_DEBUG_ZEBRA(RECV
)) {
99 char buf
[INET_ADDRSTRLEN
];
101 zlog_debug("%s: zebra router-id %s update", __func__
,
102 inet_ntop(AF_INET
, &router_id
.u
.prefix4
, buf
,
106 ospf6_router_id_update(o
);
111 /* redistribute function */
112 void ospf6_zebra_redistribute(int type
, vrf_id_t vrf_id
)
114 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], vrf_id
))
116 vrf_bitmap_set(zclient
->redist
[AFI_IP6
][type
], vrf_id
);
118 if (zclient
->sock
> 0)
119 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
,
120 AFI_IP6
, type
, 0, vrf_id
);
123 void ospf6_zebra_no_redistribute(int type
, vrf_id_t vrf_id
)
125 if (!vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], vrf_id
))
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
);
133 static int ospf6_zebra_if_address_update_add(ZAPI_CALLBACK_ARGS
)
137 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
138 zclient
->ibuf
, vrf_id
);
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
),
147 if (c
->address
->family
== AF_INET6
) {
148 ospf6_interface_state_update(c
->ifp
);
149 ospf6_interface_connected_route_update(c
->ifp
);
154 static int ospf6_zebra_if_address_update_delete(ZAPI_CALLBACK_ARGS
)
158 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
159 zclient
->ibuf
, vrf_id
);
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
),
168 if (c
->address
->family
== AF_INET6
) {
169 ospf6_interface_connected_route_update(c
->ifp
);
170 ospf6_interface_state_update(c
->ifp
);
178 static int ospf6_zebra_read_route(ZAPI_CALLBACK_ARGS
)
180 struct zapi_route api
;
181 unsigned long ifindex
;
182 struct in6_addr
*nexthop
;
185 ospf6
= ospf6_lookup_by_vrf_id(vrf_id
);
190 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
193 /* we completely ignore srcdest routes for now. */
194 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
197 if (IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
200 ifindex
= api
.nexthops
[0].ifindex
;
201 nexthop
= &api
.nexthops
[0].gate
.ipv6
;
203 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
205 "Zebra Receive route %s: %s %pFX nexthop %pI6 ifindex %ld tag %" ROUTE_TAG_PRI
,
206 (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
? "add"
208 zebra_route_string(api
.type
), &api
.prefix
, nexthop
,
211 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
212 ospf6_asbr_redistribute_add(api
.type
, ifindex
, &api
.prefix
,
213 api
.nexthop_num
, nexthop
, api
.tag
,
216 ospf6_asbr_redistribute_remove(api
.type
, ifindex
, &api
.prefix
,
223 show_ospf6_zebra_cmd
,
224 "show ipv6 ospf6 zebra",
231 if (zclient
== NULL
) {
232 vty_out(vty
, "Not connected to zebra\n");
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
],
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
));
252 static void ospf6_zebra_route_update(int type
, struct ospf6_route
*request
,
255 struct zapi_route api
;
260 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
261 zlog_debug("Send %s route: %pFX",
262 (type
== REM
? "remove" : "add"), &request
->prefix
);
264 if (zclient
->sock
< 0) {
265 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
266 zlog_debug(" Not connected to Zebra");
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");
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
))
284 " Best-path removal resulted Secondary addition");
286 request
= request
->next
;
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");
297 nhcount
= ospf6_route_num_nexthops(request
);
299 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
300 zlog_debug(" No nexthop, ignore");
304 dest
= &request
->prefix
;
306 memset(&api
, 0, sizeof(api
));
307 api
.vrf_id
= ospf6
->vrf_id
;
308 api
.type
= ZEBRA_ROUTE_OSPF6
;
309 api
.safi
= SAFI_UNICAST
;
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
;
322 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
323 api
.distance
= ospf6_distance_apply((struct prefix_ipv6
*)dest
, request
,
327 ret
= zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
329 ret
= zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
332 flog_err(EC_LIB_ZAPI_SOCKET
,
333 "zclient_route_send() %s failed: %s",
334 (type
== REM
? "delete" : "add"),
335 safe_strerror(errno
));
340 void ospf6_zebra_route_update_add(struct ospf6_route
*request
,
343 ospf6_zebra_route_update(ADD
, request
, ospf6
);
346 void ospf6_zebra_route_update_remove(struct ospf6_route
*request
,
349 ospf6_zebra_route_update(REM
, request
, ospf6
);
352 void ospf6_zebra_add_discard(struct ospf6_route
*request
, struct ospf6
*ospf6
)
354 struct zapi_route api
;
355 struct prefix
*dest
= &request
->prefix
;
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
;
363 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
365 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
367 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
368 zlog_debug("Zebra: Route add discard %pFX", dest
);
370 SET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
372 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
374 "Zebra: Blackhole route present already %pFX",
379 void ospf6_zebra_delete_discard(struct ospf6_route
*request
,
382 struct zapi_route api
;
383 struct prefix
*dest
= &request
->prefix
;
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
;
391 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
393 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
395 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
396 zlog_debug("Zebra: Route delete discard %pFX", dest
);
398 UNSET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
400 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
402 "Zebra: Blackhole route already deleted %pFX",
407 static struct ospf6_distance
*ospf6_distance_new(void)
409 return XCALLOC(MTYPE_OSPF6_DISTANCE
, sizeof(struct ospf6_distance
));
412 static void ospf6_distance_free(struct ospf6_distance
*odistance
)
414 XFREE(MTYPE_OSPF6_DISTANCE
, odistance
);
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
)
422 struct prefix_ipv6 p
;
424 struct route_node
*rn
;
425 struct ospf6_distance
*odistance
;
427 ret
= str2prefix_ipv6(ip_str
, &p
);
429 vty_out(vty
, "Malformed prefix\n");
430 return CMD_WARNING_CONFIG_FAILED
;
433 distance
= atoi(distance_str
);
435 /* Get OSPF6 distance node. */
436 rn
= route_node_get(o
->distance_table
, (struct prefix
*)&p
);
438 odistance
= rn
->info
;
439 route_unlock_node(rn
);
441 odistance
= ospf6_distance_new();
442 rn
->info
= odistance
;
445 /* Set distance value. */
446 odistance
->distance
= distance
;
448 /* Reset access-list configuration. */
449 if (odistance
->access_list
) {
450 free(odistance
->access_list
);
451 odistance
->access_list
= NULL
;
454 odistance
->access_list
= strdup(access_list_str
);
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
)
464 struct prefix_ipv6 p
;
465 struct route_node
*rn
;
466 struct ospf6_distance
*odistance
;
468 ret
= str2prefix_ipv6(ip_str
, &p
);
470 vty_out(vty
, "Malformed prefix\n");
471 return CMD_WARNING_CONFIG_FAILED
;
474 rn
= route_node_lookup(o
->distance_table
, (struct prefix
*)&p
);
476 vty_out(vty
, "Cant't find specified prefix\n");
477 return CMD_WARNING_CONFIG_FAILED
;
480 odistance
= rn
->info
;
482 if (odistance
->access_list
)
483 free(odistance
->access_list
);
484 ospf6_distance_free(odistance
);
487 route_unlock_node(rn
);
488 route_unlock_node(rn
);
493 void ospf6_distance_reset(struct ospf6
*o
)
495 struct route_node
*rn
;
496 struct ospf6_distance
*odistance
;
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
);
504 route_unlock_node(rn
);
508 uint8_t ospf6_distance_apply(struct prefix_ipv6
*p
, struct ospf6_route
* or,
517 if (o
->distance_intra
)
518 if (or->path
.type
== OSPF6_PATH_TYPE_INTRA
)
519 return o
->distance_intra
;
521 if (o
->distance_inter
)
522 if (or->path
.type
== OSPF6_PATH_TYPE_INTER
)
523 return o
->distance_inter
;
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
;
531 return o
->distance_all
;
536 static void ospf6_zebra_connected(struct zclient
*zclient
)
538 /* Send the client registration */
539 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
541 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
544 void ospf6_zebra_init(struct thread_master
*master
)
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
;
557 /* Install command element for zebra node. */
558 install_element(VIEW_NODE
, &show_ospf6_zebra_cmd
);
563 DEFUN (debug_ospf6_zebra_sendrecv
,
564 debug_ospf6_zebra_sendrecv_cmd
,
565 "debug ospf6 zebra [<send|recv>]",
568 "Debug connection between zebra\n"
569 "Debug Sending zebra\n"
570 "Debug Receiving zebra\n"
573 int idx_send_recv
= 3;
574 unsigned char level
= 0;
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
;
582 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
584 OSPF6_DEBUG_ZEBRA_ON(level
);
588 DEFUN (no_debug_ospf6_zebra_sendrecv
,
589 no_debug_ospf6_zebra_sendrecv_cmd
,
590 "no debug ospf6 zebra [<send|recv>]",
594 "Debug connection between zebra\n"
595 "Debug Sending zebra\n"
596 "Debug Receiving zebra\n"
599 int idx_send_recv
= 4;
600 unsigned char level
= 0;
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
;
608 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
610 OSPF6_DEBUG_ZEBRA_OFF(level
);
615 int config_write_ospf6_debug_zebra(struct vty
*vty
)
617 if (IS_OSPF6_DEBUG_ZEBRA(SEND
) && IS_OSPF6_DEBUG_ZEBRA(RECV
))
618 vty_out(vty
, "debug ospf6 zebra\n");
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");
628 void install_element_ospf6_debug_zebra(void)
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
);