ospfd.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. /* OSPF version 2 daemon program.
  2. Copyright (C) 1999, 2000 Toshiaki Takada
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #include <zebra.h>
  17. #include "thread.h"
  18. #include "vty.h"
  19. #include "command.h"
  20. #include "linklist.h"
  21. #include "prefix.h"
  22. #include "table.h"
  23. #include "if.h"
  24. #include "memory.h"
  25. #include "stream.h"
  26. #include "log.h"
  27. #include "sockunion.h" /* for inet_aton () */
  28. #include "zclient.h"
  29. #include "plist.h"
  30. #include "sockopt.h"
  31. #include "ospfd/ospfd.h"
  32. #include "ospfd/ospf_network.h"
  33. #include "ospfd/ospf_interface.h"
  34. #include "ospfd/ospf_ism.h"
  35. #include "ospfd/ospf_asbr.h"
  36. #include "ospfd/ospf_lsa.h"
  37. #include "ospfd/ospf_lsdb.h"
  38. #include "ospfd/ospf_neighbor.h"
  39. #include "ospfd/ospf_nsm.h"
  40. #include "ospfd/ospf_spf.h"
  41. #include "ospfd/ospf_packet.h"
  42. #include "ospfd/ospf_dump.h"
  43. #include "ospfd/ospf_zebra.h"
  44. #include "ospfd/ospf_abr.h"
  45. #include "ospfd/ospf_flood.h"
  46. #include "ospfd/ospf_route.h"
  47. #include "ospfd/ospf_ase.h"
  48. /* OSPF process wide configuration. */
  49. static struct ospf_master ospf_master;
  50. /* OSPF process wide configuration pointer to export. */
  51. struct ospf_master *om;
  52. extern struct zclient *zclient;
  53. extern struct in_addr router_id_zebra;
  54. static void ospf_remove_vls_through_area (struct ospf *, struct ospf_area *);
  55. static void ospf_network_free (struct ospf *, struct ospf_network *);
  56. static void ospf_area_free (struct ospf_area *);
  57. static void ospf_network_run (struct prefix *, struct ospf_area *);
  58. static void ospf_network_run_interface (struct prefix *, struct ospf_area *,
  59. struct interface *);
  60. static int ospf_network_match_iface (const struct connected *,
  61. const struct prefix *);
  62. static void ospf_finish_final (struct ospf *);
  63. #define OSPF_EXTERNAL_LSA_ORIGINATE_DELAY 1
  64. void
  65. ospf_router_id_update (struct ospf *ospf)
  66. {
  67. struct in_addr router_id, router_id_old;
  68. struct ospf_interface *oi;
  69. struct interface *ifp;
  70. struct listnode *node;
  71. if (IS_DEBUG_OSPF_EVENT)
  72. zlog_debug ("Router-ID[OLD:%s]: Update", inet_ntoa (ospf->router_id));
  73. router_id_old = ospf->router_id;
  74. /* Select the router ID based on these priorities:
  75. 1. Statically assigned router ID is always the first choice.
  76. 2. If there is no statically assigned router ID, then try to stick
  77. with the most recent value, since changing router ID's is very
  78. disruptive.
  79. 3. Last choice: just go with whatever the zebra daemon recommends.
  80. */
  81. if (ospf->router_id_static.s_addr != 0)
  82. router_id = ospf->router_id_static;
  83. else if (ospf->router_id.s_addr != 0)
  84. router_id = ospf->router_id;
  85. else
  86. router_id = router_id_zebra;
  87. ospf->router_id = router_id;
  88. if (IS_DEBUG_OSPF_EVENT)
  89. zlog_debug ("Router-ID[NEW:%s]: Update", inet_ntoa (ospf->router_id));
  90. if (!IPV4_ADDR_SAME (&router_id_old, &router_id))
  91. {
  92. for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
  93. /* Update self-neighbor's router_id. */
  94. oi->nbr_self->router_id = router_id;
  95. /* If AS-external-LSA is queued, then flush those LSAs. */
  96. if (router_id_old.s_addr == 0 && ospf->external_origin)
  97. {
  98. int type;
  99. /* Originate each redistributed external route. */
  100. for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
  101. if (ospf->external_origin & (1 << type))
  102. thread_add_event (master, ospf_external_lsa_originate_timer,
  103. ospf, type);
  104. /* Originate Deafult. */
  105. if (ospf->external_origin & (1 << ZEBRA_ROUTE_MAX))
  106. thread_add_event (master, ospf_default_originate_timer, ospf, 0);
  107. ospf->external_origin = 0;
  108. }
  109. /* update router-lsa's for each area */
  110. ospf_router_lsa_update (ospf);
  111. /* update ospf_interface's */
  112. for (ALL_LIST_ELEMENTS_RO (om->iflist, node, ifp))
  113. ospf_if_update (ospf, ifp);
  114. }
  115. }
  116. /* For OSPF area sort by area id. */
  117. static int
  118. ospf_area_id_cmp (struct ospf_area *a1, struct ospf_area *a2)
  119. {
  120. if (ntohl (a1->area_id.s_addr) > ntohl (a2->area_id.s_addr))
  121. return 1;
  122. if (ntohl (a1->area_id.s_addr) < ntohl (a2->area_id.s_addr))
  123. return -1;
  124. return 0;
  125. }
  126. /* Allocate new ospf structure. */
  127. static struct ospf *
  128. ospf_new (void)
  129. {
  130. int i;
  131. struct ospf *new = XCALLOC (MTYPE_OSPF_TOP, sizeof (struct ospf));
  132. new->router_id.s_addr = htonl (0);
  133. new->router_id_static.s_addr = htonl (0);
  134. new->abr_type = OSPF_ABR_DEFAULT;
  135. new->oiflist = list_new ();
  136. new->vlinks = list_new ();
  137. new->areas = list_new ();
  138. new->areas->cmp = (int (*)(void *, void *)) ospf_area_id_cmp;
  139. new->networks = route_table_init ();
  140. new->nbr_nbma = route_table_init ();
  141. new->lsdb = ospf_lsdb_new ();
  142. new->default_originate = DEFAULT_ORIGINATE_NONE;
  143. new->passive_interface_default = OSPF_IF_ACTIVE;
  144. new->new_external_route = route_table_init ();
  145. new->old_external_route = route_table_init ();
  146. new->external_lsas = route_table_init ();
  147. new->stub_router_startup_time = OSPF_STUB_ROUTER_UNCONFIGURED;
  148. new->stub_router_shutdown_time = OSPF_STUB_ROUTER_UNCONFIGURED;
  149. new->stub_router_admin_set = OSPF_STUB_ROUTER_ADMINISTRATIVE_UNSET;
  150. /* Distribute parameter init. */
  151. for (i = 0; i <= ZEBRA_ROUTE_MAX; i++)
  152. {
  153. new->dmetric[i].type = -1;
  154. new->dmetric[i].value = -1;
  155. }
  156. new->default_metric = -1;
  157. new->ref_bandwidth = OSPF_DEFAULT_REF_BANDWIDTH;
  158. /* SPF timer value init. */
  159. new->spf_delay = OSPF_SPF_DELAY_DEFAULT;
  160. new->spf_holdtime = OSPF_SPF_HOLDTIME_DEFAULT;
  161. new->spf_max_holdtime = OSPF_SPF_MAX_HOLDTIME_DEFAULT;
  162. new->spf_hold_multiplier = 1;
  163. /* MaxAge init. */
  164. new->maxage_delay = OSPF_LSA_MAXAGE_REMOVE_DELAY_DEFAULT;
  165. new->maxage_lsa = route_table_init();
  166. new->t_maxage_walker =
  167. thread_add_timer (master, ospf_lsa_maxage_walker,
  168. new, OSPF_LSA_MAXAGE_CHECK_INTERVAL);
  169. /* Distance table init. */
  170. new->distance_table = route_table_init ();
  171. new->lsa_refresh_queue.index = 0;
  172. new->lsa_refresh_interval = OSPF_LSA_REFRESH_INTERVAL_DEFAULT;
  173. new->t_lsa_refresher = thread_add_timer (master, ospf_lsa_refresh_walker,
  174. new, new->lsa_refresh_interval);
  175. new->lsa_refresher_started = quagga_time (NULL);
  176. if ((new->fd = ospf_sock_init()) < 0)
  177. {
  178. zlog_err("ospf_new: fatal error: ospf_sock_init was unable to open "
  179. "a socket");
  180. exit(1);
  181. }
  182. new->maxsndbuflen = getsockopt_so_sendbuf (new->fd);
  183. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  184. zlog_debug ("%s: starting with OSPF send buffer size %u",
  185. __func__, new->maxsndbuflen);
  186. if ((new->ibuf = stream_new(OSPF_MAX_PACKET_SIZE+1)) == NULL)
  187. {
  188. zlog_err("ospf_new: fatal error: stream_new(%u) failed allocating ibuf",
  189. OSPF_MAX_PACKET_SIZE+1);
  190. exit(1);
  191. }
  192. new->t_read = thread_add_read (master, ospf_read, new, new->fd);
  193. new->oi_write_q = list_new ();
  194. return new;
  195. }
  196. struct ospf *
  197. ospf_lookup ()
  198. {
  199. if (listcount (om->ospf) == 0)
  200. return NULL;
  201. return listgetdata (listhead (om->ospf));
  202. }
  203. static void
  204. ospf_add (struct ospf *ospf)
  205. {
  206. listnode_add (om->ospf, ospf);
  207. }
  208. static void
  209. ospf_delete (struct ospf *ospf)
  210. {
  211. listnode_delete (om->ospf, ospf);
  212. }
  213. struct ospf *
  214. ospf_get ()
  215. {
  216. struct ospf *ospf;
  217. ospf = ospf_lookup ();
  218. if (ospf == NULL)
  219. {
  220. ospf = ospf_new ();
  221. ospf_add (ospf);
  222. if (ospf->router_id_static.s_addr == 0)
  223. ospf_router_id_update (ospf);
  224. #ifdef HAVE_OPAQUE_LSA
  225. ospf_opaque_type11_lsa_init (ospf);
  226. #endif /* HAVE_OPAQUE_LSA */
  227. }
  228. return ospf;
  229. }
  230. /* Handle the second half of deferred shutdown. This is called either
  231. * from the deferred-shutdown timer thread, or directly through
  232. * ospf_deferred_shutdown_check.
  233. *
  234. * Function is to cleanup G-R state, if required then call ospf_finish_final
  235. * to complete shutdown of this ospf instance. Possibly exit if the
  236. * whole process is being shutdown and this was the last OSPF instance.
  237. */
  238. static void
  239. ospf_deferred_shutdown_finish (struct ospf *ospf)
  240. {
  241. ospf->stub_router_shutdown_time = OSPF_STUB_ROUTER_UNCONFIGURED;
  242. OSPF_TIMER_OFF (ospf->t_deferred_shutdown);
  243. ospf_finish_final (ospf);
  244. /* *ospf is now invalid */
  245. /* ospfd being shut-down? If so, was this the last ospf instance? */
  246. if (CHECK_FLAG (om->options, OSPF_MASTER_SHUTDOWN)
  247. && (listcount (om->ospf) == 0))
  248. exit (0);
  249. return;
  250. }
  251. /* Timer thread for G-R */
  252. static int
  253. ospf_deferred_shutdown_timer (struct thread *t)
  254. {
  255. struct ospf *ospf = THREAD_ARG(t);
  256. ospf_deferred_shutdown_finish (ospf);
  257. return 0;
  258. }
  259. /* Check whether deferred-shutdown must be scheduled, otherwise call
  260. * down directly into second-half of instance shutdown.
  261. */
  262. static void
  263. ospf_deferred_shutdown_check (struct ospf *ospf)
  264. {
  265. unsigned long timeout;
  266. struct listnode *ln;
  267. struct ospf_area *area;
  268. /* deferred shutdown already running? */
  269. if (ospf->t_deferred_shutdown)
  270. return;
  271. /* Should we try push out max-metric LSAs? */
  272. if (ospf->stub_router_shutdown_time != OSPF_STUB_ROUTER_UNCONFIGURED)
  273. {
  274. for (ALL_LIST_ELEMENTS_RO (ospf->areas, ln, area))
  275. {
  276. SET_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED);
  277. if (!CHECK_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED))
  278. ospf_router_lsa_update_area (area);
  279. }
  280. timeout = ospf->stub_router_shutdown_time;
  281. }
  282. else
  283. {
  284. /* No timer needed */
  285. ospf_deferred_shutdown_finish (ospf);
  286. return;
  287. }
  288. OSPF_TIMER_ON (ospf->t_deferred_shutdown, ospf_deferred_shutdown_timer,
  289. timeout);
  290. return;
  291. }
  292. /* Shut down the entire process */
  293. void
  294. ospf_terminate (void)
  295. {
  296. struct ospf *ospf;
  297. struct listnode *node, *nnode;
  298. /* shutdown already in progress */
  299. if (CHECK_FLAG (om->options, OSPF_MASTER_SHUTDOWN))
  300. return;
  301. SET_FLAG (om->options, OSPF_MASTER_SHUTDOWN);
  302. /* exit immediately if OSPF not actually running */
  303. if (listcount(om->ospf) == 0)
  304. exit(0);
  305. for (ALL_LIST_ELEMENTS (om->ospf, node, nnode, ospf))
  306. ospf_finish (ospf);
  307. /* Deliberately go back up, hopefully to thread scheduler, as
  308. * One or more ospf_finish()'s may have deferred shutdown to a timer
  309. * thread
  310. */
  311. }
  312. void
  313. ospf_finish (struct ospf *ospf)
  314. {
  315. /* let deferred shutdown decide */
  316. ospf_deferred_shutdown_check (ospf);
  317. /* if ospf_deferred_shutdown returns, then ospf_finish_final is
  318. * deferred to expiry of G-S timer thread. Return back up, hopefully
  319. * to thread scheduler.
  320. */
  321. return;
  322. }
  323. /* Final cleanup of ospf instance */
  324. static void
  325. ospf_finish_final (struct ospf *ospf)
  326. {
  327. struct route_node *rn;
  328. struct ospf_nbr_nbma *nbr_nbma;
  329. struct ospf_lsa *lsa;
  330. struct ospf_interface *oi;
  331. struct ospf_area *area;
  332. struct ospf_vl_data *vl_data;
  333. struct listnode *node, *nnode;
  334. int i;
  335. #ifdef HAVE_OPAQUE_LSA
  336. ospf_opaque_type11_lsa_term (ospf);
  337. #endif /* HAVE_OPAQUE_LSA */
  338. /* be nice if this worked, but it doesn't */
  339. /*ospf_flush_self_originated_lsas_now (ospf);*/
  340. /* Unregister redistribution */
  341. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  342. ospf_redistribute_unset (ospf, i);
  343. ospf_redistribute_default_unset (ospf);
  344. for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
  345. ospf_remove_vls_through_area (ospf, area);
  346. for (ALL_LIST_ELEMENTS (ospf->vlinks, node, nnode, vl_data))
  347. ospf_vl_delete (ospf, vl_data);
  348. list_delete (ospf->vlinks);
  349. /* Reset interface. */
  350. for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
  351. ospf_if_free (oi);
  352. /* Clear static neighbors */
  353. for (rn = route_top (ospf->nbr_nbma); rn; rn = route_next (rn))
  354. if ((nbr_nbma = rn->info))
  355. {
  356. OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
  357. if (nbr_nbma->nbr)
  358. {
  359. nbr_nbma->nbr->nbr_nbma = NULL;
  360. nbr_nbma->nbr = NULL;
  361. }
  362. if (nbr_nbma->oi)
  363. {
  364. listnode_delete (nbr_nbma->oi->nbr_nbma, nbr_nbma);
  365. nbr_nbma->oi = NULL;
  366. }
  367. XFREE (MTYPE_OSPF_NEIGHBOR_STATIC, nbr_nbma);
  368. }
  369. route_table_finish (ospf->nbr_nbma);
  370. /* Clear networks and Areas. */
  371. for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
  372. {
  373. struct ospf_network *network;
  374. if ((network = rn->info) != NULL)
  375. {
  376. ospf_network_free (ospf, network);
  377. rn->info = NULL;
  378. route_unlock_node (rn);
  379. }
  380. }
  381. for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
  382. {
  383. listnode_delete (ospf->areas, area);
  384. ospf_area_free (area);
  385. }
  386. /* Cancel all timers. */
  387. OSPF_TIMER_OFF (ospf->t_external_lsa);
  388. OSPF_TIMER_OFF (ospf->t_spf_calc);
  389. OSPF_TIMER_OFF (ospf->t_ase_calc);
  390. OSPF_TIMER_OFF (ospf->t_maxage);
  391. OSPF_TIMER_OFF (ospf->t_maxage_walker);
  392. OSPF_TIMER_OFF (ospf->t_abr_task);
  393. OSPF_TIMER_OFF (ospf->t_asbr_check);
  394. OSPF_TIMER_OFF (ospf->t_distribute_update);
  395. OSPF_TIMER_OFF (ospf->t_lsa_refresher);
  396. OSPF_TIMER_OFF (ospf->t_read);
  397. OSPF_TIMER_OFF (ospf->t_write);
  398. #ifdef HAVE_OPAQUE_LSA
  399. OSPF_TIMER_OFF (ospf->t_opaque_lsa_self);
  400. #endif
  401. close (ospf->fd);
  402. stream_free(ospf->ibuf);
  403. #ifdef HAVE_OPAQUE_LSA
  404. LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
  405. ospf_discard_from_db (ospf, ospf->lsdb, lsa);
  406. #endif /* HAVE_OPAQUE_LSA */
  407. LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
  408. ospf_discard_from_db (ospf, ospf->lsdb, lsa);
  409. ospf_lsdb_delete_all (ospf->lsdb);
  410. ospf_lsdb_free (ospf->lsdb);
  411. for (rn = route_top (ospf->maxage_lsa); rn; rn = route_next (rn))
  412. {
  413. struct ospf_lsa *lsa;
  414. if ((lsa = rn->info) != NULL)
  415. {
  416. ospf_lsa_unlock (&lsa);
  417. rn->info = NULL;
  418. }
  419. route_unlock_node (rn);
  420. }
  421. route_table_finish (ospf->maxage_lsa);
  422. if (ospf->old_table)
  423. ospf_route_table_free (ospf->old_table);
  424. if (ospf->new_table)
  425. {
  426. ospf_route_delete (ospf->new_table);
  427. ospf_route_table_free (ospf->new_table);
  428. }
  429. if (ospf->old_rtrs)
  430. ospf_rtrs_free (ospf->old_rtrs);
  431. if (ospf->new_rtrs)
  432. ospf_rtrs_free (ospf->new_rtrs);
  433. if (ospf->new_external_route)
  434. {
  435. ospf_route_delete (ospf->new_external_route);
  436. ospf_route_table_free (ospf->new_external_route);
  437. }
  438. if (ospf->old_external_route)
  439. {
  440. ospf_route_delete (ospf->old_external_route);
  441. ospf_route_table_free (ospf->old_external_route);
  442. }
  443. if (ospf->external_lsas)
  444. {
  445. ospf_ase_external_lsas_finish (ospf->external_lsas);
  446. }
  447. list_delete (ospf->areas);
  448. for (i = ZEBRA_ROUTE_SYSTEM; i <= ZEBRA_ROUTE_MAX; i++)
  449. if (EXTERNAL_INFO (i) != NULL)
  450. for (rn = route_top (EXTERNAL_INFO (i)); rn; rn = route_next (rn))
  451. {
  452. if (rn->info == NULL)
  453. continue;
  454. XFREE (MTYPE_OSPF_EXTERNAL_INFO, rn->info);
  455. rn->info = NULL;
  456. route_unlock_node (rn);
  457. }
  458. ospf_distance_reset (ospf);
  459. route_table_finish (ospf->distance_table);
  460. ospf_delete (ospf);
  461. XFREE (MTYPE_OSPF_TOP, ospf);
  462. }
  463. /* allocate new OSPF Area object */
  464. static struct ospf_area *
  465. ospf_area_new (struct ospf *ospf, struct in_addr area_id)
  466. {
  467. struct ospf_area *new;
  468. /* Allocate new config_network. */
  469. new = XCALLOC (MTYPE_OSPF_AREA, sizeof (struct ospf_area));
  470. new->ospf = ospf;
  471. new->area_id = area_id;
  472. new->external_routing = OSPF_AREA_DEFAULT;
  473. new->default_cost = 1;
  474. new->auth_type = OSPF_AUTH_NULL;
  475. /* New LSDB init. */
  476. new->lsdb = ospf_lsdb_new ();
  477. /* Self-originated LSAs initialize. */
  478. new->router_lsa_self = NULL;
  479. #ifdef HAVE_OPAQUE_LSA
  480. ospf_opaque_type10_lsa_init (new);
  481. #endif /* HAVE_OPAQUE_LSA */
  482. new->oiflist = list_new ();
  483. new->ranges = route_table_init ();
  484. if (area_id.s_addr == OSPF_AREA_BACKBONE)
  485. ospf->backbone = new;
  486. return new;
  487. }
  488. static void
  489. ospf_area_free (struct ospf_area *area)
  490. {
  491. struct route_node *rn;
  492. struct ospf_lsa *lsa;
  493. /* Free LSDBs. */
  494. LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
  495. ospf_discard_from_db (area->ospf, area->lsdb, lsa);
  496. LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
  497. ospf_discard_from_db (area->ospf, area->lsdb, lsa);
  498. LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
  499. ospf_discard_from_db (area->ospf, area->lsdb, lsa);
  500. LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
  501. ospf_discard_from_db (area->ospf, area->lsdb, lsa);
  502. LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
  503. ospf_discard_from_db (area->ospf, area->lsdb, lsa);
  504. #ifdef HAVE_OPAQUE_LSA
  505. LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
  506. ospf_discard_from_db (area->ospf, area->lsdb, lsa);
  507. LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
  508. ospf_discard_from_db (area->ospf, area->lsdb, lsa);
  509. #endif /* HAVE_OPAQUE_LSA */
  510. ospf_lsdb_delete_all (area->lsdb);
  511. ospf_lsdb_free (area->lsdb);
  512. ospf_lsa_unlock (&area->router_lsa_self);
  513. route_table_finish (area->ranges);
  514. list_delete (area->oiflist);
  515. if (EXPORT_NAME (area))
  516. free (EXPORT_NAME (area));
  517. if (IMPORT_NAME (area))
  518. free (IMPORT_NAME (area));
  519. /* Cancel timer. */
  520. OSPF_TIMER_OFF (area->t_stub_router);
  521. #ifdef HAVE_OPAQUE_LSA
  522. OSPF_TIMER_OFF (area->t_opaque_lsa_self);
  523. #endif /* HAVE_OPAQUE_LSA */
  524. if (OSPF_IS_AREA_BACKBONE (area))
  525. area->ospf->backbone = NULL;
  526. XFREE (MTYPE_OSPF_AREA, area);
  527. }
  528. void
  529. ospf_area_check_free (struct ospf *ospf, struct in_addr area_id)
  530. {
  531. struct ospf_area *area;
  532. area = ospf_area_lookup_by_area_id (ospf, area_id);
  533. if (area &&
  534. listcount (area->oiflist) == 0 &&
  535. area->ranges->top == NULL &&
  536. area->shortcut_configured == OSPF_SHORTCUT_DEFAULT &&
  537. area->external_routing == OSPF_AREA_DEFAULT &&
  538. area->no_summary == 0 &&
  539. area->default_cost == 1 &&
  540. EXPORT_NAME (area) == NULL &&
  541. IMPORT_NAME (area) == NULL &&
  542. area->auth_type == OSPF_AUTH_NULL)
  543. {
  544. listnode_delete (ospf->areas, area);
  545. ospf_area_free (area);
  546. }
  547. }
  548. struct ospf_area *
  549. ospf_area_get (struct ospf *ospf, struct in_addr area_id, int format)
  550. {
  551. struct ospf_area *area;
  552. area = ospf_area_lookup_by_area_id (ospf, area_id);
  553. if (!area)
  554. {
  555. area = ospf_area_new (ospf, area_id);
  556. area->format = format;
  557. listnode_add_sort (ospf->areas, area);
  558. ospf_check_abr_status (ospf);
  559. if (ospf->stub_router_admin_set == OSPF_STUB_ROUTER_ADMINISTRATIVE_SET)
  560. {
  561. SET_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED);
  562. }
  563. }
  564. return area;
  565. }
  566. struct ospf_area *
  567. ospf_area_lookup_by_area_id (struct ospf *ospf, struct in_addr area_id)
  568. {
  569. struct ospf_area *area;
  570. struct listnode *node;
  571. for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
  572. if (IPV4_ADDR_SAME (&area->area_id, &area_id))
  573. return area;
  574. return NULL;
  575. }
  576. void
  577. ospf_area_add_if (struct ospf_area *area, struct ospf_interface *oi)
  578. {
  579. listnode_add (area->oiflist, oi);
  580. }
  581. void
  582. ospf_area_del_if (struct ospf_area *area, struct ospf_interface *oi)
  583. {
  584. listnode_delete (area->oiflist, oi);
  585. }
  586. /* Config network statement related functions. */
  587. static struct ospf_network *
  588. ospf_network_new (struct in_addr area_id, int format)
  589. {
  590. struct ospf_network *new;
  591. new = XCALLOC (MTYPE_OSPF_NETWORK, sizeof (struct ospf_network));
  592. new->area_id = area_id;
  593. new->format = format;
  594. return new;
  595. }
  596. static void
  597. ospf_network_free (struct ospf *ospf, struct ospf_network *network)
  598. {
  599. ospf_area_check_free (ospf, network->area_id);
  600. ospf_schedule_abr_task (ospf);
  601. XFREE (MTYPE_OSPF_NETWORK, network);
  602. }
  603. int
  604. ospf_network_set (struct ospf *ospf, struct prefix_ipv4 *p,
  605. struct in_addr area_id)
  606. {
  607. struct ospf_network *network;
  608. struct ospf_area *area;
  609. struct route_node *rn;
  610. struct external_info *ei;
  611. int ret = OSPF_AREA_ID_FORMAT_ADDRESS;
  612. rn = route_node_get (ospf->networks, (struct prefix *)p);
  613. if (rn->info)
  614. {
  615. /* There is already same network statement. */
  616. route_unlock_node (rn);
  617. return 0;
  618. }
  619. rn->info = network = ospf_network_new (area_id, ret);
  620. area = ospf_area_get (ospf, area_id, ret);
  621. /* Run network config now. */
  622. ospf_network_run ((struct prefix *)p, area);
  623. /* Update connected redistribute. */
  624. if (ospf_is_type_redistributed (ZEBRA_ROUTE_CONNECT))
  625. if (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT))
  626. for (rn = route_top (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT));
  627. rn; rn = route_next (rn))
  628. if ((ei = rn->info) != NULL)
  629. if (ospf_external_info_find_lsa (ospf, &ei->p))
  630. if (!ospf_distribute_check_connected (ospf, ei))
  631. ospf_external_lsa_flush (ospf, ei->type, &ei->p,
  632. ei->ifindex /*, ei->nexthop */);
  633. ospf_area_check_free (ospf, area_id);
  634. return 1;
  635. }
  636. int
  637. ospf_network_unset (struct ospf *ospf, struct prefix_ipv4 *p,
  638. struct in_addr area_id)
  639. {
  640. struct route_node *rn;
  641. struct ospf_network *network;
  642. struct external_info *ei;
  643. struct listnode *node, *nnode;
  644. struct ospf_interface *oi;
  645. rn = route_node_lookup (ospf->networks, (struct prefix *)p);
  646. if (rn == NULL)
  647. return 0;
  648. network = rn->info;
  649. route_unlock_node (rn);
  650. if (!IPV4_ADDR_SAME (&area_id, &network->area_id))
  651. return 0;
  652. ospf_network_free (ospf, rn->info);
  653. rn->info = NULL;
  654. route_unlock_node (rn); /* initial reference */
  655. /* Find interfaces that not configured already. */
  656. for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
  657. {
  658. int found = 0;
  659. struct connected *co = oi->connected;
  660. if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  661. continue;
  662. for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
  663. {
  664. if (rn->info == NULL)
  665. continue;
  666. if (ospf_network_match_iface(co,&rn->p))
  667. {
  668. found = 1;
  669. route_unlock_node (rn);
  670. break;
  671. }
  672. }
  673. if (found == 0)
  674. {
  675. ospf_if_free (oi);
  676. ospf_area_check_free (ospf, area_id);
  677. }
  678. }
  679. /* Update connected redistribute. */
  680. if (ospf_is_type_redistributed (ZEBRA_ROUTE_CONNECT))
  681. if (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT))
  682. for (rn = route_top (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT));
  683. rn; rn = route_next (rn))
  684. if ((ei = rn->info) != NULL)
  685. if (!ospf_external_info_find_lsa (ospf, &ei->p))
  686. if (ospf_distribute_check_connected (ospf, ei))
  687. ospf_external_lsa_originate (ospf, ei);
  688. return 1;
  689. }
  690. /* Check whether interface matches given network
  691. * returns: 1, true. 0, false
  692. */
  693. static int
  694. ospf_network_match_iface(const struct connected *co, const struct prefix *net)
  695. {
  696. /* new approach: more elegant and conceptually clean */
  697. return prefix_match(net, CONNECTED_PREFIX(co));
  698. }
  699. static void
  700. ospf_network_run_interface (struct prefix *p, struct ospf_area *area,
  701. struct interface *ifp)
  702. {
  703. struct listnode *cnode;
  704. struct connected *co;
  705. if (memcmp (ifp->name, "VLINK", 5) == 0)
  706. return;
  707. /* if interface prefix is match specified prefix,
  708. then create socket and join multicast group. */
  709. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, co))
  710. {
  711. if (CHECK_FLAG(co->flags,ZEBRA_IFA_SECONDARY))
  712. continue;
  713. if (p->family == co->address->family
  714. && ! ospf_if_table_lookup(ifp, co->address)
  715. && ospf_network_match_iface(co,p))
  716. {
  717. struct ospf_interface *oi;
  718. oi = ospf_if_new (area->ospf, ifp, co->address);
  719. oi->connected = co;
  720. oi->area = area;
  721. oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4);
  722. oi->output_cost = ospf_if_get_output_cost (oi);
  723. /* Add pseudo neighbor. */
  724. ospf_nbr_add_self (oi);
  725. /* Relate ospf interface to ospf instance. */
  726. oi->ospf = area->ospf;
  727. /* update network type as interface flag */
  728. /* If network type is specified previously,
  729. skip network type setting. */
  730. oi->type = IF_DEF_PARAMS (ifp)->type;
  731. ospf_area_add_if (oi->area, oi);
  732. /* if router_id is not configured, dont bring up
  733. * interfaces.
  734. * ospf_router_id_update() will call ospf_if_update
  735. * whenever r-id is configured instead.
  736. */
  737. if ((area->ospf->router_id.s_addr != 0)
  738. && if_is_operative (ifp))
  739. ospf_if_up (oi);
  740. }
  741. }
  742. }
  743. static void
  744. ospf_network_run (struct prefix *p, struct ospf_area *area)
  745. {
  746. struct interface *ifp;
  747. struct listnode *node;
  748. /* Schedule Router ID Update. */
  749. if (area->ospf->router_id.s_addr == 0)
  750. ospf_router_id_update (area->ospf);
  751. /* Get target interface. */
  752. for (ALL_LIST_ELEMENTS_RO (om->iflist, node, ifp))
  753. ospf_network_run_interface (p, area, ifp);
  754. }
  755. void
  756. ospf_ls_upd_queue_empty (struct ospf_interface *oi)
  757. {
  758. struct route_node *rn;
  759. struct listnode *node, *nnode;
  760. struct list *lst;
  761. struct ospf_lsa *lsa;
  762. /* empty ls update queue */
  763. for (rn = route_top (oi->ls_upd_queue); rn;
  764. rn = route_next (rn))
  765. if ((lst = (struct list *) rn->info))
  766. {
  767. for (ALL_LIST_ELEMENTS (lst, node, nnode, lsa))
  768. ospf_lsa_unlock (&lsa); /* oi->ls_upd_queue */
  769. list_free (lst);
  770. rn->info = NULL;
  771. }
  772. /* remove update event */
  773. if (oi->t_ls_upd_event)
  774. {
  775. thread_cancel (oi->t_ls_upd_event);
  776. oi->t_ls_upd_event = NULL;
  777. }
  778. }
  779. void
  780. ospf_if_update (struct ospf *ospf, struct interface *ifp)
  781. {
  782. struct route_node *rn;
  783. struct ospf_network *network;
  784. struct ospf_area *area;
  785. if (!ospf)
  786. ospf = ospf_lookup ();
  787. /* OSPF must be on and Router-ID must be configured. */
  788. if (!ospf || ospf->router_id.s_addr == 0)
  789. return;
  790. /* Run each netowrk for this interface. */
  791. for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
  792. if (rn->info != NULL)
  793. {
  794. network = (struct ospf_network *) rn->info;
  795. area = ospf_area_get (ospf, network->area_id, network->format);
  796. ospf_network_run_interface (&rn->p, area, ifp);
  797. }
  798. }
  799. void
  800. ospf_remove_vls_through_area (struct ospf *ospf, struct ospf_area *area)
  801. {
  802. struct listnode *node, *nnode;
  803. struct ospf_vl_data *vl_data;
  804. for (ALL_LIST_ELEMENTS (ospf->vlinks, node, nnode, vl_data))
  805. if (IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
  806. ospf_vl_delete (ospf, vl_data);
  807. }
  808. static const struct message ospf_area_type_msg[] =
  809. {
  810. { OSPF_AREA_DEFAULT, "Default" },
  811. { OSPF_AREA_STUB, "Stub" },
  812. { OSPF_AREA_NSSA, "NSSA" },
  813. };
  814. static const int ospf_area_type_msg_max = OSPF_AREA_TYPE_MAX;
  815. static void
  816. ospf_area_type_set (struct ospf_area *area, int type)
  817. {
  818. struct listnode *node;
  819. struct ospf_interface *oi;
  820. if (area->external_routing == type)
  821. {
  822. if (IS_DEBUG_OSPF_EVENT)
  823. zlog_debug ("Area[%s]: Types are the same, ignored.",
  824. inet_ntoa (area->area_id));
  825. return;
  826. }
  827. area->external_routing = type;
  828. if (IS_DEBUG_OSPF_EVENT)
  829. zlog_debug ("Area[%s]: Configured as %s", inet_ntoa (area->area_id),
  830. LOOKUP (ospf_area_type_msg, type));
  831. switch (area->external_routing)
  832. {
  833. case OSPF_AREA_DEFAULT:
  834. for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
  835. if (oi->nbr_self != NULL)
  836. {
  837. UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
  838. SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
  839. }
  840. break;
  841. case OSPF_AREA_STUB:
  842. for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
  843. if (oi->nbr_self != NULL)
  844. {
  845. if (IS_DEBUG_OSPF_EVENT)
  846. zlog_debug ("setting options on %s accordingly", IF_NAME (oi));
  847. UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
  848. UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
  849. if (IS_DEBUG_OSPF_EVENT)
  850. zlog_debug ("options set on %s: %x",
  851. IF_NAME (oi), OPTIONS (oi));
  852. }
  853. break;
  854. case OSPF_AREA_NSSA:
  855. for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
  856. if (oi->nbr_self != NULL)
  857. {
  858. zlog_debug ("setting nssa options on %s accordingly", IF_NAME (oi));
  859. UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
  860. SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
  861. zlog_debug ("options set on %s: %x", IF_NAME (oi), OPTIONS (oi));
  862. }
  863. break;
  864. default:
  865. break;
  866. }
  867. ospf_router_lsa_update_area (area);
  868. ospf_schedule_abr_task (area->ospf);
  869. }
  870. int
  871. ospf_area_shortcut_set (struct ospf *ospf, struct ospf_area *area, int mode)
  872. {
  873. if (area->shortcut_configured == mode)
  874. return 0;
  875. area->shortcut_configured = mode;
  876. ospf_router_lsa_update_area (area);
  877. ospf_schedule_abr_task (ospf);
  878. ospf_area_check_free (ospf, area->area_id);
  879. return 1;
  880. }
  881. int
  882. ospf_area_shortcut_unset (struct ospf *ospf, struct ospf_area *area)
  883. {
  884. area->shortcut_configured = OSPF_SHORTCUT_DEFAULT;
  885. ospf_router_lsa_update_area (area);
  886. ospf_area_check_free (ospf, area->area_id);
  887. ospf_schedule_abr_task (ospf);
  888. return 1;
  889. }
  890. static int
  891. ospf_area_vlink_count (struct ospf *ospf, struct ospf_area *area)
  892. {
  893. struct ospf_vl_data *vl;
  894. struct listnode *node;
  895. int count = 0;
  896. for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl))
  897. if (IPV4_ADDR_SAME (&vl->vl_area_id, &area->area_id))
  898. count++;
  899. return count;
  900. }
  901. int
  902. ospf_area_stub_set (struct ospf *ospf, struct in_addr area_id)
  903. {
  904. struct ospf_area *area;
  905. int format = OSPF_AREA_ID_FORMAT_ADDRESS;
  906. area = ospf_area_get (ospf, area_id, format);
  907. if (ospf_area_vlink_count (ospf, area))
  908. return 0;
  909. if (area->external_routing != OSPF_AREA_STUB)
  910. ospf_area_type_set (area, OSPF_AREA_STUB);
  911. return 1;
  912. }
  913. int
  914. ospf_area_stub_unset (struct ospf *ospf, struct in_addr area_id)
  915. {
  916. struct ospf_area *area;
  917. area = ospf_area_lookup_by_area_id (ospf, area_id);
  918. if (area == NULL)
  919. return 1;
  920. if (area->external_routing == OSPF_AREA_STUB)
  921. ospf_area_type_set (area, OSPF_AREA_DEFAULT);
  922. ospf_area_check_free (ospf, area_id);
  923. return 1;
  924. }
  925. int
  926. ospf_area_no_summary_set (struct ospf *ospf, struct in_addr area_id)
  927. {
  928. struct ospf_area *area;
  929. int format = OSPF_AREA_ID_FORMAT_ADDRESS;
  930. area = ospf_area_get (ospf, area_id, format);
  931. area->no_summary = 1;
  932. return 1;
  933. }
  934. int
  935. ospf_area_no_summary_unset (struct ospf *ospf, struct in_addr area_id)
  936. {
  937. struct ospf_area *area;
  938. area = ospf_area_lookup_by_area_id (ospf, area_id);
  939. if (area == NULL)
  940. return 0;
  941. area->no_summary = 0;
  942. ospf_area_check_free (ospf, area_id);
  943. return 1;
  944. }
  945. int
  946. ospf_area_nssa_set (struct ospf *ospf, struct in_addr area_id)
  947. {
  948. struct ospf_area *area;
  949. int format = OSPF_AREA_ID_FORMAT_ADDRESS;
  950. area = ospf_area_get (ospf, area_id, format);
  951. if (ospf_area_vlink_count (ospf, area))
  952. return 0;
  953. if (area->external_routing != OSPF_AREA_NSSA)
  954. {
  955. ospf_area_type_set (area, OSPF_AREA_NSSA);
  956. ospf->anyNSSA++;
  957. }
  958. /* set NSSA area defaults */
  959. area->no_summary = 0;
  960. area->NSSATranslatorRole = OSPF_NSSA_ROLE_CANDIDATE;
  961. area->NSSATranslatorState = OSPF_NSSA_TRANSLATE_DISABLED;
  962. area->NSSATranslatorStabilityInterval = OSPF_NSSA_TRANS_STABLE_DEFAULT;
  963. return 1;
  964. }
  965. int
  966. ospf_area_nssa_unset (struct ospf *ospf, struct in_addr area_id)
  967. {
  968. struct ospf_area *area;
  969. area = ospf_area_lookup_by_area_id (ospf, area_id);
  970. if (area == NULL)
  971. return 0;
  972. if (area->external_routing == OSPF_AREA_NSSA)
  973. {
  974. ospf->anyNSSA--;
  975. ospf_area_type_set (area, OSPF_AREA_DEFAULT);
  976. }
  977. ospf_area_check_free (ospf, area_id);
  978. return 1;
  979. }
  980. int
  981. ospf_area_nssa_translator_role_set (struct ospf *ospf, struct in_addr area_id,
  982. int role)
  983. {
  984. struct ospf_area *area;
  985. area = ospf_area_lookup_by_area_id (ospf, area_id);
  986. if (area == NULL)
  987. return 0;
  988. area->NSSATranslatorRole = role;
  989. return 1;
  990. }
  991. #if 0
  992. /* XXX: unused? Leave for symmetry? */
  993. static int
  994. ospf_area_nssa_translator_role_unset (struct ospf *ospf,
  995. struct in_addr area_id)
  996. {
  997. struct ospf_area *area;
  998. area = ospf_area_lookup_by_area_id (ospf, area_id);
  999. if (area == NULL)
  1000. return 0;
  1001. area->NSSATranslatorRole = OSPF_NSSA_ROLE_CANDIDATE;
  1002. ospf_area_check_free (ospf, area_id);
  1003. return 1;
  1004. }
  1005. #endif
  1006. int
  1007. ospf_area_export_list_set (struct ospf *ospf,
  1008. struct ospf_area *area, const char *list_name)
  1009. {
  1010. struct access_list *list;
  1011. list = access_list_lookup (AFI_IP, list_name);
  1012. EXPORT_LIST (area) = list;
  1013. if (EXPORT_NAME (area))
  1014. free (EXPORT_NAME (area));
  1015. EXPORT_NAME (area) = strdup (list_name);
  1016. ospf_schedule_abr_task (ospf);
  1017. return 1;
  1018. }
  1019. int
  1020. ospf_area_export_list_unset (struct ospf *ospf, struct ospf_area * area)
  1021. {
  1022. EXPORT_LIST (area) = 0;
  1023. if (EXPORT_NAME (area))
  1024. free (EXPORT_NAME (area));
  1025. EXPORT_NAME (area) = NULL;
  1026. ospf_area_check_free (ospf, area->area_id);
  1027. ospf_schedule_abr_task (ospf);
  1028. return 1;
  1029. }
  1030. int
  1031. ospf_area_import_list_set (struct ospf *ospf, struct ospf_area *area,
  1032. const char *name)
  1033. {
  1034. struct access_list *list;
  1035. list = access_list_lookup (AFI_IP, name);
  1036. IMPORT_LIST (area) = list;
  1037. if (IMPORT_NAME (area))
  1038. free (IMPORT_NAME (area));
  1039. IMPORT_NAME (area) = strdup (name);
  1040. ospf_schedule_abr_task (ospf);
  1041. return 1;
  1042. }
  1043. int
  1044. ospf_area_import_list_unset (struct ospf *ospf, struct ospf_area * area)
  1045. {
  1046. IMPORT_LIST (area) = 0;
  1047. if (IMPORT_NAME (area))
  1048. free (IMPORT_NAME (area));
  1049. IMPORT_NAME (area) = NULL;
  1050. ospf_area_check_free (ospf, area->area_id);
  1051. ospf_schedule_abr_task (ospf);
  1052. return 1;
  1053. }
  1054. int
  1055. ospf_timers_refresh_set (struct ospf *ospf, int interval)
  1056. {
  1057. int time_left;
  1058. if (ospf->lsa_refresh_interval == interval)
  1059. return 1;
  1060. time_left = ospf->lsa_refresh_interval -
  1061. (quagga_time (NULL) - ospf->lsa_refresher_started);
  1062. if (time_left > interval)
  1063. {
  1064. OSPF_TIMER_OFF (ospf->t_lsa_refresher);
  1065. ospf->t_lsa_refresher =
  1066. thread_add_timer (master, ospf_lsa_refresh_walker, ospf, interval);
  1067. }
  1068. ospf->lsa_refresh_interval = interval;
  1069. return 1;
  1070. }
  1071. int
  1072. ospf_timers_refresh_unset (struct ospf *ospf)
  1073. {
  1074. int time_left;
  1075. time_left = ospf->lsa_refresh_interval -
  1076. (quagga_time (NULL) - ospf->lsa_refresher_started);
  1077. if (time_left > OSPF_LSA_REFRESH_INTERVAL_DEFAULT)
  1078. {
  1079. OSPF_TIMER_OFF (ospf->t_lsa_refresher);
  1080. ospf->t_lsa_refresher =
  1081. thread_add_timer (master, ospf_lsa_refresh_walker, ospf,
  1082. OSPF_LSA_REFRESH_INTERVAL_DEFAULT);
  1083. }
  1084. ospf->lsa_refresh_interval = OSPF_LSA_REFRESH_INTERVAL_DEFAULT;
  1085. return 1;
  1086. }
  1087. static struct ospf_nbr_nbma *
  1088. ospf_nbr_nbma_new (void)
  1089. {
  1090. struct ospf_nbr_nbma *nbr_nbma;
  1091. nbr_nbma = XCALLOC (MTYPE_OSPF_NEIGHBOR_STATIC,
  1092. sizeof (struct ospf_nbr_nbma));
  1093. nbr_nbma->priority = OSPF_NEIGHBOR_PRIORITY_DEFAULT;
  1094. nbr_nbma->v_poll = OSPF_POLL_INTERVAL_DEFAULT;
  1095. return nbr_nbma;
  1096. }
  1097. static void
  1098. ospf_nbr_nbma_free (struct ospf_nbr_nbma *nbr_nbma)
  1099. {
  1100. XFREE (MTYPE_OSPF_NEIGHBOR_STATIC, nbr_nbma);
  1101. }
  1102. static void
  1103. ospf_nbr_nbma_delete (struct ospf *ospf, struct ospf_nbr_nbma *nbr_nbma)
  1104. {
  1105. struct route_node *rn;
  1106. struct prefix_ipv4 p;
  1107. p.family = AF_INET;
  1108. p.prefix = nbr_nbma->addr;
  1109. p.prefixlen = IPV4_MAX_BITLEN;
  1110. rn = route_node_lookup (ospf->nbr_nbma, (struct prefix *)&p);
  1111. if (rn)
  1112. {
  1113. ospf_nbr_nbma_free (rn->info);
  1114. rn->info = NULL;
  1115. route_unlock_node (rn);
  1116. route_unlock_node (rn);
  1117. }
  1118. }
  1119. static void
  1120. ospf_nbr_nbma_down (struct ospf_nbr_nbma *nbr_nbma)
  1121. {
  1122. OSPF_TIMER_OFF (nbr_nbma->t_poll);
  1123. if (nbr_nbma->nbr)
  1124. {
  1125. nbr_nbma->nbr->nbr_nbma = NULL;
  1126. OSPF_NSM_EVENT_EXECUTE (nbr_nbma->nbr, NSM_KillNbr);
  1127. }
  1128. if (nbr_nbma->oi)
  1129. listnode_delete (nbr_nbma->oi->nbr_nbma, nbr_nbma);
  1130. }
  1131. static void
  1132. ospf_nbr_nbma_add (struct ospf_nbr_nbma *nbr_nbma,
  1133. struct ospf_interface *oi)
  1134. {
  1135. struct ospf_neighbor *nbr;
  1136. struct route_node *rn;
  1137. struct prefix p;
  1138. if (oi->type != OSPF_IFTYPE_NBMA)
  1139. return;
  1140. if (nbr_nbma->nbr != NULL)
  1141. return;
  1142. if (IPV4_ADDR_SAME (&oi->nbr_self->address.u.prefix4, &nbr_nbma->addr))
  1143. return;
  1144. nbr_nbma->oi = oi;
  1145. listnode_add (oi->nbr_nbma, nbr_nbma);
  1146. /* Get neighbor information from table. */
  1147. p.family = AF_INET;
  1148. p.prefixlen = IPV4_MAX_BITLEN;
  1149. p.u.prefix4 = nbr_nbma->addr;
  1150. rn = route_node_get (oi->nbrs, (struct prefix *)&p);
  1151. if (rn->info)
  1152. {
  1153. nbr = rn->info;
  1154. nbr->nbr_nbma = nbr_nbma;
  1155. nbr_nbma->nbr = nbr;
  1156. route_unlock_node (rn);
  1157. }
  1158. else
  1159. {
  1160. nbr = rn->info = ospf_nbr_new (oi);
  1161. nbr->state = NSM_Down;
  1162. nbr->src = nbr_nbma->addr;
  1163. nbr->nbr_nbma = nbr_nbma;
  1164. nbr->priority = nbr_nbma->priority;
  1165. nbr->address = p;
  1166. nbr_nbma->nbr = nbr;
  1167. OSPF_NSM_EVENT_EXECUTE (nbr, NSM_Start);
  1168. }
  1169. }
  1170. void
  1171. ospf_nbr_nbma_if_update (struct ospf *ospf, struct ospf_interface *oi)
  1172. {
  1173. struct ospf_nbr_nbma *nbr_nbma;
  1174. struct route_node *rn;
  1175. struct prefix_ipv4 p;
  1176. if (oi->type != OSPF_IFTYPE_NBMA)
  1177. return;
  1178. for (rn = route_top (ospf->nbr_nbma); rn; rn = route_next (rn))
  1179. if ((nbr_nbma = rn->info))
  1180. if (nbr_nbma->oi == NULL && nbr_nbma->nbr == NULL)
  1181. {
  1182. p.family = AF_INET;
  1183. p.prefix = nbr_nbma->addr;
  1184. p.prefixlen = IPV4_MAX_BITLEN;
  1185. if (prefix_match (oi->address, (struct prefix *)&p))
  1186. ospf_nbr_nbma_add (nbr_nbma, oi);
  1187. }
  1188. }
  1189. struct ospf_nbr_nbma *
  1190. ospf_nbr_nbma_lookup (struct ospf *ospf, struct in_addr nbr_addr)
  1191. {
  1192. struct route_node *rn;
  1193. struct prefix_ipv4 p;
  1194. p.family = AF_INET;
  1195. p.prefix = nbr_addr;
  1196. p.prefixlen = IPV4_MAX_BITLEN;
  1197. rn = route_node_lookup (ospf->nbr_nbma, (struct prefix *)&p);
  1198. if (rn)
  1199. {
  1200. route_unlock_node (rn);
  1201. return rn->info;
  1202. }
  1203. return NULL;
  1204. }
  1205. struct ospf_nbr_nbma *
  1206. ospf_nbr_nbma_lookup_next (struct ospf *ospf, struct in_addr *addr, int first)
  1207. {
  1208. #if 0
  1209. struct ospf_nbr_nbma *nbr_nbma;
  1210. struct listnode *node;
  1211. #endif
  1212. if (ospf == NULL)
  1213. return NULL;
  1214. #if 0
  1215. for (ALL_LIST_ELEMENTS_RO (ospf->nbr_nbma, node, nbr_nbma))
  1216. {
  1217. if (first)
  1218. {
  1219. *addr = nbr_nbma->addr;
  1220. return nbr_nbma;
  1221. }
  1222. else if (ntohl (nbr_nbma->addr.s_addr) > ntohl (addr->s_addr))
  1223. {
  1224. *addr = nbr_nbma->addr;
  1225. return nbr_nbma;
  1226. }
  1227. }
  1228. #endif
  1229. return NULL;
  1230. }
  1231. int
  1232. ospf_nbr_nbma_set (struct ospf *ospf, struct in_addr nbr_addr)
  1233. {
  1234. struct ospf_nbr_nbma *nbr_nbma;
  1235. struct ospf_interface *oi;
  1236. struct prefix_ipv4 p;
  1237. struct route_node *rn;
  1238. struct listnode *node;
  1239. nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
  1240. if (nbr_nbma)
  1241. return 0;
  1242. nbr_nbma = ospf_nbr_nbma_new ();
  1243. nbr_nbma->addr = nbr_addr;
  1244. p.family = AF_INET;
  1245. p.prefix = nbr_addr;
  1246. p.prefixlen = IPV4_MAX_BITLEN;
  1247. rn = route_node_get (ospf->nbr_nbma, (struct prefix *)&p);
  1248. rn->info = nbr_nbma;
  1249. for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
  1250. {
  1251. if (oi->type == OSPF_IFTYPE_NBMA)
  1252. if (prefix_match (oi->address, (struct prefix *)&p))
  1253. {
  1254. ospf_nbr_nbma_add (nbr_nbma, oi);
  1255. break;
  1256. }
  1257. }
  1258. return 1;
  1259. }
  1260. int
  1261. ospf_nbr_nbma_unset (struct ospf *ospf, struct in_addr nbr_addr)
  1262. {
  1263. struct ospf_nbr_nbma *nbr_nbma;
  1264. nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
  1265. if (nbr_nbma == NULL)
  1266. return 0;
  1267. ospf_nbr_nbma_down (nbr_nbma);
  1268. ospf_nbr_nbma_delete (ospf, nbr_nbma);
  1269. return 1;
  1270. }
  1271. int
  1272. ospf_nbr_nbma_priority_set (struct ospf *ospf, struct in_addr nbr_addr,
  1273. u_char priority)
  1274. {
  1275. struct ospf_nbr_nbma *nbr_nbma;
  1276. nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
  1277. if (nbr_nbma == NULL)
  1278. return 0;
  1279. if (nbr_nbma->priority != priority)
  1280. nbr_nbma->priority = priority;
  1281. return 1;
  1282. }
  1283. int
  1284. ospf_nbr_nbma_priority_unset (struct ospf *ospf, struct in_addr nbr_addr)
  1285. {
  1286. struct ospf_nbr_nbma *nbr_nbma;
  1287. nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
  1288. if (nbr_nbma == NULL)
  1289. return 0;
  1290. if (nbr_nbma != OSPF_NEIGHBOR_PRIORITY_DEFAULT)
  1291. nbr_nbma->priority = OSPF_NEIGHBOR_PRIORITY_DEFAULT;
  1292. return 1;
  1293. }
  1294. int
  1295. ospf_nbr_nbma_poll_interval_set (struct ospf *ospf, struct in_addr nbr_addr,
  1296. unsigned int interval)
  1297. {
  1298. struct ospf_nbr_nbma *nbr_nbma;
  1299. nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
  1300. if (nbr_nbma == NULL)
  1301. return 0;
  1302. if (nbr_nbma->v_poll != interval)
  1303. {
  1304. nbr_nbma->v_poll = interval;
  1305. if (nbr_nbma->oi && ospf_if_is_up (nbr_nbma->oi))
  1306. {
  1307. OSPF_TIMER_OFF (nbr_nbma->t_poll);
  1308. OSPF_POLL_TIMER_ON (nbr_nbma->t_poll, ospf_poll_timer,
  1309. nbr_nbma->v_poll);
  1310. }
  1311. }
  1312. return 1;
  1313. }
  1314. int
  1315. ospf_nbr_nbma_poll_interval_unset (struct ospf *ospf, struct in_addr addr)
  1316. {
  1317. struct ospf_nbr_nbma *nbr_nbma;
  1318. nbr_nbma = ospf_nbr_nbma_lookup (ospf, addr);
  1319. if (nbr_nbma == NULL)
  1320. return 0;
  1321. if (nbr_nbma->v_poll != OSPF_POLL_INTERVAL_DEFAULT)
  1322. nbr_nbma->v_poll = OSPF_POLL_INTERVAL_DEFAULT;
  1323. return 1;
  1324. }
  1325. void
  1326. ospf_master_init ()
  1327. {
  1328. memset (&ospf_master, 0, sizeof (struct ospf_master));
  1329. om = &ospf_master;
  1330. om->ospf = list_new ();
  1331. om->master = thread_master_create ();
  1332. om->start_time = quagga_time (NULL);
  1333. }