ospf_asbr.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. route_tag_t tag)
  118. {
  119. struct external_info *new;
  120. struct route_node *rn;
  121. /* Initialize route table. */
  122. if (EXTERNAL_INFO (type) == NULL)
  123. EXTERNAL_INFO (type) = route_table_init ();
  124. rn = route_node_get (EXTERNAL_INFO (type), (struct prefix *) &p);
  125. /* If old info exists, -- discard new one or overwrite with new one? */
  126. if (rn)
  127. if (rn->info)
  128. {
  129. route_unlock_node (rn);
  130. zlog_warn ("Redistribute[%s]: %s/%d already exists, discard.",
  131. ospf_redist_string(type),
  132. inet_ntoa (p.prefix), p.prefixlen);
  133. /* XFREE (MTYPE_OSPF_TMP, rn->info); */
  134. return rn->info;
  135. }
  136. /* Create new External info instance. */
  137. new = ospf_external_info_new (type);
  138. new->p = p;
  139. new->ifindex = ifindex;
  140. new->nexthop = nexthop;
  141. new->tag = tag;
  142. if (rn)
  143. rn->info = new;
  144. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  145. zlog_debug ("Redistribute[%s]: %s/%d external info created.",
  146. ospf_redist_string(type),
  147. inet_ntoa (p.prefix), p.prefixlen);
  148. return new;
  149. }
  150. void
  151. ospf_external_info_delete (u_char type, struct prefix_ipv4 p)
  152. {
  153. struct route_node *rn;
  154. rn = route_node_lookup (EXTERNAL_INFO (type), (struct prefix *) &p);
  155. if (rn)
  156. {
  157. ospf_external_info_free (rn->info);
  158. rn->info = NULL;
  159. route_unlock_node (rn);
  160. route_unlock_node (rn);
  161. }
  162. }
  163. struct external_info *
  164. ospf_external_info_lookup (u_char type, struct prefix_ipv4 *p)
  165. {
  166. struct route_node *rn;
  167. rn = route_node_lookup (EXTERNAL_INFO (type), (struct prefix *) p);
  168. if (rn)
  169. {
  170. route_unlock_node (rn);
  171. if (rn->info)
  172. return rn->info;
  173. }
  174. return NULL;
  175. }
  176. struct ospf_lsa *
  177. ospf_external_info_find_lsa (struct ospf *ospf,
  178. struct prefix_ipv4 *p)
  179. {
  180. struct ospf_lsa *lsa;
  181. struct as_external_lsa *al;
  182. struct in_addr mask, id;
  183. lsa = ospf_lsdb_lookup_by_id (ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
  184. p->prefix, ospf->router_id);
  185. if (!lsa)
  186. return NULL;
  187. al = (struct as_external_lsa *) lsa->data;
  188. masklen2ip (p->prefixlen, &mask);
  189. if (mask.s_addr != al->mask.s_addr)
  190. {
  191. id.s_addr = p->prefix.s_addr | (~mask.s_addr);
  192. lsa = ospf_lsdb_lookup_by_id (ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
  193. id, ospf->router_id);
  194. if (!lsa)
  195. return NULL;
  196. }
  197. return lsa;
  198. }
  199. /* Update ASBR status. */
  200. void
  201. ospf_asbr_status_update (struct ospf *ospf, u_char status)
  202. {
  203. zlog_info ("ASBR[Status:%d]: Update", status);
  204. /* ASBR on. */
  205. if (status)
  206. {
  207. /* Already ASBR. */
  208. if (IS_OSPF_ASBR (ospf))
  209. {
  210. zlog_info ("ASBR[Status:%d]: Already ASBR", status);
  211. return;
  212. }
  213. SET_FLAG (ospf->flags, OSPF_FLAG_ASBR);
  214. }
  215. else
  216. {
  217. /* Already non ASBR. */
  218. if (! IS_OSPF_ASBR (ospf))
  219. {
  220. zlog_info ("ASBR[Status:%d]: Already non ASBR", status);
  221. return;
  222. }
  223. UNSET_FLAG (ospf->flags, OSPF_FLAG_ASBR);
  224. }
  225. /* Transition from/to status ASBR, schedule timer. */
  226. ospf_spf_calculate_schedule (ospf, SPF_FLAG_ASBR_STATUS_CHANGE);
  227. ospf_router_lsa_update (ospf);
  228. }
  229. void
  230. ospf_redistribute_withdraw (struct ospf *ospf, u_char type)
  231. {
  232. struct route_node *rn;
  233. struct external_info *ei;
  234. /* Delete external info for specified type. */
  235. if (EXTERNAL_INFO (type))
  236. for (rn = route_top (EXTERNAL_INFO (type)); rn; rn = route_next (rn))
  237. if ((ei = rn->info))
  238. if (ospf_external_info_find_lsa (ospf, &ei->p))
  239. {
  240. if (is_prefix_default (&ei->p) &&
  241. ospf->default_originate != DEFAULT_ORIGINATE_NONE)
  242. continue;
  243. ospf_external_lsa_flush (ospf, type, &ei->p,
  244. ei->ifindex /*, ei->nexthop */);
  245. ospf_external_info_free (ei);
  246. route_unlock_node (rn);
  247. rn->info = NULL;
  248. }
  249. }