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