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