]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_flood.c
*: auto-convert to SPDX License IDs
[mirror_frr.git] / ospfd / ospf_flood.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * OSPF Flooding -- RFC2328 Section 13.
4 * Copyright (C) 1999, 2000 Toshiaki Takada
5 */
6
7 #include <zebra.h>
8
9 #include "monotime.h"
10 #include "linklist.h"
11 #include "prefix.h"
12 #include "if.h"
13 #include "command.h"
14 #include "table.h"
15 #include "thread.h"
16 #include "memory.h"
17 #include "log.h"
18 #include "zclient.h"
19
20 #include "ospfd/ospfd.h"
21 #include "ospfd/ospf_interface.h"
22 #include "ospfd/ospf_ism.h"
23 #include "ospfd/ospf_asbr.h"
24 #include "ospfd/ospf_lsa.h"
25 #include "ospfd/ospf_lsdb.h"
26 #include "ospfd/ospf_neighbor.h"
27 #include "ospfd/ospf_nsm.h"
28 #include "ospfd/ospf_spf.h"
29 #include "ospfd/ospf_flood.h"
30 #include "ospfd/ospf_packet.h"
31 #include "ospfd/ospf_abr.h"
32 #include "ospfd/ospf_route.h"
33 #include "ospfd/ospf_zebra.h"
34 #include "ospfd/ospf_dump.h"
35
36 extern struct zclient *zclient;
37
38 /* Do the LSA acking specified in table 19, Section 13.5, row 2
39 * This get called from ospf_flood_out_interface. Declared inline
40 * for speed. */
41 static void ospf_flood_delayed_lsa_ack(struct ospf_neighbor *inbr,
42 struct ospf_lsa *lsa)
43 {
44 /* LSA is more recent than database copy, but was not
45 flooded back out receiving interface. Delayed
46 acknowledgment sent. If interface is in Backup state
47 delayed acknowledgment sent only if advertisement
48 received from Designated Router, otherwise do nothing See
49 RFC 2328 Section 13.5 */
50
51 /* Whether LSA is more recent or not, and whether this is in
52 response to the LSA being sent out recieving interface has been
53 worked out previously */
54
55 /* Deal with router as BDR */
56 if (inbr->oi->state == ISM_Backup && !NBR_IS_DR(inbr))
57 return;
58
59 /* Schedule a delayed LSA Ack to be sent */
60 listnode_add(inbr->oi->ls_ack,
61 ospf_lsa_lock(lsa)); /* delayed LSA Ack */
62 }
63
64 /* Check LSA is related to external info. */
65 struct external_info *ospf_external_info_check(struct ospf *ospf,
66 struct ospf_lsa *lsa)
67 {
68 struct as_external_lsa *al;
69 struct prefix_ipv4 p;
70 struct route_node *rn;
71 struct list *ext_list;
72 struct listnode *node;
73 struct ospf_external *ext;
74 int type;
75
76 al = (struct as_external_lsa *)lsa->data;
77
78 p.family = AF_INET;
79 p.prefix = lsa->data->id;
80 p.prefixlen = ip_masklen(al->mask);
81
82 for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
83 int redist_on = 0;
84
85 redist_on =
86 is_default_prefix4(&p)
87 ? vrf_bitmap_check(
88 zclient->default_information[AFI_IP],
89 ospf->vrf_id)
90 : (zclient->mi_redist[AFI_IP][type].enabled
91 || vrf_bitmap_check(
92 zclient->redist[AFI_IP][type],
93 ospf->vrf_id));
94 // Pending: check for MI above.
95 if (redist_on) {
96 ext_list = ospf->external[type];
97 if (!ext_list)
98 continue;
99
100 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
101 rn = NULL;
102 if (ext->external_info)
103 rn = route_node_lookup(
104 ext->external_info,
105 (struct prefix *)&p);
106 if (rn) {
107 route_unlock_node(rn);
108 if (rn->info != NULL)
109 return (struct external_info *)
110 rn->info;
111 }
112 }
113 }
114 }
115
116 if (is_default_prefix4(&p) && ospf->external[DEFAULT_ROUTE]) {
117 ext_list = ospf->external[DEFAULT_ROUTE];
118
119 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
120 if (!ext->external_info)
121 continue;
122
123 rn = route_node_lookup(ext->external_info,
124 (struct prefix *)&p);
125 if (!rn)
126 continue;
127 route_unlock_node(rn);
128 if (rn->info != NULL)
129 return (struct external_info *)rn->info;
130 }
131 }
132 return NULL;
133 }
134
135 static void ospf_process_self_originated_lsa(struct ospf *ospf,
136 struct ospf_lsa *new,
137 struct ospf_area *area)
138 {
139 struct ospf_interface *oi;
140 struct external_info *ei;
141 struct listnode *node;
142 struct as_external_lsa *al;
143 struct prefix_ipv4 p;
144 struct ospf_external_aggr_rt *aggr;
145
146 if (IS_DEBUG_OSPF_EVENT)
147 zlog_debug(
148 "%s:LSA[Type%d:%pI4]: Process self-originated LSA seq 0x%x",
149 ospf_get_name(ospf), new->data->type,
150 &new->data->id, ntohl(new->data->ls_seqnum));
151
152 /* If we're here, we installed a self-originated LSA that we received
153 from a neighbor, i.e. it's more recent. We must see whether we want
154 to originate it.
155 If yes, we should use this LSA's sequence number and reoriginate
156 a new instance.
157 if not --- we must flush this LSA from the domain. */
158 switch (new->data->type) {
159 case OSPF_ROUTER_LSA:
160 /* Originate a new instance and schedule flooding */
161 if (area->router_lsa_self)
162 area->router_lsa_self->data->ls_seqnum =
163 new->data->ls_seqnum;
164 ospf_router_lsa_update_area(area);
165 return;
166 case OSPF_NETWORK_LSA:
167 case OSPF_OPAQUE_LINK_LSA:
168 /* We must find the interface the LSA could belong to.
169 If the interface is no more a broadcast type or we are no
170 more
171 the DR, we flush the LSA otherwise -- create the new instance
172 and
173 schedule flooding. */
174
175 /* Look through all interfaces, not just area, since interface
176 could be moved from one area to another. */
177 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
178 /* These are sanity check. */
179 if (IPV4_ADDR_SAME(&oi->address->u.prefix4,
180 &new->data->id)) {
181 if (oi->area != area
182 || oi->type != OSPF_IFTYPE_BROADCAST
183 || !IPV4_ADDR_SAME(&oi->address->u.prefix4,
184 &DR(oi))) {
185 ospf_schedule_lsa_flush_area(area, new);
186 return;
187 }
188
189 if (new->data->type == OSPF_OPAQUE_LINK_LSA) {
190 ospf_opaque_lsa_refresh(new);
191 return;
192 }
193
194 if (oi->network_lsa_self)
195 oi->network_lsa_self->data->ls_seqnum =
196 new->data->ls_seqnum;
197 /* Schedule network-LSA origination. */
198 ospf_network_lsa_update(oi);
199 return;
200 }
201 break;
202 case OSPF_SUMMARY_LSA:
203 case OSPF_ASBR_SUMMARY_LSA:
204 ospf_schedule_abr_task(ospf);
205 break;
206 case OSPF_AS_EXTERNAL_LSA:
207 case OSPF_AS_NSSA_LSA:
208 if ((new->data->type == OSPF_AS_EXTERNAL_LSA)
209 && CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT)) {
210 ospf_translated_nssa_refresh(ospf, NULL, new);
211 return;
212 }
213
214 al = (struct as_external_lsa *)new->data;
215 p.family = AF_INET;
216 p.prefixlen = ip_masklen(al->mask);
217 p.prefix = new->data->id;
218
219 ei = ospf_external_info_check(ospf, new);
220 if (ei) {
221 if (ospf_external_aggr_match(ospf, &ei->p)) {
222 if (IS_DEBUG_OSPF(lsa, EXTNL_LSA_AGGR))
223 zlog_debug(
224 "%s, Matching external aggregate route found for %pI4, so don't refresh it.",
225 __func__,
226 &ei->p.prefix);
227
228 /* Aggregated external route shouldn't
229 * be in LSDB.
230 */
231 if (!IS_LSA_MAXAGE(new))
232 ospf_lsa_flush_as(ospf, new);
233
234 return;
235 }
236
237 ospf_external_lsa_refresh(ospf, new, ei,
238 LSA_REFRESH_FORCE, false);
239 } else {
240 aggr = (struct ospf_external_aggr_rt *)
241 ospf_extrenal_aggregator_lookup(ospf, &p);
242 if (aggr) {
243 struct external_info ei_aggr;
244
245 memset(&ei_aggr, 0,
246 sizeof(struct external_info));
247 ei_aggr.p = aggr->p;
248 ei_aggr.tag = aggr->tag;
249 ei_aggr.instance = ospf->instance;
250 ei_aggr.route_map_set.metric = -1;
251 ei_aggr.route_map_set.metric_type = -1;
252
253 ospf_external_lsa_refresh(ospf, new, &ei_aggr,
254 LSA_REFRESH_FORCE, true);
255 SET_FLAG(aggr->flags,
256 OSPF_EXTERNAL_AGGRT_ORIGINATED);
257 } else
258 ospf_lsa_flush_as(ospf, new);
259 }
260 break;
261 case OSPF_OPAQUE_AREA_LSA:
262 ospf_opaque_lsa_refresh(new);
263 break;
264 case OSPF_OPAQUE_AS_LSA:
265 ospf_opaque_lsa_refresh(new);
266 /* Reconsideration may needed. */ /* XXX */
267 break;
268 default:
269 break;
270 }
271 }
272
273 /* OSPF LSA flooding -- RFC2328 Section 13.(5). */
274
275 /* Now Updated for NSSA operation, as follows:
276
277
278 Type-5's have no change. Blocked to STUB or NSSA.
279
280 Type-7's can be received, and if a DR
281 they will also flood the local NSSA Area as Type-7's
282
283 If a Self-Originated LSA (now an ASBR),
284 The LSDB will be updated as Type-5's, (for continual re-fresh)
285
286 If an NSSA-IR it is installed/flooded as Type-7, P-bit on.
287 if an NSSA-ABR it is installed/flooded as Type-7, P-bit off.
288
289 Later, during the ABR TASK, if the ABR is the Elected NSSA
290 translator, then All Type-7s (with P-bit ON) are Translated to
291 Type-5's and flooded to all non-NSSA/STUB areas.
292
293 During ASE Calculations,
294 non-ABRs calculate external routes from Type-7's
295 ABRs calculate external routes from Type-5's and non-self Type-7s
296 */
297 int ospf_flood(struct ospf *ospf, struct ospf_neighbor *nbr,
298 struct ospf_lsa *current, struct ospf_lsa *new)
299 {
300 struct ospf_interface *oi;
301 int lsa_ack_flag;
302
303 /* Type-7 LSA's will be flooded throughout their native NSSA area,
304 but will also be flooded as Type-5's into ABR capable links. */
305
306 if (IS_DEBUG_OSPF_EVENT)
307 zlog_debug(
308 "%s:LSA[Flooding]: start, NBR %pI4 (%s), cur(%p), New-LSA[%s]",
309 ospf_get_name(ospf), &nbr->router_id,
310 lookup_msg(ospf_nsm_state_msg, nbr->state, NULL),
311 (void *)current, dump_lsa_key(new));
312
313 oi = nbr->oi;
314
315 /* If there is already a database copy, and if the
316 database copy was received via flooding and installed less
317 than MinLSArrival seconds ago, discard the new LSA
318 (without acknowledging it). */
319 if (current != NULL) /* -- endo. */
320 {
321 if (IS_LSA_SELF(current)
322 && (ntohs(current->data->ls_age) == 0
323 && ntohl(current->data->ls_seqnum)
324 == OSPF_INITIAL_SEQUENCE_NUMBER)) {
325 if (IS_DEBUG_OSPF_EVENT)
326 zlog_debug(
327 "%s:LSA[Flooding]: Got a self-originated LSA, while local one is initial instance.",
328 ospf_get_name(ospf));
329 ; /* Accept this LSA for quick LSDB resynchronization.
330 */
331 } else if (monotime_since(&current->tv_recv, NULL)
332 < ospf->min_ls_arrival * 1000LL) {
333 if (IS_DEBUG_OSPF_EVENT)
334 zlog_debug(
335 "%s:LSA[Flooding]: LSA is received recently.",
336 ospf_get_name(ospf));
337 return -1;
338 }
339 }
340
341 /* Flood the new LSA out some subset of the router's interfaces.
342 In some cases (e.g., the state of the receiving interface is
343 DR and the LSA was received from a router other than the
344 Backup DR) the LSA will be flooded back out the receiving
345 interface. */
346 lsa_ack_flag = ospf_flood_through(ospf, nbr, new);
347
348 /* Remove the current database copy from all neighbors' Link state
349 retransmission lists. AS_EXTERNAL and AS_EXTERNAL_OPAQUE does
350 ^^^^^^^^^^^^^^^^^^^^^^^
351 not have area ID.
352 All other (even NSSA's) do have area ID. */
353 if (current) {
354 switch (current->data->type) {
355 case OSPF_AS_EXTERNAL_LSA:
356 case OSPF_OPAQUE_AS_LSA:
357 ospf_ls_retransmit_delete_nbr_as(ospf, current);
358 break;
359 default:
360 ospf_ls_retransmit_delete_nbr_area(oi->area, current);
361 break;
362 }
363 }
364
365 /* Do some internal house keeping that is needed here */
366 SET_FLAG(new->flags, OSPF_LSA_RECEIVED);
367 (void)ospf_lsa_is_self_originated(ospf, new); /* Let it set the flag */
368
369 /* Received non-self-originated Grace LSA */
370 if (IS_GRACE_LSA(new) && !IS_LSA_SELF(new)) {
371
372 if (IS_LSA_MAXAGE(new)) {
373
374 /* Handling Max age grace LSA.*/
375 if (IS_DEBUG_OSPF_GR)
376 zlog_debug(
377 "%s, Received a maxage GRACE-LSA from router %pI4",
378 __func__, &new->data->adv_router);
379
380 if (current) {
381 ospf_process_maxage_grace_lsa(ospf, new, nbr);
382 } else {
383 if (IS_DEBUG_OSPF_GR)
384 zlog_debug(
385 "%s, Grace LSA doesn't exist in lsdb, so discarding grace lsa",
386 __func__);
387 return -1;
388 }
389 } else {
390 if (IS_DEBUG_OSPF_GR)
391 zlog_debug(
392 "%s, Received a GRACE-LSA from router %pI4",
393 __func__, &new->data->adv_router);
394
395 if (ospf_process_grace_lsa(ospf, new, nbr)
396 == OSPF_GR_NOT_HELPER) {
397 if (IS_DEBUG_OSPF_GR)
398 zlog_debug(
399 "%s, Not moving to HELPER role, So discarding grace LSA",
400 __func__);
401 return -1;
402 }
403 }
404 }
405
406 /* Install the new LSA in the link state database
407 (replacing the current database copy). This may cause the
408 routing table calculation to be scheduled. In addition,
409 timestamp the new LSA with the current time. The flooding
410 procedure cannot overwrite the newly installed LSA until
411 MinLSArrival seconds have elapsed. */
412
413 if (!(new = ospf_lsa_install(ospf, oi, new)))
414 return -1; /* unknown LSA type or any other error condition */
415
416 /* Acknowledge the receipt of the LSA by sending a Link State
417 Acknowledgment packet back out the receiving interface. */
418 if (lsa_ack_flag)
419 ospf_flood_delayed_lsa_ack(nbr, new);
420
421 /* If this new LSA indicates that it was originated by the
422 receiving router itself, the router must take special action,
423 either updating the LSA or in some cases flushing it from
424 the routing domain. */
425 if (ospf_lsa_is_self_originated(ospf, new))
426 ospf_process_self_originated_lsa(ospf, new, oi->area);
427 else
428 /* Update statistics value for OSPF-MIB. */
429 ospf->rx_lsa_count++;
430
431 return 0;
432 }
433
434 /* OSPF LSA flooding -- RFC2328 Section 13.3. */
435 int ospf_flood_through_interface(struct ospf_interface *oi,
436 struct ospf_neighbor *inbr,
437 struct ospf_lsa *lsa)
438 {
439 struct ospf_neighbor *onbr;
440 struct route_node *rn;
441 int retx_flag;
442
443 if (IS_DEBUG_OSPF_EVENT)
444 zlog_debug(
445 "%s: considering int %s (%s), INBR(%pI4), LSA[%s] AGE %u",
446 __func__, IF_NAME(oi), ospf_get_name(oi->ospf),
447 inbr ? &inbr->router_id : NULL, dump_lsa_key(lsa),
448 ntohs(lsa->data->ls_age));
449
450 if (!ospf_if_is_enable(oi))
451 return 0;
452
453 /* Remember if new LSA is added to a retransmit list. */
454 retx_flag = 0;
455
456 /* Each of the neighbors attached to this interface are examined,
457 to determine whether they must receive the new LSA. The following
458 steps are executed for each neighbor: */
459 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
460 struct ospf_lsa *ls_req;
461
462 if (rn->info == NULL)
463 continue;
464
465 onbr = rn->info;
466 if (IS_DEBUG_OSPF_EVENT)
467 zlog_debug(
468 "%s: considering nbr %pI4 via %s (%s), state: %s",
469 __func__, &onbr->router_id, IF_NAME(oi),
470 ospf_get_name(oi->ospf),
471 lookup_msg(ospf_nsm_state_msg, onbr->state,
472 NULL));
473
474 /* If the neighbor is in a lesser state than Exchange, it
475 does not participate in flooding, and the next neighbor
476 should be examined. */
477 if (onbr->state < NSM_Exchange)
478 continue;
479
480 /* If the adjacency is not yet full (neighbor state is
481 Exchange or Loading), examine the Link state request
482 list associated with this adjacency. If there is an
483 instance of the new LSA on the list, it indicates that
484 the neighboring router has an instance of the LSA
485 already. Compare the new LSA to the neighbor's copy: */
486 if (onbr->state < NSM_Full) {
487 if (IS_DEBUG_OSPF_EVENT)
488 zlog_debug(
489 "%s: adj to onbr %pI4 is not Full (%s)",
490 __func__, &onbr->router_id,
491 lookup_msg(ospf_nsm_state_msg,
492 onbr->state, NULL));
493 ls_req = ospf_ls_request_lookup(onbr, lsa);
494 if (ls_req != NULL) {
495 int ret;
496
497 ret = ospf_lsa_more_recent(ls_req, lsa);
498 /* The new LSA is less recent. */
499 if (ret > 0)
500 continue;
501 /* The two copies are the same instance, then
502 delete
503 the LSA from the Link state request list. */
504 else if (ret == 0) {
505 ospf_ls_request_delete(onbr, ls_req);
506 ospf_check_nbr_loading(onbr);
507 continue;
508 }
509 /* The new LSA is more recent. Delete the LSA
510 from the Link state request list. */
511 else {
512 ospf_ls_request_delete(onbr, ls_req);
513 ospf_check_nbr_loading(onbr);
514 }
515 }
516 }
517
518 if (IS_OPAQUE_LSA(lsa->data->type)) {
519 if (!CHECK_FLAG(onbr->options, OSPF_OPTION_O)) {
520 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
521 zlog_debug(
522 "%s: Skipping neighbor %s via %pI4 -- Not Opaque-capable.",
523 __func__, IF_NAME(oi),
524 &onbr->router_id);
525 continue;
526 }
527 }
528
529 /* If the new LSA was received from this neighbor,
530 examine the next neighbor. */
531 if (inbr) {
532 /*
533 * Triggered by LSUpd message parser "ospf_ls_upd ()".
534 * E.g., all LSAs handling here is received via network.
535 */
536 if (IPV4_ADDR_SAME(&inbr->router_id,
537 &onbr->router_id)) {
538 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
539 zlog_debug(
540 "%s: Skipping neighbor %s via %pI4 -- inbr == onbr.",
541 __func__, IF_NAME(oi),
542 &inbr->router_id);
543 continue;
544 }
545 } else {
546 /*
547 * Triggered by MaxAge remover, so far.
548 * NULL "inbr" means flooding starts from this node.
549 */
550 if (IPV4_ADDR_SAME(&lsa->data->adv_router,
551 &onbr->router_id)) {
552 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
553 zlog_debug(
554 "%s: Skipping neighbor %s via %pI4 -- lsah->adv_router == onbr.",
555 __func__, IF_NAME(oi),
556 &onbr->router_id);
557 continue;
558 }
559 }
560
561 /* Add the new LSA to the Link state retransmission list
562 for the adjacency. The LSA will be retransmitted
563 at intervals until an acknowledgment is seen from
564 the neighbor. */
565 ospf_ls_retransmit_add(onbr, lsa);
566 retx_flag = 1;
567 }
568
569 /* If in the previous step, the LSA was NOT added to any of
570 the Link state retransmission lists, there is no need to
571 flood the LSA out the interface. */
572 if (retx_flag == 0) {
573 return (inbr && inbr->oi == oi);
574 }
575
576 /* if we've received the lsa on this interface we need to perform
577 additional checking */
578 if (inbr && (inbr->oi == oi)) {
579 /* If the new LSA was received on this interface, and it was
580 received from either the Designated Router or the Backup
581 Designated Router, chances are that all the neighbors have
582 received the LSA already. */
583 if (NBR_IS_DR(inbr) || NBR_IS_BDR(inbr)) {
584 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
585 zlog_debug("%s: DR/BDR NOT SEND to int %s (%s)",
586 __func__, IF_NAME(oi),
587 ospf_get_name(oi->ospf));
588 return 1;
589 }
590
591 /* If the new LSA was received on this interface, and the
592 interface state is Backup, examine the next interface. The
593 Designated Router will do the flooding on this interface.
594 However, if the Designated Router fails the router will
595 end up retransmitting the updates. */
596
597 if (oi->state == ISM_Backup) {
598 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
599 zlog_debug(
600 "%s: ISM_Backup NOT SEND to int %s (%s)",
601 __func__, IF_NAME(oi),
602 ospf_get_name(oi->ospf));
603 return 1;
604 }
605 }
606
607 /* The LSA must be flooded out the interface. Send a Link State
608 Update packet (including the new LSA as contents) out the
609 interface. The LSA's LS age must be incremented by InfTransDelay
610 (which must be > 0) when it is copied into the outgoing Link
611 State Update packet (until the LS age field reaches the maximum
612 value of MaxAge). */
613 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
614 zlog_debug("%s: DR/BDR sending upd to int %s (%s)", __func__,
615 IF_NAME(oi), ospf_get_name(oi->ospf));
616
617 /* RFC2328 Section 13.3
618 On non-broadcast networks, separate Link State Update
619 packets must be sent, as unicasts, to each adjacent neighbor
620 (i.e., those in state Exchange or greater). The destination
621 IP addresses for these packets are the neighbors' IP
622 addresses. */
623 if (oi->type == OSPF_IFTYPE_NBMA) {
624 struct ospf_neighbor *nbr;
625
626 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
627 nbr = rn->info;
628
629 if (!nbr)
630 continue;
631 if (nbr != oi->nbr_self && nbr->state >= NSM_Exchange)
632 ospf_ls_upd_send_lsa(nbr, lsa,
633 OSPF_SEND_PACKET_DIRECT);
634 }
635 } else
636 /* Optimization: for P2MP interfaces,
637 don't send back out the incoming interface immediately,
638 allow time to rx multicast ack to the rx'ed (multicast)
639 update */
640 if (retx_flag != 1 ||
641 oi->type != OSPF_IFTYPE_POINTOMULTIPOINT || inbr == NULL ||
642 oi != inbr->oi)
643 ospf_ls_upd_send_lsa(oi->nbr_self, lsa,
644 OSPF_SEND_PACKET_INDIRECT);
645
646 return 0;
647 }
648
649 int ospf_flood_through_area(struct ospf_area *area, struct ospf_neighbor *inbr,
650 struct ospf_lsa *lsa)
651 {
652 struct listnode *node, *nnode;
653 struct ospf_interface *oi;
654 int lsa_ack_flag = 0;
655
656 assert(area);
657 /* All other types are specific to a single area (Area A). The
658 eligible interfaces are all those interfaces attaching to the
659 Area A. If Area A is the backbone, this includes all the virtual
660 links. */
661 for (ALL_LIST_ELEMENTS(area->oiflist, node, nnode, oi)) {
662 if (area->area_id.s_addr != OSPF_AREA_BACKBONE
663 && oi->type == OSPF_IFTYPE_VIRTUALLINK)
664 continue;
665
666 if ((lsa->data->type == OSPF_OPAQUE_LINK_LSA)
667 && (lsa->oi != oi)) {
668 /*
669 * Link local scoped Opaque-LSA should only be flooded
670 * for the link on which the LSA has received.
671 */
672 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
673 zlog_debug(
674 "Type-9 Opaque-LSA: lsa->oi(%p) != oi(%p)",
675 (void *)lsa->oi, (void *)oi);
676 continue;
677 }
678
679 if (ospf_flood_through_interface(oi, inbr, lsa))
680 lsa_ack_flag = 1;
681 }
682
683 return (lsa_ack_flag);
684 }
685
686 int ospf_flood_through_as(struct ospf *ospf, struct ospf_neighbor *inbr,
687 struct ospf_lsa *lsa)
688 {
689 struct listnode *node;
690 struct ospf_area *area;
691 int lsa_ack_flag;
692
693 lsa_ack_flag = 0;
694
695 /* The incoming LSA is type 5 or type 7 (AS-EXTERNAL or AS-NSSA )
696
697 Divert the Type-5 LSA's to all non-NSSA/STUB areas
698
699 Divert the Type-7 LSA's to all NSSA areas
700
701 AS-external-LSAs are flooded throughout the entire AS, with the
702 exception of stub areas (see Section 3.6). The eligible
703 interfaces are all the router's interfaces, excluding virtual
704 links and those interfaces attaching to stub areas. */
705
706 if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT)) /* Translated from 7 */
707 if (IS_DEBUG_OSPF_NSSA)
708 zlog_debug("Flood/AS: NSSA TRANSLATED LSA");
709
710 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
711 int continue_flag = 0;
712 struct listnode *if_node;
713 struct ospf_interface *oi;
714
715 switch (area->external_routing) {
716 /* Don't send AS externals into stub areas. Various types
717 of support for partial stub areas can be implemented
718 here. NSSA's will receive Type-7's that have areas
719 matching the originl LSA. */
720 case OSPF_AREA_NSSA: /* Sending Type 5 or 7 into NSSA area */
721 /* Type-7, flood NSSA area */
722 if (lsa->data->type == OSPF_AS_NSSA_LSA
723 && area == lsa->area)
724 /* We will send it. */
725 continue_flag = 0;
726 else
727 continue_flag = 1; /* Skip this NSSA area for
728 Type-5's et al */
729 break;
730
731 case OSPF_AREA_TYPE_MAX:
732 case OSPF_AREA_STUB:
733 continue_flag = 1; /* Skip this area. */
734 break;
735
736 case OSPF_AREA_DEFAULT:
737 default:
738 /* No Type-7 into normal area */
739 if (lsa->data->type == OSPF_AS_NSSA_LSA)
740 continue_flag = 1; /* skip Type-7 */
741 else
742 continue_flag = 0; /* Do this area. */
743 break;
744 }
745
746 /* Do continue for above switch. Saves a big if then mess */
747 if (continue_flag)
748 continue; /* main for-loop */
749
750 /* send to every interface in this area */
751
752 for (ALL_LIST_ELEMENTS_RO(area->oiflist, if_node, oi)) {
753 /* Skip virtual links */
754 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
755 if (ospf_flood_through_interface(oi, inbr,
756 lsa)) /* lsa */
757 lsa_ack_flag = 1;
758 }
759 } /* main area for-loop */
760
761 return (lsa_ack_flag);
762 }
763
764 int ospf_flood_through(struct ospf *ospf, struct ospf_neighbor *inbr,
765 struct ospf_lsa *lsa)
766 {
767 int lsa_ack_flag = 0;
768
769 /* Type-7 LSA's for NSSA are flooded throughout the AS here, and
770 upon return are updated in the LSDB for Type-7's. Later,
771 re-fresh will re-send them (and also, if ABR, packet code will
772 translate to Type-5's)
773
774 As usual, Type-5 LSA's (if not DISCARDED because we are STUB or
775 NSSA) are flooded throughout the AS, and are updated in the
776 global table. */
777 /*
778 * At the common sub-sub-function "ospf_flood_through_interface()",
779 * a parameter "inbr" will be used to distinguish the called context
780 * whether the given LSA was received from the neighbor, or the
781 * flooding for the LSA starts from this node (e.g. the LSA was self-
782 * originated, or the LSA is going to be flushed from routing domain).
783 *
784 * So, for consistency reasons, this function "ospf_flood_through()"
785 * should also allow the usage that the given "inbr" parameter to be
786 * NULL. If we do so, corresponding AREA parameter should be referred
787 * by "lsa->area", instead of "inbr->oi->area".
788 */
789 switch (lsa->data->type) {
790 case OSPF_AS_EXTERNAL_LSA: /* Type-5 */
791 case OSPF_OPAQUE_AS_LSA:
792 lsa_ack_flag = ospf_flood_through_as(ospf, inbr, lsa);
793 break;
794 /* Type-7 Only received within NSSA, then flooded */
795 case OSPF_AS_NSSA_LSA:
796 /* Any P-bit was installed with the Type-7. */
797
798 if (IS_DEBUG_OSPF_NSSA)
799 zlog_debug("%s: LOCAL NSSA FLOOD of Type-7.", __func__);
800 /* Fallthrough */
801 default:
802 lsa_ack_flag = ospf_flood_through_area(lsa->area, inbr, lsa);
803 break;
804 }
805
806 /* always need to send ack when incoming intf is PTP or P2MP */
807 if (inbr != NULL && (inbr->oi->type == OSPF_IFTYPE_POINTOMULTIPOINT ||
808 inbr->oi->type == OSPF_IFTYPE_POINTOPOINT))
809 lsa_ack_flag = 1;
810
811 return (lsa_ack_flag);
812 }
813
814
815 /* Management functions for neighbor's Link State Request list. */
816 void ospf_ls_request_add(struct ospf_neighbor *nbr, struct ospf_lsa *lsa)
817 {
818 /*
819 * We cannot make use of the newly introduced callback function
820 * "lsdb->new_lsa_hook" to replace debug output below, just because
821 * it seems no simple and smart way to pass neighbor information to
822 * the common function "ospf_lsdb_add()" -- endo.
823 */
824 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
825 zlog_debug("RqstL(%lu)++, NBR(%pI4(%s)), LSA[%s]",
826 ospf_ls_request_count(nbr),
827 &nbr->router_id,
828 ospf_get_name(nbr->oi->ospf), dump_lsa_key(lsa));
829
830 ospf_lsdb_add(&nbr->ls_req, lsa);
831 }
832
833 unsigned long ospf_ls_request_count(struct ospf_neighbor *nbr)
834 {
835 return ospf_lsdb_count_all(&nbr->ls_req);
836 }
837
838 int ospf_ls_request_isempty(struct ospf_neighbor *nbr)
839 {
840 return ospf_lsdb_isempty(&nbr->ls_req);
841 }
842
843 /* Remove LSA from neighbor's ls-request list. */
844 void ospf_ls_request_delete(struct ospf_neighbor *nbr, struct ospf_lsa *lsa)
845 {
846 if (nbr->ls_req_last == lsa) {
847 ospf_lsa_unlock(&nbr->ls_req_last);
848 nbr->ls_req_last = NULL;
849 }
850
851 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING)) /* -- endo. */
852 zlog_debug("RqstL(%lu)--, NBR(%pI4(%s)), LSA[%s]",
853 ospf_ls_request_count(nbr),
854 &nbr->router_id,
855 ospf_get_name(nbr->oi->ospf), dump_lsa_key(lsa));
856
857 ospf_lsdb_delete(&nbr->ls_req, lsa);
858 }
859
860 /* Remove all LSA from neighbor's ls-requenst list. */
861 void ospf_ls_request_delete_all(struct ospf_neighbor *nbr)
862 {
863 ospf_lsa_unlock(&nbr->ls_req_last);
864 nbr->ls_req_last = NULL;
865 ospf_lsdb_delete_all(&nbr->ls_req);
866 }
867
868 /* Lookup LSA from neighbor's ls-request list. */
869 struct ospf_lsa *ospf_ls_request_lookup(struct ospf_neighbor *nbr,
870 struct ospf_lsa *lsa)
871 {
872 return ospf_lsdb_lookup(&nbr->ls_req, lsa);
873 }
874
875 struct ospf_lsa *ospf_ls_request_new(struct lsa_header *lsah)
876 {
877 struct ospf_lsa *new;
878
879 new = ospf_lsa_new_and_data(OSPF_LSA_HEADER_SIZE);
880 memcpy(new->data, lsah, OSPF_LSA_HEADER_SIZE);
881
882 return new;
883 }
884
885
886 /* Management functions for neighbor's ls-retransmit list. */
887 unsigned long ospf_ls_retransmit_count(struct ospf_neighbor *nbr)
888 {
889 return ospf_lsdb_count_all(&nbr->ls_rxmt);
890 }
891
892 unsigned long ospf_ls_retransmit_count_self(struct ospf_neighbor *nbr,
893 int lsa_type)
894 {
895 return ospf_lsdb_count_self(&nbr->ls_rxmt, lsa_type);
896 }
897
898 int ospf_ls_retransmit_isempty(struct ospf_neighbor *nbr)
899 {
900 return ospf_lsdb_isempty(&nbr->ls_rxmt);
901 }
902
903 /* Add LSA to be retransmitted to neighbor's ls-retransmit list. */
904 void ospf_ls_retransmit_add(struct ospf_neighbor *nbr, struct ospf_lsa *lsa)
905 {
906 struct ospf_lsa *old;
907
908 old = ospf_ls_retransmit_lookup(nbr, lsa);
909
910 if (ospf_lsa_more_recent(old, lsa) < 0) {
911 if (old) {
912 old->retransmit_counter--;
913 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
914 zlog_debug("RXmtL(%lu)--, NBR(%pI4(%s)), LSA[%s]",
915 ospf_ls_retransmit_count(nbr),
916 &nbr->router_id,
917 ospf_get_name(nbr->oi->ospf),
918 dump_lsa_key(old));
919 ospf_lsdb_delete(&nbr->ls_rxmt, old);
920 }
921 lsa->retransmit_counter++;
922 /*
923 * We cannot make use of the newly introduced callback function
924 * "lsdb->new_lsa_hook" to replace debug output below, just
925 * because
926 * it seems no simple and smart way to pass neighbor information
927 * to
928 * the common function "ospf_lsdb_add()" -- endo.
929 */
930 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
931 zlog_debug("RXmtL(%lu)++, NBR(%pI4(%s)), LSA[%s]",
932 ospf_ls_retransmit_count(nbr),
933 &nbr->router_id,
934 ospf_get_name(nbr->oi->ospf),
935 dump_lsa_key(lsa));
936 ospf_lsdb_add(&nbr->ls_rxmt, lsa);
937 }
938 }
939
940 /* Remove LSA from neibghbor's ls-retransmit list. */
941 void ospf_ls_retransmit_delete(struct ospf_neighbor *nbr, struct ospf_lsa *lsa)
942 {
943 if (ospf_ls_retransmit_lookup(nbr, lsa)) {
944 lsa->retransmit_counter--;
945 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING)) /* -- endo. */
946 zlog_debug("RXmtL(%lu)--, NBR(%pI4(%s)), LSA[%s]",
947 ospf_ls_retransmit_count(nbr),
948 &nbr->router_id,
949 ospf_get_name(nbr->oi->ospf),
950 dump_lsa_key(lsa));
951 ospf_lsdb_delete(&nbr->ls_rxmt, lsa);
952 }
953 }
954
955 /* Clear neighbor's ls-retransmit list. */
956 void ospf_ls_retransmit_clear(struct ospf_neighbor *nbr)
957 {
958 struct ospf_lsdb *lsdb;
959 int i;
960
961 lsdb = &nbr->ls_rxmt;
962
963 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) {
964 struct route_table *table = lsdb->type[i].db;
965 struct route_node *rn;
966 struct ospf_lsa *lsa;
967
968 for (rn = route_top(table); rn; rn = route_next(rn))
969 if ((lsa = rn->info) != NULL)
970 ospf_ls_retransmit_delete(nbr, lsa);
971 }
972
973 ospf_lsa_unlock(&nbr->ls_req_last);
974 nbr->ls_req_last = NULL;
975 }
976
977 /* Lookup LSA from neighbor's ls-retransmit list. */
978 struct ospf_lsa *ospf_ls_retransmit_lookup(struct ospf_neighbor *nbr,
979 struct ospf_lsa *lsa)
980 {
981 return ospf_lsdb_lookup(&nbr->ls_rxmt, lsa);
982 }
983
984 static void ospf_ls_retransmit_delete_nbr_if(struct ospf_interface *oi,
985 struct ospf_lsa *lsa)
986 {
987 struct route_node *rn;
988 struct ospf_neighbor *nbr;
989 struct ospf_lsa *lsr;
990
991 if (ospf_if_is_enable(oi))
992 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
993 /* If LSA find in LS-retransmit list, then remove it. */
994 nbr = rn->info;
995
996 if (!nbr)
997 continue;
998
999 lsr = ospf_ls_retransmit_lookup(nbr, lsa);
1000
1001 /* If LSA find in ls-retransmit list, remove it. */
1002 if (lsr != NULL &&
1003 lsr->data->ls_seqnum == lsa->data->ls_seqnum)
1004 ospf_ls_retransmit_delete(nbr, lsr);
1005 }
1006 }
1007
1008 void ospf_ls_retransmit_delete_nbr_area(struct ospf_area *area,
1009 struct ospf_lsa *lsa)
1010 {
1011 struct listnode *node, *nnode;
1012 struct ospf_interface *oi;
1013
1014 for (ALL_LIST_ELEMENTS(area->oiflist, node, nnode, oi))
1015 ospf_ls_retransmit_delete_nbr_if(oi, lsa);
1016 }
1017
1018 void ospf_ls_retransmit_delete_nbr_as(struct ospf *ospf, struct ospf_lsa *lsa)
1019 {
1020 struct listnode *node, *nnode;
1021 struct ospf_interface *oi;
1022
1023 for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi))
1024 ospf_ls_retransmit_delete_nbr_if(oi, lsa);
1025 }
1026
1027
1028 /* Sets ls_age to MaxAge and floods throu the area.
1029 When we implement ASE routing, there will be another function
1030 flushing an LSA from the whole domain. */
1031 void ospf_lsa_flush_area(struct ospf_lsa *lsa, struct ospf_area *area)
1032 {
1033 struct ospf *ospf = area->ospf;
1034
1035 if (ospf_lsa_is_self_originated(ospf, lsa)
1036 && ospf->gr_info.restart_in_progress) {
1037 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1038 zlog_debug(
1039 "%s:LSA[Type%d:%pI4]: Graceful Restart in progress -- not flushing self-originated LSA",
1040 ospf_get_name(ospf), lsa->data->type,
1041 &lsa->data->id);
1042 return;
1043 }
1044
1045 /* Reset the lsa origination time such that it gives
1046 more time for the ACK to be received and avoid
1047 retransmissions */
1048 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1049 if (IS_DEBUG_OSPF_EVENT)
1050 zlog_debug("%s: MaxAge set to LSA[%s]", __func__,
1051 dump_lsa_key(lsa));
1052 monotime(&lsa->tv_recv);
1053 lsa->tv_orig = lsa->tv_recv;
1054 ospf_flood_through_area(area, NULL, lsa);
1055 ospf_lsa_maxage(ospf, lsa);
1056 }
1057
1058 void ospf_lsa_flush_as(struct ospf *ospf, struct ospf_lsa *lsa)
1059 {
1060 if (ospf_lsa_is_self_originated(ospf, lsa)
1061 && ospf->gr_info.restart_in_progress) {
1062 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1063 zlog_debug(
1064 "%s:LSA[Type%d:%pI4]: Graceful Restart in progress -- not flushing self-originated LSA",
1065 ospf_get_name(ospf), lsa->data->type,
1066 &lsa->data->id);
1067 return;
1068 }
1069
1070 /* Reset the lsa origination time such that it gives
1071 more time for the ACK to be received and avoid
1072 retransmissions */
1073 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1074 if (IS_DEBUG_OSPF_EVENT)
1075 zlog_debug("%s: MaxAge set to LSA[%s]", __func__,
1076 dump_lsa_key(lsa));
1077 monotime(&lsa->tv_recv);
1078 lsa->tv_orig = lsa->tv_recv;
1079 ospf_flood_through_as(ospf, NULL, lsa);
1080 ospf_lsa_maxage(ospf, lsa);
1081 }
1082
1083 void ospf_lsa_flush(struct ospf *ospf, struct ospf_lsa *lsa)
1084 {
1085 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
1086
1087 switch (lsa->data->type) {
1088 case OSPF_ROUTER_LSA:
1089 case OSPF_NETWORK_LSA:
1090 case OSPF_SUMMARY_LSA:
1091 case OSPF_ASBR_SUMMARY_LSA:
1092 case OSPF_AS_NSSA_LSA:
1093 case OSPF_OPAQUE_LINK_LSA:
1094 case OSPF_OPAQUE_AREA_LSA:
1095 ospf_lsa_flush_area(lsa, lsa->area);
1096 break;
1097 case OSPF_AS_EXTERNAL_LSA:
1098 case OSPF_OPAQUE_AS_LSA:
1099 ospf_lsa_flush_as(ospf, lsa);
1100 break;
1101 default:
1102 zlog_info("%s: Unknown LSA type %u", __func__, lsa->data->type);
1103 break;
1104 }
1105 }