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"
23 #include "lib/lib_errors.h"
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"
43 DEFINE_MTYPE_STATIC(OSPFD
, OSPF_EXTERNAL
, "OSPF External route table");
44 DEFINE_MTYPE_STATIC(OSPFD
, OSPF_REDISTRIBUTE
, "OSPF Redistriute");
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
;
52 /* For registering threads. */
53 extern struct event_loop
*master
;
55 /* Router-id update message from zebra. */
56 static int ospf_router_id_update_zebra(ZAPI_CALLBACK_ARGS
)
58 struct ospf
*ospf
= NULL
;
59 struct prefix router_id
;
60 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
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
);
66 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
69 ospf
->router_id_zebra
= router_id
.u
.prefix4
;
70 ospf_router_id_update(ospf
);
72 if (IS_DEBUG_OSPF_EVENT
)
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
,
81 static int ospf_interface_address_add(ZAPI_CALLBACK_ARGS
)
84 struct ospf
*ospf
= NULL
;
87 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
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
);
97 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
101 ospf_if_update(ospf
, c
->ifp
);
103 ospf_if_interface(c
->ifp
);
108 static int ospf_interface_address_delete(ZAPI_CALLBACK_ARGS
)
111 struct interface
*ifp
;
112 struct ospf_interface
*oi
;
113 struct route_node
*rn
;
116 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
121 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
122 zlog_debug("Zebra: interface %s address delete %pFX",
123 c
->ifp
->name
, c
->address
);
127 p
.prefixlen
= IPV4_MAX_BITLEN
;
129 rn
= route_node_lookup(IF_OIFS(ifp
), &p
);
137 route_unlock_node(rn
);
139 /* Call interface hook functions to clean up */
142 ospf_if_interface(c
->ifp
);
149 static int ospf_interface_link_params(ZAPI_CALLBACK_ARGS
)
151 struct interface
*ifp
;
152 bool changed
= false;
154 ifp
= zebra_interface_link_params_read(zclient
->ibuf
, vrf_id
, &changed
);
156 if (ifp
== NULL
|| !changed
)
160 ospf_mpls_te_update_if(ifp
);
165 /* VRF update for an interface. */
166 static int ospf_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
168 struct interface
*ifp
= NULL
;
171 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
176 if (IS_DEBUG_OSPF_EVENT
)
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
);
182 /*if_update(ifp, ifp->name, strlen(ifp->name), new_vrf_id);*/
183 if_update_to_new_vrf(ifp
, new_vrf_id
);
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
)
192 struct zapi_nexthop
*api_nh
;
193 struct zapi_nexthop
*api_nh_backup
;
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
;
200 api_nh_backup
->type
= NEXTHOP_TYPE_IPV4
;
201 api_nh_backup
->gate
.ipv4
= path
->srni
.backup_nexthop
;
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
);
209 api
->backup_nexthop_num
++;
212 /* And here comes the primary nexthop */
213 api_nh
= &api
->nexthops
[api
->nexthop_num
];
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
;
227 api_nh
->ifindex
= path
->ifindex
;
228 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
230 api_nh
->vrf_id
= ospf
->vrf_id
;
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
);
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;
245 static void ospf_zebra_append_opaque_attr(struct ospf_route
*or,
246 struct zapi_route
*api
)
248 struct ospf_zebra_opaque ospf_opaque
= {};
251 snprintf(ospf_opaque
.path_type
, sizeof(ospf_opaque
.path_type
), "%s",
252 ospf_path_type_name(or->path_type
));
254 switch (or->path_type
) {
255 case OSPF_PATH_INTRA_AREA
:
256 case OSPF_PATH_INTER_AREA
:
258 (void)inet_ntop(AF_INET
, &or->u
.std
.area_id
,
260 sizeof(ospf_opaque
.area_id
));
262 case OSPF_PATH_TYPE1_EXTERNAL
:
263 case OSPF_PATH_TYPE2_EXTERNAL
:
265 snprintf(ospf_opaque
.tag
, sizeof(ospf_opaque
.tag
), "%u",
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
);
277 void ospf_zebra_add(struct ospf
*ospf
, struct prefix_ipv4
*p
,
278 struct ospf_route
* or)
280 struct zapi_route api
;
282 struct ospf_path
*path
;
283 struct listnode
*node
;
285 if (ospf
->gr_info
.restart_in_progress
) {
286 if (IS_DEBUG_OSPF_GR
)
288 "Zebra: Graceful Restart in progress -- not installing %pFX",
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
;
299 memcpy(&api
.prefix
, p
, sizeof(*p
));
300 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
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
;
309 api
.metric
= or->cost
;
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
;
319 /* Distance value. */
320 distance
= ospf_distance_apply(ospf
, p
, or);
322 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
323 api
.distance
= distance
;
326 for (ALL_LIST_ELEMENTS_RO(or->paths
, node
, path
)) {
327 if (api
.nexthop_num
>= ospf
->max_multipath
)
330 ospf_zebra_add_nexthop(ospf
, path
, &api
);
332 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
)) {
333 struct interface
*ifp
;
335 ifp
= if_lookup_by_index(path
->ifindex
, ospf
->vrf_id
);
338 "Zebra: Route add %pFX nexthop %pI4, ifindex=%d %s",
339 p
, &path
->nexthop
, path
->ifindex
,
340 ifp
? ifp
->name
: " ");
344 if (CHECK_FLAG(ospf
->config
, OSPF_SEND_EXTRA_DATA_TO_ZEBRA
))
345 ospf_zebra_append_opaque_attr(or, &api
);
347 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
350 void ospf_zebra_delete(struct ospf
*ospf
, struct prefix_ipv4
*p
,
351 struct ospf_route
* or)
353 struct zapi_route api
;
355 if (ospf
->gr_info
.restart_in_progress
) {
356 if (IS_DEBUG_OSPF_GR
)
358 "Zebra: Graceful Restart in progress -- not uninstalling %pFX",
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
));
370 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
371 zlog_debug("Zebra: Route delete %pFX", p
);
373 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
376 void ospf_zebra_add_discard(struct ospf
*ospf
, struct prefix_ipv4
*p
)
378 struct zapi_route api
;
380 if (ospf
->gr_info
.restart_in_progress
) {
381 if (IS_DEBUG_OSPF_GR
)
383 "Zebra: Graceful Restart in progress -- not installing %pFX",
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
);
396 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
398 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
399 zlog_debug("Zebra: Route add discard %pFX", p
);
402 void ospf_zebra_delete_discard(struct ospf
*ospf
, struct prefix_ipv4
*p
)
404 struct zapi_route api
;
406 if (ospf
->gr_info
.restart_in_progress
) {
407 if (IS_DEBUG_OSPF_GR
)
409 "Zebra: Graceful Restart in progress -- not uninstalling %pFX",
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
);
422 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
424 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
425 zlog_debug("Zebra: Route delete discard %pFX", p
);
428 struct ospf_external
*ospf_external_lookup(struct ospf
*ospf
, uint8_t type
,
429 unsigned short instance
)
431 struct list
*ext_list
;
432 struct listnode
*node
;
433 struct ospf_external
*ext
;
435 ext_list
= ospf
->external
[type
];
439 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
))
440 if (ext
->instance
== instance
)
446 struct ospf_external
*ospf_external_add(struct ospf
*ospf
, uint8_t type
,
447 unsigned short instance
)
449 struct list
*ext_list
;
450 struct ospf_external
*ext
;
452 ext
= ospf_external_lookup(ospf
, type
, instance
);
456 if (!ospf
->external
[type
])
457 ospf
->external
[type
] = list_new();
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();
464 listnode_add(ext_list
, ext
);
470 * Walk all the ei received from zebra for a route type and apply
473 bool ospf_external_default_routemap_apply_walk(struct ospf
*ospf
,
474 struct list
*ext_list
,
475 struct external_info
*default_ei
)
477 struct listnode
*node
;
478 struct ospf_external
*ext
;
479 struct route_node
*rn
;
480 struct external_info
*ei
= NULL
;
483 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
484 if (!ext
->external_info
)
487 for (rn
= route_top(ext
->external_info
); rn
;
488 rn
= route_next(rn
)) {
492 ret
= ospf_external_info_apply_default_routemap(
493 ospf
, ei
, default_ei
);
500 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
501 zlog_debug("Default originate routemap permit ei: %pI4",
510 * Function to originate or flush default after applying
511 * route-map on all ei.
513 static void ospf_external_lsa_default_routemap_timer(struct event
*thread
)
515 struct list
*ext_list
;
516 struct ospf
*ospf
= EVENT_ARG(thread
);
517 struct prefix_ipv4 p
;
520 struct ospf_lsa
*lsa
;
521 struct external_info
*default_ei
;
525 p
.prefix
.s_addr
= INADDR_ANY
;
527 /* Get the default extenal info. */
528 default_ei
= ospf_external_info_lookup(ospf
, DEFAULT_ROUTE
,
531 /* Nothing to be done here. */
532 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
533 zlog_debug("Default originate info not present");
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
)
543 ret
= ospf_external_default_routemap_apply_walk(ospf
, ext_list
,
549 /* Get the default LSA. */
550 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
552 /* If permit then originate default. */
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);
562 void ospf_external_del(struct ospf
*ospf
, uint8_t type
, unsigned short instance
)
564 struct ospf_external
*ext
;
566 ext
= ospf_external_lookup(ospf
, type
, instance
);
569 if (EXTERNAL_INFO(ext
))
570 route_table_finish(EXTERNAL_INFO(ext
));
572 listnode_delete(ospf
->external
[type
], ext
);
574 if (!ospf
->external
[type
]->count
)
575 list_delete(&ospf
->external
[type
]);
577 XFREE(MTYPE_OSPF_EXTERNAL
, ext
);
581 * Check if default needs to be flushed too.
583 event_add_event(master
, ospf_external_lsa_default_routemap_timer
, ospf
,
584 0, &ospf
->t_default_routemap_timer
);
587 /* Update NHLFE for Prefix SID */
588 void ospf_zebra_update_prefix_sid(const struct sr_prefix
*srp
)
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
;
596 /* Prepare message. */
597 memset(&zl
, 0, sizeof(zl
));
598 zl
.type
= ZEBRA_LSP_OSPF_SR
;
599 zl
.local_label
= srp
->label_in
;
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
;
608 znh
->labels
[0] = srp
->nhlfe
.label_out
;
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
);
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;
625 /* Check that SRP contains at least one valid path */
626 if (srp
->route
== NULL
) {
630 osr_debug("SR (%s): Configure Prefix %pFX with",
631 __func__
, (struct prefix
*)&srp
->prefv4
);
633 for (ALL_LIST_ELEMENTS_RO(srp
->route
->paths
, node
, path
)) {
634 if (path
->srni
.label_out
== MPLS_INVALID_LABEL
)
637 if (zl
.nexthop_num
>= MULTIPATH_NUM
)
641 * TI-LFA backup path label stack comes first, if
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
;
651 memcpy(znh_backup
->labels
,
652 path
->srni
.backup_label_stack
->label
,
654 * path
->srni
.backup_label_stack
657 znh_backup
->label_num
=
658 path
->srni
.backup_label_stack
660 if (path
->srni
.label_out
661 != MPLS_LABEL_IPV4_EXPLICIT_NULL
662 && path
->srni
.label_out
663 != MPLS_LABEL_IMPLICIT_NULL
)
665 [znh_backup
->label_num
++] =
666 path
->srni
.label_out
;
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
;
674 znh
->labels
[0] = path
->srni
.label_out
;
676 osr_debug(" |- labels %u/%u", srp
->label_in
,
677 path
->srni
.label_out
);
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
);
683 ZAPI_NEXTHOP_FLAG_HAS_BACKUP
);
685 /* Just care about a single TI-LFA backup path
688 znh
->backup_idx
[0] = zl
.backup_nexthop_num
- 1;
697 /* Finally, send message to zebra. */
698 (void)zebra_send_mpls_labels(zclient
, ZEBRA_MPLS_LABELS_REPLACE
, &zl
);
701 /* Remove NHLFE for Prefix-SID */
702 void ospf_zebra_delete_prefix_sid(const struct sr_prefix
*srp
)
704 struct zapi_labels zl
;
706 osr_debug("SR (%s): Delete Labels %u for Prefix %pFX", __func__
,
707 srp
->label_in
, (struct prefix
*)&srp
->prefv4
);
709 /* Prepare message. */
710 memset(&zl
, 0, sizeof(zl
));
711 zl
.type
= ZEBRA_LSP_OSPF_SR
;
712 zl
.local_label
= srp
->label_in
;
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;
724 /* Send message to zebra. */
725 (void)zebra_send_mpls_labels(zclient
, ZEBRA_MPLS_LABELS_DELETE
, &zl
);
728 /* Send MPLS Label entry to Zebra for installation or deletion */
729 void ospf_zebra_send_adjacency_sid(int cmd
, struct sr_nhlfe nhlfe
)
731 struct zapi_labels zl
;
732 struct zapi_nexthop
*znh
;
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
);
738 memset(&zl
, 0, sizeof(zl
));
739 zl
.type
= ZEBRA_LSP_OSPF_SR
;
740 zl
.local_label
= nhlfe
.label_in
;
742 znh
= &zl
.nexthops
[0];
743 znh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
744 znh
->gate
.ipv4
= nhlfe
.nexthop
;
745 znh
->ifindex
= nhlfe
.ifindex
;
747 znh
->labels
[0] = nhlfe
.label_out
;
749 (void)zebra_send_mpls_labels(zclient
, cmd
, &zl
);
752 struct ospf_redist
*ospf_redist_lookup(struct ospf
*ospf
, uint8_t type
,
753 unsigned short instance
)
755 struct list
*red_list
;
756 struct listnode
*node
;
757 struct ospf_redist
*red
;
759 red_list
= ospf
->redist
[type
];
763 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
764 if (red
->instance
== instance
)
770 struct ospf_redist
*ospf_redist_add(struct ospf
*ospf
, uint8_t type
,
771 unsigned short instance
)
773 struct list
*red_list
;
774 struct ospf_redist
*red
;
776 red
= ospf_redist_lookup(ospf
, type
, instance
);
780 if (!ospf
->redist
[type
])
781 ospf
->redist
[type
] = list_new();
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
;
791 listnode_add(red_list
, red
);
796 void ospf_redist_del(struct ospf
*ospf
, uint8_t type
, unsigned short instance
)
798 struct ospf_redist
*red
;
800 red
= ospf_redist_lookup(ospf
, type
, instance
);
803 listnode_delete(ospf
->redist
[type
], red
);
804 if (!ospf
->redist
[type
]->count
) {
805 list_delete(&ospf
->redist
[type
]);
807 ospf_routemap_unset(red
);
808 XFREE(MTYPE_OSPF_REDISTRIBUTE
, red
);
813 int ospf_is_type_redistributed(struct ospf
*ospf
, int type
,
814 unsigned short instance
)
816 return (DEFAULT_ROUTE_TYPE(type
)
817 ? vrf_bitmap_check(zclient
->default_information
[AFI_IP
],
820 && redist_check_instance(
821 &zclient
->mi_redist
[AFI_IP
][type
],
825 zclient
->redist
[AFI_IP
][type
],
829 int ospf_redistribute_update(struct ospf
*ospf
, struct ospf_redist
*red
,
830 int type
, unsigned short instance
, int mtype
,
835 if (mtype
!= red
->dmetric
.type
) {
836 red
->dmetric
.type
= mtype
;
837 force
= LSA_REFRESH_FORCE
;
839 if (mvalue
!= red
->dmetric
.value
) {
840 red
->dmetric
.value
= mvalue
;
841 force
= LSA_REFRESH_FORCE
;
844 ospf_external_lsa_refresh_type(ospf
, type
, instance
, force
);
846 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
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
));
856 int ospf_redistribute_set(struct ospf
*ospf
, struct ospf_redist
*red
, int type
,
857 unsigned short instance
, int mtype
, int mvalue
)
859 red
->dmetric
.type
= mtype
;
860 red
->dmetric
.value
= mvalue
;
862 ospf_external_add(ospf
, type
, instance
);
864 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
, type
,
865 instance
, ospf
->vrf_id
);
867 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
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
));
874 ospf_asbr_status_update(ospf
, ++ospf
->redistribute
);
879 int ospf_redistribute_unset(struct ospf
*ospf
, int type
,
880 unsigned short instance
)
882 if (type
== zclient
->redist_default
&& instance
== zclient
->instance
)
885 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, AFI_IP
, type
,
886 instance
, ospf
->vrf_id
);
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
);
892 /* Remove the routes from OSPF table. */
893 ospf_redistribute_withdraw(ospf
, type
, instance
);
895 ospf_external_del(ospf
, type
, instance
);
897 ospf_asbr_status_update(ospf
, --ospf
->redistribute
);
902 int ospf_redistribute_default_set(struct ospf
*ospf
, int originate
, int mtype
,
905 struct prefix_ipv4 p
;
906 struct in_addr nexthop
;
907 int cur_originate
= ospf
->default_originate
;
908 const char *type_str
= NULL
;
910 nexthop
.s_addr
= INADDR_ANY
;
912 p
.prefix
.s_addr
= INADDR_ANY
;
915 ospf
->default_originate
= originate
;
917 if (cur_originate
== originate
) {
918 /* Refresh the lsa since metric might different */
919 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
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));
926 ospf_external_lsa_refresh_default(ospf
);
930 switch (cur_originate
) {
931 case DEFAULT_ORIGINATE_NONE
:
933 case DEFAULT_ORIGINATE_ZEBRA
:
934 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
935 zclient
, AFI_IP
, ospf
->vrf_id
);
936 ospf
->redistribute
--;
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
--;
946 case DEFAULT_ORIGINATE_NONE
:
949 case DEFAULT_ORIGINATE_ZEBRA
:
951 ospf
->redistribute
++;
952 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
953 zclient
, AFI_IP
, ospf
->vrf_id
);
955 case DEFAULT_ORIGINATE_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
);
964 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
965 zlog_debug("Redistribute[DEFAULT]: %s Type[%d], Metric[%d]",
967 metric_type(ospf
, DEFAULT_ROUTE
, 0),
968 metric_value(ospf
, DEFAULT_ROUTE
, 0));
970 ospf_external_lsa_refresh_default(ospf
);
971 ospf_asbr_status_update(ospf
, ospf
->redistribute
);
975 static int ospf_external_lsa_originate_check(struct ospf
*ospf
,
976 struct external_info
*ei
)
978 /* If prefix is multicast, then do not originate LSA. */
979 if (IN_MULTICAST(htonl(ei
->p
.prefix
.s_addr
))) {
981 "LSA[Type5:%pI4]: Not originate AS-external-LSA, Prefix belongs multicast",
986 /* Take care of default-originate. */
987 if (is_default_prefix4(&ei
->p
))
988 if (ospf
->default_originate
== DEFAULT_ORIGINATE_NONE
) {
990 "LSA[Type5:0.0.0.0]: Not originate AS-external-LSA for default");
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
)
1000 struct listnode
*node
;
1001 struct ospf_interface
*oi
;
1004 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
1005 if (prefix_match(oi
->address
, (struct prefix
*)&ei
->p
))
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
)
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
;
1022 if (!ospf_external_lsa_originate_check(ospf
, default_ei
))
1025 save_values
= default_ei
->route_map_set
;
1026 ospf_reset_route_map_set_values(&default_ei
->route_map_set
);
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
;
1033 ret
= route_map_apply(ROUTEMAP(red
), (struct prefix
*)p
, ei
);
1035 if (ret
== RMAP_DENYMATCH
) {
1036 ei
->route_map_set
= save_values
;
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.
1050 static bool ospf_external_lsa_default_routemap_apply(struct ospf
*ospf
,
1051 struct external_info
*ei
,
1054 struct external_info
*default_ei
;
1055 struct prefix_ipv4 p
;
1056 struct ospf_lsa
*lsa
;
1061 p
.prefix
.s_addr
= INADDR_ANY
;
1064 /* Get the default extenal info. */
1065 default_ei
= ospf_external_info_lookup(ospf
, DEFAULT_ROUTE
,
1066 ospf
->instance
, &p
);
1068 /* Nothing to be done here. */
1072 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1073 zlog_debug("Apply default originate routemap on ei: %pI4 cmd: %d",
1074 &ei
->p
.prefix
, cmd
);
1076 ret
= ospf_external_info_apply_default_routemap(ospf
, ei
, default_ei
);
1078 /* If deny then nothing to be done both in add and del case. */
1080 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1081 zlog_debug("Default originte routemap deny for ei: %pI4",
1086 /* Get the default LSA. */
1087 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
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");
1098 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1099 zlog_debug("Originating/Refreshing default lsa");
1101 if (lsa
&& IS_LSA_MAXAGE(lsa
))
1103 ospf_external_lsa_refresh(ospf
, lsa
, default_ei
, true,
1106 /* If permit and default not advertised then advertise.
1108 ospf_external_lsa_originate(ospf
, default_ei
);
1110 } else if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_DEL
) {
1111 /* If deny and lsa is not originated then nothing to be done.*/
1113 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1115 "Default lsa not originated, not flushing");
1119 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1121 "Running default route-map again as ei: %pI4 deleted",
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.
1128 event_add_event(master
,
1129 ospf_external_lsa_default_routemap_timer
, ospf
,
1130 0, &ospf
->t_default_routemap_timer
);
1136 /* return 1 if external LSA must be originated, 0 otherwise */
1137 int ospf_redistribute_check(struct ospf
*ospf
, struct external_info
*ei
,
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;
1147 /* Default is handled differently. */
1148 if (type
== DEFAULT_ROUTE
)
1154 if (!ospf_external_lsa_originate_check(ospf
, ei
))
1157 /* Take care connected route. */
1158 if (type
== ZEBRA_ROUTE_CONNECT
1159 && !ospf_distribute_check_connected(ospf
, ei
))
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
)
1167 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
1169 "Redistribute[%s]: %pFX filtered by distribute-list.",
1170 ospf_redist_string(type
), p
);
1174 save_values
= ei
->route_map_set
;
1175 ospf_reset_route_map_set_values(&ei
->route_map_set
);
1177 saved_tag
= ei
->tag
;
1178 /* Resetting with original route tag */
1179 ei
->tag
= ei
->orig_tag
;
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
;
1186 ret
= route_map_apply(ROUTEMAP(red
), (struct prefix
*)p
, ei
);
1188 if (ret
== RMAP_DENYMATCH
) {
1189 ei
->route_map_set
= save_values
;
1190 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
1192 "Redistribute[%s]: %pFX filtered by route-map.",
1193 ospf_redist_string(type
), p
);
1197 /* check if 'route-map set' changed something */
1199 *changed
= !ospf_route_map_set_compare(
1200 &ei
->route_map_set
, &save_values
);
1202 /* check if tag is modified */
1203 *changed
|= (saved_tag
!= ei
->tag
);
1210 /* OSPF route-map set for redistribution */
1211 void ospf_routemap_set(struct ospf_redist
*red
, const char *name
)
1213 if (ROUTEMAP_NAME(red
)) {
1214 route_map_counter_decrement(ROUTEMAP(red
));
1215 free(ROUTEMAP_NAME(red
));
1218 ROUTEMAP_NAME(red
) = strdup(name
);
1219 ROUTEMAP(red
) = route_map_lookup_by_name(name
);
1220 route_map_counter_increment(ROUTEMAP(red
));
1223 void ospf_routemap_unset(struct ospf_redist
*red
)
1225 if (ROUTEMAP_NAME(red
)) {
1226 route_map_counter_decrement(ROUTEMAP(red
));
1227 free(ROUTEMAP_NAME(red
));
1230 ROUTEMAP_NAME(red
) = NULL
;
1231 ROUTEMAP(red
) = NULL
;
1234 static int ospf_zebra_gr_update(struct ospf
*ospf
, int command
,
1235 uint32_t stale_time
)
1237 struct zapi_cap api
;
1239 if (!zclient
|| zclient
->sock
< 0 || !ospf
)
1242 memset(&api
, 0, sizeof(api
));
1244 api
.stale_removal_time
= stale_time
;
1245 api
.vrf_id
= ospf
->vrf_id
;
1247 (void)zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
,
1253 int ospf_zebra_gr_enable(struct ospf
*ospf
, uint32_t stale_time
)
1255 if (IS_DEBUG_OSPF_GR
)
1256 zlog_debug("Zebra enable GR [stale time %u]", stale_time
);
1258 return ospf_zebra_gr_update(ospf
, ZEBRA_CLIENT_GR_CAPABILITIES
,
1262 int ospf_zebra_gr_disable(struct ospf
*ospf
)
1264 if (IS_DEBUG_OSPF_GR
)
1265 zlog_debug("Zebra disable GR");
1267 return ospf_zebra_gr_update(ospf
, ZEBRA_CLIENT_GR_DISABLE
, 0);
1270 /* Zebra route add and delete treatment. */
1271 static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS
)
1273 struct zapi_route api
;
1274 struct prefix_ipv4 p
;
1276 unsigned long ifindex
;
1277 struct in_addr nexthop
;
1278 struct external_info
*ei
;
1283 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
1287 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
1290 ifindex
= api
.nexthops
[0].ifindex
;
1291 nexthop
= api
.nexthops
[0].gate
.ipv4
;
1294 memcpy(&p
, &api
.prefix
, sizeof(p
));
1295 if (IPV4_NET127(ntohl(p
.prefix
.s_addr
)))
1298 pgen
.family
= p
.family
;
1299 pgen
.prefixlen
= p
.prefixlen
;
1300 pgen
.u
.prefix4
= p
.prefix
;
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.
1308 if ((is_default_prefix(&pgen
)) && (api
.type
!= ZEBRA_ROUTE_OSPF
))
1309 rt_type
= DEFAULT_ROUTE
;
1311 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
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
,
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?
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
];
1331 * Given zebra sends update for a prefix via ADD message, it
1333 * be considered as an implicit DEL for that prefix with other
1337 for (i
= 0; i
<= ZEBRA_ROUTE_MAX
; i
++)
1339 ospf_external_info_delete(ospf
, i
, api
.instance
,
1342 ei
= ospf_external_info_add(ospf
, rt_type
, api
.instance
, p
,
1343 ifindex
, nexthop
, api
.tag
,
1346 /* Nothing has changed, so nothing to do; return */
1349 if (ospf
->router_id
.s_addr
!= INADDR_ANY
) {
1350 if (is_default_prefix4(&p
))
1351 ospf_external_lsa_refresh_default(ospf
);
1353 struct ospf_external_aggr_rt
*aggr
;
1354 struct as_external_lsa
*al
;
1355 struct ospf_lsa
*lsa
= NULL
;
1356 struct in_addr mask
;
1358 aggr
= ospf_external_aggr_match(ospf
, &ei
->p
);
1361 /* Check the AS-external-LSA
1362 * should be originated.
1364 if (!ospf_redistribute_check(ospf
, ei
,
1368 if (IS_DEBUG_OSPF(lsa
, EXTNL_LSA_AGGR
))
1370 "%s: Send Aggreate LSA (%pI4/%d)",
1375 ospf_originate_summary_lsa(ospf
, aggr
,
1378 /* Handling the case where the
1379 * external route prefix
1380 * and aggegate prefix is same
1381 * If same don't flush the
1386 (struct prefix
*)&aggr
->p
,
1387 (struct prefix
*)&ei
->p
))
1390 lsa
= ospf_external_info_find_lsa(
1394 al
= (struct as_external_lsa
*)
1396 masklen2ip(ei
->p
.prefixlen
,
1403 ospf_external_lsa_flush(
1404 ospf
, ei
->type
, &ei
->p
,
1408 struct ospf_lsa
*current
;
1410 current
= ospf_external_info_find_lsa(
1418 if (!ospf_redistribute_check(
1422 ospf_external_lsa_originate(
1427 ZEBRA_REDISTRIBUTE
))
1429 "%s: %pI4 refreshing LSA",
1432 ospf_external_lsa_refresh(
1442 * Check if default-information originate is
1443 * with some routemap prefix/access list match.
1445 ospf_external_lsa_default_routemap_apply(ospf
, ei
, cmd
);
1447 } else { /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
1448 struct ospf_external_aggr_rt
*aggr
;
1450 ei
= ospf_external_info_lookup(ospf
, rt_type
, api
.instance
, &p
);
1455 * Check if default-information originate i
1456 * with some routemap prefix/access list match.
1457 * Apply before ei is deleted.
1459 ospf_external_lsa_default_routemap_apply(ospf
, ei
, cmd
);
1461 aggr
= ospf_external_aggr_match(ospf
, &ei
->p
);
1463 if (aggr
&& (ei
->aggr_route
== aggr
)) {
1464 ospf_unlink_ei_from_aggr(ospf
, aggr
, ei
);
1466 ospf_external_info_delete(ospf
, rt_type
, api
.instance
,
1469 ospf_external_info_delete(ospf
, rt_type
, api
.instance
,
1472 if (is_default_prefix4(&p
))
1473 ospf_external_lsa_refresh_default(ospf
);
1475 ospf_external_lsa_flush(ospf
, rt_type
, &p
,
1476 ifindex
/*, nexthop */);
1483 void ospf_zebra_import_default_route(struct ospf
*ospf
, bool unreg
)
1485 struct prefix prefix
= {};
1488 if (zclient
->sock
< 0) {
1489 if (IS_DEBUG_OSPF(zebra
, ZEBRA
))
1490 zlog_debug(" Not connected to Zebra");
1494 prefix
.family
= AF_INET
;
1495 prefix
.prefixlen
= 0;
1498 command
= ZEBRA_NEXTHOP_UNREGISTER
;
1500 command
= ZEBRA_NEXTHOP_REGISTER
;
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
,
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",
1513 static int ospf_zebra_import_check_update(ZAPI_CALLBACK_ARGS
)
1516 struct zapi_route nhr
;
1517 struct prefix matched
;
1519 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
1520 if (ospf
== NULL
|| !IS_OSPF_ASBR(ospf
))
1523 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &matched
, &nhr
)) {
1524 zlog_err("%s[%u]: Failure to decode route", __func__
,
1529 if (matched
.family
!= AF_INET
|| matched
.prefixlen
!= 0 ||
1530 nhr
.type
== ZEBRA_ROUTE_OSPF
)
1533 ospf
->nssa_default_import_check
.status
= !!nhr
.nexthop_num
;
1534 ospf_abr_nssa_type7_defaults(ospf
);
1539 int ospf_distribute_list_out_set(struct ospf
*ospf
, int type
, const char *name
)
1541 /* Lookup access-list for distribute-list. */
1542 DISTRIBUTE_LIST(ospf
, type
) = access_list_lookup(AFI_IP
, name
);
1544 /* Clear previous distribute-name. */
1545 if (DISTRIBUTE_NAME(ospf
, type
))
1546 free(DISTRIBUTE_NAME(ospf
, type
));
1548 /* Set distribute-name. */
1549 DISTRIBUTE_NAME(ospf
, type
) = strdup(name
);
1551 /* If access-list have been set, schedule update timer. */
1552 if (DISTRIBUTE_LIST(ospf
, type
))
1553 ospf_distribute_list_update(ospf
, type
, 0);
1558 int ospf_distribute_list_out_unset(struct ospf
*ospf
, int type
,
1561 /* Schedule update timer. */
1562 if (DISTRIBUTE_LIST(ospf
, type
))
1563 ospf_distribute_list_update(ospf
, type
, 0);
1565 /* Unset distribute-list. */
1566 DISTRIBUTE_LIST(ospf
, type
) = NULL
;
1568 /* Clear distribute-name. */
1569 if (DISTRIBUTE_NAME(ospf
, type
))
1570 free(DISTRIBUTE_NAME(ospf
, type
));
1572 DISTRIBUTE_NAME(ospf
, type
) = NULL
;
1577 /* distribute-list update timer. */
1578 static void ospf_distribute_list_update_timer(struct event
*thread
)
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
);
1590 ospf
->t_distribute_update
= NULL
;
1592 zlog_info("Zebra[Redistribute]: distribute-list update timer fired!");
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
),
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
;
1606 ext_list
= ospf
->external
[type
];
1610 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
1611 rt
= ext
->external_info
;
1614 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
1619 if (is_default_prefix4(&ei
->p
))
1620 default_refresh
= 1;
1622 struct ospf_external_aggr_rt
*aggr
;
1624 aggr
= ospf_external_aggr_match(ospf
,
1629 * should be originated.
1631 if (!ospf_redistribute_check(
1634 ospf_unlink_ei_from_aggr(
1643 "%s: Send Aggregate LSA (%pI4/%d)",
1648 /* Originate Aggregate
1651 ospf_originate_summary_lsa(
1654 (lsa
= ospf_external_info_find_lsa(
1657 LSA_REFRESH_IF_CHANGED
;
1658 /* If this is a MaxAge
1661 * because distribute
1662 * settings might have
1664 * this LSA needs to be
1665 * originated, not be
1668 * refresh it, it will
1669 * remain a MaxAge LSA
1670 * because it will look
1672 * changed. Neighbors
1674 * updates for this LSA.
1676 if (IS_LSA_MAXAGE(lsa
))
1677 force
= LSA_REFRESH_FORCE
;
1679 ospf_external_lsa_refresh(
1680 ospf
, lsa
, ei
, force
,
1683 if (!ospf_redistribute_check(
1686 ospf_external_lsa_originate(
1693 if (default_refresh
)
1694 ospf_external_lsa_refresh_default(ospf
);
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
)
1701 struct ospf_external
*ext
;
1703 /* External info does not exist. */
1704 ext
= ospf_external_lookup(ospf
, type
, instance
);
1705 if (!ext
|| !EXTERNAL_INFO(ext
))
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
);
1713 /* If access-list is updated, apply some check. */
1714 static void ospf_filter_update(struct access_list
*access
)
1719 struct ospf_area
*area
;
1720 struct listnode
*node
, *n1
;
1722 /* If OSPF instance does not exist, return right now. */
1723 if (listcount(om
->ospf
) == 0)
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
;
1733 red_list
= ospf
->redist
[type
];
1735 for (ALL_LIST_ELEMENTS_RO(red_list
, node
,
1737 if (ROUTEMAP(red
)) {
1738 /* if route-map is not NULL it
1740 * using this access list */
1741 ospf_distribute_list_update(
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
)
1753 if (DISTRIBUTE_NAME(ospf
, type
)) {
1754 /* Keep old access-list for distribute-list. */
1755 struct access_list
*old
=
1756 DISTRIBUTE_LIST(ospf
, type
);
1758 /* Update access-list for distribute-list. */
1759 DISTRIBUTE_LIST(ospf
, type
) =
1762 DISTRIBUTE_NAME(ospf
, type
));
1764 /* No update for this distribute type. */
1766 && DISTRIBUTE_LIST(ospf
, type
) == NULL
)
1769 /* Schedule distribute-list update timer. */
1770 if (DISTRIBUTE_LIST(ospf
, type
) == NULL
1771 || strcmp(DISTRIBUTE_NAME(ospf
, type
),
1774 ospf_distribute_list_update(ospf
, type
,
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
;
1786 if (IMPORT_NAME(area
)) {
1787 IMPORT_LIST(area
) = NULL
;
1792 /* Schedule ABR tasks -- this will be changed -- takada. */
1793 if (IS_OSPF_ABR(ospf
) && abr_inv
)
1794 ospf_schedule_abr_task(ospf
);
1798 /* If prefix-list is updated, do some updates. */
1799 static void ospf_prefix_list_update(struct prefix_list
*plist
)
1801 struct ospf
*ospf
= NULL
;
1804 struct ospf_area
*area
;
1805 struct listnode
*node
, *n1
;
1807 /* If OSPF instatnce does not exist, return right now. */
1808 if (listcount(om
->ospf
) == 0)
1811 /* Iterate all ospf [VRF] instances */
1812 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
1814 /* Update all route-maps which are used
1815 * as redistribution filters.
1816 * They might use prefix-list.
1818 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
1819 struct list
*red_list
;
1820 struct ospf_redist
*red
;
1822 red_list
= ospf
->redist
[type
];
1826 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
1827 if (ROUTEMAP(red
)) {
1828 /* if route-map is not NULL
1830 * this prefix list */
1831 ospf_distribute_list_update(
1832 ospf
, type
, red
->instance
);
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
))
1844 PREFIX_LIST_IN(area
) = prefix_list_lookup(
1845 AFI_IP
, PREFIX_NAME_IN(area
));
1849 /* Update filter-list out. */
1850 if (PREFIX_NAME_OUT(area
)
1851 && strcmp(PREFIX_NAME_OUT(area
),
1852 prefix_list_name(plist
))
1854 PREFIX_LIST_OUT(area
) = prefix_list_lookup(
1855 AFI_IP
, PREFIX_NAME_OUT(area
));
1860 /* Schedule ABR task. */
1861 if (IS_OSPF_ABR(ospf
) && abr_inv
)
1862 ospf_schedule_abr_task(ospf
);
1866 static struct ospf_distance
*ospf_distance_new(void)
1868 return XCALLOC(MTYPE_OSPF_DISTANCE
, sizeof(struct ospf_distance
));
1871 static void ospf_distance_free(struct ospf_distance
*odistance
)
1873 XFREE(MTYPE_OSPF_DISTANCE
, odistance
);
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
)
1881 struct prefix_ipv4 p
;
1883 struct route_node
*rn
;
1884 struct ospf_distance
*odistance
;
1886 ret
= str2prefix_ipv4(ip_str
, &p
);
1888 vty_out(vty
, "Malformed prefix\n");
1889 return CMD_WARNING_CONFIG_FAILED
;
1892 distance
= atoi(distance_str
);
1894 /* Get OSPF distance node. */
1895 rn
= route_node_get(ospf
->distance_table
, (struct prefix
*)&p
);
1897 odistance
= rn
->info
;
1898 route_unlock_node(rn
);
1900 odistance
= ospf_distance_new();
1901 rn
->info
= odistance
;
1904 /* Set distance value. */
1905 odistance
->distance
= distance
;
1907 /* Reset access-list configuration. */
1908 if (odistance
->access_list
) {
1909 free(odistance
->access_list
);
1910 odistance
->access_list
= NULL
;
1912 if (access_list_str
)
1913 odistance
->access_list
= strdup(access_list_str
);
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
)
1923 struct prefix_ipv4 p
;
1924 struct route_node
*rn
;
1925 struct ospf_distance
*odistance
;
1927 ret
= str2prefix_ipv4(ip_str
, &p
);
1929 vty_out(vty
, "Malformed prefix\n");
1930 return CMD_WARNING_CONFIG_FAILED
;
1933 rn
= route_node_lookup(ospf
->distance_table
, (struct prefix
*)&p
);
1935 vty_out(vty
, "Can't find specified prefix\n");
1936 return CMD_WARNING_CONFIG_FAILED
;
1939 odistance
= rn
->info
;
1941 if (odistance
->access_list
)
1942 free(odistance
->access_list
);
1943 ospf_distance_free(odistance
);
1946 route_unlock_node(rn
);
1947 route_unlock_node(rn
);
1952 void ospf_distance_reset(struct ospf
*ospf
)
1954 struct route_node
*rn
;
1955 struct ospf_distance
*odistance
;
1957 for (rn
= route_top(ospf
->distance_table
); rn
; rn
= route_next(rn
)) {
1958 odistance
= rn
->info
;
1962 if (odistance
->access_list
)
1963 free(odistance
->access_list
);
1964 ospf_distance_free(odistance
);
1966 route_unlock_node(rn
);
1970 uint8_t ospf_distance_apply(struct ospf
*ospf
, struct prefix_ipv4
*p
,
1971 struct ospf_route
* or)
1977 if (ospf
->distance_intra
&& or->path_type
== OSPF_PATH_INTRA_AREA
)
1978 return ospf
->distance_intra
;
1980 if (ospf
->distance_inter
&& or->path_type
== OSPF_PATH_INTER_AREA
)
1981 return ospf
->distance_inter
;
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
;
1988 if (ospf
->distance_all
)
1989 return ospf
->distance_all
;
1994 void ospf_zebra_vrf_register(struct ospf
*ospf
)
1996 if (!zclient
|| zclient
->sock
< 0 || !ospf
)
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
),
2004 zclient_send_reg_requests(zclient
, ospf
->vrf_id
);
2008 void ospf_zebra_vrf_deregister(struct ospf
*ospf
)
2010 if (!zclient
|| zclient
->sock
< 0 || !ospf
)
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
),
2018 /* Deregister for router-id, interfaces,
2019 * redistributed routes. */
2020 zclient_send_dereg_requests(zclient
, ospf
->vrf_id
);
2024 /* Label Manager Functions */
2027 * Check if Label Manager is Ready or not.
2029 * @return True if Label Manager is ready, False otherwise
2031 bool ospf_zebra_label_manager_ready(void)
2033 return (zclient_sync
->sock
> 0);
2037 * Request Label Range to the Label Manager.
2039 * @param base base label of the label range to request
2040 * @param chunk_size size of the label range to request
2042 * @return 0 on success, -1 on failure
2044 int ospf_zebra_request_label_range(uint32_t base
, uint32_t chunk_size
)
2047 uint32_t start
, end
;
2049 if (zclient_sync
->sock
< 0)
2052 ret
= lm_get_label_chunk(zclient_sync
, 0, base
, chunk_size
, &start
,
2055 zlog_warn("%s: error getting label range!", __func__
);
2063 * Release Label Range to the Label Manager.
2065 * @param start start of label range to release
2066 * @param end end of label range to release
2068 * @return 0 on success, -1 otherwise
2070 int ospf_zebra_release_label_range(uint32_t start
, uint32_t end
)
2074 if (zclient_sync
->sock
< 0)
2077 ret
= lm_release_label_chunk(zclient_sync
, start
, end
);
2079 zlog_warn("%s: error releasing label range!", __func__
);
2087 * Connect to the Label Manager.
2089 * @return 0 on success, -1 otherwise
2091 int ospf_zebra_label_manager_connect(void)
2093 /* Connect to label manager. */
2094 if (zclient_socket_connect(zclient_sync
) < 0) {
2095 zlog_warn("%s: failed connecting synchronous zclient!",
2099 /* make socket non-blocking */
2100 set_nonblocking(zclient_sync
->sock
);
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!",
2106 close(zclient_sync
->sock
);
2107 zclient_sync
->sock
= -1;
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;
2121 osr_debug("SR (%s): Successfully connected to the Label Manager",
2127 static void ospf_zebra_connected(struct zclient
*zclient
)
2130 struct listnode
*node
;
2132 /* Send the client registration */
2133 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
2135 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
2137 /* Activate graceful restart if configured. */
2138 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, node
, ospf
)) {
2139 if (!ospf
->gr_info
.restart_support
)
2141 (void)ospf_zebra_gr_enable(ospf
, ospf
->gr_info
.grace_period
);
2146 * opaque messages between processes
2148 static int ospf_opaque_msg_handler(ZAPI_CALLBACK_ARGS
)
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
;
2159 if (zclient_opaque_decode(s
, &info
) != 0)
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
);
2170 case LDP_IGP_SYNC_IF_STATE_UPDATE
:
2171 STREAM_GET(&state
, s
, sizeof(state
));
2172 ret
= ospf_ldp_sync_state_update(state
);
2174 case LDP_IGP_SYNC_ANNOUNCE_UPDATE
:
2175 STREAM_GET(&announce
, s
, sizeof(announce
));
2176 ret
= ospf_ldp_sync_announce_update(announce
);
2187 static int ospf_zebra_client_close_notify(ZAPI_CALLBACK_ARGS
)
2191 struct zapi_client_close_info info
;
2193 if (zapi_client_close_notify_decode(zclient
->ibuf
, &info
) < 0)
2196 ospf_ldp_sync_handle_client_close(&info
);
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
,
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
,
2212 [ZEBRA_OPAQUE_MESSAGE
] = ospf_opaque_msg_handler
,
2214 [ZEBRA_CLIENT_CLOSE_NOTIFY
] = ospf_zebra_client_close_notify
,
2217 void ospf_zebra_init(struct event_loop
*master
, unsigned short instance
)
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
;
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
;
2233 * session_id must be different from default value (0) to distinguish
2234 * the asynchronous socket from the synchronous one
2236 zclient_sync
->session_id
= 1;
2237 zclient_sync
->privs
= &ospfd_privs
;
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
);
2245 void ospf_zebra_send_arp(const struct interface
*ifp
, const struct prefix
*p
)
2247 zclient_send_neigh_discovery_req(zclient
, ifp
, p
);