bgp_zebra.c 27 KB

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