]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_zebra.c
Merge pull request #805 from Orange-OpenSource/master
[mirror_frr.git] / ospf6d / ospf6_zebra.c
1 /*
2 * Copyright (C) 2003 Yasuhiro Ohara
3 *
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 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
19 */
20
21 #include <zebra.h>
22
23 #include "log.h"
24 #include "vty.h"
25 #include "command.h"
26 #include "prefix.h"
27 #include "stream.h"
28 #include "zclient.h"
29 #include "memory.h"
30 #include "lib/bfd.h"
31
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"
40 #include "ospf6d.h"
41
42 DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_DISTANCE, "OSPF6 distance")
43
44 unsigned char conf_debug_ospf6_zebra = 0;
45
46 /* information about zebra. */
47 struct zclient *zclient = NULL;
48
49 struct in_addr router_id_zebra;
50
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)
54 {
55 struct prefix router_id;
56 struct ospf6 *o = ospf6;
57
58 zebra_router_id_update_read(zclient->ibuf, &router_id);
59 router_id_zebra = router_id.u.prefix4;
60
61 if (o == NULL)
62 return 0;
63
64 if (o->router_id == 0)
65 o->router_id = (u_int32_t)router_id_zebra.s_addr;
66
67 return 0;
68 }
69
70 /* redistribute function */
71 void ospf6_zebra_redistribute(int type)
72 {
73 if (vrf_bitmap_check(zclient->redist[AFI_IP6][type], VRF_DEFAULT))
74 return;
75 vrf_bitmap_set(zclient->redist[AFI_IP6][type], VRF_DEFAULT);
76
77 if (zclient->sock > 0)
78 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient,
79 AFI_IP6, type, 0, VRF_DEFAULT);
80 }
81
82 void ospf6_zebra_no_redistribute(int type)
83 {
84 if (!vrf_bitmap_check(zclient->redist[AFI_IP6][type], VRF_DEFAULT))
85 return;
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);
90 }
91
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)
95 {
96 struct interface *ifp;
97
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);
103 return 0;
104 }
105
106 static int ospf6_zebra_if_del(int command, struct zclient *zclient,
107 zebra_size_t length, vrf_id_t vrf_id)
108 {
109 struct interface *ifp;
110
111 if (!(ifp = zebra_interface_state_read(zclient->ibuf, vrf_id)))
112 return 0;
113
114 if (if_is_up(ifp))
115 zlog_warn("Zebra: got delete of %s, but interface is still up",
116 ifp->name);
117
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);
121
122 #if 0
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);
127 #endif /*0*/
128
129 ifp->ifindex = IFINDEX_DELETED;
130 return 0;
131 }
132
133 static int ospf6_zebra_if_state_update(int command, struct zclient *zclient,
134 zebra_size_t length, vrf_id_t vrf_id)
135 {
136 struct interface *ifp;
137
138 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
139 if (ifp == NULL)
140 return 0;
141
142 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
143 zlog_debug(
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);
148
149 ospf6_interface_state_update(ifp);
150 return 0;
151 }
152
153 static int ospf6_zebra_if_address_update_add(int command,
154 struct zclient *zclient,
155 zebra_size_t length,
156 vrf_id_t vrf_id)
157 {
158 struct connected *c;
159 char buf[128];
160
161 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD,
162 zclient->ibuf, vrf_id);
163 if (c == NULL)
164 return 0;
165
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,
170 buf, sizeof(buf)),
171 c->address->prefixlen);
172
173 if (c->address->family == AF_INET6) {
174 ospf6_interface_state_update(c->ifp);
175 ospf6_interface_connected_route_update(c->ifp);
176 }
177 return 0;
178 }
179
180 static int ospf6_zebra_if_address_update_delete(int command,
181 struct zclient *zclient,
182 zebra_size_t length,
183 vrf_id_t vrf_id)
184 {
185 struct connected *c;
186 char buf[128];
187
188 c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE,
189 zclient->ibuf, vrf_id);
190 if (c == NULL)
191 return 0;
192
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,
197 buf, sizeof(buf)),
198 c->address->prefixlen);
199
200 if (c->address->family == AF_INET6) {
201 ospf6_interface_connected_route_update(c->ifp);
202 ospf6_interface_state_update(c->ifp);
203 }
204
205 connected_free(c);
206
207 return 0;
208 }
209
210 static int ospf6_zebra_read_ipv6(int command, struct zclient *zclient,
211 zebra_size_t length, vrf_id_t vrf_id)
212 {
213 struct stream *s;
214 struct zapi_ipv6 api;
215 unsigned long ifindex;
216 struct prefix p, src_p;
217 struct in6_addr *nexthop;
218
219 if (ospf6 == NULL)
220 return 0;
221
222 s = zclient->ibuf;
223 ifindex = 0;
224 nexthop = NULL;
225 memset(&api, 0, sizeof(api));
226
227 /* Type, flags, message. */
228 api.type = stream_getc(s);
229 api.instance = stream_getw(s);
230 api.flags = stream_getl(s);
231 api.message = stream_getc(s);
232
233 /* IPv6 prefix. */
234 memset(&p, 0, sizeof(struct prefix));
235 p.family = AF_INET6;
236 p.prefixlen = MIN(IPV6_MAX_PREFIXLEN, stream_getc(s));
237 stream_get(&p.u.prefix6, s, PSIZE(p.prefixlen));
238
239 memset(&src_p, 0, sizeof(struct prefix));
240 src_p.family = AF_INET6;
241 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
242 src_p.prefixlen = stream_getc(s);
243 stream_get(&src_p.u.prefix6, s, PSIZE(src_p.prefixlen));
244 }
245
246 if (src_p.prefixlen)
247 /* we completely ignore srcdest routes for now. */
248 return 0;
249
250 /* Nexthop, ifindex, distance, metric. */
251 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
252 api.nexthop_num = stream_getc(s);
253 nexthop = (struct in6_addr *)malloc(api.nexthop_num
254 * sizeof(struct in6_addr));
255 stream_get(nexthop, s,
256 api.nexthop_num * sizeof(struct in6_addr));
257 }
258 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_IFINDEX)) {
259 api.ifindex_num = stream_getc(s);
260 ifindex = stream_getl(s);
261 }
262 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
263 api.distance = stream_getc(s);
264 else
265 api.distance = 0;
266 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
267 api.metric = stream_getl(s);
268 else
269 api.metric = 0;
270
271 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
272 api.tag = stream_getl(s);
273 else
274 api.tag = 0;
275
276 if (IS_OSPF6_DEBUG_ZEBRA(RECV)) {
277 char prefixstr[PREFIX2STR_BUFFER], nexthopstr[128];
278 prefix2str((struct prefix *)&p, prefixstr, sizeof(prefixstr));
279 if (nexthop)
280 inet_ntop(AF_INET6, nexthop, nexthopstr,
281 sizeof(nexthopstr));
282 else
283 snprintf(nexthopstr, sizeof(nexthopstr), "::");
284
285 zlog_debug(
286 "Zebra Receive route %s: %s %s nexthop %s ifindex %ld tag %" ROUTE_TAG_PRI,
287 (command == ZEBRA_REDISTRIBUTE_IPV6_ADD ? "add"
288 : "delete"),
289 zebra_route_string(api.type), prefixstr, nexthopstr,
290 ifindex, api.tag);
291 }
292
293 if (command == ZEBRA_REDISTRIBUTE_IPV6_ADD)
294 ospf6_asbr_redistribute_add(api.type, ifindex, &p,
295 api.nexthop_num, nexthop, api.tag);
296 else
297 ospf6_asbr_redistribute_remove(api.type, ifindex, &p);
298
299 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP))
300 free(nexthop);
301
302 return 0;
303 }
304
305 DEFUN (show_zebra,
306 show_ospf6_zebra_cmd,
307 "show ipv6 ospf6 zebra",
308 SHOW_STR
309 IPV6_STR
310 OSPF6_STR
311 "Zebra information\n")
312 {
313 int i;
314 if (zclient == NULL) {
315 vty_out(vty, "Not connected to zebra\n");
316 return CMD_SUCCESS;
317 }
318
319 vty_out(vty, "Zebra Infomation\n");
320 vty_out(vty, " enable: %d fail: %d\n", zclient->enable, zclient->fail);
321 vty_out(vty, " redistribute default: %d\n",
322 vrf_bitmap_check(zclient->default_information, VRF_DEFAULT));
323 vty_out(vty, " redistribute:");
324 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
325 if (vrf_bitmap_check(zclient->redist[AFI_IP6][i], VRF_DEFAULT))
326 vty_out(vty, " %s", zebra_route_string(i));
327 }
328 vty_out(vty, "\n");
329 return CMD_SUCCESS;
330 }
331
332 /* Zebra configuration write function. */
333 static int config_write_ospf6_zebra(struct vty *vty)
334 {
335 if (!zclient->enable) {
336 vty_out(vty, "no router zebra\n");
337 vty_out(vty, "!\n");
338 } else if (!vrf_bitmap_check(
339 zclient->redist[AFI_IP6][ZEBRA_ROUTE_OSPF6],
340 VRF_DEFAULT)) {
341 vty_out(vty, "router zebra\n");
342 vty_out(vty, " no redistribute ospf6\n");
343 vty_out(vty, "!\n");
344 }
345 return 0;
346 }
347
348 /* Zebra node structure. */
349 static struct cmd_node zebra_node = {
350 ZEBRA_NODE,
351 "%s(config-zebra)# ",
352 };
353
354 #define ADD 0
355 #define REM 1
356 static void ospf6_zebra_route_update(int type, struct ospf6_route *request)
357 {
358 struct zapi_ipv6 api;
359 char buf[PREFIX2STR_BUFFER];
360 int nhcount;
361 struct in6_addr **nexthops;
362 ifindex_t *ifindexes;
363 int ret = 0;
364 struct prefix_ipv6 *dest;
365
366 if (IS_OSPF6_DEBUG_ZEBRA(SEND)) {
367 prefix2str(&request->prefix, buf, sizeof(buf));
368 zlog_debug("Send %s route: %s",
369 (type == REM ? "remove" : "add"), buf);
370 }
371
372 if (zclient->sock < 0) {
373 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
374 zlog_debug(" Not connected to Zebra");
375 return;
376 }
377
378 if (request->path.origin.adv_router == ospf6->router_id
379 && (request->path.type == OSPF6_PATH_TYPE_EXTERNAL1
380 || request->path.type == OSPF6_PATH_TYPE_EXTERNAL2)) {
381 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
382 zlog_debug(" Ignore self-originated external route");
383 return;
384 }
385
386 /* If removing is the best path and if there's another path,
387 treat this request as add the secondary path */
388 if (type == REM && ospf6_route_is_best(request) && request->next
389 && ospf6_route_is_same(request, request->next)) {
390 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
391 zlog_debug(
392 " Best-path removal resulted Sencondary addition");
393 type = ADD;
394 request = request->next;
395 }
396
397 /* Only the best path will be sent to zebra. */
398 if (!ospf6_route_is_best(request)) {
399 /* this is not preferred best route, ignore */
400 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
401 zlog_debug(" Ignore non-best route");
402 return;
403 }
404
405 nhcount = ospf6_route_num_nexthops(request);
406 if (nhcount == 0) {
407 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
408 zlog_debug(" No nexthop, ignore");
409 return;
410 }
411
412 /* allocate memory for nexthop_list */
413 nexthops =
414 XCALLOC(MTYPE_OSPF6_OTHER, nhcount * sizeof(struct in6_addr *));
415 if (nexthops == NULL) {
416 zlog_warn("Can't send route to zebra: malloc failed");
417 return;
418 }
419
420 /* allocate memory for ifindex_list */
421 ifindexes = XCALLOC(MTYPE_OSPF6_OTHER, nhcount * sizeof(ifindex_t));
422 if (ifindexes == NULL) {
423 zlog_warn("Can't send route to zebra: malloc failed");
424 XFREE(MTYPE_OSPF6_OTHER, nexthops);
425 return;
426 }
427
428 ospf6_route_zebra_copy_nexthops(request, ifindexes, nexthops, nhcount);
429
430 api.vrf_id = VRF_DEFAULT;
431 api.type = ZEBRA_ROUTE_OSPF6;
432 api.instance = 0;
433 api.flags = 0;
434 api.message = 0;
435 api.safi = SAFI_UNICAST;
436 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
437 api.nexthop_num = nhcount;
438 api.nexthop = nexthops;
439 SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX);
440 api.ifindex_num = nhcount;
441 api.ifindex = ifindexes;
442 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
443 api.metric = (request->path.metric_type == 2 ? request->path.u.cost_e2
444 : request->path.cost);
445 if (request->path.tag) {
446 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
447 api.tag = request->path.tag;
448 }
449
450 dest = (struct prefix_ipv6 *)&request->prefix;
451
452 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
453 api.distance = ospf6_distance_apply(dest, request);
454
455 if (type == REM)
456 ret = zapi_ipv6_route(ZEBRA_IPV6_ROUTE_DELETE, zclient, dest,
457 NULL, &api);
458 else
459 ret = zapi_ipv6_route(ZEBRA_IPV6_ROUTE_ADD, zclient, dest, NULL,
460 &api);
461
462 if (ret < 0)
463 zlog_err("zapi_ipv6_route() %s failed: %s",
464 (type == REM ? "delete" : "add"),
465 safe_strerror(errno));
466
467 XFREE(MTYPE_OSPF6_OTHER, nexthops);
468 XFREE(MTYPE_OSPF6_OTHER, ifindexes);
469
470 return;
471 }
472
473 void ospf6_zebra_route_update_add(struct ospf6_route *request)
474 {
475 if (!vrf_bitmap_check(zclient->redist[AFI_IP6][ZEBRA_ROUTE_OSPF6],
476 VRF_DEFAULT)) {
477 ospf6->route_table->hook_add = NULL;
478 ospf6->route_table->hook_remove = NULL;
479 return;
480 }
481 ospf6_zebra_route_update(ADD, request);
482 }
483
484 void ospf6_zebra_route_update_remove(struct ospf6_route *request)
485 {
486 if (!vrf_bitmap_check(zclient->redist[AFI_IP6][ZEBRA_ROUTE_OSPF6],
487 VRF_DEFAULT)) {
488 ospf6->route_table->hook_add = NULL;
489 ospf6->route_table->hook_remove = NULL;
490 return;
491 }
492 ospf6_zebra_route_update(REM, request);
493 }
494
495 void ospf6_zebra_add_discard(struct ospf6_route *request)
496 {
497 struct zapi_ipv6 api;
498 char buf[INET6_ADDRSTRLEN];
499 struct prefix_ipv6 *dest;
500
501 if (vrf_bitmap_check(zclient->redist[AFI_IP6][ZEBRA_ROUTE_OSPF6],
502 VRF_DEFAULT)) {
503 if (!CHECK_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED)) {
504 api.vrf_id = VRF_DEFAULT;
505 api.type = ZEBRA_ROUTE_OSPF6;
506 api.flags = ZEBRA_FLAG_BLACKHOLE;
507 api.instance = 0;
508 api.message = 0;
509 api.safi = SAFI_UNICAST;
510 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
511 api.nexthop_num = 0;
512 api.ifindex_num = 0;
513
514 dest = (struct prefix_ipv6 *)&request->prefix;
515
516 zapi_ipv6_route(ZEBRA_IPV6_ROUTE_ADD, zclient, dest,
517 NULL, &api);
518
519 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
520 zlog_debug("Zebra: Route add discard %s/%d",
521 inet_ntop(AF_INET6, &dest->prefix,
522 buf, INET6_ADDRSTRLEN),
523 dest->prefixlen);
524 SET_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED);
525 } else {
526 dest = (struct prefix_ipv6 *)&request->prefix;
527
528 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
529 zlog_debug(
530 "Zebra: Blackhole route present already %s/%d",
531 inet_ntop(AF_INET6, &dest->prefix, buf,
532 INET6_ADDRSTRLEN),
533 dest->prefixlen);
534 }
535 }
536 }
537
538 void ospf6_zebra_delete_discard(struct ospf6_route *request)
539 {
540 struct zapi_ipv6 api;
541 char buf[INET6_ADDRSTRLEN];
542 struct prefix_ipv6 *dest;
543
544 if (vrf_bitmap_check(zclient->redist[AFI_IP6][ZEBRA_ROUTE_OSPF6],
545 VRF_DEFAULT)) {
546 if (CHECK_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED)) {
547 api.vrf_id = VRF_DEFAULT;
548 api.type = ZEBRA_ROUTE_OSPF6;
549 api.flags = ZEBRA_FLAG_BLACKHOLE;
550 api.instance = 0;
551 api.message = 0;
552 api.safi = SAFI_UNICAST;
553 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
554 api.nexthop_num = 0;
555 api.ifindex_num = 0;
556
557 dest = (struct prefix_ipv6 *)&request->prefix;
558
559 zapi_ipv6_route(ZEBRA_IPV6_ROUTE_DELETE, zclient, dest,
560 NULL, &api);
561
562 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
563 zlog_debug("Zebra: Route delete discard %s/%d",
564 inet_ntop(AF_INET6, &dest->prefix,
565 buf, INET6_ADDRSTRLEN),
566 dest->prefixlen);
567 UNSET_FLAG(request->flag, OSPF6_ROUTE_BLACKHOLE_ADDED);
568 } else {
569 dest = (struct prefix_ipv6 *)&request->prefix;
570 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
571 zlog_debug(
572 "Zebra: Blackhole route already deleted %s/%d",
573 inet_ntop(AF_INET6, &dest->prefix, buf,
574 INET6_ADDRSTRLEN),
575 dest->prefixlen);
576 }
577 }
578 }
579
580 DEFUN (redistribute_ospf6,
581 redistribute_ospf6_cmd,
582 "redistribute ospf6",
583 "Redistribute control\n"
584 "OSPF6 route\n")
585 {
586 struct ospf6_route *route;
587
588 if (vrf_bitmap_check(zclient->redist[AFI_IP6][ZEBRA_ROUTE_OSPF6],
589 VRF_DEFAULT))
590 return CMD_SUCCESS;
591
592 vrf_bitmap_set(zclient->redist[AFI_IP6][ZEBRA_ROUTE_OSPF6],
593 VRF_DEFAULT);
594
595 if (ospf6 == NULL)
596 return CMD_SUCCESS;
597
598 /* send ospf6 route to zebra route table */
599 for (route = ospf6_route_head(ospf6->route_table); route;
600 route = ospf6_route_next(route))
601 ospf6_zebra_route_update_add(route);
602
603 ospf6->route_table->hook_add = ospf6_zebra_route_update_add;
604 ospf6->route_table->hook_remove = ospf6_zebra_route_update_remove;
605
606 return CMD_SUCCESS;
607 }
608
609 DEFUN (no_redistribute_ospf6,
610 no_redistribute_ospf6_cmd,
611 "no redistribute ospf6",
612 NO_STR
613 "Redistribute control\n"
614 "OSPF6 route\n")
615 {
616 struct ospf6_route *route;
617
618 if (!vrf_bitmap_check(zclient->redist[AFI_IP6][ZEBRA_ROUTE_OSPF6],
619 VRF_DEFAULT))
620 return CMD_SUCCESS;
621
622 vrf_bitmap_unset(zclient->redist[AFI_IP6][ZEBRA_ROUTE_OSPF6],
623 VRF_DEFAULT);
624
625 if (ospf6 == NULL)
626 return CMD_SUCCESS;
627
628 ospf6->route_table->hook_add = NULL;
629 ospf6->route_table->hook_remove = NULL;
630
631 /* withdraw ospf6 route from zebra route table */
632 for (route = ospf6_route_head(ospf6->route_table); route;
633 route = ospf6_route_next(route))
634 ospf6_zebra_route_update_remove(route);
635
636 return CMD_SUCCESS;
637 }
638
639 static struct ospf6_distance *ospf6_distance_new(void)
640 {
641 return XCALLOC(MTYPE_OSPF6_DISTANCE, sizeof(struct ospf6_distance));
642 }
643
644 static void ospf6_distance_free(struct ospf6_distance *odistance)
645 {
646 XFREE(MTYPE_OSPF6_DISTANCE, odistance);
647 }
648
649 int ospf6_distance_set(struct vty *vty, struct ospf6 *o,
650 const char *distance_str, const char *ip_str,
651 const char *access_list_str)
652 {
653 int ret;
654 struct prefix_ipv6 p;
655 u_char distance;
656 struct route_node *rn;
657 struct ospf6_distance *odistance;
658
659 ret = str2prefix_ipv6(ip_str, &p);
660 if (ret == 0) {
661 vty_out(vty, "Malformed prefix\n");
662 return CMD_WARNING_CONFIG_FAILED;
663 }
664
665 distance = atoi(distance_str);
666
667 /* Get OSPF6 distance node. */
668 rn = route_node_get(o->distance_table, (struct prefix *)&p);
669 if (rn->info) {
670 odistance = rn->info;
671 route_unlock_node(rn);
672 } else {
673 odistance = ospf6_distance_new();
674 rn->info = odistance;
675 }
676
677 /* Set distance value. */
678 odistance->distance = distance;
679
680 /* Reset access-list configuration. */
681 if (odistance->access_list) {
682 free(odistance->access_list);
683 odistance->access_list = NULL;
684 }
685 if (access_list_str)
686 odistance->access_list = strdup(access_list_str);
687
688 return CMD_SUCCESS;
689 }
690
691 int ospf6_distance_unset(struct vty *vty, struct ospf6 *o,
692 const char *distance_str, const char *ip_str,
693 const char *access_list_str)
694 {
695 int ret;
696 struct prefix_ipv6 p;
697 struct route_node *rn;
698 struct ospf6_distance *odistance;
699
700 ret = str2prefix_ipv6(ip_str, &p);
701 if (ret == 0) {
702 vty_out(vty, "Malformed prefix\n");
703 return CMD_WARNING_CONFIG_FAILED;
704 }
705
706 rn = route_node_lookup(o->distance_table, (struct prefix *)&p);
707 if (!rn) {
708 vty_out(vty, "Cant't find specified prefix\n");
709 return CMD_WARNING_CONFIG_FAILED;
710 }
711
712 odistance = rn->info;
713
714 if (odistance->access_list)
715 free(odistance->access_list);
716 ospf6_distance_free(odistance);
717
718 rn->info = NULL;
719 route_unlock_node(rn);
720 route_unlock_node(rn);
721
722 return CMD_SUCCESS;
723 }
724
725 void ospf6_distance_reset(struct ospf6 *o)
726 {
727 struct route_node *rn;
728 struct ospf6_distance *odistance;
729
730 for (rn = route_top(o->distance_table); rn; rn = route_next(rn))
731 if ((odistance = rn->info) != NULL) {
732 if (odistance->access_list)
733 free(odistance->access_list);
734 ospf6_distance_free(odistance);
735 rn->info = NULL;
736 route_unlock_node(rn);
737 }
738 }
739
740 u_char ospf6_distance_apply(struct prefix_ipv6 *p, struct ospf6_route * or)
741 {
742 struct ospf6 *o;
743
744 o = ospf6;
745 if (o == NULL)
746 return 0;
747
748 if (o->distance_intra)
749 if (or->path.type == OSPF6_PATH_TYPE_INTRA)
750 return o->distance_intra;
751
752 if (o->distance_inter)
753 if (or->path.type == OSPF6_PATH_TYPE_INTER)
754 return o->distance_inter;
755
756 if (o->distance_external)
757 if (or->path.type == OSPF6_PATH_TYPE_EXTERNAL1 ||
758 or->path.type == OSPF6_PATH_TYPE_EXTERNAL2)
759 return o->distance_external;
760
761 if (o->distance_all)
762 return o->distance_all;
763
764 return 0;
765 }
766
767 static void ospf6_zebra_connected(struct zclient *zclient)
768 {
769 /* Send the client registration */
770 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
771
772 zclient_send_reg_requests(zclient, VRF_DEFAULT);
773 }
774
775 void ospf6_zebra_init(struct thread_master *master)
776 {
777 /* Allocate zebra structure. */
778 zclient = zclient_new(master);
779 zclient_init(zclient, ZEBRA_ROUTE_OSPF6, 0);
780 zclient->zebra_connected = ospf6_zebra_connected;
781 zclient->router_id_update = ospf6_router_id_update_zebra;
782 zclient->interface_add = ospf6_zebra_if_add;
783 zclient->interface_delete = ospf6_zebra_if_del;
784 zclient->interface_up = ospf6_zebra_if_state_update;
785 zclient->interface_down = ospf6_zebra_if_state_update;
786 zclient->interface_address_add = ospf6_zebra_if_address_update_add;
787 zclient->interface_address_delete =
788 ospf6_zebra_if_address_update_delete;
789 zclient->redistribute_route_ipv4_add = NULL;
790 zclient->redistribute_route_ipv4_del = NULL;
791 zclient->redistribute_route_ipv6_add = ospf6_zebra_read_ipv6;
792 zclient->redistribute_route_ipv6_del = ospf6_zebra_read_ipv6;
793
794 /* redistribute connected route by default */
795 /* ospf6_zebra_redistribute (ZEBRA_ROUTE_CONNECT); */
796
797 /* Install zebra node. */
798 install_node(&zebra_node, config_write_ospf6_zebra);
799
800 /* Install command element for zebra node. */
801 install_element(VIEW_NODE, &show_ospf6_zebra_cmd);
802 install_default(ZEBRA_NODE);
803 install_element(ZEBRA_NODE, &redistribute_ospf6_cmd);
804 install_element(ZEBRA_NODE, &no_redistribute_ospf6_cmd);
805
806 return;
807 }
808
809 /* Debug */
810
811 DEFUN (debug_ospf6_zebra_sendrecv,
812 debug_ospf6_zebra_sendrecv_cmd,
813 "debug ospf6 zebra [<send|recv>]",
814 DEBUG_STR
815 OSPF6_STR
816 "Debug connection between zebra\n"
817 "Debug Sending zebra\n"
818 "Debug Receiving zebra\n"
819 )
820 {
821 int idx_send_recv = 3;
822 unsigned char level = 0;
823
824 if (argc == 4) {
825 if (strmatch(argv[idx_send_recv]->text, "send"))
826 level = OSPF6_DEBUG_ZEBRA_SEND;
827 else if (strmatch(argv[idx_send_recv]->text, "recv"))
828 level = OSPF6_DEBUG_ZEBRA_RECV;
829 } else
830 level = OSPF6_DEBUG_ZEBRA_SEND | OSPF6_DEBUG_ZEBRA_RECV;
831
832 OSPF6_DEBUG_ZEBRA_ON(level);
833 return CMD_SUCCESS;
834 }
835
836 DEFUN (no_debug_ospf6_zebra_sendrecv,
837 no_debug_ospf6_zebra_sendrecv_cmd,
838 "no debug ospf6 zebra [<send|recv>]",
839 NO_STR
840 DEBUG_STR
841 OSPF6_STR
842 "Debug connection between zebra\n"
843 "Debug Sending zebra\n"
844 "Debug Receiving zebra\n"
845 )
846 {
847 int idx_send_recv = 4;
848 unsigned char level = 0;
849
850 if (argc == 5) {
851 if (strmatch(argv[idx_send_recv]->text, "send"))
852 level = OSPF6_DEBUG_ZEBRA_SEND;
853 else if (strmatch(argv[idx_send_recv]->text, "recv"))
854 level = OSPF6_DEBUG_ZEBRA_RECV;
855 } else
856 level = OSPF6_DEBUG_ZEBRA_SEND | OSPF6_DEBUG_ZEBRA_RECV;
857
858 OSPF6_DEBUG_ZEBRA_OFF(level);
859 return CMD_SUCCESS;
860 }
861
862
863 int config_write_ospf6_debug_zebra(struct vty *vty)
864 {
865 if (IS_OSPF6_DEBUG_ZEBRA(SEND) && IS_OSPF6_DEBUG_ZEBRA(RECV))
866 vty_out(vty, "debug ospf6 zebra\n");
867 else {
868 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
869 vty_out(vty, "debug ospf6 zebra send\n");
870 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
871 vty_out(vty, "debug ospf6 zebra recv\n");
872 }
873 return 0;
874 }
875
876 void install_element_ospf6_debug_zebra(void)
877 {
878 install_element(ENABLE_NODE, &debug_ospf6_zebra_sendrecv_cmd);
879 install_element(ENABLE_NODE, &no_debug_ospf6_zebra_sendrecv_cmd);
880 install_element(CONFIG_NODE, &debug_ospf6_zebra_sendrecv_cmd);
881 install_element(CONFIG_NODE, &no_debug_ospf6_zebra_sendrecv_cmd);
882 }