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