bgp_nexthop.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /* BGP nexthop scan
  2. Copyright (C) 2000 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #include <zebra.h>
  17. #include "command.h"
  18. #include "thread.h"
  19. #include "prefix.h"
  20. #include "zclient.h"
  21. #include "stream.h"
  22. #include "network.h"
  23. #include "log.h"
  24. #include "memory.h"
  25. #include "bgpd/bgpd.h"
  26. #include "bgpd/bgp_table.h"
  27. #include "bgpd/bgp_route.h"
  28. #include "bgpd/bgp_attr.h"
  29. #include "bgpd/bgp_nexthop.h"
  30. #include "bgpd/bgp_debug.h"
  31. #include "bgpd/bgp_damp.h"
  32. #include "zebra/rib.h"
  33. #include "zebra/zserv.h" /* For ZEBRA_SERV_PATH. */
  34. struct bgp_nexthop_cache *zlookup_query (struct in_addr);
  35. #ifdef HAVE_IPV6
  36. struct bgp_nexthop_cache *zlookup_query_ipv6 (struct in6_addr *);
  37. #endif /* HAVE_IPV6 */
  38. /* Only one BGP scan thread are activated at the same time. */
  39. struct thread *bgp_scan_thread = NULL;
  40. /* BGP import thread */
  41. struct thread *bgp_import_thread = NULL;
  42. /* BGP scan interval. */
  43. int bgp_scan_interval;
  44. /* BGP import interval. */
  45. int bgp_import_interval;
  46. /* Route table for next-hop lookup cache. */
  47. struct bgp_table *bgp_nexthop_cache_ipv4;
  48. struct bgp_table *cache1;
  49. struct bgp_table *cache2;
  50. /* Route table for next-hop lookup cache. */
  51. struct bgp_table *bgp_nexthop_cache_ipv6;
  52. struct bgp_table *cache6_1;
  53. struct bgp_table *cache6_2;
  54. /* Route table for connected route. */
  55. struct bgp_table *bgp_connected_ipv4;
  56. /* Route table for connected route. */
  57. struct bgp_table *bgp_connected_ipv6;
  58. /* BGP nexthop lookup query client. */
  59. static struct zclient *zlookup = NULL;
  60. /* BGP process function. */
  61. int bgp_process (struct bgp *, struct bgp_node *, afi_t, safi_t);
  62. /* Add nexthop to the end of the list. */
  63. void
  64. bnc_nexthop_add (struct bgp_nexthop_cache *bnc, struct nexthop *nexthop)
  65. {
  66. struct nexthop *last;
  67. for (last = bnc->nexthop; last && last->next; last = last->next)
  68. ;
  69. if (last)
  70. last->next = nexthop;
  71. else
  72. bnc->nexthop = nexthop;
  73. nexthop->prev = last;
  74. }
  75. void
  76. bnc_nexthop_free (struct bgp_nexthop_cache *bnc)
  77. {
  78. struct nexthop *nexthop;
  79. struct nexthop *next = NULL;
  80. for (nexthop = bnc->nexthop; nexthop; nexthop = next)
  81. {
  82. next = nexthop->next;
  83. XFREE (MTYPE_NEXTHOP, nexthop);
  84. }
  85. }
  86. struct bgp_nexthop_cache *
  87. bnc_new ()
  88. {
  89. struct bgp_nexthop_cache *new;
  90. new = XMALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache));
  91. memset (new, 0, sizeof (struct bgp_nexthop_cache));
  92. return new;
  93. }
  94. void
  95. bnc_free (struct bgp_nexthop_cache *bnc)
  96. {
  97. bnc_nexthop_free (bnc);
  98. XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc);
  99. }
  100. int
  101. bgp_nexthop_same (struct nexthop *next1, struct nexthop *next2)
  102. {
  103. if (next1->type != next2->type)
  104. return 0;
  105. switch (next1->type)
  106. {
  107. case ZEBRA_NEXTHOP_IPV4:
  108. if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4))
  109. return 0;
  110. break;
  111. case ZEBRA_NEXTHOP_IFINDEX:
  112. case ZEBRA_NEXTHOP_IFNAME:
  113. if (next1->ifindex != next2->ifindex)
  114. return 0;
  115. break;
  116. #ifdef HAVE_IPV6
  117. case ZEBRA_NEXTHOP_IPV6:
  118. if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
  119. return 0;
  120. break;
  121. case ZEBRA_NEXTHOP_IPV6_IFINDEX:
  122. case ZEBRA_NEXTHOP_IPV6_IFNAME:
  123. if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
  124. return 0;
  125. if (next1->ifindex != next2->ifindex)
  126. return 0;
  127. break;
  128. #endif /* HAVE_IPV6 */
  129. }
  130. return 1;
  131. }
  132. int
  133. bgp_nexthop_cache_changed (struct bgp_nexthop_cache *bnc1,
  134. struct bgp_nexthop_cache *bnc2)
  135. {
  136. int i;
  137. struct nexthop *next1, *next2;
  138. if (bnc1->nexthop_num != bnc2->nexthop_num)
  139. return 1;
  140. next1 = bnc1->nexthop;
  141. next2 = bnc2->nexthop;
  142. for (i = 0; i < bnc1->nexthop_num; i++)
  143. {
  144. if (! bgp_nexthop_same (next1, next2))
  145. return 1;
  146. next1 = next1->next;
  147. next2 = next2->next;
  148. }
  149. return 0;
  150. }
  151. /* If nexthop exists on connected network return 1. */
  152. int
  153. bgp_nexthop_check_ebgp (afi_t afi, struct attr *attr)
  154. {
  155. struct bgp_node *rn;
  156. /* If zebra is not enabled return */
  157. if (zlookup->sock < 0)
  158. return 1;
  159. /* Lookup the address is onlink or not. */
  160. if (afi == AFI_IP)
  161. {
  162. rn = bgp_node_match_ipv4 (bgp_connected_ipv4, &attr->nexthop);
  163. if (rn)
  164. {
  165. bgp_unlock_node (rn);
  166. return 1;
  167. }
  168. }
  169. #ifdef HAVE_IPV6
  170. else if (afi == AFI_IP6)
  171. {
  172. if (attr->mp_nexthop_len == 32)
  173. return 1;
  174. else if (attr->mp_nexthop_len == 16)
  175. {
  176. if (IN6_IS_ADDR_LINKLOCAL (&attr->mp_nexthop_global))
  177. return 1;
  178. rn = bgp_node_match_ipv6 (bgp_connected_ipv6,
  179. &attr->mp_nexthop_global);
  180. if (rn)
  181. {
  182. bgp_unlock_node (rn);
  183. return 1;
  184. }
  185. }
  186. }
  187. #endif /* HAVE_IPV6 */
  188. return 0;
  189. }
  190. #ifdef HAVE_IPV6
  191. /* Check specified next-hop is reachable or not. */
  192. int
  193. bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
  194. int *metricchanged)
  195. {
  196. struct bgp_node *rn;
  197. struct prefix p;
  198. struct bgp_nexthop_cache *bnc;
  199. struct attr *attr;
  200. /* If lookup is not enabled, return valid. */
  201. if (zlookup->sock < 0)
  202. {
  203. ri->igpmetric = 0;
  204. return 1;
  205. }
  206. /* Only check IPv6 global address only nexthop. */
  207. attr = ri->attr;
  208. if (attr->mp_nexthop_len != 16
  209. || IN6_IS_ADDR_LINKLOCAL (&attr->mp_nexthop_global))
  210. return 1;
  211. memset (&p, 0, sizeof (struct prefix));
  212. p.family = AF_INET6;
  213. p.prefixlen = IPV6_MAX_BITLEN;
  214. p.u.prefix6 = attr->mp_nexthop_global;
  215. /* IBGP or ebgp-multihop */
  216. rn = bgp_node_get (bgp_nexthop_cache_ipv6, &p);
  217. if (rn->info)
  218. {
  219. bnc = rn->info;
  220. bgp_unlock_node (rn);
  221. }
  222. else
  223. {
  224. bnc = zlookup_query_ipv6 (&attr->mp_nexthop_global);
  225. if (bnc)
  226. {
  227. struct bgp_table *old;
  228. struct bgp_node *oldrn;
  229. struct bgp_nexthop_cache *oldbnc;
  230. if (changed)
  231. {
  232. if (bgp_nexthop_cache_ipv6 == cache6_1)
  233. old = cache6_2;
  234. else
  235. old = cache6_1;
  236. oldrn = bgp_node_lookup (old, &p);
  237. if (oldrn)
  238. {
  239. oldbnc = oldrn->info;
  240. bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
  241. if (bnc->metric != oldbnc->metric)
  242. bnc->metricchanged = 1;
  243. }
  244. }
  245. }
  246. else
  247. {
  248. bnc = bnc_new ();
  249. bnc->valid = 0;
  250. }
  251. rn->info = bnc;
  252. }
  253. if (changed)
  254. *changed = bnc->changed;
  255. if (metricchanged)
  256. *metricchanged = bnc->metricchanged;
  257. if (bnc->valid)
  258. ri->igpmetric = bnc->metric;
  259. else
  260. ri->igpmetric = 0;
  261. return bnc->valid;
  262. }
  263. #endif /* HAVE_IPV6 */
  264. /* Check specified next-hop is reachable or not. */
  265. int
  266. bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
  267. int *changed, int *metricchanged)
  268. {
  269. struct bgp_node *rn;
  270. struct prefix p;
  271. struct bgp_nexthop_cache *bnc;
  272. struct in_addr addr;
  273. /* If lookup is not enabled, return valid. */
  274. if (zlookup->sock < 0)
  275. {
  276. ri->igpmetric = 0;
  277. return 1;
  278. }
  279. #ifdef HAVE_IPV6
  280. if (afi == AFI_IP6)
  281. return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
  282. #endif /* HAVE_IPV6 */
  283. addr = ri->attr->nexthop;
  284. memset (&p, 0, sizeof (struct prefix));
  285. p.family = AF_INET;
  286. p.prefixlen = IPV4_MAX_BITLEN;
  287. p.u.prefix4 = addr;
  288. /* IBGP or ebgp-multihop */
  289. rn = bgp_node_get (bgp_nexthop_cache_ipv4, &p);
  290. if (rn->info)
  291. {
  292. bnc = rn->info;
  293. bgp_unlock_node (rn);
  294. }
  295. else
  296. {
  297. bnc = zlookup_query (addr);
  298. if (bnc)
  299. {
  300. struct bgp_table *old;
  301. struct bgp_node *oldrn;
  302. struct bgp_nexthop_cache *oldbnc;
  303. if (changed)
  304. {
  305. if (bgp_nexthop_cache_ipv4 == cache1)
  306. old = cache2;
  307. else
  308. old = cache1;
  309. oldrn = bgp_node_lookup (old, &p);
  310. if (oldrn)
  311. {
  312. oldbnc = oldrn->info;
  313. bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
  314. if (bnc->metric != oldbnc->metric)
  315. bnc->metricchanged = 1;
  316. }
  317. }
  318. }
  319. else
  320. {
  321. bnc = bnc_new ();
  322. bnc->valid = 0;
  323. }
  324. rn->info = bnc;
  325. }
  326. if (changed)
  327. *changed = bnc->changed;
  328. if (metricchanged)
  329. *metricchanged = bnc->metricchanged;
  330. if (bnc->valid)
  331. ri->igpmetric = bnc->metric;
  332. else
  333. ri->igpmetric = 0;
  334. return bnc->valid;
  335. }
  336. /* Reset and free all BGP nexthop cache. */
  337. void
  338. bgp_nexthop_cache_reset (struct bgp_table *table)
  339. {
  340. struct bgp_node *rn;
  341. struct bgp_nexthop_cache *bnc;
  342. for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
  343. if ((bnc = rn->info) != NULL)
  344. {
  345. bnc_free (bnc);
  346. rn->info = NULL;
  347. bgp_unlock_node (rn);
  348. }
  349. }
  350. void
  351. bgp_scan_ipv4 ()
  352. {
  353. struct bgp_node *rn;
  354. struct bgp *bgp;
  355. struct bgp_info *bi;
  356. struct bgp_info *next;
  357. struct peer *peer;
  358. struct listnode *nn;
  359. int valid;
  360. int current;
  361. int changed;
  362. int metricchanged;
  363. /* Change cache. */
  364. if (bgp_nexthop_cache_ipv4 == cache1)
  365. bgp_nexthop_cache_ipv4 = cache2;
  366. else
  367. bgp_nexthop_cache_ipv4 = cache1;
  368. /* Get default bgp. */
  369. bgp = bgp_get_default ();
  370. if (bgp == NULL)
  371. return;
  372. /* Maximum prefix check */
  373. LIST_LOOP (bgp->peer, peer, nn)
  374. {
  375. if (peer->status != Established)
  376. continue;
  377. if (peer->afc[AFI_IP][SAFI_UNICAST])
  378. bgp_maximum_prefix_overflow (peer, AFI_IP, SAFI_UNICAST);
  379. if (peer->afc[AFI_IP][SAFI_MULTICAST])
  380. bgp_maximum_prefix_overflow (peer, AFI_IP, SAFI_MULTICAST);
  381. if (peer->afc[AFI_IP][SAFI_MPLS_VPN])
  382. bgp_maximum_prefix_overflow (peer, AFI_IP, SAFI_MPLS_VPN);
  383. }
  384. for (rn = bgp_table_top (bgp->rib[AFI_IP][SAFI_UNICAST]); rn;
  385. rn = bgp_route_next (rn))
  386. {
  387. for (bi = rn->info; bi; bi = next)
  388. {
  389. next = bi->next;
  390. if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
  391. {
  392. changed = 0;
  393. metricchanged = 0;
  394. if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1)
  395. valid = bgp_nexthop_check_ebgp (AFI_IP, bi->attr);
  396. else
  397. valid = bgp_nexthop_lookup (AFI_IP, bi->peer, bi,
  398. &changed, &metricchanged);
  399. current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
  400. if (changed)
  401. SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
  402. else
  403. UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
  404. if (valid != current)
  405. {
  406. if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
  407. {
  408. bgp_aggregate_decrement (bgp, &rn->p, bi,
  409. AFI_IP, SAFI_UNICAST);
  410. UNSET_FLAG (bi->flags, BGP_INFO_VALID);
  411. }
  412. else
  413. {
  414. SET_FLAG (bi->flags, BGP_INFO_VALID);
  415. bgp_aggregate_increment (bgp, &rn->p, bi,
  416. AFI_IP, SAFI_UNICAST);
  417. }
  418. }
  419. if (CHECK_FLAG (bgp->af_flags[AFI_IP][SAFI_UNICAST],
  420. BGP_CONFIG_DAMPENING)
  421. && bi->damp_info )
  422. if (bgp_damp_scan (bi, AFI_IP, SAFI_UNICAST))
  423. bgp_aggregate_increment (bgp, &rn->p, bi,
  424. AFI_IP, SAFI_UNICAST);
  425. }
  426. }
  427. bgp_process (bgp, rn, AFI_IP, SAFI_UNICAST);
  428. }
  429. /* Flash old cache. */
  430. if (bgp_nexthop_cache_ipv4 == cache1)
  431. bgp_nexthop_cache_reset (cache2);
  432. else
  433. bgp_nexthop_cache_reset (cache1);
  434. }
  435. #ifdef HAVE_IPV6
  436. void
  437. bgp_scan_ipv6 ()
  438. {
  439. struct bgp_node *rn;
  440. struct bgp *bgp;
  441. struct bgp_info *bi;
  442. struct bgp_info *next;
  443. struct peer *peer;
  444. struct listnode *nn;
  445. int valid;
  446. int current;
  447. int changed;
  448. int metricchanged;
  449. /* Change cache. */
  450. if (bgp_nexthop_cache_ipv6 == cache6_1)
  451. bgp_nexthop_cache_ipv6 = cache6_2;
  452. else
  453. bgp_nexthop_cache_ipv6 = cache6_1;
  454. /* Get default bgp. */
  455. bgp = bgp_get_default ();
  456. if (bgp == NULL)
  457. return;
  458. /* Maximum prefix check */
  459. LIST_LOOP (bgp->peer, peer, nn)
  460. {
  461. if (peer->status != Established)
  462. continue;
  463. if (peer->afc[AFI_IP6][SAFI_UNICAST])
  464. bgp_maximum_prefix_overflow (peer, AFI_IP6, SAFI_UNICAST);
  465. if (peer->afc[AFI_IP6][SAFI_MULTICAST])
  466. bgp_maximum_prefix_overflow (peer, AFI_IP6, SAFI_MULTICAST);
  467. }
  468. for (rn = bgp_table_top (bgp->rib[AFI_IP6][SAFI_UNICAST]); rn;
  469. rn = bgp_route_next (rn))
  470. {
  471. for (bi = rn->info; bi; bi = next)
  472. {
  473. next = bi->next;
  474. if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
  475. {
  476. changed = 0;
  477. metricchanged = 0;
  478. if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1)
  479. valid = 1;
  480. else
  481. valid = bgp_nexthop_lookup_ipv6 (bi->peer, bi,
  482. &changed, &metricchanged);
  483. current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
  484. if (changed)
  485. SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
  486. else
  487. UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
  488. if (valid != current)
  489. {
  490. if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
  491. {
  492. bgp_aggregate_decrement (bgp, &rn->p, bi,
  493. AFI_IP6, SAFI_UNICAST);
  494. UNSET_FLAG (bi->flags, BGP_INFO_VALID);
  495. }
  496. else
  497. {
  498. SET_FLAG (bi->flags, BGP_INFO_VALID);
  499. bgp_aggregate_increment (bgp, &rn->p, bi,
  500. AFI_IP6, SAFI_UNICAST);
  501. }
  502. }
  503. if (CHECK_FLAG (bgp->af_flags[AFI_IP6][SAFI_UNICAST],
  504. BGP_CONFIG_DAMPENING)
  505. && bi->damp_info )
  506. if (bgp_damp_scan (bi, AFI_IP6, SAFI_UNICAST))
  507. bgp_aggregate_increment (bgp, &rn->p, bi,
  508. AFI_IP6, SAFI_UNICAST);
  509. }
  510. }
  511. bgp_process (bgp, rn, AFI_IP6, SAFI_UNICAST);
  512. }
  513. /* Flash old cache. */
  514. if (bgp_nexthop_cache_ipv6 == cache6_1)
  515. bgp_nexthop_cache_reset (cache6_2);
  516. else
  517. bgp_nexthop_cache_reset (cache6_1);
  518. }
  519. #endif /* HAVE_IPV6 */
  520. /* BGP scan thread. This thread check nexthop reachability. */
  521. int
  522. bgp_scan (struct thread *t)
  523. {
  524. bgp_scan_thread =
  525. thread_add_timer (master, bgp_scan, NULL, bgp_scan_interval);
  526. if (BGP_DEBUG (normal, NORMAL))
  527. zlog_info ("Performing BGP general scanning");
  528. bgp_scan_ipv4 ();
  529. #ifdef HAVE_IPV6
  530. bgp_scan_ipv6 ();
  531. #endif /* HAVE_IPV6 */
  532. return 0;
  533. }
  534. struct bgp_connected
  535. {
  536. unsigned int refcnt;
  537. };
  538. void
  539. bgp_connected_add (struct connected *ifc)
  540. {
  541. struct prefix p;
  542. struct prefix *addr;
  543. struct prefix *dest;
  544. struct interface *ifp;
  545. struct bgp_node *rn;
  546. struct bgp_connected *bc;
  547. ifp = ifc->ifp;
  548. if (! ifp)
  549. return;
  550. if (if_is_loopback (ifp))
  551. return;
  552. addr = ifc->address;
  553. dest = ifc->destination;
  554. if (addr->family == AF_INET)
  555. {
  556. memset (&p, 0, sizeof (struct prefix));
  557. p.family = AF_INET;
  558. p.prefixlen = addr->prefixlen;
  559. if (ifc_pointopoint (ifc))
  560. p.u.prefix4 = dest->u.prefix4;
  561. else
  562. p.u.prefix4 = addr->u.prefix4;
  563. apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
  564. if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
  565. return;
  566. rn = bgp_node_get (bgp_connected_ipv4, (struct prefix *) &p);
  567. if (rn->info)
  568. {
  569. bc = rn->info;
  570. bc->refcnt++;
  571. }
  572. else
  573. {
  574. bc = XMALLOC (0, sizeof (struct bgp_connected));
  575. memset (bc, 0, sizeof (struct bgp_connected));
  576. bc->refcnt = 1;
  577. rn->info = bc;
  578. }
  579. }
  580. #ifdef HAVE_IPV6
  581. if (addr->family == AF_INET6)
  582. {
  583. memset (&p, 0, sizeof (struct prefix));
  584. p.family = AF_INET6;
  585. p.prefixlen = addr->prefixlen;
  586. if (ifc_pointopoint (ifc))
  587. p.u.prefix6 = dest->u.prefix6;
  588. else
  589. p.u.prefix6 = addr->u.prefix6;
  590. apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
  591. if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
  592. return;
  593. if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
  594. return;
  595. rn = bgp_node_get (bgp_connected_ipv6, (struct prefix *) &p);
  596. if (rn->info)
  597. {
  598. bc = rn->info;
  599. bc->refcnt++;
  600. }
  601. else
  602. {
  603. bc = XMALLOC (0, sizeof (struct bgp_connected));
  604. memset (bc, 0, sizeof (struct bgp_connected));
  605. bc->refcnt = 1;
  606. rn->info = bc;
  607. }
  608. }
  609. #endif /* HAVE_IPV6 */
  610. }
  611. void
  612. bgp_connected_delete (struct connected *ifc)
  613. {
  614. struct prefix p;
  615. struct prefix *addr;
  616. struct prefix *dest;
  617. struct interface *ifp;
  618. struct bgp_node *rn;
  619. struct bgp_connected *bc;
  620. ifp = ifc->ifp;
  621. if (if_is_loopback (ifp))
  622. return;
  623. addr = ifc->address;
  624. dest = ifc->destination;
  625. if (addr->family == AF_INET)
  626. {
  627. memset (&p, 0, sizeof (struct prefix));
  628. p.family = AF_INET;
  629. p.prefixlen = addr->prefixlen;
  630. if (ifc_pointopoint (ifc))
  631. p.u.prefix4 = dest->u.prefix4;
  632. else
  633. p.u.prefix4 = addr->u.prefix4;
  634. apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
  635. if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
  636. return;
  637. rn = bgp_node_lookup (bgp_connected_ipv4, &p);
  638. if (! rn)
  639. return;
  640. bc = rn->info;
  641. bc->refcnt--;
  642. if (bc->refcnt == 0)
  643. {
  644. XFREE (0, bc);
  645. rn->info = NULL;
  646. }
  647. bgp_unlock_node (rn);
  648. bgp_unlock_node (rn);
  649. }
  650. #ifdef HAVE_IPV6
  651. else if (addr->family == AF_INET6)
  652. {
  653. memset (&p, 0, sizeof (struct prefix));
  654. p.family = AF_INET6;
  655. p.prefixlen = addr->prefixlen;
  656. if (ifc_pointopoint (ifc))
  657. p.u.prefix6 = dest->u.prefix6;
  658. else
  659. p.u.prefix6 = addr->u.prefix6;
  660. apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
  661. if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
  662. return;
  663. if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
  664. return;
  665. rn = bgp_node_lookup (bgp_connected_ipv6, (struct prefix *) &p);
  666. if (! rn)
  667. return;
  668. bc = rn->info;
  669. bc->refcnt--;
  670. if (bc->refcnt == 0)
  671. {
  672. XFREE (0, bc);
  673. rn->info = NULL;
  674. }
  675. bgp_unlock_node (rn);
  676. bgp_unlock_node (rn);
  677. }
  678. #endif /* HAVE_IPV6 */
  679. }
  680. int
  681. bgp_nexthop_self (afi_t afi, struct attr *attr)
  682. {
  683. listnode node;
  684. listnode node2;
  685. struct interface *ifp;
  686. struct connected *ifc;
  687. struct prefix *p;
  688. for (node = listhead (iflist); node; nextnode (node))
  689. {
  690. ifp = getdata (node);
  691. for (node2 = listhead (ifp->connected); node2; nextnode (node2))
  692. {
  693. ifc = getdata (node2);
  694. p = ifc->address;
  695. if (p && p->family == AF_INET
  696. && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop))
  697. return 1;
  698. }
  699. }
  700. return 0;
  701. }
  702. struct bgp_nexthop_cache *
  703. zlookup_read ()
  704. {
  705. struct stream *s;
  706. u_int16_t length;
  707. u_char command;
  708. int nbytes;
  709. struct in_addr raddr;
  710. u_int32_t metric;
  711. int i;
  712. u_char nexthop_num;
  713. struct nexthop *nexthop;
  714. struct bgp_nexthop_cache *bnc;
  715. s = zlookup->ibuf;
  716. stream_reset (s);
  717. nbytes = stream_read (s, zlookup->sock, 2);
  718. length = stream_getw (s);
  719. nbytes = stream_read (s, zlookup->sock, length - 2);
  720. command = stream_getc (s);
  721. raddr.s_addr = stream_get_ipv4 (s);
  722. metric = stream_getl (s);
  723. nexthop_num = stream_getc (s);
  724. if (nexthop_num)
  725. {
  726. bnc = bnc_new ();
  727. bnc->valid = 1;
  728. bnc->metric = metric;
  729. bnc->nexthop_num = nexthop_num;
  730. for (i = 0; i < nexthop_num; i++)
  731. {
  732. nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  733. memset (nexthop, 0, sizeof (struct nexthop));
  734. nexthop->type = stream_getc (s);
  735. switch (nexthop->type)
  736. {
  737. case ZEBRA_NEXTHOP_IPV4:
  738. nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
  739. break;
  740. case ZEBRA_NEXTHOP_IFINDEX:
  741. case ZEBRA_NEXTHOP_IFNAME:
  742. nexthop->ifindex = stream_getl (s);
  743. break;
  744. }
  745. bnc_nexthop_add (bnc, nexthop);
  746. }
  747. }
  748. else
  749. return NULL;
  750. return bnc;
  751. }
  752. struct bgp_nexthop_cache *
  753. zlookup_query (struct in_addr addr)
  754. {
  755. int ret;
  756. struct stream *s;
  757. /* Check socket. */
  758. if (zlookup->sock < 0)
  759. return NULL;
  760. s = zlookup->obuf;
  761. stream_reset (s);
  762. stream_putw (s, 7);
  763. stream_putc (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
  764. stream_put_in_addr (s, &addr);
  765. ret = writen (zlookup->sock, s->data, 7);
  766. if (ret < 0)
  767. {
  768. zlog_err ("can't write to zlookup->sock");
  769. close (zlookup->sock);
  770. zlookup->sock = -1;
  771. return NULL;
  772. }
  773. if (ret == 0)
  774. {
  775. zlog_err ("zlookup->sock connection closed");
  776. close (zlookup->sock);
  777. zlookup->sock = -1;
  778. return NULL;
  779. }
  780. return zlookup_read ();
  781. }
  782. #ifdef HAVE_IPV6
  783. struct bgp_nexthop_cache *
  784. zlookup_read_ipv6 ()
  785. {
  786. struct stream *s;
  787. u_int16_t length;
  788. u_char command;
  789. int nbytes;
  790. struct in6_addr raddr;
  791. u_int32_t metric;
  792. int i;
  793. u_char nexthop_num;
  794. struct nexthop *nexthop;
  795. struct bgp_nexthop_cache *bnc;
  796. s = zlookup->ibuf;
  797. stream_reset (s);
  798. nbytes = stream_read (s, zlookup->sock, 2);
  799. length = stream_getw (s);
  800. nbytes = stream_read (s, zlookup->sock, length - 2);
  801. command = stream_getc (s);
  802. stream_get (&raddr, s, 16);
  803. metric = stream_getl (s);
  804. nexthop_num = stream_getc (s);
  805. if (nexthop_num)
  806. {
  807. bnc = bnc_new ();
  808. bnc->valid = 1;
  809. bnc->metric = metric;
  810. bnc->nexthop_num = nexthop_num;
  811. for (i = 0; i < nexthop_num; i++)
  812. {
  813. nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
  814. memset (nexthop, 0, sizeof (struct nexthop));
  815. nexthop->type = stream_getc (s);
  816. switch (nexthop->type)
  817. {
  818. case ZEBRA_NEXTHOP_IPV6:
  819. stream_get (&nexthop->gate.ipv6, s, 16);
  820. break;
  821. case ZEBRA_NEXTHOP_IPV6_IFINDEX:
  822. case ZEBRA_NEXTHOP_IPV6_IFNAME:
  823. stream_get (&nexthop->gate.ipv6, s, 16);
  824. nexthop->ifindex = stream_getl (s);
  825. break;
  826. case ZEBRA_NEXTHOP_IFINDEX:
  827. case ZEBRA_NEXTHOP_IFNAME:
  828. nexthop->ifindex = stream_getl (s);
  829. break;
  830. }
  831. bnc_nexthop_add (bnc, nexthop);
  832. }
  833. }
  834. else
  835. return NULL;
  836. return bnc;
  837. }
  838. struct bgp_nexthop_cache *
  839. zlookup_query_ipv6 (struct in6_addr *addr)
  840. {
  841. int ret;
  842. struct stream *s;
  843. /* Check socket. */
  844. if (zlookup->sock < 0)
  845. return NULL;
  846. s = zlookup->obuf;
  847. stream_reset (s);
  848. stream_putw (s, 19);
  849. stream_putc (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
  850. stream_put (s, addr, 16);
  851. ret = writen (zlookup->sock, s->data, 19);
  852. if (ret < 0)
  853. {
  854. zlog_err ("can't write to zlookup->sock");
  855. close (zlookup->sock);
  856. zlookup->sock = -1;
  857. return NULL;
  858. }
  859. if (ret == 0)
  860. {
  861. zlog_err ("zlookup->sock connection closed");
  862. close (zlookup->sock);
  863. zlookup->sock = -1;
  864. return NULL;
  865. }
  866. return zlookup_read_ipv6 ();
  867. }
  868. #endif /* HAVE_IPV6 */
  869. int
  870. bgp_import_check (struct prefix *p, u_int32_t *igpmetric, struct in_addr *igpnexthop)
  871. {
  872. struct stream *s;
  873. int ret;
  874. u_int16_t length;
  875. u_char command;
  876. int nbytes;
  877. struct in_addr addr;
  878. struct in_addr nexthop;
  879. u_int32_t metric = 0;
  880. u_char nexthop_num;
  881. u_char nexthop_type;
  882. /* If lookup connection is not available return valid. */
  883. if (zlookup->sock < 0)
  884. {
  885. if (igpmetric)
  886. *igpmetric = 0;
  887. return 1;
  888. }
  889. /* Send query to the lookup connection */
  890. s = zlookup->obuf;
  891. stream_reset (s);
  892. stream_putw (s, 8);
  893. stream_putc (s, ZEBRA_IPV4_IMPORT_LOOKUP);
  894. stream_putc (s, p->prefixlen);
  895. stream_put_in_addr (s, &p->u.prefix4);
  896. /* Write the packet. */
  897. ret = writen (zlookup->sock, s->data, 8);
  898. if (ret < 0)
  899. {
  900. zlog_err ("can't write to zlookup->sock");
  901. close (zlookup->sock);
  902. zlookup->sock = -1;
  903. return 1;
  904. }
  905. if (ret == 0)
  906. {
  907. zlog_err ("zlookup->sock connection closed");
  908. close (zlookup->sock);
  909. zlookup->sock = -1;
  910. return 1;
  911. }
  912. /* Get result. */
  913. stream_reset (s);
  914. /* Fetch length. */
  915. nbytes = stream_read (s, zlookup->sock, 2);
  916. length = stream_getw (s);
  917. /* Fetch whole data. */
  918. nbytes = stream_read (s, zlookup->sock, length - 2);
  919. command = stream_getc (s);
  920. addr.s_addr = stream_get_ipv4 (s);
  921. metric = stream_getl (s);
  922. nexthop_num = stream_getc (s);
  923. /* Set IGP metric value. */
  924. if (igpmetric)
  925. *igpmetric = metric;
  926. /* If there is nexthop then this is active route. */
  927. if (nexthop_num)
  928. {
  929. nexthop.s_addr = 0;
  930. nexthop_type = stream_getc (s);
  931. if (nexthop_type == ZEBRA_NEXTHOP_IPV4)
  932. {
  933. nexthop.s_addr = stream_get_ipv4 (s);
  934. if (igpnexthop)
  935. *igpnexthop = nexthop;
  936. }
  937. else
  938. *igpnexthop = nexthop;
  939. return 1;
  940. }
  941. else
  942. return 0;
  943. }
  944. /* Scan all configured BGP route then check the route exists in IGP or
  945. not. */
  946. int
  947. bgp_import (struct thread *t)
  948. {
  949. struct bgp_master *bm;
  950. struct bgp *bgp;
  951. struct bgp_node *rn;
  952. struct bgp_static *bgp_static;
  953. struct listnode *nn;
  954. int valid;
  955. u_int32_t metric;
  956. struct in_addr nexthop;
  957. afi_t afi;
  958. safi_t safi;
  959. bgp_import_thread =
  960. thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
  961. bm = bgp_get_master ();
  962. if (! bm)
  963. return 0;
  964. LIST_LOOP (bm->bgp, bgp, nn)
  965. {
  966. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  967. for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
  968. for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
  969. rn = bgp_route_next (rn))
  970. if ((bgp_static = rn->info) != NULL)
  971. {
  972. if (bgp_static->backdoor)
  973. continue;
  974. valid = bgp_static->valid;
  975. metric = bgp_static->igpmetric;
  976. nexthop = bgp_static->igpnexthop;
  977. if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
  978. && afi == AFI_IP && safi == SAFI_UNICAST)
  979. bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
  980. &bgp_static->igpnexthop);
  981. else
  982. {
  983. bgp_static->valid = 1;
  984. bgp_static->igpmetric = 0;
  985. bgp_static->igpnexthop.s_addr = 0;
  986. }
  987. if (bgp_static->valid != valid)
  988. {
  989. if (bgp_static->valid)
  990. bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
  991. else
  992. bgp_static_withdraw (bgp, &rn->p, afi, safi);
  993. }
  994. else if (bgp_static->valid)
  995. {
  996. if (bgp_static->igpmetric != metric
  997. || bgp_static->igpnexthop.s_addr != nexthop.s_addr
  998. || bgp_static->rmap.name)
  999. bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
  1000. }
  1001. }
  1002. }
  1003. return 0;
  1004. }
  1005. /* Connect to zebra for nexthop lookup. */
  1006. int
  1007. zlookup_connect (struct thread *t)
  1008. {
  1009. struct zclient *zlookup;
  1010. zlookup = THREAD_ARG (t);
  1011. zlookup->t_connect = NULL;
  1012. if (zlookup->sock != -1)
  1013. return 0;
  1014. #ifdef HAVE_TCP_ZEBRA
  1015. zlookup->sock = zclient_socket ();
  1016. #else
  1017. zlookup->sock = zclient_socket_un (ZEBRA_SERV_PATH);
  1018. #endif /* HAVE_TCP_ZEBRA */
  1019. if (zlookup->sock < 0)
  1020. return -1;
  1021. return 0;
  1022. }
  1023. /* Check specified multiaccess next-hop. */
  1024. int
  1025. bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
  1026. {
  1027. struct bgp_node *rn1;
  1028. struct bgp_node *rn2;
  1029. struct prefix p1;
  1030. struct prefix p2;
  1031. struct in_addr addr;
  1032. int ret;
  1033. ret = inet_aton (peer, &addr);
  1034. if (! ret)
  1035. return 0;
  1036. memset (&p1, 0, sizeof (struct prefix));
  1037. p1.family = AF_INET;
  1038. p1.prefixlen = IPV4_MAX_BITLEN;
  1039. p1.u.prefix4 = nexthop;
  1040. memset (&p2, 0, sizeof (struct prefix));
  1041. p2.family = AF_INET;
  1042. p2.prefixlen = IPV4_MAX_BITLEN;
  1043. p2.u.prefix4 = addr;
  1044. /* If bgp scan is not enabled, return invalid. */
  1045. if (zlookup->sock < 0)
  1046. return 0;
  1047. rn1 = bgp_node_match (bgp_connected_ipv4, &p1);
  1048. if (! rn1)
  1049. return 0;
  1050. rn2 = bgp_node_match (bgp_connected_ipv4, &p2);
  1051. if (! rn2)
  1052. return 0;
  1053. if (rn1 == rn2)
  1054. return 1;
  1055. return 0;
  1056. }
  1057. DEFUN (bgp_scan_time,
  1058. bgp_scan_time_cmd,
  1059. "bgp scan-time <5-60>",
  1060. "BGP specific commands\n"
  1061. "Configure background scanner interval\n"
  1062. "Scanner interval (seconds)\n")
  1063. {
  1064. bgp_scan_interval = atoi (argv[0]);
  1065. if (bgp_scan_thread)
  1066. {
  1067. thread_cancel (bgp_scan_thread);
  1068. bgp_scan_thread =
  1069. thread_add_timer (master, bgp_scan, NULL, bgp_scan_interval);
  1070. }
  1071. return CMD_SUCCESS;
  1072. }
  1073. DEFUN (no_bgp_scan_time,
  1074. no_bgp_scan_time_cmd,
  1075. "no bgp scan-time",
  1076. NO_STR
  1077. "BGP specific commands\n"
  1078. "Configure background scanner interval\n")
  1079. {
  1080. bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
  1081. if (bgp_scan_thread)
  1082. {
  1083. thread_cancel (bgp_scan_thread);
  1084. bgp_scan_thread =
  1085. thread_add_timer (master, bgp_scan, NULL, bgp_scan_interval);
  1086. }
  1087. return CMD_SUCCESS;
  1088. }
  1089. ALIAS (no_bgp_scan_time,
  1090. no_bgp_scan_time_val_cmd,
  1091. "no bgp scan-time <5-60>",
  1092. NO_STR
  1093. "BGP specific commands\n"
  1094. "Configure background scanner interval\n"
  1095. "Scanner interval (seconds)\n")
  1096. DEFUN (show_ip_bgp_scan,
  1097. show_ip_bgp_scan_cmd,
  1098. "show ip bgp scan",
  1099. SHOW_STR
  1100. IP_STR
  1101. BGP_STR
  1102. "BGP scan status\n")
  1103. {
  1104. struct bgp_node *rn;
  1105. struct bgp_nexthop_cache *bnc;
  1106. if (bgp_scan_thread)
  1107. vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
  1108. else
  1109. vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
  1110. vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
  1111. vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
  1112. for (rn = bgp_table_top (bgp_nexthop_cache_ipv4); rn; rn = bgp_route_next (rn))
  1113. if ((bnc = rn->info) != NULL)
  1114. {
  1115. if (bnc->valid)
  1116. vty_out (vty, " %s valid [IGP metric %d]%s",
  1117. inet_ntoa (rn->p.u.prefix4), bnc->metric, VTY_NEWLINE);
  1118. else
  1119. vty_out (vty, " %s invalid%s",
  1120. inet_ntoa (rn->p.u.prefix4), VTY_NEWLINE);
  1121. }
  1122. #ifdef HAVE_IPV6
  1123. {
  1124. char buf[BUFSIZ];
  1125. for (rn = bgp_table_top (bgp_nexthop_cache_ipv6); rn; rn = bgp_route_next (rn))
  1126. if ((bnc = rn->info) != NULL)
  1127. {
  1128. if (bnc->valid)
  1129. vty_out (vty, " %s valid [IGP metric %d]%s",
  1130. inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
  1131. bnc->metric, VTY_NEWLINE);
  1132. else
  1133. vty_out (vty, " %s invalid%s",
  1134. inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
  1135. VTY_NEWLINE);
  1136. }
  1137. }
  1138. #endif /* HAVE_IPV6 */
  1139. vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
  1140. for (rn = bgp_table_top (bgp_connected_ipv4); rn; rn = bgp_route_next (rn))
  1141. if (rn->info != NULL)
  1142. vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
  1143. VTY_NEWLINE);
  1144. #ifdef HAVE_IPV6
  1145. {
  1146. char buf[BUFSIZ];
  1147. for (rn = bgp_table_top (bgp_connected_ipv6); rn; rn = bgp_route_next (rn))
  1148. if (rn->info != NULL)
  1149. vty_out (vty, " %s/%d%s",
  1150. inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
  1151. rn->p.prefixlen,
  1152. VTY_NEWLINE);
  1153. }
  1154. #endif /* HAVE_IPV6 */
  1155. return CMD_SUCCESS;
  1156. }
  1157. int
  1158. bgp_config_write_scan_time (struct vty *vty)
  1159. {
  1160. if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
  1161. vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
  1162. return CMD_SUCCESS;
  1163. }
  1164. void
  1165. bgp_scan_init ()
  1166. {
  1167. zlookup = zclient_new ();
  1168. zlookup->sock = -1;
  1169. zlookup->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
  1170. zlookup->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
  1171. zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
  1172. bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
  1173. bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
  1174. cache1 = bgp_table_init ();
  1175. cache2 = bgp_table_init ();
  1176. bgp_nexthop_cache_ipv4 = cache1;
  1177. bgp_connected_ipv4 = bgp_table_init ();
  1178. #ifdef HAVE_IPV6
  1179. cache6_1 = bgp_table_init ();
  1180. cache6_2 = bgp_table_init ();
  1181. bgp_nexthop_cache_ipv6 = cache6_1;
  1182. bgp_connected_ipv6 = bgp_table_init ();
  1183. #endif /* HAVE_IPV6 */
  1184. /* Make BGP scan thread. */
  1185. bgp_scan_thread = thread_add_timer (master, bgp_scan, NULL, bgp_scan_interval);
  1186. /* Make BGP import there. */
  1187. bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
  1188. install_element (BGP_NODE, &bgp_scan_time_cmd);
  1189. install_element (BGP_NODE, &no_bgp_scan_time_cmd);
  1190. install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
  1191. install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
  1192. install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
  1193. }