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
);
61 o
->router_id_zebra
= router_id
.u
.prefix4
;
63 if (o
->router_id
== 0)
64 o
->router_id
= (uint32_t)o
->router_id_zebra
.s_addr
;
69 /* redistribute function */
70 void ospf6_zebra_redistribute(int type
)
72 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
))
74 vrf_bitmap_set(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
);
76 if (zclient
->sock
> 0)
77 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
,
78 AFI_IP6
, type
, 0, VRF_DEFAULT
);
81 void ospf6_zebra_no_redistribute(int type
)
83 if (!vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
))
85 vrf_bitmap_unset(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
);
86 if (zclient
->sock
> 0)
87 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
,
88 AFI_IP6
, type
, 0, VRF_DEFAULT
);
91 /* Inteface addition message from zebra. */
92 static int ospf6_zebra_if_add(int command
, struct zclient
*zclient
,
93 zebra_size_t length
, vrf_id_t vrf_id
)
95 struct interface
*ifp
;
97 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
98 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
99 zlog_debug("Zebra Interface add: %s index %d mtu %d", ifp
->name
,
100 ifp
->ifindex
, ifp
->mtu6
);
101 ospf6_interface_if_add(ifp
);
105 static int ospf6_zebra_if_del(int command
, struct zclient
*zclient
,
106 zebra_size_t length
, vrf_id_t vrf_id
)
108 struct interface
*ifp
;
110 if (!(ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
)))
114 zlog_warn("Zebra: got delete of %s, but interface is still up",
117 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
118 zlog_debug("Zebra Interface delete: %s index %d mtu %d",
119 ifp
->name
, ifp
->ifindex
, ifp
->mtu6
);
122 /* XXX: ospf6_interface_if_del is not the right way to handle this,
123 * because among other thinkable issues, it will also clear all
124 * settings as they are contained in the struct ospf6_interface. */
125 ospf6_interface_if_del (ifp
);
128 if_set_index(ifp
, IFINDEX_INTERNAL
);
132 static int ospf6_zebra_if_state_update(int command
, struct zclient
*zclient
,
133 zebra_size_t length
, vrf_id_t vrf_id
)
135 struct interface
*ifp
;
137 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
141 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
143 "Zebra Interface state change: "
144 "%s index %d flags %llx metric %d mtu %d bandwidth %d",
145 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
146 ifp
->metric
, ifp
->mtu6
, ifp
->bandwidth
);
148 ospf6_interface_state_update(ifp
);
152 static int ospf6_zebra_if_address_update_add(int command
,
153 struct zclient
*zclient
,
160 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
161 zclient
->ibuf
, vrf_id
);
165 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
166 zlog_debug("Zebra Interface address add: %s %5s %s/%d",
167 c
->ifp
->name
, prefix_family_str(c
->address
),
168 inet_ntop(c
->address
->family
, &c
->address
->u
.prefix
,
170 c
->address
->prefixlen
);
172 if (c
->address
->family
== AF_INET6
) {
173 ospf6_interface_state_update(c
->ifp
);
174 ospf6_interface_connected_route_update(c
->ifp
);
179 static int ospf6_zebra_if_address_update_delete(int command
,
180 struct zclient
*zclient
,
187 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
188 zclient
->ibuf
, vrf_id
);
192 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
193 zlog_debug("Zebra Interface address delete: %s %5s %s/%d",
194 c
->ifp
->name
, prefix_family_str(c
->address
),
195 inet_ntop(c
->address
->family
, &c
->address
->u
.prefix
,
197 c
->address
->prefixlen
);
199 if (c
->address
->family
== AF_INET6
) {
200 ospf6_interface_connected_route_update(c
->ifp
);
201 ospf6_interface_state_update(c
->ifp
);
209 static int ospf6_zebra_read_route(int command
, struct zclient
*zclient
,
210 zebra_size_t length
, vrf_id_t vrf_id
)
212 struct zapi_route api
;
213 unsigned long ifindex
;
214 struct in6_addr
*nexthop
;
219 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
222 /* we completely ignore srcdest routes for now. */
223 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
226 ifindex
= api
.nexthops
[0].ifindex
;
227 nexthop
= &api
.nexthops
[0].gate
.ipv6
;
229 if (IS_OSPF6_DEBUG_ZEBRA(RECV
)) {
230 char prefixstr
[PREFIX2STR_BUFFER
], nexthopstr
[128];
231 prefix2str((struct prefix
*)&api
.prefix
, prefixstr
,
233 inet_ntop(AF_INET6
, nexthop
, nexthopstr
, sizeof(nexthopstr
));
236 "Zebra Receive route %s: %s %s nexthop %s ifindex %ld tag %" ROUTE_TAG_PRI
,
237 (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
? "add"
239 zebra_route_string(api
.type
), prefixstr
, nexthopstr
,
243 if (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
244 ospf6_asbr_redistribute_add(api
.type
, ifindex
, &api
.prefix
,
245 api
.nexthop_num
, nexthop
, api
.tag
);
247 ospf6_asbr_redistribute_remove(api
.type
, ifindex
, &api
.prefix
);
253 show_ospf6_zebra_cmd
,
254 "show ipv6 ospf6 zebra",
261 if (zclient
== NULL
) {
262 vty_out(vty
, "Not connected to zebra\n");
266 vty_out(vty
, "Zebra Infomation\n");
267 vty_out(vty
, " fail: %d\n", zclient
->fail
);
268 vty_out(vty
, " redistribute default: %d\n",
269 vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
));
270 vty_out(vty
, " redistribute:");
271 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
272 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][i
], VRF_DEFAULT
))
273 vty_out(vty
, " %s", zebra_route_string(i
));
281 static void ospf6_zebra_route_update(int type
, struct ospf6_route
*request
)
283 struct zapi_route api
;
284 char buf
[PREFIX2STR_BUFFER
];
289 if (IS_OSPF6_DEBUG_ZEBRA(SEND
)) {
290 prefix2str(&request
->prefix
, buf
, sizeof(buf
));
291 zlog_debug("Send %s route: %s",
292 (type
== REM
? "remove" : "add"), buf
);
295 if (zclient
->sock
< 0) {
296 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
297 zlog_debug(" Not connected to Zebra");
301 if (request
->path
.origin
.adv_router
== ospf6
->router_id
302 && (request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
303 || request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)) {
304 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
305 zlog_debug(" Ignore self-originated external route");
309 /* If removing is the best path and if there's another path,
310 treat this request as add the secondary path */
311 if (type
== REM
&& ospf6_route_is_best(request
) && request
->next
312 && ospf6_route_is_same(request
, request
->next
)) {
313 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
315 " Best-path removal resulted Sencondary addition");
317 request
= request
->next
;
320 /* Only the best path will be sent to zebra. */
321 if (!ospf6_route_is_best(request
)) {
322 /* this is not preferred best route, ignore */
323 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
324 zlog_debug(" Ignore non-best route");
328 nhcount
= ospf6_route_num_nexthops(request
);
330 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
331 zlog_debug(" No nexthop, ignore");
335 dest
= &request
->prefix
;
337 memset(&api
, 0, sizeof(api
));
338 api
.vrf_id
= VRF_DEFAULT
;
339 api
.type
= ZEBRA_ROUTE_OSPF6
;
340 api
.safi
= SAFI_UNICAST
;
342 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
343 api
.nexthop_num
= MIN(nhcount
, MULTIPATH_NUM
);
344 ospf6_route_zebra_copy_nexthops(request
, api
.nexthops
, api
.nexthop_num
);
345 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
346 api
.metric
= (request
->path
.metric_type
== 2 ? request
->path
.u
.cost_e2
347 : request
->path
.cost
);
348 if (request
->path
.tag
) {
349 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
350 api
.tag
= request
->path
.tag
;
353 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
355 ospf6_distance_apply((struct prefix_ipv6
*)dest
, request
);
358 ret
= zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
360 ret
= zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
363 zlog_err("zclient_route_send() %s failed: %s",
364 (type
== REM
? "delete" : "add"),
365 safe_strerror(errno
));
370 void ospf6_zebra_route_update_add(struct ospf6_route
*request
)
372 ospf6_zebra_route_update(ADD
, request
);
375 void ospf6_zebra_route_update_remove(struct ospf6_route
*request
)
377 ospf6_zebra_route_update(REM
, request
);
380 void ospf6_zebra_add_discard(struct ospf6_route
*request
)
382 struct zapi_route api
;
383 char buf
[INET6_ADDRSTRLEN
];
384 struct prefix
*dest
= &request
->prefix
;
386 if (!CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
387 memset(&api
, 0, sizeof(api
));
388 api
.vrf_id
= VRF_DEFAULT
;
389 api
.type
= ZEBRA_ROUTE_OSPF6
;
390 api
.safi
= SAFI_UNICAST
;
392 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
394 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
396 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
397 zlog_debug("Zebra: Route add discard %s/%d",
398 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
402 SET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
404 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
406 "Zebra: Blackhole route present already %s/%d",
407 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
413 void ospf6_zebra_delete_discard(struct ospf6_route
*request
)
415 struct zapi_route api
;
416 char buf
[INET6_ADDRSTRLEN
];
417 struct prefix
*dest
= &request
->prefix
;
419 if (CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
420 memset(&api
, 0, sizeof(api
));
421 api
.vrf_id
= VRF_DEFAULT
;
422 api
.type
= ZEBRA_ROUTE_OSPF6
;
423 api
.safi
= SAFI_UNICAST
;
425 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
427 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
429 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
430 zlog_debug("Zebra: Route delete discard %s/%d",
431 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
435 UNSET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
437 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
439 "Zebra: Blackhole route already deleted %s/%d",
440 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
446 static struct ospf6_distance
*ospf6_distance_new(void)
448 return XCALLOC(MTYPE_OSPF6_DISTANCE
, sizeof(struct ospf6_distance
));
451 static void ospf6_distance_free(struct ospf6_distance
*odistance
)
453 XFREE(MTYPE_OSPF6_DISTANCE
, odistance
);
456 int ospf6_distance_set(struct vty
*vty
, struct ospf6
*o
,
457 const char *distance_str
, const char *ip_str
,
458 const char *access_list_str
)
461 struct prefix_ipv6 p
;
463 struct route_node
*rn
;
464 struct ospf6_distance
*odistance
;
466 ret
= str2prefix_ipv6(ip_str
, &p
);
468 vty_out(vty
, "Malformed prefix\n");
469 return CMD_WARNING_CONFIG_FAILED
;
472 distance
= atoi(distance_str
);
474 /* Get OSPF6 distance node. */
475 rn
= route_node_get(o
->distance_table
, (struct prefix
*)&p
);
477 odistance
= rn
->info
;
478 route_unlock_node(rn
);
480 odistance
= ospf6_distance_new();
481 rn
->info
= odistance
;
484 /* Set distance value. */
485 odistance
->distance
= distance
;
487 /* Reset access-list configuration. */
488 if (odistance
->access_list
) {
489 free(odistance
->access_list
);
490 odistance
->access_list
= NULL
;
493 odistance
->access_list
= strdup(access_list_str
);
498 int ospf6_distance_unset(struct vty
*vty
, struct ospf6
*o
,
499 const char *distance_str
, const char *ip_str
,
500 const char *access_list_str
)
503 struct prefix_ipv6 p
;
504 struct route_node
*rn
;
505 struct ospf6_distance
*odistance
;
507 ret
= str2prefix_ipv6(ip_str
, &p
);
509 vty_out(vty
, "Malformed prefix\n");
510 return CMD_WARNING_CONFIG_FAILED
;
513 rn
= route_node_lookup(o
->distance_table
, (struct prefix
*)&p
);
515 vty_out(vty
, "Cant't find specified prefix\n");
516 return CMD_WARNING_CONFIG_FAILED
;
519 odistance
= rn
->info
;
521 if (odistance
->access_list
)
522 free(odistance
->access_list
);
523 ospf6_distance_free(odistance
);
526 route_unlock_node(rn
);
527 route_unlock_node(rn
);
532 void ospf6_distance_reset(struct ospf6
*o
)
534 struct route_node
*rn
;
535 struct ospf6_distance
*odistance
;
537 for (rn
= route_top(o
->distance_table
); rn
; rn
= route_next(rn
))
538 if ((odistance
= rn
->info
) != NULL
) {
539 if (odistance
->access_list
)
540 free(odistance
->access_list
);
541 ospf6_distance_free(odistance
);
543 route_unlock_node(rn
);
547 u_char
ospf6_distance_apply(struct prefix_ipv6
*p
, struct ospf6_route
* or)
555 if (o
->distance_intra
)
556 if (or->path
.type
== OSPF6_PATH_TYPE_INTRA
)
557 return o
->distance_intra
;
559 if (o
->distance_inter
)
560 if (or->path
.type
== OSPF6_PATH_TYPE_INTER
)
561 return o
->distance_inter
;
563 if (o
->distance_external
)
564 if (or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
||
565 or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)
566 return o
->distance_external
;
569 return o
->distance_all
;
574 static void ospf6_zebra_connected(struct zclient
*zclient
)
576 /* Send the client registration */
577 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
579 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
582 void ospf6_zebra_init(struct thread_master
*master
)
584 /* Allocate zebra structure. */
585 zclient
= zclient_new_notify(master
, &zclient_options_default
);
586 zclient_init(zclient
, ZEBRA_ROUTE_OSPF6
, 0, &ospf6d_privs
);
587 zclient
->zebra_connected
= ospf6_zebra_connected
;
588 zclient
->router_id_update
= ospf6_router_id_update_zebra
;
589 zclient
->interface_add
= ospf6_zebra_if_add
;
590 zclient
->interface_delete
= ospf6_zebra_if_del
;
591 zclient
->interface_up
= ospf6_zebra_if_state_update
;
592 zclient
->interface_down
= ospf6_zebra_if_state_update
;
593 zclient
->interface_address_add
= ospf6_zebra_if_address_update_add
;
594 zclient
->interface_address_delete
=
595 ospf6_zebra_if_address_update_delete
;
596 zclient
->redistribute_route_add
= ospf6_zebra_read_route
;
597 zclient
->redistribute_route_del
= ospf6_zebra_read_route
;
599 /* Install command element for zebra node. */
600 install_element(VIEW_NODE
, &show_ospf6_zebra_cmd
);
605 DEFUN (debug_ospf6_zebra_sendrecv
,
606 debug_ospf6_zebra_sendrecv_cmd
,
607 "debug ospf6 zebra [<send|recv>]",
610 "Debug connection between zebra\n"
611 "Debug Sending zebra\n"
612 "Debug Receiving zebra\n"
615 int idx_send_recv
= 3;
616 unsigned char level
= 0;
619 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
620 level
= OSPF6_DEBUG_ZEBRA_SEND
;
621 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
622 level
= OSPF6_DEBUG_ZEBRA_RECV
;
624 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
626 OSPF6_DEBUG_ZEBRA_ON(level
);
630 DEFUN (no_debug_ospf6_zebra_sendrecv
,
631 no_debug_ospf6_zebra_sendrecv_cmd
,
632 "no debug ospf6 zebra [<send|recv>]",
636 "Debug connection between zebra\n"
637 "Debug Sending zebra\n"
638 "Debug Receiving zebra\n"
641 int idx_send_recv
= 4;
642 unsigned char level
= 0;
645 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
646 level
= OSPF6_DEBUG_ZEBRA_SEND
;
647 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
648 level
= OSPF6_DEBUG_ZEBRA_RECV
;
650 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
652 OSPF6_DEBUG_ZEBRA_OFF(level
);
657 int config_write_ospf6_debug_zebra(struct vty
*vty
)
659 if (IS_OSPF6_DEBUG_ZEBRA(SEND
) && IS_OSPF6_DEBUG_ZEBRA(RECV
))
660 vty_out(vty
, "debug ospf6 zebra\n");
662 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
663 vty_out(vty
, "debug ospf6 zebra send\n");
664 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
665 vty_out(vty
, "debug ospf6 zebra recv\n");
670 void install_element_ospf6_debug_zebra(void)
672 install_element(ENABLE_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
673 install_element(ENABLE_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);
674 install_element(CONFIG_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
675 install_element(CONFIG_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);