]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospfd.c
Merge pull request #11903 from sri-mohan1/sri-bfd-dbg1
[mirror_frr.git] / ospfd / ospfd.c
1 /* OSPF version 2 daemon program.
2 * Copyright (C) 1999, 2000 Toshiaki Takada
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
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
19 */
20
21 #include <zebra.h>
22
23 #include "thread.h"
24 #include "vty.h"
25 #include "command.h"
26 #include "linklist.h"
27 #include "prefix.h"
28 #include "table.h"
29 #include "if.h"
30 #include "memory.h"
31 #include "stream.h"
32 #include "log.h"
33 #include "sockunion.h" /* for inet_aton () */
34 #include "zclient.h"
35 #include "routemap.h"
36 #include "plist.h"
37 #include "sockopt.h"
38 #include "bfd.h"
39 #include "libfrr.h"
40 #include "defaults.h"
41 #include "lib_errors.h"
42 #include "ldp_sync.h"
43
44 #include "ospfd/ospfd.h"
45 #include "ospfd/ospf_bfd.h"
46 #include "ospfd/ospf_network.h"
47 #include "ospfd/ospf_interface.h"
48 #include "ospfd/ospf_ism.h"
49 #include "ospfd/ospf_asbr.h"
50 #include "ospfd/ospf_lsa.h"
51 #include "ospfd/ospf_lsdb.h"
52 #include "ospfd/ospf_neighbor.h"
53 #include "ospfd/ospf_nsm.h"
54 #include "ospfd/ospf_spf.h"
55 #include "ospfd/ospf_packet.h"
56 #include "ospfd/ospf_dump.h"
57 #include "ospfd/ospf_route.h"
58 #include "ospfd/ospf_zebra.h"
59 #include "ospfd/ospf_abr.h"
60 #include "ospfd/ospf_flood.h"
61 #include "ospfd/ospf_ase.h"
62 #include "ospfd/ospf_ldp_sync.h"
63 #include "ospfd/ospf_gr.h"
64 #include "ospfd/ospf_apiserver.h"
65
66
67 DEFINE_QOBJ_TYPE(ospf);
68
69 /* OSPF process wide configuration. */
70 static struct ospf_master ospf_master;
71
72 /* OSPF process wide configuration pointer to export. */
73 struct ospf_master *om;
74
75 unsigned short ospf_instance;
76
77 extern struct zclient *zclient;
78
79
80 static void ospf_remove_vls_through_area(struct ospf *, struct ospf_area *);
81 static void ospf_network_free(struct ospf *, struct ospf_network *);
82 static void ospf_area_free(struct ospf_area *);
83 static void ospf_network_run(struct prefix *, struct ospf_area *);
84 static void ospf_network_run_interface(struct ospf *, struct interface *,
85 struct prefix *, struct ospf_area *);
86 static void ospf_network_run_subnet(struct ospf *, struct connected *,
87 struct prefix *, struct ospf_area *);
88 static int ospf_network_match_iface(const struct connected *,
89 const struct prefix *);
90 static void ospf_finish_final(struct ospf *);
91
92 #define OSPF_EXTERNAL_LSA_ORIGINATE_DELAY 1
93
94 int p_spaces_compare_func(const struct p_space *a, const struct p_space *b)
95 {
96 if (a->protected_resource->type == OSPF_TI_LFA_LINK_PROTECTION
97 && b->protected_resource->type == OSPF_TI_LFA_LINK_PROTECTION)
98 return (a->protected_resource->link->link_id.s_addr
99 - b->protected_resource->link->link_id.s_addr);
100
101 if (a->protected_resource->type == OSPF_TI_LFA_NODE_PROTECTION
102 && b->protected_resource->type == OSPF_TI_LFA_NODE_PROTECTION)
103 return (a->protected_resource->router_id.s_addr
104 - b->protected_resource->router_id.s_addr);
105
106 /* This should not happen */
107 return 0;
108 }
109
110 int q_spaces_compare_func(const struct q_space *a, const struct q_space *b)
111 {
112 return (a->root->id.s_addr - b->root->id.s_addr);
113 }
114
115 DECLARE_RBTREE_UNIQ(p_spaces, struct p_space, p_spaces_item,
116 p_spaces_compare_func);
117
118 void ospf_process_refresh_data(struct ospf *ospf, bool reset)
119 {
120 struct vrf *vrf = vrf_lookup_by_id(ospf->vrf_id);
121 struct in_addr router_id, router_id_old;
122 struct ospf_interface *oi;
123 struct interface *ifp;
124 struct listnode *node, *nnode;
125 struct ospf_area *area;
126 bool rid_change = false;
127
128 if (!ospf->oi_running) {
129 if (IS_DEBUG_OSPF_EVENT)
130 zlog_debug(
131 "Router ospf not configured -- Router-ID update postponed");
132 return;
133 }
134
135 if (IS_DEBUG_OSPF_EVENT)
136 zlog_debug("Router-ID[OLD:%pI4]: Update",
137 &ospf->router_id);
138
139 router_id_old = ospf->router_id;
140
141 /* Select the router ID based on these priorities:
142 1. Statically assigned router ID is always the first choice.
143 2. If there is no statically assigned router ID, then try to stick
144 with the most recent value, since changing router ID's is very
145 disruptive.
146 3. Last choice: just go with whatever the zebra daemon recommends.
147 */
148 if (ospf->router_id_static.s_addr != INADDR_ANY)
149 router_id = ospf->router_id_static;
150 else if (ospf->router_id.s_addr != INADDR_ANY)
151 router_id = ospf->router_id;
152 else
153 router_id = ospf->router_id_zebra;
154
155 if (IS_DEBUG_OSPF_EVENT)
156 zlog_debug("Router-ID[OLD:%pI4]: Update to %pI4",
157 &ospf->router_id, &router_id);
158
159 rid_change = !(IPV4_ADDR_SAME(&router_id_old, &router_id));
160 if (rid_change || (reset)) {
161 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
162 /* Some nbrs are identified by router_id, these needs
163 * to be rebuilt. Possible optimization would be to do
164 * oi->nbr_self->router_id = router_id for
165 * !(virtual | ptop) links
166 */
167 ospf_nbr_self_reset(oi, router_id);
168
169 /*
170 * If the old router id was not set, but now it
171 * is and the interface is operative and the
172 * state is ISM_Down we should kick the state
173 * machine as that we processed the interfaces
174 * based upon the network statement( or intf config )
175 * but could not start it at that time.
176 */
177 if (if_is_operative(oi->ifp) && oi->state == ISM_Down
178 && router_id_old.s_addr == INADDR_ANY)
179 ospf_if_up(oi);
180 }
181
182 /* Flush (inline) all the self originated LSAs */
183 ospf_flush_self_originated_lsas_now(ospf);
184
185 ospf->router_id = router_id;
186 if (IS_DEBUG_OSPF_EVENT)
187 zlog_debug("Router-ID[NEW:%pI4]: Update",
188 &ospf->router_id);
189
190 /* Flush (inline) all external LSAs which now match the new
191 router-id,
192 need to adjust the OSPF_LSA_SELF flag, so the flush doesn't
193 hit
194 asserts in ospf_refresher_unregister_lsa(). This step is
195 needed
196 because the current frr code does look-up for
197 self-originated LSAs
198 based on the self router-id alone but expects OSPF_LSA_SELF
199 to be
200 properly set */
201 if (ospf->lsdb) {
202 struct route_node *rn;
203 struct ospf_lsa *lsa;
204
205 LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa) {
206 /* AdvRouter and Router ID is the same. */
207 if (IPV4_ADDR_SAME(&lsa->data->adv_router,
208 &ospf->router_id) && rid_change) {
209 SET_FLAG(lsa->flags,
210 OSPF_LSA_SELF_CHECKED);
211 SET_FLAG(lsa->flags, OSPF_LSA_SELF);
212 ospf_lsa_flush_schedule(ospf, lsa);
213 }
214 /* The above flush will send immediately
215 * So discard the LSA to originate new
216 */
217 ospf_discard_from_db(ospf, ospf->lsdb, lsa);
218 }
219
220 LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
221 ospf_discard_from_db(ospf, ospf->lsdb, lsa);
222
223 ospf_lsdb_delete_all(ospf->lsdb);
224 }
225
226 /* Since the LSAs are deleted, need reset the aggr flag */
227 ospf_unset_all_aggr_flag(ospf);
228
229 /* Delete the LSDB */
230 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area))
231 ospf_area_lsdb_discard_delete(area);
232
233 /* update router-lsa's for each area */
234 ospf_router_lsa_update(ospf);
235
236 /* update ospf_interface's */
237 FOR_ALL_INTERFACES (vrf, ifp) {
238 if (reset)
239 ospf_if_reset(ifp);
240 else
241 ospf_if_update(ospf, ifp);
242 }
243
244 ospf_external_lsa_rid_change(ospf);
245
246 #ifdef SUPPORT_OSPF_API
247 ospf_apiserver_clients_notify_router_id_change(router_id);
248 #endif
249 }
250
251 ospf->inst_shutdown = 0;
252 }
253
254 void ospf_router_id_update(struct ospf *ospf)
255 {
256 ospf_process_refresh_data(ospf, false);
257 }
258
259 void ospf_process_reset(struct ospf *ospf)
260 {
261 ospf_process_refresh_data(ospf, true);
262 }
263
264 void ospf_neighbor_reset(struct ospf *ospf, struct in_addr nbr_id,
265 const char *nbr_str)
266 {
267 struct route_node *rn;
268 struct ospf_neighbor *nbr;
269 struct ospf_interface *oi;
270 struct listnode *node;
271
272 /* Clear only a particular nbr with nbr router id as nbr_id */
273 if (nbr_str != NULL) {
274 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
275 nbr = ospf_nbr_lookup_by_routerid(oi->nbrs, &nbr_id);
276 if (nbr)
277 OSPF_NSM_EVENT_EXECUTE(nbr, NSM_KillNbr);
278 }
279 return;
280 }
281
282 /* send Neighbor event KillNbr to all associated neighbors. */
283 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
284 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
285 nbr = rn->info;
286 if (nbr && (nbr != oi->nbr_self))
287 OSPF_NSM_EVENT_EXECUTE(nbr, NSM_KillNbr);
288 }
289 }
290 }
291
292 /* For OSPF area sort by area id. */
293 static int ospf_area_id_cmp(struct ospf_area *a1, struct ospf_area *a2)
294 {
295 if (ntohl(a1->area_id.s_addr) > ntohl(a2->area_id.s_addr))
296 return 1;
297 if (ntohl(a1->area_id.s_addr) < ntohl(a2->area_id.s_addr))
298 return -1;
299 return 0;
300 }
301
302 static void ospf_add(struct ospf *ospf)
303 {
304 listnode_add(om->ospf, ospf);
305 }
306
307 static void ospf_delete(struct ospf *ospf)
308 {
309 listnode_delete(om->ospf, ospf);
310 }
311
312 struct ospf *ospf_new_alloc(unsigned short instance, const char *name)
313 {
314 int i;
315 struct vrf *vrf = NULL;
316
317 struct ospf *new = XCALLOC(MTYPE_OSPF_TOP, sizeof(struct ospf));
318
319 new->instance = instance;
320 new->router_id.s_addr = htonl(0);
321 new->router_id_static.s_addr = htonl(0);
322
323 vrf = vrf_lookup_by_name(name);
324 if (vrf)
325 new->vrf_id = vrf->vrf_id;
326 else
327 new->vrf_id = VRF_UNKNOWN;
328
329 /* Freed in ospf_finish_final */
330 new->name = XSTRDUP(MTYPE_OSPF_TOP, name);
331 if (IS_DEBUG_OSPF_EVENT)
332 zlog_debug(
333 "%s: Create new ospf instance with vrf_name %s vrf_id %u",
334 __func__, name, new->vrf_id);
335
336 if (vrf)
337 ospf_vrf_link(new, vrf);
338
339 ospf_zebra_vrf_register(new);
340
341 new->abr_type = OSPF_ABR_DEFAULT;
342 new->oiflist = list_new();
343 new->vlinks = list_new();
344 new->areas = list_new();
345 new->areas->cmp = (int (*)(void *, void *))ospf_area_id_cmp;
346 new->networks = route_table_init();
347 new->nbr_nbma = route_table_init();
348
349 new->lsdb = ospf_lsdb_new();
350
351 new->default_originate = DEFAULT_ORIGINATE_NONE;
352
353 new->passive_interface_default = OSPF_IF_ACTIVE;
354
355 new->new_external_route = route_table_init();
356 new->old_external_route = route_table_init();
357 new->external_lsas = route_table_init();
358
359 new->stub_router_startup_time = OSPF_STUB_ROUTER_UNCONFIGURED;
360 new->stub_router_shutdown_time = OSPF_STUB_ROUTER_UNCONFIGURED;
361 new->stub_router_admin_set = OSPF_STUB_ROUTER_ADMINISTRATIVE_UNSET;
362
363 /* Distribute parameter init. */
364 for (i = 0; i <= ZEBRA_ROUTE_MAX; i++) {
365 new->dtag[i] = 0;
366 }
367 new->default_metric = -1;
368 new->ref_bandwidth = OSPF_DEFAULT_REF_BANDWIDTH;
369
370 /* LSA timers */
371 new->min_ls_interval = OSPF_MIN_LS_INTERVAL;
372 new->min_ls_arrival = OSPF_MIN_LS_ARRIVAL;
373
374 /* SPF timer value init. */
375 new->spf_delay = OSPF_SPF_DELAY_DEFAULT;
376 new->spf_holdtime = OSPF_SPF_HOLDTIME_DEFAULT;
377 new->spf_max_holdtime = OSPF_SPF_MAX_HOLDTIME_DEFAULT;
378 new->spf_hold_multiplier = 1;
379
380 /* MaxAge init. */
381 new->maxage_delay = OSPF_LSA_MAXAGE_REMOVE_DELAY_DEFAULT;
382 new->maxage_lsa = route_table_init();
383 new->t_maxage_walker = NULL;
384 thread_add_timer(master, ospf_lsa_maxage_walker, new,
385 OSPF_LSA_MAXAGE_CHECK_INTERVAL, &new->t_maxage_walker);
386
387 /* Max paths initialization */
388 new->max_multipath = MULTIPATH_NUM;
389
390 /* Distance table init. */
391 new->distance_table = route_table_init();
392
393 new->lsa_refresh_queue.index = 0;
394 new->lsa_refresh_interval = OSPF_LSA_REFRESH_INTERVAL_DEFAULT;
395 new->lsa_refresh_timer = OSPF_LS_REFRESH_TIME;
396 new->t_lsa_refresher = NULL;
397 thread_add_timer(master, ospf_lsa_refresh_walker, new,
398 new->lsa_refresh_interval, &new->t_lsa_refresher);
399 new->lsa_refresher_started = monotime(NULL);
400
401 new->ibuf = stream_new(OSPF_MAX_PACKET_SIZE + 1);
402
403 new->t_read = NULL;
404 new->oi_write_q = list_new();
405 new->write_oi_count = OSPF_WRITE_INTERFACE_COUNT_DEFAULT;
406
407 new->proactive_arp = OSPF_PROACTIVE_ARP_DEFAULT;
408
409 ospf_gr_helper_instance_init(new);
410
411 ospf_asbr_external_aggregator_init(new);
412
413 ospf_opaque_type11_lsa_init(new);
414
415 QOBJ_REG(new, ospf);
416
417 new->fd = -1;
418
419 return new;
420 }
421
422 /* Allocate new ospf structure. */
423 static struct ospf *ospf_new(unsigned short instance, const char *name)
424 {
425 struct ospf *new;
426
427 new = ospf_new_alloc(instance, name);
428 ospf_add(new);
429
430 if (new->vrf_id == VRF_UNKNOWN)
431 return new;
432
433 if ((ospf_sock_init(new)) < 0) {
434 flog_err(EC_LIB_SOCKET,
435 "%s: ospf_sock_init is unable to open a socket",
436 __func__);
437 return new;
438 }
439
440 thread_add_read(master, ospf_read, new, new->fd, &new->t_read);
441
442 new->oi_running = 1;
443 ospf_router_id_update(new);
444
445 /*
446 * Read from non-volatile memory whether this instance is performing a
447 * graceful restart or not.
448 */
449 ospf_gr_nvm_read(new);
450
451 return new;
452 }
453
454 struct ospf *ospf_lookup_instance(unsigned short instance)
455 {
456 struct ospf *ospf;
457 struct listnode *node, *nnode;
458
459 if (listcount(om->ospf) == 0)
460 return NULL;
461
462 for (ALL_LIST_ELEMENTS(om->ospf, node, nnode, ospf))
463 if ((ospf->instance == 0 && instance == 0)
464 || (ospf->instance && instance
465 && ospf->instance == instance))
466 return ospf;
467
468 return NULL;
469 }
470
471 static int ospf_is_ready(struct ospf *ospf)
472 {
473 /* OSPF must be on and Router-ID must be configured. */
474 if (!ospf || ospf->router_id.s_addr == INADDR_ANY)
475 return 0;
476
477 return 1;
478 }
479
480 struct ospf *ospf_lookup_by_inst_name(unsigned short instance, const char *name)
481 {
482 struct ospf *ospf = NULL;
483 struct listnode *node, *nnode;
484
485 for (ALL_LIST_ELEMENTS(om->ospf, node, nnode, ospf)) {
486 if ((ospf->instance == instance)
487 && ((ospf->name == NULL && name == NULL)
488 || (ospf->name && name
489 && strcmp(ospf->name, name) == 0)))
490 return ospf;
491 }
492 return NULL;
493 }
494
495 struct ospf *ospf_lookup(unsigned short instance, const char *name)
496 {
497 struct ospf *ospf;
498
499 if (ospf_instance) {
500 ospf = ospf_lookup_instance(instance);
501 } else {
502 ospf = ospf_lookup_by_inst_name(instance, name);
503 }
504
505 return ospf;
506 }
507
508 struct ospf *ospf_get(unsigned short instance, const char *name, bool *created)
509 {
510 struct ospf *ospf;
511
512 ospf = ospf_lookup(instance, name);
513
514 *created = (ospf == NULL);
515 if (ospf == NULL)
516 ospf = ospf_new(instance, name);
517
518 return ospf;
519 }
520
521 struct ospf *ospf_lookup_by_vrf_id(vrf_id_t vrf_id)
522 {
523 struct vrf *vrf = NULL;
524
525 vrf = vrf_lookup_by_id(vrf_id);
526 if (!vrf)
527 return NULL;
528 return (vrf->info) ? (struct ospf *)vrf->info : NULL;
529 }
530
531 uint32_t ospf_count_area_params(struct ospf *ospf)
532 {
533 struct vrf *vrf;
534 struct interface *ifp;
535 uint32_t count = 0;
536
537 if (ospf->vrf_id != VRF_UNKNOWN) {
538 vrf = vrf_lookup_by_id(ospf->vrf_id);
539
540 FOR_ALL_INTERFACES (vrf, ifp) {
541 count += ospf_if_count_area_params(ifp);
542 }
543 }
544
545 return count;
546 }
547
548 /* It should only be used when processing incoming info update from zebra.
549 * Other situations, it is not sufficient to lookup the ospf instance by
550 * vrf_name only without using the instance number.
551 */
552 static struct ospf *ospf_lookup_by_name(const char *vrf_name)
553 {
554 struct ospf *ospf = NULL;
555 struct listnode *node, *nnode;
556
557 for (ALL_LIST_ELEMENTS(om->ospf, node, nnode, ospf))
558 if ((ospf->name == NULL && vrf_name == NULL)
559 || (ospf->name && vrf_name
560 && strcmp(ospf->name, vrf_name) == 0))
561 return ospf;
562 return NULL;
563 }
564
565 /* Handle the second half of deferred shutdown. This is called either
566 * from the deferred-shutdown timer thread, or directly through
567 * ospf_deferred_shutdown_check.
568 *
569 * Function is to cleanup G-R state, if required then call ospf_finish_final
570 * to complete shutdown of this ospf instance. Possibly exit if the
571 * whole process is being shutdown and this was the last OSPF instance.
572 */
573 static void ospf_deferred_shutdown_finish(struct ospf *ospf)
574 {
575 ospf->stub_router_shutdown_time = OSPF_STUB_ROUTER_UNCONFIGURED;
576 THREAD_OFF(ospf->t_deferred_shutdown);
577
578 ospf_finish_final(ospf);
579
580 /* *ospf is now invalid */
581
582 /* ospfd being shut-down? If so, was this the last ospf instance? */
583 if (CHECK_FLAG(om->options, OSPF_MASTER_SHUTDOWN)
584 && (listcount(om->ospf) == 0)) {
585 frr_fini();
586 exit(0);
587 }
588
589 return;
590 }
591
592 /* Timer thread for G-R */
593 static void ospf_deferred_shutdown_timer(struct thread *t)
594 {
595 struct ospf *ospf = THREAD_ARG(t);
596
597 ospf_deferred_shutdown_finish(ospf);
598 }
599
600 /* Check whether deferred-shutdown must be scheduled, otherwise call
601 * down directly into second-half of instance shutdown.
602 */
603 static void ospf_deferred_shutdown_check(struct ospf *ospf)
604 {
605 unsigned long timeout;
606 struct listnode *ln;
607 struct ospf_area *area;
608
609 /* deferred shutdown already running? */
610 if (ospf->t_deferred_shutdown)
611 return;
612
613 /* Should we try push out max-metric LSAs? */
614 if (ospf->stub_router_shutdown_time != OSPF_STUB_ROUTER_UNCONFIGURED) {
615 for (ALL_LIST_ELEMENTS_RO(ospf->areas, ln, area)) {
616 SET_FLAG(area->stub_router_state,
617 OSPF_AREA_ADMIN_STUB_ROUTED);
618
619 if (!CHECK_FLAG(area->stub_router_state,
620 OSPF_AREA_IS_STUB_ROUTED))
621 ospf_router_lsa_update_area(area);
622 }
623 timeout = ospf->stub_router_shutdown_time;
624 } else {
625 /* No timer needed */
626 ospf_deferred_shutdown_finish(ospf);
627 return;
628 }
629
630 OSPF_TIMER_ON(ospf->t_deferred_shutdown, ospf_deferred_shutdown_timer,
631 timeout);
632 return;
633 }
634
635 /* Shut down the entire process */
636 void ospf_terminate(void)
637 {
638 struct ospf *ospf;
639 struct listnode *node, *nnode;
640
641 /* shutdown already in progress */
642 if (CHECK_FLAG(om->options, OSPF_MASTER_SHUTDOWN))
643 return;
644
645 SET_FLAG(om->options, OSPF_MASTER_SHUTDOWN);
646
647 /* Skip some steps if OSPF not actually running */
648 if (listcount(om->ospf) == 0)
649 goto done;
650
651 for (ALL_LIST_ELEMENTS(om->ospf, node, nnode, ospf))
652 ospf_finish(ospf);
653
654 /* Cleanup GR */
655 ospf_gr_helper_stop();
656
657 /* Cleanup route maps */
658 route_map_finish();
659
660 /* reverse prefix_list_init */
661 prefix_list_add_hook(NULL);
662 prefix_list_delete_hook(NULL);
663 prefix_list_reset();
664
665 /* Cleanup vrf info */
666 ospf_vrf_terminate();
667
668 /* Deliberately go back up, hopefully to thread scheduler, as
669 * One or more ospf_finish()'s may have deferred shutdown to a timer
670 * thread
671 */
672 zclient_stop(zclient);
673 zclient_free(zclient);
674
675 done:
676 frr_fini();
677 }
678
679 void ospf_finish(struct ospf *ospf)
680 {
681 /* let deferred shutdown decide */
682 ospf_deferred_shutdown_check(ospf);
683
684 /* if ospf_deferred_shutdown returns, then ospf_finish_final is
685 * deferred to expiry of G-S timer thread. Return back up, hopefully
686 * to thread scheduler.
687 */
688 return;
689 }
690
691 /* Final cleanup of ospf instance */
692 static void ospf_finish_final(struct ospf *ospf)
693 {
694 struct vrf *vrf = vrf_lookup_by_id(ospf->vrf_id);
695 struct route_node *rn;
696 struct ospf_nbr_nbma *nbr_nbma;
697 struct ospf_lsa *lsa;
698 struct ospf_interface *oi;
699 struct ospf_area *area;
700 struct ospf_vl_data *vl_data;
701 struct listnode *node, *nnode;
702 struct ospf_redist *red;
703 int i;
704
705 QOBJ_UNREG(ospf);
706
707 ospf_opaque_type11_lsa_term(ospf);
708
709 ospf_opaque_finish();
710
711 if (!ospf->gr_info.prepare_in_progress)
712 ospf_flush_self_originated_lsas_now(ospf);
713
714 /* Unregister redistribution */
715 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
716 struct list *red_list;
717
718 red_list = ospf->redist[i];
719 if (!red_list)
720 continue;
721
722 for (ALL_LIST_ELEMENTS(red_list, node, nnode, red)) {
723 ospf_redistribute_unset(ospf, i, red->instance);
724 ospf_redist_del(ospf, i, red->instance);
725 }
726 }
727 red = ospf_redist_lookup(ospf, DEFAULT_ROUTE, 0);
728 if (red) {
729 ospf_routemap_unset(red);
730 ospf_redist_del(ospf, DEFAULT_ROUTE, 0);
731 ospf_redistribute_default_set(ospf, DEFAULT_ORIGINATE_NONE, 0, 0);
732 }
733
734 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area))
735 ospf_remove_vls_through_area(ospf, area);
736
737 for (ALL_LIST_ELEMENTS(ospf->vlinks, node, nnode, vl_data))
738 ospf_vl_delete(ospf, vl_data);
739
740 list_delete(&ospf->vlinks);
741
742 /* shutdown LDP-Sync */
743 if (ospf->vrf_id == VRF_DEFAULT)
744 ospf_ldp_sync_gbl_exit(ospf, true);
745
746 /* Reset interface. */
747 for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi))
748 ospf_if_free(oi);
749 list_delete(&ospf->oiflist);
750 ospf->oi_running = 0;
751
752 /* De-Register VRF */
753 ospf_zebra_vrf_deregister(ospf);
754
755 /* Clear static neighbors */
756 for (rn = route_top(ospf->nbr_nbma); rn; rn = route_next(rn))
757 if ((nbr_nbma = rn->info)) {
758 THREAD_OFF(nbr_nbma->t_poll);
759
760 if (nbr_nbma->nbr) {
761 nbr_nbma->nbr->nbr_nbma = NULL;
762 nbr_nbma->nbr = NULL;
763 }
764
765 if (nbr_nbma->oi) {
766 listnode_delete(nbr_nbma->oi->nbr_nbma,
767 nbr_nbma);
768 nbr_nbma->oi = NULL;
769 }
770
771 XFREE(MTYPE_OSPF_NEIGHBOR_STATIC, nbr_nbma);
772 }
773
774 route_table_finish(ospf->nbr_nbma);
775
776 /* Clear networks and Areas. */
777 for (rn = route_top(ospf->networks); rn; rn = route_next(rn)) {
778 struct ospf_network *network;
779
780 if ((network = rn->info) != NULL) {
781 ospf_network_free(ospf, network);
782 rn->info = NULL;
783 route_unlock_node(rn);
784 }
785 }
786 route_table_finish(ospf->networks);
787
788 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
789 listnode_delete(ospf->areas, area);
790 ospf_area_free(area);
791 }
792
793 /* Cancel all timers. */
794 THREAD_OFF(ospf->t_read);
795 THREAD_OFF(ospf->t_write);
796 THREAD_OFF(ospf->t_spf_calc);
797 THREAD_OFF(ospf->t_ase_calc);
798 THREAD_OFF(ospf->t_maxage);
799 THREAD_OFF(ospf->t_maxage_walker);
800 THREAD_OFF(ospf->t_abr_task);
801 THREAD_OFF(ospf->t_asbr_check);
802 THREAD_OFF(ospf->t_asbr_nssa_redist_update);
803 THREAD_OFF(ospf->t_distribute_update);
804 THREAD_OFF(ospf->t_lsa_refresher);
805 THREAD_OFF(ospf->t_opaque_lsa_self);
806 THREAD_OFF(ospf->t_sr_update);
807 THREAD_OFF(ospf->t_default_routemap_timer);
808 THREAD_OFF(ospf->t_external_aggr);
809 THREAD_OFF(ospf->gr_info.t_grace_period);
810
811 LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
812 ospf_discard_from_db(ospf, ospf->lsdb, lsa);
813 LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
814 ospf_discard_from_db(ospf, ospf->lsdb, lsa);
815
816 ospf_lsdb_delete_all(ospf->lsdb);
817 ospf_lsdb_free(ospf->lsdb);
818
819 for (rn = route_top(ospf->maxage_lsa); rn; rn = route_next(rn)) {
820 if ((lsa = rn->info) != NULL) {
821 ospf_lsa_unlock(&lsa);
822 rn->info = NULL;
823 route_unlock_node(rn);
824 }
825 }
826 route_table_finish(ospf->maxage_lsa);
827
828 if (ospf->old_table)
829 ospf_route_table_free(ospf->old_table);
830 if (ospf->new_table) {
831 if (!ospf->gr_info.prepare_in_progress)
832 ospf_route_delete(ospf, ospf->new_table);
833 ospf_route_table_free(ospf->new_table);
834 }
835 if (ospf->old_rtrs)
836 ospf_rtrs_free(ospf->old_rtrs);
837 if (ospf->new_rtrs)
838 ospf_rtrs_free(ospf->new_rtrs);
839 if (ospf->new_external_route) {
840 if (!ospf->gr_info.prepare_in_progress)
841 ospf_route_delete(ospf, ospf->new_external_route);
842 ospf_route_table_free(ospf->new_external_route);
843 }
844 if (ospf->old_external_route) {
845 if (!ospf->gr_info.prepare_in_progress)
846 ospf_route_delete(ospf, ospf->old_external_route);
847 ospf_route_table_free(ospf->old_external_route);
848 }
849 if (ospf->external_lsas) {
850 ospf_ase_external_lsas_finish(ospf->external_lsas);
851 }
852
853 for (i = ZEBRA_ROUTE_SYSTEM; i <= ZEBRA_ROUTE_MAX; i++) {
854 struct list *ext_list;
855 struct ospf_external *ext;
856
857 ext_list = ospf->external[i];
858 if (!ext_list)
859 continue;
860
861 for (ALL_LIST_ELEMENTS(ext_list, node, nnode, ext)) {
862 if (ext->external_info)
863 for (rn = route_top(ext->external_info); rn;
864 rn = route_next(rn)) {
865 if (rn->info == NULL)
866 continue;
867
868 XFREE(MTYPE_OSPF_EXTERNAL_INFO,
869 rn->info);
870 rn->info = NULL;
871 route_unlock_node(rn);
872 }
873
874 ospf_external_del(ospf, i, ext->instance);
875 }
876 }
877
878 ospf_distance_reset(ospf);
879 route_table_finish(ospf->distance_table);
880
881 /* Release extrenal Aggregator table */
882 for (rn = route_top(ospf->rt_aggr_tbl); rn; rn = route_next(rn)) {
883 struct ospf_external_aggr_rt *aggr;
884
885 aggr = rn->info;
886
887 if (aggr) {
888 ospf_external_aggregator_free(aggr);
889 rn->info = NULL;
890 route_unlock_node(rn);
891 }
892 }
893
894 route_table_finish(ospf->rt_aggr_tbl);
895
896
897 list_delete(&ospf->areas);
898 list_delete(&ospf->oi_write_q);
899
900 /* Reset GR helper data structers */
901 ospf_gr_helper_instance_stop(ospf);
902
903 close(ospf->fd);
904 stream_free(ospf->ibuf);
905 ospf->fd = -1;
906 ospf->max_multipath = MULTIPATH_NUM;
907 ospf_delete(ospf);
908
909 if (vrf)
910 ospf_vrf_unlink(ospf, vrf);
911
912 XFREE(MTYPE_OSPF_TOP, ospf->name);
913 XFREE(MTYPE_OSPF_TOP, ospf);
914 }
915
916
917 /* allocate new OSPF Area object */
918 struct ospf_area *ospf_area_new(struct ospf *ospf, struct in_addr area_id)
919 {
920 struct ospf_area *new;
921
922 /* Allocate new config_network. */
923 new = XCALLOC(MTYPE_OSPF_AREA, sizeof(struct ospf_area));
924
925 new->ospf = ospf;
926
927 new->area_id = area_id;
928 new->area_id_fmt = OSPF_AREA_ID_FMT_DOTTEDQUAD;
929
930 new->external_routing = OSPF_AREA_DEFAULT;
931 new->default_cost = 1;
932 new->auth_type = OSPF_AUTH_NULL;
933
934 /* New LSDB init. */
935 new->lsdb = ospf_lsdb_new();
936
937 /* Self-originated LSAs initialize. */
938 new->router_lsa_self = NULL;
939
940 ospf_opaque_type10_lsa_init(new);
941
942 new->oiflist = list_new();
943 new->ranges = route_table_init();
944
945 if (area_id.s_addr == OSPF_AREA_BACKBONE)
946 ospf->backbone = new;
947
948 return new;
949 }
950
951 void ospf_area_lsdb_discard_delete(struct ospf_area *area)
952 {
953 struct route_node *rn;
954 struct ospf_lsa *lsa;
955
956 LSDB_LOOP (ROUTER_LSDB(area), rn, lsa)
957 ospf_discard_from_db(area->ospf, area->lsdb, lsa);
958 LSDB_LOOP (NETWORK_LSDB(area), rn, lsa)
959 ospf_discard_from_db(area->ospf, area->lsdb, lsa);
960 LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
961 ospf_discard_from_db(area->ospf, area->lsdb, lsa);
962 LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
963 ospf_discard_from_db(area->ospf, area->lsdb, lsa);
964
965 LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
966 ospf_discard_from_db(area->ospf, area->lsdb, lsa);
967 LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
968 ospf_discard_from_db(area->ospf, area->lsdb, lsa);
969 LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
970 ospf_discard_from_db(area->ospf, area->lsdb, lsa);
971
972 ospf_lsdb_delete_all(area->lsdb);
973 }
974
975 static void ospf_area_free(struct ospf_area *area)
976 {
977 ospf_opaque_type10_lsa_term(area);
978
979 /* Free LSDBs. */
980 ospf_area_lsdb_discard_delete(area);
981
982 ospf_lsdb_free(area->lsdb);
983
984 ospf_lsa_unlock(&area->router_lsa_self);
985
986 route_table_finish(area->ranges);
987 list_delete(&area->oiflist);
988
989 if (EXPORT_NAME(area))
990 free(EXPORT_NAME(area));
991
992 if (IMPORT_NAME(area))
993 free(IMPORT_NAME(area));
994
995 /* Cancel timer. */
996 THREAD_OFF(area->t_stub_router);
997 THREAD_OFF(area->t_opaque_lsa_self);
998
999 if (OSPF_IS_AREA_BACKBONE(area))
1000 area->ospf->backbone = NULL;
1001
1002 XFREE(MTYPE_OSPF_AREA, area);
1003 }
1004
1005 void ospf_area_check_free(struct ospf *ospf, struct in_addr area_id)
1006 {
1007 struct ospf_area *area;
1008
1009 area = ospf_area_lookup_by_area_id(ospf, area_id);
1010 if (area && listcount(area->oiflist) == 0 && area->ranges->top == NULL
1011 && !ospf_vl_count(ospf, area)
1012 && area->shortcut_configured == OSPF_SHORTCUT_DEFAULT
1013 && area->external_routing == OSPF_AREA_DEFAULT
1014 && area->no_summary == 0 && area->default_cost == 1
1015 && EXPORT_NAME(area) == NULL && IMPORT_NAME(area) == NULL
1016 && area->auth_type == OSPF_AUTH_NULL) {
1017 listnode_delete(ospf->areas, area);
1018 ospf_area_free(area);
1019 }
1020 }
1021
1022 struct ospf_area *ospf_area_get(struct ospf *ospf, struct in_addr area_id)
1023 {
1024 struct ospf_area *area;
1025
1026 area = ospf_area_lookup_by_area_id(ospf, area_id);
1027 if (!area) {
1028 area = ospf_area_new(ospf, area_id);
1029 listnode_add_sort(ospf->areas, area);
1030 ospf_check_abr_status(ospf);
1031 if (ospf->stub_router_admin_set
1032 == OSPF_STUB_ROUTER_ADMINISTRATIVE_SET) {
1033 SET_FLAG(area->stub_router_state,
1034 OSPF_AREA_ADMIN_STUB_ROUTED);
1035 }
1036 }
1037
1038 return area;
1039 }
1040
1041 struct ospf_area *ospf_area_lookup_by_area_id(struct ospf *ospf,
1042 struct in_addr area_id)
1043 {
1044 struct ospf_area *area;
1045 struct listnode *node;
1046
1047 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area))
1048 if (IPV4_ADDR_SAME(&area->area_id, &area_id))
1049 return area;
1050
1051 return NULL;
1052 }
1053
1054 void ospf_area_add_if(struct ospf_area *area, struct ospf_interface *oi)
1055 {
1056 listnode_add(area->oiflist, oi);
1057 }
1058
1059 void ospf_area_del_if(struct ospf_area *area, struct ospf_interface *oi)
1060 {
1061 listnode_delete(area->oiflist, oi);
1062 }
1063
1064
1065 struct ospf_interface *add_ospf_interface(struct connected *co,
1066 struct ospf_area *area)
1067 {
1068 struct ospf_interface *oi;
1069
1070 oi = ospf_if_new(area->ospf, co->ifp, co->address);
1071 oi->connected = co;
1072
1073 oi->area = area;
1074
1075 oi->params = ospf_lookup_if_params(co->ifp, oi->address->u.prefix4);
1076 oi->output_cost = ospf_if_get_output_cost(oi);
1077
1078 /* Relate ospf interface to ospf instance. */
1079 oi->ospf = area->ospf;
1080
1081 /* update network type as interface flag */
1082 /* If network type is specified previously,
1083 skip network type setting. */
1084 oi->type = IF_DEF_PARAMS(co->ifp)->type;
1085 oi->ptp_dmvpn = IF_DEF_PARAMS(co->ifp)->ptp_dmvpn;
1086
1087 /* Add pseudo neighbor. */
1088 ospf_nbr_self_reset(oi, oi->ospf->router_id);
1089
1090 ospf_area_add_if(oi->area, oi);
1091
1092 /* if LDP-IGP Sync is configured globally inherit config */
1093 ospf_ldp_sync_if_init(oi);
1094
1095 /*
1096 * if router_id is not configured, don't bring up
1097 * interfaces.
1098 * ospf_router_id_update() will call ospf_if_update
1099 * whenever r-id is configured instead.
1100 */
1101 if ((area->ospf->router_id.s_addr != INADDR_ANY)
1102 && if_is_operative(co->ifp))
1103 ospf_if_up(oi);
1104
1105 return oi;
1106 }
1107
1108 static void update_redistributed(struct ospf *ospf, int add_to_ospf)
1109 {
1110 struct route_node *rn;
1111 struct external_info *ei;
1112 struct ospf_external *ext;
1113
1114 if (ospf_is_type_redistributed(ospf, ZEBRA_ROUTE_CONNECT, 0)) {
1115 ext = ospf_external_lookup(ospf, ZEBRA_ROUTE_CONNECT, 0);
1116 if ((ext) && EXTERNAL_INFO(ext)) {
1117 for (rn = route_top(EXTERNAL_INFO(ext)); rn;
1118 rn = route_next(rn)) {
1119 ei = rn->info;
1120 if (ei == NULL)
1121 continue;
1122
1123 if (add_to_ospf) {
1124 if (ospf_external_info_find_lsa(ospf,
1125 &ei->p))
1126 if (!ospf_redistribute_check(
1127 ospf, ei, NULL))
1128 ospf_external_lsa_flush(
1129 ospf, ei->type,
1130 &ei->p,
1131 ei->ifindex /*, ei->nexthop */);
1132 } else {
1133 if (!ospf_external_info_find_lsa(
1134 ospf, &ei->p))
1135 if (ospf_redistribute_check(
1136 ospf, ei, NULL))
1137 ospf_external_lsa_originate(
1138 ospf, ei);
1139 }
1140 }
1141 }
1142 }
1143 }
1144
1145 /* Config network statement related functions. */
1146 static struct ospf_network *ospf_network_new(struct in_addr area_id)
1147 {
1148 struct ospf_network *new;
1149 new = XCALLOC(MTYPE_OSPF_NETWORK, sizeof(struct ospf_network));
1150
1151 new->area_id = area_id;
1152 new->area_id_fmt = OSPF_AREA_ID_FMT_DOTTEDQUAD;
1153
1154 return new;
1155 }
1156
1157 static void ospf_network_free(struct ospf *ospf, struct ospf_network *network)
1158 {
1159 ospf_area_check_free(ospf, network->area_id);
1160 ospf_schedule_abr_task(ospf);
1161 XFREE(MTYPE_OSPF_NETWORK, network);
1162 }
1163
1164 int ospf_network_set(struct ospf *ospf, struct prefix_ipv4 *p,
1165 struct in_addr area_id, int df)
1166 {
1167 struct ospf_network *network;
1168 struct ospf_area *area;
1169 struct route_node *rn;
1170
1171 rn = route_node_get(ospf->networks, (struct prefix *)p);
1172 if (rn->info) {
1173 network = rn->info;
1174 route_unlock_node(rn);
1175
1176 if (IPV4_ADDR_SAME(&area_id, &network->area_id)) {
1177 return 1;
1178 } else {
1179 /* There is already same network statement. */
1180 return 0;
1181 }
1182 }
1183
1184 rn->info = network = ospf_network_new(area_id);
1185 network->area_id_fmt = df;
1186 area = ospf_area_get(ospf, area_id);
1187 ospf_area_display_format_set(ospf, area, df);
1188
1189 /* Run network config now. */
1190 ospf_network_run((struct prefix *)p, area);
1191
1192 /* Update connected redistribute. */
1193 update_redistributed(ospf, 1); /* interfaces possibly added */
1194
1195 ospf_area_check_free(ospf, area_id);
1196
1197 return 1;
1198 }
1199
1200 int ospf_network_unset(struct ospf *ospf, struct prefix_ipv4 *p,
1201 struct in_addr area_id)
1202 {
1203 struct route_node *rn;
1204 struct ospf_network *network;
1205 struct listnode *node, *nnode;
1206 struct ospf_interface *oi;
1207
1208 rn = route_node_lookup(ospf->networks, (struct prefix *)p);
1209 if (rn == NULL)
1210 return 0;
1211
1212 network = rn->info;
1213 route_unlock_node(rn);
1214 if (!IPV4_ADDR_SAME(&area_id, &network->area_id))
1215 return 0;
1216
1217 ospf_network_free(ospf, rn->info);
1218 rn->info = NULL;
1219 route_unlock_node(rn); /* initial reference */
1220
1221 /* Find interfaces that are not configured already. */
1222 for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi)) {
1223
1224 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
1225 continue;
1226
1227 ospf_network_run_subnet(ospf, oi->connected, NULL, NULL);
1228 }
1229
1230 /* Update connected redistribute. */
1231 update_redistributed(ospf, 0); /* interfaces possibly removed */
1232 ospf_area_check_free(ospf, area_id);
1233
1234 return 1;
1235 }
1236
1237 /* Ensure there's an OSPF instance, as "ip ospf area" enabled OSPF means
1238 * there might not be any 'router ospf' config.
1239 *
1240 * Otherwise, doesn't do anything different to ospf_if_update for now
1241 */
1242 void ospf_interface_area_set(struct ospf *ospf, struct interface *ifp)
1243 {
1244 if (!ospf)
1245 return;
1246
1247 ospf_if_update(ospf, ifp);
1248 /* if_update does a update_redistributed */
1249
1250 return;
1251 }
1252
1253 void ospf_interface_area_unset(struct ospf *ospf, struct interface *ifp)
1254 {
1255 struct route_node *rn_oi;
1256
1257 if (!ospf)
1258 return; /* Ospf not ready yet */
1259
1260 /* Find interfaces that may need to be removed. */
1261 for (rn_oi = route_top(IF_OIFS(ifp)); rn_oi;
1262 rn_oi = route_next(rn_oi)) {
1263 struct ospf_interface *oi = NULL;
1264
1265 if ((oi = rn_oi->info) == NULL)
1266 continue;
1267
1268 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
1269 continue;
1270
1271 ospf_network_run_subnet(ospf, oi->connected, NULL, NULL);
1272 }
1273
1274 /* Update connected redistribute. */
1275 update_redistributed(ospf, 0); /* interfaces possibly removed */
1276 }
1277
1278 /* Check whether interface matches given network
1279 * returns: 1, true. 0, false
1280 */
1281 static int ospf_network_match_iface(const struct connected *co,
1282 const struct prefix *net)
1283 {
1284 /* new approach: more elegant and conceptually clean */
1285 return prefix_match_network_statement(net, CONNECTED_PREFIX(co));
1286 }
1287
1288 static void ospf_update_interface_area(struct connected *co,
1289 struct ospf_area *area)
1290 {
1291 struct ospf_interface *oi = ospf_if_table_lookup(co->ifp, co->address);
1292
1293 /* nothing to be done case */
1294 if (oi && oi->area == area) {
1295 return;
1296 }
1297
1298 if (oi)
1299 ospf_if_free(oi);
1300
1301 add_ospf_interface(co, area);
1302 }
1303
1304 /* Run OSPF for the given subnet, taking into account the following
1305 * possible sources of area configuration, in the given order of preference:
1306 *
1307 * - Whether there is interface+address specific area configuration
1308 * - Whether there is a default area for the interface
1309 * - Whether there is an area given as a parameter.
1310 * - If no specific network prefix/area is supplied, whether there's
1311 * a matching network configured.
1312 */
1313 static void ospf_network_run_subnet(struct ospf *ospf, struct connected *co,
1314 struct prefix *p,
1315 struct ospf_area *given_area)
1316 {
1317 struct ospf_interface *oi;
1318 struct ospf_if_params *params;
1319 struct ospf_area *area = NULL;
1320 struct route_node *rn;
1321 int configed = 0;
1322
1323 if (CHECK_FLAG(co->flags, ZEBRA_IFA_SECONDARY))
1324 return;
1325
1326 if (co->address->family != AF_INET)
1327 return;
1328
1329 /* Try determine the appropriate area for this interface + address
1330 * Start by checking interface config
1331 */
1332 params = ospf_lookup_if_params(co->ifp, co->address->u.prefix4);
1333 if (params && OSPF_IF_PARAM_CONFIGURED(params, if_area))
1334 area = ospf_area_get(ospf, params->if_area);
1335 else {
1336 params = IF_DEF_PARAMS(co->ifp);
1337 if (OSPF_IF_PARAM_CONFIGURED(params, if_area))
1338 area = ospf_area_get(ospf, params->if_area);
1339 }
1340
1341 /* If we've found an interface and/or addr specific area, then we're
1342 * done
1343 */
1344 if (area) {
1345 ospf_update_interface_area(co, area);
1346 return;
1347 }
1348
1349 /* Otherwise, only remaining possibility is a matching network statement
1350 */
1351 if (p) {
1352 assert(given_area != NULL);
1353
1354 /* Which either was supplied as a parameter.. (e.g. cause a new
1355 * network/area was just added)..
1356 */
1357 if (p->family == co->address->family
1358 && ospf_network_match_iface(co, p))
1359 ospf_update_interface_area(co, given_area);
1360
1361 return;
1362 }
1363
1364 /* Else we have to search the existing network/area config to see
1365 * if any match..
1366 */
1367 for (rn = route_top(ospf->networks); rn; rn = route_next(rn))
1368 if (rn->info != NULL && ospf_network_match_iface(co, &rn->p)) {
1369 struct ospf_network *network =
1370 (struct ospf_network *)rn->info;
1371 area = ospf_area_get(ospf, network->area_id);
1372 ospf_update_interface_area(co, area);
1373 configed = 1;
1374 }
1375
1376 /* If the subnet isn't in any area, deconfigure */
1377 if (!configed && (oi = ospf_if_table_lookup(co->ifp, co->address)))
1378 ospf_if_free(oi);
1379 }
1380
1381 static void ospf_network_run_interface(struct ospf *ospf, struct interface *ifp,
1382 struct prefix *p,
1383 struct ospf_area *given_area)
1384 {
1385 struct listnode *cnode;
1386 struct connected *co;
1387
1388 if (memcmp(ifp->name, "VLINK", 5) == 0)
1389 return;
1390
1391 /* Network prefix without area is nonsensical */
1392 if (p)
1393 assert(given_area != NULL);
1394
1395 /* if interface prefix is match specified prefix,
1396 then create socket and join multicast group. */
1397 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, co))
1398 ospf_network_run_subnet(ospf, co, p, given_area);
1399 }
1400
1401 static void ospf_network_run(struct prefix *p, struct ospf_area *area)
1402 {
1403 struct vrf *vrf = vrf_lookup_by_id(area->ospf->vrf_id);
1404 struct interface *ifp;
1405
1406 /* Schedule Router ID Update. */
1407 if (area->ospf->router_id.s_addr == INADDR_ANY)
1408 ospf_router_id_update(area->ospf);
1409
1410 /* Get target interface. */
1411 FOR_ALL_INTERFACES (vrf, ifp)
1412 ospf_network_run_interface(area->ospf, ifp, p, area);
1413 }
1414
1415 void ospf_ls_upd_queue_empty(struct ospf_interface *oi)
1416 {
1417 struct route_node *rn;
1418 struct listnode *node, *nnode;
1419 struct list *lst;
1420 struct ospf_lsa *lsa;
1421
1422 /* empty ls update queue */
1423 for (rn = route_top(oi->ls_upd_queue); rn; rn = route_next(rn))
1424 if ((lst = (struct list *)rn->info)) {
1425 for (ALL_LIST_ELEMENTS(lst, node, nnode, lsa))
1426 ospf_lsa_unlock(&lsa); /* oi->ls_upd_queue */
1427 list_delete(&lst);
1428 rn->info = NULL;
1429 }
1430
1431 /* remove update event */
1432 THREAD_OFF(oi->t_ls_upd_event);
1433 }
1434
1435 void ospf_if_update(struct ospf *ospf, struct interface *ifp)
1436 {
1437
1438 if (!ospf)
1439 return;
1440
1441 if (IS_DEBUG_OSPF_EVENT)
1442 zlog_debug(
1443 "%s: interface %s vrf %s(%u) ospf vrf %s vrf_id %u router_id %pI4",
1444 __func__, ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
1445 ospf_vrf_id_to_name(ospf->vrf_id), ospf->vrf_id,
1446 &ospf->router_id);
1447
1448 /* OSPF must be ready. */
1449 if (!ospf_is_ready(ospf))
1450 return;
1451
1452 ospf_network_run_interface(ospf, ifp, NULL, NULL);
1453
1454 /* Update connected redistribute. */
1455 update_redistributed(ospf, 1);
1456
1457 }
1458
1459 void ospf_remove_vls_through_area(struct ospf *ospf, struct ospf_area *area)
1460 {
1461 struct listnode *node, *nnode;
1462 struct ospf_vl_data *vl_data;
1463
1464 for (ALL_LIST_ELEMENTS(ospf->vlinks, node, nnode, vl_data))
1465 if (IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
1466 ospf_vl_delete(ospf, vl_data);
1467 }
1468
1469
1470 static const struct message ospf_area_type_msg[] = {
1471 {OSPF_AREA_DEFAULT, "Default"},
1472 {OSPF_AREA_STUB, "Stub"},
1473 {OSPF_AREA_NSSA, "NSSA"},
1474 {0}};
1475
1476 static void ospf_area_type_set(struct ospf_area *area, int type)
1477 {
1478 struct listnode *node;
1479 struct ospf_interface *oi;
1480
1481 if (area->external_routing == type) {
1482 if (IS_DEBUG_OSPF_EVENT)
1483 zlog_debug("Area[%pI4]: Types are the same, ignored.",
1484 &area->area_id);
1485 return;
1486 }
1487
1488 area->external_routing = type;
1489
1490 if (IS_DEBUG_OSPF_EVENT)
1491 zlog_debug("Area[%pI4]: Configured as %s",
1492 &area->area_id,
1493 lookup_msg(ospf_area_type_msg, type, NULL));
1494
1495 switch (area->external_routing) {
1496 case OSPF_AREA_DEFAULT:
1497 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi))
1498 if (oi->nbr_self != NULL) {
1499 UNSET_FLAG(oi->nbr_self->options,
1500 OSPF_OPTION_NP);
1501 SET_FLAG(oi->nbr_self->options, OSPF_OPTION_E);
1502 }
1503 break;
1504 case OSPF_AREA_STUB:
1505 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi))
1506 if (oi->nbr_self != NULL) {
1507 if (IS_DEBUG_OSPF_EVENT)
1508 zlog_debug(
1509 "setting options on %s accordingly",
1510 IF_NAME(oi));
1511 UNSET_FLAG(oi->nbr_self->options,
1512 OSPF_OPTION_NP);
1513 UNSET_FLAG(oi->nbr_self->options,
1514 OSPF_OPTION_E);
1515 if (IS_DEBUG_OSPF_EVENT)
1516 zlog_debug("options set on %s: %x",
1517 IF_NAME(oi), OPTIONS(oi));
1518 }
1519 break;
1520 case OSPF_AREA_NSSA:
1521 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi))
1522 if (oi->nbr_self != NULL) {
1523 zlog_debug(
1524 "setting nssa options on %s accordingly",
1525 IF_NAME(oi));
1526 UNSET_FLAG(oi->nbr_self->options,
1527 OSPF_OPTION_E);
1528 SET_FLAG(oi->nbr_self->options, OSPF_OPTION_NP);
1529 zlog_debug("options set on %s: %x", IF_NAME(oi),
1530 OPTIONS(oi));
1531 }
1532 break;
1533 default:
1534 break;
1535 }
1536
1537 ospf_router_lsa_update_area(area);
1538 ospf_schedule_abr_task(area->ospf);
1539 }
1540
1541 int ospf_area_shortcut_set(struct ospf *ospf, struct ospf_area *area, int mode)
1542 {
1543 if (area->shortcut_configured == mode)
1544 return 0;
1545
1546 area->shortcut_configured = mode;
1547 ospf_router_lsa_update_area(area);
1548 ospf_schedule_abr_task(ospf);
1549
1550 ospf_area_check_free(ospf, area->area_id);
1551
1552 return 1;
1553 }
1554
1555 int ospf_area_shortcut_unset(struct ospf *ospf, struct ospf_area *area)
1556 {
1557 area->shortcut_configured = OSPF_SHORTCUT_DEFAULT;
1558 ospf_router_lsa_update_area(area);
1559 ospf_area_check_free(ospf, area->area_id);
1560 ospf_schedule_abr_task(ospf);
1561
1562 return 1;
1563 }
1564
1565 static int ospf_area_vlink_count(struct ospf *ospf, struct ospf_area *area)
1566 {
1567 struct ospf_vl_data *vl;
1568 struct listnode *node;
1569 int count = 0;
1570
1571 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl))
1572 if (IPV4_ADDR_SAME(&vl->vl_area_id, &area->area_id))
1573 count++;
1574
1575 return count;
1576 }
1577
1578 int ospf_area_display_format_set(struct ospf *ospf, struct ospf_area *area,
1579 int df)
1580 {
1581 area->area_id_fmt = df;
1582
1583 return 1;
1584 }
1585
1586 int ospf_area_stub_set(struct ospf *ospf, struct in_addr area_id)
1587 {
1588 struct ospf_area *area;
1589
1590 area = ospf_area_get(ospf, area_id);
1591 if (ospf_area_vlink_count(ospf, area))
1592 return 0;
1593
1594 if (area->external_routing != OSPF_AREA_STUB)
1595 ospf_area_type_set(area, OSPF_AREA_STUB);
1596
1597 return 1;
1598 }
1599
1600 int ospf_area_stub_unset(struct ospf *ospf, struct in_addr area_id)
1601 {
1602 struct ospf_area *area;
1603
1604 area = ospf_area_lookup_by_area_id(ospf, area_id);
1605 if (area == NULL)
1606 return 1;
1607
1608 if (area->external_routing == OSPF_AREA_STUB)
1609 ospf_area_type_set(area, OSPF_AREA_DEFAULT);
1610
1611 ospf_area_check_free(ospf, area_id);
1612
1613 return 1;
1614 }
1615
1616 int ospf_area_no_summary_set(struct ospf *ospf, struct in_addr area_id)
1617 {
1618 struct ospf_area *area;
1619
1620 area = ospf_area_get(ospf, area_id);
1621 area->no_summary = 1;
1622
1623 return 1;
1624 }
1625
1626 int ospf_area_no_summary_unset(struct ospf *ospf, struct in_addr area_id)
1627 {
1628 struct ospf_area *area;
1629
1630 area = ospf_area_lookup_by_area_id(ospf, area_id);
1631 if (area == NULL)
1632 return 0;
1633
1634 area->no_summary = 0;
1635 ospf_area_check_free(ospf, area_id);
1636
1637 return 1;
1638 }
1639
1640 int ospf_area_nssa_no_summary_set(struct ospf *ospf, struct in_addr area_id)
1641 {
1642 struct ospf_area *area;
1643
1644 area = ospf_area_get(ospf, area_id);
1645 if (ospf_area_vlink_count(ospf, area))
1646 return 0;
1647
1648 if (area->external_routing != OSPF_AREA_NSSA) {
1649 ospf_area_type_set(area, OSPF_AREA_NSSA);
1650 ospf->anyNSSA++;
1651 area->NSSATranslatorRole = OSPF_NSSA_ROLE_CANDIDATE;
1652 }
1653
1654 ospf_area_no_summary_set(ospf, area_id);
1655
1656 return 1;
1657 }
1658
1659 int ospf_area_nssa_set(struct ospf *ospf, struct in_addr area_id)
1660 {
1661 struct ospf_area *area;
1662
1663 area = ospf_area_get(ospf, area_id);
1664 if (ospf_area_vlink_count(ospf, area))
1665 return 0;
1666
1667 if (area->external_routing != OSPF_AREA_NSSA) {
1668 ospf_area_type_set(area, OSPF_AREA_NSSA);
1669 ospf->anyNSSA++;
1670
1671 /* set NSSA area defaults */
1672 area->no_summary = 0;
1673 area->suppress_fa = 0;
1674 area->NSSATranslatorRole = OSPF_NSSA_ROLE_CANDIDATE;
1675 area->NSSATranslatorState = OSPF_NSSA_TRANSLATE_DISABLED;
1676 area->NSSATranslatorStabilityInterval =
1677 OSPF_NSSA_TRANS_STABLE_DEFAULT;
1678 }
1679 return 1;
1680 }
1681
1682 int ospf_area_nssa_unset(struct ospf *ospf, struct in_addr area_id, int argc)
1683 {
1684 struct ospf_area *area;
1685
1686 area = ospf_area_lookup_by_area_id(ospf, area_id);
1687 if (area == NULL)
1688 return 0;
1689
1690 /* argc < 5 -> 'no area x nssa' */
1691 if (argc < 5 && area->external_routing == OSPF_AREA_NSSA) {
1692 ospf->anyNSSA--;
1693 /* set NSSA area defaults */
1694 area->no_summary = 0;
1695 area->suppress_fa = 0;
1696 area->NSSATranslatorRole = OSPF_NSSA_ROLE_CANDIDATE;
1697 area->NSSATranslatorState = OSPF_NSSA_TRANSLATE_DISABLED;
1698 area->NSSATranslatorStabilityInterval =
1699 OSPF_NSSA_TRANS_STABLE_DEFAULT;
1700 ospf_area_type_set(area, OSPF_AREA_DEFAULT);
1701 } else {
1702 ospf_area_nssa_translator_role_set(ospf, area_id,
1703 OSPF_NSSA_ROLE_CANDIDATE);
1704 }
1705
1706 ospf_area_check_free(ospf, area_id);
1707
1708 return 1;
1709 }
1710
1711 int ospf_area_nssa_suppress_fa_set(struct ospf *ospf, struct in_addr area_id)
1712 {
1713 struct ospf_area *area;
1714
1715 area = ospf_area_lookup_by_area_id(ospf, area_id);
1716 if (area == NULL)
1717 return 0;
1718
1719 area->suppress_fa = 1;
1720
1721 return 1;
1722 }
1723
1724 int ospf_area_nssa_suppress_fa_unset(struct ospf *ospf, struct in_addr area_id)
1725 {
1726 struct ospf_area *area;
1727
1728 area = ospf_area_lookup_by_area_id(ospf, area_id);
1729 if (area == NULL)
1730 return 0;
1731
1732 area->suppress_fa = 0;
1733
1734 return 1;
1735 }
1736
1737 int ospf_area_nssa_translator_role_set(struct ospf *ospf,
1738 struct in_addr area_id, int role)
1739 {
1740 struct ospf_area *area;
1741
1742 area = ospf_area_lookup_by_area_id(ospf, area_id);
1743 if (area == NULL)
1744 return 0;
1745
1746 if (role != area->NSSATranslatorRole) {
1747 if ((area->NSSATranslatorRole == OSPF_NSSA_ROLE_ALWAYS)
1748 || (role == OSPF_NSSA_ROLE_ALWAYS)) {
1749 /* RFC 3101 3.1
1750 * if new role is OSPF_NSSA_ROLE_ALWAYS we need to set
1751 * Nt bit, if the role was OSPF_NSSA_ROLE_ALWAYS we need
1752 * to clear Nt bit
1753 */
1754 area->NSSATranslatorRole = role;
1755 ospf_router_lsa_update_area(area);
1756 } else
1757 area->NSSATranslatorRole = role;
1758 }
1759
1760 return 1;
1761 }
1762
1763 int ospf_area_export_list_set(struct ospf *ospf, struct ospf_area *area,
1764 const char *list_name)
1765 {
1766 struct access_list *list;
1767 list = access_list_lookup(AFI_IP, list_name);
1768
1769 EXPORT_LIST(area) = list;
1770
1771 if (EXPORT_NAME(area))
1772 free(EXPORT_NAME(area));
1773
1774 EXPORT_NAME(area) = strdup(list_name);
1775 ospf_schedule_abr_task(ospf);
1776
1777 return 1;
1778 }
1779
1780 int ospf_area_export_list_unset(struct ospf *ospf, struct ospf_area *area)
1781 {
1782
1783 EXPORT_LIST(area) = 0;
1784
1785 if (EXPORT_NAME(area))
1786 free(EXPORT_NAME(area));
1787
1788 EXPORT_NAME(area) = NULL;
1789
1790 ospf_area_check_free(ospf, area->area_id);
1791
1792 ospf_schedule_abr_task(ospf);
1793
1794 return 1;
1795 }
1796
1797 int ospf_area_import_list_set(struct ospf *ospf, struct ospf_area *area,
1798 const char *name)
1799 {
1800 struct access_list *list;
1801 list = access_list_lookup(AFI_IP, name);
1802
1803 IMPORT_LIST(area) = list;
1804
1805 if (IMPORT_NAME(area))
1806 free(IMPORT_NAME(area));
1807
1808 IMPORT_NAME(area) = strdup(name);
1809 ospf_schedule_abr_task(ospf);
1810
1811 return 1;
1812 }
1813
1814 int ospf_area_import_list_unset(struct ospf *ospf, struct ospf_area *area)
1815 {
1816 IMPORT_LIST(area) = 0;
1817
1818 if (IMPORT_NAME(area))
1819 free(IMPORT_NAME(area));
1820
1821 IMPORT_NAME(area) = NULL;
1822 ospf_area_check_free(ospf, area->area_id);
1823
1824 ospf_schedule_abr_task(ospf);
1825
1826 return 1;
1827 }
1828
1829 int ospf_timers_refresh_set(struct ospf *ospf, int interval)
1830 {
1831 int time_left;
1832
1833 if (ospf->lsa_refresh_interval == interval)
1834 return 1;
1835
1836 time_left = ospf->lsa_refresh_interval
1837 - (monotime(NULL) - ospf->lsa_refresher_started);
1838
1839 if (time_left > interval) {
1840 THREAD_OFF(ospf->t_lsa_refresher);
1841 thread_add_timer(master, ospf_lsa_refresh_walker, ospf,
1842 interval, &ospf->t_lsa_refresher);
1843 }
1844 ospf->lsa_refresh_interval = interval;
1845
1846 return 1;
1847 }
1848
1849 int ospf_timers_refresh_unset(struct ospf *ospf)
1850 {
1851 int time_left;
1852
1853 time_left = ospf->lsa_refresh_interval
1854 - (monotime(NULL) - ospf->lsa_refresher_started);
1855
1856 if (time_left > OSPF_LSA_REFRESH_INTERVAL_DEFAULT) {
1857 THREAD_OFF(ospf->t_lsa_refresher);
1858 ospf->t_lsa_refresher = NULL;
1859 thread_add_timer(master, ospf_lsa_refresh_walker, ospf,
1860 OSPF_LSA_REFRESH_INTERVAL_DEFAULT,
1861 &ospf->t_lsa_refresher);
1862 }
1863
1864 ospf->lsa_refresh_interval = OSPF_LSA_REFRESH_INTERVAL_DEFAULT;
1865
1866 return 1;
1867 }
1868
1869
1870 static struct ospf_nbr_nbma *ospf_nbr_nbma_new(void)
1871 {
1872 struct ospf_nbr_nbma *nbr_nbma;
1873
1874 nbr_nbma = XCALLOC(MTYPE_OSPF_NEIGHBOR_STATIC,
1875 sizeof(struct ospf_nbr_nbma));
1876
1877 nbr_nbma->priority = OSPF_NEIGHBOR_PRIORITY_DEFAULT;
1878 nbr_nbma->v_poll = OSPF_POLL_INTERVAL_DEFAULT;
1879
1880 return nbr_nbma;
1881 }
1882
1883 static void ospf_nbr_nbma_free(struct ospf_nbr_nbma *nbr_nbma)
1884 {
1885 XFREE(MTYPE_OSPF_NEIGHBOR_STATIC, nbr_nbma);
1886 }
1887
1888 static void ospf_nbr_nbma_delete(struct ospf *ospf,
1889 struct ospf_nbr_nbma *nbr_nbma)
1890 {
1891 struct route_node *rn;
1892 struct prefix_ipv4 p;
1893
1894 p.family = AF_INET;
1895 p.prefix = nbr_nbma->addr;
1896 p.prefixlen = IPV4_MAX_BITLEN;
1897
1898 rn = route_node_lookup(ospf->nbr_nbma, (struct prefix *)&p);
1899 if (rn) {
1900 ospf_nbr_nbma_free(rn->info);
1901 rn->info = NULL;
1902 route_unlock_node(rn);
1903 route_unlock_node(rn);
1904 }
1905 }
1906
1907 static void ospf_nbr_nbma_down(struct ospf_nbr_nbma *nbr_nbma)
1908 {
1909 THREAD_OFF(nbr_nbma->t_poll);
1910
1911 if (nbr_nbma->nbr) {
1912 nbr_nbma->nbr->nbr_nbma = NULL;
1913 OSPF_NSM_EVENT_EXECUTE(nbr_nbma->nbr, NSM_KillNbr);
1914 }
1915
1916 if (nbr_nbma->oi)
1917 listnode_delete(nbr_nbma->oi->nbr_nbma, nbr_nbma);
1918 }
1919
1920 static void ospf_nbr_nbma_add(struct ospf_nbr_nbma *nbr_nbma,
1921 struct ospf_interface *oi)
1922 {
1923 struct ospf_neighbor *nbr;
1924 struct route_node *rn;
1925 struct prefix p;
1926
1927 if (oi->type != OSPF_IFTYPE_NBMA)
1928 return;
1929
1930 if (nbr_nbma->nbr != NULL)
1931 return;
1932
1933 if (IPV4_ADDR_SAME(&oi->nbr_self->address.u.prefix4, &nbr_nbma->addr))
1934 return;
1935
1936 nbr_nbma->oi = oi;
1937 listnode_add(oi->nbr_nbma, nbr_nbma);
1938
1939 /* Get neighbor information from table. */
1940 p.family = AF_INET;
1941 p.prefixlen = IPV4_MAX_BITLEN;
1942 p.u.prefix4 = nbr_nbma->addr;
1943
1944 rn = route_node_get(oi->nbrs, &p);
1945 if (rn->info) {
1946 nbr = rn->info;
1947 nbr->nbr_nbma = nbr_nbma;
1948 nbr_nbma->nbr = nbr;
1949
1950 route_unlock_node(rn);
1951 } else {
1952 nbr = rn->info = ospf_nbr_new(oi);
1953 nbr->state = NSM_Down;
1954 nbr->src = nbr_nbma->addr;
1955 nbr->nbr_nbma = nbr_nbma;
1956 nbr->priority = nbr_nbma->priority;
1957 nbr->address = p;
1958
1959 nbr_nbma->nbr = nbr;
1960
1961 /* Configure BFD if interface has it. */
1962 ospf_neighbor_bfd_apply(nbr);
1963
1964 OSPF_NSM_EVENT_EXECUTE(nbr, NSM_Start);
1965 }
1966 }
1967
1968 void ospf_nbr_nbma_if_update(struct ospf *ospf, struct ospf_interface *oi)
1969 {
1970 struct ospf_nbr_nbma *nbr_nbma;
1971 struct route_node *rn;
1972 struct prefix_ipv4 p;
1973
1974 if (oi->type != OSPF_IFTYPE_NBMA)
1975 return;
1976
1977 for (rn = route_top(ospf->nbr_nbma); rn; rn = route_next(rn))
1978 if ((nbr_nbma = rn->info))
1979 if (nbr_nbma->oi == NULL && nbr_nbma->nbr == NULL) {
1980 p.family = AF_INET;
1981 p.prefix = nbr_nbma->addr;
1982 p.prefixlen = IPV4_MAX_BITLEN;
1983
1984 if (prefix_match(oi->address,
1985 (struct prefix *)&p))
1986 ospf_nbr_nbma_add(nbr_nbma, oi);
1987 }
1988 }
1989
1990 struct ospf_nbr_nbma *ospf_nbr_nbma_lookup(struct ospf *ospf,
1991 struct in_addr nbr_addr)
1992 {
1993 struct route_node *rn;
1994 struct prefix_ipv4 p;
1995
1996 p.family = AF_INET;
1997 p.prefix = nbr_addr;
1998 p.prefixlen = IPV4_MAX_BITLEN;
1999
2000 rn = route_node_lookup(ospf->nbr_nbma, (struct prefix *)&p);
2001 if (rn) {
2002 route_unlock_node(rn);
2003 return rn->info;
2004 }
2005 return NULL;
2006 }
2007
2008 int ospf_nbr_nbma_set(struct ospf *ospf, struct in_addr nbr_addr)
2009 {
2010 struct ospf_nbr_nbma *nbr_nbma;
2011 struct ospf_interface *oi;
2012 struct prefix_ipv4 p;
2013 struct route_node *rn;
2014 struct listnode *node;
2015
2016 nbr_nbma = ospf_nbr_nbma_lookup(ospf, nbr_addr);
2017 if (nbr_nbma)
2018 return 0;
2019
2020 nbr_nbma = ospf_nbr_nbma_new();
2021 nbr_nbma->addr = nbr_addr;
2022
2023 p.family = AF_INET;
2024 p.prefix = nbr_addr;
2025 p.prefixlen = IPV4_MAX_BITLEN;
2026
2027 rn = route_node_get(ospf->nbr_nbma, (struct prefix *)&p);
2028 if (rn->info)
2029 route_unlock_node(rn);
2030 rn->info = nbr_nbma;
2031
2032 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
2033 if (oi->type == OSPF_IFTYPE_NBMA)
2034 if (prefix_match(oi->address, (struct prefix *)&p)) {
2035 ospf_nbr_nbma_add(nbr_nbma, oi);
2036 break;
2037 }
2038 }
2039
2040 return 1;
2041 }
2042
2043 int ospf_nbr_nbma_unset(struct ospf *ospf, struct in_addr nbr_addr)
2044 {
2045 struct ospf_nbr_nbma *nbr_nbma;
2046
2047 nbr_nbma = ospf_nbr_nbma_lookup(ospf, nbr_addr);
2048 if (nbr_nbma == NULL)
2049 return 0;
2050
2051 ospf_nbr_nbma_down(nbr_nbma);
2052 ospf_nbr_nbma_delete(ospf, nbr_nbma);
2053
2054 return 1;
2055 }
2056
2057 int ospf_nbr_nbma_priority_set(struct ospf *ospf, struct in_addr nbr_addr,
2058 uint8_t priority)
2059 {
2060 struct ospf_nbr_nbma *nbr_nbma;
2061
2062 nbr_nbma = ospf_nbr_nbma_lookup(ospf, nbr_addr);
2063 if (nbr_nbma == NULL)
2064 return 0;
2065
2066 if (nbr_nbma->priority != priority)
2067 nbr_nbma->priority = priority;
2068
2069 return 1;
2070 }
2071
2072 int ospf_nbr_nbma_priority_unset(struct ospf *ospf, struct in_addr nbr_addr)
2073 {
2074 struct ospf_nbr_nbma *nbr_nbma;
2075
2076 nbr_nbma = ospf_nbr_nbma_lookup(ospf, nbr_addr);
2077 if (nbr_nbma == NULL)
2078 return 0;
2079
2080 if (nbr_nbma != OSPF_NEIGHBOR_PRIORITY_DEFAULT)
2081 nbr_nbma->priority = OSPF_NEIGHBOR_PRIORITY_DEFAULT;
2082
2083 return 1;
2084 }
2085
2086 int ospf_nbr_nbma_poll_interval_set(struct ospf *ospf, struct in_addr nbr_addr,
2087 unsigned int interval)
2088 {
2089 struct ospf_nbr_nbma *nbr_nbma;
2090
2091 nbr_nbma = ospf_nbr_nbma_lookup(ospf, nbr_addr);
2092 if (nbr_nbma == NULL)
2093 return 0;
2094
2095 if (nbr_nbma->v_poll != interval) {
2096 nbr_nbma->v_poll = interval;
2097 if (nbr_nbma->oi && ospf_if_is_up(nbr_nbma->oi)) {
2098 THREAD_OFF(nbr_nbma->t_poll);
2099 OSPF_POLL_TIMER_ON(nbr_nbma->t_poll, ospf_poll_timer,
2100 nbr_nbma->v_poll);
2101 }
2102 }
2103
2104 return 1;
2105 }
2106
2107 int ospf_nbr_nbma_poll_interval_unset(struct ospf *ospf, struct in_addr addr)
2108 {
2109 struct ospf_nbr_nbma *nbr_nbma;
2110
2111 nbr_nbma = ospf_nbr_nbma_lookup(ospf, addr);
2112 if (nbr_nbma == NULL)
2113 return 0;
2114
2115 if (nbr_nbma->v_poll != OSPF_POLL_INTERVAL_DEFAULT)
2116 nbr_nbma->v_poll = OSPF_POLL_INTERVAL_DEFAULT;
2117
2118 return 1;
2119 }
2120
2121 void ospf_master_init(struct thread_master *master)
2122 {
2123 memset(&ospf_master, 0, sizeof(ospf_master));
2124
2125 om = &ospf_master;
2126 om->ospf = list_new();
2127 om->master = master;
2128 }
2129
2130 /* Link OSPF instance to VRF. */
2131 void ospf_vrf_link(struct ospf *ospf, struct vrf *vrf)
2132 {
2133 ospf->vrf_id = vrf->vrf_id;
2134 if (vrf->info != (void *)ospf)
2135 vrf->info = (void *)ospf;
2136 }
2137
2138 /* Unlink OSPF instance from VRF. */
2139 void ospf_vrf_unlink(struct ospf *ospf, struct vrf *vrf)
2140 {
2141 if (vrf->info == (void *)ospf)
2142 vrf->info = NULL;
2143 ospf->vrf_id = VRF_UNKNOWN;
2144 }
2145
2146 /* This is hook function for vrf create called as part of vrf_init */
2147 static int ospf_vrf_new(struct vrf *vrf)
2148 {
2149 if (IS_DEBUG_OSPF_EVENT)
2150 zlog_debug("%s: VRF Created: %s(%u)", __func__, vrf->name,
2151 vrf->vrf_id);
2152
2153 return 0;
2154 }
2155
2156 /* This is hook function for vrf delete call as part of vrf_init */
2157 static int ospf_vrf_delete(struct vrf *vrf)
2158 {
2159 if (IS_DEBUG_OSPF_EVENT)
2160 zlog_debug("%s: VRF Deletion: %s(%u)", __func__, vrf->name,
2161 vrf->vrf_id);
2162
2163 return 0;
2164 }
2165
2166 static void ospf_set_redist_vrf_bitmaps(struct ospf *ospf, bool set)
2167 {
2168 int type;
2169 struct list *red_list;
2170
2171 for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
2172 red_list = ospf->redist[type];
2173 if (!red_list)
2174 continue;
2175 if (IS_DEBUG_OSPF_EVENT)
2176 zlog_debug(
2177 "%s: setting redist vrf %d bitmap for type %d",
2178 __func__, ospf->vrf_id, type);
2179 if (set)
2180 vrf_bitmap_set(zclient->redist[AFI_IP][type],
2181 ospf->vrf_id);
2182 else
2183 vrf_bitmap_unset(zclient->redist[AFI_IP][type],
2184 ospf->vrf_id);
2185 }
2186
2187 red_list = ospf->redist[DEFAULT_ROUTE];
2188 if (red_list) {
2189 if (set)
2190 vrf_bitmap_set(zclient->default_information[AFI_IP],
2191 ospf->vrf_id);
2192 else
2193 vrf_bitmap_unset(zclient->default_information[AFI_IP],
2194 ospf->vrf_id);
2195 }
2196 }
2197
2198 /* Enable OSPF VRF instance */
2199 static int ospf_vrf_enable(struct vrf *vrf)
2200 {
2201 struct ospf *ospf = NULL;
2202 vrf_id_t old_vrf_id;
2203 int ret = 0;
2204
2205 if (IS_DEBUG_OSPF_EVENT)
2206 zlog_debug("%s: VRF %s id %u enabled", __func__, vrf->name,
2207 vrf->vrf_id);
2208
2209 ospf = ospf_lookup_by_name(vrf->name);
2210 if (ospf) {
2211 old_vrf_id = ospf->vrf_id;
2212 /* We have instance configured, link to VRF and make it "up". */
2213 ospf_vrf_link(ospf, vrf);
2214 if (IS_DEBUG_OSPF_EVENT)
2215 zlog_debug(
2216 "%s: ospf linked to vrf %s vrf_id %u (old id %u)",
2217 __func__, vrf->name, ospf->vrf_id, old_vrf_id);
2218
2219 if (old_vrf_id != ospf->vrf_id) {
2220 ospf_set_redist_vrf_bitmaps(ospf, true);
2221
2222 /* start zebra redist to us for new vrf */
2223 ospf_zebra_vrf_register(ospf);
2224
2225 ret = ospf_sock_init(ospf);
2226 if (ret < 0 || ospf->fd <= 0)
2227 return 0;
2228 thread_add_read(master, ospf_read, ospf, ospf->fd,
2229 &ospf->t_read);
2230 ospf->oi_running = 1;
2231 ospf_router_id_update(ospf);
2232 }
2233 }
2234
2235 return 0;
2236 }
2237
2238 /* Disable OSPF VRF instance */
2239 static int ospf_vrf_disable(struct vrf *vrf)
2240 {
2241 struct ospf *ospf = NULL;
2242 vrf_id_t old_vrf_id = VRF_UNKNOWN;
2243
2244 if (vrf->vrf_id == VRF_DEFAULT)
2245 return 0;
2246
2247 if (IS_DEBUG_OSPF_EVENT)
2248 zlog_debug("%s: VRF %s id %d disabled.", __func__, vrf->name,
2249 vrf->vrf_id);
2250
2251 ospf = ospf_lookup_by_name(vrf->name);
2252 if (ospf) {
2253 old_vrf_id = ospf->vrf_id;
2254
2255 ospf_zebra_vrf_deregister(ospf);
2256
2257 ospf_set_redist_vrf_bitmaps(ospf, false);
2258
2259 /* We have instance configured, unlink
2260 * from VRF and make it "down".
2261 */
2262 ospf_vrf_unlink(ospf, vrf);
2263 ospf->oi_running = 0;
2264 if (IS_DEBUG_OSPF_EVENT)
2265 zlog_debug("%s: ospf old_vrf_id %d unlinked", __func__,
2266 old_vrf_id);
2267 THREAD_OFF(ospf->t_read);
2268 close(ospf->fd);
2269 ospf->fd = -1;
2270 }
2271
2272 /* Note: This is a callback, the VRF will be deleted by the caller. */
2273 return 0;
2274 }
2275
2276 void ospf_vrf_init(void)
2277 {
2278 vrf_init(ospf_vrf_new, ospf_vrf_enable, ospf_vrf_disable,
2279 ospf_vrf_delete);
2280 }
2281
2282 void ospf_vrf_terminate(void)
2283 {
2284 vrf_terminate();
2285 }
2286
2287 const char *ospf_vrf_id_to_name(vrf_id_t vrf_id)
2288 {
2289 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
2290
2291 return vrf ? vrf->name : "NIL";
2292 }
2293
2294 const char *ospf_get_name(const struct ospf *ospf)
2295 {
2296 if (ospf->name)
2297 return ospf->name;
2298 else
2299 return VRF_DEFAULT_NAME;
2300 }