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