forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisis_lsp.c
2542 lines (2172 loc) · 66.7 KB
/
isis_lsp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* IS-IS Rout(e)ing protocol - isis_lsp.c
* LSP processing
*
* Copyright (C) 2001,2002 Sampo Saaristo
* Tampere University of Technology
* Institute of Communications Engineering
* Copyright (C) 2013-2015 Christian Franke <chris@opensourcerouting.org>
*/
#include <zebra.h>
#include "linklist.h"
#include "frrevent.h"
#include "vty.h"
#include "stream.h"
#include "memory.h"
#include "log.h"
#include "prefix.h"
#include "command.h"
#include "hash.h"
#include "if.h"
#include "checksum.h"
#include "md5.h"
#include "table.h"
#include "srcdest_table.h"
#include "lib_errors.h"
#include "isisd/isis_constants.h"
#include "isisd/isis_common.h"
#include "isisd/isis_flags.h"
#include "isisd/isis_circuit.h"
#include "isisd/isisd.h"
#include "isisd/isis_lsp.h"
#include "isisd/isis_pdu.h"
#include "isisd/isis_dynhn.h"
#include "isisd/isis_misc.h"
#include "isisd/isis_csm.h"
#include "isisd/isis_adjacency.h"
#include "isisd/isis_spf.h"
#include "isisd/isis_mt.h"
#include "isisd/isis_tlvs.h"
#include "isisd/isis_te.h"
#include "isisd/isis_sr.h"
#include "isisd/fabricd.h"
#include "isisd/isis_tx_queue.h"
#include "isisd/isis_nb.h"
#include "isisd/isis_flex_algo.h"
DEFINE_MTYPE_STATIC(ISISD, ISIS_LSP, "ISIS LSP");
static void lsp_refresh(struct event *thread);
static void lsp_l1_refresh_pseudo(struct event *thread);
static void lsp_l2_refresh_pseudo(struct event *thread);
static void lsp_destroy(struct isis_lsp *lsp);
static bool device_startup;
int lsp_id_cmp(uint8_t *id1, uint8_t *id2)
{
return memcmp(id1, id2, ISIS_SYS_ID_LEN + 2);
}
int lspdb_compare(const struct isis_lsp *a, const struct isis_lsp *b)
{
return memcmp(a->hdr.lsp_id, b->hdr.lsp_id, sizeof(a->hdr.lsp_id));
}
void lsp_db_init(struct lspdb_head *head)
{
lspdb_init(head);
}
void lsp_db_fini(struct lspdb_head *head)
{
struct isis_lsp *lsp;
while ((lsp = lspdb_pop(head)))
lsp_destroy(lsp);
lspdb_fini(head);
}
struct isis_lsp *lsp_search(struct lspdb_head *head, const uint8_t *id)
{
struct isis_lsp searchfor;
memcpy(searchfor.hdr.lsp_id, id, sizeof(searchfor.hdr.lsp_id));
return lspdb_find(head, &searchfor);
}
static void lsp_clear_data(struct isis_lsp *lsp)
{
if (!lsp)
return;
isis_free_tlvs(lsp->tlvs);
lsp->tlvs = NULL;
}
static void lsp_remove_frags(struct lspdb_head *head, struct list *frags);
static void lsp_destroy(struct isis_lsp *lsp)
{
struct listnode *cnode;
struct isis_circuit *circuit;
if (!lsp)
return;
for (ALL_LIST_ELEMENTS_RO(lsp->area->circuit_list, cnode, circuit))
isis_tx_queue_del(circuit->tx_queue, lsp);
ISIS_FLAGS_CLEAR_ALL(lsp->SSNflags);
isis_te_lsp_event(lsp, LSP_DEL);
lsp_clear_data(lsp);
if (!LSP_FRAGMENT(lsp->hdr.lsp_id)) {
/* Only non-pseudo nodes and non-fragment LSPs can delete nodes. */
if (!LSP_PSEUDO_ID(lsp->hdr.lsp_id))
isis_dynhn_remove(lsp->area->isis, lsp->hdr.lsp_id);
if (lsp->lspu.frags) {
lsp_remove_frags(&lsp->area->lspdb[lsp->level - 1],
lsp->lspu.frags);
list_delete(&lsp->lspu.frags);
}
} else {
if (lsp->lspu.zero_lsp
&& lsp->lspu.zero_lsp->lspu.frags) {
listnode_delete(lsp->lspu.zero_lsp->lspu.frags, lsp);
}
}
isis_spf_schedule(lsp->area, lsp->level);
if (lsp->pdu)
stream_free(lsp->pdu);
fabricd_lsp_free(lsp);
XFREE(MTYPE_ISIS_LSP, lsp);
}
/*
* Remove all the frags belonging to the given lsp
*/
static void lsp_remove_frags(struct lspdb_head *head, struct list *frags)
{
struct listnode *lnode, *lnnode;
struct isis_lsp *lsp;
for (ALL_LIST_ELEMENTS(frags, lnode, lnnode, lsp)) {
lsp = lsp_search(head, lsp->hdr.lsp_id);
lspdb_del(head, lsp);
lsp_destroy(lsp);
}
}
void lsp_search_and_destroy(struct lspdb_head *head, const uint8_t *id)
{
struct isis_lsp *lsp;
lsp = lsp_search(head, id);
if (lsp) {
lspdb_del(head, lsp);
/*
* If this is a zero lsp, remove all the frags now
*/
if (LSP_FRAGMENT(lsp->hdr.lsp_id) == 0) {
if (lsp->lspu.frags)
lsp_remove_frags(head, lsp->lspu.frags);
} else {
/*
* else just remove this frag, from the zero lsps' frag
* list
*/
if (lsp->lspu.zero_lsp
&& lsp->lspu.zero_lsp->lspu.frags)
listnode_delete(lsp->lspu.zero_lsp->lspu.frags,
lsp);
}
lsp_destroy(lsp);
}
}
/*
* Compares a LSP to given values
* Params are given in net order
*/
int lsp_compare(char *areatag, struct isis_lsp *lsp, uint32_t seqno,
uint16_t checksum, uint16_t rem_lifetime)
{
if (lsp->hdr.seqno == seqno && lsp->hdr.checksum == checksum
&& ((lsp->hdr.rem_lifetime == 0 && rem_lifetime == 0)
|| (lsp->hdr.rem_lifetime != 0 && rem_lifetime != 0))) {
if (IS_DEBUG_SNP_PACKETS) {
zlog_debug(
"ISIS-Snp (%s): Compare LSP %pLS seq 0x%08x, cksum 0x%04hx, lifetime %hus",
areatag, lsp->hdr.lsp_id, lsp->hdr.seqno,
lsp->hdr.checksum, lsp->hdr.rem_lifetime);
zlog_debug(
"ISIS-Snp (%s): is equal to ours seq 0x%08x, cksum 0x%04hx, lifetime %hus",
areatag, seqno, checksum, rem_lifetime);
}
return LSP_EQUAL;
}
/*
* LSPs with identical checksums should only be treated as newer if:
* a) The current LSP has a remaining lifetime != 0 and the other LSP
* has a
* remaining lifetime == 0. In this case, we should participate in
* the purge
* and should not treat the current LSP with remaining lifetime == 0
* as older.
* b) The LSP has an incorrect checksum. In this case, we need to react
* as given
* in 7.3.16.2.
*/
if (seqno > lsp->hdr.seqno
|| (seqno == lsp->hdr.seqno
&& ((lsp->hdr.rem_lifetime != 0 && rem_lifetime == 0)
|| (lsp->hdr.checksum != checksum
&& lsp->hdr.rem_lifetime)))) {
if (IS_DEBUG_SNP_PACKETS) {
zlog_debug(
"ISIS-Snp (%s): Compare LSP %pLS seq 0x%08x, cksum 0x%04hx, lifetime %hus",
areatag, lsp->hdr.lsp_id, seqno, checksum,
rem_lifetime);
zlog_debug(
"ISIS-Snp (%s): is newer than ours seq 0x%08x, cksum 0x%04hx, lifetime %hus",
areatag, lsp->hdr.seqno, lsp->hdr.checksum,
lsp->hdr.rem_lifetime);
}
return LSP_NEWER;
}
if (IS_DEBUG_SNP_PACKETS) {
zlog_debug(
"ISIS-Snp (%s): Compare LSP %pLS seq 0x%08x, cksum 0x%04hx, lifetime %hus",
areatag, lsp->hdr.lsp_id, seqno, checksum,
rem_lifetime);
zlog_debug(
"ISIS-Snp (%s): is older than ours seq 0x%08x, cksum 0x%04hx, lifetime %hus",
areatag, lsp->hdr.seqno, lsp->hdr.checksum,
lsp->hdr.rem_lifetime);
}
return LSP_OLDER;
}
static void put_lsp_hdr(struct isis_lsp *lsp, size_t *len_pointer, bool keep)
{
uint8_t pdu_type =
(lsp->level == IS_LEVEL_1) ? L1_LINK_STATE : L2_LINK_STATE;
struct isis_lsp_hdr *hdr = &lsp->hdr;
struct stream *stream = lsp->pdu;
size_t orig_getp = 0, orig_endp = 0;
if (keep) {
orig_getp = stream_get_getp(lsp->pdu);
orig_endp = stream_get_endp(lsp->pdu);
}
stream_set_getp(lsp->pdu, 0);
stream_set_endp(lsp->pdu, 0);
fill_fixed_hdr(pdu_type, stream);
if (len_pointer)
*len_pointer = stream_get_endp(stream);
stream_putw(stream, hdr->pdu_len);
stream_putw(stream, hdr->rem_lifetime);
stream_put(stream, hdr->lsp_id, sizeof(hdr->lsp_id));
stream_putl(stream, hdr->seqno);
stream_putw(stream, hdr->checksum);
stream_putc(stream, hdr->lsp_bits);
if (keep) {
stream_set_endp(lsp->pdu, orig_endp);
stream_set_getp(lsp->pdu, orig_getp);
}
}
static void lsp_add_auth(struct isis_lsp *lsp)
{
struct isis_passwd *passwd;
passwd = (lsp->level == IS_LEVEL_1) ? &lsp->area->area_passwd
: &lsp->area->domain_passwd;
isis_tlvs_add_auth(lsp->tlvs, passwd);
}
static void lsp_pack_pdu(struct isis_lsp *lsp)
{
if (!lsp->tlvs)
lsp->tlvs = isis_alloc_tlvs();
lsp_add_auth(lsp);
size_t len_pointer;
put_lsp_hdr(lsp, &len_pointer, false);
isis_pack_tlvs(lsp->tlvs, lsp->pdu, len_pointer, false, true);
lsp->hdr.pdu_len = stream_get_endp(lsp->pdu);
lsp->hdr.checksum =
ntohs(fletcher_checksum(STREAM_DATA(lsp->pdu) + 12,
stream_get_endp(lsp->pdu) - 12, 12));
}
void lsp_inc_seqno(struct isis_lsp *lsp, uint32_t seqno)
{
uint32_t newseq;
if (seqno == 0 || lsp->hdr.seqno > seqno)
newseq = lsp->hdr.seqno + 1;
else
newseq = seqno + 1;
#ifndef FABRICD
/* check for overflow */
if (newseq < lsp->hdr.seqno) {
/* send northbound notification */
lsp->area->lsp_exceeded_max_counter++;
isis_notif_lsp_exceed_max(lsp->area, lsp->hdr.lsp_id);
}
#endif /* ifndef FABRICD */
lsp->hdr.seqno = newseq;
lsp_pack_pdu(lsp);
isis_spf_schedule(lsp->area, lsp->level);
isis_te_lsp_event(lsp, LSP_INC);
}
static void lsp_purge_add_poi(struct isis_lsp *lsp,
const uint8_t *sender)
{
if (lsp->area == NULL)
return;
if (!lsp->area->purge_originator)
return;
/* add purge originator identification */
if (!lsp->tlvs)
lsp->tlvs = isis_alloc_tlvs();
isis_tlvs_set_purge_originator(lsp->tlvs, lsp->area->isis->sysid,
sender);
isis_tlvs_set_dynamic_hostname(lsp->tlvs, cmd_hostname_get());
}
static void lsp_purge(struct isis_lsp *lsp, int level,
const uint8_t *sender)
{
/* reset stream */
lsp_clear_data(lsp);
stream_reset(lsp->pdu);
/* update header */
lsp->hdr.checksum = 0;
lsp->hdr.rem_lifetime = 0;
lsp->level = level;
lsp->age_out = lsp->area->max_lsp_lifetime[level - 1];
lsp->area->lsp_purge_count[level - 1]++;
lsp_purge_add_poi(lsp, sender);
lsp_pack_pdu(lsp);
lsp_flood(lsp, NULL);
}
/*
* Generates checksum for LSP and its frags
*/
static void lsp_seqno_update(struct isis_lsp *lsp0)
{
struct isis_lsp *lsp;
struct listnode *node;
lsp_inc_seqno(lsp0, 0);
if (!lsp0->lspu.frags)
return;
for (ALL_LIST_ELEMENTS_RO(lsp0->lspu.frags, node, lsp)) {
if (lsp->tlvs)
lsp_inc_seqno(lsp, 0);
else if (lsp->hdr.rem_lifetime) {
/* Purge should only be applied when the fragment has
* non-zero remaining lifetime.
*/
lsp_purge(lsp, lsp0->level, NULL);
}
}
return;
}
bool isis_level2_adj_up(struct isis_area *area)
{
struct listnode *node, *cnode;
struct isis_circuit *circuit;
struct list *adjdb;
struct isis_adjacency *adj;
if (area->is_type == IS_LEVEL_1)
return false;
for (ALL_LIST_ELEMENTS_RO(area->circuit_list, cnode, circuit)) {
if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
adjdb = circuit->u.bc.adjdb[1];
if (!adjdb || !adjdb->count)
continue;
for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj)) {
if (adj->level != ISIS_ADJ_LEVEL1
&& adj->adj_state == ISIS_ADJ_UP)
return true;
}
} else if (circuit->circ_type == CIRCUIT_T_P2P
&& circuit->u.p2p.neighbor) {
adj = circuit->u.p2p.neighbor;
if (adj->level != ISIS_ADJ_LEVEL1
&& adj->adj_state == ISIS_ADJ_UP)
return true;
}
}
return false;
}
/*
* Unset the overload bit after the timer expires
*/
void set_overload_on_start_timer(struct event *thread)
{
struct isis_area *area = EVENT_ARG(thread);
assert(area);
area->t_overload_on_startup_timer = NULL;
/* Check if set-overload-bit is not currently configured */
if (!area->overload_configured)
isis_area_overload_bit_set(area, false);
}
static uint8_t lsp_bits_generate(int level, int overload_bit, int attached_bit,
struct isis_area *area)
{
uint8_t lsp_bits = 0;
if (area->is_type == IS_LEVEL_1)
lsp_bits = IS_LEVEL_1;
else
lsp_bits = IS_LEVEL_1_AND_2;
if (overload_bit)
lsp_bits |= overload_bit;
/* only set the attach bit if we are a level-1-2 router and this is
* a level-1 LSP and we have a level-2 adjacency up from another area
*/
if (area->is_type == IS_LEVEL_1_AND_2 && level == IS_LEVEL_1
&& attached_bit && isis_level2_adj_up(area))
lsp_bits |= LSPBIT_ATT;
return lsp_bits;
}
static void lsp_update_data(struct isis_lsp *lsp, struct isis_lsp_hdr *hdr,
struct isis_tlvs *tlvs, struct stream *stream,
struct isis_area *area, int level)
{
/* free the old lsp data */
lsp_clear_data(lsp);
/* copying only the relevant part of our stream */
if (lsp->pdu != NULL)
stream_free(lsp->pdu);
lsp->pdu = stream_dup(stream);
memcpy(&lsp->hdr, hdr, sizeof(lsp->hdr));
lsp->area = area;
lsp->level = level;
lsp->age_out = ZERO_AGE_LIFETIME;
lsp->installed = time(NULL);
lsp->tlvs = tlvs;
if (area->dynhostname && lsp->hdr.rem_lifetime) {
if (lsp->tlvs->hostname) {
isis_dynhn_insert(area->isis, lsp->hdr.lsp_id,
lsp->tlvs->hostname,
(lsp->hdr.lsp_bits & LSPBIT_IST) ==
IS_LEVEL_1_AND_2
? IS_LEVEL_2
: IS_LEVEL_1);
} else {
if (!LSP_PSEUDO_ID(lsp->hdr.lsp_id) &&
!LSP_FRAGMENT(lsp->hdr.lsp_id))
isis_dynhn_remove(area->isis, lsp->hdr.lsp_id);
}
}
return;
}
static void lsp_link_fragment(struct isis_lsp *lsp, struct isis_lsp *lsp0)
{
if (!LSP_FRAGMENT(lsp->hdr.lsp_id)) {
/* zero lsp -> create list to store fragments */
lsp->lspu.frags = list_new();
} else {
/* fragment -> set backpointer and add to zero lsps list */
assert(lsp0);
lsp->lspu.zero_lsp = lsp0;
listnode_add(lsp0->lspu.frags, lsp);
}
}
void lsp_update(struct isis_lsp *lsp, struct isis_lsp_hdr *hdr,
struct isis_tlvs *tlvs, struct stream *stream,
struct isis_area *area, int level, bool confusion)
{
if (lsp->own_lsp) {
flog_err(
EC_LIB_DEVELOPMENT,
"ISIS-Upd (%s): BUG updating LSP %pLS still marked as own LSP",
area->area_tag, lsp->hdr.lsp_id);
lsp_clear_data(lsp);
lsp->own_lsp = 0;
}
if (confusion) {
lsp_purge(lsp, level, NULL);
} else {
lsp_update_data(lsp, hdr, tlvs, stream, area, level);
}
if (LSP_FRAGMENT(lsp->hdr.lsp_id) && !lsp->lspu.zero_lsp) {
uint8_t lspid[ISIS_SYS_ID_LEN + 2];
struct isis_lsp *lsp0;
memcpy(lspid, lsp->hdr.lsp_id, ISIS_SYS_ID_LEN + 1);
LSP_FRAGMENT(lspid) = 0;
lsp0 = lsp_search(&area->lspdb[level - 1], lspid);
if (lsp0)
lsp_link_fragment(lsp, lsp0);
}
if (lsp->hdr.seqno) {
isis_spf_schedule(lsp->area, lsp->level);
isis_te_lsp_event(lsp, LSP_UPD);
}
}
/* creation of LSP directly from what we received */
struct isis_lsp *lsp_new_from_recv(struct isis_lsp_hdr *hdr,
struct isis_tlvs *tlvs,
struct stream *stream, struct isis_lsp *lsp0,
struct isis_area *area, int level)
{
struct isis_lsp *lsp;
lsp = XCALLOC(MTYPE_ISIS_LSP, sizeof(struct isis_lsp));
lsp_update_data(lsp, hdr, tlvs, stream, area, level);
lsp_link_fragment(lsp, lsp0);
return lsp;
}
static void lsp_adjust_stream(struct isis_lsp *lsp)
{
if (lsp->pdu) {
if (STREAM_SIZE(lsp->pdu) == LLC_LEN + lsp->area->lsp_mtu)
return;
stream_free(lsp->pdu);
}
lsp->pdu = stream_new(LLC_LEN + lsp->area->lsp_mtu);
}
struct isis_lsp *lsp_new(struct isis_area *area, uint8_t *lsp_id,
uint16_t rem_lifetime, uint32_t seqno,
uint8_t lsp_bits, uint16_t checksum,
struct isis_lsp *lsp0, int level)
{
struct isis_lsp *lsp;
lsp = XCALLOC(MTYPE_ISIS_LSP, sizeof(struct isis_lsp));
lsp->area = area;
lsp_adjust_stream(lsp);
/* Minimal LSP PDU size */
lsp->hdr.pdu_len = ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN;
memcpy(lsp->hdr.lsp_id, lsp_id, sizeof(lsp->hdr.lsp_id));
lsp->hdr.checksum = checksum;
lsp->hdr.seqno = seqno;
lsp->hdr.rem_lifetime = rem_lifetime;
lsp->hdr.lsp_bits = lsp_bits;
lsp->level = level;
lsp->age_out = ZERO_AGE_LIFETIME;
lsp_link_fragment(lsp, lsp0);
put_lsp_hdr(lsp, NULL, false);
if (IS_DEBUG_EVENTS)
zlog_debug("New LSP with ID %pLS len %d seqnum %08x", lsp_id,
lsp->hdr.pdu_len, lsp->hdr.seqno);
return lsp;
}
void lsp_insert(struct lspdb_head *head, struct isis_lsp *lsp)
{
lspdb_add(head, lsp);
if (lsp->hdr.seqno) {
isis_spf_schedule(lsp->area, lsp->level);
isis_te_lsp_event(lsp, LSP_ADD);
}
}
/*
* Build a list of LSPs with non-zero ht and seqno bounded by start and stop ids
*/
void lsp_build_list_nonzero_ht(struct lspdb_head *head, const uint8_t *start_id,
const uint8_t *stop_id, struct list *list)
{
struct isis_lsp searchfor;
struct isis_lsp *lsp, *start;
memcpy(&searchfor.hdr.lsp_id, start_id, sizeof(searchfor.hdr.lsp_id));
start = lspdb_find_gteq(head, &searchfor);
frr_each_from (lspdb, head, lsp, start) {
if (memcmp(lsp->hdr.lsp_id, stop_id,
ISIS_SYS_ID_LEN + 2) > 0)
break;
if (lsp->hdr.rem_lifetime && lsp->hdr.seqno)
listnode_add(list, lsp);
}
}
static void lsp_set_time(struct isis_lsp *lsp)
{
assert(lsp);
if (lsp->hdr.rem_lifetime == 0) {
if (lsp->age_out > 0)
lsp->age_out--;
return;
}
lsp->hdr.rem_lifetime--;
if (lsp->pdu && stream_get_endp(lsp->pdu) >= 12)
stream_putw_at(lsp->pdu, 10, lsp->hdr.rem_lifetime);
}
void lspid_print(uint8_t *lsp_id, char *dest, size_t dest_len, char dynhost,
char frag, struct isis *isis)
{
struct isis_dynhn *dyn = NULL;
char id[SYSID_STRLEN];
if (dynhost)
dyn = dynhn_find_by_id(isis, lsp_id);
else
dyn = NULL;
if (dyn)
snprintf(id, sizeof(id), "%.14s", dyn->hostname);
else if (!memcmp(isis->sysid, lsp_id, ISIS_SYS_ID_LEN) && dynhost)
snprintf(id, sizeof(id), "%.14s", cmd_hostname_get());
else
snprintfrr(id, sizeof(id), "%pSY", lsp_id);
if (frag)
snprintf(dest, dest_len, "%s.%02x-%02x", id,
LSP_PSEUDO_ID(lsp_id), LSP_FRAGMENT(lsp_id));
else
snprintf(dest, dest_len, "%s.%02x", id, LSP_PSEUDO_ID(lsp_id));
}
/* Convert the lsp attribute bits to attribute string */
static const char *lsp_bits2string(uint8_t lsp_bits, char *buf, size_t buf_size)
{
char *pos = buf;
if (!lsp_bits)
return " none";
if (buf_size < 2 * 3)
return " error";
/* we only focus on the default metric */
pos += snprintf(pos, buf_size, "%d/",
ISIS_MASK_LSP_ATT_BITS(lsp_bits) ? 1 : 0);
pos += snprintf(pos, buf_size, "%d/",
ISIS_MASK_LSP_PARTITION_BIT(lsp_bits) ? 1 : 0);
snprintf(pos, buf_size, "%d", ISIS_MASK_LSP_OL_BIT(lsp_bits) ? 1 : 0);
return buf;
}
/* this function prints the lsp on show isis database */
void lsp_print_common(struct isis_lsp *lsp, struct vty *vty, struct json_object *json,
char dynhost, struct isis *isis)
{
if (json) {
return lsp_print_json(lsp, json, dynhost, isis);
} else {
return lsp_print_vty(lsp, vty, dynhost, isis);
}
}
void lsp_print_json(struct isis_lsp *lsp, struct json_object *json,
char dynhost, struct isis *isis)
{
char LSPid[255];
char age_out[8];
char b[200];
json_object *own_json;
char buf[256];
lspid_print(lsp->hdr.lsp_id, LSPid, sizeof(LSPid), dynhost, 1, isis);
own_json = json_object_new_object();
json_object_object_add(json, "lsp", own_json);
json_object_string_add(own_json, "id", LSPid);
json_object_string_add(own_json, "own", lsp->own_lsp ? "*" : " ");
if (lsp->own_lsp)
json_object_boolean_add(own_json, "ownLSP", true);
json_object_int_add(json, "pduLen", lsp->hdr.pdu_len);
snprintfrr(buf, sizeof(buf), "0x%08x", lsp->hdr.seqno);
json_object_string_add(json, "seqNumber", buf);
snprintfrr(buf, sizeof(buf), "0x%04hx", lsp->hdr.checksum);
json_object_string_add(json, "chksum", buf);
if (lsp->hdr.rem_lifetime == 0) {
snprintf(age_out, sizeof(age_out), "(%d)", lsp->age_out);
age_out[7] = '\0';
json_object_string_add(json, "holdtime", age_out);
} else {
json_object_int_add(json, "holdtime", lsp->hdr.rem_lifetime);
}
json_object_string_add(json, "attPOl",
lsp_bits2string(lsp->hdr.lsp_bits, b, sizeof(b)));
}
void lsp_print_vty(struct isis_lsp *lsp, struct vty *vty,
char dynhost, struct isis *isis)
{
char LSPid[255];
char age_out[8];
char b[200];
lspid_print(lsp->hdr.lsp_id, LSPid, sizeof(LSPid), dynhost, 1, isis);
vty_out(vty, "%-21s%c ", LSPid, lsp->own_lsp ? '*' : ' ');
vty_out(vty, "%5hu ", lsp->hdr.pdu_len);
vty_out(vty, "0x%08x ", lsp->hdr.seqno);
vty_out(vty, "0x%04hx ", lsp->hdr.checksum);
if (lsp->hdr.rem_lifetime == 0) {
snprintf(age_out, sizeof(age_out), "(%d)", lsp->age_out);
age_out[7] = '\0';
vty_out(vty, "%7s ", age_out);
} else
vty_out(vty, " %5hu ", lsp->hdr.rem_lifetime);
vty_out(vty, "%s\n", lsp_bits2string(lsp->hdr.lsp_bits, b, sizeof(b)));
}
void lsp_print_detail(struct isis_lsp *lsp, struct vty *vty,
struct json_object *json, char dynhost,
struct isis *isis)
{
if (json) {
lsp_print_json(lsp, json, dynhost, isis);
if (lsp->tlvs) {
isis_format_tlvs(lsp->tlvs, json);
}
} else {
lsp_print_vty(lsp, vty, dynhost, isis);
if (lsp->tlvs)
vty_multiline(vty, " ", "%s",
isis_format_tlvs(lsp->tlvs, NULL));
vty_out(vty, "\n");
}
}
/* print all the lsps info in the local lspdb */
int lsp_print_all(struct vty *vty, struct json_object *json,
struct lspdb_head *head, char detail, char dynhost,
struct isis *isis)
{
struct isis_lsp *lsp;
int lsp_count = 0;
struct json_object *lsp_json = NULL;
if (detail == ISIS_UI_LEVEL_BRIEF) {
frr_each (lspdb, head, lsp) {
if (json) {
lsp_json = json_object_new_object();
json_object_array_add(json, lsp_json);
}
lsp_print_common(lsp, vty, lsp_json, dynhost, isis);
lsp_count++;
}
} else if (detail == ISIS_UI_LEVEL_DETAIL) {
frr_each (lspdb, head, lsp) {
if (json) {
lsp_json = json_object_new_object();
json_object_array_add(json, lsp_json);
}
lsp_print_detail(lsp, vty, lsp_json, dynhost, isis);
lsp_count++;
}
}
return lsp_count;
}
static uint16_t lsp_rem_lifetime(struct isis_area *area, int level)
{
uint16_t rem_lifetime;
/* Add jitter to configured LSP lifetime */
rem_lifetime =
isis_jitter(area->max_lsp_lifetime[level - 1], MAX_AGE_JITTER);
/* No jitter if the max refresh will be less than configure gen interval
*/
/* N.B. this calucation is acceptable since rem_lifetime is in
* [332,65535] at
* this point */
if (area->lsp_gen_interval[level - 1] > (rem_lifetime - 300))
rem_lifetime = area->max_lsp_lifetime[level - 1];
return rem_lifetime;
}
static uint16_t lsp_refresh_time(struct isis_lsp *lsp, uint16_t rem_lifetime)
{
struct isis_area *area = lsp->area;
int level = lsp->level;
uint16_t refresh_time;
/* Add jitter to LSP refresh time */
refresh_time =
isis_jitter(area->lsp_refresh[level - 1], MAX_LSP_GEN_JITTER);
/* RFC 4444 : make sure the refresh time is at least less than 300
* of the remaining lifetime and more than gen interval */
if (refresh_time <= area->lsp_gen_interval[level - 1]
|| refresh_time > (rem_lifetime - 300))
refresh_time = rem_lifetime - 300;
/* In cornercases, refresh_time might be <= lsp_gen_interval, however
* we accept this violation to satisfy refresh_time <= rem_lifetime -
* 300 */
return refresh_time;
}
static void lsp_build_internal_reach_ipv4(struct isis_lsp *lsp,
struct isis_area *area,
struct prefix_ipv4 *ipv4,
uint32_t metric)
{
struct sr_prefix_cfg *pcfgs[SR_ALGORITHM_COUNT] = {NULL};
if (area->oldmetric) {
lsp_debug(
"ISIS (%s): Adding old-style IP reachability for %pFX",
area->area_tag, ipv4);
isis_tlvs_add_oldstyle_ip_reach(lsp->tlvs, ipv4, metric);
}
if (area->newmetric) {
lsp_debug("ISIS (%s): Adding te-style IP reachability for %pFX",
area->area_tag, ipv4);
if (area->srdb.enabled)
for (int i = 0; i < SR_ALGORITHM_COUNT; i++) {
#ifndef FABRICD
if (flex_algo_id_valid(i) &&
!isis_flex_algo_elected_supported(i, area))
continue;
#endif /* ifndef FABRICD */
pcfgs[i] =
isis_sr_cfg_prefix_find(area, ipv4, i);
}
isis_tlvs_add_extended_ip_reach(lsp->tlvs, ipv4, metric, false,
pcfgs);
}
}
static void lsp_build_internal_reach_ipv6(struct isis_lsp *lsp,
struct isis_area *area,
struct prefix_ipv6 *ipv6,
uint32_t metric)
{
struct sr_prefix_cfg *pcfgs[SR_ALGORITHM_COUNT] = {NULL};
lsp_debug("ISIS (%s): Adding IPv6 reachability for %pFX",
area->area_tag, ipv6);
if (area->srdb.enabled)
for (int i = 0; i < SR_ALGORITHM_COUNT; i++) {
#ifndef FABRICD
if (flex_algo_id_valid(i) &&
!isis_flex_algo_elected_supported(i, area))
continue;
#endif /* ifndef FABRICD */
pcfgs[i] = isis_sr_cfg_prefix_find(area, ipv6, i);
}
isis_tlvs_add_ipv6_reach(lsp->tlvs, isis_area_ipv6_topology(area), ipv6,
metric, false, pcfgs);
}
static void lsp_build_ext_reach_ipv4(struct isis_lsp *lsp,
struct isis_area *area)
{
struct route_table *er_table = get_ext_reach(area, AF_INET, lsp->level);
if (!er_table)
return;
for (struct route_node *rn = route_top(er_table); rn;
rn = route_next(rn)) {
if (!rn->info)
continue;
struct prefix_ipv4 *ipv4 = (struct prefix_ipv4 *)&rn->p;
struct isis_ext_info *info = rn->info;
uint32_t metric = info->metric;
if (metric > MAX_WIDE_PATH_METRIC)
metric = MAX_WIDE_PATH_METRIC;
if (area->oldmetric && metric > 0x3f)
metric = 0x3f;
if (area->oldmetric)
isis_tlvs_add_oldstyle_ip_reach(lsp->tlvs, ipv4,
metric);
if (area->newmetric) {
struct sr_prefix_cfg *pcfgs[SR_ALGORITHM_COUNT] = {
NULL};
if (area->srdb.enabled)
for (int i = 0; i < SR_ALGORITHM_COUNT; i++) {
#ifndef FABRICD
if (flex_algo_id_valid(i) &&
!isis_flex_algo_elected_supported(
i, area))
continue;
#endif /* ifndef FABRICD */
pcfgs[i] = isis_sr_cfg_prefix_find(
area, ipv4, i);
}
isis_tlvs_add_extended_ip_reach(lsp->tlvs, ipv4, metric,
true, pcfgs);
}
}
}
static void lsp_build_ext_reach_ipv6(struct isis_lsp *lsp,
struct isis_area *area)
{
struct route_table *er_table =
get_ext_reach(area, AF_INET6, lsp->level);
if (!er_table)
return;
for (struct route_node *rn = route_top(er_table); rn;
rn = srcdest_route_next(rn)) {
if (!rn->info)
continue;
struct isis_ext_info *info = rn->info;
struct prefix_ipv6 *p, *src_p;
srcdest_rnode_prefixes(rn, (const struct prefix **)&p,
(const struct prefix **)&src_p);
uint32_t metric = info->metric;
if (info->metric > MAX_WIDE_PATH_METRIC)
metric = MAX_WIDE_PATH_METRIC;
if (!src_p || !src_p->prefixlen) {
struct sr_prefix_cfg *pcfgs[SR_ALGORITHM_COUNT] = {
NULL};
if (area->srdb.enabled)
for (int i = 0; i < SR_ALGORITHM_COUNT; i++) {
#ifndef FABRICD
if (flex_algo_id_valid(i) &&
!isis_flex_algo_elected_supported(
i, area))
continue;