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
);
131 /* XXX: ospf6_interface_if_del is not the right way to handle this,
132 * because among other thinkable issues, it will also clear all
133 * settings as they are contained in the struct ospf6_interface. */
134 ospf6_interface_if_del (ifp
);
137 if_set_index(ifp
, IFINDEX_INTERNAL
);
141 static int ospf6_zebra_if_state_update(int command
, struct zclient
*zclient
,
142 zebra_size_t length
, vrf_id_t vrf_id
)
144 struct interface
*ifp
;
146 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
150 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
152 "Zebra Interface state change: "
153 "%s index %d flags %llx metric %d mtu %d bandwidth %d",
154 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
155 ifp
->metric
, ifp
->mtu6
, ifp
->bandwidth
);
157 ospf6_interface_state_update(ifp
);
161 static int ospf6_zebra_if_address_update_add(int command
,
162 struct zclient
*zclient
,
169 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
170 zclient
->ibuf
, vrf_id
);
174 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
175 zlog_debug("Zebra Interface address add: %s %5s %s/%d",
176 c
->ifp
->name
, prefix_family_str(c
->address
),
177 inet_ntop(c
->address
->family
, &c
->address
->u
.prefix
,
179 c
->address
->prefixlen
);
181 if (c
->address
->family
== AF_INET6
) {
182 ospf6_interface_state_update(c
->ifp
);
183 ospf6_interface_connected_route_update(c
->ifp
);
188 static int ospf6_zebra_if_address_update_delete(int command
,
189 struct zclient
*zclient
,
196 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
197 zclient
->ibuf
, vrf_id
);
201 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
202 zlog_debug("Zebra Interface address delete: %s %5s %s/%d",
203 c
->ifp
->name
, prefix_family_str(c
->address
),
204 inet_ntop(c
->address
->family
, &c
->address
->u
.prefix
,
206 c
->address
->prefixlen
);
208 if (c
->address
->family
== AF_INET6
) {
209 ospf6_interface_connected_route_update(c
->ifp
);
210 ospf6_interface_state_update(c
->ifp
);
218 static int ospf6_zebra_read_route(int command
, struct zclient
*zclient
,
219 zebra_size_t length
, vrf_id_t vrf_id
)
221 struct zapi_route api
;
222 unsigned long ifindex
;
223 struct in6_addr
*nexthop
;
228 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
231 /* we completely ignore srcdest routes for now. */
232 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
235 ifindex
= api
.nexthops
[0].ifindex
;
236 nexthop
= &api
.nexthops
[0].gate
.ipv6
;
238 if (IS_OSPF6_DEBUG_ZEBRA(RECV
)) {
239 char prefixstr
[PREFIX2STR_BUFFER
], nexthopstr
[128];
240 prefix2str((struct prefix
*)&api
.prefix
, prefixstr
,
242 inet_ntop(AF_INET6
, nexthop
, nexthopstr
, sizeof(nexthopstr
));
245 "Zebra Receive route %s: %s %s nexthop %s ifindex %ld tag %" ROUTE_TAG_PRI
,
246 (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
? "add"
248 zebra_route_string(api
.type
), prefixstr
, nexthopstr
,
252 if (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
253 ospf6_asbr_redistribute_add(api
.type
, ifindex
, &api
.prefix
,
254 api
.nexthop_num
, nexthop
, api
.tag
);
256 ospf6_asbr_redistribute_remove(api
.type
, ifindex
, &api
.prefix
);
262 show_ospf6_zebra_cmd
,
263 "show ipv6 ospf6 zebra",
270 if (zclient
== NULL
) {
271 vty_out(vty
, "Not connected to zebra\n");
275 vty_out(vty
, "Zebra Infomation\n");
276 vty_out(vty
, " fail: %d\n", zclient
->fail
);
277 vty_out(vty
, " redistribute default: %d\n",
278 vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
));
279 vty_out(vty
, " redistribute:");
280 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
281 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][i
], VRF_DEFAULT
))
282 vty_out(vty
, " %s", zebra_route_string(i
));
290 static void ospf6_zebra_route_update(int type
, struct ospf6_route
*request
)
292 struct zapi_route api
;
293 char buf
[PREFIX2STR_BUFFER
];
298 if (IS_OSPF6_DEBUG_ZEBRA(SEND
)) {
299 prefix2str(&request
->prefix
, buf
, sizeof(buf
));
300 zlog_debug("Send %s route: %s",
301 (type
== REM
? "remove" : "add"), buf
);
304 if (zclient
->sock
< 0) {
305 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
306 zlog_debug(" Not connected to Zebra");
310 if (request
->path
.origin
.adv_router
== ospf6
->router_id
311 && (request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
312 || request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)) {
313 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
314 zlog_debug(" Ignore self-originated external route");
318 /* If removing is the best path and if there's another path,
319 treat this request as add the secondary path */
320 if (type
== REM
&& ospf6_route_is_best(request
) && request
->next
321 && ospf6_route_is_same(request
, request
->next
)) {
322 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
324 " Best-path removal resulted Sencondary addition");
326 request
= request
->next
;
329 /* Only the best path will be sent to zebra. */
330 if (!ospf6_route_is_best(request
)) {
331 /* this is not preferred best route, ignore */
332 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
333 zlog_debug(" Ignore non-best route");
337 nhcount
= ospf6_route_num_nexthops(request
);
339 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
340 zlog_debug(" No nexthop, ignore");
344 dest
= &request
->prefix
;
346 memset(&api
, 0, sizeof(api
));
347 api
.vrf_id
= VRF_DEFAULT
;
348 api
.type
= ZEBRA_ROUTE_OSPF6
;
349 api
.safi
= SAFI_UNICAST
;
351 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
352 api
.nexthop_num
= MIN(nhcount
, MULTIPATH_NUM
);
353 ospf6_route_zebra_copy_nexthops(request
, api
.nexthops
, api
.nexthop_num
);
354 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
355 api
.metric
= (request
->path
.metric_type
== 2 ? request
->path
.u
.cost_e2
356 : request
->path
.cost
);
357 if (request
->path
.tag
) {
358 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
359 api
.tag
= request
->path
.tag
;
362 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
364 ospf6_distance_apply((struct prefix_ipv6
*)dest
, request
);
367 ret
= zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
369 ret
= zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
372 zlog_err("zclient_route_send() %s failed: %s",
373 (type
== REM
? "delete" : "add"),
374 safe_strerror(errno
));
379 void ospf6_zebra_route_update_add(struct ospf6_route
*request
)
381 ospf6_zebra_route_update(ADD
, request
);
384 void ospf6_zebra_route_update_remove(struct ospf6_route
*request
)
386 ospf6_zebra_route_update(REM
, request
);
389 void ospf6_zebra_add_discard(struct ospf6_route
*request
)
391 struct zapi_route api
;
392 char buf
[INET6_ADDRSTRLEN
];
393 struct prefix
*dest
= &request
->prefix
;
395 if (!CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
396 memset(&api
, 0, sizeof(api
));
397 api
.vrf_id
= VRF_DEFAULT
;
398 api
.type
= ZEBRA_ROUTE_OSPF6
;
399 api
.safi
= SAFI_UNICAST
;
401 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
403 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
405 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
406 zlog_debug("Zebra: Route add discard %s/%d",
407 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
411 SET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
413 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
415 "Zebra: Blackhole route present already %s/%d",
416 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
422 void ospf6_zebra_delete_discard(struct ospf6_route
*request
)
424 struct zapi_route api
;
425 char buf
[INET6_ADDRSTRLEN
];
426 struct prefix
*dest
= &request
->prefix
;
428 if (CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
429 memset(&api
, 0, sizeof(api
));
430 api
.vrf_id
= VRF_DEFAULT
;
431 api
.type
= ZEBRA_ROUTE_OSPF6
;
432 api
.safi
= SAFI_UNICAST
;
434 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
436 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
438 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
439 zlog_debug("Zebra: Route delete discard %s/%d",
440 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
444 UNSET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
446 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
448 "Zebra: Blackhole route already deleted %s/%d",
449 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
455 static struct ospf6_distance
*ospf6_distance_new(void)
457 return XCALLOC(MTYPE_OSPF6_DISTANCE
, sizeof(struct ospf6_distance
));
460 static void ospf6_distance_free(struct ospf6_distance
*odistance
)
462 XFREE(MTYPE_OSPF6_DISTANCE
, odistance
);
465 int ospf6_distance_set(struct vty
*vty
, struct ospf6
*o
,
466 const char *distance_str
, const char *ip_str
,
467 const char *access_list_str
)
470 struct prefix_ipv6 p
;
472 struct route_node
*rn
;
473 struct ospf6_distance
*odistance
;
475 ret
= str2prefix_ipv6(ip_str
, &p
);
477 vty_out(vty
, "Malformed prefix\n");
478 return CMD_WARNING_CONFIG_FAILED
;
481 distance
= atoi(distance_str
);
483 /* Get OSPF6 distance node. */
484 rn
= route_node_get(o
->distance_table
, (struct prefix
*)&p
);
486 odistance
= rn
->info
;
487 route_unlock_node(rn
);
489 odistance
= ospf6_distance_new();
490 rn
->info
= odistance
;
493 /* Set distance value. */
494 odistance
->distance
= distance
;
496 /* Reset access-list configuration. */
497 if (odistance
->access_list
) {
498 free(odistance
->access_list
);
499 odistance
->access_list
= NULL
;
502 odistance
->access_list
= strdup(access_list_str
);
507 int ospf6_distance_unset(struct vty
*vty
, struct ospf6
*o
,
508 const char *distance_str
, const char *ip_str
,
509 const char *access_list_str
)
512 struct prefix_ipv6 p
;
513 struct route_node
*rn
;
514 struct ospf6_distance
*odistance
;
516 ret
= str2prefix_ipv6(ip_str
, &p
);
518 vty_out(vty
, "Malformed prefix\n");
519 return CMD_WARNING_CONFIG_FAILED
;
522 rn
= route_node_lookup(o
->distance_table
, (struct prefix
*)&p
);
524 vty_out(vty
, "Cant't find specified prefix\n");
525 return CMD_WARNING_CONFIG_FAILED
;
528 odistance
= rn
->info
;
530 if (odistance
->access_list
)
531 free(odistance
->access_list
);
532 ospf6_distance_free(odistance
);
535 route_unlock_node(rn
);
536 route_unlock_node(rn
);
541 void ospf6_distance_reset(struct ospf6
*o
)
543 struct route_node
*rn
;
544 struct ospf6_distance
*odistance
;
546 for (rn
= route_top(o
->distance_table
); rn
; rn
= route_next(rn
))
547 if ((odistance
= rn
->info
) != NULL
) {
548 if (odistance
->access_list
)
549 free(odistance
->access_list
);
550 ospf6_distance_free(odistance
);
552 route_unlock_node(rn
);
556 u_char
ospf6_distance_apply(struct prefix_ipv6
*p
, struct ospf6_route
* or)
564 if (o
->distance_intra
)
565 if (or->path
.type
== OSPF6_PATH_TYPE_INTRA
)
566 return o
->distance_intra
;
568 if (o
->distance_inter
)
569 if (or->path
.type
== OSPF6_PATH_TYPE_INTER
)
570 return o
->distance_inter
;
572 if (o
->distance_external
)
573 if (or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
||
574 or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)
575 return o
->distance_external
;
578 return o
->distance_all
;
583 static void ospf6_zebra_connected(struct zclient
*zclient
)
585 /* Send the client registration */
586 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
588 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
591 void ospf6_zebra_init(struct thread_master
*master
)
593 /* Allocate zebra structure. */
594 zclient
= zclient_new_notify(master
, &zclient_options_default
);
595 zclient_init(zclient
, ZEBRA_ROUTE_OSPF6
, 0, &ospf6d_privs
);
596 zclient
->zebra_connected
= ospf6_zebra_connected
;
597 zclient
->router_id_update
= ospf6_router_id_update_zebra
;
598 zclient
->interface_add
= ospf6_zebra_if_add
;
599 zclient
->interface_delete
= ospf6_zebra_if_del
;
600 zclient
->interface_up
= ospf6_zebra_if_state_update
;
601 zclient
->interface_down
= ospf6_zebra_if_state_update
;
602 zclient
->interface_address_add
= ospf6_zebra_if_address_update_add
;
603 zclient
->interface_address_delete
=
604 ospf6_zebra_if_address_update_delete
;
605 zclient
->redistribute_route_add
= ospf6_zebra_read_route
;
606 zclient
->redistribute_route_del
= ospf6_zebra_read_route
;
608 /* Install command element for zebra node. */
609 install_element(VIEW_NODE
, &show_ospf6_zebra_cmd
);
614 DEFUN (debug_ospf6_zebra_sendrecv
,
615 debug_ospf6_zebra_sendrecv_cmd
,
616 "debug ospf6 zebra [<send|recv>]",
619 "Debug connection between zebra\n"
620 "Debug Sending zebra\n"
621 "Debug Receiving zebra\n"
624 int idx_send_recv
= 3;
625 unsigned char level
= 0;
628 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
629 level
= OSPF6_DEBUG_ZEBRA_SEND
;
630 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
631 level
= OSPF6_DEBUG_ZEBRA_RECV
;
633 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
635 OSPF6_DEBUG_ZEBRA_ON(level
);
639 DEFUN (no_debug_ospf6_zebra_sendrecv
,
640 no_debug_ospf6_zebra_sendrecv_cmd
,
641 "no debug ospf6 zebra [<send|recv>]",
645 "Debug connection between zebra\n"
646 "Debug Sending zebra\n"
647 "Debug Receiving zebra\n"
650 int idx_send_recv
= 4;
651 unsigned char level
= 0;
654 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
655 level
= OSPF6_DEBUG_ZEBRA_SEND
;
656 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
657 level
= OSPF6_DEBUG_ZEBRA_RECV
;
659 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
661 OSPF6_DEBUG_ZEBRA_OFF(level
);
666 int config_write_ospf6_debug_zebra(struct vty
*vty
)
668 if (IS_OSPF6_DEBUG_ZEBRA(SEND
) && IS_OSPF6_DEBUG_ZEBRA(RECV
))
669 vty_out(vty
, "debug ospf6 zebra\n");
671 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
672 vty_out(vty
, "debug ospf6 zebra send\n");
673 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
674 vty_out(vty
, "debug ospf6 zebra recv\n");
679 void install_element_ospf6_debug_zebra(void)
681 install_element(ENABLE_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
682 install_element(ENABLE_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);
683 install_element(CONFIG_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
684 install_element(CONFIG_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);