summaryrefslogtreecommitdiffstats
path: root/ospfd/ospf_snmp.c
blob: f1b876cfbe338c3da7d96ca071778bb9d7c8233e (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
/* OSPFv2 SNMP support
 * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
 * Copyright (C) 2000 IP Infusion Inc.
 *
 * Written by Kunihiro Ishiguro <kunihiro@zebra.org>
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <zebra.h>

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>

#include "if.h"
#include "log.h"
#include "prefix.h"
#include "table.h"
#include "command.h"
#include "memory.h"
#include "smux.h"
#include "libfrr.h"
#include "lib/version.h"

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

DEFINE_MTYPE_STATIC(OSPFD, SNMP, "OSPF SNMP");

/* OSPF2-MIB. */
#define OSPF2MIB 1,3,6,1,2,1,14

/* OSPF MIB General Group values. */
#define OSPFROUTERID                     1
#define OSPFADMINSTAT                    2
#define OSPFVERSIONNUMBER                3
#define OSPFAREABDRRTRSTATUS             4
#define OSPFASBDRRTRSTATUS               5
#define OSPFEXTERNLSACOUNT               6
#define OSPFEXTERNLSACKSUMSUM            7
#define OSPFTOSSUPPORT                   8
#define OSPFORIGINATENEWLSAS             9
#define OSPFRXNEWLSAS                    10
#define OSPFEXTLSDBLIMIT                 11
#define OSPFMULTICASTEXTENSIONS          12
#define OSPFEXITOVERFLOWINTERVAL         13
#define OSPFDEMANDEXTENSIONS             14

/* OSPF MIB ospfAreaTable. */
#define OSPFAREAID                       1
#define OSPFAUTHTYPE                     2
#define OSPFIMPORTASEXTERN               3
#define OSPFSPFRUNS                      4
#define OSPFAREABDRRTRCOUNT              5
#define OSPFASBDRRTRCOUNT                6
#define OSPFAREALSACOUNT                 7
#define OSPFAREALSACKSUMSUM              8
#define OSPFAREASUMMARY                  9
#define OSPFAREASTATUS                   10

/* OSPF MIB ospfStubAreaTable. */
#define OSPFSTUBAREAID                   1
#define OSPFSTUBTOS                      2
#define OSPFSTUBMETRIC                   3
#define OSPFSTUBSTATUS                   4
#define OSPFSTUBMETRICTYPE               5

/* OSPF MIB ospfLsdbTable. */
#define OSPFLSDBAREAID                   1
#define OSPFLSDBTYPE                     2
#define OSPFLSDBLSID                     3
#define OSPFLSDBROUTERID                 4
#define OSPFLSDBSEQUENCE                 5
#define OSPFLSDBAGE                      6
#define OSPFLSDBCHECKSUM                 7
#define OSPFLSDBADVERTISEMENT            8

/* OSPF MIB ospfAreaRangeTable. */
#define OSPFAREARANGEAREAID              1
#define OSPFAREARANGENET                 2
#define OSPFAREARANGEMASK                3
#define OSPFAREARANGESTATUS              4
#define OSPFAREARANGEEFFECT              5

/* OSPF MIB ospfHostTable. */
#define OSPFHOSTIPADDRESS                1
#define OSPFHOSTTOS                      2
#define OSPFHOSTMETRIC                   3
#define OSPFHOSTSTATUS                   4
#define OSPFHOSTAREAID                   5

/* OSPF MIB ospfIfTable. */
#define OSPFIFIPADDRESS                  1
#define OSPFADDRESSLESSIF                2
#define OSPFIFAREAID                     3
#define OSPFIFTYPE                       4
#define OSPFIFADMINSTAT                  5
#define OSPFIFRTRPRIORITY                6
#define OSPFIFTRANSITDELAY               7
#define OSPFIFRETRANSINTERVAL            8
#define OSPFIFHELLOINTERVAL              9
#define OSPFIFRTRDEADINTERVAL            10
#define OSPFIFPOLLINTERVAL               11
#define OSPFIFSTATE                      12
#define OSPFIFDESIGNATEDROUTER           13
#define OSPFIFBACKUPDESIGNATEDROUTER     14
#define OSPFIFEVENTS                     15
#define OSPFIFAUTHKEY                    16
#define OSPFIFSTATUS                     17
#define OSPFIFMULTICASTFORWARDING        18
#define OSPFIFDEMAND                     19
#define OSPFIFAUTHTYPE                   20

/* OSPF MIB ospfIfMetricTable. */
#define OSPFIFMETRICIPADDRESS            1
#define OSPFIFMETRICADDRESSLESSIF        2
#define OSPFIFMETRICTOS                  3
#define OSPFIFMETRICVALUE                4
#define OSPFIFMETRICSTATUS               5

/* OSPF MIB ospfVirtIfTable. */
#define OSPFVIRTIFAREAID                 1
#define OSPFVIRTIFNEIGHBOR               2
#define OSPFVIRTIFTRANSITDELAY           3
#define OSPFVIRTIFRETRANSINTERVAL        4
#define OSPFVIRTIFHELLOINTERVAL          5
#define OSPFVIRTIFRTRDEADINTERVAL        6
#define OSPFVIRTIFSTATE                  7
#define OSPFVIRTIFEVENTS                 8
#define OSPFVIRTIFAUTHKEY                9
#define OSPFVIRTIFSTATUS                 10
#define OSPFVIRTIFAUTHTYPE               11

/* OSPF MIB ospfNbrTable. */
#define OSPFNBRIPADDR                    1
#define OSPFNBRADDRESSLESSINDEX          2
#define OSPFNBRRTRID                     3
#define OSPFNBROPTIONS                   4
#define OSPFNBRPRIORITY                  5
#define OSPFNBRSTATE                     6
#define OSPFNBREVENTS                    7
#define OSPFNBRLSRETRANSQLEN             8
#define OSPFNBMANBRSTATUS                9
#define OSPFNBMANBRPERMANENCE            10
#define OSPFNBRHELLOSUPPRESSED           11

/* OSPF MIB ospfVirtNbrTable. */
#define OSPFVIRTNBRAREA                  1
#define OSPFVIRTNBRRTRID                 2
#define OSPFVIRTNBRIPADDR                3
#define OSPFVIRTNBROPTIONS               4
#define OSPFVIRTNBRSTATE                 5
#define OSPFVIRTNBREVENTS                6
#define OSPFVIRTNBRLSRETRANSQLEN         7
#define OSPFVIRTNBRHELLOSUPPRESSED       8

/* OSPF MIB ospfExtLsdbTable. */
#define OSPFEXTLSDBTYPE                  1
#define OSPFEXTLSDBLSID                  2
#define OSPFEXTLSDBROUTERID              3
#define OSPFEXTLSDBSEQUENCE              4
#define OSPFEXTLSDBAGE                   5
#define OSPFEXTLSDBCHECKSUM              6
#define OSPFEXTLSDBADVERTISEMENT         7

/* OSPF MIB ospfAreaAggregateTable. */
#define OSPFAREAAGGREGATEAREAID          1
#define OSPFAREAAGGREGATELSDBTYPE        2
#define OSPFAREAAGGREGATENET             3
#define OSPFAREAAGGREGATEMASK            4
#define OSPFAREAAGGREGATESTATUS          5
#define OSPFAREAAGGREGATEEFFECT          6

/* SYNTAX Status from OSPF-MIB. */
#define OSPF_STATUS_ENABLED  1
#define OSPF_STATUS_DISABLED 2

/* SNMP value hack. */
#define COUNTER     ASN_COUNTER
#define INTEGER     ASN_INTEGER
#define GAUGE       ASN_GAUGE
#define TIMETICKS   ASN_TIMETICKS
#define IPADDRESS   ASN_IPADDRESS
#define STRING      ASN_OCTET_STR

/* Because DR/DROther values are exhanged wrt RFC */
#define ISM_SNMP(x)                                                            \
	(((x) == ISM_DROther) ? ISM_DR : ((x) == ISM_DR) ? ISM_DROther : (x))

/* Declare static local variables for convenience. */
SNMP_LOCAL_VARIABLES

/* OSPF-MIB instances. */
static oid ospf_oid[] = {OSPF2MIB};
static oid ospf_trap_oid[] = {OSPF2MIB, 16, 2}; /* Not reverse mappable! */

/* IP address 0.0.0.0. */
static struct in_addr ospf_empty_addr = {.s_addr = 0};

/* Hook functions. */
static uint8_t *ospfGeneralGroup(struct variable *, oid *, size_t *, int,
				 size_t *, WriteMethod **);
static uint8_t *ospfAreaEntry(struct variable *, oid *, size_t *, int, size_t *,
			      WriteMethod **);
static uint8_t *ospfStubAreaEntry(struct variable *, oid *, size_t *, int,
				  size_t *, WriteMethod **);
static uint8_t *ospfLsdbEntry(struct variable *, oid *, size_t *, int, size_t *,
			      WriteMethod **);
static uint8_t *ospfAreaRangeEntry(struct variable *, oid *, size_t *, int,
				   size_t *, WriteMethod **);
static uint8_t *ospfHostEntry(struct variable *, oid *, size_t *, int, size_t *,
			      WriteMethod **);
static uint8_t *ospfIfEntry(struct variable *, oid *, size_t *, int, size_t *,
			    WriteMethod **);
static uint8_t *ospfIfMetricEntry(struct variable *, oid *, size_t *, int,
				  size_t *, WriteMethod **);
static uint8_t *ospfVirtIfEntry(struct variable *, oid *, size_t *, int,
				size_t *, WriteMethod **);
static uint8_t *ospfNbrEntry(struct variable *, oid *, size_t *, int, size_t *,
			     WriteMethod **);
static uint8_t *ospfVirtNbrEntry(struct variable *, oid *, size_t *, int,
				 size_t *, WriteMethod **);
static uint8_t *ospfExtLsdbEntry(struct variable *, oid *, size_t *, int,
				 size_t *, WriteMethod **);
static uint8_t *ospfAreaAggregateEntry(struct variable *, oid *, size_t *, int,
				       size_t *, WriteMethod **);

static struct variable ospf_variables[] = {
	/* OSPF general variables */
	{OSPFROUTERID, IPADDRESS, RWRITE, ospfGeneralGroup, 2, {1, 1}},
	{OSPFADMINSTAT, INTEGER, RWRITE, ospfGeneralGroup, 2, {1, 2}},
	{OSPFVERSIONNUMBER, INTEGER, RONLY, ospfGeneralGroup, 2, {1, 3}},
	{OSPFAREABDRRTRSTATUS, INTEGER, RONLY, ospfGeneralGroup, 2, {1, 4}},
	{OSPFASBDRRTRSTATUS, INTEGER, RWRITE, ospfGeneralGroup, 2, {1, 5}},
	{OSPFEXTERNLSACOUNT, GAUGE, RONLY, ospfGeneralGroup, 2, {1, 6}},
	{OSPFEXTERNLSACKSUMSUM, INTEGER, RONLY, ospfGeneralGroup, 2, {1, 7}},
	{OSPFTOSSUPPORT, INTEGER, RWRITE, ospfGeneralGroup, 2, {1, 8}},
	{OSPFORIGINATENEWLSAS, COUNTER, RONLY, ospfGeneralGroup, 2, {1, 9}},
	{OSPFRXNEWLSAS, COUNTER, RONLY, ospfGeneralGroup, 2, {1, 10}},
	{OSPFEXTLSDBLIMIT, INTEGER, RWRITE, ospfGeneralGroup, 2, {1, 11}},
	{OSPFMULTICASTEXTENSIONS,
	 INTEGER,
	 RWRITE,
	 ospfGeneralGroup,
	 2,
	 {1, 12}},
	{OSPFEXITOVERFLOWINTERVAL,
	 INTEGER,
	 RWRITE,
	 ospfGeneralGroup,
	 2,
	 {1, 13}},
	{OSPFDEMANDEXTENSIONS, INTEGER, RWRITE, ospfGeneralGroup, 2, {1, 14}},

	/* OSPF area data structure. */
	{OSPFAREAID, IPADDRESS, RONLY, ospfAreaEntry, 3, {2, 1, 1}},
	{OSPFAUTHTYPE, INTEGER, RWRITE, ospfAreaEntry, 3, {2, 1, 2}},
	{OSPFIMPORTASEXTERN, INTEGER, RWRITE, ospfAreaEntry, 3, {2, 1, 3}},
	{OSPFSPFRUNS, COUNTER, RONLY, ospfAreaEntry, 3, {2, 1, 4}},
	{OSPFAREABDRRTRCOUNT, GAUGE, RONLY, ospfAreaEntry, 3, {2, 1, 5}},
	{OSPFASBDRRTRCOUNT, GAUGE, RONLY, ospfAreaEntry, 3, {2, 1, 6}},
	{OSPFAREALSACOUNT, GAUGE, RONLY, ospfAreaEntry, 3, {2, 1, 7}},
	{OSPFAREALSACKSUMSUM, INTEGER, RONLY, ospfAreaEntry, 3, {2, 1, 8}},
	{OSPFAREASUMMARY, INTEGER, RWRITE, ospfAreaEntry, 3, {2, 1, 9}},
	{OSPFAREASTATUS, INTEGER, RWRITE, ospfAreaEntry, 3, {2, 1, 10}},

	/* OSPF stub area information. */
	{OSPFSTUBAREAID, IPADDRESS, RONLY, ospfStubAreaEntry, 3, {3, 1, 1}},
	{OSPFSTUBTOS, INTEGER, RONLY, ospfStubAreaEntry, 3, {3, 1, 2}},
	{OSPFSTUBMETRIC, INTEGER, RWRITE, ospfStubAreaEntry, 3, {3, 1, 3}},
	{OSPFSTUBSTATUS, INTEGER, RWRITE, ospfStubAreaEntry, 3, {3, 1, 4}},
	{OSPFSTUBMETRICTYPE, INTEGER, RWRITE, ospfStubAreaEntry, 3, {3, 1, 5}},

	/* OSPF link state database. */
	{OSPFLSDBAREAID, IPADDRESS, RONLY, ospfLsdbEntry, 3, {4, 1, 1}},
	{OSPFLSDBTYPE, INTEGER, RONLY, ospfLsdbEntry, 3, {4, 1, 2}},
	{OSPFLSDBLSID, IPADDRESS, RONLY, ospfLsdbEntry, 3, {4, 1, 3}},
	{OSPFLSDBROUTERID, IPADDRESS, RONLY, ospfLsdbEntry, 3, {4, 1, 4}},
	{OSPFLSDBSEQUENCE, INTEGER, RONLY, ospfLsdbEntry, 3, {4, 1, 5}},
	{OSPFLSDBAGE, INTEGER, RONLY, ospfLsdbEntry, 3, {4, 1, 6}},
	{OSPFLSDBCHECKSUM, INTEGER, RONLY, ospfLsdbEntry, 3, {4, 1, 7}},
	{OSPFLSDBADVERTISEMENT, STRING, RONLY, ospfLsdbEntry, 3, {4, 1, 8}},

	/* Area range table. */
	{OSPFAREARANGEAREAID,
	 IPADDRESS,
	 RONLY,
	 ospfAreaRangeEntry,
	 3,
	 {5, 1, 1}},
	{OSPFAREARANGENET, IPADDRESS, RONLY, ospfAreaRangeEntry, 3, {5, 1, 2}},
	{OSPFAREARANGEMASK,
	 IPADDRESS,
	 RWRITE,
	 ospfAreaRangeEntry,
	 3,
	 {5, 1, 3}},
	{OSPFAREARANGESTATUS,
	 INTEGER,
	 RWRITE,
	 ospfAreaRangeEntry,
	 3,
	 {5, 1, 4}},
	{OSPFAREARANGEEFFECT,
	 INTEGER,
	 RWRITE,
	 ospfAreaRangeEntry,
	 3,
	 {5, 1, 5}},

	/* OSPF host table. */
	{OSPFHOSTIPADDRESS, IPADDRESS, RONLY, ospfHostEntry, 3, {6, 1, 1}},
	{OSPFHOSTTOS, INTEGER, RONLY, ospfHostEntry, 3, {6, 1, 2}},
	{OSPFHOSTMETRIC, INTEGER, RWRITE, ospfHostEntry, 3, {6, 1, 3}},
	{OSPFHOSTSTATUS, INTEGER, RWRITE, ospfHostEntry, 3, {6, 1, 4}},
	{OSPFHOSTAREAID, IPADDRESS, RONLY, ospfHostEntry, 3, {6, 1, 5}},

	/* OSPF interface table. */
	{OSPFIFIPADDRESS, IPADDRESS, RONLY, ospfIfEntry, 3, {7, 1, 1}},
	{OSPFADDRESSLESSIF, INTEGER, RONLY, ospfIfEntry, 3, {7, 1, 2}},
	{OSPFIFAREAID, IPADDRESS, RWRITE, ospfIfEntry, 3, {7, 1, 3}},
	{OSPFIFTYPE, INTEGER, RWRITE, ospfIfEntry, 3, {7, 1, 4}},
	{OSPFIFADMINSTAT, INTEGER, RWRITE, ospfIfEntry, 3, {7, 1, 5}},
	{OSPFIFRTRPRIORITY, INTEGER, RWRITE, ospfIfEntry, 3, {7, 1, 6}},
	{OSPFIFTRANSITDELAY, INTEGER, RWRITE, ospfIfEntry, 3, {7, 1, 7}},
	{OSPFIFRETRANSINTERVAL, INTEGER, RWRITE, ospfIfEntry, 3, {7, 1, 8}},
	{OSPFIFHELLOINTERVAL, INTEGER, RWRITE, ospfIfEntry, 3, {7, 1, 9}},
	{OSPFIFRTRDEADINTERVAL, INTEGER, RWRITE, ospfIfEntry, 3, {7, 1, 10}},
	{OSPFIFPOLLINTERVAL, INTEGER, RWRITE, ospfIfEntry, 3, {7, 1, 11}},
	{OSPFIFSTATE, INTEGER, RONLY, ospfIfEntry, 3, {7, 1, 12}},
	{OSPFIFDESIGNATEDROUTER, IPADDRESS, RONLY, ospfIfEntry, 3, {7, 1, 13}},
	{OSPFIFBACKUPDESIGNATEDROUTER,
	 IPADDRESS,
	 RONLY,
	 ospfIfEntry,
	 3,
	 {7, 1, 14}},
	{OSPFIFEVENTS, COUNTER, RONLY, ospfIfEntry, 3, {7, 1, 15}},
	{OSPFIFAUTHKEY, STRING, RWRITE, ospfIfEntry, 3, {7, 1, 16}},
	{OSPFIFSTATUS, INTEGER, RWRITE, ospfIfEntry, 3, {7, 1, 17}},
	{OSPFIFMULTICASTFORWARDING,
	 INTEGER,
	 RWRITE,
	 ospfIfEntry,
	 3,
	 {7, 1, 18}},
	{OSPFIFDEMAND, INTEGER, RWRITE, ospfIfEntry, 3, {7, 1, 19}},
	{OSPFIFAUTHTYPE, INTEGER, RWRITE, ospfIfEntry, 3, {7, 1, 20}},

	/* OSPF interface metric table. */
	{OSPFIFMETRICIPADDRESS,
	 IPADDRESS,
	 RONLY,
	 ospfIfMetricEntry,
	 3,
	 {8, 1, 1}},
	{OSPFIFMETRICADDRESSLESSIF,
	 INTEGER,
	 RONLY,
	 ospfIfMetricEntry,
	 3,
	 {8, 1, 2}},
	{OSPFIFMETRICTOS, INTEGER, RONLY, ospfIfMetricEntry, 3, {8, 1, 3}},
	{OSPFIFMETRICVALUE, INTEGER, RWRITE, ospfIfMetricEntry, 3, {8, 1, 4}},
	{OSPFIFMETRICSTATUS, INTEGER, RWRITE, ospfIfMetricEntry, 3, {8, 1, 5}},

	/* OSPF virtual interface table. */
	{OSPFVIRTIFAREAID, IPADDRESS, RONLY, ospfVirtIfEntry, 3, {9, 1, 1}},
	{OSPFVIRTIFNEIGHBOR, IPADDRESS, RONLY, ospfVirtIfEntry, 3, {9, 1, 2}},
	{OSPFVIRTIFTRANSITDELAY,
	 INTEGER,
	 RWRITE,
	 ospfVirtIfEntry,
	 3,
	 {9, 1, 3}},
	{OSPFVIRTIFRETRANSINTERVAL,
	 INTEGER,
	 RWRITE,
	 ospfVirtIfEntry,
	 3,
	 {9, 1, 4}},
	{OSPFVIRTIFHELLOINTERVAL,
	 INTEGER,
	 RWRITE,
	 ospfVirtIfEntry,
	 3,
	 {9, 1, 5}},
	{OSPFVIRTIFRTRDEADINTERVAL,
	 INTEGER,
	 RWRITE,
	 ospfVirtIfEntry,
	 3,
	 {9, 1, 6}},
	{OSPFVIRTIFSTATE, INTEGER, RONLY, ospfVirtIfEntry, 3, {9, 1, 7}},
	{OSPFVIRTIFEVENTS, COUNTER, RONLY, ospfVirtIfEntry, 3, {9, 1, 8}},
	{OSPFVIRTIFAUTHKEY, STRING, RWRITE, ospfVirtIfEntry, 3, {9, 1, 9}},
	{OSPFVIRTIFSTATUS, INTEGER, RWRITE, ospfVirtIfEntry, 3, {9, 1, 10}},
	{OSPFVIRTIFAUTHTYPE, INTEGER, RWRITE, ospfVirtIfEntry, 3, {9, 1, 11}},

	/* OSPF neighbor table. */
	{OSPFNBRIPADDR, IPADDRESS, RONLY, ospfNbrEntry, 3, {10, 1, 1}},
	{OSPFNBRADDRESSLESSINDEX, INTEGER, RONLY, ospfNbrEntry, 3, {10, 1, 2}},
	{OSPFNBRRTRID, IPADDRESS, RONLY, ospfNbrEntry, 3, {10, 1, 3}},
	{OSPFNBROPTIONS, INTEGER, RONLY, ospfNbrEntry, 3, {10, 1, 4}},
	{OSPFNBRPRIORITY, INTEGER, RWRITE, ospfNbrEntry, 3, {10, 1, 5}},
	{OSPFNBRSTATE, INTEGER, RONLY, ospfNbrEntry, 3, {10, 1, 6}},
	{OSPFNBREVENTS, COUNTER, RONLY, ospfNbrEntry, 3, {10, 1, 7}},
	{OSPFNBRLSRETRANSQLEN, GAUGE, RONLY, ospfNbrEntry, 3, {10, 1, 8}},
	{OSPFNBMANBRSTATUS, INTEGER, RWRITE, ospfNbrEntry, 3, {10, 1, 9}},
	{OSPFNBMANBRPERMANENCE, INTEGER, RONLY, ospfNbrEntry, 3, {10, 1, 10}},
	{OSPFNBRHELLOSUPPRESSED, INTEGER, RONLY, ospfNbrEntry, 3, {10, 1, 11}},

	/* OSPF virtual neighbor table. */
	{OSPFVIRTNBRAREA, IPADDRESS, RONLY, ospfVirtNbrEntry, 3, {11, 1, 1}},
	{OSPFVIRTNBRRTRID, IPADDRESS, RONLY, ospfVirtNbrEntry, 3, {11, 1, 2}},
	{OSPFVIRTNBRIPADDR, IPADDRESS, RONLY, ospfVirtNbrEntry, 3, {11, 1, 3}},
	{OSPFVIRTNBROPTIONS, INTEGER, RONLY, ospfVirtNbrEntry, 3, {11, 1, 4}},
	{OSPFVIRTNBRSTATE, INTEGER, RONLY, ospfVirtNbrEntry, 3, {11, 1, 5}},
	{OSPFVIRTNBREVENTS, COUNTER, RONLY, ospfVirtNbrEntry, 3, {11, 1, 6}},
	{OSPFVIRTNBRLSRETRANSQLEN,
	 INTEGER,
	 RONLY,
	 ospfVirtNbrEntry,
	 3,
	 {11, 1, 7}},
	{OSPFVIRTNBRHELLOSUPPRESSED,
	 INTEGER,
	 RONLY,
	 ospfVirtNbrEntry,
	 3,
	 {11, 1, 8}},

	/* OSPF link state database, external. */
	{OSPFEXTLSDBTYPE, INTEGER, RONLY, ospfExtLsdbEntry, 3, {12, 1, 1}},
	{OSPFEXTLSDBLSID, IPADDRESS, RONLY, ospfExtLsdbEntry, 3, {12, 1, 2}},
	{OSPFEXTLSDBROUTERID,
	 IPADDRESS,
	 RONLY,
	 ospfExtLsdbEntry,
	 3,
	 {12, 1, 3}},
	{OSPFEXTLSDBSEQUENCE, INTEGER, RONLY, ospfExtLsdbEntry, 3, {12, 1, 4}},
	{OSPFEXTLSDBAGE, INTEGER, RONLY, ospfExtLsdbEntry, 3, {12, 1, 5}},
	{OSPFEXTLSDBCHECKSUM, INTEGER, RONLY, ospfExtLsdbEntry, 3, {12, 1, 6}},
	{OSPFEXTLSDBADVERTISEMENT,
	 STRING,
	 RONLY,
	 ospfExtLsdbEntry,
	 3,
	 {12, 1, 7}},

	/* OSPF area aggregate table. */
	{OSPFAREAAGGREGATEAREAID,
	 IPADDRESS,
	 RONLY,
	 ospfAreaAggregateEntry,
	 3,
	 {14, 1, 1}},
	{OSPFAREAAGGREGATELSDBTYPE,
	 INTEGER,
	 RONLY,
	 ospfAreaAggregateEntry,
	 3,
	 {14, 1, 2}},
	{OSPFAREAAGGREGATENET,
	 IPADDRESS,
	 RONLY,
	 ospfAreaAggregateEntry,
	 3,
	 {14, 1, 3}},
	{OSPFAREAAGGREGATEMASK,
	 IPADDRESS,
	 RONLY,
	 ospfAreaAggregateEntry,
	 3,
	 {14, 1, 4}},
	{OSPFAREAAGGREGATESTATUS,
	 INTEGER,
	 RWRITE,
	 ospfAreaAggregateEntry,
	 3,
	 {14, 1, 5}},
	{OSPFAREAAGGREGATEEFFECT,
	 INTEGER,
	 RWRITE,
	 ospfAreaAggregateEntry,
	 3,
	 {14, 1, 6}}};

/* The administrative status of OSPF.  When OSPF is enbled on at least
   one interface return 1. */
static int ospf_admin_stat(struct ospf *ospf)
{
	struct listnode *node;
	struct ospf_interface *oi;

	if (ospf == NULL)
		return 0;

	for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
		if (oi && oi->address)
			return 1;

	return 0;
}

static uint8_t *ospfGeneralGroup(struct variable *v, oid *name, size_t *length,
				 int exact, size_t *var_len,
				 WriteMethod **write_method)
{
	struct ospf *ospf;

	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);

	/* Check whether the instance identifier is valid */
	if (smux_header_generic(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	/* Return the current value of the variable */
	switch (v->magic) {
	case OSPFROUTERID: /* 1 */
		/* Router-ID of this OSPF instance. */
		if (ospf)
			return SNMP_IPADDRESS(ospf->router_id);
		else
			return SNMP_IPADDRESS(ospf_empty_addr);
	case OSPFADMINSTAT: /* 2 */
		/* The administrative status of OSPF in the router. */
		if (ospf_admin_stat(ospf))
			return SNMP_INTEGER(OSPF_STATUS_ENABLED);
		else
			return SNMP_INTEGER(OSPF_STATUS_DISABLED);
	case OSPFVERSIONNUMBER: /* 3 */
		/* OSPF version 2. */
		return SNMP_INTEGER(OSPF_VERSION);
	case OSPFAREABDRRTRSTATUS: /* 4 */
		/* Area Border router status. */
		if (ospf && CHECK_FLAG(ospf->flags, OSPF_FLAG_ABR))
			return SNMP_INTEGER(SNMP_TRUE);
		else
			return SNMP_INTEGER(SNMP_FALSE);
	case OSPFASBDRRTRSTATUS: /* 5 */
		/* AS Border router status. */
		if (ospf && CHECK_FLAG(ospf->flags, OSPF_FLAG_ASBR))
			return SNMP_INTEGER(SNMP_TRUE);
		else
			return SNMP_INTEGER(SNMP_FALSE);
	case OSPFEXTERNLSACOUNT: /* 6 */
		/* External LSA counts. */
		if (ospf)
			return SNMP_INTEGER(ospf_lsdb_count_all(ospf->lsdb));
		else
			return SNMP_INTEGER(0);
	case OSPFEXTERNLSACKSUMSUM: /* 7 */
		/* External LSA checksum. */
		return SNMP_INTEGER(0);
	case OSPFTOSSUPPORT: /* 8 */
		/* TOS is not supported. */
		return SNMP_INTEGER(SNMP_FALSE);
	case OSPFORIGINATENEWLSAS: /* 9 */
		/* The number of new link-state advertisements. */
		if (ospf)
			return SNMP_INTEGER(ospf->lsa_originate_count);
		else
			return SNMP_INTEGER(0);
	case OSPFRXNEWLSAS: /* 10 */
		/* The number of link-state advertisements received determined
		   to be new instantiations. */
		if (ospf)
			return SNMP_INTEGER(ospf->rx_lsa_count);
		else
			return SNMP_INTEGER(0);
	case OSPFEXTLSDBLIMIT: /* 11 */
		/* There is no limit for the number of non-default
		   AS-external-LSAs. */
		return SNMP_INTEGER(-1);
	case OSPFMULTICASTEXTENSIONS: /* 12 */
		/* Multicast Extensions to OSPF is not supported. */
		return SNMP_INTEGER(0);
	case OSPFEXITOVERFLOWINTERVAL: /* 13 */
		/* Overflow is not supported. */
		return SNMP_INTEGER(0);
	case OSPFDEMANDEXTENSIONS: /* 14 */
		/* Demand routing is not supported. */
		return SNMP_INTEGER(SNMP_FALSE);
	default:
		return NULL;
	}
	return NULL;
}

static struct ospf_area *
ospf_area_lookup_next(struct ospf *ospf, struct in_addr *area_id, int first)
{
	struct ospf_area *area;
	struct listnode *node;

	if (ospf == NULL)
		return NULL;

	if (first) {
		node = listhead(ospf->areas);
		if (node) {
			area = listgetdata(node);
			*area_id = area->area_id;
			return area;
		}
		return NULL;
	}
	for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
		if (ntohl(area->area_id.s_addr) > ntohl(area_id->s_addr)) {
			*area_id = area->area_id;
			return area;
		}
	}
	return NULL;
}

static struct ospf_area *ospfAreaLookup(struct variable *v, oid name[],
					size_t *length, struct in_addr *addr,
					int exact)
{
	struct ospf *ospf;
	struct ospf_area *area;
	int len;

	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	if (ospf == NULL)
		return NULL;

	if (exact) {
		/* Length is insufficient to lookup OSPF area. */
		if (*length - v->namelen != sizeof(struct in_addr))
			return NULL;

		oid2in_addr(name + v->namelen, sizeof(struct in_addr), addr);

		area = ospf_area_lookup_by_area_id(ospf, *addr);

		return area;
	} else {
		len = *length - v->namelen;
		if (len > 4)
			len = 4;

		oid2in_addr(name + v->namelen, len, addr);

		area = ospf_area_lookup_next(ospf, addr, len == 0 ? 1 : 0);

		if (area == NULL)
			return NULL;

		oid_copy_in_addr(name + v->namelen, addr);
		*length = sizeof(struct in_addr) + v->namelen;

		return area;
	}
	return NULL;
}

static uint8_t *ospfAreaEntry(struct variable *v, oid *name, size_t *length,
			      int exact, size_t *var_len,
			      WriteMethod **write_method)
{
	struct ospf_area *area;
	struct in_addr addr;

	if (smux_header_table(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	memset(&addr, 0, sizeof(addr));

	area = ospfAreaLookup(v, name, length, &addr, exact);
	if (!area)
		return NULL;

	/* Return the current value of the variable */
	switch (v->magic) {
	case OSPFAREAID: /* 1 */
		return SNMP_IPADDRESS(area->area_id);
	case OSPFAUTHTYPE: /* 2 */
		return SNMP_INTEGER(area->auth_type);
	case OSPFIMPORTASEXTERN: /* 3 */
		return SNMP_INTEGER(area->external_routing + 1);
	case OSPFSPFRUNS: /* 4 */
		return SNMP_INTEGER(area->spf_calculation);
	case OSPFAREABDRRTRCOUNT: /* 5 */
		return SNMP_INTEGER(area->abr_count);
	case OSPFASBDRRTRCOUNT: /* 6 */
		return SNMP_INTEGER(area->asbr_count);
	case OSPFAREALSACOUNT: /* 7 */
		return SNMP_INTEGER(area->lsdb->total);
	case OSPFAREALSACKSUMSUM: /* 8 */
		return SNMP_INTEGER(0);
	case OSPFAREASUMMARY: /* 9 */
#define OSPF_noAreaSummary   1
#define OSPF_sendAreaSummary 2
		if (area->no_summary)
			return SNMP_INTEGER(OSPF_noAreaSummary);
		else
			return SNMP_INTEGER(OSPF_sendAreaSummary);
	case OSPFAREASTATUS: /* 10 */
		return SNMP_INTEGER(SNMP_VALID);
	default:
		return NULL;
	}
	return NULL;
}

static struct ospf_area *ospf_stub_area_lookup_next(struct in_addr *area_id,
						    int first)
{
	struct ospf_area *area;
	struct listnode *node;
	struct ospf *ospf;

	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	if (ospf == NULL)
		return NULL;

	for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
		if (area->external_routing == OSPF_AREA_STUB) {
			if (first) {
				*area_id = area->area_id;
				return area;
			} else if (ntohl(area->area_id.s_addr)
				   > ntohl(area_id->s_addr)) {
				*area_id = area->area_id;
				return area;
			}
		}
	}
	return NULL;
}

static struct ospf_area *ospfStubAreaLookup(struct variable *v, oid name[],
					    size_t *length,
					    struct in_addr *addr, int exact)
{
	struct ospf *ospf;
	struct ospf_area *area;
	int len;

	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	if (ospf == NULL)
		return NULL;

	/* Exact lookup. */
	if (exact) {
		/* ospfStubAreaID + ospfStubTOS. */
		if (*length != v->namelen + sizeof(struct in_addr) + 1)
			return NULL;

		/* Check ospfStubTOS is zero. */
		if (name[*length - 1] != 0)
			return NULL;

		oid2in_addr(name + v->namelen, sizeof(struct in_addr), addr);

		area = ospf_area_lookup_by_area_id(ospf, *addr);

		if (area && area->external_routing == OSPF_AREA_STUB)
			return area;
		else
			return NULL;
	} else {
		len = *length - v->namelen;
		if (len > 4)
			len = 4;

		oid2in_addr(name + v->namelen, len, addr);

		area = ospf_stub_area_lookup_next(addr, len == 0 ? 1 : 0);

		if (area == NULL)
			return NULL;

		oid_copy_in_addr(name + v->namelen, addr);
		/* Set TOS 0. */
		name[v->namelen + sizeof(struct in_addr)] = 0;
		*length = v->namelen + sizeof(struct in_addr) + 1;

		return area;
	}
	return NULL;
}

static uint8_t *ospfStubAreaEntry(struct variable *v, oid *name, size_t *length,
				  int exact, size_t *var_len,
				  WriteMethod **write_method)
{
	struct ospf_area *area;
	struct in_addr addr;

	if (smux_header_table(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	memset(&addr, 0, sizeof(addr));

	area = ospfStubAreaLookup(v, name, length, &addr, exact);
	if (!area)
		return NULL;

	/* Return the current value of the variable */
	switch (v->magic) {
	case OSPFSTUBAREAID: /* 1 */
		/* OSPF stub area id. */
		return SNMP_IPADDRESS(area->area_id);
	case OSPFSTUBTOS: /* 2 */
		/* TOS value is not supported. */
		return SNMP_INTEGER(0);
	case OSPFSTUBMETRIC: /* 3 */
		/* Default cost to stub area. */
		return SNMP_INTEGER(area->default_cost);
	case OSPFSTUBSTATUS: /* 4 */
		/* Status of the stub area. */
		return SNMP_INTEGER(SNMP_VALID);
	case OSPFSTUBMETRICTYPE: /* 5 */
				 /* OSPF Metric type. */
#define OSPF_ospfMetric     1
#define OSPF_comparableCost 2
#define OSPF_nonComparable  3
		return SNMP_INTEGER(OSPF_ospfMetric);
	default:
		return NULL;
	}
	return NULL;
}

static struct ospf_lsa *lsdb_lookup_next(struct ospf_area *area, uint8_t *type,
					 int type_next, struct in_addr *ls_id,
					 int ls_id_next,
					 struct in_addr *router_id,
					 int router_id_next)
{
	struct ospf_lsa *lsa;
	int i;

	if (type_next)
		i = OSPF_MIN_LSA;
	else
		i = *type;

	/* Sanity check, if LSA type unknwon
	   merley skip any LSA */
	if ((i < OSPF_MIN_LSA) || (i >= OSPF_MAX_LSA)) {
		zlog_debug("Strange request with LSA type %d", i);
		return NULL;
	}

	for (; i < OSPF_MAX_LSA; i++) {
		*type = i;

		lsa = ospf_lsdb_lookup_by_id_next(area->lsdb, *type, *ls_id,
						  *router_id, ls_id_next);
		if (lsa)
			return lsa;

		ls_id_next = 1;
	}
	return NULL;
}

static struct ospf_lsa *ospfLsdbLookup(struct variable *v, oid *name,
				       size_t *length, struct in_addr *area_id,
				       uint8_t *type, struct in_addr *ls_id,
				       struct in_addr *router_id, int exact)
{
	struct ospf *ospf;
	struct ospf_area *area;
	struct ospf_lsa *lsa;
	int len;
	int type_next;
	int ls_id_next;
	int router_id_next;
	oid *offset;
	int offsetlen;

	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);

#define OSPF_LSDB_ENTRY_OFFSET (IN_ADDR_SIZE + 1 + IN_ADDR_SIZE + IN_ADDR_SIZE)

	if (exact) {
		/* Area ID + Type + LS ID + Router ID. */
		if (*length - v->namelen != OSPF_LSDB_ENTRY_OFFSET)
			return NULL;

		/* Set OID offset for Area ID. */
		offset = name + v->namelen;

		/* Lookup area first. */
		oid2in_addr(offset, IN_ADDR_SIZE, area_id);
		area = ospf_area_lookup_by_area_id(ospf, *area_id);
		if (!area)
			return NULL;
		offset += IN_ADDR_SIZE;

		/* Type. */
		*type = *offset;
		offset++;

		/* LS ID. */
		oid2in_addr(offset, IN_ADDR_SIZE, ls_id);
		offset += IN_ADDR_SIZE;

		/* Router ID. */
		oid2in_addr(offset, IN_ADDR_SIZE, router_id);

		/* Lookup LSDB. */
		return ospf_lsdb_lookup_by_id(area->lsdb, *type, *ls_id,
					      *router_id);
	} else {
		/* Get variable length. */
		offset = name + v->namelen;
		offsetlen = *length - v->namelen;
		len = offsetlen;

		if (len > (int)IN_ADDR_SIZE)
			len = IN_ADDR_SIZE;

		oid2in_addr(offset, len, area_id);

		/* First we search area. */
		if (len == IN_ADDR_SIZE)
			area = ospf_area_lookup_by_area_id(ospf, *area_id);
		else
			area = ospf_area_lookup_next(ospf, area_id, 1);

		if (area == NULL)
			return NULL;

		do {
			/* Next we lookup type. */
			offset += len;
			offsetlen -= len;
			len = offsetlen;

			if (len <= 0)
				type_next = 1;
			else {
				type_next = 0;
				*type = *offset;
			}

			/* LS ID. */
			offset++;
			offsetlen--;
			len = offsetlen;

			if (len <= 0)
				ls_id_next = 1;
			else {
				ls_id_next = 0;
				if (len > (int)IN_ADDR_SIZE)
					len = IN_ADDR_SIZE;

				oid2in_addr(offset, len, ls_id);
			}

			/* Router ID. */
			offset += IN_ADDR_SIZE;
			offsetlen -= IN_ADDR_SIZE;
			len = offsetlen;

			if (len <= 0)
				router_id_next = 1;
			else {
				router_id_next = 0;
				if (len > (int)IN_ADDR_SIZE)
					len = IN_ADDR_SIZE;

				oid2in_addr(offset, len, router_id);
			}

			lsa = lsdb_lookup_next(area, type, type_next, ls_id,
					       ls_id_next, router_id,
					       router_id_next);

			if (lsa) {
				/* Fill in length. */
				*length = v->namelen + OSPF_LSDB_ENTRY_OFFSET;

				/* Fill in value. */
				offset = name + v->namelen;
				oid_copy_in_addr(offset, area_id);
				offset += IN_ADDR_SIZE;
				*offset = lsa->data->type;
				offset++;
				oid_copy_in_addr(offset, &lsa->data->id);
				offset += IN_ADDR_SIZE;
				oid_copy_in_addr(offset,
						 &lsa->data->adv_router);

				return lsa;
			}
		} while ((area = ospf_area_lookup_next(ospf, area_id, 0))
			 != NULL);
	}
	return NULL;
}

static uint8_t *ospfLsdbEntry(struct variable *v, oid *name, size_t *length,
			      int exact, size_t *var_len,
			      WriteMethod **write_method)
{
	struct ospf_lsa *lsa;
	struct lsa_header *lsah;
	struct in_addr area_id;
	uint8_t type;
	struct in_addr ls_id;
	struct in_addr router_id;
	struct ospf *ospf;

	if (smux_header_table(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	/* INDEX { ospfLsdbAreaId, ospfLsdbType,
	   ospfLsdbLsid, ospfLsdbRouterId } */

	memset(&area_id, 0, sizeof(area_id));
	type = 0;
	memset(&ls_id, 0, sizeof(ls_id));
	memset(&router_id, 0, sizeof(router_id));

	/* Check OSPF instance. */
	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	if (ospf == NULL)
		return NULL;

	lsa = ospfLsdbLookup(v, name, length, &area_id, &type, &ls_id,
			     &router_id, exact);
	if (!lsa)
		return NULL;

	lsah = lsa->data;

	/* Return the current value of the variable */
	switch (v->magic) {
	case OSPFLSDBAREAID: /* 1 */
		return SNMP_IPADDRESS(lsa->area->area_id);
	case OSPFLSDBTYPE: /* 2 */
		return SNMP_INTEGER(lsah->type);
	case OSPFLSDBLSID: /* 3 */
		return SNMP_IPADDRESS(lsah->id);
	case OSPFLSDBROUTERID: /* 4 */
		return SNMP_IPADDRESS(lsah->adv_router);
	case OSPFLSDBSEQUENCE: /* 5 */
		return SNMP_INTEGER(lsah->ls_seqnum);
	case OSPFLSDBAGE: /* 6 */
		return SNMP_INTEGER(lsah->ls_age);
	case OSPFLSDBCHECKSUM: /* 7 */
		return SNMP_INTEGER(lsah->checksum);
	case OSPFLSDBADVERTISEMENT: /* 8 */
		*var_len = ntohs(lsah->length);
		return (uint8_t *)lsah;
	default:
		return NULL;
	}
	return NULL;
}

static struct ospf_area_range *ospfAreaRangeLookup(struct variable *v,
						   oid *name, size_t *length,
						   struct in_addr *area_id,
						   struct in_addr *range_net,
						   int exact)
{
	oid *offset;
	int offsetlen;
	int len;
	struct ospf *ospf;
	struct ospf_area *area;
	struct ospf_area_range *range;
	struct prefix_ipv4 p;
	p.family = AF_INET;
	p.prefixlen = IPV4_MAX_BITLEN;

	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);

	if (exact) {
		/* Area ID + Range Network. */
		if (v->namelen + IN_ADDR_SIZE + IN_ADDR_SIZE != *length)
			return NULL;

		/* Set OID offset for Area ID. */
		offset = name + v->namelen;

		/* Lookup area first. */
		oid2in_addr(offset, IN_ADDR_SIZE, area_id);

		area = ospf_area_lookup_by_area_id(ospf, *area_id);
		if (!area)
			return NULL;

		offset += IN_ADDR_SIZE;

		/* Lookup area range. */
		oid2in_addr(offset, IN_ADDR_SIZE, range_net);
		p.prefix = *range_net;

		return ospf_area_range_lookup(area, &p);
	} else {
		/* Set OID offset for Area ID. */
		offset = name + v->namelen;
		offsetlen = *length - v->namelen;

		len = offsetlen;
		if (len > (int)IN_ADDR_SIZE)
			len = IN_ADDR_SIZE;

		oid2in_addr(offset, len, area_id);

		/* First we search area. */
		if (len == IN_ADDR_SIZE)
			area = ospf_area_lookup_by_area_id(ospf, *area_id);
		else
			area = ospf_area_lookup_next(ospf, area_id,
						     len == 0 ? 1 : 0);

		if (area == NULL)
			return NULL;

		do {
			offset += IN_ADDR_SIZE;
			offsetlen -= IN_ADDR_SIZE;
			len = offsetlen;

			if (len < 0)
				len = 0;
			if (len > (int)IN_ADDR_SIZE)
				len = IN_ADDR_SIZE;

			oid2in_addr(offset, len, range_net);

			range = ospf_area_range_lookup_next(area, range_net,
							    len == 0 ? 1 : 0);

			if (range) {
				/* Fill in length. */
				*length = v->namelen + IN_ADDR_SIZE
					  + IN_ADDR_SIZE;

				/* Fill in value. */
				offset = name + v->namelen;
				oid_copy_in_addr(offset, area_id);
				offset += IN_ADDR_SIZE;
				oid_copy_in_addr(offset, range_net);

				return range;
			}
		} while ((area = ospf_area_lookup_next(ospf, area_id, 0))
			 != NULL);
	}
	return NULL;
}

static uint8_t *ospfAreaRangeEntry(struct variable *v, oid *name,
				   size_t *length, int exact, size_t *var_len,
				   WriteMethod **write_method)
{
	struct ospf_area_range *range;
	struct in_addr area_id;
	struct in_addr range_net;
	struct in_addr mask;
	struct ospf *ospf;

	if (smux_header_table(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	/* Check OSPF instance. */
	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	if (ospf == NULL)
		return NULL;

	memset(&area_id, 0, IN_ADDR_SIZE);
	memset(&range_net, 0, IN_ADDR_SIZE);

	range = ospfAreaRangeLookup(v, name, length, &area_id, &range_net,
				    exact);
	if (!range)
		return NULL;

	/* Convert prefixlen to network mask format. */
	masklen2ip(range->subst_masklen, &mask);

	/* Return the current value of the variable */
	switch (v->magic) {
	case OSPFAREARANGEAREAID: /* 1 */
		return SNMP_IPADDRESS(area_id);
	case OSPFAREARANGENET: /* 2 */
		return SNMP_IPADDRESS(range_net);
	case OSPFAREARANGEMASK: /* 3 */
		return SNMP_IPADDRESS(mask);
	case OSPFAREARANGESTATUS: /* 4 */
		return SNMP_INTEGER(SNMP_VALID);
	case OSPFAREARANGEEFFECT: /* 5 */
#define OSPF_advertiseMatching      1
#define OSPF_doNotAdvertiseMatching 2
		return SNMP_INTEGER(OSPF_advertiseMatching);
	default:
		return NULL;
	}
	return NULL;
}

static struct ospf_nbr_nbma *ospfHostLookup(struct variable *v, oid *name,
					    size_t *length,
					    struct in_addr *addr, int exact)
{
	struct ospf_nbr_nbma *nbr_nbma;
	struct ospf *ospf;

	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	if (ospf == NULL)
		return NULL;

	if (exact) {
		/* INDEX { ospfHostIpAddress, ospfHostTOS } */
		if (*length != v->namelen + IN_ADDR_SIZE + 1)
			return NULL;

		/* Check ospfHostTOS. */
		if (name[*length - 1] != 0)
			return NULL;

		oid2in_addr(name + v->namelen, IN_ADDR_SIZE, addr);

		nbr_nbma = ospf_nbr_nbma_lookup(ospf, *addr);

		return nbr_nbma;
	}

	return NULL;
}

static uint8_t *ospfHostEntry(struct variable *v, oid *name, size_t *length,
			      int exact, size_t *var_len,
			      WriteMethod **write_method)
{
	struct ospf_nbr_nbma *nbr_nbma;
	struct ospf_interface *oi;
	struct in_addr addr;
	struct ospf *ospf;

	if (smux_header_table(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	/* Check OSPF instance. */
	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	if (ospf == NULL)
		return NULL;

	memset(&addr, 0, sizeof(addr));

	nbr_nbma = ospfHostLookup(v, name, length, &addr, exact);
	if (nbr_nbma == NULL)
		return NULL;

	oi = nbr_nbma->oi;

	/* Return the current value of the variable */
	switch (v->magic) {
	case OSPFHOSTIPADDRESS: /* 1 */
		return SNMP_IPADDRESS(nbr_nbma->addr);
	case OSPFHOSTTOS: /* 2 */
		return SNMP_INTEGER(0);
	case OSPFHOSTMETRIC: /* 3 */
		if (oi)
			return SNMP_INTEGER(oi->output_cost);
		else
			return SNMP_INTEGER(1);
	case OSPFHOSTSTATUS: /* 4 */
		return SNMP_INTEGER(SNMP_VALID);
	case OSPFHOSTAREAID: /* 5 */
		if (oi && oi->area)
			return SNMP_IPADDRESS(oi->area->area_id);
		else
			return SNMP_IPADDRESS(ospf_empty_addr);
	default:
		return NULL;
	}
	return NULL;
}

static struct list *ospf_snmp_iflist;

struct ospf_snmp_if {
	struct in_addr addr;
	ifindex_t ifindex;
	struct interface *ifp;
};

static struct ospf_snmp_if *ospf_snmp_if_new(void)
{
	return XCALLOC(MTYPE_SNMP, sizeof(struct ospf_snmp_if));
}

static void ospf_snmp_if_free(struct ospf_snmp_if *osif)
{
	XFREE(MTYPE_SNMP, osif);
}

static int ospf_snmp_if_delete(struct interface *ifp)
{
	struct listnode *node, *nnode;
	struct ospf_snmp_if *osif;

	for (ALL_LIST_ELEMENTS(ospf_snmp_iflist, node, nnode, osif)) {
		if (osif->ifp == ifp) {
			list_delete_node(ospf_snmp_iflist, node);
			ospf_snmp_if_free(osif);
			break;
		}
	}
	return 0;
}

static int ospf_snmp_if_update(struct interface *ifp)
{
	struct listnode *node;
	struct listnode *pn;
	struct connected *ifc;
	struct prefix *p;
	struct ospf_snmp_if *osif;
	struct in_addr *addr;
	ifindex_t ifindex;

	ospf_snmp_if_delete(ifp);

	p = NULL;
	addr = NULL;
	ifindex = 0;

	/* Lookup first IPv4 address entry. */
	for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
		p = CONNECTED_ID(ifc);

		if (p->family == AF_INET) {
			addr = &p->u.prefix4;
			break;
		}
	}
	if (!addr)
		ifindex = ifp->ifindex;

	/* Add interface to the list. */
	pn = NULL;
	for (ALL_LIST_ELEMENTS_RO(ospf_snmp_iflist, node, osif)) {
		if (addr) {
			/* Usual interfaces --> Sort them based on interface
			 * IPv4 addresses */
			if (ntohl(osif->addr.s_addr) > ntohl(addr->s_addr))
				break;
		} else {
			/* Unnumbered interfaces --> Sort them based on
			 * interface indexes */
			if (osif->addr.s_addr != INADDR_ANY
			    || osif->ifindex > ifindex)
				break;
		}
		pn = node;
	}

	osif = ospf_snmp_if_new();
	if (addr) /* Usual interface */
	{
		osif->addr = *addr;

		/* This field is used for storing ospfAddressLessIf OID value,
		 * conform to RFC1850 OSPF-MIB specification, it must be 0 for
		 * usual interface */
		osif->ifindex = 0;
	} else /* Unnumbered interface */
		osif->ifindex = ifindex;
	osif->ifp = ifp;

	listnode_add_after(ospf_snmp_iflist, pn, osif);
	return 0;
}

static int ospf_snmp_is_if_have_addr(struct interface *ifp)
{
	struct listnode *nn;
	struct connected *ifc;

	/* Is this interface having any connected IPv4 address ? */
	for (ALL_LIST_ELEMENTS_RO(ifp->connected, nn, ifc)) {
		if (CONNECTED_PREFIX(ifc)->family == AF_INET)
			return 1;
	}

	return 0;
}

static struct ospf_interface *ospf_snmp_if_lookup(struct in_addr *ifaddr,
						  ifindex_t *ifindex)
{
	struct listnode *node;
	struct ospf_snmp_if *osif;
	struct ospf_interface *oi = NULL;
	struct ospf *ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);

	for (ALL_LIST_ELEMENTS_RO(ospf_snmp_iflist, node, osif)) {
		if (ifaddr->s_addr) {
			if (IPV4_ADDR_SAME(&osif->addr, ifaddr))
				oi = ospf_if_lookup_by_local_addr(
					ospf, osif->ifp, *ifaddr);
		} else {
			if (osif->ifindex == *ifindex)
				oi = ospf_if_lookup_by_local_addr(
					ospf, osif->ifp, *ifaddr);
		}
	}
	return oi;
}

static struct ospf_interface *ospf_snmp_if_lookup_next(struct in_addr *ifaddr,
						       ifindex_t *ifindex,
						       int ifaddr_next,
						       ifindex_t ifindex_next)
{
	struct ospf_snmp_if *osif;
	struct listnode *nn;
	struct ospf *ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	struct ospf_interface *oi = NULL;

	if (ospf == NULL)
		return NULL;

	/* No instance is specified --> Return the first OSPF interface */
	if (ifaddr_next) {
		for (ALL_LIST_ELEMENTS_RO(ospf_snmp_iflist, nn, osif)) {
			osif = listgetdata(nn);
			*ifaddr = osif->addr;
			*ifindex = osif->ifindex;
			/* Because no instance is specified, we don't care about
			 * the kind of
			 * interface (usual or unnumbered), just returning the
			 * first valid
			 * OSPF interface */
			oi = ospf_if_lookup_by_local_addr(ospf, osif->ifp,
							  *ifaddr);
			if (oi)
				return (oi);
		}
		return NULL;
	}

	/* An instance is specified --> Return the next OSPF interface */
	for (ALL_LIST_ELEMENTS_RO(ospf_snmp_iflist, nn, osif)) {
		/* Usual interface */
		if (ifaddr->s_addr) {
			/* The interface must have valid AF_INET connected
			 * address */
			/* it must have lager IPv4 address value than the lookup
			 * entry */
			if ((ospf_snmp_is_if_have_addr(osif->ifp))
			    && (ntohl(osif->addr.s_addr)
				> ntohl(ifaddr->s_addr))) {
				*ifaddr = osif->addr;
				*ifindex = osif->ifindex;

				/* and it must be an OSPF interface */
				oi = ospf_if_lookup_by_local_addr(
					ospf, osif->ifp, *ifaddr);
				if (oi)
					return oi;
			}
		}
		/* Unnumbered interface */
		else
			/* The interface must NOT have valid AF_INET connected
			   address */
			/* it must have lager interface index than the lookup
			   entry */
			if ((!ospf_snmp_is_if_have_addr(osif->ifp))
			    && (osif->ifindex > *ifindex)) {
			*ifaddr = osif->addr;
			*ifindex = osif->ifindex;

			/* and it must be an OSPF interface */
			oi = ospf_if_lookup_by_local_addr(ospf, osif->ifp,
							  *ifaddr);
			if (oi)
				return oi;
		}
	}
	return NULL;
}

static int ospf_snmp_iftype(struct interface *ifp)
{
#define ospf_snmp_iftype_broadcast         1
#define ospf_snmp_iftype_nbma              2
#define ospf_snmp_iftype_pointToPoint      3
#define ospf_snmp_iftype_pointToMultipoint 5
	if (if_is_broadcast(ifp))
		return ospf_snmp_iftype_broadcast;
	if (if_is_pointopoint(ifp))
		return ospf_snmp_iftype_pointToPoint;
	return ospf_snmp_iftype_broadcast;
}

static struct ospf_interface *ospfIfLookup(struct variable *v, oid *name,
					   size_t *length,
					   struct in_addr *ifaddr,
					   ifindex_t *ifindex, int exact)
{
	unsigned int len;
	int ifaddr_next = 0;
	ifindex_t ifindex_next = 0;
	struct ospf_interface *oi;
	oid *offset;

	if (exact) {
		if (*length != v->namelen + IN_ADDR_SIZE + 1)
			return NULL;

		oid2in_addr(name + v->namelen, IN_ADDR_SIZE, ifaddr);
		*ifindex = name[v->namelen + IN_ADDR_SIZE];

		return ospf_snmp_if_lookup(ifaddr, ifindex);
	} else {
		len = *length - v->namelen;
		if (len >= IN_ADDR_SIZE)
			len = IN_ADDR_SIZE;
		if (len == 0)
			ifaddr_next = 1;

		oid2in_addr(name + v->namelen, len, ifaddr);

		len = *length - v->namelen - IN_ADDR_SIZE;
		if (len >= 1)
			len = 1;
		else
			ifindex_next = 1;

		if (len == 1)
			*ifindex = name[v->namelen + IN_ADDR_SIZE];

		oi = ospf_snmp_if_lookup_next(ifaddr, ifindex, ifaddr_next,
					      ifindex_next);
		if (oi) {
			*length = v->namelen + IN_ADDR_SIZE + 1;
			offset = name + v->namelen;
			oid_copy_in_addr(offset, ifaddr);
			offset += IN_ADDR_SIZE;
			*offset = *ifindex;
			return oi;
		}
	}
	return NULL;
}

static uint8_t *ospfIfEntry(struct variable *v, oid *name, size_t *length,
			    int exact, size_t *var_len,
			    WriteMethod **write_method)
{
	ifindex_t ifindex;
	struct in_addr ifaddr;
	struct ospf_interface *oi;
	struct ospf *ospf;

	if (smux_header_table(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	ifindex = 0;
	memset(&ifaddr, 0, sizeof(ifaddr));

	/* Check OSPF instance. */
	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	if (ospf == NULL)
		return NULL;

	oi = ospfIfLookup(v, name, length, &ifaddr, &ifindex, exact);
	if (oi == NULL)
		return NULL;

	/* Return the current value of the variable */
	switch (v->magic) {
	case OSPFIFIPADDRESS: /* 1 */
		return SNMP_IPADDRESS(ifaddr);
	case OSPFADDRESSLESSIF: /* 2 */
		return SNMP_INTEGER(ifindex);
	case OSPFIFAREAID: /* 3 */
		if (oi->area)
			return SNMP_IPADDRESS(oi->area->area_id);
		else
			return SNMP_IPADDRESS(ospf_empty_addr);
	case OSPFIFTYPE: /* 4 */
		return SNMP_INTEGER(ospf_snmp_iftype(oi->ifp));
	case OSPFIFADMINSTAT: /* 5 */
		if (oi)
			return SNMP_INTEGER(OSPF_STATUS_ENABLED);
		else
			return SNMP_INTEGER(OSPF_STATUS_DISABLED);
	case OSPFIFRTRPRIORITY: /* 6 */
		return SNMP_INTEGER(PRIORITY(oi));
	case OSPFIFTRANSITDELAY: /* 7 */
		return SNMP_INTEGER(OSPF_IF_PARAM(oi, transmit_delay));
	case OSPFIFRETRANSINTERVAL: /* 8 */
		return SNMP_INTEGER(OSPF_IF_PARAM(oi, retransmit_interval));
	case OSPFIFHELLOINTERVAL: /* 9 */
		return SNMP_INTEGER(OSPF_IF_PARAM(oi, v_hello));
	case OSPFIFRTRDEADINTERVAL: /* 10 */
		return SNMP_INTEGER(OSPF_IF_PARAM(oi, v_wait));
	case OSPFIFPOLLINTERVAL: /* 11 */
		return SNMP_INTEGER(OSPF_POLL_INTERVAL_DEFAULT);
	case OSPFIFSTATE: /* 12 */
		return SNMP_INTEGER(ISM_SNMP(oi->state));
	case OSPFIFDESIGNATEDROUTER: /* 13 */
		return SNMP_IPADDRESS(DR(oi));
	case OSPFIFBACKUPDESIGNATEDROUTER: /* 14 */
		return SNMP_IPADDRESS(BDR(oi));
	case OSPFIFEVENTS: /* 15 */
		return SNMP_INTEGER(oi->state_change);
	case OSPFIFAUTHKEY: /* 16 */
		*var_len = 0;
		return (uint8_t *)OSPF_IF_PARAM(oi, auth_simple);
	case OSPFIFSTATUS: /* 17 */
		return SNMP_INTEGER(SNMP_VALID);
	case OSPFIFMULTICASTFORWARDING: /* 18 */
#define ospf_snmp_multiforward_blocked    1
#define ospf_snmp_multiforward_multicast  2
#define ospf_snmp_multiforward_unicast    3
		return SNMP_INTEGER(ospf_snmp_multiforward_blocked);
	case OSPFIFDEMAND: /* 19 */
		return SNMP_INTEGER(SNMP_FALSE);
	case OSPFIFAUTHTYPE: /* 20 */
		if (oi->area)
			return SNMP_INTEGER(oi->area->auth_type);
		else
			return SNMP_INTEGER(0);
	default:
		return NULL;
	}
	return NULL;
}

#define OSPF_SNMP_METRIC_VALUE 1

static struct ospf_interface *ospfIfMetricLookup(struct variable *v, oid *name,
						 size_t *length,
						 struct in_addr *ifaddr,
						 ifindex_t *ifindex, int exact)
{
	unsigned int len;
	int ifaddr_next = 0;
	ifindex_t ifindex_next = 0;
	struct ospf_interface *oi;
	oid *offset;
	int metric;

	if (exact) {
		if (*length != v->namelen + IN_ADDR_SIZE + 1 + 1)
			return NULL;

		oid2in_addr(name + v->namelen, IN_ADDR_SIZE, ifaddr);
		*ifindex = name[v->namelen + IN_ADDR_SIZE];
		metric = name[v->namelen + IN_ADDR_SIZE + 1];

		if (metric != OSPF_SNMP_METRIC_VALUE)
			return NULL;

		return ospf_snmp_if_lookup(ifaddr, ifindex);
	} else {
		len = *length - v->namelen;
		if (len >= IN_ADDR_SIZE)
			len = IN_ADDR_SIZE;
		else
			ifaddr_next = 1;

		oid2in_addr(name + v->namelen, len, ifaddr);

		len = *length - v->namelen - IN_ADDR_SIZE;
		if (len >= 1)
			len = 1;
		else
			ifindex_next = 1;

		if (len == 1)
			*ifindex = name[v->namelen + IN_ADDR_SIZE];

		oi = ospf_snmp_if_lookup_next(ifaddr, ifindex, ifaddr_next,
					      ifindex_next);
		if (oi) {
			*length = v->namelen + IN_ADDR_SIZE + 1 + 1;
			offset = name + v->namelen;
			oid_copy_in_addr(offset, ifaddr);
			offset += IN_ADDR_SIZE;
			*offset = *ifindex;
			offset++;
			*offset = OSPF_SNMP_METRIC_VALUE;
			return oi;
		}
	}
	return NULL;
}

static uint8_t *ospfIfMetricEntry(struct variable *v, oid *name, size_t *length,
				  int exact, size_t *var_len,
				  WriteMethod **write_method)
{
	/* Currently we support metric 1 only. */
	ifindex_t ifindex;
	struct in_addr ifaddr;
	struct ospf_interface *oi;
	struct ospf *ospf;

	if (smux_header_table(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	ifindex = 0;
	memset(&ifaddr, 0, sizeof(ifaddr));

	/* Check OSPF instance. */
	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	if (ospf == NULL)
		return NULL;

	oi = ospfIfMetricLookup(v, name, length, &ifaddr, &ifindex, exact);
	if (oi == NULL)
		return NULL;

	/* Return the current value of the variable */
	switch (v->magic) {
	case OSPFIFMETRICIPADDRESS:
		return SNMP_IPADDRESS(ifaddr);
	case OSPFIFMETRICADDRESSLESSIF:
		return SNMP_INTEGER(ifindex);
	case OSPFIFMETRICTOS:
		return SNMP_INTEGER(0);
	case OSPFIFMETRICVALUE:
		return SNMP_INTEGER(OSPF_SNMP_METRIC_VALUE);
	case OSPFIFMETRICSTATUS:
		return SNMP_INTEGER(1);
	default:
		return NULL;
	}
	return NULL;
}

static struct route_table *ospf_snmp_vl_table;

static int ospf_snmp_vl_add(struct ospf_vl_data *vl_data)
{
	struct prefix_ls lp;
	struct route_node *rn;

	memset(&lp, 0, sizeof(lp));
	lp.family = AF_UNSPEC;
	lp.prefixlen = 64;
	lp.id = vl_data->vl_area_id;
	lp.adv_router = vl_data->vl_peer;

	rn = route_node_get(ospf_snmp_vl_table, (struct prefix *)&lp);
	if (rn->info)
		route_unlock_node(rn);

	rn->info = vl_data;
	return 0;
}

static int ospf_snmp_vl_delete(struct ospf_vl_data *vl_data)
{
	struct prefix_ls lp;
	struct route_node *rn;

	memset(&lp, 0, sizeof(lp));
	lp.family = AF_UNSPEC;
	lp.prefixlen = 64;
	lp.id = vl_data->vl_area_id;
	lp.adv_router = vl_data->vl_peer;

	rn = route_node_lookup(ospf_snmp_vl_table, (struct prefix *)&lp);
	if (!rn)
		return 0;
	rn->info = NULL;
	route_unlock_node(rn);
	route_unlock_node(rn);
	return 0;
}

static struct ospf_vl_data *ospf_snmp_vl_lookup(struct in_addr *area_id,
						struct in_addr *neighbor)
{
	struct prefix_ls lp;
	struct route_node *rn;
	struct ospf_vl_data *vl_data;

	memset(&lp, 0, sizeof(lp));
	lp.family = AF_UNSPEC;
	lp.prefixlen = 64;
	lp.id = *area_id;
	lp.adv_router = *neighbor;

	rn = route_node_lookup(ospf_snmp_vl_table, (struct prefix *)&lp);
	if (rn) {
		vl_data = rn->info;
		route_unlock_node(rn);
		return vl_data;
	}
	return NULL;
}

static struct ospf_vl_data *ospf_snmp_vl_lookup_next(struct in_addr *area_id,
						     struct in_addr *neighbor,
						     int first)
{
	struct prefix_ls lp;
	struct route_node *rn;
	struct ospf_vl_data *vl_data;

	memset(&lp, 0, sizeof(lp));
	lp.family = AF_UNSPEC;
	lp.prefixlen = 64;
	lp.id = *area_id;
	lp.adv_router = *neighbor;

	if (first)
		rn = route_top(ospf_snmp_vl_table);
	else {
		rn = route_node_get(ospf_snmp_vl_table, (struct prefix *)&lp);
		rn = route_next(rn);
	}

	for (; rn; rn = route_next(rn))
		if (rn->info)
			break;

	if (rn && rn->info) {
		vl_data = rn->info;
		*area_id = vl_data->vl_area_id;
		*neighbor = vl_data->vl_peer;
		route_unlock_node(rn);
		return vl_data;
	}
	return NULL;
}

static struct ospf_vl_data *
ospfVirtIfLookup(struct variable *v, oid *name, size_t *length,
		 struct in_addr *area_id, struct in_addr *neighbor, int exact)
{
	int first;
	unsigned int len;
	struct ospf_vl_data *vl_data;

	if (exact) {
		if (*length != v->namelen + IN_ADDR_SIZE + IN_ADDR_SIZE)
			return NULL;

		oid2in_addr(name + v->namelen, IN_ADDR_SIZE, area_id);
		oid2in_addr(name + v->namelen + IN_ADDR_SIZE, IN_ADDR_SIZE,
			    neighbor);

		return ospf_snmp_vl_lookup(area_id, neighbor);
	} else {
		first = 0;

		len = *length - v->namelen;
		if (len == 0)
			first = 1;
		if (len > IN_ADDR_SIZE)
			len = IN_ADDR_SIZE;
		oid2in_addr(name + v->namelen, len, area_id);

		len = *length - v->namelen - IN_ADDR_SIZE;
		if (len > IN_ADDR_SIZE)
			len = IN_ADDR_SIZE;
		oid2in_addr(name + v->namelen + IN_ADDR_SIZE, len, neighbor);

		vl_data = ospf_snmp_vl_lookup_next(area_id, neighbor, first);

		if (vl_data) {
			*length = v->namelen + IN_ADDR_SIZE + IN_ADDR_SIZE;
			oid_copy_in_addr(name + v->namelen, area_id);
			oid_copy_in_addr(name + v->namelen + IN_ADDR_SIZE,
					 neighbor);
			return vl_data;
		}
	}
	return NULL;
}

static uint8_t *ospfVirtIfEntry(struct variable *v, oid *name, size_t *length,
				int exact, size_t *var_len,
				WriteMethod **write_method)
{
	struct ospf_vl_data *vl_data;
	struct ospf_interface *oi;
	struct in_addr area_id;
	struct in_addr neighbor;

	if (smux_header_table(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	memset(&area_id, 0, sizeof(area_id));
	memset(&neighbor, 0, sizeof(neighbor));

	vl_data = ospfVirtIfLookup(v, name, length, &area_id, &neighbor, exact);
	if (!vl_data)
		return NULL;
	oi = vl_data->vl_oi;
	if (!oi)
		return NULL;

	/* Return the current value of the variable */
	switch (v->magic) {
	case OSPFVIRTIFAREAID:
		return SNMP_IPADDRESS(area_id);
	case OSPFVIRTIFNEIGHBOR:
		return SNMP_IPADDRESS(neighbor);
	case OSPFVIRTIFTRANSITDELAY:
		return SNMP_INTEGER(OSPF_IF_PARAM(oi, transmit_delay));
	case OSPFVIRTIFRETRANSINTERVAL:
		return SNMP_INTEGER(OSPF_IF_PARAM(oi, retransmit_interval));
	case OSPFVIRTIFHELLOINTERVAL:
		return SNMP_INTEGER(OSPF_IF_PARAM(oi, v_hello));
	case OSPFVIRTIFRTRDEADINTERVAL:
		return SNMP_INTEGER(OSPF_IF_PARAM(oi, v_wait));
	case OSPFVIRTIFSTATE:
		return SNMP_INTEGER(oi->state);
	case OSPFVIRTIFEVENTS:
		return SNMP_INTEGER(oi->state_change);
	case OSPFVIRTIFAUTHKEY:
		*var_len = 0;
		return (uint8_t *)OSPF_IF_PARAM(oi, auth_simple);
	case OSPFVIRTIFSTATUS:
		return SNMP_INTEGER(SNMP_VALID);
	case OSPFVIRTIFAUTHTYPE:
		if (oi->area)
			return SNMP_INTEGER(oi->area->auth_type);
		else
			return SNMP_INTEGER(0);
	default:
		return NULL;
	}
	return NULL;
}

static struct ospf_neighbor *ospf_snmp_nbr_lookup(struct ospf *ospf,
						  struct in_addr *nbr_addr,
						  ifindex_t *ifindex)
{
	struct listnode *node, *nnode;
	struct ospf_interface *oi;
	struct ospf_neighbor *nbr;
	struct route_node *rn;

	for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi)) {
		for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
			if ((nbr = rn->info) != NULL
			    && nbr != oi->nbr_self
			    /* If EXACT match is needed, provide ALL entry found
					&& nbr->state != NSM_Down
			     */
			    && nbr->src.s_addr != INADDR_ANY) {
				if (IPV4_ADDR_SAME(&nbr->src, nbr_addr)) {
					route_unlock_node(rn);
					return nbr;
				}
			}
	}
	return NULL;
}

static struct ospf_neighbor *ospf_snmp_nbr_lookup_next(struct in_addr *nbr_addr,
						       ifindex_t *ifindex,
						       int first)
{
	struct listnode *nn;
	struct ospf_interface *oi;
	struct ospf_neighbor *nbr;
	struct route_node *rn;
	struct ospf_neighbor *min = NULL;
	struct ospf *ospf;

	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);

	for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, nn, oi)) {
		for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
			if ((nbr = rn->info) != NULL && nbr != oi->nbr_self
			    && nbr->state != NSM_Down
			    && nbr->src.s_addr != INADDR_ANY) {
				if (first) {
					if (!min)
						min = nbr;
					else if (ntohl(nbr->src.s_addr)
						 < ntohl(min->src.s_addr))
						min = nbr;
				} else if (ntohl(nbr->src.s_addr)
					   > ntohl(nbr_addr->s_addr)) {
					if (!min)
						min = nbr;
					else if (ntohl(nbr->src.s_addr)
						 < ntohl(min->src.s_addr))
						min = nbr;
				}
			}
	}
	if (min) {
		*nbr_addr = min->src;
		*ifindex = 0;
		return min;
	}
	return NULL;
}

static struct ospf_neighbor *ospfNbrLookup(struct variable *v, oid *name,
					   size_t *length,
					   struct in_addr *nbr_addr,
					   ifindex_t *ifindex, int exact)
{
	unsigned int len;
	int first;
	struct ospf_neighbor *nbr;
	struct ospf *ospf;

	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);

	if (!ospf)
		return NULL;

	if (exact) {
		if (*length != v->namelen + IN_ADDR_SIZE + 1)
			return NULL;

		oid2in_addr(name + v->namelen, IN_ADDR_SIZE, nbr_addr);
		*ifindex = name[v->namelen + IN_ADDR_SIZE];

		return ospf_snmp_nbr_lookup(ospf, nbr_addr, ifindex);
	} else {
		first = 0;
		len = *length - v->namelen;

		if (len == 0)
			first = 1;

		if (len > IN_ADDR_SIZE)
			len = IN_ADDR_SIZE;

		oid2in_addr(name + v->namelen, len, nbr_addr);

		len = *length - v->namelen - IN_ADDR_SIZE;
		if (len >= 1)
			*ifindex = name[v->namelen + IN_ADDR_SIZE];

		nbr = ospf_snmp_nbr_lookup_next(nbr_addr, ifindex, first);

		if (nbr) {
			*length = v->namelen + IN_ADDR_SIZE + 1;
			oid_copy_in_addr(name + v->namelen, nbr_addr);
			name[v->namelen + IN_ADDR_SIZE] = *ifindex;
			return nbr;
		}
	}
	return NULL;
}

/* map internal frr neighbor states to official MIB values:

ospfNbrState OBJECT-TYPE
	SYNTAX   INTEGER    {
		    down (1),
		    attempt (2),
		    init (3),
		    twoWay (4),
		    exchangeStart (5),
		    exchange (6),
		    loading (7),
		    full (8)
		  }
*/
static int32_t ospf_snmp_neighbor_state(uint8_t nst)
{
	switch (nst) {
	case NSM_Attempt:
		return 2;
	case NSM_Init:
		return 3;
	case NSM_TwoWay:
		return 4;
	case NSM_ExStart:
		return 5;
	case NSM_Exchange:
		return 6;
	case NSM_Loading:
		return 7;
	case NSM_Full:
		return 8;
	default:
		return 1; /* down */
	}
}

static uint8_t *ospfNbrEntry(struct variable *v, oid *name, size_t *length,
			     int exact, size_t *var_len,
			     WriteMethod **write_method)
{
	struct in_addr nbr_addr;
	ifindex_t ifindex;
	struct ospf_neighbor *nbr;
	struct ospf_interface *oi;

	if (smux_header_table(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	memset(&nbr_addr, 0, sizeof(nbr_addr));
	ifindex = 0;

	nbr = ospfNbrLookup(v, name, length, &nbr_addr, &ifindex, exact);
	if (!nbr)
		return NULL;
	oi = nbr->oi;

	/* Return the current value of the variable */
	switch (v->magic) {
	case OSPFNBRIPADDR:
		return SNMP_IPADDRESS(nbr_addr);
	case OSPFNBRADDRESSLESSINDEX:
		return SNMP_INTEGER(ifindex);
	case OSPFNBRRTRID:
		return SNMP_IPADDRESS(nbr->router_id);
	case OSPFNBROPTIONS:
		return SNMP_INTEGER(oi->nbr_self->options);
	case OSPFNBRPRIORITY:
		return SNMP_INTEGER(nbr->priority);
	case OSPFNBRSTATE:
		return SNMP_INTEGER(ospf_snmp_neighbor_state(nbr->state));
	case OSPFNBREVENTS:
		return SNMP_INTEGER(nbr->state_change);
	case OSPFNBRLSRETRANSQLEN:
		return SNMP_INTEGER(ospf_ls_retransmit_count(nbr));
	case OSPFNBMANBRSTATUS:
		return SNMP_INTEGER(SNMP_VALID);
	case OSPFNBMANBRPERMANENCE:
		return SNMP_INTEGER(2);
	case OSPFNBRHELLOSUPPRESSED:
		return SNMP_INTEGER(SNMP_FALSE);
	default:
		return NULL;
	}
	return NULL;
}

static uint8_t *ospfVirtNbrEntry(struct variable *v, oid *name, size_t *length,
				 int exact, size_t *var_len,
				 WriteMethod **write_method)
{
	struct ospf_vl_data *vl_data;
	struct in_addr area_id;
	struct in_addr neighbor;
	struct ospf *ospf;

	if (smux_header_table(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	memset(&area_id, 0, sizeof(area_id));
	memset(&neighbor, 0, sizeof(neighbor));

	/* Check OSPF instance. */
	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	if (ospf == NULL)
		return NULL;

	vl_data = ospfVirtIfLookup(v, name, length, &area_id, &neighbor, exact);
	if (!vl_data)
		return NULL;

	/* Return the current value of the variable */
	switch (v->magic) {
	case OSPFVIRTNBRAREA:
		return (uint8_t *)NULL;
	case OSPFVIRTNBRRTRID:
		return (uint8_t *)NULL;
	case OSPFVIRTNBRIPADDR:
		return (uint8_t *)NULL;
	case OSPFVIRTNBROPTIONS:
		return (uint8_t *)NULL;
	case OSPFVIRTNBRSTATE:
		return (uint8_t *)NULL;
	case OSPFVIRTNBREVENTS:
		return (uint8_t *)NULL;
	case OSPFVIRTNBRLSRETRANSQLEN:
		return (uint8_t *)NULL;
	case OSPFVIRTNBRHELLOSUPPRESSED:
		return (uint8_t *)NULL;
	default:
		return NULL;
	}
	return NULL;
}

static struct ospf_lsa *ospfExtLsdbLookup(struct variable *v, oid *name,
					  size_t *length, uint8_t *type,
					  struct in_addr *ls_id,
					  struct in_addr *router_id, int exact)
{
	int first;
	oid *offset;
	int offsetlen;
	uint8_t lsa_type;
	unsigned int len;
	struct ospf_lsa *lsa;
	struct ospf *ospf;

	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	if (exact) {
		if (*length != v->namelen + 1 + IN_ADDR_SIZE + IN_ADDR_SIZE)
			return NULL;

		offset = name + v->namelen;

		/* Make it sure given value match to type. */
		lsa_type = *offset;
		offset++;

		if (lsa_type != *type)
			return NULL;

		/* LS ID. */
		oid2in_addr(offset, IN_ADDR_SIZE, ls_id);
		offset += IN_ADDR_SIZE;

		/* Router ID. */
		oid2in_addr(offset, IN_ADDR_SIZE, router_id);

		return ospf_lsdb_lookup_by_id(ospf->lsdb, *type, *ls_id,
					      *router_id);
	} else {
		/* Get variable length. */
		first = 0;
		offset = name + v->namelen;
		offsetlen = *length - v->namelen;

		/* LSA type value. */
		lsa_type = *offset;
		offset++;
		offsetlen--;

		if (offsetlen <= 0 || lsa_type < OSPF_AS_EXTERNAL_LSA)
			first = 1;

		/* LS ID. */
		len = offsetlen;
		if (len > IN_ADDR_SIZE)
			len = IN_ADDR_SIZE;

		oid2in_addr(offset, len, ls_id);

		offset += IN_ADDR_SIZE;
		offsetlen -= IN_ADDR_SIZE;

		/* Router ID. */
		len = offsetlen;
		if (len > IN_ADDR_SIZE)
			len = IN_ADDR_SIZE;

		oid2in_addr(offset, len, router_id);

		lsa = ospf_lsdb_lookup_by_id_next(ospf->lsdb, *type, *ls_id,
						  *router_id, first);

		if (lsa) {
			/* Fill in length. */
			*length = v->namelen + 1 + IN_ADDR_SIZE + IN_ADDR_SIZE;

			/* Fill in value. */
			offset = name + v->namelen;

			*offset = OSPF_AS_EXTERNAL_LSA;
			offset++;
			oid_copy_in_addr(offset, &lsa->data->id);
			offset += IN_ADDR_SIZE;
			oid_copy_in_addr(offset, &lsa->data->adv_router);

			return lsa;
		}
	}
	return NULL;
}

static uint8_t *ospfExtLsdbEntry(struct variable *v, oid *name, size_t *length,
				 int exact, size_t *var_len,
				 WriteMethod **write_method)
{
	struct ospf_lsa *lsa;
	struct lsa_header *lsah;
	uint8_t type;
	struct in_addr ls_id;
	struct in_addr router_id;
	struct ospf *ospf;

	if (smux_header_table(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	type = OSPF_AS_EXTERNAL_LSA;
	memset(&ls_id, 0, sizeof(ls_id));
	memset(&router_id, 0, sizeof(router_id));

	/* Check OSPF instance. */
	ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	if (ospf == NULL)
		return NULL;

	lsa = ospfExtLsdbLookup(v, name, length, &type, &ls_id, &router_id,
				exact);
	if (!lsa)
		return NULL;

	lsah = lsa->data;

	/* Return the current value of the variable */
	switch (v->magic) {
	case OSPFEXTLSDBTYPE:
		return SNMP_INTEGER(OSPF_AS_EXTERNAL_LSA);
	case OSPFEXTLSDBLSID:
		return SNMP_IPADDRESS(lsah->id);
	case OSPFEXTLSDBROUTERID:
		return SNMP_IPADDRESS(lsah->adv_router);
	case OSPFEXTLSDBSEQUENCE:
		return SNMP_INTEGER(lsah->ls_seqnum);
	case OSPFEXTLSDBAGE:
		return SNMP_INTEGER(lsah->ls_age);
	case OSPFEXTLSDBCHECKSUM:
		return SNMP_INTEGER(lsah->checksum);
	case OSPFEXTLSDBADVERTISEMENT:
		*var_len = ntohs(lsah->length);
		return (uint8_t *)lsah;
	default:
		return NULL;
	}
	return NULL;
}

static uint8_t *ospfAreaAggregateEntry(struct variable *v, oid *name,
				       size_t *length, int exact,
				       size_t *var_len,
				       WriteMethod **write_method)
{
	if (smux_header_table(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	/* Return the current value of the variable */
	switch (v->magic) {
	case OSPFAREAAGGREGATEAREAID:
		return (uint8_t *)NULL;
	case OSPFAREAAGGREGATELSDBTYPE:
		return (uint8_t *)NULL;
	case OSPFAREAAGGREGATENET:
		return (uint8_t *)NULL;
	case OSPFAREAAGGREGATEMASK:
		return (uint8_t *)NULL;
	case OSPFAREAAGGREGATESTATUS:
		return (uint8_t *)NULL;
	case OSPFAREAAGGREGATEEFFECT:
		return (uint8_t *)NULL;
	default:
		return NULL;
	}
	return NULL;
}

/* OSPF Traps. */
#define IFSTATECHANGE      16
#define VIRTIFSTATECHANGE   1
#define NBRSTATECHANGE      2
#define VIRTNBRSTATECHANGE  3

static struct trap_object ospfNbrTrapList[] = {{-2, {1, OSPFROUTERID}},
					       {3, {10, 1, OSPFNBRIPADDR}},
					       {3, {10, 1, OSPFNBRRTRID}},
					       {3, {10, 1, OSPFNBRSTATE}}};


static struct trap_object ospfVirtNbrTrapList[] = {
	{-2, {1, 1}},
	{3, {11, 1, OSPFVIRTNBRAREA}},
	{3, {11, 1, OSPFVIRTNBRRTRID}},
	{3, {11, 1, OSPFVIRTNBRSTATE}}};

static struct trap_object ospfIfTrapList[] = {{-2, {1, OSPFROUTERID}},
					      {3, {7, 1, OSPFIFIPADDRESS}},
					      {3, {7, 1, OSPFADDRESSLESSIF}},
					      {3, {7, 1, OSPFIFSTATE}}};

static struct trap_object ospfVirtIfTrapList[] = {
	{-2, {1, OSPFROUTERID}},
	{3, {9, 1, OSPFVIRTIFAREAID}},
	{3, {9, 1, OSPFVIRTIFNEIGHBOR}},
	{3, {9, 1, OSPFVIRTIFSTATE}}};

static void ospfTrapNbrStateChange(struct ospf_neighbor *on)
{
	oid index[sizeof(oid) * (IN_ADDR_SIZE + 1)];
	char msgbuf[16];

	ospf_nbr_ism_state_message(on, msgbuf, sizeof(msgbuf));
	if (IS_DEBUG_OSPF_EVENT)
		zlog_info("%s: trap sent: %pI4 now %s", __func__,
			  &on->address.u.prefix4, msgbuf);

	oid_copy_in_addr(index, &(on->address.u.prefix4));
	index[IN_ADDR_SIZE] = 0;

	smux_trap(ospf_variables, array_size(ospf_variables), ospf_trap_oid,
		  array_size(ospf_trap_oid), ospf_oid,
		  sizeof(ospf_oid) / sizeof(oid), index, IN_ADDR_SIZE + 1,
		  ospfNbrTrapList, array_size(ospfNbrTrapList), NBRSTATECHANGE);
}

static void ospfTrapVirtNbrStateChange(struct ospf_neighbor *on)
{
	oid index[sizeof(oid) * (IN_ADDR_SIZE + 1)];

	zlog_info("ospfTrapVirtNbrStateChange trap sent");

	oid_copy_in_addr(index, &(on->address.u.prefix4));
	index[IN_ADDR_SIZE] = 0;

	smux_trap(ospf_variables, array_size(ospf_variables), ospf_trap_oid,
		  array_size(ospf_trap_oid), ospf_oid,
		  sizeof(ospf_oid) / sizeof(oid), index, IN_ADDR_SIZE + 1,
		  ospfVirtNbrTrapList, array_size(ospfVirtNbrTrapList),
		  VIRTNBRSTATECHANGE);
}

static int ospf_snmp_nsm_change(struct ospf_neighbor *nbr, int next_state,
				int old_state)
{
	/* Transition to/from state Full should be handled only by
	 * DR when in Broadcast or Non-Brodcast Multi-Access networks
	 */
	if ((next_state == NSM_Full || old_state == NSM_Full)
	    && (nbr->oi->state != ISM_DR)
	    && (nbr->oi->type == OSPF_IFTYPE_BROADCAST
		|| nbr->oi->type == OSPF_IFTYPE_NBMA))
		return 0;

	/* State progression to non-terminal state */
	if (next_state > old_state && next_state != NSM_Full
	    && next_state != NSM_TwoWay)
		return 0;

	if (nbr->oi->type == OSPF_IFTYPE_VIRTUALLINK)
		ospfTrapVirtNbrStateChange(nbr);
	else
		ospfTrapNbrStateChange(nbr);

	return 0;
}

static void ospfTrapIfStateChange(struct ospf_interface *oi)
{
	oid index[sizeof(oid) * (IN_ADDR_SIZE + 1)];

	if (IS_DEBUG_OSPF_EVENT)
		zlog_info("%s: trap sent: %pI4 now %s", __func__,
			  &oi->address->u.prefix4,
			  lookup_msg(ospf_ism_state_msg, oi->state, NULL));

	oid_copy_in_addr(index, &(oi->address->u.prefix4));
	index[IN_ADDR_SIZE] = 0;

	smux_trap(ospf_variables, array_size(ospf_variables), ospf_trap_oid,
		  array_size(ospf_trap_oid), ospf_oid,
		  sizeof(ospf_oid) / sizeof(oid), index, IN_ADDR_SIZE + 1,
		  ospfIfTrapList, array_size(ospfIfTrapList), IFSTATECHANGE);
}

static void ospfTrapVirtIfStateChange(struct ospf_interface *oi)
{
	oid index[sizeof(oid) * (IN_ADDR_SIZE + 1)];

	zlog_info("ospfTrapVirtIfStateChange trap sent");

	oid_copy_in_addr(index, &(oi->address->u.prefix4));
	index[IN_ADDR_SIZE] = 0;

	smux_trap(ospf_variables, array_size(ospf_variables), ospf_trap_oid,
		  array_size(ospf_trap_oid), ospf_oid,
		  sizeof(ospf_oid) / sizeof(oid), index, IN_ADDR_SIZE + 1,
		  ospfVirtIfTrapList, array_size(ospfVirtIfTrapList),
		  VIRTIFSTATECHANGE);
}

static int ospf_snmp_ism_change(struct ospf_interface *oi, int state,
				int old_state)
{
	/* Terminal state or regression */
	if ((state == ISM_DR) || (state == ISM_Backup) || (state == ISM_DROther)
	    || (state == ISM_PointToPoint) || (state < old_state)) {
		/* ospfVirtIfStateChange */
		if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
			ospfTrapVirtIfStateChange(oi);
		/* ospfIfStateChange */
		else
			ospfTrapIfStateChange(oi);
	}
	return 0;
}

/* Register OSPF2-MIB. */
static int ospf_snmp_init(struct thread_master *tm)
{
	ospf_snmp_iflist = list_new();
	ospf_snmp_vl_table = route_table_init();
	smux_init(tm);
	REGISTER_MIB("mibII/ospf", ospf_variables, variable, ospf_oid);
	return 0;
}

static int ospf_snmp_module_init(void)
{
	hook_register(ospf_if_update, ospf_snmp_if_update);
	hook_register(ospf_if_delete, ospf_snmp_if_delete);
	hook_register(ospf_vl_add, ospf_snmp_vl_add);
	hook_register(ospf_vl_delete, ospf_snmp_vl_delete);
	hook_register(ospf_ism_change, ospf_snmp_ism_change);
	hook_register(ospf_nsm_change, ospf_snmp_nsm_change);

	hook_register(frr_late_init, ospf_snmp_init);
	return 0;
}

FRR_MODULE_SETUP(.name = "ospfd_snmp", .version = FRR_VERSION,
		 .description = "ospfd AgentX SNMP module",
		 .init = ospf_snmp_module_init,
);