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(int command
, struct zclient
*zclient
,
52 zebra_size_t length
, vrf_id_t vrf_id
)
54 struct prefix router_id
;
55 struct ospf6
*o
= ospf6
;
57 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
59 om6
->zebra_router_id
= router_id
.u
.prefix4
.s_addr
;
64 o
->router_id_zebra
= router_id
.u
.prefix4
;
65 if (IS_OSPF6_DEBUG_ZEBRA(RECV
)) {
66 char buf
[INET_ADDRSTRLEN
];
68 zlog_debug("%s: zebra router-id %s update",
70 inet_ntop(AF_INET
, &router_id
.u
.prefix4
,
71 buf
, INET_ADDRSTRLEN
));
74 ospf6_router_id_update();
79 /* redistribute function */
80 void ospf6_zebra_redistribute(int type
)
82 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
))
84 vrf_bitmap_set(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
);
86 if (zclient
->sock
> 0)
87 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
,
88 AFI_IP6
, type
, 0, VRF_DEFAULT
);
91 void ospf6_zebra_no_redistribute(int type
)
93 if (!vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
))
95 vrf_bitmap_unset(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
);
96 if (zclient
->sock
> 0)
97 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
,
98 AFI_IP6
, type
, 0, VRF_DEFAULT
);
101 /* Inteface addition message from zebra. */
102 static int ospf6_zebra_if_add(int command
, struct zclient
*zclient
,
103 zebra_size_t length
, vrf_id_t vrf_id
)
105 struct interface
*ifp
;
107 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
108 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
109 zlog_debug("Zebra Interface add: %s index %d mtu %d", ifp
->name
,
110 ifp
->ifindex
, ifp
->mtu6
);
111 ospf6_interface_if_add(ifp
);
115 static int ospf6_zebra_if_del(int command
, struct zclient
*zclient
,
116 zebra_size_t length
, vrf_id_t vrf_id
)
118 struct interface
*ifp
;
120 if (!(ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
)))
124 zlog_warn("Zebra: got delete of %s, but interface is still up",
127 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
128 zlog_debug("Zebra Interface delete: %s index %d mtu %d",
129 ifp
->name
, ifp
->ifindex
, ifp
->mtu6
);
131 if_set_index(ifp
, IFINDEX_INTERNAL
);
135 static int ospf6_zebra_if_state_update(int command
, struct zclient
*zclient
,
136 zebra_size_t length
, vrf_id_t vrf_id
)
138 struct interface
*ifp
;
140 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
144 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
146 "Zebra Interface state change: "
147 "%s index %d flags %llx metric %d mtu %d bandwidth %d",
148 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
149 ifp
->metric
, ifp
->mtu6
, ifp
->bandwidth
);
151 ospf6_interface_state_update(ifp
);
155 static int ospf6_zebra_if_address_update_add(int command
,
156 struct zclient
*zclient
,
163 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
164 zclient
->ibuf
, vrf_id
);
168 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
169 zlog_debug("Zebra Interface address add: %s %5s %s/%d",
170 c
->ifp
->name
, prefix_family_str(c
->address
),
171 inet_ntop(c
->address
->family
, &c
->address
->u
.prefix
,
173 c
->address
->prefixlen
);
175 if (c
->address
->family
== AF_INET6
) {
176 ospf6_interface_state_update(c
->ifp
);
177 ospf6_interface_connected_route_update(c
->ifp
);
182 static int ospf6_zebra_if_address_update_delete(int command
,
183 struct zclient
*zclient
,
190 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
191 zclient
->ibuf
, vrf_id
);
195 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
196 zlog_debug("Zebra Interface address delete: %s %5s %s/%d",
197 c
->ifp
->name
, prefix_family_str(c
->address
),
198 inet_ntop(c
->address
->family
, &c
->address
->u
.prefix
,
200 c
->address
->prefixlen
);
202 if (c
->address
->family
== AF_INET6
) {
203 ospf6_interface_connected_route_update(c
->ifp
);
204 ospf6_interface_state_update(c
->ifp
);
212 static int ospf6_zebra_read_route(int command
, struct zclient
*zclient
,
213 zebra_size_t length
, vrf_id_t vrf_id
)
215 struct zapi_route api
;
216 unsigned long ifindex
;
217 struct in6_addr
*nexthop
;
222 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
225 /* we completely ignore srcdest routes for now. */
226 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
229 if (IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
232 ifindex
= api
.nexthops
[0].ifindex
;
233 nexthop
= &api
.nexthops
[0].gate
.ipv6
;
235 if (IS_OSPF6_DEBUG_ZEBRA(RECV
)) {
236 char prefixstr
[PREFIX2STR_BUFFER
], nexthopstr
[128];
237 prefix2str((struct prefix
*)&api
.prefix
, prefixstr
,
239 inet_ntop(AF_INET6
, nexthop
, nexthopstr
, sizeof(nexthopstr
));
242 "Zebra Receive route %s: %s %s nexthop %s ifindex %ld tag %" ROUTE_TAG_PRI
,
243 (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
? "add"
245 zebra_route_string(api
.type
), prefixstr
, nexthopstr
,
249 if (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
250 ospf6_asbr_redistribute_add(api
.type
, ifindex
, &api
.prefix
,
251 api
.nexthop_num
, nexthop
, api
.tag
);
253 ospf6_asbr_redistribute_remove(api
.type
, ifindex
, &api
.prefix
);
259 show_ospf6_zebra_cmd
,
260 "show ipv6 ospf6 zebra",
267 if (zclient
== NULL
) {
268 vty_out(vty
, "Not connected to zebra\n");
272 vty_out(vty
, "Zebra Infomation\n");
273 vty_out(vty
, " fail: %d\n", zclient
->fail
);
274 vty_out(vty
, " redistribute default: %d\n",
275 vrf_bitmap_check(zclient
->default_information
[AFI_IP6
],
277 vty_out(vty
, " redistribute:");
278 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
279 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][i
], VRF_DEFAULT
))
280 vty_out(vty
, " %s", zebra_route_string(i
));
288 static void ospf6_zebra_route_update(int type
, struct ospf6_route
*request
)
290 struct zapi_route api
;
291 char buf
[PREFIX2STR_BUFFER
];
296 if (IS_OSPF6_DEBUG_ZEBRA(SEND
)) {
297 prefix2str(&request
->prefix
, buf
, sizeof(buf
));
298 zlog_debug("Send %s route: %s",
299 (type
== REM
? "remove" : "add"), buf
);
302 if (zclient
->sock
< 0) {
303 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
304 zlog_debug(" Not connected to Zebra");
308 if (request
->path
.origin
.adv_router
== ospf6
->router_id
309 && (request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
310 || request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)) {
311 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
312 zlog_debug(" Ignore self-originated external route");
316 /* If removing is the best path and if there's another path,
317 treat this request as add the secondary path */
318 if (type
== REM
&& ospf6_route_is_best(request
) && request
->next
319 && ospf6_route_is_same(request
, request
->next
)) {
320 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
322 " Best-path removal resulted Sencondary addition");
324 request
= request
->next
;
327 /* Only the best path will be sent to zebra. */
328 if (!ospf6_route_is_best(request
)) {
329 /* this is not preferred best route, ignore */
330 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
331 zlog_debug(" Ignore non-best route");
335 nhcount
= ospf6_route_num_nexthops(request
);
337 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
338 zlog_debug(" No nexthop, ignore");
342 dest
= &request
->prefix
;
344 memset(&api
, 0, sizeof(api
));
345 api
.vrf_id
= VRF_DEFAULT
;
346 api
.type
= ZEBRA_ROUTE_OSPF6
;
347 api
.safi
= SAFI_UNICAST
;
349 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
350 api
.nexthop_num
= MIN(nhcount
, MULTIPATH_NUM
);
351 ospf6_route_zebra_copy_nexthops(request
, api
.nexthops
, api
.nexthop_num
);
352 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
353 api
.metric
= (request
->path
.metric_type
== 2 ? request
->path
.u
.cost_e2
354 : request
->path
.cost
);
355 if (request
->path
.tag
) {
356 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
357 api
.tag
= request
->path
.tag
;
360 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
362 ospf6_distance_apply((struct prefix_ipv6
*)dest
, request
);
365 ret
= zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
367 ret
= zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
370 flog_err(EC_LIB_ZAPI_SOCKET
,
371 "zclient_route_send() %s failed: %s",
372 (type
== REM
? "delete" : "add"),
373 safe_strerror(errno
));
378 void ospf6_zebra_route_update_add(struct ospf6_route
*request
)
380 ospf6_zebra_route_update(ADD
, request
);
383 void ospf6_zebra_route_update_remove(struct ospf6_route
*request
)
385 ospf6_zebra_route_update(REM
, request
);
388 void ospf6_zebra_add_discard(struct ospf6_route
*request
)
390 struct zapi_route api
;
391 char buf
[INET6_ADDRSTRLEN
];
392 struct prefix
*dest
= &request
->prefix
;
394 if (!CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
395 memset(&api
, 0, sizeof(api
));
396 api
.vrf_id
= VRF_DEFAULT
;
397 api
.type
= ZEBRA_ROUTE_OSPF6
;
398 api
.safi
= SAFI_UNICAST
;
400 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
402 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
404 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
405 zlog_debug("Zebra: Route add discard %s/%d",
406 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
410 SET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
412 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
414 "Zebra: Blackhole route present already %s/%d",
415 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
421 void ospf6_zebra_delete_discard(struct ospf6_route
*request
)
423 struct zapi_route api
;
424 char buf
[INET6_ADDRSTRLEN
];
425 struct prefix
*dest
= &request
->prefix
;
427 if (CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
428 memset(&api
, 0, sizeof(api
));
429 api
.vrf_id
= VRF_DEFAULT
;
430 api
.type
= ZEBRA_ROUTE_OSPF6
;
431 api
.safi
= SAFI_UNICAST
;
433 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
435 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
437 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
438 zlog_debug("Zebra: Route delete discard %s/%d",
439 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
443 UNSET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
445 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
447 "Zebra: Blackhole route already deleted %s/%d",
448 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
454 static struct ospf6_distance
*ospf6_distance_new(void)
456 return XCALLOC(MTYPE_OSPF6_DISTANCE
, sizeof(struct ospf6_distance
));
459 static void ospf6_distance_free(struct ospf6_distance
*odistance
)
461 XFREE(MTYPE_OSPF6_DISTANCE
, odistance
);
464 int ospf6_distance_set(struct vty
*vty
, struct ospf6
*o
,
465 const char *distance_str
, const char *ip_str
,
466 const char *access_list_str
)
469 struct prefix_ipv6 p
;
471 struct route_node
*rn
;
472 struct ospf6_distance
*odistance
;
474 ret
= str2prefix_ipv6(ip_str
, &p
);
476 vty_out(vty
, "Malformed prefix\n");
477 return CMD_WARNING_CONFIG_FAILED
;
480 distance
= atoi(distance_str
);
482 /* Get OSPF6 distance node. */
483 rn
= route_node_get(o
->distance_table
, (struct prefix
*)&p
);
485 odistance
= rn
->info
;
486 route_unlock_node(rn
);
488 odistance
= ospf6_distance_new();
489 rn
->info
= odistance
;
492 /* Set distance value. */
493 odistance
->distance
= distance
;
495 /* Reset access-list configuration. */
496 if (odistance
->access_list
) {
497 free(odistance
->access_list
);
498 odistance
->access_list
= NULL
;
501 odistance
->access_list
= strdup(access_list_str
);
506 int ospf6_distance_unset(struct vty
*vty
, struct ospf6
*o
,
507 const char *distance_str
, const char *ip_str
,
508 const char *access_list_str
)
511 struct prefix_ipv6 p
;
512 struct route_node
*rn
;
513 struct ospf6_distance
*odistance
;
515 ret
= str2prefix_ipv6(ip_str
, &p
);
517 vty_out(vty
, "Malformed prefix\n");
518 return CMD_WARNING_CONFIG_FAILED
;
521 rn
= route_node_lookup(o
->distance_table
, (struct prefix
*)&p
);
523 vty_out(vty
, "Cant't find specified prefix\n");
524 return CMD_WARNING_CONFIG_FAILED
;
527 odistance
= rn
->info
;
529 if (odistance
->access_list
)
530 free(odistance
->access_list
);
531 ospf6_distance_free(odistance
);
534 route_unlock_node(rn
);
535 route_unlock_node(rn
);
540 void ospf6_distance_reset(struct ospf6
*o
)
542 struct route_node
*rn
;
543 struct ospf6_distance
*odistance
;
545 for (rn
= route_top(o
->distance_table
); rn
; rn
= route_next(rn
))
546 if ((odistance
= rn
->info
) != NULL
) {
547 if (odistance
->access_list
)
548 free(odistance
->access_list
);
549 ospf6_distance_free(odistance
);
551 route_unlock_node(rn
);
555 uint8_t ospf6_distance_apply(struct prefix_ipv6
*p
, struct ospf6_route
* or)
563 if (o
->distance_intra
)
564 if (or->path
.type
== OSPF6_PATH_TYPE_INTRA
)
565 return o
->distance_intra
;
567 if (o
->distance_inter
)
568 if (or->path
.type
== OSPF6_PATH_TYPE_INTER
)
569 return o
->distance_inter
;
571 if (o
->distance_external
)
572 if (or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
||
573 or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)
574 return o
->distance_external
;
577 return o
->distance_all
;
582 static void ospf6_zebra_connected(struct zclient
*zclient
)
584 /* Send the client registration */
585 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
587 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
590 void ospf6_zebra_init(struct thread_master
*master
)
592 /* Allocate zebra structure. */
593 zclient
= zclient_new(master
, &zclient_options_default
);
594 zclient_init(zclient
, ZEBRA_ROUTE_OSPF6
, 0, &ospf6d_privs
);
595 zclient
->zebra_connected
= ospf6_zebra_connected
;
596 zclient
->router_id_update
= ospf6_router_id_update_zebra
;
597 zclient
->interface_add
= ospf6_zebra_if_add
;
598 zclient
->interface_delete
= ospf6_zebra_if_del
;
599 zclient
->interface_up
= ospf6_zebra_if_state_update
;
600 zclient
->interface_down
= ospf6_zebra_if_state_update
;
601 zclient
->interface_address_add
= ospf6_zebra_if_address_update_add
;
602 zclient
->interface_address_delete
=
603 ospf6_zebra_if_address_update_delete
;
604 zclient
->redistribute_route_add
= ospf6_zebra_read_route
;
605 zclient
->redistribute_route_del
= ospf6_zebra_read_route
;
607 /* Install command element for zebra node. */
608 install_element(VIEW_NODE
, &show_ospf6_zebra_cmd
);
613 DEFUN (debug_ospf6_zebra_sendrecv
,
614 debug_ospf6_zebra_sendrecv_cmd
,
615 "debug ospf6 zebra [<send|recv>]",
618 "Debug connection between zebra\n"
619 "Debug Sending zebra\n"
620 "Debug Receiving zebra\n"
623 int idx_send_recv
= 3;
624 unsigned char level
= 0;
627 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
628 level
= OSPF6_DEBUG_ZEBRA_SEND
;
629 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
630 level
= OSPF6_DEBUG_ZEBRA_RECV
;
632 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
634 OSPF6_DEBUG_ZEBRA_ON(level
);
638 DEFUN (no_debug_ospf6_zebra_sendrecv
,
639 no_debug_ospf6_zebra_sendrecv_cmd
,
640 "no debug ospf6 zebra [<send|recv>]",
644 "Debug connection between zebra\n"
645 "Debug Sending zebra\n"
646 "Debug Receiving zebra\n"
649 int idx_send_recv
= 4;
650 unsigned char level
= 0;
653 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
654 level
= OSPF6_DEBUG_ZEBRA_SEND
;
655 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
656 level
= OSPF6_DEBUG_ZEBRA_RECV
;
658 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
660 OSPF6_DEBUG_ZEBRA_OFF(level
);
665 int config_write_ospf6_debug_zebra(struct vty
*vty
)
667 if (IS_OSPF6_DEBUG_ZEBRA(SEND
) && IS_OSPF6_DEBUG_ZEBRA(RECV
))
668 vty_out(vty
, "debug ospf6 zebra\n");
670 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
671 vty_out(vty
, "debug ospf6 zebra send\n");
672 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
673 vty_out(vty
, "debug ospf6 zebra recv\n");
678 void install_element_ospf6_debug_zebra(void)
680 install_element(ENABLE_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
681 install_element(ENABLE_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);
682 install_element(CONFIG_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
683 install_element(CONFIG_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);