bgp_zebra.c 31 KB

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