summaryrefslogtreecommitdiffstats
path: root/ospf6d/ospf6_asbr.c
blob: d1c2b8bfc9a4dc96c1c5b7d339e7c32f65fbef46 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
// 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 "command.h"
#include "vty.h"
#include "routemap.h"
#include "table.h"
#include "plist.h"
#include "frrevent.h"
#include "linklist.h"
#include "lib/northbound_cli.h"

#include "ospf6_proto.h"
#include "ospf6_lsa.h"
#include "ospf6_lsdb.h"
#include "ospf6_route.h"
#include "ospf6_zebra.h"
#include "ospf6_message.h"
#include "ospf6_spf.h"

#include "ospf6_top.h"
#include "ospf6d.h"
#include "ospf6_area.h"
#include "ospf6_interface.h"
#include "ospf6_neighbor.h"
#include "ospf6_asbr.h"
#include "ospf6_abr.h"
#include "ospf6_intra.h"
#include "ospf6_flood.h"
#include "ospf6_nssa.h"
#include "ospf6d.h"
#include "ospf6_spf.h"
#include "ospf6_nssa.h"
#include "ospf6_gr.h"
#include "lib/json.h"

DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_EXTERNAL_INFO, "OSPF6 ext. info");
DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_DIST_ARGS,     "OSPF6 Distribute arguments");
DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_REDISTRIBUTE, "OSPF6 Redistribute arguments");
DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_EXTERNAL_RT_AGGR, "OSPF6 ASBR Summarisation");

static void ospf6_asbr_redistribute_set(struct ospf6 *ospf6, int type);
static void ospf6_asbr_redistribute_unset(struct ospf6 *ospf6,
					  struct ospf6_redist *red, int type);

#include "ospf6d/ospf6_asbr_clippy.c"

unsigned char conf_debug_ospf6_asbr = 0;

#define ZROUTE_NAME(x) zebra_route_string(x)

/* Originate Type-5 and Type-7 LSA */
static struct ospf6_lsa *ospf6_originate_type5_type7_lsas(
						struct ospf6_route *route,
						struct ospf6 *ospf6)
{
	struct ospf6_lsa *lsa;
	struct listnode *lnode;
	struct ospf6_area *oa = NULL;

	lsa = ospf6_as_external_lsa_originate(route, ospf6);

	for (ALL_LIST_ELEMENTS_RO(ospf6->area_list, lnode, oa)) {
		if (IS_AREA_NSSA(oa))
			ospf6_nssa_lsa_originate(route, oa, true);
	}

	return lsa;
}

/* AS External LSA origination */
struct ospf6_lsa *ospf6_as_external_lsa_originate(struct ospf6_route *route,
					    struct ospf6 *ospf6)
{
	char buffer[OSPF6_MAX_LSASIZE];
	struct ospf6_lsa_header *lsa_header;
	struct ospf6_lsa *lsa;
	struct ospf6_external_info *info = route->route_option;

	struct ospf6_as_external_lsa *as_external_lsa;
	caddr_t p;

	if (ospf6->gr_info.restart_in_progress) {
		if (IS_DEBUG_OSPF6_GR)
			zlog_debug(
				"Graceful Restart in progress, don't originate LSA");
		return NULL;
	}

	if (IS_OSPF6_DEBUG_ASBR || IS_OSPF6_DEBUG_ORIGINATE(AS_EXTERNAL))
		zlog_debug("Originate AS-External-LSA for %pFX",
			   &route->prefix);

	/* prepare buffer */
	memset(buffer, 0, sizeof(buffer));
	lsa_header = (struct ospf6_lsa_header *)buffer;
	as_external_lsa = (struct ospf6_as_external_lsa
				   *)((caddr_t)lsa_header
				      + sizeof(struct ospf6_lsa_header));
	p = (caddr_t)((caddr_t)as_external_lsa
		      + sizeof(struct ospf6_as_external_lsa));

	/* Fill AS-External-LSA */
	/* Metric type */
	if (route->path.metric_type == 2)
		SET_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_E);
	else
		UNSET_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_E);

	/* forwarding address */
	if (!IN6_IS_ADDR_UNSPECIFIED(&info->forwarding))
		SET_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_F);
	else
		UNSET_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_F);

	/* external route tag */
	if (info->tag)
		SET_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_T);
	else
		UNSET_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_T);

	/* Set metric */
	OSPF6_ASBR_METRIC_SET(as_external_lsa, route->path.cost);

	/* prefixlen */
	as_external_lsa->prefix.prefix_length = route->prefix.prefixlen;

	/* PrefixOptions */
	as_external_lsa->prefix.prefix_options = route->prefix_options;

	/* don't use refer LS-type */
	as_external_lsa->prefix.prefix_refer_lstype = htons(0);

	/* set Prefix */
	memcpy(p, &route->prefix.u.prefix6,
	       OSPF6_PREFIX_SPACE(route->prefix.prefixlen));
	ospf6_prefix_apply_mask(&as_external_lsa->prefix);
	p += OSPF6_PREFIX_SPACE(route->prefix.prefixlen);

	/* Forwarding address */
	if (CHECK_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_F)) {
		memcpy(p, &info->forwarding, sizeof(struct in6_addr));
		p += sizeof(struct in6_addr);
	}

	/* External Route Tag */
	if (CHECK_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_T)) {
		route_tag_t network_order = htonl(info->tag);

		memcpy(p, &network_order, sizeof(network_order));
		p += sizeof(network_order);
	}

	/* Fill LSA Header */
	lsa_header->age = 0;
	lsa_header->type = htons(OSPF6_LSTYPE_AS_EXTERNAL);
	lsa_header->id = route->path.origin.id;
	lsa_header->adv_router = ospf6->router_id;
	lsa_header->seqnum =
		ospf6_new_ls_seqnum(lsa_header->type, lsa_header->id,
				    lsa_header->adv_router, ospf6->lsdb);
	lsa_header->length = htons((caddr_t)p - (caddr_t)lsa_header);

	/* LSA checksum */
	ospf6_lsa_checksum(lsa_header);

	/* create LSA */
	lsa = ospf6_lsa_create(lsa_header);

	/* Originate */
	ospf6_lsa_originate_process(lsa, ospf6);

	return lsa;
}

void ospf6_orig_as_external_lsa(struct event *thread)
{
	struct ospf6_interface *oi;
	struct ospf6_lsa *lsa;
	uint32_t type, adv_router;

	oi = (struct ospf6_interface *)EVENT_ARG(thread);

	if (oi->state == OSPF6_INTERFACE_DOWN)
		return;
	if (IS_AREA_NSSA(oi->area) || IS_AREA_STUB(oi->area))
		return;

	type = htons(OSPF6_LSTYPE_AS_EXTERNAL);
	adv_router = oi->area->ospf6->router_id;
	for (ALL_LSDB_TYPED_ADVRTR(oi->area->ospf6->lsdb, type, adv_router,
				   lsa)) {
		if (IS_OSPF6_DEBUG_ASBR)
			zlog_debug(
				"%s: Send update of AS-External LSA %s seq 0x%x",
				__func__, lsa->name,
				ntohl(lsa->header->seqnum));

		ospf6_flood_interface(NULL, lsa, oi);
	}
}

static route_tag_t ospf6_as_external_lsa_get_tag(struct ospf6_lsa *lsa)
{
	struct ospf6_as_external_lsa *external;
	ptrdiff_t tag_offset;
	route_tag_t network_order;

	if (!lsa)
		return 0;

	external = (struct ospf6_as_external_lsa *)OSPF6_LSA_HEADER_END(
		lsa->header);

	if (!CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_T))
		return 0;

	tag_offset = sizeof(*external)
		     + OSPF6_PREFIX_SPACE(external->prefix.prefix_length);
	if (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_F))
		tag_offset += sizeof(struct in6_addr);

	memcpy(&network_order, (caddr_t)external + tag_offset,
	       sizeof(network_order));
	return ntohl(network_order);
}

void ospf6_asbr_update_route_ecmp_path(struct ospf6_route *old,
				       struct ospf6_route *route,
				       struct ospf6 *ospf6)
{
	struct ospf6_route *old_route, *next_route;
	struct ospf6_path *ecmp_path, *o_path = NULL;
	struct listnode *anode, *anext;
	struct listnode *nnode, *rnode, *rnext;
	struct ospf6_nexthop *nh, *rnh;
	bool route_found = false;

	/* check for old entry match with new route origin,
	 * delete old entry.
	 */
	for (old_route = old; old_route; old_route = next_route) {
		bool route_updated = false;

		next_route = old_route->next;

		/* The route linked-list is grouped in batches of prefix.
		 * If the new prefix is not the same as the one of interest
		 * then we have walked over the end of the batch and so we
		 * should break rather than continuing unnecessarily.
		 */
		if (!ospf6_route_is_same(old_route, route))
			break;
		if (old_route->path.type != route->path.type)
			continue;

		/* Current and New route has same origin,
		 * delete old entry.
		 */
		for (ALL_LIST_ELEMENTS(old_route->paths, anode, anext,
				       o_path)) {
			/* Check old route path and route has same
			 * origin.
			 */
			if (o_path->area_id != route->path.area_id
			    || !ospf6_ls_origin_same(o_path, &route->path))
				continue;

			/* Cost is not same then delete current path */
			if ((o_path->cost == route->path.cost)
			    && (o_path->u.cost_e2 == route->path.u.cost_e2))
				continue;

			if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
				zlog_debug(
					"%s: route %pFX cost old %u new %u is not same, replace route",
					__func__, &old_route->prefix, o_path->cost,
					route->path.cost);
			}

			/* Remove selected current rout path's nh from
			 * effective nh list.
			 */
			for (ALL_LIST_ELEMENTS_RO(o_path->nh_list, nnode, nh)) {
				for (ALL_LIST_ELEMENTS(old_route->nh_list,
						       rnode, rnext, rnh)) {
					if (!ospf6_nexthop_is_same(rnh, nh))
						continue;
					listnode_delete(old_route->nh_list,
							rnh);
					ospf6_nexthop_delete(rnh);
				}
			}

			listnode_delete(old_route->paths, o_path);
			ospf6_path_free(o_path);
			route_updated = true;

			/* Current route's path (adv_router info) is similar
			 * to route being added.
			 * Replace current route's path with paths list head.
			 * Update FIB with effective NHs.
			 */
			if (listcount(old_route->paths)) {
				for (ALL_LIST_ELEMENTS(old_route->paths,
						anode, anext, o_path)) {
					ospf6_merge_nexthops(
						old_route->nh_list,
						o_path->nh_list);
				}
				/* Update RIB/FIB with effective
				 * nh_list
				 */
				if (ospf6->route_table->hook_add)
					(*ospf6->route_table->hook_add)(
						old_route);

				if (old_route->path.origin.id
					    == route->path.origin.id
				    && old_route->path.origin.adv_router
					       == route->path.origin
							  .adv_router) {
					struct ospf6_path *h_path;

					h_path = (struct ospf6_path *)
						listgetdata(listhead(
							old_route->paths));
					old_route->path.origin.type =
						h_path->origin.type;
					old_route->path.origin.id =
						h_path->origin.id;
					old_route->path.origin.adv_router =
						h_path->origin.adv_router;
				}
			} else {
				if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
					zlog_debug(
						"%s: route %pFX old cost %u new cost %u, delete old entry.",
						__func__, &old_route->prefix,
						old_route->path.cost,
						route->path.cost);
				}
				if (old == old_route)
					old = next_route;
				ospf6_route_remove(old_route,
						   ospf6->route_table);
			}
		}
		if (route_updated)
			break;
	}

	/* Add new route */
	for (old_route = old; old_route; old_route = old_route->next) {

		/* The route linked-list is grouped in batches of prefix.
		 * If the new prefix is not the same as the one of interest
		 * then we have walked over the end of the batch and so we
		 * should break rather than continuing unnecessarily.
		 */
		if (!ospf6_route_is_same(old_route, route))
			break;
		if (old_route->path.type != route->path.type)
			continue;

		/* Old Route and New Route have Equal Cost, Merge NHs */
		if ((old_route->path.cost == route->path.cost)
		    && (old_route->path.u.cost_e2 == route->path.u.cost_e2)) {

			if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
				zlog_debug(
					"%s: old route %pFX path  cost %u e2 %u",
					__func__, &old_route->prefix,
					old_route->path.cost,
					old_route->path.u.cost_e2);
			}
			route_found = true;
			/* check if this path exists already in
			 * route->paths list, if so, replace nh_list
			 * from asbr_entry.
			 */
			for (ALL_LIST_ELEMENTS_RO(old_route->paths, anode,
						  o_path)) {
				if (o_path->area_id == route->path.area_id
				    && ospf6_ls_origin_same(o_path, &route->path))
					break;
			}
			/* If path is not found in old_route paths's list,
			 * add a new path to route paths list and merge
			 * nexthops in route->path->nh_list.
			 * Otherwise replace existing path's nh_list.
			 */
			if (o_path == NULL) {
				ecmp_path = ospf6_path_dup(&route->path);

				/* Add a nh_list to new ecmp path */
				ospf6_copy_nexthops(ecmp_path->nh_list,
						    route->nh_list);

				/* Add the new path to route's path list */
				listnode_add_sort(old_route->paths, ecmp_path);

				if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
					zlog_debug(
						"%s: route %pFX another path added with nh %u, effective paths %u nh %u",
						__func__, &route->prefix,
						listcount(ecmp_path->nh_list),
						old_route->paths ? listcount(
							old_route->paths)
								 : 0,
						listcount(old_route->nh_list));
				}
			} else {
				list_delete_all_node(o_path->nh_list);
				ospf6_copy_nexthops(o_path->nh_list,
						    route->nh_list);
			}

			/* Reset nexthop lists, rebuild from brouter table
			 * for each adv. router.
			 */
			list_delete_all_node(old_route->nh_list);

			for (ALL_LIST_ELEMENTS_RO(old_route->paths, anode,
						  o_path)) {
				struct ospf6_route *asbr_entry;

				asbr_entry = ospf6_route_lookup(
							&o_path->ls_prefix,
							ospf6->brouter_table);
				if (asbr_entry == NULL) {
					if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
						zlog_debug(
							"%s: ls_prfix %pFX asbr_entry not found.",
							__func__,
							&old_route->prefix);
					continue;
				}
				ospf6_route_merge_nexthops(old_route,
							   asbr_entry);
			}

			if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
				zlog_debug(
					"%s: route %pFX with effective paths %u nh %u",
					__func__, &route->prefix,
					old_route->paths
						? listcount(old_route->paths)
						: 0,
					old_route->nh_list
						? listcount(old_route->nh_list)
						: 0);

			/* Update RIB/FIB */
			if (ospf6->route_table->hook_add)
				(*ospf6->route_table->hook_add)(old_route);

			/* Delete the new route its info added to existing
			 * route.
			 */
			ospf6_route_delete(route);

			break;
		}
	}

	if (!route_found) {
		/* Add new route to existing node in ospf6 route table. */
		ospf6_route_add(route, ospf6->route_table);
	}
}

/* Check if the forwarding address is local address */
static int ospf6_ase_forward_address_check(struct ospf6 *ospf6,
					   struct in6_addr *fwd_addr)
{
	struct listnode *anode, *node;
	struct ospf6_interface *oi;
	struct ospf6_area *oa;
	struct interface *ifp;
	struct connected *c;

	for (ALL_LIST_ELEMENTS_RO(ospf6->area_list, anode, oa)) {
		for (ALL_LIST_ELEMENTS_RO(oa->if_list, node, oi)) {
			if (!if_is_operative(oi->interface)
			    || oi->type == OSPF_IFTYPE_VIRTUALLINK)
				continue;

			ifp = oi->interface;
			frr_each (if_connected, ifp->connected, c) {
				if (IPV6_ADDR_SAME(&c->address->u.prefix6,
						   fwd_addr))
					return 0;
			}
		}
	}

	return 1;
}

void ospf6_asbr_lsa_add(struct ospf6_lsa *lsa)
{
	struct ospf6_as_external_lsa *external;
	struct prefix asbr_id;
	struct ospf6_route *asbr_entry, *route, *old = NULL;
	struct ospf6_path *path;
	struct ospf6 *ospf6;
	int type;
	struct ospf6_area *oa = NULL;
	struct prefix fwd_addr;
	ptrdiff_t offset;

	type = ntohs(lsa->header->type);
	oa = lsa->lsdb->data;

	external = (struct ospf6_as_external_lsa *)OSPF6_LSA_HEADER_END(
		lsa->header);

	if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
		zlog_debug("Calculate AS-External route for %s", lsa->name);

	ospf6 = ospf6_get_by_lsdb(lsa);

	if (lsa->header->adv_router == ospf6->router_id) {
		if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
			zlog_debug("Ignore self-originated AS-External-LSA");
		return;
	}

	if (OSPF6_ASBR_METRIC(external) == OSPF_LS_INFINITY) {
		if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
			zlog_debug("Ignore LSA with LSInfinity Metric");
		return;
	}

	if (CHECK_FLAG(external->prefix.prefix_options,
		       OSPF6_PREFIX_OPTION_NU)) {
		if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
			zlog_debug("Ignore LSA with NU bit set Metric");
		return;
	}

	ospf6_linkstate_prefix(lsa->header->adv_router, htonl(0), &asbr_id);
	asbr_entry = ospf6_route_lookup(&asbr_id, ospf6->brouter_table);
	if (asbr_entry == NULL) {
		if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
			zlog_debug("ASBR entry not found: %pFX", &asbr_id);
		return;
	} else {
		/* The router advertising external LSA can be ASBR or ABR */
		if (!CHECK_FLAG(asbr_entry->path.router_bits,
				OSPF6_ROUTER_BIT_E)) {
			if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
				zlog_debug(
					"External bit reset ASBR route entry : %pFX",
					&asbr_id);
			return;
		}

		/*
		 * RFC 3101 - Section 2.5:
		 * "For a Type-7 LSA the matching routing table entry must
		 * specify an intra-area path through the LSA's originating
		 * NSSA".
		 */
		if (ntohs(lsa->header->type) == OSPF6_LSTYPE_TYPE_7
		    && (asbr_entry->path.area_id != oa->area_id
			|| asbr_entry->path.type != OSPF6_PATH_TYPE_INTRA)) {
			if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
				zlog_debug(
					"Intra-area route to NSSA ASBR not found: %pFX",
					&asbr_id);
			return;
		}
	}

	/*
	 * RFC 3101 - Section 2.5:
	 * "If the destination is a Type-7 default route (destination ID =
	 * DefaultDestination) and one of the following is true, then do
	 * nothing with this LSA and consider the next in the list:
	 *
	 *  o  The calculating router is a border router and the LSA has
	 *     its P-bit clear.  Appendix E describes a technique
	 *     whereby an NSSA border router installs a Type-7 default
	 *     LSA without propagating it.
	 *
	 *  o  The calculating router is a border router and is
	 *     suppressing the import of summary routes as Type-3
	 *     summary-LSAs".
	 */
	if (ntohs(lsa->header->type) == OSPF6_LSTYPE_TYPE_7
	    && external->prefix.prefix_length == 0
	    && CHECK_FLAG(ospf6->flag, OSPF6_FLAG_ABR)
	    && (CHECK_FLAG(external->prefix.prefix_options,
			   OSPF6_PREFIX_OPTION_P)
		|| oa->no_summary)) {
		if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
			zlog_debug("Skipping Type-7 default route");
		return;
	}

	/* Check the forwarding address */
	if (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_F)) {
		offset = sizeof(*external)
			 + OSPF6_PREFIX_SPACE(external->prefix.prefix_length);
		memset(&fwd_addr, 0, sizeof(fwd_addr));
		fwd_addr.family = AF_INET6;
		fwd_addr.prefixlen = IPV6_MAX_BITLEN;
		memcpy(&fwd_addr.u.prefix6, (caddr_t)external + offset,
		       sizeof(struct in6_addr));

		if (!IN6_IS_ADDR_UNSPECIFIED(&fwd_addr.u.prefix6)) {
			if (!ospf6_ase_forward_address_check(
				    ospf6, &fwd_addr.u.prefix6)) {
				if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
					zlog_debug(
						"Fwd address %pFX is local address",
						&fwd_addr);
				return;
			}

			/* Find the forwarding entry */
			asbr_entry = ospf6_route_lookup_bestmatch(
				&fwd_addr, ospf6->route_table);
			if (asbr_entry == NULL) {
				if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
					zlog_debug(
						"Fwd address not found: %pFX",
						&fwd_addr);
				return;
			}
		}
	}

	route = ospf6_route_create(ospf6);
	route->type = OSPF6_DEST_TYPE_NETWORK;
	route->prefix.family = AF_INET6;
	route->prefix.prefixlen = external->prefix.prefix_length;
	ospf6_prefix_in6_addr(&route->prefix.u.prefix6, external,
			      &external->prefix);
	route->prefix_options = external->prefix.prefix_options;

	route->path.area_id = asbr_entry->path.area_id;
	route->path.origin.type = lsa->header->type;
	route->path.origin.id = lsa->header->id;
	route->path.origin.adv_router = lsa->header->adv_router;
	memcpy(&route->path.ls_prefix, &asbr_id, sizeof(struct prefix));

	if (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_E)) {
		route->path.type = OSPF6_PATH_TYPE_EXTERNAL2;
		route->path.metric_type = 2;
		route->path.cost = asbr_entry->path.cost;
		route->path.u.cost_e2 = OSPF6_ASBR_METRIC(external);
	} else {
		route->path.type = OSPF6_PATH_TYPE_EXTERNAL1;
		route->path.metric_type = 1;
		route->path.cost =
			asbr_entry->path.cost + OSPF6_ASBR_METRIC(external);
		route->path.u.cost_e2 = 0;
	}

	route->path.tag = ospf6_as_external_lsa_get_tag(lsa);

	ospf6_route_copy_nexthops(route, asbr_entry);

	path = ospf6_path_dup(&route->path);
	ospf6_copy_nexthops(path->nh_list, asbr_entry->nh_list);
	listnode_add_sort(route->paths, path);


	if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
		zlog_debug(
			"%s: %s %u route add %pFX cost %u(%u) nh %u", __func__,
			(type == OSPF6_LSTYPE_AS_EXTERNAL) ? "AS-External"
							   : "NSSA",
			(route->path.type == OSPF6_PATH_TYPE_EXTERNAL1) ? 1 : 2,
			&route->prefix, route->path.cost, route->path.u.cost_e2,
			listcount(route->nh_list));

	if (type == OSPF6_LSTYPE_AS_EXTERNAL)
		old = ospf6_route_lookup(&route->prefix, ospf6->route_table);
	else if (type == OSPF6_LSTYPE_TYPE_7)
		old = ospf6_route_lookup(&route->prefix, oa->route_table);
	if (!old) {
		if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
			zlog_debug("%s: Adding new route", __func__);
		/* Add the new route to ospf6 instance route table. */
		if (type == OSPF6_LSTYPE_AS_EXTERNAL)
			ospf6_route_add(route, ospf6->route_table);
		/* Add the route to the area route table */
		else if (type == OSPF6_LSTYPE_TYPE_7) {
			ospf6_route_add(route, oa->route_table);
		}
	} else {
		/* RFC 2328 16.4 (6)
		 * ECMP: Keep new equal preference path in current
		 * route's path list, update zebra with new effective
		 * list along with addition of ECMP path.
		 */
		if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
			zlog_debug("%s : old route %pFX cost %u(%u) nh %u",
				   __func__, &route->prefix, route->path.cost,
				   route->path.u.cost_e2,
				   listcount(route->nh_list));
		ospf6_asbr_update_route_ecmp_path(old, route, ospf6);
	}
}

void ospf6_asbr_lsa_remove(struct ospf6_lsa *lsa,
			   struct ospf6_route *asbr_entry)
{
	struct ospf6_as_external_lsa *external;
	struct prefix prefix;
	struct ospf6_route *route, *nroute, *route_to_del;
	struct ospf6_area *oa = NULL;
	struct ospf6 *ospf6;
	int type;
	bool debug = false;

	external = (struct ospf6_as_external_lsa *)OSPF6_LSA_HEADER_END(
		lsa->header);

	if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL) || (IS_OSPF6_DEBUG_NSSA))
		debug = true;

	ospf6 = ospf6_get_by_lsdb(lsa);
	type = ntohs(lsa->header->type);

	if (type == OSPF6_LSTYPE_TYPE_7) {
		if (debug)
			zlog_debug("%s: Withdraw  Type 7 route for %s",
				   __func__, lsa->name);
		oa = lsa->lsdb->data;
	} else {
		if (debug)
			zlog_debug("%s: Withdraw AS-External route for %s",
				   __func__, lsa->name);

		if (ospf6_check_and_set_router_abr(ospf6))
			oa = ospf6->backbone;
		else
			oa = listnode_head(ospf6->area_list);
	}

	if (oa == NULL) {
		if (debug)
			zlog_debug("%s: Invalid area", __func__);
		return;
	}

	if (lsa->header->adv_router == oa->ospf6->router_id) {
		if (debug)
			zlog_debug("Ignore self-originated AS-External-LSA");
		return;
	}

	route_to_del = ospf6_route_create(ospf6);
	route_to_del->type = OSPF6_DEST_TYPE_NETWORK;
	route_to_del->prefix.family = AF_INET6;
	route_to_del->prefix.prefixlen = external->prefix.prefix_length;
	ospf6_prefix_in6_addr(&route_to_del->prefix.u.prefix6, external,
			      &external->prefix);

	route_to_del->path.origin.type = lsa->header->type;
	route_to_del->path.origin.id = lsa->header->id;
	route_to_del->path.origin.adv_router = lsa->header->adv_router;

	if (asbr_entry) {
		route_to_del->path.area_id = asbr_entry->path.area_id;
		if (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_E)) {
			route_to_del->path.type = OSPF6_PATH_TYPE_EXTERNAL2;
			route_to_del->path.metric_type = 2;
			route_to_del->path.cost = asbr_entry->path.cost;
			route_to_del->path.u.cost_e2 =
				OSPF6_ASBR_METRIC(external);
		} else {
			route_to_del->path.type = OSPF6_PATH_TYPE_EXTERNAL1;
			route_to_del->path.metric_type = 1;
			route_to_del->path.cost = asbr_entry->path.cost
						  + OSPF6_ASBR_METRIC(external);
			route_to_del->path.u.cost_e2 = 0;
		}
	}

	memset(&prefix, 0, sizeof(struct prefix));
	prefix.family = AF_INET6;
	prefix.prefixlen = external->prefix.prefix_length;
	ospf6_prefix_in6_addr(&prefix.u.prefix6, external, &external->prefix);

	if (type == OSPF6_LSTYPE_TYPE_7)
		route = ospf6_route_lookup(&prefix, oa->route_table);
	else
		route = ospf6_route_lookup(&prefix, oa->ospf6->route_table);

	if (route == NULL) {
		if (debug)
			zlog_debug("AS-External route %pFX not found", &prefix);
		ospf6_route_delete(route_to_del);
		return;
	}

	if (debug)
		zlog_debug(
			"%s: Current route %pFX cost %u e2 %u, route to del cost %u e2 %u",
			__func__, &prefix, route->path.cost, route->path.u.cost_e2,
			route_to_del->path.cost, route_to_del->path.u.cost_e2);

	for (ospf6_route_lock(route);
	     route && ospf6_route_is_prefix(&prefix, route); route = nroute) {
		nroute = ospf6_route_next(route);

		if (route->type != OSPF6_DEST_TYPE_NETWORK)
			continue;

		/* Route has multiple ECMP paths, remove matching
		 * path. Update current route's effective nh list
		 * after removal of one of the path.
		 */
		if (listcount(route->paths) > 1) {
			struct listnode *anode, *anext;
			struct listnode *nnode, *rnode, *rnext;
			struct ospf6_nexthop *nh, *rnh;
			struct ospf6_path *o_path;
			bool nh_updated = false;

			/* Iterate all paths of route to find maching with LSA
			 * remove from route path list. If route->path is same,
			 * replace from paths list.
			 */
			for (ALL_LIST_ELEMENTS(route->paths, anode, anext,
					       o_path)) {
				if ((o_path->origin.type != lsa->header->type)
				    || (o_path->origin.adv_router
					!= lsa->header->adv_router)
				    || (o_path->origin.id != lsa->header->id))
					continue;

				/* Compare LSA cost with current
				 * route info.
				 */
				if (asbr_entry
				    && (o_path->cost != route_to_del->path.cost
					|| o_path->u.cost_e2
						   != route_to_del->path.u
							      .cost_e2)) {
					if (IS_OSPF6_DEBUG_EXAMIN(
						    AS_EXTERNAL)) {
						zlog_debug(
							"%s: route %pFX to delete is not same, cost %u del cost %u. skip",
							__func__, &prefix,
							route->path.cost,
							route_to_del->path
								.cost);
					}
					continue;
				}

				if (debug) {
					zlog_debug(
						"%s: route %pFX path found with cost %u nh %u to remove.",
						__func__, &prefix, route->path.cost,
						listcount(o_path->nh_list));
				}

				/* Remove found path's nh_list from
				 * the route's nh_list.
				 */
				for (ALL_LIST_ELEMENTS_RO(o_path->nh_list,
							  nnode, nh)) {
					for (ALL_LIST_ELEMENTS(route->nh_list,
							       rnode, rnext,
							       rnh)) {
						if (!ospf6_nexthop_is_same(rnh,
									   nh))
							continue;
						listnode_delete(route->nh_list,
								rnh);
						ospf6_nexthop_delete(rnh);
					}
				}
				/* Delete the path from route's path list */
				listnode_delete(route->paths, o_path);
				ospf6_path_free(o_path);
				nh_updated = true;
			}

			if (nh_updated) {
				/* Iterate all paths and merge nexthop,
				 * unlesss any of the nexthop similar to
				 * ones deleted as part of path deletion.
				 */

				for (ALL_LIST_ELEMENTS(route->paths, anode,
						       anext, o_path)) {
					ospf6_merge_nexthops(route->nh_list,
							     o_path->nh_list);
				}

				if (debug) {
					zlog_debug(
						"%s: AS-External %u route %pFX update paths %u nh %u",
						__func__,
						(route->path.type
						 == OSPF6_PATH_TYPE_EXTERNAL1)
							? 1
							: 2,
						&route->prefix, listcount(route->paths),
						route->nh_list ? listcount(
							route->nh_list)
							       : 0);
				}

				if (listcount(route->paths)) {
					/* Update RIB/FIB with effective
					 * nh_list
					 */
					if (oa->ospf6->route_table->hook_add)
						(*oa->ospf6->route_table
							  ->hook_add)(route);

					/* route's primary path is similar
					 * to LSA, replace route's primary
					 * path with route's paths list head.
					 */
					if ((route->path.origin.id ==
					    lsa->header->id) &&
					    (route->path.origin.adv_router
						 == lsa->header->adv_router)) {
						struct ospf6_path *h_path;

						h_path = (struct ospf6_path *)
						listgetdata(
							listhead(route->paths));
						route->path.origin.type =
							h_path->origin.type;
						route->path.origin.id =
							h_path->origin.id;
						route->path.origin.adv_router =
						h_path->origin.adv_router;
					}
				} else {
					if (type == OSPF6_LSTYPE_TYPE_7)
						ospf6_route_remove(
							route, oa->route_table);
					else
						ospf6_route_remove(
							route,
							oa->ospf6->route_table);
				}
			}
			continue;

		} else {
			/* Compare LSA origin and cost with current route info.
			 * if any check fails skip del this route node.
			 */
			if (asbr_entry
			    && (!ospf6_route_is_same_origin(route, route_to_del)
				|| (route->path.type != route_to_del->path.type)
				|| (route->path.cost != route_to_del->path.cost)
				|| (route->path.u.cost_e2
				    != route_to_del->path.u.cost_e2))) {
				if (debug) {
					zlog_debug(
						"%s: route %pFX to delete is not same, cost %u del cost %u. skip",
						__func__, &prefix, route->path.cost,
						route_to_del->path.cost);
				}
				continue;
			}

			if ((route->path.origin.type != lsa->header->type)
			    || (route->path.origin.adv_router
				!= lsa->header->adv_router)
			    || (route->path.origin.id != lsa->header->id))
				continue;
		}
		if (debug) {
			zlog_debug(
				"%s: AS-External %u route remove %pFX cost %u(%u) nh %u",
				__func__,
				route->path.type == OSPF6_PATH_TYPE_EXTERNAL1
					? 1
					: 2,
				&route->prefix, route->path.cost, route->path.u.cost_e2,
				listcount(route->nh_list));
		}
		if (type == OSPF6_LSTYPE_TYPE_7)
			ospf6_route_remove(route, oa->route_table);
		else
			ospf6_route_remove(route, oa->ospf6->route_table);
	}
	if (route != NULL)
		ospf6_route_unlock(route);

	ospf6_route_delete(route_to_del);
}

void ospf6_asbr_lsentry_add(struct ospf6_route *asbr_entry, struct ospf6 *ospf6)
{
	struct ospf6_lsa *lsa;
	uint16_t type;
	uint32_t router;

	if (!CHECK_FLAG(asbr_entry->flag, OSPF6_ROUTE_BEST)) {
		char buf[16];
		inet_ntop(AF_INET, &ADV_ROUTER_IN_PREFIX(&asbr_entry->prefix),
			  buf, sizeof(buf));
		zlog_info("ignore non-best path: lsentry %s add", buf);
		return;
	}

	type = htons(OSPF6_LSTYPE_AS_EXTERNAL);
	router = ospf6_linkstate_prefix_adv_router(&asbr_entry->prefix);
	for (ALL_LSDB_TYPED_ADVRTR(ospf6->lsdb, type, router, lsa)) {
		if (!OSPF6_LSA_IS_MAXAGE(lsa))
			ospf6_asbr_lsa_add(lsa);
	}
}

void ospf6_asbr_lsentry_remove(struct ospf6_route *asbr_entry,
			       struct ospf6 *ospf6)
{
	struct ospf6_lsa *lsa;
	uint16_t type;
	uint32_t router;

	type = htons(OSPF6_LSTYPE_AS_EXTERNAL);
	router = ospf6_linkstate_prefix_adv_router(&asbr_entry->prefix);
	for (ALL_LSDB_TYPED_ADVRTR(ospf6->lsdb, type, router, lsa))
		ospf6_asbr_lsa_remove(lsa, asbr_entry);
}


/* redistribute function */
static void ospf6_asbr_routemap_set(struct ospf6_redist *red,
				    const char *mapname)
{
	if (ROUTEMAP_NAME(red)) {
		route_map_counter_decrement(ROUTEMAP(red));
		free(ROUTEMAP_NAME(red));
	}

	ROUTEMAP_NAME(red) = strdup(mapname);
	ROUTEMAP(red) = route_map_lookup_by_name(mapname);
	route_map_counter_increment(ROUTEMAP(red));
}

static void ospf6_asbr_routemap_unset(struct ospf6_redist *red)
{
	if (ROUTEMAP_NAME(red))
		free(ROUTEMAP_NAME(red));

	route_map_counter_decrement(ROUTEMAP(red));

	ROUTEMAP_NAME(red) = NULL;
	ROUTEMAP(red) = NULL;
}

static void ospf6_asbr_routemap_update_timer(struct event *thread)
{
	struct ospf6 *ospf6 = EVENT_ARG(thread);
	struct ospf6_redist *red;
	int type;

	for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
		red = ospf6_redist_lookup(ospf6, type, 0);

		if (!red)
			continue;

		if (!CHECK_FLAG(red->flag, OSPF6_IS_RMAP_CHANGED))
			continue;

		if (ROUTEMAP_NAME(red))
			ROUTEMAP(red) =
				route_map_lookup_by_name(ROUTEMAP_NAME(red));

		if (ROUTEMAP(red)) {
			if (IS_OSPF6_DEBUG_ASBR)
				zlog_debug(
					"%s: route-map %s update, reset redist %s",
					__func__, ROUTEMAP_NAME(red),
					ZROUTE_NAME(type));

			ospf6_zebra_no_redistribute(type, ospf6->vrf_id);
			ospf6_zebra_redistribute(type, ospf6->vrf_id);
		}

		UNSET_FLAG(red->flag, OSPF6_IS_RMAP_CHANGED);
	}
}

void ospf6_asbr_distribute_list_update(struct ospf6 *ospf6,
				       struct ospf6_redist *red)
{
	SET_FLAG(red->flag, OSPF6_IS_RMAP_CHANGED);

	if (event_is_scheduled(ospf6->t_distribute_update))
		return;

	if (IS_OSPF6_DEBUG_ASBR)
		zlog_debug("%s: trigger redistribute reset thread", __func__);

	event_add_timer_msec(master, ospf6_asbr_routemap_update_timer, ospf6,
			     OSPF_MIN_LS_INTERVAL, &ospf6->t_distribute_update);
}

void ospf6_asbr_routemap_update(const char *mapname)
{
	int type;
	struct listnode *node, *nnode;
	struct ospf6 *ospf6 = NULL;
	struct ospf6_redist *red;

	if (om6 == NULL)
		return;

	for (ALL_LIST_ELEMENTS(om6->ospf6, node, nnode, ospf6)) {
		for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
			red = ospf6_redist_lookup(ospf6, type, 0);
			if (!red || (ROUTEMAP_NAME(red) == NULL))
				continue;
			ROUTEMAP(red) =
				route_map_lookup_by_name(ROUTEMAP_NAME(red));

			if (mapname == NULL
			    || strcmp(ROUTEMAP_NAME(red), mapname))
				continue;
			if (ROUTEMAP(red)) {
				if (IS_OSPF6_DEBUG_ASBR)
					zlog_debug(
							"%s: route-map %s update, reset redist %s",
							__func__,
							mapname,
							ZROUTE_NAME(
								type));

				route_map_counter_increment(ROUTEMAP(red));
				ospf6_asbr_distribute_list_update(ospf6, red);
			} else {
				/*
				* if the mapname matches a
				* route-map on ospf6 but the
				* map doesn't exist, it is
				* being deleted. flush and then
				* readvertise
				*/
				if (IS_OSPF6_DEBUG_ASBR)
					zlog_debug(
							"%s: route-map %s deleted, reset redist %s",
							__func__,
							mapname,
							ZROUTE_NAME(
								type));
				ospf6_asbr_redistribute_unset(ospf6, red, type);
				ospf6_asbr_routemap_set(red, mapname);
				ospf6_asbr_redistribute_set(ospf6, type);
			}
		}
	}
}

static void ospf6_asbr_routemap_event(const char *name)
{
	int type;
	struct listnode *node, *nnode;
	struct ospf6 *ospf6;
	struct ospf6_redist *red;

	if (om6 == NULL)
		return;
	for (ALL_LIST_ELEMENTS(om6->ospf6, node, nnode, ospf6)) {
		for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
			red = ospf6_redist_lookup(ospf6, type, 0);
			if (red && ROUTEMAP_NAME(red)
			    && (strcmp(ROUTEMAP_NAME(red), name) == 0))
				ospf6_asbr_distribute_list_update(ospf6, red);
		}
	}
}

int ospf6_asbr_is_asbr(struct ospf6 *o)
{
	return (o->external_table->count || IS_OSPF6_ASBR(o));
}

struct ospf6_redist *ospf6_redist_lookup(struct ospf6 *ospf6, int type,
					 unsigned short instance)
{
	struct list *red_list;
	struct listnode *node;
	struct ospf6_redist *red;

	red_list = ospf6->redist[type];
	if (!red_list)
		return (NULL);

	for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
		if (red->instance == instance)
			return red;

	return NULL;
}

static struct ospf6_redist *ospf6_redist_add(struct ospf6 *ospf6, int type,
					     uint8_t instance)
{
	struct ospf6_redist *red;

	red = ospf6_redist_lookup(ospf6, type, instance);
	if (red)
		return red;

	if (!ospf6->redist[type])
		ospf6->redist[type] = list_new();

	red = XCALLOC(MTYPE_OSPF6_REDISTRIBUTE, sizeof(struct ospf6_redist));
	red->instance = instance;
	red->dmetric.type = -1;
	red->dmetric.value = -1;
	ROUTEMAP_NAME(red) = NULL;
	ROUTEMAP(red) = NULL;

	listnode_add(ospf6->redist[type], red);
	ospf6->redistribute++;

	return red;
}

static void ospf6_redist_del(struct ospf6 *ospf6, struct ospf6_redist *red,
			     int type)
{
	if (red) {
		listnode_delete(ospf6->redist[type], red);
		if (!ospf6->redist[type]->count) {
			list_delete(&ospf6->redist[type]);
		}
		XFREE(MTYPE_OSPF6_REDISTRIBUTE, red);
		ospf6->redistribute--;
	}
}

/*Set the status of the ospf instance to ASBR based on the status parameter,
 * rechedule SPF calculation, originate router LSA*/
void ospf6_asbr_status_update(struct ospf6 *ospf6, int status)
{
	struct listnode *lnode, *lnnode;
	struct ospf6_area *oa;

	zlog_info("ASBR[%s:Status:%d]: Update", ospf6->name, status);

	if (status) {
		if (IS_OSPF6_ASBR(ospf6)) {
			zlog_info("ASBR[%s:Status:%d]: Already ASBR",
				  ospf6->name, status);
			return;
		}
		SET_FLAG(ospf6->flag, OSPF6_FLAG_ASBR);
	} else {
		if (!IS_OSPF6_ASBR(ospf6)) {
			zlog_info("ASBR[%s:Status:%d]: Already non ASBR",
				  ospf6->name, status);
			return;
		}
		UNSET_FLAG(ospf6->flag, OSPF6_FLAG_ASBR);
	}

	/* Transition from/to status ASBR, schedule timer. */
	ospf6_spf_schedule(ospf6, OSPF6_SPF_FLAGS_ASBR_STATUS_CHANGE);

	/* Reoriginate router LSA for all areas */
	for (ALL_LIST_ELEMENTS(ospf6->area_list, lnode, lnnode, oa))
		OSPF6_ROUTER_LSA_SCHEDULE(oa);
}

static void ospf6_asbr_redistribute_set(struct ospf6 *ospf6, int type)
{
	ospf6_zebra_redistribute(type, ospf6->vrf_id);

	++ospf6->redist_count;
	ospf6_asbr_status_update(ospf6, ospf6->redist_count);
}

static void ospf6_asbr_redistribute_unset(struct ospf6 *ospf6,
					  struct ospf6_redist *red, int type)
{
	struct ospf6_route *route;
	struct ospf6_external_info *info;

	ospf6_zebra_no_redistribute(type, ospf6->vrf_id);

	for (route = ospf6_route_head(ospf6->external_table); route;
	     route = ospf6_route_next(route)) {
		info = route->route_option;
		if (info->type != type)
			continue;

		ospf6_asbr_redistribute_remove(info->type, 0, &route->prefix,
					       ospf6);
	}

	ospf6_asbr_routemap_unset(red);
	--ospf6->redist_count;
	ospf6_asbr_status_update(ospf6, ospf6->redist_count);
}

/* When an area is unstubified, flood all the external LSAs in the area */
void ospf6_asbr_send_externals_to_area(struct ospf6_area *oa)
{
	struct ospf6_lsa *lsa, *lsanext;

	for (ALL_LSDB(oa->ospf6->lsdb, lsa, lsanext)) {
		if (ntohs(lsa->header->type) == OSPF6_LSTYPE_AS_EXTERNAL) {
			if (IS_OSPF6_DEBUG_ASBR)
				zlog_debug("%s: Flooding AS-External LSA %s",
					   __func__, lsa->name);

			ospf6_flood_area(NULL, lsa, oa);
		}
	}
}

/* When an area is stubified, remove all the external LSAs in the area */
void ospf6_asbr_remove_externals_from_area(struct ospf6_area *oa)
{
	struct ospf6_lsa *lsa, *lsanext;
	struct listnode *node, *nnode;
	struct ospf6_area *area;
	struct ospf6 *ospf6 = oa->ospf6;
	const struct route_node *iterend;

	/* skip if router is in other non-stub/non-NSSA areas */
	for (ALL_LIST_ELEMENTS(ospf6->area_list, node, nnode, area))
		if (!IS_AREA_STUB(area) && !IS_AREA_NSSA(area))
			return;

	/* if router is only in a stub area then purge AS-External LSAs */
	iterend = ospf6_lsdb_head(ospf6->lsdb, 0, 0, 0, &lsa);
	while (lsa != NULL) {
		assert(lsa->lock > 1);
		lsanext = ospf6_lsdb_next(iterend, lsa);
		if (ntohs(lsa->header->type) == OSPF6_LSTYPE_AS_EXTERNAL)
			ospf6_lsdb_remove(lsa, ospf6->lsdb);
		lsa = lsanext;
	}
}

static struct ospf6_external_aggr_rt *
ospf6_external_aggr_match(struct ospf6 *ospf6, struct prefix *p)
{
	struct route_node *node;

	node = route_node_match(ospf6->rt_aggr_tbl, p);
	if (node == NULL)
		return NULL;

	if (IS_OSPF6_DEBUG_AGGR) {
		struct ospf6_external_aggr_rt *ag = node->info;
		zlog_debug("%s: Matching aggregator found.prefix: %pFX Aggregator %pFX",
			__func__,
			p,
			&ag->p);
	}

	route_unlock_node(node);

	return node->info;
}

static void ospf6_external_lsa_fwd_addr_set(struct ospf6 *ospf6,
					    const struct in6_addr *nexthop,
					    struct in6_addr *fwd_addr)
{
	struct vrf *vrf;
	struct interface *ifp;
	struct prefix nh;

	/* Initialize forwarding address to zero. */
	memset(fwd_addr, 0, sizeof(*fwd_addr));

	vrf = vrf_lookup_by_id(ospf6->vrf_id);
	if (!vrf)
		return;

	nh.family = AF_INET6;
	nh.u.prefix6 = *nexthop;
	nh.prefixlen = IPV6_MAX_BITLEN;

	/*
	 * Use the route's nexthop as the forwarding address if it meets the
	 * following conditions:
	 * - It's a global address.
	 * - The associated nexthop interface is OSPF-enabled.
	 */
	if (IN6_IS_ADDR_UNSPECIFIED(nexthop) || IN6_IS_ADDR_LINKLOCAL(nexthop))
		return;

	FOR_ALL_INTERFACES (vrf, ifp) {
		struct ospf6_interface *oi = ifp->info;
		struct connected *connected;

		if (!oi || CHECK_FLAG(oi->flag, OSPF6_INTERFACE_DISABLE))
			continue;

		frr_each (if_connected, ifp->connected, connected) {
			if (connected->address->family != AF_INET6)
				continue;
			if (IN6_IS_ADDR_LINKLOCAL(&connected->address->u.prefix6))
				continue;
			if (!prefix_match(connected->address, &nh))
				continue;

			*fwd_addr = *nexthop;
			return;
		}
	}
}

void ospf6_asbr_redistribute_add(int type, ifindex_t ifindex,
				 struct prefix *prefix,
				 unsigned int nexthop_num,
				 const struct in6_addr *nexthop,
				 route_tag_t tag, struct ospf6 *ospf6)
{
	route_map_result_t ret;
	struct ospf6_route troute;
	struct ospf6_external_info tinfo;
	struct ospf6_route *route, *match;
	struct ospf6_external_info *info;
	struct ospf6_redist *red;

	red = ospf6_redist_lookup(ospf6, type, 0);

	if (!red)
		return;

	if ((type != DEFAULT_ROUTE)
	    && !ospf6_zebra_is_redistribute(type, ospf6->vrf_id))
		return;

	memset(&troute, 0, sizeof(troute));
	memset(&tinfo, 0, sizeof(tinfo));

	if (IS_OSPF6_DEBUG_ASBR)
		zlog_debug("Redistribute %pFX (%s)", prefix,
			   type == DEFAULT_ROUTE
				   ? "default-information-originate"
				   : ZROUTE_NAME(type));

	/* if route-map was specified but not found, do not advertise */
	if (ROUTEMAP_NAME(red)) {
		if (ROUTEMAP(red) == NULL)
			ospf6_asbr_routemap_update(NULL);
		if (ROUTEMAP(red) == NULL) {
			zlog_warn(
				"route-map \"%s\" not found, suppress redistributing",
				ROUTEMAP_NAME(red));
			return;
		}
	}

	/* apply route-map */
	if (ROUTEMAP(red)) {
		troute.route_option = &tinfo;
		troute.ospf6 = ospf6;
		tinfo.ifindex = ifindex;
		tinfo.tag = tag;

		ret = route_map_apply(ROUTEMAP(red), prefix, &troute);
		if (ret == RMAP_DENYMATCH) {
			if (IS_OSPF6_DEBUG_ASBR)
				zlog_debug("Denied by route-map \"%s\"",
					   ROUTEMAP_NAME(red));
			ospf6_asbr_redistribute_remove(type, ifindex, prefix,
						       ospf6);
			return;
		}
	}

	match = ospf6_route_lookup(prefix, ospf6->external_table);
	if (match) {
		info = match->route_option;
		/* copy result of route-map */
		if (ROUTEMAP(red)) {
			if (troute.path.metric_type)
				match->path.metric_type =
					troute.path.metric_type;
			else
				match->path.metric_type =
					metric_type(ospf6, type, 0);
			if (troute.path.cost)
				match->path.cost = troute.path.cost;
			else
				match->path.cost = metric_value(ospf6, type, 0);

			if (!IN6_IS_ADDR_UNSPECIFIED(&tinfo.forwarding))
				memcpy(&info->forwarding, &tinfo.forwarding,
				       sizeof(struct in6_addr));
			info->tag = tinfo.tag;
		} else {
			/* If there is no route-map, simply update the tag and
			 * metric fields
			 */
			match->path.metric_type = metric_type(ospf6, type, 0);
			match->path.cost = metric_value(ospf6, type, 0);
			info->tag = tag;
		}

		info->type = type;

		if (nexthop_num && nexthop) {
			ospf6_route_add_nexthop(match, ifindex, nexthop);
			ospf6_external_lsa_fwd_addr_set(ospf6, nexthop,
							&info->forwarding);
		} else
			ospf6_route_add_nexthop(match, ifindex, NULL);

		match->path.origin.id = htonl(info->id);
		ospf6_handle_external_lsa_origination(ospf6, match, prefix);

		ospf6_asbr_status_update(ospf6, ospf6->redistribute);

		return;
	}

	/* create new entry */
	route = ospf6_route_create(ospf6);
	route->type = OSPF6_DEST_TYPE_NETWORK;
	prefix_copy(&route->prefix, prefix);

	info = (struct ospf6_external_info *)XCALLOC(
		MTYPE_OSPF6_EXTERNAL_INFO, sizeof(struct ospf6_external_info));
	route->route_option = info;

	/* copy result of route-map */
	if (ROUTEMAP(red)) {
		if (troute.path.metric_type)
			route->path.metric_type = troute.path.metric_type;
		else
			route->path.metric_type = metric_type(ospf6, type, 0);
		if (troute.path.cost)
			route->path.cost = troute.path.cost;
		else
			route->path.cost = metric_value(ospf6, type, 0);
		if (!IN6_IS_ADDR_UNSPECIFIED(&tinfo.forwarding))
			memcpy(&info->forwarding, &tinfo.forwarding,
			       sizeof(struct in6_addr));
		info->tag = tinfo.tag;
	} else {
		/* If there is no route-map, simply update the tag and metric
		 * fields
		 */
		route->path.metric_type = metric_type(ospf6, type, 0);
		route->path.cost = metric_value(ospf6, type, 0);
		info->tag = tag;
	}

	info->type = type;
	if (nexthop_num && nexthop) {
		ospf6_route_add_nexthop(route, ifindex, nexthop);
		ospf6_external_lsa_fwd_addr_set(ospf6, nexthop,
						&info->forwarding);
	} else
		ospf6_route_add_nexthop(route, ifindex, NULL);

	route = ospf6_route_add(route, ospf6->external_table);
	ospf6_handle_external_lsa_origination(ospf6, route, prefix);

	ospf6_asbr_status_update(ospf6, ospf6->redistribute);

}

static void ospf6_asbr_external_lsa_remove_by_id(struct ospf6 *ospf6,
					 uint32_t id)
{
	struct ospf6_lsa *lsa;

	lsa = ospf6_lsdb_lookup(htons(OSPF6_LSTYPE_AS_EXTERNAL),
				htonl(id), ospf6->router_id, ospf6->lsdb);
	if (!lsa)
		return;

	ospf6_external_lsa_purge(ospf6, lsa);

}

static void
ospf6_link_route_to_aggr(struct ospf6_external_aggr_rt *aggr,
			struct ospf6_route *rt)
{
	(void)hash_get(aggr->match_extnl_hash, rt, hash_alloc_intern);
	rt->aggr_route = aggr;
}

static void
ospf6_asbr_summary_remove_lsa_and_route(struct ospf6 *ospf6,
					struct ospf6_external_aggr_rt *aggr)
{

	/* Send a Max age LSA if it is already originated.*/
	if (!CHECK_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_ORIGINATED))
		return;

	if (IS_OSPF6_DEBUG_AGGR)
		zlog_debug("%s: Flushing Aggregate route (%pFX)",
				__func__,
				&aggr->p);

	ospf6_asbr_external_lsa_remove_by_id(ospf6, aggr->id);

	if (aggr->route) {
		if (IS_OSPF6_DEBUG_AGGR)
			zlog_debug(
				"%s: Remove the blackhole route",
				__func__);

		ospf6_zebra_route_update_remove(aggr->route, ospf6);
		if (aggr->route->route_option)
			XFREE(MTYPE_OSPF6_EXTERNAL_INFO,
			      aggr->route->route_option);
		ospf6_route_delete(aggr->route);
		aggr->route = NULL;
	}

	aggr->id = 0;
	/* Unset the Origination flag */
	UNSET_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_ORIGINATED);
}

static void
ospf6_unlink_route_from_aggr(struct ospf6 *ospf6,
			     struct ospf6_external_aggr_rt *aggr,
			     struct ospf6_route *rt)
{
	if (IS_OSPF6_DEBUG_AGGR)
		zlog_debug("%s: Unlinking external route(%pFX) from aggregator(%pFX), external route count:%ld",
					__func__,
					&rt->prefix,
					&aggr->p,
					OSPF6_EXTERNAL_RT_COUNT(aggr));

	hash_release(aggr->match_extnl_hash, rt);
	rt->aggr_route = NULL;

	/* Flush the aggregate route if matching
	 * external route count becomes zero.
	 */
	if (!OSPF6_EXTERNAL_RT_COUNT(aggr))
		ospf6_asbr_summary_remove_lsa_and_route(ospf6, aggr);
}

void ospf6_asbr_redistribute_remove(int type, ifindex_t ifindex,
				    struct prefix *prefix, struct ospf6 *ospf6)
{
	struct ospf6_route *match;
	struct ospf6_external_info *info = NULL;

	match = ospf6_route_lookup(prefix, ospf6->external_table);
	if (match == NULL) {
		if (IS_OSPF6_DEBUG_ASBR)
			zlog_debug("No such route %pFX to withdraw", prefix);
		return;
	}

	info = match->route_option;
	assert(info);

	if (info->type != type) {
		if (IS_OSPF6_DEBUG_ASBR)
			zlog_debug("Original protocol mismatch: %pFX", prefix);
		return;
	}

	/* This means aggregation on this route was not done, hence remove LSA
	 * if any originated for this prefix
	 */
	if (!match->aggr_route)
		ospf6_asbr_external_lsa_remove_by_id(ospf6, info->id);
	else
		ospf6_unlink_route_from_aggr(ospf6, match->aggr_route, match);

	if (IS_OSPF6_DEBUG_ASBR)
		zlog_debug("Removing route from external table %pFX",
			   prefix);

	ospf6_route_remove(match, ospf6->external_table);
	XFREE(MTYPE_OSPF6_EXTERNAL_INFO, info);

	ospf6_asbr_status_update(ospf6, ospf6->redistribute);
}

DEFPY (ospf6_redistribute,
       ospf6_redistribute_cmd,
       "redistribute " FRR_REDIST_STR_OSPF6D "[{metric (0-16777214)|metric-type (1-2)$metric_type|route-map RMAP_NAME$rmap_str}]",
       "Redistribute\n"
       FRR_REDIST_HELP_STR_OSPF6D
       "Metric for redistributed routes\n"
       "OSPF default metric\n"
       "OSPF exterior metric type for redistributed routes\n"
       "Set OSPF External Type 1/2 metrics\n"
       "Route map reference\n"
       "Route map name\n")
{
	int type;
	struct ospf6_redist *red;
	int idx_protocol = 1;
	char *proto = argv[idx_protocol]->text;

	VTY_DECLVAR_CONTEXT(ospf6, ospf6);

	type = proto_redistnum(AFI_IP6, proto);
	if (type < 0)
		return CMD_WARNING_CONFIG_FAILED;

	if (!metric_str)
		metric = -1;
	if (!metric_type_str)
		metric_type = -1;

	red = ospf6_redist_lookup(ospf6, type, 0);
	if (!red) {
		red = ospf6_redist_add(ospf6, type, 0);
	} else {
		/* Check if nothing has changed. */
		if (red->dmetric.value == metric
		    && red->dmetric.type == metric_type
		    && ((!ROUTEMAP_NAME(red) && !rmap_str)
			|| (ROUTEMAP_NAME(red) && rmap_str
			    && strmatch(ROUTEMAP_NAME(red), rmap_str))))
			return CMD_SUCCESS;

		ospf6_asbr_redistribute_unset(ospf6, red, type);
	}

	red->dmetric.value = metric;
	red->dmetric.type = metric_type;
	if (rmap_str)
		ospf6_asbr_routemap_set(red, rmap_str);
	else
		ospf6_asbr_routemap_unset(red);
	ospf6_asbr_redistribute_set(ospf6, type);

	return CMD_SUCCESS;
}

DEFUN (no_ospf6_redistribute,
       no_ospf6_redistribute_cmd,
       "no redistribute " FRR_REDIST_STR_OSPF6D "[{metric (0-16777214)|metric-type (1-2)|route-map RMAP_NAME}]",
       NO_STR
       "Redistribute\n"
       FRR_REDIST_HELP_STR_OSPF6D
       "Metric for redistributed routes\n"
       "OSPF default metric\n"
       "OSPF exterior metric type for redistributed routes\n"
       "Set OSPF External Type 1/2 metrics\n"
       "Route map reference\n"
       "Route map name\n")
{
	int type;
	struct ospf6_redist *red;
	int idx_protocol = 2;
	char *proto = argv[idx_protocol]->text;

	VTY_DECLVAR_CONTEXT(ospf6, ospf6);

	type = proto_redistnum(AFI_IP6, proto);
	if (type < 0)
		return CMD_WARNING_CONFIG_FAILED;

	red = ospf6_redist_lookup(ospf6, type, 0);
	if (!red)
		return CMD_SUCCESS;

	ospf6_asbr_redistribute_unset(ospf6, red, type);
	ospf6_redist_del(ospf6, red, type);

	return CMD_SUCCESS;
}

int ospf6_redistribute_config_write(struct vty *vty, struct ospf6 *ospf6)
{
	int type;
	struct ospf6_redist *red;

	for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
		red = ospf6_redist_lookup(ospf6, type, 0);
		if (!red)
			continue;
		if (type == ZEBRA_ROUTE_OSPF6)
			continue;

		vty_out(vty, " redistribute %s", ZROUTE_NAME(type));
		if (red->dmetric.value >= 0)
			vty_out(vty, " metric %d", red->dmetric.value);
		if (red->dmetric.type == 1)
			vty_out(vty, " metric-type 1");
		if (ROUTEMAP_NAME(red))
			vty_out(vty, " route-map %s", ROUTEMAP_NAME(red));
		vty_out(vty, "\n");
	}

	return 0;
}

static void ospf6_redistribute_show_config(struct vty *vty, struct ospf6 *ospf6,
					   json_object *json_array,
					   json_object *json, bool use_json)
{
	int type;
	int nroute[ZEBRA_ROUTE_MAX];
	int total;
	struct ospf6_route *route;
	struct ospf6_external_info *info;
	json_object *json_route;
	struct ospf6_redist *red;

	total = 0;
	memset(nroute, 0, sizeof(nroute));
	for (route = ospf6_route_head(ospf6->external_table); route;
	     route = ospf6_route_next(route)) {
		info = route->route_option;
		nroute[info->type]++;
		total++;
	}

	if (!use_json)
		vty_out(vty, "Redistributing External Routes from:\n");

	for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {

		red = ospf6_redist_lookup(ospf6, type, 0);

		if (!red)
			continue;
		if (type == ZEBRA_ROUTE_OSPF6)
			continue;

		if (use_json) {
			json_route = json_object_new_object();
			json_object_string_add(json_route, "routeType",
					       ZROUTE_NAME(type));
			json_object_int_add(json_route, "numberOfRoutes",
					    nroute[type]);
			json_object_boolean_add(json_route,
						"routeMapNamePresent",
						ROUTEMAP_NAME(red));
		}

		if (ROUTEMAP_NAME(red)) {
			if (use_json) {
				json_object_string_add(json_route,
						       "routeMapName",
						       ROUTEMAP_NAME(red));
				json_object_boolean_add(json_route,
							"routeMapFound",
							ROUTEMAP(red));
			} else
				vty_out(vty,
					"    %d: %s with route-map \"%s\"%s\n",
					nroute[type], ZROUTE_NAME(type),
					ROUTEMAP_NAME(red),
					(ROUTEMAP(red) ? ""
						       : " (not found !)"));
		} else {
			if (!use_json)
				vty_out(vty, "    %d: %s\n", nroute[type],
					ZROUTE_NAME(type));
		}

		if (use_json)
			json_object_array_add(json_array, json_route);
	}
	if (use_json) {
		json_object_object_add(json, "redistributedRoutes", json_array);
		json_object_int_add(json, "totalRoutes", total);
	} else
		vty_out(vty, "Total %d routes\n", total);
}

static void ospf6_redistribute_default_set(struct ospf6 *ospf6, int originate)
{
	struct prefix_ipv6 p = {};
	struct in6_addr nexthop = {};
	int cur_originate = ospf6->default_originate;

	p.family = AF_INET6;
	p.prefixlen = 0;

	ospf6->default_originate = originate;

	switch (cur_originate) {
	case DEFAULT_ORIGINATE_NONE:
		break;
	case DEFAULT_ORIGINATE_ZEBRA:
		zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE,
					     zclient, AFI_IP6, ospf6->vrf_id);
		ospf6_asbr_redistribute_remove(DEFAULT_ROUTE, 0,
					       (struct prefix *)&p, ospf6);

		break;
	case DEFAULT_ORIGINATE_ALWAYS:
		ospf6_asbr_redistribute_remove(DEFAULT_ROUTE, 0,
					       (struct prefix *)&p, ospf6);
		break;
	}

	switch (originate) {
	case DEFAULT_ORIGINATE_NONE:
		break;
	case DEFAULT_ORIGINATE_ZEBRA:
		zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD,
					     zclient, AFI_IP6, ospf6->vrf_id);

		break;
	case DEFAULT_ORIGINATE_ALWAYS:
		ospf6_asbr_redistribute_add(DEFAULT_ROUTE, 0,
					    (struct prefix *)&p, 0, &nexthop, 0,
					    ospf6);
		break;
	}
}

/* Default Route originate. */
DEFPY (ospf6_default_route_originate,
       ospf6_default_route_originate_cmd,
       "default-information originate [{always$always|metric (0-16777214)$mval|metric-type (1-2)$mtype|route-map RMAP_NAME$rtmap}]",
       "Control distribution of default route\n"
       "Distribute a default route\n"
       "Always advertise default route\n"
       "OSPFv3 default metric\n"
       "OSPFv3 metric\n"
       "OSPFv3 metric type for default routes\n"
       "Set OSPFv3 External Type 1/2 metrics\n"
       "Route map reference\n"
       "Pointer to route-map entries\n")
{
	int default_originate = DEFAULT_ORIGINATE_ZEBRA;
	struct ospf6_redist *red;
	bool sameRtmap = false;

	VTY_DECLVAR_CONTEXT(ospf6, ospf6);

	int cur_originate = ospf6->default_originate;

	red = ospf6_redist_add(ospf6, DEFAULT_ROUTE, 0);

	if (always != NULL)
		default_originate = DEFAULT_ORIGINATE_ALWAYS;

	if (mval_str == NULL)
		mval = -1;

	if (mtype_str == NULL)
		mtype = -1;

	/* To check if user is providing same route map */
	if ((!rtmap && !ROUTEMAP_NAME(red)) ||
	    (rtmap && ROUTEMAP_NAME(red) &&
	     (strcmp(rtmap, ROUTEMAP_NAME(red)) == 0)))
		sameRtmap = true;

	/* Don't allow if the same lsa is already originated. */
	if ((sameRtmap) && (red->dmetric.type == mtype)
	    && (red->dmetric.value == mval)
	    && (cur_originate == default_originate))
		return CMD_SUCCESS;

	/* Updating Metric details */
	red->dmetric.type = mtype;
	red->dmetric.value = mval;

	/* updating route map details */
	if (rtmap)
		ospf6_asbr_routemap_set(red, rtmap);
	else
		ospf6_asbr_routemap_unset(red);

	ospf6_redistribute_default_set(ospf6, default_originate);
	return CMD_SUCCESS;
}

DEFPY (no_ospf6_default_information_originate,
       no_ospf6_default_information_originate_cmd,
       "no default-information originate [{always|metric (0-16777214)|metric-type (1-2)|route-map RMAP_NAME}]",
       NO_STR
       "Control distribution of default information\n"
       "Distribute a default route\n"
       "Always advertise default route\n"
       "OSPFv3 default metric\n"
       "OSPFv3 metric\n"
       "OSPFv3 metric type for default routes\n"
       "Set OSPFv3 External Type 1/2 metrics\n"
       "Route map reference\n"
       "Pointer to route-map entries\n")
{
	struct ospf6_redist *red;

	VTY_DECLVAR_CONTEXT(ospf6, ospf6);

	red = ospf6_redist_lookup(ospf6, DEFAULT_ROUTE, 0);
	if (!red)
		return CMD_SUCCESS;

	ospf6_asbr_routemap_unset(red);
	ospf6_redist_del(ospf6, red, DEFAULT_ROUTE);

	ospf6_redistribute_default_set(ospf6, DEFAULT_ORIGINATE_NONE);
	return CMD_SUCCESS;
}

/* Routemap Functions */
static enum route_map_cmd_result_t
ospf6_routemap_rule_match_address_prefixlist(void *rule,
					     const struct prefix *prefix,

					     void *object)
{
	struct prefix_list *plist;

	plist = prefix_list_lookup(AFI_IP6, (char *)rule);
	if (plist == NULL)
		return RMAP_NOMATCH;

	return (prefix_list_apply(plist, prefix) == PREFIX_DENY ? RMAP_NOMATCH
								: RMAP_MATCH);
}

static void *
ospf6_routemap_rule_match_address_prefixlist_compile(const char *arg)
{
	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
}

static void ospf6_routemap_rule_match_address_prefixlist_free(void *rule)
{
	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
}

static const struct route_map_rule_cmd
		ospf6_routemap_rule_match_address_prefixlist_cmd = {
	"ipv6 address prefix-list",
	ospf6_routemap_rule_match_address_prefixlist,
	ospf6_routemap_rule_match_address_prefixlist_compile,
	ospf6_routemap_rule_match_address_prefixlist_free,
};

/* `match interface IFNAME' */
/* Match function should return 1 if match is success else return
   zero. */
static enum route_map_cmd_result_t
ospf6_routemap_rule_match_interface(void *rule, const struct prefix *prefix,
				    void *object)
{
	struct interface *ifp;
	struct ospf6_route *route;
	struct ospf6_external_info *ei;

	route = object;
	ei = route->route_option;
	ifp = if_lookup_by_name((char *)rule, route->ospf6->vrf_id);

	if (ifp != NULL && ei->ifindex == ifp->ifindex)
		return RMAP_MATCH;

	return RMAP_NOMATCH;
}

/* Route map `interface' match statement.  `arg' should be
   interface name. */
static void *ospf6_routemap_rule_match_interface_compile(const char *arg)
{
	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
}

/* Free route map's compiled `interface' value. */
static void ospf6_routemap_rule_match_interface_free(void *rule)
{
	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
}

/* Route map commands for interface matching. */
static const struct route_map_rule_cmd
		ospf6_routemap_rule_match_interface_cmd = {
	"interface",
	ospf6_routemap_rule_match_interface,
	ospf6_routemap_rule_match_interface_compile,
	ospf6_routemap_rule_match_interface_free
};

/* Match function for matching route tags */
static enum route_map_cmd_result_t
ospf6_routemap_rule_match_tag(void *rule, const struct prefix *p, void *object)
{
	route_tag_t *tag = rule;
	struct ospf6_route *route = object;
	struct ospf6_external_info *info = route->route_option;

	if (info->tag == *tag)
		return RMAP_MATCH;

	return RMAP_NOMATCH;
}

static const struct route_map_rule_cmd
		ospf6_routemap_rule_match_tag_cmd = {
	"tag",
	ospf6_routemap_rule_match_tag,
	route_map_rule_tag_compile,
	route_map_rule_tag_free,
};

static enum route_map_cmd_result_t
ospf6_routemap_rule_set_metric_type(void *rule, const struct prefix *prefix,
				    void *object)
{
	char *metric_type = rule;
	struct ospf6_route *route = object;

	if (strcmp(metric_type, "type-2") == 0)
		route->path.metric_type = 2;
	else
		route->path.metric_type = 1;

	return RMAP_OKAY;
}

static void *ospf6_routemap_rule_set_metric_type_compile(const char *arg)
{
	if (strcmp(arg, "type-2") && strcmp(arg, "type-1"))
		return NULL;
	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
}

static void ospf6_routemap_rule_set_metric_type_free(void *rule)
{
	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
}

static const struct route_map_rule_cmd
		ospf6_routemap_rule_set_metric_type_cmd = {
	"metric-type",
	ospf6_routemap_rule_set_metric_type,
	ospf6_routemap_rule_set_metric_type_compile,
	ospf6_routemap_rule_set_metric_type_free,
};

static enum route_map_cmd_result_t
ospf6_routemap_rule_set_metric(void *rule, const struct prefix *prefix,
			       void *object)
{
	char *metric = rule;
	struct ospf6_route *route = object;

	route->path.cost = atoi(metric);
	return RMAP_OKAY;
}

static void *ospf6_routemap_rule_set_metric_compile(const char *arg)
{
	uint32_t metric;
	char *endp;
	metric = strtoul(arg, &endp, 0);
	if (metric > OSPF_LS_INFINITY || *endp != '\0')
		return NULL;
	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
}

static void ospf6_routemap_rule_set_metric_free(void *rule)
{
	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
}

static const struct route_map_rule_cmd
		ospf6_routemap_rule_set_metric_cmd = {
	"metric",
	ospf6_routemap_rule_set_metric,
	ospf6_routemap_rule_set_metric_compile,
	ospf6_routemap_rule_set_metric_free,
};

static enum route_map_cmd_result_t
ospf6_routemap_rule_set_forwarding(void *rule, const struct prefix *prefix,
				   void *object)
{
	char *forwarding = rule;
	struct ospf6_route *route = object;
	struct ospf6_external_info *info = route->route_option;

	if (inet_pton(AF_INET6, forwarding, &info->forwarding) != 1) {
		memset(&info->forwarding, 0, sizeof(struct in6_addr));
		return RMAP_ERROR;
	}

	return RMAP_OKAY;
}

static void *ospf6_routemap_rule_set_forwarding_compile(const char *arg)
{
	struct in6_addr a;
	if (inet_pton(AF_INET6, arg, &a) != 1)
		return NULL;
	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
}

static void ospf6_routemap_rule_set_forwarding_free(void *rule)
{
	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
}

static const struct route_map_rule_cmd
		ospf6_routemap_rule_set_forwarding_cmd = {
	"forwarding-address",
	ospf6_routemap_rule_set_forwarding,
	ospf6_routemap_rule_set_forwarding_compile,
	ospf6_routemap_rule_set_forwarding_free,
};

static enum route_map_cmd_result_t
ospf6_routemap_rule_set_tag(void *rule, const struct prefix *p, void *object)
{
	route_tag_t *tag = rule;
	struct ospf6_route *route = object;
	struct ospf6_external_info *info = route->route_option;

	info->tag = *tag;
	return RMAP_OKAY;
}

static const struct route_map_rule_cmd ospf6_routemap_rule_set_tag_cmd = {
	"tag",
	ospf6_routemap_rule_set_tag,
	route_map_rule_tag_compile,
	route_map_rule_tag_free,
};

/* add "set metric-type" */
DEFUN_YANG (ospf6_routemap_set_metric_type, ospf6_routemap_set_metric_type_cmd,
      "set metric-type <type-1|type-2>",
       SET_STR
       "Type of metric for destination routing protocol\n"
       "OSPF[6] external type 1 metric\n"
       "OSPF[6] external type 2 metric\n")
{
	char *ext = argv[2]->text;

	const char *xpath =
		"./set-action[action='frr-ospf-route-map:metric-type']";
	char xpath_value[XPATH_MAXLEN];

	nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
	snprintf(xpath_value, sizeof(xpath_value),
		 "%s/rmap-set-action/frr-ospf-route-map:metric-type", xpath);
	nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, ext);
	return nb_cli_apply_changes(vty, NULL);
}

/* delete "set metric-type" */
DEFUN_YANG (ospf6_routemap_no_set_metric_type, ospf6_routemap_no_set_metric_type_cmd,
      "no set metric-type [<type-1|type-2>]",
      NO_STR
      SET_STR
      "Type of metric for destination routing protocol\n"
      "OSPF[6] external type 1 metric\n"
      "OSPF[6] external type 2 metric\n")
{
	const char *xpath =
		"./set-action[action='frr-ospf-route-map:metric-type']";

	nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
	return nb_cli_apply_changes(vty, NULL);
}

/* add "set forwarding-address" */
DEFUN_YANG (ospf6_routemap_set_forwarding, ospf6_routemap_set_forwarding_cmd,
      "set forwarding-address X:X::X:X",
      "Set value\n"
      "Forwarding Address\n"
      "IPv6 Address\n")
{
	int idx_ipv6 = 2;
	const char *xpath =
		"./set-action[action='frr-ospf6-route-map:forwarding-address']";
	char xpath_value[XPATH_MAXLEN];

	nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
	snprintf(xpath_value, sizeof(xpath_value),
		 "%s/rmap-set-action/frr-ospf6-route-map:ipv6-address", xpath);
	nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
			      argv[idx_ipv6]->arg);
	return nb_cli_apply_changes(vty, NULL);
}

/* delete "set forwarding-address" */
DEFUN_YANG (ospf6_routemap_no_set_forwarding, ospf6_routemap_no_set_forwarding_cmd,
      "no set forwarding-address [X:X::X:X]",
      NO_STR
      "Set value\n"
      "Forwarding Address\n"
      "IPv6 Address\n")
{
	const char *xpath =
		"./set-action[action='frr-ospf6-route-map:forwarding-address']";

	nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
	return nb_cli_apply_changes(vty, NULL);
}

static void ospf6_routemap_init(void)
{
	route_map_init();

	route_map_add_hook(ospf6_asbr_routemap_update);
	route_map_delete_hook(ospf6_asbr_routemap_update);
	route_map_event_hook(ospf6_asbr_routemap_event);

	route_map_set_metric_hook(generic_set_add);
	route_map_no_set_metric_hook(generic_set_delete);

	route_map_set_tag_hook(generic_set_add);
	route_map_no_set_tag_hook(generic_set_delete);

	route_map_match_tag_hook(generic_match_add);
	route_map_no_match_tag_hook(generic_match_delete);

	route_map_match_ipv6_address_prefix_list_hook(generic_match_add);
	route_map_no_match_ipv6_address_prefix_list_hook(generic_match_delete);

	route_map_match_interface_hook(generic_match_add);
	route_map_no_match_interface_hook(generic_match_delete);

	route_map_install_match(
		&ospf6_routemap_rule_match_address_prefixlist_cmd);
	route_map_install_match(&ospf6_routemap_rule_match_interface_cmd);
	route_map_install_match(&ospf6_routemap_rule_match_tag_cmd);

	route_map_install_set(&ospf6_routemap_rule_set_metric_type_cmd);
	route_map_install_set(&ospf6_routemap_rule_set_metric_cmd);
	route_map_install_set(&ospf6_routemap_rule_set_forwarding_cmd);
	route_map_install_set(&ospf6_routemap_rule_set_tag_cmd);

	/* ASE Metric Type (e.g. Type-1/Type-2) */
	install_element(RMAP_NODE, &ospf6_routemap_set_metric_type_cmd);
	install_element(RMAP_NODE, &ospf6_routemap_no_set_metric_type_cmd);

	/* ASE Metric */
	install_element(RMAP_NODE, &ospf6_routemap_set_forwarding_cmd);
	install_element(RMAP_NODE, &ospf6_routemap_no_set_forwarding_cmd);
}


/* Display functions */
static char *ospf6_as_external_lsa_get_prefix_str(struct ospf6_lsa *lsa,
						  char *buf, int buflen,
						  int pos)
{
	struct ospf6_as_external_lsa *external;
	struct in6_addr in6;
	int prefix_length = 0;
	char tbuf[16];

	if (lsa) {
		external = (struct ospf6_as_external_lsa *)OSPF6_LSA_HEADER_END(
			lsa->header);

		if (pos == 0) {
			ospf6_prefix_in6_addr(&in6, external,
					      &external->prefix);
			prefix_length = external->prefix.prefix_length;
		} else {
			in6 = *((struct in6_addr
					 *)((caddr_t)external
					    + sizeof(struct
						     ospf6_as_external_lsa)
					    + OSPF6_PREFIX_SPACE(
						      external->prefix
							      .prefix_length)));
		}
		if (buf) {
			inet_ntop(AF_INET6, &in6, buf, buflen);
			if (prefix_length) {
				snprintf(tbuf, sizeof(tbuf), "/%d",
					 prefix_length);
				strlcat(buf, tbuf, buflen);
			}
		}
	}
	return (buf);
}

static int ospf6_as_external_lsa_show(struct vty *vty, struct ospf6_lsa *lsa,
				      json_object *json_obj, bool use_json)
{
	struct ospf6_as_external_lsa *external;
	char buf[64];

	assert(lsa->header);
	external = (struct ospf6_as_external_lsa *)OSPF6_LSA_HEADER_END(
		lsa->header);

	/* bits */
	snprintf(buf, sizeof(buf), "%c%c%c",
		 (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_E) ? 'E'
								      : '-'),
		 (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_F) ? 'F'
								      : '-'),
		 (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_T) ? 'T'
								      : '-'));

	if (use_json) {
		json_object_string_add(json_obj, "bits", buf);
		json_object_int_add(json_obj, "metric",
				    (unsigned long)OSPF6_ASBR_METRIC(external));
		ospf6_prefix_options_printbuf(external->prefix.prefix_options,
					      buf, sizeof(buf));
		json_object_string_add(json_obj, "prefixOptions", buf);
		json_object_int_add(
			json_obj, "referenceLsType",
			ntohs(external->prefix.prefix_refer_lstype));
		json_object_string_add(json_obj, "prefix",
				       ospf6_as_external_lsa_get_prefix_str(
					       lsa, buf, sizeof(buf), 0));

		/* Forwarding-Address */
		json_object_boolean_add(
			json_obj, "forwardingAddressPresent",
			CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_F));
		if (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_F))
			json_object_string_add(
				json_obj, "forwardingAddress",
				ospf6_as_external_lsa_get_prefix_str(
					lsa, buf, sizeof(buf), 1));

		/* Tag */
		json_object_boolean_add(
			json_obj, "tagPresent",
			CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_T));
		if (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_T))
			json_object_int_add(json_obj, "tag",
					    ospf6_as_external_lsa_get_tag(lsa));
	} else {
		vty_out(vty, "     Bits: %s\n", buf);
		vty_out(vty, "     Metric: %5lu\n",
			(unsigned long)OSPF6_ASBR_METRIC(external));

		ospf6_prefix_options_printbuf(external->prefix.prefix_options,
					      buf, sizeof(buf));
		vty_out(vty, "     Prefix Options: %s\n", buf);

		vty_out(vty, "     Referenced LSType: %d\n",
			ntohs(external->prefix.prefix_refer_lstype));

		vty_out(vty, "     Prefix: %s\n",
			ospf6_as_external_lsa_get_prefix_str(lsa, buf,
							     sizeof(buf), 0));

		/* Forwarding-Address */
		if (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_F)) {
			vty_out(vty, "     Forwarding-Address: %s\n",
				ospf6_as_external_lsa_get_prefix_str(
					lsa, buf, sizeof(buf), 1));
		}

		/* Tag */
		if (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_T)) {
			vty_out(vty, "     Tag: %" ROUTE_TAG_PRI "\n",
				ospf6_as_external_lsa_get_tag(lsa));
		}
	}

	return 0;
}

static void ospf6_asbr_external_route_show(struct vty *vty,
					   struct ospf6_route *route,
					   json_object *json_array,
					   bool use_json)
{
	struct ospf6_external_info *info = route->route_option;
	char prefix[PREFIX2STR_BUFFER], id[16], forwarding[64];
	uint32_t tmp_id;
	json_object *json_route;
	char route_type[2];

	prefix2str(&route->prefix, prefix, sizeof(prefix));
	tmp_id = ntohl(info->id);
	inet_ntop(AF_INET, &tmp_id, id, sizeof(id));
	if (!IN6_IS_ADDR_UNSPECIFIED(&info->forwarding))
		inet_ntop(AF_INET6, &info->forwarding, forwarding,
			  sizeof(forwarding));
	else
		snprintf(forwarding, sizeof(forwarding), ":: (ifindex %d)",
			 ospf6_route_get_first_nh_index(route));

	if (use_json) {
		json_route = json_object_new_object();
		snprintf(route_type, sizeof(route_type), "%c",
			 zebra_route_char(info->type));
		json_object_string_add(json_route, "routeType", route_type);
		json_object_string_add(json_route, "destination", prefix);
		json_object_string_add(json_route, "id", id);
		json_object_int_add(json_route, "metricType",
				    route->path.metric_type);
		json_object_int_add(
			json_route, "routeCost",
			(unsigned long)(route->path.metric_type == 2
						? route->path.u.cost_e2
						: route->path.cost));
		json_object_string_add(json_route, "forwarding", forwarding);

		json_object_array_add(json_array, json_route);
	} else

		vty_out(vty, "%c %-32pFX %-15s type-%d %5lu %s\n",
			zebra_route_char(info->type), &route->prefix, id,
			route->path.metric_type,
			(unsigned long)(route->path.metric_type == 2
						? route->path.u.cost_e2
						: route->path.cost),
			forwarding);
}

DEFUN(show_ipv6_ospf6_redistribute, show_ipv6_ospf6_redistribute_cmd,
      "show ipv6 ospf6 [vrf <NAME|all>] redistribute [json]",
      SHOW_STR IP6_STR OSPF6_STR VRF_CMD_HELP_STR
      "All VRFs\n"
      "redistributing External information\n" JSON_STR)
{
	struct ospf6_route *route;
	struct ospf6 *ospf6 = NULL;
	json_object *json = NULL;
	bool uj = use_json(argc, argv);
	struct listnode *node;
	const char *vrf_name = NULL;
	bool all_vrf = false;
	int idx_vrf = 0;

	json_object *json_array_routes = NULL;
	json_object *json_array_redistribute = NULL;

	OSPF6_FIND_VRF_ARGS(argv, argc, idx_vrf, vrf_name, all_vrf);

	if (uj) {
		json = json_object_new_object();
		json_array_routes = json_object_new_array();
		json_array_redistribute = json_object_new_array();
	}

	for (ALL_LIST_ELEMENTS_RO(om6->ospf6, node, ospf6)) {
		if (all_vrf
		    || ((ospf6->name == NULL && vrf_name == NULL)
			|| (ospf6->name && vrf_name
			    && strcmp(ospf6->name, vrf_name) == 0))) {
			ospf6_redistribute_show_config(
				vty, ospf6, json_array_redistribute, json, uj);

			for (route = ospf6_route_head(ospf6->external_table);
			     route; route = ospf6_route_next(route)) {
				ospf6_asbr_external_route_show(
					vty, route, json_array_routes, uj);
			}

			if (uj) {
				json_object_object_add(json, "routes",
						       json_array_routes);
				vty_json(vty, json);
			}

			if (!all_vrf)
				break;
		}
	}

	OSPF6_CMD_CHECK_VRF(uj, all_vrf, ospf6);

	return CMD_SUCCESS;
}

static struct ospf6_lsa_handler as_external_handler = {
	.lh_type = OSPF6_LSTYPE_AS_EXTERNAL,
	.lh_name = "AS-External",
	.lh_short_name = "ASE",
	.lh_show = ospf6_as_external_lsa_show,
	.lh_get_prefix_str = ospf6_as_external_lsa_get_prefix_str,
	.lh_debug = 0};

static struct ospf6_lsa_handler nssa_external_handler = {
	.lh_type = OSPF6_LSTYPE_TYPE_7,
	.lh_name = "NSSA",
	.lh_short_name = "Type7",
	.lh_show = ospf6_as_external_lsa_show,
	.lh_get_prefix_str = ospf6_as_external_lsa_get_prefix_str,
	.lh_debug = 0};

void ospf6_asbr_init(void)
{
	ospf6_routemap_init();

	ospf6_install_lsa_handler(&as_external_handler);
	ospf6_install_lsa_handler(&nssa_external_handler);

	install_element(VIEW_NODE, &show_ipv6_ospf6_redistribute_cmd);

	install_element(OSPF6_NODE, &ospf6_default_route_originate_cmd);
	install_element(OSPF6_NODE,
			&no_ospf6_default_information_originate_cmd);
	install_element(OSPF6_NODE, &ospf6_redistribute_cmd);
	install_element(OSPF6_NODE, &no_ospf6_redistribute_cmd);
}

void ospf6_asbr_redistribute_disable(struct ospf6 *ospf6)
{
	int type;
	struct ospf6_redist *red;

	for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
		red = ospf6_redist_lookup(ospf6, type, 0);
		if (!red)
			continue;
		if (type == ZEBRA_ROUTE_OSPF6)
			continue;
		ospf6_asbr_redistribute_unset(ospf6, red, type);
		ospf6_redist_del(ospf6, red, type);
	}
	red = ospf6_redist_lookup(ospf6, DEFAULT_ROUTE, 0);
	if (red) {
		ospf6_asbr_routemap_unset(red);
		ospf6_redist_del(ospf6, red, type);
		ospf6_redistribute_default_set(ospf6, DEFAULT_ORIGINATE_NONE);
	}
}

void ospf6_asbr_redistribute_reset(struct ospf6 *ospf6)
{
	int type;
	struct ospf6_redist *red;
	char buf[RMAP_NAME_MAXLEN];

	for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
		buf[0] = '\0';
		if (type == ZEBRA_ROUTE_OSPF6)
			continue;
		red = ospf6_redist_lookup(ospf6, type, 0);
		if (!red)
			continue;

		if (type == DEFAULT_ROUTE) {
			ospf6_redistribute_default_set(
				ospf6, ospf6->default_originate);
			continue;
		}
		if (ROUTEMAP_NAME(red))
			strlcpy(buf, ROUTEMAP_NAME(red), sizeof(buf));

		ospf6_asbr_redistribute_unset(ospf6, red, type);
		if (buf[0])
			ospf6_asbr_routemap_set(red, buf);
		ospf6_asbr_redistribute_set(ospf6, type);
	}
}

void ospf6_asbr_terminate(void)
{
	/* Cleanup route maps */
	route_map_finish();
}

DEFUN (debug_ospf6_asbr,
       debug_ospf6_asbr_cmd,
       "debug ospf6 asbr",
       DEBUG_STR
       OSPF6_STR
       "Debug OSPFv3 ASBR function\n"
      )
{
	OSPF6_DEBUG_ASBR_ON();
	return CMD_SUCCESS;
}

DEFUN (no_debug_ospf6_asbr,
       no_debug_ospf6_asbr_cmd,
       "no debug ospf6 asbr",
       NO_STR
       DEBUG_STR
       OSPF6_STR
       "Debug OSPFv3 ASBR function\n"
      )
{
	OSPF6_DEBUG_ASBR_OFF();
	return CMD_SUCCESS;
}

int config_write_ospf6_debug_asbr(struct vty *vty)
{
	if (IS_OSPF6_DEBUG_ASBR)
		vty_out(vty, "debug ospf6 asbr\n");
	return 0;
}

static void ospf6_default_originate_write(struct vty *vty, struct ospf6 *o)
{
	struct ospf6_redist *red;

	vty_out(vty, " default-information originate");
	if (o->default_originate == DEFAULT_ORIGINATE_ALWAYS)
		vty_out(vty, " always");

	red = ospf6_redist_lookup(o, DEFAULT_ROUTE, 0);
	if (red == NULL) {
		vty_out(vty, "\n");
		return;
	}

	if (red->dmetric.value >= 0)
		vty_out(vty, " metric %d", red->dmetric.value);

	if (red->dmetric.type >= 0)
		vty_out(vty, " metric-type %d", red->dmetric.type);

	if (ROUTEMAP_NAME(red))
		vty_out(vty, " route-map %s", ROUTEMAP_NAME(red));

	vty_out(vty, "\n");
}

int ospf6_distribute_config_write(struct vty *vty, struct ospf6 *o)
{
	if (o == NULL)
		return 0;

	/* Print default originate configuration. */
	if (o->default_originate != DEFAULT_ORIGINATE_NONE)
		ospf6_default_originate_write(vty, o);

	return 0;
}

void install_element_ospf6_debug_asbr(void)
{
	install_element(ENABLE_NODE, &debug_ospf6_asbr_cmd);
	install_element(ENABLE_NODE, &no_debug_ospf6_asbr_cmd);
	install_element(CONFIG_NODE, &debug_ospf6_asbr_cmd);
	install_element(CONFIG_NODE, &no_debug_ospf6_asbr_cmd);
}

/* ASBR Summarisation */
void ospf6_fill_aggr_route_details(struct ospf6 *ospf6,
				   struct ospf6_external_aggr_rt *aggr)
{
	struct ospf6_route *rt_aggr = aggr->route;
	struct ospf6_external_info *ei_aggr = rt_aggr->route_option;

	rt_aggr->prefix = aggr->p;
	ei_aggr->tag = aggr->tag;
	ei_aggr->type = 0;
	ei_aggr->id = aggr->id;

	/* When metric is not configured, apply the default metric */
	rt_aggr->path.cost = ((aggr->metric == -1) ?
				DEFAULT_DEFAULT_METRIC
				: (unsigned int)(aggr->metric));
	rt_aggr->path.metric_type = aggr->mtype;

	rt_aggr->path.origin.id = htonl(aggr->id);
}

static void
ospf6_summary_add_aggr_route_and_blackhole(struct ospf6 *ospf6,
					   struct ospf6_external_aggr_rt *aggr)
{
	struct ospf6_route *rt_aggr;
	struct ospf6_route *old_rt = NULL;
	struct ospf6_external_info *info;

	/* Check if a route is already present. */
	if (aggr->route)
		old_rt = aggr->route;

	/* Create summary route and save it. */
	rt_aggr = ospf6_route_create(ospf6);
	rt_aggr->type = OSPF6_DEST_TYPE_NETWORK;
	/* Needed to install route while calling zebra api */
	SET_FLAG(rt_aggr->flag, OSPF6_ROUTE_BEST);

	info = XCALLOC(MTYPE_OSPF6_EXTERNAL_INFO, sizeof(*info));
	rt_aggr->route_option = info;
	aggr->route = rt_aggr;

	/* Prepare the external_info for aggregator
	 * Fill all the details which will get advertised
	 */
	ospf6_fill_aggr_route_details(ospf6, aggr);

	/* Add next-hop to Null interface. */
	ospf6_add_route_nexthop_blackhole(rt_aggr);

	/* Free the old route, if any. */
	if (old_rt) {
		ospf6_zebra_route_update_remove(old_rt, ospf6);

		if (old_rt->route_option)
			XFREE(MTYPE_OSPF6_EXTERNAL_INFO, old_rt->route_option);

		ospf6_route_delete(old_rt);
	}

	ospf6_zebra_route_update_add(rt_aggr, ospf6);
}

static void ospf6_originate_new_aggr_lsa(struct ospf6 *ospf6,
					 struct ospf6_external_aggr_rt *aggr)
{
	struct prefix prefix_id;
	struct ospf6_lsa *lsa = NULL;

	if (IS_OSPF6_DEBUG_AGGR)
		zlog_debug("%s: Originate new aggregate route(%pFX)", __func__,
			   &aggr->p);

	aggr->id = ospf6->external_id++;

	if (IS_OSPF6_DEBUG_AGGR)
		zlog_debug(
			"Advertise AS-External Id:%pI4 prefix %pFX metric %u",
			&prefix_id.u.prefix4, &aggr->p, aggr->metric);

	ospf6_summary_add_aggr_route_and_blackhole(ospf6, aggr);

	/* Originate summary LSA */
	lsa = ospf6_originate_type5_type7_lsas(aggr->route, ospf6);
	if (lsa) {
		if (IS_OSPF6_DEBUG_AGGR)
			zlog_debug("%s: Set the origination bit for aggregator",
					__func__);
		SET_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_ORIGINATED);
	}
}

static void
ospf6_aggr_handle_advertise_change(struct ospf6 *ospf6,
		struct ospf6_external_aggr_rt *aggr)
{
	/* Check if advertise option modified. */
	if (CHECK_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_NO_ADVERTISE)) {
		if (IS_OSPF6_DEBUG_AGGR)
			zlog_debug("%s: Don't originate the summary address,It is configured to not-advertise.",
					__func__);
		ospf6_asbr_summary_remove_lsa_and_route(ospf6, aggr);

		return;
	}

	/* There are no routes present under this aggregation config, hence
	 * nothing to originate here
	 */
	if (OSPF6_EXTERNAL_RT_COUNT(aggr) == 0) {
		if (IS_OSPF6_DEBUG_AGGR)
			zlog_debug("%s: No routes present under this aggregation",
					__func__);
		return;
	}

	if (!CHECK_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_ORIGINATED)) {
		if (IS_OSPF6_DEBUG_AGGR)
			zlog_debug("%s: Now it is advertisable",
					__func__);

		ospf6_originate_new_aggr_lsa(ospf6, aggr);

		return;
	}
}

static void
ospf6_originate_summary_lsa(struct ospf6 *ospf6,
			    struct ospf6_external_aggr_rt *aggr,
			    struct ospf6_route *rt)
{
	struct ospf6_lsa *lsa = NULL, *aggr_lsa = NULL;
	struct ospf6_external_info *info = NULL;
	struct ospf6_external_aggr_rt *old_aggr;
	struct ospf6_as_external_lsa *external;
	struct ospf6_route *rt_aggr = NULL;
	route_tag_t tag = 0;
	unsigned int metric = 0;
	int mtype;

	if (IS_OSPF6_DEBUG_AGGR)
		zlog_debug("%s: Prepare to originate Summary route(%pFX)",
			   __func__, &aggr->p);

	/* This case to handle when the overlapping aggregator address
	 * is available. Best match will be considered.So need to delink
	 * from old aggregator and link to the new aggr.
	 */
	if (rt->aggr_route) {
		if (rt->aggr_route != aggr) {
			old_aggr = rt->aggr_route;
			ospf6_unlink_route_from_aggr(ospf6, old_aggr, rt);
		}
	}

	/* Add the external route to hash table */
	ospf6_link_route_to_aggr(aggr, rt);

	/* The key for ID field is a running number and not prefix */
	info = rt->route_option;
	assert(info);
	if (info->id)
		lsa = ospf6_lsdb_lookup(htons(OSPF6_LSTYPE_AS_EXTERNAL),
					htonl(info->id), ospf6->router_id,
					ospf6->lsdb);

	aggr_lsa = ospf6_lsdb_lookup(htons(OSPF6_LSTYPE_AS_EXTERNAL),
				htonl(aggr->id), ospf6->router_id, ospf6->lsdb);

	if (IS_OSPF6_DEBUG_AGGR)
		zlog_debug("%s: Aggr LSA ID: %d flags %x.",
		   __func__, aggr->id, aggr->aggrflags);
	/* Don't originate external LSA,
	 * If it is configured not to advertise.
	 */
	if (CHECK_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_NO_ADVERTISE)) {
		/* If it is already originated as external LSA,
		 * But, it is configured not to advertise then
		 * flush the originated external lsa.
		 */
		if (lsa) {
			if (IS_OSPF6_DEBUG_AGGR)
				zlog_debug("%s: Purge the external LSA %s.",
					   __func__, lsa->name);
			ospf6_external_lsa_purge(ospf6, lsa);
			info->id = 0;
			rt->path.origin.id = 0;
		}

		if (aggr_lsa) {
			if (IS_OSPF6_DEBUG_AGGR)
				zlog_debug("%s: Purge the aggr external LSA %s.",
					   __func__, lsa->name);
			ospf6_asbr_summary_remove_lsa_and_route(ospf6, aggr);
		}

		UNSET_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_ORIGINATED);

		if (IS_OSPF6_DEBUG_AGGR)
			zlog_debug("%s: Don't originate the summary address,It is configured to not-advertise.",
				__func__);
		return;
	}

	/* Summary route already originated,
	 * So, Do nothing.
	 */
	if (CHECK_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_ORIGINATED)) {
		if (!aggr_lsa) {
			zlog_warn(
				"%s: Could not refresh/originate %pFX",
						__func__,
						&aggr->p);
			/* Remove the assert later */
			assert(aggr_lsa);
			return;
		}

		external = (struct ospf6_as_external_lsa *)OSPF6_LSA_HEADER_END
					(aggr_lsa->header);
		metric = (unsigned long)OSPF6_ASBR_METRIC(external);
		tag = ospf6_as_external_lsa_get_tag(aggr_lsa);
		mtype = CHECK_FLAG(external->bits_metric,
				   OSPF6_ASBR_BIT_E) ? 2 : 1;

		/* Prepare the external_info for aggregator */
		ospf6_fill_aggr_route_details(ospf6, aggr);
		rt_aggr = aggr->route;
		/* If tag/metric/metric-type modified , then re-originate the
		 * route with modified tag/metric/metric-type details.
		 */
		if ((tag != aggr->tag)
		    || (metric != (unsigned int)rt_aggr->path.cost)
		    || (mtype != aggr->mtype)) {

			if (IS_OSPF6_DEBUG_AGGR)
				zlog_debug(
					"%s: Routetag(old:%d new:%d)/Metric(o:%u,n:%u)/mtype(o:%d n:%d) modified,So refresh the summary route.(%pFX)",
					__func__, tag, aggr->tag,
					metric,
					aggr->metric,
					mtype, aggr->mtype,
					&aggr->p);

			aggr_lsa = ospf6_originate_type5_type7_lsas(aggr->route,
								    ospf6);
			if (aggr_lsa)
				SET_FLAG(aggr->aggrflags,
					OSPF6_EXTERNAL_AGGRT_ORIGINATED);
		}

		return;
	}

	/* If the external route prefix same as aggregate route
	 * and if external route is already originated as TYPE-5
	 * then just update the aggr info and remove the route info
	 */
	if (lsa && prefix_same(&aggr->p, &rt->prefix)) {
		if (IS_OSPF6_DEBUG_AGGR)
			zlog_debug(
				"%s: Route prefix is same as aggr so no need to re-originate LSA(%pFX)",
				__PRETTY_FUNCTION__, &aggr->p);

		aggr->id = info->id;
		info->id = 0;
		rt->path.origin.id = 0;

		ospf6_summary_add_aggr_route_and_blackhole(ospf6, aggr);

		SET_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_ORIGINATED);

		return;
	}

	ospf6_originate_new_aggr_lsa(ospf6, aggr);
}

static void ospf6_aggr_handle_external_info(void *data)
{
	struct ospf6_route *rt = (struct ospf6_route *)data;
	struct ospf6_external_aggr_rt *aggr = NULL;
	struct ospf6_lsa *lsa = NULL;
	struct ospf6_external_info *info;
	struct ospf6 *ospf6 = NULL;

	rt->aggr_route = NULL;

	rt->to_be_processed = true;

	if (IS_OSPF6_DEBUG_ASBR || IS_OSPF6_DEBUG_ORIGINATE(AS_EXTERNAL))
		zlog_debug("%s: Handle external route for origination/refresh (%pFX)",
					__func__,
					&rt->prefix);

	ospf6 = rt->ospf6;
	assert(ospf6);

	aggr = ospf6_external_aggr_match(ospf6,
					&rt->prefix);
	if (aggr) {
		ospf6_originate_summary_lsa(ospf6, aggr, rt);
		return;
	}

	info = rt->route_option;
	if (info->id) {
		lsa = ospf6_lsdb_lookup(htons(OSPF6_LSTYPE_AS_EXTERNAL),
					htonl(info->id), ospf6->router_id,
					ospf6->lsdb);
		if (lsa) {
			if (IS_OSPF6_DEBUG_AGGR)
				zlog_debug("%s: LSA found, refresh it",
					   __func__);
			EVENT_OFF(lsa->refresh);
			event_add_event(master, ospf6_lsa_refresh, lsa, 0,
					&lsa->refresh);
			return;
		}
	}

	info->id  = ospf6->external_id++;
	rt->path.origin.id = htonl(info->id);

	(void)ospf6_originate_type5_type7_lsas(rt, ospf6);
}

void ospf6_asbr_summary_config_delete(struct ospf6 *ospf6,
				      struct route_node *rn)
{
	struct ospf6_external_aggr_rt *aggr = rn->info;

	if (IS_OSPF6_DEBUG_AGGR)
		zlog_debug("%s: Deleting Aggregate route (%pFX)",
						__func__,
						&aggr->p);

	ospf6_asbr_summary_remove_lsa_and_route(ospf6, aggr);

	rn->info = NULL;
	route_unlock_node(rn);
}

static int
ospf6_handle_external_aggr_modify(struct ospf6 *ospf6,
				  struct ospf6_external_aggr_rt *aggr)
{
	struct ospf6_lsa *lsa = NULL;
	struct ospf6_as_external_lsa *asel = NULL;
	struct ospf6_route *rt_aggr;
	unsigned int metric = 0;
	route_tag_t tag = 0;
	int mtype;

	lsa = ospf6_lsdb_lookup(
		htons(OSPF6_LSTYPE_AS_EXTERNAL),
		htonl(aggr->id), ospf6->router_id,
		ospf6->lsdb);
	if (!lsa) {
		zlog_warn(
			"%s: Could not refresh/originate %pFX",
			__func__,
			&aggr->p);

		return OSPF6_FAILURE;
	}

	asel = (struct ospf6_as_external_lsa *)
		OSPF6_LSA_HEADER_END(lsa->header);
	metric = (unsigned long)OSPF6_ASBR_METRIC(asel);
	tag = ospf6_as_external_lsa_get_tag(lsa);
	mtype = CHECK_FLAG(asel->bits_metric,
			   OSPF6_ASBR_BIT_E) ? 2 : 1;

	/* Fill all the details for advertisement */
	ospf6_fill_aggr_route_details(ospf6, aggr);
	rt_aggr = aggr->route;
	/* If tag/metric/metric-type modified , then
	 * re-originate the route with modified
	 * tag/metric/metric-type details.
	 */
	if ((tag != aggr->tag)
	    || (metric
		!= (unsigned int)rt_aggr->path.cost)
	    || (mtype
		!= aggr->mtype)) {
		if (IS_OSPF6_DEBUG_AGGR)
			zlog_debug(
			"%s: Changed tag(old:%d new:%d)/metric(o:%u n:%d)/mtype(o:%d n:%d),So refresh the summary route.(%pFX)",
			__func__, tag,
			aggr->tag,
			metric,
			(unsigned int)rt_aggr->path.cost,
			mtype, aggr->mtype,
			&aggr->p);

		(void)ospf6_originate_type5_type7_lsas(
					aggr->route,
					ospf6);
	}

	return OSPF6_SUCCESS;
}

static void ospf6_handle_external_aggr_update(struct ospf6 *ospf6)
{
	struct route_node *rn = NULL;
	int ret;

	if (IS_OSPF6_DEBUG_AGGR)
		zlog_debug("%s: Process modified aggregators.", __func__);

	for (rn = route_top(ospf6->rt_aggr_tbl); rn; rn = route_next(rn)) {
		struct ospf6_external_aggr_rt *aggr;

		if (!rn->info)
			continue;

		aggr = rn->info;

		if (aggr->action == OSPF6_ROUTE_AGGR_DEL) {
			aggr->action = OSPF6_ROUTE_AGGR_NONE;
			ospf6_asbr_summary_config_delete(ospf6, rn);

			hash_clean_and_free(&aggr->match_extnl_hash,
					    ospf6_aggr_handle_external_info);

			XFREE(MTYPE_OSPF6_EXTERNAL_RT_AGGR, aggr);

		} else if (aggr->action == OSPF6_ROUTE_AGGR_MODIFY) {

			aggr->action = OSPF6_ROUTE_AGGR_NONE;

			/* Check if tag/metric/metric-type modified */
			if (CHECK_FLAG(aggr->aggrflags,
				OSPF6_EXTERNAL_AGGRT_ORIGINATED)
			    && !CHECK_FLAG(aggr->aggrflags,
				OSPF6_EXTERNAL_AGGRT_NO_ADVERTISE)) {

				ret = ospf6_handle_external_aggr_modify(ospf6,
									aggr);
				if (ret == OSPF6_FAILURE)
					continue;
			}

			/* Advertise option modified ?
			 * If so, handled it here.
			 */
			ospf6_aggr_handle_advertise_change(ospf6, aggr);
		}
	}
}

static void ospf6_aggr_unlink_external_info(void *data)
{
	struct ospf6_route *rt = (struct ospf6_route *)data;

	rt->aggr_route = NULL;

	rt->to_be_processed = true;
}

void ospf6_external_aggregator_free(struct ospf6_external_aggr_rt *aggr)
{
	hash_clean_and_free(&aggr->match_extnl_hash,
			    ospf6_aggr_unlink_external_info);

	if (IS_OSPF6_DEBUG_AGGR)
		zlog_debug("%s: Release the aggregator Address(%pFX)",
						__func__,
						&aggr->p);

	XFREE(MTYPE_OSPF6_EXTERNAL_RT_AGGR, aggr);
}

static void
ospf6_delete_all_marked_aggregators(struct ospf6 *ospf6)
{
	struct route_node *rn = NULL;
	struct ospf6_external_aggr_rt *aggr;

	/* Loop through all the aggregators, Delete all aggregators
	 * which are marked as DELETE. Set action to NONE for remaining
	 * aggregators
	 */
	for (rn = route_top(ospf6->rt_aggr_tbl); rn; rn = route_next(rn)) {
		if (!rn->info)
			continue;

		aggr = rn->info;

		if (aggr->action != OSPF6_ROUTE_AGGR_DEL) {
			aggr->action = OSPF6_ROUTE_AGGR_NONE;
			continue;
		}
		ospf6_asbr_summary_config_delete(ospf6, rn);
		ospf6_external_aggregator_free(aggr);
	}
}

static void ospf6_handle_exnl_rt_after_aggr_del(struct ospf6 *ospf6,
					       struct ospf6_route *rt)
{
	struct ospf6_lsa *lsa;

	/* Process only marked external routes.
	 * These routes were part of a deleted
	 * aggregator.So, originate now.
	 */
	if (!rt->to_be_processed)
		return;

	rt->to_be_processed = false;

	lsa = ospf6_find_external_lsa(ospf6, &rt->prefix);

	if (lsa) {
		EVENT_OFF(lsa->refresh);
		event_add_event(master, ospf6_lsa_refresh, lsa, 0,
				&lsa->refresh);
	} else {
		if (IS_OSPF6_DEBUG_AGGR)
			zlog_debug("%s: Originate external route(%pFX)",
				__func__,
				&rt->prefix);

		(void)ospf6_originate_type5_type7_lsas(rt, ospf6);
	}
}

static void ospf6_handle_aggregated_exnl_rt(struct ospf6 *ospf6,
					   struct ospf6_external_aggr_rt *aggr,
					   struct ospf6_route *rt)
{
	struct ospf6_lsa *lsa;
	struct ospf6_as_external_lsa *ext_lsa;
	struct ospf6_external_info *info;

	/* Handling the case where the external route prefix
	 * and aggegate prefix is same
	 * If same don't flush the originated external LSA.
	 */
	if (prefix_same(&aggr->p, &rt->prefix)) {
		if (IS_OSPF6_DEBUG_AGGR)
			zlog_debug("%s: External Route prefix same as Aggregator(%pFX), so don't flush.",
				__func__,
				&rt->prefix);

		return;
	}

	info = rt->route_option;
	assert(info);

	lsa = ospf6_lsdb_lookup(htons(OSPF6_LSTYPE_AS_EXTERNAL),
				htonl(info->id), ospf6->router_id, ospf6->lsdb);
	if (lsa) {
		ext_lsa = (struct ospf6_as_external_lsa
			*)((char *)(lsa->header)
			+ sizeof(struct ospf6_lsa_header));

		if (rt->prefix.prefixlen != ext_lsa->prefix.prefix_length)
			return;

		ospf6_external_lsa_purge(ospf6, lsa);

		/* Resetting the ID of route */
		rt->path.origin.id = 0;
		info->id = 0;
	}
}

static void
ospf6_handle_external_aggr_add(struct ospf6 *ospf6)
{
	struct ospf6_route *rt = NULL;
	struct ospf6_external_info *ei = NULL;
	struct ospf6_external_aggr_rt *aggr;

	/* Delete all the aggregators which are marked as
	 * OSPF6_ROUTE_AGGR_DEL.
	 */
	ospf6_delete_all_marked_aggregators(ospf6);

	for (rt = ospf6_route_head(ospf6->external_table); rt;
		rt = ospf6_route_next(rt)) {
		ei = rt->route_option;
		if (ei == NULL)
			continue;

		if (is_default_prefix(&rt->prefix))
			continue;

		aggr = ospf6_external_aggr_match(ospf6,
					&rt->prefix);

		/* If matching aggregator found, Add
		 * the external route refrenace to the
		 * aggregator and originate the aggr
		 * route if it is advertisable.
		 * flush the external LSA if it is
		 * already originated for this external
		 * prefix.
		 */
		if (aggr) {
			ospf6_originate_summary_lsa(ospf6, aggr, rt);

			/* All aggregated external rts
			 * are handled here.
			 */
			ospf6_handle_aggregated_exnl_rt(
				ospf6, aggr, rt);
			continue;
		}

		/* External routes which are only out
		 * of aggregation will be handled here.
		 */
		ospf6_handle_exnl_rt_after_aggr_del(
					ospf6, rt);
	}
}

static void ospf6_asbr_summary_process(struct event *thread)
{
	struct ospf6 *ospf6 = EVENT_ARG(thread);
	int operation = 0;

	operation = ospf6->aggr_action;

	if (IS_OSPF6_DEBUG_AGGR)
		zlog_debug("%s: operation:%d",
				__func__,
				operation);

	switch (operation) {
	case OSPF6_ROUTE_AGGR_ADD:
		ospf6_handle_external_aggr_add(ospf6);
		break;
	case OSPF6_ROUTE_AGGR_DEL:
	case OSPF6_ROUTE_AGGR_MODIFY:
		ospf6_handle_external_aggr_update(ospf6);
		break;
	default:
		break;
	}
}

static void
ospf6_start_asbr_summary_delay_timer(struct ospf6 *ospf6,
			struct ospf6_external_aggr_rt *aggr,
			ospf6_aggr_action_t operation)
{
	aggr->action = operation;

	if (event_is_scheduled(ospf6->t_external_aggr)) {
		if (ospf6->aggr_action == OSPF6_ROUTE_AGGR_ADD) {

			if (IS_OSPF6_DEBUG_AGGR)
				zlog_debug("%s: Not required to restart timer,set is already added.",
					__func__);
			return;
		}

		if (operation == OSPF6_ROUTE_AGGR_ADD) {
			if (IS_OSPF6_DEBUG_AGGR)
				zlog_debug("%s, Restarting Aggregator delay timer.",
							__func__);
			EVENT_OFF(ospf6->t_external_aggr);
		}
	}

	if (IS_OSPF6_DEBUG_AGGR)
		zlog_debug("%s: Start Aggregator delay timer %u(in seconds).",
			   __func__, ospf6->aggr_delay_interval);

	ospf6->aggr_action = operation;
	event_add_timer(master, ospf6_asbr_summary_process, ospf6,
			ospf6->aggr_delay_interval, &ospf6->t_external_aggr);
}

int ospf6_asbr_external_rt_advertise(struct ospf6 *ospf6,
				struct prefix *p)
{
	struct route_node *rn;
	struct ospf6_external_aggr_rt *aggr;

	rn = route_node_lookup(ospf6->rt_aggr_tbl, p);
	if (!rn)
		return OSPF6_INVALID;

	aggr = rn->info;

	route_unlock_node(rn);

	if (!CHECK_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_NO_ADVERTISE))
		return OSPF6_INVALID;

	UNSET_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_NO_ADVERTISE);

	if (!OSPF6_EXTERNAL_RT_COUNT(aggr))
		return OSPF6_SUCCESS;

	ospf6_start_asbr_summary_delay_timer(ospf6, aggr,
					     OSPF6_ROUTE_AGGR_MODIFY);

	return OSPF6_SUCCESS;
}

int ospf6_external_aggr_delay_timer_set(struct ospf6 *ospf6, uint16_t interval)
{
	ospf6->aggr_delay_interval = interval;

	return OSPF6_SUCCESS;
}

static unsigned int ospf6_external_rt_hash_key(const void *data)
{
	const struct ospf6_route *rt = data;
	unsigned int key = 0;

	key = prefix_hash_key(&rt->prefix);
	return key;
}

static bool ospf6_external_rt_hash_cmp(const void *d1, const void *d2)
{
	const struct ospf6_route *rt1 = d1;
	const struct ospf6_route *rt2 = d2;

	return prefix_same(&rt1->prefix, &rt2->prefix);
}

static struct ospf6_external_aggr_rt *
ospf6_external_aggr_new(struct prefix *p)
{
	struct ospf6_external_aggr_rt *aggr;

	aggr = XCALLOC(MTYPE_OSPF6_EXTERNAL_RT_AGGR,
		       sizeof(struct ospf6_external_aggr_rt));

	prefix_copy(&aggr->p, p);
	aggr->metric = -1;
	aggr->mtype = DEFAULT_METRIC_TYPE;
	aggr->match_extnl_hash = hash_create(ospf6_external_rt_hash_key,
					     ospf6_external_rt_hash_cmp,
					     "Ospf6 external route hash");
	return aggr;
}

static void ospf6_external_aggr_add(struct ospf6 *ospf6,
		struct ospf6_external_aggr_rt *aggr)
{
	struct route_node *rn;

	if (IS_OSPF6_DEBUG_AGGR)
		zlog_debug("%s: Adding Aggregate route to Aggr table (%pFX)",
					__func__,
					&aggr->p);

	rn = route_node_get(ospf6->rt_aggr_tbl, &aggr->p);
	if (rn->info)
		route_unlock_node(rn);
	else
		rn->info = aggr;
}

int ospf6_asbr_external_rt_no_advertise(struct ospf6 *ospf6,
				struct prefix *p)
{
	struct ospf6_external_aggr_rt *aggr;
	route_tag_t tag = 0;

	aggr = ospf6_external_aggr_config_lookup(ospf6, p);
	if (aggr) {
		if (CHECK_FLAG(aggr->aggrflags,
			OSPF6_EXTERNAL_AGGRT_NO_ADVERTISE))
			return OSPF6_SUCCESS;

		SET_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_NO_ADVERTISE);

		aggr->tag = tag;
		aggr->metric = -1;

		if (!OSPF6_EXTERNAL_RT_COUNT(aggr))
			return OSPF6_SUCCESS;

		ospf6_start_asbr_summary_delay_timer(ospf6, aggr,
			OSPF6_ROUTE_AGGR_MODIFY);
	} else {
		aggr = ospf6_external_aggr_new(p);

		if (!aggr)
			return OSPF6_FAILURE;

		SET_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_NO_ADVERTISE);
		ospf6_external_aggr_add(ospf6, aggr);
		ospf6_start_asbr_summary_delay_timer(ospf6, aggr,
					OSPF6_ROUTE_AGGR_ADD);
	}

	return OSPF6_SUCCESS;
}

struct ospf6_external_aggr_rt *
ospf6_external_aggr_config_lookup(struct ospf6 *ospf6, struct prefix *p)
{
	struct route_node *rn;

	rn = route_node_lookup(ospf6->rt_aggr_tbl, p);
	if (rn) {
		route_unlock_node(rn);
		return rn->info;
	}

	return NULL;
}


int ospf6_external_aggr_config_set(struct ospf6 *ospf6, struct prefix *p,
				      route_tag_t tag, int metric, int mtype)
{
	struct ospf6_external_aggr_rt *aggregator;

	aggregator = ospf6_external_aggr_config_lookup(ospf6, p);

	if (aggregator) {
		if (CHECK_FLAG(aggregator->aggrflags,
			       OSPF6_EXTERNAL_AGGRT_NO_ADVERTISE))
			UNSET_FLAG(aggregator->aggrflags,
				   OSPF6_EXTERNAL_AGGRT_NO_ADVERTISE);
		else if ((aggregator->tag == tag)
			 && (aggregator->metric == metric)
			 && (aggregator->mtype == mtype))
			return OSPF6_SUCCESS;

		aggregator->tag = tag;
		aggregator->metric = metric;
		aggregator->mtype = mtype;

		ospf6_start_asbr_summary_delay_timer(ospf6, aggregator,
					 OSPF6_ROUTE_AGGR_MODIFY);
	} else {
		aggregator = ospf6_external_aggr_new(p);
		if (!aggregator)
			return OSPF6_FAILURE;

		aggregator->tag = tag;
		aggregator->metric = metric;
		aggregator->mtype = mtype;

		ospf6_external_aggr_add(ospf6, aggregator);
		ospf6_start_asbr_summary_delay_timer(ospf6, aggregator,
						OSPF6_ROUTE_AGGR_ADD);
	}

	return OSPF6_SUCCESS;
}

int ospf6_external_aggr_config_unset(struct ospf6 *ospf6,
					struct prefix *p)
{
	struct route_node *rn;
	struct ospf6_external_aggr_rt *aggr;

	rn = route_node_lookup(ospf6->rt_aggr_tbl, p);
	if (!rn)
		return OSPF6_INVALID;

	aggr = rn->info;

	route_unlock_node(rn);

	if (!OSPF6_EXTERNAL_RT_COUNT(aggr)) {
		ospf6_asbr_summary_config_delete(ospf6, rn);
		ospf6_external_aggregator_free(aggr);
		return OSPF6_SUCCESS;
	}

	ospf6_start_asbr_summary_delay_timer(ospf6, aggr,
				OSPF6_ROUTE_AGGR_DEL);

	return OSPF6_SUCCESS;
}

void ospf6_handle_external_lsa_origination(struct ospf6 *ospf6,
					       struct ospf6_route *rt,
					       struct prefix *p)
{

	struct ospf6_external_aggr_rt *aggr;
	struct ospf6_external_info *info;
	struct prefix prefix_id;

	if (!is_default_prefix(p)) {
		aggr = ospf6_external_aggr_match(ospf6,
						p);

		if (aggr) {

			if (IS_OSPF6_DEBUG_AGGR)
				zlog_debug("%s: Send Aggregate LSA (%pFX)",
				__func__,
				&aggr->p);

			ospf6_originate_summary_lsa(
				ospf6, aggr, rt);

			/* Handling the case where the
			 * external route prefix
			 * and aggegate prefix is same
			 * If same don't flush the
			 * originated
			 * external LSA.
			 */
			ospf6_handle_aggregated_exnl_rt(
					ospf6, aggr, rt);
			return;
		}
	}

	info = rt->route_option;

	/* When the info->id = 0, it means it is being originated for the
	 * first time.
	 */
	if (!info->id) {
		info->id = ospf6->external_id++;
	} else {
		prefix_id.family = AF_INET;
		prefix_id.prefixlen = 32;
		prefix_id.u.prefix4.s_addr = htonl(info->id);
	}

	rt->path.origin.id = htonl(info->id);

	if (IS_OSPF6_DEBUG_ASBR) {
		zlog_debug("Advertise new AS-External Id:%pI4 prefix %pFX metric %u",
			   &prefix_id.u.prefix4, p, rt->path.metric_type);
	}

	ospf6_originate_type5_type7_lsas(rt, ospf6);

}

void ospf6_unset_all_aggr_flag(struct ospf6 *ospf6)
{
	struct route_node *rn = NULL;
	struct ospf6_external_aggr_rt *aggr;

	if (IS_OSPF6_DEBUG_AGGR)
		zlog_debug("Unset the origination bit for all aggregator");

	/* Resetting the running external ID counter so that the origination
	 * of external LSAs starts from the beginning 0.0.0.1
	 */
	ospf6->external_id = OSPF6_EXT_INIT_LS_ID;

	for (rn = route_top(ospf6->rt_aggr_tbl); rn; rn = route_next(rn)) {
		if (!rn->info)
			continue;

		aggr = rn->info;

		UNSET_FLAG(aggr->aggrflags, OSPF6_EXTERNAL_AGGRT_ORIGINATED);
	}
}