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