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