]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_route.c
Merge pull request #2166 from qlyoung/docuser
[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 if (nh)
178 XFREE(MTYPE_OSPF6_NEXTHOP, nh);
179 }
180
181 void ospf6_clear_nexthops(struct list *nh_list)
182 {
183 struct listnode *node;
184 struct ospf6_nexthop *nh;
185
186 if (nh_list) {
187 for (ALL_LIST_ELEMENTS_RO(nh_list, node, nh))
188 ospf6_nexthop_clear(nh);
189 }
190 }
191
192 static struct ospf6_nexthop *
193 ospf6_route_find_nexthop(struct list *nh_list, struct ospf6_nexthop *nh_match)
194 {
195 struct listnode *node;
196 struct ospf6_nexthop *nh;
197
198 if (nh_list && nh_match) {
199 for (ALL_LIST_ELEMENTS_RO(nh_list, node, nh)) {
200 if (ospf6_nexthop_is_same(nh, nh_match))
201 return (nh);
202 }
203 }
204
205 return (NULL);
206 }
207
208 void ospf6_copy_nexthops(struct list *dst, struct list *src)
209 {
210 struct ospf6_nexthop *nh_new, *nh;
211 struct listnode *node;
212
213 if (dst && src) {
214 for (ALL_LIST_ELEMENTS_RO(src, node, nh)) {
215 if (ospf6_nexthop_is_set(nh)) {
216 nh_new = ospf6_nexthop_create();
217 ospf6_nexthop_copy(nh_new, nh);
218 listnode_add_sort(dst, nh_new);
219 }
220 }
221 }
222 }
223
224 void ospf6_merge_nexthops(struct list *dst, struct list *src)
225 {
226 struct listnode *node;
227 struct ospf6_nexthop *nh, *nh_new;
228
229 if (src && dst) {
230 for (ALL_LIST_ELEMENTS_RO(src, node, nh)) {
231 if (!ospf6_route_find_nexthop(dst, nh)) {
232 nh_new = ospf6_nexthop_create();
233 ospf6_nexthop_copy(nh_new, nh);
234 listnode_add_sort(dst, nh_new);
235 }
236 }
237 }
238 }
239
240 int ospf6_route_cmp_nexthops(struct ospf6_route *a, struct ospf6_route *b)
241 {
242 struct listnode *anode, *bnode;
243 struct ospf6_nexthop *anh, *bnh;
244 bool identical = false;
245
246 if (a && b) {
247 if (listcount(a->nh_list) == listcount(b->nh_list)) {
248 for (ALL_LIST_ELEMENTS_RO(a->nh_list, anode, anh)) {
249 identical = false;
250 for (ALL_LIST_ELEMENTS_RO(b->nh_list, bnode,
251 bnh)) {
252 if (ospf6_nexthop_is_same(anh, bnh))
253 identical = true;
254 }
255 /* Currnet List A element not found List B
256 * Non-Identical lists return */
257 if (identical == false)
258 return 1;
259 }
260 return 0;
261 } else
262 return 1;
263 }
264 /* One of the routes doesn't exist ? */
265 return (1);
266 }
267
268 int ospf6_num_nexthops(struct list *nh_list)
269 {
270 return (listcount(nh_list));
271 }
272
273 void ospf6_add_nexthop(struct list *nh_list, int ifindex, struct in6_addr *addr)
274 {
275 struct ospf6_nexthop *nh;
276 struct ospf6_nexthop nh_match;
277
278 if (nh_list) {
279 nh_match.ifindex = ifindex;
280 if (addr != NULL)
281 memcpy(&nh_match.address, addr,
282 sizeof(struct in6_addr));
283 else
284 memset(&nh_match.address, 0, sizeof(struct in6_addr));
285
286 if (!ospf6_route_find_nexthop(nh_list, &nh_match)) {
287 nh = ospf6_nexthop_create();
288 ospf6_nexthop_copy(nh, &nh_match);
289 listnode_add(nh_list, nh);
290 }
291 }
292 }
293
294 void ospf6_route_zebra_copy_nexthops(struct ospf6_route *route,
295 struct zapi_nexthop nexthops[],
296 int entries)
297 {
298 struct ospf6_nexthop *nh;
299 struct listnode *node;
300 char buf[64];
301 int i;
302
303 if (route) {
304 i = 0;
305 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
306 if (IS_OSPF6_DEBUG_ZEBRA(SEND)) {
307 const char *ifname;
308 inet_ntop(AF_INET6, &nh->address, buf,
309 sizeof(buf));
310 ifname = ifindex2ifname(nh->ifindex,
311 VRF_DEFAULT);
312 zlog_debug(" nexthop: %s%%%.*s(%d)", buf,
313 IFNAMSIZ, ifname, nh->ifindex);
314 }
315 if (i >= entries)
316 return;
317
318 nexthops[i].vrf_id = VRF_DEFAULT;
319 nexthops[i].ifindex = nh->ifindex;
320 if (!IN6_IS_ADDR_UNSPECIFIED(&nh->address)) {
321 nexthops[i].gate.ipv6 = nh->address;
322 nexthops[i].type = NEXTHOP_TYPE_IPV6_IFINDEX;
323 } else
324 nexthops[i].type = NEXTHOP_TYPE_IFINDEX;
325 i++;
326 }
327 }
328 }
329
330 int ospf6_route_get_first_nh_index(struct ospf6_route *route)
331 {
332 struct ospf6_nexthop *nh;
333
334 if (route) {
335 if ((nh = (struct ospf6_nexthop *)listhead(route->nh_list)))
336 return (nh->ifindex);
337 }
338
339 return (-1);
340 }
341
342 int ospf6_nexthop_cmp(struct ospf6_nexthop *a, struct ospf6_nexthop *b)
343 {
344 if (a->ifindex < b->ifindex)
345 return -1;
346 else if (a->ifindex > b->ifindex)
347 return 1;
348 else
349 return memcmp(&a->address, &b->address,
350 sizeof(struct in6_addr));
351
352 return 0;
353 }
354
355 static int ospf6_path_cmp(struct ospf6_path *a, struct ospf6_path *b)
356 {
357 if (a->origin.adv_router < b->origin.adv_router)
358 return -1;
359 else if (a->origin.adv_router > b->origin.adv_router)
360 return 1;
361 else
362 return 0;
363 }
364
365 void ospf6_path_free(struct ospf6_path *op)
366 {
367 if (op->nh_list)
368 list_delete_and_null(&op->nh_list);
369 XFREE(MTYPE_OSPF6_PATH, op);
370 }
371
372 struct ospf6_path *ospf6_path_dup(struct ospf6_path *path)
373 {
374 struct ospf6_path *new;
375
376 new = XCALLOC(MTYPE_OSPF6_PATH, sizeof(struct ospf6_path));
377 memcpy(new, path, sizeof(struct ospf6_path));
378 new->nh_list = list_new();
379 new->nh_list->cmp = (int (*)(void *, void *))ospf6_nexthop_cmp;
380 new->nh_list->del = (void (*)(void *))ospf6_nexthop_delete;
381
382 return new;
383 }
384
385 void ospf6_copy_paths(struct list *dst, struct list *src)
386 {
387 struct ospf6_path *path_new, *path;
388 struct listnode *node;
389
390 if (dst && src) {
391 for (ALL_LIST_ELEMENTS_RO(src, node, path)) {
392 path_new = ospf6_path_dup(path);
393 ospf6_copy_nexthops(path_new->nh_list, path->nh_list);
394 listnode_add_sort(dst, path_new);
395 }
396 }
397 }
398
399 struct ospf6_route *ospf6_route_create(void)
400 {
401 struct ospf6_route *route;
402 route = XCALLOC(MTYPE_OSPF6_ROUTE, sizeof(struct ospf6_route));
403 route->nh_list = list_new();
404 route->nh_list->cmp = (int (*)(void *, void *))ospf6_nexthop_cmp;
405 route->nh_list->del = (void (*)(void *))ospf6_nexthop_delete;
406 route->paths = list_new();
407 route->paths->cmp = (int (*)(void *, void *))ospf6_path_cmp;
408 route->paths->del = (void (*)(void *))ospf6_path_free;
409 return route;
410 }
411
412 void ospf6_route_delete(struct ospf6_route *route)
413 {
414 if (route) {
415 if (route->nh_list)
416 list_delete_and_null(&route->nh_list);
417 if (route->paths)
418 list_delete_and_null(&route->paths);
419 XFREE(MTYPE_OSPF6_ROUTE, route);
420 }
421 }
422
423 struct ospf6_route *ospf6_route_copy(struct ospf6_route *route)
424 {
425 struct ospf6_route *new;
426
427 new = ospf6_route_create();
428 new->type = route->type;
429 memcpy(&new->prefix, &route->prefix, sizeof(struct prefix));
430 new->installed = route->installed;
431 new->changed = route->changed;
432 new->flag = route->flag;
433 new->route_option = route->route_option;
434 new->linkstate_id = route->linkstate_id;
435 new->path = route->path;
436 ospf6_copy_nexthops(new->nh_list, route->nh_list);
437 ospf6_copy_paths(new->paths, route->paths);
438 new->rnode = NULL;
439 new->prev = NULL;
440 new->next = NULL;
441 new->table = NULL;
442 new->lock = 0;
443 return new;
444 }
445
446 void ospf6_route_lock(struct ospf6_route *route)
447 {
448 route->lock++;
449 }
450
451 void ospf6_route_unlock(struct ospf6_route *route)
452 {
453 assert(route->lock > 0);
454 route->lock--;
455 if (route->lock == 0) {
456 /* Can't detach from the table until here
457 because ospf6_route_next () will use
458 the 'route->table' pointer for logging */
459 route->table = NULL;
460 ospf6_route_delete(route);
461 }
462 }
463
464 /* Route compare function. If ra is more preferred, it returns
465 less than 0. If rb is more preferred returns greater than 0.
466 Otherwise (neither one is preferred), returns 0 */
467 int ospf6_route_cmp(struct ospf6_route *ra, struct ospf6_route *rb)
468 {
469 assert(ospf6_route_is_same(ra, rb));
470 assert(OSPF6_PATH_TYPE_NONE < ra->path.type
471 && ra->path.type < OSPF6_PATH_TYPE_MAX);
472 assert(OSPF6_PATH_TYPE_NONE < rb->path.type
473 && rb->path.type < OSPF6_PATH_TYPE_MAX);
474
475 if (ra->type != rb->type)
476 return (ra->type - rb->type);
477
478 if (ra->path.type != rb->path.type)
479 return (ra->path.type - rb->path.type);
480
481 if (ra->path.type == OSPF6_PATH_TYPE_EXTERNAL2) {
482 if (ra->path.u.cost_e2 != rb->path.u.cost_e2)
483 return (ra->path.u.cost_e2 - rb->path.u.cost_e2);
484 else
485 return (ra->path.cost - rb->path.cost);
486 } else {
487 if (ra->path.cost != rb->path.cost)
488 return (ra->path.cost - rb->path.cost);
489 }
490
491 if (ra->path.area_id != rb->path.area_id)
492 return (ntohl(ra->path.area_id) - ntohl(rb->path.area_id));
493
494 return 0;
495 }
496
497 struct ospf6_route *ospf6_route_lookup(struct prefix *prefix,
498 struct ospf6_route_table *table)
499 {
500 struct route_node *node;
501 struct ospf6_route *route;
502
503 node = route_node_lookup(table->table, prefix);
504 if (node == NULL)
505 return NULL;
506
507 route = (struct ospf6_route *)node->info;
508 route_unlock_node(node); /* to free the lookup lock */
509 return route;
510 }
511
512 struct ospf6_route *
513 ospf6_route_lookup_identical(struct ospf6_route *route,
514 struct ospf6_route_table *table)
515 {
516 struct ospf6_route *target;
517
518 for (target = ospf6_route_lookup(&route->prefix, table); target;
519 target = target->next) {
520 if (target->type == route->type
521 && (memcmp(&target->prefix, &route->prefix,
522 sizeof(struct prefix))
523 == 0)
524 && target->path.type == route->path.type
525 && target->path.cost == route->path.cost
526 && target->path.u.cost_e2 == route->path.u.cost_e2
527 && ospf6_route_cmp_nexthops(target, route) == 0)
528 return target;
529 }
530 return NULL;
531 }
532
533 struct ospf6_route *
534 ospf6_route_lookup_bestmatch(struct prefix *prefix,
535 struct ospf6_route_table *table)
536 {
537 struct route_node *node;
538 struct ospf6_route *route;
539
540 node = route_node_match(table->table, prefix);
541 if (node == NULL)
542 return NULL;
543 route_unlock_node(node);
544
545 route = (struct ospf6_route *)node->info;
546 return route;
547 }
548
549 #ifdef DEBUG
550 static void route_table_assert(struct ospf6_route_table *table)
551 {
552 struct ospf6_route *prev, *r, *next;
553 char buf[PREFIX2STR_BUFFER];
554 unsigned int link_error = 0, num = 0;
555
556 r = ospf6_route_head(table);
557 prev = NULL;
558 while (r) {
559 if (r->prev != prev)
560 link_error++;
561
562 next = ospf6_route_next(r);
563
564 if (r->next != next)
565 link_error++;
566
567 prev = r;
568 r = next;
569 }
570
571 for (r = ospf6_route_head(table); r; r = ospf6_route_next(r))
572 num++;
573
574 if (link_error == 0 && num == table->count)
575 return;
576
577 zlog_err("PANIC !!");
578 zlog_err("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->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",
929 ospf6_route_table_name(route->table),
930 (void *)route->table, (void *)route->prev,
931 (void *)route, (void *)route->next);
932
933 ospf6_route_unlock(route);
934 if (next)
935 ospf6_route_lock(next);
936
937 return next;
938 }
939
940 struct ospf6_route *ospf6_route_best_next(struct ospf6_route *route)
941 {
942 struct route_node *rnode;
943 struct ospf6_route *next;
944
945 ospf6_route_unlock(route);
946
947 rnode = route->rnode;
948 route_lock_node(rnode);
949 rnode = route_next(rnode);
950 while (rnode && rnode->info == NULL)
951 rnode = route_next(rnode);
952 if (rnode == NULL)
953 return NULL;
954 route_unlock_node(rnode);
955
956 assert(rnode->info);
957 next = (struct ospf6_route *)rnode->info;
958 ospf6_route_lock(next);
959 return next;
960 }
961
962 struct ospf6_route *ospf6_route_match_head(struct prefix *prefix,
963 struct ospf6_route_table *table)
964 {
965 struct route_node *node;
966 struct ospf6_route *route;
967
968 /* Walk down tree. */
969 node = table->table->top;
970 while (node && node->p.prefixlen < prefix->prefixlen
971 && prefix_match(&node->p, prefix))
972 node = node->link[prefix_bit(&prefix->u.prefix,
973 node->p.prefixlen)];
974
975 if (node)
976 route_lock_node(node);
977 while (node && node->info == NULL)
978 node = route_next(node);
979 if (node == NULL)
980 return NULL;
981 route_unlock_node(node);
982
983 if (!prefix_match(prefix, &node->p))
984 return NULL;
985
986 route = node->info;
987 ospf6_route_lock(route);
988 return route;
989 }
990
991 struct ospf6_route *ospf6_route_match_next(struct prefix *prefix,
992 struct ospf6_route *route)
993 {
994 struct ospf6_route *next;
995
996 next = ospf6_route_next(route);
997 if (next && !prefix_match(prefix, &next->prefix)) {
998 ospf6_route_unlock(next);
999 next = NULL;
1000 }
1001
1002 return next;
1003 }
1004
1005 void ospf6_route_remove_all(struct ospf6_route_table *table)
1006 {
1007 struct ospf6_route *route;
1008 for (route = ospf6_route_head(table); route;
1009 route = ospf6_route_next(route))
1010 ospf6_route_remove(route, table);
1011 }
1012
1013 struct ospf6_route_table *ospf6_route_table_create(int s, int t)
1014 {
1015 struct ospf6_route_table *new;
1016 new = XCALLOC(MTYPE_OSPF6_ROUTE, sizeof(struct ospf6_route_table));
1017 new->table = route_table_init();
1018 new->scope_type = s;
1019 new->table_type = t;
1020 return new;
1021 }
1022
1023 void ospf6_route_table_delete(struct ospf6_route_table *table)
1024 {
1025 ospf6_route_remove_all(table);
1026 bf_free(table->idspace);
1027 route_table_finish(table->table);
1028 XFREE(MTYPE_OSPF6_ROUTE, table);
1029 }
1030
1031
1032 /* VTY commands */
1033 void ospf6_route_show(struct vty *vty, struct ospf6_route *route)
1034 {
1035 int i;
1036 char destination[PREFIX2STR_BUFFER], nexthop[64];
1037 char duration[64];
1038 const char *ifname;
1039 struct timeval now, res;
1040 struct listnode *node;
1041 struct ospf6_nexthop *nh;
1042
1043 monotime(&now);
1044 timersub(&now, &route->changed, &res);
1045 timerstring(&res, duration, sizeof(duration));
1046
1047 /* destination */
1048 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
1049 ospf6_linkstate_prefix2str(&route->prefix, destination,
1050 sizeof(destination));
1051 else if (route->type == OSPF6_DEST_TYPE_ROUTER)
1052 inet_ntop(route->prefix.family, &route->prefix.u.prefix,
1053 destination, sizeof(destination));
1054 else
1055 prefix2str(&route->prefix, destination, sizeof(destination));
1056
1057 i = 0;
1058 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
1059 /* nexthop */
1060 inet_ntop(AF_INET6, &nh->address, nexthop, sizeof(nexthop));
1061 ifname = ifindex2ifname(nh->ifindex, VRF_DEFAULT);
1062
1063 if (!i) {
1064 vty_out(vty, "%c%1s %2s %-30s %-25s %6.*s %s\n",
1065 (ospf6_route_is_best(route) ? '*' : ' '),
1066 OSPF6_DEST_TYPE_SUBSTR(route->type),
1067 OSPF6_PATH_TYPE_SUBSTR(route->path.type),
1068 destination, nexthop, IFNAMSIZ, ifname,
1069 duration);
1070 i++;
1071 } else
1072 vty_out(vty, "%c%1s %2s %-30s %-25s %6.*s %s\n", ' ',
1073 "", "", "", nexthop, IFNAMSIZ, ifname, "");
1074 }
1075 }
1076
1077 void ospf6_route_show_detail(struct vty *vty, struct ospf6_route *route)
1078 {
1079 const char *ifname;
1080 char destination[PREFIX2STR_BUFFER], nexthop[64];
1081 char area_id[16], id[16], adv_router[16], capa[16], options[16];
1082 struct timeval now, res;
1083 char duration[64];
1084 struct listnode *node;
1085 struct ospf6_nexthop *nh;
1086
1087 monotime(&now);
1088
1089 /* destination */
1090 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
1091 ospf6_linkstate_prefix2str(&route->prefix, destination,
1092 sizeof(destination));
1093 else if (route->type == OSPF6_DEST_TYPE_ROUTER)
1094 inet_ntop(route->prefix.family, &route->prefix.u.prefix,
1095 destination, sizeof(destination));
1096 else
1097 prefix2str(&route->prefix, destination, sizeof(destination));
1098 vty_out(vty, "Destination: %s\n", destination);
1099
1100 /* destination type */
1101 vty_out(vty, "Destination type: %s\n",
1102 OSPF6_DEST_TYPE_NAME(route->type));
1103
1104 /* Time */
1105 timersub(&now, &route->installed, &res);
1106 timerstring(&res, duration, sizeof(duration));
1107 vty_out(vty, "Installed Time: %s ago\n", duration);
1108
1109 timersub(&now, &route->changed, &res);
1110 timerstring(&res, duration, sizeof(duration));
1111 vty_out(vty, " Changed Time: %s ago\n", duration);
1112
1113 /* Debugging info */
1114 vty_out(vty, "Lock: %d Flags: %s%s%s%s\n", route->lock,
1115 (CHECK_FLAG(route->flag, OSPF6_ROUTE_BEST) ? "B" : "-"),
1116 (CHECK_FLAG(route->flag, OSPF6_ROUTE_ADD) ? "A" : "-"),
1117 (CHECK_FLAG(route->flag, OSPF6_ROUTE_REMOVE) ? "R" : "-"),
1118 (CHECK_FLAG(route->flag, OSPF6_ROUTE_CHANGE) ? "C" : "-"));
1119 vty_out(vty, "Memory: prev: %p this: %p next: %p\n",
1120 (void *)route->prev, (void *)route, (void *)route->next);
1121
1122 /* Path section */
1123
1124 /* Area-ID */
1125 inet_ntop(AF_INET, &route->path.area_id, area_id, sizeof(area_id));
1126 vty_out(vty, "Associated Area: %s\n", area_id);
1127
1128 /* Path type */
1129 vty_out(vty, "Path Type: %s\n", OSPF6_PATH_TYPE_NAME(route->path.type));
1130
1131 /* LS Origin */
1132 inet_ntop(AF_INET, &route->path.origin.id, id, sizeof(id));
1133 inet_ntop(AF_INET, &route->path.origin.adv_router, adv_router,
1134 sizeof(adv_router));
1135 vty_out(vty, "LS Origin: %s Id: %s Adv: %s\n",
1136 ospf6_lstype_name(route->path.origin.type), id, adv_router);
1137
1138 /* Options */
1139 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1140 vty_out(vty, "Options: %s\n", options);
1141
1142 /* Router Bits */
1143 ospf6_capability_printbuf(route->path.router_bits, capa, sizeof(capa));
1144 vty_out(vty, "Router Bits: %s\n", capa);
1145
1146 /* Prefix Options */
1147 vty_out(vty, "Prefix Options: xxx\n");
1148
1149 /* Metrics */
1150 vty_out(vty, "Metric Type: %d\n", route->path.metric_type);
1151 vty_out(vty, "Metric: %d (%d)\n", route->path.cost,
1152 route->path.u.cost_e2);
1153
1154 vty_out(vty, "Paths count: %u\n", route->paths->count);
1155 vty_out(vty, "Nexthop count: %u\n", route->nh_list->count);
1156 /* Nexthops */
1157 vty_out(vty, "Nexthop:\n");
1158 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
1159 /* nexthop */
1160 inet_ntop(AF_INET6, &nh->address, nexthop, sizeof(nexthop));
1161 ifname = ifindex2ifname(nh->ifindex, VRF_DEFAULT);
1162 vty_out(vty, " %s %.*s\n", nexthop, IFNAMSIZ, ifname);
1163 }
1164 vty_out(vty, "\n");
1165 }
1166
1167 static void ospf6_route_show_table_summary(struct vty *vty,
1168 struct ospf6_route_table *table)
1169 {
1170 struct ospf6_route *route, *prev = NULL;
1171 int i, pathtype[OSPF6_PATH_TYPE_MAX];
1172 unsigned int number = 0;
1173 int nh_count = 0, nhinval = 0, ecmp = 0;
1174 int alternative = 0, destination = 0;
1175
1176 for (i = 0; i < OSPF6_PATH_TYPE_MAX; i++)
1177 pathtype[i] = 0;
1178
1179 for (route = ospf6_route_head(table); route;
1180 route = ospf6_route_next(route)) {
1181 if (prev == NULL || !ospf6_route_is_same(prev, route))
1182 destination++;
1183 else
1184 alternative++;
1185 nh_count = ospf6_num_nexthops(route->nh_list);
1186 if (!nh_count)
1187 nhinval++;
1188 else if (nh_count > 1)
1189 ecmp++;
1190 pathtype[route->path.type]++;
1191 number++;
1192
1193 prev = route;
1194 }
1195
1196 assert(number == table->count);
1197
1198 vty_out(vty, "Number of OSPFv3 routes: %d\n", number);
1199 vty_out(vty, "Number of Destination: %d\n", destination);
1200 vty_out(vty, "Number of Alternative routes: %d\n", alternative);
1201 vty_out(vty, "Number of Equal Cost Multi Path: %d\n", ecmp);
1202 for (i = OSPF6_PATH_TYPE_INTRA; i <= OSPF6_PATH_TYPE_EXTERNAL2; i++) {
1203 vty_out(vty, "Number of %s routes: %d\n",
1204 OSPF6_PATH_TYPE_NAME(i), pathtype[i]);
1205 }
1206 }
1207
1208 static void ospf6_route_show_table_prefix(struct vty *vty,
1209 struct prefix *prefix,
1210 struct ospf6_route_table *table)
1211 {
1212 struct ospf6_route *route;
1213
1214 route = ospf6_route_lookup(prefix, table);
1215 if (route == NULL)
1216 return;
1217
1218 ospf6_route_lock(route);
1219 while (route && ospf6_route_is_prefix(prefix, route)) {
1220 /* Specifying a prefix will always display details */
1221 ospf6_route_show_detail(vty, route);
1222 route = ospf6_route_next(route);
1223 }
1224 if (route)
1225 ospf6_route_unlock(route);
1226 }
1227
1228 static void ospf6_route_show_table_address(struct vty *vty,
1229 struct prefix *prefix,
1230 struct ospf6_route_table *table)
1231 {
1232 struct ospf6_route *route;
1233
1234 route = ospf6_route_lookup_bestmatch(prefix, table);
1235 if (route == NULL)
1236 return;
1237
1238 prefix = &route->prefix;
1239 ospf6_route_lock(route);
1240 while (route && ospf6_route_is_prefix(prefix, route)) {
1241 /* Specifying a prefix will always display details */
1242 ospf6_route_show_detail(vty, route);
1243 route = ospf6_route_next(route);
1244 }
1245 if (route)
1246 ospf6_route_unlock(route);
1247 }
1248
1249 static void ospf6_route_show_table_match(struct vty *vty, int detail,
1250 struct prefix *prefix,
1251 struct ospf6_route_table *table)
1252 {
1253 struct ospf6_route *route;
1254 assert(prefix->family);
1255
1256 route = ospf6_route_match_head(prefix, table);
1257 while (route) {
1258 if (detail)
1259 ospf6_route_show_detail(vty, route);
1260 else
1261 ospf6_route_show(vty, route);
1262 route = ospf6_route_match_next(prefix, route);
1263 }
1264 }
1265
1266 static void ospf6_route_show_table_type(struct vty *vty, int detail,
1267 uint8_t type,
1268 struct ospf6_route_table *table)
1269 {
1270 struct ospf6_route *route;
1271
1272 route = ospf6_route_head(table);
1273 while (route) {
1274 if (route->path.type == type) {
1275 if (detail)
1276 ospf6_route_show_detail(vty, route);
1277 else
1278 ospf6_route_show(vty, route);
1279 }
1280 route = ospf6_route_next(route);
1281 }
1282 }
1283
1284 static void ospf6_route_show_table(struct vty *vty, int detail,
1285 struct ospf6_route_table *table)
1286 {
1287 struct ospf6_route *route;
1288
1289 route = ospf6_route_head(table);
1290 while (route) {
1291 if (detail)
1292 ospf6_route_show_detail(vty, route);
1293 else
1294 ospf6_route_show(vty, route);
1295 route = ospf6_route_next(route);
1296 }
1297 }
1298
1299 int ospf6_route_table_show(struct vty *vty, int argc_start, int argc,
1300 struct cmd_token **argv,
1301 struct ospf6_route_table *table)
1302 {
1303 int summary = 0;
1304 int match = 0;
1305 int detail = 0;
1306 int slash = 0;
1307 int isprefix = 0;
1308 int i, ret;
1309 struct prefix prefix;
1310 uint8_t type = 0;
1311
1312 memset(&prefix, 0, sizeof(struct prefix));
1313
1314 for (i = argc_start; i < argc; i++) {
1315 if (strmatch(argv[i]->text, "summary")) {
1316 summary++;
1317 continue;
1318 }
1319
1320 if (strmatch(argv[i]->text, "intra-area")) {
1321 type = OSPF6_PATH_TYPE_INTRA;
1322 continue;
1323 }
1324
1325 if (strmatch(argv[i]->text, "inter-area")) {
1326 type = OSPF6_PATH_TYPE_INTER;
1327 continue;
1328 }
1329
1330 if (strmatch(argv[i]->text, "external-1")) {
1331 type = OSPF6_PATH_TYPE_EXTERNAL1;
1332 continue;
1333 }
1334
1335 if (strmatch(argv[i]->text, "external-2")) {
1336 type = OSPF6_PATH_TYPE_EXTERNAL2;
1337 continue;
1338 }
1339
1340 if (strmatch(argv[i]->text, "detail")) {
1341 detail++;
1342 continue;
1343 }
1344
1345 if (strmatch(argv[i]->text, "match")) {
1346 match++;
1347 continue;
1348 }
1349
1350 ret = str2prefix(argv[i]->arg, &prefix);
1351 if (ret == 1 && prefix.family == AF_INET6) {
1352 isprefix++;
1353 if (strchr(argv[i]->arg, '/'))
1354 slash++;
1355 continue;
1356 }
1357
1358 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1359 return CMD_SUCCESS;
1360 }
1361
1362 /* Give summary of this route table */
1363 if (summary) {
1364 ospf6_route_show_table_summary(vty, table);
1365 return CMD_SUCCESS;
1366 }
1367
1368 /* Give exact prefix-match route */
1369 if (isprefix && !match) {
1370 /* If exact address, give best matching route */
1371 if (!slash)
1372 ospf6_route_show_table_address(vty, &prefix, table);
1373 else
1374 ospf6_route_show_table_prefix(vty, &prefix, table);
1375
1376 return CMD_SUCCESS;
1377 }
1378
1379 if (match)
1380 ospf6_route_show_table_match(vty, detail, &prefix, table);
1381 else if (type)
1382 ospf6_route_show_table_type(vty, detail, type, table);
1383 else
1384 ospf6_route_show_table(vty, detail, table);
1385
1386 return CMD_SUCCESS;
1387 }
1388
1389 static void ospf6_linkstate_show_header(struct vty *vty)
1390 {
1391 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %s\n", "Type", "Router-ID",
1392 "Net-ID", "Rtr-Bits", "Options", "Cost");
1393 }
1394
1395 static void ospf6_linkstate_show(struct vty *vty, struct ospf6_route *route)
1396 {
1397 uint32_t router, id;
1398 char routername[16], idname[16], rbits[16], options[16];
1399
1400 router = ospf6_linkstate_prefix_adv_router(&route->prefix);
1401 inet_ntop(AF_INET, &router, routername, sizeof(routername));
1402 id = ospf6_linkstate_prefix_id(&route->prefix);
1403 inet_ntop(AF_INET, &id, idname, sizeof(idname));
1404
1405 ospf6_capability_printbuf(route->path.router_bits, rbits,
1406 sizeof(rbits));
1407 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1408
1409 if (ntohl(id))
1410 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Network",
1411 routername, idname, rbits, options,
1412 (unsigned long)route->path.cost);
1413 else
1414 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Router",
1415 routername, idname, rbits, options,
1416 (unsigned long)route->path.cost);
1417 }
1418
1419
1420 static void ospf6_linkstate_show_table_exact(struct vty *vty,
1421 struct prefix *prefix,
1422 struct ospf6_route_table *table)
1423 {
1424 struct ospf6_route *route;
1425
1426 route = ospf6_route_lookup(prefix, table);
1427 if (route == NULL)
1428 return;
1429
1430 ospf6_route_lock(route);
1431 while (route && ospf6_route_is_prefix(prefix, route)) {
1432 /* Specifying a prefix will always display details */
1433 ospf6_route_show_detail(vty, route);
1434 route = ospf6_route_next(route);
1435 }
1436 if (route)
1437 ospf6_route_unlock(route);
1438 }
1439
1440 static void ospf6_linkstate_show_table(struct vty *vty, int detail,
1441 struct ospf6_route_table *table)
1442 {
1443 struct ospf6_route *route;
1444
1445 if (!detail)
1446 ospf6_linkstate_show_header(vty);
1447
1448 route = ospf6_route_head(table);
1449 while (route) {
1450 if (detail)
1451 ospf6_route_show_detail(vty, route);
1452 else
1453 ospf6_linkstate_show(vty, route);
1454 route = ospf6_route_next(route);
1455 }
1456 }
1457
1458 int ospf6_linkstate_table_show(struct vty *vty, int idx_ipv4, int argc,
1459 struct cmd_token **argv,
1460 struct ospf6_route_table *table)
1461 {
1462 int detail = 0;
1463 int is_id = 0;
1464 int is_router = 0;
1465 int i, ret;
1466 struct prefix router, id, prefix;
1467
1468 memset(&router, 0, sizeof(struct prefix));
1469 memset(&id, 0, sizeof(struct prefix));
1470 memset(&prefix, 0, sizeof(struct prefix));
1471
1472 for (i = idx_ipv4; i < argc; i++) {
1473 if (strmatch(argv[i]->text, "detail")) {
1474 detail++;
1475 continue;
1476 }
1477
1478 if (!is_router) {
1479 ret = str2prefix(argv[i]->arg, &router);
1480 if (ret == 1 && router.family == AF_INET) {
1481 is_router++;
1482 continue;
1483 }
1484 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1485 return CMD_SUCCESS;
1486 }
1487
1488 if (!is_id) {
1489 ret = str2prefix(argv[i]->arg, &id);
1490 if (ret == 1 && id.family == AF_INET) {
1491 is_id++;
1492 continue;
1493 }
1494 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1495 return CMD_SUCCESS;
1496 }
1497
1498 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1499 return CMD_SUCCESS;
1500 }
1501
1502 if (is_router)
1503 ospf6_linkstate_prefix(router.u.prefix4.s_addr,
1504 id.u.prefix4.s_addr, &prefix);
1505
1506 if (prefix.family)
1507 ospf6_linkstate_show_table_exact(vty, &prefix, table);
1508 else
1509 ospf6_linkstate_show_table(vty, detail, table);
1510
1511 return CMD_SUCCESS;
1512 }
1513
1514
1515 void ospf6_brouter_show_header(struct vty *vty)
1516 {
1517 vty_out(vty, "%-15s %-8s %-14s %-10s %-15s\n", "Router-ID", "Rtr-Bits",
1518 "Options", "Path-Type", "Area");
1519 }
1520
1521 void ospf6_brouter_show(struct vty *vty, struct ospf6_route *route)
1522 {
1523 uint32_t adv_router;
1524 char adv[16], rbits[16], options[16], area[16];
1525
1526 adv_router = ospf6_linkstate_prefix_adv_router(&route->prefix);
1527 inet_ntop(AF_INET, &adv_router, adv, sizeof(adv));
1528 ospf6_capability_printbuf(route->path.router_bits, rbits,
1529 sizeof(rbits));
1530 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1531 inet_ntop(AF_INET, &route->path.area_id, area, sizeof(area));
1532
1533 /* vty_out (vty, "%-15s %-8s %-14s %-10s %-15s\n",
1534 "Router-ID", "Rtr-Bits", "Options", "Path-Type", "Area"); */
1535 vty_out(vty, "%-15s %-8s %-14s %-10s %-15s\n", adv, rbits, options,
1536 OSPF6_PATH_TYPE_NAME(route->path.type), area);
1537 }
1538
1539 DEFUN (debug_ospf6_route,
1540 debug_ospf6_route_cmd,
1541 "debug ospf6 route <table|intra-area|inter-area|memory>",
1542 DEBUG_STR
1543 OSPF6_STR
1544 "Debug routes\n"
1545 "Debug route table calculation\n"
1546 "Debug intra-area route calculation\n"
1547 "Debug inter-area route calculation\n"
1548 "Debug route memory use\n"
1549 )
1550 {
1551 int idx_type = 3;
1552 unsigned char level = 0;
1553
1554 if (!strcmp(argv[idx_type]->text, "table"))
1555 level = OSPF6_DEBUG_ROUTE_TABLE;
1556 else if (!strcmp(argv[idx_type]->text, "intra-area"))
1557 level = OSPF6_DEBUG_ROUTE_INTRA;
1558 else if (!strcmp(argv[idx_type]->text, "inter-area"))
1559 level = OSPF6_DEBUG_ROUTE_INTER;
1560 else if (!strcmp(argv[idx_type]->text, "memory"))
1561 level = OSPF6_DEBUG_ROUTE_MEMORY;
1562 OSPF6_DEBUG_ROUTE_ON(level);
1563 return CMD_SUCCESS;
1564 }
1565
1566 DEFUN (no_debug_ospf6_route,
1567 no_debug_ospf6_route_cmd,
1568 "no debug ospf6 route <table|intra-area|inter-area|memory>",
1569 NO_STR
1570 DEBUG_STR
1571 OSPF6_STR
1572 "Debug routes\n"
1573 "Debug route table calculation\n"
1574 "Debug intra-area route calculation\n"
1575 "Debug inter-area route calculation\n"
1576 "Debug route memory use\n")
1577 {
1578 int idx_type = 4;
1579 unsigned char level = 0;
1580
1581 if (!strcmp(argv[idx_type]->text, "table"))
1582 level = OSPF6_DEBUG_ROUTE_TABLE;
1583 else if (!strcmp(argv[idx_type]->text, "intra-area"))
1584 level = OSPF6_DEBUG_ROUTE_INTRA;
1585 else if (!strcmp(argv[idx_type]->text, "inter-area"))
1586 level = OSPF6_DEBUG_ROUTE_INTER;
1587 else if (!strcmp(argv[idx_type]->text, "memory"))
1588 level = OSPF6_DEBUG_ROUTE_MEMORY;
1589 OSPF6_DEBUG_ROUTE_OFF(level);
1590 return CMD_SUCCESS;
1591 }
1592
1593 int config_write_ospf6_debug_route(struct vty *vty)
1594 {
1595 if (IS_OSPF6_DEBUG_ROUTE(TABLE))
1596 vty_out(vty, "debug ospf6 route table\n");
1597 if (IS_OSPF6_DEBUG_ROUTE(INTRA))
1598 vty_out(vty, "debug ospf6 route intra-area\n");
1599 if (IS_OSPF6_DEBUG_ROUTE(INTER))
1600 vty_out(vty, "debug ospf6 route inter-area\n");
1601 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
1602 vty_out(vty, "debug ospf6 route memory\n");
1603
1604 return 0;
1605 }
1606
1607 void install_element_ospf6_debug_route(void)
1608 {
1609 install_element(ENABLE_NODE, &debug_ospf6_route_cmd);
1610 install_element(ENABLE_NODE, &no_debug_ospf6_route_cmd);
1611 install_element(CONFIG_NODE, &debug_ospf6_route_cmd);
1612 install_element(CONFIG_NODE, &no_debug_ospf6_route_cmd);
1613 }