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 struct in_addr router_id_zebra
;
51 /* Router-id update message from zebra. */
52 static int ospf6_router_id_update_zebra(int command
, struct zclient
*zclient
,
53 zebra_size_t length
, vrf_id_t vrf_id
)
55 struct prefix router_id
;
56 struct ospf6
*o
= ospf6
;
58 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
59 router_id_zebra
= router_id
.u
.prefix4
;
64 if (o
->router_id
== 0)
65 o
->router_id
= (u_int32_t
)router_id_zebra
.s_addr
;
70 /* redistribute function */
71 void ospf6_zebra_redistribute(int type
)
73 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
))
75 vrf_bitmap_set(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
);
77 if (zclient
->sock
> 0)
78 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
,
79 AFI_IP6
, type
, 0, VRF_DEFAULT
);
82 void ospf6_zebra_no_redistribute(int type
)
84 if (!vrf_bitmap_check(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
))
86 vrf_bitmap_unset(zclient
->redist
[AFI_IP6
][type
], VRF_DEFAULT
);
87 if (zclient
->sock
> 0)
88 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
,
89 AFI_IP6
, type
, 0, VRF_DEFAULT
);
92 /* Inteface addition message from zebra. */
93 static int ospf6_zebra_if_add(int command
, struct zclient
*zclient
,
94 zebra_size_t length
, vrf_id_t vrf_id
)
96 struct interface
*ifp
;
98 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
99 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
100 zlog_debug("Zebra Interface add: %s index %d mtu %d", ifp
->name
,
101 ifp
->ifindex
, ifp
->mtu6
);
102 ospf6_interface_if_add(ifp
);
106 static int ospf6_zebra_if_del(int command
, struct zclient
*zclient
,
107 zebra_size_t length
, vrf_id_t vrf_id
)
109 struct interface
*ifp
;
111 if (!(ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
)))
115 zlog_warn("Zebra: got delete of %s, but interface is still up",
118 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
119 zlog_debug("Zebra Interface delete: %s index %d mtu %d",
120 ifp
->name
, ifp
->ifindex
, ifp
->mtu6
);
123 /* XXX: ospf6_interface_if_del is not the right way to handle this,
124 * because among other thinkable issues, it will also clear all
125 * settings as they are contained in the struct ospf6_interface. */
126 ospf6_interface_if_del (ifp
);
129 if_set_index(ifp
, IFINDEX_INTERNAL
);
133 static int ospf6_zebra_if_state_update(int command
, struct zclient
*zclient
,
134 zebra_size_t length
, vrf_id_t vrf_id
)
136 struct interface
*ifp
;
138 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
142 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
144 "Zebra Interface state change: "
145 "%s index %d flags %llx metric %d mtu %d bandwidth %d",
146 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
,
147 ifp
->metric
, ifp
->mtu6
, ifp
->bandwidth
);
149 ospf6_interface_state_update(ifp
);
153 static int ospf6_zebra_if_address_update_add(int command
,
154 struct zclient
*zclient
,
161 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD
,
162 zclient
->ibuf
, vrf_id
);
166 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
167 zlog_debug("Zebra Interface address add: %s %5s %s/%d",
168 c
->ifp
->name
, prefix_family_str(c
->address
),
169 inet_ntop(c
->address
->family
, &c
->address
->u
.prefix
,
171 c
->address
->prefixlen
);
173 if (c
->address
->family
== AF_INET6
) {
174 ospf6_interface_state_update(c
->ifp
);
175 ospf6_interface_connected_route_update(c
->ifp
);
180 static int ospf6_zebra_if_address_update_delete(int command
,
181 struct zclient
*zclient
,
188 c
= zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE
,
189 zclient
->ibuf
, vrf_id
);
193 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
194 zlog_debug("Zebra Interface address delete: %s %5s %s/%d",
195 c
->ifp
->name
, prefix_family_str(c
->address
),
196 inet_ntop(c
->address
->family
, &c
->address
->u
.prefix
,
198 c
->address
->prefixlen
);
200 if (c
->address
->family
== AF_INET6
) {
201 ospf6_interface_connected_route_update(c
->ifp
);
202 ospf6_interface_state_update(c
->ifp
);
210 static int ospf6_zebra_read_route(int command
, struct zclient
*zclient
,
211 zebra_size_t length
, vrf_id_t vrf_id
)
213 struct zapi_route api
;
214 unsigned long ifindex
;
215 struct in6_addr
*nexthop
;
220 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
223 /* we completely ignore srcdest routes for now. */
224 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
227 ifindex
= api
.nexthops
[0].ifindex
;
228 nexthop
= &api
.nexthops
[0].gate
.ipv6
;
230 if (IS_OSPF6_DEBUG_ZEBRA(RECV
)) {
231 char prefixstr
[PREFIX2STR_BUFFER
], nexthopstr
[128];
232 prefix2str((struct prefix
*)&api
.prefix
, prefixstr
,
234 inet_ntop(AF_INET6
, nexthop
, nexthopstr
, sizeof(nexthopstr
));
237 "Zebra Receive route %s: %s %s nexthop %s ifindex %ld tag %" ROUTE_TAG_PRI
,
238 (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
? "add"
240 zebra_route_string(api
.type
), prefixstr
, nexthopstr
,
244 if (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
245 ospf6_asbr_redistribute_add(api
.type
, ifindex
, &api
.prefix
,
246 api
.nexthop_num
, nexthop
, api
.tag
);
248 ospf6_asbr_redistribute_remove(api
.type
, ifindex
, &api
.prefix
);
254 show_ospf6_zebra_cmd
,
255 "show ipv6 ospf6 zebra",
262 if (zclient
== NULL
) {
263 vty_out(vty
, "Not connected to zebra\n");
267 vty_out(vty
, "Zebra Infomation\n");
268 vty_out(vty
, " fail: %d\n", zclient
->fail
);
269 vty_out(vty
, " redistribute default: %d\n",
270 vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
));
271 vty_out(vty
, " redistribute:");
272 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
273 if (vrf_bitmap_check(zclient
->redist
[AFI_IP6
][i
], VRF_DEFAULT
))
274 vty_out(vty
, " %s", zebra_route_string(i
));
282 static void ospf6_zebra_route_update(int type
, struct ospf6_route
*request
)
284 struct zapi_route api
;
285 char buf
[PREFIX2STR_BUFFER
];
290 if (IS_OSPF6_DEBUG_ZEBRA(SEND
)) {
291 prefix2str(&request
->prefix
, buf
, sizeof(buf
));
292 zlog_debug("Send %s route: %s",
293 (type
== REM
? "remove" : "add"), buf
);
296 if (zclient
->sock
< 0) {
297 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
298 zlog_debug(" Not connected to Zebra");
302 if (request
->path
.origin
.adv_router
== ospf6
->router_id
303 && (request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
304 || request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)) {
305 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
306 zlog_debug(" Ignore self-originated external route");
310 /* If removing is the best path and if there's another path,
311 treat this request as add the secondary path */
312 if (type
== REM
&& ospf6_route_is_best(request
) && request
->next
313 && ospf6_route_is_same(request
, request
->next
)) {
314 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
316 " Best-path removal resulted Sencondary addition");
318 request
= request
->next
;
321 /* Only the best path will be sent to zebra. */
322 if (!ospf6_route_is_best(request
)) {
323 /* this is not preferred best route, ignore */
324 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
325 zlog_debug(" Ignore non-best route");
329 nhcount
= ospf6_route_num_nexthops(request
);
331 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
332 zlog_debug(" No nexthop, ignore");
336 dest
= &request
->prefix
;
338 memset(&api
, 0, sizeof(api
));
339 api
.vrf_id
= VRF_DEFAULT
;
340 api
.nh_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
.nh_vrf_id
= VRF_DEFAULT
;
392 api
.type
= ZEBRA_ROUTE_OSPF6
;
393 api
.safi
= SAFI_UNICAST
;
395 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
397 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
399 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
400 zlog_debug("Zebra: Route add discard %s/%d",
401 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
405 SET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
407 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
409 "Zebra: Blackhole route present already %s/%d",
410 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
416 void ospf6_zebra_delete_discard(struct ospf6_route
*request
)
418 struct zapi_route api
;
419 char buf
[INET6_ADDRSTRLEN
];
420 struct prefix
*dest
= &request
->prefix
;
422 if (CHECK_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
)) {
423 memset(&api
, 0, sizeof(api
));
424 api
.vrf_id
= VRF_DEFAULT
;
425 api
.nh_vrf_id
= VRF_DEFAULT
;
426 api
.type
= ZEBRA_ROUTE_OSPF6
;
427 api
.safi
= SAFI_UNICAST
;
429 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
431 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
433 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
434 zlog_debug("Zebra: Route delete discard %s/%d",
435 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
439 UNSET_FLAG(request
->flag
, OSPF6_ROUTE_BLACKHOLE_ADDED
);
441 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
443 "Zebra: Blackhole route already deleted %s/%d",
444 inet_ntop(AF_INET6
, &dest
->u
.prefix6
, buf
,
450 static struct ospf6_distance
*ospf6_distance_new(void)
452 return XCALLOC(MTYPE_OSPF6_DISTANCE
, sizeof(struct ospf6_distance
));
455 static void ospf6_distance_free(struct ospf6_distance
*odistance
)
457 XFREE(MTYPE_OSPF6_DISTANCE
, odistance
);
460 int ospf6_distance_set(struct vty
*vty
, struct ospf6
*o
,
461 const char *distance_str
, const char *ip_str
,
462 const char *access_list_str
)
465 struct prefix_ipv6 p
;
467 struct route_node
*rn
;
468 struct ospf6_distance
*odistance
;
470 ret
= str2prefix_ipv6(ip_str
, &p
);
472 vty_out(vty
, "Malformed prefix\n");
473 return CMD_WARNING_CONFIG_FAILED
;
476 distance
= atoi(distance_str
);
478 /* Get OSPF6 distance node. */
479 rn
= route_node_get(o
->distance_table
, (struct prefix
*)&p
);
481 odistance
= rn
->info
;
482 route_unlock_node(rn
);
484 odistance
= ospf6_distance_new();
485 rn
->info
= odistance
;
488 /* Set distance value. */
489 odistance
->distance
= distance
;
491 /* Reset access-list configuration. */
492 if (odistance
->access_list
) {
493 free(odistance
->access_list
);
494 odistance
->access_list
= NULL
;
497 odistance
->access_list
= strdup(access_list_str
);
502 int ospf6_distance_unset(struct vty
*vty
, struct ospf6
*o
,
503 const char *distance_str
, const char *ip_str
,
504 const char *access_list_str
)
507 struct prefix_ipv6 p
;
508 struct route_node
*rn
;
509 struct ospf6_distance
*odistance
;
511 ret
= str2prefix_ipv6(ip_str
, &p
);
513 vty_out(vty
, "Malformed prefix\n");
514 return CMD_WARNING_CONFIG_FAILED
;
517 rn
= route_node_lookup(o
->distance_table
, (struct prefix
*)&p
);
519 vty_out(vty
, "Cant't find specified prefix\n");
520 return CMD_WARNING_CONFIG_FAILED
;
523 odistance
= rn
->info
;
525 if (odistance
->access_list
)
526 free(odistance
->access_list
);
527 ospf6_distance_free(odistance
);
530 route_unlock_node(rn
);
531 route_unlock_node(rn
);
536 void ospf6_distance_reset(struct ospf6
*o
)
538 struct route_node
*rn
;
539 struct ospf6_distance
*odistance
;
541 for (rn
= route_top(o
->distance_table
); rn
; rn
= route_next(rn
))
542 if ((odistance
= rn
->info
) != NULL
) {
543 if (odistance
->access_list
)
544 free(odistance
->access_list
);
545 ospf6_distance_free(odistance
);
547 route_unlock_node(rn
);
551 u_char
ospf6_distance_apply(struct prefix_ipv6
*p
, struct ospf6_route
* or)
559 if (o
->distance_intra
)
560 if (or->path
.type
== OSPF6_PATH_TYPE_INTRA
)
561 return o
->distance_intra
;
563 if (o
->distance_inter
)
564 if (or->path
.type
== OSPF6_PATH_TYPE_INTER
)
565 return o
->distance_inter
;
567 if (o
->distance_external
)
568 if (or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
||
569 or->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
)
570 return o
->distance_external
;
573 return o
->distance_all
;
578 static void ospf6_zebra_connected(struct zclient
*zclient
)
580 /* Send the client registration */
581 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
583 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
586 void ospf6_zebra_init(struct thread_master
*master
)
588 /* Allocate zebra structure. */
589 zclient
= zclient_new_notify(master
, &zclient_options_default
);
590 zclient_init(zclient
, ZEBRA_ROUTE_OSPF6
, 0, &ospf6d_privs
);
591 zclient
->zebra_connected
= ospf6_zebra_connected
;
592 zclient
->router_id_update
= ospf6_router_id_update_zebra
;
593 zclient
->interface_add
= ospf6_zebra_if_add
;
594 zclient
->interface_delete
= ospf6_zebra_if_del
;
595 zclient
->interface_up
= ospf6_zebra_if_state_update
;
596 zclient
->interface_down
= ospf6_zebra_if_state_update
;
597 zclient
->interface_address_add
= ospf6_zebra_if_address_update_add
;
598 zclient
->interface_address_delete
=
599 ospf6_zebra_if_address_update_delete
;
600 zclient
->redistribute_route_add
= ospf6_zebra_read_route
;
601 zclient
->redistribute_route_del
= ospf6_zebra_read_route
;
603 /* Install command element for zebra node. */
604 install_element(VIEW_NODE
, &show_ospf6_zebra_cmd
);
609 DEFUN (debug_ospf6_zebra_sendrecv
,
610 debug_ospf6_zebra_sendrecv_cmd
,
611 "debug ospf6 zebra [<send|recv>]",
614 "Debug connection between zebra\n"
615 "Debug Sending zebra\n"
616 "Debug Receiving zebra\n"
619 int idx_send_recv
= 3;
620 unsigned char level
= 0;
623 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
624 level
= OSPF6_DEBUG_ZEBRA_SEND
;
625 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
626 level
= OSPF6_DEBUG_ZEBRA_RECV
;
628 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
630 OSPF6_DEBUG_ZEBRA_ON(level
);
634 DEFUN (no_debug_ospf6_zebra_sendrecv
,
635 no_debug_ospf6_zebra_sendrecv_cmd
,
636 "no debug ospf6 zebra [<send|recv>]",
640 "Debug connection between zebra\n"
641 "Debug Sending zebra\n"
642 "Debug Receiving zebra\n"
645 int idx_send_recv
= 4;
646 unsigned char level
= 0;
649 if (strmatch(argv
[idx_send_recv
]->text
, "send"))
650 level
= OSPF6_DEBUG_ZEBRA_SEND
;
651 else if (strmatch(argv
[idx_send_recv
]->text
, "recv"))
652 level
= OSPF6_DEBUG_ZEBRA_RECV
;
654 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
656 OSPF6_DEBUG_ZEBRA_OFF(level
);
661 int config_write_ospf6_debug_zebra(struct vty
*vty
)
663 if (IS_OSPF6_DEBUG_ZEBRA(SEND
) && IS_OSPF6_DEBUG_ZEBRA(RECV
))
664 vty_out(vty
, "debug ospf6 zebra\n");
666 if (IS_OSPF6_DEBUG_ZEBRA(SEND
))
667 vty_out(vty
, "debug ospf6 zebra send\n");
668 if (IS_OSPF6_DEBUG_ZEBRA(RECV
))
669 vty_out(vty
, "debug ospf6 zebra recv\n");
674 void install_element_ospf6_debug_zebra(void)
676 install_element(ENABLE_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
677 install_element(ENABLE_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);
678 install_element(CONFIG_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
679 install_element(CONFIG_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);