]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_zebra.c
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / ospfd / ospf_zebra.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Zebra connect library for OSPFd
4 * Copyright (C) 1997, 98, 99, 2000 Kunihiro Ishiguro, Toshiaki Takada
5 */
6
7 #include <zebra.h>
8
9 #include "frrevent.h"
10 #include "command.h"
11 #include "network.h"
12 #include "prefix.h"
13 #include "routemap.h"
14 #include "table.h"
15 #include "stream.h"
16 #include "memory.h"
17 #include "zclient.h"
18 #include "filter.h"
19 #include "plist.h"
20 #include "log.h"
21 #include "route_opaque.h"
22 #include "lib/bfd.h"
23 #include "lib/lib_errors.h"
24 #include "nexthop.h"
25
26 #include "ospfd/ospfd.h"
27 #include "ospfd/ospf_interface.h"
28 #include "ospfd/ospf_ism.h"
29 #include "ospfd/ospf_asbr.h"
30 #include "ospfd/ospf_asbr.h"
31 #include "ospfd/ospf_abr.h"
32 #include "ospfd/ospf_lsa.h"
33 #include "ospfd/ospf_dump.h"
34 #include "ospfd/ospf_route.h"
35 #include "ospfd/ospf_lsdb.h"
36 #include "ospfd/ospf_neighbor.h"
37 #include "ospfd/ospf_nsm.h"
38 #include "ospfd/ospf_zebra.h"
39 #include "ospfd/ospf_te.h"
40 #include "ospfd/ospf_sr.h"
41 #include "ospfd/ospf_ldp_sync.h"
42
43 DEFINE_MTYPE_STATIC(OSPFD, OSPF_EXTERNAL, "OSPF External route table");
44 DEFINE_MTYPE_STATIC(OSPFD, OSPF_REDISTRIBUTE, "OSPF Redistriute");
45
46
47 /* Zebra structure to hold current status. */
48 struct zclient *zclient = NULL;
49 /* and for the Synchronous connection to the Label Manager */
50 static struct zclient *zclient_sync;
51
52 /* For registering threads. */
53 extern struct event_loop *master;
54
55 /* Router-id update message from zebra. */
56 static int ospf_router_id_update_zebra(ZAPI_CALLBACK_ARGS)
57 {
58 struct ospf *ospf = NULL;
59 struct prefix router_id;
60 zebra_router_id_update_read(zclient->ibuf, &router_id);
61
62 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
63 zlog_debug("Zebra rcvd: router id update %pFX vrf %s id %u",
64 &router_id, ospf_vrf_id_to_name(vrf_id), vrf_id);
65
66 ospf = ospf_lookup_by_vrf_id(vrf_id);
67
68 if (ospf != NULL) {
69 ospf->router_id_zebra = router_id.u.prefix4;
70 ospf_router_id_update(ospf);
71 } else {
72 if (IS_DEBUG_OSPF_EVENT)
73 zlog_debug(
74 "%s: ospf instance not found for vrf %s id %u router_id %pFX",
75 __func__, ospf_vrf_id_to_name(vrf_id), vrf_id,
76 &router_id);
77 }
78 return 0;
79 }
80
81 static int ospf_interface_address_add(ZAPI_CALLBACK_ARGS)
82 {
83 struct connected *c;
84 struct ospf *ospf = NULL;
85
86
87 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
88
89 if (c == NULL)
90 return 0;
91
92 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
93 zlog_debug("Zebra: interface %s address add %pFX vrf %s id %u",
94 c->ifp->name, c->address,
95 ospf_vrf_id_to_name(vrf_id), vrf_id);
96
97 ospf = ospf_lookup_by_vrf_id(vrf_id);
98 if (!ospf)
99 return 0;
100
101 ospf_if_update(ospf, c->ifp);
102
103 ospf_if_interface(c->ifp);
104
105 return 0;
106 }
107
108 static int ospf_interface_address_delete(ZAPI_CALLBACK_ARGS)
109 {
110 struct connected *c;
111 struct interface *ifp;
112 struct ospf_interface *oi;
113 struct route_node *rn;
114 struct prefix p;
115
116 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
117
118 if (c == NULL)
119 return 0;
120
121 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
122 zlog_debug("Zebra: interface %s address delete %pFX",
123 c->ifp->name, c->address);
124
125 ifp = c->ifp;
126 p = *c->address;
127 p.prefixlen = IPV4_MAX_BITLEN;
128
129 rn = route_node_lookup(IF_OIFS(ifp), &p);
130 if (!rn) {
131 connected_free(&c);
132 return 0;
133 }
134
135 assert(rn->info);
136 oi = rn->info;
137 route_unlock_node(rn);
138
139 /* Call interface hook functions to clean up */
140 ospf_if_free(oi);
141
142 ospf_if_interface(c->ifp);
143
144 connected_free(&c);
145
146 return 0;
147 }
148
149 static int ospf_interface_link_params(ZAPI_CALLBACK_ARGS)
150 {
151 struct interface *ifp;
152 bool changed = false;
153
154 ifp = zebra_interface_link_params_read(zclient->ibuf, vrf_id, &changed);
155
156 if (ifp == NULL || !changed)
157 return 0;
158
159 /* Update TE TLV */
160 ospf_mpls_te_update_if(ifp);
161
162 return 0;
163 }
164
165 /* VRF update for an interface. */
166 static int ospf_interface_vrf_update(ZAPI_CALLBACK_ARGS)
167 {
168 struct interface *ifp = NULL;
169 vrf_id_t new_vrf_id;
170
171 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
172 &new_vrf_id);
173 if (!ifp)
174 return 0;
175
176 if (IS_DEBUG_OSPF_EVENT)
177 zlog_debug(
178 "%s: Rx Interface %s VRF change vrf_id %u New vrf %s id %u",
179 __func__, ifp->name, vrf_id,
180 ospf_vrf_id_to_name(new_vrf_id), new_vrf_id);
181
182 /*if_update(ifp, ifp->name, strlen(ifp->name), new_vrf_id);*/
183 if_update_to_new_vrf(ifp, new_vrf_id);
184
185 return 0;
186 }
187
188 /* Nexthop, ifindex, distance and metric information. */
189 static void ospf_zebra_add_nexthop(struct ospf *ospf, struct ospf_path *path,
190 struct zapi_route *api)
191 {
192 struct zapi_nexthop *api_nh;
193 struct zapi_nexthop *api_nh_backup;
194
195 /* TI-LFA backup path label stack comes first, if present */
196 if (path->srni.backup_label_stack) {
197 api_nh_backup = &api->backup_nexthops[api->backup_nexthop_num];
198 api_nh_backup->vrf_id = ospf->vrf_id;
199
200 api_nh_backup->type = NEXTHOP_TYPE_IPV4;
201 api_nh_backup->gate.ipv4 = path->srni.backup_nexthop;
202
203 api_nh_backup->label_num =
204 path->srni.backup_label_stack->num_labels;
205 memcpy(api_nh_backup->labels,
206 path->srni.backup_label_stack->label,
207 sizeof(mpls_label_t) * api_nh_backup->label_num);
208
209 api->backup_nexthop_num++;
210 }
211
212 /* And here comes the primary nexthop */
213 api_nh = &api->nexthops[api->nexthop_num];
214 #ifdef HAVE_NETLINK
215 if (path->unnumbered
216 || (path->nexthop.s_addr != INADDR_ANY && path->ifindex != 0)) {
217 #else /* HAVE_NETLINK */
218 if (path->nexthop.s_addr != INADDR_ANY && path->ifindex != 0) {
219 #endif /* HAVE_NETLINK */
220 api_nh->gate.ipv4 = path->nexthop;
221 api_nh->ifindex = path->ifindex;
222 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
223 } else if (path->nexthop.s_addr != INADDR_ANY) {
224 api_nh->gate.ipv4 = path->nexthop;
225 api_nh->type = NEXTHOP_TYPE_IPV4;
226 } else {
227 api_nh->ifindex = path->ifindex;
228 api_nh->type = NEXTHOP_TYPE_IFINDEX;
229 }
230 api_nh->vrf_id = ospf->vrf_id;
231
232 /* Set TI-LFA backup nexthop info if present */
233 if (path->srni.backup_label_stack) {
234 SET_FLAG(api->message, ZAPI_MESSAGE_BACKUP_NEXTHOPS);
235 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP);
236
237 /* Just care about a single TI-LFA backup path for now */
238 api_nh->backup_num = 1;
239 api_nh->backup_idx[0] = api->backup_nexthop_num - 1;
240 }
241
242 api->nexthop_num++;
243 }
244
245 static void ospf_zebra_append_opaque_attr(struct ospf_route *or,
246 struct zapi_route *api)
247 {
248 struct ospf_zebra_opaque ospf_opaque = {};
249
250 /* OSPF path type */
251 snprintf(ospf_opaque.path_type, sizeof(ospf_opaque.path_type), "%s",
252 ospf_path_type_name(or->path_type));
253
254 switch (or->path_type) {
255 case OSPF_PATH_INTRA_AREA:
256 case OSPF_PATH_INTER_AREA:
257 /* OSPF area ID */
258 (void)inet_ntop(AF_INET, &or->u.std.area_id,
259 ospf_opaque.area_id,
260 sizeof(ospf_opaque.area_id));
261 break;
262 case OSPF_PATH_TYPE1_EXTERNAL:
263 case OSPF_PATH_TYPE2_EXTERNAL:
264 /* OSPF route tag */
265 snprintf(ospf_opaque.tag, sizeof(ospf_opaque.tag), "%u",
266 or->u.ext.tag);
267 break;
268 default:
269 break;
270 }
271
272 SET_FLAG(api->message, ZAPI_MESSAGE_OPAQUE);
273 api->opaque.length = sizeof(struct ospf_zebra_opaque);
274 memcpy(api->opaque.data, &ospf_opaque, api->opaque.length);
275 }
276
277 void ospf_zebra_add(struct ospf *ospf, struct prefix_ipv4 *p,
278 struct ospf_route * or)
279 {
280 struct zapi_route api;
281 uint8_t distance;
282 struct ospf_path *path;
283 struct listnode *node;
284
285 if (ospf->gr_info.restart_in_progress) {
286 if (IS_DEBUG_OSPF_GR)
287 zlog_debug(
288 "Zebra: Graceful Restart in progress -- not installing %pFX",
289 p);
290 return;
291 }
292
293 memset(&api, 0, sizeof(api));
294 api.vrf_id = ospf->vrf_id;
295 api.type = ZEBRA_ROUTE_OSPF;
296 api.instance = ospf->instance;
297 api.safi = SAFI_UNICAST;
298
299 memcpy(&api.prefix, p, sizeof(*p));
300 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
301
302 /* Metric value. */
303 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
304 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
305 api.metric = or->cost + or->u.ext.type2_cost;
306 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
307 api.metric = or->u.ext.type2_cost;
308 else
309 api.metric = or->cost;
310
311 /* Check if path type is ASE */
312 if (((or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
313 || (or->path_type == OSPF_PATH_TYPE2_EXTERNAL))
314 && (or->u.ext.tag > 0) && (or->u.ext.tag <= ROUTE_TAG_MAX)) {
315 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
316 api.tag = or->u.ext.tag;
317 }
318
319 /* Distance value. */
320 distance = ospf_distance_apply(ospf, p, or);
321 if (distance) {
322 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
323 api.distance = distance;
324 }
325
326 for (ALL_LIST_ELEMENTS_RO(or->paths, node, path)) {
327 if (api.nexthop_num >= ospf->max_multipath)
328 break;
329
330 ospf_zebra_add_nexthop(ospf, path, &api);
331
332 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
333 struct interface *ifp;
334
335 ifp = if_lookup_by_index(path->ifindex, ospf->vrf_id);
336
337 zlog_debug(
338 "Zebra: Route add %pFX nexthop %pI4, ifindex=%d %s",
339 p, &path->nexthop, path->ifindex,
340 ifp ? ifp->name : " ");
341 }
342 }
343
344 if (CHECK_FLAG(ospf->config, OSPF_SEND_EXTRA_DATA_TO_ZEBRA))
345 ospf_zebra_append_opaque_attr(or, &api);
346
347 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
348 }
349
350 void ospf_zebra_delete(struct ospf *ospf, struct prefix_ipv4 *p,
351 struct ospf_route * or)
352 {
353 struct zapi_route api;
354
355 if (ospf->gr_info.restart_in_progress) {
356 if (IS_DEBUG_OSPF_GR)
357 zlog_debug(
358 "Zebra: Graceful Restart in progress -- not uninstalling %pFX",
359 p);
360 return;
361 }
362
363 memset(&api, 0, sizeof(api));
364 api.vrf_id = ospf->vrf_id;
365 api.type = ZEBRA_ROUTE_OSPF;
366 api.instance = ospf->instance;
367 api.safi = SAFI_UNICAST;
368 memcpy(&api.prefix, p, sizeof(*p));
369
370 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
371 zlog_debug("Zebra: Route delete %pFX", p);
372
373 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
374 }
375
376 void ospf_zebra_add_discard(struct ospf *ospf, struct prefix_ipv4 *p)
377 {
378 struct zapi_route api;
379
380 if (ospf->gr_info.restart_in_progress) {
381 if (IS_DEBUG_OSPF_GR)
382 zlog_debug(
383 "Zebra: Graceful Restart in progress -- not installing %pFX",
384 p);
385 return;
386 }
387
388 memset(&api, 0, sizeof(api));
389 api.vrf_id = ospf->vrf_id;
390 api.type = ZEBRA_ROUTE_OSPF;
391 api.instance = ospf->instance;
392 api.safi = SAFI_UNICAST;
393 memcpy(&api.prefix, p, sizeof(*p));
394 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
395
396 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
397
398 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
399 zlog_debug("Zebra: Route add discard %pFX", p);
400 }
401
402 void ospf_zebra_delete_discard(struct ospf *ospf, struct prefix_ipv4 *p)
403 {
404 struct zapi_route api;
405
406 if (ospf->gr_info.restart_in_progress) {
407 if (IS_DEBUG_OSPF_GR)
408 zlog_debug(
409 "Zebra: Graceful Restart in progress -- not uninstalling %pFX",
410 p);
411 return;
412 }
413
414 memset(&api, 0, sizeof(api));
415 api.vrf_id = ospf->vrf_id;
416 api.type = ZEBRA_ROUTE_OSPF;
417 api.instance = ospf->instance;
418 api.safi = SAFI_UNICAST;
419 memcpy(&api.prefix, p, sizeof(*p));
420 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
421
422 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
423
424 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
425 zlog_debug("Zebra: Route delete discard %pFX", p);
426 }
427
428 struct ospf_external *ospf_external_lookup(struct ospf *ospf, uint8_t type,
429 unsigned short instance)
430 {
431 struct list *ext_list;
432 struct listnode *node;
433 struct ospf_external *ext;
434
435 ext_list = ospf->external[type];
436 if (!ext_list)
437 return (NULL);
438
439 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext))
440 if (ext->instance == instance)
441 return ext;
442
443 return NULL;
444 }
445
446 struct ospf_external *ospf_external_add(struct ospf *ospf, uint8_t type,
447 unsigned short instance)
448 {
449 struct list *ext_list;
450 struct ospf_external *ext;
451
452 ext = ospf_external_lookup(ospf, type, instance);
453 if (ext)
454 return ext;
455
456 if (!ospf->external[type])
457 ospf->external[type] = list_new();
458
459 ext_list = ospf->external[type];
460 ext = XCALLOC(MTYPE_OSPF_EXTERNAL, sizeof(struct ospf_external));
461 ext->instance = instance;
462 EXTERNAL_INFO(ext) = route_table_init();
463
464 listnode_add(ext_list, ext);
465
466 return ext;
467 }
468
469 /*
470 * Walk all the ei received from zebra for a route type and apply
471 * default route-map.
472 */
473 bool ospf_external_default_routemap_apply_walk(struct ospf *ospf,
474 struct list *ext_list,
475 struct external_info *default_ei)
476 {
477 struct listnode *node;
478 struct ospf_external *ext;
479 struct route_node *rn;
480 struct external_info *ei = NULL;
481 int ret = 0;
482
483 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
484 if (!ext->external_info)
485 continue;
486
487 for (rn = route_top(ext->external_info); rn;
488 rn = route_next(rn)) {
489 ei = rn->info;
490 if (!ei)
491 continue;
492 ret = ospf_external_info_apply_default_routemap(
493 ospf, ei, default_ei);
494 if (ret)
495 break;
496 }
497 }
498
499 if (ret && ei) {
500 if (IS_DEBUG_OSPF_DEFAULT_INFO)
501 zlog_debug("Default originate routemap permit ei: %pI4",
502 &ei->p.prefix);
503 return true;
504 }
505
506 return false;
507 }
508
509 /*
510 * Function to originate or flush default after applying
511 * route-map on all ei.
512 */
513 static void ospf_external_lsa_default_routemap_timer(struct event *thread)
514 {
515 struct list *ext_list;
516 struct ospf *ospf = EVENT_ARG(thread);
517 struct prefix_ipv4 p;
518 int type;
519 int ret = 0;
520 struct ospf_lsa *lsa;
521 struct external_info *default_ei;
522
523 p.family = AF_INET;
524 p.prefixlen = 0;
525 p.prefix.s_addr = INADDR_ANY;
526
527 /* Get the default extenal info. */
528 default_ei = ospf_external_info_lookup(ospf, DEFAULT_ROUTE,
529 ospf->instance, &p);
530 if (!default_ei) {
531 /* Nothing to be done here. */
532 if (IS_DEBUG_OSPF_DEFAULT_INFO)
533 zlog_debug("Default originate info not present");
534 return;
535 }
536
537 /* For all the ei apply route-map */
538 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
539 ext_list = ospf->external[type];
540 if (!ext_list || type == ZEBRA_ROUTE_OSPF)
541 continue;
542
543 ret = ospf_external_default_routemap_apply_walk(ospf, ext_list,
544 default_ei);
545 if (ret)
546 break;
547 }
548
549 /* Get the default LSA. */
550 lsa = ospf_external_info_find_lsa(ospf, &p);
551
552 /* If permit then originate default. */
553 if (ret && !lsa)
554 ospf_external_lsa_originate(ospf, default_ei);
555 else if (ret && lsa && IS_LSA_MAXAGE(lsa))
556 ospf_external_lsa_refresh(ospf, lsa, default_ei, true, false);
557 else if (!ret && lsa)
558 ospf_external_lsa_flush(ospf, DEFAULT_ROUTE, &default_ei->p, 0);
559 }
560
561
562 void ospf_external_del(struct ospf *ospf, uint8_t type, unsigned short instance)
563 {
564 struct ospf_external *ext;
565
566 ext = ospf_external_lookup(ospf, type, instance);
567
568 if (ext) {
569 if (EXTERNAL_INFO(ext))
570 route_table_finish(EXTERNAL_INFO(ext));
571
572 listnode_delete(ospf->external[type], ext);
573
574 if (!ospf->external[type]->count)
575 list_delete(&ospf->external[type]);
576
577 XFREE(MTYPE_OSPF_EXTERNAL, ext);
578 }
579
580 /*
581 * Check if default needs to be flushed too.
582 */
583 event_add_event(master, ospf_external_lsa_default_routemap_timer, ospf,
584 0, &ospf->t_default_routemap_timer);
585 }
586
587 /* Update NHLFE for Prefix SID */
588 void ospf_zebra_update_prefix_sid(const struct sr_prefix *srp)
589 {
590 struct zapi_labels zl;
591 struct zapi_nexthop *znh;
592 struct zapi_nexthop *znh_backup;
593 struct listnode *node;
594 struct ospf_path *path;
595
596 /* Prepare message. */
597 memset(&zl, 0, sizeof(zl));
598 zl.type = ZEBRA_LSP_OSPF_SR;
599 zl.local_label = srp->label_in;
600
601 switch (srp->type) {
602 case LOCAL_SID:
603 /* Set Label for local Prefix */
604 znh = &zl.nexthops[zl.nexthop_num++];
605 znh->type = NEXTHOP_TYPE_IFINDEX;
606 znh->ifindex = srp->nhlfe.ifindex;
607 znh->label_num = 1;
608 znh->labels[0] = srp->nhlfe.label_out;
609
610 osr_debug("SR (%s): Configure Prefix %pFX with labels %u/%u",
611 __func__, (struct prefix *)&srp->prefv4,
612 srp->label_in, srp->nhlfe.label_out);
613
614 break;
615
616 case PREF_SID:
617 /* Update route in the RIB too. */
618 SET_FLAG(zl.message, ZAPI_LABELS_FTN);
619 zl.route.prefix.u.prefix4 = srp->prefv4.prefix;
620 zl.route.prefix.prefixlen = srp->prefv4.prefixlen;
621 zl.route.prefix.family = srp->prefv4.family;
622 zl.route.type = ZEBRA_ROUTE_OSPF;
623 zl.route.instance = 0;
624
625 /* Check that SRP contains at least one valid path */
626 if (srp->route == NULL) {
627 return;
628 }
629
630 osr_debug("SR (%s): Configure Prefix %pFX with",
631 __func__, (struct prefix *)&srp->prefv4);
632
633 for (ALL_LIST_ELEMENTS_RO(srp->route->paths, node, path)) {
634 if (path->srni.label_out == MPLS_INVALID_LABEL)
635 continue;
636
637 if (zl.nexthop_num >= MULTIPATH_NUM)
638 break;
639
640 /*
641 * TI-LFA backup path label stack comes first, if
642 * present.
643 */
644 if (path->srni.backup_label_stack) {
645 znh_backup = &zl.backup_nexthops
646 [zl.backup_nexthop_num++];
647 znh_backup->type = NEXTHOP_TYPE_IPV4;
648 znh_backup->gate.ipv4 =
649 path->srni.backup_nexthop;
650
651 memcpy(znh_backup->labels,
652 path->srni.backup_label_stack->label,
653 sizeof(mpls_label_t)
654 * path->srni.backup_label_stack
655 ->num_labels);
656
657 znh_backup->label_num =
658 path->srni.backup_label_stack
659 ->num_labels;
660 if (path->srni.label_out
661 != MPLS_LABEL_IPV4_EXPLICIT_NULL
662 && path->srni.label_out
663 != MPLS_LABEL_IMPLICIT_NULL)
664 znh_backup->labels
665 [znh_backup->label_num++] =
666 path->srni.label_out;
667 }
668
669 znh = &zl.nexthops[zl.nexthop_num++];
670 znh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
671 znh->gate.ipv4 = path->nexthop;
672 znh->ifindex = path->ifindex;
673 znh->label_num = 1;
674 znh->labels[0] = path->srni.label_out;
675
676 osr_debug(" |- labels %u/%u", srp->label_in,
677 path->srni.label_out);
678
679 /* Set TI-LFA backup nexthop info if present */
680 if (path->srni.backup_label_stack) {
681 SET_FLAG(zl.message, ZAPI_LABELS_HAS_BACKUPS);
682 SET_FLAG(znh->flags,
683 ZAPI_NEXTHOP_FLAG_HAS_BACKUP);
684
685 /* Just care about a single TI-LFA backup path
686 * for now */
687 znh->backup_num = 1;
688 znh->backup_idx[0] = zl.backup_nexthop_num - 1;
689 }
690 }
691 break;
692 case ADJ_SID:
693 case LAN_ADJ_SID:
694 return;
695 }
696
697 /* Finally, send message to zebra. */
698 (void)zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_REPLACE, &zl);
699 }
700
701 /* Remove NHLFE for Prefix-SID */
702 void ospf_zebra_delete_prefix_sid(const struct sr_prefix *srp)
703 {
704 struct zapi_labels zl;
705
706 osr_debug("SR (%s): Delete Labels %u for Prefix %pFX", __func__,
707 srp->label_in, (struct prefix *)&srp->prefv4);
708
709 /* Prepare message. */
710 memset(&zl, 0, sizeof(zl));
711 zl.type = ZEBRA_LSP_OSPF_SR;
712 zl.local_label = srp->label_in;
713
714 if (srp->type == PREF_SID) {
715 /* Update route in the RIB too */
716 SET_FLAG(zl.message, ZAPI_LABELS_FTN);
717 zl.route.prefix.u.prefix4 = srp->prefv4.prefix;
718 zl.route.prefix.prefixlen = srp->prefv4.prefixlen;
719 zl.route.prefix.family = srp->prefv4.family;
720 zl.route.type = ZEBRA_ROUTE_OSPF;
721 zl.route.instance = 0;
722 }
723
724 /* Send message to zebra. */
725 (void)zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_DELETE, &zl);
726 }
727
728 /* Send MPLS Label entry to Zebra for installation or deletion */
729 void ospf_zebra_send_adjacency_sid(int cmd, struct sr_nhlfe nhlfe)
730 {
731 struct zapi_labels zl;
732 struct zapi_nexthop *znh;
733
734 osr_debug("SR (%s): %s Labels %u/%u for Adjacency via %u", __func__,
735 cmd == ZEBRA_MPLS_LABELS_ADD ? "Add" : "Delete",
736 nhlfe.label_in, nhlfe.label_out, nhlfe.ifindex);
737
738 memset(&zl, 0, sizeof(zl));
739 zl.type = ZEBRA_LSP_OSPF_SR;
740 zl.local_label = nhlfe.label_in;
741 zl.nexthop_num = 1;
742 znh = &zl.nexthops[0];
743 znh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
744 znh->gate.ipv4 = nhlfe.nexthop;
745 znh->ifindex = nhlfe.ifindex;
746 znh->label_num = 1;
747 znh->labels[0] = nhlfe.label_out;
748
749 (void)zebra_send_mpls_labels(zclient, cmd, &zl);
750 }
751
752 struct ospf_redist *ospf_redist_lookup(struct ospf *ospf, uint8_t type,
753 unsigned short instance)
754 {
755 struct list *red_list;
756 struct listnode *node;
757 struct ospf_redist *red;
758
759 red_list = ospf->redist[type];
760 if (!red_list)
761 return (NULL);
762
763 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
764 if (red->instance == instance)
765 return red;
766
767 return NULL;
768 }
769
770 struct ospf_redist *ospf_redist_add(struct ospf *ospf, uint8_t type,
771 unsigned short instance)
772 {
773 struct list *red_list;
774 struct ospf_redist *red;
775
776 red = ospf_redist_lookup(ospf, type, instance);
777 if (red)
778 return red;
779
780 if (!ospf->redist[type])
781 ospf->redist[type] = list_new();
782
783 red_list = ospf->redist[type];
784 red = XCALLOC(MTYPE_OSPF_REDISTRIBUTE, sizeof(struct ospf_redist));
785 red->instance = instance;
786 red->dmetric.type = -1;
787 red->dmetric.value = -1;
788 ROUTEMAP_NAME(red) = NULL;
789 ROUTEMAP(red) = NULL;
790
791 listnode_add(red_list, red);
792
793 return red;
794 }
795
796 void ospf_redist_del(struct ospf *ospf, uint8_t type, unsigned short instance)
797 {
798 struct ospf_redist *red;
799
800 red = ospf_redist_lookup(ospf, type, instance);
801
802 if (red) {
803 listnode_delete(ospf->redist[type], red);
804 if (!ospf->redist[type]->count) {
805 list_delete(&ospf->redist[type]);
806 }
807 ospf_routemap_unset(red);
808 XFREE(MTYPE_OSPF_REDISTRIBUTE, red);
809 }
810 }
811
812
813 int ospf_is_type_redistributed(struct ospf *ospf, int type,
814 unsigned short instance)
815 {
816 return (DEFAULT_ROUTE_TYPE(type)
817 ? vrf_bitmap_check(zclient->default_information[AFI_IP],
818 ospf->vrf_id)
819 : ((instance
820 && redist_check_instance(
821 &zclient->mi_redist[AFI_IP][type],
822 instance))
823 || (!instance
824 && vrf_bitmap_check(
825 zclient->redist[AFI_IP][type],
826 ospf->vrf_id))));
827 }
828
829 int ospf_redistribute_update(struct ospf *ospf, struct ospf_redist *red,
830 int type, unsigned short instance, int mtype,
831 int mvalue)
832 {
833 int force = 0;
834
835 if (mtype != red->dmetric.type) {
836 red->dmetric.type = mtype;
837 force = LSA_REFRESH_FORCE;
838 }
839 if (mvalue != red->dmetric.value) {
840 red->dmetric.value = mvalue;
841 force = LSA_REFRESH_FORCE;
842 }
843
844 ospf_external_lsa_refresh_type(ospf, type, instance, force);
845
846 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
847 zlog_debug(
848 "Redistribute[%s][%d]: Refresh Type[%d], Metric[%d]",
849 ospf_redist_string(type), instance,
850 metric_type(ospf, type, instance),
851 metric_value(ospf, type, instance));
852
853 return CMD_SUCCESS;
854 }
855
856 int ospf_redistribute_set(struct ospf *ospf, struct ospf_redist *red, int type,
857 unsigned short instance, int mtype, int mvalue)
858 {
859 red->dmetric.type = mtype;
860 red->dmetric.value = mvalue;
861
862 ospf_external_add(ospf, type, instance);
863
864 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type,
865 instance, ospf->vrf_id);
866
867 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
868 zlog_debug(
869 "Redistribute[%s][%d] vrf id %u: Start Type[%d], Metric[%d]",
870 ospf_redist_string(type), instance, ospf->vrf_id,
871 metric_type(ospf, type, instance),
872 metric_value(ospf, type, instance));
873
874 ospf_asbr_status_update(ospf, ++ospf->redistribute);
875
876 return CMD_SUCCESS;
877 }
878
879 int ospf_redistribute_unset(struct ospf *ospf, int type,
880 unsigned short instance)
881 {
882 if (type == zclient->redist_default && instance == zclient->instance)
883 return CMD_SUCCESS;
884
885 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, AFI_IP, type,
886 instance, ospf->vrf_id);
887
888 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
889 zlog_debug("Redistribute[%s][%d] vrf id %u: Stop",
890 ospf_redist_string(type), instance, ospf->vrf_id);
891
892 /* Remove the routes from OSPF table. */
893 ospf_redistribute_withdraw(ospf, type, instance);
894
895 ospf_external_del(ospf, type, instance);
896
897 ospf_asbr_status_update(ospf, --ospf->redistribute);
898
899 return CMD_SUCCESS;
900 }
901
902 int ospf_redistribute_default_set(struct ospf *ospf, int originate, int mtype,
903 int mvalue)
904 {
905 struct prefix_ipv4 p;
906 struct in_addr nexthop;
907 int cur_originate = ospf->default_originate;
908 const char *type_str = NULL;
909
910 nexthop.s_addr = INADDR_ANY;
911 p.family = AF_INET;
912 p.prefix.s_addr = INADDR_ANY;
913 p.prefixlen = 0;
914
915 ospf->default_originate = originate;
916
917 if (cur_originate == originate) {
918 /* Refresh the lsa since metric might different */
919 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
920 zlog_debug(
921 "Redistribute[%s]: Refresh Type[%d], Metric[%d]",
922 ospf_redist_string(DEFAULT_ROUTE),
923 metric_type(ospf, DEFAULT_ROUTE, 0),
924 metric_value(ospf, DEFAULT_ROUTE, 0));
925
926 ospf_external_lsa_refresh_default(ospf);
927 return CMD_SUCCESS;
928 }
929
930 switch (cur_originate) {
931 case DEFAULT_ORIGINATE_NONE:
932 break;
933 case DEFAULT_ORIGINATE_ZEBRA:
934 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE,
935 zclient, AFI_IP, ospf->vrf_id);
936 ospf->redistribute--;
937 break;
938 case DEFAULT_ORIGINATE_ALWAYS:
939 ospf_external_info_delete(ospf, DEFAULT_ROUTE, 0, p);
940 ospf_external_del(ospf, DEFAULT_ROUTE, 0);
941 ospf->redistribute--;
942 break;
943 }
944
945 switch (originate) {
946 case DEFAULT_ORIGINATE_NONE:
947 type_str = "none";
948 break;
949 case DEFAULT_ORIGINATE_ZEBRA:
950 type_str = "normal";
951 ospf->redistribute++;
952 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD,
953 zclient, AFI_IP, ospf->vrf_id);
954 break;
955 case DEFAULT_ORIGINATE_ALWAYS:
956 type_str = "always";
957 ospf->redistribute++;
958 ospf_external_add(ospf, DEFAULT_ROUTE, 0);
959 ospf_external_info_add(ospf, DEFAULT_ROUTE, 0, p, 0, nexthop, 0,
960 DEFAULT_DEFAULT_METRIC);
961 break;
962 }
963
964 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
965 zlog_debug("Redistribute[DEFAULT]: %s Type[%d], Metric[%d]",
966 type_str,
967 metric_type(ospf, DEFAULT_ROUTE, 0),
968 metric_value(ospf, DEFAULT_ROUTE, 0));
969
970 ospf_external_lsa_refresh_default(ospf);
971 ospf_asbr_status_update(ospf, ospf->redistribute);
972 return CMD_SUCCESS;
973 }
974
975 static int ospf_external_lsa_originate_check(struct ospf *ospf,
976 struct external_info *ei)
977 {
978 /* If prefix is multicast, then do not originate LSA. */
979 if (IN_MULTICAST(htonl(ei->p.prefix.s_addr))) {
980 zlog_info(
981 "LSA[Type5:%pI4]: Not originate AS-external-LSA, Prefix belongs multicast",
982 &ei->p.prefix);
983 return 0;
984 }
985
986 /* Take care of default-originate. */
987 if (is_default_prefix4(&ei->p))
988 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE) {
989 zlog_info(
990 "LSA[Type5:0.0.0.0]: Not originate AS-external-LSA for default");
991 return 0;
992 }
993
994 return 1;
995 }
996
997 /* If connected prefix is OSPF enable interface, then do not announce. */
998 int ospf_distribute_check_connected(struct ospf *ospf, struct external_info *ei)
999 {
1000 struct listnode *node;
1001 struct ospf_interface *oi;
1002
1003
1004 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
1005 if (prefix_match(oi->address, (struct prefix *)&ei->p))
1006 return 0;
1007 return 1;
1008 }
1009
1010
1011 /* Apply default route-map on ei received. */
1012 int ospf_external_info_apply_default_routemap(struct ospf *ospf,
1013 struct external_info *ei,
1014 struct external_info *default_ei)
1015 {
1016 struct ospf_redist *red;
1017 int type = default_ei->type;
1018 struct prefix_ipv4 *p = &ei->p;
1019 struct route_map_set_values save_values;
1020
1021
1022 if (!ospf_external_lsa_originate_check(ospf, default_ei))
1023 return 0;
1024
1025 save_values = default_ei->route_map_set;
1026 ospf_reset_route_map_set_values(&default_ei->route_map_set);
1027
1028 /* apply route-map if needed */
1029 red = ospf_redist_lookup(ospf, type, ospf->instance);
1030 if (red && ROUTEMAP_NAME(red)) {
1031 route_map_result_t ret;
1032
1033 ret = route_map_apply(ROUTEMAP(red), (struct prefix *)p, ei);
1034
1035 if (ret == RMAP_DENYMATCH) {
1036 ei->route_map_set = save_values;
1037 return 0;
1038 }
1039 }
1040
1041 return 1;
1042 }
1043
1044
1045 /*
1046 * Default originated is based on route-map condition then
1047 * apply route-map on received external info. Originate or
1048 * flush based on route-map condition.
1049 */
1050 static bool ospf_external_lsa_default_routemap_apply(struct ospf *ospf,
1051 struct external_info *ei,
1052 int cmd)
1053 {
1054 struct external_info *default_ei;
1055 struct prefix_ipv4 p;
1056 struct ospf_lsa *lsa;
1057 int ret;
1058
1059 p.family = AF_INET;
1060 p.prefixlen = 0;
1061 p.prefix.s_addr = INADDR_ANY;
1062
1063
1064 /* Get the default extenal info. */
1065 default_ei = ospf_external_info_lookup(ospf, DEFAULT_ROUTE,
1066 ospf->instance, &p);
1067 if (!default_ei) {
1068 /* Nothing to be done here. */
1069 return false;
1070 }
1071
1072 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1073 zlog_debug("Apply default originate routemap on ei: %pI4 cmd: %d",
1074 &ei->p.prefix, cmd);
1075
1076 ret = ospf_external_info_apply_default_routemap(ospf, ei, default_ei);
1077
1078 /* If deny then nothing to be done both in add and del case. */
1079 if (!ret) {
1080 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1081 zlog_debug("Default originte routemap deny for ei: %pI4",
1082 &ei->p.prefix);
1083 return false;
1084 }
1085
1086 /* Get the default LSA. */
1087 lsa = ospf_external_info_find_lsa(ospf, &p);
1088
1089 /* If this is add route and permit then ooriginate default. */
1090 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
1091 /* If permit and default already advertise then return. */
1092 if (lsa && !IS_LSA_MAXAGE(lsa)) {
1093 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1094 zlog_debug("Default lsa already originated");
1095 return true;
1096 }
1097
1098 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1099 zlog_debug("Originating/Refreshing default lsa");
1100
1101 if (lsa && IS_LSA_MAXAGE(lsa))
1102 /* Refresh lsa.*/
1103 ospf_external_lsa_refresh(ospf, lsa, default_ei, true,
1104 false);
1105 else
1106 /* If permit and default not advertised then advertise.
1107 */
1108 ospf_external_lsa_originate(ospf, default_ei);
1109
1110 } else if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) {
1111 /* If deny and lsa is not originated then nothing to be done.*/
1112 if (!lsa) {
1113 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1114 zlog_debug(
1115 "Default lsa not originated, not flushing");
1116 return true;
1117 }
1118
1119 if (IS_DEBUG_OSPF_DEFAULT_INFO)
1120 zlog_debug(
1121 "Running default route-map again as ei: %pI4 deleted",
1122 &ei->p.prefix);
1123 /*
1124 * if this route delete was permitted then we need to check
1125 * there are any other external info which can still trigger
1126 * default route origination else flush it.
1127 */
1128 event_add_event(master,
1129 ospf_external_lsa_default_routemap_timer, ospf,
1130 0, &ospf->t_default_routemap_timer);
1131 }
1132
1133 return true;
1134 }
1135
1136 /* return 1 if external LSA must be originated, 0 otherwise */
1137 int ospf_redistribute_check(struct ospf *ospf, struct external_info *ei,
1138 int *changed)
1139 {
1140 struct route_map_set_values save_values;
1141 struct prefix_ipv4 *p = &ei->p;
1142 struct ospf_redist *red;
1143 uint8_t type = is_default_prefix4(&ei->p) ? DEFAULT_ROUTE : ei->type;
1144 unsigned short instance = is_default_prefix4(&ei->p) ? 0 : ei->instance;
1145 route_tag_t saved_tag = 0;
1146
1147 /* Default is handled differently. */
1148 if (type == DEFAULT_ROUTE)
1149 return 1;
1150
1151 if (changed)
1152 *changed = 0;
1153
1154 if (!ospf_external_lsa_originate_check(ospf, ei))
1155 return 0;
1156
1157 /* Take care connected route. */
1158 if (type == ZEBRA_ROUTE_CONNECT
1159 && !ospf_distribute_check_connected(ospf, ei))
1160 return 0;
1161
1162 if (!DEFAULT_ROUTE_TYPE(type) && DISTRIBUTE_NAME(ospf, type))
1163 /* distirbute-list exists, but access-list may not? */
1164 if (DISTRIBUTE_LIST(ospf, type))
1165 if (access_list_apply(DISTRIBUTE_LIST(ospf, type), p)
1166 == FILTER_DENY) {
1167 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
1168 zlog_debug(
1169 "Redistribute[%s]: %pFX filtered by distribute-list.",
1170 ospf_redist_string(type), p);
1171 return 0;
1172 }
1173
1174 save_values = ei->route_map_set;
1175 ospf_reset_route_map_set_values(&ei->route_map_set);
1176
1177 saved_tag = ei->tag;
1178 /* Resetting with original route tag */
1179 ei->tag = ei->orig_tag;
1180
1181 /* apply route-map if needed */
1182 red = ospf_redist_lookup(ospf, type, instance);
1183 if (red && ROUTEMAP_NAME(red)) {
1184 route_map_result_t ret;
1185
1186 ret = route_map_apply(ROUTEMAP(red), (struct prefix *)p, ei);
1187
1188 if (ret == RMAP_DENYMATCH) {
1189 ei->route_map_set = save_values;
1190 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
1191 zlog_debug(
1192 "Redistribute[%s]: %pFX filtered by route-map.",
1193 ospf_redist_string(type), p);
1194 return 0;
1195 }
1196
1197 /* check if 'route-map set' changed something */
1198 if (changed) {
1199 *changed = !ospf_route_map_set_compare(
1200 &ei->route_map_set, &save_values);
1201
1202 /* check if tag is modified */
1203 *changed |= (saved_tag != ei->tag);
1204 }
1205 }
1206
1207 return 1;
1208 }
1209
1210 /* OSPF route-map set for redistribution */
1211 void ospf_routemap_set(struct ospf_redist *red, const char *name)
1212 {
1213 if (ROUTEMAP_NAME(red)) {
1214 route_map_counter_decrement(ROUTEMAP(red));
1215 free(ROUTEMAP_NAME(red));
1216 }
1217
1218 ROUTEMAP_NAME(red) = strdup(name);
1219 ROUTEMAP(red) = route_map_lookup_by_name(name);
1220 route_map_counter_increment(ROUTEMAP(red));
1221 }
1222
1223 void ospf_routemap_unset(struct ospf_redist *red)
1224 {
1225 if (ROUTEMAP_NAME(red)) {
1226 route_map_counter_decrement(ROUTEMAP(red));
1227 free(ROUTEMAP_NAME(red));
1228 }
1229
1230 ROUTEMAP_NAME(red) = NULL;
1231 ROUTEMAP(red) = NULL;
1232 }
1233
1234 static int ospf_zebra_gr_update(struct ospf *ospf, int command,
1235 uint32_t stale_time)
1236 {
1237 struct zapi_cap api;
1238
1239 if (!zclient || zclient->sock < 0 || !ospf)
1240 return 1;
1241
1242 memset(&api, 0, sizeof(api));
1243 api.cap = command;
1244 api.stale_removal_time = stale_time;
1245 api.vrf_id = ospf->vrf_id;
1246
1247 (void)zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient,
1248 &api);
1249
1250 return 0;
1251 }
1252
1253 int ospf_zebra_gr_enable(struct ospf *ospf, uint32_t stale_time)
1254 {
1255 if (IS_DEBUG_OSPF_GR)
1256 zlog_debug("Zebra enable GR [stale time %u]", stale_time);
1257
1258 return ospf_zebra_gr_update(ospf, ZEBRA_CLIENT_GR_CAPABILITIES,
1259 stale_time);
1260 }
1261
1262 int ospf_zebra_gr_disable(struct ospf *ospf)
1263 {
1264 if (IS_DEBUG_OSPF_GR)
1265 zlog_debug("Zebra disable GR");
1266
1267 return ospf_zebra_gr_update(ospf, ZEBRA_CLIENT_GR_DISABLE, 0);
1268 }
1269
1270 /* Zebra route add and delete treatment. */
1271 static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS)
1272 {
1273 struct zapi_route api;
1274 struct prefix_ipv4 p;
1275 struct prefix pgen;
1276 unsigned long ifindex;
1277 struct in_addr nexthop;
1278 struct external_info *ei;
1279 struct ospf *ospf;
1280 int i;
1281 uint8_t rt_type;
1282
1283 ospf = ospf_lookup_by_vrf_id(vrf_id);
1284 if (ospf == NULL)
1285 return 0;
1286
1287 if (zapi_route_decode(zclient->ibuf, &api) < 0)
1288 return -1;
1289
1290 ifindex = api.nexthops[0].ifindex;
1291 nexthop = api.nexthops[0].gate.ipv4;
1292 rt_type = api.type;
1293
1294 memcpy(&p, &api.prefix, sizeof(p));
1295 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
1296 return 0;
1297
1298 pgen.family = p.family;
1299 pgen.prefixlen = p.prefixlen;
1300 pgen.u.prefix4 = p.prefix;
1301
1302 /* Re-destributed route is default route.
1303 * Here, route type is used as 'ZEBRA_ROUTE_KERNEL' for
1304 * updating ex-info. But in resetting (no default-info
1305 * originate)ZEBRA_ROUTE_MAX is used to delete the ex-info.
1306 * Resolved this inconsistency by maintaining same route type.
1307 */
1308 if ((is_default_prefix(&pgen)) && (api.type != ZEBRA_ROUTE_OSPF))
1309 rt_type = DEFAULT_ROUTE;
1310
1311 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
1312 zlog_debug(
1313 "%s: cmd %s from client %s: vrf_id %d, p %pFX, metric %d",
1314 __func__, zserv_command_string(cmd),
1315 zebra_route_string(api.type), vrf_id, &api.prefix,
1316 api.metric);
1317
1318 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
1319 /* XXX|HACK|TODO|FIXME:
1320 * Maybe we should ignore reject/blackhole routes? Testing
1321 * shows that there is no problems though and this is only way
1322 * to "summarize" routes in ASBR at the moment. Maybe we need
1323 * just a better generalised solution for these types?
1324 */
1325
1326 /* Protocol tag overwrites all other tag value sent by zebra */
1327 if (ospf->dtag[rt_type] > 0)
1328 api.tag = ospf->dtag[rt_type];
1329
1330 /*
1331 * Given zebra sends update for a prefix via ADD message, it
1332 * should
1333 * be considered as an implicit DEL for that prefix with other
1334 * source
1335 * types.
1336 */
1337 for (i = 0; i <= ZEBRA_ROUTE_MAX; i++)
1338 if (i != rt_type)
1339 ospf_external_info_delete(ospf, i, api.instance,
1340 p);
1341
1342 ei = ospf_external_info_add(ospf, rt_type, api.instance, p,
1343 ifindex, nexthop, api.tag,
1344 api.metric);
1345 if (ei == NULL) {
1346 /* Nothing has changed, so nothing to do; return */
1347 return 0;
1348 }
1349 if (ospf->router_id.s_addr != INADDR_ANY) {
1350 if (is_default_prefix4(&p))
1351 ospf_external_lsa_refresh_default(ospf);
1352 else {
1353 struct ospf_external_aggr_rt *aggr;
1354 struct as_external_lsa *al;
1355 struct ospf_lsa *lsa = NULL;
1356 struct in_addr mask;
1357
1358 aggr = ospf_external_aggr_match(ospf, &ei->p);
1359
1360 if (aggr) {
1361 /* Check the AS-external-LSA
1362 * should be originated.
1363 */
1364 if (!ospf_redistribute_check(ospf, ei,
1365 NULL))
1366 return 0;
1367
1368 if (IS_DEBUG_OSPF(lsa, EXTNL_LSA_AGGR))
1369 zlog_debug(
1370 "%s: Send Aggreate LSA (%pI4/%d)",
1371 __func__,
1372 &aggr->p.prefix,
1373 aggr->p.prefixlen);
1374
1375 ospf_originate_summary_lsa(ospf, aggr,
1376 ei);
1377
1378 /* Handling the case where the
1379 * external route prefix
1380 * and aggegate prefix is same
1381 * If same don't flush the
1382 * originated
1383 * external LSA.
1384 */
1385 if (prefix_same(
1386 (struct prefix *)&aggr->p,
1387 (struct prefix *)&ei->p))
1388 return 0;
1389
1390 lsa = ospf_external_info_find_lsa(
1391 ospf, &ei->p);
1392
1393 if (lsa) {
1394 al = (struct as_external_lsa *)
1395 lsa->data;
1396 masklen2ip(ei->p.prefixlen,
1397 &mask);
1398
1399 if (mask.s_addr
1400 != al->mask.s_addr)
1401 return 0;
1402
1403 ospf_external_lsa_flush(
1404 ospf, ei->type, &ei->p,
1405 0);
1406 }
1407 } else {
1408 struct ospf_lsa *current;
1409
1410 current = ospf_external_info_find_lsa(
1411 ospf, &ei->p);
1412 if (!current) {
1413 /* Check the
1414 * AS-external-LSA
1415 * should be
1416 * originated.
1417 */
1418 if (!ospf_redistribute_check(
1419 ospf, ei, NULL))
1420 return 0;
1421
1422 ospf_external_lsa_originate(
1423 ospf, ei);
1424 } else {
1425 if (IS_DEBUG_OSPF(
1426 zebra,
1427 ZEBRA_REDISTRIBUTE))
1428 zlog_debug(
1429 "%s: %pI4 refreshing LSA",
1430 __func__,
1431 &p.prefix);
1432 ospf_external_lsa_refresh(
1433 ospf, current, ei,
1434 LSA_REFRESH_FORCE,
1435 false);
1436 }
1437 }
1438 }
1439 }
1440
1441 /*
1442 * Check if default-information originate is
1443 * with some routemap prefix/access list match.
1444 */
1445 ospf_external_lsa_default_routemap_apply(ospf, ei, cmd);
1446
1447 } else { /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
1448 struct ospf_external_aggr_rt *aggr;
1449
1450 ei = ospf_external_info_lookup(ospf, rt_type, api.instance, &p);
1451 if (ei == NULL)
1452 return 0;
1453
1454 /*
1455 * Check if default-information originate i
1456 * with some routemap prefix/access list match.
1457 * Apply before ei is deleted.
1458 */
1459 ospf_external_lsa_default_routemap_apply(ospf, ei, cmd);
1460
1461 aggr = ospf_external_aggr_match(ospf, &ei->p);
1462
1463 if (aggr && (ei->aggr_route == aggr)) {
1464 ospf_unlink_ei_from_aggr(ospf, aggr, ei);
1465
1466 ospf_external_info_delete(ospf, rt_type, api.instance,
1467 p);
1468 } else {
1469 ospf_external_info_delete(ospf, rt_type, api.instance,
1470 p);
1471
1472 if (is_default_prefix4(&p))
1473 ospf_external_lsa_refresh_default(ospf);
1474 else
1475 ospf_external_lsa_flush(ospf, rt_type, &p,
1476 ifindex /*, nexthop */);
1477 }
1478 }
1479
1480 return 0;
1481 }
1482
1483 void ospf_zebra_import_default_route(struct ospf *ospf, bool unreg)
1484 {
1485 struct prefix prefix = {};
1486 int command;
1487
1488 if (zclient->sock < 0) {
1489 if (IS_DEBUG_OSPF(zebra, ZEBRA))
1490 zlog_debug(" Not connected to Zebra");
1491 return;
1492 }
1493
1494 prefix.family = AF_INET;
1495 prefix.prefixlen = 0;
1496
1497 if (unreg)
1498 command = ZEBRA_NEXTHOP_UNREGISTER;
1499 else
1500 command = ZEBRA_NEXTHOP_REGISTER;
1501
1502 if (IS_DEBUG_OSPF(zebra, ZEBRA))
1503 zlog_debug("%s: sending cmd %s for %pFX (vrf %u)", __func__,
1504 zserv_command_string(command), &prefix,
1505 ospf->vrf_id);
1506
1507 if (zclient_send_rnh(zclient, command, &prefix, SAFI_UNICAST, false,
1508 true, ospf->vrf_id) == ZCLIENT_SEND_FAILURE)
1509 flog_err(EC_LIB_ZAPI_SOCKET, "%s: zclient_send_rnh() failed",
1510 __func__);
1511 }
1512
1513 static int ospf_zebra_import_check_update(ZAPI_CALLBACK_ARGS)
1514 {
1515 struct ospf *ospf;
1516 struct zapi_route nhr;
1517 struct prefix matched;
1518
1519 ospf = ospf_lookup_by_vrf_id(vrf_id);
1520 if (ospf == NULL || !IS_OSPF_ASBR(ospf))
1521 return 0;
1522
1523 if (!zapi_nexthop_update_decode(zclient->ibuf, &matched, &nhr)) {
1524 zlog_err("%s[%u]: Failure to decode route", __func__,
1525 ospf->vrf_id);
1526 return -1;
1527 }
1528
1529 if (matched.family != AF_INET || matched.prefixlen != 0 ||
1530 nhr.type == ZEBRA_ROUTE_OSPF)
1531 return 0;
1532
1533 ospf->nssa_default_import_check.status = !!nhr.nexthop_num;
1534 ospf_abr_nssa_type7_defaults(ospf);
1535
1536 return 0;
1537 }
1538
1539 int ospf_distribute_list_out_set(struct ospf *ospf, int type, const char *name)
1540 {
1541 /* Lookup access-list for distribute-list. */
1542 DISTRIBUTE_LIST(ospf, type) = access_list_lookup(AFI_IP, name);
1543
1544 /* Clear previous distribute-name. */
1545 if (DISTRIBUTE_NAME(ospf, type))
1546 free(DISTRIBUTE_NAME(ospf, type));
1547
1548 /* Set distribute-name. */
1549 DISTRIBUTE_NAME(ospf, type) = strdup(name);
1550
1551 /* If access-list have been set, schedule update timer. */
1552 if (DISTRIBUTE_LIST(ospf, type))
1553 ospf_distribute_list_update(ospf, type, 0);
1554
1555 return CMD_SUCCESS;
1556 }
1557
1558 int ospf_distribute_list_out_unset(struct ospf *ospf, int type,
1559 const char *name)
1560 {
1561 /* Schedule update timer. */
1562 if (DISTRIBUTE_LIST(ospf, type))
1563 ospf_distribute_list_update(ospf, type, 0);
1564
1565 /* Unset distribute-list. */
1566 DISTRIBUTE_LIST(ospf, type) = NULL;
1567
1568 /* Clear distribute-name. */
1569 if (DISTRIBUTE_NAME(ospf, type))
1570 free(DISTRIBUTE_NAME(ospf, type));
1571
1572 DISTRIBUTE_NAME(ospf, type) = NULL;
1573
1574 return CMD_SUCCESS;
1575 }
1576
1577 /* distribute-list update timer. */
1578 static void ospf_distribute_list_update_timer(struct event *thread)
1579 {
1580 struct route_node *rn;
1581 struct external_info *ei;
1582 struct route_table *rt;
1583 struct ospf_lsa *lsa;
1584 int type, default_refresh = 0;
1585 struct ospf *ospf = EVENT_ARG(thread);
1586
1587 if (ospf == NULL)
1588 return;
1589
1590 ospf->t_distribute_update = NULL;
1591
1592 zlog_info("Zebra[Redistribute]: distribute-list update timer fired!");
1593
1594 if (IS_DEBUG_OSPF_EVENT) {
1595 zlog_debug("%s: ospf distribute-list update vrf %s id %d",
1596 __func__, ospf_vrf_id_to_name(ospf->vrf_id),
1597 ospf->vrf_id);
1598 }
1599
1600 /* foreach all external info. */
1601 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1602 struct list *ext_list;
1603 struct listnode *node;
1604 struct ospf_external *ext;
1605
1606 ext_list = ospf->external[type];
1607 if (!ext_list)
1608 continue;
1609
1610 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
1611 rt = ext->external_info;
1612 if (!rt)
1613 continue;
1614 for (rn = route_top(rt); rn; rn = route_next(rn)) {
1615 ei = rn->info;
1616 if (!ei)
1617 continue;
1618
1619 if (is_default_prefix4(&ei->p))
1620 default_refresh = 1;
1621 else {
1622 struct ospf_external_aggr_rt *aggr;
1623
1624 aggr = ospf_external_aggr_match(ospf,
1625 &ei->p);
1626 if (aggr) {
1627 /* Check the
1628 * AS-external-LSA
1629 * should be originated.
1630 */
1631 if (!ospf_redistribute_check(
1632 ospf, ei, NULL)) {
1633
1634 ospf_unlink_ei_from_aggr(
1635 ospf, aggr, ei);
1636 continue;
1637 }
1638
1639 if (IS_DEBUG_OSPF(
1640 lsa,
1641 EXTNL_LSA_AGGR))
1642 zlog_debug(
1643 "%s: Send Aggregate LSA (%pI4/%d)",
1644 __func__,
1645 &aggr->p.prefix,
1646 aggr->p.prefixlen);
1647
1648 /* Originate Aggregate
1649 * LSA
1650 */
1651 ospf_originate_summary_lsa(
1652 ospf, aggr, ei);
1653 } else if (
1654 (lsa = ospf_external_info_find_lsa(
1655 ospf, &ei->p))) {
1656 int force =
1657 LSA_REFRESH_IF_CHANGED;
1658 /* If this is a MaxAge
1659 * LSA, we need to
1660 * force refresh it
1661 * because distribute
1662 * settings might have
1663 * changed and now,
1664 * this LSA needs to be
1665 * originated, not be
1666 * removed.
1667 * If we don't force
1668 * refresh it, it will
1669 * remain a MaxAge LSA
1670 * because it will look
1671 * like it hasn't
1672 * changed. Neighbors
1673 * will not receive
1674 * updates for this LSA.
1675 */
1676 if (IS_LSA_MAXAGE(lsa))
1677 force = LSA_REFRESH_FORCE;
1678
1679 ospf_external_lsa_refresh(
1680 ospf, lsa, ei, force,
1681 false);
1682 } else {
1683 if (!ospf_redistribute_check(
1684 ospf, ei, NULL))
1685 continue;
1686 ospf_external_lsa_originate(
1687 ospf, ei);
1688 }
1689 }
1690 }
1691 }
1692 }
1693 if (default_refresh)
1694 ospf_external_lsa_refresh_default(ospf);
1695 }
1696
1697 /* Update distribute-list and set timer to apply access-list. */
1698 void ospf_distribute_list_update(struct ospf *ospf, int type,
1699 unsigned short instance)
1700 {
1701 struct ospf_external *ext;
1702
1703 /* External info does not exist. */
1704 ext = ospf_external_lookup(ospf, type, instance);
1705 if (!ext || !EXTERNAL_INFO(ext))
1706 return;
1707
1708 /* Set timer. If timer is already started, this call does nothing. */
1709 event_add_timer_msec(master, ospf_distribute_list_update_timer, ospf,
1710 ospf->min_ls_interval, &ospf->t_distribute_update);
1711 }
1712
1713 /* If access-list is updated, apply some check. */
1714 static void ospf_filter_update(struct access_list *access)
1715 {
1716 struct ospf *ospf;
1717 int type;
1718 int abr_inv = 0;
1719 struct ospf_area *area;
1720 struct listnode *node, *n1;
1721
1722 /* If OSPF instance does not exist, return right now. */
1723 if (listcount(om->ospf) == 0)
1724 return;
1725
1726 /* Iterate all ospf [VRF] instances */
1727 for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
1728 /* Update distribute-list, and apply filter. */
1729 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1730 struct list *red_list;
1731 struct ospf_redist *red;
1732
1733 red_list = ospf->redist[type];
1734 if (red_list)
1735 for (ALL_LIST_ELEMENTS_RO(red_list, node,
1736 red)) {
1737 if (ROUTEMAP(red)) {
1738 /* if route-map is not NULL it
1739 * may be
1740 * using this access list */
1741 ospf_distribute_list_update(
1742 ospf, type,
1743 red->instance);
1744 }
1745 }
1746
1747 /* There is place for route-map for default-information
1748 * (ZEBRA_ROUTE_MAX),
1749 * but no distribute list. */
1750 if (type == ZEBRA_ROUTE_MAX)
1751 break;
1752
1753 if (DISTRIBUTE_NAME(ospf, type)) {
1754 /* Keep old access-list for distribute-list. */
1755 struct access_list *old =
1756 DISTRIBUTE_LIST(ospf, type);
1757
1758 /* Update access-list for distribute-list. */
1759 DISTRIBUTE_LIST(ospf, type) =
1760 access_list_lookup(
1761 AFI_IP,
1762 DISTRIBUTE_NAME(ospf, type));
1763
1764 /* No update for this distribute type. */
1765 if (old == NULL
1766 && DISTRIBUTE_LIST(ospf, type) == NULL)
1767 continue;
1768
1769 /* Schedule distribute-list update timer. */
1770 if (DISTRIBUTE_LIST(ospf, type) == NULL
1771 || strcmp(DISTRIBUTE_NAME(ospf, type),
1772 access->name)
1773 == 0)
1774 ospf_distribute_list_update(ospf, type,
1775 0);
1776 }
1777 }
1778
1779 /* Update Area access-list. */
1780 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1781 if (EXPORT_NAME(area)) {
1782 EXPORT_LIST(area) = NULL;
1783 abr_inv++;
1784 }
1785
1786 if (IMPORT_NAME(area)) {
1787 IMPORT_LIST(area) = NULL;
1788 abr_inv++;
1789 }
1790 }
1791
1792 /* Schedule ABR tasks -- this will be changed -- takada. */
1793 if (IS_OSPF_ABR(ospf) && abr_inv)
1794 ospf_schedule_abr_task(ospf);
1795 }
1796 }
1797
1798 /* If prefix-list is updated, do some updates. */
1799 static void ospf_prefix_list_update(struct prefix_list *plist)
1800 {
1801 struct ospf *ospf = NULL;
1802 int type;
1803 int abr_inv = 0;
1804 struct ospf_area *area;
1805 struct listnode *node, *n1;
1806
1807 /* If OSPF instatnce does not exist, return right now. */
1808 if (listcount(om->ospf) == 0)
1809 return;
1810
1811 /* Iterate all ospf [VRF] instances */
1812 for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
1813
1814 /* Update all route-maps which are used
1815 * as redistribution filters.
1816 * They might use prefix-list.
1817 */
1818 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1819 struct list *red_list;
1820 struct ospf_redist *red;
1821
1822 red_list = ospf->redist[type];
1823 if (!red_list)
1824 continue;
1825
1826 for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) {
1827 if (ROUTEMAP(red)) {
1828 /* if route-map is not NULL
1829 * it may be using
1830 * this prefix list */
1831 ospf_distribute_list_update(
1832 ospf, type, red->instance);
1833 }
1834 }
1835 }
1836
1837 /* Update area filter-lists. */
1838 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1839 /* Update filter-list in. */
1840 if (PREFIX_NAME_IN(area)
1841 && strcmp(PREFIX_NAME_IN(area),
1842 prefix_list_name(plist))
1843 == 0) {
1844 PREFIX_LIST_IN(area) = prefix_list_lookup(
1845 AFI_IP, PREFIX_NAME_IN(area));
1846 abr_inv++;
1847 }
1848
1849 /* Update filter-list out. */
1850 if (PREFIX_NAME_OUT(area)
1851 && strcmp(PREFIX_NAME_OUT(area),
1852 prefix_list_name(plist))
1853 == 0) {
1854 PREFIX_LIST_OUT(area) = prefix_list_lookup(
1855 AFI_IP, PREFIX_NAME_OUT(area));
1856 abr_inv++;
1857 }
1858 }
1859
1860 /* Schedule ABR task. */
1861 if (IS_OSPF_ABR(ospf) && abr_inv)
1862 ospf_schedule_abr_task(ospf);
1863 }
1864 }
1865
1866 static struct ospf_distance *ospf_distance_new(void)
1867 {
1868 return XCALLOC(MTYPE_OSPF_DISTANCE, sizeof(struct ospf_distance));
1869 }
1870
1871 static void ospf_distance_free(struct ospf_distance *odistance)
1872 {
1873 XFREE(MTYPE_OSPF_DISTANCE, odistance);
1874 }
1875
1876 int ospf_distance_set(struct vty *vty, struct ospf *ospf,
1877 const char *distance_str, const char *ip_str,
1878 const char *access_list_str)
1879 {
1880 int ret;
1881 struct prefix_ipv4 p;
1882 uint8_t distance;
1883 struct route_node *rn;
1884 struct ospf_distance *odistance;
1885
1886 ret = str2prefix_ipv4(ip_str, &p);
1887 if (ret == 0) {
1888 vty_out(vty, "Malformed prefix\n");
1889 return CMD_WARNING_CONFIG_FAILED;
1890 }
1891
1892 distance = atoi(distance_str);
1893
1894 /* Get OSPF distance node. */
1895 rn = route_node_get(ospf->distance_table, (struct prefix *)&p);
1896 if (rn->info) {
1897 odistance = rn->info;
1898 route_unlock_node(rn);
1899 } else {
1900 odistance = ospf_distance_new();
1901 rn->info = odistance;
1902 }
1903
1904 /* Set distance value. */
1905 odistance->distance = distance;
1906
1907 /* Reset access-list configuration. */
1908 if (odistance->access_list) {
1909 free(odistance->access_list);
1910 odistance->access_list = NULL;
1911 }
1912 if (access_list_str)
1913 odistance->access_list = strdup(access_list_str);
1914
1915 return CMD_SUCCESS;
1916 }
1917
1918 int ospf_distance_unset(struct vty *vty, struct ospf *ospf,
1919 const char *distance_str, const char *ip_str,
1920 char const *access_list_str)
1921 {
1922 int ret;
1923 struct prefix_ipv4 p;
1924 struct route_node *rn;
1925 struct ospf_distance *odistance;
1926
1927 ret = str2prefix_ipv4(ip_str, &p);
1928 if (ret == 0) {
1929 vty_out(vty, "Malformed prefix\n");
1930 return CMD_WARNING_CONFIG_FAILED;
1931 }
1932
1933 rn = route_node_lookup(ospf->distance_table, (struct prefix *)&p);
1934 if (!rn) {
1935 vty_out(vty, "Can't find specified prefix\n");
1936 return CMD_WARNING_CONFIG_FAILED;
1937 }
1938
1939 odistance = rn->info;
1940
1941 if (odistance->access_list)
1942 free(odistance->access_list);
1943 ospf_distance_free(odistance);
1944
1945 rn->info = NULL;
1946 route_unlock_node(rn);
1947 route_unlock_node(rn);
1948
1949 return CMD_SUCCESS;
1950 }
1951
1952 void ospf_distance_reset(struct ospf *ospf)
1953 {
1954 struct route_node *rn;
1955 struct ospf_distance *odistance;
1956
1957 for (rn = route_top(ospf->distance_table); rn; rn = route_next(rn)) {
1958 odistance = rn->info;
1959 if (!odistance)
1960 continue;
1961
1962 if (odistance->access_list)
1963 free(odistance->access_list);
1964 ospf_distance_free(odistance);
1965 rn->info = NULL;
1966 route_unlock_node(rn);
1967 }
1968 }
1969
1970 uint8_t ospf_distance_apply(struct ospf *ospf, struct prefix_ipv4 *p,
1971 struct ospf_route * or)
1972 {
1973
1974 if (ospf == NULL)
1975 return 0;
1976
1977 if (ospf->distance_intra && or->path_type == OSPF_PATH_INTRA_AREA)
1978 return ospf->distance_intra;
1979
1980 if (ospf->distance_inter && or->path_type == OSPF_PATH_INTER_AREA)
1981 return ospf->distance_inter;
1982
1983 if (ospf->distance_external
1984 && (or->path_type == OSPF_PATH_TYPE1_EXTERNAL ||
1985 or->path_type == OSPF_PATH_TYPE2_EXTERNAL))
1986 return ospf->distance_external;
1987
1988 if (ospf->distance_all)
1989 return ospf->distance_all;
1990
1991 return 0;
1992 }
1993
1994 void ospf_zebra_vrf_register(struct ospf *ospf)
1995 {
1996 if (!zclient || zclient->sock < 0 || !ospf)
1997 return;
1998
1999 if (ospf->vrf_id != VRF_UNKNOWN) {
2000 if (IS_DEBUG_OSPF_EVENT)
2001 zlog_debug("%s: Register VRF %s id %u", __func__,
2002 ospf_vrf_id_to_name(ospf->vrf_id),
2003 ospf->vrf_id);
2004 zclient_send_reg_requests(zclient, ospf->vrf_id);
2005 }
2006 }
2007
2008 void ospf_zebra_vrf_deregister(struct ospf *ospf)
2009 {
2010 if (!zclient || zclient->sock < 0 || !ospf)
2011 return;
2012
2013 if (ospf->vrf_id != VRF_DEFAULT && ospf->vrf_id != VRF_UNKNOWN) {
2014 if (IS_DEBUG_OSPF_EVENT)
2015 zlog_debug("%s: De-Register VRF %s id %u to Zebra.",
2016 __func__, ospf_vrf_id_to_name(ospf->vrf_id),
2017 ospf->vrf_id);
2018 /* Deregister for router-id, interfaces,
2019 * redistributed routes. */
2020 zclient_send_dereg_requests(zclient, ospf->vrf_id);
2021 }
2022 }
2023
2024 /* Label Manager Functions */
2025
2026 /**
2027 * Check if Label Manager is Ready or not.
2028 *
2029 * @return True if Label Manager is ready, False otherwise
2030 */
2031 bool ospf_zebra_label_manager_ready(void)
2032 {
2033 return (zclient_sync->sock > 0);
2034 }
2035
2036 /**
2037 * Request Label Range to the Label Manager.
2038 *
2039 * @param base base label of the label range to request
2040 * @param chunk_size size of the label range to request
2041 *
2042 * @return 0 on success, -1 on failure
2043 */
2044 int ospf_zebra_request_label_range(uint32_t base, uint32_t chunk_size)
2045 {
2046 int ret;
2047 uint32_t start, end;
2048
2049 if (zclient_sync->sock < 0)
2050 return -1;
2051
2052 ret = lm_get_label_chunk(zclient_sync, 0, base, chunk_size, &start,
2053 &end);
2054 if (ret < 0) {
2055 zlog_warn("%s: error getting label range!", __func__);
2056 return -1;
2057 }
2058
2059 return 0;
2060 }
2061
2062 /**
2063 * Release Label Range to the Label Manager.
2064 *
2065 * @param start start of label range to release
2066 * @param end end of label range to release
2067 *
2068 * @return 0 on success, -1 otherwise
2069 */
2070 int ospf_zebra_release_label_range(uint32_t start, uint32_t end)
2071 {
2072 int ret;
2073
2074 if (zclient_sync->sock < 0)
2075 return -1;
2076
2077 ret = lm_release_label_chunk(zclient_sync, start, end);
2078 if (ret < 0) {
2079 zlog_warn("%s: error releasing label range!", __func__);
2080 return -1;
2081 }
2082
2083 return 0;
2084 }
2085
2086 /**
2087 * Connect to the Label Manager.
2088 *
2089 * @return 0 on success, -1 otherwise
2090 */
2091 int ospf_zebra_label_manager_connect(void)
2092 {
2093 /* Connect to label manager. */
2094 if (zclient_socket_connect(zclient_sync) < 0) {
2095 zlog_warn("%s: failed connecting synchronous zclient!",
2096 __func__);
2097 return -1;
2098 }
2099 /* make socket non-blocking */
2100 set_nonblocking(zclient_sync->sock);
2101
2102 /* Send hello to notify zebra this is a synchronous client */
2103 if (zclient_send_hello(zclient_sync) == ZCLIENT_SEND_FAILURE) {
2104 zlog_warn("%s: failed sending hello for synchronous zclient!",
2105 __func__);
2106 close(zclient_sync->sock);
2107 zclient_sync->sock = -1;
2108 return -1;
2109 }
2110
2111 /* Connect to label manager */
2112 if (lm_label_manager_connect(zclient_sync, 0) != 0) {
2113 zlog_warn("%s: failed connecting to label manager!", __func__);
2114 if (zclient_sync->sock > 0) {
2115 close(zclient_sync->sock);
2116 zclient_sync->sock = -1;
2117 }
2118 return -1;
2119 }
2120
2121 osr_debug("SR (%s): Successfully connected to the Label Manager",
2122 __func__);
2123
2124 return 0;
2125 }
2126
2127 static void ospf_zebra_connected(struct zclient *zclient)
2128 {
2129 struct ospf *ospf;
2130 struct listnode *node;
2131
2132 /* Send the client registration */
2133 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
2134
2135 zclient_send_reg_requests(zclient, VRF_DEFAULT);
2136
2137 /* Activate graceful restart if configured. */
2138 for (ALL_LIST_ELEMENTS_RO(om->ospf, node, ospf)) {
2139 if (!ospf->gr_info.restart_support)
2140 continue;
2141 (void)ospf_zebra_gr_enable(ospf, ospf->gr_info.grace_period);
2142 }
2143 }
2144
2145 /*
2146 * opaque messages between processes
2147 */
2148 static int ospf_opaque_msg_handler(ZAPI_CALLBACK_ARGS)
2149 {
2150 struct stream *s;
2151 struct zapi_opaque_msg info;
2152 struct ldp_igp_sync_if_state state;
2153 struct ldp_igp_sync_announce announce;
2154 struct zapi_opaque_reg_info dst;
2155 int ret = 0;
2156
2157 s = zclient->ibuf;
2158
2159 if (zclient_opaque_decode(s, &info) != 0)
2160 return -1;
2161
2162 switch (info.type) {
2163 case LINK_STATE_SYNC:
2164 STREAM_GETC(s, dst.proto);
2165 STREAM_GETW(s, dst.instance);
2166 STREAM_GETL(s, dst.session_id);
2167 dst.type = LINK_STATE_SYNC;
2168 ret = ospf_te_sync_ted(dst);
2169 break;
2170 case LDP_IGP_SYNC_IF_STATE_UPDATE:
2171 STREAM_GET(&state, s, sizeof(state));
2172 ret = ospf_ldp_sync_state_update(state);
2173 break;
2174 case LDP_IGP_SYNC_ANNOUNCE_UPDATE:
2175 STREAM_GET(&announce, s, sizeof(announce));
2176 ret = ospf_ldp_sync_announce_update(announce);
2177 break;
2178 default:
2179 break;
2180 }
2181
2182 stream_failure:
2183
2184 return ret;
2185 }
2186
2187 static int ospf_zebra_client_close_notify(ZAPI_CALLBACK_ARGS)
2188 {
2189 int ret = 0;
2190
2191 struct zapi_client_close_info info;
2192
2193 if (zapi_client_close_notify_decode(zclient->ibuf, &info) < 0)
2194 return -1;
2195
2196 ospf_ldp_sync_handle_client_close(&info);
2197
2198 return ret;
2199 }
2200
2201 static zclient_handler *const ospf_handlers[] = {
2202 [ZEBRA_ROUTER_ID_UPDATE] = ospf_router_id_update_zebra,
2203 [ZEBRA_INTERFACE_ADDRESS_ADD] = ospf_interface_address_add,
2204 [ZEBRA_INTERFACE_ADDRESS_DELETE] = ospf_interface_address_delete,
2205 [ZEBRA_INTERFACE_LINK_PARAMS] = ospf_interface_link_params,
2206 [ZEBRA_INTERFACE_VRF_UPDATE] = ospf_interface_vrf_update,
2207
2208 [ZEBRA_REDISTRIBUTE_ROUTE_ADD] = ospf_zebra_read_route,
2209 [ZEBRA_REDISTRIBUTE_ROUTE_DEL] = ospf_zebra_read_route,
2210 [ZEBRA_NEXTHOP_UPDATE] = ospf_zebra_import_check_update,
2211
2212 [ZEBRA_OPAQUE_MESSAGE] = ospf_opaque_msg_handler,
2213
2214 [ZEBRA_CLIENT_CLOSE_NOTIFY] = ospf_zebra_client_close_notify,
2215 };
2216
2217 void ospf_zebra_init(struct event_loop *master, unsigned short instance)
2218 {
2219 /* Allocate zebra structure. */
2220 zclient = zclient_new(master, &zclient_options_default, ospf_handlers,
2221 array_size(ospf_handlers));
2222 zclient_init(zclient, ZEBRA_ROUTE_OSPF, instance, &ospfd_privs);
2223 zclient->zebra_connected = ospf_zebra_connected;
2224
2225 /* Initialize special zclient for synchronous message exchanges. */
2226 struct zclient_options options = zclient_options_default;
2227 options.synchronous = true;
2228 zclient_sync = zclient_new(master, &options, NULL, 0);
2229 zclient_sync->sock = -1;
2230 zclient_sync->redist_default = ZEBRA_ROUTE_OSPF;
2231 zclient_sync->instance = instance;
2232 /*
2233 * session_id must be different from default value (0) to distinguish
2234 * the asynchronous socket from the synchronous one
2235 */
2236 zclient_sync->session_id = 1;
2237 zclient_sync->privs = &ospfd_privs;
2238
2239 access_list_add_hook(ospf_filter_update);
2240 access_list_delete_hook(ospf_filter_update);
2241 prefix_list_add_hook(ospf_prefix_list_update);
2242 prefix_list_delete_hook(ospf_prefix_list_update);
2243 }
2244
2245 void ospf_zebra_send_arp(const struct interface *ifp, const struct prefix *p)
2246 {
2247 zclient_send_neigh_discovery_req(zclient, ifp, p);
2248 }