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