ospf_lsa.c 97 KB

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