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"
43 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_DISTANCE
, "OSPF6 distance")
45 unsigned char conf_debug_ospf6_zebra
= 0;
47 /* information about zebra. */
48 struct zclient
*zclient
= NULL
;
50 /* Router-id update message from zebra. */
51 static int ospf6_router_id_update_zebra(ZAPI_CALLBACK_ARGS
)
53 struct prefix router_id
;
54 struct ospf6
*o
= ospf6
;
56 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
58 om6
->zebra_router_id
= router_id
.u
.prefix4
.s_addr
;
63 o
->router_id_zebra
= router_id
.u
.prefix4
;
64 if (IS_OSPF6_DEBUG_ZEBRA(RECV
)) {
65 char buf
[INET_ADDRSTRLEN
];
67 zlog_debug("%s: zebra router-id %s update", __func__
,
68 inet_ntop(AF_INET
, &router_id
.u
.prefix4
, buf
,
72 ospf6_router_id_update();
77 /* redistribute function */
78 void ospf6_zebra_redistribute(int type
)
80 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
))
82 vrf_bitmap_set(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
);
84 if (zclient
->sock
> 0)
85 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
,
86 AFI_IP6
, type
, 0, VRF_DEFAULT
);
89 void ospf6_zebra_no_redistribute(int type
)
91 if (!vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
))
93 vrf_bitmap_unset(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
);
94 if (zclient
->sock
> 0)
95 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
,
96 AFI_IP6
, type
, 0, VRF_DEFAULT
);
99 static int ospf6_zebra_if_address_update_add(ZAPI_CALLBACK_ARGS
)
104 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
105 zclient
->ibuf
, vrf_id
);
109 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
110 zlog_debug("Zebra Interface address add: %s %5s %s/%d",
111 c
->ifp
->name
, prefix_family_str(c
->address
),
112 inet_ntop(c
->address
->family
, &c
->address
->u
.prefix
,
114 c
->address
->prefixlen
);
116 if (c
->address
->family
== AF_INET6
) {
117 ospf6_interface_state_update(c
->ifp
);
118 ospf6_interface_connected_route_update(c
->ifp
);
123 static int ospf6_zebra_if_address_update_delete(ZAPI_CALLBACK_ARGS
)
128 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
129 zclient
->ibuf
, vrf_id
);
133 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
134 zlog_debug("Zebra Interface address delete: %s %5s %s/%d",
135 c
->ifp
->name
, prefix_family_str(c
->address
),
136 inet_ntop(c
->address
->family
, &c
->address
->u
.prefix
,
138 c
->address
->prefixlen
);
140 if (c
->address
->family
== AF_INET6
) {
141 ospf6_interface_connected_route_update(c
->ifp
);
142 ospf6_interface_state_update(c
->ifp
);
150 static int ospf6_zebra_read_route(ZAPI_CALLBACK_ARGS
)
152 struct zapi_route api
;
153 unsigned long ifindex
;
154 struct in6_addr
*nexthop
;
159 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
162 /* we completely ignore srcdest routes for now. */
163 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
166 if (IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
169 ifindex
= api
.nexthops
[0].ifindex
;
170 nexthop
= &api
.nexthops
[0].gate
.ipv6
;
172 if (IS_OSPF6_DEBUG_ZEBRA(RECV
)) {
173 char prefixstr
[PREFIX2STR_BUFFER
], nexthopstr
[128];
175 prefix2str(&api
.prefix
, prefixstr
, sizeof(prefixstr
));
176 inet_ntop(AF_INET6
, nexthop
, nexthopstr
, sizeof(nexthopstr
));
179 "Zebra Receive route %s: %s %s nexthop %s ifindex %ld tag %" ROUTE_TAG_PRI
,
180 (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
? "add"
182 zebra_route_string(api
.type
), prefixstr
, nexthopstr
,
186 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
187 ospf6_asbr_redistribute_add(api
.type
, ifindex
, &api
.prefix
,
188 api
.nexthop_num
, nexthop
, api
.tag
);
190 ospf6_asbr_redistribute_remove(api
.type
, ifindex
, &api
.prefix
);
196 show_ospf6_zebra_cmd
,
197 "show ipv6 ospf6 zebra",
204 if (zclient
== NULL
) {
205 vty_out(vty
, "Not connected to zebra\n");
209 vty_out(vty
, "Zebra Information\n");
210 vty_out(vty
, " fail: %d\n", zclient
->fail
);
211 vty_out(vty
, " redistribute default: %d\n",
212 vrf_bitmap_check(zclient
->default_information
[AFI_IP6
],
214 vty_out(vty
, " redistribute:");
215 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
216 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][i
], VRF_DEFAULT
))
217 vty_out(vty
, " %s", zebra_route_string(i
));
225 static void ospf6_zebra_route_update(int type
, struct ospf6_route
*request
)
227 struct zapi_route api
;
228 char buf
[PREFIX2STR_BUFFER
];
233 if (IS_OSPF6_DEBUG_ZEBRA(SEND
)) {
234 prefix2str(&request
->prefix
, buf
, sizeof(buf
));
235 zlog_debug("Send %s route: %s",
236 (type
== REM
? "remove" : "add"), buf
);
239 if (zclient
->sock
< 0) {
240 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
241 zlog_debug(" Not connected to Zebra");
245 if (request
->path
.origin
.adv_router
== ospf6
->router_id
246 && (request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
247 || request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)) {
248 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
249 zlog_debug(" Ignore self-originated external route");
253 /* If removing is the best path and if there's another path,
254 treat this request as add the secondary path */
255 if (type
== REM
&& ospf6_route_is_best(request
) && request
->next
256 && ospf6_route_is_same(request
, request
->next
)) {
257 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
259 " Best-path removal resulted Secondary addition");
261 request
= request
->next
;
264 /* Only the best path will be sent to zebra. */
265 if (!ospf6_route_is_best(request
)) {
266 /* this is not preferred best route, ignore */
267 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
268 zlog_debug(" Ignore non-best route");
272 nhcount
= ospf6_route_num_nexthops(request
);
274 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
275 zlog_debug(" No nexthop, ignore");
279 dest
= &request
->prefix
;
281 memset(&api
, 0, sizeof(api
));
282 api
.vrf_id
= VRF_DEFAULT
;
283 api
.type
= ZEBRA_ROUTE_OSPF6
;
284 api
.safi
= SAFI_UNICAST
;
286 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
287 api
.nexthop_num
= MIN(nhcount
, MULTIPATH_NUM
);
288 ospf6_route_zebra_copy_nexthops(request
, api
.nexthops
, api
.nexthop_num
);
289 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
290 api
.metric
= (request
->path
.metric_type
== 2 ? request
->path
.u
.cost_e2
291 : request
->path
.cost
);
292 if (request
->path
.tag
) {
293 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
294 api
.tag
= request
->path
.tag
;
297 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
299 ospf6_distance_apply((struct prefix_ipv6
*)dest
, request
);
302 ret
= zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
304 ret
= zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
307 flog_err(EC_LIB_ZAPI_SOCKET
,
308 "zclient_route_send() %s failed: %s",
309 (type
== REM
? "delete" : "add"),
310 safe_strerror(errno
));
315 void ospf6_zebra_route_update_add(struct ospf6_route
*request
)
317 ospf6_zebra_route_update(ADD
, request
);
320 void ospf6_zebra_route_update_remove(struct ospf6_route
*request
)
322 ospf6_zebra_route_update(REM
, request
);
325 void ospf6_zebra_add_discard(struct ospf6_route
*request
)
327 struct zapi_route api
;
328 char buf
[INET6_ADDRSTRLEN
];
329 struct prefix
*dest
= &request
->prefix
;
331 if (!CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
332 memset(&api
, 0, sizeof(api
));
333 api
.vrf_id
= VRF_DEFAULT
;
334 api
.type
= ZEBRA_ROUTE_OSPF6
;
335 api
.safi
= SAFI_UNICAST
;
337 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
339 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
341 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
342 zlog_debug("Zebra: Route add discard %s/%d",
343 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
347 SET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
349 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
351 "Zebra: Blackhole route present already %s/%d",
352 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
358 void ospf6_zebra_delete_discard(struct ospf6_route
*request
)
360 struct zapi_route api
;
361 char buf
[INET6_ADDRSTRLEN
];
362 struct prefix
*dest
= &request
->prefix
;
364 if (CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
365 memset(&api
, 0, sizeof(api
));
366 api
.vrf_id
= VRF_DEFAULT
;
367 api
.type
= ZEBRA_ROUTE_OSPF6
;
368 api
.safi
= SAFI_UNICAST
;
370 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
372 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
374 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
375 zlog_debug("Zebra: Route delete discard %s/%d",
376 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
380 UNSET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
382 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
384 "Zebra: Blackhole route already deleted %s/%d",
385 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
391 static struct ospf6_distance
*ospf6_distance_new(void)
393 return XCALLOC(MTYPE_OSPF6_DISTANCE
, sizeof(struct ospf6_distance
));
396 static void ospf6_distance_free(struct ospf6_distance
*odistance
)
398 XFREE(MTYPE_OSPF6_DISTANCE
, odistance
);
401 int ospf6_distance_set(struct vty
*vty
, struct ospf6
*o
,
402 const char *distance_str
, const char *ip_str
,
403 const char *access_list_str
)
406 struct prefix_ipv6 p
;
408 struct route_node
*rn
;
409 struct ospf6_distance
*odistance
;
411 ret
= str2prefix_ipv6(ip_str
, &p
);
413 vty_out(vty
, "Malformed prefix\n");
414 return CMD_WARNING_CONFIG_FAILED
;
417 distance
= atoi(distance_str
);
419 /* Get OSPF6 distance node. */
420 rn
= route_node_get(o
->distance_table
, (struct prefix
*)&p
);
422 odistance
= rn
->info
;
423 route_unlock_node(rn
);
425 odistance
= ospf6_distance_new();
426 rn
->info
= odistance
;
429 /* Set distance value. */
430 odistance
->distance
= distance
;
432 /* Reset access-list configuration. */
433 if (odistance
->access_list
) {
434 free(odistance
->access_list
);
435 odistance
->access_list
= NULL
;
438 odistance
->access_list
= strdup(access_list_str
);
443 int ospf6_distance_unset(struct vty
*vty
, struct ospf6
*o
,
444 const char *distance_str
, const char *ip_str
,
445 const char *access_list_str
)
448 struct prefix_ipv6 p
;
449 struct route_node
*rn
;
450 struct ospf6_distance
*odistance
;
452 ret
= str2prefix_ipv6(ip_str
, &p
);
454 vty_out(vty
, "Malformed prefix\n");
455 return CMD_WARNING_CONFIG_FAILED
;
458 rn
= route_node_lookup(o
->distance_table
, (struct prefix
*)&p
);
460 vty_out(vty
, "Cant't find specified prefix\n");
461 return CMD_WARNING_CONFIG_FAILED
;
464 odistance
= rn
->info
;
466 if (odistance
->access_list
)
467 free(odistance
->access_list
);
468 ospf6_distance_free(odistance
);
471 route_unlock_node(rn
);
472 route_unlock_node(rn
);
477 void ospf6_distance_reset(struct ospf6
*o
)
479 struct route_node
*rn
;
480 struct ospf6_distance
*odistance
;
482 for (rn
= route_top(o
->distance_table
); rn
; rn
= route_next(rn
))
483 if ((odistance
= rn
->info
) != NULL
) {
484 if (odistance
->access_list
)
485 free(odistance
->access_list
);
486 ospf6_distance_free(odistance
);
488 route_unlock_node(rn
);
492 uint8_t ospf6_distance_apply(struct prefix_ipv6
*p
, struct ospf6_route
* or)
500 if (o
->distance_intra
)
501 if (or->path
.type
== OSPF6_PATH_TYPE_INTRA
)
502 return o
->distance_intra
;
504 if (o
->distance_inter
)
505 if (or->path
.type
== OSPF6_PATH_TYPE_INTER
)
506 return o
->distance_inter
;
508 if (o
->distance_external
)
509 if (or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
||
510 or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)
511 return o
->distance_external
;
514 return o
->distance_all
;
519 static void ospf6_zebra_connected(struct zclient
*zclient
)
521 /* Send the client registration */
522 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
524 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
527 void ospf6_zebra_init(struct thread_master
*master
)
529 /* Allocate zebra structure. */
530 zclient
= zclient_new(master
, &zclient_options_default
);
531 zclient_init(zclient
, ZEBRA_ROUTE_OSPF6
, 0, &ospf6d_privs
);
532 zclient
->zebra_connected
= ospf6_zebra_connected
;
533 zclient
->router_id_update
= ospf6_router_id_update_zebra
;
534 zclient
->interface_address_add
= ospf6_zebra_if_address_update_add
;
535 zclient
->interface_address_delete
=
536 ospf6_zebra_if_address_update_delete
;
537 zclient
->redistribute_route_add
= ospf6_zebra_read_route
;
538 zclient
->redistribute_route_del
= ospf6_zebra_read_route
;
540 /* Install command element for zebra node. */
541 install_element(VIEW_NODE
, &show_ospf6_zebra_cmd
);
546 DEFUN (debug_ospf6_zebra_sendrecv
,
547 debug_ospf6_zebra_sendrecv_cmd
,
548 "debug ospf6 zebra [<send|recv>]",
551 "Debug connection between zebra\n"
552 "Debug Sending zebra\n"
553 "Debug Receiving zebra\n"
556 int idx_send_recv
= 3;
557 unsigned char level
= 0;
560 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
561 level
= OSPF6_DEBUG_ZEBRA_SEND
;
562 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
563 level
= OSPF6_DEBUG_ZEBRA_RECV
;
565 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
567 OSPF6_DEBUG_ZEBRA_ON(level
);
571 DEFUN (no_debug_ospf6_zebra_sendrecv
,
572 no_debug_ospf6_zebra_sendrecv_cmd
,
573 "no debug ospf6 zebra [<send|recv>]",
577 "Debug connection between zebra\n"
578 "Debug Sending zebra\n"
579 "Debug Receiving zebra\n"
582 int idx_send_recv
= 4;
583 unsigned char level
= 0;
586 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
587 level
= OSPF6_DEBUG_ZEBRA_SEND
;
588 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
589 level
= OSPF6_DEBUG_ZEBRA_RECV
;
591 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
593 OSPF6_DEBUG_ZEBRA_OFF(level
);
598 int config_write_ospf6_debug_zebra(struct vty
*vty
)
600 if (IS_OSPF6_DEBUG_ZEBRA(SEND
) && IS_OSPF6_DEBUG_ZEBRA(RECV
))
601 vty_out(vty
, "debug ospf6 zebra\n");
603 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
604 vty_out(vty
, "debug ospf6 zebra send\n");
605 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
606 vty_out(vty
, "debug ospf6 zebra recv\n");
611 void install_element_ospf6_debug_zebra(void)
613 install_element(ENABLE_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
614 install_element(ENABLE_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);
615 install_element(CONFIG_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
616 install_element(CONFIG_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);