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 () */
40 #include "ospfd/ospfd.h"
41 #include "ospfd/ospf_network.h"
42 #include "ospfd/ospf_interface.h"
43 #include "ospfd/ospf_ism.h"
44 #include "ospfd/ospf_asbr.h"
45 #include "ospfd/ospf_lsa.h"
46 #include "ospfd/ospf_lsdb.h"
47 #include "ospfd/ospf_neighbor.h"
48 #include "ospfd/ospf_nsm.h"
49 #include "ospfd/ospf_spf.h"
50 #include "ospfd/ospf_packet.h"
51 #include "ospfd/ospf_dump.h"
52 #include "ospfd/ospf_zebra.h"
53 #include "ospfd/ospf_abr.h"
54 #include "ospfd/ospf_flood.h"
55 #include "ospfd/ospf_route.h"
56 #include "ospfd/ospf_ase.h"
59 DEFINE_QOBJ_TYPE(ospf
)
61 /* OSPF process wide configuration. */
62 static struct ospf_master ospf_master
;
64 /* OSPF process wide configuration pointer to export. */
65 struct ospf_master
*om
;
67 extern struct zclient
*zclient
;
68 extern struct in_addr router_id_zebra
;
71 static void ospf_remove_vls_through_area(struct ospf
*, struct ospf_area
*);
72 static void ospf_network_free(struct ospf
*, struct ospf_network
*);
73 static void ospf_area_free(struct ospf_area
*);
74 static void ospf_network_run(struct prefix
*, struct ospf_area
*);
75 static void ospf_network_run_interface(struct ospf
*, struct interface
*,
76 struct prefix
*, struct ospf_area
*);
77 static void ospf_network_run_subnet(struct ospf
*, struct connected
*,
78 struct prefix
*, struct ospf_area
*);
79 static int ospf_network_match_iface(const struct connected
*,
80 const struct prefix
*);
81 static void ospf_finish_final(struct ospf
*);
83 #define OSPF_EXTERNAL_LSA_ORIGINATE_DELAY 1
85 void ospf_router_id_update(struct ospf
*ospf
)
87 struct in_addr router_id
, router_id_old
;
88 struct ospf_interface
*oi
;
89 struct interface
*ifp
;
90 struct listnode
*node
;
93 if (!ospf
->oi_running
) {
94 if (IS_DEBUG_OSPF_EVENT
)
96 "Router ospf not configured -- Router-ID update postponed");
100 if (IS_DEBUG_OSPF_EVENT
)
101 zlog_debug("Router-ID[OLD:%s]: Update",
102 inet_ntoa(ospf
->router_id
));
104 router_id_old
= ospf
->router_id
;
106 /* Select the router ID based on these priorities:
107 1. Statically assigned router ID is always the first choice.
108 2. If there is no statically assigned router ID, then try to stick
109 with the most recent value, since changing router ID's is very
111 3. Last choice: just go with whatever the zebra daemon recommends.
113 if (ospf
->router_id_static
.s_addr
!= 0)
114 router_id
= ospf
->router_id_static
;
115 else if (ospf
->router_id
.s_addr
!= 0)
116 router_id
= ospf
->router_id
;
118 router_id
= router_id_zebra
;
121 if (!IPV4_ADDR_SAME(&router_id_old
, &router_id
)) {
123 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
124 /* Some nbrs are identified by router_id, these needs
125 * to be rebuilt. Possible optimization would be to do
126 * oi->nbr_self->router_id = router_id for
127 * !(virtual | ptop) links
129 ospf_nbr_self_reset(oi
, router_id
);
132 /* If AS-external-LSA is queued, then flush those LSAs. */
133 if (router_id_old
.s_addr
== 0 && ospf
->external_origin
) {
134 /* Originate each redistributed external route. */
135 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++)
136 if (ospf
->external_origin
& (1 << type
))
139 ospf_external_lsa_originate_timer
,
141 /* Originate Deafult. */
142 if (ospf
->external_origin
& (1 << ZEBRA_ROUTE_MAX
))
143 thread_add_event(master
,
144 ospf_default_originate_timer
,
147 ospf
->external_origin
= 0;
150 /* Flush (inline) all external LSAs based on the OSPF_LSA_SELF
153 struct route_node
*rn
;
154 struct ospf_lsa
*lsa
;
156 LSDB_LOOP(EXTERNAL_LSDB(ospf
), rn
, lsa
)
157 if (IS_LSA_SELF(lsa
))
158 ospf_lsa_flush_schedule(ospf
, lsa
);
161 ospf
->router_id
= router_id
;
162 if (IS_DEBUG_OSPF_EVENT
)
163 zlog_debug("Router-ID[NEW:%s]: Update",
164 inet_ntoa(ospf
->router_id
));
166 /* Flush (inline) all external LSAs which now match the new
168 need to adjust the OSPF_LSA_SELF flag, so the flush doesnt
170 asserts in ospf_refresher_unregister_lsa(). This step is
172 because the current quagga code does look-up for
174 based on the self router-id alone but expects OSPF_LSA_SELF
178 struct route_node
*rn
;
179 struct ospf_lsa
*lsa
;
181 LSDB_LOOP(EXTERNAL_LSDB(ospf
), rn
, lsa
)
183 /* AdvRouter and Router ID is the same. */
184 if (IPV4_ADDR_SAME(&lsa
->data
->adv_router
,
187 OSPF_LSA_SELF_CHECKED
);
188 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
189 ospf_lsa_flush_schedule(ospf
, lsa
);
194 /* Originate each redistributed external route. */
195 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++)
196 thread_add_event(master
,
197 ospf_external_lsa_originate_timer
,
199 thread_add_event(master
, ospf_default_originate_timer
, ospf
, 0,
202 /* update router-lsa's for each area */
203 ospf_router_lsa_update(ospf
);
205 /* update ospf_interface's */
206 for (ALL_LIST_ELEMENTS_RO(om
->iflist
, node
, ifp
))
207 ospf_if_update(ospf
, ifp
);
211 /* For OSPF area sort by area id. */
212 static int ospf_area_id_cmp(struct ospf_area
*a1
, struct ospf_area
*a2
)
214 if (ntohl(a1
->area_id
.s_addr
) > ntohl(a2
->area_id
.s_addr
))
216 if (ntohl(a1
->area_id
.s_addr
) < ntohl(a2
->area_id
.s_addr
))
221 /* Allocate new ospf structure. */
222 static struct ospf
*ospf_new(u_short instance
)
226 struct ospf
*new = XCALLOC(MTYPE_OSPF_TOP
, sizeof(struct ospf
));
228 new->instance
= instance
;
229 new->router_id
.s_addr
= htonl(0);
230 new->router_id_static
.s_addr
= htonl(0);
232 new->abr_type
= OSPF_ABR_DEFAULT
;
233 new->oiflist
= list_new();
234 new->vlinks
= list_new();
235 new->areas
= list_new();
236 new->areas
->cmp
= (int (*)(void *, void *))ospf_area_id_cmp
;
237 new->networks
= route_table_init();
238 new->nbr_nbma
= route_table_init();
240 new->lsdb
= ospf_lsdb_new();
242 new->default_originate
= DEFAULT_ORIGINATE_NONE
;
244 new->passive_interface_default
= OSPF_IF_ACTIVE
;
246 new->new_external_route
= route_table_init();
247 new->old_external_route
= route_table_init();
248 new->external_lsas
= route_table_init();
250 new->stub_router_startup_time
= OSPF_STUB_ROUTER_UNCONFIGURED
;
251 new->stub_router_shutdown_time
= OSPF_STUB_ROUTER_UNCONFIGURED
;
252 new->stub_router_admin_set
= OSPF_STUB_ROUTER_ADMINISTRATIVE_UNSET
;
254 /* Distribute parameter init. */
255 for (i
= 0; i
<= ZEBRA_ROUTE_MAX
; i
++) {
258 new->default_metric
= -1;
259 new->ref_bandwidth
= OSPF_DEFAULT_REF_BANDWIDTH
;
262 new->min_ls_interval
= OSPF_MIN_LS_INTERVAL
;
263 new->min_ls_arrival
= OSPF_MIN_LS_ARRIVAL
;
265 /* SPF timer value init. */
266 new->spf_delay
= OSPF_SPF_DELAY_DEFAULT
;
267 new->spf_holdtime
= OSPF_SPF_HOLDTIME_DEFAULT
;
268 new->spf_max_holdtime
= OSPF_SPF_MAX_HOLDTIME_DEFAULT
;
269 new->spf_hold_multiplier
= 1;
272 new->maxage_delay
= OSPF_LSA_MAXAGE_REMOVE_DELAY_DEFAULT
;
273 new->maxage_lsa
= route_table_init();
274 new->t_maxage_walker
= NULL
;
275 thread_add_timer(master
, ospf_lsa_maxage_walker
, new,
276 OSPF_LSA_MAXAGE_CHECK_INTERVAL
, &new->t_maxage_walker
);
278 /* Distance table init. */
279 new->distance_table
= route_table_init();
281 new->lsa_refresh_queue
.index
= 0;
282 new->lsa_refresh_interval
= OSPF_LSA_REFRESH_INTERVAL_DEFAULT
;
283 new->t_lsa_refresher
= NULL
;
284 thread_add_timer(master
, ospf_lsa_refresh_walker
, new,
285 new->lsa_refresh_interval
, &new->t_lsa_refresher
);
286 new->lsa_refresher_started
= monotime(NULL
);
288 if ((new->fd
= ospf_sock_init()) < 0) {
290 "ospf_new: fatal error: ospf_sock_init was unable to open "
294 if ((new->ibuf
= stream_new(OSPF_MAX_PACKET_SIZE
+ 1)) == NULL
) {
296 "ospf_new: fatal error: stream_new(%u) failed allocating ibuf",
297 OSPF_MAX_PACKET_SIZE
+ 1);
301 thread_add_read(master
, ospf_read
, new, new->fd
, &new->t_read
);
302 new->oi_write_q
= list_new();
303 new->write_oi_count
= OSPF_WRITE_INTERFACE_COUNT_DEFAULT
;
305 /* Enable "log-adjacency-changes" */
306 #if DFLT_OSPF_LOG_ADJACENCY_CHANGES
307 SET_FLAG(new->config
, OSPF_LOG_ADJACENCY_CHANGES
);
315 struct ospf
*ospf_lookup()
317 if (listcount(om
->ospf
) == 0)
320 return listgetdata((struct listnode
*)listhead(om
->ospf
));
323 struct ospf
*ospf_lookup_instance(u_short instance
)
326 struct listnode
*node
, *nnode
;
328 if (listcount(om
->ospf
) == 0)
331 for (ALL_LIST_ELEMENTS(om
->ospf
, node
, nnode
, ospf
))
332 if ((ospf
->instance
== 0 && instance
== 0)
333 || (ospf
->instance
&& instance
334 && ospf
->instance
== instance
))
340 static int ospf_is_ready(struct ospf
*ospf
)
342 /* OSPF must be on and Router-ID must be configured. */
343 if (!ospf
|| ospf
->router_id
.s_addr
== 0)
349 static void ospf_add(struct ospf
*ospf
)
351 listnode_add(om
->ospf
, ospf
);
354 static void ospf_delete(struct ospf
*ospf
)
356 listnode_delete(om
->ospf
, ospf
);
359 struct ospf
*ospf_get()
363 ospf
= ospf_lookup();
368 if (ospf
->router_id_static
.s_addr
== 0)
369 ospf_router_id_update(ospf
);
371 ospf_opaque_type11_lsa_init(ospf
);
377 struct ospf
*ospf_get_instance(u_short instance
)
381 ospf
= ospf_lookup_instance(instance
);
383 ospf
= ospf_new(instance
);
386 if (ospf
->router_id_static
.s_addr
== 0)
387 ospf_router_id_update(ospf
);
389 ospf_opaque_type11_lsa_init(ospf
);
395 /* Handle the second half of deferred shutdown. This is called either
396 * from the deferred-shutdown timer thread, or directly through
397 * ospf_deferred_shutdown_check.
399 * Function is to cleanup G-R state, if required then call ospf_finish_final
400 * to complete shutdown of this ospf instance. Possibly exit if the
401 * whole process is being shutdown and this was the last OSPF instance.
403 static void ospf_deferred_shutdown_finish(struct ospf
*ospf
)
405 ospf
->stub_router_shutdown_time
= OSPF_STUB_ROUTER_UNCONFIGURED
;
406 OSPF_TIMER_OFF(ospf
->t_deferred_shutdown
);
408 ospf_finish_final(ospf
);
410 /* *ospf is now invalid */
412 /* ospfd being shut-down? If so, was this the last ospf instance? */
413 if (CHECK_FLAG(om
->options
, OSPF_MASTER_SHUTDOWN
)
414 && (listcount(om
->ospf
) == 0)) {
421 /* Timer thread for G-R */
422 static int ospf_deferred_shutdown_timer(struct thread
*t
)
424 struct ospf
*ospf
= THREAD_ARG(t
);
426 ospf_deferred_shutdown_finish(ospf
);
431 /* Check whether deferred-shutdown must be scheduled, otherwise call
432 * down directly into second-half of instance shutdown.
434 static void ospf_deferred_shutdown_check(struct ospf
*ospf
)
436 unsigned long timeout
;
438 struct ospf_area
*area
;
440 /* deferred shutdown already running? */
441 if (ospf
->t_deferred_shutdown
)
444 /* Should we try push out max-metric LSAs? */
445 if (ospf
->stub_router_shutdown_time
!= OSPF_STUB_ROUTER_UNCONFIGURED
) {
446 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, ln
, area
)) {
447 SET_FLAG(area
->stub_router_state
,
448 OSPF_AREA_ADMIN_STUB_ROUTED
);
450 if (!CHECK_FLAG(area
->stub_router_state
,
451 OSPF_AREA_IS_STUB_ROUTED
))
452 ospf_router_lsa_update_area(area
);
454 timeout
= ospf
->stub_router_shutdown_time
;
456 /* No timer needed */
457 ospf_deferred_shutdown_finish(ospf
);
461 OSPF_TIMER_ON(ospf
->t_deferred_shutdown
, ospf_deferred_shutdown_timer
,
466 /* Shut down the entire process */
467 void ospf_terminate(void)
470 struct listnode
*node
, *nnode
;
472 /* shutdown already in progress */
473 if (CHECK_FLAG(om
->options
, OSPF_MASTER_SHUTDOWN
))
476 SET_FLAG(om
->options
, OSPF_MASTER_SHUTDOWN
);
478 /* exit immediately if OSPF not actually running */
479 if (listcount(om
->ospf
) == 0)
483 for (ALL_LIST_ELEMENTS(om
->ospf
, node
, nnode
, ospf
))
486 /* Deliberately go back up, hopefully to thread scheduler, as
487 * One or more ospf_finish()'s may have deferred shutdown to a timer
490 zclient_stop(zclient
);
491 zclient_free(zclient
);
494 void ospf_finish(struct ospf
*ospf
)
496 /* let deferred shutdown decide */
497 ospf_deferred_shutdown_check(ospf
);
499 /* if ospf_deferred_shutdown returns, then ospf_finish_final is
500 * deferred to expiry of G-S timer thread. Return back up, hopefully
501 * to thread scheduler.
506 /* Final cleanup of ospf instance */
507 static void ospf_finish_final(struct ospf
*ospf
)
509 struct route_node
*rn
;
510 struct ospf_nbr_nbma
*nbr_nbma
;
511 struct ospf_lsa
*lsa
;
512 struct interface
*ifp
;
513 struct ospf_interface
*oi
;
514 struct ospf_area
*area
;
515 struct ospf_vl_data
*vl_data
;
516 struct listnode
*node
, *nnode
;
518 u_short instance
= 0;
522 ospf_opaque_type11_lsa_term(ospf
);
524 /* be nice if this worked, but it doesn't */
525 /*ospf_flush_self_originated_lsas_now (ospf);*/
527 /* Unregister redistribution */
528 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
529 struct list
*red_list
;
530 struct ospf_redist
*red
;
532 red_list
= ospf
->redist
[i
];
536 for (ALL_LIST_ELEMENTS(red_list
, node
, nnode
, red
))
537 ospf_redistribute_unset(ospf
, i
, red
->instance
);
539 ospf_redistribute_default_unset(ospf
);
541 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
))
542 ospf_remove_vls_through_area(ospf
, area
);
544 for (ALL_LIST_ELEMENTS(ospf
->vlinks
, node
, nnode
, vl_data
))
545 ospf_vl_delete(ospf
, vl_data
);
547 list_delete(ospf
->vlinks
);
549 /* Remove any ospf interface config params */
550 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT
), node
, ifp
)) {
551 struct ospf_if_params
*params
;
553 params
= IF_DEF_PARAMS(ifp
);
554 if (OSPF_IF_PARAM_CONFIGURED(params
, if_area
))
555 UNSET_IF_PARAM(params
, if_area
);
558 /* Reset interface. */
559 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
))
562 /* Clear static neighbors */
563 for (rn
= route_top(ospf
->nbr_nbma
); rn
; rn
= route_next(rn
))
564 if ((nbr_nbma
= rn
->info
)) {
565 OSPF_POLL_TIMER_OFF(nbr_nbma
->t_poll
);
568 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
569 nbr_nbma
->nbr
= NULL
;
573 listnode_delete(nbr_nbma
->oi
->nbr_nbma
,
578 XFREE(MTYPE_OSPF_NEIGHBOR_STATIC
, nbr_nbma
);
581 route_table_finish(ospf
->nbr_nbma
);
583 /* Clear networks and Areas. */
584 for (rn
= route_top(ospf
->networks
); rn
; rn
= route_next(rn
)) {
585 struct ospf_network
*network
;
587 if ((network
= rn
->info
) != NULL
) {
588 ospf_network_free(ospf
, network
);
590 route_unlock_node(rn
);
594 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
595 listnode_delete(ospf
->areas
, area
);
596 ospf_area_free(area
);
599 /* Cancel all timers. */
600 OSPF_TIMER_OFF(ospf
->t_external_lsa
);
601 OSPF_TIMER_OFF(ospf
->t_spf_calc
);
602 OSPF_TIMER_OFF(ospf
->t_ase_calc
);
603 OSPF_TIMER_OFF(ospf
->t_maxage
);
604 OSPF_TIMER_OFF(ospf
->t_maxage_walker
);
605 OSPF_TIMER_OFF(ospf
->t_abr_task
);
606 OSPF_TIMER_OFF(ospf
->t_asbr_check
);
607 OSPF_TIMER_OFF(ospf
->t_distribute_update
);
608 OSPF_TIMER_OFF(ospf
->t_lsa_refresher
);
609 OSPF_TIMER_OFF(ospf
->t_read
);
610 OSPF_TIMER_OFF(ospf
->t_write
);
611 OSPF_TIMER_OFF(ospf
->t_opaque_lsa_self
);
614 stream_free(ospf
->ibuf
);
616 LSDB_LOOP(OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
617 ospf_discard_from_db(ospf
, ospf
->lsdb
, lsa
);
618 LSDB_LOOP(EXTERNAL_LSDB(ospf
), rn
, lsa
)
619 ospf_discard_from_db(ospf
, ospf
->lsdb
, lsa
);
621 ospf_lsdb_delete_all(ospf
->lsdb
);
622 ospf_lsdb_free(ospf
->lsdb
);
624 for (rn
= route_top(ospf
->maxage_lsa
); rn
; rn
= route_next(rn
)) {
625 struct ospf_lsa
*lsa
;
627 if ((lsa
= rn
->info
) != NULL
) {
628 ospf_lsa_unlock(&lsa
);
631 route_unlock_node(rn
);
633 route_table_finish(ospf
->maxage_lsa
);
636 ospf_route_table_free(ospf
->old_table
);
637 if (ospf
->new_table
) {
638 ospf_route_delete(ospf
->new_table
);
639 ospf_route_table_free(ospf
->new_table
);
642 ospf_rtrs_free(ospf
->old_rtrs
);
644 ospf_rtrs_free(ospf
->new_rtrs
);
645 if (ospf
->new_external_route
) {
646 ospf_route_delete(ospf
->new_external_route
);
647 ospf_route_table_free(ospf
->new_external_route
);
649 if (ospf
->old_external_route
) {
650 ospf_route_delete(ospf
->old_external_route
);
651 ospf_route_table_free(ospf
->old_external_route
);
653 if (ospf
->external_lsas
) {
654 ospf_ase_external_lsas_finish(ospf
->external_lsas
);
657 list_delete(ospf
->areas
);
659 for (i
= ZEBRA_ROUTE_SYSTEM
; i
<= ZEBRA_ROUTE_MAX
; i
++) {
660 struct list
*ext_list
;
661 struct listnode
*node
;
662 struct ospf_external
*ext
;
664 ext_list
= om
->external
[i
];
668 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
669 if (ext
->external_info
)
670 for (rn
= route_top(ext
->external_info
); rn
;
671 rn
= route_next(rn
)) {
672 if (rn
->info
== NULL
)
675 XFREE(MTYPE_OSPF_EXTERNAL_INFO
,
678 route_unlock_node(rn
);
683 ospf_distance_reset(ospf
);
684 route_table_finish(ospf
->distance_table
);
686 if (!CHECK_FLAG(om
->options
, OSPF_MASTER_SHUTDOWN
))
687 instance
= ospf
->instance
;
691 XFREE(MTYPE_OSPF_TOP
, ospf
);
693 if (!CHECK_FLAG(om
->options
, OSPF_MASTER_SHUTDOWN
))
694 ospf_get_instance(instance
);
698 /* allocate new OSPF Area object */
699 static struct ospf_area
*ospf_area_new(struct ospf
*ospf
,
700 struct in_addr area_id
)
702 struct ospf_area
*new;
704 /* Allocate new config_network. */
705 new = XCALLOC(MTYPE_OSPF_AREA
, sizeof(struct ospf_area
));
709 new->area_id
= area_id
;
710 new->area_id_fmt
= OSPF_AREA_ID_FMT_DOTTEDQUAD
;
712 new->external_routing
= OSPF_AREA_DEFAULT
;
713 new->default_cost
= 1;
714 new->auth_type
= OSPF_AUTH_NULL
;
717 new->lsdb
= ospf_lsdb_new();
719 /* Self-originated LSAs initialize. */
720 new->router_lsa_self
= NULL
;
722 ospf_opaque_type10_lsa_init(new);
724 new->oiflist
= list_new();
725 new->ranges
= route_table_init();
727 if (area_id
.s_addr
== OSPF_AREA_BACKBONE
)
728 ospf
->backbone
= new;
733 static void ospf_area_free(struct ospf_area
*area
)
735 struct route_node
*rn
;
736 struct ospf_lsa
*lsa
;
739 LSDB_LOOP(ROUTER_LSDB(area
), rn
, lsa
)
740 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
741 LSDB_LOOP(NETWORK_LSDB(area
), rn
, lsa
)
742 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
743 LSDB_LOOP(SUMMARY_LSDB(area
), rn
, lsa
)
744 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
745 LSDB_LOOP(ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
746 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
748 LSDB_LOOP(NSSA_LSDB(area
), rn
, lsa
)
749 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
750 LSDB_LOOP(OPAQUE_AREA_LSDB(area
), rn
, lsa
)
751 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
752 LSDB_LOOP(OPAQUE_LINK_LSDB(area
), rn
, lsa
)
753 ospf_discard_from_db(area
->ospf
, area
->lsdb
, lsa
);
755 ospf_lsdb_delete_all(area
->lsdb
);
756 ospf_lsdb_free(area
->lsdb
);
758 ospf_lsa_unlock(&area
->router_lsa_self
);
760 route_table_finish(area
->ranges
);
761 list_delete(area
->oiflist
);
763 if (EXPORT_NAME(area
))
764 free(EXPORT_NAME(area
));
766 if (IMPORT_NAME(area
))
767 free(IMPORT_NAME(area
));
770 OSPF_TIMER_OFF(area
->t_stub_router
);
771 OSPF_TIMER_OFF(area
->t_opaque_lsa_self
);
773 if (OSPF_IS_AREA_BACKBONE(area
))
774 area
->ospf
->backbone
= NULL
;
776 XFREE(MTYPE_OSPF_AREA
, area
);
779 void ospf_area_check_free(struct ospf
*ospf
, struct in_addr area_id
)
781 struct ospf_area
*area
;
783 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
784 if (area
&& listcount(area
->oiflist
) == 0 && area
->ranges
->top
== NULL
785 && area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
786 && area
->external_routing
== OSPF_AREA_DEFAULT
787 && area
->no_summary
== 0 && area
->default_cost
== 1
788 && EXPORT_NAME(area
) == NULL
&& IMPORT_NAME(area
) == NULL
789 && area
->auth_type
== OSPF_AUTH_NULL
) {
790 listnode_delete(ospf
->areas
, area
);
791 ospf_area_free(area
);
795 struct ospf_area
*ospf_area_get(struct ospf
*ospf
, struct in_addr area_id
)
797 struct ospf_area
*area
;
799 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
801 area
= ospf_area_new(ospf
, area_id
);
802 listnode_add_sort(ospf
->areas
, area
);
803 ospf_check_abr_status(ospf
);
804 if (ospf
->stub_router_admin_set
805 == OSPF_STUB_ROUTER_ADMINISTRATIVE_SET
) {
806 SET_FLAG(area
->stub_router_state
,
807 OSPF_AREA_ADMIN_STUB_ROUTED
);
814 struct ospf_area
*ospf_area_lookup_by_area_id(struct ospf
*ospf
,
815 struct in_addr area_id
)
817 struct ospf_area
*area
;
818 struct listnode
*node
;
820 for (ALL_LIST_ELEMENTS_RO(ospf
->areas
, node
, area
))
821 if (IPV4_ADDR_SAME(&area
->area_id
, &area_id
))
827 void ospf_area_add_if(struct ospf_area
*area
, struct ospf_interface
*oi
)
829 listnode_add(area
->oiflist
, oi
);
832 void ospf_area_del_if(struct ospf_area
*area
, struct ospf_interface
*oi
)
834 listnode_delete(area
->oiflist
, oi
);
838 static void add_ospf_interface(struct connected
*co
, struct ospf_area
*area
)
840 struct ospf_interface
*oi
;
842 oi
= ospf_if_new(area
->ospf
, co
->ifp
, co
->address
);
847 oi
->params
= ospf_lookup_if_params(co
->ifp
, oi
->address
->u
.prefix4
);
848 oi
->output_cost
= ospf_if_get_output_cost(oi
);
850 /* Relate ospf interface to ospf instance. */
851 oi
->ospf
= area
->ospf
;
853 /* update network type as interface flag */
854 /* If network type is specified previously,
855 skip network type setting. */
856 oi
->type
= IF_DEF_PARAMS(co
->ifp
)->type
;
858 /* Add pseudo neighbor. */
859 ospf_nbr_self_reset(oi
, oi
->ospf
->router_id
);
861 ospf_area_add_if(oi
->area
, oi
);
864 * if router_id is not configured, dont bring up
866 * ospf_router_id_update() will call ospf_if_update
867 * whenever r-id is configured instead.
869 if ((area
->ospf
->router_id
.s_addr
!= 0) && if_is_operative(co
->ifp
))
873 static void update_redistributed(struct ospf
*ospf
, int add_to_ospf
)
875 struct route_node
*rn
;
876 struct external_info
*ei
;
877 struct ospf_external
*ext
;
879 if (ospf_is_type_redistributed(ZEBRA_ROUTE_CONNECT
, 0))
880 if ((ext
= ospf_external_lookup(ZEBRA_ROUTE_CONNECT
, 0))
881 && EXTERNAL_INFO(ext
)) {
882 for (rn
= route_top(EXTERNAL_INFO(ext
)); rn
;
883 rn
= route_next(rn
)) {
884 if ((ei
= rn
->info
) != NULL
) {
886 if (ospf_external_info_find_lsa(
888 if (!ospf_distribute_check_connected(
890 ospf_external_lsa_flush(
894 ei
->ifindex
/*, ei->nexthop */);
896 if (!ospf_external_info_find_lsa(
898 if (ospf_distribute_check_connected(
900 ospf_external_lsa_originate(
909 /* Config network statement related functions. */
910 static struct ospf_network
*ospf_network_new(struct in_addr area_id
)
912 struct ospf_network
*new;
913 new = XCALLOC(MTYPE_OSPF_NETWORK
, sizeof(struct ospf_network
));
915 new->area_id
= area_id
;
916 new->area_id_fmt
= OSPF_AREA_ID_FMT_DOTTEDQUAD
;
921 static void ospf_network_free(struct ospf
*ospf
, struct ospf_network
*network
)
923 ospf_area_check_free(ospf
, network
->area_id
);
924 ospf_schedule_abr_task(ospf
);
925 XFREE(MTYPE_OSPF_NETWORK
, network
);
928 int ospf_network_set(struct ospf
*ospf
, struct prefix_ipv4
*p
,
929 struct in_addr area_id
, int df
)
931 struct ospf_network
*network
;
932 struct ospf_area
*area
;
933 struct route_node
*rn
;
935 rn
= route_node_get(ospf
->networks
, (struct prefix
*)p
);
937 /* There is already same network statement. */
938 route_unlock_node(rn
);
942 rn
->info
= network
= ospf_network_new(area_id
);
943 network
->area_id_fmt
= df
;
944 area
= ospf_area_get(ospf
, area_id
);
945 ospf_area_display_format_set(ospf
, area
, df
);
947 /* Run network config now. */
948 ospf_network_run((struct prefix
*)p
, area
);
950 /* Update connected redistribute. */
951 update_redistributed(ospf
, 1); /* interfaces possibly added */
953 ospf_area_check_free(ospf
, area_id
);
958 int ospf_network_unset(struct ospf
*ospf
, struct prefix_ipv4
*p
,
959 struct in_addr area_id
)
961 struct route_node
*rn
;
962 struct ospf_network
*network
;
963 struct listnode
*node
, *nnode
;
964 struct ospf_interface
*oi
;
966 rn
= route_node_lookup(ospf
->networks
, (struct prefix
*)p
);
971 route_unlock_node(rn
);
972 if (!IPV4_ADDR_SAME(&area_id
, &network
->area_id
))
975 ospf_network_free(ospf
, rn
->info
);
977 route_unlock_node(rn
); /* initial reference */
979 /* Find interfaces that not configured already. */
980 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
)) {
981 ospf_network_run_subnet(ospf
, oi
->connected
, NULL
, NULL
);
984 /* Update connected redistribute. */
985 update_redistributed(ospf
, 0); /* interfaces possibly removed */
986 ospf_area_check_free(ospf
, area_id
);
991 /* Ensure there's an OSPF instance, as "ip ospf area" enabled OSPF means
992 * there might not be any 'router ospf' config.
994 * Otherwise, doesn't do anything different to ospf_if_update for now
996 void ospf_interface_area_set(struct interface
*ifp
)
998 struct ospf
*ospf
= ospf_get();
1000 ospf_if_update(ospf
, ifp
);
1001 /* if_update does a update_redistributed */
1006 void ospf_interface_area_unset(struct interface
*ifp
)
1008 struct route_node
*rn_oi
;
1011 ospf
= ospf_lookup();
1013 return; /* Ospf not ready yet */
1015 /* Find interfaces that may need to be removed. */
1016 for (rn_oi
= route_top(IF_OIFS(ifp
)); rn_oi
;
1017 rn_oi
= route_next(rn_oi
)) {
1018 struct ospf_interface
*oi
;
1020 if ((oi
= rn_oi
->info
) == NULL
)
1023 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
1026 ospf_network_run_subnet(ospf
, oi
->connected
, NULL
, NULL
);
1029 /* Update connected redistribute. */
1030 update_redistributed(ospf
, 0); /* interfaces possibly removed */
1033 /* Check whether interface matches given network
1034 * returns: 1, true. 0, false
1036 static int ospf_network_match_iface(const struct connected
*co
,
1037 const struct prefix
*net
)
1039 /* new approach: more elegant and conceptually clean */
1040 return prefix_match_network_statement(net
, CONNECTED_PREFIX(co
));
1043 static void ospf_update_interface_area(struct connected
*co
,
1044 struct ospf_area
*area
)
1046 struct ospf_interface
*oi
= ospf_if_table_lookup(co
->ifp
, co
->address
);
1048 /* nothing to be done case */
1049 if (oi
&& oi
->area
== area
) {
1056 add_ospf_interface(co
, area
);
1059 /* Run OSPF for the given subnet, taking into account the following
1060 * possible sources of area configuration, in the given order of preference:
1062 * - Whether there is interface+address specific area configuration
1063 * - Whether there is a default area for the interface
1064 * - Whether there is an area given as a parameter.
1065 * - If no specific network prefix/area is supplied, whether there's
1066 * a matching network configured.
1068 static void ospf_network_run_subnet(struct ospf
*ospf
, struct connected
*co
,
1070 struct ospf_area
*given_area
)
1072 struct ospf_interface
*oi
;
1073 struct ospf_if_params
*params
;
1074 struct ospf_area
*area
= NULL
;
1075 struct route_node
*rn
;
1078 if (CHECK_FLAG(co
->flags
, ZEBRA_IFA_SECONDARY
))
1081 if (co
->address
->family
!= AF_INET
)
1084 /* Try determine the appropriate area for this interface + address
1085 * Start by checking interface config
1087 params
= ospf_lookup_if_params(co
->ifp
, co
->address
->u
.prefix4
);
1088 if (params
&& OSPF_IF_PARAM_CONFIGURED(params
, if_area
))
1089 area
= ospf_area_get(ospf
, params
->if_area
);
1091 params
= IF_DEF_PARAMS(co
->ifp
);
1092 if (OSPF_IF_PARAM_CONFIGURED(params
, if_area
))
1093 area
= ospf_area_get(ospf
, params
->if_area
);
1096 /* If we've found an interface and/or addr specific area, then we're
1100 ospf_update_interface_area(co
, area
);
1104 /* Otherwise, only remaining possibility is a matching network statement
1107 assert(given_area
!= NULL
);
1109 /* Which either was supplied as a parameter.. (e.g. cause a new
1110 * network/area was just added)..
1112 if (p
->family
== co
->address
->family
1113 && ospf_network_match_iface(co
, p
))
1114 ospf_update_interface_area(co
, given_area
);
1119 /* Else we have to search the existing network/area config to see
1122 for (rn
= route_top(ospf
->networks
); rn
; rn
= route_next(rn
))
1123 if (rn
->info
!= NULL
&& ospf_network_match_iface(co
, &rn
->p
)) {
1124 struct ospf_network
*network
=
1125 (struct ospf_network
*)rn
->info
;
1126 area
= ospf_area_get(ospf
, network
->area_id
);
1127 ospf_update_interface_area(co
, area
);
1131 /* If the subnet isn't in any area, deconfigure */
1132 if (!configed
&& (oi
= ospf_if_table_lookup(co
->ifp
, co
->address
)))
1136 static void ospf_network_run_interface(struct ospf
*ospf
, struct interface
*ifp
,
1138 struct ospf_area
*given_area
)
1140 struct listnode
*cnode
;
1141 struct connected
*co
;
1143 if (memcmp(ifp
->name
, "VLINK", 5) == 0)
1146 /* Network prefix without area is nonsensical */
1148 assert(given_area
!= NULL
);
1150 /* if interface prefix is match specified prefix,
1151 then create socket and join multicast group. */
1152 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, co
))
1153 ospf_network_run_subnet(ospf
, co
, p
, given_area
);
1156 static void ospf_network_run(struct prefix
*p
, struct ospf_area
*area
)
1158 struct interface
*ifp
;
1159 struct listnode
*node
;
1161 /* Schedule Router ID Update. */
1162 if (area
->ospf
->router_id
.s_addr
== 0)
1163 ospf_router_id_update(area
->ospf
);
1165 /* Get target interface. */
1166 for (ALL_LIST_ELEMENTS_RO(om
->iflist
, node
, ifp
))
1167 ospf_network_run_interface(area
->ospf
, ifp
, p
, area
);
1170 void ospf_ls_upd_queue_empty(struct ospf_interface
*oi
)
1172 struct route_node
*rn
;
1173 struct listnode
*node
, *nnode
;
1175 struct ospf_lsa
*lsa
;
1177 /* empty ls update queue */
1178 for (rn
= route_top(oi
->ls_upd_queue
); rn
; rn
= route_next(rn
))
1179 if ((lst
= (struct list
*)rn
->info
)) {
1180 for (ALL_LIST_ELEMENTS(lst
, node
, nnode
, lsa
))
1181 ospf_lsa_unlock(&lsa
); /* oi->ls_upd_queue */
1186 /* remove update event */
1187 if (oi
->t_ls_upd_event
) {
1188 thread_cancel(oi
->t_ls_upd_event
);
1189 oi
->t_ls_upd_event
= NULL
;
1193 void ospf_if_update(struct ospf
*ospf
, struct interface
*ifp
)
1196 ospf
= ospf_lookup();
1198 /* OSPF must be ready. */
1199 if (!ospf_is_ready(ospf
))
1202 ospf_network_run_interface(ospf
, ifp
, NULL
, NULL
);
1204 /* Update connected redistribute. */
1205 update_redistributed(ospf
, 1);
1208 void ospf_remove_vls_through_area(struct ospf
*ospf
, struct ospf_area
*area
)
1210 struct listnode
*node
, *nnode
;
1211 struct ospf_vl_data
*vl_data
;
1213 for (ALL_LIST_ELEMENTS(ospf
->vlinks
, node
, nnode
, vl_data
))
1214 if (IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
))
1215 ospf_vl_delete(ospf
, vl_data
);
1219 static const struct message ospf_area_type_msg
[] = {
1220 {OSPF_AREA_DEFAULT
, "Default"},
1221 {OSPF_AREA_STUB
, "Stub"},
1222 {OSPF_AREA_NSSA
, "NSSA"},
1225 static void ospf_area_type_set(struct ospf_area
*area
, int type
)
1227 struct listnode
*node
;
1228 struct ospf_interface
*oi
;
1230 if (area
->external_routing
== type
) {
1231 if (IS_DEBUG_OSPF_EVENT
)
1232 zlog_debug("Area[%s]: Types are the same, ignored.",
1233 inet_ntoa(area
->area_id
));
1237 area
->external_routing
= type
;
1239 if (IS_DEBUG_OSPF_EVENT
)
1240 zlog_debug("Area[%s]: Configured as %s",
1241 inet_ntoa(area
->area_id
),
1242 lookup_msg(ospf_area_type_msg
, type
, NULL
));
1244 switch (area
->external_routing
) {
1245 case OSPF_AREA_DEFAULT
:
1246 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
))
1247 if (oi
->nbr_self
!= NULL
) {
1248 UNSET_FLAG(oi
->nbr_self
->options
,
1250 SET_FLAG(oi
->nbr_self
->options
, OSPF_OPTION_E
);
1253 case OSPF_AREA_STUB
:
1254 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
))
1255 if (oi
->nbr_self
!= NULL
) {
1256 if (IS_DEBUG_OSPF_EVENT
)
1258 "setting options on %s accordingly",
1260 UNSET_FLAG(oi
->nbr_self
->options
,
1262 UNSET_FLAG(oi
->nbr_self
->options
,
1264 if (IS_DEBUG_OSPF_EVENT
)
1265 zlog_debug("options set on %s: %x",
1266 IF_NAME(oi
), OPTIONS(oi
));
1269 case OSPF_AREA_NSSA
:
1270 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
))
1271 if (oi
->nbr_self
!= NULL
) {
1273 "setting nssa options on %s accordingly",
1275 UNSET_FLAG(oi
->nbr_self
->options
,
1277 SET_FLAG(oi
->nbr_self
->options
, OSPF_OPTION_NP
);
1278 zlog_debug("options set on %s: %x", IF_NAME(oi
),
1286 ospf_router_lsa_update_area(area
);
1287 ospf_schedule_abr_task(area
->ospf
);
1290 int ospf_area_shortcut_set(struct ospf
*ospf
, struct ospf_area
*area
, int mode
)
1292 if (area
->shortcut_configured
== mode
)
1295 area
->shortcut_configured
= mode
;
1296 ospf_router_lsa_update_area(area
);
1297 ospf_schedule_abr_task(ospf
);
1299 ospf_area_check_free(ospf
, area
->area_id
);
1304 int ospf_area_shortcut_unset(struct ospf
*ospf
, struct ospf_area
*area
)
1306 area
->shortcut_configured
= OSPF_SHORTCUT_DEFAULT
;
1307 ospf_router_lsa_update_area(area
);
1308 ospf_area_check_free(ospf
, area
->area_id
);
1309 ospf_schedule_abr_task(ospf
);
1314 static int ospf_area_vlink_count(struct ospf
*ospf
, struct ospf_area
*area
)
1316 struct ospf_vl_data
*vl
;
1317 struct listnode
*node
;
1320 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl
))
1321 if (IPV4_ADDR_SAME(&vl
->vl_area_id
, &area
->area_id
))
1327 int ospf_area_display_format_set(struct ospf
*ospf
, struct ospf_area
*area
,
1330 area
->area_id_fmt
= df
;
1335 int ospf_area_stub_set(struct ospf
*ospf
, struct in_addr area_id
)
1337 struct ospf_area
*area
;
1339 area
= ospf_area_get(ospf
, area_id
);
1340 if (ospf_area_vlink_count(ospf
, area
))
1343 if (area
->external_routing
!= OSPF_AREA_STUB
)
1344 ospf_area_type_set(area
, OSPF_AREA_STUB
);
1349 int ospf_area_stub_unset(struct ospf
*ospf
, struct in_addr area_id
)
1351 struct ospf_area
*area
;
1353 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1357 if (area
->external_routing
== OSPF_AREA_STUB
)
1358 ospf_area_type_set(area
, OSPF_AREA_DEFAULT
);
1360 ospf_area_check_free(ospf
, area_id
);
1365 int ospf_area_no_summary_set(struct ospf
*ospf
, struct in_addr area_id
)
1367 struct ospf_area
*area
;
1369 area
= ospf_area_get(ospf
, area_id
);
1370 area
->no_summary
= 1;
1375 int ospf_area_no_summary_unset(struct ospf
*ospf
, struct in_addr area_id
)
1377 struct ospf_area
*area
;
1379 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1383 area
->no_summary
= 0;
1384 ospf_area_check_free(ospf
, area_id
);
1389 int ospf_area_nssa_set(struct ospf
*ospf
, struct in_addr area_id
)
1391 struct ospf_area
*area
;
1393 area
= ospf_area_get(ospf
, area_id
);
1394 if (ospf_area_vlink_count(ospf
, area
))
1397 if (area
->external_routing
!= OSPF_AREA_NSSA
) {
1398 ospf_area_type_set(area
, OSPF_AREA_NSSA
);
1402 /* set NSSA area defaults */
1403 area
->no_summary
= 0;
1404 area
->NSSATranslatorRole
= OSPF_NSSA_ROLE_CANDIDATE
;
1405 area
->NSSATranslatorState
= OSPF_NSSA_TRANSLATE_DISABLED
;
1406 area
->NSSATranslatorStabilityInterval
= OSPF_NSSA_TRANS_STABLE_DEFAULT
;
1411 int ospf_area_nssa_unset(struct ospf
*ospf
, struct in_addr area_id
)
1413 struct ospf_area
*area
;
1415 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1419 if (area
->external_routing
== OSPF_AREA_NSSA
) {
1421 ospf_area_type_set(area
, OSPF_AREA_DEFAULT
);
1424 ospf_area_check_free(ospf
, area_id
);
1429 int ospf_area_nssa_translator_role_set(struct ospf
*ospf
,
1430 struct in_addr area_id
, int role
)
1432 struct ospf_area
*area
;
1434 area
= ospf_area_lookup_by_area_id(ospf
, area_id
);
1438 area
->NSSATranslatorRole
= role
;
1444 /* XXX: unused? Leave for symmetry? */
1446 ospf_area_nssa_translator_role_unset (struct ospf
*ospf
,
1447 struct in_addr area_id
)
1449 struct ospf_area
*area
;
1451 area
= ospf_area_lookup_by_area_id (ospf
, area_id
);
1455 area
->NSSATranslatorRole
= OSPF_NSSA_ROLE_CANDIDATE
;
1457 ospf_area_check_free (ospf
, area_id
);
1463 int ospf_area_export_list_set(struct ospf
*ospf
, struct ospf_area
*area
,
1464 const char *list_name
)
1466 struct access_list
*list
;
1467 list
= access_list_lookup(AFI_IP
, list_name
);
1469 EXPORT_LIST(area
) = list
;
1471 if (EXPORT_NAME(area
))
1472 free(EXPORT_NAME(area
));
1474 EXPORT_NAME(area
) = strdup(list_name
);
1475 ospf_schedule_abr_task(ospf
);
1480 int ospf_area_export_list_unset(struct ospf
*ospf
, struct ospf_area
*area
)
1483 EXPORT_LIST(area
) = 0;
1485 if (EXPORT_NAME(area
))
1486 free(EXPORT_NAME(area
));
1488 EXPORT_NAME(area
) = NULL
;
1490 ospf_area_check_free(ospf
, area
->area_id
);
1492 ospf_schedule_abr_task(ospf
);
1497 int ospf_area_import_list_set(struct ospf
*ospf
, struct ospf_area
*area
,
1500 struct access_list
*list
;
1501 list
= access_list_lookup(AFI_IP
, name
);
1503 IMPORT_LIST(area
) = list
;
1505 if (IMPORT_NAME(area
))
1506 free(IMPORT_NAME(area
));
1508 IMPORT_NAME(area
) = strdup(name
);
1509 ospf_schedule_abr_task(ospf
);
1514 int ospf_area_import_list_unset(struct ospf
*ospf
, struct ospf_area
*area
)
1516 IMPORT_LIST(area
) = 0;
1518 if (IMPORT_NAME(area
))
1519 free(IMPORT_NAME(area
));
1521 IMPORT_NAME(area
) = NULL
;
1522 ospf_area_check_free(ospf
, area
->area_id
);
1524 ospf_schedule_abr_task(ospf
);
1529 int ospf_timers_refresh_set(struct ospf
*ospf
, int interval
)
1533 if (ospf
->lsa_refresh_interval
== interval
)
1536 time_left
= ospf
->lsa_refresh_interval
1537 - (monotime(NULL
) - ospf
->lsa_refresher_started
);
1539 if (time_left
> interval
) {
1540 OSPF_TIMER_OFF(ospf
->t_lsa_refresher
);
1541 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
1542 interval
, &ospf
->t_lsa_refresher
);
1544 ospf
->lsa_refresh_interval
= interval
;
1549 int ospf_timers_refresh_unset(struct ospf
*ospf
)
1553 time_left
= ospf
->lsa_refresh_interval
1554 - (monotime(NULL
) - ospf
->lsa_refresher_started
);
1556 if (time_left
> OSPF_LSA_REFRESH_INTERVAL_DEFAULT
) {
1557 OSPF_TIMER_OFF(ospf
->t_lsa_refresher
);
1558 ospf
->t_lsa_refresher
= NULL
;
1559 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
1560 OSPF_LSA_REFRESH_INTERVAL_DEFAULT
,
1561 &ospf
->t_lsa_refresher
);
1564 ospf
->lsa_refresh_interval
= OSPF_LSA_REFRESH_INTERVAL_DEFAULT
;
1570 static struct ospf_nbr_nbma
*ospf_nbr_nbma_new(void)
1572 struct ospf_nbr_nbma
*nbr_nbma
;
1574 nbr_nbma
= XCALLOC(MTYPE_OSPF_NEIGHBOR_STATIC
,
1575 sizeof(struct ospf_nbr_nbma
));
1577 nbr_nbma
->priority
= OSPF_NEIGHBOR_PRIORITY_DEFAULT
;
1578 nbr_nbma
->v_poll
= OSPF_POLL_INTERVAL_DEFAULT
;
1583 static void ospf_nbr_nbma_free(struct ospf_nbr_nbma
*nbr_nbma
)
1585 XFREE(MTYPE_OSPF_NEIGHBOR_STATIC
, nbr_nbma
);
1588 static void ospf_nbr_nbma_delete(struct ospf
*ospf
,
1589 struct ospf_nbr_nbma
*nbr_nbma
)
1591 struct route_node
*rn
;
1592 struct prefix_ipv4 p
;
1595 p
.prefix
= nbr_nbma
->addr
;
1596 p
.prefixlen
= IPV4_MAX_BITLEN
;
1598 rn
= route_node_lookup(ospf
->nbr_nbma
, (struct prefix
*)&p
);
1600 ospf_nbr_nbma_free(rn
->info
);
1602 route_unlock_node(rn
);
1603 route_unlock_node(rn
);
1607 static void ospf_nbr_nbma_down(struct ospf_nbr_nbma
*nbr_nbma
)
1609 OSPF_TIMER_OFF(nbr_nbma
->t_poll
);
1611 if (nbr_nbma
->nbr
) {
1612 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
1613 OSPF_NSM_EVENT_EXECUTE(nbr_nbma
->nbr
, NSM_KillNbr
);
1617 listnode_delete(nbr_nbma
->oi
->nbr_nbma
, nbr_nbma
);
1620 static void ospf_nbr_nbma_add(struct ospf_nbr_nbma
*nbr_nbma
,
1621 struct ospf_interface
*oi
)
1623 struct ospf_neighbor
*nbr
;
1624 struct route_node
*rn
;
1627 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
1630 if (nbr_nbma
->nbr
!= NULL
)
1633 if (IPV4_ADDR_SAME(&oi
->nbr_self
->address
.u
.prefix4
, &nbr_nbma
->addr
))
1637 listnode_add(oi
->nbr_nbma
, nbr_nbma
);
1639 /* Get neighbor information from table. */
1641 p
.prefixlen
= IPV4_MAX_BITLEN
;
1642 p
.u
.prefix4
= nbr_nbma
->addr
;
1644 rn
= route_node_get(oi
->nbrs
, (struct prefix
*)&p
);
1647 nbr
->nbr_nbma
= nbr_nbma
;
1648 nbr_nbma
->nbr
= nbr
;
1650 route_unlock_node(rn
);
1652 nbr
= rn
->info
= ospf_nbr_new(oi
);
1653 nbr
->state
= NSM_Down
;
1654 nbr
->src
= nbr_nbma
->addr
;
1655 nbr
->nbr_nbma
= nbr_nbma
;
1656 nbr
->priority
= nbr_nbma
->priority
;
1659 nbr_nbma
->nbr
= nbr
;
1661 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_Start
);
1665 void ospf_nbr_nbma_if_update(struct ospf
*ospf
, struct ospf_interface
*oi
)
1667 struct ospf_nbr_nbma
*nbr_nbma
;
1668 struct route_node
*rn
;
1669 struct prefix_ipv4 p
;
1671 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
1674 for (rn
= route_top(ospf
->nbr_nbma
); rn
; rn
= route_next(rn
))
1675 if ((nbr_nbma
= rn
->info
))
1676 if (nbr_nbma
->oi
== NULL
&& nbr_nbma
->nbr
== NULL
) {
1678 p
.prefix
= nbr_nbma
->addr
;
1679 p
.prefixlen
= IPV4_MAX_BITLEN
;
1681 if (prefix_match(oi
->address
,
1682 (struct prefix
*)&p
))
1683 ospf_nbr_nbma_add(nbr_nbma
, oi
);
1687 struct ospf_nbr_nbma
*ospf_nbr_nbma_lookup(struct ospf
*ospf
,
1688 struct in_addr nbr_addr
)
1690 struct route_node
*rn
;
1691 struct prefix_ipv4 p
;
1694 p
.prefix
= nbr_addr
;
1695 p
.prefixlen
= IPV4_MAX_BITLEN
;
1697 rn
= route_node_lookup(ospf
->nbr_nbma
, (struct prefix
*)&p
);
1699 route_unlock_node(rn
);
1705 struct ospf_nbr_nbma
*ospf_nbr_nbma_lookup_next(struct ospf
*ospf
,
1706 struct in_addr
*addr
, int first
)
1709 struct ospf_nbr_nbma
*nbr_nbma
;
1710 struct listnode
*node
;
1717 for (ALL_LIST_ELEMENTS_RO (ospf
->nbr_nbma
, node
, nbr_nbma
))
1721 *addr
= nbr_nbma
->addr
;
1724 else if (ntohl (nbr_nbma
->addr
.s_addr
) > ntohl (addr
->s_addr
))
1726 *addr
= nbr_nbma
->addr
;
1734 int ospf_nbr_nbma_set(struct ospf
*ospf
, struct in_addr nbr_addr
)
1736 struct ospf_nbr_nbma
*nbr_nbma
;
1737 struct ospf_interface
*oi
;
1738 struct prefix_ipv4 p
;
1739 struct route_node
*rn
;
1740 struct listnode
*node
;
1742 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1746 nbr_nbma
= ospf_nbr_nbma_new();
1747 nbr_nbma
->addr
= nbr_addr
;
1750 p
.prefix
= nbr_addr
;
1751 p
.prefixlen
= IPV4_MAX_BITLEN
;
1753 rn
= route_node_get(ospf
->nbr_nbma
, (struct prefix
*)&p
);
1755 route_unlock_node(rn
);
1756 rn
->info
= nbr_nbma
;
1758 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
1759 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1760 if (prefix_match(oi
->address
, (struct prefix
*)&p
)) {
1761 ospf_nbr_nbma_add(nbr_nbma
, oi
);
1769 int ospf_nbr_nbma_unset(struct ospf
*ospf
, struct in_addr nbr_addr
)
1771 struct ospf_nbr_nbma
*nbr_nbma
;
1773 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1774 if (nbr_nbma
== NULL
)
1777 ospf_nbr_nbma_down(nbr_nbma
);
1778 ospf_nbr_nbma_delete(ospf
, nbr_nbma
);
1783 int ospf_nbr_nbma_priority_set(struct ospf
*ospf
, struct in_addr nbr_addr
,
1786 struct ospf_nbr_nbma
*nbr_nbma
;
1788 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1789 if (nbr_nbma
== NULL
)
1792 if (nbr_nbma
->priority
!= priority
)
1793 nbr_nbma
->priority
= priority
;
1798 int ospf_nbr_nbma_priority_unset(struct ospf
*ospf
, struct in_addr nbr_addr
)
1800 struct ospf_nbr_nbma
*nbr_nbma
;
1802 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1803 if (nbr_nbma
== NULL
)
1806 if (nbr_nbma
!= OSPF_NEIGHBOR_PRIORITY_DEFAULT
)
1807 nbr_nbma
->priority
= OSPF_NEIGHBOR_PRIORITY_DEFAULT
;
1812 int ospf_nbr_nbma_poll_interval_set(struct ospf
*ospf
, struct in_addr nbr_addr
,
1813 unsigned int interval
)
1815 struct ospf_nbr_nbma
*nbr_nbma
;
1817 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, nbr_addr
);
1818 if (nbr_nbma
== NULL
)
1821 if (nbr_nbma
->v_poll
!= interval
) {
1822 nbr_nbma
->v_poll
= interval
;
1823 if (nbr_nbma
->oi
&& ospf_if_is_up(nbr_nbma
->oi
)) {
1824 OSPF_TIMER_OFF(nbr_nbma
->t_poll
);
1825 OSPF_POLL_TIMER_ON(nbr_nbma
->t_poll
, ospf_poll_timer
,
1833 int ospf_nbr_nbma_poll_interval_unset(struct ospf
*ospf
, struct in_addr addr
)
1835 struct ospf_nbr_nbma
*nbr_nbma
;
1837 nbr_nbma
= ospf_nbr_nbma_lookup(ospf
, addr
);
1838 if (nbr_nbma
== NULL
)
1841 if (nbr_nbma
->v_poll
!= OSPF_POLL_INTERVAL_DEFAULT
)
1842 nbr_nbma
->v_poll
= OSPF_POLL_INTERVAL_DEFAULT
;
1847 void ospf_master_init(struct thread_master
*master
)
1849 memset(&ospf_master
, 0, sizeof(struct ospf_master
));
1852 om
->ospf
= list_new();
1853 om
->master
= master
;