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
;
70 extern struct zebra_privs_t ospfd_privs
;
73 static void ospf_remove_vls_through_area(struct ospf
*, struct ospf_area
*);
74 static void ospf_network_free(struct ospf
*, struct ospf_network
*);
75 static void ospf_area_free(struct ospf_area
*);
76 static void ospf_network_run(struct prefix
*, struct ospf_area
*);
77 static void ospf_network_run_interface(struct ospf
*, struct interface
*,
78 struct prefix
*, struct ospf_area
*);
79 static void ospf_network_run_subnet(struct ospf
*, struct connected
*,
80 struct prefix
*, struct ospf_area
*);
81 static int ospf_network_match_iface(const struct connected
*,
82 const struct prefix
*);
83 static void ospf_finish_final(struct ospf
*);
85 #define OSPF_EXTERNAL_LSA_ORIGINATE_DELAY 1
87 void ospf_router_id_update(struct ospf
*ospf
)
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_LIST_ELEMENTS_RO(vrf_iflist(ospf
->vrf_id
), node
, 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 route_node
*rn
;
585 struct ospf_nbr_nbma
*nbr_nbma
;
586 struct ospf_lsa
*lsa
;
587 struct interface
*ifp
;
588 struct ospf_interface
*oi
;
589 struct ospf_area
*area
;
590 struct ospf_vl_data
*vl_data
;
591 struct listnode
*node
, *nnode
;
593 u_short instance
= 0;
594 struct vrf
*vrf
= NULL
;
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(ospf
->vlinks
);
625 /* Remove any ospf interface config params */
626 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(ospf
->vrf_id
), node
, 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
))
638 /* De-Register VRF */
639 ospf_zebra_vrf_deregister(ospf
);
641 /* Clear static neighbors */
642 for (rn
= route_top(ospf
->nbr_nbma
); rn
; rn
= route_next(rn
))
643 if ((nbr_nbma
= rn
->info
)) {
644 OSPF_POLL_TIMER_OFF(nbr_nbma
->t_poll
);
647 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
648 nbr_nbma
->nbr
= NULL
;
652 listnode_delete(nbr_nbma
->oi
->nbr_nbma
,
657 XFREE(MTYPE_OSPF_NEIGHBOR_STATIC
, nbr_nbma
);
660 route_table_finish(ospf
->nbr_nbma
);
662 /* Clear networks and Areas. */
663 for (rn
= route_top(ospf
->networks
); rn
; rn
= route_next(rn
)) {
664 struct ospf_network
*network
;
666 if ((network
= rn
->info
) != NULL
) {
667 ospf_network_free(ospf
, network
);
669 route_unlock_node(rn
);
672 route_table_finish(ospf
->networks
);
674 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
675 listnode_delete(ospf
->areas
, area
);
676 ospf_area_free(area
);
679 /* Cancel all timers. */
680 OSPF_TIMER_OFF(ospf
->t_external_lsa
);
681 OSPF_TIMER_OFF(ospf
->t_spf_calc
);
682 OSPF_TIMER_OFF(ospf
->t_ase_calc
);
683 OSPF_TIMER_OFF(ospf
->t_maxage
);
684 OSPF_TIMER_OFF(ospf
->t_maxage_walker
);
685 OSPF_TIMER_OFF(ospf
->t_abr_task
);
686 OSPF_TIMER_OFF(ospf
->t_asbr_check
);
687 OSPF_TIMER_OFF(ospf
->t_distribute_update
);
688 OSPF_TIMER_OFF(ospf
->t_lsa_refresher
);
689 OSPF_TIMER_OFF(ospf
->t_read
);
690 OSPF_TIMER_OFF(ospf
->t_write
);
691 OSPF_TIMER_OFF(ospf
->t_opaque_lsa_self
);
694 stream_free(ospf
->ibuf
);
696 LSDB_LOOP(OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
697 ospf_discard_from_db(ospf
, ospf
->lsdb
, lsa
);
698 LSDB_LOOP(EXTERNAL_LSDB(ospf
), rn
, lsa
)
699 ospf_discard_from_db(ospf
, ospf
->lsdb
, lsa
);
701 ospf_lsdb_delete_all(ospf
->lsdb
);
702 ospf_lsdb_free(ospf
->lsdb
);
704 for (rn
= route_top(ospf
->maxage_lsa
); rn
; rn
= route_next(rn
)) {
705 struct ospf_lsa
*lsa
;
707 if ((lsa
= rn
->info
) != NULL
) {
708 ospf_lsa_unlock(&lsa
);
711 route_unlock_node(rn
);
713 route_table_finish(ospf
->maxage_lsa
);
716 ospf_route_table_free(ospf
->old_table
);
717 if (ospf
->new_table
) {
718 ospf_route_delete(ospf
, ospf
->new_table
);
719 ospf_route_table_free(ospf
->new_table
);
722 ospf_rtrs_free(ospf
->old_rtrs
);
724 ospf_rtrs_free(ospf
->new_rtrs
);
725 if (ospf
->new_external_route
) {
726 ospf_route_delete(ospf
, ospf
->new_external_route
);
727 ospf_route_table_free(ospf
->new_external_route
);
729 if (ospf
->old_external_route
) {
730 ospf_route_delete(ospf
, ospf
->old_external_route
);
731 ospf_route_table_free(ospf
->old_external_route
);
733 if (ospf
->external_lsas
) {
734 ospf_ase_external_lsas_finish(ospf
->external_lsas
);
737 list_delete(ospf
->areas
);
738 list_delete(ospf
->oi_write_q
);
739 list_delete(ospf
->oiflist
);
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
;
832 LSDB_LOOP(ROUTER_LSDB(area
), rn
, lsa
)
833 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
834 LSDB_LOOP(NETWORK_LSDB(area
), rn
, lsa
)
835 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
836 LSDB_LOOP(SUMMARY_LSDB(area
), rn
, lsa
)
837 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
838 LSDB_LOOP(ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
839 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
841 LSDB_LOOP(NSSA_LSDB(area
), rn
, lsa
)
842 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
843 LSDB_LOOP(OPAQUE_AREA_LSDB(area
), rn
, lsa
)
844 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
845 LSDB_LOOP(OPAQUE_LINK_LSDB(area
), rn
, lsa
)
846 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
848 ospf_opaque_type10_lsa_term(area
);
849 ospf_lsdb_delete_all(area
->lsdb
);
850 ospf_lsdb_free(area
->lsdb
);
852 ospf_lsa_unlock(&area
->router_lsa_self
);
854 route_table_finish(area
->ranges
);
855 list_delete(area
->oiflist
);
857 if (EXPORT_NAME(area
))
858 free(EXPORT_NAME(area
));
860 if (IMPORT_NAME(area
))
861 free(IMPORT_NAME(area
));
864 OSPF_TIMER_OFF(area
->t_stub_router
);
865 OSPF_TIMER_OFF(area
->t_opaque_lsa_self
);
867 if (OSPF_IS_AREA_BACKBONE(area
))
868 area
->ospf
->backbone
= NULL
;
870 XFREE(MTYPE_OSPF_AREA
, area
);
873 void ospf_area_check_free(struct ospf
*ospf
, struct in_addr area_id
)
875 struct ospf_area
*area
;
877 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
878 if (area
&& listcount(area
->oiflist
) == 0 && area
->ranges
->top
== NULL
879 && area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
880 && area
->external_routing
== OSPF_AREA_DEFAULT
881 && area
->no_summary
== 0 && area
->default_cost
== 1
882 && EXPORT_NAME(area
) == NULL
&& IMPORT_NAME(area
) == NULL
883 && area
->auth_type
== OSPF_AUTH_NULL
) {
884 listnode_delete(ospf
->areas
, area
);
885 ospf_area_free(area
);
889 struct ospf_area
*ospf_area_get(struct ospf
*ospf
, struct in_addr area_id
)
891 struct ospf_area
*area
;
893 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
895 area
= ospf_area_new(ospf
, area_id
);
896 listnode_add_sort(ospf
->areas
, area
);
897 ospf_check_abr_status(ospf
);
898 if (ospf
->stub_router_admin_set
899 == OSPF_STUB_ROUTER_ADMINISTRATIVE_SET
) {
900 SET_FLAG(area
->stub_router_state
,
901 OSPF_AREA_ADMIN_STUB_ROUTED
);
908 struct ospf_area
*ospf_area_lookup_by_area_id(struct ospf
*ospf
,
909 struct in_addr area_id
)
911 struct ospf_area
*area
;
912 struct listnode
*node
;
914 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, node
, area
))
915 if (IPV4_ADDR_SAME(&area
->area_id
, &area_id
))
921 void ospf_area_add_if(struct ospf_area
*area
, struct ospf_interface
*oi
)
923 listnode_add(area
->oiflist
, oi
);
926 void ospf_area_del_if(struct ospf_area
*area
, struct ospf_interface
*oi
)
928 listnode_delete(area
->oiflist
, oi
);
932 static void add_ospf_interface(struct connected
*co
, struct ospf_area
*area
)
934 struct ospf_interface
*oi
;
936 oi
= ospf_if_new(area
->ospf
, co
->ifp
, co
->address
);
941 oi
->params
= ospf_lookup_if_params(co
->ifp
, oi
->address
->u
.prefix4
);
942 oi
->output_cost
= ospf_if_get_output_cost(oi
);
944 /* Relate ospf interface to ospf instance. */
945 oi
->ospf
= area
->ospf
;
947 /* update network type as interface flag */
948 /* If network type is specified previously,
949 skip network type setting. */
950 oi
->type
= IF_DEF_PARAMS(co
->ifp
)->type
;
952 /* Add pseudo neighbor. */
953 ospf_nbr_self_reset(oi
, oi
->ospf
->router_id
);
955 ospf_area_add_if(oi
->area
, oi
);
958 * if router_id is not configured, dont bring up
960 * ospf_router_id_update() will call ospf_if_update
961 * whenever r-id is configured instead.
963 if ((area
->ospf
->router_id
.s_addr
!= 0) && if_is_operative(co
->ifp
))
967 static void update_redistributed(struct ospf
*ospf
, int add_to_ospf
)
969 struct route_node
*rn
;
970 struct external_info
*ei
;
971 struct ospf_external
*ext
;
973 if (ospf_is_type_redistributed(ospf
, ZEBRA_ROUTE_CONNECT
, 0))
974 if ((ext
= ospf_external_lookup(ZEBRA_ROUTE_CONNECT
, 0))
975 && EXTERNAL_INFO(ext
)) {
976 for (rn
= route_top(EXTERNAL_INFO(ext
)); rn
;
977 rn
= route_next(rn
)) {
978 if ((ei
= rn
->info
) != NULL
) {
980 if (ospf_external_info_find_lsa(
982 if (!ospf_distribute_check_connected(
984 ospf_external_lsa_flush(
988 ei
->ifindex
/*, ei->nexthop */);
990 if (!ospf_external_info_find_lsa(
992 if (ospf_distribute_check_connected(
994 ospf_external_lsa_originate(
1003 /* Config network statement related functions. */
1004 static struct ospf_network
*ospf_network_new(struct in_addr area_id
)
1006 struct ospf_network
*new;
1007 new = XCALLOC(MTYPE_OSPF_NETWORK
, sizeof(struct ospf_network
));
1009 new->area_id
= area_id
;
1010 new->area_id_fmt
= OSPF_AREA_ID_FMT_DOTTEDQUAD
;
1015 static void ospf_network_free(struct ospf
*ospf
, struct ospf_network
*network
)
1017 ospf_area_check_free(ospf
, network
->area_id
);
1018 ospf_schedule_abr_task(ospf
);
1019 XFREE(MTYPE_OSPF_NETWORK
, network
);
1022 int ospf_network_set(struct ospf
*ospf
, struct prefix_ipv4
*p
,
1023 struct in_addr area_id
, int df
)
1025 struct ospf_network
*network
;
1026 struct ospf_area
*area
;
1027 struct route_node
*rn
;
1029 rn
= route_node_get(ospf
->networks
, (struct prefix
*)p
);
1031 /* There is already same network statement. */
1032 route_unlock_node(rn
);
1036 rn
->info
= network
= ospf_network_new(area_id
);
1037 network
->area_id_fmt
= df
;
1038 area
= ospf_area_get(ospf
, area_id
);
1039 ospf_area_display_format_set(ospf
, area
, df
);
1041 /* Run network config now. */
1042 ospf_network_run((struct prefix
*)p
, area
);
1044 /* Update connected redistribute. */
1045 update_redistributed(ospf
, 1); /* interfaces possibly added */
1047 ospf_area_check_free(ospf
, area_id
);
1052 int ospf_network_unset(struct ospf
*ospf
, struct prefix_ipv4
*p
,
1053 struct in_addr area_id
)
1055 struct route_node
*rn
;
1056 struct ospf_network
*network
;
1057 struct listnode
*node
, *nnode
;
1058 struct ospf_interface
*oi
;
1060 rn
= route_node_lookup(ospf
->networks
, (struct prefix
*)p
);
1065 route_unlock_node(rn
);
1066 if (!IPV4_ADDR_SAME(&area_id
, &network
->area_id
))
1069 ospf_network_free(ospf
, rn
->info
);
1071 route_unlock_node(rn
); /* initial reference */
1073 /* Find interfaces that are not configured already. */
1074 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
)) {
1076 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
1079 ospf_network_run_subnet(ospf
, oi
->connected
, NULL
, NULL
);
1082 /* Update connected redistribute. */
1083 update_redistributed(ospf
, 0); /* interfaces possibly removed */
1084 ospf_area_check_free(ospf
, area_id
);
1089 /* Ensure there's an OSPF instance, as "ip ospf area" enabled OSPF means
1090 * there might not be any 'router ospf' config.
1092 * Otherwise, doesn't do anything different to ospf_if_update for now
1094 void ospf_interface_area_set(struct ospf
*ospf
, struct interface
*ifp
)
1099 ospf_if_update(ospf
, ifp
);
1100 /* if_update does a update_redistributed */
1105 void ospf_interface_area_unset(struct ospf
*ospf
, struct interface
*ifp
)
1107 struct route_node
*rn_oi
;
1110 return; /* Ospf not ready yet */
1112 /* Find interfaces that may need to be removed. */
1113 for (rn_oi
= route_top(IF_OIFS(ifp
)); rn_oi
;
1114 rn_oi
= route_next(rn_oi
)) {
1115 struct ospf_interface
*oi
= NULL
;
1117 if ((oi
= rn_oi
->info
) == NULL
)
1120 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
1123 ospf_network_run_subnet(ospf
, oi
->connected
, NULL
, NULL
);
1126 /* Update connected redistribute. */
1127 update_redistributed(ospf
, 0); /* interfaces possibly removed */
1130 /* Check whether interface matches given network
1131 * returns: 1, true. 0, false
1133 static int ospf_network_match_iface(const struct connected
*co
,
1134 const struct prefix
*net
)
1136 /* new approach: more elegant and conceptually clean */
1137 return prefix_match_network_statement(net
, CONNECTED_PREFIX(co
));
1140 static void ospf_update_interface_area(struct connected
*co
,
1141 struct ospf_area
*area
)
1143 struct ospf_interface
*oi
= ospf_if_table_lookup(co
->ifp
, co
->address
);
1145 /* nothing to be done case */
1146 if (oi
&& oi
->area
== area
) {
1153 add_ospf_interface(co
, area
);
1156 /* Run OSPF for the given subnet, taking into account the following
1157 * possible sources of area configuration, in the given order of preference:
1159 * - Whether there is interface+address specific area configuration
1160 * - Whether there is a default area for the interface
1161 * - Whether there is an area given as a parameter.
1162 * - If no specific network prefix/area is supplied, whether there's
1163 * a matching network configured.
1165 static void ospf_network_run_subnet(struct ospf
*ospf
, struct connected
*co
,
1167 struct ospf_area
*given_area
)
1169 struct ospf_interface
*oi
;
1170 struct ospf_if_params
*params
;
1171 struct ospf_area
*area
= NULL
;
1172 struct route_node
*rn
;
1175 if (CHECK_FLAG(co
->flags
, ZEBRA_IFA_SECONDARY
))
1178 if (co
->address
->family
!= AF_INET
)
1181 /* Try determine the appropriate area for this interface + address
1182 * Start by checking interface config
1184 params
= ospf_lookup_if_params(co
->ifp
, co
->address
->u
.prefix4
);
1185 if (params
&& OSPF_IF_PARAM_CONFIGURED(params
, if_area
))
1186 area
= ospf_area_get(ospf
, params
->if_area
);
1188 params
= IF_DEF_PARAMS(co
->ifp
);
1189 if (OSPF_IF_PARAM_CONFIGURED(params
, if_area
))
1190 area
= ospf_area_get(ospf
, params
->if_area
);
1193 /* If we've found an interface and/or addr specific area, then we're
1197 ospf_update_interface_area(co
, area
);
1201 /* Otherwise, only remaining possibility is a matching network statement
1204 assert(given_area
!= NULL
);
1206 /* Which either was supplied as a parameter.. (e.g. cause a new
1207 * network/area was just added)..
1209 if (p
->family
== co
->address
->family
1210 && ospf_network_match_iface(co
, p
))
1211 ospf_update_interface_area(co
, given_area
);
1216 /* Else we have to search the existing network/area config to see
1219 for (rn
= route_top(ospf
->networks
); rn
; rn
= route_next(rn
))
1220 if (rn
->info
!= NULL
&& ospf_network_match_iface(co
, &rn
->p
)) {
1221 struct ospf_network
*network
=
1222 (struct ospf_network
*)rn
->info
;
1223 area
= ospf_area_get(ospf
, network
->area_id
);
1224 ospf_update_interface_area(co
, area
);
1228 /* If the subnet isn't in any area, deconfigure */
1229 if (!configed
&& (oi
= ospf_if_table_lookup(co
->ifp
, co
->address
)))
1233 static void ospf_network_run_interface(struct ospf
*ospf
, struct interface
*ifp
,
1235 struct ospf_area
*given_area
)
1237 struct listnode
*cnode
;
1238 struct connected
*co
;
1240 if (memcmp(ifp
->name
, "VLINK", 5) == 0)
1243 /* Network prefix without area is nonsensical */
1245 assert(given_area
!= NULL
);
1247 /* if interface prefix is match specified prefix,
1248 then create socket and join multicast group. */
1249 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, co
))
1250 ospf_network_run_subnet(ospf
, co
, p
, given_area
);
1253 static void ospf_network_run(struct prefix
*p
, struct ospf_area
*area
)
1255 struct interface
*ifp
;
1256 struct listnode
*node
;
1258 /* Schedule Router ID Update. */
1259 if (area
->ospf
->router_id
.s_addr
== 0)
1260 ospf_router_id_update(area
->ospf
);
1262 /* Get target interface. */
1263 for (ALL_LIST_ELEMENTS_RO(om
->iflist
, node
, ifp
))
1264 ospf_network_run_interface(area
->ospf
, ifp
, p
, area
);
1267 void ospf_ls_upd_queue_empty(struct ospf_interface
*oi
)
1269 struct route_node
*rn
;
1270 struct listnode
*node
, *nnode
;
1272 struct ospf_lsa
*lsa
;
1274 /* empty ls update queue */
1275 for (rn
= route_top(oi
->ls_upd_queue
); rn
; rn
= route_next(rn
))
1276 if ((lst
= (struct list
*)rn
->info
)) {
1277 for (ALL_LIST_ELEMENTS(lst
, node
, nnode
, lsa
))
1278 ospf_lsa_unlock(&lsa
); /* oi->ls_upd_queue */
1283 /* remove update event */
1284 if (oi
->t_ls_upd_event
) {
1285 thread_cancel(oi
->t_ls_upd_event
);
1286 oi
->t_ls_upd_event
= NULL
;
1290 void ospf_if_update(struct ospf
*ospf
, struct interface
*ifp
)
1296 if (IS_DEBUG_OSPF_EVENT
)
1297 zlog_debug("%s: interface %s ifp->vrf_id %u ospf vrf %s vrf_id %u router_id %s",
1298 __PRETTY_FUNCTION__
, ifp
->name
, ifp
->vrf_id
,
1299 ospf_vrf_id_to_name(ospf
->vrf_id
), ospf
->vrf_id
,
1300 inet_ntoa(ospf
->router_id
));
1302 /* OSPF must be ready. */
1303 if (!ospf_is_ready(ospf
))
1306 ospf_network_run_interface(ospf
, ifp
, NULL
, NULL
);
1308 /* Update connected redistribute. */
1309 update_redistributed(ospf
, 1);
1312 void ospf_remove_vls_through_area(struct ospf
*ospf
, struct ospf_area
*area
)
1314 struct listnode
*node
, *nnode
;
1315 struct ospf_vl_data
*vl_data
;
1317 for (ALL_LIST_ELEMENTS(ospf
->vlinks
, node
, nnode
, vl_data
))
1318 if (IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
))
1319 ospf_vl_delete(ospf
, vl_data
);
1323 static const struct message ospf_area_type_msg
[] = {
1324 {OSPF_AREA_DEFAULT
, "Default"},
1325 {OSPF_AREA_STUB
, "Stub"},
1326 {OSPF_AREA_NSSA
, "NSSA"},
1329 static void ospf_area_type_set(struct ospf_area
*area
, int type
)
1331 struct listnode
*node
;
1332 struct ospf_interface
*oi
;
1334 if (area
->external_routing
== type
) {
1335 if (IS_DEBUG_OSPF_EVENT
)
1336 zlog_debug("Area[%s]: Types are the same, ignored.",
1337 inet_ntoa(area
->area_id
));
1341 area
->external_routing
= type
;
1343 if (IS_DEBUG_OSPF_EVENT
)
1344 zlog_debug("Area[%s]: Configured as %s",
1345 inet_ntoa(area
->area_id
),
1346 lookup_msg(ospf_area_type_msg
, type
, NULL
));
1348 switch (area
->external_routing
) {
1349 case OSPF_AREA_DEFAULT
:
1350 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
))
1351 if (oi
->nbr_self
!= NULL
) {
1352 UNSET_FLAG(oi
->nbr_self
->options
,
1354 SET_FLAG(oi
->nbr_self
->options
, OSPF_OPTION_E
);
1357 case OSPF_AREA_STUB
:
1358 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
))
1359 if (oi
->nbr_self
!= NULL
) {
1360 if (IS_DEBUG_OSPF_EVENT
)
1362 "setting options on %s accordingly",
1364 UNSET_FLAG(oi
->nbr_self
->options
,
1366 UNSET_FLAG(oi
->nbr_self
->options
,
1368 if (IS_DEBUG_OSPF_EVENT
)
1369 zlog_debug("options set on %s: %x",
1370 IF_NAME(oi
), OPTIONS(oi
));
1373 case OSPF_AREA_NSSA
:
1374 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
))
1375 if (oi
->nbr_self
!= NULL
) {
1377 "setting nssa options on %s accordingly",
1379 UNSET_FLAG(oi
->nbr_self
->options
,
1381 SET_FLAG(oi
->nbr_self
->options
, OSPF_OPTION_NP
);
1382 zlog_debug("options set on %s: %x", IF_NAME(oi
),
1390 ospf_router_lsa_update_area(area
);
1391 ospf_schedule_abr_task(area
->ospf
);
1394 int ospf_area_shortcut_set(struct ospf
*ospf
, struct ospf_area
*area
, int mode
)
1396 if (area
->shortcut_configured
== mode
)
1399 area
->shortcut_configured
= mode
;
1400 ospf_router_lsa_update_area(area
);
1401 ospf_schedule_abr_task(ospf
);
1403 ospf_area_check_free(ospf
, area
->area_id
);
1408 int ospf_area_shortcut_unset(struct ospf
*ospf
, struct ospf_area
*area
)
1410 area
->shortcut_configured
= OSPF_SHORTCUT_DEFAULT
;
1411 ospf_router_lsa_update_area(area
);
1412 ospf_area_check_free(ospf
, area
->area_id
);
1413 ospf_schedule_abr_task(ospf
);
1418 static int ospf_area_vlink_count(struct ospf
*ospf
, struct ospf_area
*area
)
1420 struct ospf_vl_data
*vl
;
1421 struct listnode
*node
;
1424 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl
))
1425 if (IPV4_ADDR_SAME(&vl
->vl_area_id
, &area
->area_id
))
1431 int ospf_area_display_format_set(struct ospf
*ospf
, struct ospf_area
*area
,
1434 area
->area_id_fmt
= df
;
1439 int ospf_area_stub_set(struct ospf
*ospf
, struct in_addr area_id
)
1441 struct ospf_area
*area
;
1443 area
= ospf_area_get(ospf
, area_id
);
1444 if (ospf_area_vlink_count(ospf
, area
))
1447 if (area
->external_routing
!= OSPF_AREA_STUB
)
1448 ospf_area_type_set(area
, OSPF_AREA_STUB
);
1453 int ospf_area_stub_unset(struct ospf
*ospf
, struct in_addr area_id
)
1455 struct ospf_area
*area
;
1457 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1461 if (area
->external_routing
== OSPF_AREA_STUB
)
1462 ospf_area_type_set(area
, OSPF_AREA_DEFAULT
);
1464 ospf_area_check_free(ospf
, area_id
);
1469 int ospf_area_no_summary_set(struct ospf
*ospf
, struct in_addr area_id
)
1471 struct ospf_area
*area
;
1473 area
= ospf_area_get(ospf
, area_id
);
1474 area
->no_summary
= 1;
1479 int ospf_area_no_summary_unset(struct ospf
*ospf
, struct in_addr area_id
)
1481 struct ospf_area
*area
;
1483 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1487 area
->no_summary
= 0;
1488 ospf_area_check_free(ospf
, area_id
);
1493 int ospf_area_nssa_set(struct ospf
*ospf
, struct in_addr area_id
)
1495 struct ospf_area
*area
;
1497 area
= ospf_area_get(ospf
, area_id
);
1498 if (ospf_area_vlink_count(ospf
, area
))
1501 if (area
->external_routing
!= OSPF_AREA_NSSA
) {
1502 ospf_area_type_set(area
, OSPF_AREA_NSSA
);
1506 /* set NSSA area defaults */
1507 area
->no_summary
= 0;
1508 area
->NSSATranslatorRole
= OSPF_NSSA_ROLE_CANDIDATE
;
1509 area
->NSSATranslatorState
= OSPF_NSSA_TRANSLATE_DISABLED
;
1510 area
->NSSATranslatorStabilityInterval
= OSPF_NSSA_TRANS_STABLE_DEFAULT
;
1515 int ospf_area_nssa_unset(struct ospf
*ospf
, struct in_addr area_id
)
1517 struct ospf_area
*area
;
1519 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1523 if (area
->external_routing
== OSPF_AREA_NSSA
) {
1525 ospf_area_type_set(area
, OSPF_AREA_DEFAULT
);
1528 ospf_area_check_free(ospf
, area_id
);
1533 int ospf_area_nssa_translator_role_set(struct ospf
*ospf
,
1534 struct in_addr area_id
, int role
)
1536 struct ospf_area
*area
;
1538 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1542 area
->NSSATranslatorRole
= role
;
1548 /* XXX: unused? Leave for symmetry? */
1550 ospf_area_nssa_translator_role_unset (struct ospf
*ospf
,
1551 struct in_addr area_id
)
1553 struct ospf_area
*area
;
1555 area
= ospf_area_lookup_by_area_id (ospf
, area_id
);
1559 area
->NSSATranslatorRole
= OSPF_NSSA_ROLE_CANDIDATE
;
1561 ospf_area_check_free (ospf
, area_id
);
1567 int ospf_area_export_list_set(struct ospf
*ospf
, struct ospf_area
*area
,
1568 const char *list_name
)
1570 struct access_list
*list
;
1571 list
= access_list_lookup(AFI_IP
, list_name
);
1573 EXPORT_LIST(area
) = list
;
1575 if (EXPORT_NAME(area
))
1576 free(EXPORT_NAME(area
));
1578 EXPORT_NAME(area
) = strdup(list_name
);
1579 ospf_schedule_abr_task(ospf
);
1584 int ospf_area_export_list_unset(struct ospf
*ospf
, struct ospf_area
*area
)
1587 EXPORT_LIST(area
) = 0;
1589 if (EXPORT_NAME(area
))
1590 free(EXPORT_NAME(area
));
1592 EXPORT_NAME(area
) = NULL
;
1594 ospf_area_check_free(ospf
, area
->area_id
);
1596 ospf_schedule_abr_task(ospf
);
1601 int ospf_area_import_list_set(struct ospf
*ospf
, struct ospf_area
*area
,
1604 struct access_list
*list
;
1605 list
= access_list_lookup(AFI_IP
, name
);
1607 IMPORT_LIST(area
) = list
;
1609 if (IMPORT_NAME(area
))
1610 free(IMPORT_NAME(area
));
1612 IMPORT_NAME(area
) = strdup(name
);
1613 ospf_schedule_abr_task(ospf
);
1618 int ospf_area_import_list_unset(struct ospf
*ospf
, struct ospf_area
*area
)
1620 IMPORT_LIST(area
) = 0;
1622 if (IMPORT_NAME(area
))
1623 free(IMPORT_NAME(area
));
1625 IMPORT_NAME(area
) = NULL
;
1626 ospf_area_check_free(ospf
, area
->area_id
);
1628 ospf_schedule_abr_task(ospf
);
1633 int ospf_timers_refresh_set(struct ospf
*ospf
, int interval
)
1637 if (ospf
->lsa_refresh_interval
== interval
)
1640 time_left
= ospf
->lsa_refresh_interval
1641 - (monotime(NULL
) - ospf
->lsa_refresher_started
);
1643 if (time_left
> interval
) {
1644 OSPF_TIMER_OFF(ospf
->t_lsa_refresher
);
1645 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
1646 interval
, &ospf
->t_lsa_refresher
);
1648 ospf
->lsa_refresh_interval
= interval
;
1653 int ospf_timers_refresh_unset(struct ospf
*ospf
)
1657 time_left
= ospf
->lsa_refresh_interval
1658 - (monotime(NULL
) - ospf
->lsa_refresher_started
);
1660 if (time_left
> OSPF_LSA_REFRESH_INTERVAL_DEFAULT
) {
1661 OSPF_TIMER_OFF(ospf
->t_lsa_refresher
);
1662 ospf
->t_lsa_refresher
= NULL
;
1663 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
1664 OSPF_LSA_REFRESH_INTERVAL_DEFAULT
,
1665 &ospf
->t_lsa_refresher
);
1668 ospf
->lsa_refresh_interval
= OSPF_LSA_REFRESH_INTERVAL_DEFAULT
;
1674 static struct ospf_nbr_nbma
*ospf_nbr_nbma_new(void)
1676 struct ospf_nbr_nbma
*nbr_nbma
;
1678 nbr_nbma
= XCALLOC(MTYPE_OSPF_NEIGHBOR_STATIC
,
1679 sizeof(struct ospf_nbr_nbma
));
1681 nbr_nbma
->priority
= OSPF_NEIGHBOR_PRIORITY_DEFAULT
;
1682 nbr_nbma
->v_poll
= OSPF_POLL_INTERVAL_DEFAULT
;
1687 static void ospf_nbr_nbma_free(struct ospf_nbr_nbma
*nbr_nbma
)
1689 XFREE(MTYPE_OSPF_NEIGHBOR_STATIC
, nbr_nbma
);
1692 static void ospf_nbr_nbma_delete(struct ospf
*ospf
,
1693 struct ospf_nbr_nbma
*nbr_nbma
)
1695 struct route_node
*rn
;
1696 struct prefix_ipv4 p
;
1699 p
.prefix
= nbr_nbma
->addr
;
1700 p
.prefixlen
= IPV4_MAX_BITLEN
;
1702 rn
= route_node_lookup(ospf
->nbr_nbma
, (struct prefix
*)&p
);
1704 ospf_nbr_nbma_free(rn
->info
);
1706 route_unlock_node(rn
);
1707 route_unlock_node(rn
);
1711 static void ospf_nbr_nbma_down(struct ospf_nbr_nbma
*nbr_nbma
)
1713 OSPF_TIMER_OFF(nbr_nbma
->t_poll
);
1715 if (nbr_nbma
->nbr
) {
1716 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
1717 OSPF_NSM_EVENT_EXECUTE(nbr_nbma
->nbr
, NSM_KillNbr
);
1721 listnode_delete(nbr_nbma
->oi
->nbr_nbma
, nbr_nbma
);
1724 static void ospf_nbr_nbma_add(struct ospf_nbr_nbma
*nbr_nbma
,
1725 struct ospf_interface
*oi
)
1727 struct ospf_neighbor
*nbr
;
1728 struct route_node
*rn
;
1731 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
1734 if (nbr_nbma
->nbr
!= NULL
)
1737 if (IPV4_ADDR_SAME(&oi
->nbr_self
->address
.u
.prefix4
, &nbr_nbma
->addr
))
1741 listnode_add(oi
->nbr_nbma
, nbr_nbma
);
1743 /* Get neighbor information from table. */
1745 p
.prefixlen
= IPV4_MAX_BITLEN
;
1746 p
.u
.prefix4
= nbr_nbma
->addr
;
1748 rn
= route_node_get(oi
->nbrs
, (struct prefix
*)&p
);
1751 nbr
->nbr_nbma
= nbr_nbma
;
1752 nbr_nbma
->nbr
= nbr
;
1754 route_unlock_node(rn
);
1756 nbr
= rn
->info
= ospf_nbr_new(oi
);
1757 nbr
->state
= NSM_Down
;
1758 nbr
->src
= nbr_nbma
->addr
;
1759 nbr
->nbr_nbma
= nbr_nbma
;
1760 nbr
->priority
= nbr_nbma
->priority
;
1763 nbr_nbma
->nbr
= nbr
;
1765 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_Start
);
1769 void ospf_nbr_nbma_if_update(struct ospf
*ospf
, struct ospf_interface
*oi
)
1771 struct ospf_nbr_nbma
*nbr_nbma
;
1772 struct route_node
*rn
;
1773 struct prefix_ipv4 p
;
1775 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
1778 for (rn
= route_top(ospf
->nbr_nbma
); rn
; rn
= route_next(rn
))
1779 if ((nbr_nbma
= rn
->info
))
1780 if (nbr_nbma
->oi
== NULL
&& nbr_nbma
->nbr
== NULL
) {
1782 p
.prefix
= nbr_nbma
->addr
;
1783 p
.prefixlen
= IPV4_MAX_BITLEN
;
1785 if (prefix_match(oi
->address
,
1786 (struct prefix
*)&p
))
1787 ospf_nbr_nbma_add(nbr_nbma
, oi
);
1791 struct ospf_nbr_nbma
*ospf_nbr_nbma_lookup(struct ospf
*ospf
,
1792 struct in_addr nbr_addr
)
1794 struct route_node
*rn
;
1795 struct prefix_ipv4 p
;
1798 p
.prefix
= nbr_addr
;
1799 p
.prefixlen
= IPV4_MAX_BITLEN
;
1801 rn
= route_node_lookup(ospf
->nbr_nbma
, (struct prefix
*)&p
);
1803 route_unlock_node(rn
);
1809 struct ospf_nbr_nbma
*ospf_nbr_nbma_lookup_next(struct ospf
*ospf
,
1810 struct in_addr
*addr
, int first
)
1813 struct ospf_nbr_nbma
*nbr_nbma
;
1814 struct listnode
*node
;
1821 for (ALL_LIST_ELEMENTS_RO (ospf
->nbr_nbma
, node
, nbr_nbma
))
1825 *addr
= nbr_nbma
->addr
;
1828 else if (ntohl (nbr_nbma
->addr
.s_addr
) > ntohl (addr
->s_addr
))
1830 *addr
= nbr_nbma
->addr
;
1838 int ospf_nbr_nbma_set(struct ospf
*ospf
, struct in_addr nbr_addr
)
1840 struct ospf_nbr_nbma
*nbr_nbma
;
1841 struct ospf_interface
*oi
;
1842 struct prefix_ipv4 p
;
1843 struct route_node
*rn
;
1844 struct listnode
*node
;
1846 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1850 nbr_nbma
= ospf_nbr_nbma_new();
1851 nbr_nbma
->addr
= nbr_addr
;
1854 p
.prefix
= nbr_addr
;
1855 p
.prefixlen
= IPV4_MAX_BITLEN
;
1857 rn
= route_node_get(ospf
->nbr_nbma
, (struct prefix
*)&p
);
1859 route_unlock_node(rn
);
1860 rn
->info
= nbr_nbma
;
1862 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
1863 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1864 if (prefix_match(oi
->address
, (struct prefix
*)&p
)) {
1865 ospf_nbr_nbma_add(nbr_nbma
, oi
);
1873 int ospf_nbr_nbma_unset(struct ospf
*ospf
, struct in_addr nbr_addr
)
1875 struct ospf_nbr_nbma
*nbr_nbma
;
1877 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1878 if (nbr_nbma
== NULL
)
1881 ospf_nbr_nbma_down(nbr_nbma
);
1882 ospf_nbr_nbma_delete(ospf
, nbr_nbma
);
1887 int ospf_nbr_nbma_priority_set(struct ospf
*ospf
, struct in_addr nbr_addr
,
1890 struct ospf_nbr_nbma
*nbr_nbma
;
1892 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1893 if (nbr_nbma
== NULL
)
1896 if (nbr_nbma
->priority
!= priority
)
1897 nbr_nbma
->priority
= priority
;
1902 int ospf_nbr_nbma_priority_unset(struct ospf
*ospf
, struct in_addr nbr_addr
)
1904 struct ospf_nbr_nbma
*nbr_nbma
;
1906 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1907 if (nbr_nbma
== NULL
)
1910 if (nbr_nbma
!= OSPF_NEIGHBOR_PRIORITY_DEFAULT
)
1911 nbr_nbma
->priority
= OSPF_NEIGHBOR_PRIORITY_DEFAULT
;
1916 int ospf_nbr_nbma_poll_interval_set(struct ospf
*ospf
, struct in_addr nbr_addr
,
1917 unsigned int interval
)
1919 struct ospf_nbr_nbma
*nbr_nbma
;
1921 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1922 if (nbr_nbma
== NULL
)
1925 if (nbr_nbma
->v_poll
!= interval
) {
1926 nbr_nbma
->v_poll
= interval
;
1927 if (nbr_nbma
->oi
&& ospf_if_is_up(nbr_nbma
->oi
)) {
1928 OSPF_TIMER_OFF(nbr_nbma
->t_poll
);
1929 OSPF_POLL_TIMER_ON(nbr_nbma
->t_poll
, ospf_poll_timer
,
1937 int ospf_nbr_nbma_poll_interval_unset(struct ospf
*ospf
, struct in_addr addr
)
1939 struct ospf_nbr_nbma
*nbr_nbma
;
1941 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, addr
);
1942 if (nbr_nbma
== NULL
)
1945 if (nbr_nbma
->v_poll
!= OSPF_POLL_INTERVAL_DEFAULT
)
1946 nbr_nbma
->v_poll
= OSPF_POLL_INTERVAL_DEFAULT
;
1951 void ospf_master_init(struct thread_master
*master
)
1953 memset(&ospf_master
, 0, sizeof(struct ospf_master
));
1956 om
->ospf
= list_new();
1957 om
->master
= master
;
1960 /* Link OSPF instance to VRF. */
1961 void ospf_vrf_link(struct ospf
*ospf
, struct vrf
*vrf
)
1963 ospf
->vrf_id
= vrf
->vrf_id
;
1964 if (vrf
->info
!= (void *)ospf
)
1965 vrf
->info
= (void *)ospf
;
1968 /* Unlink OSPF instance from VRF. */
1969 void ospf_vrf_unlink(struct ospf
*ospf
, struct vrf
*vrf
)
1971 if (vrf
->info
== (void *)ospf
)
1973 ospf
->vrf_id
= VRF_UNKNOWN
;
1976 /* This is hook function for vrf create called as part of vrf_init */
1977 static int ospf_vrf_new(struct vrf
*vrf
)
1979 if (IS_DEBUG_OSPF_EVENT
)
1980 zlog_debug("%s: VRF Created: %s(%d)", __PRETTY_FUNCTION__
,
1981 vrf
->name
, vrf
->vrf_id
);
1986 /* This is hook function for vrf delete call as part of vrf_init */
1987 static int ospf_vrf_delete(struct vrf
*vrf
)
1989 if (IS_DEBUG_OSPF_EVENT
)
1990 zlog_debug("%s: VRF Deletion: %s(%d)", __PRETTY_FUNCTION__
,
1991 vrf
->name
, vrf
->vrf_id
);
1996 /* Enable OSPF VRF instance */
1997 static int ospf_vrf_enable(struct vrf
*vrf
)
1999 struct ospf
*ospf
= NULL
;
2000 vrf_id_t old_vrf_id
= VRF_DEFAULT
;
2002 if (IS_DEBUG_OSPF_EVENT
)
2003 zlog_debug("%s: VRF %s id %d enabled",
2004 __PRETTY_FUNCTION__
, vrf
->name
, vrf
->vrf_id
);
2006 ospf
= ospf_lookup_by_name(vrf
->name
);
2008 old_vrf_id
= ospf
->vrf_id
;
2009 /* We have instance configured, link to VRF and make it "up". */
2010 ospf_vrf_link(ospf
, vrf
);
2011 if (IS_DEBUG_OSPF_EVENT
)
2012 zlog_debug("%s: ospf linked to vrf %s vrf_id %d (old id %d)",
2013 __PRETTY_FUNCTION__
, vrf
->name
, ospf
->vrf_id
,
2016 if (old_vrf_id
!= ospf
->vrf_id
) {
2017 if (ospfd_privs
.change(ZPRIVS_RAISE
)) {
2018 zlog_err("ospf_sock_init: could not raise privs, %s",
2019 safe_strerror(errno
));
2021 if (ospf_bind_vrfdevice(ospf
, ospf
->fd
) < 0)
2023 if (ospfd_privs
.change(ZPRIVS_LOWER
)) {
2024 zlog_err("ospf_sock_init: could not lower privs, %s",
2025 safe_strerror(errno
));
2028 ospf
->oi_running
= 1;
2029 ospf_router_id_update(ospf
);
2036 /* Disable OSPF VRF instance */
2037 static int ospf_vrf_disable(struct vrf
*vrf
)
2039 struct ospf
*ospf
= NULL
;
2040 vrf_id_t old_vrf_id
= VRF_UNKNOWN
;
2042 if (vrf
->vrf_id
== VRF_DEFAULT
)
2045 if (IS_DEBUG_OSPF_EVENT
)
2046 zlog_debug("%s: VRF %s id %d disabled.",
2047 __PRETTY_FUNCTION__
, vrf
->name
, vrf
->vrf_id
);
2049 ospf
= ospf_lookup_by_name(vrf
->name
);
2051 old_vrf_id
= ospf
->vrf_id
;
2053 /* We have instance configured, unlink
2054 * from VRF and make it "down".
2056 ospf_vrf_unlink(ospf
, vrf
);
2057 ospf
->oi_running
= 0;
2058 if (IS_DEBUG_OSPF_EVENT
)
2059 zlog_debug("%s: ospf old_vrf_id %d unlinked",
2060 __PRETTY_FUNCTION__
, old_vrf_id
);
2063 /* Note: This is a callback, the VRF will be deleted by the caller. */
2067 void ospf_vrf_init(void)
2069 vrf_init(ospf_vrf_new
, ospf_vrf_enable
,
2070 ospf_vrf_disable
, ospf_vrf_delete
);
2073 void ospf_vrf_terminate(void)
2078 const char *ospf_vrf_id_to_name(vrf_id_t vrf_id
)
2080 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2082 return vrf
? vrf
->name
: "NIL";