summaryrefslogtreecommitdiffstats
path: root/bgpd/bgp_zebra.c
blob: d76eb951db3ff40b2f0989d3415460938547384d (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
/* zebra client
   Copyright (C) 1997, 98, 99 Kunihiro Ishiguro

This file is part of GNU Zebra.

GNU Zebra is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

GNU Zebra is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Zebra; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

#include <zebra.h>

#include "command.h"
#include "stream.h"
#include "network.h"
#include "prefix.h"
#include "log.h"
#include "sockunion.h"
#include "zclient.h"
#include "routemap.h"
#include "thread.h"
#include "queue.h"
#include "memory.h"
#include "lib/json.h"
#include "lib/bfd.h"
#include "filter.h"
#include "mpls.h"

#include "bgpd/bgpd.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_nexthop.h"
#include "bgpd/bgp_zebra.h"
#include "bgpd/bgp_fsm.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_mpath.h"
#include "bgpd/bgp_nexthop.h"
#include "bgpd/bgp_nht.h"
#include "bgpd/bgp_bfd.h"
#include "bgpd/bgp_label.h"
#if ENABLE_BGP_VNC
# include "bgpd/rfapi/rfapi_backend.h"
# include "bgpd/rfapi/vnc_export_bgp.h"
#endif

/* All information about zebra. */
struct zclient *zclient = NULL;

/* Growable buffer for nexthops sent to zebra */
struct stream *bgp_nexthop_buf = NULL;
struct stream *bgp_ifindices_buf = NULL;
struct stream *bgp_label_buf = NULL;

/* These array buffers are used in making a copy of the attributes for
   route-map apply. Arrays are being used here to minimize mallocs and
   frees for the temporary copy of the attributes.
   Given the zapi api expects the nexthop buffer to contain pointer to
   pointers for nexthops, we couldnt have used a single nexthop variable
   on the stack, hence we had two options:
     1. maintain a linked-list and free it after zapi_*_route call
     2. use an array to avoid number of mallocs.
   Number of supported next-hops are finite, use of arrays should be ok. */
struct attr attr_cp[MULTIPATH_NUM];
struct attr_extra attr_extra_cp[MULTIPATH_NUM];
unsigned int attr_index = 0;

/* Once per address-family initialization of the attribute array */
#define BGP_INFO_ATTR_BUF_INIT()\
do {\
  memset(attr_cp, 0, MULTIPATH_NUM * sizeof(struct attr));\
  memset(attr_extra_cp, 0, MULTIPATH_NUM * sizeof(struct attr_extra));\
  attr_index = 0;\
} while (0)

#define BGP_INFO_ATTR_BUF_COPY(info_src, info_dst)\
do { \
  *info_dst = *info_src; \
  assert(attr_index != multipath_num);\
  attr_cp[attr_index].extra = &attr_extra_cp[attr_index]; \
  bgp_attr_dup (&attr_cp[attr_index], info_src->attr); \
  bgp_attr_deep_dup (&attr_cp[attr_index], info_src->attr); \
  info_dst->attr = &attr_cp[attr_index]; \
  attr_index++;\
} while (0)

#define BGP_INFO_ATTR_BUF_FREE(info) \
do { \
  bgp_attr_deep_free(info->attr); \
} while (0)


/* Can we install into zebra? */
static inline int
bgp_install_info_to_zebra (struct bgp *bgp)
{
  if (zclient->sock <= 0)
    return 0;

  if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
    return 0;

  return 1;
}

int zclient_num_connects;

/* Router-id update message from zebra. */
static int
bgp_router_id_update (int command, struct zclient *zclient, zebra_size_t length,
    vrf_id_t vrf_id)
{
  struct prefix router_id;

  zebra_router_id_update_read(zclient->ibuf,&router_id);

  if (BGP_DEBUG (zebra, ZEBRA))
    {
      char buf[PREFIX2STR_BUFFER];
      prefix2str(&router_id, buf, sizeof(buf));
      zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id, buf);
    }

  bgp_router_id_zebra_bump (vrf_id, &router_id);
  return 0;
}

/* Nexthop update message from zebra. */
static int
bgp_read_nexthop_update (int command, struct zclient *zclient,
			 zebra_size_t length, vrf_id_t vrf_id)
{
  bgp_parse_nexthop_update(command, vrf_id);
  return 0;
}

static int
bgp_read_import_check_update(int command, struct zclient *zclient,
			     zebra_size_t length, vrf_id_t vrf_id)
{
  bgp_parse_nexthop_update(command, vrf_id);
  return 0;
}

/* Set or clear interface on which unnumbered neighbor is configured. This
 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
 * interface.
 */
static void
bgp_update_interface_nbrs (struct bgp *bgp, struct interface *ifp,
                           struct interface *upd_ifp)
{
  struct listnode *node, *nnode;
  struct peer *peer;

  for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
    {
      if (peer->conf_if &&
          (strcmp (peer->conf_if, ifp->name) == 0))
        {
          if (upd_ifp)
	    {
	      peer->ifp = upd_ifp;
	      bgp_zebra_initiate_radv (bgp, peer);
	    }
          else
	    {
	      bgp_zebra_terminate_radv (bgp, peer);
	      peer->ifp = upd_ifp;
	    }
        }
    }
}

static int
bgp_read_fec_update (int command, struct zclient *zclient,
                    zebra_size_t length)
{
  bgp_parse_fec_update();
  return 0;
}

static void
bgp_start_interface_nbrs (struct bgp *bgp, struct interface *ifp)
{
  struct listnode *node, *nnode;
  struct peer *peer;

  for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
    {
      if (peer->conf_if &&
          (strcmp (peer->conf_if, ifp->name) == 0) &&
          peer->status != Established)
        {
          if (peer_active(peer))
            BGP_EVENT_ADD (peer, BGP_Stop);
          BGP_EVENT_ADD (peer, BGP_Start);
        }
    }
}

static void
bgp_nbr_connected_add (struct bgp *bgp, struct nbr_connected *ifc)
{
  struct listnode *node;
  struct connected *connected;
  struct interface *ifp;
  struct prefix *p;

  /* Kick-off the FSM for any relevant peers only if there is a
   * valid local address on the interface.
   */
  ifp = ifc->ifp;
  for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
    {
      p = connected->address;
      if (p->family == AF_INET6 &&
          IN6_IS_ADDR_LINKLOCAL (&p->u.prefix6))
        break;
    }
  if (!connected)
    return;

  bgp_start_interface_nbrs (bgp, ifp);
}

static void
bgp_nbr_connected_delete (struct bgp *bgp, struct nbr_connected *ifc, int del)
{
  struct listnode *node, *nnode;
  struct peer *peer;
  struct interface *ifp;

  for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
    {
      if (peer->conf_if && (strcmp (peer->conf_if, ifc->ifp->name) == 0))
        {
          peer->last_reset = PEER_DOWN_NBR_ADDR_DEL;
          BGP_EVENT_ADD (peer, BGP_Stop);
        }
    }
  /* Free neighbor also, if we're asked to. */
  if (del)
    {
      ifp = ifc->ifp;
      listnode_delete (ifp->nbr_connected, ifc);
      nbr_connected_free (ifc);
    }
}

/* Inteface addition message from zebra. */
static int
bgp_interface_add (int command, struct zclient *zclient, zebra_size_t length,
    vrf_id_t vrf_id)
{
  struct interface *ifp;
  struct bgp *bgp;

  ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
  if (!ifp) // unexpected
    return 0;

  if (BGP_DEBUG (zebra, ZEBRA) && ifp)
    zlog_debug("Rx Intf add VRF %u IF %s", vrf_id, ifp->name);

  bgp = bgp_lookup_by_vrf_id (vrf_id);
  if (!bgp)
    return 0;

  bgp_update_interface_nbrs (bgp, ifp, ifp);
  return 0;
}

static int
bgp_interface_delete (int command, struct zclient *zclient,
		      zebra_size_t length, vrf_id_t vrf_id)
{
  struct stream *s;
  struct interface *ifp;
  struct bgp *bgp;

  s = zclient->ibuf;
  ifp = zebra_interface_state_read (s, vrf_id);
  if (!ifp) /* This may happen if we've just unregistered for a VRF. */
    return 0;

  ifp->ifindex = IFINDEX_DELETED;

  if (BGP_DEBUG (zebra, ZEBRA))
    zlog_debug("Rx Intf del VRF %u IF %s", vrf_id, ifp->name);

  bgp = bgp_lookup_by_vrf_id (vrf_id);
  if (!bgp)
    return 0;

  bgp_update_interface_nbrs (bgp, ifp, NULL);
  return 0;
}

static int
bgp_interface_up (int command, struct zclient *zclient, zebra_size_t length,
    vrf_id_t vrf_id)
{
  struct stream *s;
  struct interface *ifp;
  struct connected *c;
  struct nbr_connected *nc;
  struct listnode *node, *nnode;
  struct bgp *bgp;

  s = zclient->ibuf;
  ifp = zebra_interface_state_read (s, vrf_id);

  if (! ifp)
    return 0;

  if (BGP_DEBUG (zebra, ZEBRA))
    zlog_debug("Rx Intf up VRF %u IF %s", vrf_id, ifp->name);

  bgp = bgp_lookup_by_vrf_id (vrf_id);
  if (!bgp)
    return 0;

  for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
    bgp_connected_add (bgp, c);

  for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
    bgp_nbr_connected_add (bgp, nc);

  return 0;
}

static int
bgp_interface_down (int command, struct zclient *zclient, zebra_size_t length,
    vrf_id_t vrf_id)
{
  struct stream *s;
  struct interface *ifp;
  struct connected *c;
  struct nbr_connected *nc;
  struct listnode *node, *nnode;
  struct bgp *bgp;

  s = zclient->ibuf;
  ifp = zebra_interface_state_read (s, vrf_id);
  if (! ifp)
    return 0;

  if (BGP_DEBUG (zebra, ZEBRA))
    zlog_debug("Rx Intf down VRF %u IF %s", vrf_id, ifp->name);

  bgp = bgp_lookup_by_vrf_id (vrf_id);
  if (!bgp)
    return 0;

  for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
    bgp_connected_delete (bgp, c);

  for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
    bgp_nbr_connected_delete (bgp, nc, 1);

  /* Fast external-failover */
  {
    struct peer *peer;

    if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
      return 0;

    for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
      {
#if defined(HAVE_CUMULUS)
        /* Take down directly connected EBGP peers as well as 1-hop BFD
         * tracked (directly connected) IBGP peers.
         */
        if ((peer->ttl != 1) && (peer->gtsm_hops != 1) &&
            (!peer->bfd_info || bgp_bfd_is_peer_multihop(peer)))
#else
        /* Take down directly connected EBGP peers */
        if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
#endif
          continue;

        if (ifp == peer->nexthop.ifp)
          {
            BGP_EVENT_ADD (peer, BGP_Stop);
            peer->last_reset = PEER_DOWN_IF_DOWN;
          }
      }
  }

  return 0;
}

static int
bgp_interface_address_add (int command, struct zclient *zclient,
			   zebra_size_t length, vrf_id_t vrf_id)
{
  struct connected *ifc;

  ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);

  if (ifc == NULL)
    return 0;

  if (bgp_debug_zebra(ifc->address))
    {
      char buf[PREFIX2STR_BUFFER];
      prefix2str(ifc->address, buf, sizeof(buf));
      zlog_debug("Rx Intf address add VRF %u IF %s addr %s",
                 vrf_id, ifc->ifp->name, buf);
    }

  if (if_is_operative (ifc->ifp))
    {
      struct bgp *bgp;

      bgp = bgp_lookup_by_vrf_id (vrf_id);
      if (!bgp)
        return 0;

      bgp_connected_add (bgp, ifc);
      /* If we have learnt of any neighbors on this interface,
       * check to kick off any BGP interface-based neighbors,
       * but only if this is a link-local address.
       */
      if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6) &&
          !list_isempty(ifc->ifp->nbr_connected))
        bgp_start_interface_nbrs (bgp, ifc->ifp);
    }

  return 0;
}

static int
bgp_interface_address_delete (int command, struct zclient *zclient,
			      zebra_size_t length, vrf_id_t vrf_id)
{
  struct connected *ifc;
  struct bgp *bgp;

  ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);

  if (ifc == NULL)
    return 0;

  if (bgp_debug_zebra(ifc->address))
    {
      char buf[PREFIX2STR_BUFFER];
      prefix2str(ifc->address, buf, sizeof(buf));
      zlog_debug("Rx Intf address del VRF %u IF %s addr %s",
                 vrf_id, ifc->ifp->name, buf);
    }

  if (if_is_operative (ifc->ifp))
    {
      bgp = bgp_lookup_by_vrf_id (vrf_id);
      if (bgp)
        bgp_connected_delete (bgp, ifc);
    }

  connected_free (ifc);

  return 0;
}

static int
bgp_interface_nbr_address_add (int command, struct zclient *zclient,
			   zebra_size_t length, vrf_id_t vrf_id)
{
  struct nbr_connected *ifc = NULL;
  struct bgp *bgp;

  ifc = zebra_interface_nbr_address_read (command, zclient->ibuf, vrf_id);

  if (ifc == NULL)
    return 0;

  if (bgp_debug_zebra(ifc->address))
    {
      char buf[PREFIX2STR_BUFFER];
      prefix2str(ifc->address, buf, sizeof(buf));
      zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s",
                 vrf_id, ifc->ifp->name, buf);
    }

  if (if_is_operative (ifc->ifp))
    {
      bgp = bgp_lookup_by_vrf_id (vrf_id);
      if (bgp)
        bgp_nbr_connected_add (bgp, ifc);
    }

  return 0;
}

static int
bgp_interface_nbr_address_delete (int command, struct zclient *zclient,
			      zebra_size_t length, vrf_id_t vrf_id)
{
  struct nbr_connected *ifc = NULL;
  struct bgp *bgp;

  ifc = zebra_interface_nbr_address_read (command, zclient->ibuf, vrf_id);

  if (ifc == NULL)
    return 0;

  if (bgp_debug_zebra(ifc->address))
    {
      char buf[PREFIX2STR_BUFFER];
      prefix2str(ifc->address, buf, sizeof(buf));
      zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s",
                 vrf_id, ifc->ifp->name, buf);
    }

  if (if_is_operative (ifc->ifp))
    {
      bgp = bgp_lookup_by_vrf_id (vrf_id);
      if (bgp)
        bgp_nbr_connected_delete (bgp, ifc, 0);
    }

  nbr_connected_free (ifc);

  return 0;
}

/* VRF update for an interface. */
static int
bgp_interface_vrf_update (int command, struct zclient *zclient, zebra_size_t length,
    vrf_id_t vrf_id)
{
  struct interface *ifp;
  vrf_id_t new_vrf_id;
  struct connected *c;
  struct nbr_connected *nc;
  struct listnode *node, *nnode;
  struct bgp *bgp;

  ifp = zebra_interface_vrf_update_read (zclient->ibuf, vrf_id, &new_vrf_id);
  if (! ifp)
    return 0;

  if (BGP_DEBUG (zebra, ZEBRA) && ifp)
    zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u",
               vrf_id, ifp->name, new_vrf_id);

  bgp = bgp_lookup_by_vrf_id (vrf_id);
  if (!bgp)
    return 0;

  for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
    bgp_connected_delete (bgp, c);

  for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
    bgp_nbr_connected_delete (bgp, nc, 1);

  /* Fast external-failover */
  {
    struct peer *peer;

    if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
      return 0;

    for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
      {
        if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
          continue;

        if (ifp == peer->nexthop.ifp)
          BGP_EVENT_ADD (peer, BGP_Stop);
      }
  }

  if_update (ifp, ifp->name, strlen (ifp->name), new_vrf_id);

  bgp = bgp_lookup_by_vrf_id (new_vrf_id);
  if (!bgp)
    return 0;

  for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
    bgp_connected_add (bgp, c);

  for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
    bgp_nbr_connected_add (bgp, nc);
  return 0;
}

/* Zebra route add and delete treatment. */
static int
zebra_read_ipv4 (int command, struct zclient *zclient, zebra_size_t length,
    vrf_id_t vrf_id)
{
  struct stream *s;
  struct zapi_ipv4 api;
  struct in_addr nexthop;
  struct prefix_ipv4 p;
  unsigned int ifindex;
  int i;
  struct bgp *bgp;

  bgp = bgp_lookup_by_vrf_id (vrf_id);
  if (!bgp)
    return 0;

  s = zclient->ibuf;
  nexthop.s_addr = 0;

  /* Type, flags, message. */
  api.type = stream_getc (s);
  api.instance = stream_getw (s);
  api.flags = stream_getl (s);
  api.message = stream_getc (s);

  /* IPv4 prefix. */
  memset (&p, 0, sizeof (struct prefix_ipv4));
  p.family = AF_INET;
  p.prefixlen = MIN(IPV4_MAX_PREFIXLEN, stream_getc (s));
  stream_get (&p.prefix, s, PSIZE (p.prefixlen));

  /* Nexthop, ifindex, distance, metric. */
  if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
    {
      api.nexthop_num = stream_getc (s);
      nexthop.s_addr = stream_get_ipv4 (s);
    }

  if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
    {
      api.ifindex_num = stream_getc (s);
      ifindex = stream_getl (s); /* ifindex, unused */
    }
  else
    {
      ifindex = 0;
    }

  if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
    api.distance = stream_getc (s);

  if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
    api.metric = stream_getl (s);
  else
    api.metric = 0;

  if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
    api.tag = stream_getl (s);
  else
    api.tag = 0;

  if (command == ZEBRA_REDISTRIBUTE_IPV4_ADD)
    {
      if (bgp_debug_zebra((struct prefix *)&p))
	{
	  char buf[2][INET_ADDRSTRLEN];
	  zlog_debug("Rx IPv4 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %"ROUTE_TAG_PRI,
                     vrf_id,
		     zebra_route_string(api.type), api.instance,
		     inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
		     p.prefixlen,
		     inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
		     api.metric,
		     api.tag);
	}

      /*
       * The ADD message is actually an UPDATE and there is no explicit DEL
       * for a prior redistributed route, if any. So, perform an implicit
       * DEL processing for the same redistributed route from any other
       * source type.
       */
      for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
        {
          if (i != api.type)
            bgp_redistribute_delete(bgp, (struct prefix *)&p, i, api.instance);
        }

      /* Now perform the add/update. */
      bgp_redistribute_add(bgp, (struct prefix *)&p, &nexthop, NULL, ifindex,
			   api.metric, api.type, api.instance, api.tag);
    }
  else if (command == ZEBRA_REDISTRIBUTE_IPV4_DEL)
    {
      if (bgp_debug_zebra((struct prefix *)&p))
	{
	  char buf[2][INET_ADDRSTRLEN];
	  zlog_debug("Rx IPv4 route delete VRF %u %s[%d] %s/%d "
		     "nexthop %s metric %u tag %"ROUTE_TAG_PRI,
                     vrf_id,
		     zebra_route_string(api.type), api.instance,
		     inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
		     p.prefixlen,
		     inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
		     api.metric,
		     api.tag);
	}
      bgp_redistribute_delete(bgp, (struct prefix *)&p, api.type, api.instance);
    }

  return 0;
}

/* Zebra route add and delete treatment. */
static int
zebra_read_ipv6 (int command, struct zclient *zclient, zebra_size_t length,
    vrf_id_t vrf_id)
{
  struct stream *s;
  struct zapi_ipv6 api;
  struct in6_addr nexthop;
  struct prefix_ipv6 p, src_p;
  unsigned int ifindex;
  int i;
  struct bgp *bgp;

  bgp = bgp_lookup_by_vrf_id (vrf_id);
  if (!bgp)
    return 0;

  s = zclient->ibuf;
  memset (&nexthop, 0, sizeof (struct in6_addr));

  /* Type, flags, message. */
  api.type = stream_getc (s);
  api.instance = stream_getw (s);
  api.flags = stream_getl (s);
  api.message = stream_getc (s);

  /* IPv6 prefix. */
  memset (&p, 0, sizeof (struct prefix_ipv6));
  p.family = AF_INET6;
  p.prefixlen = MIN(IPV6_MAX_PREFIXLEN, stream_getc (s));
  stream_get (&p.prefix, s, PSIZE (p.prefixlen));

  memset (&src_p, 0, sizeof (struct prefix_ipv6));
  src_p.family = AF_INET6;
  if (CHECK_FLAG (api.message, ZAPI_MESSAGE_SRCPFX))
    {
      src_p.prefixlen = stream_getc (s);
      stream_get (&src_p.prefix, s, PSIZE (src_p.prefixlen));
    }

  if (src_p.prefixlen)
    /* we completely ignore srcdest routes for now. */
    return 0;

  /* Nexthop, ifindex, distance, metric. */
  if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
    {
      api.nexthop_num = stream_getc (s);
      stream_get (&nexthop, s, 16);
    }

  if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
    {
      api.ifindex_num = stream_getc (s);
      ifindex = stream_getl (s); /* ifindex, unused */
    }
  else
    {
      ifindex = 0;
    }

  if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
    api.distance = stream_getc (s);
  else
    api.distance = 0;

  if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
    api.metric = stream_getl (s);
  else
    api.metric = 0;

  if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
    api.tag = stream_getl (s);
  else
    api.tag = 0;

  /* Simply ignore link-local address. */
  if (IN6_IS_ADDR_LINKLOCAL (&p.prefix))
    return 0;

  if (command == ZEBRA_REDISTRIBUTE_IPV6_ADD)
    {
      if (bgp_debug_zebra((struct prefix *)&p))
	{
	  char buf[2][INET6_ADDRSTRLEN];
	  zlog_debug("Rx IPv6 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %"ROUTE_TAG_PRI,
                     vrf_id,
		     zebra_route_string(api.type), api.instance,
		     inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
		     p.prefixlen,
		     inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
		     api.metric,
		     api.tag);
	}

      /*
       * The ADD message is actually an UPDATE and there is no explicit DEL
       * for a prior redistributed route, if any. So, perform an implicit
       * DEL processing for the same redistributed route from any other
       * source type.
       */
      for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
        {
          if (i != api.type)
            bgp_redistribute_delete(bgp, (struct prefix *)&p, i, api.instance);
        }

      bgp_redistribute_add (bgp, (struct prefix *)&p, NULL, &nexthop, ifindex,
			    api.metric, api.type, api.instance, api.tag);
    }
  else if (command == ZEBRA_REDISTRIBUTE_IPV6_DEL)
    {
      if (bgp_debug_zebra((struct prefix *)&p))
	{
	  char buf[2][INET6_ADDRSTRLEN];
	  zlog_debug("Rx IPv6 route delete VRF %u %s[%d] %s/%d "
		     "nexthop %s metric %u tag %"ROUTE_TAG_PRI,
                     vrf_id,
		     zebra_route_string(api.type), api.instance,
		     inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
		     p.prefixlen,
		     inet_ntop(AF_INET6, &nexthop, buf[1], sizeof(buf[1])),
		     api.metric,
                     api.tag);
	}
      bgp_redistribute_delete (bgp, (struct prefix *) &p, api.type, api.instance);
    }
  
  return 0;
}

struct interface *
if_lookup_by_ipv4 (struct in_addr *addr, vrf_id_t vrf_id)
{
  struct listnode *ifnode;
  struct listnode *cnode;
  struct interface *ifp;
  struct connected *connected;
  struct prefix_ipv4 p;
  struct prefix *cp; 
  
  p.family = AF_INET;
  p.prefix = *addr;
  p.prefixlen = IPV4_MAX_BITLEN;

  for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
    {
      for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
	{
	  cp = connected->address;
	    
	  if (cp->family == AF_INET)
	    if (prefix_match (cp, (struct prefix *)&p))
	      return ifp;
	}
    }
  return NULL;
}

struct interface *
if_lookup_by_ipv4_exact (struct in_addr *addr, vrf_id_t vrf_id)
{
  struct listnode *ifnode;
  struct listnode *cnode;
  struct interface *ifp;
  struct connected *connected;
  struct prefix *cp; 
  
  for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
    {
      for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
	{
	  cp = connected->address;
	    
	  if (cp->family == AF_INET)
	    if (IPV4_ADDR_SAME (&cp->u.prefix4, addr))
	      return ifp;
	}
    }
  return NULL;
}

struct interface *
if_lookup_by_ipv6 (struct in6_addr *addr, ifindex_t ifindex, vrf_id_t vrf_id)
{
  struct listnode *ifnode;
  struct listnode *cnode;
  struct interface *ifp;
  struct connected *connected;
  struct prefix_ipv6 p;
  struct prefix *cp; 
  
  p.family = AF_INET6;
  p.prefix = *addr;
  p.prefixlen = IPV6_MAX_BITLEN;

  for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
    {
      for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
	{
	  cp = connected->address;
	    
	  if (cp->family == AF_INET6)
	    if (prefix_match (cp, (struct prefix *)&p))
	      {
		if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6))
		  {
		    if (ifindex == ifp->ifindex)
		      return ifp;
		  }
		else
		  return ifp;
	      }
	}
    }
  return NULL;
}

struct interface *
if_lookup_by_ipv6_exact (struct in6_addr *addr, ifindex_t ifindex, vrf_id_t vrf_id)
{
  struct listnode *ifnode;
  struct listnode *cnode;
  struct interface *ifp;
  struct connected *connected;
  struct prefix *cp; 

  for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
    {
      for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
	{
	  cp = connected->address;
	    
	  if (cp->family == AF_INET6)
	    if (IPV6_ADDR_SAME (&cp->u.prefix6, addr))
	      {
		if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6))
		  {
		    if (ifindex == ifp->ifindex)
		      return ifp;
		  }
		else
		  return ifp;
	      }
	}
    }
  return NULL;
}

static int
if_get_ipv6_global (struct interface *ifp, struct in6_addr *addr)
{
  struct listnode *cnode;
  struct connected *connected;
  struct prefix *cp; 
  
  for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
    {
      cp = connected->address;
	    
      if (cp->family == AF_INET6)
	if (! IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
	  {
	    memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
	    return 1;
	  }
    }
  return 0;
}

static int
if_get_ipv6_local (struct interface *ifp, struct in6_addr *addr)
{
  struct listnode *cnode;
  struct connected *connected;
  struct prefix *cp; 
  
  for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
    {
      cp = connected->address;
	    
      if (cp->family == AF_INET6)
	if (IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
	  {
	    memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
	    return 1;
	  }
    }
  return 0;
}

static int
if_get_ipv4_address (struct interface *ifp, struct in_addr *addr)
{
  struct listnode *cnode;
  struct connected *connected;
  struct prefix *cp;

  for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
    {
      cp = connected->address;
      if ((cp->family == AF_INET) && !ipv4_martian(&(cp->u.prefix4)))
	  {
	    *addr = cp->u.prefix4;
	    return 1;
	  }
    }
  return 0;
}

int
bgp_nexthop_set (union sockunion *local, union sockunion *remote, 
		 struct bgp_nexthop *nexthop, struct peer *peer)
{
  int ret = 0;
  struct interface *ifp = NULL;

  memset (nexthop, 0, sizeof (struct bgp_nexthop));

  if (!local)
    return -1;
  if (!remote)
    return -1;

  if (local->sa.sa_family == AF_INET)
    {
      nexthop->v4 = local->sin.sin_addr;
      if (peer->update_if)
        ifp = if_lookup_by_name (peer->update_if, peer->bgp->vrf_id);
      else
        ifp = if_lookup_by_ipv4_exact (&local->sin.sin_addr, peer->bgp->vrf_id);
    }
  if (local->sa.sa_family == AF_INET6)
    {
      if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
	{
	  if (peer->conf_if || peer->ifname)
	    ifp = if_lookup_by_name (peer->conf_if ? peer->conf_if : peer->ifname, peer->bgp->vrf_id);
	}
      else if (peer->update_if)
        ifp = if_lookup_by_name (peer->update_if, peer->bgp->vrf_id);
      else
        ifp = if_lookup_by_ipv6_exact (&local->sin6.sin6_addr,
				       local->sin6.sin6_scope_id,
                                       peer->bgp->vrf_id);
    }

  if (!ifp)
    return -1;

  nexthop->ifp = ifp;

  /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
  if (local->sa.sa_family == AF_INET)
    {
      /* IPv6 nexthop*/
      ret = if_get_ipv6_global (ifp, &nexthop->v6_global);

      if (!ret)
        {
          /* There is no global nexthop. Use link-local address as both the
           * global and link-local nexthop. In this scenario, the expectation
           * for interop is that the network admin would use a route-map to
           * specify the global IPv6 nexthop.
           */
          if_get_ipv6_local (ifp, &nexthop->v6_global);
          memcpy (&nexthop->v6_local, &nexthop->v6_global,
                  IPV6_MAX_BYTELEN);
        }
      else
	if_get_ipv6_local (ifp, &nexthop->v6_local);

      if (if_lookup_by_ipv4 (&remote->sin.sin_addr, peer->bgp->vrf_id))
        peer->shared_network = 1;
      else
        peer->shared_network = 0;
    }

  /* IPv6 connection, fetch and store IPv4 local address if any. */
  if (local->sa.sa_family == AF_INET6)
    {
      struct interface *direct = NULL;

      /* IPv4 nexthop. */
      ret = if_get_ipv4_address(ifp, &nexthop->v4);
      if (!ret && peer->local_id.s_addr)
	nexthop->v4 = peer->local_id;

      /* Global address*/
      if (! IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
	{
	  memcpy (&nexthop->v6_global, &local->sin6.sin6_addr, 
		  IPV6_MAX_BYTELEN);

	  /* If directory connected set link-local address. */
	  direct = if_lookup_by_ipv6 (&remote->sin6.sin6_addr,
				      remote->sin6.sin6_scope_id, peer->bgp->vrf_id);
	  if (direct)
	    if_get_ipv6_local (ifp, &nexthop->v6_local);
	}
      else
	/* Link-local address. */
	{
	  ret = if_get_ipv6_global (ifp, &nexthop->v6_global);

	  /* If there is no global address.  Set link-local address as
             global.  I know this break RFC specification... */
          /* In this scenario, the expectation for interop is that the
           * network admin would use a route-map to specify the global
           * IPv6 nexthop.
           */
	  if (!ret)
	    memcpy (&nexthop->v6_global, &local->sin6.sin6_addr, 
		    IPV6_MAX_BYTELEN);
          /* Always set the link-local address */
          memcpy (&nexthop->v6_local, &local->sin6.sin6_addr,
                  IPV6_MAX_BYTELEN);
	}

      if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr) ||
          if_lookup_by_ipv6 (&remote->sin6.sin6_addr, remote->sin6.sin6_scope_id,
                             peer->bgp->vrf_id))
        peer->shared_network = 1;
      else
        peer->shared_network = 0;
    }

  /* KAME stack specific treatment.  */
#ifdef KAME
  if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_global)
      && IN6_LINKLOCAL_IFINDEX (nexthop->v6_global))
    {
      SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_global, 0);
    }
  if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_local)
      && IN6_LINKLOCAL_IFINDEX (nexthop->v6_local))
    {
      SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_local, 0);
    }
#endif /* KAME */

  /* If we have identified the local interface, there is no error for now. */
  return 0;
}

static struct in6_addr *
bgp_info_to_ipv6_nexthop (struct bgp_info *info)
{
  struct in6_addr *nexthop = NULL;

  /* Only global address nexthop exists. */
  if (info->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL)
    nexthop = &info->attr->extra->mp_nexthop_global;

  /* If both global and link-local address present. */
  if (info->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
    {
      /* Check if route-map is set to prefer global over link-local */
      if (info->attr->extra->mp_nexthop_prefer_global)
        nexthop = &info->attr->extra->mp_nexthop_global;
      else
        {
          /* Workaround for Cisco's nexthop bug.  */
          if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->extra->mp_nexthop_global)
              && info->peer->su_remote->sa.sa_family == AF_INET6)
            nexthop = &info->peer->su_remote->sin6.sin6_addr;
          else
            nexthop = &info->attr->extra->mp_nexthop_local;
        }
    }

  return nexthop;
}

static int
bgp_table_map_apply (struct route_map *map, struct prefix *p,
                     struct bgp_info *info)
{
  if (route_map_apply(map, p, RMAP_BGP, info) != RMAP_DENYMATCH)
    return 1;

  if (bgp_debug_zebra(p))
    {
      if (p->family == AF_INET)
        {
          char buf[2][INET_ADDRSTRLEN];
          zlog_debug("Zebra rmap deny: IPv4 route %s/%d nexthop %s",
                     inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
                     p->prefixlen,
                     inet_ntop(AF_INET, &info->attr->nexthop, buf[1],
                               sizeof(buf[1])));
        }
      if (p->family == AF_INET6)
        {
          char buf[2][INET6_ADDRSTRLEN];
          zlog_debug("Zebra rmap deny: IPv6 route %s/%d nexthop %s",
                     inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
                     p->prefixlen,
                     inet_ntop(AF_INET6, bgp_info_to_ipv6_nexthop(info), buf[1],
                               sizeof(buf[1])));
        }
    }
  return 0;
}

void
bgp_zebra_announce (struct bgp_node *rn, struct prefix *p, struct bgp_info *info,
                    struct bgp *bgp, afi_t afi, safi_t safi)
{
  u_int32_t flags;
  u_char distance;
  struct peer *peer;
  struct bgp_info *mpinfo;
  size_t oldsize, newsize;
  u_int32_t nhcount, metric;
  struct bgp_info local_info;
  struct bgp_info *info_cp = &local_info;
  route_tag_t tag;
  u_int32_t label;

  /* Don't try to install if we're not connected to Zebra or Zebra doesn't
   * know of this instance.
   */
  if (!bgp_install_info_to_zebra (bgp))
    return;

  if ((p->family == AF_INET &&
       !vrf_bitmap_check (zclient->redist[AFI_IP][ZEBRA_ROUTE_BGP], bgp->vrf_id))
      || (p->family == AF_INET6 &&
       !vrf_bitmap_check (zclient->redist[AFI_IP6][ZEBRA_ROUTE_BGP], bgp->vrf_id)))
    return;

  if (bgp->main_zebra_update_hold)
    return;

  flags = 0;
  peer = info->peer;

  if ((info->attr->extra) && (info->attr->extra->tag != 0))
    tag = info->attr->extra->tag;
  else
    tag = 0;

  /* When we create an aggregate route we must also install a Null0 route in
   * the RIB */
  if (info->sub_type == BGP_ROUTE_AGGREGATE)
      SET_FLAG (flags, ZEBRA_FLAG_BLACKHOLE);

  if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED ||
      info->sub_type == BGP_ROUTE_AGGREGATE)
    {
      SET_FLAG (flags, ZEBRA_FLAG_IBGP);
      SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
    }

  if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
      || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
      || bgp_flag_check(bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))

    SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);

  nhcount = 1 + bgp_info_mpath_count (info);

  if (p->family == AF_INET && !BGP_ATTR_NEXTHOP_AFI_IP6(info->attr))
    {
      struct zapi_ipv4 api;
      struct in_addr *nexthop;
      char buf[2][INET_ADDRSTRLEN];
      int valid_nh_count = 0;

      /* resize nexthop buffer size if necessary */
      if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
          (sizeof (struct in_addr *) * nhcount))
        {
          newsize = sizeof (struct in_addr *) * nhcount;
          newsize = stream_resize (bgp_nexthop_buf, newsize);
          if (newsize == oldsize)
            {
	          zlog_err ("can't resize nexthop buffer");
	          return;
            }
        }
      stream_reset (bgp_nexthop_buf);
      nexthop = NULL;

      /* For labeled unicast, each nexthop has a label too. Resize label
       * buffer, if required.
       */
      if (safi == SAFI_LABELED_UNICAST)
        {
          if ((oldsize = stream_get_size (bgp_label_buf)) <
              (sizeof (unsigned int) * nhcount))
            {
              newsize = (sizeof (unsigned int) * nhcount);
              newsize = stream_resize (bgp_label_buf, newsize);
              if (newsize == oldsize)
                {
                  zlog_err ("can't resize label buffer");
                  return;
                }
            }
          stream_reset (bgp_label_buf);
        }

      /* Metric is currently based on the best-path only. */
      metric = info->attr->med;

      if (bgp->table_map[afi][safi].name)
        {
          BGP_INFO_ATTR_BUF_INIT();

          /* Copy info and attributes, so the route-map apply doesn't modify the
             BGP route info. */
          BGP_INFO_ATTR_BUF_COPY(info, info_cp);
          if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
            {
              metric = info_cp->attr->med;
              nexthop = &info_cp->attr->nexthop;

              if (info_cp->attr->extra)
                tag = info_cp->attr->extra->tag;
            }
          BGP_INFO_ATTR_BUF_FREE(info_cp);
        }
      else
        {
          nexthop = &info->attr->nexthop;
        }

      if (nexthop)
        {
          stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
          valid_nh_count++;
          if (safi == SAFI_LABELED_UNICAST)
            {
              label = label_pton(info->extra->tag);
              stream_put (bgp_label_buf, &label, sizeof (u_int32_t));
            }
        }

      for (mpinfo = bgp_info_mpath_first (info); mpinfo;
           mpinfo = bgp_info_mpath_next (mpinfo))
        {
          nexthop = NULL;

          if (bgp->table_map[afi][safi].name)
            {
              /* Copy info and attributes, so the route-map apply doesn't modify the
                 BGP route info. */
              BGP_INFO_ATTR_BUF_COPY(mpinfo, info_cp);
              if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
                nexthop = &info_cp->attr->nexthop;
              BGP_INFO_ATTR_BUF_FREE(info_cp);
            }
          else
            {
              nexthop = &mpinfo->attr->nexthop;
            }

          if (nexthop == NULL)
            continue;

          stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
          if (safi == SAFI_LABELED_UNICAST)
            {
              label = label_pton(mpinfo->extra->tag);
              stream_put (bgp_label_buf, &label, sizeof (u_int32_t));
            }
          valid_nh_count++;
        }

      api.vrf_id = bgp->vrf_id;
      api.flags = flags;
      api.type = ZEBRA_ROUTE_BGP;
      api.instance = 0;
      api.message = 0;
      api.safi = (safi == SAFI_LABELED_UNICAST) ? SAFI_UNICAST : safi;
      SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
      if (safi == SAFI_LABELED_UNICAST)
        SET_FLAG (api.message, ZAPI_MESSAGE_LABEL);

      /* Note that this currently only applies to Null0 routes for aggregates.
       * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv4_route to encode a special
       * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
       * do not want to also encode the 0.0.0.0 nexthop for the aggregate route.
       */
      if (CHECK_FLAG(flags, ZEBRA_FLAG_BLACKHOLE))
        api.nexthop_num = 0;
      else
        api.nexthop_num = valid_nh_count;

      api.nexthop = (struct in_addr **)STREAM_DATA (bgp_nexthop_buf);
      if (safi == SAFI_LABELED_UNICAST)
        {
          api.label_num = valid_nh_count;
          api.label = (unsigned int *)STREAM_DATA (bgp_label_buf);
        }
      else
        {
          api.label_num = 0;
          api.label = NULL;
        }
      api.ifindex_num = 0;
      SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
      api.metric = metric;
      api.tag = 0;

      if (tag)
        {
          SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
          api.tag = tag;
        }

      distance = bgp_distance_apply (p, info, afi, safi, bgp);
      if (distance)
	{
	  SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
	  api.distance = distance;
	}

      if (bgp_debug_zebra(p))
        {
          int i;
          char label_buf[20];
          zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI
                     " count %d", (valid_nh_count ? "add":"delete"),
                     bgp->vrf_id,
                     inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
                     p->prefixlen, api.metric, api.tag, api.nexthop_num);
          for (i = 0; i < api.nexthop_num; i++)
            {
              label_buf[0] = '\0';
              if (safi == SAFI_LABELED_UNICAST)
                sprintf(label_buf, "label %u", api.label[i]);
              zlog_debug("  nhop [%d]: %s %s",
                         i+1,
                         inet_ntop(AF_INET, api.nexthop[i], buf[1], sizeof(buf[1])),
                         label_buf);
            }
        }

      zapi_ipv4_route (valid_nh_count ? ZEBRA_IPV4_ROUTE_ADD: ZEBRA_IPV4_ROUTE_DELETE,
                       zclient, (struct prefix_ipv4 *) p, &api);
    }

  /* We have to think about a IPv6 link-local address curse. */
  if (p->family == AF_INET6 ||
      (p->family == AF_INET && BGP_ATTR_NEXTHOP_AFI_IP6(info->attr)))
    {
      ifindex_t ifindex;
      struct in6_addr *nexthop;
      struct zapi_ipv6 api;
      int valid_nh_count = 0;
	    char buf[2][INET6_ADDRSTRLEN];

      /* resize nexthop buffer size if necessary */
      if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
          (sizeof (struct in6_addr *) * nhcount))
        {
          newsize = (sizeof (struct in6_addr *) * nhcount);
          newsize = stream_resize (bgp_nexthop_buf, newsize);
          if (newsize == oldsize)
            {
              zlog_err ("can't resize nexthop buffer");
              return;
            }
        }
      stream_reset (bgp_nexthop_buf);

      /* resize ifindices buffer size if necessary */
      if ((oldsize = stream_get_size (bgp_ifindices_buf)) <
          (sizeof (unsigned int) * nhcount))
        {
          newsize = (sizeof (unsigned int) * nhcount);
          newsize = stream_resize (bgp_ifindices_buf, newsize);
          if (newsize == oldsize)
            {
              zlog_err ("can't resize nexthop buffer");
              return;
            }
        }
      stream_reset (bgp_ifindices_buf);

      /* For labeled unicast, each nexthop has a label too. Resize label
       * buffer, if required.
       */
      if (safi == SAFI_LABELED_UNICAST)
        {
          if ((oldsize = stream_get_size (bgp_label_buf)) <
              (sizeof (unsigned int) * nhcount))
            {
              newsize = (sizeof (unsigned int) * nhcount);
              newsize = stream_resize (bgp_label_buf, newsize);
              if (newsize == oldsize)
                {
                  zlog_err ("can't resize label buffer");
                  return;
                }
            }
          stream_reset (bgp_label_buf);
        }

      ifindex = 0;
      nexthop = NULL;

      assert (info->attr->extra);

      /* Metric is currently based on the best-path only. */
      metric = info->attr->med;

      if (bgp->table_map[afi][safi].name)
        {
          BGP_INFO_ATTR_BUF_INIT();

          /* Copy info and attributes, so the route-map apply doesn't modify the
             BGP route info. */
          BGP_INFO_ATTR_BUF_COPY(info, info_cp);
          if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
            {
              metric = info_cp->attr->med;
              nexthop = bgp_info_to_ipv6_nexthop(info_cp);

              if (info_cp->attr->extra)
                tag = info_cp->attr->extra->tag;
            }
          BGP_INFO_ATTR_BUF_FREE(info_cp);
        }
      else
        {
           nexthop = bgp_info_to_ipv6_nexthop(info);
        }

      if (nexthop)
        {
          if (info->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
            if (info->peer->nexthop.ifp)
              ifindex = info->peer->nexthop.ifp->ifindex;

          if (!ifindex)
	    {
	      if (info->peer->conf_if || info->peer->ifname)
		ifindex = if_nametoindex (info->peer->conf_if ? info->peer->conf_if : info->peer->ifname);
	      else if (info->peer->nexthop.ifp)
		ifindex = info->peer->nexthop.ifp->ifindex;
	    }
          stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
          stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
          if (safi == SAFI_LABELED_UNICAST)
            {
              label = label_pton(info->extra->tag);
              stream_put (bgp_label_buf, &label, sizeof (u_int32_t));
            }
          valid_nh_count++;
        }

      for (mpinfo = bgp_info_mpath_first (info); mpinfo;
           mpinfo = bgp_info_mpath_next (mpinfo))
        {
          ifindex = 0;
          nexthop = NULL;

          if (bgp->table_map[afi][safi].name)
            {
              /* Copy info and attributes, so the route-map apply doesn't modify the
                 BGP route info. */
              BGP_INFO_ATTR_BUF_COPY(mpinfo, info_cp);
              if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
                nexthop = bgp_info_to_ipv6_nexthop(info_cp);
              BGP_INFO_ATTR_BUF_FREE(info_cp);
            }
          else
            {
              nexthop = bgp_info_to_ipv6_nexthop(mpinfo);
            }

          if (nexthop == NULL)
            continue;

          if (mpinfo->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
            if (mpinfo->peer->nexthop.ifp)
              ifindex = mpinfo->peer->nexthop.ifp->ifindex;

          if (!ifindex)
	    {
	      if (mpinfo->peer->conf_if || mpinfo->peer->ifname)
		ifindex = ifname2ifindex (mpinfo->peer->conf_if ? mpinfo->peer->conf_if : mpinfo->peer->ifname, bgp->vrf_id);
	      else if (mpinfo->peer->nexthop.ifp)
		ifindex = mpinfo->peer->nexthop.ifp->ifindex;
	    }
          if (ifindex == 0)
            continue;

          stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
          stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
          if (safi == SAFI_LABELED_UNICAST)
            {
              label = label_pton(mpinfo->extra->tag);
              stream_put (bgp_label_buf, &label, sizeof (u_int32_t));
            }
          valid_nh_count++;
        }

      /* Make Zebra API structure. */
      api.vrf_id = bgp->vrf_id;
      api.flags = flags;
      api.type = ZEBRA_ROUTE_BGP;
      api.instance = 0;
      api.message = 0;
      api.safi = (safi == SAFI_LABELED_UNICAST) ? SAFI_UNICAST : safi;
      SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
      if (safi == SAFI_LABELED_UNICAST)
        SET_FLAG (api.message, ZAPI_MESSAGE_LABEL);

      /* Note that this currently only applies to Null0 routes for aggregates.
       * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv6_route to encode a special
       * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
       * do not want to also encode the :: nexthop for the aggregate route.
       */
      if (CHECK_FLAG(flags, ZEBRA_FLAG_BLACKHOLE))
        api.nexthop_num = 0;
      else
        api.nexthop_num = valid_nh_count;

      api.nexthop = (struct in6_addr **)STREAM_DATA (bgp_nexthop_buf);
      SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
      api.ifindex_num = valid_nh_count;
      api.ifindex = (ifindex_t *)STREAM_DATA (bgp_ifindices_buf);
      if (safi == SAFI_LABELED_UNICAST)
        {
          api.label_num = valid_nh_count;
          api.label = (unsigned int *)STREAM_DATA (bgp_label_buf);
        }
      else
        {
          api.label_num = 0;
          api.label = NULL;
        }
      SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
      api.metric = metric;
      api.tag = 0;

      if (tag)
        {
          SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
          api.tag = tag;
        }

      distance = bgp_distance_apply (p, info, afi, safi, bgp);
      if (distance)
        {
          SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
          api.distance = distance;
        }

      if (p->family == AF_INET)
        {
          if (bgp_debug_zebra(p))
            {
              int i;
              char label_buf[20];
              zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI,
                         valid_nh_count ? "add" : "delete", bgp->vrf_id,
                         inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
                         p->prefixlen, api.metric, api.tag);
              for (i = 0; i < api.nexthop_num; i++)
                {
                  label_buf[0] = '\0';
                  if (safi == SAFI_LABELED_UNICAST)
                    sprintf(label_buf, "label %u", api.label[i]);
                  zlog_debug("  nhop [%d]: %s if %s %s",
                             i+1,
                             inet_ntop(AF_INET6, api.nexthop[i], buf[1], sizeof(buf[1])),
                             ifindex2ifname (api.ifindex[i], bgp->vrf_id),
                             label_buf);
                }
            }

          if (valid_nh_count)
            zapi_ipv4_route_ipv6_nexthop (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD,
                                          zclient, (struct prefix_ipv4 *) p,
					  (struct zapi_ipv6 *)&api);
          else
            zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE,
                             zclient, (struct prefix_ipv4 *) p, (struct zapi_ipv4 *)&api);
        }
      else
        {
          if (bgp_debug_zebra(p))
            {
              int i;
              char label_buf[20];
              zlog_debug("Tx IPv6 route %s VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI,
                         valid_nh_count ? "add" : "delete", bgp->vrf_id,
                         inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
                         p->prefixlen, api.metric, api.tag);
              for (i = 0; i < api.nexthop_num; i++)
                {
                  label_buf[0] = '\0';
                  if (safi == SAFI_LABELED_UNICAST)
                    sprintf(label_buf, "label %u", api.label[i]);
                  zlog_debug("  nhop [%d]: %s if %s %s",
                             i+1,
                             inet_ntop(AF_INET6, api.nexthop[i], buf[1], sizeof(buf[1])),
                             ifindex2ifname (api.ifindex[i], bgp->vrf_id),
                             label_buf);
                }
            }

          zapi_ipv6_route (valid_nh_count ?
                           ZEBRA_IPV6_ROUTE_ADD : ZEBRA_IPV6_ROUTE_DELETE,
                           zclient, (struct prefix_ipv6 *) p, NULL, &api);
        }
    }
}

/* Announce all routes of a table to zebra */
void
bgp_zebra_announce_table (struct bgp *bgp, afi_t afi, safi_t safi)
{
  struct bgp_node *rn;
  struct bgp_table *table;
  struct bgp_info *ri;

  /* Don't try to install if we're not connected to Zebra or Zebra doesn't
   * know of this instance.
   */
  if (!bgp_install_info_to_zebra (bgp))
    return;

  table = bgp->rib[afi][safi];
  if (!table) return;

  for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
    for (ri = rn->info; ri; ri = ri->next)
      if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED)
          && ri->type == ZEBRA_ROUTE_BGP
          && ri->sub_type == BGP_ROUTE_NORMAL)
        bgp_zebra_announce (rn, &rn->p, ri, bgp, afi, safi);
}

void
bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info, safi_t safi)
{
  u_int32_t flags;
  struct peer *peer;

  peer = info->peer;
  assert(peer);

  /* Don't try to install if we're not connected to Zebra or Zebra doesn't
   * know of this instance.
   */
  if (!bgp_install_info_to_zebra (peer->bgp))
    return;

  if ((p->family == AF_INET &&
       !vrf_bitmap_check (zclient->redist[AFI_IP][ZEBRA_ROUTE_BGP], peer->bgp->vrf_id))
      || (p->family == AF_INET6 &&
       !vrf_bitmap_check (zclient->redist[AFI_IP6][ZEBRA_ROUTE_BGP], peer->bgp->vrf_id)))
    return;

  flags = 0;

  if (peer->sort == BGP_PEER_IBGP)
    {
      SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
      SET_FLAG (flags, ZEBRA_FLAG_IBGP);
    }

  if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
      || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
      || bgp_flag_check(peer->bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
    SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);

  if (p->family == AF_INET)
    {
      struct zapi_ipv4 api;

      api.vrf_id = peer->bgp->vrf_id;
      api.flags = flags;

      api.type = ZEBRA_ROUTE_BGP;
      api.instance = 0;
      api.message = 0;
      api.safi = (safi == SAFI_LABELED_UNICAST) ? SAFI_UNICAST : safi;
      SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
      if (safi == SAFI_LABELED_UNICAST)
        SET_FLAG (api.message, ZAPI_MESSAGE_LABEL);
      api.nexthop_num = 0;
      api.nexthop = NULL;
      api.label_num = 0;
      api.label = NULL;
      api.ifindex_num = 0;
      SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
      api.metric = info->attr->med;
      api.tag = 0;

      if ((info->attr->extra) && (info->attr->extra->tag != 0))
        {
          SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
          api.tag = info->attr->extra->tag;
        }

      if (bgp_debug_zebra(p))
	{
	  char buf[2][INET_ADDRSTRLEN];
	  zlog_debug("Tx IPv4 route delete VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI,
                     peer->bgp->vrf_id,
		     inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
		     p->prefixlen, api.metric, api.tag);
	}

      zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, 
                       (struct prefix_ipv4 *) p, &api);
    }
  /* We have to think about a IPv6 link-local address curse. */
  if (p->family == AF_INET6)
    {
      struct zapi_ipv6 api;
      
      assert (info->attr->extra);

      api.vrf_id = peer->bgp->vrf_id;
      api.flags = flags;
      api.type = ZEBRA_ROUTE_BGP;
      api.instance = 0;
      api.message = 0;
      api.safi = (safi == SAFI_LABELED_UNICAST) ? SAFI_UNICAST : safi;
      SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
      if (safi == SAFI_LABELED_UNICAST)
        SET_FLAG (api.message, ZAPI_MESSAGE_LABEL);
      api.nexthop_num = 0;
      api.nexthop = NULL;
      api.ifindex_num = 0;
      api.label_num = 0;
      SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
      api.metric = info->attr->med;
      api.tag = 0;

      if ((info->attr->extra) && (info->attr->extra->tag != 0))
        {
          SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
          api.tag = info->attr->extra->tag;
        }

      if (bgp_debug_zebra(p))
	{
	  char buf[2][INET6_ADDRSTRLEN];
	  zlog_debug("Tx IPv6 route delete VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI,
                     peer->bgp->vrf_id,
		     inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
		     p->prefixlen, api.metric, api.tag);
	}

      zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient, 
                       (struct prefix_ipv6 *) p, NULL, &api);
    }
}

struct bgp_redist *
bgp_redist_lookup (struct bgp *bgp, afi_t afi, u_char type, u_short instance)
{
  struct list *red_list;
  struct listnode *node;
  struct bgp_redist *red;

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

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

  return NULL;
}

struct bgp_redist *
bgp_redist_add (struct bgp *bgp, afi_t afi, u_char type, u_short instance)
{
  struct list *red_list;
  struct bgp_redist *red;

  red = bgp_redist_lookup(bgp, afi, type, instance);
  if (red)
    return red;

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

  red_list = bgp->redist[afi][type];
  red = (struct bgp_redist *)XCALLOC(MTYPE_BGP_REDIST, sizeof(struct bgp_redist));
  red->instance = instance;

  listnode_add(red_list, red);

  return red;
}

static void
bgp_redist_del (struct bgp *bgp, afi_t afi, u_char type, u_short instance)
{
  struct bgp_redist *red;

  red = bgp_redist_lookup(bgp, afi, type, instance);

  if (red)
    {
      listnode_delete(bgp->redist[afi][type], red);
      XFREE (MTYPE_BGP_REDIST, red);
      if (!bgp->redist[afi][type]->count)
        {
          list_free(bgp->redist[afi][type]);
          bgp->redist[afi][type] = NULL;
        }
    }
}

/* Other routes redistribution into BGP. */
int
bgp_redistribute_set (struct bgp *bgp, afi_t afi, int type, u_short instance)
{

  /* Return if already redistribute flag is set. */
  if (instance)
    {
      if (redist_check_instance(&zclient->mi_redist[afi][type], instance))
        return CMD_WARNING;

      redist_add_instance(&zclient->mi_redist[afi][type], instance);
    }
  else
    {
      if (vrf_bitmap_check (zclient->redist[afi][type], bgp->vrf_id))
        return CMD_WARNING;

#if ENABLE_BGP_VNC
      if (bgp->vrf_id == VRF_DEFAULT &&
          type == ZEBRA_ROUTE_VNC_DIRECT) {
        vnc_export_bgp_enable(bgp, afi);	/* only enables if mode bits cfg'd */
      }
#endif

      vrf_bitmap_set (zclient->redist[afi][type], bgp->vrf_id);
    }

  /* Don't try to register if we're not connected to Zebra or Zebra doesn't
   * know of this instance.
   */
  if (!bgp_install_info_to_zebra (bgp))
    return CMD_WARNING;

  if (BGP_DEBUG (zebra, ZEBRA))
    zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
               bgp->vrf_id, afi,
               zebra_route_string(type), instance);

  /* Send distribute add message to zebra. */
  zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
                           instance, bgp->vrf_id);

  return CMD_SUCCESS;
}

int
bgp_redistribute_resend (struct bgp *bgp, afi_t afi, int type, u_short instance)
{
  /* Don't try to send if we're not connected to Zebra or Zebra doesn't
   * know of this instance.
   */
  if (!bgp_install_info_to_zebra (bgp))
    return -1;

  if (BGP_DEBUG (zebra, ZEBRA))
    zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
               bgp->vrf_id, afi,
               zebra_route_string(type), instance);

  /* Send distribute add message to zebra. */
  zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
                           instance, bgp->vrf_id);
  zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
                           instance, bgp->vrf_id);

  return 0;
}

/* Redistribute with route-map specification.  */
int
bgp_redistribute_rmap_set (struct bgp_redist *red, const char *name)
{
  if (red->rmap.name
      && (strcmp (red->rmap.name, name) == 0))
    return 0;

  if (red->rmap.name)
    XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
  red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
  red->rmap.map = route_map_lookup_by_name (name);

  return 1;
}

/* Redistribute with metric specification.  */
int
bgp_redistribute_metric_set (struct bgp *bgp, struct bgp_redist *red, afi_t afi,
			     int type, u_int32_t metric)
{
  struct bgp_node *rn;
  struct bgp_info *ri;

  if (red->redist_metric_flag
      && red->redist_metric == metric)
    return 0;

  red->redist_metric_flag = 1;
  red->redist_metric = metric;

  for (rn = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); rn; rn = bgp_route_next(rn))
    {
      for (ri = rn->info; ri; ri = ri->next)
        {
          if (ri->sub_type == BGP_ROUTE_REDISTRIBUTE &&
              ri->type == type &&
              ri->instance == red->instance)
            {
              struct attr *old_attr;
              struct attr new_attr;
              struct attr_extra new_extra;

              new_attr.extra = &new_extra;
              bgp_attr_dup (&new_attr, ri->attr);
              new_attr.med = red->redist_metric;
              old_attr = ri->attr;
              ri->attr = bgp_attr_intern (&new_attr);
              bgp_attr_unintern (&old_attr);

              bgp_info_set_flag(rn, ri, BGP_INFO_ATTR_CHANGED);
              bgp_process(bgp, rn, afi, SAFI_UNICAST);
            }
        }
    }

  return 1;
}

/* Unset redistribution.  */
int
bgp_redistribute_unreg (struct bgp *bgp, afi_t afi, int type, u_short instance)
{
  struct bgp_redist *red;

  red = bgp_redist_lookup(bgp, afi, type, instance);
  if (!red)
    return CMD_SUCCESS;

  /* Return if zebra connection is disabled. */
  if (instance)
    {
      if (!redist_check_instance(&zclient->mi_redist[afi][type], instance))
        return CMD_WARNING;
      redist_del_instance(&zclient->mi_redist[afi][type], instance);
    }
  else
    {
      if (! vrf_bitmap_check (zclient->redist[afi][type], bgp->vrf_id))
        return CMD_WARNING;
      vrf_bitmap_unset (zclient->redist[afi][type], bgp->vrf_id);
    }

#if ENABLE_BGP_VNC
  if (bgp->vrf_id == VRF_DEFAULT &&
      type == ZEBRA_ROUTE_VNC_DIRECT) {
    vnc_export_bgp_disable(bgp, afi);
  }
#endif

  if (bgp_install_info_to_zebra (bgp))
    {
      /* Send distribute delete message to zebra. */
      if (BGP_DEBUG (zebra, ZEBRA))
	zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
		   bgp->vrf_id, afi, zebra_route_string(type), instance);
      zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type, instance,
                               bgp->vrf_id);
    }
  
  /* Withdraw redistributed routes from current BGP's routing table. */
  bgp_redistribute_withdraw (bgp, afi, type, instance);

  return CMD_SUCCESS;
}

/* Unset redistribution.  */
int
bgp_redistribute_unset (struct bgp *bgp, afi_t afi, int type, u_short instance)
{
  struct bgp_redist *red;

  red = bgp_redist_lookup(bgp, afi, type, instance);
  if (!red)
    return CMD_SUCCESS;

  bgp_redistribute_unreg(bgp, afi, type, instance);

  /* Unset route-map. */
  if (red->rmap.name)
    XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
  red->rmap.name = NULL;
  red->rmap.map = NULL;

  /* Unset metric. */
  red->redist_metric_flag = 0;
  red->redist_metric = 0;

  bgp_redist_del(bgp, afi, type, instance);

  return CMD_SUCCESS;
}

/* Update redistribute vrf bitmap during triggers like
   restart networking or delete/add VRFs */
void
bgp_update_redist_vrf_bitmaps (struct bgp *bgp, vrf_id_t old_vrf_id)
{
  int i;
  afi_t afi;

  for (afi = AFI_IP; afi < AFI_MAX; afi++)
    for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
      if (vrf_bitmap_check (zclient->redist[afi][i], old_vrf_id))
        {
          vrf_bitmap_unset (zclient->redist[afi][i], old_vrf_id);
          vrf_bitmap_set (zclient->redist[afi][i], bgp->vrf_id);
        }
  return;
}

void
bgp_zclient_reset (void)
{
  zclient_reset (zclient);
}

/* Register this instance with Zebra. Invoked upon connect (for
 * default instance) and when other VRFs are learnt (or created and
 * already learnt).
 */
void
bgp_zebra_instance_register (struct bgp *bgp)
{
  /* Don't try to register if we're not connected to Zebra */
  if (!zclient || zclient->sock < 0)
    return;

  if (BGP_DEBUG (zebra, ZEBRA))
    zlog_debug("Registering VRF %u", bgp->vrf_id);

  /* Register for router-id, interfaces, redistributed routes. */
  zclient_send_reg_requests (zclient, bgp->vrf_id);
}

/* Deregister this instance with Zebra. Invoked upon the instance
 * being deleted (default or VRF) and it is already registered.
 */
void
bgp_zebra_instance_deregister (struct bgp *bgp)
{
  /* Don't try to deregister if we're not connected to Zebra */
  if (zclient->sock < 0)
    return;

  if (BGP_DEBUG (zebra, ZEBRA))
    zlog_debug("Deregistering VRF %u", bgp->vrf_id);

  /* Deregister for router-id, interfaces, redistributed routes. */
  zclient_send_dereg_requests (zclient, bgp->vrf_id);
}

void
bgp_zebra_initiate_radv (struct bgp *bgp, struct peer *peer)
{
  int ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;

  /* Don't try to initiate if we're not connected to Zebra */
  if (zclient->sock < 0)
    return;

  if (BGP_DEBUG (zebra, ZEBRA))
    zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id, peer->host);

  zclient_send_interface_radv_req (zclient, bgp->vrf_id, peer->ifp, 1, ra_interval);
}

void
bgp_zebra_terminate_radv (struct bgp *bgp, struct peer *peer)
{
  /* Don't try to terminate if we're not connected to Zebra */
  if (zclient->sock < 0)
    return;

  if (BGP_DEBUG (zebra, ZEBRA))
    zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id, peer->host);

  zclient_send_interface_radv_req (zclient, bgp->vrf_id, peer->ifp, 0, 0);
}

/* BGP has established connection with Zebra. */
static void
bgp_zebra_connected (struct zclient *zclient)
{
  struct bgp *bgp;

  zclient_num_connects++;       /* increment even if not responding */

  /* At this point, we may or may not have BGP instances configured, but
   * we're only interested in the default VRF (others wouldn't have learnt
   * the VRF from Zebra yet.)
   */
  bgp = bgp_get_default();
  if (!bgp)
    return;

  bgp_zebra_instance_register (bgp);

  /* Send the client registration */
  bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);

  /* TODO - What if we have peers and networks configured, do we have to
   * kick-start them?
   */
}


void
bgp_zebra_init (struct thread_master *master)
{
  zclient_num_connects = 0;

  /* Set default values. */
  zclient = zclient_new (master);
  zclient_init (zclient, ZEBRA_ROUTE_BGP, 0);
  zclient->zebra_connected = bgp_zebra_connected;
  zclient->router_id_update = bgp_router_id_update;
  zclient->interface_add = bgp_interface_add;
  zclient->interface_delete = bgp_interface_delete;
  zclient->interface_address_add = bgp_interface_address_add;
  zclient->interface_address_delete = bgp_interface_address_delete;
  zclient->interface_nbr_address_add = bgp_interface_nbr_address_add;
  zclient->interface_nbr_address_delete = bgp_interface_nbr_address_delete;
  zclient->interface_vrf_update = bgp_interface_vrf_update;
  zclient->redistribute_route_ipv4_add = zebra_read_ipv4;
  zclient->redistribute_route_ipv4_del = zebra_read_ipv4;
  zclient->interface_up = bgp_interface_up;
  zclient->interface_down = bgp_interface_down;
  zclient->redistribute_route_ipv6_add = zebra_read_ipv6;
  zclient->redistribute_route_ipv6_del = zebra_read_ipv6;
  zclient->nexthop_update = bgp_read_nexthop_update;
  zclient->import_check_update = bgp_read_import_check_update;
  zclient->fec_update = bgp_read_fec_update;

  bgp_nexthop_buf = stream_new(BGP_NEXTHOP_BUF_SIZE);
  bgp_ifindices_buf = stream_new(BGP_IFINDICES_BUF_SIZE);
  bgp_label_buf = stream_new(BGP_LABEL_BUF_SIZE);
}

void
bgp_zebra_destroy(void)
{
  if (zclient == NULL)
    return;
  zclient_stop(zclient);
  zclient_free(zclient);
  zclient = NULL;
}

int
bgp_zebra_num_connects(void)
{
  return zclient_num_connects;
}