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