isis_lsp.c 77 KB

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