ospfd.c 41 KB

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