ospf_asbr.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * OSPF AS Boundary Router functions.
  3. * Copyright (C) 1999, 2000 Kunihiro Ishiguro, Toshiaki Takada
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "thread.h"
  24. #include "memory.h"
  25. #include "linklist.h"
  26. #include "prefix.h"
  27. #include "if.h"
  28. #include "table.h"
  29. #include "vty.h"
  30. #include "filter.h"
  31. #include "log.h"
  32. #include "ospfd/ospfd.h"
  33. #include "ospfd/ospf_interface.h"
  34. #include "ospfd/ospf_asbr.h"
  35. #include "ospfd/ospf_lsa.h"
  36. #include "ospfd/ospf_lsdb.h"
  37. #include "ospfd/ospf_neighbor.h"
  38. #include "ospfd/ospf_spf.h"
  39. #include "ospfd/ospf_flood.h"
  40. #include "ospfd/ospf_route.h"
  41. #include "ospfd/ospf_zebra.h"
  42. #include "ospfd/ospf_dump.h"
  43. /* Remove external route. */
  44. void
  45. ospf_external_route_remove (struct ospf *ospf, struct prefix_ipv4 *p)
  46. {
  47. struct route_node *rn;
  48. struct ospf_route *or;
  49. rn = route_node_lookup (ospf->old_external_route, (struct prefix *) p);
  50. if (rn)
  51. if ((or = rn->info))
  52. {
  53. zlog_info ("Route[%s/%d]: external path deleted",
  54. inet_ntoa (p->prefix), p->prefixlen);
  55. /* Remove route from zebra. */
  56. if (or->type == OSPF_DESTINATION_NETWORK)
  57. ospf_zebra_delete ((struct prefix_ipv4 *) &rn->p, or);
  58. ospf_route_free (or);
  59. rn->info = NULL;
  60. route_unlock_node (rn);
  61. route_unlock_node (rn);
  62. return;
  63. }
  64. zlog_info ("Route[%s/%d]: no such external path",
  65. inet_ntoa (p->prefix), p->prefixlen);
  66. }
  67. /* Lookup external route. */
  68. struct ospf_route *
  69. ospf_external_route_lookup (struct ospf *ospf,
  70. struct prefix_ipv4 *p)
  71. {
  72. struct route_node *rn;
  73. rn = route_node_lookup (ospf->old_external_route, (struct prefix *) p);
  74. if (rn)
  75. {
  76. route_unlock_node (rn);
  77. if (rn->info)
  78. return rn->info;
  79. }
  80. zlog_warn ("Route[%s/%d]: lookup, no such prefix",
  81. inet_ntoa (p->prefix), p->prefixlen);
  82. return NULL;
  83. }
  84. /* Add an External info for AS-external-LSA. */
  85. struct external_info *
  86. ospf_external_info_new (u_char type)
  87. {
  88. struct external_info *new;
  89. new = (struct external_info *)
  90. XCALLOC (MTYPE_OSPF_EXTERNAL_INFO, sizeof (struct external_info));
  91. new->type = type;
  92. ospf_reset_route_map_set_values (&new->route_map_set);
  93. return new;
  94. }
  95. static void
  96. ospf_external_info_free (struct external_info *ei)
  97. {
  98. XFREE (MTYPE_OSPF_EXTERNAL_INFO, ei);
  99. }
  100. void
  101. ospf_reset_route_map_set_values (struct route_map_set_values *values)
  102. {
  103. values->metric = -1;
  104. values->metric_type = -1;
  105. }
  106. int
  107. ospf_route_map_set_compare (struct route_map_set_values *values1,
  108. struct route_map_set_values *values2)
  109. {
  110. return values1->metric == values2->metric &&
  111. values1->metric_type == values2->metric_type;
  112. }
  113. /* Add an External info for AS-external-LSA. */
  114. struct external_info *
  115. ospf_external_info_add (u_char type, struct prefix_ipv4 p,
  116. ifindex_t ifindex, struct in_addr nexthop)
  117. {
  118. struct external_info *new;
  119. struct route_node *rn;
  120. /* Initialize route table. */
  121. if (EXTERNAL_INFO (type) == NULL)
  122. EXTERNAL_INFO (type) = route_table_init ();
  123. rn = route_node_get (EXTERNAL_INFO (type), (struct prefix *) &p);
  124. /* If old info exists, -- discard new one or overwrite with new one? */
  125. if (rn)
  126. if (rn->info)
  127. {
  128. route_unlock_node (rn);
  129. zlog_warn ("Redistribute[%s]: %s/%d already exists, discard.",
  130. ospf_redist_string(type),
  131. inet_ntoa (p.prefix), p.prefixlen);
  132. /* XFREE (MTYPE_OSPF_TMP, rn->info); */
  133. return rn->info;
  134. }
  135. /* Create new External info instance. */
  136. new = ospf_external_info_new (type);
  137. new->p = p;
  138. new->ifindex = ifindex;
  139. new->nexthop = nexthop;
  140. new->tag = 0;
  141. if (rn)
  142. rn->info = new;
  143. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  144. zlog_debug ("Redistribute[%s]: %s/%d external info created.",
  145. ospf_redist_string(type),
  146. inet_ntoa (p.prefix), p.prefixlen);
  147. return new;
  148. }
  149. void
  150. ospf_external_info_delete (u_char type, struct prefix_ipv4 p)
  151. {
  152. struct route_node *rn;
  153. rn = route_node_lookup (EXTERNAL_INFO (type), (struct prefix *) &p);
  154. if (rn)
  155. {
  156. ospf_external_info_free (rn->info);
  157. rn->info = NULL;
  158. route_unlock_node (rn);
  159. route_unlock_node (rn);
  160. }
  161. }
  162. struct external_info *
  163. ospf_external_info_lookup (u_char type, struct prefix_ipv4 *p)
  164. {
  165. struct route_node *rn;
  166. rn = route_node_lookup (EXTERNAL_INFO (type), (struct prefix *) p);
  167. if (rn)
  168. {
  169. route_unlock_node (rn);
  170. if (rn->info)
  171. return rn->info;
  172. }
  173. return NULL;
  174. }
  175. struct ospf_lsa *
  176. ospf_external_info_find_lsa (struct ospf *ospf,
  177. struct prefix_ipv4 *p)
  178. {
  179. struct ospf_lsa *lsa;
  180. struct as_external_lsa *al;
  181. struct in_addr mask, id;
  182. lsa = ospf_lsdb_lookup_by_id (ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
  183. p->prefix, ospf->router_id);
  184. if (!lsa)
  185. return NULL;
  186. al = (struct as_external_lsa *) lsa->data;
  187. masklen2ip (p->prefixlen, &mask);
  188. if (mask.s_addr != al->mask.s_addr)
  189. {
  190. id.s_addr = p->prefix.s_addr | (~mask.s_addr);
  191. lsa = ospf_lsdb_lookup_by_id (ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
  192. id, ospf->router_id);
  193. if (!lsa)
  194. return NULL;
  195. }
  196. return lsa;
  197. }
  198. /* Update ASBR status. */
  199. void
  200. ospf_asbr_status_update (struct ospf *ospf, u_char status)
  201. {
  202. zlog_info ("ASBR[Status:%d]: Update", status);
  203. /* ASBR on. */
  204. if (status)
  205. {
  206. /* Already ASBR. */
  207. if (IS_OSPF_ASBR (ospf))
  208. {
  209. zlog_info ("ASBR[Status:%d]: Already ASBR", status);
  210. return;
  211. }
  212. SET_FLAG (ospf->flags, OSPF_FLAG_ASBR);
  213. }
  214. else
  215. {
  216. /* Already non ASBR. */
  217. if (! IS_OSPF_ASBR (ospf))
  218. {
  219. zlog_info ("ASBR[Status:%d]: Already non ASBR", status);
  220. return;
  221. }
  222. UNSET_FLAG (ospf->flags, OSPF_FLAG_ASBR);
  223. }
  224. /* Transition from/to status ASBR, schedule timer. */
  225. ospf_spf_calculate_schedule (ospf, SPF_FLAG_ASBR_STATUS_CHANGE);
  226. ospf_router_lsa_update (ospf);
  227. }
  228. void
  229. ospf_redistribute_withdraw (struct ospf *ospf, u_char type)
  230. {
  231. struct route_node *rn;
  232. struct external_info *ei;
  233. /* Delete external info for specified type. */
  234. if (EXTERNAL_INFO (type))
  235. for (rn = route_top (EXTERNAL_INFO (type)); rn; rn = route_next (rn))
  236. if ((ei = rn->info))
  237. if (ospf_external_info_find_lsa (ospf, &ei->p))
  238. {
  239. if (is_prefix_default (&ei->p) &&
  240. ospf->default_originate != DEFAULT_ORIGINATE_NONE)
  241. continue;
  242. ospf_external_lsa_flush (ospf, type, &ei->p,
  243. ei->ifindex /*, ei->nexthop */);
  244. ospf_external_info_free (ei);
  245. route_unlock_node (rn);
  246. rn->info = NULL;
  247. }
  248. }