bgp_zebra.c 27 KB

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