redistribute.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 "zebra/rib.h"
  32. #include "zebra/zserv.h"
  33. #include "zebra/redistribute.h"
  34. #include "zebra/debug.h"
  35. #include "zebra/router-id.h"
  36. /* master zebra server structure */
  37. extern struct zebra_t zebrad;
  38. int
  39. zebra_check_addr (struct prefix *p)
  40. {
  41. if (p->family == AF_INET)
  42. {
  43. u_int32_t addr;
  44. addr = p->u.prefix4.s_addr;
  45. addr = ntohl (addr);
  46. if (IPV4_NET127 (addr)
  47. || IN_CLASSD (addr)
  48. || IPV4_LINKLOCAL(addr))
  49. return 0;
  50. }
  51. #ifdef HAVE_IPV6
  52. if (p->family == AF_INET6)
  53. {
  54. if (IN6_IS_ADDR_LOOPBACK (&p->u.prefix6))
  55. return 0;
  56. if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
  57. return 0;
  58. }
  59. #endif /* HAVE_IPV6 */
  60. return 1;
  61. }
  62. static int
  63. is_default (struct prefix *p)
  64. {
  65. if (p->family == AF_INET)
  66. if (p->u.prefix4.s_addr == 0 && p->prefixlen == 0)
  67. return 1;
  68. #ifdef HAVE_IPV6
  69. #if 0 /* IPv6 default separation is now pending until protocol daemon
  70. can handle that. */
  71. if (p->family == AF_INET6)
  72. if (IN6_IS_ADDR_UNSPECIFIED (&p->u.prefix6) && p->prefixlen == 0)
  73. return 1;
  74. #endif /* 0 */
  75. #endif /* HAVE_IPV6 */
  76. return 0;
  77. }
  78. static void
  79. zebra_redistribute_default (struct zserv *client)
  80. {
  81. struct prefix_ipv4 p;
  82. struct route_table *table;
  83. struct route_node *rn;
  84. struct rib *newrib;
  85. #ifdef HAVE_IPV6
  86. struct prefix_ipv6 p6;
  87. #endif /* HAVE_IPV6 */
  88. /* Lookup default route. */
  89. memset (&p, 0, sizeof (struct prefix_ipv4));
  90. p.family = AF_INET;
  91. /* Lookup table. */
  92. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  93. if (table)
  94. {
  95. rn = route_node_lookup (table, (struct prefix *)&p);
  96. if (rn)
  97. {
  98. RNODE_FOREACH_RIB (rn, newrib)
  99. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  100. && newrib->distance != DISTANCE_INFINITY)
  101. zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, &rn->p, newrib);
  102. route_unlock_node (rn);
  103. }
  104. }
  105. #ifdef HAVE_IPV6
  106. /* Lookup default route. */
  107. memset (&p6, 0, sizeof (struct prefix_ipv6));
  108. p6.family = AF_INET6;
  109. /* Lookup table. */
  110. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  111. if (table)
  112. {
  113. rn = route_node_lookup (table, (struct prefix *)&p6);
  114. if (rn)
  115. {
  116. RNODE_FOREACH_RIB (rn, newrib)
  117. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  118. && newrib->distance != DISTANCE_INFINITY)
  119. zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, &rn->p, newrib);
  120. route_unlock_node (rn);
  121. }
  122. }
  123. #endif /* HAVE_IPV6 */
  124. }
  125. /* Redistribute routes. */
  126. static void
  127. zebra_redistribute (struct zserv *client, int type)
  128. {
  129. struct rib *newrib;
  130. struct route_table *table;
  131. struct route_node *rn;
  132. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  133. if (table)
  134. for (rn = route_top (table); rn; rn = route_next (rn))
  135. RNODE_FOREACH_RIB (rn, newrib)
  136. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  137. && newrib->type == type
  138. && newrib->distance != DISTANCE_INFINITY
  139. && zebra_check_addr (&rn->p))
  140. zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, &rn->p, newrib);
  141. #ifdef HAVE_IPV6
  142. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  143. if (table)
  144. for (rn = route_top (table); rn; rn = route_next (rn))
  145. RNODE_FOREACH_RIB (rn, newrib)
  146. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  147. && newrib->type == type
  148. && newrib->distance != DISTANCE_INFINITY
  149. && zebra_check_addr (&rn->p))
  150. zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, &rn->p, newrib);
  151. #endif /* HAVE_IPV6 */
  152. }
  153. void
  154. redistribute_add (struct prefix *p, struct rib *rib)
  155. {
  156. struct listnode *node, *nnode;
  157. struct zserv *client;
  158. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  159. {
  160. if (is_default (p))
  161. {
  162. if (client->redist_default || 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. else if (client->redist[rib->type])
  173. {
  174. if (p->family == AF_INET)
  175. zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, p, rib);
  176. #ifdef HAVE_IPV6
  177. if (p->family == AF_INET6)
  178. zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, p, rib);
  179. #endif /* HAVE_IPV6 */
  180. }
  181. }
  182. }
  183. void
  184. redistribute_delete (struct prefix *p, struct rib *rib)
  185. {
  186. struct listnode *node, *nnode;
  187. struct zserv *client;
  188. /* Add DISTANCE_INFINITY check. */
  189. if (rib->distance == DISTANCE_INFINITY)
  190. return;
  191. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  192. {
  193. if (is_default (p))
  194. {
  195. if (client->redist_default || client->redist[rib->type])
  196. {
  197. if (p->family == AF_INET)
  198. zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE, client, p,
  199. rib);
  200. #ifdef HAVE_IPV6
  201. if (p->family == AF_INET6)
  202. zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE, client, p,
  203. rib);
  204. #endif /* HAVE_IPV6 */
  205. }
  206. }
  207. else if (client->redist[rib->type])
  208. {
  209. if (p->family == AF_INET)
  210. zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE, client, p, rib);
  211. #ifdef HAVE_IPV6
  212. if (p->family == AF_INET6)
  213. zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE, client, p, rib);
  214. #endif /* HAVE_IPV6 */
  215. }
  216. }
  217. }
  218. void
  219. zebra_redistribute_add (int command, struct zserv *client, int length)
  220. {
  221. int type;
  222. type = stream_getc (client->ibuf);
  223. if (type == 0 || type >= ZEBRA_ROUTE_MAX)
  224. return;
  225. if (! client->redist[type])
  226. {
  227. client->redist[type] = 1;
  228. zebra_redistribute (client, type);
  229. }
  230. }
  231. void
  232. zebra_redistribute_delete (int command, struct zserv *client, int length)
  233. {
  234. int type;
  235. type = stream_getc (client->ibuf);
  236. if (type == 0 || type >= ZEBRA_ROUTE_MAX)
  237. return;
  238. client->redist[type] = 0;
  239. }
  240. void
  241. zebra_redistribute_default_add (int command, struct zserv *client, int length)
  242. {
  243. client->redist_default = 1;
  244. zebra_redistribute_default (client);
  245. }
  246. void
  247. zebra_redistribute_default_delete (int command, struct zserv *client,
  248. int length)
  249. {
  250. client->redist_default = 0;;
  251. }
  252. /* Interface up information. */
  253. void
  254. zebra_interface_up_update (struct interface *ifp)
  255. {
  256. struct listnode *node, *nnode;
  257. struct zserv *client;
  258. if (IS_ZEBRA_DEBUG_EVENT)
  259. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp->name);
  260. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  261. zsend_interface_update (ZEBRA_INTERFACE_UP, client, ifp);
  262. }
  263. /* Interface down information. */
  264. void
  265. zebra_interface_down_update (struct interface *ifp)
  266. {
  267. struct listnode *node, *nnode;
  268. struct zserv *client;
  269. if (IS_ZEBRA_DEBUG_EVENT)
  270. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp->name);
  271. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  272. zsend_interface_update (ZEBRA_INTERFACE_DOWN, client, ifp);
  273. }
  274. /* Interface information update. */
  275. void
  276. zebra_interface_add_update (struct interface *ifp)
  277. {
  278. struct listnode *node, *nnode;
  279. struct zserv *client;
  280. if (IS_ZEBRA_DEBUG_EVENT)
  281. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADD %s", ifp->name);
  282. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  283. if (client->ifinfo)
  284. zsend_interface_add (client, ifp);
  285. }
  286. void
  287. zebra_interface_delete_update (struct interface *ifp)
  288. {
  289. struct listnode *node, *nnode;
  290. struct zserv *client;
  291. if (IS_ZEBRA_DEBUG_EVENT)
  292. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DELETE %s", ifp->name);
  293. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  294. if (client->ifinfo)
  295. zsend_interface_delete (client, ifp);
  296. }
  297. /* Interface address addition. */
  298. void
  299. zebra_interface_address_add_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[INET6_ADDRSTRLEN];
  308. p = ifc->address;
  309. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s/%d on %s",
  310. inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN),
  311. p->prefixlen, ifc->ifp->name);
  312. }
  313. if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
  314. zlog_warn("WARNING: advertising address to clients that is not yet usable.");
  315. router_id_add_address(ifc);
  316. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  317. if (client->ifinfo && CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  318. zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client, ifp, ifc);
  319. }
  320. /* Interface address deletion. */
  321. void
  322. zebra_interface_address_delete_update (struct interface *ifp,
  323. struct connected *ifc)
  324. {
  325. struct listnode *node, *nnode;
  326. struct zserv *client;
  327. struct prefix *p;
  328. if (IS_ZEBRA_DEBUG_EVENT)
  329. {
  330. char buf[INET6_ADDRSTRLEN];
  331. p = ifc->address;
  332. zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s/%d on %s",
  333. inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN),
  334. p->prefixlen, ifc->ifp->name);
  335. }
  336. router_id_del_address(ifc);
  337. for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
  338. if (client->ifinfo && CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  339. zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_DELETE, client, ifp, ifc);
  340. }