blob: a98d5be044d0e60fb015e5a84b766de016eaebe2 [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
55/* OSPF instance top. */
56struct ospf *ospf_top;
57
58extern struct zclient *zclient;
59
60
paul68980082003-03-25 05:07:42 +000061void ospf_remove_vls_through_area (struct ospf *, struct ospf_area *);
62void ospf_network_free (struct ospf *, struct ospf_network *);
paul718e3742002-12-13 20:15:29 +000063void ospf_area_free (struct ospf_area *);
64void ospf_network_run (struct ospf *, struct prefix *, struct ospf_area *);
65
66/* Get Router ID from ospf interface list. */
67struct in_addr
68ospf_router_id_get (list if_list)
69{
70 listnode node;
71 struct in_addr router_id;
72
73 memset (&router_id, 0, sizeof (struct in_addr));
74
75 for (node = listhead (if_list); node; nextnode (node))
76 {
77 struct ospf_interface *oi = getdata (node);
78
79 if (!if_is_up (oi->ifp) ||
80 OSPF_IF_PARAM (oi, passive_interface) == OSPF_IF_PASSIVE)
81 continue;
82
83 /* Ignore virtual link interface. */
84 if (oi->type != OSPF_IFTYPE_VIRTUALLINK &&
85 oi->type != OSPF_IFTYPE_LOOPBACK)
86 if (IPV4_ADDR_CMP (&router_id, &oi->address->u.prefix4) < 0)
87 router_id = oi->address->u.prefix4;
88 }
89
90 return router_id;
91}
92
93#define OSPF_EXTERNAL_LSA_ORIGINATE_DELAY 1
94
95void
paul68980082003-03-25 05:07:42 +000096ospf_router_id_update (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +000097{
paul718e3742002-12-13 20:15:29 +000098 struct in_addr router_id, router_id_old;
paul68980082003-03-25 05:07:42 +000099 listnode node;
paul718e3742002-12-13 20:15:29 +0000100
101 if (IS_DEBUG_OSPF_EVENT)
paul68980082003-03-25 05:07:42 +0000102 zlog_info ("Router-ID[OLD:%s]: Update", inet_ntoa (ospf->router_id));
paul718e3742002-12-13 20:15:29 +0000103
paul68980082003-03-25 05:07:42 +0000104 router_id_old = ospf->router_id;
paul718e3742002-12-13 20:15:29 +0000105
paul68980082003-03-25 05:07:42 +0000106 if (ospf->router_id_static.s_addr != 0)
107 router_id = ospf->router_id_static;
paul718e3742002-12-13 20:15:29 +0000108 else
paul68980082003-03-25 05:07:42 +0000109 router_id = ospf_router_id_get (ospf->oiflist);
paul718e3742002-12-13 20:15:29 +0000110
paul68980082003-03-25 05:07:42 +0000111 ospf->router_id = router_id;
paul718e3742002-12-13 20:15:29 +0000112
113 if (IS_DEBUG_OSPF_EVENT)
paul68980082003-03-25 05:07:42 +0000114 zlog_info ("Router-ID[NEW:%s]: Update", inet_ntoa (ospf->router_id));
paul718e3742002-12-13 20:15:29 +0000115
116 if (!IPV4_ADDR_SAME (&router_id_old, &router_id))
117 {
paul68980082003-03-25 05:07:42 +0000118 for (node = listhead (ospf->oiflist); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +0000119 {
120 struct ospf_interface *oi = getdata (node);
121
122 /* Update self-neighbor's router_id. */
123 oi->nbr_self->router_id = router_id;
124 }
125
126 /* If AS-external-LSA is queued, then flush those LSAs. */
paul68980082003-03-25 05:07:42 +0000127 if (router_id_old.s_addr == 0 && ospf->external_origin)
paul718e3742002-12-13 20:15:29 +0000128 {
129 int type;
130 /* Originate each redistributed external route. */
131 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
paul68980082003-03-25 05:07:42 +0000132 if (ospf->external_origin & (1 << type))
paul718e3742002-12-13 20:15:29 +0000133 thread_add_event (master, ospf_external_lsa_originate_timer,
paul68980082003-03-25 05:07:42 +0000134 ospf, type);
paul718e3742002-12-13 20:15:29 +0000135 /* Originate Deafult. */
paul68980082003-03-25 05:07:42 +0000136 if (ospf->external_origin & (1 << ZEBRA_ROUTE_MAX))
paul718e3742002-12-13 20:15:29 +0000137 thread_add_event (master, ospf_default_originate_timer,
paul68980082003-03-25 05:07:42 +0000138 &ospf->default_originate, 0);
paul718e3742002-12-13 20:15:29 +0000139
paul68980082003-03-25 05:07:42 +0000140 ospf->external_origin = 0;
paul718e3742002-12-13 20:15:29 +0000141 }
142
paul68980082003-03-25 05:07:42 +0000143 OSPF_TIMER_ON (ospf->t_router_lsa_update,
paul718e3742002-12-13 20:15:29 +0000144 ospf_router_lsa_update_timer, OSPF_LSA_UPDATE_DELAY);
145 }
146}
147
148int
149ospf_router_id_update_timer (struct thread *thread)
150{
paul68980082003-03-25 05:07:42 +0000151 struct ospf *ospf = ospf_top;
152
paul718e3742002-12-13 20:15:29 +0000153 if (IS_DEBUG_OSPF_EVENT)
154 zlog_info ("Router-ID: Update timer fired!");
155
paul68980082003-03-25 05:07:42 +0000156 ospf->t_router_id_update = NULL;
157 ospf_router_id_update (ospf);
paul718e3742002-12-13 20:15:29 +0000158
159 return 0;
160}
161
162/* For OSPF area sort by area id. */
163int
164ospf_area_id_cmp (struct ospf_area *a1, struct ospf_area *a2)
165{
166 if (ntohl (a1->area_id.s_addr) > ntohl (a2->area_id.s_addr))
167 return 1;
168 if (ntohl (a1->area_id.s_addr) < ntohl (a2->area_id.s_addr))
169 return -1;
170 return 0;
171}
172
173/* Allocate new ospf structure. */
174struct ospf *
175ospf_new ()
176{
177 int i;
178
179 struct ospf *new = XCALLOC (MTYPE_OSPF_TOP, sizeof (struct ospf));
180
181 new->router_id.s_addr = htonl (0);
182 new->router_id_static.s_addr = htonl (0);
183
184 new->abr_type = OSPF_ABR_STAND;
185 new->iflist = iflist;
186 new->oiflist = list_new ();
187 new->vlinks = list_new ();
188 new->areas = list_new ();
189 new->areas->cmp = (int (*)(void *, void *)) ospf_area_id_cmp;
190 new->networks = route_table_init ();
191 new->nbr_nbma = route_table_init ();
192
193 new->lsdb = ospf_lsdb_new ();
194
195 new->default_originate = DEFAULT_ORIGINATE_NONE;
196
197 new->new_external_route = route_table_init ();
198 new->old_external_route = route_table_init ();
199 new->external_lsas = route_table_init ();
200
201 /* Distribute parameter init. */
202 for (i = 0; i <= ZEBRA_ROUTE_MAX; i++)
203 {
204 new->dmetric[i].type = -1;
205 new->dmetric[i].value = -1;
206 }
207 new->default_metric = -1;
208 new->ref_bandwidth = OSPF_DEFAULT_REF_BANDWIDTH;
209
210 /* SPF timer value init. */
211 new->spf_delay = OSPF_SPF_DELAY_DEFAULT;
212 new->spf_holdtime = OSPF_SPF_HOLDTIME_DEFAULT;
213
214 /* MaxAge init. */
215 new->maxage_lsa = list_new ();
216 new->t_maxage_walker =
217 thread_add_timer (master, ospf_lsa_maxage_walker,
paul68980082003-03-25 05:07:42 +0000218 new, OSPF_LSA_MAXAGE_CHECK_INTERVAL);
paul718e3742002-12-13 20:15:29 +0000219
220 /* Distance table init. */
221 new->distance_table = route_table_init ();
222
223 new->lsa_refresh_queue.index = 0;
224 new->lsa_refresh_interval = OSPF_LSA_REFRESH_INTERVAL_DEFAULT;
225 new->t_lsa_refresher = thread_add_timer (master, ospf_lsa_refresh_walker,
226 new, new->lsa_refresh_interval);
227 new->lsa_refresher_started = time (NULL);
228
229 new->fd = ospf_sock_init ();
230 if (new->fd >= 0)
231 new->t_read = thread_add_read (master, ospf_read, new, new->fd);
232 new->oi_write_q = list_new ();
233
234 return new;
235}
236
237struct ospf *
238ospf_get ()
239{
paul68980082003-03-25 05:07:42 +0000240 struct ospf *ospf = ospf_top;
paul718e3742002-12-13 20:15:29 +0000241
paul68980082003-03-25 05:07:42 +0000242 if (ospf != NULL)
243 return ospf;
paul718e3742002-12-13 20:15:29 +0000244
paul68980082003-03-25 05:07:42 +0000245 ospf = ospf_new ();
246
247 if (ospf->router_id_static.s_addr == 0)
248 ospf_router_id_update (ospf);
paul718e3742002-12-13 20:15:29 +0000249
250#ifdef HAVE_OPAQUE_LSA
paul68980082003-03-25 05:07:42 +0000251 ospf_opaque_type11_lsa_init (ospf);
paul718e3742002-12-13 20:15:29 +0000252#endif /* HAVE_OPAQUE_LSA */
253
paul68980082003-03-25 05:07:42 +0000254 ospf_top = ospf;
255
256 return ospf;
paul718e3742002-12-13 20:15:29 +0000257}
258
259void
260ospf_finish (struct ospf *ospf)
261{
262 struct route_node *rn;
263 struct ospf_nbr_nbma *nbr_nbma;
paul68980082003-03-25 05:07:42 +0000264 struct ospf_lsa *lsa;
paul718e3742002-12-13 20:15:29 +0000265 listnode node;
266 int i;
267
268#ifdef HAVE_OPAQUE_LSA
269 ospf_opaque_type11_lsa_term (ospf);
270#endif /* HAVE_OPAQUE_LSA */
271
272 /* Unredister redistribution */
273 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
274 ospf_redistribute_unset (i);
275
276 for (node = listhead (ospf->areas); node;)
277 {
278 struct ospf_area *area = getdata (node);
279 nextnode (node);
280
paul68980082003-03-25 05:07:42 +0000281 ospf_remove_vls_through_area (ospf, area);
paul718e3742002-12-13 20:15:29 +0000282 }
283
284 for (node = listhead (ospf->vlinks); node; )
285 {
286 struct ospf_vl_data *vl_data = node->data;
287 nextnode (node);
288
paul68980082003-03-25 05:07:42 +0000289 ospf_vl_delete (ospf, vl_data);
paul718e3742002-12-13 20:15:29 +0000290 }
291
292 list_delete (ospf->vlinks);
293
294 /* Reset interface. */
295 for (node = listhead (ospf->oiflist); node;)
296 {
297 struct ospf_interface *oi = getdata (node);
298 nextnode (node);
299
300 if (oi)
301 ospf_if_free (oi);
302 }
303
304 /* Clear static neighbors */
305 for (rn = route_top (ospf->nbr_nbma); rn; rn = route_next (rn))
306 if ((nbr_nbma = rn->info))
307 {
308 OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
309
310 if (nbr_nbma->nbr)
311 {
312 nbr_nbma->nbr->nbr_nbma = NULL;
313 nbr_nbma->nbr = NULL;
314 }
315
316 if (nbr_nbma->oi)
317 {
318 listnode_delete (nbr_nbma->oi->nbr_nbma, nbr_nbma);
319 nbr_nbma->oi = NULL;
320 }
321
322 XFREE (MTYPE_OSPF_NEIGHBOR_STATIC, nbr_nbma);
323 }
324
325 route_table_finish (ospf->nbr_nbma);
326
327 /* Clear networks and Areas. */
328 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
329 {
330 struct ospf_network *network;
331
332 if ((network = rn->info) != NULL)
333 {
paul68980082003-03-25 05:07:42 +0000334 ospf_network_free (ospf, network);
paul718e3742002-12-13 20:15:29 +0000335 rn->info = NULL;
336 route_unlock_node (rn);
337 }
338 }
339
340 for (node = listhead (ospf->areas); node;)
341 {
342 struct ospf_area *area = getdata (node);
343 nextnode (node);
344
345 listnode_delete (ospf->areas, area);
346 ospf_area_free (area);
347 }
348
349 /* Cancel all timers. */
350 OSPF_TIMER_OFF (ospf->t_external_lsa);
351 OSPF_TIMER_OFF (ospf->t_router_id_update);
352 OSPF_TIMER_OFF (ospf->t_router_lsa_update);
353 OSPF_TIMER_OFF (ospf->t_spf_calc);
354 OSPF_TIMER_OFF (ospf->t_ase_calc);
355 OSPF_TIMER_OFF (ospf->t_maxage);
356 OSPF_TIMER_OFF (ospf->t_maxage_walker);
357 OSPF_TIMER_OFF (ospf->t_abr_task);
358 OSPF_TIMER_OFF (ospf->t_distribute_update);
359 OSPF_TIMER_OFF (ospf->t_lsa_refresher);
360 OSPF_TIMER_OFF (ospf->t_read);
361 OSPF_TIMER_OFF (ospf->t_write);
362
363 close (ospf->fd);
364
365#ifdef HAVE_OPAQUE_LSA
paul68980082003-03-25 05:07:42 +0000366 LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
367 ospf_discard_from_db (ospf, ospf->lsdb, lsa);
paul718e3742002-12-13 20:15:29 +0000368#endif /* HAVE_OPAQUE_LSA */
paul68980082003-03-25 05:07:42 +0000369 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
370 ospf_discard_from_db (ospf, ospf->lsdb, lsa);
371
paul718e3742002-12-13 20:15:29 +0000372 ospf_lsdb_delete_all (ospf->lsdb);
373 ospf_lsdb_free (ospf->lsdb);
374
375 for (node = listhead (ospf->maxage_lsa); node; nextnode (node))
376 ospf_lsa_unlock (getdata (node));
377
378 list_delete (ospf->maxage_lsa);
379
380 if (ospf->old_table)
381 ospf_route_table_free (ospf->old_table);
382 if (ospf->new_table)
383 {
384 ospf_route_delete (ospf->new_table);
385 ospf_route_table_free (ospf->new_table);
386 }
387 if (ospf->old_rtrs)
388 ospf_rtrs_free (ospf->old_rtrs);
389 if (ospf->new_rtrs)
390 ospf_rtrs_free (ospf->new_rtrs);
391 if (ospf->new_external_route)
392 {
393 ospf_route_delete (ospf->new_external_route);
394 ospf_route_table_free (ospf->new_external_route);
395 }
396 if (ospf->old_external_route)
397 {
398 ospf_route_delete (ospf->old_external_route);
399 ospf_route_table_free (ospf->old_external_route);
400 }
401 if (ospf->external_lsas)
402 {
403 ospf_ase_external_lsas_finish (ospf->external_lsas);
404 }
405
406 list_delete (ospf->areas);
407
408 for (i = ZEBRA_ROUTE_SYSTEM; i <= ZEBRA_ROUTE_MAX; i++)
409 if (EXTERNAL_INFO (i) != NULL)
410 for (rn = route_top (EXTERNAL_INFO (i)); rn; rn = route_next (rn))
411 {
412 if (rn->info == NULL)
413 continue;
414
415 XFREE (MTYPE_OSPF_EXTERNAL_INFO, rn->info);
416 rn->info = NULL;
417 route_unlock_node (rn);
418 }
419
paul68980082003-03-25 05:07:42 +0000420 ospf_distance_reset (ospf);
paul718e3742002-12-13 20:15:29 +0000421 route_table_finish (ospf->distance_table);
422
423 XFREE (MTYPE_OSPF_TOP, ospf);
424
425 ospf_top = NULL;
426}
427
428
429/* allocate new OSPF Area object */
430struct ospf_area *
paul68980082003-03-25 05:07:42 +0000431ospf_area_new (struct ospf *ospf, struct in_addr area_id)
paul718e3742002-12-13 20:15:29 +0000432{
433 struct ospf_area *new;
434
435 /* Allocate new config_network. */
436 new = XCALLOC (MTYPE_OSPF_AREA, sizeof (struct ospf_area));
437
paul68980082003-03-25 05:07:42 +0000438 new->ospf = ospf;
paul718e3742002-12-13 20:15:29 +0000439
440 new->area_id = area_id;
441
442 new->external_routing = OSPF_AREA_DEFAULT;
443 new->default_cost = 1;
444 new->auth_type = OSPF_AUTH_NULL;
445
446 /* New LSDB init. */
447 new->lsdb = ospf_lsdb_new ();
448
449 /* Self-originated LSAs initialize. */
450 new->router_lsa_self = NULL;
451
452#ifdef HAVE_OPAQUE_LSA
453 ospf_opaque_type10_lsa_init (new);
454#endif /* HAVE_OPAQUE_LSA */
455
456 new->oiflist = list_new ();
457 new->ranges = route_table_init ();
458
459 if (area_id.s_addr == OSPF_AREA_BACKBONE)
paul68980082003-03-25 05:07:42 +0000460 ospf->backbone = new;
paul718e3742002-12-13 20:15:29 +0000461
462 return new;
463}
464
465void
466ospf_area_free (struct ospf_area *area)
467{
paul68980082003-03-25 05:07:42 +0000468 struct route_node *rn;
469 struct ospf_lsa *lsa;
470
paul718e3742002-12-13 20:15:29 +0000471 /* Free LSDBs. */
paul68980082003-03-25 05:07:42 +0000472 LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
473 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
474 LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
475 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
476 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
477 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
478 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
479 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
paul718e3742002-12-13 20:15:29 +0000480
481#ifdef HAVE_NSSA
paul68980082003-03-25 05:07:42 +0000482 LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
483 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
paul718e3742002-12-13 20:15:29 +0000484#endif /* HAVE_NSSA */
485#ifdef HAVE_OPAQUE_LSA
paul68980082003-03-25 05:07:42 +0000486 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
487 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
488 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
489 ospf_discard_from_db (area->ospf, area->lsdb, lsa);
paul718e3742002-12-13 20:15:29 +0000490#endif /* HAVE_OPAQUE_LSA */
491
492 ospf_lsdb_delete_all (area->lsdb);
493 ospf_lsdb_free (area->lsdb);
494
paul718e3742002-12-13 20:15:29 +0000495 ospf_lsa_unlock (area->router_lsa_self);
496
497 route_table_finish (area->ranges);
498 list_delete (area->oiflist);
499
500 if (EXPORT_NAME (area))
501 free (EXPORT_NAME (area));
502
503 if (IMPORT_NAME (area))
504 free (IMPORT_NAME (area));
505
506 /* Cancel timer. */
507 OSPF_TIMER_OFF (area->t_router_lsa_self);
508
509 if (OSPF_IS_AREA_BACKBONE (area))
paul68980082003-03-25 05:07:42 +0000510 area->ospf->backbone = NULL;
paul718e3742002-12-13 20:15:29 +0000511
512 XFREE (MTYPE_OSPF_AREA, area);
513}
514
515void
paul68980082003-03-25 05:07:42 +0000516ospf_area_check_free (struct ospf *ospf, struct in_addr area_id)
paul718e3742002-12-13 20:15:29 +0000517{
518 struct ospf_area *area;
519
paul68980082003-03-25 05:07:42 +0000520 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +0000521 if (area &&
522 listcount (area->oiflist) == 0 &&
523 area->ranges->top == NULL &&
524 area->shortcut_configured == OSPF_SHORTCUT_DEFAULT &&
525 area->external_routing == OSPF_AREA_DEFAULT &&
526 area->no_summary == 0 &&
527 area->default_cost == 1 &&
528 EXPORT_NAME (area) == NULL &&
529 IMPORT_NAME (area) == NULL &&
530 area->auth_type == OSPF_AUTH_NULL)
531 {
paul68980082003-03-25 05:07:42 +0000532 listnode_delete (ospf->areas, area);
paul718e3742002-12-13 20:15:29 +0000533 ospf_area_free (area);
534 }
535}
536
537struct ospf_area *
paul68980082003-03-25 05:07:42 +0000538ospf_area_get (struct ospf *ospf, struct in_addr area_id, int format)
paul718e3742002-12-13 20:15:29 +0000539{
540 struct ospf_area *area;
541
paul68980082003-03-25 05:07:42 +0000542 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +0000543 if (!area)
544 {
paul68980082003-03-25 05:07:42 +0000545 area = ospf_area_new (ospf, area_id);
paul718e3742002-12-13 20:15:29 +0000546 area->format = format;
paul68980082003-03-25 05:07:42 +0000547 listnode_add_sort (ospf->areas, area);
548 ospf_check_abr_status (ospf);
paul718e3742002-12-13 20:15:29 +0000549 }
550
551 return area;
552}
553
554struct ospf_area *
paul68980082003-03-25 05:07:42 +0000555ospf_area_lookup_by_area_id (struct ospf *ospf, struct in_addr area_id)
paul718e3742002-12-13 20:15:29 +0000556{
557 struct ospf_area *area;
558 listnode node;
559
paul68980082003-03-25 05:07:42 +0000560 for (node = listhead (ospf->areas); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +0000561 {
562 area = getdata (node);
563
564 if (IPV4_ADDR_SAME (&area->area_id, &area_id))
565 return area;
566 }
567
568 return NULL;
569}
570
571void
572ospf_area_add_if (struct ospf_area *area, struct ospf_interface *oi)
573{
574 listnode_add (area->oiflist, oi);
575}
576
577void
578ospf_area_del_if (struct ospf_area *area, struct ospf_interface *oi)
579{
580 listnode_delete (area->oiflist, oi);
581}
582
583
584/* Config network statement related functions. */
585struct ospf_network *
586ospf_network_new (struct in_addr area_id, int format)
587{
588 struct ospf_network *new;
589 new = XCALLOC (MTYPE_OSPF_NETWORK, sizeof (struct ospf_network));
590
591 new->area_id = area_id;
592 new->format = format;
593
594 return new;
595}
596
597void
paul68980082003-03-25 05:07:42 +0000598ospf_network_free (struct ospf *ospf, struct ospf_network *network)
paul718e3742002-12-13 20:15:29 +0000599{
paul68980082003-03-25 05:07:42 +0000600 ospf_area_check_free (ospf, network->area_id);
601 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +0000602 XFREE (MTYPE_OSPF_NETWORK, network);
603}
604
605int
606ospf_network_set (struct ospf *ospf, struct prefix_ipv4 *p,
607 struct in_addr area_id)
608{
609 struct ospf_network *network;
610 struct ospf_area *area;
611 struct route_node *rn;
612 struct external_info *ei;
613 int ret = OSPF_AREA_ID_FORMAT_DECIMAL;
614
615 rn = route_node_get (ospf->networks, (struct prefix *)p);
616 if (rn->info)
617 {
618 /* There is already same network statement. */
619 route_unlock_node (rn);
620 return 0;
621 }
622
623 rn->info = network = ospf_network_new (area_id, ret);
paul68980082003-03-25 05:07:42 +0000624 area = ospf_area_get (ospf, area_id, ret);
paul718e3742002-12-13 20:15:29 +0000625
626 /* Run network config now. */
627 ospf_network_run (ospf, (struct prefix *)p, area);
628
629 /* Update connected redistribute. */
630 if (ospf_is_type_redistributed (ZEBRA_ROUTE_CONNECT))
631 if (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT))
632 for (rn = route_top (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT));
633 rn; rn = route_next (rn))
634 if ((ei = rn->info) != NULL)
paul68980082003-03-25 05:07:42 +0000635 if (ospf_external_info_find_lsa (ospf, &ei->p))
636 if (!ospf_distribute_check_connected (ospf, ei))
637 ospf_external_lsa_flush (ospf, ei->type, &ei->p,
paul718e3742002-12-13 20:15:29 +0000638 ei->ifindex, ei->nexthop);
639
paul68980082003-03-25 05:07:42 +0000640 ospf_area_check_free (ospf, area_id);
paul718e3742002-12-13 20:15:29 +0000641
642 return 1;
643}
644
645int
646ospf_network_unset (struct ospf *ospf, struct prefix_ipv4 *p,
647 struct in_addr area_id)
648{
649 struct route_node *rn;
650 struct ospf_network *network;
651 struct external_info *ei;
652
653 rn = route_node_lookup (ospf->networks, (struct prefix *)p);
654 if (rn == NULL)
655 return 0;
656
657 network = rn->info;
658 if (!IPV4_ADDR_SAME (&area_id, &network->area_id))
659 return 0;
660
paul68980082003-03-25 05:07:42 +0000661 ospf_network_free (ospf, rn->info);
paul718e3742002-12-13 20:15:29 +0000662 rn->info = NULL;
663 route_unlock_node (rn);
664
paul68980082003-03-25 05:07:42 +0000665 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000666
667 /* Update connected redistribute. */
668 if (ospf_is_type_redistributed (ZEBRA_ROUTE_CONNECT))
669 if (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT))
670 for (rn = route_top (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT));
671 rn; rn = route_next (rn))
672 if ((ei = rn->info) != NULL)
paul68980082003-03-25 05:07:42 +0000673 if (!ospf_external_info_find_lsa (ospf, &ei->p))
674 if (ospf_distribute_check_connected (ospf, ei))
675 ospf_external_lsa_originate (ospf, ei);
paul718e3742002-12-13 20:15:29 +0000676
677 return 1;
678}
679
paul570f7592003-01-25 06:47:41 +0000680/* Check whether interface matches given network
681 * returns: 1, true. 0, false
682 */
683int
684ospf_network_match_iface(struct connected *co, struct prefix *net)
685{
686 /* Behaviour to match both Cisco where:
687 * iface address lies within network specified -> ospf
688 * and zebra 0.9[2ish-3]:
689 * PtP special case: network specified == iface peer addr -> ospf
690 */
691 return (
692 ((ifc_pointopoint (co) &&
693 IPV4_ADDR_SAME ( &(co->destination->u.prefix4), &(net->u.prefix4)))
694 || prefix_match (net, co->address))
695 ? 1 : 0
696 );
697}
698
paul718e3742002-12-13 20:15:29 +0000699void
700ospf_network_run (struct ospf *ospf, struct prefix *p, struct ospf_area *area)
701{
702 struct interface *ifp;
703 listnode node;
704
705 /* Schedule Router ID Update. */
706 if (ospf->router_id_static.s_addr == 0)
707 if (ospf->t_router_id_update == NULL)
708 {
709 ospf->t_router_id_update =
710 thread_add_timer (master, ospf_router_id_update_timer, ospf,
711 OSPF_ROUTER_ID_UPDATE_DELAY);
712 }
713
714 /* Get target interface. */
715 for (node = listhead (ospf->iflist); node; nextnode (node))
716 {
717 listnode cn;
718
719 if ((ifp = getdata (node)) == NULL)
720 continue;
721
722 if (memcmp (ifp->name, "VLINK", 5) == 0)
723 continue;
724
725 /* if interface prefix is match specified prefix,
726 then create socket and join multicast group. */
727 for (cn = listhead (ifp->connected); cn; nextnode (cn))
728 {
729 struct connected *co = getdata (cn);
730 struct prefix *addr;
paul800dc102003-03-28 01:51:40 +0000731
732 if (CHECK_FLAG(co->flags,ZEBRA_IFA_SECONDARY))
733 continue;
paul718e3742002-12-13 20:15:29 +0000734
paul00df0c12002-12-13 21:07:36 +0000735 if (ifc_pointopoint (co))
paul718e3742002-12-13 20:15:29 +0000736 addr = co->destination;
737 else
738 addr = co->address;
739
paulcb3f37d2003-02-18 23:26:37 +0000740 if (p->family == co->address->family
paul68980082003-03-25 05:07:42 +0000741 && ! ospf_if_is_configured (ospf, &(addr->u.prefix4))
paulcb3f37d2003-02-18 23:26:37 +0000742 && ospf_network_match_iface(co,p))
paul570f7592003-01-25 06:47:41 +0000743 {
paul487a5912003-02-19 02:54:57 +0000744 struct ospf_interface *oi;
745 assert(co);
paul718e3742002-12-13 20:15:29 +0000746
paul68980082003-03-25 05:07:42 +0000747 oi = ospf_if_new (ospf, ifp, co->address);
paul718e3742002-12-13 20:15:29 +0000748 oi->connected = co;
749
750 oi->nbr_self->address = *oi->address;
751
752 area->act_ints++;
753 oi->area = area;
754
755 oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4);
756 oi->output_cost = ospf_if_get_output_cost (oi);
757
758 if (area->external_routing != OSPF_AREA_DEFAULT)
759 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
760 oi->nbr_self->priority = OSPF_IF_PARAM (oi, priority);
761
762 /* Add pseudo neighbor. */
763 ospf_nbr_add_self (oi);
764
765 /* Make sure pseudo neighbor's router_id. */
paul68980082003-03-25 05:07:42 +0000766 oi->nbr_self->router_id = ospf->router_id;
paul718e3742002-12-13 20:15:29 +0000767 oi->nbr_self->src = oi->address->u.prefix4;
768
769 /* Relate ospf interface to ospf instance. */
paul68980082003-03-25 05:07:42 +0000770 oi->ospf = ospf;
paul718e3742002-12-13 20:15:29 +0000771
772 /* update network type as interface flag */
773 /* If network type is specified previously,
774 skip network type setting. */
775 oi->type = IF_DEF_PARAMS (ifp)->type;
776
777 /* Set area flag. */
778 switch (area->external_routing)
779 {
780 case OSPF_AREA_DEFAULT:
781 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
782 break;
783 case OSPF_AREA_STUB:
784 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
785 break;
786#ifdef HAVE_NSSA
787 case OSPF_AREA_NSSA:
788 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
789 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
790 break;
791#endif /* HAVE_NSSA */
792 }
793
794 ospf_area_add_if (oi->area, oi);
795
paul2e3b2e42002-12-13 21:03:13 +0000796 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +0000797 ospf_if_up (oi);
798
799 break;
800 }
801 }
802 }
803}
804
805void
806ospf_ls_upd_queue_empty (struct ospf_interface *oi)
807{
808 struct route_node *rn;
809 listnode node;
810 list lst;
811 struct ospf_lsa *lsa;
812
813 /* empty ls update queue */
814 for (rn = route_top (oi->ls_upd_queue); rn;
815 rn = route_next (rn))
816 if ((lst = (list) rn->info))
817 {
818 for (node = listhead (lst); node; nextnode (node))
819 if ((lsa = getdata (node)))
820 ospf_lsa_unlock (lsa);
821 list_free (lst);
822 rn->info = NULL;
823 }
824
825 /* remove update event */
826 if (oi->t_ls_upd_event)
827 {
828 thread_cancel (oi->t_ls_upd_event);
829 oi->t_ls_upd_event = NULL;
830 }
831}
832
833void
paul68980082003-03-25 05:07:42 +0000834ospf_if_update (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000835{
836 struct route_node *rn;
837 listnode node;
838 listnode next;
839 struct ospf_network *network;
840 struct ospf_area *area;
841
paul68980082003-03-25 05:07:42 +0000842 if (ospf != NULL)
paul718e3742002-12-13 20:15:29 +0000843 {
844 /* Update Router ID scheduled. */
paul68980082003-03-25 05:07:42 +0000845 if (ospf->router_id_static.s_addr == 0)
846 if (ospf->t_router_id_update == NULL)
paul718e3742002-12-13 20:15:29 +0000847 {
paul68980082003-03-25 05:07:42 +0000848 ospf->t_router_id_update =
paul718e3742002-12-13 20:15:29 +0000849 thread_add_timer (master, ospf_router_id_update_timer, NULL,
850 OSPF_ROUTER_ID_UPDATE_DELAY);
851 }
852
853 /* Find interfaces that not configured already. */
paul68980082003-03-25 05:07:42 +0000854 for (node = listhead (ospf->oiflist); node; node = next)
paul718e3742002-12-13 20:15:29 +0000855 {
856 int found = 0;
857 struct ospf_interface *oi = getdata (node);
858 struct connected *co = oi->connected;
859
860 next = nextnode (node);
861
862 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
863 continue;
864
paul68980082003-03-25 05:07:42 +0000865 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000866 {
867 if (rn->info == NULL)
868 continue;
869
paul570f7592003-01-25 06:47:41 +0000870 if (ospf_network_match_iface(co,&rn->p))
paul718e3742002-12-13 20:15:29 +0000871 {
872 found = 1;
873 route_unlock_node (rn);
874 break;
875 }
876 }
877
878 if (found == 0)
879 ospf_if_free (oi);
880 }
881
882 /* Run each interface. */
paul68980082003-03-25 05:07:42 +0000883 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000884 if (rn->info != NULL)
885 {
886 network = (struct ospf_network *) rn->info;
paul68980082003-03-25 05:07:42 +0000887 area = ospf_area_get (ospf, network->area_id, network->format);
888 ospf_network_run (ospf, &rn->p, area);
paul718e3742002-12-13 20:15:29 +0000889 }
890 }
891}
892
893void
paul68980082003-03-25 05:07:42 +0000894ospf_remove_vls_through_area (struct ospf *ospf, struct ospf_area *area)
paul718e3742002-12-13 20:15:29 +0000895{
896 listnode node, next;
897 struct ospf_vl_data *vl_data;
898
paul68980082003-03-25 05:07:42 +0000899 for (node = listhead (ospf->vlinks); node; node = next)
paul718e3742002-12-13 20:15:29 +0000900 {
901 next = node->next;
902 if ((vl_data = getdata (node)) != NULL)
903 if (IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
paul68980082003-03-25 05:07:42 +0000904 ospf_vl_delete (ospf, vl_data);
paul718e3742002-12-13 20:15:29 +0000905 }
906}
907
908
909struct message ospf_area_type_msg[] =
910{
911 { OSPF_AREA_DEFAULT, "Default" },
912 { OSPF_AREA_STUB, "Stub" },
913 { OSPF_AREA_NSSA, "NSSA" },
914};
915int ospf_area_type_msg_max = OSPF_AREA_TYPE_MAX;
916
917void
918ospf_area_type_set (struct ospf_area *area, int type)
919{
920 listnode node;
921 struct ospf_interface *oi;
922
923 if (area->external_routing == type)
924 {
925 if (IS_DEBUG_OSPF_EVENT)
926 zlog_info ("Area[%s]: Types are the same, ignored.",
927 inet_ntoa (area->area_id));
928 return;
929 }
930
931 area->external_routing = type;
932
933 if (IS_DEBUG_OSPF_EVENT)
934 zlog_info ("Area[%s]: Configured as %s", inet_ntoa (area->area_id),
935 LOOKUP (ospf_area_type_msg, type));
936
937 switch (area->external_routing)
938 {
939 case OSPF_AREA_DEFAULT:
940 for (node = listhead (area->oiflist); node; nextnode (node))
941 if ((oi = getdata (node)) != NULL)
942 if (oi->nbr_self != NULL)
943 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
944 break;
945 case OSPF_AREA_STUB:
946 for (node = listhead (area->oiflist); node; nextnode (node))
947 if ((oi = getdata (node)) != NULL)
948 if (oi->nbr_self != NULL)
949 {
950 if (IS_DEBUG_OSPF_EVENT)
951 zlog_info ("setting options on %s accordingly", IF_NAME (oi));
952 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
953 if (IS_DEBUG_OSPF_EVENT)
954 zlog_info ("options set on %s: %x",
955 IF_NAME (oi), OPTIONS (oi));
956 }
957 break;
958 case OSPF_AREA_NSSA:
959#ifdef HAVE_NSSA
960 for (node = listhead (area->oiflist); node; nextnode (node))
961 if ((oi = getdata (node)) != NULL)
962 if (oi->nbr_self != NULL)
963 {
964 zlog_info ("setting nssa options on %s accordingly", IF_NAME (oi));
965 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
966 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
967 zlog_info ("options set on %s: %x", IF_NAME (oi), OPTIONS (oi));
968 }
969#endif /* HAVE_NSSA */
970 break;
971 default:
972 break;
973 }
974
975 ospf_router_lsa_timer_add (area);
paul68980082003-03-25 05:07:42 +0000976 ospf_schedule_abr_task (area->ospf);
paul718e3742002-12-13 20:15:29 +0000977}
978
979int
paul68980082003-03-25 05:07:42 +0000980ospf_area_shortcut_set (struct ospf *ospf, struct ospf_area *area, int mode)
paul718e3742002-12-13 20:15:29 +0000981{
982 if (area->shortcut_configured == mode)
983 return 0;
984
985 area->shortcut_configured = mode;
986 ospf_router_lsa_timer_add (area);
paul68980082003-03-25 05:07:42 +0000987 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +0000988
paul68980082003-03-25 05:07:42 +0000989 ospf_area_check_free (ospf, area->area_id);
paul718e3742002-12-13 20:15:29 +0000990
991 return 1;
992}
993
994int
paul68980082003-03-25 05:07:42 +0000995ospf_area_shortcut_unset (struct ospf *ospf, struct ospf_area *area)
paul718e3742002-12-13 20:15:29 +0000996{
997 area->shortcut_configured = OSPF_SHORTCUT_DEFAULT;
998 ospf_router_lsa_timer_add (area);
paul68980082003-03-25 05:07:42 +0000999 ospf_area_check_free (ospf, area->area_id);
1000 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001001
1002 return 1;
1003}
1004
1005int
1006ospf_area_vlink_count (struct ospf *ospf, struct ospf_area *area)
1007{
1008 struct ospf_vl_data *vl;
1009 listnode node;
1010 int count = 0;
1011
1012 for (node = listhead (ospf->vlinks); node; nextnode (node))
1013 {
1014 vl = getdata (node);
1015 if (IPV4_ADDR_SAME (&vl->vl_area_id, &area->area_id))
1016 count++;
1017 }
1018
1019 return count;
1020}
1021
1022int
1023ospf_area_stub_set (struct ospf *ospf, struct in_addr area_id)
1024{
1025 struct ospf_area *area;
1026 int format = OSPF_AREA_ID_FORMAT_DECIMAL;
1027
paul68980082003-03-25 05:07:42 +00001028 area = ospf_area_get (ospf, area_id, format);
paul718e3742002-12-13 20:15:29 +00001029 if (ospf_area_vlink_count (ospf, area))
1030 return 0;
1031
1032 if (area->external_routing != OSPF_AREA_STUB)
1033 ospf_area_type_set (area, OSPF_AREA_STUB);
1034
1035 return 1;
1036}
1037
1038int
1039ospf_area_stub_unset (struct ospf *ospf, struct in_addr area_id)
1040{
1041 struct ospf_area *area;
1042
paul68980082003-03-25 05:07:42 +00001043 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001044 if (area == NULL)
1045 return 1;
1046
1047 if (area->external_routing == OSPF_AREA_STUB)
1048 ospf_area_type_set (area, OSPF_AREA_DEFAULT);
1049
paul68980082003-03-25 05:07:42 +00001050 ospf_area_check_free (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001051
1052 return 1;
1053}
1054
1055int
1056ospf_area_no_summary_set (struct ospf *ospf, struct in_addr area_id)
1057{
1058 struct ospf_area *area;
1059 int format = OSPF_AREA_ID_FORMAT_DECIMAL;
1060
paul68980082003-03-25 05:07:42 +00001061 area = ospf_area_get (ospf, area_id, format);
paul718e3742002-12-13 20:15:29 +00001062 area->no_summary = 1;
1063
1064 return 1;
1065}
1066
1067int
1068ospf_area_no_summary_unset (struct ospf *ospf, struct in_addr area_id)
1069{
1070 struct ospf_area *area;
1071
paul68980082003-03-25 05:07:42 +00001072 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001073 if (area == NULL)
1074 return 0;
1075
1076 area->no_summary = 0;
paul68980082003-03-25 05:07:42 +00001077 ospf_area_check_free (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001078
1079 return 1;
1080}
1081
1082int
1083ospf_area_nssa_set (struct ospf *ospf, struct in_addr area_id)
1084{
1085 struct ospf_area *area;
1086 int format = OSPF_AREA_ID_FORMAT_DECIMAL;
1087
paul68980082003-03-25 05:07:42 +00001088 area = ospf_area_get (ospf, area_id, format);
paul718e3742002-12-13 20:15:29 +00001089 if (ospf_area_vlink_count (ospf, area))
1090 return 0;
1091
1092 if (area->external_routing != OSPF_AREA_NSSA)
1093 {
1094 ospf_area_type_set (area, OSPF_AREA_NSSA);
1095 ospf->anyNSSA++;
1096 }
1097
1098 return 1;
1099}
1100
1101int
1102ospf_area_nssa_unset (struct ospf *ospf, struct in_addr area_id)
1103{
1104 struct ospf_area *area;
1105
paul68980082003-03-25 05:07:42 +00001106 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001107 if (area == NULL)
1108 return 0;
1109
1110 if (area->external_routing == OSPF_AREA_NSSA)
1111 {
1112 ospf->anyNSSA--;
1113 ospf_area_type_set (area, OSPF_AREA_DEFAULT);
1114 }
1115
paul68980082003-03-25 05:07:42 +00001116 ospf_area_check_free (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001117
1118 return 1;
1119}
1120
1121int
1122ospf_area_nssa_translator_role_set (struct ospf *ospf, struct in_addr area_id,
1123 int role)
1124{
1125 struct ospf_area *area;
1126
paul68980082003-03-25 05:07:42 +00001127 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001128 if (area == NULL)
1129 return 0;
1130
1131 area->NSSATranslator = role;
1132
1133 return 1;
1134}
1135
1136int
1137ospf_area_nssa_translator_role_unset (struct ospf *ospf,
1138 struct in_addr area_id)
1139{
1140 struct ospf_area *area;
1141
paul68980082003-03-25 05:07:42 +00001142 area = ospf_area_lookup_by_area_id (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001143 if (area == NULL)
1144 return 0;
1145
1146 area->NSSATranslator = OSPF_NSSA_ROLE_CANDIDATE;
1147
paul68980082003-03-25 05:07:42 +00001148 ospf_area_check_free (ospf, area_id);
paul718e3742002-12-13 20:15:29 +00001149
1150 return 1;
1151}
1152
1153int
paul68980082003-03-25 05:07:42 +00001154ospf_area_export_list_set (struct ospf *ospf,
1155 struct ospf_area *area, char *list_name)
paul718e3742002-12-13 20:15:29 +00001156{
1157 struct access_list *list;
1158 list = access_list_lookup (AFI_IP, list_name);
1159
1160 EXPORT_LIST (area) = list;
1161
1162 if (EXPORT_NAME (area))
1163 free (EXPORT_NAME (area));
1164
1165 EXPORT_NAME (area) = strdup (list_name);
paul68980082003-03-25 05:07:42 +00001166 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001167
1168 return 1;
1169}
1170
1171int
paul68980082003-03-25 05:07:42 +00001172ospf_area_export_list_unset (struct ospf *ospf, struct ospf_area * area)
paul718e3742002-12-13 20:15:29 +00001173{
1174
1175 EXPORT_LIST (area) = 0;
1176
1177 if (EXPORT_NAME (area))
1178 free (EXPORT_NAME (area));
1179
1180 EXPORT_NAME (area) = NULL;
1181
paul68980082003-03-25 05:07:42 +00001182 ospf_area_check_free (ospf, area->area_id);
paul718e3742002-12-13 20:15:29 +00001183
paul68980082003-03-25 05:07:42 +00001184 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001185
1186 return 1;
1187}
1188
1189int
paul68980082003-03-25 05:07:42 +00001190ospf_area_import_list_set (struct ospf *ospf,
1191 struct ospf_area *area, char *name)
paul718e3742002-12-13 20:15:29 +00001192{
1193 struct access_list *list;
1194 list = access_list_lookup (AFI_IP, name);
1195
1196 IMPORT_LIST (area) = list;
1197
1198 if (IMPORT_NAME (area))
1199 free (IMPORT_NAME (area));
1200
1201 IMPORT_NAME (area) = strdup (name);
paul68980082003-03-25 05:07:42 +00001202 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001203
1204 return 1;
1205}
1206
1207int
paul68980082003-03-25 05:07:42 +00001208ospf_area_import_list_unset (struct ospf *ospf, struct ospf_area * area)
paul718e3742002-12-13 20:15:29 +00001209{
1210 IMPORT_LIST (area) = 0;
1211
1212 if (IMPORT_NAME (area))
1213 free (IMPORT_NAME (area));
1214
1215 IMPORT_NAME (area) = NULL;
paul68980082003-03-25 05:07:42 +00001216 ospf_area_check_free (ospf, area->area_id);
paul718e3742002-12-13 20:15:29 +00001217
paul68980082003-03-25 05:07:42 +00001218 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001219
1220 return 1;
1221}
1222
1223int
1224ospf_timers_spf_set (struct ospf *ospf, u_int32_t delay, u_int32_t hold)
1225{
1226 ospf->spf_delay = delay;
1227 ospf->spf_holdtime = hold;
1228
1229 return 1;
1230}
1231
1232int
1233ospf_timers_spf_unset (struct ospf *ospf)
1234{
1235 ospf->spf_delay = OSPF_SPF_DELAY_DEFAULT;
1236 ospf->spf_holdtime = OSPF_SPF_HOLDTIME_DEFAULT;
1237
1238 return 1;
1239}
1240
1241int
1242ospf_timers_refresh_set (struct ospf *ospf, int interval)
1243{
1244 int time_left;
1245
1246 if (ospf->lsa_refresh_interval == interval)
1247 return 1;
1248
1249 time_left = ospf->lsa_refresh_interval -
1250 (time (NULL) - ospf->lsa_refresher_started);
1251
1252 if (time_left > interval)
1253 {
1254 OSPF_TIMER_OFF (ospf->t_lsa_refresher);
1255 ospf->t_lsa_refresher =
1256 thread_add_timer (master, ospf_lsa_refresh_walker, ospf, interval);
1257 }
1258 ospf->lsa_refresh_interval = interval;
1259
1260 return 1;
1261}
1262
1263int
1264ospf_timers_refresh_unset (struct ospf *ospf)
1265{
1266 int time_left;
1267
1268 time_left = ospf->lsa_refresh_interval -
1269 (time (NULL) - ospf->lsa_refresher_started);
1270
1271 if (time_left > OSPF_LSA_REFRESH_INTERVAL_DEFAULT)
1272 {
1273 OSPF_TIMER_OFF (ospf->t_lsa_refresher);
1274 ospf->t_lsa_refresher =
1275 thread_add_timer (master, ospf_lsa_refresh_walker, ospf,
1276 OSPF_LSA_REFRESH_INTERVAL_DEFAULT);
1277 }
1278
1279 ospf->lsa_refresh_interval = OSPF_LSA_REFRESH_INTERVAL_DEFAULT;
1280
1281 return 1;
1282}
1283
1284
1285struct ospf_nbr_nbma *
1286ospf_nbr_nbma_new ()
1287{
1288 struct ospf_nbr_nbma *nbr_nbma;
1289
1290 nbr_nbma = XMALLOC (MTYPE_OSPF_NEIGHBOR_STATIC,
1291 sizeof (struct ospf_nbr_nbma));
1292 memset (nbr_nbma, 0, sizeof (struct ospf_nbr_nbma));
1293
1294 nbr_nbma->priority = OSPF_NEIGHBOR_PRIORITY_DEFAULT;
1295 nbr_nbma->v_poll = OSPF_POLL_INTERVAL_DEFAULT;
1296
1297 return nbr_nbma;
1298}
1299
1300void
1301ospf_nbr_nbma_free (struct ospf_nbr_nbma *nbr_nbma)
1302{
1303 XFREE (MTYPE_OSPF_NEIGHBOR_STATIC, nbr_nbma);
1304}
1305
1306void
1307ospf_nbr_nbma_delete (struct ospf *ospf, struct ospf_nbr_nbma *nbr_nbma)
1308{
1309 struct route_node *rn;
1310 struct prefix_ipv4 p;
1311
1312 p.family = AF_INET;
1313 p.prefix = nbr_nbma->addr;
1314 p.prefixlen = IPV4_MAX_BITLEN;
1315
1316 rn = route_node_lookup (ospf->nbr_nbma, (struct prefix *)&p);
1317 if (rn)
1318 {
1319 ospf_nbr_nbma_free (rn->info);
1320 rn->info = NULL;
1321 route_unlock_node (rn);
1322 route_unlock_node (rn);
1323 }
1324}
1325
1326void
1327ospf_nbr_nbma_down (struct ospf_nbr_nbma *nbr_nbma)
1328{
1329 OSPF_TIMER_OFF (nbr_nbma->t_poll);
1330
1331 if (nbr_nbma->nbr)
1332 {
1333 nbr_nbma->nbr->nbr_nbma = NULL;
1334 OSPF_NSM_EVENT_EXECUTE (nbr_nbma->nbr, NSM_KillNbr);
1335 }
1336
1337 if (nbr_nbma->oi)
1338 listnode_delete (nbr_nbma->oi->nbr_nbma, nbr_nbma);
1339}
1340
1341void
1342ospf_nbr_nbma_add (struct ospf_nbr_nbma *nbr_nbma,
1343 struct ospf_interface *oi)
1344{
1345 struct ospf_neighbor *nbr;
1346 struct route_node *rn;
1347 struct prefix p;
1348
1349 if (oi->type != OSPF_IFTYPE_NBMA)
1350 return;
1351
1352 if (nbr_nbma->nbr != NULL)
1353 return;
1354
1355 if (IPV4_ADDR_SAME (&oi->nbr_self->address.u.prefix4, &nbr_nbma->addr))
1356 return;
1357
1358 nbr_nbma->oi = oi;
1359 listnode_add (oi->nbr_nbma, nbr_nbma);
1360
1361 /* Get neighbor information from table. */
1362 p.family = AF_INET;
1363 p.prefixlen = IPV4_MAX_BITLEN;
1364 p.u.prefix4 = nbr_nbma->addr;
1365
1366 rn = route_node_get (oi->nbrs, (struct prefix *)&p);
1367 if (rn->info)
1368 {
1369 nbr = rn->info;
1370 nbr->nbr_nbma = nbr_nbma;
1371 nbr_nbma->nbr = nbr;
1372
1373 route_unlock_node (rn);
1374 }
1375 else
1376 {
1377 nbr = rn->info = ospf_nbr_new (oi);
1378 nbr->state = NSM_Down;
1379 nbr->src = nbr_nbma->addr;
1380 nbr->nbr_nbma = nbr_nbma;
1381 nbr->priority = nbr_nbma->priority;
1382 nbr->address = p;
1383
1384 nbr_nbma->nbr = nbr;
1385
1386 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_Start);
1387 }
1388}
1389
1390void
paul68980082003-03-25 05:07:42 +00001391ospf_nbr_nbma_if_update (struct ospf *ospf, struct ospf_interface *oi)
paul718e3742002-12-13 20:15:29 +00001392{
1393 struct ospf_nbr_nbma *nbr_nbma;
1394 struct route_node *rn;
1395 struct prefix_ipv4 p;
1396
1397 if (oi->type != OSPF_IFTYPE_NBMA)
1398 return;
1399
paul68980082003-03-25 05:07:42 +00001400 for (rn = route_top (ospf->nbr_nbma); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +00001401 if ((nbr_nbma = rn->info))
1402 if (nbr_nbma->oi == NULL && nbr_nbma->nbr == NULL)
1403 {
1404 p.family = AF_INET;
1405 p.prefix = nbr_nbma->addr;
1406 p.prefixlen = IPV4_MAX_BITLEN;
1407
1408 if (prefix_match (oi->address, (struct prefix *)&p))
1409 ospf_nbr_nbma_add (nbr_nbma, oi);
1410 }
1411}
1412
1413struct ospf_nbr_nbma *
1414ospf_nbr_nbma_lookup (struct ospf *ospf, struct in_addr nbr_addr)
1415{
1416 struct route_node *rn;
1417 struct prefix_ipv4 p;
1418
1419 p.family = AF_INET;
1420 p.prefix = nbr_addr;
1421 p.prefixlen = IPV4_MAX_BITLEN;
1422
1423 rn = route_node_lookup (ospf->nbr_nbma, (struct prefix *)&p);
1424 if (rn)
1425 {
1426 route_unlock_node (rn);
1427 return rn->info;
1428 }
1429 return NULL;
1430}
1431
1432struct ospf_nbr_nbma *
paul68980082003-03-25 05:07:42 +00001433ospf_nbr_nbma_lookup_next (struct ospf *ospf, struct in_addr *addr, int first)
paul718e3742002-12-13 20:15:29 +00001434{
1435#if 0
1436 struct ospf_nbr_nbma *nbr_nbma;
1437 listnode node;
1438#endif
1439
paul68980082003-03-25 05:07:42 +00001440 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001441 return NULL;
1442
1443#if 0
paul68980082003-03-25 05:07:42 +00001444 for (node = listhead (ospf->nbr_nbma); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001445 {
1446 nbr_nbma = getdata (node);
1447
1448 if (first)
1449 {
1450 *addr = nbr_nbma->addr;
1451 return nbr_nbma;
1452 }
1453 else if (ntohl (nbr_nbma->addr.s_addr) > ntohl (addr->s_addr))
1454 {
1455 *addr = nbr_nbma->addr;
1456 return nbr_nbma;
1457 }
1458 }
1459#endif
1460 return NULL;
1461}
1462
1463int
1464ospf_nbr_nbma_set (struct ospf *ospf, struct in_addr nbr_addr)
1465{
1466 struct ospf_nbr_nbma *nbr_nbma;
1467 struct ospf_interface *oi;
1468 struct prefix_ipv4 p;
1469 struct route_node *rn;
1470 listnode node;
1471
1472 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1473 if (nbr_nbma)
1474 return 0;
1475
1476 nbr_nbma = ospf_nbr_nbma_new ();
1477 nbr_nbma->addr = nbr_addr;
1478
1479 p.family = AF_INET;
1480 p.prefix = nbr_addr;
1481 p.prefixlen = IPV4_MAX_BITLEN;
1482
1483 rn = route_node_get (ospf->nbr_nbma, (struct prefix *)&p);
1484 rn->info = nbr_nbma;
1485
1486 for (node = listhead (ospf->oiflist); node; nextnode (node))
1487 {
1488 oi = getdata (node);
1489 if (oi->type == OSPF_IFTYPE_NBMA)
1490 if (prefix_match (oi->address, (struct prefix *)&p))
1491 {
1492 ospf_nbr_nbma_add (nbr_nbma, oi);
1493 break;
1494 }
1495 }
1496
1497 return 1;
1498}
1499
1500int
1501ospf_nbr_nbma_unset (struct ospf *ospf, struct in_addr nbr_addr)
1502{
1503 struct ospf_nbr_nbma *nbr_nbma;
1504
1505 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1506 if (nbr_nbma == NULL)
1507 return 0;
1508
1509 ospf_nbr_nbma_down (nbr_nbma);
1510 ospf_nbr_nbma_delete (ospf, nbr_nbma);
1511
1512 return 1;
1513}
1514
1515int
1516ospf_nbr_nbma_priority_set (struct ospf *ospf, struct in_addr nbr_addr,
1517 u_char priority)
1518{
1519 struct ospf_nbr_nbma *nbr_nbma;
1520
1521 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1522 if (nbr_nbma == NULL)
1523 return 0;
1524
1525 if (nbr_nbma->priority != priority)
1526 nbr_nbma->priority = priority;
1527
1528 return 1;
1529}
1530
1531int
1532ospf_nbr_nbma_priority_unset (struct ospf *ospf, struct in_addr nbr_addr)
1533{
1534 struct ospf_nbr_nbma *nbr_nbma;
1535
1536 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1537 if (nbr_nbma == NULL)
1538 return 0;
1539
1540 if (nbr_nbma != OSPF_NEIGHBOR_PRIORITY_DEFAULT)
1541 nbr_nbma->priority = OSPF_NEIGHBOR_PRIORITY_DEFAULT;
1542
1543 return 1;
1544}
1545
1546int
1547ospf_nbr_nbma_poll_interval_set (struct ospf *ospf, struct in_addr nbr_addr,
1548 int interval)
1549{
1550 struct ospf_nbr_nbma *nbr_nbma;
1551
1552 nbr_nbma = ospf_nbr_nbma_lookup (ospf, nbr_addr);
1553 if (nbr_nbma == NULL)
1554 return 0;
1555
1556 if (nbr_nbma->v_poll != interval)
1557 {
1558 nbr_nbma->v_poll = interval;
1559 if (nbr_nbma->oi && ospf_if_is_up (nbr_nbma->oi))
1560 {
1561 OSPF_TIMER_OFF (nbr_nbma->t_poll);
1562 OSPF_POLL_TIMER_ON (nbr_nbma->t_poll, ospf_poll_timer,
1563 nbr_nbma->v_poll);
1564 }
1565 }
1566
1567 return 1;
1568}
1569
1570int
1571ospf_nbr_nbma_poll_interval_unset (struct ospf *ospf, struct in_addr addr)
1572{
1573 struct ospf_nbr_nbma *nbr_nbma;
1574
1575 nbr_nbma = ospf_nbr_nbma_lookup (ospf, addr);
1576 if (nbr_nbma == NULL)
1577 return 0;
1578
1579 if (nbr_nbma->v_poll != OSPF_POLL_INTERVAL_DEFAULT)
1580 nbr_nbma->v_poll = OSPF_POLL_INTERVAL_DEFAULT;
1581
1582 return 1;
1583}
1584
1585
1586void
1587ospf_prefix_list_update (struct prefix_list *plist)
1588{
paul68980082003-03-25 05:07:42 +00001589 struct ospf *ospf = ospf_top;
paul718e3742002-12-13 20:15:29 +00001590 struct ospf_area *area;
1591 listnode node;
1592 int abr_inv = 0;
1593
1594 /* If OSPF instatnce does not exist, return right now. */
paul68980082003-03-25 05:07:42 +00001595 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001596 return;
1597
1598 /* Update Area prefix-list. */
paul68980082003-03-25 05:07:42 +00001599 for (node = listhead (ospf->areas); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001600 {
1601 area = getdata (node);
1602
1603 /* Update filter-list in. */
1604 if (PREFIX_NAME_IN (area))
1605 if (strcmp (PREFIX_NAME_IN (area), plist->name) == 0)
1606 {
1607 PREFIX_LIST_IN (area) =
1608 prefix_list_lookup (AFI_IP, PREFIX_NAME_IN (area));
1609 abr_inv++;
1610 }
1611
1612 /* Update filter-list out. */
1613 if (PREFIX_NAME_OUT (area))
1614 if (strcmp (PREFIX_NAME_OUT (area), plist->name) == 0)
1615 {
1616 PREFIX_LIST_IN (area) =
1617 prefix_list_lookup (AFI_IP, PREFIX_NAME_OUT (area));
1618 abr_inv++;
1619 }
1620 }
1621
1622 /* Schedule ABR tasks. */
1623 if (OSPF_IS_ABR && abr_inv)
paul68980082003-03-25 05:07:42 +00001624 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001625}
1626
1627void
1628ospf_init ()
1629{
1630 /* Make empty list of ospf list. */
1631 ospf_top = NULL;
1632
1633 prefix_list_add_hook (ospf_prefix_list_update);
1634 prefix_list_delete_hook (ospf_prefix_list_update);
1635}