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 "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 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 (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 (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 (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 (master);
  1194. zlookup->sock = -1;
  1195. zlookup->t_connect = thread_add_event (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 (master, bgp_scan_timer,
  1210. NULL, bgp_scan_interval);
  1211. /* Make BGP import there. */
  1212. bgp_import_thread = thread_add_timer (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. /* Only the current one needs to be reset. */
  1226. bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP]);
  1227. bgp_table_unlock (cache1_table[AFI_IP]);
  1228. cache1_table[AFI_IP] = NULL;
  1229. bgp_table_unlock (cache2_table[AFI_IP]);
  1230. cache2_table[AFI_IP] = NULL;
  1231. bgp_table_unlock (bgp_connected_table[AFI_IP]);
  1232. bgp_connected_table[AFI_IP] = NULL;
  1233. #ifdef HAVE_IPV6
  1234. /* Only the current one needs to be reset. */
  1235. bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP6]);
  1236. bgp_table_unlock (cache1_table[AFI_IP6]);
  1237. cache1_table[AFI_IP6] = NULL;
  1238. bgp_table_unlock (cache2_table[AFI_IP6]);
  1239. cache2_table[AFI_IP6] = NULL;
  1240. bgp_table_unlock (bgp_connected_table[AFI_IP6]);
  1241. bgp_connected_table[AFI_IP6] = NULL;
  1242. #endif /* HAVE_IPV6 */
  1243. }