bgp_nexthop.c 31 KB

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