]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospfd.c
*: remove THREAD_ON macros, add nullity check
[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
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
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 "plist.h"
36 #include "sockopt.h"
37 #include "bfd.h"
38 #include "defaults.h"
39
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"
57
58
59 DEFINE_QOBJ_TYPE(ospf)
60
61 /* OSPF process wide configuration. */
62 static struct ospf_master ospf_master;
63
64 /* OSPF process wide configuration pointer to export. */
65 struct ospf_master *om;
66
67 extern struct zclient *zclient;
68 extern struct in_addr router_id_zebra;
69
70
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 prefix *, struct ospf_area *,
76 struct interface *);
77 static int ospf_network_match_iface (const struct connected *,
78 const struct prefix *);
79 static void ospf_finish_final (struct ospf *);
80
81 #define OSPF_EXTERNAL_LSA_ORIGINATE_DELAY 1
82
83 void
84 ospf_router_id_update (struct ospf *ospf)
85 {
86 struct in_addr router_id, router_id_old;
87 struct ospf_interface *oi;
88 struct interface *ifp;
89 struct listnode *node;
90 int type;
91
92 if (!ospf->oi_running)
93 {
94 if (IS_DEBUG_OSPF_EVENT)
95 zlog_debug ("Router ospf not configured -- Router-ID update postponed");
96 return;
97 }
98
99 if (IS_DEBUG_OSPF_EVENT)
100 zlog_debug ("Router-ID[OLD:%s]: Update", inet_ntoa (ospf->router_id));
101
102 router_id_old = ospf->router_id;
103
104 /* Select the router ID based on these priorities:
105 1. Statically assigned router ID is always the first choice.
106 2. If there is no statically assigned router ID, then try to stick
107 with the most recent value, since changing router ID's is very
108 disruptive.
109 3. Last choice: just go with whatever the zebra daemon recommends.
110 */
111 if (ospf->router_id_static.s_addr != 0)
112 router_id = ospf->router_id_static;
113 else if (ospf->router_id.s_addr != 0)
114 router_id = ospf->router_id;
115 else
116 router_id = router_id_zebra;
117
118
119 if (!IPV4_ADDR_SAME (&router_id_old, &router_id))
120 {
121
122 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
123 {
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
128 */
129 ospf_nbr_self_reset (oi, router_id);
130 }
131
132 /* If AS-external-LSA is queued, then flush those LSAs. */
133 if (router_id_old.s_addr == 0 && ospf->external_origin)
134 {
135 /* Originate each redistributed external route. */
136 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
137 if (ospf->external_origin & (1 << type))
138 thread_add_event(master, ospf_external_lsa_originate_timer,
139 ospf, type, NULL);
140 /* Originate Deafult. */
141 if (ospf->external_origin & (1 << ZEBRA_ROUTE_MAX))
142 thread_add_event(master, ospf_default_originate_timer, ospf, 0,
143 NULL);
144
145 ospf->external_origin = 0;
146 }
147
148 /* Flush (inline) all external LSAs based on the OSPF_LSA_SELF flag */
149 if (ospf->lsdb)
150 {
151 struct route_node *rn;
152 struct ospf_lsa *lsa;
153
154 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
155 if (IS_LSA_SELF(lsa))
156 ospf_lsa_flush_schedule(ospf, lsa);
157 }
158
159 ospf->router_id = router_id;
160 if (IS_DEBUG_OSPF_EVENT)
161 zlog_debug ("Router-ID[NEW:%s]: Update", inet_ntoa (ospf->router_id));
162
163 /* Flush (inline) all external LSAs which now match the new router-id,
164 need to adjust the OSPF_LSA_SELF flag, so the flush doesnt hit
165 asserts in ospf_refresher_unregister_lsa(). This step is needed
166 because the current quagga code does look-up for self-originated LSAs
167 based on the self router-id alone but expects OSPF_LSA_SELF to be
168 properly set */
169 if (ospf->lsdb)
170 {
171 struct route_node *rn;
172 struct ospf_lsa *lsa;
173
174 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
175 {
176 /* AdvRouter and Router ID is the same. */
177 if (IPV4_ADDR_SAME (&lsa->data->adv_router, &ospf->router_id))
178 {
179 SET_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED);
180 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
181 ospf_lsa_flush_schedule(ospf, lsa);
182 }
183 }
184 }
185
186 /* Originate each redistributed external route. */
187 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
188 thread_add_event(master, ospf_external_lsa_originate_timer, ospf,
189 type, NULL);
190 thread_add_event(master, ospf_default_originate_timer, ospf, 0, NULL);
191
192 /* update router-lsa's for each area */
193 ospf_router_lsa_update (ospf);
194
195 /* update ospf_interface's */
196 for (ALL_LIST_ELEMENTS_RO (om->iflist, node, ifp))
197 ospf_if_update (ospf, ifp);
198 }
199 }
200
201 /* For OSPF area sort by area id. */
202 static int
203 ospf_area_id_cmp (struct ospf_area *a1, struct ospf_area *a2)
204 {
205 if (ntohl (a1->area_id.s_addr) > ntohl (a2->area_id.s_addr))
206 return 1;
207 if (ntohl (a1->area_id.s_addr) < ntohl (a2->area_id.s_addr))
208 return -1;
209 return 0;
210 }
211
212 /* Allocate new ospf structure. */
213 static struct ospf *
214 ospf_new (u_short instance)
215 {
216 int i;
217
218 struct ospf *new = XCALLOC (MTYPE_OSPF_TOP, sizeof (struct ospf));
219
220 new->instance = instance;
221 new->router_id.s_addr = htonl (0);
222 new->router_id_static.s_addr = htonl (0);
223
224 new->abr_type = OSPF_ABR_DEFAULT;
225 new->oiflist = list_new ();
226 new->vlinks = list_new ();
227 new->areas = list_new ();
228 new->areas->cmp = (int (*)(void *, void *)) ospf_area_id_cmp;
229 new->networks = route_table_init ();
230 new->nbr_nbma = route_table_init ();
231
232 new->lsdb = ospf_lsdb_new ();
233
234 new->default_originate = DEFAULT_ORIGINATE_NONE;
235
236 new->passive_interface_default = OSPF_IF_ACTIVE;
237
238 new->new_external_route = route_table_init ();
239 new->old_external_route = route_table_init ();
240 new->external_lsas = route_table_init ();
241
242 new->stub_router_startup_time = OSPF_STUB_ROUTER_UNCONFIGURED;
243 new->stub_router_shutdown_time = OSPF_STUB_ROUTER_UNCONFIGURED;
244 new->stub_router_admin_set = OSPF_STUB_ROUTER_ADMINISTRATIVE_UNSET;
245
246 /* Distribute parameter init. */
247 for (i = 0; i <= ZEBRA_ROUTE_MAX; i++)
248 {
249 new->dtag[i] = 0;
250 }
251 new->default_metric = -1;
252 new->ref_bandwidth = OSPF_DEFAULT_REF_BANDWIDTH;
253
254 /* LSA timers */
255 new->min_ls_interval = OSPF_MIN_LS_INTERVAL;
256 new->min_ls_arrival = OSPF_MIN_LS_ARRIVAL;
257
258 /* SPF timer value init. */
259 new->spf_delay = OSPF_SPF_DELAY_DEFAULT;
260 new->spf_holdtime = OSPF_SPF_HOLDTIME_DEFAULT;
261 new->spf_max_holdtime = OSPF_SPF_MAX_HOLDTIME_DEFAULT;
262 new->spf_hold_multiplier = 1;
263
264 /* MaxAge init. */
265 new->maxage_delay = OSPF_LSA_MAXAGE_REMOVE_DELAY_DEFAULT;
266 new->maxage_lsa = route_table_init();
267 new->t_maxage_walker =
268 thread_add_timer(master, ospf_lsa_maxage_walker, new,
269 OSPF_LSA_MAXAGE_CHECK_INTERVAL, NULL);
270
271 /* Distance table init. */
272 new->distance_table = route_table_init ();
273
274 new->lsa_refresh_queue.index = 0;
275 new->lsa_refresh_interval = OSPF_LSA_REFRESH_INTERVAL_DEFAULT;
276 new->t_lsa_refresher = thread_add_timer(master, ospf_lsa_refresh_walker,
277 new, new->lsa_refresh_interval,
278 NULL);
279 new->lsa_refresher_started = monotime(NULL);
280
281 if ((new->fd = ospf_sock_init()) < 0)
282 {
283 zlog_err("ospf_new: fatal error: ospf_sock_init was unable to open "
284 "a socket");
285 exit(1);
286 }
287 if ((new->ibuf = stream_new(OSPF_MAX_PACKET_SIZE+1)) == NULL)
288 {
289 zlog_err("ospf_new: fatal error: stream_new(%u) failed allocating ibuf",
290 OSPF_MAX_PACKET_SIZE+1);
291 exit(1);
292 }
293 new->t_read = thread_add_read(master, ospf_read, new, new->fd, NULL);
294 new->oi_write_q = list_new ();
295 new->write_oi_count = OSPF_WRITE_INTERFACE_COUNT_DEFAULT;
296
297 /* Enable "log-adjacency-changes" */
298 #if DFLT_OSPF_LOG_ADJACENCY_CHANGES
299 SET_FLAG(new->config, OSPF_LOG_ADJACENCY_CHANGES);
300 #endif
301
302 QOBJ_REG (new, ospf);
303
304 return new;
305 }
306
307 struct ospf *
308 ospf_lookup ()
309 {
310 if (listcount (om->ospf) == 0)
311 return NULL;
312
313 return listgetdata ((struct listnode *)listhead (om->ospf));
314 }
315
316 struct ospf *
317 ospf_lookup_instance (u_short instance)
318 {
319 struct ospf *ospf;
320 struct listnode *node, *nnode;
321
322 if (listcount (om->ospf) == 0)
323 return NULL;
324
325 for (ALL_LIST_ELEMENTS (om->ospf, node, nnode, ospf))
326 if ((ospf->instance == 0 && instance == 0)
327 || (ospf->instance && instance && ospf->instance == instance))
328 return ospf;
329
330 return NULL;
331 }
332
333 static void
334 ospf_add (struct ospf *ospf)
335 {
336 listnode_add (om->ospf, ospf);
337 }
338
339 static void
340 ospf_delete (struct ospf *ospf)
341 {
342 listnode_delete (om->ospf, ospf);
343 }
344
345 struct ospf *
346 ospf_get ()
347 {
348 struct ospf *ospf;
349
350 ospf = ospf_lookup ();
351 if (ospf == NULL)
352 {
353 ospf = ospf_new (0);
354 ospf_add (ospf);
355
356 if (ospf->router_id_static.s_addr == 0)
357 ospf_router_id_update (ospf);
358
359 ospf_opaque_type11_lsa_init (ospf);
360 }
361
362 return ospf;
363 }
364
365 struct ospf *
366 ospf_get_instance (u_short instance)
367 {
368 struct ospf *ospf;
369
370 ospf = ospf_lookup_instance (instance);
371 if (ospf == NULL)
372 {
373 ospf = ospf_new (instance);
374 ospf_add (ospf);
375
376 if (ospf->router_id_static.s_addr == 0)
377 ospf_router_id_update (ospf);
378
379 ospf_opaque_type11_lsa_init (ospf);
380 }
381
382 return ospf;
383 }
384
385 /* Handle the second half of deferred shutdown. This is called either
386 * from the deferred-shutdown timer thread, or directly through
387 * ospf_deferred_shutdown_check.
388 *
389 * Function is to cleanup G-R state, if required then call ospf_finish_final
390 * to complete shutdown of this ospf instance. Possibly exit if the
391 * whole process is being shutdown and this was the last OSPF instance.
392 */
393 static void
394 ospf_deferred_shutdown_finish (struct ospf *ospf)
395 {
396 ospf->stub_router_shutdown_time = OSPF_STUB_ROUTER_UNCONFIGURED;
397 OSPF_TIMER_OFF (ospf->t_deferred_shutdown);
398
399 ospf_finish_final (ospf);
400
401 /* *ospf is now invalid */
402
403 /* ospfd being shut-down? If so, was this the last ospf instance? */
404 if (CHECK_FLAG (om->options, OSPF_MASTER_SHUTDOWN)
405 && (listcount (om->ospf) == 0))
406 {
407 exit (0);
408 }
409
410 return;
411 }
412
413 /* Timer thread for G-R */
414 static int
415 ospf_deferred_shutdown_timer (struct thread *t)
416 {
417 struct ospf *ospf = THREAD_ARG(t);
418
419 ospf_deferred_shutdown_finish (ospf);
420
421 return 0;
422 }
423
424 /* Check whether deferred-shutdown must be scheduled, otherwise call
425 * down directly into second-half of instance shutdown.
426 */
427 static void
428 ospf_deferred_shutdown_check (struct ospf *ospf)
429 {
430 unsigned long timeout;
431 struct listnode *ln;
432 struct ospf_area *area;
433
434 /* deferred shutdown already running? */
435 if (ospf->t_deferred_shutdown)
436 return;
437
438 /* Should we try push out max-metric LSAs? */
439 if (ospf->stub_router_shutdown_time != OSPF_STUB_ROUTER_UNCONFIGURED)
440 {
441 for (ALL_LIST_ELEMENTS_RO (ospf->areas, ln, area))
442 {
443 SET_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED);
444
445 if (!CHECK_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED))
446 ospf_router_lsa_update_area (area);
447 }
448 timeout = ospf->stub_router_shutdown_time;
449 }
450 else
451 {
452 /* No timer needed */
453 ospf_deferred_shutdown_finish (ospf);
454 return;
455 }
456
457 OSPF_TIMER_ON (ospf->t_deferred_shutdown, ospf_deferred_shutdown_timer,
458 timeout);
459 return;
460 }
461
462 /* Shut down the entire process */
463 void
464 ospf_terminate (void)
465 {
466 struct ospf *ospf;
467 struct listnode *node, *nnode;
468
469 /* shutdown already in progress */
470 if (CHECK_FLAG (om->options, OSPF_MASTER_SHUTDOWN))
471 return;
472
473 SET_FLAG (om->options, OSPF_MASTER_SHUTDOWN);
474
475 /* exit immediately if OSPF not actually running */
476 if (listcount(om->ospf) == 0)
477 exit(0);
478
479 bfd_gbl_exit();
480 for (ALL_LIST_ELEMENTS (om->ospf, node, nnode, ospf))
481 ospf_finish (ospf);
482
483 /* Deliberately go back up, hopefully to thread scheduler, as
484 * One or more ospf_finish()'s may have deferred shutdown to a timer
485 * thread
486 */
487 }
488
489 void
490 ospf_finish (struct ospf *ospf)
491 {
492 /* let deferred shutdown decide */
493 ospf_deferred_shutdown_check (ospf);
494
495 /* if ospf_deferred_shutdown returns, then ospf_finish_final is
496 * deferred to expiry of G-S timer thread. Return back up, hopefully
497 * to thread scheduler.
498 */
499 return;
500 }
501
502 /* Final cleanup of ospf instance */
503 static void
504 ospf_finish_final (struct ospf *ospf)
505 {
506 struct route_node *rn;
507 struct ospf_nbr_nbma *nbr_nbma;
508 struct ospf_lsa *lsa;
509 struct interface *ifp;
510 struct ospf_interface *oi;
511 struct ospf_area *area;
512 struct ospf_vl_data *vl_data;
513 struct listnode *node, *nnode;
514 int i;
515 u_short instance = 0;
516
517 QOBJ_UNREG (ospf);
518
519 ospf_opaque_type11_lsa_term (ospf);
520
521 /* be nice if this worked, but it doesn't */
522 /*ospf_flush_self_originated_lsas_now (ospf);*/
523
524 /* Unregister redistribution */
525 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
526 {
527 struct list *red_list;
528 struct ospf_redist *red;
529
530 red_list = ospf->redist[i];
531 if (!red_list)
532 continue;
533
534 for (ALL_LIST_ELEMENTS(red_list, node, nnode, red))
535 ospf_redistribute_unset (ospf, i, red->instance);
536 }
537 ospf_redistribute_default_unset (ospf);
538
539 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
540 ospf_remove_vls_through_area (ospf, area);
541
542 for (ALL_LIST_ELEMENTS (ospf->vlinks, node, nnode, vl_data))
543 ospf_vl_delete (ospf, vl_data);
544
545 list_delete (ospf->vlinks);
546
547 /* Remove any ospf interface config params */
548 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
549 {
550 struct ospf_if_params *params;
551
552 params = IF_DEF_PARAMS (ifp);
553 if (OSPF_IF_PARAM_CONFIGURED(params, if_area))
554 UNSET_IF_PARAM (params, if_area);
555 }
556
557 /* Reset interface. */
558 for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
559 ospf_if_free (oi);
560
561 /* Clear static neighbors */
562 for (rn = route_top (ospf->nbr_nbma); rn; rn = route_next (rn))
563 if ((nbr_nbma = rn->info))
564 {
565 OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
566
567 if (nbr_nbma->nbr)
568 {
569 nbr_nbma->nbr->nbr_nbma = NULL;
570 nbr_nbma->nbr = NULL;
571 }
572
573 if (nbr_nbma->oi)
574 {
575 listnode_delete (nbr_nbma->oi->nbr_nbma, nbr_nbma);
576 nbr_nbma->oi = NULL;
577 }
578
579 XFREE (MTYPE_OSPF_NEIGHBOR_STATIC, nbr_nbma);
580 }
581
582 route_table_finish (ospf->nbr_nbma);
583
584 /* Clear networks and Areas. */
585 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
586 {
587 struct ospf_network *network;
588
589 if ((network = rn->info) != NULL)
590 {
591 ospf_network_free (ospf, network);
592 rn->info = NULL;
593 route_unlock_node (rn);
594 }
595 }
596
597 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
598 {
599 listnode_delete (ospf->areas, area);
600 ospf_area_free (area);
601 }
602
603 /* Cancel all timers. */
604 OSPF_TIMER_OFF (ospf->t_external_lsa);
605 OSPF_TIMER_OFF (ospf->t_spf_calc);
606 OSPF_TIMER_OFF (ospf->t_ase_calc);
607 OSPF_TIMER_OFF (ospf->t_maxage);
608 OSPF_TIMER_OFF (ospf->t_maxage_walker);
609 OSPF_TIMER_OFF (ospf->t_abr_task);
610 OSPF_TIMER_OFF (ospf->t_asbr_check);
611 OSPF_TIMER_OFF (ospf->t_distribute_update);
612 OSPF_TIMER_OFF (ospf->t_lsa_refresher);
613 OSPF_TIMER_OFF (ospf->t_read);
614 OSPF_TIMER_OFF (ospf->t_write);
615 OSPF_TIMER_OFF (ospf->t_opaque_lsa_self);
616
617 close (ospf->fd);
618 stream_free(ospf->ibuf);
619
620 LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
621 ospf_discard_from_db (ospf, ospf->lsdb, lsa);
622 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
623 ospf_discard_from_db (ospf, ospf->lsdb, lsa);
624
625 ospf_lsdb_delete_all (ospf->lsdb);
626 ospf_lsdb_free (ospf->lsdb);
627
628 for (rn = route_top (ospf->maxage_lsa); rn; rn = route_next (rn))
629 {
630 struct ospf_lsa *lsa;
631
632 if ((lsa = rn->info) != NULL)
633 {
634 ospf_lsa_unlock (&lsa);
635 rn->info = NULL;
636 }
637 route_unlock_node (rn);
638 }
639 route_table_finish (ospf->maxage_lsa);
640
641 if (ospf->old_table)
642 ospf_route_table_free (ospf->old_table);
643 if (ospf->new_table)
644 {
645 ospf_route_delete (ospf->new_table);
646 ospf_route_table_free (ospf->new_table);
647 }
648 if (ospf->old_rtrs)
649 ospf_rtrs_free (ospf->old_rtrs);
650 if (ospf->new_rtrs)
651 ospf_rtrs_free (ospf->new_rtrs);
652 if (ospf->new_external_route)
653 {
654 ospf_route_delete (ospf->new_external_route);
655 ospf_route_table_free (ospf->new_external_route);
656 }
657 if (ospf->old_external_route)
658 {
659 ospf_route_delete (ospf->old_external_route);
660 ospf_route_table_free (ospf->old_external_route);
661 }
662 if (ospf->external_lsas)
663 {
664 ospf_ase_external_lsas_finish (ospf->external_lsas);
665 }
666
667 list_delete (ospf->areas);
668
669 for (i = ZEBRA_ROUTE_SYSTEM; i <= ZEBRA_ROUTE_MAX; i++)
670 {
671 struct list *ext_list;
672 struct listnode *node;
673 struct ospf_external *ext;
674
675 ext_list = om->external[i];
676 if (!ext_list)
677 continue;
678
679 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext))
680 {
681 if (ext->external_info)
682 for (rn = route_top (ext->external_info); rn; rn = route_next (rn))
683 {
684 if (rn->info == NULL)
685 continue;
686
687 XFREE (MTYPE_OSPF_EXTERNAL_INFO, rn->info);
688 rn->info = NULL;
689 route_unlock_node (rn);
690 }
691 }
692 }
693
694 ospf_distance_reset (ospf);
695 route_table_finish (ospf->distance_table);
696
697 if (!CHECK_FLAG (om->options, OSPF_MASTER_SHUTDOWN))
698 instance = ospf->instance;
699
700 ospf_delete (ospf);
701
702 XFREE (MTYPE_OSPF_TOP, ospf);
703
704 if (!CHECK_FLAG (om->options, OSPF_MASTER_SHUTDOWN))
705 ospf_get_instance(instance);
706
707 }
708
709
710 /* allocate new OSPF Area object */
711 static struct ospf_area *
712 ospf_area_new (struct ospf *ospf, struct in_addr area_id)
713 {
714 struct ospf_area *new;
715
716 /* Allocate new config_network. */
717 new = XCALLOC (MTYPE_OSPF_AREA, sizeof (struct ospf_area));
718
719 new->ospf = ospf;
720
721 new->area_id = area_id;
722
723 new->external_routing = OSPF_AREA_DEFAULT;
724 new->default_cost = 1;
725 new->auth_type = OSPF_AUTH_NULL;
726
727 /* New LSDB init. */
728 new->lsdb = ospf_lsdb_new ();
729
730 /* Self-originated LSAs initialize. */
731 new->router_lsa_self = NULL;
732
733 ospf_opaque_type10_lsa_init (new);
734
735 new->oiflist = list_new ();
736 new->ranges = route_table_init ();
737
738 if (area_id.s_addr == OSPF_AREA_BACKBONE)
739 ospf->backbone = new;
740
741 return new;
742 }
743
744 static void
745 ospf_area_free (struct ospf_area *area)
746 {
747 struct route_node *rn;
748 struct ospf_lsa *lsa;
749
750 /* Free LSDBs. */
751 LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
752 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
753 LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
754 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
755 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
756 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
757 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
758 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
759
760 LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
761 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
762 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
763 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
764 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
765 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
766
767 ospf_lsdb_delete_all (area->lsdb);
768 ospf_lsdb_free (area->lsdb);
769
770 ospf_lsa_unlock (&area->router_lsa_self);
771
772 route_table_finish (area->ranges);
773 list_delete (area->oiflist);
774
775 if (EXPORT_NAME (area))
776 free (EXPORT_NAME (area));
777
778 if (IMPORT_NAME (area))
779 free (IMPORT_NAME (area));
780
781 /* Cancel timer. */
782 OSPF_TIMER_OFF (area->t_stub_router);
783 OSPF_TIMER_OFF (area->t_opaque_lsa_self);
784
785 if (OSPF_IS_AREA_BACKBONE (area))
786 area->ospf->backbone = NULL;
787
788 XFREE (MTYPE_OSPF_AREA, area);
789 }
790
791 void
792 ospf_area_check_free (struct ospf *ospf, struct in_addr area_id)
793 {
794 struct ospf_area *area;
795
796 area = ospf_area_lookup_by_area_id (ospf, area_id);
797 if (area &&
798 listcount (area->oiflist) == 0 &&
799 area->ranges->top == NULL &&
800 area->shortcut_configured == OSPF_SHORTCUT_DEFAULT &&
801 area->external_routing == OSPF_AREA_DEFAULT &&
802 area->no_summary == 0 &&
803 area->default_cost == 1 &&
804 EXPORT_NAME (area) == NULL &&
805 IMPORT_NAME (area) == NULL &&
806 area->auth_type == OSPF_AUTH_NULL)
807 {
808 listnode_delete (ospf->areas, area);
809 ospf_area_free (area);
810 }
811 }
812
813 struct ospf_area *
814 ospf_area_get (struct ospf *ospf, struct in_addr area_id, int format)
815 {
816 struct ospf_area *area;
817
818 area = ospf_area_lookup_by_area_id (ospf, area_id);
819 if (!area)
820 {
821 area = ospf_area_new (ospf, area_id);
822 area->format = format;
823 listnode_add_sort (ospf->areas, area);
824 ospf_check_abr_status (ospf);
825 if (ospf->stub_router_admin_set == OSPF_STUB_ROUTER_ADMINISTRATIVE_SET)
826 {
827 SET_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED);
828 }
829 }
830
831 return area;
832 }
833
834 struct ospf_area *
835 ospf_area_lookup_by_area_id (struct ospf *ospf, struct in_addr area_id)
836 {
837 struct ospf_area *area;
838 struct listnode *node;
839
840 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
841 if (IPV4_ADDR_SAME (&area->area_id, &area_id))
842 return area;
843
844 return NULL;
845 }
846
847 void
848 ospf_area_add_if (struct ospf_area *area, struct ospf_interface *oi)
849 {
850 listnode_add (area->oiflist, oi);
851 }
852
853 void
854 ospf_area_del_if (struct ospf_area *area, struct ospf_interface *oi)
855 {
856 listnode_delete (area->oiflist, oi);
857 }
858
859 static struct ospf_interface *
860 add_ospf_interface (struct interface *ifp, struct ospf_area *area,
861 struct connected *co)
862 {
863 struct ospf_interface *oi;
864
865 oi = ospf_if_new (area->ospf, ifp, co->address);
866 oi->connected = co;
867
868 oi->area = area;
869
870 oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4);
871 oi->output_cost = ospf_if_get_output_cost (oi);
872
873 /* Relate ospf interface to ospf instance. */
874 oi->ospf = area->ospf;
875
876 /* update network type as interface flag */
877 /* If network type is specified previously,
878 skip network type setting. */
879 oi->type = IF_DEF_PARAMS (ifp)->type;
880
881 /* Add pseudo neighbor. */
882 ospf_nbr_self_reset (oi, oi->ospf->router_id);
883
884 ospf_area_add_if (oi->area, oi);
885
886 return (oi);
887 }
888
889 static void update_redistributed(struct ospf *ospf, int add_to_ospf)
890 {
891 struct route_node *rn;
892 struct external_info *ei;
893 struct ospf_external *ext;
894
895 if (ospf_is_type_redistributed (ZEBRA_ROUTE_CONNECT, 0))
896 if ((ext = ospf_external_lookup(ZEBRA_ROUTE_CONNECT, 0)) &&
897 EXTERNAL_INFO (ext))
898 {
899 for (rn = route_top (EXTERNAL_INFO (ext));
900 rn; rn = route_next (rn))
901 {
902 if ((ei = rn->info) != NULL)
903 {
904 if (add_to_ospf)
905 {
906 if (ospf_external_info_find_lsa (ospf, &ei->p))
907 if (!ospf_distribute_check_connected (ospf, ei))
908 ospf_external_lsa_flush (ospf, ei->type, &ei->p,
909 ei->ifindex /*, ei->nexthop */);
910 }
911 else
912 {
913 if (!ospf_external_info_find_lsa (ospf, &ei->p))
914 if (ospf_distribute_check_connected (ospf, ei))
915 ospf_external_lsa_originate (ospf, ei);
916 }
917 }
918 }
919 }
920 }
921
922
923 /* Config network statement related functions. */
924 static struct ospf_network *
925 ospf_network_new (struct in_addr area_id, int format)
926 {
927 struct ospf_network *new;
928 new = XCALLOC (MTYPE_OSPF_NETWORK, sizeof (struct ospf_network));
929
930 new->area_id = area_id;
931 new->format = format;
932
933 return new;
934 }
935
936 static void
937 ospf_network_free (struct ospf *ospf, struct ospf_network *network)
938 {
939 ospf_area_check_free (ospf, network->area_id);
940 ospf_schedule_abr_task (ospf);
941 XFREE (MTYPE_OSPF_NETWORK, network);
942 }
943
944 int
945 ospf_network_set (struct ospf *ospf, struct prefix_ipv4 *p,
946 struct in_addr area_id)
947 {
948 struct ospf_network *network;
949 struct ospf_area *area;
950 struct route_node *rn;
951 int ret = OSPF_AREA_ID_FORMAT_ADDRESS;
952
953 rn = route_node_get (ospf->networks, (struct prefix *)p);
954 if (rn->info)
955 {
956 /* There is already same network statement. */
957 route_unlock_node (rn);
958 return 0;
959 }
960
961 rn->info = network = ospf_network_new (area_id, ret);
962 area = ospf_area_get (ospf, area_id, ret);
963
964 /* Run network config now. */
965 ospf_network_run ((struct prefix *)p, area);
966
967 /* Update connected redistribute. */
968 update_redistributed(ospf, 1); /* interfaces possibly added */
969
970 ospf_area_check_free (ospf, area_id);
971
972 return 1;
973 }
974
975 int
976 ospf_network_unset (struct ospf *ospf, struct prefix_ipv4 *p,
977 struct in_addr area_id)
978 {
979 struct route_node *rn;
980 struct ospf_network *network;
981 struct listnode *node, *nnode;
982 struct ospf_interface *oi;
983
984 rn = route_node_lookup (ospf->networks, (struct prefix *)p);
985 if (rn == NULL)
986 return 0;
987
988 network = rn->info;
989 route_unlock_node (rn);
990 if (!IPV4_ADDR_SAME (&area_id, &network->area_id))
991 return 0;
992
993 ospf_network_free (ospf, rn->info);
994 rn->info = NULL;
995 route_unlock_node (rn); /* initial reference */
996
997 /* Find interfaces that not configured already. */
998 for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
999 {
1000 int found = 0;
1001 struct connected *co = oi->connected;
1002
1003 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
1004 continue;
1005
1006 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
1007 {
1008 if (rn->info == NULL)
1009 continue;
1010
1011 if (ospf_network_match_iface(co,&rn->p))
1012 {
1013 found = 1;
1014 route_unlock_node (rn);
1015 break;
1016 }
1017 }
1018
1019 if (found == 0)
1020 {
1021 ospf_if_free (oi);
1022 ospf_area_check_free (ospf, area_id);
1023 }
1024 }
1025
1026 /* Update connected redistribute. */
1027 update_redistributed(ospf, 0); /* interfaces possibly removed */
1028 ospf_area_check_free (ospf, area_id);
1029
1030 return 1;
1031 }
1032
1033 int
1034 ospf_interface_set (struct interface *ifp, struct in_addr area_id)
1035 {
1036 struct ospf_area *area;
1037 struct listnode *cnode;
1038 struct connected *co;
1039 struct ospf *ospf;
1040 struct ospf_if_params *params;
1041 struct ospf_interface *oi;
1042 int ret = OSPF_AREA_ID_FORMAT_ADDRESS;
1043
1044 if ((ospf = ospf_lookup ()) == NULL)
1045 return 1; /* Ospf not ready yet */
1046
1047 params = IF_DEF_PARAMS (ifp);
1048
1049 SET_IF_PARAM (params, if_area);
1050 params->if_area = area_id;
1051
1052 area = ospf_area_get (ospf, area_id, ret);
1053
1054 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, co))
1055 {
1056 if (CHECK_FLAG(co->flags,ZEBRA_IFA_SECONDARY))
1057 continue;
1058
1059 if (co->address->family == AF_INET)
1060 {
1061 oi = ospf_if_table_lookup(ifp, co->address);
1062 if (!oi)
1063 oi = add_ospf_interface(ifp, area, co);
1064
1065 /* if router_id is not configured, dont bring up
1066 * interfaces.
1067 * ospf_router_id_update() will call ospf_if_update
1068 * whenever r-id is configured instead.
1069 */
1070 if ((area->ospf->router_id.s_addr != 0) && if_is_operative (ifp))
1071 ospf_if_up (oi);
1072 }
1073 }
1074
1075 /* Update connected redistribute. */
1076 update_redistributed(ospf, 1); /* interface possibly added */
1077 return 1;
1078 }
1079
1080 int
1081 ospf_interface_unset (struct interface *ifp)
1082 {
1083 struct ospf *ospf;
1084 struct ospf_if_params *params;
1085 struct listnode *node, *nnode;
1086 struct ospf_interface *oi;
1087 struct in_addr area_id;
1088
1089 ospf = ospf_lookup ();
1090 if (!ospf)
1091 return 1; /* Ospf not ready yet */
1092
1093 params = IF_DEF_PARAMS (ifp);
1094 UNSET_IF_PARAM (params, if_area);
1095 area_id = params->if_area;
1096
1097 for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
1098 {
1099 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
1100 continue;
1101 if (oi->ifp == ifp) ospf_if_free (oi);
1102 }
1103
1104 /* Update connected redistribute. */
1105 update_redistributed(ospf, 0); /* interfaces possibly removed */
1106 ospf_area_check_free (ospf, area_id);
1107
1108 return 1;
1109 }
1110
1111 /* Check whether interface matches given network
1112 * returns: 1, true. 0, false
1113 */
1114 static int
1115 ospf_network_match_iface(const struct connected *co, const struct prefix *net)
1116 {
1117 /* new approach: more elegant and conceptually clean */
1118 return prefix_match(net, CONNECTED_PREFIX(co));
1119 }
1120
1121 static void
1122 ospf_network_run_interface (struct prefix *p, struct ospf_area *area,
1123 struct interface *ifp)
1124 {
1125 struct listnode *cnode;
1126 struct connected *co;
1127
1128 if (memcmp (ifp->name, "VLINK", 5) == 0)
1129 return;
1130
1131 /* if interface prefix is match specified prefix,
1132 then create socket and join multicast group. */
1133 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, co))
1134 {
1135
1136 if (CHECK_FLAG(co->flags,ZEBRA_IFA_SECONDARY))
1137 continue;
1138
1139 if (p->family == co->address->family
1140 && ! ospf_if_table_lookup(ifp, co->address)
1141 && ospf_network_match_iface(co,p))
1142 {
1143 struct ospf_interface *oi;
1144
1145 oi = ospf_if_new (area->ospf, ifp, co->address);
1146 oi->connected = co;
1147
1148 oi->area = area;
1149
1150 oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4);
1151 oi->output_cost = ospf_if_get_output_cost (oi);
1152
1153 /* Add pseudo neighbor. */
1154 ospf_nbr_add_self (oi, oi->ospf->router_id);
1155
1156 /* Relate ospf interface to ospf instance. */
1157 oi->ospf = area->ospf;
1158
1159 /* update network type as interface flag */
1160 /* If network type is specified previously,
1161 skip network type setting. */
1162 oi->type = IF_DEF_PARAMS (ifp)->type;
1163
1164 ospf_area_add_if (oi->area, oi);
1165
1166 /* if router_id is not configured, dont bring up
1167 * interfaces.
1168 * ospf_router_id_update() will call ospf_if_update
1169 * whenever r-id is configured instead.
1170 */
1171 if ((area->ospf->router_id.s_addr != 0)
1172 && if_is_operative (ifp))
1173 ospf_if_up (oi);
1174 }
1175 }
1176 }
1177
1178 static void
1179 ospf_network_run (struct prefix *p, struct ospf_area *area)
1180 {
1181 struct interface *ifp;
1182 struct listnode *node;
1183
1184 /* Schedule Router ID Update. */
1185 if (area->ospf->router_id.s_addr == 0)
1186 ospf_router_id_update (area->ospf);
1187
1188 /* Get target interface. */
1189 for (ALL_LIST_ELEMENTS_RO (om->iflist, node, ifp))
1190 ospf_network_run_interface (p, area, ifp);
1191 }
1192
1193 void
1194 ospf_ls_upd_queue_empty (struct ospf_interface *oi)
1195 {
1196 struct route_node *rn;
1197 struct listnode *node, *nnode;
1198 struct list *lst;
1199 struct ospf_lsa *lsa;
1200
1201 /* empty ls update queue */
1202 for (rn = route_top (oi->ls_upd_queue); rn;
1203 rn = route_next (rn))
1204 if ((lst = (struct list *) rn->info))
1205 {
1206 for (ALL_LIST_ELEMENTS (lst, node, nnode, lsa))
1207 ospf_lsa_unlock (&lsa); /* oi->ls_upd_queue */
1208 list_free (lst);
1209 rn->info = NULL;
1210 }
1211
1212 /* remove update event */
1213 if (oi->t_ls_upd_event)
1214 {
1215 thread_cancel (oi->t_ls_upd_event);
1216 oi->t_ls_upd_event = NULL;
1217 }
1218 }
1219
1220 void
1221 ospf_if_update (struct ospf *ospf, struct interface *ifp)
1222 {
1223 struct route_node *rn;
1224 struct ospf_network *network;
1225 struct ospf_area *area;
1226 struct ospf_if_params *params;
1227
1228 if (!ospf)
1229 ospf = ospf_lookup ();
1230
1231 /* OSPF must be on and Router-ID must be configured. */
1232 if (!ospf || ospf->router_id.s_addr == 0)
1233 return;
1234
1235 /* Run each netowrk for this interface. */
1236 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
1237 if (rn->info != NULL)
1238 {
1239 network = (struct ospf_network *) rn->info;
1240 area = ospf_area_get (ospf, network->area_id, network->format);
1241 ospf_network_run_interface (&rn->p, area, ifp);
1242 }
1243
1244 /* create oif for any new co */
1245 params = IF_DEF_PARAMS (ifp);
1246 if (OSPF_IF_PARAM_CONFIGURED(params, if_area))
1247 {
1248 ospf_interface_set (ifp, params->if_area);
1249 }
1250 }
1251
1252 void
1253 ospf_remove_vls_through_area (struct ospf *ospf, struct ospf_area *area)
1254 {
1255 struct listnode *node, *nnode;
1256 struct ospf_vl_data *vl_data;
1257
1258 for (ALL_LIST_ELEMENTS (ospf->vlinks, node, nnode, vl_data))
1259 if (IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
1260 ospf_vl_delete (ospf, vl_data);
1261 }
1262
1263
1264 static const struct message ospf_area_type_msg[] =
1265 {
1266 { OSPF_AREA_DEFAULT, "Default" },
1267 { OSPF_AREA_STUB, "Stub" },
1268 { OSPF_AREA_NSSA, "NSSA" },
1269 };
1270 static const int ospf_area_type_msg_max = OSPF_AREA_TYPE_MAX;
1271
1272 static void
1273 ospf_area_type_set (struct ospf_area *area, int type)
1274 {
1275 struct listnode *node;
1276 struct ospf_interface *oi;
1277
1278 if (area->external_routing == type)
1279 {
1280 if (IS_DEBUG_OSPF_EVENT)
1281 zlog_debug ("Area[%s]: Types are the same, ignored.",
1282 inet_ntoa (area->area_id));
1283 return;
1284 }
1285
1286 area->external_routing = type;
1287
1288 if (IS_DEBUG_OSPF_EVENT)
1289 zlog_debug ("Area[%s]: Configured as %s", inet_ntoa (area->area_id),
1290 LOOKUP (ospf_area_type_msg, type));
1291
1292 switch (area->external_routing)
1293 {
1294 case OSPF_AREA_DEFAULT:
1295 for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
1296 if (oi->nbr_self != NULL)
1297 {
1298 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
1299 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
1300 }
1301 break;
1302 case OSPF_AREA_STUB:
1303 for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
1304 if (oi->nbr_self != NULL)
1305 {
1306 if (IS_DEBUG_OSPF_EVENT)
1307 zlog_debug ("setting options on %s accordingly", IF_NAME (oi));
1308 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
1309 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
1310 if (IS_DEBUG_OSPF_EVENT)
1311 zlog_debug ("options set on %s: %x",
1312 IF_NAME (oi), OPTIONS (oi));
1313 }
1314 break;
1315 case OSPF_AREA_NSSA:
1316 for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
1317 if (oi->nbr_self != NULL)
1318 {
1319 zlog_debug ("setting nssa options on %s accordingly", IF_NAME (oi));
1320 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
1321 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
1322 zlog_debug ("options set on %s: %x", IF_NAME (oi), OPTIONS (oi));
1323 }
1324 break;
1325 default:
1326 break;
1327 }
1328
1329 ospf_router_lsa_update_area (area);
1330 ospf_schedule_abr_task (area->ospf);
1331 }
1332
1333 int
1334 ospf_area_shortcut_set (struct ospf *ospf, struct ospf_area *area, int mode)
1335 {
1336 if (area->shortcut_configured == mode)
1337 return 0;
1338
1339 area->shortcut_configured = mode;
1340 ospf_router_lsa_update_area (area);
1341 ospf_schedule_abr_task (ospf);
1342
1343 ospf_area_check_free (ospf, area->area_id);
1344
1345 return 1;
1346 }
1347
1348 int
1349 ospf_area_shortcut_unset (struct ospf *ospf, struct ospf_area *area)
1350 {
1351 area->shortcut_configured = OSPF_SHORTCUT_DEFAULT;
1352 ospf_router_lsa_update_area (area);
1353 ospf_area_check_free (ospf, area->area_id);
1354 ospf_schedule_abr_task (ospf);
1355
1356 return 1;
1357 }
1358
1359 static int
1360 ospf_area_vlink_count (struct ospf *ospf, struct ospf_area *area)
1361 {
1362 struct ospf_vl_data *vl;
1363 struct listnode *node;
1364 int count = 0;
1365
1366 for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl))
1367 if (IPV4_ADDR_SAME (&vl->vl_area_id, &area->area_id))
1368 count++;
1369
1370 return count;
1371 }
1372
1373 int
1374 ospf_area_stub_set (struct ospf *ospf, struct in_addr area_id)
1375 {
1376 struct ospf_area *area;
1377 int format = OSPF_AREA_ID_FORMAT_ADDRESS;
1378
1379 area = ospf_area_get (ospf, area_id, format);
1380 if (ospf_area_vlink_count (ospf, area))
1381 return 0;
1382
1383 if (area->external_routing != OSPF_AREA_STUB)
1384 ospf_area_type_set (area, OSPF_AREA_STUB);
1385
1386 return 1;
1387 }
1388
1389 int
1390 ospf_area_stub_unset (struct ospf *ospf, struct in_addr area_id)
1391 {
1392 struct ospf_area *area;
1393
1394 area = ospf_area_lookup_by_area_id (ospf, area_id);
1395 if (area == NULL)
1396 return 1;
1397
1398 if (area->external_routing == OSPF_AREA_STUB)
1399 ospf_area_type_set (area, OSPF_AREA_DEFAULT);
1400
1401 ospf_area_check_free (ospf, area_id);
1402
1403 return 1;
1404 }
1405
1406 int
1407 ospf_area_no_summary_set (struct ospf *ospf, struct in_addr area_id)
1408 {
1409 struct ospf_area *area;
1410 int format = OSPF_AREA_ID_FORMAT_ADDRESS;
1411
1412 area = ospf_area_get (ospf, area_id, format);
1413 area->no_summary = 1;
1414
1415 return 1;
1416 }
1417
1418 int
1419 ospf_area_no_summary_unset (struct ospf *ospf, struct in_addr area_id)
1420 {
1421 struct ospf_area *area;
1422
1423 area = ospf_area_lookup_by_area_id (ospf, area_id);
1424 if (area == NULL)
1425 return 0;
1426
1427 area->no_summary = 0;
1428 ospf_area_check_free (ospf, area_id);
1429
1430 return 1;
1431 }
1432
1433 int
1434 ospf_area_nssa_set (struct ospf *ospf, struct in_addr area_id)
1435 {
1436 struct ospf_area *area;
1437 int format = OSPF_AREA_ID_FORMAT_ADDRESS;
1438
1439 area = ospf_area_get (ospf, area_id, format);
1440 if (ospf_area_vlink_count (ospf, area))
1441 return 0;
1442
1443 if (area->external_routing != OSPF_AREA_NSSA)
1444 {
1445 ospf_area_type_set (area, OSPF_AREA_NSSA);
1446 ospf->anyNSSA++;
1447 }
1448
1449 /* set NSSA area defaults */
1450 area->no_summary = 0;
1451 area->NSSATranslatorRole = OSPF_NSSA_ROLE_CANDIDATE;
1452 area->NSSATranslatorState = OSPF_NSSA_TRANSLATE_DISABLED;
1453 area->NSSATranslatorStabilityInterval = OSPF_NSSA_TRANS_STABLE_DEFAULT;
1454
1455 return 1;
1456 }
1457
1458 int
1459 ospf_area_nssa_unset (struct ospf *ospf, struct in_addr area_id)
1460 {
1461 struct ospf_area *area;
1462
1463 area = ospf_area_lookup_by_area_id (ospf, area_id);
1464 if (area == NULL)
1465 return 0;
1466
1467 if (area->external_routing == OSPF_AREA_NSSA)
1468 {
1469 ospf->anyNSSA--;
1470 ospf_area_type_set (area, OSPF_AREA_DEFAULT);
1471 }
1472
1473 ospf_area_check_free (ospf, area_id);
1474
1475 return 1;
1476 }
1477
1478 int
1479 ospf_area_nssa_translator_role_set (struct ospf *ospf, struct in_addr area_id,
1480 int role)
1481 {
1482 struct ospf_area *area;
1483
1484 area = ospf_area_lookup_by_area_id (ospf, area_id);
1485 if (area == NULL)
1486 return 0;
1487
1488 area->NSSATranslatorRole = role;
1489
1490 return 1;
1491 }
1492
1493 #if 0
1494 /* XXX: unused? Leave for symmetry? */
1495 static int
1496 ospf_area_nssa_translator_role_unset (struct ospf *ospf,
1497 struct in_addr area_id)
1498 {
1499 struct ospf_area *area;
1500
1501 area = ospf_area_lookup_by_area_id (ospf, area_id);
1502 if (area == NULL)
1503 return 0;
1504
1505 area->NSSATranslatorRole = OSPF_NSSA_ROLE_CANDIDATE;
1506
1507 ospf_area_check_free (ospf, area_id);
1508
1509 return 1;
1510 }
1511 #endif
1512
1513 int
1514 ospf_area_export_list_set (struct ospf *ospf,
1515 struct ospf_area *area, const char *list_name)
1516 {
1517 struct access_list *list;
1518 list = access_list_lookup (AFI_IP, list_name);
1519
1520 EXPORT_LIST (area) = list;
1521
1522 if (EXPORT_NAME (area))
1523 free (EXPORT_NAME (area));
1524
1525 EXPORT_NAME (area) = strdup (list_name);
1526 ospf_schedule_abr_task (ospf);
1527
1528 return 1;
1529 }
1530
1531 int
1532 ospf_area_export_list_unset (struct ospf *ospf, struct ospf_area * area)
1533 {
1534
1535 EXPORT_LIST (area) = 0;
1536
1537 if (EXPORT_NAME (area))
1538 free (EXPORT_NAME (area));
1539
1540 EXPORT_NAME (area) = NULL;
1541
1542 ospf_area_check_free (ospf, area->area_id);
1543
1544 ospf_schedule_abr_task (ospf);
1545
1546 return 1;
1547 }
1548
1549 int
1550 ospf_area_import_list_set (struct ospf *ospf, struct ospf_area *area,
1551 const char *name)
1552 {
1553 struct access_list *list;
1554 list = access_list_lookup (AFI_IP, name);
1555
1556 IMPORT_LIST (area) = list;
1557
1558 if (IMPORT_NAME (area))
1559 free (IMPORT_NAME (area));
1560
1561 IMPORT_NAME (area) = strdup (name);
1562 ospf_schedule_abr_task (ospf);
1563
1564 return 1;
1565 }
1566
1567 int
1568 ospf_area_import_list_unset (struct ospf *ospf, struct ospf_area * area)
1569 {
1570 IMPORT_LIST (area) = 0;
1571
1572 if (IMPORT_NAME (area))
1573 free (IMPORT_NAME (area));
1574
1575 IMPORT_NAME (area) = NULL;
1576 ospf_area_check_free (ospf, area->area_id);
1577
1578 ospf_schedule_abr_task (ospf);
1579
1580 return 1;
1581 }
1582
1583 int
1584 ospf_timers_refresh_set (struct ospf *ospf, int interval)
1585 {
1586 int time_left;
1587
1588 if (ospf->lsa_refresh_interval == interval)
1589 return 1;
1590
1591 time_left = ospf->lsa_refresh_interval -
1592 (monotime(NULL) - ospf->lsa_refresher_started);
1593
1594 if (time_left > interval)
1595 {
1596 OSPF_TIMER_OFF (ospf->t_lsa_refresher);
1597 ospf->t_lsa_refresher =
1598 thread_add_timer(master, ospf_lsa_refresh_walker, ospf, interval,
1599 NULL);
1600 }
1601 ospf->lsa_refresh_interval = interval;
1602
1603 return 1;
1604 }
1605
1606 int
1607 ospf_timers_refresh_unset (struct ospf *ospf)
1608 {
1609 int time_left;
1610
1611 time_left = ospf->lsa_refresh_interval -
1612 (monotime(NULL) - ospf->lsa_refresher_started);
1613
1614 if (time_left > OSPF_LSA_REFRESH_INTERVAL_DEFAULT)
1615 {
1616 OSPF_TIMER_OFF (ospf->t_lsa_refresher);
1617 ospf->t_lsa_refresher =
1618 thread_add_timer(master, ospf_lsa_refresh_walker, ospf,
1619 OSPF_LSA_REFRESH_INTERVAL_DEFAULT, NULL);
1620 }
1621
1622 ospf->lsa_refresh_interval = OSPF_LSA_REFRESH_INTERVAL_DEFAULT;
1623
1624 return 1;
1625 }
1626
1627
1628 static struct ospf_nbr_nbma *
1629 ospf_nbr_nbma_new (void)
1630 {
1631 struct ospf_nbr_nbma *nbr_nbma;
1632
1633 nbr_nbma = XCALLOC (MTYPE_OSPF_NEIGHBOR_STATIC,
1634 sizeof (struct ospf_nbr_nbma));
1635
1636 nbr_nbma->priority = OSPF_NEIGHBOR_PRIORITY_DEFAULT;
1637 nbr_nbma->v_poll = OSPF_POLL_INTERVAL_DEFAULT;
1638
1639 return nbr_nbma;
1640 }
1641
1642 static void
1643 ospf_nbr_nbma_free (struct ospf_nbr_nbma *nbr_nbma)
1644 {
1645 XFREE (MTYPE_OSPF_NEIGHBOR_STATIC, nbr_nbma);
1646 }
1647
1648 static void
1649 ospf_nbr_nbma_delete (struct ospf *ospf, struct ospf_nbr_nbma *nbr_nbma)
1650 {
1651 struct route_node *rn;
1652 struct prefix_ipv4 p;
1653
1654 p.family = AF_INET;
1655 p.prefix = nbr_nbma->addr;
1656 p.prefixlen = IPV4_MAX_BITLEN;
1657
1658 rn = route_node_lookup (ospf->nbr_nbma, (struct prefix *)&p);
1659 if (rn)
1660 {
1661 ospf_nbr_nbma_free (rn->info);
1662 rn->info = NULL;
1663 route_unlock_node (rn);
1664 route_unlock_node (rn);
1665 }
1666 }
1667
1668 static void
1669 ospf_nbr_nbma_down (struct ospf_nbr_nbma *nbr_nbma)
1670 {
1671 OSPF_TIMER_OFF (nbr_nbma->t_poll);
1672
1673 if (nbr_nbma->nbr)
1674 {
1675 nbr_nbma->nbr->nbr_nbma = NULL;
1676 OSPF_NSM_EVENT_EXECUTE (nbr_nbma->nbr, NSM_KillNbr);
1677 }
1678
1679 if (nbr_nbma->oi)
1680 listnode_delete (nbr_nbma->oi->nbr_nbma, nbr_nbma);
1681 }
1682
1683 static void
1684 ospf_nbr_nbma_add (struct ospf_nbr_nbma *nbr_nbma,
1685 struct ospf_interface *oi)
1686 {
1687 struct ospf_neighbor *nbr;
1688 struct route_node *rn;
1689 struct prefix p;
1690
1691 if (oi->type != OSPF_IFTYPE_NBMA)
1692 return;
1693
1694 if (nbr_nbma->nbr != NULL)
1695 return;
1696
1697 if (IPV4_ADDR_SAME (&oi->nbr_self->address.u.prefix4, &nbr_nbma->addr))
1698 return;
1699
1700 nbr_nbma->oi = oi;
1701 listnode_add (oi->nbr_nbma, nbr_nbma);
1702
1703 /* Get neighbor information from table. */
1704 p.family = AF_INET;
1705 p.prefixlen = IPV4_MAX_BITLEN;
1706 p.u.prefix4 = nbr_nbma->addr;
1707
1708 rn = route_node_get (oi->nbrs, (struct prefix *)&p);
1709 if (rn->info)
1710 {
1711 nbr = rn->info;
1712 nbr->nbr_nbma = nbr_nbma;
1713 nbr_nbma->nbr = nbr;
1714
1715 route_unlock_node (rn);
1716 }
1717 else
1718 {
1719 nbr = rn->info = ospf_nbr_new (oi);
1720 nbr->state = NSM_Down;
1721 nbr->src = nbr_nbma->addr;
1722 nbr->nbr_nbma = nbr_nbma;
1723 nbr->priority = nbr_nbma->priority;
1724 nbr->address = p;
1725
1726 nbr_nbma->nbr = nbr;
1727
1728 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_Start);
1729 }
1730 }
1731
1732 void
1733 ospf_nbr_nbma_if_update (struct ospf *ospf, struct ospf_interface *oi)
1734 {
1735 struct ospf_nbr_nbma *nbr_nbma;
1736 struct route_node *rn;
1737 struct prefix_ipv4 p;
1738
1739 if (oi->type != OSPF_IFTYPE_NBMA)
1740 return;
1741
1742 for (rn = route_top (ospf->nbr_nbma); rn; rn = route_next (rn))
1743 if ((nbr_nbma = rn->info))
1744 if (nbr_nbma->oi == NULL && nbr_nbma->nbr == NULL)
1745 {
1746 p.family = AF_INET;
1747 p.prefix = nbr_nbma->addr;
1748 p.prefixlen = IPV4_MAX_BITLEN;
1749
1750 if (prefix_match (oi->address, (struct prefix *)&p))
1751 ospf_nbr_nbma_add (nbr_nbma, oi);
1752 }
1753 }
1754
1755 struct ospf_nbr_nbma *
1756 ospf_nbr_nbma_lookup (struct ospf *ospf, struct in_addr nbr_addr)
1757 {
1758 struct route_node *rn;
1759 struct prefix_ipv4 p;
1760
1761 p.family = AF_INET;
1762 p.prefix = nbr_addr;
1763 p.prefixlen = IPV4_MAX_BITLEN;
1764
1765 rn = route_node_lookup (ospf->nbr_nbma, (struct prefix *)&p);
1766 if (rn)
1767 {
1768 route_unlock_node (rn);
1769 return rn->info;
1770 }
1771 return NULL;
1772 }
1773
1774 struct ospf_nbr_nbma *
1775 ospf_nbr_nbma_lookup_next (struct ospf *ospf, struct in_addr *addr, int first)
1776 {
1777 #if 0
1778 struct ospf_nbr_nbma *nbr_nbma;
1779 struct listnode *node;
1780 #endif
1781
1782 if (ospf == NULL)
1783 return NULL;
1784
1785 #if 0
1786 for (ALL_LIST_ELEMENTS_RO (ospf->nbr_nbma, node, nbr_nbma))
1787 {
1788 if (first)
1789 {
1790 *addr = nbr_nbma->addr;
1791 return nbr_nbma;
1792 }
1793 else if (ntohl (nbr_nbma->addr.s_addr) > ntohl (addr->s_addr))
1794 {
1795 *addr = nbr_nbma->addr;
1796 return nbr_nbma;
1797 }
1798 }
1799 #endif
1800 return NULL;
1801 }
1802
1803 int
1804 ospf_nbr_nbma_set (struct ospf *ospf, struct in_addr nbr_addr)
1805 {
1806 struct ospf_nbr_nbma *nbr_nbma;
1807 struct ospf_interface *oi;
1808 struct prefix_ipv4 p;
1809 struct route_node *rn;
1810 struct listnode *node;
1811
1812 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1813 if (nbr_nbma)
1814 return 0;
1815
1816 nbr_nbma = ospf_nbr_nbma_new ();
1817 nbr_nbma->addr = nbr_addr;
1818
1819 p.family = AF_INET;
1820 p.prefix = nbr_addr;
1821 p.prefixlen = IPV4_MAX_BITLEN;
1822
1823 rn = route_node_get (ospf->nbr_nbma, (struct prefix *)&p);
1824 if (rn->info)
1825 route_unlock_node (rn);
1826 rn->info = nbr_nbma;
1827
1828 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
1829 {
1830 if (oi->type == OSPF_IFTYPE_NBMA)
1831 if (prefix_match (oi->address, (struct prefix *)&p))
1832 {
1833 ospf_nbr_nbma_add (nbr_nbma, oi);
1834 break;
1835 }
1836 }
1837
1838 return 1;
1839 }
1840
1841 int
1842 ospf_nbr_nbma_unset (struct ospf *ospf, struct in_addr nbr_addr)
1843 {
1844 struct ospf_nbr_nbma *nbr_nbma;
1845
1846 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1847 if (nbr_nbma == NULL)
1848 return 0;
1849
1850 ospf_nbr_nbma_down (nbr_nbma);
1851 ospf_nbr_nbma_delete (ospf, nbr_nbma);
1852
1853 return 1;
1854 }
1855
1856 int
1857 ospf_nbr_nbma_priority_set (struct ospf *ospf, struct in_addr nbr_addr,
1858 u_char priority)
1859 {
1860 struct ospf_nbr_nbma *nbr_nbma;
1861
1862 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1863 if (nbr_nbma == NULL)
1864 return 0;
1865
1866 if (nbr_nbma->priority != priority)
1867 nbr_nbma->priority = priority;
1868
1869 return 1;
1870 }
1871
1872 int
1873 ospf_nbr_nbma_priority_unset (struct ospf *ospf, struct in_addr nbr_addr)
1874 {
1875 struct ospf_nbr_nbma *nbr_nbma;
1876
1877 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1878 if (nbr_nbma == NULL)
1879 return 0;
1880
1881 if (nbr_nbma != OSPF_NEIGHBOR_PRIORITY_DEFAULT)
1882 nbr_nbma->priority = OSPF_NEIGHBOR_PRIORITY_DEFAULT;
1883
1884 return 1;
1885 }
1886
1887 int
1888 ospf_nbr_nbma_poll_interval_set (struct ospf *ospf, struct in_addr nbr_addr,
1889 unsigned int interval)
1890 {
1891 struct ospf_nbr_nbma *nbr_nbma;
1892
1893 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1894 if (nbr_nbma == NULL)
1895 return 0;
1896
1897 if (nbr_nbma->v_poll != interval)
1898 {
1899 nbr_nbma->v_poll = interval;
1900 if (nbr_nbma->oi && ospf_if_is_up (nbr_nbma->oi))
1901 {
1902 OSPF_TIMER_OFF (nbr_nbma->t_poll);
1903 OSPF_POLL_TIMER_ON (nbr_nbma->t_poll, ospf_poll_timer,
1904 nbr_nbma->v_poll);
1905 }
1906 }
1907
1908 return 1;
1909 }
1910
1911 int
1912 ospf_nbr_nbma_poll_interval_unset (struct ospf *ospf, struct in_addr addr)
1913 {
1914 struct ospf_nbr_nbma *nbr_nbma;
1915
1916 nbr_nbma = ospf_nbr_nbma_lookup (ospf, addr);
1917 if (nbr_nbma == NULL)
1918 return 0;
1919
1920 if (nbr_nbma->v_poll != OSPF_POLL_INTERVAL_DEFAULT)
1921 nbr_nbma->v_poll = OSPF_POLL_INTERVAL_DEFAULT;
1922
1923 return 1;
1924 }
1925
1926 void
1927 ospf_master_init (struct thread_master *master)
1928 {
1929 memset (&ospf_master, 0, sizeof (struct ospf_master));
1930
1931 om = &ospf_master;
1932 om->ospf = list_new ();
1933 om->master = master;
1934 }