1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* OSPF version 2 daemon program.
3 * Copyright (C) 1999, 2000 Toshiaki Takada
18 #include "sockunion.h" /* for inet_aton () */
26 #include "lib_errors.h"
29 #include "ospfd/ospfd.h"
30 #include "ospfd/ospf_bfd.h"
31 #include "ospfd/ospf_network.h"
32 #include "ospfd/ospf_interface.h"
33 #include "ospfd/ospf_ism.h"
34 #include "ospfd/ospf_asbr.h"
35 #include "ospfd/ospf_lsa.h"
36 #include "ospfd/ospf_lsdb.h"
37 #include "ospfd/ospf_neighbor.h"
38 #include "ospfd/ospf_nsm.h"
39 #include "ospfd/ospf_spf.h"
40 #include "ospfd/ospf_packet.h"
41 #include "ospfd/ospf_dump.h"
42 #include "ospfd/ospf_route.h"
43 #include "ospfd/ospf_zebra.h"
44 #include "ospfd/ospf_abr.h"
45 #include "ospfd/ospf_flood.h"
46 #include "ospfd/ospf_ase.h"
47 #include "ospfd/ospf_ldp_sync.h"
48 #include "ospfd/ospf_gr.h"
49 #include "ospfd/ospf_apiserver.h"
52 DEFINE_QOBJ_TYPE(ospf
);
54 /* OSPF process wide configuration. */
55 static struct ospf_master ospf_master
;
57 /* OSPF process wide configuration pointer to export. */
58 struct ospf_master
*om
;
60 unsigned short ospf_instance
;
62 extern struct zclient
*zclient
;
65 static void ospf_remove_vls_through_area(struct ospf
*, struct ospf_area
*);
66 static void ospf_network_free(struct ospf
*, struct ospf_network
*);
67 static void ospf_area_free(struct ospf_area
*);
68 static void ospf_network_run(struct prefix
*, struct ospf_area
*);
69 static void ospf_network_run_interface(struct ospf
*, struct interface
*,
70 struct prefix
*, struct ospf_area
*);
71 static void ospf_network_run_subnet(struct ospf
*, struct connected
*,
72 struct prefix
*, struct ospf_area
*);
73 static int ospf_network_match_iface(const struct connected
*,
74 const struct prefix
*);
75 static void ospf_finish_final(struct ospf
*);
77 /* API to clean refresh queues and LSAs */
78 static void ospf_free_refresh_queue(struct ospf
*ospf
)
80 for (int i
= 0; i
< OSPF_LSA_REFRESHER_SLOTS
; i
++) {
81 struct list
*list
= ospf
->lsa_refresh_queue
.qs
[i
];
82 struct listnode
*node
, *nnode
;
86 for (ALL_LIST_ELEMENTS(list
, node
, nnode
, lsa
)) {
87 listnode_delete(list
, lsa
);
88 lsa
->refresh_list
= -1;
89 ospf_lsa_unlock(&lsa
);
92 ospf
->lsa_refresh_queue
.qs
[i
] = NULL
;
96 #define OSPF_EXTERNAL_LSA_ORIGINATE_DELAY 1
98 int p_spaces_compare_func(const struct p_space
*a
, const struct p_space
*b
)
100 if (a
->protected_resource
->type
== OSPF_TI_LFA_LINK_PROTECTION
101 && b
->protected_resource
->type
== OSPF_TI_LFA_LINK_PROTECTION
)
102 return (a
->protected_resource
->link
->link_id
.s_addr
103 - b
->protected_resource
->link
->link_id
.s_addr
);
105 if (a
->protected_resource
->type
== OSPF_TI_LFA_NODE_PROTECTION
106 && b
->protected_resource
->type
== OSPF_TI_LFA_NODE_PROTECTION
)
107 return (a
->protected_resource
->router_id
.s_addr
108 - b
->protected_resource
->router_id
.s_addr
);
110 /* This should not happen */
114 int q_spaces_compare_func(const struct q_space
*a
, const struct q_space
*b
)
116 return (a
->root
->id
.s_addr
- b
->root
->id
.s_addr
);
119 DECLARE_RBTREE_UNIQ(p_spaces
, struct p_space
, p_spaces_item
,
120 p_spaces_compare_func
);
122 void ospf_process_refresh_data(struct ospf
*ospf
, bool reset
)
124 struct vrf
*vrf
= vrf_lookup_by_id(ospf
->vrf_id
);
125 struct in_addr router_id
, router_id_old
;
126 struct ospf_interface
*oi
;
127 struct interface
*ifp
;
128 struct listnode
*node
, *nnode
;
129 struct ospf_area
*area
;
130 bool rid_change
= false;
132 if (!ospf
->oi_running
) {
133 if (IS_DEBUG_OSPF_EVENT
)
135 "Router ospf not configured -- Router-ID update postponed");
139 if (IS_DEBUG_OSPF_EVENT
)
140 zlog_debug("Router-ID[OLD:%pI4]: Update",
143 router_id_old
= ospf
->router_id
;
145 /* Select the router ID based on these priorities:
146 1. Statically assigned router ID is always the first choice.
147 2. If there is no statically assigned router ID, then try to stick
148 with the most recent value, since changing router ID's is very
150 3. Last choice: just go with whatever the zebra daemon recommends.
152 if (ospf
->router_id_static
.s_addr
!= INADDR_ANY
)
153 router_id
= ospf
->router_id_static
;
154 else if (ospf
->router_id
.s_addr
!= INADDR_ANY
)
155 router_id
= ospf
->router_id
;
157 router_id
= ospf
->router_id_zebra
;
159 if (IS_DEBUG_OSPF_EVENT
)
160 zlog_debug("Router-ID[OLD:%pI4]: Update to %pI4",
161 &ospf
->router_id
, &router_id
);
163 rid_change
= !(IPV4_ADDR_SAME(&router_id_old
, &router_id
));
164 if (rid_change
|| (reset
)) {
165 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
166 /* Some nbrs are identified by router_id, these needs
167 * to be rebuilt. Possible optimization would be to do
168 * oi->nbr_self->router_id = router_id for
169 * !(virtual | ptop) links
171 ospf_nbr_self_reset(oi
, router_id
);
174 * If the old router id was not set, but now it
175 * is and the interface is operative and the
176 * state is ISM_Down we should kick the state
177 * machine as that we processed the interfaces
178 * based upon the network statement( or intf config )
179 * but could not start it at that time.
181 if (if_is_operative(oi
->ifp
) && oi
->state
== ISM_Down
182 && router_id_old
.s_addr
== INADDR_ANY
)
186 /* Flush (inline) all the self originated LSAs */
187 ospf_flush_self_originated_lsas_now(ospf
);
189 ospf
->router_id
= router_id
;
190 if (IS_DEBUG_OSPF_EVENT
)
191 zlog_debug("Router-ID[NEW:%pI4]: Update",
194 /* Flush (inline) all external LSAs which now match the new
196 need to adjust the OSPF_LSA_SELF flag, so the flush doesn't
198 asserts in ospf_refresher_unregister_lsa(). This step is
200 because the current frr code does look-up for
202 based on the self router-id alone but expects OSPF_LSA_SELF
206 struct route_node
*rn
;
207 struct ospf_lsa
*lsa
;
209 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
) {
210 /* AdvRouter and Router ID is the same. */
211 if (IPV4_ADDR_SAME(&lsa
->data
->adv_router
,
212 &ospf
->router_id
) && rid_change
) {
214 OSPF_LSA_SELF_CHECKED
);
215 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
216 ospf_lsa_flush_schedule(ospf
, lsa
);
218 /* The above flush will send immediately
219 * So discard the LSA to originate new
221 ospf_discard_from_db(ospf
, ospf
->lsdb
, lsa
);
224 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
225 ospf_discard_from_db(ospf
, ospf
->lsdb
, lsa
);
227 ospf_lsdb_delete_all(ospf
->lsdb
);
230 /* Since the LSAs are deleted, need reset the aggr flag */
231 ospf_unset_all_aggr_flag(ospf
);
233 /* Delete the LSDB */
234 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
))
235 ospf_area_lsdb_discard_delete(area
);
237 /* update router-lsa's for each area */
238 ospf_router_lsa_update(ospf
);
240 /* update ospf_interface's */
241 FOR_ALL_INTERFACES (vrf
, ifp
) {
245 ospf_if_update(ospf
, ifp
);
248 ospf_external_lsa_rid_change(ospf
);
250 #ifdef SUPPORT_OSPF_API
251 ospf_apiserver_clients_notify_router_id_change(router_id
);
255 ospf
->inst_shutdown
= 0;
258 void ospf_router_id_update(struct ospf
*ospf
)
260 ospf_process_refresh_data(ospf
, false);
263 void ospf_process_reset(struct ospf
*ospf
)
265 ospf_process_refresh_data(ospf
, true);
268 void ospf_neighbor_reset(struct ospf
*ospf
, struct in_addr nbr_id
,
271 struct route_node
*rn
;
272 struct ospf_neighbor
*nbr
;
273 struct ospf_interface
*oi
;
274 struct listnode
*node
;
276 /* Clear only a particular nbr with nbr router id as nbr_id */
277 if (nbr_str
!= NULL
) {
278 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
279 nbr
= ospf_nbr_lookup_by_routerid(oi
->nbrs
, &nbr_id
);
281 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_KillNbr
);
286 /* send Neighbor event KillNbr to all associated neighbors. */
287 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
288 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
290 if (nbr
&& (nbr
!= oi
->nbr_self
))
291 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_KillNbr
);
296 /* For OSPF area sort by area id. */
297 static int ospf_area_id_cmp(struct ospf_area
*a1
, struct ospf_area
*a2
)
299 if (ntohl(a1
->area_id
.s_addr
) > ntohl(a2
->area_id
.s_addr
))
301 if (ntohl(a1
->area_id
.s_addr
) < ntohl(a2
->area_id
.s_addr
))
306 static void ospf_add(struct ospf
*ospf
)
308 listnode_add(om
->ospf
, ospf
);
311 static void ospf_delete(struct ospf
*ospf
)
313 listnode_delete(om
->ospf
, ospf
);
316 struct ospf
*ospf_new_alloc(unsigned short instance
, const char *name
)
319 struct vrf
*vrf
= NULL
;
321 struct ospf
*new = XCALLOC(MTYPE_OSPF_TOP
, sizeof(struct ospf
));
323 new->instance
= instance
;
324 new->router_id
.s_addr
= htonl(0);
325 new->router_id_static
.s_addr
= htonl(0);
327 vrf
= vrf_lookup_by_name(name
);
329 new->vrf_id
= vrf
->vrf_id
;
331 new->vrf_id
= VRF_UNKNOWN
;
333 /* Freed in ospf_finish_final */
334 new->name
= XSTRDUP(MTYPE_OSPF_TOP
, name
);
335 if (IS_DEBUG_OSPF_EVENT
)
337 "%s: Create new ospf instance with vrf_name %s vrf_id %u",
338 __func__
, name
, new->vrf_id
);
341 ospf_vrf_link(new, vrf
);
343 ospf_zebra_vrf_register(new);
345 new->abr_type
= OSPF_ABR_DEFAULT
;
346 new->oiflist
= list_new();
347 new->vlinks
= list_new();
348 new->areas
= list_new();
349 new->areas
->cmp
= (int (*)(void *, void *))ospf_area_id_cmp
;
350 new->networks
= route_table_init();
351 new->nbr_nbma
= route_table_init();
353 new->lsdb
= ospf_lsdb_new();
355 new->default_originate
= DEFAULT_ORIGINATE_NONE
;
357 new->passive_interface_default
= OSPF_IF_ACTIVE
;
359 new->new_external_route
= route_table_init();
360 new->old_external_route
= route_table_init();
361 new->external_lsas
= route_table_init();
363 new->stub_router_startup_time
= OSPF_STUB_ROUTER_UNCONFIGURED
;
364 new->stub_router_shutdown_time
= OSPF_STUB_ROUTER_UNCONFIGURED
;
365 new->stub_router_admin_set
= OSPF_STUB_ROUTER_ADMINISTRATIVE_UNSET
;
367 /* Distribute parameter init. */
368 for (i
= 0; i
<= ZEBRA_ROUTE_MAX
; i
++) {
371 new->default_metric
= -1;
372 new->ref_bandwidth
= OSPF_DEFAULT_REF_BANDWIDTH
;
375 new->min_ls_interval
= OSPF_MIN_LS_INTERVAL
;
376 new->min_ls_arrival
= OSPF_MIN_LS_ARRIVAL
;
378 /* SPF timer value init. */
379 new->spf_delay
= OSPF_SPF_DELAY_DEFAULT
;
380 new->spf_holdtime
= OSPF_SPF_HOLDTIME_DEFAULT
;
381 new->spf_max_holdtime
= OSPF_SPF_MAX_HOLDTIME_DEFAULT
;
382 new->spf_hold_multiplier
= 1;
385 new->maxage_delay
= OSPF_LSA_MAXAGE_REMOVE_DELAY_DEFAULT
;
386 new->maxage_lsa
= route_table_init();
387 new->t_maxage_walker
= NULL
;
388 event_add_timer(master
, ospf_lsa_maxage_walker
, new,
389 OSPF_LSA_MAXAGE_CHECK_INTERVAL
, &new->t_maxage_walker
);
391 /* Max paths initialization */
392 new->max_multipath
= MULTIPATH_NUM
;
394 /* Distance table init. */
395 new->distance_table
= route_table_init();
397 new->lsa_refresh_queue
.index
= 0;
398 new->lsa_refresh_interval
= OSPF_LSA_REFRESH_INTERVAL_DEFAULT
;
399 new->lsa_refresh_timer
= OSPF_LS_REFRESH_TIME
;
400 new->t_lsa_refresher
= NULL
;
401 event_add_timer(master
, ospf_lsa_refresh_walker
, new,
402 new->lsa_refresh_interval
, &new->t_lsa_refresher
);
403 new->lsa_refresher_started
= monotime(NULL
);
405 new->ibuf
= stream_new(OSPF_MAX_PACKET_SIZE
+ 1);
408 new->oi_write_q
= list_new();
409 new->write_oi_count
= OSPF_WRITE_INTERFACE_COUNT_DEFAULT
;
411 new->proactive_arp
= OSPF_PROACTIVE_ARP_DEFAULT
;
413 ospf_gr_helper_instance_init(new);
415 ospf_asbr_external_aggregator_init(new);
417 ospf_opaque_type11_lsa_init(new);
422 new->intf_socket_enabled
= true;
424 new->recv_sock_bufsize
= OSPF_DEFAULT_SOCK_BUFSIZE
;
425 new->send_sock_bufsize
= OSPF_DEFAULT_SOCK_BUFSIZE
;
430 /* Allocate new ospf structure. */
431 static struct ospf
*ospf_new(unsigned short instance
, const char *name
)
435 new = ospf_new_alloc(instance
, name
);
438 if (new->vrf_id
== VRF_UNKNOWN
)
441 if ((ospf_sock_init(new)) < 0) {
442 flog_err(EC_LIB_SOCKET
,
443 "%s: ospf_sock_init is unable to open a socket",
448 event_add_read(master
, ospf_read
, new, new->fd
, &new->t_read
);
451 ospf_router_id_update(new);
454 * Read from non-volatile memory whether this instance is performing a
455 * graceful restart or not.
457 ospf_gr_nvm_read(new);
459 new->fr_configured
= false;
464 struct ospf
*ospf_lookup_instance(unsigned short instance
)
467 struct listnode
*node
, *nnode
;
469 if (listcount(om
->ospf
) == 0)
472 for (ALL_LIST_ELEMENTS(om
->ospf
, node
, nnode
, ospf
))
473 if ((ospf
->instance
== 0 && instance
== 0)
474 || (ospf
->instance
&& instance
475 && ospf
->instance
== instance
))
481 static int ospf_is_ready(struct ospf
*ospf
)
483 /* OSPF must be on and Router-ID must be configured. */
484 if (!ospf
|| ospf
->router_id
.s_addr
== INADDR_ANY
)
490 struct ospf
*ospf_lookup_by_inst_name(unsigned short instance
, const char *name
)
492 struct ospf
*ospf
= NULL
;
493 struct listnode
*node
, *nnode
;
495 for (ALL_LIST_ELEMENTS(om
->ospf
, node
, nnode
, ospf
)) {
496 if ((ospf
->instance
== instance
)
497 && ((ospf
->name
== NULL
&& name
== NULL
)
498 || (ospf
->name
&& name
499 && strcmp(ospf
->name
, name
) == 0)))
505 struct ospf
*ospf_lookup(unsigned short instance
, const char *name
)
510 ospf
= ospf_lookup_instance(instance
);
512 ospf
= ospf_lookup_by_inst_name(instance
, name
);
518 struct ospf
*ospf_get(unsigned short instance
, const char *name
, bool *created
)
522 ospf
= ospf_lookup(instance
, name
);
524 *created
= (ospf
== NULL
);
526 ospf
= ospf_new(instance
, name
);
531 struct ospf
*ospf_lookup_by_vrf_id(vrf_id_t vrf_id
)
533 struct vrf
*vrf
= NULL
;
535 vrf
= vrf_lookup_by_id(vrf_id
);
538 return (vrf
->info
) ? (struct ospf
*)vrf
->info
: NULL
;
541 uint32_t ospf_count_area_params(struct ospf
*ospf
)
544 struct interface
*ifp
;
547 if (ospf
->vrf_id
!= VRF_UNKNOWN
) {
548 vrf
= vrf_lookup_by_id(ospf
->vrf_id
);
550 FOR_ALL_INTERFACES (vrf
, ifp
) {
551 count
+= ospf_if_count_area_params(ifp
);
558 /* It should only be used when processing incoming info update from zebra.
559 * Other situations, it is not sufficient to lookup the ospf instance by
560 * vrf_name only without using the instance number.
562 static struct ospf
*ospf_lookup_by_name(const char *vrf_name
)
564 struct ospf
*ospf
= NULL
;
565 struct listnode
*node
, *nnode
;
567 for (ALL_LIST_ELEMENTS(om
->ospf
, node
, nnode
, ospf
))
568 if ((ospf
->name
== NULL
&& vrf_name
== NULL
)
569 || (ospf
->name
&& vrf_name
570 && strcmp(ospf
->name
, vrf_name
) == 0))
575 /* Handle the second half of deferred shutdown. This is called either
576 * from the deferred-shutdown timer thread, or directly through
577 * ospf_deferred_shutdown_check.
579 * Function is to cleanup G-R state, if required then call ospf_finish_final
580 * to complete shutdown of this ospf instance. Possibly exit if the
581 * whole process is being shutdown and this was the last OSPF instance.
583 static void ospf_deferred_shutdown_finish(struct ospf
*ospf
)
585 ospf
->stub_router_shutdown_time
= OSPF_STUB_ROUTER_UNCONFIGURED
;
586 EVENT_OFF(ospf
->t_deferred_shutdown
);
588 ospf_finish_final(ospf
);
590 /* *ospf is now invalid */
592 /* ospfd being shut-down? If so, was this the last ospf instance? */
593 if (CHECK_FLAG(om
->options
, OSPF_MASTER_SHUTDOWN
)
594 && (listcount(om
->ospf
) == 0)) {
602 /* Timer thread for G-R */
603 static void ospf_deferred_shutdown_timer(struct event
*t
)
605 struct ospf
*ospf
= EVENT_ARG(t
);
607 ospf_deferred_shutdown_finish(ospf
);
610 /* Check whether deferred-shutdown must be scheduled, otherwise call
611 * down directly into second-half of instance shutdown.
613 static void ospf_deferred_shutdown_check(struct ospf
*ospf
)
615 unsigned long timeout
;
617 struct ospf_area
*area
;
619 /* deferred shutdown already running? */
620 if (ospf
->t_deferred_shutdown
)
623 /* Should we try push out max-metric LSAs? */
624 if (ospf
->stub_router_shutdown_time
!= OSPF_STUB_ROUTER_UNCONFIGURED
) {
625 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, ln
, area
)) {
626 SET_FLAG(area
->stub_router_state
,
627 OSPF_AREA_ADMIN_STUB_ROUTED
);
629 if (!CHECK_FLAG(area
->stub_router_state
,
630 OSPF_AREA_IS_STUB_ROUTED
))
631 ospf_router_lsa_update_area(area
);
633 timeout
= ospf
->stub_router_shutdown_time
;
635 /* No timer needed */
636 ospf_deferred_shutdown_finish(ospf
);
640 OSPF_TIMER_ON(ospf
->t_deferred_shutdown
, ospf_deferred_shutdown_timer
,
645 /* Shut down the entire process */
646 void ospf_terminate(void)
649 struct listnode
*node
, *nnode
;
651 /* shutdown already in progress */
652 if (CHECK_FLAG(om
->options
, OSPF_MASTER_SHUTDOWN
))
655 SET_FLAG(om
->options
, OSPF_MASTER_SHUTDOWN
);
657 /* Skip some steps if OSPF not actually running */
658 if (listcount(om
->ospf
) == 0)
661 for (ALL_LIST_ELEMENTS(om
->ospf
, node
, nnode
, ospf
))
665 ospf_gr_helper_stop();
667 /* Cleanup route maps */
670 /* reverse prefix_list_init */
671 prefix_list_add_hook(NULL
);
672 prefix_list_delete_hook(NULL
);
675 /* Cleanup vrf info */
676 ospf_vrf_terminate();
678 /* Deliberately go back up, hopefully to thread scheduler, as
679 * One or more ospf_finish()'s may have deferred shutdown to a timer
682 zclient_stop(zclient
);
683 zclient_free(zclient
);
689 void ospf_finish(struct ospf
*ospf
)
691 /* let deferred shutdown decide */
692 ospf_deferred_shutdown_check(ospf
);
694 /* if ospf_deferred_shutdown returns, then ospf_finish_final is
695 * deferred to expiry of G-S timer thread. Return back up, hopefully
696 * to thread scheduler.
701 /* Final cleanup of ospf instance */
702 static void ospf_finish_final(struct ospf
*ospf
)
704 struct vrf
*vrf
= vrf_lookup_by_id(ospf
->vrf_id
);
705 struct route_node
*rn
;
706 struct ospf_nbr_nbma
*nbr_nbma
;
707 struct ospf_lsa
*lsa
;
708 struct ospf_interface
*oi
;
709 struct ospf_area
*area
;
710 struct ospf_vl_data
*vl_data
;
711 struct listnode
*node
, *nnode
;
712 struct ospf_redist
*red
;
717 ospf_opaque_type11_lsa_term(ospf
);
719 ospf_opaque_finish();
721 if (!ospf
->gr_info
.prepare_in_progress
)
722 ospf_flush_self_originated_lsas_now(ospf
);
724 /* Unregister redistribution */
725 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
726 struct list
*red_list
;
728 red_list
= ospf
->redist
[i
];
732 for (ALL_LIST_ELEMENTS(red_list
, node
, nnode
, red
)) {
733 ospf_redistribute_unset(ospf
, i
, red
->instance
);
734 ospf_redist_del(ospf
, i
, red
->instance
);
737 red
= ospf_redist_lookup(ospf
, DEFAULT_ROUTE
, 0);
739 ospf_routemap_unset(red
);
740 ospf_redist_del(ospf
, DEFAULT_ROUTE
, 0);
741 ospf_redistribute_default_set(ospf
, DEFAULT_ORIGINATE_NONE
, 0, 0);
744 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
))
745 ospf_remove_vls_through_area(ospf
, area
);
747 for (ALL_LIST_ELEMENTS(ospf
->vlinks
, node
, nnode
, vl_data
))
748 ospf_vl_delete(ospf
, vl_data
);
750 list_delete(&ospf
->vlinks
);
752 /* shutdown LDP-Sync */
753 if (ospf
->vrf_id
== VRF_DEFAULT
)
754 ospf_ldp_sync_gbl_exit(ospf
, true);
756 /* Reset interface. */
757 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
))
759 list_delete(&ospf
->oiflist
);
760 ospf
->oi_running
= 0;
762 /* De-Register VRF */
763 ospf_zebra_vrf_deregister(ospf
);
765 /* Clear static neighbors */
766 for (rn
= route_top(ospf
->nbr_nbma
); rn
; rn
= route_next(rn
))
767 if ((nbr_nbma
= rn
->info
)) {
768 EVENT_OFF(nbr_nbma
->t_poll
);
771 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
772 nbr_nbma
->nbr
= NULL
;
776 listnode_delete(nbr_nbma
->oi
->nbr_nbma
,
781 XFREE(MTYPE_OSPF_NEIGHBOR_STATIC
, nbr_nbma
);
784 route_table_finish(ospf
->nbr_nbma
);
786 /* Clear networks and Areas. */
787 for (rn
= route_top(ospf
->networks
); rn
; rn
= route_next(rn
)) {
788 struct ospf_network
*network
;
790 if ((network
= rn
->info
) != NULL
) {
791 ospf_network_free(ospf
, network
);
793 route_unlock_node(rn
);
796 route_table_finish(ospf
->networks
);
798 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
799 listnode_delete(ospf
->areas
, area
);
800 ospf_area_free(area
);
803 /* Cancel all timers. */
804 EVENT_OFF(ospf
->t_read
);
805 EVENT_OFF(ospf
->t_write
);
806 EVENT_OFF(ospf
->t_spf_calc
);
807 EVENT_OFF(ospf
->t_ase_calc
);
808 EVENT_OFF(ospf
->t_maxage
);
809 EVENT_OFF(ospf
->t_maxage_walker
);
810 EVENT_OFF(ospf
->t_abr_task
);
811 EVENT_OFF(ospf
->t_abr_fr
);
812 EVENT_OFF(ospf
->t_asbr_check
);
813 EVENT_OFF(ospf
->t_asbr_nssa_redist_update
);
814 EVENT_OFF(ospf
->t_distribute_update
);
815 EVENT_OFF(ospf
->t_lsa_refresher
);
816 EVENT_OFF(ospf
->t_opaque_lsa_self
);
817 EVENT_OFF(ospf
->t_sr_update
);
818 EVENT_OFF(ospf
->t_default_routemap_timer
);
819 EVENT_OFF(ospf
->t_external_aggr
);
820 EVENT_OFF(ospf
->gr_info
.t_grace_period
);
822 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
823 ospf_discard_from_db(ospf
, ospf
->lsdb
, lsa
);
824 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
825 ospf_discard_from_db(ospf
, ospf
->lsdb
, lsa
);
827 ospf_lsdb_delete_all(ospf
->lsdb
);
828 ospf_lsdb_free(ospf
->lsdb
);
830 for (rn
= route_top(ospf
->maxage_lsa
); rn
; rn
= route_next(rn
)) {
831 if ((lsa
= rn
->info
) != NULL
) {
832 ospf_lsa_unlock(&lsa
);
834 route_unlock_node(rn
);
837 route_table_finish(ospf
->maxage_lsa
);
840 ospf_route_table_free(ospf
->old_table
);
841 if (ospf
->new_table
) {
842 if (!ospf
->gr_info
.prepare_in_progress
)
843 ospf_route_delete(ospf
, ospf
->new_table
);
844 ospf_route_table_free(ospf
->new_table
);
847 ospf_rtrs_free(ospf
->oall_rtrs
);
849 ospf_rtrs_free(ospf
->all_rtrs
);
851 ospf_rtrs_free(ospf
->old_rtrs
);
853 ospf_rtrs_free(ospf
->new_rtrs
);
854 if (ospf
->new_external_route
) {
855 if (!ospf
->gr_info
.prepare_in_progress
)
856 ospf_route_delete(ospf
, ospf
->new_external_route
);
857 ospf_route_table_free(ospf
->new_external_route
);
859 if (ospf
->old_external_route
) {
860 if (!ospf
->gr_info
.prepare_in_progress
)
861 ospf_route_delete(ospf
, ospf
->old_external_route
);
862 ospf_route_table_free(ospf
->old_external_route
);
864 if (ospf
->external_lsas
) {
865 ospf_ase_external_lsas_finish(ospf
->external_lsas
);
868 for (i
= ZEBRA_ROUTE_SYSTEM
; i
<= ZEBRA_ROUTE_MAX
; i
++) {
869 struct list
*ext_list
;
870 struct ospf_external
*ext
;
872 ext_list
= ospf
->external
[i
];
876 for (ALL_LIST_ELEMENTS(ext_list
, node
, nnode
, ext
)) {
877 if (ext
->external_info
)
878 for (rn
= route_top(ext
->external_info
); rn
;
879 rn
= route_next(rn
)) {
880 if (rn
->info
== NULL
)
883 XFREE(MTYPE_OSPF_EXTERNAL_INFO
,
886 route_unlock_node(rn
);
889 ospf_external_del(ospf
, i
, ext
->instance
);
893 ospf_distance_reset(ospf
);
894 route_table_finish(ospf
->distance_table
);
896 /* Release extrenal Aggregator table */
897 for (rn
= route_top(ospf
->rt_aggr_tbl
); rn
; rn
= route_next(rn
)) {
898 struct ospf_external_aggr_rt
*aggr
;
903 ospf_external_aggregator_free(aggr
);
905 route_unlock_node(rn
);
909 route_table_finish(ospf
->rt_aggr_tbl
);
912 ospf_free_refresh_queue(ospf
);
914 list_delete(&ospf
->areas
);
915 list_delete(&ospf
->oi_write_q
);
917 /* Reset GR helper data structers */
918 ospf_gr_helper_instance_stop(ospf
);
921 stream_free(ospf
->ibuf
);
923 ospf
->max_multipath
= MULTIPATH_NUM
;
927 ospf_vrf_unlink(ospf
, vrf
);
929 XFREE(MTYPE_OSPF_TOP
, ospf
->name
);
930 XFREE(MTYPE_OSPF_TOP
, ospf
);
934 /* allocate new OSPF Area object */
935 struct ospf_area
*ospf_area_new(struct ospf
*ospf
, struct in_addr area_id
)
937 struct ospf_area
*new;
939 /* Allocate new config_network. */
940 new = XCALLOC(MTYPE_OSPF_AREA
, sizeof(struct ospf_area
));
944 new->area_id
= area_id
;
945 new->area_id_fmt
= OSPF_AREA_ID_FMT_DOTTEDQUAD
;
947 new->external_routing
= OSPF_AREA_DEFAULT
;
948 new->default_cost
= 1;
949 new->auth_type
= OSPF_AUTH_NULL
;
952 new->lsdb
= ospf_lsdb_new();
954 /* Self-originated LSAs initialize. */
955 new->router_lsa_self
= NULL
;
957 /* Initialize FR field */
958 new->fr_info
.enabled
= false;
959 new->fr_info
.configured
= false;
960 new->fr_info
.state_changed
= false;
961 new->fr_info
.router_lsas_recv_dc_bit
= 0;
962 new->fr_info
.indication_lsa_self
= NULL
;
963 new->fr_info
.area_ind_lsa_recvd
= false;
964 new->fr_info
.area_dc_clear
= false;
966 ospf_opaque_type10_lsa_init(new);
968 new->oiflist
= list_new();
969 new->ranges
= route_table_init();
970 new->nssa_ranges
= route_table_init();
972 if (area_id
.s_addr
== OSPF_AREA_BACKBONE
)
973 ospf
->backbone
= new;
978 void ospf_area_lsdb_discard_delete(struct ospf_area
*area
)
980 struct route_node
*rn
;
981 struct ospf_lsa
*lsa
;
983 LSDB_LOOP (ROUTER_LSDB(area
), rn
, lsa
)
984 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
985 LSDB_LOOP (NETWORK_LSDB(area
), rn
, lsa
)
986 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
987 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
988 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
989 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
990 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
992 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
993 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
994 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
995 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
996 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
997 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
999 ospf_lsdb_delete_all(area
->lsdb
);
1002 static void ospf_area_free(struct ospf_area
*area
)
1004 ospf_opaque_type10_lsa_term(area
);
1007 ospf_area_lsdb_discard_delete(area
);
1009 ospf_lsdb_free(area
->lsdb
);
1011 ospf_lsa_unlock(&area
->router_lsa_self
);
1013 route_table_finish(area
->ranges
);
1014 route_table_finish(area
->nssa_ranges
);
1015 list_delete(&area
->oiflist
);
1017 if (EXPORT_NAME(area
))
1018 free(EXPORT_NAME(area
));
1020 if (IMPORT_NAME(area
))
1021 free(IMPORT_NAME(area
));
1024 EVENT_OFF(area
->t_stub_router
);
1025 EVENT_OFF(area
->t_opaque_lsa_self
);
1027 if (OSPF_IS_AREA_BACKBONE(area
))
1028 area
->ospf
->backbone
= NULL
;
1030 XFREE(MTYPE_OSPF_AREA
, area
);
1033 void ospf_area_check_free(struct ospf
*ospf
, struct in_addr area_id
)
1035 struct ospf_area
*area
;
1037 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1038 if (area
&& listcount(area
->oiflist
) == 0 &&
1039 area
->ranges
->top
== NULL
&& area
->nssa_ranges
->top
== NULL
&&
1040 !ospf_vl_count(ospf
, area
) &&
1041 area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
&&
1042 area
->external_routing
== OSPF_AREA_DEFAULT
&&
1043 area
->no_summary
== 0 && area
->default_cost
== 1 &&
1044 EXPORT_NAME(area
) == NULL
&& IMPORT_NAME(area
) == NULL
&&
1045 area
->auth_type
== OSPF_AUTH_NULL
) {
1046 listnode_delete(ospf
->areas
, area
);
1047 ospf_area_free(area
);
1051 struct ospf_area
*ospf_area_get(struct ospf
*ospf
, struct in_addr area_id
)
1053 struct ospf_area
*area
;
1055 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1057 area
= ospf_area_new(ospf
, area_id
);
1058 listnode_add_sort(ospf
->areas
, area
);
1059 ospf_check_abr_status(ospf
);
1060 if (ospf
->stub_router_admin_set
1061 == OSPF_STUB_ROUTER_ADMINISTRATIVE_SET
) {
1062 SET_FLAG(area
->stub_router_state
,
1063 OSPF_AREA_ADMIN_STUB_ROUTED
);
1070 struct ospf_area
*ospf_area_lookup_by_area_id(struct ospf
*ospf
,
1071 struct in_addr area_id
)
1073 struct ospf_area
*area
;
1074 struct listnode
*node
;
1076 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, node
, area
))
1077 if (IPV4_ADDR_SAME(&area
->area_id
, &area_id
))
1083 void ospf_area_add_if(struct ospf_area
*area
, struct ospf_interface
*oi
)
1085 listnode_add(area
->oiflist
, oi
);
1088 void ospf_area_del_if(struct ospf_area
*area
, struct ospf_interface
*oi
)
1090 listnode_delete(area
->oiflist
, oi
);
1094 struct ospf_interface
*add_ospf_interface(struct connected
*co
,
1095 struct ospf_area
*area
)
1097 struct ospf_interface
*oi
;
1099 oi
= ospf_if_new(area
->ospf
, co
->ifp
, co
->address
);
1104 oi
->params
= ospf_lookup_if_params(co
->ifp
, oi
->address
->u
.prefix4
);
1105 oi
->output_cost
= ospf_if_get_output_cost(oi
);
1107 /* Relate ospf interface to ospf instance. */
1108 oi
->ospf
= area
->ospf
;
1110 /* update network type as interface flag */
1111 /* If network type is specified previously,
1112 skip network type setting. */
1113 oi
->type
= IF_DEF_PARAMS(co
->ifp
)->type
;
1114 oi
->ptp_dmvpn
= IF_DEF_PARAMS(co
->ifp
)->ptp_dmvpn
;
1116 /* Add pseudo neighbor. */
1117 ospf_nbr_self_reset(oi
, oi
->ospf
->router_id
);
1119 ospf_area_add_if(oi
->area
, oi
);
1121 /* if LDP-IGP Sync is configured globally inherit config */
1122 ospf_ldp_sync_if_init(oi
);
1125 * if router_id is not configured, don't bring up
1127 * ospf_router_id_update() will call ospf_if_update
1128 * whenever r-id is configured instead.
1130 if ((area
->ospf
->router_id
.s_addr
!= INADDR_ANY
)
1131 && if_is_operative(co
->ifp
))
1137 static void update_redistributed(struct ospf
*ospf
, int add_to_ospf
)
1139 struct route_node
*rn
;
1140 struct external_info
*ei
;
1141 struct ospf_external
*ext
;
1143 if (ospf_is_type_redistributed(ospf
, ZEBRA_ROUTE_CONNECT
, 0)) {
1144 ext
= ospf_external_lookup(ospf
, ZEBRA_ROUTE_CONNECT
, 0);
1145 if ((ext
) && EXTERNAL_INFO(ext
)) {
1146 for (rn
= route_top(EXTERNAL_INFO(ext
)); rn
;
1147 rn
= route_next(rn
)) {
1153 if (ospf_external_info_find_lsa(ospf
,
1155 if (!ospf_redistribute_check(
1157 ospf_external_lsa_flush(
1160 ei
->ifindex
/*, ei->nexthop */);
1162 if (!ospf_external_info_find_lsa(
1164 if (ospf_redistribute_check(
1166 ospf_external_lsa_originate(
1174 /* Config network statement related functions. */
1175 static struct ospf_network
*ospf_network_new(struct in_addr area_id
)
1177 struct ospf_network
*new;
1178 new = XCALLOC(MTYPE_OSPF_NETWORK
, sizeof(struct ospf_network
));
1180 new->area_id
= area_id
;
1181 new->area_id_fmt
= OSPF_AREA_ID_FMT_DOTTEDQUAD
;
1186 static void ospf_network_free(struct ospf
*ospf
, struct ospf_network
*network
)
1188 ospf_area_check_free(ospf
, network
->area_id
);
1189 ospf_schedule_abr_task(ospf
);
1190 XFREE(MTYPE_OSPF_NETWORK
, network
);
1193 int ospf_network_set(struct ospf
*ospf
, struct prefix_ipv4
*p
,
1194 struct in_addr area_id
, int df
)
1196 struct ospf_network
*network
;
1197 struct ospf_area
*area
;
1198 struct route_node
*rn
;
1200 rn
= route_node_get(ospf
->networks
, (struct prefix
*)p
);
1203 route_unlock_node(rn
);
1205 if (IPV4_ADDR_SAME(&area_id
, &network
->area_id
)) {
1208 /* There is already same network statement. */
1213 rn
->info
= network
= ospf_network_new(area_id
);
1214 network
->area_id_fmt
= df
;
1215 area
= ospf_area_get(ospf
, area_id
);
1216 ospf_area_display_format_set(ospf
, area
, df
);
1218 /* Run network config now. */
1219 ospf_network_run((struct prefix
*)p
, area
);
1221 /* Update connected redistribute. */
1222 update_redistributed(ospf
, 1); /* interfaces possibly added */
1224 ospf_area_check_free(ospf
, area_id
);
1229 int ospf_network_unset(struct ospf
*ospf
, struct prefix_ipv4
*p
,
1230 struct in_addr area_id
)
1232 struct route_node
*rn
;
1233 struct ospf_network
*network
;
1234 struct listnode
*node
, *nnode
;
1235 struct ospf_interface
*oi
;
1237 rn
= route_node_lookup(ospf
->networks
, (struct prefix
*)p
);
1242 route_unlock_node(rn
);
1243 if (!IPV4_ADDR_SAME(&area_id
, &network
->area_id
))
1246 ospf_network_free(ospf
, rn
->info
);
1248 route_unlock_node(rn
); /* initial reference */
1250 /* Find interfaces that are not configured already. */
1251 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
)) {
1253 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
1256 ospf_network_run_subnet(ospf
, oi
->connected
, NULL
, NULL
);
1259 /* Update connected redistribute. */
1260 update_redistributed(ospf
, 0); /* interfaces possibly removed */
1261 ospf_area_check_free(ospf
, area_id
);
1266 /* Ensure there's an OSPF instance, as "ip ospf area" enabled OSPF means
1267 * there might not be any 'router ospf' config.
1269 * Otherwise, doesn't do anything different to ospf_if_update for now
1271 void ospf_interface_area_set(struct ospf
*ospf
, struct interface
*ifp
)
1276 ospf_if_update(ospf
, ifp
);
1277 /* if_update does a update_redistributed */
1282 void ospf_interface_area_unset(struct ospf
*ospf
, struct interface
*ifp
)
1284 struct route_node
*rn_oi
;
1287 return; /* Ospf not ready yet */
1289 /* Find interfaces that may need to be removed. */
1290 for (rn_oi
= route_top(IF_OIFS(ifp
)); rn_oi
;
1291 rn_oi
= route_next(rn_oi
)) {
1292 struct ospf_interface
*oi
= NULL
;
1294 if ((oi
= rn_oi
->info
) == NULL
)
1297 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
1300 ospf_network_run_subnet(ospf
, oi
->connected
, NULL
, NULL
);
1303 /* Update connected redistribute. */
1304 update_redistributed(ospf
, 0); /* interfaces possibly removed */
1307 /* Check whether interface matches given network
1308 * returns: 1, true. 0, false
1310 static int ospf_network_match_iface(const struct connected
*co
,
1311 const struct prefix
*net
)
1313 /* new approach: more elegant and conceptually clean */
1314 return prefix_match_network_statement(net
, CONNECTED_PREFIX(co
));
1317 static void ospf_update_interface_area(struct connected
*co
,
1318 struct ospf_area
*area
)
1320 struct ospf_interface
*oi
= ospf_if_table_lookup(co
->ifp
, co
->address
);
1322 /* nothing to be done case */
1323 if (oi
&& oi
->area
== area
) {
1330 add_ospf_interface(co
, area
);
1333 /* Run OSPF for the given subnet, taking into account the following
1334 * possible sources of area configuration, in the given order of preference:
1336 * - Whether there is interface+address specific area configuration
1337 * - Whether there is a default area for the interface
1338 * - Whether there is an area given as a parameter.
1339 * - If no specific network prefix/area is supplied, whether there's
1340 * a matching network configured.
1342 static void ospf_network_run_subnet(struct ospf
*ospf
, struct connected
*co
,
1344 struct ospf_area
*given_area
)
1346 struct ospf_interface
*oi
;
1347 struct ospf_if_params
*params
;
1348 struct ospf_area
*area
= NULL
;
1349 struct route_node
*rn
;
1352 if (CHECK_FLAG(co
->flags
, ZEBRA_IFA_SECONDARY
))
1355 if (co
->address
->family
!= AF_INET
)
1358 /* Try determine the appropriate area for this interface + address
1359 * Start by checking interface config
1361 params
= ospf_lookup_if_params(co
->ifp
, co
->address
->u
.prefix4
);
1362 if (params
&& OSPF_IF_PARAM_CONFIGURED(params
, if_area
))
1363 area
= ospf_area_get(ospf
, params
->if_area
);
1365 params
= IF_DEF_PARAMS(co
->ifp
);
1366 if (OSPF_IF_PARAM_CONFIGURED(params
, if_area
))
1367 area
= ospf_area_get(ospf
, params
->if_area
);
1370 /* If we've found an interface and/or addr specific area, then we're
1374 ospf_update_interface_area(co
, area
);
1378 /* Otherwise, only remaining possibility is a matching network statement
1381 assert(given_area
!= NULL
);
1383 /* Which either was supplied as a parameter.. (e.g. cause a new
1384 * network/area was just added)..
1386 if (p
->family
== co
->address
->family
1387 && ospf_network_match_iface(co
, p
))
1388 ospf_update_interface_area(co
, given_area
);
1393 /* Else we have to search the existing network/area config to see
1396 for (rn
= route_top(ospf
->networks
); rn
; rn
= route_next(rn
))
1397 if (rn
->info
!= NULL
&& ospf_network_match_iface(co
, &rn
->p
)) {
1398 struct ospf_network
*network
=
1399 (struct ospf_network
*)rn
->info
;
1400 area
= ospf_area_get(ospf
, network
->area_id
);
1401 ospf_update_interface_area(co
, area
);
1405 /* If the subnet isn't in any area, deconfigure */
1406 if (!configed
&& (oi
= ospf_if_table_lookup(co
->ifp
, co
->address
)))
1410 static void ospf_network_run_interface(struct ospf
*ospf
, struct interface
*ifp
,
1412 struct ospf_area
*given_area
)
1414 struct listnode
*cnode
;
1415 struct connected
*co
;
1417 if (memcmp(ifp
->name
, "VLINK", 5) == 0)
1420 /* Network prefix without area is nonsensical */
1422 assert(given_area
!= NULL
);
1424 /* if interface prefix is match specified prefix,
1425 then create socket and join multicast group. */
1426 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, co
))
1427 ospf_network_run_subnet(ospf
, co
, p
, given_area
);
1430 static void ospf_network_run(struct prefix
*p
, struct ospf_area
*area
)
1432 struct vrf
*vrf
= vrf_lookup_by_id(area
->ospf
->vrf_id
);
1433 struct interface
*ifp
;
1435 /* Schedule Router ID Update. */
1436 if (area
->ospf
->router_id
.s_addr
== INADDR_ANY
)
1437 ospf_router_id_update(area
->ospf
);
1439 /* Get target interface. */
1440 FOR_ALL_INTERFACES (vrf
, ifp
)
1441 ospf_network_run_interface(area
->ospf
, ifp
, p
, area
);
1444 void ospf_ls_upd_queue_empty(struct ospf_interface
*oi
)
1446 struct route_node
*rn
;
1447 struct listnode
*node
, *nnode
;
1449 struct ospf_lsa
*lsa
;
1451 /* empty ls update queue */
1452 for (rn
= route_top(oi
->ls_upd_queue
); rn
; rn
= route_next(rn
))
1453 if ((lst
= (struct list
*)rn
->info
)) {
1454 for (ALL_LIST_ELEMENTS(lst
, node
, nnode
, lsa
))
1455 ospf_lsa_unlock(&lsa
); /* oi->ls_upd_queue */
1460 /* remove update event */
1461 EVENT_OFF(oi
->t_ls_upd_event
);
1464 void ospf_if_update(struct ospf
*ospf
, struct interface
*ifp
)
1470 if (IS_DEBUG_OSPF_EVENT
)
1472 "%s: interface %s vrf %s(%u) ospf vrf %s vrf_id %u router_id %pI4",
1473 __func__
, ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
1474 ospf_vrf_id_to_name(ospf
->vrf_id
), ospf
->vrf_id
,
1477 /* OSPF must be ready. */
1478 if (!ospf_is_ready(ospf
))
1481 ospf_network_run_interface(ospf
, ifp
, NULL
, NULL
);
1483 /* Update connected redistribute. */
1484 update_redistributed(ospf
, 1);
1488 void ospf_remove_vls_through_area(struct ospf
*ospf
, struct ospf_area
*area
)
1490 struct listnode
*node
, *nnode
;
1491 struct ospf_vl_data
*vl_data
;
1493 for (ALL_LIST_ELEMENTS(ospf
->vlinks
, node
, nnode
, vl_data
))
1494 if (IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
))
1495 ospf_vl_delete(ospf
, vl_data
);
1499 static const struct message ospf_area_type_msg
[] = {
1500 {OSPF_AREA_DEFAULT
, "Default"},
1501 {OSPF_AREA_STUB
, "Stub"},
1502 {OSPF_AREA_NSSA
, "NSSA"},
1505 static void ospf_area_type_set(struct ospf_area
*area
, int type
)
1507 struct listnode
*node
;
1508 struct ospf_interface
*oi
;
1510 if (area
->external_routing
== type
) {
1511 if (IS_DEBUG_OSPF_EVENT
)
1512 zlog_debug("Area[%pI4]: Types are the same, ignored.",
1517 area
->external_routing
= type
;
1519 if (IS_DEBUG_OSPF_EVENT
)
1520 zlog_debug("Area[%pI4]: Configured as %s",
1522 lookup_msg(ospf_area_type_msg
, type
, NULL
));
1524 switch (area
->external_routing
) {
1525 case OSPF_AREA_DEFAULT
:
1526 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
))
1527 if (oi
->nbr_self
!= NULL
) {
1528 UNSET_FLAG(oi
->nbr_self
->options
,
1530 SET_FLAG(oi
->nbr_self
->options
, OSPF_OPTION_E
);
1533 case OSPF_AREA_STUB
:
1534 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
))
1535 if (oi
->nbr_self
!= NULL
) {
1536 if (IS_DEBUG_OSPF_EVENT
)
1538 "setting options on %s accordingly",
1540 UNSET_FLAG(oi
->nbr_self
->options
,
1542 UNSET_FLAG(oi
->nbr_self
->options
,
1544 if (IS_DEBUG_OSPF_EVENT
)
1545 zlog_debug("options set on %s: %x",
1546 IF_NAME(oi
), OPTIONS(oi
));
1549 case OSPF_AREA_NSSA
:
1550 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
))
1551 if (oi
->nbr_self
!= NULL
) {
1553 "setting nssa options on %s accordingly",
1555 UNSET_FLAG(oi
->nbr_self
->options
,
1557 SET_FLAG(oi
->nbr_self
->options
, OSPF_OPTION_NP
);
1558 zlog_debug("options set on %s: %x", IF_NAME(oi
),
1566 ospf_router_lsa_update_area(area
);
1567 ospf_schedule_abr_task(area
->ospf
);
1570 int ospf_area_shortcut_set(struct ospf
*ospf
, struct ospf_area
*area
, int mode
)
1572 if (area
->shortcut_configured
== mode
)
1575 area
->shortcut_configured
= mode
;
1576 ospf_router_lsa_update_area(area
);
1577 ospf_schedule_abr_task(ospf
);
1579 ospf_area_check_free(ospf
, area
->area_id
);
1584 int ospf_area_shortcut_unset(struct ospf
*ospf
, struct ospf_area
*area
)
1586 area
->shortcut_configured
= OSPF_SHORTCUT_DEFAULT
;
1587 ospf_router_lsa_update_area(area
);
1588 ospf_area_check_free(ospf
, area
->area_id
);
1589 ospf_schedule_abr_task(ospf
);
1594 static int ospf_area_vlink_count(struct ospf
*ospf
, struct ospf_area
*area
)
1596 struct ospf_vl_data
*vl
;
1597 struct listnode
*node
;
1600 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl
))
1601 if (IPV4_ADDR_SAME(&vl
->vl_area_id
, &area
->area_id
))
1607 int ospf_area_display_format_set(struct ospf
*ospf
, struct ospf_area
*area
,
1610 area
->area_id_fmt
= df
;
1615 int ospf_area_stub_set(struct ospf
*ospf
, struct in_addr area_id
)
1617 struct ospf_area
*area
;
1619 area
= ospf_area_get(ospf
, area_id
);
1620 if (ospf_area_vlink_count(ospf
, area
))
1623 if (area
->external_routing
!= OSPF_AREA_STUB
)
1624 ospf_area_type_set(area
, OSPF_AREA_STUB
);
1629 int ospf_area_stub_unset(struct ospf
*ospf
, struct in_addr area_id
)
1631 struct ospf_area
*area
;
1633 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1637 if (area
->external_routing
== OSPF_AREA_STUB
)
1638 ospf_area_type_set(area
, OSPF_AREA_DEFAULT
);
1640 ospf_area_check_free(ospf
, area_id
);
1645 int ospf_area_no_summary_set(struct ospf
*ospf
, struct in_addr area_id
)
1647 struct ospf_area
*area
;
1649 area
= ospf_area_get(ospf
, area_id
);
1650 area
->no_summary
= 1;
1655 int ospf_area_no_summary_unset(struct ospf
*ospf
, struct in_addr area_id
)
1657 struct ospf_area
*area
;
1659 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1663 area
->no_summary
= 0;
1664 ospf_area_check_free(ospf
, area_id
);
1669 int ospf_area_nssa_no_summary_set(struct ospf
*ospf
, struct in_addr area_id
)
1671 struct ospf_area
*area
;
1673 area
= ospf_area_get(ospf
, area_id
);
1674 if (ospf_area_vlink_count(ospf
, area
))
1677 if (area
->external_routing
!= OSPF_AREA_NSSA
) {
1678 ospf_area_type_set(area
, OSPF_AREA_NSSA
);
1680 area
->NSSATranslatorRole
= OSPF_NSSA_ROLE_CANDIDATE
;
1683 ospf_area_no_summary_set(ospf
, area_id
);
1688 int ospf_area_nssa_set(struct ospf
*ospf
, struct in_addr area_id
)
1690 struct ospf_area
*area
;
1692 area
= ospf_area_get(ospf
, area_id
);
1693 if (ospf_area_vlink_count(ospf
, area
))
1696 if (area
->external_routing
!= OSPF_AREA_NSSA
) {
1697 ospf_area_type_set(area
, OSPF_AREA_NSSA
);
1700 /* set NSSA area defaults */
1701 area
->no_summary
= 0;
1702 area
->suppress_fa
= 0;
1703 area
->NSSATranslatorRole
= OSPF_NSSA_ROLE_CANDIDATE
;
1704 area
->NSSATranslatorState
= OSPF_NSSA_TRANSLATE_DISABLED
;
1705 area
->NSSATranslatorStabilityInterval
=
1706 OSPF_NSSA_TRANS_STABLE_DEFAULT
;
1711 int ospf_area_nssa_unset(struct ospf
*ospf
, struct in_addr area_id
)
1713 struct ospf_area
*area
;
1715 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1720 /* set NSSA area defaults */
1721 area
->no_summary
= 0;
1722 area
->suppress_fa
= 0;
1723 area
->NSSATranslatorRole
= OSPF_NSSA_ROLE_CANDIDATE
;
1724 area
->NSSATranslatorState
= OSPF_NSSA_TRANSLATE_DISABLED
;
1725 area
->NSSATranslatorStabilityInterval
= OSPF_NSSA_TRANS_STABLE_DEFAULT
;
1726 ospf_area_type_set(area
, OSPF_AREA_DEFAULT
);
1727 ospf_area_check_free(ospf
, area_id
);
1732 int ospf_area_nssa_suppress_fa_set(struct ospf
*ospf
, struct in_addr area_id
)
1734 struct ospf_area
*area
;
1736 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1740 area
->suppress_fa
= 1;
1745 int ospf_area_nssa_suppress_fa_unset(struct ospf
*ospf
, struct in_addr area_id
)
1747 struct ospf_area
*area
;
1749 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1753 area
->suppress_fa
= 0;
1758 int ospf_area_nssa_translator_role_set(struct ospf
*ospf
,
1759 struct in_addr area_id
, int role
)
1761 struct ospf_area
*area
;
1763 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1767 if (role
!= area
->NSSATranslatorRole
) {
1768 if ((area
->NSSATranslatorRole
== OSPF_NSSA_ROLE_ALWAYS
)
1769 || (role
== OSPF_NSSA_ROLE_ALWAYS
)) {
1771 * if new role is OSPF_NSSA_ROLE_ALWAYS we need to set
1772 * Nt bit, if the role was OSPF_NSSA_ROLE_ALWAYS we need
1775 area
->NSSATranslatorRole
= role
;
1776 ospf_router_lsa_update_area(area
);
1778 area
->NSSATranslatorRole
= role
;
1784 void ospf_area_nssa_default_originate_set(struct ospf
*ospf
,
1785 struct in_addr area_id
, int metric
,
1788 struct ospf_area
*area
;
1790 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1794 if (!area
->nssa_default_originate
.enabled
) {
1795 area
->nssa_default_originate
.enabled
= true;
1796 if (++ospf
->nssa_default_import_check
.refcnt
== 1) {
1797 ospf
->nssa_default_import_check
.status
= false;
1798 ospf_zebra_import_default_route(ospf
, false);
1802 area
->nssa_default_originate
.metric_value
= metric
;
1803 area
->nssa_default_originate
.metric_type
= metric_type
;
1806 void ospf_area_nssa_default_originate_unset(struct ospf
*ospf
,
1807 struct in_addr area_id
)
1809 struct ospf_area
*area
;
1811 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1815 if (area
->nssa_default_originate
.enabled
) {
1816 area
->nssa_default_originate
.enabled
= false;
1817 if (--ospf
->nssa_default_import_check
.refcnt
== 0) {
1818 ospf
->nssa_default_import_check
.status
= false;
1819 ospf_zebra_import_default_route(ospf
, true);
1821 area
->nssa_default_originate
.metric_value
= -1;
1822 area
->nssa_default_originate
.metric_type
= -1;
1824 if (!IS_OSPF_ABR(ospf
))
1825 ospf_abr_nssa_type7_defaults(ospf
);
1829 int ospf_area_export_list_set(struct ospf
*ospf
, struct ospf_area
*area
,
1830 const char *list_name
)
1832 struct access_list
*list
;
1833 list
= access_list_lookup(AFI_IP
, list_name
);
1835 EXPORT_LIST(area
) = list
;
1837 if (EXPORT_NAME(area
))
1838 free(EXPORT_NAME(area
));
1840 EXPORT_NAME(area
) = strdup(list_name
);
1841 ospf_schedule_abr_task(ospf
);
1846 int ospf_area_export_list_unset(struct ospf
*ospf
, struct ospf_area
*area
)
1849 EXPORT_LIST(area
) = 0;
1851 if (EXPORT_NAME(area
))
1852 free(EXPORT_NAME(area
));
1854 EXPORT_NAME(area
) = NULL
;
1856 ospf_area_check_free(ospf
, area
->area_id
);
1858 ospf_schedule_abr_task(ospf
);
1863 int ospf_area_import_list_set(struct ospf
*ospf
, struct ospf_area
*area
,
1866 struct access_list
*list
;
1867 list
= access_list_lookup(AFI_IP
, name
);
1869 IMPORT_LIST(area
) = list
;
1871 if (IMPORT_NAME(area
))
1872 free(IMPORT_NAME(area
));
1874 IMPORT_NAME(area
) = strdup(name
);
1875 ospf_schedule_abr_task(ospf
);
1880 int ospf_area_import_list_unset(struct ospf
*ospf
, struct ospf_area
*area
)
1882 IMPORT_LIST(area
) = 0;
1884 if (IMPORT_NAME(area
))
1885 free(IMPORT_NAME(area
));
1887 IMPORT_NAME(area
) = NULL
;
1888 ospf_area_check_free(ospf
, area
->area_id
);
1890 ospf_schedule_abr_task(ospf
);
1895 int ospf_timers_refresh_set(struct ospf
*ospf
, int interval
)
1899 if (ospf
->lsa_refresh_interval
== interval
)
1902 time_left
= ospf
->lsa_refresh_interval
1903 - (monotime(NULL
) - ospf
->lsa_refresher_started
);
1905 if (time_left
> interval
) {
1906 EVENT_OFF(ospf
->t_lsa_refresher
);
1907 event_add_timer(master
, ospf_lsa_refresh_walker
, ospf
, interval
,
1908 &ospf
->t_lsa_refresher
);
1910 ospf
->lsa_refresh_interval
= interval
;
1915 int ospf_timers_refresh_unset(struct ospf
*ospf
)
1919 time_left
= ospf
->lsa_refresh_interval
1920 - (monotime(NULL
) - ospf
->lsa_refresher_started
);
1922 if (time_left
> OSPF_LSA_REFRESH_INTERVAL_DEFAULT
) {
1923 EVENT_OFF(ospf
->t_lsa_refresher
);
1924 ospf
->t_lsa_refresher
= NULL
;
1925 event_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
1926 OSPF_LSA_REFRESH_INTERVAL_DEFAULT
,
1927 &ospf
->t_lsa_refresher
);
1930 ospf
->lsa_refresh_interval
= OSPF_LSA_REFRESH_INTERVAL_DEFAULT
;
1936 static struct ospf_nbr_nbma
*ospf_nbr_nbma_new(void)
1938 struct ospf_nbr_nbma
*nbr_nbma
;
1940 nbr_nbma
= XCALLOC(MTYPE_OSPF_NEIGHBOR_STATIC
,
1941 sizeof(struct ospf_nbr_nbma
));
1943 nbr_nbma
->priority
= OSPF_NEIGHBOR_PRIORITY_DEFAULT
;
1944 nbr_nbma
->v_poll
= OSPF_POLL_INTERVAL_DEFAULT
;
1949 static void ospf_nbr_nbma_free(struct ospf_nbr_nbma
*nbr_nbma
)
1951 XFREE(MTYPE_OSPF_NEIGHBOR_STATIC
, nbr_nbma
);
1954 static void ospf_nbr_nbma_delete(struct ospf
*ospf
,
1955 struct ospf_nbr_nbma
*nbr_nbma
)
1957 struct route_node
*rn
;
1958 struct prefix_ipv4 p
;
1961 p
.prefix
= nbr_nbma
->addr
;
1962 p
.prefixlen
= IPV4_MAX_BITLEN
;
1964 rn
= route_node_lookup(ospf
->nbr_nbma
, (struct prefix
*)&p
);
1966 ospf_nbr_nbma_free(rn
->info
);
1968 route_unlock_node(rn
);
1969 route_unlock_node(rn
);
1973 static void ospf_nbr_nbma_down(struct ospf_nbr_nbma
*nbr_nbma
)
1975 EVENT_OFF(nbr_nbma
->t_poll
);
1977 if (nbr_nbma
->nbr
) {
1978 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
1979 OSPF_NSM_EVENT_EXECUTE(nbr_nbma
->nbr
, NSM_KillNbr
);
1983 listnode_delete(nbr_nbma
->oi
->nbr_nbma
, nbr_nbma
);
1986 static void ospf_nbr_nbma_add(struct ospf_nbr_nbma
*nbr_nbma
,
1987 struct ospf_interface
*oi
)
1989 struct ospf_neighbor
*nbr
;
1990 struct route_node
*rn
;
1993 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
1996 if (nbr_nbma
->nbr
!= NULL
)
1999 if (IPV4_ADDR_SAME(&oi
->nbr_self
->address
.u
.prefix4
, &nbr_nbma
->addr
))
2003 listnode_add(oi
->nbr_nbma
, nbr_nbma
);
2005 /* Get neighbor information from table. */
2007 p
.prefixlen
= IPV4_MAX_BITLEN
;
2008 p
.u
.prefix4
= nbr_nbma
->addr
;
2010 rn
= route_node_get(oi
->nbrs
, &p
);
2013 nbr
->nbr_nbma
= nbr_nbma
;
2014 nbr_nbma
->nbr
= nbr
;
2016 route_unlock_node(rn
);
2018 nbr
= rn
->info
= ospf_nbr_new(oi
);
2019 nbr
->state
= NSM_Down
;
2020 nbr
->src
= nbr_nbma
->addr
;
2021 nbr
->nbr_nbma
= nbr_nbma
;
2022 nbr
->priority
= nbr_nbma
->priority
;
2025 nbr_nbma
->nbr
= nbr
;
2027 /* Configure BFD if interface has it. */
2028 ospf_neighbor_bfd_apply(nbr
);
2030 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_Start
);
2034 void ospf_nbr_nbma_if_update(struct ospf
*ospf
, struct ospf_interface
*oi
)
2036 struct ospf_nbr_nbma
*nbr_nbma
;
2037 struct route_node
*rn
;
2038 struct prefix_ipv4 p
;
2040 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
2043 for (rn
= route_top(ospf
->nbr_nbma
); rn
; rn
= route_next(rn
))
2044 if ((nbr_nbma
= rn
->info
))
2045 if (nbr_nbma
->oi
== NULL
&& nbr_nbma
->nbr
== NULL
) {
2047 p
.prefix
= nbr_nbma
->addr
;
2048 p
.prefixlen
= IPV4_MAX_BITLEN
;
2050 if (prefix_match(oi
->address
,
2051 (struct prefix
*)&p
))
2052 ospf_nbr_nbma_add(nbr_nbma
, oi
);
2056 struct ospf_nbr_nbma
*ospf_nbr_nbma_lookup(struct ospf
*ospf
,
2057 struct in_addr nbr_addr
)
2059 struct route_node
*rn
;
2060 struct prefix_ipv4 p
;
2063 p
.prefix
= nbr_addr
;
2064 p
.prefixlen
= IPV4_MAX_BITLEN
;
2066 rn
= route_node_lookup(ospf
->nbr_nbma
, (struct prefix
*)&p
);
2068 route_unlock_node(rn
);
2074 int ospf_nbr_nbma_set(struct ospf
*ospf
, struct in_addr nbr_addr
)
2076 struct ospf_nbr_nbma
*nbr_nbma
;
2077 struct ospf_interface
*oi
;
2078 struct prefix_ipv4 p
;
2079 struct route_node
*rn
;
2080 struct listnode
*node
;
2082 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
2086 nbr_nbma
= ospf_nbr_nbma_new();
2087 nbr_nbma
->addr
= nbr_addr
;
2090 p
.prefix
= nbr_addr
;
2091 p
.prefixlen
= IPV4_MAX_BITLEN
;
2093 rn
= route_node_get(ospf
->nbr_nbma
, (struct prefix
*)&p
);
2095 route_unlock_node(rn
);
2096 rn
->info
= nbr_nbma
;
2098 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
2099 if (oi
->type
== OSPF_IFTYPE_NBMA
)
2100 if (prefix_match(oi
->address
, (struct prefix
*)&p
)) {
2101 ospf_nbr_nbma_add(nbr_nbma
, oi
);
2109 int ospf_nbr_nbma_unset(struct ospf
*ospf
, struct in_addr nbr_addr
)
2111 struct ospf_nbr_nbma
*nbr_nbma
;
2113 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
2114 if (nbr_nbma
== NULL
)
2117 ospf_nbr_nbma_down(nbr_nbma
);
2118 ospf_nbr_nbma_delete(ospf
, nbr_nbma
);
2123 int ospf_nbr_nbma_priority_set(struct ospf
*ospf
, struct in_addr nbr_addr
,
2126 struct ospf_nbr_nbma
*nbr_nbma
;
2128 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
2129 if (nbr_nbma
== NULL
)
2132 if (nbr_nbma
->priority
!= priority
)
2133 nbr_nbma
->priority
= priority
;
2138 int ospf_nbr_nbma_priority_unset(struct ospf
*ospf
, struct in_addr nbr_addr
)
2140 struct ospf_nbr_nbma
*nbr_nbma
;
2142 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
2143 if (nbr_nbma
== NULL
)
2146 if (nbr_nbma
!= OSPF_NEIGHBOR_PRIORITY_DEFAULT
)
2147 nbr_nbma
->priority
= OSPF_NEIGHBOR_PRIORITY_DEFAULT
;
2152 int ospf_nbr_nbma_poll_interval_set(struct ospf
*ospf
, struct in_addr nbr_addr
,
2153 unsigned int interval
)
2155 struct ospf_nbr_nbma
*nbr_nbma
;
2157 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
2158 if (nbr_nbma
== NULL
)
2161 if (nbr_nbma
->v_poll
!= interval
) {
2162 nbr_nbma
->v_poll
= interval
;
2163 if (nbr_nbma
->oi
&& ospf_if_is_up(nbr_nbma
->oi
)) {
2164 EVENT_OFF(nbr_nbma
->t_poll
);
2165 OSPF_POLL_TIMER_ON(nbr_nbma
->t_poll
, ospf_poll_timer
,
2173 int ospf_nbr_nbma_poll_interval_unset(struct ospf
*ospf
, struct in_addr addr
)
2175 struct ospf_nbr_nbma
*nbr_nbma
;
2177 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, addr
);
2178 if (nbr_nbma
== NULL
)
2181 if (nbr_nbma
->v_poll
!= OSPF_POLL_INTERVAL_DEFAULT
)
2182 nbr_nbma
->v_poll
= OSPF_POLL_INTERVAL_DEFAULT
;
2188 * Update socket bufsize(s), usually after config change
2190 void ospf_update_bufsize(struct ospf
*ospf
, uint32_t recvsize
,
2193 enum ospf_sock_type_e type
= OSPF_SOCK_NONE
;
2195 /* Figure out whether there's been a change */
2196 if (recvsize
!= ospf
->recv_sock_bufsize
) {
2197 type
= OSPF_SOCK_RECV
;
2198 ospf
->recv_sock_bufsize
= recvsize
;
2200 if (sendsize
!= ospf
->send_sock_bufsize
) {
2201 type
= OSPF_SOCK_BOTH
;
2202 ospf
->send_sock_bufsize
= sendsize
;
2204 } else if (sendsize
!= ospf
->send_sock_bufsize
) {
2205 type
= OSPF_SOCK_SEND
;
2206 ospf
->send_sock_bufsize
= sendsize
;
2209 if (type
!= OSPF_SOCK_NONE
)
2210 ospf_sock_bufsize_update(ospf
, ospf
->fd
, type
);
2213 void ospf_master_init(struct event_loop
*master
)
2215 memset(&ospf_master
, 0, sizeof(ospf_master
));
2218 om
->ospf
= list_new();
2219 om
->master
= master
;
2222 /* Link OSPF instance to VRF. */
2223 void ospf_vrf_link(struct ospf
*ospf
, struct vrf
*vrf
)
2225 ospf
->vrf_id
= vrf
->vrf_id
;
2226 if (vrf
->info
!= (void *)ospf
)
2227 vrf
->info
= (void *)ospf
;
2230 /* Unlink OSPF instance from VRF. */
2231 void ospf_vrf_unlink(struct ospf
*ospf
, struct vrf
*vrf
)
2233 if (vrf
->info
== (void *)ospf
)
2235 ospf
->vrf_id
= VRF_UNKNOWN
;
2238 /* This is hook function for vrf create called as part of vrf_init */
2239 static int ospf_vrf_new(struct vrf
*vrf
)
2241 if (IS_DEBUG_OSPF_EVENT
)
2242 zlog_debug("%s: VRF Created: %s(%u)", __func__
, vrf
->name
,
2248 /* This is hook function for vrf delete call as part of vrf_init */
2249 static int ospf_vrf_delete(struct vrf
*vrf
)
2251 if (IS_DEBUG_OSPF_EVENT
)
2252 zlog_debug("%s: VRF Deletion: %s(%u)", __func__
, vrf
->name
,
2258 static void ospf_set_redist_vrf_bitmaps(struct ospf
*ospf
, bool set
)
2261 struct list
*red_list
;
2263 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++) {
2264 red_list
= ospf
->redist
[type
];
2267 if (IS_DEBUG_OSPF_EVENT
)
2269 "%s: setting redist vrf %d bitmap for type %d",
2270 __func__
, ospf
->vrf_id
, type
);
2272 vrf_bitmap_set(zclient
->redist
[AFI_IP
][type
],
2275 vrf_bitmap_unset(zclient
->redist
[AFI_IP
][type
],
2279 red_list
= ospf
->redist
[DEFAULT_ROUTE
];
2282 vrf_bitmap_set(zclient
->default_information
[AFI_IP
],
2285 vrf_bitmap_unset(zclient
->default_information
[AFI_IP
],
2290 /* Enable OSPF VRF instance */
2291 static int ospf_vrf_enable(struct vrf
*vrf
)
2293 struct ospf
*ospf
= NULL
;
2294 vrf_id_t old_vrf_id
;
2297 if (IS_DEBUG_OSPF_EVENT
)
2298 zlog_debug("%s: VRF %s id %u enabled", __func__
, vrf
->name
,
2301 ospf
= ospf_lookup_by_name(vrf
->name
);
2303 old_vrf_id
= ospf
->vrf_id
;
2304 /* We have instance configured, link to VRF and make it "up". */
2305 ospf_vrf_link(ospf
, vrf
);
2306 if (IS_DEBUG_OSPF_EVENT
)
2308 "%s: ospf linked to vrf %s vrf_id %u (old id %u)",
2309 __func__
, vrf
->name
, ospf
->vrf_id
, old_vrf_id
);
2311 if (old_vrf_id
!= ospf
->vrf_id
) {
2312 ospf_set_redist_vrf_bitmaps(ospf
, true);
2314 /* start zebra redist to us for new vrf */
2315 ospf_zebra_vrf_register(ospf
);
2317 ret
= ospf_sock_init(ospf
);
2318 if (ret
< 0 || ospf
->fd
<= 0)
2320 event_add_read(master
, ospf_read
, ospf
, ospf
->fd
,
2322 ospf
->oi_running
= 1;
2323 ospf_router_id_update(ospf
);
2330 /* Disable OSPF VRF instance */
2331 static int ospf_vrf_disable(struct vrf
*vrf
)
2333 struct ospf
*ospf
= NULL
;
2334 vrf_id_t old_vrf_id
= VRF_UNKNOWN
;
2336 if (vrf
->vrf_id
== VRF_DEFAULT
)
2339 if (IS_DEBUG_OSPF_EVENT
)
2340 zlog_debug("%s: VRF %s id %d disabled.", __func__
, vrf
->name
,
2343 ospf
= ospf_lookup_by_name(vrf
->name
);
2345 old_vrf_id
= ospf
->vrf_id
;
2347 ospf_zebra_vrf_deregister(ospf
);
2349 ospf_set_redist_vrf_bitmaps(ospf
, false);
2351 /* We have instance configured, unlink
2352 * from VRF and make it "down".
2354 ospf_vrf_unlink(ospf
, vrf
);
2355 ospf
->oi_running
= 0;
2356 if (IS_DEBUG_OSPF_EVENT
)
2357 zlog_debug("%s: ospf old_vrf_id %d unlinked", __func__
,
2359 EVENT_OFF(ospf
->t_read
);
2364 /* Note: This is a callback, the VRF will be deleted by the caller. */
2368 void ospf_vrf_init(void)
2370 vrf_init(ospf_vrf_new
, ospf_vrf_enable
, ospf_vrf_disable
,
2374 void ospf_vrf_terminate(void)
2379 const char *ospf_vrf_id_to_name(vrf_id_t vrf_id
)
2381 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2383 return vrf
? vrf
->name
: "NIL";
2386 const char *ospf_get_name(const struct ospf
*ospf
)
2391 return VRF_DEFAULT_NAME
;