]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_ext.c
f56bca6b7bc170d1ad59bfe0d03f0d565f109af7
[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 uint32_t get_ext_pref_instance_value(void)
209 {
210 static uint32_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 uint32_t get_ext_link_instance_value(void)
222 {
223 static uint32_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, uint8_t route_type,
269 uint8_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, uint8_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, uint8_t algorithm,
298 uint32_t value, int value_type)
299 {
300
301 uint8_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, uint32_t value,
340 int value_type)
341 {
342 int index;
343 uint8_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, uint32_t value,
381 int value_type, struct in_addr neighbor_id)
382 {
383
384 int index;
385 uint8_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, uint32_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 uint32_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 uint32_t tmp;
919 uint16_t length;
920
921 /* Sanity Check */
922 if (exti == NULL)
923 return NULL;
924
925 /* Create a stream for LSA. */
926 if ((s = stream_new(OSPF_MAX_LSA_SIZE)) == NULL) {
927 zlog_warn("EXT: stream_new() error");
928 return NULL;
929 }
930
931 /* Prepare LSA Header */
932 lsah = (struct lsa_header *)STREAM_DATA(s);
933
934 lsa_type = OspfEXT.scope;
935
936 /* LSA ID is a variable number identifying different instances of
937 * Extended Prefix Opaque LSA from the same router see RFC 7684 */
938 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_PREFIX_LSA, exti->instance);
939 lsa_id.s_addr = htonl(tmp);
940
941 options = OSPF_OPTION_O; /* Don't forget this :-) */
942
943 /* Fix Options and Router ID depending of the flooding scope */
944 if ((OspfEXT.scope == OSPF_OPAQUE_AS_LSA) || (area == NULL)) {
945 options = OSPF_OPTION_E;
946 struct ospf *top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
947 router_id = top->router_id;
948 } else {
949 options |= LSA_OPTIONS_GET(area); /* Get area default option */
950 options |= LSA_OPTIONS_NSSA_GET(area);
951 router_id = area->ospf->router_id;
952 }
953
954 /* Set opaque-LSA header fields. */
955 lsa_header_set(s, options, lsa_type, lsa_id, router_id);
956
957 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
958 zlog_debug(
959 "EXT: LSA[Type%d:%s]: Create an Opaque-LSA/Extended "
960 "Prefix Opaque LSA instance",
961 lsa_type, inet_ntoa(lsa_id));
962
963
964 /* Set opaque-LSA body fields. */
965 ospf_ext_pref_lsa_body_set(s, exti);
966
967 /* Set length. */
968 length = stream_get_endp(s);
969 lsah->length = htons(length);
970
971 /* Now, create an OSPF LSA instance. */
972 if ((new = ospf_lsa_new()) == NULL) {
973 zlog_warn("EXT: ospf_lsa_new() error");
974 stream_free(s);
975 return NULL;
976 }
977 if ((new->data = ospf_lsa_data_new(length)) == NULL) {
978 zlog_warn("EXT: ospf_lsa_data_new() error");
979 ospf_lsa_unlock(&new);
980 new = NULL;
981 stream_free(s);
982 return NULL;
983 }
984
985 new->area = area;
986 SET_FLAG(new->flags, OSPF_LSA_SELF);
987 memcpy(new->data, lsah, length);
988 stream_free(s);
989
990 return new;
991 }
992
993 /* Create new Extended Link opaque-LSA for every extended link TLV */
994 static struct ospf_lsa *ospf_ext_link_lsa_new(struct ospf_area *area,
995 struct ext_itf *exti)
996 {
997 struct stream *s;
998 struct lsa_header *lsah;
999 struct ospf_lsa *new = NULL;
1000 u_char options, lsa_type;
1001 struct in_addr lsa_id;
1002 uint32_t tmp;
1003 uint16_t length;
1004
1005 /* Sanity Check */
1006 if (exti == NULL)
1007 return NULL;
1008
1009 /* Create a stream for LSA. */
1010 if ((s = stream_new(OSPF_MAX_LSA_SIZE)) == NULL) {
1011 zlog_warn("EXT: stream_new() error");
1012 return NULL;
1013 }
1014 lsah = (struct lsa_header *)STREAM_DATA(s);
1015
1016 options = OSPF_OPTION_O; /* Don't forget this :-) */
1017 options |= LSA_OPTIONS_GET(area); /* Get area default option */
1018 options |= LSA_OPTIONS_NSSA_GET(area);
1019 /* Extended Link Opaque LSA are only flooded within an area */
1020 lsa_type = OSPF_OPAQUE_AREA_LSA;
1021
1022 /* LSA ID is a variable number identifying different instances of
1023 * Extended Link Opaque LSA from the same router see RFC 7684 */
1024 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_LINK_LSA, exti->instance);
1025 lsa_id.s_addr = htonl(tmp);
1026
1027 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1028 zlog_debug(
1029 "EXT: LSA[Type%d:%s]: Create an Opaque-LSA/Extended "
1030 "Link Opaque LSA instance",
1031 lsa_type, inet_ntoa(lsa_id));
1032
1033 /* Set opaque-LSA header fields. */
1034 lsa_header_set(s, options, lsa_type, lsa_id, area->ospf->router_id);
1035
1036 /* Set opaque-LSA body fields. */
1037 ospf_ext_link_lsa_body_set(s, exti);
1038
1039 /* Set length. */
1040 length = stream_get_endp(s);
1041 lsah->length = htons(length);
1042
1043 /* Now, create an OSPF LSA instance. */
1044 if ((new = ospf_lsa_new()) == NULL) {
1045 zlog_warn("EXT: ospf_lsa_new() error");
1046 stream_free(s);
1047 return NULL;
1048 }
1049 if ((new->data = ospf_lsa_data_new(length)) == NULL) {
1050 zlog_warn("EXT: ospf_lsa_data_new() error");
1051 ospf_lsa_unlock(&new);
1052 new = NULL;
1053 stream_free(s);
1054 return NULL;
1055 }
1056
1057 new->area = area;
1058 SET_FLAG(new->flags, OSPF_LSA_SELF);
1059 memcpy(new->data, lsah, length);
1060 stream_free(s);
1061
1062 return new;
1063 }
1064
1065 /* Process the origination of an Extended Prefix Opaque LSA
1066 * for every extended prefix TLV */
1067 static int ospf_ext_pref_lsa_originate1(struct ospf_area *area,
1068 struct ext_itf *exti)
1069 {
1070 struct ospf_lsa *new;
1071 int rc = -1;
1072
1073
1074 /* Create new Opaque-LSA/Extended Prefix Opaque LSA instance. */
1075 if ((new = ospf_ext_pref_lsa_new(area, exti)) == NULL) {
1076 zlog_warn("EXT: ospf_ext_pref_lsa_new() error");
1077 return rc;
1078 }
1079
1080 /* Install this LSA into LSDB. */
1081 if (ospf_lsa_install(area->ospf, NULL /*oi */, new) == NULL) {
1082 zlog_warn("EXT: ospf_lsa_install() error");
1083 ospf_lsa_unlock(&new);
1084 return rc;
1085 }
1086
1087 /* Now this Extended Prefix Opaque LSA info parameter entry has
1088 * associated LSA. */
1089 SET_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED);
1090
1091 /* Update new LSA origination count. */
1092 area->ospf->lsa_originate_count++;
1093
1094 /* Flood new LSA through area. */
1095 ospf_flood_through_area(area, NULL /*nbr */, new);
1096
1097 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1098 char area_id[INET_ADDRSTRLEN];
1099 strncpy(area_id, inet_ntoa(area->area_id), INET_ADDRSTRLEN);
1100 zlog_debug(
1101 "EXT: LSA[Type%d:%s]: Originate Opaque-LSA/Extended "
1102 "Prefix Opaque LSA: Area(%s), Link(%s)",
1103 new->data->type, inet_ntoa(new->data->id), area_id,
1104 exti->ifp->name);
1105 ospf_lsa_header_dump(new->data);
1106 }
1107
1108 rc = 0;
1109
1110 return rc;
1111 }
1112
1113 /* Process the origination of an Extended Link Opaque LSA
1114 * for every extended link TLV */
1115 static int ospf_ext_link_lsa_originate1(struct ospf_area *area,
1116 struct ext_itf *exti)
1117 {
1118 struct ospf_lsa *new;
1119 int rc = -1;
1120
1121 /* Create new Opaque-LSA/Extended Link Opaque LSA instance. */
1122 if ((new = ospf_ext_link_lsa_new(area, exti)) == NULL) {
1123 zlog_warn("EXT: ospf_ext_link_lsa_new() error");
1124 return rc;
1125 }
1126
1127 /* Install this LSA into LSDB. */
1128 if (ospf_lsa_install(area->ospf, NULL /*oi */, new) == NULL) {
1129 zlog_warn("EXT: ospf_lsa_install() error");
1130 ospf_lsa_unlock(&new);
1131 return rc;
1132 }
1133
1134 /* Now this link-parameter entry has associated LSA. */
1135 SET_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED);
1136
1137 /* Update new LSA origination count. */
1138 area->ospf->lsa_originate_count++;
1139
1140 /* Flood new LSA through area. */
1141 ospf_flood_through_area(area, NULL /*nbr */, new);
1142
1143 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1144 char area_id[INET_ADDRSTRLEN];
1145 strncpy(area_id, inet_ntoa(area->area_id), INET_ADDRSTRLEN);
1146 zlog_debug(
1147 "EXT: LSA[Type%d:%s]: Originate Opaque-LSA/Extended "
1148 "Link Opaque LSA: Area(%s), Link(%s)",
1149 new->data->type, inet_ntoa(new->data->id), area_id,
1150 exti->ifp->name);
1151 ospf_lsa_header_dump(new->data);
1152 }
1153
1154 rc = 0;
1155
1156 return rc;
1157 }
1158
1159 /* Trigger the origination of Extended Prefix Opaque LSAs */
1160 static int ospf_ext_pref_lsa_originate(void *arg)
1161 {
1162 struct ospf_area *area = (struct ospf_area *)arg;
1163 struct listnode *node;
1164 struct ext_itf *exti;
1165 int rc = -1;
1166
1167 if (!OspfEXT.enabled) {
1168 zlog_info(
1169 "EXT: Segment Routing "
1170 "functionality is Disabled now.");
1171 rc = 0; /* This is not an error case. */
1172 return rc;
1173 }
1174 if (IS_DEBUG_OSPF_SR)
1175 zlog_debug(
1176 "EXT (ospf_ext_pref_lsa_originate) "
1177 "Start Originate Prefix LSA for area %s",
1178 inet_ntoa(area->area_id));
1179
1180 /* Check if Extended Prefix Opaque LSA is already engaged */
1181 for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti)) {
1182
1183 /* Process only Prefix SID */
1184 if (exti->stype != PREF_SID)
1185 continue;
1186
1187 /* Process only Extended Prefix with valid Area ID */
1188 if ((exti->area == NULL)
1189 || (!IPV4_ADDR_SAME(&exti->area->area_id, &area->area_id)))
1190 continue;
1191
1192 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED)) {
1193 if (CHECK_FLAG(exti->flags,
1194 EXT_LPFLG_LSA_FORCED_REFRESH)) {
1195 zlog_warn("EXT: Refresh instead of Originate");
1196 UNSET_FLAG(exti->flags,
1197 EXT_LPFLG_LSA_FORCED_REFRESH);
1198 ospf_ext_pref_lsa_schedule(exti,
1199 REFRESH_THIS_LSA);
1200 }
1201 continue;
1202 }
1203
1204 /* Ok, let's try to originate an LSA */
1205 if (IS_DEBUG_OSPF_SR)
1206 zlog_debug(
1207 "EXT: Let's finally reoriginate the "
1208 "LSA 7.0.0.%d for Itf %s",
1209 exti->instance,
1210 exti->ifp ? exti->ifp->name : "");
1211 ospf_ext_pref_lsa_originate1(area, exti);
1212 }
1213
1214 rc = 0;
1215 return rc;
1216 }
1217
1218 /* Trigger the origination of Extended Link Opaque LSAs */
1219 static int ospf_ext_link_lsa_originate(void *arg)
1220 {
1221 struct ospf_area *area = (struct ospf_area *)arg;
1222 struct listnode *node;
1223 struct ext_itf *exti;
1224 int rc = -1;
1225
1226 if (!OspfEXT.enabled) {
1227 zlog_info(
1228 "EXT: Segment Routing "
1229 "functionality is Disabled now.");
1230 rc = 0; /* This is not an error case. */
1231 return rc;
1232 }
1233
1234 /* Check if Extended Prefix Opaque LSA is already engaged */
1235 for (ALL_LIST_ELEMENTS_RO(OspfEXT.iflist, node, exti)) {
1236 /* Process only Adjacency or LAN SID */
1237 if (exti->stype == PREF_SID)
1238 continue;
1239
1240 /* Process only Extended Link with valid Area ID */
1241 if ((exti->area == NULL)
1242 || (!IPV4_ADDR_SAME(&exti->area->area_id, &area->area_id)))
1243 continue;
1244
1245 /* Check if LSA not already engaged */
1246 if (CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED)) {
1247 if (CHECK_FLAG(exti->flags,
1248 EXT_LPFLG_LSA_FORCED_REFRESH)) {
1249 zlog_warn("EXT: Refresh instead of Originate");
1250 UNSET_FLAG(exti->flags,
1251 EXT_LPFLG_LSA_FORCED_REFRESH);
1252 ospf_ext_link_lsa_schedule(exti,
1253 REFRESH_THIS_LSA);
1254 }
1255 continue;
1256 }
1257
1258 /* Ok, let's try to originate an LSA */
1259 if (IS_DEBUG_OSPF_SR)
1260 zlog_debug(
1261 "EXT Let's finally reoriginate the "
1262 "LSA 8.0.0.%d for Itf %s through the Area %s",
1263 exti->instance,
1264 exti->ifp ? exti->ifp->name : "-",
1265 inet_ntoa(area->area_id));
1266 ospf_ext_link_lsa_originate1(area, exti);
1267 }
1268
1269 rc = 0;
1270 return rc;
1271 }
1272
1273 /* Refresh an Extended Prefix Opaque LSA */
1274 static struct ospf_lsa *ospf_ext_pref_lsa_refresh(struct ospf_lsa *lsa)
1275 {
1276 struct ospf_lsa *new = NULL;
1277 struct ospf_area *area = lsa->area;
1278 struct ospf *top;
1279 struct ext_itf *exti;
1280
1281 if (!OspfEXT.enabled) {
1282 /*
1283 * This LSA must have flushed before due to Extended Prefix
1284 * Opaque LSA status change.
1285 * It seems a slip among routers in the routing domain.
1286 */
1287 zlog_info("EXT: Segment Routing functionality is Disabled");
1288 /* Flush it anyway. */
1289 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1290 }
1291
1292 /* Lookup this lsa corresponding Extended parameters */
1293 if ((exti = lookup_ext_by_instance(lsa)) == NULL) {
1294 zlog_warn("EXT: Invalid parameter LSA ID");
1295 /* Flush it anyway. */
1296 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1297 }
1298
1299 /* Check if Interface was not disable in the interval */
1300 if ((exti != NULL) && !CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE)) {
1301 zlog_warn("EXT: Interface was Disabled: Flush it!");
1302 /* Flush it anyway. */
1303 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1304 }
1305
1306 /* If the lsa's age reached to MaxAge, start flushing procedure. */
1307 if (IS_LSA_MAXAGE(lsa)) {
1308 if (exti)
1309 UNSET_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED);
1310 ospf_opaque_lsa_flush_schedule(lsa);
1311 return NULL;
1312 }
1313
1314 /* Create new Opaque-LSA/Extended Prefix Opaque LSA instance. */
1315 if (exti)
1316 new = ospf_ext_pref_lsa_new(area, exti);
1317
1318 if (new == NULL) {
1319 zlog_warn("EXT: ospf_ext_pref_lsa_new() error");
1320 return NULL;
1321 }
1322 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
1323
1324 /* Install this LSA into LSDB. */
1325 /* Given "lsa" will be freed in the next function. */
1326 /* As area could be NULL i.e. when using OPAQUE_LSA_AS, we prefer to use
1327 * ospf_lookup() to get ospf instance */
1328 if (area)
1329 top = area->ospf;
1330 else
1331 top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
1332
1333 if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
1334 zlog_warn("EXT: ospf_lsa_install() error");
1335 ospf_lsa_unlock(&new);
1336 return NULL;
1337 }
1338
1339 /* Flood updated LSA through the Prefix Area according to the RFC7684 */
1340 ospf_flood_through_area(area, NULL /*nbr */, new);
1341
1342 /* Debug logging. */
1343 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1344 zlog_debug("EXT: LSA[Type%d:%s]: Refresh Extended Prefix LSA",
1345 new->data->type, inet_ntoa(new->data->id));
1346 ospf_lsa_header_dump(new->data);
1347 }
1348
1349 return new;
1350 }
1351
1352 /* Refresh an Extended Link Opaque LSA */
1353 static struct ospf_lsa *ospf_ext_link_lsa_refresh(struct ospf_lsa *lsa)
1354 {
1355 struct ext_itf *exti;
1356 struct ospf_area *area = lsa->area;
1357 struct ospf *top = area->ospf;
1358 struct ospf_lsa *new = NULL;
1359
1360 if (!OspfEXT.enabled) {
1361 /*
1362 * This LSA must have flushed before due to OSPF-SR status
1363 * change. It seems a slip among routers in the routing domain.
1364 */
1365 zlog_info(
1366 "EXT (ospf_ext_link_lsa_refresh): Segment Routing "
1367 "functionality is Disabled");
1368 /* Flush it anyway. */
1369 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1370 }
1371
1372 /* Lookup this lsa corresponding Extended parameters */
1373 if ((exti = lookup_ext_by_instance(lsa)) == NULL) {
1374 zlog_warn(
1375 "EXT (ospf_ext_link_lsa_refresh): Invalid parameter "
1376 "LSA ID");
1377 /* Flush it anyway. */
1378 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1379 }
1380
1381 /* Check if Interface was not disable in the interval */
1382 if ((exti != NULL) && !CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE)) {
1383 zlog_warn(
1384 "EXT (ospf_ext_link_lsa_refresh): Interface was "
1385 "Disabled: Flush it!");
1386 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1387 }
1388
1389 /* If the lsa's age reached to MaxAge, start flushing procedure. */
1390 if (IS_LSA_MAXAGE(lsa)) {
1391 if (exti)
1392 UNSET_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED);
1393 ospf_opaque_lsa_flush_schedule(lsa);
1394 return NULL;
1395 }
1396
1397 /* Create new Opaque-LSA/MPLS-TE instance. */
1398 if ((new = ospf_ext_link_lsa_new(area, exti)) == NULL) {
1399 zlog_warn(
1400 "EXT (ospf_ext_link_lsa_refresh): Error creating "
1401 "new LSA");
1402 return NULL;
1403 }
1404 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
1405
1406 /* Install this LSA into LSDB. */
1407 /* Given "lsa" will be freed in the next function. */
1408 if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
1409 zlog_warn(
1410 "EXT (ospf_ext_link_lsa_refresh): Error installing "
1411 "new LSA");
1412 ospf_lsa_unlock(&new);
1413 return NULL;
1414 }
1415
1416 /* Flood updated LSA through the link Area according to the RFC7684 */
1417 ospf_flood_through_area(area, NULL /*nbr */, new);
1418
1419 /* Debug logging. */
1420 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1421 zlog_debug(
1422 "EXT (ospf_ext_link_lsa_refresh): LSA[Type%d:%s]: "
1423 "Refresh Extended Link LSA",
1424 new->data->type, inet_ntoa(new->data->id));
1425 ospf_lsa_header_dump(new->data);
1426 }
1427
1428 return new;
1429 }
1430
1431 /* Schedule Extended Prefix Opaque LSA origination/refreshment/flushing */
1432 static void ospf_ext_pref_lsa_schedule(struct ext_itf *exti,
1433 enum lsa_opcode opcode)
1434 {
1435 struct ospf_lsa lsa;
1436 struct lsa_header lsah;
1437 struct ospf *top;
1438 uint32_t tmp;
1439
1440 memset(&lsa, 0, sizeof(lsa));
1441 memset(&lsah, 0, sizeof(lsah));
1442
1443 /* Sanity Check */
1444 if (exti == NULL)
1445 return;
1446
1447 /* Check if the corresponding link is ready to be flooded */
1448 if (!(CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE)))
1449 return;
1450
1451 zlog_debug(
1452 "EXT (ospf_ext_pref_lsa_schedule): Schedule %s%s%s LSA "
1453 "for interface %s",
1454 opcode == REORIGINATE_THIS_LSA ? "Re-Originate" : "",
1455 opcode == REFRESH_THIS_LSA ? "Refresh" : "",
1456 opcode == FLUSH_THIS_LSA ? "Flush" : "",
1457 exti->ifp ? exti->ifp->name : "-");
1458
1459 /* Set LSA header information */
1460 if (exti->area == NULL) {
1461 zlog_warn(
1462 "EXT (ospf_ext_pref_lsa_schedule): Flooding is "
1463 "Area scope but area is not yet set");
1464 if (OspfEXT.area == NULL) {
1465 top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
1466 OspfEXT.area = ospf_area_lookup_by_area_id(
1467 top, OspfEXT.area_id);
1468 }
1469 exti->area = OspfEXT.area;
1470 }
1471 lsa.area = exti->area;
1472 lsa.data = &lsah;
1473 lsah.type = OSPF_OPAQUE_AREA_LSA;
1474 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_PREFIX_LSA, exti->instance);
1475 lsah.id.s_addr = htonl(tmp);
1476
1477 switch (opcode) {
1478 case REORIGINATE_THIS_LSA:
1479 ospf_opaque_lsa_reoriginate_schedule(
1480 (void *)exti->area, OSPF_OPAQUE_AREA_LSA,
1481 OPAQUE_TYPE_EXTENDED_PREFIX_LSA);
1482 break;
1483 case REFRESH_THIS_LSA:
1484 ospf_opaque_lsa_refresh_schedule(&lsa);
1485 break;
1486 case FLUSH_THIS_LSA:
1487 UNSET_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED);
1488 ospf_opaque_lsa_flush_schedule(&lsa);
1489 break;
1490 default:
1491 zlog_warn("EXT (ospf_ext_pref_lsa_schedule): Unknown opcode");
1492 break;
1493 }
1494
1495 return;
1496 }
1497
1498 /* Schedule Extended Link Opaque LSA origination/refreshment/flushing */
1499 static void ospf_ext_link_lsa_schedule(struct ext_itf *exti,
1500 enum lsa_opcode opcode)
1501 {
1502 struct ospf_lsa lsa;
1503 struct lsa_header lsah;
1504 struct ospf *top;
1505 uint32_t tmp;
1506
1507 memset(&lsa, 0, sizeof(lsa));
1508 memset(&lsah, 0, sizeof(lsah));
1509
1510 /* Sanity Check */
1511 if (exti == NULL)
1512 return;
1513
1514 /* Check if the corresponding link is ready to be flooded */
1515 if (!(CHECK_FLAG(exti->flags, EXT_LPFLG_LSA_ACTIVE)))
1516 return;
1517
1518 zlog_debug(
1519 "EXT (ospf_ext_link_lsa_schedule): Schedule %s%s%s LSA "
1520 "for interface %s",
1521 opcode == REORIGINATE_THIS_LSA ? "Re-Originate" : "",
1522 opcode == REFRESH_THIS_LSA ? "Refresh" : "",
1523 opcode == FLUSH_THIS_LSA ? "Flush" : "",
1524 exti->ifp ? exti->ifp->name : "-");
1525
1526 /* Set LSA header information */
1527 if (exti->area == NULL) {
1528 zlog_warn(
1529 "EXT (ospf_ext_link_lsa_schedule): Flooding is "
1530 "Area scope but area is not yet set");
1531 if (OspfEXT.area == NULL) {
1532 top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
1533 OspfEXT.area = ospf_area_lookup_by_area_id(
1534 top, OspfEXT.area_id);
1535 }
1536 exti->area = OspfEXT.area;
1537 }
1538 lsa.area = exti->area;
1539 lsa.data = &lsah;
1540 lsah.type = OSPF_OPAQUE_AREA_LSA;
1541 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_LINK_LSA, exti->instance);
1542 lsah.id.s_addr = htonl(tmp);
1543
1544 switch (opcode) {
1545 case REORIGINATE_THIS_LSA:
1546 ospf_opaque_lsa_reoriginate_schedule(
1547 (void *)exti->area, OSPF_OPAQUE_AREA_LSA,
1548 OPAQUE_TYPE_EXTENDED_LINK_LSA);
1549 break;
1550 case REFRESH_THIS_LSA:
1551 ospf_opaque_lsa_refresh_schedule(&lsa);
1552 break;
1553 case FLUSH_THIS_LSA:
1554 UNSET_FLAG(exti->flags, EXT_LPFLG_LSA_ENGAGED);
1555 ospf_opaque_lsa_flush_schedule(&lsa);
1556 break;
1557 default:
1558 zlog_warn("EXT (ospf_ext_link_lsa_schedule): Unknown opcode");
1559 break;
1560 }
1561
1562 return;
1563 }
1564
1565 /* Schedule Extended Link or Prefix depending of the Type of LSA */
1566 static void ospf_ext_lsa_schedule(struct ext_itf *exti, enum lsa_opcode op)
1567 {
1568
1569 if (exti->stype == PREF_SID)
1570 ospf_ext_pref_lsa_schedule(exti, op);
1571 else
1572 ospf_ext_link_lsa_schedule(exti, op);
1573 }
1574
1575 /*------------------------------------------------------------------------*
1576 * Followings are vty show functions.
1577 *------------------------------------------------------------------------*/
1578 /* Cisco experimental SubTLV */
1579 static uint16_t show_vty_ext_link_rmt_itf_addr(struct vty *vty,
1580 struct tlv_header *tlvh)
1581 {
1582 struct ext_subtlv_rmt_itf_addr *top;
1583 top = (struct ext_subtlv_rmt_itf_addr *)tlvh;
1584
1585 vty_out(vty,
1586 " Remote Interface Address Sub-TLV: Length %d\n "
1587 "Address: %s\n",
1588 ntohs(top->header.length), inet_ntoa(top->value));
1589
1590 return TLV_SIZE(tlvh);
1591 }
1592
1593 /* Adjacency SID SubTLV */
1594 static uint16_t show_vty_ext_link_adj_sid(struct vty *vty,
1595 struct tlv_header *tlvh)
1596 {
1597 struct ext_subtlv_adj_sid *top = (struct ext_subtlv_adj_sid *)tlvh;
1598
1599 vty_out(vty,
1600 " Adj-SID Sub-TLV: Length %d\n\tFlags: "
1601 "0x%x\n\tMT-ID:0x%x\n\tWeight: 0x%x\n\t%s: %d\n",
1602 ntohs(top->header.length), top->flags, top->mtid, top->weight,
1603 CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG) ? "Label"
1604 : "Index",
1605 CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG)
1606 ? GET_LABEL(ntohl(top->value))
1607 : ntohl(top->value));
1608
1609 return TLV_SIZE(tlvh);
1610 }
1611
1612 /* LAN Adjacency SubTLV */
1613 static uint16_t show_vty_ext_link_lan_adj_sid(struct vty *vty,
1614 struct tlv_header *tlvh)
1615 {
1616 struct ext_subtlv_lan_adj_sid *top =
1617 (struct ext_subtlv_lan_adj_sid *)tlvh;
1618
1619 vty_out(vty,
1620 " LAN-Adj-SID Sub-TLV: Length %d\n\tFlags: "
1621 "0x%x\n\tMT-ID:0x%x\n\tWeight: 0x%x\n\tNeighbor ID: "
1622 "%s\n\tLabel: %d\n",
1623 ntohs(top->header.length), top->flags, top->mtid, top->weight,
1624 CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG) ? "Label"
1625 : "Index",
1626 CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG)
1627 ? GET_LABEL(ntohl(top->value))
1628 : ntohl(top->value));
1629
1630 return TLV_SIZE(tlvh);
1631 }
1632
1633 static uint16_t show_vty_unknown_tlv(struct vty *vty, struct tlv_header *tlvh)
1634 {
1635 vty_out(vty, " Unknown TLV: [type(0x%x), length(0x%x)]\n",
1636 ntohs(tlvh->type), ntohs(tlvh->length));
1637
1638 return TLV_SIZE(tlvh);
1639 }
1640
1641 /* Extended Link Sub TLVs */
1642 static uint16_t show_vty_link_info(struct vty *vty, struct tlv_header *ext)
1643 {
1644 struct ext_tlv_link *top = (struct ext_tlv_link *)ext;
1645 struct tlv_header *tlvh;
1646 uint16_t length = ntohs(top->header.length) - 3 * sizeof(uint32_t);
1647 uint16_t sum = 0;
1648
1649 vty_out(vty,
1650 " Extended Link TLV: Length %d\n Link Type: 0x%x\n"
1651 " Link ID: %s\n",
1652 ntohs(top->header.length), top->link_type,
1653 inet_ntoa(top->link_id));
1654 vty_out(vty, " Link data: %s\n", inet_ntoa(top->link_data));
1655
1656 tlvh = (struct tlv_header *)((char *)(ext) + TLV_HDR_SIZE
1657 + EXT_TLV_LINK_SIZE);
1658 for (; sum < length; tlvh = TLV_HDR_NEXT(tlvh)) {
1659 switch (ntohs(tlvh->type)) {
1660 case EXT_SUBTLV_ADJ_SID:
1661 sum += show_vty_ext_link_adj_sid(vty, tlvh);
1662 break;
1663 case EXT_SUBTLV_LAN_ADJ_SID:
1664 sum += show_vty_ext_link_lan_adj_sid(vty, tlvh);
1665 break;
1666 case EXT_SUBTLV_RMT_ITF_ADDR:
1667 sum += show_vty_ext_link_rmt_itf_addr(vty, tlvh);
1668 break;
1669 default:
1670 sum += show_vty_unknown_tlv(vty, tlvh);
1671 break;
1672 }
1673 }
1674
1675 return sum + sizeof(struct ext_tlv_link);
1676 }
1677
1678 /* Extended Link TLVs */
1679 static void ospf_ext_link_show_info(struct vty *vty, struct ospf_lsa *lsa)
1680 {
1681 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1682 struct tlv_header *tlvh;
1683 uint16_t length = 0, sum = 0;
1684
1685 /* Initialize TLV browsing */
1686 length = ntohs(lsah->length) - OSPF_LSA_HEADER_SIZE;
1687
1688 for (tlvh = TLV_HDR_TOP(lsah); sum < length;
1689 tlvh = TLV_HDR_NEXT(tlvh)) {
1690 switch (ntohs(tlvh->type)) {
1691 case EXT_TLV_LINK:
1692 sum += show_vty_link_info(vty, tlvh);
1693 break;
1694 default:
1695 sum += show_vty_unknown_tlv(vty, tlvh);
1696 break;
1697 }
1698 }
1699
1700 return;
1701 }
1702
1703 /* Prefix SID SubTLV */
1704 static uint16_t show_vty_ext_pref_pref_sid(struct vty *vty,
1705 struct tlv_header *tlvh)
1706 {
1707 struct ext_subtlv_prefix_sid *top =
1708 (struct ext_subtlv_prefix_sid *)tlvh;
1709
1710 vty_out(vty,
1711 " Prefix SID Sub-TLV: Length %d\n\tAlgorithm: "
1712 "%d\n\tFlags: 0x%x\n\tMT-ID:0x%x\n\t%s: %d\n",
1713 ntohs(top->header.length), top->algorithm, top->flags,
1714 top->mtid,
1715 CHECK_FLAG(top->flags, EXT_SUBTLV_PREFIX_SID_VFLG) ? "Label"
1716 : "Index",
1717 CHECK_FLAG(top->flags, EXT_SUBTLV_PREFIX_SID_VFLG)
1718 ? GET_LABEL(ntohl(top->value))
1719 : ntohl(top->value));
1720
1721 return TLV_SIZE(tlvh);
1722 }
1723
1724 /* Extended Prefix SubTLVs */
1725 static uint16_t show_vty_pref_info(struct vty *vty, struct tlv_header *ext)
1726 {
1727 struct ext_tlv_prefix *top = (struct ext_tlv_prefix *)ext;
1728 struct tlv_header *tlvh;
1729 uint16_t length = ntohs(top->header.length) - 2 * sizeof(uint32_t);
1730 uint16_t sum = 0;
1731
1732 vty_out(vty,
1733 " Extended Prefix TLV: Length %d\n\tRoute Type: %d\n"
1734 "\tAddress Family: 0x%x\n\tFlags: 0x%x\n\tAddress: %s/%d\n",
1735 ntohs(top->header.length), top->route_type, top->af, top->flags,
1736 inet_ntoa(top->address), top->pref_length);
1737
1738 tlvh = (struct tlv_header *)((char *)(ext) + TLV_HDR_SIZE
1739 + EXT_TLV_PREFIX_SIZE);
1740 for (; sum < length; tlvh = TLV_HDR_NEXT(tlvh)) {
1741 switch (ntohs(tlvh->type)) {
1742 case EXT_SUBTLV_PREFIX_SID:
1743 sum += show_vty_ext_pref_pref_sid(vty, tlvh);
1744 break;
1745 default:
1746 sum += show_vty_unknown_tlv(vty, tlvh);
1747 break;
1748 }
1749 }
1750
1751 return sum + sizeof(struct ext_tlv_prefix);
1752 }
1753
1754 /* Extended Prefix TLVs */
1755 static void ospf_ext_pref_show_info(struct vty *vty, struct ospf_lsa *lsa)
1756 {
1757 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1758 struct tlv_header *tlvh;
1759 uint16_t length = 0, sum = 0;
1760
1761 /* Initialize TLV browsing */
1762 length = ntohs(lsah->length) - OSPF_LSA_HEADER_SIZE;
1763
1764 for (tlvh = TLV_HDR_TOP(lsah); sum < length;
1765 tlvh = TLV_HDR_NEXT(tlvh)) {
1766 switch (ntohs(tlvh->type)) {
1767 case EXT_TLV_PREFIX:
1768 sum += show_vty_pref_info(vty, tlvh);
1769 break;
1770 default:
1771 sum += show_vty_unknown_tlv(vty, tlvh);
1772 break;
1773 }
1774 }
1775
1776 return;
1777 }