ospf_lsa.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777
  1. /*
  2. * OSPF Link State Advertisement
  3. * Copyright (C) 1999, 2000 Toshiaki Takada
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "linklist.h"
  24. #include "prefix.h"
  25. #include "if.h"
  26. #include "table.h"
  27. #include "memory.h"
  28. #include "stream.h"
  29. #include "log.h"
  30. #include "thread.h"
  31. #include "hash.h"
  32. #include "sockunion.h" /* for inet_aton() */
  33. #include "checksum.h"
  34. #include "ospfd/ospfd.h"
  35. #include "ospfd/ospf_interface.h"
  36. #include "ospfd/ospf_ism.h"
  37. #include "ospfd/ospf_asbr.h"
  38. #include "ospfd/ospf_lsa.h"
  39. #include "ospfd/ospf_lsdb.h"
  40. #include "ospfd/ospf_neighbor.h"
  41. #include "ospfd/ospf_nsm.h"
  42. #include "ospfd/ospf_flood.h"
  43. #include "ospfd/ospf_packet.h"
  44. #include "ospfd/ospf_spf.h"
  45. #include "ospfd/ospf_dump.h"
  46. #include "ospfd/ospf_route.h"
  47. #include "ospfd/ospf_ase.h"
  48. #include "ospfd/ospf_zebra.h"
  49. u_int32_t
  50. get_metric (u_char *metric)
  51. {
  52. u_int32_t m;
  53. m = metric[0];
  54. m = (m << 8) + metric[1];
  55. m = (m << 8) + metric[2];
  56. return m;
  57. }
  58. struct timeval
  59. tv_adjust (struct timeval a)
  60. {
  61. while (a.tv_usec >= 1000000)
  62. {
  63. a.tv_usec -= 1000000;
  64. a.tv_sec++;
  65. }
  66. while (a.tv_usec < 0)
  67. {
  68. a.tv_usec += 1000000;
  69. a.tv_sec--;
  70. }
  71. return a;
  72. }
  73. int
  74. tv_ceil (struct timeval a)
  75. {
  76. a = tv_adjust (a);
  77. return (a.tv_usec ? a.tv_sec + 1 : a.tv_sec);
  78. }
  79. int
  80. tv_floor (struct timeval a)
  81. {
  82. a = tv_adjust (a);
  83. return a.tv_sec;
  84. }
  85. struct timeval
  86. int2tv (int a)
  87. {
  88. struct timeval ret;
  89. ret.tv_sec = a;
  90. ret.tv_usec = 0;
  91. return ret;
  92. }
  93. struct timeval
  94. tv_add (struct timeval a, struct timeval b)
  95. {
  96. struct timeval ret;
  97. ret.tv_sec = a.tv_sec + b.tv_sec;
  98. ret.tv_usec = a.tv_usec + b.tv_usec;
  99. return tv_adjust (ret);
  100. }
  101. struct timeval
  102. tv_sub (struct timeval a, struct timeval b)
  103. {
  104. struct timeval ret;
  105. ret.tv_sec = a.tv_sec - b.tv_sec;
  106. ret.tv_usec = a.tv_usec - b.tv_usec;
  107. return tv_adjust (ret);
  108. }
  109. int
  110. tv_cmp (struct timeval a, struct timeval b)
  111. {
  112. return (a.tv_sec == b.tv_sec ?
  113. a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec);
  114. }
  115. int
  116. ospf_lsa_refresh_delay (struct ospf_lsa *lsa)
  117. {
  118. struct timeval delta, now;
  119. int delay = 0;
  120. quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
  121. delta = tv_sub (now, lsa->tv_orig);
  122. if (tv_cmp (delta, int2tv (OSPF_MIN_LS_INTERVAL)) < 0)
  123. {
  124. delay = tv_ceil (tv_sub (int2tv (OSPF_MIN_LS_INTERVAL), delta));
  125. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  126. zlog_debug ("LSA[Type%d:%s]: Refresh timer delay %d seconds",
  127. lsa->data->type, inet_ntoa (lsa->data->id), delay);
  128. assert (delay > 0);
  129. }
  130. return delay;
  131. }
  132. int
  133. get_age (struct ospf_lsa *lsa)
  134. {
  135. int age;
  136. age = ntohs (lsa->data->ls_age)
  137. + tv_floor (tv_sub (recent_relative_time (), lsa->tv_recv));
  138. return age;
  139. }
  140. /* Fletcher Checksum -- Refer to RFC1008. */
  141. /* All the offsets are zero-based. The offsets in the RFC1008 are
  142. one-based. */
  143. u_int16_t
  144. ospf_lsa_checksum (struct lsa_header *lsa)
  145. {
  146. u_char *buffer = (u_char *) &lsa->options;
  147. int options_offset = buffer - (u_char *) &lsa->ls_age; /* should be 2 */
  148. /* Skip the AGE field */
  149. u_int16_t len = ntohs(lsa->length) - options_offset;
  150. /* Checksum offset starts from "options" field, not the beginning of the
  151. lsa_header struct. The offset is 14, rather than 16. */
  152. int checksum_offset = (u_char *) &lsa->checksum - buffer;
  153. return fletcher_checksum(buffer, len, checksum_offset);
  154. }
  155. int
  156. ospf_lsa_checksum_valid (struct lsa_header *lsa)
  157. {
  158. u_char *buffer = (u_char *) &lsa->options;
  159. int options_offset = buffer - (u_char *) &lsa->ls_age; /* should be 2 */
  160. /* Skip the AGE field */
  161. u_int16_t len = ntohs(lsa->length) - options_offset;
  162. return(fletcher_checksum(buffer, len, FLETCHER_CHECKSUM_VALIDATE) == 0);
  163. }
  164. /* Create OSPF LSA. */
  165. struct ospf_lsa *
  166. ospf_lsa_new ()
  167. {
  168. struct ospf_lsa *new;
  169. new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
  170. new->flags = 0;
  171. new->lock = 1;
  172. new->retransmit_counter = 0;
  173. new->tv_recv = recent_relative_time ();
  174. new->tv_orig = new->tv_recv;
  175. new->refresh_list = -1;
  176. return new;
  177. }
  178. /* Duplicate OSPF LSA. */
  179. struct ospf_lsa *
  180. ospf_lsa_dup (struct ospf_lsa *lsa)
  181. {
  182. struct ospf_lsa *new;
  183. if (lsa == NULL)
  184. return NULL;
  185. new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
  186. memcpy (new, lsa, sizeof (struct ospf_lsa));
  187. UNSET_FLAG (new->flags, OSPF_LSA_DISCARD);
  188. new->lock = 1;
  189. new->retransmit_counter = 0;
  190. new->data = ospf_lsa_data_dup (lsa->data);
  191. /* kevinm: Clear the refresh_list, otherwise there are going
  192. to be problems when we try to remove the LSA from the
  193. queue (which it's not a member of.)
  194. XXX: Should we add the LSA to the refresh_list queue? */
  195. new->refresh_list = -1;
  196. if (IS_DEBUG_OSPF (lsa, LSA))
  197. zlog_debug ("LSA: duplicated %p (new: %p)", lsa, new);
  198. return new;
  199. }
  200. /* Free OSPF LSA. */
  201. void
  202. ospf_lsa_free (struct ospf_lsa *lsa)
  203. {
  204. assert (lsa->lock == 0);
  205. if (IS_DEBUG_OSPF (lsa, LSA))
  206. zlog_debug ("LSA: freed %p", lsa);
  207. /* Delete LSA data. */
  208. if (lsa->data != NULL)
  209. ospf_lsa_data_free (lsa->data);
  210. assert (lsa->refresh_list < 0);
  211. memset (lsa, 0, sizeof (struct ospf_lsa));
  212. XFREE (MTYPE_OSPF_LSA, lsa);
  213. }
  214. /* Lock LSA. */
  215. struct ospf_lsa *
  216. ospf_lsa_lock (struct ospf_lsa *lsa)
  217. {
  218. lsa->lock++;
  219. return lsa;
  220. }
  221. /* Unlock LSA. */
  222. void
  223. ospf_lsa_unlock (struct ospf_lsa **lsa)
  224. {
  225. /* This is sanity check. */
  226. if (!lsa || !*lsa)
  227. return;
  228. (*lsa)->lock--;
  229. assert ((*lsa)->lock >= 0);
  230. if ((*lsa)->lock == 0)
  231. {
  232. assert (CHECK_FLAG ((*lsa)->flags, OSPF_LSA_DISCARD));
  233. ospf_lsa_free (*lsa);
  234. *lsa = NULL;
  235. }
  236. }
  237. /* Check discard flag. */
  238. void
  239. ospf_lsa_discard (struct ospf_lsa *lsa)
  240. {
  241. if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD))
  242. {
  243. SET_FLAG (lsa->flags, OSPF_LSA_DISCARD);
  244. ospf_lsa_unlock (&lsa);
  245. }
  246. }
  247. /* Create LSA data. */
  248. struct lsa_header *
  249. ospf_lsa_data_new (size_t size)
  250. {
  251. return XCALLOC (MTYPE_OSPF_LSA_DATA, size);
  252. }
  253. /* Duplicate LSA data. */
  254. struct lsa_header *
  255. ospf_lsa_data_dup (struct lsa_header *lsah)
  256. {
  257. struct lsa_header *new;
  258. new = ospf_lsa_data_new (ntohs (lsah->length));
  259. memcpy (new, lsah, ntohs (lsah->length));
  260. return new;
  261. }
  262. /* Free LSA data. */
  263. void
  264. ospf_lsa_data_free (struct lsa_header *lsah)
  265. {
  266. if (IS_DEBUG_OSPF (lsa, LSA))
  267. zlog_debug ("LSA[Type%d:%s]: data freed %p",
  268. lsah->type, inet_ntoa (lsah->id), lsah);
  269. XFREE (MTYPE_OSPF_LSA_DATA, lsah);
  270. }
  271. /* LSA general functions. */
  272. const char *
  273. dump_lsa_key (struct ospf_lsa *lsa)
  274. {
  275. static char buf[] = {
  276. "Type255,id(255.255.255.255),ar(255.255.255.255)"
  277. };
  278. struct lsa_header *lsah;
  279. if (lsa != NULL && (lsah = lsa->data) != NULL)
  280. {
  281. char id[INET_ADDRSTRLEN], ar[INET_ADDRSTRLEN];
  282. strcpy (id, inet_ntoa (lsah->id));
  283. strcpy (ar, inet_ntoa (lsah->adv_router));
  284. sprintf (buf, "Type%d,id(%s),ar(%s)", lsah->type, id, ar);
  285. }
  286. else
  287. strcpy (buf, "NULL");
  288. return buf;
  289. }
  290. u_int32_t
  291. lsa_seqnum_increment (struct ospf_lsa *lsa)
  292. {
  293. u_int32_t seqnum;
  294. seqnum = ntohl (lsa->data->ls_seqnum) + 1;
  295. return htonl (seqnum);
  296. }
  297. void
  298. lsa_header_set (struct stream *s, u_char options,
  299. u_char type, struct in_addr id, struct in_addr router_id)
  300. {
  301. struct lsa_header *lsah;
  302. lsah = (struct lsa_header *) STREAM_DATA (s);
  303. lsah->ls_age = htons (OSPF_LSA_INITIAL_AGE);
  304. lsah->options = options;
  305. lsah->type = type;
  306. lsah->id = id;
  307. lsah->adv_router = router_id;
  308. lsah->ls_seqnum = htonl (OSPF_INITIAL_SEQUENCE_NUMBER);
  309. stream_forward_endp (s, OSPF_LSA_HEADER_SIZE);
  310. }
  311. /* router-LSA related functions. */
  312. /* Get router-LSA flags. */
  313. static u_char
  314. router_lsa_flags (struct ospf_area *area)
  315. {
  316. u_char flags;
  317. flags = area->ospf->flags;
  318. /* Set virtual link flag. */
  319. if (ospf_full_virtual_nbrs (area))
  320. SET_FLAG (flags, ROUTER_LSA_VIRTUAL);
  321. else
  322. /* Just sanity check */
  323. UNSET_FLAG (flags, ROUTER_LSA_VIRTUAL);
  324. /* Set Shortcut ABR behabiour flag. */
  325. UNSET_FLAG (flags, ROUTER_LSA_SHORTCUT);
  326. if (area->ospf->abr_type == OSPF_ABR_SHORTCUT)
  327. if (!OSPF_IS_AREA_BACKBONE (area))
  328. if ((area->shortcut_configured == OSPF_SHORTCUT_DEFAULT &&
  329. area->ospf->backbone == NULL) ||
  330. area->shortcut_configured == OSPF_SHORTCUT_ENABLE)
  331. SET_FLAG (flags, ROUTER_LSA_SHORTCUT);
  332. /* ASBR can't exit in stub area. */
  333. if (area->external_routing == OSPF_AREA_STUB)
  334. UNSET_FLAG (flags, ROUTER_LSA_EXTERNAL);
  335. /* If ASBR set External flag */
  336. else if (IS_OSPF_ASBR (area->ospf))
  337. SET_FLAG (flags, ROUTER_LSA_EXTERNAL);
  338. /* Set ABR dependent flags */
  339. if (IS_OSPF_ABR (area->ospf))
  340. {
  341. SET_FLAG (flags, ROUTER_LSA_BORDER);
  342. /* If Area is NSSA and we are both ABR and unconditional translator,
  343. * set Nt bit to inform other routers.
  344. */
  345. if ( (area->external_routing == OSPF_AREA_NSSA)
  346. && (area->NSSATranslatorRole == OSPF_NSSA_ROLE_ALWAYS))
  347. SET_FLAG (flags, ROUTER_LSA_NT);
  348. }
  349. return flags;
  350. }
  351. /* Lookup neighbor other than myself.
  352. And check neighbor count,
  353. Point-to-Point link must have only 1 neighbor. */
  354. struct ospf_neighbor *
  355. ospf_nbr_lookup_ptop (struct ospf_interface *oi)
  356. {
  357. struct ospf_neighbor *nbr = NULL;
  358. struct route_node *rn;
  359. /* Search neighbor, there must be one of two nbrs. */
  360. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  361. if ((nbr = rn->info))
  362. if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
  363. if (nbr->state == NSM_Full)
  364. {
  365. route_unlock_node (rn);
  366. break;
  367. }
  368. /* PtoP link must have only 1 neighbor. */
  369. if (ospf_nbr_count (oi, 0) > 1)
  370. zlog_warn ("Point-to-Point link has more than 1 neighobrs.");
  371. return nbr;
  372. }
  373. /* Determine cost of link, taking RFC3137 stub-router support into
  374. * consideration
  375. */
  376. static u_int16_t
  377. ospf_link_cost (struct ospf_interface *oi)
  378. {
  379. /* RFC3137 stub router support */
  380. if (!CHECK_FLAG (oi->area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED))
  381. return oi->output_cost;
  382. else
  383. return OSPF_OUTPUT_COST_INFINITE;
  384. }
  385. /* Set a link information. */
  386. static char
  387. link_info_set (struct stream *s, struct in_addr id,
  388. struct in_addr data, u_char type, u_char tos, u_int16_t cost)
  389. {
  390. /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
  391. * vast majority of cases. Some rare routers with lots of links need more.
  392. * we try accomodate those here.
  393. */
  394. if (STREAM_WRITEABLE(s) < OSPF_ROUTER_LSA_LINK_SIZE)
  395. {
  396. size_t ret = OSPF_MAX_LSA_SIZE;
  397. /* Can we enlarge the stream still? */
  398. if (STREAM_SIZE(s) == OSPF_MAX_LSA_SIZE)
  399. {
  400. /* we futz the size here for simplicity, really we need to account
  401. * for just:
  402. * IP Header - (sizeof (struct ip))
  403. * OSPF Header - OSPF_HEADER_SIZE
  404. * LSA Header - OSPF_LSA_HEADER_SIZE
  405. * MD5 auth data, if MD5 is configured - OSPF_AUTH_MD5_SIZE.
  406. *
  407. * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
  408. */
  409. ret = stream_resize (s, OSPF_MAX_PACKET_SIZE - OSPF_MAX_LSA_SIZE);
  410. }
  411. if (ret == OSPF_MAX_LSA_SIZE)
  412. {
  413. zlog_warn ("%s: Out of space in LSA stream, left %zd, size %zd",
  414. __func__, STREAM_REMAIN (s), STREAM_SIZE (s));
  415. return 0;
  416. }
  417. }
  418. /* TOS based routing is not supported. */
  419. stream_put_ipv4 (s, id.s_addr); /* Link ID. */
  420. stream_put_ipv4 (s, data.s_addr); /* Link Data. */
  421. stream_putc (s, type); /* Link Type. */
  422. stream_putc (s, tos); /* TOS = 0. */
  423. stream_putw (s, cost); /* Link Cost. */
  424. return 1;
  425. }
  426. /* Describe Point-to-Point link (Section 12.4.1.1). */
  427. static int
  428. lsa_link_ptop_set (struct stream *s, struct ospf_interface *oi)
  429. {
  430. int links = 0;
  431. struct ospf_neighbor *nbr;
  432. struct in_addr id, mask;
  433. u_int16_t cost = ospf_link_cost (oi);
  434. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  435. zlog_debug ("LSA[Type1]: Set link Point-to-Point");
  436. if ((nbr = ospf_nbr_lookup_ptop (oi)))
  437. if (nbr->state == NSM_Full)
  438. {
  439. /* For unnumbered point-to-point networks, the Link Data field
  440. should specify the interface's MIB-II ifIndex value. */
  441. links += link_info_set (s, nbr->router_id, oi->address->u.prefix4,
  442. LSA_LINK_TYPE_POINTOPOINT, 0, cost);
  443. }
  444. /* Regardless of the state of the neighboring router, we must
  445. add a Type 3 link (stub network).
  446. N.B. Options 1 & 2 share basically the same logic. */
  447. masklen2ip (oi->address->prefixlen, &mask);
  448. id.s_addr = CONNECTED_PREFIX(oi->connected)->u.prefix4.s_addr & mask.s_addr;
  449. links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
  450. oi->output_cost);
  451. return links;
  452. }
  453. /* Describe Broadcast Link. */
  454. static int
  455. lsa_link_broadcast_set (struct stream *s, struct ospf_interface *oi)
  456. {
  457. struct ospf_neighbor *dr;
  458. struct in_addr id, mask;
  459. u_int16_t cost = ospf_link_cost (oi);
  460. /* Describe Type 3 Link. */
  461. if (oi->state == ISM_Waiting)
  462. {
  463. masklen2ip (oi->address->prefixlen, &mask);
  464. id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
  465. return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
  466. oi->output_cost);
  467. }
  468. dr = ospf_nbr_lookup_by_addr (oi->nbrs, &DR (oi));
  469. /* Describe Type 2 link. */
  470. if (dr && (dr->state == NSM_Full ||
  471. IPV4_ADDR_SAME (&oi->address->u.prefix4, &DR (oi))) &&
  472. ospf_nbr_count (oi, NSM_Full) > 0)
  473. {
  474. return link_info_set (s, DR (oi), oi->address->u.prefix4,
  475. LSA_LINK_TYPE_TRANSIT, 0, cost);
  476. }
  477. /* Describe type 3 link. */
  478. else
  479. {
  480. masklen2ip (oi->address->prefixlen, &mask);
  481. id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
  482. return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
  483. oi->output_cost);
  484. }
  485. }
  486. static int
  487. lsa_link_loopback_set (struct stream *s, struct ospf_interface *oi)
  488. {
  489. struct in_addr id, mask;
  490. /* Describe Type 3 Link. */
  491. if (oi->state != ISM_Loopback)
  492. return 0;
  493. mask.s_addr = 0xffffffff;
  494. id.s_addr = oi->address->u.prefix4.s_addr;
  495. return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost);
  496. }
  497. /* Describe Virtual Link. */
  498. static int
  499. lsa_link_virtuallink_set (struct stream *s, struct ospf_interface *oi)
  500. {
  501. struct ospf_neighbor *nbr;
  502. u_int16_t cost = ospf_link_cost (oi);
  503. if (oi->state == ISM_PointToPoint)
  504. if ((nbr = ospf_nbr_lookup_ptop (oi)))
  505. if (nbr->state == NSM_Full)
  506. {
  507. return link_info_set (s, nbr->router_id, oi->address->u.prefix4,
  508. LSA_LINK_TYPE_VIRTUALLINK, 0, cost);
  509. }
  510. return 0;
  511. }
  512. #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
  513. /* this function add for support point-to-multipoint ,see rfc2328
  514. 12.4.1.4.*/
  515. /* from "edward rrr" <edward_rrr@hotmail.com>
  516. http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
  517. static int
  518. lsa_link_ptomp_set (struct stream *s, struct ospf_interface *oi)
  519. {
  520. int links = 0;
  521. struct route_node *rn;
  522. struct ospf_neighbor *nbr = NULL;
  523. struct in_addr id, mask;
  524. u_int16_t cost = ospf_link_cost (oi);
  525. mask.s_addr = 0xffffffff;
  526. id.s_addr = oi->address->u.prefix4.s_addr;
  527. links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
  528. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  529. zlog_debug ("PointToMultipoint: running ptomultip_set");
  530. /* Search neighbor, */
  531. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  532. if ((nbr = rn->info) != NULL)
  533. /* Ignore myself. */
  534. if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
  535. if (nbr->state == NSM_Full)
  536. {
  537. links += link_info_set (s, nbr->router_id, oi->address->u.prefix4,
  538. LSA_LINK_TYPE_POINTOPOINT, 0, cost);
  539. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  540. zlog_debug ("PointToMultipoint: set link to %s",
  541. inet_ntoa(oi->address->u.prefix4));
  542. }
  543. return links;
  544. }
  545. /* Set router-LSA link information. */
  546. static int
  547. router_lsa_link_set (struct stream *s, struct ospf_area *area)
  548. {
  549. struct listnode *node;
  550. struct ospf_interface *oi;
  551. int links = 0;
  552. for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
  553. {
  554. struct interface *ifp = oi->ifp;
  555. /* Check interface is up, OSPF is enable. */
  556. if (if_is_operative (ifp))
  557. {
  558. if (oi->state != ISM_Down)
  559. {
  560. oi->lsa_pos_beg = links;
  561. /* Describe each link. */
  562. switch (oi->type)
  563. {
  564. case OSPF_IFTYPE_POINTOPOINT:
  565. links += lsa_link_ptop_set (s, oi);
  566. break;
  567. case OSPF_IFTYPE_BROADCAST:
  568. links += lsa_link_broadcast_set (s, oi);
  569. break;
  570. case OSPF_IFTYPE_NBMA:
  571. links += lsa_link_nbma_set (s, oi);
  572. break;
  573. case OSPF_IFTYPE_POINTOMULTIPOINT:
  574. links += lsa_link_ptomp_set (s, oi);
  575. break;
  576. case OSPF_IFTYPE_VIRTUALLINK:
  577. links += lsa_link_virtuallink_set (s, oi);
  578. break;
  579. case OSPF_IFTYPE_LOOPBACK:
  580. links += lsa_link_loopback_set (s, oi);
  581. }
  582. oi->lsa_pos_end = links;
  583. }
  584. }
  585. }
  586. return links;
  587. }
  588. /* Set router-LSA body. */
  589. static void
  590. ospf_router_lsa_body_set (struct stream *s, struct ospf_area *area)
  591. {
  592. unsigned long putp;
  593. u_int16_t cnt;
  594. /* Set flags. */
  595. stream_putc (s, router_lsa_flags (area));
  596. /* Set Zero fields. */
  597. stream_putc (s, 0);
  598. /* Keep pointer to # links. */
  599. putp = stream_get_endp(s);
  600. /* Forward word */
  601. stream_putw(s, 0);
  602. /* Set all link information. */
  603. cnt = router_lsa_link_set (s, area);
  604. /* Set # of links here. */
  605. stream_putw_at (s, putp, cnt);
  606. }
  607. static int
  608. ospf_stub_router_timer (struct thread *t)
  609. {
  610. struct ospf_area *area = THREAD_ARG (t);
  611. area->t_stub_router = NULL;
  612. SET_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
  613. /* clear stub route state and generate router-lsa refresh, don't
  614. * clobber an administratively set stub-router state though.
  615. */
  616. if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
  617. return 0;
  618. UNSET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
  619. ospf_router_lsa_update_area (area);
  620. return 0;
  621. }
  622. static void
  623. ospf_stub_router_check (struct ospf_area *area)
  624. {
  625. /* area must either be administratively configured to be stub
  626. * or startup-time stub-router must be configured and we must in a pre-stub
  627. * state.
  628. */
  629. if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
  630. {
  631. SET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
  632. return;
  633. }
  634. /* not admin-stubbed, check whether startup stubbing is configured and
  635. * whether it's not been done yet
  636. */
  637. if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED))
  638. return;
  639. if (area->ospf->stub_router_startup_time == OSPF_STUB_ROUTER_UNCONFIGURED)
  640. {
  641. /* stub-router is hence done forever for this area, even if someone
  642. * tries configure it (take effect next restart).
  643. */
  644. SET_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
  645. return;
  646. }
  647. /* startup stub-router configured and not yet done */
  648. SET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
  649. OSPF_AREA_TIMER_ON (area->t_stub_router, ospf_stub_router_timer,
  650. area->ospf->stub_router_startup_time);
  651. }
  652. /* Create new router-LSA. */
  653. static struct ospf_lsa *
  654. ospf_router_lsa_new (struct ospf_area *area)
  655. {
  656. struct ospf *ospf = area->ospf;
  657. struct stream *s;
  658. struct lsa_header *lsah;
  659. struct ospf_lsa *new;
  660. int length;
  661. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  662. zlog_debug ("LSA[Type1]: Create router-LSA instance");
  663. /* check whether stub-router is desired, and if this is the first
  664. * router LSA.
  665. */
  666. ospf_stub_router_check (area);
  667. /* Create a stream for LSA. */
  668. s = stream_new (OSPF_MAX_LSA_SIZE);
  669. /* Set LSA common header fields. */
  670. lsa_header_set (s, LSA_OPTIONS_GET (area) | LSA_OPTIONS_NSSA_GET (area),
  671. OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);
  672. /* Set router-LSA body fields. */
  673. ospf_router_lsa_body_set (s, area);
  674. /* Set length. */
  675. length = stream_get_endp (s);
  676. lsah = (struct lsa_header *) STREAM_DATA (s);
  677. lsah->length = htons (length);
  678. /* Now, create OSPF LSA instance. */
  679. if ( (new = ospf_lsa_new ()) == NULL)
  680. {
  681. zlog_err ("%s: Unable to create new lsa", __func__);
  682. return NULL;
  683. }
  684. new->area = area;
  685. SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
  686. /* Copy LSA data to store, discard stream. */
  687. new->data = ospf_lsa_data_new (length);
  688. memcpy (new->data, lsah, length);
  689. stream_free (s);
  690. return new;
  691. }
  692. /* Originate Router-LSA. */
  693. static struct ospf_lsa *
  694. ospf_router_lsa_originate (struct ospf_area *area)
  695. {
  696. struct ospf_lsa *new;
  697. /* Create new router-LSA instance. */
  698. if ( (new = ospf_router_lsa_new (area)) == NULL)
  699. {
  700. zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__);
  701. return NULL;
  702. }
  703. /* Sanity check. */
  704. if (new->data->adv_router.s_addr == 0)
  705. {
  706. if (IS_DEBUG_OSPF_EVENT)
  707. zlog_debug ("LSA[Type1]: AdvRouter is 0, discard");
  708. ospf_lsa_discard (new);
  709. return NULL;
  710. }
  711. /* Install LSA to LSDB. */
  712. new = ospf_lsa_install (area->ospf, NULL, new);
  713. /* Update LSA origination count. */
  714. area->ospf->lsa_originate_count++;
  715. /* Flooding new LSA through area. */
  716. ospf_flood_through_area (area, NULL, new);
  717. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  718. {
  719. zlog_debug ("LSA[Type%d:%s]: Originate router-LSA %p",
  720. new->data->type, inet_ntoa (new->data->id), new);
  721. ospf_lsa_header_dump (new->data);
  722. }
  723. return new;
  724. }
  725. /* Refresh router-LSA. */
  726. static struct ospf_lsa *
  727. ospf_router_lsa_refresh (struct ospf_lsa *lsa)
  728. {
  729. struct ospf_area *area = lsa->area;
  730. struct ospf_lsa *new;
  731. /* Sanity check. */
  732. assert (lsa->data);
  733. /* Delete LSA from neighbor retransmit-list. */
  734. ospf_ls_retransmit_delete_nbr_area (area, lsa);
  735. /* Unregister LSA from refresh-list */
  736. ospf_refresher_unregister_lsa (area->ospf, lsa);
  737. /* Create new router-LSA instance. */
  738. if ( (new = ospf_router_lsa_new (area)) == NULL)
  739. {
  740. zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__);
  741. return NULL;
  742. }
  743. new->data->ls_seqnum = lsa_seqnum_increment (lsa);
  744. ospf_lsa_install (area->ospf, NULL, new);
  745. /* Flood LSA through area. */
  746. ospf_flood_through_area (area, NULL, new);
  747. /* Debug logging. */
  748. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  749. {
  750. zlog_debug ("LSA[Type%d:%s]: router-LSA refresh",
  751. new->data->type, inet_ntoa (new->data->id));
  752. ospf_lsa_header_dump (new->data);
  753. }
  754. return NULL;
  755. }
  756. int
  757. ospf_router_lsa_update_area (struct ospf_area *area)
  758. {
  759. if (IS_DEBUG_OSPF_EVENT)
  760. zlog_debug ("[router-LSA]: (router-LSA area update)");
  761. /* Now refresh router-LSA. */
  762. if (area->router_lsa_self)
  763. ospf_lsa_refresh (area->ospf, area->router_lsa_self);
  764. /* Newly originate router-LSA. */
  765. else
  766. ospf_router_lsa_originate (area);
  767. return 0;
  768. }
  769. int
  770. ospf_router_lsa_update (struct ospf *ospf)
  771. {
  772. struct listnode *node, *nnode;
  773. struct ospf_area *area;
  774. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  775. zlog_debug ("Timer[router-LSA Update]: (timer expire)");
  776. for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
  777. {
  778. struct ospf_lsa *lsa = area->router_lsa_self;
  779. struct router_lsa *rl;
  780. const char *area_str;
  781. /* Keep Area ID string. */
  782. area_str = AREA_NAME (area);
  783. /* If LSA not exist in this Area, originate new. */
  784. if (lsa == NULL)
  785. {
  786. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  787. zlog_debug("LSA[Type1]: Create router-LSA for Area %s", area_str);
  788. ospf_router_lsa_originate (area);
  789. }
  790. /* If router-ID is changed, Link ID must change.
  791. First flush old LSA, then originate new. */
  792. else if (!IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
  793. {
  794. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  795. zlog_debug("LSA[Type%d:%s]: Refresh router-LSA for Area %s",
  796. lsa->data->type, inet_ntoa (lsa->data->id), area_str);
  797. ospf_refresher_unregister_lsa (ospf, lsa);
  798. ospf_lsa_flush_area (lsa, area);
  799. ospf_lsa_unlock (&area->router_lsa_self);
  800. area->router_lsa_self = NULL;
  801. /* Refresh router-LSA, (not install) and flood through area. */
  802. ospf_router_lsa_update_area (area);
  803. }
  804. else
  805. {
  806. rl = (struct router_lsa *) lsa->data;
  807. /* Refresh router-LSA, (not install) and flood through area. */
  808. if (rl->flags != ospf->flags)
  809. ospf_router_lsa_update_area (area);
  810. }
  811. }
  812. return 0;
  813. }
  814. /* network-LSA related functions. */
  815. /* Originate Network-LSA. */
  816. static void
  817. ospf_network_lsa_body_set (struct stream *s, struct ospf_interface *oi)
  818. {
  819. struct in_addr mask;
  820. struct route_node *rn;
  821. struct ospf_neighbor *nbr;
  822. masklen2ip (oi->address->prefixlen, &mask);
  823. stream_put_ipv4 (s, mask.s_addr);
  824. /* The network-LSA lists those routers that are fully adjacent to
  825. the Designated Router; each fully adjacent router is identified by
  826. its OSPF Router ID. The Designated Router includes itself in this
  827. list. RFC2328, Section 12.4.2 */
  828. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  829. if ((nbr = rn->info) != NULL)
  830. if (nbr->state == NSM_Full || nbr == oi->nbr_self)
  831. stream_put_ipv4 (s, nbr->router_id.s_addr);
  832. }
  833. static struct ospf_lsa *
  834. ospf_network_lsa_new (struct ospf_interface *oi)
  835. {
  836. struct stream *s;
  837. struct ospf_lsa *new;
  838. struct lsa_header *lsah;
  839. struct ospf_if_params *oip;
  840. int length;
  841. /* If there are no neighbours on this network (the net is stub),
  842. the router does not originate network-LSA (see RFC 12.4.2) */
  843. if (oi->full_nbrs == 0)
  844. return NULL;
  845. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  846. zlog_debug ("LSA[Type2]: Create network-LSA instance");
  847. /* Create new stream for LSA. */
  848. s = stream_new (OSPF_MAX_LSA_SIZE);
  849. lsah = (struct lsa_header *) STREAM_DATA (s);
  850. lsa_header_set (s, (OPTIONS (oi) | LSA_OPTIONS_GET (oi->area)),
  851. OSPF_NETWORK_LSA, DR (oi), oi->ospf->router_id);
  852. /* Set network-LSA body fields. */
  853. ospf_network_lsa_body_set (s, oi);
  854. /* Set length. */
  855. length = stream_get_endp (s);
  856. lsah->length = htons (length);
  857. /* Create OSPF LSA instance. */
  858. if ( (new = ospf_lsa_new ()) == NULL)
  859. {
  860. zlog_err ("%s: ospf_lsa_new returned NULL", __func__);
  861. return NULL;
  862. }
  863. new->area = oi->area;
  864. SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
  865. /* Copy LSA to store. */
  866. new->data = ospf_lsa_data_new (length);
  867. memcpy (new->data, lsah, length);
  868. stream_free (s);
  869. /* Remember prior network LSA sequence numbers, even if we stop
  870. * originating one for this oi, to try avoid re-originating LSAs with a
  871. * prior sequence number, and thus speed up adjency forming & convergence.
  872. */
  873. if ((oip = ospf_lookup_if_params (oi->ifp, oi->address->u.prefix4)))
  874. {
  875. new->data->ls_seqnum = oip->network_lsa_seqnum;
  876. new->data->ls_seqnum = lsa_seqnum_increment (new);
  877. }
  878. else
  879. {
  880. oip = ospf_get_if_params (oi->ifp, oi->address->u.prefix4);
  881. ospf_if_update_params (oi->ifp, oi->address->u.prefix4);
  882. }
  883. oip->network_lsa_seqnum = new->data->ls_seqnum;
  884. return new;
  885. }
  886. /* Originate network-LSA. */
  887. void
  888. ospf_network_lsa_update (struct ospf_interface *oi)
  889. {
  890. struct ospf_lsa *new;
  891. if (oi->network_lsa_self != NULL)
  892. {
  893. ospf_lsa_refresh (oi->ospf, oi->network_lsa_self);
  894. return;
  895. }
  896. /* Create new network-LSA instance. */
  897. new = ospf_network_lsa_new (oi);
  898. if (new == NULL)
  899. return;
  900. /* Install LSA to LSDB. */
  901. new = ospf_lsa_install (oi->ospf, oi, new);
  902. /* Update LSA origination count. */
  903. oi->ospf->lsa_originate_count++;
  904. /* Flooding new LSA through area. */
  905. ospf_flood_through_area (oi->area, NULL, new);
  906. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  907. {
  908. zlog_debug ("LSA[Type%d:%s]: Originate network-LSA %p",
  909. new->data->type, inet_ntoa (new->data->id), new);
  910. ospf_lsa_header_dump (new->data);
  911. }
  912. return;
  913. }
  914. static struct ospf_lsa *
  915. ospf_network_lsa_refresh (struct ospf_lsa *lsa)
  916. {
  917. struct ospf_area *area = lsa->area;
  918. struct ospf_lsa *new, *new2;
  919. struct ospf_if_params *oip;
  920. struct ospf_interface *oi;
  921. assert (lsa->data);
  922. /* Retrieve the oi for the network LSA */
  923. oi = ospf_if_lookup_by_local_addr (area->ospf, NULL, lsa->data->id);
  924. if (oi == NULL)
  925. {
  926. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  927. {
  928. zlog_debug ("LSA[Type%d:%s]: network-LSA refresh: "
  929. "no oi found, ick, ignoring.",
  930. lsa->data->type, inet_ntoa (lsa->data->id));
  931. ospf_lsa_header_dump (lsa->data);
  932. }
  933. return NULL;
  934. }
  935. /* Delete LSA from neighbor retransmit-list. */
  936. ospf_ls_retransmit_delete_nbr_area (area, lsa);
  937. /* Unregister LSA from refresh-list */
  938. ospf_refresher_unregister_lsa (area->ospf, lsa);
  939. /* Create new network-LSA instance. */
  940. new = ospf_network_lsa_new (oi);
  941. if (new == NULL)
  942. return NULL;
  943. oip = ospf_lookup_if_params (oi->ifp, oi->address->u.prefix4);
  944. assert (oip != NULL);
  945. oip->network_lsa_seqnum = new->data->ls_seqnum = lsa_seqnum_increment (lsa);
  946. new2 = ospf_lsa_install (area->ospf, oi, new);
  947. assert (new2 == new);
  948. /* Flood LSA through aera. */
  949. ospf_flood_through_area (area, NULL, new);
  950. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  951. {
  952. zlog_debug ("LSA[Type%d:%s]: network-LSA refresh",
  953. new->data->type, inet_ntoa (new->data->id));
  954. ospf_lsa_header_dump (new->data);
  955. }
  956. return new;
  957. }
  958. static void
  959. stream_put_ospf_metric (struct stream *s, u_int32_t metric_value)
  960. {
  961. u_int32_t metric;
  962. char *mp;
  963. /* Put 0 metric. TOS metric is not supported. */
  964. metric = htonl (metric_value);
  965. mp = (char *) &metric;
  966. mp++;
  967. stream_put (s, mp, 3);
  968. }
  969. /* summary-LSA related functions. */
  970. static void
  971. ospf_summary_lsa_body_set (struct stream *s, struct prefix *p,
  972. u_int32_t metric)
  973. {
  974. struct in_addr mask;
  975. masklen2ip (p->prefixlen, &mask);
  976. /* Put Network Mask. */
  977. stream_put_ipv4 (s, mask.s_addr);
  978. /* Set # TOS. */
  979. stream_putc (s, (u_char) 0);
  980. /* Set metric. */
  981. stream_put_ospf_metric (s, metric);
  982. }
  983. static struct ospf_lsa *
  984. ospf_summary_lsa_new (struct ospf_area *area, struct prefix *p,
  985. u_int32_t metric, struct in_addr id)
  986. {
  987. struct stream *s;
  988. struct ospf_lsa *new;
  989. struct lsa_header *lsah;
  990. int length;
  991. if (id.s_addr == 0xffffffff)
  992. {
  993. /* Maybe Link State ID not available. */
  994. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  995. zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
  996. OSPF_SUMMARY_LSA);
  997. return NULL;
  998. }
  999. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1000. zlog_debug ("LSA[Type3]: Create summary-LSA instance");
  1001. /* Create new stream for LSA. */
  1002. s = stream_new (OSPF_MAX_LSA_SIZE);
  1003. lsah = (struct lsa_header *) STREAM_DATA (s);
  1004. lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_SUMMARY_LSA,
  1005. id, area->ospf->router_id);
  1006. /* Set summary-LSA body fields. */
  1007. ospf_summary_lsa_body_set (s, p, metric);
  1008. /* Set length. */
  1009. length = stream_get_endp (s);
  1010. lsah->length = htons (length);
  1011. /* Create OSPF LSA instance. */
  1012. new = ospf_lsa_new ();
  1013. new->area = area;
  1014. SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
  1015. /* Copy LSA to store. */
  1016. new->data = ospf_lsa_data_new (length);
  1017. memcpy (new->data, lsah, length);
  1018. stream_free (s);
  1019. return new;
  1020. }
  1021. /* Originate Summary-LSA. */
  1022. struct ospf_lsa *
  1023. ospf_summary_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric,
  1024. struct ospf_area *area)
  1025. {
  1026. struct ospf_lsa *new;
  1027. struct in_addr id;
  1028. id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_SUMMARY_LSA, p);
  1029. if (id.s_addr == 0xffffffff)
  1030. {
  1031. /* Maybe Link State ID not available. */
  1032. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1033. zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
  1034. OSPF_SUMMARY_LSA);
  1035. return NULL;
  1036. }
  1037. /* Create new summary-LSA instance. */
  1038. if ( !(new = ospf_summary_lsa_new (area, (struct prefix *) p, metric, id)))
  1039. return NULL;
  1040. /* Instlal LSA to LSDB. */
  1041. new = ospf_lsa_install (area->ospf, NULL, new);
  1042. /* Update LSA origination count. */
  1043. area->ospf->lsa_originate_count++;
  1044. /* Flooding new LSA through area. */
  1045. ospf_flood_through_area (area, NULL, new);
  1046. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1047. {
  1048. zlog_debug ("LSA[Type%d:%s]: Originate summary-LSA %p",
  1049. new->data->type, inet_ntoa (new->data->id), new);
  1050. ospf_lsa_header_dump (new->data);
  1051. }
  1052. return new;
  1053. }
  1054. static struct ospf_lsa*
  1055. ospf_summary_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
  1056. {
  1057. struct ospf_lsa *new;
  1058. struct summary_lsa *sl;
  1059. struct prefix p;
  1060. /* Sanity check. */
  1061. assert (lsa->data);
  1062. sl = (struct summary_lsa *)lsa->data;
  1063. p.prefixlen = ip_masklen (sl->mask);
  1064. new = ospf_summary_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
  1065. sl->header.id);
  1066. if (!new)
  1067. return NULL;
  1068. new->data->ls_seqnum = lsa_seqnum_increment (lsa);
  1069. ospf_lsa_install (ospf, NULL, new);
  1070. /* Flood LSA through AS. */
  1071. ospf_flood_through_area (new->area, NULL, new);
  1072. /* Debug logging. */
  1073. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1074. {
  1075. zlog_debug ("LSA[Type%d:%s]: summary-LSA refresh",
  1076. new->data->type, inet_ntoa (new->data->id));
  1077. ospf_lsa_header_dump (new->data);
  1078. }
  1079. return new;
  1080. }
  1081. /* summary-ASBR-LSA related functions. */
  1082. static void
  1083. ospf_summary_asbr_lsa_body_set (struct stream *s, struct prefix *p,
  1084. u_int32_t metric)
  1085. {
  1086. struct in_addr mask;
  1087. masklen2ip (p->prefixlen, &mask);
  1088. /* Put Network Mask. */
  1089. stream_put_ipv4 (s, mask.s_addr);
  1090. /* Set # TOS. */
  1091. stream_putc (s, (u_char) 0);
  1092. /* Set metric. */
  1093. stream_put_ospf_metric (s, metric);
  1094. }
  1095. static struct ospf_lsa *
  1096. ospf_summary_asbr_lsa_new (struct ospf_area *area, struct prefix *p,
  1097. u_int32_t metric, struct in_addr id)
  1098. {
  1099. struct stream *s;
  1100. struct ospf_lsa *new;
  1101. struct lsa_header *lsah;
  1102. int length;
  1103. if (id.s_addr == 0xffffffff)
  1104. {
  1105. /* Maybe Link State ID not available. */
  1106. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1107. zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
  1108. OSPF_ASBR_SUMMARY_LSA);
  1109. return NULL;
  1110. }
  1111. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1112. zlog_debug ("LSA[Type3]: Create summary-LSA instance");
  1113. /* Create new stream for LSA. */
  1114. s = stream_new (OSPF_MAX_LSA_SIZE);
  1115. lsah = (struct lsa_header *) STREAM_DATA (s);
  1116. lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_ASBR_SUMMARY_LSA,
  1117. id, area->ospf->router_id);
  1118. /* Set summary-LSA body fields. */
  1119. ospf_summary_asbr_lsa_body_set (s, p, metric);
  1120. /* Set length. */
  1121. length = stream_get_endp (s);
  1122. lsah->length = htons (length);
  1123. /* Create OSPF LSA instance. */
  1124. new = ospf_lsa_new ();
  1125. new->area = area;
  1126. SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
  1127. /* Copy LSA to store. */
  1128. new->data = ospf_lsa_data_new (length);
  1129. memcpy (new->data, lsah, length);
  1130. stream_free (s);
  1131. return new;
  1132. }
  1133. /* Originate summary-ASBR-LSA. */
  1134. struct ospf_lsa *
  1135. ospf_summary_asbr_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric,
  1136. struct ospf_area *area)
  1137. {
  1138. struct ospf_lsa *new;
  1139. struct in_addr id;
  1140. id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_ASBR_SUMMARY_LSA, p);
  1141. if (id.s_addr == 0xffffffff)
  1142. {
  1143. /* Maybe Link State ID not available. */
  1144. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1145. zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
  1146. OSPF_ASBR_SUMMARY_LSA);
  1147. return NULL;
  1148. }
  1149. /* Create new summary-LSA instance. */
  1150. new = ospf_summary_asbr_lsa_new (area, (struct prefix *) p, metric, id);
  1151. if (!new)
  1152. return NULL;
  1153. /* Install LSA to LSDB. */
  1154. new = ospf_lsa_install (area->ospf, NULL, new);
  1155. /* Update LSA origination count. */
  1156. area->ospf->lsa_originate_count++;
  1157. /* Flooding new LSA through area. */
  1158. ospf_flood_through_area (area, NULL, new);
  1159. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1160. {
  1161. zlog_debug ("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
  1162. new->data->type, inet_ntoa (new->data->id), new);
  1163. ospf_lsa_header_dump (new->data);
  1164. }
  1165. return new;
  1166. }
  1167. static struct ospf_lsa*
  1168. ospf_summary_asbr_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
  1169. {
  1170. struct ospf_lsa *new;
  1171. struct summary_lsa *sl;
  1172. struct prefix p;
  1173. /* Sanity check. */
  1174. assert (lsa->data);
  1175. sl = (struct summary_lsa *)lsa->data;
  1176. p.prefixlen = ip_masklen (sl->mask);
  1177. new = ospf_summary_asbr_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
  1178. sl->header.id);
  1179. if (!new)
  1180. return NULL;
  1181. new->data->ls_seqnum = lsa_seqnum_increment (lsa);
  1182. ospf_lsa_install (ospf, NULL, new);
  1183. /* Flood LSA through area. */
  1184. ospf_flood_through_area (new->area, NULL, new);
  1185. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1186. {
  1187. zlog_debug ("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
  1188. new->data->type, inet_ntoa (new->data->id));
  1189. ospf_lsa_header_dump (new->data);
  1190. }
  1191. return new;
  1192. }
  1193. /* AS-external-LSA related functions. */
  1194. /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
  1195. is connected, else 0*/
  1196. static struct in_addr
  1197. ospf_external_lsa_nexthop_get (struct ospf *ospf, struct in_addr nexthop)
  1198. {
  1199. struct in_addr fwd;
  1200. struct prefix nh;
  1201. struct listnode *node;
  1202. struct ospf_interface *oi;
  1203. fwd.s_addr = 0;
  1204. if (!nexthop.s_addr)
  1205. return fwd;
  1206. /* Check whether nexthop is covered by OSPF network. */
  1207. nh.family = AF_INET;
  1208. nh.u.prefix4 = nexthop;
  1209. nh.prefixlen = IPV4_MAX_BITLEN;
  1210. /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
  1211. * better to make use of the per-ifp table of ois.
  1212. */
  1213. for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
  1214. if (if_is_operative (oi->ifp))
  1215. if (oi->address->family == AF_INET)
  1216. if (prefix_match (oi->address, &nh))
  1217. return nexthop;
  1218. return fwd;
  1219. }
  1220. /* NSSA-external-LSA related functions. */
  1221. /* Get 1st IP connection for Forward Addr */
  1222. struct in_addr
  1223. ospf_get_ip_from_ifp (struct ospf_interface *oi)
  1224. {
  1225. struct in_addr fwd;
  1226. fwd.s_addr = 0;
  1227. if (if_is_operative (oi->ifp))
  1228. return oi->address->u.prefix4;
  1229. return fwd;
  1230. }
  1231. /* Get 1st IP connection for Forward Addr */
  1232. struct in_addr
  1233. ospf_get_nssa_ip (struct ospf_area *area)
  1234. {
  1235. struct in_addr fwd;
  1236. struct in_addr best_default;
  1237. struct listnode *node;
  1238. struct ospf_interface *oi;
  1239. fwd.s_addr = 0;
  1240. best_default.s_addr = 0;
  1241. for (ALL_LIST_ELEMENTS_RO (area->ospf->oiflist, node, oi))
  1242. {
  1243. if (if_is_operative (oi->ifp))
  1244. if (oi->area->external_routing == OSPF_AREA_NSSA)
  1245. if (oi->address && oi->address->family == AF_INET)
  1246. {
  1247. if (best_default.s_addr == 0)
  1248. best_default = oi->address->u.prefix4;
  1249. if (oi->area == area)
  1250. return oi->address->u.prefix4;
  1251. }
  1252. }
  1253. if (best_default.s_addr != 0)
  1254. return best_default;
  1255. if (best_default.s_addr != 0)
  1256. return best_default;
  1257. return fwd;
  1258. }
  1259. #define DEFAULT_DEFAULT_METRIC 20
  1260. #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
  1261. #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
  1262. #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
  1263. int
  1264. metric_type (struct ospf *ospf, u_char src)
  1265. {
  1266. return (ospf->dmetric[src].type < 0 ?
  1267. DEFAULT_METRIC_TYPE : ospf->dmetric[src].type);
  1268. }
  1269. int
  1270. metric_value (struct ospf *ospf, u_char src)
  1271. {
  1272. if (ospf->dmetric[src].value < 0)
  1273. {
  1274. if (src == DEFAULT_ROUTE)
  1275. {
  1276. if (ospf->default_originate == DEFAULT_ORIGINATE_ZEBRA)
  1277. return DEFAULT_DEFAULT_ORIGINATE_METRIC;
  1278. else
  1279. return DEFAULT_DEFAULT_ALWAYS_METRIC;
  1280. }
  1281. else if (ospf->default_metric < 0)
  1282. return DEFAULT_DEFAULT_METRIC;
  1283. else
  1284. return ospf->default_metric;
  1285. }
  1286. return ospf->dmetric[src].value;
  1287. }
  1288. /* Set AS-external-LSA body. */
  1289. static void
  1290. ospf_external_lsa_body_set (struct stream *s, struct external_info *ei,
  1291. struct ospf *ospf)
  1292. {
  1293. struct prefix_ipv4 *p = &ei->p;
  1294. struct in_addr mask, fwd_addr;
  1295. u_int32_t mvalue;
  1296. int mtype;
  1297. int type;
  1298. /* Put Network Mask. */
  1299. masklen2ip (p->prefixlen, &mask);
  1300. stream_put_ipv4 (s, mask.s_addr);
  1301. /* If prefix is default, specify DEFAULT_ROUTE. */
  1302. type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
  1303. mtype = (ROUTEMAP_METRIC_TYPE (ei) != -1) ?
  1304. ROUTEMAP_METRIC_TYPE (ei) : metric_type (ospf, type);
  1305. mvalue = (ROUTEMAP_METRIC (ei) != -1) ?
  1306. ROUTEMAP_METRIC (ei) : metric_value (ospf, type);
  1307. /* Put type of external metric. */
  1308. stream_putc (s, (mtype == EXTERNAL_METRIC_TYPE_2 ? 0x80 : 0));
  1309. /* Put 0 metric. TOS metric is not supported. */
  1310. stream_put_ospf_metric (s, mvalue);
  1311. /* Get forwarding address to nexthop if on the Connection List, else 0. */
  1312. fwd_addr = ospf_external_lsa_nexthop_get (ospf, ei->nexthop);
  1313. /* Put forwarding address. */
  1314. stream_put_ipv4 (s, fwd_addr.s_addr);
  1315. /* Put route tag -- This value should be introduced from configuration. */
  1316. stream_putl (s, 0);
  1317. }
  1318. /* Create new external-LSA. */
  1319. static struct ospf_lsa *
  1320. ospf_external_lsa_new (struct ospf *ospf,
  1321. struct external_info *ei, struct in_addr *old_id)
  1322. {
  1323. struct stream *s;
  1324. struct lsa_header *lsah;
  1325. struct ospf_lsa *new;
  1326. struct in_addr id;
  1327. int length;
  1328. if (ei == NULL)
  1329. {
  1330. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1331. zlog_debug ("LSA[Type5]: External info is NULL, can't originate");
  1332. return NULL;
  1333. }
  1334. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1335. zlog_debug ("LSA[Type5]: Originate AS-external-LSA instance");
  1336. /* If old Link State ID is specified, refresh LSA with same ID. */
  1337. if (old_id)
  1338. id = *old_id;
  1339. /* Get Link State with unique ID. */
  1340. else
  1341. {
  1342. id = ospf_lsa_unique_id (ospf, ospf->lsdb, OSPF_AS_EXTERNAL_LSA, &ei->p);
  1343. if (id.s_addr == 0xffffffff)
  1344. {
  1345. /* Maybe Link State ID not available. */
  1346. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1347. zlog_debug ("LSA[Type5]: Link ID not available, can't originate");
  1348. return NULL;
  1349. }
  1350. }
  1351. /* Create new stream for LSA. */
  1352. s = stream_new (OSPF_MAX_LSA_SIZE);
  1353. lsah = (struct lsa_header *) STREAM_DATA (s);
  1354. /* Set LSA common header fields. */
  1355. lsa_header_set (s, OSPF_OPTION_E, OSPF_AS_EXTERNAL_LSA,
  1356. id, ospf->router_id);
  1357. /* Set AS-external-LSA body fields. */
  1358. ospf_external_lsa_body_set (s, ei, ospf);
  1359. /* Set length. */
  1360. length = stream_get_endp (s);
  1361. lsah->length = htons (length);
  1362. /* Now, create OSPF LSA instance. */
  1363. new = ospf_lsa_new ();
  1364. new->area = NULL;
  1365. SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_APPROVED | OSPF_LSA_SELF_CHECKED);
  1366. /* Copy LSA data to store, discard stream. */
  1367. new->data = ospf_lsa_data_new (length);
  1368. memcpy (new->data, lsah, length);
  1369. stream_free (s);
  1370. return new;
  1371. }
  1372. /* As Type-7 */
  1373. static void
  1374. ospf_install_flood_nssa (struct ospf *ospf,
  1375. struct ospf_lsa *lsa, struct external_info *ei)
  1376. {
  1377. struct ospf_lsa *new;
  1378. struct as_external_lsa *extlsa;
  1379. struct ospf_area *area;
  1380. struct listnode *node, *nnode;
  1381. /* LSA may be a Type-5 originated via translation of a Type-7 LSA
  1382. * which originated from an NSSA area. In which case it should not be
  1383. * flooded back to NSSA areas.
  1384. */
  1385. if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
  1386. return;
  1387. /* NSSA Originate or Refresh (If anyNSSA)
  1388. LSA is self-originated. And just installed as Type-5.
  1389. Additionally, install as Type-7 LSDB for every attached NSSA.
  1390. P-Bit controls which ABR performs translation to outside world; If
  1391. we are an ABR....do not set the P-bit, because we send the Type-5,
  1392. not as the ABR Translator, but as the ASBR owner within the AS!
  1393. If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
  1394. elected ABR Translator will see the P-bit, Translate, and re-flood.
  1395. Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
  1396. Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
  1397. for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
  1398. {
  1399. /* Don't install Type-7 LSA's into nonNSSA area */
  1400. if (area->external_routing != OSPF_AREA_NSSA)
  1401. continue;
  1402. /* make lsa duplicate, lock=1 */
  1403. new = ospf_lsa_dup (lsa);
  1404. new->area = area;
  1405. new->data->type = OSPF_AS_NSSA_LSA;
  1406. /* set P-bit if not ABR */
  1407. if (! IS_OSPF_ABR (ospf))
  1408. {
  1409. SET_FLAG(new->data->options, OSPF_OPTION_NP);
  1410. /* set non-zero FWD ADDR
  1411. draft-ietf-ospf-nssa-update-09.txt
  1412. if the network between the NSSA AS boundary router and the
  1413. adjacent AS is advertised into OSPF as an internal OSPF route,
  1414. the forwarding address should be the next op address as is cu
  1415. currently done with type-5 LSAs. If the intervening network is
  1416. not adversited into OSPF as an internal OSPF route and the
  1417. type-7 LSA's P-bit is set a forwarding address should be
  1418. selected from one of the router's active OSPF inteface addresses
  1419. which belong to the NSSA. If no such addresses exist, then
  1420. no type-7 LSA's with the P-bit set should originate from this
  1421. router. */
  1422. /* kevinm: not updating lsa anymore, just new */
  1423. extlsa = (struct as_external_lsa *)(new->data);
  1424. if (extlsa->e[0].fwd_addr.s_addr == 0)
  1425. extlsa->e[0].fwd_addr = ospf_get_nssa_ip(area); /* this NSSA area in ifp */
  1426. if (extlsa->e[0].fwd_addr.s_addr == 0)
  1427. {
  1428. if (IS_DEBUG_OSPF_NSSA)
  1429. zlog_debug ("LSA[Type-7]: Could not build FWD-ADDR");
  1430. ospf_lsa_discard (new);
  1431. return;
  1432. }
  1433. }
  1434. /* install also as Type-7 */
  1435. ospf_lsa_install (ospf, NULL, new); /* Remove Old, Lock New = 2 */
  1436. /* will send each copy, lock=2+n */
  1437. ospf_flood_through_as (ospf, NULL, new); /* all attached NSSA's, no AS/STUBs */
  1438. }
  1439. }
  1440. static struct ospf_lsa *
  1441. ospf_lsa_translated_nssa_new (struct ospf *ospf,
  1442. struct ospf_lsa *type7)
  1443. {
  1444. struct ospf_lsa *new;
  1445. struct as_external_lsa *ext, *extnew;
  1446. struct external_info ei;
  1447. ext = (struct as_external_lsa *)(type7->data);
  1448. /* need external_info struct, fill in bare minimum */
  1449. ei.p.family = AF_INET;
  1450. ei.p.prefix = type7->data->id;
  1451. ei.p.prefixlen = ip_masklen (ext->mask);
  1452. ei.type = ZEBRA_ROUTE_OSPF;
  1453. ei.nexthop = ext->header.adv_router;
  1454. ei.route_map_set.metric = -1;
  1455. ei.route_map_set.metric_type = -1;
  1456. ei.tag = 0;
  1457. if ( (new = ospf_external_lsa_new (ospf, &ei, &type7->data->id)) == NULL)
  1458. {
  1459. if (IS_DEBUG_OSPF_NSSA)
  1460. zlog_debug ("ospf_nssa_translate_originate(): Could not originate "
  1461. "Translated Type-5 for %s",
  1462. inet_ntoa (ei.p.prefix));
  1463. return NULL;
  1464. }
  1465. extnew = (struct as_external_lsa *)(new->data);
  1466. /* copy over Type-7 data to new */
  1467. extnew->e[0].tos = ext->e[0].tos;
  1468. extnew->e[0].route_tag = ext->e[0].route_tag;
  1469. extnew->e[0].fwd_addr.s_addr = ext->e[0].fwd_addr.s_addr;
  1470. new->data->ls_seqnum = type7->data->ls_seqnum;
  1471. /* add translated flag, checksum and lock new lsa */
  1472. SET_FLAG (new->flags, OSPF_LSA_LOCAL_XLT); /* Translated from 7 */
  1473. new = ospf_lsa_lock (new);
  1474. return new;
  1475. }
  1476. /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
  1477. struct ospf_lsa *
  1478. ospf_translated_nssa_originate (struct ospf *ospf, struct ospf_lsa *type7)
  1479. {
  1480. struct ospf_lsa *new;
  1481. struct as_external_lsa *extnew;
  1482. /* we cant use ospf_external_lsa_originate() as we need to set
  1483. * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
  1484. */
  1485. if ( (new = ospf_lsa_translated_nssa_new (ospf, type7)) == NULL)
  1486. {
  1487. if (IS_DEBUG_OSPF_NSSA)
  1488. zlog_debug ("ospf_translated_nssa_originate(): Could not translate "
  1489. "Type-7, Id %s, to Type-5",
  1490. inet_ntoa (type7->data->id));
  1491. return NULL;
  1492. }
  1493. extnew = (struct as_external_lsa *)new;
  1494. if (IS_DEBUG_OSPF_NSSA)
  1495. {
  1496. zlog_debug ("ospf_translated_nssa_originate(): "
  1497. "translated Type 7, installed:");
  1498. ospf_lsa_header_dump (new->data);
  1499. zlog_debug (" Network mask: %d",ip_masklen (extnew->mask));
  1500. zlog_debug (" Forward addr: %s", inet_ntoa (extnew->e[0].fwd_addr));
  1501. }
  1502. if ( (new = ospf_lsa_install (ospf, NULL, new)) == NULL)
  1503. {
  1504. if (IS_DEBUG_OSPF_NSSA)
  1505. zlog_debug ("ospf_lsa_translated_nssa_originate(): "
  1506. "Could not install LSA "
  1507. "id %s", inet_ntoa (type7->data->id));
  1508. return NULL;
  1509. }
  1510. ospf->lsa_originate_count++;
  1511. ospf_flood_through_as (ospf, NULL, new);
  1512. return new;
  1513. }
  1514. /* Refresh Translated from NSSA AS-external-LSA. */
  1515. struct ospf_lsa *
  1516. ospf_translated_nssa_refresh (struct ospf *ospf, struct ospf_lsa *type7,
  1517. struct ospf_lsa *type5)
  1518. {
  1519. struct ospf_lsa *new = NULL;
  1520. /* Sanity checks. */
  1521. assert (type7 || type5);
  1522. if (!(type7 || type5))
  1523. return NULL;
  1524. if (type7)
  1525. assert (type7->data);
  1526. if (type5)
  1527. assert (type5->data);
  1528. assert (ospf->anyNSSA);
  1529. /* get required data according to what has been given */
  1530. if (type7 && type5 == NULL)
  1531. {
  1532. /* find the translated Type-5 for this Type-7 */
  1533. struct as_external_lsa *ext = (struct as_external_lsa *)(type7->data);
  1534. struct prefix_ipv4 p =
  1535. {
  1536. .prefix = type7->data->id,
  1537. .prefixlen = ip_masklen (ext->mask),
  1538. .family = AF_INET,
  1539. };
  1540. type5 = ospf_external_info_find_lsa (ospf, &p);
  1541. }
  1542. else if (type5 && type7 == NULL)
  1543. {
  1544. /* find the type-7 from which supplied type-5 was translated,
  1545. * ie find first type-7 with same LSA Id.
  1546. */
  1547. struct listnode *ln, *lnn;
  1548. struct route_node *rn;
  1549. struct ospf_lsa *lsa;
  1550. struct ospf_area *area;
  1551. for (ALL_LIST_ELEMENTS (ospf->areas, ln, lnn, area))
  1552. {
  1553. if (area->external_routing != OSPF_AREA_NSSA
  1554. && !type7)
  1555. continue;
  1556. LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
  1557. {
  1558. if (lsa->data->id.s_addr == type5->data->id.s_addr)
  1559. {
  1560. type7 = lsa;
  1561. break;
  1562. }
  1563. }
  1564. }
  1565. }
  1566. /* do we have type7? */
  1567. if (!type7)
  1568. {
  1569. if (IS_DEBUG_OSPF_NSSA)
  1570. zlog_debug ("ospf_translated_nssa_refresh(): no Type-7 found for "
  1571. "Type-5 LSA Id %s",
  1572. inet_ntoa (type5->data->id));
  1573. return NULL;
  1574. }
  1575. /* do we have valid translated type5? */
  1576. if (type5 == NULL || !CHECK_FLAG (type5->flags, OSPF_LSA_LOCAL_XLT) )
  1577. {
  1578. if (IS_DEBUG_OSPF_NSSA)
  1579. zlog_debug ("ospf_translated_nssa_refresh(): No translated Type-5 "
  1580. "found for Type-7 with Id %s",
  1581. inet_ntoa (type7->data->id));
  1582. return NULL;
  1583. }
  1584. /* Delete LSA from neighbor retransmit-list. */
  1585. ospf_ls_retransmit_delete_nbr_as (ospf, type5);
  1586. /* create new translated LSA */
  1587. if ( (new = ospf_lsa_translated_nssa_new (ospf, type7)) == NULL)
  1588. {
  1589. if (IS_DEBUG_OSPF_NSSA)
  1590. zlog_debug ("ospf_translated_nssa_refresh(): Could not translate "
  1591. "Type-7 for %s to Type-5",
  1592. inet_ntoa (type7->data->id));
  1593. return NULL;
  1594. }
  1595. if ( !(new = ospf_lsa_install (ospf, NULL, new)) )
  1596. {
  1597. if (IS_DEBUG_OSPF_NSSA)
  1598. zlog_debug ("ospf_translated_nssa_refresh(): Could not install "
  1599. "translated LSA, Id %s",
  1600. inet_ntoa (type7->data->id));
  1601. return NULL;
  1602. }
  1603. /* Flood LSA through area. */
  1604. ospf_flood_through_as (ospf, NULL, new);
  1605. return new;
  1606. }
  1607. int
  1608. is_prefix_default (struct prefix_ipv4 *p)
  1609. {
  1610. struct prefix_ipv4 q;
  1611. q.family = AF_INET;
  1612. q.prefix.s_addr = 0;
  1613. q.prefixlen = 0;
  1614. return prefix_same ((struct prefix *) p, (struct prefix *) &q);
  1615. }
  1616. /* Originate an AS-external-LSA, install and flood. */
  1617. struct ospf_lsa *
  1618. ospf_external_lsa_originate (struct ospf *ospf, struct external_info *ei)
  1619. {
  1620. struct ospf_lsa *new;
  1621. /* Added for NSSA project....
  1622. External LSAs are originated in ASBRs as usual, but for NSSA systems.
  1623. there is the global Type-5 LSDB and a Type-7 LSDB installed for
  1624. every area. The Type-7's are flooded to every IR and every ABR; We
  1625. install the Type-5 LSDB so that the normal "refresh" code operates
  1626. as usual, and flag them as not used during ASE calculations. The
  1627. Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
  1628. Address of non-zero.
  1629. If an ABR is the elected NSSA translator, following SPF and during
  1630. the ABR task it will translate all the scanned Type-7's, with P-bit
  1631. ON and not-self generated, and translate to Type-5's throughout the
  1632. non-NSSA/STUB AS.
  1633. A difference in operation depends whether this ASBR is an ABR
  1634. or not. If not an ABR, the P-bit is ON, to indicate that any
  1635. elected NSSA-ABR can perform its translation.
  1636. If an ABR, the P-bit is OFF; No ABR will perform translation and
  1637. this ASBR will flood the Type-5 LSA as usual.
  1638. For the case where this ASBR is not an ABR, the ASE calculations
  1639. are based on the Type-5 LSDB; The Type-7 LSDB exists just to
  1640. demonstrate to the user that there are LSA's that belong to any
  1641. attached NSSA.
  1642. Finally, it just so happens that when the ABR is translating every
  1643. Type-7 into Type-5, it installs it into the Type-5 LSDB as an
  1644. approved Type-5 (translated from Type-7); at the end of translation
  1645. if any Translated Type-5's remain unapproved, then they must be
  1646. flushed from the AS.
  1647. */
  1648. /* Check the AS-external-LSA should be originated. */
  1649. if (!ospf_redistribute_check (ospf, ei, NULL))
  1650. return NULL;
  1651. /* Create new AS-external-LSA instance. */
  1652. if ((new = ospf_external_lsa_new (ospf, ei, NULL)) == NULL)
  1653. {
  1654. if (IS_DEBUG_OSPF_EVENT)
  1655. zlog_debug ("LSA[Type5:%s]: Could not originate AS-external-LSA",
  1656. inet_ntoa (ei->p.prefix));
  1657. return NULL;
  1658. }
  1659. /* Install newly created LSA into Type-5 LSDB, lock = 1. */
  1660. ospf_lsa_install (ospf, NULL, new);
  1661. /* Update LSA origination count. */
  1662. ospf->lsa_originate_count++;
  1663. /* Flooding new LSA. only to AS (non-NSSA/STUB) */
  1664. ospf_flood_through_as (ospf, NULL, new);
  1665. /* If there is any attached NSSA, do special handling */
  1666. if (ospf->anyNSSA &&
  1667. /* stay away from translated LSAs! */
  1668. !(CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT)))
  1669. ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood Type-7 to all NSSAs */
  1670. /* Debug logging. */
  1671. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1672. {
  1673. zlog_debug ("LSA[Type%d:%s]: Originate AS-external-LSA %p",
  1674. new->data->type, inet_ntoa (new->data->id), new);
  1675. ospf_lsa_header_dump (new->data);
  1676. }
  1677. return new;
  1678. }
  1679. /* Originate AS-external-LSA from external info with initial flag. */
  1680. int
  1681. ospf_external_lsa_originate_timer (struct thread *thread)
  1682. {
  1683. struct ospf *ospf = THREAD_ARG (thread);
  1684. struct route_node *rn;
  1685. struct external_info *ei;
  1686. struct route_table *rt;
  1687. int type = THREAD_VAL (thread);
  1688. ospf->t_external_lsa = NULL;
  1689. /* Originate As-external-LSA from all type of distribute source. */
  1690. if ((rt = EXTERNAL_INFO (type)))
  1691. for (rn = route_top (rt); rn; rn = route_next (rn))
  1692. if ((ei = rn->info) != NULL)
  1693. if (!is_prefix_default ((struct prefix_ipv4 *)&ei->p))
  1694. if (!ospf_external_lsa_originate (ospf, ei))
  1695. zlog_warn ("LSA: AS-external-LSA was not originated.");
  1696. return 0;
  1697. }
  1698. static struct external_info *
  1699. ospf_default_external_info (struct ospf *ospf)
  1700. {
  1701. int type;
  1702. struct route_node *rn;
  1703. struct prefix_ipv4 p;
  1704. p.family = AF_INET;
  1705. p.prefix.s_addr = 0;
  1706. p.prefixlen = 0;
  1707. /* First, lookup redistributed default route. */
  1708. for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
  1709. if (EXTERNAL_INFO (type) && type != ZEBRA_ROUTE_OSPF)
  1710. {
  1711. rn = route_node_lookup (EXTERNAL_INFO (type), (struct prefix *) &p);
  1712. if (rn != NULL)
  1713. {
  1714. route_unlock_node (rn);
  1715. assert (rn->info);
  1716. if (ospf_redistribute_check (ospf, rn->info, NULL))
  1717. return rn->info;
  1718. }
  1719. }
  1720. return NULL;
  1721. }
  1722. int
  1723. ospf_default_originate_timer (struct thread *thread)
  1724. {
  1725. struct prefix_ipv4 p;
  1726. struct in_addr nexthop;
  1727. struct external_info *ei;
  1728. struct ospf *ospf;
  1729. ospf = THREAD_ARG (thread);
  1730. p.family = AF_INET;
  1731. p.prefix.s_addr = 0;
  1732. p.prefixlen = 0;
  1733. if (ospf->default_originate == DEFAULT_ORIGINATE_ALWAYS)
  1734. {
  1735. /* If there is no default route via redistribute,
  1736. then originate AS-external-LSA with nexthop 0 (self). */
  1737. nexthop.s_addr = 0;
  1738. ospf_external_info_add (DEFAULT_ROUTE, p, 0, nexthop);
  1739. }
  1740. if ((ei = ospf_default_external_info (ospf)))
  1741. ospf_external_lsa_originate (ospf, ei);
  1742. return 0;
  1743. }
  1744. /* Flush any NSSA LSAs for given prefix */
  1745. void
  1746. ospf_nssa_lsa_flush (struct ospf *ospf, struct prefix_ipv4 *p)
  1747. {
  1748. struct listnode *node, *nnode;
  1749. struct ospf_lsa *lsa;
  1750. struct ospf_area *area;
  1751. for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
  1752. {
  1753. if (area->external_routing == OSPF_AREA_NSSA)
  1754. {
  1755. if (!(lsa = ospf_lsa_lookup (area, OSPF_AS_NSSA_LSA, p->prefix,
  1756. ospf->router_id)))
  1757. {
  1758. if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
  1759. zlog_debug ("LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
  1760. inet_ntoa (p->prefix), p->prefixlen);
  1761. continue;
  1762. }
  1763. ospf_ls_retransmit_delete_nbr_area (area, lsa);
  1764. if (!IS_LSA_MAXAGE (lsa))
  1765. {
  1766. ospf_refresher_unregister_lsa (ospf, lsa);
  1767. ospf_lsa_flush_area (lsa, area);
  1768. }
  1769. }
  1770. }
  1771. }
  1772. /* Flush an AS-external-LSA from LSDB and routing domain. */
  1773. void
  1774. ospf_external_lsa_flush (struct ospf *ospf,
  1775. u_char type, struct prefix_ipv4 *p,
  1776. unsigned int ifindex /*, struct in_addr nexthop */)
  1777. {
  1778. struct ospf_lsa *lsa;
  1779. if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
  1780. zlog_debug ("LSA: Flushing AS-external-LSA %s/%d",
  1781. inet_ntoa (p->prefix), p->prefixlen);
  1782. /* First lookup LSA from LSDB. */
  1783. if (!(lsa = ospf_external_info_find_lsa (ospf, p)))
  1784. {
  1785. if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
  1786. zlog_debug ("LSA: There is no such AS-external-LSA %s/%d in LSDB",
  1787. inet_ntoa (p->prefix), p->prefixlen);
  1788. return;
  1789. }
  1790. /* If LSA is selforiginated, not a translated LSA, and there is
  1791. * NSSA area, flush Type-7 LSA's at first.
  1792. */
  1793. if (IS_LSA_SELF(lsa) && (ospf->anyNSSA)
  1794. && !(CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT)))
  1795. ospf_nssa_lsa_flush (ospf, p);
  1796. /* Sweep LSA from Link State Retransmit List. */
  1797. ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
  1798. /* There must be no self-originated LSA in rtrs_external. */
  1799. #if 0
  1800. /* Remove External route from Zebra. */
  1801. ospf_zebra_delete ((struct prefix_ipv4 *) p, &nexthop);
  1802. #endif
  1803. if (!IS_LSA_MAXAGE (lsa))
  1804. {
  1805. /* Unregister LSA from Refresh queue. */
  1806. ospf_refresher_unregister_lsa (ospf, lsa);
  1807. /* Flush AS-external-LSA through AS. */
  1808. ospf_lsa_flush_as (ospf, lsa);
  1809. }
  1810. if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
  1811. zlog_debug ("ospf_external_lsa_flush(): stop");
  1812. }
  1813. void
  1814. ospf_external_lsa_refresh_default (struct ospf *ospf)
  1815. {
  1816. struct prefix_ipv4 p;
  1817. struct external_info *ei;
  1818. struct ospf_lsa *lsa;
  1819. p.family = AF_INET;
  1820. p.prefixlen = 0;
  1821. p.prefix.s_addr = 0;
  1822. ei = ospf_default_external_info (ospf);
  1823. lsa = ospf_external_info_find_lsa (ospf, &p);
  1824. if (ei)
  1825. {
  1826. if (lsa)
  1827. {
  1828. if (IS_DEBUG_OSPF_EVENT)
  1829. zlog_debug ("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p", lsa);
  1830. ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
  1831. }
  1832. else
  1833. {
  1834. if (IS_DEBUG_OSPF_EVENT)
  1835. zlog_debug ("LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
  1836. ospf_external_lsa_originate (ospf, ei);
  1837. }
  1838. }
  1839. else
  1840. {
  1841. if (lsa)
  1842. {
  1843. if (IS_DEBUG_OSPF_EVENT)
  1844. zlog_debug ("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
  1845. ospf_refresher_unregister_lsa (ospf, lsa);
  1846. ospf_lsa_flush_as (ospf, lsa);
  1847. }
  1848. }
  1849. }
  1850. void
  1851. ospf_external_lsa_refresh_type (struct ospf *ospf, u_char type, int force)
  1852. {
  1853. struct route_node *rn;
  1854. struct external_info *ei;
  1855. if (type != DEFAULT_ROUTE)
  1856. if (EXTERNAL_INFO(type))
  1857. /* Refresh each redistributed AS-external-LSAs. */
  1858. for (rn = route_top (EXTERNAL_INFO (type)); rn; rn = route_next (rn))
  1859. if ((ei = rn->info))
  1860. if (!is_prefix_default (&ei->p))
  1861. {
  1862. struct ospf_lsa *lsa;
  1863. if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
  1864. ospf_external_lsa_refresh (ospf, lsa, ei, force);
  1865. else
  1866. ospf_external_lsa_originate (ospf, ei);
  1867. }
  1868. }
  1869. /* Refresh AS-external-LSA. */
  1870. struct ospf_lsa *
  1871. ospf_external_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa,
  1872. struct external_info *ei, int force)
  1873. {
  1874. struct ospf_lsa *new;
  1875. int changed;
  1876. /* Check the AS-external-LSA should be originated. */
  1877. if (!ospf_redistribute_check (ospf, ei, &changed))
  1878. {
  1879. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1880. zlog_debug ("LSA[Type%d:%s]: Could not be refreshed, "
  1881. "redist check fail",
  1882. lsa->data->type, inet_ntoa (lsa->data->id));
  1883. ospf_external_lsa_flush (ospf, ei->type, &ei->p,
  1884. ei->ifindex /*, ei->nexthop */);
  1885. return NULL;
  1886. }
  1887. if (!changed && !force)
  1888. {
  1889. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1890. zlog_debug ("LSA[Type%d:%s]: Not refreshed, not changed/forced",
  1891. lsa->data->type, inet_ntoa (lsa->data->id));
  1892. return NULL;
  1893. }
  1894. /* Delete LSA from neighbor retransmit-list. */
  1895. ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
  1896. /* Unregister AS-external-LSA from refresh-list. */
  1897. ospf_refresher_unregister_lsa (ospf, lsa);
  1898. new = ospf_external_lsa_new (ospf, ei, &lsa->data->id);
  1899. if (new == NULL)
  1900. {
  1901. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1902. zlog_debug ("LSA[Type%d:%s]: Could not be refreshed", lsa->data->type,
  1903. inet_ntoa (lsa->data->id));
  1904. return NULL;
  1905. }
  1906. new->data->ls_seqnum = lsa_seqnum_increment (lsa);
  1907. ospf_lsa_install (ospf, NULL, new); /* As type-5. */
  1908. /* Flood LSA through AS. */
  1909. ospf_flood_through_as (ospf, NULL, new);
  1910. /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
  1911. if (ospf->anyNSSA && !(CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT)))
  1912. ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood per new rules */
  1913. /* Register self-originated LSA to refresh queue.
  1914. * Translated LSAs should not be registered, but refreshed upon
  1915. * refresh of the Type-7
  1916. */
  1917. if ( !CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT) )
  1918. ospf_refresher_register_lsa (ospf, new);
  1919. /* Debug logging. */
  1920. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  1921. {
  1922. zlog_debug ("LSA[Type%d:%s]: AS-external-LSA refresh",
  1923. new->data->type, inet_ntoa (new->data->id));
  1924. ospf_lsa_header_dump (new->data);
  1925. }
  1926. return new;
  1927. }
  1928. /* LSA installation functions. */
  1929. /* Install router-LSA to an area. */
  1930. static struct ospf_lsa *
  1931. ospf_router_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
  1932. int rt_recalc)
  1933. {
  1934. struct ospf_area *area = new->area;
  1935. /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
  1936. The entire routing table must be recalculated, starting with
  1937. the shortest path calculations for each area (not just the
  1938. area whose link-state database has changed).
  1939. */
  1940. if (IS_LSA_SELF (new))
  1941. {
  1942. /* Only install LSA if it is originated/refreshed by us.
  1943. * If LSA was received by flooding, the RECEIVED flag is set so do
  1944. * not link the LSA */
  1945. if (CHECK_FLAG (new->flags, OSPF_LSA_RECEIVED))
  1946. return new; /* ignore stale LSA */
  1947. /* Set self-originated router-LSA. */
  1948. ospf_lsa_unlock (&area->router_lsa_self);
  1949. area->router_lsa_self = ospf_lsa_lock (new);
  1950. ospf_refresher_register_lsa (ospf, new);
  1951. }
  1952. if (rt_recalc)
  1953. ospf_spf_calculate_schedule (ospf);
  1954. return new;
  1955. }
  1956. #define OSPF_INTERFACE_TIMER_ON(T,F,V) \
  1957. if (!(T)) \
  1958. (T) = thread_add_timer (master, (F), oi, (V))
  1959. /* Install network-LSA to an area. */
  1960. static struct ospf_lsa *
  1961. ospf_network_lsa_install (struct ospf *ospf,
  1962. struct ospf_interface *oi,
  1963. struct ospf_lsa *new,
  1964. int rt_recalc)
  1965. {
  1966. /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
  1967. The entire routing table must be recalculated, starting with
  1968. the shortest path calculations for each area (not just the
  1969. area whose link-state database has changed).
  1970. */
  1971. if (IS_LSA_SELF (new))
  1972. {
  1973. /* We supposed that when LSA is originated by us, we pass the int
  1974. for which it was originated. If LSA was received by flooding,
  1975. the RECEIVED flag is set, so we do not link the LSA to the int. */
  1976. if (CHECK_FLAG (new->flags, OSPF_LSA_RECEIVED))
  1977. return new; /* ignore stale LSA */
  1978. ospf_lsa_unlock (&oi->network_lsa_self);
  1979. oi->network_lsa_self = ospf_lsa_lock (new);
  1980. ospf_refresher_register_lsa (ospf, new);
  1981. }
  1982. if (rt_recalc)
  1983. ospf_spf_calculate_schedule (ospf);
  1984. return new;
  1985. }
  1986. /* Install summary-LSA to an area. */
  1987. static struct ospf_lsa *
  1988. ospf_summary_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
  1989. int rt_recalc)
  1990. {
  1991. if (rt_recalc && !IS_LSA_SELF (new))
  1992. {
  1993. /* RFC 2328 Section 13.2 Summary-LSAs
  1994. The best route to the destination described by the summary-
  1995. LSA must be recalculated (see Section 16.5). If this
  1996. destination is an AS boundary router, it may also be
  1997. necessary to re-examine all the AS-external-LSAs.
  1998. */
  1999. #if 0
  2000. /* This doesn't exist yet... */
  2001. ospf_summary_incremental_update(new); */
  2002. #else /* #if 0 */
  2003. ospf_spf_calculate_schedule (ospf);
  2004. #endif /* #if 0 */
  2005. if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
  2006. zlog_debug ("ospf_summary_lsa_install(): SPF scheduled");
  2007. }
  2008. if (IS_LSA_SELF (new))
  2009. ospf_refresher_register_lsa (ospf, new);
  2010. return new;
  2011. }
  2012. /* Install ASBR-summary-LSA to an area. */
  2013. static struct ospf_lsa *
  2014. ospf_summary_asbr_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
  2015. int rt_recalc)
  2016. {
  2017. if (rt_recalc && !IS_LSA_SELF (new))
  2018. {
  2019. /* RFC 2328 Section 13.2 Summary-LSAs
  2020. The best route to the destination described by the summary-
  2021. LSA must be recalculated (see Section 16.5). If this
  2022. destination is an AS boundary router, it may also be
  2023. necessary to re-examine all the AS-external-LSAs.
  2024. */
  2025. #if 0
  2026. /* These don't exist yet... */
  2027. ospf_summary_incremental_update(new);
  2028. /* Isn't this done by the above call?
  2029. - RFC 2328 Section 16.5 implies it should be */
  2030. /* ospf_ase_calculate_schedule(); */
  2031. #else /* #if 0 */
  2032. ospf_spf_calculate_schedule (ospf);
  2033. #endif /* #if 0 */
  2034. }
  2035. /* register LSA to refresh-list. */
  2036. if (IS_LSA_SELF (new))
  2037. ospf_refresher_register_lsa (ospf, new);
  2038. return new;
  2039. }
  2040. /* Install AS-external-LSA. */
  2041. static struct ospf_lsa *
  2042. ospf_external_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
  2043. int rt_recalc)
  2044. {
  2045. ospf_ase_register_external_lsa (new, ospf);
  2046. /* If LSA is not self-originated, calculate an external route. */
  2047. if (rt_recalc)
  2048. {
  2049. /* RFC 2328 Section 13.2 AS-external-LSAs
  2050. The best route to the destination described by the AS-
  2051. external-LSA must be recalculated (see Section 16.6).
  2052. */
  2053. if (!IS_LSA_SELF (new))
  2054. ospf_ase_incremental_update (ospf, new);
  2055. }
  2056. if (new->data->type == OSPF_AS_NSSA_LSA)
  2057. {
  2058. /* There is no point to register selforiginate Type-7 LSA for
  2059. * refreshing. We rely on refreshing Type-5 LSA's
  2060. */
  2061. if (IS_LSA_SELF (new))
  2062. return new;
  2063. else
  2064. {
  2065. /* Try refresh type-5 translated LSA for this LSA, if one exists.
  2066. * New translations will be taken care of by the abr_task.
  2067. */
  2068. ospf_translated_nssa_refresh (ospf, new, NULL);
  2069. }
  2070. }
  2071. /* Register self-originated LSA to refresh queue.
  2072. * Leave Translated LSAs alone if NSSA is enabled
  2073. */
  2074. if (IS_LSA_SELF (new) && !CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT ) )
  2075. ospf_refresher_register_lsa (ospf, new);
  2076. return new;
  2077. }
  2078. void
  2079. ospf_discard_from_db (struct ospf *ospf,
  2080. struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
  2081. {
  2082. struct ospf_lsa *old;
  2083. if (!lsdb)
  2084. {
  2085. zlog_warn ("%s: Called with NULL lsdb!", __func__);
  2086. if (!lsa)
  2087. zlog_warn ("%s: and NULL LSA!", __func__);
  2088. else
  2089. zlog_warn ("LSA[Type%d:%s]: not associated with LSDB!",
  2090. lsa->data->type, inet_ntoa (lsa->data->id));
  2091. return;
  2092. }
  2093. old = ospf_lsdb_lookup (lsdb, lsa);
  2094. if (!old)
  2095. return;
  2096. if (old->refresh_list >= 0)
  2097. ospf_refresher_unregister_lsa (ospf, old);
  2098. switch (old->data->type)
  2099. {
  2100. case OSPF_AS_EXTERNAL_LSA:
  2101. ospf_ase_unregister_external_lsa (old, ospf);
  2102. ospf_ls_retransmit_delete_nbr_as (ospf, old);
  2103. break;
  2104. #ifdef HAVE_OPAQUE_LSA
  2105. case OSPF_OPAQUE_AS_LSA:
  2106. ospf_ls_retransmit_delete_nbr_as (ospf, old);
  2107. break;
  2108. #endif /* HAVE_OPAQUE_LSA */
  2109. case OSPF_AS_NSSA_LSA:
  2110. ospf_ls_retransmit_delete_nbr_area (old->area, old);
  2111. ospf_ase_unregister_external_lsa (old, ospf);
  2112. break;
  2113. default:
  2114. ospf_ls_retransmit_delete_nbr_area (old->area, old);
  2115. break;
  2116. }
  2117. ospf_lsa_maxage_delete (ospf, old);
  2118. ospf_lsa_discard (old);
  2119. }
  2120. struct ospf_lsa *
  2121. ospf_lsa_install (struct ospf *ospf, struct ospf_interface *oi,
  2122. struct ospf_lsa *lsa)
  2123. {
  2124. struct ospf_lsa *new = NULL;
  2125. struct ospf_lsa *old = NULL;
  2126. struct ospf_lsdb *lsdb = NULL;
  2127. int rt_recalc;
  2128. /* Set LSDB. */
  2129. switch (lsa->data->type)
  2130. {
  2131. /* kevinm */
  2132. case OSPF_AS_NSSA_LSA:
  2133. if (lsa->area)
  2134. lsdb = lsa->area->lsdb;
  2135. else
  2136. lsdb = ospf->lsdb;
  2137. break;
  2138. case OSPF_AS_EXTERNAL_LSA:
  2139. #ifdef HAVE_OPAQUE_LSA
  2140. case OSPF_OPAQUE_AS_LSA:
  2141. #endif /* HAVE_OPAQUE_LSA */
  2142. lsdb = ospf->lsdb;
  2143. break;
  2144. default:
  2145. lsdb = lsa->area->lsdb;
  2146. break;
  2147. }
  2148. assert (lsdb);
  2149. /* RFC 2328 13.2. Installing LSAs in the database
  2150. Installing a new LSA in the database, either as the result of
  2151. flooding or a newly self-originated LSA, may cause the OSPF
  2152. routing table structure to be recalculated. The contents of the
  2153. new LSA should be compared to the old instance, if present. If
  2154. there is no difference, there is no need to recalculate the
  2155. routing table. When comparing an LSA to its previous instance,
  2156. the following are all considered to be differences in contents:
  2157. o The LSA's Options field has changed.
  2158. o One of the LSA instances has LS age set to MaxAge, and
  2159. the other does not.
  2160. o The length field in the LSA header has changed.
  2161. o The body of the LSA (i.e., anything outside the 20-byte
  2162. LSA header) has changed. Note that this excludes changes
  2163. in LS Sequence Number and LS Checksum.
  2164. */
  2165. /* Look up old LSA and determine if any SPF calculation or incremental
  2166. update is needed */
  2167. old = ospf_lsdb_lookup (lsdb, lsa);
  2168. /* Do comparision and record if recalc needed. */
  2169. rt_recalc = 0;
  2170. if ( old == NULL || ospf_lsa_different(old, lsa))
  2171. rt_recalc = 1;
  2172. /*
  2173. Sequence number check (Section 14.1 of rfc 2328)
  2174. "Premature aging is used when it is time for a self-originated
  2175. LSA's sequence number field to wrap. At this point, the current
  2176. LSA instance (having LS sequence number MaxSequenceNumber) must
  2177. be prematurely aged and flushed from the routing domain before a
  2178. new instance with sequence number equal to InitialSequenceNumber
  2179. can be originated. "
  2180. */
  2181. if (ntohl(lsa->data->ls_seqnum) - 1 == OSPF_MAX_SEQUENCE_NUMBER)
  2182. {
  2183. if (ospf_lsa_is_self_originated(ospf, lsa))
  2184. {
  2185. lsa->data->ls_seqnum = htonl(OSPF_MAX_SEQUENCE_NUMBER);
  2186. if (!IS_LSA_MAXAGE(lsa))
  2187. lsa->flags |= OSPF_LSA_PREMATURE_AGE;
  2188. lsa->data->ls_age = htons (OSPF_LSA_MAXAGE);
  2189. if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
  2190. {
  2191. zlog_debug ("ospf_lsa_install() Premature Aging "
  2192. "lsa 0x%p, seqnum 0x%x",
  2193. lsa, ntohl(lsa->data->ls_seqnum));
  2194. ospf_lsa_header_dump (lsa->data);
  2195. }
  2196. }
  2197. else
  2198. {
  2199. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  2200. {
  2201. zlog_debug ("ospf_lsa_install() got an lsa with seq 0x80000000 "
  2202. "that was not self originated. Ignoring\n");
  2203. ospf_lsa_header_dump (lsa->data);
  2204. }
  2205. return old;
  2206. }
  2207. }
  2208. /* discard old LSA from LSDB */
  2209. if (old != NULL)
  2210. ospf_discard_from_db (ospf, lsdb, lsa);
  2211. /* Calculate Checksum if self-originated?. */
  2212. if (IS_LSA_SELF (lsa))
  2213. ospf_lsa_checksum (lsa->data);
  2214. /* Insert LSA to LSDB. */
  2215. ospf_lsdb_add (lsdb, lsa);
  2216. lsa->lsdb = lsdb;
  2217. /* Do LSA specific installation process. */
  2218. switch (lsa->data->type)
  2219. {
  2220. case OSPF_ROUTER_LSA:
  2221. new = ospf_router_lsa_install (ospf, lsa, rt_recalc);
  2222. break;
  2223. case OSPF_NETWORK_LSA:
  2224. assert (oi);
  2225. new = ospf_network_lsa_install (ospf, oi, lsa, rt_recalc);
  2226. break;
  2227. case OSPF_SUMMARY_LSA:
  2228. new = ospf_summary_lsa_install (ospf, lsa, rt_recalc);
  2229. break;
  2230. case OSPF_ASBR_SUMMARY_LSA:
  2231. new = ospf_summary_asbr_lsa_install (ospf, lsa, rt_recalc);
  2232. break;
  2233. case OSPF_AS_EXTERNAL_LSA:
  2234. new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
  2235. break;
  2236. #ifdef HAVE_OPAQUE_LSA
  2237. case OSPF_OPAQUE_LINK_LSA:
  2238. if (IS_LSA_SELF (lsa))
  2239. lsa->oi = oi; /* Specify outgoing ospf-interface for this LSA. */
  2240. else
  2241. ; /* Incoming "oi" for this LSA has set at LSUpd reception. */
  2242. /* Fallthrough */
  2243. case OSPF_OPAQUE_AREA_LSA:
  2244. case OSPF_OPAQUE_AS_LSA:
  2245. new = ospf_opaque_lsa_install (lsa, rt_recalc);
  2246. break;
  2247. #endif /* HAVE_OPAQUE_LSA */
  2248. case OSPF_AS_NSSA_LSA:
  2249. new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
  2250. default: /* type-6,8,9....nothing special */
  2251. break;
  2252. }
  2253. if (new == NULL)
  2254. return new; /* Installation failed, cannot proceed further -- endo. */
  2255. /* Debug logs. */
  2256. if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
  2257. {
  2258. char area_str[INET_ADDRSTRLEN];
  2259. switch (lsa->data->type)
  2260. {
  2261. case OSPF_AS_EXTERNAL_LSA:
  2262. #ifdef HAVE_OPAQUE_LSA
  2263. case OSPF_OPAQUE_AS_LSA:
  2264. #endif /* HAVE_OPAQUE_LSA */
  2265. case OSPF_AS_NSSA_LSA:
  2266. zlog_debug ("LSA[%s]: Install %s",
  2267. dump_lsa_key (new),
  2268. LOOKUP (ospf_lsa_type_msg, new->data->type));
  2269. break;
  2270. default:
  2271. strcpy (area_str, inet_ntoa (new->area->area_id));
  2272. zlog_debug ("LSA[%s]: Install %s to Area %s",
  2273. dump_lsa_key (new),
  2274. LOOKUP (ospf_lsa_type_msg, new->data->type), area_str);
  2275. break;
  2276. }
  2277. }
  2278. /*
  2279. If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
  2280. (it's getting flushed out of the area), set LSA on MaxAge LSA list.
  2281. */
  2282. if ((lsa->flags & OSPF_LSA_PREMATURE_AGE) ||
  2283. (IS_LSA_MAXAGE (new) && !IS_LSA_SELF (new)))
  2284. {
  2285. if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
  2286. zlog_debug ("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
  2287. new->data->type,
  2288. inet_ntoa (new->data->id),
  2289. lsa);
  2290. ospf_lsa_flush (ospf, lsa);
  2291. }
  2292. return new;
  2293. }
  2294. static int
  2295. ospf_check_nbr_status (struct ospf *ospf)
  2296. {
  2297. struct listnode *node, *nnode;
  2298. struct ospf_interface *oi;
  2299. for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
  2300. {
  2301. struct route_node *rn;
  2302. struct ospf_neighbor *nbr;
  2303. if (ospf_if_is_enable (oi))
  2304. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  2305. if ((nbr = rn->info) != NULL)
  2306. if (nbr->state == NSM_Exchange || nbr->state == NSM_Loading)
  2307. {
  2308. route_unlock_node (rn);
  2309. return 0;
  2310. }
  2311. }
  2312. return 1;
  2313. }
  2314. static int
  2315. ospf_maxage_lsa_remover (struct thread *thread)
  2316. {
  2317. struct ospf *ospf = THREAD_ARG (thread);
  2318. struct ospf_lsa *lsa;
  2319. struct route_node *rn;
  2320. int reschedule = 0;
  2321. ospf->t_maxage = NULL;
  2322. if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
  2323. zlog_debug ("LSA[MaxAge]: remover Start");
  2324. reschedule = !ospf_check_nbr_status (ospf);
  2325. if (!reschedule)
  2326. for (rn = route_top(ospf->maxage_lsa); rn; rn = route_next(rn))
  2327. {
  2328. if ((lsa = rn->info) == NULL)
  2329. {
  2330. continue;
  2331. }
  2332. if (lsa->retransmit_counter > 0)
  2333. {
  2334. reschedule = 1;
  2335. continue;
  2336. }
  2337. /* TODO: maybe convert this function to a work-queue */
  2338. if (thread_should_yield (thread))
  2339. OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover, 0);
  2340. /* Remove LSA from the LSDB */
  2341. if (IS_LSA_SELF (lsa))
  2342. if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
  2343. zlog_debug ("LSA[Type%d:%s]: LSA 0x%lx is self-originated: ",
  2344. lsa->data->type, inet_ntoa (lsa->data->id), (u_long)lsa);
  2345. if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
  2346. zlog_debug ("LSA[Type%d:%s]: MaxAge LSA removed from list",
  2347. lsa->data->type, inet_ntoa (lsa->data->id));
  2348. if (CHECK_FLAG (lsa->flags, OSPF_LSA_PREMATURE_AGE))
  2349. {
  2350. if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
  2351. zlog_debug ("originating new lsa for lsa 0x%p\n", lsa);
  2352. ospf_lsa_refresh (ospf, lsa);
  2353. }
  2354. /* Remove from lsdb. */
  2355. if (lsa->lsdb)
  2356. {
  2357. ospf_discard_from_db (ospf, lsa->lsdb, lsa);
  2358. ospf_lsdb_delete (lsa->lsdb, lsa);
  2359. }
  2360. else
  2361. zlog_warn ("%s: LSA[Type%d:%s]: No associated LSDB!", __func__,
  2362. lsa->data->type, inet_ntoa (lsa->data->id));
  2363. }
  2364. /* A MaxAge LSA must be removed immediately from the router's link
  2365. state database as soon as both a) it is no longer contained on any
  2366. neighbor Link state retransmission lists and b) none of the router's
  2367. neighbors are in states Exchange or Loading. */
  2368. if (reschedule)
  2369. OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover,
  2370. ospf->maxage_delay);
  2371. return 0;
  2372. }
  2373. void
  2374. ospf_lsa_maxage_delete (struct ospf *ospf, struct ospf_lsa *lsa)
  2375. {
  2376. struct route_node *rn;
  2377. struct prefix_ls lsa_prefix;
  2378. ls_prefix_set (&lsa_prefix, lsa);
  2379. if ((rn = route_node_lookup(ospf->maxage_lsa,
  2380. (struct prefix *)&lsa_prefix)))
  2381. {
  2382. if (rn->info == lsa)
  2383. {
  2384. UNSET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
  2385. ospf_lsa_unlock (&lsa); /* maxage_lsa */
  2386. rn->info = NULL;
  2387. route_unlock_node (rn); /* route_node_lookup */
  2388. }
  2389. route_unlock_node (rn); /* route_node_lookup */
  2390. }
  2391. }
  2392. /* Add LSA onto the MaxAge list, and schedule for removal.
  2393. * This does *not* lead to the LSA being flooded, that must be taken
  2394. * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
  2395. * function).
  2396. */
  2397. void
  2398. ospf_lsa_maxage (struct ospf *ospf, struct ospf_lsa *lsa)
  2399. {
  2400. struct prefix_ls lsa_prefix;
  2401. struct route_node *rn;
  2402. /* When we saw a MaxAge LSA flooded to us, we put it on the list
  2403. and schedule the MaxAge LSA remover. */
  2404. if (CHECK_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE))
  2405. {
  2406. if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
  2407. zlog_debug ("LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
  2408. lsa->data->type, inet_ntoa (lsa->data->id), lsa);
  2409. return;
  2410. }
  2411. ls_prefix_set (&lsa_prefix, lsa);
  2412. if ((rn = route_node_get (ospf->maxage_lsa,
  2413. (struct prefix *)&lsa_prefix)) != NULL)
  2414. {
  2415. if (rn->info != NULL)
  2416. {
  2417. route_unlock_node (rn);
  2418. }
  2419. else
  2420. {
  2421. rn->info = ospf_lsa_lock(lsa);
  2422. SET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
  2423. }
  2424. }
  2425. else
  2426. {
  2427. zlog_err("Unable to allocate memory for maxage lsa\n");
  2428. assert(0);
  2429. }
  2430. if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
  2431. zlog_debug ("LSA[%s]: MaxAge LSA remover scheduled.", dump_lsa_key (lsa));
  2432. OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover,
  2433. ospf->maxage_delay);
  2434. }
  2435. static int
  2436. ospf_lsa_maxage_walker_remover (struct ospf *ospf, struct ospf_lsa *lsa)
  2437. {
  2438. /* Stay away from any Local Translated Type-7 LSAs */
  2439. if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
  2440. return 0;
  2441. if (IS_LSA_MAXAGE (lsa))
  2442. /* Self-originated LSAs should NOT time-out instead,
  2443. they're flushed and submitted to the max_age list explicitly. */
  2444. if (!ospf_lsa_is_self_originated (ospf, lsa))
  2445. {
  2446. if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
  2447. zlog_debug("LSA[%s]: is MaxAge", dump_lsa_key (lsa));
  2448. switch (lsa->data->type)
  2449. {
  2450. #ifdef HAVE_OPAQUE_LSA
  2451. case OSPF_OPAQUE_LINK_LSA:
  2452. case OSPF_OPAQUE_AREA_LSA:
  2453. case OSPF_OPAQUE_AS_LSA:
  2454. /*
  2455. * As a general rule, whenever network topology has changed
  2456. * (due to an LSA removal in this case), routing recalculation
  2457. * should be triggered. However, this is not true for opaque
  2458. * LSAs. Even if an opaque LSA instance is going to be removed
  2459. * from the routing domain, it does not mean a change in network
  2460. * topology, and thus, routing recalculation is not needed here.
  2461. */
  2462. break;
  2463. #endif /* HAVE_OPAQUE_LSA */
  2464. case OSPF_AS_EXTERNAL_LSA:
  2465. case OSPF_AS_NSSA_LSA:
  2466. ospf_ase_incremental_update (ospf, lsa);
  2467. break;
  2468. default:
  2469. ospf_spf_calculate_schedule (ospf);
  2470. break;
  2471. }
  2472. ospf_lsa_maxage (ospf, lsa);
  2473. }
  2474. if (IS_LSA_MAXAGE (lsa) && !ospf_lsa_is_self_originated (ospf, lsa))
  2475. if (LS_AGE (lsa) > OSPF_LSA_MAXAGE + 30)
  2476. printf ("Eek! Shouldn't happen!\n");
  2477. return 0;
  2478. }
  2479. /* Periodical check of MaxAge LSA. */
  2480. int
  2481. ospf_lsa_maxage_walker (struct thread *thread)
  2482. {
  2483. struct ospf *ospf = THREAD_ARG (thread);
  2484. struct route_node *rn;
  2485. struct ospf_lsa *lsa;
  2486. struct ospf_area *area;
  2487. struct listnode *node, *nnode;
  2488. ospf->t_maxage_walker = NULL;
  2489. for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
  2490. {
  2491. LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
  2492. ospf_lsa_maxage_walker_remover (ospf, lsa);
  2493. LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
  2494. ospf_lsa_maxage_walker_remover (ospf, lsa);
  2495. LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
  2496. ospf_lsa_maxage_walker_remover (ospf, lsa);
  2497. LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
  2498. ospf_lsa_maxage_walker_remover (ospf, lsa);
  2499. #ifdef HAVE_OPAQUE_LSA
  2500. LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
  2501. ospf_lsa_maxage_walker_remover (ospf, lsa);
  2502. LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
  2503. ospf_lsa_maxage_walker_remover (ospf, lsa);
  2504. #endif /* HAVE_OPAQUE_LSA */
  2505. LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
  2506. ospf_lsa_maxage_walker_remover (ospf, lsa);
  2507. }
  2508. /* for AS-external-LSAs. */
  2509. if (ospf->lsdb)
  2510. {
  2511. LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
  2512. ospf_lsa_maxage_walker_remover (ospf, lsa);
  2513. #ifdef HAVE_OPAQUE_LSA
  2514. LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
  2515. ospf_lsa_maxage_walker_remover (ospf, lsa);
  2516. #endif /* HAVE_OPAQUE_LSA */
  2517. }
  2518. OSPF_TIMER_ON (ospf->t_maxage_walker, ospf_lsa_maxage_walker,
  2519. OSPF_LSA_MAXAGE_CHECK_INTERVAL);
  2520. return 0;
  2521. }
  2522. struct ospf_lsa *
  2523. ospf_lsa_lookup_by_prefix (struct ospf_lsdb *lsdb, u_char type,
  2524. struct prefix_ipv4 *p, struct in_addr router_id)
  2525. {
  2526. struct ospf_lsa *lsa;
  2527. struct in_addr mask, id;
  2528. struct lsa_header_mask
  2529. {
  2530. struct lsa_header header;
  2531. struct in_addr mask;
  2532. } *hmask;
  2533. lsa = ospf_lsdb_lookup_by_id (lsdb, type, p->prefix, router_id);
  2534. if (lsa == NULL)
  2535. return NULL;
  2536. masklen2ip (p->prefixlen, &mask);
  2537. hmask = (struct lsa_header_mask *) lsa->data;
  2538. if (mask.s_addr != hmask->mask.s_addr)
  2539. {
  2540. id.s_addr = p->prefix.s_addr | (~mask.s_addr);
  2541. lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, router_id);
  2542. if (!lsa)
  2543. return NULL;
  2544. }
  2545. return lsa;
  2546. }
  2547. struct ospf_lsa *
  2548. ospf_lsa_lookup (struct ospf_area *area, u_int32_t type,
  2549. struct in_addr id, struct in_addr adv_router)
  2550. {
  2551. struct ospf *ospf = ospf_lookup();
  2552. assert(ospf);
  2553. switch (type)
  2554. {
  2555. case OSPF_ROUTER_LSA:
  2556. case OSPF_NETWORK_LSA:
  2557. case OSPF_SUMMARY_LSA:
  2558. case OSPF_ASBR_SUMMARY_LSA:
  2559. case OSPF_AS_NSSA_LSA:
  2560. #ifdef HAVE_OPAQUE_LSA
  2561. case OSPF_OPAQUE_LINK_LSA:
  2562. case OSPF_OPAQUE_AREA_LSA:
  2563. #endif /* HAVE_OPAQUE_LSA */
  2564. return ospf_lsdb_lookup_by_id (area->lsdb, type, id, adv_router);
  2565. case OSPF_AS_EXTERNAL_LSA:
  2566. #ifdef HAVE_OPAQUE_LSA
  2567. case OSPF_OPAQUE_AS_LSA:
  2568. #endif /* HAVE_OPAQUE_LSA */
  2569. return ospf_lsdb_lookup_by_id (ospf->lsdb, type, id, adv_router);
  2570. default:
  2571. break;
  2572. }
  2573. return NULL;
  2574. }
  2575. struct ospf_lsa *
  2576. ospf_lsa_lookup_by_id (struct ospf_area *area, u_int32_t type,
  2577. struct in_addr id)
  2578. {
  2579. struct ospf_lsa *lsa;
  2580. struct route_node *rn;
  2581. switch (type)
  2582. {
  2583. case OSPF_ROUTER_LSA:
  2584. return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
  2585. case OSPF_NETWORK_LSA:
  2586. for (rn = route_top (NETWORK_LSDB (area)); rn; rn = route_next (rn))
  2587. if ((lsa = rn->info))
  2588. if (IPV4_ADDR_SAME (&lsa->data->id, &id))
  2589. {
  2590. route_unlock_node (rn);
  2591. return lsa;
  2592. }
  2593. break;
  2594. case OSPF_SUMMARY_LSA:
  2595. case OSPF_ASBR_SUMMARY_LSA:
  2596. /* Currently not used. */
  2597. assert (1);
  2598. return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
  2599. case OSPF_AS_EXTERNAL_LSA:
  2600. case OSPF_AS_NSSA_LSA:
  2601. #ifdef HAVE_OPAQUE_LSA
  2602. case OSPF_OPAQUE_LINK_LSA:
  2603. case OSPF_OPAQUE_AREA_LSA:
  2604. case OSPF_OPAQUE_AS_LSA:
  2605. /* Currently not used. */
  2606. break;
  2607. #endif /* HAVE_OPAQUE_LSA */
  2608. default:
  2609. break;
  2610. }
  2611. return NULL;
  2612. }
  2613. struct ospf_lsa *
  2614. ospf_lsa_lookup_by_header (struct ospf_area *area, struct lsa_header *lsah)
  2615. {
  2616. struct ospf_lsa *match;
  2617. #ifdef HAVE_OPAQUE_LSA
  2618. /*
  2619. * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
  2620. * is redefined to have two subfields; opaque-type and opaque-id.
  2621. * However, it is harmless to treat the two sub fields together, as if
  2622. * they two were forming a unique LSA-ID.
  2623. */
  2624. #endif /* HAVE_OPAQUE_LSA */
  2625. match = ospf_lsa_lookup (area, lsah->type, lsah->id, lsah->adv_router);
  2626. if (match == NULL)
  2627. if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
  2628. zlog_debug ("LSA[Type%d:%s]: Lookup by header, NO MATCH",
  2629. lsah->type, inet_ntoa (lsah->id));
  2630. return match;
  2631. }
  2632. /* return +n, l1 is more recent.
  2633. return -n, l2 is more recent.
  2634. return 0, l1 and l2 is identical. */
  2635. int
  2636. ospf_lsa_more_recent (struct ospf_lsa *l1, struct ospf_lsa *l2)
  2637. {
  2638. int r;
  2639. int x, y;
  2640. if (l1 == NULL && l2 == NULL)
  2641. return 0;
  2642. if (l1 == NULL)
  2643. return -1;
  2644. if (l2 == NULL)
  2645. return 1;
  2646. /* compare LS sequence number. */
  2647. x = (int) ntohl (l1->data->ls_seqnum);
  2648. y = (int) ntohl (l2->data->ls_seqnum);
  2649. if (x > y)
  2650. return 1;
  2651. if (x < y)
  2652. return -1;
  2653. /* compare LS checksum. */
  2654. r = ntohs (l1->data->checksum) - ntohs (l2->data->checksum);
  2655. if (r)
  2656. return r;
  2657. /* compare LS age. */
  2658. if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
  2659. return 1;
  2660. else if (!IS_LSA_MAXAGE (l1) && IS_LSA_MAXAGE (l2))
  2661. return -1;
  2662. /* compare LS age with MaxAgeDiff. */
  2663. if (LS_AGE (l1) - LS_AGE (l2) > OSPF_LSA_MAXAGE_DIFF)
  2664. return -1;
  2665. else if (LS_AGE (l2) - LS_AGE (l1) > OSPF_LSA_MAXAGE_DIFF)
  2666. return 1;
  2667. /* LSAs are identical. */
  2668. return 0;
  2669. }
  2670. /* If two LSAs are different, return 1, otherwise return 0. */
  2671. int
  2672. ospf_lsa_different (struct ospf_lsa *l1, struct ospf_lsa *l2)
  2673. {
  2674. char *p1, *p2;
  2675. assert (l1);
  2676. assert (l2);
  2677. assert (l1->data);
  2678. assert (l2->data);
  2679. if (l1->data->options != l2->data->options)
  2680. return 1;
  2681. if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
  2682. return 1;
  2683. if (IS_LSA_MAXAGE (l2) && !IS_LSA_MAXAGE (l1))
  2684. return 1;
  2685. if (l1->data->length != l2->data->length)
  2686. return 1;
  2687. if (l1->data->length == 0)
  2688. return 1;
  2689. if (CHECK_FLAG ((l1->flags ^ l2->flags), OSPF_LSA_RECEIVED))
  2690. return 1; /* May be a stale LSA in the LSBD */
  2691. assert ( ntohs(l1->data->length) > OSPF_LSA_HEADER_SIZE);
  2692. p1 = (char *) l1->data;
  2693. p2 = (char *) l2->data;
  2694. if (memcmp (p1 + OSPF_LSA_HEADER_SIZE, p2 + OSPF_LSA_HEADER_SIZE,
  2695. ntohs( l1->data->length ) - OSPF_LSA_HEADER_SIZE) != 0)
  2696. return 1;
  2697. return 0;
  2698. }
  2699. #ifdef ORIGINAL_CODING
  2700. void
  2701. ospf_lsa_flush_self_originated (struct ospf_neighbor *nbr,
  2702. struct ospf_lsa *self,
  2703. struct ospf_lsa *new)
  2704. {
  2705. u_int32_t seqnum;
  2706. /* Adjust LS Sequence Number. */
  2707. seqnum = ntohl (new->data->ls_seqnum) + 1;
  2708. self->data->ls_seqnum = htonl (seqnum);
  2709. /* Recalculate LSA checksum. */
  2710. ospf_lsa_checksum (self->data);
  2711. /* Reflooding LSA. */
  2712. /* RFC2328 Section 13.3
  2713. On non-broadcast networks, separate Link State Update
  2714. packets must be sent, as unicasts, to each adjacent neighbor
  2715. (i.e., those in state Exchange or greater). The destination
  2716. IP addresses for these packets are the neighbors' IP
  2717. addresses. */
  2718. if (nbr->oi->type == OSPF_IFTYPE_NBMA)
  2719. {
  2720. struct route_node *rn;
  2721. struct ospf_neighbor *onbr;
  2722. for (rn = route_top (nbr->oi->nbrs); rn; rn = route_next (rn))
  2723. if ((onbr = rn->info) != NULL)
  2724. if (onbr != nbr->oi->nbr_self && onbr->status >= NSM_Exchange)
  2725. ospf_ls_upd_send_lsa (onbr, self, OSPF_SEND_PACKET_DIRECT);
  2726. }
  2727. else
  2728. ospf_ls_upd_send_lsa (nbr, self, OSPF_SEND_PACKET_INDIRECT);
  2729. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  2730. zlog_debug ("LSA[Type%d:%s]: Flush self-originated LSA",
  2731. self->data->type, inet_ntoa (self->data->id));
  2732. }
  2733. #else /* ORIGINAL_CODING */
  2734. static int
  2735. ospf_lsa_flush_schedule (struct ospf *ospf, struct ospf_lsa *lsa)
  2736. {
  2737. if (lsa == NULL || !IS_LSA_SELF (lsa))
  2738. return 0;
  2739. if (IS_DEBUG_OSPF_EVENT)
  2740. zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
  2741. /* Force given lsa's age to MaxAge. */
  2742. lsa->data->ls_age = htons (OSPF_LSA_MAXAGE);
  2743. switch (lsa->data->type)
  2744. {
  2745. #ifdef HAVE_OPAQUE_LSA
  2746. /* Opaque wants to be notified of flushes */
  2747. case OSPF_OPAQUE_LINK_LSA:
  2748. case OSPF_OPAQUE_AREA_LSA:
  2749. case OSPF_OPAQUE_AS_LSA:
  2750. ospf_opaque_lsa_refresh (lsa);
  2751. break;
  2752. #endif /* HAVE_OPAQUE_LSA */
  2753. default:
  2754. ospf_refresher_unregister_lsa (ospf, lsa);
  2755. ospf_lsa_flush (ospf, lsa);
  2756. break;
  2757. }
  2758. return 0;
  2759. }
  2760. void
  2761. ospf_flush_self_originated_lsas_now (struct ospf *ospf)
  2762. {
  2763. struct listnode *node, *nnode;
  2764. struct listnode *node2, *nnode2;
  2765. struct ospf_area *area;
  2766. struct ospf_interface *oi;
  2767. struct ospf_lsa *lsa;
  2768. struct route_node *rn;
  2769. int need_to_flush_ase = 0;
  2770. for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
  2771. {
  2772. if ((lsa = area->router_lsa_self) != NULL)
  2773. {
  2774. if (IS_DEBUG_OSPF_EVENT)
  2775. zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
  2776. lsa->data->type, inet_ntoa (lsa->data->id));
  2777. ospf_refresher_unregister_lsa (ospf, lsa);
  2778. ospf_lsa_flush_area (lsa, area);
  2779. ospf_lsa_unlock (&area->router_lsa_self);
  2780. area->router_lsa_self = NULL;
  2781. }
  2782. for (ALL_LIST_ELEMENTS (area->oiflist, node2, nnode2, oi))
  2783. {
  2784. if ((lsa = oi->network_lsa_self) != NULL
  2785. && oi->state == ISM_DR
  2786. && oi->full_nbrs > 0)
  2787. {
  2788. if (IS_DEBUG_OSPF_EVENT)
  2789. zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
  2790. lsa->data->type, inet_ntoa (lsa->data->id));
  2791. ospf_refresher_unregister_lsa (ospf, oi->network_lsa_self);
  2792. ospf_lsa_flush_area (oi->network_lsa_self, area);
  2793. ospf_lsa_unlock (&oi->network_lsa_self);
  2794. oi->network_lsa_self = NULL;
  2795. }
  2796. if (oi->type != OSPF_IFTYPE_VIRTUALLINK
  2797. && area->external_routing == OSPF_AREA_DEFAULT)
  2798. need_to_flush_ase = 1;
  2799. }
  2800. LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
  2801. ospf_lsa_flush_schedule (ospf, lsa);
  2802. LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
  2803. ospf_lsa_flush_schedule (ospf, lsa);
  2804. #ifdef HAVE_OPAQUE_LSA
  2805. LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
  2806. ospf_lsa_flush_schedule (ospf, lsa);
  2807. LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
  2808. ospf_lsa_flush_schedule (ospf, lsa);
  2809. #endif /* HAVE_OPAQUE_LSA */
  2810. }
  2811. if (need_to_flush_ase)
  2812. {
  2813. LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
  2814. ospf_lsa_flush_schedule (ospf, lsa);
  2815. #ifdef HAVE_OPAQUE_LSA
  2816. LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
  2817. ospf_lsa_flush_schedule (ospf, lsa);
  2818. #endif /* HAVE_OPAQUE_LSA */
  2819. }
  2820. /*
  2821. * Make sure that the MaxAge LSA remover is executed immediately,
  2822. * without conflicting to other threads.
  2823. */
  2824. if (ospf->t_maxage != NULL)
  2825. {
  2826. OSPF_TIMER_OFF (ospf->t_maxage);
  2827. thread_execute (master, ospf_maxage_lsa_remover, ospf, 0);
  2828. }
  2829. return;
  2830. }
  2831. #endif /* ORIGINAL_CODING */
  2832. /* If there is self-originated LSA, then return 1, otherwise return 0. */
  2833. /* An interface-independent version of ospf_lsa_is_self_originated */
  2834. int
  2835. ospf_lsa_is_self_originated (struct ospf *ospf, struct ospf_lsa *lsa)
  2836. {
  2837. struct listnode *node;
  2838. struct ospf_interface *oi;
  2839. /* This LSA is already checked. */
  2840. if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED))
  2841. return IS_LSA_SELF (lsa);
  2842. /* Make sure LSA is self-checked. */
  2843. SET_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED);
  2844. /* AdvRouter and Router ID is the same. */
  2845. if (IPV4_ADDR_SAME (&lsa->data->adv_router, &ospf->router_id))
  2846. SET_FLAG (lsa->flags, OSPF_LSA_SELF);
  2847. /* LSA is router-LSA. */
  2848. else if (lsa->data->type == OSPF_ROUTER_LSA &&
  2849. IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
  2850. SET_FLAG (lsa->flags, OSPF_LSA_SELF);
  2851. /* LSA is network-LSA. Compare Link ID with all interfaces. */
  2852. else if (lsa->data->type == OSPF_NETWORK_LSA)
  2853. for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
  2854. {
  2855. /* Ignore virtual link. */
  2856. if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
  2857. if (oi->address->family == AF_INET)
  2858. if (IPV4_ADDR_SAME (&lsa->data->id, &oi->address->u.prefix4))
  2859. {
  2860. /* to make it easier later */
  2861. SET_FLAG (lsa->flags, OSPF_LSA_SELF);
  2862. return IS_LSA_SELF (lsa);
  2863. }
  2864. }
  2865. return IS_LSA_SELF (lsa);
  2866. }
  2867. /* Get unique Link State ID. */
  2868. struct in_addr
  2869. ospf_lsa_unique_id (struct ospf *ospf,
  2870. struct ospf_lsdb *lsdb, u_char type, struct prefix_ipv4 *p)
  2871. {
  2872. struct ospf_lsa *lsa;
  2873. struct in_addr mask, id;
  2874. id = p->prefix;
  2875. /* Check existence of LSA instance. */
  2876. lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, ospf->router_id);
  2877. if (lsa)
  2878. {
  2879. struct as_external_lsa *al = (struct as_external_lsa *) lsa->data;
  2880. if (ip_masklen (al->mask) == p->prefixlen)
  2881. {
  2882. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  2883. zlog_debug ("ospf_lsa_unique_id(): "
  2884. "Can't get Link State ID for %s/%d",
  2885. inet_ntoa (p->prefix), p->prefixlen);
  2886. /* id.s_addr = 0; */
  2887. id.s_addr = 0xffffffff;
  2888. return id;
  2889. }
  2890. /* Masklen differs, then apply wildcard mask to Link State ID. */
  2891. else
  2892. {
  2893. masklen2ip (p->prefixlen, &mask);
  2894. id.s_addr = p->prefix.s_addr | (~mask.s_addr);
  2895. lsa = ospf_lsdb_lookup_by_id (ospf->lsdb, type,
  2896. id, ospf->router_id);
  2897. if (lsa)
  2898. {
  2899. if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  2900. zlog_debug ("ospf_lsa_unique_id(): "
  2901. "Can't get Link State ID for %s/%d",
  2902. inet_ntoa (p->prefix), p->prefixlen);
  2903. /* id.s_addr = 0; */
  2904. id.s_addr = 0xffffffff;
  2905. return id;
  2906. }
  2907. }
  2908. }
  2909. return id;
  2910. }
  2911. #define LSA_ACTION_FLOOD_AREA 1
  2912. #define LSA_ACTION_FLUSH_AREA 2
  2913. struct lsa_action
  2914. {
  2915. u_char action;
  2916. struct ospf_area *area;
  2917. struct ospf_lsa *lsa;
  2918. };
  2919. static int
  2920. ospf_lsa_action (struct thread *t)
  2921. {
  2922. struct lsa_action *data;
  2923. data = THREAD_ARG (t);
  2924. if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
  2925. zlog_debug ("LSA[Action]: Performing scheduled LSA action: %d",
  2926. data->action);
  2927. switch (data->action)
  2928. {
  2929. case LSA_ACTION_FLOOD_AREA:
  2930. ospf_flood_through_area (data->area, NULL, data->lsa);
  2931. break;
  2932. case LSA_ACTION_FLUSH_AREA:
  2933. ospf_lsa_flush_area (data->lsa, data->area);
  2934. break;
  2935. }
  2936. ospf_lsa_unlock (&data->lsa); /* Message */
  2937. XFREE (MTYPE_OSPF_MESSAGE, data);
  2938. return 0;
  2939. }
  2940. void
  2941. ospf_schedule_lsa_flood_area (struct ospf_area *area, struct ospf_lsa *lsa)
  2942. {
  2943. struct lsa_action *data;
  2944. data = XCALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
  2945. data->action = LSA_ACTION_FLOOD_AREA;
  2946. data->area = area;
  2947. data->lsa = ospf_lsa_lock (lsa); /* Message / Flood area */
  2948. thread_add_event (master, ospf_lsa_action, data, 0);
  2949. }
  2950. void
  2951. ospf_schedule_lsa_flush_area (struct ospf_area *area, struct ospf_lsa *lsa)
  2952. {
  2953. struct lsa_action *data;
  2954. data = XCALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
  2955. data->action = LSA_ACTION_FLUSH_AREA;
  2956. data->area = area;
  2957. data->lsa = ospf_lsa_lock (lsa); /* Message / Flush area */
  2958. thread_add_event (master, ospf_lsa_action, data, 0);
  2959. }
  2960. /* LSA Refreshment functions. */
  2961. struct ospf_lsa *
  2962. ospf_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
  2963. {
  2964. struct external_info *ei;
  2965. struct ospf_lsa *new = NULL;
  2966. assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
  2967. assert (IS_LSA_SELF (lsa));
  2968. assert (lsa->lock > 0);
  2969. switch (lsa->data->type)
  2970. {
  2971. /* Router and Network LSAs are processed differently. */
  2972. case OSPF_ROUTER_LSA:
  2973. new = ospf_router_lsa_refresh (lsa);
  2974. break;
  2975. case OSPF_NETWORK_LSA:
  2976. new = ospf_network_lsa_refresh (lsa);
  2977. break;
  2978. case OSPF_SUMMARY_LSA:
  2979. new = ospf_summary_lsa_refresh (ospf, lsa);
  2980. break;
  2981. case OSPF_ASBR_SUMMARY_LSA:
  2982. new = ospf_summary_asbr_lsa_refresh (ospf, lsa);
  2983. break;
  2984. case OSPF_AS_EXTERNAL_LSA:
  2985. /* Translated from NSSA Type-5s are refreshed when
  2986. * from refresh of Type-7 - do not refresh these directly.
  2987. */
  2988. if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
  2989. break;
  2990. ei = ospf_external_info_check (lsa);
  2991. if (ei)
  2992. new = ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
  2993. else
  2994. ospf_lsa_flush_as (ospf, lsa);
  2995. break;
  2996. #ifdef HAVE_OPAQUE_LSA
  2997. case OSPF_OPAQUE_LINK_LSA:
  2998. case OSPF_OPAQUE_AREA_LSA:
  2999. case OSPF_OPAQUE_AS_LSA:
  3000. new = ospf_opaque_lsa_refresh (lsa);
  3001. break;
  3002. #endif /* HAVE_OPAQUE_LSA */
  3003. default:
  3004. break;
  3005. }
  3006. return new;
  3007. }
  3008. void
  3009. ospf_refresher_register_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
  3010. {
  3011. u_int16_t index, current_index;
  3012. assert (lsa->lock > 0);
  3013. assert (IS_LSA_SELF (lsa));
  3014. if (lsa->refresh_list < 0)
  3015. {
  3016. int delay;
  3017. if (LS_AGE (lsa) == 0 &&
  3018. ntohl (lsa->data->ls_seqnum) == OSPF_INITIAL_SEQUENCE_NUMBER)
  3019. /* Randomize first update by OSPF_LS_REFRESH_SHIFT factor */
  3020. delay = OSPF_LS_REFRESH_SHIFT + (random () % OSPF_LS_REFRESH_TIME);
  3021. else
  3022. /* Randomize another updates by +-OSPF_LS_REFRESH_JITTER factor */
  3023. delay = OSPF_LS_REFRESH_TIME - LS_AGE (lsa) - OSPF_LS_REFRESH_JITTER
  3024. + (random () % (2*OSPF_LS_REFRESH_JITTER));
  3025. if (delay < 0)
  3026. delay = 0;
  3027. current_index = ospf->lsa_refresh_queue.index + (quagga_time (NULL)
  3028. - ospf->lsa_refresher_started)/OSPF_LSA_REFRESHER_GRANULARITY;
  3029. index = (current_index + delay/OSPF_LSA_REFRESHER_GRANULARITY)
  3030. % (OSPF_LSA_REFRESHER_SLOTS);
  3031. if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
  3032. zlog_debug ("LSA[Refresh]: lsa %s with age %d added to index %d",
  3033. inet_ntoa (lsa->data->id), LS_AGE (lsa), index);
  3034. if (!ospf->lsa_refresh_queue.qs[index])
  3035. ospf->lsa_refresh_queue.qs[index] = list_new ();
  3036. listnode_add (ospf->lsa_refresh_queue.qs[index],
  3037. ospf_lsa_lock (lsa)); /* lsa_refresh_queue */
  3038. lsa->refresh_list = index;
  3039. if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
  3040. zlog_debug ("LSA[Refresh:%s]: ospf_refresher_register_lsa(): "
  3041. "setting refresh_list on lsa %p (slod %d)",
  3042. inet_ntoa (lsa->data->id), lsa, index);
  3043. }
  3044. }
  3045. void
  3046. ospf_refresher_unregister_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
  3047. {
  3048. assert (lsa->lock > 0);
  3049. assert (IS_LSA_SELF (lsa));
  3050. if (lsa->refresh_list >= 0)
  3051. {
  3052. struct list *refresh_list = ospf->lsa_refresh_queue.qs[lsa->refresh_list];
  3053. listnode_delete (refresh_list, lsa);
  3054. if (!listcount (refresh_list))
  3055. {
  3056. list_free (refresh_list);
  3057. ospf->lsa_refresh_queue.qs[lsa->refresh_list] = NULL;
  3058. }
  3059. ospf_lsa_unlock (&lsa); /* lsa_refresh_queue */
  3060. lsa->refresh_list = -1;
  3061. }
  3062. }
  3063. int
  3064. ospf_lsa_refresh_walker (struct thread *t)
  3065. {
  3066. struct list *refresh_list;
  3067. struct listnode *node, *nnode;
  3068. struct ospf *ospf = THREAD_ARG (t);
  3069. struct ospf_lsa *lsa;
  3070. int i;
  3071. struct list *lsa_to_refresh = list_new ();
  3072. if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
  3073. zlog_debug ("LSA[Refresh]:ospf_lsa_refresh_walker(): start");
  3074. i = ospf->lsa_refresh_queue.index;
  3075. /* Note: if clock has jumped backwards, then time change could be negative,
  3076. so we are careful to cast the expression to unsigned before taking
  3077. modulus. */
  3078. ospf->lsa_refresh_queue.index =
  3079. ((unsigned long)(ospf->lsa_refresh_queue.index +
  3080. (quagga_time (NULL) - ospf->lsa_refresher_started)
  3081. / OSPF_LSA_REFRESHER_GRANULARITY))
  3082. % OSPF_LSA_REFRESHER_SLOTS;
  3083. if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
  3084. zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
  3085. ospf->lsa_refresh_queue.index);
  3086. for (;i != ospf->lsa_refresh_queue.index;
  3087. i = (i + 1) % OSPF_LSA_REFRESHER_SLOTS)
  3088. {
  3089. if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
  3090. zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): "
  3091. "refresh index %d", i);
  3092. refresh_list = ospf->lsa_refresh_queue.qs [i];
  3093. assert (i >= 0);
  3094. ospf->lsa_refresh_queue.qs [i] = NULL;
  3095. if (refresh_list)
  3096. {
  3097. for (ALL_LIST_ELEMENTS (refresh_list, node, nnode, lsa))
  3098. {
  3099. if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
  3100. zlog_debug ("LSA[Refresh:%s]: ospf_lsa_refresh_walker(): "
  3101. "refresh lsa %p (slot %d)",
  3102. inet_ntoa (lsa->data->id), lsa, i);
  3103. assert (lsa->lock > 0);
  3104. list_delete_node (refresh_list, node);
  3105. lsa->refresh_list = -1;
  3106. listnode_add (lsa_to_refresh, lsa);
  3107. }
  3108. list_free (refresh_list);
  3109. }
  3110. }
  3111. ospf->t_lsa_refresher = thread_add_timer (master, ospf_lsa_refresh_walker,
  3112. ospf, ospf->lsa_refresh_interval);
  3113. ospf->lsa_refresher_started = quagga_time (NULL);
  3114. for (ALL_LIST_ELEMENTS (lsa_to_refresh, node, nnode, lsa))
  3115. {
  3116. ospf_lsa_refresh (ospf, lsa);
  3117. assert (lsa->lock > 0);
  3118. ospf_lsa_unlock (&lsa); /* lsa_refresh_queue & temp for lsa_to_refresh*/
  3119. }
  3120. list_delete (lsa_to_refresh);
  3121. if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
  3122. zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): end");
  3123. return 0;
  3124. }