forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathospf6_route.c
1860 lines (1597 loc) · 47.8 KB
/
ospf6_route.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2003 Yasuhiro Ohara
*/
#include <zebra.h>
#include "log.h"
#include "memory.h"
#include "prefix.h"
#include "table.h"
#include "vty.h"
#include "command.h"
#include "linklist.h"
#include "ospf6_proto.h"
#include "ospf6_lsa.h"
#include "ospf6_lsdb.h"
#include "ospf6_route.h"
#include "ospf6_top.h"
#include "ospf6_area.h"
#include "ospf6_interface.h"
#include "ospf6d.h"
#include "ospf6_zebra.h"
#include "ospf6d/ospf6_route_clippy.c"
DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_ROUTE, "OSPF6 route");
DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_ROUTE_TABLE, "OSPF6 route table");
DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_NEXTHOP, "OSPF6 nexthop");
DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_PATH, "OSPF6 Path");
unsigned char conf_debug_ospf6_route = 0;
static char *ospf6_route_table_name(struct ospf6_route_table *table)
{
static char name[64];
switch (table->scope_type) {
case OSPF6_SCOPE_TYPE_GLOBAL: {
switch (table->table_type) {
case OSPF6_TABLE_TYPE_ROUTES:
snprintf(name, sizeof(name), "global route table");
break;
case OSPF6_TABLE_TYPE_BORDER_ROUTERS:
snprintf(name, sizeof(name), "global brouter table");
break;
case OSPF6_TABLE_TYPE_EXTERNAL_ROUTES:
snprintf(name, sizeof(name), "global external table");
break;
default:
snprintf(name, sizeof(name), "global unknown table");
break;
}
} break;
case OSPF6_SCOPE_TYPE_AREA: {
struct ospf6_area *oa = (struct ospf6_area *)table->scope;
switch (table->table_type) {
case OSPF6_TABLE_TYPE_SPF_RESULTS:
snprintf(name, sizeof(name), "area %s spf table",
oa->name);
break;
case OSPF6_TABLE_TYPE_ROUTES:
snprintf(name, sizeof(name), "area %s route table",
oa->name);
break;
case OSPF6_TABLE_TYPE_PREFIX_RANGES:
snprintf(name, sizeof(name), "area %s range table",
oa->name);
break;
case OSPF6_TABLE_TYPE_SUMMARY_PREFIXES:
snprintf(name, sizeof(name),
"area %s summary prefix table", oa->name);
break;
case OSPF6_TABLE_TYPE_SUMMARY_ROUTERS:
snprintf(name, sizeof(name),
"area %s summary router table", oa->name);
break;
default:
snprintf(name, sizeof(name), "area %s unknown table",
oa->name);
break;
}
} break;
case OSPF6_SCOPE_TYPE_INTERFACE: {
struct ospf6_interface *oi =
(struct ospf6_interface *)table->scope;
switch (table->table_type) {
case OSPF6_TABLE_TYPE_CONNECTED_ROUTES:
snprintf(name, sizeof(name),
"interface %s connected table",
oi->interface->name);
break;
default:
snprintf(name, sizeof(name),
"interface %s unknown table",
oi->interface->name);
break;
}
} break;
default: {
switch (table->table_type) {
case OSPF6_TABLE_TYPE_SPF_RESULTS:
snprintf(name, sizeof(name), "temporary spf table");
break;
default:
snprintf(name, sizeof(name), "temporary unknown table");
break;
}
} break;
}
return name;
}
void ospf6_linkstate_prefix(uint32_t adv_router, uint32_t id,
struct prefix *prefix)
{
memset(prefix, 0, sizeof(struct prefix));
prefix->family = AF_INET6;
prefix->prefixlen = 64;
memcpy(&prefix->u.prefix6.s6_addr[0], &adv_router, 4);
memcpy(&prefix->u.prefix6.s6_addr[4], &id, 4);
}
void ospf6_linkstate_prefix2str(struct prefix *prefix, char *buf, int size)
{
uint32_t adv_router, id;
char adv_router_str[16], id_str[16];
memcpy(&adv_router, &prefix->u.prefix6.s6_addr[0], 4);
memcpy(&id, &prefix->u.prefix6.s6_addr[4], 4);
inet_ntop(AF_INET, &adv_router, adv_router_str, sizeof(adv_router_str));
inet_ntop(AF_INET, &id, id_str, sizeof(id_str));
if (ntohl(id))
snprintf(buf, size, "%s Net-ID: %s", adv_router_str, id_str);
else
snprintf(buf, size, "%s", adv_router_str);
}
/* Global strings for logging */
const char *const ospf6_dest_type_str[OSPF6_DEST_TYPE_MAX] = {
"Unknown", "Router", "Network", "Discard", "Linkstate", "AddressRange",
};
const char *const ospf6_dest_type_substr[OSPF6_DEST_TYPE_MAX] = {
"?", "R", "N", "D", "L", "A",
};
const char *const ospf6_path_type_str[OSPF6_PATH_TYPE_MAX] = {
"Unknown", "Intra-Area", "Inter-Area", "External-1", "External-2",
};
const char *const ospf6_path_type_substr[OSPF6_PATH_TYPE_MAX] = {
"??", "IA", "IE", "E1", "E2",
};
const char *ospf6_path_type_json[OSPF6_PATH_TYPE_MAX] = {
"UnknownRoute", "IntraArea", "InterArea", "External1", "External2",
};
struct ospf6_nexthop *ospf6_nexthop_create(void)
{
struct ospf6_nexthop *nh;
nh = XCALLOC(MTYPE_OSPF6_NEXTHOP, sizeof(struct ospf6_nexthop));
return nh;
}
void ospf6_nexthop_delete(struct ospf6_nexthop *nh)
{
XFREE(MTYPE_OSPF6_NEXTHOP, nh);
}
void ospf6_clear_nexthops(struct list *nh_list)
{
struct listnode *node;
struct ospf6_nexthop *nh;
if (nh_list) {
for (ALL_LIST_ELEMENTS_RO(nh_list, node, nh))
ospf6_nexthop_clear(nh);
}
}
static struct ospf6_nexthop *
ospf6_route_find_nexthop(struct list *nh_list, struct ospf6_nexthop *nh_match)
{
struct listnode *node;
struct ospf6_nexthop *nh;
if (nh_list && nh_match) {
for (ALL_LIST_ELEMENTS_RO(nh_list, node, nh)) {
if (ospf6_nexthop_is_same(nh, nh_match))
return (nh);
}
}
return (NULL);
}
void ospf6_copy_nexthops(struct list *dst, struct list *src)
{
struct ospf6_nexthop *nh_new, *nh;
struct listnode *node;
if (dst && src) {
for (ALL_LIST_ELEMENTS_RO(src, node, nh)) {
if (ospf6_nexthop_is_set(nh)) {
nh_new = ospf6_nexthop_create();
ospf6_nexthop_copy(nh_new, nh);
listnode_add_sort(dst, nh_new);
}
}
}
}
void ospf6_merge_nexthops(struct list *dst, struct list *src)
{
struct listnode *node;
struct ospf6_nexthop *nh, *nh_new;
if (src && dst) {
for (ALL_LIST_ELEMENTS_RO(src, node, nh)) {
if (!ospf6_route_find_nexthop(dst, nh)) {
nh_new = ospf6_nexthop_create();
ospf6_nexthop_copy(nh_new, nh);
listnode_add_sort(dst, nh_new);
}
}
}
}
/*
* If the nexthops are the same return true
*/
bool ospf6_route_cmp_nexthops(struct ospf6_route *a, struct ospf6_route *b)
{
struct listnode *anode, *bnode;
struct ospf6_nexthop *anh, *bnh;
bool identical = false;
if (a && b) {
if (listcount(a->nh_list) == listcount(b->nh_list)) {
for (ALL_LIST_ELEMENTS_RO(a->nh_list, anode, anh)) {
identical = false;
for (ALL_LIST_ELEMENTS_RO(b->nh_list, bnode,
bnh)) {
if (ospf6_nexthop_is_same(anh, bnh))
identical = true;
}
/* Currnet List A element not found List B
* Non-Identical lists return */
if (identical == false)
return false;
}
return true;
} else
return false;
}
/* One of the routes doesn't exist ? */
return false;
}
int ospf6_num_nexthops(struct list *nh_list)
{
return (listcount(nh_list));
}
void ospf6_add_nexthop(struct list *nh_list, int ifindex,
const struct in6_addr *addr)
{
struct ospf6_nexthop *nh;
struct ospf6_nexthop nh_match;
if (nh_list) {
if (addr) {
if (ifindex)
nh_match.type = NEXTHOP_TYPE_IPV6_IFINDEX;
else
nh_match.type = NEXTHOP_TYPE_IPV6;
memcpy(&nh_match.address, addr,
sizeof(struct in6_addr));
} else {
nh_match.type = NEXTHOP_TYPE_IFINDEX;
memset(&nh_match.address, 0, sizeof(struct in6_addr));
}
nh_match.ifindex = ifindex;
if (!ospf6_route_find_nexthop(nh_list, &nh_match)) {
nh = ospf6_nexthop_create();
ospf6_nexthop_copy(nh, &nh_match);
listnode_add(nh_list, nh);
}
}
}
void ospf6_add_route_nexthop_blackhole(struct ospf6_route *route)
{
struct ospf6_nexthop *nh;
struct ospf6_nexthop nh_match = {};
/* List not allocated. */
if (route->nh_list == NULL)
return;
/* Entry already exists. */
nh_match.type = NEXTHOP_TYPE_BLACKHOLE;
if (ospf6_route_find_nexthop(route->nh_list, &nh_match))
return;
nh = ospf6_nexthop_create();
ospf6_nexthop_copy(nh, &nh_match);
listnode_add(route->nh_list, nh);
}
void ospf6_route_zebra_copy_nexthops(struct ospf6_route *route,
struct zapi_nexthop nexthops[],
int entries, vrf_id_t vrf_id)
{
struct ospf6_nexthop *nh;
struct listnode *node;
int i;
if (route) {
i = 0;
for (ALL_LIST_ELEMENTS_RO(route->nh_list, node, nh)) {
if (IS_OSPF6_DEBUG_ZEBRA(SEND)) {
zlog_debug(" nexthop: %s %pI6%%%.*s(%d)",
nexthop_type_to_str(nh->type),
&nh->address, IFNAMSIZ,
ifindex2ifname(nh->ifindex, vrf_id),
nh->ifindex);
}
if (i >= entries)
return;
nexthops[i].vrf_id = vrf_id;
nexthops[i].type = nh->type;
switch (nh->type) {
case NEXTHOP_TYPE_BLACKHOLE:
/* NOTHING */
break;
case NEXTHOP_TYPE_IFINDEX:
nexthops[i].ifindex = nh->ifindex;
break;
case NEXTHOP_TYPE_IPV4_IFINDEX:
case NEXTHOP_TYPE_IPV4:
/*
* OSPFv3 with IPv4 routes is not supported
* yet. Skip this next hop.
*/
if (IS_OSPF6_DEBUG_ZEBRA(SEND))
zlog_debug(" Skipping IPv4 next hop");
continue;
case NEXTHOP_TYPE_IPV6_IFINDEX:
nexthops[i].ifindex = nh->ifindex;
fallthrough;
case NEXTHOP_TYPE_IPV6:
nexthops[i].gate.ipv6 = nh->address;
break;
}
i++;
}
}
}
int ospf6_route_get_first_nh_index(struct ospf6_route *route)
{
struct ospf6_nexthop *nh;
if (route) {
nh = listnode_head(route->nh_list);
if (nh)
return nh->ifindex;
}
return -1;
}
int ospf6_nexthop_cmp(struct ospf6_nexthop *a, struct ospf6_nexthop *b)
{
if (a->ifindex < b->ifindex)
return -1;
else if (a->ifindex > b->ifindex)
return 1;
else
return memcmp(&a->address, &b->address,
sizeof(struct in6_addr));
}
static int ospf6_path_cmp(struct ospf6_path *a, struct ospf6_path *b)
{
if (a->origin.adv_router < b->origin.adv_router)
return -1;
else if (a->origin.adv_router > b->origin.adv_router)
return 1;
else
return 0;
}
void ospf6_path_free(struct ospf6_path *op)
{
if (op->nh_list)
list_delete(&op->nh_list);
XFREE(MTYPE_OSPF6_PATH, op);
}
struct ospf6_path *ospf6_path_dup(struct ospf6_path *path)
{
struct ospf6_path *new;
new = XCALLOC(MTYPE_OSPF6_PATH, sizeof(struct ospf6_path));
memcpy(new, path, sizeof(struct ospf6_path));
new->nh_list = list_new();
new->nh_list->cmp = (int (*)(void *, void *))ospf6_nexthop_cmp;
new->nh_list->del = (void (*)(void *))ospf6_nexthop_delete;
return new;
}
void ospf6_copy_paths(struct list *dst, struct list *src)
{
struct ospf6_path *path_new, *path;
struct listnode *node;
if (dst && src) {
for (ALL_LIST_ELEMENTS_RO(src, node, path)) {
path_new = ospf6_path_dup(path);
ospf6_copy_nexthops(path_new->nh_list, path->nh_list);
listnode_add_sort(dst, path_new);
}
}
}
struct ospf6_route *ospf6_route_create(struct ospf6 *ospf6)
{
struct ospf6_route *route;
route = XCALLOC(MTYPE_OSPF6_ROUTE, sizeof(struct ospf6_route));
route->nh_list = list_new();
route->nh_list->cmp = (int (*)(void *, void *))ospf6_nexthop_cmp;
route->nh_list->del = (void (*)(void *))ospf6_nexthop_delete;
route->paths = list_new();
route->paths->cmp = (int (*)(void *, void *))ospf6_path_cmp;
route->paths->del = (void (*)(void *))ospf6_path_free;
route->ospf6 = ospf6;
return route;
}
void ospf6_route_delete(struct ospf6_route *route)
{
if (route) {
if (route->nh_list)
list_delete(&route->nh_list);
if (route->paths)
list_delete(&route->paths);
XFREE(MTYPE_OSPF6_ROUTE, route);
}
}
struct ospf6_route *ospf6_route_copy(struct ospf6_route *route)
{
struct ospf6_route *new;
new = ospf6_route_create(route->ospf6);
new->type = route->type;
memcpy(&new->prefix, &route->prefix, sizeof(struct prefix));
new->prefix_options = route->prefix_options;
new->installed = route->installed;
new->changed = route->changed;
new->flag = route->flag;
new->route_option = route->route_option;
new->linkstate_id = route->linkstate_id;
new->path = route->path;
ospf6_copy_nexthops(new->nh_list, route->nh_list);
ospf6_copy_paths(new->paths, route->paths);
new->rnode = NULL;
new->prev = NULL;
new->next = NULL;
new->table = NULL;
new->lock = 0;
return new;
}
void ospf6_route_lock(struct ospf6_route *route)
{
route->lock++;
}
void ospf6_route_unlock(struct ospf6_route *route)
{
assert(route->lock > 0);
route->lock--;
if (route->lock == 0) {
/* Can't detach from the table until here
because ospf6_route_next () will use
the 'route->table' pointer for logging */
route->table = NULL;
ospf6_route_delete(route);
}
}
/* Route compare function. If ra is more preferred, it returns
less than 0. If rb is more preferred returns greater than 0.
Otherwise (neither one is preferred), returns 0 */
int ospf6_route_cmp(struct ospf6_route *ra, struct ospf6_route *rb)
{
assert(ospf6_route_is_same(ra, rb));
assert(OSPF6_PATH_TYPE_NONE < ra->path.type
&& ra->path.type < OSPF6_PATH_TYPE_MAX);
assert(OSPF6_PATH_TYPE_NONE < rb->path.type
&& rb->path.type < OSPF6_PATH_TYPE_MAX);
if (ra->type != rb->type)
return (ra->type - rb->type);
if (ra->path.type != rb->path.type)
return (ra->path.type - rb->path.type);
if (ra->path.type == OSPF6_PATH_TYPE_EXTERNAL2) {
if (ra->path.u.cost_e2 != rb->path.u.cost_e2)
return (ra->path.u.cost_e2 - rb->path.u.cost_e2);
else
return (ra->path.cost - rb->path.cost);
} else {
if (ra->path.cost != rb->path.cost)
return (ra->path.cost - rb->path.cost);
}
if (ra->path.area_id != rb->path.area_id)
return (ntohl(ra->path.area_id) - ntohl(rb->path.area_id));
if ((ra->prefix_options & OSPF6_PREFIX_OPTION_LA)
!= (rb->prefix_options & OSPF6_PREFIX_OPTION_LA))
return ra->prefix_options & OSPF6_PREFIX_OPTION_LA ? -1 : 1;
return 0;
}
struct ospf6_route *ospf6_route_lookup(struct prefix *prefix,
struct ospf6_route_table *table)
{
struct route_node *node;
struct ospf6_route *route;
node = route_node_lookup(table->table, prefix);
if (node == NULL)
return NULL;
route = (struct ospf6_route *)node->info;
route_unlock_node(node); /* to free the lookup lock */
return route;
}
struct ospf6_route *
ospf6_route_lookup_identical(struct ospf6_route *route,
struct ospf6_route_table *table)
{
struct ospf6_route *target;
for (target = ospf6_route_lookup(&route->prefix, table); target;
target = target->next) {
if (ospf6_route_is_identical(target, route))
return target;
}
return NULL;
}
struct ospf6_route *
ospf6_route_lookup_bestmatch(struct prefix *prefix,
struct ospf6_route_table *table)
{
struct route_node *node;
struct ospf6_route *route;
node = route_node_match(table->table, prefix);
if (node == NULL)
return NULL;
route_unlock_node(node);
route = (struct ospf6_route *)node->info;
return route;
}
#ifdef DEBUG
static void route_table_assert(struct ospf6_route_table *table)
{
struct ospf6_route *prev, *r, *next;
unsigned int link_error = 0, num = 0;
r = ospf6_route_head(table);
prev = NULL;
while (r) {
if (r->prev != prev)
link_error++;
next = ospf6_route_next(r);
if (r->next != next)
link_error++;
prev = r;
r = next;
}
for (r = ospf6_route_head(table); r; r = ospf6_route_next(r))
num++;
if (link_error == 0 && num == table->count)
return;
flog_err(EC_LIB_DEVELOPMENT, "PANIC !!");
flog_err(EC_LIB_DEVELOPMENT,
"Something has gone wrong with ospf6_route_table[%p]", table);
zlog_debug("table count = %d, real number = %d", table->count, num);
zlog_debug("DUMP START");
for (r = ospf6_route_head(table); r; r = ospf6_route_next(r))
zlog_info("%p<-[%p]->%p : %pFX", r->prev, r, r->next,
&r->prefix);
zlog_debug("DUMP END");
assert(link_error == 0 && num == table->count);
}
#define ospf6_route_table_assert(t) (route_table_assert (t))
#else
#define ospf6_route_table_assert(t) ((void) 0)
#endif /*DEBUG*/
struct ospf6_route *ospf6_route_add(struct ospf6_route *route,
struct ospf6_route_table *table)
{
struct route_node *node, *nextnode, *prevnode;
struct ospf6_route *current = NULL;
struct ospf6_route *prev = NULL, *old = NULL, *next = NULL;
char buf[PREFIX2STR_BUFFER];
struct timeval now;
assert(route->rnode == NULL);
assert(route->lock == 0);
assert(route->next == NULL);
assert(route->prev == NULL);
if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
ospf6_linkstate_prefix2str(&route->prefix, buf, sizeof(buf));
else if (route->type == OSPF6_DEST_TYPE_ROUTER)
inet_ntop(AF_INET, &ADV_ROUTER_IN_PREFIX(&route->prefix), buf,
sizeof(buf));
else
prefix2str(&route->prefix, buf, sizeof(buf));
if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
zlog_debug("%s %p: route add %p: %s paths %u nh %u",
ospf6_route_table_name(table), (void *)table,
(void *)route, buf, listcount(route->paths),
listcount(route->nh_list));
else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
zlog_debug("%s: route add: %s", ospf6_route_table_name(table),
buf);
monotime(&now);
node = route_node_get(table->table, &route->prefix);
route->rnode = node;
/* find place to insert */
for (current = node->info; current; current = current->next) {
if (!ospf6_route_is_same(current, route))
next = current;
else if (current->type != route->type)
prev = current;
else if (ospf6_route_is_same_origin(current, route))
old = current;
else if (ospf6_route_cmp(current, route) > 0)
next = current;
else
prev = current;
if (old || next)
break;
}
if (old) {
/* if route does not actually change, return unchanged */
if (ospf6_route_is_identical(old, route)) {
if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
zlog_debug(
"%s %p: route add %p: needless update of %p old cost %u",
ospf6_route_table_name(table),
(void *)table, (void *)route,
(void *)old, old->path.cost);
else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
zlog_debug("%s: route add: needless update",
ospf6_route_table_name(table));
ospf6_route_delete(route);
SET_FLAG(old->flag, OSPF6_ROUTE_ADD);
ospf6_route_table_assert(table);
/* to free the lookup lock */
route_unlock_node(node);
return old;
}
if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
zlog_debug(
"%s %p: route add %p cost %u paths %u nh %u: update of %p cost %u paths %u nh %u",
ospf6_route_table_name(table), (void *)table,
(void *)route, route->path.cost,
listcount(route->paths),
listcount(route->nh_list), (void *)old,
old->path.cost, listcount(old->paths),
listcount(old->nh_list));
else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
zlog_debug("%s: route add: update",
ospf6_route_table_name(table));
/* replace old one if exists */
if (node->info == old) {
node->info = route;
SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
zlog_debug("%s: replace old route %s",
__func__, buf);
}
if (old->prev)
old->prev->next = route;
route->prev = old->prev;
if (old->next)
old->next->prev = route;
route->next = old->next;
route->installed = old->installed;
route->changed = now;
assert(route->table == NULL);
route->table = table;
ospf6_route_unlock(old); /* will be deleted later */
ospf6_route_lock(route);
SET_FLAG(route->flag, OSPF6_ROUTE_CHANGE);
ospf6_route_table_assert(table);
if (table->hook_add)
(*table->hook_add)(route);
return route;
}
/* insert if previous or next node found */
if (prev || next) {
if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
zlog_debug(
"%s %p: route add %p cost %u: another path: prev %p, next %p node ref %u",
ospf6_route_table_name(table), (void *)table,
(void *)route, route->path.cost, (void *)prev,
(void *)next, route_node_get_lock_count(node));
else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
zlog_debug("%s: route add cost %u: another path found",
ospf6_route_table_name(table),
route->path.cost);
if (prev == NULL)
prev = next->prev;
if (next == NULL)
next = prev->next;
if (prev)
prev->next = route;
route->prev = prev;
if (next)
next->prev = route;
route->next = next;
if (node->info == next) {
assert(next && next->rnode == node);
node->info = route;
UNSET_FLAG(next->flag, OSPF6_ROUTE_BEST);
SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
zlog_debug(
"%s %p: route add %p cost %u: replacing previous best: %p cost %u",
ospf6_route_table_name(table),
(void *)table, (void *)route,
route->path.cost, (void *)next,
next->path.cost);
}
route->installed = now;
route->changed = now;
assert(route->table == NULL);
route->table = table;
ospf6_route_lock(route);
table->count++;
ospf6_route_table_assert(table);
SET_FLAG(route->flag, OSPF6_ROUTE_ADD);
if (table->hook_add)
(*table->hook_add)(route);
return route;
}
/* Else, this is the brand new route regarding to the prefix */
if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
zlog_debug("%s %p: route add %p %s cost %u: brand new route",
ospf6_route_table_name(table), (void *)table,
(void *)route, buf, route->path.cost);
else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
zlog_debug("%s: route add: brand new route",
ospf6_route_table_name(table));
assert(node->info == NULL);
node->info = route;
SET_FLAG(route->flag, OSPF6_ROUTE_BEST);
ospf6_route_lock(route);
route->installed = now;
route->changed = now;
assert(route->table == NULL);
route->table = table;
/* lookup real existing next route */
nextnode = node;
route_lock_node(nextnode);
do {
nextnode = route_next(nextnode);
} while (nextnode && nextnode->info == NULL);
/* set next link */
if (nextnode == NULL)
route->next = NULL;
else {
route_unlock_node(nextnode);
next = nextnode->info;
route->next = next;
next->prev = route;
}
/* lookup real existing prev route */
prevnode = node;
route_lock_node(prevnode);
do {
prevnode = route_prev(prevnode);
} while (prevnode && prevnode->info == NULL);
/* set prev link */
if (prevnode == NULL)
route->prev = NULL;
else {
route_unlock_node(prevnode);
prev = prevnode->info;
while (prev->next && ospf6_route_is_same(prev, prev->next))
prev = prev->next;
route->prev = prev;
prev->next = route;
}
table->count++;
ospf6_route_table_assert(table);
SET_FLAG(route->flag, OSPF6_ROUTE_ADD);
if (table->hook_add)
(*table->hook_add)(route);
return route;
}
void ospf6_route_remove(struct ospf6_route *route,
struct ospf6_route_table *table)
{
struct route_node *node;
struct ospf6_route *current;
char buf[PREFIX2STR_BUFFER];
if (route->type == OSPF6_DEST_TYPE_LINKSTATE)
ospf6_linkstate_prefix2str(&route->prefix, buf, sizeof(buf));
else if (route->type == OSPF6_DEST_TYPE_ROUTER)
inet_ntop(AF_INET, &ADV_ROUTER_IN_PREFIX(&route->prefix), buf,
sizeof(buf));
else
prefix2str(&route->prefix, buf, sizeof(buf));
if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
zlog_debug("%s %p: route remove %p: %s cost %u refcount %u",
ospf6_route_table_name(table), (void *)table,
(void *)route, buf, route->path.cost, route->lock);
else if (IS_OSPF6_DEBUG_ROUTE(TABLE))
zlog_debug("%s: route remove: %s",
ospf6_route_table_name(table), buf);
node = route_node_lookup(table->table, &route->prefix);
assert(node);
/* find the route to remove, making sure that the route pointer
is from the route table. */
current = node->info;
while (current && current != route)
current = current->next;
assert(current == route);
/* adjust doubly linked list */
if (route->prev)
route->prev->next = route->next;
if (route->next)
route->next->prev = route->prev;
if (node->info == route) {
if (route->next && route->next->rnode == node) {
node->info = route->next;
SET_FLAG(route->next->flag, OSPF6_ROUTE_BEST);
if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
zlog_debug("%s: remove route %s", __func__,
buf);
} else {
node->info = NULL;
route->rnode = NULL;
route_unlock_node(node); /* to free the original lock */
}
}
route_unlock_node(node); /* to free the lookup lock */
table->count--;
ospf6_route_table_assert(table);
SET_FLAG(route->flag, OSPF6_ROUTE_WAS_REMOVED);
/* Note hook_remove may call ospf6_route_remove */
if (table->hook_remove)
(*table->hook_remove)(route);
ospf6_route_unlock(route);
}
struct ospf6_route *ospf6_route_head(struct ospf6_route_table *table)
{
struct route_node *node;
struct ospf6_route *route;
node = route_top(table->table);
if (node == NULL)
return NULL;
/* skip to the real existing entry */
while (node && node->info == NULL)
node = route_next(node);
if (node == NULL)
return NULL;
route_unlock_node(node);
assert(node->info);
route = (struct ospf6_route *)node->info;
assert(route->prev == NULL);
assert(route->table == table);
ospf6_route_lock(route);
if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
zlog_info("%s %p: route head: %p<-[%p]->%p",
ospf6_route_table_name(table), (void *)table,
(void *)route->prev, (void *)route,
(void *)route->next);
return route;
}
struct ospf6_route *ospf6_route_next(struct ospf6_route *route)
{
struct ospf6_route *next = route->next;
if (IS_OSPF6_DEBUG_ROUTE(MEMORY))
zlog_info("%s %p: route next: %p<-[%p]->%p , route ref count %u",
ospf6_route_table_name(route->table),
(void *)route->table, (void *)route->prev,
(void *)route, (void *)route->next,
route->lock);
ospf6_route_unlock(route);
if (next)
ospf6_route_lock(next);
return next;
}
struct ospf6_route *ospf6_route_best_next(struct ospf6_route *route)
{
struct route_node *rnode;