ospfd.c 40 KB

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