]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospfd.c
Merge pull request #901 from qlyoung/style-fix
[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 are not configured already. */
980 for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi)) {
981
982 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
983 continue;
984
985 ospf_network_run_subnet(ospf, oi->connected, NULL, NULL);
986 }
987
988 /* Update connected redistribute. */
989 update_redistributed(ospf, 0); /* interfaces possibly removed */
990 ospf_area_check_free(ospf, area_id);
991
992 return 1;
993 }
994
995 /* Ensure there's an OSPF instance, as "ip ospf area" enabled OSPF means
996 * there might not be any 'router ospf' config.
997 *
998 * Otherwise, doesn't do anything different to ospf_if_update for now
999 */
1000 void ospf_interface_area_set(struct interface *ifp)
1001 {
1002 struct ospf *ospf = ospf_get();
1003
1004 ospf_if_update(ospf, ifp);
1005 /* if_update does a update_redistributed */
1006
1007 return;
1008 }
1009
1010 void ospf_interface_area_unset(struct interface *ifp)
1011 {
1012 struct route_node *rn_oi;
1013 struct ospf *ospf;
1014
1015 ospf = ospf_lookup();
1016 if (!ospf)
1017 return; /* Ospf not ready yet */
1018
1019 /* Find interfaces that may need to be removed. */
1020 for (rn_oi = route_top(IF_OIFS(ifp)); rn_oi;
1021 rn_oi = route_next(rn_oi)) {
1022 struct ospf_interface *oi;
1023
1024 if ((oi = rn_oi->info) == NULL)
1025 continue;
1026
1027 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
1028 continue;
1029
1030 ospf_network_run_subnet(ospf, oi->connected, NULL, NULL);
1031 }
1032
1033 /* Update connected redistribute. */
1034 update_redistributed(ospf, 0); /* interfaces possibly removed */
1035 }
1036
1037 /* Check whether interface matches given network
1038 * returns: 1, true. 0, false
1039 */
1040 static int ospf_network_match_iface(const struct connected *co,
1041 const struct prefix *net)
1042 {
1043 /* new approach: more elegant and conceptually clean */
1044 return prefix_match_network_statement(net, CONNECTED_PREFIX(co));
1045 }
1046
1047 static void ospf_update_interface_area(struct connected *co,
1048 struct ospf_area *area)
1049 {
1050 struct ospf_interface *oi = ospf_if_table_lookup(co->ifp, co->address);
1051
1052 /* nothing to be done case */
1053 if (oi && oi->area == area) {
1054 return;
1055 }
1056
1057 if (oi)
1058 ospf_if_free(oi);
1059
1060 add_ospf_interface(co, area);
1061 }
1062
1063 /* Run OSPF for the given subnet, taking into account the following
1064 * possible sources of area configuration, in the given order of preference:
1065 *
1066 * - Whether there is interface+address specific area configuration
1067 * - Whether there is a default area for the interface
1068 * - Whether there is an area given as a parameter.
1069 * - If no specific network prefix/area is supplied, whether there's
1070 * a matching network configured.
1071 */
1072 static void ospf_network_run_subnet(struct ospf *ospf, struct connected *co,
1073 struct prefix *p,
1074 struct ospf_area *given_area)
1075 {
1076 struct ospf_interface *oi;
1077 struct ospf_if_params *params;
1078 struct ospf_area *area = NULL;
1079 struct route_node *rn;
1080 int configed = 0;
1081
1082 if (CHECK_FLAG(co->flags, ZEBRA_IFA_SECONDARY))
1083 return;
1084
1085 if (co->address->family != AF_INET)
1086 return;
1087
1088 /* Try determine the appropriate area for this interface + address
1089 * Start by checking interface config
1090 */
1091 params = ospf_lookup_if_params(co->ifp, co->address->u.prefix4);
1092 if (params && OSPF_IF_PARAM_CONFIGURED(params, if_area))
1093 area = ospf_area_get(ospf, params->if_area);
1094 else {
1095 params = IF_DEF_PARAMS(co->ifp);
1096 if (OSPF_IF_PARAM_CONFIGURED(params, if_area))
1097 area = ospf_area_get(ospf, params->if_area);
1098 }
1099
1100 /* If we've found an interface and/or addr specific area, then we're
1101 * done
1102 */
1103 if (area) {
1104 ospf_update_interface_area(co, area);
1105 return;
1106 }
1107
1108 /* Otherwise, only remaining possibility is a matching network statement
1109 */
1110 if (p) {
1111 assert(given_area != NULL);
1112
1113 /* Which either was supplied as a parameter.. (e.g. cause a new
1114 * network/area was just added)..
1115 */
1116 if (p->family == co->address->family
1117 && ospf_network_match_iface(co, p))
1118 ospf_update_interface_area(co, given_area);
1119
1120 return;
1121 }
1122
1123 /* Else we have to search the existing network/area config to see
1124 * if any match..
1125 */
1126 for (rn = route_top(ospf->networks); rn; rn = route_next(rn))
1127 if (rn->info != NULL && ospf_network_match_iface(co, &rn->p)) {
1128 struct ospf_network *network =
1129 (struct ospf_network *)rn->info;
1130 area = ospf_area_get(ospf, network->area_id);
1131 ospf_update_interface_area(co, area);
1132 configed = 1;
1133 }
1134
1135 /* If the subnet isn't in any area, deconfigure */
1136 if (!configed && (oi = ospf_if_table_lookup(co->ifp, co->address)))
1137 ospf_if_free(oi);
1138 }
1139
1140 static void ospf_network_run_interface(struct ospf *ospf, struct interface *ifp,
1141 struct prefix *p,
1142 struct ospf_area *given_area)
1143 {
1144 struct listnode *cnode;
1145 struct connected *co;
1146
1147 if (memcmp(ifp->name, "VLINK", 5) == 0)
1148 return;
1149
1150 /* Network prefix without area is nonsensical */
1151 if (p)
1152 assert(given_area != NULL);
1153
1154 /* if interface prefix is match specified prefix,
1155 then create socket and join multicast group. */
1156 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, co))
1157 ospf_network_run_subnet(ospf, co, p, given_area);
1158 }
1159
1160 static void ospf_network_run(struct prefix *p, struct ospf_area *area)
1161 {
1162 struct interface *ifp;
1163 struct listnode *node;
1164
1165 /* Schedule Router ID Update. */
1166 if (area->ospf->router_id.s_addr == 0)
1167 ospf_router_id_update(area->ospf);
1168
1169 /* Get target interface. */
1170 for (ALL_LIST_ELEMENTS_RO(om->iflist, node, ifp))
1171 ospf_network_run_interface(area->ospf, ifp, p, area);
1172 }
1173
1174 void ospf_ls_upd_queue_empty(struct ospf_interface *oi)
1175 {
1176 struct route_node *rn;
1177 struct listnode *node, *nnode;
1178 struct list *lst;
1179 struct ospf_lsa *lsa;
1180
1181 /* empty ls update queue */
1182 for (rn = route_top(oi->ls_upd_queue); rn; rn = route_next(rn))
1183 if ((lst = (struct list *)rn->info)) {
1184 for (ALL_LIST_ELEMENTS(lst, node, nnode, lsa))
1185 ospf_lsa_unlock(&lsa); /* oi->ls_upd_queue */
1186 list_free(lst);
1187 rn->info = NULL;
1188 }
1189
1190 /* remove update event */
1191 if (oi->t_ls_upd_event) {
1192 thread_cancel(oi->t_ls_upd_event);
1193 oi->t_ls_upd_event = NULL;
1194 }
1195 }
1196
1197 void ospf_if_update(struct ospf *ospf, struct interface *ifp)
1198 {
1199 if (!ospf)
1200 ospf = ospf_lookup();
1201
1202 /* OSPF must be ready. */
1203 if (!ospf_is_ready(ospf))
1204 return;
1205
1206 ospf_network_run_interface(ospf, ifp, NULL, NULL);
1207
1208 /* Update connected redistribute. */
1209 update_redistributed(ospf, 1);
1210 }
1211
1212 void ospf_remove_vls_through_area(struct ospf *ospf, struct ospf_area *area)
1213 {
1214 struct listnode *node, *nnode;
1215 struct ospf_vl_data *vl_data;
1216
1217 for (ALL_LIST_ELEMENTS(ospf->vlinks, node, nnode, vl_data))
1218 if (IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
1219 ospf_vl_delete(ospf, vl_data);
1220 }
1221
1222
1223 static const struct message ospf_area_type_msg[] = {
1224 {OSPF_AREA_DEFAULT, "Default"},
1225 {OSPF_AREA_STUB, "Stub"},
1226 {OSPF_AREA_NSSA, "NSSA"},
1227 {0}};
1228
1229 static void ospf_area_type_set(struct ospf_area *area, int type)
1230 {
1231 struct listnode *node;
1232 struct ospf_interface *oi;
1233
1234 if (area->external_routing == type) {
1235 if (IS_DEBUG_OSPF_EVENT)
1236 zlog_debug("Area[%s]: Types are the same, ignored.",
1237 inet_ntoa(area->area_id));
1238 return;
1239 }
1240
1241 area->external_routing = type;
1242
1243 if (IS_DEBUG_OSPF_EVENT)
1244 zlog_debug("Area[%s]: Configured as %s",
1245 inet_ntoa(area->area_id),
1246 lookup_msg(ospf_area_type_msg, type, NULL));
1247
1248 switch (area->external_routing) {
1249 case OSPF_AREA_DEFAULT:
1250 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi))
1251 if (oi->nbr_self != NULL) {
1252 UNSET_FLAG(oi->nbr_self->options,
1253 OSPF_OPTION_NP);
1254 SET_FLAG(oi->nbr_self->options, OSPF_OPTION_E);
1255 }
1256 break;
1257 case OSPF_AREA_STUB:
1258 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi))
1259 if (oi->nbr_self != NULL) {
1260 if (IS_DEBUG_OSPF_EVENT)
1261 zlog_debug(
1262 "setting options on %s accordingly",
1263 IF_NAME(oi));
1264 UNSET_FLAG(oi->nbr_self->options,
1265 OSPF_OPTION_NP);
1266 UNSET_FLAG(oi->nbr_self->options,
1267 OSPF_OPTION_E);
1268 if (IS_DEBUG_OSPF_EVENT)
1269 zlog_debug("options set on %s: %x",
1270 IF_NAME(oi), OPTIONS(oi));
1271 }
1272 break;
1273 case OSPF_AREA_NSSA:
1274 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi))
1275 if (oi->nbr_self != NULL) {
1276 zlog_debug(
1277 "setting nssa options on %s accordingly",
1278 IF_NAME(oi));
1279 UNSET_FLAG(oi->nbr_self->options,
1280 OSPF_OPTION_E);
1281 SET_FLAG(oi->nbr_self->options, OSPF_OPTION_NP);
1282 zlog_debug("options set on %s: %x", IF_NAME(oi),
1283 OPTIONS(oi));
1284 }
1285 break;
1286 default:
1287 break;
1288 }
1289
1290 ospf_router_lsa_update_area(area);
1291 ospf_schedule_abr_task(area->ospf);
1292 }
1293
1294 int ospf_area_shortcut_set(struct ospf *ospf, struct ospf_area *area, int mode)
1295 {
1296 if (area->shortcut_configured == mode)
1297 return 0;
1298
1299 area->shortcut_configured = mode;
1300 ospf_router_lsa_update_area(area);
1301 ospf_schedule_abr_task(ospf);
1302
1303 ospf_area_check_free(ospf, area->area_id);
1304
1305 return 1;
1306 }
1307
1308 int ospf_area_shortcut_unset(struct ospf *ospf, struct ospf_area *area)
1309 {
1310 area->shortcut_configured = OSPF_SHORTCUT_DEFAULT;
1311 ospf_router_lsa_update_area(area);
1312 ospf_area_check_free(ospf, area->area_id);
1313 ospf_schedule_abr_task(ospf);
1314
1315 return 1;
1316 }
1317
1318 static int ospf_area_vlink_count(struct ospf *ospf, struct ospf_area *area)
1319 {
1320 struct ospf_vl_data *vl;
1321 struct listnode *node;
1322 int count = 0;
1323
1324 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl))
1325 if (IPV4_ADDR_SAME(&vl->vl_area_id, &area->area_id))
1326 count++;
1327
1328 return count;
1329 }
1330
1331 int ospf_area_display_format_set(struct ospf *ospf, struct ospf_area *area,
1332 int df)
1333 {
1334 area->area_id_fmt = df;
1335
1336 return 1;
1337 }
1338
1339 int ospf_area_stub_set(struct ospf *ospf, struct in_addr area_id)
1340 {
1341 struct ospf_area *area;
1342
1343 area = ospf_area_get(ospf, area_id);
1344 if (ospf_area_vlink_count(ospf, area))
1345 return 0;
1346
1347 if (area->external_routing != OSPF_AREA_STUB)
1348 ospf_area_type_set(area, OSPF_AREA_STUB);
1349
1350 return 1;
1351 }
1352
1353 int ospf_area_stub_unset(struct ospf *ospf, struct in_addr area_id)
1354 {
1355 struct ospf_area *area;
1356
1357 area = ospf_area_lookup_by_area_id(ospf, area_id);
1358 if (area == NULL)
1359 return 1;
1360
1361 if (area->external_routing == OSPF_AREA_STUB)
1362 ospf_area_type_set(area, OSPF_AREA_DEFAULT);
1363
1364 ospf_area_check_free(ospf, area_id);
1365
1366 return 1;
1367 }
1368
1369 int ospf_area_no_summary_set(struct ospf *ospf, struct in_addr area_id)
1370 {
1371 struct ospf_area *area;
1372
1373 area = ospf_area_get(ospf, area_id);
1374 area->no_summary = 1;
1375
1376 return 1;
1377 }
1378
1379 int ospf_area_no_summary_unset(struct ospf *ospf, struct in_addr area_id)
1380 {
1381 struct ospf_area *area;
1382
1383 area = ospf_area_lookup_by_area_id(ospf, area_id);
1384 if (area == NULL)
1385 return 0;
1386
1387 area->no_summary = 0;
1388 ospf_area_check_free(ospf, area_id);
1389
1390 return 1;
1391 }
1392
1393 int ospf_area_nssa_set(struct ospf *ospf, struct in_addr area_id)
1394 {
1395 struct ospf_area *area;
1396
1397 area = ospf_area_get(ospf, area_id);
1398 if (ospf_area_vlink_count(ospf, area))
1399 return 0;
1400
1401 if (area->external_routing != OSPF_AREA_NSSA) {
1402 ospf_area_type_set(area, OSPF_AREA_NSSA);
1403 ospf->anyNSSA++;
1404 }
1405
1406 /* set NSSA area defaults */
1407 area->no_summary = 0;
1408 area->NSSATranslatorRole = OSPF_NSSA_ROLE_CANDIDATE;
1409 area->NSSATranslatorState = OSPF_NSSA_TRANSLATE_DISABLED;
1410 area->NSSATranslatorStabilityInterval = OSPF_NSSA_TRANS_STABLE_DEFAULT;
1411
1412 return 1;
1413 }
1414
1415 int ospf_area_nssa_unset(struct ospf *ospf, struct in_addr area_id)
1416 {
1417 struct ospf_area *area;
1418
1419 area = ospf_area_lookup_by_area_id(ospf, area_id);
1420 if (area == NULL)
1421 return 0;
1422
1423 if (area->external_routing == OSPF_AREA_NSSA) {
1424 ospf->anyNSSA--;
1425 ospf_area_type_set(area, OSPF_AREA_DEFAULT);
1426 }
1427
1428 ospf_area_check_free(ospf, area_id);
1429
1430 return 1;
1431 }
1432
1433 int ospf_area_nssa_translator_role_set(struct ospf *ospf,
1434 struct in_addr area_id, int role)
1435 {
1436 struct ospf_area *area;
1437
1438 area = ospf_area_lookup_by_area_id(ospf, area_id);
1439 if (area == NULL)
1440 return 0;
1441
1442 area->NSSATranslatorRole = role;
1443
1444 return 1;
1445 }
1446
1447 #if 0
1448 /* XXX: unused? Leave for symmetry? */
1449 static int
1450 ospf_area_nssa_translator_role_unset (struct ospf *ospf,
1451 struct in_addr area_id)
1452 {
1453 struct ospf_area *area;
1454
1455 area = ospf_area_lookup_by_area_id (ospf, area_id);
1456 if (area == NULL)
1457 return 0;
1458
1459 area->NSSATranslatorRole = OSPF_NSSA_ROLE_CANDIDATE;
1460
1461 ospf_area_check_free (ospf, area_id);
1462
1463 return 1;
1464 }
1465 #endif
1466
1467 int ospf_area_export_list_set(struct ospf *ospf, struct ospf_area *area,
1468 const char *list_name)
1469 {
1470 struct access_list *list;
1471 list = access_list_lookup(AFI_IP, list_name);
1472
1473 EXPORT_LIST(area) = list;
1474
1475 if (EXPORT_NAME(area))
1476 free(EXPORT_NAME(area));
1477
1478 EXPORT_NAME(area) = strdup(list_name);
1479 ospf_schedule_abr_task(ospf);
1480
1481 return 1;
1482 }
1483
1484 int ospf_area_export_list_unset(struct ospf *ospf, struct ospf_area *area)
1485 {
1486
1487 EXPORT_LIST(area) = 0;
1488
1489 if (EXPORT_NAME(area))
1490 free(EXPORT_NAME(area));
1491
1492 EXPORT_NAME(area) = NULL;
1493
1494 ospf_area_check_free(ospf, area->area_id);
1495
1496 ospf_schedule_abr_task(ospf);
1497
1498 return 1;
1499 }
1500
1501 int ospf_area_import_list_set(struct ospf *ospf, struct ospf_area *area,
1502 const char *name)
1503 {
1504 struct access_list *list;
1505 list = access_list_lookup(AFI_IP, name);
1506
1507 IMPORT_LIST(area) = list;
1508
1509 if (IMPORT_NAME(area))
1510 free(IMPORT_NAME(area));
1511
1512 IMPORT_NAME(area) = strdup(name);
1513 ospf_schedule_abr_task(ospf);
1514
1515 return 1;
1516 }
1517
1518 int ospf_area_import_list_unset(struct ospf *ospf, struct ospf_area *area)
1519 {
1520 IMPORT_LIST(area) = 0;
1521
1522 if (IMPORT_NAME(area))
1523 free(IMPORT_NAME(area));
1524
1525 IMPORT_NAME(area) = NULL;
1526 ospf_area_check_free(ospf, area->area_id);
1527
1528 ospf_schedule_abr_task(ospf);
1529
1530 return 1;
1531 }
1532
1533 int ospf_timers_refresh_set(struct ospf *ospf, int interval)
1534 {
1535 int time_left;
1536
1537 if (ospf->lsa_refresh_interval == interval)
1538 return 1;
1539
1540 time_left = ospf->lsa_refresh_interval
1541 - (monotime(NULL) - ospf->lsa_refresher_started);
1542
1543 if (time_left > interval) {
1544 OSPF_TIMER_OFF(ospf->t_lsa_refresher);
1545 thread_add_timer(master, ospf_lsa_refresh_walker, ospf,
1546 interval, &ospf->t_lsa_refresher);
1547 }
1548 ospf->lsa_refresh_interval = interval;
1549
1550 return 1;
1551 }
1552
1553 int ospf_timers_refresh_unset(struct ospf *ospf)
1554 {
1555 int time_left;
1556
1557 time_left = ospf->lsa_refresh_interval
1558 - (monotime(NULL) - ospf->lsa_refresher_started);
1559
1560 if (time_left > OSPF_LSA_REFRESH_INTERVAL_DEFAULT) {
1561 OSPF_TIMER_OFF(ospf->t_lsa_refresher);
1562 ospf->t_lsa_refresher = NULL;
1563 thread_add_timer(master, ospf_lsa_refresh_walker, ospf,
1564 OSPF_LSA_REFRESH_INTERVAL_DEFAULT,
1565 &ospf->t_lsa_refresher);
1566 }
1567
1568 ospf->lsa_refresh_interval = OSPF_LSA_REFRESH_INTERVAL_DEFAULT;
1569
1570 return 1;
1571 }
1572
1573
1574 static struct ospf_nbr_nbma *ospf_nbr_nbma_new(void)
1575 {
1576 struct ospf_nbr_nbma *nbr_nbma;
1577
1578 nbr_nbma = XCALLOC(MTYPE_OSPF_NEIGHBOR_STATIC,
1579 sizeof(struct ospf_nbr_nbma));
1580
1581 nbr_nbma->priority = OSPF_NEIGHBOR_PRIORITY_DEFAULT;
1582 nbr_nbma->v_poll = OSPF_POLL_INTERVAL_DEFAULT;
1583
1584 return nbr_nbma;
1585 }
1586
1587 static void ospf_nbr_nbma_free(struct ospf_nbr_nbma *nbr_nbma)
1588 {
1589 XFREE(MTYPE_OSPF_NEIGHBOR_STATIC, nbr_nbma);
1590 }
1591
1592 static void ospf_nbr_nbma_delete(struct ospf *ospf,
1593 struct ospf_nbr_nbma *nbr_nbma)
1594 {
1595 struct route_node *rn;
1596 struct prefix_ipv4 p;
1597
1598 p.family = AF_INET;
1599 p.prefix = nbr_nbma->addr;
1600 p.prefixlen = IPV4_MAX_BITLEN;
1601
1602 rn = route_node_lookup(ospf->nbr_nbma, (struct prefix *)&p);
1603 if (rn) {
1604 ospf_nbr_nbma_free(rn->info);
1605 rn->info = NULL;
1606 route_unlock_node(rn);
1607 route_unlock_node(rn);
1608 }
1609 }
1610
1611 static void ospf_nbr_nbma_down(struct ospf_nbr_nbma *nbr_nbma)
1612 {
1613 OSPF_TIMER_OFF(nbr_nbma->t_poll);
1614
1615 if (nbr_nbma->nbr) {
1616 nbr_nbma->nbr->nbr_nbma = NULL;
1617 OSPF_NSM_EVENT_EXECUTE(nbr_nbma->nbr, NSM_KillNbr);
1618 }
1619
1620 if (nbr_nbma->oi)
1621 listnode_delete(nbr_nbma->oi->nbr_nbma, nbr_nbma);
1622 }
1623
1624 static void ospf_nbr_nbma_add(struct ospf_nbr_nbma *nbr_nbma,
1625 struct ospf_interface *oi)
1626 {
1627 struct ospf_neighbor *nbr;
1628 struct route_node *rn;
1629 struct prefix p;
1630
1631 if (oi->type != OSPF_IFTYPE_NBMA)
1632 return;
1633
1634 if (nbr_nbma->nbr != NULL)
1635 return;
1636
1637 if (IPV4_ADDR_SAME(&oi->nbr_self->address.u.prefix4, &nbr_nbma->addr))
1638 return;
1639
1640 nbr_nbma->oi = oi;
1641 listnode_add(oi->nbr_nbma, nbr_nbma);
1642
1643 /* Get neighbor information from table. */
1644 p.family = AF_INET;
1645 p.prefixlen = IPV4_MAX_BITLEN;
1646 p.u.prefix4 = nbr_nbma->addr;
1647
1648 rn = route_node_get(oi->nbrs, (struct prefix *)&p);
1649 if (rn->info) {
1650 nbr = rn->info;
1651 nbr->nbr_nbma = nbr_nbma;
1652 nbr_nbma->nbr = nbr;
1653
1654 route_unlock_node(rn);
1655 } else {
1656 nbr = rn->info = ospf_nbr_new(oi);
1657 nbr->state = NSM_Down;
1658 nbr->src = nbr_nbma->addr;
1659 nbr->nbr_nbma = nbr_nbma;
1660 nbr->priority = nbr_nbma->priority;
1661 nbr->address = p;
1662
1663 nbr_nbma->nbr = nbr;
1664
1665 OSPF_NSM_EVENT_EXECUTE(nbr, NSM_Start);
1666 }
1667 }
1668
1669 void ospf_nbr_nbma_if_update(struct ospf *ospf, struct ospf_interface *oi)
1670 {
1671 struct ospf_nbr_nbma *nbr_nbma;
1672 struct route_node *rn;
1673 struct prefix_ipv4 p;
1674
1675 if (oi->type != OSPF_IFTYPE_NBMA)
1676 return;
1677
1678 for (rn = route_top(ospf->nbr_nbma); rn; rn = route_next(rn))
1679 if ((nbr_nbma = rn->info))
1680 if (nbr_nbma->oi == NULL && nbr_nbma->nbr == NULL) {
1681 p.family = AF_INET;
1682 p.prefix = nbr_nbma->addr;
1683 p.prefixlen = IPV4_MAX_BITLEN;
1684
1685 if (prefix_match(oi->address,
1686 (struct prefix *)&p))
1687 ospf_nbr_nbma_add(nbr_nbma, oi);
1688 }
1689 }
1690
1691 struct ospf_nbr_nbma *ospf_nbr_nbma_lookup(struct ospf *ospf,
1692 struct in_addr nbr_addr)
1693 {
1694 struct route_node *rn;
1695 struct prefix_ipv4 p;
1696
1697 p.family = AF_INET;
1698 p.prefix = nbr_addr;
1699 p.prefixlen = IPV4_MAX_BITLEN;
1700
1701 rn = route_node_lookup(ospf->nbr_nbma, (struct prefix *)&p);
1702 if (rn) {
1703 route_unlock_node(rn);
1704 return rn->info;
1705 }
1706 return NULL;
1707 }
1708
1709 struct ospf_nbr_nbma *ospf_nbr_nbma_lookup_next(struct ospf *ospf,
1710 struct in_addr *addr, int first)
1711 {
1712 #if 0
1713 struct ospf_nbr_nbma *nbr_nbma;
1714 struct listnode *node;
1715 #endif
1716
1717 if (ospf == NULL)
1718 return NULL;
1719
1720 #if 0
1721 for (ALL_LIST_ELEMENTS_RO (ospf->nbr_nbma, node, nbr_nbma))
1722 {
1723 if (first)
1724 {
1725 *addr = nbr_nbma->addr;
1726 return nbr_nbma;
1727 }
1728 else if (ntohl (nbr_nbma->addr.s_addr) > ntohl (addr->s_addr))
1729 {
1730 *addr = nbr_nbma->addr;
1731 return nbr_nbma;
1732 }
1733 }
1734 #endif
1735 return NULL;
1736 }
1737
1738 int ospf_nbr_nbma_set(struct ospf *ospf, struct in_addr nbr_addr)
1739 {
1740 struct ospf_nbr_nbma *nbr_nbma;
1741 struct ospf_interface *oi;
1742 struct prefix_ipv4 p;
1743 struct route_node *rn;
1744 struct listnode *node;
1745
1746 nbr_nbma = ospf_nbr_nbma_lookup(ospf, nbr_addr);
1747 if (nbr_nbma)
1748 return 0;
1749
1750 nbr_nbma = ospf_nbr_nbma_new();
1751 nbr_nbma->addr = nbr_addr;
1752
1753 p.family = AF_INET;
1754 p.prefix = nbr_addr;
1755 p.prefixlen = IPV4_MAX_BITLEN;
1756
1757 rn = route_node_get(ospf->nbr_nbma, (struct prefix *)&p);
1758 if (rn->info)
1759 route_unlock_node(rn);
1760 rn->info = nbr_nbma;
1761
1762 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
1763 if (oi->type == OSPF_IFTYPE_NBMA)
1764 if (prefix_match(oi->address, (struct prefix *)&p)) {
1765 ospf_nbr_nbma_add(nbr_nbma, oi);
1766 break;
1767 }
1768 }
1769
1770 return 1;
1771 }
1772
1773 int ospf_nbr_nbma_unset(struct ospf *ospf, struct in_addr nbr_addr)
1774 {
1775 struct ospf_nbr_nbma *nbr_nbma;
1776
1777 nbr_nbma = ospf_nbr_nbma_lookup(ospf, nbr_addr);
1778 if (nbr_nbma == NULL)
1779 return 0;
1780
1781 ospf_nbr_nbma_down(nbr_nbma);
1782 ospf_nbr_nbma_delete(ospf, nbr_nbma);
1783
1784 return 1;
1785 }
1786
1787 int ospf_nbr_nbma_priority_set(struct ospf *ospf, struct in_addr nbr_addr,
1788 u_char priority)
1789 {
1790 struct ospf_nbr_nbma *nbr_nbma;
1791
1792 nbr_nbma = ospf_nbr_nbma_lookup(ospf, nbr_addr);
1793 if (nbr_nbma == NULL)
1794 return 0;
1795
1796 if (nbr_nbma->priority != priority)
1797 nbr_nbma->priority = priority;
1798
1799 return 1;
1800 }
1801
1802 int ospf_nbr_nbma_priority_unset(struct ospf *ospf, struct in_addr nbr_addr)
1803 {
1804 struct ospf_nbr_nbma *nbr_nbma;
1805
1806 nbr_nbma = ospf_nbr_nbma_lookup(ospf, nbr_addr);
1807 if (nbr_nbma == NULL)
1808 return 0;
1809
1810 if (nbr_nbma != OSPF_NEIGHBOR_PRIORITY_DEFAULT)
1811 nbr_nbma->priority = OSPF_NEIGHBOR_PRIORITY_DEFAULT;
1812
1813 return 1;
1814 }
1815
1816 int ospf_nbr_nbma_poll_interval_set(struct ospf *ospf, struct in_addr nbr_addr,
1817 unsigned int interval)
1818 {
1819 struct ospf_nbr_nbma *nbr_nbma;
1820
1821 nbr_nbma = ospf_nbr_nbma_lookup(ospf, nbr_addr);
1822 if (nbr_nbma == NULL)
1823 return 0;
1824
1825 if (nbr_nbma->v_poll != interval) {
1826 nbr_nbma->v_poll = interval;
1827 if (nbr_nbma->oi && ospf_if_is_up(nbr_nbma->oi)) {
1828 OSPF_TIMER_OFF(nbr_nbma->t_poll);
1829 OSPF_POLL_TIMER_ON(nbr_nbma->t_poll, ospf_poll_timer,
1830 nbr_nbma->v_poll);
1831 }
1832 }
1833
1834 return 1;
1835 }
1836
1837 int ospf_nbr_nbma_poll_interval_unset(struct ospf *ospf, struct in_addr addr)
1838 {
1839 struct ospf_nbr_nbma *nbr_nbma;
1840
1841 nbr_nbma = ospf_nbr_nbma_lookup(ospf, addr);
1842 if (nbr_nbma == NULL)
1843 return 0;
1844
1845 if (nbr_nbma->v_poll != OSPF_POLL_INTERVAL_DEFAULT)
1846 nbr_nbma->v_poll = OSPF_POLL_INTERVAL_DEFAULT;
1847
1848 return 1;
1849 }
1850
1851 void ospf_master_init(struct thread_master *master)
1852 {
1853 memset(&ospf_master, 0, sizeof(struct ospf_master));
1854
1855 om = &ospf_master;
1856 om->ospf = list_new();
1857 om->master = master;
1858 }