blob: 5d5db0b93a5de729934fbbcabd3c14c36a7e28c1 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
hasso508e53e2004-05-18 18:57:06 +00002 * Copyright (C) 2003 Yasuhiro Ohara
paul718e3742002-12-13 20:15:29 +00003 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
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.
20 */
21
hasso508e53e2004-05-18 18:57:06 +000022#include <zebra.h>
23
24#include "log.h"
25#include "vty.h"
26#include "command.h"
27#include "prefix.h"
28#include "stream.h"
29#include "zclient.h"
30#include "memory.h"
31
hasso508e53e2004-05-18 18:57:06 +000032#include "ospf6_proto.h"
33#include "ospf6_top.h"
paul718e3742002-12-13 20:15:29 +000034#include "ospf6_interface.h"
hasso508e53e2004-05-18 18:57:06 +000035#include "ospf6_route.h"
36#include "ospf6_lsa.h"
hasso049207c2004-08-04 20:02:13 +000037#include "ospf6_lsdb.h"
paul718e3742002-12-13 20:15:29 +000038#include "ospf6_asbr.h"
hasso508e53e2004-05-18 18:57:06 +000039#include "ospf6_zebra.h"
hasso049207c2004-08-04 20:02:13 +000040#include "ospf6d.h"
paul718e3742002-12-13 20:15:29 +000041
hasso508e53e2004-05-18 18:57:06 +000042unsigned char conf_debug_ospf6_zebra = 0;
paul718e3742002-12-13 20:15:29 +000043
44/* information about zebra. */
45struct zclient *zclient = NULL;
46
hasso18a6dce2004-10-03 18:18:34 +000047struct in_addr router_id_zebra;
48
49/* Router-id update message from zebra. */
Paul Jakma6ac29a52008-08-15 13:45:30 +010050static int
hasso18a6dce2004-10-03 18:18:34 +000051ospf6_router_id_update_zebra (int command, struct zclient *zclient,
Feng Luc99f3482014-10-16 09:52:36 +080052 zebra_size_t length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +000053{
54 struct prefix router_id;
55 struct ospf6 *o = ospf6;
56
57 zebra_router_id_update_read(zclient->ibuf,&router_id);
58 router_id_zebra = router_id.u.prefix4;
59
jardinc1ba9e82005-03-02 22:43:26 +000060 if (o == NULL)
61 return 0;
62
hasso18a6dce2004-10-03 18:18:34 +000063 if (o->router_id == 0)
64 o->router_id = (u_int32_t) router_id_zebra.s_addr;
65
66 return 0;
67}
68
paul718e3742002-12-13 20:15:29 +000069/* redistribute function */
70void
71ospf6_zebra_redistribute (int type)
72{
Feng Luc99f3482014-10-16 09:52:36 +080073 if (vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT))
paul718e3742002-12-13 20:15:29 +000074 return;
Feng Luc99f3482014-10-16 09:52:36 +080075 vrf_bitmap_set (zclient->redist[type], VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +000076 if (zclient->sock > 0)
Feng Luc99f3482014-10-16 09:52:36 +080077 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, type,
78 VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +000079}
80
81void
82ospf6_zebra_no_redistribute (int type)
83{
Feng Luc99f3482014-10-16 09:52:36 +080084 if (! vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT))
paul718e3742002-12-13 20:15:29 +000085 return;
Feng Luc99f3482014-10-16 09:52:36 +080086 vrf_bitmap_unset (zclient->redist[type], VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +000087 if (zclient->sock > 0)
Feng Luc99f3482014-10-16 09:52:36 +080088 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, type,
89 VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +000090}
91
paul718e3742002-12-13 20:15:29 +000092/* Inteface addition message from zebra. */
Paul Jakma6ac29a52008-08-15 13:45:30 +010093static int
Feng Luc99f3482014-10-16 09:52:36 +080094ospf6_zebra_if_add (int command, struct zclient *zclient, zebra_size_t length,
95 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +000096{
97 struct interface *ifp;
98
Feng Luc99f3482014-10-16 09:52:36 +080099 ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
hasso508e53e2004-05-18 18:57:06 +0000100 if (IS_OSPF6_DEBUG_ZEBRA (RECV))
hassoc6487d62004-12-24 06:00:11 +0000101 zlog_debug ("Zebra Interface add: %s index %d mtu %d",
102 ifp->name, ifp->ifindex, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000103 ospf6_interface_if_add (ifp);
paul718e3742002-12-13 20:15:29 +0000104 return 0;
105}
106
Paul Jakma6ac29a52008-08-15 13:45:30 +0100107static int
Feng Luc99f3482014-10-16 09:52:36 +0800108ospf6_zebra_if_del (int command, struct zclient *zclient, zebra_size_t length,
109 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000110{
hasso508e53e2004-05-18 18:57:06 +0000111 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000112
Feng Luc99f3482014-10-16 09:52:36 +0800113 if (!(ifp = zebra_interface_state_read (zclient->ibuf, vrf_id)))
ajsd2fc8892005-04-02 18:38:43 +0000114 return 0;
115
116 if (if_is_up (ifp))
117 zlog_warn ("Zebra: got delete of %s, but interface is still up", ifp->name);
118
hasso508e53e2004-05-18 18:57:06 +0000119 if (IS_OSPF6_DEBUG_ZEBRA (RECV))
hassoc6487d62004-12-24 06:00:11 +0000120 zlog_debug ("Zebra Interface delete: %s index %d mtu %d",
121 ifp->name, ifp->ifindex, ifp->mtu6);
paul718e3742002-12-13 20:15:29 +0000122
ajsd2fc8892005-04-02 18:38:43 +0000123#if 0
Christian Franked9628722013-03-08 21:47:35 +0100124 /* XXX: ospf6_interface_if_del is not the right way to handle this,
125 * because among other thinkable issues, it will also clear all
126 * settings as they are contained in the struct ospf6_interface. */
paul718e3742002-12-13 20:15:29 +0000127 ospf6_interface_if_del (ifp);
hasso508e53e2004-05-18 18:57:06 +0000128#endif /*0*/
ajsd2fc8892005-04-02 18:38:43 +0000129
130 ifp->ifindex = IFINDEX_INTERNAL;
paul718e3742002-12-13 20:15:29 +0000131 return 0;
132}
133
Paul Jakma6ac29a52008-08-15 13:45:30 +0100134static int
paul718e3742002-12-13 20:15:29 +0000135ospf6_zebra_if_state_update (int command, struct zclient *zclient,
Feng Luc99f3482014-10-16 09:52:36 +0800136 zebra_size_t length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000137{
138 struct interface *ifp;
139
Feng Luc99f3482014-10-16 09:52:36 +0800140 ifp = zebra_interface_state_read (zclient->ibuf, vrf_id);
Ingo Flaschberger57c4f4f2011-04-04 11:17:45 +0100141 if (ifp == NULL)
142 return 0;
143
hasso508e53e2004-05-18 18:57:06 +0000144 if (IS_OSPF6_DEBUG_ZEBRA (RECV))
hassoc6487d62004-12-24 06:00:11 +0000145 zlog_debug ("Zebra Interface state change: "
Vincent Bernatc19543b2012-10-24 14:45:53 +0000146 "%s index %d flags %llx metric %d mtu %d bandwidth %d",
Stephen Hemminger30d20592009-07-28 11:58:51 +0100147 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
Vincent Bernatc19543b2012-10-24 14:45:53 +0000148 ifp->metric, ifp->mtu6, ifp->bandwidth);
paul718e3742002-12-13 20:15:29 +0000149
150 ospf6_interface_state_update (ifp);
151 return 0;
152}
153
Paul Jakma6ac29a52008-08-15 13:45:30 +0100154static int
paul718e3742002-12-13 20:15:29 +0000155ospf6_zebra_if_address_update_add (int command, struct zclient *zclient,
Feng Luc99f3482014-10-16 09:52:36 +0800156 zebra_size_t length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000157{
158 struct connected *c;
159 char buf[128];
160
Feng Luc99f3482014-10-16 09:52:36 +0800161 c = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD, zclient->ibuf,
162 vrf_id);
paul718e3742002-12-13 20:15:29 +0000163 if (c == NULL)
164 return 0;
165
hasso508e53e2004-05-18 18:57:06 +0000166 if (IS_OSPF6_DEBUG_ZEBRA (RECV))
hassoc6487d62004-12-24 06:00:11 +0000167 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,
170 buf, sizeof (buf)), c->address->prefixlen);
paul718e3742002-12-13 20:15:29 +0000171
172 if (c->address->family == AF_INET6)
Christian Frankeb13c1d92012-12-13 16:11:16 +0100173 {
174 ospf6_interface_state_update (c->ifp);
175 ospf6_interface_connected_route_update (c->ifp);
176 }
paul718e3742002-12-13 20:15:29 +0000177 return 0;
178}
179
Paul Jakma6ac29a52008-08-15 13:45:30 +0100180static int
paul718e3742002-12-13 20:15:29 +0000181ospf6_zebra_if_address_update_delete (int command, struct zclient *zclient,
Feng Luc99f3482014-10-16 09:52:36 +0800182 zebra_size_t length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000183{
184 struct connected *c;
185 char buf[128];
186
Feng Luc99f3482014-10-16 09:52:36 +0800187 c = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_DELETE, zclient->ibuf,
188 vrf_id);
paul718e3742002-12-13 20:15:29 +0000189 if (c == NULL)
190 return 0;
191
hasso508e53e2004-05-18 18:57:06 +0000192 if (IS_OSPF6_DEBUG_ZEBRA (RECV))
hassoc6487d62004-12-24 06:00:11 +0000193 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,
196 buf, sizeof (buf)), c->address->prefixlen);
paul718e3742002-12-13 20:15:29 +0000197
198 if (c->address->family == AF_INET6)
Christian Frankeb13c1d92012-12-13 16:11:16 +0100199 {
200 ospf6_interface_connected_route_update (c->ifp);
201 ospf6_interface_state_update (c->ifp);
202 }
paul718e3742002-12-13 20:15:29 +0000203
204 return 0;
205}
206
Paul Jakma6ac29a52008-08-15 13:45:30 +0100207static int
paul718e3742002-12-13 20:15:29 +0000208ospf6_zebra_read_ipv6 (int command, struct zclient *zclient,
Feng Luc99f3482014-10-16 09:52:36 +0800209 zebra_size_t length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000210{
211 struct stream *s;
212 struct zapi_ipv6 api;
213 unsigned long ifindex;
214 struct prefix_ipv6 p;
215 struct in6_addr *nexthop;
Donald Sharp5e57b5f2016-03-11 16:28:34 -0500216 unsigned char plength = 0;
paul718e3742002-12-13 20:15:29 +0000217
218 s = zclient->ibuf;
219 ifindex = 0;
220 nexthop = NULL;
221 memset (&api, 0, sizeof (api));
222
223 /* Type, flags, message. */
224 api.type = stream_getc (s);
225 api.flags = stream_getc (s);
226 api.message = stream_getc (s);
227
228 /* IPv6 prefix. */
229 memset (&p, 0, sizeof (struct prefix_ipv6));
230 p.family = AF_INET6;
Donald Sharp5e57b5f2016-03-11 16:28:34 -0500231 plength = stream_getc (s);
232 p.prefixlen = MIN(IPV6_MAX_PREFIXLEN, plength);
paul718e3742002-12-13 20:15:29 +0000233 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
234
235 /* Nexthop, ifindex, distance, metric. */
236 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
237 {
238 api.nexthop_num = stream_getc (s);
239 nexthop = (struct in6_addr *)
240 malloc (api.nexthop_num * sizeof (struct in6_addr));
241 stream_get (nexthop, s, api.nexthop_num * sizeof (struct in6_addr));
242 }
243 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
244 {
245 api.ifindex_num = stream_getc (s);
246 ifindex = stream_getl (s);
247 }
248 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
249 api.distance = stream_getc (s);
250 else
251 api.distance = 0;
252 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
253 api.metric = stream_getl (s);
254 else
255 api.metric = 0;
256
hasso508e53e2004-05-18 18:57:06 +0000257 if (IS_OSPF6_DEBUG_ZEBRA (RECV))
paul718e3742002-12-13 20:15:29 +0000258 {
hasso508e53e2004-05-18 18:57:06 +0000259 char prefixstr[128], nexthopstr[128];
paul718e3742002-12-13 20:15:29 +0000260 prefix2str ((struct prefix *)&p, prefixstr, sizeof (prefixstr));
hasso508e53e2004-05-18 18:57:06 +0000261 if (nexthop)
262 inet_ntop (AF_INET6, nexthop, nexthopstr, sizeof (nexthopstr));
paul718e3742002-12-13 20:15:29 +0000263 else
hasso508e53e2004-05-18 18:57:06 +0000264 snprintf (nexthopstr, sizeof (nexthopstr), "::");
265
hassoc6487d62004-12-24 06:00:11 +0000266 zlog_debug ("Zebra Receive route %s: %s %s nexthop %s ifindex %ld",
267 (command == ZEBRA_IPV6_ROUTE_ADD ? "add" : "delete"),
ajsf52d13c2005-10-01 17:38:06 +0000268 zebra_route_string(api.type), prefixstr, nexthopstr, ifindex);
paul718e3742002-12-13 20:15:29 +0000269 }
270
271 if (command == ZEBRA_IPV6_ROUTE_ADD)
hasso508e53e2004-05-18 18:57:06 +0000272 ospf6_asbr_redistribute_add (api.type, ifindex, (struct prefix *) &p,
273 api.nexthop_num, nexthop);
paul718e3742002-12-13 20:15:29 +0000274 else
hasso508e53e2004-05-18 18:57:06 +0000275 ospf6_asbr_redistribute_remove (api.type, ifindex, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000276
277 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
278 free (nexthop);
279
280 return 0;
281}
282
hasso508e53e2004-05-18 18:57:06 +0000283
284
David Lamparter6b0655a2014-06-04 06:53:35 +0200285
paul718e3742002-12-13 20:15:29 +0000286DEFUN (show_zebra,
287 show_zebra_cmd,
288 "show zebra",
289 SHOW_STR
290 "Zebra information\n")
291{
292 int i;
hasso508e53e2004-05-18 18:57:06 +0000293 if (zclient == NULL)
294 {
hasso049207c2004-08-04 20:02:13 +0000295 vty_out (vty, "Not connected to zebra%s", VNL);
hasso508e53e2004-05-18 18:57:06 +0000296 return CMD_SUCCESS;
297 }
paul718e3742002-12-13 20:15:29 +0000298
hasso049207c2004-08-04 20:02:13 +0000299 vty_out (vty, "Zebra Infomation%s", VNL);
hasso508e53e2004-05-18 18:57:06 +0000300 vty_out (vty, " enable: %d fail: %d%s",
hasso049207c2004-08-04 20:02:13 +0000301 zclient->enable, zclient->fail, VNL);
Feng Luc99f3482014-10-16 09:52:36 +0800302 vty_out (vty, " redistribute default: %d%s",
303 vrf_bitmap_check (zclient->default_information, VRF_DEFAULT),
hasso049207c2004-08-04 20:02:13 +0000304 VNL);
hasso508e53e2004-05-18 18:57:06 +0000305 vty_out (vty, " redistribute:");
paul718e3742002-12-13 20:15:29 +0000306 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
hasso508e53e2004-05-18 18:57:06 +0000307 {
Feng Luc99f3482014-10-16 09:52:36 +0800308 if (vrf_bitmap_check (zclient->redist[i], VRF_DEFAULT))
ajsf52d13c2005-10-01 17:38:06 +0000309 vty_out (vty, " %s", zebra_route_string(i));
hasso508e53e2004-05-18 18:57:06 +0000310 }
hasso049207c2004-08-04 20:02:13 +0000311 vty_out (vty, "%s", VNL);
paul718e3742002-12-13 20:15:29 +0000312 return CMD_SUCCESS;
313}
314
315DEFUN (router_zebra,
316 router_zebra_cmd,
317 "router zebra",
318 "Enable a routing process\n"
319 "Make connection to zebra daemon\n")
320{
paul718e3742002-12-13 20:15:29 +0000321 vty->node = ZEBRA_NODE;
322 zclient->enable = 1;
323 zclient_start (zclient);
324 return CMD_SUCCESS;
325}
326
327DEFUN (no_router_zebra,
328 no_router_zebra_cmd,
329 "no router zebra",
330 NO_STR
331 "Configure routing process\n"
332 "Disable connection to zebra daemon\n")
333{
paul718e3742002-12-13 20:15:29 +0000334 zclient->enable = 0;
335 zclient_stop (zclient);
336 return CMD_SUCCESS;
337}
338
339/* Zebra configuration write function. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100340static int
hasso508e53e2004-05-18 18:57:06 +0000341config_write_ospf6_zebra (struct vty *vty)
paul718e3742002-12-13 20:15:29 +0000342{
343 if (! zclient->enable)
344 {
hasso049207c2004-08-04 20:02:13 +0000345 vty_out (vty, "no router zebra%s", VNL);
346 vty_out (vty, "!%s", VNL);
paul718e3742002-12-13 20:15:29 +0000347 }
Feng Luc99f3482014-10-16 09:52:36 +0800348 else if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT))
paul718e3742002-12-13 20:15:29 +0000349 {
hasso049207c2004-08-04 20:02:13 +0000350 vty_out (vty, "router zebra%s", VNL);
351 vty_out (vty, " no redistribute ospf6%s", VNL);
352 vty_out (vty, "!%s", VNL);
paul718e3742002-12-13 20:15:29 +0000353 }
354 return 0;
355}
356
357/* Zebra node structure. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -0800358static struct cmd_node zebra_node =
paul718e3742002-12-13 20:15:29 +0000359{
360 ZEBRA_NODE,
361 "%s(config-zebra)# ",
paul718e3742002-12-13 20:15:29 +0000362};
363
364#define ADD 0
hasso508e53e2004-05-18 18:57:06 +0000365#define REM 1
paul718e3742002-12-13 20:15:29 +0000366static void
hasso508e53e2004-05-18 18:57:06 +0000367ospf6_zebra_route_update (int type, struct ospf6_route *request)
paul718e3742002-12-13 20:15:29 +0000368{
paul718e3742002-12-13 20:15:29 +0000369 struct zapi_ipv6 api;
ajsd2fc8892005-04-02 18:38:43 +0000370 char buf[64];
hasso508e53e2004-05-18 18:57:06 +0000371 int nhcount;
paul718e3742002-12-13 20:15:29 +0000372 struct in6_addr **nexthops;
Paul Jakma9099f9b2016-01-18 10:12:10 +0000373 ifindex_t *ifindexes;
paul718e3742002-12-13 20:15:29 +0000374 int i, ret = 0;
hasso508e53e2004-05-18 18:57:06 +0000375 struct prefix_ipv6 *dest;
paul718e3742002-12-13 20:15:29 +0000376
hasso508e53e2004-05-18 18:57:06 +0000377 if (IS_OSPF6_DEBUG_ZEBRA (SEND))
paul718e3742002-12-13 20:15:29 +0000378 {
hasso508e53e2004-05-18 18:57:06 +0000379 prefix2str (&request->prefix, buf, sizeof (buf));
hassoc6487d62004-12-24 06:00:11 +0000380 zlog_debug ("Send %s route: %s",
381 (type == REM ? "remove" : "add"), buf);
paul718e3742002-12-13 20:15:29 +0000382 }
383
384 if (zclient->sock < 0)
385 {
hasso508e53e2004-05-18 18:57:06 +0000386 if (IS_OSPF6_DEBUG_ZEBRA (SEND))
hassoc6487d62004-12-24 06:00:11 +0000387 zlog_debug (" Not connected to Zebra");
paul718e3742002-12-13 20:15:29 +0000388 return;
389 }
390
391 if (request->path.origin.adv_router == ospf6->router_id &&
392 (request->path.type == OSPF6_PATH_TYPE_EXTERNAL1 ||
393 request->path.type == OSPF6_PATH_TYPE_EXTERNAL2))
394 {
hasso508e53e2004-05-18 18:57:06 +0000395 if (IS_OSPF6_DEBUG_ZEBRA (SEND))
hassoc6487d62004-12-24 06:00:11 +0000396 zlog_debug (" Ignore self-originated external route");
paul718e3742002-12-13 20:15:29 +0000397 return;
398 }
399
hasso508e53e2004-05-18 18:57:06 +0000400 /* If removing is the best path and if there's another path,
401 treat this request as add the secondary path */
402 if (type == REM && ospf6_route_is_best (request) &&
403 request->next && ospf6_route_is_same (request, request->next))
404 {
405 if (IS_OSPF6_DEBUG_ZEBRA (SEND))
hassoc6487d62004-12-24 06:00:11 +0000406 zlog_debug (" Best-path removal resulted Sencondary addition");
hasso508e53e2004-05-18 18:57:06 +0000407 type = ADD;
408 request = request->next;
409 }
410
411 /* Only the best path will be sent to zebra. */
412 if (! ospf6_route_is_best (request))
paul718e3742002-12-13 20:15:29 +0000413 {
414 /* this is not preferred best route, ignore */
hasso508e53e2004-05-18 18:57:06 +0000415 if (IS_OSPF6_DEBUG_ZEBRA (SEND))
hassoc6487d62004-12-24 06:00:11 +0000416 zlog_debug (" Ignore non-best route");
paul718e3742002-12-13 20:15:29 +0000417 return;
418 }
419
hasso508e53e2004-05-18 18:57:06 +0000420 nhcount = 0;
421 for (i = 0; i < OSPF6_MULTI_PATH_LIMIT; i++)
422 if (ospf6_nexthop_is_set (&request->nexthop[i]))
423 nhcount++;
paul718e3742002-12-13 20:15:29 +0000424
hasso508e53e2004-05-18 18:57:06 +0000425 if (nhcount == 0)
paul718e3742002-12-13 20:15:29 +0000426 {
hasso508e53e2004-05-18 18:57:06 +0000427 if (IS_OSPF6_DEBUG_ZEBRA (SEND))
hassoc6487d62004-12-24 06:00:11 +0000428 zlog_debug (" No nexthop, ignore");
paul718e3742002-12-13 20:15:29 +0000429 return;
430 }
431
432 /* allocate memory for nexthop_list */
433 nexthops = XCALLOC (MTYPE_OSPF6_OTHER,
hasso508e53e2004-05-18 18:57:06 +0000434 nhcount * sizeof (struct in6_addr *));
435 if (nexthops == NULL)
paul718e3742002-12-13 20:15:29 +0000436 {
hasso508e53e2004-05-18 18:57:06 +0000437 zlog_warn ("Can't send route to zebra: malloc failed");
paul718e3742002-12-13 20:15:29 +0000438 return;
439 }
440
441 /* allocate memory for ifindex_list */
442 ifindexes = XCALLOC (MTYPE_OSPF6_OTHER,
hasso508e53e2004-05-18 18:57:06 +0000443 nhcount * sizeof (unsigned int));
444 if (ifindexes == NULL)
paul718e3742002-12-13 20:15:29 +0000445 {
hasso508e53e2004-05-18 18:57:06 +0000446 zlog_warn ("Can't send route to zebra: malloc failed");
paul718e3742002-12-13 20:15:29 +0000447 XFREE (MTYPE_OSPF6_OTHER, nexthops);
448 return;
449 }
450
hasso508e53e2004-05-18 18:57:06 +0000451 for (i = 0; i < nhcount; i++)
paul718e3742002-12-13 20:15:29 +0000452 {
hasso508e53e2004-05-18 18:57:06 +0000453 if (IS_OSPF6_DEBUG_ZEBRA (SEND))
ajsd2fc8892005-04-02 18:38:43 +0000454 {
Feng Lu395828e2015-05-22 11:39:55 +0200455 const char *ifname;
ajsd2fc8892005-04-02 18:38:43 +0000456 inet_ntop (AF_INET6, &request->nexthop[i].address,
457 buf, sizeof (buf));
Feng Lu395828e2015-05-22 11:39:55 +0200458 ifname = ifindex2ifname (request->nexthop[i].ifindex);
ajsd2fc8892005-04-02 18:38:43 +0000459 zlog_debug (" nexthop: %s%%%.*s(%d)", buf, IFNAMSIZ, ifname,
hassoc6487d62004-12-24 06:00:11 +0000460 request->nexthop[i].ifindex);
ajsd2fc8892005-04-02 18:38:43 +0000461 }
hasso508e53e2004-05-18 18:57:06 +0000462 nexthops[i] = &request->nexthop[i].address;
463 ifindexes[i] = request->nexthop[i].ifindex;
paul718e3742002-12-13 20:15:29 +0000464 }
465
Feng Luc99f3482014-10-16 09:52:36 +0800466 api.vrf_id = VRF_DEFAULT;
paul718e3742002-12-13 20:15:29 +0000467 api.type = ZEBRA_ROUTE_OSPF6;
468 api.flags = 0;
469 api.message = 0;
Denis Ovsienkob4e45f62011-12-05 16:35:14 +0400470 api.safi = SAFI_UNICAST;
paul718e3742002-12-13 20:15:29 +0000471 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
hasso508e53e2004-05-18 18:57:06 +0000472 api.nexthop_num = nhcount;
paul718e3742002-12-13 20:15:29 +0000473 api.nexthop = nexthops;
hasso508e53e2004-05-18 18:57:06 +0000474 SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
475 api.ifindex_num = nhcount;
paul718e3742002-12-13 20:15:29 +0000476 api.ifindex = ifindexes;
hasso508e53e2004-05-18 18:57:06 +0000477 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
478 api.metric = (request->path.metric_type == 2 ?
479 request->path.cost_e2 : request->path.cost);
Roman Hoog Antinkd8f7f862014-03-05 09:13:43 +0100480 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
481 api.distance = ospf6_distance_apply (request, ospf6);
paul718e3742002-12-13 20:15:29 +0000482
hasso508e53e2004-05-18 18:57:06 +0000483 dest = (struct prefix_ipv6 *) &request->prefix;
484 if (type == REM)
485 ret = zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient, dest, &api);
paul718e3742002-12-13 20:15:29 +0000486 else
hasso508e53e2004-05-18 18:57:06 +0000487 ret = zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD, zclient, dest, &api);
paul718e3742002-12-13 20:15:29 +0000488
489 if (ret < 0)
hasso508e53e2004-05-18 18:57:06 +0000490 zlog_err ("zapi_ipv6_route() %s failed: %s",
ajs6099b3b2004-11-20 02:06:59 +0000491 (type == REM ? "delete" : "add"), safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000492
paul718e3742002-12-13 20:15:29 +0000493 XFREE (MTYPE_OSPF6_OTHER, nexthops);
494 XFREE (MTYPE_OSPF6_OTHER, ifindexes);
495
496 return;
497}
498
499void
hasso508e53e2004-05-18 18:57:06 +0000500ospf6_zebra_route_update_add (struct ospf6_route *request)
paul718e3742002-12-13 20:15:29 +0000501{
Feng Luc99f3482014-10-16 09:52:36 +0800502 if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT))
hasso508e53e2004-05-18 18:57:06 +0000503 {
504 ospf6->route_table->hook_add = NULL;
505 ospf6->route_table->hook_remove = NULL;
506 return;
507 }
paul718e3742002-12-13 20:15:29 +0000508 ospf6_zebra_route_update (ADD, request);
509}
510
511void
hasso508e53e2004-05-18 18:57:06 +0000512ospf6_zebra_route_update_remove (struct ospf6_route *request)
paul718e3742002-12-13 20:15:29 +0000513{
Feng Luc99f3482014-10-16 09:52:36 +0800514 if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT))
paul718e3742002-12-13 20:15:29 +0000515 {
hasso508e53e2004-05-18 18:57:06 +0000516 ospf6->route_table->hook_add = NULL;
517 ospf6->route_table->hook_remove = NULL;
518 return;
paul718e3742002-12-13 20:15:29 +0000519 }
hasso508e53e2004-05-18 18:57:06 +0000520 ospf6_zebra_route_update (REM, request);
paul718e3742002-12-13 20:15:29 +0000521}
522
paul718e3742002-12-13 20:15:29 +0000523DEFUN (redistribute_ospf6,
524 redistribute_ospf6_cmd,
525 "redistribute ospf6",
526 "Redistribute control\n"
527 "OSPF6 route\n")
528{
hasso508e53e2004-05-18 18:57:06 +0000529 struct ospf6_route *route;
530
Feng Luc99f3482014-10-16 09:52:36 +0800531 if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT))
hasso508e53e2004-05-18 18:57:06 +0000532 return CMD_SUCCESS;
paul718e3742002-12-13 20:15:29 +0000533
Feng Luc99f3482014-10-16 09:52:36 +0800534 vrf_bitmap_set (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000535
hasso508e53e2004-05-18 18:57:06 +0000536 if (ospf6 == NULL)
537 return CMD_SUCCESS;
538
539 /* send ospf6 route to zebra route table */
540 for (route = ospf6_route_head (ospf6->route_table); route;
541 route = ospf6_route_next (route))
542 ospf6_zebra_route_update_add (route);
543
544 ospf6->route_table->hook_add = ospf6_zebra_route_update_add;
545 ospf6->route_table->hook_remove = ospf6_zebra_route_update_remove;
paul718e3742002-12-13 20:15:29 +0000546
547 return CMD_SUCCESS;
548}
549
550DEFUN (no_redistribute_ospf6,
551 no_redistribute_ospf6_cmd,
552 "no redistribute ospf6",
553 NO_STR
554 "Redistribute control\n"
555 "OSPF6 route\n")
556{
hasso508e53e2004-05-18 18:57:06 +0000557 struct ospf6_route *route;
558
Feng Luc99f3482014-10-16 09:52:36 +0800559 if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT))
hasso508e53e2004-05-18 18:57:06 +0000560 return CMD_SUCCESS;
paul718e3742002-12-13 20:15:29 +0000561
Feng Luc99f3482014-10-16 09:52:36 +0800562 vrf_bitmap_unset (zclient->redist[ZEBRA_ROUTE_OSPF6], VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000563
hasso508e53e2004-05-18 18:57:06 +0000564 if (ospf6 == NULL)
paul718e3742002-12-13 20:15:29 +0000565 return CMD_SUCCESS;
566
hasso508e53e2004-05-18 18:57:06 +0000567 ospf6->route_table->hook_add = NULL;
568 ospf6->route_table->hook_remove = NULL;
paul718e3742002-12-13 20:15:29 +0000569
hasso508e53e2004-05-18 18:57:06 +0000570 /* withdraw ospf6 route from zebra route table */
571 for (route = ospf6_route_head (ospf6->route_table); route;
572 route = ospf6_route_next (route))
573 ospf6_zebra_route_update_remove (route);
paul718e3742002-12-13 20:15:29 +0000574
575 return CMD_SUCCESS;
576}
577
Feng Luc99f3482014-10-16 09:52:36 +0800578static void
579ospf6_zebra_connected (struct zclient *zclient)
580{
581 zclient_send_requests (zclient, VRF_DEFAULT);
582}
583
Roman Hoog Antinkd8f7f862014-03-05 09:13:43 +0100584static struct ospf6_distance *
585ospf6_distance_new (void)
586{
587 return XCALLOC (MTYPE_OSPF6_DISTANCE, sizeof (struct ospf6_distance));
588}
589
590static void
591ospf6_distance_free (struct ospf6_distance *odistance)
592{
593 XFREE (MTYPE_OSPF6_DISTANCE, odistance);
594}
595
596int
597ospf6_distance_set (struct vty *vty, struct ospf6 *o,
598 const char *distance_str,
599 const char *ip_str,
600 const char *access_list_str)
601{
602 int ret;
603 struct prefix_ipv6 p;
604 u_char distance;
605 struct route_node *rn;
606 struct ospf6_distance *odistance;
607
608 ret = str2prefix_ipv6 (ip_str, &p);
609 if (ret == 0)
610 {
611 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
612 return CMD_WARNING;
613 }
614
615 distance = atoi (distance_str);
616
617 /* Get OSPF6 distance node. */
618 rn = route_node_get (o->distance_table, (struct prefix *) &p);
619 if (rn->info)
620 {
621 odistance = rn->info;
622 route_unlock_node (rn);
623 }
624 else
625 {
626 odistance = ospf6_distance_new ();
627 rn->info = odistance;
628 }
629
630 /* Set distance value. */
631 odistance->distance = distance;
632
633 /*Reset access-list configuration. */
634 if (odistance->access_list)
635 {
636 free (odistance->access_list);
637 odistance->access_list = NULL;
638 }
639 if (access_list_str)
640 odistance->access_list = strdup (access_list_str);
641
642 return CMD_SUCCESS;
643}
644
645int
646ospf6_distance_unset (struct vty *vty, struct ospf6 *o,
647 const char *ip_str,
648 const char *access_list_str)
649{
650 int ret;
651 struct prefix_ipv6 p;
652 struct route_node *rn;
653 struct ospf6_distance *odistance;
654
655 ret = str2prefix_ipv6 (ip_str, &p);
656 if (ret == 0)
657 {
658 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
659 return CMD_WARNING;
660 }
661
662 rn = route_node_lookup (o->distance_table, (struct prefix *) &p);
663 if (!rn)
664 {
665 vty_out (vty, "Cant't find specified prefix%s", VTY_NEWLINE);
666 return CMD_WARNING;
667 }
668
669 odistance = rn->info;
670
671 if (odistance->access_list)
672 free (odistance->access_list);
673 ospf6_distance_free (odistance);
674
675 rn->info = NULL;
676 route_unlock_node (rn);
677 route_unlock_node (rn);
678
679 return CMD_SUCCESS;
680}
681
682void
683ospf6_distance_reset (struct ospf6 *o)
684{
685 struct route_node *rn;
686 struct ospf6_distance *odistance;
687
688 for (rn = route_top (o->distance_table); rn; rn = route_next (rn))
689 if ((odistance = rn->info) != NULL)
690 {
691 if (odistance->access_list)
692 free (odistance->access_list);
693 ospf6_distance_free (odistance);
694 rn->info = NULL;
695 route_unlock_node (rn);
696 }
697}
698
699u_char
700ospf6_distance_apply (struct ospf6_route *or, struct ospf6 *o)
701{
702
703 if (o == NULL)
704 return 0;
705
706 if (o->distance_intra)
707 if (or->path.type == OSPF6_PATH_TYPE_INTRA)
708 return o->distance_intra;
709
710 if (o->distance_inter)
711 if (or->path.type == OSPF6_PATH_TYPE_INTER)
712 return o->distance_inter;
713
714 if (o->distance_external)
715 if(or->path.type == OSPF6_PATH_TYPE_EXTERNAL1
716 || or->path.type == OSPF6_PATH_TYPE_EXTERNAL2)
717 return o->distance_external;
718
719 if (o->distance_all)
720 return o->distance_all;
721
722 return 0;
723}
724
paul718e3742002-12-13 20:15:29 +0000725void
Donald Sharp71252932015-09-24 09:25:19 -0400726ospf6_zebra_init (struct thread_master *master)
paul718e3742002-12-13 20:15:29 +0000727{
728 /* Allocate zebra structure. */
Donald Sharp71252932015-09-24 09:25:19 -0400729 zclient = zclient_new (master);
paul718e3742002-12-13 20:15:29 +0000730 zclient_init (zclient, ZEBRA_ROUTE_OSPF6);
Feng Luc99f3482014-10-16 09:52:36 +0800731 zclient->zebra_connected = ospf6_zebra_connected;
hasso18a6dce2004-10-03 18:18:34 +0000732 zclient->router_id_update = ospf6_router_id_update_zebra;
paul718e3742002-12-13 20:15:29 +0000733 zclient->interface_add = ospf6_zebra_if_add;
734 zclient->interface_delete = ospf6_zebra_if_del;
735 zclient->interface_up = ospf6_zebra_if_state_update;
736 zclient->interface_down = ospf6_zebra_if_state_update;
737 zclient->interface_address_add = ospf6_zebra_if_address_update_add;
738 zclient->interface_address_delete = ospf6_zebra_if_address_update_delete;
739 zclient->ipv4_route_add = NULL;
740 zclient->ipv4_route_delete = NULL;
741 zclient->ipv6_route_add = ospf6_zebra_read_ipv6;
742 zclient->ipv6_route_delete = ospf6_zebra_read_ipv6;
743
744 /* redistribute connected route by default */
745 /* ospf6_zebra_redistribute (ZEBRA_ROUTE_CONNECT); */
746
747 /* Install zebra node. */
hasso508e53e2004-05-18 18:57:06 +0000748 install_node (&zebra_node, config_write_ospf6_zebra);
paul718e3742002-12-13 20:15:29 +0000749
750 /* Install command element for zebra node. */
751 install_element (VIEW_NODE, &show_zebra_cmd);
paul718e3742002-12-13 20:15:29 +0000752 install_element (CONFIG_NODE, &router_zebra_cmd);
753 install_element (CONFIG_NODE, &no_router_zebra_cmd);
hasso508e53e2004-05-18 18:57:06 +0000754
paul718e3742002-12-13 20:15:29 +0000755 install_default (ZEBRA_NODE);
756 install_element (ZEBRA_NODE, &redistribute_ospf6_cmd);
757 install_element (ZEBRA_NODE, &no_redistribute_ospf6_cmd);
758
paul718e3742002-12-13 20:15:29 +0000759 return;
760}
761
hasso508e53e2004-05-18 18:57:06 +0000762/* Debug */
David Lamparter6b0655a2014-06-04 06:53:35 +0200763
hasso508e53e2004-05-18 18:57:06 +0000764DEFUN (debug_ospf6_zebra_sendrecv,
765 debug_ospf6_zebra_sendrecv_cmd,
766 "debug ospf6 zebra (send|recv)",
767 DEBUG_STR
768 OSPF6_STR
769 "Debug connection between zebra\n"
770 "Debug Sending zebra\n"
771 "Debug Receiving zebra\n"
772 )
paul718e3742002-12-13 20:15:29 +0000773{
hasso508e53e2004-05-18 18:57:06 +0000774 unsigned char level = 0;
775
776 if (argc)
777 {
778 if (! strncmp (argv[0], "s", 1))
779 level = OSPF6_DEBUG_ZEBRA_SEND;
780 else if (! strncmp (argv[0], "r", 1))
781 level = OSPF6_DEBUG_ZEBRA_RECV;
782 }
783 else
784 level = OSPF6_DEBUG_ZEBRA_SEND | OSPF6_DEBUG_ZEBRA_RECV;
785
786 OSPF6_DEBUG_ZEBRA_ON (level);
787 return CMD_SUCCESS;
paul718e3742002-12-13 20:15:29 +0000788}
789
hasso508e53e2004-05-18 18:57:06 +0000790ALIAS (debug_ospf6_zebra_sendrecv,
791 debug_ospf6_zebra_cmd,
792 "debug ospf6 zebra",
793 DEBUG_STR
794 OSPF6_STR
795 "Debug connection between zebra\n"
Paul Jakma6ac29a52008-08-15 13:45:30 +0100796 )
hasso508e53e2004-05-18 18:57:06 +0000797
798
799DEFUN (no_debug_ospf6_zebra_sendrecv,
800 no_debug_ospf6_zebra_sendrecv_cmd,
801 "no debug ospf6 zebra (send|recv)",
802 NO_STR
803 DEBUG_STR
804 OSPF6_STR
805 "Debug connection between zebra\n"
806 "Debug Sending zebra\n"
807 "Debug Receiving zebra\n"
808 )
809{
810 unsigned char level = 0;
811
812 if (argc)
813 {
814 if (! strncmp (argv[0], "s", 1))
815 level = OSPF6_DEBUG_ZEBRA_SEND;
816 else if (! strncmp (argv[0], "r", 1))
817 level = OSPF6_DEBUG_ZEBRA_RECV;
818 }
819 else
820 level = OSPF6_DEBUG_ZEBRA_SEND | OSPF6_DEBUG_ZEBRA_RECV;
821
822 OSPF6_DEBUG_ZEBRA_OFF (level);
823 return CMD_SUCCESS;
824}
825
826ALIAS (no_debug_ospf6_zebra_sendrecv,
827 no_debug_ospf6_zebra_cmd,
828 "no debug ospf6 zebra",
829 NO_STR
830 DEBUG_STR
831 OSPF6_STR
832 "Debug connection between zebra\n"
Paul Jakma6ac29a52008-08-15 13:45:30 +0100833 )
hasso508e53e2004-05-18 18:57:06 +0000834
835int
836config_write_ospf6_debug_zebra (struct vty *vty)
837{
838 if (IS_OSPF6_DEBUG_ZEBRA (SEND) && IS_OSPF6_DEBUG_ZEBRA (RECV))
hasso049207c2004-08-04 20:02:13 +0000839 vty_out (vty, "debug ospf6 zebra%s", VNL);
hasso508e53e2004-05-18 18:57:06 +0000840 else
841 {
842 if (IS_OSPF6_DEBUG_ZEBRA (SEND))
hasso049207c2004-08-04 20:02:13 +0000843 vty_out (vty, "debug ospf6 zebra send%s", VNL);
hasso508e53e2004-05-18 18:57:06 +0000844 if (IS_OSPF6_DEBUG_ZEBRA (RECV))
hasso049207c2004-08-04 20:02:13 +0000845 vty_out (vty, "debug ospf6 zebra recv%s", VNL);
hasso508e53e2004-05-18 18:57:06 +0000846 }
847 return 0;
848}
849
850void
Paul Jakma6ac29a52008-08-15 13:45:30 +0100851install_element_ospf6_debug_zebra (void)
hasso508e53e2004-05-18 18:57:06 +0000852{
853 install_element (ENABLE_NODE, &debug_ospf6_zebra_cmd);
854 install_element (ENABLE_NODE, &no_debug_ospf6_zebra_cmd);
855 install_element (ENABLE_NODE, &debug_ospf6_zebra_sendrecv_cmd);
856 install_element (ENABLE_NODE, &no_debug_ospf6_zebra_sendrecv_cmd);
857 install_element (CONFIG_NODE, &debug_ospf6_zebra_cmd);
858 install_element (CONFIG_NODE, &no_debug_ospf6_zebra_cmd);
859 install_element (CONFIG_NODE, &debug_ospf6_zebra_sendrecv_cmd);
860 install_element (CONFIG_NODE, &no_debug_ospf6_zebra_sendrecv_cmd);
861}
862
863