redistribute.c 9.3 KB

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