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