redistribute.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  138. && newrib->type == type
  139. && newrib->distance != DISTANCE_INFINITY
  140. && zebra_check_addr (&rn->p))
  141. zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, &rn->p, newrib);
  142. #ifdef HAVE_IPV6
  143. table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id);
  144. if (table)
  145. for (rn = route_top (table); rn; rn = route_next (rn))
  146. RNODE_FOREACH_RIB (rn, newrib)
  147. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  148. && newrib->type == type
  149. && newrib->distance != DISTANCE_INFINITY
  150. && zebra_check_addr (&rn->p))
  151. zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, &rn->p, newrib);
  152. #endif /* HAVE_IPV6 */
  153. }
  154. void
  155. redistribute_add (struct prefix *p, struct rib *rib)
  156. {
  157. struct listnode *node, *nnode;
  158. struct zserv *client;
  159. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  160. {
  161. if ((is_default (p) &&
  162. vrf_bitmap_check (client->redist_default, rib->vrf_id))
  163. || vrf_bitmap_check (client->redist[rib->type], rib->vrf_id))
  164. {
  165. if (p->family == AF_INET)
  166. zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, p, rib);
  167. #ifdef HAVE_IPV6
  168. if (p->family == AF_INET6)
  169. zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, p, rib);
  170. #endif /* HAVE_IPV6 */
  171. }
  172. }
  173. }
  174. void
  175. redistribute_delete (struct prefix *p, struct rib *rib)
  176. {
  177. struct listnode *node, *nnode;
  178. struct zserv *client;
  179. /* Add DISTANCE_INFINITY check. */
  180. if (rib->distance == DISTANCE_INFINITY)
  181. return;
  182. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  183. {
  184. if ((is_default (p) &&
  185. vrf_bitmap_check (client->redist_default, rib->vrf_id))
  186. || vrf_bitmap_check (client->redist[rib->type], rib->vrf_id))
  187. {
  188. if (p->family == AF_INET)
  189. zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE, client, p, rib);
  190. #ifdef HAVE_IPV6
  191. if (p->family == AF_INET6)
  192. zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE, client, p, rib);
  193. #endif /* HAVE_IPV6 */
  194. }
  195. }
  196. }
  197. void
  198. zebra_redistribute_add (int command, struct zserv *client, int length,
  199. vrf_id_t vrf_id)
  200. {
  201. int type;
  202. type = stream_getc (client->ibuf);
  203. if (type == 0 || type >= ZEBRA_ROUTE_MAX)
  204. return;
  205. if (! vrf_bitmap_check (client->redist[type], vrf_id))
  206. {
  207. vrf_bitmap_set (client->redist[type], vrf_id);
  208. zebra_redistribute (client, type, vrf_id);
  209. }
  210. }
  211. void
  212. zebra_redistribute_delete (int command, struct zserv *client, int length,
  213. vrf_id_t vrf_id)
  214. {
  215. int type;
  216. type = stream_getc (client->ibuf);
  217. if (type == 0 || type >= ZEBRA_ROUTE_MAX)
  218. return;
  219. vrf_bitmap_unset (client->redist[type], vrf_id);
  220. }
  221. void
  222. zebra_redistribute_default_add (int command, struct zserv *client, int length,
  223. vrf_id_t vrf_id)
  224. {
  225. vrf_bitmap_set (client->redist_default, vrf_id);
  226. zebra_redistribute_default (client, vrf_id);
  227. }
  228. void
  229. zebra_redistribute_default_delete (int command, struct zserv *client,
  230. int length, vrf_id_t vrf_id)
  231. {
  232. vrf_bitmap_unset (client->redist_default, vrf_id);
  233. }
  234. /* Interface up information. */
  235. void
  236. zebra_interface_up_update (struct interface *ifp)
  237. {
  238. struct listnode *node, *nnode;
  239. struct zserv *client;
  240. if (IS_ZEBRA_DEBUG_EVENT)
  241. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp->name);
  242. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  243. zsend_interface_update (ZEBRA_INTERFACE_UP, client, ifp);
  244. }
  245. /* Interface down information. */
  246. void
  247. zebra_interface_down_update (struct interface *ifp)
  248. {
  249. struct listnode *node, *nnode;
  250. struct zserv *client;
  251. if (IS_ZEBRA_DEBUG_EVENT)
  252. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp->name);
  253. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  254. zsend_interface_update (ZEBRA_INTERFACE_DOWN, client, ifp);
  255. }
  256. /* Interface information update. */
  257. void
  258. zebra_interface_add_update (struct interface *ifp)
  259. {
  260. struct listnode *node, *nnode;
  261. struct zserv *client;
  262. if (IS_ZEBRA_DEBUG_EVENT)
  263. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADD %s", ifp->name);
  264. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  265. if (client->ifinfo)
  266. zsend_interface_add (client, ifp);
  267. }
  268. void
  269. zebra_interface_delete_update (struct interface *ifp)
  270. {
  271. struct listnode *node, *nnode;
  272. struct zserv *client;
  273. if (IS_ZEBRA_DEBUG_EVENT)
  274. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DELETE %s", ifp->name);
  275. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  276. if (client->ifinfo)
  277. zsend_interface_delete (client, ifp);
  278. }
  279. /* Interface address addition. */
  280. void
  281. zebra_interface_address_add_update (struct interface *ifp,
  282. struct connected *ifc)
  283. {
  284. struct listnode *node, *nnode;
  285. struct zserv *client;
  286. struct prefix *p;
  287. if (IS_ZEBRA_DEBUG_EVENT)
  288. {
  289. char buf[PREFIX_STRLEN];
  290. p = ifc->address;
  291. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s on %s",
  292. prefix2str (p, buf, sizeof(buf)),
  293. ifc->ifp->name);
  294. }
  295. if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
  296. zlog_warn("WARNING: advertising address to clients that is not yet usable.");
  297. router_id_add_address(ifc);
  298. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  299. if (client->ifinfo && CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  300. zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client, ifp, ifc);
  301. }
  302. /* Interface address deletion. */
  303. void
  304. zebra_interface_address_delete_update (struct interface *ifp,
  305. struct connected *ifc)
  306. {
  307. struct listnode *node, *nnode;
  308. struct zserv *client;
  309. struct prefix *p;
  310. if (IS_ZEBRA_DEBUG_EVENT)
  311. {
  312. char buf[PREFIX_STRLEN];
  313. p = ifc->address;
  314. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s on %s",
  315. prefix2str (p, buf, sizeof(buf)),
  316. ifc->ifp->name);
  317. }
  318. router_id_del_address(ifc);
  319. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  320. if (client->ifinfo && CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  321. zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_DELETE, client, ifp, ifc);
  322. }