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
32 #include "ospf6_proto.h"
33 #include "ospf6_top.h"
34 #include "ospf6_interface.h"
35 #include "ospf6_route.h"
36 #include "ospf6_lsa.h"
37 #include "ospf6_lsdb.h"
38 #include "ospf6_asbr.h"
39 #include "ospf6_zebra.h"
42 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_DISTANCE
, "OSPF6 distance")
44 unsigned char conf_debug_ospf6_zebra
= 0;
46 /* information about zebra. */
47 struct zclient
*zclient
= NULL
;
49 /* Router-id update message from zebra. */
50 static int ospf6_router_id_update_zebra(int command
, struct zclient
*zclient
,
51 zebra_size_t length
, vrf_id_t vrf_id
)
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",
69 inet_ntop(AF_INET
, &router_id
.u
.prefix4
,
70 buf
, INET_ADDRSTRLEN
));
73 ospf6_router_id_update();
78 /* redistribute function */
79 void ospf6_zebra_redistribute(int type
)
81 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
))
83 vrf_bitmap_set(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
);
85 if (zclient
->sock
> 0)
86 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
,
87 AFI_IP6
, type
, 0, VRF_DEFAULT
);
90 void ospf6_zebra_no_redistribute(int type
)
92 if (!vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
))
94 vrf_bitmap_unset(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
);
95 if (zclient
->sock
> 0)
96 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
,
97 AFI_IP6
, type
, 0, VRF_DEFAULT
);
100 /* Inteface addition message from zebra. */
101 static int ospf6_zebra_if_add(int command
, struct zclient
*zclient
,
102 zebra_size_t length
, vrf_id_t vrf_id
)
104 struct interface
*ifp
;
106 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
107 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
108 zlog_debug("Zebra Interface add: %s index %d mtu %d", ifp
->name
,
109 ifp
->ifindex
, ifp
->mtu6
);
110 ospf6_interface_if_add(ifp
);
114 static int ospf6_zebra_if_del(int command
, struct zclient
*zclient
,
115 zebra_size_t length
, vrf_id_t vrf_id
)
117 struct interface
*ifp
;
119 if (!(ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
)))
123 zlog_warn("Zebra: got delete of %s, but interface is still up",
126 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
127 zlog_debug("Zebra Interface delete: %s index %d mtu %d",
128 ifp
->name
, ifp
->ifindex
, ifp
->mtu6
);
130 if_set_index(ifp
, IFINDEX_INTERNAL
);
134 static int ospf6_zebra_if_state_update(int command
, struct zclient
*zclient
,
135 zebra_size_t length
, vrf_id_t vrf_id
)
137 struct interface
*ifp
;
139 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
143 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
145 "Zebra Interface state change: "
146 "%s index %d flags %llx metric %d mtu %d bandwidth %d",
147 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
148 ifp
->metric
, ifp
->mtu6
, ifp
->bandwidth
);
150 ospf6_interface_state_update(ifp
);
154 static int ospf6_zebra_if_address_update_add(int command
,
155 struct zclient
*zclient
,
162 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
163 zclient
->ibuf
, vrf_id
);
167 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
168 zlog_debug("Zebra Interface address add: %s %5s %s/%d",
169 c
->ifp
->name
, prefix_family_str(c
->address
),
170 inet_ntop(c
->address
->family
, &c
->address
->u
.prefix
,
172 c
->address
->prefixlen
);
174 if (c
->address
->family
== AF_INET6
) {
175 ospf6_interface_state_update(c
->ifp
);
176 ospf6_interface_connected_route_update(c
->ifp
);
181 static int ospf6_zebra_if_address_update_delete(int command
,
182 struct zclient
*zclient
,
189 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
190 zclient
->ibuf
, vrf_id
);
194 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
195 zlog_debug("Zebra Interface address delete: %s %5s %s/%d",
196 c
->ifp
->name
, prefix_family_str(c
->address
),
197 inet_ntop(c
->address
->family
, &c
->address
->u
.prefix
,
199 c
->address
->prefixlen
);
201 if (c
->address
->family
== AF_INET6
) {
202 ospf6_interface_connected_route_update(c
->ifp
);
203 ospf6_interface_state_update(c
->ifp
);
211 static int ospf6_zebra_read_route(int command
, struct zclient
*zclient
,
212 zebra_size_t length
, vrf_id_t vrf_id
)
214 struct zapi_route api
;
215 unsigned long ifindex
;
216 struct in6_addr
*nexthop
;
221 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
224 /* we completely ignore srcdest routes for now. */
225 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
228 ifindex
= api
.nexthops
[0].ifindex
;
229 nexthop
= &api
.nexthops
[0].gate
.ipv6
;
231 if (IS_OSPF6_DEBUG_ZEBRA(RECV
)) {
232 char prefixstr
[PREFIX2STR_BUFFER
], nexthopstr
[128];
233 prefix2str((struct prefix
*)&api
.prefix
, prefixstr
,
235 inet_ntop(AF_INET6
, nexthop
, nexthopstr
, sizeof(nexthopstr
));
238 "Zebra Receive route %s: %s %s nexthop %s ifindex %ld tag %" ROUTE_TAG_PRI
,
239 (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
? "add"
241 zebra_route_string(api
.type
), prefixstr
, nexthopstr
,
245 if (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
246 ospf6_asbr_redistribute_add(api
.type
, ifindex
, &api
.prefix
,
247 api
.nexthop_num
, nexthop
, api
.tag
);
249 ospf6_asbr_redistribute_remove(api
.type
, ifindex
, &api
.prefix
);
255 show_ospf6_zebra_cmd
,
256 "show ipv6 ospf6 zebra",
263 if (zclient
== NULL
) {
264 vty_out(vty
, "Not connected to zebra\n");
268 vty_out(vty
, "Zebra Infomation\n");
269 vty_out(vty
, " fail: %d\n", zclient
->fail
);
270 vty_out(vty
, " redistribute default: %d\n",
271 vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
));
272 vty_out(vty
, " redistribute:");
273 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
274 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][i
], VRF_DEFAULT
))
275 vty_out(vty
, " %s", zebra_route_string(i
));
283 static void ospf6_zebra_route_update(int type
, struct ospf6_route
*request
)
285 struct zapi_route api
;
286 char buf
[PREFIX2STR_BUFFER
];
291 if (IS_OSPF6_DEBUG_ZEBRA(SEND
)) {
292 prefix2str(&request
->prefix
, buf
, sizeof(buf
));
293 zlog_debug("Send %s route: %s",
294 (type
== REM
? "remove" : "add"), buf
);
297 if (zclient
->sock
< 0) {
298 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
299 zlog_debug(" Not connected to Zebra");
303 if (request
->path
.origin
.adv_router
== ospf6
->router_id
304 && (request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
305 || request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)) {
306 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
307 zlog_debug(" Ignore self-originated external route");
311 /* If removing is the best path and if there's another path,
312 treat this request as add the secondary path */
313 if (type
== REM
&& ospf6_route_is_best(request
) && request
->next
314 && ospf6_route_is_same(request
, request
->next
)) {
315 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
317 " Best-path removal resulted Sencondary addition");
319 request
= request
->next
;
322 /* Only the best path will be sent to zebra. */
323 if (!ospf6_route_is_best(request
)) {
324 /* this is not preferred best route, ignore */
325 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
326 zlog_debug(" Ignore non-best route");
330 nhcount
= ospf6_route_num_nexthops(request
);
332 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
333 zlog_debug(" No nexthop, ignore");
337 dest
= &request
->prefix
;
339 memset(&api
, 0, sizeof(api
));
340 api
.vrf_id
= VRF_DEFAULT
;
341 api
.type
= ZEBRA_ROUTE_OSPF6
;
342 api
.safi
= SAFI_UNICAST
;
344 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
345 api
.nexthop_num
= MIN(nhcount
, MULTIPATH_NUM
);
346 ospf6_route_zebra_copy_nexthops(request
, api
.nexthops
, api
.nexthop_num
);
347 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
348 api
.metric
= (request
->path
.metric_type
== 2 ? request
->path
.u
.cost_e2
349 : request
->path
.cost
);
350 if (request
->path
.tag
) {
351 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
352 api
.tag
= request
->path
.tag
;
355 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
357 ospf6_distance_apply((struct prefix_ipv6
*)dest
, request
);
360 ret
= zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
362 ret
= zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
365 zlog_err("zclient_route_send() %s failed: %s",
366 (type
== REM
? "delete" : "add"),
367 safe_strerror(errno
));
372 void ospf6_zebra_route_update_add(struct ospf6_route
*request
)
374 ospf6_zebra_route_update(ADD
, request
);
377 void ospf6_zebra_route_update_remove(struct ospf6_route
*request
)
379 ospf6_zebra_route_update(REM
, request
);
382 void ospf6_zebra_add_discard(struct ospf6_route
*request
)
384 struct zapi_route api
;
385 char buf
[INET6_ADDRSTRLEN
];
386 struct prefix
*dest
= &request
->prefix
;
388 if (!CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
389 memset(&api
, 0, sizeof(api
));
390 api
.vrf_id
= VRF_DEFAULT
;
391 api
.type
= ZEBRA_ROUTE_OSPF6
;
392 api
.safi
= SAFI_UNICAST
;
394 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
396 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
398 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
399 zlog_debug("Zebra: Route add discard %s/%d",
400 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
404 SET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
406 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
408 "Zebra: Blackhole route present already %s/%d",
409 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
415 void ospf6_zebra_delete_discard(struct ospf6_route
*request
)
417 struct zapi_route api
;
418 char buf
[INET6_ADDRSTRLEN
];
419 struct prefix
*dest
= &request
->prefix
;
421 if (CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
422 memset(&api
, 0, sizeof(api
));
423 api
.vrf_id
= VRF_DEFAULT
;
424 api
.type
= ZEBRA_ROUTE_OSPF6
;
425 api
.safi
= SAFI_UNICAST
;
427 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
429 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
431 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
432 zlog_debug("Zebra: Route delete discard %s/%d",
433 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
437 UNSET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
439 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
441 "Zebra: Blackhole route already deleted %s/%d",
442 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
448 static struct ospf6_distance
*ospf6_distance_new(void)
450 return XCALLOC(MTYPE_OSPF6_DISTANCE
, sizeof(struct ospf6_distance
));
453 static void ospf6_distance_free(struct ospf6_distance
*odistance
)
455 XFREE(MTYPE_OSPF6_DISTANCE
, odistance
);
458 int ospf6_distance_set(struct vty
*vty
, struct ospf6
*o
,
459 const char *distance_str
, const char *ip_str
,
460 const char *access_list_str
)
463 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 distance
= atoi(distance_str
);
476 /* Get OSPF6 distance node. */
477 rn
= route_node_get(o
->distance_table
, (struct prefix
*)&p
);
479 odistance
= rn
->info
;
480 route_unlock_node(rn
);
482 odistance
= ospf6_distance_new();
483 rn
->info
= odistance
;
486 /* Set distance value. */
487 odistance
->distance
= distance
;
489 /* Reset access-list configuration. */
490 if (odistance
->access_list
) {
491 free(odistance
->access_list
);
492 odistance
->access_list
= NULL
;
495 odistance
->access_list
= strdup(access_list_str
);
500 int ospf6_distance_unset(struct vty
*vty
, struct ospf6
*o
,
501 const char *distance_str
, const char *ip_str
,
502 const char *access_list_str
)
505 struct prefix_ipv6 p
;
506 struct route_node
*rn
;
507 struct ospf6_distance
*odistance
;
509 ret
= str2prefix_ipv6(ip_str
, &p
);
511 vty_out(vty
, "Malformed prefix\n");
512 return CMD_WARNING_CONFIG_FAILED
;
515 rn
= route_node_lookup(o
->distance_table
, (struct prefix
*)&p
);
517 vty_out(vty
, "Cant't find specified prefix\n");
518 return CMD_WARNING_CONFIG_FAILED
;
521 odistance
= rn
->info
;
523 if (odistance
->access_list
)
524 free(odistance
->access_list
);
525 ospf6_distance_free(odistance
);
528 route_unlock_node(rn
);
529 route_unlock_node(rn
);
534 void ospf6_distance_reset(struct ospf6
*o
)
536 struct route_node
*rn
;
537 struct ospf6_distance
*odistance
;
539 for (rn
= route_top(o
->distance_table
); rn
; rn
= route_next(rn
))
540 if ((odistance
= rn
->info
) != NULL
) {
541 if (odistance
->access_list
)
542 free(odistance
->access_list
);
543 ospf6_distance_free(odistance
);
545 route_unlock_node(rn
);
549 uint8_t ospf6_distance_apply(struct prefix_ipv6
*p
, struct ospf6_route
* or)
557 if (o
->distance_intra
)
558 if (or->path
.type
== OSPF6_PATH_TYPE_INTRA
)
559 return o
->distance_intra
;
561 if (o
->distance_inter
)
562 if (or->path
.type
== OSPF6_PATH_TYPE_INTER
)
563 return o
->distance_inter
;
565 if (o
->distance_external
)
566 if (or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
||
567 or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)
568 return o
->distance_external
;
571 return o
->distance_all
;
576 static void ospf6_zebra_connected(struct zclient
*zclient
)
578 /* Send the client registration */
579 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
581 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
584 void ospf6_zebra_init(struct thread_master
*master
)
586 /* Allocate zebra structure. */
587 zclient
= zclient_new_notify(master
, &zclient_options_default
);
588 zclient_init(zclient
, ZEBRA_ROUTE_OSPF6
, 0, &ospf6d_privs
);
589 zclient
->zebra_connected
= ospf6_zebra_connected
;
590 zclient
->router_id_update
= ospf6_router_id_update_zebra
;
591 zclient
->interface_add
= ospf6_zebra_if_add
;
592 zclient
->interface_delete
= ospf6_zebra_if_del
;
593 zclient
->interface_up
= ospf6_zebra_if_state_update
;
594 zclient
->interface_down
= ospf6_zebra_if_state_update
;
595 zclient
->interface_address_add
= ospf6_zebra_if_address_update_add
;
596 zclient
->interface_address_delete
=
597 ospf6_zebra_if_address_update_delete
;
598 zclient
->redistribute_route_add
= ospf6_zebra_read_route
;
599 zclient
->redistribute_route_del
= ospf6_zebra_read_route
;
601 /* Install command element for zebra node. */
602 install_element(VIEW_NODE
, &show_ospf6_zebra_cmd
);
607 DEFUN (debug_ospf6_zebra_sendrecv
,
608 debug_ospf6_zebra_sendrecv_cmd
,
609 "debug ospf6 zebra [<send|recv>]",
612 "Debug connection between zebra\n"
613 "Debug Sending zebra\n"
614 "Debug Receiving zebra\n"
617 int idx_send_recv
= 3;
618 unsigned char level
= 0;
621 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
622 level
= OSPF6_DEBUG_ZEBRA_SEND
;
623 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
624 level
= OSPF6_DEBUG_ZEBRA_RECV
;
626 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
628 OSPF6_DEBUG_ZEBRA_ON(level
);
632 DEFUN (no_debug_ospf6_zebra_sendrecv
,
633 no_debug_ospf6_zebra_sendrecv_cmd
,
634 "no debug ospf6 zebra [<send|recv>]",
638 "Debug connection between zebra\n"
639 "Debug Sending zebra\n"
640 "Debug Receiving zebra\n"
643 int idx_send_recv
= 4;
644 unsigned char level
= 0;
647 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
648 level
= OSPF6_DEBUG_ZEBRA_SEND
;
649 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
650 level
= OSPF6_DEBUG_ZEBRA_RECV
;
652 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
654 OSPF6_DEBUG_ZEBRA_OFF(level
);
659 int config_write_ospf6_debug_zebra(struct vty
*vty
)
661 if (IS_OSPF6_DEBUG_ZEBRA(SEND
) && IS_OSPF6_DEBUG_ZEBRA(RECV
))
662 vty_out(vty
, "debug ospf6 zebra\n");
664 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
665 vty_out(vty
, "debug ospf6 zebra send\n");
666 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
667 vty_out(vty
, "debug ospf6 zebra recv\n");
672 void install_element_ospf6_debug_zebra(void)
674 install_element(ENABLE_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
675 install_element(ENABLE_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);
676 install_element(CONFIG_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
677 install_element(CONFIG_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);