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