summaryrefslogtreecommitdiffstats
path: root/ospfd/ospf_ri.c
blob: 99326b41b3bc4a768897f5a0141a04a9b2732059 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * This is an implementation of RFC4970 Router Information
 * with support of RFC5088 PCE Capabilites announcement
 *
 * Module name: Router Information
 * Author: Olivier Dugeon <olivier.dugeon@orange.com>
 * Copyright (C) 2012 - 2017 Orange Labs http://www.orange.com/
 */

#include <zebra.h>
#include <math.h>

#include "linklist.h"
#include "prefix.h"
#include "if.h"
#include "table.h"
#include "memory.h"
#include "command.h"
#include "vty.h"
#include "stream.h"
#include "log.h"
#include "frrevent.h"
#include "hash.h"
#include "sockunion.h" /* for inet_aton() */
#include "mpls.h"
#include <lib/json.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_sr.h"
#include "ospfd/ospf_ri.h"
#include "ospfd/ospf_errors.h"

/*
 * Global variable to manage Opaque-LSA/Router Information on this node.
 * Note that all parameter values are stored in network byte order.
 */
static struct ospf_router_info OspfRI;

/*------------------------------------------------------------------------------*
 * Following are initialize/terminate functions for Router Information
 *handling.
 *------------------------------------------------------------------------------*/

static void ospf_router_info_ism_change(struct ospf_interface *oi,
					int old_status);
static void ospf_router_info_config_write_router(struct vty *vty);
static void ospf_router_info_show_info(struct vty *vty,
				       struct json_object *json,
				       struct ospf_lsa *lsa);
static int ospf_router_info_lsa_originate(void *arg);
static struct ospf_lsa *ospf_router_info_lsa_refresh(struct ospf_lsa *lsa);
static void ospf_router_info_lsa_schedule(struct ospf_ri_area_info *ai,
					  enum lsa_opcode opcode);
static void ospf_router_info_register_vty(void);
static int ospf_router_info_lsa_update(struct ospf_lsa *lsa);
static void del_area_info(void *val);
static void del_pce_info(void *val);

int ospf_router_info_init(void)
{

	zlog_info("RI (%s): Initialize Router Information", __func__);

	memset(&OspfRI, 0, sizeof(OspfRI));
	OspfRI.enabled = false;
	OspfRI.registered = 0;
	OspfRI.scope = OSPF_OPAQUE_AS_LSA;
	OspfRI.as_flags = RIFLG_LSA_INACTIVE;
	OspfRI.area_info = list_new();
	OspfRI.area_info->del = del_area_info;

	/* Initialize pce domain and neighbor list */
	OspfRI.pce_info.enabled = false;
	OspfRI.pce_info.pce_domain = list_new();
	OspfRI.pce_info.pce_domain->del = del_pce_info;
	OspfRI.pce_info.pce_neighbor = list_new();
	OspfRI.pce_info.pce_neighbor->del = del_pce_info;

	/* Initialize Segment Routing information structure */
	OspfRI.sr_info.enabled = false;

	ospf_router_info_register_vty();

	return 0;
}

static int ospf_router_info_register(uint8_t scope)
{
	int rc = 0;

	if (OspfRI.registered)
		return rc;

	zlog_info("RI (%s): Register Router Information with scope %s(%d)",
		  __func__,
		  scope == OSPF_OPAQUE_AREA_LSA ? "Area" : "AS", scope);
	rc = ospf_register_opaque_functab(
		scope, OPAQUE_TYPE_ROUTER_INFORMATION_LSA,
		NULL, /* new interface */
		NULL, /* del interface */
		ospf_router_info_ism_change,
		NULL, /* NSM change */
		ospf_router_info_config_write_router,
		NULL, /* Config. write interface */
		NULL, /* Config. write debug */
		ospf_router_info_show_info, ospf_router_info_lsa_originate,
		ospf_router_info_lsa_refresh, ospf_router_info_lsa_update,
		NULL); /* del_lsa_hook */

	if (rc != 0) {
		flog_warn(
			EC_OSPF_OPAQUE_REGISTRATION,
			"RI (%s): Failed to register functions", __func__);
		return rc;
	}

	OspfRI.registered = 1;
	OspfRI.scope = scope;
	return rc;
}

static int ospf_router_info_unregister(void)
{

	if ((OspfRI.scope != OSPF_OPAQUE_AS_LSA)
	    && (OspfRI.scope != OSPF_OPAQUE_AREA_LSA)) {
		assert("Unable to unregister Router Info functions: Wrong scope!"
		       == NULL);
		return -1;
	}

	ospf_delete_opaque_functab(OspfRI.scope,
				   OPAQUE_TYPE_ROUTER_INFORMATION_LSA);

	OspfRI.registered = 0;
	return 0;
}

void ospf_router_info_term(void)
{

	list_delete(&OspfRI.area_info);
	list_delete(&OspfRI.pce_info.pce_domain);
	list_delete(&OspfRI.pce_info.pce_neighbor);

	OspfRI.enabled = false;

	ospf_router_info_unregister();

	return;
}

void ospf_router_info_finish(void)
{
	struct listnode *node, *nnode;
	struct ospf_ri_area_info *ai;

	/* Flush Router Info LSA */
	for (ALL_LIST_ELEMENTS(OspfRI.area_info, node, nnode, ai))
		if (CHECK_FLAG(ai->flags, RIFLG_LSA_ENGAGED))
			ospf_router_info_lsa_schedule(ai, FLUSH_THIS_LSA);

	list_delete_all_node(OspfRI.pce_info.pce_domain);
	list_delete_all_node(OspfRI.pce_info.pce_neighbor);

	OspfRI.enabled = false;
}

static void del_area_info(void *val)
{
	XFREE(MTYPE_OSPF_ROUTER_INFO, val);
}

static void del_pce_info(void *val)
{
	XFREE(MTYPE_OSPF_PCE_PARAMS, val);
}

/* Catch RI LSA flooding Scope for ospf_ext.[h,c] code */
struct scope_info ospf_router_info_get_flooding_scope(void)
{
	struct scope_info flooding_scope;

	if (OspfRI.scope == OSPF_OPAQUE_AS_LSA) {
		flooding_scope.scope = OSPF_OPAQUE_AS_LSA;
		flooding_scope.areas = NULL;
		return flooding_scope;
	}
	flooding_scope.scope = OSPF_OPAQUE_AREA_LSA;
	flooding_scope.areas = OspfRI.area_info;
	return flooding_scope;
}

static struct ospf_ri_area_info *lookup_by_area(struct ospf_area *area)
{
	struct listnode *node, *nnode;
	struct ospf_ri_area_info *ai;

	for (ALL_LIST_ELEMENTS(OspfRI.area_info, node, nnode, ai))
		if (ai->area == area)
			return ai;

	return NULL;
}

/*------------------------------------------------------------------------*
 * Following are control functions for ROUTER INFORMATION parameters
 *management.
 *------------------------------------------------------------------------*/

static void set_router_info_capabilities(struct ri_tlv_router_cap *ric,
					 uint32_t cap)
{
	ric->header.type = htons(RI_TLV_CAPABILITIES);
	ric->header.length = htons(RI_TLV_LENGTH);
	ric->value = htonl(cap);
	return;
}

static int set_pce_header(struct ospf_pce_info *pce)
{
	uint16_t length = 0;
	struct listnode *node;
	struct ri_pce_subtlv_domain *domain;
	struct ri_pce_subtlv_neighbor *neighbor;

	/* PCE Address */
	if (ntohs(pce->pce_address.header.type) != 0)
		length += TLV_SIZE(&pce->pce_address.header);

	/* PCE Path Scope */
	if (ntohs(pce->pce_scope.header.type) != 0)
		length += TLV_SIZE(&pce->pce_scope.header);

	/* PCE Domain */
	for (ALL_LIST_ELEMENTS_RO(pce->pce_domain, node, domain)) {
		if (ntohs(domain->header.type) != 0)
			length += TLV_SIZE(&domain->header);
	}

	/* PCE Neighbor */
	for (ALL_LIST_ELEMENTS_RO(pce->pce_neighbor, node, neighbor)) {
		if (ntohs(neighbor->header.type) != 0)
			length += TLV_SIZE(&neighbor->header);
	}

	/* PCE Capabilities */
	if (ntohs(pce->pce_cap_flag.header.type) != 0)
		length += TLV_SIZE(&pce->pce_cap_flag.header);

	if (length != 0) {
		pce->pce_header.header.type = htons(RI_TLV_PCE);
		pce->pce_header.header.length = htons(length);
		pce->enabled = true;
	} else {
		pce->pce_header.header.type = 0;
		pce->pce_header.header.length = 0;
		pce->enabled = false;
	}

	return length;
}

static void set_pce_address(struct in_addr ipv4, struct ospf_pce_info *pce)
{

	/* Enable PCE Info */
	pce->pce_header.header.type = htons(RI_TLV_PCE);
	/* Set PCE Address */
	pce->pce_address.header.type = htons(RI_PCE_SUBTLV_ADDRESS);
	pce->pce_address.header.length = htons(PCE_ADDRESS_IPV4_SIZE);
	pce->pce_address.address.type = htons(PCE_ADDRESS_IPV4);
	pce->pce_address.address.value = ipv4;

	return;
}

static void set_pce_path_scope(uint32_t scope, struct ospf_pce_info *pce)
{

	/* Set PCE Scope */
	pce->pce_scope.header.type = htons(RI_PCE_SUBTLV_PATH_SCOPE);
	pce->pce_scope.header.length = htons(RI_TLV_LENGTH);
	pce->pce_scope.value = htonl(scope);

	return;
}

static void set_pce_domain(uint16_t type, uint32_t domain,
			   struct ospf_pce_info *pce)
{

	struct ri_pce_subtlv_domain *new;

	/* Create new domain info */
	new = XCALLOC(MTYPE_OSPF_PCE_PARAMS,
		      sizeof(struct ri_pce_subtlv_domain));

	new->header.type = htons(RI_PCE_SUBTLV_DOMAIN);
	new->header.length = htons(PCE_ADDRESS_IPV4_SIZE);
	new->type = htons(type);
	new->value = htonl(domain);

	/* Add new domain to the list */
	listnode_add(pce->pce_domain, new);

	return;
}

static void unset_pce_domain(uint16_t type, uint32_t domain,
			     struct ospf_pce_info *pce)
{
	struct listnode *node;
	struct ri_pce_subtlv_domain *old = NULL;
	int found = 0;

	/* Search the corresponding node */
	for (ALL_LIST_ELEMENTS_RO(pce->pce_domain, node, old)) {
		if ((old->type == htons(type))
		    && (old->value == htonl(domain))) {
			found = 1;
			break;
		}
	}

	/* if found remove it */
	if (found) {
		listnode_delete(pce->pce_domain, old);

		/* Finally free the old domain */
		XFREE(MTYPE_OSPF_PCE_PARAMS, old);
	}
}

static void set_pce_neighbor(uint16_t type, uint32_t domain,
			     struct ospf_pce_info *pce)
{

	struct ri_pce_subtlv_neighbor *new;

	/* Create new neighbor info */
	new = XCALLOC(MTYPE_OSPF_PCE_PARAMS,
		      sizeof(struct ri_pce_subtlv_neighbor));

	new->header.type = htons(RI_PCE_SUBTLV_NEIGHBOR);
	new->header.length = htons(PCE_ADDRESS_IPV4_SIZE);
	new->type = htons(type);
	new->value = htonl(domain);

	/* Add new domain to the list */
	listnode_add(pce->pce_neighbor, new);

	return;
}

static void unset_pce_neighbor(uint16_t type, uint32_t domain,
			       struct ospf_pce_info *pce)
{
	struct listnode *node;
	struct ri_pce_subtlv_neighbor *old = NULL;
	int found = 0;

	/* Search the corresponding node */
	for (ALL_LIST_ELEMENTS_RO(pce->pce_neighbor, node, old)) {
		if ((old->type == htons(type))
		    && (old->value == htonl(domain))) {
			found = 1;
			break;
		}
	}

	/* if found remove it */
	if (found) {
		listnode_delete(pce->pce_neighbor, old);

		/* Finally free the old domain */
		XFREE(MTYPE_OSPF_PCE_PARAMS, old);
	}
}

static void set_pce_cap_flag(uint32_t cap, struct ospf_pce_info *pce)
{

	/* Set PCE Capabilities flag */
	pce->pce_cap_flag.header.type = htons(RI_PCE_SUBTLV_CAP_FLAG);
	pce->pce_cap_flag.header.length = htons(RI_TLV_LENGTH);
	pce->pce_cap_flag.value = htonl(cap);

	return;
}

/* Segment Routing TLV setter */

/* Algorithm SubTLV - section 3.1 */
static void set_sr_algorithm(uint8_t algo)
{

	OspfRI.sr_info.algo.value[0] = algo;
	for (int i = 1; i < ALGORITHM_COUNT; i++)
		OspfRI.sr_info.algo.value[i] = SR_ALGORITHM_UNSET;

	/* Set TLV type and length == only 1 Algorithm */
	TLV_TYPE(OspfRI.sr_info.algo) = htons(RI_SR_TLV_SR_ALGORITHM);
	TLV_LEN(OspfRI.sr_info.algo) = htons(sizeof(uint8_t));
}

/* unset Aglogithm SubTLV */
static void unset_sr_algorithm(uint8_t algo)
{

	for (int i = 0; i < ALGORITHM_COUNT; i++)
		OspfRI.sr_info.algo.value[i] = SR_ALGORITHM_UNSET;

	/* Unset TLV type and length */
	TLV_TYPE(OspfRI.sr_info.algo) = htons(0);
	TLV_LEN(OspfRI.sr_info.algo) = htons(0);
}

/* Set Segment Routing Global Block SubTLV - section 3.2 */
static void set_sr_global_label_range(struct sr_block srgb)
{
	/* Set Header */
	TLV_TYPE(OspfRI.sr_info.srgb) = htons(RI_SR_TLV_SRGB_LABEL_RANGE);
	TLV_LEN(OspfRI.sr_info.srgb) = htons(RI_SR_TLV_LABEL_RANGE_SIZE);
	/* Set Range Size */
	OspfRI.sr_info.srgb.size = htonl(SET_RANGE_SIZE(srgb.range_size));
	/* Set Lower bound label SubTLV */
	TLV_TYPE(OspfRI.sr_info.srgb.lower) = htons(SUBTLV_SID_LABEL);
	TLV_LEN(OspfRI.sr_info.srgb.lower) = htons(SID_RANGE_LABEL_LENGTH);
	OspfRI.sr_info.srgb.lower.value = htonl(SET_LABEL(srgb.lower_bound));
}

/* Unset Segment Routing Global Block SubTLV */
static void unset_sr_global_label_range(void)
{
	TLV_TYPE(OspfRI.sr_info.srgb) = htons(0);
	TLV_LEN(OspfRI.sr_info.srgb) = htons(0);
	TLV_TYPE(OspfRI.sr_info.srgb.lower) = htons(0);
	TLV_LEN(OspfRI.sr_info.srgb.lower) = htons(0);
}

/* Set Segment Routing Local Block SubTLV - section 3.2 */
static void set_sr_local_label_range(struct sr_block srlb)
{
	/* Set Header */
	TLV_TYPE(OspfRI.sr_info.srlb) = htons(RI_SR_TLV_SRLB_LABEL_RANGE);
	TLV_LEN(OspfRI.sr_info.srlb) = htons(RI_SR_TLV_LABEL_RANGE_SIZE);
	/* Set Range Size */
	OspfRI.sr_info.srlb.size = htonl(SET_RANGE_SIZE(srlb.range_size));
	/* Set Lower bound label SubTLV */
	TLV_TYPE(OspfRI.sr_info.srlb.lower) = htons(SUBTLV_SID_LABEL);
	TLV_LEN(OspfRI.sr_info.srlb.lower) = htons(SID_RANGE_LABEL_LENGTH);
	OspfRI.sr_info.srlb.lower.value = htonl(SET_LABEL(srlb.lower_bound));
}

/* Unset Segment Routing Local Block SubTLV */
static void unset_sr_local_label_range(void)
{
	TLV_TYPE(OspfRI.sr_info.srlb) = htons(0);
	TLV_LEN(OspfRI.sr_info.srlb) = htons(0);
	TLV_TYPE(OspfRI.sr_info.srlb.lower) = htons(0);
	TLV_LEN(OspfRI.sr_info.srlb.lower) = htons(0);
}

/* Set Maximum Stack Depth for this router */
static void set_sr_node_msd(uint8_t msd)
{
	TLV_TYPE(OspfRI.sr_info.msd) = htons(RI_SR_TLV_NODE_MSD);
	TLV_LEN(OspfRI.sr_info.msd) = htons(sizeof(uint32_t));
	OspfRI.sr_info.msd.value = msd;
}

/* Unset this router MSD */
static void unset_sr_node_msd(void)
{
	TLV_TYPE(OspfRI.sr_info.msd) = htons(0);
	TLV_LEN(OspfRI.sr_info.msd) = htons(0);
}

static void unset_param(void *tlv_buffer)
{
	struct tlv_header *tlv = (struct tlv_header *)tlv_buffer;

	tlv->type = 0;
	/* Fill the Value to 0 */
	memset(TLV_DATA(tlv_buffer), 0, TLV_BODY_SIZE(tlv));
	tlv->length = 0;

	return;
}

static void initialize_params(struct ospf_router_info *ori)
{
	uint32_t cap = 0;
	struct ospf *top;
	struct listnode *node, *nnode;
	struct ospf_area *area;
	struct ospf_ri_area_info *new;

	/*
	 * Initialize default Router Information Capabilities.
	 */
	cap = RI_TE_SUPPORT;

	set_router_info_capabilities(&ori->router_cap, cap);

	/* If Area address is not null and exist, retrieve corresponding
	 * structure */
	top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
	zlog_info("RI (%s): Initialize Router Info for %s scope", __func__,
		  OspfRI.scope == OSPF_OPAQUE_AREA_LSA ? "Area" : "AS");

	/* Try to get available Area's context from ospf at this step.
	 * Do it latter if not available */
	if (OspfRI.scope == OSPF_OPAQUE_AREA_LSA) {
		if (!list_isempty(OspfRI.area_info))
			list_delete_all_node(OspfRI.area_info);
		for (ALL_LIST_ELEMENTS(top->areas, node, nnode, area)) {
			zlog_debug("RI (%s): Add area %pI4 to Router Information",
				__func__, &area->area_id);
			new = XCALLOC(MTYPE_OSPF_ROUTER_INFO,
				sizeof(struct ospf_ri_area_info));
			new->area = area;
			new->flags = RIFLG_LSA_INACTIVE;
			listnode_add(OspfRI.area_info, new);
		}
	}

	/*
	 * Initialize default PCE Information values
	 */
	/* PCE address == OSPF Router ID */
	set_pce_address(top->router_id, &ori->pce_info);

	/* PCE scope */
	cap = 7; /* Set L, R and Rd bits to one = intra & inter-area path
		    computation */
	set_pce_path_scope(cap, &ori->pce_info);

	/* PCE Capabilities */
	cap = PCE_CAP_BIDIRECTIONAL | PCE_CAP_DIVERSE_PATH | PCE_CAP_OBJECTIVES
	      | PCE_CAP_ADDITIVE | PCE_CAP_MULTIPLE_REQ;
	set_pce_cap_flag(cap, &ori->pce_info);

	return;
}

static int is_mandated_params_set(struct ospf_router_info *ori)
{
	int rc = 0;

	if (ori == NULL)
		return rc;

	if (ntohs(ori->router_cap.header.type) == 0)
		return rc;

	if ((ntohs(ori->pce_info.pce_header.header.type) == RI_TLV_PCE)
	    && (ntohs(ori->pce_info.pce_address.header.type) == 0)
	    && (ntohs(ori->pce_info.pce_cap_flag.header.type) == 0))
		return rc;

	if ((ori->sr_info.enabled) && (ntohs(TLV_TYPE(ori->sr_info.algo)) == 0)
	    && (ntohs(TLV_TYPE(ori->sr_info.srgb)) == 0))
		return rc;

	rc = 1;

	return rc;
}

/*
 * Used by Segment Routing to set new TLVs and Sub-TLVs values
 *
 * @param enable To activate or not Segment Routing router Information flooding
 * @param srn    Self Segment Routing node
 *
 * @return none
 */
void ospf_router_info_update_sr(bool enable, struct sr_node *srn)
{
	struct listnode *node, *nnode;
	struct ospf_ri_area_info *ai;

	/* First, check if Router Information is registered or not */
	if (!OspfRI.registered)
		ospf_router_info_register(OSPF_OPAQUE_AREA_LSA);

	/* Verify that scope is AREA */
	if (OspfRI.scope != OSPF_OPAQUE_AREA_LSA) {
		zlog_err(
			"RI (%s): Router Info is %s flooding: Change scope to Area flooding for Segment Routing",
			__func__,
			OspfRI.scope == OSPF_OPAQUE_AREA_LSA ? "Area" : "AS");
		return;
	}

	/* Then, activate and initialize Router Information if necessary */
	if (!OspfRI.enabled) {
		OspfRI.enabled = true;
		initialize_params(&OspfRI);
	}

	/* Check that SR node is valid */
	if (srn == NULL)
		return;

	if (IS_DEBUG_OSPF_SR)
		zlog_debug("RI (%s): %s Routing Information for Segment Routing",
			   __func__, enable ? "Enable" : "Disable");

	/* Unset or Set SR parameters */
	if (!enable) {
		unset_sr_algorithm(SR_ALGORITHM_SPF);
		unset_sr_global_label_range();
		unset_sr_local_label_range();
		unset_sr_node_msd();
		OspfRI.sr_info.enabled = false;
	} else {
		// Only SR_ALGORITHM_SPF is supported
		set_sr_algorithm(SR_ALGORITHM_SPF);
		set_sr_global_label_range(srn->srgb);
		set_sr_local_label_range(srn->srlb);
		if (srn->msd != 0)
			set_sr_node_msd(srn->msd);
		else
			unset_sr_node_msd();
		OspfRI.sr_info.enabled = true;
	}

	/* Refresh if already engaged or originate RI LSA */
	for (ALL_LIST_ELEMENTS(OspfRI.area_info, node, nnode, ai)) {
		if (CHECK_FLAG(ai->flags, RIFLG_LSA_ENGAGED))
			ospf_router_info_lsa_schedule(ai, REFRESH_THIS_LSA);
		else
			ospf_router_info_lsa_schedule(ai,
				REORIGINATE_THIS_LSA);

	}
}

/*------------------------------------------------------------------------*
 * Following are callback functions against generic Opaque-LSAs handling.
 *------------------------------------------------------------------------*/
static void ospf_router_info_ism_change(struct ospf_interface *oi,
					int old_state)
{

	struct ospf_ri_area_info *ai;

	/* Collect area information */
	ai = lookup_by_area(oi->area);

	/* Check if area is not yet registered */
	if (ai != NULL)
		return;

	/* Add this new area to the list */
	ai = XCALLOC(MTYPE_OSPF_ROUTER_INFO, sizeof(struct ospf_ri_area_info));
	ai->area = oi->area;
	ai->flags = RIFLG_LSA_INACTIVE;
	listnode_add(OspfRI.area_info, ai);

	return;
}

/*------------------------------------------------------------------------*
 * Following are OSPF protocol processing functions for ROUTER INFORMATION
 *------------------------------------------------------------------------*/

static void build_tlv_header(struct stream *s, struct tlv_header *tlvh)
{

	stream_put(s, tlvh, sizeof(struct tlv_header));
	return;
}

static void build_tlv(struct stream *s, struct tlv_header *tlvh)
{

	if (ntohs(tlvh->type) != 0) {
		build_tlv_header(s, tlvh);
		stream_put(s, TLV_DATA(tlvh), TLV_BODY_SIZE(tlvh));
	}
	return;
}

static void ospf_router_info_lsa_body_set(struct stream *s)
{

	struct listnode *node;
	struct ri_pce_subtlv_domain *domain;
	struct ri_pce_subtlv_neighbor *neighbor;

	/* Build Router Information TLV */
	build_tlv(s, &OspfRI.router_cap.header);

	/* Build Segment Routing TLVs if enabled */
	if (OspfRI.sr_info.enabled) {
		/* Build Algorithm TLV */
		build_tlv(s, &TLV_HDR(OspfRI.sr_info.algo));
		/* Build SRGB TLV */
		build_tlv(s, &TLV_HDR(OspfRI.sr_info.srgb));
		/* Build SRLB TLV */
		build_tlv(s, &TLV_HDR(OspfRI.sr_info.srlb));
		/* Build MSD TLV */
		build_tlv(s, &TLV_HDR(OspfRI.sr_info.msd));
	}

	/* Add RI PCE TLV if it is set */
	if (OspfRI.pce_info.enabled) {

		/* Compute PCE Info header first */
		set_pce_header(&OspfRI.pce_info);

		/* Build PCE TLV */
		build_tlv_header(s, &OspfRI.pce_info.pce_header.header);

		/* Build PCE address sub-tlv */
		build_tlv(s, &OspfRI.pce_info.pce_address.header);

		/* Build PCE path scope sub-tlv */
		build_tlv(s, &OspfRI.pce_info.pce_scope.header);

		/* Build PCE domain sub-tlv */
		for (ALL_LIST_ELEMENTS_RO(OspfRI.pce_info.pce_domain, node,
					  domain))
			build_tlv(s, &domain->header);

		/* Build PCE neighbor sub-tlv */
		for (ALL_LIST_ELEMENTS_RO(OspfRI.pce_info.pce_neighbor, node,
					  neighbor))
			build_tlv(s, &neighbor->header);

		/* Build PCE cap flag sub-tlv */
		build_tlv(s, &OspfRI.pce_info.pce_cap_flag.header);
	}

	return;
}

/* Create new opaque-LSA. */
static struct ospf_lsa *ospf_router_info_lsa_new(struct ospf_area *area)
{
	struct ospf *top;
	struct stream *s;
	struct lsa_header *lsah;
	struct ospf_lsa *new = NULL;
	uint8_t options, lsa_type;
	struct in_addr lsa_id;
	uint32_t tmp;
	uint16_t length;

	/* Create a stream for LSA. */
	s = stream_new(OSPF_MAX_LSA_SIZE);

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

	options = OSPF_OPTION_E;  /* Enable AS external as we flood RI with
				     Opaque Type 11 */
	options |= OSPF_OPTION_O; /* Don't forget this :-) */

	lsa_type = OspfRI.scope;
	/* LSA ID == 0 for Router Information see RFC 4970 */
	tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_ROUTER_INFORMATION_LSA, 0);
	lsa_id.s_addr = htonl(tmp);

	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
		zlog_debug(
			"LSA[Type%d:%pI4]: Create an Opaque-LSA/ROUTER INFORMATION instance",
			lsa_type, &lsa_id);

	top = ospf_lookup_by_vrf_id(VRF_DEFAULT);

	/* Set opaque-LSA header fields. */
	lsa_header_set(s, options, lsa_type, lsa_id, top->router_id);

	/* Set opaque-LSA body fields. */
	ospf_router_info_lsa_body_set(s);

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

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

	/* Routing Information is only supported for default VRF */
	new->vrf_id = VRF_DEFAULT;
	new->area = area;

	SET_FLAG(new->flags, OSPF_LSA_SELF);
	memcpy(new->data, lsah, length);
	stream_free(s);

	return new;
}

static int ospf_router_info_lsa_originate_as(void *arg)
{
	struct ospf_lsa *new;
	struct ospf *top;
	int rc = -1;

	/* Sanity Check */
	if (OspfRI.scope == OSPF_OPAQUE_AREA_LSA) {
		flog_warn(
			EC_OSPF_LSA_INSTALL_FAILURE,
			"RI (%s): wrong flooding scope AREA instead of AS ?",
			__func__);
		return rc;
	}

	/* Create new Opaque-LSA/ROUTER INFORMATION instance. */
	new = ospf_router_info_lsa_new(NULL);
	top = (struct ospf *)arg;

	/* Check ospf info */
	if (top == NULL) {
		zlog_debug("RI (%s): ospf instance not found for vrf id %u",
			   __func__, VRF_DEFAULT);
		ospf_lsa_unlock(&new);
		return rc;
	}

	/* Install this LSA into LSDB. */
	if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
		flog_warn(
			EC_OSPF_LSA_INSTALL_FAILURE,
			"RI (%s): ospf_lsa_install() ?", __func__);
		ospf_lsa_unlock(&new);
		return rc;
	}

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

	/* Flood new LSA through AREA or AS. */
	SET_FLAG(OspfRI.as_flags, RIFLG_LSA_ENGAGED);
	ospf_flood_through_as(top, NULL /*nbr */, new);

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

	rc = 0;
	return rc;
}

static int ospf_router_info_lsa_originate_area(void *arg)
{
	struct ospf_lsa *new;
	struct ospf *top;
	struct ospf_ri_area_info *ai = NULL;
	int rc = -1;

	/* Sanity Check */
	if (OspfRI.scope == OSPF_OPAQUE_AS_LSA) {
		flog_warn(
			EC_OSPF_LSA_INSTALL_FAILURE,
			"RI (%s): wrong flooding scope AS instead of AREA ?",
			__func__);
		return rc;
	}

	/* Create new Opaque-LSA/ROUTER INFORMATION instance. */
	ai = lookup_by_area((struct ospf_area *)arg);
	if (ai == NULL) {
		zlog_debug(
			"RI (%s): There is no context for this Router Information. Stop processing",
			__func__);
		return rc;
	}

	if (ai->area->ospf)
		top = ai->area->ospf;
	else
		top = ospf_lookup_by_vrf_id(VRF_DEFAULT);

	new = ospf_router_info_lsa_new(ai->area);

	/* Check ospf info */
	if (top == NULL) {
		zlog_debug("RI (%s): ospf instance not found for vrf id %u",
			   __func__, VRF_DEFAULT);
		ospf_lsa_unlock(&new);
		return rc;
	}

	/* Install this LSA into LSDB. */
	if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
		flog_warn(
			EC_OSPF_LSA_INSTALL_FAILURE,
			"RI (%s): ospf_lsa_install() ?", __func__);
		ospf_lsa_unlock(&new);
		return rc;
	}

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

	/* Flood new LSA through AREA or AS. */
	SET_FLAG(ai->flags, RIFLG_LSA_ENGAGED);
	ospf_flood_through_area(ai->area, NULL /*nbr */, new);

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

	rc = 0;
	return rc;
}

static int ospf_router_info_lsa_originate(void *arg)
{

	struct ospf_ri_area_info *ai;
	int rc = -1;

	if (!OspfRI.enabled) {
		zlog_info("RI (%s): ROUTER INFORMATION is disabled now.",
			  __func__);
		rc = 0; /* This is not an error case. */
		return rc;
	}

	/* Check if Router Information LSA is already engaged */
	if (OspfRI.scope == OSPF_OPAQUE_AS_LSA) {
		if ((CHECK_FLAG(OspfRI.as_flags, RIFLG_LSA_ENGAGED))
			&& (CHECK_FLAG(OspfRI.as_flags,
				RIFLG_LSA_FORCED_REFRESH))) {
			UNSET_FLAG(OspfRI.as_flags, RIFLG_LSA_FORCED_REFRESH);
			ospf_router_info_lsa_schedule(NULL, REFRESH_THIS_LSA);
			rc = 0;
			return rc;
		}
	} else {
		ai = lookup_by_area((struct ospf_area *)arg);
		if (ai == NULL) {
			flog_warn(
				EC_OSPF_LSA,
				"RI (%s): Missing area information", __func__);
			return rc;
		}
		if ((CHECK_FLAG(ai->flags, RIFLG_LSA_ENGAGED))
			&& (CHECK_FLAG(ai->flags, RIFLG_LSA_FORCED_REFRESH))) {
			UNSET_FLAG(ai->flags, RIFLG_LSA_FORCED_REFRESH);
			ospf_router_info_lsa_schedule(ai, REFRESH_THIS_LSA);
			rc = 0;
			return rc;
		}
	}

	/* Router Information is not yet Engaged, check parameters */
	if (!is_mandated_params_set(&OspfRI))
		flog_warn(
			EC_OSPF_LSA,
			"RI (%s): lacks mandated ROUTER INFORMATION parameters",
			__func__);

	/* Ok, let's try to originate an LSA */
	if (OspfRI.scope == OSPF_OPAQUE_AS_LSA)
		rc = ospf_router_info_lsa_originate_as(arg);
	else
		rc = ospf_router_info_lsa_originate_area(arg);

	return rc;
}

static struct ospf_lsa *ospf_router_info_lsa_refresh(struct ospf_lsa *lsa)
{
	struct ospf_ri_area_info *ai = NULL;
	struct ospf_lsa *new = NULL;
	struct ospf *top;

	if (!OspfRI.enabled) {
		/*
		 * This LSA must have flushed before due to ROUTER INFORMATION
		 * status change.
		 * It seems a slip among routers in the routing domain.
		 */
		zlog_info("RI (%s): ROUTER INFORMATION is disabled now.",
			  __func__);
		lsa->data->ls_age =
			htons(OSPF_LSA_MAXAGE); /* Flush it anyway. */
	}

	/* Verify that the Router Information ID is supported */
	if (GET_OPAQUE_ID(ntohl(lsa->data->id.s_addr)) != 0) {
		flog_warn(
			EC_OSPF_LSA,
			"RI (%s): Unsupported Router Information ID",
			__func__);
		return NULL;
	}

	/* Process LSA depending of the flooding scope */
	if (OspfRI.scope == OSPF_OPAQUE_AREA_LSA) {
		/* Get context AREA context */
		ai = lookup_by_area(lsa->area);
		if (ai == NULL) {
			flog_warn(
				EC_OSPF_LSA,
				"RI (%s): No associated Area", __func__);
			return NULL;
		}
		/* Flush LSA, if the lsa's age reached to MaxAge. */
		if (IS_LSA_MAXAGE(lsa)) {
			UNSET_FLAG(ai->flags, RIFLG_LSA_ENGAGED);
			ospf_opaque_lsa_flush_schedule(lsa);
			return NULL;
		}
		/* Create new Opaque-LSA/ROUTER INFORMATION instance. */
		new = ospf_router_info_lsa_new(ai->area);
		new->data->ls_seqnum = lsa_seqnum_increment(lsa);
		/* Install this LSA into LSDB. */
		/* Given "lsa" will be freed in the next function. */
		top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
		if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
			flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
				  "RI (%s): ospf_lsa_install() ?", __func__);
			ospf_lsa_unlock(&new);
			return new;
		}
		/* Flood updated LSA through AREA */
		ospf_flood_through_area(ai->area, NULL /*nbr */, new);

	} else { /* AS Flooding scope */
		/* Flush LSA, if the lsa's age reached to MaxAge. */
		if (IS_LSA_MAXAGE(lsa)) {
			UNSET_FLAG(OspfRI.as_flags, RIFLG_LSA_ENGAGED);
			ospf_opaque_lsa_flush_schedule(lsa);
			return NULL;
		}
		/* Create new Opaque-LSA/ROUTER INFORMATION instance. */
		new = ospf_router_info_lsa_new(NULL);
		new->data->ls_seqnum = lsa_seqnum_increment(lsa);
		/* Install this LSA into LSDB. */
		/* Given "lsa" will be freed in the next function. */
		top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
		if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
			flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
				  "RI (%s): ospf_lsa_install() ?", __func__);
			ospf_lsa_unlock(&new);
			return new;
		}
		/* Flood updated LSA through AS */
		ospf_flood_through_as(top, NULL /*nbr */, new);
	}

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

	return new;
}

static void ospf_router_info_lsa_schedule(struct ospf_ri_area_info *ai,
					  enum lsa_opcode opcode)
{
	struct ospf_lsa lsa;
	struct lsa_header lsah;
	struct ospf *top;
	uint32_t tmp;

	memset(&lsa, 0, sizeof(lsa));
	memset(&lsah, 0, sizeof(lsah));

	zlog_debug("RI (%s): LSA schedule %s%s%s", __func__,
		   opcode == REORIGINATE_THIS_LSA ? "Re-Originate" : "",
		   opcode == REFRESH_THIS_LSA ? "Refresh" : "",
		   opcode == FLUSH_THIS_LSA ? "Flush" : "");

	/* Check LSA flags state coherence and collect area information */
	if (OspfRI.scope == OSPF_OPAQUE_AREA_LSA) {
		if ((ai == NULL) || (ai->area == NULL)) {
			flog_warn(
				EC_OSPF_LSA,
				"RI (%s): Router Info is Area scope flooding but area is not set",
				__func__);
				return;
		}

		if (!CHECK_FLAG(ai->flags, RIFLG_LSA_ENGAGED)
		    && (opcode != REORIGINATE_THIS_LSA))
			return;

		if (CHECK_FLAG(ai->flags, RIFLG_LSA_ENGAGED)
		    && (opcode == REORIGINATE_THIS_LSA))
			opcode = REFRESH_THIS_LSA;

		lsa.area = ai->area;
		top = ai->area->ospf;
	} else {
		if (!CHECK_FLAG(OspfRI.as_flags, RIFLG_LSA_ENGAGED)
		    && (opcode != REORIGINATE_THIS_LSA))
			return;

		if (CHECK_FLAG(OspfRI.as_flags, RIFLG_LSA_ENGAGED)
		    && (opcode == REORIGINATE_THIS_LSA))
			opcode = REFRESH_THIS_LSA;

		top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
		lsa.area = NULL;
	}

	lsa.data = &lsah;
	lsah.type = OspfRI.scope;

	/* LSA ID is set to 0 for the Router Information. See RFC 4970 */
	tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_ROUTER_INFORMATION_LSA, 0);
	lsah.id.s_addr = htonl(tmp);

	switch (opcode) {
	case REORIGINATE_THIS_LSA:
		if (OspfRI.scope == OSPF_OPAQUE_AREA_LSA)
			ospf_opaque_lsa_reoriginate_schedule(
				(void *)ai->area, OSPF_OPAQUE_AREA_LSA,
				OPAQUE_TYPE_ROUTER_INFORMATION_LSA);
		else
			ospf_opaque_lsa_reoriginate_schedule(
				(void *)top, OSPF_OPAQUE_AS_LSA,
				OPAQUE_TYPE_ROUTER_INFORMATION_LSA);
		break;
	case REFRESH_THIS_LSA:
		ospf_opaque_lsa_refresh_schedule(&lsa);
		break;
	case FLUSH_THIS_LSA:
		if (OspfRI.scope == OSPF_OPAQUE_AREA_LSA)
			UNSET_FLAG(ai->flags, RIFLG_LSA_ENGAGED);
		else
			UNSET_FLAG(OspfRI.as_flags, RIFLG_LSA_ENGAGED);
		ospf_opaque_lsa_flush_schedule(&lsa);
		break;
	}

	return;
}

/* Callback to handle Segment Routing information */
static int ospf_router_info_lsa_update(struct ospf_lsa *lsa)
{

	/* Sanity Check */
	if (lsa == NULL) {
		flog_warn(EC_OSPF_LSA, "RI (%s): Abort! LSA is NULL",
			  __func__);
		return -1;
	}

	/* Process only Opaque LSA */
	if ((lsa->data->type != OSPF_OPAQUE_AREA_LSA)
	    && (lsa->data->type != OSPF_OPAQUE_AS_LSA))
		return 0;

	/* Process only Router Information LSA */
	if (GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr))
	    != OPAQUE_TYPE_ROUTER_INFORMATION_LSA)
		return 0;

	/* Check if it is not my LSA */
	if (IS_LSA_SELF(lsa))
		return 0;

	/* Check if Router Info & Segment Routing are enable */
	if (!OspfRI.enabled || !OspfRI.sr_info.enabled)
		return 0;

	/* Call Segment Routing LSA update or deletion */
	if (!IS_LSA_MAXAGE(lsa))
		ospf_sr_ri_lsa_update(lsa);
	else
		ospf_sr_ri_lsa_delete(lsa);

	return 0;
}

/*------------------------------------------------------------------------*
 * Following are vty session control functions.
 *------------------------------------------------------------------------*/

#define check_tlv_size(size, msg)                                              \
	do {                                                                   \
		if (ntohs(tlvh->length) > size) {                              \
			if (vty != NULL)                                       \
				vty_out(vty, "  Wrong %s TLV size: %d(%d)\n",  \
					msg, ntohs(tlvh->length), size);       \
			else                                                   \
				zlog_debug("    Wrong %s TLV size: %d(%d)",    \
					   msg, ntohs(tlvh->length), size);    \
			return size + TLV_HDR_SIZE;                            \
		}                                                              \
	} while (0)

static uint16_t show_vty_router_cap(struct vty *vty, struct tlv_header *tlvh,
				    json_object *json)
{
	struct ri_tlv_router_cap *top = (struct ri_tlv_router_cap *)tlvh;

	check_tlv_size(RI_TLV_CAPABILITIES_SIZE, "Router Capabilities");

	if (vty != NULL)
		if (!json)
			vty_out(vty, "  Router Capabilities: 0x%x\n",
				ntohl(top->value));
		else
			json_object_string_addf(json, "routerCapabilities",
						"0x%x", ntohl(top->value));
	else
		zlog_debug("    Router Capabilities: 0x%x", ntohl(top->value));

	return TLV_SIZE(tlvh);
}

static uint16_t show_vty_pce_subtlv_address(struct vty *vty,
					    struct tlv_header *tlvh,
					    json_object *json)
{
	struct ri_pce_subtlv_address *top =
		(struct ri_pce_subtlv_address *)tlvh;

	if (ntohs(top->address.type) == PCE_ADDRESS_IPV4) {
		check_tlv_size(PCE_ADDRESS_IPV4_SIZE, "PCE Address");
		if (vty != NULL)
			if (!json)
				vty_out(vty, "  PCE Address: %pI4\n",
					&top->address.value);
			else
				json_object_string_addf(json, "pceAddress",
							"%pI4",
							&top->address.value);
		else
			zlog_debug("    PCE Address: %pI4",
				   &top->address.value);
	} else if (ntohs(top->address.type) == PCE_ADDRESS_IPV6) {
		check_tlv_size(PCE_ADDRESS_IPV6_SIZE, "PCE Address");
		if (vty != NULL)
			if (!json)
				vty_out(vty,
					"  PCE Address: unsupported IPv6\n");
			else
				json_object_string_add(json, "pceAddress",
						       "unsupported IPv6");

		else
			zlog_debug("    PCE Address: unsupported IPv6");
	} else {
		if (vty != NULL)
			vty_out(vty, "  Wrong PCE Address type: 0x%x\n",
				ntohl(top->address.type));
		else
			zlog_debug("    Wrong PCE Address type: 0x%x",
				   ntohl(top->address.type));
	}

	return TLV_SIZE(tlvh);
}

static uint16_t show_vty_pce_subtlv_path_scope(struct vty *vty,
					       struct tlv_header *tlvh,
					       json_object *json)
{
	struct ri_pce_subtlv_path_scope *top =
		(struct ri_pce_subtlv_path_scope *)tlvh;

	check_tlv_size(RI_PCE_SUBTLV_PATH_SCOPE_SIZE, "PCE Path Scope");

	if (vty != NULL)
		if (!json)
			vty_out(vty, "  PCE Path Scope: 0x%x\n",
				ntohl(top->value));
		else
			json_object_string_addf(json, "pcePathScope", "0x%x",
						ntohl(top->value));
	else
		zlog_debug("    PCE Path Scope: 0x%x", ntohl(top->value));

	return TLV_SIZE(tlvh);
}

static uint16_t show_vty_pce_subtlv_domain(struct vty *vty,
					   struct tlv_header *tlvh,
					   json_object *json)
{
	struct ri_pce_subtlv_domain *top = (struct ri_pce_subtlv_domain *)tlvh;
	struct in_addr tmp;

	check_tlv_size(RI_PCE_SUBTLV_DOMAIN_SIZE, "PCE Domain");

	if (ntohs(top->type) == PCE_DOMAIN_TYPE_AREA) {
		tmp.s_addr = top->value;
		if (vty != NULL)
			if (!json)
				vty_out(vty, "  PCE Domain Area: %pI4\n", &tmp);
			else
				json_object_string_addf(json, "pceDomainArea",
							"%pI4", &tmp);
		else
			zlog_debug("    PCE Domain Area: %pI4", &tmp);
	} else if (ntohs(top->type) == PCE_DOMAIN_TYPE_AS) {
		if (vty != NULL)
			if (!json)
				vty_out(vty, "  PCE Domain AS: %d\n",
					ntohl(top->value));
			else
				json_object_int_add(json, "pceDomainAS",
						    ntohl(top->value));
		else
			zlog_debug("    PCE Domain AS: %d", ntohl(top->value));
	} else {
		if (vty != NULL)
			vty_out(vty, "  Wrong PCE Domain type: %d\n",
				ntohl(top->type));
		else
			zlog_debug("    Wrong PCE Domain type: %d",
				   ntohl(top->type));
	}

	return TLV_SIZE(tlvh);
}

static uint16_t show_vty_pce_subtlv_neighbor(struct vty *vty,
					     struct tlv_header *tlvh,
					     json_object *json)
{

	struct ri_pce_subtlv_neighbor *top =
		(struct ri_pce_subtlv_neighbor *)tlvh;
	struct in_addr tmp;

	check_tlv_size(RI_PCE_SUBTLV_NEIGHBOR_SIZE, "PCE Neighbor");

	if (ntohs(top->type) == PCE_DOMAIN_TYPE_AREA) {
		tmp.s_addr = top->value;
		if (vty != NULL)
			if (!json)
				vty_out(vty, "  PCE Neighbor Area: %pI4\n",
					&tmp);
			else
				json_object_string_addf(json, "pceNeighborArea",
							"%pI4", &tmp);
		else
			zlog_debug("    PCE Neighbor Area: %pI4", &tmp);
	} else if (ntohs(top->type) == PCE_DOMAIN_TYPE_AS) {
		if (vty != NULL)
			if (!json)
				vty_out(vty, "  PCE Neighbor AS: %d\n",
					ntohl(top->value));
			else
				json_object_int_add(json, "pceNeighborAS",
						    ntohl(top->value));
		else
			zlog_debug("    PCE Neighbor AS: %d",
				   ntohl(top->value));
	} else {
		if (vty != NULL)
			vty_out(vty, "  Wrong PCE Neighbor type: %d\n",
				ntohl(top->type));
		else
			zlog_debug("    Wrong PCE Neighbor type: %d",
				   ntohl(top->type));
	}

	return TLV_SIZE(tlvh);
}

static uint16_t show_vty_pce_subtlv_cap_flag(struct vty *vty,
					     struct tlv_header *tlvh,
					     json_object *json)
{
	struct ri_pce_subtlv_cap_flag *top =
		(struct ri_pce_subtlv_cap_flag *)tlvh;

	check_tlv_size(RI_PCE_SUBTLV_CAP_FLAG_SIZE, "PCE Capabilities");

	if (vty != NULL)
		if (!json)
			vty_out(vty, "  PCE Capabilities Flag: 0x%x\n",
				ntohl(top->value));
		else
			json_object_string_addf(json, "pceCapabilities",
						"0x%x", ntohl(top->value));
	else
		zlog_debug("    PCE Capabilities Flag: 0x%x",
			   ntohl(top->value));

	return TLV_SIZE(tlvh);
}

static uint16_t show_vty_unknown_tlv(struct vty *vty, struct tlv_header *tlvh,
				     size_t buf_size, json_object *json)
{
	json_object *obj;

	if (TLV_SIZE(tlvh) > buf_size) {
		if (vty != NULL)
			vty_out(vty,
				"    TLV size %d exceeds buffer size. Abort!",
				TLV_SIZE(tlvh));
		else
			zlog_debug(
				"    TLV size %d exceeds buffer size. Abort!",
				TLV_SIZE(tlvh));
		return buf_size;
	}

	if (vty != NULL)
		if (!json)
			vty_out(vty,
				"  Unknown TLV: [type(0x%x), length(0x%x)]\n",
				ntohs(tlvh->type), ntohs(tlvh->length));
		else {
			obj = json_object_new_object();
			json_object_string_addf(obj, "type", "0x%x",
						ntohs(tlvh->type));
			json_object_string_addf(obj, "length", "0x%x",
						ntohs(tlvh->length));
			json_object_object_add(json, "unknownTLV", obj);
		}
	else
		zlog_debug("    Unknown TLV: [type(0x%x), length(0x%x)]",
			   ntohs(tlvh->type), ntohs(tlvh->length));

	return TLV_SIZE(tlvh);
}

static uint16_t show_vty_pce_info(struct vty *vty, struct tlv_header *ri,
				  size_t buf_size, json_object *json)
{
	struct tlv_header *tlvh;
	uint16_t length = ntohs(ri->length);
	uint16_t sum = 0;

	/* Verify that TLV length is valid against remaining buffer size */
	if (length > buf_size) {
		vty_out(vty,
			"  PCE Info TLV size %d exceeds buffer size. Abort!\n",
			length);
		return buf_size;
	}

	for (tlvh = ri; sum < length; tlvh = TLV_HDR_NEXT(tlvh)) {
		switch (ntohs(tlvh->type)) {
		case RI_PCE_SUBTLV_ADDRESS:
			sum += show_vty_pce_subtlv_address(vty, tlvh, json);
			break;
		case RI_PCE_SUBTLV_PATH_SCOPE:
			sum += show_vty_pce_subtlv_path_scope(vty, tlvh, json);
			break;
		case RI_PCE_SUBTLV_DOMAIN:
			sum += show_vty_pce_subtlv_domain(vty, tlvh, json);
			break;
		case RI_PCE_SUBTLV_NEIGHBOR:
			sum += show_vty_pce_subtlv_neighbor(vty, tlvh, json);
			break;
		case RI_PCE_SUBTLV_CAP_FLAG:
			sum += show_vty_pce_subtlv_cap_flag(vty, tlvh, json);
			break;
		default:
			sum += show_vty_unknown_tlv(vty, tlvh, length - sum,
						    json);
			break;
		}
	}
	return sum;
}

/* Display Segment Routing Algorithm TLV information */
static uint16_t show_vty_sr_algorithm(struct vty *vty, struct tlv_header *tlvh,
				      json_object *json)
{
	struct ri_sr_tlv_sr_algorithm *algo =
		(struct ri_sr_tlv_sr_algorithm *)tlvh;
	int i;
	json_object *json_algo, *obj;
	char buf[2];

	check_tlv_size(ALGORITHM_COUNT, "Segment Routing Algorithm");

	if (vty != NULL)
		if (!json) {
			vty_out(vty, "  Segment Routing Algorithm TLV:\n");
			for (i = 0; i < ntohs(algo->header.length); i++) {
				switch (algo->value[i]) {
				case 0:
					vty_out(vty,
						"    Algorithm %d: SPF\n", i);
					break;
				case 1:
					vty_out(vty,
						"    Algorithm %d: Strict SPF\n",
						i);
					break;
				default:
					vty_out(vty,
						"  Algorithm %d: Unknown value %d\n", i,
						algo->value[i]);
					break;
				}
			}
		} else {
			json_algo = json_object_new_array();
			json_object_object_add(json, "algorithms",
					       json_algo);
			for (i = 0; i < ntohs(algo->header.length); i++) {
				obj = json_object_new_object();
				snprintfrr(buf, 2, "%d", i);
				switch (algo->value[i]) {
				case 0:
					json_object_string_add(obj, buf, "SPF");
					break;
				case 1:
					json_object_string_add(obj, buf,
							       "strictSPF");
					break;
				default:
					json_object_string_add(obj, buf,
							       "unknown");
					break;
				}
				json_object_array_add(json_algo, obj);
			}
		}
	else {
		zlog_debug("  Segment Routing Algorithm TLV:");
		for (i = 0; i < ntohs(algo->header.length); i++)
			switch (algo->value[i]) {
			case 0:
				zlog_debug("    Algorithm %d: SPF", i);
				break;
			case 1:
				zlog_debug("    Algorithm %d: Strict SPF", i);
				break;
			default:
				zlog_debug("    Algorithm %d: Unknown value %d",
					   i, algo->value[i]);
				break;
			}
	}

	return TLV_SIZE(tlvh);
}

/* Display Segment Routing SID/Label Range TLV information */
static uint16_t show_vty_sr_range(struct vty *vty, struct tlv_header *tlvh,
				  json_object *json)
{
	struct ri_sr_tlv_sid_label_range *range =
		(struct ri_sr_tlv_sid_label_range *)tlvh;
	json_object *obj;
	uint32_t upper;

	check_tlv_size(RI_SR_TLV_LABEL_RANGE_SIZE, "SR Label Range");

	if (vty != NULL)
		if (!json) {
			vty_out(vty,
				"  Segment Routing %s Range TLV:\n"
				"    Range Size = %d\n"
				"    SID Label = %d\n\n",
				ntohs(range->header.type) ==
						RI_SR_TLV_SRGB_LABEL_RANGE
					? "Global"
					: "Local",
				GET_RANGE_SIZE(ntohl(range->size)),
				GET_LABEL(ntohl(range->lower.value)));
		} else {
			/*
			 * According to draft-ietf-teas-yang-sr-te-topo, SRGB
			 * and SRLB are describe with lower and upper bounds
			 */
			upper = GET_LABEL(ntohl(range->lower.value)) +
				GET_RANGE_SIZE(ntohl(range->size)) - 1;
			obj = json_object_new_object();
			json_object_int_add(obj, "upperBound", upper);
			json_object_int_add(obj, "lowerBound",
				GET_LABEL(ntohl(range->lower.value)));
			json_object_object_add(json,
					       ntohs(range->header.type) ==
						     RI_SR_TLV_SRGB_LABEL_RANGE
						     ? "srgb"
						     : "srlb",
					       obj);
		}
	else {
		zlog_debug(
			"  Segment Routing %s Range TLV:  Range Size = %d  SID Label = %d",
			ntohs(range->header.type) == RI_SR_TLV_SRGB_LABEL_RANGE
				? "Global"
				: "Local",
			GET_RANGE_SIZE(ntohl(range->size)),
			GET_LABEL(ntohl(range->lower.value)));
	}

	return TLV_SIZE(tlvh);
}

/* Display Segment Routing Maximum Stack Depth TLV information */
static uint16_t show_vty_sr_msd(struct vty *vty, struct tlv_header *tlvh,
				json_object *json)
{
	struct ri_sr_tlv_node_msd *msd = (struct ri_sr_tlv_node_msd *)tlvh;

	check_tlv_size(RI_SR_TLV_NODE_MSD_SIZE, "Node Maximum Stack Depth");

	if (vty != NULL)
		if (!json)
			vty_out(vty,
				"  Segment Routing MSD TLV:\n"
				"    Node Maximum Stack Depth = %d\n",
				msd->value);
		else
			json_object_int_add(json, "nodeMsd", msd->value);
	else
		zlog_debug(
			"  Segment Routing MSD TLV:  Node Maximum Stack Depth = %d",
			msd->value);

	return TLV_SIZE(tlvh);
}

static void ospf_router_info_show_info(struct vty *vty,
				       struct json_object *json,
				       struct ospf_lsa *lsa)
{
	struct lsa_header *lsah = lsa->data;
	struct tlv_header *tlvh;
	uint16_t length = 0, sum = 0;
	json_object *jri = NULL, *jpce = NULL, *jsr = NULL;

	if (json) {
		jri = json_object_new_object();
		json_object_object_add(json, "routerInformation", jri);
		jpce = json_object_new_object();
		jsr = json_object_new_object();
	}

	/* Initialize TLV browsing */
	length = lsa->size - OSPF_LSA_HEADER_SIZE;

	for (tlvh = TLV_HDR_TOP(lsah); sum < length && tlvh;
	     tlvh = TLV_HDR_NEXT(tlvh)) {
		switch (ntohs(tlvh->type)) {
		case RI_TLV_CAPABILITIES:
			sum += show_vty_router_cap(vty, tlvh, jri);
			break;
		case RI_TLV_PCE:
			tlvh++;
			sum += TLV_HDR_SIZE;
			sum += show_vty_pce_info(vty, tlvh, length - sum, jpce);
			break;
		case RI_SR_TLV_SR_ALGORITHM:
			sum += show_vty_sr_algorithm(vty, tlvh, jsr);
			break;
		case RI_SR_TLV_SRGB_LABEL_RANGE:
		case RI_SR_TLV_SRLB_LABEL_RANGE:
			sum += show_vty_sr_range(vty, tlvh, jsr);
			break;
		case RI_SR_TLV_NODE_MSD:
			sum += show_vty_sr_msd(vty, tlvh, jsr);
			break;

		default:
			sum += show_vty_unknown_tlv(vty, tlvh, length, jri);
			break;
		}
	}

	if (json) {
		if (json_object_object_length(jpce) > 1)
			json_object_object_add(jri, "pceInformation", jpce);
		if (json_object_object_length(jsr) > 1)
			json_object_object_add(jri, "segmentRouting", jsr);
	}
	return;
}

static void ospf_router_info_config_write_router(struct vty *vty)
{
	struct ospf_pce_info *pce = &OspfRI.pce_info;
	struct listnode *node;
	struct ri_pce_subtlv_domain *domain;
	struct ri_pce_subtlv_neighbor *neighbor;
	struct in_addr tmp;

	if (!OspfRI.enabled)
		return;

	if (OspfRI.scope == OSPF_OPAQUE_AS_LSA)
		vty_out(vty, " router-info as\n");
	else
		vty_out(vty, " router-info area\n");

	if (OspfRI.pce_info.enabled) {

		if (pce->pce_address.header.type != 0)
			vty_out(vty, "  pce address %pI4\n",
				&pce->pce_address.address.value);

		if (pce->pce_cap_flag.header.type != 0)
			vty_out(vty, "  pce flag 0x%x\n",
				ntohl(pce->pce_cap_flag.value));

		for (ALL_LIST_ELEMENTS_RO(pce->pce_domain, node, domain)) {
			if (domain->header.type != 0) {
				if (domain->type == PCE_DOMAIN_TYPE_AREA) {
					tmp.s_addr = domain->value;
					vty_out(vty, "  pce domain area %pI4\n",
						&tmp);
				} else {
					vty_out(vty, "  pce domain as %d\n",
						ntohl(domain->value));
				}
			}
		}

		for (ALL_LIST_ELEMENTS_RO(pce->pce_neighbor, node, neighbor)) {
			if (neighbor->header.type != 0) {
				if (neighbor->type == PCE_DOMAIN_TYPE_AREA) {
					tmp.s_addr = neighbor->value;
					vty_out(vty,
						"  pce neighbor area %pI4\n",
						&tmp);
				} else {
					vty_out(vty, "  pce neighbor as %d\n",
						ntohl(neighbor->value));
				}
			}
		}

		if (pce->pce_scope.header.type != 0)
			vty_out(vty, "  pce scope 0x%x\n",
				ntohl(OspfRI.pce_info.pce_scope.value));
	}
	return;
}

/*------------------------------------------------------------------------*
 * Following are vty command functions.
 *------------------------------------------------------------------------*/
/* Simple wrapper schedule RI LSA action in function of the scope */
static void ospf_router_info_schedule(enum lsa_opcode opcode)
{
	struct listnode *node, *nnode;
	struct ospf_ri_area_info *ai;

	if (OspfRI.scope == OSPF_OPAQUE_AS_LSA) {
		if (CHECK_FLAG(OspfRI.as_flags, RIFLG_LSA_ENGAGED))
			ospf_router_info_lsa_schedule(NULL, opcode);
		else if (opcode == REORIGINATE_THIS_LSA)
			ospf_router_info_lsa_schedule(NULL, opcode);
	} else {
		for (ALL_LIST_ELEMENTS(OspfRI.area_info, node, nnode, ai)) {
			if (CHECK_FLAG(ai->flags, RIFLG_LSA_ENGAGED))
				ospf_router_info_lsa_schedule(ai, opcode);
		}
	}
}

DEFUN (router_info,
       router_info_area_cmd,
       "router-info <as|area [A.B.C.D]>",
       OSPF_RI_STR
       "Enable the Router Information functionality with AS flooding scope\n"
       "Enable the Router Information functionality with Area flooding scope\n"
       "OSPF area ID in IP format (deprecated)\n")
{
	int idx_mode = 1;
	uint8_t scope;
	VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);

	if (OspfRI.enabled)
		return CMD_SUCCESS;

	/* Check that the OSPF is using default VRF */
	if (ospf->vrf_id != VRF_DEFAULT) {
		vty_out(vty,
			"Router Information is only supported in default VRF\n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* Check and get Area value if present */
	if (strncmp(argv[idx_mode]->arg, "as", 2) == 0)
		scope = OSPF_OPAQUE_AS_LSA;
	else
		scope = OSPF_OPAQUE_AREA_LSA;

	/* First start to register Router Information callbacks */
	if (!OspfRI.registered && (ospf_router_info_register(scope)) != 0) {
		vty_out(vty,
			"%% Unable to register Router Information callbacks.");
		flog_err(
			EC_OSPF_INIT_FAIL,
			"RI (%s): Unable to register Router Information callbacks. Abort!",
			__func__);
		return CMD_WARNING_CONFIG_FAILED;
	}

	OspfRI.enabled = true;

	if (IS_DEBUG_OSPF_EVENT)
		zlog_debug("RI-> Router Information (%s flooding): OFF -> ON",
			   OspfRI.scope == OSPF_OPAQUE_AREA_LSA ? "Area"
								: "AS");

	/*
	 * Following code is intended to handle two cases;
	 *
	 * 1) Router Information was disabled at startup time, but now become
	 * enabled.
	 * 2) Router Information was once enabled then disabled, and now enabled
	 * again.
	 */

	initialize_params(&OspfRI);

	/* Originate or Refresh RI LSA if already engaged */
	ospf_router_info_schedule(REORIGINATE_THIS_LSA);
	return CMD_SUCCESS;
}


DEFUN (no_router_info,
       no_router_info_cmd,
       "no router-info [<area|as>]",
       NO_STR
       "Disable the Router Information functionality\n"
       "Disable the Router Information functionality with AS flooding scope\n"
       "Disable the Router Information functionality with Area flooding scope\n")
{

	if (!OspfRI.enabled)
		return CMD_SUCCESS;

	if (IS_DEBUG_OSPF_EVENT)
		zlog_debug("RI-> Router Information: ON -> OFF");

	ospf_router_info_schedule(FLUSH_THIS_LSA);

	OspfRI.enabled = false;

	return CMD_SUCCESS;
}

static int ospf_ri_enabled(struct vty *vty)
{
	if (OspfRI.enabled)
		return 1;

	if (vty)
		vty_out(vty, "%% OSPF RI is not turned on\n");

	return 0;
}

DEFUN (pce_address,
       pce_address_cmd,
       "pce address A.B.C.D",
       PCE_STR
       "Stable IP address of the PCE\n"
       "PCE address in IPv4 address format\n")
{
	int idx_ipv4 = 2;
	struct in_addr value;
	struct ospf_pce_info *pi = &OspfRI.pce_info;

	if (!ospf_ri_enabled(vty))
		return CMD_WARNING_CONFIG_FAILED;

	if (!inet_aton(argv[idx_ipv4]->arg, &value)) {
		vty_out(vty, "Please specify PCE Address by A.B.C.D\n");
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (ntohs(pi->pce_address.header.type) == 0
	    || ntohl(pi->pce_address.address.value.s_addr)
		       != ntohl(value.s_addr)) {

		set_pce_address(value, pi);

		/* Refresh RI LSA if already engaged */
		ospf_router_info_schedule(REFRESH_THIS_LSA);
	}

	return CMD_SUCCESS;
}

DEFUN (no_pce_address,
       no_pce_address_cmd,
       "no pce address [A.B.C.D]",
       NO_STR
       PCE_STR
       "Disable PCE address\n"
       "PCE address in IPv4 address format\n")
{

	unset_param(&OspfRI.pce_info.pce_address);

	/* Refresh RI LSA if already engaged */
	ospf_router_info_schedule(REFRESH_THIS_LSA);

	return CMD_SUCCESS;
}

DEFUN (pce_path_scope,
       pce_path_scope_cmd,
       "pce scope BITPATTERN",
       PCE_STR
       "Path scope visibilities of the PCE for path computation\n"
       "32-bit Hexadecimal value\n")
{
	int idx_bitpattern = 2;
	uint32_t scope;
	struct ospf_pce_info *pi = &OspfRI.pce_info;

	if (!ospf_ri_enabled(vty))
		return CMD_WARNING_CONFIG_FAILED;

	if (sscanf(argv[idx_bitpattern]->arg, "0x%x", &scope) != 1) {
		vty_out(vty, "pce_path_scope: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (ntohl(pi->pce_scope.header.type) == 0
	    || scope != pi->pce_scope.value) {
		set_pce_path_scope(scope, pi);

		/* Refresh RI LSA if already engaged */
		ospf_router_info_schedule(REFRESH_THIS_LSA);
	}

	return CMD_SUCCESS;
}

DEFUN (no_pce_path_scope,
       no_pce_path_scope_cmd,
       "no pce scope [BITPATTERN]",
       NO_STR
       PCE_STR
       "Disable PCE path scope\n"
       "32-bit Hexadecimal value\n")
{

	unset_param(&OspfRI.pce_info.pce_address);

	/* Refresh RI LSA if already engaged */
	ospf_router_info_schedule(REFRESH_THIS_LSA);

	return CMD_SUCCESS;
}

DEFUN (pce_domain,
       pce_domain_cmd,
       "pce domain as (0-65535)",
       PCE_STR
       "Configure PCE domain AS number\n"
       "AS number where the PCE as visibilities for path computation\n"
       "AS number in decimal <0-65535>\n")
{
	int idx_number = 3;

	uint32_t as;
	struct ospf_pce_info *pce = &OspfRI.pce_info;
	struct listnode *node;
	struct ri_pce_subtlv_domain *domain;

	if (!ospf_ri_enabled(vty))
		return CMD_WARNING_CONFIG_FAILED;

	if (sscanf(argv[idx_number]->arg, "%" SCNu32, &as) != 1) {
		vty_out(vty, "pce_domain: fscanf: %s\n", safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* Check if the domain is not already in the domain list */
	for (ALL_LIST_ELEMENTS_RO(pce->pce_domain, node, domain)) {
		if (ntohl(domain->header.type) == 0 && as == domain->value)
			return CMD_SUCCESS;
	}

	/* Create new domain if not found */
	set_pce_domain(PCE_DOMAIN_TYPE_AS, as, pce);

	/* Refresh RI LSA if already engaged */
	ospf_router_info_schedule(REFRESH_THIS_LSA);

	return CMD_SUCCESS;
}

DEFUN (no_pce_domain,
       no_pce_domain_cmd,
       "no pce domain as (0-65535)",
       NO_STR
       PCE_STR
       "Disable PCE domain AS number\n"
       "AS number where the PCE as visibilities for path computation\n"
       "AS number in decimal <0-65535>\n")
{
	int idx_number = 4;

	uint32_t as;
	struct ospf_pce_info *pce = &OspfRI.pce_info;

	if (sscanf(argv[idx_number]->arg, "%" SCNu32, &as) != 1) {
		vty_out(vty, "no_pce_domain: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* Unset corresponding PCE domain */
	unset_pce_domain(PCE_DOMAIN_TYPE_AS, as, pce);

	/* Refresh RI LSA if already engaged */
	ospf_router_info_schedule(REFRESH_THIS_LSA);

	return CMD_SUCCESS;
}

DEFUN (pce_neigbhor,
       pce_neighbor_cmd,
       "pce neighbor as (0-65535)",
       PCE_STR
       "Configure PCE neighbor domain AS number\n"
       "AS number of PCE neighbors\n"
       "AS number in decimal <0-65535>\n")
{
	int idx_number = 3;

	uint32_t as;
	struct ospf_pce_info *pce = &OspfRI.pce_info;
	struct listnode *node;
	struct ri_pce_subtlv_neighbor *neighbor;

	if (!ospf_ri_enabled(vty))
		return CMD_WARNING_CONFIG_FAILED;

	if (sscanf(argv[idx_number]->arg, "%" SCNu32, &as) != 1) {
		vty_out(vty, "pce_neighbor: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* Check if the domain is not already in the domain list */
	for (ALL_LIST_ELEMENTS_RO(pce->pce_neighbor, node, neighbor)) {
		if (ntohl(neighbor->header.type) == 0 && as == neighbor->value)
			return CMD_SUCCESS;
	}

	/* Create new domain if not found */
	set_pce_neighbor(PCE_DOMAIN_TYPE_AS, as, pce);

	/* Refresh RI LSA if already engaged */
	ospf_router_info_schedule(REFRESH_THIS_LSA);

	return CMD_SUCCESS;
}

DEFUN (no_pce_neighbor,
       no_pce_neighbor_cmd,
       "no pce neighbor as (0-65535)",
       NO_STR
       PCE_STR
       "Disable PCE neighbor AS number\n"
       "AS number of PCE neighbor\n"
       "AS number in decimal <0-65535>\n")
{
	int idx_number = 4;

	uint32_t as;
	struct ospf_pce_info *pce = &OspfRI.pce_info;

	if (sscanf(argv[idx_number]->arg, "%" SCNu32, &as) != 1) {
		vty_out(vty, "no_pce_neighbor: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	/* Unset corresponding PCE domain */
	unset_pce_neighbor(PCE_DOMAIN_TYPE_AS, as, pce);

	/* Refresh RI LSA if already engaged */
	ospf_router_info_schedule(REFRESH_THIS_LSA);

	return CMD_SUCCESS;
}

DEFUN (pce_cap_flag,
       pce_cap_flag_cmd,
       "pce flag BITPATTERN",
       PCE_STR
       "Capabilities of the PCE for path computation\n"
       "32-bit Hexadecimal value\n")
{
	int idx_bitpattern = 2;

	uint32_t cap;
	struct ospf_pce_info *pce = &OspfRI.pce_info;

	if (!ospf_ri_enabled(vty))
		return CMD_WARNING_CONFIG_FAILED;

	if (sscanf(argv[idx_bitpattern]->arg, "0x%x", &cap) != 1) {
		vty_out(vty, "pce_cap_flag: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}

	if (ntohl(pce->pce_cap_flag.header.type) == 0
	    || cap != pce->pce_cap_flag.value) {
		set_pce_cap_flag(cap, pce);

		/* Refresh RI LSA if already engaged */
		ospf_router_info_schedule(REFRESH_THIS_LSA);
	}

	return CMD_SUCCESS;
}

DEFUN (no_pce_cap_flag,
       no_pce_cap_flag_cmd,
       "no pce flag",
       NO_STR
       PCE_STR
       "Disable PCE capabilities\n")
{

	unset_param(&OspfRI.pce_info.pce_cap_flag);

	/* Refresh RI LSA if already engaged */
	ospf_router_info_schedule(REFRESH_THIS_LSA);

	return CMD_SUCCESS;
}

DEFUN (show_ip_ospf_router_info,
       show_ip_ospf_router_info_cmd,
       "show ip ospf router-info",
       SHOW_STR
       IP_STR
       OSPF_STR
       "Router Information\n")
{

	if (OspfRI.enabled) {
		vty_out(vty, "--- Router Information parameters ---\n");
		show_vty_router_cap(vty, &OspfRI.router_cap.header, NULL);
	} else {
		if (vty != NULL)
			vty_out(vty,
				"  Router Information is disabled on this router\n");
	}
	return CMD_SUCCESS;
}

DEFUN (show_ip_opsf_router_info_pce,
       show_ip_ospf_router_info_pce_cmd,
       "show ip ospf router-info pce",
       SHOW_STR
       IP_STR
       OSPF_STR
       "Router Information\n"
       "PCE information\n")
{

	struct ospf_pce_info *pce = &OspfRI.pce_info;
	struct listnode *node;
	struct ri_pce_subtlv_domain *domain;
	struct ri_pce_subtlv_neighbor *neighbor;

	if ((OspfRI.enabled) && (OspfRI.pce_info.enabled)) {
		vty_out(vty, "--- PCE parameters ---\n");

		if (pce->pce_address.header.type != 0)
			show_vty_pce_subtlv_address(vty,
						    &pce->pce_address.header,
						    NULL);

		if (pce->pce_scope.header.type != 0)
			show_vty_pce_subtlv_path_scope(vty,
						       &pce->pce_scope.header,
						       NULL);

		for (ALL_LIST_ELEMENTS_RO(pce->pce_domain, node, domain)) {
			if (domain->header.type != 0)
				show_vty_pce_subtlv_domain(vty,
							   &domain->header,
							   NULL);
		}

		for (ALL_LIST_ELEMENTS_RO(pce->pce_neighbor, node, neighbor)) {
			if (neighbor->header.type != 0)
				show_vty_pce_subtlv_neighbor(vty,
							     &neighbor->header,
							     NULL);
		}

		if (pce->pce_cap_flag.header.type != 0)
			show_vty_pce_subtlv_cap_flag(vty,
						     &pce->pce_cap_flag.header,
						     NULL);

	} else {
		vty_out(vty, "  PCE info is disabled on this router\n");
	}

	return CMD_SUCCESS;
}

/* Install new CLI commands */
static void ospf_router_info_register_vty(void)
{
	install_element(VIEW_NODE, &show_ip_ospf_router_info_cmd);
	install_element(VIEW_NODE, &show_ip_ospf_router_info_pce_cmd);

	install_element(OSPF_NODE, &router_info_area_cmd);
	install_element(OSPF_NODE, &no_router_info_cmd);
	install_element(OSPF_NODE, &pce_address_cmd);
	install_element(OSPF_NODE, &no_pce_address_cmd);
	install_element(OSPF_NODE, &pce_path_scope_cmd);
	install_element(OSPF_NODE, &no_pce_path_scope_cmd);
	install_element(OSPF_NODE, &pce_domain_cmd);
	install_element(OSPF_NODE, &no_pce_domain_cmd);
	install_element(OSPF_NODE, &pce_neighbor_cmd);
	install_element(OSPF_NODE, &no_pce_neighbor_cmd);
	install_element(OSPF_NODE, &pce_cap_flag_cmd);
	install_element(OSPF_NODE, &no_pce_cap_flag_cmd);

	return;
}