ospfd.c 44 KB

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