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