]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_ri.c
Merge pull request #2818 from kssoman/rmap_fix
[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
61 /* Store Router Information PCE TLV and SubTLV in network byte order. */
62 struct ospf_pce_info {
63 bool enabled;
64 struct ri_tlv_pce pce_header;
65 struct ri_pce_subtlv_address pce_address;
66 struct ri_pce_subtlv_path_scope pce_scope;
67 struct list *pce_domain;
68 struct list *pce_neighbor;
69 struct ri_pce_subtlv_cap_flag pce_cap_flag;
70 };
71
72 /*
73 * Store Router Information Segment Routing TLV and SubTLV
74 * in network byte order
75 */
76 struct ospf_ri_sr_info {
77 bool enabled;
78 /* Algorithms supported by the node */
79 struct ri_sr_tlv_sr_algorithm algo;
80 /*
81 * Segment Routing Global Block i.e. label range
82 * Only one range supported in this code
83 */
84 struct ri_sr_tlv_sid_label_range range;
85 /* Maximum SID Depth supported by the node */
86 struct ri_sr_tlv_node_msd msd;
87 };
88
89 /* Following structure are internal use only. */
90 struct ospf_router_info {
91 bool enabled;
92
93 uint8_t registered;
94 uint8_t scope;
95
96 /* Flags to manage this router information. */
97 #define RIFLG_LSA_ENGAGED 0x1
98 #define RIFLG_LSA_FORCED_REFRESH 0x2
99 uint32_t flags;
100
101 /* area pointer if flooding is Type 10 Null if flooding is AS scope */
102 struct ospf_area *area;
103 struct in_addr area_id;
104
105 /* Store Router Information Capabilities LSA */
106 struct ri_tlv_router_cap router_cap;
107
108 /* Store PCE capability LSA */
109 struct ospf_pce_info pce_info;
110
111 /* Store SR capability LSA */
112 struct ospf_ri_sr_info sr_info;
113 };
114
115 /*
116 * Global variable to manage Opaque-LSA/Router Information on this node.
117 * Note that all parameter values are stored in network byte order.
118 */
119 static struct ospf_router_info OspfRI;
120
121 /*------------------------------------------------------------------------------*
122 * Followings are initialize/terminate functions for Router Information
123 *handling.
124 *------------------------------------------------------------------------------*/
125
126 static void ospf_router_info_ism_change(struct ospf_interface *oi,
127 int old_status);
128 static void ospf_router_info_nsm_change(struct ospf_neighbor *nbr,
129 int old_status);
130 static void ospf_router_info_config_write_router(struct vty *vty);
131 static void ospf_router_info_show_info(struct vty *vty, struct ospf_lsa *lsa);
132 static int ospf_router_info_lsa_originate(void *arg);
133 static struct ospf_lsa *ospf_router_info_lsa_refresh(struct ospf_lsa *lsa);
134 static void ospf_router_info_lsa_schedule(enum lsa_opcode opcode);
135 static void ospf_router_info_register_vty(void);
136 static int ospf_router_info_lsa_update(struct ospf_lsa *lsa);
137 static void del_pce_info(void *val);
138
139 int ospf_router_info_init(void)
140 {
141
142 zlog_info("RI -> Initialize Router Information");
143
144 memset(&OspfRI, 0, sizeof(struct ospf_router_info));
145 OspfRI.enabled = false;
146 OspfRI.registered = 0;
147 OspfRI.scope = OSPF_OPAQUE_AS_LSA;
148 OspfRI.area_id.s_addr = 0;
149 OspfRI.flags = 0;
150
151 /* Initialize pce domain and neighbor list */
152 OspfRI.pce_info.enabled = false;
153 OspfRI.pce_info.pce_domain = list_new();
154 OspfRI.pce_info.pce_domain->del = del_pce_info;
155 OspfRI.pce_info.pce_neighbor = list_new();
156 OspfRI.pce_info.pce_neighbor->del = del_pce_info;
157
158 /* Initialize Segment Routing information structure */
159 OspfRI.sr_info.enabled = false;
160
161 ospf_router_info_register_vty();
162
163 return 0;
164 }
165
166 static int ospf_router_info_register(uint8_t scope)
167 {
168 int rc = 0;
169
170 if (OspfRI.registered)
171 return rc;
172
173 zlog_info("RI -> Register Router Information with scope %s(%d)",
174 scope == OSPF_OPAQUE_AREA_LSA ? "Area" : "AS", scope);
175 rc = ospf_register_opaque_functab(
176 scope, OPAQUE_TYPE_ROUTER_INFORMATION_LSA,
177 NULL, /* new interface */
178 NULL, /* del interface */
179 ospf_router_info_ism_change, ospf_router_info_nsm_change,
180 ospf_router_info_config_write_router,
181 NULL, /* Config. write interface */
182 NULL, /* Config. write debug */
183 ospf_router_info_show_info, ospf_router_info_lsa_originate,
184 ospf_router_info_lsa_refresh, ospf_router_info_lsa_update,
185 NULL); /* del_lsa_hook */
186
187 if (rc != 0) {
188 zlog_warn(
189 "ospf_router_info_init: Failed to register functions");
190 return rc;
191 }
192
193 OspfRI.registered = 1;
194 OspfRI.scope = scope;
195 return rc;
196 }
197
198 static int ospf_router_info_unregister()
199 {
200
201 if ((OspfRI.scope != OSPF_OPAQUE_AS_LSA)
202 && (OspfRI.scope != OSPF_OPAQUE_AREA_LSA)) {
203 zlog_warn(
204 "Unable to unregister Router Info functions: Wrong scope!");
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 if ((s = stream_new(OSPF_MAX_LSA_SIZE)) == NULL) {
746 zlog_warn("ospf_router_info_lsa_new: stream_new() ?");
747 return NULL;
748 }
749 lsah = (struct lsa_header *)STREAM_DATA(s);
750
751 options = OSPF_OPTION_E; /* Enable AS external as we flood RI with
752 Opaque Type 11 */
753 options |= OSPF_OPTION_O; /* Don't forget this :-) */
754
755 lsa_type = OspfRI.scope;
756 /* LSA ID == 0 for Router Information see RFC 4970 */
757 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_ROUTER_INFORMATION_LSA, 0);
758 lsa_id.s_addr = htonl(tmp);
759
760 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
761 zlog_debug(
762 "LSA[Type%d:%s]: Create an Opaque-LSA/ROUTER INFORMATION instance",
763 lsa_type, inet_ntoa(lsa_id));
764
765 top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
766
767 /* Set opaque-LSA header fields. */
768 lsa_header_set(s, options, lsa_type, lsa_id, top->router_id);
769
770 /* Set opaque-LSA body fields. */
771 ospf_router_info_lsa_body_set(s);
772
773 /* Set length. */
774 length = stream_get_endp(s);
775 lsah->length = htons(length);
776
777 /* Now, create an OSPF LSA instance. */
778 new = ospf_lsa_new_and_data(length);
779
780 new->area = OspfRI.area; /* Area must be null if the Opaque type is AS
781 scope, fulfill otherwise */
782
783 if (new->area && new->area->ospf)
784 new->vrf_id = new->area->ospf->vrf_id;
785 else
786 new->vrf_id = VRF_DEFAULT;
787
788 SET_FLAG(new->flags, OSPF_LSA_SELF);
789 memcpy(new->data, lsah, length);
790 stream_free(s);
791
792 return new;
793 }
794
795 static int ospf_router_info_lsa_originate1(void *arg)
796 {
797 struct ospf_lsa *new;
798 struct ospf *top;
799 struct ospf_area *area;
800 int rc = -1;
801 vrf_id_t vrf_id = VRF_DEFAULT;
802
803 /* First check if the area is known if flooding scope is Area */
804 if (OspfRI.scope == OSPF_OPAQUE_AREA_LSA) {
805 area = (struct ospf_area *)arg;
806 if (area->area_id.s_addr != OspfRI.area_id.s_addr) {
807 zlog_debug(
808 "RI -> This is not the Router Information Area. Stop processing");
809 return rc;
810 }
811 OspfRI.area = area;
812 if (area->ospf)
813 vrf_id = area->ospf->vrf_id;
814 }
815
816 /* Create new Opaque-LSA/ROUTER INFORMATION instance. */
817 if ((new = ospf_router_info_lsa_new()) == NULL) {
818 zlog_warn(
819 "ospf_router_info_lsa_originate1: ospf_router_info_lsa_new() ?");
820 return rc;
821 }
822 new->vrf_id = vrf_id;
823
824 /* Get ospf info */
825 top = ospf_lookup_by_vrf_id(vrf_id);
826 if (top == NULL) {
827 zlog_debug("%s: ospf instance not found for vrf id %u",
828 __PRETTY_FUNCTION__, vrf_id);
829 ospf_lsa_unlock(&new);
830 return rc;
831 }
832
833 /* Install this LSA into LSDB. */
834 if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
835 zlog_warn(
836 "ospf_router_info_lsa_originate1: ospf_lsa_install() ?");
837 ospf_lsa_unlock(&new);
838 return rc;
839 }
840
841 /* Now this Router Info parameter entry has associated LSA. */
842 SET_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED);
843
844 /* Update new LSA origination count. */
845 top->lsa_originate_count++;
846
847 /* Flood new LSA through AS. */
848 if (OspfRI.scope == OSPF_OPAQUE_AS_LSA)
849 ospf_flood_through_as(top, NULL /*nbr */, new);
850 else
851 ospf_flood_through_area(OspfRI.area, NULL /*nbr */, new);
852
853 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
854 zlog_debug(
855 "LSA[Type%d:%s]: Originate Opaque-LSA/ROUTER INFORMATION",
856 new->data->type, inet_ntoa(new->data->id));
857 ospf_lsa_header_dump(new->data);
858 }
859
860 rc = 0;
861 return rc;
862 }
863
864 static int ospf_router_info_lsa_originate(void *arg)
865 {
866
867 int rc = -1;
868
869 if (!OspfRI.enabled) {
870 zlog_info(
871 "ospf_router_info_lsa_originate: ROUTER INFORMATION is disabled now.");
872 rc = 0; /* This is not an error case. */
873 return rc;
874 }
875
876 /* Check if Router Information LSA is already engaged */
877 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED)) {
878 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_FORCED_REFRESH)) {
879 UNSET_FLAG(OspfRI.flags, RIFLG_LSA_FORCED_REFRESH);
880 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
881 }
882 } else {
883 if (!is_mandated_params_set(OspfRI))
884 zlog_warn(
885 "ospf_router_info_lsa_originate: lacks mandated ROUTER INFORMATION parameters");
886
887 /* Ok, let's try to originate an LSA */
888 if (ospf_router_info_lsa_originate1(arg) != 0)
889 return rc;
890 }
891
892 rc = 0;
893 return rc;
894 }
895
896 static struct ospf_lsa *ospf_router_info_lsa_refresh(struct ospf_lsa *lsa)
897 {
898 struct ospf_lsa *new = NULL;
899 struct ospf *top;
900
901 if (!OspfRI.enabled) {
902 /*
903 * This LSA must have flushed before due to ROUTER INFORMATION
904 * status change.
905 * It seems a slip among routers in the routing domain.
906 */
907 zlog_info(
908 "ospf_router_info_lsa_refresh: ROUTER INFORMATION is disabled now.");
909 lsa->data->ls_age =
910 htons(OSPF_LSA_MAXAGE); /* Flush it anyway. */
911 }
912
913 /* Verify that the Router Information ID is supported */
914 if (GET_OPAQUE_ID(ntohl(lsa->data->id.s_addr)) != 0) {
915 zlog_warn(
916 "ospf_router_info_lsa_refresh: Unsupported Router Information ID");
917 return NULL;
918 }
919
920 /* If the lsa's age reached to MaxAge, start flushing procedure. */
921 if (IS_LSA_MAXAGE(lsa)) {
922 UNSET_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED);
923 ospf_opaque_lsa_flush_schedule(lsa);
924 return NULL;
925 }
926
927 /* Create new Opaque-LSA/ROUTER INFORMATION instance. */
928 if ((new = ospf_router_info_lsa_new()) == NULL) {
929 zlog_warn(
930 "ospf_router_info_lsa_refresh: ospf_router_info_lsa_new() ?");
931 return NULL;
932 }
933 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
934 new->vrf_id = lsa->vrf_id;
935
936 /* Install this LSA into LSDB. */
937 /* Given "lsa" will be freed in the next function. */
938 top = ospf_lookup_by_vrf_id(lsa->vrf_id);
939 if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
940 zlog_warn("ospf_router_info_lsa_refresh: ospf_lsa_install() ?");
941 ospf_lsa_unlock(&new);
942 return new;
943 }
944
945 /* Flood updated LSA through AS or AREA depending of OspfRI.scope. */
946 if (OspfRI.scope == OSPF_OPAQUE_AS_LSA)
947 ospf_flood_through_as(top, NULL /*nbr */, new);
948 else
949 ospf_flood_through_area(OspfRI.area, NULL /*nbr */, new);
950
951 /* Debug logging. */
952 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
953 zlog_debug(
954 "LSA[Type%d:%s]: Refresh Opaque-LSA/ROUTER INFORMATION",
955 new->data->type, inet_ntoa(new->data->id));
956 ospf_lsa_header_dump(new->data);
957 }
958
959 return new;
960 }
961
962 static void ospf_router_info_lsa_schedule(enum lsa_opcode opcode)
963 {
964 struct ospf_lsa lsa;
965 struct lsa_header lsah;
966 struct ospf *top;
967 uint32_t tmp;
968
969 memset(&lsa, 0, sizeof(lsa));
970 memset(&lsah, 0, sizeof(lsah));
971
972 zlog_debug("RI-> LSA schedule %s%s%s",
973 opcode == REORIGINATE_THIS_LSA ? "Re-Originate" : "",
974 opcode == REFRESH_THIS_LSA ? "Refresh" : "",
975 opcode == FLUSH_THIS_LSA ? "Flush" : "");
976
977 /* Check LSA flags state coherence */
978 if (!CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED)
979 && (opcode != REORIGINATE_THIS_LSA))
980 return;
981
982 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED)
983 && (opcode == REORIGINATE_THIS_LSA))
984 opcode = REFRESH_THIS_LSA;
985
986 top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
987 if ((OspfRI.scope == OSPF_OPAQUE_AREA_LSA) && (OspfRI.area == NULL)) {
988 zlog_warn(
989 "ospf_router_info_lsa_schedule(): Router Info is Area scope flooding but area is not set");
990 OspfRI.area = ospf_area_lookup_by_area_id(top, OspfRI.area_id);
991 }
992 lsa.area = OspfRI.area;
993 lsa.data = &lsah;
994 lsah.type = OspfRI.scope;
995
996 /* LSA ID is set to 0 for the Router Information. See RFC 4970 */
997 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_ROUTER_INFORMATION_LSA, 0);
998 lsah.id.s_addr = htonl(tmp);
999
1000 switch (opcode) {
1001 case REORIGINATE_THIS_LSA:
1002 if (OspfRI.scope == OSPF_OPAQUE_AREA_LSA)
1003 ospf_opaque_lsa_reoriginate_schedule(
1004 (void *)OspfRI.area, OSPF_OPAQUE_AREA_LSA,
1005 OPAQUE_TYPE_ROUTER_INFORMATION_LSA);
1006 else
1007 ospf_opaque_lsa_reoriginate_schedule(
1008 (void *)top, OSPF_OPAQUE_AS_LSA,
1009 OPAQUE_TYPE_ROUTER_INFORMATION_LSA);
1010 break;
1011 case REFRESH_THIS_LSA:
1012 ospf_opaque_lsa_refresh_schedule(&lsa);
1013 break;
1014 case FLUSH_THIS_LSA:
1015 UNSET_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED);
1016 ospf_opaque_lsa_flush_schedule(&lsa);
1017 break;
1018 default:
1019 zlog_warn("ospf_router_info_lsa_schedule: Unknown opcode (%u)",
1020 opcode);
1021 break;
1022 }
1023
1024 return;
1025 }
1026
1027 /* Callback to handle Segment Routing information */
1028 static int ospf_router_info_lsa_update(struct ospf_lsa *lsa)
1029 {
1030
1031 /* Sanity Check */
1032 if (lsa == NULL) {
1033 zlog_warn("OSPF-RI (%s): Abort! LSA is NULL", __func__);
1034 return -1;
1035 }
1036
1037 /* Process only Opaque LSA */
1038 if ((lsa->data->type != OSPF_OPAQUE_AREA_LSA)
1039 && (lsa->data->type != OSPF_OPAQUE_AS_LSA))
1040 return 0;
1041
1042 /* Process only Router Information LSA */
1043 if (GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr))
1044 != OPAQUE_TYPE_ROUTER_INFORMATION_LSA)
1045 return 0;
1046
1047 /* Check if it is not my LSA */
1048 if (IS_LSA_SELF(lsa))
1049 return 0;
1050
1051 /* Check if Router Info & Segment Routing are enable */
1052 if (!OspfRI.enabled || !OspfRI.sr_info.enabled)
1053 return 0;
1054
1055 /* Call Segment Routing LSA update or deletion */
1056 if (!IS_LSA_MAXAGE(lsa))
1057 ospf_sr_ri_lsa_update(lsa);
1058 else
1059 ospf_sr_ri_lsa_delete(lsa);
1060
1061 return 0;
1062 }
1063
1064 /*------------------------------------------------------------------------*
1065 * Followings are vty session control functions.
1066 *------------------------------------------------------------------------*/
1067
1068 static uint16_t show_vty_router_cap(struct vty *vty, struct tlv_header *tlvh)
1069 {
1070 struct ri_tlv_router_cap *top = (struct ri_tlv_router_cap *)tlvh;
1071
1072 if (vty != NULL)
1073 vty_out(vty, " Router Capabilities: 0x%x\n",
1074 ntohl(top->value));
1075 else
1076 zlog_debug(" Router Capabilities: 0x%x", ntohl(top->value));
1077
1078 return TLV_SIZE(tlvh);
1079 }
1080
1081 static uint16_t show_vty_pce_subtlv_address(struct vty *vty,
1082 struct tlv_header *tlvh)
1083 {
1084 struct ri_pce_subtlv_address *top =
1085 (struct ri_pce_subtlv_address *)tlvh;
1086
1087 if (ntohs(top->address.type) == PCE_ADDRESS_TYPE_IPV4) {
1088 if (vty != NULL)
1089 vty_out(vty, " PCE Address: %s\n",
1090 inet_ntoa(top->address.value));
1091 else
1092 zlog_debug(" PCE Address: %s",
1093 inet_ntoa(top->address.value));
1094 } else {
1095 /* TODO: Add support to IPv6 with inet_ntop() */
1096 if (vty != NULL)
1097 vty_out(vty, " PCE Address: 0x%x\n",
1098 ntohl(top->address.value.s_addr));
1099 else
1100 zlog_debug(" PCE Address: 0x%x",
1101 ntohl(top->address.value.s_addr));
1102 }
1103
1104 return TLV_SIZE(tlvh);
1105 }
1106
1107 static uint16_t show_vty_pce_subtlv_path_scope(struct vty *vty,
1108 struct tlv_header *tlvh)
1109 {
1110 struct ri_pce_subtlv_path_scope *top =
1111 (struct ri_pce_subtlv_path_scope *)tlvh;
1112
1113 if (vty != NULL)
1114 vty_out(vty, " PCE Path Scope: 0x%x\n", ntohl(top->value));
1115 else
1116 zlog_debug(" PCE Path Scope: 0x%x", ntohl(top->value));
1117
1118 return TLV_SIZE(tlvh);
1119 }
1120
1121 static uint16_t show_vty_pce_subtlv_domain(struct vty *vty,
1122 struct tlv_header *tlvh)
1123 {
1124 struct ri_pce_subtlv_domain *top = (struct ri_pce_subtlv_domain *)tlvh;
1125 struct in_addr tmp;
1126
1127 if (ntohs(top->type) == PCE_DOMAIN_TYPE_AREA) {
1128 tmp.s_addr = top->value;
1129 if (vty != NULL)
1130 vty_out(vty, " PCE domain Area: %s\n", inet_ntoa(tmp));
1131 else
1132 zlog_debug(" PCE domain Area: %s", inet_ntoa(tmp));
1133 } else {
1134 if (vty != NULL)
1135 vty_out(vty, " PCE domain AS: %d\n",
1136 ntohl(top->value));
1137 else
1138 zlog_debug(" PCE domain AS: %d", ntohl(top->value));
1139 }
1140 return TLV_SIZE(tlvh);
1141 }
1142
1143 static uint16_t show_vty_pce_subtlv_neighbor(struct vty *vty,
1144 struct tlv_header *tlvh)
1145 {
1146
1147 struct ri_pce_subtlv_neighbor *top =
1148 (struct ri_pce_subtlv_neighbor *)tlvh;
1149 struct in_addr tmp;
1150
1151 if (ntohs(top->type) == PCE_DOMAIN_TYPE_AREA) {
1152 tmp.s_addr = top->value;
1153 if (vty != NULL)
1154 vty_out(vty, " PCE neighbor Area: %s\n",
1155 inet_ntoa(tmp));
1156 else
1157 zlog_debug(" PCE neighbor Area: %s", inet_ntoa(tmp));
1158 } else {
1159 if (vty != NULL)
1160 vty_out(vty, " PCE neighbor AS: %d\n",
1161 ntohl(top->value));
1162 else
1163 zlog_debug(" PCE neighbor AS: %d",
1164 ntohl(top->value));
1165 }
1166 return TLV_SIZE(tlvh);
1167 }
1168
1169 static uint16_t show_vty_pce_subtlv_cap_flag(struct vty *vty,
1170 struct tlv_header *tlvh)
1171 {
1172 struct ri_pce_subtlv_cap_flag *top =
1173 (struct ri_pce_subtlv_cap_flag *)tlvh;
1174
1175 if (vty != NULL)
1176 vty_out(vty, " PCE Capabilities Flag: 0x%x\n",
1177 ntohl(top->value));
1178 else
1179 zlog_debug(" PCE Capabilities Flag: 0x%x",
1180 ntohl(top->value));
1181
1182 return TLV_SIZE(tlvh);
1183 }
1184
1185 static uint16_t show_vty_unknown_tlv(struct vty *vty, struct tlv_header *tlvh)
1186 {
1187 if (vty != NULL)
1188 vty_out(vty, " Unknown TLV: [type(0x%x), length(0x%x)]\n",
1189 ntohs(tlvh->type), ntohs(tlvh->length));
1190 else
1191 zlog_debug(" Unknown TLV: [type(0x%x), length(0x%x)]",
1192 ntohs(tlvh->type), ntohs(tlvh->length));
1193
1194 return TLV_SIZE(tlvh);
1195 }
1196
1197 static uint16_t show_vty_pce_info(struct vty *vty, struct tlv_header *ri,
1198 uint32_t total)
1199 {
1200 struct tlv_header *tlvh;
1201 uint16_t sum = 0;
1202
1203 for (tlvh = ri; sum < total; tlvh = TLV_HDR_NEXT(tlvh)) {
1204 switch (ntohs(tlvh->type)) {
1205 case RI_PCE_SUBTLV_ADDRESS:
1206 sum += show_vty_pce_subtlv_address(vty, tlvh);
1207 break;
1208 case RI_PCE_SUBTLV_PATH_SCOPE:
1209 sum += show_vty_pce_subtlv_path_scope(vty, tlvh);
1210 break;
1211 case RI_PCE_SUBTLV_DOMAIN:
1212 sum += show_vty_pce_subtlv_domain(vty, tlvh);
1213 break;
1214 case RI_PCE_SUBTLV_NEIGHBOR:
1215 sum += show_vty_pce_subtlv_neighbor(vty, tlvh);
1216 break;
1217 case RI_PCE_SUBTLV_CAP_FLAG:
1218 sum += show_vty_pce_subtlv_cap_flag(vty, tlvh);
1219 break;
1220 default:
1221 sum += show_vty_unknown_tlv(vty, tlvh);
1222 break;
1223 }
1224 }
1225 return sum;
1226 }
1227
1228 /* Display Segment Routing Algorithm TLV information */
1229 static uint16_t show_vty_sr_algorithm(struct vty *vty, struct tlv_header *tlvh)
1230 {
1231 struct ri_sr_tlv_sr_algorithm *algo =
1232 (struct ri_sr_tlv_sr_algorithm *)tlvh;
1233 int i;
1234
1235 if (vty != NULL) {
1236 vty_out(vty, " Segment Routing Algorithm TLV:\n");
1237 for (i = 0; i < ntohs(algo->header.length); i++) {
1238 switch (algo->value[i]) {
1239 case 0:
1240 vty_out(vty, " Algorithm %d: SPF\n", i);
1241 break;
1242 case 1:
1243 vty_out(vty, " Algorithm %d: Strict SPF\n",
1244 i);
1245 break;
1246 default:
1247 vty_out(vty,
1248 " Algorithm %d: Unknown value %d\n", i,
1249 algo->value[i]);
1250 break;
1251 }
1252 }
1253 }
1254
1255 else {
1256 zlog_debug(" Segment Routing Algorithm TLV:\n");
1257 for (i = 0; i < ntohs(algo->header.length); i++)
1258 switch (algo->value[i]) {
1259 case 0:
1260 zlog_debug(" Algorithm %d: SPF\n", i);
1261 break;
1262 case 1:
1263 zlog_debug(" Algorithm %d: Strict SPF\n", i);
1264 break;
1265 default:
1266 zlog_debug(
1267 " Algorithm %d: Unknown value %d\n",
1268 i, algo->value[i]);
1269 break;
1270 }
1271 }
1272
1273 return TLV_SIZE(tlvh);
1274 }
1275
1276 /* Display Segment Routing SID/Label Range TLV information */
1277 static uint16_t show_vty_sr_range(struct vty *vty, struct tlv_header *tlvh)
1278 {
1279 struct ri_sr_tlv_sid_label_range *range =
1280 (struct ri_sr_tlv_sid_label_range *)tlvh;
1281
1282 if (vty != NULL) {
1283 vty_out(vty,
1284 " Segment Routing Range TLV:\n"
1285 " Range Size = %d\n"
1286 " SID Label = %d\n\n",
1287 GET_RANGE_SIZE(ntohl(range->size)),
1288 GET_LABEL(ntohl(range->lower.value)));
1289 } else {
1290 zlog_debug(
1291 " Segment Routing Range TLV:\n"
1292 " Range Size = %d\n"
1293 " SID Label = %d\n\n",
1294 GET_RANGE_SIZE(ntohl(range->size)),
1295 GET_LABEL(ntohl(range->lower.value)));
1296 }
1297
1298 return TLV_SIZE(tlvh);
1299 }
1300
1301 /* Display Segment Routing Maximum Stack Depth TLV information */
1302 static uint16_t show_vty_sr_msd(struct vty *vty, struct tlv_header *tlvh)
1303 {
1304 struct ri_sr_tlv_node_msd *msd = (struct ri_sr_tlv_node_msd *)tlvh;
1305
1306 if (vty != NULL) {
1307 vty_out(vty,
1308 " Segment Routing MSD TLV:\n"
1309 " Node Maximum Stack Depth = %d\n",
1310 msd->value);
1311 } else {
1312 zlog_debug(
1313 " Segment Routing MSD TLV:\n"
1314 " Node Maximum Stack Depth = %d\n",
1315 msd->value);
1316 }
1317
1318 return TLV_SIZE(tlvh);
1319 }
1320
1321 static void ospf_router_info_show_info(struct vty *vty, struct ospf_lsa *lsa)
1322 {
1323 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1324 struct tlv_header *tlvh;
1325 uint16_t length = 0, sum = 0;
1326
1327 /* Initialize TLV browsing */
1328 length = ntohs(lsah->length) - OSPF_LSA_HEADER_SIZE;
1329
1330 for (tlvh = TLV_HDR_TOP(lsah); sum < length;
1331 tlvh = TLV_HDR_NEXT(tlvh)) {
1332 switch (ntohs(tlvh->type)) {
1333 case RI_TLV_CAPABILITIES:
1334 sum += show_vty_router_cap(vty, tlvh);
1335 break;
1336 case RI_TLV_PCE:
1337 tlvh++;
1338 sum += TLV_HDR_SIZE;
1339 sum += show_vty_pce_info(vty, tlvh, length - sum);
1340 break;
1341 case RI_SR_TLV_SR_ALGORITHM:
1342 sum += show_vty_sr_algorithm(vty, tlvh);
1343 break;
1344 case RI_SR_TLV_SID_LABEL_RANGE:
1345 sum += show_vty_sr_range(vty, tlvh);
1346 break;
1347 case RI_SR_TLV_NODE_MSD:
1348 sum += show_vty_sr_msd(vty, tlvh);
1349 break;
1350
1351 default:
1352 sum += show_vty_unknown_tlv(vty, tlvh);
1353 break;
1354 }
1355 }
1356
1357 return;
1358 }
1359
1360 static void ospf_router_info_config_write_router(struct vty *vty)
1361 {
1362 struct ospf_pce_info *pce = &OspfRI.pce_info;
1363 struct listnode *node;
1364 struct ri_pce_subtlv_domain *domain;
1365 struct ri_pce_subtlv_neighbor *neighbor;
1366 struct in_addr tmp;
1367
1368 if (!OspfRI.enabled)
1369 return;
1370
1371 if (OspfRI.scope == OSPF_OPAQUE_AS_LSA)
1372 vty_out(vty, " router-info as\n");
1373 else
1374 vty_out(vty, " router-info area %s\n",
1375 inet_ntoa(OspfRI.area_id));
1376
1377 if (OspfRI.pce_info.enabled) {
1378
1379 if (pce->pce_address.header.type != 0)
1380 vty_out(vty, " pce address %s\n",
1381 inet_ntoa(pce->pce_address.address.value));
1382
1383 if (pce->pce_cap_flag.header.type != 0)
1384 vty_out(vty, " pce flag 0x%x\n",
1385 ntohl(pce->pce_cap_flag.value));
1386
1387 for (ALL_LIST_ELEMENTS_RO(pce->pce_domain, node, domain)) {
1388 if (domain->header.type != 0) {
1389 if (domain->type == PCE_DOMAIN_TYPE_AREA) {
1390 tmp.s_addr = domain->value;
1391 vty_out(vty, " pce domain area %s\n",
1392 inet_ntoa(tmp));
1393 } else {
1394 vty_out(vty, " pce domain as %d\n",
1395 ntohl(domain->value));
1396 }
1397 }
1398 }
1399
1400 for (ALL_LIST_ELEMENTS_RO(pce->pce_neighbor, node, neighbor)) {
1401 if (neighbor->header.type != 0) {
1402 if (neighbor->type == PCE_DOMAIN_TYPE_AREA) {
1403 tmp.s_addr = neighbor->value;
1404 vty_out(vty, " pce neighbor area %s\n",
1405 inet_ntoa(tmp));
1406 } else {
1407 vty_out(vty, " pce neighbor as %d\n",
1408 ntohl(neighbor->value));
1409 }
1410 }
1411 }
1412
1413 if (pce->pce_scope.header.type != 0)
1414 vty_out(vty, " pce scope 0x%x\n",
1415 ntohl(OspfRI.pce_info.pce_scope.value));
1416 }
1417 return;
1418 }
1419
1420 /*------------------------------------------------------------------------*
1421 * Followings are vty command functions.
1422 *------------------------------------------------------------------------*/
1423
1424 DEFUN (router_info,
1425 router_info_area_cmd,
1426 "router-info <as|area A.B.C.D>",
1427 OSPF_RI_STR
1428 "Enable the Router Information functionality with AS flooding scope\n"
1429 "Enable the Router Information functionality with Area flooding scope\n"
1430 "OSPF area ID in IP format\n")
1431 {
1432 int idx_ipv4 = 2;
1433 char *area = (argc == 3) ? argv[idx_ipv4]->arg : NULL;
1434
1435 uint8_t scope;
1436
1437 if (OspfRI.enabled)
1438 return CMD_SUCCESS;
1439
1440 /* Check and get Area value if present */
1441 if (area) {
1442 if (!inet_aton(area, &OspfRI.area_id)) {
1443 vty_out(vty, "%% specified Area ID %s is invalid\n",
1444 area);
1445 return CMD_WARNING_CONFIG_FAILED;
1446 }
1447 scope = OSPF_OPAQUE_AREA_LSA;
1448 } else {
1449 OspfRI.area_id.s_addr = 0;
1450 scope = OSPF_OPAQUE_AS_LSA;
1451 }
1452
1453 /* First start to register Router Information callbacks */
1454 if ((ospf_router_info_register(scope)) != 0) {
1455 zlog_warn(
1456 "Unable to register Router Information callbacks. Abort!");
1457 return CMD_WARNING_CONFIG_FAILED;
1458 }
1459
1460 OspfRI.enabled = true;
1461
1462 if (IS_DEBUG_OSPF_EVENT)
1463 zlog_debug("RI-> Router Information (%s flooding): OFF -> ON",
1464 OspfRI.scope == OSPF_OPAQUE_AREA_LSA ? "Area"
1465 : "AS");
1466
1467 /*
1468 * Following code is intended to handle two cases;
1469 *
1470 * 1) Router Information was disabled at startup time, but now become
1471 * enabled.
1472 * 2) Router Information was once enabled then disabled, and now enabled
1473 * again.
1474 */
1475
1476 initialize_params(&OspfRI);
1477
1478 /* Refresh RI LSA if already engaged */
1479 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED)) {
1480 zlog_debug("RI-> Refresh LSA following configuration");
1481 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1482 } else {
1483 zlog_debug("RI-> Initial origination following configuration");
1484 ospf_router_info_lsa_schedule(REORIGINATE_THIS_LSA);
1485 }
1486 return CMD_SUCCESS;
1487 }
1488
1489
1490 DEFUN (no_router_info,
1491 no_router_info_cmd,
1492 "no router-info",
1493 NO_STR
1494 "Disable the Router Information functionality\n")
1495 {
1496
1497 if (!OspfRI.enabled)
1498 return CMD_SUCCESS;
1499
1500 if (IS_DEBUG_OSPF_EVENT)
1501 zlog_debug("RI-> Router Information: ON -> OFF");
1502
1503 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1504 ospf_router_info_lsa_schedule(FLUSH_THIS_LSA);
1505
1506 OspfRI.enabled = false;
1507
1508 return CMD_SUCCESS;
1509 }
1510
1511 static int ospf_ri_enabled(struct vty *vty)
1512 {
1513 if (OspfRI.enabled)
1514 return 1;
1515
1516 if (vty)
1517 vty_out(vty, "%% OSPF RI is not turned on\n");
1518
1519 return 0;
1520 }
1521
1522 DEFUN (pce_address,
1523 pce_address_cmd,
1524 "pce address A.B.C.D",
1525 PCE_STR
1526 "Stable IP address of the PCE\n"
1527 "PCE address in IPv4 address format\n")
1528 {
1529 int idx_ipv4 = 2;
1530 struct in_addr value;
1531 struct ospf_pce_info *pi = &OspfRI.pce_info;
1532
1533 if (!ospf_ri_enabled(vty))
1534 return CMD_WARNING_CONFIG_FAILED;
1535
1536 if (!inet_aton(argv[idx_ipv4]->arg, &value)) {
1537 vty_out(vty, "Please specify PCE Address by A.B.C.D\n");
1538 return CMD_WARNING_CONFIG_FAILED;
1539 }
1540
1541 if (ntohs(pi->pce_address.header.type) == 0
1542 || ntohl(pi->pce_address.address.value.s_addr)
1543 != ntohl(value.s_addr)) {
1544
1545 set_pce_address(value, pi);
1546
1547 /* Refresh RI LSA if already engaged */
1548 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1549 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1550 }
1551
1552 return CMD_SUCCESS;
1553 }
1554
1555 DEFUN (no_pce_address,
1556 no_pce_address_cmd,
1557 "no pce address [A.B.C.D]",
1558 NO_STR
1559 PCE_STR
1560 "Disable PCE address\n"
1561 "PCE address in IPv4 address format\n")
1562 {
1563
1564 unset_param(&OspfRI.pce_info.pce_address);
1565
1566 /* Refresh RI LSA if already engaged */
1567 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1568 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1569
1570 return CMD_SUCCESS;
1571 }
1572
1573 DEFUN (pce_path_scope,
1574 pce_path_scope_cmd,
1575 "pce scope BITPATTERN",
1576 PCE_STR
1577 "Path scope visibilities of the PCE for path computation\n"
1578 "32-bit Hexadecimal value\n")
1579 {
1580 int idx_bitpattern = 2;
1581 uint32_t scope;
1582 struct ospf_pce_info *pi = &OspfRI.pce_info;
1583
1584 if (!ospf_ri_enabled(vty))
1585 return CMD_WARNING_CONFIG_FAILED;
1586
1587 if (sscanf(argv[idx_bitpattern]->arg, "0x%x", &scope) != 1) {
1588 vty_out(vty, "pce_path_scope: fscanf: %s\n",
1589 safe_strerror(errno));
1590 return CMD_WARNING_CONFIG_FAILED;
1591 }
1592
1593 if (ntohl(pi->pce_scope.header.type) == 0
1594 || scope != pi->pce_scope.value) {
1595 set_pce_path_scope(scope, pi);
1596
1597 /* Refresh RI LSA if already engaged */
1598 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1599 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1600 }
1601
1602 return CMD_SUCCESS;
1603 }
1604
1605 DEFUN (no_pce_path_scope,
1606 no_pce_path_scope_cmd,
1607 "no pce scope [BITPATTERN]",
1608 NO_STR
1609 PCE_STR
1610 "Disable PCE path scope\n"
1611 "32-bit Hexadecimal value\n")
1612 {
1613
1614 unset_param(&OspfRI.pce_info.pce_address);
1615
1616 /* Refresh RI LSA if already engaged */
1617 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1618 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1619
1620 return CMD_SUCCESS;
1621 }
1622
1623 DEFUN (pce_domain,
1624 pce_domain_cmd,
1625 "pce domain as (0-65535)",
1626 PCE_STR
1627 "Configure PCE domain AS number\n"
1628 "AS number where the PCE as visibilities for path computation\n"
1629 "AS number in decimal <0-65535>\n")
1630 {
1631 int idx_number = 3;
1632
1633 uint32_t as;
1634 struct ospf_pce_info *pce = &OspfRI.pce_info;
1635 struct listnode *node;
1636 struct ri_pce_subtlv_domain *domain;
1637
1638 if (!ospf_ri_enabled(vty))
1639 return CMD_WARNING_CONFIG_FAILED;
1640
1641 if (sscanf(argv[idx_number]->arg, "%" SCNu32, &as) != 1) {
1642 vty_out(vty, "pce_domain: fscanf: %s\n", safe_strerror(errno));
1643 return CMD_WARNING_CONFIG_FAILED;
1644 }
1645
1646 /* Check if the domain is not already in the domain list */
1647 for (ALL_LIST_ELEMENTS_RO(pce->pce_domain, node, domain)) {
1648 if (ntohl(domain->header.type) == 0 && as == domain->value)
1649 return CMD_SUCCESS;
1650 }
1651
1652 /* Create new domain if not found */
1653 set_pce_domain(PCE_DOMAIN_TYPE_AS, as, pce);
1654
1655 /* Refresh RI LSA if already engaged */
1656 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1657 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1658
1659 return CMD_SUCCESS;
1660 }
1661
1662 DEFUN (no_pce_domain,
1663 no_pce_domain_cmd,
1664 "no pce domain as (0-65535)",
1665 NO_STR
1666 PCE_STR
1667 "Disable PCE domain AS number\n"
1668 "AS number where the PCE as visibilities for path computation\n"
1669 "AS number in decimal <0-65535>\n")
1670 {
1671 int idx_number = 4;
1672
1673 uint32_t as;
1674 struct ospf_pce_info *pce = &OspfRI.pce_info;
1675
1676 if (sscanf(argv[idx_number]->arg, "%" SCNu32, &as) != 1) {
1677 vty_out(vty, "no_pce_domain: fscanf: %s\n",
1678 safe_strerror(errno));
1679 return CMD_WARNING_CONFIG_FAILED;
1680 }
1681
1682 /* Unset corresponding PCE domain */
1683 unset_pce_domain(PCE_DOMAIN_TYPE_AS, as, pce);
1684
1685 /* Refresh RI LSA if already engaged */
1686 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1687 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1688
1689 return CMD_SUCCESS;
1690 }
1691
1692 DEFUN (pce_neigbhor,
1693 pce_neighbor_cmd,
1694 "pce neighbor as (0-65535)",
1695 PCE_STR
1696 "Configure PCE neighbor domain AS number\n"
1697 "AS number of PCE neighbors\n"
1698 "AS number in decimal <0-65535>\n")
1699 {
1700 int idx_number = 3;
1701
1702 uint32_t as;
1703 struct ospf_pce_info *pce = &OspfRI.pce_info;
1704 struct listnode *node;
1705 struct ri_pce_subtlv_neighbor *neighbor;
1706
1707 if (!ospf_ri_enabled(vty))
1708 return CMD_WARNING_CONFIG_FAILED;
1709
1710 if (sscanf(argv[idx_number]->arg, "%" SCNu32, &as) != 1) {
1711 vty_out(vty, "pce_neighbor: fscanf: %s\n",
1712 safe_strerror(errno));
1713 return CMD_WARNING_CONFIG_FAILED;
1714 }
1715
1716 /* Check if the domain is not already in the domain list */
1717 for (ALL_LIST_ELEMENTS_RO(pce->pce_neighbor, node, neighbor)) {
1718 if (ntohl(neighbor->header.type) == 0 && as == neighbor->value)
1719 return CMD_SUCCESS;
1720 }
1721
1722 /* Create new domain if not found */
1723 set_pce_neighbor(PCE_DOMAIN_TYPE_AS, as, pce);
1724
1725 /* Refresh RI LSA if already engaged */
1726 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1727 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1728
1729 return CMD_SUCCESS;
1730 }
1731
1732 DEFUN (no_pce_neighbor,
1733 no_pce_neighbor_cmd,
1734 "no pce neighbor as (0-65535)",
1735 NO_STR
1736 PCE_STR
1737 "Disable PCE neighbor AS number\n"
1738 "AS number of PCE neighbor\n"
1739 "AS number in decimal <0-65535>\n")
1740 {
1741 int idx_number = 4;
1742
1743 uint32_t as;
1744 struct ospf_pce_info *pce = &OspfRI.pce_info;
1745
1746 if (sscanf(argv[idx_number]->arg, "%" SCNu32, &as) != 1) {
1747 vty_out(vty, "no_pce_neighbor: fscanf: %s\n",
1748 safe_strerror(errno));
1749 return CMD_WARNING_CONFIG_FAILED;
1750 }
1751
1752 /* Unset corresponding PCE domain */
1753 unset_pce_neighbor(PCE_DOMAIN_TYPE_AS, as, pce);
1754
1755 /* Refresh RI LSA if already engaged */
1756 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1757 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1758
1759 return CMD_SUCCESS;
1760 }
1761
1762 DEFUN (pce_cap_flag,
1763 pce_cap_flag_cmd,
1764 "pce flag BITPATTERN",
1765 PCE_STR
1766 "Capabilities of the PCE for path computation\n"
1767 "32-bit Hexadecimal value\n")
1768 {
1769 int idx_bitpattern = 2;
1770
1771 uint32_t cap;
1772 struct ospf_pce_info *pce = &OspfRI.pce_info;
1773
1774 if (!ospf_ri_enabled(vty))
1775 return CMD_WARNING_CONFIG_FAILED;
1776
1777 if (sscanf(argv[idx_bitpattern]->arg, "0x%x", &cap) != 1) {
1778 vty_out(vty, "pce_cap_flag: fscanf: %s\n",
1779 safe_strerror(errno));
1780 return CMD_WARNING_CONFIG_FAILED;
1781 }
1782
1783 if (ntohl(pce->pce_cap_flag.header.type) == 0
1784 || cap != pce->pce_cap_flag.value) {
1785 set_pce_cap_flag(cap, pce);
1786
1787 /* Refresh RI LSA if already engaged */
1788 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1789 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1790 }
1791
1792 return CMD_SUCCESS;
1793 }
1794
1795 DEFUN (no_pce_cap_flag,
1796 no_pce_cap_flag_cmd,
1797 "no pce flag",
1798 NO_STR
1799 PCE_STR
1800 "Disable PCE capabilities\n")
1801 {
1802
1803 unset_param(&OspfRI.pce_info.pce_cap_flag);
1804
1805 /* Refresh RI LSA if already engaged */
1806 if (CHECK_FLAG(OspfRI.flags, RIFLG_LSA_ENGAGED))
1807 ospf_router_info_lsa_schedule(REFRESH_THIS_LSA);
1808
1809 return CMD_SUCCESS;
1810 }
1811
1812 DEFUN (show_ip_ospf_router_info,
1813 show_ip_ospf_router_info_cmd,
1814 "show ip ospf router-info",
1815 SHOW_STR
1816 IP_STR
1817 OSPF_STR
1818 "Router Information\n")
1819 {
1820
1821 if (OspfRI.enabled) {
1822 vty_out(vty, "--- Router Information parameters ---\n");
1823 show_vty_router_cap(vty, &OspfRI.router_cap.header);
1824 } else {
1825 if (vty != NULL)
1826 vty_out(vty,
1827 " Router Information is disabled on this router\n");
1828 }
1829 return CMD_SUCCESS;
1830 }
1831
1832 DEFUN (show_ip_opsf_router_info_pce,
1833 show_ip_ospf_router_info_pce_cmd,
1834 "show ip ospf router-info pce",
1835 SHOW_STR
1836 IP_STR
1837 OSPF_STR
1838 "Router Information\n"
1839 "PCE information\n")
1840 {
1841
1842 struct ospf_pce_info *pce = &OspfRI.pce_info;
1843 struct listnode *node;
1844 struct ri_pce_subtlv_domain *domain;
1845 struct ri_pce_subtlv_neighbor *neighbor;
1846
1847 if ((OspfRI.enabled) && (OspfRI.pce_info.enabled)) {
1848 vty_out(vty, "--- PCE parameters ---\n");
1849
1850 if (pce->pce_address.header.type != 0)
1851 show_vty_pce_subtlv_address(vty,
1852 &pce->pce_address.header);
1853
1854 if (pce->pce_scope.header.type != 0)
1855 show_vty_pce_subtlv_path_scope(vty,
1856 &pce->pce_scope.header);
1857
1858 for (ALL_LIST_ELEMENTS_RO(pce->pce_domain, node, domain)) {
1859 if (domain->header.type != 0)
1860 show_vty_pce_subtlv_domain(vty,
1861 &domain->header);
1862 }
1863
1864 for (ALL_LIST_ELEMENTS_RO(pce->pce_neighbor, node, neighbor)) {
1865 if (neighbor->header.type != 0)
1866 show_vty_pce_subtlv_neighbor(vty,
1867 &neighbor->header);
1868 }
1869
1870 if (pce->pce_cap_flag.header.type != 0)
1871 show_vty_pce_subtlv_cap_flag(vty,
1872 &pce->pce_cap_flag.header);
1873
1874 } else {
1875 vty_out(vty, " PCE info is disabled on this router\n");
1876 }
1877
1878 return CMD_SUCCESS;
1879 }
1880
1881 /* Install new CLI commands */
1882 static void ospf_router_info_register_vty(void)
1883 {
1884 install_element(VIEW_NODE, &show_ip_ospf_router_info_cmd);
1885 install_element(VIEW_NODE, &show_ip_ospf_router_info_pce_cmd);
1886
1887 install_element(OSPF_NODE, &router_info_area_cmd);
1888 install_element(OSPF_NODE, &no_router_info_cmd);
1889 install_element(OSPF_NODE, &pce_address_cmd);
1890 install_element(OSPF_NODE, &no_pce_address_cmd);
1891 install_element(OSPF_NODE, &pce_path_scope_cmd);
1892 install_element(OSPF_NODE, &no_pce_path_scope_cmd);
1893 install_element(OSPF_NODE, &pce_domain_cmd);
1894 install_element(OSPF_NODE, &no_pce_domain_cmd);
1895 install_element(OSPF_NODE, &pce_neighbor_cmd);
1896 install_element(OSPF_NODE, &no_pce_neighbor_cmd);
1897 install_element(OSPF_NODE, &pce_cap_flag_cmd);
1898 install_element(OSPF_NODE, &no_pce_cap_flag_cmd);
1899
1900 return;
1901 }