bgp_zebra.c 28 KB

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