1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Zebra connect library for OSPFd
4 * Copyright (C) 1997, 98, 99, 2000 Kunihiro Ishiguro, Toshiaki Takada
21 #include "route_opaque.h"
25 #include "ospfd/ospfd.h"
26 #include "ospfd/ospf_interface.h"
27 #include "ospfd/ospf_ism.h"
28 #include "ospfd/ospf_asbr.h"
29 #include "ospfd/ospf_asbr.h"
30 #include "ospfd/ospf_abr.h"
31 #include "ospfd/ospf_lsa.h"
32 #include "ospfd/ospf_dump.h"
33 #include "ospfd/ospf_route.h"
34 #include "ospfd/ospf_lsdb.h"
35 #include "ospfd/ospf_neighbor.h"
36 #include "ospfd/ospf_nsm.h"
37 #include "ospfd/ospf_zebra.h"
38 #include "ospfd/ospf_te.h"
39 #include "ospfd/ospf_sr.h"
40 #include "ospfd/ospf_ldp_sync.h"
42 DEFINE_MTYPE_STATIC(OSPFD
, OSPF_EXTERNAL
, "OSPF External route table");
43 DEFINE_MTYPE_STATIC(OSPFD
, OSPF_REDISTRIBUTE
, "OSPF Redistriute");
46 /* Zebra structure to hold current status. */
47 struct zclient
*zclient
= NULL
;
48 /* and for the Synchronous connection to the Label Manager */
49 static struct zclient
*zclient_sync
;
51 /* For registering threads. */
52 extern struct thread_master
*master
;
54 /* Router-id update message from zebra. */
55 static int ospf_router_id_update_zebra(ZAPI_CALLBACK_ARGS
)
57 struct ospf
*ospf
= NULL
;
58 struct prefix router_id
;
59 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
61 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
62 zlog_debug("Zebra rcvd: router id update %pFX vrf %s id %u",
63 &router_id
, ospf_vrf_id_to_name(vrf_id
), vrf_id
);
65 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
68 ospf
->router_id_zebra
= router_id
.u
.prefix4
;
69 ospf_router_id_update(ospf
);
71 if (IS_DEBUG_OSPF_EVENT
)
73 "%s: ospf instance not found for vrf %s id %u router_id %pFX",
74 __func__
, ospf_vrf_id_to_name(vrf_id
), vrf_id
,
80 static int ospf_interface_address_add(ZAPI_CALLBACK_ARGS
)
83 struct ospf
*ospf
= NULL
;
86 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
91 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
92 zlog_debug("Zebra: interface %s address add %pFX vrf %s id %u",
93 c
->ifp
->name
, c
->address
,
94 ospf_vrf_id_to_name(vrf_id
), vrf_id
);
96 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
100 ospf_if_update(ospf
, c
->ifp
);
102 ospf_if_interface(c
->ifp
);
107 static int ospf_interface_address_delete(ZAPI_CALLBACK_ARGS
)
110 struct interface
*ifp
;
111 struct ospf_interface
*oi
;
112 struct route_node
*rn
;
115 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
120 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
121 zlog_debug("Zebra: interface %s address delete %pFX",
122 c
->ifp
->name
, c
->address
);
126 p
.prefixlen
= IPV4_MAX_BITLEN
;
128 rn
= route_node_lookup(IF_OIFS(ifp
), &p
);
136 route_unlock_node(rn
);
138 /* Call interface hook functions to clean up */
141 ospf_if_interface(c
->ifp
);
148 static int ospf_interface_link_params(ZAPI_CALLBACK_ARGS
)
150 struct interface
*ifp
;
151 bool changed
= false;
153 ifp
= zebra_interface_link_params_read(zclient
->ibuf
, vrf_id
, &changed
);
155 if (ifp
== NULL
|| !changed
)
159 ospf_mpls_te_update_if(ifp
);
164 /* VRF update for an interface. */
165 static int ospf_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
167 struct interface
*ifp
= NULL
;
170 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
175 if (IS_DEBUG_OSPF_EVENT
)
177 "%s: Rx Interface %s VRF change vrf_id %u New vrf %s id %u",
178 __func__
, ifp
->name
, vrf_id
,
179 ospf_vrf_id_to_name(new_vrf_id
), new_vrf_id
);
181 /*if_update(ifp, ifp->name, strlen(ifp->name), new_vrf_id);*/
182 if_update_to_new_vrf(ifp
, new_vrf_id
);
187 /* Nexthop, ifindex, distance and metric information. */
188 static void ospf_zebra_add_nexthop(struct ospf
*ospf
, struct ospf_path
*path
,
189 struct zapi_route
*api
)
191 struct zapi_nexthop
*api_nh
;
192 struct zapi_nexthop
*api_nh_backup
;
194 /* TI-LFA backup path label stack comes first, if present */
195 if (path
->srni
.backup_label_stack
) {
196 api_nh_backup
= &api
->backup_nexthops
[api
->backup_nexthop_num
];
197 api_nh_backup
->vrf_id
= ospf
->vrf_id
;
199 api_nh_backup
->type
= NEXTHOP_TYPE_IPV4
;
200 api_nh_backup
->gate
.ipv4
= path
->srni
.backup_nexthop
;
202 api_nh_backup
->label_num
=
203 path
->srni
.backup_label_stack
->num_labels
;
204 memcpy(api_nh_backup
->labels
,
205 path
->srni
.backup_label_stack
->label
,
206 sizeof(mpls_label_t
) * api_nh_backup
->label_num
);
208 api
->backup_nexthop_num
++;
211 /* And here comes the primary nexthop */
212 api_nh
= &api
->nexthops
[api
->nexthop_num
];
215 || (path
->nexthop
.s_addr
!= INADDR_ANY
&& path
->ifindex
!= 0)) {
216 #else /* HAVE_NETLINK */
217 if (path
->nexthop
.s_addr
!= INADDR_ANY
&& path
->ifindex
!= 0) {
218 #endif /* HAVE_NETLINK */
219 api_nh
->gate
.ipv4
= path
->nexthop
;
220 api_nh
->ifindex
= path
->ifindex
;
221 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
222 } else if (path
->nexthop
.s_addr
!= INADDR_ANY
) {
223 api_nh
->gate
.ipv4
= path
->nexthop
;
224 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
226 api_nh
->ifindex
= path
->ifindex
;
227 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
229 api_nh
->vrf_id
= ospf
->vrf_id
;
231 /* Set TI-LFA backup nexthop info if present */
232 if (path
->srni
.backup_label_stack
) {
233 SET_FLAG(api
->message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
);
234 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
);
236 /* Just care about a single TI-LFA backup path for now */
237 api_nh
->backup_num
= 1;
238 api_nh
->backup_idx
[0] = api
->backup_nexthop_num
- 1;
244 static void ospf_zebra_append_opaque_attr(struct ospf_route
*or,
245 struct zapi_route
*api
)
247 struct ospf_zebra_opaque ospf_opaque
= {};
250 snprintf(ospf_opaque
.path_type
, sizeof(ospf_opaque
.path_type
), "%s",
251 ospf_path_type_name(or->path_type
));
253 switch (or->path_type
) {
254 case OSPF_PATH_INTRA_AREA
:
255 case OSPF_PATH_INTER_AREA
:
257 (void)inet_ntop(AF_INET
, &or->u
.std
.area_id
,
259 sizeof(ospf_opaque
.area_id
));
261 case OSPF_PATH_TYPE1_EXTERNAL
:
262 case OSPF_PATH_TYPE2_EXTERNAL
:
264 snprintf(ospf_opaque
.tag
, sizeof(ospf_opaque
.tag
), "%u",
271 SET_FLAG(api
->message
, ZAPI_MESSAGE_OPAQUE
);
272 api
->opaque
.length
= sizeof(struct ospf_zebra_opaque
);
273 memcpy(api
->opaque
.data
, &ospf_opaque
, api
->opaque
.length
);
276 void ospf_zebra_add(struct ospf
*ospf
, struct prefix_ipv4
*p
,
277 struct ospf_route
* or)
279 struct zapi_route api
;
281 struct ospf_path
*path
;
282 struct listnode
*node
;
284 if (ospf
->gr_info
.restart_in_progress
) {
285 if (IS_DEBUG_OSPF_GR
)
287 "Zebra: Graceful Restart in progress -- not installing %pFX",
292 memset(&api
, 0, sizeof(api
));
293 api
.vrf_id
= ospf
->vrf_id
;
294 api
.type
= ZEBRA_ROUTE_OSPF
;
295 api
.instance
= ospf
->instance
;
296 api
.safi
= SAFI_UNICAST
;
298 memcpy(&api
.prefix
, p
, sizeof(*p
));
299 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
302 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
303 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
)
304 api
.metric
= or->cost
+ or->u
.ext
.type2_cost
;
305 else if (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
306 api
.metric
= or->u
.ext
.type2_cost
;
308 api
.metric
= or->cost
;
310 /* Check if path type is ASE */
311 if (((or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
)
312 || (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
))
313 && (or->u
.ext
.tag
> 0) && (or->u
.ext
.tag
<= ROUTE_TAG_MAX
)) {
314 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
315 api
.tag
= or->u
.ext
.tag
;
318 /* Distance value. */
319 distance
= ospf_distance_apply(ospf
, p
, or);
321 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
322 api
.distance
= distance
;
325 for (ALL_LIST_ELEMENTS_RO(or->paths
, node
, path
)) {
326 if (api
.nexthop_num
>= ospf
->max_multipath
)
329 ospf_zebra_add_nexthop(ospf
, path
, &api
);
331 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
)) {
332 struct interface
*ifp
;
334 ifp
= if_lookup_by_index(path
->ifindex
, ospf
->vrf_id
);
337 "Zebra: Route add %pFX nexthop %pI4, ifindex=%d %s",
338 p
, &path
->nexthop
, path
->ifindex
,
339 ifp
? ifp
->name
: " ");
343 if (CHECK_FLAG(ospf
->config
, OSPF_SEND_EXTRA_DATA_TO_ZEBRA
))
344 ospf_zebra_append_opaque_attr(or, &api
);
346 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
349 void ospf_zebra_delete(struct ospf
*ospf
, struct prefix_ipv4
*p
,
350 struct ospf_route
* or)
352 struct zapi_route api
;
354 if (ospf
->gr_info
.restart_in_progress
) {
355 if (IS_DEBUG_OSPF_GR
)
357 "Zebra: Graceful Restart in progress -- not uninstalling %pFX",
362 memset(&api
, 0, sizeof(api
));
363 api
.vrf_id
= ospf
->vrf_id
;
364 api
.type
= ZEBRA_ROUTE_OSPF
;
365 api
.instance
= ospf
->instance
;
366 api
.safi
= SAFI_UNICAST
;
367 memcpy(&api
.prefix
, p
, sizeof(*p
));
369 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
370 zlog_debug("Zebra: Route delete %pFX", p
);
372 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
375 void ospf_zebra_add_discard(struct ospf
*ospf
, struct prefix_ipv4
*p
)
377 struct zapi_route api
;
379 if (ospf
->gr_info
.restart_in_progress
) {
380 if (IS_DEBUG_OSPF_GR
)
382 "Zebra: Graceful Restart in progress -- not installing %pFX",
387 memset(&api
, 0, sizeof(api
));
388 api
.vrf_id
= ospf
->vrf_id
;
389 api
.type
= ZEBRA_ROUTE_OSPF
;
390 api
.instance
= ospf
->instance
;
391 api
.safi
= SAFI_UNICAST
;
392 memcpy(&api
.prefix
, p
, sizeof(*p
));
393 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
395 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
397 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
398 zlog_debug("Zebra: Route add discard %pFX", p
);
401 void ospf_zebra_delete_discard(struct ospf
*ospf
, struct prefix_ipv4
*p
)
403 struct zapi_route api
;
405 if (ospf
->gr_info
.restart_in_progress
) {
406 if (IS_DEBUG_OSPF_GR
)
408 "Zebra: Graceful Restart in progress -- not uninstalling %pFX",
413 memset(&api
, 0, sizeof(api
));
414 api
.vrf_id
= ospf
->vrf_id
;
415 api
.type
= ZEBRA_ROUTE_OSPF
;
416 api
.instance
= ospf
->instance
;
417 api
.safi
= SAFI_UNICAST
;
418 memcpy(&api
.prefix
, p
, sizeof(*p
));
419 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
421 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
423 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
424 zlog_debug("Zebra: Route delete discard %pFX", p
);
427 struct ospf_external
*ospf_external_lookup(struct ospf
*ospf
, uint8_t type
,
428 unsigned short instance
)
430 struct list
*ext_list
;
431 struct listnode
*node
;
432 struct ospf_external
*ext
;
434 ext_list
= ospf
->external
[type
];
438 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
))
439 if (ext
->instance
== instance
)
445 struct ospf_external
*ospf_external_add(struct ospf
*ospf
, uint8_t type
,
446 unsigned short instance
)
448 struct list
*ext_list
;
449 struct ospf_external
*ext
;
451 ext
= ospf_external_lookup(ospf
, type
, instance
);
455 if (!ospf
->external
[type
])
456 ospf
->external
[type
] = list_new();
458 ext_list
= ospf
->external
[type
];
459 ext
= XCALLOC(MTYPE_OSPF_EXTERNAL
, sizeof(struct ospf_external
));
460 ext
->instance
= instance
;
461 EXTERNAL_INFO(ext
) = route_table_init();
463 listnode_add(ext_list
, ext
);
469 * Walk all the ei received from zebra for a route type and apply
472 bool ospf_external_default_routemap_apply_walk(struct ospf
*ospf
,
473 struct list
*ext_list
,
474 struct external_info
*default_ei
)
476 struct listnode
*node
;
477 struct ospf_external
*ext
;
478 struct route_node
*rn
;
479 struct external_info
*ei
= NULL
;
482 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
483 if (!ext
->external_info
)
486 for (rn
= route_top(ext
->external_info
); rn
;
487 rn
= route_next(rn
)) {
491 ret
= ospf_external_info_apply_default_routemap(
492 ospf
, ei
, default_ei
);
499 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
500 zlog_debug("Default originate routemap permit ei: %pI4",
509 * Function to originate or flush default after applying
510 * route-map on all ei.
512 static void ospf_external_lsa_default_routemap_timer(struct event
*thread
)
514 struct list
*ext_list
;
515 struct ospf
*ospf
= THREAD_ARG(thread
);
516 struct prefix_ipv4 p
;
519 struct ospf_lsa
*lsa
;
520 struct external_info
*default_ei
;
524 p
.prefix
.s_addr
= INADDR_ANY
;
526 /* Get the default extenal info. */
527 default_ei
= ospf_external_info_lookup(ospf
, DEFAULT_ROUTE
,
530 /* Nothing to be done here. */
531 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
532 zlog_debug("Default originate info not present");
536 /* For all the ei apply route-map */
537 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
538 ext_list
= ospf
->external
[type
];
539 if (!ext_list
|| type
== ZEBRA_ROUTE_OSPF
)
542 ret
= ospf_external_default_routemap_apply_walk(ospf
, ext_list
,
548 /* Get the default LSA. */
549 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
551 /* If permit then originate default. */
553 ospf_external_lsa_originate(ospf
, default_ei
);
554 else if (ret
&& lsa
&& IS_LSA_MAXAGE(lsa
))
555 ospf_external_lsa_refresh(ospf
, lsa
, default_ei
, true, false);
556 else if (!ret
&& lsa
)
557 ospf_external_lsa_flush(ospf
, DEFAULT_ROUTE
, &default_ei
->p
, 0);
561 void ospf_external_del(struct ospf
*ospf
, uint8_t type
, unsigned short instance
)
563 struct ospf_external
*ext
;
565 ext
= ospf_external_lookup(ospf
, type
, instance
);
568 if (EXTERNAL_INFO(ext
))
569 route_table_finish(EXTERNAL_INFO(ext
));
571 listnode_delete(ospf
->external
[type
], ext
);
573 if (!ospf
->external
[type
]->count
)
574 list_delete(&ospf
->external
[type
]);
576 XFREE(MTYPE_OSPF_EXTERNAL
, ext
);
580 * Check if default needs to be flushed too.
582 thread_add_event(master
, ospf_external_lsa_default_routemap_timer
, ospf
,
583 0, &ospf
->t_default_routemap_timer
);
586 /* Update NHLFE for Prefix SID */
587 void ospf_zebra_update_prefix_sid(const struct sr_prefix
*srp
)
589 struct zapi_labels zl
;
590 struct zapi_nexthop
*znh
;
591 struct zapi_nexthop
*znh_backup
;
592 struct listnode
*node
;
593 struct ospf_path
*path
;
595 /* Prepare message. */
596 memset(&zl
, 0, sizeof(zl
));
597 zl
.type
= ZEBRA_LSP_OSPF_SR
;
598 zl
.local_label
= srp
->label_in
;
602 /* Set Label for local Prefix */
603 znh
= &zl
.nexthops
[zl
.nexthop_num
++];
604 znh
->type
= NEXTHOP_TYPE_IFINDEX
;
605 znh
->ifindex
= srp
->nhlfe
.ifindex
;
607 znh
->labels
[0] = srp
->nhlfe
.label_out
;
609 osr_debug("SR (%s): Configure Prefix %pFX with labels %u/%u",
610 __func__
, (struct prefix
*)&srp
->prefv4
,
611 srp
->label_in
, srp
->nhlfe
.label_out
);
616 /* Update route in the RIB too. */
617 SET_FLAG(zl
.message
, ZAPI_LABELS_FTN
);
618 zl
.route
.prefix
.u
.prefix4
= srp
->prefv4
.prefix
;
619 zl
.route
.prefix
.prefixlen
= srp
->prefv4
.prefixlen
;
620 zl
.route
.prefix
.family
= srp
->prefv4
.family
;
621 zl
.route
.type
= ZEBRA_ROUTE_OSPF
;
622 zl
.route
.instance
= 0;
624 /* Check that SRP contains at least one valid path */
625 if (srp
->route
== NULL
) {
629 osr_debug("SR (%s): Configure Prefix %pFX with",
630 __func__
, (struct prefix
*)&srp
->prefv4
);
632 for (ALL_LIST_ELEMENTS_RO(srp
->route
->paths
, node
, path
)) {
633 if (path
->srni
.label_out
== MPLS_INVALID_LABEL
)
636 if (zl
.nexthop_num
>= MULTIPATH_NUM
)
640 * TI-LFA backup path label stack comes first, if
643 if (path
->srni
.backup_label_stack
) {
644 znh_backup
= &zl
.backup_nexthops
645 [zl
.backup_nexthop_num
++];
646 znh_backup
->type
= NEXTHOP_TYPE_IPV4
;
647 znh_backup
->gate
.ipv4
=
648 path
->srni
.backup_nexthop
;
650 memcpy(znh_backup
->labels
,
651 path
->srni
.backup_label_stack
->label
,
653 * path
->srni
.backup_label_stack
656 znh_backup
->label_num
=
657 path
->srni
.backup_label_stack
659 if (path
->srni
.label_out
660 != MPLS_LABEL_IPV4_EXPLICIT_NULL
661 && path
->srni
.label_out
662 != MPLS_LABEL_IMPLICIT_NULL
)
664 [znh_backup
->label_num
++] =
665 path
->srni
.label_out
;
668 znh
= &zl
.nexthops
[zl
.nexthop_num
++];
669 znh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
670 znh
->gate
.ipv4
= path
->nexthop
;
671 znh
->ifindex
= path
->ifindex
;
673 znh
->labels
[0] = path
->srni
.label_out
;
675 osr_debug(" |- labels %u/%u", srp
->label_in
,
676 path
->srni
.label_out
);
678 /* Set TI-LFA backup nexthop info if present */
679 if (path
->srni
.backup_label_stack
) {
680 SET_FLAG(zl
.message
, ZAPI_LABELS_HAS_BACKUPS
);
682 ZAPI_NEXTHOP_FLAG_HAS_BACKUP
);
684 /* Just care about a single TI-LFA backup path
687 znh
->backup_idx
[0] = zl
.backup_nexthop_num
- 1;
696 /* Finally, send message to zebra. */
697 (void)zebra_send_mpls_labels(zclient
, ZEBRA_MPLS_LABELS_REPLACE
, &zl
);
700 /* Remove NHLFE for Prefix-SID */
701 void ospf_zebra_delete_prefix_sid(const struct sr_prefix
*srp
)
703 struct zapi_labels zl
;
705 osr_debug("SR (%s): Delete Labels %u for Prefix %pFX", __func__
,
706 srp
->label_in
, (struct prefix
*)&srp
->prefv4
);
708 /* Prepare message. */
709 memset(&zl
, 0, sizeof(zl
));
710 zl
.type
= ZEBRA_LSP_OSPF_SR
;
711 zl
.local_label
= srp
->label_in
;
713 if (srp
->type
== PREF_SID
) {
714 /* Update route in the RIB too */
715 SET_FLAG(zl
.message
, ZAPI_LABELS_FTN
);
716 zl
.route
.prefix
.u
.prefix4
= srp
->prefv4
.prefix
;
717 zl
.route
.prefix
.prefixlen
= srp
->prefv4
.prefixlen
;
718 zl
.route
.prefix
.family
= srp
->prefv4
.family
;
719 zl
.route
.type
= ZEBRA_ROUTE_OSPF
;
720 zl
.route
.instance
= 0;
723 /* Send message to zebra. */
724 (void)zebra_send_mpls_labels(zclient
, ZEBRA_MPLS_LABELS_DELETE
, &zl
);
727 /* Send MPLS Label entry to Zebra for installation or deletion */
728 void ospf_zebra_send_adjacency_sid(int cmd
, struct sr_nhlfe nhlfe
)
730 struct zapi_labels zl
;
731 struct zapi_nexthop
*znh
;
733 osr_debug("SR (%s): %s Labels %u/%u for Adjacency via %u", __func__
,
734 cmd
== ZEBRA_MPLS_LABELS_ADD
? "Add" : "Delete",
735 nhlfe
.label_in
, nhlfe
.label_out
, nhlfe
.ifindex
);
737 memset(&zl
, 0, sizeof(zl
));
738 zl
.type
= ZEBRA_LSP_OSPF_SR
;
739 zl
.local_label
= nhlfe
.label_in
;
741 znh
= &zl
.nexthops
[0];
742 znh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
743 znh
->gate
.ipv4
= nhlfe
.nexthop
;
744 znh
->ifindex
= nhlfe
.ifindex
;
746 znh
->labels
[0] = nhlfe
.label_out
;
748 (void)zebra_send_mpls_labels(zclient
, cmd
, &zl
);
751 struct ospf_redist
*ospf_redist_lookup(struct ospf
*ospf
, uint8_t type
,
752 unsigned short instance
)
754 struct list
*red_list
;
755 struct listnode
*node
;
756 struct ospf_redist
*red
;
758 red_list
= ospf
->redist
[type
];
762 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
763 if (red
->instance
== instance
)
769 struct ospf_redist
*ospf_redist_add(struct ospf
*ospf
, uint8_t type
,
770 unsigned short instance
)
772 struct list
*red_list
;
773 struct ospf_redist
*red
;
775 red
= ospf_redist_lookup(ospf
, type
, instance
);
779 if (!ospf
->redist
[type
])
780 ospf
->redist
[type
] = list_new();
782 red_list
= ospf
->redist
[type
];
783 red
= XCALLOC(MTYPE_OSPF_REDISTRIBUTE
, sizeof(struct ospf_redist
));
784 red
->instance
= instance
;
785 red
->dmetric
.type
= -1;
786 red
->dmetric
.value
= -1;
787 ROUTEMAP_NAME(red
) = NULL
;
788 ROUTEMAP(red
) = NULL
;
790 listnode_add(red_list
, red
);
795 void ospf_redist_del(struct ospf
*ospf
, uint8_t type
, unsigned short instance
)
797 struct ospf_redist
*red
;
799 red
= ospf_redist_lookup(ospf
, type
, instance
);
802 listnode_delete(ospf
->redist
[type
], red
);
803 if (!ospf
->redist
[type
]->count
) {
804 list_delete(&ospf
->redist
[type
]);
806 ospf_routemap_unset(red
);
807 XFREE(MTYPE_OSPF_REDISTRIBUTE
, red
);
812 int ospf_is_type_redistributed(struct ospf
*ospf
, int type
,
813 unsigned short instance
)
815 return (DEFAULT_ROUTE_TYPE(type
)
816 ? vrf_bitmap_check(zclient
->default_information
[AFI_IP
],
819 && redist_check_instance(
820 &zclient
->mi_redist
[AFI_IP
][type
],
824 zclient
->redist
[AFI_IP
][type
],
828 int ospf_redistribute_update(struct ospf
*ospf
, struct ospf_redist
*red
,
829 int type
, unsigned short instance
, int mtype
,
834 if (mtype
!= red
->dmetric
.type
) {
835 red
->dmetric
.type
= mtype
;
836 force
= LSA_REFRESH_FORCE
;
838 if (mvalue
!= red
->dmetric
.value
) {
839 red
->dmetric
.value
= mvalue
;
840 force
= LSA_REFRESH_FORCE
;
843 ospf_external_lsa_refresh_type(ospf
, type
, instance
, force
);
845 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
847 "Redistribute[%s][%d]: Refresh Type[%d], Metric[%d]",
848 ospf_redist_string(type
), instance
,
849 metric_type(ospf
, type
, instance
),
850 metric_value(ospf
, type
, instance
));
855 int ospf_redistribute_set(struct ospf
*ospf
, struct ospf_redist
*red
, int type
,
856 unsigned short instance
, int mtype
, int mvalue
)
858 red
->dmetric
.type
= mtype
;
859 red
->dmetric
.value
= mvalue
;
861 ospf_external_add(ospf
, type
, instance
);
863 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
, type
,
864 instance
, ospf
->vrf_id
);
866 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
868 "Redistribute[%s][%d] vrf id %u: Start Type[%d], Metric[%d]",
869 ospf_redist_string(type
), instance
, ospf
->vrf_id
,
870 metric_type(ospf
, type
, instance
),
871 metric_value(ospf
, type
, instance
));
873 ospf_asbr_status_update(ospf
, ++ospf
->redistribute
);
878 int ospf_redistribute_unset(struct ospf
*ospf
, int type
,
879 unsigned short instance
)
881 if (type
== zclient
->redist_default
&& instance
== zclient
->instance
)
884 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, AFI_IP
, type
,
885 instance
, ospf
->vrf_id
);
887 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
888 zlog_debug("Redistribute[%s][%d] vrf id %u: Stop",
889 ospf_redist_string(type
), instance
, ospf
->vrf_id
);
891 /* Remove the routes from OSPF table. */
892 ospf_redistribute_withdraw(ospf
, type
, instance
);
894 ospf_external_del(ospf
, type
, instance
);
896 ospf_asbr_status_update(ospf
, --ospf
->redistribute
);
901 int ospf_redistribute_default_set(struct ospf
*ospf
, int originate
, int mtype
,
904 struct prefix_ipv4 p
;
905 struct in_addr nexthop
;
906 int cur_originate
= ospf
->default_originate
;
907 const char *type_str
= NULL
;
909 nexthop
.s_addr
= INADDR_ANY
;
911 p
.prefix
.s_addr
= INADDR_ANY
;
914 ospf
->default_originate
= originate
;
916 if (cur_originate
== originate
) {
917 /* Refresh the lsa since metric might different */
918 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
920 "Redistribute[%s]: Refresh Type[%d], Metric[%d]",
921 ospf_redist_string(DEFAULT_ROUTE
),
922 metric_type(ospf
, DEFAULT_ROUTE
, 0),
923 metric_value(ospf
, DEFAULT_ROUTE
, 0));
925 ospf_external_lsa_refresh_default(ospf
);
929 switch (cur_originate
) {
930 case DEFAULT_ORIGINATE_NONE
:
932 case DEFAULT_ORIGINATE_ZEBRA
:
933 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
934 zclient
, AFI_IP
, ospf
->vrf_id
);
935 ospf
->redistribute
--;
937 case DEFAULT_ORIGINATE_ALWAYS
:
938 ospf_external_info_delete(ospf
, DEFAULT_ROUTE
, 0, p
);
939 ospf_external_del(ospf
, DEFAULT_ROUTE
, 0);
940 ospf
->redistribute
--;
945 case DEFAULT_ORIGINATE_NONE
:
948 case DEFAULT_ORIGINATE_ZEBRA
:
950 ospf
->redistribute
++;
951 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
952 zclient
, AFI_IP
, ospf
->vrf_id
);
954 case DEFAULT_ORIGINATE_ALWAYS
:
956 ospf
->redistribute
++;
957 ospf_external_add(ospf
, DEFAULT_ROUTE
, 0);
958 ospf_external_info_add(ospf
, DEFAULT_ROUTE
, 0, p
, 0, nexthop
,
963 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
964 zlog_debug("Redistribute[DEFAULT]: %s Type[%d], Metric[%d]",
966 metric_type(ospf
, DEFAULT_ROUTE
, 0),
967 metric_value(ospf
, DEFAULT_ROUTE
, 0));
969 ospf_external_lsa_refresh_default(ospf
);
970 ospf_asbr_status_update(ospf
, ospf
->redistribute
);
974 static int ospf_external_lsa_originate_check(struct ospf
*ospf
,
975 struct external_info
*ei
)
977 /* If prefix is multicast, then do not originate LSA. */
978 if (IN_MULTICAST(htonl(ei
->p
.prefix
.s_addr
))) {
980 "LSA[Type5:%pI4]: Not originate AS-external-LSA, Prefix belongs multicast",
985 /* Take care of default-originate. */
986 if (is_default_prefix4(&ei
->p
))
987 if (ospf
->default_originate
== DEFAULT_ORIGINATE_NONE
) {
989 "LSA[Type5:0.0.0.0]: Not originate AS-external-LSA for default");
996 /* If connected prefix is OSPF enable interface, then do not announce. */
997 int ospf_distribute_check_connected(struct ospf
*ospf
, struct external_info
*ei
)
999 struct listnode
*node
;
1000 struct ospf_interface
*oi
;
1003 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
1004 if (prefix_match(oi
->address
, (struct prefix
*)&ei
->p
))
1010 /* Apply default route-map on ei received. */
1011 int ospf_external_info_apply_default_routemap(struct ospf
*ospf
,
1012 struct external_info
*ei
,
1013 struct external_info
*default_ei
)
1015 struct ospf_redist
*red
;
1016 int type
= default_ei
->type
;
1017 struct prefix_ipv4
*p
= &ei
->p
;
1018 struct route_map_set_values save_values
;
1021 if (!ospf_external_lsa_originate_check(ospf
, default_ei
))
1024 save_values
= default_ei
->route_map_set
;
1025 ospf_reset_route_map_set_values(&default_ei
->route_map_set
);
1027 /* apply route-map if needed */
1028 red
= ospf_redist_lookup(ospf
, type
, ospf
->instance
);
1029 if (red
&& ROUTEMAP_NAME(red
)) {
1030 route_map_result_t ret
;
1032 ret
= route_map_apply(ROUTEMAP(red
), (struct prefix
*)p
, ei
);
1034 if (ret
== RMAP_DENYMATCH
) {
1035 ei
->route_map_set
= save_values
;
1045 * Default originated is based on route-map condition then
1046 * apply route-map on received external info. Originate or
1047 * flush based on route-map condition.
1049 static bool ospf_external_lsa_default_routemap_apply(struct ospf
*ospf
,
1050 struct external_info
*ei
,
1053 struct external_info
*default_ei
;
1054 struct prefix_ipv4 p
;
1055 struct ospf_lsa
*lsa
;
1060 p
.prefix
.s_addr
= INADDR_ANY
;
1063 /* Get the default extenal info. */
1064 default_ei
= ospf_external_info_lookup(ospf
, DEFAULT_ROUTE
,
1065 ospf
->instance
, &p
);
1067 /* Nothing to be done here. */
1071 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1072 zlog_debug("Apply default originate routemap on ei: %pI4 cmd: %d",
1073 &ei
->p
.prefix
, cmd
);
1075 ret
= ospf_external_info_apply_default_routemap(ospf
, ei
, default_ei
);
1077 /* If deny then nothing to be done both in add and del case. */
1079 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1080 zlog_debug("Default originte routemap deny for ei: %pI4",
1085 /* Get the default LSA. */
1086 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
1088 /* If this is add route and permit then ooriginate default. */
1089 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
) {
1090 /* If permit and default already advertise then return. */
1091 if (lsa
&& !IS_LSA_MAXAGE(lsa
)) {
1092 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1093 zlog_debug("Default lsa already originated");
1097 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1098 zlog_debug("Originating/Refreshing default lsa");
1100 if (lsa
&& IS_LSA_MAXAGE(lsa
))
1102 ospf_external_lsa_refresh(ospf
, lsa
, default_ei
, true,
1105 /* If permit and default not advertised then advertise.
1107 ospf_external_lsa_originate(ospf
, default_ei
);
1109 } else if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_DEL
) {
1110 /* If deny and lsa is not originated then nothing to be done.*/
1112 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1114 "Default lsa not originated, not flushing");
1118 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1120 "Running default route-map again as ei: %pI4 deleted",
1123 * if this route delete was permitted then we need to check
1124 * there are any other external info which can still trigger
1125 * default route origination else flush it.
1127 thread_add_event(master
,
1128 ospf_external_lsa_default_routemap_timer
, ospf
,
1129 0, &ospf
->t_default_routemap_timer
);
1135 /* return 1 if external LSA must be originated, 0 otherwise */
1136 int ospf_redistribute_check(struct ospf
*ospf
, struct external_info
*ei
,
1139 struct route_map_set_values save_values
;
1140 struct prefix_ipv4
*p
= &ei
->p
;
1141 struct ospf_redist
*red
;
1142 uint8_t type
= is_default_prefix4(&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
1143 unsigned short instance
= is_default_prefix4(&ei
->p
) ? 0 : ei
->instance
;
1144 route_tag_t saved_tag
= 0;
1146 /* Default is handled differently. */
1147 if (type
== DEFAULT_ROUTE
)
1153 if (!ospf_external_lsa_originate_check(ospf
, ei
))
1156 /* Take care connected route. */
1157 if (type
== ZEBRA_ROUTE_CONNECT
1158 && !ospf_distribute_check_connected(ospf
, ei
))
1161 if (!DEFAULT_ROUTE_TYPE(type
) && DISTRIBUTE_NAME(ospf
, type
))
1162 /* distirbute-list exists, but access-list may not? */
1163 if (DISTRIBUTE_LIST(ospf
, type
))
1164 if (access_list_apply(DISTRIBUTE_LIST(ospf
, type
), p
)
1166 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
1168 "Redistribute[%s]: %pFX filtered by distribute-list.",
1169 ospf_redist_string(type
), p
);
1173 save_values
= ei
->route_map_set
;
1174 ospf_reset_route_map_set_values(&ei
->route_map_set
);
1176 saved_tag
= ei
->tag
;
1177 /* Resetting with original route tag */
1178 ei
->tag
= ei
->orig_tag
;
1180 /* apply route-map if needed */
1181 red
= ospf_redist_lookup(ospf
, type
, instance
);
1182 if (red
&& ROUTEMAP_NAME(red
)) {
1183 route_map_result_t ret
;
1185 ret
= route_map_apply(ROUTEMAP(red
), (struct prefix
*)p
, ei
);
1187 if (ret
== RMAP_DENYMATCH
) {
1188 ei
->route_map_set
= save_values
;
1189 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
1191 "Redistribute[%s]: %pFX filtered by route-map.",
1192 ospf_redist_string(type
), p
);
1196 /* check if 'route-map set' changed something */
1198 *changed
= !ospf_route_map_set_compare(
1199 &ei
->route_map_set
, &save_values
);
1201 /* check if tag is modified */
1202 *changed
|= (saved_tag
!= ei
->tag
);
1209 /* OSPF route-map set for redistribution */
1210 void ospf_routemap_set(struct ospf_redist
*red
, const char *name
)
1212 if (ROUTEMAP_NAME(red
)) {
1213 route_map_counter_decrement(ROUTEMAP(red
));
1214 free(ROUTEMAP_NAME(red
));
1217 ROUTEMAP_NAME(red
) = strdup(name
);
1218 ROUTEMAP(red
) = route_map_lookup_by_name(name
);
1219 route_map_counter_increment(ROUTEMAP(red
));
1222 void ospf_routemap_unset(struct ospf_redist
*red
)
1224 if (ROUTEMAP_NAME(red
)) {
1225 route_map_counter_decrement(ROUTEMAP(red
));
1226 free(ROUTEMAP_NAME(red
));
1229 ROUTEMAP_NAME(red
) = NULL
;
1230 ROUTEMAP(red
) = NULL
;
1233 static int ospf_zebra_gr_update(struct ospf
*ospf
, int command
,
1234 uint32_t stale_time
)
1236 struct zapi_cap api
;
1238 if (!zclient
|| zclient
->sock
< 0 || !ospf
)
1241 memset(&api
, 0, sizeof(api
));
1243 api
.stale_removal_time
= stale_time
;
1244 api
.vrf_id
= ospf
->vrf_id
;
1246 (void)zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
,
1252 int ospf_zebra_gr_enable(struct ospf
*ospf
, uint32_t stale_time
)
1254 return ospf_zebra_gr_update(ospf
, ZEBRA_CLIENT_GR_CAPABILITIES
,
1258 int ospf_zebra_gr_disable(struct ospf
*ospf
)
1260 return ospf_zebra_gr_update(ospf
, ZEBRA_CLIENT_GR_DISABLE
, 0);
1263 /* Zebra route add and delete treatment. */
1264 static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS
)
1266 struct zapi_route api
;
1267 struct prefix_ipv4 p
;
1269 unsigned long ifindex
;
1270 struct in_addr nexthop
;
1271 struct external_info
*ei
;
1276 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
1280 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
1283 ifindex
= api
.nexthops
[0].ifindex
;
1284 nexthop
= api
.nexthops
[0].gate
.ipv4
;
1287 memcpy(&p
, &api
.prefix
, sizeof(p
));
1288 if (IPV4_NET127(ntohl(p
.prefix
.s_addr
)))
1291 pgen
.family
= p
.family
;
1292 pgen
.prefixlen
= p
.prefixlen
;
1293 pgen
.u
.prefix4
= p
.prefix
;
1295 /* Re-destributed route is default route.
1296 * Here, route type is used as 'ZEBRA_ROUTE_KERNEL' for
1297 * updating ex-info. But in resetting (no default-info
1298 * originate)ZEBRA_ROUTE_MAX is used to delete the ex-info.
1299 * Resolved this inconsistency by maintaining same route type.
1301 if ((is_default_prefix(&pgen
)) && (api
.type
!= ZEBRA_ROUTE_OSPF
))
1302 rt_type
= DEFAULT_ROUTE
;
1304 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
1305 zlog_debug("%s: cmd %s from client %s: vrf_id %d, p %pFX",
1306 __func__
, zserv_command_string(cmd
),
1307 zebra_route_string(api
.type
), vrf_id
, &api
.prefix
);
1309 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
) {
1310 /* XXX|HACK|TODO|FIXME:
1311 * Maybe we should ignore reject/blackhole routes? Testing
1312 * shows that there is no problems though and this is only way
1313 * to "summarize" routes in ASBR at the moment. Maybe we need
1314 * just a better generalised solution for these types?
1317 /* Protocol tag overwrites all other tag value sent by zebra */
1318 if (ospf
->dtag
[rt_type
] > 0)
1319 api
.tag
= ospf
->dtag
[rt_type
];
1322 * Given zebra sends update for a prefix via ADD message, it
1324 * be considered as an implicit DEL for that prefix with other
1328 for (i
= 0; i
<= ZEBRA_ROUTE_MAX
; i
++)
1330 ospf_external_info_delete(ospf
, i
, api
.instance
,
1333 ei
= ospf_external_info_add(ospf
, rt_type
, api
.instance
, p
,
1334 ifindex
, nexthop
, api
.tag
);
1336 /* Nothing has changed, so nothing to do; return */
1339 if (ospf
->router_id
.s_addr
!= INADDR_ANY
) {
1340 if (is_default_prefix4(&p
))
1341 ospf_external_lsa_refresh_default(ospf
);
1343 struct ospf_external_aggr_rt
*aggr
;
1344 struct as_external_lsa
*al
;
1345 struct ospf_lsa
*lsa
= NULL
;
1346 struct in_addr mask
;
1348 aggr
= ospf_external_aggr_match(ospf
, &ei
->p
);
1351 /* Check the AS-external-LSA
1352 * should be originated.
1354 if (!ospf_redistribute_check(ospf
, ei
,
1358 if (IS_DEBUG_OSPF(lsa
, EXTNL_LSA_AGGR
))
1360 "%s: Send Aggreate LSA (%pI4/%d)",
1365 ospf_originate_summary_lsa(ospf
, aggr
,
1368 /* Handling the case where the
1369 * external route prefix
1370 * and aggegate prefix is same
1371 * If same don't flush the
1376 (struct prefix
*)&aggr
->p
,
1377 (struct prefix
*)&ei
->p
))
1380 lsa
= ospf_external_info_find_lsa(
1384 al
= (struct as_external_lsa
*)
1386 masklen2ip(ei
->p
.prefixlen
,
1393 ospf_external_lsa_flush(
1394 ospf
, ei
->type
, &ei
->p
,
1398 struct ospf_lsa
*current
;
1400 current
= ospf_external_info_find_lsa(
1408 if (!ospf_redistribute_check(
1412 ospf_external_lsa_originate(
1417 ZEBRA_REDISTRIBUTE
))
1419 "%s: %pI4 refreshing LSA",
1422 ospf_external_lsa_refresh(
1432 * Check if default-information originate is
1433 * with some routemap prefix/access list match.
1435 ospf_external_lsa_default_routemap_apply(ospf
, ei
, cmd
);
1437 } else { /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
1438 struct ospf_external_aggr_rt
*aggr
;
1440 ei
= ospf_external_info_lookup(ospf
, rt_type
, api
.instance
, &p
);
1445 * Check if default-information originate i
1446 * with some routemap prefix/access list match.
1447 * Apply before ei is deleted.
1449 ospf_external_lsa_default_routemap_apply(ospf
, ei
, cmd
);
1451 aggr
= ospf_external_aggr_match(ospf
, &ei
->p
);
1453 if (aggr
&& (ei
->aggr_route
== aggr
)) {
1454 ospf_unlink_ei_from_aggr(ospf
, aggr
, ei
);
1456 ospf_external_info_delete(ospf
, rt_type
, api
.instance
,
1459 ospf_external_info_delete(ospf
, rt_type
, api
.instance
,
1462 if (is_default_prefix4(&p
))
1463 ospf_external_lsa_refresh_default(ospf
);
1465 ospf_external_lsa_flush(ospf
, rt_type
, &p
,
1466 ifindex
/*, nexthop */);
1474 int ospf_distribute_list_out_set(struct ospf
*ospf
, int type
, const char *name
)
1476 /* Lookup access-list for distribute-list. */
1477 DISTRIBUTE_LIST(ospf
, type
) = access_list_lookup(AFI_IP
, name
);
1479 /* Clear previous distribute-name. */
1480 if (DISTRIBUTE_NAME(ospf
, type
))
1481 free(DISTRIBUTE_NAME(ospf
, type
));
1483 /* Set distribute-name. */
1484 DISTRIBUTE_NAME(ospf
, type
) = strdup(name
);
1486 /* If access-list have been set, schedule update timer. */
1487 if (DISTRIBUTE_LIST(ospf
, type
))
1488 ospf_distribute_list_update(ospf
, type
, 0);
1493 int ospf_distribute_list_out_unset(struct ospf
*ospf
, int type
,
1496 /* Schedule update timer. */
1497 if (DISTRIBUTE_LIST(ospf
, type
))
1498 ospf_distribute_list_update(ospf
, type
, 0);
1500 /* Unset distribute-list. */
1501 DISTRIBUTE_LIST(ospf
, type
) = NULL
;
1503 /* Clear distribute-name. */
1504 if (DISTRIBUTE_NAME(ospf
, type
))
1505 free(DISTRIBUTE_NAME(ospf
, type
));
1507 DISTRIBUTE_NAME(ospf
, type
) = NULL
;
1512 /* distribute-list update timer. */
1513 static void ospf_distribute_list_update_timer(struct event
*thread
)
1515 struct route_node
*rn
;
1516 struct external_info
*ei
;
1517 struct route_table
*rt
;
1518 struct ospf_lsa
*lsa
;
1519 int type
, default_refresh
= 0;
1520 struct ospf
*ospf
= THREAD_ARG(thread
);
1525 ospf
->t_distribute_update
= NULL
;
1527 zlog_info("Zebra[Redistribute]: distribute-list update timer fired!");
1529 if (IS_DEBUG_OSPF_EVENT
) {
1530 zlog_debug("%s: ospf distribute-list update vrf %s id %d",
1531 __func__
, ospf_vrf_id_to_name(ospf
->vrf_id
),
1535 /* foreach all external info. */
1536 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
1537 struct list
*ext_list
;
1538 struct listnode
*node
;
1539 struct ospf_external
*ext
;
1541 ext_list
= ospf
->external
[type
];
1545 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
1546 rt
= ext
->external_info
;
1549 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
1554 if (is_default_prefix4(&ei
->p
))
1555 default_refresh
= 1;
1557 struct ospf_external_aggr_rt
*aggr
;
1559 aggr
= ospf_external_aggr_match(ospf
,
1564 * should be originated.
1566 if (!ospf_redistribute_check(
1569 ospf_unlink_ei_from_aggr(
1578 "%s: Send Aggregate LSA (%pI4/%d)",
1583 /* Originate Aggregate
1586 ospf_originate_summary_lsa(
1589 (lsa
= ospf_external_info_find_lsa(
1592 LSA_REFRESH_IF_CHANGED
;
1593 /* If this is a MaxAge
1596 * because distribute
1597 * settings might have
1599 * this LSA needs to be
1600 * originated, not be
1603 * refresh it, it will
1604 * remain a MaxAge LSA
1605 * because it will look
1607 * changed. Neighbors
1609 * updates for this LSA.
1611 if (IS_LSA_MAXAGE(lsa
))
1612 force
= LSA_REFRESH_FORCE
;
1614 ospf_external_lsa_refresh(
1615 ospf
, lsa
, ei
, force
,
1618 if (!ospf_redistribute_check(
1621 ospf_external_lsa_originate(
1628 if (default_refresh
)
1629 ospf_external_lsa_refresh_default(ospf
);
1632 /* Update distribute-list and set timer to apply access-list. */
1633 void ospf_distribute_list_update(struct ospf
*ospf
, int type
,
1634 unsigned short instance
)
1636 struct ospf_external
*ext
;
1638 /* External info does not exist. */
1639 ext
= ospf_external_lookup(ospf
, type
, instance
);
1640 if (!ext
|| !EXTERNAL_INFO(ext
))
1643 /* Set timer. If timer is already started, this call does nothing. */
1644 thread_add_timer_msec(master
, ospf_distribute_list_update_timer
, ospf
,
1645 ospf
->min_ls_interval
,
1646 &ospf
->t_distribute_update
);
1649 /* If access-list is updated, apply some check. */
1650 static void ospf_filter_update(struct access_list
*access
)
1655 struct ospf_area
*area
;
1656 struct listnode
*node
, *n1
;
1658 /* If OSPF instance does not exist, return right now. */
1659 if (listcount(om
->ospf
) == 0)
1662 /* Iterate all ospf [VRF] instances */
1663 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
1664 /* Update distribute-list, and apply filter. */
1665 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
1666 struct list
*red_list
;
1667 struct ospf_redist
*red
;
1669 red_list
= ospf
->redist
[type
];
1671 for (ALL_LIST_ELEMENTS_RO(red_list
, node
,
1673 if (ROUTEMAP(red
)) {
1674 /* if route-map is not NULL it
1676 * using this access list */
1677 ospf_distribute_list_update(
1683 /* There is place for route-map for default-information
1684 * (ZEBRA_ROUTE_MAX),
1685 * but no distribute list. */
1686 if (type
== ZEBRA_ROUTE_MAX
)
1689 if (DISTRIBUTE_NAME(ospf
, type
)) {
1690 /* Keep old access-list for distribute-list. */
1691 struct access_list
*old
=
1692 DISTRIBUTE_LIST(ospf
, type
);
1694 /* Update access-list for distribute-list. */
1695 DISTRIBUTE_LIST(ospf
, type
) =
1698 DISTRIBUTE_NAME(ospf
, type
));
1700 /* No update for this distribute type. */
1702 && DISTRIBUTE_LIST(ospf
, type
) == NULL
)
1705 /* Schedule distribute-list update timer. */
1706 if (DISTRIBUTE_LIST(ospf
, type
) == NULL
1707 || strcmp(DISTRIBUTE_NAME(ospf
, type
),
1710 ospf_distribute_list_update(ospf
, type
,
1715 /* Update Area access-list. */
1716 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, node
, area
)) {
1717 if (EXPORT_NAME(area
)) {
1718 EXPORT_LIST(area
) = NULL
;
1722 if (IMPORT_NAME(area
)) {
1723 IMPORT_LIST(area
) = NULL
;
1728 /* Schedule ABR tasks -- this will be changed -- takada. */
1729 if (IS_OSPF_ABR(ospf
) && abr_inv
)
1730 ospf_schedule_abr_task(ospf
);
1734 /* If prefix-list is updated, do some updates. */
1735 static void ospf_prefix_list_update(struct prefix_list
*plist
)
1737 struct ospf
*ospf
= NULL
;
1740 struct ospf_area
*area
;
1741 struct listnode
*node
, *n1
;
1743 /* If OSPF instatnce does not exist, return right now. */
1744 if (listcount(om
->ospf
) == 0)
1747 /* Iterate all ospf [VRF] instances */
1748 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
1750 /* Update all route-maps which are used
1751 * as redistribution filters.
1752 * They might use prefix-list.
1754 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
1755 struct list
*red_list
;
1756 struct ospf_redist
*red
;
1758 red_list
= ospf
->redist
[type
];
1762 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
1763 if (ROUTEMAP(red
)) {
1764 /* if route-map is not NULL
1766 * this prefix list */
1767 ospf_distribute_list_update(
1768 ospf
, type
, red
->instance
);
1773 /* Update area filter-lists. */
1774 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, node
, area
)) {
1775 /* Update filter-list in. */
1776 if (PREFIX_NAME_IN(area
)
1777 && strcmp(PREFIX_NAME_IN(area
),
1778 prefix_list_name(plist
))
1780 PREFIX_LIST_IN(area
) = prefix_list_lookup(
1781 AFI_IP
, PREFIX_NAME_IN(area
));
1785 /* Update filter-list out. */
1786 if (PREFIX_NAME_OUT(area
)
1787 && strcmp(PREFIX_NAME_OUT(area
),
1788 prefix_list_name(plist
))
1790 PREFIX_LIST_OUT(area
) = prefix_list_lookup(
1791 AFI_IP
, PREFIX_NAME_OUT(area
));
1796 /* Schedule ABR task. */
1797 if (IS_OSPF_ABR(ospf
) && abr_inv
)
1798 ospf_schedule_abr_task(ospf
);
1802 static struct ospf_distance
*ospf_distance_new(void)
1804 return XCALLOC(MTYPE_OSPF_DISTANCE
, sizeof(struct ospf_distance
));
1807 static void ospf_distance_free(struct ospf_distance
*odistance
)
1809 XFREE(MTYPE_OSPF_DISTANCE
, odistance
);
1812 int ospf_distance_set(struct vty
*vty
, struct ospf
*ospf
,
1813 const char *distance_str
, const char *ip_str
,
1814 const char *access_list_str
)
1817 struct prefix_ipv4 p
;
1819 struct route_node
*rn
;
1820 struct ospf_distance
*odistance
;
1822 ret
= str2prefix_ipv4(ip_str
, &p
);
1824 vty_out(vty
, "Malformed prefix\n");
1825 return CMD_WARNING_CONFIG_FAILED
;
1828 distance
= atoi(distance_str
);
1830 /* Get OSPF distance node. */
1831 rn
= route_node_get(ospf
->distance_table
, (struct prefix
*)&p
);
1833 odistance
= rn
->info
;
1834 route_unlock_node(rn
);
1836 odistance
= ospf_distance_new();
1837 rn
->info
= odistance
;
1840 /* Set distance value. */
1841 odistance
->distance
= distance
;
1843 /* Reset access-list configuration. */
1844 if (odistance
->access_list
) {
1845 free(odistance
->access_list
);
1846 odistance
->access_list
= NULL
;
1848 if (access_list_str
)
1849 odistance
->access_list
= strdup(access_list_str
);
1854 int ospf_distance_unset(struct vty
*vty
, struct ospf
*ospf
,
1855 const char *distance_str
, const char *ip_str
,
1856 char const *access_list_str
)
1859 struct prefix_ipv4 p
;
1860 struct route_node
*rn
;
1861 struct ospf_distance
*odistance
;
1863 ret
= str2prefix_ipv4(ip_str
, &p
);
1865 vty_out(vty
, "Malformed prefix\n");
1866 return CMD_WARNING_CONFIG_FAILED
;
1869 rn
= route_node_lookup(ospf
->distance_table
, (struct prefix
*)&p
);
1871 vty_out(vty
, "Can't find specified prefix\n");
1872 return CMD_WARNING_CONFIG_FAILED
;
1875 odistance
= rn
->info
;
1877 if (odistance
->access_list
)
1878 free(odistance
->access_list
);
1879 ospf_distance_free(odistance
);
1882 route_unlock_node(rn
);
1883 route_unlock_node(rn
);
1888 void ospf_distance_reset(struct ospf
*ospf
)
1890 struct route_node
*rn
;
1891 struct ospf_distance
*odistance
;
1893 for (rn
= route_top(ospf
->distance_table
); rn
; rn
= route_next(rn
)) {
1894 odistance
= rn
->info
;
1898 if (odistance
->access_list
)
1899 free(odistance
->access_list
);
1900 ospf_distance_free(odistance
);
1902 route_unlock_node(rn
);
1906 uint8_t ospf_distance_apply(struct ospf
*ospf
, struct prefix_ipv4
*p
,
1907 struct ospf_route
* or)
1913 if (ospf
->distance_intra
&& or->path_type
== OSPF_PATH_INTRA_AREA
)
1914 return ospf
->distance_intra
;
1916 if (ospf
->distance_inter
&& or->path_type
== OSPF_PATH_INTER_AREA
)
1917 return ospf
->distance_inter
;
1919 if (ospf
->distance_external
1920 && (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
||
1921 or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
))
1922 return ospf
->distance_external
;
1924 if (ospf
->distance_all
)
1925 return ospf
->distance_all
;
1930 void ospf_zebra_vrf_register(struct ospf
*ospf
)
1932 if (!zclient
|| zclient
->sock
< 0 || !ospf
)
1935 if (ospf
->vrf_id
!= VRF_UNKNOWN
) {
1936 if (IS_DEBUG_OSPF_EVENT
)
1937 zlog_debug("%s: Register VRF %s id %u", __func__
,
1938 ospf_vrf_id_to_name(ospf
->vrf_id
),
1940 zclient_send_reg_requests(zclient
, ospf
->vrf_id
);
1944 void ospf_zebra_vrf_deregister(struct ospf
*ospf
)
1946 if (!zclient
|| zclient
->sock
< 0 || !ospf
)
1949 if (ospf
->vrf_id
!= VRF_DEFAULT
&& ospf
->vrf_id
!= VRF_UNKNOWN
) {
1950 if (IS_DEBUG_OSPF_EVENT
)
1951 zlog_debug("%s: De-Register VRF %s id %u to Zebra.",
1952 __func__
, ospf_vrf_id_to_name(ospf
->vrf_id
),
1954 /* Deregister for router-id, interfaces,
1955 * redistributed routes. */
1956 zclient_send_dereg_requests(zclient
, ospf
->vrf_id
);
1960 /* Label Manager Functions */
1963 * Check if Label Manager is Ready or not.
1965 * @return True if Label Manager is ready, False otherwise
1967 bool ospf_zebra_label_manager_ready(void)
1969 return (zclient_sync
->sock
> 0);
1973 * Request Label Range to the Label Manager.
1975 * @param base base label of the label range to request
1976 * @param chunk_size size of the label range to request
1978 * @return 0 on success, -1 on failure
1980 int ospf_zebra_request_label_range(uint32_t base
, uint32_t chunk_size
)
1983 uint32_t start
, end
;
1985 if (zclient_sync
->sock
< 0)
1988 ret
= lm_get_label_chunk(zclient_sync
, 0, base
, chunk_size
, &start
,
1991 zlog_warn("%s: error getting label range!", __func__
);
1999 * Release Label Range to the Label Manager.
2001 * @param start start of label range to release
2002 * @param end end of label range to release
2004 * @return 0 on success, -1 otherwise
2006 int ospf_zebra_release_label_range(uint32_t start
, uint32_t end
)
2010 if (zclient_sync
->sock
< 0)
2013 ret
= lm_release_label_chunk(zclient_sync
, start
, end
);
2015 zlog_warn("%s: error releasing label range!", __func__
);
2023 * Connect to the Label Manager.
2025 * @return 0 on success, -1 otherwise
2027 int ospf_zebra_label_manager_connect(void)
2029 /* Connect to label manager. */
2030 if (zclient_socket_connect(zclient_sync
) < 0) {
2031 zlog_warn("%s: failed connecting synchronous zclient!",
2035 /* make socket non-blocking */
2036 set_nonblocking(zclient_sync
->sock
);
2038 /* Send hello to notify zebra this is a synchronous client */
2039 if (zclient_send_hello(zclient_sync
) == ZCLIENT_SEND_FAILURE
) {
2040 zlog_warn("%s: failed sending hello for synchronous zclient!",
2042 close(zclient_sync
->sock
);
2043 zclient_sync
->sock
= -1;
2047 /* Connect to label manager */
2048 if (lm_label_manager_connect(zclient_sync
, 0) != 0) {
2049 zlog_warn("%s: failed connecting to label manager!", __func__
);
2050 if (zclient_sync
->sock
> 0) {
2051 close(zclient_sync
->sock
);
2052 zclient_sync
->sock
= -1;
2057 osr_debug("SR (%s): Successfully connected to the Label Manager",
2063 static void ospf_zebra_connected(struct zclient
*zclient
)
2065 /* Send the client registration */
2066 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
2068 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
2072 * opaque messages between processes
2074 static int ospf_opaque_msg_handler(ZAPI_CALLBACK_ARGS
)
2077 struct zapi_opaque_msg info
;
2078 struct ldp_igp_sync_if_state state
;
2079 struct ldp_igp_sync_announce announce
;
2080 struct zapi_opaque_reg_info dst
;
2085 if (zclient_opaque_decode(s
, &info
) != 0)
2088 switch (info
.type
) {
2089 case LINK_STATE_SYNC
:
2090 STREAM_GETC(s
, dst
.proto
);
2091 STREAM_GETW(s
, dst
.instance
);
2092 STREAM_GETL(s
, dst
.session_id
);
2093 dst
.type
= LINK_STATE_SYNC
;
2094 ret
= ospf_te_sync_ted(dst
);
2096 case LDP_IGP_SYNC_IF_STATE_UPDATE
:
2097 STREAM_GET(&state
, s
, sizeof(state
));
2098 ret
= ospf_ldp_sync_state_update(state
);
2100 case LDP_IGP_SYNC_ANNOUNCE_UPDATE
:
2101 STREAM_GET(&announce
, s
, sizeof(announce
));
2102 ret
= ospf_ldp_sync_announce_update(announce
);
2113 static int ospf_zebra_client_close_notify(ZAPI_CALLBACK_ARGS
)
2117 struct zapi_client_close_info info
;
2119 if (zapi_client_close_notify_decode(zclient
->ibuf
, &info
) < 0)
2122 ospf_ldp_sync_handle_client_close(&info
);
2127 static zclient_handler
*const ospf_handlers
[] = {
2128 [ZEBRA_ROUTER_ID_UPDATE
] = ospf_router_id_update_zebra
,
2129 [ZEBRA_INTERFACE_ADDRESS_ADD
] = ospf_interface_address_add
,
2130 [ZEBRA_INTERFACE_ADDRESS_DELETE
] = ospf_interface_address_delete
,
2131 [ZEBRA_INTERFACE_LINK_PARAMS
] = ospf_interface_link_params
,
2132 [ZEBRA_INTERFACE_VRF_UPDATE
] = ospf_interface_vrf_update
,
2134 [ZEBRA_REDISTRIBUTE_ROUTE_ADD
] = ospf_zebra_read_route
,
2135 [ZEBRA_REDISTRIBUTE_ROUTE_DEL
] = ospf_zebra_read_route
,
2137 [ZEBRA_OPAQUE_MESSAGE
] = ospf_opaque_msg_handler
,
2139 [ZEBRA_CLIENT_CLOSE_NOTIFY
] = ospf_zebra_client_close_notify
,
2142 void ospf_zebra_init(struct thread_master
*master
, unsigned short instance
)
2144 /* Allocate zebra structure. */
2145 zclient
= zclient_new(master
, &zclient_options_default
, ospf_handlers
,
2146 array_size(ospf_handlers
));
2147 zclient_init(zclient
, ZEBRA_ROUTE_OSPF
, instance
, &ospfd_privs
);
2148 zclient
->zebra_connected
= ospf_zebra_connected
;
2150 /* Initialize special zclient for synchronous message exchanges. */
2151 struct zclient_options options
= zclient_options_default
;
2152 options
.synchronous
= true;
2153 zclient_sync
= zclient_new(master
, &options
, NULL
, 0);
2154 zclient_sync
->sock
= -1;
2155 zclient_sync
->redist_default
= ZEBRA_ROUTE_OSPF
;
2156 zclient_sync
->instance
= instance
;
2158 * session_id must be different from default value (0) to distinguish
2159 * the asynchronous socket from the synchronous one
2161 zclient_sync
->session_id
= 1;
2162 zclient_sync
->privs
= &ospfd_privs
;
2164 access_list_add_hook(ospf_filter_update
);
2165 access_list_delete_hook(ospf_filter_update
);
2166 prefix_list_add_hook(ospf_prefix_list_update
);
2167 prefix_list_delete_hook(ospf_prefix_list_update
);
2170 void ospf_zebra_send_arp(const struct interface
*ifp
, const struct prefix
*p
)
2172 zclient_send_neigh_discovery_req(zclient
, ifp
, p
);