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