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