bgp_zebra.c 30 KB

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