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
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
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_asbr.h"
39 #include "ospf6_zebra.h"
41 unsigned char conf_debug_ospf6_zebra
= 0;
43 /* information about zebra. */
44 struct zclient
*zclient
= NULL
;
46 /* redistribute function */
48 ospf6_zebra_redistribute (int type
)
50 if (zclient
->redist
[type
])
52 zclient
->redist
[type
] = 1;
53 if (zclient
->sock
> 0)
54 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
->sock
, type
);
58 ospf6_zebra_no_redistribute (int type
)
60 if (! zclient
->redist
[type
])
62 zclient
->redist
[type
] = 0;
63 if (zclient
->sock
> 0)
64 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
->sock
, type
);
67 /* Inteface addition message from zebra. */
69 ospf6_zebra_if_add (int command
, struct zclient
*zclient
, zebra_size_t length
)
71 struct interface
*ifp
;
73 ifp
= zebra_interface_add_read (zclient
->ibuf
);
74 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
75 zlog_info ("Zebra Interface add: %s index %d mtu %d",
76 ifp
->name
, ifp
->ifindex
, ifp
->mtu6
);
77 ospf6_interface_if_add (ifp
);
82 ospf6_zebra_if_del (int command
, struct zclient
*zclient
, zebra_size_t length
)
85 struct interface
*ifp
;
87 ifp
= zebra_interface_delete_read (zclient
->ibuf
);
88 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
89 zlog_info ("Zebra Interface delete: %s index %d mtu %d",
90 ifp
->name
, ifp
->ifindex
, ifp
->mtu6
);
92 ospf6_interface_if_del (ifp
);
98 ospf6_zebra_if_state_update (int command
, struct zclient
*zclient
,
101 struct interface
*ifp
;
103 ifp
= zebra_interface_state_read (zclient
->ibuf
);
104 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
105 zlog_info ("Zebra Interface state change: "
106 "%s index %d flags %ld metric %d mtu %d",
107 ifp
->name
, ifp
->ifindex
, ifp
->flags
, ifp
->metric
, ifp
->mtu6
);
109 ospf6_interface_state_update (ifp
);
114 ospf6_zebra_if_address_update_add (int command
, struct zclient
*zclient
,
120 c
= zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD
, zclient
->ibuf
);
124 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
125 zlog_info ("Zebra Interface address add: %s %5s %s/%d",
126 c
->ifp
->name
, prefix_family_str (c
->address
),
127 inet_ntop (c
->address
->family
, &c
->address
->u
.prefix
,
128 buf
, sizeof (buf
)), c
->address
->prefixlen
);
130 if (c
->address
->family
== AF_INET6
)
131 ospf6_interface_connected_route_update (c
->ifp
);
137 ospf6_zebra_if_address_update_delete (int command
, struct zclient
*zclient
,
143 c
= zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_DELETE
, zclient
->ibuf
);
147 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
148 zlog_info ("Zebra Interface address delete: %s %5s %s/%d",
149 c
->ifp
->name
, prefix_family_str (c
->address
),
150 inet_ntop (c
->address
->family
, &c
->address
->u
.prefix
,
151 buf
, sizeof (buf
)), c
->address
->prefixlen
);
153 if (c
->address
->family
== AF_INET6
)
154 ospf6_interface_connected_route_update (c
->ifp
);
161 const char *zebra_route_name
[ZEBRA_ROUTE_MAX
] =
162 { "System", "Kernel", "Connect", "Static", "RIP", "RIPng", "OSPF",
165 const char *zebra_route_abname
[ZEBRA_ROUTE_MAX
] =
166 { "X", "K", "C", "S", "r", "R", "o", "O", "B" };
169 ospf6_zebra_read_ipv6 (int command
, struct zclient
*zclient
,
173 struct zapi_ipv6 api
;
174 unsigned long ifindex
;
175 struct prefix_ipv6 p
;
176 struct in6_addr
*nexthop
;
181 memset (&api
, 0, sizeof (api
));
183 /* Type, flags, message. */
184 api
.type
= stream_getc (s
);
185 api
.flags
= stream_getc (s
);
186 api
.message
= stream_getc (s
);
189 memset (&p
, 0, sizeof (struct prefix_ipv6
));
191 p
.prefixlen
= stream_getc (s
);
192 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
194 /* Nexthop, ifindex, distance, metric. */
195 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
197 api
.nexthop_num
= stream_getc (s
);
198 nexthop
= (struct in6_addr
*)
199 malloc (api
.nexthop_num
* sizeof (struct in6_addr
));
200 stream_get (nexthop
, s
, api
.nexthop_num
* sizeof (struct in6_addr
));
202 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
204 api
.ifindex_num
= stream_getc (s
);
205 ifindex
= stream_getl (s
);
207 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
208 api
.distance
= stream_getc (s
);
211 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
212 api
.metric
= stream_getl (s
);
216 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
218 char prefixstr
[128], nexthopstr
[128];
219 prefix2str ((struct prefix
*)&p
, prefixstr
, sizeof (prefixstr
));
221 inet_ntop (AF_INET6
, nexthop
, nexthopstr
, sizeof (nexthopstr
));
223 snprintf (nexthopstr
, sizeof (nexthopstr
), "::");
225 zlog_info ("Zebra Receive route %s: %s %s nexthop %s ifindex %ld",
226 (command
== ZEBRA_IPV6_ROUTE_ADD
? "add" : "delete"),
227 zebra_route_name
[api
.type
], prefixstr
, nexthopstr
, ifindex
);
230 if (command
== ZEBRA_IPV6_ROUTE_ADD
)
231 ospf6_asbr_redistribute_add (api
.type
, ifindex
, (struct prefix
*) &p
,
232 api
.nexthop_num
, nexthop
);
234 ospf6_asbr_redistribute_remove (api
.type
, ifindex
, (struct prefix
*) &p
);
236 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
249 "Zebra information\n")
254 vty_out (vty
, "Not connected to zebra%s", VTY_NEWLINE
);
258 vty_out (vty
, "Zebra Infomation%s", VTY_NEWLINE
);
259 vty_out (vty
, " enable: %d fail: %d%s",
260 zclient
->enable
, zclient
->fail
, VTY_NEWLINE
);
261 vty_out (vty
, " redistribute default: %d%s", zclient
->redist_default
,
263 vty_out (vty
, " redistribute:");
264 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
266 if (zclient
->redist
[i
])
267 vty_out (vty
, " %s", zebra_route_name
[i
]);
269 vty_out (vty
, "%s", VTY_NEWLINE
);
276 "Enable a routing process\n"
277 "Make connection to zebra daemon\n")
279 vty
->node
= ZEBRA_NODE
;
281 zclient_start (zclient
);
285 DEFUN (no_router_zebra
,
289 "Configure routing process\n"
290 "Disable connection to zebra daemon\n")
293 zclient_stop (zclient
);
297 /* Zebra configuration write function. */
299 config_write_ospf6_zebra (struct vty
*vty
)
301 if (! zclient
->enable
)
303 vty_out (vty
, "no router zebra%s", VTY_NEWLINE
);
304 vty_out (vty
, "!%s", VTY_NEWLINE
);
306 else if (! zclient
->redist
[ZEBRA_ROUTE_OSPF6
])
308 vty_out (vty
, "router zebra%s", VTY_NEWLINE
);
309 vty_out (vty
, " no redistribute ospf6%s", VTY_NEWLINE
);
310 vty_out (vty
, "!%s", VTY_NEWLINE
);
315 /* Zebra node structure. */
316 struct cmd_node zebra_node
=
319 "%s(config-zebra)# ",
325 ospf6_zebra_route_update (int type
, struct ospf6_route
*request
)
327 struct zapi_ipv6 api
;
328 char buf
[64], ifname
[IFNAMSIZ
];
330 struct in6_addr
**nexthops
;
331 unsigned int *ifindexes
;
333 struct prefix_ipv6
*dest
;
335 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
337 prefix2str (&request
->prefix
, buf
, sizeof (buf
));
338 zlog_info ("Send %s route: %s",
339 (type
== REM
? "remove" : "add"), buf
);
342 if (zclient
->sock
< 0)
344 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
345 zlog_info (" Not connected to Zebra");
349 if (request
->path
.origin
.adv_router
== ospf6
->router_id
&&
350 (request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
||
351 request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
))
353 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
354 zlog_info (" Ignore self-originated external route");
358 /* If removing is the best path and if there's another path,
359 treat this request as add the secondary path */
360 if (type
== REM
&& ospf6_route_is_best (request
) &&
361 request
->next
&& ospf6_route_is_same (request
, request
->next
))
363 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
364 zlog_info (" Best-path removal resulted Sencondary addition");
366 request
= request
->next
;
369 /* Only the best path will be sent to zebra. */
370 if (! ospf6_route_is_best (request
))
372 /* this is not preferred best route, ignore */
373 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
374 zlog_info (" Ignore non-best route");
379 for (i
= 0; i
< OSPF6_MULTI_PATH_LIMIT
; i
++)
380 if (ospf6_nexthop_is_set (&request
->nexthop
[i
]))
385 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
386 zlog_info (" No nexthop, ignore");
390 /* allocate memory for nexthop_list */
391 nexthops
= XCALLOC (MTYPE_OSPF6_OTHER
,
392 nhcount
* sizeof (struct in6_addr
*));
393 if (nexthops
== NULL
)
395 zlog_warn ("Can't send route to zebra: malloc failed");
399 /* allocate memory for ifindex_list */
400 ifindexes
= XCALLOC (MTYPE_OSPF6_OTHER
,
401 nhcount
* sizeof (unsigned int));
402 if (ifindexes
== NULL
)
404 zlog_warn ("Can't send route to zebra: malloc failed");
405 XFREE (MTYPE_OSPF6_OTHER
, nexthops
);
409 for (i
= 0; i
< nhcount
; i
++)
411 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
413 inet_ntop (AF_INET6
, &request
->nexthop
[i
].address
,
415 if_indextoname (request
->nexthop
[i
].ifindex
, ifname
);
416 zlog_info (" nexthop: %s%%%s(%d)", buf
, ifname
,
417 request
->nexthop
[i
].ifindex
);
419 nexthops
[i
] = &request
->nexthop
[i
].address
;
420 ifindexes
[i
] = request
->nexthop
[i
].ifindex
;
423 api
.type
= ZEBRA_ROUTE_OSPF6
;
426 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
427 api
.nexthop_num
= nhcount
;
428 api
.nexthop
= nexthops
;
429 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
430 api
.ifindex_num
= nhcount
;
431 api
.ifindex
= ifindexes
;
432 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
433 api
.metric
= (request
->path
.metric_type
== 2 ?
434 request
->path
.cost_e2
: request
->path
.cost
);
436 dest
= (struct prefix_ipv6
*) &request
->prefix
;
438 ret
= zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE
, zclient
, dest
, &api
);
440 ret
= zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD
, zclient
, dest
, &api
);
443 zlog_err ("zapi_ipv6_route() %s failed: %s",
444 (type
== REM
? "delete" : "add"), strerror (errno
));
446 XFREE (MTYPE_OSPF6_OTHER
, nexthops
);
447 XFREE (MTYPE_OSPF6_OTHER
, ifindexes
);
453 ospf6_zebra_route_update_add (struct ospf6_route
*request
)
455 if (! zclient
->redist
[ZEBRA_ROUTE_OSPF6
])
457 ospf6
->route_table
->hook_add
= NULL
;
458 ospf6
->route_table
->hook_remove
= NULL
;
461 ospf6_zebra_route_update (ADD
, request
);
465 ospf6_zebra_route_update_remove (struct ospf6_route
*request
)
467 if (! zclient
->redist
[ZEBRA_ROUTE_OSPF6
])
469 ospf6
->route_table
->hook_add
= NULL
;
470 ospf6
->route_table
->hook_remove
= NULL
;
473 ospf6_zebra_route_update (REM
, request
);
476 DEFUN (redistribute_ospf6
,
477 redistribute_ospf6_cmd
,
478 "redistribute ospf6",
479 "Redistribute control\n"
482 struct ospf6_route
*route
;
484 if (zclient
->redist
[ZEBRA_ROUTE_OSPF6
])
487 zclient
->redist
[ZEBRA_ROUTE_OSPF6
] = 1;
492 /* send ospf6 route to zebra route table */
493 for (route
= ospf6_route_head (ospf6
->route_table
); route
;
494 route
= ospf6_route_next (route
))
495 ospf6_zebra_route_update_add (route
);
497 ospf6
->route_table
->hook_add
= ospf6_zebra_route_update_add
;
498 ospf6
->route_table
->hook_remove
= ospf6_zebra_route_update_remove
;
503 DEFUN (no_redistribute_ospf6
,
504 no_redistribute_ospf6_cmd
,
505 "no redistribute ospf6",
507 "Redistribute control\n"
510 struct ospf6_route
*route
;
512 if (! zclient
->redist
[ZEBRA_ROUTE_OSPF6
])
515 zclient
->redist
[ZEBRA_ROUTE_OSPF6
] = 0;
520 ospf6
->route_table
->hook_add
= NULL
;
521 ospf6
->route_table
->hook_remove
= NULL
;
523 /* withdraw ospf6 route from zebra route table */
524 for (route
= ospf6_route_head (ospf6
->route_table
); route
;
525 route
= ospf6_route_next (route
))
526 ospf6_zebra_route_update_remove (route
);
534 /* Allocate zebra structure. */
535 zclient
= zclient_new ();
536 zclient_init (zclient
, ZEBRA_ROUTE_OSPF6
);
537 zclient
->interface_add
= ospf6_zebra_if_add
;
538 zclient
->interface_delete
= ospf6_zebra_if_del
;
539 zclient
->interface_up
= ospf6_zebra_if_state_update
;
540 zclient
->interface_down
= ospf6_zebra_if_state_update
;
541 zclient
->interface_address_add
= ospf6_zebra_if_address_update_add
;
542 zclient
->interface_address_delete
= ospf6_zebra_if_address_update_delete
;
543 zclient
->ipv4_route_add
= NULL
;
544 zclient
->ipv4_route_delete
= NULL
;
545 zclient
->ipv6_route_add
= ospf6_zebra_read_ipv6
;
546 zclient
->ipv6_route_delete
= ospf6_zebra_read_ipv6
;
548 /* redistribute connected route by default */
549 /* ospf6_zebra_redistribute (ZEBRA_ROUTE_CONNECT); */
551 /* Install zebra node. */
552 install_node (&zebra_node
, config_write_ospf6_zebra
);
554 /* Install command element for zebra node. */
555 install_element (VIEW_NODE
, &show_zebra_cmd
);
556 install_element (ENABLE_NODE
, &show_zebra_cmd
);
557 install_element (CONFIG_NODE
, &router_zebra_cmd
);
558 install_element (CONFIG_NODE
, &no_router_zebra_cmd
);
560 install_default (ZEBRA_NODE
);
561 install_element (ZEBRA_NODE
, &redistribute_ospf6_cmd
);
562 install_element (ZEBRA_NODE
, &no_redistribute_ospf6_cmd
);
569 DEFUN (debug_ospf6_zebra_sendrecv
,
570 debug_ospf6_zebra_sendrecv_cmd
,
571 "debug ospf6 zebra (send|recv)",
574 "Debug connection between zebra\n"
575 "Debug Sending zebra\n"
576 "Debug Receiving zebra\n"
579 unsigned char level
= 0;
583 if (! strncmp (argv
[0], "s", 1))
584 level
= OSPF6_DEBUG_ZEBRA_SEND
;
585 else if (! strncmp (argv
[0], "r", 1))
586 level
= OSPF6_DEBUG_ZEBRA_RECV
;
589 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
591 OSPF6_DEBUG_ZEBRA_ON (level
);
595 ALIAS (debug_ospf6_zebra_sendrecv
,
596 debug_ospf6_zebra_cmd
,
600 "Debug connection between zebra\n"
604 DEFUN (no_debug_ospf6_zebra_sendrecv
,
605 no_debug_ospf6_zebra_sendrecv_cmd
,
606 "no debug ospf6 zebra (send|recv)",
610 "Debug connection between zebra\n"
611 "Debug Sending zebra\n"
612 "Debug Receiving zebra\n"
615 unsigned char level
= 0;
619 if (! strncmp (argv
[0], "s", 1))
620 level
= OSPF6_DEBUG_ZEBRA_SEND
;
621 else if (! strncmp (argv
[0], "r", 1))
622 level
= OSPF6_DEBUG_ZEBRA_RECV
;
625 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
627 OSPF6_DEBUG_ZEBRA_OFF (level
);
631 ALIAS (no_debug_ospf6_zebra_sendrecv
,
632 no_debug_ospf6_zebra_cmd
,
633 "no debug ospf6 zebra",
637 "Debug connection between zebra\n"
641 config_write_ospf6_debug_zebra (struct vty
*vty
)
643 if (IS_OSPF6_DEBUG_ZEBRA (SEND
) && IS_OSPF6_DEBUG_ZEBRA (RECV
))
644 vty_out (vty
, "debug ospf6 zebra%s", VTY_NEWLINE
);
647 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
648 vty_out (vty
, "debug ospf6 zebra send%s", VTY_NEWLINE
);
649 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
650 vty_out (vty
, "debug ospf6 zebra recv%s", VTY_NEWLINE
);
656 install_element_ospf6_debug_zebra ()
658 install_element (ENABLE_NODE
, &debug_ospf6_zebra_cmd
);
659 install_element (ENABLE_NODE
, &no_debug_ospf6_zebra_cmd
);
660 install_element (ENABLE_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
661 install_element (ENABLE_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);
662 install_element (CONFIG_NODE
, &debug_ospf6_zebra_cmd
);
663 install_element (CONFIG_NODE
, &no_debug_ospf6_zebra_cmd
);
664 install_element (CONFIG_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
665 install_element (CONFIG_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);