isis_lsp.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074
  1. /*
  2. * IS-IS Rout(e)ing protocol - isis_lsp.c
  3. * LSP processing
  4. *
  5. * Copyright (C) 2001,2002 Sampo Saaristo
  6. * Tampere University of Technology
  7. * Institute of Communications Engineering
  8. * Copyright (C) 2013-2015 Christian Franke <chris@opensourcerouting.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. #include <zebra.h>
  24. #include "linklist.h"
  25. #include "thread.h"
  26. #include "vty.h"
  27. #include "stream.h"
  28. #include "memory.h"
  29. #include "log.h"
  30. #include "prefix.h"
  31. #include "command.h"
  32. #include "hash.h"
  33. #include "if.h"
  34. #include "checksum.h"
  35. #include "md5.h"
  36. #include "table.h"
  37. #include "isisd/dict.h"
  38. #include "isisd/isis_constants.h"
  39. #include "isisd/isis_common.h"
  40. #include "isisd/isis_flags.h"
  41. #include "isisd/isis_circuit.h"
  42. #include "isisd/isisd.h"
  43. #include "isisd/isis_tlv.h"
  44. #include "isisd/isis_lsp.h"
  45. #include "isisd/isis_pdu.h"
  46. #include "isisd/isis_dynhn.h"
  47. #include "isisd/isis_misc.h"
  48. #include "isisd/isis_csm.h"
  49. #include "isisd/isis_adjacency.h"
  50. #include "isisd/isis_spf.h"
  51. #include "isisd/isis_te.h"
  52. #ifdef TOPOLOGY_GENERATE
  53. #include "spgrid.h"
  54. #endif
  55. /* staticly assigned vars for printing purposes */
  56. char lsp_bits_string[200]; /* FIXME: enough ? */
  57. static int lsp_l1_refresh (struct thread *thread);
  58. static int lsp_l2_refresh (struct thread *thread);
  59. static int lsp_l1_refresh_pseudo (struct thread *thread);
  60. static int lsp_l2_refresh_pseudo (struct thread *thread);
  61. int
  62. lsp_id_cmp (u_char * id1, u_char * id2)
  63. {
  64. return memcmp (id1, id2, ISIS_SYS_ID_LEN + 2);
  65. }
  66. dict_t *
  67. lsp_db_init (void)
  68. {
  69. dict_t *dict;
  70. dict = dict_create (DICTCOUNT_T_MAX, (dict_comp_t) lsp_id_cmp);
  71. return dict;
  72. }
  73. struct isis_lsp *
  74. lsp_search (u_char * id, dict_t * lspdb)
  75. {
  76. dnode_t *node;
  77. #ifdef EXTREME_DEBUG
  78. dnode_t *dn;
  79. zlog_debug ("searching db");
  80. for (dn = dict_first (lspdb); dn; dn = dict_next (lspdb, dn))
  81. {
  82. zlog_debug ("%s\t%pX", rawlspid_print ((u_char *) dnode_getkey (dn)),
  83. dnode_get (dn));
  84. }
  85. #endif /* EXTREME DEBUG */
  86. node = dict_lookup (lspdb, id);
  87. if (node)
  88. return (struct isis_lsp *) dnode_get (node);
  89. return NULL;
  90. }
  91. static void
  92. lsp_clear_data (struct isis_lsp *lsp)
  93. {
  94. if (!lsp)
  95. return;
  96. if (lsp->tlv_data.hostname)
  97. isis_dynhn_remove (lsp->lsp_header->lsp_id);
  98. if (lsp->own_lsp)
  99. {
  100. if (lsp->tlv_data.nlpids)
  101. XFREE (MTYPE_ISIS_TLV, lsp->tlv_data.nlpids);
  102. if (lsp->tlv_data.hostname)
  103. XFREE (MTYPE_ISIS_TLV, lsp->tlv_data.hostname);
  104. if (lsp->tlv_data.router_id)
  105. XFREE (MTYPE_ISIS_TLV, lsp->tlv_data.router_id);
  106. }
  107. free_tlvs (&lsp->tlv_data);
  108. }
  109. static void
  110. lsp_destroy (struct isis_lsp *lsp)
  111. {
  112. struct listnode *cnode, *lnode, *lnnode;
  113. struct isis_lsp *lsp_in_list;
  114. struct isis_circuit *circuit;
  115. if (!lsp)
  116. return;
  117. if (lsp->area->circuit_list) {
  118. for (ALL_LIST_ELEMENTS_RO (lsp->area->circuit_list, cnode, circuit))
  119. {
  120. if (circuit->lsp_queue == NULL)
  121. continue;
  122. for (ALL_LIST_ELEMENTS (circuit->lsp_queue, lnode, lnnode, lsp_in_list))
  123. if (lsp_in_list == lsp)
  124. list_delete_node(circuit->lsp_queue, lnode);
  125. }
  126. }
  127. ISIS_FLAGS_CLEAR_ALL (lsp->SSNflags);
  128. ISIS_FLAGS_CLEAR_ALL (lsp->SRMflags);
  129. lsp_clear_data (lsp);
  130. if (LSP_FRAGMENT (lsp->lsp_header->lsp_id) == 0 && lsp->lspu.frags)
  131. {
  132. list_delete (lsp->lspu.frags);
  133. lsp->lspu.frags = NULL;
  134. }
  135. isis_spf_schedule (lsp->area, lsp->level);
  136. #ifdef HAVE_IPV6
  137. isis_spf_schedule6 (lsp->area, lsp->level);
  138. #endif
  139. if (lsp->pdu)
  140. stream_free (lsp->pdu);
  141. XFREE (MTYPE_ISIS_LSP, lsp);
  142. }
  143. void
  144. lsp_db_destroy (dict_t * lspdb)
  145. {
  146. dnode_t *dnode, *next;
  147. struct isis_lsp *lsp;
  148. dnode = dict_first (lspdb);
  149. while (dnode)
  150. {
  151. next = dict_next (lspdb, dnode);
  152. lsp = dnode_get (dnode);
  153. lsp_destroy (lsp);
  154. dict_delete_free (lspdb, dnode);
  155. dnode = next;
  156. }
  157. dict_free (lspdb);
  158. return;
  159. }
  160. /*
  161. * Remove all the frags belonging to the given lsp
  162. */
  163. static void
  164. lsp_remove_frags (struct list *frags, dict_t * lspdb)
  165. {
  166. dnode_t *dnode;
  167. struct listnode *lnode, *lnnode;
  168. struct isis_lsp *lsp;
  169. for (ALL_LIST_ELEMENTS (frags, lnode, lnnode, lsp))
  170. {
  171. dnode = dict_lookup (lspdb, lsp->lsp_header->lsp_id);
  172. lsp_destroy (lsp);
  173. dnode_destroy (dict_delete (lspdb, dnode));
  174. }
  175. list_delete_all_node (frags);
  176. return;
  177. }
  178. void
  179. lsp_search_and_destroy (u_char * id, dict_t * lspdb)
  180. {
  181. dnode_t *node;
  182. struct isis_lsp *lsp;
  183. node = dict_lookup (lspdb, id);
  184. if (node)
  185. {
  186. node = dict_delete (lspdb, node);
  187. lsp = dnode_get (node);
  188. /*
  189. * If this is a zero lsp, remove all the frags now
  190. */
  191. if (LSP_FRAGMENT (lsp->lsp_header->lsp_id) == 0)
  192. {
  193. if (lsp->lspu.frags)
  194. lsp_remove_frags (lsp->lspu.frags, lspdb);
  195. }
  196. else
  197. {
  198. /*
  199. * else just remove this frag, from the zero lsps' frag list
  200. */
  201. if (lsp->lspu.zero_lsp && lsp->lspu.zero_lsp->lspu.frags)
  202. listnode_delete (lsp->lspu.zero_lsp->lspu.frags, lsp);
  203. }
  204. lsp_destroy (lsp);
  205. dnode_destroy (node);
  206. }
  207. }
  208. /*
  209. * Compares a LSP to given values
  210. * Params are given in net order
  211. */
  212. int
  213. lsp_compare (char *areatag, struct isis_lsp *lsp, u_int32_t seq_num,
  214. u_int16_t checksum, u_int16_t rem_lifetime)
  215. {
  216. /* no point in double ntohl on seqnum */
  217. if (lsp->lsp_header->seq_num == seq_num &&
  218. lsp->lsp_header->checksum == checksum &&
  219. /*comparing with 0, no need to do ntohl */
  220. ((lsp->lsp_header->rem_lifetime == 0 && rem_lifetime == 0) ||
  221. (lsp->lsp_header->rem_lifetime != 0 && rem_lifetime != 0)))
  222. {
  223. if (isis->debugs & DEBUG_SNP_PACKETS)
  224. {
  225. zlog_debug ("ISIS-Snp (%s): Compare LSP %s seq 0x%08x, cksum 0x%04x,"
  226. " lifetime %us",
  227. areatag,
  228. rawlspid_print (lsp->lsp_header->lsp_id),
  229. ntohl (lsp->lsp_header->seq_num),
  230. ntohs (lsp->lsp_header->checksum),
  231. ntohs (lsp->lsp_header->rem_lifetime));
  232. zlog_debug ("ISIS-Snp (%s): is equal to ours seq 0x%08x,"
  233. " cksum 0x%04x, lifetime %us",
  234. areatag,
  235. ntohl (seq_num), ntohs (checksum), ntohs (rem_lifetime));
  236. }
  237. return LSP_EQUAL;
  238. }
  239. /*
  240. * LSPs with identical checksums should only be treated as newer if:
  241. * a) The current LSP has a remaining lifetime != 0 and the other LSP has a
  242. * remaining lifetime == 0. In this case, we should participate in the purge
  243. * and should not treat the current LSP with remaining lifetime == 0 as older.
  244. * b) The LSP has an incorrect checksum. In this case, we need to react as given
  245. * in 7.3.16.2.
  246. */
  247. if (ntohl (seq_num) > ntohl (lsp->lsp_header->seq_num)
  248. || (ntohl(seq_num) == ntohl(lsp->lsp_header->seq_num)
  249. && ( (lsp->lsp_header->rem_lifetime != 0
  250. && rem_lifetime == 0)
  251. || lsp->lsp_header->checksum != checksum)))
  252. {
  253. if (isis->debugs & DEBUG_SNP_PACKETS)
  254. {
  255. zlog_debug ("ISIS-Snp (%s): Compare LSP %s seq 0x%08x, cksum 0x%04x,"
  256. " lifetime %us",
  257. areatag,
  258. rawlspid_print (lsp->lsp_header->lsp_id),
  259. ntohl (seq_num), ntohs (checksum), ntohs (rem_lifetime));
  260. zlog_debug ("ISIS-Snp (%s): is newer than ours seq 0x%08x, "
  261. "cksum 0x%04x, lifetime %us",
  262. areatag,
  263. ntohl (lsp->lsp_header->seq_num),
  264. ntohs (lsp->lsp_header->checksum),
  265. ntohs (lsp->lsp_header->rem_lifetime));
  266. }
  267. return LSP_NEWER;
  268. }
  269. if (isis->debugs & DEBUG_SNP_PACKETS)
  270. {
  271. zlog_debug
  272. ("ISIS-Snp (%s): Compare LSP %s seq 0x%08x, cksum 0x%04x, lifetime %us",
  273. areatag, rawlspid_print (lsp->lsp_header->lsp_id), ntohl (seq_num),
  274. ntohs (checksum), ntohs (rem_lifetime));
  275. zlog_debug ("ISIS-Snp (%s): is older than ours seq 0x%08x,"
  276. " cksum 0x%04x, lifetime %us", areatag,
  277. ntohl (lsp->lsp_header->seq_num),
  278. ntohs (lsp->lsp_header->checksum),
  279. ntohs (lsp->lsp_header->rem_lifetime));
  280. }
  281. return LSP_OLDER;
  282. }
  283. static void
  284. lsp_auth_add (struct isis_lsp *lsp)
  285. {
  286. struct isis_passwd *passwd;
  287. unsigned char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
  288. /*
  289. * Add the authentication info if its present
  290. */
  291. (lsp->level == IS_LEVEL_1) ? (passwd = &lsp->area->area_passwd) :
  292. (passwd = &lsp->area->domain_passwd);
  293. switch (passwd->type)
  294. {
  295. /* Cleartext */
  296. case ISIS_PASSWD_TYPE_CLEARTXT:
  297. memcpy (&lsp->tlv_data.auth_info, passwd, sizeof (struct isis_passwd));
  298. tlv_add_authinfo (passwd->type, passwd->len, passwd->passwd, lsp->pdu);
  299. break;
  300. /* HMAC MD5 */
  301. case ISIS_PASSWD_TYPE_HMAC_MD5:
  302. /* Remember where TLV is written so we can later
  303. * overwrite the MD5 hash */
  304. lsp->auth_tlv_offset = stream_get_endp (lsp->pdu);
  305. memset(&hmac_md5_hash, 0, ISIS_AUTH_MD5_SIZE);
  306. lsp->tlv_data.auth_info.type = ISIS_PASSWD_TYPE_HMAC_MD5;
  307. lsp->tlv_data.auth_info.len = ISIS_AUTH_MD5_SIZE;
  308. memcpy (&lsp->tlv_data.auth_info.passwd, hmac_md5_hash,
  309. ISIS_AUTH_MD5_SIZE);
  310. tlv_add_authinfo (passwd->type, ISIS_AUTH_MD5_SIZE, hmac_md5_hash,
  311. lsp->pdu);
  312. break;
  313. default:
  314. break;
  315. }
  316. }
  317. static void
  318. lsp_auth_update (struct isis_lsp *lsp)
  319. {
  320. struct isis_passwd *passwd;
  321. unsigned char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
  322. uint16_t checksum, rem_lifetime;
  323. /* For HMAC MD5 we need to recompute the md5 hash and store it */
  324. (lsp->level == IS_LEVEL_1) ? (passwd = &lsp->area->area_passwd) :
  325. (passwd = &lsp->area->domain_passwd);
  326. if (passwd->type != ISIS_PASSWD_TYPE_HMAC_MD5)
  327. return;
  328. /*
  329. * In transient conditions (when net is configured where authentication
  330. * config and lsp regenerate schedule is not yet run), there could be
  331. * an own_lsp with auth_tlv_offset set to 0. In such a case, simply
  332. * return, when lsp_regenerate is run, lsp will have auth tlv.
  333. */
  334. if (lsp->auth_tlv_offset == 0)
  335. return;
  336. /*
  337. * RFC 5304 set auth value, checksum and remaining lifetime to zero
  338. * before computation and reset to old values after computation.
  339. */
  340. checksum = lsp->lsp_header->checksum;
  341. rem_lifetime = lsp->lsp_header->rem_lifetime;
  342. lsp->lsp_header->checksum = 0;
  343. lsp->lsp_header->rem_lifetime = 0;
  344. /* Set the authentication value as well to zero */
  345. memset (STREAM_DATA (lsp->pdu) + lsp->auth_tlv_offset + 3,
  346. 0, ISIS_AUTH_MD5_SIZE);
  347. /* Compute autentication value */
  348. hmac_md5 (STREAM_DATA (lsp->pdu), stream_get_endp(lsp->pdu),
  349. (unsigned char *) &passwd->passwd, passwd->len,
  350. (unsigned char *) &hmac_md5_hash);
  351. /* Copy the hash into the stream */
  352. memcpy (STREAM_DATA (lsp->pdu) + lsp->auth_tlv_offset + 3,
  353. hmac_md5_hash, ISIS_AUTH_MD5_SIZE);
  354. memcpy (&lsp->tlv_data.auth_info.passwd, hmac_md5_hash,
  355. ISIS_AUTH_MD5_SIZE);
  356. /* Copy back the checksum and remaining lifetime */
  357. lsp->lsp_header->checksum = checksum;
  358. lsp->lsp_header->rem_lifetime = rem_lifetime;
  359. }
  360. void
  361. lsp_inc_seqnum (struct isis_lsp *lsp, u_int32_t seq_num)
  362. {
  363. u_int32_t newseq;
  364. if (seq_num == 0 || ntohl (lsp->lsp_header->seq_num) > seq_num)
  365. newseq = ntohl (lsp->lsp_header->seq_num) + 1;
  366. else
  367. newseq = seq_num + 1;
  368. lsp->lsp_header->seq_num = htonl (newseq);
  369. /* Recompute authentication and checksum information */
  370. lsp_auth_update (lsp);
  371. /* ISO 10589 - 7.3.11 Generation of the checksum
  372. * The checksum shall be computed over all fields in the LSP which appear
  373. * after the Remaining Lifetime field. This field (and those appearing
  374. * before it) are excluded so that the LSP may be aged by systems without
  375. * requiring recomputation.
  376. */
  377. fletcher_checksum(STREAM_DATA (lsp->pdu) + 12,
  378. ntohs (lsp->lsp_header->pdu_len) - 12, 12);
  379. isis_spf_schedule (lsp->area, lsp->level);
  380. #ifdef HAVE_IPV6
  381. isis_spf_schedule6 (lsp->area, lsp->level);
  382. #endif
  383. return;
  384. }
  385. /*
  386. * Genetates checksum for LSP and its frags
  387. */
  388. static void
  389. lsp_seqnum_update (struct isis_lsp *lsp0)
  390. {
  391. struct isis_lsp *lsp;
  392. struct listnode *node;
  393. lsp_inc_seqnum (lsp0, 0);
  394. if (!lsp0->lspu.frags)
  395. return;
  396. for (ALL_LIST_ELEMENTS_RO (lsp0->lspu.frags, node, lsp))
  397. lsp_inc_seqnum (lsp, 0);
  398. return;
  399. }
  400. static u_int8_t
  401. lsp_bits_generate (int level, int overload_bit, int attached_bit)
  402. {
  403. u_int8_t lsp_bits = 0;
  404. if (level == IS_LEVEL_1)
  405. lsp_bits = IS_LEVEL_1;
  406. else
  407. lsp_bits = IS_LEVEL_1_AND_2;
  408. if (overload_bit)
  409. lsp_bits |= overload_bit;
  410. if (attached_bit)
  411. lsp_bits |= attached_bit;
  412. return lsp_bits;
  413. }
  414. static void
  415. lsp_update_data (struct isis_lsp *lsp, struct stream *stream,
  416. struct isis_area *area, int level)
  417. {
  418. uint32_t expected = 0, found;
  419. int retval;
  420. /* free the old lsp data */
  421. lsp_clear_data (lsp);
  422. /* copying only the relevant part of our stream */
  423. if (lsp->pdu != NULL)
  424. stream_free (lsp->pdu);
  425. lsp->pdu = stream_dup (stream);
  426. /* setting pointers to the correct place */
  427. lsp->isis_header = (struct isis_fixed_hdr *) (STREAM_DATA (lsp->pdu));
  428. lsp->lsp_header = (struct isis_link_state_hdr *) (STREAM_DATA (lsp->pdu) +
  429. ISIS_FIXED_HDR_LEN);
  430. lsp->area = area;
  431. lsp->level = level;
  432. lsp->age_out = ZERO_AGE_LIFETIME;
  433. lsp->installed = time (NULL);
  434. /*
  435. * Get LSP data i.e. TLVs
  436. */
  437. expected |= TLVFLAG_AUTH_INFO;
  438. expected |= TLVFLAG_AREA_ADDRS;
  439. expected |= TLVFLAG_IS_NEIGHS;
  440. expected |= TLVFLAG_NLPID;
  441. if (area->dynhostname)
  442. expected |= TLVFLAG_DYN_HOSTNAME;
  443. if (area->newmetric)
  444. {
  445. expected |= TLVFLAG_TE_IS_NEIGHS;
  446. expected |= TLVFLAG_TE_IPV4_REACHABILITY;
  447. expected |= TLVFLAG_TE_ROUTER_ID;
  448. }
  449. expected |= TLVFLAG_IPV4_ADDR;
  450. expected |= TLVFLAG_IPV4_INT_REACHABILITY;
  451. expected |= TLVFLAG_IPV4_EXT_REACHABILITY;
  452. #ifdef HAVE_IPV6
  453. expected |= TLVFLAG_IPV6_ADDR;
  454. expected |= TLVFLAG_IPV6_REACHABILITY;
  455. #endif /* HAVE_IPV6 */
  456. retval = parse_tlvs (area->area_tag, STREAM_DATA (lsp->pdu) +
  457. ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN,
  458. ntohs (lsp->lsp_header->pdu_len) -
  459. ISIS_FIXED_HDR_LEN - ISIS_LSP_HDR_LEN,
  460. &expected, &found, &lsp->tlv_data,
  461. NULL);
  462. if (retval != ISIS_OK)
  463. {
  464. zlog_warn ("Could not parse LSP");
  465. return;
  466. }
  467. if ((found & TLVFLAG_DYN_HOSTNAME) && (area->dynhostname))
  468. {
  469. isis_dynhn_insert (lsp->lsp_header->lsp_id, lsp->tlv_data.hostname,
  470. (lsp->lsp_header->lsp_bits & LSPBIT_IST) ==
  471. IS_LEVEL_1_AND_2 ? IS_LEVEL_2 : IS_LEVEL_1);
  472. }
  473. return;
  474. }
  475. void
  476. lsp_update (struct isis_lsp *lsp, struct stream *stream,
  477. struct isis_area *area, int level)
  478. {
  479. dnode_t *dnode = NULL;
  480. /* Remove old LSP from database. This is required since the
  481. * lsp_update_data will free the lsp->pdu (which has the key, lsp_id)
  482. * and will update it with the new data in the stream. */
  483. dnode = dict_lookup (area->lspdb[level - 1], lsp->lsp_header->lsp_id);
  484. if (dnode)
  485. dnode_destroy (dict_delete (area->lspdb[level - 1], dnode));
  486. /* rebuild the lsp data */
  487. lsp_update_data (lsp, stream, area, level);
  488. /* insert the lsp back into the database */
  489. lsp_insert (lsp, area->lspdb[level - 1]);
  490. }
  491. /* creation of LSP directly from what we received */
  492. struct isis_lsp *
  493. lsp_new_from_stream_ptr (struct stream *stream,
  494. u_int16_t pdu_len, struct isis_lsp *lsp0,
  495. struct isis_area *area, int level)
  496. {
  497. struct isis_lsp *lsp;
  498. lsp = XCALLOC (MTYPE_ISIS_LSP, sizeof (struct isis_lsp));
  499. lsp_update_data (lsp, stream, area, level);
  500. if (lsp0 == NULL)
  501. {
  502. /*
  503. * zero lsp -> create the list for fragments
  504. */
  505. lsp->lspu.frags = list_new ();
  506. }
  507. else
  508. {
  509. /*
  510. * a fragment -> set the backpointer and add this to zero lsps frag list
  511. */
  512. lsp->lspu.zero_lsp = lsp0;
  513. listnode_add (lsp0->lspu.frags, lsp);
  514. }
  515. return lsp;
  516. }
  517. struct isis_lsp *
  518. lsp_new(struct isis_area *area, u_char * lsp_id,
  519. u_int16_t rem_lifetime, u_int32_t seq_num,
  520. u_int8_t lsp_bits, u_int16_t checksum, int level)
  521. {
  522. struct isis_lsp *lsp;
  523. lsp = XCALLOC (MTYPE_ISIS_LSP, sizeof (struct isis_lsp));
  524. lsp->area = area;
  525. lsp->pdu = stream_new(LLC_LEN + area->lsp_mtu);
  526. if (LSP_FRAGMENT (lsp_id) == 0)
  527. lsp->lspu.frags = list_new ();
  528. lsp->isis_header = (struct isis_fixed_hdr *) (STREAM_DATA (lsp->pdu));
  529. lsp->lsp_header = (struct isis_link_state_hdr *)
  530. (STREAM_DATA (lsp->pdu) + ISIS_FIXED_HDR_LEN);
  531. /* at first we fill the FIXED HEADER */
  532. (level == IS_LEVEL_1) ? fill_fixed_hdr (lsp->isis_header, L1_LINK_STATE) :
  533. fill_fixed_hdr (lsp->isis_header, L2_LINK_STATE);
  534. /* now for the LSP HEADER */
  535. /* Minimal LSP PDU size */
  536. lsp->lsp_header->pdu_len = htons (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
  537. memcpy (lsp->lsp_header->lsp_id, lsp_id, ISIS_SYS_ID_LEN + 2);
  538. lsp->lsp_header->checksum = checksum; /* Provided in network order */
  539. lsp->lsp_header->seq_num = htonl (seq_num);
  540. lsp->lsp_header->rem_lifetime = htons (rem_lifetime);
  541. lsp->lsp_header->lsp_bits = lsp_bits;
  542. lsp->level = level;
  543. lsp->age_out = ZERO_AGE_LIFETIME;
  544. stream_forward_endp (lsp->pdu, ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
  545. if (isis->debugs & DEBUG_EVENTS)
  546. zlog_debug ("New LSP with ID %s-%02x-%02x len %d seqnum %08x",
  547. sysid_print (lsp_id), LSP_PSEUDO_ID (lsp->lsp_header->lsp_id),
  548. LSP_FRAGMENT (lsp->lsp_header->lsp_id),
  549. ntohl (lsp->lsp_header->pdu_len),
  550. ntohl (lsp->lsp_header->seq_num));
  551. return lsp;
  552. }
  553. void
  554. lsp_insert (struct isis_lsp *lsp, dict_t * lspdb)
  555. {
  556. dict_alloc_insert (lspdb, lsp->lsp_header->lsp_id, lsp);
  557. if (lsp->lsp_header->seq_num != 0)
  558. {
  559. isis_spf_schedule (lsp->area, lsp->level);
  560. #ifdef HAVE_IPV6
  561. isis_spf_schedule6 (lsp->area, lsp->level);
  562. #endif
  563. }
  564. }
  565. /*
  566. * Build a list of LSPs with non-zero ht bounded by start and stop ids
  567. */
  568. void
  569. lsp_build_list_nonzero_ht (u_char * start_id, u_char * stop_id,
  570. struct list *list, dict_t * lspdb)
  571. {
  572. dnode_t *first, *last, *curr;
  573. first = dict_lower_bound (lspdb, start_id);
  574. if (!first)
  575. return;
  576. last = dict_upper_bound (lspdb, stop_id);
  577. curr = first;
  578. if (((struct isis_lsp *) (curr->dict_data))->lsp_header->rem_lifetime)
  579. listnode_add (list, first->dict_data);
  580. while (curr)
  581. {
  582. curr = dict_next (lspdb, curr);
  583. if (curr &&
  584. ((struct isis_lsp *) (curr->dict_data))->lsp_header->rem_lifetime)
  585. listnode_add (list, curr->dict_data);
  586. if (curr == last)
  587. break;
  588. }
  589. return;
  590. }
  591. /*
  592. * Build a list of num_lsps LSPs bounded by start_id and stop_id.
  593. */
  594. void
  595. lsp_build_list (u_char * start_id, u_char * stop_id, u_char num_lsps,
  596. struct list *list, dict_t * lspdb)
  597. {
  598. u_char count;
  599. dnode_t *first, *last, *curr;
  600. first = dict_lower_bound (lspdb, start_id);
  601. if (!first)
  602. return;
  603. last = dict_upper_bound (lspdb, stop_id);
  604. curr = first;
  605. listnode_add (list, first->dict_data);
  606. count = 1;
  607. while (curr)
  608. {
  609. curr = dict_next (lspdb, curr);
  610. if (curr)
  611. {
  612. listnode_add (list, curr->dict_data);
  613. count++;
  614. }
  615. if (count == num_lsps || curr == last)
  616. break;
  617. }
  618. return;
  619. }
  620. /*
  621. * Build a list of LSPs with SSN flag set for the given circuit
  622. */
  623. void
  624. lsp_build_list_ssn (struct isis_circuit *circuit, u_char num_lsps,
  625. struct list *list, dict_t * lspdb)
  626. {
  627. dnode_t *dnode, *next;
  628. struct isis_lsp *lsp;
  629. u_char count = 0;
  630. dnode = dict_first (lspdb);
  631. while (dnode != NULL)
  632. {
  633. next = dict_next (lspdb, dnode);
  634. lsp = dnode_get (dnode);
  635. if (ISIS_CHECK_FLAG (lsp->SSNflags, circuit))
  636. {
  637. listnode_add (list, lsp);
  638. ++count;
  639. }
  640. if (count == num_lsps)
  641. break;
  642. dnode = next;
  643. }
  644. return;
  645. }
  646. static void
  647. lsp_set_time (struct isis_lsp *lsp)
  648. {
  649. assert (lsp);
  650. if (lsp->lsp_header->rem_lifetime == 0)
  651. {
  652. if (lsp->age_out > 0)
  653. lsp->age_out--;
  654. return;
  655. }
  656. lsp->lsp_header->rem_lifetime =
  657. htons (ntohs (lsp->lsp_header->rem_lifetime) - 1);
  658. }
  659. static void
  660. lspid_print (u_char * lsp_id, u_char * trg, char dynhost, char frag)
  661. {
  662. struct isis_dynhn *dyn = NULL;
  663. u_char id[SYSID_STRLEN];
  664. if (dynhost)
  665. dyn = dynhn_find_by_id (lsp_id);
  666. else
  667. dyn = NULL;
  668. if (dyn)
  669. sprintf ((char *)id, "%.14s", dyn->name.name);
  670. else if (!memcmp (isis->sysid, lsp_id, ISIS_SYS_ID_LEN) && dynhost)
  671. sprintf ((char *)id, "%.14s", unix_hostname ());
  672. else
  673. memcpy (id, sysid_print (lsp_id), 15);
  674. if (frag)
  675. sprintf ((char *)trg, "%s.%02x-%02x", id, LSP_PSEUDO_ID (lsp_id),
  676. LSP_FRAGMENT (lsp_id));
  677. else
  678. sprintf ((char *)trg, "%s.%02x", id, LSP_PSEUDO_ID (lsp_id));
  679. }
  680. /* Convert the lsp attribute bits to attribute string */
  681. const char *
  682. lsp_bits2string (u_char * lsp_bits)
  683. {
  684. char *pos = lsp_bits_string;
  685. if (!*lsp_bits)
  686. return " none";
  687. /* we only focus on the default metric */
  688. pos += sprintf (pos, "%d/",
  689. ISIS_MASK_LSP_ATT_DEFAULT_BIT (*lsp_bits) ? 1 : 0);
  690. pos += sprintf (pos, "%d/",
  691. ISIS_MASK_LSP_PARTITION_BIT (*lsp_bits) ? 1 : 0);
  692. pos += sprintf (pos, "%d", ISIS_MASK_LSP_OL_BIT (*lsp_bits) ? 1 : 0);
  693. *(pos) = '\0';
  694. return lsp_bits_string;
  695. }
  696. /* this function prints the lsp on show isis database */
  697. void
  698. lsp_print (struct isis_lsp *lsp, struct vty *vty, char dynhost)
  699. {
  700. u_char LSPid[255];
  701. char age_out[8];
  702. lspid_print (lsp->lsp_header->lsp_id, LSPid, dynhost, 1);
  703. vty_out (vty, "%-21s%c ", LSPid, lsp->own_lsp ? '*' : ' ');
  704. vty_out (vty, "%5u ", ntohs (lsp->lsp_header->pdu_len));
  705. vty_out (vty, "0x%08x ", ntohl (lsp->lsp_header->seq_num));
  706. vty_out (vty, "0x%04x ", ntohs (lsp->lsp_header->checksum));
  707. if (ntohs (lsp->lsp_header->rem_lifetime) == 0)
  708. {
  709. snprintf (age_out, 8, "(%u)", lsp->age_out);
  710. age_out[7] = '\0';
  711. vty_out (vty, "%7s ", age_out);
  712. }
  713. else
  714. vty_out (vty, " %5u ", ntohs (lsp->lsp_header->rem_lifetime));
  715. vty_out (vty, "%s%s",
  716. lsp_bits2string (&lsp->lsp_header->lsp_bits), VTY_NEWLINE);
  717. }
  718. void
  719. lsp_print_detail (struct isis_lsp *lsp, struct vty *vty, char dynhost)
  720. {
  721. struct area_addr *area_addr;
  722. int i;
  723. struct listnode *lnode;
  724. struct is_neigh *is_neigh;
  725. struct te_is_neigh *te_is_neigh;
  726. struct ipv4_reachability *ipv4_reach;
  727. struct in_addr *ipv4_addr;
  728. struct te_ipv4_reachability *te_ipv4_reach;
  729. #ifdef HAVE_IPV6
  730. struct ipv6_reachability *ipv6_reach;
  731. struct in6_addr in6;
  732. u_char buff[BUFSIZ];
  733. #endif
  734. u_char LSPid[255];
  735. u_char hostname[255];
  736. u_char ipv4_reach_prefix[20];
  737. u_char ipv4_reach_mask[20];
  738. u_char ipv4_address[20];
  739. lspid_print (lsp->lsp_header->lsp_id, LSPid, dynhost, 1);
  740. lsp_print (lsp, vty, dynhost);
  741. /* for all area address */
  742. if (lsp->tlv_data.area_addrs)
  743. for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.area_addrs, lnode, area_addr))
  744. {
  745. vty_out (vty, " Area Address: %s%s",
  746. isonet_print (area_addr->area_addr, area_addr->addr_len),
  747. VTY_NEWLINE);
  748. }
  749. /* for the nlpid tlv */
  750. if (lsp->tlv_data.nlpids)
  751. {
  752. for (i = 0; i < lsp->tlv_data.nlpids->count; i++)
  753. {
  754. switch (lsp->tlv_data.nlpids->nlpids[i])
  755. {
  756. case NLPID_IP:
  757. case NLPID_IPV6:
  758. vty_out (vty, " NLPID : 0x%X%s",
  759. lsp->tlv_data.nlpids->nlpids[i], VTY_NEWLINE);
  760. break;
  761. default:
  762. vty_out (vty, " NLPID : %s%s", "unknown", VTY_NEWLINE);
  763. break;
  764. }
  765. }
  766. }
  767. /* for the hostname tlv */
  768. if (lsp->tlv_data.hostname)
  769. {
  770. bzero (hostname, sizeof (hostname));
  771. memcpy (hostname, lsp->tlv_data.hostname->name,
  772. lsp->tlv_data.hostname->namelen);
  773. vty_out (vty, " Hostname : %s%s", hostname, VTY_NEWLINE);
  774. }
  775. /* authentication tlv */
  776. if (lsp->tlv_data.auth_info.type != ISIS_PASSWD_TYPE_UNUSED)
  777. {
  778. if (lsp->tlv_data.auth_info.type == ISIS_PASSWD_TYPE_HMAC_MD5)
  779. vty_out (vty, " Auth type : md5%s", VTY_NEWLINE);
  780. else if (lsp->tlv_data.auth_info.type == ISIS_PASSWD_TYPE_CLEARTXT)
  781. vty_out (vty, " Auth type : clear text%s", VTY_NEWLINE);
  782. }
  783. /* TE router id */
  784. if (lsp->tlv_data.router_id)
  785. {
  786. memcpy (ipv4_address, inet_ntoa (lsp->tlv_data.router_id->id),
  787. sizeof (ipv4_address));
  788. vty_out (vty, " Router ID : %s%s", ipv4_address, VTY_NEWLINE);
  789. }
  790. if (lsp->tlv_data.ipv4_addrs)
  791. for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.ipv4_addrs, lnode, ipv4_addr))
  792. {
  793. memcpy (ipv4_address, inet_ntoa (*ipv4_addr), sizeof (ipv4_address));
  794. vty_out (vty, " IPv4 Address: %s%s", ipv4_address, VTY_NEWLINE);
  795. }
  796. /* for the IS neighbor tlv */
  797. if (lsp->tlv_data.is_neighs)
  798. for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.is_neighs, lnode, is_neigh))
  799. {
  800. lspid_print (is_neigh->neigh_id, LSPid, dynhost, 0);
  801. vty_out (vty, " Metric : %-8d IS : %s%s",
  802. is_neigh->metrics.metric_default, LSPid, VTY_NEWLINE);
  803. }
  804. /* for the internal reachable tlv */
  805. if (lsp->tlv_data.ipv4_int_reachs)
  806. for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.ipv4_int_reachs, lnode,
  807. ipv4_reach))
  808. {
  809. memcpy (ipv4_reach_prefix, inet_ntoa (ipv4_reach->prefix),
  810. sizeof (ipv4_reach_prefix));
  811. memcpy (ipv4_reach_mask, inet_ntoa (ipv4_reach->mask),
  812. sizeof (ipv4_reach_mask));
  813. vty_out (vty, " Metric : %-8d IPv4-Internal : %s %s%s",
  814. ipv4_reach->metrics.metric_default, ipv4_reach_prefix,
  815. ipv4_reach_mask, VTY_NEWLINE);
  816. }
  817. /* for the external reachable tlv */
  818. if (lsp->tlv_data.ipv4_ext_reachs)
  819. for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.ipv4_ext_reachs, lnode,
  820. ipv4_reach))
  821. {
  822. memcpy (ipv4_reach_prefix, inet_ntoa (ipv4_reach->prefix),
  823. sizeof (ipv4_reach_prefix));
  824. memcpy (ipv4_reach_mask, inet_ntoa (ipv4_reach->mask),
  825. sizeof (ipv4_reach_mask));
  826. vty_out (vty, " Metric : %-8d IPv4-External : %s %s%s",
  827. ipv4_reach->metrics.metric_default, ipv4_reach_prefix,
  828. ipv4_reach_mask, VTY_NEWLINE);
  829. }
  830. /* IPv6 tlv */
  831. #ifdef HAVE_IPV6
  832. if (lsp->tlv_data.ipv6_reachs)
  833. for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.ipv6_reachs, lnode, ipv6_reach))
  834. {
  835. memset (&in6, 0, sizeof (in6));
  836. memcpy (in6.s6_addr, ipv6_reach->prefix,
  837. PSIZE (ipv6_reach->prefix_len));
  838. inet_ntop (AF_INET6, &in6, (char *)buff, BUFSIZ);
  839. if ((ipv6_reach->control_info &
  840. CTRL_INFO_DISTRIBUTION) == DISTRIBUTION_INTERNAL)
  841. vty_out (vty, " Metric : %-8d IPv6-Internal : %s/%d%s",
  842. ntohl (ipv6_reach->metric),
  843. buff, ipv6_reach->prefix_len, VTY_NEWLINE);
  844. else
  845. vty_out (vty, " Metric : %-8d IPv6-External : %s/%d%s",
  846. ntohl (ipv6_reach->metric),
  847. buff, ipv6_reach->prefix_len, VTY_NEWLINE);
  848. }
  849. #endif
  850. /* TE IS neighbor tlv */
  851. if (lsp->tlv_data.te_is_neighs)
  852. for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.te_is_neighs, lnode, te_is_neigh))
  853. {
  854. lspid_print (te_is_neigh->neigh_id, LSPid, dynhost, 0);
  855. vty_out (vty, " Metric : %-8d IS-Extended : %s%s",
  856. GET_TE_METRIC(te_is_neigh), LSPid, VTY_NEWLINE);
  857. if (IS_MPLS_TE(isisMplsTE))
  858. mpls_te_print_detail(vty, te_is_neigh);
  859. }
  860. /* TE IPv4 tlv */
  861. if (lsp->tlv_data.te_ipv4_reachs)
  862. for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.te_ipv4_reachs, lnode,
  863. te_ipv4_reach))
  864. {
  865. /* FIXME: There should be better way to output this stuff. */
  866. vty_out (vty, " Metric : %-8d IPv4-Extended : %s/%d%s",
  867. ntohl (te_ipv4_reach->te_metric),
  868. inet_ntoa (newprefix2inaddr (&te_ipv4_reach->prefix_start,
  869. te_ipv4_reach->control)),
  870. te_ipv4_reach->control & 0x3F, VTY_NEWLINE);
  871. }
  872. vty_out (vty, "%s", VTY_NEWLINE);
  873. return;
  874. }
  875. /* print all the lsps info in the local lspdb */
  876. int
  877. lsp_print_all (struct vty *vty, dict_t * lspdb, char detail, char dynhost)
  878. {
  879. dnode_t *node = dict_first (lspdb), *next;
  880. int lsp_count = 0;
  881. if (detail == ISIS_UI_LEVEL_BRIEF)
  882. {
  883. while (node != NULL)
  884. {
  885. /* I think it is unnecessary, so I comment it out */
  886. /* dict_contains (lspdb, node); */
  887. next = dict_next (lspdb, node);
  888. lsp_print (dnode_get (node), vty, dynhost);
  889. node = next;
  890. lsp_count++;
  891. }
  892. }
  893. else if (detail == ISIS_UI_LEVEL_DETAIL)
  894. {
  895. while (node != NULL)
  896. {
  897. next = dict_next (lspdb, node);
  898. lsp_print_detail (dnode_get (node), vty, dynhost);
  899. node = next;
  900. lsp_count++;
  901. }
  902. }
  903. return lsp_count;
  904. }
  905. #define FRAG_THOLD(S,T) \
  906. ((STREAM_SIZE(S)*T)/100)
  907. /* stream*, area->lsp_frag_threshold, increment */
  908. #define FRAG_NEEDED(S,T,I) \
  909. (STREAM_SIZE(S)-STREAM_REMAIN(S)+(I) > FRAG_THOLD(S,T))
  910. /* FIXME: It shouldn't be necessary to pass tlvsize here, TLVs can have
  911. * variable length (TE TLVs, sub TLVs). */
  912. static void
  913. lsp_tlv_fit (struct isis_lsp *lsp, struct list **from, struct list **to,
  914. int tlvsize, int frag_thold,
  915. int tlv_build_func (struct list *, struct stream *))
  916. {
  917. int count, i;
  918. /* can we fit all ? */
  919. if (!FRAG_NEEDED (lsp->pdu, frag_thold, listcount (*from) * tlvsize + 2))
  920. {
  921. tlv_build_func (*from, lsp->pdu);
  922. if (listcount (*to) != 0)
  923. {
  924. struct listnode *node, *nextnode;
  925. void *elem;
  926. for (ALL_LIST_ELEMENTS (*from, node, nextnode, elem))
  927. {
  928. listnode_add (*to, elem);
  929. list_delete_node (*from, node);
  930. }
  931. }
  932. else
  933. {
  934. list_free (*to);
  935. *to = *from;
  936. *from = NULL;
  937. }
  938. }
  939. else if (!FRAG_NEEDED (lsp->pdu, frag_thold, tlvsize + 2))
  940. {
  941. /* fit all we can */
  942. count = FRAG_THOLD (lsp->pdu, frag_thold) - 2 -
  943. (STREAM_SIZE (lsp->pdu) - STREAM_REMAIN (lsp->pdu));
  944. count = count / tlvsize;
  945. if (count > (int)listcount (*from))
  946. count = listcount (*from);
  947. for (i = 0; i < count; i++)
  948. {
  949. listnode_add (*to, listgetdata (listhead (*from)));
  950. listnode_delete (*from, listgetdata (listhead (*from)));
  951. }
  952. tlv_build_func (*to, lsp->pdu);
  953. }
  954. lsp->lsp_header->pdu_len = htons (stream_get_endp (lsp->pdu));
  955. return;
  956. }
  957. /* Process IS_NEIGHBOURS TLV with TE subTLVs */
  958. static void
  959. lsp_te_tlv_fit (struct isis_lsp *lsp, struct list **from, struct list **to, int frag_thold)
  960. {
  961. int count, size = 0;
  962. struct listnode *node, *nextnode;
  963. struct te_is_neigh *elem;
  964. /* Start computing real size of TLVs */
  965. for (ALL_LIST_ELEMENTS (*from, node, nextnode, elem))
  966. size = size + elem->sub_tlvs_length + IS_NEIGHBOURS_LEN;
  967. /* can we fit all ? */
  968. if (!FRAG_NEEDED (lsp->pdu, frag_thold, size))
  969. {
  970. tlv_add_te_is_neighs (*from, lsp->pdu);
  971. if (listcount (*to) != 0)
  972. {
  973. for (ALL_LIST_ELEMENTS (*from, node, nextnode, elem))
  974. {
  975. listnode_add (*to, elem);
  976. list_delete_node (*from, node);
  977. }
  978. }
  979. else
  980. {
  981. list_free (*to);
  982. *to = *from;
  983. *from = NULL;
  984. }
  985. }
  986. else
  987. {
  988. /* fit all we can */
  989. /* Compute remaining place in LSP PDU */
  990. count = FRAG_THOLD (lsp->pdu, frag_thold) - 2 -
  991. (STREAM_SIZE (lsp->pdu) - STREAM_REMAIN (lsp->pdu));
  992. /* Determine size of TE SubTLVs */
  993. elem = (struct te_is_neigh *)listgetdata ((struct listnode *)listhead (*from));
  994. count = count - elem->sub_tlvs_length - IS_NEIGHBOURS_LEN;
  995. if (count > 0)
  996. {
  997. while (count > 0)
  998. {
  999. listnode_add (*to, listgetdata ((struct listnode *)listhead (*from)));
  1000. listnode_delete (*from, listgetdata ((struct listnode *)listhead (*from)));
  1001. elem = (struct te_is_neigh *)listgetdata ((struct listnode *)listhead (*from));
  1002. count = count - elem->sub_tlvs_length - IS_NEIGHBOURS_LEN;
  1003. }
  1004. tlv_add_te_is_neighs (*to, lsp->pdu);
  1005. }
  1006. }
  1007. lsp->lsp_header->pdu_len = htons (stream_get_endp (lsp->pdu));
  1008. return;
  1009. }
  1010. static u_int16_t
  1011. lsp_rem_lifetime (struct isis_area *area, int level)
  1012. {
  1013. u_int16_t rem_lifetime;
  1014. /* Add jitter to configured LSP lifetime */
  1015. rem_lifetime = isis_jitter (area->max_lsp_lifetime[level - 1],
  1016. MAX_AGE_JITTER);
  1017. /* No jitter if the max refresh will be less than configure gen interval */
  1018. /* N.B. this calucation is acceptable since rem_lifetime is in [332,65535] at
  1019. * this point */
  1020. if (area->lsp_gen_interval[level - 1] > (rem_lifetime - 300))
  1021. rem_lifetime = area->max_lsp_lifetime[level - 1];
  1022. return rem_lifetime;
  1023. }
  1024. static u_int16_t
  1025. lsp_refresh_time (struct isis_lsp *lsp, u_int16_t rem_lifetime)
  1026. {
  1027. struct isis_area *area = lsp->area;
  1028. int level = lsp->level;
  1029. u_int16_t refresh_time;
  1030. /* Add jitter to LSP refresh time */
  1031. refresh_time = isis_jitter (area->lsp_refresh[level - 1],
  1032. MAX_LSP_GEN_JITTER);
  1033. /* RFC 4444 : make sure the refresh time is at least less than 300
  1034. * of the remaining lifetime and more than gen interval */
  1035. if (refresh_time <= area->lsp_gen_interval[level - 1] ||
  1036. refresh_time > (rem_lifetime - 300))
  1037. refresh_time = rem_lifetime - 300;
  1038. /* In cornercases, refresh_time might be <= lsp_gen_interval, however
  1039. * we accept this violation to satisfy refresh_time <= rem_lifetime - 300 */
  1040. return refresh_time;
  1041. }
  1042. static struct isis_lsp *
  1043. lsp_next_frag (u_char frag_num, struct isis_lsp *lsp0, struct isis_area *area,
  1044. int level)
  1045. {
  1046. struct isis_lsp *lsp;
  1047. u_char frag_id[ISIS_SYS_ID_LEN + 2];
  1048. memcpy (frag_id, lsp0->lsp_header->lsp_id, ISIS_SYS_ID_LEN + 1);
  1049. LSP_FRAGMENT (frag_id) = frag_num;
  1050. /* FIXME add authentication TLV for fragment LSPs */
  1051. lsp = lsp_search (frag_id, area->lspdb[level - 1]);
  1052. if (lsp)
  1053. {
  1054. /* Clear the TLVs */
  1055. lsp_clear_data (lsp);
  1056. return lsp;
  1057. }
  1058. lsp = lsp_new (area, frag_id, ntohs(lsp0->lsp_header->rem_lifetime), 0,
  1059. lsp_bits_generate (level, area->overload_bit,
  1060. area->attached_bit), 0, level);
  1061. lsp->area = area;
  1062. lsp->own_lsp = 1;
  1063. lsp_insert (lsp, area->lspdb[level - 1]);
  1064. listnode_add (lsp0->lspu.frags, lsp);
  1065. lsp->lspu.zero_lsp = lsp0;
  1066. return lsp;
  1067. }
  1068. static void
  1069. lsp_build_ext_reach_ipv4(struct isis_lsp *lsp, struct isis_area *area,
  1070. struct tlvs *tlv_data)
  1071. {
  1072. struct route_table *er_table;
  1073. struct route_node *rn;
  1074. struct prefix_ipv4 *ipv4;
  1075. struct isis_ext_info *info;
  1076. struct ipv4_reachability *ipreach;
  1077. struct te_ipv4_reachability *te_ipreach;
  1078. er_table = get_ext_reach(area, AF_INET, lsp->level);
  1079. if (!er_table)
  1080. return;
  1081. for (rn = route_top(er_table); rn; rn = route_next(rn))
  1082. {
  1083. if (!rn->info)
  1084. continue;
  1085. ipv4 = (struct prefix_ipv4*)&rn->p;
  1086. info = rn->info;
  1087. if (area->oldmetric)
  1088. {
  1089. if (tlv_data->ipv4_ext_reachs == NULL)
  1090. {
  1091. tlv_data->ipv4_ext_reachs = list_new();
  1092. tlv_data->ipv4_ext_reachs->del = free_tlv;
  1093. }
  1094. ipreach = XMALLOC(MTYPE_ISIS_TLV, sizeof(*ipreach));
  1095. ipreach->prefix.s_addr = ipv4->prefix.s_addr;
  1096. masklen2ip(ipv4->prefixlen, &ipreach->mask);
  1097. ipreach->prefix.s_addr &= ipreach->mask.s_addr;
  1098. if ((info->metric & 0x3f) != info->metric)
  1099. ipreach->metrics.metric_default = 0x3f;
  1100. else
  1101. ipreach->metrics.metric_default = info->metric;
  1102. ipreach->metrics.metric_expense = METRICS_UNSUPPORTED;
  1103. ipreach->metrics.metric_error = METRICS_UNSUPPORTED;
  1104. ipreach->metrics.metric_delay = METRICS_UNSUPPORTED;
  1105. listnode_add(tlv_data->ipv4_ext_reachs, ipreach);
  1106. }
  1107. if (area->newmetric)
  1108. {
  1109. if (tlv_data->te_ipv4_reachs == NULL)
  1110. {
  1111. tlv_data->te_ipv4_reachs = list_new();
  1112. tlv_data->te_ipv4_reachs->del = free_tlv;
  1113. }
  1114. te_ipreach =
  1115. XCALLOC(MTYPE_ISIS_TLV,
  1116. sizeof(*te_ipreach) - 1 + PSIZE(ipv4->prefixlen));
  1117. if (info->metric > MAX_WIDE_PATH_METRIC)
  1118. te_ipreach->te_metric = htonl(MAX_WIDE_PATH_METRIC);
  1119. else
  1120. te_ipreach->te_metric = htonl(info->metric);
  1121. te_ipreach->control = ipv4->prefixlen & 0x3f;
  1122. memcpy(&te_ipreach->prefix_start, &ipv4->prefix.s_addr,
  1123. PSIZE(ipv4->prefixlen));
  1124. listnode_add(tlv_data->te_ipv4_reachs, te_ipreach);
  1125. }
  1126. }
  1127. }
  1128. static void
  1129. lsp_build_ext_reach_ipv6(struct isis_lsp *lsp, struct isis_area *area,
  1130. struct tlvs *tlv_data)
  1131. {
  1132. struct route_table *er_table;
  1133. struct route_node *rn;
  1134. struct prefix_ipv6 *ipv6;
  1135. struct isis_ext_info *info;
  1136. struct ipv6_reachability *ip6reach;
  1137. er_table = get_ext_reach(area, AF_INET6, lsp->level);
  1138. if (!er_table)
  1139. return;
  1140. for (rn = route_top(er_table); rn; rn = route_next(rn))
  1141. {
  1142. if (!rn->info)
  1143. continue;
  1144. ipv6 = (struct prefix_ipv6*)&rn->p;
  1145. info = rn->info;
  1146. if (tlv_data->ipv6_reachs == NULL)
  1147. {
  1148. tlv_data->ipv6_reachs = list_new();
  1149. tlv_data->ipv6_reachs->del = free_tlv;
  1150. }
  1151. ip6reach = XCALLOC(MTYPE_ISIS_TLV, sizeof(*ip6reach));
  1152. if (info->metric > MAX_WIDE_PATH_METRIC)
  1153. ip6reach->metric = htonl(MAX_WIDE_PATH_METRIC);
  1154. else
  1155. ip6reach->metric = htonl(info->metric);
  1156. ip6reach->control_info = DISTRIBUTION_EXTERNAL;
  1157. ip6reach->prefix_len = ipv6->prefixlen;
  1158. memcpy(ip6reach->prefix, ipv6->prefix.s6_addr, sizeof(ip6reach->prefix));
  1159. listnode_add(tlv_data->ipv6_reachs, ip6reach);
  1160. }
  1161. }
  1162. static void
  1163. lsp_build_ext_reach (struct isis_lsp *lsp, struct isis_area *area,
  1164. struct tlvs *tlv_data)
  1165. {
  1166. lsp_build_ext_reach_ipv4(lsp, area, tlv_data);
  1167. lsp_build_ext_reach_ipv6(lsp, area, tlv_data);
  1168. }
  1169. /*
  1170. * Builds the LSP data part. This func creates a new frag whenever
  1171. * area->lsp_frag_threshold is exceeded.
  1172. */
  1173. static void
  1174. lsp_build (struct isis_lsp *lsp, struct isis_area *area)
  1175. {
  1176. struct is_neigh *is_neigh;
  1177. struct te_is_neigh *te_is_neigh;
  1178. struct listnode *node, *ipnode;
  1179. int level = lsp->level;
  1180. struct isis_circuit *circuit;
  1181. struct prefix_ipv4 *ipv4;
  1182. struct ipv4_reachability *ipreach;
  1183. struct te_ipv4_reachability *te_ipreach;
  1184. struct isis_adjacency *nei;
  1185. #ifdef HAVE_IPV6
  1186. struct prefix_ipv6 *ipv6, ip6prefix;
  1187. struct ipv6_reachability *ip6reach;
  1188. #endif /* HAVE_IPV6 */
  1189. struct tlvs tlv_data;
  1190. struct isis_lsp *lsp0 = lsp;
  1191. struct in_addr *routerid;
  1192. uint32_t expected = 0, found = 0;
  1193. uint32_t metric;
  1194. u_char zero_id[ISIS_SYS_ID_LEN + 1];
  1195. int retval = ISIS_OK;
  1196. char buf[BUFSIZ];
  1197. lsp_debug("ISIS (%s): Constructing local system LSP for level %d", area->area_tag, level);
  1198. /*
  1199. * Building the zero lsp
  1200. */
  1201. memset (zero_id, 0, ISIS_SYS_ID_LEN + 1);
  1202. /* Reset stream endp. Stream is always there and on every LSP refresh only
  1203. * TLV part of it is overwritten. So we must seek past header we will not
  1204. * touch. */
  1205. stream_reset (lsp->pdu);
  1206. stream_forward_endp (lsp->pdu, ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
  1207. /*
  1208. * Add the authentication info if its present
  1209. */
  1210. lsp_auth_add (lsp);
  1211. /*
  1212. * First add the tlvs related to area
  1213. */
  1214. /* Area addresses */
  1215. if (lsp->tlv_data.area_addrs == NULL)
  1216. lsp->tlv_data.area_addrs = list_new ();
  1217. list_add_list (lsp->tlv_data.area_addrs, area->area_addrs);
  1218. if (listcount (lsp->tlv_data.area_addrs) > 0)
  1219. tlv_add_area_addrs (lsp->tlv_data.area_addrs, lsp->pdu);
  1220. /* Protocols Supported */
  1221. if (area->ip_circuits > 0
  1222. #ifdef HAVE_IPV6
  1223. || area->ipv6_circuits > 0
  1224. #endif /* HAVE_IPV6 */
  1225. )
  1226. {
  1227. lsp->tlv_data.nlpids = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct nlpids));
  1228. lsp->tlv_data.nlpids->count = 0;
  1229. if (area->ip_circuits > 0)
  1230. {
  1231. lsp_debug("ISIS (%s): Found IPv4 circuit, adding IPv4 to NLPIDs", area->area_tag);
  1232. lsp->tlv_data.nlpids->count++;
  1233. lsp->tlv_data.nlpids->nlpids[0] = NLPID_IP;
  1234. }
  1235. #ifdef HAVE_IPV6
  1236. if (area->ipv6_circuits > 0)
  1237. {
  1238. lsp_debug("ISIS (%s): Found IPv6 circuit, adding IPv6 to NLPIDs", area->area_tag);
  1239. lsp->tlv_data.nlpids->count++;
  1240. lsp->tlv_data.nlpids->nlpids[lsp->tlv_data.nlpids->count - 1] =
  1241. NLPID_IPV6;
  1242. }
  1243. #endif /* HAVE_IPV6 */
  1244. tlv_add_nlpid (lsp->tlv_data.nlpids, lsp->pdu);
  1245. }
  1246. /* Dynamic Hostname */
  1247. if (area->dynhostname)
  1248. {
  1249. const char *hostname = unix_hostname();
  1250. size_t hostname_len = strlen(hostname);
  1251. lsp->tlv_data.hostname = XMALLOC (MTYPE_ISIS_TLV,
  1252. sizeof (struct hostname));
  1253. strncpy((char *)lsp->tlv_data.hostname->name, hostname,
  1254. sizeof(lsp->tlv_data.hostname->name));
  1255. if (hostname_len <= MAX_TLV_LEN)
  1256. lsp->tlv_data.hostname->namelen = hostname_len;
  1257. else
  1258. lsp->tlv_data.hostname->namelen = MAX_TLV_LEN;
  1259. lsp_debug("ISIS (%s): Adding dynamic hostname '%.*s'", area->area_tag,
  1260. lsp->tlv_data.hostname->namelen, lsp->tlv_data.hostname->name);
  1261. tlv_add_dynamic_hostname (lsp->tlv_data.hostname, lsp->pdu);
  1262. }
  1263. else
  1264. {
  1265. lsp_debug("ISIS (%s): Not adding dynamic hostname (disabled)", area->area_tag);
  1266. }
  1267. /* IPv4 address and TE router ID TLVs. In case of the first one we don't
  1268. * follow "C" vendor, but "J" vendor behavior - one IPv4 address is put into
  1269. * LSP and this address is same as router id. */
  1270. if (isis->router_id != 0)
  1271. {
  1272. inet_ntop(AF_INET, &isis->router_id, buf, sizeof(buf));
  1273. lsp_debug("ISIS (%s): Adding router ID %s as IPv4 tlv.", area->area_tag, buf);
  1274. if (lsp->tlv_data.ipv4_addrs == NULL)
  1275. {
  1276. lsp->tlv_data.ipv4_addrs = list_new ();
  1277. lsp->tlv_data.ipv4_addrs->del = free_tlv;
  1278. }
  1279. routerid = XMALLOC (MTYPE_ISIS_TLV, sizeof (struct in_addr));
  1280. routerid->s_addr = isis->router_id;
  1281. listnode_add (lsp->tlv_data.ipv4_addrs, routerid);
  1282. tlv_add_in_addr (routerid, lsp->pdu, IPV4_ADDR);
  1283. /* Exactly same data is put into TE router ID TLV, but only if new style
  1284. * TLV's are in use. */
  1285. if (area->newmetric)
  1286. {
  1287. lsp_debug("ISIS (%s): Adding router ID also as TE router ID tlv.", area->area_tag);
  1288. lsp->tlv_data.router_id = XMALLOC (MTYPE_ISIS_TLV,
  1289. sizeof (struct in_addr));
  1290. lsp->tlv_data.router_id->id.s_addr = isis->router_id;
  1291. tlv_add_in_addr (&lsp->tlv_data.router_id->id, lsp->pdu,
  1292. TE_ROUTER_ID);
  1293. }
  1294. }
  1295. else
  1296. {
  1297. lsp_debug("ISIS (%s): Router ID is unset. Not adding tlv.", area->area_tag);
  1298. }
  1299. memset (&tlv_data, 0, sizeof (struct tlvs));
  1300. #ifdef TOPOLOGY_GENERATE
  1301. /* If topology exists (and we create topology for level 1 only), create
  1302. * (hardcoded) link to topology. */
  1303. if (area->topology && level == IS_LEVEL_1)
  1304. {
  1305. if (tlv_data.is_neighs == NULL)
  1306. {
  1307. tlv_data.is_neighs = list_new ();
  1308. tlv_data.is_neighs->del = free_tlv;
  1309. }
  1310. is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
  1311. memcpy (&is_neigh->neigh_id, area->topology_baseis, ISIS_SYS_ID_LEN);
  1312. is_neigh->neigh_id[ISIS_SYS_ID_LEN - 1] = (1 & 0xFF);
  1313. is_neigh->neigh_id[ISIS_SYS_ID_LEN - 2] = ((1 >> 8) & 0xFF);
  1314. is_neigh->metrics.metric_default = 0x01;
  1315. is_neigh->metrics.metric_delay = METRICS_UNSUPPORTED;
  1316. is_neigh->metrics.metric_expense = METRICS_UNSUPPORTED;
  1317. is_neigh->metrics.metric_error = METRICS_UNSUPPORTED;
  1318. listnode_add (tlv_data.is_neighs, is_neigh);
  1319. }
  1320. #endif /* TOPOLOGY_GENERATE */
  1321. lsp_debug("ISIS (%s): Adding circuit specific information.", area->area_tag);
  1322. /*
  1323. * Then build lists of tlvs related to circuits
  1324. */
  1325. for (ALL_LIST_ELEMENTS_RO (area->circuit_list, node, circuit))
  1326. {
  1327. if (!circuit->interface)
  1328. lsp_debug("ISIS (%s): Processing %s circuit %p with unknown interface",
  1329. area->area_tag, circuit_type2string(circuit->circ_type), circuit);
  1330. else
  1331. lsp_debug("ISIS (%s): Processing %s circuit %s",
  1332. area->area_tag, circuit_type2string(circuit->circ_type), circuit->interface->name);
  1333. if (circuit->state != C_STATE_UP)
  1334. {
  1335. lsp_debug("ISIS (%s): Circuit is not up, ignoring.", area->area_tag);
  1336. continue;
  1337. }
  1338. /*
  1339. * Add IPv4 internal reachability of this circuit
  1340. */
  1341. if (circuit->ip_router && circuit->ip_addrs &&
  1342. circuit->ip_addrs->count > 0)
  1343. {
  1344. lsp_debug("ISIS (%s): Circuit has IPv4 active, adding respective TLVs.", area->area_tag);
  1345. if (area->oldmetric)
  1346. {
  1347. if (tlv_data.ipv4_int_reachs == NULL)
  1348. {
  1349. tlv_data.ipv4_int_reachs = list_new ();
  1350. tlv_data.ipv4_int_reachs->del = free_tlv;
  1351. }
  1352. for (ALL_LIST_ELEMENTS_RO (circuit->ip_addrs, ipnode, ipv4))
  1353. {
  1354. ipreach =
  1355. XMALLOC (MTYPE_ISIS_TLV, sizeof (struct ipv4_reachability));
  1356. ipreach->metrics.metric_default = circuit->metric[level - 1];
  1357. ipreach->metrics.metric_expense = METRICS_UNSUPPORTED;
  1358. ipreach->metrics.metric_error = METRICS_UNSUPPORTED;
  1359. ipreach->metrics.metric_delay = METRICS_UNSUPPORTED;
  1360. masklen2ip (ipv4->prefixlen, &ipreach->mask);
  1361. ipreach->prefix.s_addr = ((ipreach->mask.s_addr) &
  1362. (ipv4->prefix.s_addr));
  1363. inet_ntop(AF_INET, &ipreach->prefix.s_addr, buf, sizeof(buf));
  1364. lsp_debug("ISIS (%s): Adding old-style IP reachability for %s/%d",
  1365. area->area_tag, buf, ipv4->prefixlen);
  1366. listnode_add (tlv_data.ipv4_int_reachs, ipreach);
  1367. }
  1368. }
  1369. if (area->newmetric)
  1370. {
  1371. if (tlv_data.te_ipv4_reachs == NULL)
  1372. {
  1373. tlv_data.te_ipv4_reachs = list_new ();
  1374. tlv_data.te_ipv4_reachs->del = free_tlv;
  1375. }
  1376. for (ALL_LIST_ELEMENTS_RO (circuit->ip_addrs, ipnode, ipv4))
  1377. {
  1378. /* FIXME All this assumes that we have no sub TLVs. */
  1379. te_ipreach = XCALLOC (MTYPE_ISIS_TLV,
  1380. sizeof (struct te_ipv4_reachability) +
  1381. ((ipv4->prefixlen + 7)/8) - 1);
  1382. if (area->oldmetric)
  1383. te_ipreach->te_metric = htonl (circuit->metric[level - 1]);
  1384. else
  1385. te_ipreach->te_metric = htonl (circuit->te_metric[level - 1]);
  1386. te_ipreach->control = (ipv4->prefixlen & 0x3F);
  1387. memcpy (&te_ipreach->prefix_start, &ipv4->prefix.s_addr,
  1388. (ipv4->prefixlen + 7)/8);
  1389. inet_ntop(AF_INET, &ipv4->prefix.s_addr, buf, sizeof(buf));
  1390. lsp_debug("ISIS (%s): Adding te-style IP reachability for %s/%d",
  1391. area->area_tag, buf, ipv4->prefixlen);
  1392. listnode_add (tlv_data.te_ipv4_reachs, te_ipreach);
  1393. }
  1394. }
  1395. }
  1396. #ifdef HAVE_IPV6
  1397. /*
  1398. * Add IPv6 reachability of this circuit
  1399. */
  1400. if (circuit->ipv6_router && circuit->ipv6_non_link &&
  1401. circuit->ipv6_non_link->count > 0)
  1402. {
  1403. if (tlv_data.ipv6_reachs == NULL)
  1404. {
  1405. tlv_data.ipv6_reachs = list_new ();
  1406. tlv_data.ipv6_reachs->del = free_tlv;
  1407. }
  1408. for (ALL_LIST_ELEMENTS_RO (circuit->ipv6_non_link, ipnode, ipv6))
  1409. {
  1410. ip6reach =
  1411. XCALLOC (MTYPE_ISIS_TLV, sizeof (struct ipv6_reachability));
  1412. if (area->oldmetric)
  1413. ip6reach->metric =
  1414. htonl (circuit->metric[level - 1]);
  1415. else
  1416. ip6reach->metric = htonl (circuit->te_metric[level - 1]);
  1417. ip6reach->control_info = 0;
  1418. ip6reach->prefix_len = ipv6->prefixlen;
  1419. memcpy(&ip6prefix, ipv6, sizeof(ip6prefix));
  1420. apply_mask_ipv6(&ip6prefix);
  1421. inet_ntop(AF_INET6, &ip6prefix.prefix.s6_addr, buf, sizeof(buf));
  1422. lsp_debug("ISIS (%s): Adding IPv6 reachability for %s/%d",
  1423. area->area_tag, buf, ipv6->prefixlen);
  1424. memcpy (ip6reach->prefix, ip6prefix.prefix.s6_addr,
  1425. sizeof (ip6reach->prefix));
  1426. listnode_add (tlv_data.ipv6_reachs, ip6reach);
  1427. }
  1428. }
  1429. #endif /* HAVE_IPV6 */
  1430. switch (circuit->circ_type)
  1431. {
  1432. case CIRCUIT_T_BROADCAST:
  1433. if (level & circuit->is_type)
  1434. {
  1435. if (area->oldmetric)
  1436. {
  1437. if (tlv_data.is_neighs == NULL)
  1438. {
  1439. tlv_data.is_neighs = list_new ();
  1440. tlv_data.is_neighs->del = free_tlv;
  1441. }
  1442. is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
  1443. if (level == IS_LEVEL_1)
  1444. memcpy (is_neigh->neigh_id,
  1445. circuit->u.bc.l1_desig_is, ISIS_SYS_ID_LEN + 1);
  1446. else
  1447. memcpy (is_neigh->neigh_id,
  1448. circuit->u.bc.l2_desig_is, ISIS_SYS_ID_LEN + 1);
  1449. is_neigh->metrics.metric_default = circuit->metric[level - 1];
  1450. is_neigh->metrics.metric_expense = METRICS_UNSUPPORTED;
  1451. is_neigh->metrics.metric_error = METRICS_UNSUPPORTED;
  1452. is_neigh->metrics.metric_delay = METRICS_UNSUPPORTED;
  1453. if (!memcmp (is_neigh->neigh_id, zero_id,
  1454. ISIS_SYS_ID_LEN + 1))
  1455. {
  1456. XFREE (MTYPE_ISIS_TLV, is_neigh);
  1457. lsp_debug("ISIS (%s): No DIS for circuit, not adding old-style IS neighbor.",
  1458. area->area_tag);
  1459. }
  1460. else
  1461. {
  1462. listnode_add (tlv_data.is_neighs, is_neigh);
  1463. lsp_debug("ISIS (%s): Adding DIS %s.%02x as old-style neighbor",
  1464. area->area_tag, sysid_print(is_neigh->neigh_id),
  1465. LSP_PSEUDO_ID(is_neigh->neigh_id));
  1466. }
  1467. }
  1468. if (area->newmetric)
  1469. {
  1470. if (tlv_data.te_is_neighs == NULL)
  1471. {
  1472. tlv_data.te_is_neighs = list_new ();
  1473. tlv_data.te_is_neighs->del = free_tlv;
  1474. }
  1475. te_is_neigh = XCALLOC (MTYPE_ISIS_TLV,
  1476. sizeof (struct te_is_neigh));
  1477. if (level == IS_LEVEL_1)
  1478. memcpy (te_is_neigh->neigh_id,
  1479. circuit->u.bc.l1_desig_is, ISIS_SYS_ID_LEN + 1);
  1480. else
  1481. memcpy (te_is_neigh->neigh_id,
  1482. circuit->u.bc.l2_desig_is, ISIS_SYS_ID_LEN + 1);
  1483. if (area->oldmetric)
  1484. metric = circuit->metric[level - 1];
  1485. else
  1486. metric = circuit->te_metric[level - 1];
  1487. SET_TE_METRIC(te_is_neigh, metric);
  1488. if (!memcmp (te_is_neigh->neigh_id, zero_id,
  1489. ISIS_SYS_ID_LEN + 1))
  1490. {
  1491. XFREE (MTYPE_ISIS_TLV, te_is_neigh);
  1492. lsp_debug("ISIS (%s): No DIS for circuit, not adding te-style IS neighbor.",
  1493. area->area_tag);
  1494. }
  1495. else
  1496. {
  1497. /* Check if MPLS_TE is activate */
  1498. if (IS_MPLS_TE(isisMplsTE) && HAS_LINK_PARAMS(circuit->interface))
  1499. /* Add SubTLVs & Adjust real size of SubTLVs */
  1500. te_is_neigh->sub_tlvs_length = add_te_subtlvs(te_is_neigh->sub_tlvs, circuit->mtc);
  1501. else
  1502. /* Or keep only TE metric with no SubTLVs if MPLS_TE is off */
  1503. te_is_neigh->sub_tlvs_length = 0;
  1504. listnode_add (tlv_data.te_is_neighs, te_is_neigh);
  1505. lsp_debug("ISIS (%s): Adding DIS %s.%02x as te-style neighbor",
  1506. area->area_tag, sysid_print(te_is_neigh->neigh_id),
  1507. LSP_PSEUDO_ID(te_is_neigh->neigh_id));
  1508. }
  1509. }
  1510. }
  1511. else
  1512. {
  1513. lsp_debug("ISIS (%s): Circuit is not active for current level. Not adding IS neighbors",
  1514. area->area_tag);
  1515. }
  1516. break;
  1517. case CIRCUIT_T_P2P:
  1518. nei = circuit->u.p2p.neighbor;
  1519. if (nei && (level & nei->circuit_t))
  1520. {
  1521. if (area->oldmetric)
  1522. {
  1523. if (tlv_data.is_neighs == NULL)
  1524. {
  1525. tlv_data.is_neighs = list_new ();
  1526. tlv_data.is_neighs->del = free_tlv;
  1527. }
  1528. is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
  1529. memcpy (is_neigh->neigh_id, nei->sysid, ISIS_SYS_ID_LEN);
  1530. is_neigh->metrics.metric_default = circuit->metric[level - 1];
  1531. is_neigh->metrics.metric_expense = METRICS_UNSUPPORTED;
  1532. is_neigh->metrics.metric_error = METRICS_UNSUPPORTED;
  1533. is_neigh->metrics.metric_delay = METRICS_UNSUPPORTED;
  1534. listnode_add (tlv_data.is_neighs, is_neigh);
  1535. lsp_debug("ISIS (%s): Adding old-style is reach for %s", area->area_tag,
  1536. sysid_print(is_neigh->neigh_id));
  1537. }
  1538. if (area->newmetric)
  1539. {
  1540. uint32_t metric;
  1541. if (tlv_data.te_is_neighs == NULL)
  1542. {
  1543. tlv_data.te_is_neighs = list_new ();
  1544. tlv_data.te_is_neighs->del = free_tlv;
  1545. }
  1546. te_is_neigh = XCALLOC (MTYPE_ISIS_TLV,
  1547. sizeof (struct te_is_neigh));
  1548. memcpy (te_is_neigh->neigh_id, nei->sysid, ISIS_SYS_ID_LEN);
  1549. metric = circuit->te_metric[level - 1];
  1550. SET_TE_METRIC(te_is_neigh, metric);
  1551. /* Check if MPLS_TE is activate */
  1552. if (IS_MPLS_TE(isisMplsTE) && HAS_LINK_PARAMS(circuit->interface))
  1553. /* Update Local and Remote IP address for MPLS TE circuit parameters */
  1554. /* NOTE sure that it is the pertinent place for that updates */
  1555. /* Local IP address could be updated in isis_circuit.c - isis_circuit_add_addr() */
  1556. /* But, where update remote IP address ? in isis_pdu.c - process_p2p_hello() ? */
  1557. /* Add SubTLVs & Adjust real size of SubTLVs */
  1558. te_is_neigh->sub_tlvs_length = add_te_subtlvs(te_is_neigh->sub_tlvs, circuit->mtc);
  1559. else
  1560. /* Or keep only TE metric with no SubTLVs if MPLS_TE is off */
  1561. te_is_neigh->sub_tlvs_length = 0;
  1562. listnode_add (tlv_data.te_is_neighs, te_is_neigh);
  1563. lsp_debug("ISIS (%s): Adding te-style is reach for %s", area->area_tag,
  1564. sysid_print(te_is_neigh->neigh_id));
  1565. }
  1566. }
  1567. else
  1568. {
  1569. lsp_debug("ISIS (%s): No adjacency for given level on this circuit. Not adding IS neighbors",
  1570. area->area_tag);
  1571. }
  1572. break;
  1573. case CIRCUIT_T_LOOPBACK:
  1574. break;
  1575. default:
  1576. zlog_warn ("lsp_area_create: unknown circuit type");
  1577. }
  1578. }
  1579. lsp_build_ext_reach(lsp, area, &tlv_data);
  1580. lsp_debug("ISIS (%s): LSP construction is complete. Serializing...", area->area_tag);
  1581. while (tlv_data.ipv4_int_reachs && listcount (tlv_data.ipv4_int_reachs))
  1582. {
  1583. if (lsp->tlv_data.ipv4_int_reachs == NULL)
  1584. lsp->tlv_data.ipv4_int_reachs = list_new ();
  1585. lsp_tlv_fit (lsp, &tlv_data.ipv4_int_reachs,
  1586. &lsp->tlv_data.ipv4_int_reachs,
  1587. IPV4_REACH_LEN, area->lsp_frag_threshold,
  1588. tlv_add_ipv4_int_reachs);
  1589. if (tlv_data.ipv4_int_reachs && listcount (tlv_data.ipv4_int_reachs))
  1590. lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
  1591. lsp0, area, level);
  1592. }
  1593. while (tlv_data.ipv4_ext_reachs && listcount (tlv_data.ipv4_ext_reachs))
  1594. {
  1595. if (lsp->tlv_data.ipv4_ext_reachs == NULL)
  1596. lsp->tlv_data.ipv4_ext_reachs = list_new ();
  1597. lsp_tlv_fit (lsp, &tlv_data.ipv4_ext_reachs,
  1598. &lsp->tlv_data.ipv4_ext_reachs,
  1599. IPV4_REACH_LEN, area->lsp_frag_threshold,
  1600. tlv_add_ipv4_ext_reachs);
  1601. if (tlv_data.ipv4_ext_reachs && listcount (tlv_data.ipv4_ext_reachs))
  1602. lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
  1603. lsp0, area, level);
  1604. }
  1605. /* FIXME: We pass maximum te_ipv4_reachability length to the lsp_tlv_fit()
  1606. * for now. lsp_tlv_fit() needs to be fixed to deal with variable length
  1607. * TLVs (sub TLVs!). */
  1608. while (tlv_data.te_ipv4_reachs && listcount (tlv_data.te_ipv4_reachs))
  1609. {
  1610. if (lsp->tlv_data.te_ipv4_reachs == NULL)
  1611. lsp->tlv_data.te_ipv4_reachs = list_new ();
  1612. lsp_tlv_fit (lsp, &tlv_data.te_ipv4_reachs,
  1613. &lsp->tlv_data.te_ipv4_reachs,
  1614. TE_IPV4_REACH_LEN, area->lsp_frag_threshold,
  1615. tlv_add_te_ipv4_reachs);
  1616. if (tlv_data.te_ipv4_reachs && listcount (tlv_data.te_ipv4_reachs))
  1617. lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
  1618. lsp0, area, level);
  1619. }
  1620. #ifdef HAVE_IPV6
  1621. while (tlv_data.ipv6_reachs && listcount (tlv_data.ipv6_reachs))
  1622. {
  1623. if (lsp->tlv_data.ipv6_reachs == NULL)
  1624. lsp->tlv_data.ipv6_reachs = list_new ();
  1625. lsp_tlv_fit (lsp, &tlv_data.ipv6_reachs,
  1626. &lsp->tlv_data.ipv6_reachs,
  1627. IPV6_REACH_LEN, area->lsp_frag_threshold,
  1628. tlv_add_ipv6_reachs);
  1629. if (tlv_data.ipv6_reachs && listcount (tlv_data.ipv6_reachs))
  1630. lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
  1631. lsp0, area, level);
  1632. }
  1633. #endif /* HAVE_IPV6 */
  1634. while (tlv_data.is_neighs && listcount (tlv_data.is_neighs))
  1635. {
  1636. if (lsp->tlv_data.is_neighs == NULL)
  1637. lsp->tlv_data.is_neighs = list_new ();
  1638. lsp_tlv_fit (lsp, &tlv_data.is_neighs,
  1639. &lsp->tlv_data.is_neighs,
  1640. IS_NEIGHBOURS_LEN, area->lsp_frag_threshold,
  1641. tlv_add_is_neighs);
  1642. if (tlv_data.is_neighs && listcount (tlv_data.is_neighs))
  1643. lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
  1644. lsp0, area, level);
  1645. }
  1646. while (tlv_data.te_is_neighs && listcount (tlv_data.te_is_neighs))
  1647. {
  1648. if (lsp->tlv_data.te_is_neighs == NULL)
  1649. lsp->tlv_data.te_is_neighs = list_new ();
  1650. lsp_tlv_fit (lsp, &tlv_data.te_is_neighs, &lsp->tlv_data.te_is_neighs,
  1651. IS_NEIGHBOURS_LEN, area->lsp_frag_threshold,
  1652. tlv_add_te_is_neighs);
  1653. if (tlv_data.te_is_neighs && listcount (tlv_data.te_is_neighs))
  1654. lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
  1655. lsp0, area, level);
  1656. }
  1657. lsp->lsp_header->pdu_len = htons (stream_get_endp (lsp->pdu));
  1658. free_tlvs (&tlv_data);
  1659. /* Validate the LSP */
  1660. retval = parse_tlvs (area->area_tag, STREAM_DATA (lsp->pdu) +
  1661. ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN,
  1662. stream_get_endp (lsp->pdu) -
  1663. ISIS_FIXED_HDR_LEN - ISIS_LSP_HDR_LEN,
  1664. &expected, &found, &tlv_data, NULL);
  1665. assert (retval == ISIS_OK);
  1666. return;
  1667. }
  1668. /*
  1669. * 7.3.7 and 7.3.9 Generation on non-pseudonode LSPs
  1670. */
  1671. int
  1672. lsp_generate (struct isis_area *area, int level)
  1673. {
  1674. struct isis_lsp *oldlsp, *newlsp;
  1675. u_int32_t seq_num = 0;
  1676. u_char lspid[ISIS_SYS_ID_LEN + 2];
  1677. u_int16_t rem_lifetime, refresh_time;
  1678. if ((area == NULL) || (area->is_type & level) != level)
  1679. return ISIS_ERROR;
  1680. memset (&lspid, 0, ISIS_SYS_ID_LEN + 2);
  1681. memcpy (&lspid, isis->sysid, ISIS_SYS_ID_LEN);
  1682. /* only builds the lsp if the area shares the level */
  1683. oldlsp = lsp_search (lspid, area->lspdb[level - 1]);
  1684. if (oldlsp)
  1685. {
  1686. /* FIXME: we should actually initiate a purge */
  1687. seq_num = ntohl (oldlsp->lsp_header->seq_num);
  1688. lsp_search_and_destroy (oldlsp->lsp_header->lsp_id,
  1689. area->lspdb[level - 1]);
  1690. }
  1691. rem_lifetime = lsp_rem_lifetime (area, level);
  1692. newlsp = lsp_new (area, lspid, rem_lifetime, seq_num,
  1693. area->is_type | area->overload_bit | area->attached_bit,
  1694. 0, level);
  1695. newlsp->area = area;
  1696. newlsp->own_lsp = 1;
  1697. lsp_insert (newlsp, area->lspdb[level - 1]);
  1698. /* build_lsp_data (newlsp, area); */
  1699. lsp_build (newlsp, area);
  1700. /* time to calculate our checksum */
  1701. lsp_seqnum_update (newlsp);
  1702. newlsp->last_generated = time(NULL);
  1703. lsp_set_all_srmflags (newlsp);
  1704. refresh_time = lsp_refresh_time (newlsp, rem_lifetime);
  1705. THREAD_TIMER_OFF (area->t_lsp_refresh[level - 1]);
  1706. area->lsp_regenerate_pending[level - 1] = 0;
  1707. if (level == IS_LEVEL_1)
  1708. THREAD_TIMER_ON (master, area->t_lsp_refresh[level - 1],
  1709. lsp_l1_refresh, area, refresh_time);
  1710. else if (level == IS_LEVEL_2)
  1711. THREAD_TIMER_ON (master, area->t_lsp_refresh[level - 1],
  1712. lsp_l2_refresh, area, refresh_time);
  1713. if (isis->debugs & DEBUG_UPDATE_PACKETS)
  1714. {
  1715. zlog_debug ("ISIS-Upd (%s): Building L%d LSP %s, len %d, "
  1716. "seq 0x%08x, cksum 0x%04x, lifetime %us refresh %us",
  1717. area->area_tag, level,
  1718. rawlspid_print (newlsp->lsp_header->lsp_id),
  1719. ntohl (newlsp->lsp_header->pdu_len),
  1720. ntohl (newlsp->lsp_header->seq_num),
  1721. ntohs (newlsp->lsp_header->checksum),
  1722. ntohs (newlsp->lsp_header->rem_lifetime),
  1723. refresh_time);
  1724. }
  1725. sched_debug("ISIS (%s): Built L%d LSP. Set triggered regenerate to non-pending.",
  1726. area->area_tag, level);
  1727. return ISIS_OK;
  1728. }
  1729. /*
  1730. * Search own LSPs, update holding time and set SRM
  1731. */
  1732. static int
  1733. lsp_regenerate (struct isis_area *area, int level)
  1734. {
  1735. dict_t *lspdb;
  1736. struct isis_lsp *lsp, *frag;
  1737. struct listnode *node;
  1738. u_char lspid[ISIS_SYS_ID_LEN + 2];
  1739. u_int16_t rem_lifetime, refresh_time;
  1740. if ((area == NULL) || (area->is_type & level) != level)
  1741. return ISIS_ERROR;
  1742. lspdb = area->lspdb[level - 1];
  1743. memset (lspid, 0, ISIS_SYS_ID_LEN + 2);
  1744. memcpy (lspid, isis->sysid, ISIS_SYS_ID_LEN);
  1745. lsp = lsp_search (lspid, lspdb);
  1746. if (!lsp)
  1747. {
  1748. zlog_err ("ISIS-Upd (%s): lsp_regenerate: no L%d LSP found!",
  1749. area->area_tag, level);
  1750. return ISIS_ERROR;
  1751. }
  1752. lsp_clear_data (lsp);
  1753. lsp_build (lsp, area);
  1754. lsp->lsp_header->lsp_bits = lsp_bits_generate (level, area->overload_bit,
  1755. area->attached_bit);
  1756. rem_lifetime = lsp_rem_lifetime (area, level);
  1757. lsp->lsp_header->rem_lifetime = htons (rem_lifetime);
  1758. lsp_seqnum_update (lsp);
  1759. lsp->last_generated = time (NULL);
  1760. lsp_set_all_srmflags (lsp);
  1761. for (ALL_LIST_ELEMENTS_RO (lsp->lspu.frags, node, frag))
  1762. {
  1763. frag->lsp_header->lsp_bits = lsp_bits_generate (level,
  1764. area->overload_bit,
  1765. area->attached_bit);
  1766. /* Set the lifetime values of all the fragments to the same value,
  1767. * so that no fragment expires before the lsp is refreshed.
  1768. */
  1769. frag->lsp_header->rem_lifetime = htons (rem_lifetime);
  1770. lsp_set_all_srmflags (frag);
  1771. }
  1772. refresh_time = lsp_refresh_time (lsp, rem_lifetime);
  1773. if (level == IS_LEVEL_1)
  1774. THREAD_TIMER_ON (master, area->t_lsp_refresh[level - 1],
  1775. lsp_l1_refresh, area, refresh_time);
  1776. else if (level == IS_LEVEL_2)
  1777. THREAD_TIMER_ON (master, area->t_lsp_refresh[level - 1],
  1778. lsp_l2_refresh, area, refresh_time);
  1779. area->lsp_regenerate_pending[level - 1] = 0;
  1780. if (isis->debugs & DEBUG_UPDATE_PACKETS)
  1781. {
  1782. zlog_debug ("ISIS-Upd (%s): Refreshing our L%d LSP %s, len %d, "
  1783. "seq 0x%08x, cksum 0x%04x, lifetime %us refresh %us",
  1784. area->area_tag, level,
  1785. rawlspid_print (lsp->lsp_header->lsp_id),
  1786. ntohl (lsp->lsp_header->pdu_len),
  1787. ntohl (lsp->lsp_header->seq_num),
  1788. ntohs (lsp->lsp_header->checksum),
  1789. ntohs (lsp->lsp_header->rem_lifetime),
  1790. refresh_time);
  1791. }
  1792. sched_debug("ISIS (%s): Rebuilt L%d LSP. Set triggered regenerate to non-pending.",
  1793. area->area_tag, level);
  1794. return ISIS_OK;
  1795. }
  1796. /*
  1797. * Something has changed or periodic refresh -> regenerate LSP
  1798. */
  1799. static int
  1800. lsp_l1_refresh (struct thread *thread)
  1801. {
  1802. struct isis_area *area;
  1803. area = THREAD_ARG (thread);
  1804. assert (area);
  1805. area->t_lsp_refresh[0] = NULL;
  1806. area->lsp_regenerate_pending[0] = 0;
  1807. if ((area->is_type & IS_LEVEL_1) == 0)
  1808. return ISIS_ERROR;
  1809. sched_debug("ISIS (%s): LSP L1 refresh timer expired. Refreshing LSP...", area->area_tag);
  1810. return lsp_regenerate (area, IS_LEVEL_1);
  1811. }
  1812. static int
  1813. lsp_l2_refresh (struct thread *thread)
  1814. {
  1815. struct isis_area *area;
  1816. area = THREAD_ARG (thread);
  1817. assert (area);
  1818. area->t_lsp_refresh[1] = NULL;
  1819. area->lsp_regenerate_pending[1] = 0;
  1820. if ((area->is_type & IS_LEVEL_2) == 0)
  1821. return ISIS_ERROR;
  1822. sched_debug("ISIS (%s): LSP L2 refresh timer expired. Refreshing LSP...", area->area_tag);
  1823. return lsp_regenerate (area, IS_LEVEL_2);
  1824. }
  1825. int
  1826. lsp_regenerate_schedule (struct isis_area *area, int level, int all_pseudo)
  1827. {
  1828. struct isis_lsp *lsp;
  1829. u_char id[ISIS_SYS_ID_LEN + 2];
  1830. time_t now, diff;
  1831. long timeout;
  1832. struct listnode *cnode;
  1833. struct isis_circuit *circuit;
  1834. int lvl;
  1835. if (area == NULL)
  1836. return ISIS_ERROR;
  1837. sched_debug("ISIS (%s): Scheduling regeneration of %s LSPs, %sincluding PSNs",
  1838. area->area_tag, circuit_t2string(level), all_pseudo ? "" : "not ");
  1839. memcpy (id, isis->sysid, ISIS_SYS_ID_LEN);
  1840. LSP_PSEUDO_ID (id) = LSP_FRAGMENT (id) = 0;
  1841. now = time (NULL);
  1842. for (lvl = IS_LEVEL_1; lvl <= IS_LEVEL_2; lvl++)
  1843. {
  1844. if (!((level & lvl) && (area->is_type & lvl)))
  1845. continue;
  1846. sched_debug("ISIS (%s): Checking whether L%d needs to be scheduled",
  1847. area->area_tag, lvl);
  1848. if (area->lsp_regenerate_pending[lvl - 1])
  1849. {
  1850. struct timeval remain = thread_timer_remain(area->t_lsp_refresh[lvl - 1]);
  1851. sched_debug("ISIS (%s): Regeneration is already pending, nothing todo."
  1852. " (Due in %lld.%03lld seconds)", area->area_tag,
  1853. (long long)remain.tv_sec, (long long)remain.tv_usec / 1000);
  1854. continue;
  1855. }
  1856. lsp = lsp_search (id, area->lspdb[lvl - 1]);
  1857. if (!lsp)
  1858. {
  1859. sched_debug("ISIS (%s): We do not have any LSPs to regenerate, nothing todo.",
  1860. area->area_tag);
  1861. continue;
  1862. }
  1863. /*
  1864. * Throttle avoidance
  1865. */
  1866. sched_debug("ISIS (%s): Will schedule regen timer. Last run was: %lld, Now is: %lld",
  1867. area->area_tag, (long long)lsp->last_generated, (long long)now);
  1868. THREAD_TIMER_OFF (area->t_lsp_refresh[lvl - 1]);
  1869. diff = now - lsp->last_generated;
  1870. if (diff < area->lsp_gen_interval[lvl - 1])
  1871. {
  1872. timeout = 1000 * (area->lsp_gen_interval[lvl - 1] - diff);
  1873. sched_debug("ISIS (%s): Scheduling in %ld ms to match configured lsp_gen_interval",
  1874. area->area_tag, timeout);
  1875. }
  1876. else
  1877. {
  1878. /*
  1879. * lsps are not regenerated if lsp_regenerate function is called
  1880. * directly. However if the lsp_regenerate call is queued for
  1881. * later execution it works.
  1882. */
  1883. timeout = 100;
  1884. sched_debug("ISIS (%s): Last generation was more than lsp_gen_interval ago."
  1885. " Scheduling for execution in %ld ms.", area->area_tag, timeout);
  1886. }
  1887. area->lsp_regenerate_pending[lvl - 1] = 1;
  1888. if (lvl == IS_LEVEL_1)
  1889. {
  1890. THREAD_TIMER_MSEC_ON(master, area->t_lsp_refresh[lvl - 1],
  1891. lsp_l1_refresh, area, timeout);
  1892. }
  1893. else if (lvl == IS_LEVEL_2)
  1894. {
  1895. THREAD_TIMER_MSEC_ON(master, area->t_lsp_refresh[lvl - 1],
  1896. lsp_l2_refresh, area, timeout);
  1897. }
  1898. }
  1899. if (all_pseudo)
  1900. {
  1901. for (ALL_LIST_ELEMENTS_RO (area->circuit_list, cnode, circuit))
  1902. lsp_regenerate_schedule_pseudo (circuit, level);
  1903. }
  1904. return ISIS_OK;
  1905. }
  1906. /*
  1907. * Funcs for pseudonode LSPs
  1908. */
  1909. /*
  1910. * 7.3.8 and 7.3.10 Generation of level 1 and 2 pseudonode LSPs
  1911. */
  1912. static void
  1913. lsp_build_pseudo (struct isis_lsp *lsp, struct isis_circuit *circuit,
  1914. int level)
  1915. {
  1916. struct isis_adjacency *adj;
  1917. struct is_neigh *is_neigh;
  1918. struct te_is_neigh *te_is_neigh;
  1919. struct es_neigh *es_neigh;
  1920. struct list *adj_list;
  1921. struct listnode *node;
  1922. struct isis_area *area = circuit->area;
  1923. lsp_debug("ISIS (%s): Constructing pseudo LSP %s for interface %s level %d",
  1924. area->area_tag, rawlspid_print(lsp->lsp_header->lsp_id),
  1925. circuit->interface->name, level);
  1926. lsp->level = level;
  1927. /* RFC3787 section 4 SHOULD not set overload bit in pseudo LSPs */
  1928. lsp->lsp_header->lsp_bits = lsp_bits_generate (level, 0,
  1929. circuit->area->attached_bit);
  1930. /*
  1931. * add self to IS neighbours
  1932. */
  1933. if (circuit->area->oldmetric)
  1934. {
  1935. if (lsp->tlv_data.is_neighs == NULL)
  1936. {
  1937. lsp->tlv_data.is_neighs = list_new ();
  1938. lsp->tlv_data.is_neighs->del = free_tlv;
  1939. }
  1940. is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
  1941. memcpy (&is_neigh->neigh_id, isis->sysid, ISIS_SYS_ID_LEN);
  1942. listnode_add (lsp->tlv_data.is_neighs, is_neigh);
  1943. lsp_debug("ISIS (%s): Adding %s.%02x as old-style neighbor (self)",
  1944. area->area_tag, sysid_print(is_neigh->neigh_id),
  1945. LSP_PSEUDO_ID(is_neigh->neigh_id));
  1946. }
  1947. if (circuit->area->newmetric)
  1948. {
  1949. if (lsp->tlv_data.te_is_neighs == NULL)
  1950. {
  1951. lsp->tlv_data.te_is_neighs = list_new ();
  1952. lsp->tlv_data.te_is_neighs->del = free_tlv;
  1953. }
  1954. te_is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct te_is_neigh));
  1955. memcpy (&te_is_neigh->neigh_id, isis->sysid, ISIS_SYS_ID_LEN);
  1956. listnode_add (lsp->tlv_data.te_is_neighs, te_is_neigh);
  1957. lsp_debug("ISIS (%s): Adding %s.%02x as te-style neighbor (self)",
  1958. area->area_tag, sysid_print(te_is_neigh->neigh_id),
  1959. LSP_PSEUDO_ID(te_is_neigh->neigh_id));
  1960. }
  1961. adj_list = list_new ();
  1962. isis_adj_build_up_list (circuit->u.bc.adjdb[level - 1], adj_list);
  1963. for (ALL_LIST_ELEMENTS_RO (adj_list, node, adj))
  1964. {
  1965. if (adj->level & level)
  1966. {
  1967. if ((level == IS_LEVEL_1 && adj->sys_type == ISIS_SYSTYPE_L1_IS) ||
  1968. (level == IS_LEVEL_1 && adj->sys_type == ISIS_SYSTYPE_L2_IS &&
  1969. adj->adj_usage == ISIS_ADJ_LEVEL1AND2) ||
  1970. (level == IS_LEVEL_2 && adj->sys_type == ISIS_SYSTYPE_L2_IS))
  1971. {
  1972. /* an IS neighbour -> add it */
  1973. if (circuit->area->oldmetric)
  1974. {
  1975. is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
  1976. memcpy (&is_neigh->neigh_id, adj->sysid, ISIS_SYS_ID_LEN);
  1977. listnode_add (lsp->tlv_data.is_neighs, is_neigh);
  1978. lsp_debug("ISIS (%s): Adding %s.%02x as old-style neighbor (peer)",
  1979. area->area_tag, sysid_print(is_neigh->neigh_id),
  1980. LSP_PSEUDO_ID(is_neigh->neigh_id));
  1981. }
  1982. if (circuit->area->newmetric)
  1983. {
  1984. te_is_neigh = XCALLOC (MTYPE_ISIS_TLV,
  1985. sizeof (struct te_is_neigh));
  1986. memcpy (&te_is_neigh->neigh_id, adj->sysid, ISIS_SYS_ID_LEN);
  1987. listnode_add (lsp->tlv_data.te_is_neighs, te_is_neigh);
  1988. lsp_debug("ISIS (%s): Adding %s.%02x as te-style neighbor (peer)",
  1989. area->area_tag, sysid_print(te_is_neigh->neigh_id),
  1990. LSP_PSEUDO_ID(te_is_neigh->neigh_id));
  1991. }
  1992. }
  1993. else if (level == IS_LEVEL_1 && adj->sys_type == ISIS_SYSTYPE_ES)
  1994. {
  1995. /* an ES neigbour add it, if we are building level 1 LSP */
  1996. /* FIXME: the tlv-format is hard to use here */
  1997. if (lsp->tlv_data.es_neighs == NULL)
  1998. {
  1999. lsp->tlv_data.es_neighs = list_new ();
  2000. lsp->tlv_data.es_neighs->del = free_tlv;
  2001. }
  2002. es_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct es_neigh));
  2003. memcpy (&es_neigh->first_es_neigh, adj->sysid, ISIS_SYS_ID_LEN);
  2004. listnode_add (lsp->tlv_data.es_neighs, es_neigh);
  2005. lsp_debug("ISIS (%s): Adding %s as ES neighbor (peer)",
  2006. area->area_tag, sysid_print(es_neigh->first_es_neigh));
  2007. }
  2008. else
  2009. {
  2010. lsp_debug("ISIS (%s): Ignoring neighbor %s, level does not match",
  2011. area->area_tag, sysid_print(adj->sysid));
  2012. }
  2013. }
  2014. else
  2015. {
  2016. lsp_debug("ISIS (%s): Ignoring neighbor %s, level does not intersect",
  2017. area->area_tag, sysid_print(adj->sysid));
  2018. }
  2019. }
  2020. list_delete (adj_list);
  2021. lsp_debug("ISIS (%s): Pseudo LSP construction is complete.", area->area_tag);
  2022. /* Reset endp of stream to overwrite only TLV part of it. */
  2023. stream_reset (lsp->pdu);
  2024. stream_forward_endp (lsp->pdu, ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
  2025. /*
  2026. * Add the authentication info if it's present
  2027. */
  2028. lsp_auth_add (lsp);
  2029. if (lsp->tlv_data.is_neighs && listcount (lsp->tlv_data.is_neighs) > 0)
  2030. tlv_add_is_neighs (lsp->tlv_data.is_neighs, lsp->pdu);
  2031. if (lsp->tlv_data.te_is_neighs && listcount (lsp->tlv_data.te_is_neighs) > 0)
  2032. tlv_add_te_is_neighs (lsp->tlv_data.te_is_neighs, lsp->pdu);
  2033. if (lsp->tlv_data.es_neighs && listcount (lsp->tlv_data.es_neighs) > 0)
  2034. tlv_add_is_neighs (lsp->tlv_data.es_neighs, lsp->pdu);
  2035. lsp->lsp_header->pdu_len = htons (stream_get_endp (lsp->pdu));
  2036. /* Recompute authentication and checksum information */
  2037. lsp_auth_update (lsp);
  2038. fletcher_checksum(STREAM_DATA (lsp->pdu) + 12,
  2039. ntohs (lsp->lsp_header->pdu_len) - 12, 12);
  2040. return;
  2041. }
  2042. int
  2043. lsp_generate_pseudo (struct isis_circuit *circuit, int level)
  2044. {
  2045. dict_t *lspdb = circuit->area->lspdb[level - 1];
  2046. struct isis_lsp *lsp;
  2047. u_char lsp_id[ISIS_SYS_ID_LEN + 2];
  2048. u_int16_t rem_lifetime, refresh_time;
  2049. if ((circuit->is_type & level) != level ||
  2050. (circuit->state != C_STATE_UP) ||
  2051. (circuit->circ_type != CIRCUIT_T_BROADCAST) ||
  2052. (circuit->u.bc.is_dr[level - 1] == 0))
  2053. return ISIS_ERROR;
  2054. memcpy (lsp_id, isis->sysid, ISIS_SYS_ID_LEN);
  2055. LSP_FRAGMENT (lsp_id) = 0;
  2056. LSP_PSEUDO_ID (lsp_id) = circuit->circuit_id;
  2057. /*
  2058. * If for some reason have a pseudo LSP in the db already -> regenerate
  2059. */
  2060. if (lsp_search (lsp_id, lspdb))
  2061. return lsp_regenerate_schedule_pseudo (circuit, level);
  2062. rem_lifetime = lsp_rem_lifetime (circuit->area, level);
  2063. /* RFC3787 section 4 SHOULD not set overload bit in pseudo LSPs */
  2064. lsp = lsp_new (circuit->area, lsp_id, rem_lifetime, 1,
  2065. circuit->area->is_type | circuit->area->attached_bit,
  2066. 0, level);
  2067. lsp->area = circuit->area;
  2068. lsp_build_pseudo (lsp, circuit, level);
  2069. lsp->own_lsp = 1;
  2070. lsp_insert (lsp, lspdb);
  2071. lsp_set_all_srmflags (lsp);
  2072. refresh_time = lsp_refresh_time (lsp, rem_lifetime);
  2073. THREAD_TIMER_OFF (circuit->u.bc.t_refresh_pseudo_lsp[level - 1]);
  2074. circuit->lsp_regenerate_pending[level - 1] = 0;
  2075. if (level == IS_LEVEL_1)
  2076. THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[level - 1],
  2077. lsp_l1_refresh_pseudo, circuit, refresh_time);
  2078. else if (level == IS_LEVEL_2)
  2079. THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[level - 1],
  2080. lsp_l2_refresh_pseudo, circuit, refresh_time);
  2081. if (isis->debugs & DEBUG_UPDATE_PACKETS)
  2082. {
  2083. zlog_debug ("ISIS-Upd (%s): Building L%d Pseudo LSP %s, len %d, "
  2084. "seq 0x%08x, cksum 0x%04x, lifetime %us, refresh %us",
  2085. circuit->area->area_tag, level,
  2086. rawlspid_print (lsp->lsp_header->lsp_id),
  2087. ntohl (lsp->lsp_header->pdu_len),
  2088. ntohl (lsp->lsp_header->seq_num),
  2089. ntohs (lsp->lsp_header->checksum),
  2090. ntohs (lsp->lsp_header->rem_lifetime),
  2091. refresh_time);
  2092. }
  2093. return ISIS_OK;
  2094. }
  2095. static int
  2096. lsp_regenerate_pseudo (struct isis_circuit *circuit, int level)
  2097. {
  2098. dict_t *lspdb = circuit->area->lspdb[level - 1];
  2099. struct isis_lsp *lsp;
  2100. u_char lsp_id[ISIS_SYS_ID_LEN + 2];
  2101. u_int16_t rem_lifetime, refresh_time;
  2102. if ((circuit->is_type & level) != level ||
  2103. (circuit->state != C_STATE_UP) ||
  2104. (circuit->circ_type != CIRCUIT_T_BROADCAST) ||
  2105. (circuit->u.bc.is_dr[level - 1] == 0))
  2106. return ISIS_ERROR;
  2107. memcpy (lsp_id, isis->sysid, ISIS_SYS_ID_LEN);
  2108. LSP_PSEUDO_ID (lsp_id) = circuit->circuit_id;
  2109. LSP_FRAGMENT (lsp_id) = 0;
  2110. lsp = lsp_search (lsp_id, lspdb);
  2111. if (!lsp)
  2112. {
  2113. zlog_err ("lsp_regenerate_pseudo: no l%d LSP %s found!",
  2114. level, rawlspid_print (lsp_id));
  2115. return ISIS_ERROR;
  2116. }
  2117. lsp_clear_data (lsp);
  2118. lsp_build_pseudo (lsp, circuit, level);
  2119. /* RFC3787 section 4 SHOULD not set overload bit in pseudo LSPs */
  2120. lsp->lsp_header->lsp_bits = lsp_bits_generate (level, 0,
  2121. circuit->area->attached_bit);
  2122. rem_lifetime = lsp_rem_lifetime (circuit->area, level);
  2123. lsp->lsp_header->rem_lifetime = htons (rem_lifetime);
  2124. lsp_inc_seqnum (lsp, 0);
  2125. lsp->last_generated = time (NULL);
  2126. lsp_set_all_srmflags (lsp);
  2127. refresh_time = lsp_refresh_time (lsp, rem_lifetime);
  2128. if (level == IS_LEVEL_1)
  2129. THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[level - 1],
  2130. lsp_l1_refresh_pseudo, circuit, refresh_time);
  2131. else if (level == IS_LEVEL_2)
  2132. THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[level - 1],
  2133. lsp_l2_refresh_pseudo, circuit, refresh_time);
  2134. if (isis->debugs & DEBUG_UPDATE_PACKETS)
  2135. {
  2136. zlog_debug ("ISIS-Upd (%s): Refreshing L%d Pseudo LSP %s, len %d, "
  2137. "seq 0x%08x, cksum 0x%04x, lifetime %us, refresh %us",
  2138. circuit->area->area_tag, level,
  2139. rawlspid_print (lsp->lsp_header->lsp_id),
  2140. ntohl (lsp->lsp_header->pdu_len),
  2141. ntohl (lsp->lsp_header->seq_num),
  2142. ntohs (lsp->lsp_header->checksum),
  2143. ntohs (lsp->lsp_header->rem_lifetime),
  2144. refresh_time);
  2145. }
  2146. return ISIS_OK;
  2147. }
  2148. /*
  2149. * Something has changed or periodic refresh -> regenerate pseudo LSP
  2150. */
  2151. static int
  2152. lsp_l1_refresh_pseudo (struct thread *thread)
  2153. {
  2154. struct isis_circuit *circuit;
  2155. u_char id[ISIS_SYS_ID_LEN + 2];
  2156. circuit = THREAD_ARG (thread);
  2157. circuit->u.bc.t_refresh_pseudo_lsp[0] = NULL;
  2158. circuit->lsp_regenerate_pending[0] = 0;
  2159. if ((circuit->u.bc.is_dr[0] == 0) ||
  2160. (circuit->is_type & IS_LEVEL_1) == 0)
  2161. {
  2162. memcpy (id, isis->sysid, ISIS_SYS_ID_LEN);
  2163. LSP_PSEUDO_ID (id) = circuit->circuit_id;
  2164. LSP_FRAGMENT (id) = 0;
  2165. lsp_purge_pseudo (id, circuit, IS_LEVEL_1);
  2166. return ISIS_ERROR;
  2167. }
  2168. return lsp_regenerate_pseudo (circuit, IS_LEVEL_1);
  2169. }
  2170. static int
  2171. lsp_l2_refresh_pseudo (struct thread *thread)
  2172. {
  2173. struct isis_circuit *circuit;
  2174. u_char id[ISIS_SYS_ID_LEN + 2];
  2175. circuit = THREAD_ARG (thread);
  2176. circuit->u.bc.t_refresh_pseudo_lsp[1] = NULL;
  2177. circuit->lsp_regenerate_pending[1] = 0;
  2178. if ((circuit->u.bc.is_dr[1] == 0) ||
  2179. (circuit->is_type & IS_LEVEL_2) == 0)
  2180. {
  2181. memcpy (id, isis->sysid, ISIS_SYS_ID_LEN);
  2182. LSP_PSEUDO_ID (id) = circuit->circuit_id;
  2183. LSP_FRAGMENT (id) = 0;
  2184. lsp_purge_pseudo (id, circuit, IS_LEVEL_2);
  2185. return ISIS_ERROR;
  2186. }
  2187. return lsp_regenerate_pseudo (circuit, IS_LEVEL_2);
  2188. }
  2189. int
  2190. lsp_regenerate_schedule_pseudo (struct isis_circuit *circuit, int level)
  2191. {
  2192. struct isis_lsp *lsp;
  2193. u_char lsp_id[ISIS_SYS_ID_LEN + 2];
  2194. time_t now, diff;
  2195. long timeout;
  2196. int lvl;
  2197. struct isis_area *area = circuit->area;
  2198. if (circuit == NULL ||
  2199. circuit->circ_type != CIRCUIT_T_BROADCAST ||
  2200. circuit->state != C_STATE_UP)
  2201. return ISIS_OK;
  2202. sched_debug("ISIS (%s): Scheduling regeneration of %s pseudo LSP for interface %s",
  2203. area->area_tag, circuit_t2string(level), circuit->interface->name);
  2204. memcpy (lsp_id, isis->sysid, ISIS_SYS_ID_LEN);
  2205. LSP_PSEUDO_ID (lsp_id) = circuit->circuit_id;
  2206. LSP_FRAGMENT (lsp_id) = 0;
  2207. now = time (NULL);
  2208. for (lvl = IS_LEVEL_1; lvl <= IS_LEVEL_2; lvl++)
  2209. {
  2210. sched_debug("ISIS (%s): Checking whether L%d pseudo LSP needs to be scheduled",
  2211. area->area_tag, lvl);
  2212. if (!((level & lvl) && (circuit->is_type & lvl)))
  2213. {
  2214. sched_debug("ISIS (%s): Level is not active on circuit",
  2215. area->area_tag);
  2216. continue;
  2217. }
  2218. if (circuit->u.bc.is_dr[lvl - 1] == 0)
  2219. {
  2220. sched_debug("ISIS (%s): This IS is not DR, nothing to do.",
  2221. area->area_tag);
  2222. continue;
  2223. }
  2224. if (circuit->lsp_regenerate_pending[lvl - 1])
  2225. {
  2226. struct timeval remain =
  2227. thread_timer_remain(circuit->u.bc.t_refresh_pseudo_lsp[lvl - 1]);
  2228. sched_debug("ISIS (%s): Regenerate is already pending, nothing todo."
  2229. " (Due in %lld.%03lld seconds)", area->area_tag,
  2230. (long long)remain.tv_sec, (long long)remain.tv_usec/1000);
  2231. continue;
  2232. }
  2233. lsp = lsp_search (lsp_id, circuit->area->lspdb[lvl - 1]);
  2234. if (!lsp)
  2235. {
  2236. sched_debug("ISIS (%s): Pseudonode LSP does not exist yet, nothing to regenerate.",
  2237. area->area_tag);
  2238. continue;
  2239. }
  2240. /*
  2241. * Throttle avoidance
  2242. */
  2243. sched_debug("ISIS (%s): Will schedule PSN regen timer. Last run was: %lld, Now is: %lld",
  2244. area->area_tag, (long long)lsp->last_generated, (long long) now);
  2245. THREAD_TIMER_OFF (circuit->u.bc.t_refresh_pseudo_lsp[lvl - 1]);
  2246. diff = now - lsp->last_generated;
  2247. if (diff < circuit->area->lsp_gen_interval[lvl - 1])
  2248. {
  2249. timeout = 1000 * (circuit->area->lsp_gen_interval[lvl - 1] - diff);
  2250. sched_debug("ISIS (%s): Sechduling in %ld ms to match configured lsp_gen_interval",
  2251. area->area_tag, timeout);
  2252. }
  2253. else
  2254. {
  2255. timeout = 100;
  2256. sched_debug("ISIS (%s): Last generation was more than lsp_gen_interval ago."
  2257. " Scheduling for execution in %ld ms.", area->area_tag, timeout);
  2258. }
  2259. circuit->lsp_regenerate_pending[lvl - 1] = 1;
  2260. if (lvl == IS_LEVEL_1)
  2261. {
  2262. THREAD_TIMER_MSEC_ON(master,
  2263. circuit->u.bc.t_refresh_pseudo_lsp[lvl - 1],
  2264. lsp_l1_refresh_pseudo, circuit, timeout);
  2265. }
  2266. else if (lvl == IS_LEVEL_2)
  2267. {
  2268. THREAD_TIMER_MSEC_ON(master,
  2269. circuit->u.bc.t_refresh_pseudo_lsp[lvl - 1],
  2270. lsp_l2_refresh_pseudo, circuit, timeout);
  2271. }
  2272. }
  2273. return ISIS_OK;
  2274. }
  2275. /*
  2276. * Walk through LSPs for an area
  2277. * - set remaining lifetime
  2278. * - set LSPs with SRMflag set for sending
  2279. */
  2280. int
  2281. lsp_tick (struct thread *thread)
  2282. {
  2283. struct isis_area *area;
  2284. struct isis_circuit *circuit;
  2285. struct isis_lsp *lsp;
  2286. struct list *lsp_list;
  2287. struct listnode *lspnode, *cnode;
  2288. dnode_t *dnode, *dnode_next;
  2289. int level;
  2290. u_int16_t rem_lifetime;
  2291. lsp_list = list_new ();
  2292. area = THREAD_ARG (thread);
  2293. assert (area);
  2294. area->t_tick = NULL;
  2295. THREAD_TIMER_ON (master, area->t_tick, lsp_tick, area, 1);
  2296. /*
  2297. * Build a list of LSPs with (any) SRMflag set
  2298. * and removed the ones that have aged out
  2299. */
  2300. for (level = 0; level < ISIS_LEVELS; level++)
  2301. {
  2302. if (area->lspdb[level] && dict_count (area->lspdb[level]) > 0)
  2303. {
  2304. for (dnode = dict_first (area->lspdb[level]);
  2305. dnode != NULL; dnode = dnode_next)
  2306. {
  2307. dnode_next = dict_next (area->lspdb[level], dnode);
  2308. lsp = dnode_get (dnode);
  2309. /*
  2310. * The lsp rem_lifetime is kept at 0 for MaxAge or
  2311. * ZeroAgeLifetime depending on explicit purge or
  2312. * natural age out. So schedule spf only once when
  2313. * the first time rem_lifetime becomes 0.
  2314. */
  2315. rem_lifetime = ntohs(lsp->lsp_header->rem_lifetime);
  2316. lsp_set_time (lsp);
  2317. /*
  2318. * Schedule may run spf which should be done only after
  2319. * the lsp rem_lifetime becomes 0 for the first time.
  2320. * ISO 10589 - 7.3.16.4 first paragraph.
  2321. */
  2322. if (rem_lifetime == 1 && lsp->lsp_header->seq_num != 0)
  2323. {
  2324. /* 7.3.16.4 a) set SRM flags on all */
  2325. lsp_set_all_srmflags (lsp);
  2326. /* 7.3.16.4 b) retain only the header FIXME */
  2327. /* 7.3.16.4 c) record the time to purge FIXME */
  2328. /* run/schedule spf */
  2329. /* isis_spf_schedule is called inside lsp_destroy() below;
  2330. * so it is not needed here. */
  2331. /* isis_spf_schedule (lsp->area, lsp->level); */
  2332. }
  2333. if (lsp->age_out == 0)
  2334. {
  2335. zlog_debug ("ISIS-Upd (%s): L%u LSP %s seq 0x%08x aged out",
  2336. area->area_tag,
  2337. lsp->level,
  2338. rawlspid_print (lsp->lsp_header->lsp_id),
  2339. ntohl (lsp->lsp_header->seq_num));
  2340. #ifdef TOPOLOGY_GENERATE
  2341. if (lsp->from_topology)
  2342. THREAD_TIMER_OFF (lsp->t_lsp_top_ref);
  2343. #endif /* TOPOLOGY_GENERATE */
  2344. lsp_destroy (lsp);
  2345. lsp = NULL;
  2346. dict_delete_free (area->lspdb[level], dnode);
  2347. }
  2348. else if (flags_any_set (lsp->SRMflags))
  2349. listnode_add (lsp_list, lsp);
  2350. }
  2351. /*
  2352. * Send LSPs on circuits indicated by the SRMflags
  2353. */
  2354. if (listcount (lsp_list) > 0)
  2355. {
  2356. for (ALL_LIST_ELEMENTS_RO (area->circuit_list, cnode, circuit))
  2357. {
  2358. int diff = time (NULL) - circuit->lsp_queue_last_cleared;
  2359. if (circuit->lsp_queue == NULL ||
  2360. diff < MIN_LSP_TRANS_INTERVAL)
  2361. continue;
  2362. for (ALL_LIST_ELEMENTS_RO (lsp_list, lspnode, lsp))
  2363. {
  2364. if (circuit->upadjcount[lsp->level - 1] &&
  2365. ISIS_CHECK_FLAG (lsp->SRMflags, circuit))
  2366. {
  2367. /* Add the lsp only if it is not already in lsp
  2368. * queue */
  2369. if (! listnode_lookup (circuit->lsp_queue, lsp))
  2370. {
  2371. listnode_add (circuit->lsp_queue, lsp);
  2372. thread_add_event (master, send_lsp, circuit, 0);
  2373. }
  2374. }
  2375. }
  2376. }
  2377. list_delete_all_node (lsp_list);
  2378. }
  2379. }
  2380. }
  2381. list_delete (lsp_list);
  2382. return ISIS_OK;
  2383. }
  2384. void
  2385. lsp_purge_pseudo (u_char * id, struct isis_circuit *circuit, int level)
  2386. {
  2387. struct isis_lsp *lsp;
  2388. u_int16_t seq_num;
  2389. u_int8_t lsp_bits;
  2390. lsp = lsp_search (id, circuit->area->lspdb[level - 1]);
  2391. if (!lsp)
  2392. return;
  2393. /* store old values */
  2394. seq_num = lsp->lsp_header->seq_num;
  2395. lsp_bits = lsp->lsp_header->lsp_bits;
  2396. /* reset stream */
  2397. lsp_clear_data (lsp);
  2398. stream_reset (lsp->pdu);
  2399. /* update header */
  2400. lsp->lsp_header->pdu_len = htons (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
  2401. memcpy (lsp->lsp_header->lsp_id, id, ISIS_SYS_ID_LEN + 2);
  2402. lsp->lsp_header->checksum = 0;
  2403. lsp->lsp_header->seq_num = seq_num;
  2404. lsp->lsp_header->rem_lifetime = 0;
  2405. lsp->lsp_header->lsp_bits = lsp_bits;
  2406. lsp->level = level;
  2407. lsp->age_out = lsp->area->max_lsp_lifetime[level-1];
  2408. stream_forward_endp (lsp->pdu, ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
  2409. /*
  2410. * Add and update the authentication info if its present
  2411. */
  2412. lsp_auth_add (lsp);
  2413. lsp->lsp_header->pdu_len = htons (stream_get_endp (lsp->pdu));
  2414. lsp_auth_update (lsp);
  2415. fletcher_checksum(STREAM_DATA (lsp->pdu) + 12,
  2416. ntohs (lsp->lsp_header->pdu_len) - 12, 12);
  2417. lsp_set_all_srmflags (lsp);
  2418. return;
  2419. }
  2420. /*
  2421. * Purge own LSP that is received and we don't have.
  2422. * -> Do as in 7.3.16.4
  2423. */
  2424. void
  2425. lsp_purge_non_exist (int level,
  2426. struct isis_link_state_hdr *lsp_hdr,
  2427. struct isis_area *area)
  2428. {
  2429. struct isis_lsp *lsp;
  2430. /*
  2431. * We need to create the LSP to be purged
  2432. */
  2433. lsp = XCALLOC (MTYPE_ISIS_LSP, sizeof (struct isis_lsp));
  2434. lsp->area = area;
  2435. lsp->level = level;
  2436. lsp->pdu = stream_new(LLC_LEN + area->lsp_mtu);
  2437. lsp->isis_header = (struct isis_fixed_hdr *) STREAM_DATA (lsp->pdu);
  2438. fill_fixed_hdr (lsp->isis_header, (lsp->level == IS_LEVEL_1) ? L1_LINK_STATE
  2439. : L2_LINK_STATE);
  2440. lsp->lsp_header = (struct isis_link_state_hdr *) (STREAM_DATA (lsp->pdu) +
  2441. ISIS_FIXED_HDR_LEN);
  2442. memcpy (lsp->lsp_header, lsp_hdr, ISIS_LSP_HDR_LEN);
  2443. stream_forward_endp (lsp->pdu, ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
  2444. /*
  2445. * Set the remaining lifetime to 0
  2446. */
  2447. lsp->lsp_header->rem_lifetime = 0;
  2448. /*
  2449. * Add and update the authentication info if its present
  2450. */
  2451. lsp_auth_add (lsp);
  2452. lsp_auth_update (lsp);
  2453. /*
  2454. * Update the PDU length to header plus any authentication TLV.
  2455. */
  2456. lsp->lsp_header->pdu_len = htons (stream_get_endp (lsp->pdu));
  2457. /*
  2458. * Put the lsp into LSPdb
  2459. */
  2460. lsp_insert (lsp, area->lspdb[lsp->level - 1]);
  2461. /*
  2462. * Send in to whole area
  2463. */
  2464. lsp_set_all_srmflags (lsp);
  2465. return;
  2466. }
  2467. void lsp_set_all_srmflags (struct isis_lsp *lsp)
  2468. {
  2469. struct listnode *node;
  2470. struct isis_circuit *circuit;
  2471. assert (lsp);
  2472. ISIS_FLAGS_CLEAR_ALL(lsp->SRMflags);
  2473. if (lsp->area)
  2474. {
  2475. struct list *circuit_list = lsp->area->circuit_list;
  2476. for (ALL_LIST_ELEMENTS_RO (circuit_list, node, circuit))
  2477. {
  2478. ISIS_SET_FLAG(lsp->SRMflags, circuit);
  2479. }
  2480. }
  2481. }
  2482. #ifdef TOPOLOGY_GENERATE
  2483. static int
  2484. top_lsp_refresh (struct thread *thread)
  2485. {
  2486. struct isis_lsp *lsp;
  2487. u_int16_t rem_lifetime;
  2488. lsp = THREAD_ARG (thread);
  2489. assert (lsp);
  2490. lsp->t_lsp_top_ref = NULL;
  2491. lsp_seqnum_update (lsp);
  2492. lsp_set_all_srmflags (lsp);
  2493. if (isis->debugs & DEBUG_UPDATE_PACKETS)
  2494. {
  2495. zlog_debug ("ISIS-Upd (): refreshing Topology L1 %s",
  2496. rawlspid_print (lsp->lsp_header->lsp_id));
  2497. }
  2498. /* Refresh dynamic hostname in the cache. */
  2499. isis_dynhn_insert (lsp->lsp_header->lsp_id, lsp->tlv_data.hostname,
  2500. IS_LEVEL_1);
  2501. lsp->lsp_header->lsp_bits = lsp_bits_generate (lsp->level,
  2502. lsp->area->overload_bit,
  2503. lsp->area->attached_bit);
  2504. rem_lifetime = lsp_rem_lifetime (lsp->area, IS_LEVEL_1);
  2505. lsp->lsp_header->rem_lifetime = htons (rem_lifetime);
  2506. /* refresh_time = lsp_refresh_time (lsp, rem_lifetime); */
  2507. THREAD_TIMER_ON (master, lsp->t_lsp_top_ref, top_lsp_refresh, lsp,
  2508. lsp->area->lsp_refresh[0]);
  2509. return ISIS_OK;
  2510. }
  2511. void
  2512. generate_topology_lsps (struct isis_area *area)
  2513. {
  2514. struct listnode *node;
  2515. int i, max = 0;
  2516. struct arc *arc;
  2517. u_char lspid[ISIS_SYS_ID_LEN + 2];
  2518. struct isis_lsp *lsp;
  2519. u_int16_t rem_lifetime, refresh_time;
  2520. /* first we find the maximal node */
  2521. for (ALL_LIST_ELEMENTS_RO (area->topology, node, arc))
  2522. {
  2523. if (arc->from_node > max)
  2524. max = arc->from_node;
  2525. if (arc->to_node > max)
  2526. max = arc->to_node;
  2527. }
  2528. for (i = 1; i < (max + 1); i++)
  2529. {
  2530. memcpy (lspid, area->topology_baseis, ISIS_SYS_ID_LEN);
  2531. LSP_PSEUDO_ID (lspid) = 0x00;
  2532. LSP_FRAGMENT (lspid) = 0x00;
  2533. lspid[ISIS_SYS_ID_LEN - 1] = (i & 0xFF);
  2534. lspid[ISIS_SYS_ID_LEN - 2] = ((i >> 8) & 0xFF);
  2535. rem_lifetime = lsp_rem_lifetime (area, IS_LEVEL_1);
  2536. lsp = lsp_new (area, lspid, rem_lifetime, 1,
  2537. IS_LEVEL_1 | area->overload_bit | area->attached_bit,
  2538. 0, 1);
  2539. if (!lsp)
  2540. return;
  2541. lsp->from_topology = 1;
  2542. /* Creating LSP data based on topology info. */
  2543. build_topology_lsp_data (lsp, area, i);
  2544. /* Checksum is also calculated here. */
  2545. lsp_seqnum_update (lsp);
  2546. /* Take care of inserting dynamic hostname into cache. */
  2547. isis_dynhn_insert (lspid, lsp->tlv_data.hostname, IS_LEVEL_1);
  2548. refresh_time = lsp_refresh_time (lsp, rem_lifetime);
  2549. THREAD_TIMER_ON (master, lsp->t_lsp_top_ref, top_lsp_refresh, lsp,
  2550. refresh_time);
  2551. lsp_set_all_srmflags (lsp);
  2552. lsp_insert (lsp, area->lspdb[0]);
  2553. }
  2554. }
  2555. void
  2556. remove_topology_lsps (struct isis_area *area)
  2557. {
  2558. struct isis_lsp *lsp;
  2559. dnode_t *dnode, *dnode_next;
  2560. dnode = dict_first (area->lspdb[0]);
  2561. while (dnode != NULL)
  2562. {
  2563. dnode_next = dict_next (area->lspdb[0], dnode);
  2564. lsp = dnode_get (dnode);
  2565. if (lsp->from_topology)
  2566. {
  2567. THREAD_TIMER_OFF (lsp->t_lsp_top_ref);
  2568. lsp_destroy (lsp);
  2569. dict_delete (area->lspdb[0], dnode);
  2570. }
  2571. dnode = dnode_next;
  2572. }
  2573. }
  2574. void
  2575. build_topology_lsp_data (struct isis_lsp *lsp, struct isis_area *area,
  2576. int lsp_top_num)
  2577. {
  2578. struct listnode *node;
  2579. struct arc *arc;
  2580. struct is_neigh *is_neigh;
  2581. struct te_is_neigh *te_is_neigh;
  2582. char buff[200];
  2583. struct tlvs tlv_data;
  2584. struct isis_lsp *lsp0 = lsp;
  2585. /* Add area addresses. FIXME: Is it needed at all? */
  2586. if (lsp->tlv_data.area_addrs == NULL)
  2587. lsp->tlv_data.area_addrs = list_new ();
  2588. list_add_list (lsp->tlv_data.area_addrs, area->area_addrs);
  2589. if (lsp->tlv_data.nlpids == NULL)
  2590. lsp->tlv_data.nlpids = XMALLOC (MTYPE_ISIS_TLV, sizeof (struct nlpids));
  2591. lsp->tlv_data.nlpids->count = 1;
  2592. lsp->tlv_data.nlpids->nlpids[0] = NLPID_IP;
  2593. if (area->dynhostname)
  2594. {
  2595. lsp->tlv_data.hostname = XMALLOC (MTYPE_ISIS_TLV,
  2596. sizeof (struct hostname));
  2597. memset (buff, 0x00, 200);
  2598. sprintf (buff, "%s%d", area->topology_basedynh ? area->topology_basedynh :
  2599. "feedme", lsp_top_num);
  2600. memcpy (lsp->tlv_data.hostname->name, buff, strlen (buff));
  2601. lsp->tlv_data.hostname->namelen = strlen (buff);
  2602. }
  2603. if (lsp->tlv_data.nlpids)
  2604. tlv_add_nlpid (lsp->tlv_data.nlpids, lsp->pdu);
  2605. if (lsp->tlv_data.hostname)
  2606. tlv_add_dynamic_hostname (lsp->tlv_data.hostname, lsp->pdu);
  2607. if (lsp->tlv_data.area_addrs && listcount (lsp->tlv_data.area_addrs) > 0)
  2608. tlv_add_area_addrs (lsp->tlv_data.area_addrs, lsp->pdu);
  2609. memset (&tlv_data, 0, sizeof (struct tlvs));
  2610. if (tlv_data.is_neighs == NULL)
  2611. {
  2612. tlv_data.is_neighs = list_new ();
  2613. tlv_data.is_neighs->del = free_tlv;
  2614. }
  2615. /* Add reachability for this IS for simulated 1. */
  2616. if (lsp_top_num == 1)
  2617. {
  2618. is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
  2619. memcpy (&is_neigh->neigh_id, isis->sysid, ISIS_SYS_ID_LEN);
  2620. LSP_PSEUDO_ID (is_neigh->neigh_id) = 0x00;
  2621. /* Metric MUST NOT be 0, unless it's not alias TLV. */
  2622. is_neigh->metrics.metric_default = 0x01;
  2623. is_neigh->metrics.metric_delay = METRICS_UNSUPPORTED;
  2624. is_neigh->metrics.metric_expense = METRICS_UNSUPPORTED;
  2625. is_neigh->metrics.metric_error = METRICS_UNSUPPORTED;
  2626. listnode_add (tlv_data.is_neighs, is_neigh);
  2627. }
  2628. /* Add IS reachabilities. */
  2629. for (ALL_LIST_ELEMENTS_RO (area->topology, node, arc))
  2630. {
  2631. int to_lsp = 0;
  2632. if ((lsp_top_num != arc->from_node) && (lsp_top_num != arc->to_node))
  2633. continue;
  2634. if (lsp_top_num == arc->from_node)
  2635. to_lsp = arc->to_node;
  2636. else
  2637. to_lsp = arc->from_node;
  2638. if (area->oldmetric)
  2639. {
  2640. is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
  2641. memcpy (&is_neigh->neigh_id, area->topology_baseis, ISIS_SYS_ID_LEN);
  2642. is_neigh->neigh_id[ISIS_SYS_ID_LEN - 1] = (to_lsp & 0xFF);
  2643. is_neigh->neigh_id[ISIS_SYS_ID_LEN - 2] = ((to_lsp >> 8) & 0xFF);
  2644. is_neigh->metrics.metric_default = arc->distance;
  2645. is_neigh->metrics.metric_delay = METRICS_UNSUPPORTED;
  2646. is_neigh->metrics.metric_expense = METRICS_UNSUPPORTED;
  2647. is_neigh->metrics.metric_error = METRICS_UNSUPPORTED;
  2648. listnode_add (tlv_data.is_neighs, is_neigh);
  2649. }
  2650. if (area->newmetric)
  2651. {
  2652. if (tlv_data.te_is_neighs == NULL)
  2653. {
  2654. tlv_data.te_is_neighs = list_new ();
  2655. tlv_data.te_is_neighs->del = free_tlv;
  2656. }
  2657. te_is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct te_is_neigh));
  2658. memcpy (&te_is_neigh->neigh_id, area->topology_baseis,
  2659. ISIS_SYS_ID_LEN);
  2660. te_is_neigh->neigh_id[ISIS_SYS_ID_LEN - 1] = (to_lsp & 0xFF);
  2661. te_is_neigh->neigh_id[ISIS_SYS_ID_LEN - 2] = ((to_lsp >> 8) & 0xFF);
  2662. SET_TE_METRIC(te_is_neigh, arc->distance);
  2663. listnode_add (tlv_data.te_is_neighs, te_is_neigh);
  2664. }
  2665. }
  2666. while (tlv_data.is_neighs && listcount (tlv_data.is_neighs))
  2667. {
  2668. if (lsp->tlv_data.is_neighs == NULL)
  2669. lsp->tlv_data.is_neighs = list_new ();
  2670. lsp_tlv_fit (lsp, &tlv_data.is_neighs, &lsp->tlv_data.is_neighs,
  2671. IS_NEIGHBOURS_LEN, area->lsp_frag_threshold,
  2672. tlv_add_is_neighs);
  2673. if (tlv_data.is_neighs && listcount (tlv_data.is_neighs))
  2674. lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
  2675. lsp0, area, IS_LEVEL_1);
  2676. }
  2677. while (tlv_data.te_is_neighs && listcount (tlv_data.te_is_neighs))
  2678. {
  2679. if (lsp->tlv_data.te_is_neighs == NULL)
  2680. lsp->tlv_data.te_is_neighs = list_new ();
  2681. lsp_tlv_fit (lsp, &tlv_data.te_is_neighs, &lsp->tlv_data.te_is_neighs,
  2682. IS_NEIGHBOURS_LEN, area->lsp_frag_threshold,
  2683. tlv_add_te_is_neighs);
  2684. if (tlv_data.te_is_neighs && listcount (tlv_data.te_is_neighs))
  2685. lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
  2686. lsp0, area, IS_LEVEL_1);
  2687. }
  2688. free_tlvs (&tlv_data);
  2689. return;
  2690. }
  2691. #endif /* TOPOLOGY_GENERATE */