2 * Zebra connect library for OSPFd
3 * Copyright (C) 1997, 98, 99, 2000 Kunihiro Ishiguro, Toshiaki Takada
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39 #include "ospfd/ospfd.h"
40 #include "ospfd/ospf_interface.h"
41 #include "ospfd/ospf_ism.h"
42 #include "ospfd/ospf_asbr.h"
43 #include "ospfd/ospf_asbr.h"
44 #include "ospfd/ospf_abr.h"
45 #include "ospfd/ospf_lsa.h"
46 #include "ospfd/ospf_dump.h"
47 #include "ospfd/ospf_route.h"
48 #include "ospfd/ospf_lsdb.h"
49 #include "ospfd/ospf_neighbor.h"
50 #include "ospfd/ospf_nsm.h"
51 #include "ospfd/ospf_zebra.h"
52 #include "ospfd/ospf_te.h"
53 #include "ospfd/ospf_sr.h"
54 #include "ospfd/ospf_ldp_sync.h"
56 DEFINE_MTYPE_STATIC(OSPFD
, OSPF_EXTERNAL
, "OSPF External route table");
57 DEFINE_MTYPE_STATIC(OSPFD
, OSPF_REDISTRIBUTE
, "OSPF Redistriute");
58 DEFINE_MTYPE_STATIC(OSPFD
, OSPF_DIST_ARGS
, "OSPF Distribute arguments");
61 /* Zebra structure to hold current status. */
62 struct zclient
*zclient
= NULL
;
63 /* and for the Synchronous connection to the Label Manager */
64 static struct zclient
*zclient_sync
;
66 /* For registering threads. */
67 extern struct thread_master
*master
;
69 /* Router-id update message from zebra. */
70 static int ospf_router_id_update_zebra(ZAPI_CALLBACK_ARGS
)
72 struct ospf
*ospf
= NULL
;
73 struct prefix router_id
;
74 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
76 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
77 zlog_debug("Zebra rcvd: router id update %pFX vrf %s id %u",
78 &router_id
, ospf_vrf_id_to_name(vrf_id
), vrf_id
);
80 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
83 ospf
->router_id_zebra
= router_id
.u
.prefix4
;
84 ospf_router_id_update(ospf
);
86 if (IS_DEBUG_OSPF_EVENT
)
88 "%s: ospf instance not found for vrf %s id %u router_id %pFX",
89 __func__
, ospf_vrf_id_to_name(vrf_id
), vrf_id
,
95 static int ospf_interface_address_add(ZAPI_CALLBACK_ARGS
)
98 struct ospf
*ospf
= NULL
;
101 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
106 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
107 zlog_debug("Zebra: interface %s address add %pFX vrf %s id %u",
108 c
->ifp
->name
, c
->address
,
109 ospf_vrf_id_to_name(vrf_id
), vrf_id
);
111 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
115 ospf_if_update(ospf
, c
->ifp
);
117 ospf_if_interface(c
->ifp
);
122 static int ospf_interface_address_delete(ZAPI_CALLBACK_ARGS
)
125 struct interface
*ifp
;
126 struct ospf_interface
*oi
;
127 struct route_node
*rn
;
130 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
135 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
136 zlog_debug("Zebra: interface %s address delete %pFX",
137 c
->ifp
->name
, c
->address
);
141 p
.prefixlen
= IPV4_MAX_BITLEN
;
143 rn
= route_node_lookup(IF_OIFS(ifp
), &p
);
151 route_unlock_node(rn
);
153 /* Call interface hook functions to clean up */
156 ospf_if_interface(c
->ifp
);
163 static int ospf_interface_link_params(ZAPI_CALLBACK_ARGS
)
165 struct interface
*ifp
;
166 bool changed
= false;
168 ifp
= zebra_interface_link_params_read(zclient
->ibuf
, vrf_id
, &changed
);
170 if (ifp
== NULL
|| !changed
)
174 ospf_mpls_te_update_if(ifp
);
179 /* VRF update for an interface. */
180 static int ospf_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
182 struct interface
*ifp
= NULL
;
185 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
190 if (IS_DEBUG_OSPF_EVENT
)
192 "%s: Rx Interface %s VRF change vrf_id %u New vrf %s id %u",
193 __func__
, ifp
->name
, vrf_id
,
194 ospf_vrf_id_to_name(new_vrf_id
), new_vrf_id
);
196 /*if_update(ifp, ifp->name, strlen(ifp->name), new_vrf_id);*/
197 if_update_to_new_vrf(ifp
, new_vrf_id
);
202 /* Nexthop, ifindex, distance and metric information. */
203 static void ospf_zebra_add_nexthop(struct ospf
*ospf
, struct ospf_path
*path
,
204 struct zapi_route
*api
)
206 struct zapi_nexthop
*api_nh
;
207 struct zapi_nexthop
*api_nh_backup
;
209 /* TI-LFA backup path label stack comes first, if present */
210 if (path
->srni
.backup_label_stack
) {
211 api_nh_backup
= &api
->backup_nexthops
[api
->backup_nexthop_num
];
212 api_nh_backup
->vrf_id
= ospf
->vrf_id
;
214 api_nh_backup
->type
= NEXTHOP_TYPE_IPV4
;
215 api_nh_backup
->gate
.ipv4
= path
->srni
.backup_nexthop
;
217 api_nh_backup
->label_num
=
218 path
->srni
.backup_label_stack
->num_labels
;
219 memcpy(api_nh_backup
->labels
,
220 path
->srni
.backup_label_stack
->label
,
221 sizeof(mpls_label_t
) * api_nh_backup
->label_num
);
223 api
->backup_nexthop_num
++;
226 /* And here comes the primary nexthop */
227 api_nh
= &api
->nexthops
[api
->nexthop_num
];
230 || (path
->nexthop
.s_addr
!= INADDR_ANY
&& path
->ifindex
!= 0)) {
231 #else /* HAVE_NETLINK */
232 if (path
->nexthop
.s_addr
!= INADDR_ANY
&& path
->ifindex
!= 0) {
233 #endif /* HAVE_NETLINK */
234 api_nh
->gate
.ipv4
= path
->nexthop
;
235 api_nh
->ifindex
= path
->ifindex
;
236 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
237 } else if (path
->nexthop
.s_addr
!= INADDR_ANY
) {
238 api_nh
->gate
.ipv4
= path
->nexthop
;
239 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
241 api_nh
->ifindex
= path
->ifindex
;
242 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
244 api_nh
->vrf_id
= ospf
->vrf_id
;
246 /* Set TI-LFA backup nexthop info if present */
247 if (path
->srni
.backup_label_stack
) {
248 SET_FLAG(api
->message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
);
249 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
);
251 /* Just care about a single TI-LFA backup path for now */
252 api_nh
->backup_num
= 1;
253 api_nh
->backup_idx
[0] = api
->backup_nexthop_num
- 1;
259 void ospf_zebra_add(struct ospf
*ospf
, struct prefix_ipv4
*p
,
260 struct ospf_route
* or)
262 struct zapi_route api
;
264 struct ospf_path
*path
;
265 struct listnode
*node
;
267 if (ospf
->gr_info
.restart_in_progress
) {
268 if (IS_DEBUG_OSPF_GR
)
270 "Zebra: Graceful Restart in progress -- not installing %pFX",
275 memset(&api
, 0, sizeof(api
));
276 api
.vrf_id
= ospf
->vrf_id
;
277 api
.type
= ZEBRA_ROUTE_OSPF
;
278 api
.instance
= ospf
->instance
;
279 api
.safi
= SAFI_UNICAST
;
281 memcpy(&api
.prefix
, p
, sizeof(*p
));
282 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
285 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
286 if (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
)
287 api
.metric
= or->cost
+ or->u
.ext
.type2_cost
;
288 else if (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
)
289 api
.metric
= or->u
.ext
.type2_cost
;
291 api
.metric
= or->cost
;
293 /* Check if path type is ASE */
294 if (((or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
)
295 || (or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
))
296 && (or->u
.ext
.tag
> 0) && (or->u
.ext
.tag
<= ROUTE_TAG_MAX
)) {
297 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
298 api
.tag
= or->u
.ext
.tag
;
301 /* Distance value. */
302 distance
= ospf_distance_apply(ospf
, p
, or);
304 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
305 api
.distance
= distance
;
308 for (ALL_LIST_ELEMENTS_RO(or->paths
, node
, path
)) {
309 if (api
.nexthop_num
>= ospf
->max_multipath
)
312 ospf_zebra_add_nexthop(ospf
, path
, &api
);
314 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
)) {
315 struct interface
*ifp
;
317 ifp
= if_lookup_by_index(path
->ifindex
, ospf
->vrf_id
);
320 "Zebra: Route add %pFX nexthop %pI4, ifindex=%d %s",
321 p
, &path
->nexthop
, path
->ifindex
,
322 ifp
? ifp
->name
: " ");
326 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
329 void ospf_zebra_delete(struct ospf
*ospf
, struct prefix_ipv4
*p
,
330 struct ospf_route
* or)
332 struct zapi_route api
;
334 if (ospf
->gr_info
.restart_in_progress
) {
335 if (IS_DEBUG_OSPF_GR
)
337 "Zebra: Graceful Restart in progress -- not uninstalling %pFX",
342 memset(&api
, 0, sizeof(api
));
343 api
.vrf_id
= ospf
->vrf_id
;
344 api
.type
= ZEBRA_ROUTE_OSPF
;
345 api
.instance
= ospf
->instance
;
346 api
.safi
= SAFI_UNICAST
;
347 memcpy(&api
.prefix
, p
, sizeof(*p
));
349 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
350 zlog_debug("Zebra: Route delete %pFX", p
);
352 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
355 void ospf_zebra_add_discard(struct ospf
*ospf
, struct prefix_ipv4
*p
)
357 struct zapi_route api
;
359 if (ospf
->gr_info
.restart_in_progress
) {
360 if (IS_DEBUG_OSPF_GR
)
362 "Zebra: Graceful Restart in progress -- not installing %pFX",
367 memset(&api
, 0, sizeof(api
));
368 api
.vrf_id
= ospf
->vrf_id
;
369 api
.type
= ZEBRA_ROUTE_OSPF
;
370 api
.instance
= ospf
->instance
;
371 api
.safi
= SAFI_UNICAST
;
372 memcpy(&api
.prefix
, p
, sizeof(*p
));
373 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
375 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
377 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
378 zlog_debug("Zebra: Route add discard %pFX", p
);
381 void ospf_zebra_delete_discard(struct ospf
*ospf
, struct prefix_ipv4
*p
)
383 struct zapi_route api
;
385 if (ospf
->gr_info
.restart_in_progress
) {
386 if (IS_DEBUG_OSPF_GR
)
388 "Zebra: Graceful Restart in progress -- not uninstalling %pFX",
393 memset(&api
, 0, sizeof(api
));
394 api
.vrf_id
= ospf
->vrf_id
;
395 api
.type
= ZEBRA_ROUTE_OSPF
;
396 api
.instance
= ospf
->instance
;
397 api
.safi
= SAFI_UNICAST
;
398 memcpy(&api
.prefix
, p
, sizeof(*p
));
399 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
401 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
403 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
404 zlog_debug("Zebra: Route delete discard %pFX", p
);
407 struct ospf_external
*ospf_external_lookup(struct ospf
*ospf
, uint8_t type
,
408 unsigned short instance
)
410 struct list
*ext_list
;
411 struct listnode
*node
;
412 struct ospf_external
*ext
;
414 ext_list
= ospf
->external
[type
];
418 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
))
419 if (ext
->instance
== instance
)
425 struct ospf_external
*ospf_external_add(struct ospf
*ospf
, uint8_t type
,
426 unsigned short instance
)
428 struct list
*ext_list
;
429 struct ospf_external
*ext
;
431 ext
= ospf_external_lookup(ospf
, type
, instance
);
435 if (!ospf
->external
[type
])
436 ospf
->external
[type
] = list_new();
438 ext_list
= ospf
->external
[type
];
439 ext
= XCALLOC(MTYPE_OSPF_EXTERNAL
, sizeof(struct ospf_external
));
440 ext
->instance
= instance
;
441 EXTERNAL_INFO(ext
) = route_table_init();
443 listnode_add(ext_list
, ext
);
449 * Walk all the ei received from zebra for a route type and apply
452 bool ospf_external_default_routemap_apply_walk(struct ospf
*ospf
,
453 struct list
*ext_list
,
454 struct external_info
*default_ei
)
456 struct listnode
*node
;
457 struct ospf_external
*ext
;
458 struct route_node
*rn
;
459 struct external_info
*ei
= NULL
;
462 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
463 if (!ext
->external_info
)
466 for (rn
= route_top(ext
->external_info
); rn
;
467 rn
= route_next(rn
)) {
471 ret
= ospf_external_info_apply_default_routemap(
472 ospf
, ei
, default_ei
);
479 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
480 zlog_debug("Default originate routemap permit ei: %pI4",
489 * Function to originate or flush default after applying
490 * route-map on all ei.
492 static int ospf_external_lsa_default_routemap_timer(struct thread
*thread
)
494 struct list
*ext_list
;
495 struct ospf
*ospf
= THREAD_ARG(thread
);
496 struct prefix_ipv4 p
;
499 struct ospf_lsa
*lsa
;
500 struct external_info
*default_ei
;
504 p
.prefix
.s_addr
= INADDR_ANY
;
506 /* Get the default extenal info. */
507 default_ei
= ospf_external_info_lookup(ospf
, DEFAULT_ROUTE
,
510 /* Nothing to be done here. */
511 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
512 zlog_debug("Default originate info not present");
516 /* For all the ei apply route-map */
517 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
518 ext_list
= ospf
->external
[type
];
519 if (!ext_list
|| type
== ZEBRA_ROUTE_OSPF
)
522 ret
= ospf_external_default_routemap_apply_walk(ospf
, ext_list
,
528 /* Get the default LSA. */
529 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
531 /* If permit then originate default. */
533 ospf_external_lsa_originate(ospf
, default_ei
);
534 else if (ret
&& lsa
&& IS_LSA_MAXAGE(lsa
))
535 ospf_external_lsa_refresh(ospf
, lsa
, default_ei
, true, false);
536 else if (!ret
&& lsa
)
537 ospf_external_lsa_flush(ospf
, DEFAULT_ROUTE
, &default_ei
->p
, 0);
543 void ospf_external_del(struct ospf
*ospf
, uint8_t type
, unsigned short instance
)
545 struct ospf_external
*ext
;
547 ext
= ospf_external_lookup(ospf
, type
, instance
);
550 if (EXTERNAL_INFO(ext
))
551 route_table_finish(EXTERNAL_INFO(ext
));
553 listnode_delete(ospf
->external
[type
], ext
);
555 if (!ospf
->external
[type
]->count
)
556 list_delete(&ospf
->external
[type
]);
558 XFREE(MTYPE_OSPF_EXTERNAL
, ext
);
562 * Check if default needs to be flushed too.
564 thread_add_event(master
, ospf_external_lsa_default_routemap_timer
, ospf
,
565 0, &ospf
->t_default_routemap_timer
);
568 /* Update NHLFE for Prefix SID */
569 void ospf_zebra_update_prefix_sid(const struct sr_prefix
*srp
)
571 struct zapi_labels zl
;
572 struct zapi_nexthop
*znh
;
573 struct zapi_nexthop
*znh_backup
;
574 struct listnode
*node
;
575 struct ospf_path
*path
;
577 /* Prepare message. */
578 memset(&zl
, 0, sizeof(zl
));
579 zl
.type
= ZEBRA_LSP_OSPF_SR
;
580 zl
.local_label
= srp
->label_in
;
584 /* Set Label for local Prefix */
585 znh
= &zl
.nexthops
[zl
.nexthop_num
++];
586 znh
->type
= NEXTHOP_TYPE_IFINDEX
;
587 znh
->ifindex
= srp
->nhlfe
.ifindex
;
589 znh
->labels
[0] = srp
->nhlfe
.label_out
;
591 osr_debug("SR (%s): Configure Prefix %pFX with labels %u/%u",
592 __func__
, (struct prefix
*)&srp
->prefv4
,
593 srp
->label_in
, srp
->nhlfe
.label_out
);
598 /* Update route in the RIB too. */
599 SET_FLAG(zl
.message
, ZAPI_LABELS_FTN
);
600 zl
.route
.prefix
.u
.prefix4
= srp
->prefv4
.prefix
;
601 zl
.route
.prefix
.prefixlen
= srp
->prefv4
.prefixlen
;
602 zl
.route
.prefix
.family
= srp
->prefv4
.family
;
603 zl
.route
.type
= ZEBRA_ROUTE_OSPF
;
604 zl
.route
.instance
= 0;
606 /* Check that SRP contains at least one valid path */
607 if (srp
->route
== NULL
) {
611 osr_debug("SR (%s): Configure Prefix %pFX with",
612 __func__
, (struct prefix
*)&srp
->prefv4
);
614 for (ALL_LIST_ELEMENTS_RO(srp
->route
->paths
, node
, path
)) {
615 if (path
->srni
.label_out
== MPLS_INVALID_LABEL
)
618 if (zl
.nexthop_num
>= MULTIPATH_NUM
)
622 * TI-LFA backup path label stack comes first, if
625 if (path
->srni
.backup_label_stack
) {
626 znh_backup
= &zl
.backup_nexthops
627 [zl
.backup_nexthop_num
++];
628 znh_backup
->type
= NEXTHOP_TYPE_IPV4
;
629 znh_backup
->gate
.ipv4
=
630 path
->srni
.backup_nexthop
;
632 memcpy(znh_backup
->labels
,
633 path
->srni
.backup_label_stack
->label
,
635 * path
->srni
.backup_label_stack
638 znh_backup
->label_num
=
639 path
->srni
.backup_label_stack
641 if (path
->srni
.label_out
642 != MPLS_LABEL_IPV4_EXPLICIT_NULL
643 && path
->srni
.label_out
644 != MPLS_LABEL_IMPLICIT_NULL
)
646 [znh_backup
->label_num
++] =
647 path
->srni
.label_out
;
650 znh
= &zl
.nexthops
[zl
.nexthop_num
++];
651 znh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
652 znh
->gate
.ipv4
= path
->nexthop
;
653 znh
->ifindex
= path
->ifindex
;
655 znh
->labels
[0] = path
->srni
.label_out
;
657 osr_debug(" |- labels %u/%u", srp
->label_in
,
658 path
->srni
.label_out
);
660 /* Set TI-LFA backup nexthop info if present */
661 if (path
->srni
.backup_label_stack
) {
662 SET_FLAG(zl
.message
, ZAPI_LABELS_HAS_BACKUPS
);
664 ZAPI_NEXTHOP_FLAG_HAS_BACKUP
);
666 /* Just care about a single TI-LFA backup path
669 znh
->backup_idx
[0] = zl
.backup_nexthop_num
- 1;
678 /* Finally, send message to zebra. */
679 (void)zebra_send_mpls_labels(zclient
, ZEBRA_MPLS_LABELS_REPLACE
, &zl
);
682 /* Remove NHLFE for Prefix-SID */
683 void ospf_zebra_delete_prefix_sid(const struct sr_prefix
*srp
)
685 struct zapi_labels zl
;
687 osr_debug("SR (%s): Delete Labels %u for Prefix %pFX", __func__
,
688 srp
->label_in
, (struct prefix
*)&srp
->prefv4
);
690 /* Prepare message. */
691 memset(&zl
, 0, sizeof(zl
));
692 zl
.type
= ZEBRA_LSP_OSPF_SR
;
693 zl
.local_label
= srp
->label_in
;
695 if (srp
->type
== PREF_SID
) {
696 /* Update route in the RIB too */
697 SET_FLAG(zl
.message
, ZAPI_LABELS_FTN
);
698 zl
.route
.prefix
.u
.prefix4
= srp
->prefv4
.prefix
;
699 zl
.route
.prefix
.prefixlen
= srp
->prefv4
.prefixlen
;
700 zl
.route
.prefix
.family
= srp
->prefv4
.family
;
701 zl
.route
.type
= ZEBRA_ROUTE_OSPF
;
702 zl
.route
.instance
= 0;
705 /* Send message to zebra. */
706 (void)zebra_send_mpls_labels(zclient
, ZEBRA_MPLS_LABELS_DELETE
, &zl
);
709 /* Send MPLS Label entry to Zebra for installation or deletion */
710 void ospf_zebra_send_adjacency_sid(int cmd
, struct sr_nhlfe nhlfe
)
712 struct zapi_labels zl
;
713 struct zapi_nexthop
*znh
;
715 osr_debug("SR (%s): %s Labels %u/%u for Adjacency via %u", __func__
,
716 cmd
== ZEBRA_MPLS_LABELS_ADD
? "Add" : "Delete",
717 nhlfe
.label_in
, nhlfe
.label_out
, nhlfe
.ifindex
);
719 memset(&zl
, 0, sizeof(zl
));
720 zl
.type
= ZEBRA_LSP_OSPF_SR
;
721 zl
.local_label
= nhlfe
.label_in
;
723 znh
= &zl
.nexthops
[0];
724 znh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
725 znh
->gate
.ipv4
= nhlfe
.nexthop
;
726 znh
->ifindex
= nhlfe
.ifindex
;
728 znh
->labels
[0] = nhlfe
.label_out
;
730 (void)zebra_send_mpls_labels(zclient
, cmd
, &zl
);
733 struct ospf_redist
*ospf_redist_lookup(struct ospf
*ospf
, uint8_t type
,
734 unsigned short instance
)
736 struct list
*red_list
;
737 struct listnode
*node
;
738 struct ospf_redist
*red
;
740 red_list
= ospf
->redist
[type
];
744 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
745 if (red
->instance
== instance
)
751 struct ospf_redist
*ospf_redist_add(struct ospf
*ospf
, uint8_t type
,
752 unsigned short instance
)
754 struct list
*red_list
;
755 struct ospf_redist
*red
;
757 red
= ospf_redist_lookup(ospf
, type
, instance
);
761 if (!ospf
->redist
[type
])
762 ospf
->redist
[type
] = list_new();
764 red_list
= ospf
->redist
[type
];
765 red
= XCALLOC(MTYPE_OSPF_REDISTRIBUTE
, sizeof(struct ospf_redist
));
766 red
->instance
= instance
;
767 red
->dmetric
.type
= -1;
768 red
->dmetric
.value
= -1;
769 ROUTEMAP_NAME(red
) = NULL
;
770 ROUTEMAP(red
) = NULL
;
772 listnode_add(red_list
, red
);
777 void ospf_redist_del(struct ospf
*ospf
, uint8_t type
, unsigned short instance
)
779 struct ospf_redist
*red
;
781 red
= ospf_redist_lookup(ospf
, type
, instance
);
784 listnode_delete(ospf
->redist
[type
], red
);
785 if (!ospf
->redist
[type
]->count
) {
786 list_delete(&ospf
->redist
[type
]);
788 ospf_routemap_unset(red
);
789 XFREE(MTYPE_OSPF_REDISTRIBUTE
, red
);
794 int ospf_is_type_redistributed(struct ospf
*ospf
, int type
,
795 unsigned short instance
)
797 return (DEFAULT_ROUTE_TYPE(type
)
798 ? vrf_bitmap_check(zclient
->default_information
[AFI_IP
],
801 && redist_check_instance(
802 &zclient
->mi_redist
[AFI_IP
][type
],
806 zclient
->redist
[AFI_IP
][type
],
810 int ospf_redistribute_update(struct ospf
*ospf
, struct ospf_redist
*red
,
811 int type
, unsigned short instance
, int mtype
,
816 if (mtype
!= red
->dmetric
.type
) {
817 red
->dmetric
.type
= mtype
;
818 force
= LSA_REFRESH_FORCE
;
820 if (mvalue
!= red
->dmetric
.value
) {
821 red
->dmetric
.value
= mvalue
;
822 force
= LSA_REFRESH_FORCE
;
825 ospf_external_lsa_refresh_type(ospf
, type
, instance
, force
);
827 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
829 "Redistribute[%s][%d]: Refresh Type[%d], Metric[%d]",
830 ospf_redist_string(type
), instance
,
831 metric_type(ospf
, type
, instance
),
832 metric_value(ospf
, type
, instance
));
837 int ospf_redistribute_set(struct ospf
*ospf
, struct ospf_redist
*red
, int type
,
838 unsigned short instance
, int mtype
, int mvalue
)
840 red
->dmetric
.type
= mtype
;
841 red
->dmetric
.value
= mvalue
;
843 ospf_external_add(ospf
, type
, instance
);
845 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
, type
,
846 instance
, ospf
->vrf_id
);
848 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
850 "Redistribute[%s][%d] vrf id %u: Start Type[%d], Metric[%d]",
851 ospf_redist_string(type
), instance
, ospf
->vrf_id
,
852 metric_type(ospf
, type
, instance
),
853 metric_value(ospf
, type
, instance
));
855 ospf_asbr_status_update(ospf
, ++ospf
->redistribute
);
860 int ospf_redistribute_unset(struct ospf
*ospf
, int type
,
861 unsigned short instance
)
863 if (type
== zclient
->redist_default
&& instance
== zclient
->instance
)
866 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, AFI_IP
, type
,
867 instance
, ospf
->vrf_id
);
869 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
870 zlog_debug("Redistribute[%s][%d] vrf id %u: Stop",
871 ospf_redist_string(type
), instance
, ospf
->vrf_id
);
873 /* Remove the routes from OSPF table. */
874 ospf_redistribute_withdraw(ospf
, type
, instance
);
876 ospf_external_del(ospf
, type
, instance
);
878 ospf_asbr_status_update(ospf
, --ospf
->redistribute
);
883 int ospf_redistribute_default_set(struct ospf
*ospf
, int originate
, int mtype
,
886 struct prefix_ipv4 p
;
887 struct in_addr nexthop
;
888 int cur_originate
= ospf
->default_originate
;
889 const char *type_str
= NULL
;
891 nexthop
.s_addr
= INADDR_ANY
;
893 p
.prefix
.s_addr
= INADDR_ANY
;
896 ospf
->default_originate
= originate
;
898 if (cur_originate
== originate
) {
899 /* Refresh the lsa since metric might different */
900 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
902 "Redistribute[%s]: Refresh Type[%d], Metric[%d]",
903 ospf_redist_string(DEFAULT_ROUTE
),
904 metric_type(ospf
, DEFAULT_ROUTE
, 0),
905 metric_value(ospf
, DEFAULT_ROUTE
, 0));
907 ospf_external_lsa_refresh_default(ospf
);
911 switch (cur_originate
) {
912 case DEFAULT_ORIGINATE_NONE
:
914 case DEFAULT_ORIGINATE_ZEBRA
:
915 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
916 zclient
, AFI_IP
, ospf
->vrf_id
);
917 ospf
->redistribute
--;
919 case DEFAULT_ORIGINATE_ALWAYS
:
920 ospf_external_info_delete(ospf
, DEFAULT_ROUTE
, 0, p
);
921 ospf_external_del(ospf
, DEFAULT_ROUTE
, 0);
922 ospf
->redistribute
--;
927 case DEFAULT_ORIGINATE_NONE
:
930 case DEFAULT_ORIGINATE_ZEBRA
:
932 ospf
->redistribute
++;
933 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
934 zclient
, AFI_IP
, ospf
->vrf_id
);
936 case DEFAULT_ORIGINATE_ALWAYS
:
938 ospf
->redistribute
++;
939 ospf_external_add(ospf
, DEFAULT_ROUTE
, 0);
940 ospf_external_info_add(ospf
, DEFAULT_ROUTE
, 0, p
, 0, nexthop
,
945 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
946 zlog_debug("Redistribute[DEFAULT]: %s Type[%d], Metric[%d]",
948 metric_type(ospf
, DEFAULT_ROUTE
, 0),
949 metric_value(ospf
, DEFAULT_ROUTE
, 0));
951 ospf_external_lsa_refresh_default(ospf
);
952 ospf_asbr_status_update(ospf
, ospf
->redistribute
);
956 static int ospf_external_lsa_originate_check(struct ospf
*ospf
,
957 struct external_info
*ei
)
959 /* If prefix is multicast, then do not originate LSA. */
960 if (IN_MULTICAST(htonl(ei
->p
.prefix
.s_addr
))) {
962 "LSA[Type5:%pI4]: Not originate AS-external-LSA, Prefix belongs multicast",
967 /* Take care of default-originate. */
968 if (is_default_prefix4(&ei
->p
))
969 if (ospf
->default_originate
== DEFAULT_ORIGINATE_NONE
) {
971 "LSA[Type5:0.0.0.0]: Not originate AS-external-LSA for default");
978 /* If connected prefix is OSPF enable interface, then do not announce. */
979 int ospf_distribute_check_connected(struct ospf
*ospf
, struct external_info
*ei
)
981 struct listnode
*node
;
982 struct ospf_interface
*oi
;
985 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
986 if (prefix_match(oi
->address
, (struct prefix
*)&ei
->p
))
992 /* Apply default route-map on ei received. */
993 int ospf_external_info_apply_default_routemap(struct ospf
*ospf
,
994 struct external_info
*ei
,
995 struct external_info
*default_ei
)
997 struct ospf_redist
*red
;
998 int type
= default_ei
->type
;
999 struct prefix_ipv4
*p
= &ei
->p
;
1000 struct route_map_set_values save_values
;
1003 if (!ospf_external_lsa_originate_check(ospf
, default_ei
))
1006 save_values
= default_ei
->route_map_set
;
1007 ospf_reset_route_map_set_values(&default_ei
->route_map_set
);
1009 /* apply route-map if needed */
1010 red
= ospf_redist_lookup(ospf
, type
, ospf
->instance
);
1011 if (red
&& ROUTEMAP_NAME(red
)) {
1012 route_map_result_t ret
;
1014 ret
= route_map_apply(ROUTEMAP(red
), (struct prefix
*)p
, ei
);
1016 if (ret
== RMAP_DENYMATCH
) {
1017 ei
->route_map_set
= save_values
;
1027 * Default originated is based on route-map condition then
1028 * apply route-map on received external info. Originate or
1029 * flush based on route-map condition.
1031 static bool ospf_external_lsa_default_routemap_apply(struct ospf
*ospf
,
1032 struct external_info
*ei
,
1035 struct external_info
*default_ei
;
1036 struct prefix_ipv4 p
;
1037 struct ospf_lsa
*lsa
;
1042 p
.prefix
.s_addr
= INADDR_ANY
;
1045 /* Get the default extenal info. */
1046 default_ei
= ospf_external_info_lookup(ospf
, DEFAULT_ROUTE
,
1047 ospf
->instance
, &p
);
1049 /* Nothing to be done here. */
1053 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1054 zlog_debug("Apply default originate routemap on ei: %pI4 cmd: %d",
1055 &ei
->p
.prefix
, cmd
);
1057 ret
= ospf_external_info_apply_default_routemap(ospf
, ei
, default_ei
);
1059 /* If deny then nothing to be done both in add and del case. */
1061 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1062 zlog_debug("Default originte routemap deny for ei: %pI4",
1067 /* Get the default LSA. */
1068 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
1070 /* If this is add route and permit then ooriginate default. */
1071 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
) {
1072 /* If permit and default already advertise then return. */
1073 if (lsa
&& !IS_LSA_MAXAGE(lsa
)) {
1074 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1075 zlog_debug("Default lsa already originated");
1079 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1080 zlog_debug("Originating/Refreshing default lsa");
1082 if (lsa
&& IS_LSA_MAXAGE(lsa
))
1084 ospf_external_lsa_refresh(ospf
, lsa
, default_ei
, true,
1087 /* If permit and default not advertised then advertise.
1089 ospf_external_lsa_originate(ospf
, default_ei
);
1091 } else if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_DEL
) {
1092 /* If deny and lsa is not originated then nothing to be done.*/
1094 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1096 "Default lsa not originated, not flushing");
1100 if (IS_DEBUG_OSPF_DEFAULT_INFO
)
1102 "Running default route-map again as ei: %pI4 deleted",
1105 * if this route delete was permitted then we need to check
1106 * there are any other external info which can still trigger
1107 * default route origination else flush it.
1109 thread_add_event(master
,
1110 ospf_external_lsa_default_routemap_timer
, ospf
,
1111 0, &ospf
->t_default_routemap_timer
);
1117 /* return 1 if external LSA must be originated, 0 otherwise */
1118 int ospf_redistribute_check(struct ospf
*ospf
, struct external_info
*ei
,
1121 struct route_map_set_values save_values
;
1122 struct prefix_ipv4
*p
= &ei
->p
;
1123 struct ospf_redist
*red
;
1124 uint8_t type
= is_default_prefix4(&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
1125 unsigned short instance
= is_default_prefix4(&ei
->p
) ? 0 : ei
->instance
;
1126 route_tag_t saved_tag
= 0;
1128 /* Default is handled differently. */
1129 if (type
== DEFAULT_ROUTE
)
1135 if (!ospf_external_lsa_originate_check(ospf
, ei
))
1138 /* Take care connected route. */
1139 if (type
== ZEBRA_ROUTE_CONNECT
1140 && !ospf_distribute_check_connected(ospf
, ei
))
1143 if (!DEFAULT_ROUTE_TYPE(type
) && DISTRIBUTE_NAME(ospf
, type
))
1144 /* distirbute-list exists, but access-list may not? */
1145 if (DISTRIBUTE_LIST(ospf
, type
))
1146 if (access_list_apply(DISTRIBUTE_LIST(ospf
, type
), p
)
1148 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
1150 "Redistribute[%s]: %pFX filtered by distribute-list.",
1151 ospf_redist_string(type
), p
);
1155 save_values
= ei
->route_map_set
;
1156 ospf_reset_route_map_set_values(&ei
->route_map_set
);
1158 saved_tag
= ei
->tag
;
1159 /* Resetting with original route tag */
1160 ei
->tag
= ei
->orig_tag
;
1162 /* apply route-map if needed */
1163 red
= ospf_redist_lookup(ospf
, type
, instance
);
1164 if (red
&& ROUTEMAP_NAME(red
)) {
1165 route_map_result_t ret
;
1167 ret
= route_map_apply(ROUTEMAP(red
), (struct prefix
*)p
, ei
);
1169 if (ret
== RMAP_DENYMATCH
) {
1170 ei
->route_map_set
= save_values
;
1171 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
1173 "Redistribute[%s]: %pFX filtered by route-map.",
1174 ospf_redist_string(type
), p
);
1178 /* check if 'route-map set' changed something */
1180 *changed
= !ospf_route_map_set_compare(
1181 &ei
->route_map_set
, &save_values
);
1183 /* check if tag is modified */
1184 *changed
|= (saved_tag
!= ei
->tag
);
1191 /* OSPF route-map set for redistribution */
1192 void ospf_routemap_set(struct ospf_redist
*red
, const char *name
)
1194 if (ROUTEMAP_NAME(red
)) {
1195 route_map_counter_decrement(ROUTEMAP(red
));
1196 free(ROUTEMAP_NAME(red
));
1199 ROUTEMAP_NAME(red
) = strdup(name
);
1200 ROUTEMAP(red
) = route_map_lookup_by_name(name
);
1201 route_map_counter_increment(ROUTEMAP(red
));
1204 void ospf_routemap_unset(struct ospf_redist
*red
)
1206 if (ROUTEMAP_NAME(red
)) {
1207 route_map_counter_decrement(ROUTEMAP(red
));
1208 free(ROUTEMAP_NAME(red
));
1211 ROUTEMAP_NAME(red
) = NULL
;
1212 ROUTEMAP(red
) = NULL
;
1215 static int ospf_zebra_gr_update(struct ospf
*ospf
, int command
,
1216 uint32_t stale_time
)
1218 struct zapi_cap api
;
1220 if (!zclient
|| zclient
->sock
< 0 || !ospf
)
1223 memset(&api
, 0, sizeof(struct zapi_cap
));
1225 api
.stale_removal_time
= stale_time
;
1226 api
.vrf_id
= ospf
->vrf_id
;
1228 (void)zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
,
1234 int ospf_zebra_gr_enable(struct ospf
*ospf
, uint32_t stale_time
)
1236 return ospf_zebra_gr_update(ospf
, ZEBRA_CLIENT_GR_CAPABILITIES
,
1240 int ospf_zebra_gr_disable(struct ospf
*ospf
)
1242 return ospf_zebra_gr_update(ospf
, ZEBRA_CLIENT_GR_DISABLE
, 0);
1245 /* Zebra route add and delete treatment. */
1246 static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS
)
1248 struct zapi_route api
;
1249 struct prefix_ipv4 p
;
1250 unsigned long ifindex
;
1251 struct in_addr nexthop
;
1252 struct external_info
*ei
;
1257 ospf
= ospf_lookup_by_vrf_id(vrf_id
);
1261 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
1264 ifindex
= api
.nexthops
[0].ifindex
;
1265 nexthop
= api
.nexthops
[0].gate
.ipv4
;
1268 memcpy(&p
, &api
.prefix
, sizeof(p
));
1269 if (IPV4_NET127(ntohl(p
.prefix
.s_addr
)))
1272 /* Re-destributed route is default route.
1273 * Here, route type is used as 'ZEBRA_ROUTE_KERNEL' for
1274 * updating ex-info. But in resetting (no default-info
1275 * originate)ZEBRA_ROUTE_MAX is used to delete the ex-info.
1276 * Resolved this inconsistency by maintaining same route type.
1278 if (is_default_prefix4(&p
))
1279 rt_type
= DEFAULT_ROUTE
;
1281 if (IS_DEBUG_OSPF(zebra
, ZEBRA_REDISTRIBUTE
))
1282 zlog_debug("%s: cmd %s from client %s: vrf_id %d, p %pFX",
1283 __func__
, zserv_command_string(cmd
),
1284 zebra_route_string(api
.type
), vrf_id
, &api
.prefix
);
1286 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
) {
1287 /* XXX|HACK|TODO|FIXME:
1288 * Maybe we should ignore reject/blackhole routes? Testing
1289 * shows that there is no problems though and this is only way
1290 * to "summarize" routes in ASBR at the moment. Maybe we need
1291 * just a better generalised solution for these types?
1294 /* Protocol tag overwrites all other tag value sent by zebra */
1295 if (ospf
->dtag
[rt_type
] > 0)
1296 api
.tag
= ospf
->dtag
[rt_type
];
1299 * Given zebra sends update for a prefix via ADD message, it
1301 * be considered as an implicit DEL for that prefix with other
1305 for (i
= 0; i
<= ZEBRA_ROUTE_MAX
; i
++)
1307 ospf_external_info_delete(ospf
, i
, api
.instance
,
1310 ei
= ospf_external_info_add(ospf
, rt_type
, api
.instance
, p
,
1311 ifindex
, nexthop
, api
.tag
);
1313 /* Nothing has changed, so nothing to do; return */
1316 if (ospf
->router_id
.s_addr
!= INADDR_ANY
) {
1317 if (is_default_prefix4(&p
))
1318 ospf_external_lsa_refresh_default(ospf
);
1320 struct ospf_external_aggr_rt
*aggr
;
1321 struct as_external_lsa
*al
;
1322 struct ospf_lsa
*lsa
= NULL
;
1323 struct in_addr mask
;
1325 aggr
= ospf_external_aggr_match(ospf
, &ei
->p
);
1328 /* Check the AS-external-LSA
1329 * should be originated.
1331 if (!ospf_redistribute_check(ospf
, ei
,
1335 if (IS_DEBUG_OSPF(lsa
, EXTNL_LSA_AGGR
))
1337 "%s: Send Aggreate LSA (%pI4/%d)",
1342 ospf_originate_summary_lsa(ospf
, aggr
,
1345 /* Handling the case where the
1346 * external route prefix
1347 * and aggegate prefix is same
1348 * If same dont flush the
1353 (struct prefix
*)&aggr
->p
,
1354 (struct prefix
*)&ei
->p
))
1357 lsa
= ospf_external_info_find_lsa(
1361 al
= (struct as_external_lsa
*)
1363 masklen2ip(ei
->p
.prefixlen
,
1370 ospf_external_lsa_flush(
1371 ospf
, ei
->type
, &ei
->p
,
1375 struct ospf_lsa
*current
;
1377 current
= ospf_external_info_find_lsa(
1385 if (!ospf_redistribute_check(
1389 ospf_external_lsa_originate(
1394 ZEBRA_REDISTRIBUTE
))
1396 "%s: %pI4 refreshing LSA",
1399 ospf_external_lsa_refresh(
1409 * Check if default-information originate is
1410 * with some routemap prefix/access list match.
1412 ospf_external_lsa_default_routemap_apply(ospf
, ei
, cmd
);
1414 } else { /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
1415 struct ospf_external_aggr_rt
*aggr
;
1417 ei
= ospf_external_info_lookup(ospf
, rt_type
, api
.instance
, &p
);
1422 * Check if default-information originate i
1423 * with some routemap prefix/access list match.
1424 * Apply before ei is deleted.
1426 ospf_external_lsa_default_routemap_apply(ospf
, ei
, cmd
);
1428 aggr
= ospf_external_aggr_match(ospf
, &ei
->p
);
1430 if (aggr
&& (ei
->aggr_route
== aggr
)) {
1431 ospf_unlink_ei_from_aggr(ospf
, aggr
, ei
);
1433 ospf_external_info_delete(ospf
, rt_type
, api
.instance
,
1436 ospf_external_info_delete(ospf
, rt_type
, api
.instance
,
1439 if (is_default_prefix4(&p
))
1440 ospf_external_lsa_refresh_default(ospf
);
1442 ospf_external_lsa_flush(ospf
, rt_type
, &p
,
1443 ifindex
/*, nexthop */);
1451 int ospf_distribute_list_out_set(struct ospf
*ospf
, int type
, const char *name
)
1453 /* Lookup access-list for distribute-list. */
1454 DISTRIBUTE_LIST(ospf
, type
) = access_list_lookup(AFI_IP
, name
);
1456 /* Clear previous distribute-name. */
1457 if (DISTRIBUTE_NAME(ospf
, type
))
1458 free(DISTRIBUTE_NAME(ospf
, type
));
1460 /* Set distribute-name. */
1461 DISTRIBUTE_NAME(ospf
, type
) = strdup(name
);
1463 /* If access-list have been set, schedule update timer. */
1464 if (DISTRIBUTE_LIST(ospf
, type
))
1465 ospf_distribute_list_update(ospf
, type
, 0);
1470 int ospf_distribute_list_out_unset(struct ospf
*ospf
, int type
,
1473 /* Schedule update timer. */
1474 if (DISTRIBUTE_LIST(ospf
, type
))
1475 ospf_distribute_list_update(ospf
, type
, 0);
1477 /* Unset distribute-list. */
1478 DISTRIBUTE_LIST(ospf
, type
) = NULL
;
1480 /* Clear distribute-name. */
1481 if (DISTRIBUTE_NAME(ospf
, type
))
1482 free(DISTRIBUTE_NAME(ospf
, type
));
1484 DISTRIBUTE_NAME(ospf
, type
) = NULL
;
1489 /* distribute-list update timer. */
1490 static int ospf_distribute_list_update_timer(struct thread
*thread
)
1492 struct route_node
*rn
;
1493 struct external_info
*ei
;
1494 struct route_table
*rt
;
1495 struct ospf_lsa
*lsa
;
1496 int type
, default_refresh
= 0, arg_type
;
1497 struct ospf
*ospf
= NULL
;
1498 void **arg
= THREAD_ARG(thread
);
1500 ospf
= (struct ospf
*)arg
[0];
1501 arg_type
= (int)(intptr_t)arg
[1];
1506 ospf
->t_distribute_update
= NULL
;
1508 zlog_info("Zebra[Redistribute]: distribute-list update timer fired!");
1510 if (IS_DEBUG_OSPF_EVENT
) {
1512 "%s: ospf distribute-list update arg_type %d vrf %s id %d",
1513 __func__
, arg_type
, ospf_vrf_id_to_name(ospf
->vrf_id
),
1517 /* foreach all external info. */
1518 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
1519 struct list
*ext_list
;
1520 struct listnode
*node
;
1521 struct ospf_external
*ext
;
1523 ext_list
= ospf
->external
[type
];
1527 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
1528 rt
= ext
->external_info
;
1531 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
1536 if (is_default_prefix4(&ei
->p
))
1537 default_refresh
= 1;
1539 struct ospf_external_aggr_rt
*aggr
;
1541 aggr
= ospf_external_aggr_match(ospf
,
1546 * should be originated.
1548 if (!ospf_redistribute_check(
1551 ospf_unlink_ei_from_aggr(
1560 "%s: Send Aggregate LSA (%pI4/%d)",
1565 /* Originate Aggregate
1568 ospf_originate_summary_lsa(
1571 (lsa
= ospf_external_info_find_lsa(
1574 LSA_REFRESH_IF_CHANGED
;
1575 /* If this is a MaxAge
1578 * because distribute
1579 * settings might have
1581 * this LSA needs to be
1582 * originated, not be
1585 * refresh it, it will
1586 * remain a MaxAge LSA
1587 * because it will look
1589 * changed. Neighbors
1591 * updates for this LSA.
1593 if (IS_LSA_MAXAGE(lsa
))
1594 force
= LSA_REFRESH_FORCE
;
1596 ospf_external_lsa_refresh(
1597 ospf
, lsa
, ei
, force
,
1600 if (!ospf_redistribute_check(
1603 ospf_external_lsa_originate(
1610 if (default_refresh
)
1611 ospf_external_lsa_refresh_default(ospf
);
1613 XFREE(MTYPE_OSPF_DIST_ARGS
, arg
);
1617 /* Update distribute-list and set timer to apply access-list. */
1618 void ospf_distribute_list_update(struct ospf
*ospf
, int type
,
1619 unsigned short instance
)
1621 struct ospf_external
*ext
;
1622 void **args
= XCALLOC(MTYPE_OSPF_DIST_ARGS
, sizeof(void *) * 2);
1625 args
[1] = (void *)((ptrdiff_t)type
);
1627 /* External info does not exist. */
1628 ext
= ospf_external_lookup(ospf
, type
, instance
);
1629 if (!ext
|| !EXTERNAL_INFO(ext
)) {
1630 XFREE(MTYPE_OSPF_DIST_ARGS
, args
);
1634 /* If exists previously invoked thread, then let it continue. */
1635 if (ospf
->t_distribute_update
) {
1636 XFREE(MTYPE_OSPF_DIST_ARGS
, args
);
1641 ospf
->t_distribute_update
= NULL
;
1642 thread_add_timer_msec(master
, ospf_distribute_list_update_timer
, args
,
1643 ospf
->min_ls_interval
,
1644 &ospf
->t_distribute_update
);
1647 /* If access-list is updated, apply some check. */
1648 static void ospf_filter_update(struct access_list
*access
)
1653 struct ospf_area
*area
;
1654 struct listnode
*node
, *n1
;
1656 /* If OSPF instance does not exist, return right now. */
1657 if (listcount(om
->ospf
) == 0)
1660 /* Iterate all ospf [VRF] instances */
1661 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
1662 /* Update distribute-list, and apply filter. */
1663 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
1664 struct list
*red_list
;
1665 struct ospf_redist
*red
;
1667 red_list
= ospf
->redist
[type
];
1669 for (ALL_LIST_ELEMENTS_RO(red_list
, node
,
1671 if (ROUTEMAP(red
)) {
1672 /* if route-map is not NULL it
1674 * using this access list */
1675 ospf_distribute_list_update(
1681 /* There is place for route-map for default-information
1682 * (ZEBRA_ROUTE_MAX),
1683 * but no distribute list. */
1684 if (type
== ZEBRA_ROUTE_MAX
)
1687 if (DISTRIBUTE_NAME(ospf
, type
)) {
1688 /* Keep old access-list for distribute-list. */
1689 struct access_list
*old
=
1690 DISTRIBUTE_LIST(ospf
, type
);
1692 /* Update access-list for distribute-list. */
1693 DISTRIBUTE_LIST(ospf
, type
) =
1696 DISTRIBUTE_NAME(ospf
, type
));
1698 /* No update for this distribute type. */
1700 && DISTRIBUTE_LIST(ospf
, type
) == NULL
)
1703 /* Schedule distribute-list update timer. */
1704 if (DISTRIBUTE_LIST(ospf
, type
) == NULL
1705 || strcmp(DISTRIBUTE_NAME(ospf
, type
),
1708 ospf_distribute_list_update(ospf
, type
,
1713 /* Update Area access-list. */
1714 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, node
, area
)) {
1715 if (EXPORT_NAME(area
)) {
1716 EXPORT_LIST(area
) = NULL
;
1720 if (IMPORT_NAME(area
)) {
1721 IMPORT_LIST(area
) = NULL
;
1726 /* Schedule ABR tasks -- this will be changed -- takada. */
1727 if (IS_OSPF_ABR(ospf
) && abr_inv
)
1728 ospf_schedule_abr_task(ospf
);
1732 /* If prefix-list is updated, do some updates. */
1733 void ospf_prefix_list_update(struct prefix_list
*plist
)
1735 struct ospf
*ospf
= NULL
;
1738 struct ospf_area
*area
;
1739 struct listnode
*node
, *n1
;
1741 /* If OSPF instatnce does not exist, return right now. */
1742 if (listcount(om
->ospf
) == 0)
1745 /* Iterate all ospf [VRF] instances */
1746 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
1748 /* Update all route-maps which are used
1749 * as redistribution filters.
1750 * They might use prefix-list.
1752 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
1753 struct list
*red_list
;
1754 struct ospf_redist
*red
;
1756 red_list
= ospf
->redist
[type
];
1760 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
1761 if (ROUTEMAP(red
)) {
1762 /* if route-map is not NULL
1764 * this prefix list */
1765 ospf_distribute_list_update(
1766 ospf
, type
, red
->instance
);
1771 /* Update area filter-lists. */
1772 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, node
, area
)) {
1773 /* Update filter-list in. */
1774 if (PREFIX_NAME_IN(area
)
1775 && strcmp(PREFIX_NAME_IN(area
),
1776 prefix_list_name(plist
))
1778 PREFIX_LIST_IN(area
) = prefix_list_lookup(
1779 AFI_IP
, PREFIX_NAME_IN(area
));
1783 /* Update filter-list out. */
1784 if (PREFIX_NAME_OUT(area
)
1785 && strcmp(PREFIX_NAME_OUT(area
),
1786 prefix_list_name(plist
))
1788 PREFIX_LIST_IN(area
) = prefix_list_lookup(
1789 AFI_IP
, PREFIX_NAME_OUT(area
));
1794 /* Schedule ABR task. */
1795 if (IS_OSPF_ABR(ospf
) && abr_inv
)
1796 ospf_schedule_abr_task(ospf
);
1800 static struct ospf_distance
*ospf_distance_new(void)
1802 return XCALLOC(MTYPE_OSPF_DISTANCE
, sizeof(struct ospf_distance
));
1805 static void ospf_distance_free(struct ospf_distance
*odistance
)
1807 XFREE(MTYPE_OSPF_DISTANCE
, odistance
);
1810 int ospf_distance_set(struct vty
*vty
, struct ospf
*ospf
,
1811 const char *distance_str
, const char *ip_str
,
1812 const char *access_list_str
)
1815 struct prefix_ipv4 p
;
1817 struct route_node
*rn
;
1818 struct ospf_distance
*odistance
;
1820 ret
= str2prefix_ipv4(ip_str
, &p
);
1822 vty_out(vty
, "Malformed prefix\n");
1823 return CMD_WARNING_CONFIG_FAILED
;
1826 distance
= atoi(distance_str
);
1828 /* Get OSPF distance node. */
1829 rn
= route_node_get(ospf
->distance_table
, (struct prefix
*)&p
);
1831 odistance
= rn
->info
;
1832 route_unlock_node(rn
);
1834 odistance
= ospf_distance_new();
1835 rn
->info
= odistance
;
1838 /* Set distance value. */
1839 odistance
->distance
= distance
;
1841 /* Reset access-list configuration. */
1842 if (odistance
->access_list
) {
1843 free(odistance
->access_list
);
1844 odistance
->access_list
= NULL
;
1846 if (access_list_str
)
1847 odistance
->access_list
= strdup(access_list_str
);
1852 int ospf_distance_unset(struct vty
*vty
, struct ospf
*ospf
,
1853 const char *distance_str
, const char *ip_str
,
1854 char const *access_list_str
)
1857 struct prefix_ipv4 p
;
1858 struct route_node
*rn
;
1859 struct ospf_distance
*odistance
;
1861 ret
= str2prefix_ipv4(ip_str
, &p
);
1863 vty_out(vty
, "Malformed prefix\n");
1864 return CMD_WARNING_CONFIG_FAILED
;
1867 rn
= route_node_lookup(ospf
->distance_table
, (struct prefix
*)&p
);
1869 vty_out(vty
, "Can't find specified prefix\n");
1870 return CMD_WARNING_CONFIG_FAILED
;
1873 odistance
= rn
->info
;
1875 if (odistance
->access_list
)
1876 free(odistance
->access_list
);
1877 ospf_distance_free(odistance
);
1880 route_unlock_node(rn
);
1881 route_unlock_node(rn
);
1886 void ospf_distance_reset(struct ospf
*ospf
)
1888 struct route_node
*rn
;
1889 struct ospf_distance
*odistance
;
1891 for (rn
= route_top(ospf
->distance_table
); rn
; rn
= route_next(rn
)) {
1892 odistance
= rn
->info
;
1896 if (odistance
->access_list
)
1897 free(odistance
->access_list
);
1898 ospf_distance_free(odistance
);
1900 route_unlock_node(rn
);
1904 uint8_t ospf_distance_apply(struct ospf
*ospf
, struct prefix_ipv4
*p
,
1905 struct ospf_route
* or)
1911 if (ospf
->distance_intra
&& or->path_type
== OSPF_PATH_INTRA_AREA
)
1912 return ospf
->distance_intra
;
1914 if (ospf
->distance_inter
&& or->path_type
== OSPF_PATH_INTER_AREA
)
1915 return ospf
->distance_inter
;
1917 if (ospf
->distance_external
1918 && (or->path_type
== OSPF_PATH_TYPE1_EXTERNAL
||
1919 or->path_type
== OSPF_PATH_TYPE2_EXTERNAL
))
1920 return ospf
->distance_external
;
1922 if (ospf
->distance_all
)
1923 return ospf
->distance_all
;
1928 void ospf_zebra_vrf_register(struct ospf
*ospf
)
1930 if (!zclient
|| zclient
->sock
< 0 || !ospf
)
1933 if (ospf
->vrf_id
!= VRF_UNKNOWN
) {
1934 if (IS_DEBUG_OSPF_EVENT
)
1935 zlog_debug("%s: Register VRF %s id %u", __func__
,
1936 ospf_vrf_id_to_name(ospf
->vrf_id
),
1938 zclient_send_reg_requests(zclient
, ospf
->vrf_id
);
1942 void ospf_zebra_vrf_deregister(struct ospf
*ospf
)
1944 if (!zclient
|| zclient
->sock
< 0 || !ospf
)
1947 if (ospf
->vrf_id
!= VRF_DEFAULT
&& ospf
->vrf_id
!= VRF_UNKNOWN
) {
1948 if (IS_DEBUG_OSPF_EVENT
)
1949 zlog_debug("%s: De-Register VRF %s id %u to Zebra.",
1950 __func__
, ospf_vrf_id_to_name(ospf
->vrf_id
),
1952 /* Deregister for router-id, interfaces,
1953 * redistributed routes. */
1954 zclient_send_dereg_requests(zclient
, ospf
->vrf_id
);
1958 /* Label Manager Functions */
1961 * Check if Label Manager is Ready or not.
1963 * @return True if Label Manager is ready, False otherwise
1965 bool ospf_zebra_label_manager_ready(void)
1967 return (zclient_sync
->sock
> 0);
1971 * Request Label Range to the Label Manager.
1973 * @param base base label of the label range to request
1974 * @param chunk_size size of the label range to request
1976 * @return 0 on success, -1 on failure
1978 int ospf_zebra_request_label_range(uint32_t base
, uint32_t chunk_size
)
1981 uint32_t start
, end
;
1983 if (zclient_sync
->sock
< 0)
1986 ret
= lm_get_label_chunk(zclient_sync
, 0, base
, chunk_size
, &start
,
1989 zlog_warn("%s: error getting label range!", __func__
);
1997 * Release Label Range to the Label Manager.
1999 * @param start start of label range to release
2000 * @param end end of label range to release
2002 * @return 0 on success, -1 otherwise
2004 int ospf_zebra_release_label_range(uint32_t start
, uint32_t end
)
2008 if (zclient_sync
->sock
< 0)
2011 ret
= lm_release_label_chunk(zclient_sync
, start
, end
);
2013 zlog_warn("%s: error releasing label range!", __func__
);
2021 * Connect to the Label Manager.
2023 * @return 0 on success, -1 otherwise
2025 int ospf_zebra_label_manager_connect(void)
2027 /* Connect to label manager. */
2028 if (zclient_socket_connect(zclient_sync
) < 0) {
2029 zlog_warn("%s: failed connecting synchronous zclient!",
2033 /* make socket non-blocking */
2034 set_nonblocking(zclient_sync
->sock
);
2036 /* Send hello to notify zebra this is a synchronous client */
2037 if (zclient_send_hello(zclient_sync
) == ZCLIENT_SEND_FAILURE
) {
2038 zlog_warn("%s: failed sending hello for synchronous zclient!",
2040 close(zclient_sync
->sock
);
2041 zclient_sync
->sock
= -1;
2045 /* Connect to label manager */
2046 if (lm_label_manager_connect(zclient_sync
, 0) != 0) {
2047 zlog_warn("%s: failed connecting to label manager!", __func__
);
2048 if (zclient_sync
->sock
> 0) {
2049 close(zclient_sync
->sock
);
2050 zclient_sync
->sock
= -1;
2055 osr_debug("SR (%s): Successfully connected to the Label Manager",
2061 static void ospf_zebra_connected(struct zclient
*zclient
)
2063 /* Send the client registration */
2064 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
2066 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
2070 * opaque messages between processes
2072 static int ospf_opaque_msg_handler(ZAPI_CALLBACK_ARGS
)
2075 struct zapi_opaque_msg info
;
2076 struct ldp_igp_sync_if_state state
;
2077 struct ldp_igp_sync_announce announce
;
2078 struct zapi_opaque_reg_info dst
;
2083 if (zclient_opaque_decode(s
, &info
) != 0)
2086 switch (info
.type
) {
2087 case LINK_STATE_SYNC
:
2088 STREAM_GETC(s
, dst
.proto
);
2089 STREAM_GETW(s
, dst
.instance
);
2090 STREAM_GETL(s
, dst
.session_id
);
2091 dst
.type
= LINK_STATE_SYNC
;
2092 ret
= ospf_te_sync_ted(dst
);
2094 case LDP_IGP_SYNC_IF_STATE_UPDATE
:
2095 STREAM_GET(&state
, s
, sizeof(state
));
2096 ret
= ospf_ldp_sync_state_update(state
);
2098 case LDP_IGP_SYNC_ANNOUNCE_UPDATE
:
2099 STREAM_GET(&announce
, s
, sizeof(announce
));
2100 ret
= ospf_ldp_sync_announce_update(announce
);
2111 static int ospf_zebra_client_close_notify(ZAPI_CALLBACK_ARGS
)
2115 struct zapi_client_close_info info
;
2117 if (zapi_client_close_notify_decode(zclient
->ibuf
, &info
) < 0)
2120 ospf_ldp_sync_handle_client_close(&info
);
2125 void ospf_zebra_init(struct thread_master
*master
, unsigned short instance
)
2127 /* Allocate zebra structure. */
2128 zclient
= zclient_new(master
, &zclient_options_default
);
2129 zclient_init(zclient
, ZEBRA_ROUTE_OSPF
, instance
, &ospfd_privs
);
2130 zclient
->zebra_connected
= ospf_zebra_connected
;
2131 zclient
->router_id_update
= ospf_router_id_update_zebra
;
2132 zclient
->interface_address_add
= ospf_interface_address_add
;
2133 zclient
->interface_address_delete
= ospf_interface_address_delete
;
2134 zclient
->interface_link_params
= ospf_interface_link_params
;
2135 zclient
->interface_vrf_update
= ospf_interface_vrf_update
;
2137 zclient
->redistribute_route_add
= ospf_zebra_read_route
;
2138 zclient
->redistribute_route_del
= ospf_zebra_read_route
;
2140 /* Initialize special zclient for synchronous message exchanges. */
2141 struct zclient_options options
= zclient_options_default
;
2142 options
.synchronous
= true;
2143 zclient_sync
= zclient_new(master
, &options
);
2144 zclient_sync
->sock
= -1;
2145 zclient_sync
->redist_default
= ZEBRA_ROUTE_OSPF
;
2146 zclient_sync
->instance
= instance
;
2148 * session_id must be different from default value (0) to distinguish
2149 * the asynchronous socket from the synchronous one
2151 zclient_sync
->session_id
= 1;
2152 zclient_sync
->privs
= &ospfd_privs
;
2154 access_list_add_hook(ospf_filter_update
);
2155 access_list_delete_hook(ospf_filter_update
);
2156 prefix_list_add_hook(ospf_prefix_list_update
);
2157 prefix_list_delete_hook(ospf_prefix_list_update
);
2159 zclient
->opaque_msg_handler
= ospf_opaque_msg_handler
;
2161 zclient
->zebra_client_close_notify
= ospf_zebra_client_close_notify
;
2164 void ospf_zebra_send_arp(const struct interface
*ifp
, const struct prefix
*p
)
2166 zclient_send_neigh_discovery_req(zclient
, ifp
, p
);