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