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