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