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