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