]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_te.c
*: auto-convert to SPDX License IDs
[mirror_frr.git] / ospfd / ospf_te.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * This is an implementation of RFC3630
4 * Copyright (C) 2001 KDD R&D Laboratories, Inc.
5 * http://www.kddlabs.co.jp/
6 *
7 * Copyright (C) 2012 Orange Labs
8 * http://www.orange.com
9 */
10
11 /* Add support of RFC7471 */
12 /* Add support of RFC5392, RFC6827 */
13
14 #include <zebra.h>
15 #include <math.h>
16
17 #include "linklist.h"
18 #include "prefix.h"
19 #include "vrf.h"
20 #include "if.h"
21 #include "table.h"
22 #include "memory.h"
23 #include "command.h"
24 #include "vty.h"
25 #include "stream.h"
26 #include "log.h"
27 #include "thread.h"
28 #include "hash.h"
29 #include "sockunion.h" /* for inet_aton() */
30 #include "network.h"
31 #include "link_state.h"
32 #include "zclient.h"
33 #include "printfrr.h"
34
35 #include "ospfd/ospfd.h"
36 #include "ospfd/ospf_interface.h"
37 #include "ospfd/ospf_ism.h"
38 #include "ospfd/ospf_asbr.h"
39 #include "ospfd/ospf_lsa.h"
40 #include "ospfd/ospf_lsdb.h"
41 #include "ospfd/ospf_neighbor.h"
42 #include "ospfd/ospf_nsm.h"
43 #include "ospfd/ospf_flood.h"
44 #include "ospfd/ospf_packet.h"
45 #include "ospfd/ospf_spf.h"
46 #include "ospfd/ospf_dump.h"
47 #include "ospfd/ospf_route.h"
48 #include "ospfd/ospf_ase.h"
49 #include "ospfd/ospf_zebra.h"
50 #include "ospfd/ospf_te.h"
51 #include "ospfd/ospf_sr.h"
52 #include "ospfd/ospf_ri.h"
53 #include "ospfd/ospf_ext.h"
54 #include "ospfd/ospf_vty.h"
55 #include "ospfd/ospf_errors.h"
56
57 /*
58 * Global variable to manage Opaque-LSA/MPLS-TE on this node.
59 * Note that all parameter values are stored in network byte order.
60 */
61 struct ospf_mpls_te OspfMplsTE;
62
63 static const char *const mode2text[] = {"Off", "AS", "Area"};
64
65
66 /*------------------------------------------------------------------------*
67 * Following are initialize/terminate functions for MPLS-TE handling.
68 *------------------------------------------------------------------------*/
69
70 static int ospf_mpls_te_new_if(struct interface *ifp);
71 static int ospf_mpls_te_del_if(struct interface *ifp);
72 static void ospf_mpls_te_ism_change(struct ospf_interface *oi, int old_status);
73 static void ospf_mpls_te_nsm_change(struct ospf_neighbor *nbr, int old_status);
74 static void ospf_mpls_te_config_write_router(struct vty *vty);
75 static void ospf_mpls_te_show_info(struct vty *vty, struct json_object *json,
76 struct ospf_lsa *lsa);
77 static int ospf_mpls_te_lsa_originate_area(void *arg);
78 static int ospf_mpls_te_lsa_inter_as_as(void *arg);
79 static int ospf_mpls_te_lsa_inter_as_area(void *arg);
80 static struct ospf_lsa *ospf_mpls_te_lsa_refresh(struct ospf_lsa *lsa);
81 static int ospf_mpls_te_lsa_update(struct ospf_lsa *lsa);
82 static int ospf_mpls_te_lsa_delete(struct ospf_lsa *lsa);
83
84 static void del_mpls_te_link(void *val);
85 static void ospf_mpls_te_register_vty(void);
86
87 int ospf_mpls_te_init(void)
88 {
89 int rc;
90
91 /* Register Opaque AREA LSA Type 1 for Traffic Engineering */
92 rc = ospf_register_opaque_functab(
93 OSPF_OPAQUE_AREA_LSA,
94 OPAQUE_TYPE_TRAFFIC_ENGINEERING_LSA,
95 ospf_mpls_te_new_if,
96 ospf_mpls_te_del_if,
97 ospf_mpls_te_ism_change,
98 ospf_mpls_te_nsm_change,
99 ospf_mpls_te_config_write_router,
100 NULL, /* ospf_mpls_te_config_write_if */
101 NULL, /* ospf_mpls_te_config_write_debug */
102 ospf_mpls_te_show_info, ospf_mpls_te_lsa_originate_area,
103 ospf_mpls_te_lsa_refresh,
104 ospf_mpls_te_lsa_update, /* ospf_mpls_te_new_lsa_hook */
105 ospf_mpls_te_lsa_delete /* ospf_mpls_te_del_lsa_hook */);
106 if (rc != 0) {
107 flog_warn(
108 EC_OSPF_OPAQUE_REGISTRATION,
109 "MPLS-TE (%s): Failed to register Traffic Engineering functions",
110 __func__);
111 return rc;
112 }
113
114 /*
115 * Wee need also to register Opaque LSA Type 6 i.e. Inter-AS RFC5392 for
116 * both AREA and AS at least to have the possibility to call the show()
117 * function when looking to the opaque LSA of the OSPF database.
118 */
119 rc = ospf_register_opaque_functab(OSPF_OPAQUE_AREA_LSA,
120 OPAQUE_TYPE_INTER_AS_LSA, NULL,
121 NULL, NULL, NULL, NULL, NULL, NULL,
122 ospf_mpls_te_show_info,
123 ospf_mpls_te_lsa_inter_as_area,
124 ospf_mpls_te_lsa_refresh, NULL, NULL);
125 if (rc != 0) {
126 flog_warn(
127 EC_OSPF_OPAQUE_REGISTRATION,
128 "MPLS-TE (%s): Failed to register Inter-AS with Area scope",
129 __func__);
130 return rc;
131 }
132
133 rc = ospf_register_opaque_functab(OSPF_OPAQUE_AS_LSA,
134 OPAQUE_TYPE_INTER_AS_LSA, NULL,
135 NULL, NULL, NULL, NULL, NULL, NULL,
136 ospf_mpls_te_show_info,
137 ospf_mpls_te_lsa_inter_as_as,
138 ospf_mpls_te_lsa_refresh, NULL, NULL);
139 if (rc != 0) {
140 flog_warn(
141 EC_OSPF_OPAQUE_REGISTRATION,
142 "MPLS-TE (%s): Failed to register Inter-AS with AS scope",
143 __func__);
144 return rc;
145 }
146
147 memset(&OspfMplsTE, 0, sizeof(OspfMplsTE));
148 OspfMplsTE.enabled = false;
149 OspfMplsTE.export = false;
150 OspfMplsTE.inter_as = Off;
151 OspfMplsTE.iflist = list_new();
152 OspfMplsTE.iflist->del = del_mpls_te_link;
153
154 ospf_mpls_te_register_vty();
155
156 return rc;
157 }
158
159 void ospf_mpls_te_term(void)
160 {
161 list_delete(&OspfMplsTE.iflist);
162
163 ospf_delete_opaque_functab(OSPF_OPAQUE_AREA_LSA,
164 OPAQUE_TYPE_TRAFFIC_ENGINEERING_LSA);
165 ospf_delete_opaque_functab(OSPF_OPAQUE_AREA_LSA,
166 OPAQUE_TYPE_INTER_AS_LSA);
167 ospf_delete_opaque_functab(OSPF_OPAQUE_AS_LSA,
168 OPAQUE_TYPE_INTER_AS_LSA);
169
170 OspfMplsTE.enabled = false;
171 OspfMplsTE.inter_as = Off;
172 OspfMplsTE.export = false;
173
174 return;
175 }
176
177 void ospf_mpls_te_finish(void)
178 {
179 OspfMplsTE.enabled = false;
180 OspfMplsTE.inter_as = Off;
181 OspfMplsTE.export = false;
182 }
183
184 /*------------------------------------------------------------------------*
185 * Following are control functions for MPLS-TE parameters management.
186 *------------------------------------------------------------------------*/
187 static void del_mpls_te_link(void *val)
188 {
189 XFREE(MTYPE_OSPF_MPLS_TE, val);
190 return;
191 }
192
193 static uint32_t get_mpls_te_instance_value(void)
194 {
195 static uint32_t seqno = 0;
196
197 if (seqno < MAX_LEGAL_TE_INSTANCE_NUM)
198 seqno += 1;
199 else
200 seqno = 1; /* Avoid zero. */
201
202 return seqno;
203 }
204
205 static struct mpls_te_link *lookup_linkparams_by_ifp(struct interface *ifp)
206 {
207 struct listnode *node, *nnode;
208 struct mpls_te_link *lp;
209
210 for (ALL_LIST_ELEMENTS(OspfMplsTE.iflist, node, nnode, lp))
211 if (lp->ifp == ifp)
212 return lp;
213
214 return NULL;
215 }
216
217 static struct mpls_te_link *lookup_linkparams_by_instance(struct ospf_lsa *lsa)
218 {
219 struct listnode *node;
220 struct mpls_te_link *lp;
221 unsigned int key = GET_OPAQUE_ID(ntohl(lsa->data->id.s_addr));
222
223 for (ALL_LIST_ELEMENTS_RO(OspfMplsTE.iflist, node, lp))
224 if (lp->instance == key)
225 return lp;
226
227 ote_debug("MPLS-TE (%s): Entry not found: key(%x)", __func__, key);
228 return NULL;
229 }
230
231 static void ospf_mpls_te_foreach_area(
232 void (*func)(struct mpls_te_link *lp, enum lsa_opcode sched_opcode),
233 enum lsa_opcode sched_opcode)
234 {
235 struct listnode *node, *nnode;
236 struct listnode *node2;
237 struct mpls_te_link *lp;
238 struct ospf_area *area;
239
240 for (ALL_LIST_ELEMENTS(OspfMplsTE.iflist, node, nnode, lp)) {
241 /* Skip Inter-AS TEv2 Links */
242 if (IS_INTER_AS(lp->type))
243 continue;
244 if ((area = lp->area) == NULL)
245 continue;
246 if (CHECK_FLAG(lp->flags, LPFLG_LOOKUP_DONE))
247 continue;
248
249 if (func != NULL)
250 (*func)(lp, sched_opcode);
251
252 for (node2 = listnextnode(node); node2;
253 node2 = listnextnode(node2))
254 if ((lp = listgetdata(node2)) != NULL)
255 if (lp->area != NULL)
256 if (IPV4_ADDR_SAME(&lp->area->area_id,
257 &area->area_id))
258 SET_FLAG(lp->flags,
259 LPFLG_LOOKUP_DONE);
260 }
261
262 for (ALL_LIST_ELEMENTS_RO(OspfMplsTE.iflist, node, lp))
263 if (lp->area != NULL)
264 UNSET_FLAG(lp->flags, LPFLG_LOOKUP_DONE);
265
266 return;
267 }
268
269 static void set_mpls_te_router_addr(struct in_addr ipv4)
270 {
271 OspfMplsTE.router_addr.header.type = htons(TE_TLV_ROUTER_ADDR);
272 OspfMplsTE.router_addr.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
273 OspfMplsTE.router_addr.value = ipv4;
274 return;
275 }
276
277 static void set_linkparams_link_header(struct mpls_te_link *lp)
278 {
279 uint16_t length = 0;
280
281 /* TE_LINK_SUBTLV_LINK_TYPE */
282 if (ntohs(lp->link_type.header.type) != 0)
283 length += TLV_SIZE(&lp->link_type.header);
284
285 /* TE_LINK_SUBTLV_LINK_ID */
286 if (ntohs(lp->link_id.header.type) != 0)
287 length += TLV_SIZE(&lp->link_id.header);
288
289 /* TE_LINK_SUBTLV_LCLIF_IPADDR */
290 if (lp->lclif_ipaddr.header.type != 0)
291 length += TLV_SIZE(&lp->lclif_ipaddr.header);
292
293 /* TE_LINK_SUBTLV_RMTIF_IPADDR */
294 if (lp->rmtif_ipaddr.header.type != 0)
295 length += TLV_SIZE(&lp->rmtif_ipaddr.header);
296
297 /* TE_LINK_SUBTLV_TE_METRIC */
298 if (ntohs(lp->te_metric.header.type) != 0)
299 length += TLV_SIZE(&lp->te_metric.header);
300
301 /* TE_LINK_SUBTLV_MAX_BW */
302 if (ntohs(lp->max_bw.header.type) != 0)
303 length += TLV_SIZE(&lp->max_bw.header);
304
305 /* TE_LINK_SUBTLV_MAX_RSV_BW */
306 if (ntohs(lp->max_rsv_bw.header.type) != 0)
307 length += TLV_SIZE(&lp->max_rsv_bw.header);
308
309 /* TE_LINK_SUBTLV_UNRSV_BW */
310 if (ntohs(lp->unrsv_bw.header.type) != 0)
311 length += TLV_SIZE(&lp->unrsv_bw.header);
312
313 /* TE_LINK_SUBTLV_RSC_CLSCLR */
314 if (ntohs(lp->rsc_clsclr.header.type) != 0)
315 length += TLV_SIZE(&lp->rsc_clsclr.header);
316
317 /* TE_LINK_SUBTLV_LLRI */
318 if (ntohs(lp->llri.header.type) != 0)
319 length += TLV_SIZE(&lp->llri.header);
320
321 /* TE_LINK_SUBTLV_RIP */
322 if (ntohs(lp->rip.header.type) != 0)
323 length += TLV_SIZE(&lp->rip.header);
324
325 /* TE_LINK_SUBTLV_RAS */
326 if (ntohs(lp->ras.header.type) != 0)
327 length += TLV_SIZE(&lp->ras.header);
328
329 /* TE_LINK_SUBTLV_LRRID */
330 if (ntohs(lp->lrrid.header.type) != 0)
331 length += TLV_SIZE(&lp->lrrid.header);
332
333 /* TE_LINK_SUBTLV_AV_DELAY */
334 if (ntohs(lp->av_delay.header.type) != 0)
335 length += TLV_SIZE(&lp->av_delay.header);
336
337 /* TE_LINK_SUBTLV_MM_DELAY */
338 if (ntohs(lp->mm_delay.header.type) != 0)
339 length += TLV_SIZE(&lp->mm_delay.header);
340
341 /* TE_LINK_SUBTLV_DELAY_VAR */
342 if (ntohs(lp->delay_var.header.type) != 0)
343 length += TLV_SIZE(&lp->delay_var.header);
344
345 /* TE_LINK_SUBTLV_PKT_LOSS */
346 if (ntohs(lp->pkt_loss.header.type) != 0)
347 length += TLV_SIZE(&lp->pkt_loss.header);
348
349 /* TE_LINK_SUBTLV_RES_BW */
350 if (ntohs(lp->res_bw.header.type) != 0)
351 length += TLV_SIZE(&lp->res_bw.header);
352
353 /* TE_LINK_SUBTLV_AVA_BW */
354 if (ntohs(lp->ava_bw.header.type) != 0)
355 length += TLV_SIZE(&lp->ava_bw.header);
356
357 /* TE_LINK_SUBTLV_USE_BW */
358 if (ntohs(lp->use_bw.header.type) != 0)
359 length += TLV_SIZE(&lp->use_bw.header);
360
361 lp->link_header.header.type = htons(TE_TLV_LINK);
362 lp->link_header.header.length = htons(length);
363
364 return;
365 }
366
367 static void set_linkparams_link_type(struct ospf_interface *oi,
368 struct mpls_te_link *lp)
369 {
370 lp->link_type.header.type = htons(TE_LINK_SUBTLV_LINK_TYPE);
371 lp->link_type.header.length = htons(TE_LINK_SUBTLV_TYPE_SIZE);
372
373 switch (oi->type) {
374 case OSPF_IFTYPE_POINTOPOINT:
375 lp->link_type.link_type.value = LINK_TYPE_SUBTLV_VALUE_PTP;
376 break;
377 case OSPF_IFTYPE_BROADCAST:
378 case OSPF_IFTYPE_NBMA:
379 lp->link_type.link_type.value = LINK_TYPE_SUBTLV_VALUE_MA;
380 break;
381 default:
382 /* Not supported yet. */ /* XXX */
383 lp->link_type.header.type = htons(0);
384 break;
385 }
386 return;
387 }
388
389 static void set_linkparams_link_id(struct mpls_te_link *lp,
390 struct in_addr link_id)
391 {
392
393 lp->link_id.header.type = htons(TE_LINK_SUBTLV_LINK_ID);
394 lp->link_id.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
395 lp->link_id.value = link_id;
396 return;
397 }
398
399 static void set_linkparams_lclif_ipaddr(struct mpls_te_link *lp,
400 struct in_addr lclif)
401 {
402
403 lp->lclif_ipaddr.header.type = htons(TE_LINK_SUBTLV_LCLIF_IPADDR);
404 lp->lclif_ipaddr.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
405 lp->lclif_ipaddr.value[0] = lclif;
406 return;
407 }
408
409 static void set_linkparams_rmtif_ipaddr(struct mpls_te_link *lp,
410 struct in_addr rmtif)
411 {
412
413 lp->rmtif_ipaddr.header.type = htons(TE_LINK_SUBTLV_RMTIF_IPADDR);
414 lp->rmtif_ipaddr.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
415 lp->rmtif_ipaddr.value[0] = rmtif;
416 return;
417 }
418
419 static void set_linkparams_te_metric(struct mpls_te_link *lp,
420 uint32_t te_metric)
421 {
422 lp->te_metric.header.type = htons(TE_LINK_SUBTLV_TE_METRIC);
423 lp->te_metric.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
424 lp->te_metric.value = htonl(te_metric);
425 return;
426 }
427
428 static void set_linkparams_max_bw(struct mpls_te_link *lp, float fp)
429 {
430 lp->max_bw.header.type = htons(TE_LINK_SUBTLV_MAX_BW);
431 lp->max_bw.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
432 lp->max_bw.value = htonf(fp);
433 return;
434 }
435
436 static void set_linkparams_max_rsv_bw(struct mpls_te_link *lp, float fp)
437 {
438 lp->max_rsv_bw.header.type = htons(TE_LINK_SUBTLV_MAX_RSV_BW);
439 lp->max_rsv_bw.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
440 lp->max_rsv_bw.value = htonf(fp);
441 return;
442 }
443
444 static void set_linkparams_unrsv_bw(struct mpls_te_link *lp, int priority,
445 float fp)
446 {
447 /* Note that TLV-length field is the size of array. */
448 lp->unrsv_bw.header.type = htons(TE_LINK_SUBTLV_UNRSV_BW);
449 lp->unrsv_bw.header.length = htons(TE_LINK_SUBTLV_UNRSV_SIZE);
450 lp->unrsv_bw.value[priority] = htonf(fp);
451 return;
452 }
453
454 static void set_linkparams_rsc_clsclr(struct mpls_te_link *lp,
455 uint32_t classcolor)
456 {
457 lp->rsc_clsclr.header.type = htons(TE_LINK_SUBTLV_RSC_CLSCLR);
458 lp->rsc_clsclr.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
459 lp->rsc_clsclr.value = htonl(classcolor);
460 return;
461 }
462
463 static void set_linkparams_inter_as(struct mpls_te_link *lp,
464 struct in_addr addr, uint32_t as)
465 {
466
467 /* Set the Remote ASBR IP address and then the associated AS number */
468 lp->rip.header.type = htons(TE_LINK_SUBTLV_RIP);
469 lp->rip.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
470 lp->rip.value = addr;
471
472 lp->ras.header.type = htons(TE_LINK_SUBTLV_RAS);
473 lp->ras.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
474 lp->ras.value = htonl(as);
475
476 /* Set Type & Flooding flag accordingly */
477 lp->type = INTER_AS;
478 if (OspfMplsTE.inter_as == AS)
479 SET_FLAG(lp->flags, LPFLG_LSA_FLOOD_AS);
480 else
481 UNSET_FLAG(lp->flags, LPFLG_LSA_FLOOD_AS);
482 }
483
484 static void unset_linkparams_inter_as(struct mpls_te_link *lp)
485 {
486
487 /* Reset the Remote ASBR IP address and then the associated AS number */
488 lp->rip.header.type = htons(0);
489 lp->rip.header.length = htons(0);
490 lp->rip.value.s_addr = htonl(0);
491
492 lp->ras.header.type = htons(0);
493 lp->ras.header.length = htons(0);
494 lp->ras.value = htonl(0);
495
496 /* Reset Type & Flooding flag accordingly */
497 lp->type = STD_TE;
498 UNSET_FLAG(lp->flags, LPFLG_LSA_FLOOD_AS);
499 }
500
501 void set_linkparams_llri(struct mpls_te_link *lp, uint32_t local,
502 uint32_t remote)
503 {
504
505 lp->llri.header.type = htons(TE_LINK_SUBTLV_LLRI);
506 lp->llri.header.length = htons(TE_LINK_SUBTLV_LLRI_SIZE);
507 lp->llri.local = htonl(local);
508 lp->llri.remote = htonl(remote);
509 }
510
511 void set_linkparams_lrrid(struct mpls_te_link *lp, struct in_addr local,
512 struct in_addr remote)
513 {
514
515 lp->lrrid.header.type = htons(TE_LINK_SUBTLV_LRRID);
516 lp->lrrid.header.length = htons(TE_LINK_SUBTLV_LRRID_SIZE);
517 lp->lrrid.local.s_addr = local.s_addr;
518 lp->lrrid.remote.s_addr = remote.s_addr;
519 }
520
521 static void set_linkparams_av_delay(struct mpls_te_link *lp, uint32_t delay,
522 uint8_t anormal)
523 {
524 uint32_t tmp;
525 /* Note that TLV-length field is the size of array. */
526 lp->av_delay.header.type = htons(TE_LINK_SUBTLV_AV_DELAY);
527 lp->av_delay.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
528 tmp = delay & TE_EXT_MASK;
529 if (anormal)
530 tmp |= TE_EXT_ANORMAL;
531 lp->av_delay.value = htonl(tmp);
532 return;
533 }
534
535 static void set_linkparams_mm_delay(struct mpls_te_link *lp, uint32_t low,
536 uint32_t high, uint8_t anormal)
537 {
538 uint32_t tmp;
539 /* Note that TLV-length field is the size of array. */
540 lp->mm_delay.header.type = htons(TE_LINK_SUBTLV_MM_DELAY);
541 lp->mm_delay.header.length = htons(TE_LINK_SUBTLV_MM_DELAY_SIZE);
542 tmp = low & TE_EXT_MASK;
543 if (anormal)
544 tmp |= TE_EXT_ANORMAL;
545 lp->mm_delay.low = htonl(tmp);
546 lp->mm_delay.high = htonl(high);
547 return;
548 }
549
550 static void set_linkparams_delay_var(struct mpls_te_link *lp, uint32_t jitter)
551 {
552 /* Note that TLV-length field is the size of array. */
553 lp->delay_var.header.type = htons(TE_LINK_SUBTLV_DELAY_VAR);
554 lp->delay_var.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
555 lp->delay_var.value = htonl(jitter & TE_EXT_MASK);
556 return;
557 }
558
559 static void set_linkparams_pkt_loss(struct mpls_te_link *lp, uint32_t loss,
560 uint8_t anormal)
561 {
562 uint32_t tmp;
563 /* Note that TLV-length field is the size of array. */
564 lp->pkt_loss.header.type = htons(TE_LINK_SUBTLV_PKT_LOSS);
565 lp->pkt_loss.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
566 tmp = loss & TE_EXT_MASK;
567 if (anormal)
568 tmp |= TE_EXT_ANORMAL;
569 lp->pkt_loss.value = htonl(tmp);
570 return;
571 }
572
573 static void set_linkparams_res_bw(struct mpls_te_link *lp, float fp)
574 {
575 /* Note that TLV-length field is the size of array. */
576 lp->res_bw.header.type = htons(TE_LINK_SUBTLV_RES_BW);
577 lp->res_bw.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
578 lp->res_bw.value = htonf(fp);
579 return;
580 }
581
582 static void set_linkparams_ava_bw(struct mpls_te_link *lp, float fp)
583 {
584 /* Note that TLV-length field is the size of array. */
585 lp->ava_bw.header.type = htons(TE_LINK_SUBTLV_AVA_BW);
586 lp->ava_bw.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
587 lp->ava_bw.value = htonf(fp);
588 return;
589 }
590
591 static void set_linkparams_use_bw(struct mpls_te_link *lp, float fp)
592 {
593 /* Note that TLV-length field is the size of array. */
594 lp->use_bw.header.type = htons(TE_LINK_SUBTLV_USE_BW);
595 lp->use_bw.header.length = htons(TE_LINK_SUBTLV_DEF_SIZE);
596 lp->use_bw.value = htonf(fp);
597 return;
598 }
599
600 /* Update TE parameters from Interface */
601 static void update_linkparams(struct mpls_te_link *lp)
602 {
603 int i;
604 struct interface *ifp;
605
606 /* Get the Interface structure */
607 if ((ifp = lp->ifp) == NULL) {
608 ote_debug(
609 "MPLS-TE (%s): Abort update TE parameters: no interface associated to Link Parameters",
610 __func__);
611 return;
612 }
613 if (!HAS_LINK_PARAMS(ifp)) {
614 ote_debug(
615 "MPLS-TE (%s): Abort update TE parameters: no Link Parameters for interface",
616 __func__);
617 return;
618 }
619
620 /* RFC3630 metrics */
621 if (IS_PARAM_SET(ifp->link_params, LP_ADM_GRP))
622 set_linkparams_rsc_clsclr(lp, ifp->link_params->admin_grp);
623 else
624 TLV_TYPE(lp->rsc_clsclr) = 0;
625
626 if (IS_PARAM_SET(ifp->link_params, LP_MAX_BW))
627 set_linkparams_max_bw(lp, ifp->link_params->max_bw);
628 else
629 TLV_TYPE(lp->max_bw) = 0;
630
631 if (IS_PARAM_SET(ifp->link_params, LP_MAX_RSV_BW))
632 set_linkparams_max_rsv_bw(lp, ifp->link_params->max_rsv_bw);
633 else
634 TLV_TYPE(lp->max_rsv_bw) = 0;
635
636 if (IS_PARAM_SET(ifp->link_params, LP_UNRSV_BW))
637 for (i = 0; i < MAX_CLASS_TYPE; i++)
638 set_linkparams_unrsv_bw(lp, i,
639 ifp->link_params->unrsv_bw[i]);
640 else
641 TLV_TYPE(lp->unrsv_bw) = 0;
642
643 if (IS_PARAM_SET(ifp->link_params, LP_TE_METRIC))
644 set_linkparams_te_metric(lp, ifp->link_params->te_metric);
645 else
646 TLV_TYPE(lp->te_metric) = 0;
647
648 /* TE metric Extensions */
649 if (IS_PARAM_SET(ifp->link_params, LP_DELAY))
650 set_linkparams_av_delay(lp, ifp->link_params->av_delay, 0);
651 else
652 TLV_TYPE(lp->av_delay) = 0;
653
654 if (IS_PARAM_SET(ifp->link_params, LP_MM_DELAY))
655 set_linkparams_mm_delay(lp, ifp->link_params->min_delay,
656 ifp->link_params->max_delay, 0);
657 else
658 TLV_TYPE(lp->mm_delay) = 0;
659
660 if (IS_PARAM_SET(ifp->link_params, LP_DELAY_VAR))
661 set_linkparams_delay_var(lp, ifp->link_params->delay_var);
662 else
663 TLV_TYPE(lp->delay_var) = 0;
664
665 if (IS_PARAM_SET(ifp->link_params, LP_PKT_LOSS))
666 set_linkparams_pkt_loss(lp, ifp->link_params->pkt_loss, 0);
667 else
668 TLV_TYPE(lp->pkt_loss) = 0;
669
670 if (IS_PARAM_SET(ifp->link_params, LP_RES_BW))
671 set_linkparams_res_bw(lp, ifp->link_params->res_bw);
672 else
673 TLV_TYPE(lp->res_bw) = 0;
674
675 if (IS_PARAM_SET(ifp->link_params, LP_AVA_BW))
676 set_linkparams_ava_bw(lp, ifp->link_params->ava_bw);
677 else
678 TLV_TYPE(lp->ava_bw) = 0;
679
680 if (IS_PARAM_SET(ifp->link_params, LP_USE_BW))
681 set_linkparams_use_bw(lp, ifp->link_params->use_bw);
682 else
683 TLV_TYPE(lp->use_bw) = 0;
684
685 /* RFC5392 */
686 if (IS_PARAM_SET(ifp->link_params, LP_RMT_AS)) {
687 /* Flush LSA if it engaged and was previously a STD_TE one */
688 if (IS_STD_TE(lp->type)
689 && CHECK_FLAG(lp->flags, LPFLG_LSA_ENGAGED)) {
690 ote_debug(
691 "MPLS-TE (%s): Update IF: Switch from Standard LSA to INTER-AS for %s[%d/%d]",
692 __func__, ifp->name, lp->flags, lp->type);
693
694 ospf_mpls_te_lsa_schedule(lp, FLUSH_THIS_LSA);
695 /* Then, switch it to INTER-AS */
696 if (OspfMplsTE.inter_as == AS) {
697 lp->type = INTER_AS;
698 SET_FLAG(lp->flags, LPFLG_LSA_FLOOD_AS);
699 } else {
700 lp->type = INTER_AS;
701 UNSET_FLAG(lp->flags, LPFLG_LSA_FLOOD_AS);
702 lp->area = ospf_area_lookup_by_area_id(
703 ospf_lookup_by_vrf_id(VRF_DEFAULT),
704 OspfMplsTE.interas_areaid);
705 }
706 }
707 set_linkparams_inter_as(lp, ifp->link_params->rmt_ip,
708 ifp->link_params->rmt_as);
709 } else {
710 ote_debug(
711 "MPLS-TE (%s): Update IF: Switch from INTER-AS LSA to Standard for %s[%d/%d]",
712 __func__, ifp->name, lp->flags, lp->type);
713
714 /* reset inter-as TE params */
715 /* Flush LSA if it engaged and was previously an INTER_AS one */
716 if (IS_INTER_AS(lp->type)
717 && CHECK_FLAG(lp->flags, LPFLG_LSA_ENGAGED)) {
718 ospf_mpls_te_lsa_schedule(lp, FLUSH_THIS_LSA);
719 /* Then, switch it to Standard TE */
720 lp->flags = STD_TE;
721 UNSET_FLAG(lp->flags, LPFLG_LSA_FLOOD_AS);
722 }
723 unset_linkparams_inter_as(lp);
724 }
725 }
726
727 static void initialize_linkparams(struct mpls_te_link *lp)
728 {
729 struct interface *ifp = lp->ifp;
730 struct ospf_interface *oi = NULL;
731 struct route_node *rn;
732
733 ote_debug("MPLS-TE (%s): Initialize Link Parameters for interface %s",
734 __func__, ifp->name);
735
736 /* Search OSPF Interface parameters for this interface */
737 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
738
739 if ((oi = rn->info) == NULL)
740 continue;
741
742 if (oi->ifp == ifp)
743 break;
744 }
745
746 if ((oi == NULL) || (oi->ifp != ifp)) {
747 ote_debug(
748 "MPLS-TE (%s): Could not find corresponding OSPF Interface for %s",
749 __func__, ifp->name);
750 return;
751 }
752
753 /*
754 * Try to set initial values those can be derived from
755 * zebra-interface information.
756 */
757 set_linkparams_link_type(oi, lp);
758
759 /* Set local IP addr */
760 set_linkparams_lclif_ipaddr(lp, oi->address->u.prefix4);
761
762 /* Set Remote IP addr if Point to Point Interface */
763 if (oi->type == OSPF_IFTYPE_POINTOPOINT) {
764 struct prefix *pref = CONNECTED_PREFIX(oi->connected);
765 if (pref != NULL)
766 set_linkparams_rmtif_ipaddr(lp, pref->u.prefix4);
767 }
768
769 /* Keep Area information in combination with link parameters. */
770 lp->area = oi->area;
771
772 return;
773 }
774
775 static int is_mandated_params_set(struct mpls_te_link *lp)
776 {
777 int rc = 0;
778
779 if (ntohs(OspfMplsTE.router_addr.header.type) == 0) {
780 flog_warn(EC_OSPF_TE_UNEXPECTED,
781 "MPLS-TE (%s): Missing Router Address", __func__);
782 return rc;
783 }
784
785 if (ntohs(lp->link_type.header.type) == 0) {
786 flog_warn(EC_OSPF_TE_UNEXPECTED,
787 "MPLS-TE (%s): Missing Link Type", __func__);
788 return rc;
789 }
790
791 if (!IS_INTER_AS(lp->type) && (ntohs(lp->link_id.header.type) == 0)) {
792 flog_warn(EC_OSPF_TE_UNEXPECTED, "MPLS-TE (%s) Missing Link ID",
793 __func__);
794 return rc;
795 }
796
797 rc = 1;
798 return rc;
799 }
800
801 /*------------------------------------------------------------------------*
802 * Following are callback functions against generic Opaque-LSAs handling.
803 *------------------------------------------------------------------------*/
804
805 static int ospf_mpls_te_new_if(struct interface *ifp)
806 {
807 struct mpls_te_link *new;
808
809 ote_debug("MPLS-TE (%s): Add new %s interface %s to MPLS-TE list",
810 __func__, ifp->link_params ? "Active" : "Inactive",
811 ifp->name);
812
813 if (lookup_linkparams_by_ifp(ifp) != NULL)
814 return 0;
815
816 new = XCALLOC(MTYPE_OSPF_MPLS_TE, sizeof(struct mpls_te_link));
817
818 new->instance = get_mpls_te_instance_value();
819 new->ifp = ifp;
820 /* By default TE-Link is RFC3630 compatible flooding in Area and not
821 * active */
822 /* This default behavior will be adapted with call to
823 * ospf_mpls_te_update_if() */
824 new->type = STD_TE;
825 new->flags = LPFLG_LSA_INACTIVE;
826
827 /* Initialize Link Parameters from Interface */
828 initialize_linkparams(new);
829
830 /* Set TE Parameters from Interface */
831 update_linkparams(new);
832
833 /* Add Link Parameters structure to the list */
834 listnode_add(OspfMplsTE.iflist, new);
835
836 ote_debug("MPLS-TE (%s): Add new LP context for %s[%d/%d]", __func__,
837 ifp->name, new->flags, new->type);
838
839 /* Schedule Opaque-LSA refresh. */ /* XXX */
840 return 0;
841 }
842
843 static int ospf_mpls_te_del_if(struct interface *ifp)
844 {
845 struct mpls_te_link *lp;
846 int rc = -1;
847
848 if ((lp = lookup_linkparams_by_ifp(ifp)) != NULL) {
849 struct list *iflist = OspfMplsTE.iflist;
850
851 /* Dequeue listnode entry from the list. */
852 listnode_delete(iflist, lp);
853
854 XFREE(MTYPE_OSPF_MPLS_TE, lp);
855 }
856
857 /* Schedule Opaque-LSA refresh. */ /* XXX */
858
859 rc = 0;
860 return rc;
861 }
862
863 /* Main initialization / update function of the MPLS TE Link context */
864
865 /* Call when interface TE Link parameters are modified */
866 void ospf_mpls_te_update_if(struct interface *ifp)
867 {
868 struct mpls_te_link *lp;
869
870 ote_debug("MPLS-TE (%s): Update LSA parameters for interface %s [%s]",
871 __func__, ifp->name, HAS_LINK_PARAMS(ifp) ? "ON" : "OFF");
872
873 /* Get Link context from interface */
874 if ((lp = lookup_linkparams_by_ifp(ifp)) == NULL) {
875 flog_warn(
876 EC_OSPF_TE_UNEXPECTED,
877 "MPLS-TE (%s): Did not find Link Parameters context for interface %s",
878 __func__, ifp->name);
879 return;
880 }
881
882 /* Fulfill MPLS-TE Link TLV from Interface TE Link parameters */
883 if (HAS_LINK_PARAMS(ifp)) {
884 SET_FLAG(lp->flags, LPFLG_LSA_ACTIVE);
885
886 /* Update TE parameters */
887 update_linkparams(lp);
888
889 /* Finally Re-Originate or Refresh Opaque LSA if MPLS_TE is
890 * enabled */
891 if (OspfMplsTE.enabled)
892 if (lp->area != NULL) {
893 if (CHECK_FLAG(lp->flags, LPFLG_LSA_ENGAGED))
894 ospf_mpls_te_lsa_schedule(
895 lp, REFRESH_THIS_LSA);
896 else
897 ospf_mpls_te_lsa_schedule(
898 lp, REORIGINATE_THIS_LSA);
899 }
900 } else {
901 /* If MPLS TE is disable on this interface, flush LSA if it is
902 * already engaged */
903 if (CHECK_FLAG(lp->flags, LPFLG_LSA_ENGAGED))
904 ospf_mpls_te_lsa_schedule(lp, FLUSH_THIS_LSA);
905 else
906 /* Reset Activity flag */
907 lp->flags = LPFLG_LSA_INACTIVE;
908 }
909
910 return;
911 }
912
913 /*
914 * Just add interface and set available information. Other information
915 * and flooding of LSA will be done later when adjacency will be up
916 * See ospf_mpls_te_nsm_change() after
917 */
918 static void ospf_mpls_te_ism_change(struct ospf_interface *oi, int old_state)
919 {
920
921 struct mpls_te_link *lp;
922
923 lp = lookup_linkparams_by_ifp(oi->ifp);
924 if (lp == NULL) {
925 flog_warn(
926 EC_OSPF_TE_UNEXPECTED,
927 "MPLS-TE (%s): Cannot get linkparams from OI(%s)?",
928 __func__, IF_NAME(oi));
929 return;
930 }
931
932 if (oi->area == NULL || oi->area->ospf == NULL) {
933 flog_warn(
934 EC_OSPF_TE_UNEXPECTED,
935 "MPLS-TE (%s): Cannot refer to OSPF from OI(%s)?",
936 __func__, IF_NAME(oi));
937 return;
938 }
939
940 /* Keep Area information in combination with linkparams. */
941 lp->area = oi->area;
942
943 switch (oi->state) {
944 case ISM_PointToPoint:
945 case ISM_DROther:
946 case ISM_Backup:
947 case ISM_DR:
948 /* Set Link type and Local IP addr */
949 set_linkparams_link_type(oi, lp);
950 set_linkparams_lclif_ipaddr(lp, oi->address->u.prefix4);
951
952 break;
953 case ISM_Down:
954 /* Interface goes Down: Flush LSA if engaged */
955 if (CHECK_FLAG(lp->flags, LPFLG_LSA_ENGAGED)) {
956 ote_debug(
957 "MPLS-TE (%s): Interface %s goes down: flush LSA",
958 __func__, IF_NAME(oi));
959 ospf_mpls_te_lsa_schedule(lp, FLUSH_THIS_LSA);
960 return;
961 }
962 break;
963 default:
964 break;
965 }
966
967 ote_debug("MPLS-TE (%s): Update Link parameters for interface %s",
968 __func__, IF_NAME(oi));
969
970 return;
971 }
972
973 /*
974 * Complete TE info and schedule LSA flooding
975 * Link-ID and Remote IP address must be set with neighbor info
976 * which are only valid once NSM state is FULL
977 */
978 static void ospf_mpls_te_nsm_change(struct ospf_neighbor *nbr, int old_state)
979 {
980 struct ospf_interface *oi = nbr->oi;
981 struct mpls_te_link *lp;
982
983 /* Process Neighbor only when its state is NSM Full */
984 if (nbr->state != NSM_Full)
985 return;
986
987 /* Get interface information for Traffic Engineering */
988 lp = lookup_linkparams_by_ifp(oi->ifp);
989 if (lp == NULL) {
990 flog_warn(
991 EC_OSPF_TE_UNEXPECTED,
992 "MPLS-TE (%s): Cannot get linkparams from OI(%s)?",
993 __func__, IF_NAME(oi));
994 return;
995 }
996
997 if (oi->area == NULL || oi->area->ospf == NULL) {
998 flog_warn(
999 EC_OSPF_TE_UNEXPECTED,
1000 "MPLS-TE (%s): Cannot refer to OSPF from OI(%s)?",
1001 __func__, IF_NAME(oi));
1002 return;
1003 }
1004
1005 /* Flush TE Opaque LSA if Neighbor State goes Down or Deleted */
1006 if (OspfMplsTE.enabled
1007 && (nbr->state == NSM_Down || nbr->state == NSM_Deleted)) {
1008 if (CHECK_FLAG(lp->flags, EXT_LPFLG_LSA_ENGAGED)) {
1009 ote_debug(
1010 "MPLS-TE (%s): Interface %s goes down: flush LSA",
1011 __func__, IF_NAME(oi));
1012 ospf_mpls_te_lsa_schedule(lp, FLUSH_THIS_LSA);
1013 }
1014 return;
1015 }
1016
1017 /* Keep Area information in combination with SR info. */
1018 lp->area = oi->area;
1019
1020 /*
1021 * The Link ID is identical to the contents of the Link ID field
1022 * in the Router LSA for these link types.
1023 */
1024 switch (oi->state) {
1025 case ISM_PointToPoint:
1026 /* Set Link ID with neighbor Router ID */
1027 set_linkparams_link_id(lp, nbr->router_id);
1028 /* Set Remote IP address */
1029 set_linkparams_rmtif_ipaddr(lp, nbr->address.u.prefix4);
1030 break;
1031
1032 case ISM_DR:
1033 case ISM_DROther:
1034 case ISM_Backup:
1035 /* Set Link ID with the Designated Router ID */
1036 set_linkparams_link_id(lp, DR(oi));
1037 break;
1038
1039 case ISM_Down:
1040 /* State goes Down: Flush LSA if engaged */
1041 if (OspfMplsTE.enabled
1042 && CHECK_FLAG(lp->flags, LPFLG_LSA_ENGAGED)) {
1043 ote_debug(
1044 "MPLS-TE (%s): Interface %s goes down: flush LSA",
1045 __func__, IF_NAME(oi));
1046 ospf_mpls_te_lsa_schedule(lp, FLUSH_THIS_LSA);
1047 }
1048 return;
1049 default:
1050 break;
1051 }
1052
1053 ote_debug("MPLS-TE (%s): Add Link-ID %pI4 for interface %s ", __func__,
1054 &lp->link_id.value, oi->ifp->name);
1055
1056 /* Try to Schedule LSA */
1057 if (OspfMplsTE.enabled) {
1058 if (CHECK_FLAG(lp->flags, LPFLG_LSA_ENGAGED))
1059 ospf_mpls_te_lsa_schedule(lp, REFRESH_THIS_LSA);
1060 else
1061 ospf_mpls_te_lsa_schedule(lp, REORIGINATE_THIS_LSA);
1062 }
1063 return;
1064 }
1065
1066 /*------------------------------------------------------------------------*
1067 * Following are OSPF protocol processing functions for MPLS-TE LSA.
1068 *------------------------------------------------------------------------*/
1069
1070 static void build_tlv_header(struct stream *s, struct tlv_header *tlvh)
1071 {
1072 stream_put(s, tlvh, sizeof(struct tlv_header));
1073 return;
1074 }
1075
1076 static void build_router_tlv(struct stream *s)
1077 {
1078 struct tlv_header *tlvh = &OspfMplsTE.router_addr.header;
1079 if (ntohs(tlvh->type) != 0) {
1080 build_tlv_header(s, tlvh);
1081 stream_put(s, TLV_DATA(tlvh), TLV_BODY_SIZE(tlvh));
1082 }
1083 return;
1084 }
1085
1086 static void build_link_subtlv(struct stream *s, struct tlv_header *tlvh)
1087 {
1088
1089 if ((tlvh != NULL) && (ntohs(tlvh->type) != 0)) {
1090 build_tlv_header(s, tlvh);
1091 stream_put(s, TLV_DATA(tlvh), TLV_BODY_SIZE(tlvh));
1092 }
1093 return;
1094 }
1095
1096 static void build_link_tlv(struct stream *s, struct mpls_te_link *lp)
1097 {
1098 set_linkparams_link_header(lp);
1099 build_tlv_header(s, &lp->link_header.header);
1100
1101 build_link_subtlv(s, &lp->link_type.header);
1102 build_link_subtlv(s, &lp->link_id.header);
1103 build_link_subtlv(s, &lp->lclif_ipaddr.header);
1104 build_link_subtlv(s, &lp->rmtif_ipaddr.header);
1105 build_link_subtlv(s, &lp->te_metric.header);
1106 build_link_subtlv(s, &lp->max_bw.header);
1107 build_link_subtlv(s, &lp->max_rsv_bw.header);
1108 build_link_subtlv(s, &lp->unrsv_bw.header);
1109 build_link_subtlv(s, &lp->rsc_clsclr.header);
1110 build_link_subtlv(s, &lp->lrrid.header);
1111 build_link_subtlv(s, &lp->llri.header);
1112 build_link_subtlv(s, &lp->rip.header);
1113 build_link_subtlv(s, &lp->ras.header);
1114 build_link_subtlv(s, &lp->av_delay.header);
1115 build_link_subtlv(s, &lp->mm_delay.header);
1116 build_link_subtlv(s, &lp->delay_var.header);
1117 build_link_subtlv(s, &lp->pkt_loss.header);
1118 build_link_subtlv(s, &lp->res_bw.header);
1119 build_link_subtlv(s, &lp->ava_bw.header);
1120 build_link_subtlv(s, &lp->use_bw.header);
1121
1122 return;
1123 }
1124
1125 static void ospf_mpls_te_lsa_body_set(struct stream *s, struct mpls_te_link *lp)
1126 {
1127 /*
1128 * The router address TLV is type 1, and ... It must appear in exactly
1129 * one Traffic Engineering LSA originated by a router but not in
1130 * Inter-AS TLV.
1131 */
1132 if (!IS_INTER_AS(lp->type))
1133 build_router_tlv(s);
1134
1135 /*
1136 * Only one Link TLV shall be carried in each LSA, allowing for fine
1137 * granularity changes in topology.
1138 */
1139 build_link_tlv(s, lp);
1140 return;
1141 }
1142
1143 /* Create new opaque-LSA. */
1144 static struct ospf_lsa *ospf_mpls_te_lsa_new(struct ospf *ospf,
1145 struct ospf_area *area,
1146 struct mpls_te_link *lp)
1147 {
1148 struct stream *s;
1149 struct lsa_header *lsah;
1150 struct ospf_lsa *new = NULL;
1151 uint8_t options, lsa_type = 0;
1152 struct in_addr lsa_id;
1153 uint32_t tmp;
1154 uint16_t length;
1155
1156 /* Create a stream for LSA. */
1157 s = stream_new(OSPF_MAX_LSA_SIZE);
1158 lsah = (struct lsa_header *)STREAM_DATA(s);
1159
1160 options = OSPF_OPTION_O; /* Don't forget this :-) */
1161
1162 /* Set opaque-LSA header fields depending of the type of RFC */
1163 if (IS_INTER_AS(lp->type)) {
1164 if (IS_FLOOD_AS(lp->flags)) {
1165 /* Enable AS external as we flood Inter-AS with Opaque
1166 * Type 11
1167 */
1168 options |= OSPF_OPTION_E;
1169 lsa_type = OSPF_OPAQUE_AS_LSA;
1170 } else {
1171 options |= LSA_OPTIONS_GET(
1172 area); /* Get area default option */
1173 options |= LSA_OPTIONS_NSSA_GET(area);
1174 lsa_type = OSPF_OPAQUE_AREA_LSA;
1175 }
1176 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_INTER_AS_LSA, lp->instance);
1177 lsa_id.s_addr = htonl(tmp);
1178
1179 if (!ospf) {
1180 stream_free(s);
1181 return NULL;
1182 }
1183
1184 lsa_header_set(s, options, lsa_type, lsa_id, ospf->router_id);
1185 } else {
1186 options |= LSA_OPTIONS_GET(area); /* Get area default option */
1187 options |= LSA_OPTIONS_NSSA_GET(area);
1188 lsa_type = OSPF_OPAQUE_AREA_LSA;
1189 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_TRAFFIC_ENGINEERING_LSA,
1190 lp->instance);
1191 lsa_id.s_addr = htonl(tmp);
1192 lsa_header_set(s, options, lsa_type, lsa_id,
1193 area->ospf->router_id);
1194 }
1195
1196 ote_debug(
1197 "MPLS-TE (%s): LSA[Type%d:%pI4]: Create an Opaque-LSA/MPLS-TE instance",
1198 __func__, lsa_type, &lsa_id);
1199
1200 /* Set opaque-LSA body fields. */
1201 ospf_mpls_te_lsa_body_set(s, lp);
1202
1203 /* Set length. */
1204 length = stream_get_endp(s);
1205 lsah->length = htons(length);
1206
1207 /* Now, create an OSPF LSA instance. */
1208 new = ospf_lsa_new_and_data(length);
1209
1210 new->vrf_id = ospf->vrf_id;
1211 if (area && area->ospf)
1212 new->vrf_id = area->ospf->vrf_id;
1213 new->area = area;
1214 SET_FLAG(new->flags, OSPF_LSA_SELF);
1215 memcpy(new->data, lsah, length);
1216 stream_free(s);
1217
1218 return new;
1219 }
1220
1221 static int ospf_mpls_te_lsa_originate1(struct ospf_area *area,
1222 struct mpls_te_link *lp)
1223 {
1224 struct ospf_lsa *new = NULL;
1225 int rc = -1;
1226
1227 /* Create new Opaque-LSA/MPLS-TE instance. */
1228 new = ospf_mpls_te_lsa_new(area->ospf, area, lp);
1229 if (new == NULL) {
1230 flog_warn(EC_OSPF_TE_UNEXPECTED,
1231 "MPLS-TE (%s): ospf_mpls_te_lsa_new() ?", __func__);
1232 return rc;
1233 }
1234
1235 /* Install this LSA into LSDB. */
1236 if (ospf_lsa_install(area->ospf, NULL /*oi*/, new) == NULL) {
1237 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
1238 "MPLS-TE (%s): ospf_lsa_install() ?", __func__);
1239 ospf_lsa_unlock(&new);
1240 return rc;
1241 }
1242
1243 /* Now this link-parameter entry has associated LSA. */
1244 SET_FLAG(lp->flags, LPFLG_LSA_ENGAGED);
1245 /* Update new LSA origination count. */
1246 area->ospf->lsa_originate_count++;
1247
1248 /* Flood new LSA through area. */
1249 ospf_flood_through_area(area, NULL /*nbr*/, new);
1250
1251 ote_debug(
1252 "MPLS-TE (%s): LSA[Type%d:%pI4]: Originate Opaque-LSA/MPLS-TE: Area(%pI4), Link(%s)",
1253 __func__, new->data->type, &new->data->id, &area->area_id,
1254 lp->ifp->name);
1255 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1256 ospf_lsa_header_dump(new->data);
1257
1258 rc = 0;
1259 return rc;
1260 }
1261
1262 static int ospf_mpls_te_lsa_originate_area(void *arg)
1263 {
1264 struct ospf_area *area = (struct ospf_area *)arg;
1265 struct listnode *node, *nnode;
1266 struct mpls_te_link *lp;
1267 int rc = -1;
1268
1269 if (!OspfMplsTE.enabled) {
1270 ote_debug("MPLS-TE (%s): MPLS-TE is disabled now.", __func__);
1271 rc = 0; /* This is not an error case. */
1272 return rc;
1273 }
1274
1275 for (ALL_LIST_ELEMENTS(OspfMplsTE.iflist, node, nnode, lp)) {
1276 /* Process only enabled LSA with area scope flooding */
1277 if (!CHECK_FLAG(lp->flags, LPFLG_LSA_ACTIVE)
1278 || IS_FLOOD_AS(lp->flags))
1279 continue;
1280
1281 if (lp->area == NULL)
1282 continue;
1283
1284 if (!IPV4_ADDR_SAME(&lp->area->area_id, &area->area_id))
1285 continue;
1286
1287 if (CHECK_FLAG(lp->flags, LPFLG_LSA_ENGAGED)) {
1288 if (CHECK_FLAG(lp->flags, LPFLG_LSA_FORCED_REFRESH)) {
1289 UNSET_FLAG(lp->flags, LPFLG_LSA_FORCED_REFRESH);
1290 ote_debug(
1291 "MPLS-TE (%s): Refresh instead of Originate",
1292 __func__);
1293 ospf_mpls_te_lsa_schedule(lp, REFRESH_THIS_LSA);
1294 }
1295 continue;
1296 }
1297
1298 if (!is_mandated_params_set(lp)) {
1299 ote_debug(
1300 "MPLS-TE (%s): Link(%s) lacks some mandated MPLS-TE parameters.",
1301 __func__, lp->ifp ? lp->ifp->name : "?");
1302 continue;
1303 }
1304
1305 /* Ok, let's try to originate an LSA for this area and Link. */
1306 ote_debug(
1307 "MPLS-TE (%s): Let's finally reoriginate the LSA %d through the Area %pI4 for Link %s",
1308 __func__, lp->instance, &area->area_id,
1309 lp->ifp ? lp->ifp->name : "?");
1310 if (ospf_mpls_te_lsa_originate1(area, lp) != 0)
1311 return rc;
1312 }
1313
1314 rc = 0;
1315 return rc;
1316 }
1317
1318 static int ospf_mpls_te_lsa_originate2(struct ospf *top,
1319 struct mpls_te_link *lp)
1320 {
1321 struct ospf_lsa *new;
1322 int rc = -1;
1323
1324 /* Create new Opaque-LSA/Inter-AS instance. */
1325 new = ospf_mpls_te_lsa_new(top, NULL, lp);
1326 if (new == NULL) {
1327 flog_warn(EC_OSPF_LSA_UNEXPECTED,
1328 "MPLS-TE (%s): ospf_router_info_lsa_new() ?",
1329 __func__);
1330 return rc;
1331 }
1332 new->vrf_id = top->vrf_id;
1333
1334 /* Install this LSA into LSDB. */
1335 if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
1336 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
1337 "MPLS-TE (%s): ospf_lsa_install() ?", __func__);
1338 ospf_lsa_unlock(&new);
1339 return rc;
1340 }
1341
1342 /* Now this Router Info parameter entry has associated LSA. */
1343 SET_FLAG(lp->flags, LPFLG_LSA_ENGAGED);
1344 /* Update new LSA origination count. */
1345 top->lsa_originate_count++;
1346
1347 /* Flood new LSA through AS. */
1348 ospf_flood_through_as(top, NULL /*nbr */, new);
1349
1350 ote_debug(
1351 "MPLS-TE (%s): LSA[Type%d:%pI4]: Originate Opaque-LSA/MPLS-TE Inter-AS",
1352 __func__, new->data->type, &new->data->id);
1353 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1354 ospf_lsa_header_dump(new->data);
1355
1356
1357 rc = 0;
1358 return rc;
1359 }
1360
1361 static int ospf_mpls_te_lsa_originate_as(void *arg)
1362 {
1363 struct ospf *top;
1364 struct ospf_area *area;
1365 struct listnode *node, *nnode;
1366 struct mpls_te_link *lp;
1367 int rc = -1;
1368
1369 if ((!OspfMplsTE.enabled) || (OspfMplsTE.inter_as == Off)) {
1370 ote_debug("MPLS-TE (%s): Inter-AS is disabled for now",
1371 __func__);
1372 rc = 0; /* This is not an error case. */
1373 return rc;
1374 }
1375
1376 for (ALL_LIST_ELEMENTS(OspfMplsTE.iflist, node, nnode, lp)) {
1377 /* Process only enabled INTER_AS Links or Pseudo-Links */
1378 if (!CHECK_FLAG(lp->flags, LPFLG_LSA_ACTIVE)
1379 || !CHECK_FLAG(lp->flags, LPFLG_LSA_FLOOD_AS)
1380 || !IS_INTER_AS(lp->type))
1381 continue;
1382
1383 if (CHECK_FLAG(lp->flags, LPFLG_LSA_ENGAGED)) {
1384 if (CHECK_FLAG(lp->flags, LPFLG_LSA_FORCED_REFRESH)) {
1385 UNSET_FLAG(lp->flags, LPFLG_LSA_FORCED_REFRESH);
1386 ospf_mpls_te_lsa_schedule(lp, REFRESH_THIS_LSA);
1387 }
1388 continue;
1389 }
1390
1391 if (!is_mandated_params_set(lp)) {
1392 flog_warn(
1393 EC_OSPF_TE_UNEXPECTED,
1394 "MPLS-TE (%s): Link(%s) lacks some mandated MPLS-TE parameters.",
1395 __func__, lp->ifp ? lp->ifp->name : "?");
1396 continue;
1397 }
1398
1399 /* Ok, let's try to originate an LSA for this AS and Link. */
1400 ote_debug(
1401 "MPLS-TE (%s): Let's finally re-originate the Inter-AS LSA %d through the %s for Link %s",
1402 __func__, lp->instance,
1403 IS_FLOOD_AS(lp->flags) ? "AS" : "Area",
1404 lp->ifp ? lp->ifp->name : "Unknown");
1405
1406 if (IS_FLOOD_AS(lp->flags)) {
1407 top = (struct ospf *)arg;
1408 ospf_mpls_te_lsa_originate2(top, lp);
1409 } else {
1410 area = (struct ospf_area *)arg;
1411 ospf_mpls_te_lsa_originate1(area, lp);
1412 }
1413 }
1414
1415 rc = 0;
1416 return rc;
1417 }
1418
1419 /*
1420 * As Inter-AS LSA must be registered with both AREA and AS flooding, and
1421 * because all origination callback functions are call (disregarding the Opaque
1422 * LSA type and Flooding scope) it is necessary to determine which flooding
1423 * scope is associated with the LSA origination as parameter is of type void and
1424 * must be cast to struct *ospf for AS flooding and to struct *ospf_area for
1425 * Area flooding.
1426 */
1427 static int ospf_mpls_te_lsa_inter_as_as(void *arg)
1428 {
1429 if (OspfMplsTE.inter_as == AS)
1430 return ospf_mpls_te_lsa_originate_as(arg);
1431 else
1432 return 0;
1433 }
1434
1435 static int ospf_mpls_te_lsa_inter_as_area(void *arg)
1436 {
1437 if (OspfMplsTE.inter_as == Area)
1438 return ospf_mpls_te_lsa_originate_area(arg);
1439 else
1440 return 0;
1441 }
1442
1443 static struct ospf_lsa *ospf_mpls_te_lsa_refresh(struct ospf_lsa *lsa)
1444 {
1445 struct mpls_te_link *lp;
1446 struct ospf_area *area = lsa->area;
1447 struct ospf *top;
1448 struct ospf_lsa *new = NULL;
1449
1450 if (!OspfMplsTE.enabled) {
1451 /*
1452 * This LSA must have flushed before due to MPLS-TE status
1453 * change.
1454 * It seems a slip among routers in the routing domain.
1455 */
1456 ote_debug("MPLS-TE (%s): MPLS-TE is disabled now", __func__);
1457 lsa->data->ls_age =
1458 htons(OSPF_LSA_MAXAGE); /* Flush it anyway. */
1459 }
1460
1461 /* At first, resolve lsa/lp relationship. */
1462 if ((lp = lookup_linkparams_by_instance(lsa)) == NULL) {
1463 flog_warn(EC_OSPF_TE_UNEXPECTED,
1464 "MPLS-TE (%s): Invalid parameter?", __func__);
1465 lsa->data->ls_age =
1466 htons(OSPF_LSA_MAXAGE); /* Flush it anyway. */
1467 ospf_opaque_lsa_flush_schedule(lsa);
1468 return NULL;
1469 }
1470
1471 /* Check if lp was not disable in the interval */
1472 if (!CHECK_FLAG(lp->flags, LPFLG_LSA_ACTIVE)) {
1473 flog_warn(EC_OSPF_TE_UNEXPECTED,
1474 "MPLS-TE (%s): lp was disabled: Flush it!", __func__);
1475 lsa->data->ls_age =
1476 htons(OSPF_LSA_MAXAGE); /* Flush it anyway. */
1477 }
1478
1479 /* If the lsa's age reached to MaxAge, start flushing procedure. */
1480 if (IS_LSA_MAXAGE(lsa)) {
1481 UNSET_FLAG(lp->flags, LPFLG_LSA_ENGAGED);
1482 ospf_opaque_lsa_flush_schedule(lsa);
1483 return NULL;
1484 }
1485 top = ospf_lookup_by_vrf_id(lsa->vrf_id);
1486 /* Create new Opaque-LSA/MPLS-TE instance. */
1487 new = ospf_mpls_te_lsa_new(top, area, lp);
1488 if (new == NULL) {
1489 flog_warn(EC_OSPF_TE_UNEXPECTED,
1490 "MPLS-TE (%s): ospf_mpls_te_lsa_new() ?", __func__);
1491 return NULL;
1492 }
1493 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
1494
1495 /* Install this LSA into LSDB. */
1496 /* Given "lsa" will be freed in the next function. */
1497 /* As area could be NULL i.e. when using OPAQUE_LSA_AS, we prefer to use
1498 * ospf_lookup() to get ospf instance */
1499 if (area)
1500 top = area->ospf;
1501
1502 if (ospf_lsa_install(top, NULL /*oi */, new) == NULL) {
1503 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
1504 "MPLS-TE (%s): ospf_lsa_install() ?", __func__);
1505 ospf_lsa_unlock(&new);
1506 return NULL;
1507 }
1508
1509 /* Flood updated LSA through AS or Area depending of the RFC of the link
1510 */
1511 if (IS_FLOOD_AS(lp->flags))
1512 ospf_flood_through_as(top, NULL, new);
1513 else
1514 ospf_flood_through_area(area, NULL /*nbr*/, new);
1515
1516 /* Debug logging. */
1517 ote_debug("MPLS-TE (%s): LSA[Type%d:%pI4]: Refresh Opaque-LSA/MPLS-TE",
1518 __func__, new->data->type, &new->data->id);
1519 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1520 ospf_lsa_header_dump(new->data);
1521
1522 return new;
1523 }
1524
1525 void ospf_mpls_te_lsa_schedule(struct mpls_te_link *lp, enum lsa_opcode opcode)
1526 {
1527 struct ospf_lsa lsa;
1528 struct lsa_header lsah;
1529 struct ospf *top;
1530 uint32_t tmp;
1531
1532 memset(&lsa, 0, sizeof(lsa));
1533 memset(&lsah, 0, sizeof(lsah));
1534 top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
1535
1536 /* Check if the pseudo link is ready to flood */
1537 if (!CHECK_FLAG(lp->flags, LPFLG_LSA_ACTIVE))
1538 return;
1539
1540 ote_debug("MPLS-TE (%s): Schedule %s%s%s LSA for interface %s",
1541 __func__,
1542 opcode == REORIGINATE_THIS_LSA ? "Re-Originate" : "",
1543 opcode == REFRESH_THIS_LSA ? "Refresh" : "",
1544 opcode == FLUSH_THIS_LSA ? "Flush" : "",
1545 lp->ifp ? lp->ifp->name : "-");
1546
1547 lsa.area = lp->area;
1548 lsa.data = &lsah;
1549 if (IS_FLOOD_AS(lp->flags)) {
1550 lsah.type = OSPF_OPAQUE_AS_LSA;
1551 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_INTER_AS_LSA, lp->instance);
1552 lsah.id.s_addr = htonl(tmp);
1553 } else {
1554 lsah.type = OSPF_OPAQUE_AREA_LSA;
1555 if (IS_INTER_AS(lp->type)) {
1556 /* Set the area context if not know */
1557 if (lp->area == NULL)
1558 lp->area = ospf_area_lookup_by_area_id(
1559 top, OspfMplsTE.interas_areaid);
1560 /* Unable to set the area context. Abort! */
1561 if (lp->area == NULL) {
1562 flog_warn(
1563 EC_OSPF_TE_UNEXPECTED,
1564 "MPLS-TE (%s): Area context is null. Abort !",
1565 __func__);
1566 return;
1567 }
1568 tmp = SET_OPAQUE_LSID(OPAQUE_TYPE_INTER_AS_LSA,
1569 lp->instance);
1570 } else
1571 tmp = SET_OPAQUE_LSID(
1572 OPAQUE_TYPE_TRAFFIC_ENGINEERING_LSA,
1573 lp->instance);
1574 lsah.id.s_addr = htonl(tmp);
1575 }
1576
1577 switch (opcode) {
1578 case REORIGINATE_THIS_LSA:
1579 if (IS_FLOOD_AS(lp->flags)) {
1580 ospf_opaque_lsa_reoriginate_schedule(
1581 (void *)top, OSPF_OPAQUE_AS_LSA,
1582 OPAQUE_TYPE_INTER_AS_LSA);
1583 } else {
1584 if (IS_INTER_AS(lp->type))
1585 ospf_opaque_lsa_reoriginate_schedule(
1586 (void *)lp->area, OSPF_OPAQUE_AREA_LSA,
1587 OPAQUE_TYPE_INTER_AS_LSA);
1588 else
1589 ospf_opaque_lsa_reoriginate_schedule(
1590 (void *)lp->area, OSPF_OPAQUE_AREA_LSA,
1591 OPAQUE_TYPE_TRAFFIC_ENGINEERING_LSA);
1592 }
1593 break;
1594 case REFRESH_THIS_LSA:
1595 ospf_opaque_lsa_refresh_schedule(&lsa);
1596 break;
1597 case FLUSH_THIS_LSA:
1598 /* Reset Activity flag */
1599 lp->flags = LPFLG_LSA_INACTIVE;
1600 ospf_opaque_lsa_flush_schedule(&lsa);
1601 break;
1602 default:
1603 flog_warn(EC_OSPF_TE_UNEXPECTED,
1604 "MPLS-TE (%s): Unknown opcode (%u)", __func__,
1605 opcode);
1606 break;
1607 }
1608 }
1609
1610 /**
1611 * ------------------------------------------------------
1612 * Following are Link State Data Base control functions.
1613 * ------------------------------------------------------
1614 */
1615
1616 /**
1617 * Get Vertex from TED by the router which advertised the LSA. A new Vertex and
1618 * associated Link State Node are created if Vertex is not found.
1619 *
1620 * @param ted Link State Traffic Engineering Database
1621 * @param lsa OSPF Link State Advertisement
1622 *
1623 * @return Link State Vertex
1624 */
1625 static struct ls_vertex *get_vertex(struct ls_ted *ted, struct ospf_lsa *lsa)
1626 {
1627 struct ls_node_id lnid;
1628 struct ls_node *lnode;
1629 struct ls_vertex *vertex;
1630
1631 /* Sanity Check */
1632 if (!ted || !lsa || !lsa->data || !lsa->area)
1633 return NULL;
1634
1635 /* Search if a Link State Vertex already exist */
1636 lnid.origin = OSPFv2;
1637 lnid.id.ip.addr = lsa->data->adv_router;
1638 lnid.id.ip.area_id = lsa->area->area_id;
1639 vertex = ls_find_vertex_by_id(ted, lnid);
1640
1641 /* Create Node & Vertex in the Link State Date Base if not found */
1642 if (!vertex) {
1643 const struct in_addr inaddr_any = {.s_addr = INADDR_ANY};
1644
1645 lnode = ls_node_new(lnid, inaddr_any, in6addr_any);
1646 snprintfrr(lnode->name, MAX_NAME_LENGTH, "%pI4",
1647 &lnid.id.ip.addr);
1648 vertex = ls_vertex_add(ted, lnode);
1649 }
1650
1651 if (IS_LSA_SELF(lsa))
1652 ted->self = vertex;
1653
1654 return vertex;
1655 }
1656
1657 /**
1658 * Get Edge from TED by Link State Attribute ID. A new Edge and associated Link
1659 * State Attributes are created if not found.
1660 *
1661 * @param ted Link State Traffic Engineering Database
1662 * @param adv Link State Node ID of router which advertised Edge
1663 * @param link_id Link State Attribute ID
1664 *
1665 * @return Link State Edge
1666 */
1667 static struct ls_edge *get_edge(struct ls_ted *ted, struct ls_node_id adv,
1668 struct in_addr link_id)
1669 {
1670 uint64_t key;
1671 struct ls_edge *edge;
1672 struct ls_attributes *attr;
1673
1674 /* Search Edge that corresponds to the Link ID */
1675 key = ((uint64_t)ntohl(link_id.s_addr)) & 0xffffffff;
1676 edge = ls_find_edge_by_key(ted, key);
1677
1678 /* Create new one if not exist */
1679 if (!edge) {
1680 attr = ls_attributes_new(adv, link_id, in6addr_any, 0);
1681 edge = ls_edge_add(ted, attr);
1682 }
1683
1684 return edge;
1685 }
1686
1687 /**
1688 * Export Link State information to consumer daemon through ZAPI Link State
1689 * Opaque Message.
1690 *
1691 * @param type Type of Link State Element i.e. Vertex, Edge or Subnet
1692 * @param link_state Pointer to Link State Vertex, Edge or Subnet
1693 *
1694 * @return 0 if success, -1 otherwise
1695 */
1696 static int ospf_te_export(uint8_t type, void *link_state)
1697 {
1698 struct ls_message msg = {};
1699 int rc = 0;
1700
1701 if (!OspfMplsTE.export)
1702 return rc;
1703
1704 switch (type) {
1705 case LS_MSG_TYPE_NODE:
1706 ls_vertex2msg(&msg, (struct ls_vertex *)link_state);
1707 rc = ls_send_msg(zclient, &msg, NULL);
1708 break;
1709 case LS_MSG_TYPE_ATTRIBUTES:
1710 ls_edge2msg(&msg, (struct ls_edge *)link_state);
1711 rc = ls_send_msg(zclient, &msg, NULL);
1712 break;
1713 case LS_MSG_TYPE_PREFIX:
1714 ls_subnet2msg(&msg, (struct ls_subnet *)link_state);
1715 rc = ls_send_msg(zclient, &msg, NULL);
1716 break;
1717 default:
1718 rc = -1;
1719 break;
1720 }
1721
1722 return rc;
1723 }
1724
1725 /**
1726 * Update Link State Edge & Attributes from the given Link State Attributes ID
1727 * and metric. This function is called when parsing Router LSA.
1728 *
1729 * @param ted Link State Traffic Engineering Database
1730 * @param vertex Vertex where the Edge is attached as source
1731 * @param link_data Link State Edge ID
1732 * @param metric Standard metric attached to this Edge
1733 */
1734 static void ospf_te_update_link(struct ls_ted *ted, struct ls_vertex *vertex,
1735 struct in_addr link_data, uint8_t metric)
1736 {
1737 struct ls_edge *edge;
1738 struct ls_attributes *attr;
1739
1740 /* Sanity check */
1741 if (!ted || !vertex || !vertex->node)
1742 return;
1743
1744 /* Get Corresponding Edge from Link State Data Base */
1745 edge = get_edge(ted, vertex->node->adv, link_data);
1746 attr = edge->attributes;
1747
1748 /* re-attached edge to vertex if needed */
1749 if (!edge->source)
1750 edge->source = vertex;
1751
1752 /* Check if it is just an LSA refresh */
1753 if ((CHECK_FLAG(attr->flags, LS_ATTR_METRIC)
1754 && (attr->metric == metric))) {
1755 edge->status = SYNC;
1756 return;
1757 }
1758
1759 /* Update metric value */
1760 attr->metric = metric;
1761 SET_FLAG(attr->flags, LS_ATTR_METRIC);
1762 if (edge->status != NEW)
1763 edge->status = UPDATE;
1764
1765 ote_debug(" |- %s Edge %pI4 with metric %d",
1766 edge->status == NEW ? "Add" : "Update", &attr->standard.local,
1767 attr->metric);
1768
1769 /* Export Link State Edge */
1770 ospf_te_export(LS_MSG_TYPE_ATTRIBUTES, edge);
1771 edge->status = SYNC;
1772 }
1773
1774 /**
1775 * Update Link State Subnet & Prefix from the given prefix and metric. This
1776 * function is called when parsing Router LSA.
1777 *
1778 * @param ted Link State Traffic Engineering Database
1779 * @param vertex Vertex where the Edge is attached as source
1780 * @param p Prefix associated to the Subnet
1781 * @param metric Standard metric attached to this Edge
1782 */
1783 static void ospf_te_update_subnet(struct ls_ted *ted, struct ls_vertex *vertex,
1784 struct prefix p, uint8_t metric)
1785 {
1786 struct ls_subnet *subnet;
1787 struct ls_prefix *ls_pref;
1788
1789 /* Search if there is a Subnet for this prefix */
1790 subnet = ls_find_subnet(ted, p);
1791
1792 /* If found a Subnet, check if it is attached to this Vertex */
1793 if (subnet) {
1794 /* Re-attach the subnet to the vertex if necessary */
1795 if (subnet->vertex != vertex) {
1796 subnet->vertex = vertex;
1797 listnode_add_sort_nodup(vertex->prefixes, subnet);
1798 }
1799 /* Check if it is a simple refresh */
1800 ls_pref = subnet->ls_pref;
1801 if ((CHECK_FLAG(ls_pref->flags, LS_PREF_METRIC))
1802 && (ls_pref->metric == metric)) {
1803 subnet->status = SYNC;
1804 return;
1805 }
1806 ls_pref->metric = metric;
1807 SET_FLAG(ls_pref->flags, LS_PREF_METRIC);
1808 subnet->status = UPDATE;
1809 } else {
1810 /* Create new Link State Prefix */
1811 ls_pref = ls_prefix_new(vertex->node->adv, p);
1812 ls_pref->metric = metric;
1813 SET_FLAG(ls_pref->flags, LS_PREF_METRIC);
1814 /* and add it to the TED */
1815 subnet = ls_subnet_add(ted, ls_pref);
1816 }
1817
1818 ote_debug(" |- %s subnet %pFX with metric %d",
1819 subnet->status == NEW ? "Add" : "Update", &subnet->key,
1820 ls_pref->metric);
1821
1822 /* Export Link State Subnet */
1823 ospf_te_export(LS_MSG_TYPE_PREFIX, subnet);
1824 subnet->status = SYNC;
1825 }
1826
1827 /**
1828 * Delete Subnet that correspond to the given IPv4 address and export deletion
1829 * information before removal. Prefix length is fixed to IPV4_MAX_BITLEN.
1830 *
1831 * @param ted Links State Database
1832 * @param addr IPv4 address
1833 */
1834 static void ospf_te_delete_subnet(struct ls_ted *ted, struct in_addr addr)
1835 {
1836 struct prefix p;
1837 struct ls_subnet *subnet;
1838
1839 /* Search subnet that correspond to the address/32 as prefix */
1840 p.family = AF_INET;
1841 p.prefixlen = IPV4_MAX_BITLEN;
1842 p.u.prefix4 = addr;
1843 subnet = ls_find_subnet(ted, p);
1844
1845 /* Remove subnet if found */
1846 if (subnet) {
1847 subnet->status = DELETE;
1848 ospf_te_export(LS_MSG_TYPE_PREFIX, subnet);
1849 ls_subnet_del_all(ted, subnet);
1850 }
1851 }
1852
1853 /**
1854 * Parse Router LSA. This function will create or update corresponding Vertex,
1855 * Edge and Subnet. It also remove Edge and Subnet if they are marked as Orphan
1856 * once Router LSA is parsed.
1857 *
1858 * @param ted Link State Traffic Engineering Database
1859 * @param lsa OSPF Link State Advertisement
1860 *
1861 * @return 0 if success, -1 otherwise
1862 */
1863 static int ospf_te_parse_router_lsa(struct ls_ted *ted, struct ospf_lsa *lsa)
1864 {
1865 struct router_lsa *rl;
1866 enum ls_node_type type;
1867 struct ls_vertex *vertex;
1868 struct ls_edge *edge;
1869 struct ls_subnet *subnet;
1870 struct listnode *node;
1871 int len, links;
1872
1873 /* Sanity Check */
1874 if (!ted || !lsa || !lsa->data)
1875 return -1;
1876
1877 ote_debug("MPLS-TE (%s): Parse Router LSA[%pI4] from Router[%pI4]",
1878 __func__, &lsa->data->id, &lsa->data->adv_router);
1879
1880 /* Get vertex from LSA Advertise Router ID */
1881 vertex = get_vertex(ted, lsa);
1882
1883 /* Set Node type information if it has changed */
1884 rl = (struct router_lsa *)lsa->data;
1885 if (IS_ROUTER_LSA_VIRTUAL(rl))
1886 type = PSEUDO;
1887 else if (IS_ROUTER_LSA_EXTERNAL(rl))
1888 type = ASBR;
1889 else if (IS_ROUTER_LSA_BORDER(rl))
1890 type = ABR;
1891 else
1892 type = STANDARD;
1893
1894 if (vertex->status == NEW) {
1895 vertex->node->type = type;
1896 SET_FLAG(vertex->node->flags, LS_NODE_TYPE);
1897 } else if (vertex->node->type != type) {
1898 vertex->node->type = type;
1899 vertex->status = UPDATE;
1900 }
1901
1902 /* Check if Vertex has been modified */
1903 if (vertex->status != SYNC) {
1904 ote_debug(" |- %s Vertex %pI4",
1905 vertex->status == NEW ? "Add" : "Update",
1906 &vertex->node->router_id);
1907
1908 /* Vertex is out of sync: export it */
1909 ospf_te_export(LS_MSG_TYPE_NODE, vertex);
1910 vertex->status = SYNC;
1911 }
1912
1913 /* Mark outgoing Edge and Subnet as ORPHAN to detect deletion */
1914 for (ALL_LIST_ELEMENTS_RO(vertex->outgoing_edges, node, edge))
1915 edge->status = ORPHAN;
1916
1917 for (ALL_LIST_ELEMENTS_RO(vertex->prefixes, node, subnet))
1918 subnet->status = ORPHAN;
1919
1920 /* Then, process Link Information */
1921 len = lsa->size - OSPF_LSA_HEADER_SIZE - OSPF_ROUTER_LSA_MIN_SIZE;
1922 links = ntohs(rl->links);
1923 for (int i = 0; i < links && len > 0; len -= 12, i++) {
1924 struct prefix p;
1925 uint32_t metric;
1926
1927 switch (rl->link[i].type) {
1928 case LSA_LINK_TYPE_POINTOPOINT:
1929 ospf_te_update_link(ted, vertex, rl->link[i].link_data,
1930 ntohs(rl->link[i].metric));
1931 /* Add corresponding subnet */
1932 p.family = AF_INET;
1933 p.prefixlen = IPV4_MAX_BITLEN;
1934 p.u.prefix4 = rl->link[i].link_data;
1935 metric = ntohs(rl->link[i].metric);
1936 ospf_te_update_subnet(ted, vertex, p, metric);
1937 break;
1938 case LSA_LINK_TYPE_STUB:
1939 /* Keep only /32 prefix */
1940 p.prefixlen = ip_masklen(rl->link[i].link_data);
1941 if (p.prefixlen == IPV4_MAX_BITLEN) {
1942 p.family = AF_INET;
1943 p.u.prefix4 = rl->link[i].link_id;
1944 metric = ntohs(rl->link[i].metric);
1945 ospf_te_update_subnet(ted, vertex, p, metric);
1946 }
1947 break;
1948 default:
1949 break;
1950 }
1951 }
1952 /* Clean remaining Orphan Edges or Subnets */
1953 if (OspfMplsTE.export)
1954 ls_vertex_clean(ted, vertex, zclient);
1955 else
1956 ls_vertex_clean(ted, vertex, NULL);
1957
1958 return 0;
1959 }
1960
1961 /**
1962 * Delete Vertex, Edge and Subnet associated to this Router LSA. This function
1963 * is called when the router received such LSA with MAX_AGE (Flush) or when the
1964 * router stop OSPF.
1965 *
1966 * @param ted Link State Traffic Engineering Database
1967 * @param lsa OSPF Link State Advertisement
1968 *
1969 * @return 0 if success, -1 otherwise
1970 */
1971 static int ospf_te_delete_router_lsa(struct ls_ted *ted, struct ospf_lsa *lsa)
1972 {
1973 struct ls_node_id lnid;
1974 struct ls_vertex *vertex;
1975
1976 /* Sanity Check */
1977 if (!ted || !lsa || !lsa->data)
1978 return -1;
1979
1980 /* Search Vertex that corresponds to this LSA */
1981 lnid.origin = OSPFv2;
1982 lnid.id.ip.addr = lsa->data->adv_router;
1983 lnid.id.ip.area_id = lsa->area->area_id;
1984 vertex = ls_find_vertex_by_id(ted, lnid);
1985 if (!vertex)
1986 return -1;
1987
1988 ote_debug("MPLS-TE (%s): Delete Vertex %pI4 from Router LSA[%pI4]",
1989 __func__, &vertex->node->router_id, &lsa->data->id);
1990
1991 /* Export deleted vertex ... */
1992 vertex->status = DELETE;
1993 ospf_te_export(LS_MSG_TYPE_NODE, vertex);
1994
1995 /* ... and remove Node & Vertex from Link State Date Base */
1996 ls_vertex_del_all(ted, vertex);
1997
1998 return 0;
1999 }
2000
2001 /**
2002 * Create or update Remote Vertex that corresponds to the remote ASBR of the
2003 * foreign network if Edge is associated to an Inter-AS LSA (Type 6).
2004 *
2005 * @param ted Link State Traffic Engineering Database
2006 * @param edge Link State Edge
2007 */
2008 static void ospf_te_update_remote_asbr(struct ls_ted *ted, struct ls_edge *edge)
2009 {
2010 struct ls_node_id lnid;
2011 struct ls_vertex *vertex;
2012 struct ls_node *lnode;
2013 struct ls_attributes *attr;
2014 struct prefix p;
2015
2016 /* Sanity Check */
2017 if (!ted || !edge)
2018 return;
2019
2020 /* Search if a Link State Vertex already exist */
2021 attr = edge->attributes;
2022 lnid.origin = OSPFv2;
2023 lnid.id.ip.addr = attr->standard.remote_addr;
2024 lnid.id.ip.area_id = attr->adv.id.ip.area_id;
2025 vertex = ls_find_vertex_by_id(ted, lnid);
2026
2027 /* Create Node & Vertex in the Link State Date Base if not found */
2028 if (!vertex) {
2029 const struct in_addr inaddr_any = {.s_addr = INADDR_ANY};
2030
2031 lnode = ls_node_new(lnid, inaddr_any, in6addr_any);
2032 snprintfrr(lnode->name, MAX_NAME_LENGTH, "%pI4",
2033 &lnid.id.ip.addr);
2034 vertex = ls_vertex_add(ted, lnode);
2035 }
2036
2037 /* Update Node information */
2038 lnode = vertex->node;
2039 if (CHECK_FLAG(lnode->flags, LS_NODE_TYPE)) {
2040 if (lnode->type != RMT_ASBR) {
2041 lnode->type = RMT_ASBR;
2042 if (vertex->status != NEW)
2043 vertex->status = UPDATE;
2044 }
2045 } else {
2046 lnode->type = RMT_ASBR;
2047 SET_FLAG(lnode->flags, LS_NODE_TYPE);
2048 if (vertex->status != NEW)
2049 vertex->status = UPDATE;
2050 }
2051 if (CHECK_FLAG(lnode->flags, LS_NODE_AS_NUMBER)) {
2052 if (lnode->as_number != attr->standard.remote_as) {
2053 lnode->as_number = attr->standard.remote_as;
2054 if (vertex->status != NEW)
2055 vertex->status = UPDATE;
2056 }
2057 } else {
2058 lnode->as_number = attr->standard.remote_as;
2059 SET_FLAG(lnode->flags, LS_NODE_AS_NUMBER);
2060 if (vertex->status != NEW)
2061 vertex->status = UPDATE;
2062 }
2063
2064 /* Export Link State Vertex if needed */
2065 if (vertex->status == NEW || vertex->status == UPDATE) {
2066 ote_debug(" |- %s Remote Vertex %pI4 for AS %u",
2067 vertex->status == NEW ? "Add" : "Update",
2068 &lnode->router_id, lnode->as_number);
2069 ospf_te_export(LS_MSG_TYPE_NODE, vertex);
2070 vertex->status = SYNC;
2071 }
2072
2073 /* Update corresponding Subnets */
2074 p.family = AF_INET;
2075 p.prefixlen = IPV4_MAX_BITLEN;
2076 p.u.prefix4 = attr->standard.local;
2077 ospf_te_update_subnet(ted, edge->source, p, attr->standard.te_metric);
2078
2079 p.family = AF_INET;
2080 p.prefixlen = IPV4_MAX_BITLEN;
2081 p.u.prefix4 = attr->standard.remote_addr;
2082 ospf_te_update_subnet(ted, vertex, p, attr->standard.te_metric);
2083
2084 /* Connect Edge to the remote Vertex */
2085 if (edge->destination == NULL) {
2086 edge->destination = vertex;
2087 listnode_add_sort_nodup(vertex->incoming_edges, edge);
2088 }
2089
2090 /* Finally set type to ASBR the node that advertised this Edge ... */
2091 vertex = edge->source;
2092 lnode = vertex->node;
2093 if (CHECK_FLAG(lnode->flags, LS_NODE_TYPE)) {
2094 if (lnode->type != ASBR) {
2095 lnode->type = ASBR;
2096 if (vertex->status != NEW)
2097 vertex->status = UPDATE;
2098 }
2099 } else {
2100 lnode->type = ASBR;
2101 SET_FLAG(lnode->flags, LS_NODE_TYPE);
2102 if (vertex->status != NEW)
2103 vertex->status = UPDATE;
2104 }
2105
2106 /* ... and Export it if needed */
2107 if (vertex->status == NEW || vertex->status == UPDATE) {
2108 ospf_te_export(LS_MSG_TYPE_NODE, vertex);
2109 vertex->status = SYNC;
2110 }
2111 }
2112
2113 /**
2114 * Parse Opaque Traffic Engineering LSA (Type 1) TLVs and create or update the
2115 * corresponding Link State Edge and Attributes. Vertex connections are also
2116 * updated if needed based on the remote IP address of the Edge and existing
2117 * reverse Edge.
2118 *
2119 * @param ted Link State Traffic Engineering Database
2120 * @param lsa OSPF Link State Advertisement
2121 *
2122 * @return 0 if success, -1 otherwise
2123 */
2124 static int ospf_te_parse_te(struct ls_ted *ted, struct ospf_lsa *lsa)
2125 {
2126 struct ls_edge *edge;
2127 struct ls_vertex *vertex;
2128 struct ls_attributes *old, attr = {};
2129 struct tlv_header *tlvh;
2130 void *value;
2131 uint16_t len, sum;
2132 uint8_t lsa_id;
2133
2134 /* Initialize Attribute */
2135 attr.adv.origin = OSPFv2;
2136 attr.adv.id.ip.addr = lsa->data->adv_router;
2137 if (lsa->data->type != OSPF_OPAQUE_AS_LSA)
2138 attr.adv.id.ip.area_id = lsa->area->area_id;
2139
2140 /* Initialize TLV browsing */
2141 tlvh = TLV_HDR_TOP(lsa->data);
2142 len = lsa->size - OSPF_LSA_HEADER_SIZE;
2143
2144 /* Check if TE Router-ID TLV is present */
2145 if (ntohs(tlvh->type) == TE_TLV_ROUTER_ADDR) {
2146 /* if TE Router-ID is alone, we are done ... */
2147 if (len == TE_LINK_SUBTLV_DEF_SIZE)
2148 return 0;
2149
2150 /* ... otherwise, skip it */
2151 len -= TE_LINK_SUBTLV_DEF_SIZE + TLV_HDR_SIZE;
2152 tlvh = TLV_HDR_NEXT(tlvh);
2153 }
2154
2155 /* Check if we have a valid TE Link TLV */
2156 if ((len == 0) || (ntohs(tlvh->type) != TE_TLV_LINK))
2157 return 0;
2158
2159 sum = sizeof(struct tlv_header);
2160 /* Browse sub-TLV and fulfill Link State Attributes */
2161 for (tlvh = TLV_DATA(tlvh); sum < len; tlvh = TLV_HDR_NEXT(tlvh)) {
2162 uint32_t val32, tab32[2];
2163 float valf, tabf[8];
2164 struct in_addr addr;
2165
2166 value = TLV_DATA(tlvh);
2167 switch (ntohs(tlvh->type)) {
2168 case TE_LINK_SUBTLV_LCLIF_IPADDR:
2169 memcpy(&addr, value, TE_LINK_SUBTLV_DEF_SIZE);
2170 attr.standard.local = addr;
2171 SET_FLAG(attr.flags, LS_ATTR_LOCAL_ADDR);
2172 break;
2173 case TE_LINK_SUBTLV_RMTIF_IPADDR:
2174 memcpy(&addr, value, TE_LINK_SUBTLV_DEF_SIZE);
2175 attr.standard.remote = addr;
2176 SET_FLAG(attr.flags, LS_ATTR_NEIGH_ADDR);
2177 break;
2178 case TE_LINK_SUBTLV_TE_METRIC:
2179 memcpy(&val32, value, TE_LINK_SUBTLV_DEF_SIZE);
2180 attr.standard.te_metric = ntohl(val32);
2181 SET_FLAG(attr.flags, LS_ATTR_TE_METRIC);
2182 break;
2183 case TE_LINK_SUBTLV_MAX_BW:
2184 memcpy(&valf, value, TE_LINK_SUBTLV_DEF_SIZE);
2185 attr.standard.max_bw = ntohf(valf);
2186 SET_FLAG(attr.flags, LS_ATTR_MAX_BW);
2187 break;
2188 case TE_LINK_SUBTLV_MAX_RSV_BW:
2189 memcpy(&valf, value, TE_LINK_SUBTLV_DEF_SIZE);
2190 attr.standard.max_rsv_bw = ntohf(valf);
2191 SET_FLAG(attr.flags, LS_ATTR_MAX_RSV_BW);
2192 break;
2193 case TE_LINK_SUBTLV_UNRSV_BW:
2194 memcpy(tabf, value, TE_LINK_SUBTLV_UNRSV_SIZE);
2195 for (int i = 0; i < MAX_CLASS_TYPE; i++)
2196 attr.standard.unrsv_bw[i] = ntohf(tabf[i]);
2197 SET_FLAG(attr.flags, LS_ATTR_UNRSV_BW);
2198 break;
2199 case TE_LINK_SUBTLV_RSC_CLSCLR:
2200 memcpy(&val32, value, TE_LINK_SUBTLV_DEF_SIZE);
2201 attr.standard.admin_group = ntohl(val32);
2202 SET_FLAG(attr.flags, LS_ATTR_ADM_GRP);
2203 break;
2204 case TE_LINK_SUBTLV_LLRI:
2205 memcpy(tab32, value, TE_LINK_SUBTLV_LLRI_SIZE);
2206 attr.standard.local_id = ntohl(tab32[0]);
2207 attr.standard.remote_id = ntohl(tab32[1]);
2208 SET_FLAG(attr.flags, LS_ATTR_LOCAL_ID);
2209 SET_FLAG(attr.flags, LS_ATTR_NEIGH_ID);
2210 break;
2211 case TE_LINK_SUBTLV_RIP:
2212 memcpy(&addr, value, TE_LINK_SUBTLV_DEF_SIZE);
2213 attr.standard.remote_addr = addr;
2214 SET_FLAG(attr.flags, LS_ATTR_REMOTE_ADDR);
2215 break;
2216 case TE_LINK_SUBTLV_RAS:
2217 memcpy(&val32, value, TE_LINK_SUBTLV_DEF_SIZE);
2218 attr.standard.remote_as = ntohl(val32);
2219 SET_FLAG(attr.flags, LS_ATTR_REMOTE_AS);
2220 break;
2221 case TE_LINK_SUBTLV_AV_DELAY:
2222 memcpy(&val32, value, TE_LINK_SUBTLV_DEF_SIZE);
2223 attr.extended.delay = ntohl(val32);
2224 SET_FLAG(attr.flags, LS_ATTR_DELAY);
2225 break;
2226 case TE_LINK_SUBTLV_MM_DELAY:
2227 memcpy(tab32, value, TE_LINK_SUBTLV_MM_DELAY_SIZE);
2228 attr.extended.min_delay = ntohl(tab32[0]);
2229 attr.extended.max_delay = ntohl(tab32[1]);
2230 SET_FLAG(attr.flags, LS_ATTR_MIN_MAX_DELAY);
2231 break;
2232 case TE_LINK_SUBTLV_DELAY_VAR:
2233 memcpy(&val32, value, TE_LINK_SUBTLV_DEF_SIZE);
2234 attr.extended.jitter = ntohl(val32);
2235 SET_FLAG(attr.flags, LS_ATTR_JITTER);
2236 break;
2237 case TE_LINK_SUBTLV_PKT_LOSS:
2238 memcpy(&val32, value, TE_LINK_SUBTLV_DEF_SIZE);
2239 attr.extended.pkt_loss = ntohl(val32);
2240 SET_FLAG(attr.flags, LS_ATTR_PACKET_LOSS);
2241 break;
2242 case TE_LINK_SUBTLV_RES_BW:
2243 memcpy(&valf, value, TE_LINK_SUBTLV_DEF_SIZE);
2244 attr.extended.rsv_bw = ntohf(valf);
2245 SET_FLAG(attr.flags, LS_ATTR_RSV_BW);
2246 break;
2247 case TE_LINK_SUBTLV_AVA_BW:
2248 memcpy(&valf, value, TE_LINK_SUBTLV_DEF_SIZE);
2249 attr.extended.ava_bw = ntohf(valf);
2250 SET_FLAG(attr.flags, LS_ATTR_AVA_BW);
2251 break;
2252 case TE_LINK_SUBTLV_USE_BW:
2253 memcpy(&valf, value, TE_LINK_SUBTLV_DEF_SIZE);
2254 attr.extended.used_bw = ntohf(valf);
2255 SET_FLAG(attr.flags, LS_ATTR_USE_BW);
2256 break;
2257 default:
2258 break;
2259 }
2260 sum += TLV_SIZE(tlvh);
2261 }
2262
2263 /* Get corresponding Edge from Link State Data Base */
2264 edge = get_edge(ted, attr.adv, attr.standard.local);
2265 old = edge->attributes;
2266
2267 ote_debug(" |- Process Traffic Engineering LSA %pI4 for Edge %pI4",
2268 &lsa->data->id, &attr.standard.local);
2269
2270 /* Update standard fields */
2271 len = sizeof(struct ls_standard);
2272 if ((attr.flags & 0x0FFFF) == (old->flags & 0x0FFFF)) {
2273 if (memcmp(&attr.standard, &old->standard, len) != 0) {
2274 memcpy(&old->standard, &attr.standard, len);
2275 if (edge->status != NEW)
2276 edge->status = UPDATE;
2277 }
2278 } else {
2279 memcpy(&old->standard, &attr.standard, len);
2280 old->flags |= attr.flags & 0x0FFFF;
2281 if (edge->status != NEW)
2282 edge->status = UPDATE;
2283 }
2284 /* Update extended fields */
2285 len = sizeof(struct ls_extended);
2286 if ((attr.flags & 0x0FF0000) == (old->flags & 0x0FF0000)) {
2287 if (memcmp(&attr.extended, &old->extended, len) != 0) {
2288 memcpy(&old->extended, &attr.extended, len);
2289 if (edge->status != NEW)
2290 edge->status = UPDATE;
2291 }
2292 } else {
2293 memcpy(&old->extended, &attr.extended, len);
2294 old->flags |= attr.flags & 0x0FF0000;
2295 if (edge->status != NEW)
2296 edge->status = UPDATE;
2297 }
2298
2299 /* If LSA is an Opaque Inter-AS, Add Node and Subnet */
2300 lsa_id = GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr));
2301 if (lsa_id == OPAQUE_TYPE_INTER_AS_LSA)
2302 ospf_te_update_remote_asbr(ted, edge);
2303
2304 /* Update remote Link if remote IP addr is known */
2305 if (CHECK_FLAG(old->flags, LS_ATTR_NEIGH_ADDR)) {
2306 struct ls_edge *dst;
2307
2308 dst = ls_find_edge_by_destination(ted, old);
2309 /* Attach remote link if not set */
2310 if (dst && edge->source && dst->destination == NULL) {
2311 vertex = edge->source;
2312 if (vertex->incoming_edges)
2313 listnode_add_sort_nodup(vertex->incoming_edges,
2314 dst);
2315 dst->destination = vertex;
2316 }
2317 /* and destination vertex to this edge */
2318 if (dst && dst->source && edge->destination == NULL) {
2319 vertex = dst->source;
2320 if (vertex->incoming_edges)
2321 listnode_add_sort_nodup(vertex->incoming_edges,
2322 edge);
2323 edge->destination = vertex;
2324 }
2325 }
2326
2327 /* Export Link State Edge if needed */
2328 if (edge->status == NEW || edge->status == UPDATE) {
2329 ote_debug(" |- %s TE info. for Edge %pI4",
2330 edge->status == NEW ? "Add" : "Update",
2331 &edge->attributes->standard.local);
2332
2333 ospf_te_export(LS_MSG_TYPE_ATTRIBUTES, edge);
2334 edge->status = SYNC;
2335 }
2336
2337 return 0;
2338 }
2339
2340 /**
2341 * Delete Link State Attributes information that correspond to the Opaque
2342 * Traffic Engineering LSA (Type 1) TLVs. Note that the Edge is not removed.
2343 *
2344 * @param ted Link State Traffic Engineering Database
2345 * @param lsa OSPF Link State Advertisement
2346 *
2347 * @return 0 if success, -1 otherwise
2348 */
2349 static int ospf_te_delete_te(struct ls_ted *ted, struct ospf_lsa *lsa)
2350 {
2351 struct ls_edge *edge;
2352 struct ls_attributes *attr;
2353 struct tlv_header *tlvh;
2354 struct in_addr addr;
2355 uint64_t key = 0;
2356 uint16_t len, sum;
2357 uint8_t lsa_id;
2358
2359 /* Initialize TLV browsing */
2360 tlvh = TLV_HDR_TOP(lsa->data);
2361 /* Skip Router TE ID if present */
2362 if (ntohs(tlvh->type) == TE_TLV_ROUTER_ADDR)
2363 tlvh = TLV_HDR_NEXT(tlvh);
2364 len = TLV_BODY_SIZE(tlvh);
2365 sum = sizeof(struct tlv_header);
2366
2367 /* Browse sub-TLV to find Link ID */
2368 for (tlvh = TLV_DATA(tlvh); sum < len; tlvh = TLV_HDR_NEXT(tlvh)) {
2369 if (ntohs(tlvh->type) == TE_LINK_SUBTLV_LCLIF_IPADDR) {
2370 memcpy(&addr, TLV_DATA(tlvh), TE_LINK_SUBTLV_DEF_SIZE);
2371 key = ((uint64_t)ntohl(addr.s_addr)) & 0xffffffff;
2372 break;
2373 }
2374 sum += TLV_SIZE(tlvh);
2375 }
2376 if (key == 0)
2377 return 0;
2378
2379 /* Search Edge that corresponds to the Link ID */
2380 edge = ls_find_edge_by_key(ted, key);
2381 if (!edge || !edge->attributes)
2382 return 0;
2383 attr = edge->attributes;
2384
2385 /* First, remove Remote ASBR and associated Edge & Subnet if any */
2386 lsa_id = GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr));
2387 if (lsa_id == OPAQUE_TYPE_INTER_AS_LSA) {
2388 ote_debug(" |- Delete remote ASBR, Edge and Subnet");
2389
2390 if (edge->destination) {
2391 edge->destination->status = DELETE;
2392 ospf_te_export(LS_MSG_TYPE_NODE, edge->destination);
2393 ls_vertex_del_all(ted, edge->destination);
2394 }
2395
2396 ospf_te_delete_subnet(ted, attr->standard.local);
2397
2398 edge->status = DELETE;
2399 ospf_te_export(LS_MSG_TYPE_ATTRIBUTES, edge);
2400 ls_edge_del_all(ted, edge);
2401
2402 return 0;
2403 }
2404
2405 ote_debug(" |- Delete TE info. for Edge %pI4",
2406 &edge->attributes->standard.local);
2407
2408 /* Remove Link State Attributes TE information */
2409 memset(&attr->standard, 0, sizeof(struct ls_standard));
2410 attr->flags &= 0x0FFFF;
2411 memset(&attr->extended, 0, sizeof(struct ls_extended));
2412 attr->flags &= 0x0FF0000;
2413 ls_attributes_srlg_del(attr);
2414
2415 /* Export Edge that has been updated */
2416 if (CHECK_FLAG(attr->flags, LS_ATTR_ADJ_SID)
2417 || CHECK_FLAG(attr->flags, LS_ATTR_BCK_ADJ_SID)) {
2418 edge->status = UPDATE;
2419 ospf_te_export(LS_MSG_TYPE_ATTRIBUTES, edge);
2420 edge->status = SYNC;
2421 } else {
2422 /* Remove completely the Edge if Segment Routing is not set */
2423 ospf_te_delete_subnet(ted, attr->standard.local);
2424 edge->status = DELETE;
2425 ospf_te_export(LS_MSG_TYPE_ATTRIBUTES, edge);
2426 ls_edge_del_all(ted, edge);
2427 }
2428
2429 return 0;
2430 }
2431
2432 /**
2433 * Parse Opaque Router Information LSA (Type 4) TLVs and update the
2434 * corresponding Link State Vertex with these information (Segment Routing).
2435 *
2436 * @param ted Link State Traffic Engineering Database
2437 * @param lsa OSPF Link State Advertisement
2438 *
2439 * @return 0 if success, -1 otherwise
2440 */
2441 static int ospf_te_parse_ri(struct ls_ted *ted, struct ospf_lsa *lsa)
2442 {
2443 struct ls_vertex *vertex;
2444 struct ls_node *node;
2445 struct lsa_header *lsah = lsa->data;
2446 struct tlv_header *tlvh;
2447 uint16_t len = 0, sum = 0;
2448
2449 /* Get vertex / Node from LSA Advertised Router ID */
2450 vertex = get_vertex(ted, lsa);
2451 node = vertex->node;
2452
2453 ote_debug(" |- Process Router Information LSA %pI4 for Vertex %pI4",
2454 &lsa->data->id, &node->router_id);
2455
2456 /* Initialize TLV browsing */
2457 len = lsa->size - OSPF_LSA_HEADER_SIZE;
2458 for (tlvh = TLV_HDR_TOP(lsah); sum < len && tlvh;
2459 tlvh = TLV_HDR_NEXT(tlvh)) {
2460 struct ri_sr_tlv_sr_algorithm *algo;
2461 struct ri_sr_tlv_sid_label_range *range;
2462 struct ri_sr_tlv_node_msd *msd;
2463 uint32_t size, lower;
2464
2465 switch (ntohs(tlvh->type)) {
2466 case RI_SR_TLV_SR_ALGORITHM:
2467 algo = (struct ri_sr_tlv_sr_algorithm *)tlvh;
2468
2469 for (int i = 0; i < ntohs(algo->header.length); i++) {
2470 if (CHECK_FLAG(node->flags, LS_NODE_SR)
2471 && (node->algo[i] == algo->value[i]))
2472 continue;
2473
2474 node->algo[i] = algo->value[i];
2475 SET_FLAG(node->flags, LS_NODE_SR);
2476 if (vertex->status != NEW)
2477 vertex->status = UPDATE;
2478 }
2479
2480 /* Reset other Algorithms */
2481 for (int i = ntohs(algo->header.length); i < 2; i++) {
2482 if (vertex->status != NEW
2483 && node->algo[i] != SR_ALGORITHM_UNSET)
2484 vertex->status = UPDATE;
2485 node->algo[i] = SR_ALGORITHM_UNSET;
2486 }
2487
2488 break;
2489
2490 case RI_SR_TLV_SRGB_LABEL_RANGE:
2491 range = (struct ri_sr_tlv_sid_label_range *)tlvh;
2492 size = GET_RANGE_SIZE(ntohl(range->size));
2493 lower = GET_LABEL(ntohl(range->lower.value));
2494 if ((CHECK_FLAG(node->flags, LS_NODE_SR))
2495 && ((node->srgb.range_size == size)
2496 && (node->srgb.lower_bound == lower)))
2497 break;
2498
2499 node->srgb.range_size = size;
2500 node->srgb.lower_bound = lower;
2501 SET_FLAG(node->flags, LS_NODE_SR);
2502 if (vertex->status != NEW)
2503 vertex->status = UPDATE;
2504
2505 break;
2506
2507 case RI_SR_TLV_SRLB_LABEL_RANGE:
2508 range = (struct ri_sr_tlv_sid_label_range *)tlvh;
2509 size = GET_RANGE_SIZE(ntohl(range->size));
2510 lower = GET_LABEL(ntohl(range->lower.value));
2511 if ((CHECK_FLAG(node->flags, LS_NODE_SRLB))
2512 && ((node->srlb.range_size == size)
2513 && (node->srlb.lower_bound == lower)))
2514 break;
2515
2516 node->srlb.range_size = size;
2517 node->srlb.lower_bound = lower;
2518 SET_FLAG(node->flags, LS_NODE_SRLB);
2519 if (vertex->status != NEW)
2520 vertex->status = UPDATE;
2521
2522 break;
2523
2524 case RI_SR_TLV_NODE_MSD:
2525 msd = (struct ri_sr_tlv_node_msd *)tlvh;
2526 if ((CHECK_FLAG(node->flags, LS_NODE_MSD))
2527 && (node->msd == msd->value))
2528 break;
2529
2530 node->msd = msd->value;
2531 SET_FLAG(node->flags, LS_NODE_MSD);
2532 if (vertex->status != NEW)
2533 vertex->status = UPDATE;
2534
2535 break;
2536
2537 default:
2538 break;
2539 }
2540 sum += TLV_SIZE(tlvh);
2541 }
2542
2543 /* Vertex has been created or updated: export it */
2544 if (vertex->status == NEW || vertex->status == UPDATE) {
2545 ote_debug(" |- %s SR info - SRGB[%d/%d] for Vertex %pI4",
2546 vertex->status == NEW ? "Add" : "Update",
2547 vertex->node->srgb.lower_bound,
2548 vertex->node->srgb.range_size,
2549 &vertex->node->router_id);
2550
2551 ospf_te_export(LS_MSG_TYPE_NODE, vertex);
2552 vertex->status = SYNC;
2553 }
2554
2555 return 0;
2556 }
2557
2558 /**
2559 * Delete Link State Node information (Segment Routing) that correspond to the
2560 * Opaque Router Information LSA (Type 4) TLVs. Note that the Vertex is not
2561 * removed.
2562 *
2563 * @param ted Link State Traffic Engineering Database
2564 * @param lsa OSPF Link State Advertisement
2565 *
2566 * @return 0 if success, -1 otherwise
2567 */
2568 static int ospf_te_delete_ri(struct ls_ted *ted, struct ospf_lsa *lsa)
2569 {
2570 struct ls_node_id lnid;
2571 struct ls_vertex *vertex;
2572 struct ls_node *node;
2573
2574 /* Search if a Link State Vertex already exist */
2575 lnid.origin = OSPFv2;
2576 lnid.id.ip.addr = lsa->data->adv_router;
2577 lnid.id.ip.area_id = lsa->area->area_id;
2578 vertex = ls_find_vertex_by_id(ted, lnid);
2579 if (!vertex)
2580 return -1;
2581
2582 /* Remove Segment Routing Information if any */
2583 node = vertex->node;
2584 UNSET_FLAG(node->flags, LS_NODE_SR);
2585 memset(&node->srgb, 0, sizeof(struct ls_srgb));
2586 node->algo[0] = SR_ALGORITHM_UNSET;
2587 node->algo[1] = SR_ALGORITHM_UNSET;
2588 UNSET_FLAG(node->flags, LS_NODE_SRLB);
2589 memset(&node->srlb, 0, sizeof(struct ls_srlb));
2590 UNSET_FLAG(node->flags, LS_NODE_MSD);
2591 node->msd = 0;
2592 vertex->status = UPDATE;
2593
2594 ote_debug(" |- Delete SR info. for Vertex %pI4",
2595 &vertex->node->router_id);
2596
2597 /* Vertex has been updated: export it */
2598 ospf_te_export(LS_MSG_TYPE_NODE, vertex);
2599 vertex->status = SYNC;
2600
2601 return 0;
2602 }
2603
2604 /**
2605 * Parse Opaque Extended Prefix LSA (Type 7) TLVs and update the corresponding
2606 * Link State Subnet with these information (Segment Routing ID).
2607 *
2608 * @param ted Link State Traffic Engineering Database
2609 * @param lsa OSPF Link State Advertisement
2610 *
2611 * @return 0 if success, -1 otherwise
2612 */
2613 static int ospf_te_parse_ext_pref(struct ls_ted *ted, struct ospf_lsa *lsa)
2614 {
2615 struct ls_node_id lnid;
2616 struct ls_subnet *subnet;
2617 struct ls_prefix *ls_pref;
2618 struct prefix pref;
2619 struct ext_tlv_prefix *ext;
2620 struct ext_subtlv_prefix_sid *pref_sid;
2621 uint32_t label;
2622
2623 /* Get corresponding Subnet from Link State Data Base */
2624 ext = (struct ext_tlv_prefix *)TLV_HDR_TOP(lsa->data);
2625 pref.family = AF_INET;
2626 pref.prefixlen = ext->pref_length;
2627 pref.u.prefix4 = ext->address;
2628 subnet = ls_find_subnet(ted, pref);
2629
2630 /* Create new Link State Prefix if not found */
2631 if (!subnet) {
2632 lnid.origin = OSPFv2;
2633 lnid.id.ip.addr = lsa->data->adv_router;
2634 lnid.id.ip.area_id = lsa->area->area_id;
2635 ls_pref = ls_prefix_new(lnid, pref);
2636 /* and add it to the TED */
2637 subnet = ls_subnet_add(ted, ls_pref);
2638 }
2639
2640 ote_debug(" |- Process Extended Prefix LSA %pI4 for subnet %pFX",
2641 &lsa->data->id, &pref);
2642
2643 /* Initialize TLV browsing */
2644 ls_pref = subnet->ls_pref;
2645 pref_sid = (struct ext_subtlv_prefix_sid *)((char *)(ext) + TLV_HDR_SIZE
2646 + EXT_TLV_PREFIX_SIZE);
2647 label = CHECK_FLAG(pref_sid->flags, EXT_SUBTLV_PREFIX_SID_VFLG)
2648 ? GET_LABEL(ntohl(pref_sid->value))
2649 : ntohl(pref_sid->value);
2650
2651 /* Check if it is a simple refresh */
2652 if (CHECK_FLAG(ls_pref->flags, LS_PREF_SR)
2653 && ls_pref->sr.algo == pref_sid->algorithm
2654 && ls_pref->sr.sid_flag == pref_sid->flags
2655 && ls_pref->sr.sid == label)
2656 return 0;
2657
2658 /* Fulfill SR information */
2659 ls_pref->sr.algo = pref_sid->algorithm;
2660 ls_pref->sr.sid_flag = pref_sid->flags;
2661 ls_pref->sr.sid = label;
2662 SET_FLAG(ls_pref->flags, LS_PREF_SR);
2663 if (subnet->status != NEW)
2664 subnet->status = UPDATE;
2665
2666 /* Export Subnet if needed */
2667 if (subnet->status == NEW || subnet->status == UPDATE) {
2668 ote_debug(" |- %s SID %d to subnet %pFX",
2669 subnet->status == NEW ? "Add" : "Update",
2670 ls_pref->sr.sid, &ls_pref->pref);
2671
2672 ospf_te_export(LS_MSG_TYPE_PREFIX, subnet);
2673 subnet->status = SYNC;
2674 }
2675
2676 return 0;
2677 }
2678
2679 /**
2680 * Delete Link State Subnet information (Segment Routing ID) that correspond to
2681 * the Opaque Extended Prefix LSA (Type 7) TLVs. Note that the Subnet is not
2682 * removed.
2683 *
2684 * @param ted Link State Traffic Engineering Database
2685 * @param lsa OSPF Link State Advertisement
2686 *
2687 * @return 0 if success, -1 otherwise
2688 */
2689 static int ospf_te_delete_ext_pref(struct ls_ted *ted, struct ospf_lsa *lsa)
2690 {
2691 struct ls_subnet *subnet;
2692 struct ls_prefix *ls_pref;
2693 struct prefix pref;
2694 struct ext_tlv_prefix *ext;
2695
2696 /* Get corresponding Subnet from Link State Data Base */
2697 ext = (struct ext_tlv_prefix *)TLV_HDR_TOP(lsa->data);
2698 pref.family = AF_INET;
2699 pref.prefixlen = ext->pref_length;
2700 pref.u.prefix4 = ext->address;
2701 subnet = ls_find_subnet(ted, pref);
2702
2703 /* Check if there is a corresponding subnet */
2704 if (!subnet)
2705 return -1;
2706
2707 ote_debug(" |- Delete SID %d to subnet %pFX", subnet->ls_pref->sr.sid,
2708 &subnet->ls_pref->pref);
2709
2710 /* Remove Segment Routing information */
2711 ls_pref = subnet->ls_pref;
2712 UNSET_FLAG(ls_pref->flags, LS_PREF_SR);
2713 memset(&ls_pref->sr, 0, sizeof(struct ls_sid));
2714 subnet->status = UPDATE;
2715
2716 /* Subnet has been updated: export it */
2717 ospf_te_export(LS_MSG_TYPE_PREFIX, subnet);
2718 subnet->status = SYNC;
2719
2720 return 0;
2721 }
2722
2723 /**
2724 * Parse Opaque Extended Link LSA (Type 8) TLVs and update the corresponding
2725 * Link State Edge with these information (Segment Routing Adjacency).
2726 *
2727 * @param ted Link State Traffic Engineering Database
2728 * @param lsa OSPF Link State Advertisement
2729 *
2730 * @return 0 if success, -1 otherwise
2731 */
2732 static int ospf_te_parse_ext_link(struct ls_ted *ted, struct ospf_lsa *lsa)
2733 {
2734 struct ls_node_id lnid;
2735 struct tlv_header *tlvh;
2736 struct ext_tlv_link *ext;
2737 struct ls_edge *edge;
2738 struct ls_attributes *atr;
2739 uint16_t len = 0, sum = 0, i;
2740 uint32_t label;
2741
2742 /* Get corresponding Edge from Link State Data Base */
2743 lnid.origin = OSPFv2;
2744 lnid.id.ip.addr = lsa->data->adv_router;
2745 lnid.id.ip.area_id = lsa->area->area_id;
2746 ext = (struct ext_tlv_link *)TLV_HDR_TOP(lsa->data);
2747 edge = get_edge(ted, lnid, ext->link_data);
2748 atr = edge->attributes;
2749
2750 ote_debug(" |- Process Extended Link LSA %pI4 for edge %pI4",
2751 &lsa->data->id, &edge->attributes->standard.local);
2752
2753 /* Initialize TLV browsing */
2754 len = TLV_BODY_SIZE(&ext->header) - EXT_TLV_LINK_SIZE;
2755 tlvh = (struct tlv_header *)((char *)(ext) + TLV_HDR_SIZE
2756 + EXT_TLV_LINK_SIZE);
2757 for (; sum < len; tlvh = TLV_HDR_NEXT(tlvh)) {
2758 struct ext_subtlv_adj_sid *adj;
2759 struct ext_subtlv_lan_adj_sid *ladj;
2760 struct ext_subtlv_rmt_itf_addr *rmt;
2761
2762 switch (ntohs(tlvh->type)) {
2763 case EXT_SUBTLV_ADJ_SID:
2764 adj = (struct ext_subtlv_adj_sid *)tlvh;
2765 label = CHECK_FLAG(adj->flags,
2766 EXT_SUBTLV_LINK_ADJ_SID_VFLG)
2767 ? GET_LABEL(ntohl(adj->value))
2768 : ntohl(adj->value);
2769 i = CHECK_FLAG(adj->flags,
2770 EXT_SUBTLV_LINK_ADJ_SID_BFLG) ? 1 : 0;
2771 if (((i && CHECK_FLAG(atr->flags, LS_ATTR_BCK_ADJ_SID))
2772 || (!i && CHECK_FLAG(atr->flags, LS_ATTR_ADJ_SID)))
2773 && atr->adj_sid[i].flags == adj->flags
2774 && atr->adj_sid[i].sid == label
2775 && atr->adj_sid[i].weight == adj->weight)
2776 break;
2777
2778 atr->adj_sid[i].flags = adj->flags;
2779 atr->adj_sid[i].sid = label;
2780 atr->adj_sid[i].weight = adj->weight;
2781 if (i == 0)
2782 SET_FLAG(atr->flags, LS_ATTR_ADJ_SID);
2783 else
2784 SET_FLAG(atr->flags, LS_ATTR_BCK_ADJ_SID);
2785 if (edge->status != NEW)
2786 edge->status = UPDATE;
2787
2788 break;
2789 case EXT_SUBTLV_LAN_ADJ_SID:
2790 ladj = (struct ext_subtlv_lan_adj_sid *)tlvh;
2791 label = CHECK_FLAG(ladj->flags,
2792 EXT_SUBTLV_LINK_ADJ_SID_VFLG)
2793 ? GET_LABEL(ntohl(ladj->value))
2794 : ntohl(ladj->value);
2795 i = CHECK_FLAG(ladj->flags,
2796 EXT_SUBTLV_LINK_ADJ_SID_BFLG) ? 1 : 0;
2797 if (((i && CHECK_FLAG(atr->flags, LS_ATTR_BCK_ADJ_SID))
2798 || (!i && CHECK_FLAG(atr->flags, LS_ATTR_ADJ_SID)))
2799 && atr->adj_sid[i].flags == ladj->flags
2800 && atr->adj_sid[i].sid == label
2801 && atr->adj_sid[i].weight == ladj->weight
2802 && IPV4_ADDR_SAME(&atr->adj_sid[1].neighbor.addr,
2803 &ladj->neighbor_id))
2804 break;
2805
2806 atr->adj_sid[i].flags = ladj->flags;
2807 atr->adj_sid[i].sid = label;
2808 atr->adj_sid[i].weight = ladj->weight;
2809 atr->adj_sid[i].neighbor.addr = ladj->neighbor_id;
2810 if (i == 0)
2811 SET_FLAG(atr->flags, LS_ATTR_ADJ_SID);
2812 else
2813 SET_FLAG(atr->flags, LS_ATTR_BCK_ADJ_SID);
2814 if (edge->status != NEW)
2815 edge->status = UPDATE;
2816
2817 break;
2818 case EXT_SUBTLV_RMT_ITF_ADDR:
2819 rmt = (struct ext_subtlv_rmt_itf_addr *)tlvh;
2820 if (CHECK_FLAG(atr->flags, LS_ATTR_NEIGH_ADDR)
2821 && IPV4_ADDR_SAME(&atr->standard.remote,
2822 &rmt->value))
2823 break;
2824
2825 atr->standard.remote = rmt->value;
2826 SET_FLAG(atr->flags, LS_ATTR_NEIGH_ADDR);
2827 if (edge->status != NEW)
2828 edge->status = UPDATE;
2829
2830 break;
2831 default:
2832 break;
2833 }
2834 sum += TLV_SIZE(tlvh);
2835 }
2836
2837 /* Export Link State Edge if needed */
2838 if (edge->status == NEW || edge->status == UPDATE) {
2839 ote_debug(" |- %s Adj-SID %d & %d to edge %pI4",
2840 edge->status == NEW ? "Add" : "Update",
2841 edge->attributes->adj_sid[0].sid,
2842 edge->attributes->adj_sid[1].sid,
2843 &edge->attributes->standard.local);
2844
2845 ospf_te_export(LS_MSG_TYPE_ATTRIBUTES, edge);
2846 edge->status = SYNC;
2847 }
2848
2849 return 0;
2850 }
2851
2852 /**
2853 * Delete Link State Edge information (Segment Routing Adjacency) that
2854 * correspond to the Opaque Extended Link LSA (Type 8) TLVs. Note that the Edge
2855 * is not removed.
2856 *
2857 * @param ted Link State Traffic Engineering Database
2858 * @param lsa OSPF Link State Advertisement
2859 *
2860 * @return 0 if success, -1 otherwise
2861 */
2862 static int ospf_te_delete_ext_link(struct ls_ted *ted, struct ospf_lsa *lsa)
2863 {
2864 struct ls_edge *edge;
2865 struct ls_attributes *atr;
2866 struct ext_tlv_link *ext;
2867 uint64_t key;
2868
2869 /* Search for corresponding Edge from Link State Data Base */
2870 ext = (struct ext_tlv_link *)TLV_HDR_TOP(lsa->data);
2871 key = ((uint64_t)ntohl(ext->link_data.s_addr)) & 0xffffffff;
2872 edge = ls_find_edge_by_key(ted, key);
2873
2874 /* Check if there is a corresponding Edge */
2875 if (!edge)
2876 return -1;
2877
2878 ote_debug(" |- Delete Adj-SID %d to edge %pI4",
2879 edge->attributes->adj_sid[0].sid,
2880 &edge->attributes->standard.local);
2881
2882 /* Remove Segment Routing information */
2883 atr = edge->attributes;
2884 UNSET_FLAG(atr->flags, LS_ATTR_ADJ_SID);
2885 UNSET_FLAG(atr->flags, LS_ATTR_BCK_ADJ_SID);
2886 memset(atr->adj_sid, 0, 2 * sizeof(struct ls_sid));
2887 edge->status = UPDATE;
2888
2889 /* Edge has been updated: export it */
2890 ospf_te_export(LS_MSG_TYPE_ATTRIBUTES, edge);
2891 edge->status = SYNC;
2892
2893 return 0;
2894 }
2895
2896 /**
2897 * Parse Opaque LSA Type and call corresponding parser.
2898 *
2899 * @param ted Link State Traffic Engineering Database
2900 * @param lsa OSPF Link State Advertisement
2901 *
2902 * @return 0 if success, -1 otherwise
2903 */
2904 static int ospf_te_parse_opaque_lsa(struct ls_ted *ted, struct ospf_lsa *lsa)
2905 {
2906 uint8_t key = GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr));
2907 int rc = -1;
2908
2909 ote_debug("MPLS-TE (%s): Parse Opaque LSA[%pI4] from Router[%pI4]",
2910 __func__, &lsa->data->id, &lsa->data->adv_router);
2911
2912 switch (key) {
2913 case OPAQUE_TYPE_TRAFFIC_ENGINEERING_LSA:
2914 case OPAQUE_TYPE_INTER_AS_LSA:
2915 rc = ospf_te_parse_te(ted, lsa);
2916 break;
2917 case OPAQUE_TYPE_ROUTER_INFORMATION_LSA:
2918 rc = ospf_te_parse_ri(ted, lsa);
2919 break;
2920 case OPAQUE_TYPE_EXTENDED_PREFIX_LSA:
2921 rc = ospf_te_parse_ext_pref(ted, lsa);
2922 break;
2923 case OPAQUE_TYPE_EXTENDED_LINK_LSA:
2924 rc = ospf_te_parse_ext_link(ted, lsa);
2925 break;
2926 default:
2927 break;
2928 }
2929
2930 return rc;
2931 }
2932
2933 /**
2934 * Parse Opaque LSA Type and call corresponding deletion function.
2935 *
2936 * @param ted Link State Traffic Engineering Database
2937 * @param lsa OSPF Link State Advertisement
2938 *
2939 * @return 0 if success, -1 otherwise
2940 */
2941 static int ospf_te_delete_opaque_lsa(struct ls_ted *ted, struct ospf_lsa *lsa)
2942 {
2943 uint8_t key = GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr));
2944 int rc = -1;
2945
2946 ote_debug("MPLS-TE (%s): Parse Opaque LSA[%pI4] from Router[%pI4]",
2947 __func__, &lsa->data->id, &lsa->data->adv_router);
2948
2949 switch (key) {
2950 case OPAQUE_TYPE_TRAFFIC_ENGINEERING_LSA:
2951 case OPAQUE_TYPE_INTER_AS_LSA:
2952 rc = ospf_te_delete_te(ted, lsa);
2953 break;
2954 case OPAQUE_TYPE_ROUTER_INFORMATION_LSA:
2955 rc = ospf_te_delete_ri(ted, lsa);
2956 break;
2957 case OPAQUE_TYPE_EXTENDED_PREFIX_LSA:
2958 rc = ospf_te_delete_ext_pref(ted, lsa);
2959 break;
2960 case OPAQUE_TYPE_EXTENDED_LINK_LSA:
2961 rc = ospf_te_delete_ext_link(ted, lsa);
2962 break;
2963 default:
2964 break;
2965 }
2966
2967 return rc;
2968 }
2969
2970 /**
2971 * Update Traffic Engineering Database Elements that correspond to the received
2972 * OSPF LSA. If LSA age is equal to MAX_AGE, call deletion function instead.
2973 *
2974 * @param lsa OSPF Link State Advertisement
2975 *
2976 * @return 0 if success, -1 otherwise
2977 */
2978 static int ospf_mpls_te_lsa_update(struct ospf_lsa *lsa)
2979 {
2980
2981 uint8_t rc;
2982
2983 /* Check that MPLS-TE is active */
2984 if (!OspfMplsTE.enabled || !OspfMplsTE.ted)
2985 return 0;
2986
2987 /* Sanity Check */
2988 if (lsa == NULL) {
2989 flog_warn(EC_OSPF_LSA_NULL, "TE (%s): Abort! LSA is NULL",
2990 __func__);
2991 return -1;
2992 }
2993
2994 /* If LSA is MAX_AGE, remove corresponding Link State element */
2995 if (IS_LSA_MAXAGE(lsa)) {
2996 switch (lsa->data->type) {
2997 case OSPF_ROUTER_LSA:
2998 rc = ospf_te_delete_router_lsa(OspfMplsTE.ted, lsa);
2999 break;
3000 case OSPF_OPAQUE_AREA_LSA:
3001 case OSPF_OPAQUE_AS_LSA:
3002 rc = ospf_te_delete_opaque_lsa(OspfMplsTE.ted, lsa);
3003 break;
3004 default:
3005 rc = 0;
3006 break;
3007 }
3008 } else {
3009 /* Parse LSA to Update corresponding Link State element */
3010 switch (lsa->data->type) {
3011 case OSPF_ROUTER_LSA:
3012 rc = ospf_te_parse_router_lsa(OspfMplsTE.ted, lsa);
3013 break;
3014 case OSPF_OPAQUE_AREA_LSA:
3015 case OSPF_OPAQUE_AS_LSA:
3016 rc = ospf_te_parse_opaque_lsa(OspfMplsTE.ted, lsa);
3017 break;
3018 default:
3019 rc = 0;
3020 break;
3021 }
3022 }
3023
3024 return rc;
3025 }
3026
3027 /**
3028 * Delete Traffic Engineering Database element from OSPF LSA. This function
3029 * process only self LSA (i.e. advertised by the router) which reach MAX_AGE
3030 * as LSA deleted by neighbor routers are Flushed (i.e. advertised with
3031 * age == MAX_AGE) and processed by ospf_mpls_te_lsa_update() function.
3032 *
3033 * @param lsa OSPF Link State Advertisement
3034 *
3035 * @return 0 if success, -1 otherwise
3036 */
3037 static int ospf_mpls_te_lsa_delete(struct ospf_lsa *lsa)
3038 {
3039
3040 uint8_t rc;
3041
3042 /* Check that MPLS-TE is active */
3043 if (!OspfMplsTE.enabled || !OspfMplsTE.ted)
3044 return 0;
3045
3046 /* Sanity Check */
3047 if (lsa == NULL) {
3048 flog_warn(EC_OSPF_LSA_NULL, "TE (%s): Abort! LSA is NULL",
3049 __func__);
3050 return -1;
3051 }
3052
3053 /*
3054 * Process only self LSAs that reach MAX_AGE. Indeed, when the router
3055 * need to update or refresh an LSA, it first removes the old LSA from
3056 * the LSDB and then insert the new one. Thus, to avoid removing
3057 * corresponding Link State element and loosing some parameters
3058 * instead of just updating it, only self LSAs that reach MAX_AGE are
3059 * processed here. Other LSAs are processed by ospf_mpls_te_lsa_update()
3060 * and eventually removed when LSA age is MAX_AGE i.e. LSA is flushed
3061 * by the originator.
3062 */
3063 if (!IS_LSA_SELF(lsa) || !IS_LSA_MAXAGE(lsa))
3064 return 0;
3065
3066 /* Parse Link State information */
3067 switch (lsa->data->type) {
3068 case OSPF_ROUTER_LSA:
3069 rc = ospf_te_delete_router_lsa(OspfMplsTE.ted, lsa);
3070 break;
3071 case OSPF_OPAQUE_AREA_LSA:
3072 case OSPF_OPAQUE_AS_LSA:
3073 rc = ospf_te_delete_opaque_lsa(OspfMplsTE.ted, lsa);
3074 break;
3075 default:
3076 rc = 0;
3077 break;
3078 }
3079
3080 return rc;
3081 }
3082
3083 /**
3084 * Send the whole Link State Traffic Engineering Database to the consumer that
3085 * request it through a ZAPI Link State Synchronous Opaque Message.
3086 *
3087 * @param info ZAPI Opaque message
3088 *
3089 * @return 0 if success, -1 otherwise
3090 */
3091 int ospf_te_sync_ted(struct zapi_opaque_reg_info dst)
3092 {
3093 int rc = -1;
3094
3095 /* Check that MPLS-TE and TE distribution are enabled */
3096 if (!OspfMplsTE.enabled || !OspfMplsTE.export)
3097 return rc;
3098
3099 rc = ls_sync_ted(OspfMplsTE.ted, zclient, &dst);
3100
3101 return rc;
3102 }
3103
3104 /**
3105 * Initialize Traffic Engineering Database from the various OSPF Link State
3106 * Database (LSDB).
3107 *
3108 * @param ted Link State Traffice Engineering Database
3109 * @param ospf OSPF main structure
3110 */
3111 static void ospf_te_init_ted(struct ls_ted *ted, struct ospf *ospf)
3112 {
3113 struct listnode *node, *nnode;
3114 struct route_node *rn;
3115 struct ospf_area *area;
3116 struct ospf_lsa *lsa;
3117
3118 /* Iterate over all areas. */
3119 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
3120 if (!area->lsdb)
3121 continue;
3122
3123 /* Parse all Router LSAs from the area LSDB */
3124 LSDB_LOOP (ROUTER_LSDB(area), rn, lsa)
3125 ospf_te_parse_router_lsa(ted, lsa);
3126
3127 /* Parse all Opaque LSAs from the area LSDB */
3128 LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
3129 ospf_te_parse_opaque_lsa(ted, lsa);
3130 }
3131
3132 /* Parse AS-external opaque LSAs from OSPF LSDB */
3133 if (ospf->lsdb) {
3134 LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
3135 ospf_te_parse_opaque_lsa(ted, lsa);
3136 }
3137
3138 }
3139
3140 /*------------------------------------------------------------------------*
3141 * Following are vty session control functions.
3142 *------------------------------------------------------------------------*/
3143 #define check_tlv_size(size, msg) \
3144 do { \
3145 if (ntohs(tlvh->length) > size) { \
3146 if (vty != NULL) \
3147 vty_out(vty, " Wrong %s TLV size: %d(%d)\n", \
3148 msg, ntohs(tlvh->length), size); \
3149 else \
3150 zlog_debug(" Wrong %s TLV size: %d(%d)", \
3151 msg, ntohs(tlvh->length), size); \
3152 return size + TLV_HDR_SIZE; \
3153 } \
3154 } while (0)
3155
3156 static uint16_t show_vty_router_addr(struct vty *vty, struct tlv_header *tlvh)
3157 {
3158 struct te_tlv_router_addr *top = (struct te_tlv_router_addr *)tlvh;
3159
3160 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "Router Address");
3161
3162 if (vty != NULL)
3163 vty_out(vty, " Router-Address: %pI4\n", &top->value);
3164 else
3165 zlog_debug(" Router-Address: %pI4", &top->value);
3166
3167 return TLV_SIZE(tlvh);
3168 }
3169
3170 static uint16_t show_vty_link_header(struct vty *vty, struct tlv_header *tlvh,
3171 size_t buf_size)
3172 {
3173 struct te_tlv_link *top = (struct te_tlv_link *)tlvh;
3174
3175 if (TLV_SIZE(tlvh) > buf_size) {
3176 if (vty != NULL)
3177 vty_out(vty,
3178 " TLV size %d exceeds buffer size. Abort!",
3179 TLV_SIZE(tlvh));
3180 else
3181 zlog_debug(
3182 " TLV size %d exceeds buffer size. Abort!",
3183 TLV_SIZE(tlvh));
3184 return buf_size;
3185 }
3186
3187 if (vty != NULL)
3188 vty_out(vty, " Link: %u octets of data\n",
3189 ntohs(top->header.length));
3190 else
3191 zlog_debug(" Link: %u octets of data",
3192 ntohs(top->header.length));
3193
3194 return TLV_HDR_SIZE; /* Here is special, not "TLV_SIZE". */
3195 }
3196
3197 static uint16_t show_vty_link_subtlv_link_type(struct vty *vty,
3198 struct tlv_header *tlvh)
3199 {
3200 struct te_link_subtlv_link_type *top;
3201 const char *cp = "Unknown";
3202
3203 check_tlv_size(TE_LINK_SUBTLV_TYPE_SIZE, "Link Type");
3204
3205 top = (struct te_link_subtlv_link_type *)tlvh;
3206 switch (top->link_type.value) {
3207 case LINK_TYPE_SUBTLV_VALUE_PTP:
3208 cp = "Point-to-point";
3209 break;
3210 case LINK_TYPE_SUBTLV_VALUE_MA:
3211 cp = "Multiaccess";
3212 break;
3213 default:
3214 break;
3215 }
3216
3217 if (vty != NULL)
3218 vty_out(vty, " Link-Type: %s (%u)\n", cp,
3219 top->link_type.value);
3220 else
3221 zlog_debug(" Link-Type: %s (%u)", cp, top->link_type.value);
3222
3223 return TLV_SIZE(tlvh);
3224 }
3225
3226 static uint16_t show_vty_link_subtlv_link_id(struct vty *vty,
3227 struct tlv_header *tlvh)
3228 {
3229 struct te_link_subtlv_link_id *top;
3230
3231 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "Link ID");
3232
3233 top = (struct te_link_subtlv_link_id *)tlvh;
3234 if (vty != NULL)
3235 vty_out(vty, " Link-ID: %pI4\n", &top->value);
3236 else
3237 zlog_debug(" Link-ID: %pI4", &top->value);
3238
3239 return TLV_SIZE(tlvh);
3240 }
3241
3242 static uint16_t show_vty_link_subtlv_lclif_ipaddr(struct vty *vty,
3243 struct tlv_header *tlvh,
3244 size_t buf_size)
3245 {
3246 struct te_link_subtlv_lclif_ipaddr *top;
3247 int i, n;
3248
3249 if (TLV_SIZE(tlvh) > buf_size) {
3250 if (vty != NULL)
3251 vty_out(vty,
3252 " TLV size %d exceeds buffer size. Abort!",
3253 TLV_SIZE(tlvh));
3254 else
3255 zlog_debug(
3256 " TLV size %d exceeds buffer size. Abort!",
3257 TLV_SIZE(tlvh));
3258 return buf_size;
3259 }
3260
3261 top = (struct te_link_subtlv_lclif_ipaddr *)tlvh;
3262 n = ntohs(tlvh->length) / sizeof(top->value[0]);
3263
3264 if (vty != NULL)
3265 vty_out(vty, " Local Interface IP Address(es): %d\n", n);
3266 else
3267 zlog_debug(" Local Interface IP Address(es): %d", n);
3268
3269 for (i = 0; i < n; i++) {
3270 if (vty != NULL)
3271 vty_out(vty, " #%d: %pI4\n", i, &top->value[i]);
3272 else
3273 zlog_debug(" #%d: %pI4", i, &top->value[i]);
3274 }
3275 return TLV_SIZE(tlvh);
3276 }
3277
3278 static uint16_t show_vty_link_subtlv_rmtif_ipaddr(struct vty *vty,
3279 struct tlv_header *tlvh,
3280 size_t buf_size)
3281 {
3282 struct te_link_subtlv_rmtif_ipaddr *top;
3283 int i, n;
3284
3285 if (TLV_SIZE(tlvh) > buf_size) {
3286 if (vty != NULL)
3287 vty_out(vty,
3288 " TLV size %d exceeds buffer size. Abort!",
3289 TLV_SIZE(tlvh));
3290 else
3291 zlog_debug(
3292 " TLV size %d exceeds buffer size. Abort!",
3293 TLV_SIZE(tlvh));
3294 return buf_size;
3295 }
3296
3297 top = (struct te_link_subtlv_rmtif_ipaddr *)tlvh;
3298 n = ntohs(tlvh->length) / sizeof(top->value[0]);
3299 if (vty != NULL)
3300 vty_out(vty, " Remote Interface IP Address(es): %d\n", n);
3301 else
3302 zlog_debug(" Remote Interface IP Address(es): %d", n);
3303
3304 for (i = 0; i < n; i++) {
3305 if (vty != NULL)
3306 vty_out(vty, " #%d: %pI4\n", i, &top->value[i]);
3307 else
3308 zlog_debug(" #%d: %pI4", i, &top->value[i]);
3309 }
3310 return TLV_SIZE(tlvh);
3311 }
3312
3313 static uint16_t show_vty_link_subtlv_te_metric(struct vty *vty,
3314 struct tlv_header *tlvh)
3315 {
3316 struct te_link_subtlv_te_metric *top;
3317
3318 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "TE Metric");
3319
3320 top = (struct te_link_subtlv_te_metric *)tlvh;
3321 if (vty != NULL)
3322 vty_out(vty, " Traffic Engineering Metric: %u\n",
3323 (uint32_t)ntohl(top->value));
3324 else
3325 zlog_debug(" Traffic Engineering Metric: %u",
3326 (uint32_t)ntohl(top->value));
3327
3328 return TLV_SIZE(tlvh);
3329 }
3330
3331 static uint16_t show_vty_link_subtlv_max_bw(struct vty *vty,
3332 struct tlv_header *tlvh)
3333 {
3334 struct te_link_subtlv_max_bw *top;
3335 float fval;
3336
3337 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "Maximum Bandwidth");
3338
3339 top = (struct te_link_subtlv_max_bw *)tlvh;
3340 fval = ntohf(top->value);
3341
3342 if (vty != NULL)
3343 vty_out(vty, " Maximum Bandwidth: %g (Bytes/sec)\n", fval);
3344 else
3345 zlog_debug(" Maximum Bandwidth: %g (Bytes/sec)", fval);
3346
3347 return TLV_SIZE(tlvh);
3348 }
3349
3350 static uint16_t show_vty_link_subtlv_max_rsv_bw(struct vty *vty,
3351 struct tlv_header *tlvh)
3352 {
3353 struct te_link_subtlv_max_rsv_bw *top;
3354 float fval;
3355
3356 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "Maximum Reservable Bandwidth");
3357
3358 top = (struct te_link_subtlv_max_rsv_bw *)tlvh;
3359 fval = ntohf(top->value);
3360
3361 if (vty != NULL)
3362 vty_out(vty, " Maximum Reservable Bandwidth: %g (Bytes/sec)\n",
3363 fval);
3364 else
3365 zlog_debug(" Maximum Reservable Bandwidth: %g (Bytes/sec)",
3366 fval);
3367
3368 return TLV_SIZE(tlvh);
3369 }
3370
3371 static uint16_t show_vty_link_subtlv_unrsv_bw(struct vty *vty,
3372 struct tlv_header *tlvh)
3373 {
3374 struct te_link_subtlv_unrsv_bw *top;
3375 float fval1, fval2;
3376 int i;
3377
3378 check_tlv_size(TE_LINK_SUBTLV_UNRSV_SIZE, "Unreserved Bandwidth");
3379
3380 top = (struct te_link_subtlv_unrsv_bw *)tlvh;
3381 if (vty != NULL)
3382 vty_out(vty,
3383 " Unreserved Bandwidth per Class Type in Byte/s:\n");
3384 else
3385 zlog_debug(
3386 " Unreserved Bandwidth per Class Type in Byte/s:");
3387 for (i = 0; i < MAX_CLASS_TYPE; i += 2) {
3388 fval1 = ntohf(top->value[i]);
3389 fval2 = ntohf(top->value[i + 1]);
3390
3391 if (vty != NULL)
3392 vty_out(vty,
3393 " [%d]: %g (Bytes/sec),\t[%d]: %g (Bytes/sec)\n",
3394 i, fval1, i + 1, fval2);
3395 else
3396 zlog_debug(
3397 " [%d]: %g (Bytes/sec), [%d]: %g (Bytes/sec)",
3398 i, fval1, i + 1, fval2);
3399 }
3400
3401 return TLV_SIZE(tlvh);
3402 }
3403
3404 static uint16_t show_vty_link_subtlv_rsc_clsclr(struct vty *vty,
3405 struct tlv_header *tlvh)
3406 {
3407 struct te_link_subtlv_rsc_clsclr *top;
3408
3409 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "Resource class/color");
3410
3411 top = (struct te_link_subtlv_rsc_clsclr *)tlvh;
3412 if (vty != NULL)
3413 vty_out(vty, " Resource class/color: 0x%x\n",
3414 (uint32_t)ntohl(top->value));
3415 else
3416 zlog_debug(" Resource Class/Color: 0x%x",
3417 (uint32_t)ntohl(top->value));
3418
3419 return TLV_SIZE(tlvh);
3420 }
3421
3422 static uint16_t show_vty_link_subtlv_lrrid(struct vty *vty,
3423 struct tlv_header *tlvh)
3424 {
3425 struct te_link_subtlv_lrrid *top;
3426
3427 check_tlv_size(TE_LINK_SUBTLV_LRRID_SIZE, "Local/Remote Router ID");
3428
3429 top = (struct te_link_subtlv_lrrid *)tlvh;
3430
3431 if (vty != NULL) {
3432 vty_out(vty, " Local TE Router ID: %pI4\n",
3433 &top->local);
3434 vty_out(vty, " Remote TE Router ID: %pI4\n",
3435 &top->remote);
3436 } else {
3437 zlog_debug(" Local TE Router ID: %pI4",
3438 &top->local);
3439 zlog_debug(" Remote TE Router ID: %pI4",
3440 &top->remote);
3441 }
3442
3443 return TLV_SIZE(tlvh);
3444 }
3445
3446 static uint16_t show_vty_link_subtlv_llri(struct vty *vty,
3447 struct tlv_header *tlvh)
3448 {
3449 struct te_link_subtlv_llri *top;
3450
3451 check_tlv_size(TE_LINK_SUBTLV_LLRI_SIZE, "Link Local/Remote ID");
3452
3453 top = (struct te_link_subtlv_llri *)tlvh;
3454
3455 if (vty != NULL) {
3456 vty_out(vty, " Link Local ID: %d\n",
3457 (uint32_t)ntohl(top->local));
3458 vty_out(vty, " Link Remote ID: %d\n",
3459 (uint32_t)ntohl(top->remote));
3460 } else {
3461 zlog_debug(" Link Local ID: %d",
3462 (uint32_t)ntohl(top->local));
3463 zlog_debug(" Link Remote ID: %d",
3464 (uint32_t)ntohl(top->remote));
3465 }
3466
3467 return TLV_SIZE(tlvh);
3468 }
3469
3470 static uint16_t show_vty_link_subtlv_rip(struct vty *vty,
3471 struct tlv_header *tlvh)
3472 {
3473 struct te_link_subtlv_rip *top;
3474
3475 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "Remote ASBR Address");
3476
3477 top = (struct te_link_subtlv_rip *)tlvh;
3478
3479 if (vty != NULL)
3480 vty_out(vty, " Inter-AS TE Remote ASBR IP address: %pI4\n",
3481 &top->value);
3482 else
3483 zlog_debug(" Inter-AS TE Remote ASBR IP address: %pI4",
3484 &top->value);
3485
3486 return TLV_SIZE(tlvh);
3487 }
3488
3489 static uint16_t show_vty_link_subtlv_ras(struct vty *vty,
3490 struct tlv_header *tlvh)
3491 {
3492 struct te_link_subtlv_ras *top;
3493
3494 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "Remote AS number");
3495
3496 top = (struct te_link_subtlv_ras *)tlvh;
3497
3498 if (vty != NULL)
3499 vty_out(vty, " Inter-AS TE Remote AS number: %u\n",
3500 ntohl(top->value));
3501 else
3502 zlog_debug(" Inter-AS TE Remote AS number: %u",
3503 ntohl(top->value));
3504
3505 return TLV_SIZE(tlvh);
3506 }
3507
3508 static uint16_t show_vty_link_subtlv_av_delay(struct vty *vty,
3509 struct tlv_header *tlvh)
3510 {
3511 struct te_link_subtlv_av_delay *top;
3512 uint32_t delay;
3513 uint32_t anomalous;
3514
3515 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "Average Link Delay");
3516
3517 top = (struct te_link_subtlv_av_delay *)tlvh;
3518 delay = (uint32_t)ntohl(top->value) & TE_EXT_MASK;
3519 anomalous = (uint32_t)ntohl(top->value) & TE_EXT_ANORMAL;
3520
3521 if (vty != NULL)
3522 vty_out(vty, " %s Average Link Delay: %d (micro-sec)\n",
3523 anomalous ? "Anomalous" : "Normal", delay);
3524 else
3525 zlog_debug(" %s Average Link Delay: %d (micro-sec)",
3526 anomalous ? "Anomalous" : "Normal", delay);
3527
3528 return TLV_SIZE(tlvh);
3529 }
3530
3531 static uint16_t show_vty_link_subtlv_mm_delay(struct vty *vty,
3532 struct tlv_header *tlvh)
3533 {
3534 struct te_link_subtlv_mm_delay *top;
3535 uint32_t low, high;
3536 uint32_t anomalous;
3537
3538 check_tlv_size(TE_LINK_SUBTLV_MM_DELAY_SIZE, "Min/Max Link Delay");
3539
3540 top = (struct te_link_subtlv_mm_delay *)tlvh;
3541 low = (uint32_t)ntohl(top->low) & TE_EXT_MASK;
3542 anomalous = (uint32_t)ntohl(top->low) & TE_EXT_ANORMAL;
3543 high = (uint32_t)ntohl(top->high);
3544
3545 if (vty != NULL)
3546 vty_out(vty, " %s Min/Max Link Delay: %d/%d (micro-sec)\n",
3547 anomalous ? "Anomalous" : "Normal", low, high);
3548 else
3549 zlog_debug(" %s Min/Max Link Delay: %d/%d (micro-sec)",
3550 anomalous ? "Anomalous" : "Normal", low, high);
3551
3552 return TLV_SIZE(tlvh);
3553 }
3554
3555 static uint16_t show_vty_link_subtlv_delay_var(struct vty *vty,
3556 struct tlv_header *tlvh)
3557 {
3558 struct te_link_subtlv_delay_var *top;
3559 uint32_t jitter;
3560
3561 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "Link Delay Variation");
3562
3563 top = (struct te_link_subtlv_delay_var *)tlvh;
3564 jitter = (uint32_t)ntohl(top->value) & TE_EXT_MASK;
3565
3566 if (vty != NULL)
3567 vty_out(vty, " Delay Variation: %d (micro-sec)\n", jitter);
3568 else
3569 zlog_debug(" Delay Variation: %d (micro-sec)", jitter);
3570
3571 return TLV_SIZE(tlvh);
3572 }
3573
3574 static uint16_t show_vty_link_subtlv_pkt_loss(struct vty *vty,
3575 struct tlv_header *tlvh)
3576 {
3577 struct te_link_subtlv_pkt_loss *top;
3578 uint32_t loss;
3579 uint32_t anomalous;
3580 float fval;
3581
3582 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "Link Loss");
3583
3584 top = (struct te_link_subtlv_pkt_loss *)tlvh;
3585 loss = (uint32_t)ntohl(top->value) & TE_EXT_MASK;
3586 fval = (float)(loss * LOSS_PRECISION);
3587 anomalous = (uint32_t)ntohl(top->value) & TE_EXT_ANORMAL;
3588
3589 if (vty != NULL)
3590 vty_out(vty, " %s Link Loss: %g (%%)\n",
3591 anomalous ? "Anomalous" : "Normal", fval);
3592 else
3593 zlog_debug(" %s Link Loss: %g (%%)",
3594 anomalous ? "Anomalous" : "Normal", fval);
3595
3596 return TLV_SIZE(tlvh);
3597 }
3598
3599 static uint16_t show_vty_link_subtlv_res_bw(struct vty *vty,
3600 struct tlv_header *tlvh)
3601 {
3602 struct te_link_subtlv_res_bw *top;
3603 float fval;
3604
3605 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "Residual Bandwidth");
3606
3607 top = (struct te_link_subtlv_res_bw *)tlvh;
3608 fval = ntohf(top->value);
3609
3610 if (vty != NULL)
3611 vty_out(vty,
3612 " Unidirectional Residual Bandwidth: %g (Bytes/sec)\n",
3613 fval);
3614 else
3615 zlog_debug(
3616 " Unidirectional Residual Bandwidth: %g (Bytes/sec)",
3617 fval);
3618
3619 return TLV_SIZE(tlvh);
3620 }
3621
3622 static uint16_t show_vty_link_subtlv_ava_bw(struct vty *vty,
3623 struct tlv_header *tlvh)
3624 {
3625 struct te_link_subtlv_ava_bw *top;
3626 float fval;
3627
3628 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "Available Bandwidth");
3629
3630 top = (struct te_link_subtlv_ava_bw *)tlvh;
3631 fval = ntohf(top->value);
3632
3633 if (vty != NULL)
3634 vty_out(vty,
3635 " Unidirectional Available Bandwidth: %g (Bytes/sec)\n",
3636 fval);
3637 else
3638 zlog_debug(
3639 " Unidirectional Available Bandwidth: %g (Bytes/sec)",
3640 fval);
3641
3642 return TLV_SIZE(tlvh);
3643 }
3644
3645 static uint16_t show_vty_link_subtlv_use_bw(struct vty *vty,
3646 struct tlv_header *tlvh)
3647 {
3648 struct te_link_subtlv_use_bw *top;
3649 float fval;
3650
3651 check_tlv_size(TE_LINK_SUBTLV_DEF_SIZE, "Utilized Bandwidth");
3652
3653 top = (struct te_link_subtlv_use_bw *)tlvh;
3654 fval = ntohf(top->value);
3655
3656 if (vty != NULL)
3657 vty_out(vty,
3658 " Unidirectional Utilized Bandwidth: %g (Bytes/sec)\n",
3659 fval);
3660 else
3661 zlog_debug(
3662 " Unidirectional Utilized Bandwidth: %g (Bytes/sec)",
3663 fval);
3664
3665 return TLV_SIZE(tlvh);
3666 }
3667
3668 static uint16_t show_vty_unknown_tlv(struct vty *vty, struct tlv_header *tlvh,
3669 size_t buf_size)
3670 {
3671 if (TLV_SIZE(tlvh) > buf_size) {
3672 if (vty != NULL)
3673 vty_out(vty,
3674 " TLV size %d exceeds buffer size. Abort!",
3675 TLV_SIZE(tlvh));
3676 else
3677 zlog_debug(
3678 " TLV size %d exceeds buffer size. Abort!",
3679 TLV_SIZE(tlvh));
3680 return buf_size;
3681 }
3682
3683 if (vty != NULL)
3684 vty_out(vty, " Unknown TLV: [type(0x%x), length(0x%x)]\n",
3685 ntohs(tlvh->type), ntohs(tlvh->length));
3686 else
3687 zlog_debug(" Unknown TLV: [type(0x%x), length(0x%x)]",
3688 ntohs(tlvh->type), ntohs(tlvh->length));
3689
3690 return TLV_SIZE(tlvh);
3691 }
3692
3693 static uint16_t ospf_mpls_te_show_link_subtlv(struct vty *vty,
3694 struct tlv_header *tlvh0,
3695 uint16_t subtotal, uint16_t total)
3696 {
3697 struct tlv_header *tlvh;
3698 uint16_t sum = subtotal;
3699
3700 for (tlvh = tlvh0; sum < total; tlvh = TLV_HDR_NEXT(tlvh)) {
3701 switch (ntohs(tlvh->type)) {
3702 case TE_LINK_SUBTLV_LINK_TYPE:
3703 sum += show_vty_link_subtlv_link_type(vty, tlvh);
3704 break;
3705 case TE_LINK_SUBTLV_LINK_ID:
3706 sum += show_vty_link_subtlv_link_id(vty, tlvh);
3707 break;
3708 case TE_LINK_SUBTLV_LCLIF_IPADDR:
3709 sum += show_vty_link_subtlv_lclif_ipaddr(vty, tlvh,
3710 total - sum);
3711 break;
3712 case TE_LINK_SUBTLV_RMTIF_IPADDR:
3713 sum += show_vty_link_subtlv_rmtif_ipaddr(vty, tlvh,
3714 total - sum);
3715 break;
3716 case TE_LINK_SUBTLV_TE_METRIC:
3717 sum += show_vty_link_subtlv_te_metric(vty, tlvh);
3718 break;
3719 case TE_LINK_SUBTLV_MAX_BW:
3720 sum += show_vty_link_subtlv_max_bw(vty, tlvh);
3721 break;
3722 case TE_LINK_SUBTLV_MAX_RSV_BW:
3723 sum += show_vty_link_subtlv_max_rsv_bw(vty, tlvh);
3724 break;
3725 case TE_LINK_SUBTLV_UNRSV_BW:
3726 sum += show_vty_link_subtlv_unrsv_bw(vty, tlvh);
3727 break;
3728 case TE_LINK_SUBTLV_RSC_CLSCLR:
3729 sum += show_vty_link_subtlv_rsc_clsclr(vty, tlvh);
3730 break;
3731 case TE_LINK_SUBTLV_LRRID:
3732 sum += show_vty_link_subtlv_lrrid(vty, tlvh);
3733 break;
3734 case TE_LINK_SUBTLV_LLRI:
3735 sum += show_vty_link_subtlv_llri(vty, tlvh);
3736 break;
3737 case TE_LINK_SUBTLV_RIP:
3738 sum += show_vty_link_subtlv_rip(vty, tlvh);
3739 break;
3740 case TE_LINK_SUBTLV_RAS:
3741 sum += show_vty_link_subtlv_ras(vty, tlvh);
3742 break;
3743 case TE_LINK_SUBTLV_AV_DELAY:
3744 sum += show_vty_link_subtlv_av_delay(vty, tlvh);
3745 break;
3746 case TE_LINK_SUBTLV_MM_DELAY:
3747 sum += show_vty_link_subtlv_mm_delay(vty, tlvh);
3748 break;
3749 case TE_LINK_SUBTLV_DELAY_VAR:
3750 sum += show_vty_link_subtlv_delay_var(vty, tlvh);
3751 break;
3752 case TE_LINK_SUBTLV_PKT_LOSS:
3753 sum += show_vty_link_subtlv_pkt_loss(vty, tlvh);
3754 break;
3755 case TE_LINK_SUBTLV_RES_BW:
3756 sum += show_vty_link_subtlv_res_bw(vty, tlvh);
3757 break;
3758 case TE_LINK_SUBTLV_AVA_BW:
3759 sum += show_vty_link_subtlv_ava_bw(vty, tlvh);
3760 break;
3761 case TE_LINK_SUBTLV_USE_BW:
3762 sum += show_vty_link_subtlv_use_bw(vty, tlvh);
3763 break;
3764 default:
3765 sum += show_vty_unknown_tlv(vty, tlvh, total - sum);
3766 break;
3767 }
3768 }
3769 return sum;
3770 }
3771
3772 static void ospf_mpls_te_show_info(struct vty *vty, struct json_object *json,
3773 struct ospf_lsa *lsa)
3774 {
3775 struct lsa_header *lsah = lsa->data;
3776 struct tlv_header *tlvh, *next;
3777 uint16_t sum, total;
3778 uint16_t (*subfunc)(struct vty * vty, struct tlv_header * tlvh,
3779 uint16_t subtotal, uint16_t total) = NULL;
3780
3781 if (json)
3782 return;
3783
3784 sum = 0;
3785 total = lsa->size - OSPF_LSA_HEADER_SIZE;
3786
3787 for (tlvh = TLV_HDR_TOP(lsah); sum < total && tlvh;
3788 tlvh = (next ? next : TLV_HDR_NEXT(tlvh))) {
3789 if (subfunc != NULL) {
3790 sum = (*subfunc)(vty, tlvh, sum, total);
3791 next = (struct tlv_header *)((char *)tlvh + sum);
3792 subfunc = NULL;
3793 continue;
3794 }
3795
3796 next = NULL;
3797 switch (ntohs(tlvh->type)) {
3798 case TE_TLV_ROUTER_ADDR:
3799 sum += show_vty_router_addr(vty, tlvh);
3800 break;
3801 case TE_TLV_LINK:
3802 sum += show_vty_link_header(vty, tlvh, total - sum);
3803 subfunc = ospf_mpls_te_show_link_subtlv;
3804 next = TLV_DATA(tlvh);
3805 break;
3806 default:
3807 sum += show_vty_unknown_tlv(vty, tlvh, total - sum);
3808 break;
3809 }
3810 }
3811 return;
3812 }
3813
3814 static void ospf_mpls_te_config_write_router(struct vty *vty)
3815 {
3816
3817 if (OspfMplsTE.enabled) {
3818 vty_out(vty, " mpls-te on\n");
3819 vty_out(vty, " mpls-te router-address %pI4\n",
3820 &OspfMplsTE.router_addr.value);
3821
3822 if (OspfMplsTE.inter_as == AS)
3823 vty_out(vty, " mpls-te inter-as as\n");
3824 if (OspfMplsTE.inter_as == Area)
3825 vty_out(vty, " mpls-te inter-as area %pI4 \n",
3826 &OspfMplsTE.interas_areaid);
3827 if (OspfMplsTE.export)
3828 vty_out(vty, " mpls-te export\n");
3829 }
3830 return;
3831 }
3832
3833 /*------------------------------------------------------------------------*
3834 * Following are vty command functions.
3835 *------------------------------------------------------------------------*/
3836
3837 DEFUN (ospf_mpls_te_on,
3838 ospf_mpls_te_on_cmd,
3839 "mpls-te on",
3840 MPLS_TE_STR
3841 "Enable the MPLS-TE functionality\n")
3842 {
3843 VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
3844 struct listnode *node;
3845 struct mpls_te_link *lp;
3846
3847 if (OspfMplsTE.enabled)
3848 return CMD_SUCCESS;
3849
3850 ote_debug("MPLS-TE: OFF -> ON");
3851
3852 OspfMplsTE.enabled = true;
3853
3854 /* Reoriginate RFC3630 & RFC6827 Links */
3855 ospf_mpls_te_foreach_area(ospf_mpls_te_lsa_schedule,
3856 REORIGINATE_THIS_LSA);
3857
3858 /* Reoriginate LSA if INTER-AS is always on */
3859 if (OspfMplsTE.inter_as != Off) {
3860 for (ALL_LIST_ELEMENTS_RO(OspfMplsTE.iflist, node, lp)) {
3861 if (IS_INTER_AS(lp->type)) {
3862 ospf_mpls_te_lsa_schedule(lp,
3863 REORIGINATE_THIS_LSA);
3864 }
3865 }
3866 }
3867
3868 /* Create TED and initialize it */
3869 OspfMplsTE.ted = ls_ted_new(1, "OSPF", 0);
3870 if (!OspfMplsTE.ted) {
3871 vty_out(vty, "Unable to create Link State Data Base\n");
3872 return CMD_WARNING;
3873 }
3874 ospf_te_init_ted(OspfMplsTE.ted, ospf);
3875
3876 return CMD_SUCCESS;
3877 }
3878
3879 DEFUN (no_ospf_mpls_te,
3880 no_ospf_mpls_te_cmd,
3881 "no mpls-te [on]",
3882 NO_STR
3883 MPLS_TE_STR
3884 "Disable the MPLS-TE functionality\n")
3885 {
3886 VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
3887 struct listnode *node, *nnode;
3888 struct mpls_te_link *lp;
3889
3890 if (!OspfMplsTE.enabled)
3891 return CMD_SUCCESS;
3892
3893 ote_debug("MPLS-TE: ON -> OFF");
3894
3895 /* Remove TED */
3896 ls_ted_del_all(&OspfMplsTE.ted);
3897 OspfMplsTE.enabled = false;
3898
3899 /* Flush all TE Opaque LSAs */
3900 for (ALL_LIST_ELEMENTS(OspfMplsTE.iflist, node, nnode, lp))
3901 if (CHECK_FLAG(lp->flags, LPFLG_LSA_ENGAGED))
3902 ospf_mpls_te_lsa_schedule(lp, FLUSH_THIS_LSA);
3903
3904 /*
3905 * This resets the OspfMplsTE.inter_as to its initial state.
3906 * This is to avoid having an inter-as value different from
3907 * Off when mpls-te gets restarted (after being removed)
3908 */
3909 OspfMplsTE.inter_as = Off;
3910
3911 return CMD_SUCCESS;
3912 }
3913
3914 DEFUN (ospf_mpls_te_router_addr,
3915 ospf_mpls_te_router_addr_cmd,
3916 "mpls-te router-address A.B.C.D",
3917 MPLS_TE_STR
3918 "Stable IP address of the advertising router\n"
3919 "MPLS-TE router address in IPv4 address format\n")
3920 {
3921 VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
3922 int idx_ipv4 = 2;
3923 struct te_tlv_router_addr *ra = &OspfMplsTE.router_addr;
3924 struct in_addr value;
3925
3926 if (!inet_aton(argv[idx_ipv4]->arg, &value)) {
3927 vty_out(vty, "Please specify Router-Addr by A.B.C.D\n");
3928 return CMD_WARNING;
3929 }
3930
3931 if (ntohs(ra->header.type) == 0
3932 || ntohl(ra->value.s_addr) != ntohl(value.s_addr)) {
3933 struct listnode *node, *nnode;
3934 struct mpls_te_link *lp;
3935 int need_to_reoriginate = 0;
3936
3937 set_mpls_te_router_addr(value);
3938
3939 if (!OspfMplsTE.enabled)
3940 return CMD_SUCCESS;
3941
3942 for (ALL_LIST_ELEMENTS(OspfMplsTE.iflist, node, nnode, lp)) {
3943 if ((lp->area == NULL) || IS_FLOOD_AS(lp->flags))
3944 continue;
3945
3946 if (!CHECK_FLAG(lp->flags, LPFLG_LSA_ENGAGED)) {
3947 need_to_reoriginate = 1;
3948 break;
3949 }
3950 }
3951
3952 for (ALL_LIST_ELEMENTS(OspfMplsTE.iflist, node, nnode, lp)) {
3953 if ((lp->area == NULL) || IS_FLOOD_AS(lp->flags))
3954 continue;
3955
3956 if (need_to_reoriginate)
3957 SET_FLAG(lp->flags, LPFLG_LSA_FORCED_REFRESH);
3958 else
3959 ospf_mpls_te_lsa_schedule(lp, REFRESH_THIS_LSA);
3960 }
3961
3962 if (need_to_reoriginate)
3963 ospf_mpls_te_foreach_area(ospf_mpls_te_lsa_schedule,
3964 REORIGINATE_THIS_LSA);
3965 }
3966
3967 return CMD_SUCCESS;
3968 }
3969
3970 static int set_inter_as_mode(struct vty *vty, const char *mode_name,
3971 const char *area_id)
3972 {
3973 enum inter_as_mode mode;
3974 struct listnode *node;
3975 struct mpls_te_link *lp;
3976 int format;
3977
3978 if (OspfMplsTE.enabled) {
3979
3980 /* Read and Check inter_as mode */
3981 if (strcmp(mode_name, "as") == 0)
3982 mode = AS;
3983 else if (strcmp(mode_name, "area") == 0) {
3984 mode = Area;
3985 VTY_GET_OSPF_AREA_ID(OspfMplsTE.interas_areaid, format,
3986 area_id);
3987 } else {
3988 vty_out(vty,
3989 "Unknown mode. Please choose between as or area\n");
3990 return CMD_WARNING;
3991 }
3992
3993 ote_debug(
3994 "MPLS-TE (%s): Inter-AS enable with %s flooding support",
3995 __func__, mode2text[mode]);
3996
3997 /* Enable mode and re-originate LSA if needed */
3998 if ((OspfMplsTE.inter_as == Off)
3999 && (mode != OspfMplsTE.inter_as)) {
4000 OspfMplsTE.inter_as = mode;
4001 /* Re-originate all InterAS-TEv2 LSA */
4002 for (ALL_LIST_ELEMENTS_RO(OspfMplsTE.iflist, node,
4003 lp)) {
4004 if (IS_INTER_AS(lp->type)) {
4005 if (mode == AS)
4006 SET_FLAG(lp->flags,
4007 LPFLG_LSA_FLOOD_AS);
4008 else
4009 UNSET_FLAG(lp->flags,
4010 LPFLG_LSA_FLOOD_AS);
4011 ospf_mpls_te_lsa_schedule(
4012 lp, REORIGINATE_THIS_LSA);
4013 }
4014 }
4015 } else {
4016 vty_out(vty,
4017 "Please change Inter-AS support to disable first before going to mode %s\n",
4018 mode2text[mode]);
4019 return CMD_WARNING;
4020 }
4021 } else {
4022 vty_out(vty, "mpls-te has not been turned on\n");
4023 return CMD_WARNING;
4024 }
4025 return CMD_SUCCESS;
4026 }
4027
4028
4029 DEFUN (ospf_mpls_te_inter_as_as,
4030 ospf_mpls_te_inter_as_cmd,
4031 "mpls-te inter-as as",
4032 MPLS_TE_STR
4033 "Configure MPLS-TE Inter-AS support\n"
4034 "AS native mode self originate INTER_AS LSA with Type 11 (as flooding scope)\n")
4035 {
4036 return set_inter_as_mode(vty, "as", "");
4037 }
4038
4039 DEFUN (ospf_mpls_te_inter_as_area,
4040 ospf_mpls_te_inter_as_area_cmd,
4041 "mpls-te inter-as area <A.B.C.D|(0-4294967295)>",
4042 MPLS_TE_STR
4043 "Configure MPLS-TE Inter-AS support\n"
4044 "AREA native mode self originate INTER_AS LSA with Type 10 (area flooding scope)\n"
4045 "OSPF area ID in IP format\n"
4046 "OSPF area ID as decimal value\n")
4047 {
4048 int idx_ipv4_number = 3;
4049 return set_inter_as_mode(vty, "area", argv[idx_ipv4_number]->arg);
4050 }
4051
4052 DEFUN (no_ospf_mpls_te_inter_as,
4053 no_ospf_mpls_te_inter_as_cmd,
4054 "no mpls-te inter-as",
4055 NO_STR
4056 MPLS_TE_STR
4057 "Disable MPLS-TE Inter-AS support\n")
4058 {
4059
4060 struct listnode *node, *nnode;
4061 struct mpls_te_link *lp;
4062
4063 ote_debug("MPLS-TE: Inter-AS support OFF");
4064
4065 if ((OspfMplsTE.enabled) && (OspfMplsTE.inter_as != Off)) {
4066 /* Flush all Inter-AS LSA */
4067 for (ALL_LIST_ELEMENTS(OspfMplsTE.iflist, node, nnode, lp))
4068 if (IS_INTER_AS(lp->type)
4069 && CHECK_FLAG(lp->flags, LPFLG_LSA_ENGAGED))
4070 ospf_mpls_te_lsa_schedule(lp, FLUSH_THIS_LSA);
4071
4072 OspfMplsTE.inter_as = Off;
4073 }
4074
4075 return CMD_SUCCESS;
4076 }
4077
4078 DEFUN (ospf_mpls_te_export,
4079 ospf_mpls_te_export_cmd,
4080 "mpls-te export",
4081 MPLS_TE_STR
4082 "Export the MPLS-TE information as Link State\n")
4083 {
4084
4085 VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
4086
4087 if (OspfMplsTE.enabled) {
4088 if (ls_register(zclient, true) != 0) {
4089 vty_out(vty, "Unable to register Link State\n");
4090 return CMD_WARNING;
4091 }
4092 OspfMplsTE.export = true;
4093 } else {
4094 vty_out(vty, "mpls-te has not been turned on\n");
4095 return CMD_WARNING;
4096 }
4097 return CMD_SUCCESS;
4098 }
4099
4100
4101 DEFUN (no_ospf_mpls_te_export,
4102 no_ospf_mpls_te_export_cmd,
4103 "no mpls-te export",
4104 NO_STR
4105 MPLS_TE_STR
4106 "Stop export of the MPLS-TE information as Link State\n")
4107 {
4108
4109 VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
4110
4111 if (OspfMplsTE.export) {
4112 if (ls_unregister(zclient, true) != 0) {
4113 vty_out(vty, "Unable to unregister Link State\n");
4114 return CMD_WARNING;
4115 }
4116 OspfMplsTE.export = false;
4117 }
4118 return CMD_SUCCESS;
4119 }
4120
4121 DEFUN (show_ip_ospf_mpls_te_router,
4122 show_ip_ospf_mpls_te_router_cmd,
4123 "show ip ospf mpls-te router",
4124 SHOW_STR
4125 IP_STR
4126 OSPF_STR
4127 "MPLS-TE information\n"
4128 "MPLS-TE Router parameters\n")
4129 {
4130 if (OspfMplsTE.enabled) {
4131 vty_out(vty, "--- MPLS-TE router parameters ---\n");
4132
4133 if (ntohs(OspfMplsTE.router_addr.header.type) != 0)
4134 show_vty_router_addr(vty,
4135 &OspfMplsTE.router_addr.header);
4136 else
4137 vty_out(vty, " Router address is not set\n");
4138 vty_out(vty, " Link State distribution is %s\n",
4139 OspfMplsTE.export ? "Active" : "Inactive");
4140 }
4141 return CMD_SUCCESS;
4142 }
4143
4144 static void show_mpls_te_link_sub(struct vty *vty, struct interface *ifp)
4145 {
4146 struct mpls_te_link *lp;
4147
4148 if ((OspfMplsTE.enabled) && HAS_LINK_PARAMS(ifp) && !if_is_loopback(ifp)
4149 && if_is_up(ifp)
4150 && ((lp = lookup_linkparams_by_ifp(ifp)) != NULL)) {
4151 /* Continue only if interface is not passive or support Inter-AS
4152 * TEv2 */
4153 if (!(ospf_oi_count(ifp) > 0)) {
4154 if (IS_INTER_AS(lp->type)) {
4155 vty_out(vty,
4156 "-- Inter-AS TEv2 link parameters for %s --\n",
4157 ifp->name);
4158 } else {
4159 /* MPLS-TE is not activate on this interface */
4160 /* or this interface is passive and Inter-AS
4161 * TEv2 is not activate */
4162 vty_out(vty,
4163 " %s: MPLS-TE is disabled on this interface\n",
4164 ifp->name);
4165 return;
4166 }
4167 } else {
4168 vty_out(vty, "-- MPLS-TE link parameters for %s --\n",
4169 ifp->name);
4170 }
4171
4172 if (TLV_TYPE(lp->link_type) != 0)
4173 show_vty_link_subtlv_link_type(vty,
4174 &lp->link_type.header);
4175 if (TLV_TYPE(lp->link_id) != 0)
4176 show_vty_link_subtlv_link_id(vty, &lp->link_id.header);
4177 if (TLV_TYPE(lp->lclif_ipaddr) != 0)
4178 show_vty_link_subtlv_lclif_ipaddr(
4179 vty, &lp->lclif_ipaddr.header,
4180 lp->lclif_ipaddr.header.length);
4181 if (TLV_TYPE(lp->rmtif_ipaddr) != 0)
4182 show_vty_link_subtlv_rmtif_ipaddr(
4183 vty, &lp->rmtif_ipaddr.header,
4184 lp->rmtif_ipaddr.header.length);
4185 if (TLV_TYPE(lp->rip) != 0)
4186 show_vty_link_subtlv_rip(vty, &lp->rip.header);
4187 if (TLV_TYPE(lp->ras) != 0)
4188 show_vty_link_subtlv_ras(vty, &lp->ras.header);
4189 if (TLV_TYPE(lp->te_metric) != 0)
4190 show_vty_link_subtlv_te_metric(vty,
4191 &lp->te_metric.header);
4192 if (TLV_TYPE(lp->max_bw) != 0)
4193 show_vty_link_subtlv_max_bw(vty, &lp->max_bw.header);
4194 if (TLV_TYPE(lp->max_rsv_bw) != 0)
4195 show_vty_link_subtlv_max_rsv_bw(vty,
4196 &lp->max_rsv_bw.header);
4197 if (TLV_TYPE(lp->unrsv_bw) != 0)
4198 show_vty_link_subtlv_unrsv_bw(vty,
4199 &lp->unrsv_bw.header);
4200 if (TLV_TYPE(lp->rsc_clsclr) != 0)
4201 show_vty_link_subtlv_rsc_clsclr(vty,
4202 &lp->rsc_clsclr.header);
4203 if (TLV_TYPE(lp->av_delay) != 0)
4204 show_vty_link_subtlv_av_delay(vty,
4205 &lp->av_delay.header);
4206 if (TLV_TYPE(lp->mm_delay) != 0)
4207 show_vty_link_subtlv_mm_delay(vty,
4208 &lp->mm_delay.header);
4209 if (TLV_TYPE(lp->delay_var) != 0)
4210 show_vty_link_subtlv_delay_var(vty,
4211 &lp->delay_var.header);
4212 if (TLV_TYPE(lp->pkt_loss) != 0)
4213 show_vty_link_subtlv_pkt_loss(vty,
4214 &lp->pkt_loss.header);
4215 if (TLV_TYPE(lp->res_bw) != 0)
4216 show_vty_link_subtlv_res_bw(vty, &lp->res_bw.header);
4217 if (TLV_TYPE(lp->ava_bw) != 0)
4218 show_vty_link_subtlv_ava_bw(vty, &lp->ava_bw.header);
4219 if (TLV_TYPE(lp->use_bw) != 0)
4220 show_vty_link_subtlv_use_bw(vty, &lp->use_bw.header);
4221 vty_out(vty, "---------------\n\n");
4222 } else {
4223 vty_out(vty, " %s: MPLS-TE is disabled on this interface\n",
4224 ifp->name);
4225 }
4226
4227 return;
4228 }
4229
4230 DEFUN (show_ip_ospf_mpls_te_link,
4231 show_ip_ospf_mpls_te_link_cmd,
4232 "show ip ospf [vrf <NAME|all>] mpls-te interface [INTERFACE]",
4233 SHOW_STR
4234 IP_STR
4235 OSPF_STR
4236 VRF_CMD_HELP_STR
4237 "All VRFs\n"
4238 "MPLS-TE information\n"
4239 "Interface information\n"
4240 "Interface name\n")
4241 {
4242 struct vrf *vrf;
4243 int idx_interface = 0;
4244 struct interface *ifp = NULL;
4245 struct listnode *node;
4246 char *vrf_name = NULL;
4247 bool all_vrf = false;
4248 int inst = 0;
4249 int idx_vrf = 0;
4250 struct ospf *ospf = NULL;
4251
4252 if (argv_find(argv, argc, "vrf", &idx_vrf)) {
4253 vrf_name = argv[idx_vrf + 1]->arg;
4254 all_vrf = strmatch(vrf_name, "all");
4255 }
4256 argv_find(argv, argc, "INTERFACE", &idx_interface);
4257 /* vrf input is provided could be all or specific vrf*/
4258 if (vrf_name) {
4259 if (all_vrf) {
4260 for (ALL_LIST_ELEMENTS_RO(om->ospf, node, ospf)) {
4261 if (!ospf->oi_running)
4262 continue;
4263 vrf = vrf_lookup_by_id(ospf->vrf_id);
4264 FOR_ALL_INTERFACES (vrf, ifp)
4265 show_mpls_te_link_sub(vty, ifp);
4266 }
4267 return CMD_SUCCESS;
4268 }
4269 ospf = ospf_lookup_by_inst_name(inst, vrf_name);
4270 } else
4271 ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
4272 if (ospf == NULL || !ospf->oi_running)
4273 return CMD_SUCCESS;
4274
4275 vrf = vrf_lookup_by_id(ospf->vrf_id);
4276 if (!vrf)
4277 return CMD_SUCCESS;
4278 if (idx_interface) {
4279 ifp = if_lookup_by_name(
4280 argv[idx_interface]->arg,
4281 ospf->vrf_id);
4282 if (ifp == NULL) {
4283 vty_out(vty, "No such interface name in vrf %s\n",
4284 vrf->name);
4285 return CMD_SUCCESS;
4286 }
4287 }
4288 if (!ifp) {
4289 FOR_ALL_INTERFACES (vrf, ifp)
4290 show_mpls_te_link_sub(vty, ifp);
4291 return CMD_SUCCESS;
4292 }
4293
4294 show_mpls_te_link_sub(vty, ifp);
4295 return CMD_SUCCESS;
4296 }
4297
4298 DEFUN (show_ip_ospf_mpls_te_db,
4299 show_ip_ospf_mpls_te_db_cmd,
4300 "show ip ospf mpls-te database [<vertex [<self-originate|adv-router A.B.C.D>]|edge [A.B.C.D]|subnet [A.B.C.D/M]>] [verbose|json]",
4301 SHOW_STR
4302 IP_STR
4303 OSPF_STR
4304 "MPLS-TE information\n"
4305 "MPLS-TE database\n"
4306 "MPLS-TE Vertex\n"
4307 "Self-originated MPLS-TE router\n"
4308 "Advertised MPLS-TE router\n"
4309 "MPLS-TE router ID (as an IP address)\n"
4310 "MPLS-TE Edge\n"
4311 "MPLS-TE Edge ID (as an IP address)\n"
4312 "MPLS-TE Subnet\n"
4313 "MPLS-TE Subnet ID (as an IP prefix)\n"
4314 "Verbose output\n"
4315 JSON_STR)
4316 {
4317 int idx = 0;
4318 struct in_addr ip_addr;
4319 struct prefix pref;
4320 struct ls_vertex *vertex;
4321 struct ls_edge *edge;
4322 struct ls_subnet *subnet;
4323 uint64_t key;
4324 bool verbose = false;
4325 bool uj = use_json(argc, argv);
4326 json_object *json = NULL;
4327
4328 if (!OspfMplsTE.enabled || !OspfMplsTE.ted) {
4329 vty_out(vty, "MPLS-TE database is not enabled\n");
4330 return CMD_WARNING;
4331 }
4332
4333 if (uj)
4334 json = json_object_new_object();
4335
4336 if (argv[argc - 1]->arg && strmatch(argv[argc - 1]->text, "verbose"))
4337 verbose = true;
4338
4339 idx = 5;
4340 if (argv_find(argv, argc, "vertex", &idx)) {
4341 /* Show Vertex */
4342 if (argv_find(argv, argc, "self-originate", &idx))
4343 vertex = OspfMplsTE.ted->self;
4344 else if (argv_find(argv, argc, "adv-router", &idx)) {
4345 if (!inet_aton(argv[idx + 1]->arg, &ip_addr)) {
4346 vty_out(vty,
4347 "Specified Router ID %s is invalid\n",
4348 argv[idx + 1]->arg);
4349 return CMD_WARNING_CONFIG_FAILED;
4350 }
4351 /* Get the Vertex from the Link State Database */
4352 key = ((uint64_t)ntohl(ip_addr.s_addr)) & 0xffffffff;
4353 vertex = ls_find_vertex_by_key(OspfMplsTE.ted, key);
4354 if (!vertex) {
4355 vty_out(vty, "No vertex found for ID %pI4\n",
4356 &ip_addr);
4357 return CMD_WARNING;
4358 }
4359 } else
4360 vertex = NULL;
4361
4362 if (vertex)
4363 ls_show_vertex(vertex, vty, json, verbose);
4364 else
4365 ls_show_vertices(OspfMplsTE.ted, vty, json, verbose);
4366
4367 } else if (argv_find(argv, argc, "edge", &idx)) {
4368 /* Show Edge */
4369 if (argv_find(argv, argc, "A.B.C.D", &idx)) {
4370 if (!inet_aton(argv[idx]->arg, &ip_addr)) {
4371 vty_out(vty,
4372 "Specified Edge ID %s is invalid\n",
4373 argv[idx]->arg);
4374 return CMD_WARNING_CONFIG_FAILED;
4375 }
4376 /* Get the Edge from the Link State Database */
4377 key = ((uint64_t)ntohl(ip_addr.s_addr)) & 0xffffffff;
4378 edge = ls_find_edge_by_key(OspfMplsTE.ted, key);
4379 if (!edge) {
4380 vty_out(vty, "No edge found for ID %pI4\n",
4381 &ip_addr);
4382 return CMD_WARNING;
4383 }
4384 } else
4385 edge = NULL;
4386
4387 if (edge)
4388 ls_show_edge(edge, vty, json, verbose);
4389 else
4390 ls_show_edges(OspfMplsTE.ted, vty, json, verbose);
4391
4392 } else if (argv_find(argv, argc, "subnet", &idx)) {
4393 /* Show Subnet */
4394 if (argv_find(argv, argc, "A.B.C.D/M", &idx)) {
4395 if (!str2prefix(argv[idx]->arg, &pref)) {
4396 vty_out(vty, "Invalid prefix format %s\n",
4397 argv[idx]->arg);
4398 return CMD_WARNING_CONFIG_FAILED;
4399 }
4400 /* Get the Subnet from the Link State Database */
4401 subnet = ls_find_subnet(OspfMplsTE.ted, pref);
4402 if (!subnet) {
4403 vty_out(vty, "No subnet found for ID %pFX\n",
4404 &pref);
4405 return CMD_WARNING;
4406 }
4407 } else
4408 subnet = NULL;
4409
4410 if (subnet)
4411 ls_show_subnet(subnet, vty, json, verbose);
4412 else
4413 ls_show_subnets(OspfMplsTE.ted, vty, json, verbose);
4414
4415 } else {
4416 /* Show the complete TED */
4417 ls_show_ted(OspfMplsTE.ted, vty, json, verbose);
4418 }
4419
4420 if (uj)
4421 vty_json(vty, json);
4422 return CMD_SUCCESS;
4423 }
4424
4425 static void ospf_mpls_te_register_vty(void)
4426 {
4427 install_element(VIEW_NODE, &show_ip_ospf_mpls_te_router_cmd);
4428 install_element(VIEW_NODE, &show_ip_ospf_mpls_te_link_cmd);
4429 install_element(VIEW_NODE, &show_ip_ospf_mpls_te_db_cmd);
4430
4431 install_element(OSPF_NODE, &ospf_mpls_te_on_cmd);
4432 install_element(OSPF_NODE, &no_ospf_mpls_te_cmd);
4433 install_element(OSPF_NODE, &ospf_mpls_te_router_addr_cmd);
4434 install_element(OSPF_NODE, &ospf_mpls_te_inter_as_cmd);
4435 install_element(OSPF_NODE, &ospf_mpls_te_inter_as_area_cmd);
4436 install_element(OSPF_NODE, &no_ospf_mpls_te_inter_as_cmd);
4437 install_element(OSPF_NODE, &ospf_mpls_te_export_cmd);
4438 install_element(OSPF_NODE, &no_ospf_mpls_te_export_cmd);
4439
4440 return;
4441 }