redistribute.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. /* master zebra server structure */
  36. extern struct zebra_t zebrad;
  37. int
  38. zebra_check_addr (struct prefix *p)
  39. {
  40. if (p->family == AF_INET)
  41. {
  42. u_int32_t addr;
  43. addr = p->u.prefix4.s_addr;
  44. addr = ntohl (addr);
  45. if (IPV4_NET127 (addr) || IN_CLASSD (addr))
  46. return 0;
  47. }
  48. #ifdef HAVE_IPV6
  49. if (p->family == AF_INET6)
  50. {
  51. if (IN6_IS_ADDR_LOOPBACK (&p->u.prefix6))
  52. return 0;
  53. if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
  54. return 0;
  55. }
  56. #endif /* HAVE_IPV6 */
  57. return 1;
  58. }
  59. int
  60. is_default (struct prefix *p)
  61. {
  62. if (p->family == AF_INET)
  63. if (p->u.prefix4.s_addr == 0 && p->prefixlen == 0)
  64. return 1;
  65. #ifdef HAVE_IPV6
  66. #if 0 /* IPv6 default separation is now pending until protocol daemon
  67. can handle that. */
  68. if (p->family == AF_INET6)
  69. if (IN6_IS_ADDR_UNSPECIFIED (&p->u.prefix6) && p->prefixlen == 0)
  70. return 1;
  71. #endif /* 0 */
  72. #endif /* HAVE_IPV6 */
  73. return 0;
  74. }
  75. void
  76. zebra_redistribute_default (struct zserv *client)
  77. {
  78. struct prefix_ipv4 p;
  79. struct route_table *table;
  80. struct route_node *rn;
  81. struct rib *newrib;
  82. #ifdef HAVE_IPV6
  83. struct prefix_ipv6 p6;
  84. #endif /* HAVE_IPV6 */
  85. /* Lookup default route. */
  86. memset (&p, 0, sizeof (struct prefix_ipv4));
  87. p.family = AF_INET;
  88. /* Lookup table. */
  89. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  90. if (table)
  91. {
  92. rn = route_node_lookup (table, (struct prefix *)&p);
  93. if (rn)
  94. {
  95. for (newrib = rn->info; newrib; newrib = newrib->next)
  96. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  97. && newrib->distance != DISTANCE_INFINITY)
  98. zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, &rn->p, newrib);
  99. route_unlock_node (rn);
  100. }
  101. }
  102. #ifdef HAVE_IPV6
  103. /* Lookup default route. */
  104. memset (&p6, 0, sizeof (struct prefix_ipv6));
  105. p6.family = AF_INET6;
  106. /* Lookup table. */
  107. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  108. if (table)
  109. {
  110. rn = route_node_lookup (table, (struct prefix *)&p6);
  111. if (rn)
  112. {
  113. for (newrib = rn->info; newrib; newrib = newrib->next)
  114. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  115. && newrib->distance != DISTANCE_INFINITY)
  116. zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, &rn->p, newrib);
  117. route_unlock_node (rn);
  118. }
  119. }
  120. #endif /* HAVE_IPV6 */
  121. }
  122. /* Redistribute routes. */
  123. void
  124. zebra_redistribute (struct zserv *client, int type)
  125. {
  126. struct rib *newrib;
  127. struct route_table *table;
  128. struct route_node *rn;
  129. table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
  130. if (table)
  131. for (rn = route_top (table); rn; rn = route_next (rn))
  132. for (newrib = rn->info; newrib; newrib = newrib->next)
  133. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  134. && newrib->type == type
  135. && newrib->distance != DISTANCE_INFINITY
  136. && zebra_check_addr (&rn->p))
  137. zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, &rn->p, newrib);
  138. #ifdef HAVE_IPV6
  139. table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
  140. if (table)
  141. for (rn = route_top (table); rn; rn = route_next (rn))
  142. for (newrib = rn->info; newrib; newrib = newrib->next)
  143. if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
  144. && newrib->type == type
  145. && newrib->distance != DISTANCE_INFINITY
  146. && zebra_check_addr (&rn->p))
  147. zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, &rn->p, newrib);
  148. #endif /* HAVE_IPV6 */
  149. }
  150. void
  151. redistribute_add (struct prefix *p, struct rib *rib)
  152. {
  153. listnode node;
  154. struct zserv *client;
  155. for (node = listhead (zebrad.client_list); node; nextnode (node))
  156. if ((client = getdata (node)) != NULL)
  157. {
  158. if (is_default (p))
  159. {
  160. if (client->redist_default || client->redist[rib->type])
  161. {
  162. if (p->family == AF_INET)
  163. zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, p, rib);
  164. #ifdef HAVE_IPV6
  165. if (p->family == AF_INET6)
  166. zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, p, rib);
  167. #endif /* HAVE_IPV6 */
  168. }
  169. }
  170. else if (client->redist[rib->type])
  171. {
  172. if (p->family == AF_INET)
  173. zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, p, rib);
  174. #ifdef HAVE_IPV6
  175. if (p->family == AF_INET6)
  176. zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, p, rib);
  177. #endif /* HAVE_IPV6 */
  178. }
  179. }
  180. }
  181. void
  182. redistribute_delete (struct prefix *p, struct rib *rib)
  183. {
  184. listnode node;
  185. struct zserv *client;
  186. /* Add DISTANCE_INFINITY check. */
  187. if (rib->distance == DISTANCE_INFINITY)
  188. return;
  189. for (node = listhead (zebrad.client_list); node; nextnode (node))
  190. if ((client = getdata (node)) != NULL)
  191. {
  192. if (is_default (p))
  193. {
  194. if (client->redist_default || client->redist[rib->type])
  195. {
  196. if (p->family == AF_INET)
  197. zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE, client, p, rib);
  198. #ifdef HAVE_IPV6
  199. if (p->family == AF_INET6)
  200. zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE, client, p, rib);
  201. #endif /* HAVE_IPV6 */
  202. }
  203. }
  204. else if (client->redist[rib->type])
  205. {
  206. if (p->family == AF_INET)
  207. zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE, client, p, rib);
  208. #ifdef HAVE_IPV6
  209. if (p->family == AF_INET6)
  210. zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE, client, p, rib);
  211. #endif /* HAVE_IPV6 */
  212. }
  213. }
  214. }
  215. void
  216. zebra_redistribute_add (int command, struct zserv *client, int length)
  217. {
  218. int type;
  219. type = stream_getc (client->ibuf);
  220. switch (type)
  221. {
  222. case ZEBRA_ROUTE_KERNEL:
  223. case ZEBRA_ROUTE_CONNECT:
  224. case ZEBRA_ROUTE_STATIC:
  225. case ZEBRA_ROUTE_RIP:
  226. case ZEBRA_ROUTE_RIPNG:
  227. case ZEBRA_ROUTE_OSPF:
  228. case ZEBRA_ROUTE_OSPF6:
  229. case ZEBRA_ROUTE_BGP:
  230. if (! client->redist[type])
  231. {
  232. client->redist[type] = 1;
  233. zebra_redistribute (client, type);
  234. }
  235. break;
  236. default:
  237. break;
  238. }
  239. }
  240. void
  241. zebra_redistribute_delete (int command, struct zserv *client, int length)
  242. {
  243. int type;
  244. type = stream_getc (client->ibuf);
  245. switch (type)
  246. {
  247. case ZEBRA_ROUTE_KERNEL:
  248. case ZEBRA_ROUTE_CONNECT:
  249. case ZEBRA_ROUTE_STATIC:
  250. case ZEBRA_ROUTE_RIP:
  251. case ZEBRA_ROUTE_RIPNG:
  252. case ZEBRA_ROUTE_OSPF:
  253. case ZEBRA_ROUTE_OSPF6:
  254. case ZEBRA_ROUTE_BGP:
  255. client->redist[type] = 0;
  256. break;
  257. default:
  258. break;
  259. }
  260. }
  261. void
  262. zebra_redistribute_default_add (int command, struct zserv *client, int length)
  263. {
  264. client->redist_default = 1;
  265. zebra_redistribute_default (client);
  266. }
  267. void
  268. zebra_redistribute_default_delete (int command, struct zserv *client,
  269. int length)
  270. {
  271. client->redist_default = 0;;
  272. }
  273. /* Interface up information. */
  274. void
  275. zebra_interface_up_update (struct interface *ifp)
  276. {
  277. listnode node;
  278. struct zserv *client;
  279. if (IS_ZEBRA_DEBUG_EVENT)
  280. zlog_info ("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp->name);
  281. for (node = listhead (zebrad.client_list); node; nextnode (node))
  282. if ((client = getdata (node)) != NULL)
  283. zsend_interface_update (ZEBRA_INTERFACE_UP, client, ifp);
  284. }
  285. /* Interface down information. */
  286. void
  287. zebra_interface_down_update (struct interface *ifp)
  288. {
  289. listnode node;
  290. struct zserv *client;
  291. if (IS_ZEBRA_DEBUG_EVENT)
  292. zlog_info ("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp->name);
  293. for (node = listhead (zebrad.client_list); node; nextnode (node))
  294. if ((client = getdata (node)) != NULL)
  295. zsend_interface_update (ZEBRA_INTERFACE_DOWN, client, ifp);
  296. }
  297. /* Interface information update. */
  298. void
  299. zebra_interface_add_update (struct interface *ifp)
  300. {
  301. listnode node;
  302. struct zserv *client;
  303. if (IS_ZEBRA_DEBUG_EVENT)
  304. zlog_info ("MESSAGE: ZEBRA_INTERFACE_ADD %s", ifp->name);
  305. for (node = listhead (zebrad.client_list); node; nextnode (node))
  306. if ((client = getdata (node)) != NULL)
  307. if (client->ifinfo)
  308. zsend_interface_add (client, ifp);
  309. }
  310. /*
  311. * This function is only called when support for
  312. * RTM_IFANNOUNCE or AF_NETLINK sockets (RTM_DELLINK message)
  313. * is available. It is not called on Solaris.
  314. */
  315. #if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK))
  316. void
  317. zebra_interface_delete_update (struct interface *ifp)
  318. {
  319. listnode node;
  320. struct zserv *client;
  321. if (IS_ZEBRA_DEBUG_EVENT)
  322. zlog_info ("MESSAGE: ZEBRA_INTERFACE_DELETE %s", ifp->name);
  323. for (node = listhead (zebrad.client_list); node; nextnode (node))
  324. if ((client = getdata (node)) != NULL)
  325. if (client->ifinfo)
  326. zsend_interface_delete (client, ifp);
  327. }
  328. #endif /* defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK) */
  329. /* Interface address addition. */
  330. void
  331. zebra_interface_address_add_update (struct interface *ifp,
  332. struct connected *ifc)
  333. {
  334. listnode node;
  335. struct zserv *client;
  336. struct prefix *p;
  337. char buf[BUFSIZ];
  338. if (IS_ZEBRA_DEBUG_EVENT)
  339. {
  340. p = ifc->address;
  341. zlog_info ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s/%d on %s",
  342. inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
  343. p->prefixlen, ifc->ifp->name);
  344. }
  345. for (node = listhead (zebrad.client_list); node; nextnode (node))
  346. if ((client = getdata (node)) != NULL)
  347. if (client->ifinfo && CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  348. zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client, ifp, ifc);
  349. }
  350. /* Interface address deletion. */
  351. void
  352. zebra_interface_address_delete_update (struct interface *ifp,
  353. struct connected *ifc)
  354. {
  355. listnode node;
  356. struct zserv *client;
  357. struct prefix *p;
  358. char buf[BUFSIZ];
  359. if (IS_ZEBRA_DEBUG_EVENT)
  360. {
  361. p = ifc->address;
  362. zlog_info ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s/%d on %s",
  363. inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
  364. p->prefixlen, ifc->ifp->name);
  365. }
  366. for (node = listhead (zebrad.client_list); node; nextnode (node))
  367. if ((client = getdata (node)) != NULL)
  368. if (client->ifinfo && CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  369. zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_DELETE, client, ifp, ifc);
  370. }