]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_route.c
Merge pull request #1990 from donaldsharp/bgp_v6_ifindex
[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 struct ospf6_route *ospf6_route_create(void)
386 {
387 struct ospf6_route *route;
388 route = XCALLOC(MTYPE_OSPF6_ROUTE, sizeof(struct ospf6_route));
389 route->nh_list = list_new();
390 route->nh_list->cmp = (int (*)(void *, void *))ospf6_nexthop_cmp;
391 route->nh_list->del = (void (*)(void *))ospf6_nexthop_delete;
392 route->paths = list_new();
393 route->paths->cmp = (int (*)(void *, void *))ospf6_path_cmp;
394 route->paths->del = (void (*)(void *))ospf6_path_free;
395 return route;
396 }
397
398 void ospf6_route_delete(struct ospf6_route *route)
399 {
400 if (route) {
401 if (route->nh_list)
402 list_delete_and_null(&route->nh_list);
403 if (route->paths)
404 list_delete_and_null(&route->paths);
405 XFREE(MTYPE_OSPF6_ROUTE, route);
406 }
407 }
408
409 struct ospf6_route *ospf6_route_copy(struct ospf6_route *route)
410 {
411 struct ospf6_route *new;
412
413 new = ospf6_route_create();
414 new->type = route->type;
415 memcpy(&new->prefix, &route->prefix, sizeof(struct prefix));
416 new->installed = route->installed;
417 new->changed = route->changed;
418 new->flag = route->flag;
419 new->route_option = route->route_option;
420 new->linkstate_id = route->linkstate_id;
421 new->path = route->path;
422 ospf6_copy_nexthops(new->nh_list, route->nh_list);
423 new->rnode = NULL;
424 new->prev = NULL;
425 new->next = NULL;
426 new->table = NULL;
427 new->lock = 0;
428 return new;
429 }
430
431 void ospf6_route_lock(struct ospf6_route *route)
432 {
433 route->lock++;
434 }
435
436 void ospf6_route_unlock(struct ospf6_route *route)
437 {
438 assert(route->lock > 0);
439 route->lock--;
440 if (route->lock == 0) {
441 /* Can't detach from the table until here
442 because ospf6_route_next () will use
443 the 'route->table' pointer for logging */
444 route->table = NULL;
445 ospf6_route_delete(route);
446 }
447 }
448
449 /* Route compare function. If ra is more preferred, it returns
450 less than 0. If rb is more preferred returns greater than 0.
451 Otherwise (neither one is preferred), returns 0 */
452 int ospf6_route_cmp(struct ospf6_route *ra, struct ospf6_route *rb)
453 {
454 assert(ospf6_route_is_same(ra, rb));
455 assert(OSPF6_PATH_TYPE_NONE < ra->path.type
456 && ra->path.type < OSPF6_PATH_TYPE_MAX);
457 assert(OSPF6_PATH_TYPE_NONE < rb->path.type
458 && rb->path.type < OSPF6_PATH_TYPE_MAX);
459
460 if (ra->type != rb->type)
461 return (ra->type - rb->type);
462
463 if (ra->path.type != rb->path.type)
464 return (ra->path.type - rb->path.type);
465
466 if (ra->path.type == OSPF6_PATH_TYPE_EXTERNAL2) {
467 if (ra->path.u.cost_e2 != rb->path.u.cost_e2)
468 return (ra->path.u.cost_e2 - rb->path.u.cost_e2);
469 else
470 return (ra->path.cost - rb->path.cost);
471 } else {
472 if (ra->path.cost != rb->path.cost)
473 return (ra->path.cost - rb->path.cost);
474 }
475
476 if (ra->path.area_id != rb->path.area_id)
477 return (ntohl(ra->path.area_id) - ntohl(rb->path.area_id));
478
479 return 0;
480 }
481
482 struct ospf6_route *ospf6_route_lookup(struct prefix *prefix,
483 struct ospf6_route_table *table)
484 {
485 struct route_node *node;
486 struct ospf6_route *route;
487
488 node = route_node_lookup(table->table, prefix);
489 if (node == NULL)
490 return NULL;
491
492 route = (struct ospf6_route *)node->info;
493 route_unlock_node(node); /* to free the lookup lock */
494 return route;
495 }
496
497 struct ospf6_route *
498 ospf6_route_lookup_identical(struct ospf6_route *route,
499 struct ospf6_route_table *table)
500 {
501 struct ospf6_route *target;
502
503 for (target = ospf6_route_lookup(&route->prefix, table); target;
504 target = target->next) {
505 if (target->type == route->type
506 && (memcmp(&target->prefix, &route->prefix,
507 sizeof(struct prefix))
508 == 0)
509 && target->path.type == route->path.type
510 && target->path.cost == route->path.cost
511 && target->path.u.cost_e2 == route->path.u.cost_e2
512 && ospf6_route_cmp_nexthops(target, route) == 0)
513 return target;
514 }
515 return NULL;
516 }
517
518 struct ospf6_route *
519 ospf6_route_lookup_bestmatch(struct prefix *prefix,
520 struct ospf6_route_table *table)
521 {
522 struct route_node *node;
523 struct ospf6_route *route;
524
525 node = route_node_match(table->table, prefix);
526 if (node == NULL)
527 return NULL;
528 route_unlock_node(node);
529
530 route = (struct ospf6_route *)node->info;
531 return route;
532 }
533
534 #ifdef DEBUG
535 static void route_table_assert(struct ospf6_route_table *table)
536 {
537 struct ospf6_route *prev, *r, *next;
538 char buf[PREFIX2STR_BUFFER];
539 unsigned int link_error = 0, num = 0;
540
541 r = ospf6_route_head(table);
542 prev = NULL;
543 while (r) {
544 if (r->prev != prev)
545 link_error++;
546
547 next = ospf6_route_next(r);
548
549 if (r->next != next)
550 link_error++;
551
552 prev = r;
553 r = next;
554 }
555
556 for (r = ospf6_route_head(table); r; r = ospf6_route_next(r))
557 num++;
558
559 if (link_error == 0 && num == table->count)
560 return;
561
562 zlog_err("PANIC !!");
563 zlog_err("Something has gone wrong with ospf6_route_table[%p]", table);
564 zlog_debug("table count = %d, real number = %d", table->count, num);
565 zlog_debug("DUMP START");
566 for (r = ospf6_route_head(table); r; r = ospf6_route_next(r)) {
567 prefix2str(&r->prefix, buf, sizeof(buf));
568 zlog_info("%p<-[%p]->%p : %s", r->prev, r, r->next, buf);
569 }
570 zlog_debug("DUMP END");
571
572 assert(link_error == 0 && num == table->count);
573 }
574 #define ospf6_route_table_assert(t) (route_table_assert (t))
575 #else
576 #define ospf6_route_table_assert(t) ((void) 0)
577 #endif /*DEBUG*/
578
579 struct ospf6_route *ospf6_route_add(struct ospf6_route *route,
580 struct ospf6_route_table *table)
581 {
582 struct route_node *node, *nextnode, *prevnode;
583 struct ospf6_route *current = NULL;
584 struct ospf6_route *prev = NULL, *old = NULL, *next = NULL;
585 char buf[PREFIX2STR_BUFFER];
586 struct timeval now;
587
588 assert(route->rnode == NULL);
589 assert(route->lock == 0);
590 assert(route->next == NULL);
591 assert(route->prev == NULL);
592
593 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
594 ospf6_linkstate_prefix2str(&route->prefix, buf, sizeof(buf));
595 else
596 prefix2str(&route->prefix, buf, sizeof(buf));
597
598 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
599 zlog_debug("%s %p: route add %p: %s",
600 ospf6_route_table_name(table), (void *)table,
601 (void *)route, buf);
602 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
603 zlog_debug("%s: route add: %s", ospf6_route_table_name(table),
604 buf);
605
606 monotime(&now);
607
608 node = route_node_get(table->table, &route->prefix);
609 route->rnode = node;
610
611 /* find place to insert */
612 for (current = node->info; current; current = current->next) {
613 if (!ospf6_route_is_same(current, route))
614 next = current;
615 else if (current->type != route->type)
616 prev = current;
617 else if (ospf6_route_is_same_origin(current, route))
618 old = current;
619 else if (ospf6_route_cmp(current, route) > 0)
620 next = current;
621 else
622 prev = current;
623
624 if (old || next)
625 break;
626 }
627
628 if (old) {
629 /* if route does not actually change, return unchanged */
630 if (ospf6_route_is_identical(old, route)) {
631 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
632 zlog_debug(
633 "%s %p: route add %p: needless update of %p old cost %u",
634 ospf6_route_table_name(table),
635 (void *)table, (void *)route,
636 (void *)old, old->path.cost);
637 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
638 zlog_debug("%s: route add: needless update",
639 ospf6_route_table_name(table));
640
641 ospf6_route_delete(route);
642 SET_FLAG(old->flag, OSPF6_ROUTE_ADD);
643 ospf6_route_table_assert(table);
644
645 /* to free the lookup lock */
646 route_unlock_node(node);
647 return old;
648 }
649
650 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
651 zlog_debug(
652 "%s %p: route add %p cost %u: update of %p old cost %u",
653 ospf6_route_table_name(table), (void *)table,
654 (void *)route, route->path.cost, (void *)old,
655 old->path.cost);
656 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
657 zlog_debug("%s: route add: update",
658 ospf6_route_table_name(table));
659
660 /* replace old one if exists */
661 if (node->info == old) {
662 node->info = route;
663 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
664 }
665
666 if (old->prev)
667 old->prev->next = route;
668 route->prev = old->prev;
669 if (old->next)
670 old->next->prev = route;
671 route->next = old->next;
672
673 route->installed = old->installed;
674 route->changed = now;
675 assert(route->table == NULL);
676 route->table = table;
677
678 ospf6_route_unlock(old); /* will be deleted later */
679 ospf6_route_lock(route);
680
681 SET_FLAG(route->flag, OSPF6_ROUTE_CHANGE);
682 ospf6_route_table_assert(table);
683
684 if (table->hook_add)
685 (*table->hook_add)(route);
686
687 return route;
688 }
689
690 /* insert if previous or next node found */
691 if (prev || next) {
692 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
693 zlog_debug(
694 "%s %p: route add %p cost %u: another path: prev %p, next %p node ref %u",
695 ospf6_route_table_name(table), (void *)table,
696 (void *)route, route->path.cost, (void *)prev,
697 (void *)next, node->lock);
698 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
699 zlog_debug("%s: route add cost %u: another path found",
700 ospf6_route_table_name(table),
701 route->path.cost);
702
703 if (prev == NULL)
704 prev = next->prev;
705 if (next == NULL)
706 next = prev->next;
707
708 if (prev)
709 prev->next = route;
710 route->prev = prev;
711 if (next)
712 next->prev = route;
713 route->next = next;
714
715 if (node->info == next) {
716 assert(next->rnode == node);
717 node->info = route;
718 UNSET_FLAG(next->flag, OSPF6_ROUTE_BEST);
719 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
720 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
721 zlog_info(
722 "%s %p: route add %p cost %u: replacing previous best: %p cost %u",
723 ospf6_route_table_name(table),
724 (void *)table, (void *)route,
725 route->path.cost,
726 (void *)next, next->path.cost);
727 }
728
729 route->installed = now;
730 route->changed = now;
731 assert(route->table == NULL);
732 route->table = table;
733
734 ospf6_route_lock(route);
735 table->count++;
736 ospf6_route_table_assert(table);
737
738 SET_FLAG(route->flag, OSPF6_ROUTE_ADD);
739 if (table->hook_add)
740 (*table->hook_add)(route);
741
742 return route;
743 }
744
745 /* Else, this is the brand new route regarding to the prefix */
746 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
747 zlog_debug("%s %p: route add %p %s cost %u: brand new route",
748 ospf6_route_table_name(table), (void *)table,
749 (void *)route, buf, route->path.cost);
750 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
751 zlog_debug("%s: route add: brand new route",
752 ospf6_route_table_name(table));
753
754 assert(node->info == NULL);
755 node->info = route;
756 SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
757 ospf6_route_lock(route);
758 route->installed = now;
759 route->changed = now;
760 assert(route->table == NULL);
761 route->table = table;
762
763 /* lookup real existing next route */
764 nextnode = node;
765 route_lock_node(nextnode);
766 do {
767 nextnode = route_next(nextnode);
768 } while (nextnode && nextnode->info == NULL);
769
770 /* set next link */
771 if (nextnode == NULL)
772 route->next = NULL;
773 else {
774 route_unlock_node(nextnode);
775
776 next = nextnode->info;
777 route->next = next;
778 next->prev = route;
779 }
780
781 /* lookup real existing prev route */
782 prevnode = node;
783 route_lock_node(prevnode);
784 do {
785 prevnode = route_prev(prevnode);
786 } while (prevnode && prevnode->info == NULL);
787
788 /* set prev link */
789 if (prevnode == NULL)
790 route->prev = NULL;
791 else {
792 route_unlock_node(prevnode);
793
794 prev = prevnode->info;
795 while (prev->next && ospf6_route_is_same(prev, prev->next))
796 prev = prev->next;
797 route->prev = prev;
798 prev->next = route;
799 }
800
801 table->count++;
802 ospf6_route_table_assert(table);
803
804 SET_FLAG(route->flag, OSPF6_ROUTE_ADD);
805 if (table->hook_add)
806 (*table->hook_add)(route);
807
808 return route;
809 }
810
811 void ospf6_route_remove(struct ospf6_route *route,
812 struct ospf6_route_table *table)
813 {
814 struct route_node *node;
815 struct ospf6_route *current;
816 char buf[PREFIX2STR_BUFFER];
817
818 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
819 ospf6_linkstate_prefix2str(&route->prefix, buf, sizeof(buf));
820 else
821 prefix2str(&route->prefix, buf, sizeof(buf));
822
823 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
824 zlog_debug("%s %p: route remove %p: %s cost %u refcount %u",
825 ospf6_route_table_name(table), (void *)table,
826 (void *)route, buf, route->path.cost, route->lock);
827 else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
828 zlog_debug("%s: route remove: %s",
829 ospf6_route_table_name(table), buf);
830
831 node = route_node_lookup(table->table, &route->prefix);
832 assert(node);
833
834 /* find the route to remove, making sure that the route pointer
835 is from the route table. */
836 current = node->info;
837 while (current && current != route)
838 current = current->next;
839
840 assert(current == route);
841
842 /* adjust doubly linked list */
843 if (route->prev)
844 route->prev->next = route->next;
845 if (route->next)
846 route->next->prev = route->prev;
847
848 if (node->info == route) {
849 if (route->next && route->next->rnode == node) {
850 node->info = route->next;
851 SET_FLAG(route->next->flag, OSPF6_ROUTE_BEST);
852 } else {
853 node->info = NULL;
854 route->rnode = NULL;
855 route_unlock_node(node); /* to free the original lock */
856 }
857 }
858
859 route_unlock_node(node); /* to free the lookup lock */
860 table->count--;
861 ospf6_route_table_assert(table);
862
863 SET_FLAG(route->flag, OSPF6_ROUTE_WAS_REMOVED);
864
865 /* Note hook_remove may call ospf6_route_remove */
866 if (table->hook_remove)
867 (*table->hook_remove)(route);
868
869 ospf6_route_unlock(route);
870 }
871
872 struct ospf6_route *ospf6_route_head(struct ospf6_route_table *table)
873 {
874 struct route_node *node;
875 struct ospf6_route *route;
876
877 node = route_top(table->table);
878 if (node == NULL)
879 return NULL;
880
881 /* skip to the real existing entry */
882 while (node && node->info == NULL)
883 node = route_next(node);
884 if (node == NULL)
885 return NULL;
886
887 route_unlock_node(node);
888 assert(node->info);
889
890 route = (struct ospf6_route *)node->info;
891 assert(route->prev == NULL);
892 assert(route->table == table);
893 ospf6_route_lock(route);
894
895 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
896 zlog_info("%s %p: route head: %p<-[%p]->%p",
897 ospf6_route_table_name(table), (void *)table,
898 (void *)route->prev, (void *)route,
899 (void *)route->next);
900
901 return route;
902 }
903
904 struct ospf6_route *ospf6_route_next(struct ospf6_route *route)
905 {
906 struct ospf6_route *next = route->next;
907
908 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
909 zlog_info("%s %p: route next: %p<-[%p]->%p",
910 ospf6_route_table_name(route->table),
911 (void *)route->table, (void *)route->prev,
912 (void *)route, (void *)route->next);
913
914 ospf6_route_unlock(route);
915 if (next)
916 ospf6_route_lock(next);
917
918 return next;
919 }
920
921 struct ospf6_route *ospf6_route_best_next(struct ospf6_route *route)
922 {
923 struct route_node *rnode;
924 struct ospf6_route *next;
925
926 ospf6_route_unlock(route);
927
928 rnode = route->rnode;
929 route_lock_node(rnode);
930 rnode = route_next(rnode);
931 while (rnode && rnode->info == NULL)
932 rnode = route_next(rnode);
933 if (rnode == NULL)
934 return NULL;
935 route_unlock_node(rnode);
936
937 assert(rnode->info);
938 next = (struct ospf6_route *)rnode->info;
939 ospf6_route_lock(next);
940 return next;
941 }
942
943 struct ospf6_route *ospf6_route_match_head(struct prefix *prefix,
944 struct ospf6_route_table *table)
945 {
946 struct route_node *node;
947 struct ospf6_route *route;
948
949 /* Walk down tree. */
950 node = table->table->top;
951 while (node && node->p.prefixlen < prefix->prefixlen
952 && prefix_match(&node->p, prefix))
953 node = node->link[prefix_bit(&prefix->u.prefix,
954 node->p.prefixlen)];
955
956 if (node)
957 route_lock_node(node);
958 while (node && node->info == NULL)
959 node = route_next(node);
960 if (node == NULL)
961 return NULL;
962 route_unlock_node(node);
963
964 if (!prefix_match(prefix, &node->p))
965 return NULL;
966
967 route = node->info;
968 ospf6_route_lock(route);
969 return route;
970 }
971
972 struct ospf6_route *ospf6_route_match_next(struct prefix *prefix,
973 struct ospf6_route *route)
974 {
975 struct ospf6_route *next;
976
977 next = ospf6_route_next(route);
978 if (next && !prefix_match(prefix, &next->prefix)) {
979 ospf6_route_unlock(next);
980 next = NULL;
981 }
982
983 return next;
984 }
985
986 void ospf6_route_remove_all(struct ospf6_route_table *table)
987 {
988 struct ospf6_route *route;
989 for (route = ospf6_route_head(table); route;
990 route = ospf6_route_next(route))
991 ospf6_route_remove(route, table);
992 }
993
994 struct ospf6_route_table *ospf6_route_table_create(int s, int t)
995 {
996 struct ospf6_route_table *new;
997 new = XCALLOC(MTYPE_OSPF6_ROUTE, sizeof(struct ospf6_route_table));
998 new->table = route_table_init();
999 new->scope_type = s;
1000 new->table_type = t;
1001 return new;
1002 }
1003
1004 void ospf6_route_table_delete(struct ospf6_route_table *table)
1005 {
1006 ospf6_route_remove_all(table);
1007 bf_free(table->idspace);
1008 route_table_finish(table->table);
1009 XFREE(MTYPE_OSPF6_ROUTE, table);
1010 }
1011
1012
1013 /* VTY commands */
1014 void ospf6_route_show(struct vty *vty, struct ospf6_route *route)
1015 {
1016 int i;
1017 char destination[PREFIX2STR_BUFFER], nexthop[64];
1018 char duration[64];
1019 const char *ifname;
1020 struct timeval now, res;
1021 struct listnode *node;
1022 struct ospf6_nexthop *nh;
1023
1024 monotime(&now);
1025 timersub(&now, &route->changed, &res);
1026 timerstring(&res, duration, sizeof(duration));
1027
1028 /* destination */
1029 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
1030 ospf6_linkstate_prefix2str(&route->prefix, destination,
1031 sizeof(destination));
1032 else if (route->type == OSPF6_DEST_TYPE_ROUTER)
1033 inet_ntop(route->prefix.family, &route->prefix.u.prefix,
1034 destination, sizeof(destination));
1035 else
1036 prefix2str(&route->prefix, destination, sizeof(destination));
1037
1038 i = 0;
1039 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
1040 /* nexthop */
1041 inet_ntop(AF_INET6, &nh->address, nexthop, sizeof(nexthop));
1042 ifname = ifindex2ifname(nh->ifindex, VRF_DEFAULT);
1043
1044 if (!i) {
1045 vty_out(vty, "%c%1s %2s %-30s %-25s %6.*s %s\n",
1046 (ospf6_route_is_best(route) ? '*' : ' '),
1047 OSPF6_DEST_TYPE_SUBSTR(route->type),
1048 OSPF6_PATH_TYPE_SUBSTR(route->path.type),
1049 destination, nexthop, IFNAMSIZ, ifname,
1050 duration);
1051 i++;
1052 } else
1053 vty_out(vty, "%c%1s %2s %-30s %-25s %6.*s %s\n", ' ',
1054 "", "", "", nexthop, IFNAMSIZ, ifname, "");
1055 }
1056 }
1057
1058 void ospf6_route_show_detail(struct vty *vty, struct ospf6_route *route)
1059 {
1060 const char *ifname;
1061 char destination[PREFIX2STR_BUFFER], nexthop[64];
1062 char area_id[16], id[16], adv_router[16], capa[16], options[16];
1063 struct timeval now, res;
1064 char duration[64];
1065 struct listnode *node;
1066 struct ospf6_nexthop *nh;
1067
1068 monotime(&now);
1069
1070 /* destination */
1071 if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
1072 ospf6_linkstate_prefix2str(&route->prefix, destination,
1073 sizeof(destination));
1074 else if (route->type == OSPF6_DEST_TYPE_ROUTER)
1075 inet_ntop(route->prefix.family, &route->prefix.u.prefix,
1076 destination, sizeof(destination));
1077 else
1078 prefix2str(&route->prefix, destination, sizeof(destination));
1079 vty_out(vty, "Destination: %s\n", destination);
1080
1081 /* destination type */
1082 vty_out(vty, "Destination type: %s\n",
1083 OSPF6_DEST_TYPE_NAME(route->type));
1084
1085 /* Time */
1086 timersub(&now, &route->installed, &res);
1087 timerstring(&res, duration, sizeof(duration));
1088 vty_out(vty, "Installed Time: %s ago\n", duration);
1089
1090 timersub(&now, &route->changed, &res);
1091 timerstring(&res, duration, sizeof(duration));
1092 vty_out(vty, " Changed Time: %s ago\n", duration);
1093
1094 /* Debugging info */
1095 vty_out(vty, "Lock: %d Flags: %s%s%s%s\n", route->lock,
1096 (CHECK_FLAG(route->flag, OSPF6_ROUTE_BEST) ? "B" : "-"),
1097 (CHECK_FLAG(route->flag, OSPF6_ROUTE_ADD) ? "A" : "-"),
1098 (CHECK_FLAG(route->flag, OSPF6_ROUTE_REMOVE) ? "R" : "-"),
1099 (CHECK_FLAG(route->flag, OSPF6_ROUTE_CHANGE) ? "C" : "-"));
1100 vty_out(vty, "Memory: prev: %p this: %p next: %p\n",
1101 (void *)route->prev, (void *)route, (void *)route->next);
1102
1103 /* Path section */
1104
1105 /* Area-ID */
1106 inet_ntop(AF_INET, &route->path.area_id, area_id, sizeof(area_id));
1107 vty_out(vty, "Associated Area: %s\n", area_id);
1108
1109 /* Path type */
1110 vty_out(vty, "Path Type: %s\n", OSPF6_PATH_TYPE_NAME(route->path.type));
1111
1112 /* LS Origin */
1113 inet_ntop(AF_INET, &route->path.origin.id, id, sizeof(id));
1114 inet_ntop(AF_INET, &route->path.origin.adv_router, adv_router,
1115 sizeof(adv_router));
1116 vty_out(vty, "LS Origin: %s Id: %s Adv: %s\n",
1117 ospf6_lstype_name(route->path.origin.type), id, adv_router);
1118
1119 /* Options */
1120 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1121 vty_out(vty, "Options: %s\n", options);
1122
1123 /* Router Bits */
1124 ospf6_capability_printbuf(route->path.router_bits, capa, sizeof(capa));
1125 vty_out(vty, "Router Bits: %s\n", capa);
1126
1127 /* Prefix Options */
1128 vty_out(vty, "Prefix Options: xxx\n");
1129
1130 /* Metrics */
1131 vty_out(vty, "Metric Type: %d\n", route->path.metric_type);
1132 vty_out(vty, "Metric: %d (%d)\n", route->path.cost,
1133 route->path.u.cost_e2);
1134
1135 vty_out(vty, "Paths count: %u\n", route->paths->count);
1136 vty_out(vty, "Nexthop count: %u\n", route->nh_list->count);
1137 /* Nexthops */
1138 vty_out(vty, "Nexthop:\n");
1139 for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
1140 /* nexthop */
1141 inet_ntop(AF_INET6, &nh->address, nexthop, sizeof(nexthop));
1142 ifname = ifindex2ifname(nh->ifindex, VRF_DEFAULT);
1143 vty_out(vty, " %s %.*s\n", nexthop, IFNAMSIZ, ifname);
1144 }
1145 vty_out(vty, "\n");
1146 }
1147
1148 static void ospf6_route_show_table_summary(struct vty *vty,
1149 struct ospf6_route_table *table)
1150 {
1151 struct ospf6_route *route, *prev = NULL;
1152 int i, pathtype[OSPF6_PATH_TYPE_MAX];
1153 unsigned int number = 0;
1154 int nh_count = 0, nhinval = 0, ecmp = 0;
1155 int alternative = 0, destination = 0;
1156
1157 for (i = 0; i < OSPF6_PATH_TYPE_MAX; i++)
1158 pathtype[i] = 0;
1159
1160 for (route = ospf6_route_head(table); route;
1161 route = ospf6_route_next(route)) {
1162 if (prev == NULL || !ospf6_route_is_same(prev, route))
1163 destination++;
1164 else
1165 alternative++;
1166 nh_count = ospf6_num_nexthops(route->nh_list);
1167 if (!nh_count)
1168 nhinval++;
1169 else if (nh_count > 1)
1170 ecmp++;
1171 pathtype[route->path.type]++;
1172 number++;
1173
1174 prev = route;
1175 }
1176
1177 assert(number == table->count);
1178
1179 vty_out(vty, "Number of OSPFv3 routes: %d\n", number);
1180 vty_out(vty, "Number of Destination: %d\n", destination);
1181 vty_out(vty, "Number of Alternative routes: %d\n", alternative);
1182 vty_out(vty, "Number of Equal Cost Multi Path: %d\n", ecmp);
1183 for (i = OSPF6_PATH_TYPE_INTRA; i <= OSPF6_PATH_TYPE_EXTERNAL2; i++) {
1184 vty_out(vty, "Number of %s routes: %d\n",
1185 OSPF6_PATH_TYPE_NAME(i), pathtype[i]);
1186 }
1187 }
1188
1189 static void ospf6_route_show_table_prefix(struct vty *vty,
1190 struct prefix *prefix,
1191 struct ospf6_route_table *table)
1192 {
1193 struct ospf6_route *route;
1194
1195 route = ospf6_route_lookup(prefix, table);
1196 if (route == NULL)
1197 return;
1198
1199 ospf6_route_lock(route);
1200 while (route && ospf6_route_is_prefix(prefix, route)) {
1201 /* Specifying a prefix will always display details */
1202 ospf6_route_show_detail(vty, route);
1203 route = ospf6_route_next(route);
1204 }
1205 if (route)
1206 ospf6_route_unlock(route);
1207 }
1208
1209 static void ospf6_route_show_table_address(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_bestmatch(prefix, table);
1216 if (route == NULL)
1217 return;
1218
1219 prefix = &route->prefix;
1220 ospf6_route_lock(route);
1221 while (route && ospf6_route_is_prefix(prefix, route)) {
1222 /* Specifying a prefix will always display details */
1223 ospf6_route_show_detail(vty, route);
1224 route = ospf6_route_next(route);
1225 }
1226 if (route)
1227 ospf6_route_unlock(route);
1228 }
1229
1230 static void ospf6_route_show_table_match(struct vty *vty, int detail,
1231 struct prefix *prefix,
1232 struct ospf6_route_table *table)
1233 {
1234 struct ospf6_route *route;
1235 assert(prefix->family);
1236
1237 route = ospf6_route_match_head(prefix, table);
1238 while (route) {
1239 if (detail)
1240 ospf6_route_show_detail(vty, route);
1241 else
1242 ospf6_route_show(vty, route);
1243 route = ospf6_route_match_next(prefix, route);
1244 }
1245 }
1246
1247 static void ospf6_route_show_table_type(struct vty *vty, int detail,
1248 uint8_t type,
1249 struct ospf6_route_table *table)
1250 {
1251 struct ospf6_route *route;
1252
1253 route = ospf6_route_head(table);
1254 while (route) {
1255 if (route->path.type == type) {
1256 if (detail)
1257 ospf6_route_show_detail(vty, route);
1258 else
1259 ospf6_route_show(vty, route);
1260 }
1261 route = ospf6_route_next(route);
1262 }
1263 }
1264
1265 static void ospf6_route_show_table(struct vty *vty, int detail,
1266 struct ospf6_route_table *table)
1267 {
1268 struct ospf6_route *route;
1269
1270 route = ospf6_route_head(table);
1271 while (route) {
1272 if (detail)
1273 ospf6_route_show_detail(vty, route);
1274 else
1275 ospf6_route_show(vty, route);
1276 route = ospf6_route_next(route);
1277 }
1278 }
1279
1280 int ospf6_route_table_show(struct vty *vty, int argc_start, int argc,
1281 struct cmd_token **argv,
1282 struct ospf6_route_table *table)
1283 {
1284 int summary = 0;
1285 int match = 0;
1286 int detail = 0;
1287 int slash = 0;
1288 int isprefix = 0;
1289 int i, ret;
1290 struct prefix prefix;
1291 uint8_t type = 0;
1292
1293 memset(&prefix, 0, sizeof(struct prefix));
1294
1295 for (i = argc_start; i < argc; i++) {
1296 if (strmatch(argv[i]->text, "summary")) {
1297 summary++;
1298 continue;
1299 }
1300
1301 if (strmatch(argv[i]->text, "intra-area")) {
1302 type = OSPF6_PATH_TYPE_INTRA;
1303 continue;
1304 }
1305
1306 if (strmatch(argv[i]->text, "inter-area")) {
1307 type = OSPF6_PATH_TYPE_INTER;
1308 continue;
1309 }
1310
1311 if (strmatch(argv[i]->text, "external-1")) {
1312 type = OSPF6_PATH_TYPE_EXTERNAL1;
1313 continue;
1314 }
1315
1316 if (strmatch(argv[i]->text, "external-2")) {
1317 type = OSPF6_PATH_TYPE_EXTERNAL2;
1318 continue;
1319 }
1320
1321 if (strmatch(argv[i]->text, "detail")) {
1322 detail++;
1323 continue;
1324 }
1325
1326 if (strmatch(argv[i]->text, "match")) {
1327 match++;
1328 continue;
1329 }
1330
1331 ret = str2prefix(argv[i]->arg, &prefix);
1332 if (ret == 1 && prefix.family == AF_INET6) {
1333 isprefix++;
1334 if (strchr(argv[i]->arg, '/'))
1335 slash++;
1336 continue;
1337 }
1338
1339 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1340 return CMD_SUCCESS;
1341 }
1342
1343 /* Give summary of this route table */
1344 if (summary) {
1345 ospf6_route_show_table_summary(vty, table);
1346 return CMD_SUCCESS;
1347 }
1348
1349 /* Give exact prefix-match route */
1350 if (isprefix && !match) {
1351 /* If exact address, give best matching route */
1352 if (!slash)
1353 ospf6_route_show_table_address(vty, &prefix, table);
1354 else
1355 ospf6_route_show_table_prefix(vty, &prefix, table);
1356
1357 return CMD_SUCCESS;
1358 }
1359
1360 if (match)
1361 ospf6_route_show_table_match(vty, detail, &prefix, table);
1362 else if (type)
1363 ospf6_route_show_table_type(vty, detail, type, table);
1364 else
1365 ospf6_route_show_table(vty, detail, table);
1366
1367 return CMD_SUCCESS;
1368 }
1369
1370 static void ospf6_linkstate_show_header(struct vty *vty)
1371 {
1372 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %s\n", "Type", "Router-ID",
1373 "Net-ID", "Rtr-Bits", "Options", "Cost");
1374 }
1375
1376 static void ospf6_linkstate_show(struct vty *vty, struct ospf6_route *route)
1377 {
1378 uint32_t router, id;
1379 char routername[16], idname[16], rbits[16], options[16];
1380
1381 router = ospf6_linkstate_prefix_adv_router(&route->prefix);
1382 inet_ntop(AF_INET, &router, routername, sizeof(routername));
1383 id = ospf6_linkstate_prefix_id(&route->prefix);
1384 inet_ntop(AF_INET, &id, idname, sizeof(idname));
1385
1386 ospf6_capability_printbuf(route->path.router_bits, rbits,
1387 sizeof(rbits));
1388 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1389
1390 if (ntohl(id))
1391 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Network",
1392 routername, idname, rbits, options,
1393 (unsigned long)route->path.cost);
1394 else
1395 vty_out(vty, "%-7s %-15s %-15s %-8s %-14s %lu\n", "Router",
1396 routername, idname, rbits, options,
1397 (unsigned long)route->path.cost);
1398 }
1399
1400
1401 static void ospf6_linkstate_show_table_exact(struct vty *vty,
1402 struct prefix *prefix,
1403 struct ospf6_route_table *table)
1404 {
1405 struct ospf6_route *route;
1406
1407 route = ospf6_route_lookup(prefix, table);
1408 if (route == NULL)
1409 return;
1410
1411 ospf6_route_lock(route);
1412 while (route && ospf6_route_is_prefix(prefix, route)) {
1413 /* Specifying a prefix will always display details */
1414 ospf6_route_show_detail(vty, route);
1415 route = ospf6_route_next(route);
1416 }
1417 if (route)
1418 ospf6_route_unlock(route);
1419 }
1420
1421 static void ospf6_linkstate_show_table(struct vty *vty, int detail,
1422 struct ospf6_route_table *table)
1423 {
1424 struct ospf6_route *route;
1425
1426 if (!detail)
1427 ospf6_linkstate_show_header(vty);
1428
1429 route = ospf6_route_head(table);
1430 while (route) {
1431 if (detail)
1432 ospf6_route_show_detail(vty, route);
1433 else
1434 ospf6_linkstate_show(vty, route);
1435 route = ospf6_route_next(route);
1436 }
1437 }
1438
1439 int ospf6_linkstate_table_show(struct vty *vty, int idx_ipv4, int argc,
1440 struct cmd_token **argv,
1441 struct ospf6_route_table *table)
1442 {
1443 int detail = 0;
1444 int is_id = 0;
1445 int is_router = 0;
1446 int i, ret;
1447 struct prefix router, id, prefix;
1448
1449 memset(&router, 0, sizeof(struct prefix));
1450 memset(&id, 0, sizeof(struct prefix));
1451 memset(&prefix, 0, sizeof(struct prefix));
1452
1453 for (i = idx_ipv4; i < argc; i++) {
1454 if (strmatch(argv[i]->text, "detail")) {
1455 detail++;
1456 continue;
1457 }
1458
1459 if (!is_router) {
1460 ret = str2prefix(argv[i]->arg, &router);
1461 if (ret == 1 && router.family == AF_INET) {
1462 is_router++;
1463 continue;
1464 }
1465 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1466 return CMD_SUCCESS;
1467 }
1468
1469 if (!is_id) {
1470 ret = str2prefix(argv[i]->arg, &id);
1471 if (ret == 1 && id.family == AF_INET) {
1472 is_id++;
1473 continue;
1474 }
1475 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1476 return CMD_SUCCESS;
1477 }
1478
1479 vty_out(vty, "Malformed argument: %s\n", argv[i]->arg);
1480 return CMD_SUCCESS;
1481 }
1482
1483 if (is_router)
1484 ospf6_linkstate_prefix(router.u.prefix4.s_addr,
1485 id.u.prefix4.s_addr, &prefix);
1486
1487 if (prefix.family)
1488 ospf6_linkstate_show_table_exact(vty, &prefix, table);
1489 else
1490 ospf6_linkstate_show_table(vty, detail, table);
1491
1492 return CMD_SUCCESS;
1493 }
1494
1495
1496 void ospf6_brouter_show_header(struct vty *vty)
1497 {
1498 vty_out(vty, "%-15s %-8s %-14s %-10s %-15s\n", "Router-ID", "Rtr-Bits",
1499 "Options", "Path-Type", "Area");
1500 }
1501
1502 void ospf6_brouter_show(struct vty *vty, struct ospf6_route *route)
1503 {
1504 uint32_t adv_router;
1505 char adv[16], rbits[16], options[16], area[16];
1506
1507 adv_router = ospf6_linkstate_prefix_adv_router(&route->prefix);
1508 inet_ntop(AF_INET, &adv_router, adv, sizeof(adv));
1509 ospf6_capability_printbuf(route->path.router_bits, rbits,
1510 sizeof(rbits));
1511 ospf6_options_printbuf(route->path.options, options, sizeof(options));
1512 inet_ntop(AF_INET, &route->path.area_id, area, sizeof(area));
1513
1514 /* vty_out (vty, "%-15s %-8s %-14s %-10s %-15s\n",
1515 "Router-ID", "Rtr-Bits", "Options", "Path-Type", "Area"); */
1516 vty_out(vty, "%-15s %-8s %-14s %-10s %-15s\n", adv, rbits, options,
1517 OSPF6_PATH_TYPE_NAME(route->path.type), area);
1518 }
1519
1520 DEFUN (debug_ospf6_route,
1521 debug_ospf6_route_cmd,
1522 "debug ospf6 route <table|intra-area|inter-area|memory>",
1523 DEBUG_STR
1524 OSPF6_STR
1525 "Debug routes\n"
1526 "Debug route table calculation\n"
1527 "Debug intra-area route calculation\n"
1528 "Debug inter-area route calculation\n"
1529 "Debug route memory use\n"
1530 )
1531 {
1532 int idx_type = 3;
1533 unsigned char level = 0;
1534
1535 if (!strcmp(argv[idx_type]->text, "table"))
1536 level = OSPF6_DEBUG_ROUTE_TABLE;
1537 else if (!strcmp(argv[idx_type]->text, "intra-area"))
1538 level = OSPF6_DEBUG_ROUTE_INTRA;
1539 else if (!strcmp(argv[idx_type]->text, "inter-area"))
1540 level = OSPF6_DEBUG_ROUTE_INTER;
1541 else if (!strcmp(argv[idx_type]->text, "memory"))
1542 level = OSPF6_DEBUG_ROUTE_MEMORY;
1543 OSPF6_DEBUG_ROUTE_ON(level);
1544 return CMD_SUCCESS;
1545 }
1546
1547 DEFUN (no_debug_ospf6_route,
1548 no_debug_ospf6_route_cmd,
1549 "no debug ospf6 route <table|intra-area|inter-area|memory>",
1550 NO_STR
1551 DEBUG_STR
1552 OSPF6_STR
1553 "Debug routes\n"
1554 "Debug route table calculation\n"
1555 "Debug intra-area route calculation\n"
1556 "Debug inter-area route calculation\n"
1557 "Debug route memory use\n")
1558 {
1559 int idx_type = 4;
1560 unsigned char level = 0;
1561
1562 if (!strcmp(argv[idx_type]->text, "table"))
1563 level = OSPF6_DEBUG_ROUTE_TABLE;
1564 else if (!strcmp(argv[idx_type]->text, "intra-area"))
1565 level = OSPF6_DEBUG_ROUTE_INTRA;
1566 else if (!strcmp(argv[idx_type]->text, "inter-area"))
1567 level = OSPF6_DEBUG_ROUTE_INTER;
1568 else if (!strcmp(argv[idx_type]->text, "memory"))
1569 level = OSPF6_DEBUG_ROUTE_MEMORY;
1570 OSPF6_DEBUG_ROUTE_OFF(level);
1571 return CMD_SUCCESS;
1572 }
1573
1574 int config_write_ospf6_debug_route(struct vty *vty)
1575 {
1576 if (IS_OSPF6_DEBUG_ROUTE(TABLE))
1577 vty_out(vty, "debug ospf6 route table\n");
1578 if (IS_OSPF6_DEBUG_ROUTE(INTRA))
1579 vty_out(vty, "debug ospf6 route intra-area\n");
1580 if (IS_OSPF6_DEBUG_ROUTE(INTER))
1581 vty_out(vty, "debug ospf6 route inter-area\n");
1582 if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
1583 vty_out(vty, "debug ospf6 route memory\n");
1584
1585 return 0;
1586 }
1587
1588 void install_element_ospf6_debug_route(void)
1589 {
1590 install_element(ENABLE_NODE, &debug_ospf6_route_cmd);
1591 install_element(ENABLE_NODE, &no_debug_ospf6_route_cmd);
1592 install_element(CONFIG_NODE, &debug_ospf6_route_cmd);
1593 install_element(CONFIG_NODE, &no_debug_ospf6_route_cmd);
1594 }