]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_lsp.c
isisd: null check (Coverity 1424529)
[mirror_frr.git] / isisd / isis_lsp.c
1 /*
2 * IS-IS Rout(e)ing protocol - isis_lsp.c
3 * LSP processing
4 *
5 * Copyright (C) 2001,2002 Sampo Saaristo
6 * Tampere University of Technology
7 * Institute of Communications Engineering
8 * Copyright (C) 2013-2015 Christian Franke <chris@opensourcerouting.org>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; see the file COPYING; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include <zebra.h>
26
27 #include "linklist.h"
28 #include "thread.h"
29 #include "vty.h"
30 #include "stream.h"
31 #include "memory.h"
32 #include "log.h"
33 #include "prefix.h"
34 #include "command.h"
35 #include "hash.h"
36 #include "if.h"
37 #include "checksum.h"
38 #include "md5.h"
39 #include "table.h"
40 #include "srcdest_table.h"
41 #include "lib_errors.h"
42
43 #include "isisd/dict.h"
44 #include "isisd/isis_constants.h"
45 #include "isisd/isis_common.h"
46 #include "isisd/isis_flags.h"
47 #include "isisd/isis_circuit.h"
48 #include "isisd/isisd.h"
49 #include "isisd/isis_lsp.h"
50 #include "isisd/isis_pdu.h"
51 #include "isisd/isis_dynhn.h"
52 #include "isisd/isis_misc.h"
53 #include "isisd/isis_csm.h"
54 #include "isisd/isis_adjacency.h"
55 #include "isisd/isis_spf.h"
56 #include "isisd/isis_te.h"
57 #include "isisd/isis_mt.h"
58 #include "isisd/isis_tlvs.h"
59 #include "isisd/fabricd.h"
60 #include "isisd/isis_tx_queue.h"
61
62 static int lsp_l1_refresh(struct thread *thread);
63 static int lsp_l2_refresh(struct thread *thread);
64 static int lsp_l1_refresh_pseudo(struct thread *thread);
65 static int lsp_l2_refresh_pseudo(struct thread *thread);
66
67 int lsp_id_cmp(uint8_t *id1, uint8_t *id2)
68 {
69 return memcmp(id1, id2, ISIS_SYS_ID_LEN + 2);
70 }
71
72 dict_t *lsp_db_init(void)
73 {
74 dict_t *dict;
75
76 dict = dict_create(DICTCOUNT_T_MAX, (dict_comp_t)lsp_id_cmp);
77
78 return dict;
79 }
80
81 struct isis_lsp *lsp_search(uint8_t *id, dict_t *lspdb)
82 {
83 dnode_t *node;
84
85 #ifdef EXTREME_DEBUG
86 dnode_t *dn;
87
88 zlog_debug("searching db");
89 for (dn = dict_first(lspdb); dn; dn = dict_next(lspdb, dn)) {
90 zlog_debug("%s\t%pX",
91 rawlspid_print((uint8_t *)dnode_getkey(dn)),
92 dnode_get(dn));
93 }
94 #endif /* EXTREME DEBUG */
95
96 node = dict_lookup(lspdb, id);
97
98 if (node)
99 return (struct isis_lsp *)dnode_get(node);
100
101 return NULL;
102 }
103
104 static void lsp_clear_data(struct isis_lsp *lsp)
105 {
106 if (!lsp)
107 return;
108
109 isis_free_tlvs(lsp->tlvs);
110 lsp->tlvs = NULL;
111 }
112
113 static void lsp_destroy(struct isis_lsp *lsp)
114 {
115 struct listnode *cnode;
116 struct isis_circuit *circuit;
117
118 if (!lsp)
119 return;
120
121 for (ALL_LIST_ELEMENTS_RO(lsp->area->circuit_list, cnode, circuit))
122 isis_tx_queue_del(circuit->tx_queue, lsp);
123
124 ISIS_FLAGS_CLEAR_ALL(lsp->SSNflags);
125
126 lsp_clear_data(lsp);
127
128 if (LSP_FRAGMENT(lsp->hdr.lsp_id) == 0 && lsp->lspu.frags) {
129 list_delete(&lsp->lspu.frags);
130 lsp->lspu.frags = NULL;
131 }
132
133 isis_spf_schedule(lsp->area, lsp->level);
134
135 if (lsp->pdu)
136 stream_free(lsp->pdu);
137 XFREE(MTYPE_ISIS_LSP, lsp);
138 }
139
140 void lsp_db_destroy(dict_t *lspdb)
141 {
142 dnode_t *dnode, *next;
143 struct isis_lsp *lsp;
144
145 dnode = dict_first(lspdb);
146 while (dnode) {
147 next = dict_next(lspdb, dnode);
148 lsp = dnode_get(dnode);
149 lsp_destroy(lsp);
150 dict_delete_free(lspdb, dnode);
151 dnode = next;
152 }
153
154 dict_free(lspdb);
155
156 return;
157 }
158
159 /*
160 * Remove all the frags belonging to the given lsp
161 */
162 static void lsp_remove_frags(struct list *frags, dict_t *lspdb)
163 {
164 dnode_t *dnode;
165 struct listnode *lnode, *lnnode;
166 struct isis_lsp *lsp;
167
168 for (ALL_LIST_ELEMENTS(frags, lnode, lnnode, lsp)) {
169 dnode = dict_lookup(lspdb, lsp->hdr.lsp_id);
170 lsp_destroy(lsp);
171 dnode_destroy(dict_delete(lspdb, dnode));
172 }
173
174 list_delete_all_node(frags);
175
176 return;
177 }
178
179 void lsp_search_and_destroy(uint8_t *id, dict_t *lspdb)
180 {
181 dnode_t *node;
182 struct isis_lsp *lsp;
183
184 node = dict_lookup(lspdb, id);
185 if (node) {
186 node = dict_delete(lspdb, node);
187 lsp = dnode_get(node);
188 /*
189 * If this is a zero lsp, remove all the frags now
190 */
191 if (LSP_FRAGMENT(lsp->hdr.lsp_id) == 0) {
192 if (lsp->lspu.frags)
193 lsp_remove_frags(lsp->lspu.frags, lspdb);
194 } else {
195 /*
196 * else just remove this frag, from the zero lsps' frag
197 * list
198 */
199 if (lsp->lspu.zero_lsp
200 && lsp->lspu.zero_lsp->lspu.frags)
201 listnode_delete(lsp->lspu.zero_lsp->lspu.frags,
202 lsp);
203 }
204 lsp_destroy(lsp);
205 dnode_destroy(node);
206 }
207 }
208
209 /*
210 * Compares a LSP to given values
211 * Params are given in net order
212 */
213 int lsp_compare(char *areatag, struct isis_lsp *lsp, uint32_t seqno,
214 uint16_t checksum, uint16_t rem_lifetime)
215 {
216 if (lsp->hdr.seqno == seqno && lsp->hdr.checksum == checksum
217 && ((lsp->hdr.rem_lifetime == 0 && rem_lifetime == 0)
218 || (lsp->hdr.rem_lifetime != 0 && rem_lifetime != 0))) {
219 if (isis->debugs & DEBUG_SNP_PACKETS) {
220 zlog_debug(
221 "ISIS-Snp (%s): Compare LSP %s seq 0x%08" PRIx32
222 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
223 "s",
224 areatag, rawlspid_print(lsp->hdr.lsp_id),
225 lsp->hdr.seqno, lsp->hdr.checksum,
226 lsp->hdr.rem_lifetime);
227 zlog_debug(
228 "ISIS-Snp (%s): is equal to ours seq 0x%08" PRIx32
229 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
230 "s",
231 areatag, seqno, checksum, rem_lifetime);
232 }
233 return LSP_EQUAL;
234 }
235
236 /*
237 * LSPs with identical checksums should only be treated as newer if:
238 * a) The current LSP has a remaining lifetime != 0 and the other LSP
239 * has a
240 * remaining lifetime == 0. In this case, we should participate in
241 * the purge
242 * and should not treat the current LSP with remaining lifetime == 0
243 * as older.
244 * b) The LSP has an incorrect checksum. In this case, we need to react
245 * as given
246 * in 7.3.16.2.
247 */
248 if (seqno > lsp->hdr.seqno
249 || (seqno == lsp->hdr.seqno
250 && ((lsp->hdr.rem_lifetime != 0 && rem_lifetime == 0)
251 || lsp->hdr.checksum != checksum))) {
252 if (isis->debugs & DEBUG_SNP_PACKETS) {
253 zlog_debug(
254 "ISIS-Snp (%s): Compare LSP %s seq 0x%08" PRIx32
255 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
256 "s",
257 areatag, rawlspid_print(lsp->hdr.lsp_id), seqno,
258 checksum, rem_lifetime);
259 zlog_debug(
260 "ISIS-Snp (%s): is newer than ours seq 0x%08" PRIx32
261 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
262 "s",
263 areatag, lsp->hdr.seqno, lsp->hdr.checksum,
264 lsp->hdr.rem_lifetime);
265 }
266 return LSP_NEWER;
267 }
268 if (isis->debugs & DEBUG_SNP_PACKETS) {
269 zlog_debug("ISIS-Snp (%s): Compare LSP %s seq 0x%08" PRIx32
270 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16 "s",
271 areatag, rawlspid_print(lsp->hdr.lsp_id), seqno,
272 checksum, rem_lifetime);
273 zlog_debug(
274 "ISIS-Snp (%s): is older than ours seq 0x%08" PRIx32
275 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16 "s",
276 areatag, lsp->hdr.seqno, lsp->hdr.checksum,
277 lsp->hdr.rem_lifetime);
278 }
279
280 return LSP_OLDER;
281 }
282
283 static void put_lsp_hdr(struct isis_lsp *lsp, size_t *len_pointer, bool keep)
284 {
285 uint8_t pdu_type =
286 (lsp->level == IS_LEVEL_1) ? L1_LINK_STATE : L2_LINK_STATE;
287 struct isis_lsp_hdr *hdr = &lsp->hdr;
288 struct stream *stream = lsp->pdu;
289 size_t orig_getp = 0, orig_endp = 0;
290
291 if (keep) {
292 orig_getp = stream_get_getp(lsp->pdu);
293 orig_endp = stream_get_endp(lsp->pdu);
294 }
295
296 stream_set_getp(lsp->pdu, 0);
297 stream_set_endp(lsp->pdu, 0);
298
299 fill_fixed_hdr(pdu_type, stream);
300
301 if (len_pointer)
302 *len_pointer = stream_get_endp(stream);
303 stream_putw(stream, hdr->pdu_len);
304 stream_putw(stream, hdr->rem_lifetime);
305 stream_put(stream, hdr->lsp_id, sizeof(hdr->lsp_id));
306 stream_putl(stream, hdr->seqno);
307 stream_putw(stream, hdr->checksum);
308 stream_putc(stream, hdr->lsp_bits);
309
310 if (keep) {
311 stream_set_endp(lsp->pdu, orig_endp);
312 stream_set_getp(lsp->pdu, orig_getp);
313 }
314 }
315
316 static void lsp_add_auth(struct isis_lsp *lsp)
317 {
318 struct isis_passwd *passwd;
319 passwd = (lsp->level == IS_LEVEL_1) ? &lsp->area->area_passwd
320 : &lsp->area->domain_passwd;
321 isis_tlvs_add_auth(lsp->tlvs, passwd);
322 }
323
324 static void lsp_pack_pdu(struct isis_lsp *lsp)
325 {
326 if (!lsp->tlvs)
327 lsp->tlvs = isis_alloc_tlvs();
328
329 lsp_add_auth(lsp);
330
331 size_t len_pointer;
332 put_lsp_hdr(lsp, &len_pointer, false);
333 isis_pack_tlvs(lsp->tlvs, lsp->pdu, len_pointer, false, true);
334
335 lsp->hdr.pdu_len = stream_get_endp(lsp->pdu);
336 lsp->hdr.checksum =
337 ntohs(fletcher_checksum(STREAM_DATA(lsp->pdu) + 12,
338 stream_get_endp(lsp->pdu) - 12, 12));
339 }
340
341 void lsp_inc_seqno(struct isis_lsp *lsp, uint32_t seqno)
342 {
343 uint32_t newseq;
344
345 if (seqno == 0 || lsp->hdr.seqno > seqno)
346 newseq = lsp->hdr.seqno + 1;
347 else
348 newseq = seqno + 1;
349
350 lsp->hdr.seqno = newseq;
351
352 lsp_pack_pdu(lsp);
353 isis_spf_schedule(lsp->area, lsp->level);
354 }
355
356 static void lsp_purge_add_poi(struct isis_lsp *lsp,
357 const uint8_t *sender)
358 {
359 if (!lsp->area->purge_originator)
360 return;
361
362 /* add purge originator identification */
363 if (!lsp->tlvs)
364 lsp->tlvs = isis_alloc_tlvs();
365 isis_tlvs_set_purge_originator(lsp->tlvs, isis->sysid, sender);
366 isis_tlvs_set_dynamic_hostname(lsp->tlvs, cmd_hostname_get());
367 }
368
369 static void lsp_purge(struct isis_lsp *lsp, int level,
370 const uint8_t *sender)
371 {
372 /* reset stream */
373 lsp_clear_data(lsp);
374 stream_reset(lsp->pdu);
375
376 /* update header */
377 lsp->hdr.checksum = 0;
378 lsp->hdr.rem_lifetime = 0;
379 lsp->level = level;
380 lsp->age_out = lsp->area->max_lsp_lifetime[level - 1];
381
382 lsp_purge_add_poi(lsp, sender);
383
384 lsp_pack_pdu(lsp);
385 lsp_flood(lsp, NULL);
386 }
387
388 /*
389 * Generates checksum for LSP and its frags
390 */
391 static void lsp_seqno_update(struct isis_lsp *lsp0)
392 {
393 struct isis_lsp *lsp;
394 struct listnode *node;
395
396 lsp_inc_seqno(lsp0, 0);
397
398 if (!lsp0->lspu.frags)
399 return;
400
401 for (ALL_LIST_ELEMENTS_RO(lsp0->lspu.frags, node, lsp)) {
402 if (lsp->tlvs)
403 lsp_inc_seqno(lsp, 0);
404 else
405 lsp_purge(lsp, lsp0->level, NULL);
406 }
407
408 return;
409 }
410
411 static uint8_t lsp_bits_generate(int level, int overload_bit, int attached_bit)
412 {
413 uint8_t lsp_bits = 0;
414 if (level == IS_LEVEL_1)
415 lsp_bits = IS_LEVEL_1;
416 else
417 lsp_bits = IS_LEVEL_1_AND_2;
418 if (overload_bit)
419 lsp_bits |= overload_bit;
420 if (attached_bit)
421 lsp_bits |= attached_bit;
422 return lsp_bits;
423 }
424
425 static void lsp_update_data(struct isis_lsp *lsp, struct isis_lsp_hdr *hdr,
426 struct isis_tlvs *tlvs, struct stream *stream,
427 struct isis_area *area, int level)
428 {
429 /* free the old lsp data */
430 lsp_clear_data(lsp);
431
432 /* copying only the relevant part of our stream */
433 if (lsp->pdu != NULL)
434 stream_free(lsp->pdu);
435 lsp->pdu = stream_dup(stream);
436
437 memcpy(&lsp->hdr, hdr, sizeof(lsp->hdr));
438 lsp->area = area;
439 lsp->level = level;
440 lsp->age_out = ZERO_AGE_LIFETIME;
441 lsp->installed = time(NULL);
442
443 lsp->tlvs = tlvs;
444
445 if (area->dynhostname && lsp->tlvs->hostname
446 && lsp->hdr.rem_lifetime) {
447 isis_dynhn_insert(lsp->hdr.lsp_id, lsp->tlvs->hostname,
448 (lsp->hdr.lsp_bits & LSPBIT_IST)
449 == IS_LEVEL_1_AND_2
450 ? IS_LEVEL_2
451 : IS_LEVEL_1);
452 }
453
454 return;
455 }
456
457 static void lsp_link_fragment(struct isis_lsp *lsp, struct isis_lsp *lsp0)
458 {
459 if (!LSP_FRAGMENT(lsp->hdr.lsp_id)) {
460 /* zero lsp -> create list to store fragments */
461 lsp->lspu.frags = list_new();
462 } else {
463 /* fragment -> set backpointer and add to zero lsps list */
464 assert(lsp0);
465 lsp->lspu.zero_lsp = lsp0;
466 listnode_add(lsp0->lspu.frags, lsp);
467 }
468 }
469
470 void lsp_update(struct isis_lsp *lsp, struct isis_lsp_hdr *hdr,
471 struct isis_tlvs *tlvs, struct stream *stream,
472 struct isis_area *area, int level, bool confusion)
473 {
474 if (lsp->own_lsp) {
475 flog_err(
476 EC_LIB_DEVELOPMENT,
477 "ISIS-Upd (%s): BUG updating LSP %s still marked as own LSP",
478 area->area_tag, rawlspid_print(lsp->hdr.lsp_id));
479 lsp_clear_data(lsp);
480 lsp->own_lsp = 0;
481 }
482
483 if (confusion) {
484 lsp_purge(lsp, level, NULL);
485 } else {
486 lsp_update_data(lsp, hdr, tlvs, stream, area, level);
487 }
488
489 if (LSP_FRAGMENT(lsp->hdr.lsp_id) && !lsp->lspu.zero_lsp) {
490 uint8_t lspid[ISIS_SYS_ID_LEN + 2];
491 struct isis_lsp *lsp0;
492
493 memcpy(lspid, lsp->hdr.lsp_id, ISIS_SYS_ID_LEN + 1);
494 LSP_FRAGMENT(lspid) = 0;
495 lsp0 = lsp_search(lspid, area->lspdb[level - 1]);
496 if (lsp0)
497 lsp_link_fragment(lsp, lsp0);
498 }
499
500 if (lsp->hdr.seqno)
501 isis_spf_schedule(lsp->area, lsp->level);
502 }
503
504 /* creation of LSP directly from what we received */
505 struct isis_lsp *lsp_new_from_recv(struct isis_lsp_hdr *hdr,
506 struct isis_tlvs *tlvs,
507 struct stream *stream, struct isis_lsp *lsp0,
508 struct isis_area *area, int level)
509 {
510 struct isis_lsp *lsp;
511
512 lsp = XCALLOC(MTYPE_ISIS_LSP, sizeof(struct isis_lsp));
513 lsp_update_data(lsp, hdr, tlvs, stream, area, level);
514 lsp_link_fragment(lsp, lsp0);
515
516 return lsp;
517 }
518
519 struct isis_lsp *lsp_new(struct isis_area *area, uint8_t *lsp_id,
520 uint16_t rem_lifetime, uint32_t seqno,
521 uint8_t lsp_bits, uint16_t checksum,
522 struct isis_lsp *lsp0, int level)
523 {
524 struct isis_lsp *lsp;
525
526 lsp = XCALLOC(MTYPE_ISIS_LSP, sizeof(struct isis_lsp));
527 lsp->area = area;
528
529 lsp->pdu = stream_new(LLC_LEN + area->lsp_mtu);
530
531 /* Minimal LSP PDU size */
532 lsp->hdr.pdu_len = ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN;
533 memcpy(lsp->hdr.lsp_id, lsp_id, sizeof(lsp->hdr.lsp_id));
534 lsp->hdr.checksum = checksum;
535 lsp->hdr.seqno = seqno;
536 lsp->hdr.rem_lifetime = rem_lifetime;
537 lsp->hdr.lsp_bits = lsp_bits;
538 lsp->level = level;
539 lsp->age_out = ZERO_AGE_LIFETIME;
540 lsp_link_fragment(lsp, lsp0);
541 put_lsp_hdr(lsp, NULL, false);
542
543 if (isis->debugs & DEBUG_EVENTS)
544 zlog_debug("New LSP with ID %s-%02x-%02x len %d seqnum %08x",
545 sysid_print(lsp_id), LSP_PSEUDO_ID(lsp->hdr.lsp_id),
546 LSP_FRAGMENT(lsp->hdr.lsp_id), lsp->hdr.pdu_len,
547 lsp->hdr.seqno);
548
549 return lsp;
550 }
551
552 void lsp_insert(struct isis_lsp *lsp, dict_t *lspdb)
553 {
554 dict_alloc_insert(lspdb, lsp->hdr.lsp_id, lsp);
555 if (lsp->hdr.seqno)
556 isis_spf_schedule(lsp->area, lsp->level);
557 }
558
559 /*
560 * Build a list of LSPs with non-zero ht bounded by start and stop ids
561 */
562 void lsp_build_list_nonzero_ht(uint8_t *start_id, uint8_t *stop_id,
563 struct list *list, dict_t *lspdb)
564 {
565 dnode_t *first, *last, *curr;
566
567 first = dict_lower_bound(lspdb, start_id);
568 if (!first)
569 return;
570
571 last = dict_upper_bound(lspdb, stop_id);
572
573 curr = first;
574
575 if (((struct isis_lsp *)(curr->dict_data))->hdr.rem_lifetime)
576 listnode_add(list, first->dict_data);
577
578 while (curr) {
579 curr = dict_next(lspdb, curr);
580 if (curr
581 && ((struct isis_lsp *)(curr->dict_data))->hdr.rem_lifetime)
582 listnode_add(list, curr->dict_data);
583 if (curr == last)
584 break;
585 }
586
587 return;
588 }
589
590 static void lsp_set_time(struct isis_lsp *lsp)
591 {
592 assert(lsp);
593
594 if (lsp->hdr.rem_lifetime == 0) {
595 if (lsp->age_out > 0)
596 lsp->age_out--;
597 return;
598 }
599
600 lsp->hdr.rem_lifetime--;
601 if (lsp->pdu && stream_get_endp(lsp->pdu) >= 12)
602 stream_putw_at(lsp->pdu, 10, lsp->hdr.rem_lifetime);
603 }
604
605 static void lspid_print(uint8_t *lsp_id, uint8_t *trg, char dynhost, char frag)
606 {
607 struct isis_dynhn *dyn = NULL;
608 uint8_t id[SYSID_STRLEN];
609
610 if (dynhost)
611 dyn = dynhn_find_by_id(lsp_id);
612 else
613 dyn = NULL;
614
615 if (dyn)
616 sprintf((char *)id, "%.14s", dyn->hostname);
617 else if (!memcmp(isis->sysid, lsp_id, ISIS_SYS_ID_LEN) && dynhost)
618 sprintf((char *)id, "%.14s", cmd_hostname_get());
619 else
620 memcpy(id, sysid_print(lsp_id), 15);
621 if (frag)
622 sprintf((char *)trg, "%s.%02x-%02x", id, LSP_PSEUDO_ID(lsp_id),
623 LSP_FRAGMENT(lsp_id));
624 else
625 sprintf((char *)trg, "%s.%02x", id, LSP_PSEUDO_ID(lsp_id));
626 }
627
628 /* Convert the lsp attribute bits to attribute string */
629 static const char *lsp_bits2string(uint8_t lsp_bits, char *buf, size_t buf_size)
630 {
631 char *pos = buf;
632
633 if (!lsp_bits)
634 return " none";
635
636 if (buf_size < 2 * 3)
637 return " error";
638
639 /* we only focus on the default metric */
640 pos += sprintf(pos, "%d/",
641 ISIS_MASK_LSP_ATT_DEFAULT_BIT(lsp_bits) ? 1 : 0);
642
643 pos += sprintf(pos, "%d/",
644 ISIS_MASK_LSP_PARTITION_BIT(lsp_bits) ? 1 : 0);
645
646 sprintf(pos, "%d", ISIS_MASK_LSP_OL_BIT(lsp_bits) ? 1 : 0);
647
648 return buf;
649 }
650
651 /* this function prints the lsp on show isis database */
652 void lsp_print(struct isis_lsp *lsp, struct vty *vty, char dynhost)
653 {
654 uint8_t LSPid[255];
655 char age_out[8];
656 char b[200];
657
658 lspid_print(lsp->hdr.lsp_id, LSPid, dynhost, 1);
659 vty_out(vty, "%-21s%c ", LSPid, lsp->own_lsp ? '*' : ' ');
660 vty_out(vty, "%5" PRIu16 " ", lsp->hdr.pdu_len);
661 vty_out(vty, "0x%08" PRIx32 " ", lsp->hdr.seqno);
662 vty_out(vty, "0x%04" PRIx16 " ", lsp->hdr.checksum);
663 if (lsp->hdr.rem_lifetime == 0) {
664 snprintf(age_out, 8, "(%d)", lsp->age_out);
665 age_out[7] = '\0';
666 vty_out(vty, "%7s ", age_out);
667 } else
668 vty_out(vty, " %5" PRIu16 " ", lsp->hdr.rem_lifetime);
669 vty_out(vty, "%s\n", lsp_bits2string(lsp->hdr.lsp_bits, b, sizeof(b)));
670 }
671
672 void lsp_print_detail(struct isis_lsp *lsp, struct vty *vty, char dynhost)
673 {
674 lsp_print(lsp, vty, dynhost);
675 if (lsp->tlvs)
676 vty_multiline(vty, " ", "%s", isis_format_tlvs(lsp->tlvs));
677 vty_out(vty, "\n");
678 }
679
680 /* print all the lsps info in the local lspdb */
681 int lsp_print_all(struct vty *vty, dict_t *lspdb, char detail, char dynhost)
682 {
683
684 dnode_t *node = dict_first(lspdb), *next;
685 int lsp_count = 0;
686
687 if (detail == ISIS_UI_LEVEL_BRIEF) {
688 while (node != NULL) {
689 /* I think it is unnecessary, so I comment it out */
690 /* dict_contains (lspdb, node); */
691 next = dict_next(lspdb, node);
692 lsp_print(dnode_get(node), vty, dynhost);
693 node = next;
694 lsp_count++;
695 }
696 } else if (detail == ISIS_UI_LEVEL_DETAIL) {
697 while (node != NULL) {
698 next = dict_next(lspdb, node);
699 lsp_print_detail(dnode_get(node), vty, dynhost);
700 node = next;
701 lsp_count++;
702 }
703 }
704
705 return lsp_count;
706 }
707
708 static uint16_t lsp_rem_lifetime(struct isis_area *area, int level)
709 {
710 uint16_t rem_lifetime;
711
712 /* Add jitter to configured LSP lifetime */
713 rem_lifetime =
714 isis_jitter(area->max_lsp_lifetime[level - 1], MAX_AGE_JITTER);
715
716 /* No jitter if the max refresh will be less than configure gen interval
717 */
718 /* N.B. this calucation is acceptable since rem_lifetime is in
719 * [332,65535] at
720 * this point */
721 if (area->lsp_gen_interval[level - 1] > (rem_lifetime - 300))
722 rem_lifetime = area->max_lsp_lifetime[level - 1];
723
724 return rem_lifetime;
725 }
726
727 static uint16_t lsp_refresh_time(struct isis_lsp *lsp, uint16_t rem_lifetime)
728 {
729 struct isis_area *area = lsp->area;
730 int level = lsp->level;
731 uint16_t refresh_time;
732
733 /* Add jitter to LSP refresh time */
734 refresh_time =
735 isis_jitter(area->lsp_refresh[level - 1], MAX_LSP_GEN_JITTER);
736
737 /* RFC 4444 : make sure the refresh time is at least less than 300
738 * of the remaining lifetime and more than gen interval */
739 if (refresh_time <= area->lsp_gen_interval[level - 1]
740 || refresh_time > (rem_lifetime - 300))
741 refresh_time = rem_lifetime - 300;
742
743 /* In cornercases, refresh_time might be <= lsp_gen_interval, however
744 * we accept this violation to satisfy refresh_time <= rem_lifetime -
745 * 300 */
746
747 return refresh_time;
748 }
749
750 static void lsp_build_ext_reach_ipv4(struct isis_lsp *lsp,
751 struct isis_area *area)
752 {
753 struct route_table *er_table = get_ext_reach(area, AF_INET, lsp->level);
754 if (!er_table)
755 return;
756
757 for (struct route_node *rn = route_top(er_table); rn;
758 rn = route_next(rn)) {
759 if (!rn->info)
760 continue;
761
762 struct prefix_ipv4 *ipv4 = (struct prefix_ipv4 *)&rn->p;
763 struct isis_ext_info *info = rn->info;
764
765 uint32_t metric = info->metric;
766 if (metric > MAX_WIDE_PATH_METRIC)
767 metric = MAX_WIDE_PATH_METRIC;
768 if (area->oldmetric && metric > 0x3f)
769 metric = 0x3f;
770
771 if (area->oldmetric)
772 isis_tlvs_add_oldstyle_ip_reach(lsp->tlvs, ipv4,
773 metric);
774 if (area->newmetric)
775 isis_tlvs_add_extended_ip_reach(lsp->tlvs, ipv4,
776 metric);
777 }
778 }
779
780 static void lsp_build_ext_reach_ipv6(struct isis_lsp *lsp,
781 struct isis_area *area)
782 {
783 struct route_table *er_table =
784 get_ext_reach(area, AF_INET6, lsp->level);
785 if (!er_table)
786 return;
787
788 for (struct route_node *rn = route_top(er_table); rn;
789 rn = srcdest_route_next(rn)) {
790 if (!rn->info)
791 continue;
792 struct isis_ext_info *info = rn->info;
793
794 struct prefix_ipv6 *p, *src_p;
795 srcdest_rnode_prefixes(rn, (const struct prefix **)&p,
796 (const struct prefix **)&src_p);
797
798 uint32_t metric = info->metric;
799 if (info->metric > MAX_WIDE_PATH_METRIC)
800 metric = MAX_WIDE_PATH_METRIC;
801
802 if (!src_p || !src_p->prefixlen) {
803 isis_tlvs_add_ipv6_reach(lsp->tlvs,
804 isis_area_ipv6_topology(area),
805 p, metric);
806 } else if (isis_area_ipv6_dstsrc_enabled(area)) {
807 isis_tlvs_add_ipv6_dstsrc_reach(lsp->tlvs,
808 ISIS_MT_IPV6_DSTSRC,
809 p, src_p, metric);
810 }
811 }
812 }
813
814 static void lsp_build_ext_reach(struct isis_lsp *lsp, struct isis_area *area)
815 {
816 lsp_build_ext_reach_ipv4(lsp, area);
817 lsp_build_ext_reach_ipv6(lsp, area);
818 }
819
820 static struct isis_lsp *lsp_next_frag(uint8_t frag_num, struct isis_lsp *lsp0,
821 struct isis_area *area, int level)
822 {
823 struct isis_lsp *lsp;
824 uint8_t frag_id[ISIS_SYS_ID_LEN + 2];
825
826 memcpy(frag_id, lsp0->hdr.lsp_id, ISIS_SYS_ID_LEN + 1);
827 LSP_FRAGMENT(frag_id) = frag_num;
828
829 lsp = lsp_search(frag_id, area->lspdb[level - 1]);
830 if (lsp) {
831 lsp_clear_data(lsp);
832 if (!lsp->lspu.zero_lsp)
833 lsp_link_fragment(lsp, lsp0);
834 return lsp;
835 }
836
837 lsp = lsp_new(area, frag_id, lsp0->hdr.rem_lifetime, 0,
838 lsp_bits_generate(level, area->overload_bit,
839 area->attached_bit),
840 0, lsp0, level);
841 lsp->own_lsp = 1;
842 lsp_insert(lsp, area->lspdb[level - 1]);
843 return lsp;
844 }
845
846 /*
847 * Builds the LSP data part. This func creates a new frag whenever
848 * area->lsp_frag_threshold is exceeded.
849 */
850 static void lsp_build(struct isis_lsp *lsp, struct isis_area *area)
851 {
852 int level = lsp->level;
853 char buf[PREFIX2STR_BUFFER];
854 struct listnode *node;
855 struct isis_lsp *frag;
856
857 lsp_clear_data(lsp);
858 for (ALL_LIST_ELEMENTS_RO(lsp->lspu.frags, node, frag))
859 lsp_clear_data(frag);
860
861 lsp->tlvs = isis_alloc_tlvs();
862 lsp_debug("ISIS (%s): Constructing local system LSP for level %d",
863 area->area_tag, level);
864
865 lsp->hdr.lsp_bits = lsp_bits_generate(level, area->overload_bit,
866 area->attached_bit);
867
868 lsp_add_auth(lsp);
869
870 isis_tlvs_add_area_addresses(lsp->tlvs, area->area_addrs);
871
872 /* Protocols Supported */
873 if (area->ip_circuits > 0 || area->ipv6_circuits > 0) {
874 struct nlpids nlpids = {.count = 0};
875 if (area->ip_circuits > 0) {
876 lsp_debug(
877 "ISIS (%s): Found IPv4 circuit, adding IPv4 to NLPIDs",
878 area->area_tag);
879 nlpids.nlpids[nlpids.count] = NLPID_IP;
880 nlpids.count++;
881 }
882 if (area->ipv6_circuits > 0) {
883 lsp_debug(
884 "ISIS (%s): Found IPv6 circuit, adding IPv6 to NLPIDs",
885 area->area_tag);
886 nlpids.nlpids[nlpids.count] = NLPID_IPV6;
887 nlpids.count++;
888 }
889 isis_tlvs_set_protocols_supported(lsp->tlvs, &nlpids);
890 }
891
892 if (area_is_mt(area)) {
893 lsp_debug("ISIS (%s): Adding MT router tlv...", area->area_tag);
894
895 struct isis_area_mt_setting **mt_settings;
896 unsigned int mt_count;
897
898 mt_settings = area_mt_settings(area, &mt_count);
899 for (unsigned int i = 0; i < mt_count; i++) {
900 isis_tlvs_add_mt_router_info(
901 lsp->tlvs, mt_settings[i]->mtid,
902 mt_settings[i]->overload, false);
903 lsp_debug("ISIS (%s): MT %s", area->area_tag,
904 isis_mtid2str(mt_settings[i]->mtid));
905 }
906 } else {
907 lsp_debug("ISIS (%s): Not adding MT router tlv (disabled)",
908 area->area_tag);
909 }
910 /* Dynamic Hostname */
911 if (area->dynhostname) {
912 isis_tlvs_set_dynamic_hostname(lsp->tlvs, cmd_hostname_get());
913 lsp_debug("ISIS (%s): Adding dynamic hostname '%s'",
914 area->area_tag, cmd_hostname_get());
915 } else {
916 lsp_debug("ISIS (%s): Not adding dynamic hostname (disabled)",
917 area->area_tag);
918 }
919
920 /* IPv4 address and TE router ID TLVs. In case of the first one we don't
921 * follow "C" vendor, but "J" vendor behavior - one IPv4 address is put
922 * into
923 * LSP and this address is same as router id. */
924 if (isis->router_id != 0) {
925 struct in_addr id = {.s_addr = isis->router_id};
926 inet_ntop(AF_INET, &id, buf, sizeof(buf));
927 lsp_debug("ISIS (%s): Adding router ID %s as IPv4 tlv.",
928 area->area_tag, buf);
929 isis_tlvs_add_ipv4_address(lsp->tlvs, &id);
930
931 /* Exactly same data is put into TE router ID TLV, but only if
932 * new style
933 * TLV's are in use. */
934 if (area->newmetric) {
935
936 lsp_debug(
937 "ISIS (%s): Adding router ID also as TE router ID tlv.",
938 area->area_tag);
939 isis_tlvs_set_te_router_id(lsp->tlvs, &id);
940 }
941 } else {
942 lsp_debug("ISIS (%s): Router ID is unset. Not adding tlv.",
943 area->area_tag);
944 }
945
946 lsp_debug("ISIS (%s): Adding circuit specific information.",
947 area->area_tag);
948
949 if (fabricd) {
950 lsp_debug(
951 "ISIS (%s): Adding tier %" PRIu8 " spine-leaf-extension tlv.",
952 area->area_tag, fabricd_tier(area));
953 isis_tlvs_add_spine_leaf(lsp->tlvs, fabricd_tier(area), true,
954 false, false, false);
955 }
956
957 struct isis_circuit *circuit;
958 for (ALL_LIST_ELEMENTS_RO(area->circuit_list, node, circuit)) {
959 if (!circuit->interface)
960 lsp_debug(
961 "ISIS (%s): Processing %s circuit %p with unknown interface",
962 area->area_tag,
963 circuit_type2string(circuit->circ_type),
964 circuit);
965 else
966 lsp_debug("ISIS (%s): Processing %s circuit %s",
967 area->area_tag,
968 circuit_type2string(circuit->circ_type),
969 circuit->interface->name);
970
971 if (circuit->state != C_STATE_UP) {
972 lsp_debug("ISIS (%s): Circuit is not up, ignoring.",
973 area->area_tag);
974 continue;
975 }
976
977 uint32_t metric = area->oldmetric
978 ? circuit->metric[level - 1]
979 : circuit->te_metric[level - 1];
980
981 if (circuit->ip_router && circuit->ip_addrs
982 && circuit->ip_addrs->count > 0) {
983 lsp_debug(
984 "ISIS (%s): Circuit has IPv4 active, adding respective TLVs.",
985 area->area_tag);
986 struct listnode *ipnode;
987 struct prefix_ipv4 *ipv4;
988 for (ALL_LIST_ELEMENTS_RO(circuit->ip_addrs, ipnode,
989 ipv4)) {
990 if (area->oldmetric) {
991 lsp_debug(
992 "ISIS (%s): Adding old-style IP reachability for %s",
993 area->area_tag,
994 prefix2str(ipv4, buf,
995 sizeof(buf)));
996 isis_tlvs_add_oldstyle_ip_reach(
997 lsp->tlvs, ipv4, metric);
998 }
999
1000 if (area->newmetric) {
1001 lsp_debug(
1002 "ISIS (%s): Adding te-style IP reachability for %s",
1003 area->area_tag,
1004 prefix2str(ipv4, buf,
1005 sizeof(buf)));
1006 isis_tlvs_add_extended_ip_reach(
1007 lsp->tlvs, ipv4, metric);
1008 }
1009 }
1010 }
1011
1012 if (circuit->ipv6_router && circuit->ipv6_non_link
1013 && circuit->ipv6_non_link->count > 0) {
1014 struct listnode *ipnode;
1015 struct prefix_ipv6 *ipv6;
1016 for (ALL_LIST_ELEMENTS_RO(circuit->ipv6_non_link,
1017 ipnode, ipv6)) {
1018 lsp_debug(
1019 "ISIS (%s): Adding IPv6 reachability for %s",
1020 area->area_tag,
1021 prefix2str(ipv6, buf, sizeof(buf)));
1022 isis_tlvs_add_ipv6_reach(
1023 lsp->tlvs,
1024 isis_area_ipv6_topology(area), ipv6,
1025 metric);
1026 }
1027 }
1028
1029 switch (circuit->circ_type) {
1030 case CIRCUIT_T_BROADCAST:
1031 if (level & circuit->is_type) {
1032 uint8_t *ne_id =
1033 (level == IS_LEVEL_1)
1034 ? circuit->u.bc.l1_desig_is
1035 : circuit->u.bc.l2_desig_is;
1036
1037 if (LSP_PSEUDO_ID(ne_id)) {
1038 if (area->oldmetric) {
1039 lsp_debug(
1040 "ISIS (%s): Adding DIS %s.%02x as old-style neighbor",
1041 area->area_tag,
1042 sysid_print(ne_id),
1043 LSP_PSEUDO_ID(ne_id));
1044 isis_tlvs_add_oldstyle_reach(
1045 lsp->tlvs, ne_id,
1046 metric);
1047 }
1048 if (area->newmetric) {
1049 uint8_t subtlvs[256];
1050 uint8_t subtlv_len;
1051
1052 if (IS_MPLS_TE(isisMplsTE)
1053 && circuit->interface
1054 && HAS_LINK_PARAMS(
1055 circuit->interface))
1056 subtlv_len = add_te_subtlvs(
1057 subtlvs,
1058 circuit->mtc);
1059 else
1060 subtlv_len = 0;
1061
1062 tlvs_add_mt_bcast(
1063 lsp->tlvs, circuit,
1064 level, ne_id, metric,
1065 subtlvs, subtlv_len);
1066 }
1067 }
1068 } else {
1069 lsp_debug(
1070 "ISIS (%s): Circuit is not active for current level. Not adding IS neighbors",
1071 area->area_tag);
1072 }
1073 break;
1074 case CIRCUIT_T_P2P: {
1075 struct isis_adjacency *nei = circuit->u.p2p.neighbor;
1076 if (nei && nei->adj_state == ISIS_ADJ_UP
1077 && (level & nei->circuit_t)) {
1078 uint8_t ne_id[7];
1079 memcpy(ne_id, nei->sysid, ISIS_SYS_ID_LEN);
1080 LSP_PSEUDO_ID(ne_id) = 0;
1081
1082 if (area->oldmetric) {
1083 lsp_debug(
1084 "ISIS (%s): Adding old-style is reach for %s",
1085 area->area_tag,
1086 sysid_print(ne_id));
1087 isis_tlvs_add_oldstyle_reach(
1088 lsp->tlvs, ne_id, metric);
1089 }
1090 if (area->newmetric) {
1091 uint8_t subtlvs[256];
1092 uint8_t subtlv_len;
1093
1094 if (IS_MPLS_TE(isisMplsTE)
1095 && circuit->interface != NULL
1096 && HAS_LINK_PARAMS(
1097 circuit->interface))
1098 /* Update Local and Remote IP
1099 * address for MPLS TE circuit
1100 * parameters */
1101 /* NOTE sure that it is the
1102 * pertinent place for that
1103 * updates */
1104 /* Local IP address could be
1105 * updated in isis_circuit.c -
1106 * isis_circuit_add_addr() */
1107 /* But, where update remote IP
1108 * address ? in isis_pdu.c -
1109 * process_p2p_hello() ? */
1110
1111 /* Add SubTLVs & Adjust real
1112 * size of SubTLVs */
1113 subtlv_len = add_te_subtlvs(
1114 subtlvs, circuit->mtc);
1115 else
1116 /* Or keep only TE metric with
1117 * no SubTLVs if MPLS_TE is off
1118 */
1119 subtlv_len = 0;
1120
1121 uint32_t neighbor_metric;
1122 if (fabricd_tier(area) == 0) {
1123 neighbor_metric = 0xffe;
1124 } else {
1125 neighbor_metric = metric;
1126 }
1127
1128 tlvs_add_mt_p2p(lsp->tlvs, circuit,
1129 ne_id, neighbor_metric,
1130 subtlvs, subtlv_len);
1131 }
1132 } else {
1133 lsp_debug(
1134 "ISIS (%s): No adjacency for given level on this circuit. Not adding IS neighbors",
1135 area->area_tag);
1136 }
1137 } break;
1138 case CIRCUIT_T_LOOPBACK:
1139 break;
1140 default:
1141 zlog_warn("lsp_area_create: unknown circuit type");
1142 }
1143 }
1144
1145 lsp_build_ext_reach(lsp, area);
1146
1147 struct isis_tlvs *tlvs = lsp->tlvs;
1148 lsp->tlvs = NULL;
1149
1150 lsp_pack_pdu(lsp);
1151 size_t tlv_space = STREAM_WRITEABLE(lsp->pdu) - LLC_LEN;
1152 lsp_clear_data(lsp);
1153
1154 struct list *fragments = isis_fragment_tlvs(tlvs, tlv_space);
1155 if (!fragments) {
1156 zlog_warn("BUG: could not fragment own LSP:");
1157 log_multiline(LOG_WARNING, " ", "%s",
1158 isis_format_tlvs(tlvs));
1159 isis_free_tlvs(tlvs);
1160 return;
1161 }
1162 isis_free_tlvs(tlvs);
1163
1164 bool fragment_overflow = false;
1165 frag = lsp;
1166 for (ALL_LIST_ELEMENTS_RO(fragments, node, tlvs)) {
1167 if (node != listhead(fragments)) {
1168 if (LSP_FRAGMENT(frag->hdr.lsp_id) == 255) {
1169 if (!fragment_overflow) {
1170 fragment_overflow = true;
1171 zlog_warn(
1172 "ISIS (%s): Too much information for 256 fragments",
1173 area->area_tag);
1174 }
1175 isis_free_tlvs(tlvs);
1176 continue;
1177 }
1178
1179 frag = lsp_next_frag(LSP_FRAGMENT(frag->hdr.lsp_id) + 1,
1180 lsp, area, level);
1181 }
1182 frag->tlvs = tlvs;
1183 }
1184
1185 list_delete(&fragments);
1186 lsp_debug("ISIS (%s): LSP construction is complete. Serializing...",
1187 area->area_tag);
1188 return;
1189 }
1190
1191 /*
1192 * 7.3.7 and 7.3.9 Generation on non-pseudonode LSPs
1193 */
1194 int lsp_generate(struct isis_area *area, int level)
1195 {
1196 struct isis_lsp *oldlsp, *newlsp;
1197 uint32_t seq_num = 0;
1198 uint8_t lspid[ISIS_SYS_ID_LEN + 2];
1199 uint16_t rem_lifetime, refresh_time;
1200
1201 if ((area == NULL) || (area->is_type & level) != level)
1202 return ISIS_ERROR;
1203
1204 memset(&lspid, 0, ISIS_SYS_ID_LEN + 2);
1205 memcpy(&lspid, isis->sysid, ISIS_SYS_ID_LEN);
1206
1207 /* only builds the lsp if the area shares the level */
1208 oldlsp = lsp_search(lspid, area->lspdb[level - 1]);
1209 if (oldlsp) {
1210 /* FIXME: we should actually initiate a purge */
1211 seq_num = oldlsp->hdr.seqno;
1212 lsp_search_and_destroy(oldlsp->hdr.lsp_id,
1213 area->lspdb[level - 1]);
1214 }
1215 rem_lifetime = lsp_rem_lifetime(area, level);
1216 newlsp =
1217 lsp_new(area, lspid, rem_lifetime, seq_num,
1218 area->is_type | area->overload_bit | area->attached_bit,
1219 0, NULL, level);
1220 newlsp->area = area;
1221 newlsp->own_lsp = 1;
1222
1223 lsp_insert(newlsp, area->lspdb[level - 1]);
1224 /* build_lsp_data (newlsp, area); */
1225 lsp_build(newlsp, area);
1226 /* time to calculate our checksum */
1227 lsp_seqno_update(newlsp);
1228 newlsp->last_generated = time(NULL);
1229 lsp_flood(newlsp, NULL);
1230
1231 refresh_time = lsp_refresh_time(newlsp, rem_lifetime);
1232
1233 THREAD_TIMER_OFF(area->t_lsp_refresh[level - 1]);
1234 area->lsp_regenerate_pending[level - 1] = 0;
1235 if (level == IS_LEVEL_1)
1236 thread_add_timer(master, lsp_l1_refresh, area, refresh_time,
1237 &area->t_lsp_refresh[level - 1]);
1238 else if (level == IS_LEVEL_2)
1239 thread_add_timer(master, lsp_l2_refresh, area, refresh_time,
1240 &area->t_lsp_refresh[level - 1]);
1241
1242 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
1243 zlog_debug("ISIS-Upd (%s): Building L%d LSP %s, len %" PRIu16
1244 ", seq 0x%08" PRIx32 ", cksum 0x%04" PRIx16
1245 ", lifetime %" PRIu16 "s refresh %" PRIu16 "s",
1246 area->area_tag, level,
1247 rawlspid_print(newlsp->hdr.lsp_id),
1248 newlsp->hdr.pdu_len, newlsp->hdr.seqno,
1249 newlsp->hdr.checksum, newlsp->hdr.rem_lifetime,
1250 refresh_time);
1251 }
1252 sched_debug(
1253 "ISIS (%s): Built L%d LSP. Set triggered regenerate to non-pending.",
1254 area->area_tag, level);
1255
1256 return ISIS_OK;
1257 }
1258
1259 /*
1260 * Search own LSPs, update holding time and flood
1261 */
1262 static int lsp_regenerate(struct isis_area *area, int level)
1263 {
1264 dict_t *lspdb;
1265 struct isis_lsp *lsp, *frag;
1266 struct listnode *node;
1267 uint8_t lspid[ISIS_SYS_ID_LEN + 2];
1268 uint16_t rem_lifetime, refresh_time;
1269
1270 if ((area == NULL) || (area->is_type & level) != level)
1271 return ISIS_ERROR;
1272
1273 lspdb = area->lspdb[level - 1];
1274
1275 memset(lspid, 0, ISIS_SYS_ID_LEN + 2);
1276 memcpy(lspid, isis->sysid, ISIS_SYS_ID_LEN);
1277
1278 lsp = lsp_search(lspid, lspdb);
1279
1280 if (!lsp) {
1281 flog_err(EC_LIB_DEVELOPMENT,
1282 "ISIS-Upd (%s): lsp_regenerate: no L%d LSP found!",
1283 area->area_tag, level);
1284 return ISIS_ERROR;
1285 }
1286
1287 lsp_clear_data(lsp);
1288 lsp_build(lsp, area);
1289 rem_lifetime = lsp_rem_lifetime(area, level);
1290 lsp->hdr.rem_lifetime = rem_lifetime;
1291 lsp->last_generated = time(NULL);
1292 lsp_flood(lsp, NULL);
1293 for (ALL_LIST_ELEMENTS_RO(lsp->lspu.frags, node, frag)) {
1294 frag->hdr.lsp_bits = lsp_bits_generate(
1295 level, area->overload_bit, area->attached_bit);
1296 /* Set the lifetime values of all the fragments to the same
1297 * value,
1298 * so that no fragment expires before the lsp is refreshed.
1299 */
1300 frag->hdr.rem_lifetime = rem_lifetime;
1301 frag->age_out = ZERO_AGE_LIFETIME;
1302 lsp_flood(frag, NULL);
1303 }
1304 lsp_seqno_update(lsp);
1305
1306 refresh_time = lsp_refresh_time(lsp, rem_lifetime);
1307 if (level == IS_LEVEL_1)
1308 thread_add_timer(master, lsp_l1_refresh, area, refresh_time,
1309 &area->t_lsp_refresh[level - 1]);
1310 else if (level == IS_LEVEL_2)
1311 thread_add_timer(master, lsp_l2_refresh, area, refresh_time,
1312 &area->t_lsp_refresh[level - 1]);
1313 area->lsp_regenerate_pending[level - 1] = 0;
1314
1315 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
1316 zlog_debug(
1317 "ISIS-Upd (%s): Refreshed our L%d LSP %s, len %" PRIu16
1318 ", seq 0x%08" PRIx32 ", cksum 0x%04" PRIx16
1319 ", lifetime %" PRIu16 "s refresh %" PRIu16 "s",
1320 area->area_tag, level, rawlspid_print(lsp->hdr.lsp_id),
1321 lsp->hdr.pdu_len, lsp->hdr.seqno, lsp->hdr.checksum,
1322 lsp->hdr.rem_lifetime, refresh_time);
1323 }
1324 sched_debug(
1325 "ISIS (%s): Rebuilt L%d LSP. Set triggered regenerate to non-pending.",
1326 area->area_tag, level);
1327
1328 return ISIS_OK;
1329 }
1330
1331 /*
1332 * Something has changed or periodic refresh -> regenerate LSP
1333 */
1334 static int lsp_l1_refresh(struct thread *thread)
1335 {
1336 struct isis_area *area;
1337
1338 area = THREAD_ARG(thread);
1339 assert(area);
1340
1341 area->t_lsp_refresh[0] = NULL;
1342 area->lsp_regenerate_pending[0] = 0;
1343
1344 if ((area->is_type & IS_LEVEL_1) == 0)
1345 return ISIS_ERROR;
1346
1347 sched_debug(
1348 "ISIS (%s): LSP L1 refresh timer expired. Refreshing LSP...",
1349 area->area_tag);
1350 return lsp_regenerate(area, IS_LEVEL_1);
1351 }
1352
1353 static int lsp_l2_refresh(struct thread *thread)
1354 {
1355 struct isis_area *area;
1356
1357 area = THREAD_ARG(thread);
1358 assert(area);
1359
1360 area->t_lsp_refresh[1] = NULL;
1361 area->lsp_regenerate_pending[1] = 0;
1362
1363 if ((area->is_type & IS_LEVEL_2) == 0)
1364 return ISIS_ERROR;
1365
1366 sched_debug(
1367 "ISIS (%s): LSP L2 refresh timer expired. Refreshing LSP...",
1368 area->area_tag);
1369 return lsp_regenerate(area, IS_LEVEL_2);
1370 }
1371
1372 int lsp_regenerate_schedule(struct isis_area *area, int level, int all_pseudo)
1373 {
1374 struct isis_lsp *lsp;
1375 uint8_t id[ISIS_SYS_ID_LEN + 2];
1376 time_t now, diff;
1377 long timeout;
1378 struct listnode *cnode;
1379 struct isis_circuit *circuit;
1380 int lvl;
1381
1382 if (area == NULL)
1383 return ISIS_ERROR;
1384
1385 sched_debug(
1386 "ISIS (%s): Scheduling regeneration of %s LSPs, %sincluding PSNs",
1387 area->area_tag, circuit_t2string(level),
1388 all_pseudo ? "" : "not ");
1389
1390 memcpy(id, isis->sysid, ISIS_SYS_ID_LEN);
1391 LSP_PSEUDO_ID(id) = LSP_FRAGMENT(id) = 0;
1392 now = time(NULL);
1393
1394 for (lvl = IS_LEVEL_1; lvl <= IS_LEVEL_2; lvl++) {
1395 if (!((level & lvl) && (area->is_type & lvl)))
1396 continue;
1397
1398 sched_debug(
1399 "ISIS (%s): Checking whether L%d needs to be scheduled",
1400 area->area_tag, lvl);
1401
1402 if (area->lsp_regenerate_pending[lvl - 1]) {
1403 struct timeval remain = thread_timer_remain(
1404 area->t_lsp_refresh[lvl - 1]);
1405 sched_debug(
1406 "ISIS (%s): Regeneration is already pending, nothing todo."
1407 " (Due in %lld.%03lld seconds)",
1408 area->area_tag, (long long)remain.tv_sec,
1409 (long long)remain.tv_usec / 1000);
1410 continue;
1411 }
1412
1413 lsp = lsp_search(id, area->lspdb[lvl - 1]);
1414 if (!lsp) {
1415 sched_debug(
1416 "ISIS (%s): We do not have any LSPs to regenerate, nothing todo.",
1417 area->area_tag);
1418 continue;
1419 }
1420
1421 /*
1422 * Throttle avoidance
1423 */
1424 sched_debug(
1425 "ISIS (%s): Will schedule regen timer. Last run was: %lld, Now is: %lld",
1426 area->area_tag, (long long)lsp->last_generated,
1427 (long long)now);
1428 THREAD_TIMER_OFF(area->t_lsp_refresh[lvl - 1]);
1429 diff = now - lsp->last_generated;
1430 if (diff < area->lsp_gen_interval[lvl - 1]) {
1431 timeout =
1432 1000 * (area->lsp_gen_interval[lvl - 1] - diff);
1433 sched_debug(
1434 "ISIS (%s): Scheduling in %ld ms to match configured lsp_gen_interval",
1435 area->area_tag, timeout);
1436 } else {
1437 /*
1438 * lsps are not regenerated if lsp_regenerate function
1439 * is called
1440 * directly. However if the lsp_regenerate call is
1441 * queued for
1442 * later execution it works.
1443 */
1444 timeout = 100;
1445 sched_debug(
1446 "ISIS (%s): Last generation was more than lsp_gen_interval ago."
1447 " Scheduling for execution in %ld ms.",
1448 area->area_tag, timeout);
1449 }
1450
1451 area->lsp_regenerate_pending[lvl - 1] = 1;
1452 if (lvl == IS_LEVEL_1) {
1453 thread_add_timer_msec(master, lsp_l1_refresh, area,
1454 timeout,
1455 &area->t_lsp_refresh[lvl - 1]);
1456 } else if (lvl == IS_LEVEL_2) {
1457 thread_add_timer_msec(master, lsp_l2_refresh, area,
1458 timeout,
1459 &area->t_lsp_refresh[lvl - 1]);
1460 }
1461 }
1462
1463 if (all_pseudo) {
1464 for (ALL_LIST_ELEMENTS_RO(area->circuit_list, cnode, circuit))
1465 lsp_regenerate_schedule_pseudo(circuit, level);
1466 }
1467
1468 return ISIS_OK;
1469 }
1470
1471 /*
1472 * Funcs for pseudonode LSPs
1473 */
1474
1475 /*
1476 * 7.3.8 and 7.3.10 Generation of level 1 and 2 pseudonode LSPs
1477 */
1478 static void lsp_build_pseudo(struct isis_lsp *lsp, struct isis_circuit *circuit,
1479 int level)
1480 {
1481 struct isis_adjacency *adj;
1482 struct list *adj_list;
1483 struct listnode *node;
1484 struct isis_area *area = circuit->area;
1485
1486 lsp_clear_data(lsp);
1487 lsp->tlvs = isis_alloc_tlvs();
1488 lsp_debug(
1489 "ISIS (%s): Constructing pseudo LSP %s for interface %s level %d",
1490 area->area_tag, rawlspid_print(lsp->hdr.lsp_id),
1491 circuit->interface->name, level);
1492
1493 lsp->level = level;
1494 /* RFC3787 section 4 SHOULD not set overload bit in pseudo LSPs */
1495 lsp->hdr.lsp_bits =
1496 lsp_bits_generate(level, 0, circuit->area->attached_bit);
1497
1498 /*
1499 * add self to IS neighbours
1500 */
1501 uint8_t ne_id[ISIS_SYS_ID_LEN + 1];
1502
1503 memcpy(ne_id, isis->sysid, ISIS_SYS_ID_LEN);
1504 LSP_PSEUDO_ID(ne_id) = 0;
1505
1506 if (circuit->area->oldmetric) {
1507 isis_tlvs_add_oldstyle_reach(lsp->tlvs, ne_id, 0);
1508 lsp_debug(
1509 "ISIS (%s): Adding %s.%02x as old-style neighbor (self)",
1510 area->area_tag, sysid_print(ne_id),
1511 LSP_PSEUDO_ID(ne_id));
1512 }
1513 if (circuit->area->newmetric) {
1514 isis_tlvs_add_extended_reach(lsp->tlvs, ISIS_MT_IPV4_UNICAST,
1515 ne_id, 0, NULL, 0);
1516 lsp_debug(
1517 "ISIS (%s): Adding %s.%02x as te-style neighbor (self)",
1518 area->area_tag, sysid_print(ne_id),
1519 LSP_PSEUDO_ID(ne_id));
1520 }
1521
1522 adj_list = list_new();
1523 isis_adj_build_up_list(circuit->u.bc.adjdb[level - 1], adj_list);
1524
1525 for (ALL_LIST_ELEMENTS_RO(adj_list, node, adj)) {
1526 if (!(adj->level & level)) {
1527 lsp_debug(
1528 "ISIS (%s): Ignoring neighbor %s, level does not intersect",
1529 area->area_tag, sysid_print(adj->sysid));
1530 continue;
1531 }
1532
1533 if (!(level == IS_LEVEL_1
1534 && adj->sys_type == ISIS_SYSTYPE_L1_IS)
1535 && !(level == IS_LEVEL_1
1536 && adj->sys_type == ISIS_SYSTYPE_L2_IS
1537 && adj->adj_usage == ISIS_ADJ_LEVEL1AND2)
1538 && !(level == IS_LEVEL_2
1539 && adj->sys_type == ISIS_SYSTYPE_L2_IS)) {
1540 lsp_debug(
1541 "ISIS (%s): Ignoring neighbor %s, level does not match",
1542 area->area_tag, sysid_print(adj->sysid));
1543 continue;
1544 }
1545
1546 memcpy(ne_id, adj->sysid, ISIS_SYS_ID_LEN);
1547 if (circuit->area->oldmetric) {
1548 isis_tlvs_add_oldstyle_reach(lsp->tlvs, ne_id, 0);
1549 lsp_debug(
1550 "ISIS (%s): Adding %s.%02x as old-style neighbor (peer)",
1551 area->area_tag, sysid_print(ne_id),
1552 LSP_PSEUDO_ID(ne_id));
1553 }
1554 if (circuit->area->newmetric) {
1555 isis_tlvs_add_extended_reach(lsp->tlvs,
1556 ISIS_MT_IPV4_UNICAST,
1557 ne_id, 0, NULL, 0);
1558 lsp_debug(
1559 "ISIS (%s): Adding %s.%02x as te-style neighbor (peer)",
1560 area->area_tag, sysid_print(ne_id),
1561 LSP_PSEUDO_ID(ne_id));
1562 }
1563 }
1564 list_delete(&adj_list);
1565 return;
1566 }
1567
1568 int lsp_generate_pseudo(struct isis_circuit *circuit, int level)
1569 {
1570 dict_t *lspdb = circuit->area->lspdb[level - 1];
1571 struct isis_lsp *lsp;
1572 uint8_t lsp_id[ISIS_SYS_ID_LEN + 2];
1573 uint16_t rem_lifetime, refresh_time;
1574
1575 if ((circuit->is_type & level) != level
1576 || (circuit->state != C_STATE_UP)
1577 || (circuit->circ_type != CIRCUIT_T_BROADCAST)
1578 || (circuit->u.bc.is_dr[level - 1] == 0))
1579 return ISIS_ERROR;
1580
1581 memcpy(lsp_id, isis->sysid, ISIS_SYS_ID_LEN);
1582 LSP_FRAGMENT(lsp_id) = 0;
1583 LSP_PSEUDO_ID(lsp_id) = circuit->circuit_id;
1584
1585 /*
1586 * If for some reason have a pseudo LSP in the db already -> regenerate
1587 */
1588 if (lsp_search(lsp_id, lspdb))
1589 return lsp_regenerate_schedule_pseudo(circuit, level);
1590
1591 rem_lifetime = lsp_rem_lifetime(circuit->area, level);
1592 /* RFC3787 section 4 SHOULD not set overload bit in pseudo LSPs */
1593 lsp = lsp_new(circuit->area, lsp_id, rem_lifetime, 1,
1594 circuit->area->is_type | circuit->area->attached_bit, 0,
1595 NULL, level);
1596 lsp->area = circuit->area;
1597
1598 lsp_build_pseudo(lsp, circuit, level);
1599 lsp_pack_pdu(lsp);
1600 lsp->own_lsp = 1;
1601 lsp_insert(lsp, lspdb);
1602 lsp_flood(lsp, NULL);
1603
1604 refresh_time = lsp_refresh_time(lsp, rem_lifetime);
1605 THREAD_TIMER_OFF(circuit->u.bc.t_refresh_pseudo_lsp[level - 1]);
1606 circuit->lsp_regenerate_pending[level - 1] = 0;
1607 if (level == IS_LEVEL_1)
1608 thread_add_timer(
1609 master, lsp_l1_refresh_pseudo, circuit, refresh_time,
1610 &circuit->u.bc.t_refresh_pseudo_lsp[level - 1]);
1611 else if (level == IS_LEVEL_2)
1612 thread_add_timer(
1613 master, lsp_l2_refresh_pseudo, circuit, refresh_time,
1614 &circuit->u.bc.t_refresh_pseudo_lsp[level - 1]);
1615
1616 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
1617 zlog_debug(
1618 "ISIS-Upd (%s): Built L%d Pseudo LSP %s, len %" PRIu16
1619 ", seq 0x%08" PRIx32 ", cksum 0x%04" PRIx16
1620 ", lifetime %" PRIu16 "s, refresh %" PRIu16 "s",
1621 circuit->area->area_tag, level,
1622 rawlspid_print(lsp->hdr.lsp_id), lsp->hdr.pdu_len,
1623 lsp->hdr.seqno, lsp->hdr.checksum,
1624 lsp->hdr.rem_lifetime, refresh_time);
1625 }
1626
1627 return ISIS_OK;
1628 }
1629
1630 static int lsp_regenerate_pseudo(struct isis_circuit *circuit, int level)
1631 {
1632 dict_t *lspdb = circuit->area->lspdb[level - 1];
1633 struct isis_lsp *lsp;
1634 uint8_t lsp_id[ISIS_SYS_ID_LEN + 2];
1635 uint16_t rem_lifetime, refresh_time;
1636
1637 if ((circuit->is_type & level) != level
1638 || (circuit->state != C_STATE_UP)
1639 || (circuit->circ_type != CIRCUIT_T_BROADCAST)
1640 || (circuit->u.bc.is_dr[level - 1] == 0))
1641 return ISIS_ERROR;
1642
1643 memcpy(lsp_id, isis->sysid, ISIS_SYS_ID_LEN);
1644 LSP_PSEUDO_ID(lsp_id) = circuit->circuit_id;
1645 LSP_FRAGMENT(lsp_id) = 0;
1646
1647 lsp = lsp_search(lsp_id, lspdb);
1648
1649 if (!lsp) {
1650 flog_err(EC_LIB_DEVELOPMENT,
1651 "lsp_regenerate_pseudo: no l%d LSP %s found!", level,
1652 rawlspid_print(lsp_id));
1653 return ISIS_ERROR;
1654 }
1655
1656 rem_lifetime = lsp_rem_lifetime(circuit->area, level);
1657 lsp->hdr.rem_lifetime = rem_lifetime;
1658 lsp_build_pseudo(lsp, circuit, level);
1659 lsp_inc_seqno(lsp, 0);
1660 lsp->last_generated = time(NULL);
1661 lsp_flood(lsp, NULL);
1662
1663 refresh_time = lsp_refresh_time(lsp, rem_lifetime);
1664 if (level == IS_LEVEL_1)
1665 thread_add_timer(
1666 master, lsp_l1_refresh_pseudo, circuit, refresh_time,
1667 &circuit->u.bc.t_refresh_pseudo_lsp[level - 1]);
1668 else if (level == IS_LEVEL_2)
1669 thread_add_timer(
1670 master, lsp_l2_refresh_pseudo, circuit, refresh_time,
1671 &circuit->u.bc.t_refresh_pseudo_lsp[level - 1]);
1672
1673 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
1674 zlog_debug(
1675 "ISIS-Upd (%s): Refreshed L%d Pseudo LSP %s, len %" PRIu16
1676 ", seq 0x%08" PRIx32 ", cksum 0x%04" PRIx16
1677 ", lifetime %" PRIu16 "s, refresh %" PRIu16 "s",
1678 circuit->area->area_tag, level,
1679 rawlspid_print(lsp->hdr.lsp_id), lsp->hdr.pdu_len,
1680 lsp->hdr.seqno, lsp->hdr.checksum,
1681 lsp->hdr.rem_lifetime, refresh_time);
1682 }
1683
1684 return ISIS_OK;
1685 }
1686
1687 /*
1688 * Something has changed or periodic refresh -> regenerate pseudo LSP
1689 */
1690 static int lsp_l1_refresh_pseudo(struct thread *thread)
1691 {
1692 struct isis_circuit *circuit;
1693 uint8_t id[ISIS_SYS_ID_LEN + 2];
1694
1695 circuit = THREAD_ARG(thread);
1696
1697 circuit->u.bc.t_refresh_pseudo_lsp[0] = NULL;
1698 circuit->lsp_regenerate_pending[0] = 0;
1699
1700 if ((circuit->u.bc.is_dr[0] == 0)
1701 || (circuit->is_type & IS_LEVEL_1) == 0) {
1702 memcpy(id, isis->sysid, ISIS_SYS_ID_LEN);
1703 LSP_PSEUDO_ID(id) = circuit->circuit_id;
1704 LSP_FRAGMENT(id) = 0;
1705 lsp_purge_pseudo(id, circuit, IS_LEVEL_1);
1706 return ISIS_ERROR;
1707 }
1708
1709 return lsp_regenerate_pseudo(circuit, IS_LEVEL_1);
1710 }
1711
1712 static int lsp_l2_refresh_pseudo(struct thread *thread)
1713 {
1714 struct isis_circuit *circuit;
1715 uint8_t id[ISIS_SYS_ID_LEN + 2];
1716
1717 circuit = THREAD_ARG(thread);
1718
1719 circuit->u.bc.t_refresh_pseudo_lsp[1] = NULL;
1720 circuit->lsp_regenerate_pending[1] = 0;
1721
1722 if ((circuit->u.bc.is_dr[1] == 0)
1723 || (circuit->is_type & IS_LEVEL_2) == 0) {
1724 memcpy(id, isis->sysid, ISIS_SYS_ID_LEN);
1725 LSP_PSEUDO_ID(id) = circuit->circuit_id;
1726 LSP_FRAGMENT(id) = 0;
1727 lsp_purge_pseudo(id, circuit, IS_LEVEL_2);
1728 return ISIS_ERROR;
1729 }
1730
1731 return lsp_regenerate_pseudo(circuit, IS_LEVEL_2);
1732 }
1733
1734 int lsp_regenerate_schedule_pseudo(struct isis_circuit *circuit, int level)
1735 {
1736 struct isis_lsp *lsp;
1737 uint8_t lsp_id[ISIS_SYS_ID_LEN + 2];
1738 time_t now, diff;
1739 long timeout;
1740 int lvl;
1741 struct isis_area *area = circuit->area;
1742
1743 if (circuit->circ_type != CIRCUIT_T_BROADCAST
1744 || circuit->state != C_STATE_UP)
1745 return ISIS_OK;
1746
1747 sched_debug(
1748 "ISIS (%s): Scheduling regeneration of %s pseudo LSP for interface %s",
1749 area->area_tag, circuit_t2string(level),
1750 circuit->interface->name);
1751
1752 memcpy(lsp_id, isis->sysid, ISIS_SYS_ID_LEN);
1753 LSP_PSEUDO_ID(lsp_id) = circuit->circuit_id;
1754 LSP_FRAGMENT(lsp_id) = 0;
1755 now = time(NULL);
1756
1757 for (lvl = IS_LEVEL_1; lvl <= IS_LEVEL_2; lvl++) {
1758 sched_debug(
1759 "ISIS (%s): Checking whether L%d pseudo LSP needs to be scheduled",
1760 area->area_tag, lvl);
1761
1762 if (!((level & lvl) && (circuit->is_type & lvl))) {
1763 sched_debug("ISIS (%s): Level is not active on circuit",
1764 area->area_tag);
1765 continue;
1766 }
1767
1768 if (circuit->u.bc.is_dr[lvl - 1] == 0) {
1769 sched_debug(
1770 "ISIS (%s): This IS is not DR, nothing to do.",
1771 area->area_tag);
1772 continue;
1773 }
1774
1775 if (circuit->lsp_regenerate_pending[lvl - 1]) {
1776 struct timeval remain = thread_timer_remain(
1777 circuit->u.bc.t_refresh_pseudo_lsp[lvl - 1]);
1778 sched_debug(
1779 "ISIS (%s): Regenerate is already pending, nothing todo."
1780 " (Due in %lld.%03lld seconds)",
1781 area->area_tag, (long long)remain.tv_sec,
1782 (long long)remain.tv_usec / 1000);
1783 continue;
1784 }
1785
1786 lsp = lsp_search(lsp_id, circuit->area->lspdb[lvl - 1]);
1787 if (!lsp) {
1788 sched_debug(
1789 "ISIS (%s): Pseudonode LSP does not exist yet, nothing to regenerate.",
1790 area->area_tag);
1791 continue;
1792 }
1793
1794 /*
1795 * Throttle avoidance
1796 */
1797 sched_debug(
1798 "ISIS (%s): Will schedule PSN regen timer. Last run was: %lld, Now is: %lld",
1799 area->area_tag, (long long)lsp->last_generated,
1800 (long long)now);
1801 THREAD_TIMER_OFF(circuit->u.bc.t_refresh_pseudo_lsp[lvl - 1]);
1802 diff = now - lsp->last_generated;
1803 if (diff < circuit->area->lsp_gen_interval[lvl - 1]) {
1804 timeout =
1805 1000 * (circuit->area->lsp_gen_interval[lvl - 1]
1806 - diff);
1807 sched_debug(
1808 "ISIS (%s): Sechduling in %ld ms to match configured lsp_gen_interval",
1809 area->area_tag, timeout);
1810 } else {
1811 timeout = 100;
1812 sched_debug(
1813 "ISIS (%s): Last generation was more than lsp_gen_interval ago."
1814 " Scheduling for execution in %ld ms.",
1815 area->area_tag, timeout);
1816 }
1817
1818 circuit->lsp_regenerate_pending[lvl - 1] = 1;
1819
1820 if (lvl == IS_LEVEL_1) {
1821 thread_add_timer_msec(
1822 master, lsp_l1_refresh_pseudo, circuit, timeout,
1823 &circuit->u.bc.t_refresh_pseudo_lsp[lvl - 1]);
1824 } else if (lvl == IS_LEVEL_2) {
1825 thread_add_timer_msec(
1826 master, lsp_l2_refresh_pseudo, circuit, timeout,
1827 &circuit->u.bc.t_refresh_pseudo_lsp[lvl - 1]);
1828 }
1829 }
1830
1831 return ISIS_OK;
1832 }
1833
1834 /*
1835 * Walk through LSPs for an area
1836 * - set remaining lifetime
1837 */
1838 int lsp_tick(struct thread *thread)
1839 {
1840 struct isis_area *area;
1841 struct isis_lsp *lsp;
1842 dnode_t *dnode, *dnode_next;
1843 int level;
1844 uint16_t rem_lifetime;
1845 bool fabricd_sync_incomplete = false;
1846
1847 area = THREAD_ARG(thread);
1848 assert(area);
1849 area->t_tick = NULL;
1850 thread_add_timer(master, lsp_tick, area, 1, &area->t_tick);
1851
1852 struct isis_circuit *fabricd_init_c = fabricd_initial_sync_circuit(area);
1853
1854 /*
1855 * Remove LSPs which have aged out
1856 */
1857 for (level = 0; level < ISIS_LEVELS; level++) {
1858 if (area->lspdb[level] && dict_count(area->lspdb[level]) > 0) {
1859 for (dnode = dict_first(area->lspdb[level]);
1860 dnode != NULL; dnode = dnode_next) {
1861 dnode_next =
1862 dict_next(area->lspdb[level], dnode);
1863 lsp = dnode_get(dnode);
1864
1865 /*
1866 * The lsp rem_lifetime is kept at 0 for MaxAge
1867 * or
1868 * ZeroAgeLifetime depending on explicit purge
1869 * or
1870 * natural age out. So schedule spf only once
1871 * when
1872 * the first time rem_lifetime becomes 0.
1873 */
1874 rem_lifetime = lsp->hdr.rem_lifetime;
1875 lsp_set_time(lsp);
1876
1877 /*
1878 * Schedule may run spf which should be done
1879 * only after
1880 * the lsp rem_lifetime becomes 0 for the first
1881 * time.
1882 * ISO 10589 - 7.3.16.4 first paragraph.
1883 */
1884 if (rem_lifetime == 1 && lsp->hdr.seqno != 0) {
1885 /* 7.3.16.4 a) set SRM flags on all */
1886 /* 7.3.16.4 b) retain only the header */
1887 if (lsp->area->purge_originator)
1888 lsp_purge(lsp, lsp->level, NULL);
1889 else
1890 lsp_flood(lsp, NULL);
1891 /* 7.3.16.4 c) record the time to purge
1892 * FIXME */
1893 isis_spf_schedule(lsp->area, lsp->level);
1894 }
1895
1896 if (lsp->age_out == 0) {
1897 zlog_debug(
1898 "ISIS-Upd (%s): L%u LSP %s seq "
1899 "0x%08" PRIx32 " aged out",
1900 area->area_tag, lsp->level,
1901 rawlspid_print(lsp->hdr.lsp_id),
1902 lsp->hdr.seqno);
1903 lsp_destroy(lsp);
1904 lsp = NULL;
1905 dict_delete_free(area->lspdb[level],
1906 dnode);
1907 }
1908
1909 if (fabricd_init_c && lsp) {
1910 fabricd_sync_incomplete |=
1911 ISIS_CHECK_FLAG(lsp->SSNflags,
1912 fabricd_init_c);
1913 }
1914 }
1915 }
1916 }
1917
1918 if (fabricd_init_c
1919 && !fabricd_sync_incomplete
1920 && !isis_tx_queue_len(fabricd_init_c->tx_queue)) {
1921 fabricd_initial_sync_finish(area);
1922 }
1923
1924 return ISIS_OK;
1925 }
1926
1927 void lsp_purge_pseudo(uint8_t *id, struct isis_circuit *circuit, int level)
1928 {
1929 struct isis_lsp *lsp;
1930
1931 lsp = lsp_search(id, circuit->area->lspdb[level - 1]);
1932 if (!lsp)
1933 return;
1934
1935 lsp_purge(lsp, level, NULL);
1936 }
1937
1938 /*
1939 * Purge own LSP that is received and we don't have.
1940 * -> Do as in 7.3.16.4
1941 */
1942 void lsp_purge_non_exist(int level, struct isis_lsp_hdr *hdr,
1943 struct isis_area *area)
1944 {
1945 struct isis_lsp *lsp;
1946
1947 /*
1948 * We need to create the LSP to be purged
1949 */
1950 lsp = XCALLOC(MTYPE_ISIS_LSP, sizeof(struct isis_lsp));
1951 lsp->area = area;
1952 lsp->level = level;
1953 lsp->pdu = stream_new(LLC_LEN + area->lsp_mtu);
1954 lsp->age_out = ZERO_AGE_LIFETIME;
1955
1956 memcpy(&lsp->hdr, hdr, sizeof(lsp->hdr));
1957 lsp->hdr.rem_lifetime = 0;
1958
1959 lsp_purge_add_poi(lsp, NULL);
1960
1961 lsp_pack_pdu(lsp);
1962
1963 lsp_insert(lsp, area->lspdb[lsp->level - 1]);
1964 lsp_flood(lsp, NULL);
1965
1966 return;
1967 }
1968
1969 void lsp_set_all_srmflags(struct isis_lsp *lsp, bool set)
1970 {
1971 struct listnode *node;
1972 struct isis_circuit *circuit;
1973
1974 assert(lsp);
1975
1976 if (!lsp->area)
1977 return;
1978
1979 struct list *circuit_list = lsp->area->circuit_list;
1980 for (ALL_LIST_ELEMENTS_RO(circuit_list, node, circuit)) {
1981 if (set) {
1982 isis_tx_queue_add(circuit->tx_queue, lsp,
1983 TX_LSP_NORMAL);
1984 } else {
1985 isis_tx_queue_del(circuit->tx_queue, lsp);
1986 }
1987 }
1988 }
1989
1990 void lsp_flood(struct isis_lsp *lsp, struct isis_circuit *circuit)
1991 {
1992 if (!fabricd) {
1993 lsp_set_all_srmflags(lsp, true);
1994 if (circuit)
1995 isis_tx_queue_del(circuit->tx_queue, lsp);
1996 } else {
1997 fabricd_lsp_flood(lsp);
1998 }
1999 }