]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_ext.c
Merge pull request #8688 from idryzhov/bgp-vrf-bind-priv
[mirror_frr.git] / ospfd / ospf_ext.c
CommitLineData
cf9b9f77
OD
1/*
2 * This is an implementation of RFC7684 OSPFv2 Prefix/Link Attribute
3 * Advertisement
4 *
5 * Module name: Extended Prefix/Link Opaque LSA
6 *
cf9b9f77 7 * Author: Olivier Dugeon <olivier.dugeon@orange.com>
7743f2f8 8 * Author: Anselme Sawadogo <anselmesawadogo@gmail.com>
cf9b9f77 9 *
7743f2f8 10 * Copyright (C) 2016 - 2018 Orange Labs http://www.orange.com
cf9b9f77 11 *
7743f2f8
OD
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
cf9b9f77 16 *
7743f2f8
OD
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
cf9b9f77 21 *
7743f2f8
OD
22 * You should have received a copy of the GNU General Public License along
23 * with this program; see the file COPYING; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
cf9b9f77
OD
25 */
26
27#include <zebra.h>
28#include <math.h>
29#include <stdio.h>
30#include <stdlib.h>
31
32#include "linklist.h"
33#include "prefix.h"
34#include "if.h"
35#include "table.h"
36#include "memory.h"
37#include "command.h"
38#include "vty.h"
39#include "stream.h"
40#include "log.h"
41#include "thread.h"
42#include "hash.h"
43#include "sockunion.h" /* for inet_aton() */
44#include "network.h"
45#include "if.h"
46#include "libospf.h" /* for ospf interface types */
47
48#include "ospfd/ospfd.h"
49#include "ospfd/ospf_interface.h"
50#include "ospfd/ospf_ism.h"
51#include "ospfd/ospf_asbr.h"
52#include "ospfd/ospf_lsa.h"
53#include "ospfd/ospf_lsdb.h"
54#include "ospfd/ospf_neighbor.h"
55#include "ospfd/ospf_nsm.h"
56#include "ospfd/ospf_flood.h"
57#include "ospfd/ospf_packet.h"
58#include "ospfd/ospf_spf.h"
59#include "ospfd/ospf_dump.h"
60#include "ospfd/ospf_route.h"
61#include "ospfd/ospf_ase.h"
62#include "ospfd/ospf_zebra.h"
63#include "ospfd/ospf_sr.h"
64#include "ospfd/ospf_ext.h"
85c9b439 65#include "ospfd/ospf_errors.h"
cf9b9f77
OD
66
67/* Following structure are internal use only. */
68
69/*
70 * Global variable to manage Extended Prefix/Link Opaque LSA on this node.
71 * Note that all parameter values are stored in network byte order.
72 */
73static struct ospf_ext_lp OspfEXT;
74
7743f2f8
OD
75/*
76 * -----------------------------------------------------------------------
77 * Followings are initialize/terminate functions for Extended Prefix/Link
78 * Opaque LSA handling.
79 * -----------------------------------------------------------------------
80 */
cf9b9f77
OD
81
82/* Extended Prefix Opaque LSA related callback functions */
cf9b9f77
OD
83static void ospf_ext_pref_show_info(struct vty *vty, struct ospf_lsa *lsa);
84static int ospf_ext_pref_lsa_originate(void *arg);
85static struct ospf_lsa *ospf_ext_pref_lsa_refresh(struct ospf_lsa *lsa);
86static void ospf_ext_pref_lsa_schedule(struct ext_itf *exti,
87 enum lsa_opcode opcode);
88/* Extended Link Opaque LSA related callback functions */
89static int ospf_ext_link_new_if(struct interface *ifp);
90static int ospf_ext_link_del_if(struct interface *ifp);
731271b0 91static void ospf_ext_ism_change(struct ospf_interface *oi, int old_status);
cf9b9f77
OD
92static void ospf_ext_link_nsm_change(struct ospf_neighbor *nbr, int old_status);
93static void ospf_ext_link_show_info(struct vty *vty, struct ospf_lsa *lsa);
94static int ospf_ext_link_lsa_originate(void *arg);
95static struct ospf_lsa *ospf_ext_link_lsa_refresh(struct ospf_lsa *lsa);
96static void ospf_ext_link_lsa_schedule(struct ext_itf *exti,
97 enum lsa_opcode opcode);
98static void ospf_ext_lsa_schedule(struct ext_itf *exti, enum lsa_opcode op);
99static int ospf_ext_link_lsa_update(struct ospf_lsa *lsa);
100static int ospf_ext_pref_lsa_update(struct ospf_lsa *lsa);
6f751f14 101static void ospf_ext_link_delete_adj_sid(struct ext_itf *exti);
cf9b9f77
OD
102static void del_ext_info(void *val);
103
104/*
105 * Extended Link/Prefix initialization
106 *
107 * @param - none
108 *
109 * @return - 0 if OK, <> 0 otherwise
110 */
111int ospf_ext_init(void)
112{
113 int rc = 0;
114
115 memset(&OspfEXT, 0, sizeof(struct ospf_ext_lp));
116 OspfEXT.enabled = false;
117 /* Only Area flooding is supported yet */
118 OspfEXT.scope = OSPF_OPAQUE_AREA_LSA;
119 /* Initialize interface list */
120 OspfEXT.iflist = list_new();
121 OspfEXT.iflist->del = del_ext_info;
122
7743f2f8 123 zlog_info("EXT (%s): Register Extended Link Opaque LSA", __func__);
cf9b9f77
OD
124 rc = ospf_register_opaque_functab(
125 OSPF_OPAQUE_AREA_LSA, OPAQUE_TYPE_EXTENDED_LINK_LSA,
996c9314
LB
126 ospf_ext_link_new_if, /* new if */
127 ospf_ext_link_del_if, /* del if */
731271b0 128 ospf_ext_ism_change, /* ism change */
cf9b9f77
OD
129 ospf_ext_link_nsm_change, /* nsm change */
130 NULL, /* Write router config. */
131 NULL, /* Write interface conf. */
132 NULL, /* Write debug config. */
133 ospf_ext_link_show_info, /* Show LSA info */
134 ospf_ext_link_lsa_originate, /* Originate LSA */
135 ospf_ext_link_lsa_refresh, /* Refresh LSA */
136 ospf_ext_link_lsa_update, /* new_lsa_hook */
137 NULL); /* del_lsa_hook */
138
139 if (rc != 0) {
cf444bcf 140 flog_warn(EC_OSPF_OPAQUE_REGISTRATION,
85c9b439 141 "EXT (%s): Failed to register Extended Link LSA",
7743f2f8 142 __func__);
cf9b9f77
OD
143 return rc;
144 }
145
7743f2f8 146 zlog_info("EXT (%s): Register Extended Prefix Opaque LSA", __func__);
cf9b9f77
OD
147 rc = ospf_register_opaque_functab(
148 OspfEXT.scope, OPAQUE_TYPE_EXTENDED_PREFIX_LSA,
7743f2f8
OD
149 NULL, /* new if handle by link */
150 NULL, /* del if handle by link */
731271b0 151 NULL, /* ism change */
cf9b9f77
OD
152 NULL, /* nsm change */
153 ospf_sr_config_write_router, /* Write router config. */
154 NULL, /* Write interface conf. */
155 NULL, /* Write debug config. */
156 ospf_ext_pref_show_info, /* Show LSA info */
157 ospf_ext_pref_lsa_originate, /* Originate LSA */
158 ospf_ext_pref_lsa_refresh, /* Refresh LSA */
159 ospf_ext_pref_lsa_update, /* new_lsa_hook */
160 NULL); /* del_lsa_hook */
161 if (rc != 0) {
cf444bcf 162 flog_warn(EC_OSPF_OPAQUE_REGISTRATION,
85c9b439 163 "EXT (%s): Failed to register Extended Prefix LSA",
7743f2f8 164 __func__);
cf9b9f77
OD
165 return rc;
166 }
167
168 return rc;
169}
170
171/*
172 * Extended Link/Prefix termination function
173 *
bcf4475e 174 * @param - none
cf9b9f77
OD
175 * @return - none
176 */
177void ospf_ext_term(void)
178{
179
89f60109
DS
180 if ((OspfEXT.scope == OSPF_OPAQUE_AREA_LSA)
181 || (OspfEXT.scope == OSPF_OPAQUE_AS_LSA))
bcf4475e
OD
182 ospf_delete_opaque_functab(OspfEXT.scope,
183 OPAQUE_TYPE_EXTENDED_PREFIX_LSA);
184
185 ospf_delete_opaque_functab(OSPF_OPAQUE_AREA_LSA,
186 OPAQUE_TYPE_EXTENDED_LINK_LSA);
187
6a154c88 188 list_delete(&OspfEXT.iflist);
cf9b9f77
OD
189 OspfEXT.scope = 0;
190 OspfEXT.enabled = false;
191
bcf4475e
OD
192 return;
193}
194
195/*
196 * Extended Link/Prefix finish function
197 *
198 * @param - none
199 * @return - none
200 */
201void ospf_ext_finish(void)
202{
731271b0
OD
203
204 struct listnode *node;
205 struct ext_itf *exti;
206
207 /* Flush Router Info LSA */
208 for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti))
209 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
210 ospf_ext_lsa_schedule(exti, FLUSH_THIS_LSA);
211
bcf4475e 212 OspfEXT.enabled = false;
cf9b9f77
OD
213}
214
7743f2f8
OD
215/*
216 * ---------------------------------------------------------------------
cf9b9f77
OD
217 * Followings are control functions for Extended Prefix/Link Opaque LSA
218 * parameters management.
7743f2f8
OD
219 * ---------------------------------------------------------------------
220 */
221
cf9b9f77
OD
222/* Functions to free memory space */
223static void del_ext_info(void *val)
224{
225 XFREE(MTYPE_OSPF_EXT_PARAMS, val);
cf9b9f77
OD
226}
227
228/* Increment instance value for Extended Prefix Opaque LSAs Opaque ID field */
93f0a26e 229static uint32_t get_ext_pref_instance_value(void)
cf9b9f77 230{
93f0a26e 231 static uint32_t seqno = 0;
cf9b9f77
OD
232
233 if (seqno < MAX_LEGAL_EXT_INSTANCE_NUM)
234 seqno += 1;
235 else
236 seqno = 1; /* Avoid zero. */
237
238 return seqno;
239}
240
241/* Increment instance value for Extended Link Opaque LSAs Opaque ID field */
93f0a26e 242static uint32_t get_ext_link_instance_value(void)
cf9b9f77 243{
93f0a26e 244 static uint32_t seqno = 0;
cf9b9f77
OD
245
246 if (seqno < MAX_LEGAL_EXT_INSTANCE_NUM)
247 seqno += 1;
248 else
249 seqno = 1; /* Avoid zero. */
250
251 return seqno;
252}
253
254/* Lookup Extended Prefix/Links by ifp from OspfEXT struct iflist */
255static struct ext_itf *lookup_ext_by_ifp(struct interface *ifp)
256{
257 struct listnode *node, *nnode;
258 struct ext_itf *exti;
259
260 for (ALL_LIST_ELEMENTS(OspfEXT.iflist, node, nnode, exti))
261 if (exti->ifp == ifp)
262 return exti;
263
264 return NULL;
265}
266
267/* Lookup Extended Prefix/Links by LSA ID from OspfEXT struct iflist */
268static struct ext_itf *lookup_ext_by_instance(struct ospf_lsa *lsa)
269{
270 struct listnode *node;
271 struct ext_itf *exti;
db28a51f
OD
272 uint32_t key = GET_OPAQUE_ID(ntohl(lsa->data->id.s_addr));
273 uint8_t type = GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr));
274
cf9b9f77
OD
275
276 for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti))
db28a51f 277 if ((exti->instance == key) && (exti->type == type))
cf9b9f77
OD
278 return exti;
279
cf9b9f77
OD
280 return NULL;
281}
282
7743f2f8
OD
283/*
284 * ----------------------------------------------------------------------
cf9b9f77
OD
285 * The underlying subsection defines setters and unsetters to create and
286 * delete tlvs and subtlvs
7743f2f8
OD
287 * ----------------------------------------------------------------------
288 */
cf9b9f77
OD
289
290/* Extended Prefix TLV - RFC7684 section 2.1 */
93f0a26e
OD
291static void set_ext_prefix(struct ext_itf *exti, uint8_t route_type,
292 uint8_t flags, struct prefix_ipv4 p)
cf9b9f77
OD
293{
294
295 TLV_TYPE(exti->prefix) = htons(EXT_TLV_PREFIX);
296 /* Warning: Size must be adjust depending of subTLV's */
297 TLV_LEN(exti->prefix) = htons(EXT_TLV_PREFIX_SIZE);
298 exti->prefix.route_type = route_type;
299 exti->prefix.flags = flags;
300 /* Only Address Family Ipv4 (0) is defined in RFC 7684 */
301 exti->prefix.af = 0;
302 exti->prefix.pref_length = p.prefixlen;
303 exti->prefix.address = p.prefix;
270e66a2
OD
304
305 SET_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE);
cf9b9f77
OD
306}
307
308/* Extended Link TLV - RFC7684 section 3.1 */
93f0a26e 309static void set_ext_link(struct ext_itf *exti, uint8_t type, struct in_addr id,
cf9b9f77
OD
310 struct in_addr data)
311{
312
313 TLV_TYPE(exti->link) = htons(EXT_TLV_LINK);
314 /* Warning: Size must be adjust depending of subTLV's */
315 TLV_LEN(exti->link) = htons(EXT_TLV_LINK_SIZE);
316 exti->link.link_type = type;
317 exti->link.link_id = id;
318 exti->link.link_data = data;
319}
320
321/* Prefix SID SubTLV - section 5 */
93f0a26e 322static void set_prefix_sid(struct ext_itf *exti, uint8_t algorithm,
7743f2f8 323 uint32_t value, int value_type, uint8_t flags)
cf9b9f77
OD
324{
325
cf9b9f77
OD
326 if ((algorithm != SR_ALGORITHM_SPF)
327 && (algorithm != SR_ALGORITHM_STRICT_SPF)) {
cf444bcf 328 flog_err(EC_OSPF_INVALID_ALGORITHM,
89f60109
DS
329 "EXT (%s): unrecognized algorithm, not SPF or S-SPF",
330 __func__);
cf9b9f77
OD
331 return;
332 }
333
7743f2f8 334 /* Update flags according to the type of value field: label or index */
cf9b9f77 335 if (value_type == SID_LABEL)
7743f2f8 336 SET_FLAG(flags, EXT_SUBTLV_PREFIX_SID_VFLG);
cf9b9f77
OD
337
338 /* set prefix sid subtlv for an extended prefix tlv */
339 TLV_TYPE(exti->node_sid) = htons(EXT_SUBTLV_PREFIX_SID);
340 exti->node_sid.algorithm = algorithm;
341 exti->node_sid.flags = flags;
342 exti->node_sid.mtid = 0; /* Multi-Topology is not supported */
343
344 /* Set Label or Index value */
345 if (value_type == SID_LABEL) {
d922605d
OD
346 TLV_LEN(exti->node_sid) =
347 htons(SID_LABEL_SIZE(EXT_SUBTLV_PREFIX_SID_SIZE));
cf9b9f77
OD
348 exti->node_sid.value = htonl(SET_LABEL(value));
349 } else {
d922605d
OD
350 TLV_LEN(exti->node_sid) =
351 htons(SID_INDEX_SIZE(EXT_SUBTLV_PREFIX_SID_SIZE));
cf9b9f77
OD
352 exti->node_sid.value = htonl(value);
353 }
cf9b9f77
OD
354}
355
356/* Adjacency SID SubTLV - section 6.1 */
93f0a26e 357static void set_adj_sid(struct ext_itf *exti, bool backup, uint32_t value,
cf9b9f77
OD
358 int value_type)
359{
360 int index;
93f0a26e 361 uint8_t flags;
cf9b9f77
OD
362
363 /* Determine which ADJ_SID must be set: nominal or backup */
364 if (backup) {
365 flags = EXT_SUBTLV_LINK_ADJ_SID_BFLG;
366 index = 1;
367 } else {
368 index = 0;
369 flags = 0;
370 }
371
372 /* Set Header */
373 TLV_TYPE(exti->adj_sid[index]) = htons(EXT_SUBTLV_ADJ_SID);
374
375 /* Only Local ADJ-SID is supported for the moment */
376 SET_FLAG(flags, EXT_SUBTLV_LINK_ADJ_SID_LFLG);
377
378 exti->adj_sid[index].mtid = 0; /* Multi-Topology is not supported */
379
380 /* Adjust Length, Flags and Value depending on the type of Label */
381 if (value_type == SID_LABEL) {
382 SET_FLAG(flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG);
d922605d
OD
383 TLV_LEN(exti->adj_sid[index]) =
384 htons(SID_LABEL_SIZE(EXT_SUBTLV_ADJ_SID_SIZE));
cf9b9f77
OD
385 exti->adj_sid[index].value = htonl(SET_LABEL(value));
386 } else {
387 UNSET_FLAG(flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG);
d922605d
OD
388 TLV_LEN(exti->adj_sid[index]) =
389 htons(SID_INDEX_SIZE(EXT_SUBTLV_ADJ_SID_SIZE));
cf9b9f77
OD
390 exti->adj_sid[index].value = htonl(value);
391 }
392
393 exti->adj_sid[index].flags = flags; /* Set computed flags */
394 exti->adj_sid[index].mtid = 0; /* Multi-Topology is not supported */
395 exti->adj_sid[index].weight = 0; /* Load-Balancing is not supported */
cf9b9f77
OD
396}
397
398/* LAN Adjacency SID SubTLV - section 6.2 */
93f0a26e 399static void set_lan_adj_sid(struct ext_itf *exti, bool backup, uint32_t value,
cf9b9f77
OD
400 int value_type, struct in_addr neighbor_id)
401{
402
403 int index;
93f0a26e 404 uint8_t flags;
cf9b9f77
OD
405
406 /* Determine which ADJ_SID must be set: nominal or backup */
407 if (backup) {
408 flags = EXT_SUBTLV_LINK_ADJ_SID_BFLG;
409 index = 1;
410 } else {
411 index = 0;
412 flags = 0;
413 }
414
415 /* Set Header */
d922605d 416 TLV_TYPE(exti->lan_sid[index]) = htons(EXT_SUBTLV_LAN_ADJ_SID);
cf9b9f77
OD
417
418 /* Only Local ADJ-SID is supported for the moment */
419 SET_FLAG(flags, EXT_SUBTLV_LINK_ADJ_SID_LFLG);
420
421 /* Adjust Length, Flags and Value depending on the type of Label */
422 if (value_type == SID_LABEL) {
423 SET_FLAG(flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG);
d922605d
OD
424 TLV_LEN(exti->lan_sid[index]) =
425 htons(SID_LABEL_SIZE(EXT_SUBTLV_PREFIX_RANGE_SIZE));
cf9b9f77
OD
426 exti->lan_sid[index].value = htonl(SET_LABEL(value));
427 } else {
428 UNSET_FLAG(flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG);
d922605d
OD
429 TLV_LEN(exti->lan_sid[index]) =
430 htons(SID_INDEX_SIZE(EXT_SUBTLV_PREFIX_RANGE_SIZE));
cf9b9f77
OD
431 exti->lan_sid[index].value = htonl(value);
432 }
433
434 exti->lan_sid[index].flags = flags; /* Set computed flags */
435 exti->lan_sid[index].mtid = 0; /* Multi-Topology is not supported */
436 exti->lan_sid[index].weight = 0; /* Load-Balancing is not supported */
437 exti->lan_sid[index].neighbor_id = neighbor_id;
cf9b9f77
OD
438}
439
6f751f14
OD
440static void unset_adjacency_sid(struct ext_itf *exti)
441{
442 /* Reset Adjacency TLV */
443 if (exti->type == ADJ_SID) {
444 TLV_TYPE(exti->adj_sid[0]) = 0;
445 TLV_TYPE(exti->adj_sid[1]) = 0;
446 }
447 /* or Lan-Adjacency TLV */
448 if (exti->type == LAN_ADJ_SID) {
449 TLV_TYPE(exti->lan_sid[0]) = 0;
450 TLV_TYPE(exti->lan_sid[1]) = 0;
451 }
452}
453
cf9b9f77
OD
454/* Experimental SubTLV from Cisco */
455static void set_rmt_itf_addr(struct ext_itf *exti, struct in_addr rmtif)
456{
457
458 TLV_TYPE(exti->rmt_itf_addr) = htons(EXT_SUBTLV_RMT_ITF_ADDR);
459 TLV_LEN(exti->rmt_itf_addr) = htons(sizeof(struct in_addr));
460 exti->rmt_itf_addr.value = rmtif;
cf9b9f77
OD
461}
462
21baf89a
OD
463/* Delete Extended LSA */
464static void ospf_extended_lsa_delete(struct ext_itf *exti)
465{
466
a351b3e4
OD
467 /* Avoid deleting LSA if Extended is not enable */
468 if (!OspfEXT.enabled)
469 return;
470
21baf89a
OD
471 /* Process only Active Extended Prefix/Link LSA */
472 if (!CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE))
473 return;
474
475 osr_debug("EXT (%s): Disable %s%s%s-SID on interface %s", __func__,
6f751f14 476 exti->stype == LOCAL_SID ? "Prefix" : "",
21baf89a
OD
477 exti->stype == ADJ_SID ? "Adjacency" : "",
478 exti->stype == LAN_ADJ_SID ? "LAN-Adjacency" : "",
479 exti->ifp->name);
480
481 /* Flush LSA if already engaged */
482 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED)) {
483 ospf_ext_lsa_schedule(exti, FLUSH_THIS_LSA);
484 UNSET_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED);
485 }
486
487 /* De-activate this Extended Prefix/Link and remove corresponding
488 * Segment-Routing Prefix-SID or (LAN)-ADJ-SID */
6f751f14
OD
489 if (exti->stype == ADJ_SID || exti->stype == LAN_ADJ_SID)
490 ospf_ext_link_delete_adj_sid(exti);
491 else
492 ospf_sr_ext_itf_delete(exti);
21baf89a
OD
493}
494
cf9b9f77
OD
495/*
496 * Update Extended prefix SID index for Loopback interface type
497 *
498 * @param ifname - Loopback interface name
499 * @param index - new value for the prefix SID of this interface
500 * @param p - prefix for this interface or NULL if Extended Prefix
501 * should be remove
502 *
503 * @return instance number if update is OK, 0 otherwise
504 */
db28a51f 505uint32_t ospf_ext_schedule_prefix_index(struct interface *ifp, uint32_t index,
996c9314 506 struct prefix_ipv4 *p, uint8_t flags)
cf9b9f77
OD
507{
508 int rc = 0;
509 struct ext_itf *exti;
510
511 /* Find Extended Prefix interface */
512 exti = lookup_ext_by_ifp(ifp);
513 if (exti == NULL)
514 return rc;
515
516 if (p != NULL) {
3efd0893 517 osr_debug("EXT (%s): Schedule new prefix %pFX with index %u on interface %s", __func__, p, index, ifp->name);
cf9b9f77
OD
518
519 /* Set first Extended Prefix then the Prefix SID information */
520 set_ext_prefix(exti, OSPF_PATH_INTRA_AREA, EXT_TLV_PREF_NFLG,
521 *p);
7743f2f8 522 set_prefix_sid(exti, SR_ALGORITHM_SPF, index, SID_INDEX, flags);
cf9b9f77
OD
523
524 /* Try to Schedule LSA */
731271b0
OD
525 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE)) {
526 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
527 ospf_ext_pref_lsa_schedule(exti,
528 REFRESH_THIS_LSA);
529 else
530 ospf_ext_pref_lsa_schedule(
531 exti, REORIGINATE_THIS_LSA);
532 }
cf9b9f77 533 } else {
b37eb79c
OD
534 osr_debug("EXT (%s): Remove prefix for interface %s", __func__,
535 ifp->name);
cf9b9f77 536
0d174b66 537 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
cf9b9f77 538 ospf_ext_pref_lsa_schedule(exti, FLUSH_THIS_LSA);
cf9b9f77
OD
539 }
540
db28a51f 541 return SET_OPAQUE_LSID(exti->type, exti->instance);
cf9b9f77
OD
542}
543
6f751f14
OD
544/**
545 * Update Adjacecny-SID for Extended Link LSA
546 *
547 * @param exti Extended Link information
548 */
549static void ospf_ext_link_update_adj_sid(struct ext_itf *exti)
550{
551 mpls_label_t label;
552 mpls_label_t bck_label;
553
554 /* Process only (LAN)Adjacency-SID Type */
555 if (exti->stype != ADJ_SID && exti->stype != LAN_ADJ_SID)
556 return;
557
558 /* Request Primary & Backup Labels from Label Manager */
559 bck_label = ospf_sr_local_block_request_label();
560 label = ospf_sr_local_block_request_label();
561 if (bck_label == MPLS_INVALID_LABEL || label == MPLS_INVALID_LABEL) {
562 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
563 ospf_ext_lsa_schedule(exti, FLUSH_THIS_LSA);
564 return;
565 }
566
567 /* Set Adjacency-SID, backup first */
568 if (exti->stype == ADJ_SID) {
569 set_adj_sid(exti, true, bck_label, SID_LABEL);
570 set_adj_sid(exti, false, label, SID_LABEL);
571 } else {
83df36e8 572 set_lan_adj_sid(exti, true, bck_label, SID_LABEL,
6f751f14 573 exti->lan_sid[0].neighbor_id);
83df36e8 574 set_lan_adj_sid(exti, false, label, SID_LABEL,
6f751f14
OD
575 exti->lan_sid[1].neighbor_id);
576 }
577
578 /* Finally, add corresponding SR Link in SRDB & MPLS LFIB */
579 SET_FLAG(exti->flags, EXT_LPFLG_FIB_ENTRY_SET);
580 ospf_sr_ext_itf_add(exti);
581}
582
583/**
584 * Delete Adjacecny-SID for Extended Link LSA
585 *
586 * @param exti Extended Link information
587 */
588static void ospf_ext_link_delete_adj_sid(struct ext_itf *exti)
589{
590 /* Process only (LAN)Adjacency-SID Type */
591 if (exti->stype != ADJ_SID && exti->stype != LAN_ADJ_SID)
592 return;
593
594 /* Release Primary & Backup Labels from Label Manager */
595 if (exti->stype == ADJ_SID) {
596 ospf_sr_local_block_release_label(exti->adj_sid[0].value);
597 ospf_sr_local_block_release_label(exti->adj_sid[1].value);
598 } else {
83df36e8
OD
599 ospf_sr_local_block_release_label(exti->lan_sid[0].value);
600 ospf_sr_local_block_release_label(exti->lan_sid[1].value);
6f751f14
OD
601 }
602 /* And reset corresponding TLV */
603 unset_adjacency_sid(exti);
604
605 /* Finally, remove corresponding SR Link in SRDB & MPLS LFIB */
606 UNSET_FLAG(exti->flags, EXT_LPFLG_FIB_ENTRY_SET);
607 ospf_sr_ext_itf_delete(exti);
608}
609
610/**
611 * Update Extended Link LSA once Segment Routing Label Block has been changed.
612 */
613void ospf_ext_link_srlb_update(void)
614{
615 struct listnode *node;
616 struct ext_itf *exti;
617
618
619 osr_debug("EXT (%s): Update Extended Links with new SRLB", __func__);
620
621 /* Update all Extended Link Adjaceny-SID */
622 for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti)) {
623 /* Skip Extended Prefix */
624 if (exti->stype == PREF_SID || exti->stype == LOCAL_SID)
625 continue;
626
627 /* Skip inactive Extended Link */
628 if (!CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE))
629 continue;
630
631 ospf_ext_link_update_adj_sid(exti);
632 }
633}
634
cf9b9f77
OD
635/*
636 * Used by Segment Routing to activate/deactivate Extended Link/Prefix flooding
637 *
638 * @param enable To activate or not Segment Routing Extended LSA flooding
639 *
640 * @return none
641 */
642void ospf_ext_update_sr(bool enable)
643{
644 struct listnode *node;
645 struct ext_itf *exti;
646
b37eb79c
OD
647 osr_debug("EXT (%s): %s Extended LSAs for Segment Routing ", __func__,
648 enable ? "Enable" : "Disable");
cf9b9f77
OD
649
650 if (enable) {
651 OspfEXT.enabled = true;
731271b0 652
cf9b9f77 653 /* Refresh LSAs if already engaged or originate */
731271b0 654 for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti)) {
6f751f14 655 /* Skip Inactive Extended Link */
731271b0
OD
656 if (!CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE))
657 continue;
658
6f751f14
OD
659 /* Update Extended Link (LAN)Adj-SID if not set */
660 if (!CHECK_FLAG(exti->flags, EXT_LPFLG_FIB_ENTRY_SET))
661 ospf_ext_link_update_adj_sid(exti);
662
663 /* Finally, flood the extended Link */
cf9b9f77
OD
664 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
665 ospf_ext_lsa_schedule(exti, REFRESH_THIS_LSA);
666 else
667 ospf_ext_lsa_schedule(exti,
668 REORIGINATE_THIS_LSA);
731271b0 669 }
cf9b9f77 670 } else {
21baf89a
OD
671 /* Start by Removing Extended LSA */
672 for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti))
673 ospf_extended_lsa_delete(exti);
731271b0 674
cf9b9f77
OD
675 /* And then disable Extended Link/Prefix */
676 OspfEXT.enabled = false;
677 }
678}
21baf89a 679
7743f2f8
OD
680/*
681 * -----------------------------------------------------------------------
682 * Followings are callback functions against generic Opaque-LSAs handling
683 * -----------------------------------------------------------------------
684 */
cf9b9f77
OD
685
686/* Add new Interface in Extended Interface List */
687static int ospf_ext_link_new_if(struct interface *ifp)
688{
689 struct ext_itf *new;
690 int rc = -1;
691
692 if (lookup_ext_by_ifp(ifp) != NULL) {
cf9b9f77
OD
693 rc = 0; /* Do nothing here. */
694 return rc;
695 }
696
697 new = XCALLOC(MTYPE_OSPF_EXT_PARAMS, sizeof(struct ext_itf));
cf9b9f77
OD
698
699 /* initialize new information and link back the interface */
700 new->ifp = ifp;
701 new->flags = EXT_LPFLG_LSA_INACTIVE;
702
703 listnode_add(OspfEXT.iflist, new);
704
705 rc = 0;
706 return rc;
707}
708
709/* Remove existing Interface from Extended Interface List */
710static int ospf_ext_link_del_if(struct interface *ifp)
711{
712 struct ext_itf *exti;
713 int rc = -1;
714
7743f2f8
OD
715 exti = lookup_ext_by_ifp(ifp);
716 if (exti != NULL) {
21baf89a
OD
717 /* Flush LSA and remove Adjacency SID */
718 ospf_extended_lsa_delete(exti);
cf9b9f77 719
cf9b9f77 720 /* Dequeue listnode entry from the list. */
21baf89a 721 listnode_delete(OspfEXT.iflist, exti);
cf9b9f77 722
cf9b9f77
OD
723 XFREE(MTYPE_OSPF_EXT_PARAMS, exti);
724
725 rc = 0;
726 } else {
cf444bcf 727 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
89f60109 728 "EXT (%s): interface %s is not found", __func__,
996c9314 729 ifp ? ifp->name : "-");
cf9b9f77
OD
730 }
731
732 return rc;
733}
734
7743f2f8 735/*
731271b0
OD
736 * Determine if an Interface belongs to an Extended Link Adjacency or
737 * Extended Prefix SID type and allocate new instance value accordingly
cf9b9f77 738 */
731271b0 739static void ospf_ext_ism_change(struct ospf_interface *oi, int old_status)
cf9b9f77
OD
740{
741 struct ext_itf *exti;
742
743 /* Get interface information for Segment Routing */
7743f2f8
OD
744 exti = lookup_ext_by_ifp(oi->ifp);
745 if (exti == NULL) {
cf444bcf 746 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
89f60109 747 "EXT (%s): Cannot get Extended info. from OI(%s)",
996c9314 748 __func__, IF_NAME(oi));
cf9b9f77
OD
749 return;
750 }
751
731271b0
OD
752 /* Reset Extended information if ospf interface goes Down */
753 if (oi->state == ISM_Down) {
21baf89a 754 ospf_extended_lsa_delete(exti);
731271b0
OD
755 exti->area = NULL;
756 exti->flags = EXT_LPFLG_LSA_INACTIVE;
757 return;
758 }
759
760 /* Determine if interface is related to a Prefix or an Adjacency SID */
cf9b9f77
OD
761 if (oi->type == OSPF_IFTYPE_LOOPBACK) {
762 exti->stype = PREF_SID;
db28a51f 763 exti->type = OPAQUE_TYPE_EXTENDED_PREFIX_LSA;
731271b0
OD
764 exti->instance = get_ext_pref_instance_value();
765 exti->area = oi->area;
7743f2f8 766
7743f2f8 767 /* Complete SRDB if the interface belongs to a Prefix */
a351b3e4
OD
768 if (OspfEXT.enabled) {
769 osr_debug("EXT (%s): Set Prefix SID to interface %s ",
770 __func__, oi->ifp->name);
b37eb79c 771 ospf_sr_update_local_prefix(oi->ifp, oi->address);
a351b3e4 772 }
731271b0
OD
773 } else {
774 /* Determine if interface is related to Adj. or LAN Adj. SID */
775 if (oi->state == ISM_DR)
776 exti->stype = LAN_ADJ_SID;
777 else
778 exti->stype = ADJ_SID;
779
780 exti->type = OPAQUE_TYPE_EXTENDED_LINK_LSA;
781 exti->instance = get_ext_link_instance_value();
782 exti->area = oi->area;
783
784 /*
785 * Note: Adjacency SID information are completed when ospf
786 * adjacency become up see ospf_ext_link_nsm_change()
787 */
a351b3e4
OD
788 if (OspfEXT.enabled)
789 osr_debug(
790 "EXT (%s): Set %sAdjacency SID for interface %s ",
791 __func__, exti->stype == ADJ_SID ? "" : "LAN-",
792 oi->ifp->name);
cf9b9f77 793 }
cf9b9f77
OD
794}
795
796/*
797 * Finish Extended Link configuration and flood corresponding LSA
798 * when OSPF adjacency on this link fire up
799 */
800static void ospf_ext_link_nsm_change(struct ospf_neighbor *nbr, int old_status)
801{
802 struct ospf_interface *oi = nbr->oi;
803 struct ext_itf *exti;
cf9b9f77 804
21baf89a
OD
805 /* Process Link only when neighbor old or new state is NSM Full */
806 if (nbr->state != NSM_Full && old_status != NSM_Full)
cf9b9f77
OD
807 return;
808
809 /* Get interface information for Segment Routing */
7743f2f8
OD
810 exti = lookup_ext_by_ifp(oi->ifp);
811 if (exti == NULL) {
cf444bcf 812 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
89f60109 813 "EXT (%s): Cannot get Extended info. from OI(%s)",
996c9314 814 __func__, IF_NAME(oi));
cf9b9f77
OD
815 return;
816 }
817
21baf89a
OD
818 /* Check that we have a valid area and ospf context */
819 if (oi->area == NULL || oi->area->ospf == NULL) {
820 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
821 "EXT (%s): Cannot refer to OSPF from OI(%s)",
822 __func__, IF_NAME(oi));
823 return;
824 }
825
826 /* Remove Extended Link if Neighbor State goes Down or Deleted */
a351b3e4
OD
827 if (OspfEXT.enabled
828 && (nbr->state == NSM_Down || nbr->state == NSM_Deleted)) {
6f751f14
OD
829 ospf_ext_link_delete_adj_sid(exti);
830 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
831 ospf_ext_link_lsa_schedule(exti, FLUSH_THIS_LSA);
832 exti->flags = EXT_LPFLG_LSA_INACTIVE;
21baf89a
OD
833 return;
834 }
835
836 /* Keep Area information in combination with SR info. */
837 exti->area = oi->area;
838
cf9b9f77
OD
839 /* Process only Adjacency/LAN SID */
840 if (exti->stype == PREF_SID)
841 return;
842
843 switch (oi->state) {
844 case ISM_PointToPoint:
845 /* Segment ID is an Adjacency one */
846 exti->stype = ADJ_SID;
847
848 /* Set Extended Link TLV with link_id == Nbr Router ID */
849 set_ext_link(exti, OSPF_IFTYPE_POINTOPOINT, nbr->router_id,
850 oi->address->u.prefix4);
851
cf9b9f77
OD
852 /* And Remote Interface address */
853 set_rmt_itf_addr(exti, nbr->address.u.prefix4);
854
855 break;
856
857 case ISM_DR:
858 /* Segment ID is a LAN Adjacency for the DR only */
859 exti->stype = LAN_ADJ_SID;
860
861 /* Set Extended Link TLV with link_id == DR */
862 set_ext_link(exti, OSPF_IFTYPE_BROADCAST, DR(oi),
863 oi->address->u.prefix4);
864
6f751f14
OD
865 /* Set Neighbor ID */
866 exti->lan_sid[0].neighbor_id = nbr->router_id;
867 exti->lan_sid[1].neighbor_id = nbr->router_id;
cf9b9f77
OD
868
869 break;
870
871 case ISM_DROther:
872 case ISM_Backup:
873 /* Segment ID is an Adjacency if not the DR */
874 exti->stype = ADJ_SID;
875
876 /* Set Extended Link TLV with link_id == DR */
877 set_ext_link(exti, OSPF_IFTYPE_BROADCAST, DR(oi),
878 oi->address->u.prefix4);
879
cf9b9f77
OD
880 break;
881
882 default:
6f751f14
OD
883 if (CHECK_FLAG(exti->flags, EXT_LPFLG_FIB_ENTRY_SET))
884 ospf_ext_link_delete_adj_sid(exti);
731271b0 885 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
cf9b9f77 886 ospf_ext_link_lsa_schedule(exti, FLUSH_THIS_LSA);
731271b0 887 exti->flags = EXT_LPFLG_LSA_INACTIVE;
cf9b9f77
OD
888 return;
889 }
890
cf9b9f77 891 SET_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE);
6f751f14 892
cf9b9f77 893 if (OspfEXT.enabled) {
6f751f14
OD
894 osr_debug("EXT (%s): Set %sAdjacency SID for interface %s ",
895 __func__, exti->stype == ADJ_SID ? "" : "LAN-",
896 oi->ifp->name);
897
898 /* Update (LAN)Adjacency SID */
899 ospf_ext_link_update_adj_sid(exti);
900
901 /* flood this links params if everything is ok */
cf9b9f77
OD
902 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED))
903 ospf_ext_link_lsa_schedule(exti, REFRESH_THIS_LSA);
904 else
905 ospf_ext_link_lsa_schedule(exti, REORIGINATE_THIS_LSA);
906 }
cf9b9f77
OD
907}
908
909/* Callbacks to handle Extended Link Segment Routing LSA information */
910static int ospf_ext_link_lsa_update(struct ospf_lsa *lsa)
911{
912 /* Sanity Check */
913 if (lsa == NULL) {
cf444bcf 914 flog_warn(EC_OSPF_LSA_NULL, "EXT (%s): Abort! LSA is NULL",
ade6974d 915 __func__);
cf9b9f77
OD
916 return -1;
917 }
918
6aaf0fdd
OD
919 /* Process only Opaque LSA */
920 if ((lsa->data->type != OSPF_OPAQUE_AREA_LSA)
921 && (lsa->data->type != OSPF_OPAQUE_AS_LSA))
922 return 0;
923
cf9b9f77
OD
924 /* Process only Extended Link LSA */
925 if (GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr))
926 != OPAQUE_TYPE_EXTENDED_LINK_LSA)
927 return 0;
928
21baf89a
OD
929 /* Check if it is not my LSA */
930 if (IS_LSA_SELF(lsa))
931 return 0;
932
cf9b9f77
OD
933 /* Check if Extended is enable */
934 if (!OspfEXT.enabled)
935 return 0;
936
937 /* Call Segment Routing LSA update or deletion */
938 if (!IS_LSA_MAXAGE(lsa))
939 ospf_sr_ext_link_lsa_update(lsa);
940 else
941 ospf_sr_ext_link_lsa_delete(lsa);
942
943 return 0;
944}
945
946/* Callbacks to handle Extended Prefix Segment Routing LSA information */
947static int ospf_ext_pref_lsa_update(struct ospf_lsa *lsa)
948{
949
950 /* Sanity Check */
951 if (lsa == NULL) {
cf444bcf 952 flog_warn(EC_OSPF_LSA_NULL, "EXT (%s): Abort! LSA is NULL",
ade6974d 953 __func__);
cf9b9f77
OD
954 return -1;
955 }
956
6aaf0fdd
OD
957 /* Process only Opaque LSA */
958 if ((lsa->data->type != OSPF_OPAQUE_AREA_LSA)
959 && (lsa->data->type != OSPF_OPAQUE_AS_LSA))
7743f2f8
OD
960 return 0;
961
cf9b9f77
OD
962 /* Process only Extended Prefix LSA */
963 if (GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr))
964 != OPAQUE_TYPE_EXTENDED_PREFIX_LSA)
965 return 0;
966
6aaf0fdd
OD
967 /* Check if it is not my LSA */
968 if (IS_LSA_SELF(lsa))
969 return 0;
970
cf9b9f77
OD
971 /* Check if Extended is enable */
972 if (!OspfEXT.enabled)
973 return 0;
974
975 /* Call Segment Routing LSA update or deletion */
976 if (!IS_LSA_MAXAGE(lsa))
977 ospf_sr_ext_prefix_lsa_update(lsa);
978 else
979 ospf_sr_ext_prefix_lsa_delete(lsa);
980
981 return 0;
982}
983
7743f2f8
OD
984/*
985 * -------------------------------------------------------
cf9b9f77
OD
986 * Followings are OSPF protocol processing functions for
987 * Extended Prefix/Link Opaque LSA
7743f2f8
OD
988 * -------------------------------------------------------
989 */
cf9b9f77
OD
990
991static void build_tlv_header(struct stream *s, struct tlv_header *tlvh)
992{
993 stream_put(s, tlvh, sizeof(struct tlv_header));
cf9b9f77
OD
994}
995
996static void build_tlv(struct stream *s, struct tlv_header *tlvh)
997{
998
999 if ((tlvh != NULL) && (ntohs(tlvh->type) != 0)) {
1000 build_tlv_header(s, tlvh);
1001 stream_put(s, TLV_DATA(tlvh), TLV_BODY_SIZE(tlvh));
1002 }
cf9b9f77
OD
1003}
1004
1005/* Build an Extended Prefix Opaque LSA body for extended prefix TLV */
1006static void ospf_ext_pref_lsa_body_set(struct stream *s, struct ext_itf *exti)
1007{
1008
1009 /* Sanity check */
1010 if ((exti == NULL) || (exti->stype != PREF_SID))
1011 return;
1012
1013 /* Adjust Extended Prefix TLV size */
996c9314
LB
1014 TLV_LEN(exti->prefix) = htons(ntohs(TLV_LEN(exti->node_sid))
1015 + EXT_TLV_PREFIX_SIZE + TLV_HDR_SIZE);
cf9b9f77
OD
1016
1017 /* Build LSA body for an Extended Prefix TLV */
1018 build_tlv_header(s, &exti->prefix.header);
1019 stream_put(s, TLV_DATA(&exti->prefix.header), EXT_TLV_PREFIX_SIZE);
1020 /* Then add Prefix SID SubTLV */
1021 build_tlv(s, &exti->node_sid.header);
cf9b9f77
OD
1022}
1023
1024/* Build an Extended Link Opaque LSA body for extended link TLV */
1025static void ospf_ext_link_lsa_body_set(struct stream *s, struct ext_itf *exti)
1026{
7743f2f8 1027 size_t size;
cf9b9f77
OD
1028
1029 /* Sanity check */
1030 if ((exti == NULL)
1031 || ((exti->stype != ADJ_SID) && (exti->stype != LAN_ADJ_SID)))
1032 return;
1033
1034 if (exti->stype == ADJ_SID) {
1035 /* Adjust Extended Link TLV size for Adj. SID */
7743f2f8 1036 size = EXT_TLV_LINK_SIZE + 2 * EXT_SUBTLV_ADJ_SID_SIZE
996c9314 1037 + 2 * TLV_HDR_SIZE;
7743f2f8
OD
1038 if (ntohs(TLV_TYPE(exti->rmt_itf_addr)) != 0)
1039 size = size + EXT_SUBTLV_RMT_ITF_ADDR_SIZE
996c9314 1040 + TLV_HDR_SIZE;
7743f2f8 1041 TLV_LEN(exti->link) = htons(size);
cf9b9f77
OD
1042
1043 /* Build LSA body for an Extended Link TLV with Adj. SID */
1044 build_tlv_header(s, &exti->link.header);
1045 stream_put(s, TLV_DATA(&exti->link.header), EXT_TLV_LINK_SIZE);
d922605d 1046 /* then add Adjacency SubTLVs */
cf9b9f77
OD
1047 build_tlv(s, &exti->adj_sid[1].header);
1048 build_tlv(s, &exti->adj_sid[0].header);
7743f2f8
OD
1049
1050 /* Add Cisco experimental SubTLV if interface is PtoP */
1051 if (ntohs(TLV_TYPE(exti->rmt_itf_addr)) != 0)
1052 build_tlv(s, &exti->rmt_itf_addr.header);
cf9b9f77
OD
1053 } else {
1054 /* Adjust Extended Link TLV size for LAN SID */
7743f2f8 1055 size = EXT_TLV_LINK_SIZE
996c9314 1056 + 2 * (EXT_SUBTLV_LAN_ADJ_SID_SIZE + TLV_HDR_SIZE);
7743f2f8 1057 TLV_LEN(exti->link) = htons(size);
cf9b9f77
OD
1058
1059 /* Build LSA body for an Extended Link TLV with LAN SID */
1060 build_tlv_header(s, &exti->link.header);
d922605d
OD
1061 stream_put(s, TLV_DATA(&exti->link.header), EXT_TLV_LINK_SIZE);
1062 /* then add LAN-Adjacency SubTLVs */
cf9b9f77
OD
1063 build_tlv(s, &exti->lan_sid[1].header);
1064 build_tlv(s, &exti->lan_sid[0].header);
1065 }
cf9b9f77
OD
1066}
1067
1068/* Create new Extended Prefix opaque-LSA for every extended prefix */
1069static struct ospf_lsa *ospf_ext_pref_lsa_new(struct ospf_area *area,
1070 struct ext_itf *exti)
1071{
1072 struct stream *s;
1073 struct lsa_header *lsah;
1074 struct ospf_lsa *new = NULL;
7743f2f8 1075 struct ospf *top;
d7c0a89a 1076 uint8_t options, lsa_type;
cf9b9f77
OD
1077 struct in_addr lsa_id;
1078 struct in_addr router_id;
93f0a26e
OD
1079 uint32_t tmp;
1080 uint16_t length;
cf9b9f77 1081
fd3b19f2
OD
1082 /* Sanity Check */
1083 if (exti == NULL)
1084 return NULL;
1085
cf9b9f77 1086 /* Create a stream for LSA. */
7743f2f8 1087 s = stream_new(OSPF_MAX_LSA_SIZE);
cf9b9f77
OD
1088
1089 /* Prepare LSA Header */
1090 lsah = (struct lsa_header *)STREAM_DATA(s);
1091
1092 lsa_type = OspfEXT.scope;
1093
7743f2f8
OD
1094 /*
1095 * LSA ID is a variable number identifying different instances of
1096 * Extended Prefix Opaque LSA from the same router see RFC 7684
1097 */
cf9b9f77
OD
1098 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_PREFIX_LSA, exti->instance);
1099 lsa_id.s_addr = htonl(tmp);
1100
1101 options = OSPF_OPTION_O; /* Don't forget this :-) */
1102
1103 /* Fix Options and Router ID depending of the flooding scope */
1104 if ((OspfEXT.scope == OSPF_OPAQUE_AS_LSA) || (area == NULL)) {
1105 options = OSPF_OPTION_E;
7743f2f8
OD
1106 top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
1107 router_id.s_addr = top ? top->router_id.s_addr : 0;
cf9b9f77
OD
1108 } else {
1109 options |= LSA_OPTIONS_GET(area); /* Get area default option */
1110 options |= LSA_OPTIONS_NSSA_GET(area);
1111 router_id = area->ospf->router_id;
1112 }
1113
1114 /* Set opaque-LSA header fields. */
1115 lsa_header_set(s, options, lsa_type, lsa_id, router_id);
1116
b37eb79c 1117 osr_debug(
3efd0893 1118 "EXT (%s): LSA[Type%u:%pI4]: Create an Opaque-LSA Extended Prefix Opaque LSA instance",
b37eb79c 1119 __func__, lsa_type, &lsa_id);
cf9b9f77
OD
1120
1121 /* Set opaque-LSA body fields. */
1122 ospf_ext_pref_lsa_body_set(s, exti);
1123
1124 /* Set length. */
1125 length = stream_get_endp(s);
1126 lsah->length = htons(length);
1127
1128 /* Now, create an OSPF LSA instance. */
5b3d4186 1129 new = ospf_lsa_new_and_data(length);
cf9b9f77 1130
7743f2f8
OD
1131 /* Segment Routing belongs only to default VRF */
1132 new->vrf_id = VRF_DEFAULT;
cf9b9f77
OD
1133 new->area = area;
1134 SET_FLAG(new->flags, OSPF_LSA_SELF);
1135 memcpy(new->data, lsah, length);
1136 stream_free(s);
1137
1138 return new;
1139}
1140
1141/* Create new Extended Link opaque-LSA for every extended link TLV */
1142static struct ospf_lsa *ospf_ext_link_lsa_new(struct ospf_area *area,
1143 struct ext_itf *exti)
1144{
1145 struct stream *s;
1146 struct lsa_header *lsah;
1147 struct ospf_lsa *new = NULL;
d7c0a89a 1148 uint8_t options, lsa_type;
cf9b9f77 1149 struct in_addr lsa_id;
93f0a26e
OD
1150 uint32_t tmp;
1151 uint16_t length;
cf9b9f77 1152
fd3b19f2
OD
1153 /* Sanity Check */
1154 if (exti == NULL)
1155 return NULL;
1156
cf9b9f77 1157 /* Create a stream for LSA. */
7743f2f8 1158 s = stream_new(OSPF_MAX_LSA_SIZE);
cf9b9f77
OD
1159 lsah = (struct lsa_header *)STREAM_DATA(s);
1160
1161 options = OSPF_OPTION_O; /* Don't forget this :-) */
1162 options |= LSA_OPTIONS_GET(area); /* Get area default option */
1163 options |= LSA_OPTIONS_NSSA_GET(area);
1164 /* Extended Link Opaque LSA are only flooded within an area */
1165 lsa_type = OSPF_OPAQUE_AREA_LSA;
1166
7743f2f8
OD
1167 /*
1168 * LSA ID is a variable number identifying different instances of
1169 * Extended Link Opaque LSA from the same router see RFC 7684
1170 */
cf9b9f77
OD
1171 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_LINK_LSA, exti->instance);
1172 lsa_id.s_addr = htonl(tmp);
1173
b37eb79c 1174 osr_debug(
3efd0893 1175 "EXT (%s) LSA[Type%u:%pI4]: Create an Opaque-LSA Extended Link Opaque LSA instance",
b37eb79c 1176 __func__, lsa_type, &lsa_id);
cf9b9f77
OD
1177
1178 /* Set opaque-LSA header fields. */
1179 lsa_header_set(s, options, lsa_type, lsa_id, area->ospf->router_id);
1180
1181 /* Set opaque-LSA body fields. */
1182 ospf_ext_link_lsa_body_set(s, exti);
1183
1184 /* Set length. */
1185 length = stream_get_endp(s);
1186 lsah->length = htons(length);
1187
1188 /* Now, create an OSPF LSA instance. */
5b3d4186 1189 new = ospf_lsa_new_and_data(length);
cf9b9f77 1190
7743f2f8
OD
1191 /* Segment Routing belongs only to default VRF */
1192 new->vrf_id = VRF_DEFAULT;
cf9b9f77
OD
1193 new->area = area;
1194 SET_FLAG(new->flags, OSPF_LSA_SELF);
1195 memcpy(new->data, lsah, length);
1196 stream_free(s);
1197
1198 return new;
1199}
1200
7743f2f8
OD
1201/*
1202 * Process the origination of an Extended Prefix Opaque LSA
1203 * for every extended prefix TLV
1204 */
cf9b9f77
OD
1205static int ospf_ext_pref_lsa_originate1(struct ospf_area *area,
1206 struct ext_itf *exti)
1207{
1208 struct ospf_lsa *new;
1209 int rc = -1;
1210
1211
1212 /* Create new Opaque-LSA/Extended Prefix Opaque LSA instance. */
7743f2f8
OD
1213 new = ospf_ext_pref_lsa_new(area, exti);
1214 if (new == NULL) {
cf444bcf 1215 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
89f60109 1216 "EXT (%s): ospf_ext_pref_lsa_new() error", __func__);
cf9b9f77
OD
1217 return rc;
1218 }
1219
1220 /* Install this LSA into LSDB. */
1221 if (ospf_lsa_install(area->ospf, NULL /*oi */, new) == NULL) {
cf444bcf 1222 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
542a208f 1223 "EXT (%s): ospf_lsa_install() error", __func__);
cf9b9f77
OD
1224 ospf_lsa_unlock(&new);
1225 return rc;
1226 }
1227
1228 /* Now this Extended Prefix Opaque LSA info parameter entry has
7743f2f8
OD
1229 * associated LSA.
1230 */
cf9b9f77
OD
1231 SET_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED);
1232
1233 /* Update new LSA origination count. */
1234 area->ospf->lsa_originate_count++;
1235
1236 /* Flood new LSA through area. */
1237 ospf_flood_through_area(area, NULL /*nbr */, new);
1238
b37eb79c 1239 osr_debug(
3efd0893 1240 "EXT (%s): LSA[Type%u:%pI4]: Originate Opaque-LSAExtended Prefix Opaque LSA: Area(%pI4), Link(%s)",
b37eb79c
OD
1241 __func__, new->data->type, &new->data->id,
1242 &area->area_id, exti->ifp->name);
1243 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
cf9b9f77 1244 ospf_lsa_header_dump(new->data);
cf9b9f77
OD
1245
1246 rc = 0;
1247
1248 return rc;
1249}
1250
7743f2f8
OD
1251/*
1252 * Process the origination of an Extended Link Opaque LSA
1253 * for every extended link TLV
1254 */
cf9b9f77
OD
1255static int ospf_ext_link_lsa_originate1(struct ospf_area *area,
1256 struct ext_itf *exti)
1257{
1258 struct ospf_lsa *new;
1259 int rc = -1;
1260
1261 /* Create new Opaque-LSA/Extended Link Opaque LSA instance. */
7743f2f8
OD
1262 new = ospf_ext_link_lsa_new(area, exti);
1263 if (new == NULL) {
cf444bcf 1264 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
89f60109 1265 "EXT (%s): ospf_ext_link_lsa_new() error", __func__);
cf9b9f77
OD
1266 return rc;
1267 }
1268
1269 /* Install this LSA into LSDB. */
1270 if (ospf_lsa_install(area->ospf, NULL /*oi */, new) == NULL) {
cf444bcf 1271 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
542a208f 1272 "EXT (%s): ospf_lsa_install() error", __func__);
cf9b9f77
OD
1273 ospf_lsa_unlock(&new);
1274 return rc;
1275 }
1276
1277 /* Now this link-parameter entry has associated LSA. */
1278 SET_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED);
1279
1280 /* Update new LSA origination count. */
1281 area->ospf->lsa_originate_count++;
1282
1283 /* Flood new LSA through area. */
1284 ospf_flood_through_area(area, NULL /*nbr */, new);
1285
b37eb79c 1286 osr_debug(
3efd0893 1287 "EXT (%s): LSA[Type%u:%pI4]: Originate Opaque-LSA Extended Link Opaque LSA: Area(%pI4), Link(%s)",
b37eb79c
OD
1288 __func__, new->data->type, &new->data->id,
1289 &area->area_id, exti->ifp->name);
1290 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
cf9b9f77 1291 ospf_lsa_header_dump(new->data);
cf9b9f77
OD
1292
1293 rc = 0;
1294
1295 return rc;
1296}
1297
1298/* Trigger the origination of Extended Prefix Opaque LSAs */
1299static int ospf_ext_pref_lsa_originate(void *arg)
1300{
1301 struct ospf_area *area = (struct ospf_area *)arg;
1302 struct listnode *node;
1303 struct ext_itf *exti;
1304 int rc = -1;
1305
1306 if (!OspfEXT.enabled) {
1307 zlog_info(
b37eb79c 1308 "EXT (%s): Segment Routing functionality is Disabled now",
996c9314 1309 __func__);
cf9b9f77
OD
1310 rc = 0; /* This is not an error case. */
1311 return rc;
1312 }
b37eb79c
OD
1313 osr_debug("EXT (%s): Start Originate Prefix LSA for area %pI4",
1314 __func__, &area->area_id);
cf9b9f77
OD
1315
1316 /* Check if Extended Prefix Opaque LSA is already engaged */
1317 for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti)) {
1318
1319 /* Process only Prefix SID */
1320 if (exti->stype != PREF_SID)
1321 continue;
1322
1323 /* Process only Extended Prefix with valid Area ID */
1324 if ((exti->area == NULL)
1325 || (!IPV4_ADDR_SAME(&exti->area->area_id, &area->area_id)))
1326 continue;
1327
1328 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED)) {
1329 if (CHECK_FLAG(exti->flags,
1330 EXT_LPFLG_LSA_FORCED_REFRESH)) {
ade6974d 1331 flog_warn(
cf444bcf 1332 EC_OSPF_EXT_LSA_UNEXPECTED,
ade6974d 1333 "EXT (%s): Refresh instead of Originate",
996c9314 1334 __func__);
cf9b9f77
OD
1335 UNSET_FLAG(exti->flags,
1336 EXT_LPFLG_LSA_FORCED_REFRESH);
1337 ospf_ext_pref_lsa_schedule(exti,
1338 REFRESH_THIS_LSA);
1339 }
1340 continue;
1341 }
1342
1343 /* Ok, let's try to originate an LSA */
b37eb79c 1344 osr_debug(
3efd0893 1345 "EXT (%s): Let's finally re-originate the LSA 7.0.0.%u for Itf %s", __func__, exti->instance,
b37eb79c 1346 exti->ifp ? exti->ifp->name : "");
cf9b9f77
OD
1347 ospf_ext_pref_lsa_originate1(area, exti);
1348 }
1349
1350 rc = 0;
1351 return rc;
1352}
1353
1354/* Trigger the origination of Extended Link Opaque LSAs */
1355static int ospf_ext_link_lsa_originate(void *arg)
1356{
1357 struct ospf_area *area = (struct ospf_area *)arg;
1358 struct listnode *node;
1359 struct ext_itf *exti;
1360 int rc = -1;
1361
1362 if (!OspfEXT.enabled) {
1363 zlog_info(
b37eb79c 1364 "EXT (%s): Segment Routing functionality is Disabled now",
996c9314 1365 __func__);
cf9b9f77
OD
1366 rc = 0; /* This is not an error case. */
1367 return rc;
1368 }
1369
1370 /* Check if Extended Prefix Opaque LSA is already engaged */
1371 for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti)) {
1372 /* Process only Adjacency or LAN SID */
1373 if (exti->stype == PREF_SID)
1374 continue;
1375
21baf89a
OD
1376 /* Skip Inactive Extended Link */
1377 if (!CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE))
1378 continue;
1379
cf9b9f77
OD
1380 /* Process only Extended Link with valid Area ID */
1381 if ((exti->area == NULL)
1382 || (!IPV4_ADDR_SAME(&exti->area->area_id, &area->area_id)))
1383 continue;
1384
1385 /* Check if LSA not already engaged */
1386 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED)) {
1387 if (CHECK_FLAG(exti->flags,
1388 EXT_LPFLG_LSA_FORCED_REFRESH)) {
ade6974d 1389 flog_warn(
cf444bcf 1390 EC_OSPF_EXT_LSA_UNEXPECTED,
ade6974d
QY
1391 "EXT (%s): Refresh instead of Originate",
1392 __func__);
cf9b9f77
OD
1393 UNSET_FLAG(exti->flags,
1394 EXT_LPFLG_LSA_FORCED_REFRESH);
1395 ospf_ext_link_lsa_schedule(exti,
1396 REFRESH_THIS_LSA);
1397 }
1398 continue;
1399 }
1400
1401 /* Ok, let's try to originate an LSA */
b37eb79c 1402 osr_debug(
3efd0893 1403 "EXT (%s): Let's finally reoriginate the LSA 8.0.0.%u for Itf %s through the Area %pI4", __func__,
b37eb79c
OD
1404 exti->instance, exti->ifp ? exti->ifp->name : "-",
1405 &area->area_id);
cf9b9f77
OD
1406 ospf_ext_link_lsa_originate1(area, exti);
1407 }
1408
1409 rc = 0;
1410 return rc;
1411}
1412
1413/* Refresh an Extended Prefix Opaque LSA */
1414static struct ospf_lsa *ospf_ext_pref_lsa_refresh(struct ospf_lsa *lsa)
1415{
1416 struct ospf_lsa *new = NULL;
1417 struct ospf_area *area = lsa->area;
1418 struct ospf *top;
1419 struct ext_itf *exti;
1420
1421 if (!OspfEXT.enabled) {
1422 /*
1423 * This LSA must have flushed before due to Extended Prefix
1424 * Opaque LSA status change.
1425 * It seems a slip among routers in the routing domain.
1426 */
7743f2f8 1427 zlog_info(
b37eb79c 1428 "EXT (%s): Segment Routing functionality is Disabled",
996c9314 1429 __func__);
cf9b9f77
OD
1430 /* Flush it anyway. */
1431 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1432 }
1433
1434 /* Lookup this lsa corresponding Extended parameters */
7743f2f8
OD
1435 exti = lookup_ext_by_instance(lsa);
1436 if (exti == NULL) {
cf444bcf 1437 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
89f60109 1438 "EXT (%s): Invalid parameter LSA ID", __func__);
cf9b9f77
OD
1439 /* Flush it anyway. */
1440 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1441 }
1442
1443 /* Check if Interface was not disable in the interval */
1444 if ((exti != NULL) && !CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE)) {
cf444bcf 1445 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
89f60109 1446 "EXT (%s): Interface was Disabled: Flush it!",
996c9314 1447 __func__);
cf9b9f77
OD
1448 /* Flush it anyway. */
1449 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1450 }
1451
1452 /* If the lsa's age reached to MaxAge, start flushing procedure. */
1453 if (IS_LSA_MAXAGE(lsa)) {
1454 if (exti)
1455 UNSET_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED);
1456 ospf_opaque_lsa_flush_schedule(lsa);
1457 return NULL;
1458 }
1459
1460 /* Create new Opaque-LSA/Extended Prefix Opaque LSA instance. */
db28a51f 1461 new = ospf_ext_pref_lsa_new(area, exti);
cf9b9f77
OD
1462
1463 if (new == NULL) {
cf444bcf 1464 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
89f60109 1465 "EXT (%s): ospf_ext_pref_lsa_new() error", __func__);
cf9b9f77
OD
1466 return NULL;
1467 }
1468 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
1469
7743f2f8
OD
1470 /*
1471 * Install this LSA into LSDB
1472 * Given "lsa" will be freed in the next function
1473 * As area could be NULL i.e. when using OPAQUE_LSA_AS, we prefer to use
1474 * ospf_lookup() to get ospf instance
1475 */
cf9b9f77
OD
1476 if (area)
1477 top = area->ospf;
1478 else
1479 top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
1480
1481 if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
cf444bcf 1482 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
542a208f 1483 "EXT (%s): ospf_lsa_install() error", __func__);
cf9b9f77
OD
1484 ospf_lsa_unlock(&new);
1485 return NULL;
1486 }
1487
1488 /* Flood updated LSA through the Prefix Area according to the RFC7684 */
1489 ospf_flood_through_area(area, NULL /*nbr */, new);
1490
1491 /* Debug logging. */
b37eb79c
OD
1492 osr_debug("EXT (%s): LSA[Type%u:%pI4] Refresh Extended Prefix LSA",
1493 __func__, new->data->type, &new->data->id);
1494 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
cf9b9f77 1495 ospf_lsa_header_dump(new->data);
b37eb79c 1496
cf9b9f77
OD
1497
1498 return new;
1499}
1500
1501/* Refresh an Extended Link Opaque LSA */
1502static struct ospf_lsa *ospf_ext_link_lsa_refresh(struct ospf_lsa *lsa)
1503{
1504 struct ext_itf *exti;
1505 struct ospf_area *area = lsa->area;
1506 struct ospf *top = area->ospf;
1507 struct ospf_lsa *new = NULL;
1508
1509 if (!OspfEXT.enabled) {
1510 /*
1511 * This LSA must have flushed before due to OSPF-SR status
1512 * change. It seems a slip among routers in the routing domain.
1513 */
996c9314
LB
1514 zlog_info("EXT (%s): Segment Routing functionality is Disabled",
1515 __func__);
cf9b9f77
OD
1516 /* Flush it anyway. */
1517 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1518 }
1519
7743f2f8
OD
1520 /* Lookup this LSA corresponding Extended parameters */
1521 exti = lookup_ext_by_instance(lsa);
1522 if (exti == NULL) {
cf444bcf 1523 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
89f60109 1524 "EXT (%s): Invalid parameter LSA ID", __func__);
cf9b9f77
OD
1525 /* Flush it anyway. */
1526 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1527 }
1528
1529 /* Check if Interface was not disable in the interval */
1530 if ((exti != NULL) && !CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE)) {
cf444bcf 1531 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
89f60109 1532 "EXT (%s): Interface was Disabled: Flush it!",
996c9314 1533 __func__);
cf9b9f77
OD
1534 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1535 }
1536
7743f2f8 1537 /* If the lsa's age reached to MaxAge, start flushing procedure */
cf9b9f77
OD
1538 if (IS_LSA_MAXAGE(lsa)) {
1539 if (exti)
1540 UNSET_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED);
1541 ospf_opaque_lsa_flush_schedule(lsa);
1542 return NULL;
1543 }
1544
7743f2f8
OD
1545 /* Create new Opaque-LSA/Extended Link instance */
1546 new = ospf_ext_link_lsa_new(area, exti);
1547 if (new == NULL) {
cf444bcf 1548 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
89f60109 1549 "EXT (%s): Error creating new LSA", __func__);
cf9b9f77
OD
1550 return NULL;
1551 }
1552 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
1553
1554 /* Install this LSA into LSDB. */
7743f2f8 1555 /* Given "lsa" will be freed in the next function */
cf9b9f77 1556 if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
cf444bcf 1557 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
542a208f 1558 "EXT (%s): Error installing new LSA", __func__);
cf9b9f77
OD
1559 ospf_lsa_unlock(&new);
1560 return NULL;
1561 }
1562
1563 /* Flood updated LSA through the link Area according to the RFC7684 */
1564 ospf_flood_through_area(area, NULL /*nbr */, new);
1565
1566 /* Debug logging. */
b37eb79c
OD
1567 osr_debug("EXT (%s): LSA[Type%u:%pI4]: Refresh Extended Link LSA",
1568 __func__, new->data->type, &new->data->id);
1569 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
cf9b9f77 1570 ospf_lsa_header_dump(new->data);
cf9b9f77
OD
1571
1572 return new;
1573}
1574
1575/* Schedule Extended Prefix Opaque LSA origination/refreshment/flushing */
1576static void ospf_ext_pref_lsa_schedule(struct ext_itf *exti,
1577 enum lsa_opcode opcode)
1578{
1579 struct ospf_lsa lsa;
1580 struct lsa_header lsah;
1581 struct ospf *top;
93f0a26e 1582 uint32_t tmp;
cf9b9f77
OD
1583
1584 memset(&lsa, 0, sizeof(lsa));
1585 memset(&lsah, 0, sizeof(lsah));
1586
1587 /* Sanity Check */
1588 if (exti == NULL)
1589 return;
1590
1591 /* Check if the corresponding link is ready to be flooded */
1592 if (!(CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE)))
1593 return;
1594
b37eb79c
OD
1595 osr_debug("EXT (%s): Schedule %s%s%s LSA for interface %s", __func__,
1596 opcode == REORIGINATE_THIS_LSA ? "Re-Originate" : "",
1597 opcode == REFRESH_THIS_LSA ? "Refresh" : "",
1598 opcode == FLUSH_THIS_LSA ? "Flush" : "",
1599 exti->ifp ? exti->ifp->name : "-");
cf9b9f77 1600
731271b0 1601 /* Verify Area */
cf9b9f77 1602 if (exti->area == NULL) {
b37eb79c
OD
1603 osr_debug(
1604 "EXT (%s): Area is not yet set. Try to use Backbone Area",
1605 __func__);
731271b0
OD
1606
1607 top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
1608 struct in_addr backbone = {.s_addr = INADDR_ANY};
1609 exti->area = ospf_area_lookup_by_area_id(top, backbone);
1610 if (exti->area == NULL) {
1611 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
1612 "EXT (%s): Unable to set Area", __func__);
1613 return;
cf9b9f77 1614 }
cf9b9f77 1615 }
731271b0 1616 /* Set LSA header information */
cf9b9f77
OD
1617 lsa.area = exti->area;
1618 lsa.data = &lsah;
1619 lsah.type = OSPF_OPAQUE_AREA_LSA;
1620 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_PREFIX_LSA, exti->instance);
1621 lsah.id.s_addr = htonl(tmp);
1622
1623 switch (opcode) {
1624 case REORIGINATE_THIS_LSA:
1625 ospf_opaque_lsa_reoriginate_schedule(
1626 (void *)exti->area, OSPF_OPAQUE_AREA_LSA,
1627 OPAQUE_TYPE_EXTENDED_PREFIX_LSA);
1628 break;
1629 case REFRESH_THIS_LSA:
1630 ospf_opaque_lsa_refresh_schedule(&lsa);
1631 break;
1632 case FLUSH_THIS_LSA:
1633 UNSET_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED);
1634 ospf_opaque_lsa_flush_schedule(&lsa);
1635 break;
cf9b9f77 1636 }
cf9b9f77
OD
1637}
1638
1639/* Schedule Extended Link Opaque LSA origination/refreshment/flushing */
1640static void ospf_ext_link_lsa_schedule(struct ext_itf *exti,
1641 enum lsa_opcode opcode)
1642{
1643 struct ospf_lsa lsa;
1644 struct lsa_header lsah;
1645 struct ospf *top;
93f0a26e 1646 uint32_t tmp;
cf9b9f77
OD
1647
1648 memset(&lsa, 0, sizeof(lsa));
1649 memset(&lsah, 0, sizeof(lsah));
1650
1651 /* Sanity Check */
1652 if (exti == NULL)
1653 return;
1654
1655 /* Check if the corresponding link is ready to be flooded */
1656 if (!(CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE)))
1657 return;
1658
b37eb79c
OD
1659 osr_debug("EXT (%s): Schedule %s%s%s LSA for interface %s", __func__,
1660 opcode == REORIGINATE_THIS_LSA ? "Re-Originate" : "",
1661 opcode == REFRESH_THIS_LSA ? "Refresh" : "",
1662 opcode == FLUSH_THIS_LSA ? "Flush" : "",
1663 exti->ifp ? exti->ifp->name : "-");
cf9b9f77 1664
731271b0 1665 /* Verify Area */
cf9b9f77 1666 if (exti->area == NULL) {
b37eb79c
OD
1667 osr_debug(
1668 "EXT (%s): Area is not yet set. Try to use Backbone Area",
1669 __func__);
731271b0
OD
1670
1671 top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
1672 struct in_addr backbone = {.s_addr = INADDR_ANY};
1673 exti->area = ospf_area_lookup_by_area_id(top, backbone);
1674 if (exti->area == NULL) {
1675 flog_warn(EC_OSPF_EXT_LSA_UNEXPECTED,
1676 "EXT (%s): Unable to set Area", __func__);
1677 return;
cf9b9f77 1678 }
cf9b9f77 1679 }
731271b0 1680 /* Set LSA header information */
cf9b9f77
OD
1681 lsa.area = exti->area;
1682 lsa.data = &lsah;
1683 lsah.type = OSPF_OPAQUE_AREA_LSA;
1684 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_LINK_LSA, exti->instance);
1685 lsah.id.s_addr = htonl(tmp);
1686
1687 switch (opcode) {
1688 case REORIGINATE_THIS_LSA:
1689 ospf_opaque_lsa_reoriginate_schedule(
1690 (void *)exti->area, OSPF_OPAQUE_AREA_LSA,
1691 OPAQUE_TYPE_EXTENDED_LINK_LSA);
1692 break;
1693 case REFRESH_THIS_LSA:
1694 ospf_opaque_lsa_refresh_schedule(&lsa);
1695 break;
1696 case FLUSH_THIS_LSA:
cf9b9f77
OD
1697 ospf_opaque_lsa_flush_schedule(&lsa);
1698 break;
cf9b9f77 1699 }
cf9b9f77
OD
1700}
1701
1702/* Schedule Extended Link or Prefix depending of the Type of LSA */
1703static void ospf_ext_lsa_schedule(struct ext_itf *exti, enum lsa_opcode op)
1704{
1705
1706 if (exti->stype == PREF_SID)
1707 ospf_ext_pref_lsa_schedule(exti, op);
1708 else
1709 ospf_ext_link_lsa_schedule(exti, op);
1710}
1711
7743f2f8
OD
1712/*
1713 * ------------------------------------
cf9b9f77 1714 * Followings are vty show functions.
7743f2f8
OD
1715 * ------------------------------------
1716 */
1717
cf9b9f77 1718/* Cisco experimental SubTLV */
93f0a26e 1719static uint16_t show_vty_ext_link_rmt_itf_addr(struct vty *vty,
996c9314 1720 struct tlv_header *tlvh)
cf9b9f77
OD
1721{
1722 struct ext_subtlv_rmt_itf_addr *top;
7743f2f8 1723
cf9b9f77
OD
1724 top = (struct ext_subtlv_rmt_itf_addr *)tlvh;
1725
1726 vty_out(vty,
96b663a3
MS
1727 " Remote Interface Address Sub-TLV: Length %u\n Address: %pI4\n",
1728 ntohs(top->header.length), &top->value);
cf9b9f77
OD
1729
1730 return TLV_SIZE(tlvh);
1731}
1732
1733/* Adjacency SID SubTLV */
93f0a26e 1734static uint16_t show_vty_ext_link_adj_sid(struct vty *vty,
996c9314 1735 struct tlv_header *tlvh)
cf9b9f77
OD
1736{
1737 struct ext_subtlv_adj_sid *top = (struct ext_subtlv_adj_sid *)tlvh;
1738
1739 vty_out(vty,
3efd0893 1740 " Adj-SID Sub-TLV: Length %u\n\tFlags: 0x%x\n\tMT-ID:0x%x\n\tWeight: 0x%x\n\t%s: %u\n",
cf9b9f77
OD
1741 ntohs(top->header.length), top->flags, top->mtid, top->weight,
1742 CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG) ? "Label"
1743 : "Index",
1744 CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG)
1745 ? GET_LABEL(ntohl(top->value))
1746 : ntohl(top->value));
1747
1748 return TLV_SIZE(tlvh);
1749}
1750
1751/* LAN Adjacency SubTLV */
93f0a26e 1752static uint16_t show_vty_ext_link_lan_adj_sid(struct vty *vty,
996c9314 1753 struct tlv_header *tlvh)
cf9b9f77
OD
1754{
1755 struct ext_subtlv_lan_adj_sid *top =
1756 (struct ext_subtlv_lan_adj_sid *)tlvh;
1757
1758 vty_out(vty,
96b663a3 1759 " LAN-Adj-SID Sub-TLV: Length %u\n\tFlags: 0x%x\n\tMT-ID:0x%x\n\tWeight: 0x%x\n\tNeighbor ID: %pI4\n\t%s: %u\n",
cf9b9f77 1760 ntohs(top->header.length), top->flags, top->mtid, top->weight,
96b663a3 1761 &top->neighbor_id,
cf9b9f77
OD
1762 CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG) ? "Label"
1763 : "Index",
1764 CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG)
1765 ? GET_LABEL(ntohl(top->value))
1766 : ntohl(top->value));
1767
1768 return TLV_SIZE(tlvh);
1769}
1770
93f0a26e 1771static uint16_t show_vty_unknown_tlv(struct vty *vty, struct tlv_header *tlvh)
cf9b9f77
OD
1772{
1773 vty_out(vty, " Unknown TLV: [type(0x%x), length(0x%x)]\n",
1774 ntohs(tlvh->type), ntohs(tlvh->length));
1775
1776 return TLV_SIZE(tlvh);
1777}
1778
1779/* Extended Link Sub TLVs */
93f0a26e 1780static uint16_t show_vty_link_info(struct vty *vty, struct tlv_header *ext)
cf9b9f77
OD
1781{
1782 struct ext_tlv_link *top = (struct ext_tlv_link *)ext;
1783 struct tlv_header *tlvh;
93f0a26e
OD
1784 uint16_t length = ntohs(top->header.length) - 3 * sizeof(uint32_t);
1785 uint16_t sum = 0;
cf9b9f77
OD
1786
1787 vty_out(vty,
7743f2f8 1788 " Extended Link TLV: Length %u\n Link Type: 0x%x\n"
96b663a3 1789 " Link ID: %pI4\n",
cf9b9f77 1790 ntohs(top->header.length), top->link_type,
96b663a3
MS
1791 &top->link_id);
1792 vty_out(vty, " Link data: %pI4\n", &top->link_data);
cf9b9f77
OD
1793
1794 tlvh = (struct tlv_header *)((char *)(ext) + TLV_HDR_SIZE
1795 + EXT_TLV_LINK_SIZE);
1796 for (; sum < length; tlvh = TLV_HDR_NEXT(tlvh)) {
1797 switch (ntohs(tlvh->type)) {
1798 case EXT_SUBTLV_ADJ_SID:
1799 sum += show_vty_ext_link_adj_sid(vty, tlvh);
1800 break;
1801 case EXT_SUBTLV_LAN_ADJ_SID:
1802 sum += show_vty_ext_link_lan_adj_sid(vty, tlvh);
1803 break;
1804 case EXT_SUBTLV_RMT_ITF_ADDR:
1805 sum += show_vty_ext_link_rmt_itf_addr(vty, tlvh);
1806 break;
1807 default:
1808 sum += show_vty_unknown_tlv(vty, tlvh);
1809 break;
1810 }
1811 }
1812
1813 return sum + sizeof(struct ext_tlv_link);
1814}
1815
1816/* Extended Link TLVs */
1817static void ospf_ext_link_show_info(struct vty *vty, struct ospf_lsa *lsa)
1818{
c4efd0f4 1819 struct lsa_header *lsah = lsa->data;
cf9b9f77 1820 struct tlv_header *tlvh;
93f0a26e 1821 uint16_t length = 0, sum = 0;
cf9b9f77
OD
1822
1823 /* Initialize TLV browsing */
1824 length = ntohs(lsah->length) - OSPF_LSA_HEADER_SIZE;
1825
1826 for (tlvh = TLV_HDR_TOP(lsah); sum < length;
1827 tlvh = TLV_HDR_NEXT(tlvh)) {
1828 switch (ntohs(tlvh->type)) {
1829 case EXT_TLV_LINK:
1830 sum += show_vty_link_info(vty, tlvh);
1831 break;
1832 default:
1833 sum += show_vty_unknown_tlv(vty, tlvh);
1834 break;
1835 }
1836 }
cf9b9f77
OD
1837}
1838
1839/* Prefix SID SubTLV */
93f0a26e 1840static uint16_t show_vty_ext_pref_pref_sid(struct vty *vty,
996c9314 1841 struct tlv_header *tlvh)
cf9b9f77
OD
1842{
1843 struct ext_subtlv_prefix_sid *top =
1844 (struct ext_subtlv_prefix_sid *)tlvh;
1845
1846 vty_out(vty,
3efd0893 1847 " Prefix SID Sub-TLV: Length %u\n\tAlgorithm: %u\n\tFlags: 0x%x\n\tMT-ID:0x%x\n\t%s: %u\n",
cf9b9f77
OD
1848 ntohs(top->header.length), top->algorithm, top->flags,
1849 top->mtid,
1850 CHECK_FLAG(top->flags, EXT_SUBTLV_PREFIX_SID_VFLG) ? "Label"
1851 : "Index",
1852 CHECK_FLAG(top->flags, EXT_SUBTLV_PREFIX_SID_VFLG)
1853 ? GET_LABEL(ntohl(top->value))
1854 : ntohl(top->value));
1855
1856 return TLV_SIZE(tlvh);
1857}
1858
1859/* Extended Prefix SubTLVs */
93f0a26e 1860static uint16_t show_vty_pref_info(struct vty *vty, struct tlv_header *ext)
cf9b9f77
OD
1861{
1862 struct ext_tlv_prefix *top = (struct ext_tlv_prefix *)ext;
1863 struct tlv_header *tlvh;
93f0a26e
OD
1864 uint16_t length = ntohs(top->header.length) - 2 * sizeof(uint32_t);
1865 uint16_t sum = 0;
cf9b9f77
OD
1866
1867 vty_out(vty,
7743f2f8 1868 " Extended Prefix TLV: Length %u\n\tRoute Type: %u\n"
96b663a3 1869 "\tAddress Family: 0x%x\n\tFlags: 0x%x\n\tAddress: %pI4/%u\n",
cf9b9f77 1870 ntohs(top->header.length), top->route_type, top->af, top->flags,
96b663a3 1871 &top->address, top->pref_length);
cf9b9f77
OD
1872
1873 tlvh = (struct tlv_header *)((char *)(ext) + TLV_HDR_SIZE
1874 + EXT_TLV_PREFIX_SIZE);
1875 for (; sum < length; tlvh = TLV_HDR_NEXT(tlvh)) {
1876 switch (ntohs(tlvh->type)) {
1877 case EXT_SUBTLV_PREFIX_SID:
1878 sum += show_vty_ext_pref_pref_sid(vty, tlvh);
1879 break;
1880 default:
1881 sum += show_vty_unknown_tlv(vty, tlvh);
1882 break;
1883 }
1884 }
1885
1886 return sum + sizeof(struct ext_tlv_prefix);
1887}
1888
1889/* Extended Prefix TLVs */
1890static void ospf_ext_pref_show_info(struct vty *vty, struct ospf_lsa *lsa)
1891{
c4efd0f4 1892 struct lsa_header *lsah = lsa->data;
cf9b9f77 1893 struct tlv_header *tlvh;
93f0a26e 1894 uint16_t length = 0, sum = 0;
cf9b9f77
OD
1895
1896 /* Initialize TLV browsing */
1897 length = ntohs(lsah->length) - OSPF_LSA_HEADER_SIZE;
1898
1899 for (tlvh = TLV_HDR_TOP(lsah); sum < length;
1900 tlvh = TLV_HDR_NEXT(tlvh)) {
1901 switch (ntohs(tlvh->type)) {
1902 case EXT_TLV_PREFIX:
1903 sum += show_vty_pref_info(vty, tlvh);
1904 break;
1905 default:
1906 sum += show_vty_unknown_tlv(vty, tlvh);
1907 break;
1908 }
1909 }
cf9b9f77 1910}