1 /* OSPF version 2 daemon program.
2 * Copyright (C) 1999, 2000 Toshiaki Takada
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "sockunion.h" /* for inet_aton () */
41 #include "ospfd/ospfd.h"
42 #include "ospfd/ospf_network.h"
43 #include "ospfd/ospf_interface.h"
44 #include "ospfd/ospf_ism.h"
45 #include "ospfd/ospf_asbr.h"
46 #include "ospfd/ospf_lsa.h"
47 #include "ospfd/ospf_lsdb.h"
48 #include "ospfd/ospf_neighbor.h"
49 #include "ospfd/ospf_nsm.h"
50 #include "ospfd/ospf_spf.h"
51 #include "ospfd/ospf_packet.h"
52 #include "ospfd/ospf_dump.h"
53 #include "ospfd/ospf_zebra.h"
54 #include "ospfd/ospf_abr.h"
55 #include "ospfd/ospf_flood.h"
56 #include "ospfd/ospf_route.h"
57 #include "ospfd/ospf_ase.h"
60 DEFINE_QOBJ_TYPE(ospf
)
62 /* OSPF process wide configuration. */
63 static struct ospf_master ospf_master
;
65 /* OSPF process wide configuration pointer to export. */
66 struct ospf_master
*om
;
68 extern struct zclient
*zclient
;
69 extern struct in_addr router_id_zebra
;
72 static void ospf_remove_vls_through_area(struct ospf
*, struct ospf_area
*);
73 static void ospf_network_free(struct ospf
*, struct ospf_network
*);
74 static void ospf_area_free(struct ospf_area
*);
75 static void ospf_network_run(struct prefix
*, struct ospf_area
*);
76 static void ospf_network_run_interface(struct ospf
*, struct interface
*,
77 struct prefix
*, struct ospf_area
*);
78 static void ospf_network_run_subnet(struct ospf
*, struct connected
*,
79 struct prefix
*, struct ospf_area
*);
80 static int ospf_network_match_iface(const struct connected
*,
81 const struct prefix
*);
82 static void ospf_finish_final(struct ospf
*);
84 #define OSPF_EXTERNAL_LSA_ORIGINATE_DELAY 1
86 void ospf_router_id_update(struct ospf
*ospf
)
88 struct vrf
*vrf
= vrf_lookup_by_id(ospf
->vrf_id
);
89 struct in_addr router_id
, router_id_old
;
90 struct ospf_interface
*oi
;
91 struct interface
*ifp
;
92 struct listnode
*node
;
95 if (!ospf
->oi_running
) {
96 if (IS_DEBUG_OSPF_EVENT
)
98 "Router ospf not configured -- Router-ID update postponed");
102 if (IS_DEBUG_OSPF_EVENT
)
103 zlog_debug("Router-ID[OLD:%s]: Update",
104 inet_ntoa(ospf
->router_id
));
106 router_id_old
= ospf
->router_id
;
108 /* Select the router ID based on these priorities:
109 1. Statically assigned router ID is always the first choice.
110 2. If there is no statically assigned router ID, then try to stick
111 with the most recent value, since changing router ID's is very
113 3. Last choice: just go with whatever the zebra daemon recommends.
115 if (ospf
->router_id_static
.s_addr
!= 0)
116 router_id
= ospf
->router_id_static
;
117 else if (ospf
->router_id
.s_addr
!= 0)
118 router_id
= ospf
->router_id
;
120 router_id
= router_id_zebra
;
122 if (IS_DEBUG_OSPF_EVENT
)
123 zlog_debug("Router-ID[OLD:%s]: Update to %s",
124 inet_ntoa(ospf
->router_id
),
125 inet_ntoa(router_id_old
));
127 if (!IPV4_ADDR_SAME(&router_id_old
, &router_id
)) {
129 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
130 /* Some nbrs are identified by router_id, these needs
131 * to be rebuilt. Possible optimization would be to do
132 * oi->nbr_self->router_id = router_id for
133 * !(virtual | ptop) links
135 ospf_nbr_self_reset(oi
, router_id
);
138 /* If AS-external-LSA is queued, then flush those LSAs. */
139 if (router_id_old
.s_addr
== 0 && ospf
->external_origin
) {
140 /* Originate each redistributed external route. */
141 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++)
142 if (ospf
->external_origin
& (1 << type
))
145 ospf_external_lsa_originate_timer
,
147 /* Originate Deafult. */
148 if (ospf
->external_origin
& (1 << ZEBRA_ROUTE_MAX
))
149 thread_add_event(master
,
150 ospf_default_originate_timer
,
153 ospf
->external_origin
= 0;
156 /* Flush (inline) all external LSAs based on the OSPF_LSA_SELF
159 struct route_node
*rn
;
160 struct ospf_lsa
*lsa
;
162 LSDB_LOOP(EXTERNAL_LSDB(ospf
), rn
, lsa
)
163 if (IS_LSA_SELF(lsa
))
164 ospf_lsa_flush_schedule(ospf
, lsa
);
167 ospf
->router_id
= router_id
;
168 if (IS_DEBUG_OSPF_EVENT
)
169 zlog_debug("Router-ID[NEW:%s]: Update",
170 inet_ntoa(ospf
->router_id
));
172 /* Flush (inline) all external LSAs which now match the new
174 need to adjust the OSPF_LSA_SELF flag, so the flush doesnt
176 asserts in ospf_refresher_unregister_lsa(). This step is
178 because the current quagga code does look-up for
180 based on the self router-id alone but expects OSPF_LSA_SELF
184 struct route_node
*rn
;
185 struct ospf_lsa
*lsa
;
187 LSDB_LOOP(EXTERNAL_LSDB(ospf
), rn
, lsa
)
189 /* AdvRouter and Router ID is the same. */
190 if (IPV4_ADDR_SAME(&lsa
->data
->adv_router
,
193 OSPF_LSA_SELF_CHECKED
);
194 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
195 ospf_lsa_flush_schedule(ospf
, lsa
);
200 /* Originate each redistributed external route. */
201 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++)
202 thread_add_event(master
,
203 ospf_external_lsa_originate_timer
,
205 thread_add_event(master
, ospf_default_originate_timer
, ospf
, 0,
208 /* update router-lsa's for each area */
209 ospf_router_lsa_update(ospf
);
211 /* update ospf_interface's */
212 FOR_ALL_INTERFACES (vrf
, ifp
)
213 ospf_if_update(ospf
, ifp
);
217 /* For OSPF area sort by area id. */
218 static int ospf_area_id_cmp(struct ospf_area
*a1
, struct ospf_area
*a2
)
220 if (ntohl(a1
->area_id
.s_addr
) > ntohl(a2
->area_id
.s_addr
))
222 if (ntohl(a1
->area_id
.s_addr
) < ntohl(a2
->area_id
.s_addr
))
227 /* Allocate new ospf structure. */
228 static struct ospf
*ospf_new(u_short instance
, const char *name
)
231 struct vrf
*vrf
= NULL
;
233 struct ospf
*new = XCALLOC(MTYPE_OSPF_TOP
, sizeof(struct ospf
));
235 new->instance
= instance
;
236 new->router_id
.s_addr
= htonl(0);
237 new->router_id_static
.s_addr
= htonl(0);
240 new->vrf_id
= VRF_UNKNOWN
;
241 /* Freed in ospf_finish_final */
242 new->name
= XSTRDUP(MTYPE_OSPF_TOP
, name
);
243 vrf
= vrf_lookup_by_name(new->name
);
244 if (IS_DEBUG_OSPF_EVENT
)
245 zlog_debug("%s: Create new ospf instance with vrf_name %s vrf_id %d",
246 __PRETTY_FUNCTION__
, name
, new->vrf_id
);
248 ospf_vrf_link(new, vrf
);
250 new->vrf_id
= VRF_DEFAULT
;
251 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
252 ospf_vrf_link(new, vrf
);
254 ospf_zebra_vrf_register(new);
256 new->abr_type
= OSPF_ABR_DEFAULT
;
257 new->oiflist
= list_new();
258 new->vlinks
= list_new();
259 new->areas
= list_new();
260 new->areas
->cmp
= (int (*)(void *, void *))ospf_area_id_cmp
;
261 new->networks
= route_table_init();
262 new->nbr_nbma
= route_table_init();
264 new->lsdb
= ospf_lsdb_new();
266 new->default_originate
= DEFAULT_ORIGINATE_NONE
;
268 new->passive_interface_default
= OSPF_IF_ACTIVE
;
270 new->new_external_route
= route_table_init();
271 new->old_external_route
= route_table_init();
272 new->external_lsas
= route_table_init();
274 new->stub_router_startup_time
= OSPF_STUB_ROUTER_UNCONFIGURED
;
275 new->stub_router_shutdown_time
= OSPF_STUB_ROUTER_UNCONFIGURED
;
276 new->stub_router_admin_set
= OSPF_STUB_ROUTER_ADMINISTRATIVE_UNSET
;
278 /* Distribute parameter init. */
279 for (i
= 0; i
<= ZEBRA_ROUTE_MAX
; i
++) {
282 new->default_metric
= -1;
283 new->ref_bandwidth
= OSPF_DEFAULT_REF_BANDWIDTH
;
286 new->min_ls_interval
= OSPF_MIN_LS_INTERVAL
;
287 new->min_ls_arrival
= OSPF_MIN_LS_ARRIVAL
;
289 /* SPF timer value init. */
290 new->spf_delay
= OSPF_SPF_DELAY_DEFAULT
;
291 new->spf_holdtime
= OSPF_SPF_HOLDTIME_DEFAULT
;
292 new->spf_max_holdtime
= OSPF_SPF_MAX_HOLDTIME_DEFAULT
;
293 new->spf_hold_multiplier
= 1;
296 new->maxage_delay
= OSPF_LSA_MAXAGE_REMOVE_DELAY_DEFAULT
;
297 new->maxage_lsa
= route_table_init();
298 new->t_maxage_walker
= NULL
;
299 thread_add_timer(master
, ospf_lsa_maxage_walker
, new,
300 OSPF_LSA_MAXAGE_CHECK_INTERVAL
, &new->t_maxage_walker
);
302 /* Distance table init. */
303 new->distance_table
= route_table_init();
305 new->lsa_refresh_queue
.index
= 0;
306 new->lsa_refresh_interval
= OSPF_LSA_REFRESH_INTERVAL_DEFAULT
;
307 new->t_lsa_refresher
= NULL
;
308 thread_add_timer(master
, ospf_lsa_refresh_walker
, new,
309 new->lsa_refresh_interval
, &new->t_lsa_refresher
);
310 new->lsa_refresher_started
= monotime(NULL
);
312 if ((ospf_sock_init(new)) < 0) {
314 "ospf_new: fatal error: ospf_sock_init was unable to open "
318 if ((new->ibuf
= stream_new(OSPF_MAX_PACKET_SIZE
+ 1)) == NULL
) {
320 "ospf_new: fatal error: stream_new(%u) failed allocating ibuf",
321 OSPF_MAX_PACKET_SIZE
+ 1);
325 thread_add_read(master
, ospf_read
, new, new->fd
, &new->t_read
);
326 new->oi_write_q
= list_new();
327 new->write_oi_count
= OSPF_WRITE_INTERFACE_COUNT_DEFAULT
;
329 /* Enable "log-adjacency-changes" */
330 #if DFLT_OSPF_LOG_ADJACENCY_CHANGES
331 SET_FLAG(new->config
, OSPF_LOG_ADJACENCY_CHANGES
);
339 struct ospf
*ospf_lookup_instance(u_short instance
)
342 struct listnode
*node
, *nnode
;
344 if (listcount(om
->ospf
) == 0)
347 for (ALL_LIST_ELEMENTS(om
->ospf
, node
, nnode
, ospf
))
348 if ((ospf
->instance
== 0 && instance
== 0)
349 || (ospf
->instance
&& instance
350 && ospf
->instance
== instance
))
356 static int ospf_is_ready(struct ospf
*ospf
)
358 /* OSPF must be on and Router-ID must be configured. */
359 if (!ospf
|| ospf
->router_id
.s_addr
== 0)
365 static void ospf_add(struct ospf
*ospf
)
367 listnode_add(om
->ospf
, ospf
);
370 static void ospf_delete(struct ospf
*ospf
)
372 listnode_delete(om
->ospf
, ospf
);
375 struct ospf
*ospf_lookup_by_inst_name(u_short instance
, const char *name
)
377 struct ospf
*ospf
= NULL
;
378 struct listnode
*node
, *nnode
;
380 for (ALL_LIST_ELEMENTS(om
->ospf
, node
, nnode
, ospf
)) {
381 if ((ospf
->instance
== instance
) &&
382 ((ospf
->name
== NULL
&& name
== NULL
) ||
383 (ospf
->name
&& name
&& strcmp(ospf
->name
, name
) == 0)))
389 struct ospf
*ospf_get(u_short instance
, const char *name
)
393 /* vrf name provided call inst and name based api
394 * in case of no name pass default ospf instance */
396 ospf
= ospf_lookup_by_inst_name(instance
, name
);
398 ospf
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
401 ospf
= ospf_new(instance
, name
);
404 if (ospf
->router_id_static
.s_addr
== 0)
405 ospf_router_id_update(ospf
);
407 ospf_opaque_type11_lsa_init(ospf
);
413 struct ospf
*ospf_get_instance(u_short instance
)
417 ospf
= ospf_lookup_instance(instance
);
419 ospf
= ospf_new(instance
, NULL
/* VRF_DEFAULT*/);
422 if (ospf
->router_id_static
.s_addr
== 0) {
423 if (vrf_lookup_by_id(ospf
->vrf_id
))
424 ospf_router_id_update(ospf
);
426 if (IS_DEBUG_OSPF_EVENT
)
427 zlog_debug("%s: ospf VRF (id %d) is not active yet, skip router id update"
428 , __PRETTY_FUNCTION__
,
431 ospf_router_id_update(ospf
);
434 ospf_opaque_type11_lsa_init(ospf
);
440 struct ospf
*ospf_lookup_by_vrf_id(vrf_id_t vrf_id
)
442 struct vrf
*vrf
= NULL
;
444 vrf
= vrf_lookup_by_id(vrf_id
);
447 return (vrf
->info
) ? (struct ospf
*)vrf
->info
: NULL
;
451 /* It should only be used when processing incoming info update from zebra.
452 * Other situations, it is not sufficient to lookup the ospf instance by
453 * vrf_name only without using the instance number.
455 static struct ospf
*ospf_lookup_by_name(const char *vrf_name
)
457 struct ospf
*ospf
= NULL
;
458 struct listnode
*node
, *nnode
;
460 for (ALL_LIST_ELEMENTS(om
->ospf
, node
, nnode
, ospf
))
461 if ((ospf
->name
== NULL
&& vrf_name
== NULL
)
462 || (ospf
->name
&& vrf_name
&&
463 strcmp(ospf
->name
, vrf_name
) == 0))
468 /* Handle the second half of deferred shutdown. This is called either
469 * from the deferred-shutdown timer thread, or directly through
470 * ospf_deferred_shutdown_check.
472 * Function is to cleanup G-R state, if required then call ospf_finish_final
473 * to complete shutdown of this ospf instance. Possibly exit if the
474 * whole process is being shutdown and this was the last OSPF instance.
476 static void ospf_deferred_shutdown_finish(struct ospf
*ospf
)
478 ospf
->stub_router_shutdown_time
= OSPF_STUB_ROUTER_UNCONFIGURED
;
479 OSPF_TIMER_OFF(ospf
->t_deferred_shutdown
);
481 ospf_finish_final(ospf
);
483 /* *ospf is now invalid */
485 /* ospfd being shut-down? If so, was this the last ospf instance? */
486 if (CHECK_FLAG(om
->options
, OSPF_MASTER_SHUTDOWN
)
487 && (listcount(om
->ospf
) == 0)) {
494 /* Timer thread for G-R */
495 static int ospf_deferred_shutdown_timer(struct thread
*t
)
497 struct ospf
*ospf
= THREAD_ARG(t
);
499 ospf_deferred_shutdown_finish(ospf
);
504 /* Check whether deferred-shutdown must be scheduled, otherwise call
505 * down directly into second-half of instance shutdown.
507 static void ospf_deferred_shutdown_check(struct ospf
*ospf
)
509 unsigned long timeout
;
511 struct ospf_area
*area
;
513 /* deferred shutdown already running? */
514 if (ospf
->t_deferred_shutdown
)
517 /* Should we try push out max-metric LSAs? */
518 if (ospf
->stub_router_shutdown_time
!= OSPF_STUB_ROUTER_UNCONFIGURED
) {
519 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, ln
, area
)) {
520 SET_FLAG(area
->stub_router_state
,
521 OSPF_AREA_ADMIN_STUB_ROUTED
);
523 if (!CHECK_FLAG(area
->stub_router_state
,
524 OSPF_AREA_IS_STUB_ROUTED
))
525 ospf_router_lsa_update_area(area
);
527 timeout
= ospf
->stub_router_shutdown_time
;
529 /* No timer needed */
530 ospf_deferred_shutdown_finish(ospf
);
534 OSPF_TIMER_ON(ospf
->t_deferred_shutdown
, ospf_deferred_shutdown_timer
,
539 /* Shut down the entire process */
540 void ospf_terminate(void)
543 struct listnode
*node
, *nnode
;
545 /* shutdown already in progress */
546 if (CHECK_FLAG(om
->options
, OSPF_MASTER_SHUTDOWN
))
549 SET_FLAG(om
->options
, OSPF_MASTER_SHUTDOWN
);
551 /* exit immediately if OSPF not actually running */
552 if (listcount(om
->ospf
) == 0)
556 for (ALL_LIST_ELEMENTS(om
->ospf
, node
, nnode
, ospf
))
559 /* Deliberately go back up, hopefully to thread scheduler, as
560 * One or more ospf_finish()'s may have deferred shutdown to a timer
563 zclient_stop(zclient
);
564 zclient_free(zclient
);
569 void ospf_finish(struct ospf
*ospf
)
571 /* let deferred shutdown decide */
572 ospf_deferred_shutdown_check(ospf
);
574 /* if ospf_deferred_shutdown returns, then ospf_finish_final is
575 * deferred to expiry of G-S timer thread. Return back up, hopefully
576 * to thread scheduler.
581 /* Final cleanup of ospf instance */
582 static void ospf_finish_final(struct ospf
*ospf
)
584 struct vrf
*vrf
= vrf_lookup_by_id(ospf
->vrf_id
);
585 struct route_node
*rn
;
586 struct ospf_nbr_nbma
*nbr_nbma
;
587 struct ospf_lsa
*lsa
;
588 struct interface
*ifp
;
589 struct ospf_interface
*oi
;
590 struct ospf_area
*area
;
591 struct ospf_vl_data
*vl_data
;
592 struct listnode
*node
, *nnode
;
594 u_short instance
= 0;
598 ospf_opaque_type11_lsa_term(ospf
);
600 /* be nice if this worked, but it doesn't */
601 /*ospf_flush_self_originated_lsas_now (ospf);*/
603 /* Unregister redistribution */
604 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
605 struct list
*red_list
;
606 struct ospf_redist
*red
;
608 red_list
= ospf
->redist
[i
];
612 for (ALL_LIST_ELEMENTS(red_list
, node
, nnode
, red
))
613 ospf_redistribute_unset(ospf
, i
, red
->instance
);
615 ospf_redistribute_default_unset(ospf
);
617 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
))
618 ospf_remove_vls_through_area(ospf
, area
);
620 for (ALL_LIST_ELEMENTS(ospf
->vlinks
, node
, nnode
, vl_data
))
621 ospf_vl_delete(ospf
, vl_data
);
623 list_delete_and_null(&ospf
->vlinks
);
625 /* Remove any ospf interface config params */
626 FOR_ALL_INTERFACES (vrf
, ifp
) {
627 struct ospf_if_params
*params
;
629 params
= IF_DEF_PARAMS(ifp
);
630 if (OSPF_IF_PARAM_CONFIGURED(params
, if_area
))
631 UNSET_IF_PARAM(params
, if_area
);
634 /* Reset interface. */
635 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
))
637 list_delete_and_null(&ospf
->oiflist
);
639 /* De-Register VRF */
640 ospf_zebra_vrf_deregister(ospf
);
642 /* Clear static neighbors */
643 for (rn
= route_top(ospf
->nbr_nbma
); rn
; rn
= route_next(rn
))
644 if ((nbr_nbma
= rn
->info
)) {
645 OSPF_POLL_TIMER_OFF(nbr_nbma
->t_poll
);
648 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
649 nbr_nbma
->nbr
= NULL
;
653 listnode_delete(nbr_nbma
->oi
->nbr_nbma
,
658 XFREE(MTYPE_OSPF_NEIGHBOR_STATIC
, nbr_nbma
);
661 route_table_finish(ospf
->nbr_nbma
);
663 /* Clear networks and Areas. */
664 for (rn
= route_top(ospf
->networks
); rn
; rn
= route_next(rn
)) {
665 struct ospf_network
*network
;
667 if ((network
= rn
->info
) != NULL
) {
668 ospf_network_free(ospf
, network
);
670 route_unlock_node(rn
);
673 route_table_finish(ospf
->networks
);
675 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
676 listnode_delete(ospf
->areas
, area
);
677 ospf_area_free(area
);
680 /* Cancel all timers. */
681 OSPF_TIMER_OFF(ospf
->t_external_lsa
);
682 OSPF_TIMER_OFF(ospf
->t_spf_calc
);
683 OSPF_TIMER_OFF(ospf
->t_ase_calc
);
684 OSPF_TIMER_OFF(ospf
->t_maxage
);
685 OSPF_TIMER_OFF(ospf
->t_maxage_walker
);
686 OSPF_TIMER_OFF(ospf
->t_abr_task
);
687 OSPF_TIMER_OFF(ospf
->t_asbr_check
);
688 OSPF_TIMER_OFF(ospf
->t_distribute_update
);
689 OSPF_TIMER_OFF(ospf
->t_lsa_refresher
);
690 OSPF_TIMER_OFF(ospf
->t_read
);
691 OSPF_TIMER_OFF(ospf
->t_write
);
692 OSPF_TIMER_OFF(ospf
->t_opaque_lsa_self
);
695 stream_free(ospf
->ibuf
);
697 LSDB_LOOP(OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
698 ospf_discard_from_db(ospf
, ospf
->lsdb
, lsa
);
699 LSDB_LOOP(EXTERNAL_LSDB(ospf
), rn
, lsa
)
700 ospf_discard_from_db(ospf
, ospf
->lsdb
, lsa
);
702 ospf_lsdb_delete_all(ospf
->lsdb
);
703 ospf_lsdb_free(ospf
->lsdb
);
705 for (rn
= route_top(ospf
->maxage_lsa
); rn
; rn
= route_next(rn
)) {
706 struct ospf_lsa
*lsa
;
708 if ((lsa
= rn
->info
) != NULL
) {
709 ospf_lsa_unlock(&lsa
);
712 route_unlock_node(rn
);
714 route_table_finish(ospf
->maxage_lsa
);
717 ospf_route_table_free(ospf
->old_table
);
718 if (ospf
->new_table
) {
719 ospf_route_delete(ospf
, ospf
->new_table
);
720 ospf_route_table_free(ospf
->new_table
);
723 ospf_rtrs_free(ospf
->old_rtrs
);
725 ospf_rtrs_free(ospf
->new_rtrs
);
726 if (ospf
->new_external_route
) {
727 ospf_route_delete(ospf
, ospf
->new_external_route
);
728 ospf_route_table_free(ospf
->new_external_route
);
730 if (ospf
->old_external_route
) {
731 ospf_route_delete(ospf
, ospf
->old_external_route
);
732 ospf_route_table_free(ospf
->old_external_route
);
734 if (ospf
->external_lsas
) {
735 ospf_ase_external_lsas_finish(ospf
->external_lsas
);
738 list_delete_and_null(&ospf
->areas
);
739 list_delete_and_null(&ospf
->oi_write_q
);
741 for (i
= ZEBRA_ROUTE_SYSTEM
; i
<= ZEBRA_ROUTE_MAX
; i
++) {
742 struct list
*ext_list
;
743 struct listnode
*node
;
744 struct ospf_external
*ext
;
746 ext_list
= om
->external
[i
];
750 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
751 if (ext
->external_info
)
752 for (rn
= route_top(ext
->external_info
); rn
;
753 rn
= route_next(rn
)) {
754 if (rn
->info
== NULL
)
757 XFREE(MTYPE_OSPF_EXTERNAL_INFO
,
760 route_unlock_node(rn
);
765 ospf_distance_reset(ospf
);
766 route_table_finish(ospf
->distance_table
);
768 if (!CHECK_FLAG(om
->options
, OSPF_MASTER_SHUTDOWN
))
769 instance
= ospf
->instance
;
774 vrf
= vrf_lookup_by_name(ospf
->name
);
776 ospf_vrf_unlink(ospf
, vrf
);
777 XFREE(MTYPE_OSPF_TOP
, ospf
->name
);
779 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
781 ospf_vrf_unlink(ospf
, vrf
);
784 XFREE(MTYPE_OSPF_TOP
, ospf
);
786 if (!CHECK_FLAG(om
->options
, OSPF_MASTER_SHUTDOWN
))
787 ospf_get_instance(instance
);
791 /* allocate new OSPF Area object */
792 static struct ospf_area
*ospf_area_new(struct ospf
*ospf
,
793 struct in_addr area_id
)
795 struct ospf_area
*new;
797 /* Allocate new config_network. */
798 new = XCALLOC(MTYPE_OSPF_AREA
, sizeof(struct ospf_area
));
802 new->area_id
= area_id
;
803 new->area_id_fmt
= OSPF_AREA_ID_FMT_DOTTEDQUAD
;
805 new->external_routing
= OSPF_AREA_DEFAULT
;
806 new->default_cost
= 1;
807 new->auth_type
= OSPF_AUTH_NULL
;
810 new->lsdb
= ospf_lsdb_new();
812 /* Self-originated LSAs initialize. */
813 new->router_lsa_self
= NULL
;
815 ospf_opaque_type10_lsa_init(new);
817 new->oiflist
= list_new();
818 new->ranges
= route_table_init();
820 if (area_id
.s_addr
== OSPF_AREA_BACKBONE
)
821 ospf
->backbone
= new;
826 static void ospf_area_free(struct ospf_area
*area
)
828 struct route_node
*rn
;
829 struct ospf_lsa
*lsa
;
831 ospf_opaque_type10_lsa_term(area
);
834 LSDB_LOOP(ROUTER_LSDB(area
), rn
, lsa
)
835 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
836 LSDB_LOOP(NETWORK_LSDB(area
), rn
, lsa
)
837 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
838 LSDB_LOOP(SUMMARY_LSDB(area
), rn
, lsa
)
839 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
840 LSDB_LOOP(ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
841 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
843 LSDB_LOOP(NSSA_LSDB(area
), rn
, lsa
)
844 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
845 LSDB_LOOP(OPAQUE_AREA_LSDB(area
), rn
, lsa
)
846 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
847 LSDB_LOOP(OPAQUE_LINK_LSDB(area
), rn
, lsa
)
848 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
850 ospf_opaque_type10_lsa_term(area
);
851 ospf_lsdb_delete_all(area
->lsdb
);
852 ospf_lsdb_free(area
->lsdb
);
854 ospf_lsa_unlock(&area
->router_lsa_self
);
856 route_table_finish(area
->ranges
);
857 list_delete_and_null(&area
->oiflist
);
859 if (EXPORT_NAME(area
))
860 free(EXPORT_NAME(area
));
862 if (IMPORT_NAME(area
))
863 free(IMPORT_NAME(area
));
866 OSPF_TIMER_OFF(area
->t_stub_router
);
867 OSPF_TIMER_OFF(area
->t_opaque_lsa_self
);
869 if (OSPF_IS_AREA_BACKBONE(area
))
870 area
->ospf
->backbone
= NULL
;
872 XFREE(MTYPE_OSPF_AREA
, area
);
875 void ospf_area_check_free(struct ospf
*ospf
, struct in_addr area_id
)
877 struct ospf_area
*area
;
879 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
880 if (area
&& listcount(area
->oiflist
) == 0 && area
->ranges
->top
== NULL
881 && area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
882 && area
->external_routing
== OSPF_AREA_DEFAULT
883 && area
->no_summary
== 0 && area
->default_cost
== 1
884 && EXPORT_NAME(area
) == NULL
&& IMPORT_NAME(area
) == NULL
885 && area
->auth_type
== OSPF_AUTH_NULL
) {
886 listnode_delete(ospf
->areas
, area
);
887 ospf_area_free(area
);
891 struct ospf_area
*ospf_area_get(struct ospf
*ospf
, struct in_addr area_id
)
893 struct ospf_area
*area
;
895 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
897 area
= ospf_area_new(ospf
, area_id
);
898 listnode_add_sort(ospf
->areas
, area
);
899 ospf_check_abr_status(ospf
);
900 if (ospf
->stub_router_admin_set
901 == OSPF_STUB_ROUTER_ADMINISTRATIVE_SET
) {
902 SET_FLAG(area
->stub_router_state
,
903 OSPF_AREA_ADMIN_STUB_ROUTED
);
910 struct ospf_area
*ospf_area_lookup_by_area_id(struct ospf
*ospf
,
911 struct in_addr area_id
)
913 struct ospf_area
*area
;
914 struct listnode
*node
;
916 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, node
, area
))
917 if (IPV4_ADDR_SAME(&area
->area_id
, &area_id
))
923 void ospf_area_add_if(struct ospf_area
*area
, struct ospf_interface
*oi
)
925 listnode_add(area
->oiflist
, oi
);
928 void ospf_area_del_if(struct ospf_area
*area
, struct ospf_interface
*oi
)
930 listnode_delete(area
->oiflist
, oi
);
934 static void add_ospf_interface(struct connected
*co
, struct ospf_area
*area
)
936 struct ospf_interface
*oi
;
938 oi
= ospf_if_new(area
->ospf
, co
->ifp
, co
->address
);
943 oi
->params
= ospf_lookup_if_params(co
->ifp
, oi
->address
->u
.prefix4
);
944 oi
->output_cost
= ospf_if_get_output_cost(oi
);
946 /* Relate ospf interface to ospf instance. */
947 oi
->ospf
= area
->ospf
;
949 /* update network type as interface flag */
950 /* If network type is specified previously,
951 skip network type setting. */
952 oi
->type
= IF_DEF_PARAMS(co
->ifp
)->type
;
954 /* Add pseudo neighbor. */
955 ospf_nbr_self_reset(oi
, oi
->ospf
->router_id
);
957 ospf_area_add_if(oi
->area
, oi
);
960 * if router_id is not configured, dont bring up
962 * ospf_router_id_update() will call ospf_if_update
963 * whenever r-id is configured instead.
965 if ((area
->ospf
->router_id
.s_addr
!= 0) && if_is_operative(co
->ifp
))
969 static void update_redistributed(struct ospf
*ospf
, int add_to_ospf
)
971 struct route_node
*rn
;
972 struct external_info
*ei
;
973 struct ospf_external
*ext
;
975 if (ospf_is_type_redistributed(ospf
, ZEBRA_ROUTE_CONNECT
, 0))
976 if ((ext
= ospf_external_lookup(ZEBRA_ROUTE_CONNECT
, 0))
977 && EXTERNAL_INFO(ext
)) {
978 for (rn
= route_top(EXTERNAL_INFO(ext
)); rn
;
979 rn
= route_next(rn
)) {
980 if ((ei
= rn
->info
) != NULL
) {
982 if (ospf_external_info_find_lsa(
984 if (!ospf_distribute_check_connected(
986 ospf_external_lsa_flush(
990 ei
->ifindex
/*, ei->nexthop */);
992 if (!ospf_external_info_find_lsa(
994 if (ospf_distribute_check_connected(
996 ospf_external_lsa_originate(
1005 /* Config network statement related functions. */
1006 static struct ospf_network
*ospf_network_new(struct in_addr area_id
)
1008 struct ospf_network
*new;
1009 new = XCALLOC(MTYPE_OSPF_NETWORK
, sizeof(struct ospf_network
));
1011 new->area_id
= area_id
;
1012 new->area_id_fmt
= OSPF_AREA_ID_FMT_DOTTEDQUAD
;
1017 static void ospf_network_free(struct ospf
*ospf
, struct ospf_network
*network
)
1019 ospf_area_check_free(ospf
, network
->area_id
);
1020 ospf_schedule_abr_task(ospf
);
1021 XFREE(MTYPE_OSPF_NETWORK
, network
);
1024 int ospf_network_set(struct ospf
*ospf
, struct prefix_ipv4
*p
,
1025 struct in_addr area_id
, int df
)
1027 struct ospf_network
*network
;
1028 struct ospf_area
*area
;
1029 struct route_node
*rn
;
1031 rn
= route_node_get(ospf
->networks
, (struct prefix
*)p
);
1033 /* There is already same network statement. */
1034 route_unlock_node(rn
);
1038 rn
->info
= network
= ospf_network_new(area_id
);
1039 network
->area_id_fmt
= df
;
1040 area
= ospf_area_get(ospf
, area_id
);
1041 ospf_area_display_format_set(ospf
, area
, df
);
1043 /* Run network config now. */
1044 ospf_network_run((struct prefix
*)p
, area
);
1046 /* Update connected redistribute. */
1047 update_redistributed(ospf
, 1); /* interfaces possibly added */
1049 ospf_area_check_free(ospf
, area_id
);
1054 int ospf_network_unset(struct ospf
*ospf
, struct prefix_ipv4
*p
,
1055 struct in_addr area_id
)
1057 struct route_node
*rn
;
1058 struct ospf_network
*network
;
1059 struct listnode
*node
, *nnode
;
1060 struct ospf_interface
*oi
;
1062 rn
= route_node_lookup(ospf
->networks
, (struct prefix
*)p
);
1067 route_unlock_node(rn
);
1068 if (!IPV4_ADDR_SAME(&area_id
, &network
->area_id
))
1071 ospf_network_free(ospf
, rn
->info
);
1073 route_unlock_node(rn
); /* initial reference */
1075 /* Find interfaces that are not configured already. */
1076 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
)) {
1078 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
1081 ospf_network_run_subnet(ospf
, oi
->connected
, NULL
, NULL
);
1084 /* Update connected redistribute. */
1085 update_redistributed(ospf
, 0); /* interfaces possibly removed */
1086 ospf_area_check_free(ospf
, area_id
);
1091 /* Ensure there's an OSPF instance, as "ip ospf area" enabled OSPF means
1092 * there might not be any 'router ospf' config.
1094 * Otherwise, doesn't do anything different to ospf_if_update for now
1096 void ospf_interface_area_set(struct ospf
*ospf
, struct interface
*ifp
)
1101 ospf_if_update(ospf
, ifp
);
1102 /* if_update does a update_redistributed */
1107 void ospf_interface_area_unset(struct ospf
*ospf
, struct interface
*ifp
)
1109 struct route_node
*rn_oi
;
1112 return; /* Ospf not ready yet */
1114 /* Find interfaces that may need to be removed. */
1115 for (rn_oi
= route_top(IF_OIFS(ifp
)); rn_oi
;
1116 rn_oi
= route_next(rn_oi
)) {
1117 struct ospf_interface
*oi
= NULL
;
1119 if ((oi
= rn_oi
->info
) == NULL
)
1122 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
1125 ospf_network_run_subnet(ospf
, oi
->connected
, NULL
, NULL
);
1128 /* Update connected redistribute. */
1129 update_redistributed(ospf
, 0); /* interfaces possibly removed */
1132 /* Check whether interface matches given network
1133 * returns: 1, true. 0, false
1135 static int ospf_network_match_iface(const struct connected
*co
,
1136 const struct prefix
*net
)
1138 /* new approach: more elegant and conceptually clean */
1139 return prefix_match_network_statement(net
, CONNECTED_PREFIX(co
));
1142 static void ospf_update_interface_area(struct connected
*co
,
1143 struct ospf_area
*area
)
1145 struct ospf_interface
*oi
= ospf_if_table_lookup(co
->ifp
, co
->address
);
1147 /* nothing to be done case */
1148 if (oi
&& oi
->area
== area
) {
1155 add_ospf_interface(co
, area
);
1158 /* Run OSPF for the given subnet, taking into account the following
1159 * possible sources of area configuration, in the given order of preference:
1161 * - Whether there is interface+address specific area configuration
1162 * - Whether there is a default area for the interface
1163 * - Whether there is an area given as a parameter.
1164 * - If no specific network prefix/area is supplied, whether there's
1165 * a matching network configured.
1167 static void ospf_network_run_subnet(struct ospf
*ospf
, struct connected
*co
,
1169 struct ospf_area
*given_area
)
1171 struct ospf_interface
*oi
;
1172 struct ospf_if_params
*params
;
1173 struct ospf_area
*area
= NULL
;
1174 struct route_node
*rn
;
1177 if (CHECK_FLAG(co
->flags
, ZEBRA_IFA_SECONDARY
))
1180 if (co
->address
->family
!= AF_INET
)
1183 /* Try determine the appropriate area for this interface + address
1184 * Start by checking interface config
1186 params
= ospf_lookup_if_params(co
->ifp
, co
->address
->u
.prefix4
);
1187 if (params
&& OSPF_IF_PARAM_CONFIGURED(params
, if_area
))
1188 area
= ospf_area_get(ospf
, params
->if_area
);
1190 params
= IF_DEF_PARAMS(co
->ifp
);
1191 if (OSPF_IF_PARAM_CONFIGURED(params
, if_area
))
1192 area
= ospf_area_get(ospf
, params
->if_area
);
1195 /* If we've found an interface and/or addr specific area, then we're
1199 ospf_update_interface_area(co
, area
);
1203 /* Otherwise, only remaining possibility is a matching network statement
1206 assert(given_area
!= NULL
);
1208 /* Which either was supplied as a parameter.. (e.g. cause a new
1209 * network/area was just added)..
1211 if (p
->family
== co
->address
->family
1212 && ospf_network_match_iface(co
, p
))
1213 ospf_update_interface_area(co
, given_area
);
1218 /* Else we have to search the existing network/area config to see
1221 for (rn
= route_top(ospf
->networks
); rn
; rn
= route_next(rn
))
1222 if (rn
->info
!= NULL
&& ospf_network_match_iface(co
, &rn
->p
)) {
1223 struct ospf_network
*network
=
1224 (struct ospf_network
*)rn
->info
;
1225 area
= ospf_area_get(ospf
, network
->area_id
);
1226 ospf_update_interface_area(co
, area
);
1230 /* If the subnet isn't in any area, deconfigure */
1231 if (!configed
&& (oi
= ospf_if_table_lookup(co
->ifp
, co
->address
)))
1235 static void ospf_network_run_interface(struct ospf
*ospf
, struct interface
*ifp
,
1237 struct ospf_area
*given_area
)
1239 struct listnode
*cnode
;
1240 struct connected
*co
;
1242 if (memcmp(ifp
->name
, "VLINK", 5) == 0)
1245 /* Network prefix without area is nonsensical */
1247 assert(given_area
!= NULL
);
1249 /* if interface prefix is match specified prefix,
1250 then create socket and join multicast group. */
1251 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, co
))
1252 ospf_network_run_subnet(ospf
, co
, p
, given_area
);
1255 static void ospf_network_run(struct prefix
*p
, struct ospf_area
*area
)
1257 struct vrf
*vrf
= vrf_lookup_by_id(area
->ospf
->vrf_id
);
1258 struct interface
*ifp
;
1260 /* Schedule Router ID Update. */
1261 if (area
->ospf
->router_id
.s_addr
== 0)
1262 ospf_router_id_update(area
->ospf
);
1264 /* Get target interface. */
1265 FOR_ALL_INTERFACES (vrf
, ifp
)
1266 ospf_network_run_interface(area
->ospf
, ifp
, p
, area
);
1269 void ospf_ls_upd_queue_empty(struct ospf_interface
*oi
)
1271 struct route_node
*rn
;
1272 struct listnode
*node
, *nnode
;
1274 struct ospf_lsa
*lsa
;
1276 /* empty ls update queue */
1277 for (rn
= route_top(oi
->ls_upd_queue
); rn
; rn
= route_next(rn
))
1278 if ((lst
= (struct list
*)rn
->info
)) {
1279 for (ALL_LIST_ELEMENTS(lst
, node
, nnode
, lsa
))
1280 ospf_lsa_unlock(&lsa
); /* oi->ls_upd_queue */
1281 list_delete_and_null(&lst
);
1285 /* remove update event */
1286 if (oi
->t_ls_upd_event
) {
1287 thread_cancel(oi
->t_ls_upd_event
);
1288 oi
->t_ls_upd_event
= NULL
;
1292 void ospf_if_update(struct ospf
*ospf
, struct interface
*ifp
)
1298 if (IS_DEBUG_OSPF_EVENT
)
1299 zlog_debug("%s: interface %s ifp->vrf_id %u ospf vrf %s vrf_id %u router_id %s",
1300 __PRETTY_FUNCTION__
, ifp
->name
, ifp
->vrf_id
,
1301 ospf_vrf_id_to_name(ospf
->vrf_id
), ospf
->vrf_id
,
1302 inet_ntoa(ospf
->router_id
));
1304 /* OSPF must be ready. */
1305 if (!ospf_is_ready(ospf
))
1308 ospf_network_run_interface(ospf
, ifp
, NULL
, NULL
);
1310 /* Update connected redistribute. */
1311 update_redistributed(ospf
, 1);
1314 void ospf_remove_vls_through_area(struct ospf
*ospf
, struct ospf_area
*area
)
1316 struct listnode
*node
, *nnode
;
1317 struct ospf_vl_data
*vl_data
;
1319 for (ALL_LIST_ELEMENTS(ospf
->vlinks
, node
, nnode
, vl_data
))
1320 if (IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
))
1321 ospf_vl_delete(ospf
, vl_data
);
1325 static const struct message ospf_area_type_msg
[] = {
1326 {OSPF_AREA_DEFAULT
, "Default"},
1327 {OSPF_AREA_STUB
, "Stub"},
1328 {OSPF_AREA_NSSA
, "NSSA"},
1331 static void ospf_area_type_set(struct ospf_area
*area
, int type
)
1333 struct listnode
*node
;
1334 struct ospf_interface
*oi
;
1336 if (area
->external_routing
== type
) {
1337 if (IS_DEBUG_OSPF_EVENT
)
1338 zlog_debug("Area[%s]: Types are the same, ignored.",
1339 inet_ntoa(area
->area_id
));
1343 area
->external_routing
= type
;
1345 if (IS_DEBUG_OSPF_EVENT
)
1346 zlog_debug("Area[%s]: Configured as %s",
1347 inet_ntoa(area
->area_id
),
1348 lookup_msg(ospf_area_type_msg
, type
, NULL
));
1350 switch (area
->external_routing
) {
1351 case OSPF_AREA_DEFAULT
:
1352 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
))
1353 if (oi
->nbr_self
!= NULL
) {
1354 UNSET_FLAG(oi
->nbr_self
->options
,
1356 SET_FLAG(oi
->nbr_self
->options
, OSPF_OPTION_E
);
1359 case OSPF_AREA_STUB
:
1360 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
))
1361 if (oi
->nbr_self
!= NULL
) {
1362 if (IS_DEBUG_OSPF_EVENT
)
1364 "setting options on %s accordingly",
1366 UNSET_FLAG(oi
->nbr_self
->options
,
1368 UNSET_FLAG(oi
->nbr_self
->options
,
1370 if (IS_DEBUG_OSPF_EVENT
)
1371 zlog_debug("options set on %s: %x",
1372 IF_NAME(oi
), OPTIONS(oi
));
1375 case OSPF_AREA_NSSA
:
1376 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
))
1377 if (oi
->nbr_self
!= NULL
) {
1379 "setting nssa options on %s accordingly",
1381 UNSET_FLAG(oi
->nbr_self
->options
,
1383 SET_FLAG(oi
->nbr_self
->options
, OSPF_OPTION_NP
);
1384 zlog_debug("options set on %s: %x", IF_NAME(oi
),
1392 ospf_router_lsa_update_area(area
);
1393 ospf_schedule_abr_task(area
->ospf
);
1396 int ospf_area_shortcut_set(struct ospf
*ospf
, struct ospf_area
*area
, int mode
)
1398 if (area
->shortcut_configured
== mode
)
1401 area
->shortcut_configured
= mode
;
1402 ospf_router_lsa_update_area(area
);
1403 ospf_schedule_abr_task(ospf
);
1405 ospf_area_check_free(ospf
, area
->area_id
);
1410 int ospf_area_shortcut_unset(struct ospf
*ospf
, struct ospf_area
*area
)
1412 area
->shortcut_configured
= OSPF_SHORTCUT_DEFAULT
;
1413 ospf_router_lsa_update_area(area
);
1414 ospf_area_check_free(ospf
, area
->area_id
);
1415 ospf_schedule_abr_task(ospf
);
1420 static int ospf_area_vlink_count(struct ospf
*ospf
, struct ospf_area
*area
)
1422 struct ospf_vl_data
*vl
;
1423 struct listnode
*node
;
1426 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl
))
1427 if (IPV4_ADDR_SAME(&vl
->vl_area_id
, &area
->area_id
))
1433 int ospf_area_display_format_set(struct ospf
*ospf
, struct ospf_area
*area
,
1436 area
->area_id_fmt
= df
;
1441 int ospf_area_stub_set(struct ospf
*ospf
, struct in_addr area_id
)
1443 struct ospf_area
*area
;
1445 area
= ospf_area_get(ospf
, area_id
);
1446 if (ospf_area_vlink_count(ospf
, area
))
1449 if (area
->external_routing
!= OSPF_AREA_STUB
)
1450 ospf_area_type_set(area
, OSPF_AREA_STUB
);
1455 int ospf_area_stub_unset(struct ospf
*ospf
, struct in_addr area_id
)
1457 struct ospf_area
*area
;
1459 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1463 if (area
->external_routing
== OSPF_AREA_STUB
)
1464 ospf_area_type_set(area
, OSPF_AREA_DEFAULT
);
1466 ospf_area_check_free(ospf
, area_id
);
1471 int ospf_area_no_summary_set(struct ospf
*ospf
, struct in_addr area_id
)
1473 struct ospf_area
*area
;
1475 area
= ospf_area_get(ospf
, area_id
);
1476 area
->no_summary
= 1;
1481 int ospf_area_no_summary_unset(struct ospf
*ospf
, struct in_addr area_id
)
1483 struct ospf_area
*area
;
1485 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1489 area
->no_summary
= 0;
1490 ospf_area_check_free(ospf
, area_id
);
1495 int ospf_area_nssa_no_summary_set(struct ospf
*ospf
, struct in_addr area_id
)
1497 struct ospf_area
*area
;
1499 area
= ospf_area_get(ospf
, area_id
);
1500 if (ospf_area_vlink_count(ospf
, area
))
1503 if (area
->external_routing
!= OSPF_AREA_NSSA
) {
1504 ospf_area_type_set(area
, OSPF_AREA_NSSA
);
1506 area
->NSSATranslatorRole
= OSPF_NSSA_ROLE_CANDIDATE
;
1509 ospf_area_no_summary_set(ospf
, area_id
);
1514 int ospf_area_nssa_set(struct ospf
*ospf
, struct in_addr area_id
)
1516 struct ospf_area
*area
;
1518 area
= ospf_area_get(ospf
, area_id
);
1519 if (ospf_area_vlink_count(ospf
, area
))
1522 if (area
->external_routing
!= OSPF_AREA_NSSA
) {
1523 ospf_area_type_set(area
, OSPF_AREA_NSSA
);
1526 /* set NSSA area defaults */
1527 area
->no_summary
= 0;
1528 area
->NSSATranslatorRole
= OSPF_NSSA_ROLE_CANDIDATE
;
1529 area
->NSSATranslatorState
= OSPF_NSSA_TRANSLATE_DISABLED
;
1530 area
->NSSATranslatorStabilityInterval
=
1531 OSPF_NSSA_TRANS_STABLE_DEFAULT
;
1536 int ospf_area_nssa_unset(struct ospf
*ospf
, struct in_addr area_id
, int argc
)
1538 struct ospf_area
*area
;
1540 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1544 /* argc < 5 -> 'no area x nssa' */
1545 if (argc
< 5 && area
->external_routing
== OSPF_AREA_NSSA
) {
1547 /* set NSSA area defaults */
1548 area
->no_summary
= 0;
1549 area
->NSSATranslatorRole
= OSPF_NSSA_ROLE_CANDIDATE
;
1550 area
->NSSATranslatorState
= OSPF_NSSA_TRANSLATE_DISABLED
;
1551 area
->NSSATranslatorStabilityInterval
=
1552 OSPF_NSSA_TRANS_STABLE_DEFAULT
;
1553 ospf_area_type_set(area
, OSPF_AREA_DEFAULT
);
1555 area
->NSSATranslatorRole
= OSPF_NSSA_ROLE_CANDIDATE
;
1558 ospf_area_check_free(ospf
, area_id
);
1563 int ospf_area_nssa_translator_role_set(struct ospf
*ospf
,
1564 struct in_addr area_id
, int role
)
1566 struct ospf_area
*area
;
1568 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1572 area
->NSSATranslatorRole
= role
;
1578 /* XXX: unused? Leave for symmetry? */
1580 ospf_area_nssa_translator_role_unset (struct ospf
*ospf
,
1581 struct in_addr area_id
)
1583 struct ospf_area
*area
;
1585 area
= ospf_area_lookup_by_area_id (ospf
, area_id
);
1589 area
->NSSATranslatorRole
= OSPF_NSSA_ROLE_CANDIDATE
;
1591 ospf_area_check_free (ospf
, area_id
);
1597 int ospf_area_export_list_set(struct ospf
*ospf
, struct ospf_area
*area
,
1598 const char *list_name
)
1600 struct access_list
*list
;
1601 list
= access_list_lookup(AFI_IP
, list_name
);
1603 EXPORT_LIST(area
) = list
;
1605 if (EXPORT_NAME(area
))
1606 free(EXPORT_NAME(area
));
1608 EXPORT_NAME(area
) = strdup(list_name
);
1609 ospf_schedule_abr_task(ospf
);
1614 int ospf_area_export_list_unset(struct ospf
*ospf
, struct ospf_area
*area
)
1617 EXPORT_LIST(area
) = 0;
1619 if (EXPORT_NAME(area
))
1620 free(EXPORT_NAME(area
));
1622 EXPORT_NAME(area
) = NULL
;
1624 ospf_area_check_free(ospf
, area
->area_id
);
1626 ospf_schedule_abr_task(ospf
);
1631 int ospf_area_import_list_set(struct ospf
*ospf
, struct ospf_area
*area
,
1634 struct access_list
*list
;
1635 list
= access_list_lookup(AFI_IP
, name
);
1637 IMPORT_LIST(area
) = list
;
1639 if (IMPORT_NAME(area
))
1640 free(IMPORT_NAME(area
));
1642 IMPORT_NAME(area
) = strdup(name
);
1643 ospf_schedule_abr_task(ospf
);
1648 int ospf_area_import_list_unset(struct ospf
*ospf
, struct ospf_area
*area
)
1650 IMPORT_LIST(area
) = 0;
1652 if (IMPORT_NAME(area
))
1653 free(IMPORT_NAME(area
));
1655 IMPORT_NAME(area
) = NULL
;
1656 ospf_area_check_free(ospf
, area
->area_id
);
1658 ospf_schedule_abr_task(ospf
);
1663 int ospf_timers_refresh_set(struct ospf
*ospf
, int interval
)
1667 if (ospf
->lsa_refresh_interval
== interval
)
1670 time_left
= ospf
->lsa_refresh_interval
1671 - (monotime(NULL
) - ospf
->lsa_refresher_started
);
1673 if (time_left
> interval
) {
1674 OSPF_TIMER_OFF(ospf
->t_lsa_refresher
);
1675 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
1676 interval
, &ospf
->t_lsa_refresher
);
1678 ospf
->lsa_refresh_interval
= interval
;
1683 int ospf_timers_refresh_unset(struct ospf
*ospf
)
1687 time_left
= ospf
->lsa_refresh_interval
1688 - (monotime(NULL
) - ospf
->lsa_refresher_started
);
1690 if (time_left
> OSPF_LSA_REFRESH_INTERVAL_DEFAULT
) {
1691 OSPF_TIMER_OFF(ospf
->t_lsa_refresher
);
1692 ospf
->t_lsa_refresher
= NULL
;
1693 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
1694 OSPF_LSA_REFRESH_INTERVAL_DEFAULT
,
1695 &ospf
->t_lsa_refresher
);
1698 ospf
->lsa_refresh_interval
= OSPF_LSA_REFRESH_INTERVAL_DEFAULT
;
1704 static struct ospf_nbr_nbma
*ospf_nbr_nbma_new(void)
1706 struct ospf_nbr_nbma
*nbr_nbma
;
1708 nbr_nbma
= XCALLOC(MTYPE_OSPF_NEIGHBOR_STATIC
,
1709 sizeof(struct ospf_nbr_nbma
));
1711 nbr_nbma
->priority
= OSPF_NEIGHBOR_PRIORITY_DEFAULT
;
1712 nbr_nbma
->v_poll
= OSPF_POLL_INTERVAL_DEFAULT
;
1717 static void ospf_nbr_nbma_free(struct ospf_nbr_nbma
*nbr_nbma
)
1719 XFREE(MTYPE_OSPF_NEIGHBOR_STATIC
, nbr_nbma
);
1722 static void ospf_nbr_nbma_delete(struct ospf
*ospf
,
1723 struct ospf_nbr_nbma
*nbr_nbma
)
1725 struct route_node
*rn
;
1726 struct prefix_ipv4 p
;
1729 p
.prefix
= nbr_nbma
->addr
;
1730 p
.prefixlen
= IPV4_MAX_BITLEN
;
1732 rn
= route_node_lookup(ospf
->nbr_nbma
, (struct prefix
*)&p
);
1734 ospf_nbr_nbma_free(rn
->info
);
1736 route_unlock_node(rn
);
1737 route_unlock_node(rn
);
1741 static void ospf_nbr_nbma_down(struct ospf_nbr_nbma
*nbr_nbma
)
1743 OSPF_TIMER_OFF(nbr_nbma
->t_poll
);
1745 if (nbr_nbma
->nbr
) {
1746 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
1747 OSPF_NSM_EVENT_EXECUTE(nbr_nbma
->nbr
, NSM_KillNbr
);
1751 listnode_delete(nbr_nbma
->oi
->nbr_nbma
, nbr_nbma
);
1754 static void ospf_nbr_nbma_add(struct ospf_nbr_nbma
*nbr_nbma
,
1755 struct ospf_interface
*oi
)
1757 struct ospf_neighbor
*nbr
;
1758 struct route_node
*rn
;
1761 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
1764 if (nbr_nbma
->nbr
!= NULL
)
1767 if (IPV4_ADDR_SAME(&oi
->nbr_self
->address
.u
.prefix4
, &nbr_nbma
->addr
))
1771 listnode_add(oi
->nbr_nbma
, nbr_nbma
);
1773 /* Get neighbor information from table. */
1775 p
.prefixlen
= IPV4_MAX_BITLEN
;
1776 p
.u
.prefix4
= nbr_nbma
->addr
;
1778 rn
= route_node_get(oi
->nbrs
, (struct prefix
*)&p
);
1781 nbr
->nbr_nbma
= nbr_nbma
;
1782 nbr_nbma
->nbr
= nbr
;
1784 route_unlock_node(rn
);
1786 nbr
= rn
->info
= ospf_nbr_new(oi
);
1787 nbr
->state
= NSM_Down
;
1788 nbr
->src
= nbr_nbma
->addr
;
1789 nbr
->nbr_nbma
= nbr_nbma
;
1790 nbr
->priority
= nbr_nbma
->priority
;
1793 nbr_nbma
->nbr
= nbr
;
1795 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_Start
);
1799 void ospf_nbr_nbma_if_update(struct ospf
*ospf
, struct ospf_interface
*oi
)
1801 struct ospf_nbr_nbma
*nbr_nbma
;
1802 struct route_node
*rn
;
1803 struct prefix_ipv4 p
;
1805 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
1808 for (rn
= route_top(ospf
->nbr_nbma
); rn
; rn
= route_next(rn
))
1809 if ((nbr_nbma
= rn
->info
))
1810 if (nbr_nbma
->oi
== NULL
&& nbr_nbma
->nbr
== NULL
) {
1812 p
.prefix
= nbr_nbma
->addr
;
1813 p
.prefixlen
= IPV4_MAX_BITLEN
;
1815 if (prefix_match(oi
->address
,
1816 (struct prefix
*)&p
))
1817 ospf_nbr_nbma_add(nbr_nbma
, oi
);
1821 struct ospf_nbr_nbma
*ospf_nbr_nbma_lookup(struct ospf
*ospf
,
1822 struct in_addr nbr_addr
)
1824 struct route_node
*rn
;
1825 struct prefix_ipv4 p
;
1828 p
.prefix
= nbr_addr
;
1829 p
.prefixlen
= IPV4_MAX_BITLEN
;
1831 rn
= route_node_lookup(ospf
->nbr_nbma
, (struct prefix
*)&p
);
1833 route_unlock_node(rn
);
1839 struct ospf_nbr_nbma
*ospf_nbr_nbma_lookup_next(struct ospf
*ospf
,
1840 struct in_addr
*addr
, int first
)
1843 struct ospf_nbr_nbma
*nbr_nbma
;
1844 struct listnode
*node
;
1851 for (ALL_LIST_ELEMENTS_RO (ospf
->nbr_nbma
, node
, nbr_nbma
))
1855 *addr
= nbr_nbma
->addr
;
1858 else if (ntohl (nbr_nbma
->addr
.s_addr
) > ntohl (addr
->s_addr
))
1860 *addr
= nbr_nbma
->addr
;
1868 int ospf_nbr_nbma_set(struct ospf
*ospf
, struct in_addr nbr_addr
)
1870 struct ospf_nbr_nbma
*nbr_nbma
;
1871 struct ospf_interface
*oi
;
1872 struct prefix_ipv4 p
;
1873 struct route_node
*rn
;
1874 struct listnode
*node
;
1876 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1880 nbr_nbma
= ospf_nbr_nbma_new();
1881 nbr_nbma
->addr
= nbr_addr
;
1884 p
.prefix
= nbr_addr
;
1885 p
.prefixlen
= IPV4_MAX_BITLEN
;
1887 rn
= route_node_get(ospf
->nbr_nbma
, (struct prefix
*)&p
);
1889 route_unlock_node(rn
);
1890 rn
->info
= nbr_nbma
;
1892 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
1893 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1894 if (prefix_match(oi
->address
, (struct prefix
*)&p
)) {
1895 ospf_nbr_nbma_add(nbr_nbma
, oi
);
1903 int ospf_nbr_nbma_unset(struct ospf
*ospf
, struct in_addr nbr_addr
)
1905 struct ospf_nbr_nbma
*nbr_nbma
;
1907 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1908 if (nbr_nbma
== NULL
)
1911 ospf_nbr_nbma_down(nbr_nbma
);
1912 ospf_nbr_nbma_delete(ospf
, nbr_nbma
);
1917 int ospf_nbr_nbma_priority_set(struct ospf
*ospf
, struct in_addr nbr_addr
,
1920 struct ospf_nbr_nbma
*nbr_nbma
;
1922 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1923 if (nbr_nbma
== NULL
)
1926 if (nbr_nbma
->priority
!= priority
)
1927 nbr_nbma
->priority
= priority
;
1932 int ospf_nbr_nbma_priority_unset(struct ospf
*ospf
, struct in_addr nbr_addr
)
1934 struct ospf_nbr_nbma
*nbr_nbma
;
1936 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1937 if (nbr_nbma
== NULL
)
1940 if (nbr_nbma
!= OSPF_NEIGHBOR_PRIORITY_DEFAULT
)
1941 nbr_nbma
->priority
= OSPF_NEIGHBOR_PRIORITY_DEFAULT
;
1946 int ospf_nbr_nbma_poll_interval_set(struct ospf
*ospf
, struct in_addr nbr_addr
,
1947 unsigned int interval
)
1949 struct ospf_nbr_nbma
*nbr_nbma
;
1951 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1952 if (nbr_nbma
== NULL
)
1955 if (nbr_nbma
->v_poll
!= interval
) {
1956 nbr_nbma
->v_poll
= interval
;
1957 if (nbr_nbma
->oi
&& ospf_if_is_up(nbr_nbma
->oi
)) {
1958 OSPF_TIMER_OFF(nbr_nbma
->t_poll
);
1959 OSPF_POLL_TIMER_ON(nbr_nbma
->t_poll
, ospf_poll_timer
,
1967 int ospf_nbr_nbma_poll_interval_unset(struct ospf
*ospf
, struct in_addr addr
)
1969 struct ospf_nbr_nbma
*nbr_nbma
;
1971 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, addr
);
1972 if (nbr_nbma
== NULL
)
1975 if (nbr_nbma
->v_poll
!= OSPF_POLL_INTERVAL_DEFAULT
)
1976 nbr_nbma
->v_poll
= OSPF_POLL_INTERVAL_DEFAULT
;
1981 void ospf_master_init(struct thread_master
*master
)
1983 memset(&ospf_master
, 0, sizeof(struct ospf_master
));
1986 om
->ospf
= list_new();
1987 om
->master
= master
;
1990 /* Link OSPF instance to VRF. */
1991 void ospf_vrf_link(struct ospf
*ospf
, struct vrf
*vrf
)
1993 ospf
->vrf_id
= vrf
->vrf_id
;
1994 if (vrf
->info
!= (void *)ospf
)
1995 vrf
->info
= (void *)ospf
;
1998 /* Unlink OSPF instance from VRF. */
1999 void ospf_vrf_unlink(struct ospf
*ospf
, struct vrf
*vrf
)
2001 if (vrf
->info
== (void *)ospf
)
2003 ospf
->vrf_id
= VRF_UNKNOWN
;
2006 /* This is hook function for vrf create called as part of vrf_init */
2007 static int ospf_vrf_new(struct vrf
*vrf
)
2009 if (IS_DEBUG_OSPF_EVENT
)
2010 zlog_debug("%s: VRF Created: %s(%d)", __PRETTY_FUNCTION__
,
2011 vrf
->name
, vrf
->vrf_id
);
2016 /* This is hook function for vrf delete call as part of vrf_init */
2017 static int ospf_vrf_delete(struct vrf
*vrf
)
2019 if (IS_DEBUG_OSPF_EVENT
)
2020 zlog_debug("%s: VRF Deletion: %s(%d)", __PRETTY_FUNCTION__
,
2021 vrf
->name
, vrf
->vrf_id
);
2026 /* Enable OSPF VRF instance */
2027 static int ospf_vrf_enable(struct vrf
*vrf
)
2029 struct ospf
*ospf
= NULL
;
2030 vrf_id_t old_vrf_id
= VRF_DEFAULT
;
2032 if (IS_DEBUG_OSPF_EVENT
)
2033 zlog_debug("%s: VRF %s id %d enabled",
2034 __PRETTY_FUNCTION__
, vrf
->name
, vrf
->vrf_id
);
2036 ospf
= ospf_lookup_by_name(vrf
->name
);
2038 old_vrf_id
= ospf
->vrf_id
;
2039 /* We have instance configured, link to VRF and make it "up". */
2040 ospf_vrf_link(ospf
, vrf
);
2041 if (IS_DEBUG_OSPF_EVENT
)
2042 zlog_debug("%s: ospf linked to vrf %s vrf_id %d (old id %d)",
2043 __PRETTY_FUNCTION__
, vrf
->name
, ospf
->vrf_id
,
2046 if (old_vrf_id
!= ospf
->vrf_id
) {
2047 if (ospfd_privs
.change(ZPRIVS_RAISE
)) {
2048 zlog_err("ospf_sock_init: could not raise privs, %s",
2049 safe_strerror(errno
));
2051 if (ospf_bind_vrfdevice(ospf
, ospf
->fd
) < 0)
2053 if (ospfd_privs
.change(ZPRIVS_LOWER
)) {
2054 zlog_err("ospf_sock_init: could not lower privs, %s",
2055 safe_strerror(errno
));
2058 ospf
->oi_running
= 1;
2059 ospf_router_id_update(ospf
);
2066 /* Disable OSPF VRF instance */
2067 static int ospf_vrf_disable(struct vrf
*vrf
)
2069 struct ospf
*ospf
= NULL
;
2070 vrf_id_t old_vrf_id
= VRF_UNKNOWN
;
2072 if (vrf
->vrf_id
== VRF_DEFAULT
)
2075 if (IS_DEBUG_OSPF_EVENT
)
2076 zlog_debug("%s: VRF %s id %d disabled.",
2077 __PRETTY_FUNCTION__
, vrf
->name
, vrf
->vrf_id
);
2079 ospf
= ospf_lookup_by_name(vrf
->name
);
2081 old_vrf_id
= ospf
->vrf_id
;
2083 /* We have instance configured, unlink
2084 * from VRF and make it "down".
2086 ospf_vrf_unlink(ospf
, vrf
);
2087 ospf
->oi_running
= 0;
2088 if (IS_DEBUG_OSPF_EVENT
)
2089 zlog_debug("%s: ospf old_vrf_id %d unlinked",
2090 __PRETTY_FUNCTION__
, old_vrf_id
);
2093 /* Note: This is a callback, the VRF will be deleted by the caller. */
2097 void ospf_vrf_init(void)
2099 vrf_init(ospf_vrf_new
, ospf_vrf_enable
,
2100 ospf_vrf_disable
, ospf_vrf_delete
);
2103 void ospf_vrf_terminate(void)
2108 const char *ospf_vrf_id_to_name(vrf_id_t vrf_id
)
2110 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2112 return vrf
? vrf
->name
: "NIL";