bgp_nexthop.c 30 KB

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