ospf_lsa.c 101 KB

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