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