]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_ri.c
ospfd: Convert ospf_ri to use error-code subsystem
[mirror_frr.git] / ospfd / ospf_ri.c
1 /*
2 * This is an implementation of RFC4970 Router Information
3 * with support of RFC5088 PCE Capabilites announcement
4 *
5 * Module name: Router Information
6 * Author: Olivier Dugeon <olivier.dugeon@orange.com>
7 * Copyright (C) 2012 - 2017 Orange Labs http://www.orange.com/
8 *
9 * This file is part of GNU Quagga.
10 *
11 * GNU Zebra is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2, or (at your option) any
14 * later version.
15 *
16 * GNU Quagga is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; see the file COPYING; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <zebra.h>
27 #include <math.h>
28
29 #include "linklist.h"
30 #include "prefix.h"
31 #include "if.h"
32 #include "table.h"
33 #include "memory.h"
34 #include "command.h"
35 #include "vty.h"
36 #include "stream.h"
37 #include "log.h"
38 #include "thread.h"
39 #include "hash.h"
40 #include "sockunion.h" /* for inet_aton() */
41 #include "mpls.h"
42
43 #include "ospfd/ospfd.h"
44 #include "ospfd/ospf_interface.h"
45 #include "ospfd/ospf_ism.h"
46 #include "ospfd/ospf_asbr.h"
47 #include "ospfd/ospf_lsa.h"
48 #include "ospfd/ospf_lsdb.h"
49 #include "ospfd/ospf_neighbor.h"
50 #include "ospfd/ospf_nsm.h"
51 #include "ospfd/ospf_flood.h"
52 #include "ospfd/ospf_packet.h"
53 #include "ospfd/ospf_spf.h"
54 #include "ospfd/ospf_dump.h"
55 #include "ospfd/ospf_route.h"
56 #include "ospfd/ospf_ase.h"
57 #include "ospfd/ospf_zebra.h"
58 #include "ospfd/ospf_sr.h"
59 #include "ospfd/ospf_ri.h"
60 #include "ospfd/ospf_errors.h"
61
62 /* Store Router Information PCE TLV and SubTLV in network byte order. */
63 struct ospf_pce_info {
64 bool enabled;
65 struct ri_tlv_pce pce_header;
66 struct ri_pce_subtlv_address pce_address;
67 struct ri_pce_subtlv_path_scope pce_scope;
68 struct list *pce_domain;
69 struct list *pce_neighbor;
70 struct ri_pce_subtlv_cap_flag pce_cap_flag;
71 };
72
73 /*
74 * Store Router Information Segment Routing TLV and SubTLV
75 * in network byte order
76 */
77 struct ospf_ri_sr_info {
78 bool enabled;
79 /* Algorithms supported by the node */
80 struct ri_sr_tlv_sr_algorithm algo;
81 /*
82 * Segment Routing Global Block i.e. label range
83 * Only one range supported in this code
84 */
85 struct ri_sr_tlv_sid_label_range range;
86 /* Maximum SID Depth supported by the node */
87 struct ri_sr_tlv_node_msd msd;
88 };
89
90 /* Following structure are internal use only. */
91 struct ospf_router_info {
92 bool enabled;
93
94 uint8_t registered;
95 uint8_t scope;
96
97 /* Flags to manage this router information. */
98 #define RIFLG_LSA_ENGAGED 0x1
99 #define RIFLG_LSA_FORCED_REFRESH 0x2
100 uint32_t flags;
101
102 /* area pointer if flooding is Type 10 Null if flooding is AS scope */
103 struct ospf_area *area;
104 struct in_addr area_id;
105
106 /* Store Router Information Capabilities LSA */
107 struct ri_tlv_router_cap router_cap;
108
109 /* Store PCE capability LSA */
110 struct ospf_pce_info pce_info;
111
112 /* Store SR capability LSA */
113 struct ospf_ri_sr_info sr_info;
114 };
115
116 /*
117 * Global variable to manage Opaque-LSA/Router Information on this node.
118 * Note that all parameter values are stored in network byte order.
119 */
120 static struct ospf_router_info OspfRI;
121
122 /*------------------------------------------------------------------------------*
123 * Followings are initialize/terminate functions for Router Information
124 *handling.
125 *------------------------------------------------------------------------------*/
126
127 static void ospf_router_info_ism_change(struct ospf_interface *oi,
128 int old_status);
129 static void ospf_router_info_nsm_change(struct ospf_neighbor *nbr,
130 int old_status);
131 static void ospf_router_info_config_write_router(struct vty *vty);
132 static void ospf_router_info_show_info(struct vty *vty, struct ospf_lsa *lsa);
133 static int ospf_router_info_lsa_originate(void *arg);
134 static struct ospf_lsa *ospf_router_info_lsa_refresh(struct ospf_lsa *lsa);
135 static void ospf_router_info_lsa_schedule(enum lsa_opcode opcode);
136 static void ospf_router_info_register_vty(void);
137 static int ospf_router_info_lsa_update(struct ospf_lsa *lsa);
138 static void del_pce_info(void *val);
139
140 int ospf_router_info_init(void)
141 {
142
143 zlog_info("RI -> Initialize Router Information");
144
145 memset(&OspfRI, 0, sizeof(struct ospf_router_info));
146 OspfRI.enabled = false;
147 OspfRI.registered = 0;
148 OspfRI.scope = OSPF_OPAQUE_AS_LSA;
149 OspfRI.area_id.s_addr = 0;
150 OspfRI.flags = 0;
151
152 /* Initialize pce domain and neighbor list */
153 OspfRI.pce_info.enabled = false;
154 OspfRI.pce_info.pce_domain = list_new();
155 OspfRI.pce_info.pce_domain->del = del_pce_info;
156 OspfRI.pce_info.pce_neighbor = list_new();
157 OspfRI.pce_info.pce_neighbor->del = del_pce_info;
158
159 /* Initialize Segment Routing information structure */
160 OspfRI.sr_info.enabled = false;
161
162 ospf_router_info_register_vty();
163
164 return 0;
165 }
166
167 static int ospf_router_info_register(uint8_t scope)
168 {
169 int rc = 0;
170
171 if (OspfRI.registered)
172 return rc;
173
174 zlog_info("RI -> Register Router Information with scope %s(%d)",
175 scope == OSPF_OPAQUE_AREA_LSA ? "Area" : "AS", scope);
176 rc = ospf_register_opaque_functab(
177 scope, OPAQUE_TYPE_ROUTER_INFORMATION_LSA,
178 NULL, /* new interface */
179 NULL, /* del interface */
180 ospf_router_info_ism_change, ospf_router_info_nsm_change,
181 ospf_router_info_config_write_router,
182 NULL, /* Config. write interface */
183 NULL, /* Config. write debug */
184 ospf_router_info_show_info, ospf_router_info_lsa_originate,
185 ospf_router_info_lsa_refresh, ospf_router_info_lsa_update,
186 NULL); /* del_lsa_hook */
187
188 if (rc != 0) {
189 flog_warn(OSPF_WARN_OPAQUE_REGISTRATION,
190 "ospf_router_info_init: Failed to register functions");
191 return rc;
192 }
193
194 OspfRI.registered = 1;
195 OspfRI.scope = scope;
196 return rc;
197 }
198
199 static int ospf_router_info_unregister()
200 {
201
202 if ((OspfRI.scope != OSPF_OPAQUE_AS_LSA)
203 && (OspfRI.scope != OSPF_OPAQUE_AREA_LSA)) {
204 assert("Unable to unregister Router Info functions: Wrong scope!" == NULL);
205 return -1;
206 }
207
208 ospf_delete_opaque_functab(OspfRI.scope,
209 OPAQUE_TYPE_ROUTER_INFORMATION_LSA);
210
211 OspfRI.registered = 0;
212 return 0;
213 }
214
215 void ospf_router_info_term(void)
216 {
217
218 list_delete_and_null(&OspfRI.pce_info.pce_domain);
219 list_delete_and_null(&OspfRI.pce_info.pce_neighbor);
220
221 OspfRI.enabled = false;
222
223 ospf_router_info_unregister();
224
225 return;
226 }
227
228 void ospf_router_info_finish(void)
229 {
230 list_delete_all_node(OspfRI.pce_info.pce_domain);
231 list_delete_all_node(OspfRI.pce_info.pce_neighbor);
232
233 OspfRI.enabled = false;
234 }
235
236 static void del_pce_info(void *val)
237 {
238 XFREE(MTYPE_OSPF_PCE_PARAMS, val);
239 return;
240 }
241
242 /* Catch RI LSA flooding Scope for ospf_ext.[h,c] code */
243 struct scope_info ospf_router_info_get_flooding_scope(void)
244 {
245 struct scope_info flooding_scope;
246
247 if (OspfRI.scope == OSPF_OPAQUE_AS_LSA) {
248 flooding_scope.scope = OSPF_OPAQUE_AS_LSA;
249 flooding_scope.area_id.s_addr = 0;
250 return flooding_scope;
251 }
252 flooding_scope.scope = OSPF_OPAQUE_AREA_LSA;
253 flooding_scope.area_id.s_addr = OspfRI.area_id.s_addr;
254 return flooding_scope;
255 }
256
257 /*------------------------------------------------------------------------*
258 * Followings are control functions for ROUTER INFORMATION parameters
259 *management.
260 *------------------------------------------------------------------------*/
261
262 static void set_router_info_capabilities(struct ri_tlv_router_cap *ric,
263 uint32_t cap)
264 {
265 ric->header.type = htons(RI_TLV_CAPABILITIES);
266 ric->header.length = htons(RI_TLV_LENGTH);
267 ric->value = htonl(cap);
268 return;
269 }
270
271 static int set_pce_header(struct ospf_pce_info *pce)
272 {
273 uint16_t length = 0;
274 struct listnode *node;
275 struct ri_pce_subtlv_domain *domain;
276 struct ri_pce_subtlv_neighbor *neighbor;
277
278 /* PCE Address */
279 if (ntohs(pce->pce_address.header.type) != 0)
280 length += TLV_SIZE(&pce->pce_address.header);
281
282 /* PCE Path Scope */
283 if (ntohs(pce->pce_scope.header.type) != 0)
284 length += TLV_SIZE(&pce->pce_scope.header);
285
286 /* PCE Domain */
287 for (ALL_LIST_ELEMENTS_RO(pce->pce_domain, node, domain)) {
288 if (ntohs(domain->header.type) != 0)
289 length += TLV_SIZE(&domain->header);
290 }
291
292 /* PCE Neighbor */
293 for (ALL_LIST_ELEMENTS_RO(pce->pce_neighbor, node, neighbor)) {
294 if (ntohs(neighbor->header.type) != 0)
295 length += TLV_SIZE(&neighbor->header);
296 }
297
298 /* PCE Capabilities */
299 if (ntohs(pce->pce_cap_flag.header.type) != 0)
300 length += TLV_SIZE(&pce->pce_cap_flag.header);
301
302 if (length != 0) {
303 pce->pce_header.header.type = htons(RI_TLV_PCE);
304 pce->pce_header.header.length = htons(length);
305 pce->enabled = true;
306 } else {
307 pce->pce_header.header.type = 0;
308 pce->pce_header.header.length = 0;
309 pce->enabled = false;
310 }
311
312 return length;
313 }
314
315 static void set_pce_address(struct in_addr ipv4, struct ospf_pce_info *pce)
316 {
317
318 /* Enable PCE Info */
319 pce->pce_header.header.type = htons(RI_TLV_PCE);
320 /* Set PCE Address */
321 pce->pce_address.header.type = htons(RI_PCE_SUBTLV_ADDRESS);
322 pce->pce_address.header.length = htons(PCE_ADDRESS_LENGTH_IPV4);
323 pce->pce_address.address.type = htons(PCE_ADDRESS_TYPE_IPV4);
324 pce->pce_address.address.value = ipv4;
325
326 return;
327 }
328
329 static void set_pce_path_scope(uint32_t scope, struct ospf_pce_info *pce)
330 {
331
332 /* Set PCE Scope */
333 pce->pce_scope.header.type = htons(RI_PCE_SUBTLV_PATH_SCOPE);
334 pce->pce_scope.header.length = htons(RI_TLV_LENGTH);
335 pce->pce_scope.value = htonl(scope);
336
337 return;
338 }
339
340 static void set_pce_domain(uint16_t type, uint32_t domain,
341 struct ospf_pce_info *pce)
342 {
343
344 struct ri_pce_subtlv_domain *new;
345
346 /* Create new domain info */
347 new = XCALLOC(MTYPE_OSPF_PCE_PARAMS,
348 sizeof(struct ri_pce_subtlv_domain));
349
350 new->header.type = htons(RI_PCE_SUBTLV_DOMAIN);
351 new->header.length = htons(PCE_ADDRESS_LENGTH_IPV4);
352 new->type = htons(type);
353 new->value = htonl(domain);
354
355 /* Add new domain to the list */
356 listnode_add(pce->pce_domain, new);
357
358 return;
359 }
360
361 static void unset_pce_domain(uint16_t type, uint32_t domain,
362 struct ospf_pce_info *pce)
363 {
364 struct listnode *node;
365 struct ri_pce_subtlv_domain *old = NULL;
366 int found = 0;
367
368 /* Search the corresponding node */
369 for (ALL_LIST_ELEMENTS_RO(pce->pce_domain, node, old)) {
370 if ((old->type == htons(type))
371 && (old->value == htonl(domain))) {
372 found = 1;
373 break;
374 }
375 }
376
377 /* if found remove it */
378 if (found) {
379 listnode_delete(pce->pce_domain, old);
380
381 /* Avoid misjudgement in the next lookup. */
382 if (listcount(pce->pce_domain) == 0)
383 pce->pce_domain->head = pce->pce_domain->tail = NULL;
384
385 /* Finally free the old domain */
386 XFREE(MTYPE_OSPF_PCE_PARAMS, old);
387 }
388 }
389
390 static void set_pce_neighbor(uint16_t type, uint32_t domain,
391 struct ospf_pce_info *pce)
392 {
393
394 struct ri_pce_subtlv_neighbor *new;
395
396 /* Create new neighbor info */
397 new = XCALLOC(MTYPE_OSPF_PCE_PARAMS,
398 sizeof(struct ri_pce_subtlv_neighbor));
399
400 new->header.type = htons(RI_PCE_SUBTLV_NEIGHBOR);
401 new->header.length = htons(PCE_ADDRESS_LENGTH_IPV4);
402 new->type = htons(type);
403 new->value = htonl(domain);
404
405 /* Add new domain to the list */
406 listnode_add(pce->pce_neighbor, new);
407
408 return;
409 }
410
411 static void unset_pce_neighbor(uint16_t type, uint32_t domain,
412 struct ospf_pce_info *pce)
413 {
414 struct listnode *node;
415 struct ri_pce_subtlv_neighbor *old = NULL;
416 int found = 0;
417
418 /* Search the corresponding node */
419 for (ALL_LIST_ELEMENTS_RO(pce->pce_neighbor, node, old)) {
420 if ((old->type == htons(type))
421 && (old->value == htonl(domain))) {
422 found = 1;
423 break;
424 }
425 }
426
427 /* if found remove it */
428 if (found) {
429 listnode_delete(pce->pce_neighbor, old);
430
431 /* Avoid misjudgement in the next lookup. */
432 if (listcount(pce->pce_neighbor) == 0)
433 pce->pce_neighbor->head = pce->pce_neighbor->tail =
434 NULL;
435
436 /* Finally free the old domain */
437 XFREE(MTYPE_OSPF_PCE_PARAMS, old);
438 }
439 }
440
441 static void set_pce_cap_flag(uint32_t cap, struct ospf_pce_info *pce)
442 {
443
444 /* Set PCE Capabilities flag */
445 pce->pce_cap_flag.header.type = htons(RI_PCE_SUBTLV_CAP_FLAG);
446 pce->pce_cap_flag.header.length = htons(RI_TLV_LENGTH);
447 pce->pce_cap_flag.value = htonl(cap);
448
449 return;
450 }
451
452 /* Segment Routing TLV setter */
453
454 /* Algorithm SubTLV - section 3.1 */
455 static void set_sr_algorithm(uint8_t algo)
456 {
457
458 OspfRI.sr_info.algo.value[0] = algo;
459 for (int i = 1; i < ALGORITHM_COUNT; i++)
460 OspfRI.sr_info.algo.value[i] = SR_ALGORITHM_UNSET;
461
462 /* Set TLV type and length == only 1 Algorithm */
463 TLV_TYPE(OspfRI.sr_info.algo) = htons(RI_SR_TLV_SR_ALGORITHM);
464 TLV_LEN(OspfRI.sr_info.algo) = htons(sizeof(uint8_t));
465 }
466
467 /* unset Aglogithm SubTLV */
468 static void unset_sr_algorithm(uint8_t algo)
469 {
470
471 for (int i = 0; i < ALGORITHM_COUNT; i++)
472 OspfRI.sr_info.algo.value[i] = SR_ALGORITHM_UNSET;
473
474 /* Unset TLV type and length */
475 TLV_TYPE(OspfRI.sr_info.algo) = htons(0);
476 TLV_LEN(OspfRI.sr_info.algo) = htons(0);
477 }
478
479 /* Segment Routing Global Block SubTLV - section 3.2 */
480 static void set_sr_sid_label_range(struct sr_srgb srgb)
481 {
482 /* Set Header */
483 TLV_TYPE(OspfRI.sr_info.range) = htons(RI_SR_TLV_SID_LABEL_RANGE);
484 TLV_LEN(OspfRI.sr_info.range) =
485 htons(SUBTLV_SID_LABEL_SIZE + sizeof(uint32_t));
486 /* Set Range Size */
487 OspfRI.sr_info.range.size = htonl(SET_RANGE_SIZE(srgb.range_size));
488 /* Set Lower bound label SubTLV */
489 TLV_TYPE(OspfRI.sr_info.range.lower) = htons(SUBTLV_SID_LABEL);
490 TLV_LEN(OspfRI.sr_info.range.lower) = htons(SID_RANGE_LABEL_LENGTH);
491 OspfRI.sr_info.range.lower.value = htonl(SET_LABEL(srgb.lower_bound));
492 }
493
494 /* Unset this SRGB SubTLV */
495 static void unset_sr_sid_label_range(void)
496 {
497
498 TLV_TYPE(OspfRI.sr_info.range) = htons(0);
499 TLV_LEN(OspfRI.sr_info.range) = htons(0);
500 TLV_TYPE(OspfRI.sr_info.range.lower) = htons(0);
501 TLV_LEN(OspfRI.sr_info.range.lower) = htons(0);
502 }
503
504 /* Set Maximum Stack Depth for this router */
505 static void set_sr_node_msd(uint8_t msd)
506 {
507 TLV_TYPE(OspfRI.sr_info.msd) = htons(RI_SR_TLV_NODE_MSD);
508 TLV_LEN(OspfRI.sr_info.msd) = htons(sizeof(uint32_t));
509 OspfRI.sr_info.msd.value = msd;
510 }
511
512 /* Unset this router MSD */
513 static void unset_sr_node_msd(void)
514 {
515 TLV_TYPE(OspfRI.sr_info.msd) = htons(0);
516 TLV_LEN(OspfRI.sr_info.msd) = htons(0);
517 }
518
519 static void unset_param(void *tlv_buffer)
520 {
521 struct tlv_header *tlv = (struct tlv_header *)tlv_buffer;
522
523 tlv->type = 0;
524 /* Fill the Value to 0 */
525 memset(TLV_DATA(tlv_buffer), 0, TLV_BODY_SIZE(tlv));
526 tlv->length = 0;
527
528 return;
529 }
530
531 static void initialize_params(struct ospf_router_info *ori)
532 {
533 uint32_t cap = 0;
534 struct ospf *top;
535
536 /*
537 * Initialize default Router Information Capabilities.
538 */
539 cap = RI_TE_SUPPORT;
540
541 set_router_info_capabilities(&ori->router_cap, cap);
542
543 /* If Area address is not null and exist, retrieve corresponding
544 * structure */
545 top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
546 zlog_info("RI-> Initialize Router Info for %s scope within area %s",
547 OspfRI.scope == OSPF_OPAQUE_AREA_LSA ? "Area" : "AS",
548 inet_ntoa(OspfRI.area_id));
549
550 /* Try to get the Area context at this step. Do it latter if not
551 * available */
552 if ((OspfRI.scope == OSPF_OPAQUE_AREA_LSA) && (OspfRI.area == NULL))
553 OspfRI.area = ospf_area_lookup_by_area_id(top, OspfRI.area_id);
554
555 /*
556 * Initialize default PCE Information values
557 */
558 /* PCE address == OSPF Router ID */
559 set_pce_address(top->router_id, &ori->pce_info);
560
561 /* PCE scope */
562 cap = 7; /* Set L, R and Rd bits to one = intra & inter-area path
563 computation */
564 set_pce_path_scope(cap, &ori->pce_info);
565
566 /* PCE Capabilities */
567 cap = PCE_CAP_BIDIRECTIONAL | PCE_CAP_DIVERSE_PATH | PCE_CAP_OBJECTIVES
568 | PCE_CAP_ADDITIVE | PCE_CAP_MULTIPLE_REQ;
569 set_pce_cap_flag(cap, &ori->pce_info);
570
571 return;
572 }
573
574 static int is_mandated_params_set(struct ospf_router_info ori)
575 {
576 int rc = 0;
577
578 if (ntohs(ori.router_cap.header.type) == 0)
579 return rc;
580
581 if ((ntohs(ori.pce_info.pce_header.header.type) == RI_TLV_PCE)
582 && (ntohs(ori.pce_info.pce_address.header.type) == 0)
583 && (ntohs(ori.pce_info.pce_cap_flag.header.type) == 0))
584 return rc;
585
586 if ((ori.sr_info.enabled) && (ntohs(TLV_TYPE(ori.sr_info.algo)) == 0)
587 && (ntohs(TLV_TYPE(ori.sr_info.range)) == 0))
588 return rc;
589
590 rc = 1;
591
592 return rc;
593 }
594
595 /*
596 * Used by Segment Routing to set new TLVs and Sub-TLVs values
597 *
598 * @param enable To activate or not Segment Routing router Information flooding
599 * @param size Size of Label Range i.e. SRGB size
600 * @param lower Lower bound of the Label Range i.e. SRGB first label
601 * @param msd Maximum label Stack Depth suported by the router
602 *
603 * @return none
604 */
605 void ospf_router_info_update_sr(bool enable, struct sr_srgb srgb, uint8_t msd)
606 {
607
608 /* First activate and initialize Router Information is necessary */
609 if (!OspfRI.enabled) {
610 OspfRI.enabled = true;
611 initialize_params(&OspfRI);
612 }
613
614 if (IS_DEBUG_OSPF_SR)
615 zlog_debug("RI-> %s Routing Information for Segment Routing",
616 enable ? "Enable" : "Disable");
617
618 /* Unset or Set SR parameters */
619 if (!enable) {
620 unset_sr_algorithm(SR_ALGORITHM_SPF);
621 unset_sr_sid_label_range();
622 unset_sr_node_msd();
623 OspfRI.sr_info.enabled = false;
624 } else {
625 // Only SR_ALGORITHM_SPF is supported
626 set_sr_algorithm(SR_ALGORITHM_SPF);
627 set_sr_sid_label_range(srgb);
628 if (msd != 0)
629 set_sr_node_msd(msd);
630 else
631 unset_sr_node_msd();
632 OspfRI.sr_info.enabled = true;
633 }
634
635 /* Refresh if already engaged or originate RI LSA */
636 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
637 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
638 else
639 ospf_router_info_lsa_schedule(REORIGINATE_THIS_LSA);
640 }
641
642 /*------------------------------------------------------------------------*
643 * Followings are callback functions against generic Opaque-LSAs handling.
644 *------------------------------------------------------------------------*/
645 static void ospf_router_info_ism_change(struct ospf_interface *oi,
646 int old_state)
647 {
648 /* So far, nothing to do here. */
649 return;
650 }
651
652 static void ospf_router_info_nsm_change(struct ospf_neighbor *nbr,
653 int old_state)
654 {
655 /* So far, nothing to do here. */
656 return;
657 }
658
659 /*------------------------------------------------------------------------*
660 * Followings are OSPF protocol processing functions for ROUTER INFORMATION
661 *------------------------------------------------------------------------*/
662
663 static void build_tlv_header(struct stream *s, struct tlv_header *tlvh)
664 {
665
666 stream_put(s, tlvh, sizeof(struct tlv_header));
667 return;
668 }
669
670 static void build_tlv(struct stream *s, struct tlv_header *tlvh)
671 {
672
673 if (ntohs(tlvh->type) != 0) {
674 build_tlv_header(s, tlvh);
675 stream_put(s, TLV_DATA(tlvh), TLV_BODY_SIZE(tlvh));
676 }
677 return;
678 }
679
680 static void ospf_router_info_lsa_body_set(struct stream *s)
681 {
682
683 struct listnode *node;
684 struct ri_pce_subtlv_domain *domain;
685 struct ri_pce_subtlv_neighbor *neighbor;
686
687 /* Build Router Information TLV */
688 build_tlv(s, &OspfRI.router_cap.header);
689
690 /* Build Segment Routing TLVs if enabled */
691 if (OspfRI.sr_info.enabled) {
692 /* Build Algorithm TLV */
693 build_tlv(s, &TLV_HDR(OspfRI.sr_info.algo));
694 /* Build SRGB TLV */
695 build_tlv(s, &TLV_HDR(OspfRI.sr_info.range));
696 /* Build MSD TLV */
697 build_tlv(s, &TLV_HDR(OspfRI.sr_info.msd));
698 }
699
700 /* Add RI PCE TLV if it is set */
701 if (OspfRI.pce_info.enabled) {
702
703 /* Compute PCE Info header first */
704 set_pce_header(&OspfRI.pce_info);
705
706 /* Build PCE TLV */
707 build_tlv_header(s, &OspfRI.pce_info.pce_header.header);
708
709 /* Build PCE address sub-tlv */
710 build_tlv(s, &OspfRI.pce_info.pce_address.header);
711
712 /* Build PCE path scope sub-tlv */
713 build_tlv(s, &OspfRI.pce_info.pce_scope.header);
714
715 /* Build PCE domain sub-tlv */
716 for (ALL_LIST_ELEMENTS_RO(OspfRI.pce_info.pce_domain, node,
717 domain))
718 build_tlv(s, &domain->header);
719
720 /* Build PCE neighbor sub-tlv */
721 for (ALL_LIST_ELEMENTS_RO(OspfRI.pce_info.pce_neighbor, node,
722 neighbor))
723 build_tlv(s, &neighbor->header);
724
725 /* Build PCE cap flag sub-tlv */
726 build_tlv(s, &OspfRI.pce_info.pce_cap_flag.header);
727 }
728
729 return;
730 }
731
732 /* Create new opaque-LSA. */
733 static struct ospf_lsa *ospf_router_info_lsa_new()
734 {
735 struct ospf *top;
736 struct stream *s;
737 struct lsa_header *lsah;
738 struct ospf_lsa *new = NULL;
739 uint8_t options, lsa_type;
740 struct in_addr lsa_id;
741 uint32_t tmp;
742 uint16_t length;
743
744 /* Create a stream for LSA. */
745 s = stream_new(OSPF_MAX_LSA_SIZE);
746
747 lsah = (struct lsa_header *)STREAM_DATA(s);
748
749 options = OSPF_OPTION_E; /* Enable AS external as we flood RI with
750 Opaque Type 11 */
751 options |= OSPF_OPTION_O; /* Don't forget this :-) */
752
753 lsa_type = OspfRI.scope;
754 /* LSA ID == 0 for Router Information see RFC 4970 */
755 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_ROUTER_INFORMATION_LSA, 0);
756 lsa_id.s_addr = htonl(tmp);
757
758 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
759 zlog_debug(
760 "LSA[Type%d:%s]: Create an Opaque-LSA/ROUTER INFORMATION instance",
761 lsa_type, inet_ntoa(lsa_id));
762
763 top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
764
765 /* Set opaque-LSA header fields. */
766 lsa_header_set(s, options, lsa_type, lsa_id, top->router_id);
767
768 /* Set opaque-LSA body fields. */
769 ospf_router_info_lsa_body_set(s);
770
771 /* Set length. */
772 length = stream_get_endp(s);
773 lsah->length = htons(length);
774
775 /* Now, create an OSPF LSA instance. */
776 new = ospf_lsa_new_and_data(length);
777
778 new->area = OspfRI.area; /* Area must be null if the Opaque type is AS
779 scope, fulfill otherwise */
780
781 if (new->area && new->area->ospf)
782 new->vrf_id = new->area->ospf->vrf_id;
783 else
784 new->vrf_id = VRF_DEFAULT;
785
786 SET_FLAG(new->flags, OSPF_LSA_SELF);
787 memcpy(new->data, lsah, length);
788 stream_free(s);
789
790 return new;
791 }
792
793 static int ospf_router_info_lsa_originate1(void *arg)
794 {
795 struct ospf_lsa *new;
796 struct ospf *top;
797 struct ospf_area *area;
798 int rc = -1;
799 vrf_id_t vrf_id = VRF_DEFAULT;
800
801 /* First check if the area is known if flooding scope is Area */
802 if (OspfRI.scope == OSPF_OPAQUE_AREA_LSA) {
803 area = (struct ospf_area *)arg;
804 if (area->area_id.s_addr != OspfRI.area_id.s_addr) {
805 zlog_debug(
806 "RI -> This is not the Router Information Area. Stop processing");
807 return rc;
808 }
809 OspfRI.area = area;
810 if (area->ospf)
811 vrf_id = area->ospf->vrf_id;
812 }
813
814 /* Create new Opaque-LSA/ROUTER INFORMATION instance. */
815 new = ospf_router_info_lsa_new();
816 new->vrf_id = vrf_id;
817
818 /* Get ospf info */
819 top = ospf_lookup_by_vrf_id(vrf_id);
820 if (top == NULL) {
821 zlog_debug("%s: ospf instance not found for vrf id %u",
822 __PRETTY_FUNCTION__, vrf_id);
823 ospf_lsa_unlock(&new);
824 return rc;
825 }
826
827 /* Install this LSA into LSDB. */
828 if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
829 flog_warn(
830 OSPF_WARN_LSA_INSTALL_FAILURE,
831 "ospf_router_info_lsa_originate1: ospf_lsa_install() ?");
832 ospf_lsa_unlock(&new);
833 return rc;
834 }
835
836 /* Now this Router Info parameter entry has associated LSA. */
837 SET_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED);
838
839 /* Update new LSA origination count. */
840 top->lsa_originate_count++;
841
842 /* Flood new LSA through AS. */
843 if (OspfRI.scope == OSPF_OPAQUE_AS_LSA)
844 ospf_flood_through_as(top, NULL /*nbr */, new);
845 else
846 ospf_flood_through_area(OspfRI.area, NULL /*nbr */, new);
847
848 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
849 zlog_debug(
850 "LSA[Type%d:%s]: Originate Opaque-LSA/ROUTER INFORMATION",
851 new->data->type, inet_ntoa(new->data->id));
852 ospf_lsa_header_dump(new->data);
853 }
854
855 rc = 0;
856 return rc;
857 }
858
859 static int ospf_router_info_lsa_originate(void *arg)
860 {
861
862 int rc = -1;
863
864 if (!OspfRI.enabled) {
865 zlog_info(
866 "ospf_router_info_lsa_originate: ROUTER INFORMATION is disabled now.");
867 rc = 0; /* This is not an error case. */
868 return rc;
869 }
870
871 /* Check if Router Information LSA is already engaged */
872 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED)) {
873 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_FORCED_REFRESH)) {
874 UNSET_FLAG(OspfRI.flags, RIFLG_LSA_FORCED_REFRESH);
875 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
876 }
877 } else {
878 if (!is_mandated_params_set(OspfRI))
879 flog_warn(
880 OSPF_WARN_LSA,
881 "ospf_router_info_lsa_originate: lacks mandated ROUTER INFORMATION parameters");
882
883 /* Ok, let's try to originate an LSA */
884 if (ospf_router_info_lsa_originate1(arg) != 0)
885 return rc;
886 }
887
888 rc = 0;
889 return rc;
890 }
891
892 static struct ospf_lsa *ospf_router_info_lsa_refresh(struct ospf_lsa *lsa)
893 {
894 struct ospf_lsa *new = NULL;
895 struct ospf *top;
896
897 if (!OspfRI.enabled) {
898 /*
899 * This LSA must have flushed before due to ROUTER INFORMATION
900 * status change.
901 * It seems a slip among routers in the routing domain.
902 */
903 zlog_info(
904 "ospf_router_info_lsa_refresh: ROUTER INFORMATION is disabled now.");
905 lsa->data->ls_age =
906 htons(OSPF_LSA_MAXAGE); /* Flush it anyway. */
907 }
908
909 /* Verify that the Router Information ID is supported */
910 if (GET_OPAQUE_ID(ntohl(lsa->data->id.s_addr)) != 0) {
911 flog_warn(
912 OSPF_WARN_LSA,
913 "ospf_router_info_lsa_refresh: Unsupported Router Information ID");
914 return NULL;
915 }
916
917 /* If the lsa's age reached to MaxAge, start flushing procedure. */
918 if (IS_LSA_MAXAGE(lsa)) {
919 UNSET_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED);
920 ospf_opaque_lsa_flush_schedule(lsa);
921 return NULL;
922 }
923
924 /* Create new Opaque-LSA/ROUTER INFORMATION instance. */
925 new = ospf_router_info_lsa_new();
926 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
927 new->vrf_id = lsa->vrf_id;
928
929 /* Install this LSA into LSDB. */
930 /* Given "lsa" will be freed in the next function. */
931 top = ospf_lookup_by_vrf_id(lsa->vrf_id);
932 if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
933 flog_warn(OSPF_WARN_LSA_INSTALL_FAILURE,
934 "ospf_router_info_lsa_refresh: ospf_lsa_install() ?");
935 ospf_lsa_unlock(&new);
936 return new;
937 }
938
939 /* Flood updated LSA through AS or AREA depending of OspfRI.scope. */
940 if (OspfRI.scope == OSPF_OPAQUE_AS_LSA)
941 ospf_flood_through_as(top, NULL /*nbr */, new);
942 else
943 ospf_flood_through_area(OspfRI.area, NULL /*nbr */, new);
944
945 /* Debug logging. */
946 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
947 zlog_debug(
948 "LSA[Type%d:%s]: Refresh Opaque-LSA/ROUTER INFORMATION",
949 new->data->type, inet_ntoa(new->data->id));
950 ospf_lsa_header_dump(new->data);
951 }
952
953 return new;
954 }
955
956 static void ospf_router_info_lsa_schedule(enum lsa_opcode opcode)
957 {
958 struct ospf_lsa lsa;
959 struct lsa_header lsah;
960 struct ospf *top;
961 uint32_t tmp;
962
963 memset(&lsa, 0, sizeof(lsa));
964 memset(&lsah, 0, sizeof(lsah));
965
966 zlog_debug("RI-> LSA schedule %s%s%s",
967 opcode == REORIGINATE_THIS_LSA ? "Re-Originate" : "",
968 opcode == REFRESH_THIS_LSA ? "Refresh" : "",
969 opcode == FLUSH_THIS_LSA ? "Flush" : "");
970
971 /* Check LSA flags state coherence */
972 if (!CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED)
973 && (opcode != REORIGINATE_THIS_LSA))
974 return;
975
976 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED)
977 && (opcode == REORIGINATE_THIS_LSA))
978 opcode = REFRESH_THIS_LSA;
979
980 top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
981 if ((OspfRI.scope == OSPF_OPAQUE_AREA_LSA) && (OspfRI.area == NULL)) {
982 flog_warn(
983 OSPF_WARN_LSA,
984 "ospf_router_info_lsa_schedule(): Router Info is Area scope flooding but area is not set");
985 OspfRI.area = ospf_area_lookup_by_area_id(top, OspfRI.area_id);
986 }
987 lsa.area = OspfRI.area;
988 lsa.data = &lsah;
989 lsah.type = OspfRI.scope;
990
991 /* LSA ID is set to 0 for the Router Information. See RFC 4970 */
992 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_ROUTER_INFORMATION_LSA, 0);
993 lsah.id.s_addr = htonl(tmp);
994
995 switch (opcode) {
996 case REORIGINATE_THIS_LSA:
997 if (OspfRI.scope == OSPF_OPAQUE_AREA_LSA)
998 ospf_opaque_lsa_reoriginate_schedule(
999 (void *)OspfRI.area, OSPF_OPAQUE_AREA_LSA,
1000 OPAQUE_TYPE_ROUTER_INFORMATION_LSA);
1001 else
1002 ospf_opaque_lsa_reoriginate_schedule(
1003 (void *)top, OSPF_OPAQUE_AS_LSA,
1004 OPAQUE_TYPE_ROUTER_INFORMATION_LSA);
1005 break;
1006 case REFRESH_THIS_LSA:
1007 ospf_opaque_lsa_refresh_schedule(&lsa);
1008 break;
1009 case FLUSH_THIS_LSA:
1010 UNSET_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED);
1011 ospf_opaque_lsa_flush_schedule(&lsa);
1012 break;
1013 }
1014
1015 return;
1016 }
1017
1018 /* Callback to handle Segment Routing information */
1019 static int ospf_router_info_lsa_update(struct ospf_lsa *lsa)
1020 {
1021
1022 /* Sanity Check */
1023 if (lsa == NULL) {
1024 flog_warn(OSPF_WARN_LSA, "OSPF-RI (%s): Abort! LSA is NULL",
1025 __func__);
1026 return -1;
1027 }
1028
1029 /* Process only Opaque LSA */
1030 if ((lsa->data->type != OSPF_OPAQUE_AREA_LSA)
1031 && (lsa->data->type != OSPF_OPAQUE_AS_LSA))
1032 return 0;
1033
1034 /* Process only Router Information LSA */
1035 if (GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr))
1036 != OPAQUE_TYPE_ROUTER_INFORMATION_LSA)
1037 return 0;
1038
1039 /* Check if it is not my LSA */
1040 if (IS_LSA_SELF(lsa))
1041 return 0;
1042
1043 /* Check if Router Info & Segment Routing are enable */
1044 if (!OspfRI.enabled || !OspfRI.sr_info.enabled)
1045 return 0;
1046
1047 /* Call Segment Routing LSA update or deletion */
1048 if (!IS_LSA_MAXAGE(lsa))
1049 ospf_sr_ri_lsa_update(lsa);
1050 else
1051 ospf_sr_ri_lsa_delete(lsa);
1052
1053 return 0;
1054 }
1055
1056 /*------------------------------------------------------------------------*
1057 * Followings are vty session control functions.
1058 *------------------------------------------------------------------------*/
1059
1060 static uint16_t show_vty_router_cap(struct vty *vty, struct tlv_header *tlvh)
1061 {
1062 struct ri_tlv_router_cap *top = (struct ri_tlv_router_cap *)tlvh;
1063
1064 if (vty != NULL)
1065 vty_out(vty, " Router Capabilities: 0x%x\n",
1066 ntohl(top->value));
1067 else
1068 zlog_debug(" Router Capabilities: 0x%x", ntohl(top->value));
1069
1070 return TLV_SIZE(tlvh);
1071 }
1072
1073 static uint16_t show_vty_pce_subtlv_address(struct vty *vty,
1074 struct tlv_header *tlvh)
1075 {
1076 struct ri_pce_subtlv_address *top =
1077 (struct ri_pce_subtlv_address *)tlvh;
1078
1079 if (ntohs(top->address.type) == PCE_ADDRESS_TYPE_IPV4) {
1080 if (vty != NULL)
1081 vty_out(vty, " PCE Address: %s\n",
1082 inet_ntoa(top->address.value));
1083 else
1084 zlog_debug(" PCE Address: %s",
1085 inet_ntoa(top->address.value));
1086 } else {
1087 /* TODO: Add support to IPv6 with inet_ntop() */
1088 if (vty != NULL)
1089 vty_out(vty, " PCE Address: 0x%x\n",
1090 ntohl(top->address.value.s_addr));
1091 else
1092 zlog_debug(" PCE Address: 0x%x",
1093 ntohl(top->address.value.s_addr));
1094 }
1095
1096 return TLV_SIZE(tlvh);
1097 }
1098
1099 static uint16_t show_vty_pce_subtlv_path_scope(struct vty *vty,
1100 struct tlv_header *tlvh)
1101 {
1102 struct ri_pce_subtlv_path_scope *top =
1103 (struct ri_pce_subtlv_path_scope *)tlvh;
1104
1105 if (vty != NULL)
1106 vty_out(vty, " PCE Path Scope: 0x%x\n", ntohl(top->value));
1107 else
1108 zlog_debug(" PCE Path Scope: 0x%x", ntohl(top->value));
1109
1110 return TLV_SIZE(tlvh);
1111 }
1112
1113 static uint16_t show_vty_pce_subtlv_domain(struct vty *vty,
1114 struct tlv_header *tlvh)
1115 {
1116 struct ri_pce_subtlv_domain *top = (struct ri_pce_subtlv_domain *)tlvh;
1117 struct in_addr tmp;
1118
1119 if (ntohs(top->type) == PCE_DOMAIN_TYPE_AREA) {
1120 tmp.s_addr = top->value;
1121 if (vty != NULL)
1122 vty_out(vty, " PCE domain Area: %s\n", inet_ntoa(tmp));
1123 else
1124 zlog_debug(" PCE domain Area: %s", inet_ntoa(tmp));
1125 } else {
1126 if (vty != NULL)
1127 vty_out(vty, " PCE domain AS: %d\n",
1128 ntohl(top->value));
1129 else
1130 zlog_debug(" PCE domain AS: %d", ntohl(top->value));
1131 }
1132 return TLV_SIZE(tlvh);
1133 }
1134
1135 static uint16_t show_vty_pce_subtlv_neighbor(struct vty *vty,
1136 struct tlv_header *tlvh)
1137 {
1138
1139 struct ri_pce_subtlv_neighbor *top =
1140 (struct ri_pce_subtlv_neighbor *)tlvh;
1141 struct in_addr tmp;
1142
1143 if (ntohs(top->type) == PCE_DOMAIN_TYPE_AREA) {
1144 tmp.s_addr = top->value;
1145 if (vty != NULL)
1146 vty_out(vty, " PCE neighbor Area: %s\n",
1147 inet_ntoa(tmp));
1148 else
1149 zlog_debug(" PCE neighbor Area: %s", inet_ntoa(tmp));
1150 } else {
1151 if (vty != NULL)
1152 vty_out(vty, " PCE neighbor AS: %d\n",
1153 ntohl(top->value));
1154 else
1155 zlog_debug(" PCE neighbor AS: %d",
1156 ntohl(top->value));
1157 }
1158 return TLV_SIZE(tlvh);
1159 }
1160
1161 static uint16_t show_vty_pce_subtlv_cap_flag(struct vty *vty,
1162 struct tlv_header *tlvh)
1163 {
1164 struct ri_pce_subtlv_cap_flag *top =
1165 (struct ri_pce_subtlv_cap_flag *)tlvh;
1166
1167 if (vty != NULL)
1168 vty_out(vty, " PCE Capabilities Flag: 0x%x\n",
1169 ntohl(top->value));
1170 else
1171 zlog_debug(" PCE Capabilities Flag: 0x%x",
1172 ntohl(top->value));
1173
1174 return TLV_SIZE(tlvh);
1175 }
1176
1177 static uint16_t show_vty_unknown_tlv(struct vty *vty, struct tlv_header *tlvh)
1178 {
1179 if (vty != NULL)
1180 vty_out(vty, " Unknown TLV: [type(0x%x), length(0x%x)]\n",
1181 ntohs(tlvh->type), ntohs(tlvh->length));
1182 else
1183 zlog_debug(" Unknown TLV: [type(0x%x), length(0x%x)]",
1184 ntohs(tlvh->type), ntohs(tlvh->length));
1185
1186 return TLV_SIZE(tlvh);
1187 }
1188
1189 static uint16_t show_vty_pce_info(struct vty *vty, struct tlv_header *ri,
1190 uint32_t total)
1191 {
1192 struct tlv_header *tlvh;
1193 uint16_t sum = 0;
1194
1195 for (tlvh = ri; sum < total; tlvh = TLV_HDR_NEXT(tlvh)) {
1196 switch (ntohs(tlvh->type)) {
1197 case RI_PCE_SUBTLV_ADDRESS:
1198 sum += show_vty_pce_subtlv_address(vty, tlvh);
1199 break;
1200 case RI_PCE_SUBTLV_PATH_SCOPE:
1201 sum += show_vty_pce_subtlv_path_scope(vty, tlvh);
1202 break;
1203 case RI_PCE_SUBTLV_DOMAIN:
1204 sum += show_vty_pce_subtlv_domain(vty, tlvh);
1205 break;
1206 case RI_PCE_SUBTLV_NEIGHBOR:
1207 sum += show_vty_pce_subtlv_neighbor(vty, tlvh);
1208 break;
1209 case RI_PCE_SUBTLV_CAP_FLAG:
1210 sum += show_vty_pce_subtlv_cap_flag(vty, tlvh);
1211 break;
1212 default:
1213 sum += show_vty_unknown_tlv(vty, tlvh);
1214 break;
1215 }
1216 }
1217 return sum;
1218 }
1219
1220 /* Display Segment Routing Algorithm TLV information */
1221 static uint16_t show_vty_sr_algorithm(struct vty *vty, struct tlv_header *tlvh)
1222 {
1223 struct ri_sr_tlv_sr_algorithm *algo =
1224 (struct ri_sr_tlv_sr_algorithm *)tlvh;
1225 int i;
1226
1227 if (vty != NULL) {
1228 vty_out(vty, " Segment Routing Algorithm TLV:\n");
1229 for (i = 0; i < ntohs(algo->header.length); i++) {
1230 switch (algo->value[i]) {
1231 case 0:
1232 vty_out(vty, " Algorithm %d: SPF\n", i);
1233 break;
1234 case 1:
1235 vty_out(vty, " Algorithm %d: Strict SPF\n",
1236 i);
1237 break;
1238 default:
1239 vty_out(vty,
1240 " Algorithm %d: Unknown value %d\n", i,
1241 algo->value[i]);
1242 break;
1243 }
1244 }
1245 }
1246
1247 else {
1248 zlog_debug(" Segment Routing Algorithm TLV:\n");
1249 for (i = 0; i < ntohs(algo->header.length); i++)
1250 switch (algo->value[i]) {
1251 case 0:
1252 zlog_debug(" Algorithm %d: SPF\n", i);
1253 break;
1254 case 1:
1255 zlog_debug(" Algorithm %d: Strict SPF\n", i);
1256 break;
1257 default:
1258 zlog_debug(
1259 " Algorithm %d: Unknown value %d\n",
1260 i, algo->value[i]);
1261 break;
1262 }
1263 }
1264
1265 return TLV_SIZE(tlvh);
1266 }
1267
1268 /* Display Segment Routing SID/Label Range TLV information */
1269 static uint16_t show_vty_sr_range(struct vty *vty, struct tlv_header *tlvh)
1270 {
1271 struct ri_sr_tlv_sid_label_range *range =
1272 (struct ri_sr_tlv_sid_label_range *)tlvh;
1273
1274 if (vty != NULL) {
1275 vty_out(vty,
1276 " Segment Routing Range TLV:\n"
1277 " Range Size = %d\n"
1278 " SID Label = %d\n\n",
1279 GET_RANGE_SIZE(ntohl(range->size)),
1280 GET_LABEL(ntohl(range->lower.value)));
1281 } else {
1282 zlog_debug(
1283 " Segment Routing Range TLV:\n"
1284 " Range Size = %d\n"
1285 " SID Label = %d\n\n",
1286 GET_RANGE_SIZE(ntohl(range->size)),
1287 GET_LABEL(ntohl(range->lower.value)));
1288 }
1289
1290 return TLV_SIZE(tlvh);
1291 }
1292
1293 /* Display Segment Routing Maximum Stack Depth TLV information */
1294 static uint16_t show_vty_sr_msd(struct vty *vty, struct tlv_header *tlvh)
1295 {
1296 struct ri_sr_tlv_node_msd *msd = (struct ri_sr_tlv_node_msd *)tlvh;
1297
1298 if (vty != NULL) {
1299 vty_out(vty,
1300 " Segment Routing MSD TLV:\n"
1301 " Node Maximum Stack Depth = %d\n",
1302 msd->value);
1303 } else {
1304 zlog_debug(
1305 " Segment Routing MSD TLV:\n"
1306 " Node Maximum Stack Depth = %d\n",
1307 msd->value);
1308 }
1309
1310 return TLV_SIZE(tlvh);
1311 }
1312
1313 static void ospf_router_info_show_info(struct vty *vty, struct ospf_lsa *lsa)
1314 {
1315 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1316 struct tlv_header *tlvh;
1317 uint16_t length = 0, sum = 0;
1318
1319 /* Initialize TLV browsing */
1320 length = ntohs(lsah->length) - OSPF_LSA_HEADER_SIZE;
1321
1322 for (tlvh = TLV_HDR_TOP(lsah); sum < length;
1323 tlvh = TLV_HDR_NEXT(tlvh)) {
1324 switch (ntohs(tlvh->type)) {
1325 case RI_TLV_CAPABILITIES:
1326 sum += show_vty_router_cap(vty, tlvh);
1327 break;
1328 case RI_TLV_PCE:
1329 tlvh++;
1330 sum += TLV_HDR_SIZE;
1331 sum += show_vty_pce_info(vty, tlvh, length - sum);
1332 break;
1333 case RI_SR_TLV_SR_ALGORITHM:
1334 sum += show_vty_sr_algorithm(vty, tlvh);
1335 break;
1336 case RI_SR_TLV_SID_LABEL_RANGE:
1337 sum += show_vty_sr_range(vty, tlvh);
1338 break;
1339 case RI_SR_TLV_NODE_MSD:
1340 sum += show_vty_sr_msd(vty, tlvh);
1341 break;
1342
1343 default:
1344 sum += show_vty_unknown_tlv(vty, tlvh);
1345 break;
1346 }
1347 }
1348
1349 return;
1350 }
1351
1352 static void ospf_router_info_config_write_router(struct vty *vty)
1353 {
1354 struct ospf_pce_info *pce = &OspfRI.pce_info;
1355 struct listnode *node;
1356 struct ri_pce_subtlv_domain *domain;
1357 struct ri_pce_subtlv_neighbor *neighbor;
1358 struct in_addr tmp;
1359
1360 if (!OspfRI.enabled)
1361 return;
1362
1363 if (OspfRI.scope == OSPF_OPAQUE_AS_LSA)
1364 vty_out(vty, " router-info as\n");
1365 else
1366 vty_out(vty, " router-info area %s\n",
1367 inet_ntoa(OspfRI.area_id));
1368
1369 if (OspfRI.pce_info.enabled) {
1370
1371 if (pce->pce_address.header.type != 0)
1372 vty_out(vty, " pce address %s\n",
1373 inet_ntoa(pce->pce_address.address.value));
1374
1375 if (pce->pce_cap_flag.header.type != 0)
1376 vty_out(vty, " pce flag 0x%x\n",
1377 ntohl(pce->pce_cap_flag.value));
1378
1379 for (ALL_LIST_ELEMENTS_RO(pce->pce_domain, node, domain)) {
1380 if (domain->header.type != 0) {
1381 if (domain->type == PCE_DOMAIN_TYPE_AREA) {
1382 tmp.s_addr = domain->value;
1383 vty_out(vty, " pce domain area %s\n",
1384 inet_ntoa(tmp));
1385 } else {
1386 vty_out(vty, " pce domain as %d\n",
1387 ntohl(domain->value));
1388 }
1389 }
1390 }
1391
1392 for (ALL_LIST_ELEMENTS_RO(pce->pce_neighbor, node, neighbor)) {
1393 if (neighbor->header.type != 0) {
1394 if (neighbor->type == PCE_DOMAIN_TYPE_AREA) {
1395 tmp.s_addr = neighbor->value;
1396 vty_out(vty, " pce neighbor area %s\n",
1397 inet_ntoa(tmp));
1398 } else {
1399 vty_out(vty, " pce neighbor as %d\n",
1400 ntohl(neighbor->value));
1401 }
1402 }
1403 }
1404
1405 if (pce->pce_scope.header.type != 0)
1406 vty_out(vty, " pce scope 0x%x\n",
1407 ntohl(OspfRI.pce_info.pce_scope.value));
1408 }
1409 return;
1410 }
1411
1412 /*------------------------------------------------------------------------*
1413 * Followings are vty command functions.
1414 *------------------------------------------------------------------------*/
1415
1416 DEFUN (router_info,
1417 router_info_area_cmd,
1418 "router-info <as|area A.B.C.D>",
1419 OSPF_RI_STR
1420 "Enable the Router Information functionality with AS flooding scope\n"
1421 "Enable the Router Information functionality with Area flooding scope\n"
1422 "OSPF area ID in IP format\n")
1423 {
1424 int idx_ipv4 = 2;
1425 char *area = (argc == 3) ? argv[idx_ipv4]->arg : NULL;
1426
1427 uint8_t scope;
1428
1429 if (OspfRI.enabled)
1430 return CMD_SUCCESS;
1431
1432 /* Check and get Area value if present */
1433 if (area) {
1434 if (!inet_aton(area, &OspfRI.area_id)) {
1435 vty_out(vty, "%% specified Area ID %s is invalid\n",
1436 area);
1437 return CMD_WARNING_CONFIG_FAILED;
1438 }
1439 scope = OSPF_OPAQUE_AREA_LSA;
1440 } else {
1441 OspfRI.area_id.s_addr = 0;
1442 scope = OSPF_OPAQUE_AS_LSA;
1443 }
1444
1445 /* First start to register Router Information callbacks */
1446 if ((ospf_router_info_register(scope)) != 0) {
1447 vty_out(vty,
1448 "%% Unable to register Router Information callbacks.");
1449 flog_err(
1450 OSPF_ERR_INIT_FAIL,
1451 "Unable to register Router Information callbacks. Abort!");
1452 return CMD_WARNING_CONFIG_FAILED;
1453 }
1454
1455 OspfRI.enabled = true;
1456
1457 if (IS_DEBUG_OSPF_EVENT)
1458 zlog_debug("RI-> Router Information (%s flooding): OFF -> ON",
1459 OspfRI.scope == OSPF_OPAQUE_AREA_LSA ? "Area"
1460 : "AS");
1461
1462 /*
1463 * Following code is intended to handle two cases;
1464 *
1465 * 1) Router Information was disabled at startup time, but now become
1466 * enabled.
1467 * 2) Router Information was once enabled then disabled, and now enabled
1468 * again.
1469 */
1470
1471 initialize_params(&OspfRI);
1472
1473 /* Refresh RI LSA if already engaged */
1474 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED)) {
1475 zlog_debug("RI-> Refresh LSA following configuration");
1476 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1477 } else {
1478 zlog_debug("RI-> Initial origination following configuration");
1479 ospf_router_info_lsa_schedule(REORIGINATE_THIS_LSA);
1480 }
1481 return CMD_SUCCESS;
1482 }
1483
1484
1485 DEFUN (no_router_info,
1486 no_router_info_cmd,
1487 "no router-info",
1488 NO_STR
1489 "Disable the Router Information functionality\n")
1490 {
1491
1492 if (!OspfRI.enabled)
1493 return CMD_SUCCESS;
1494
1495 if (IS_DEBUG_OSPF_EVENT)
1496 zlog_debug("RI-> Router Information: ON -> OFF");
1497
1498 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1499 ospf_router_info_lsa_schedule(FLUSH_THIS_LSA);
1500
1501 OspfRI.enabled = false;
1502
1503 return CMD_SUCCESS;
1504 }
1505
1506 static int ospf_ri_enabled(struct vty *vty)
1507 {
1508 if (OspfRI.enabled)
1509 return 1;
1510
1511 if (vty)
1512 vty_out(vty, "%% OSPF RI is not turned on\n");
1513
1514 return 0;
1515 }
1516
1517 DEFUN (pce_address,
1518 pce_address_cmd,
1519 "pce address A.B.C.D",
1520 PCE_STR
1521 "Stable IP address of the PCE\n"
1522 "PCE address in IPv4 address format\n")
1523 {
1524 int idx_ipv4 = 2;
1525 struct in_addr value;
1526 struct ospf_pce_info *pi = &OspfRI.pce_info;
1527
1528 if (!ospf_ri_enabled(vty))
1529 return CMD_WARNING_CONFIG_FAILED;
1530
1531 if (!inet_aton(argv[idx_ipv4]->arg, &value)) {
1532 vty_out(vty, "Please specify PCE Address by A.B.C.D\n");
1533 return CMD_WARNING_CONFIG_FAILED;
1534 }
1535
1536 if (ntohs(pi->pce_address.header.type) == 0
1537 || ntohl(pi->pce_address.address.value.s_addr)
1538 != ntohl(value.s_addr)) {
1539
1540 set_pce_address(value, pi);
1541
1542 /* Refresh RI LSA if already engaged */
1543 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1544 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1545 }
1546
1547 return CMD_SUCCESS;
1548 }
1549
1550 DEFUN (no_pce_address,
1551 no_pce_address_cmd,
1552 "no pce address [A.B.C.D]",
1553 NO_STR
1554 PCE_STR
1555 "Disable PCE address\n"
1556 "PCE address in IPv4 address format\n")
1557 {
1558
1559 unset_param(&OspfRI.pce_info.pce_address);
1560
1561 /* Refresh RI LSA if already engaged */
1562 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1563 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1564
1565 return CMD_SUCCESS;
1566 }
1567
1568 DEFUN (pce_path_scope,
1569 pce_path_scope_cmd,
1570 "pce scope BITPATTERN",
1571 PCE_STR
1572 "Path scope visibilities of the PCE for path computation\n"
1573 "32-bit Hexadecimal value\n")
1574 {
1575 int idx_bitpattern = 2;
1576 uint32_t scope;
1577 struct ospf_pce_info *pi = &OspfRI.pce_info;
1578
1579 if (!ospf_ri_enabled(vty))
1580 return CMD_WARNING_CONFIG_FAILED;
1581
1582 if (sscanf(argv[idx_bitpattern]->arg, "0x%x", &scope) != 1) {
1583 vty_out(vty, "pce_path_scope: fscanf: %s\n",
1584 safe_strerror(errno));
1585 return CMD_WARNING_CONFIG_FAILED;
1586 }
1587
1588 if (ntohl(pi->pce_scope.header.type) == 0
1589 || scope != pi->pce_scope.value) {
1590 set_pce_path_scope(scope, pi);
1591
1592 /* Refresh RI LSA if already engaged */
1593 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1594 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1595 }
1596
1597 return CMD_SUCCESS;
1598 }
1599
1600 DEFUN (no_pce_path_scope,
1601 no_pce_path_scope_cmd,
1602 "no pce scope [BITPATTERN]",
1603 NO_STR
1604 PCE_STR
1605 "Disable PCE path scope\n"
1606 "32-bit Hexadecimal value\n")
1607 {
1608
1609 unset_param(&OspfRI.pce_info.pce_address);
1610
1611 /* Refresh RI LSA if already engaged */
1612 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1613 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1614
1615 return CMD_SUCCESS;
1616 }
1617
1618 DEFUN (pce_domain,
1619 pce_domain_cmd,
1620 "pce domain as (0-65535)",
1621 PCE_STR
1622 "Configure PCE domain AS number\n"
1623 "AS number where the PCE as visibilities for path computation\n"
1624 "AS number in decimal <0-65535>\n")
1625 {
1626 int idx_number = 3;
1627
1628 uint32_t as;
1629 struct ospf_pce_info *pce = &OspfRI.pce_info;
1630 struct listnode *node;
1631 struct ri_pce_subtlv_domain *domain;
1632
1633 if (!ospf_ri_enabled(vty))
1634 return CMD_WARNING_CONFIG_FAILED;
1635
1636 if (sscanf(argv[idx_number]->arg, "%" SCNu32, &as) != 1) {
1637 vty_out(vty, "pce_domain: fscanf: %s\n", safe_strerror(errno));
1638 return CMD_WARNING_CONFIG_FAILED;
1639 }
1640
1641 /* Check if the domain is not already in the domain list */
1642 for (ALL_LIST_ELEMENTS_RO(pce->pce_domain, node, domain)) {
1643 if (ntohl(domain->header.type) == 0 && as == domain->value)
1644 return CMD_SUCCESS;
1645 }
1646
1647 /* Create new domain if not found */
1648 set_pce_domain(PCE_DOMAIN_TYPE_AS, as, pce);
1649
1650 /* Refresh RI LSA if already engaged */
1651 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1652 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1653
1654 return CMD_SUCCESS;
1655 }
1656
1657 DEFUN (no_pce_domain,
1658 no_pce_domain_cmd,
1659 "no pce domain as (0-65535)",
1660 NO_STR
1661 PCE_STR
1662 "Disable PCE domain AS number\n"
1663 "AS number where the PCE as visibilities for path computation\n"
1664 "AS number in decimal <0-65535>\n")
1665 {
1666 int idx_number = 4;
1667
1668 uint32_t as;
1669 struct ospf_pce_info *pce = &OspfRI.pce_info;
1670
1671 if (sscanf(argv[idx_number]->arg, "%" SCNu32, &as) != 1) {
1672 vty_out(vty, "no_pce_domain: fscanf: %s\n",
1673 safe_strerror(errno));
1674 return CMD_WARNING_CONFIG_FAILED;
1675 }
1676
1677 /* Unset corresponding PCE domain */
1678 unset_pce_domain(PCE_DOMAIN_TYPE_AS, as, pce);
1679
1680 /* Refresh RI LSA if already engaged */
1681 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1682 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1683
1684 return CMD_SUCCESS;
1685 }
1686
1687 DEFUN (pce_neigbhor,
1688 pce_neighbor_cmd,
1689 "pce neighbor as (0-65535)",
1690 PCE_STR
1691 "Configure PCE neighbor domain AS number\n"
1692 "AS number of PCE neighbors\n"
1693 "AS number in decimal <0-65535>\n")
1694 {
1695 int idx_number = 3;
1696
1697 uint32_t as;
1698 struct ospf_pce_info *pce = &OspfRI.pce_info;
1699 struct listnode *node;
1700 struct ri_pce_subtlv_neighbor *neighbor;
1701
1702 if (!ospf_ri_enabled(vty))
1703 return CMD_WARNING_CONFIG_FAILED;
1704
1705 if (sscanf(argv[idx_number]->arg, "%" SCNu32, &as) != 1) {
1706 vty_out(vty, "pce_neighbor: fscanf: %s\n",
1707 safe_strerror(errno));
1708 return CMD_WARNING_CONFIG_FAILED;
1709 }
1710
1711 /* Check if the domain is not already in the domain list */
1712 for (ALL_LIST_ELEMENTS_RO(pce->pce_neighbor, node, neighbor)) {
1713 if (ntohl(neighbor->header.type) == 0 && as == neighbor->value)
1714 return CMD_SUCCESS;
1715 }
1716
1717 /* Create new domain if not found */
1718 set_pce_neighbor(PCE_DOMAIN_TYPE_AS, as, pce);
1719
1720 /* Refresh RI LSA if already engaged */
1721 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1722 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1723
1724 return CMD_SUCCESS;
1725 }
1726
1727 DEFUN (no_pce_neighbor,
1728 no_pce_neighbor_cmd,
1729 "no pce neighbor as (0-65535)",
1730 NO_STR
1731 PCE_STR
1732 "Disable PCE neighbor AS number\n"
1733 "AS number of PCE neighbor\n"
1734 "AS number in decimal <0-65535>\n")
1735 {
1736 int idx_number = 4;
1737
1738 uint32_t as;
1739 struct ospf_pce_info *pce = &OspfRI.pce_info;
1740
1741 if (sscanf(argv[idx_number]->arg, "%" SCNu32, &as) != 1) {
1742 vty_out(vty, "no_pce_neighbor: fscanf: %s\n",
1743 safe_strerror(errno));
1744 return CMD_WARNING_CONFIG_FAILED;
1745 }
1746
1747 /* Unset corresponding PCE domain */
1748 unset_pce_neighbor(PCE_DOMAIN_TYPE_AS, as, pce);
1749
1750 /* Refresh RI LSA if already engaged */
1751 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1752 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1753
1754 return CMD_SUCCESS;
1755 }
1756
1757 DEFUN (pce_cap_flag,
1758 pce_cap_flag_cmd,
1759 "pce flag BITPATTERN",
1760 PCE_STR
1761 "Capabilities of the PCE for path computation\n"
1762 "32-bit Hexadecimal value\n")
1763 {
1764 int idx_bitpattern = 2;
1765
1766 uint32_t cap;
1767 struct ospf_pce_info *pce = &OspfRI.pce_info;
1768
1769 if (!ospf_ri_enabled(vty))
1770 return CMD_WARNING_CONFIG_FAILED;
1771
1772 if (sscanf(argv[idx_bitpattern]->arg, "0x%x", &cap) != 1) {
1773 vty_out(vty, "pce_cap_flag: fscanf: %s\n",
1774 safe_strerror(errno));
1775 return CMD_WARNING_CONFIG_FAILED;
1776 }
1777
1778 if (ntohl(pce->pce_cap_flag.header.type) == 0
1779 || cap != pce->pce_cap_flag.value) {
1780 set_pce_cap_flag(cap, pce);
1781
1782 /* Refresh RI LSA if already engaged */
1783 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1784 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1785 }
1786
1787 return CMD_SUCCESS;
1788 }
1789
1790 DEFUN (no_pce_cap_flag,
1791 no_pce_cap_flag_cmd,
1792 "no pce flag",
1793 NO_STR
1794 PCE_STR
1795 "Disable PCE capabilities\n")
1796 {
1797
1798 unset_param(&OspfRI.pce_info.pce_cap_flag);
1799
1800 /* Refresh RI LSA if already engaged */
1801 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1802 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1803
1804 return CMD_SUCCESS;
1805 }
1806
1807 DEFUN (show_ip_ospf_router_info,
1808 show_ip_ospf_router_info_cmd,
1809 "show ip ospf router-info",
1810 SHOW_STR
1811 IP_STR
1812 OSPF_STR
1813 "Router Information\n")
1814 {
1815
1816 if (OspfRI.enabled) {
1817 vty_out(vty, "--- Router Information parameters ---\n");
1818 show_vty_router_cap(vty, &OspfRI.router_cap.header);
1819 } else {
1820 if (vty != NULL)
1821 vty_out(vty,
1822 " Router Information is disabled on this router\n");
1823 }
1824 return CMD_SUCCESS;
1825 }
1826
1827 DEFUN (show_ip_opsf_router_info_pce,
1828 show_ip_ospf_router_info_pce_cmd,
1829 "show ip ospf router-info pce",
1830 SHOW_STR
1831 IP_STR
1832 OSPF_STR
1833 "Router Information\n"
1834 "PCE information\n")
1835 {
1836
1837 struct ospf_pce_info *pce = &OspfRI.pce_info;
1838 struct listnode *node;
1839 struct ri_pce_subtlv_domain *domain;
1840 struct ri_pce_subtlv_neighbor *neighbor;
1841
1842 if ((OspfRI.enabled) && (OspfRI.pce_info.enabled)) {
1843 vty_out(vty, "--- PCE parameters ---\n");
1844
1845 if (pce->pce_address.header.type != 0)
1846 show_vty_pce_subtlv_address(vty,
1847 &pce->pce_address.header);
1848
1849 if (pce->pce_scope.header.type != 0)
1850 show_vty_pce_subtlv_path_scope(vty,
1851 &pce->pce_scope.header);
1852
1853 for (ALL_LIST_ELEMENTS_RO(pce->pce_domain, node, domain)) {
1854 if (domain->header.type != 0)
1855 show_vty_pce_subtlv_domain(vty,
1856 &domain->header);
1857 }
1858
1859 for (ALL_LIST_ELEMENTS_RO(pce->pce_neighbor, node, neighbor)) {
1860 if (neighbor->header.type != 0)
1861 show_vty_pce_subtlv_neighbor(vty,
1862 &neighbor->header);
1863 }
1864
1865 if (pce->pce_cap_flag.header.type != 0)
1866 show_vty_pce_subtlv_cap_flag(vty,
1867 &pce->pce_cap_flag.header);
1868
1869 } else {
1870 vty_out(vty, " PCE info is disabled on this router\n");
1871 }
1872
1873 return CMD_SUCCESS;
1874 }
1875
1876 /* Install new CLI commands */
1877 static void ospf_router_info_register_vty(void)
1878 {
1879 install_element(VIEW_NODE, &show_ip_ospf_router_info_cmd);
1880 install_element(VIEW_NODE, &show_ip_ospf_router_info_pce_cmd);
1881
1882 install_element(OSPF_NODE, &router_info_area_cmd);
1883 install_element(OSPF_NODE, &no_router_info_cmd);
1884 install_element(OSPF_NODE, &pce_address_cmd);
1885 install_element(OSPF_NODE, &no_pce_address_cmd);
1886 install_element(OSPF_NODE, &pce_path_scope_cmd);
1887 install_element(OSPF_NODE, &no_pce_path_scope_cmd);
1888 install_element(OSPF_NODE, &pce_domain_cmd);
1889 install_element(OSPF_NODE, &no_pce_domain_cmd);
1890 install_element(OSPF_NODE, &pce_neighbor_cmd);
1891 install_element(OSPF_NODE, &no_pce_neighbor_cmd);
1892 install_element(OSPF_NODE, &pce_cap_flag_cmd);
1893 install_element(OSPF_NODE, &no_pce_cap_flag_cmd);
1894
1895 return;
1896 }