summaryrefslogtreecommitdiffstats
path: root/bgpd/bgp_evpn.c
blob: fc7549671e0f35fa7aa10711745cac6358c1245b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
/* Ethernet-VPN Packet and vty Processing File
 * Copyright (C) 2016 6WIND
 * Copyright (C) 2017 Cumulus Networks, Inc.
 *
 * This file is part of FRR.
 *
 * FRRouting 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.
 *
 * FRRouting 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 "command.h"
#include "filter.h"
#include "prefix.h"
#include "log.h"
#include "memory.h"
#include "stream.h"
#include "hash.h"
#include "jhash.h"
#include "bitfield.h"
#include "zclient.h"

#include "bgpd/bgp_attr_evpn.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_table.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_mplsvpn.h"
#include "bgpd/bgp_label.h"
#include "bgpd/bgp_evpn.h"
#include "bgpd/bgp_evpn_private.h"
#include "bgpd/bgp_ecommunity.h"
#include "bgpd/bgp_encap_types.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_aspath.h"
#include "bgpd/bgp_zebra.h"
#include "bgpd/bgp_nexthop.h"

/*
 * Definitions and external declarations.
 */
extern struct zclient *zclient;

DEFINE_QOBJ_TYPE(bgpevpn)


/*
 * Static function declarations
 */
static void delete_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
				    afi_t afi, safi_t safi, struct bgp_node *rn,
				    struct bgp_info **ri);
static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn);

/*
 * Private functions.
 */

/*
 * Make vni hash key.
 */
static unsigned int vni_hash_key_make(void *p)
{
	struct bgpevpn *vpn = p;
	return (jhash_1word(vpn->vni, 0));
}

/*
 * Comparison function for vni hash
 */
static int vni_hash_cmp(const void *p1, const void *p2)
{
	const struct bgpevpn *vpn1 = p1;
	const struct bgpevpn *vpn2 = p2;

	if (!vpn1 && !vpn2)
		return 1;
	if (!vpn1 || !vpn2)
		return 0;
	return (vpn1->vni == vpn2->vni);
}

/*
 * Make vrf import route target hash key.
 */
static unsigned int vrf_import_rt_hash_key_make(void *p)
{
	struct vrf_irt_node *irt = p;
	char *pnt = irt->rt.val;

	return jhash(pnt, 8, 0x5abc1234);
}

/*
 * Comparison function for vrf import rt hash
 */
static int vrf_import_rt_hash_cmp(const void *p1, const void *p2)
{
	const struct vrf_irt_node *irt1 = p1;
	const struct vrf_irt_node *irt2 = p2;

	if (irt1 == NULL && irt2 == NULL)
		return 1;

	if (irt1 == NULL || irt2 == NULL)
		return 0;

	return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
}

/*
 * Create a new vrf import_rt in default instance
 */
static struct vrf_irt_node *vrf_import_rt_new(struct ecommunity_val *rt)
{
	struct bgp *bgp_def = NULL;
	struct vrf_irt_node *irt;

	bgp_def = bgp_get_default();
	if (!bgp_def) {
		zlog_err("vrf import rt new - def instance not created yet");
		return NULL;
	}

	irt = XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT,
		      sizeof(struct vrf_irt_node));
	if (!irt)
		return NULL;

	irt->rt = *rt;
	irt->vrfs = list_new();

	/* Add to hash */
	if (!hash_get(bgp_def->vrf_import_rt_hash, irt, hash_alloc_intern)) {
		XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
		return NULL;
	}

	return irt;
}

/*
 * Free the vrf import rt node
 */
static void vrf_import_rt_free(struct vrf_irt_node *irt)
{
	struct bgp *bgp_def = NULL;

	bgp_def = bgp_get_default();
	if (!bgp_def) {
		zlog_err("vrf import rt free - def instance not created yet");
		return;
	}

	hash_release(bgp_def->vrf_import_rt_hash, irt);
	XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
}

/*
 * Function to lookup Import RT node - used to map a RT to set of
 * VNIs importing routes with that RT.
 */
static struct vrf_irt_node *lookup_vrf_import_rt(struct ecommunity_val *rt)
{
	struct bgp *bgp_def = NULL;
	struct vrf_irt_node *irt;
	struct vrf_irt_node tmp;

	bgp_def = bgp_get_default();
	if (!bgp_def) {
		zlog_err("vrf import rt lookup - def instance not created yet");
		return NULL;
	}

	memset(&tmp, 0, sizeof(struct vrf_irt_node));
	memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
	irt = hash_lookup(bgp_def->vrf_import_rt_hash, &tmp);
	return irt;
}

/*
 * Is specified VRF present on the RT's list of "importing" VRFs?
 */
static int is_vrf_present_in_irt_vrfs(struct list *vrfs, struct bgp *bgp_vrf)
{
	struct listnode *node = NULL, *nnode = NULL;
	struct bgp *tmp_bgp_vrf = NULL;

	for (ALL_LIST_ELEMENTS(vrfs, node, nnode, tmp_bgp_vrf)) {
		if (tmp_bgp_vrf == bgp_vrf)
			return 1;
	}
	return 0;
}

/*
 * Make import route target hash key.
 */
static unsigned int import_rt_hash_key_make(void *p)
{
	struct irt_node *irt = p;
	char *pnt = irt->rt.val;

	return jhash(pnt, 8, 0xdeadbeef);
}

/*
 * Comparison function for import rt hash
 */
static int import_rt_hash_cmp(const void *p1, const void *p2)
{
	const struct irt_node *irt1 = p1;
	const struct irt_node *irt2 = p2;

	if (irt1 == NULL && irt2 == NULL)
		return 1;

	if (irt1 == NULL || irt2 == NULL)
		return 0;

	return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
}

/*
 * Create a new import_rt
 */
static struct irt_node *import_rt_new(struct bgp *bgp,
				      struct ecommunity_val *rt)
{
	struct irt_node *irt;

	if (!bgp)
		return NULL;

	irt = XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT, sizeof(struct irt_node));
	if (!irt)
		return NULL;

	irt->rt = *rt;
	irt->vnis = list_new();

	/* Add to hash */
	if (!hash_get(bgp->import_rt_hash, irt, hash_alloc_intern)) {
		XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
		return NULL;
	}

	return irt;
}

/*
 * Free the import rt node
 */
static void import_rt_free(struct bgp *bgp, struct irt_node *irt)
{
	hash_release(bgp->import_rt_hash, irt);
	XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
}

/*
 * Function to lookup Import RT node - used to map a RT to set of
 * VNIs importing routes with that RT.
 */
static struct irt_node *lookup_import_rt(struct bgp *bgp,
					 struct ecommunity_val *rt)
{
	struct irt_node *irt;
	struct irt_node tmp;

	memset(&tmp, 0, sizeof(struct irt_node));
	memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
	irt = hash_lookup(bgp->import_rt_hash, &tmp);
	return irt;
}

/*
 * Is specified VNI present on the RT's list of "importing" VNIs?
 */
static int is_vni_present_in_irt_vnis(struct list *vnis, struct bgpevpn *vpn)
{
	struct listnode *node, *nnode;
	struct bgpevpn *tmp_vpn;

	for (ALL_LIST_ELEMENTS(vnis, node, nnode, tmp_vpn)) {
		if (tmp_vpn == vpn)
			return 1;
	}

	return 0;
}

/*
 * Compare Route Targets.
 */
static int evpn_route_target_cmp(struct ecommunity *ecom1,
				 struct ecommunity *ecom2)
{
	if (ecom1 && !ecom2)
		return -1;

	if (!ecom1 && ecom2)
		return 1;

	if (!ecom1 && !ecom2)
		return 0;

	if (ecom1->str && !ecom2->str)
		return -1;

	if (!ecom1->str && ecom2->str)
		return 1;

	if (!ecom1->str && !ecom2->str)
		return 0;

	return strcmp(ecom1->str, ecom2->str);
}

/*
 * Mask off global-admin field of specified extended community (RT),
 * just retain the local-admin field.
 */
static inline void mask_ecom_global_admin(struct ecommunity_val *dst,
					  struct ecommunity_val *src)
{
	u_char type;

	type = src->val[0];
	dst->val[0] = 0;
	if (type == ECOMMUNITY_ENCODE_AS) {
		dst->val[2] = dst->val[3] = 0;
	} else if (type == ECOMMUNITY_ENCODE_AS4
		   || type == ECOMMUNITY_ENCODE_IP) {
		dst->val[2] = dst->val[3] = 0;
		dst->val[4] = dst->val[5] = 0;
	}
}

/*
 * Map one RT to specified VRF.
 * bgp_vrf = BGP vrf instance
 */
static void map_vrf_to_rt(struct bgp *bgp_vrf, struct ecommunity_val *eval)
{
	struct vrf_irt_node *irt = NULL;
	struct ecommunity_val eval_tmp;

	/* If using "automatic" RT,
	 * we only care about the local-admin sub-field.
	 * This is to facilitate using L3VNI(VRF-VNI)
	 * as the RT for EBGP peering too.
	 */
	memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
	if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
		mask_ecom_global_admin(&eval_tmp, eval);

	irt = lookup_vrf_import_rt(&eval_tmp);
	if (irt && irt->vrfs)
		if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
			/* Already mapped. */
			return;

	if (!irt) {
		irt = vrf_import_rt_new(&eval_tmp);
		assert(irt);
	}

	/* Add VRF to the list for this RT. */
	listnode_add(irt->vrfs, bgp_vrf);
}

/*
 * Unmap specified VRF from specified RT. If there are no other
 * VRFs for this RT, then the RT hash is deleted.
 * bgp_vrf: BGP VRF specific instance
 */
static void unmap_vrf_from_rt(struct bgp *bgp_vrf, struct vrf_irt_node *irt)
{
	/* Delete VRF from list for this RT. */
	listnode_delete(irt->vrfs, bgp_vrf);
	if (!listnode_head(irt->vrfs)) {
		list_delete_and_null(&irt->vrfs);
		vrf_import_rt_free(irt);
	}
}

/*
 * Map one RT to specified VNI.
 */
static void map_vni_to_rt(struct bgp *bgp, struct bgpevpn *vpn,
			  struct ecommunity_val *eval)
{
	struct irt_node *irt;
	struct ecommunity_val eval_tmp;

	/* If using "automatic" RT, we only care about the local-admin
	 * sub-field.
	 * This is to facilitate using VNI as the RT for EBGP peering too.
	 */
	memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
	if (!is_import_rt_configured(vpn))
		mask_ecom_global_admin(&eval_tmp, eval);

	irt = lookup_import_rt(bgp, &eval_tmp);
	if (irt && irt->vnis)
		if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
			/* Already mapped. */
			return;

	if (!irt) {
		irt = import_rt_new(bgp, &eval_tmp);
		assert(irt);
	}

	/* Add VNI to the hash list for this RT. */
	listnode_add(irt->vnis, vpn);
}

/*
 * Unmap specified VNI from specified RT. If there are no other
 * VNIs for this RT, then the RT hash is deleted.
 */
static void unmap_vni_from_rt(struct bgp *bgp, struct bgpevpn *vpn,
			      struct irt_node *irt)
{
	/* Delete VNI from hash list for this RT. */
	listnode_delete(irt->vnis, vpn);
	if (!listnode_head(irt->vnis)) {
		list_delete_and_null(&irt->vnis);
		import_rt_free(bgp, irt);
	}
}

/*
 * Create RT extended community automatically from passed information:
 * of the form AS:VNI.
 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
 * the need is to get a RT value that will be unique across different
 * VNIs but the same across routers (in the same AS) for a particular
 * VNI.
 */
static void form_auto_rt(struct bgp *bgp, vni_t vni, struct list *rtl)
{
	struct ecommunity_val eval;
	struct ecommunity *ecomadd;

	encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);

	ecomadd = ecommunity_new();
	ecommunity_add_val(ecomadd, &eval);
	listnode_add_sort(rtl, ecomadd);
}

/*
 * Derive RD and RT for a VNI automatically. Invoked at the time of
 * creation of a VNI.
 */
static void derive_rd_rt_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
{
	bgp_evpn_derive_auto_rd(bgp, vpn);
	bgp_evpn_derive_auto_rt_import(bgp, vpn);
	bgp_evpn_derive_auto_rt_export(bgp, vpn);
}

/*
 * Add (update) or delete MACIP from zebra.
 */
static int bgp_zebra_send_remote_macip(struct bgp *bgp, struct bgpevpn *vpn,
				       struct prefix_evpn *p,
				       struct in_addr remote_vtep_ip, int add,
				       u_char flags)
{
	struct stream *s;
	int ipa_len;
	char buf1[ETHER_ADDR_STRLEN];
	char buf2[INET6_ADDRSTRLEN];
	char buf3[INET6_ADDRSTRLEN];

	/* Check socket. */
	if (!zclient || zclient->sock < 0)
		return 0;

	/* Don't try to register if Zebra doesn't know of this instance. */
	if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
		return 0;

	s = zclient->obuf;
	stream_reset(s);

	zclient_create_header(
		s, add ? ZEBRA_REMOTE_MACIP_ADD : ZEBRA_REMOTE_MACIP_DEL,
		bgp->vrf_id);
	stream_putl(s, vpn->vni);
	stream_put(s, &p->prefix.mac.octet, ETH_ALEN); /* Mac Addr */
	/* IP address length and IP address, if any. */
	if (IS_EVPN_PREFIX_IPADDR_NONE(p))
		stream_putl(s, 0);
	else {
		ipa_len = IS_EVPN_PREFIX_IPADDR_V4(p) ? IPV4_MAX_BYTELEN
						      : IPV6_MAX_BYTELEN;
		stream_putl(s, ipa_len);
		stream_put(s, &p->prefix.ip.ip.addr, ipa_len);
	}
	stream_put_in_addr(s, &remote_vtep_ip);

	/* TX flags - MAC sticky status and/or gateway mac */
	if (add)
		stream_putc(s, flags);

	stream_putw_at(s, 0, stream_get_endp(s));

	if (bgp_debug_zebra(NULL))
		zlog_debug(
			"Tx %s MACIP, VNI %u MAC %s IP %s (flags: 0x%x) remote VTEP %s",
			add ? "ADD" : "DEL", vpn->vni,
			prefix_mac2str(&p->prefix.mac, buf1, sizeof(buf1)),
			ipaddr2str(&p->prefix.ip, buf3, sizeof(buf3)), flags,
			inet_ntop(AF_INET, &remote_vtep_ip, buf2,
				  sizeof(buf2)));

	return zclient_send_message(zclient);
}

/*
 * Add (update) or delete remote VTEP from zebra.
 */
static int bgp_zebra_send_remote_vtep(struct bgp *bgp, struct bgpevpn *vpn,
				      struct prefix_evpn *p, int add)
{
	struct stream *s;

	/* Check socket. */
	if (!zclient || zclient->sock < 0)
		return 0;

	/* Don't try to register if Zebra doesn't know of this instance. */
	if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
		return 0;

	s = zclient->obuf;
	stream_reset(s);

	zclient_create_header(
		s, add ? ZEBRA_REMOTE_VTEP_ADD : ZEBRA_REMOTE_VTEP_DEL,
		bgp->vrf_id);
	stream_putl(s, vpn->vni);
	if (IS_EVPN_PREFIX_IPADDR_V4(p))
		stream_put_in_addr(s, &p->prefix.ip.ipaddr_v4);
	else if (IS_EVPN_PREFIX_IPADDR_V6(p)) {
		zlog_err(
			"Bad remote IP when trying to %s remote VTEP for VNI %u",
			add ? "ADD" : "DEL", vpn->vni);
		return -1;
	}

	stream_putw_at(s, 0, stream_get_endp(s));

	if (bgp_debug_zebra(NULL))
		zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
			   add ? "ADD" : "DEL", vpn->vni,
			   inet_ntoa(p->prefix.ip.ipaddr_v4));

	return zclient_send_message(zclient);
}

/*
 * Build extended communities for EVPN prefix route.
 */
static void build_evpn_type5_route_extcomm(struct bgp *bgp_vrf,
					   struct attr *attr)
{
	struct ecommunity ecom_encap;
	struct ecommunity ecom_rmac;
	struct ecommunity_val eval;
	struct ecommunity_val eval_rmac;
	bgp_encap_types tnl_type;
	struct listnode *node, *nnode;
	struct ecommunity *ecom;
	struct list *vrf_export_rtl = NULL;

	/* Encap */
	tnl_type = BGP_ENCAP_TYPE_VXLAN;
	memset(&ecom_encap, 0, sizeof(ecom_encap));
	encode_encap_extcomm(tnl_type, &eval);
	ecom_encap.size = 1;
	ecom_encap.val = (u_int8_t *)eval.val;

	/* Add Encap */
	attr->ecommunity = ecommunity_dup(&ecom_encap);

	/* Add the export RTs for L3VNI/VRF */
	vrf_export_rtl = bgp_vrf->vrf_export_rtl;
	if (vrf_export_rtl && !list_isempty(vrf_export_rtl)) {
		for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode, ecom))
			attr->ecommunity =
				ecommunity_merge(attr->ecommunity, ecom);
	}

	/* add the router mac extended community */
	if (!is_zero_mac(&attr->rmac)) {
		memset(&ecom_rmac, 0, sizeof(ecom_rmac));
		encode_rmac_extcomm(&eval_rmac, &attr->rmac);
		ecom_rmac.size = 1;
		ecom_rmac.val = (uint8_t *)eval_rmac.val;
		attr->ecommunity =
			ecommunity_merge(attr->ecommunity, &ecom_rmac);
	}

	attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
}

/*
 * Build extended communities for EVPN route. RT and ENCAP are
 * applicable to all routes.
 * TODO: currently kernel doesnt support ipv6 routes with ipv4 nexthops.
 * This means that we can't do symmetric routing for ipv6 hosts routes
 * in the same way as ipv4 host routes.
 * We wont attach l3-vni related RTs for ipv6 routes.
 * For now, We will only adevrtise ipv4 host routes
 * with L3-VNI related ext-comm.
 */
static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr,
				     afi_t afi)
{
	struct ecommunity ecom_encap;
	struct ecommunity ecom_sticky;
	struct ecommunity ecom_default_gw;
	struct ecommunity ecom_rmac;
	struct ecommunity_val eval;
	struct ecommunity_val eval_sticky;
	struct ecommunity_val eval_default_gw;
	struct ecommunity_val eval_rmac;
	bgp_encap_types tnl_type;
	struct listnode *node, *nnode;
	struct ecommunity *ecom;
	u_int32_t seqnum;
	struct list *vrf_export_rtl = NULL;

	/* Encap */
	tnl_type = BGP_ENCAP_TYPE_VXLAN;
	memset(&ecom_encap, 0, sizeof(ecom_encap));
	encode_encap_extcomm(tnl_type, &eval);
	ecom_encap.size = 1;
	ecom_encap.val = (u_int8_t *)eval.val;

	/* Add Encap */
	attr->ecommunity = ecommunity_dup(&ecom_encap);

	/* Add the export RTs for L2VNI */
	for (ALL_LIST_ELEMENTS(vpn->export_rtl, node, nnode, ecom))
		attr->ecommunity = ecommunity_merge(attr->ecommunity, ecom);

	/*
	 * only attach l3-vni export rts for ipv4 address family and if we are
	 * advertising both the labels in type-2 routes
	 */
	if (afi == AFI_IP && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
		vrf_export_rtl = bgpevpn_get_vrf_export_rtl(vpn);
		if (vrf_export_rtl && !list_isempty(vrf_export_rtl)) {
			for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode,
					       ecom))
				attr->ecommunity = ecommunity_merge(
					attr->ecommunity, ecom);
		}
	}

	if (attr->sticky) {
		seqnum = 0;
		memset(&ecom_sticky, 0, sizeof(ecom_sticky));
		encode_mac_mobility_extcomm(1, seqnum, &eval_sticky);
		ecom_sticky.size = 1;
		ecom_sticky.val = (u_int8_t *)eval_sticky.val;
		attr->ecommunity =
			ecommunity_merge(attr->ecommunity, &ecom_sticky);
	}

	/*
	 * only attach l3-vni rmac for ipv4 address family and if we are
	 * advertising both the labels in type-2 routes
	 */
	if (afi == AFI_IP && !is_zero_mac(&attr->rmac)
	    && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
		memset(&ecom_rmac, 0, sizeof(ecom_rmac));
		encode_rmac_extcomm(&eval_rmac, &attr->rmac);
		ecom_rmac.size = 1;
		ecom_rmac.val = (uint8_t *)eval_rmac.val;
		attr->ecommunity =
			ecommunity_merge(attr->ecommunity, &ecom_rmac);
	}

	if (attr->default_gw) {
		memset(&ecom_default_gw, 0, sizeof(ecom_default_gw));
		encode_default_gw_extcomm(&eval_default_gw);
		ecom_default_gw.size = 1;
		ecom_default_gw.val = (uint8_t *)eval_default_gw.val;
		attr->ecommunity =
			ecommunity_merge(attr->ecommunity, &ecom_default_gw);
	}

	attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
}

/*
 * Add MAC mobility extended community to attribute.
 */
static void add_mac_mobility_to_attr(u_int32_t seq_num, struct attr *attr)
{
	struct ecommunity ecom_tmp;
	struct ecommunity_val eval;
	u_int8_t *ecom_val_ptr;
	int i;
	u_int8_t *pnt;
	int type = 0;
	int sub_type = 0;

	/* Build MM */
	encode_mac_mobility_extcomm(0, seq_num, &eval);

	/* Find current MM ecommunity */
	ecom_val_ptr = NULL;

	if (attr->ecommunity) {
		for (i = 0; i < attr->ecommunity->size; i++) {
			pnt = attr->ecommunity->val + (i * 8);
			type = *pnt++;
			sub_type = *pnt++;

			if (type == ECOMMUNITY_ENCODE_EVPN
			    && sub_type
				       == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY) {
				ecom_val_ptr =
					(u_int8_t *)(attr->ecommunity->val
						     + (i * 8));
				break;
			}
		}
	}

	/* Update the existing MM ecommunity */
	if (ecom_val_ptr) {
		memcpy(ecom_val_ptr, eval.val, sizeof(char) * ECOMMUNITY_SIZE);
	}
	/* Add MM to existing */
	else {
		memset(&ecom_tmp, 0, sizeof(ecom_tmp));
		ecom_tmp.size = 1;
		ecom_tmp.val = (u_int8_t *)eval.val;

		attr->ecommunity =
			ecommunity_merge(attr->ecommunity, &ecom_tmp);
	}
}

/* Install EVPN route into zebra. */
static int evpn_zebra_install(struct bgp *bgp, struct bgpevpn *vpn,
			      struct prefix_evpn *p,
			      struct in_addr remote_vtep_ip, u_char flags)
{
	int ret;

	if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
		ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
						  1, flags);
	else
		ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 1);

	return ret;
}

/* Uninstall EVPN route from zebra. */
static int evpn_zebra_uninstall(struct bgp *bgp, struct bgpevpn *vpn,
				struct prefix_evpn *p,
				struct in_addr remote_vtep_ip)
{
	int ret;

	if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
		ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
						  0, 0);
	else
		ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 0);

	return ret;
}

/*
 * Due to MAC mobility, the prior "local" best route has been supplanted
 * by a "remote" best route. The prior route has to be deleted and withdrawn
 * from peers.
 */
static void evpn_delete_old_local_route(struct bgp *bgp, struct bgpevpn *vpn,
					struct bgp_node *rn,
					struct bgp_info *old_local)
{
	struct bgp_node *global_rn;
	struct bgp_info *ri;
	afi_t afi = AFI_L2VPN;
	safi_t safi = SAFI_EVPN;

	/* Locate route node in the global EVPN routing table. Note that
	 * this table is a 2-level tree (RD-level + Prefix-level) similar to
	 * L3VPN routes.
	 */
	global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
					(struct prefix *)&rn->p, &vpn->prd);
	if (global_rn) {
		/* Delete route entry in the global EVPN table. */
		delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);

		/* Schedule for processing - withdraws to peers happen from
		 * this table.
		 */
		if (ri)
			bgp_process(bgp, global_rn, afi, safi);
		bgp_unlock_node(global_rn);
	}

	/* Delete route entry in the VNI route table, caller to remove. */
	bgp_info_delete(rn, old_local);
}

/*
 * Calculate the best path for an EVPN route. Install/update best path in zebra,
 * if appropriate.
 */
static int evpn_route_select_install(struct bgp *bgp, struct bgpevpn *vpn,
				     struct bgp_node *rn)
{
	struct bgp_info *old_select, *new_select;
	struct bgp_info_pair old_and_new;
	afi_t afi = AFI_L2VPN;
	safi_t safi = SAFI_EVPN;
	int ret = 0;
	u_char flags = 0;

	/* Compute the best path. */
	bgp_best_selection(bgp, rn, &bgp->maxpaths[afi][safi], &old_and_new,
			   afi, safi);
	old_select = old_and_new.old;
	new_select = old_and_new.new;

	/* If the best path hasn't changed - see if there is still something to
	 * update
	 * to zebra RIB.
	 */
	if (old_select && old_select == new_select
	    && old_select->type == ZEBRA_ROUTE_BGP
	    && old_select->sub_type == BGP_ROUTE_NORMAL
	    && !CHECK_FLAG(rn->flags, BGP_NODE_USER_CLEAR)
	    && !CHECK_FLAG(old_select->flags, BGP_INFO_ATTR_CHANGED)
	    && !bgp->addpath_tx_used[afi][safi]) {
		if (bgp_zebra_has_route_changed(rn, old_select)) {
			if (old_select->attr->sticky)
				SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
			if (old_select->attr->default_gw)
				SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
			ret = evpn_zebra_install(
				bgp, vpn, (struct prefix_evpn *)&rn->p,
				old_select->attr->nexthop, flags);
		}
		UNSET_FLAG(old_select->flags, BGP_INFO_MULTIPATH_CHG);
		bgp_zebra_clear_route_change_flags(rn);
		return ret;
	}

	/* If the user did a "clear" this flag will be set */
	UNSET_FLAG(rn->flags, BGP_NODE_USER_CLEAR);

	/* bestpath has changed; update relevant fields and install or uninstall
	 * into the zebra RIB.
	 */
	if (old_select || new_select)
		bgp_bump_version(rn);

	if (old_select)
		bgp_info_unset_flag(rn, old_select, BGP_INFO_SELECTED);
	if (new_select) {
		bgp_info_set_flag(rn, new_select, BGP_INFO_SELECTED);
		bgp_info_unset_flag(rn, new_select, BGP_INFO_ATTR_CHANGED);
		UNSET_FLAG(new_select->flags, BGP_INFO_MULTIPATH_CHG);
	}

	if (new_select && new_select->type == ZEBRA_ROUTE_BGP
	    && new_select->sub_type == BGP_ROUTE_NORMAL) {
		flags = 0;
		if (new_select->attr->sticky)
			SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
		if (new_select->attr->default_gw)
			SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
		ret = evpn_zebra_install(bgp, vpn, (struct prefix_evpn *)&rn->p,
					 new_select->attr->nexthop, flags);
		/* If an old best existed and it was a "local" route, the only
		 * reason
		 * it would be supplanted is due to MAC mobility procedures. So,
		 * we
		 * need to do an implicit delete and withdraw that route from
		 * peers.
		 */
		if (old_select && old_select->peer == bgp->peer_self
		    && old_select->type == ZEBRA_ROUTE_BGP
		    && old_select->sub_type == BGP_ROUTE_STATIC)
			evpn_delete_old_local_route(bgp, vpn, rn, old_select);
	} else {
		if (old_select && old_select->type == ZEBRA_ROUTE_BGP
		    && old_select->sub_type == BGP_ROUTE_NORMAL)
			ret = evpn_zebra_uninstall(bgp, vpn,
						   (struct prefix_evpn *)&rn->p,
						   old_select->attr->nexthop);
	}

	/* Clear any route change flags. */
	bgp_zebra_clear_route_change_flags(rn);

	/* Reap old select bgp_info, if it has been removed */
	if (old_select && CHECK_FLAG(old_select->flags, BGP_INFO_REMOVED))
		bgp_info_reap(rn, old_select);

	return ret;
}

/*
 * Return true if the local ri for this rn is of type gateway mac
 */
static int evpn_route_is_def_gw(struct bgp *bgp, struct bgp_node *rn)
{
	struct bgp_info *tmp_ri = NULL;
	struct bgp_info *local_ri = NULL;

	local_ri = NULL;
	for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
		if (tmp_ri->peer == bgp->peer_self
		    && tmp_ri->type == ZEBRA_ROUTE_BGP
		    && tmp_ri->sub_type == BGP_ROUTE_STATIC)
			local_ri = tmp_ri;
	}

	if (!local_ri)
		return 0;

	return local_ri->attr->default_gw;
}


/*
 * Return true if the local ri for this rn has sticky set
 */
static int evpn_route_is_sticky(struct bgp *bgp, struct bgp_node *rn)
{
	struct bgp_info *tmp_ri;
	struct bgp_info *local_ri;

	local_ri = NULL;
	for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
		if (tmp_ri->peer == bgp->peer_self
		    && tmp_ri->type == ZEBRA_ROUTE_BGP
		    && tmp_ri->sub_type == BGP_ROUTE_STATIC)
			local_ri = tmp_ri;
	}

	if (!local_ri)
		return 0;

	return local_ri->attr->sticky;
}

static int update_evpn_type5_route_entry(struct bgp *bgp_def,
					 struct bgp *bgp_vrf, afi_t afi,
					 safi_t safi, struct bgp_node *rn,
					 struct attr *attr, int *route_changed)
{
	struct attr *attr_new = NULL;
	struct bgp_info *ri = NULL;
	mpls_label_t label = MPLS_INVALID_LABEL;
	struct bgp_info *local_ri = NULL;
	struct bgp_info *tmp_ri = NULL;

	*route_changed = 0;
	/* locate the local route entry if any */
	for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
		if (tmp_ri->peer == bgp_def->peer_self
		    && tmp_ri->type == ZEBRA_ROUTE_BGP
		    && tmp_ri->sub_type == BGP_ROUTE_STATIC)
			local_ri = tmp_ri;
	}

	/* create a new route entry if one doesnt exist.
	   Otherwise see if route attr has changed
	 */
	if (!local_ri) {

		/* route has changed as this is the first entry */
		*route_changed = 1;

		/* Add (or update) attribute to hash. */
		attr_new = bgp_attr_intern(attr);

		/* create the route info from attribute */
		ri = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
			       bgp_def->peer_self, attr_new, rn);
		SET_FLAG(ri->flags, BGP_INFO_VALID);

		/* Type-5 routes advertise the L3-VNI */
		bgp_info_extra_get(ri);
		vni2label(bgp_vrf->l3vni, &label);
		memcpy(&ri->extra->label, &label, sizeof(label));
		ri->extra->num_labels = 1;

		/* add the route entry to route node*/
		bgp_info_add(rn, ri);
	} else {

		tmp_ri = local_ri;
		if (!attrhash_cmp(tmp_ri->attr, attr)) {

			/* attribute changed */
			*route_changed = 1;

			/* The attribute has changed. */
			/* Add (or update) attribute to hash. */
			attr_new = bgp_attr_intern(attr);
			bgp_info_set_flag(rn, tmp_ri, BGP_INFO_ATTR_CHANGED);

			/* Restore route, if needed. */
			if (CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
				bgp_info_restore(rn, tmp_ri);

			/* Unintern existing, set to new. */
			bgp_attr_unintern(&tmp_ri->attr);
			tmp_ri->attr = attr_new;
			tmp_ri->uptime = bgp_clock();
		}
	}
	return 0;
}

/* update evpn type-5 route entry */
static int update_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp,
				   struct attr *src_attr)
{
	afi_t afi = AFI_L2VPN;
	safi_t safi = SAFI_EVPN;
	struct attr attr;
	struct bgp_node *rn = NULL;
	struct bgp *bgp_def = NULL;
	int route_changed = 0;

	bgp_def = bgp_get_default();
	if (!bgp_def)
		return 0;

	/* Build path attribute for this route - use the source attr, if
	 * present, else treat as locally originated.
	 */
	if (src_attr)
		bgp_attr_dup(&attr, src_attr);
	else {
		memset(&attr, 0, sizeof(struct attr));
		bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
	}
	/* Set nexthop to ourselves and fill in the Router MAC. */
	attr.nexthop = bgp_vrf->originator_ip;
	attr.mp_nexthop_global_in = bgp_vrf->originator_ip;
	attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
	memcpy(&attr.rmac, &bgp_vrf->rmac, sizeof(struct ethaddr));

	/* Setup RT and encap extended community */
	build_evpn_type5_route_extcomm(bgp_vrf, &attr);

	/* get the route node in global table */
	rn = bgp_afi_node_get(bgp_def->rib[afi][safi], afi, safi,
			      (struct prefix *)evp, &bgp_vrf->vrf_prd);
	assert(rn);

	/* create or update the route entry within the route node */
	update_evpn_type5_route_entry(bgp_def, bgp_vrf, afi, safi, rn, &attr,
				      &route_changed);

	/* schedule for processing and unlock node */
	if (route_changed) {
		bgp_process(bgp_def, rn, afi, safi);
		bgp_unlock_node(rn);
	}

	/* uninten temporary */
	if (!src_attr)
		aspath_unintern(&attr.aspath);
	return 0;
}

/*
 * Create or update EVPN route entry. This could be in the VNI route table
 * or the global route table.
 */
static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
				   afi_t afi, safi_t safi, struct bgp_node *rn,
				   struct attr *attr, int add, int vni_table,
				   struct bgp_info **ri, u_char flags)
{
	struct bgp_info *tmp_ri;
	struct bgp_info *local_ri, *remote_ri;
	struct attr *attr_new;
	mpls_label_t label[BGP_MAX_LABELS];
	u_int32_t num_labels = 1;
	int route_change = 1;
	u_char sticky = 0;
	struct prefix_evpn *evp;

	*ri = NULL;
	evp = (struct prefix_evpn *)&rn->p;
	memset(&label, 0, sizeof(label));

	/* See if this is an update of an existing route, or a new add. Also,
	 * identify if already known from remote, and if so, the one with the
	 * highest sequence number; this is only when adding to the VNI routing
	 * table.
	 */
	local_ri = remote_ri = NULL;
	for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
		if (tmp_ri->peer == bgp->peer_self
		    && tmp_ri->type == ZEBRA_ROUTE_BGP
		    && tmp_ri->sub_type == BGP_ROUTE_STATIC)
			local_ri = tmp_ri;
		if (vni_table) {
			if (tmp_ri->type == ZEBRA_ROUTE_BGP
			    && tmp_ri->sub_type == BGP_ROUTE_NORMAL
			    && CHECK_FLAG(tmp_ri->flags, BGP_INFO_VALID)) {
				if (!remote_ri)
					remote_ri = tmp_ri;
				else if (mac_mobility_seqnum(tmp_ri->attr)
					 > mac_mobility_seqnum(remote_ri->attr))
					remote_ri = tmp_ri;
			}
		}
	}

	/* If route doesn't exist already, create a new one, if told to.
	 * Otherwise act based on whether the attributes of the route have
	 * changed or not.
	 */
	if (!local_ri && !add)
		return 0;

	if (!local_ri) {
		/* When learnt locally for the first time but already known from
		 * remote, we have to initiate appropriate MAC mobility steps.
		 * This
		 * is applicable when updating the VNI routing table.
		 * We need to skip mobility steps for g/w macs (local mac on g/w
		 * SVI) advertised in EVPN.
		 * This will ensure that local routes are preferred for g/w macs
		 */
		if (remote_ri && !CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW)) {
			u_int32_t cur_seqnum;

			/* Add MM extended community to route. */
			cur_seqnum = mac_mobility_seqnum(remote_ri->attr);
			add_mac_mobility_to_attr(cur_seqnum + 1, attr);
		}

		/* Add (or update) attribute to hash. */
		attr_new = bgp_attr_intern(attr);

		/* Extract MAC mobility sequence number, if any. */
		attr_new->mm_seqnum =
			bgp_attr_mac_mobility_seqnum(attr_new, &sticky);
		attr_new->sticky = sticky;

		/* Create new route with its attribute. */
		tmp_ri = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
				   bgp->peer_self, attr_new, rn);
		SET_FLAG(tmp_ri->flags, BGP_INFO_VALID);
		bgp_info_extra_get(tmp_ri);

		/* The VNI goes into the 'label' field of the route */
		vni2label(vpn->vni, &label[0]);

		/* Type-2 routes may carry a second VNI - the L3-VNI.
		 * Only attach second label if we are advertising two labels for
		 * type-2 routes.
		 */
		if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
		    && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
			vni_t l3vni;

			l3vni = bgpevpn_get_l3vni(vpn);
			if (l3vni) {
				vni2label(l3vni, &label[1]);
				num_labels++;
			}
		}

		memcpy(&tmp_ri->extra->label, label, sizeof(label));
		tmp_ri->extra->num_labels = num_labels;
		bgp_info_add(rn, tmp_ri);
	} else {
		tmp_ri = local_ri;
		if (attrhash_cmp(tmp_ri->attr, attr)
		    && !CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
			route_change = 0;
		else {
			/*
			 * The attributes have changed, type-2 routes needs to
			 * be advertised with right labels.
			 */
			vni2label(vpn->vni, &label[0]);
			if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
			    && CHECK_FLAG(vpn->flags,
					  VNI_FLAG_USE_TWO_LABELS)) {
				vni_t l3vni;

				l3vni = bgpevpn_get_l3vni(vpn);
				if (l3vni) {
					vni2label(l3vni, &label[1]);
					num_labels++;
				}
			}
			memcpy(&tmp_ri->extra->label, label, sizeof(label));
			tmp_ri->extra->num_labels = num_labels;

			/* The attribute has changed. */
			/* Add (or update) attribute to hash. */
			attr_new = bgp_attr_intern(attr);
			bgp_info_set_flag(rn, tmp_ri, BGP_INFO_ATTR_CHANGED);

			/* Restore route, if needed. */
			if (CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
				bgp_info_restore(rn, tmp_ri);

			/* Unintern existing, set to new. */
			bgp_attr_unintern(&tmp_ri->attr);
			tmp_ri->attr = attr_new;
			tmp_ri->uptime = bgp_clock();
		}
	}

	/* Return back the route entry. */
	*ri = tmp_ri;
	return route_change;
}

/*
 * Create or update EVPN route (of type based on prefix) for specified VNI
 * and schedule for processing.
 */
static int update_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
			     struct prefix_evpn *p, u_char flags)
{
	struct bgp_node *rn;
	struct attr attr;
	struct attr *attr_new;
	struct bgp_info *ri;
	afi_t afi = AFI_L2VPN;
	safi_t safi = SAFI_EVPN;
	int route_change;

	memset(&attr, 0, sizeof(struct attr));

	/* Build path-attribute for this route. */
	bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
	attr.nexthop = vpn->originator_ip;
	attr.mp_nexthop_global_in = vpn->originator_ip;
	attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
	attr.sticky = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY) ? 1 : 0;
	attr.default_gw = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW) ? 1 : 0;

	/* PMSI is only needed for type-3 routes */
	if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE)
		attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL);

	/* router mac is only needed for type-2 and type-5  routes */
	if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
		bgpevpn_get_rmac(vpn, &attr.rmac);
	vni2label(vpn->vni, &(attr.label));

	/* Set up RT and ENCAP extended community. */
	build_evpn_route_extcomm(
		vpn, &attr, IS_EVPN_PREFIX_IPADDR_V4(p) ? AFI_IP : AFI_IP6);

	/* First, create (or fetch) route node within the VNI. */
	/* NOTE: There is no RD here. */
	rn = bgp_node_get(vpn->route_table, (struct prefix *)p);

	/* Create or update route entry. */
	route_change = update_evpn_route_entry(bgp, vpn, afi, safi, rn, &attr,
					       1, 1, &ri, flags);
	assert(ri);
	attr_new = ri->attr;

	/* Perform route selection; this is just to set the flags correctly
	 * as local route in the VNI always wins.
	 */
	evpn_route_select_install(bgp, vpn, rn);
	bgp_unlock_node(rn);

	/* If this is a new route or some attribute has changed, export the
	 * route to the global table. The route will be advertised to peers
	 * from there. Note that this table is a 2-level tree (RD-level +
	 * Prefix-level) similar to L3VPN routes.
	 */
	if (route_change) {
		struct bgp_info *global_ri;

		rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
				      (struct prefix *)p, &vpn->prd);
		update_evpn_route_entry(bgp, vpn, afi, safi, rn, attr_new, 1, 0,
					&global_ri, flags);

		/* Schedule for processing and unlock node. */
		bgp_process(bgp, rn, afi, safi);
		bgp_unlock_node(rn);
	}

	/* Unintern temporary. */
	aspath_unintern(&attr.aspath);

	return 0;
}

/* Delete EVPN type5 route entry from global table */
static void delete_evpn_type5_route_entry(struct bgp *bgp_def,
					  struct bgp *bgp_vrf, afi_t afi,
					  safi_t safi, struct bgp_node *rn,
					  struct bgp_info **ri)
{
	struct bgp_info *tmp_ri = NULL;

	*ri = NULL;

	/* find the matching route entry */
	for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next)
		if (tmp_ri->peer == bgp_def->peer_self
		    && tmp_ri->type == ZEBRA_ROUTE_BGP
		    && tmp_ri->sub_type == BGP_ROUTE_STATIC)
			break;

	*ri = tmp_ri;

	/* Mark route for delete. */
	if (tmp_ri)
		bgp_info_delete(rn, tmp_ri);
}

/* Delete EVPN type5 route */
static int delete_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp)
{
	afi_t afi = AFI_L2VPN;
	safi_t safi = SAFI_EVPN;
	struct bgp_node *rn = NULL;
	struct bgp_info *ri = NULL;
	struct bgp *bgp_def = NULL; /* default bgp instance */

	bgp_def = bgp_get_default();
	if (!bgp_def)
		return 0;

	/* locate the global route entry for this type-5 prefix */
	rn = bgp_afi_node_lookup(bgp_def->rib[afi][safi], afi, safi,
				 (struct prefix *)evp, &bgp_vrf->vrf_prd);
	if (!rn)
		return 0;

	delete_evpn_type5_route_entry(bgp_def, bgp_vrf, afi, safi, rn, &ri);
	if (ri)
		bgp_process(bgp_def, rn, afi, safi);
	bgp_unlock_node(rn);
	return 0;
}

/*
 * Delete EVPN route entry. This could be in the VNI route table
 * or the global route table.
 */
static void delete_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
				    afi_t afi, safi_t safi, struct bgp_node *rn,
				    struct bgp_info **ri)
{
	struct bgp_info *tmp_ri;

	*ri = NULL;

	/* Now, find matching route. */
	for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next)
		if (tmp_ri->peer == bgp->peer_self
		    && tmp_ri->type == ZEBRA_ROUTE_BGP
		    && tmp_ri->sub_type == BGP_ROUTE_STATIC)
			break;

	*ri = tmp_ri;

	/* Mark route for delete. */
	if (tmp_ri)
		bgp_info_delete(rn, tmp_ri);
}

/*
 * Delete EVPN route (of type based on prefix) for specified VNI and
 * schedule for processing.
 */
static int delete_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
			     struct prefix_evpn *p)
{
	struct bgp_node *rn, *global_rn;
	struct bgp_info *ri;
	afi_t afi = AFI_L2VPN;
	safi_t safi = SAFI_EVPN;

	/* First, locate the route node within the VNI. If it doesn't exist,
	 * there
	 * is nothing further to do.
	 */
	/* NOTE: There is no RD here. */
	rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
	if (!rn)
		return 0;

	/* Next, locate route node in the global EVPN routing table. Note that
	 * this table is a 2-level tree (RD-level + Prefix-level) similar to
	 * L3VPN routes.
	 */
	global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
					(struct prefix *)p, &vpn->prd);
	if (global_rn) {
		/* Delete route entry in the global EVPN table. */
		delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);

		/* Schedule for processing - withdraws to peers happen from
		 * this table.
		 */
		if (ri)
			bgp_process(bgp, global_rn, afi, safi);
		bgp_unlock_node(global_rn);
	}

	/* Delete route entry in the VNI route table. This can just be removed.
	 */
	delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
	if (ri)
		bgp_info_reap(rn, ri);
	bgp_unlock_node(rn);

	return 0;
}

/*
 * Update all type-2 (MACIP) local routes for this VNI - these should also
 * be scheduled for advertise to peers.
 */
static int update_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
{
	afi_t afi;
	safi_t safi;
	struct bgp_node *rn;
	struct bgp_info *ri;
	struct attr attr;
	struct attr attr_sticky;
	struct attr attr_def_gw;
	struct attr attr_ip6;
	struct attr attr_sticky_ip6;
	struct attr attr_def_gw_ip6;
	struct attr *attr_new;

	afi = AFI_L2VPN;
	safi = SAFI_EVPN;
	memset(&attr, 0, sizeof(struct attr));
	memset(&attr_sticky, 0, sizeof(struct attr));
	memset(&attr_def_gw, 0, sizeof(struct attr));
	memset(&attr_ip6, 0, sizeof(struct attr));
	memset(&attr_sticky_ip6, 0, sizeof(struct attr));
	memset(&attr_def_gw_ip6, 0, sizeof(struct attr));

	/* Build path-attribute - all type-2 routes for this VNI will share the
	 * same path attribute.
	 */
	bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
	bgp_attr_default_set(&attr_sticky, BGP_ORIGIN_IGP);
	bgp_attr_default_set(&attr_def_gw, BGP_ORIGIN_IGP);
	attr.nexthop = vpn->originator_ip;
	attr.mp_nexthop_global_in = vpn->originator_ip;
	attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
	bgpevpn_get_rmac(vpn, &attr.rmac);
	attr_sticky.nexthop = vpn->originator_ip;
	attr_sticky.mp_nexthop_global_in = vpn->originator_ip;
	attr_sticky.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
	attr_sticky.sticky = 1;
	bgpevpn_get_rmac(vpn, &attr_sticky.rmac);
	attr_def_gw.nexthop = vpn->originator_ip;
	attr_def_gw.mp_nexthop_global_in = vpn->originator_ip;
	attr_def_gw.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
	attr_def_gw.default_gw = 1;
	bgpevpn_get_rmac(vpn, &attr_def_gw.rmac);
	bgp_attr_default_set(&attr_ip6, BGP_ORIGIN_IGP);
	bgp_attr_default_set(&attr_sticky_ip6, BGP_ORIGIN_IGP);
	bgp_attr_default_set(&attr_def_gw_ip6, BGP_ORIGIN_IGP);
	attr_ip6.nexthop = vpn->originator_ip;
	attr_ip6.mp_nexthop_global_in = vpn->originator_ip;
	attr_ip6.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
	bgpevpn_get_rmac(vpn, &attr_ip6.rmac);
	attr_sticky_ip6.nexthop = vpn->originator_ip;
	attr_sticky_ip6.mp_nexthop_global_in = vpn->originator_ip;
	attr_sticky_ip6.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
	attr_sticky_ip6.sticky = 1;
	bgpevpn_get_rmac(vpn, &attr_sticky_ip6.rmac);
	attr_def_gw_ip6.nexthop = vpn->originator_ip;
	attr_def_gw_ip6.mp_nexthop_global_in = vpn->originator_ip;
	attr_def_gw_ip6.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
	attr_def_gw_ip6.default_gw = 1;
	bgpevpn_get_rmac(vpn, &attr_def_gw_ip6.rmac);

	/* Set up RT, ENCAP and sticky MAC extended community. */
	build_evpn_route_extcomm(vpn, &attr, AFI_IP);
	build_evpn_route_extcomm(vpn, &attr_sticky, AFI_IP);
	build_evpn_route_extcomm(vpn, &attr_def_gw, AFI_IP);
	build_evpn_route_extcomm(vpn, &attr_ip6, AFI_IP6);
	build_evpn_route_extcomm(vpn, &attr_sticky_ip6, AFI_IP6);
	build_evpn_route_extcomm(vpn, &attr_def_gw_ip6, AFI_IP);

	/* Walk this VNI's route table and update local type-2 routes. For any
	 * routes updated, update corresponding entry in the global table too.
	 */
	for (rn = bgp_table_top(vpn->route_table); rn;
	     rn = bgp_route_next(rn)) {
		struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
		struct bgp_node *rd_rn;
		struct bgp_info *global_ri;

		if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
			continue;

		if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
			if (evpn_route_is_sticky(bgp, rn))
				update_evpn_route_entry(bgp, vpn, afi, safi, rn,
							&attr_sticky, 0, 1, &ri,
							0);
			else if (evpn_route_is_def_gw(bgp, rn))
				update_evpn_route_entry(bgp, vpn, afi, safi, rn,
							&attr_def_gw, 0, 1, &ri,
							0);
			else
				update_evpn_route_entry(bgp, vpn, afi, safi, rn,
							&attr, 0, 1, &ri, 0);
		} else {
			if (evpn_route_is_sticky(bgp, rn))
				update_evpn_route_entry(bgp, vpn, afi, safi, rn,
							&attr_sticky_ip6, 0, 1,
							&ri, 0);
			else if (evpn_route_is_def_gw(bgp, rn))
				update_evpn_route_entry(bgp, vpn, afi, safi, rn,
							&attr_def_gw_ip6, 0, 1,
							&ri, 0);
			else
				update_evpn_route_entry(bgp, vpn, afi, safi, rn,
							&attr_ip6, 0, 1, &ri,
							0);
		}

		/* If a local route exists for this prefix, we need to update
		 * the global routing table too.
		 */
		if (!ri)
			continue;

		/* Perform route selection; this is just to set the flags
		 * correctly
		 * as local route in the VNI always wins.
		 */
		evpn_route_select_install(bgp, vpn, rn);

		attr_new = ri->attr;

		/* Update route in global routing table. */
		rd_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
					 (struct prefix *)evp, &vpn->prd);
		assert(rd_rn);
		update_evpn_route_entry(bgp, vpn, afi, safi, rd_rn, attr_new, 0,
					0, &global_ri, 0);

		/* Schedule for processing and unlock node. */
		bgp_process(bgp, rd_rn, afi, safi);
		bgp_unlock_node(rd_rn);
	}

	/* Unintern temporary. */
	aspath_unintern(&attr.aspath);
	aspath_unintern(&attr_ip6.aspath);
	aspath_unintern(&attr_sticky.aspath);
	aspath_unintern(&attr_sticky_ip6.aspath);
	aspath_unintern(&attr_def_gw.aspath);
	aspath_unintern(&attr_def_gw_ip6.aspath);

	return 0;
}

/*
 * Delete all type-2 (MACIP) local routes for this VNI - only from the
 * global routing table. These are also scheduled for withdraw from peers.
 */
static int delete_global_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
{
	afi_t afi;
	safi_t safi;
	struct bgp_node *rdrn, *rn;
	struct bgp_table *table;
	struct bgp_info *ri;

	afi = AFI_L2VPN;
	safi = SAFI_EVPN;

	rdrn = bgp_node_lookup(bgp->rib[afi][safi], (struct prefix *)&vpn->prd);
	if (rdrn && rdrn->info) {
		table = (struct bgp_table *)rdrn->info;
		for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
			struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;

			if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
				continue;

			delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
			if (ri)
				bgp_process(bgp, rn, afi, safi);
		}
	}

	/* Unlock RD node. */
	if (rdrn)
		bgp_unlock_node(rdrn);

	return 0;
}

/*
 * Delete all type-2 (MACIP) local routes for this VNI - from the global
 * table as well as the per-VNI route table.
 */
static int delete_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
{
	afi_t afi;
	safi_t safi;
	struct bgp_node *rn;
	struct bgp_info *ri;

	afi = AFI_L2VPN;
	safi = SAFI_EVPN;

	/* First, walk the global route table for this VNI's type-2 local
	 * routes.
	 * EVPN routes are a 2-level table, first get the RD table.
	 */
	delete_global_type2_routes(bgp, vpn);

	/* Next, walk this VNI's route table and delete local type-2 routes. */
	for (rn = bgp_table_top(vpn->route_table); rn;
	     rn = bgp_route_next(rn)) {
		struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;

		if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
			continue;

		delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);

		/* Route entry in local table gets deleted immediately. */
		if (ri)
			bgp_info_reap(rn, ri);
	}

	return 0;
}

/*
 * Delete all routes in the per-VNI route table.
 */
static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
{
	struct bgp_node *rn;
	struct bgp_info *ri, *nextri;

	/* Walk this VNI's route table and delete all routes. */
	for (rn = bgp_table_top(vpn->route_table); rn;
	     rn = bgp_route_next(rn)) {
		for (ri = rn->info; (ri != NULL) && (nextri = ri->next, 1);
		     ri = nextri) {
			bgp_info_delete(rn, ri);
			bgp_info_reap(rn, ri);
		}
	}

	return 0;
}

/*
 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
 * export RT getting modified or change to tunnel IP. Note that these
 * situations need the route in the per-VNI table as well as the global
 * table to be updated (as attributes change).
 */
static int update_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
{
	int ret;
	struct prefix_evpn p;

	/* Update and advertise the type-3 route (only one) followed by the
	 * locally learnt type-2 routes (MACIP) - for this VNI.
	 */
	build_evpn_type3_prefix(&p, vpn->originator_ip);
	ret = update_evpn_route(bgp, vpn, &p, 0);
	if (ret)
		return ret;

	return update_all_type2_routes(bgp, vpn);
}

/*
 * Delete (and withdraw) local routes for specified VNI from the global
 * table and per-VNI table. After this, remove all other routes from
 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
 * (advertise-all-vni) being disabled.
 */
static int delete_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
{
	int ret;
	struct prefix_evpn p;

	/* Delete and withdraw locally learnt type-2 routes (MACIP)
	 * followed by type-3 routes (only one) - for this VNI.
	 */
	ret = delete_all_type2_routes(bgp, vpn);
	if (ret)
		return ret;

	build_evpn_type3_prefix(&p, vpn->originator_ip);
	ret = delete_evpn_route(bgp, vpn, &p);
	if (ret)
		return ret;

	/* Delete all routes from the per-VNI table. */
	return delete_all_vni_routes(bgp, vpn);
}

/*
 * There is a tunnel endpoint IP address change for this VNI, delete
 * prior type-3 route (if needed) and update.
 * Note: Route re-advertisement happens elsewhere after other processing
 * other changes.
 */
static int handle_tunnel_ip_change(struct bgp *bgp, struct bgpevpn *vpn,
				   struct in_addr originator_ip)
{
	struct prefix_evpn p;

	/* If VNI is not live, we only need to update the originator ip */
	if (!is_vni_live(vpn)) {
		vpn->originator_ip = originator_ip;
		return 0;
	}

	/* Update the tunnel-ip hash */
	bgp_tip_del(bgp, &vpn->originator_ip);
	bgp_tip_add(bgp, &originator_ip);

	/* filter routes as martian nexthop db has changed */
	bgp_filter_evpn_routes_upon_martian_nh_change(bgp);

	/* Need to withdraw type-3 route as the originator IP is part
	 * of the key.
	 */
	build_evpn_type3_prefix(&p, vpn->originator_ip);
	delete_evpn_route(bgp, vpn, &p);

	/* Update the tunnel IP and re-advertise all routes for this VNI. */
	vpn->originator_ip = originator_ip;
	return 0;
}

/*
 * Install route entry into the VRF routing table and invoke route selection.
 */
static int install_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
					   struct prefix_evpn *evp,
					   struct bgp_info *parent_ri)
{
	struct bgp_node *rn;
	struct bgp_info *ri;
	struct attr *attr_new;
	int ret = 0;
	struct prefix p;
	struct prefix *pp = &p;
	afi_t afi = 0;
	safi_t safi = 0;
	char buf[PREFIX_STRLEN];
	char buf1[PREFIX_STRLEN];

	memset(pp, 0, sizeof(struct prefix));
	if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
		ip_prefix_from_type2_prefix(evp, pp);
	else if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
		ip_prefix_from_type5_prefix(evp, pp);

	if (bgp_debug_zebra(NULL)) {
		zlog_debug(
			"installing evpn prefix %s as ip prefix %s in vrf %s",
			prefix2str(evp, buf, sizeof(buf)),
			prefix2str(pp, buf1, sizeof(buf)),
			vrf_id_to_name(bgp_vrf->vrf_id));
	}

	/* Create (or fetch) route within the VRF. */
	/* NOTE: There is no RD here. */
	if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
		afi = AFI_IP;
		safi = SAFI_UNICAST;
		rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
	} else if (IS_EVPN_PREFIX_IPADDR_V6(evp)) {
		afi = AFI_IP6;
		safi = SAFI_UNICAST;
		rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
	} else
		return 0;

	/* Check if route entry is already present. */
	for (ri = rn->info; ri; ri = ri->next)
		if (ri->extra
		    && (struct bgp_info *)ri->extra->parent == parent_ri)
			break;

	if (!ri) {
		/* Add (or update) attribute to hash. */
		attr_new = bgp_attr_intern(parent_ri->attr);

		/* Create new route with its attribute. */
		ri = info_make(parent_ri->type, parent_ri->sub_type, 0,
			       parent_ri->peer, attr_new, rn);
		SET_FLAG(ri->flags, BGP_INFO_VALID);
		bgp_info_extra_get(ri);
		ri->extra->parent = parent_ri;
		if (parent_ri->extra) {
			memcpy(&ri->extra->label, &parent_ri->extra->label,
			       sizeof(ri->extra->label));
			ri->extra->num_labels = parent_ri->extra->num_labels;
		}
		bgp_info_add(rn, ri);
	} else {
		if (attrhash_cmp(ri->attr, parent_ri->attr)
		    && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) {
			bgp_unlock_node(rn);
			return 0;
		}
		/* The attribute has changed. */
		/* Add (or update) attribute to hash. */
		attr_new = bgp_attr_intern(parent_ri->attr);

		/* Restore route, if needed. */
		if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
			bgp_info_restore(rn, ri);

		/* Mark if nexthop has changed. */
		if (!IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop))
			SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED);

		/* Unintern existing, set to new. */
		bgp_attr_unintern(&ri->attr);
		ri->attr = attr_new;
		ri->uptime = bgp_clock();
	}

	/* Perform route selection and update zebra, if required. */
	bgp_process(bgp_vrf, rn, afi, safi);

	return ret;
}

/*
 * Install route entry into the VNI routing table and invoke route selection.
 */
static int install_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
				    struct prefix_evpn *p,
				    struct bgp_info *parent_ri)
{
	struct bgp_node *rn;
	struct bgp_info *ri;
	struct attr *attr_new;
	int ret;

	/* Create (or fetch) route within the VNI. */
	/* NOTE: There is no RD here. */
	rn = bgp_node_get(vpn->route_table, (struct prefix *)p);

	/* Check if route entry is already present. */
	for (ri = rn->info; ri; ri = ri->next)
		if (ri->extra
		    && (struct bgp_info *)ri->extra->parent == parent_ri)
			break;

	if (!ri) {
		/* Add (or update) attribute to hash. */
		attr_new = bgp_attr_intern(parent_ri->attr);

		/* Create new route with its attribute. */
		ri = info_make(parent_ri->type, parent_ri->sub_type, 0,
			       parent_ri->peer, attr_new, rn);
		SET_FLAG(ri->flags, BGP_INFO_VALID);
		bgp_info_extra_get(ri);
		ri->extra->parent = parent_ri;
		if (parent_ri->extra) {
			memcpy(&ri->extra->label, &parent_ri->extra->label,
			       sizeof(ri->extra->label));
			ri->extra->num_labels = parent_ri->extra->num_labels;
		}
		bgp_info_add(rn, ri);
	} else {
		if (attrhash_cmp(ri->attr, parent_ri->attr)
		    && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) {
			bgp_unlock_node(rn);
			return 0;
		}
		/* The attribute has changed. */
		/* Add (or update) attribute to hash. */
		attr_new = bgp_attr_intern(parent_ri->attr);

		/* Restore route, if needed. */
		if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
			bgp_info_restore(rn, ri);

		/* Mark if nexthop has changed. */
		if (!IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop))
			SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED);

		/* Unintern existing, set to new. */
		bgp_attr_unintern(&ri->attr);
		ri->attr = attr_new;
		ri->uptime = bgp_clock();
	}

	/* Perform route selection and update zebra, if required. */
	ret = evpn_route_select_install(bgp, vpn, rn);

	return ret;
}

/*
 * Uninstall route entry from the VRF routing table and send message
 * to zebra, if appropriate.
 */
static int uninstall_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
					     struct prefix_evpn *evp,
					     struct bgp_info *parent_ri)
{
	struct bgp_node *rn;
	struct bgp_info *ri;
	int ret = 0;
	struct prefix p;
	struct prefix *pp = &p;
	afi_t afi = 0;
	safi_t safi = 0;
	char buf[PREFIX_STRLEN];
	char buf1[PREFIX_STRLEN];

	memset(pp, 0, sizeof(struct prefix));
	if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
		ip_prefix_from_type2_prefix(evp, pp);
	else if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
		ip_prefix_from_type5_prefix(evp, pp);

	if (bgp_debug_zebra(NULL)) {
		zlog_debug(
			"uninstalling evpn prefix %s as ip prefix %s in vrf %s",
			prefix2str(evp, buf, sizeof(buf)),
			prefix2str(pp, buf1, sizeof(buf)),
			vrf_id_to_name(bgp_vrf->vrf_id));
	}

	/* Locate route within the VRF. */
	/* NOTE: There is no RD here. */
	if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
		afi = AFI_IP;
		safi = SAFI_UNICAST;
		rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
	} else {
		afi = AFI_IP6;
		safi = SAFI_UNICAST;
		rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
	}

	if (!rn)
		return 0;

	/* Find matching route entry. */
	for (ri = rn->info; ri; ri = ri->next)
		if (ri->extra
		    && (struct bgp_info *)ri->extra->parent == parent_ri)
			break;

	if (!ri)
		return 0;

	/* Mark entry for deletion */
	bgp_info_delete(rn, ri);

	/* Perform route selection and update zebra, if required. */
	bgp_process(bgp_vrf, rn, afi, safi);

	/* Unlock route node. */
	bgp_unlock_node(rn);

	return ret;
}

/*
 * Uninstall route entry from the VNI routing table and send message
 * to zebra, if appropriate.
 */
static int uninstall_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
				      struct prefix_evpn *p,
				      struct bgp_info *parent_ri)
{
	struct bgp_node *rn;
	struct bgp_info *ri;
	int ret;

	/* Locate route within the VNI. */
	/* NOTE: There is no RD here. */
	rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
	if (!rn)
		return 0;

	/* Find matching route entry. */
	for (ri = rn->info; ri; ri = ri->next)
		if (ri->extra
		    && (struct bgp_info *)ri->extra->parent == parent_ri)
			break;

	if (!ri)
		return 0;

	/* Mark entry for deletion */
	bgp_info_delete(rn, ri);

	/* Perform route selection and update zebra, if required. */
	ret = evpn_route_select_install(bgp, vpn, rn);

	/* Unlock route node. */
	bgp_unlock_node(rn);

	return ret;
}

/*
 * Given a route entry and a VRF, see if this route entry should be
 * imported into the VRF i.e., RTs match.
 */
static int is_route_matching_for_vrf(struct bgp *bgp_vrf, struct bgp_info *ri)
{
	struct attr *attr = ri->attr;
	struct ecommunity *ecom;
	int i;

	assert(attr);
	/* Route should have valid RT to be even considered. */
	if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
		return 0;

	ecom = attr->ecommunity;
	if (!ecom || !ecom->size)
		return 0;

	/* For each extended community RT, see if it matches this VNI. If any RT
	 * matches, we're done.
	 */
	for (i = 0; i < ecom->size; i++) {
		u_char *pnt;
		u_char type, sub_type;
		struct ecommunity_val *eval;
		struct ecommunity_val eval_tmp;
		struct vrf_irt_node *irt;

		/* Only deal with RTs */
		pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
		eval = (struct ecommunity_val *)(ecom->val
						 + (i * ECOMMUNITY_SIZE));
		type = *pnt++;
		sub_type = *pnt++;
		if (sub_type != ECOMMUNITY_ROUTE_TARGET)
			continue;

		/* See if this RT matches specified VNIs import RTs */
		irt = lookup_vrf_import_rt(eval);
		if (irt && irt->vrfs)
			if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
				return 1;

		/* Also check for non-exact match. In this, we mask out the AS
		 * and
		 * only check on the local-admin sub-field. This is to
		 * facilitate using
		 * VNI as the RT for EBGP peering too.
		 */
		irt = NULL;
		if (type == ECOMMUNITY_ENCODE_AS
		    || type == ECOMMUNITY_ENCODE_AS4
		    || type == ECOMMUNITY_ENCODE_IP) {
			memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
			mask_ecom_global_admin(&eval_tmp, eval);
			irt = lookup_vrf_import_rt(&eval_tmp);
		}
		if (irt && irt->vrfs)
			if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
				return 1;
	}

	return 0;
}

/*
 * Given a route entry and a VNI, see if this route entry should be
 * imported into the VNI i.e., RTs match.
 */
static int is_route_matching_for_vni(struct bgp *bgp, struct bgpevpn *vpn,
				     struct bgp_info *ri)
{
	struct attr *attr = ri->attr;
	struct ecommunity *ecom;
	int i;

	assert(attr);
	/* Route should have valid RT to be even considered. */
	if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
		return 0;

	ecom = attr->ecommunity;
	if (!ecom || !ecom->size)
		return 0;

	/* For each extended community RT, see if it matches this VNI. If any RT
	 * matches, we're done.
	 */
	for (i = 0; i < ecom->size; i++) {
		u_char *pnt;
		u_char type, sub_type;
		struct ecommunity_val *eval;
		struct ecommunity_val eval_tmp;
		struct irt_node *irt;

		/* Only deal with RTs */
		pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
		eval = (struct ecommunity_val *)(ecom->val
						 + (i * ECOMMUNITY_SIZE));
		type = *pnt++;
		sub_type = *pnt++;
		if (sub_type != ECOMMUNITY_ROUTE_TARGET)
			continue;

		/* See if this RT matches specified VNIs import RTs */
		irt = lookup_import_rt(bgp, eval);
		if (irt && irt->vnis)
			if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
				return 1;

		/* Also check for non-exact match. In this, we mask out the AS
		 * and
		 * only check on the local-admin sub-field. This is to
		 * facilitate using
		 * VNI as the RT for EBGP peering too.
		 */
		irt = NULL;
		if (type == ECOMMUNITY_ENCODE_AS
		    || type == ECOMMUNITY_ENCODE_AS4
		    || type == ECOMMUNITY_ENCODE_IP) {
			memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
			mask_ecom_global_admin(&eval_tmp, eval);
			irt = lookup_import_rt(bgp, &eval_tmp);
		}
		if (irt && irt->vnis)
			if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
				return 1;
	}

	return 0;
}

/*
 * Install or uninstall mac-ip routes are appropriate for this
 * particular VRF.
 */
static int install_uninstall_routes_for_vrf(struct bgp *bgp_vrf, int install)
{
	afi_t afi;
	safi_t safi;
	struct bgp_node *rd_rn, *rn;
	struct bgp_table *table;
	struct bgp_info *ri;
	int ret;
	char buf[PREFIX_STRLEN];
	struct bgp *bgp_def = NULL;

	afi = AFI_L2VPN;
	safi = SAFI_EVPN;
	bgp_def = bgp_get_default();
	if (!bgp_def)
		return -1;

	/* Walk entire global routing table and evaluate routes which could be
	 * imported into this VRF. Note that we need to loop through all global
	 * routes to determine which route matches the import rt on vrf
	 */
	for (rd_rn = bgp_table_top(bgp_def->rib[afi][safi]); rd_rn;
	     rd_rn = bgp_route_next(rd_rn)) {
		table = (struct bgp_table *)(rd_rn->info);
		if (!table)
			continue;

		for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
			struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;

			/* if not mac-ip route skip this route */
			if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
			      || evp->prefix.route_type
					 == BGP_EVPN_IP_PREFIX_ROUTE))
				continue;

			/* if not a mac+ip route skip this route */
			if (!(IS_EVPN_PREFIX_IPADDR_V4(evp)
			      || IS_EVPN_PREFIX_IPADDR_V6(evp)))
				continue;

			for (ri = rn->info; ri; ri = ri->next) {
				/* Consider "valid" remote routes applicable for
				 * this VRF.
				 */
				if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID)
				      && ri->type == ZEBRA_ROUTE_BGP
				      && ri->sub_type == BGP_ROUTE_NORMAL))
					continue;

				if (is_route_matching_for_vrf(bgp_vrf, ri)) {
					if (install)
						ret = install_evpn_route_entry_in_vrf(
							bgp_vrf, evp, ri);
					else
						ret = uninstall_evpn_route_entry_in_vrf(
							bgp_vrf, evp, ri);

					if (ret) {
						zlog_err(
							"Failed to %s EVPN %s route in VRF %s",
							install ? "install"
								: "uninstall",
							prefix2str(evp, buf,
								   sizeof(buf)),
							vrf_id_to_name(
								bgp_vrf->vrf_id));
						return ret;
					}
				}
			}
		}
	}

	return 0;
}

/*
 * Install or uninstall routes of specified type that are appropriate for this
 * particular VNI.
 */
static int install_uninstall_routes_for_vni(struct bgp *bgp,
					    struct bgpevpn *vpn,
					    bgp_evpn_route_type rtype,
					    int install)
{
	afi_t afi;
	safi_t safi;
	struct bgp_node *rd_rn, *rn;
	struct bgp_table *table;
	struct bgp_info *ri;
	int ret;

	afi = AFI_L2VPN;
	safi = SAFI_EVPN;

	/* Walk entire global routing table and evaluate routes which could be
	 * imported into this VPN. Note that we cannot just look at the routes
	 * for
	 * the VNI's RD - remote routes applicable for this VNI could have any
	 * RD.
	 */
	/* EVPN routes are a 2-level table. */
	for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
	     rd_rn = bgp_route_next(rd_rn)) {
		table = (struct bgp_table *)(rd_rn->info);
		if (!table)
			continue;

		for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
			struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;

			if (evp->prefix.route_type != rtype)
				continue;

			for (ri = rn->info; ri; ri = ri->next) {
				/* Consider "valid" remote routes applicable for
				 * this VNI. */
				if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID)
				      && ri->type == ZEBRA_ROUTE_BGP
				      && ri->sub_type == BGP_ROUTE_NORMAL))
					continue;

				if (is_route_matching_for_vni(bgp, vpn, ri)) {
					if (install)
						ret = install_evpn_route_entry(
							bgp, vpn, evp, ri);
					else
						ret = uninstall_evpn_route_entry(
							bgp, vpn, evp, ri);

					if (ret) {
						zlog_err(
							"%u: Failed to %s EVPN %s route in VNI %u",
							bgp->vrf_id,
							install ? "install"
								: "uninstall",
							rtype == BGP_EVPN_MAC_IP_ROUTE
								? "MACIP"
								: "IMET",
							vpn->vni);
						return ret;
					}
				}
			}
		}
	}

	return 0;
}

/* Install any existing remote routes applicable for this VRF into VRF RIB. This
 * is invoked upon l3vni-add or l3vni import rt change
 */
static int install_routes_for_vrf(struct bgp *bgp_vrf)
{
	install_uninstall_routes_for_vrf(bgp_vrf, 1);
	return 0;
}

/*
 * Install any existing remote routes applicable for this VNI into its
 * routing table. This is invoked when a VNI becomes "live" or its Import
 * RT is changed.
 */
static int install_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
{
	int ret;

	/* Install type-3 routes followed by type-2 routes - the ones applicable
	 * for this VNI.
	 */
	ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
					       1);
	if (ret)
		return ret;

	return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
						1);
}

/* uninstall routes from l3vni vrf. */
static int uninstall_routes_for_vrf(struct bgp *bgp_vrf)
{
	install_uninstall_routes_for_vrf(bgp_vrf, 0);
	return 0;
}

/*
 * Uninstall any existing remote routes for this VNI. One scenario in which
 * this is invoked is upon an import RT change.
 */
static int uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
{
	int ret;

	/* Uninstall type-2 routes followed by type-3 routes - the ones
	 * applicable
	 * for this VNI.
	 */
	ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
					       0);
	if (ret)
		return ret;

	return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
						0);
}

/*
 * Install or uninstall route in matching VRFs (list).
 */
static int install_uninstall_route_in_vrfs(struct bgp *bgp_def, afi_t afi,
					   safi_t safi, struct prefix_evpn *evp,
					   struct bgp_info *ri,
					   struct list *vrfs, int install)
{
	char buf[PREFIX2STR_BUFFER];
	struct bgp *bgp_vrf;
	struct listnode *node, *nnode;

	/* Only type-2/type-5 routes go into a VRF */
	if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
	      || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
		return 0;

	/* if it is type-2 route and not a mac+ip route skip this route */
	if ((evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
	    && !(IS_EVPN_PREFIX_IPADDR_V4(evp)
		 || IS_EVPN_PREFIX_IPADDR_V6(evp)))
		return 0;

	for (ALL_LIST_ELEMENTS(vrfs, node, nnode, bgp_vrf)) {
		int ret;

		if (install)
			ret = install_evpn_route_entry_in_vrf(bgp_vrf, evp, ri);
		else
			ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf, evp,
								ri);

		if (ret) {
			zlog_err("%u: Failed to %s prefix %s in VRF %s",
				 bgp_def->vrf_id,
				 install ? "install" : "uninstall",
				 prefix2str(evp, buf, sizeof(buf)),
				 vrf_id_to_name(bgp_vrf->vrf_id));
			return ret;
		}
	}

	return 0;
}

/*
 * Install or uninstall route in matching VNIs (list).
 */
static int install_uninstall_route_in_vnis(struct bgp *bgp, afi_t afi,
					   safi_t safi, struct prefix_evpn *evp,
					   struct bgp_info *ri,
					   struct list *vnis, int install)
{
	struct bgpevpn *vpn;
	struct listnode *node, *nnode;

	for (ALL_LIST_ELEMENTS(vnis, node, nnode, vpn)) {
		int ret;

		if (!is_vni_live(vpn))
			continue;

		if (install)
			ret = install_evpn_route_entry(bgp, vpn, evp, ri);
		else
			ret = uninstall_evpn_route_entry(bgp, vpn, evp, ri);

		if (ret) {
			zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
				 bgp->vrf_id, install ? "install" : "uninstall",
				 evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
					 ? "MACIP"
					 : "IMET",
				 vpn->vni);
			return ret;
		}
	}

	return 0;
}

/*
 * Install or uninstall route for appropriate VNIs.
 */
static int install_uninstall_evpn_route(struct bgp *bgp, afi_t afi, safi_t safi,
					struct prefix *p, struct bgp_info *ri,
					int import)
{
	struct prefix_evpn *evp = (struct prefix_evpn *)p;
	struct attr *attr = ri->attr;
	struct ecommunity *ecom;
	int i;

	assert(attr);

	/* Only type-2 and type-3 and type-5 are supported currently */
	if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
	      || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE
	      || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
		return 0;

	/* If we don't have Route Target, nothing much to do. */
	if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
		return 0;

	ecom = attr->ecommunity;
	if (!ecom || !ecom->size)
		return -1;

	/* For each extended community RT, see which VNIs/VRFs match and import
	 * the route into matching VNIs/VRFs.
	 */
	for (i = 0; i < ecom->size; i++) {
		u_char *pnt;
		u_char type, sub_type;
		struct ecommunity_val *eval;
		struct ecommunity_val eval_tmp;
		struct irt_node *irt;	 /* import rt for l2vni */
		struct vrf_irt_node *vrf_irt; /* import rt for l3vni */

		/* Only deal with RTs */
		pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
		eval = (struct ecommunity_val *)(ecom->val
						 + (i * ECOMMUNITY_SIZE));
		type = *pnt++;
		sub_type = *pnt++;
		if (sub_type != ECOMMUNITY_ROUTE_TARGET)
			continue;

		/* Import route into matching l2-vnis (type-2/type-3 routes go
		 * into l2vni table)
		 */
		irt = lookup_import_rt(bgp, eval);
		if (irt && irt->vnis)
			install_uninstall_route_in_vnis(bgp, afi, safi, evp, ri,
							irt->vnis, import);

		/* Import route into matching l3-vnis (type-2/type-5 routes go
		 * into l3vni/vrf table)
		 */
		vrf_irt = lookup_vrf_import_rt(eval);
		if (vrf_irt && vrf_irt->vrfs)
			install_uninstall_route_in_vrfs(bgp, afi, safi, evp, ri,
							vrf_irt->vrfs, import);

		/* Also check for non-exact match. In this,
		 *  we mask out the AS and
		 * only check on the local-admin sub-field.
		 * This is to facilitate using
		 * VNI as the RT for EBGP peering too.
		 */
		irt = NULL;
		vrf_irt = NULL;
		if (type == ECOMMUNITY_ENCODE_AS
		    || type == ECOMMUNITY_ENCODE_AS4
		    || type == ECOMMUNITY_ENCODE_IP) {
			memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
			mask_ecom_global_admin(&eval_tmp, eval);
			irt = lookup_import_rt(bgp, &eval_tmp);
			vrf_irt = lookup_vrf_import_rt(&eval_tmp);
		}
		if (irt && irt->vnis)
			install_uninstall_route_in_vnis(bgp, afi, safi, evp, ri,
							irt->vnis, import);
		if (vrf_irt && vrf_irt->vrfs)
			install_uninstall_route_in_vrfs(bgp, afi, safi, evp, ri,
							vrf_irt->vrfs, import);
	}

	return 0;
}

/* delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
 * routes */
static void delete_withdraw_vrf_routes(struct bgp *bgp_vrf)
{
	/* delete all ipv4 routes and withdraw from peers */
	bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);

	/* delete all ipv6 routes and withdraw from peers */
	bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
}

/* update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
 * routes */
static void update_advertise_vrf_routes(struct bgp *bgp_vrf)
{
	/* update all ipv4 routes */
	bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);

	/* update all ipv6 routes */
	bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
}

/*
 * update and advertise local routes for a VRF as type-5 routes.
 * This is invoked upon RD change for a VRF. Note taht the processing is only
 * done in the global route table using the routes which already exist in the
 * VRF routing table
 */
static void update_router_id_vrf(struct bgp *bgp_vrf)
{
	/* skip if the RD is configured */
	if (is_vrf_rd_configured(bgp_vrf))
		return;

	/* derive the RD for the VRF based on new router-id */
	bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);

	/* update advertise ipv4|ipv6 routes as type-5 routes */
	update_advertise_vrf_routes(bgp_vrf);
}

/*
 * Delete and withdraw all type-5 routes  for the RD corresponding to VRF.
 * This is invoked upon VRF RD change. The processing is done only from global
 * table.
 */
static void withdraw_router_id_vrf(struct bgp *bgp_vrf)
{
	/* skip if the RD is configured */
	if (is_vrf_rd_configured(bgp_vrf))
		return;

	/* delete/withdraw ipv4|ipv6 routes as type-5 routes */
	delete_withdraw_vrf_routes(bgp_vrf);
}

/*
 * Update and advertise local routes for a VNI. Invoked upon router-id
 * change. Note that the processing is done only on the global route table
 * using routes that already exist in the per-VNI table.
 */
static int update_advertise_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
{
	struct prefix_evpn p;
	struct bgp_node *rn, *global_rn;
	struct bgp_info *ri, *global_ri;
	struct attr *attr;
	afi_t afi = AFI_L2VPN;
	safi_t safi = SAFI_EVPN;

	/* Locate type-3 route for VNI in the per-VNI table and use its
	 * attributes to create and advertise the type-3 route for this VNI
	 * in the global table.
	 */
	build_evpn_type3_prefix(&p, vpn->originator_ip);
	rn = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
	if (!rn) /* unexpected */
		return 0;
	for (ri = rn->info; ri; ri = ri->next)
		if (ri->peer == bgp->peer_self && ri->type == ZEBRA_ROUTE_BGP
		    && ri->sub_type == BGP_ROUTE_STATIC)
			break;
	if (!ri) /* unexpected */
		return 0;
	attr = ri->attr;

	global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
				     (struct prefix *)&p, &vpn->prd);
	update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1, 0, &ri,
				0);

	/* Schedule for processing and unlock node. */
	bgp_process(bgp, global_rn, afi, safi);
	bgp_unlock_node(global_rn);

	/* Now, walk this VNI's route table and use the route and its attribute
	 * to create and schedule route in global table.
	 */
	for (rn = bgp_table_top(vpn->route_table); rn;
	     rn = bgp_route_next(rn)) {
		struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;

		/* Identify MAC-IP local routes. */
		if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
			continue;

		for (ri = rn->info; ri; ri = ri->next)
			if (ri->peer == bgp->peer_self
			    && ri->type == ZEBRA_ROUTE_BGP
			    && ri->sub_type == BGP_ROUTE_STATIC)
				break;
		if (!ri)
			continue;

		/* Create route in global routing table using this route entry's
		 * attribute.
		 */
		attr = ri->attr;
		global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
					     (struct prefix *)evp, &vpn->prd);
		assert(global_rn);
		update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1,
					0, &global_ri, 0);

		/* Schedule for processing and unlock node. */
		bgp_process(bgp, global_rn, afi, safi);
		bgp_unlock_node(global_rn);
	}

	return 0;
}

/*
 * Delete (and withdraw) local routes for a VNI - only from the global
 * table. Invoked upon router-id change.
 */
static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
{
	int ret;
	struct prefix_evpn p;
	struct bgp_node *global_rn;
	struct bgp_info *ri;
	afi_t afi = AFI_L2VPN;
	safi_t safi = SAFI_EVPN;

	/* Delete and withdraw locally learnt type-2 routes (MACIP)
	 * for this VNI - from the global table.
	 */
	ret = delete_global_type2_routes(bgp, vpn);
	if (ret)
		return ret;

	/* Remove type-3 route for this VNI from global table. */
	build_evpn_type3_prefix(&p, vpn->originator_ip);
	global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
					(struct prefix *)&p, &vpn->prd);
	if (global_rn) {
		/* Delete route entry in the global EVPN table. */
		delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);

		/* Schedule for processing - withdraws to peers happen from
		 * this table.
		 */
		if (ri)
			bgp_process(bgp, global_rn, afi, safi);
		bgp_unlock_node(global_rn);
	}

	return 0;
}

/*
 * Handle router-id change. Update and advertise local routes corresponding
 * to this VNI from peers. Note that this is invoked after updating the
 * router-id. The routes in the per-VNI table are used to create routes in
 * the global table and schedule them.
 */
static void update_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
{
	struct bgpevpn *vpn;

	vpn = (struct bgpevpn *)backet->data;

	if (!vpn) {
		zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__);
		return;
	}

	/* Skip VNIs with configured RD. */
	if (is_rd_configured(vpn))
		return;

	bgp_evpn_derive_auto_rd(bgp, vpn);
	update_advertise_vni_routes(bgp, vpn);
}

/*
 * Handle router-id change. Delete and withdraw local routes corresponding
 * to this VNI from peers. Note that this is invoked prior to updating
 * the router-id and is done only on the global route table, the routes
 * are needed in the per-VNI table to re-advertise with new router id.
 */
static void withdraw_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
{
	struct bgpevpn *vpn;

	vpn = (struct bgpevpn *)backet->data;

	if (!vpn) {
		zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__);
		return;
	}

	/* Skip VNIs with configured RD. */
	if (is_rd_configured(vpn))
		return;

	delete_withdraw_vni_routes(bgp, vpn);
}

/*
 * Process received EVPN type-2 route (advertise or withdraw).
 */
static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi,
			       struct attr *attr, u_char *pfx, int psize,
			       u_int32_t addpath_id)
{
	struct prefix_rd prd;
	struct prefix_evpn p;
	u_char ipaddr_len;
	u_char macaddr_len;
	mpls_label_t label[BGP_MAX_LABELS]; /* holds the VNI(s) as in packet */
	u_int32_t num_labels = 0;
	int ret;

	/* Type-2 route should be either 33, 37 or 49 bytes or an
	 * additional 3 bytes if there is a second label (VNI):
	 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
	 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
	 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
	 */
	if (psize != 33 && psize != 37 && psize != 49 && psize != 36
	    && psize != 40 && psize != 52) {
		zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
			 peer->bgp->vrf_id, peer->host, psize);
		return -1;
	}

	/* Make prefix_rd */
	prd.family = AF_UNSPEC;
	prd.prefixlen = 64;
	memcpy(&prd.val, pfx, 8);
	pfx += 8;

	/* Make EVPN prefix. */
	memset(&p, 0, sizeof(struct prefix_evpn));
	p.family = AF_EVPN;
	p.prefixlen = EVPN_TYPE_2_ROUTE_PREFIXLEN;
	p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;

	/* Skip over Ethernet Seg Identifier for now. */
	pfx += 10;

	/* Skip over Ethernet Tag for now. */
	pfx += 4;

	/* Get the MAC Addr len */
	macaddr_len = *pfx++;

	/* Get the MAC Addr */
	if (macaddr_len == (ETH_ALEN * 8)) {
		memcpy(&p.prefix.mac.octet, pfx, ETH_ALEN);
		pfx += ETH_ALEN;
	} else {
		zlog_err(
			"%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
			peer->bgp->vrf_id, peer->host, macaddr_len);
		return -1;
	}


	/* Get the IP. */
	ipaddr_len = *pfx++;
	if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN
	    && ipaddr_len != IPV6_MAX_BITLEN) {
		zlog_err(
			"%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
			peer->bgp->vrf_id, peer->host, ipaddr_len);
		return -1;
	}

	if (ipaddr_len) {
		ipaddr_len /= 8; /* Convert to bytes. */
		p.prefix.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN)
					       ? IPADDR_V4
					       : IPADDR_V6;
		memcpy(&p.prefix.ip.ip.addr, pfx, ipaddr_len);
	}
	pfx += ipaddr_len;

	/* Get the VNI(s). Stored as bytes here. */
	num_labels++;
	memset(label, 0, sizeof(label));
	memcpy(&label[0], pfx, BGP_LABEL_BYTES);
	pfx += BGP_LABEL_BYTES;
	psize -= (33 + ipaddr_len);
	/* Do we have a second VNI? */
	if (psize) {
		num_labels++;
		memcpy(&label[1], pfx, BGP_LABEL_BYTES);
		/*
		 * If in future, we are required to access additional fields,
		 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
		 * the next field
		 */
	}

	/* Process the route. */
	if (attr)
		ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
				 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
				 &prd, &label[0], num_labels, 0, NULL);
	else
		ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
				   afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
				   &prd, &label[0], num_labels, NULL);
	return ret;
}

/*
 * Process received EVPN type-3 route (advertise or withdraw).
 */
static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi,
			       struct attr *attr, u_char *pfx, int psize,
			       u_int32_t addpath_id)
{
	struct prefix_rd prd;
	struct prefix_evpn p;
	u_char ipaddr_len;
	int ret;

	/* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
	 * IP len (1) and IP (4 or 16).
	 */
	if (psize != 17 && psize != 29) {
		zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
			 peer->bgp->vrf_id, peer->host, psize);
		return -1;
	}

	/* Make prefix_rd */
	prd.family = AF_UNSPEC;
	prd.prefixlen = 64;
	memcpy(&prd.val, pfx, 8);
	pfx += 8;

	/* Make EVPN prefix. */
	memset(&p, 0, sizeof(struct prefix_evpn));
	p.family = AF_EVPN;
	p.prefixlen = EVPN_TYPE_3_ROUTE_PREFIXLEN;
	p.prefix.route_type = BGP_EVPN_IMET_ROUTE;

	/* Skip over Ethernet Tag for now. */
	pfx += 4;

	/* Get the IP. */
	ipaddr_len = *pfx++;
	if (ipaddr_len == IPV4_MAX_BITLEN) {
		p.prefix.ip.ipa_type = IPADDR_V4;
		memcpy(&p.prefix.ip.ip.addr, pfx, IPV4_MAX_BYTELEN);
	} else {
		zlog_err(
			"%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
			peer->bgp->vrf_id, peer->host, ipaddr_len);
		return -1;
	}

	/* Process the route. */
	if (attr)
		ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
				 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
				 &prd, NULL, 0, 0, NULL);
	else
		ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
				   afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
				   &prd, NULL, 0, NULL);
	return ret;
}

/*
 * Process received EVPN type-5 route (advertise or withdraw).
 */
static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi,
			       struct attr *attr, u_char *pfx, int psize,
			       u_int32_t addpath_id, int withdraw)
{
	struct prefix_rd prd;
	struct prefix_evpn p;
	struct bgp_route_evpn evpn;
	u_char ippfx_len;
	u_int32_t eth_tag;
	mpls_label_t label; /* holds the VNI as in the packet */
	int ret;

	/* Type-5 route should be 34 or 58 bytes:
	 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
	 * GW (4 or 16) and VNI (3).
	 * Note that the IP and GW should both be IPv4 or both IPv6.
	 */
	if (psize != 34 && psize != 58) {
		zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
			 peer->bgp->vrf_id, peer->host, psize);
		return -1;
	}

	/* Make prefix_rd */
	prd.family = AF_UNSPEC;
	prd.prefixlen = 64;
	memcpy(&prd.val, pfx, 8);
	pfx += 8;

	/* Make EVPN prefix. */
	memset(&p, 0, sizeof(struct prefix_evpn));
	p.family = AF_EVPN;
	p.prefixlen = EVPN_TYPE_5_ROUTE_PREFIXLEN;
	p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;

	/* Additional information outside of prefix - ESI and GW IP */
	memset(&evpn, 0, sizeof(evpn));

	/* Fetch ESI */
	memcpy(&evpn.eth_s_id.val, pfx, 10);
	pfx += 10;

	/* Fetch Ethernet Tag. */
	memcpy(&eth_tag, pfx, 4);
	p.prefix.eth_tag = ntohl(eth_tag);
	pfx += 4;

	/* Fetch IP prefix length. */
	ippfx_len = *pfx++;
	if (ippfx_len > IPV6_MAX_BITLEN) {
		zlog_err(
			"%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
			peer->bgp->vrf_id, peer->host, ippfx_len);
		return -1;
	}
	p.prefix.ip_prefix_length = ippfx_len;

	/* Determine IPv4 or IPv6 prefix */
	/* Since the address and GW are from the same family, this just becomes
	 * a simple check on the total size.
	 */
	if (psize == 34) {
		SET_IPADDR_V4(&p.prefix.ip);
		memcpy(&p.prefix.ip.ipaddr_v4, pfx, 4);
		pfx += 4;
		memcpy(&evpn.gw_ip.ipv4, pfx, 4);
		pfx += 4;
	} else {
		SET_IPADDR_V6(&p.prefix.ip);
		memcpy(&p.prefix.ip.ipaddr_v6, pfx, 16);
		pfx += 16;
		memcpy(&evpn.gw_ip.ipv6, pfx, 16);
		pfx += 16;
	}

	/* Get the VNI (in MPLS label field). Stored as bytes here. */
	memset(&label, 0, sizeof(label));
	memcpy(&label, pfx, BGP_LABEL_BYTES);

	/*
	 * If in future, we are required to access additional fields,
	 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
	 * field
	 */

	/* Process the route. */
	if (!withdraw)
		ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
				 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
				 &prd, &label, 1, 0, &evpn);
	else
		ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
				   afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
				   &prd, &label, 1, &evpn);

	return ret;
}

static void evpn_mpattr_encode_type5(struct stream *s, struct prefix *p,
				     struct prefix_rd *prd, mpls_label_t *label,
				     u_int32_t num_labels, struct attr *attr)
{
	int len;
	char temp[16];
	struct evpn_addr *p_evpn_p;

	memset(&temp, 0, 16);
	if (p->family != AF_EVPN)
		return;
	p_evpn_p = &(p->u.prefix_evpn);

	/* len denites the total len of IP and GW-IP in the route
	   IP and GW-IP have to be both ipv4 or ipv6
	 */
	if (IS_IPADDR_V4(&p_evpn_p->ip))
		len = 8; /* IP and GWIP are both ipv4 */
	else
		len = 32; /* IP and GWIP are both ipv6 */
	/* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
	stream_putc(s, 8 + 10 + 4 + 1 + len + 3);
	stream_put(s, prd->val, 8);
	if (attr)
		stream_put(s, &(attr->evpn_overlay.eth_s_id), 10);
	else
		stream_put(s, &temp, 10);
	stream_putl(s, p_evpn_p->eth_tag);
	stream_putc(s, p_evpn_p->ip_prefix_length);
	if (IS_IPADDR_V4(&p_evpn_p->ip))
		stream_put_ipv4(s, p_evpn_p->ip.ipaddr_v4.s_addr);
	else
		stream_put(s, &p_evpn_p->ip.ipaddr_v6, 16);
	if (attr) {
		if (IS_IPADDR_V4(&p_evpn_p->ip))
			stream_put_ipv4(s,
					attr->evpn_overlay.gw_ip.ipv4.s_addr);
		else
			stream_put(s, &(attr->evpn_overlay.gw_ip.ipv6), 16);
	} else {
		if (IS_IPADDR_V4(&p_evpn_p->ip))
			stream_put_ipv4(s, 0);
		else
			stream_put(s, &temp, 16);
	}

	if (num_labels)
		stream_put(s, label, 3);
	else
		stream_put3(s, 0);
}

/*
 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
 */
static void cleanup_vni_on_disable(struct hash_backet *backet, struct bgp *bgp)
{
	struct bgpevpn *vpn = (struct bgpevpn *)backet->data;

	/* Remove EVPN routes and schedule for processing. */
	delete_routes_for_vni(bgp, vpn);

	/* Clear "live" flag and see if hash needs to be freed. */
	UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
	if (!is_vni_configured(vpn))
		bgp_evpn_free(bgp, vpn);
}

/*
 * Free a VNI entry; iterator function called during cleanup.
 */
static void free_vni_entry(struct hash_backet *backet, struct bgp *bgp)
{
	struct bgpevpn *vpn;

	vpn = (struct bgpevpn *)backet->data;
	delete_all_vni_routes(bgp, vpn);
	bgp_evpn_free(bgp, vpn);
}

/*
 * Derive AUTO import RT for BGP VRF - L3VNI
 */
static void evpn_auto_rt_import_add_for_vrf(struct bgp *bgp_vrf)
{
	struct bgp *bgp_def = NULL;

	form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
	UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);

	/* Map RT to VRF */
	bgp_def = bgp_get_default();
	if (!bgp_def)
		return;
	bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
}

/*
 * Delete AUTO import RT from BGP VRF - L3VNI
 */
static void evpn_auto_rt_import_delete_for_vrf(struct bgp *bgp_vrf)
{
	evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
}

/*
 * Derive AUTO export RT for BGP VRF - L3VNI
 */
static void evpn_auto_rt_export_add_for_vrf(struct bgp *bgp_vrf)
{
	UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
	form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
}

/*
 * Delete AUTO export RT from BGP VRF - L3VNI
 */
static void evpn_auto_rt_export_delete_for_vrf(struct bgp *bgp_vrf)
{
	evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
}

static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp *bgp_vrf)
{
	struct bgp *bgp_def = NULL;
	struct listnode *node = NULL;
	struct bgpevpn *vpn = NULL;

	bgp_def = bgp_get_default();
	if (!bgp_def)
		return;

	/* update all type-5 routes */
	update_advertise_vrf_routes(bgp_vrf);

	/* update all type-2 routes */
	for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
		update_routes_for_vni(bgp_def, vpn);
}

/*
 * Public functions.
 */

/* withdraw type-5 route corresponding to ip prefix */
void bgp_evpn_withdraw_type5_route(struct bgp *bgp_vrf, struct prefix *p,
				   afi_t afi, safi_t safi)
{
	int ret = 0;
	struct prefix_evpn evp;
	char buf[PREFIX_STRLEN];

	/* NOTE: Check needed as this is called per-route also. */
	if (!advertise_type5_routes(bgp_vrf, afi))
		return;

	build_type5_prefix_from_ip_prefix(&evp, p);
	ret = delete_evpn_type5_route(bgp_vrf, &evp);
	if (ret) {
		zlog_err(
			"%u failed to delete type-5 route for prefix %s in vrf %s",
			bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf)),
			vrf_id_to_name(bgp_vrf->vrf_id));
	}
}

/* withdraw all type-5 routes for an address family */
void bgp_evpn_withdraw_type5_routes(struct bgp *bgp_vrf, afi_t afi, safi_t safi)
{
	struct bgp_table *table = NULL;
	struct bgp_node *rn = NULL;
	struct bgp_info *ri;

	/* Bail out early if we don't have to advertise type-5 routes. */
	if (!advertise_type5_routes(bgp_vrf, afi))
		return;

	table = bgp_vrf->rib[afi][safi];
	for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
		/* Only care about "selected" routes - non-imported. */
		/* TODO: Support for AddPath for EVPN. */
		for (ri = rn->info; ri; ri = ri->next) {
			if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED)
			    && (!ri->extra || !ri->extra->parent)) {
				bgp_evpn_withdraw_type5_route(bgp_vrf, &rn->p,
							      afi, safi);
				break;
			}
		}
	}
}

/*
 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
 * to this function correspond to those of the source IP prefix (best
 * path in the case of the attr. In the case of a local prefix (when we
 * are advertising local subnets), the src_attr will be NULL.
 */
void bgp_evpn_advertise_type5_route(struct bgp *bgp_vrf, struct prefix *p,
				    struct attr *src_attr, afi_t afi,
				    safi_t safi)
{
	int ret = 0;
	struct prefix_evpn evp;
	char buf[PREFIX_STRLEN];

	/* NOTE: Check needed as this is called per-route also. */
	if (!advertise_type5_routes(bgp_vrf, afi))
		return;

	build_type5_prefix_from_ip_prefix(&evp, p);
	ret = update_evpn_type5_route(bgp_vrf, &evp, src_attr);
	if (ret)
		zlog_err("%u: Failed to create type-5 route for prefix %s",
			 bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf)));
}

/* Inject all prefixes of a particular address-family (currently, IPv4 or
 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
 * advertisement is enabled.
 */
void bgp_evpn_advertise_type5_routes(struct bgp *bgp_vrf, afi_t afi,
				     safi_t safi)
{
	struct bgp_table *table = NULL;
	struct bgp_node *rn = NULL;
	struct bgp_info *ri;

	/* Bail out early if we don't have to advertise type-5 routes. */
	if (!advertise_type5_routes(bgp_vrf, afi))
		return;

	table = bgp_vrf->rib[afi][safi];
	for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
		/* Need to identify the "selected" route entry to use its
		 * attribute. Also, we only consider "non-imported" routes.
		 * TODO: Support for AddPath for EVPN.
		 */
		for (ri = rn->info; ri; ri = ri->next) {
			if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED)
			    && (!ri->extra || !ri->extra->parent)) {

				/* apply the route-map */
				if (bgp_vrf->adv_cmd_rmap[afi][safi].map) {
					int ret = 0;

					ret = route_map_apply(
						bgp_vrf->adv_cmd_rmap[afi][safi]
							.map,
						&rn->p, RMAP_BGP, ri);
					if (ret == RMAP_DENYMATCH)
						continue;
				}
				bgp_evpn_advertise_type5_route(
					bgp_vrf, &rn->p, ri->attr, afi, safi);
				break;
			}
		}
	}
}

void evpn_rt_delete_auto(struct bgp *bgp, vni_t vni, struct list *rtl)
{
	struct listnode *node, *nnode, *node_to_del;
	struct ecommunity *ecom, *ecom_auto;
	struct ecommunity_val eval;

	encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);

	ecom_auto = ecommunity_new();
	ecommunity_add_val(ecom_auto, &eval);
	node_to_del = NULL;

	for (ALL_LIST_ELEMENTS(rtl, node, nnode, ecom)) {
		if (ecommunity_match(ecom, ecom_auto)) {
			ecommunity_free(&ecom);
			node_to_del = node;
		}
	}

	if (node_to_del)
		list_delete_node(rtl, node_to_del);

	ecommunity_free(&ecom_auto);
}

void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
					  struct ecommunity *ecomadd)
{
	/* uninstall routes from vrf */
	uninstall_routes_for_vrf(bgp_vrf);

	/* Cleanup the RT to VRF mapping */
	bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);

	/* Remove auto generated RT */
	evpn_auto_rt_import_delete_for_vrf(bgp_vrf);

	/* Add the newly configured RT to RT list */
	listnode_add_sort(bgp_vrf->vrf_import_rtl, ecomadd);
	SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);

	/* map VRF to its RTs */
	bgp_evpn_map_vrf_to_its_rts(bgp_vrf);

	/* install routes matching the new VRF */
	install_routes_for_vrf(bgp_vrf);
}

void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
					    struct ecommunity *ecomdel)
{
	struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
	struct ecommunity *ecom = NULL;

	/* uninstall routes from vrf */
	uninstall_routes_for_vrf(bgp_vrf);

	/* Cleanup the RT to VRF mapping */
	bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);

	/* remove the RT from the RT list */
	for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
		if (ecommunity_match(ecom, ecomdel)) {
			ecommunity_free(&ecom);
			node_to_del = node;
			break;
		}
	}

	if (node_to_del)
		list_delete_node(bgp_vrf->vrf_import_rtl, node_to_del);

	/* fallback to auto import rt, if this was the last RT */
	if (list_isempty(bgp_vrf->vrf_import_rtl)) {
		UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
		evpn_auto_rt_import_add_for_vrf(bgp_vrf);
	}

	/* map VRFs to its RTs */
	bgp_evpn_map_vrf_to_its_rts(bgp_vrf);

	/* install routes matching this new RT */
	install_routes_for_vrf(bgp_vrf);
}

void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
					  struct ecommunity *ecomadd)
{
	/* remove auto-generated RT */
	evpn_auto_rt_export_delete_for_vrf(bgp_vrf);

	/* Add the new RT to the RT list */
	listnode_add_sort(bgp_vrf->vrf_export_rtl, ecomadd);
	SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);

	bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
}

void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
					    struct ecommunity *ecomdel)
{
	struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
	struct ecommunity *ecom = NULL;

	/* Remove the RT from the RT list */
	for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_export_rtl, node, nnode, ecom)) {
		if (ecommunity_match(ecom, ecomdel)) {
			ecommunity_free(&ecom);
			node_to_del = node;
			break;
		}
	}

	if (node_to_del)
		list_delete_node(bgp_vrf->vrf_export_rtl, node_to_del);

	/* fall back to auto-generated RT if this was the last RT */
	if (bgp_vrf->vrf_export_rtl && list_isempty(bgp_vrf->vrf_export_rtl)) {
		UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
		evpn_auto_rt_export_add_for_vrf(bgp_vrf);
	}

	bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
}

/*
 * Handle change to BGP router id. This is invoked twice by the change
 * handler, first before the router id has been changed and then after
 * the router id has been changed. The first invocation will result in
 * local routes for all VNIs/VRF being deleted and withdrawn and the next
 * will result in the routes being re-advertised.
 */
void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
{
	if (withdraw) {

		/* delete and withdraw all the type-5 routes
		   stored in the global table for this vrf
		 */
		withdraw_router_id_vrf(bgp);

		/* delete all the VNI routes (type-2/type-3) routes for all the
		 * L2-VNIs
		 */
		hash_iterate(bgp->vnihash,
			     (void (*)(struct hash_backet *,
				       void *))withdraw_router_id_vni,
			     bgp);
	} else {

		/* advertise all routes in the vrf as type-5 routes with the new
		 * RD
		 */
		update_router_id_vrf(bgp);

		/* advertise all the VNI routes (type-2/type-3) routes with the
		 * new RD
		 */
		hash_iterate(bgp->vnihash,
			     (void (*)(struct hash_backet *,
				       void *))update_router_id_vni,
			     bgp);
	}
}

/*
 * Handle change to export RT - update and advertise local routes.
 */
int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
{
	return update_routes_for_vni(bgp, vpn);
}

void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw)
{
	if (withdraw)
		delete_withdraw_vrf_routes(bgp_vrf);
	else
		update_advertise_vrf_routes(bgp_vrf);
}

/*
 * Handle change to RD. This is invoked twice by the change handler,
 * first before the RD has been changed and then after the RD has
 * been changed. The first invocation will result in local routes
 * of this VNI being deleted and withdrawn and the next will result
 * in the routes being re-advertised.
 */
void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
			       int withdraw)
{
	if (withdraw)
		delete_withdraw_vni_routes(bgp, vpn);
	else
		update_advertise_vni_routes(bgp, vpn);
}

/*
 * Install routes for this VNI. Invoked upon change to Import RT.
 */
int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
{
	return install_routes_for_vni(bgp, vpn);
}

/*
 * Uninstall all routes installed for this VNI. Invoked upon change
 * to Import RT.
 */
int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
{
	return uninstall_routes_for_vni(bgp, vpn);
}

/*
 * TODO: Hardcoded for a maximum of 2 VNIs right now
 */
char *bgp_evpn_label2str(mpls_label_t *label, u_int32_t num_labels, char *buf,
			 int len)
{
	vni_t vni1, vni2;

	vni1 = label2vni(label);
	if (num_labels == 2) {
		vni2 = label2vni(label + 1);
		snprintf(buf, len, "%u/%u", vni1, vni2);
	} else
		snprintf(buf, len, "%u", vni1);
	return buf;
}

/*
 * Function to convert evpn route to json format.
 * NOTE: We don't use prefix2str as the output here is a bit different.
 */
void bgp_evpn_route2json(struct prefix_evpn *p, json_object *json)
{
	char buf1[ETHER_ADDR_STRLEN];
	char buf2[PREFIX2STR_BUFFER];

	if (!json)
		return;

	if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
		json_object_int_add(json, "routeType", p->prefix.route_type);
		json_object_int_add(json, "ethTag", 0);
		json_object_int_add(json, "ipLen",
				    IS_EVPN_PREFIX_IPADDR_V4(p)
					    ? IPV4_MAX_BITLEN
					    : IPV6_MAX_BITLEN);
		json_object_string_add(json, "ip",
				       inet_ntoa(p->prefix.ip.ipaddr_v4));
	} else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
		if (IS_EVPN_PREFIX_IPADDR_NONE(p)) {
			json_object_int_add(json, "routeType",
					    p->prefix.route_type);
			json_object_int_add(
				json, "esi",
				0); /* TODO: we don't support esi yet */
			json_object_int_add(json, "ethTag", 0);
			json_object_int_add(json, "macLen", 8 * ETH_ALEN);
			json_object_string_add(json, "mac",
					       prefix_mac2str(&p->prefix.mac,
							      buf1,
							      sizeof(buf1)));
		} else {
			u_char family;

			family = IS_EVPN_PREFIX_IPADDR_V4(p) ? AF_INET
							     : AF_INET6;

			json_object_int_add(json, "routeType",
					    p->prefix.route_type);
			json_object_int_add(
				json, "esi",
				0); /* TODO: we don't support esi yet */
			json_object_int_add(json, "ethTag", 0);
			json_object_int_add(json, "macLen", 8 * ETH_ALEN);
			json_object_string_add(json, "mac",
					       prefix_mac2str(&p->prefix.mac,
							      buf1,
							      sizeof(buf1)));
			json_object_int_add(json, "ipLen",
					    IS_EVPN_PREFIX_IPADDR_V4(p)
						    ? IPV4_MAX_BITLEN
						    : IPV6_MAX_BITLEN);
			json_object_string_add(
				json, "ip",
				inet_ntop(family, &p->prefix.ip.ip.addr, buf2,
					  PREFIX2STR_BUFFER));
		}
	} else {
		/* Currently, this is to cater to other AF_ETHERNET code. */
	}
}

/*
 * Function to convert evpn route to string.
 * NOTE: We don't use prefix2str as the output here is a bit different.
 */
char *bgp_evpn_route2str(struct prefix_evpn *p, char *buf, int len)
{
	char buf1[ETHER_ADDR_STRLEN];
	char buf2[PREFIX2STR_BUFFER];

	if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
		snprintf(buf, len, "[%d]:[0]:[%d]:[%s]", p->prefix.route_type,
			 IS_EVPN_PREFIX_IPADDR_V4(p) ? IPV4_MAX_BITLEN
						     : IPV6_MAX_BITLEN,
			 inet_ntoa(p->prefix.ip.ipaddr_v4));
	} else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
		if (IS_EVPN_PREFIX_IPADDR_NONE(p))
			snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]",
				 p->prefix.route_type, 8 * ETH_ALEN,
				 prefix_mac2str(&p->prefix.mac, buf1,
						sizeof(buf1)));
		else {
			u_char family;

			family = IS_EVPN_PREFIX_IPADDR_V4(p) ? AF_INET
							     : AF_INET6;
			snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]:[%d]:[%s]",
				 p->prefix.route_type, 8 * ETH_ALEN,
				 prefix_mac2str(&p->prefix.mac, buf1,
						sizeof(buf1)),
				 family == AF_INET ? IPV4_MAX_BITLEN
						   : IPV6_MAX_BITLEN,
				 inet_ntop(family, &p->prefix.ip.ip.addr, buf2,
					   PREFIX2STR_BUFFER));
		}
	} else if (p->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) {
		snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]",
			 p->prefix.route_type, p->prefix.ip_prefix_length,
			 IS_EVPN_PREFIX_IPADDR_V4(p)
				 ? inet_ntoa(p->prefix.ip.ipaddr_v4)
				 : inet6_ntoa(p->prefix.ip.ipaddr_v6));
	} else {
		/* For EVPN route types not supported yet. */
		snprintf(buf, len, "(unsupported route type %d)",
			 p->prefix.route_type);
	}

	return (buf);
}

/*
 * Encode EVPN prefix in Update (MP_REACH)
 */
void bgp_evpn_encode_prefix(struct stream *s, struct prefix *p,
			    struct prefix_rd *prd, mpls_label_t *label,
			    u_int32_t num_labels, struct attr *attr,
			    int addpath_encode, u_int32_t addpath_tx_id)
{
	struct prefix_evpn *evp = (struct prefix_evpn *)p;
	int len, ipa_len = 0;

	if (addpath_encode)
		stream_putl(s, addpath_tx_id);

	/* Route type */
	stream_putc(s, evp->prefix.route_type);

	switch (evp->prefix.route_type) {
	case BGP_EVPN_MAC_IP_ROUTE:
		if (IS_EVPN_PREFIX_IPADDR_V4(evp))
			ipa_len = IPV4_MAX_BYTELEN;
		else if (IS_EVPN_PREFIX_IPADDR_V6(evp))
			ipa_len = IPV6_MAX_BYTELEN;
		/* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
		len = 8 + 10 + 4 + 1 + 6 + 1 + ipa_len + 3;
		if (ipa_len && num_labels > 1) /* There are 2 VNIs */
			len += 3;
		stream_putc(s, len);
		stream_put(s, prd->val, 8);   /* RD */
		stream_put(s, 0, 10);	 /* ESI */
		stream_putl(s, 0);	    /* Ethernet Tag ID */
		stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
		stream_put(s, evp->prefix.mac.octet, 6); /* Mac Addr */
		stream_putc(s, 8 * ipa_len);		 /* IP address Length */
		if (ipa_len)				 /* IP */
			stream_put(s, &evp->prefix.ip.ip.addr, ipa_len);
		/* 1st label is the L2 VNI */
		stream_put(s, label, BGP_LABEL_BYTES);
		/* Include 2nd label (L3 VNI) if advertising MAC+IP */
		if (ipa_len && num_labels > 1)
			stream_put(s, label + 1, BGP_LABEL_BYTES);
		break;

	case BGP_EVPN_IMET_ROUTE:
		stream_putc(s, 17); // TODO: length - assumes IPv4 address
		stream_put(s, prd->val, 8);      /* RD */
		stream_putl(s, 0);		 /* Ethernet Tag ID */
		stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
		/* Originating Router's IP Addr */
		stream_put_in_addr(s, &evp->prefix.ip.ipaddr_v4);
		break;

	case BGP_EVPN_IP_PREFIX_ROUTE:
		/* TODO: AddPath support. */
		evpn_mpattr_encode_type5(s, p, prd, label, num_labels, attr);
		break;

	default:
		break;
	}
}

int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
			struct bgp_nlri *packet, int withdraw)
{
	u_char *pnt;
	u_char *lim;
	afi_t afi;
	safi_t safi;
	u_int32_t addpath_id;
	int addpath_encoded;
	int psize = 0;
	u_char rtype;
	u_char rlen;
	struct prefix p;

	/* Check peer status. */
	if (peer->status != Established) {
		zlog_err("%u:%s - EVPN update received in state %d",
			 peer->bgp->vrf_id, peer->host, peer->status);
		return -1;
	}

	/* Start processing the NLRI - there may be multiple in the MP_REACH */
	pnt = packet->nlri;
	lim = pnt + packet->length;
	afi = packet->afi;
	safi = packet->safi;
	addpath_id = 0;

	addpath_encoded =
		(CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
		 && CHECK_FLAG(peer->af_cap[afi][safi],
			       PEER_CAP_ADDPATH_AF_TX_RCV));

	for (; pnt < lim; pnt += psize) {
		/* Clear prefix structure. */
		memset(&p, 0, sizeof(struct prefix));

		/* Deal with path-id if AddPath is supported. */
		if (addpath_encoded) {
			/* When packet overflow occurs return immediately. */
			if (pnt + BGP_ADDPATH_ID_LEN > lim)
				return -1;

			addpath_id = ntohl(*((uint32_t *)pnt));
			pnt += BGP_ADDPATH_ID_LEN;
		}

		/* All EVPN NLRI types start with type and length. */
		if (pnt + 2 > lim)
			return -1;

		rtype = *pnt++;
		psize = rlen = *pnt++;

		/* When packet overflow occur return immediately. */
		if (pnt + psize > lim)
			return -1;

		switch (rtype) {
		case BGP_EVPN_MAC_IP_ROUTE:
			if (process_type2_route(peer, afi, safi,
						withdraw ? NULL : attr, pnt,
						psize, addpath_id)) {
				zlog_err(
					"%u:%s - Error in processing EVPN type-2 NLRI size %d",
					peer->bgp->vrf_id, peer->host, psize);
				return -1;
			}
			break;

		case BGP_EVPN_IMET_ROUTE:
			if (process_type3_route(peer, afi, safi,
						withdraw ? NULL : attr, pnt,
						psize, addpath_id)) {
				zlog_err(
					"%u:%s - Error in processing EVPN type-3 NLRI size %d",
					peer->bgp->vrf_id, peer->host, psize);
				return -1;
			}
			break;

		case BGP_EVPN_IP_PREFIX_ROUTE:
			if (process_type5_route(peer, afi, safi, attr, pnt,
						psize, addpath_id, withdraw)) {
				zlog_err(
					"%u:%s - Error in processing EVPN type-5 NLRI size %d",
					peer->bgp->vrf_id, peer->host, psize);
				return -1;
			}
			break;

		default:
			break;
		}
	}

	/* Packet length consistency check. */
	if (pnt != lim)
		return -1;

	return 0;
}

/*
 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
 * The mapping will be used during route processing.
 * bgp_def: default bgp instance
 * bgp_vrf: specific bgp vrf instance on which RT is configured
 */
void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf)
{
	int i = 0;
	struct ecommunity_val *eval = NULL;
	struct listnode *node = NULL, *nnode = NULL;
	struct ecommunity *ecom = NULL;

	for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
		for (i = 0; i < ecom->size; i++) {
			eval = (struct ecommunity_val *)(ecom->val
							 + (i
							    * ECOMMUNITY_SIZE));
			map_vrf_to_rt(bgp_vrf, eval);
		}
	}
}

/*
 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
 */
void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf)
{
	int i;
	struct ecommunity_val *eval;
	struct listnode *node, *nnode;
	struct ecommunity *ecom;

	for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
		for (i = 0; i < ecom->size; i++) {
			struct vrf_irt_node *irt;
			struct ecommunity_val eval_tmp;

			eval = (struct ecommunity_val *)(ecom->val
							 + (i
							    * ECOMMUNITY_SIZE));
			/* If using "automatic" RT, we only care about the
			 * local-admin sub-field.
			 * This is to facilitate using VNI as the RT for EBGP
			 * peering too.
			 */
			memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
			if (!CHECK_FLAG(bgp_vrf->vrf_flags,
					BGP_VRF_IMPORT_RT_CFGD))
				mask_ecom_global_admin(&eval_tmp, eval);

			irt = lookup_vrf_import_rt(&eval_tmp);
			if (irt)
				unmap_vrf_from_rt(bgp_vrf, irt);
		}
	}
}


/*
 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
 * The mapping will be used during route processing.
 */
void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
{
	int i;
	struct ecommunity_val *eval;
	struct listnode *node, *nnode;
	struct ecommunity *ecom;

	for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
		for (i = 0; i < ecom->size; i++) {
			eval = (struct ecommunity_val *)(ecom->val
							 + (i
							    * ECOMMUNITY_SIZE));
			map_vni_to_rt(bgp, vpn, eval);
		}
	}
}

/*
 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
 */
void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
{
	int i;
	struct ecommunity_val *eval;
	struct listnode *node, *nnode;
	struct ecommunity *ecom;

	for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
		for (i = 0; i < ecom->size; i++) {
			struct irt_node *irt;
			struct ecommunity_val eval_tmp;

			eval = (struct ecommunity_val *)(ecom->val
							 + (i
							    * ECOMMUNITY_SIZE));
			/* If using "automatic" RT, we only care about the
			 * local-admin sub-field.
			 * This is to facilitate using VNI as the RT for EBGP
			 * peering too.
			 */
			memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
			if (!is_import_rt_configured(vpn))
				mask_ecom_global_admin(&eval_tmp, eval);

			irt = lookup_import_rt(bgp, &eval_tmp);
			if (irt)
				unmap_vni_from_rt(bgp, vpn, irt);
		}
	}
}

/*
 * Derive Import RT automatically for VNI and map VNI to RT.
 * The mapping will be used during route processing.
 */
void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
{
	form_auto_rt(bgp, vpn->vni, vpn->import_rtl);
	UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);

	/* Map RT to VNI */
	bgp_evpn_map_vni_to_its_rts(bgp, vpn);
}

/*
 * Derive Export RT automatically for VNI.
 */
void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
{
	form_auto_rt(bgp, vpn->vni, vpn->export_rtl);
	UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
}

/*
 * Derive RD automatically for VNI using passed information - it
 * is of the form RouterId:unique-id-for-vni.
 */
void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp)
{
	char buf[100];

	bgp->vrf_prd.family = AF_UNSPEC;
	bgp->vrf_prd.prefixlen = 64;
	sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), bgp->vrf_rd_id);
	str2prefix_rd(buf, &bgp->vrf_prd);
}

/*
 * Derive RD automatically for VNI using passed information - it
 * is of the form RouterId:unique-id-for-vni.
 */
void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
{
	char buf[100];

	vpn->prd.family = AF_UNSPEC;
	vpn->prd.prefixlen = 64;
	sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), vpn->rd_id);
	(void)str2prefix_rd(buf, &vpn->prd);
	UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
}

/*
 * Lookup VNI.
 */
struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
{
	struct bgpevpn *vpn;
	struct bgpevpn tmp;

	memset(&tmp, 0, sizeof(struct bgpevpn));
	tmp.vni = vni;
	vpn = hash_lookup(bgp->vnihash, &tmp);
	return vpn;
}

/*
 * Create a new vpn - invoked upon configuration or zebra notification.
 */
struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
			     struct in_addr originator_ip,
			     vrf_id_t tenant_vrf_id)
{
	struct bgpevpn *vpn;

	if (!bgp)
		return NULL;

	vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
	if (!vpn)
		return NULL;

	/* Set values - RD and RT set to defaults. */
	vpn->vni = vni;
	vpn->originator_ip = originator_ip;
	vpn->tenant_vrf_id = tenant_vrf_id;

	/* Initialize route-target import and export lists */
	vpn->import_rtl = list_new();
	vpn->import_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
	vpn->export_rtl = list_new();
	vpn->export_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
	bf_assign_index(bm->rd_idspace, vpn->rd_id);
	derive_rd_rt_for_vni(bgp, vpn);

	/* Initialize EVPN route table. */
	vpn->route_table = bgp_table_init(AFI_L2VPN, SAFI_EVPN);

	/* Add to hash */
	if (!hash_get(bgp->vnihash, vpn, hash_alloc_intern)) {
		XFREE(MTYPE_BGP_EVPN, vpn);
		return NULL;
	}

	/* add to l2vni list on corresponding vrf */
	bgpevpn_link_to_l3vni(vpn);

	QOBJ_REG(vpn, bgpevpn);
	return vpn;
}

/*
 * Free a given VPN - called in multiple scenarios such as zebra
 * notification, configuration being deleted, advertise-all-vni disabled etc.
 * This just frees appropriate memory, caller should have taken other
 * needed actions.
 */
void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
{
	bgpevpn_unlink_from_l3vni(vpn);
	bgp_table_unlock(vpn->route_table);
	bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
	list_delete_and_null(&vpn->import_rtl);
	list_delete_and_null(&vpn->export_rtl);
	bf_release_index(bm->rd_idspace, vpn->rd_id);
	hash_release(bgp->vnihash, vpn);
	QOBJ_UNREG(vpn);
	XFREE(MTYPE_BGP_EVPN, vpn);
}

/*
 * Import route into matching VNI(s).
 */
int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
			  struct prefix *p, struct bgp_info *ri)
{
	return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 1);
}

/*
 * Unimport route from matching VNI(s).
 */
int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
			    struct prefix *p, struct bgp_info *ri)
{
	return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 0);
}

/* filter routes which have martian next hops */
int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp)
{
	afi_t afi;
	safi_t safi;
	struct bgp_node *rd_rn, *rn;
	struct bgp_table *table;
	struct bgp_info *ri;

	afi = AFI_L2VPN;
	safi = SAFI_EVPN;

	/* Walk entire global routing table and evaluate routes which could be
	 * imported into this VPN. Note that we cannot just look at the routes
	 * for the VNI's RD -
	 * remote routes applicable for this VNI could have any RD.
	 */
	/* EVPN routes are a 2-level table. */
	for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
	     rd_rn = bgp_route_next(rd_rn)) {
		table = (struct bgp_table *)(rd_rn->info);
		if (!table)
			continue;

		for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {

			for (ri = rn->info; ri; ri = ri->next) {

				/* Consider "valid" remote routes applicable for
				 * this VNI. */
				if (!(ri->type == ZEBRA_ROUTE_BGP
				      && ri->sub_type == BGP_ROUTE_NORMAL))
					continue;

				if (bgp_nexthop_self(bgp, ri->attr->nexthop)) {

					char attr_str[BUFSIZ];
					char pbuf[PREFIX_STRLEN];

					bgp_dump_attr(ri->attr, attr_str,
						      BUFSIZ);

					if (bgp_debug_update(ri->peer, &rn->p,
							     NULL, 1))
						zlog_debug(
							"%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
							bgp->vrf_id,
							prefix2str(
								&rn->p, pbuf,
								sizeof(pbuf)),
							attr_str);

					bgp_evpn_unimport_route(bgp, afi, safi,
								&rn->p, ri);

					bgp_rib_remove(rn, ri, ri->peer, afi,
						       safi);
				}
			}
		}
	}

	return 0;
}

/*
 * Handle del of a local MACIP.
 */
int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
			     struct ipaddr *ip)
{
	struct bgpevpn *vpn;
	struct prefix_evpn p;

	if (!bgp->vnihash) {
		zlog_err("%u: VNI hash not created", bgp->vrf_id);
		return -1;
	}

	/* Lookup VNI hash - should exist. */
	vpn = bgp_evpn_lookup_vni(bgp, vni);
	if (!vpn || !is_vni_live(vpn)) {
		zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
			  bgp->vrf_id, vni, vpn ? "not live" : "not found");
		return -1;
	}

	/* Remove EVPN type-2 route and schedule for processing. */
	build_evpn_type2_prefix(&p, mac, ip);
	delete_evpn_route(bgp, vpn, &p);

	return 0;
}

/*
 * Handle add of a local MACIP.
 */
int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
			     struct ipaddr *ip, u_char flags)
{
	struct bgpevpn *vpn;
	struct prefix_evpn p;

	if (!bgp->vnihash) {
		zlog_err("%u: VNI hash not created", bgp->vrf_id);
		return -1;
	}

	/* Lookup VNI hash - should exist. */
	vpn = bgp_evpn_lookup_vni(bgp, vni);
	if (!vpn || !is_vni_live(vpn)) {
		zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
			  bgp->vrf_id, vni, vpn ? "not live" : "not found");
		return -1;
	}

	/* Create EVPN type-2 route and schedule for processing. */
	build_evpn_type2_prefix(&p, mac, ip);
	if (update_evpn_route(bgp, vpn, &p, flags)) {
		char buf[ETHER_ADDR_STRLEN];
		char buf2[INET6_ADDRSTRLEN];

		zlog_err(
			"%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
			bgp->vrf_id, vpn->vni,
			CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY)
				? "sticky gateway"
				: "",
			prefix_mac2str(mac, buf, sizeof(buf)),
			ipaddr2str(ip, buf2, sizeof(buf2)), flags);
		return -1;
	}

	return 0;
}

static void link_l2vni_hash_to_l3vni(struct hash_backet *backet,
				     struct bgp *bgp_vrf)
{
	struct bgpevpn *vpn = NULL;
	struct bgp *bgp_def = NULL;

	bgp_def = bgp_get_default();
	assert(bgp_def);

	vpn = (struct bgpevpn *)backet->data;
	if (vpn->tenant_vrf_id == bgp_vrf->vrf_id)
		bgpevpn_link_to_l3vni(vpn);
}

int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id, struct ethaddr *rmac,
			     struct in_addr originator_ip, int filter)
{
	struct bgp *bgp_vrf = NULL; /* bgp VRF instance */
	struct bgp *bgp_def = NULL; /* default bgp instance */
	struct listnode *node = NULL;
	struct bgpevpn *vpn = NULL;
	as_t as = 0;

	/* get the default instamce - required to get the AS number for VRF
	 * auto-creatio
	 */
	bgp_def = bgp_get_default();
	if (!bgp_def) {
		zlog_err(
			"Cannot process L3VNI  %u ADD - default BGP instance not yet created",
			l3vni);
		return -1;
	}
	as = bgp_def->as;

	/* if the BGP vrf instance doesnt exist - create one */
	bgp_vrf = bgp_lookup_by_name(vrf_id_to_name(vrf_id));
	if (!bgp_vrf) {

		int ret = 0;

		ret = bgp_get(&bgp_vrf, &as, vrf_id_to_name(vrf_id),
			      BGP_INSTANCE_TYPE_VRF);
		switch (ret) {
		case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET:
			zlog_err("'bgp multiple-instance' not present\n");
			return -1;
		case BGP_ERR_AS_MISMATCH:
			zlog_err("BGP is already running; AS is %u\n", as);
			return -1;
		case BGP_ERR_INSTANCE_MISMATCH:
			zlog_err("BGP instance name and AS number mismatch\n");
			return -1;
		}

		/* mark as auto created */
		SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO);
	}

	/* associate with l3vni */
	bgp_vrf->l3vni = l3vni;

	/* set the router mac - to be used in mac-ip routes for this vrf */
	memcpy(&bgp_vrf->rmac, rmac, sizeof(struct ethaddr));

	/* set the originator ip */
	bgp_vrf->originator_ip = originator_ip;

	/* set the right filter - are we using l3vni only for prefix routes? */
	if (filter)
		SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);

	/* auto derive RD/RT */
	if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
		evpn_auto_rt_import_add_for_vrf(bgp_vrf);
	if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD))
		evpn_auto_rt_export_add_for_vrf(bgp_vrf);
	bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);

	/* link all corresponding l2vnis */
	hash_iterate(bgp_def->vnihash,
		     (void (*)(struct hash_backet *,
			       void *))link_l2vni_hash_to_l3vni,
		     bgp_vrf);

	/* Only update all corresponding type-2 routes if we are advertising two
	 * labels along with type-2 routes
	 */
	if (!filter)
		for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
			update_routes_for_vni(bgp_def, vpn);

	/* advertise type-5 routes if needed */
	update_advertise_vrf_routes(bgp_vrf);

	/* install all remote routes belonging to this l3vni into correspondng
	 * vrf */
	install_routes_for_vrf(bgp_vrf);

	return 0;
}

int bgp_evpn_local_l3vni_del(vni_t l3vni, vrf_id_t vrf_id)
{
	struct bgp *bgp_vrf = NULL; /* bgp vrf instance */
	struct bgp *bgp_def = NULL; /* default bgp instance */
	struct listnode *node = NULL;
	struct bgpevpn *vpn = NULL;

	bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
	if (!bgp_vrf) {
		zlog_err(
			"Cannot process L3VNI %u Del - Could not find BGP instance",
			l3vni);
		return -1;
	}

	bgp_def = bgp_get_default();
	if (!bgp_def) {
		zlog_err(
			"Cannot process L3VNI %u Del - Could not find default BGP instance",
			l3vni);
		return -1;
	}

	/* unimport remote routes from VRF, if it is AUTO vrf bgp_delete will
	 * take care of uninstalling the routes from zebra
	 */
	if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
		uninstall_routes_for_vrf(bgp_vrf);

	/* delete/withdraw all type-5 routes */
	delete_withdraw_vrf_routes(bgp_vrf);

	/* remove the l3vni from vrf instance */
	bgp_vrf->l3vni = 0;

	/* remove the Rmac from the BGP vrf */
	memset(&bgp_vrf->rmac, 0, sizeof(struct ethaddr));

	/* delete RD/RT */
	if (bgp_vrf->vrf_import_rtl && !list_isempty(bgp_vrf->vrf_import_rtl)) {
		bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
		list_delete_all_node(bgp_vrf->vrf_import_rtl);
	}
	if (bgp_vrf->vrf_export_rtl && !list_isempty(bgp_vrf->vrf_export_rtl)) {
		list_delete_all_node(bgp_vrf->vrf_export_rtl);
	}

	/* update all corresponding local mac-ip routes */
	if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY)) {
		for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
			UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
			update_routes_for_vni(bgp_def, vpn);
		}
	}

	/* Delete the instance if it was autocreated */
	if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
		bgp_delete(bgp_vrf);

	return 0;
}

/*
 * Handle del of a local VNI.
 */
int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
{
	struct bgpevpn *vpn;

	if (!bgp->vnihash) {
		zlog_err("%u: VNI hash not created", bgp->vrf_id);
		return -1;
	}

	/* Locate VNI hash */
	vpn = bgp_evpn_lookup_vni(bgp, vni);
	if (!vpn) {
		zlog_warn("%u: VNI hash entry for VNI %u not found at DEL",
			  bgp->vrf_id, vni);
		return 0;
	}

	/* Remove all local EVPN routes and schedule for processing (to
	 * withdraw from peers).
	 */
	delete_routes_for_vni(bgp, vpn);

	/*
	 * tunnel is no longer active, del tunnel ip address from tip_hash
	 */
	bgp_tip_del(bgp, &vpn->originator_ip);

	/* Clear "live" flag and see if hash needs to be freed. */
	UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
	if (!is_vni_configured(vpn))
		bgp_evpn_free(bgp, vpn);

	return 0;
}

/*
 * Handle add (or update) of a local VNI. The VNI changes we care
 * about are for the local-tunnel-ip and the (tenant) VRF.
 */
int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
			   struct in_addr originator_ip, vrf_id_t tenant_vrf_id)
{
	struct bgpevpn *vpn;
	struct prefix_evpn p;

	if (!bgp->vnihash) {
		zlog_err("%u: VNI hash not created", bgp->vrf_id);
		return -1;
	}

	/* Lookup VNI. If present and no change, exit. */
	vpn = bgp_evpn_lookup_vni(bgp, vni);
	if (vpn) {

		if (is_vni_live(vpn)
		    && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip)
		    && vpn->tenant_vrf_id == tenant_vrf_id)
			/* Probably some other param has changed that we don't
			 * care about. */
			return 0;

		/* Update tenant_vrf_id if it has changed. */
		if (vpn->tenant_vrf_id != tenant_vrf_id) {
			bgpevpn_unlink_from_l3vni(vpn);
			vpn->tenant_vrf_id = tenant_vrf_id;
			bgpevpn_link_to_l3vni(vpn);
		}

		/* If tunnel endpoint IP has changed, update (and delete prior
		 * type-3 route, if needed.)
		 */
		if (!IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
			handle_tunnel_ip_change(bgp, vpn, originator_ip);

		/* Update all routes with new endpoint IP and/or export RT
		 * for VRFs
		 */
		if (is_vni_live(vpn))
			update_routes_for_vni(bgp, vpn);
	}

	/* Create or update as appropriate. */
	if (!vpn) {
		vpn = bgp_evpn_new(bgp, vni, originator_ip, tenant_vrf_id);
		if (!vpn) {
			zlog_err(
				"%u: Failed to allocate VNI entry for VNI %u - at Add",
				bgp->vrf_id, vni);
			return -1;
		}
	}

	/* if the VNI is live already, there is nothing more to do */
	if (is_vni_live(vpn))
		return 0;

	/* Mark as "live" */
	SET_FLAG(vpn->flags, VNI_FLAG_LIVE);

	/* tunnel is now active, add tunnel-ip to db */
	bgp_tip_add(bgp, &originator_ip);

	/* filter routes as nexthop database has changed */
	bgp_filter_evpn_routes_upon_martian_nh_change(bgp);

	/* Create EVPN type-3 route and schedule for processing. */
	build_evpn_type3_prefix(&p, vpn->originator_ip);
	if (update_evpn_route(bgp, vpn, &p, 0)) {
		zlog_err("%u: Type3 route creation failure for VNI %u",
			 bgp->vrf_id, vni);
		return -1;
	}

	/* If we have learnt and retained remote routes (VTEPs, MACs) for this
	 * VNI,
	 * install them.
	 */
	install_routes_for_vni(bgp, vpn);

	/* If we are advertising gateway mac-ip
	   It needs to be conveyed again to zebra */
	bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni);

	return 0;
}

/*
 * Cleanup EVPN information on disable - Need to delete and withdraw
 * EVPN routes from peers.
 */
void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
{
	hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
					     void *))cleanup_vni_on_disable,
		     bgp);
}

/*
 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
 * BGP instance (default) is being freed.
 */
void bgp_evpn_cleanup(struct bgp *bgp)
{
	if (bgp->vnihash)
		hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
						     void *))free_vni_entry,
			     bgp);
	if (bgp->import_rt_hash)
		hash_free(bgp->import_rt_hash);
	bgp->import_rt_hash = NULL;
	if (bgp->vrf_import_rt_hash)
		hash_free(bgp->vrf_import_rt_hash);
	bgp->vrf_import_rt_hash = NULL;
	if (bgp->vnihash)
		hash_free(bgp->vnihash);
	bgp->vnihash = NULL;
	if (bgp->vrf_import_rtl)
		list_delete_and_null(&bgp->vrf_import_rtl);
	if (bgp->vrf_export_rtl)
		list_delete_and_null(&bgp->vrf_export_rtl);
	if (bgp->l2vnis)
		list_delete_and_null(&bgp->l2vnis);
	bf_release_index(bm->rd_idspace, bgp->vrf_rd_id);
}

/*
 * Initialization for EVPN
 * Create
 *  VNI hash table
 *  hash for RT to VNI
 *  assign a unique rd id for auto derivation of vrf_prd
 */
void bgp_evpn_init(struct bgp *bgp)
{
	bgp->vnihash =
		hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
	bgp->import_rt_hash =
		hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
			    "BGP Import RT Hash");
	bgp->vrf_import_rt_hash =
		hash_create(vrf_import_rt_hash_key_make, vrf_import_rt_hash_cmp,
			    "BGP VRF Import RT Hash");
	bgp->vrf_import_rtl = list_new();
	bgp->vrf_import_rtl->cmp =
		(int (*)(void *, void *))evpn_route_target_cmp;

	bgp->vrf_export_rtl = list_new();
	bgp->vrf_export_rtl->cmp =
		(int (*)(void *, void *))evpn_route_target_cmp;
	bgp->l2vnis = list_new();
	bgp->l2vnis->cmp = (int (*)(void *, void *))vni_hash_cmp;
	bf_assign_index(bm->rd_idspace, bgp->vrf_rd_id);
}

void bgp_evpn_vrf_delete(struct bgp *bgp_vrf)
{
	bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
}