]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_route.c
tests: Adding ospfv3 asbr summarisation
[mirror_frr.git] / ospf6d / ospf6_route.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 "memory.h"
25 #include "prefix.h"
26 #include "table.h"
27 #include "vty.h"
28 #include "command.h"
29 #include "linklist.h"
30
31 #include "ospf6_proto.h"
32 #include "ospf6_lsa.h"
33 #include "ospf6_lsdb.h"
34 #include "ospf6_route.h"
35 #include "ospf6_top.h"
36 #include "ospf6_area.h"
37 #include "ospf6_interface.h"
38 #include "ospf6d.h"
39 #include "ospf6_zebra.h"
40
41 DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_ROUTE, "OSPF6 route");
42 DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_ROUTE_TABLE, "OSPF6 route table");
43 DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_NEXTHOP, "OSPF6 nexthop");
44 DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_PATH, "OSPF6 Path");
45
46 unsigned char conf_debug_ospf6_route = 0;
47
48 static char *ospf6_route_table_name(struct ospf6_route_table *table)
49 {
50 static char name[64];
51 switch (table->scope_type) {
52 case OSPF6_SCOPE_TYPE_GLOBAL: {
53 switch (table->table_type) {
54 case OSPF6_TABLE_TYPE_ROUTES:
55 snprintf(name, sizeof(name), "global route table");
56 break;
57 case OSPF6_TABLE_TYPE_BORDER_ROUTERS:
58 snprintf(name, sizeof(name), "global brouter table");
59 break;
60 case OSPF6_TABLE_TYPE_EXTERNAL_ROUTES:
61 snprintf(name, sizeof(name), "global external table");
62 break;
63 default:
64 snprintf(name, sizeof(name), "global unknown table");
65 break;
66 }
67 } break;
68
69 case OSPF6_SCOPE_TYPE_AREA: {
70 struct ospf6_area *oa = (struct ospf6_area *)table->scope;
71 switch (table->table_type) {
72 case OSPF6_TABLE_TYPE_SPF_RESULTS:
73 snprintf(name, sizeof(name), "area %s spf table",
74 oa->name);
75 break;
76 case OSPF6_TABLE_TYPE_ROUTES:
77 snprintf(name, sizeof(name), "area %s route table",
78 oa->name);
79 break;
80 case OSPF6_TABLE_TYPE_PREFIX_RANGES:
81 snprintf(name, sizeof(name), "area %s range table",
82 oa->name);
83 break;
84 case OSPF6_TABLE_TYPE_SUMMARY_PREFIXES:
85 snprintf(name, sizeof(name),
86 "area %s summary prefix table", oa->name);
87 break;
88 case OSPF6_TABLE_TYPE_SUMMARY_ROUTERS:
89 snprintf(name, sizeof(name),
90 "area %s summary router table", oa->name);
91 break;
92 default:
93 snprintf(name, sizeof(name), "area %s unknown table",
94 oa->name);
95 break;
96 }
97 } break;
98
99 case OSPF6_SCOPE_TYPE_INTERFACE: {
100 struct ospf6_interface *oi =
101 (struct ospf6_interface *)table->scope;
102 switch (table->table_type) {
103 case OSPF6_TABLE_TYPE_CONNECTED_ROUTES:
104 snprintf(name, sizeof(name),
105 "interface %s connected table",
106 oi->interface->name);
107 break;
108 default:
109 snprintf(name, sizeof(name),
110 "interface %s unknown table",
111 oi->interface->name);
112 break;
113 }
114 } break;
115
116 default: {
117 switch (table->table_type) {
118 case OSPF6_TABLE_TYPE_SPF_RESULTS:
119 snprintf(name, sizeof(name), "temporary spf table");
120 break;
121 default:
122 snprintf(name, sizeof(name), "temporary unknown table");
123 break;
124 }
125 } break;
126 }
127 return name;
128 }
129
130 void ospf6_linkstate_prefix(uint32_t adv_router, uint32_t id,
131 struct prefix *prefix)
132 {
133 memset(prefix, 0, sizeof(struct prefix));
134 prefix->family = AF_INET6;
135 prefix->prefixlen = 64;
136 memcpy(&prefix->u.prefix6.s6_addr[0], &adv_router, 4);
137 memcpy(&prefix->u.prefix6.s6_addr[4], &id, 4);
138 }
139
140 void ospf6_linkstate_prefix2str(struct prefix *prefix, char *buf, int size)
141 {
142 uint32_t adv_router, id;
143 char adv_router_str[16], id_str[16];
144 memcpy(&adv_router, &prefix->u.prefix6.s6_addr[0], 4);
145 memcpy(&id, &prefix->u.prefix6.s6_addr[4], 4);
146 inet_ntop(AF_INET, &adv_router, adv_router_str, sizeof(adv_router_str));
147 inet_ntop(AF_INET, &id, id_str, sizeof(id_str));
148 if (ntohl(id))
149 snprintf(buf, size, "%s Net-ID: %s", adv_router_str, id_str);
150 else
151 snprintf(buf, size, "%s", adv_router_str);
152 }
153
154 /* Global strings for logging */
155 const char *const ospf6_dest_type_str[OSPF6_DEST_TYPE_MAX] = {
156 "Unknown", "Router", "Network", "Discard", "Linkstate", "AddressRange",
157 };
158
159 const char *const ospf6_dest_type_substr[OSPF6_DEST_TYPE_MAX] = {
160 "?", "R", "N", "D", "L", "A",
161 };
162
163 const char *const ospf6_path_type_str[OSPF6_PATH_TYPE_MAX] = {
164 "Unknown", "Intra-Area", "Inter-Area", "External-1", "External-2",
165 };
166
167 const char *const ospf6_path_type_substr[OSPF6_PATH_TYPE_MAX] = {
168 "??", "IA", "IE", "E1", "E2",
169 };
170
171 const char *ospf6_path_type_json[OSPF6_PATH_TYPE_MAX] = {
172 "UnknownRoute", "IntraArea", "InterArea", "External1", "External2",
173 };
174
175
176 struct ospf6_nexthop *ospf6_nexthop_create(void)
177 {
178 struct ospf6_nexthop *nh;
179
180 nh = XCALLOC(MTYPE_OSPF6_NEXTHOP, sizeof(struct ospf6_nexthop));
181 return nh;
182 }
183
184 void ospf6_nexthop_delete(struct ospf6_nexthop *nh)
185 {
186 XFREE(MTYPE_OSPF6_NEXTHOP, nh);
187 }
188
189 void ospf6_clear_nexthops(struct list *nh_list)
190 {
191 struct listnode *node;
192 struct ospf6_nexthop *nh;
193
194 if (nh_list) {
195 for (ALL_LIST_ELEMENTS_RO(nh_list, node, nh))
196 ospf6_nexthop_clear(nh);
197 }
198 }
199
200 static struct ospf6_nexthop *
201 ospf6_route_find_nexthop(struct list *nh_list, struct ospf6_nexthop *nh_match)
202 {
203 struct listnode *node;
204 struct ospf6_nexthop *nh;
205
206 if (nh_list && nh_match) {
207 for (ALL_LIST_ELEMENTS_RO(nh_list, node, nh)) {
208 if (ospf6_nexthop_is_same(nh, nh_match))
209 return (nh);
210 }
211 }
212
213 return (NULL);
214 }
215
216 void ospf6_copy_nexthops(struct list *dst, struct list *src)
217 {
218 struct ospf6_nexthop *nh_new, *nh;
219 struct listnode *node;
220
221 if (dst && src) {
222 for (ALL_LIST_ELEMENTS_RO(src, node, nh)) {
223 if (ospf6_nexthop_is_set(nh)) {
224 nh_new = ospf6_nexthop_create();
225 ospf6_nexthop_copy(nh_new, nh);
226 listnode_add_sort(dst, nh_new);
227 }
228 }
229 }
230 }
231
232 void ospf6_merge_nexthops(struct list *dst, struct list *src)
233 {
234 struct listnode *node;
235 struct ospf6_nexthop *nh, *nh_new;
236
237 if (src && dst) {
238 for (ALL_LIST_ELEMENTS_RO(src, node, nh)) {
239 if (!ospf6_route_find_nexthop(dst, nh)) {
240 nh_new = ospf6_nexthop_create();
241 ospf6_nexthop_copy(nh_new, nh);
242 listnode_add_sort(dst, nh_new);
243 }
244 }
245 }
246 }
247
248 int ospf6_route_cmp_nexthops(struct ospf6_route *a, struct ospf6_route *b)
249 {
250 struct listnode *anode, *bnode;
251 struct ospf6_nexthop *anh, *bnh;
252 bool identical = false;
253
254 if (a && b) {
255 if (listcount(a->nh_list) == listcount(b->nh_list)) {
256 for (ALL_LIST_ELEMENTS_RO(a->nh_list, anode, anh)) {
257 identical = false;
258 for (ALL_LIST_ELEMENTS_RO(b->nh_list, bnode,
259 bnh)) {
260 if (ospf6_nexthop_is_same(anh, bnh))
261 identical = true;
262 }
263 /* Currnet List A element not found List B
264 * Non-Identical lists return */
265 if (identical == false)
266 return 1;
267 }
268 return 0;
269 } else
270 return 1;
271 }
272 /* One of the routes doesn't exist ? */
273 return (1);
274 }
275
276 int ospf6_num_nexthops(struct list *nh_list)
277 {
278 return (listcount(nh_list));
279 }
280
281 void ospf6_add_nexthop(struct list *nh_list, int ifindex, struct in6_addr *addr)
282 {
283 struct ospf6_nexthop *nh;
284 struct ospf6_nexthop nh_match;
285
286 if (nh_list) {
287 if (addr) {
288 if (ifindex)
289 nh_match.type = NEXTHOP_TYPE_IPV6_IFINDEX;
290 else
291 nh_match.type = NEXTHOP_TYPE_IPV6;
292
293 memcpy(&nh_match.address, addr,
294 sizeof(struct in6_addr));
295 } else {
296 nh_match.type = NEXTHOP_TYPE_IFINDEX;
297
298 memset(&nh_match.address, 0, sizeof(struct in6_addr));
299 }
300
301 nh_match.ifindex = ifindex;
302
303 if (!ospf6_route_find_nexthop(nh_list, &nh_match)) {
304 nh = ospf6_nexthop_create();
305 ospf6_nexthop_copy(nh, &nh_match);
306 listnode_add(nh_list, nh);
307 }
308 }
309 }
310
311 void ospf6_add_route_nexthop_blackhole(struct ospf6_route *route)
312 {
313 struct ospf6_nexthop *nh;
314 struct ospf6_nexthop nh_match = {};
315
316 /* List not allocated. */
317 if (route->nh_list == NULL)
318 return;
319
320 /* Entry already exists. */
321 nh_match.type = NEXTHOP_TYPE_BLACKHOLE;
322 if (ospf6_route_find_nexthop(route->nh_list, &nh_match))
323 return;
324
325 nh = ospf6_nexthop_create();
326 ospf6_nexthop_copy(nh, &nh_match);
327 listnode_add(route->nh_list, nh);
328 }
329
330 void ospf6_route_zebra_copy_nexthops(struct ospf6_route *route,
331 struct zapi_nexthop nexthops[],
332 int entries, vrf_id_t vrf_id)
333 {
334 struct ospf6_nexthop *nh;
335 struct listnode *node;
336 int i;
337
338 if (route) {
339 i = 0;
340 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
341 if (IS_OSPF6_DEBUG_ZEBRA(SEND)) {
342 zlog_debug(" nexthop: %s %pI6%%%.*s(%d)",
343 nexthop_type_to_str(nh->type),
344 &nh->address, IFNAMSIZ,
345 ifindex2ifname(nh->ifindex, vrf_id),
346 nh->ifindex);
347 }
348
349 if (i >= entries)
350 return;
351
352 nexthops[i].vrf_id = vrf_id;
353 nexthops[i].type = nh->type;
354
355 switch (nh->type) {
356 case NEXTHOP_TYPE_BLACKHOLE:
357 /* NOTHING */
358 break;
359
360 case NEXTHOP_TYPE_IFINDEX:
361 nexthops[i].ifindex = nh->ifindex;
362 break;
363
364 case NEXTHOP_TYPE_IPV4_IFINDEX:
365 case NEXTHOP_TYPE_IPV4:
366 /*
367 * OSPFv3 with IPv4 routes is not supported
368 * yet. Skip this next hop.
369 */
370 if (IS_OSPF6_DEBUG_ZEBRA(SEND))
371 zlog_debug(" Skipping IPv4 next hop");
372 continue;
373
374 case NEXTHOP_TYPE_IPV6_IFINDEX:
375 nexthops[i].ifindex = nh->ifindex;
376 /* FALLTHROUGH */
377 case NEXTHOP_TYPE_IPV6:
378 nexthops[i].gate.ipv6 = nh->address;
379 break;
380 }
381 i++;
382 }
383 }
384 }
385
386 int ospf6_route_get_first_nh_index(struct ospf6_route *route)
387 {
388 struct ospf6_nexthop *nh;
389
390 if (route) {
391 nh = listnode_head(route->nh_list);
392 if (nh)
393 return nh->ifindex;
394 }
395
396 return -1;
397 }
398
399 int ospf6_nexthop_cmp(struct ospf6_nexthop *a, struct ospf6_nexthop *b)
400 {
401 if (a->ifindex < b->ifindex)
402 return -1;
403 else if (a->ifindex > b->ifindex)
404 return 1;
405 else
406 return memcmp(&a->address, &b->address,
407 sizeof(struct in6_addr));
408
409 return 0;
410 }
411
412 static int ospf6_path_cmp(struct ospf6_path *a, struct ospf6_path *b)
413 {
414 if (a->origin.adv_router < b->origin.adv_router)
415 return -1;
416 else if (a->origin.adv_router > b->origin.adv_router)
417 return 1;
418 else
419 return 0;
420 }
421
422 void ospf6_path_free(struct ospf6_path *op)
423 {
424 if (op->nh_list)
425 list_delete(&op->nh_list);
426 XFREE(MTYPE_OSPF6_PATH, op);
427 }
428
429 struct ospf6_path *ospf6_path_dup(struct ospf6_path *path)
430 {
431 struct ospf6_path *new;
432
433 new = XCALLOC(MTYPE_OSPF6_PATH, sizeof(struct ospf6_path));
434 memcpy(new, path, sizeof(struct ospf6_path));
435 new->nh_list = list_new();
436 new->nh_list->cmp = (int (*)(void *, void *))ospf6_nexthop_cmp;
437 new->nh_list->del = (void (*)(void *))ospf6_nexthop_delete;
438
439 return new;
440 }
441
442 void ospf6_copy_paths(struct list *dst, struct list *src)
443 {
444 struct ospf6_path *path_new, *path;
445 struct listnode *node;
446
447 if (dst && src) {
448 for (ALL_LIST_ELEMENTS_RO(src, node, path)) {
449 path_new = ospf6_path_dup(path);
450 ospf6_copy_nexthops(path_new->nh_list, path->nh_list);
451 listnode_add_sort(dst, path_new);
452 }
453 }
454 }
455
456 struct ospf6_route *ospf6_route_create(struct ospf6 *ospf6)
457 {
458 struct ospf6_route *route;
459
460 route = XCALLOC(MTYPE_OSPF6_ROUTE, sizeof(struct ospf6_route));
461 route->nh_list = list_new();
462 route->nh_list->cmp = (int (*)(void *, void *))ospf6_nexthop_cmp;
463 route->nh_list->del = (void (*)(void *))ospf6_nexthop_delete;
464 route->paths = list_new();
465 route->paths->cmp = (int (*)(void *, void *))ospf6_path_cmp;
466 route->paths->del = (void (*)(void *))ospf6_path_free;
467 route->ospf6 = ospf6;
468
469 return route;
470 }
471
472 void ospf6_route_delete(struct ospf6_route *route)
473 {
474 if (route) {
475 if (route->nh_list)
476 list_delete(&route->nh_list);
477 if (route->paths)
478 list_delete(&route->paths);
479 XFREE(MTYPE_OSPF6_ROUTE, route);
480 }
481 }
482
483 struct ospf6_route *ospf6_route_copy(struct ospf6_route *route)
484 {
485 struct ospf6_route *new;
486
487 new = ospf6_route_create(route->ospf6);
488 new->type = route->type;
489 memcpy(&new->prefix, &route->prefix, sizeof(struct prefix));
490 new->installed = route->installed;
491 new->changed = route->changed;
492 new->flag = route->flag;
493 new->route_option = route->route_option;
494 new->linkstate_id = route->linkstate_id;
495 new->path = route->path;
496 ospf6_copy_nexthops(new->nh_list, route->nh_list);
497 ospf6_copy_paths(new->paths, route->paths);
498 new->rnode = NULL;
499 new->prev = NULL;
500 new->next = NULL;
501 new->table = NULL;
502 new->lock = 0;
503 return new;
504 }
505
506 void ospf6_route_lock(struct ospf6_route *route)
507 {
508 route->lock++;
509 }
510
511 void ospf6_route_unlock(struct ospf6_route *route)
512 {
513 assert(route->lock > 0);
514 route->lock--;
515 if (route->lock == 0) {
516 /* Can't detach from the table until here
517 because ospf6_route_next () will use
518 the 'route->table' pointer for logging */
519 route->table = NULL;
520 ospf6_route_delete(route);
521 }
522 }
523
524 /* Route compare function. If ra is more preferred, it returns
525 less than 0. If rb is more preferred returns greater than 0.
526 Otherwise (neither one is preferred), returns 0 */
527 int ospf6_route_cmp(struct ospf6_route *ra, struct ospf6_route *rb)
528 {
529 assert(ospf6_route_is_same(ra, rb));
530 assert(OSPF6_PATH_TYPE_NONE < ra->path.type
531 && ra->path.type < OSPF6_PATH_TYPE_MAX);
532 assert(OSPF6_PATH_TYPE_NONE < rb->path.type
533 && rb->path.type < OSPF6_PATH_TYPE_MAX);
534
535 if (ra->type != rb->type)
536 return (ra->type - rb->type);
537
538 if (ra->path.type != rb->path.type)
539 return (ra->path.type - rb->path.type);
540
541 if (ra->path.type == OSPF6_PATH_TYPE_EXTERNAL2) {
542 if (ra->path.u.cost_e2 != rb->path.u.cost_e2)
543 return (ra->path.u.cost_e2 - rb->path.u.cost_e2);
544 else
545 return (ra->path.cost - rb->path.cost);
546 } else {
547 if (ra->path.cost != rb->path.cost)
548 return (ra->path.cost - rb->path.cost);
549 }
550
551 if (ra->path.area_id != rb->path.area_id)
552 return (ntohl(ra->path.area_id) - ntohl(rb->path.area_id));
553
554 return 0;
555 }
556
557 struct ospf6_route *ospf6_route_lookup(struct prefix *prefix,
558 struct ospf6_route_table *table)
559 {
560 struct route_node *node;
561 struct ospf6_route *route;
562
563 node = route_node_lookup(table->table, prefix);
564 if (node == NULL)
565 return NULL;
566
567 route = (struct ospf6_route *)node->info;
568 route_unlock_node(node); /* to free the lookup lock */
569 return route;
570 }
571
572 struct ospf6_route *
573 ospf6_route_lookup_identical(struct ospf6_route *route,
574 struct ospf6_route_table *table)
575 {
576 struct ospf6_route *target;
577
578 for (target = ospf6_route_lookup(&route->prefix, table); target;
579 target = target->next) {
580 if (target->type == route->type
581 && (memcmp(&target->prefix, &route->prefix,
582 sizeof(struct prefix))
583 == 0)
584 && target->path.type == route->path.type
585 && target->path.cost == route->path.cost
586 && target->path.u.cost_e2 == route->path.u.cost_e2
587 && ospf6_route_cmp_nexthops(target, route) == 0)
588 return target;
589 }
590 return NULL;
591 }
592
593 struct ospf6_route *
594 ospf6_route_lookup_bestmatch(struct prefix *prefix,
595 struct ospf6_route_table *table)
596 {
597 struct route_node *node;
598 struct ospf6_route *route;
599
600 node = route_node_match(table->table, prefix);
601 if (node == NULL)
602 return NULL;
603 route_unlock_node(node);
604
605 route = (struct ospf6_route *)node->info;
606 return route;
607 }
608
609 #ifdef DEBUG
610 static void route_table_assert(struct ospf6_route_table *table)
611 {
612 struct ospf6_route *prev, *r, *next;
613 unsigned int link_error = 0, num = 0;
614
615 r = ospf6_route_head(table);
616 prev = NULL;
617 while (r) {
618 if (r->prev != prev)
619 link_error++;
620
621 next = ospf6_route_next(r);
622
623 if (r->next != next)
624 link_error++;
625
626 prev = r;
627 r = next;
628 }
629
630 for (r = ospf6_route_head(table); r; r = ospf6_route_next(r))
631 num++;
632
633 if (link_error == 0 && num == table->count)
634 return;
635
636 flog_err(EC_LIB_DEVELOPMENT, "PANIC !!");
637 flog_err(EC_LIB_DEVELOPMENT,
638 "Something has gone wrong with ospf6_route_table[%p]", table);
639 zlog_debug("table count = %d, real number = %d", table->count, num);
640 zlog_debug("DUMP START");
641 for (r = ospf6_route_head(table); r; r = ospf6_route_next(r))
642 zlog_info("%p<-[%p]->%p : %pFX", r->prev, r, r->next,
643 &r->prefix);
644 zlog_debug("DUMP END");
645
646 assert(link_error == 0 && num == table->count);
647 }
648 #define ospf6_route_table_assert(t) (route_table_assert (t))
649 #else
650 #define ospf6_route_table_assert(t) ((void) 0)
651 #endif /*DEBUG*/
652
653 struct ospf6_route *ospf6_route_add(struct ospf6_route *route,
654 struct ospf6_route_table *table)
655 {
656 struct route_node *node, *nextnode, *prevnode;
657 struct ospf6_route *current = NULL;
658 struct ospf6_route *prev = NULL, *old = NULL, *next = NULL;
659 char buf[PREFIX2STR_BUFFER];
660 struct timeval now;
661
662 assert(route->rnode == NULL);
663 assert(route->lock == 0);
664 assert(route->next == NULL);
665 assert(route->prev == NULL);
666
667 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
668 ospf6_linkstate_prefix2str(&route->prefix, buf, sizeof(buf));
669 else
670 prefix2str(&route->prefix, buf, sizeof(buf));
671
672 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
673 zlog_debug("%s %p: route add %p: %s paths %u nh %u",
674 ospf6_route_table_name(table), (void *)table,
675 (void *)route, buf, listcount(route->paths),
676 listcount(route->nh_list));
677 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
678 zlog_debug("%s: route add: %s", ospf6_route_table_name(table),
679 buf);
680
681 monotime(&now);
682
683 node = route_node_get(table->table, &route->prefix);
684 route->rnode = node;
685
686 /* find place to insert */
687 for (current = node->info; current; current = current->next) {
688 if (!ospf6_route_is_same(current, route))
689 next = current;
690 else if (current->type != route->type)
691 prev = current;
692 else if (ospf6_route_is_same_origin(current, route))
693 old = current;
694 else if (ospf6_route_cmp(current, route) > 0)
695 next = current;
696 else
697 prev = current;
698
699 if (old || next)
700 break;
701 }
702
703 if (old) {
704 /* if route does not actually change, return unchanged */
705 if (ospf6_route_is_identical(old, route)) {
706 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
707 zlog_debug(
708 "%s %p: route add %p: needless update of %p old cost %u",
709 ospf6_route_table_name(table),
710 (void *)table, (void *)route,
711 (void *)old, old->path.cost);
712 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
713 zlog_debug("%s: route add: needless update",
714 ospf6_route_table_name(table));
715
716 ospf6_route_delete(route);
717 SET_FLAG(old->flag, OSPF6_ROUTE_ADD);
718 ospf6_route_table_assert(table);
719
720 /* to free the lookup lock */
721 route_unlock_node(node);
722 return old;
723 }
724
725 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
726 zlog_debug(
727 "%s %p: route add %p cost %u paths %u nh %u: update of %p cost %u paths %u nh %u",
728 ospf6_route_table_name(table), (void *)table,
729 (void *)route, route->path.cost,
730 listcount(route->paths),
731 listcount(route->nh_list), (void *)old,
732 old->path.cost, listcount(old->paths),
733 listcount(old->nh_list));
734 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
735 zlog_debug("%s: route add: update",
736 ospf6_route_table_name(table));
737
738 /* replace old one if exists */
739 if (node->info == old) {
740 node->info = route;
741 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
742 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
743 zlog_debug("%s: replace old route %s",
744 __func__, buf);
745 }
746
747 if (old->prev)
748 old->prev->next = route;
749 route->prev = old->prev;
750 if (old->next)
751 old->next->prev = route;
752 route->next = old->next;
753
754 route->installed = old->installed;
755 route->changed = now;
756 assert(route->table == NULL);
757 route->table = table;
758
759 ospf6_route_unlock(old); /* will be deleted later */
760 ospf6_route_lock(route);
761
762 SET_FLAG(route->flag, OSPF6_ROUTE_CHANGE);
763 ospf6_route_table_assert(table);
764
765 if (table->hook_add)
766 (*table->hook_add)(route);
767
768 return route;
769 }
770
771 /* insert if previous or next node found */
772 if (prev || next) {
773 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
774 zlog_debug(
775 "%s %p: route add %p cost %u: another path: prev %p, next %p node ref %u",
776 ospf6_route_table_name(table), (void *)table,
777 (void *)route, route->path.cost, (void *)prev,
778 (void *)next, route_node_get_lock_count(node));
779 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
780 zlog_debug("%s: route add cost %u: another path found",
781 ospf6_route_table_name(table),
782 route->path.cost);
783
784 if (prev == NULL)
785 prev = next->prev;
786 if (next == NULL)
787 next = prev->next;
788
789 if (prev)
790 prev->next = route;
791 route->prev = prev;
792 if (next)
793 next->prev = route;
794 route->next = next;
795
796 if (node->info == next) {
797 assert(next && next->rnode == node);
798 node->info = route;
799 UNSET_FLAG(next->flag, OSPF6_ROUTE_BEST);
800 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
801 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
802 zlog_debug(
803 "%s %p: route add %p cost %u: replacing previous best: %p cost %u",
804 ospf6_route_table_name(table),
805 (void *)table, (void *)route,
806 route->path.cost, (void *)next,
807 next->path.cost);
808 }
809
810 route->installed = now;
811 route->changed = now;
812 assert(route->table == NULL);
813 route->table = table;
814
815 ospf6_route_lock(route);
816 table->count++;
817 ospf6_route_table_assert(table);
818
819 SET_FLAG(route->flag, OSPF6_ROUTE_ADD);
820 if (table->hook_add)
821 (*table->hook_add)(route);
822
823 return route;
824 }
825
826 /* Else, this is the brand new route regarding to the prefix */
827 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
828 zlog_debug("%s %p: route add %p %s cost %u: brand new route",
829 ospf6_route_table_name(table), (void *)table,
830 (void *)route, buf, route->path.cost);
831 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
832 zlog_debug("%s: route add: brand new route",
833 ospf6_route_table_name(table));
834
835 assert(node->info == NULL);
836 node->info = route;
837 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
838 ospf6_route_lock(route);
839 route->installed = now;
840 route->changed = now;
841 assert(route->table == NULL);
842 route->table = table;
843
844 /* lookup real existing next route */
845 nextnode = node;
846 route_lock_node(nextnode);
847 do {
848 nextnode = route_next(nextnode);
849 } while (nextnode && nextnode->info == NULL);
850
851 /* set next link */
852 if (nextnode == NULL)
853 route->next = NULL;
854 else {
855 route_unlock_node(nextnode);
856
857 next = nextnode->info;
858 route->next = next;
859 next->prev = route;
860 }
861
862 /* lookup real existing prev route */
863 prevnode = node;
864 route_lock_node(prevnode);
865 do {
866 prevnode = route_prev(prevnode);
867 } while (prevnode && prevnode->info == NULL);
868
869 /* set prev link */
870 if (prevnode == NULL)
871 route->prev = NULL;
872 else {
873 route_unlock_node(prevnode);
874
875 prev = prevnode->info;
876 while (prev->next && ospf6_route_is_same(prev, prev->next))
877 prev = prev->next;
878 route->prev = prev;
879 prev->next = route;
880 }
881
882 table->count++;
883 ospf6_route_table_assert(table);
884
885 SET_FLAG(route->flag, OSPF6_ROUTE_ADD);
886 if (table->hook_add)
887 (*table->hook_add)(route);
888
889 return route;
890 }
891
892 void ospf6_route_remove(struct ospf6_route *route,
893 struct ospf6_route_table *table)
894 {
895 struct route_node *node;
896 struct ospf6_route *current;
897 char buf[PREFIX2STR_BUFFER];
898
899 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
900 ospf6_linkstate_prefix2str(&route->prefix, buf, sizeof(buf));
901 else
902 prefix2str(&route->prefix, buf, sizeof(buf));
903
904 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
905 zlog_debug("%s %p: route remove %p: %s cost %u refcount %u",
906 ospf6_route_table_name(table), (void *)table,
907 (void *)route, buf, route->path.cost, route->lock);
908 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
909 zlog_debug("%s: route remove: %s",
910 ospf6_route_table_name(table), buf);
911
912 node = route_node_lookup(table->table, &route->prefix);
913 assert(node);
914
915 /* find the route to remove, making sure that the route pointer
916 is from the route table. */
917 current = node->info;
918 while (current && current != route)
919 current = current->next;
920
921 assert(current == route);
922
923 /* adjust doubly linked list */
924 if (route->prev)
925 route->prev->next = route->next;
926 if (route->next)
927 route->next->prev = route->prev;
928
929 if (node->info == route) {
930 if (route->next && route->next->rnode == node) {
931 node->info = route->next;
932 SET_FLAG(route->next->flag, OSPF6_ROUTE_BEST);
933 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
934 zlog_debug("%s: remove route %s", __func__,
935 buf);
936 } else {
937 node->info = NULL;
938 route->rnode = NULL;
939 route_unlock_node(node); /* to free the original lock */
940 }
941 }
942
943 route_unlock_node(node); /* to free the lookup lock */
944 table->count--;
945 ospf6_route_table_assert(table);
946
947 SET_FLAG(route->flag, OSPF6_ROUTE_WAS_REMOVED);
948
949 /* Note hook_remove may call ospf6_route_remove */
950 if (table->hook_remove)
951 (*table->hook_remove)(route);
952
953 ospf6_route_unlock(route);
954 }
955
956 struct ospf6_route *ospf6_route_head(struct ospf6_route_table *table)
957 {
958 struct route_node *node;
959 struct ospf6_route *route;
960
961 node = route_top(table->table);
962 if (node == NULL)
963 return NULL;
964
965 /* skip to the real existing entry */
966 while (node && node->info == NULL)
967 node = route_next(node);
968 if (node == NULL)
969 return NULL;
970
971 route_unlock_node(node);
972 assert(node->info);
973
974 route = (struct ospf6_route *)node->info;
975 assert(route->prev == NULL);
976 assert(route->table == table);
977 ospf6_route_lock(route);
978
979 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
980 zlog_info("%s %p: route head: %p<-[%p]->%p",
981 ospf6_route_table_name(table), (void *)table,
982 (void *)route->prev, (void *)route,
983 (void *)route->next);
984
985 return route;
986 }
987
988 struct ospf6_route *ospf6_route_next(struct ospf6_route *route)
989 {
990 struct ospf6_route *next = route->next;
991
992 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
993 zlog_info("%s %p: route next: %p<-[%p]->%p , route ref count %u",
994 ospf6_route_table_name(route->table),
995 (void *)route->table, (void *)route->prev,
996 (void *)route, (void *)route->next,
997 route->lock);
998
999 ospf6_route_unlock(route);
1000 if (next)
1001 ospf6_route_lock(next);
1002
1003 return next;
1004 }
1005
1006 struct ospf6_route *ospf6_route_best_next(struct ospf6_route *route)
1007 {
1008 struct route_node *rnode;
1009 struct ospf6_route *next;
1010
1011 ospf6_route_unlock(route);
1012
1013 rnode = route->rnode;
1014 route_lock_node(rnode);
1015 rnode = route_next(rnode);
1016 while (rnode && rnode->info == NULL)
1017 rnode = route_next(rnode);
1018 if (rnode == NULL)
1019 return NULL;
1020 route_unlock_node(rnode);
1021
1022 assert(rnode->info);
1023 next = (struct ospf6_route *)rnode->info;
1024 ospf6_route_lock(next);
1025 return next;
1026 }
1027
1028 struct ospf6_route *ospf6_route_match_head(struct prefix *prefix,
1029 struct ospf6_route_table *table)
1030 {
1031 struct route_node *node;
1032 struct ospf6_route *route;
1033
1034 /* Walk down tree. */
1035 node = table->table->top;
1036 while (node && node->p.prefixlen < prefix->prefixlen
1037 && prefix_match(&node->p, prefix))
1038 node = node->link[prefix_bit(&prefix->u.prefix,
1039 node->p.prefixlen)];
1040
1041 if (node)
1042 route_lock_node(node);
1043 while (node && node->info == NULL)
1044 node = route_next(node);
1045 if (node == NULL)
1046 return NULL;
1047 route_unlock_node(node);
1048
1049 if (!prefix_match(prefix, &node->p))
1050 return NULL;
1051
1052 route = node->info;
1053 ospf6_route_lock(route);
1054 return route;
1055 }
1056
1057 struct ospf6_route *ospf6_route_match_next(struct prefix *prefix,
1058 struct ospf6_route *route)
1059 {
1060 struct ospf6_route *next;
1061
1062 next = ospf6_route_next(route);
1063 if (next && !prefix_match(prefix, &next->prefix)) {
1064 ospf6_route_unlock(next);
1065 next = NULL;
1066 }
1067
1068 return next;
1069 }
1070
1071 void ospf6_route_remove_all(struct ospf6_route_table *table)
1072 {
1073 struct ospf6_route *route;
1074 for (route = ospf6_route_head(table); route;
1075 route = ospf6_route_next(route))
1076 ospf6_route_remove(route, table);
1077 }
1078
1079 struct ospf6_route_table *ospf6_route_table_create(int s, int t)
1080 {
1081 struct ospf6_route_table *new;
1082 new = XCALLOC(MTYPE_OSPF6_ROUTE_TABLE,
1083 sizeof(struct ospf6_route_table));
1084 new->table = route_table_init();
1085 new->scope_type = s;
1086 new->table_type = t;
1087 return new;
1088 }
1089
1090 void ospf6_route_table_delete(struct ospf6_route_table *table)
1091 {
1092 ospf6_route_remove_all(table);
1093 bf_free(table->idspace);
1094 route_table_finish(table->table);
1095 XFREE(MTYPE_OSPF6_ROUTE_TABLE, table);
1096 }
1097
1098
1099 /* VTY commands */
1100 void ospf6_route_show(struct vty *vty, struct ospf6_route *route,
1101 json_object *json_routes, bool use_json)
1102 {
1103 int i;
1104 char destination[PREFIX2STR_BUFFER], nexthop[64];
1105 char duration[64];
1106 struct timeval now, res;
1107 struct listnode *node;
1108 struct ospf6_nexthop *nh;
1109 json_object *json_route = NULL;
1110 json_object *json_array_next_hops = NULL;
1111 json_object *json_next_hop;
1112
1113 if (om6->ospf6 == NULL) {
1114 vty_out(vty, "OSPFv3 is not running\n");
1115 return;
1116 }
1117
1118 monotime(&now);
1119 timersub(&now, &route->changed, &res);
1120 timerstring(&res, duration, sizeof(duration));
1121
1122 /* destination */
1123 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
1124 ospf6_linkstate_prefix2str(&route->prefix, destination,
1125 sizeof(destination));
1126 else if (route->type == OSPF6_DEST_TYPE_ROUTER)
1127 inet_ntop(route->prefix.family, &route->prefix.u.prefix,
1128 destination, sizeof(destination));
1129 else
1130 prefix2str(&route->prefix, destination, sizeof(destination));
1131
1132 if (use_json) {
1133 json_route = json_object_new_object();
1134 json_object_boolean_add(json_route, "isBestRoute",
1135 ospf6_route_is_best(route));
1136 json_object_string_add(json_route, "destinationType",
1137 OSPF6_DEST_TYPE_SUBSTR(route->type));
1138 json_object_string_add(
1139 json_route, "pathType",
1140 OSPF6_PATH_TYPE_SUBSTR(route->path.type));
1141 json_object_string_add(json_route, "duration", duration);
1142 }
1143
1144 /* Nexthops */
1145 if (use_json)
1146 json_array_next_hops = json_object_new_array();
1147 else
1148 i = 0;
1149 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
1150 struct interface *ifp;
1151 /* nexthop */
1152 inet_ntop(AF_INET6, &nh->address, nexthop, sizeof(nexthop));
1153 ifp = if_lookup_by_index_all_vrf(nh->ifindex);
1154 if (use_json) {
1155 json_next_hop = json_object_new_object();
1156 json_object_string_add(json_next_hop, "nextHop",
1157 nexthop);
1158 json_object_string_add(json_next_hop, "interfaceName",
1159 ifp->name);
1160 json_object_array_add(json_array_next_hops,
1161 json_next_hop);
1162 } else {
1163 if (!i) {
1164 vty_out(vty, "%c%1s %2s %-30s %-25s %6.*s %s\n",
1165 (ospf6_route_is_best(route) ? '*'
1166 : ' '),
1167 OSPF6_DEST_TYPE_SUBSTR(route->type),
1168 OSPF6_PATH_TYPE_SUBSTR(
1169 route->path.type),
1170 destination, nexthop, IFNAMSIZ,
1171 ifp->name, duration);
1172 i++;
1173 } else
1174 vty_out(vty, "%c%1s %2s %-30s %-25s %6.*s %s\n",
1175 ' ', "", "", "", nexthop, IFNAMSIZ,
1176 ifp->name, "");
1177 }
1178 }
1179 if (use_json) {
1180 json_object_object_add(json_route, "nextHops",
1181 json_array_next_hops);
1182 json_object_object_add(json_routes, destination, json_route);
1183 }
1184 }
1185
1186 void ospf6_route_show_detail(struct vty *vty, struct ospf6_route *route,
1187 json_object *json_routes, bool use_json)
1188 {
1189 char destination[PREFIX2STR_BUFFER], nexthop[64];
1190 char area_id[16], id[16], adv_router[16], capa[16], options[16];
1191 struct timeval now, res;
1192 char duration[64];
1193 struct listnode *node;
1194 struct ospf6_nexthop *nh;
1195 char flag[6];
1196 json_object *json_route = NULL;
1197 json_object *json_array_next_hops = NULL;
1198 json_object *json_next_hop;
1199
1200 if (om6->ospf6 == NULL) {
1201 vty_out(vty, "OSPFv3 is not running\n");
1202 return;
1203 }
1204
1205 monotime(&now);
1206
1207 /* destination */
1208 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
1209 ospf6_linkstate_prefix2str(&route->prefix, destination,
1210 sizeof(destination));
1211 else if (route->type == OSPF6_DEST_TYPE_ROUTER)
1212 inet_ntop(route->prefix.family, &route->prefix.u.prefix,
1213 destination, sizeof(destination));
1214 else
1215 prefix2str(&route->prefix, destination, sizeof(destination));
1216
1217 if (use_json) {
1218 json_route = json_object_new_object();
1219 json_object_string_add(json_route, "destinationType",
1220 OSPF6_DEST_TYPE_NAME(route->type));
1221 } else {
1222 vty_out(vty, "Destination: %s\n", destination);
1223 vty_out(vty, "Destination type: %s\n",
1224 OSPF6_DEST_TYPE_NAME(route->type));
1225 }
1226
1227 /* Time */
1228 timersub(&now, &route->installed, &res);
1229 timerstring(&res, duration, sizeof(duration));
1230 if (use_json)
1231 json_object_string_add(json_route, "installedTimeSince",
1232 duration);
1233 else
1234 vty_out(vty, "Installed Time: %s ago\n", duration);
1235
1236 timersub(&now, &route->changed, &res);
1237 timerstring(&res, duration, sizeof(duration));
1238 if (use_json)
1239 json_object_string_add(json_route, "changedTimeSince",
1240 duration);
1241 else
1242 vty_out(vty, "Changed Time: %s ago\n", duration);
1243
1244 /* Debugging info */
1245 if (use_json) {
1246 json_object_int_add(json_route, "numberOfLock", route->lock);
1247 snprintf(
1248 flag, sizeof(flag), "%s%s%s%s",
1249 (CHECK_FLAG(route->flag, OSPF6_ROUTE_BEST) ? "B" : "-"),
1250 (CHECK_FLAG(route->flag, OSPF6_ROUTE_ADD) ? "A" : "-"),
1251 (CHECK_FLAG(route->flag, OSPF6_ROUTE_REMOVE) ? "R"
1252 : "-"),
1253 (CHECK_FLAG(route->flag, OSPF6_ROUTE_CHANGE) ? "C"
1254 : "-"));
1255 json_object_string_add(json_route, "flags", flag);
1256 } else {
1257 vty_out(vty, "Lock: %d Flags: %s%s%s%s\n", route->lock,
1258 (CHECK_FLAG(route->flag, OSPF6_ROUTE_BEST) ? "B" : "-"),
1259 (CHECK_FLAG(route->flag, OSPF6_ROUTE_ADD) ? "A" : "-"),
1260 (CHECK_FLAG(route->flag, OSPF6_ROUTE_REMOVE) ? "R"
1261 : "-"),
1262 (CHECK_FLAG(route->flag, OSPF6_ROUTE_CHANGE) ? "C"
1263 : "-"));
1264 vty_out(vty, "Memory: prev: %p this: %p next: %p\n",
1265 (void *)route->prev, (void *)route,
1266 (void *)route->next);
1267 }
1268
1269 /* Path section */
1270
1271 /* Area-ID */
1272 inet_ntop(AF_INET, &route->path.area_id, area_id, sizeof(area_id));
1273 if (use_json)
1274 json_object_string_add(json_route, "associatedArea", area_id);
1275 else
1276 vty_out(vty, "Associated Area: %s\n", area_id);
1277
1278 /* Path type */
1279 if (use_json)
1280 json_object_string_add(json_route, "pathType",
1281 OSPF6_PATH_TYPE_NAME(route->path.type));
1282 else
1283 vty_out(vty, "Path Type: %s\n",
1284 OSPF6_PATH_TYPE_NAME(route->path.type));
1285
1286 /* LS Origin */
1287 inet_ntop(AF_INET, &route->path.origin.id, id, sizeof(id));
1288 inet_ntop(AF_INET, &route->path.origin.adv_router, adv_router,
1289 sizeof(adv_router));
1290 if (use_json) {
1291 json_object_string_add(
1292 json_route, "lsOriginRoutePathType",
1293 ospf6_lstype_name(route->path.origin.type));
1294 json_object_string_add(json_route, "lsId", id);
1295 json_object_string_add(json_route, "lsAdvertisingRouter",
1296 adv_router);
1297 } else {
1298 vty_out(vty, "LS Origin: %s Id: %s Adv: %s\n",
1299 ospf6_lstype_name(route->path.origin.type), id,
1300 adv_router);
1301 }
1302
1303 /* Options */
1304 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1305 if (use_json)
1306 json_object_string_add(json_route, "options", options);
1307 else
1308 vty_out(vty, "Options: %s\n", options);
1309
1310 /* Router Bits */
1311 ospf6_capability_printbuf(route->path.router_bits, capa, sizeof(capa));
1312 if (use_json)
1313 json_object_string_add(json_route, "routerBits", capa);
1314 else
1315 vty_out(vty, "Router Bits: %s\n", capa);
1316
1317 /* Prefix Options */
1318 if (use_json)
1319 json_object_string_add(json_route, "prefixOptions", "xxx");
1320 else
1321 vty_out(vty, "Prefix Options: xxx\n");
1322
1323 /* Metrics */
1324 if (use_json) {
1325 json_object_int_add(json_route, "metricType",
1326 route->path.metric_type);
1327 json_object_int_add(json_route, "metricCost", route->path.cost);
1328 json_object_int_add(json_route, "metricCostE2",
1329 route->path.u.cost_e2);
1330
1331 json_object_int_add(json_route, "pathsCount",
1332 route->paths->count);
1333 json_object_int_add(json_route, "nextHopCount",
1334 route->nh_list->count);
1335 } else {
1336 vty_out(vty, "Metric Type: %d\n", route->path.metric_type);
1337 vty_out(vty, "Metric: %d (%d)\n", route->path.cost,
1338 route->path.u.cost_e2);
1339
1340 vty_out(vty, "Paths count: %u\n", route->paths->count);
1341 vty_out(vty, "Nexthop count: %u\n", route->nh_list->count);
1342 }
1343
1344 /* Nexthops */
1345 if (use_json)
1346 json_array_next_hops = json_object_new_array();
1347 else
1348 vty_out(vty, "Nexthop:\n");
1349
1350 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
1351 struct interface *ifp;
1352 ifp = if_lookup_by_index_all_vrf(nh->ifindex);
1353 /* nexthop */
1354 if (use_json) {
1355 inet_ntop(AF_INET6, &nh->address, nexthop,
1356 sizeof(nexthop));
1357 json_next_hop = json_object_new_object();
1358 json_object_string_add(json_next_hop, "nextHop",
1359 nexthop);
1360 json_object_string_add(json_next_hop, "interfaceName",
1361 ifp->name);
1362 json_object_array_add(json_array_next_hops,
1363 json_next_hop);
1364 } else
1365 vty_out(vty, " %pI6 %.*s\n", &nh->address, IFNAMSIZ,
1366 ifp->name);
1367 }
1368 if (use_json) {
1369 json_object_object_add(json_route, "nextHops",
1370 json_array_next_hops);
1371 json_object_object_add(json_routes, destination, json_route);
1372 } else
1373 vty_out(vty, "\n");
1374 }
1375
1376 static void ospf6_route_show_table_summary(struct vty *vty,
1377 struct ospf6_route_table *table,
1378 json_object *json, bool use_json)
1379 {
1380 struct ospf6_route *route, *prev = NULL;
1381 int i, pathtype[OSPF6_PATH_TYPE_MAX];
1382 unsigned int number = 0;
1383 int nh_count = 0, nhinval = 0, ecmp = 0;
1384 int alternative = 0, destination = 0;
1385 char path_str[30];
1386
1387 for (i = 0; i < OSPF6_PATH_TYPE_MAX; i++)
1388 pathtype[i] = 0;
1389
1390 for (route = ospf6_route_head(table); route;
1391 route = ospf6_route_next(route)) {
1392 if (prev == NULL || !ospf6_route_is_same(prev, route))
1393 destination++;
1394 else
1395 alternative++;
1396 nh_count = ospf6_num_nexthops(route->nh_list);
1397 if (!nh_count)
1398 nhinval++;
1399 else if (nh_count > 1)
1400 ecmp++;
1401 pathtype[route->path.type]++;
1402 number++;
1403
1404 prev = route;
1405 }
1406
1407 assert(number == table->count);
1408 if (use_json) {
1409 json_object_int_add(json, "numberOfOspfv3Routes", number);
1410 json_object_int_add(json, "numberOfDestination", destination);
1411 json_object_int_add(json, "numberOfAlternativeRoutes",
1412 alternative);
1413 json_object_int_add(json, "numberOfEcmp", ecmp);
1414 } else {
1415 vty_out(vty, "Number of OSPFv3 routes: %d\n", number);
1416 vty_out(vty, "Number of Destination: %d\n", destination);
1417 vty_out(vty, "Number of Alternative routes: %d\n", alternative);
1418 vty_out(vty, "Number of Equal Cost Multi Path: %d\n", ecmp);
1419 }
1420 for (i = OSPF6_PATH_TYPE_INTRA; i <= OSPF6_PATH_TYPE_EXTERNAL2; i++) {
1421 if (use_json) {
1422 snprintf(path_str, sizeof(path_str), "numberOf%sRoutes",
1423 OSPF6_PATH_TYPE_JSON(i));
1424 json_object_int_add(json, path_str, pathtype[i]);
1425 } else
1426 vty_out(vty, "Number of %s routes: %d\n",
1427 OSPF6_PATH_TYPE_NAME(i), pathtype[i]);
1428 }
1429 }
1430
1431 static void ospf6_route_show_table_prefix(struct vty *vty,
1432 struct prefix *prefix,
1433 struct ospf6_route_table *table,
1434 json_object *json, bool use_json)
1435 {
1436 struct ospf6_route *route;
1437 json_object *json_routes = NULL;
1438
1439 route = ospf6_route_lookup(prefix, table);
1440 if (route == NULL)
1441 return;
1442
1443 if (use_json)
1444 json_routes = json_object_new_object();
1445 ospf6_route_lock(route);
1446 while (route && ospf6_route_is_prefix(prefix, route)) {
1447 /* Specifying a prefix will always display details */
1448 ospf6_route_show_detail(vty, route, json_routes, use_json);
1449 route = ospf6_route_next(route);
1450 }
1451
1452 if (use_json)
1453 json_object_object_add(json, "routes", json_routes);
1454 if (route)
1455 ospf6_route_unlock(route);
1456 }
1457
1458 static void ospf6_route_show_table_address(struct vty *vty,
1459 struct prefix *prefix,
1460 struct ospf6_route_table *table,
1461 json_object *json, bool use_json)
1462 {
1463 struct ospf6_route *route;
1464 json_object *json_routes = NULL;
1465
1466 route = ospf6_route_lookup_bestmatch(prefix, table);
1467 if (route == NULL)
1468 return;
1469
1470 if (use_json)
1471 json_routes = json_object_new_object();
1472 prefix = &route->prefix;
1473 ospf6_route_lock(route);
1474 while (route && ospf6_route_is_prefix(prefix, route)) {
1475 /* Specifying a prefix will always display details */
1476 ospf6_route_show_detail(vty, route, json_routes, use_json);
1477 route = ospf6_route_next(route);
1478 }
1479 if (use_json)
1480 json_object_object_add(json, "routes", json_routes);
1481 if (route)
1482 ospf6_route_unlock(route);
1483 }
1484
1485 static void ospf6_route_show_table_match(struct vty *vty, int detail,
1486 struct prefix *prefix,
1487 struct ospf6_route_table *table,
1488 json_object *json, bool use_json)
1489 {
1490 struct ospf6_route *route;
1491 json_object *json_routes = NULL;
1492
1493 assert(prefix->family);
1494
1495 route = ospf6_route_match_head(prefix, table);
1496 if (use_json)
1497 json_routes = json_object_new_object();
1498 while (route) {
1499 if (detail)
1500 ospf6_route_show_detail(vty, route, json_routes,
1501 use_json);
1502 else
1503 ospf6_route_show(vty, route, json_routes, use_json);
1504 route = ospf6_route_match_next(prefix, route);
1505 }
1506 if (use_json)
1507 json_object_object_add(json, "routes", json_routes);
1508 }
1509
1510 static void ospf6_route_show_table_type(struct vty *vty, int detail,
1511 uint8_t type,
1512 struct ospf6_route_table *table,
1513 json_object *json, bool use_json)
1514 {
1515 struct ospf6_route *route;
1516 json_object *json_routes = NULL;
1517
1518 route = ospf6_route_head(table);
1519 if (use_json)
1520 json_routes = json_object_new_object();
1521 while (route) {
1522 if (route->path.type == type) {
1523 if (detail)
1524 ospf6_route_show_detail(vty, route, json_routes,
1525 use_json);
1526 else
1527 ospf6_route_show(vty, route, json_routes,
1528 use_json);
1529 }
1530 route = ospf6_route_next(route);
1531 }
1532 if (use_json)
1533 json_object_object_add(json, "routes", json_routes);
1534 }
1535
1536 static void ospf6_route_show_table(struct vty *vty, int detail,
1537 struct ospf6_route_table *table,
1538 json_object *json, bool use_json)
1539 {
1540 struct ospf6_route *route;
1541 json_object *json_routes = NULL;
1542
1543 route = ospf6_route_head(table);
1544 if (use_json)
1545 json_routes = json_object_new_object();
1546 while (route) {
1547 if (detail)
1548 ospf6_route_show_detail(vty, route, json_routes,
1549 use_json);
1550 else
1551 ospf6_route_show(vty, route, json_routes, use_json);
1552 route = ospf6_route_next(route);
1553 }
1554 if (use_json)
1555 json_object_object_add(json, "routes", json_routes);
1556 }
1557
1558 int ospf6_route_table_show(struct vty *vty, int argc_start, int argc,
1559 struct cmd_token **argv,
1560 struct ospf6_route_table *table, bool use_json)
1561 {
1562 int summary = 0;
1563 int match = 0;
1564 int detail = 0;
1565 int slash = 0;
1566 int isprefix = 0;
1567 int i, ret;
1568 struct prefix prefix;
1569 uint8_t type = 0;
1570 int arg_end = use_json ? (argc - 1) : argc;
1571 json_object *json = NULL;
1572
1573 memset(&prefix, 0, sizeof(struct prefix));
1574
1575 if (use_json)
1576 json = json_object_new_object();
1577
1578 for (i = argc_start; i < arg_end; i++) {
1579 if (strmatch(argv[i]->text, "summary")) {
1580 summary++;
1581 continue;
1582 }
1583
1584 if (strmatch(argv[i]->text, "intra-area")) {
1585 type = OSPF6_PATH_TYPE_INTRA;
1586 continue;
1587 }
1588
1589 if (strmatch(argv[i]->text, "inter-area")) {
1590 type = OSPF6_PATH_TYPE_INTER;
1591 continue;
1592 }
1593
1594 if (strmatch(argv[i]->text, "external-1")) {
1595 type = OSPF6_PATH_TYPE_EXTERNAL1;
1596 continue;
1597 }
1598
1599 if (strmatch(argv[i]->text, "external-2")) {
1600 type = OSPF6_PATH_TYPE_EXTERNAL2;
1601 continue;
1602 }
1603
1604 if (strmatch(argv[i]->text, "detail")) {
1605 detail++;
1606 continue;
1607 }
1608
1609 if (strmatch(argv[i]->text, "match")) {
1610 match++;
1611 continue;
1612 }
1613
1614 ret = str2prefix(argv[i]->arg, &prefix);
1615 if (ret == 1 && prefix.family == AF_INET6) {
1616 isprefix++;
1617 if (strchr(argv[i]->arg, '/'))
1618 slash++;
1619 continue;
1620 }
1621 if (use_json)
1622 json_object_string_add(json, "malformedArgument",
1623 argv[i]->arg);
1624 else
1625 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1626
1627 return CMD_SUCCESS;
1628 }
1629
1630 /* Give summary of this route table */
1631 if (summary) {
1632 ospf6_route_show_table_summary(vty, table, json, use_json);
1633 if (use_json) {
1634 vty_out(vty, "%s\n",
1635 json_object_to_json_string_ext(
1636 json, JSON_C_TO_STRING_PRETTY));
1637 json_object_free(json);
1638 }
1639 return CMD_SUCCESS;
1640 }
1641
1642 /* Give exact prefix-match route */
1643 if (isprefix && !match) {
1644 /* If exact address, give best matching route */
1645 if (!slash)
1646 ospf6_route_show_table_address(vty, &prefix, table,
1647 json, use_json);
1648 else
1649 ospf6_route_show_table_prefix(vty, &prefix, table, json,
1650 use_json);
1651
1652 if (use_json) {
1653 vty_out(vty, "%s\n",
1654 json_object_to_json_string_ext(
1655 json, JSON_C_TO_STRING_PRETTY));
1656 json_object_free(json);
1657 }
1658 return CMD_SUCCESS;
1659 }
1660
1661 if (match)
1662 ospf6_route_show_table_match(vty, detail, &prefix, table, json,
1663 use_json);
1664 else if (type)
1665 ospf6_route_show_table_type(vty, detail, type, table, json,
1666 use_json);
1667 else
1668 ospf6_route_show_table(vty, detail, table, json, use_json);
1669
1670 if (use_json) {
1671 vty_out(vty, "%s\n",
1672 json_object_to_json_string_ext(
1673 json, JSON_C_TO_STRING_PRETTY));
1674 json_object_free(json);
1675 }
1676 return CMD_SUCCESS;
1677 }
1678
1679 static void ospf6_linkstate_show_header(struct vty *vty)
1680 {
1681 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %s\n", "Type", "Router-ID",
1682 "Net-ID", "Rtr-Bits", "Options", "Cost");
1683 }
1684
1685 static void ospf6_linkstate_show(struct vty *vty, struct ospf6_route *route)
1686 {
1687 uint32_t router, id;
1688 char routername[16], idname[16], rbits[16], options[16];
1689
1690 router = ospf6_linkstate_prefix_adv_router(&route->prefix);
1691 inet_ntop(AF_INET, &router, routername, sizeof(routername));
1692 id = ospf6_linkstate_prefix_id(&route->prefix);
1693 inet_ntop(AF_INET, &id, idname, sizeof(idname));
1694
1695 ospf6_capability_printbuf(route->path.router_bits, rbits,
1696 sizeof(rbits));
1697 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1698
1699 if (ntohl(id))
1700 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Network",
1701 routername, idname, rbits, options,
1702 (unsigned long)route->path.cost);
1703 else
1704 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Router",
1705 routername, idname, rbits, options,
1706 (unsigned long)route->path.cost);
1707 }
1708
1709
1710 static void ospf6_linkstate_show_table_exact(struct vty *vty,
1711 struct prefix *prefix,
1712 struct ospf6_route_table *table)
1713 {
1714 struct ospf6_route *route;
1715
1716 route = ospf6_route_lookup(prefix, table);
1717 if (route == NULL)
1718 return;
1719
1720 ospf6_route_lock(route);
1721 while (route && ospf6_route_is_prefix(prefix, route)) {
1722 /* Specifying a prefix will always display details */
1723 ospf6_route_show_detail(vty, route, NULL, false);
1724 route = ospf6_route_next(route);
1725 }
1726 if (route)
1727 ospf6_route_unlock(route);
1728 }
1729
1730 static void ospf6_linkstate_show_table(struct vty *vty, int detail,
1731 struct ospf6_route_table *table)
1732 {
1733 struct ospf6_route *route;
1734
1735 if (!detail)
1736 ospf6_linkstate_show_header(vty);
1737
1738 route = ospf6_route_head(table);
1739 while (route) {
1740 if (detail)
1741 ospf6_route_show_detail(vty, route, NULL, false);
1742 else
1743 ospf6_linkstate_show(vty, route);
1744 route = ospf6_route_next(route);
1745 }
1746 }
1747
1748 int ospf6_linkstate_table_show(struct vty *vty, int idx_ipv4, int argc,
1749 struct cmd_token **argv,
1750 struct ospf6_route_table *table)
1751 {
1752 int detail = 0;
1753 int is_id = 0;
1754 int is_router = 0;
1755 int i, ret;
1756 struct prefix router, id, prefix;
1757
1758 memset(&router, 0, sizeof(struct prefix));
1759 memset(&id, 0, sizeof(struct prefix));
1760 memset(&prefix, 0, sizeof(struct prefix));
1761
1762 for (i = idx_ipv4; i < argc; i++) {
1763 if (strmatch(argv[i]->text, "detail")) {
1764 detail++;
1765 continue;
1766 }
1767
1768 if (!is_router) {
1769 ret = str2prefix(argv[i]->arg, &router);
1770 if (ret == 1 && router.family == AF_INET) {
1771 is_router++;
1772 continue;
1773 }
1774 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1775 return CMD_SUCCESS;
1776 }
1777
1778 if (!is_id) {
1779 ret = str2prefix(argv[i]->arg, &id);
1780 if (ret == 1 && id.family == AF_INET) {
1781 is_id++;
1782 continue;
1783 }
1784 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1785 return CMD_SUCCESS;
1786 }
1787
1788 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1789 return CMD_SUCCESS;
1790 }
1791
1792 if (is_router)
1793 ospf6_linkstate_prefix(router.u.prefix4.s_addr,
1794 id.u.prefix4.s_addr, &prefix);
1795
1796 if (prefix.family)
1797 ospf6_linkstate_show_table_exact(vty, &prefix, table);
1798 else
1799 ospf6_linkstate_show_table(vty, detail, table);
1800
1801 return CMD_SUCCESS;
1802 }
1803
1804
1805 void ospf6_brouter_show_header(struct vty *vty)
1806 {
1807 vty_out(vty, "%-15s %-8s %-14s %-10s %-15s\n", "Router-ID", "Rtr-Bits",
1808 "Options", "Path-Type", "Area");
1809 }
1810
1811 void ospf6_brouter_show(struct vty *vty, struct ospf6_route *route)
1812 {
1813 uint32_t adv_router;
1814 char adv[16], rbits[16], options[16], area[16];
1815
1816 adv_router = ospf6_linkstate_prefix_adv_router(&route->prefix);
1817 inet_ntop(AF_INET, &adv_router, adv, sizeof(adv));
1818 ospf6_capability_printbuf(route->path.router_bits, rbits,
1819 sizeof(rbits));
1820 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1821 inet_ntop(AF_INET, &route->path.area_id, area, sizeof(area));
1822
1823 /* vty_out (vty, "%-15s %-8s %-14s %-10s %-15s\n",
1824 "Router-ID", "Rtr-Bits", "Options", "Path-Type", "Area"); */
1825 vty_out(vty, "%-15s %-8s %-14s %-10s %-15s\n", adv, rbits, options,
1826 OSPF6_PATH_TYPE_NAME(route->path.type), area);
1827 }
1828
1829 DEFUN (debug_ospf6_route,
1830 debug_ospf6_route_cmd,
1831 "debug ospf6 route <table|intra-area|inter-area|memory>",
1832 DEBUG_STR
1833 OSPF6_STR
1834 "Debug routes\n"
1835 "Debug route table calculation\n"
1836 "Debug intra-area route calculation\n"
1837 "Debug inter-area route calculation\n"
1838 "Debug route memory use\n"
1839 )
1840 {
1841 int idx_type = 3;
1842 unsigned char level = 0;
1843
1844 if (!strcmp(argv[idx_type]->text, "table"))
1845 level = OSPF6_DEBUG_ROUTE_TABLE;
1846 else if (!strcmp(argv[idx_type]->text, "intra-area"))
1847 level = OSPF6_DEBUG_ROUTE_INTRA;
1848 else if (!strcmp(argv[idx_type]->text, "inter-area"))
1849 level = OSPF6_DEBUG_ROUTE_INTER;
1850 else if (!strcmp(argv[idx_type]->text, "memory"))
1851 level = OSPF6_DEBUG_ROUTE_MEMORY;
1852 OSPF6_DEBUG_ROUTE_ON(level);
1853 return CMD_SUCCESS;
1854 }
1855
1856 DEFUN (no_debug_ospf6_route,
1857 no_debug_ospf6_route_cmd,
1858 "no debug ospf6 route <table|intra-area|inter-area|memory>",
1859 NO_STR
1860 DEBUG_STR
1861 OSPF6_STR
1862 "Debug routes\n"
1863 "Debug route table calculation\n"
1864 "Debug intra-area route calculation\n"
1865 "Debug inter-area route calculation\n"
1866 "Debug route memory use\n")
1867 {
1868 int idx_type = 4;
1869 unsigned char level = 0;
1870
1871 if (!strcmp(argv[idx_type]->text, "table"))
1872 level = OSPF6_DEBUG_ROUTE_TABLE;
1873 else if (!strcmp(argv[idx_type]->text, "intra-area"))
1874 level = OSPF6_DEBUG_ROUTE_INTRA;
1875 else if (!strcmp(argv[idx_type]->text, "inter-area"))
1876 level = OSPF6_DEBUG_ROUTE_INTER;
1877 else if (!strcmp(argv[idx_type]->text, "memory"))
1878 level = OSPF6_DEBUG_ROUTE_MEMORY;
1879 OSPF6_DEBUG_ROUTE_OFF(level);
1880 return CMD_SUCCESS;
1881 }
1882
1883 int config_write_ospf6_debug_route(struct vty *vty)
1884 {
1885 if (IS_OSPF6_DEBUG_ROUTE(TABLE))
1886 vty_out(vty, "debug ospf6 route table\n");
1887 if (IS_OSPF6_DEBUG_ROUTE(INTRA))
1888 vty_out(vty, "debug ospf6 route intra-area\n");
1889 if (IS_OSPF6_DEBUG_ROUTE(INTER))
1890 vty_out(vty, "debug ospf6 route inter-area\n");
1891 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
1892 vty_out(vty, "debug ospf6 route memory\n");
1893
1894 return 0;
1895 }
1896
1897 void install_element_ospf6_debug_route(void)
1898 {
1899 install_element(ENABLE_NODE, &debug_ospf6_route_cmd);
1900 install_element(ENABLE_NODE, &no_debug_ospf6_route_cmd);
1901 install_element(CONFIG_NODE, &debug_ospf6_route_cmd);
1902 install_element(CONFIG_NODE, &no_debug_ospf6_route_cmd);
1903 }