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.
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 unsigned char conf_debug_ospf6_zebra
= 0;
44 /* information about zebra. */
45 struct zclient
*zclient
= NULL
;
47 struct in_addr router_id_zebra
;
49 /* Router-id update message from zebra. */
51 ospf6_router_id_update_zebra (int command
, struct zclient
*zclient
,
54 struct prefix router_id
;
55 struct ospf6
*o
= ospf6
;
57 zebra_router_id_update_read(zclient
->ibuf
,&router_id
);
58 router_id_zebra
= router_id
.u
.prefix4
;
60 if (o
->router_id
== 0)
61 o
->router_id
= (u_int32_t
) router_id_zebra
.s_addr
;
66 /* redistribute function */
68 ospf6_zebra_redistribute (int type
)
70 if (zclient
->redist
[type
])
72 zclient
->redist
[type
] = 1;
73 if (zclient
->sock
> 0)
74 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
->sock
, type
);
78 ospf6_zebra_no_redistribute (int type
)
80 if (! zclient
->redist
[type
])
82 zclient
->redist
[type
] = 0;
83 if (zclient
->sock
> 0)
84 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
->sock
, type
);
87 /* Inteface addition message from zebra. */
89 ospf6_zebra_if_add (int command
, struct zclient
*zclient
, zebra_size_t length
)
91 struct interface
*ifp
;
93 ifp
= zebra_interface_add_read (zclient
->ibuf
);
94 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
95 zlog_info ("Zebra Interface add: %s index %d mtu %d",
96 ifp
->name
, ifp
->ifindex
, ifp
->mtu6
);
97 ospf6_interface_if_add (ifp
);
102 ospf6_zebra_if_del (int command
, struct zclient
*zclient
, zebra_size_t length
)
105 struct interface
*ifp
;
107 ifp
= zebra_interface_delete_read (zclient
->ibuf
);
108 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
109 zlog_info ("Zebra Interface delete: %s index %d mtu %d",
110 ifp
->name
, ifp
->ifindex
, ifp
->mtu6
);
112 ospf6_interface_if_del (ifp
);
118 ospf6_zebra_if_state_update (int command
, struct zclient
*zclient
,
121 struct interface
*ifp
;
123 ifp
= zebra_interface_state_read (zclient
->ibuf
);
124 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
125 zlog_info ("Zebra Interface state change: "
126 "%s index %d flags %ld metric %d mtu %d",
127 ifp
->name
, ifp
->ifindex
, ifp
->flags
, ifp
->metric
, ifp
->mtu6
);
129 ospf6_interface_state_update (ifp
);
134 ospf6_zebra_if_address_update_add (int command
, struct zclient
*zclient
,
140 c
= zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD
, zclient
->ibuf
);
144 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
145 zlog_info ("Zebra Interface address add: %s %5s %s/%d",
146 c
->ifp
->name
, prefix_family_str (c
->address
),
147 inet_ntop (c
->address
->family
, &c
->address
->u
.prefix
,
148 buf
, sizeof (buf
)), c
->address
->prefixlen
);
150 if (c
->address
->family
== AF_INET6
)
151 ospf6_interface_connected_route_update (c
->ifp
);
157 ospf6_zebra_if_address_update_delete (int command
, struct zclient
*zclient
,
163 c
= zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_DELETE
, zclient
->ibuf
);
167 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
168 zlog_info ("Zebra Interface address delete: %s %5s %s/%d",
169 c
->ifp
->name
, prefix_family_str (c
->address
),
170 inet_ntop (c
->address
->family
, &c
->address
->u
.prefix
,
171 buf
, sizeof (buf
)), c
->address
->prefixlen
);
173 if (c
->address
->family
== AF_INET6
)
174 ospf6_interface_connected_route_update (c
->ifp
);
181 const char *zebra_route_name
[ZEBRA_ROUTE_MAX
] =
182 { "System", "Kernel", "Connect", "Static", "RIP", "RIPng", "OSPF",
183 "OSPF6", "ISIS", "BGP" };
185 const char *zebra_route_abname
[ZEBRA_ROUTE_MAX
] =
186 { "X", "K", "C", "S", "r", "R", "o", "O", "I", "B" };
189 ospf6_zebra_read_ipv6 (int command
, struct zclient
*zclient
,
193 struct zapi_ipv6 api
;
194 unsigned long ifindex
;
195 struct prefix_ipv6 p
;
196 struct in6_addr
*nexthop
;
201 memset (&api
, 0, sizeof (api
));
203 /* Type, flags, message. */
204 api
.type
= stream_getc (s
);
205 api
.flags
= stream_getc (s
);
206 api
.message
= stream_getc (s
);
209 memset (&p
, 0, sizeof (struct prefix_ipv6
));
211 p
.prefixlen
= stream_getc (s
);
212 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
214 /* Nexthop, ifindex, distance, metric. */
215 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
217 api
.nexthop_num
= stream_getc (s
);
218 nexthop
= (struct in6_addr
*)
219 malloc (api
.nexthop_num
* sizeof (struct in6_addr
));
220 stream_get (nexthop
, s
, api
.nexthop_num
* sizeof (struct in6_addr
));
222 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
224 api
.ifindex_num
= stream_getc (s
);
225 ifindex
= stream_getl (s
);
227 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
228 api
.distance
= stream_getc (s
);
231 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
232 api
.metric
= stream_getl (s
);
236 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
238 char prefixstr
[128], nexthopstr
[128];
239 prefix2str ((struct prefix
*)&p
, prefixstr
, sizeof (prefixstr
));
241 inet_ntop (AF_INET6
, nexthop
, nexthopstr
, sizeof (nexthopstr
));
243 snprintf (nexthopstr
, sizeof (nexthopstr
), "::");
245 zlog_info ("Zebra Receive route %s: %s %s nexthop %s ifindex %ld",
246 (command
== ZEBRA_IPV6_ROUTE_ADD
? "add" : "delete"),
247 zebra_route_name
[api
.type
], prefixstr
, nexthopstr
, ifindex
);
250 if (command
== ZEBRA_IPV6_ROUTE_ADD
)
251 ospf6_asbr_redistribute_add (api
.type
, ifindex
, (struct prefix
*) &p
,
252 api
.nexthop_num
, nexthop
);
254 ospf6_asbr_redistribute_remove (api
.type
, ifindex
, (struct prefix
*) &p
);
256 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
269 "Zebra information\n")
274 vty_out (vty
, "Not connected to zebra%s", VNL
);
278 vty_out (vty
, "Zebra Infomation%s", VNL
);
279 vty_out (vty
, " enable: %d fail: %d%s",
280 zclient
->enable
, zclient
->fail
, VNL
);
281 vty_out (vty
, " redistribute default: %d%s", zclient
->redist_default
,
283 vty_out (vty
, " redistribute:");
284 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
286 if (zclient
->redist
[i
])
287 vty_out (vty
, " %s", zebra_route_name
[i
]);
289 vty_out (vty
, "%s", VNL
);
296 "Enable a routing process\n"
297 "Make connection to zebra daemon\n")
299 vty
->node
= ZEBRA_NODE
;
301 zclient_start (zclient
);
305 DEFUN (no_router_zebra
,
309 "Configure routing process\n"
310 "Disable connection to zebra daemon\n")
313 zclient_stop (zclient
);
317 /* Zebra configuration write function. */
319 config_write_ospf6_zebra (struct vty
*vty
)
321 if (! zclient
->enable
)
323 vty_out (vty
, "no router zebra%s", VNL
);
324 vty_out (vty
, "!%s", VNL
);
326 else if (! zclient
->redist
[ZEBRA_ROUTE_OSPF6
])
328 vty_out (vty
, "router zebra%s", VNL
);
329 vty_out (vty
, " no redistribute ospf6%s", VNL
);
330 vty_out (vty
, "!%s", VNL
);
335 /* Zebra node structure. */
336 struct cmd_node zebra_node
=
339 "%s(config-zebra)# ",
345 ospf6_zebra_route_update (int type
, struct ospf6_route
*request
)
347 struct zapi_ipv6 api
;
348 char buf
[64], ifname
[IFNAMSIZ
];
350 struct in6_addr
**nexthops
;
351 unsigned int *ifindexes
;
353 struct prefix_ipv6
*dest
;
355 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
357 prefix2str (&request
->prefix
, buf
, sizeof (buf
));
358 zlog_info ("Send %s route: %s",
359 (type
== REM
? "remove" : "add"), buf
);
362 if (zclient
->sock
< 0)
364 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
365 zlog_info (" Not connected to Zebra");
369 if (request
->path
.origin
.adv_router
== ospf6
->router_id
&&
370 (request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL1
||
371 request
->path
.type
== OSPF6_PATH_TYPE_EXTERNAL2
))
373 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
374 zlog_info (" Ignore self-originated external route");
378 /* If removing is the best path and if there's another path,
379 treat this request as add the secondary path */
380 if (type
== REM
&& ospf6_route_is_best (request
) &&
381 request
->next
&& ospf6_route_is_same (request
, request
->next
))
383 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
384 zlog_info (" Best-path removal resulted Sencondary addition");
386 request
= request
->next
;
389 /* Only the best path will be sent to zebra. */
390 if (! ospf6_route_is_best (request
))
392 /* this is not preferred best route, ignore */
393 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
394 zlog_info (" Ignore non-best route");
399 for (i
= 0; i
< OSPF6_MULTI_PATH_LIMIT
; i
++)
400 if (ospf6_nexthop_is_set (&request
->nexthop
[i
]))
405 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
406 zlog_info (" No nexthop, ignore");
410 /* allocate memory for nexthop_list */
411 nexthops
= XCALLOC (MTYPE_OSPF6_OTHER
,
412 nhcount
* sizeof (struct in6_addr
*));
413 if (nexthops
== NULL
)
415 zlog_warn ("Can't send route to zebra: malloc failed");
419 /* allocate memory for ifindex_list */
420 ifindexes
= XCALLOC (MTYPE_OSPF6_OTHER
,
421 nhcount
* sizeof (unsigned int));
422 if (ifindexes
== NULL
)
424 zlog_warn ("Can't send route to zebra: malloc failed");
425 XFREE (MTYPE_OSPF6_OTHER
, nexthops
);
429 for (i
= 0; i
< nhcount
; i
++)
431 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
433 inet_ntop (AF_INET6
, &request
->nexthop
[i
].address
,
435 if_indextoname (request
->nexthop
[i
].ifindex
, ifname
);
436 zlog_info (" nexthop: %s%%%s(%d)", buf
, ifname
,
437 request
->nexthop
[i
].ifindex
);
439 nexthops
[i
] = &request
->nexthop
[i
].address
;
440 ifindexes
[i
] = request
->nexthop
[i
].ifindex
;
443 api
.type
= ZEBRA_ROUTE_OSPF6
;
446 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
447 api
.nexthop_num
= nhcount
;
448 api
.nexthop
= nexthops
;
449 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
450 api
.ifindex_num
= nhcount
;
451 api
.ifindex
= ifindexes
;
452 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
453 api
.metric
= (request
->path
.metric_type
== 2 ?
454 request
->path
.cost_e2
: request
->path
.cost
);
456 dest
= (struct prefix_ipv6
*) &request
->prefix
;
458 ret
= zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE
, zclient
, dest
, &api
);
460 ret
= zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD
, zclient
, dest
, &api
);
463 zlog_err ("zapi_ipv6_route() %s failed: %s",
464 (type
== REM
? "delete" : "add"), strerror (errno
));
466 XFREE (MTYPE_OSPF6_OTHER
, nexthops
);
467 XFREE (MTYPE_OSPF6_OTHER
, ifindexes
);
473 ospf6_zebra_route_update_add (struct ospf6_route
*request
)
475 if (! zclient
->redist
[ZEBRA_ROUTE_OSPF6
])
477 ospf6
->route_table
->hook_add
= NULL
;
478 ospf6
->route_table
->hook_remove
= NULL
;
481 ospf6_zebra_route_update (ADD
, request
);
485 ospf6_zebra_route_update_remove (struct ospf6_route
*request
)
487 if (! zclient
->redist
[ZEBRA_ROUTE_OSPF6
])
489 ospf6
->route_table
->hook_add
= NULL
;
490 ospf6
->route_table
->hook_remove
= NULL
;
493 ospf6_zebra_route_update (REM
, request
);
496 DEFUN (redistribute_ospf6
,
497 redistribute_ospf6_cmd
,
498 "redistribute ospf6",
499 "Redistribute control\n"
502 struct ospf6_route
*route
;
504 if (zclient
->redist
[ZEBRA_ROUTE_OSPF6
])
507 zclient
->redist
[ZEBRA_ROUTE_OSPF6
] = 1;
512 /* send ospf6 route to zebra route table */
513 for (route
= ospf6_route_head (ospf6
->route_table
); route
;
514 route
= ospf6_route_next (route
))
515 ospf6_zebra_route_update_add (route
);
517 ospf6
->route_table
->hook_add
= ospf6_zebra_route_update_add
;
518 ospf6
->route_table
->hook_remove
= ospf6_zebra_route_update_remove
;
523 DEFUN (no_redistribute_ospf6
,
524 no_redistribute_ospf6_cmd
,
525 "no redistribute ospf6",
527 "Redistribute control\n"
530 struct ospf6_route
*route
;
532 if (! zclient
->redist
[ZEBRA_ROUTE_OSPF6
])
535 zclient
->redist
[ZEBRA_ROUTE_OSPF6
] = 0;
540 ospf6
->route_table
->hook_add
= NULL
;
541 ospf6
->route_table
->hook_remove
= NULL
;
543 /* withdraw ospf6 route from zebra route table */
544 for (route
= ospf6_route_head (ospf6
->route_table
); route
;
545 route
= ospf6_route_next (route
))
546 ospf6_zebra_route_update_remove (route
);
554 /* Allocate zebra structure. */
555 zclient
= zclient_new ();
556 zclient_init (zclient
, ZEBRA_ROUTE_OSPF6
);
557 zclient
->router_id_update
= ospf6_router_id_update_zebra
;
558 zclient
->interface_add
= ospf6_zebra_if_add
;
559 zclient
->interface_delete
= ospf6_zebra_if_del
;
560 zclient
->interface_up
= ospf6_zebra_if_state_update
;
561 zclient
->interface_down
= ospf6_zebra_if_state_update
;
562 zclient
->interface_address_add
= ospf6_zebra_if_address_update_add
;
563 zclient
->interface_address_delete
= ospf6_zebra_if_address_update_delete
;
564 zclient
->ipv4_route_add
= NULL
;
565 zclient
->ipv4_route_delete
= NULL
;
566 zclient
->ipv6_route_add
= ospf6_zebra_read_ipv6
;
567 zclient
->ipv6_route_delete
= ospf6_zebra_read_ipv6
;
569 /* redistribute connected route by default */
570 /* ospf6_zebra_redistribute (ZEBRA_ROUTE_CONNECT); */
572 /* Install zebra node. */
573 install_node (&zebra_node
, config_write_ospf6_zebra
);
575 /* Install command element for zebra node. */
576 install_element (VIEW_NODE
, &show_zebra_cmd
);
577 install_element (ENABLE_NODE
, &show_zebra_cmd
);
578 install_element (CONFIG_NODE
, &router_zebra_cmd
);
579 install_element (CONFIG_NODE
, &no_router_zebra_cmd
);
581 install_default (ZEBRA_NODE
);
582 install_element (ZEBRA_NODE
, &redistribute_ospf6_cmd
);
583 install_element (ZEBRA_NODE
, &no_redistribute_ospf6_cmd
);
590 DEFUN (debug_ospf6_zebra_sendrecv
,
591 debug_ospf6_zebra_sendrecv_cmd
,
592 "debug ospf6 zebra (send|recv)",
595 "Debug connection between zebra\n"
596 "Debug Sending zebra\n"
597 "Debug Receiving zebra\n"
600 unsigned char level
= 0;
604 if (! strncmp (argv
[0], "s", 1))
605 level
= OSPF6_DEBUG_ZEBRA_SEND
;
606 else if (! strncmp (argv
[0], "r", 1))
607 level
= OSPF6_DEBUG_ZEBRA_RECV
;
610 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
612 OSPF6_DEBUG_ZEBRA_ON (level
);
616 ALIAS (debug_ospf6_zebra_sendrecv
,
617 debug_ospf6_zebra_cmd
,
621 "Debug connection between zebra\n"
625 DEFUN (no_debug_ospf6_zebra_sendrecv
,
626 no_debug_ospf6_zebra_sendrecv_cmd
,
627 "no debug ospf6 zebra (send|recv)",
631 "Debug connection between zebra\n"
632 "Debug Sending zebra\n"
633 "Debug Receiving zebra\n"
636 unsigned char level
= 0;
640 if (! strncmp (argv
[0], "s", 1))
641 level
= OSPF6_DEBUG_ZEBRA_SEND
;
642 else if (! strncmp (argv
[0], "r", 1))
643 level
= OSPF6_DEBUG_ZEBRA_RECV
;
646 level
= OSPF6_DEBUG_ZEBRA_SEND
| OSPF6_DEBUG_ZEBRA_RECV
;
648 OSPF6_DEBUG_ZEBRA_OFF (level
);
652 ALIAS (no_debug_ospf6_zebra_sendrecv
,
653 no_debug_ospf6_zebra_cmd
,
654 "no debug ospf6 zebra",
658 "Debug connection between zebra\n"
662 config_write_ospf6_debug_zebra (struct vty
*vty
)
664 if (IS_OSPF6_DEBUG_ZEBRA (SEND
) && IS_OSPF6_DEBUG_ZEBRA (RECV
))
665 vty_out (vty
, "debug ospf6 zebra%s", VNL
);
668 if (IS_OSPF6_DEBUG_ZEBRA (SEND
))
669 vty_out (vty
, "debug ospf6 zebra send%s", VNL
);
670 if (IS_OSPF6_DEBUG_ZEBRA (RECV
))
671 vty_out (vty
, "debug ospf6 zebra recv%s", VNL
);
677 install_element_ospf6_debug_zebra ()
679 install_element (ENABLE_NODE
, &debug_ospf6_zebra_cmd
);
680 install_element (ENABLE_NODE
, &no_debug_ospf6_zebra_cmd
);
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_cmd
);
684 install_element (CONFIG_NODE
, &no_debug_ospf6_zebra_cmd
);
685 install_element (CONFIG_NODE
, &debug_ospf6_zebra_sendrecv_cmd
);
686 install_element (CONFIG_NODE
, &no_debug_ospf6_zebra_sendrecv_cmd
);