forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathospf_ext.c
1910 lines (1606 loc) · 53.2 KB
/
ospf_ext.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
/*
* This is an implementation of RFC7684 OSPFv2 Prefix/Link Attribute
* Advertisement
*
* Module name: Extended Prefix/Link Opaque LSA
*
* Author: Olivier Dugeon <[email protected]>
* Author: Anselme Sawadogo <[email protected]>
*
* Copyright (C) 2016 - 2018 Orange Labs http://www.orange.com
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"
#include "prefix.h"
#include "if.h"
#include "table.h"
#include "memory.h"
#include "command.h"
#include "vty.h"
#include "stream.h"
#include "log.h"
#include "thread.h"
#include "hash.h"
#include "sockunion.h" /* for inet_aton() */
#include "network.h"
#include "if.h"
#include "libospf.h" /* for ospf interface types */
#include "ospfd/ospfd.h"
#include "ospfd/ospf_interface.h"
#include "ospfd/ospf_ism.h"
#include "ospfd/ospf_asbr.h"
#include "ospfd/ospf_lsa.h"
#include "ospfd/ospf_lsdb.h"
#include "ospfd/ospf_neighbor.h"
#include "ospfd/ospf_nsm.h"
#include "ospfd/ospf_flood.h"
#include "ospfd/ospf_packet.h"
#include "ospfd/ospf_spf.h"
#include "ospfd/ospf_dump.h"
#include "ospfd/ospf_route.h"
#include "ospfd/ospf_ase.h"
#include "ospfd/ospf_zebra.h"
#include "ospfd/ospf_sr.h"
#include "ospfd/ospf_ext.h"
#include "ospfd/ospf_errors.h"
/* Following structure are internal use only. */
/*
* Global variable to manage Extended Prefix/Link Opaque LSA on this node.
* Note that all parameter values are stored in network byte order.
*/
static struct ospf_ext_lp OspfEXT;
/*
* -----------------------------------------------------------------------
* Followings are initialize/terminate functions for Extended Prefix/Link
* Opaque LSA handling.
* -----------------------------------------------------------------------
*/
/* Extended Prefix Opaque LSA related callback functions */
static void ospf_ext_pref_show_info(struct vty *vty, struct ospf_lsa *lsa);
static int ospf_ext_pref_lsa_originate(void *arg);
static struct ospf_lsa *ospf_ext_pref_lsa_refresh(struct ospf_lsa *lsa);
static void ospf_ext_pref_lsa_schedule(struct ext_itf *exti,
enum lsa_opcode opcode);
/* Extended Link Opaque LSA related callback functions */
static int ospf_ext_link_new_if(struct interface *ifp);
static int ospf_ext_link_del_if(struct interface *ifp);
static void ospf_ext_ism_change(struct ospf_interface *oi, int old_status);
static void ospf_ext_link_nsm_change(struct ospf_neighbor *nbr, int old_status);
static void ospf_ext_link_show_info(struct vty *vty, struct ospf_lsa *lsa);
static int ospf_ext_link_lsa_originate(void *arg);
static struct ospf_lsa *ospf_ext_link_lsa_refresh(struct ospf_lsa *lsa);
static void ospf_ext_link_lsa_schedule(struct ext_itf *exti,
enum lsa_opcode opcode);
static void ospf_ext_lsa_schedule(struct ext_itf *exti, enum lsa_opcode op);
static int ospf_ext_link_lsa_update(struct ospf_lsa *lsa);
static int ospf_ext_pref_lsa_update(struct ospf_lsa *lsa);
static void ospf_ext_link_delete_adj_sid(struct ext_itf *exti);
static void del_ext_info(void *val);
/*
* Extended Link/Prefix initialization
*
* @param - none
*
* @return - 0 if OK, <> 0 otherwise
*/
int ospf_ext_init(void)
{
int rc = 0;
memset(&OspfEXT, 0, sizeof(struct ospf_ext_lp));
OspfEXT.enabled = false;
/* Only Area flooding is supported yet */
OspfEXT.scope = OSPF_OPAQUE_AREA_LSA;
/* Initialize interface list */
OspfEXT.iflist = list_new();
OspfEXT.iflist->del = del_ext_info;
zlog_info("EXT (%s): Register Extended Link Opaque LSA", __func__);
rc = ospf_register_opaque_functab(
OSPF_OPAQUE_AREA_LSA, OPAQUE_TYPE_EXTENDED_LINK_LSA,
ospf_ext_link_new_if, /* new if */
ospf_ext_link_del_if, /* del if */
ospf_ext_ism_change, /* ism change */
ospf_ext_link_nsm_change, /* nsm change */
NULL, /* Write router config. */
NULL, /* Write interface conf. */
NULL, /* Write debug config. */
ospf_ext_link_show_info, /* Show LSA info */
ospf_ext_link_lsa_originate, /* Originate LSA */
ospf_ext_link_lsa_refresh, /* Refresh LSA */
ospf_ext_link_lsa_update, /* new_lsa_hook */
NULL); /* del_lsa_hook */
if (rc != 0) {
flog_warn(EC_OSPF_OPAQUE_REGISTRATION,
"EXT (%s): Failed to register Extended Link LSA",
__func__);
return rc;
}
zlog_info("EXT (%s): Register Extended Prefix Opaque LSA", __func__);
rc = ospf_register_opaque_functab(
OspfEXT.scope, OPAQUE_TYPE_EXTENDED_PREFIX_LSA,
NULL, /* new if handle by link */
NULL, /* del if handle by link */
NULL, /* ism change */
NULL, /* nsm change */
ospf_sr_config_write_router, /* Write router config. */
NULL, /* Write interface conf. */
NULL, /* Write debug config. */
ospf_ext_pref_show_info, /* Show LSA info */
ospf_ext_pref_lsa_originate, /* Originate LSA */
ospf_ext_pref_lsa_refresh, /* Refresh LSA */
ospf_ext_pref_lsa_update, /* new_lsa_hook */
NULL); /* del_lsa_hook */
if (rc != 0) {
flog_warn(EC_OSPF_OPAQUE_REGISTRATION,
"EXT (%s): Failed to register Extended Prefix LSA",
__func__);
return rc;
}
return rc;
}
/*
* Extended Link/Prefix termination function
*
* @param - none
* @return - none
*/
void ospf_ext_term(void)
{
if ((OspfEXT.scope == OSPF_OPAQUE_AREA_LSA)
|| (OspfEXT.scope == OSPF_OPAQUE_AS_LSA))
ospf_delete_opaque_functab(OspfEXT.scope,
OPAQUE_TYPE_EXTENDED_PREFIX_LSA);
ospf_delete_opaque_functab(OSPF_OPAQUE_AREA_LSA,
OPAQUE_TYPE_EXTENDED_LINK_LSA);
list_delete(&OspfEXT.iflist);
OspfEXT.scope = 0;
OspfEXT.enabled = false;
return;
}
/*
* Extended Link/Prefix finish function
*
* @param - none
* @return - none
*/
void ospf_ext_finish(void)
{
struct listnode *node;
struct ext_itf *exti;
/* Flush Router Info LSA */
for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti))
if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
ospf_ext_lsa_schedule(exti, FLUSH_THIS_LSA);
OspfEXT.enabled = false;
}
/*
* ---------------------------------------------------------------------
* Followings are control functions for Extended Prefix/Link Opaque LSA
* parameters management.
* ---------------------------------------------------------------------
*/
/* Functions to free memory space */
static void del_ext_info(void *val)
{
XFREE(MTYPE_OSPF_EXT_PARAMS, val);
}
/* Increment instance value for Extended Prefix Opaque LSAs Opaque ID field */
static uint32_t get_ext_pref_instance_value(void)
{
static uint32_t seqno = 0;
if (seqno < MAX_LEGAL_EXT_INSTANCE_NUM)
seqno += 1;
else
seqno = 1; /* Avoid zero. */
return seqno;
}
/* Increment instance value for Extended Link Opaque LSAs Opaque ID field */
static uint32_t get_ext_link_instance_value(void)
{
static uint32_t seqno = 0;
if (seqno < MAX_LEGAL_EXT_INSTANCE_NUM)
seqno += 1;
else
seqno = 1; /* Avoid zero. */
return seqno;
}
/* Lookup Extended Prefix/Links by ifp from OspfEXT struct iflist */
static struct ext_itf *lookup_ext_by_ifp(struct interface *ifp)
{
struct listnode *node, *nnode;
struct ext_itf *exti;
for (ALL_LIST_ELEMENTS(OspfEXT.iflist, node, nnode, exti))
if (exti->ifp == ifp)
return exti;
return NULL;
}
/* Lookup Extended Prefix/Links by LSA ID from OspfEXT struct iflist */
static struct ext_itf *lookup_ext_by_instance(struct ospf_lsa *lsa)
{
struct listnode *node;
struct ext_itf *exti;
uint32_t key = GET_OPAQUE_ID(ntohl(lsa->data->id.s_addr));
uint8_t type = GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr));
for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti))
if ((exti->instance == key) && (exti->type == type))
return exti;
return NULL;
}
/*
* ----------------------------------------------------------------------
* The underlying subsection defines setters and unsetters to create and
* delete tlvs and subtlvs
* ----------------------------------------------------------------------
*/
/* Extended Prefix TLV - RFC7684 section 2.1 */
static void set_ext_prefix(struct ext_itf *exti, uint8_t route_type,
uint8_t flags, struct prefix_ipv4 p)
{
TLV_TYPE(exti->prefix) = htons(EXT_TLV_PREFIX);
/* Warning: Size must be adjust depending of subTLV's */
TLV_LEN(exti->prefix) = htons(EXT_TLV_PREFIX_SIZE);
exti->prefix.route_type = route_type;
exti->prefix.flags = flags;
/* Only Address Family Ipv4 (0) is defined in RFC 7684 */
exti->prefix.af = 0;
exti->prefix.pref_length = p.prefixlen;
exti->prefix.address = p.prefix;
SET_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE);
}
/* Extended Link TLV - RFC7684 section 3.1 */
static void set_ext_link(struct ext_itf *exti, uint8_t type, struct in_addr id,
struct in_addr data)
{
TLV_TYPE(exti->link) = htons(EXT_TLV_LINK);
/* Warning: Size must be adjust depending of subTLV's */
TLV_LEN(exti->link) = htons(EXT_TLV_LINK_SIZE);
exti->link.link_type = type;
exti->link.link_id = id;
exti->link.link_data = data;
}
/* Prefix SID SubTLV - section 5 */
static void set_prefix_sid(struct ext_itf *exti, uint8_t algorithm,
uint32_t value, int value_type, uint8_t flags)
{
if ((algorithm != SR_ALGORITHM_SPF)
&& (algorithm != SR_ALGORITHM_STRICT_SPF)) {
flog_err(EC_OSPF_INVALID_ALGORITHM,
"EXT (%s): unrecognized algorithm, not SPF or S-SPF",
__func__);
return;
}
/* Update flags according to the type of value field: label or index */
if (value_type == SID_LABEL)
SET_FLAG(flags, EXT_SUBTLV_PREFIX_SID_VFLG);
/* set prefix sid subtlv for an extended prefix tlv */
TLV_TYPE(exti->node_sid) = htons(EXT_SUBTLV_PREFIX_SID);
exti->node_sid.algorithm = algorithm;
exti->node_sid.flags = flags;
exti->node_sid.mtid = 0; /* Multi-Topology is not supported */
/* Set Label or Index value */
if (value_type == SID_LABEL) {
TLV_LEN(exti->node_sid) =
htons(SID_LABEL_SIZE(EXT_SUBTLV_PREFIX_SID_SIZE));
exti->node_sid.value = htonl(SET_LABEL(value));
} else {
TLV_LEN(exti->node_sid) =
htons(SID_INDEX_SIZE(EXT_SUBTLV_PREFIX_SID_SIZE));
exti->node_sid.value = htonl(value);
}
}
/* Adjacency SID SubTLV - section 6.1 */
static void set_adj_sid(struct ext_itf *exti, bool backup, uint32_t value,
int value_type)
{
int index;
uint8_t flags;
/* Determine which ADJ_SID must be set: nominal or backup */
if (backup) {
flags = EXT_SUBTLV_LINK_ADJ_SID_BFLG;
index = 1;
} else {
index = 0;
flags = 0;
}
/* Set Header */
TLV_TYPE(exti->adj_sid[index]) = htons(EXT_SUBTLV_ADJ_SID);
/* Only Local ADJ-SID is supported for the moment */
SET_FLAG(flags, EXT_SUBTLV_LINK_ADJ_SID_LFLG);
exti->adj_sid[index].mtid = 0; /* Multi-Topology is not supported */
/* Adjust Length, Flags and Value depending on the type of Label */
if (value_type == SID_LABEL) {
SET_FLAG(flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG);
TLV_LEN(exti->adj_sid[index]) =
htons(SID_LABEL_SIZE(EXT_SUBTLV_ADJ_SID_SIZE));
exti->adj_sid[index].value = htonl(SET_LABEL(value));
} else {
UNSET_FLAG(flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG);
TLV_LEN(exti->adj_sid[index]) =
htons(SID_INDEX_SIZE(EXT_SUBTLV_ADJ_SID_SIZE));
exti->adj_sid[index].value = htonl(value);
}
exti->adj_sid[index].flags = flags; /* Set computed flags */
exti->adj_sid[index].mtid = 0; /* Multi-Topology is not supported */
exti->adj_sid[index].weight = 0; /* Load-Balancing is not supported */
}
/* LAN Adjacency SID SubTLV - section 6.2 */
static void set_lan_adj_sid(struct ext_itf *exti, bool backup, uint32_t value,
int value_type, struct in_addr neighbor_id)
{
int index;
uint8_t flags;
/* Determine which ADJ_SID must be set: nominal or backup */
if (backup) {
flags = EXT_SUBTLV_LINK_ADJ_SID_BFLG;
index = 1;
} else {
index = 0;
flags = 0;
}
/* Set Header */
TLV_TYPE(exti->lan_sid[index]) = htons(EXT_SUBTLV_LAN_ADJ_SID);
/* Only Local ADJ-SID is supported for the moment */
SET_FLAG(flags, EXT_SUBTLV_LINK_ADJ_SID_LFLG);
/* Adjust Length, Flags and Value depending on the type of Label */
if (value_type == SID_LABEL) {
SET_FLAG(flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG);
TLV_LEN(exti->lan_sid[index]) =
htons(SID_LABEL_SIZE(EXT_SUBTLV_PREFIX_RANGE_SIZE));
exti->lan_sid[index].value = htonl(SET_LABEL(value));
} else {
UNSET_FLAG(flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG);
TLV_LEN(exti->lan_sid[index]) =
htons(SID_INDEX_SIZE(EXT_SUBTLV_PREFIX_RANGE_SIZE));
exti->lan_sid[index].value = htonl(value);
}
exti->lan_sid[index].flags = flags; /* Set computed flags */
exti->lan_sid[index].mtid = 0; /* Multi-Topology is not supported */
exti->lan_sid[index].weight = 0; /* Load-Balancing is not supported */
exti->lan_sid[index].neighbor_id = neighbor_id;
}
static void unset_adjacency_sid(struct ext_itf *exti)
{
/* Reset Adjacency TLV */
if (exti->type == ADJ_SID) {
TLV_TYPE(exti->adj_sid[0]) = 0;
TLV_TYPE(exti->adj_sid[1]) = 0;
}
/* or Lan-Adjacency TLV */
if (exti->type == LAN_ADJ_SID) {
TLV_TYPE(exti->lan_sid[0]) = 0;
TLV_TYPE(exti->lan_sid[1]) = 0;
}
}
/* Experimental SubTLV from Cisco */
static void set_rmt_itf_addr(struct ext_itf *exti, struct in_addr rmtif)
{
TLV_TYPE(exti->rmt_itf_addr) = htons(EXT_SUBTLV_RMT_ITF_ADDR);
TLV_LEN(exti->rmt_itf_addr) = htons(sizeof(struct in_addr));
exti->rmt_itf_addr.value = rmtif;
}
/* Delete Extended LSA */
static void ospf_extended_lsa_delete(struct ext_itf *exti)
{
/* Avoid deleting LSA if Extended is not enable */
if (!OspfEXT.enabled)
return;
/* Process only Active Extended Prefix/Link LSA */
if (!CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE))
return;
osr_debug("EXT (%s): Disable %s%s%s-SID on interface %s", __func__,
exti->stype == LOCAL_SID ? "Prefix" : "",
exti->stype == ADJ_SID ? "Adjacency" : "",
exti->stype == LAN_ADJ_SID ? "LAN-Adjacency" : "",
exti->ifp->name);
/* Flush LSA if already engaged */
if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED)) {
ospf_ext_lsa_schedule(exti, FLUSH_THIS_LSA);
UNSET_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED);
}
/* De-activate this Extended Prefix/Link and remove corresponding
* Segment-Routing Prefix-SID or (LAN)-ADJ-SID */
if (exti->stype == ADJ_SID || exti->stype == LAN_ADJ_SID)
ospf_ext_link_delete_adj_sid(exti);
else
ospf_sr_ext_itf_delete(exti);
}
/*
* Update Extended prefix SID index for Loopback interface type
*
* @param ifname - Loopback interface name
* @param index - new value for the prefix SID of this interface
* @param p - prefix for this interface or NULL if Extended Prefix
* should be remove
*
* @return instance number if update is OK, 0 otherwise
*/
uint32_t ospf_ext_schedule_prefix_index(struct interface *ifp, uint32_t index,
struct prefix_ipv4 *p, uint8_t flags)
{
int rc = 0;
struct ext_itf *exti;
/* Find Extended Prefix interface */
exti = lookup_ext_by_ifp(ifp);
if (exti == NULL)
return rc;
if (p != NULL) {
osr_debug("EXT (%s): Schedule new prefix %pFX with index %u on interface %s", __func__, p, index, ifp->name);
/* Set first Extended Prefix then the Prefix SID information */
set_ext_prefix(exti, OSPF_PATH_INTRA_AREA, EXT_TLV_PREF_NFLG,
*p);
set_prefix_sid(exti, SR_ALGORITHM_SPF, index, SID_INDEX, flags);
/* Try to Schedule LSA */
if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE)) {
if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
ospf_ext_pref_lsa_schedule(exti,
REFRESH_THIS_LSA);
else
ospf_ext_pref_lsa_schedule(
exti, REORIGINATE_THIS_LSA);
}
} else {
osr_debug("EXT (%s): Remove prefix for interface %s", __func__,
ifp->name);
if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
ospf_ext_pref_lsa_schedule(exti, FLUSH_THIS_LSA);
}
return SET_OPAQUE_LSID(exti->type, exti->instance);
}
/**
* Update Adjacecny-SID for Extended Link LSA
*
* @param exti Extended Link information
*/
static void ospf_ext_link_update_adj_sid(struct ext_itf *exti)
{
mpls_label_t label;
mpls_label_t bck_label;
/* Process only (LAN)Adjacency-SID Type */
if (exti->stype != ADJ_SID && exti->stype != LAN_ADJ_SID)
return;
/* Request Primary & Backup Labels from Label Manager */
bck_label = ospf_sr_local_block_request_label();
label = ospf_sr_local_block_request_label();
if (bck_label == MPLS_INVALID_LABEL || label == MPLS_INVALID_LABEL) {
if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
ospf_ext_lsa_schedule(exti, FLUSH_THIS_LSA);
return;
}
/* Set Adjacency-SID, backup first */
if (exti->stype == ADJ_SID) {
set_adj_sid(exti, true, bck_label, SID_LABEL);
set_adj_sid(exti, false, label, SID_LABEL);
} else {
set_lan_adj_sid(exti, true, bck_label, SID_LABEL,
exti->lan_sid[0].neighbor_id);
set_lan_adj_sid(exti, false, label, SID_LABEL,
exti->lan_sid[1].neighbor_id);
}
/* Finally, add corresponding SR Link in SRDB & MPLS LFIB */
SET_FLAG(exti->flags, EXT_LPFLG_FIB_ENTRY_SET);
ospf_sr_ext_itf_add(exti);
}
/**
* Delete Adjacecny-SID for Extended Link LSA
*
* @param exti Extended Link information
*/
static void ospf_ext_link_delete_adj_sid(struct ext_itf *exti)
{
/* Process only (LAN)Adjacency-SID Type */
if (exti->stype != ADJ_SID && exti->stype != LAN_ADJ_SID)
return;
/* Release Primary & Backup Labels from Label Manager */
if (exti->stype == ADJ_SID) {
ospf_sr_local_block_release_label(exti->adj_sid[0].value);
ospf_sr_local_block_release_label(exti->adj_sid[1].value);
} else {
ospf_sr_local_block_release_label(exti->lan_sid[0].value);
ospf_sr_local_block_release_label(exti->lan_sid[1].value);
}
/* And reset corresponding TLV */
unset_adjacency_sid(exti);
/* Finally, remove corresponding SR Link in SRDB & MPLS LFIB */
UNSET_FLAG(exti->flags, EXT_LPFLG_FIB_ENTRY_SET);
ospf_sr_ext_itf_delete(exti);
}
/**
* Update Extended Link LSA once Segment Routing Label Block has been changed.
*/
void ospf_ext_link_srlb_update(void)
{
struct listnode *node;
struct ext_itf *exti;
osr_debug("EXT (%s): Update Extended Links with new SRLB", __func__);
/* Update all Extended Link Adjaceny-SID */
for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti)) {
/* Skip Extended Prefix */
if (exti->stype == PREF_SID || exti->stype == LOCAL_SID)
continue;
/* Skip inactive Extended Link */
if (!CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE))
continue;
ospf_ext_link_update_adj_sid(exti);
}
}
/*
* Used by Segment Routing to activate/deactivate Extended Link/Prefix flooding
*
* @param enable To activate or not Segment Routing Extended LSA flooding
*
* @return none
*/
void ospf_ext_update_sr(bool enable)
{
struct listnode *node;
struct ext_itf *exti;
osr_debug("EXT (%s): %s Extended LSAs for Segment Routing ", __func__,
enable ? "Enable" : "Disable");
if (enable) {
OspfEXT.enabled = true;
/* Refresh LSAs if already engaged or originate */
for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti)) {
/* Skip Inactive Extended Link */
if (!CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE))
continue;
/* Update Extended Link (LAN)Adj-SID if not set */
if (!CHECK_FLAG(exti->flags, EXT_LPFLG_FIB_ENTRY_SET))
ospf_ext_link_update_adj_sid(exti);
/* Finally, flood the extended Link */
if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
ospf_ext_lsa_schedule(exti, REFRESH_THIS_LSA);
else
ospf_ext_lsa_schedule(exti,
REORIGINATE_THIS_LSA);
}
} else {
/* Start by Removing Extended LSA */
for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti))
ospf_extended_lsa_delete(exti);
/* And then disable Extended Link/Prefix */
OspfEXT.enabled = false;
}
}
/*
* -----------------------------------------------------------------------
* Followings are callback functions against generic Opaque-LSAs handling
* -----------------------------------------------------------------------
*/
/* Add new Interface in Extended Interface List */
static int ospf_ext_link_new_if(struct interface *ifp)
{
struct ext_itf *new;
int rc = -1;
if (lookup_ext_by_ifp(ifp) != NULL) {
rc = 0; /* Do nothing here. */
return rc;
}
new = XCALLOC(MTYPE_OSPF_EXT_PARAMS, sizeof(struct ext_itf));
/* initialize new information and link back the interface */
new->ifp = ifp;
new->flags = EXT_LPFLG_LSA_INACTIVE;
listnode_add(OspfEXT.iflist, new);
rc = 0;
return rc;
}
/* Remove existing Interface from Extended Interface List */
static int ospf_ext_link_del_if(struct interface *ifp)
{
struct ext_itf *exti;
int rc = -1;
exti = lookup_ext_by_ifp(ifp);
if (exti != NULL) {
/* Flush LSA and remove Adjacency SID */
ospf_extended_lsa_delete(exti);
/* Dequeue listnode entry from the list. */
listnode_delete(OspfEXT.iflist, exti);
XFREE(MTYPE_OSPF_EXT_PARAMS, exti);
rc = 0;
} else {
flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
"EXT (%s): interface %s is not found", __func__,
ifp ? ifp->name : "-");
}
return rc;
}
/*
* Determine if an Interface belongs to an Extended Link Adjacency or
* Extended Prefix SID type and allocate new instance value accordingly
*/
static void ospf_ext_ism_change(struct ospf_interface *oi, int old_status)
{
struct ext_itf *exti;
/* Get interface information for Segment Routing */
exti = lookup_ext_by_ifp(oi->ifp);
if (exti == NULL) {
flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
"EXT (%s): Cannot get Extended info. from OI(%s)",
__func__, IF_NAME(oi));
return;
}
/* Reset Extended information if ospf interface goes Down */
if (oi->state == ISM_Down) {
ospf_extended_lsa_delete(exti);
exti->area = NULL;
exti->flags = EXT_LPFLG_LSA_INACTIVE;
return;
}
/* Determine if interface is related to a Prefix or an Adjacency SID */
if (oi->type == OSPF_IFTYPE_LOOPBACK) {
exti->stype = PREF_SID;
exti->type = OPAQUE_TYPE_EXTENDED_PREFIX_LSA;
exti->instance = get_ext_pref_instance_value();
exti->area = oi->area;
/* Complete SRDB if the interface belongs to a Prefix */
if (OspfEXT.enabled) {
osr_debug("EXT (%s): Set Prefix SID to interface %s ",
__func__, oi->ifp->name);
ospf_sr_update_local_prefix(oi->ifp, oi->address);
}
} else {
/* Determine if interface is related to Adj. or LAN Adj. SID */
if (oi->state == ISM_DR)
exti->stype = LAN_ADJ_SID;
else
exti->stype = ADJ_SID;
exti->type = OPAQUE_TYPE_EXTENDED_LINK_LSA;
exti->instance = get_ext_link_instance_value();
exti->area = oi->area;
/*
* Note: Adjacency SID information are completed when ospf
* adjacency become up see ospf_ext_link_nsm_change()
*/
if (OspfEXT.enabled)
osr_debug(
"EXT (%s): Set %sAdjacency SID for interface %s ",
__func__, exti->stype == ADJ_SID ? "" : "LAN-",
oi->ifp->name);
}
}
/*
* Finish Extended Link configuration and flood corresponding LSA
* when OSPF adjacency on this link fire up
*/
static void ospf_ext_link_nsm_change(struct ospf_neighbor *nbr, int old_status)
{
struct ospf_interface *oi = nbr->oi;
struct ext_itf *exti;
/* Process Link only when neighbor old or new state is NSM Full */
if (nbr->state != NSM_Full && old_status != NSM_Full)
return;
/* Get interface information for Segment Routing */
exti = lookup_ext_by_ifp(oi->ifp);
if (exti == NULL) {
flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
"EXT (%s): Cannot get Extended info. from OI(%s)",
__func__, IF_NAME(oi));
return;
}
/* Check that we have a valid area and ospf context */
if (oi->area == NULL || oi->area->ospf == NULL) {
flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
"EXT (%s): Cannot refer to OSPF from OI(%s)",
__func__, IF_NAME(oi));
return;
}
/* Remove Extended Link if Neighbor State goes Down or Deleted */
if (OspfEXT.enabled
&& (nbr->state == NSM_Down || nbr->state == NSM_Deleted)) {
ospf_ext_link_delete_adj_sid(exti);
if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
ospf_ext_link_lsa_schedule(exti, FLUSH_THIS_LSA);
exti->flags = EXT_LPFLG_LSA_INACTIVE;
return;
}
/* Keep Area information in combination with SR info. */
exti->area = oi->area;
/* Process only Adjacency/LAN SID */
if (exti->stype == PREF_SID)
return;
switch (oi->state) {
case ISM_PointToPoint:
/* Segment ID is an Adjacency one */
exti->stype = ADJ_SID;
/* Set Extended Link TLV with link_id == Nbr Router ID */
set_ext_link(exti, OSPF_IFTYPE_POINTOPOINT, nbr->router_id,
oi->address->u.prefix4);
/* And Remote Interface address */
set_rmt_itf_addr(exti, nbr->address.u.prefix4);
break;
case ISM_DR:
/* Segment ID is a LAN Adjacency for the DR only */
exti->stype = LAN_ADJ_SID;
/* Set Extended Link TLV with link_id == DR */
set_ext_link(exti, OSPF_IFTYPE_BROADCAST, DR(oi),
oi->address->u.prefix4);
/* Set Neighbor ID */
exti->lan_sid[0].neighbor_id = nbr->router_id;
exti->lan_sid[1].neighbor_id = nbr->router_id;
break;
case ISM_DROther:
case ISM_Backup:
/* Segment ID is an Adjacency if not the DR */
exti->stype = ADJ_SID;
/* Set Extended Link TLV with link_id == DR */
set_ext_link(exti, OSPF_IFTYPE_BROADCAST, DR(oi),
oi->address->u.prefix4);
break;
default:
if (CHECK_FLAG(exti->flags, EXT_LPFLG_FIB_ENTRY_SET))
ospf_ext_link_delete_adj_sid(exti);
if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
ospf_ext_link_lsa_schedule(exti, FLUSH_THIS_LSA);
exti->flags = EXT_LPFLG_LSA_INACTIVE;
return;
}
SET_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE);
if (OspfEXT.enabled) {
osr_debug("EXT (%s): Set %sAdjacency SID for interface %s ",
__func__, exti->stype == ADJ_SID ? "" : "LAN-",
oi->ifp->name);
/* Update (LAN)Adjacency SID */
ospf_ext_link_update_adj_sid(exti);
/* flood this links params if everything is ok */
if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
ospf_ext_link_lsa_schedule(exti, REFRESH_THIS_LSA);
else
ospf_ext_link_lsa_schedule(exti, REORIGINATE_THIS_LSA);
}
}
/* Callbacks to handle Extended Link Segment Routing LSA information */
static int ospf_ext_link_lsa_update(struct ospf_lsa *lsa)
{
/* Sanity Check */
if (lsa == NULL) {
flog_warn(EC_OSPF_LSA_NULL, "EXT (%s): Abort! LSA is NULL",
__func__);
return -1;
}
/* Process only Opaque LSA */
if ((lsa->data->type != OSPF_OPAQUE_AREA_LSA)
&& (lsa->data->type != OSPF_OPAQUE_AS_LSA))
return 0;
/* Process only Extended Link LSA */
if (GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr))
!= OPAQUE_TYPE_EXTENDED_LINK_LSA)
return 0;
/* Check if it is not my LSA */
if (IS_LSA_SELF(lsa))
return 0;
/* Check if Extended is enable */
if (!OspfEXT.enabled)
return 0;
/* Call Segment Routing LSA update or deletion */
if (!IS_LSA_MAXAGE(lsa))
ospf_sr_ext_link_lsa_update(lsa);
else
ospf_sr_ext_link_lsa_delete(lsa);
return 0;
}
/* Callbacks to handle Extended Prefix Segment Routing LSA information */
static int ospf_ext_pref_lsa_update(struct ospf_lsa *lsa)
{
/* Sanity Check */
if (lsa == NULL) {
flog_warn(EC_OSPF_LSA_NULL, "EXT (%s): Abort! LSA is NULL",
__func__);
return -1;
}
/* Process only Opaque LSA */
if ((lsa->data->type != OSPF_OPAQUE_AREA_LSA)
&& (lsa->data->type != OSPF_OPAQUE_AS_LSA))
return 0;
/* Process only Extended Prefix LSA */
if (GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr))
!= OPAQUE_TYPE_EXTENDED_PREFIX_LSA)
return 0;
/* Check if it is not my LSA */
if (IS_LSA_SELF(lsa))
return 0;
/* Check if Extended is enable */
if (!OspfEXT.enabled)
return 0;
/* Call Segment Routing LSA update or deletion */
if (!IS_LSA_MAXAGE(lsa))
ospf_sr_ext_prefix_lsa_update(lsa);
else
ospf_sr_ext_prefix_lsa_delete(lsa);
return 0;
}
/*
* -------------------------------------------------------
* Followings are OSPF protocol processing functions for
* Extended Prefix/Link Opaque LSA
* -------------------------------------------------------
*/
static void build_tlv_header(struct stream *s, struct tlv_header *tlvh)
{
stream_put(s, tlvh, sizeof(struct tlv_header));
}
static void build_tlv(struct stream *s, struct tlv_header *tlvh)
{
if ((tlvh != NULL) && (ntohs(tlvh->type) != 0)) {
build_tlv_header(s, tlvh);