blob: abf898ca666763f46435c0be0ee2157ab3b2b1b8 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* OSPF version 2 daemon program.
2 Copyright (C) 1999, 2000 Toshiaki Takada
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-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
37#include "ospfd/ospfd.h"
38#include "ospfd/ospf_network.h"
39#include "ospfd/ospf_interface.h"
40#include "ospfd/ospf_ism.h"
41#include "ospfd/ospf_asbr.h"
42#include "ospfd/ospf_lsa.h"
43#include "ospfd/ospf_lsdb.h"
44#include "ospfd/ospf_neighbor.h"
45#include "ospfd/ospf_nsm.h"
46#include "ospfd/ospf_spf.h"
47#include "ospfd/ospf_packet.h"
48#include "ospfd/ospf_dump.h"
49#include "ospfd/ospf_zebra.h"
50#include "ospfd/ospf_abr.h"
51#include "ospfd/ospf_flood.h"
52#include "ospfd/ospf_route.h"
53#include "ospfd/ospf_ase.h"
54
paul020709f2003-04-04 02:44:16 +000055
pauledd7c242003-06-04 13:59:38 +000056
paul020709f2003-04-04 02:44:16 +000057/* OSPF process wide configuration. */
58static struct ospf_master ospf_master;
59
60/* OSPF process wide configuration pointer to export. */
61struct ospf_master *om;
paul718e3742002-12-13 20:15:29 +000062
63extern struct zclient *zclient;
64
65
paul68980082003-03-25 05:07:42 +000066void ospf_remove_vls_through_area (struct ospf *, struct ospf_area *);
67void ospf_network_free (struct ospf *, struct ospf_network *);
paul718e3742002-12-13 20:15:29 +000068void ospf_area_free (struct ospf_area *);
69void ospf_network_run (struct ospf *, struct prefix *, struct ospf_area *);
70
71/* Get Router ID from ospf interface list. */
72struct in_addr
73ospf_router_id_get (list if_list)
74{
75 listnode node;
76 struct in_addr router_id;
77
78 memset (&router_id, 0, sizeof (struct in_addr));
79
80 for (node = listhead (if_list); node; nextnode (node))
81 {
82 struct ospf_interface *oi = getdata (node);
83
84 if (!if_is_up (oi->ifp) ||
85 OSPF_IF_PARAM (oi, passive_interface) == OSPF_IF_PASSIVE)
86 continue;
87
88 /* Ignore virtual link interface. */
89 if (oi->type != OSPF_IFTYPE_VIRTUALLINK &&
90 oi->type != OSPF_IFTYPE_LOOPBACK)
91 if (IPV4_ADDR_CMP (&router_id, &oi->address->u.prefix4) < 0)
92 router_id = oi->address->u.prefix4;
93 }
94
95 return router_id;
96}
97
98#define OSPF_EXTERNAL_LSA_ORIGINATE_DELAY 1
99
100void
paul68980082003-03-25 05:07:42 +0000101ospf_router_id_update (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000102{
paul718e3742002-12-13 20:15:29 +0000103 struct in_addr router_id, router_id_old;
paul68980082003-03-25 05:07:42 +0000104 listnode node;
paul718e3742002-12-13 20:15:29 +0000105
106 if (IS_DEBUG_OSPF_EVENT)
paul68980082003-03-25 05:07:42 +0000107 zlog_info ("Router-ID[OLD:%s]: Update", inet_ntoa (ospf->router_id));
paul718e3742002-12-13 20:15:29 +0000108
paul68980082003-03-25 05:07:42 +0000109 router_id_old = ospf->router_id;
paul718e3742002-12-13 20:15:29 +0000110
paul68980082003-03-25 05:07:42 +0000111 if (ospf->router_id_static.s_addr != 0)
112 router_id = ospf->router_id_static;
paul718e3742002-12-13 20:15:29 +0000113 else
paul68980082003-03-25 05:07:42 +0000114 router_id = ospf_router_id_get (ospf->oiflist);
paul718e3742002-12-13 20:15:29 +0000115
paul68980082003-03-25 05:07:42 +0000116 ospf->router_id = router_id;
paul718e3742002-12-13 20:15:29 +0000117
118 if (IS_DEBUG_OSPF_EVENT)
paul68980082003-03-25 05:07:42 +0000119 zlog_info ("Router-ID[NEW:%s]: Update", inet_ntoa (ospf->router_id));
paul718e3742002-12-13 20:15:29 +0000120
121 if (!IPV4_ADDR_SAME (&router_id_old, &router_id))
122 {
paul68980082003-03-25 05:07:42 +0000123 for (node = listhead (ospf->oiflist); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +0000124 {
125 struct ospf_interface *oi = getdata (node);
126
127 /* Update self-neighbor's router_id. */
128 oi->nbr_self->router_id = router_id;
129 }
130
131 /* If AS-external-LSA is queued, then flush those LSAs. */
paul68980082003-03-25 05:07:42 +0000132 if (router_id_old.s_addr == 0 && ospf->external_origin)
paul718e3742002-12-13 20:15:29 +0000133 {
134 int type;
135 /* Originate each redistributed external route. */
136 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
paul68980082003-03-25 05:07:42 +0000137 if (ospf->external_origin & (1 << type))
paul718e3742002-12-13 20:15:29 +0000138 thread_add_event (master, ospf_external_lsa_originate_timer,
paul68980082003-03-25 05:07:42 +0000139 ospf, type);
paul718e3742002-12-13 20:15:29 +0000140 /* Originate Deafult. */
paul68980082003-03-25 05:07:42 +0000141 if (ospf->external_origin & (1 << ZEBRA_ROUTE_MAX))
paul718e3742002-12-13 20:15:29 +0000142 thread_add_event (master, ospf_default_originate_timer,
paul68980082003-03-25 05:07:42 +0000143 &ospf->default_originate, 0);
paul718e3742002-12-13 20:15:29 +0000144
paul68980082003-03-25 05:07:42 +0000145 ospf->external_origin = 0;
paul718e3742002-12-13 20:15:29 +0000146 }
147
paul68980082003-03-25 05:07:42 +0000148 OSPF_TIMER_ON (ospf->t_router_lsa_update,
paul718e3742002-12-13 20:15:29 +0000149 ospf_router_lsa_update_timer, OSPF_LSA_UPDATE_DELAY);
150 }
151}
152
153int
154ospf_router_id_update_timer (struct thread *thread)
155{
paul020709f2003-04-04 02:44:16 +0000156 struct ospf *ospf = THREAD_ARG (thread);
paul68980082003-03-25 05:07:42 +0000157
paul718e3742002-12-13 20:15:29 +0000158 if (IS_DEBUG_OSPF_EVENT)
159 zlog_info ("Router-ID: Update timer fired!");
160
paul68980082003-03-25 05:07:42 +0000161 ospf->t_router_id_update = NULL;
162 ospf_router_id_update (ospf);
paul718e3742002-12-13 20:15:29 +0000163
164 return 0;
165}
166
167/* For OSPF area sort by area id. */
168int
169ospf_area_id_cmp (struct ospf_area *a1, struct ospf_area *a2)
170{
171 if (ntohl (a1->area_id.s_addr) > ntohl (a2->area_id.s_addr))
172 return 1;
173 if (ntohl (a1->area_id.s_addr) < ntohl (a2->area_id.s_addr))
174 return -1;
175 return 0;
176}
177
178/* Allocate new ospf structure. */
179struct ospf *
180ospf_new ()
181{
182 int i;
183
184 struct ospf *new = XCALLOC (MTYPE_OSPF_TOP, sizeof (struct ospf));
185
186 new->router_id.s_addr = htonl (0);
187 new->router_id_static.s_addr = htonl (0);
188
189 new->abr_type = OSPF_ABR_STAND;
paul718e3742002-12-13 20:15:29 +0000190 new->oiflist = list_new ();
191 new->vlinks = list_new ();
192 new->areas = list_new ();
193 new->areas->cmp = (int (*)(void *, void *)) ospf_area_id_cmp;
194 new->networks = route_table_init ();
195 new->nbr_nbma = route_table_init ();
196
197 new->lsdb = ospf_lsdb_new ();
198
199 new->default_originate = DEFAULT_ORIGINATE_NONE;
200
201 new->new_external_route = route_table_init ();
202 new->old_external_route = route_table_init ();
203 new->external_lsas = route_table_init ();
204
205 /* Distribute parameter init. */
206 for (i = 0; i <= ZEBRA_ROUTE_MAX; i++)
207 {
208 new->dmetric[i].type = -1;
209 new->dmetric[i].value = -1;
210 }
211 new->default_metric = -1;
212 new->ref_bandwidth = OSPF_DEFAULT_REF_BANDWIDTH;
213
214 /* SPF timer value init. */
215 new->spf_delay = OSPF_SPF_DELAY_DEFAULT;
216 new->spf_holdtime = OSPF_SPF_HOLDTIME_DEFAULT;
217
218 /* MaxAge init. */
219 new->maxage_lsa = list_new ();
220 new->t_maxage_walker =
221 thread_add_timer (master, ospf_lsa_maxage_walker,
paul68980082003-03-25 05:07:42 +0000222 new, OSPF_LSA_MAXAGE_CHECK_INTERVAL);
paul718e3742002-12-13 20:15:29 +0000223
224 /* Distance table init. */
225 new->distance_table = route_table_init ();
226
227 new->lsa_refresh_queue.index = 0;
228 new->lsa_refresh_interval = OSPF_LSA_REFRESH_INTERVAL_DEFAULT;
229 new->t_lsa_refresher = thread_add_timer (master, ospf_lsa_refresh_walker,
230 new, new->lsa_refresh_interval);
231 new->lsa_refresher_started = time (NULL);
232
233 new->fd = ospf_sock_init ();
234 if (new->fd >= 0)
235 new->t_read = thread_add_read (master, ospf_read, new, new->fd);
236 new->oi_write_q = list_new ();
237
238 return new;
239}
240
241struct ospf *
paul020709f2003-04-04 02:44:16 +0000242ospf_lookup ()
243{
244 if (listcount (om->ospf) == 0)
245 return NULL;
246
247 return getdata (listhead (om->ospf));
248}
249
250void
251ospf_add (struct ospf *ospf)
252{
253 listnode_add (om->ospf, ospf);
254}
255
256void
257ospf_delete (struct ospf *ospf)
258{
259 listnode_delete (om->ospf, ospf);
260}
261
262struct ospf *
paul718e3742002-12-13 20:15:29 +0000263ospf_get ()
264{
paul020709f2003-04-04 02:44:16 +0000265 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000266
paul020709f2003-04-04 02:44:16 +0000267 ospf = ospf_lookup ();
268 if (ospf == NULL)
269 {
270 ospf = ospf_new ();
271 ospf_add (ospf);
paul718e3742002-12-13 20:15:29 +0000272
paul020709f2003-04-04 02:44:16 +0000273 if (ospf->router_id_static.s_addr == 0)
274 ospf_router_id_update (ospf);
paul718e3742002-12-13 20:15:29 +0000275
276#ifdef HAVE_OPAQUE_LSA
paul020709f2003-04-04 02:44:16 +0000277 ospf_opaque_type11_lsa_init (ospf);
paul718e3742002-12-13 20:15:29 +0000278#endif /* HAVE_OPAQUE_LSA */
paul020709f2003-04-04 02:44:16 +0000279 }
paul68980082003-03-25 05:07:42 +0000280
281 return ospf;
paul718e3742002-12-13 20:15:29 +0000282}
283
284void
285ospf_finish (struct ospf *ospf)
286{
287 struct route_node *rn;
288 struct ospf_nbr_nbma *nbr_nbma;
paul68980082003-03-25 05:07:42 +0000289 struct ospf_lsa *lsa;
paul718e3742002-12-13 20:15:29 +0000290 listnode node;
291 int i;
292
293#ifdef HAVE_OPAQUE_LSA
294 ospf_opaque_type11_lsa_term (ospf);
295#endif /* HAVE_OPAQUE_LSA */
296
297 /* Unredister redistribution */
298 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
paul020709f2003-04-04 02:44:16 +0000299 ospf_redistribute_unset (ospf, i);
paul718e3742002-12-13 20:15:29 +0000300
301 for (node = listhead (ospf->areas); node;)
302 {
303 struct ospf_area *area = getdata (node);
304 nextnode (node);
305
paul68980082003-03-25 05:07:42 +0000306 ospf_remove_vls_through_area (ospf, area);
paul718e3742002-12-13 20:15:29 +0000307 }
308
309 for (node = listhead (ospf->vlinks); node; )
310 {
311 struct ospf_vl_data *vl_data = node->data;
312 nextnode (node);
313
paul68980082003-03-25 05:07:42 +0000314 ospf_vl_delete (ospf, vl_data);
paul718e3742002-12-13 20:15:29 +0000315 }
316
317 list_delete (ospf->vlinks);
318
319 /* Reset interface. */
320 for (node = listhead (ospf->oiflist); node;)
321 {
322 struct ospf_interface *oi = getdata (node);
323 nextnode (node);
324
325 if (oi)
326 ospf_if_free (oi);
327 }
328
329 /* Clear static neighbors */
330 for (rn = route_top (ospf->nbr_nbma); rn; rn = route_next (rn))
331 if ((nbr_nbma = rn->info))
332 {
333 OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
334
335 if (nbr_nbma->nbr)
336 {
337 nbr_nbma->nbr->nbr_nbma = NULL;
338 nbr_nbma->nbr = NULL;
339 }
340
341 if (nbr_nbma->oi)
342 {
343 listnode_delete (nbr_nbma->oi->nbr_nbma, nbr_nbma);
344 nbr_nbma->oi = NULL;
345 }
346
347 XFREE (MTYPE_OSPF_NEIGHBOR_STATIC, nbr_nbma);
348 }
349
350 route_table_finish (ospf->nbr_nbma);
351
352 /* Clear networks and Areas. */
353 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
354 {
355 struct ospf_network *network;
356
357 if ((network = rn->info) != NULL)
358 {
paul68980082003-03-25 05:07:42 +0000359 ospf_network_free (ospf, network);
paul718e3742002-12-13 20:15:29 +0000360 rn->info = NULL;
361 route_unlock_node (rn);
362 }
363 }
364
365 for (node = listhead (ospf->areas); node;)
366 {
367 struct ospf_area *area = getdata (node);
368 nextnode (node);
369
370 listnode_delete (ospf->areas, area);
371 ospf_area_free (area);
372 }
373
374 /* Cancel all timers. */
375 OSPF_TIMER_OFF (ospf->t_external_lsa);
376 OSPF_TIMER_OFF (ospf->t_router_id_update);
377 OSPF_TIMER_OFF (ospf->t_router_lsa_update);
378 OSPF_TIMER_OFF (ospf->t_spf_calc);
379 OSPF_TIMER_OFF (ospf->t_ase_calc);
380 OSPF_TIMER_OFF (ospf->t_maxage);
381 OSPF_TIMER_OFF (ospf->t_maxage_walker);
382 OSPF_TIMER_OFF (ospf->t_abr_task);
383 OSPF_TIMER_OFF (ospf->t_distribute_update);
384 OSPF_TIMER_OFF (ospf->t_lsa_refresher);
385 OSPF_TIMER_OFF (ospf->t_read);
386 OSPF_TIMER_OFF (ospf->t_write);
387
388 close (ospf->fd);
389
390#ifdef HAVE_OPAQUE_LSA
paul68980082003-03-25 05:07:42 +0000391 LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
392 ospf_discard_from_db (ospf, ospf->lsdb, lsa);
paul718e3742002-12-13 20:15:29 +0000393#endif /* HAVE_OPAQUE_LSA */
paul68980082003-03-25 05:07:42 +0000394 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
395 ospf_discard_from_db (ospf, ospf->lsdb, lsa);
396
paul718e3742002-12-13 20:15:29 +0000397 ospf_lsdb_delete_all (ospf->lsdb);
398 ospf_lsdb_free (ospf->lsdb);
399
400 for (node = listhead (ospf->maxage_lsa); node; nextnode (node))
401 ospf_lsa_unlock (getdata (node));
402
403 list_delete (ospf->maxage_lsa);
404
405 if (ospf->old_table)
406 ospf_route_table_free (ospf->old_table);
407 if (ospf->new_table)
408 {
409 ospf_route_delete (ospf->new_table);
410 ospf_route_table_free (ospf->new_table);
411 }
412 if (ospf->old_rtrs)
413 ospf_rtrs_free (ospf->old_rtrs);
414 if (ospf->new_rtrs)
415 ospf_rtrs_free (ospf->new_rtrs);
416 if (ospf->new_external_route)
417 {
418 ospf_route_delete (ospf->new_external_route);
419 ospf_route_table_free (ospf->new_external_route);
420 }
421 if (ospf->old_external_route)
422 {
423 ospf_route_delete (ospf->old_external_route);
424 ospf_route_table_free (ospf->old_external_route);
425 }
426 if (ospf->external_lsas)
427 {
428 ospf_ase_external_lsas_finish (ospf->external_lsas);
429 }
430
431 list_delete (ospf->areas);
432
433 for (i = ZEBRA_ROUTE_SYSTEM; i <= ZEBRA_ROUTE_MAX; i++)
434 if (EXTERNAL_INFO (i) != NULL)
435 for (rn = route_top (EXTERNAL_INFO (i)); rn; rn = route_next (rn))
436 {
437 if (rn->info == NULL)
438 continue;
439
440 XFREE (MTYPE_OSPF_EXTERNAL_INFO, rn->info);
441 rn->info = NULL;
442 route_unlock_node (rn);
443 }
444
paul68980082003-03-25 05:07:42 +0000445 ospf_distance_reset (ospf);
paul718e3742002-12-13 20:15:29 +0000446 route_table_finish (ospf->distance_table);
447
paul020709f2003-04-04 02:44:16 +0000448 ospf_delete (ospf);
paul718e3742002-12-13 20:15:29 +0000449
paul020709f2003-04-04 02:44:16 +0000450 XFREE (MTYPE_OSPF_TOP, ospf);
paul718e3742002-12-13 20:15:29 +0000451}
452
453
454/* allocate new OSPF Area object */
455struct ospf_area *
paul68980082003-03-25 05:07:42 +0000456ospf_area_new (struct ospf *ospf, struct in_addr area_id)
paul718e3742002-12-13 20:15:29 +0000457{
458 struct ospf_area *new;
459
460 /* Allocate new config_network. */
461 new = XCALLOC (MTYPE_OSPF_AREA, sizeof (struct ospf_area));
462
paul68980082003-03-25 05:07:42 +0000463 new->ospf = ospf;
paul718e3742002-12-13 20:15:29 +0000464
465 new->area_id = area_id;
466
467 new->external_routing = OSPF_AREA_DEFAULT;
468 new->default_cost = 1;
469 new->auth_type = OSPF_AUTH_NULL;
470
471 /* New LSDB init. */
472 new->lsdb = ospf_lsdb_new ();
473
474 /* Self-originated LSAs initialize. */
475 new->router_lsa_self = NULL;
476
477#ifdef HAVE_OPAQUE_LSA
478 ospf_opaque_type10_lsa_init (new);
479#endif /* HAVE_OPAQUE_LSA */
480
481 new->oiflist = list_new ();
482 new->ranges = route_table_init ();
483
484 if (area_id.s_addr == OSPF_AREA_BACKBONE)
paul68980082003-03-25 05:07:42 +0000485 ospf->backbone = new;
paul718e3742002-12-13 20:15:29 +0000486
487 return new;
488}
489
490void
491ospf_area_free (struct ospf_area *area)
492{
paul68980082003-03-25 05:07:42 +0000493 struct route_node *rn;
494 struct ospf_lsa *lsa;
495
paul718e3742002-12-13 20:15:29 +0000496 /* Free LSDBs. */
paul68980082003-03-25 05:07:42 +0000497 LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
498 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
499 LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
500 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
501 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
502 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
503 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
504 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
paul718e3742002-12-13 20:15:29 +0000505
506#ifdef HAVE_NSSA
paul68980082003-03-25 05:07:42 +0000507 LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
508 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
paul718e3742002-12-13 20:15:29 +0000509#endif /* HAVE_NSSA */
510#ifdef HAVE_OPAQUE_LSA
paul68980082003-03-25 05:07:42 +0000511 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
512 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
513 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
514 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
paul718e3742002-12-13 20:15:29 +0000515#endif /* HAVE_OPAQUE_LSA */
516
517 ospf_lsdb_delete_all (area->lsdb);
518 ospf_lsdb_free (area->lsdb);
519
paul718e3742002-12-13 20:15:29 +0000520 ospf_lsa_unlock (area->router_lsa_self);
521
522 route_table_finish (area->ranges);
523 list_delete (area->oiflist);
524
525 if (EXPORT_NAME (area))
526 free (EXPORT_NAME (area));
527
528 if (IMPORT_NAME (area))
529 free (IMPORT_NAME (area));
530
531 /* Cancel timer. */
532 OSPF_TIMER_OFF (area->t_router_lsa_self);
533
534 if (OSPF_IS_AREA_BACKBONE (area))
paul68980082003-03-25 05:07:42 +0000535 area->ospf->backbone = NULL;
paul718e3742002-12-13 20:15:29 +0000536
537 XFREE (MTYPE_OSPF_AREA, area);
538}
539
540void
paul68980082003-03-25 05:07:42 +0000541ospf_area_check_free (struct ospf *ospf, struct in_addr area_id)
paul718e3742002-12-13 20:15:29 +0000542{
543 struct ospf_area *area;
544
paul68980082003-03-25 05:07:42 +0000545 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +0000546 if (area &&
547 listcount (area->oiflist) == 0 &&
548 area->ranges->top == NULL &&
549 area->shortcut_configured == OSPF_SHORTCUT_DEFAULT &&
550 area->external_routing == OSPF_AREA_DEFAULT &&
551 area->no_summary == 0 &&
552 area->default_cost == 1 &&
553 EXPORT_NAME (area) == NULL &&
554 IMPORT_NAME (area) == NULL &&
555 area->auth_type == OSPF_AUTH_NULL)
556 {
paul68980082003-03-25 05:07:42 +0000557 listnode_delete (ospf->areas, area);
paul718e3742002-12-13 20:15:29 +0000558 ospf_area_free (area);
559 }
560}
561
562struct ospf_area *
paul68980082003-03-25 05:07:42 +0000563ospf_area_get (struct ospf *ospf, struct in_addr area_id, int format)
paul718e3742002-12-13 20:15:29 +0000564{
565 struct ospf_area *area;
566
paul68980082003-03-25 05:07:42 +0000567 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +0000568 if (!area)
569 {
paul68980082003-03-25 05:07:42 +0000570 area = ospf_area_new (ospf, area_id);
paul718e3742002-12-13 20:15:29 +0000571 area->format = format;
paul68980082003-03-25 05:07:42 +0000572 listnode_add_sort (ospf->areas, area);
573 ospf_check_abr_status (ospf);
paul718e3742002-12-13 20:15:29 +0000574 }
575
576 return area;
577}
578
579struct ospf_area *
paul68980082003-03-25 05:07:42 +0000580ospf_area_lookup_by_area_id (struct ospf *ospf, struct in_addr area_id)
paul718e3742002-12-13 20:15:29 +0000581{
582 struct ospf_area *area;
583 listnode node;
584
paul68980082003-03-25 05:07:42 +0000585 for (node = listhead (ospf->areas); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +0000586 {
587 area = getdata (node);
588
589 if (IPV4_ADDR_SAME (&area->area_id, &area_id))
590 return area;
591 }
592
593 return NULL;
594}
595
596void
597ospf_area_add_if (struct ospf_area *area, struct ospf_interface *oi)
598{
599 listnode_add (area->oiflist, oi);
600}
601
602void
603ospf_area_del_if (struct ospf_area *area, struct ospf_interface *oi)
604{
605 listnode_delete (area->oiflist, oi);
606}
607
608
609/* Config network statement related functions. */
610struct ospf_network *
611ospf_network_new (struct in_addr area_id, int format)
612{
613 struct ospf_network *new;
614 new = XCALLOC (MTYPE_OSPF_NETWORK, sizeof (struct ospf_network));
615
616 new->area_id = area_id;
617 new->format = format;
618
619 return new;
620}
621
622void
paul68980082003-03-25 05:07:42 +0000623ospf_network_free (struct ospf *ospf, struct ospf_network *network)
paul718e3742002-12-13 20:15:29 +0000624{
paul68980082003-03-25 05:07:42 +0000625 ospf_area_check_free (ospf, network->area_id);
626 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +0000627 XFREE (MTYPE_OSPF_NETWORK, network);
628}
629
630int
631ospf_network_set (struct ospf *ospf, struct prefix_ipv4 *p,
632 struct in_addr area_id)
633{
634 struct ospf_network *network;
635 struct ospf_area *area;
636 struct route_node *rn;
637 struct external_info *ei;
paul147193a2003-04-19 00:31:59 +0000638 int ret = OSPF_AREA_ID_FORMAT_ADDRESS;
paul718e3742002-12-13 20:15:29 +0000639
640 rn = route_node_get (ospf->networks, (struct prefix *)p);
641 if (rn->info)
642 {
643 /* There is already same network statement. */
644 route_unlock_node (rn);
645 return 0;
646 }
647
648 rn->info = network = ospf_network_new (area_id, ret);
paul68980082003-03-25 05:07:42 +0000649 area = ospf_area_get (ospf, area_id, ret);
paul718e3742002-12-13 20:15:29 +0000650
651 /* Run network config now. */
652 ospf_network_run (ospf, (struct prefix *)p, area);
653
654 /* Update connected redistribute. */
655 if (ospf_is_type_redistributed (ZEBRA_ROUTE_CONNECT))
656 if (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT))
657 for (rn = route_top (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT));
658 rn; rn = route_next (rn))
659 if ((ei = rn->info) != NULL)
paul68980082003-03-25 05:07:42 +0000660 if (ospf_external_info_find_lsa (ospf, &ei->p))
661 if (!ospf_distribute_check_connected (ospf, ei))
662 ospf_external_lsa_flush (ospf, ei->type, &ei->p,
paul718e3742002-12-13 20:15:29 +0000663 ei->ifindex, ei->nexthop);
664
paul68980082003-03-25 05:07:42 +0000665 ospf_area_check_free (ospf, area_id);
paul718e3742002-12-13 20:15:29 +0000666
667 return 1;
668}
669
670int
671ospf_network_unset (struct ospf *ospf, struct prefix_ipv4 *p,
672 struct in_addr area_id)
673{
674 struct route_node *rn;
675 struct ospf_network *network;
676 struct external_info *ei;
677
678 rn = route_node_lookup (ospf->networks, (struct prefix *)p);
679 if (rn == NULL)
680 return 0;
681
682 network = rn->info;
683 if (!IPV4_ADDR_SAME (&area_id, &network->area_id))
684 return 0;
685
paul68980082003-03-25 05:07:42 +0000686 ospf_network_free (ospf, rn->info);
paul718e3742002-12-13 20:15:29 +0000687 rn->info = NULL;
688 route_unlock_node (rn);
689
paul68980082003-03-25 05:07:42 +0000690 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000691
692 /* Update connected redistribute. */
693 if (ospf_is_type_redistributed (ZEBRA_ROUTE_CONNECT))
694 if (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT))
695 for (rn = route_top (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT));
696 rn; rn = route_next (rn))
697 if ((ei = rn->info) != NULL)
paul68980082003-03-25 05:07:42 +0000698 if (!ospf_external_info_find_lsa (ospf, &ei->p))
699 if (ospf_distribute_check_connected (ospf, ei))
700 ospf_external_lsa_originate (ospf, ei);
paul718e3742002-12-13 20:15:29 +0000701
702 return 1;
703}
704
paul570f7592003-01-25 06:47:41 +0000705/* Check whether interface matches given network
706 * returns: 1, true. 0, false
707 */
708int
709ospf_network_match_iface(struct connected *co, struct prefix *net)
710{
711 /* Behaviour to match both Cisco where:
712 * iface address lies within network specified -> ospf
713 * and zebra 0.9[2ish-3]:
714 * PtP special case: network specified == iface peer addr -> ospf
715 */
gdt8f40e892003-12-05 14:01:43 +0000716
717 /* For PtP, match if peer address matches network address exactly.
718 * This can be addr/32 or addr/p for p < 32, but the addr must match
719 * exactly; this is not a test for falling within the prefix. This
720 * test is solely for compatibility with zebra.
721 */
722 if (if_is_pointopoint (co->ifp) &&
723 IPV4_ADDR_SAME ( &(co->destination->u.prefix4), &(net->u.prefix4)))
724 return 1;
725
726#if 0
727 /* Decline to accept PtP if dst address does not match the
728 * prefix. (ifdefed out because this is a workaround, not the
729 * desired behavior.) */
730 if (if_is_pointopoint (co->ifp) &&
731 ! prefix_match (net, co->destination))
732 return 0;
733#endif
734
735 /* If the address is within the prefix, accept. Note that this
736 * applies to PtP as well as other types.
737 */
738 if (prefix_match (net, co->address))
739 return 1;
740
741 return 0; /* no match */
paul570f7592003-01-25 06:47:41 +0000742}
743
paul718e3742002-12-13 20:15:29 +0000744void
745ospf_network_run (struct ospf *ospf, struct prefix *p, struct ospf_area *area)
746{
747 struct interface *ifp;
748 listnode node;
749
750 /* Schedule Router ID Update. */
751 if (ospf->router_id_static.s_addr == 0)
752 if (ospf->t_router_id_update == NULL)
753 {
paul020709f2003-04-04 02:44:16 +0000754 OSPF_TIMER_ON (ospf->t_router_id_update, ospf_router_id_update_timer,
755 OSPF_ROUTER_ID_UPDATE_DELAY);
paul718e3742002-12-13 20:15:29 +0000756 }
757
758 /* Get target interface. */
paul020709f2003-04-04 02:44:16 +0000759 for (node = listhead (om->iflist); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +0000760 {
761 listnode cn;
762
763 if ((ifp = getdata (node)) == NULL)
764 continue;
765
766 if (memcmp (ifp->name, "VLINK", 5) == 0)
767 continue;
768
769 /* if interface prefix is match specified prefix,
770 then create socket and join multicast group. */
771 for (cn = listhead (ifp->connected); cn; nextnode (cn))
772 {
773 struct connected *co = getdata (cn);
774 struct prefix *addr;
paul800dc102003-03-28 01:51:40 +0000775
paule7b050c2003-04-07 06:38:02 +0000776 if (CHECK_FLAG(co->flags,ZEBRA_IFA_SECONDARY))
777 continue;
paul718e3742002-12-13 20:15:29 +0000778
paul3738d8c2003-10-27 22:02:00 +0000779 if (if_is_pointopoint (co->ifp))
paul718e3742002-12-13 20:15:29 +0000780 addr = co->destination;
781 else
782 addr = co->address;
783
paulcb3f37d2003-02-18 23:26:37 +0000784 if (p->family == co->address->family
paul68980082003-03-25 05:07:42 +0000785 && ! ospf_if_is_configured (ospf, &(addr->u.prefix4))
paulcb3f37d2003-02-18 23:26:37 +0000786 && ospf_network_match_iface(co,p))
paul570f7592003-01-25 06:47:41 +0000787 {
paul537d8ea2003-08-27 06:45:32 +0000788 struct ospf_interface *oi;
paul718e3742002-12-13 20:15:29 +0000789
paul68980082003-03-25 05:07:42 +0000790 oi = ospf_if_new (ospf, ifp, co->address);
paul718e3742002-12-13 20:15:29 +0000791 oi->connected = co;
792
793 oi->nbr_self->address = *oi->address;
794
paul718e3742002-12-13 20:15:29 +0000795 oi->area = area;
796
797 oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4);
798 oi->output_cost = ospf_if_get_output_cost (oi);
799
800 if (area->external_routing != OSPF_AREA_DEFAULT)
801 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
802 oi->nbr_self->priority = OSPF_IF_PARAM (oi, priority);
803
804 /* Add pseudo neighbor. */
805 ospf_nbr_add_self (oi);
806
807 /* Make sure pseudo neighbor's router_id. */
paul68980082003-03-25 05:07:42 +0000808 oi->nbr_self->router_id = ospf->router_id;
paul718e3742002-12-13 20:15:29 +0000809 oi->nbr_self->src = oi->address->u.prefix4;
810
811 /* Relate ospf interface to ospf instance. */
paul68980082003-03-25 05:07:42 +0000812 oi->ospf = ospf;
paul718e3742002-12-13 20:15:29 +0000813
814 /* update network type as interface flag */
815 /* If network type is specified previously,
816 skip network type setting. */
817 oi->type = IF_DEF_PARAMS (ifp)->type;
818
819 /* Set area flag. */
820 switch (area->external_routing)
821 {
822 case OSPF_AREA_DEFAULT:
823 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
824 break;
825 case OSPF_AREA_STUB:
826 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
827 break;
828#ifdef HAVE_NSSA
829 case OSPF_AREA_NSSA:
830 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
831 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
832 break;
833#endif /* HAVE_NSSA */
834 }
835
836 ospf_area_add_if (oi->area, oi);
837
paul2e3b2e42002-12-13 21:03:13 +0000838 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +0000839 ospf_if_up (oi);
840
841 break;
842 }
843 }
844 }
845}
846
847void
848ospf_ls_upd_queue_empty (struct ospf_interface *oi)
849{
850 struct route_node *rn;
851 listnode node;
852 list lst;
853 struct ospf_lsa *lsa;
854
855 /* empty ls update queue */
856 for (rn = route_top (oi->ls_upd_queue); rn;
857 rn = route_next (rn))
858 if ((lst = (list) rn->info))
859 {
860 for (node = listhead (lst); node; nextnode (node))
861 if ((lsa = getdata (node)))
862 ospf_lsa_unlock (lsa);
863 list_free (lst);
864 rn->info = NULL;
865 }
866
867 /* remove update event */
868 if (oi->t_ls_upd_event)
869 {
870 thread_cancel (oi->t_ls_upd_event);
871 oi->t_ls_upd_event = NULL;
872 }
873}
874
875void
paul68980082003-03-25 05:07:42 +0000876ospf_if_update (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000877{
878 struct route_node *rn;
879 listnode node;
880 listnode next;
881 struct ospf_network *network;
882 struct ospf_area *area;
883
paul68980082003-03-25 05:07:42 +0000884 if (ospf != NULL)
paul718e3742002-12-13 20:15:29 +0000885 {
886 /* Update Router ID scheduled. */
paul68980082003-03-25 05:07:42 +0000887 if (ospf->router_id_static.s_addr == 0)
888 if (ospf->t_router_id_update == NULL)
paul718e3742002-12-13 20:15:29 +0000889 {
paul020709f2003-04-04 02:44:16 +0000890 OSPF_TIMER_ON (ospf->t_router_id_update,
891 ospf_router_id_update_timer,
892 OSPF_ROUTER_ID_UPDATE_DELAY);
paul718e3742002-12-13 20:15:29 +0000893 }
894
895 /* Find interfaces that not configured already. */
paul68980082003-03-25 05:07:42 +0000896 for (node = listhead (ospf->oiflist); node; node = next)
paul718e3742002-12-13 20:15:29 +0000897 {
898 int found = 0;
899 struct ospf_interface *oi = getdata (node);
900 struct connected *co = oi->connected;
901
902 next = nextnode (node);
903
904 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
905 continue;
906
paul68980082003-03-25 05:07:42 +0000907 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000908 {
909 if (rn->info == NULL)
910 continue;
911
paul570f7592003-01-25 06:47:41 +0000912 if (ospf_network_match_iface(co,&rn->p))
paul718e3742002-12-13 20:15:29 +0000913 {
914 found = 1;
915 route_unlock_node (rn);
916 break;
917 }
918 }
919
920 if (found == 0)
921 ospf_if_free (oi);
922 }
923
924 /* Run each interface. */
paul68980082003-03-25 05:07:42 +0000925 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000926 if (rn->info != NULL)
927 {
928 network = (struct ospf_network *) rn->info;
paul68980082003-03-25 05:07:42 +0000929 area = ospf_area_get (ospf, network->area_id, network->format);
930 ospf_network_run (ospf, &rn->p, area);
paul718e3742002-12-13 20:15:29 +0000931 }
932 }
933}
934
935void
paul68980082003-03-25 05:07:42 +0000936ospf_remove_vls_through_area (struct ospf *ospf, struct ospf_area *area)
paul718e3742002-12-13 20:15:29 +0000937{
938 listnode node, next;
939 struct ospf_vl_data *vl_data;
940
paul68980082003-03-25 05:07:42 +0000941 for (node = listhead (ospf->vlinks); node; node = next)
paul718e3742002-12-13 20:15:29 +0000942 {
943 next = node->next;
944 if ((vl_data = getdata (node)) != NULL)
945 if (IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
paul68980082003-03-25 05:07:42 +0000946 ospf_vl_delete (ospf, vl_data);
paul718e3742002-12-13 20:15:29 +0000947 }
948}
949
950
951struct message ospf_area_type_msg[] =
952{
953 { OSPF_AREA_DEFAULT, "Default" },
954 { OSPF_AREA_STUB, "Stub" },
955 { OSPF_AREA_NSSA, "NSSA" },
956};
957int ospf_area_type_msg_max = OSPF_AREA_TYPE_MAX;
958
959void
960ospf_area_type_set (struct ospf_area *area, int type)
961{
962 listnode node;
963 struct ospf_interface *oi;
964
965 if (area->external_routing == type)
966 {
967 if (IS_DEBUG_OSPF_EVENT)
968 zlog_info ("Area[%s]: Types are the same, ignored.",
969 inet_ntoa (area->area_id));
970 return;
971 }
972
973 area->external_routing = type;
974
975 if (IS_DEBUG_OSPF_EVENT)
976 zlog_info ("Area[%s]: Configured as %s", inet_ntoa (area->area_id),
977 LOOKUP (ospf_area_type_msg, type));
978
979 switch (area->external_routing)
980 {
981 case OSPF_AREA_DEFAULT:
982 for (node = listhead (area->oiflist); node; nextnode (node))
983 if ((oi = getdata (node)) != NULL)
984 if (oi->nbr_self != NULL)
hasso8585d4e2004-04-20 17:25:12 +0000985 {
986#ifdef HAVE_NSSA
987 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
988#endif /* HAVE_NSSA */
989 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
990 }
paul718e3742002-12-13 20:15:29 +0000991 break;
992 case OSPF_AREA_STUB:
993 for (node = listhead (area->oiflist); node; nextnode (node))
994 if ((oi = getdata (node)) != NULL)
995 if (oi->nbr_self != NULL)
996 {
997 if (IS_DEBUG_OSPF_EVENT)
998 zlog_info ("setting options on %s accordingly", IF_NAME (oi));
hasso8585d4e2004-04-20 17:25:12 +0000999#ifdef HAVE_NSSA
1000 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
1001#endif /* HAVE_NSSA */
paul718e3742002-12-13 20:15:29 +00001002 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
1003 if (IS_DEBUG_OSPF_EVENT)
1004 zlog_info ("options set on %s: %x",
1005 IF_NAME (oi), OPTIONS (oi));
1006 }
1007 break;
1008 case OSPF_AREA_NSSA:
1009#ifdef HAVE_NSSA
1010 for (node = listhead (area->oiflist); node; nextnode (node))
1011 if ((oi = getdata (node)) != NULL)
1012 if (oi->nbr_self != NULL)
1013 {
1014 zlog_info ("setting nssa options on %s accordingly", IF_NAME (oi));
1015 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
1016 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
1017 zlog_info ("options set on %s: %x", IF_NAME (oi), OPTIONS (oi));
1018 }
1019#endif /* HAVE_NSSA */
1020 break;
1021 default:
1022 break;
1023 }
1024
1025 ospf_router_lsa_timer_add (area);
paul68980082003-03-25 05:07:42 +00001026 ospf_schedule_abr_task (area->ospf);
paul718e3742002-12-13 20:15:29 +00001027}
1028
1029int
paul68980082003-03-25 05:07:42 +00001030ospf_area_shortcut_set (struct ospf *ospf, struct ospf_area *area, int mode)
paul718e3742002-12-13 20:15:29 +00001031{
1032 if (area->shortcut_configured == mode)
1033 return 0;
1034
1035 area->shortcut_configured = mode;
1036 ospf_router_lsa_timer_add (area);
paul68980082003-03-25 05:07:42 +00001037 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001038
paul68980082003-03-25 05:07:42 +00001039 ospf_area_check_free (ospf, area->area_id);
paul718e3742002-12-13 20:15:29 +00001040
1041 return 1;
1042}
1043
1044int
paul68980082003-03-25 05:07:42 +00001045ospf_area_shortcut_unset (struct ospf *ospf, struct ospf_area *area)
paul718e3742002-12-13 20:15:29 +00001046{
1047 area->shortcut_configured = OSPF_SHORTCUT_DEFAULT;
1048 ospf_router_lsa_timer_add (area);
paul68980082003-03-25 05:07:42 +00001049 ospf_area_check_free (ospf, area->area_id);
1050 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001051
1052 return 1;
1053}
1054
1055int
1056ospf_area_vlink_count (struct ospf *ospf, struct ospf_area *area)
1057{
1058 struct ospf_vl_data *vl;
1059 listnode node;
1060 int count = 0;
1061
1062 for (node = listhead (ospf->vlinks); node; nextnode (node))
1063 {
1064 vl = getdata (node);
1065 if (IPV4_ADDR_SAME (&vl->vl_area_id, &area->area_id))
1066 count++;
1067 }
1068
1069 return count;
1070}
1071
1072int
1073ospf_area_stub_set (struct ospf *ospf, struct in_addr area_id)
1074{
1075 struct ospf_area *area;
paul147193a2003-04-19 00:31:59 +00001076 int format = OSPF_AREA_ID_FORMAT_ADDRESS;
paul718e3742002-12-13 20:15:29 +00001077
paul68980082003-03-25 05:07:42 +00001078 area = ospf_area_get (ospf, area_id, format);
paul718e3742002-12-13 20:15:29 +00001079 if (ospf_area_vlink_count (ospf, area))
1080 return 0;
1081
1082 if (area->external_routing != OSPF_AREA_STUB)
1083 ospf_area_type_set (area, OSPF_AREA_STUB);
1084
1085 return 1;
1086}
1087
1088int
1089ospf_area_stub_unset (struct ospf *ospf, struct in_addr area_id)
1090{
1091 struct ospf_area *area;
1092
paul68980082003-03-25 05:07:42 +00001093 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001094 if (area == NULL)
1095 return 1;
1096
1097 if (area->external_routing == OSPF_AREA_STUB)
1098 ospf_area_type_set (area, OSPF_AREA_DEFAULT);
1099
paul68980082003-03-25 05:07:42 +00001100 ospf_area_check_free (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001101
1102 return 1;
1103}
1104
1105int
1106ospf_area_no_summary_set (struct ospf *ospf, struct in_addr area_id)
1107{
1108 struct ospf_area *area;
paul147193a2003-04-19 00:31:59 +00001109 int format = OSPF_AREA_ID_FORMAT_ADDRESS;
paul718e3742002-12-13 20:15:29 +00001110
paul68980082003-03-25 05:07:42 +00001111 area = ospf_area_get (ospf, area_id, format);
paul718e3742002-12-13 20:15:29 +00001112 area->no_summary = 1;
1113
1114 return 1;
1115}
1116
1117int
1118ospf_area_no_summary_unset (struct ospf *ospf, struct in_addr area_id)
1119{
1120 struct ospf_area *area;
1121
paul68980082003-03-25 05:07:42 +00001122 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001123 if (area == NULL)
1124 return 0;
1125
1126 area->no_summary = 0;
paul68980082003-03-25 05:07:42 +00001127 ospf_area_check_free (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001128
1129 return 1;
1130}
1131
1132int
1133ospf_area_nssa_set (struct ospf *ospf, struct in_addr area_id)
1134{
1135 struct ospf_area *area;
paul147193a2003-04-19 00:31:59 +00001136 int format = OSPF_AREA_ID_FORMAT_ADDRESS;
paul718e3742002-12-13 20:15:29 +00001137
paul68980082003-03-25 05:07:42 +00001138 area = ospf_area_get (ospf, area_id, format);
paul718e3742002-12-13 20:15:29 +00001139 if (ospf_area_vlink_count (ospf, area))
1140 return 0;
1141
1142 if (area->external_routing != OSPF_AREA_NSSA)
1143 {
1144 ospf_area_type_set (area, OSPF_AREA_NSSA);
1145 ospf->anyNSSA++;
1146 }
1147
paul084c7842003-06-22 08:35:18 +00001148 /* set NSSA area defaults */
1149 area->no_summary = 0;
1150 area->NSSATranslatorRole = OSPF_NSSA_ROLE_CANDIDATE;
pauld4a53d52003-07-12 21:30:57 +00001151 area->NSSATranslatorState = OSPF_NSSA_TRANSLATE_DISABLED;
paul084c7842003-06-22 08:35:18 +00001152 area->NSSATranslatorStabilityInterval = OSPF_NSSA_TRANS_STABLE_DEFAULT;
1153
paul718e3742002-12-13 20:15:29 +00001154 return 1;
1155}
1156
1157int
1158ospf_area_nssa_unset (struct ospf *ospf, struct in_addr area_id)
1159{
1160 struct ospf_area *area;
1161
paul68980082003-03-25 05:07:42 +00001162 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001163 if (area == NULL)
1164 return 0;
1165
1166 if (area->external_routing == OSPF_AREA_NSSA)
1167 {
1168 ospf->anyNSSA--;
1169 ospf_area_type_set (area, OSPF_AREA_DEFAULT);
1170 }
1171
paul68980082003-03-25 05:07:42 +00001172 ospf_area_check_free (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001173
1174 return 1;
1175}
1176
1177int
1178ospf_area_nssa_translator_role_set (struct ospf *ospf, struct in_addr area_id,
1179 int role)
1180{
1181 struct ospf_area *area;
1182
paul68980082003-03-25 05:07:42 +00001183 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001184 if (area == NULL)
1185 return 0;
1186
paul084c7842003-06-22 08:35:18 +00001187 area->NSSATranslatorRole = role;
paul718e3742002-12-13 20:15:29 +00001188
1189 return 1;
1190}
1191
1192int
1193ospf_area_nssa_translator_role_unset (struct ospf *ospf,
1194 struct in_addr area_id)
1195{
1196 struct ospf_area *area;
1197
paul68980082003-03-25 05:07:42 +00001198 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001199 if (area == NULL)
1200 return 0;
1201
paul084c7842003-06-22 08:35:18 +00001202 area->NSSATranslatorRole = OSPF_NSSA_ROLE_CANDIDATE;
paul718e3742002-12-13 20:15:29 +00001203
paul68980082003-03-25 05:07:42 +00001204 ospf_area_check_free (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001205
1206 return 1;
1207}
1208
1209int
paul68980082003-03-25 05:07:42 +00001210ospf_area_export_list_set (struct ospf *ospf,
1211 struct ospf_area *area, char *list_name)
paul718e3742002-12-13 20:15:29 +00001212{
1213 struct access_list *list;
1214 list = access_list_lookup (AFI_IP, list_name);
1215
1216 EXPORT_LIST (area) = list;
1217
1218 if (EXPORT_NAME (area))
1219 free (EXPORT_NAME (area));
1220
1221 EXPORT_NAME (area) = strdup (list_name);
paul68980082003-03-25 05:07:42 +00001222 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001223
1224 return 1;
1225}
1226
1227int
paul68980082003-03-25 05:07:42 +00001228ospf_area_export_list_unset (struct ospf *ospf, struct ospf_area * area)
paul718e3742002-12-13 20:15:29 +00001229{
1230
1231 EXPORT_LIST (area) = 0;
1232
1233 if (EXPORT_NAME (area))
1234 free (EXPORT_NAME (area));
1235
1236 EXPORT_NAME (area) = NULL;
1237
paul68980082003-03-25 05:07:42 +00001238 ospf_area_check_free (ospf, area->area_id);
paul718e3742002-12-13 20:15:29 +00001239
paul68980082003-03-25 05:07:42 +00001240 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001241
1242 return 1;
1243}
1244
1245int
paul68980082003-03-25 05:07:42 +00001246ospf_area_import_list_set (struct ospf *ospf,
1247 struct ospf_area *area, char *name)
paul718e3742002-12-13 20:15:29 +00001248{
1249 struct access_list *list;
1250 list = access_list_lookup (AFI_IP, name);
1251
1252 IMPORT_LIST (area) = list;
1253
1254 if (IMPORT_NAME (area))
1255 free (IMPORT_NAME (area));
1256
1257 IMPORT_NAME (area) = strdup (name);
paul68980082003-03-25 05:07:42 +00001258 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001259
1260 return 1;
1261}
1262
1263int
paul68980082003-03-25 05:07:42 +00001264ospf_area_import_list_unset (struct ospf *ospf, struct ospf_area * area)
paul718e3742002-12-13 20:15:29 +00001265{
1266 IMPORT_LIST (area) = 0;
1267
1268 if (IMPORT_NAME (area))
1269 free (IMPORT_NAME (area));
1270
1271 IMPORT_NAME (area) = NULL;
paul68980082003-03-25 05:07:42 +00001272 ospf_area_check_free (ospf, area->area_id);
paul718e3742002-12-13 20:15:29 +00001273
paul68980082003-03-25 05:07:42 +00001274 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001275
1276 return 1;
1277}
1278
1279int
1280ospf_timers_spf_set (struct ospf *ospf, u_int32_t delay, u_int32_t hold)
1281{
1282 ospf->spf_delay = delay;
1283 ospf->spf_holdtime = hold;
1284
1285 return 1;
1286}
1287
1288int
1289ospf_timers_spf_unset (struct ospf *ospf)
1290{
1291 ospf->spf_delay = OSPF_SPF_DELAY_DEFAULT;
1292 ospf->spf_holdtime = OSPF_SPF_HOLDTIME_DEFAULT;
1293
1294 return 1;
1295}
1296
1297int
1298ospf_timers_refresh_set (struct ospf *ospf, int interval)
1299{
1300 int time_left;
1301
1302 if (ospf->lsa_refresh_interval == interval)
1303 return 1;
1304
1305 time_left = ospf->lsa_refresh_interval -
1306 (time (NULL) - ospf->lsa_refresher_started);
1307
1308 if (time_left > interval)
1309 {
1310 OSPF_TIMER_OFF (ospf->t_lsa_refresher);
1311 ospf->t_lsa_refresher =
1312 thread_add_timer (master, ospf_lsa_refresh_walker, ospf, interval);
1313 }
1314 ospf->lsa_refresh_interval = interval;
1315
1316 return 1;
1317}
1318
1319int
1320ospf_timers_refresh_unset (struct ospf *ospf)
1321{
1322 int time_left;
1323
1324 time_left = ospf->lsa_refresh_interval -
1325 (time (NULL) - ospf->lsa_refresher_started);
1326
1327 if (time_left > OSPF_LSA_REFRESH_INTERVAL_DEFAULT)
1328 {
1329 OSPF_TIMER_OFF (ospf->t_lsa_refresher);
1330 ospf->t_lsa_refresher =
1331 thread_add_timer (master, ospf_lsa_refresh_walker, ospf,
1332 OSPF_LSA_REFRESH_INTERVAL_DEFAULT);
1333 }
1334
1335 ospf->lsa_refresh_interval = OSPF_LSA_REFRESH_INTERVAL_DEFAULT;
1336
1337 return 1;
1338}
1339
1340
1341struct ospf_nbr_nbma *
1342ospf_nbr_nbma_new ()
1343{
1344 struct ospf_nbr_nbma *nbr_nbma;
1345
1346 nbr_nbma = XMALLOC (MTYPE_OSPF_NEIGHBOR_STATIC,
1347 sizeof (struct ospf_nbr_nbma));
1348 memset (nbr_nbma, 0, sizeof (struct ospf_nbr_nbma));
1349
1350 nbr_nbma->priority = OSPF_NEIGHBOR_PRIORITY_DEFAULT;
1351 nbr_nbma->v_poll = OSPF_POLL_INTERVAL_DEFAULT;
1352
1353 return nbr_nbma;
1354}
1355
1356void
1357ospf_nbr_nbma_free (struct ospf_nbr_nbma *nbr_nbma)
1358{
1359 XFREE (MTYPE_OSPF_NEIGHBOR_STATIC, nbr_nbma);
1360}
1361
1362void
1363ospf_nbr_nbma_delete (struct ospf *ospf, struct ospf_nbr_nbma *nbr_nbma)
1364{
1365 struct route_node *rn;
1366 struct prefix_ipv4 p;
1367
1368 p.family = AF_INET;
1369 p.prefix = nbr_nbma->addr;
1370 p.prefixlen = IPV4_MAX_BITLEN;
1371
1372 rn = route_node_lookup (ospf->nbr_nbma, (struct prefix *)&p);
1373 if (rn)
1374 {
1375 ospf_nbr_nbma_free (rn->info);
1376 rn->info = NULL;
1377 route_unlock_node (rn);
1378 route_unlock_node (rn);
1379 }
1380}
1381
1382void
1383ospf_nbr_nbma_down (struct ospf_nbr_nbma *nbr_nbma)
1384{
1385 OSPF_TIMER_OFF (nbr_nbma->t_poll);
1386
1387 if (nbr_nbma->nbr)
1388 {
1389 nbr_nbma->nbr->nbr_nbma = NULL;
1390 OSPF_NSM_EVENT_EXECUTE (nbr_nbma->nbr, NSM_KillNbr);
1391 }
1392
1393 if (nbr_nbma->oi)
1394 listnode_delete (nbr_nbma->oi->nbr_nbma, nbr_nbma);
1395}
1396
1397void
1398ospf_nbr_nbma_add (struct ospf_nbr_nbma *nbr_nbma,
1399 struct ospf_interface *oi)
1400{
1401 struct ospf_neighbor *nbr;
1402 struct route_node *rn;
1403 struct prefix p;
1404
1405 if (oi->type != OSPF_IFTYPE_NBMA)
1406 return;
1407
1408 if (nbr_nbma->nbr != NULL)
1409 return;
1410
1411 if (IPV4_ADDR_SAME (&oi->nbr_self->address.u.prefix4, &nbr_nbma->addr))
1412 return;
1413
1414 nbr_nbma->oi = oi;
1415 listnode_add (oi->nbr_nbma, nbr_nbma);
1416
1417 /* Get neighbor information from table. */
1418 p.family = AF_INET;
1419 p.prefixlen = IPV4_MAX_BITLEN;
1420 p.u.prefix4 = nbr_nbma->addr;
1421
1422 rn = route_node_get (oi->nbrs, (struct prefix *)&p);
1423 if (rn->info)
1424 {
1425 nbr = rn->info;
1426 nbr->nbr_nbma = nbr_nbma;
1427 nbr_nbma->nbr = nbr;
1428
1429 route_unlock_node (rn);
1430 }
1431 else
1432 {
1433 nbr = rn->info = ospf_nbr_new (oi);
1434 nbr->state = NSM_Down;
1435 nbr->src = nbr_nbma->addr;
1436 nbr->nbr_nbma = nbr_nbma;
1437 nbr->priority = nbr_nbma->priority;
1438 nbr->address = p;
1439
1440 nbr_nbma->nbr = nbr;
1441
1442 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_Start);
1443 }
1444}
1445
1446void
paul68980082003-03-25 05:07:42 +00001447ospf_nbr_nbma_if_update (struct ospf *ospf, struct ospf_interface *oi)
paul718e3742002-12-13 20:15:29 +00001448{
1449 struct ospf_nbr_nbma *nbr_nbma;
1450 struct route_node *rn;
1451 struct prefix_ipv4 p;
1452
1453 if (oi->type != OSPF_IFTYPE_NBMA)
1454 return;
1455
paul68980082003-03-25 05:07:42 +00001456 for (rn = route_top (ospf->nbr_nbma); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +00001457 if ((nbr_nbma = rn->info))
1458 if (nbr_nbma->oi == NULL && nbr_nbma->nbr == NULL)
1459 {
1460 p.family = AF_INET;
1461 p.prefix = nbr_nbma->addr;
1462 p.prefixlen = IPV4_MAX_BITLEN;
1463
1464 if (prefix_match (oi->address, (struct prefix *)&p))
1465 ospf_nbr_nbma_add (nbr_nbma, oi);
1466 }
1467}
1468
1469struct ospf_nbr_nbma *
1470ospf_nbr_nbma_lookup (struct ospf *ospf, struct in_addr nbr_addr)
1471{
1472 struct route_node *rn;
1473 struct prefix_ipv4 p;
1474
1475 p.family = AF_INET;
1476 p.prefix = nbr_addr;
1477 p.prefixlen = IPV4_MAX_BITLEN;
1478
1479 rn = route_node_lookup (ospf->nbr_nbma, (struct prefix *)&p);
1480 if (rn)
1481 {
1482 route_unlock_node (rn);
1483 return rn->info;
1484 }
1485 return NULL;
1486}
1487
1488struct ospf_nbr_nbma *
paul68980082003-03-25 05:07:42 +00001489ospf_nbr_nbma_lookup_next (struct ospf *ospf, struct in_addr *addr, int first)
paul718e3742002-12-13 20:15:29 +00001490{
1491#if 0
1492 struct ospf_nbr_nbma *nbr_nbma;
1493 listnode node;
1494#endif
1495
paul68980082003-03-25 05:07:42 +00001496 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001497 return NULL;
1498
1499#if 0
paul68980082003-03-25 05:07:42 +00001500 for (node = listhead (ospf->nbr_nbma); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001501 {
1502 nbr_nbma = getdata (node);
1503
1504 if (first)
1505 {
1506 *addr = nbr_nbma->addr;
1507 return nbr_nbma;
1508 }
1509 else if (ntohl (nbr_nbma->addr.s_addr) > ntohl (addr->s_addr))
1510 {
1511 *addr = nbr_nbma->addr;
1512 return nbr_nbma;
1513 }
1514 }
1515#endif
1516 return NULL;
1517}
1518
1519int
1520ospf_nbr_nbma_set (struct ospf *ospf, struct in_addr nbr_addr)
1521{
1522 struct ospf_nbr_nbma *nbr_nbma;
1523 struct ospf_interface *oi;
1524 struct prefix_ipv4 p;
1525 struct route_node *rn;
1526 listnode node;
1527
1528 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1529 if (nbr_nbma)
1530 return 0;
1531
1532 nbr_nbma = ospf_nbr_nbma_new ();
1533 nbr_nbma->addr = nbr_addr;
1534
1535 p.family = AF_INET;
1536 p.prefix = nbr_addr;
1537 p.prefixlen = IPV4_MAX_BITLEN;
1538
1539 rn = route_node_get (ospf->nbr_nbma, (struct prefix *)&p);
1540 rn->info = nbr_nbma;
1541
1542 for (node = listhead (ospf->oiflist); node; nextnode (node))
1543 {
1544 oi = getdata (node);
1545 if (oi->type == OSPF_IFTYPE_NBMA)
1546 if (prefix_match (oi->address, (struct prefix *)&p))
1547 {
1548 ospf_nbr_nbma_add (nbr_nbma, oi);
1549 break;
1550 }
1551 }
1552
1553 return 1;
1554}
1555
1556int
1557ospf_nbr_nbma_unset (struct ospf *ospf, struct in_addr nbr_addr)
1558{
1559 struct ospf_nbr_nbma *nbr_nbma;
1560
1561 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1562 if (nbr_nbma == NULL)
1563 return 0;
1564
1565 ospf_nbr_nbma_down (nbr_nbma);
1566 ospf_nbr_nbma_delete (ospf, nbr_nbma);
1567
1568 return 1;
1569}
1570
1571int
1572ospf_nbr_nbma_priority_set (struct ospf *ospf, struct in_addr nbr_addr,
1573 u_char priority)
1574{
1575 struct ospf_nbr_nbma *nbr_nbma;
1576
1577 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1578 if (nbr_nbma == NULL)
1579 return 0;
1580
1581 if (nbr_nbma->priority != priority)
1582 nbr_nbma->priority = priority;
1583
1584 return 1;
1585}
1586
1587int
1588ospf_nbr_nbma_priority_unset (struct ospf *ospf, struct in_addr nbr_addr)
1589{
1590 struct ospf_nbr_nbma *nbr_nbma;
1591
1592 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1593 if (nbr_nbma == NULL)
1594 return 0;
1595
1596 if (nbr_nbma != OSPF_NEIGHBOR_PRIORITY_DEFAULT)
1597 nbr_nbma->priority = OSPF_NEIGHBOR_PRIORITY_DEFAULT;
1598
1599 return 1;
1600}
1601
1602int
1603ospf_nbr_nbma_poll_interval_set (struct ospf *ospf, struct in_addr nbr_addr,
1604 int interval)
1605{
1606 struct ospf_nbr_nbma *nbr_nbma;
1607
1608 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1609 if (nbr_nbma == NULL)
1610 return 0;
1611
1612 if (nbr_nbma->v_poll != interval)
1613 {
1614 nbr_nbma->v_poll = interval;
1615 if (nbr_nbma->oi && ospf_if_is_up (nbr_nbma->oi))
1616 {
1617 OSPF_TIMER_OFF (nbr_nbma->t_poll);
1618 OSPF_POLL_TIMER_ON (nbr_nbma->t_poll, ospf_poll_timer,
1619 nbr_nbma->v_poll);
1620 }
1621 }
1622
1623 return 1;
1624}
1625
1626int
1627ospf_nbr_nbma_poll_interval_unset (struct ospf *ospf, struct in_addr addr)
1628{
1629 struct ospf_nbr_nbma *nbr_nbma;
1630
1631 nbr_nbma = ospf_nbr_nbma_lookup (ospf, addr);
1632 if (nbr_nbma == NULL)
1633 return 0;
1634
1635 if (nbr_nbma->v_poll != OSPF_POLL_INTERVAL_DEFAULT)
1636 nbr_nbma->v_poll = OSPF_POLL_INTERVAL_DEFAULT;
1637
1638 return 1;
1639}
1640
1641
1642void
1643ospf_prefix_list_update (struct prefix_list *plist)
1644{
paul020709f2003-04-04 02:44:16 +00001645 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001646 struct ospf_area *area;
1647 listnode node;
1648 int abr_inv = 0;
1649
1650 /* If OSPF instatnce does not exist, return right now. */
paul020709f2003-04-04 02:44:16 +00001651 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001652 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001653 return;
1654
1655 /* Update Area prefix-list. */
paul68980082003-03-25 05:07:42 +00001656 for (node = listhead (ospf->areas); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001657 {
1658 area = getdata (node);
1659
1660 /* Update filter-list in. */
1661 if (PREFIX_NAME_IN (area))
1662 if (strcmp (PREFIX_NAME_IN (area), plist->name) == 0)
1663 {
1664 PREFIX_LIST_IN (area) =
1665 prefix_list_lookup (AFI_IP, PREFIX_NAME_IN (area));
1666 abr_inv++;
1667 }
1668
1669 /* Update filter-list out. */
1670 if (PREFIX_NAME_OUT (area))
1671 if (strcmp (PREFIX_NAME_OUT (area), plist->name) == 0)
1672 {
1673 PREFIX_LIST_IN (area) =
1674 prefix_list_lookup (AFI_IP, PREFIX_NAME_OUT (area));
1675 abr_inv++;
1676 }
1677 }
1678
1679 /* Schedule ABR tasks. */
paul020709f2003-04-04 02:44:16 +00001680 if (IS_OSPF_ABR (ospf) && abr_inv)
paul68980082003-03-25 05:07:42 +00001681 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001682}
1683
1684void
paul020709f2003-04-04 02:44:16 +00001685ospf_master_init ()
1686{
1687 memset (&ospf_master, 0, sizeof (struct ospf_master));
1688
1689 om = &ospf_master;
1690 om->ospf = list_new ();
1691 om->master = thread_master_create ();
1692 om->start_time = time (NULL);
1693}
1694
1695void
paul718e3742002-12-13 20:15:29 +00001696ospf_init ()
1697{
paul718e3742002-12-13 20:15:29 +00001698 prefix_list_add_hook (ospf_prefix_list_update);
1699 prefix_list_delete_hook (ospf_prefix_list_update);
1700}