bgp_nexthop.c 34 KB

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