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