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