]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_lsa.c
Merge pull request #13455 from sri-mohan1/srib-ldpd
[mirror_frr.git] / ospfd / ospf_lsa.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * OSPF Link State Advertisement
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 "table.h"
14 #include "memory.h"
15 #include "stream.h"
16 #include "log.h"
17 #include "frrevent.h"
18 #include "hash.h"
19 #include "sockunion.h" /* for inet_aton() */
20 #include "checksum.h"
21 #include "network.h"
22
23 #include "ospfd/ospfd.h"
24 #include "ospfd/ospf_interface.h"
25 #include "ospfd/ospf_ism.h"
26 #include "ospfd/ospf_asbr.h"
27 #include "ospfd/ospf_lsa.h"
28 #include "ospfd/ospf_lsdb.h"
29 #include "ospfd/ospf_neighbor.h"
30 #include "ospfd/ospf_nsm.h"
31 #include "ospfd/ospf_flood.h"
32 #include "ospfd/ospf_packet.h"
33 #include "ospfd/ospf_spf.h"
34 #include "ospfd/ospf_dump.h"
35 #include "ospfd/ospf_route.h"
36 #include "ospfd/ospf_ase.h"
37 #include "ospfd/ospf_zebra.h"
38 #include "ospfd/ospf_abr.h"
39 #include "ospfd/ospf_errors.h"
40
41 static struct ospf_lsa *ospf_handle_summarylsa_lsId_chg(struct ospf_area *area,
42 struct prefix_ipv4 *p,
43 uint8_t type,
44 uint32_t metric,
45 struct in_addr old_id);
46 static struct ospf_lsa *
47 ospf_summary_lsa_prepare_and_flood(struct prefix_ipv4 *p, uint32_t metric,
48 struct ospf_area *area, struct in_addr id);
49 static struct ospf_lsa *ospf_summary_lsa_refresh(struct ospf *ospf,
50 struct ospf_lsa *lsa);
51 static struct ospf_lsa *
52 ospf_asbr_summary_lsa_prepare_and_flood(struct prefix_ipv4 *p, uint32_t metric,
53 struct ospf_area *area,
54 struct in_addr id);
55 static struct ospf_lsa *ospf_summary_asbr_lsa_refresh(struct ospf *ospf,
56 struct ospf_lsa *lsa);
57 static struct ospf_lsa *ospf_handle_exnl_lsa_lsId_chg(struct ospf *ospf,
58 struct external_info *ei,
59 struct in_addr id);
60 static struct ospf_lsa *
61 ospf_exnl_lsa_prepare_and_flood(struct ospf *ospf, struct external_info *ei,
62 struct in_addr id);
63
64 uint32_t get_metric(uint8_t *metric)
65 {
66 uint32_t m;
67 m = metric[0];
68 m = (m << 8) + metric[1];
69 m = (m << 8) + metric[2];
70 return m;
71 }
72
73 /** @brief The Function checks self generated DoNotAge.
74 * @param lsa pointer.
75 * @return true or false.
76 */
77 bool ospf_check_dna_lsa(const struct ospf_lsa *lsa)
78 {
79 return ((IS_LSA_SELF(lsa) && CHECK_FLAG(lsa->data->ls_age, DO_NOT_AGE))
80 ? true
81 : false);
82 }
83
84 struct timeval int2tv(int a)
85 {
86 struct timeval ret;
87
88 ret.tv_sec = a;
89 ret.tv_usec = 0;
90
91 return ret;
92 }
93
94 struct timeval msec2tv(int a)
95 {
96 struct timeval ret;
97
98 ret.tv_sec = a / 1000;
99 ret.tv_usec = (a % 1000) * 1000;
100
101 return ret;
102 }
103
104 int ospf_lsa_refresh_delay(struct ospf_lsa *lsa)
105 {
106 struct timeval delta;
107 int delay = 0;
108
109 if (monotime_since(&lsa->tv_orig, &delta)
110 < OSPF_MIN_LS_INTERVAL * 1000LL) {
111 struct timeval minv = msec2tv(OSPF_MIN_LS_INTERVAL);
112 timersub(&minv, &delta, &minv);
113
114 /* TBD: remove padding to full sec, return timeval instead */
115 delay = minv.tv_sec + !!minv.tv_usec;
116
117 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
118 zlog_debug(
119 "LSA[Type%d:%pI4]: Refresh timer delay %d seconds",
120 lsa->data->type, &lsa->data->id,
121 delay);
122
123 assert(delay > 0);
124 }
125
126 return delay;
127 }
128
129
130 int get_age(struct ospf_lsa *lsa)
131 {
132 struct timeval rel;
133
134 /* As per rfc4136, the self-originated LSAs in their
135 * own database keep aging, however rfc doesn't tell
136 * till how long the LSA should be aged, as of now
137 * we are capping it for OSPF_LSA_MAXAGE.
138 */
139
140 /* If LSA is marked as donotage */
141 if (CHECK_FLAG(lsa->data->ls_age, DO_NOT_AGE) && !IS_LSA_SELF(lsa))
142 return ntohs(lsa->data->ls_age);
143
144 monotime_since(&lsa->tv_recv, &rel);
145 return ntohs(lsa->data->ls_age) + rel.tv_sec;
146 }
147
148
149 /* Fletcher Checksum -- Refer to RFC1008. */
150
151 /* All the offsets are zero-based. The offsets in the RFC1008 are
152 one-based. */
153 uint16_t ospf_lsa_checksum(struct lsa_header *lsa)
154 {
155 uint8_t *buffer = &lsa->options;
156 int options_offset = buffer - (uint8_t *)&lsa->ls_age; /* should be 2 */
157
158 /* Skip the AGE field */
159 uint16_t len = ntohs(lsa->length) - options_offset;
160
161 /* Checksum offset starts from "options" field, not the beginning of the
162 lsa_header struct. The offset is 14, rather than 16. */
163 int checksum_offset = (uint8_t *)&lsa->checksum - buffer;
164
165 return fletcher_checksum(buffer, len, checksum_offset);
166 }
167
168 int ospf_lsa_checksum_valid(struct lsa_header *lsa)
169 {
170 uint8_t *buffer = &lsa->options;
171 int options_offset = buffer - (uint8_t *)&lsa->ls_age; /* should be 2 */
172
173 /* Skip the AGE field */
174 uint16_t len = ntohs(lsa->length) - options_offset;
175
176 return (fletcher_checksum(buffer, len, FLETCHER_CHECKSUM_VALIDATE)
177 == 0);
178 }
179
180
181 /* Create OSPF LSA. */
182 struct ospf_lsa *ospf_lsa_new(void)
183 {
184 struct ospf_lsa *new;
185
186 new = XCALLOC(MTYPE_OSPF_LSA, sizeof(struct ospf_lsa));
187
188 new->flags = 0;
189 new->lock = 1;
190 new->retransmit_counter = 0;
191 monotime(&new->tv_recv);
192 new->tv_orig = new->tv_recv;
193 new->refresh_list = -1;
194 new->vrf_id = VRF_DEFAULT;
195 new->to_be_acknowledged = 0;
196 new->opaque_zero_len_delete = 0;
197
198 return new;
199 }
200
201 struct ospf_lsa *ospf_lsa_new_and_data(size_t size)
202 {
203 struct ospf_lsa *new;
204
205 new = ospf_lsa_new();
206 new->data = ospf_lsa_data_new(size);
207 new->size = size;
208
209 return new;
210 }
211
212 /* Duplicate OSPF LSA. */
213 struct ospf_lsa *ospf_lsa_dup(struct ospf_lsa *lsa)
214 {
215 struct ospf_lsa *new;
216
217 if (lsa == NULL)
218 return NULL;
219
220 new = XCALLOC(MTYPE_OSPF_LSA, sizeof(struct ospf_lsa));
221
222 memcpy(new, lsa, sizeof(struct ospf_lsa));
223 UNSET_FLAG(new->flags, OSPF_LSA_DISCARD);
224 new->lock = 1;
225 new->retransmit_counter = 0;
226 new->data = ospf_lsa_data_dup(lsa->data);
227
228 /* kevinm: Clear the refresh_list, otherwise there are going
229 to be problems when we try to remove the LSA from the
230 queue (which it's not a member of.)
231 XXX: Should we add the LSA to the refresh_list queue? */
232 new->refresh_list = -1;
233
234 if (IS_DEBUG_OSPF(lsa, LSA))
235 zlog_debug("LSA: duplicated %p (new: %p)", (void *)lsa,
236 (void *)new);
237
238 return new;
239 }
240
241 /* Free OSPF LSA. */
242 void ospf_lsa_free(struct ospf_lsa *lsa)
243 {
244 assert(lsa->lock == 0);
245
246 if (IS_DEBUG_OSPF(lsa, LSA))
247 zlog_debug("LSA: freed %p", (void *)lsa);
248
249 /* Delete LSA data. */
250 if (lsa->data != NULL)
251 ospf_lsa_data_free(lsa->data);
252
253 assert(lsa->refresh_list < 0);
254
255 memset(lsa, 0, sizeof(struct ospf_lsa));
256 XFREE(MTYPE_OSPF_LSA, lsa);
257 }
258
259 /* Lock LSA. */
260 struct ospf_lsa *ospf_lsa_lock(struct ospf_lsa *lsa)
261 {
262 lsa->lock++;
263 return lsa;
264 }
265
266 /* Unlock LSA. */
267 void ospf_lsa_unlock(struct ospf_lsa **lsa)
268 {
269 /* This is sanity check. */
270 if (!lsa || !*lsa)
271 return;
272
273 (*lsa)->lock--;
274
275 assert((*lsa)->lock >= 0);
276
277 if ((*lsa)->lock == 0) {
278 assert(CHECK_FLAG((*lsa)->flags, OSPF_LSA_DISCARD));
279 ospf_lsa_free(*lsa);
280 *lsa = NULL;
281 }
282 }
283
284 /* Check discard flag. */
285 void ospf_lsa_discard(struct ospf_lsa *lsa)
286 {
287 if (!CHECK_FLAG(lsa->flags, OSPF_LSA_DISCARD)) {
288 SET_FLAG(lsa->flags, OSPF_LSA_DISCARD);
289 ospf_lsa_unlock(&lsa);
290 }
291 }
292
293 /* Create LSA data. */
294 struct lsa_header *ospf_lsa_data_new(size_t size)
295 {
296 return XCALLOC(MTYPE_OSPF_LSA_DATA, size);
297 }
298
299 /* Duplicate LSA data. */
300 struct lsa_header *ospf_lsa_data_dup(struct lsa_header *lsah)
301 {
302 struct lsa_header *new;
303
304 new = ospf_lsa_data_new(ntohs(lsah->length));
305 memcpy(new, lsah, ntohs(lsah->length));
306
307 return new;
308 }
309
310 /* Free LSA data. */
311 void ospf_lsa_data_free(struct lsa_header *lsah)
312 {
313 if (IS_DEBUG_OSPF(lsa, LSA))
314 zlog_debug("LSA[Type%d:%pI4]: data freed %p", lsah->type,
315 &lsah->id, (void *)lsah);
316
317 XFREE(MTYPE_OSPF_LSA_DATA, lsah);
318 }
319
320
321 /* LSA general functions. */
322
323 const char *dump_lsa_key(struct ospf_lsa *lsa)
324 {
325 static char buf[sizeof("Type255,id(255.255.255.255),ar(255.255.255.255)")+1];
326 struct lsa_header *lsah;
327
328 if (lsa != NULL && (lsah = lsa->data) != NULL) {
329 char id[INET_ADDRSTRLEN], ar[INET_ADDRSTRLEN];
330 inet_ntop(AF_INET, &lsah->id, id, sizeof(id));
331 inet_ntop(AF_INET, &lsah->adv_router, ar, sizeof(ar));
332
333 snprintf(buf, sizeof(buf), "Type%d,id(%s),ar(%s)", lsah->type,
334 id, ar);
335 } else
336 strlcpy(buf, "NULL", sizeof(buf));
337
338 return buf;
339 }
340
341 uint32_t lsa_seqnum_increment(struct ospf_lsa *lsa)
342 {
343 uint32_t seqnum;
344
345 seqnum = ntohl(lsa->data->ls_seqnum) + 1;
346
347 return htonl(seqnum);
348 }
349
350 void lsa_header_set(struct stream *s, uint8_t options, uint8_t type,
351 struct in_addr id, struct in_addr router_id)
352 {
353 struct lsa_header *lsah;
354
355 lsah = (struct lsa_header *)STREAM_DATA(s);
356
357 lsah->ls_age = htons(OSPF_LSA_INITIAL_AGE);
358 lsah->options = options;
359 lsah->type = type;
360 lsah->id = id;
361 lsah->adv_router = router_id;
362 lsah->ls_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);
363
364 stream_forward_endp(s, OSPF_LSA_HEADER_SIZE);
365 }
366
367
368 /* router-LSA related functions. */
369 /* Get router-LSA flags. */
370 uint8_t router_lsa_flags(struct ospf_area *area)
371 {
372 uint8_t flags;
373
374 flags = area->ospf->flags;
375
376 /* Set virtual link flag. */
377 if (ospf_full_virtual_nbrs(area))
378 SET_FLAG(flags, ROUTER_LSA_VIRTUAL);
379 else
380 /* Just sanity check */
381 UNSET_FLAG(flags, ROUTER_LSA_VIRTUAL);
382
383 /* Set Shortcut ABR behabiour flag. */
384 UNSET_FLAG(flags, ROUTER_LSA_SHORTCUT);
385 if (area->ospf->abr_type == OSPF_ABR_SHORTCUT)
386 if (!OSPF_IS_AREA_BACKBONE(area))
387 if ((area->shortcut_configured == OSPF_SHORTCUT_DEFAULT
388 && area->ospf->backbone == NULL)
389 || area->shortcut_configured
390 == OSPF_SHORTCUT_ENABLE)
391 SET_FLAG(flags, ROUTER_LSA_SHORTCUT);
392
393 /* ASBR can't exit in stub area. */
394 if (area->external_routing == OSPF_AREA_STUB)
395 UNSET_FLAG(flags, ROUTER_LSA_EXTERNAL);
396 /* If ASBR set External flag */
397 else if (IS_OSPF_ASBR(area->ospf))
398 SET_FLAG(flags, ROUTER_LSA_EXTERNAL);
399
400 /* Set ABR dependent flags */
401 if (IS_OSPF_ABR(area->ospf)) {
402 SET_FLAG(flags, ROUTER_LSA_BORDER);
403 /* If Area is NSSA and we are both ABR and unconditional
404 * translator,
405 * set Nt bit to inform other routers.
406 */
407 if ((area->external_routing == OSPF_AREA_NSSA)
408 && (area->NSSATranslatorRole == OSPF_NSSA_ROLE_ALWAYS))
409 SET_FLAG(flags, ROUTER_LSA_NT);
410 }
411 return flags;
412 }
413
414 /* Lookup neighbor other than myself.
415 And check neighbor count,
416 Point-to-Point link must have only 1 neighbor. */
417 struct ospf_neighbor *ospf_nbr_lookup_ptop(struct ospf_interface *oi)
418 {
419 struct ospf_neighbor *nbr = NULL;
420 struct route_node *rn;
421
422 /* Search neighbor, there must be one of two nbrs. */
423 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
424 if ((nbr = rn->info))
425 if (!IPV4_ADDR_SAME(&nbr->router_id,
426 &oi->ospf->router_id))
427 if (nbr->state == NSM_Full) {
428 route_unlock_node(rn);
429 break;
430 }
431
432 /* PtoP link must have only 1 neighbor. */
433 if (ospf_nbr_count(oi, 0) > 1)
434 flog_warn(
435 EC_OSPF_PTP_NEIGHBOR,
436 "Point-to-Point link on interface %s has more than 1 neighbor.",
437 oi->ifp->name);
438
439 return nbr;
440 }
441
442 /* Determine cost of link, taking RFC3137 stub-router support into
443 * consideration
444 */
445 static uint16_t ospf_link_cost(struct ospf_interface *oi)
446 {
447 /* RFC3137 stub router support */
448 if (!CHECK_FLAG(oi->area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED))
449 return oi->output_cost;
450 else
451 return OSPF_OUTPUT_COST_INFINITE;
452 }
453
454 /* Set a link information. */
455 char link_info_set(struct stream **s, struct in_addr id, struct in_addr data,
456 uint8_t type, uint8_t tos, uint16_t cost)
457 {
458 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
459 * vast majority of cases. Some rare routers with lots of links need
460 * more.
461 * we try accommodate those here.
462 */
463 if (STREAM_WRITEABLE(*s) < OSPF_ROUTER_LSA_LINK_SIZE) {
464 size_t ret = OSPF_MAX_LSA_SIZE;
465
466 /* Can we enlarge the stream still? */
467 if (STREAM_SIZE(*s) == OSPF_MAX_LSA_SIZE) {
468 /* we futz the size here for simplicity, really we need
469 * to account
470 * for just:
471 * IP Header - (sizeof(struct ip))
472 * OSPF Header - OSPF_HEADER_SIZE
473 * LSA Header - OSPF_LSA_HEADER_SIZE
474 * MD5 auth data, if MD5 is configured -
475 * OSPF_AUTH_MD5_SIZE.
476 *
477 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
478 */
479 ret = stream_resize_inplace(
480 s, OSPF_MAX_PACKET_SIZE - OSPF_MAX_LSA_SIZE);
481 }
482
483 if (ret == OSPF_MAX_LSA_SIZE) {
484 flog_warn(
485 EC_OSPF_LSA_SIZE,
486 "%s: Out of space in LSA stream, left %zd, size %zd",
487 __func__, STREAM_WRITEABLE(*s),
488 STREAM_SIZE(*s));
489 return 0;
490 }
491 }
492
493 /* TOS based routing is not supported. */
494 stream_put_ipv4(*s, id.s_addr); /* Link ID. */
495 stream_put_ipv4(*s, data.s_addr); /* Link Data. */
496 stream_putc(*s, type); /* Link Type. */
497 stream_putc(*s, tos); /* TOS = 0. */
498 stream_putw(*s, cost); /* Link Cost. */
499
500 return 1;
501 }
502
503 /* Describe Point-to-Point link (Section 12.4.1.1). */
504
505 /* Note: If the interface is configured as point-to-point dmvpn then the other
506 * end of link is dmvpn hub with point-to-multipoint ospf network type. The
507 * hub then expects this router to populate the stub network and also Link Data
508 * Field set to IP Address and not MIB-II ifIndex
509 */
510 static int lsa_link_ptop_set(struct stream **s, struct ospf_interface *oi)
511 {
512 int links = 0;
513 struct ospf_neighbor *nbr;
514 struct in_addr id, mask, data;
515 uint16_t cost = ospf_link_cost(oi);
516
517 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
518 zlog_debug("LSA[Type1]: Set link Point-to-Point");
519
520 if ((nbr = ospf_nbr_lookup_ptop(oi)))
521 if (nbr->state == NSM_Full) {
522 if (CHECK_FLAG(oi->connected->flags,
523 ZEBRA_IFA_UNNUMBERED)
524 && !oi->ptp_dmvpn) {
525 /* For unnumbered point-to-point networks, the
526 Link Data field
527 should specify the interface's MIB-II ifIndex
528 value. */
529 data.s_addr = htonl(oi->ifp->ifindex);
530 links += link_info_set(
531 s, nbr->router_id, data,
532 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
533 } else {
534 links += link_info_set(
535 s, nbr->router_id,
536 oi->address->u.prefix4,
537 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
538 }
539 }
540
541 /* no need for a stub link for unnumbered interfaces */
542 if (oi->ptp_dmvpn
543 || !CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED)) {
544 /* Regardless of the state of the neighboring router, we must
545 add a Type 3 link (stub network).
546 N.B. Options 1 & 2 share basically the same logic. */
547 masklen2ip(oi->address->prefixlen, &mask);
548 id.s_addr = CONNECTED_PREFIX(oi->connected)->u.prefix4.s_addr
549 & mask.s_addr;
550 links += link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
551 oi->output_cost);
552 }
553
554 return links;
555 }
556
557 /* Describe Broadcast Link. */
558 static int lsa_link_broadcast_set(struct stream **s, struct ospf_interface *oi)
559 {
560 struct ospf_neighbor *dr;
561 struct in_addr id, mask;
562 uint16_t cost = ospf_link_cost(oi);
563
564 /* Describe Type 3 Link. */
565 if (oi->state == ISM_Waiting) {
566 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
567 zlog_debug(
568 "LSA[Type1]: Interface %s is in state Waiting. Adding stub interface",
569 oi->ifp->name);
570 masklen2ip(oi->address->prefixlen, &mask);
571 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
572 return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
573 oi->output_cost);
574 }
575
576 dr = ospf_nbr_lookup_by_addr(oi->nbrs, &DR(oi));
577 /* Describe Type 2 link. */
578 if (dr && (dr->state == NSM_Full
579 || IPV4_ADDR_SAME(&oi->address->u.prefix4, &DR(oi)))
580 && ospf_nbr_count(oi, NSM_Full) > 0) {
581 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
582 zlog_debug(
583 "LSA[Type1]: Interface %s has a DR. Adding transit interface",
584 oi->ifp->name);
585 return link_info_set(s, DR(oi), oi->address->u.prefix4,
586 LSA_LINK_TYPE_TRANSIT, 0, cost);
587 }
588 /* Describe type 3 link. */
589 else {
590 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
591 zlog_debug(
592 "LSA[Type1]: Interface %s has no DR. Adding stub interface",
593 oi->ifp->name);
594 masklen2ip(oi->address->prefixlen, &mask);
595 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
596 return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
597 oi->output_cost);
598 }
599 }
600
601 static int lsa_link_loopback_set(struct stream **s, struct ospf_interface *oi)
602 {
603 struct in_addr id, mask;
604
605 /* Describe Type 3 Link. */
606 if (oi->state != ISM_Loopback)
607 return 0;
608
609 mask.s_addr = 0xffffffff;
610 id.s_addr = oi->address->u.prefix4.s_addr;
611 return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
612 }
613
614 /* Describe Virtual Link. */
615 static int lsa_link_virtuallink_set(struct stream **s,
616 struct ospf_interface *oi)
617 {
618 struct ospf_neighbor *nbr;
619 uint16_t cost = ospf_link_cost(oi);
620
621 if (oi->state == ISM_PointToPoint)
622 if ((nbr = ospf_nbr_lookup_ptop(oi)))
623 if (nbr->state == NSM_Full) {
624 return link_info_set(s, nbr->router_id,
625 oi->address->u.prefix4,
626 LSA_LINK_TYPE_VIRTUALLINK,
627 0, cost);
628 }
629
630 return 0;
631 }
632
633 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
634
635 /* this function add for support point-to-multipoint ,see rfc2328
636 12.4.1.4.*/
637 /* from "edward rrr" <edward_rrr@hotmail.com>
638 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
639 static int lsa_link_ptomp_set(struct stream **s, struct ospf_interface *oi)
640 {
641 int links = 0;
642 struct route_node *rn;
643 struct ospf_neighbor *nbr = NULL;
644 struct in_addr id, mask;
645 uint16_t cost = ospf_link_cost(oi);
646
647 mask.s_addr = 0xffffffff;
648 id.s_addr = oi->address->u.prefix4.s_addr;
649 links += link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
650
651 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
652 zlog_debug("PointToMultipoint: running ptomultip_set");
653
654 /* Search neighbor, */
655 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
656 if ((nbr = rn->info) != NULL)
657 /* Ignore myself. */
658 if (!IPV4_ADDR_SAME(&nbr->router_id,
659 &oi->ospf->router_id))
660 if (nbr->state == NSM_Full)
661
662 {
663 links += link_info_set(
664 s, nbr->router_id,
665 oi->address->u.prefix4,
666 LSA_LINK_TYPE_POINTOPOINT, 0,
667 cost);
668 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
669 zlog_debug(
670 "PointToMultipoint: set link to %pI4",
671 &oi->address->u.prefix4);
672 }
673
674 return links;
675 }
676
677 /* Set router-LSA link information. */
678 static int router_lsa_link_set(struct stream **s, struct ospf_area *area)
679 {
680 struct listnode *node;
681 struct ospf_interface *oi;
682 int links = 0;
683
684 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi)) {
685 struct interface *ifp = oi->ifp;
686
687 /* Check interface is up, OSPF is enable. */
688 if (if_is_operative(ifp)) {
689 if (oi->state != ISM_Down) {
690 oi->lsa_pos_beg = links;
691 /* Describe each link. */
692 switch (oi->type) {
693 case OSPF_IFTYPE_POINTOPOINT:
694 links += lsa_link_ptop_set(s, oi);
695 break;
696 case OSPF_IFTYPE_BROADCAST:
697 links += lsa_link_broadcast_set(s, oi);
698 break;
699 case OSPF_IFTYPE_NBMA:
700 links += lsa_link_nbma_set(s, oi);
701 break;
702 case OSPF_IFTYPE_POINTOMULTIPOINT:
703 links += lsa_link_ptomp_set(s, oi);
704 break;
705 case OSPF_IFTYPE_VIRTUALLINK:
706 links +=
707 lsa_link_virtuallink_set(s, oi);
708 break;
709 case OSPF_IFTYPE_LOOPBACK:
710 links += lsa_link_loopback_set(s, oi);
711 }
712 oi->lsa_pos_end = links;
713 }
714 }
715 }
716
717 return links;
718 }
719
720 /* Set router-LSA body. */
721 void ospf_router_lsa_body_set(struct stream **s, struct ospf_area *area)
722 {
723 unsigned long putp;
724 uint16_t cnt;
725
726 /* Set flags. */
727 stream_putc(*s, router_lsa_flags(area));
728
729 /* Set Zero fields. */
730 stream_putc(*s, 0);
731
732 /* Keep pointer to # links. */
733 putp = stream_get_endp(*s);
734
735 /* Forward word */
736 stream_putw(*s, 0);
737
738 /* Set all link information. */
739 cnt = router_lsa_link_set(s, area);
740
741 /* Set # of links here. */
742 stream_putw_at(*s, putp, cnt);
743 }
744
745 static void ospf_stub_router_timer(struct event *t)
746 {
747 struct ospf_area *area = EVENT_ARG(t);
748
749 area->t_stub_router = NULL;
750
751 SET_FLAG(area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
752
753 /* clear stub route state and generate router-lsa refresh, don't
754 * clobber an administratively set stub-router state though.
755 */
756 if (CHECK_FLAG(area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
757 return;
758
759 UNSET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
760
761 ospf_router_lsa_update_area(area);
762 }
763
764 static void ospf_stub_router_check(struct ospf_area *area)
765 {
766 /* area must either be administratively configured to be stub
767 * or startup-time stub-router must be configured and we must in a
768 * pre-stub
769 * state.
770 */
771 if (CHECK_FLAG(area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED)) {
772 SET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
773 return;
774 }
775
776 /* not admin-stubbed, check whether startup stubbing is configured and
777 * whether it's not been done yet
778 */
779 if (CHECK_FLAG(area->stub_router_state,
780 OSPF_AREA_WAS_START_STUB_ROUTED))
781 return;
782
783 if (area->ospf->stub_router_startup_time
784 == OSPF_STUB_ROUTER_UNCONFIGURED) {
785 /* stub-router is hence done forever for this area, even if
786 * someone
787 * tries configure it (take effect next restart).
788 */
789 SET_FLAG(area->stub_router_state,
790 OSPF_AREA_WAS_START_STUB_ROUTED);
791 return;
792 }
793
794 /* startup stub-router configured and not yet done */
795 SET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
796
797 OSPF_AREA_TIMER_ON(area->t_stub_router, ospf_stub_router_timer,
798 area->ospf->stub_router_startup_time);
799 }
800
801 /* Create new router-LSA. */
802 static struct ospf_lsa *ospf_router_lsa_new(struct ospf_area *area)
803 {
804 struct ospf *ospf = area->ospf;
805 struct stream *s;
806 struct lsa_header *lsah;
807 struct ospf_lsa *new;
808 int length;
809
810 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
811 zlog_debug("LSA[Type1]: Create router-LSA instance");
812
813 /* check whether stub-router is desired, and if this is the first
814 * router LSA.
815 */
816 ospf_stub_router_check(area);
817
818 /* Create a stream for LSA. */
819 s = stream_new(OSPF_MAX_LSA_SIZE);
820 /* Set LSA common header fields. */
821 lsa_header_set(s, LSA_OPTIONS_GET(area) | LSA_OPTIONS_NSSA_GET(area),
822 OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);
823
824 /* Set router-LSA body fields. */
825 ospf_router_lsa_body_set(&s, area);
826
827 /* Set length. */
828 length = stream_get_endp(s);
829 lsah = (struct lsa_header *)STREAM_DATA(s);
830 lsah->length = htons(length);
831
832 /* Now, create OSPF LSA instance. */
833 new = ospf_lsa_new_and_data(length);
834
835 new->area = area;
836 SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
837 new->vrf_id = area->ospf->vrf_id;
838
839 /* Copy LSA data to store, discard stream. */
840 memcpy(new->data, lsah, length);
841 stream_free(s);
842
843 return new;
844 }
845
846 /* Originate Router-LSA. */
847 static struct ospf_lsa *ospf_router_lsa_originate(struct ospf_area *area)
848 {
849 struct ospf_lsa *new;
850
851 if (area->ospf->gr_info.restart_in_progress) {
852 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
853 zlog_debug(
854 "LSA[Type%d]: Graceful Restart in progress, don't originate",
855 OSPF_ROUTER_LSA);
856 return NULL;
857 }
858
859 /* Create new router-LSA instance. */
860 if ((new = ospf_router_lsa_new(area)) == NULL) {
861 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__);
862 return NULL;
863 }
864
865 /* Sanity check. */
866 if (new->data->adv_router.s_addr == INADDR_ANY) {
867 if (IS_DEBUG_OSPF_EVENT)
868 zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
869 ospf_lsa_discard(new);
870 return NULL;
871 }
872
873 /* Install LSA to LSDB. */
874 new = ospf_lsa_install(area->ospf, NULL, new);
875
876 /* Update LSA origination count. */
877 area->ospf->lsa_originate_count++;
878
879 /* Flooding new LSA through area. */
880 ospf_flood_through_area(area, NULL, new);
881
882 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
883 zlog_debug("LSA[Type%d:%pI4]: Originate router-LSA %p",
884 new->data->type, &new->data->id,
885 (void *)new);
886 ospf_lsa_header_dump(new->data);
887 }
888
889 return new;
890 }
891
892 /* Refresh router-LSA. */
893 static struct ospf_lsa *ospf_router_lsa_refresh(struct ospf_lsa *lsa)
894 {
895 struct ospf_area *area = lsa->area;
896 struct ospf_lsa *new;
897
898 /* Sanity check. */
899 assert(lsa->data);
900
901 /* Delete LSA from neighbor retransmit-list. */
902 ospf_ls_retransmit_delete_nbr_area(area, lsa);
903
904 /* Unregister LSA from refresh-list */
905 ospf_refresher_unregister_lsa(area->ospf, lsa);
906
907 /* Create new router-LSA instance. */
908 if ((new = ospf_router_lsa_new(area)) == NULL) {
909 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__);
910 return NULL;
911 }
912
913 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
914
915 ospf_lsa_install(area->ospf, NULL, new);
916
917 /* Flood LSA through area. */
918 ospf_flood_through_area(area, NULL, new);
919
920 /* Debug logging. */
921 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
922 zlog_debug("LSA[Type%d:%pI4]: router-LSA refresh",
923 new->data->type, &new->data->id);
924 ospf_lsa_header_dump(new->data);
925 }
926
927 return NULL;
928 }
929
930 int ospf_router_lsa_update_area(struct ospf_area *area)
931 {
932 if (IS_DEBUG_OSPF_EVENT)
933 zlog_debug("[router-LSA]: (router-LSA area update)");
934
935 /* Now refresh router-LSA. */
936 if (area->router_lsa_self)
937 ospf_lsa_refresh(area->ospf, area->router_lsa_self);
938 /* Newly originate router-LSA. */
939 else
940 ospf_router_lsa_originate(area);
941
942 return 0;
943 }
944
945 int ospf_router_lsa_update(struct ospf *ospf)
946 {
947 struct listnode *node, *nnode;
948 struct ospf_area *area;
949
950 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
951 zlog_debug("Timer[router-LSA Update]: (timer expire)");
952
953 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
954 struct ospf_lsa *lsa = area->router_lsa_self;
955 struct router_lsa *rl;
956 const char *area_str;
957
958 /* Keep Area ID string. */
959 area_str = AREA_NAME(area);
960
961 /* If LSA not exist in this Area, originate new. */
962 if (lsa == NULL) {
963 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
964 zlog_debug(
965 "LSA[Type1]: Create router-LSA for Area %s",
966 area_str);
967
968 ospf_router_lsa_originate(area);
969 }
970 /* If router-ID is changed, Link ID must change.
971 First flush old LSA, then originate new. */
972 else if (!IPV4_ADDR_SAME(&lsa->data->id, &ospf->router_id)) {
973 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
974 zlog_debug(
975 "LSA[Type%d:%pI4]: Refresh router-LSA for Area %s",
976 lsa->data->type,
977 &lsa->data->id, area_str);
978 ospf_refresher_unregister_lsa(ospf, lsa);
979 ospf_lsa_flush_area(lsa, area);
980 ospf_lsa_unlock(&area->router_lsa_self);
981 area->router_lsa_self = NULL;
982
983 /* Refresh router-LSA, (not install) and flood through
984 * area. */
985 ospf_router_lsa_update_area(area);
986 } else {
987 rl = (struct router_lsa *)lsa->data;
988 /* Refresh router-LSA, (not install) and flood through
989 * area. */
990 if (rl->flags != ospf->flags)
991 ospf_router_lsa_update_area(area);
992 }
993 }
994
995 return 0;
996 }
997
998
999 /* network-LSA related functions. */
1000 /* Originate Network-LSA. */
1001 static void ospf_network_lsa_body_set(struct stream *s,
1002 struct ospf_interface *oi)
1003 {
1004 struct in_addr mask;
1005 struct route_node *rn;
1006 struct ospf_neighbor *nbr;
1007
1008 masklen2ip(oi->address->prefixlen, &mask);
1009 stream_put_ipv4(s, mask.s_addr);
1010
1011 /* The network-LSA lists those routers that are fully adjacent to
1012 the Designated Router; each fully adjacent router is identified by
1013 its OSPF Router ID. The Designated Router includes itself in this
1014 list. RFC2328, Section 12.4.2 */
1015
1016 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
1017 if ((nbr = rn->info) != NULL)
1018 if (nbr->state == NSM_Full || nbr == oi->nbr_self)
1019 stream_put_ipv4(s, nbr->router_id.s_addr);
1020 }
1021
1022 static struct ospf_lsa *ospf_network_lsa_new(struct ospf_interface *oi)
1023 {
1024 struct stream *s;
1025 struct ospf_lsa *new;
1026 struct lsa_header *lsah;
1027 struct ospf_if_params *oip;
1028 int length;
1029
1030 /* If there are no neighbours on this network (the net is stub),
1031 the router does not originate network-LSA (see RFC 12.4.2) */
1032 if (oi->full_nbrs == 0)
1033 return NULL;
1034
1035 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1036 zlog_debug("LSA[Type2]: Create network-LSA instance");
1037
1038 /* Create new stream for LSA. */
1039 s = stream_new(OSPF_MAX_LSA_SIZE);
1040 lsah = (struct lsa_header *)STREAM_DATA(s);
1041
1042 lsa_header_set(s, (OPTIONS(oi) | LSA_OPTIONS_GET(oi->area)),
1043 OSPF_NETWORK_LSA, DR(oi), oi->ospf->router_id);
1044
1045 /* Set network-LSA body fields. */
1046 ospf_network_lsa_body_set(s, oi);
1047
1048 /* Set length. */
1049 length = stream_get_endp(s);
1050 lsah->length = htons(length);
1051
1052 /* Create OSPF LSA instance. */
1053 new = ospf_lsa_new_and_data(length);
1054
1055 new->area = oi->area;
1056 SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1057 new->vrf_id = oi->ospf->vrf_id;
1058
1059 /* Copy LSA to store. */
1060 memcpy(new->data, lsah, length);
1061 stream_free(s);
1062
1063 /* Remember prior network LSA sequence numbers, even if we stop
1064 * originating one for this oi, to try avoid re-originating LSAs with a
1065 * prior sequence number, and thus speed up adjency forming &
1066 * convergence.
1067 */
1068 if ((oip = ospf_lookup_if_params(oi->ifp, oi->address->u.prefix4))) {
1069 new->data->ls_seqnum = oip->network_lsa_seqnum;
1070 new->data->ls_seqnum = lsa_seqnum_increment(new);
1071 } else {
1072 oip = ospf_get_if_params(oi->ifp, oi->address->u.prefix4);
1073 ospf_if_update_params(oi->ifp, oi->address->u.prefix4);
1074 }
1075 oip->network_lsa_seqnum = new->data->ls_seqnum;
1076
1077 return new;
1078 }
1079
1080 /* Originate network-LSA. */
1081 void ospf_network_lsa_update(struct ospf_interface *oi)
1082 {
1083 struct ospf_lsa *new;
1084
1085 if (oi->area->ospf->gr_info.restart_in_progress) {
1086 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1087 zlog_debug(
1088 "LSA[Type%d]: Graceful Restart in progress, don't originate",
1089 OSPF_NETWORK_LSA);
1090 return;
1091 }
1092
1093 if (oi->network_lsa_self != NULL) {
1094 ospf_lsa_refresh(oi->ospf, oi->network_lsa_self);
1095 return;
1096 }
1097
1098 /* Create new network-LSA instance. */
1099 new = ospf_network_lsa_new(oi);
1100 if (new == NULL)
1101 return;
1102
1103 /* Install LSA to LSDB. */
1104 new = ospf_lsa_install(oi->ospf, oi, new);
1105
1106 /* Update LSA origination count. */
1107 oi->ospf->lsa_originate_count++;
1108
1109 /* Flooding new LSA through area. */
1110 ospf_flood_through_area(oi->area, NULL, new);
1111
1112 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1113 zlog_debug("LSA[Type%d:%pI4]: Originate network-LSA %p",
1114 new->data->type, &new->data->id,
1115 (void *)new);
1116 ospf_lsa_header_dump(new->data);
1117 }
1118
1119 return;
1120 }
1121
1122 static struct ospf_lsa *ospf_network_lsa_refresh(struct ospf_lsa *lsa)
1123 {
1124 struct ospf_area *area = lsa->area;
1125 struct ospf_lsa *new, *new2;
1126 struct ospf_if_params *oip;
1127 struct ospf_interface *oi;
1128
1129 assert(lsa->data);
1130
1131 /* Retrieve the oi for the network LSA */
1132 oi = ospf_if_lookup_by_local_addr(area->ospf, NULL, lsa->data->id);
1133 if (oi == NULL) {
1134 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1135 zlog_debug(
1136 "LSA[Type%d:%pI4]: network-LSA refresh: no oi found, ick, ignoring.",
1137 lsa->data->type, &lsa->data->id);
1138 ospf_lsa_header_dump(lsa->data);
1139 }
1140 return NULL;
1141 }
1142
1143 if (oi->state != ISM_DR)
1144 return NULL;
1145
1146 /* Delete LSA from neighbor retransmit-list. */
1147 ospf_ls_retransmit_delete_nbr_area(area, lsa);
1148
1149 /* Unregister LSA from refresh-list */
1150 ospf_refresher_unregister_lsa(area->ospf, lsa);
1151
1152 /* Create new network-LSA instance. */
1153 new = ospf_network_lsa_new(oi);
1154 if (new == NULL)
1155 return NULL;
1156
1157 oip = ospf_lookup_if_params(oi->ifp, oi->address->u.prefix4);
1158 assert(oip != NULL);
1159 oip->network_lsa_seqnum = new->data->ls_seqnum =
1160 lsa_seqnum_increment(lsa);
1161
1162 new2 = ospf_lsa_install(area->ospf, oi, new);
1163
1164 assert(new2 == new);
1165
1166 /* Flood LSA through aera. */
1167 ospf_flood_through_area(area, NULL, new);
1168
1169 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1170 zlog_debug("LSA[Type%d:%pI4]: network-LSA refresh",
1171 new->data->type, &new->data->id);
1172 ospf_lsa_header_dump(new->data);
1173 }
1174
1175 return new;
1176 }
1177
1178 static void stream_put_ospf_metric(struct stream *s, uint32_t metric_value)
1179 {
1180 uint32_t metric;
1181 char *mp;
1182
1183 /* Put 0 metric. TOS metric is not supported. */
1184 metric = htonl(metric_value);
1185 mp = (char *)&metric;
1186 mp++;
1187 stream_put(s, mp, 3);
1188 }
1189
1190 /* summary-LSA related functions. */
1191 static void ospf_summary_lsa_body_set(struct stream *s, struct prefix *p,
1192 uint32_t metric)
1193 {
1194 struct in_addr mask;
1195
1196 masklen2ip(p->prefixlen, &mask);
1197
1198 /* Put Network Mask. */
1199 stream_put_ipv4(s, mask.s_addr);
1200
1201 /* Set # TOS. */
1202 stream_putc(s, (uint8_t)0);
1203
1204 /* Set metric. */
1205 stream_put_ospf_metric(s, metric);
1206 }
1207
1208 static struct ospf_lsa *ospf_summary_lsa_new(struct ospf_area *area,
1209 struct prefix *p, uint32_t metric,
1210 struct in_addr id)
1211 {
1212 struct stream *s;
1213 struct ospf_lsa *new;
1214 struct lsa_header *lsah;
1215 int length;
1216
1217 if (id.s_addr == 0xffffffff) {
1218 /* Maybe Link State ID not available. */
1219 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1220 zlog_debug(
1221 "LSA[Type%d]: Link ID not available, can't originate",
1222 OSPF_SUMMARY_LSA);
1223 return NULL;
1224 }
1225
1226 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1227 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1228
1229 /* Create new stream for LSA. */
1230 s = stream_new(OSPF_MAX_LSA_SIZE);
1231 lsah = (struct lsa_header *)STREAM_DATA(s);
1232
1233 lsa_header_set(s, LSA_OPTIONS_GET(area), OSPF_SUMMARY_LSA, id,
1234 area->ospf->router_id);
1235
1236 /* Set summary-LSA body fields. */
1237 ospf_summary_lsa_body_set(s, p, metric);
1238
1239 /* Set length. */
1240 length = stream_get_endp(s);
1241 lsah->length = htons(length);
1242
1243 /* Create OSPF LSA instance. */
1244 new = ospf_lsa_new_and_data(length);
1245 new->area = area;
1246 SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1247 new->vrf_id = area->ospf->vrf_id;
1248
1249 /* Copy LSA to store. */
1250 memcpy(new->data, lsah, length);
1251 stream_free(s);
1252
1253 return new;
1254 }
1255
1256 /* Originate Summary-LSA. */
1257 static struct ospf_lsa *
1258 ospf_summary_lsa_prepare_and_flood(struct prefix_ipv4 *p, uint32_t metric,
1259 struct ospf_area *area, struct in_addr id)
1260 {
1261 struct ospf_lsa *new;
1262
1263 /* Create new summary-LSA instance. */
1264 if (!(new = ospf_summary_lsa_new(area, (struct prefix *)p, metric, id)))
1265 return NULL;
1266
1267 /* Instlal LSA to LSDB. */
1268 new = ospf_lsa_install(area->ospf, NULL, new);
1269
1270 /* Update LSA origination count. */
1271 area->ospf->lsa_originate_count++;
1272
1273 /* Flooding new LSA through area. */
1274 ospf_flood_through_area(area, NULL, new);
1275
1276 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1277 zlog_debug("LSA[Type%d:%pI4]: Originate summary-LSA %p",
1278 new->data->type, &new->data->id,
1279 (void *)new);
1280 ospf_lsa_header_dump(new->data);
1281 }
1282
1283 return new;
1284 }
1285
1286 static struct ospf_lsa *ospf_handle_summarylsa_lsId_chg(struct ospf_area *area,
1287 struct prefix_ipv4 *p,
1288 uint8_t type,
1289 uint32_t metric,
1290 struct in_addr old_id)
1291 {
1292 struct ospf_lsa *lsa = NULL;
1293 struct ospf_lsa *summary_lsa = NULL;
1294 struct summary_lsa *sl = NULL;
1295 struct ospf_area *old_area = NULL;
1296 struct ospf *ospf = area->ospf;
1297 struct prefix_ipv4 old_prefix;
1298 uint32_t old_metric;
1299 struct in_addr mask;
1300 uint32_t metric_val;
1301 char *metric_buf;
1302
1303 lsa = ospf_lsdb_lookup_by_id(area->lsdb, type, p->prefix,
1304 ospf->router_id);
1305
1306 if (!lsa) {
1307 flog_warn(EC_OSPF_LSA_NULL, "(%s): LSA not found", __func__);
1308 return NULL;
1309 }
1310
1311 sl = (struct summary_lsa *)lsa->data;
1312
1313 old_area = lsa->area;
1314 old_metric = GET_METRIC(sl->metric);
1315 old_prefix.prefix = sl->header.id;
1316 old_prefix.prefixlen = ip_masklen(sl->mask);
1317 old_prefix.family = AF_INET;
1318
1319
1320 /* change the mask */
1321 masklen2ip(p->prefixlen, &mask);
1322 sl->mask.s_addr = mask.s_addr;
1323
1324 /* Copy the metric*/
1325 metric_val = htonl(metric);
1326 metric_buf = (char *)&metric_val;
1327 memcpy(sl->metric, metric_buf, sizeof(metric_val));
1328
1329 if (type == OSPF_SUMMARY_LSA) {
1330 /*Refresh the LSA with new LSA*/
1331 summary_lsa = ospf_summary_lsa_refresh(ospf, lsa);
1332
1333 ospf_summary_lsa_prepare_and_flood(&old_prefix, old_metric,
1334 old_area, old_id);
1335 } else {
1336 /*Refresh the LSA with new LSA*/
1337 summary_lsa = ospf_summary_asbr_lsa_refresh(ospf, lsa);
1338
1339 ospf_asbr_summary_lsa_prepare_and_flood(&old_prefix, old_metric,
1340 old_area, old_id);
1341 }
1342
1343 return summary_lsa;
1344 }
1345
1346 /* Originate Summary-LSA. */
1347 struct ospf_lsa *ospf_summary_lsa_originate(struct prefix_ipv4 *p,
1348 uint32_t metric,
1349 struct ospf_area *area)
1350 {
1351 struct in_addr id;
1352 enum lsid_status status;
1353 struct ospf_lsa *new = NULL;
1354
1355 status = ospf_lsa_unique_id(area->ospf, area->lsdb, OSPF_SUMMARY_LSA, p,
1356 &id);
1357
1358 if (status == LSID_CHANGE) {
1359 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1360 zlog_debug("Link ID has to be changed.");
1361
1362 new = ospf_handle_summarylsa_lsId_chg(area, p, OSPF_SUMMARY_LSA,
1363 metric, id);
1364 return new;
1365 } else if (status == LSID_NOT_AVAILABLE) {
1366 /* Link State ID not available. */
1367 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1368 zlog_debug(
1369 "LSA[Type5]: Link ID not available, can't originate");
1370
1371 return NULL;
1372 }
1373
1374 new = ospf_summary_lsa_prepare_and_flood(p, metric, area, id);
1375 return new;
1376 }
1377
1378 static struct ospf_lsa *ospf_summary_lsa_refresh(struct ospf *ospf,
1379 struct ospf_lsa *lsa)
1380 {
1381 struct ospf_lsa *new;
1382 struct summary_lsa *sl;
1383 struct prefix p;
1384
1385 /* Sanity check. */
1386 assert(lsa->data);
1387
1388 sl = (struct summary_lsa *)lsa->data;
1389 p.prefixlen = ip_masklen(sl->mask);
1390 new = ospf_summary_lsa_new(lsa->area, &p, GET_METRIC(sl->metric),
1391 sl->header.id);
1392
1393 if (!new)
1394 return NULL;
1395
1396 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
1397
1398 ospf_lsa_install(ospf, NULL, new);
1399
1400 /* Flood LSA through AS. */
1401 ospf_flood_through_area(new->area, NULL, new);
1402
1403 /* Debug logging. */
1404 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1405 zlog_debug("LSA[Type%d:%pI4]: summary-LSA refresh",
1406 new->data->type, &new->data->id);
1407 ospf_lsa_header_dump(new->data);
1408 }
1409
1410 return new;
1411 }
1412
1413
1414 /* summary-ASBR-LSA related functions. */
1415 static void ospf_summary_asbr_lsa_body_set(struct stream *s, struct prefix *p,
1416 uint32_t metric)
1417 {
1418 /* Put Network Mask. */
1419 stream_put_ipv4(s, (uint32_t)0);
1420
1421 /* Set # TOS. */
1422 stream_putc(s, (uint8_t)0);
1423
1424 /* Set metric. */
1425 stream_put_ospf_metric(s, metric);
1426 }
1427
1428 static struct ospf_lsa *ospf_summary_asbr_lsa_new(struct ospf_area *area,
1429 struct prefix *p,
1430 uint32_t metric,
1431 struct in_addr id)
1432 {
1433 struct stream *s;
1434 struct ospf_lsa *new;
1435 struct lsa_header *lsah;
1436 int length;
1437
1438 if (id.s_addr == 0xffffffff) {
1439 /* Maybe Link State ID not available. */
1440 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1441 zlog_debug(
1442 "LSA[Type%d]: Link ID not available, can't originate",
1443 OSPF_ASBR_SUMMARY_LSA);
1444 return NULL;
1445 }
1446
1447 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1448 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1449
1450 /* Create new stream for LSA. */
1451 s = stream_new(OSPF_MAX_LSA_SIZE);
1452 lsah = (struct lsa_header *)STREAM_DATA(s);
1453
1454 lsa_header_set(s, LSA_OPTIONS_GET(area), OSPF_ASBR_SUMMARY_LSA, id,
1455 area->ospf->router_id);
1456
1457 /* Set summary-LSA body fields. */
1458 ospf_summary_asbr_lsa_body_set(s, p, metric);
1459
1460 /* Set length. */
1461 length = stream_get_endp(s);
1462 lsah->length = htons(length);
1463
1464 /* Create OSPF LSA instance. */
1465 new = ospf_lsa_new_and_data(length);
1466 new->area = area;
1467 SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1468 new->vrf_id = area->ospf->vrf_id;
1469
1470 /* Copy LSA to store. */
1471 memcpy(new->data, lsah, length);
1472 stream_free(s);
1473
1474 return new;
1475 }
1476
1477 /* Originate summary-ASBR-LSA. */
1478 static struct ospf_lsa *
1479 ospf_asbr_summary_lsa_prepare_and_flood(struct prefix_ipv4 *p, uint32_t metric,
1480 struct ospf_area *area,
1481 struct in_addr id)
1482 {
1483 struct ospf_lsa *new;
1484
1485 /* Create new summary-LSA instance. */
1486 new = ospf_summary_asbr_lsa_new(area, (struct prefix *)p, metric, id);
1487 if (!new)
1488 return NULL;
1489
1490 /* Install LSA to LSDB. */
1491 new = ospf_lsa_install(area->ospf, NULL, new);
1492
1493 /* Update LSA origination count. */
1494 area->ospf->lsa_originate_count++;
1495
1496 /* Flooding new LSA through area. */
1497 ospf_flood_through_area(area, NULL, new);
1498
1499 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1500 zlog_debug("LSA[Type%d:%pI4]: Originate summary-ASBR-LSA %p",
1501 new->data->type, &new->data->id,
1502 (void *)new);
1503 ospf_lsa_header_dump(new->data);
1504 }
1505
1506 return new;
1507 }
1508
1509 struct ospf_lsa *ospf_summary_asbr_lsa_originate(struct prefix_ipv4 *p,
1510 uint32_t metric,
1511 struct ospf_area *area)
1512 {
1513 struct ospf_lsa *new;
1514 struct in_addr id;
1515 enum lsid_status status;
1516
1517 status = ospf_lsa_unique_id(area->ospf, area->lsdb,
1518 OSPF_ASBR_SUMMARY_LSA, p, &id);
1519
1520 if (status == LSID_CHANGE) {
1521 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1522 zlog_debug("Link ID has to be changed.");
1523
1524 new = ospf_handle_summarylsa_lsId_chg(
1525 area, p, OSPF_ASBR_SUMMARY_LSA, metric, id);
1526 return new;
1527 } else if (status == LSID_NOT_AVAILABLE) {
1528 /* Link State ID not available. */
1529 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1530 zlog_debug(
1531 "LSA[Type5]: Link ID not available, can't originate");
1532
1533 return NULL;
1534 }
1535
1536 new = ospf_asbr_summary_lsa_prepare_and_flood(p, metric, area, id);
1537 return new;
1538 }
1539
1540 static struct ospf_lsa *ospf_summary_asbr_lsa_refresh(struct ospf *ospf,
1541 struct ospf_lsa *lsa)
1542 {
1543 struct ospf_lsa *new;
1544 struct summary_lsa *sl;
1545 struct prefix p;
1546 bool ind_lsa = false;
1547
1548 /* Sanity check. */
1549 assert(lsa->data);
1550
1551 if (lsa->area->fr_info.indication_lsa_self &&
1552 (lsa->area->fr_info.indication_lsa_self == lsa))
1553 ind_lsa = true;
1554
1555 sl = (struct summary_lsa *)lsa->data;
1556 p.prefixlen = ip_masklen(sl->mask);
1557 new = ospf_summary_asbr_lsa_new(lsa->area, &p, GET_METRIC(sl->metric),
1558 sl->header.id);
1559 if (!new)
1560 return NULL;
1561
1562 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
1563
1564 ospf_lsa_install(ospf, NULL, new);
1565
1566 /* Flood LSA through area. */
1567 ospf_flood_through_area(new->area, NULL, new);
1568
1569 if (ind_lsa)
1570 new->area->fr_info.indication_lsa_self = new;
1571
1572 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1573 zlog_debug("LSA[Type%d:%pI4]: summary-ASBR-LSA refresh",
1574 new->data->type, &new->data->id);
1575 ospf_lsa_header_dump(new->data);
1576 }
1577
1578 return new;
1579 }
1580
1581 /* AS-external-LSA related functions. */
1582
1583 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1584 is connected, else 0*/
1585 static struct in_addr ospf_external_lsa_nexthop_get(struct ospf *ospf,
1586 struct in_addr nexthop)
1587 {
1588 struct in_addr fwd;
1589 struct prefix nh;
1590 struct listnode *node;
1591 struct ospf_interface *oi;
1592
1593 fwd.s_addr = 0;
1594
1595 if (!nexthop.s_addr)
1596 return fwd;
1597
1598 /* Check whether nexthop is covered by OSPF network. */
1599 nh.family = AF_INET;
1600 nh.u.prefix4 = nexthop;
1601 nh.prefixlen = IPV4_MAX_BITLEN;
1602
1603 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1604 * better to make use of the per-ifp table of ois.
1605 */
1606 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
1607 if (if_is_operative(oi->ifp))
1608 if (oi->address->family == AF_INET)
1609 if (prefix_match(oi->address, &nh))
1610 return nexthop;
1611
1612 return fwd;
1613 }
1614
1615 /* NSSA-external-LSA related functions. */
1616
1617 /* Get 1st IP connection for Forward Addr */
1618
1619 struct in_addr ospf_get_ip_from_ifp(struct ospf_interface *oi)
1620 {
1621 struct in_addr fwd;
1622
1623 fwd.s_addr = INADDR_ANY;
1624
1625 if (if_is_operative(oi->ifp))
1626 return oi->address->u.prefix4;
1627
1628 return fwd;
1629 }
1630
1631 /* Get 1st IP connection for Forward Addr */
1632 struct in_addr ospf_get_nssa_ip(struct ospf_area *area)
1633 {
1634 struct in_addr fwd;
1635 struct in_addr best_default;
1636 struct listnode *node;
1637 struct ospf_interface *oi;
1638
1639 fwd.s_addr = 0;
1640 best_default.s_addr = 0;
1641
1642 for (ALL_LIST_ELEMENTS_RO(area->ospf->oiflist, node, oi)) {
1643 if (if_is_operative(oi->ifp))
1644 if (oi->area->external_routing == OSPF_AREA_NSSA)
1645 if (oi->address
1646 && oi->address->family == AF_INET) {
1647 if (best_default.s_addr == INADDR_ANY)
1648 best_default =
1649 oi->address->u.prefix4;
1650 if (oi->area == area)
1651 return oi->address->u.prefix4;
1652 }
1653 }
1654 if (best_default.s_addr != INADDR_ANY)
1655 return best_default;
1656
1657 return fwd;
1658 }
1659
1660 int metric_type(struct ospf *ospf, uint8_t src, unsigned short instance)
1661 {
1662 struct ospf_redist *red;
1663
1664 red = ospf_redist_lookup(ospf, src, instance);
1665
1666 return ((!red || red->dmetric.type < 0) ? DEFAULT_METRIC_TYPE
1667 : red->dmetric.type);
1668 }
1669
1670 int metric_value(struct ospf *ospf, uint8_t src, unsigned short instance)
1671 {
1672 struct ospf_redist *red;
1673
1674 red = ospf_redist_lookup(ospf, src, instance);
1675 if (!red || red->dmetric.value < 0) {
1676 if (src == DEFAULT_ROUTE) {
1677 if (ospf->default_originate == DEFAULT_ORIGINATE_ZEBRA)
1678 return DEFAULT_DEFAULT_ORIGINATE_METRIC;
1679 else
1680 return DEFAULT_DEFAULT_ALWAYS_METRIC;
1681 } else if (ospf->default_metric < 0)
1682 return DEFAULT_DEFAULT_METRIC;
1683 else
1684 return ospf->default_metric;
1685 }
1686
1687 return red->dmetric.value;
1688 }
1689
1690 /* Set AS-external-LSA body. */
1691 static void ospf_external_lsa_body_set(struct stream *s,
1692 struct external_info *ei,
1693 struct ospf *ospf)
1694 {
1695 struct prefix_ipv4 *p = &ei->p;
1696 struct in_addr mask, fwd_addr;
1697 uint32_t mvalue;
1698 int mtype;
1699 int type;
1700 unsigned short instance;
1701
1702 /* Put Network Mask. */
1703 masklen2ip(p->prefixlen, &mask);
1704 stream_put_ipv4(s, mask.s_addr);
1705
1706 /* If prefix is default, specify DEFAULT_ROUTE. */
1707 type = is_default_prefix4(&ei->p) ? DEFAULT_ROUTE : ei->type;
1708 instance = is_default_prefix4(&ei->p) ? 0 : ei->instance;
1709
1710 mtype = (ROUTEMAP_METRIC_TYPE(ei) != -1)
1711 ? ROUTEMAP_METRIC_TYPE(ei)
1712 : metric_type(ospf, type, instance);
1713
1714 mvalue = (ROUTEMAP_METRIC(ei) != -1)
1715 ? ROUTEMAP_METRIC(ei)
1716 : metric_value(ospf, type, instance);
1717
1718 /* Put type of external metric. */
1719 stream_putc(s, (mtype == EXTERNAL_METRIC_TYPE_2 ? 0x80 : 0));
1720
1721 /* Put 0 metric. TOS metric is not supported. */
1722 stream_put_ospf_metric(s, mvalue);
1723
1724 /* Get forwarding address to nexthop if on the Connection List, else 0.
1725 */
1726 fwd_addr = ospf_external_lsa_nexthop_get(ospf, ei->nexthop);
1727
1728 /* Put forwarding address. */
1729 stream_put_ipv4(s, fwd_addr.s_addr);
1730
1731 /* Put route tag */
1732 stream_putl(s, ei->tag);
1733 }
1734
1735 /* Create new external-LSA. */
1736 static struct ospf_lsa *
1737 ospf_exnl_lsa_prepare_and_flood(struct ospf *ospf, struct external_info *ei,
1738 struct in_addr id)
1739 {
1740 struct stream *s;
1741 struct lsa_header *lsah;
1742 struct ospf_lsa *new;
1743 int length;
1744
1745 /* Create new stream for LSA. */
1746 s = stream_new(OSPF_MAX_LSA_SIZE);
1747 lsah = (struct lsa_header *)STREAM_DATA(s);
1748
1749 /* Set LSA common header fields. */
1750 lsa_header_set(s, OSPF_OPTION_E, OSPF_AS_EXTERNAL_LSA, id,
1751 ospf->router_id);
1752
1753 /* Set AS-external-LSA body fields. */
1754 ospf_external_lsa_body_set(s, ei, ospf);
1755
1756 /* Set length. */
1757 length = stream_get_endp(s);
1758 lsah->length = htons(length);
1759
1760 /* Now, create OSPF LSA instance. */
1761 new = ospf_lsa_new_and_data(length);
1762 new->area = NULL;
1763 SET_FLAG(new->flags,
1764 OSPF_LSA_SELF | OSPF_LSA_APPROVED | OSPF_LSA_SELF_CHECKED);
1765 new->vrf_id = ospf->vrf_id;
1766
1767 /* Copy LSA data to store, discard stream. */
1768 memcpy(new->data, lsah, length);
1769 stream_free(s);
1770
1771 return new;
1772 }
1773
1774 static struct ospf_lsa *ospf_handle_exnl_lsa_lsId_chg(struct ospf *ospf,
1775 struct external_info *ei,
1776 struct in_addr id)
1777 {
1778 struct ospf_lsa *lsa;
1779 struct as_external_lsa *al;
1780 struct in_addr mask;
1781 struct ospf_lsa *new;
1782 struct external_info ei_summary = {};
1783 struct external_info *ei_old;
1784
1785 lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
1786 ei->p.prefix, ospf->router_id);
1787
1788 if (!lsa) {
1789 flog_warn(EC_OSPF_LSA_NULL, "(%s): LSA not found", __func__);
1790 return NULL;
1791 }
1792
1793 ei_old = ospf_external_info_check(ospf, lsa);
1794
1795 al = (struct as_external_lsa *)lsa->data;
1796
1797 if (!ei_old) {
1798 /* eii_old pointer of LSA is NULL, this
1799 * must be external aggregate route.
1800 */
1801 ei_summary.p.family = AF_INET;
1802 ei_summary.p.prefix = al->header.id;
1803 ei_summary.p.prefixlen = ip_masklen(al->mask);
1804 ei_summary.tag = (unsigned long)ntohl(al->e[0].route_tag);
1805 ei_old = &ei_summary;
1806 }
1807
1808 /* change the mask */
1809 masklen2ip(ei->p.prefixlen, &mask);
1810 al->mask.s_addr = mask.s_addr;
1811
1812 /*Refresh the LSA with new LSA*/
1813 ospf_external_lsa_refresh(ospf, lsa, ei, LSA_REFRESH_FORCE, 0);
1814
1815 /*Originate the old LSA with changed LSID*/
1816 new = ospf_exnl_lsa_prepare_and_flood(ospf, ei_old, id);
1817
1818 return new;
1819 }
1820
1821 static struct ospf_lsa *ospf_external_lsa_new(struct ospf *ospf,
1822 struct external_info *ei,
1823 struct in_addr *old_id)
1824 {
1825 struct ospf_lsa *new;
1826 struct in_addr id;
1827 enum lsid_status status;
1828
1829 if (ei == NULL) {
1830 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1831 zlog_debug(
1832 "LSA[Type5]: External info is NULL, can't originate");
1833 return NULL;
1834 }
1835
1836 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1837 zlog_debug("LSA[Type5]: Originate AS-external-LSA instance");
1838
1839 /* If old Link State ID is specified, refresh LSA with same ID. */
1840 if (old_id)
1841 id = *old_id;
1842 /* Get Link State with unique ID. */
1843 else {
1844 status = ospf_lsa_unique_id(ospf, ospf->lsdb,
1845 OSPF_AS_EXTERNAL_LSA, &ei->p, &id);
1846
1847 if (status == LSID_CHANGE) {
1848 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1849 zlog_debug("Link ID has to be changed.");
1850
1851 new = ospf_handle_exnl_lsa_lsId_chg(ospf, ei, id);
1852 return new;
1853 } else if (status == LSID_NOT_AVAILABLE) {
1854 /* Link State ID not available. */
1855 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1856 zlog_debug(
1857 "LSA[Type5]: Link ID not available, can't originate");
1858 return NULL;
1859 }
1860 }
1861
1862 new = ospf_exnl_lsa_prepare_and_flood(ospf, ei, id);
1863
1864 return new;
1865 }
1866
1867 /* As Type-7 */
1868 static void ospf_install_flood_nssa(struct ospf *ospf, struct ospf_lsa *lsa)
1869 {
1870 struct ospf_lsa *new;
1871 struct as_external_lsa *extlsa;
1872 struct ospf_area *area;
1873 struct listnode *node, *nnode;
1874
1875 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1876 * which originated from an NSSA area. In which case it should not be
1877 * flooded back to NSSA areas.
1878 */
1879 if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
1880 return;
1881
1882 /* NSSA Originate or Refresh (If anyNSSA)
1883
1884 LSA is self-originated. And just installed as Type-5.
1885 Additionally, install as Type-7 LSDB for every attached NSSA.
1886
1887 P-Bit controls which ABR performs translation to outside world; If
1888 we are an ABR....do not set the P-bit, because we send the Type-5,
1889 not as the ABR Translator, but as the ASBR owner within the AS!
1890
1891 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1892 elected ABR Translator will see the P-bit, Translate, and re-flood.
1893
1894 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1895 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1896
1897 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
1898 /* Don't install Type-7 LSA's into nonNSSA area */
1899 if (area->external_routing != OSPF_AREA_NSSA)
1900 continue;
1901
1902 /* make lsa duplicate, lock=1 */
1903 new = ospf_lsa_dup(lsa);
1904 new->area = area;
1905 new->data->type = OSPF_AS_NSSA_LSA;
1906
1907 /* set P-bit if not ABR */
1908 if (!IS_OSPF_ABR(ospf)) {
1909 SET_FLAG(new->data->options, OSPF_OPTION_NP);
1910
1911 /* set non-zero FWD ADDR
1912
1913 draft-ietf-ospf-nssa-update-09.txt
1914
1915 if the network between the NSSA AS boundary router and
1916 the
1917 adjacent AS is advertised into OSPF as an internal OSPF
1918 route,
1919 the forwarding address should be the next op address as
1920 is cu
1921 currently done with type-5 LSAs. If the intervening
1922 network is
1923 not adversited into OSPF as an internal OSPF route and
1924 the
1925 type-7 LSA's P-bit is set a forwarding address should be
1926 selected from one of the router's active OSPF interface
1927 addresses
1928 which belong to the NSSA. If no such addresses exist,
1929 then
1930 no type-7 LSA's with the P-bit set should originate from
1931 this
1932 router. */
1933
1934 /* kevinm: not updating lsa anymore, just new */
1935 extlsa = (struct as_external_lsa *)(new->data);
1936
1937 if (extlsa->e[0].fwd_addr.s_addr == INADDR_ANY)
1938 extlsa->e[0].fwd_addr = ospf_get_nssa_ip(
1939 area); /* this NSSA area in ifp */
1940
1941 if (extlsa->e[0].fwd_addr.s_addr == INADDR_ANY) {
1942 if (IS_DEBUG_OSPF_NSSA)
1943 zlog_debug(
1944 "LSA[Type-7]: Could not build FWD-ADDR");
1945 ospf_lsa_discard(new);
1946 return;
1947 }
1948 }
1949
1950 /* install also as Type-7 */
1951 ospf_lsa_install(ospf, NULL,
1952 new); /* Remove Old, Lock New = 2 */
1953
1954 /* will send each copy, lock=2+n */
1955 ospf_flood_through_as(
1956 ospf, NULL, new); /* all attached NSSA's, no AS/STUBs */
1957 }
1958 }
1959
1960 static struct ospf_lsa *ospf_lsa_translated_nssa_new(struct ospf *ospf,
1961 struct ospf_lsa *type7)
1962 {
1963
1964 struct ospf_lsa *new;
1965 struct as_external_lsa *ext, *extnew;
1966 struct external_info ei;
1967
1968 ext = (struct as_external_lsa *)(type7->data);
1969
1970 /* need external_info struct, fill in bare minimum */
1971 ei.p.family = AF_INET;
1972 ei.p.prefix = type7->data->id;
1973 ei.p.prefixlen = ip_masklen(ext->mask);
1974 ei.type = ZEBRA_ROUTE_OSPF;
1975 ei.nexthop = ext->header.adv_router;
1976 ei.route_map_set.metric = -1;
1977 ei.route_map_set.metric_type = -1;
1978 ei.metric = DEFAULT_DEFAULT_METRIC;
1979 ei.max_metric = OSPF_LS_INFINITY;
1980 ei.min_metric = 0;
1981 ei.tag = 0;
1982 ei.instance = 0;
1983
1984 if ((new = ospf_external_lsa_new(ospf, &ei, &type7->data->id))
1985 == NULL) {
1986 if (IS_DEBUG_OSPF_NSSA)
1987 zlog_debug(
1988 "%s: Could not originate Translated Type-5 for %pI4",
1989 __func__, &ei.p.prefix);
1990 return NULL;
1991 }
1992
1993 extnew = (struct as_external_lsa *)(new->data);
1994
1995 /* copy over Type-7 data to new */
1996 extnew->e[0].tos = ext->e[0].tos;
1997 extnew->e[0].route_tag = ext->e[0].route_tag;
1998 if (type7->area->suppress_fa) {
1999 extnew->e[0].fwd_addr.s_addr = 0;
2000 if (IS_DEBUG_OSPF_NSSA)
2001 zlog_debug("%s: Suppress forwarding address for %pI4",
2002 __func__, &ei.p.prefix);
2003 } else
2004 extnew->e[0].fwd_addr.s_addr = ext->e[0].fwd_addr.s_addr;
2005 new->data->ls_seqnum = type7->data->ls_seqnum;
2006
2007 /* add translated flag, checksum and lock new lsa */
2008 SET_FLAG(new->flags, OSPF_LSA_LOCAL_XLT); /* Translated from 7 */
2009 new = ospf_lsa_lock(new);
2010
2011 return new;
2012 }
2013
2014 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
2015 struct ospf_lsa *ospf_translated_nssa_originate(struct ospf *ospf,
2016 struct ospf_lsa *type7,
2017 struct ospf_lsa *type5)
2018 {
2019 struct ospf_lsa *new, *translated_lsa;
2020 struct as_external_lsa *extnew;
2021
2022 if (ospf->gr_info.restart_in_progress) {
2023 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
2024 zlog_debug(
2025 "LSA[Translated Type5]: Graceful Restart in progress, don't originate");
2026 return NULL;
2027 }
2028
2029 /* we cant use ospf_external_lsa_originate() as we need to set
2030 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
2031 */
2032
2033 if ((translated_lsa = ospf_lsa_translated_nssa_new(ospf, type7)) ==
2034 NULL) {
2035 if (IS_DEBUG_OSPF_NSSA)
2036 zlog_debug(
2037 "%s: Could not translate Type-7, Id %pI4, to Type-5",
2038 __func__, &type7->data->id);
2039 return NULL;
2040 }
2041
2042 extnew = (struct as_external_lsa *)translated_lsa->data;
2043
2044 /* Update LSA sequence number from translated Type-5 LSA */
2045 if (type5)
2046 translated_lsa->data->ls_seqnum = lsa_seqnum_increment(type5);
2047
2048 if ((new = ospf_lsa_install(ospf, NULL, translated_lsa)) == NULL) {
2049 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
2050 "%s: Could not install LSA id %pI4", __func__,
2051 &type7->data->id);
2052 ospf_lsa_free(translated_lsa);
2053 return NULL;
2054 }
2055
2056 if (IS_DEBUG_OSPF_NSSA) {
2057 zlog_debug("%s: translated Type 7, installed", __func__);
2058 ospf_lsa_header_dump(new->data);
2059 zlog_debug(" Network mask: %d", ip_masklen(extnew->mask));
2060 zlog_debug(" Forward addr: %pI4",
2061 &extnew->e[0].fwd_addr);
2062 }
2063
2064 ospf->lsa_originate_count++;
2065 ospf_flood_through_as(ospf, NULL, new);
2066
2067 return new;
2068 }
2069
2070 /* Refresh Translated from NSSA AS-external-LSA. */
2071 struct ospf_lsa *ospf_translated_nssa_refresh(struct ospf *ospf,
2072 struct ospf_lsa *type7,
2073 struct ospf_lsa *type5)
2074 {
2075 struct ospf_lsa *new = NULL, *translated_lsa = NULL;
2076 struct as_external_lsa *extold = NULL;
2077 uint32_t ls_seqnum = 0;
2078
2079 /* Sanity checks. */
2080 assert(type7 || type5);
2081 if (!(type7 || type5))
2082 return NULL;
2083 if (type7)
2084 assert(type7->data);
2085 if (type5)
2086 assert(type5->data);
2087 assert(ospf->anyNSSA);
2088
2089 /* get required data according to what has been given */
2090 if (type7 && type5 == NULL) {
2091 /* find the translated Type-5 for this Type-7 */
2092 struct as_external_lsa *ext =
2093 (struct as_external_lsa *)(type7->data);
2094 struct prefix_ipv4 p = {
2095 .prefix = type7->data->id,
2096 .prefixlen = ip_masklen(ext->mask),
2097 .family = AF_INET,
2098 };
2099
2100 type5 = ospf_external_info_find_lsa(ospf, &p);
2101 } else if (type5 && type7 == NULL) {
2102 /* find the type-7 from which supplied type-5 was translated,
2103 * ie find first type-7 with same LSA Id.
2104 */
2105 struct listnode *ln, *lnn;
2106 struct route_node *rn;
2107 struct ospf_lsa *lsa;
2108 struct ospf_area *area;
2109
2110 for (ALL_LIST_ELEMENTS(ospf->areas, ln, lnn, area)) {
2111 if (area->external_routing != OSPF_AREA_NSSA && !type7)
2112 continue;
2113
2114 LSDB_LOOP (NSSA_LSDB(area), rn, lsa) {
2115 if (lsa->data->id.s_addr
2116 == type5->data->id.s_addr) {
2117 type7 = lsa;
2118 break;
2119 }
2120 }
2121 }
2122 }
2123
2124 /* do we have type7? */
2125 if (!type7) {
2126 if (IS_DEBUG_OSPF_NSSA)
2127 zlog_debug("%s: no Type-7 found for Type-5 LSA Id %pI4",
2128 __func__, &type5->data->id);
2129 return NULL;
2130 }
2131
2132 /* do we have valid translated type5? */
2133 if (type5 == NULL || !CHECK_FLAG(type5->flags, OSPF_LSA_LOCAL_XLT)) {
2134 if (IS_DEBUG_OSPF_NSSA)
2135 zlog_debug(
2136 "%s: No translated Type-5 found for Type-7 with Id %pI4",
2137 __func__, &type7->data->id);
2138 return NULL;
2139 }
2140
2141 extold = (struct as_external_lsa *)type5->data;
2142 if (type7->area->suppress_fa == 1) {
2143 if (extold->e[0].fwd_addr.s_addr == 0)
2144 ls_seqnum = ntohl(type5->data->ls_seqnum);
2145 }
2146
2147 /* Delete LSA from neighbor retransmit-list. */
2148 ospf_ls_retransmit_delete_nbr_as(ospf, type5);
2149
2150 /* create new translated LSA */
2151 if ((translated_lsa = ospf_lsa_translated_nssa_new(ospf, type7)) ==
2152 NULL) {
2153 if (IS_DEBUG_OSPF_NSSA)
2154 zlog_debug(
2155 "%s: Could not translate Type-7 for %pI4 to Type-5",
2156 __func__, &type7->data->id);
2157 return NULL;
2158 }
2159
2160 if (type7->area->suppress_fa == 1) {
2161 if (extold->e[0].fwd_addr.s_addr == 0)
2162 translated_lsa->data->ls_seqnum = htonl(ls_seqnum + 1);
2163 }
2164
2165 if (!(new = ospf_lsa_install(ospf, NULL, translated_lsa))) {
2166 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
2167 "%s: Could not install translated LSA, Id %pI4",
2168 __func__, &type7->data->id);
2169 ospf_lsa_free(translated_lsa);
2170 return NULL;
2171 }
2172
2173 /* Flood LSA through area. */
2174 ospf_flood_through_as(ospf, NULL, new);
2175
2176 return new;
2177 }
2178
2179 /* Originate an AS-external-LSA, install and flood. */
2180 struct ospf_lsa *ospf_external_lsa_originate(struct ospf *ospf,
2181 struct external_info *ei)
2182 {
2183 struct ospf_lsa *new;
2184
2185 if (ospf->gr_info.restart_in_progress) {
2186 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
2187 zlog_debug(
2188 "LSA[Type5]: Graceful Restart in progress, don't originate");
2189 return NULL;
2190 }
2191
2192 /* Added for NSSA project....
2193
2194 External LSAs are originated in ASBRs as usual, but for NSSA
2195 systems.
2196 there is the global Type-5 LSDB and a Type-7 LSDB installed for
2197 every area. The Type-7's are flooded to every IR and every ABR; We
2198 install the Type-5 LSDB so that the normal "refresh" code operates
2199 as usual, and flag them as not used during ASE calculations. The
2200 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
2201 Address of non-zero.
2202
2203 If an ABR is the elected NSSA translator, following SPF and during
2204 the ABR task it will translate all the scanned Type-7's, with P-bit
2205 ON and not-self generated, and translate to Type-5's throughout the
2206 non-NSSA/STUB AS.
2207
2208 A difference in operation depends whether this ASBR is an ABR
2209 or not. If not an ABR, the P-bit is ON, to indicate that any
2210 elected NSSA-ABR can perform its translation.
2211
2212 If an ABR, the P-bit is OFF; No ABR will perform translation and
2213 this ASBR will flood the Type-5 LSA as usual.
2214
2215 For the case where this ASBR is not an ABR, the ASE calculations
2216 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
2217 demonstrate to the user that there are LSA's that belong to any
2218 attached NSSA.
2219
2220 Finally, it just so happens that when the ABR is translating every
2221 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
2222 approved Type-5 (translated from Type-7); at the end of translation
2223 if any Translated Type-5's remain unapproved, then they must be
2224 flushed from the AS.
2225
2226 */
2227
2228 if (ospf->router_id.s_addr == INADDR_ANY) {
2229 if (ei && IS_DEBUG_OSPF_EVENT)
2230 zlog_debug(
2231 "LSA[Type5:%pI4]: deferring AS-external-LSA origination, router ID is zero",
2232 &ei->p.prefix);
2233 return NULL;
2234 }
2235
2236 /* Create new AS-external-LSA instance. */
2237 if ((new = ospf_external_lsa_new(ospf, ei, NULL)) == NULL) {
2238 if (ei && IS_DEBUG_OSPF_EVENT)
2239 zlog_debug(
2240 "LSA[Type5:%pI4]: Could not originate AS-external-LSA",
2241 &ei->p.prefix);
2242 return NULL;
2243 }
2244
2245 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2246 ospf_lsa_install(ospf, NULL, new);
2247
2248 /* Update LSA origination count. */
2249 ospf->lsa_originate_count++;
2250
2251 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2252 ospf_flood_through_as(ospf, NULL, new);
2253
2254 /* If there is any attached NSSA, do special handling */
2255 if (ospf->anyNSSA &&
2256 /* stay away from translated LSAs! */
2257 !(CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT)))
2258 ospf_install_flood_nssa(
2259 ospf, new); /* Install/Flood Type-7 to all NSSAs */
2260
2261 /* Debug logging. */
2262 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
2263 zlog_debug("LSA[Type%d:%pI4]: Originate AS-external-LSA %p",
2264 new->data->type, &new->data->id,
2265 (void *)new);
2266 ospf_lsa_header_dump(new->data);
2267 }
2268
2269 return new;
2270 }
2271
2272 /* Originate an NSSA-LSA, install and flood. */
2273 struct ospf_lsa *ospf_nssa_lsa_originate(struct ospf_area *area,
2274 struct external_info *ei)
2275 {
2276 struct ospf *ospf = area->ospf;
2277 struct ospf_lsa *new;
2278
2279 if (ospf->gr_info.restart_in_progress) {
2280 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
2281 zlog_debug(
2282 "LSA[Type7]: Graceful Restart in progress, don't originate");
2283 return NULL;
2284 }
2285
2286 if (ospf->router_id.s_addr == INADDR_ANY) {
2287 if (IS_DEBUG_OSPF_EVENT)
2288 zlog_debug(
2289 "LSA[Type7:%pI4]: deferring NSSA-LSA origination, router ID is zero",
2290 &ei->p.prefix);
2291 return NULL;
2292 }
2293
2294 /* Create new NSSA-LSA instance. */
2295 if ((new = ospf_external_lsa_new(ospf, ei, NULL)) == NULL) {
2296 if (IS_DEBUG_OSPF_EVENT)
2297 zlog_debug(
2298 "LSA[Type7:%pI4]: Could not originate NSSA-LSA",
2299 &ei->p.prefix);
2300 return NULL;
2301 }
2302 new->data->type = OSPF_AS_NSSA_LSA;
2303 new->area = area;
2304
2305 /* Install newly created LSA into Type-7 LSDB. */
2306 ospf_lsa_install(ospf, NULL, new);
2307
2308 /* Update LSA origination count. */
2309 ospf->lsa_originate_count++;
2310
2311 /* Flooding new LSA */
2312 ospf_flood_through_area(area, NULL, new);
2313
2314 /* Debug logging. */
2315 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
2316 zlog_debug("LSA[Type%d:%pI4]: Originate NSSA-LSA %p",
2317 new->data->type, &new->data->id, (void *)new);
2318 ospf_lsa_header_dump(new->data);
2319 }
2320
2321 return new;
2322 }
2323
2324 /* Refresh NSSA-LSA. */
2325 struct ospf_lsa *ospf_nssa_lsa_refresh(struct ospf_area *area,
2326 struct ospf_lsa *lsa,
2327 struct external_info *ei)
2328 {
2329 struct ospf *ospf = area->ospf;
2330 struct ospf_lsa *new;
2331
2332 /* Delete LSA from neighbor retransmit-list. */
2333 ospf_ls_retransmit_delete_nbr_as(ospf, lsa);
2334
2335 /* Unregister AS-external-LSA from refresh-list. */
2336 ospf_refresher_unregister_lsa(ospf, lsa);
2337
2338 /* Create new NSSA-LSA instance. */
2339 if ((new = ospf_external_lsa_new(ospf, ei, NULL)) == NULL) {
2340 if (IS_DEBUG_OSPF_EVENT)
2341 zlog_debug(
2342 "LSA[Type7:%pI4]: Could not originate NSSA-LSA",
2343 &ei->p.prefix);
2344 return NULL;
2345 }
2346 new->data->type = OSPF_AS_NSSA_LSA;
2347 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
2348 new->area = area;
2349
2350 /* Install newly created LSA into Type-7 LSDB. */
2351 ospf_lsa_install(ospf, NULL, new);
2352
2353 /* Flooding new LSA */
2354 ospf_flood_through_area(area, NULL, new);
2355
2356 /* Debug logging. */
2357 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
2358 zlog_debug("LSA[Type%d:%pI4]: NSSA-LSA refresh",
2359 new->data->type, &new->data->id);
2360 ospf_lsa_header_dump(new->data);
2361 }
2362
2363 return new;
2364 }
2365
2366 static struct external_info *ospf_default_external_info(struct ospf *ospf)
2367 {
2368 int type;
2369 struct prefix_ipv4 p;
2370 struct external_info *default_ei;
2371 int ret = 0;
2372
2373 p.family = AF_INET;
2374 p.prefix.s_addr = 0;
2375 p.prefixlen = 0;
2376
2377 default_ei = ospf_external_info_lookup(ospf, DEFAULT_ROUTE, 0, &p);
2378 if (!default_ei)
2379 return NULL;
2380
2381 /* First, lookup redistributed default route. */
2382 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
2383 struct list *ext_list;
2384
2385 if (type == ZEBRA_ROUTE_OSPF)
2386 continue;
2387
2388 ext_list = ospf->external[type];
2389 if (!ext_list)
2390 continue;
2391
2392 ret = ospf_external_default_routemap_apply_walk(ospf, ext_list,
2393 default_ei);
2394 if (ret)
2395 return default_ei;
2396 }
2397
2398 return NULL;
2399 }
2400
2401 void ospf_external_lsa_rid_change(struct ospf *ospf)
2402 {
2403 struct external_info *ei;
2404 struct ospf_external_aggr_rt *aggr;
2405 struct ospf_lsa *lsa = NULL;
2406 int force;
2407 int type;
2408
2409 for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
2410 struct route_node *rn;
2411 struct route_table *rt;
2412 struct list *ext_list;
2413 struct listnode *node;
2414 struct ospf_external *ext;
2415
2416 ext_list = ospf->external[type];
2417 if (!ext_list)
2418 continue;
2419
2420 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
2421 /* Originate As-external-LSA from all type of
2422 * distribute source.
2423 */
2424 rt = ext->external_info;
2425 if (!rt)
2426 continue;
2427
2428 for (rn = route_top(rt); rn; rn = route_next(rn)) {
2429 ei = rn->info;
2430
2431 if (!ei)
2432 continue;
2433
2434 if (is_default_prefix4(&ei->p))
2435 continue;
2436
2437 lsa = ospf_external_info_find_lsa(ospf, &ei->p);
2438
2439 aggr = ospf_external_aggr_match(ospf, &ei->p);
2440 if (aggr) {
2441
2442 if (!ospf_redistribute_check(ospf, ei,
2443 NULL))
2444 continue;
2445
2446 if (IS_DEBUG_OSPF(lsa, EXTNL_LSA_AGGR))
2447 zlog_debug(
2448 "Originate Summary LSA after reset/router-ID change");
2449
2450 /* Here the LSA is originated as new */
2451 ospf_originate_summary_lsa(ospf, aggr,
2452 ei);
2453 } else if (lsa) {
2454 /* LSA needs to be refreshed even if
2455 * there is no change in the route
2456 * params if the LSA is in maxage.
2457 */
2458 if (IS_LSA_MAXAGE(lsa))
2459 force = LSA_REFRESH_FORCE;
2460 else
2461 force = LSA_REFRESH_IF_CHANGED;
2462
2463 ospf_external_lsa_refresh(ospf, lsa,
2464 ei, force, 0);
2465 } else {
2466 if (!ospf_redistribute_check(ospf, ei,
2467 NULL))
2468 continue;
2469
2470 if (!ospf_external_lsa_originate(ospf,
2471 ei))
2472 flog_warn(
2473 EC_OSPF_LSA_INSTALL_FAILURE,
2474 "LSA: AS-external-LSA was not originated.");
2475 }
2476 }
2477 }
2478 }
2479
2480 ei = ospf_default_external_info(ospf);
2481 if (ei && !ospf_external_lsa_originate(ospf, ei)) {
2482 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
2483 "LSA: AS-external-LSA for default route was not originated.");
2484 }
2485 }
2486
2487 /* Flush any NSSA LSAs for given prefix */
2488 void ospf_nssa_lsa_flush(struct ospf *ospf, struct prefix_ipv4 *p)
2489 {
2490 struct listnode *node, *nnode;
2491 struct ospf_lsa *lsa = NULL;
2492 struct ospf_area *area;
2493
2494 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
2495 if (area->external_routing == OSPF_AREA_NSSA) {
2496 lsa = ospf_lsa_lookup(ospf, area, OSPF_AS_NSSA_LSA,
2497 p->prefix, ospf->router_id);
2498 if (!lsa) {
2499 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2500 zlog_debug(
2501 "LSA: There is no such AS-NSSA-LSA %pFX in LSDB",
2502 p);
2503 continue;
2504 }
2505 ospf_ls_retransmit_delete_nbr_area(area, lsa);
2506 if (!IS_LSA_MAXAGE(lsa)) {
2507 ospf_refresher_unregister_lsa(ospf, lsa);
2508 ospf_lsa_flush_area(lsa, area);
2509 }
2510 }
2511 }
2512 }
2513
2514 /* Flush an AS-external-LSA from LSDB and routing domain. */
2515 void ospf_external_lsa_flush(struct ospf *ospf, uint8_t type,
2516 struct prefix_ipv4 *p,
2517 ifindex_t ifindex /*, struct in_addr nexthop */)
2518 {
2519 struct ospf_lsa *lsa;
2520
2521 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2522 zlog_debug("LSA: Flushing AS-external-LSA %pFX", p);
2523
2524 /* First lookup LSA from LSDB. */
2525 if (!(lsa = ospf_external_info_find_lsa(ospf, p))) {
2526 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2527 zlog_debug(
2528 "LSA: There is no such AS-external-LSA %pFX in LSDB",
2529 p);
2530 return;
2531 }
2532
2533 /* If LSA is selforiginated, not a translated LSA, and there is
2534 * NSSA area, flush Type-7 LSA's at first.
2535 */
2536 if (IS_LSA_SELF(lsa) && (ospf->anyNSSA)
2537 && !(CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT)))
2538 ospf_nssa_lsa_flush(ospf, p);
2539
2540 if (!IS_LSA_MAXAGE(lsa)) {
2541 /* Sweep LSA from Link State Retransmit List. */
2542 ospf_ls_retransmit_delete_nbr_as(ospf, lsa);
2543
2544 /* Unregister LSA from Refresh queue. */
2545 ospf_refresher_unregister_lsa(ospf, lsa);
2546
2547 /* Flush AS-external-LSA through AS. */
2548 ospf_lsa_flush_as(ospf, lsa);
2549 }
2550
2551 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2552 zlog_debug("%s: stop", __func__);
2553 }
2554
2555 void ospf_external_lsa_refresh_default(struct ospf *ospf)
2556 {
2557 struct prefix_ipv4 p;
2558 struct external_info *ei;
2559 struct ospf_lsa *lsa;
2560
2561 p.family = AF_INET;
2562 p.prefixlen = 0;
2563 p.prefix.s_addr = INADDR_ANY;
2564
2565 ei = ospf_default_external_info(ospf);
2566 lsa = ospf_external_info_find_lsa(ospf, &p);
2567
2568 if (ei && lsa) {
2569 if (IS_DEBUG_OSPF_EVENT)
2570 zlog_debug("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
2571 (void *)lsa);
2572 ospf_external_lsa_refresh(ospf, lsa, ei, LSA_REFRESH_FORCE,
2573 false);
2574 } else if (ei && !lsa) {
2575 if (IS_DEBUG_OSPF_EVENT)
2576 zlog_debug(
2577 "LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2578 ospf_external_lsa_originate(ospf, ei);
2579 } else if (lsa) {
2580 if (IS_DEBUG_OSPF_EVENT)
2581 zlog_debug("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2582 ospf_external_lsa_flush(ospf, DEFAULT_ROUTE, &p, 0);
2583 }
2584 }
2585
2586 void ospf_external_lsa_refresh_type(struct ospf *ospf, uint8_t type,
2587 unsigned short instance, int force)
2588 {
2589 struct route_node *rn;
2590 struct external_info *ei;
2591 struct ospf_external *ext;
2592
2593 if (type == DEFAULT_ROUTE)
2594 return;
2595
2596 ext = ospf_external_lookup(ospf, type, instance);
2597
2598 if (ext && EXTERNAL_INFO(ext)) {
2599 /* Refresh each redistributed AS-external-LSAs. */
2600 for (rn = route_top(EXTERNAL_INFO(ext)); rn;
2601 rn = route_next(rn)) {
2602 ei = rn->info;
2603 if (ei) {
2604 if (!is_default_prefix4(&ei->p)) {
2605 struct ospf_lsa *lsa;
2606 struct ospf_external_aggr_rt *aggr;
2607
2608 aggr = ospf_external_aggr_match(ospf,
2609 &ei->p);
2610 lsa = ospf_external_info_find_lsa(
2611 ospf, &ei->p);
2612 if (aggr) {
2613 /* Check the AS-external-LSA
2614 * should be originated.
2615 */
2616 if (!ospf_redistribute_check(
2617 ospf, ei, NULL)) {
2618
2619 ospf_unlink_ei_from_aggr(
2620 ospf, aggr, ei);
2621 continue;
2622 }
2623
2624 if (IS_DEBUG_OSPF(
2625 lsa,
2626 EXTNL_LSA_AGGR))
2627 zlog_debug(
2628 "%s: Send Aggreate LSA (%pFX)",
2629 __func__,
2630 &aggr->p);
2631
2632 ospf_originate_summary_lsa(
2633 ospf, aggr, ei);
2634
2635 } else if (lsa) {
2636
2637 if (IS_LSA_MAXAGE(lsa))
2638 force = LSA_REFRESH_FORCE;
2639
2640 ospf_external_lsa_refresh(
2641 ospf, lsa, ei, force,
2642 false);
2643 } else {
2644 if (!ospf_redistribute_check(
2645 ospf, ei, NULL))
2646 continue;
2647 ospf_external_lsa_originate(
2648 ospf, ei);
2649 }
2650 }
2651 }
2652 }
2653 }
2654 }
2655
2656 /* Refresh AS-external-LSA. */
2657 struct ospf_lsa *ospf_external_lsa_refresh(struct ospf *ospf,
2658 struct ospf_lsa *lsa,
2659 struct external_info *ei, int force,
2660 bool is_aggr)
2661 {
2662 struct ospf_lsa *new;
2663 int changed = 0;
2664
2665 /* Check the AS-external-LSA should be originated. */
2666 if (!is_aggr)
2667 if (!ospf_redistribute_check(ospf, ei, &changed)) {
2668 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
2669 zlog_debug(
2670 "LSA[Type%d:%pI4] Could not be refreshed, redist check fail",
2671 lsa->data->type,
2672 &lsa->data->id);
2673
2674 ospf_external_lsa_flush(ospf, ei->type, &ei->p,
2675 ei->ifindex /*, ei->nexthop */);
2676 return NULL;
2677 }
2678
2679 if (!changed && !force) {
2680 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
2681 zlog_debug(
2682 "LSA[Type%d:%pI4]: Not refreshed, not changed/forced",
2683 lsa->data->type, &lsa->data->id);
2684 return NULL;
2685 }
2686
2687 /* Delete LSA from neighbor retransmit-list. */
2688 ospf_ls_retransmit_delete_nbr_as(ospf, lsa);
2689
2690 /* Unregister AS-external-LSA from refresh-list. */
2691 ospf_refresher_unregister_lsa(ospf, lsa);
2692
2693 new = ospf_external_lsa_new(ospf, ei, &lsa->data->id);
2694
2695 if (new == NULL) {
2696 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
2697 zlog_debug("LSA[Type%d:%pI4]: Could not be refreshed",
2698 lsa->data->type, &lsa->data->id);
2699 return NULL;
2700 }
2701
2702 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
2703
2704 ospf_lsa_install(ospf, NULL, new); /* As type-5. */
2705
2706 /* Flood LSA through AS. */
2707 ospf_flood_through_as(ospf, NULL, new);
2708
2709 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2710 if (ospf->anyNSSA && !(CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT)))
2711 ospf_install_flood_nssa(ospf,
2712 new); /* Install/Flood per new rules */
2713
2714 /* Register self-originated LSA to refresh queue.
2715 * Translated LSAs should not be registered, but refreshed upon
2716 * refresh of the Type-7
2717 */
2718 if (!CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT))
2719 ospf_refresher_register_lsa(ospf, new);
2720
2721 /* Debug logging. */
2722 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
2723 zlog_debug("LSA[Type%d:%pI4]: AS-external-LSA refresh",
2724 new->data->type, &new->data->id);
2725 ospf_lsa_header_dump(new->data);
2726 }
2727
2728 return new;
2729 }
2730
2731
2732 /* LSA installation functions. */
2733
2734 /* Install router-LSA to an area. */
2735 static struct ospf_lsa *
2736 ospf_router_lsa_install(struct ospf *ospf, struct ospf_lsa *new, int rt_recalc)
2737 {
2738 struct ospf_area *area = new->area;
2739
2740 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2741 The entire routing table must be recalculated, starting with
2742 the shortest path calculations for each area (not just the
2743 area whose link-state database has changed).
2744 */
2745
2746 if (IS_LSA_SELF(new)) {
2747
2748 /* Only install LSA if it is originated/refreshed by us.
2749 * If LSA was received by flooding, the RECEIVED flag is set so
2750 * do
2751 * not link the LSA */
2752 if (CHECK_FLAG(new->flags, OSPF_LSA_RECEIVED))
2753 return new; /* ignore stale LSA */
2754
2755 /* Set self-originated router-LSA. */
2756 ospf_lsa_unlock(&area->router_lsa_self);
2757 area->router_lsa_self = ospf_lsa_lock(new);
2758
2759 ospf_refresher_register_lsa(ospf, new);
2760 }
2761 if (rt_recalc)
2762 ospf_spf_calculate_schedule(ospf, SPF_FLAG_ROUTER_LSA_INSTALL);
2763 return new;
2764 }
2765
2766 /* Install network-LSA to an area. */
2767 static struct ospf_lsa *ospf_network_lsa_install(struct ospf *ospf,
2768 struct ospf_interface *oi,
2769 struct ospf_lsa *new,
2770 int rt_recalc)
2771 {
2772
2773 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2774 The entire routing table must be recalculated, starting with
2775 the shortest path calculations for each area (not just the
2776 area whose link-state database has changed).
2777 */
2778 if (IS_LSA_SELF(new)) {
2779 /* We supposed that when LSA is originated by us, we pass the
2780 int
2781 for which it was originated. If LSA was received by flooding,
2782 the RECEIVED flag is set, so we do not link the LSA to the
2783 int. */
2784 if (CHECK_FLAG(new->flags, OSPF_LSA_RECEIVED))
2785 return new; /* ignore stale LSA */
2786
2787 ospf_lsa_unlock(&oi->network_lsa_self);
2788 oi->network_lsa_self = ospf_lsa_lock(new);
2789 ospf_refresher_register_lsa(ospf, new);
2790 }
2791 if (rt_recalc)
2792 ospf_spf_calculate_schedule(ospf, SPF_FLAG_NETWORK_LSA_INSTALL);
2793
2794 return new;
2795 }
2796
2797 /* Install summary-LSA to an area. */
2798 static struct ospf_lsa *
2799 ospf_summary_lsa_install(struct ospf *ospf, struct ospf_lsa *new, int rt_recalc)
2800 {
2801 if (rt_recalc && !IS_LSA_SELF(new)) {
2802 /* RFC 2328 Section 13.2 Summary-LSAs
2803 The best route to the destination described by the summary-
2804 LSA must be recalculated (see Section 16.5). If this
2805 destination is an AS boundary router, it may also be
2806 necessary to re-examine all the AS-external-LSAs.
2807 */
2808
2809 ospf_spf_calculate_schedule(ospf, SPF_FLAG_SUMMARY_LSA_INSTALL);
2810 }
2811
2812 if (IS_LSA_SELF(new))
2813 ospf_refresher_register_lsa(ospf, new);
2814
2815 return new;
2816 }
2817
2818 /* Install ASBR-summary-LSA to an area. */
2819 static struct ospf_lsa *ospf_summary_asbr_lsa_install(struct ospf *ospf,
2820 struct ospf_lsa *new,
2821 int rt_recalc)
2822 {
2823 if (rt_recalc && !IS_LSA_SELF(new)) {
2824 /* RFC 2328 Section 13.2 Summary-LSAs
2825 The best route to the destination described by the summary-
2826 LSA must be recalculated (see Section 16.5). If this
2827 destination is an AS boundary router, it may also be
2828 necessary to re-examine all the AS-external-LSAs.
2829 */
2830 ospf_spf_calculate_schedule(ospf,
2831 SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL);
2832 }
2833
2834 /* register LSA to refresh-list. */
2835 if (IS_LSA_SELF(new))
2836 ospf_refresher_register_lsa(ospf, new);
2837
2838 return new;
2839 }
2840
2841 /* Install AS-external-LSA. */
2842 static struct ospf_lsa *ospf_external_lsa_install(struct ospf *ospf,
2843 struct ospf_lsa *new,
2844 int rt_recalc)
2845 {
2846 ospf_ase_register_external_lsa(new, ospf);
2847 /* If LSA is not self-originated, calculate an external route. */
2848 if (rt_recalc) {
2849 /* RFC 2328 Section 13.2 AS-external-LSAs
2850 The best route to the destination described by the AS-
2851 external-LSA must be recalculated (see Section 16.6).
2852 */
2853
2854 if (!IS_LSA_SELF(new))
2855 ospf_ase_incremental_update(ospf, new);
2856 }
2857
2858 if (new->data->type == OSPF_AS_NSSA_LSA) {
2859 /* There is no point to register selforiginate Type-7 LSA for
2860 * refreshing. We rely on refreshing Type-5 LSA's
2861 */
2862 if (IS_LSA_SELF(new))
2863 return new;
2864 else {
2865 /* Try refresh type-5 translated LSA for this LSA, if
2866 * one exists.
2867 * New translations will be taken care of by the
2868 * abr_task.
2869 */
2870 ospf_translated_nssa_refresh(ospf, new, NULL);
2871 ospf_schedule_abr_task(ospf);
2872 }
2873 }
2874
2875 /* Register self-originated LSA to refresh queue.
2876 * Leave Translated LSAs alone if NSSA is enabled
2877 */
2878 if (IS_LSA_SELF(new) && !CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT))
2879 ospf_refresher_register_lsa(ospf, new);
2880
2881 return new;
2882 }
2883
2884 void ospf_discard_from_db(struct ospf *ospf, struct ospf_lsdb *lsdb,
2885 struct ospf_lsa *lsa)
2886 {
2887 struct ospf_lsa *old;
2888
2889 if (!lsdb)
2890 return;
2891
2892 old = ospf_lsdb_lookup(lsdb, lsa);
2893
2894 if (!old)
2895 return;
2896
2897 if (old->refresh_list >= 0)
2898 ospf_refresher_unregister_lsa(ospf, old);
2899
2900 switch (old->data->type) {
2901 case OSPF_AS_EXTERNAL_LSA:
2902 ospf_ase_unregister_external_lsa(old, ospf);
2903 ospf_ls_retransmit_delete_nbr_as(ospf, old);
2904 break;
2905 case OSPF_OPAQUE_AS_LSA:
2906 ospf_ls_retransmit_delete_nbr_as(ospf, old);
2907 break;
2908 case OSPF_AS_NSSA_LSA:
2909 ospf_ls_retransmit_delete_nbr_area(old->area, old);
2910 ospf_ase_unregister_external_lsa(old, ospf);
2911 break;
2912 default:
2913 ospf_ls_retransmit_delete_nbr_area(old->area, old);
2914 break;
2915 }
2916
2917 ospf_lsa_maxage_delete(ospf, old);
2918 ospf_lsa_discard(old);
2919 }
2920
2921 struct ospf_lsa *ospf_lsa_install(struct ospf *ospf, struct ospf_interface *oi,
2922 struct ospf_lsa *lsa)
2923 {
2924 struct ospf_lsa *new = NULL;
2925 struct ospf_lsa *old = NULL;
2926 struct ospf_lsdb *lsdb = NULL;
2927 int rt_recalc;
2928
2929 /* Set LSDB. */
2930 switch (lsa->data->type) {
2931 /* kevinm */
2932 case OSPF_AS_NSSA_LSA:
2933 if (lsa->area)
2934 lsdb = lsa->area->lsdb;
2935 else
2936 lsdb = ospf->lsdb;
2937 break;
2938 case OSPF_AS_EXTERNAL_LSA:
2939 case OSPF_OPAQUE_AS_LSA:
2940 lsdb = ospf->lsdb;
2941 break;
2942 default:
2943 if (lsa->area)
2944 lsdb = lsa->area->lsdb;
2945 break;
2946 }
2947
2948 assert(lsdb);
2949
2950 /* RFC 2328 13.2. Installing LSAs in the database
2951
2952 Installing a new LSA in the database, either as the result of
2953 flooding or a newly self-originated LSA, may cause the OSPF
2954 routing table structure to be recalculated. The contents of the
2955 new LSA should be compared to the old instance, if present. If
2956 there is no difference, there is no need to recalculate the
2957 routing table. When comparing an LSA to its previous instance,
2958 the following are all considered to be differences in contents:
2959
2960 o The LSA's Options field has changed.
2961
2962 o One of the LSA instances has LS age set to MaxAge, and
2963 the other does not.
2964
2965 o The length field in the LSA header has changed.
2966
2967 o The body of the LSA (i.e., anything outside the 20-byte
2968 LSA header) has changed. Note that this excludes changes
2969 in LS Sequence Number and LS Checksum.
2970
2971 */
2972 /* Look up old LSA and determine if any SPF calculation or incremental
2973 update is needed */
2974 old = ospf_lsdb_lookup(lsdb, lsa);
2975
2976 /* Do comparison and record if recalc needed. */
2977 rt_recalc = 0;
2978 if (old == NULL || ospf_lsa_different(old, lsa, false)) {
2979 /* Ref rfc3623 section 3.2.3
2980 * Installing new lsa or change in the existing LSA
2981 * or flushing existing LSA leads to topo change
2982 * and trigger SPF caculation.
2983 * So, router should be aborted from HELPER role
2984 * if it is detected as TOPO change.
2985 */
2986 if (ospf->active_restarter_cnt &&
2987 CHECK_LSA_TYPE_1_TO_5_OR_7(lsa->data->type)) {
2988 if (old == NULL || ospf_lsa_different(old, lsa, true))
2989 ospf_helper_handle_topo_chg(ospf, lsa);
2990 }
2991
2992 rt_recalc = 1;
2993 }
2994
2995 /*
2996 Sequence number check (Section 14.1 of rfc 2328)
2997 "Premature aging is used when it is time for a self-originated
2998 LSA's sequence number field to wrap. At this point, the current
2999 LSA instance (having LS sequence number MaxSequenceNumber) must
3000 be prematurely aged and flushed from the routing domain before a
3001 new instance with sequence number equal to InitialSequenceNumber
3002 can be originated. "
3003 */
3004
3005 if (ntohl(lsa->data->ls_seqnum) - 1 == OSPF_MAX_SEQUENCE_NUMBER) {
3006 if (ospf_lsa_is_self_originated(ospf, lsa)) {
3007 lsa->data->ls_seqnum = htonl(OSPF_MAX_SEQUENCE_NUMBER);
3008
3009 if (!IS_LSA_MAXAGE(lsa))
3010 lsa->flags |= OSPF_LSA_PREMATURE_AGE;
3011 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
3012
3013 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH)) {
3014 zlog_debug(
3015 "%s() Premature Aging lsa %p, seqnum 0x%x",
3016 __func__, lsa,
3017 ntohl(lsa->data->ls_seqnum));
3018 ospf_lsa_header_dump(lsa->data);
3019 }
3020 } else {
3021 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
3022 zlog_debug(
3023 "%s() got an lsa with seq 0x80000000 that was not self originated. Ignoring",
3024 __func__);
3025 ospf_lsa_header_dump(lsa->data);
3026 }
3027 return old;
3028 }
3029 }
3030
3031 /* discard old LSA from LSDB */
3032 if (old != NULL)
3033 ospf_discard_from_db(ospf, lsdb, lsa);
3034
3035 /* Calculate Checksum if self-originated?. */
3036 if (IS_LSA_SELF(lsa))
3037 ospf_lsa_checksum(lsa->data);
3038
3039 /* Insert LSA to LSDB. */
3040 ospf_lsdb_add(lsdb, lsa);
3041 lsa->lsdb = lsdb;
3042
3043 /* Do LSA specific installation process. */
3044 switch (lsa->data->type) {
3045 case OSPF_ROUTER_LSA:
3046 new = ospf_router_lsa_install(ospf, lsa, rt_recalc);
3047 break;
3048 case OSPF_NETWORK_LSA:
3049 assert(oi);
3050 new = ospf_network_lsa_install(ospf, oi, lsa, rt_recalc);
3051 break;
3052 case OSPF_SUMMARY_LSA:
3053 new = ospf_summary_lsa_install(ospf, lsa, rt_recalc);
3054 break;
3055 case OSPF_ASBR_SUMMARY_LSA:
3056 new = ospf_summary_asbr_lsa_install(ospf, lsa, rt_recalc);
3057 break;
3058 case OSPF_AS_EXTERNAL_LSA:
3059 new = ospf_external_lsa_install(ospf, lsa, rt_recalc);
3060 break;
3061 case OSPF_OPAQUE_LINK_LSA:
3062 if (IS_LSA_SELF(lsa))
3063 lsa->oi = oi; /* Specify outgoing ospf-interface for
3064 this LSA. */
3065 else {
3066 /* Incoming "oi" for this LSA has set at LSUpd
3067 * reception. */
3068 }
3069 /* Fallthrough */
3070 case OSPF_OPAQUE_AREA_LSA:
3071 case OSPF_OPAQUE_AS_LSA:
3072 new = ospf_opaque_lsa_install(lsa, rt_recalc);
3073 break;
3074 case OSPF_AS_NSSA_LSA:
3075 new = ospf_external_lsa_install(ospf, lsa, rt_recalc);
3076 default: /* type-6,8,9....nothing special */
3077 break;
3078 }
3079
3080 if (new == NULL)
3081 return new; /* Installation failed, cannot proceed further --
3082 endo. */
3083
3084 /* Debug logs. */
3085 if (IS_DEBUG_OSPF(lsa, LSA_INSTALL)) {
3086 switch (lsa->data->type) {
3087 case OSPF_AS_EXTERNAL_LSA:
3088 case OSPF_OPAQUE_AS_LSA:
3089 case OSPF_AS_NSSA_LSA:
3090 zlog_debug("LSA[%s]: Install %s", dump_lsa_key(new),
3091 lookup_msg(ospf_lsa_type_msg,
3092 new->data->type, NULL));
3093 break;
3094 default:
3095 zlog_debug("LSA[%s]: Install %s to Area %pI4",
3096 dump_lsa_key(new),
3097 lookup_msg(ospf_lsa_type_msg,
3098 new->data->type, NULL),
3099 &new->area->area_id);
3100 break;
3101 }
3102 }
3103
3104 /*
3105 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
3106 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
3107 */
3108 if (IS_LSA_MAXAGE(new)) {
3109 if (IS_DEBUG_OSPF(lsa, LSA_INSTALL))
3110 zlog_debug("LSA[%s]: Install LSA %p, MaxAge",
3111 dump_lsa_key(new), lsa);
3112 ospf_lsa_maxage(ospf, lsa);
3113 }
3114
3115 return new;
3116 }
3117
3118
3119 int ospf_check_nbr_status(struct ospf *ospf)
3120 {
3121 struct listnode *node, *nnode;
3122 struct ospf_interface *oi;
3123
3124 for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi)) {
3125 struct route_node *rn;
3126 struct ospf_neighbor *nbr;
3127
3128 if (ospf_if_is_enable(oi))
3129 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
3130 if ((nbr = rn->info) != NULL)
3131 if (nbr->state == NSM_Exchange
3132 || nbr->state == NSM_Loading) {
3133 route_unlock_node(rn);
3134 return 0;
3135 }
3136 }
3137
3138 return 1;
3139 }
3140
3141
3142 void ospf_maxage_lsa_remover(struct event *thread)
3143 {
3144 struct ospf *ospf = EVENT_ARG(thread);
3145 struct ospf_lsa *lsa, *old;
3146 struct route_node *rn;
3147 int reschedule = 0;
3148
3149 ospf->t_maxage = NULL;
3150
3151 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
3152 zlog_debug("LSA[MaxAge]: remover Start");
3153
3154 reschedule = !ospf_check_nbr_status(ospf);
3155
3156 if (!reschedule)
3157 for (rn = route_top(ospf->maxage_lsa); rn;
3158 rn = route_next(rn)) {
3159 if ((lsa = rn->info) == NULL) {
3160 continue;
3161 }
3162
3163 /* There is at least one neighbor from which we still
3164 * await an ack
3165 * for that LSA, so we are not allowed to remove it from
3166 * our lsdb yet
3167 * as per RFC 2328 section 14 para 4 a) */
3168 if (lsa->retransmit_counter > 0) {
3169 reschedule = 1;
3170 continue;
3171 }
3172
3173 /* TODO: maybe convert this function to a work-queue */
3174 if (event_should_yield(thread)) {
3175 OSPF_TIMER_ON(ospf->t_maxage,
3176 ospf_maxage_lsa_remover, 0);
3177 route_unlock_node(
3178 rn); /* route_top/route_next */
3179 return;
3180 }
3181
3182 /* Remove LSA from the LSDB */
3183 if (IS_LSA_SELF(lsa))
3184 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
3185 zlog_debug(
3186 "LSA[Type%d:%pI4]: LSA 0x%lx is self-originated: ",
3187 lsa->data->type,
3188 &lsa->data->id,
3189 (unsigned long)lsa);
3190
3191 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
3192 zlog_debug(
3193 "LSA[%s]: MaxAge LSA removed from list",
3194 dump_lsa_key(lsa));
3195
3196 if (CHECK_FLAG(lsa->flags, OSPF_LSA_PREMATURE_AGE)) {
3197 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
3198 zlog_debug(
3199 "originating new lsa for lsa %p",
3200 lsa);
3201 ospf_lsa_refresh(ospf, lsa);
3202 }
3203
3204 /* Remove from lsdb. */
3205 if (lsa->lsdb) {
3206 old = ospf_lsdb_lookup(lsa->lsdb, lsa);
3207 /* The max age LSA here must be the same
3208 * as the LSA in LSDB
3209 */
3210 if (old != lsa) {
3211 flog_err(EC_OSPF_LSA_MISSING,
3212 "%s: LSA[%s]: LSA not in LSDB",
3213 __func__, dump_lsa_key(lsa));
3214
3215 continue;
3216 }
3217 ospf_discard_from_db(ospf, lsa->lsdb, lsa);
3218 ospf_lsdb_delete(lsa->lsdb, lsa);
3219 } else {
3220 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
3221 zlog_debug(
3222 "%s: LSA[%s]: No associated LSDB!",
3223 __func__, dump_lsa_key(lsa));
3224 }
3225 }
3226
3227 /* A MaxAge LSA must be removed immediately from the router's link
3228 state database as soon as both a) it is no longer contained on any
3229 neighbor Link state retransmission lists and b) none of the
3230 router's
3231 neighbors are in states Exchange or Loading. */
3232 if (reschedule)
3233 OSPF_TIMER_ON(ospf->t_maxage, ospf_maxage_lsa_remover,
3234 ospf->maxage_delay);
3235 }
3236
3237 /* This function checks whether an LSA with initial sequence number should be
3238 * originated after a wrap in sequence number
3239 */
3240 void ospf_check_and_gen_init_seq_lsa(struct ospf_interface *oi,
3241 struct ospf_lsa *recv_lsa)
3242 {
3243 struct ospf_lsa *lsa = NULL;
3244 struct ospf *ospf = oi->ospf;
3245
3246 lsa = ospf_lsa_lookup_by_header(oi->area, recv_lsa->data);
3247
3248 if ((lsa == NULL) || (!CHECK_FLAG(lsa->flags, OSPF_LSA_PREMATURE_AGE))
3249 || (lsa->retransmit_counter != 0)) {
3250 if (IS_DEBUG_OSPF(lsa, LSA))
3251 zlog_debug(
3252 "Do not generate LSA with initial seqence number.");
3253 return;
3254 }
3255
3256 ospf_lsa_maxage_delete(ospf, lsa);
3257
3258 lsa->data->ls_seqnum = lsa_seqnum_increment(lsa);
3259
3260 ospf_lsa_refresh(ospf, lsa);
3261 }
3262
3263 void ospf_lsa_maxage_delete(struct ospf *ospf, struct ospf_lsa *lsa)
3264 {
3265 struct route_node *rn;
3266 struct prefix lsa_prefix;
3267
3268 memset(&lsa_prefix, 0, sizeof(lsa_prefix));
3269 lsa_prefix.family = AF_UNSPEC;
3270 lsa_prefix.prefixlen = sizeof(lsa_prefix.u.ptr) * CHAR_BIT;
3271 lsa_prefix.u.ptr = (uintptr_t)lsa;
3272
3273 if ((rn = route_node_lookup(ospf->maxage_lsa, &lsa_prefix))) {
3274 if (rn->info == lsa) {
3275 UNSET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
3276 ospf_lsa_unlock(&lsa); /* maxage_lsa */
3277 rn->info = NULL;
3278 route_unlock_node(
3279 rn); /* unlock node because lsa is deleted */
3280 }
3281 route_unlock_node(rn); /* route_node_lookup */
3282 } else {
3283 if (IS_DEBUG_OSPF_EVENT)
3284 zlog_debug("%s: lsa %s is not found in maxage db.",
3285 __func__, dump_lsa_key(lsa));
3286 }
3287 }
3288
3289 /* Add LSA onto the MaxAge list, and schedule for removal.
3290 * This does *not* lead to the LSA being flooded, that must be taken
3291 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
3292 * function).
3293 */
3294 void ospf_lsa_maxage(struct ospf *ospf, struct ospf_lsa *lsa)
3295 {
3296 struct prefix lsa_prefix;
3297 struct route_node *rn;
3298
3299 /* When we saw a MaxAge LSA flooded to us, we put it on the list
3300 and schedule the MaxAge LSA remover. */
3301 if (CHECK_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE)) {
3302 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
3303 zlog_debug(
3304 "LSA[%s]: %p already exists on MaxAge LSA list",
3305 dump_lsa_key(lsa), lsa);
3306 return;
3307 }
3308
3309 memset(&lsa_prefix, 0, sizeof(lsa_prefix));
3310 lsa_prefix.family = AF_UNSPEC;
3311 lsa_prefix.prefixlen = sizeof(lsa_prefix.u.ptr) * CHAR_BIT;
3312 lsa_prefix.u.ptr = (uintptr_t)lsa;
3313
3314 rn = route_node_get(ospf->maxage_lsa, &lsa_prefix);
3315 if (rn->info != NULL) {
3316 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
3317 zlog_debug(
3318 "LSA[%s]: found LSA (%p) in table for LSA %p %d",
3319 dump_lsa_key(lsa), rn->info,
3320 (void *)lsa, lsa_prefix.prefixlen);
3321 route_unlock_node(rn);
3322 } else {
3323 rn->info = ospf_lsa_lock(lsa);
3324 SET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
3325 }
3326
3327 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
3328 zlog_debug("LSA[%s]: MaxAge LSA remover scheduled.",
3329 dump_lsa_key(lsa));
3330
3331 OSPF_TIMER_ON(ospf->t_maxage, ospf_maxage_lsa_remover,
3332 ospf->maxage_delay);
3333 }
3334
3335 static int ospf_lsa_maxage_walker_remover(struct ospf *ospf,
3336 struct ospf_lsa *lsa)
3337 {
3338 /* Stay away from any Local Translated Type-7 LSAs */
3339 if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
3340 return 0;
3341
3342 if (IS_LSA_MAXAGE(lsa))
3343 /* Self-originated LSAs should NOT time-out instead,
3344 they're flushed and submitted to the max_age list explicitly.
3345 */
3346 if (!ospf_lsa_is_self_originated(ospf, lsa)) {
3347 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
3348 zlog_debug("LSA[%s]: is MaxAge",
3349 dump_lsa_key(lsa));
3350
3351 switch (lsa->data->type) {
3352 case OSPF_OPAQUE_LINK_LSA:
3353 case OSPF_OPAQUE_AREA_LSA:
3354 case OSPF_OPAQUE_AS_LSA:
3355 /*
3356 * As a general rule, whenever network topology
3357 * has changed
3358 * (due to an LSA removal in this case), routing
3359 * recalculation
3360 * should be triggered. However, this is not
3361 * true for opaque
3362 * LSAs. Even if an opaque LSA instance is going
3363 * to be removed
3364 * from the routing domain, it does not mean a
3365 * change in network
3366 * topology, and thus, routing recalculation is
3367 * not needed here.
3368 */
3369 break;
3370 case OSPF_AS_EXTERNAL_LSA:
3371 case OSPF_AS_NSSA_LSA:
3372 ospf_ase_incremental_update(ospf, lsa);
3373 break;
3374 default:
3375 ospf_spf_calculate_schedule(ospf,
3376 SPF_FLAG_MAXAGE);
3377 break;
3378 }
3379 ospf_lsa_maxage(ospf, lsa);
3380 }
3381
3382 if (IS_LSA_MAXAGE(lsa) && !ospf_lsa_is_self_originated(ospf, lsa))
3383 if (LS_AGE(lsa) > OSPF_LSA_MAXAGE + 30)
3384 printf("Eek! Shouldn't happen!\n");
3385
3386 return 0;
3387 }
3388
3389 /* Periodical check of MaxAge LSA. */
3390 void ospf_lsa_maxage_walker(struct event *thread)
3391 {
3392 struct ospf *ospf = EVENT_ARG(thread);
3393 struct route_node *rn;
3394 struct ospf_lsa *lsa;
3395 struct ospf_area *area;
3396 struct listnode *node, *nnode;
3397
3398 ospf->t_maxage_walker = NULL;
3399
3400 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
3401 LSDB_LOOP (ROUTER_LSDB(area), rn, lsa)
3402 ospf_lsa_maxage_walker_remover(ospf, lsa);
3403 LSDB_LOOP (NETWORK_LSDB(area), rn, lsa)
3404 ospf_lsa_maxage_walker_remover(ospf, lsa);
3405 LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
3406 ospf_lsa_maxage_walker_remover(ospf, lsa);
3407 LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
3408 ospf_lsa_maxage_walker_remover(ospf, lsa);
3409 LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
3410 ospf_lsa_maxage_walker_remover(ospf, lsa);
3411 LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
3412 ospf_lsa_maxage_walker_remover(ospf, lsa);
3413 LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
3414 ospf_lsa_maxage_walker_remover(ospf, lsa);
3415 }
3416
3417 /* for AS-external-LSAs. */
3418 if (ospf->lsdb) {
3419 LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
3420 ospf_lsa_maxage_walker_remover(ospf, lsa);
3421 LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
3422 ospf_lsa_maxage_walker_remover(ospf, lsa);
3423 }
3424
3425 OSPF_TIMER_ON(ospf->t_maxage_walker, ospf_lsa_maxage_walker,
3426 OSPF_LSA_MAXAGE_CHECK_INTERVAL);
3427 }
3428
3429 struct ospf_lsa *ospf_lsa_lookup_by_prefix(struct ospf_lsdb *lsdb, uint8_t type,
3430 struct prefix_ipv4 *p,
3431 struct in_addr router_id)
3432 {
3433 struct ospf_lsa *lsa;
3434 struct in_addr mask, id;
3435 struct lsa_header_mask {
3436 struct lsa_header header;
3437 struct in_addr mask;
3438 } * hmask;
3439
3440 lsa = ospf_lsdb_lookup_by_id(lsdb, type, p->prefix, router_id);
3441 if (lsa == NULL)
3442 return NULL;
3443
3444 masklen2ip(p->prefixlen, &mask);
3445
3446 hmask = (struct lsa_header_mask *)lsa->data;
3447
3448 if (mask.s_addr != hmask->mask.s_addr) {
3449 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3450 lsa = ospf_lsdb_lookup_by_id(lsdb, type, id, router_id);
3451 if (!lsa)
3452 return NULL;
3453 }
3454
3455 return lsa;
3456 }
3457
3458 struct ospf_lsa *ospf_lsa_lookup(struct ospf *ospf, struct ospf_area *area,
3459 uint32_t type, struct in_addr id,
3460 struct in_addr adv_router)
3461 {
3462 if (!ospf)
3463 return NULL;
3464
3465 switch (type) {
3466 case OSPF_ROUTER_LSA:
3467 case OSPF_NETWORK_LSA:
3468 case OSPF_SUMMARY_LSA:
3469 case OSPF_ASBR_SUMMARY_LSA:
3470 case OSPF_AS_NSSA_LSA:
3471 case OSPF_OPAQUE_LINK_LSA:
3472 case OSPF_OPAQUE_AREA_LSA:
3473 return ospf_lsdb_lookup_by_id(area->lsdb, type, id, adv_router);
3474 case OSPF_AS_EXTERNAL_LSA:
3475 case OSPF_OPAQUE_AS_LSA:
3476 return ospf_lsdb_lookup_by_id(ospf->lsdb, type, id, adv_router);
3477 default:
3478 break;
3479 }
3480
3481 return NULL;
3482 }
3483
3484 struct ospf_lsa *ospf_lsa_lookup_by_id(struct ospf_area *area, uint32_t type,
3485 struct in_addr id)
3486 {
3487 struct ospf_lsa *lsa;
3488 struct route_node *rn;
3489
3490 switch (type) {
3491 case OSPF_ROUTER_LSA:
3492 return ospf_lsdb_lookup_by_id(area->lsdb, type, id, id);
3493 case OSPF_NETWORK_LSA:
3494 for (rn = route_top(NETWORK_LSDB(area)); rn;
3495 rn = route_next(rn))
3496 if ((lsa = rn->info))
3497 if (IPV4_ADDR_SAME(&lsa->data->id, &id)) {
3498 route_unlock_node(rn);
3499 return lsa;
3500 }
3501 break;
3502 case OSPF_SUMMARY_LSA:
3503 case OSPF_ASBR_SUMMARY_LSA:
3504 /* Currently not used. */
3505 assert(1);
3506 return ospf_lsdb_lookup_by_id(area->lsdb, type, id, id);
3507 case OSPF_AS_EXTERNAL_LSA:
3508 case OSPF_AS_NSSA_LSA:
3509 case OSPF_OPAQUE_LINK_LSA:
3510 case OSPF_OPAQUE_AREA_LSA:
3511 case OSPF_OPAQUE_AS_LSA:
3512 /* Currently not used. */
3513 break;
3514 default:
3515 break;
3516 }
3517
3518 return NULL;
3519 }
3520
3521 struct ospf_lsa *ospf_lsa_lookup_by_header(struct ospf_area *area,
3522 struct lsa_header *lsah)
3523 {
3524 struct ospf_lsa *match;
3525
3526 /*
3527 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3528 * is redefined to have two subfields; opaque-type and opaque-id.
3529 * However, it is harmless to treat the two sub fields together, as if
3530 * they two were forming a unique LSA-ID.
3531 */
3532
3533 match = ospf_lsa_lookup(area->ospf, area, lsah->type, lsah->id,
3534 lsah->adv_router);
3535
3536 if (match == NULL)
3537 if (IS_DEBUG_OSPF(lsa, LSA) == OSPF_DEBUG_LSA)
3538 zlog_debug("LSA[Type%d:%pI4]: Lookup by header, NO MATCH",
3539 lsah->type, &lsah->id);
3540
3541 return match;
3542 }
3543
3544 /* return +n, l1 is more recent.
3545 return -n, l2 is more recent.
3546 return 0, l1 and l2 is identical. */
3547 int ospf_lsa_more_recent(struct ospf_lsa *l1, struct ospf_lsa *l2)
3548 {
3549 int r;
3550 int x, y;
3551
3552 if (l1 == NULL && l2 == NULL)
3553 return 0;
3554 if (l1 == NULL)
3555 return -1;
3556 if (l2 == NULL)
3557 return 1;
3558
3559 /* compare LS sequence number. */
3560 x = (int)ntohl(l1->data->ls_seqnum);
3561 y = (int)ntohl(l2->data->ls_seqnum);
3562 if (x > y)
3563 return 1;
3564 if (x < y)
3565 return -1;
3566
3567 /* compare LS checksum. */
3568 r = ntohs(l1->data->checksum) - ntohs(l2->data->checksum);
3569 if (r)
3570 return r;
3571
3572 /* compare LS age. */
3573 if (IS_LSA_MAXAGE(l1) && !IS_LSA_MAXAGE(l2))
3574 return 1;
3575 else if (!IS_LSA_MAXAGE(l1) && IS_LSA_MAXAGE(l2))
3576 return -1;
3577
3578 /* compare LS age with MaxAgeDiff. */
3579 if (LS_AGE(l1) - LS_AGE(l2) > OSPF_LSA_MAXAGE_DIFF)
3580 return -1;
3581 else if (LS_AGE(l2) - LS_AGE(l1) > OSPF_LSA_MAXAGE_DIFF)
3582 return 1;
3583
3584 /* LSAs are identical. */
3585 return 0;
3586 }
3587
3588 /*
3589 * Check if two LSAs are different.
3590 *
3591 * l1
3592 * The first LSA to compare.
3593 *
3594 * l2
3595 * The second LSA to compare.
3596 *
3597 * ignore_rcvd_flag
3598 * When set to true, ignore whether the LSAs were received from the network
3599 * or not. This parameter should be set to true when checking for topology
3600 * changes as part of the Graceful Restart helper neighbor procedures.
3601 *
3602 * Returns:
3603 * true if the LSAs are different, false otherwise.
3604 */
3605 int ospf_lsa_different(struct ospf_lsa *l1, struct ospf_lsa *l2,
3606 bool ignore_rcvd_flag)
3607 {
3608 char *p1, *p2;
3609 assert(l1);
3610 assert(l2);
3611 assert(l1->data);
3612 assert(l2->data);
3613
3614 if (l1->data->options != l2->data->options)
3615 return 1;
3616
3617 if (IS_LSA_MAXAGE(l1) && !IS_LSA_MAXAGE(l2))
3618 return 1;
3619
3620 if (IS_LSA_MAXAGE(l2) && !IS_LSA_MAXAGE(l1))
3621 return 1;
3622
3623 if (l1->size != l2->size)
3624 return 1;
3625
3626 if (l1->size == 0)
3627 return 1;
3628
3629 if (!ignore_rcvd_flag
3630 && CHECK_FLAG((l1->flags ^ l2->flags), OSPF_LSA_RECEIVED))
3631 return 1; /* May be a stale LSA in the LSBD */
3632
3633 if (l1->size == OSPF_LSA_HEADER_SIZE)
3634 return 0; /* nothing to compare */
3635
3636 p1 = (char *)l1->data;
3637 p2 = (char *)l2->data;
3638
3639 if (memcmp(p1 + OSPF_LSA_HEADER_SIZE, p2 + OSPF_LSA_HEADER_SIZE,
3640 l1->size - OSPF_LSA_HEADER_SIZE)
3641 != 0)
3642 return 1;
3643
3644 return 0;
3645 }
3646
3647 int ospf_lsa_flush_schedule(struct ospf *ospf, struct ospf_lsa *lsa)
3648 {
3649 if (lsa == NULL || !IS_LSA_SELF(lsa))
3650 return 0;
3651
3652 if (IS_DEBUG_OSPF_EVENT)
3653 zlog_debug(
3654 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3655 lsa->data->type, &lsa->data->id);
3656
3657 /* Force given lsa's age to MaxAge. */
3658 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
3659
3660 switch (lsa->data->type) {
3661 /* Opaque wants to be notified of flushes */
3662 case OSPF_OPAQUE_LINK_LSA:
3663 case OSPF_OPAQUE_AREA_LSA:
3664 case OSPF_OPAQUE_AS_LSA:
3665 ospf_opaque_lsa_refresh(lsa);
3666 break;
3667 default:
3668 ospf_refresher_unregister_lsa(ospf, lsa);
3669 ospf_lsa_flush(ospf, lsa);
3670 break;
3671 }
3672
3673 return 0;
3674 }
3675
3676 void ospf_flush_self_originated_lsas_now(struct ospf *ospf)
3677 {
3678 struct listnode *node, *nnode;
3679 struct listnode *node2, *nnode2;
3680 struct ospf_area *area;
3681 struct ospf_interface *oi;
3682 struct ospf_lsa *lsa;
3683 struct route_node *rn;
3684 struct ospf_if_params *oip;
3685 int need_to_flush_ase = 0;
3686
3687 ospf->inst_shutdown = 1;
3688
3689 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
3690 if ((lsa = area->router_lsa_self) != NULL) {
3691 if (IS_DEBUG_OSPF_EVENT)
3692 zlog_debug(
3693 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3694 lsa->data->type,
3695 &lsa->data->id);
3696
3697 ospf_refresher_unregister_lsa(ospf, lsa);
3698 ospf_lsa_flush_area(lsa, area);
3699 ospf_lsa_unlock(&area->router_lsa_self);
3700 area->router_lsa_self = NULL;
3701 }
3702
3703 for (ALL_LIST_ELEMENTS(area->oiflist, node2, nnode2, oi)) {
3704 if ((lsa = oi->network_lsa_self) != NULL
3705 && oi->state == ISM_DR && oi->full_nbrs > 0) {
3706 if (IS_DEBUG_OSPF_EVENT)
3707 zlog_debug(
3708 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3709 lsa->data->type,
3710 &lsa->data->id);
3711
3712 ospf_refresher_unregister_lsa(
3713 ospf, oi->network_lsa_self);
3714 ospf_lsa_flush_area(oi->network_lsa_self, area);
3715 ospf_lsa_unlock(&oi->network_lsa_self);
3716 oi->network_lsa_self = NULL;
3717
3718 oip = ospf_lookup_if_params(
3719 oi->ifp, oi->address->u.prefix4);
3720 if (oip)
3721 oip->network_lsa_seqnum = htonl(
3722 OSPF_INVALID_SEQUENCE_NUMBER);
3723 }
3724
3725 if (oi->type != OSPF_IFTYPE_VIRTUALLINK
3726 && area->external_routing == OSPF_AREA_DEFAULT)
3727 need_to_flush_ase = 1;
3728 }
3729
3730 LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
3731 ospf_lsa_flush_schedule(ospf, lsa);
3732 LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
3733 ospf_lsa_flush_schedule(ospf, lsa);
3734 LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
3735 ospf_lsa_flush_schedule(ospf, lsa);
3736 LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
3737 ospf_lsa_flush_schedule(ospf, lsa);
3738 }
3739
3740 if (need_to_flush_ase) {
3741 LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
3742 ospf_lsa_flush_schedule(ospf, lsa);
3743 LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
3744 ospf_lsa_flush_schedule(ospf, lsa);
3745 }
3746
3747 /*
3748 * Make sure that the MaxAge LSA remover is executed immediately,
3749 * without conflicting to other threads.
3750 */
3751 if (ospf->t_maxage != NULL) {
3752 EVENT_OFF(ospf->t_maxage);
3753 event_execute(master, ospf_maxage_lsa_remover, ospf, 0);
3754 }
3755
3756 return;
3757 }
3758
3759 /** @brief Function to refresh all the self originated
3760 * LSAs for area, when FR state change happens.
3761 * @param area pointer.
3762 * @return Void.
3763 */
3764 void ospf_refresh_area_self_lsas(struct ospf_area *area)
3765 {
3766 struct listnode *node2;
3767 struct listnode *nnode2;
3768 struct ospf_interface *oi;
3769 struct route_node *rn;
3770 struct ospf_lsa *lsa;
3771
3772 if (!area)
3773 return;
3774
3775 if (area->router_lsa_self)
3776 ospf_lsa_refresh(area->ospf, area->router_lsa_self);
3777
3778 for (ALL_LIST_ELEMENTS(area->oiflist, node2, nnode2, oi))
3779 if (oi->network_lsa_self)
3780 ospf_lsa_refresh(oi->ospf, oi->network_lsa_self);
3781
3782 LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
3783 if (IS_LSA_SELF(lsa))
3784 ospf_lsa_refresh(area->ospf, lsa);
3785 LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
3786 if (IS_LSA_SELF(lsa))
3787 ospf_lsa_refresh(area->ospf, lsa);
3788 LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
3789 if (IS_LSA_SELF(lsa))
3790 ospf_lsa_refresh(area->ospf, lsa);
3791 LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
3792 if (IS_LSA_SELF(lsa))
3793 ospf_lsa_refresh(area->ospf, lsa);
3794 LSDB_LOOP (EXTERNAL_LSDB(area->ospf), rn, lsa)
3795 if (IS_LSA_SELF(lsa))
3796 ospf_lsa_refresh(area->ospf, lsa);
3797 LSDB_LOOP (OPAQUE_AS_LSDB(area->ospf), rn, lsa)
3798 if (IS_LSA_SELF(lsa))
3799 ospf_lsa_refresh(area->ospf, lsa);
3800 }
3801
3802 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3803 /* An interface-independent version of ospf_lsa_is_self_originated */
3804 int ospf_lsa_is_self_originated(struct ospf *ospf, struct ospf_lsa *lsa)
3805 {
3806 struct listnode *node;
3807 struct ospf_interface *oi;
3808
3809 /* This LSA is already checked. */
3810 if (CHECK_FLAG(lsa->flags, OSPF_LSA_SELF_CHECKED))
3811 return IS_LSA_SELF(lsa);
3812
3813 /* Make sure LSA is self-checked. */
3814 SET_FLAG(lsa->flags, OSPF_LSA_SELF_CHECKED);
3815
3816 /* AdvRouter and Router ID is the same. */
3817 if (IPV4_ADDR_SAME(&lsa->data->adv_router, &ospf->router_id))
3818 SET_FLAG(lsa->flags, OSPF_LSA_SELF);
3819
3820 /* LSA is router-LSA. */
3821 else if (lsa->data->type == OSPF_ROUTER_LSA
3822 && IPV4_ADDR_SAME(&lsa->data->id, &ospf->router_id))
3823 SET_FLAG(lsa->flags, OSPF_LSA_SELF);
3824
3825 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3826 else if (lsa->data->type == OSPF_NETWORK_LSA)
3827 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
3828 /* Ignore virtual link. */
3829 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
3830 if (oi->address->family == AF_INET)
3831 if (IPV4_ADDR_SAME(
3832 &lsa->data->id,
3833 &oi->address->u.prefix4)) {
3834 /* to make it easier later */
3835 SET_FLAG(lsa->flags,
3836 OSPF_LSA_SELF);
3837 return IS_LSA_SELF(lsa);
3838 }
3839 }
3840
3841 return IS_LSA_SELF(lsa);
3842 }
3843
3844 /* Get unique Link State ID. */
3845 enum lsid_status ospf_lsa_unique_id(struct ospf *ospf, struct ospf_lsdb *lsdb,
3846 uint8_t type, struct prefix_ipv4 *p,
3847 struct in_addr *id)
3848 {
3849 struct ospf_lsa *lsa;
3850 struct in_addr mask;
3851
3852 *id = p->prefix;
3853
3854 /* Check existence of LSA instance. */
3855 lsa = ospf_lsdb_lookup_by_id(lsdb, type, *id, ospf->router_id);
3856 if (lsa) {
3857 struct as_external_lsa *al =
3858 (struct as_external_lsa *)lsa->data;
3859 /* Ref rfc2328,Appendex E.1
3860 * If router already originated the external lsa with lsid
3861 * as the current prefix, and the masklens are same then
3862 * terminate the LSID algorithem.
3863 */
3864 if (ip_masklen(al->mask) == p->prefixlen) {
3865 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
3866 zlog_debug(
3867 "%s: Can't get Link State ID for %pFX",
3868 __func__, p);
3869 /* id.s_addr = 0; */
3870 id->s_addr = 0xffffffff;
3871 return LSID_NOT_AVAILABLE;
3872 } else if (ip_masklen(al->mask) < p->prefixlen) {
3873 /* Ref rfc2328,Appendex E.2
3874 * the current prefix masklen is greater than the
3875 * existing LSA, then generate the Link state ID,
3876 * by setting all host bits in prefix addressa and
3877 * originate.
3878 *
3879 * Eg: 1st Route : 10.0.0.0/16 - LSID:10.0.0.0
3880 * 2nd Route : 10.0.0.0/24 - LSID:10.0.0.255
3881 */
3882 masklen2ip(p->prefixlen, &mask);
3883
3884 id->s_addr = p->prefix.s_addr | (~mask.s_addr);
3885 lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, type, *id,
3886 ospf->router_id);
3887 if (lsa) {
3888 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
3889 zlog_debug(
3890 "%s: Can't get Link State ID for %pFX",
3891 __func__, p);
3892 id->s_addr = 0xffffffff;
3893 return LSID_NOT_AVAILABLE;
3894 }
3895 } else {
3896 /* Ref rfc2328,Appendex E.3
3897 * the current prefix masklen is lesser than the
3898 * existing LSA,then the originated LSA has to be
3899 * refreshed by modifying masklen, cost and tag.
3900 * Originate the old route info with new LSID by
3901 * setting the host bits in prefix address.
3902 *
3903 * Eg: 1st Route : 10.0.0.0/24 - LSID:10.0.0.0
3904 * 2nd Route : 10.0.0.0/16 - ?
3905 * Since 2nd route mask len is less than firstone
3906 * LSID has to be changed.
3907 * 1st route LSID:10.0.0.255
3908 * 2nd route LSID:10.0.0.0
3909 */
3910 id->s_addr = lsa->data->id.s_addr | (~al->mask.s_addr);
3911 lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, type, *id,
3912 ospf->router_id);
3913 if (lsa && (ip_masklen(al->mask) != IPV4_MAX_BITLEN)) {
3914 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
3915 zlog_debug(
3916 "%s: Can't get Link State ID for %pFX",
3917 __func__, p);
3918 id->s_addr = 0xffffffff;
3919 return LSID_NOT_AVAILABLE;
3920 }
3921 return LSID_CHANGE;
3922 }
3923 }
3924
3925 return LSID_AVAILABLE;
3926 }
3927
3928
3929 #define LSA_ACTION_FLOOD_AREA 1
3930 #define LSA_ACTION_FLUSH_AREA 2
3931
3932 struct lsa_action {
3933 uint8_t action;
3934 struct ospf_area *area;
3935 struct ospf_lsa *lsa;
3936 };
3937
3938 static void ospf_lsa_action(struct event *t)
3939 {
3940 struct lsa_action *data;
3941
3942 data = EVENT_ARG(t);
3943
3944 if (IS_DEBUG_OSPF(lsa, LSA) == OSPF_DEBUG_LSA)
3945 zlog_debug("LSA[Action]: Performing scheduled LSA action: %d",
3946 data->action);
3947
3948 switch (data->action) {
3949 case LSA_ACTION_FLOOD_AREA:
3950 ospf_flood_through_area(data->area, NULL, data->lsa);
3951 break;
3952 case LSA_ACTION_FLUSH_AREA:
3953 ospf_lsa_flush_area(data->lsa, data->area);
3954 break;
3955 }
3956
3957 ospf_lsa_unlock(&data->lsa); /* Message */
3958 XFREE(MTYPE_OSPF_MESSAGE, data);
3959 }
3960
3961 void ospf_schedule_lsa_flood_area(struct ospf_area *area, struct ospf_lsa *lsa)
3962 {
3963 struct lsa_action *data;
3964
3965 data = XCALLOC(MTYPE_OSPF_MESSAGE, sizeof(struct lsa_action));
3966 data->action = LSA_ACTION_FLOOD_AREA;
3967 data->area = area;
3968 data->lsa = ospf_lsa_lock(lsa); /* Message / Flood area */
3969
3970 event_add_event(master, ospf_lsa_action, data, 0, NULL);
3971 }
3972
3973 void ospf_schedule_lsa_flush_area(struct ospf_area *area, struct ospf_lsa *lsa)
3974 {
3975 struct lsa_action *data;
3976
3977 data = XCALLOC(MTYPE_OSPF_MESSAGE, sizeof(struct lsa_action));
3978 data->action = LSA_ACTION_FLUSH_AREA;
3979 data->area = area;
3980 data->lsa = ospf_lsa_lock(lsa); /* Message / Flush area */
3981
3982 event_add_event(master, ospf_lsa_action, data, 0, NULL);
3983 }
3984
3985
3986 /* LSA Refreshment functions. */
3987 struct ospf_lsa *ospf_lsa_refresh(struct ospf *ospf, struct ospf_lsa *lsa)
3988 {
3989 struct external_info *ei;
3990 struct ospf_external_aggr_rt *aggr;
3991 struct ospf_lsa *new = NULL;
3992 struct as_external_lsa *al;
3993 struct prefix_ipv4 p;
3994
3995 assert(CHECK_FLAG(lsa->flags, OSPF_LSA_SELF));
3996 assert(IS_LSA_SELF(lsa));
3997 assert(lsa->lock > 0);
3998
3999 switch (lsa->data->type) {
4000 /* Router and Network LSAs are processed differently. */
4001 case OSPF_ROUTER_LSA:
4002 new = ospf_router_lsa_refresh(lsa);
4003 break;
4004 case OSPF_NETWORK_LSA:
4005 new = ospf_network_lsa_refresh(lsa);
4006 break;
4007 case OSPF_SUMMARY_LSA:
4008 new = ospf_summary_lsa_refresh(ospf, lsa);
4009 break;
4010 case OSPF_ASBR_SUMMARY_LSA:
4011 new = ospf_summary_asbr_lsa_refresh(ospf, lsa);
4012 break;
4013 case OSPF_AS_EXTERNAL_LSA:
4014 /* Translated from NSSA Type-5s are refreshed when
4015 * from refresh of Type-7 - do not refresh these directly.
4016 */
4017
4018 al = (struct as_external_lsa *)lsa->data;
4019 p.family = AF_INET;
4020 p.prefixlen = ip_masklen(al->mask);
4021 p.prefix = lsa->data->id;
4022
4023 if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
4024 break;
4025 ei = ospf_external_info_check(ospf, lsa);
4026 if (ei)
4027 new = ospf_external_lsa_refresh(
4028 ospf, lsa, ei, LSA_REFRESH_FORCE, false);
4029 else {
4030 aggr = (struct ospf_external_aggr_rt *)
4031 ospf_extrenal_aggregator_lookup(ospf, &p);
4032 if (aggr) {
4033 struct external_info ei_aggr;
4034
4035 memset(&ei_aggr, 0, sizeof(ei_aggr));
4036 ei_aggr.p = aggr->p;
4037 ei_aggr.tag = aggr->tag;
4038 ei_aggr.instance = ospf->instance;
4039 ei_aggr.route_map_set.metric = -1;
4040 ei_aggr.route_map_set.metric_type = -1;
4041
4042 ospf_external_lsa_refresh(ospf, lsa, &ei_aggr,
4043 LSA_REFRESH_FORCE, true);
4044 SET_FLAG(aggr->flags,
4045 OSPF_EXTERNAL_AGGRT_ORIGINATED);
4046 } else
4047 ospf_lsa_flush_as(ospf, lsa);
4048 }
4049 break;
4050 case OSPF_OPAQUE_LINK_LSA:
4051 case OSPF_OPAQUE_AREA_LSA:
4052 case OSPF_OPAQUE_AS_LSA:
4053 new = ospf_opaque_lsa_refresh(lsa);
4054 break;
4055 default:
4056 break;
4057 }
4058 return new;
4059 }
4060
4061 void ospf_refresher_register_lsa(struct ospf *ospf, struct ospf_lsa *lsa)
4062 {
4063 uint16_t index, current_index;
4064
4065 assert(lsa->lock > 0);
4066 assert(IS_LSA_SELF(lsa));
4067
4068 if (lsa->refresh_list < 0) {
4069 int delay;
4070 int min_delay =
4071 ospf->lsa_refresh_timer - (2 * OSPF_LS_REFRESH_JITTER);
4072 int max_delay =
4073 ospf->lsa_refresh_timer - OSPF_LS_REFRESH_JITTER;
4074
4075 /* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which
4076 * is
4077 * 1800s. Use jitter so that we send the LSA sometime between
4078 * 1680s
4079 * and 1740s.
4080 */
4081 delay = (frr_weak_random() % (max_delay - min_delay))
4082 + min_delay;
4083
4084 current_index = ospf->lsa_refresh_queue.index
4085 + (monotime(NULL) - ospf->lsa_refresher_started)
4086 / OSPF_LSA_REFRESHER_GRANULARITY;
4087
4088 index = (current_index + delay / OSPF_LSA_REFRESHER_GRANULARITY)
4089 % (OSPF_LSA_REFRESHER_SLOTS);
4090
4091 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
4092 zlog_debug(
4093 "LSA[Refresh:Type%d:%pI4]: age %d, added to index %d",
4094 lsa->data->type, &lsa->data->id,
4095 LS_AGE(lsa), index);
4096
4097 if (!ospf->lsa_refresh_queue.qs[index])
4098 ospf->lsa_refresh_queue.qs[index] = list_new();
4099
4100 listnode_add(ospf->lsa_refresh_queue.qs[index],
4101 ospf_lsa_lock(lsa)); /* lsa_refresh_queue */
4102 lsa->refresh_list = index;
4103
4104 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
4105 zlog_debug(
4106 "LSA[Refresh:Type%d:%pI4]: %s: setting refresh_list on lsa %p (slot %d)",
4107 lsa->data->type, &lsa->data->id, __func__,
4108 (void *)lsa, index);
4109 }
4110 }
4111
4112 void ospf_refresher_unregister_lsa(struct ospf *ospf, struct ospf_lsa *lsa)
4113 {
4114 assert(lsa->lock > 0);
4115 assert(IS_LSA_SELF(lsa));
4116 if (lsa->refresh_list >= 0) {
4117 struct list *refresh_list =
4118 ospf->lsa_refresh_queue.qs[lsa->refresh_list];
4119 listnode_delete(refresh_list, lsa);
4120 if (!listcount(refresh_list)) {
4121 list_delete(&refresh_list);
4122 ospf->lsa_refresh_queue.qs[lsa->refresh_list] = NULL;
4123 }
4124 lsa->refresh_list = -1;
4125 ospf_lsa_unlock(&lsa); /* lsa_refresh_queue */
4126 }
4127 }
4128
4129 void ospf_lsa_refresh_walker(struct event *t)
4130 {
4131 struct list *refresh_list;
4132 struct listnode *node, *nnode;
4133 struct ospf *ospf = EVENT_ARG(t);
4134 struct ospf_lsa *lsa;
4135 int i;
4136 struct list *lsa_to_refresh = list_new();
4137 bool dna_lsa;
4138
4139 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
4140 zlog_debug("LSA[Refresh]: %s: start", __func__);
4141
4142
4143 i = ospf->lsa_refresh_queue.index;
4144
4145 /* Note: if clock has jumped backwards, then time change could be
4146 negative,
4147 so we are careful to cast the expression to unsigned before taking
4148 modulus. */
4149 ospf->lsa_refresh_queue.index =
4150 ((unsigned long)(ospf->lsa_refresh_queue.index
4151 + (monotime(NULL)
4152 - ospf->lsa_refresher_started)
4153 / OSPF_LSA_REFRESHER_GRANULARITY))
4154 % OSPF_LSA_REFRESHER_SLOTS;
4155
4156 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
4157 zlog_debug("LSA[Refresh]: %s: next index %d", __func__,
4158 ospf->lsa_refresh_queue.index);
4159
4160 for (; i != ospf->lsa_refresh_queue.index;
4161 i = (i + 1) % OSPF_LSA_REFRESHER_SLOTS) {
4162 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
4163 zlog_debug("LSA[Refresh]: %s: refresh index %d",
4164 __func__, i);
4165
4166 refresh_list = ospf->lsa_refresh_queue.qs[i];
4167
4168 assert(i >= 0);
4169
4170 ospf->lsa_refresh_queue.qs[i] = NULL;
4171
4172 if (refresh_list) {
4173 for (ALL_LIST_ELEMENTS(refresh_list, node, nnode,
4174 lsa)) {
4175 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
4176 zlog_debug(
4177 "LSA[Refresh:Type%d:%pI4]: %s: refresh lsa %p (slot %d)",
4178 lsa->data->type, &lsa->data->id,
4179 __func__, (void *)lsa, i);
4180
4181 assert(lsa->lock > 0);
4182 list_delete_node(refresh_list, node);
4183 lsa->refresh_list = -1;
4184 listnode_add(lsa_to_refresh, lsa);
4185 }
4186 list_delete(&refresh_list);
4187 }
4188 }
4189
4190 ospf->t_lsa_refresher = NULL;
4191 event_add_timer(master, ospf_lsa_refresh_walker, ospf,
4192 ospf->lsa_refresh_interval, &ospf->t_lsa_refresher);
4193 ospf->lsa_refresher_started = monotime(NULL);
4194
4195 for (ALL_LIST_ELEMENTS(lsa_to_refresh, node, nnode, lsa)) {
4196 dna_lsa = ospf_check_dna_lsa(lsa);
4197 if (!dna_lsa) { /* refresh only non-DNA LSAs */
4198 ospf_lsa_refresh(ospf, lsa);
4199 }
4200 assert(lsa->lock > 0);
4201 ospf_lsa_unlock(&lsa); /* lsa_refresh_queue & temp for
4202 * lsa_to_refresh.
4203 */
4204 }
4205
4206 list_delete(&lsa_to_refresh);
4207
4208 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
4209 zlog_debug("LSA[Refresh]: %s: end", __func__);
4210 }
4211
4212 /* Flush the LSAs for the specific area */
4213 void ospf_flush_lsa_from_area(struct ospf *ospf, struct in_addr area_id,
4214 int type)
4215 {
4216 struct ospf_area *area;
4217 struct route_node *rn;
4218 struct ospf_lsa *lsa;
4219
4220 area = ospf_area_get(ospf, area_id);
4221
4222 switch (type) {
4223 case OSPF_AS_EXTERNAL_LSA:
4224 if ((area->external_routing == OSPF_AREA_NSSA) ||
4225 (area->external_routing == OSPF_AREA_STUB)) {
4226 LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
4227 if (IS_LSA_SELF(lsa) &&
4228 !(CHECK_FLAG(lsa->flags,
4229 OSPF_LSA_LOCAL_XLT)))
4230 ospf_lsa_flush_area(lsa, area);
4231 }
4232 break;
4233 case OSPF_AS_NSSA_LSA:
4234 LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
4235 if (IS_LSA_SELF(lsa))
4236 ospf_lsa_flush_area(lsa, area);
4237 break;
4238 default:
4239 break;
4240 }
4241 }