bgp_zebra.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. /* zebra client
  2. Copyright (C) 1997, 98, 99 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
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <zebra.h>
  17. #include "command.h"
  18. #include "stream.h"
  19. #include "network.h"
  20. #include "prefix.h"
  21. #include "log.h"
  22. #include "sockunion.h"
  23. #include "zclient.h"
  24. #include "routemap.h"
  25. #include "thread.h"
  26. #include "filter.h"
  27. #include "bgpd/bgpd.h"
  28. #include "bgpd/bgp_route.h"
  29. #include "bgpd/bgp_attr.h"
  30. #include "bgpd/bgp_nexthop.h"
  31. #include "bgpd/bgp_zebra.h"
  32. #include "bgpd/bgp_fsm.h"
  33. #include "bgpd/bgp_debug.h"
  34. #include "bgpd/bgp_mpath.h"
  35. #include "bgpd/bgp_nexthop.h"
  36. #include "bgpd/bgp_nht.h"
  37. /* All information about zebra. */
  38. struct zclient *zclient = NULL;
  39. struct in_addr router_id_zebra;
  40. /* Growable buffer for nexthops sent to zebra */
  41. struct stream *bgp_nexthop_buf = NULL;
  42. struct stream *bgp_ifindices_buf = NULL;
  43. int zclient_num_connects;
  44. /* Router-id update message from zebra. */
  45. static int
  46. bgp_router_id_update (int command, struct zclient *zclient, zebra_size_t length,
  47. vrf_id_t vrf_id)
  48. {
  49. struct prefix router_id;
  50. zebra_router_id_update_read(zclient->ibuf,&router_id);
  51. if (BGP_DEBUG(zebra, ZEBRA))
  52. {
  53. char buf[128];
  54. prefix2str(&router_id, buf, sizeof(buf));
  55. zlog_debug("Zebra rcvd: router id update %s", buf);
  56. }
  57. router_id_zebra = router_id.u.prefix4;
  58. bgp_router_id_zebra_bump ();
  59. return 0;
  60. }
  61. /* Nexthop update message from zebra. */
  62. static int
  63. bgp_read_nexthop_update (int command, struct zclient *zclient,
  64. zebra_size_t length, vrf_id_t vrf_id)
  65. {
  66. bgp_parse_nexthop_update();
  67. return 0;
  68. }
  69. /* Inteface addition message from zebra. */
  70. static int
  71. bgp_interface_add (int command, struct zclient *zclient, zebra_size_t length,
  72. vrf_id_t vrf_id)
  73. {
  74. struct interface *ifp;
  75. ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
  76. if (BGP_DEBUG(zebra, ZEBRA) && ifp)
  77. zlog_debug("Zebra rcvd: interface add %s", ifp->name);
  78. return 0;
  79. }
  80. static int
  81. bgp_interface_delete (int command, struct zclient *zclient,
  82. zebra_size_t length, vrf_id_t vrf_id)
  83. {
  84. struct stream *s;
  85. struct interface *ifp;
  86. s = zclient->ibuf;
  87. ifp = zebra_interface_state_read (s, vrf_id);
  88. if (! ifp)
  89. return 0;
  90. ifp->ifindex = IFINDEX_INTERNAL;
  91. if (BGP_DEBUG(zebra, ZEBRA))
  92. zlog_debug("Zebra rcvd: interface delete %s", ifp->name);
  93. return 0;
  94. }
  95. static int
  96. bgp_interface_up (int command, struct zclient *zclient, zebra_size_t length,
  97. vrf_id_t vrf_id)
  98. {
  99. struct stream *s;
  100. struct interface *ifp;
  101. struct connected *c;
  102. struct listnode *node, *nnode;
  103. s = zclient->ibuf;
  104. ifp = zebra_interface_state_read (s, vrf_id);
  105. if (! ifp)
  106. return 0;
  107. if (BGP_DEBUG(zebra, ZEBRA))
  108. zlog_debug("Zebra rcvd: interface %s up", ifp->name);
  109. for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
  110. bgp_connected_add (c);
  111. return 0;
  112. }
  113. static int
  114. bgp_interface_down (int command, struct zclient *zclient, zebra_size_t length,
  115. vrf_id_t vrf_id)
  116. {
  117. struct stream *s;
  118. struct interface *ifp;
  119. struct connected *c;
  120. struct listnode *node, *nnode;
  121. s = zclient->ibuf;
  122. ifp = zebra_interface_state_read (s, vrf_id);
  123. if (! ifp)
  124. return 0;
  125. if (BGP_DEBUG(zebra, ZEBRA))
  126. zlog_debug("Zebra rcvd: interface %s down", ifp->name);
  127. for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
  128. bgp_connected_delete (c);
  129. /* Fast external-failover */
  130. {
  131. struct listnode *mnode;
  132. struct bgp *bgp;
  133. struct peer *peer;
  134. for (ALL_LIST_ELEMENTS_RO (bm->bgp, mnode, bgp))
  135. {
  136. if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
  137. continue;
  138. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  139. {
  140. if (peer->gtsm_hops != 1 && peer_ttl (peer) != 1)
  141. continue;
  142. if (ifp == peer->nexthop.ifp)
  143. BGP_EVENT_ADD (peer, BGP_Stop);
  144. }
  145. }
  146. }
  147. return 0;
  148. }
  149. static int
  150. bgp_interface_address_add (int command, struct zclient *zclient,
  151. zebra_size_t length, vrf_id_t vrf_id)
  152. {
  153. struct connected *ifc;
  154. ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
  155. if (ifc == NULL)
  156. return 0;
  157. if (BGP_DEBUG(zebra, ZEBRA))
  158. {
  159. char buf[128];
  160. prefix2str(ifc->address, buf, sizeof(buf));
  161. zlog_debug("Zebra rcvd: interface %s address add %s",
  162. ifc->ifp->name, buf);
  163. }
  164. if (if_is_operative (ifc->ifp))
  165. bgp_connected_add (ifc);
  166. return 0;
  167. }
  168. static int
  169. bgp_interface_address_delete (int command, struct zclient *zclient,
  170. zebra_size_t length, vrf_id_t vrf_id)
  171. {
  172. struct connected *ifc;
  173. ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
  174. if (ifc == NULL)
  175. return 0;
  176. if (BGP_DEBUG(zebra, ZEBRA))
  177. {
  178. char buf[128];
  179. prefix2str(ifc->address, buf, sizeof(buf));
  180. zlog_debug("Zebra rcvd: interface %s address delete %s",
  181. ifc->ifp->name, buf);
  182. }
  183. if (if_is_operative (ifc->ifp))
  184. bgp_connected_delete (ifc);
  185. connected_free (ifc);
  186. return 0;
  187. }
  188. /* Zebra route add and delete treatment. */
  189. static int
  190. zebra_read_ipv4 (int command, struct zclient *zclient, zebra_size_t length,
  191. vrf_id_t vrf_id)
  192. {
  193. struct stream *s;
  194. struct zapi_ipv4 api;
  195. struct in_addr nexthop;
  196. struct prefix_ipv4 p;
  197. unsigned char plength = 0;
  198. s = zclient->ibuf;
  199. nexthop.s_addr = 0;
  200. /* Type, flags, message. */
  201. api.type = stream_getc (s);
  202. api.flags = stream_getc (s);
  203. api.message = stream_getc (s);
  204. /* IPv4 prefix. */
  205. memset (&p, 0, sizeof (struct prefix_ipv4));
  206. p.family = AF_INET;
  207. plength = stream_getc (s);
  208. p.prefixlen = MIN(IPV4_MAX_PREFIXLEN, plength);
  209. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  210. /* Nexthop, ifindex, distance, metric. */
  211. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  212. {
  213. api.nexthop_num = stream_getc (s);
  214. nexthop.s_addr = stream_get_ipv4 (s);
  215. }
  216. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
  217. {
  218. api.ifindex_num = stream_getc (s);
  219. stream_getl (s); /* ifindex, unused */
  220. }
  221. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  222. api.distance = stream_getc (s);
  223. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  224. api.metric = stream_getl (s);
  225. else
  226. api.metric = 0;
  227. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
  228. api.tag = stream_getl (s);
  229. else
  230. api.tag = 0;
  231. if (command == ZEBRA_IPV4_ROUTE_ADD)
  232. {
  233. if (BGP_DEBUG(zebra, ZEBRA))
  234. {
  235. char buf[2][INET_ADDRSTRLEN];
  236. zlog_debug("Zebra rcvd: IPv4 route add %s %s/%d nexthop %s metric %u tag %d",
  237. zebra_route_string(api.type),
  238. inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
  239. p.prefixlen,
  240. inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
  241. api.metric,
  242. api.tag);
  243. }
  244. bgp_redistribute_add ((struct prefix *)&p, &nexthop, NULL,
  245. api.metric, api.type, api.tag);
  246. }
  247. else
  248. {
  249. if (BGP_DEBUG(zebra, ZEBRA))
  250. {
  251. char buf[2][INET_ADDRSTRLEN];
  252. zlog_debug("Zebra rcvd: IPv4 route delete %s %s/%d "
  253. "nexthop %s metric %u tag %d",
  254. zebra_route_string(api.type),
  255. inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
  256. p.prefixlen,
  257. inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
  258. api.metric,
  259. api.tag);
  260. }
  261. bgp_redistribute_delete((struct prefix *)&p, api.type);
  262. }
  263. return 0;
  264. }
  265. /* Zebra route add and delete treatment. */
  266. static int
  267. zebra_read_ipv6 (int command, struct zclient *zclient, zebra_size_t length,
  268. vrf_id_t vrf_id)
  269. {
  270. struct stream *s;
  271. struct zapi_ipv6 api;
  272. struct in6_addr nexthop;
  273. struct prefix_ipv6 p;
  274. unsigned char plength = 0;
  275. s = zclient->ibuf;
  276. memset (&nexthop, 0, sizeof (struct in6_addr));
  277. /* Type, flags, message. */
  278. api.type = stream_getc (s);
  279. api.flags = stream_getc (s);
  280. api.message = stream_getc (s);
  281. /* IPv6 prefix. */
  282. memset (&p, 0, sizeof (struct prefix_ipv6));
  283. p.family = AF_INET6;
  284. plength = stream_getc (s);
  285. p.prefixlen = MIN(IPV6_MAX_PREFIXLEN, plength);
  286. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  287. /* Nexthop, ifindex, distance, metric. */
  288. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  289. {
  290. api.nexthop_num = stream_getc (s);
  291. stream_get (&nexthop, s, 16);
  292. }
  293. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
  294. {
  295. api.ifindex_num = stream_getc (s);
  296. stream_getl (s); /* ifindex, unused */
  297. }
  298. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  299. api.distance = stream_getc (s);
  300. else
  301. api.distance = 0;
  302. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  303. api.metric = stream_getl (s);
  304. else
  305. api.metric = 0;
  306. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
  307. api.tag = stream_getl (s);
  308. else
  309. api.tag = 0;
  310. /* Simply ignore link-local address. */
  311. if (IN6_IS_ADDR_LINKLOCAL (&p.prefix))
  312. return 0;
  313. if (command == ZEBRA_IPV6_ROUTE_ADD)
  314. {
  315. if (BGP_DEBUG(zebra, ZEBRA))
  316. {
  317. char buf[2][INET6_ADDRSTRLEN];
  318. zlog_debug("Zebra rcvd: IPv6 route add %s %s/%d nexthop %s metric %u tag %d",
  319. zebra_route_string(api.type),
  320. inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
  321. p.prefixlen,
  322. inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
  323. api.metric,
  324. api.tag);
  325. }
  326. bgp_redistribute_add ((struct prefix *)&p, NULL, &nexthop,
  327. api.metric, api.type, api.tag);
  328. }
  329. else
  330. {
  331. if (BGP_DEBUG(zebra, ZEBRA))
  332. {
  333. char buf[2][INET6_ADDRSTRLEN];
  334. zlog_debug("Zebra rcvd: IPv6 route delete %s %s/%d "
  335. "nexthop %s metric %u tag %d",
  336. zebra_route_string(api.type),
  337. inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
  338. p.prefixlen,
  339. inet_ntop(AF_INET6, &nexthop, buf[1], sizeof(buf[1])),
  340. api.metric,
  341. api.tag);
  342. }
  343. bgp_redistribute_delete ((struct prefix *) &p, api.type);
  344. }
  345. return 0;
  346. }
  347. struct interface *
  348. if_lookup_by_ipv4 (struct in_addr *addr)
  349. {
  350. struct listnode *ifnode;
  351. struct listnode *cnode;
  352. struct interface *ifp;
  353. struct connected *connected;
  354. struct prefix_ipv4 p;
  355. struct prefix *cp;
  356. p.family = AF_INET;
  357. p.prefix = *addr;
  358. p.prefixlen = IPV4_MAX_BITLEN;
  359. for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
  360. {
  361. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
  362. {
  363. cp = connected->address;
  364. if (cp->family == AF_INET)
  365. if (prefix_match (cp, (struct prefix *)&p))
  366. return ifp;
  367. }
  368. }
  369. return NULL;
  370. }
  371. struct interface *
  372. if_lookup_by_ipv4_exact (struct in_addr *addr)
  373. {
  374. struct listnode *ifnode;
  375. struct listnode *cnode;
  376. struct interface *ifp;
  377. struct connected *connected;
  378. struct prefix *cp;
  379. for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
  380. {
  381. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
  382. {
  383. cp = connected->address;
  384. if (cp->family == AF_INET)
  385. if (IPV4_ADDR_SAME (&cp->u.prefix4, addr))
  386. return ifp;
  387. }
  388. }
  389. return NULL;
  390. }
  391. struct interface *
  392. if_lookup_by_ipv6 (struct in6_addr *addr)
  393. {
  394. struct listnode *ifnode;
  395. struct listnode *cnode;
  396. struct interface *ifp;
  397. struct connected *connected;
  398. struct prefix_ipv6 p;
  399. struct prefix *cp;
  400. p.family = AF_INET6;
  401. p.prefix = *addr;
  402. p.prefixlen = IPV6_MAX_BITLEN;
  403. for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
  404. {
  405. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
  406. {
  407. cp = connected->address;
  408. if (cp->family == AF_INET6)
  409. if (prefix_match (cp, (struct prefix *)&p))
  410. return ifp;
  411. }
  412. }
  413. return NULL;
  414. }
  415. struct interface *
  416. if_lookup_by_ipv6_exact (struct in6_addr *addr)
  417. {
  418. struct listnode *ifnode;
  419. struct listnode *cnode;
  420. struct interface *ifp;
  421. struct connected *connected;
  422. struct prefix *cp;
  423. for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
  424. {
  425. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
  426. {
  427. cp = connected->address;
  428. if (cp->family == AF_INET6)
  429. if (IPV6_ADDR_SAME (&cp->u.prefix6, addr))
  430. return ifp;
  431. }
  432. }
  433. return NULL;
  434. }
  435. static int
  436. if_get_ipv6_global (struct interface *ifp, struct in6_addr *addr)
  437. {
  438. struct listnode *cnode;
  439. struct connected *connected;
  440. struct prefix *cp;
  441. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
  442. {
  443. cp = connected->address;
  444. if (cp->family == AF_INET6)
  445. if (! IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
  446. {
  447. memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
  448. return 1;
  449. }
  450. }
  451. return 0;
  452. }
  453. static int
  454. if_get_ipv6_local (struct interface *ifp, struct in6_addr *addr)
  455. {
  456. struct listnode *cnode;
  457. struct connected *connected;
  458. struct prefix *cp;
  459. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
  460. {
  461. cp = connected->address;
  462. if (cp->family == AF_INET6)
  463. if (IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
  464. {
  465. memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
  466. return 1;
  467. }
  468. }
  469. return 0;
  470. }
  471. static int
  472. if_get_ipv4_address (struct interface *ifp, struct in_addr *addr)
  473. {
  474. struct listnode *cnode;
  475. struct connected *connected;
  476. struct prefix *cp;
  477. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
  478. {
  479. cp = connected->address;
  480. if ((cp->family == AF_INET) && !ipv4_martian(&(cp->u.prefix4)))
  481. {
  482. *addr = cp->u.prefix4;
  483. return 1;
  484. }
  485. }
  486. return 0;
  487. }
  488. int
  489. bgp_nexthop_set (union sockunion *local, union sockunion *remote,
  490. struct bgp_nexthop *nexthop, struct peer *peer)
  491. {
  492. int ret = 0;
  493. struct interface *ifp = NULL;
  494. memset (nexthop, 0, sizeof (struct bgp_nexthop));
  495. if (!local)
  496. return -1;
  497. if (!remote)
  498. return -1;
  499. if (local->sa.sa_family == AF_INET)
  500. {
  501. nexthop->v4 = local->sin.sin_addr;
  502. if (peer->update_if)
  503. ifp = if_lookup_by_name (peer->update_if);
  504. else
  505. ifp = if_lookup_by_ipv4_exact (&local->sin.sin_addr);
  506. }
  507. if (local->sa.sa_family == AF_INET6)
  508. {
  509. if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
  510. {
  511. if (peer->ifname)
  512. ifp = if_lookup_by_name (peer->ifname);
  513. }
  514. else if (peer->update_if)
  515. ifp = if_lookup_by_name (peer->update_if);
  516. else
  517. ifp = if_lookup_by_ipv6_exact (&local->sin6.sin6_addr);
  518. }
  519. if (!ifp)
  520. return -1;
  521. nexthop->ifp = ifp;
  522. /* IPv4 connection. */
  523. if (local->sa.sa_family == AF_INET)
  524. {
  525. /* IPv6 nexthop*/
  526. ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
  527. /* There is no global nexthop. */
  528. if (!ret)
  529. if_get_ipv6_local (ifp, &nexthop->v6_global);
  530. else
  531. if_get_ipv6_local (ifp, &nexthop->v6_local);
  532. }
  533. /* IPv6 connection. */
  534. if (local->sa.sa_family == AF_INET6)
  535. {
  536. struct interface *direct = NULL;
  537. /* IPv4 nexthop. */
  538. ret = if_get_ipv4_address(ifp, &nexthop->v4);
  539. if (!ret && peer->local_id.s_addr)
  540. nexthop->v4 = peer->local_id;
  541. /* Global address*/
  542. if (! IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
  543. {
  544. memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
  545. IPV6_MAX_BYTELEN);
  546. /* If directory connected set link-local address. */
  547. direct = if_lookup_by_ipv6 (&remote->sin6.sin6_addr);
  548. if (direct)
  549. if_get_ipv6_local (ifp, &nexthop->v6_local);
  550. }
  551. else
  552. /* Link-local address. */
  553. {
  554. ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
  555. /* If there is no global address. Set link-local address as
  556. global. I know this break RFC specification... */
  557. if (!ret)
  558. memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
  559. IPV6_MAX_BYTELEN);
  560. else
  561. memcpy (&nexthop->v6_local, &local->sin6.sin6_addr,
  562. IPV6_MAX_BYTELEN);
  563. }
  564. }
  565. if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr) ||
  566. if_lookup_by_ipv6 (&remote->sin6.sin6_addr))
  567. peer->shared_network = 1;
  568. else
  569. peer->shared_network = 0;
  570. /* KAME stack specific treatment. */
  571. #ifdef KAME
  572. if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_global)
  573. && IN6_LINKLOCAL_IFINDEX (nexthop->v6_global))
  574. {
  575. SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_global, 0);
  576. }
  577. if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_local)
  578. && IN6_LINKLOCAL_IFINDEX (nexthop->v6_local))
  579. {
  580. SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_local, 0);
  581. }
  582. #endif /* KAME */
  583. return ret;
  584. }
  585. void
  586. bgp_zebra_announce (struct prefix *p, struct bgp_info *info, struct bgp *bgp, safi_t safi)
  587. {
  588. int flags;
  589. u_char distance;
  590. struct peer *peer;
  591. struct bgp_info *mpinfo;
  592. size_t oldsize, newsize;
  593. u_int32_t nhcount;
  594. route_tag_t tag = 0;
  595. if (zclient->sock < 0)
  596. return;
  597. if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_BGP], VRF_DEFAULT))
  598. return;
  599. flags = 0;
  600. peer = info->peer;
  601. if ((info->attr->extra) && (info->attr->extra->tag != 0))
  602. tag = info->attr->extra->tag;
  603. if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED)
  604. {
  605. SET_FLAG (flags, ZEBRA_FLAG_IBGP);
  606. SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
  607. }
  608. if ((peer->sort == BGP_PEER_EBGP && peer_ttl (peer) != 1)
  609. || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
  610. SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
  611. nhcount = 1 + bgp_info_mpath_count (info);
  612. if (p->family == AF_INET)
  613. {
  614. struct zapi_ipv4 api;
  615. struct in_addr *nexthop;
  616. /* resize nexthop buffer size if necessary */
  617. if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
  618. (sizeof (struct in_addr *) * nhcount))
  619. {
  620. newsize = (sizeof (struct in_addr *) * nhcount);
  621. newsize = stream_resize (bgp_nexthop_buf, newsize);
  622. if (newsize == oldsize)
  623. {
  624. zlog_err ("can't resize nexthop buffer");
  625. return;
  626. }
  627. }
  628. stream_reset (bgp_nexthop_buf);
  629. api.vrf_id = VRF_DEFAULT;
  630. api.flags = flags;
  631. nexthop = &info->attr->nexthop;
  632. stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
  633. for (mpinfo = bgp_info_mpath_first (info); mpinfo;
  634. mpinfo = bgp_info_mpath_next (mpinfo))
  635. {
  636. nexthop = &mpinfo->attr->nexthop;
  637. stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
  638. }
  639. api.type = ZEBRA_ROUTE_BGP;
  640. api.message = 0;
  641. api.safi = safi;
  642. SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
  643. api.nexthop_num = nhcount;
  644. api.nexthop = (struct in_addr **)STREAM_DATA (bgp_nexthop_buf);
  645. api.ifindex_num = 0;
  646. SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
  647. api.metric = info->attr->med;
  648. if (tag)
  649. {
  650. SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
  651. api.tag = tag;
  652. }
  653. distance = bgp_distance_apply (p, info, bgp);
  654. if (distance)
  655. {
  656. SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
  657. api.distance = distance;
  658. }
  659. if (BGP_DEBUG(zebra, ZEBRA))
  660. {
  661. int i;
  662. char buf[2][INET_ADDRSTRLEN];
  663. zlog_debug("Zebra send: IPv4 route add %s/%d nexthop %s metric %u"
  664. " tag %u count %d",
  665. inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
  666. p->prefixlen,
  667. inet_ntop(AF_INET, api.nexthop[0], buf[1], sizeof(buf[1])),
  668. api.metric, api.tag, api.nexthop_num);
  669. for (i = 1; i < api.nexthop_num; i++)
  670. zlog_debug("Zebra send: IPv4 route add [nexthop %d] %s",
  671. i, inet_ntop(AF_INET, api.nexthop[i], buf[1],
  672. sizeof(buf[1])));
  673. }
  674. zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient,
  675. (struct prefix_ipv4 *) p, &api);
  676. }
  677. /* We have to think about a IPv6 link-local address curse. */
  678. if (p->family == AF_INET6)
  679. {
  680. ifindex_t ifindex;
  681. struct in6_addr *nexthop;
  682. struct zapi_ipv6 api;
  683. int valid_nh_count = 0;
  684. /* resize nexthop buffer size if necessary */
  685. if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
  686. (sizeof (struct in6_addr *) * nhcount))
  687. {
  688. newsize = (sizeof (struct in6_addr *) * nhcount);
  689. newsize = stream_resize (bgp_nexthop_buf, newsize);
  690. if (newsize == oldsize)
  691. {
  692. zlog_err ("can't resize nexthop buffer");
  693. return;
  694. }
  695. }
  696. stream_reset (bgp_nexthop_buf);
  697. /* resize ifindices buffer size if necessary */
  698. if ((oldsize = stream_get_size (bgp_ifindices_buf)) <
  699. (sizeof (unsigned int) * nhcount))
  700. {
  701. newsize = (sizeof (unsigned int) * nhcount);
  702. newsize = stream_resize (bgp_ifindices_buf, newsize);
  703. if (newsize == oldsize)
  704. {
  705. zlog_err ("can't resize nexthop buffer");
  706. return;
  707. }
  708. }
  709. stream_reset (bgp_ifindices_buf);
  710. ifindex = 0;
  711. nexthop = NULL;
  712. assert (info->attr->extra);
  713. /* Only global address nexthop exists. */
  714. if (info->attr->extra->mp_nexthop_len == 16)
  715. nexthop = &info->attr->extra->mp_nexthop_global;
  716. /* If both global and link-local address present. */
  717. if (info->attr->extra->mp_nexthop_len == 32)
  718. {
  719. /* Workaround for Cisco's nexthop bug. */
  720. if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->extra->mp_nexthop_global)
  721. && peer->su_remote->sa.sa_family == AF_INET6)
  722. nexthop = &peer->su_remote->sin6.sin6_addr;
  723. else
  724. nexthop = &info->attr->extra->mp_nexthop_local;
  725. if (info->peer->nexthop.ifp)
  726. ifindex = info->peer->nexthop.ifp->ifindex;
  727. }
  728. if (nexthop == NULL)
  729. return;
  730. if (!ifindex)
  731. {
  732. if (info->peer->ifname)
  733. ifindex = ifname2ifindex (info->peer->ifname);
  734. else if (info->peer->nexthop.ifp)
  735. ifindex = info->peer->nexthop.ifp->ifindex;
  736. }
  737. stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
  738. stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
  739. valid_nh_count++;
  740. for (mpinfo = bgp_info_mpath_first (info); mpinfo;
  741. mpinfo = bgp_info_mpath_next (mpinfo))
  742. {
  743. ifindex = 0;
  744. /* Only global address nexthop exists. */
  745. if (mpinfo->attr->extra->mp_nexthop_len == 16)
  746. nexthop = &mpinfo->attr->extra->mp_nexthop_global;
  747. /* If both global and link-local address present. */
  748. if (mpinfo->attr->extra->mp_nexthop_len == 32)
  749. {
  750. /* Workaround for Cisco's nexthop bug. */
  751. if (IN6_IS_ADDR_UNSPECIFIED (&mpinfo->attr->extra->mp_nexthop_global)
  752. && mpinfo->peer->su_remote->sa.sa_family == AF_INET6)
  753. {
  754. nexthop = &mpinfo->peer->su_remote->sin6.sin6_addr;
  755. }
  756. else
  757. {
  758. nexthop = &mpinfo->attr->extra->mp_nexthop_local;
  759. }
  760. if (mpinfo->peer->nexthop.ifp)
  761. {
  762. ifindex = mpinfo->peer->nexthop.ifp->ifindex;
  763. }
  764. }
  765. if (nexthop == NULL)
  766. {
  767. continue;
  768. }
  769. if (!ifindex)
  770. {
  771. if (mpinfo->peer->ifname)
  772. {
  773. ifindex = if_nametoindex (mpinfo->peer->ifname);
  774. }
  775. else if (mpinfo->peer->nexthop.ifp)
  776. {
  777. ifindex = mpinfo->peer->nexthop.ifp->ifindex;
  778. }
  779. }
  780. if (ifindex == 0)
  781. {
  782. continue;
  783. }
  784. stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
  785. stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
  786. valid_nh_count++;
  787. }
  788. /* Make Zebra API structure. */
  789. api.vrf_id = VRF_DEFAULT;
  790. api.flags = flags;
  791. api.type = ZEBRA_ROUTE_BGP;
  792. api.message = 0;
  793. api.safi = safi;
  794. SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
  795. api.nexthop_num = valid_nh_count;
  796. api.nexthop = (struct in6_addr **)STREAM_DATA (bgp_nexthop_buf);
  797. SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
  798. api.ifindex_num = valid_nh_count;
  799. api.ifindex = (ifindex_t *)STREAM_DATA (bgp_ifindices_buf);
  800. SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
  801. api.metric = info->attr->med;
  802. distance = ipv6_bgp_distance_apply (p, info, bgp);
  803. if (distance)
  804. {
  805. SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
  806. api.distance = distance;
  807. }
  808. if (tag)
  809. {
  810. SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
  811. api.tag = tag;
  812. }
  813. if (BGP_DEBUG(zebra, ZEBRA))
  814. {
  815. char buf[2][INET6_ADDRSTRLEN];
  816. zlog_debug("Zebra send: IPv6 route add %s/%d nexthop %s metric %u"
  817. " tag %u",
  818. inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
  819. p->prefixlen,
  820. inet_ntop(AF_INET6, nexthop, buf[1], sizeof(buf[1])),
  821. api.metric, api.tag);
  822. }
  823. zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD, zclient,
  824. (struct prefix_ipv6 *) p, &api);
  825. }
  826. }
  827. void
  828. bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info, safi_t safi)
  829. {
  830. int flags;
  831. struct peer *peer;
  832. if (zclient->sock < 0)
  833. return;
  834. if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_BGP], VRF_DEFAULT))
  835. return;
  836. peer = info->peer;
  837. flags = 0;
  838. if (peer->sort == BGP_PEER_IBGP)
  839. {
  840. SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
  841. SET_FLAG (flags, ZEBRA_FLAG_IBGP);
  842. }
  843. if ((peer->sort == BGP_PEER_EBGP && peer_ttl (peer) != 1)
  844. || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
  845. SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
  846. if (p->family == AF_INET)
  847. {
  848. struct zapi_ipv4 api;
  849. api.vrf_id = VRF_DEFAULT;
  850. api.flags = flags;
  851. api.type = ZEBRA_ROUTE_BGP;
  852. api.message = 0;
  853. api.safi = safi;
  854. api.nexthop_num = 0;
  855. api.ifindex_num = 0;
  856. SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
  857. api.metric = info->attr->med;
  858. if ((info->attr->extra) && (info->attr->extra->tag != 0))
  859. {
  860. SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
  861. api.tag = info->attr->extra->tag;
  862. }
  863. if (BGP_DEBUG(zebra, ZEBRA))
  864. {
  865. char buf[2][INET_ADDRSTRLEN];
  866. zlog_debug("Zebra send: IPv4 route delete %s/%d metric %u tag %d",
  867. inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
  868. p->prefixlen,
  869. api.metric,
  870. api.tag);
  871. }
  872. zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient,
  873. (struct prefix_ipv4 *) p, &api);
  874. }
  875. /* We have to think about a IPv6 link-local address curse. */
  876. if (p->family == AF_INET6)
  877. {
  878. struct zapi_ipv6 api;
  879. api.vrf_id = VRF_DEFAULT;
  880. api.flags = flags;
  881. api.type = ZEBRA_ROUTE_BGP;
  882. api.message = 0;
  883. api.safi = safi;
  884. api.nexthop_num = 0;
  885. api.ifindex_num = 0;
  886. SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
  887. api.metric = info->attr->med;
  888. if ((info->attr->extra) && (info->attr->extra->tag != 0))
  889. {
  890. SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
  891. api.tag = info->attr->extra->tag;
  892. }
  893. if (BGP_DEBUG(zebra, ZEBRA))
  894. {
  895. char buf[2][INET6_ADDRSTRLEN];
  896. zlog_debug("Zebra send: IPv6 route delete %s/%d metric %u tag %d",
  897. inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
  898. p->prefixlen,
  899. api.metric,
  900. api.tag);
  901. }
  902. zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient,
  903. (struct prefix_ipv6 *) p, &api);
  904. }
  905. }
  906. /* Other routes redistribution into BGP. */
  907. int
  908. bgp_redistribute_set (struct bgp *bgp, afi_t afi, int type)
  909. {
  910. /* Set flag to BGP instance. */
  911. bgp->redist[afi][type] = 1;
  912. /* Return if already redistribute flag is set. */
  913. if (vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT))
  914. return CMD_WARNING;
  915. vrf_bitmap_set (zclient->redist[type], VRF_DEFAULT);
  916. /* Return if zebra connection is not established. */
  917. if (zclient->sock < 0)
  918. return CMD_WARNING;
  919. if (BGP_DEBUG(zebra, ZEBRA))
  920. zlog_debug("Zebra send: redistribute add %s", zebra_route_string(type));
  921. /* Send distribute add message to zebra. */
  922. zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT);
  923. return CMD_SUCCESS;
  924. }
  925. /* Redistribute with route-map specification. */
  926. int
  927. bgp_redistribute_rmap_set (struct bgp *bgp, afi_t afi, int type,
  928. const char *name)
  929. {
  930. if (bgp->rmap[afi][type].name
  931. && (strcmp (bgp->rmap[afi][type].name, name) == 0))
  932. return 0;
  933. if (bgp->rmap[afi][type].name)
  934. free (bgp->rmap[afi][type].name);
  935. bgp->rmap[afi][type].name = strdup (name);
  936. bgp->rmap[afi][type].map = route_map_lookup_by_name (name);
  937. return 1;
  938. }
  939. /* Redistribute with metric specification. */
  940. int
  941. bgp_redistribute_metric_set (struct bgp *bgp, afi_t afi, int type,
  942. u_int32_t metric)
  943. {
  944. if (bgp->redist_metric_flag[afi][type]
  945. && bgp->redist_metric[afi][type] == metric)
  946. return 0;
  947. bgp->redist_metric_flag[afi][type] = 1;
  948. bgp->redist_metric[afi][type] = metric;
  949. return 1;
  950. }
  951. /* Unset redistribution. */
  952. int
  953. bgp_redistribute_unset (struct bgp *bgp, afi_t afi, int type)
  954. {
  955. /* Unset flag from BGP instance. */
  956. bgp->redist[afi][type] = 0;
  957. /* Unset route-map. */
  958. if (bgp->rmap[afi][type].name)
  959. free (bgp->rmap[afi][type].name);
  960. bgp->rmap[afi][type].name = NULL;
  961. bgp->rmap[afi][type].map = NULL;
  962. /* Unset metric. */
  963. bgp->redist_metric_flag[afi][type] = 0;
  964. bgp->redist_metric[afi][type] = 0;
  965. /* Return if zebra connection is disabled. */
  966. if (! vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT))
  967. return CMD_WARNING;
  968. vrf_bitmap_unset (zclient->redist[type], VRF_DEFAULT);
  969. if (bgp->redist[AFI_IP][type] == 0
  970. && bgp->redist[AFI_IP6][type] == 0
  971. && zclient->sock >= 0)
  972. {
  973. /* Send distribute delete message to zebra. */
  974. if (BGP_DEBUG(zebra, ZEBRA))
  975. zlog_debug("Zebra send: redistribute delete %s",
  976. zebra_route_string(type));
  977. zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, type,
  978. VRF_DEFAULT);
  979. }
  980. /* Withdraw redistributed routes from current BGP's routing table. */
  981. bgp_redistribute_withdraw (bgp, afi, type);
  982. return CMD_SUCCESS;
  983. }
  984. void
  985. bgp_zclient_reset (void)
  986. {
  987. zclient_reset (zclient);
  988. }
  989. static void
  990. bgp_zebra_connected (struct zclient *zclient)
  991. {
  992. zclient_num_connects++;
  993. zclient_send_requests (zclient, VRF_DEFAULT);
  994. }
  995. void
  996. bgp_zebra_init (struct thread_master *master)
  997. {
  998. zclient_num_connects = 0;
  999. /* Set default values. */
  1000. zclient = zclient_new (master);
  1001. zclient_init (zclient, ZEBRA_ROUTE_BGP);
  1002. zclient->zebra_connected = bgp_zebra_connected;
  1003. zclient->router_id_update = bgp_router_id_update;
  1004. zclient->interface_add = bgp_interface_add;
  1005. zclient->interface_delete = bgp_interface_delete;
  1006. zclient->interface_address_add = bgp_interface_address_add;
  1007. zclient->interface_address_delete = bgp_interface_address_delete;
  1008. zclient->ipv4_route_add = zebra_read_ipv4;
  1009. zclient->ipv4_route_delete = zebra_read_ipv4;
  1010. zclient->interface_up = bgp_interface_up;
  1011. zclient->interface_down = bgp_interface_down;
  1012. zclient->ipv6_route_add = zebra_read_ipv6;
  1013. zclient->ipv6_route_delete = zebra_read_ipv6;
  1014. zclient->nexthop_update = bgp_read_nexthop_update;
  1015. bgp_nexthop_buf = stream_new(BGP_NEXTHOP_BUF_SIZE);
  1016. bgp_ifindices_buf = stream_new(BGP_IFINDICES_BUF_SIZE);
  1017. }
  1018. void
  1019. bgp_zebra_destroy(void)
  1020. {
  1021. if (zclient == NULL)
  1022. return;
  1023. zclient_stop(zclient);
  1024. zclient_free(zclient);
  1025. zclient = NULL;
  1026. }
  1027. int
  1028. bgp_zebra_num_connects(void)
  1029. {
  1030. return zclient_num_connects;
  1031. }