bgp_nexthop.c 34 KB

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