redistribute.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* Redistribution Handler
  2. * Copyright (C) 1998 Kunihiro Ishiguro
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  6. * GNU Zebra is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * GNU Zebra is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include "vector.h"
  23. #include "vty.h"
  24. #include "command.h"
  25. #include "prefix.h"
  26. #include "table.h"
  27. #include "stream.h"
  28. #include "zclient.h"
  29. #include "linklist.h"
  30. #include "log.h"
  31. #include "vrf.h"
  32. #include "zebra/rib.h"
  33. #include "zebra/zserv.h"
  34. #include "zebra/redistribute.h"
  35. #include "zebra/debug.h"
  36. #include "zebra/router-id.h"
  37. /* master zebra server structure */
  38. extern struct zebra_t zebrad;
  39. int
  40. zebra_check_addr (struct prefix *p)
  41. {
  42. if (p->family == AF_INET)
  43. {
  44. u_int32_t addr;
  45. addr = p->u.prefix4.s_addr;
  46. addr = ntohl (addr);
  47. if (IPV4_NET127 (addr)
  48. || IN_CLASSD (addr)
  49. || IPV4_LINKLOCAL(addr))
  50. return 0;
  51. }
  52. #ifdef HAVE_IPV6
  53. if (p->family == AF_INET6)
  54. {
  55. if (IN6_IS_ADDR_LOOPBACK (&p->u.prefix6))
  56. return 0;
  57. if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
  58. return 0;
  59. }
  60. #endif /* HAVE_IPV6 */
  61. return 1;
  62. }
  63. int
  64. is_default (struct prefix *p)
  65. {
  66. if (p->family == AF_INET)
  67. if (p->u.prefix4.s_addr == 0 && p->prefixlen == 0)
  68. return 1;
  69. #ifdef HAVE_IPV6
  70. #if 0 /* IPv6 default separation is now pending until protocol daemon
  71. can handle that. */
  72. if (p->family == AF_INET6)
  73. if (IN6_IS_ADDR_UNSPECIFIED (&p->u.prefix6) && p->prefixlen == 0)
  74. return 1;
  75. #endif /* 0 */
  76. #endif /* HAVE_IPV6 */
  77. return 0;
  78. }
  79. static void
  80. zebra_redistribute_default (struct zserv *client, vrf_id_t vrf_id)
  81. {
  82. struct prefix_ipv4 p;
  83. struct route_table *table;
  84. struct route_node *rn;
  85. struct rib *newrib;
  86. #ifdef HAVE_IPV6
  87. struct prefix_ipv6 p6;
  88. #endif /* HAVE_IPV6 */
  89. /* Lookup default route. */
  90. memset (&p, 0, sizeof (struct prefix_ipv4));
  91. p.family = AF_INET;
  92. /* Lookup table. */
  93. table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
  94. if (table)
  95. {
  96. rn = route_node_lookup (table, (struct prefix *)&p);
  97. if (rn)
  98. {
  99. RNODE_FOREACH_RIB (rn, newrib)
  100. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  101. && newrib->distance != DISTANCE_INFINITY)
  102. zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, &rn->p, newrib);
  103. route_unlock_node (rn);
  104. }
  105. }
  106. #ifdef HAVE_IPV6
  107. /* Lookup default route. */
  108. memset (&p6, 0, sizeof (struct prefix_ipv6));
  109. p6.family = AF_INET6;
  110. /* Lookup table. */
  111. table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id);
  112. if (table)
  113. {
  114. rn = route_node_lookup (table, (struct prefix *)&p6);
  115. if (rn)
  116. {
  117. RNODE_FOREACH_RIB (rn, newrib)
  118. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  119. && newrib->distance != DISTANCE_INFINITY)
  120. zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, &rn->p, newrib);
  121. route_unlock_node (rn);
  122. }
  123. }
  124. #endif /* HAVE_IPV6 */
  125. }
  126. /* Redistribute routes. */
  127. static void
  128. zebra_redistribute (struct zserv *client, int type, vrf_id_t vrf_id)
  129. {
  130. struct rib *newrib;
  131. struct route_table *table;
  132. struct route_node *rn;
  133. table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
  134. if (table)
  135. for (rn = route_top (table); rn; rn = route_next (rn))
  136. RNODE_FOREACH_RIB (rn, newrib)
  137. {
  138. if (IS_ZEBRA_DEBUG_EVENT)
  139. zlog_debug("%s: checking: selected=%d, type=%d, distance=%d, zebra_check_addr=%d",
  140. __func__, CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED),
  141. newrib->type, newrib->distance, zebra_check_addr (&rn->p));
  142. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  143. && newrib->type == type
  144. && newrib->distance != DISTANCE_INFINITY
  145. && zebra_check_addr (&rn->p))
  146. {
  147. client->redist_v4_add_cnt++;
  148. zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, &rn->p, newrib);
  149. }
  150. }
  151. #ifdef HAVE_IPV6
  152. table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id);
  153. if (table)
  154. for (rn = route_top (table); rn; rn = route_next (rn))
  155. RNODE_FOREACH_RIB (rn, newrib)
  156. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  157. && newrib->type == type
  158. && newrib->distance != DISTANCE_INFINITY
  159. && zebra_check_addr (&rn->p))
  160. {
  161. client->redist_v6_add_cnt++;
  162. zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, &rn->p, newrib);
  163. }
  164. #endif /* HAVE_IPV6 */
  165. }
  166. void
  167. redistribute_add (struct prefix *p, struct rib *rib, struct rib *rib_old)
  168. {
  169. struct listnode *node, *nnode;
  170. struct zserv *client;
  171. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  172. {
  173. if ((is_default (p) &&
  174. vrf_bitmap_check (client->redist_default, rib->vrf_id))
  175. || vrf_bitmap_check (client->redist[rib->type], rib->vrf_id))
  176. {
  177. if (p->family == AF_INET)
  178. {
  179. client->redist_v4_add_cnt++;
  180. zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, p, rib);
  181. }
  182. if (p->family == AF_INET6)
  183. {
  184. client->redist_v6_add_cnt++;
  185. zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, p, rib);
  186. }
  187. }
  188. else if (rib_old && vrf_bitmap_check (client->redist[rib_old->type],
  189. rib_old->vrf_id))
  190. {
  191. /* redistribute_add has implicit withdraw semantics, so there
  192. * may be an old route already redistributed that is being updated.
  193. *
  194. * However, if the new route is of a type that is /not/ redistributed
  195. * to the client, then we must ensure the old route is explicitly
  196. * withdrawn.
  197. */
  198. if (p->family == AF_INET)
  199. zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE, client, p, rib_old);
  200. if (p->family == AF_INET6)
  201. zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE, client, p, rib_old);
  202. }
  203. }
  204. }
  205. void
  206. redistribute_delete (struct prefix *p, struct rib *rib)
  207. {
  208. struct listnode *node, *nnode;
  209. struct zserv *client;
  210. /* Add DISTANCE_INFINITY check. */
  211. if (rib->distance == DISTANCE_INFINITY)
  212. return;
  213. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  214. {
  215. if ((is_default (p) &&
  216. vrf_bitmap_check (client->redist_default, rib->vrf_id))
  217. || vrf_bitmap_check (client->redist[rib->type], rib->vrf_id))
  218. {
  219. if (p->family == AF_INET)
  220. zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE, client, p, rib);
  221. #ifdef HAVE_IPV6
  222. if (p->family == AF_INET6)
  223. zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE, client, p, rib);
  224. #endif /* HAVE_IPV6 */
  225. }
  226. }
  227. }
  228. void
  229. zebra_redistribute_add (int command, struct zserv *client, int length,
  230. vrf_id_t vrf_id)
  231. {
  232. int type;
  233. type = stream_getc (client->ibuf);
  234. if (type == 0 || type >= ZEBRA_ROUTE_MAX)
  235. return;
  236. if (! vrf_bitmap_check (client->redist[type], vrf_id))
  237. {
  238. vrf_bitmap_set (client->redist[type], vrf_id);
  239. zebra_redistribute (client, type, vrf_id);
  240. }
  241. }
  242. void
  243. zebra_redistribute_delete (int command, struct zserv *client, int length,
  244. vrf_id_t vrf_id)
  245. {
  246. int type;
  247. type = stream_getc (client->ibuf);
  248. if (type == 0 || type >= ZEBRA_ROUTE_MAX)
  249. return;
  250. vrf_bitmap_unset (client->redist[type], vrf_id);
  251. }
  252. void
  253. zebra_redistribute_default_add (int command, struct zserv *client, int length,
  254. vrf_id_t vrf_id)
  255. {
  256. vrf_bitmap_set (client->redist_default, vrf_id);
  257. zebra_redistribute_default (client, vrf_id);
  258. }
  259. void
  260. zebra_redistribute_default_delete (int command, struct zserv *client,
  261. int length, vrf_id_t vrf_id)
  262. {
  263. vrf_bitmap_unset (client->redist_default, vrf_id);
  264. }
  265. /* Interface up information. */
  266. void
  267. zebra_interface_up_update (struct interface *ifp)
  268. {
  269. struct listnode *node, *nnode;
  270. struct zserv *client;
  271. if (IS_ZEBRA_DEBUG_EVENT)
  272. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp->name);
  273. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  274. if (client->ifinfo)
  275. {
  276. zsend_interface_update (ZEBRA_INTERFACE_UP, client, ifp);
  277. zsend_interface_link_params (client, ifp);
  278. }
  279. }
  280. /* Interface down information. */
  281. void
  282. zebra_interface_down_update (struct interface *ifp)
  283. {
  284. struct listnode *node, *nnode;
  285. struct zserv *client;
  286. if (IS_ZEBRA_DEBUG_EVENT)
  287. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp->name);
  288. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  289. {
  290. zsend_interface_update (ZEBRA_INTERFACE_DOWN, client, ifp);
  291. }
  292. }
  293. /* Interface information update. */
  294. void
  295. zebra_interface_add_update (struct interface *ifp)
  296. {
  297. struct listnode *node, *nnode;
  298. struct zserv *client;
  299. if (IS_ZEBRA_DEBUG_EVENT)
  300. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADD %s", ifp->name);
  301. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  302. if (client->ifinfo)
  303. {
  304. client->ifadd_cnt++;
  305. zsend_interface_add (client, ifp);
  306. zsend_interface_link_params (client, ifp);
  307. }
  308. }
  309. void
  310. zebra_interface_delete_update (struct interface *ifp)
  311. {
  312. struct listnode *node, *nnode;
  313. struct zserv *client;
  314. if (IS_ZEBRA_DEBUG_EVENT)
  315. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DELETE %s", ifp->name);
  316. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  317. if (client->ifinfo)
  318. {
  319. client->ifdel_cnt++;
  320. zsend_interface_delete (client, ifp);
  321. }
  322. }
  323. /* Interface address addition. */
  324. void
  325. zebra_interface_address_add_update (struct interface *ifp,
  326. struct connected *ifc)
  327. {
  328. struct listnode *node, *nnode;
  329. struct zserv *client;
  330. struct prefix *p;
  331. if (IS_ZEBRA_DEBUG_EVENT)
  332. {
  333. char buf[PREFIX_STRLEN];
  334. p = ifc->address;
  335. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s on %s",
  336. prefix2str (p, buf, sizeof(buf)),
  337. ifc->ifp->name);
  338. }
  339. if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
  340. zlog_warn("WARNING: advertising address to clients that is not yet usable.");
  341. router_id_add_address(ifc);
  342. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  343. if (client->ifinfo && CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  344. {
  345. client->connected_rt_add_cnt++;
  346. zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client, ifp, ifc);
  347. }
  348. }
  349. /* Interface address deletion. */
  350. void
  351. zebra_interface_address_delete_update (struct interface *ifp,
  352. struct connected *ifc)
  353. {
  354. struct listnode *node, *nnode;
  355. struct zserv *client;
  356. struct prefix *p;
  357. if (IS_ZEBRA_DEBUG_EVENT)
  358. {
  359. char buf[PREFIX_STRLEN];
  360. p = ifc->address;
  361. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s on %s",
  362. prefix2str (p, buf, sizeof(buf)),
  363. ifc->ifp->name);
  364. }
  365. router_id_del_address(ifc);
  366. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  367. if (client->ifinfo && CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  368. {
  369. client->connected_rt_del_cnt++;
  370. zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_DELETE, client, ifp, ifc);
  371. }
  372. }
  373. /* Interface parameters update */
  374. void
  375. zebra_interface_parameters_update (struct interface *ifp)
  376. {
  377. struct listnode *node, *nnode;
  378. struct zserv *client;
  379. if (IS_ZEBRA_DEBUG_EVENT)
  380. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s", ifp->name);
  381. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  382. if (client->ifinfo)
  383. zsend_interface_link_params (client, ifp);
  384. }