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