bgp_zebra.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. #ifdef HAVE_IPV6
  250. /* Zebra route add and delete treatment. */
  251. static int
  252. zebra_read_ipv6 (int command, struct zclient *zclient, zebra_size_t length,
  253. vrf_id_t vrf_id)
  254. {
  255. struct stream *s;
  256. struct zapi_ipv6 api;
  257. struct in6_addr nexthop;
  258. struct prefix_ipv6 p;
  259. s = zclient->ibuf;
  260. memset (&nexthop, 0, sizeof (struct in6_addr));
  261. /* Type, flags, message. */
  262. api.type = stream_getc (s);
  263. api.flags = stream_getc (s);
  264. api.message = stream_getc (s);
  265. /* IPv6 prefix. */
  266. memset (&p, 0, sizeof (struct prefix_ipv6));
  267. p.family = AF_INET6;
  268. p.prefixlen = stream_getc (s);
  269. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  270. /* Nexthop, ifindex, distance, metric. */
  271. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  272. {
  273. api.nexthop_num = stream_getc (s);
  274. stream_get (&nexthop, s, 16);
  275. }
  276. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
  277. {
  278. api.ifindex_num = stream_getc (s);
  279. stream_getl (s); /* ifindex, unused */
  280. }
  281. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  282. api.distance = stream_getc (s);
  283. else
  284. api.distance = 0;
  285. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  286. api.metric = stream_getl (s);
  287. else
  288. api.metric = 0;
  289. /* Simply ignore link-local address. */
  290. if (IN6_IS_ADDR_LINKLOCAL (&p.prefix))
  291. return 0;
  292. if (command == ZEBRA_IPV6_ROUTE_ADD)
  293. {
  294. if (BGP_DEBUG(zebra, ZEBRA))
  295. {
  296. char buf[2][INET6_ADDRSTRLEN];
  297. zlog_debug("Zebra rcvd: IPv6 route add %s %s/%d nexthop %s metric %u",
  298. zebra_route_string(api.type),
  299. inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
  300. p.prefixlen,
  301. inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
  302. api.metric);
  303. }
  304. bgp_redistribute_add ((struct prefix *)&p, NULL, &nexthop,
  305. api.metric, api.type);
  306. }
  307. else
  308. {
  309. if (BGP_DEBUG(zebra, ZEBRA))
  310. {
  311. char buf[2][INET6_ADDRSTRLEN];
  312. zlog_debug("Zebra rcvd: IPv6 route delete %s %s/%d "
  313. "nexthop %s metric %u",
  314. zebra_route_string(api.type),
  315. inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
  316. p.prefixlen,
  317. inet_ntop(AF_INET6, &nexthop, buf[1], sizeof(buf[1])),
  318. api.metric);
  319. }
  320. bgp_redistribute_delete ((struct prefix *) &p, api.type);
  321. }
  322. return 0;
  323. }
  324. #endif /* HAVE_IPV6 */
  325. struct interface *
  326. if_lookup_by_ipv4 (struct in_addr *addr)
  327. {
  328. struct listnode *ifnode;
  329. struct listnode *cnode;
  330. struct interface *ifp;
  331. struct connected *connected;
  332. struct prefix_ipv4 p;
  333. struct prefix *cp;
  334. p.family = AF_INET;
  335. p.prefix = *addr;
  336. p.prefixlen = IPV4_MAX_BITLEN;
  337. for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
  338. {
  339. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
  340. {
  341. cp = connected->address;
  342. if (cp->family == AF_INET)
  343. if (prefix_match (cp, (struct prefix *)&p))
  344. return ifp;
  345. }
  346. }
  347. return NULL;
  348. }
  349. struct interface *
  350. if_lookup_by_ipv4_exact (struct in_addr *addr)
  351. {
  352. struct listnode *ifnode;
  353. struct listnode *cnode;
  354. struct interface *ifp;
  355. struct connected *connected;
  356. struct prefix *cp;
  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 (IPV4_ADDR_SAME (&cp->u.prefix4, addr))
  364. return ifp;
  365. }
  366. }
  367. return NULL;
  368. }
  369. #ifdef HAVE_IPV6
  370. struct interface *
  371. if_lookup_by_ipv6 (struct in6_addr *addr)
  372. {
  373. struct listnode *ifnode;
  374. struct listnode *cnode;
  375. struct interface *ifp;
  376. struct connected *connected;
  377. struct prefix_ipv6 p;
  378. struct prefix *cp;
  379. p.family = AF_INET6;
  380. p.prefix = *addr;
  381. p.prefixlen = IPV6_MAX_BITLEN;
  382. for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
  383. {
  384. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
  385. {
  386. cp = connected->address;
  387. if (cp->family == AF_INET6)
  388. if (prefix_match (cp, (struct prefix *)&p))
  389. return ifp;
  390. }
  391. }
  392. return NULL;
  393. }
  394. struct interface *
  395. if_lookup_by_ipv6_exact (struct in6_addr *addr)
  396. {
  397. struct listnode *ifnode;
  398. struct listnode *cnode;
  399. struct interface *ifp;
  400. struct connected *connected;
  401. struct prefix *cp;
  402. for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
  403. {
  404. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
  405. {
  406. cp = connected->address;
  407. if (cp->family == AF_INET6)
  408. if (IPV6_ADDR_SAME (&cp->u.prefix6, addr))
  409. return ifp;
  410. }
  411. }
  412. return NULL;
  413. }
  414. static int
  415. if_get_ipv6_global (struct interface *ifp, struct in6_addr *addr)
  416. {
  417. struct listnode *cnode;
  418. struct connected *connected;
  419. struct prefix *cp;
  420. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
  421. {
  422. cp = connected->address;
  423. if (cp->family == AF_INET6)
  424. if (! IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
  425. {
  426. memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
  427. return 1;
  428. }
  429. }
  430. return 0;
  431. }
  432. static int
  433. if_get_ipv6_local (struct interface *ifp, struct in6_addr *addr)
  434. {
  435. struct listnode *cnode;
  436. struct connected *connected;
  437. struct prefix *cp;
  438. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
  439. {
  440. cp = connected->address;
  441. if (cp->family == AF_INET6)
  442. if (IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
  443. {
  444. memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
  445. return 1;
  446. }
  447. }
  448. return 0;
  449. }
  450. #endif /* HAVE_IPV6 */
  451. static int
  452. if_get_ipv4_address (struct interface *ifp, struct in_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_INET) && !ipv4_martian(&(cp->u.prefix4)))
  461. {
  462. *addr = cp->u.prefix4;
  463. return 1;
  464. }
  465. }
  466. return 0;
  467. }
  468. int
  469. bgp_nexthop_set (union sockunion *local, union sockunion *remote,
  470. struct bgp_nexthop *nexthop, struct peer *peer)
  471. {
  472. int ret = 0;
  473. struct interface *ifp = NULL;
  474. memset (nexthop, 0, sizeof (struct bgp_nexthop));
  475. if (!local)
  476. return -1;
  477. if (!remote)
  478. return -1;
  479. if (local->sa.sa_family == AF_INET)
  480. {
  481. nexthop->v4 = local->sin.sin_addr;
  482. ifp = if_lookup_by_ipv4 (&local->sin.sin_addr);
  483. }
  484. #ifdef HAVE_IPV6
  485. if (local->sa.sa_family == AF_INET6)
  486. {
  487. if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
  488. {
  489. if (peer->ifname)
  490. ifp = if_lookup_by_name (peer->ifname);
  491. }
  492. else
  493. ifp = if_lookup_by_ipv6 (&local->sin6.sin6_addr);
  494. }
  495. #endif /* HAVE_IPV6 */
  496. if (!ifp)
  497. return -1;
  498. nexthop->ifp = ifp;
  499. /* IPv4 connection. */
  500. if (local->sa.sa_family == AF_INET)
  501. {
  502. #ifdef HAVE_IPV6
  503. /* IPv6 nexthop*/
  504. ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
  505. /* There is no global nexthop. */
  506. if (!ret)
  507. if_get_ipv6_local (ifp, &nexthop->v6_global);
  508. else
  509. if_get_ipv6_local (ifp, &nexthop->v6_local);
  510. #endif /* HAVE_IPV6 */
  511. }
  512. #ifdef HAVE_IPV6
  513. /* IPv6 connection. */
  514. if (local->sa.sa_family == AF_INET6)
  515. {
  516. struct interface *direct = NULL;
  517. /* IPv4 nexthop. */
  518. ret = if_get_ipv4_address(ifp, &nexthop->v4);
  519. if (!ret && peer->local_id.s_addr)
  520. nexthop->v4 = peer->local_id;
  521. /* Global address*/
  522. if (! IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
  523. {
  524. memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
  525. IPV6_MAX_BYTELEN);
  526. /* If directory connected set link-local address. */
  527. direct = if_lookup_by_ipv6 (&remote->sin6.sin6_addr);
  528. if (direct)
  529. if_get_ipv6_local (ifp, &nexthop->v6_local);
  530. }
  531. else
  532. /* Link-local address. */
  533. {
  534. ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
  535. /* If there is no global address. Set link-local address as
  536. global. I know this break RFC specification... */
  537. if (!ret)
  538. memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
  539. IPV6_MAX_BYTELEN);
  540. else
  541. memcpy (&nexthop->v6_local, &local->sin6.sin6_addr,
  542. IPV6_MAX_BYTELEN);
  543. }
  544. }
  545. if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr) ||
  546. if_lookup_by_ipv6 (&remote->sin6.sin6_addr))
  547. peer->shared_network = 1;
  548. else
  549. peer->shared_network = 0;
  550. /* KAME stack specific treatment. */
  551. #ifdef KAME
  552. if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_global)
  553. && IN6_LINKLOCAL_IFINDEX (nexthop->v6_global))
  554. {
  555. SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_global, 0);
  556. }
  557. if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_local)
  558. && IN6_LINKLOCAL_IFINDEX (nexthop->v6_local))
  559. {
  560. SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_local, 0);
  561. }
  562. #endif /* KAME */
  563. #endif /* HAVE_IPV6 */
  564. return ret;
  565. }
  566. void
  567. bgp_zebra_announce (struct prefix *p, struct bgp_info *info, struct bgp *bgp, safi_t safi)
  568. {
  569. int flags;
  570. u_char distance;
  571. struct peer *peer;
  572. struct bgp_info *mpinfo;
  573. size_t oldsize, newsize;
  574. if (zclient->sock < 0)
  575. return;
  576. if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_BGP], VRF_DEFAULT))
  577. return;
  578. flags = 0;
  579. peer = info->peer;
  580. if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED)
  581. {
  582. SET_FLAG (flags, ZEBRA_FLAG_IBGP);
  583. SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
  584. }
  585. if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
  586. || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
  587. SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
  588. /* resize nexthop buffer size if necessary */
  589. if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
  590. (sizeof (struct in_addr *) * (bgp_info_mpath_count (info) + 1)))
  591. {
  592. newsize = (sizeof (struct in_addr *) * (bgp_info_mpath_count (info) + 1));
  593. newsize = stream_resize (bgp_nexthop_buf, newsize);
  594. if (newsize == oldsize)
  595. {
  596. zlog_err ("can't resize nexthop buffer");
  597. return;
  598. }
  599. }
  600. stream_reset (bgp_nexthop_buf);
  601. if (p->family == AF_INET)
  602. {
  603. struct zapi_ipv4 api;
  604. struct in_addr *nexthop;
  605. api.vrf_id = VRF_DEFAULT;
  606. api.flags = flags;
  607. nexthop = &info->attr->nexthop;
  608. stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
  609. for (mpinfo = bgp_info_mpath_first (info); mpinfo;
  610. mpinfo = bgp_info_mpath_next (mpinfo))
  611. {
  612. nexthop = &mpinfo->attr->nexthop;
  613. stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
  614. }
  615. api.type = ZEBRA_ROUTE_BGP;
  616. api.message = 0;
  617. api.safi = safi;
  618. SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
  619. api.nexthop_num = 1 + bgp_info_mpath_count (info);
  620. api.nexthop = (struct in_addr **)STREAM_DATA (bgp_nexthop_buf);
  621. api.ifindex_num = 0;
  622. SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
  623. api.metric = info->attr->med;
  624. distance = bgp_distance_apply (p, info, bgp);
  625. if (distance)
  626. {
  627. SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
  628. api.distance = distance;
  629. }
  630. if (BGP_DEBUG(zebra, ZEBRA))
  631. {
  632. int i;
  633. char buf[2][INET_ADDRSTRLEN];
  634. zlog_debug("Zebra send: IPv4 route add %s/%d nexthop %s metric %u"
  635. " count %d",
  636. inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
  637. p->prefixlen,
  638. inet_ntop(AF_INET, api.nexthop[0], buf[1], sizeof(buf[1])),
  639. api.metric, api.nexthop_num);
  640. for (i = 1; i < api.nexthop_num; i++)
  641. zlog_debug("Zebra send: IPv4 route add [nexthop %d] %s",
  642. i, inet_ntop(AF_INET, api.nexthop[i], buf[1],
  643. sizeof(buf[1])));
  644. }
  645. zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient,
  646. (struct prefix_ipv4 *) p, &api);
  647. }
  648. #ifdef HAVE_IPV6
  649. /* We have to think about a IPv6 link-local address curse. */
  650. if (p->family == AF_INET6)
  651. {
  652. unsigned int ifindex;
  653. struct in6_addr *nexthop;
  654. struct zapi_ipv6 api;
  655. ifindex = 0;
  656. nexthop = NULL;
  657. assert (info->attr->extra);
  658. /* Only global address nexthop exists. */
  659. if (info->attr->extra->mp_nexthop_len == 16)
  660. nexthop = &info->attr->extra->mp_nexthop_global;
  661. /* If both global and link-local address present. */
  662. if (info->attr->extra->mp_nexthop_len == 32)
  663. {
  664. /* Workaround for Cisco's nexthop bug. */
  665. if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->extra->mp_nexthop_global)
  666. && peer->su_remote->sa.sa_family == AF_INET6)
  667. nexthop = &peer->su_remote->sin6.sin6_addr;
  668. else
  669. nexthop = &info->attr->extra->mp_nexthop_local;
  670. if (info->peer->nexthop.ifp)
  671. ifindex = info->peer->nexthop.ifp->ifindex;
  672. }
  673. if (nexthop == NULL)
  674. return;
  675. if (IN6_IS_ADDR_LINKLOCAL (nexthop) && ! ifindex)
  676. {
  677. if (info->peer->ifname)
  678. ifindex = ifname2ifindex (info->peer->ifname);
  679. else if (info->peer->nexthop.ifp)
  680. ifindex = info->peer->nexthop.ifp->ifindex;
  681. }
  682. /* Make Zebra API structure. */
  683. api.vrf_id = VRF_DEFAULT;
  684. api.flags = flags;
  685. api.type = ZEBRA_ROUTE_BGP;
  686. api.message = 0;
  687. api.safi = safi;
  688. SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
  689. api.nexthop_num = 1;
  690. api.nexthop = &nexthop;
  691. SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
  692. api.ifindex_num = 1;
  693. api.ifindex = &ifindex;
  694. SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
  695. api.metric = info->attr->med;
  696. if (BGP_DEBUG(zebra, ZEBRA))
  697. {
  698. char buf[2][INET6_ADDRSTRLEN];
  699. zlog_debug("Zebra send: IPv6 route add %s/%d nexthop %s metric %u",
  700. inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
  701. p->prefixlen,
  702. inet_ntop(AF_INET6, nexthop, buf[1], sizeof(buf[1])),
  703. api.metric);
  704. }
  705. zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD, zclient,
  706. (struct prefix_ipv6 *) p, &api);
  707. }
  708. #endif /* HAVE_IPV6 */
  709. }
  710. void
  711. bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info, safi_t safi)
  712. {
  713. int flags;
  714. struct peer *peer;
  715. if (zclient->sock < 0)
  716. return;
  717. if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_BGP], VRF_DEFAULT))
  718. return;
  719. peer = info->peer;
  720. flags = 0;
  721. if (peer->sort == BGP_PEER_IBGP)
  722. {
  723. SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
  724. SET_FLAG (flags, ZEBRA_FLAG_IBGP);
  725. }
  726. if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
  727. || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
  728. SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
  729. if (p->family == AF_INET)
  730. {
  731. struct zapi_ipv4 api;
  732. api.vrf_id = VRF_DEFAULT;
  733. api.flags = flags;
  734. api.type = ZEBRA_ROUTE_BGP;
  735. api.message = 0;
  736. api.safi = safi;
  737. api.nexthop_num = 0;
  738. api.ifindex_num = 0;
  739. SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
  740. api.metric = info->attr->med;
  741. if (BGP_DEBUG(zebra, ZEBRA))
  742. {
  743. char buf[2][INET_ADDRSTRLEN];
  744. zlog_debug("Zebra send: IPv4 route delete %s/%d metric %u",
  745. inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
  746. p->prefixlen,
  747. api.metric);
  748. }
  749. zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient,
  750. (struct prefix_ipv4 *) p, &api);
  751. }
  752. #ifdef HAVE_IPV6
  753. /* We have to think about a IPv6 link-local address curse. */
  754. if (p->family == AF_INET6)
  755. {
  756. struct zapi_ipv6 api;
  757. api.vrf_id = VRF_DEFAULT;
  758. api.flags = flags;
  759. api.type = ZEBRA_ROUTE_BGP;
  760. api.message = 0;
  761. api.safi = safi;
  762. api.nexthop_num = 0;
  763. api.ifindex_num = 0;
  764. SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
  765. api.metric = info->attr->med;
  766. if (BGP_DEBUG(zebra, ZEBRA))
  767. {
  768. char buf[2][INET6_ADDRSTRLEN];
  769. zlog_debug("Zebra send: IPv6 route delete %s/%d metric %u",
  770. inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
  771. p->prefixlen,
  772. api.metric);
  773. }
  774. zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient,
  775. (struct prefix_ipv6 *) p, &api);
  776. }
  777. #endif /* HAVE_IPV6 */
  778. }
  779. /* Other routes redistribution into BGP. */
  780. int
  781. bgp_redistribute_set (struct bgp *bgp, afi_t afi, int type)
  782. {
  783. /* Set flag to BGP instance. */
  784. bgp->redist[afi][type] = 1;
  785. /* Return if already redistribute flag is set. */
  786. if (vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT))
  787. return CMD_WARNING;
  788. vrf_bitmap_set (zclient->redist[type], VRF_DEFAULT);
  789. /* Return if zebra connection is not established. */
  790. if (zclient->sock < 0)
  791. return CMD_WARNING;
  792. if (BGP_DEBUG(zebra, ZEBRA))
  793. zlog_debug("Zebra send: redistribute add %s", zebra_route_string(type));
  794. /* Send distribute add message to zebra. */
  795. zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT);
  796. return CMD_SUCCESS;
  797. }
  798. /* Redistribute with route-map specification. */
  799. int
  800. bgp_redistribute_rmap_set (struct bgp *bgp, afi_t afi, int type,
  801. const char *name)
  802. {
  803. if (bgp->rmap[afi][type].name
  804. && (strcmp (bgp->rmap[afi][type].name, name) == 0))
  805. return 0;
  806. if (bgp->rmap[afi][type].name)
  807. free (bgp->rmap[afi][type].name);
  808. bgp->rmap[afi][type].name = strdup (name);
  809. bgp->rmap[afi][type].map = route_map_lookup_by_name (name);
  810. return 1;
  811. }
  812. /* Redistribute with metric specification. */
  813. int
  814. bgp_redistribute_metric_set (struct bgp *bgp, afi_t afi, int type,
  815. u_int32_t metric)
  816. {
  817. if (bgp->redist_metric_flag[afi][type]
  818. && bgp->redist_metric[afi][type] == metric)
  819. return 0;
  820. bgp->redist_metric_flag[afi][type] = 1;
  821. bgp->redist_metric[afi][type] = metric;
  822. return 1;
  823. }
  824. /* Unset redistribution. */
  825. int
  826. bgp_redistribute_unset (struct bgp *bgp, afi_t afi, int type)
  827. {
  828. /* Unset flag from BGP instance. */
  829. bgp->redist[afi][type] = 0;
  830. /* Unset route-map. */
  831. if (bgp->rmap[afi][type].name)
  832. free (bgp->rmap[afi][type].name);
  833. bgp->rmap[afi][type].name = NULL;
  834. bgp->rmap[afi][type].map = NULL;
  835. /* Unset metric. */
  836. bgp->redist_metric_flag[afi][type] = 0;
  837. bgp->redist_metric[afi][type] = 0;
  838. /* Return if zebra connection is disabled. */
  839. if (! vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT))
  840. return CMD_WARNING;
  841. vrf_bitmap_unset (zclient->redist[type], VRF_DEFAULT);
  842. if (bgp->redist[AFI_IP][type] == 0
  843. && bgp->redist[AFI_IP6][type] == 0
  844. && zclient->sock >= 0)
  845. {
  846. /* Send distribute delete message to zebra. */
  847. if (BGP_DEBUG(zebra, ZEBRA))
  848. zlog_debug("Zebra send: redistribute delete %s",
  849. zebra_route_string(type));
  850. zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, type,
  851. VRF_DEFAULT);
  852. }
  853. /* Withdraw redistributed routes from current BGP's routing table. */
  854. bgp_redistribute_withdraw (bgp, afi, type);
  855. return CMD_SUCCESS;
  856. }
  857. /* Unset redistribution route-map configuration. */
  858. int
  859. bgp_redistribute_routemap_unset (struct bgp *bgp, afi_t afi, int type)
  860. {
  861. if (! bgp->rmap[afi][type].name)
  862. return 0;
  863. /* Unset route-map. */
  864. free (bgp->rmap[afi][type].name);
  865. bgp->rmap[afi][type].name = NULL;
  866. bgp->rmap[afi][type].map = NULL;
  867. return 1;
  868. }
  869. /* Unset redistribution metric configuration. */
  870. int
  871. bgp_redistribute_metric_unset (struct bgp *bgp, afi_t afi, int type)
  872. {
  873. if (! bgp->redist_metric_flag[afi][type])
  874. return 0;
  875. /* Unset metric. */
  876. bgp->redist_metric_flag[afi][type] = 0;
  877. bgp->redist_metric[afi][type] = 0;
  878. return 1;
  879. }
  880. void
  881. bgp_zclient_reset (void)
  882. {
  883. zclient_reset (zclient);
  884. }
  885. static void
  886. bgp_zebra_connected (struct zclient *zclient)
  887. {
  888. zclient_send_requests (zclient, VRF_DEFAULT);
  889. }
  890. void
  891. bgp_zebra_init (struct thread_master *master)
  892. {
  893. /* Set default values. */
  894. zclient = zclient_new (master);
  895. zclient_init (zclient, ZEBRA_ROUTE_BGP);
  896. zclient->zebra_connected = bgp_zebra_connected;
  897. zclient->router_id_update = bgp_router_id_update;
  898. zclient->interface_add = bgp_interface_add;
  899. zclient->interface_delete = bgp_interface_delete;
  900. zclient->interface_address_add = bgp_interface_address_add;
  901. zclient->interface_address_delete = bgp_interface_address_delete;
  902. zclient->ipv4_route_add = zebra_read_ipv4;
  903. zclient->ipv4_route_delete = zebra_read_ipv4;
  904. zclient->interface_up = bgp_interface_up;
  905. zclient->interface_down = bgp_interface_down;
  906. #ifdef HAVE_IPV6
  907. zclient->ipv6_route_add = zebra_read_ipv6;
  908. zclient->ipv6_route_delete = zebra_read_ipv6;
  909. #endif /* HAVE_IPV6 */
  910. bgp_nexthop_buf = stream_new(BGP_NEXTHOP_BUF_SIZE);
  911. }