bgp_zebra.c 31 KB

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