summaryrefslogtreecommitdiffstats
path: root/ospfd/ospf_lsa.c
blob: f125fa93b1e4011b415a89a09997717d0dffa511 (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
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * OSPF Link State Advertisement
 * Copyright (C) 1999, 2000 Toshiaki Takada
 */

#include <zebra.h>

#include "monotime.h"
#include "linklist.h"
#include "prefix.h"
#include "if.h"
#include "table.h"
#include "memory.h"
#include "stream.h"
#include "log.h"
#include "frrevent.h"
#include "hash.h"
#include "sockunion.h" /* for inet_aton() */
#include "checksum.h"
#include "network.h"

#include "ospfd/ospfd.h"
#include "ospfd/ospf_interface.h"
#include "ospfd/ospf_ism.h"
#include "ospfd/ospf_asbr.h"
#include "ospfd/ospf_lsa.h"
#include "ospfd/ospf_lsdb.h"
#include "ospfd/ospf_neighbor.h"
#include "ospfd/ospf_nsm.h"
#include "ospfd/ospf_flood.h"
#include "ospfd/ospf_packet.h"
#include "ospfd/ospf_spf.h"
#include "ospfd/ospf_dump.h"
#include "ospfd/ospf_route.h"
#include "ospfd/ospf_ase.h"
#include "ospfd/ospf_zebra.h"
#include "ospfd/ospf_abr.h"
#include "ospfd/ospf_errors.h"

static struct ospf_lsa *ospf_handle_summarylsa_lsId_chg(struct ospf_area *area,
							struct prefix_ipv4 *p,
							uint8_t type,
							uint32_t metric,
							struct in_addr old_id);
static struct ospf_lsa *
ospf_summary_lsa_prepare_and_flood(struct prefix_ipv4 *p, uint32_t metric,
				   struct ospf_area *area, struct in_addr id);
static struct ospf_lsa *ospf_summary_lsa_refresh(struct ospf *ospf,
						 struct ospf_lsa *lsa);
static struct ospf_lsa *
ospf_asbr_summary_lsa_prepare_and_flood(struct prefix_ipv4 *p, uint32_t metric,
					struct ospf_area *area,
					struct in_addr id);
static struct ospf_lsa *ospf_summary_asbr_lsa_refresh(struct ospf *ospf,
						      struct ospf_lsa *lsa);
static struct ospf_lsa *ospf_handle_exnl_lsa_lsId_chg(struct ospf *ospf,
						      struct external_info *ei,
						      struct in_addr id);
static struct ospf_lsa *
ospf_exnl_lsa_prepare_and_flood(struct ospf *ospf, struct external_info *ei,
				struct in_addr id);

uint32_t get_metric(uint8_t *metric)
{
	uint32_t m;
	m = metric[0];
	m = (m << 8) + metric[1];
	m = (m << 8) + metric[2];
	return m;
}

/** @brief The Function checks self generated DoNotAge.
 *  @param lsa pointer.
 *  @return true or false.
 */
bool ospf_check_dna_lsa(const struct ospf_lsa *lsa)
{
	return ((IS_LSA_SELF(lsa) && CHECK_FLAG(lsa->data->ls_age, DO_NOT_AGE))
			? true
			: false);
}

struct timeval int2tv(int a)
{
	struct timeval ret;

	ret.tv_sec = a;
	ret.tv_usec = 0;

	return ret;
}

struct timeval msec2tv(int a)
{
	struct timeval ret;

	ret.tv_sec = a / 1000;
	ret.tv_usec = (a % 1000) * 1000;

	return ret;
}

int ospf_lsa_refresh_delay(struct ospf_lsa *lsa)
{
	struct timeval delta;
	int delay = 0;

	if (monotime_since(&lsa->tv_orig, &delta)
	    < OSPF_MIN_LS_INTERVAL * 1000LL) {
		struct timeval minv = msec2tv(OSPF_MIN_LS_INTERVAL);
		timersub(&minv, &delta, &minv);

		/* TBD: remove padding to full sec, return timeval instead */
		delay = minv.tv_sec + !!minv.tv_usec;

		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug(
				"LSA[Type%d:%pI4]: Refresh timer delay %d seconds",
				lsa->data->type, &lsa->data->id,
				delay);

		assert(delay > 0);
	}

	return delay;
}


int get_age(struct ospf_lsa *lsa)
{
	struct timeval rel;

	/* As per rfc4136, the self-originated LSAs in their
	 * own database keep aging, however rfc doesn't tell
	 * till how long the LSA should be aged, as of now
	 * we are capping it for OSPF_LSA_MAXAGE.
	 */

	/* If LSA is marked as donotage */
	if (CHECK_FLAG(lsa->data->ls_age, DO_NOT_AGE) && !IS_LSA_SELF(lsa))
		return ntohs(lsa->data->ls_age);

	monotime_since(&lsa->tv_recv, &rel);
	return ntohs(lsa->data->ls_age) + rel.tv_sec;
}


/* Fletcher Checksum -- Refer to RFC1008. */

/* All the offsets are zero-based. The offsets in the RFC1008 are
   one-based. */
uint16_t ospf_lsa_checksum(struct lsa_header *lsa)
{
	uint8_t *buffer = &lsa->options;
	int options_offset = buffer - (uint8_t *)&lsa->ls_age; /* should be 2 */

	/* Skip the AGE field */
	uint16_t len = ntohs(lsa->length) - options_offset;

	/* Checksum offset starts from "options" field, not the beginning of the
	   lsa_header struct. The offset is 14, rather than 16. */
	int checksum_offset = (uint8_t *)&lsa->checksum - buffer;

	return fletcher_checksum(buffer, len, checksum_offset);
}

int ospf_lsa_checksum_valid(struct lsa_header *lsa)
{
	uint8_t *buffer = &lsa->options;
	int options_offset = buffer - (uint8_t *)&lsa->ls_age; /* should be 2 */

	/* Skip the AGE field */
	uint16_t len = ntohs(lsa->length) - options_offset;

	return (fletcher_checksum(buffer, len, FLETCHER_CHECKSUM_VALIDATE)
		== 0);
}


/* Create OSPF LSA. */
struct ospf_lsa *ospf_lsa_new(void)
{
	struct ospf_lsa *new;

	new = XCALLOC(MTYPE_OSPF_LSA, sizeof(struct ospf_lsa));

	new->flags = 0;
	new->lock = 1;
	new->retransmit_counter = 0;
	monotime(&new->tv_recv);
	new->tv_orig = new->tv_recv;
	new->refresh_list = -1;
	new->vrf_id = VRF_DEFAULT;
	new->to_be_acknowledged = 0;
	new->opaque_zero_len_delete = 0;

	return new;
}

struct ospf_lsa *ospf_lsa_new_and_data(size_t size)
{
	struct ospf_lsa *new;

	new = ospf_lsa_new();
	new->data = ospf_lsa_data_new(size);
	new->size = size;

	return new;
}

/* Duplicate OSPF LSA. */
struct ospf_lsa *ospf_lsa_dup(struct ospf_lsa *lsa)
{
	struct ospf_lsa *new;

	if (lsa == NULL)
		return NULL;

	new = XCALLOC(MTYPE_OSPF_LSA, sizeof(struct ospf_lsa));

	memcpy(new, lsa, sizeof(struct ospf_lsa));
	UNSET_FLAG(new->flags, OSPF_LSA_DISCARD);
	new->lock = 1;
	new->retransmit_counter = 0;
	new->data = ospf_lsa_data_dup(lsa->data);

	/* kevinm: Clear the refresh_list, otherwise there are going
	   to be problems when we try to remove the LSA from the
	   queue (which it's not a member of.)
	   XXX: Should we add the LSA to the refresh_list queue? */
	new->refresh_list = -1;

	if (IS_DEBUG_OSPF(lsa, LSA))
		zlog_debug("LSA: duplicated %p (new: %p)", (void *)lsa,
			   (void *)new);

	return new;
}

/* Free OSPF LSA. */
void ospf_lsa_free(struct ospf_lsa *lsa)
{
	assert(lsa->lock == 0);

	if (IS_DEBUG_OSPF(lsa, LSA))
		zlog_debug("LSA: freed %p", (void *)lsa);

	/* Delete LSA data. */
	if (lsa->data != NULL)
		ospf_lsa_data_free(lsa->data);

	assert(lsa->refresh_list < 0);

	memset(lsa, 0, sizeof(struct ospf_lsa));
	XFREE(MTYPE_OSPF_LSA, lsa);
}

/* Lock LSA. */
struct ospf_lsa *ospf_lsa_lock(struct ospf_lsa *lsa)
{
	lsa->lock++;
	return lsa;
}

/* Unlock LSA. */
void ospf_lsa_unlock(struct ospf_lsa **lsa)
{
	/* This is sanity check. */
	if (!lsa || !*lsa)
		return;

	(*lsa)->lock--;

	assert((*lsa)->lock >= 0);

	if ((*lsa)->lock == 0) {
		assert(CHECK_FLAG((*lsa)->flags, OSPF_LSA_DISCARD));
		ospf_lsa_free(*lsa);
		*lsa = NULL;
	}
}

/* Check discard flag. */
void ospf_lsa_discard(struct ospf_lsa *lsa)
{
	if (!CHECK_FLAG(lsa->flags, OSPF_LSA_DISCARD)) {
		SET_FLAG(lsa->flags, OSPF_LSA_DISCARD);
		ospf_lsa_unlock(&lsa);
	}
}

/* Create LSA data. */
struct lsa_header *ospf_lsa_data_new(size_t size)
{
	return XCALLOC(MTYPE_OSPF_LSA_DATA, size);
}

/* Duplicate LSA data. */
struct lsa_header *ospf_lsa_data_dup(struct lsa_header *lsah)
{
	struct lsa_header *new;

	new = ospf_lsa_data_new(ntohs(lsah->length));
	memcpy(new, lsah, ntohs(lsah->length));

	return new;
}

/* Free LSA data. */
void ospf_lsa_data_free(struct lsa_header *lsah)
{
	if (IS_DEBUG_OSPF(lsa, LSA))
		zlog_debug("LSA[Type%d:%pI4]: data freed %p", lsah->type,
			   &lsah->id, (void *)lsah);

	XFREE(MTYPE_OSPF_LSA_DATA, lsah);
}


/* LSA general functions. */

const char *dump_lsa_key(struct ospf_lsa *lsa)
{
	static char buf[sizeof("Type255,id(255.255.255.255),ar(255.255.255.255)")+1];
	struct lsa_header *lsah;

	if (lsa != NULL && (lsah = lsa->data) != NULL) {
		char id[INET_ADDRSTRLEN], ar[INET_ADDRSTRLEN];
		inet_ntop(AF_INET, &lsah->id, id, sizeof(id));
		inet_ntop(AF_INET, &lsah->adv_router, ar, sizeof(ar));

		snprintf(buf, sizeof(buf), "Type%d,id(%s),ar(%s)", lsah->type,
			 id, ar);
	} else
		strlcpy(buf, "NULL", sizeof(buf));

	return buf;
}

uint32_t lsa_seqnum_increment(struct ospf_lsa *lsa)
{
	uint32_t seqnum;

	seqnum = ntohl(lsa->data->ls_seqnum) + 1;

	return htonl(seqnum);
}

void lsa_header_set(struct stream *s, uint8_t options, uint8_t type,
		    struct in_addr id, struct in_addr router_id)
{
	struct lsa_header *lsah;

	lsah = (struct lsa_header *)STREAM_DATA(s);

	lsah->ls_age = htons(OSPF_LSA_INITIAL_AGE);
	lsah->options = options;
	lsah->type = type;
	lsah->id = id;
	lsah->adv_router = router_id;
	lsah->ls_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);

	stream_forward_endp(s, OSPF_LSA_HEADER_SIZE);
}


/* router-LSA related functions. */
/* Get router-LSA flags. */
uint8_t router_lsa_flags(struct ospf_area *area)
{
	uint8_t flags;

	flags = area->ospf->flags;

	/* Set virtual link flag. */
	if (ospf_full_virtual_nbrs(area))
		SET_FLAG(flags, ROUTER_LSA_VIRTUAL);
	else
		/* Just sanity check */
		UNSET_FLAG(flags, ROUTER_LSA_VIRTUAL);

	/* Set Shortcut ABR behabiour flag. */
	UNSET_FLAG(flags, ROUTER_LSA_SHORTCUT);
	if (area->ospf->abr_type == OSPF_ABR_SHORTCUT)
		if (!OSPF_IS_AREA_BACKBONE(area))
			if ((area->shortcut_configured == OSPF_SHORTCUT_DEFAULT
			     && area->ospf->backbone == NULL)
			    || area->shortcut_configured
				       == OSPF_SHORTCUT_ENABLE)
				SET_FLAG(flags, ROUTER_LSA_SHORTCUT);

	/* ASBR can't exit in stub area. */
	if (area->external_routing == OSPF_AREA_STUB)
		UNSET_FLAG(flags, ROUTER_LSA_EXTERNAL);
	/* If ASBR set External flag */
	else if (IS_OSPF_ASBR(area->ospf))
		SET_FLAG(flags, ROUTER_LSA_EXTERNAL);

	/* Set ABR dependent flags */
	if (IS_OSPF_ABR(area->ospf)) {
		SET_FLAG(flags, ROUTER_LSA_BORDER);
		/* If Area is NSSA and we are both ABR and unconditional
		 * translator,
		 * set Nt bit to inform other routers.
		 */
		if ((area->external_routing == OSPF_AREA_NSSA)
		    && (area->NSSATranslatorRole == OSPF_NSSA_ROLE_ALWAYS))
			SET_FLAG(flags, ROUTER_LSA_NT);
	}
	return flags;
}

/* Lookup neighbor other than myself.
   And check neighbor count,
   Point-to-Point link must have only 1 neighbor. */
struct ospf_neighbor *ospf_nbr_lookup_ptop(struct ospf_interface *oi)
{
	struct ospf_neighbor *nbr = NULL;
	struct route_node *rn;

	/* Search neighbor, there must be one of two nbrs. */
	for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
		if ((nbr = rn->info))
			if (!IPV4_ADDR_SAME(&nbr->router_id,
					    &oi->ospf->router_id))
				if (nbr->state == NSM_Full) {
					route_unlock_node(rn);
					break;
				}

	/* PtoP link must have only 1 neighbor. */
	if (ospf_nbr_count(oi, 0) > 1)
		flog_warn(
			EC_OSPF_PTP_NEIGHBOR,
			"Point-to-Point link on interface %s has more than 1 neighbor.",
			oi->ifp->name);

	return nbr;
}

/* Determine cost of link, taking RFC3137 stub-router support into
 * consideration
 */
static uint16_t ospf_link_cost(struct ospf_interface *oi)
{
	/* RFC3137 stub router support */
	if (!CHECK_FLAG(oi->area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED))
		return oi->output_cost;
	else
		return OSPF_OUTPUT_COST_INFINITE;
}

/* Set a link information. */
char link_info_set(struct stream **s, struct in_addr id, struct in_addr data,
		   uint8_t type, uint8_t tos, uint16_t cost)
{
	/* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
	 * vast majority of cases. Some rare routers with lots of links need
	 * more.
	 * we try accommodate those here.
	 */
	if (STREAM_WRITEABLE(*s) < OSPF_ROUTER_LSA_LINK_SIZE) {
		size_t ret = OSPF_MAX_LSA_SIZE;

		/* Can we enlarge the stream still? */
		if (STREAM_SIZE(*s) == OSPF_MAX_LSA_SIZE) {
			/* we futz the size here for simplicity, really we need
			 * to account
			 * for just:
			 * IP Header - (sizeof(struct ip))
			 * OSPF Header - OSPF_HEADER_SIZE
			 * LSA Header - OSPF_LSA_HEADER_SIZE
			 * MD5 auth data, if MD5 is configured -
			 * OSPF_AUTH_MD5_SIZE.
			 *
			 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
			 */
			ret = stream_resize_inplace(
				s, OSPF_MAX_PACKET_SIZE - OSPF_MAX_LSA_SIZE);
		}

		if (ret == OSPF_MAX_LSA_SIZE) {
			flog_warn(
				EC_OSPF_LSA_SIZE,
				"%s: Out of space in LSA stream, left %zd, size %zd",
				__func__, STREAM_WRITEABLE(*s),
				STREAM_SIZE(*s));
			return 0;
		}
	}

	/* TOS based routing is not supported. */
	stream_put_ipv4(*s, id.s_addr);   /* Link ID. */
	stream_put_ipv4(*s, data.s_addr); /* Link Data. */
	stream_putc(*s, type);		  /* Link Type. */
	stream_putc(*s, tos);		  /* TOS = 0. */
	stream_putw(*s, cost);		  /* Link Cost. */

	return 1;
}

/* Describe Point-to-Point link (Section 12.4.1.1). */

/* Note: If the interface is configured as point-to-point dmvpn then the other
 * end of link is dmvpn hub with point-to-multipoint ospf network type. The
 * hub then expects this router to populate the stub network and also Link Data
 * Field set to IP Address and not MIB-II ifIndex
 */
static int lsa_link_ptop_set(struct stream **s, struct ospf_interface *oi)
{
	int links = 0;
	struct ospf_neighbor *nbr;
	struct in_addr id, mask, data;
	uint16_t cost = ospf_link_cost(oi);

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
		zlog_debug("LSA[Type1]: Set link Point-to-Point");

	if ((nbr = ospf_nbr_lookup_ptop(oi)))
		if (nbr->state == NSM_Full) {
			if (CHECK_FLAG(oi->connected->flags,
				       ZEBRA_IFA_UNNUMBERED)
			    && !oi->ptp_dmvpn) {
				/* For unnumbered point-to-point networks, the
				   Link Data field
				   should specify the interface's MIB-II ifIndex
				   value. */
				data.s_addr = htonl(oi->ifp->ifindex);
				links += link_info_set(
					s, nbr->router_id, data,
					LSA_LINK_TYPE_POINTOPOINT, 0, cost);
			} else {
				links += link_info_set(
					s, nbr->router_id,
					oi->address->u.prefix4,
					LSA_LINK_TYPE_POINTOPOINT, 0, cost);
			}
		}

	/* no need for a stub link for unnumbered interfaces */
	if (OSPF_IF_PARAM(oi, prefix_suppression)) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug("LSA[Type1]: Interface %s stub link omitted due prefix-suppression",
				   oi->ifp->name);
	} else {
		if (oi->ptp_dmvpn ||
		    !CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED)) {
			/* Regardless of the state of the neighboring router, we must
			   add a Type 3 link (stub network).
			   N.B. Options 1 & 2 share basically the same logic. */
			masklen2ip(oi->address->prefixlen, &mask);
			id.s_addr =
				CONNECTED_PREFIX(oi->connected)->u.prefix4.s_addr &
				mask.s_addr;
			links += link_info_set(s, id, mask, LSA_LINK_TYPE_STUB,
					       0, oi->output_cost);
		}
	}

	return links;
}

/* Describe Broadcast Link. */
static int lsa_link_broadcast_set(struct stream **s, struct ospf_interface *oi)
{
	struct ospf_neighbor *dr;
	struct in_addr id, mask;
	uint16_t cost = ospf_link_cost(oi);

	/* Describe Type 3 Link. */
	if (oi->state == ISM_Waiting) {
		if (OSPF_IF_PARAM(oi, prefix_suppression)) {
			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
				zlog_debug("LSA[Type1]: Interface %s stub link omitted due prefix-suppression",
					   oi->ifp->name);
			return 0;
		}
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug("LSA[Type1]: Interface %s is in state Waiting. Adding stub interface",
				   oi->ifp->name);
		masklen2ip(oi->address->prefixlen, &mask);
		id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
		return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
				     oi->output_cost);
	}

	dr = ospf_nbr_lookup_by_addr(oi->nbrs, &DR(oi));
	/* Describe Type 2 link. */
	if (dr && (dr->state == NSM_Full
		   || IPV4_ADDR_SAME(&oi->address->u.prefix4, &DR(oi)))
	    && ospf_nbr_count(oi, NSM_Full) > 0) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug(
				"LSA[Type1]: Interface %s has a DR. Adding transit interface",
				oi->ifp->name);
		return link_info_set(s, DR(oi), oi->address->u.prefix4,
				     LSA_LINK_TYPE_TRANSIT, 0, cost);
	}
	/* Describe type 3 link. */
	else {
		if (OSPF_IF_PARAM(oi, prefix_suppression)) {
			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
				zlog_debug("LSA[Type1]: Interface %s stub link omitted due prefix-suppression",
					   oi->ifp->name);
			return 0;
		}
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug("LSA[Type1]: Interface %s has no DR. Adding stub interface",
				   oi->ifp->name);
		masklen2ip(oi->address->prefixlen, &mask);
		id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
		return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
				     oi->output_cost);
	}
}

static int lsa_link_loopback_set(struct stream **s, struct ospf_interface *oi)
{
	struct in_addr id, mask;

	/* Describe Type 3 Link. */
	if ((oi->state != ISM_Loopback) || OSPF_IF_PARAM(oi, prefix_suppression))
		return 0;

	mask.s_addr = 0xffffffff;
	id.s_addr = oi->address->u.prefix4.s_addr;
	return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
			     oi->output_cost);
}

/* Describe Virtual Link. */
static int lsa_link_virtuallink_set(struct stream **s,
				    struct ospf_interface *oi)
{
	struct ospf_neighbor *nbr;
	uint16_t cost = ospf_link_cost(oi);

	if (oi->state == ISM_PointToPoint)
		if ((nbr = ospf_nbr_lookup_ptop(oi)))
			if (nbr->state == NSM_Full) {
				return link_info_set(s, nbr->router_id,
						     oi->address->u.prefix4,
						     LSA_LINK_TYPE_VIRTUALLINK,
						     0, cost);
			}

	return 0;
}

#define lsa_link_nbma_set(S,O)  lsa_link_broadcast_set (S, O)

/* this function add for support point-to-multipoint ,see rfc2328
12.4.1.4.*/
/* from "edward rrr" <edward_rrr@hotmail.com>
   http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
static int lsa_link_ptomp_set(struct stream **s, struct ospf_interface *oi)
{
	int links = 0;
	struct route_node *rn;
	struct ospf_neighbor *nbr = NULL;
	struct in_addr id, mask;
	uint16_t cost = ospf_link_cost(oi);

	if (OSPF_IF_PARAM(oi, prefix_suppression)) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug("LSA[Type1]: Interface %s stub link omitted due prefix-suppression",
				   oi->ifp->name);
	} else {
		mask.s_addr = 0xffffffff;
		id.s_addr = oi->address->u.prefix4.s_addr;
		links += link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
	}

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
		zlog_debug("PointToMultipoint: running ptomultip_set");

	/* Search neighbor, */
	for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
		if ((nbr = rn->info) != NULL)
			/* Ignore myself. */
			if (!IPV4_ADDR_SAME(&nbr->router_id,
					    &oi->ospf->router_id))
				if (nbr->state == NSM_Full)

				{
					links += link_info_set(
						s, nbr->router_id,
						oi->address->u.prefix4,
						LSA_LINK_TYPE_POINTOPOINT, 0,
						cost);
					if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
						zlog_debug(
							"PointToMultipoint: set link to %pI4",
							&oi->address->u.prefix4);
				}

	return links;
}

/* Set router-LSA link information. */
static int router_lsa_link_set(struct stream **s, struct ospf_area *area)
{
	struct listnode *node;
	struct ospf_interface *oi;
	int links = 0;

	for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi)) {
		struct interface *ifp = oi->ifp;

		/* Check interface is up, OSPF is enable. */
		if (if_is_operative(ifp)) {
			if (oi->state != ISM_Down) {
				oi->lsa_pos_beg = links;
				/* Describe each link. */
				switch (oi->type) {
				case OSPF_IFTYPE_POINTOPOINT:
					links += lsa_link_ptop_set(s, oi);
					break;
				case OSPF_IFTYPE_BROADCAST:
					links += lsa_link_broadcast_set(s, oi);
					break;
				case OSPF_IFTYPE_NBMA:
					links += lsa_link_nbma_set(s, oi);
					break;
				case OSPF_IFTYPE_POINTOMULTIPOINT:
					links += lsa_link_ptomp_set(s, oi);
					break;
				case OSPF_IFTYPE_VIRTUALLINK:
					links +=
						lsa_link_virtuallink_set(s, oi);
					break;
				case OSPF_IFTYPE_LOOPBACK:
					links += lsa_link_loopback_set(s, oi);
				}
				oi->lsa_pos_end = links;
			}
		}
	}

	return links;
}

/* Set router-LSA body. */
void ospf_router_lsa_body_set(struct stream **s, struct ospf_area *area)
{
	unsigned long putp;
	uint16_t cnt;

	/* Set flags. */
	stream_putc(*s, router_lsa_flags(area));

	/* Set Zero fields. */
	stream_putc(*s, 0);

	/* Keep pointer to # links. */
	putp = stream_get_endp(*s);

	/* Forward word */
	stream_putw(*s, 0);

	/* Set all link information. */
	cnt = router_lsa_link_set(s, area);

	/* Set # of links here. */
	stream_putw_at(*s, putp, cnt);
}

static void ospf_stub_router_timer(struct event *t)
{
	struct ospf_area *area = EVENT_ARG(t);

	area->t_stub_router = NULL;

	SET_FLAG(area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);

	/* clear stub route state and generate router-lsa refresh, don't
	 * clobber an administratively set stub-router state though.
	 */
	if (CHECK_FLAG(area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
		return;

	UNSET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);

	ospf_router_lsa_update_area(area);
}

static void ospf_stub_router_check(struct ospf_area *area)
{
	/* area must either be administratively configured to be stub
	 * or startup-time stub-router must be configured and we must in a
	 * pre-stub
	 * state.
	 */
	if (CHECK_FLAG(area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED)) {
		SET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
		return;
	}

	/* not admin-stubbed, check whether startup stubbing is configured and
	 * whether it's not been done yet
	 */
	if (CHECK_FLAG(area->stub_router_state,
		       OSPF_AREA_WAS_START_STUB_ROUTED))
		return;

	if (area->ospf->stub_router_startup_time
	    == OSPF_STUB_ROUTER_UNCONFIGURED) {
		/* stub-router is hence done forever for this area, even if
		 * someone
		 * tries configure it (take effect next restart).
		 */
		SET_FLAG(area->stub_router_state,
			 OSPF_AREA_WAS_START_STUB_ROUTED);
		return;
	}

	/* startup stub-router configured and not yet done */
	SET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);

	OSPF_AREA_TIMER_ON(area->t_stub_router, ospf_stub_router_timer,
			   area->ospf->stub_router_startup_time);
}

/* Create new router-LSA. */
static struct ospf_lsa *ospf_router_lsa_new(struct ospf_area *area)
{
	struct ospf *ospf = area->ospf;
	struct stream *s;
	struct lsa_header *lsah;
	struct ospf_lsa *new;
	int length;

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
		zlog_debug("LSA[Type1]: Create router-LSA instance");

	/* check whether stub-router is desired, and if this is the first
	 * router LSA.
	 */
	ospf_stub_router_check(area);

	/* Create a stream for LSA. */
	s = stream_new(OSPF_MAX_LSA_SIZE);
	/* Set LSA common header fields. */
	lsa_header_set(s, LSA_OPTIONS_GET(area) | LSA_OPTIONS_NSSA_GET(area),
		       OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);

	/* Set router-LSA body fields. */
	ospf_router_lsa_body_set(&s, area);

	/* Set length. */
	length = stream_get_endp(s);
	lsah = (struct lsa_header *)STREAM_DATA(s);
	lsah->length = htons(length);

	/* Now, create OSPF LSA instance. */
	new = ospf_lsa_new_and_data(length);

	new->area = area;
	SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
	new->vrf_id = area->ospf->vrf_id;

	/* Copy LSA data to store, discard stream. */
	memcpy(new->data, lsah, length);
	stream_free(s);

	return new;
}

/* Originate Router-LSA. */
static struct ospf_lsa *ospf_router_lsa_originate(struct ospf_area *area)
{
	struct ospf_lsa *new;

	if (area->ospf->gr_info.restart_in_progress) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug(
				"LSA[Type%d]: Graceful Restart in progress, don't originate",
				OSPF_ROUTER_LSA);
		return NULL;
	}

	/* Create new router-LSA instance. */
	if ((new = ospf_router_lsa_new(area)) == NULL) {
		zlog_err("%s: ospf_router_lsa_new returned NULL", __func__);
		return NULL;
	}

	/* Sanity check. */
	if (new->data->adv_router.s_addr == INADDR_ANY) {
		if (IS_DEBUG_OSPF_EVENT)
			zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
		ospf_lsa_discard(new);
		return NULL;
	}

	/* Install LSA to LSDB. */
	new = ospf_lsa_install(area->ospf, NULL, new);

	/* Update LSA origination count. */
	area->ospf->lsa_originate_count++;

	/* Flooding new LSA through area. */
	ospf_flood_through_area(area, NULL, new);

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
		zlog_debug("LSA[Type%d:%pI4]: Originate router-LSA %p",
			   new->data->type, &new->data->id,
			   (void *)new);
		ospf_lsa_header_dump(new->data);
	}

	return new;
}

/* Refresh router-LSA. */
static struct ospf_lsa *ospf_router_lsa_refresh(struct ospf_lsa *lsa)
{
	struct ospf_area *area = lsa->area;
	struct ospf_lsa *new;

	/* Sanity check. */
	assert(lsa->data);

	/* Delete LSA from neighbor retransmit-list. */
	ospf_ls_retransmit_delete_nbr_area(area, lsa);

	/* Unregister LSA from refresh-list */
	ospf_refresher_unregister_lsa(area->ospf, lsa);

	/* Create new router-LSA instance. */
	if ((new = ospf_router_lsa_new(area)) == NULL) {
		zlog_err("%s: ospf_router_lsa_new returned NULL", __func__);
		return NULL;
	}

	new->data->ls_seqnum = lsa_seqnum_increment(lsa);

	ospf_lsa_install(area->ospf, NULL, new);

	/* Flood LSA through area. */
	ospf_flood_through_area(area, NULL, new);

	/* Debug logging. */
	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
		zlog_debug("LSA[Type%d:%pI4]: router-LSA refresh",
			   new->data->type, &new->data->id);
		ospf_lsa_header_dump(new->data);
	}

	return NULL;
}

int ospf_router_lsa_update_area(struct ospf_area *area)
{
	if (IS_DEBUG_OSPF_EVENT)
		zlog_debug("[router-LSA]: (router-LSA area update)");

	/* Now refresh router-LSA. */
	if (area->router_lsa_self)
		ospf_lsa_refresh(area->ospf, area->router_lsa_self);
	/* Newly originate router-LSA. */
	else
		ospf_router_lsa_originate(area);

	return 0;
}

int ospf_router_lsa_update(struct ospf *ospf)
{
	struct listnode *node, *nnode;
	struct ospf_area *area;

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
		zlog_debug("Timer[router-LSA Update]: (timer expire)");

	for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
		struct ospf_lsa *lsa = area->router_lsa_self;
		struct router_lsa *rl;
		const char *area_str;

		/* Keep Area ID string. */
		area_str = AREA_NAME(area);

		/* If LSA not exist in this Area, originate new. */
		if (lsa == NULL) {
			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
				zlog_debug(
					"LSA[Type1]: Create router-LSA for Area %s",
					area_str);

			ospf_router_lsa_originate(area);
		}
		/* If router-ID is changed, Link ID must change.
		   First flush old LSA, then originate new. */
		else if (!IPV4_ADDR_SAME(&lsa->data->id, &ospf->router_id)) {
			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
				zlog_debug(
					"LSA[Type%d:%pI4]: Refresh router-LSA for Area %s",
					lsa->data->type,
					&lsa->data->id, area_str);
			ospf_refresher_unregister_lsa(ospf, lsa);
			ospf_lsa_flush_area(lsa, area);
			ospf_lsa_unlock(&area->router_lsa_self);
			area->router_lsa_self = NULL;

			/* Refresh router-LSA, (not install) and flood through
			 * area. */
			ospf_router_lsa_update_area(area);
		} else {
			rl = (struct router_lsa *)lsa->data;
			/* Refresh router-LSA, (not install) and flood through
			 * area. */
			if (rl->flags != ospf->flags)
				ospf_router_lsa_update_area(area);
		}
	}

	return 0;
}


/* network-LSA related functions. */
/* Originate Network-LSA. */
static void ospf_network_lsa_body_set(struct stream *s,
				      struct ospf_interface *oi)
{
	struct in_addr mask;
	struct route_node *rn;
	struct ospf_neighbor *nbr;

	if (OSPF_IF_PARAM(oi, prefix_suppression)) {
		mask.s_addr = 0xffffffff;
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug("LSA[Type2]: Interface %s network mask set to host mask due prefix-suppression",
				   oi->ifp->name);
	} else {
		masklen2ip(oi->address->prefixlen, &mask);
	}
	stream_put_ipv4(s, mask.s_addr);

	/* The network-LSA lists those routers that are fully adjacent to
	  the Designated Router; each fully adjacent router is identified by
	  its OSPF Router ID.  The Designated Router includes itself in this
	  list. RFC2328, Section 12.4.2 */

	for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
		if ((nbr = rn->info) != NULL)
			if (nbr->state == NSM_Full || nbr == oi->nbr_self)
				stream_put_ipv4(s, nbr->router_id.s_addr);
}

static struct ospf_lsa *ospf_network_lsa_new(struct ospf_interface *oi)
{
	struct stream *s;
	struct ospf_lsa *new;
	struct lsa_header *lsah;
	struct ospf_if_params *oip;
	int length;

	/* If there are no neighbours on this network (the net is stub),
	   the router does not originate network-LSA (see RFC 12.4.2) */
	if (oi->full_nbrs == 0)
		return NULL;

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
		zlog_debug("LSA[Type2]: Create network-LSA instance");

	/* Create new stream for LSA. */
	s = stream_new(OSPF_MAX_LSA_SIZE);
	lsah = (struct lsa_header *)STREAM_DATA(s);

	lsa_header_set(s, (OPTIONS(oi) | LSA_OPTIONS_GET(oi->area)),
		       OSPF_NETWORK_LSA, DR(oi), oi->ospf->router_id);

	/* Set network-LSA body fields. */
	ospf_network_lsa_body_set(s, oi);

	/* Set length. */
	length = stream_get_endp(s);
	lsah->length = htons(length);

	/* Create OSPF LSA instance. */
	new = ospf_lsa_new_and_data(length);

	new->area = oi->area;
	SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
	new->vrf_id = oi->ospf->vrf_id;

	/* Copy LSA to store. */
	memcpy(new->data, lsah, length);
	stream_free(s);

	/* Remember prior network LSA sequence numbers, even if we stop
	 * originating one for this oi, to try avoid re-originating LSAs with a
	 * prior sequence number, and thus speed up adjency forming &
	 * convergence.
	 */
	if ((oip = ospf_lookup_if_params(oi->ifp, oi->address->u.prefix4))) {
		new->data->ls_seqnum = oip->network_lsa_seqnum;
		new->data->ls_seqnum = lsa_seqnum_increment(new);
	} else {
		oip = ospf_get_if_params(oi->ifp, oi->address->u.prefix4);
		ospf_if_update_params(oi->ifp, oi->address->u.prefix4);
	}
	oip->network_lsa_seqnum = new->data->ls_seqnum;

	return new;
}

/* Originate network-LSA. */
void ospf_network_lsa_update(struct ospf_interface *oi)
{
	struct ospf_lsa *new;

	if (oi->area->ospf->gr_info.restart_in_progress) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug(
				"LSA[Type%d]: Graceful Restart in progress, don't originate",
				OSPF_NETWORK_LSA);
		return;
	}

	if (oi->network_lsa_self != NULL) {
		ospf_lsa_refresh(oi->ospf, oi->network_lsa_self);
		return;
	}

	/* Create new network-LSA instance. */
	new = ospf_network_lsa_new(oi);
	if (new == NULL)
		return;

	/* Install LSA to LSDB. */
	new = ospf_lsa_install(oi->ospf, oi, new);

	/* Update LSA origination count. */
	oi->ospf->lsa_originate_count++;

	/* Flooding new LSA through area. */
	ospf_flood_through_area(oi->area, NULL, new);

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
		zlog_debug("LSA[Type%d:%pI4]: Originate network-LSA %p",
			   new->data->type, &new->data->id,
			   (void *)new);
		ospf_lsa_header_dump(new->data);
	}

	return;
}

static struct ospf_lsa *ospf_network_lsa_refresh(struct ospf_lsa *lsa)
{
	struct ospf_area *area = lsa->area;
	struct ospf_lsa *new, *new2;
	struct ospf_if_params *oip;
	struct ospf_interface *oi;

	assert(lsa->data);

	/* Retrieve the oi for the network LSA */
	oi = ospf_if_lookup_by_local_addr(area->ospf, NULL, lsa->data->id);
	if (oi == NULL) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
			zlog_debug(
				"LSA[Type%d:%pI4]: network-LSA refresh: no oi found, ick, ignoring.",
				lsa->data->type, &lsa->data->id);
			ospf_lsa_header_dump(lsa->data);
		}
		return NULL;
	}

	if (oi->state != ISM_DR)
		return NULL;

	/* Delete LSA from neighbor retransmit-list. */
	ospf_ls_retransmit_delete_nbr_area(area, lsa);

	/* Unregister LSA from refresh-list */
	ospf_refresher_unregister_lsa(area->ospf, lsa);

	/* Create new network-LSA instance. */
	new = ospf_network_lsa_new(oi);
	if (new == NULL)
		return NULL;

	oip = ospf_lookup_if_params(oi->ifp, oi->address->u.prefix4);
	assert(oip != NULL);
	oip->network_lsa_seqnum = new->data->ls_seqnum =
		lsa_seqnum_increment(lsa);

	new2 = ospf_lsa_install(area->ospf, oi, new);

	assert(new2 == new);

	/* Flood LSA through aera. */
	ospf_flood_through_area(area, NULL, new);

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
		zlog_debug("LSA[Type%d:%pI4]: network-LSA refresh",
			   new->data->type, &new->data->id);
		ospf_lsa_header_dump(new->data);
	}

	return new;
}

static void stream_put_ospf_metric(struct stream *s, uint32_t metric_value)
{
	uint32_t metric;
	char *mp;

	/* Put 0 metric. TOS metric is not supported. */
	metric = htonl(metric_value);
	mp = (char *)&metric;
	mp++;
	stream_put(s, mp, 3);
}

/* summary-LSA related functions. */
static void ospf_summary_lsa_body_set(struct stream *s, struct prefix *p,
				      uint32_t metric)
{
	struct in_addr mask;

	masklen2ip(p->prefixlen, &mask);

	/* Put Network Mask. */
	stream_put_ipv4(s, mask.s_addr);

	/* Set # TOS. */
	stream_putc(s, (uint8_t)0);

	/* Set metric. */
	stream_put_ospf_metric(s, metric);
}

static struct ospf_lsa *ospf_summary_lsa_new(struct ospf_area *area,
					     struct prefix *p, uint32_t metric,
					     struct in_addr id)
{
	struct stream *s;
	struct ospf_lsa *new;
	struct lsa_header *lsah;
	int length;

	if (id.s_addr == 0xffffffff) {
		/* Maybe Link State ID not available. */
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug(
				"LSA[Type%d]: Link ID not available, can't originate",
				OSPF_SUMMARY_LSA);
		return NULL;
	}

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
		zlog_debug("LSA[Type3]: Create summary-LSA instance");

	/* Create new stream for LSA. */
	s = stream_new(OSPF_MAX_LSA_SIZE);
	lsah = (struct lsa_header *)STREAM_DATA(s);

	lsa_header_set(s, LSA_OPTIONS_GET(area), OSPF_SUMMARY_LSA, id,
		       area->ospf->router_id);

	/* Set summary-LSA body fields. */
	ospf_summary_lsa_body_set(s, p, metric);

	/* Set length. */
	length = stream_get_endp(s);
	lsah->length = htons(length);

	/* Create OSPF LSA instance. */
	new = ospf_lsa_new_and_data(length);
	new->area = area;
	SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
	new->vrf_id = area->ospf->vrf_id;

	/* Copy LSA to store. */
	memcpy(new->data, lsah, length);
	stream_free(s);

	return new;
}

/* Originate Summary-LSA. */
static struct ospf_lsa *
ospf_summary_lsa_prepare_and_flood(struct prefix_ipv4 *p, uint32_t metric,
				   struct ospf_area *area, struct in_addr id)
{
	struct ospf_lsa *new;

	/* Create new summary-LSA instance. */
	if (!(new = ospf_summary_lsa_new(area, (struct prefix *)p, metric, id)))
		return NULL;

	/* Instlal LSA to LSDB. */
	new = ospf_lsa_install(area->ospf, NULL, new);

	/* Update LSA origination count. */
	area->ospf->lsa_originate_count++;

	/* Flooding new LSA through area. */
	ospf_flood_through_area(area, NULL, new);

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
		zlog_debug("LSA[Type%d:%pI4]: Originate summary-LSA %p",
			   new->data->type, &new->data->id,
			   (void *)new);
		ospf_lsa_header_dump(new->data);
	}

	return new;
}

static struct ospf_lsa *ospf_handle_summarylsa_lsId_chg(struct ospf_area *area,
							struct prefix_ipv4 *p,
							uint8_t type,
							uint32_t metric,
							struct in_addr old_id)
{
	struct ospf_lsa *lsa = NULL;
	struct ospf_lsa *summary_lsa = NULL;
	struct summary_lsa *sl = NULL;
	struct ospf_area *old_area = NULL;
	struct ospf *ospf = area->ospf;
	struct prefix_ipv4 old_prefix;
	uint32_t old_metric;
	struct in_addr mask;
	uint32_t metric_val;
	char *metric_buf;

	lsa = ospf_lsdb_lookup_by_id(area->lsdb, type, p->prefix,
				     ospf->router_id);

	if (!lsa) {
		flog_warn(EC_OSPF_LSA_NULL, "(%s): LSA not found", __func__);
		return NULL;
	}

	sl = (struct summary_lsa *)lsa->data;

	old_area = lsa->area;
	old_metric = GET_METRIC(sl->metric);
	old_prefix.prefix = sl->header.id;
	old_prefix.prefixlen = ip_masklen(sl->mask);
	old_prefix.family = AF_INET;


	/* change the mask */
	masklen2ip(p->prefixlen, &mask);
	sl->mask.s_addr = mask.s_addr;

	/* Copy the metric*/
	metric_val = htonl(metric);
	metric_buf = (char *)&metric_val;
	memcpy(sl->metric, metric_buf, sizeof(metric_val));

	if (type == OSPF_SUMMARY_LSA) {
		/*Refresh the LSA with new LSA*/
		summary_lsa = ospf_summary_lsa_refresh(ospf, lsa);

		ospf_summary_lsa_prepare_and_flood(&old_prefix, old_metric,
						   old_area, old_id);
	} else {
		/*Refresh the LSA with new LSA*/
		summary_lsa = ospf_summary_asbr_lsa_refresh(ospf, lsa);

		ospf_asbr_summary_lsa_prepare_and_flood(&old_prefix, old_metric,
							old_area, old_id);
	}

	return summary_lsa;
}

/* Originate Summary-LSA. */
struct ospf_lsa *ospf_summary_lsa_originate(struct prefix_ipv4 *p,
					    uint32_t metric,
					    struct ospf_area *area)
{
	struct in_addr id;
	enum lsid_status status;
	struct ospf_lsa *new = NULL;

	status = ospf_lsa_unique_id(area->ospf, area->lsdb, OSPF_SUMMARY_LSA, p,
				    &id);

	if (status == LSID_CHANGE) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug("Link ID has to be changed.");

		new = ospf_handle_summarylsa_lsId_chg(area, p, OSPF_SUMMARY_LSA,
						      metric, id);
		return new;
	} else if (status == LSID_NOT_AVAILABLE) {
		/* Link State ID not available. */
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug(
				"LSA[Type5]: Link ID not available, can't originate");

		return NULL;
	}

	new = ospf_summary_lsa_prepare_and_flood(p, metric, area, id);
	return new;
}

static struct ospf_lsa *ospf_summary_lsa_refresh(struct ospf *ospf,
						 struct ospf_lsa *lsa)
{
	struct ospf_lsa *new;
	struct summary_lsa *sl;
	struct prefix p;

	/* Sanity check. */
	assert(lsa->data);

	sl = (struct summary_lsa *)lsa->data;
	p.prefixlen = ip_masklen(sl->mask);
	new = ospf_summary_lsa_new(lsa->area, &p, GET_METRIC(sl->metric),
				   sl->header.id);

	if (!new)
		return NULL;

	new->data->ls_seqnum = lsa_seqnum_increment(lsa);

	ospf_lsa_install(ospf, NULL, new);

	/* Flood LSA through AS. */
	ospf_flood_through_area(new->area, NULL, new);

	/* Debug logging. */
	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
		zlog_debug("LSA[Type%d:%pI4]: summary-LSA refresh",
			   new->data->type, &new->data->id);
		ospf_lsa_header_dump(new->data);
	}

	return new;
}


/* summary-ASBR-LSA related functions. */
static void ospf_summary_asbr_lsa_body_set(struct stream *s, struct prefix *p,
					   uint32_t metric)
{
	/* Put Network Mask. */
	stream_put_ipv4(s, (uint32_t)0);

	/* Set # TOS. */
	stream_putc(s, (uint8_t)0);

	/* Set metric. */
	stream_put_ospf_metric(s, metric);
}

static struct ospf_lsa *ospf_summary_asbr_lsa_new(struct ospf_area *area,
						  struct prefix *p,
						  uint32_t metric,
						  struct in_addr id)
{
	struct stream *s;
	struct ospf_lsa *new;
	struct lsa_header *lsah;
	int length;

	if (id.s_addr == 0xffffffff) {
		/* Maybe Link State ID not available. */
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug(
				"LSA[Type%d]: Link ID not available, can't originate",
				OSPF_ASBR_SUMMARY_LSA);
		return NULL;
	}

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
		zlog_debug("LSA[Type3]: Create summary-LSA instance");

	/* Create new stream for LSA. */
	s = stream_new(OSPF_MAX_LSA_SIZE);
	lsah = (struct lsa_header *)STREAM_DATA(s);

	lsa_header_set(s, LSA_OPTIONS_GET(area), OSPF_ASBR_SUMMARY_LSA, id,
		       area->ospf->router_id);

	/* Set summary-LSA body fields. */
	ospf_summary_asbr_lsa_body_set(s, p, metric);

	/* Set length. */
	length = stream_get_endp(s);
	lsah->length = htons(length);

	/* Create OSPF LSA instance. */
	new = ospf_lsa_new_and_data(length);
	new->area = area;
	SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
	new->vrf_id = area->ospf->vrf_id;

	/* Copy LSA to store. */
	memcpy(new->data, lsah, length);
	stream_free(s);

	return new;
}

/* Originate summary-ASBR-LSA. */
static struct ospf_lsa *
ospf_asbr_summary_lsa_prepare_and_flood(struct prefix_ipv4 *p, uint32_t metric,
					struct ospf_area *area,
					struct in_addr id)
{
	struct ospf_lsa *new;

	/* Create new summary-LSA instance. */
	new = ospf_summary_asbr_lsa_new(area, (struct prefix *)p, metric, id);
	if (!new)
		return NULL;

	/* Install LSA to LSDB. */
	new = ospf_lsa_install(area->ospf, NULL, new);

	/* Update LSA origination count. */
	area->ospf->lsa_originate_count++;

	/* Flooding new LSA through area. */
	ospf_flood_through_area(area, NULL, new);

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
		zlog_debug("LSA[Type%d:%pI4]: Originate summary-ASBR-LSA %p",
			   new->data->type, &new->data->id,
			   (void *)new);
		ospf_lsa_header_dump(new->data);
	}

	return new;
}

struct ospf_lsa *ospf_summary_asbr_lsa_originate(struct prefix_ipv4 *p,
						 uint32_t metric,
						 struct ospf_area *area)
{
	struct ospf_lsa *new;
	struct in_addr id;
	enum lsid_status status;

	status = ospf_lsa_unique_id(area->ospf, area->lsdb,
				    OSPF_ASBR_SUMMARY_LSA, p, &id);

	if (status == LSID_CHANGE) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug("Link ID has to be changed.");

		new = ospf_handle_summarylsa_lsId_chg(
			area, p, OSPF_ASBR_SUMMARY_LSA, metric, id);
		return new;
	} else if (status == LSID_NOT_AVAILABLE) {
		/* Link State ID not available. */
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug(
				"LSA[Type5]: Link ID not available, can't originate");

		return NULL;
	}

	new = ospf_asbr_summary_lsa_prepare_and_flood(p, metric, area, id);
	return new;
}

static struct ospf_lsa *ospf_summary_asbr_lsa_refresh(struct ospf *ospf,
						      struct ospf_lsa *lsa)
{
	struct ospf_lsa *new;
	struct summary_lsa *sl;
	struct prefix p;
	bool ind_lsa = false;

	/* Sanity check. */
	assert(lsa->data);

	if (lsa->area->fr_info.indication_lsa_self &&
	    (lsa->area->fr_info.indication_lsa_self == lsa))
		ind_lsa = true;

	sl = (struct summary_lsa *)lsa->data;
	p.prefixlen = ip_masklen(sl->mask);
	new = ospf_summary_asbr_lsa_new(lsa->area, &p, GET_METRIC(sl->metric),
					sl->header.id);
	if (!new)
		return NULL;

	new->data->ls_seqnum = lsa_seqnum_increment(lsa);

	ospf_lsa_install(ospf, NULL, new);

	/* Flood LSA through area. */
	ospf_flood_through_area(new->area, NULL, new);

	if (ind_lsa)
		new->area->fr_info.indication_lsa_self = new;

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
		zlog_debug("LSA[Type%d:%pI4]: summary-ASBR-LSA refresh",
			   new->data->type, &new->data->id);
		ospf_lsa_header_dump(new->data);
	}

	return new;
}

/* AS-external-LSA related functions. */

/* Get nexthop for AS-external-LSAs.  Return nexthop if its interface
   is connected, else 0*/
static struct in_addr ospf_external_lsa_nexthop_get(struct ospf *ospf,
						    struct in_addr nexthop)
{
	struct in_addr fwd;
	struct prefix nh;
	struct listnode *node;
	struct ospf_interface *oi;

	fwd.s_addr = 0;

	if (!nexthop.s_addr)
		return fwd;

	/* Check whether nexthop is covered by OSPF network. */
	nh.family = AF_INET;
	nh.u.prefix4 = nexthop;
	nh.prefixlen = IPV4_MAX_BITLEN;

	/* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
	 * better to make use of the per-ifp table of ois.
	 */
	for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
		if (if_is_operative(oi->ifp))
			if (oi->address->family == AF_INET)
				if (prefix_match(oi->address, &nh))
					return nexthop;

	return fwd;
}

/* NSSA-external-LSA related functions. */

/* Get 1st IP connection for Forward Addr */

struct in_addr ospf_get_ip_from_ifp(struct ospf_interface *oi)
{
	struct in_addr fwd;

	fwd.s_addr = INADDR_ANY;

	if (if_is_operative(oi->ifp))
		return oi->address->u.prefix4;

	return fwd;
}

/* Get 1st IP connection for Forward Addr */
struct in_addr ospf_get_nssa_ip(struct ospf_area *area)
{
	struct in_addr fwd;
	struct in_addr best_default;
	struct listnode *node;
	struct ospf_interface *oi;

	fwd.s_addr = 0;
	best_default.s_addr = 0;

	for (ALL_LIST_ELEMENTS_RO(area->ospf->oiflist, node, oi)) {
		if (if_is_operative(oi->ifp))
			if (oi->area->external_routing == OSPF_AREA_NSSA)
				if (oi->address
				    && oi->address->family == AF_INET) {
					if (best_default.s_addr == INADDR_ANY)
						best_default =
							oi->address->u.prefix4;
					if (oi->area == area)
						return oi->address->u.prefix4;
				}
	}
	if (best_default.s_addr != INADDR_ANY)
		return best_default;

	return fwd;
}

int metric_type(struct ospf *ospf, uint8_t src, unsigned short instance)
{
	struct ospf_redist *red;

	red = ospf_redist_lookup(ospf, src, instance);

	return ((!red || red->dmetric.type < 0) ? DEFAULT_METRIC_TYPE
						: red->dmetric.type);
}

int metric_value(struct ospf *ospf, uint8_t src, unsigned short instance)
{
	struct ospf_redist *red;

	red = ospf_redist_lookup(ospf, src, instance);
	if (!red || red->dmetric.value < 0) {
		if (src == DEFAULT_ROUTE) {
			if (ospf->default_originate == DEFAULT_ORIGINATE_ZEBRA)
				return DEFAULT_DEFAULT_ORIGINATE_METRIC;
			else
				return DEFAULT_DEFAULT_ALWAYS_METRIC;
		} else if (ospf->default_metric < 0)
			return DEFAULT_DEFAULT_METRIC;
		else
			return ospf->default_metric;
	}

	return red->dmetric.value;
}

/* Set AS-external-LSA body. */
static void ospf_external_lsa_body_set(struct stream *s,
				       struct external_info *ei,
				       struct ospf *ospf)
{
	struct prefix_ipv4 *p = &ei->p;
	struct in_addr mask, fwd_addr;
	uint32_t mvalue;
	int mtype;
	int type;
	unsigned short instance;

	/* Put Network Mask. */
	masklen2ip(p->prefixlen, &mask);
	stream_put_ipv4(s, mask.s_addr);

	/* If prefix is default, specify DEFAULT_ROUTE. */
	type = is_default_prefix4(&ei->p) ? DEFAULT_ROUTE : ei->type;
	instance = is_default_prefix4(&ei->p) ? 0 : ei->instance;

	mtype = (ROUTEMAP_METRIC_TYPE(ei) != -1)
			? ROUTEMAP_METRIC_TYPE(ei)
			: metric_type(ospf, type, instance);

	mvalue = (ROUTEMAP_METRIC(ei) != -1)
			 ? ROUTEMAP_METRIC(ei)
			 : metric_value(ospf, type, instance);

	/* Put type of external metric. */
	stream_putc(s, (mtype == EXTERNAL_METRIC_TYPE_2 ? 0x80 : 0));

	/* Put 0 metric. TOS metric is not supported. */
	stream_put_ospf_metric(s, mvalue);

	/* Get forwarding address to nexthop if on the Connection List, else 0.
	 */
	fwd_addr = ospf_external_lsa_nexthop_get(ospf, ei->nexthop);

	/* Put forwarding address. */
	stream_put_ipv4(s, fwd_addr.s_addr);

	/* Put route tag */
	stream_putl(s, ei->tag);
}

/* Create new external-LSA. */
static struct ospf_lsa *
ospf_exnl_lsa_prepare_and_flood(struct ospf *ospf, struct external_info *ei,
				struct in_addr id)
{
	struct stream *s;
	struct lsa_header *lsah;
	struct ospf_lsa *new;
	int length;

	/* Create new stream for LSA. */
	s = stream_new(OSPF_MAX_LSA_SIZE);
	lsah = (struct lsa_header *)STREAM_DATA(s);

	/* Set LSA common header fields. */
	lsa_header_set(s, OSPF_OPTION_E, OSPF_AS_EXTERNAL_LSA, id,
		       ospf->router_id);

	/* Set AS-external-LSA body fields. */
	ospf_external_lsa_body_set(s, ei, ospf);

	/* Set length. */
	length = stream_get_endp(s);
	lsah->length = htons(length);

	/* Now, create OSPF LSA instance. */
	new = ospf_lsa_new_and_data(length);
	new->area = NULL;
	SET_FLAG(new->flags,
		 OSPF_LSA_SELF | OSPF_LSA_APPROVED | OSPF_LSA_SELF_CHECKED);
	new->vrf_id = ospf->vrf_id;

	/* Copy LSA data to store, discard stream. */
	memcpy(new->data, lsah, length);
	stream_free(s);

	return new;
}

static struct ospf_lsa *ospf_handle_exnl_lsa_lsId_chg(struct ospf *ospf,
						      struct external_info *ei,
						      struct in_addr id)
{
	struct ospf_lsa *lsa;
	struct as_external_lsa *al;
	struct in_addr mask;
	struct ospf_lsa *new;
	struct external_info ei_summary = {};
	struct external_info *ei_old;

	lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
				     ei->p.prefix, ospf->router_id);

	if (!lsa) {
		flog_warn(EC_OSPF_LSA_NULL, "(%s): LSA not found", __func__);
		return NULL;
	}

	ei_old = ospf_external_info_check(ospf, lsa);

	al = (struct as_external_lsa *)lsa->data;

	if (!ei_old) {
		/* eii_old pointer of LSA is NULL, this
		 * must be external aggregate route.
		 */
		ei_summary.p.family = AF_INET;
		ei_summary.p.prefix = al->header.id;
		ei_summary.p.prefixlen = ip_masklen(al->mask);
		ei_summary.tag = (unsigned long)ntohl(al->e[0].route_tag);
		ei_old = &ei_summary;
	}

	/* change the mask */
	masklen2ip(ei->p.prefixlen, &mask);
	al->mask.s_addr = mask.s_addr;

	/*Refresh the LSA with new LSA*/
	ospf_external_lsa_refresh(ospf, lsa, ei, LSA_REFRESH_FORCE, 0);

	/*Originate the old LSA with changed LSID*/
	new = ospf_exnl_lsa_prepare_and_flood(ospf, ei_old, id);

	return new;
}

static struct ospf_lsa *ospf_external_lsa_new(struct ospf *ospf,
					      struct external_info *ei,
					      struct in_addr *old_id)
{
	struct ospf_lsa *new;
	struct in_addr id;
	enum lsid_status status;

	if (ei == NULL) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug(
				"LSA[Type5]: External info is NULL, can't originate");
		return NULL;
	}

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
		zlog_debug("LSA[Type5]: Originate AS-external-LSA instance");

	/* If old Link State ID is specified, refresh LSA with same ID. */
	if (old_id)
		id = *old_id;
	/* Get Link State with unique ID. */
	else {
		status = ospf_lsa_unique_id(ospf, ospf->lsdb,
					    OSPF_AS_EXTERNAL_LSA, &ei->p, &id);

		if (status == LSID_CHANGE) {
			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
				zlog_debug("Link ID has to be changed.");

			new = ospf_handle_exnl_lsa_lsId_chg(ospf, ei, id);
			return new;
		} else if (status == LSID_NOT_AVAILABLE) {
			/* Link State ID not available. */
			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
				zlog_debug(
					"LSA[Type5]: Link ID not available, can't originate");
			return NULL;
		}
	}

	new = ospf_exnl_lsa_prepare_and_flood(ospf, ei, id);

	return new;
}

/* As Type-7 */
static void ospf_install_flood_nssa(struct ospf *ospf, struct ospf_lsa *lsa)
{
	struct ospf_lsa *new;
	struct as_external_lsa *extlsa;
	struct ospf_area *area;
	struct listnode *node, *nnode;

	/* LSA may be a Type-5 originated via translation of a Type-7 LSA
	 * which originated from an NSSA area. In which case it should not be
	 * flooded back to NSSA areas.
	 */
	if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
		return;

	/* NSSA Originate or Refresh (If anyNSSA)

	LSA is self-originated. And just installed as Type-5.
	Additionally, install as Type-7 LSDB for every attached NSSA.

	P-Bit controls which ABR performs translation to outside world; If
	we are an ABR....do not set the P-bit, because we send the Type-5,
	not as the ABR Translator, but as the ASBR owner within the AS!

	If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set.  The
	elected ABR Translator will see the P-bit, Translate, and re-flood.

	Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
	Type-5's to non-NSSA Areas.  (it will also attempt a re-install) */

	for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
		/* Don't install Type-7 LSA's into nonNSSA area */
		if (area->external_routing != OSPF_AREA_NSSA)
			continue;

		/* make lsa duplicate, lock=1 */
		new = ospf_lsa_dup(lsa);
		new->area = area;
		new->data->type = OSPF_AS_NSSA_LSA;

		/* set P-bit if not ABR */
		if (!IS_OSPF_ABR(ospf)) {
			SET_FLAG(new->data->options, OSPF_OPTION_NP);

			/* set non-zero FWD ADDR

			draft-ietf-ospf-nssa-update-09.txt

			if the network between the NSSA AS boundary router and
			the
			adjacent AS is advertised into OSPF as an internal OSPF
			route,
			the forwarding address should be the next op address as
			is cu
			currently done with type-5 LSAs.  If the intervening
			network is
			not adversited into OSPF as an internal OSPF route and
			the
			type-7 LSA's P-bit is set a forwarding address should be
			selected from one of the router's active OSPF interface
			addresses
			which belong to the NSSA.  If no such addresses exist,
			then
			no type-7 LSA's with the P-bit set should originate from
			this
			router.   */

			/* kevinm: not updating lsa anymore, just new */
			extlsa = (struct as_external_lsa *)(new->data);

			if (extlsa->e[0].fwd_addr.s_addr == INADDR_ANY)
				extlsa->e[0].fwd_addr = ospf_get_nssa_ip(
					area); /* this NSSA area in ifp */

			if (extlsa->e[0].fwd_addr.s_addr == INADDR_ANY) {
				if (IS_DEBUG_OSPF_NSSA)
					zlog_debug(
						"LSA[Type-7]: Could not build FWD-ADDR");
				ospf_lsa_discard(new);
				return;
			}
		}

		/* install also as Type-7 */
		ospf_lsa_install(ospf, NULL,
				 new); /* Remove Old, Lock New = 2 */

		/* will send each copy, lock=2+n */
		ospf_flood_through_as(
			ospf, NULL, new); /* all attached NSSA's, no AS/STUBs */
	}
}

static struct ospf_lsa *ospf_lsa_translated_nssa_new(struct ospf *ospf,
						     struct ospf_lsa *type7)
{

	struct ospf_lsa *new;
	struct as_external_lsa *ext, *extnew;
	struct external_info ei;

	ext = (struct as_external_lsa *)(type7->data);

	/* need external_info struct, fill in bare minimum */
	ei.p.family = AF_INET;
	ei.p.prefix = type7->data->id;
	ei.p.prefixlen = ip_masklen(ext->mask);
	ei.type = ZEBRA_ROUTE_OSPF;
	ei.nexthop = ext->header.adv_router;
	ei.route_map_set.metric = -1;
	ei.route_map_set.metric_type = -1;
	ei.metric = DEFAULT_DEFAULT_METRIC;
	ei.max_metric = OSPF_LS_INFINITY;
	ei.min_metric = 0;
	ei.tag = 0;
	ei.instance = 0;

	if ((new = ospf_external_lsa_new(ospf, &ei, &type7->data->id))
	    == NULL) {
		if (IS_DEBUG_OSPF_NSSA)
			zlog_debug(
				"%s: Could not originate Translated Type-5 for %pI4",
				__func__, &ei.p.prefix);
		return NULL;
	}

	extnew = (struct as_external_lsa *)(new->data);

	/* copy over Type-7 data to new */
	extnew->e[0].tos = ext->e[0].tos;
	extnew->e[0].route_tag = ext->e[0].route_tag;
	if (type7->area->suppress_fa) {
		extnew->e[0].fwd_addr.s_addr = 0;
		if (IS_DEBUG_OSPF_NSSA)
			zlog_debug("%s: Suppress forwarding address for %pI4",
				   __func__, &ei.p.prefix);
	} else
		extnew->e[0].fwd_addr.s_addr = ext->e[0].fwd_addr.s_addr;
	new->data->ls_seqnum = type7->data->ls_seqnum;

	/* add translated flag, checksum and lock new lsa */
	SET_FLAG(new->flags, OSPF_LSA_LOCAL_XLT); /* Translated from 7  */

	return new;
}

/* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
struct ospf_lsa *ospf_translated_nssa_originate(struct ospf *ospf,
						struct ospf_lsa *type7,
						struct ospf_lsa *type5)
{
	struct ospf_lsa *new, *translated_lsa;
	struct as_external_lsa *extnew;

	if (ospf->gr_info.restart_in_progress) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug(
				"LSA[Translated Type5]: Graceful Restart in progress, don't originate");
		return NULL;
	}

	/* we cant use ospf_external_lsa_originate() as we need to set
	 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
	 */

	if ((translated_lsa = ospf_lsa_translated_nssa_new(ospf, type7)) ==
	    NULL) {
		if (IS_DEBUG_OSPF_NSSA)
			zlog_debug(
				"%s: Could not translate Type-7, Id %pI4, to Type-5",
				__func__, &type7->data->id);
		return NULL;
	}

	extnew = (struct as_external_lsa *)translated_lsa->data;

	/* Update LSA sequence number from translated Type-5 LSA */
	if (type5)
		translated_lsa->data->ls_seqnum = lsa_seqnum_increment(type5);

	if ((new = ospf_lsa_install(ospf, NULL, translated_lsa)) == NULL) {
		flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
			  "%s: Could not install LSA id %pI4", __func__,
			  &type7->data->id);
		ospf_lsa_free(translated_lsa);
		return NULL;
	}

	if (IS_DEBUG_OSPF_NSSA) {
		zlog_debug("%s: translated Type 7, installed", __func__);
		ospf_lsa_header_dump(new->data);
		zlog_debug("   Network mask: %d", ip_masklen(extnew->mask));
		zlog_debug("   Forward addr: %pI4",
			   &extnew->e[0].fwd_addr);
	}

	ospf->lsa_originate_count++;
	ospf_flood_through_as(ospf, NULL, new);

	return new;
}

/* Refresh Translated from NSSA AS-external-LSA. */
struct ospf_lsa *ospf_translated_nssa_refresh(struct ospf *ospf,
					      struct ospf_lsa *type7,
					      struct ospf_lsa *type5)
{
	struct ospf_lsa *new = NULL, *translated_lsa = NULL;
	struct as_external_lsa *extold = NULL;
	uint32_t ls_seqnum = 0;

	/* Sanity checks. */
	assert(type7 || type5);
	if (!(type7 || type5))
		return NULL;
	if (type7)
		assert(type7->data);
	if (type5)
		assert(type5->data);
	assert(ospf->anyNSSA);

	/* get required data according to what has been given */
	if (type7 && type5 == NULL) {
		/* find the translated Type-5 for this Type-7 */
		struct as_external_lsa *ext =
			(struct as_external_lsa *)(type7->data);
		struct prefix_ipv4 p = {
			.prefix = type7->data->id,
			.prefixlen = ip_masklen(ext->mask),
			.family = AF_INET,
		};

		type5 = ospf_external_info_find_lsa(ospf, &p);
	} else if (type5 && type7 == NULL) {
		/* find the type-7 from which supplied type-5 was translated,
		 * ie find first type-7 with same LSA Id.
		 */
		struct listnode *ln, *lnn;
		struct route_node *rn;
		struct ospf_lsa *lsa;
		struct ospf_area *area;

		for (ALL_LIST_ELEMENTS(ospf->areas, ln, lnn, area)) {
			if (area->external_routing != OSPF_AREA_NSSA && !type7)
				continue;

			LSDB_LOOP (NSSA_LSDB(area), rn, lsa) {
				if (lsa->data->id.s_addr
				    == type5->data->id.s_addr) {
					type7 = lsa;
					break;
				}
			}
		}
	}

	/* do we have type7? */
	if (!type7) {
		if (IS_DEBUG_OSPF_NSSA)
			zlog_debug("%s: no Type-7 found for Type-5 LSA Id %pI4",
				   __func__, &type5->data->id);
		return NULL;
	}

	/* do we have valid translated type5? */
	if (type5 == NULL || !CHECK_FLAG(type5->flags, OSPF_LSA_LOCAL_XLT)) {
		if (IS_DEBUG_OSPF_NSSA)
			zlog_debug(
				"%s: No translated Type-5 found for Type-7 with Id %pI4",
				__func__, &type7->data->id);
		return NULL;
	}

	extold = (struct as_external_lsa *)type5->data;
	if (type7->area->suppress_fa == 1) {
		if (extold->e[0].fwd_addr.s_addr == 0)
			ls_seqnum = ntohl(type5->data->ls_seqnum);
	}

	/* Delete LSA from neighbor retransmit-list. */
	ospf_ls_retransmit_delete_nbr_as(ospf, type5);

	/* create new translated LSA */
	if ((translated_lsa = ospf_lsa_translated_nssa_new(ospf, type7)) ==
	    NULL) {
		if (IS_DEBUG_OSPF_NSSA)
			zlog_debug(
				"%s: Could not translate Type-7 for %pI4 to Type-5",
				__func__, &type7->data->id);
		return NULL;
	}

	if (type7->area->suppress_fa == 1) {
		if (extold->e[0].fwd_addr.s_addr == 0)
			translated_lsa->data->ls_seqnum = htonl(ls_seqnum + 1);
	}

	if (!(new = ospf_lsa_install(ospf, NULL, translated_lsa))) {
		flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
			  "%s: Could not install translated LSA, Id %pI4",
			  __func__, &type7->data->id);
		ospf_lsa_free(translated_lsa);
		return NULL;
	}

	/* Flood LSA through area. */
	ospf_flood_through_as(ospf, NULL, new);

	return new;
}

/* Originate an AS-external-LSA, install and flood. */
struct ospf_lsa *ospf_external_lsa_originate(struct ospf *ospf,
					     struct external_info *ei)
{
	struct ospf_lsa *new;

	if (ospf->gr_info.restart_in_progress) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug(
				"LSA[Type5]: Graceful Restart in progress, don't originate");
		return NULL;
	}

	/* Added for NSSA project....

	     External LSAs are originated in ASBRs as usual, but for NSSA
	   systems.
	   there is the global Type-5 LSDB and a Type-7 LSDB installed for
	   every area.  The Type-7's are flooded to every IR and every ABR; We
	   install the Type-5 LSDB so that the normal "refresh" code operates
	   as usual, and flag them as not used during ASE calculations.  The
	   Type-7 LSDB is used for calculations.  Each Type-7 has a Forwarding
	   Address of non-zero.

	   If an ABR is the elected NSSA translator, following SPF and during
	   the ABR task it will translate all the scanned Type-7's, with P-bit
	   ON and not-self generated, and translate to Type-5's throughout the
	   non-NSSA/STUB AS.

	   A difference in operation depends whether this ASBR is an ABR
	   or not.  If not an ABR, the P-bit is ON, to indicate that any
	   elected NSSA-ABR can perform its translation.

	   If an ABR, the P-bit is OFF;  No ABR will perform translation and
	   this ASBR will flood the Type-5 LSA as usual.

	   For the case where this ASBR is not an ABR, the ASE calculations
	   are based on the Type-5 LSDB;  The Type-7 LSDB exists just to
	   demonstrate to the user that there are LSA's that belong to any
	   attached NSSA.

	   Finally, it just so happens that when the ABR is translating every
	   Type-7 into Type-5, it installs it into the Type-5 LSDB as an
	   approved Type-5 (translated from Type-7);  at the end of translation
	   if any Translated Type-5's remain unapproved, then they must be
	   flushed from the AS.

	   */

	if (ospf->router_id.s_addr == INADDR_ANY) {
		if (ei && IS_DEBUG_OSPF_EVENT)
			zlog_debug(
				"LSA[Type5:%pI4]: deferring AS-external-LSA origination, router ID is zero",
				&ei->p.prefix);
		return NULL;
	}

	/* Create new AS-external-LSA instance. */
	if ((new = ospf_external_lsa_new(ospf, ei, NULL)) == NULL) {
		if (ei && IS_DEBUG_OSPF_EVENT)
			zlog_debug(
				"LSA[Type5:%pI4]: Could not originate AS-external-LSA",
				&ei->p.prefix);
		return NULL;
	}

	/* Install newly created LSA into Type-5 LSDB, lock = 1. */
	ospf_lsa_install(ospf, NULL, new);

	/* Update LSA origination count. */
	ospf->lsa_originate_count++;

	/* Flooding new LSA. only to AS (non-NSSA/STUB) */
	ospf_flood_through_as(ospf, NULL, new);

	/* If there is any attached NSSA, do special handling */
	if (ospf->anyNSSA &&
	    /* stay away from translated LSAs! */
	    !(CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT)))
		ospf_install_flood_nssa(
			ospf, new); /* Install/Flood Type-7 to all NSSAs */

	/* Debug logging. */
	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
		zlog_debug("LSA[Type%d:%pI4]: Originate AS-external-LSA %p",
			   new->data->type, &new->data->id,
			   (void *)new);
		ospf_lsa_header_dump(new->data);
	}

	return new;
}

/* Originate an NSSA-LSA, install and flood. */
struct ospf_lsa *ospf_nssa_lsa_originate(struct ospf_area *area,
					 struct external_info *ei)
{
	struct ospf *ospf = area->ospf;
	struct ospf_lsa *new;

	if (ospf->gr_info.restart_in_progress) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug(
				"LSA[Type7]: Graceful Restart in progress, don't originate");
		return NULL;
	}

	if (ospf->router_id.s_addr == INADDR_ANY) {
		if (IS_DEBUG_OSPF_EVENT)
			zlog_debug(
				"LSA[Type7:%pI4]: deferring NSSA-LSA origination, router ID is zero",
				&ei->p.prefix);
		return NULL;
	}

	/* Create new NSSA-LSA instance. */
	if ((new = ospf_external_lsa_new(ospf, ei, NULL)) == NULL) {
		if (IS_DEBUG_OSPF_EVENT)
			zlog_debug(
				"LSA[Type7:%pI4]: Could not originate NSSA-LSA",
				&ei->p.prefix);
		return NULL;
	}
	new->data->type = OSPF_AS_NSSA_LSA;
	new->area = area;

	/* Install newly created LSA into Type-7 LSDB. */
	ospf_lsa_install(ospf, NULL, new);

	/* Update LSA origination count. */
	ospf->lsa_originate_count++;

	/* Flooding new LSA */
	ospf_flood_through_area(area, NULL, new);

	/* Debug logging. */
	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
		zlog_debug("LSA[Type%d:%pI4]: Originate NSSA-LSA %p",
			   new->data->type, &new->data->id, (void *)new);
		ospf_lsa_header_dump(new->data);
	}

	return new;
}

/* Refresh NSSA-LSA. */
struct ospf_lsa *ospf_nssa_lsa_refresh(struct ospf_area *area,
				       struct ospf_lsa *lsa,
				       struct external_info *ei)
{
	struct ospf *ospf = area->ospf;
	struct ospf_lsa *new;

	/* Delete LSA from neighbor retransmit-list. */
	ospf_ls_retransmit_delete_nbr_as(ospf, lsa);

	/* Unregister AS-external-LSA from refresh-list. */
	ospf_refresher_unregister_lsa(ospf, lsa);

	/* Create new NSSA-LSA instance. */
	if ((new = ospf_external_lsa_new(ospf, ei, NULL)) == NULL) {
		if (IS_DEBUG_OSPF_EVENT)
			zlog_debug(
				"LSA[Type7:%pI4]: Could not originate NSSA-LSA",
				&ei->p.prefix);
		return NULL;
	}
	new->data->type = OSPF_AS_NSSA_LSA;
	new->data->ls_seqnum = lsa_seqnum_increment(lsa);
	new->area = area;

	/* Install newly created LSA into Type-7 LSDB. */
	ospf_lsa_install(ospf, NULL, new);

	/* Flooding new LSA */
	ospf_flood_through_area(area, NULL, new);

	/* Debug logging. */
	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
		zlog_debug("LSA[Type%d:%pI4]: NSSA-LSA refresh",
			   new->data->type, &new->data->id);
		ospf_lsa_header_dump(new->data);
	}

	return new;
}

static struct external_info *ospf_default_external_info(struct ospf *ospf)
{
	int type;
	struct prefix_ipv4 p;
	struct external_info *default_ei;
	int ret = 0;

	p.family = AF_INET;
	p.prefix.s_addr = 0;
	p.prefixlen = 0;

	default_ei = ospf_external_info_lookup(ospf, DEFAULT_ROUTE, 0, &p);
	if (!default_ei)
		return NULL;

	/* First, lookup redistributed default route. */
	for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
		struct list *ext_list;

		if (type == ZEBRA_ROUTE_OSPF)
			continue;

		ext_list = ospf->external[type];
		if (!ext_list)
			continue;

		ret = ospf_external_default_routemap_apply_walk(ospf, ext_list,
								default_ei);
		if (ret)
			return default_ei;
	}

	return NULL;
}

void ospf_external_lsa_rid_change(struct ospf *ospf)
{
	struct external_info *ei;
	struct ospf_external_aggr_rt *aggr;
	struct ospf_lsa *lsa = NULL;
	int force;
	int type;

	for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
		struct route_node *rn;
		struct route_table *rt;
		struct list *ext_list;
		struct listnode *node;
		struct ospf_external *ext;

		ext_list = ospf->external[type];
		if (!ext_list)
			continue;

		for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
			/* Originate As-external-LSA from all type of
			 * distribute source.
			 */
			rt = ext->external_info;
			if (!rt)
				continue;

			for (rn = route_top(rt); rn; rn = route_next(rn)) {
				ei = rn->info;

				if (!ei)
					continue;

				if (is_default_prefix4(&ei->p))
					continue;

				lsa = ospf_external_info_find_lsa(ospf, &ei->p);

				aggr = ospf_external_aggr_match(ospf, &ei->p);
				if (aggr) {

					if (!ospf_redistribute_check(ospf, ei,
								     NULL))
						continue;

					if (IS_DEBUG_OSPF(lsa, EXTNL_LSA_AGGR))
						zlog_debug(
							"Originate Summary LSA after reset/router-ID change");

					/* Here the LSA is originated as new */
					ospf_originate_summary_lsa(ospf, aggr,
								   ei);
				} else if (lsa) {
					/* LSA needs to be refreshed even if
					 * there is no change in the route
					 * params if the LSA is in maxage.
					 */
					if (IS_LSA_MAXAGE(lsa))
						force = LSA_REFRESH_FORCE;
					else
						force = LSA_REFRESH_IF_CHANGED;

					ospf_external_lsa_refresh(ospf, lsa,
								ei, force, 0);
				} else {
					if (!ospf_redistribute_check(ospf, ei,
								     NULL))
						continue;

					if (!ospf_external_lsa_originate(ospf,
									 ei))
						flog_warn(
							EC_OSPF_LSA_INSTALL_FAILURE,
							"LSA: AS-external-LSA was not originated.");
				}
			}
		}
	}

	ei = ospf_default_external_info(ospf);
	if (ei && !ospf_external_lsa_originate(ospf, ei)) {
		flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
			"LSA: AS-external-LSA for default route was not originated.");
	}
}

/* Flush any NSSA LSAs for given prefix */
void ospf_nssa_lsa_flush(struct ospf *ospf, struct prefix_ipv4 *p)
{
	struct listnode *node, *nnode;
	struct ospf_lsa *lsa = NULL;
	struct ospf_area *area;

	for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
		if (area->external_routing == OSPF_AREA_NSSA) {
			lsa = ospf_lsa_lookup(ospf, area, OSPF_AS_NSSA_LSA,
					      p->prefix, ospf->router_id);
			if (!lsa) {
				if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
					zlog_debug(
						"LSA: There is no such AS-NSSA-LSA %pFX in LSDB",
						p);
				continue;
			}
			ospf_ls_retransmit_delete_nbr_area(area, lsa);
			if (!IS_LSA_MAXAGE(lsa)) {
				ospf_refresher_unregister_lsa(ospf, lsa);
				ospf_lsa_flush_area(lsa, area);
			}
		}
	}
}

/* Flush an AS-external-LSA from LSDB and routing domain. */
void ospf_external_lsa_flush(struct ospf *ospf, uint8_t type,
			     struct prefix_ipv4 *p,
			     ifindex_t ifindex /*, struct in_addr nexthop */)
{
	struct ospf_lsa *lsa;

	if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
		zlog_debug("LSA: Flushing AS-external-LSA %pFX", p);

	/* First lookup LSA from LSDB. */
	if (!(lsa = ospf_external_info_find_lsa(ospf, p))) {
		if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
			zlog_debug(
				"LSA: There is no such AS-external-LSA %pFX in LSDB",
				p);
		return;
	}

	/* If LSA is selforiginated, not a translated LSA, and there is
	 * NSSA area, flush Type-7 LSA's at first.
	 */
	if (IS_LSA_SELF(lsa) && (ospf->anyNSSA)
	    && !(CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT)))
		ospf_nssa_lsa_flush(ospf, p);

	if (!IS_LSA_MAXAGE(lsa)) {
		/* Sweep LSA from Link State Retransmit List. */
		ospf_ls_retransmit_delete_nbr_as(ospf, lsa);

		/* Unregister LSA from Refresh queue. */
		ospf_refresher_unregister_lsa(ospf, lsa);

		/* Flush AS-external-LSA through AS. */
		ospf_lsa_flush_as(ospf, lsa);
	}

	if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
		zlog_debug("%s: stop", __func__);
}

void ospf_external_lsa_refresh_default(struct ospf *ospf)
{
	struct prefix_ipv4 p;
	struct external_info *ei;
	struct ospf_lsa *lsa;

	p.family = AF_INET;
	p.prefixlen = 0;
	p.prefix.s_addr = INADDR_ANY;

	ei = ospf_default_external_info(ospf);
	lsa = ospf_external_info_find_lsa(ospf, &p);

	if (ei && lsa) {
		if (IS_DEBUG_OSPF_EVENT)
			zlog_debug("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
				(void *)lsa);
		ospf_external_lsa_refresh(ospf, lsa, ei, LSA_REFRESH_FORCE,
					  false);
	} else if (ei && !lsa) {
		if (IS_DEBUG_OSPF_EVENT)
			zlog_debug(
				"LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
		ospf_external_lsa_originate(ospf, ei);
	} else if (lsa) {
		if (IS_DEBUG_OSPF_EVENT)
			zlog_debug("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
		ospf_external_lsa_flush(ospf, DEFAULT_ROUTE, &p, 0);
	}
}

void ospf_external_lsa_refresh_type(struct ospf *ospf, uint8_t type,
				    unsigned short instance, int force)
{
	struct route_node *rn;
	struct external_info *ei;
	struct ospf_external *ext;

	if (type == DEFAULT_ROUTE)
		return;

	ext = ospf_external_lookup(ospf, type, instance);

	if (ext && EXTERNAL_INFO(ext)) {
		/* Refresh each redistributed AS-external-LSAs. */
		for (rn = route_top(EXTERNAL_INFO(ext)); rn;
		     rn = route_next(rn)) {
			ei = rn->info;
			if (ei) {
				if (!is_default_prefix4(&ei->p)) {
					struct ospf_lsa *lsa;
					struct ospf_external_aggr_rt *aggr;

					aggr = ospf_external_aggr_match(ospf,
								&ei->p);
					lsa = ospf_external_info_find_lsa(
								ospf, &ei->p);
					if (aggr) {
						/* Check the AS-external-LSA
						 * should be originated.
						 */
						if (!ospf_redistribute_check(
							    ospf, ei, NULL)) {

							ospf_unlink_ei_from_aggr(
								ospf, aggr, ei);
							continue;
						}

						if (IS_DEBUG_OSPF(
							    lsa,
							    EXTNL_LSA_AGGR))
							zlog_debug(
								"%s: Send Aggreate LSA (%pFX)",
								__func__,
								&aggr->p);

						ospf_originate_summary_lsa(
							ospf, aggr, ei);

					} else if (lsa) {

						if (IS_LSA_MAXAGE(lsa))
							force = LSA_REFRESH_FORCE;

						ospf_external_lsa_refresh(
							ospf, lsa, ei, force,
							false);
					} else {
						if (!ospf_redistribute_check(
							    ospf, ei, NULL))
							continue;
						ospf_external_lsa_originate(
							ospf, ei);
					}
				}
			}
		}
	}
}

/* Refresh AS-external-LSA. */
struct ospf_lsa *ospf_external_lsa_refresh(struct ospf *ospf,
					   struct ospf_lsa *lsa,
					   struct external_info *ei, int force,
					   bool is_aggr)
{
	struct ospf_lsa *new;
	int changed = 0;

	/* Check the AS-external-LSA should be originated. */
	if (!is_aggr)
		if (!ospf_redistribute_check(ospf, ei, &changed)) {
			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
				zlog_debug(
					"LSA[Type%d:%pI4] Could not be refreshed, redist check fail",
					lsa->data->type,
					&lsa->data->id);

			ospf_external_lsa_flush(ospf, ei->type, &ei->p,
						ei->ifindex /*, ei->nexthop */);
			return NULL;
		}

	if (!changed && !force) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug(
				"LSA[Type%d:%pI4]: Not refreshed, not changed/forced",
				lsa->data->type, &lsa->data->id);
		return NULL;
	}

	/* Delete LSA from neighbor retransmit-list. */
	ospf_ls_retransmit_delete_nbr_as(ospf, lsa);

	/* Unregister AS-external-LSA from refresh-list. */
	ospf_refresher_unregister_lsa(ospf, lsa);

	new = ospf_external_lsa_new(ospf, ei, &lsa->data->id);

	if (new == NULL) {
		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
			zlog_debug("LSA[Type%d:%pI4]: Could not be refreshed",
				   lsa->data->type, &lsa->data->id);
		return NULL;
	}

	new->data->ls_seqnum = lsa_seqnum_increment(lsa);

	ospf_lsa_install(ospf, NULL, new); /* As type-5. */

	/* Flood LSA through AS. */
	ospf_flood_through_as(ospf, NULL, new);

	/* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
	if (ospf->anyNSSA && !(CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT)))
		ospf_install_flood_nssa(ospf,
					new); /* Install/Flood per new rules */

	/* Register self-originated LSA to refresh queue.
	 * Translated LSAs should not be registered, but refreshed upon
	 * refresh of the Type-7
	 */
	if (!CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT))
		ospf_refresher_register_lsa(ospf, new);

	/* Debug logging. */
	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
		zlog_debug("LSA[Type%d:%pI4]: AS-external-LSA refresh",
			   new->data->type, &new->data->id);
		ospf_lsa_header_dump(new->data);
	}

	return new;
}


/* LSA installation functions. */

/* Install router-LSA to an area. */
static struct ospf_lsa *
ospf_router_lsa_install(struct ospf *ospf, struct ospf_lsa *new, int rt_recalc)
{
	struct ospf_area *area = new->area;

	/* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
	   The entire routing table must be recalculated, starting with
	   the shortest path calculations for each area (not just the
	   area whose link-state database has changed).
	*/

	if (IS_LSA_SELF(new)) {

		/* Only install LSA if it is originated/refreshed by us.
		 * If LSA was received by flooding, the RECEIVED flag is set so
		 * do
		 * not link the LSA */
		if (CHECK_FLAG(new->flags, OSPF_LSA_RECEIVED))
			return new; /* ignore stale LSA */

		/* Set self-originated router-LSA. */
		ospf_lsa_unlock(&area->router_lsa_self);
		area->router_lsa_self = ospf_lsa_lock(new);

		ospf_refresher_register_lsa(ospf, new);
	}
	if (rt_recalc)
		ospf_spf_calculate_schedule(ospf, SPF_FLAG_ROUTER_LSA_INSTALL);
	return new;
}

/* Install network-LSA to an area. */
static struct ospf_lsa *ospf_network_lsa_install(struct ospf *ospf,
						 struct ospf_interface *oi,
						 struct ospf_lsa *new,
						 int rt_recalc)
{

	/* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
	   The entire routing table must be recalculated, starting with
	   the shortest path calculations for each area (not just the
	   area whose link-state database has changed).
	*/
	if (IS_LSA_SELF(new)) {
		/* We supposed that when LSA is originated by us, we pass the
		   int
		   for which it was originated. If LSA was received by flooding,
		   the RECEIVED flag is set, so we do not link the LSA to the
		   int. */
		if (CHECK_FLAG(new->flags, OSPF_LSA_RECEIVED))
			return new; /* ignore stale LSA */

		ospf_lsa_unlock(&oi->network_lsa_self);
		oi->network_lsa_self = ospf_lsa_lock(new);
		ospf_refresher_register_lsa(ospf, new);
	}
	if (rt_recalc)
		ospf_spf_calculate_schedule(ospf, SPF_FLAG_NETWORK_LSA_INSTALL);

	return new;
}

/* Install summary-LSA to an area. */
static struct ospf_lsa *
ospf_summary_lsa_install(struct ospf *ospf, struct ospf_lsa *new, int rt_recalc)
{
	if (rt_recalc && !IS_LSA_SELF(new)) {
/* RFC 2328 Section 13.2 Summary-LSAs
   The best route to the destination described by the summary-
   LSA must be recalculated (see Section 16.5).  If this
   destination is an AS boundary router, it may also be
   necessary to re-examine all the AS-external-LSAs.
*/

		ospf_spf_calculate_schedule(ospf, SPF_FLAG_SUMMARY_LSA_INSTALL);
	}

	if (IS_LSA_SELF(new))
		ospf_refresher_register_lsa(ospf, new);

	return new;
}

/* Install ASBR-summary-LSA to an area. */
static struct ospf_lsa *ospf_summary_asbr_lsa_install(struct ospf *ospf,
						      struct ospf_lsa *new,
						      int rt_recalc)
{
	if (rt_recalc && !IS_LSA_SELF(new)) {
/* RFC 2328 Section 13.2 Summary-LSAs
   The best route to the destination described by the summary-
   LSA must be recalculated (see Section 16.5).  If this
   destination is an AS boundary router, it may also be
   necessary to re-examine all the AS-external-LSAs.
*/
		ospf_spf_calculate_schedule(ospf,
					    SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL);
	}

	/* register LSA to refresh-list. */
	if (IS_LSA_SELF(new))
		ospf_refresher_register_lsa(ospf, new);

	return new;
}

/* Install AS-external-LSA. */
static struct ospf_lsa *ospf_external_lsa_install(struct ospf *ospf,
						  struct ospf_lsa *new,
						  int rt_recalc)
{
	ospf_ase_register_external_lsa(new, ospf);
	/* If LSA is not self-originated, calculate an external route. */
	if (rt_recalc) {
		/* RFC 2328 Section 13.2 AS-external-LSAs
		      The best route to the destination described by the AS-
		      external-LSA must be recalculated (see Section 16.6).
		*/

		if (!IS_LSA_SELF(new))
			ospf_ase_incremental_update(ospf, new);
	}

	if (new->data->type == OSPF_AS_NSSA_LSA) {
		/* There is no point to register selforiginate Type-7 LSA for
		 * refreshing. We rely on refreshing Type-5 LSA's
		 */
		if (IS_LSA_SELF(new))
			return new;
		else {
			/* Try refresh type-5 translated LSA for this LSA, if
			 * one exists.
			 * New translations will be taken care of by the
			 * abr_task.
			 */
			ospf_translated_nssa_refresh(ospf, new, NULL);
			ospf_schedule_abr_task(ospf);
		}
	}

	/* Register self-originated LSA to refresh queue.
	 * Leave Translated LSAs alone if NSSA is enabled
	 */
	if (IS_LSA_SELF(new) && !CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT))
		ospf_refresher_register_lsa(ospf, new);

	return new;
}

void ospf_discard_from_db(struct ospf *ospf, struct ospf_lsdb *lsdb,
			  struct ospf_lsa *lsa)
{
	struct ospf_lsa *old;

	if (!lsdb)
		return;

	old = ospf_lsdb_lookup(lsdb, lsa);

	if (!old)
		return;

	if (old->refresh_list >= 0)
		ospf_refresher_unregister_lsa(ospf, old);

	switch (old->data->type) {
	case OSPF_AS_EXTERNAL_LSA:
		ospf_ase_unregister_external_lsa(old, ospf);
		ospf_ls_retransmit_delete_nbr_as(ospf, old);
		break;
	case OSPF_OPAQUE_AS_LSA:
		ospf_ls_retransmit_delete_nbr_as(ospf, old);
		break;
	case OSPF_AS_NSSA_LSA:
		ospf_ls_retransmit_delete_nbr_area(old->area, old);
		ospf_ase_unregister_external_lsa(old, ospf);
		break;
	default:
		ospf_ls_retransmit_delete_nbr_area(old->area, old);
		break;
	}

	ospf_lsa_maxage_delete(ospf, old);
	ospf_lsa_discard(old);
}

struct ospf_lsa *ospf_lsa_install(struct ospf *ospf, struct ospf_interface *oi,
				  struct ospf_lsa *lsa)
{
	struct ospf_lsa *new = NULL;
	struct ospf_lsa *old = NULL;
	struct ospf_lsdb *lsdb = NULL;
	int rt_recalc;

	/* Set LSDB. */
	switch (lsa->data->type) {
	/* kevinm */
	case OSPF_AS_NSSA_LSA:
		if (lsa->area)
			lsdb = lsa->area->lsdb;
		else
			lsdb = ospf->lsdb;
		break;
	case OSPF_AS_EXTERNAL_LSA:
	case OSPF_OPAQUE_AS_LSA:
		lsdb = ospf->lsdb;
		break;
	default:
		if (lsa->area)
			lsdb = lsa->area->lsdb;
		break;
	}

	assert(lsdb);

	/*  RFC 2328 13.2.  Installing LSAs in the database

	      Installing a new LSA in the database, either as the result of
	      flooding or a newly self-originated LSA, may cause the OSPF
	      routing table structure to be recalculated.  The contents of the
	      new LSA should be compared to the old instance, if present.  If
	      there is no difference, there is no need to recalculate the
	      routing table. When comparing an LSA to its previous instance,
	      the following are all considered to be differences in contents:

		  o   The LSA's Options field has changed.

		  o   One of the LSA instances has LS age set to MaxAge, and
		      the other does not.

		  o   The length field in the LSA header has changed.

		  o   The body of the LSA (i.e., anything outside the 20-byte
		      LSA header) has changed. Note that this excludes changes
		      in LS Sequence Number and LS Checksum.

	*/
	/* Look up old LSA and determine if any SPF calculation or incremental
	   update is needed */
	old = ospf_lsdb_lookup(lsdb, lsa);

	/* Do comparison and record if recalc needed. */
	rt_recalc = 0;
	if (old == NULL || ospf_lsa_different(old, lsa, false)) {
		/* Ref rfc3623 section 3.2.3
		 * Installing new lsa or change in the existing LSA
		 * or flushing existing LSA leads to topo change
		 * and trigger SPF caculation.
		 * So, router should be aborted from HELPER role
		 * if it is detected as TOPO  change.
		 */
		if (ospf->active_restarter_cnt &&
		    CHECK_LSA_TYPE_1_TO_5_OR_7(lsa->data->type)) {
			if (old == NULL || ospf_lsa_different(old, lsa, true))
				ospf_helper_handle_topo_chg(ospf, lsa);
		}

		rt_recalc = 1;
	}

	/*
	   Sequence number check (Section 14.1 of rfc 2328)
	   "Premature aging is used when it is time for a self-originated
	    LSA's sequence number field to wrap.  At this point, the current
	    LSA instance (having LS sequence number MaxSequenceNumber) must
	    be prematurely aged and flushed from the routing domain before a
	    new instance with sequence number equal to InitialSequenceNumber
	    can be originated. "
	 */

	if (ntohl(lsa->data->ls_seqnum) - 1 == OSPF_MAX_SEQUENCE_NUMBER) {
		if (ospf_lsa_is_self_originated(ospf, lsa)) {
			lsa->data->ls_seqnum = htonl(OSPF_MAX_SEQUENCE_NUMBER);

			if (!IS_LSA_MAXAGE(lsa))
				lsa->flags |= OSPF_LSA_PREMATURE_AGE;
			lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);

			if (IS_DEBUG_OSPF(lsa, LSA_REFRESH)) {
				zlog_debug(
					"%s() Premature Aging lsa %p, seqnum 0x%x",
					__func__, lsa,
					ntohl(lsa->data->ls_seqnum));
				ospf_lsa_header_dump(lsa->data);
			}
		} else {
			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
				zlog_debug(
					"%s() got an lsa with seq 0x80000000 that was not self originated. Ignoring",
					__func__);
				ospf_lsa_header_dump(lsa->data);
			}
			return old;
		}
	}

	/* discard old LSA from LSDB */
	if (old != NULL)
		ospf_discard_from_db(ospf, lsdb, lsa);

	/* Calculate Checksum if self-originated?. */
	if (IS_LSA_SELF(lsa))
		ospf_lsa_checksum(lsa->data);

	/* Insert LSA to LSDB. */
	ospf_lsdb_add(lsdb, lsa);
	lsa->lsdb = lsdb;

	/* Do LSA specific installation process. */
	switch (lsa->data->type) {
	case OSPF_ROUTER_LSA:
		new = ospf_router_lsa_install(ospf, lsa, rt_recalc);
		break;
	case OSPF_NETWORK_LSA:
		assert(oi);
		new = ospf_network_lsa_install(ospf, oi, lsa, rt_recalc);
		break;
	case OSPF_SUMMARY_LSA:
		new = ospf_summary_lsa_install(ospf, lsa, rt_recalc);
		break;
	case OSPF_ASBR_SUMMARY_LSA:
		new = ospf_summary_asbr_lsa_install(ospf, lsa, rt_recalc);
		break;
	case OSPF_AS_EXTERNAL_LSA:
		new = ospf_external_lsa_install(ospf, lsa, rt_recalc);
		break;
	case OSPF_OPAQUE_LINK_LSA:
		if (IS_LSA_SELF(lsa))
			lsa->oi = oi; /* Specify outgoing ospf-interface for
					 this LSA. */
		else {
			/* Incoming "oi" for this LSA has set at LSUpd
			 * reception. */
		}
		fallthrough;
	case OSPF_OPAQUE_AREA_LSA:
	case OSPF_OPAQUE_AS_LSA:
		new = ospf_opaque_lsa_install(lsa, rt_recalc);
		break;
	case OSPF_AS_NSSA_LSA:
		new = ospf_external_lsa_install(ospf, lsa, rt_recalc);
		break;
	default: /* type-6,8,9....nothing special */
		break;
	}

	if (new == NULL)
		return new; /* Installation failed, cannot proceed further --
			       endo. */

	/* Debug logs. */
	if (IS_DEBUG_OSPF(lsa, LSA_INSTALL)) {
		switch (lsa->data->type) {
		case OSPF_AS_EXTERNAL_LSA:
		case OSPF_OPAQUE_AS_LSA:
		case OSPF_AS_NSSA_LSA:
			zlog_debug("LSA[%s]: Install %s", dump_lsa_key(new),
				   lookup_msg(ospf_lsa_type_msg,
					      new->data->type, NULL));
			break;
		default:
			zlog_debug("LSA[%s]: Install %s to Area %pI4",
				   dump_lsa_key(new),
				   lookup_msg(ospf_lsa_type_msg,
					      new->data->type, NULL),
				   &new->area->area_id);
			break;
		}
	}

	/*
	   If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
	   (it's getting flushed out of the area), set LSA on MaxAge LSA list.
	 */
	if (IS_LSA_MAXAGE(new)) {
		if (IS_DEBUG_OSPF(lsa, LSA_INSTALL))
			zlog_debug("LSA[%s]: Install LSA %p, MaxAge",
				   dump_lsa_key(new), lsa);
		ospf_lsa_maxage(ospf, lsa);
	}

	return new;
}


int ospf_check_nbr_status(struct ospf *ospf)
{
	struct listnode *node, *nnode;
	struct ospf_interface *oi;

	for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi)) {
		struct route_node *rn;
		struct ospf_neighbor *nbr;

		if (ospf_if_is_enable(oi))
			for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
				if ((nbr = rn->info) != NULL)
					if (nbr->state == NSM_Exchange
					    || nbr->state == NSM_Loading) {
						route_unlock_node(rn);
						return 0;
					}
	}

	return 1;
}


void ospf_maxage_lsa_remover(struct event *thread)
{
	struct ospf *ospf = EVENT_ARG(thread);
	struct ospf_lsa *lsa, *old;
	struct route_node *rn;
	int reschedule = 0;

	ospf->t_maxage = NULL;

	if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
		zlog_debug("LSA[MaxAge]: remover Start");

	reschedule = !ospf_check_nbr_status(ospf);

	if (!reschedule)
		for (rn = route_top(ospf->maxage_lsa); rn;
		     rn = route_next(rn)) {
			if ((lsa = rn->info) == NULL) {
				continue;
			}

			/* There is at least one neighbor from which we still
			 * await an ack
			 * for that LSA, so we are not allowed to remove it from
			 * our lsdb yet
			 * as per RFC 2328 section 14 para 4 a) */
			if (lsa->retransmit_counter > 0) {
				reschedule = 1;
				continue;
			}

			/* TODO: maybe convert this function to a work-queue */
			if (event_should_yield(thread)) {
				OSPF_TIMER_ON(ospf->t_maxage,
					      ospf_maxage_lsa_remover, 0);
				route_unlock_node(
					rn); /* route_top/route_next */
				return;
			}

			/* Remove LSA from the LSDB */
			if (IS_LSA_SELF(lsa))
				if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
					zlog_debug(
						"LSA[Type%d:%pI4]: LSA 0x%lx is self-originated: ",
						lsa->data->type,
						&lsa->data->id,
						(unsigned long)lsa);

			if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
				zlog_debug(
					"LSA[%s]: MaxAge LSA removed from list",
					dump_lsa_key(lsa));

			if (CHECK_FLAG(lsa->flags, OSPF_LSA_PREMATURE_AGE)) {
				if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
					zlog_debug(
						"originating new lsa for lsa %p",
						lsa);
				ospf_lsa_refresh(ospf, lsa);
			}

			/* Remove from lsdb. */
			if (lsa->lsdb) {
				old = ospf_lsdb_lookup(lsa->lsdb, lsa);
				/* The max age LSA here must be the same
				 * as the LSA in LSDB
				 */
				if (old != lsa) {
					flog_err(EC_OSPF_LSA_MISSING,
						 "%s: LSA[%s]: LSA not in LSDB",
						 __func__, dump_lsa_key(lsa));

					continue;
				}
				ospf_discard_from_db(ospf, lsa->lsdb, lsa);
				ospf_lsdb_delete(lsa->lsdb, lsa);
			} else {
				if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
					zlog_debug(
						"%s: LSA[%s]: No associated LSDB!",
						__func__, dump_lsa_key(lsa));
			}
		}

	/*    A MaxAge LSA must be removed immediately from the router's link
	      state database as soon as both a) it is no longer contained on any
	      neighbor Link state retransmission lists and b) none of the
	   router's
	      neighbors are in states Exchange or Loading. */
	if (reschedule)
		OSPF_TIMER_ON(ospf->t_maxage, ospf_maxage_lsa_remover,
			      ospf->maxage_delay);
}

/* This function checks whether an LSA with initial sequence number should be
 *  originated after a wrap in sequence number
 */
void ospf_check_and_gen_init_seq_lsa(struct ospf_interface *oi,
				     struct ospf_lsa *recv_lsa)
{
	struct ospf_lsa *lsa = NULL;
	struct ospf *ospf = oi->ospf;

	lsa = ospf_lsa_lookup_by_header(oi->area, recv_lsa->data);

	if ((lsa == NULL) || (!CHECK_FLAG(lsa->flags, OSPF_LSA_PREMATURE_AGE))
	    || (lsa->retransmit_counter != 0)) {
		if (IS_DEBUG_OSPF(lsa, LSA))
			zlog_debug(
				"Do not generate LSA with initial seqence number.");
		return;
	}

	ospf_lsa_maxage_delete(ospf, lsa);

	lsa->data->ls_seqnum = lsa_seqnum_increment(lsa);

	ospf_lsa_refresh(ospf, lsa);
}

void ospf_lsa_maxage_delete(struct ospf *ospf, struct ospf_lsa *lsa)
{
	struct route_node *rn;
	struct prefix lsa_prefix;

	memset(&lsa_prefix, 0, sizeof(lsa_prefix));
	lsa_prefix.family = AF_UNSPEC;
	lsa_prefix.prefixlen = sizeof(lsa_prefix.u.ptr) * CHAR_BIT;
	lsa_prefix.u.ptr = (uintptr_t)lsa;

	if ((rn = route_node_lookup(ospf->maxage_lsa, &lsa_prefix))) {
		if (rn->info == lsa) {
			UNSET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
			ospf_lsa_unlock(&lsa); /* maxage_lsa */
			rn->info = NULL;
			route_unlock_node(
				rn); /* unlock node because lsa is deleted */
		}
		route_unlock_node(rn); /* route_node_lookup */
	} else {
		if (IS_DEBUG_OSPF_EVENT)
			zlog_debug("%s: lsa %s is not found in maxage db.",
				   __func__, dump_lsa_key(lsa));
	}
}

/* Add LSA onto the MaxAge list, and schedule for removal.
 * This does *not* lead to the LSA being flooded, that must be taken
 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
 * function).
 */
void ospf_lsa_maxage(struct ospf *ospf, struct ospf_lsa *lsa)
{
	struct prefix lsa_prefix;
	struct route_node *rn;

	/* When we saw a MaxAge LSA flooded to us, we put it on the list
	   and schedule the MaxAge LSA remover. */
	if (CHECK_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE)) {
		if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
			zlog_debug(
				"LSA[%s]: %p already exists on MaxAge LSA list",
				dump_lsa_key(lsa), lsa);
		return;
	}

	memset(&lsa_prefix, 0, sizeof(lsa_prefix));
	lsa_prefix.family = AF_UNSPEC;
	lsa_prefix.prefixlen = sizeof(lsa_prefix.u.ptr) * CHAR_BIT;
	lsa_prefix.u.ptr = (uintptr_t)lsa;

	rn = route_node_get(ospf->maxage_lsa, &lsa_prefix);
	if (rn->info != NULL) {
		if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
			zlog_debug(
				   "LSA[%s]: found LSA (%p) in table for LSA %p %d",
				   dump_lsa_key(lsa), rn->info,
				   (void *)lsa, lsa_prefix.prefixlen);
		route_unlock_node(rn);
	} else {
		rn->info = ospf_lsa_lock(lsa);
		SET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
	}

	if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
		zlog_debug("LSA[%s]: MaxAge LSA remover scheduled.",
			   dump_lsa_key(lsa));

	OSPF_TIMER_ON(ospf->t_maxage, ospf_maxage_lsa_remover,
		      ospf->maxage_delay);
}

static int ospf_lsa_maxage_walker_remover(struct ospf *ospf,
					  struct ospf_lsa *lsa)
{
	/* Stay away from any Local Translated Type-7 LSAs */
	if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
		return 0;

	if (IS_LSA_MAXAGE(lsa))
		/* Self-originated LSAs should NOT time-out instead,
		   they're flushed and submitted to the max_age list explicitly.
		   */
		if (!ospf_lsa_is_self_originated(ospf, lsa)) {
			if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
				zlog_debug("LSA[%s]: is MaxAge",
					   dump_lsa_key(lsa));

			switch (lsa->data->type) {
			case OSPF_OPAQUE_LINK_LSA:
			case OSPF_OPAQUE_AREA_LSA:
			case OSPF_OPAQUE_AS_LSA:
				/*
				 * As a general rule, whenever network topology
				 * has changed
				 * (due to an LSA removal in this case), routing
				 * recalculation
				 * should be triggered. However, this is not
				 * true for opaque
				 * LSAs. Even if an opaque LSA instance is going
				 * to be removed
				 * from the routing domain, it does not mean a
				 * change in network
				 * topology, and thus, routing recalculation is
				 * not needed here.
				 */
				break;
			case OSPF_AS_EXTERNAL_LSA:
			case OSPF_AS_NSSA_LSA:
				ospf_ase_incremental_update(ospf, lsa);
				break;
			default:
				ospf_spf_calculate_schedule(ospf,
							    SPF_FLAG_MAXAGE);
				break;
			}
			ospf_lsa_maxage(ospf, lsa);
		}

	if (IS_LSA_MAXAGE(lsa) && !ospf_lsa_is_self_originated(ospf, lsa))
		if (LS_AGE(lsa) > OSPF_LSA_MAXAGE + 30)
			printf("Eek! Shouldn't happen!\n");

	return 0;
}

/* Periodical check of MaxAge LSA. */
void ospf_lsa_maxage_walker(struct event *thread)
{
	struct ospf *ospf = EVENT_ARG(thread);
	struct route_node *rn;
	struct ospf_lsa *lsa;
	struct ospf_area *area;
	struct listnode *node, *nnode;

	ospf->t_maxage_walker = NULL;

	for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
		LSDB_LOOP (ROUTER_LSDB(area), rn, lsa)
			ospf_lsa_maxage_walker_remover(ospf, lsa);
		LSDB_LOOP (NETWORK_LSDB(area), rn, lsa)
			ospf_lsa_maxage_walker_remover(ospf, lsa);
		LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
			ospf_lsa_maxage_walker_remover(ospf, lsa);
		LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
			ospf_lsa_maxage_walker_remover(ospf, lsa);
		LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
			ospf_lsa_maxage_walker_remover(ospf, lsa);
		LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
			ospf_lsa_maxage_walker_remover(ospf, lsa);
		LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
			ospf_lsa_maxage_walker_remover(ospf, lsa);
	}

	/* for AS-external-LSAs. */
	if (ospf->lsdb) {
		LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
			ospf_lsa_maxage_walker_remover(ospf, lsa);
		LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
			ospf_lsa_maxage_walker_remover(ospf, lsa);
	}

	OSPF_TIMER_ON(ospf->t_maxage_walker, ospf_lsa_maxage_walker,
		      OSPF_LSA_MAXAGE_CHECK_INTERVAL);
}

struct ospf_lsa *ospf_lsa_lookup_by_prefix(struct ospf_lsdb *lsdb, uint8_t type,
					   struct prefix_ipv4 *p,
					   struct in_addr router_id)
{
	struct ospf_lsa *lsa;
	struct in_addr mask, id;
	struct lsa_header_mask {
		struct lsa_header header;
		struct in_addr mask;
	} * hmask;

	lsa = ospf_lsdb_lookup_by_id(lsdb, type, p->prefix, router_id);
	if (lsa == NULL)
		return NULL;

	masklen2ip(p->prefixlen, &mask);

	hmask = (struct lsa_header_mask *)lsa->data;

	if (mask.s_addr != hmask->mask.s_addr) {
		id.s_addr = p->prefix.s_addr | (~mask.s_addr);
		lsa = ospf_lsdb_lookup_by_id(lsdb, type, id, router_id);
		if (!lsa)
			return NULL;
	}

	return lsa;
}

struct ospf_lsa *ospf_lsa_lookup(struct ospf *ospf, struct ospf_area *area,
				 uint32_t type, struct in_addr id,
				 struct in_addr adv_router)
{
	if (!ospf)
		return NULL;

	switch (type) {
	case OSPF_ROUTER_LSA:
	case OSPF_NETWORK_LSA:
	case OSPF_SUMMARY_LSA:
	case OSPF_ASBR_SUMMARY_LSA:
	case OSPF_AS_NSSA_LSA:
	case OSPF_OPAQUE_LINK_LSA:
	case OSPF_OPAQUE_AREA_LSA:
		return ospf_lsdb_lookup_by_id(area->lsdb, type, id, adv_router);
	case OSPF_AS_EXTERNAL_LSA:
	case OSPF_OPAQUE_AS_LSA:
		return ospf_lsdb_lookup_by_id(ospf->lsdb, type, id, adv_router);
	default:
		break;
	}

	return NULL;
}

struct ospf_lsa *ospf_lsa_lookup_by_id(struct ospf_area *area, uint32_t type,
				       struct in_addr id)
{
	struct ospf_lsa *lsa;
	struct route_node *rn;

	switch (type) {
	case OSPF_ROUTER_LSA:
		return ospf_lsdb_lookup_by_id(area->lsdb, type, id, id);
	case OSPF_NETWORK_LSA:
		for (rn = route_top(NETWORK_LSDB(area)); rn;
		     rn = route_next(rn))
			if ((lsa = rn->info))
				if (IPV4_ADDR_SAME(&lsa->data->id, &id)) {
					route_unlock_node(rn);
					return lsa;
				}
		break;
	case OSPF_SUMMARY_LSA:
	case OSPF_ASBR_SUMMARY_LSA:
		/* Currently not used. */
		assert(1);
		return ospf_lsdb_lookup_by_id(area->lsdb, type, id, id);
	case OSPF_AS_EXTERNAL_LSA:
	case OSPF_AS_NSSA_LSA:
	case OSPF_OPAQUE_LINK_LSA:
	case OSPF_OPAQUE_AREA_LSA:
	case OSPF_OPAQUE_AS_LSA:
		/* Currently not used. */
		break;
	default:
		break;
	}

	return NULL;
}

struct ospf_lsa *ospf_lsa_lookup_by_header(struct ospf_area *area,
					   struct lsa_header *lsah)
{
	struct ospf_lsa *match;

	/*
	 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
	 * is redefined to have two subfields; opaque-type and opaque-id.
	 * However, it is harmless to treat the two sub fields together, as if
	 * they two were forming a unique LSA-ID.
	 */

	match = ospf_lsa_lookup(area->ospf, area, lsah->type, lsah->id,
				lsah->adv_router);

	if (match == NULL)
		if (IS_DEBUG_OSPF(lsa, LSA) == OSPF_DEBUG_LSA)
			zlog_debug("LSA[Type%d:%pI4]: Lookup by header, NO MATCH",
				   lsah->type, &lsah->id);

	return match;
}

/* return +n, l1 is more recent.
   return -n, l2 is more recent.
   return 0, l1 and l2 is identical. */
int ospf_lsa_more_recent(struct ospf_lsa *l1, struct ospf_lsa *l2)
{
	int r;
	int x, y;

	if (l1 == NULL && l2 == NULL)
		return 0;
	if (l1 == NULL)
		return -1;
	if (l2 == NULL)
		return 1;

	/* compare LS sequence number. */
	x = (int)ntohl(l1->data->ls_seqnum);
	y = (int)ntohl(l2->data->ls_seqnum);
	if (x > y)
		return 1;
	if (x < y)
		return -1;

	/* compare LS checksum. */
	r = ntohs(l1->data->checksum) - ntohs(l2->data->checksum);
	if (r)
		return r;

	/* compare LS age. */
	if (IS_LSA_MAXAGE(l1) && !IS_LSA_MAXAGE(l2))
		return 1;
	else if (!IS_LSA_MAXAGE(l1) && IS_LSA_MAXAGE(l2))
		return -1;

	/* compare LS age with MaxAgeDiff. */
	if (LS_AGE(l1) - LS_AGE(l2) > OSPF_LSA_MAXAGE_DIFF)
		return -1;
	else if (LS_AGE(l2) - LS_AGE(l1) > OSPF_LSA_MAXAGE_DIFF)
		return 1;

	/* LSAs are identical. */
	return 0;
}

/*
 * Check if two LSAs are different.
 *
 * l1
 *    The first LSA to compare.
 *
 * l2
 *    The second LSA to compare.
 *
 * ignore_rcvd_flag
 *    When set to true, ignore whether the LSAs were received from the network
 *    or not. This parameter should be set to true when checking for topology
 *    changes as part of the Graceful Restart helper neighbor procedures.
 *
 * Returns:
 *    true if the LSAs are different, false otherwise.
 */
int ospf_lsa_different(struct ospf_lsa *l1, struct ospf_lsa *l2,
		       bool ignore_rcvd_flag)
{
	char *p1, *p2;
	assert(l1);
	assert(l2);
	assert(l1->data);
	assert(l2->data);

	if (l1->data->options != l2->data->options)
		return 1;

	if (IS_LSA_MAXAGE(l1) && !IS_LSA_MAXAGE(l2))
		return 1;

	if (IS_LSA_MAXAGE(l2) && !IS_LSA_MAXAGE(l1))
		return 1;

	if (l1->size != l2->size)
		return 1;

	if (l1->size == 0)
		return 1;

	if (!ignore_rcvd_flag
	    && CHECK_FLAG((l1->flags ^ l2->flags), OSPF_LSA_RECEIVED))
		return 1; /* May be a stale LSA in the LSBD */

	if (l1->size == OSPF_LSA_HEADER_SIZE)
		return 0; /* nothing to compare */

	p1 = (char *)l1->data;
	p2 = (char *)l2->data;

	if (memcmp(p1 + OSPF_LSA_HEADER_SIZE, p2 + OSPF_LSA_HEADER_SIZE,
		   l1->size - OSPF_LSA_HEADER_SIZE)
	    != 0)
		return 1;

	return 0;
}

int ospf_lsa_flush_schedule(struct ospf *ospf, struct ospf_lsa *lsa)
{
	if (lsa == NULL || !IS_LSA_SELF(lsa))
		return 0;

	if (IS_DEBUG_OSPF_EVENT)
		zlog_debug(
			"LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
			lsa->data->type, &lsa->data->id);

	/* Force given lsa's age to MaxAge. */
	lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);

	switch (lsa->data->type) {
	/* Opaque wants to be notified of flushes */
	case OSPF_OPAQUE_LINK_LSA:
	case OSPF_OPAQUE_AREA_LSA:
	case OSPF_OPAQUE_AS_LSA:
		ospf_opaque_lsa_refresh(lsa);
		break;
	default:
		ospf_refresher_unregister_lsa(ospf, lsa);
		ospf_lsa_flush(ospf, lsa);
		break;
	}

	return 0;
}

void ospf_flush_self_originated_lsas_now(struct ospf *ospf)
{
	struct listnode *node, *nnode;
	struct listnode *node2, *nnode2;
	struct ospf_area *area;
	struct ospf_interface *oi;
	struct ospf_lsa *lsa;
	struct route_node *rn;
	struct ospf_if_params *oip;
	int need_to_flush_ase = 0;

	ospf->inst_shutdown = 1;

	for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
		if ((lsa = area->router_lsa_self) != NULL) {
			if (IS_DEBUG_OSPF_EVENT)
				zlog_debug(
					"LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
					lsa->data->type,
					&lsa->data->id);

			ospf_refresher_unregister_lsa(ospf, lsa);
			ospf_lsa_flush_area(lsa, area);
			ospf_lsa_unlock(&area->router_lsa_self);
			area->router_lsa_self = NULL;
		}

		for (ALL_LIST_ELEMENTS(area->oiflist, node2, nnode2, oi)) {
			if ((lsa = oi->network_lsa_self) != NULL
			    && oi->state == ISM_DR && oi->full_nbrs > 0) {
				if (IS_DEBUG_OSPF_EVENT)
					zlog_debug(
						"LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
						lsa->data->type,
						&lsa->data->id);

				ospf_refresher_unregister_lsa(
					ospf, oi->network_lsa_self);
				ospf_lsa_flush_area(oi->network_lsa_self, area);
				ospf_lsa_unlock(&oi->network_lsa_self);
				oi->network_lsa_self = NULL;

				oip = ospf_lookup_if_params(
					oi->ifp, oi->address->u.prefix4);
				if (oip)
					oip->network_lsa_seqnum = htonl(
						OSPF_INVALID_SEQUENCE_NUMBER);
			}

			if (oi->type != OSPF_IFTYPE_VIRTUALLINK
			    && area->external_routing == OSPF_AREA_DEFAULT)
				need_to_flush_ase = 1;
		}

		LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
			ospf_lsa_flush_schedule(ospf, lsa);
		LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
			ospf_lsa_flush_schedule(ospf, lsa);
		LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
			ospf_lsa_flush_schedule(ospf, lsa);
		LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
			ospf_lsa_flush_schedule(ospf, lsa);
	}

	if (need_to_flush_ase) {
		LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
			ospf_lsa_flush_schedule(ospf, lsa);
		LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
			ospf_lsa_flush_schedule(ospf, lsa);
	}

	/*
	 * Make sure that the MaxAge LSA remover is executed immediately,
	 * without conflicting to other threads.
	 */
	if (ospf->t_maxage != NULL) {
		EVENT_OFF(ospf->t_maxage);
		event_execute(master, ospf_maxage_lsa_remover, ospf, 0, NULL);
	}

	return;
}

/** @brief Function to refresh all the self originated
 *	   LSAs for area, when FR state change happens.
 *  @param area pointer.
 *  @return Void.
 */
void ospf_refresh_area_self_lsas(struct ospf_area *area)
{
	struct listnode *node2;
	struct listnode *nnode2;
	struct ospf_interface *oi;
	struct route_node *rn;
	struct ospf_lsa *lsa;

	if (!area)
		return;

	if (area->router_lsa_self)
		ospf_lsa_refresh(area->ospf, area->router_lsa_self);

	for (ALL_LIST_ELEMENTS(area->oiflist, node2, nnode2, oi))
		if (oi->network_lsa_self)
			ospf_lsa_refresh(oi->ospf, oi->network_lsa_self);

	LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
		if (IS_LSA_SELF(lsa))
			ospf_lsa_refresh(area->ospf, lsa);
	LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
		if (IS_LSA_SELF(lsa))
			ospf_lsa_refresh(area->ospf, lsa);
	LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
		if (IS_LSA_SELF(lsa))
			ospf_lsa_refresh(area->ospf, lsa);
	LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
		if (IS_LSA_SELF(lsa))
			ospf_lsa_refresh(area->ospf, lsa);
	LSDB_LOOP (EXTERNAL_LSDB(area->ospf), rn, lsa)
		if (IS_LSA_SELF(lsa))
			ospf_lsa_refresh(area->ospf, lsa);
	LSDB_LOOP (OPAQUE_AS_LSDB(area->ospf), rn, lsa)
		if (IS_LSA_SELF(lsa))
			ospf_lsa_refresh(area->ospf, lsa);
}

/* If there is self-originated LSA, then return 1, otherwise return 0. */
/* An interface-independent version of ospf_lsa_is_self_originated */
int ospf_lsa_is_self_originated(struct ospf *ospf, struct ospf_lsa *lsa)
{
	struct listnode *node;
	struct ospf_interface *oi;

	/* This LSA is already checked. */
	if (CHECK_FLAG(lsa->flags, OSPF_LSA_SELF_CHECKED))
		return IS_LSA_SELF(lsa);

	/* Make sure LSA is self-checked. */
	SET_FLAG(lsa->flags, OSPF_LSA_SELF_CHECKED);

	/* AdvRouter and Router ID is the same. */
	if (IPV4_ADDR_SAME(&lsa->data->adv_router, &ospf->router_id))
		SET_FLAG(lsa->flags, OSPF_LSA_SELF);

	/* LSA is router-LSA. */
	else if (lsa->data->type == OSPF_ROUTER_LSA
		 && IPV4_ADDR_SAME(&lsa->data->id, &ospf->router_id))
		SET_FLAG(lsa->flags, OSPF_LSA_SELF);

	/* LSA is network-LSA.  Compare Link ID with all interfaces. */
	else if (lsa->data->type == OSPF_NETWORK_LSA)
		for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
			/* Ignore virtual link. */
			if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
				if (oi->address->family == AF_INET)
					if (IPV4_ADDR_SAME(
						    &lsa->data->id,
						    &oi->address->u.prefix4)) {
						/* to make it easier later */
						SET_FLAG(lsa->flags,
							 OSPF_LSA_SELF);
						return IS_LSA_SELF(lsa);
					}
		}

	return IS_LSA_SELF(lsa);
}

/* Get unique Link State ID. */
enum lsid_status ospf_lsa_unique_id(struct ospf *ospf, struct ospf_lsdb *lsdb,
				    uint8_t type, struct prefix_ipv4 *p,
				    struct in_addr *id)
{
	struct ospf_lsa *lsa;
	struct in_addr mask;

	*id = p->prefix;

	/* Check existence of LSA instance. */
	lsa = ospf_lsdb_lookup_by_id(lsdb, type, *id, ospf->router_id);
	if (lsa) {
		struct as_external_lsa *al =
			(struct as_external_lsa *)lsa->data;
		/* Ref rfc2328,Appendex E.1
		 * If router already originated the external lsa with lsid
		 * as the current prefix, and the masklens are same then
		 * terminate the LSID algorithem.
		 */
		if (ip_masklen(al->mask) == p->prefixlen) {
			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
				zlog_debug(
					"%s: Can't get Link State ID for %pFX",
					__func__, p);
			/*	  id.s_addr = 0; */
			id->s_addr = 0xffffffff;
			return LSID_NOT_AVAILABLE;
		} else if (ip_masklen(al->mask) < p->prefixlen) {
			/* Ref rfc2328,Appendex E.2
			 * the current prefix masklen is greater than the
			 * existing LSA, then generate the Link state ID,
			 * by setting all host bits in prefix addressa and
			 * originate.
			 *
			 * Eg: 1st Route : 10.0.0.0/16 - LSID:10.0.0.0
			 *     2nd Route : 10.0.0.0/24 - LSID:10.0.0.255
			 */
			masklen2ip(p->prefixlen, &mask);

			id->s_addr = p->prefix.s_addr | (~mask.s_addr);
			lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, type, *id,
						     ospf->router_id);
			if (lsa) {
				if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
					zlog_debug(
						"%s: Can't get Link State ID for %pFX",
						__func__, p);
				id->s_addr = 0xffffffff;
				return LSID_NOT_AVAILABLE;
			}
		} else {
			/* Ref rfc2328,Appendex E.3
			 * the current prefix masklen is lesser than the
			 * existing LSA,then the originated LSA has to be
			 * refreshed by modifying masklen, cost and tag.
			 * Originate the old route info with new LSID by
			 * setting the host bits in prefix address.
			 *
			 * Eg: 1st Route : 10.0.0.0/24 - LSID:10.0.0.0
			 *     2nd Route : 10.0.0.0/16 - ?
			 * Since 2nd route mask len is less than firstone
			 * LSID has to be changed.
			 *     1st route LSID:10.0.0.255
			 *     2nd route LSID:10.0.0.0
			 */
			id->s_addr = lsa->data->id.s_addr | (~al->mask.s_addr);
			lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, type, *id,
						     ospf->router_id);
			if (lsa && (ip_masklen(al->mask) != IPV4_MAX_BITLEN)) {
				if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
					zlog_debug(
						"%s: Can't get Link State ID for %pFX",
						__func__, p);
				id->s_addr = 0xffffffff;
				return LSID_NOT_AVAILABLE;
			}
			return LSID_CHANGE;
		}
	}

	return LSID_AVAILABLE;
}


#define LSA_ACTION_FLOOD_AREA 1
#define LSA_ACTION_FLUSH_AREA 2

struct lsa_action {
	uint8_t action;
	struct ospf_area *area;
	struct ospf_lsa *lsa;
};

static void ospf_lsa_action(struct event *t)
{
	struct lsa_action *data;

	data = EVENT_ARG(t);

	if (IS_DEBUG_OSPF(lsa, LSA) == OSPF_DEBUG_LSA)
		zlog_debug("LSA[Action]: Performing scheduled LSA action: %d",
			   data->action);

	switch (data->action) {
	case LSA_ACTION_FLOOD_AREA:
		ospf_flood_through_area(data->area, NULL, data->lsa);
		break;
	case LSA_ACTION_FLUSH_AREA:
		ospf_lsa_flush_area(data->lsa, data->area);
		break;
	}

	ospf_lsa_unlock(&data->lsa); /* Message */
	XFREE(MTYPE_OSPF_MESSAGE, data);
}

void ospf_schedule_lsa_flood_area(struct ospf_area *area, struct ospf_lsa *lsa)
{
	struct lsa_action *data;

	data = XCALLOC(MTYPE_OSPF_MESSAGE, sizeof(struct lsa_action));
	data->action = LSA_ACTION_FLOOD_AREA;
	data->area = area;
	data->lsa = ospf_lsa_lock(lsa); /* Message / Flood area */

	event_add_event(master, ospf_lsa_action, data, 0, NULL);
}

void ospf_schedule_lsa_flush_area(struct ospf_area *area, struct ospf_lsa *lsa)
{
	struct lsa_action *data;

	data = XCALLOC(MTYPE_OSPF_MESSAGE, sizeof(struct lsa_action));
	data->action = LSA_ACTION_FLUSH_AREA;
	data->area = area;
	data->lsa = ospf_lsa_lock(lsa); /* Message / Flush area */

	event_add_event(master, ospf_lsa_action, data, 0, NULL);
}


/* LSA Refreshment functions. */
struct ospf_lsa *ospf_lsa_refresh(struct ospf *ospf, struct ospf_lsa *lsa)
{
	struct external_info *ei;
	struct ospf_external_aggr_rt *aggr;
	struct ospf_lsa *new = NULL;
	struct as_external_lsa *al;
	struct prefix_ipv4 p;

	assert(CHECK_FLAG(lsa->flags, OSPF_LSA_SELF));
	assert(IS_LSA_SELF(lsa));
	assert(lsa->lock > 0);

	switch (lsa->data->type) {
	/* Router and Network LSAs are processed differently. */
	case OSPF_ROUTER_LSA:
		new = ospf_router_lsa_refresh(lsa);
		break;
	case OSPF_NETWORK_LSA:
		new = ospf_network_lsa_refresh(lsa);
		break;
	case OSPF_SUMMARY_LSA:
		new = ospf_summary_lsa_refresh(ospf, lsa);
		break;
	case OSPF_ASBR_SUMMARY_LSA:
		new = ospf_summary_asbr_lsa_refresh(ospf, lsa);
		break;
	case OSPF_AS_EXTERNAL_LSA:
		/* Translated from NSSA Type-5s are refreshed when
		 * from refresh of Type-7 - do not refresh these directly.
		 */

		al = (struct as_external_lsa *)lsa->data;
		p.family = AF_INET;
		p.prefixlen = ip_masklen(al->mask);
		p.prefix = lsa->data->id;

		if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
			break;
		ei = ospf_external_info_check(ospf, lsa);
		if (ei)
			new = ospf_external_lsa_refresh(
				ospf, lsa, ei, LSA_REFRESH_FORCE, false);
		else {
			aggr = (struct ospf_external_aggr_rt *)
				ospf_extrenal_aggregator_lookup(ospf, &p);
			if (aggr) {
				struct external_info ei_aggr;

				memset(&ei_aggr, 0, sizeof(ei_aggr));
				ei_aggr.p = aggr->p;
				ei_aggr.tag = aggr->tag;
				ei_aggr.instance = ospf->instance;
				ei_aggr.route_map_set.metric = -1;
				ei_aggr.route_map_set.metric_type = -1;

				ospf_external_lsa_refresh(ospf, lsa, &ei_aggr,
						  LSA_REFRESH_FORCE, true);
				SET_FLAG(aggr->flags,
					 OSPF_EXTERNAL_AGGRT_ORIGINATED);
			} else
				ospf_lsa_flush_as(ospf, lsa);
		}
		break;
	case OSPF_OPAQUE_LINK_LSA:
	case OSPF_OPAQUE_AREA_LSA:
	case OSPF_OPAQUE_AS_LSA:
		new = ospf_opaque_lsa_refresh(lsa);
		break;
	default:
		break;
	}
	return new;
}

void ospf_refresher_register_lsa(struct ospf *ospf, struct ospf_lsa *lsa)
{
	uint16_t index, current_index;

	assert(lsa->lock > 0);
	assert(IS_LSA_SELF(lsa));

	if (lsa->refresh_list < 0) {
		int delay;
		int min_delay =
			ospf->lsa_refresh_timer - (2 * OSPF_LS_REFRESH_JITTER);
		int max_delay =
			ospf->lsa_refresh_timer - OSPF_LS_REFRESH_JITTER;

		/* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which
		 * is
		 * 1800s. Use jitter so that we send the LSA sometime between
		 * 1680s
		 * and 1740s.
		 */
		delay = (frr_weak_random() % (max_delay - min_delay))
			+ min_delay;

		current_index = ospf->lsa_refresh_queue.index
				+ (monotime(NULL) - ospf->lsa_refresher_started)
					  / OSPF_LSA_REFRESHER_GRANULARITY;

		index = (current_index + delay / OSPF_LSA_REFRESHER_GRANULARITY)
			% (OSPF_LSA_REFRESHER_SLOTS);

		if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
			zlog_debug(
				"LSA[Refresh:Type%d:%pI4]: age %d, added to index %d",
				lsa->data->type, &lsa->data->id,
				LS_AGE(lsa), index);

		if (!ospf->lsa_refresh_queue.qs[index])
			ospf->lsa_refresh_queue.qs[index] = list_new();

		listnode_add(ospf->lsa_refresh_queue.qs[index],
			     ospf_lsa_lock(lsa)); /* lsa_refresh_queue */
		lsa->refresh_list = index;

		if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
			zlog_debug(
				"LSA[Refresh:Type%d:%pI4]: %s: setting refresh_list on lsa %p (slot %d)",
				lsa->data->type, &lsa->data->id, __func__,
				(void *)lsa, index);
	}
}

void ospf_refresher_unregister_lsa(struct ospf *ospf, struct ospf_lsa *lsa)
{
	assert(lsa->lock > 0);
	assert(IS_LSA_SELF(lsa));
	if (lsa->refresh_list >= 0) {
		struct list *refresh_list =
			ospf->lsa_refresh_queue.qs[lsa->refresh_list];
		listnode_delete(refresh_list, lsa);
		if (!listcount(refresh_list)) {
			list_delete(&refresh_list);
			ospf->lsa_refresh_queue.qs[lsa->refresh_list] = NULL;
		}
		lsa->refresh_list = -1;
		ospf_lsa_unlock(&lsa); /* lsa_refresh_queue */
	}
}

void ospf_lsa_refresh_walker(struct event *t)
{
	struct list *refresh_list;
	struct listnode *node, *nnode;
	struct ospf *ospf = EVENT_ARG(t);
	struct ospf_lsa *lsa;
	int i;
	struct list *lsa_to_refresh = list_new();
	bool dna_lsa;

	if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
		zlog_debug("LSA[Refresh]: %s: start", __func__);


	i = ospf->lsa_refresh_queue.index;

	/* Note: if clock has jumped backwards, then time change could be
	   negative,
	   so we are careful to cast the expression to unsigned before taking
	   modulus. */
	ospf->lsa_refresh_queue.index =
		((unsigned long)(ospf->lsa_refresh_queue.index
				 + (monotime(NULL)
				    - ospf->lsa_refresher_started)
					   / OSPF_LSA_REFRESHER_GRANULARITY))
		% OSPF_LSA_REFRESHER_SLOTS;

	if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
		zlog_debug("LSA[Refresh]: %s: next index %d", __func__,
			   ospf->lsa_refresh_queue.index);

	for (; i != ospf->lsa_refresh_queue.index;
	     i = (i + 1) % OSPF_LSA_REFRESHER_SLOTS) {
		if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
			zlog_debug("LSA[Refresh]: %s: refresh index %d",
				   __func__, i);

		refresh_list = ospf->lsa_refresh_queue.qs[i];

		assert(i >= 0);

		ospf->lsa_refresh_queue.qs[i] = NULL;

		if (refresh_list) {
			for (ALL_LIST_ELEMENTS(refresh_list, node, nnode,
					       lsa)) {
				if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
					zlog_debug(
						"LSA[Refresh:Type%d:%pI4]: %s: refresh lsa %p (slot %d)",
						lsa->data->type, &lsa->data->id,
						__func__, (void *)lsa, i);

				assert(lsa->lock > 0);
				list_delete_node(refresh_list, node);
				lsa->refresh_list = -1;
				listnode_add(lsa_to_refresh, lsa);
			}
			list_delete(&refresh_list);
		}
	}

	ospf->t_lsa_refresher = NULL;
	event_add_timer(master, ospf_lsa_refresh_walker, ospf,
			ospf->lsa_refresh_interval, &ospf->t_lsa_refresher);
	ospf->lsa_refresher_started = monotime(NULL);

	for (ALL_LIST_ELEMENTS(lsa_to_refresh, node, nnode, lsa)) {
		dna_lsa = ospf_check_dna_lsa(lsa);
		if (!dna_lsa) { /* refresh only non-DNA LSAs */
			ospf_lsa_refresh(ospf, lsa);
		}
		assert(lsa->lock > 0);
		ospf_lsa_unlock(&lsa); /* lsa_refresh_queue & temp for
					* lsa_to_refresh.
					*/
	}

	list_delete(&lsa_to_refresh);

	if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
		zlog_debug("LSA[Refresh]: %s: end", __func__);
}

/* Flush the LSAs for the specific area */
void ospf_flush_lsa_from_area(struct ospf *ospf, struct in_addr area_id,
			      int type)
{
	struct ospf_area *area;
	struct route_node *rn;
	struct ospf_lsa *lsa;

	area = ospf_area_get(ospf, area_id);

	switch (type) {
	case OSPF_AS_EXTERNAL_LSA:
		if ((area->external_routing == OSPF_AREA_NSSA) ||
		    (area->external_routing == OSPF_AREA_STUB)) {
			LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
				if (IS_LSA_SELF(lsa) &&
				    !(CHECK_FLAG(lsa->flags,
						 OSPF_LSA_LOCAL_XLT)))
					ospf_lsa_flush_area(lsa, area);
		}
		break;
	case OSPF_AS_NSSA_LSA:
		LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
			if (IS_LSA_SELF(lsa))
				ospf_lsa_flush_area(lsa, area);
		break;
	default:
		break;
	}
}